public static void genClosureFields( CalculatedClosure closure, ClassBuilder v, JetTypeMapper typeMapper) { ClassifierDescriptor captureThis = closure.getCaptureThis(); int access = NO_FLAG_PACKAGE_PRIVATE | ACC_SYNTHETIC | ACC_FINAL; if (captureThis != null) { v.newField( null, access, CAPTURED_THIS_FIELD, typeMapper.mapType(captureThis).getDescriptor(), null, null); } ClassifierDescriptor captureReceiver = closure.getCaptureReceiver(); if (captureReceiver != null) { v.newField( null, access, CAPTURED_RECEIVER_FIELD, typeMapper.mapType(captureReceiver).getDescriptor(), null, null); } List<Pair<String, Type>> fields = closure.getRecordedFields(); for (Pair<String, Type> field : fields) { v.newField(null, access, field.first, field.second.getDescriptor(), null, null); } }
@NotNull protected ExpressionCodegen createOrGetClInitCodegen() { DeclarationDescriptor descriptor = context.getContextDescriptor(); assert state.getClassBuilderMode() == ClassBuilderMode.FULL : "<clinit> should not be generated for light classes. Descriptor: " + descriptor; if (clInit == null) { MethodVisitor mv = v.newMethod(null, ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode(); SimpleFunctionDescriptorImpl clInit = SimpleFunctionDescriptorImpl.create( descriptor, Annotations.EMPTY, Name.special("<clinit>"), SYNTHESIZED); clInit.initialize( null, null, Collections.<TypeParameterDescriptor>emptyList(), Collections.<ValueParameterDescriptor>emptyList(), null, null, Visibilities.PRIVATE); this.clInit = new ExpressionCodegen( mv, new FrameMap(), Type.VOID_TYPE, context.intoFunction(clInit), state, this); } return clInit; }
private void done() { if (clInit != null) { clInit.v.visitInsn(RETURN); FunctionCodegen.endVisit(clInit.v, "static initializer", element); } v.done(); }
protected void generatePropertyMetadataArrayFieldIfNeeded(@NotNull Type thisAsmType) { List<JetProperty> delegatedProperties = new ArrayList<JetProperty>(); for (JetDeclaration declaration : ((JetDeclarationContainer) element).getDeclarations()) { if (declaration instanceof JetProperty) { JetProperty property = (JetProperty) declaration; if (property.getDelegate() != null) { delegatedProperties.add(property); } } } if (delegatedProperties.isEmpty()) return; v.newField( null, ACC_PRIVATE | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, JvmAbi.PROPERTY_METADATA_ARRAY_NAME, "[" + PROPERTY_METADATA_TYPE, null, null); InstructionAdapter iv = createOrGetClInitCodegen().v; iv.iconst(delegatedProperties.size()); iv.newarray(PROPERTY_METADATA_TYPE); for (int i = 0, size = delegatedProperties.size(); i < size; i++) { VariableDescriptor property = BindingContextUtils.getNotNull(bindingContext, VARIABLE, delegatedProperties.get(i)); iv.dup(); iv.iconst(i); iv.anew(PROPERTY_METADATA_IMPL_TYPE); iv.dup(); iv.visitLdcInsn(property.getName().asString()); iv.invokespecial( PROPERTY_METADATA_IMPL_TYPE.getInternalName(), "<init>", "(Ljava/lang/String;)V"); iv.astore(PROPERTY_METADATA_IMPL_TYPE); } iv.putstatic( thisAsmType.getInternalName(), JvmAbi.PROPERTY_METADATA_ARRAY_NAME, "[" + PROPERTY_METADATA_TYPE); }
private void generateBackingField(JetProperty p, PropertyDescriptor propertyDescriptor) { if (state.getBindingContext().get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor)) { DeclarationDescriptor containingDeclaration = propertyDescriptor.getContainingDeclaration(); if (CodegenUtil.isInterface(containingDeclaration)) return; Object value = null; final JetExpression initializer = p.getInitializer(); if (initializer != null) { if (initializer instanceof JetConstantExpression) { CompileTimeConstant<?> compileTimeValue = state.getBindingContext().get(BindingContext.COMPILE_TIME_VALUE, initializer); value = compileTimeValue != null ? compileTimeValue.getValue() : null; } } int modifiers; if (kind == OwnerKind.NAMESPACE) { int access = JetTypeMapper.getAccessModifiers(propertyDescriptor, 0); modifiers = access | Opcodes.ACC_STATIC; } else { modifiers = JetTypeMapper.getAccessModifiers(propertyDescriptor, 0); } if (!propertyDescriptor.isVar()) { modifiers |= Opcodes.ACC_FINAL; } if (state.getInjector().getJetStandardLibrary().isVolatile(propertyDescriptor)) { modifiers |= Opcodes.ACC_VOLATILE; } Type type = state .getInjector() .getJetTypeMapper() .mapType(propertyDescriptor.getType(), MapTypeMode.VALUE); FieldVisitor fieldVisitor = v.newField(p, modifiers, p.getName(), type.getDescriptor(), null, value); AnnotationCodegen.forField(fieldVisitor, state.getInjector().getJetTypeMapper()) .genAnnotations(propertyDescriptor); } }
public void generateDefaultSetter( PropertyDescriptor propertyDescriptor, int flags, PsiElement origin) { if (propertyDescriptor.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) { throw new IllegalStateException("must not generate code for fake overrides"); } if (kind == OwnerKind.TRAIT_IMPL) { return; } if (kind == OwnerKind.NAMESPACE) { flags |= Opcodes.ACC_STATIC; } PsiElement psiElement = state .getBindingContext() .get( BindingContext.DESCRIPTOR_TO_DECLARATION, propertyDescriptor.getContainingDeclaration()); boolean isTrait = psiElement instanceof JetClass && ((JetClass) psiElement).isTrait(); if (isTrait && !(kind instanceof OwnerKind.DelegateKind)) flags |= Opcodes.ACC_ABSTRACT; if (propertyDescriptor.getModality() == Modality.FINAL) { flags |= Opcodes.ACC_FINAL; } JvmPropertyAccessorSignature signature = state.getInjector().getJetTypeMapper().mapSetterSignature(propertyDescriptor, kind); final String descriptor = signature.getJvmMethodSignature().getAsmMethod().getDescriptor(); MethodVisitor mv = v.newMethod( origin, flags, setterName(propertyDescriptor.getName()), descriptor, null, null); generateJetPropertyAnnotation( mv, signature.getPropertyTypeKotlinSignature(), signature.getJvmMethodSignature().getKotlinTypeParameter()); if (propertyDescriptor.getSetter() != null) { assert !propertyDescriptor.getSetter().hasBody(); AnnotationCodegen.forMethod(mv, state.getInjector().getJetTypeMapper()) .genAnnotations(propertyDescriptor.getSetter()); } if (v.generateCode() != ClassBuilder.Mode.SIGNATURES && (!isTrait || kind instanceof OwnerKind.DelegateKind)) { if (propertyDescriptor.getModality() != Modality.ABSTRACT) { mv.visitCode(); if (v.generateCode() == ClassBuilder.Mode.STUBS) { StubCodegen.generateStubThrow(mv); } else { InstructionAdapter iv = new InstructionAdapter(mv); final Type type = state .getInjector() .getJetTypeMapper() .mapType(propertyDescriptor.getType(), MapTypeMode.VALUE); int paramCode = 0; if (kind != OwnerKind.NAMESPACE) { iv.load(0, JetTypeMapper.TYPE_OBJECT); paramCode = 1; } if ((kind instanceof OwnerKind.DelegateKind) != (propertyDescriptor.getKind() == FunctionDescriptor.Kind.DELEGATION)) { throw new IllegalStateException("mismatching kind in " + propertyDescriptor); } if (kind instanceof OwnerKind.DelegateKind) { OwnerKind.DelegateKind dk = (OwnerKind.DelegateKind) kind; iv.load(0, JetTypeMapper.TYPE_OBJECT); dk.getDelegate().put(JetTypeMapper.TYPE_OBJECT, iv); iv.load(paramCode, type); iv.invokeinterface( dk.getOwnerClass(), setterName(propertyDescriptor.getName()), descriptor); } else { iv.load(paramCode, type); iv.visitFieldInsn( kind == OwnerKind.NAMESPACE ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, state.getInjector().getJetTypeMapper().getOwner(propertyDescriptor, kind), propertyDescriptor.getName(), type.getDescriptor()); } iv.visitInsn(Opcodes.RETURN); } } FunctionCodegen.endVisit(mv, "setter", origin); } }