Search in sources :

Example 1 with MutableClosure

use of org.jetbrains.kotlin.codegen.binding.MutableClosure in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateClosureInitialization.

private void generateClosureInitialization(@NotNull InstructionAdapter iv) {
    MutableClosure closure = context.closure;
    if (closure != null) {
        List<FieldInfo> argsFromClosure = ClosureCodegen.calculateConstructorParameters(typeMapper, closure, classAsmType);
        int k = 1;
        for (FieldInfo info : argsFromClosure) {
            k = AsmUtil.genAssignInstanceFieldFromParam(info, k, iv);
        }
    }
}
Also used : MutableClosure(org.jetbrains.kotlin.codegen.binding.MutableClosure)

Example 2 with MutableClosure

use of org.jetbrains.kotlin.codegen.binding.MutableClosure in project kotlin by JetBrains.

the class KotlinTypeMapper method writeAdditionalConstructorParameters.

private void writeAdditionalConstructorParameters(@NotNull ClassConstructorDescriptor descriptor, @NotNull JvmSignatureWriter sw) {
    boolean isSynthesized = descriptor.getKind() == CallableMemberDescriptor.Kind.SYNTHESIZED;
    //if (isSynthesized) return;
    MutableClosure closure = bindingContext.get(CodegenBinding.CLOSURE, descriptor.getContainingDeclaration());
    ClassDescriptor captureThis = getDispatchReceiverParameterForConstructorCall(descriptor, closure);
    if (!isSynthesized && captureThis != null) {
        writeParameter(sw, JvmMethodParameterKind.OUTER, captureThis.getDefaultType(), descriptor);
    }
    KotlinType captureReceiverType = closure != null ? closure.getCaptureReceiverType() : null;
    if (captureReceiverType != null) {
        writeParameter(sw, JvmMethodParameterKind.RECEIVER, captureReceiverType, descriptor);
    }
    ClassDescriptor containingDeclaration = descriptor.getContainingDeclaration();
    if (!isSynthesized) {
        if (containingDeclaration.getKind() == ClassKind.ENUM_CLASS || containingDeclaration.getKind() == ClassKind.ENUM_ENTRY) {
            writeParameter(sw, JvmMethodParameterKind.ENUM_NAME_OR_ORDINAL, DescriptorUtilsKt.getBuiltIns(descriptor).getStringType(), descriptor);
            writeParameter(sw, JvmMethodParameterKind.ENUM_NAME_OR_ORDINAL, DescriptorUtilsKt.getBuiltIns(descriptor).getIntType(), descriptor);
        }
    }
    if (closure == null)
        return;
    for (DeclarationDescriptor variableDescriptor : closure.getCaptureVariables().keySet()) {
        Type type;
        if (variableDescriptor instanceof VariableDescriptor && !(variableDescriptor instanceof PropertyDescriptor)) {
            Type sharedVarType = getSharedVarType(variableDescriptor);
            if (sharedVarType == null) {
                if (isDelegatedLocalVariable(variableDescriptor)) {
                    VariableDescriptor delegateVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_DELEGATE, (VariableDescriptor) variableDescriptor);
                    assert delegateVariableDescriptor != null : "Local delegated property " + variableDescriptor + " delegate descriptor should be not null";
                    sharedVarType = mapType(delegateVariableDescriptor.getType());
                } else {
                    sharedVarType = mapType(((VariableDescriptor) variableDescriptor).getType());
                }
            }
            type = sharedVarType;
        } else if (isLocalFunction(variableDescriptor)) {
            //noinspection CastConflictsWithInstanceof
            type = asmTypeForAnonymousClass(bindingContext, (FunctionDescriptor) variableDescriptor);
        } else {
            type = null;
        }
        if (type != null) {
            closure.setCapturedParameterOffsetInConstructor(variableDescriptor, sw.getCurrentSignatureSize() + 1);
            writeParameter(sw, JvmMethodParameterKind.CAPTURED_LOCAL_VARIABLE, type);
        }
    }
    // because such classes are not accessible from the outside world
    if (classBuilderMode.generateBodies) {
        ResolvedCall<ConstructorDescriptor> superCall = findFirstDelegatingSuperCall(descriptor);
        if (superCall == null)
            return;
        writeSuperConstructorCallParameters(sw, descriptor, superCall, captureThis != null);
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) DeserializedClassDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor) FunctionClassDescriptor(org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor) JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) MutableClosure(org.jetbrains.kotlin.codegen.binding.MutableClosure) TypeAliasConstructorDescriptor(org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor) LocalVariableDescriptor(org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)

Aggregations

MutableClosure (org.jetbrains.kotlin.codegen.binding.MutableClosure)2 FunctionClassDescriptor (org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor)1 LocalVariableDescriptor (org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)1 TypeAliasConstructorDescriptor (org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor)1 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)1 DeserializedClassDescriptor (org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor)1 Type (org.jetbrains.org.objectweb.asm.Type)1