Search in sources :

Example 16 with Type

use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.

the class ExpressionCodegen method generateFor.

private void generateFor(@NotNull KtForExpression forExpression) {
    ResolvedCall<? extends CallableDescriptor> loopRangeCall = RangeCodegenUtil.getLoopRangeResolvedCall(forExpression, bindingContext);
    if (loopRangeCall != null) {
        AbstractForLoopGenerator optimizedForLoopGenerator = createOptimizedForLoopGeneratorOrNull(forExpression, loopRangeCall);
        if (optimizedForLoopGenerator != null) {
            generateForLoop(optimizedForLoopGenerator);
            return;
        }
    }
    KtExpression loopRange = forExpression.getLoopRange();
    assert loopRange != null;
    KotlinType loopRangeType = bindingContext.getType(loopRange);
    assert loopRangeType != null;
    Type asmLoopRangeType = asmType(loopRangeType);
    if (asmLoopRangeType.getSort() == Type.ARRAY) {
        generateForLoop(new ForInArrayLoopGenerator(forExpression));
    } else if (RangeCodegenUtil.isRange(loopRangeType)) {
        generateForLoop(new ForInRangeInstanceLoopGenerator(forExpression));
    } else if (RangeCodegenUtil.isProgression(loopRangeType)) {
        generateForLoop(new ForInProgressionExpressionLoopGenerator(forExpression));
    } else {
        generateForLoop(new IteratorForLoopGenerator(forExpression));
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 17 with Type

use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generatePrimaryConstructorImpl.

private void generatePrimaryConstructorImpl(@NotNull ClassConstructorDescriptor constructorDescriptor, @NotNull ExpressionCodegen codegen, @NotNull DelegationFieldsInfo fieldsInfo, @Nullable KtPrimaryConstructor primaryConstructor) {
    InstructionAdapter iv = codegen.v;
    markLineNumberForConstructor(constructorDescriptor, primaryConstructor, codegen);
    generateClosureInitialization(iv);
    generateDelegatorToConstructorCall(iv, codegen, constructorDescriptor, getDelegationConstructorCall(bindingContext, constructorDescriptor));
    if (isNonCompanionObject(descriptor)) {
        StackValue.singletonViaInstance(descriptor, typeMapper).store(StackValue.LOCAL_0, iv);
    }
    for (KtSuperTypeListEntry specifier : myClass.getSuperTypeListEntries()) {
        if (specifier instanceof KtDelegatedSuperTypeEntry) {
            genCallToDelegatorByExpressionSpecifier(iv, codegen, (KtDelegatedSuperTypeEntry) specifier, fieldsInfo);
        }
    }
    int curParam = 0;
    List<ValueParameterDescriptor> parameters = constructorDescriptor.getValueParameters();
    for (KtParameter parameter : getPrimaryConstructorParameters()) {
        if (parameter.hasValOrVar()) {
            VariableDescriptor descriptor = parameters.get(curParam);
            Type type = typeMapper.mapType(descriptor);
            iv.load(0, classAsmType);
            iv.load(codegen.myFrameMap.getIndex(descriptor), type);
            PropertyDescriptor propertyDescriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
            assert propertyDescriptor != null : "Property descriptor is not found for primary constructor parameter: " + parameter;
            iv.putfield(classAsmType.getInternalName(), context.getFieldName(propertyDescriptor, false), type.getDescriptor());
        }
        curParam++;
    }
    if (isCompanionObject(descriptor)) {
        ImplementationBodyCodegen parentCodegen = (ImplementationBodyCodegen) getParentCodegen();
        parentCodegen.generateCompanionObjectInitializer(descriptor);
    }
    if (JvmAbi.isCompanionObjectWithBackingFieldsInOuter(descriptor)) {
        final ImplementationBodyCodegen parentCodegen = (ImplementationBodyCodegen) getParentCodegen();
        generateInitializers(new Function0<ExpressionCodegen>() {

            @Override
            public ExpressionCodegen invoke() {
                return parentCodegen.createOrGetClInitCodegen();
            }
        });
    } else {
        generateInitializers(codegen);
    }
    iv.visitInsn(RETURN);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)

Example 18 with Type

use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.

the class ImplementationBodyCodegen method signature.

@NotNull
public static JvmClassSignature signature(@NotNull ClassDescriptor descriptor, @NotNull Type classAsmType, @NotNull SuperClassInfo superClassInfo, @NotNull KotlinTypeMapper typeMapper) {
    JvmSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS);
    typeMapper.writeFormalTypeParameters(descriptor.getDeclaredTypeParameters(), sw);
    sw.writeSuperclass();
    if (superClassInfo.getKotlinType() == null) {
        sw.writeClassBegin(superClassInfo.getType());
        sw.writeClassEnd();
    } else {
        typeMapper.mapSupertype(superClassInfo.getKotlinType(), sw);
    }
    sw.writeSuperclassEnd();
    LinkedHashSet<String> superInterfaces = new LinkedHashSet<String>();
    Set<String> kotlinMarkerInterfaces = new LinkedHashSet<String>();
    for (KotlinType supertype : descriptor.getTypeConstructor().getSupertypes()) {
        if (isJvmInterface(supertype.getConstructor().getDeclarationDescriptor())) {
            sw.writeInterface();
            Type jvmInterfaceType = typeMapper.mapSupertype(supertype, sw);
            sw.writeInterfaceEnd();
            String jvmInterfaceInternalName = jvmInterfaceType.getInternalName();
            superInterfaces.add(jvmInterfaceInternalName);
            FqName kotlinInterfaceName = DescriptorUtils.getFqName(supertype.getConstructor().getDeclarationDescriptor()).toSafe();
            String kotlinMarkerInterfaceInternalName = KOTLIN_MARKER_INTERFACES.get(kotlinInterfaceName);
            if (kotlinMarkerInterfaceInternalName != null) {
                kotlinMarkerInterfaces.add(kotlinMarkerInterfaceInternalName);
            }
        }
    }
    for (String kotlinMarkerInterface : kotlinMarkerInterfaces) {
        sw.writeInterface();
        sw.writeAsmType(getObjectType(kotlinMarkerInterface));
        sw.writeInterfaceEnd();
    }
    superInterfaces.addAll(kotlinMarkerInterfaces);
    return new JvmClassSignature(classAsmType.getInternalName(), superClassInfo.getType().getInternalName(), new ArrayList<String>(superInterfaces), sw.makeJavaGenericSignature());
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) JvmSignatureWriter(org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter) BothSignatureWriter(org.jetbrains.kotlin.codegen.signature.BothSignatureWriter) FqName(org.jetbrains.kotlin.name.FqName) KotlinType(org.jetbrains.kotlin.types.KotlinType) JvmClassSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature) NotNull(org.jetbrains.annotations.NotNull) BindingContextUtils.getNotNull(org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)

Example 19 with Type

use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.

the class ImplementationBodyCodegen method genPropertyOnStack.

public Type genPropertyOnStack(InstructionAdapter iv, MethodContext context, @NotNull PropertyDescriptor propertyDescriptor, Type classAsmType, int index) {
    iv.load(index, classAsmType);
    if (couldUseDirectAccessToProperty(propertyDescriptor, /* forGetter = */
    true, /* isDelegated = */
    false, context, state.getShouldInlineConstVals())) {
        Type type = typeMapper.mapType(propertyDescriptor.getType());
        String fieldName = ((FieldOwnerContext) context.getParentContext()).getFieldName(propertyDescriptor, false);
        iv.getfield(classAsmType.getInternalName(), fieldName, type.getDescriptor());
        return type;
    } else {
        //noinspection ConstantConditions
        Method method = typeMapper.mapAsmMethod(propertyDescriptor.getGetter());
        iv.invokevirtual(classAsmType.getInternalName(), method.getName(), method.getDescriptor(), false);
        return method.getReturnType();
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) Method(org.jetbrains.org.objectweb.asm.commons.Method)

Example 20 with Type

use of org.jetbrains.org.objectweb.asm.Type in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateSuperCallImplicitArguments.

@NotNull
private ArgumentGenerator generateSuperCallImplicitArguments(@NotNull InstructionAdapter iv, @NotNull ExpressionCodegen codegen, @NotNull ConstructorDescriptor constructorDescriptor, @NotNull ConstructorDescriptor superConstructor, @NotNull CallableMethod superCallable, @NotNull List<JvmMethodParameterSignature> superParameters, @NotNull List<JvmMethodParameterSignature> parameters) {
    int offset = 1;
    int superIndex = 0;
    // them all onto the stack and update "offset" variable so that in the end it points to the slot of the first VALUE argument
    for (JvmMethodParameterSignature parameter : parameters) {
        if (superIndex >= superParameters.size())
            break;
        JvmMethodParameterKind superKind = superParameters.get(superIndex).getKind();
        JvmMethodParameterKind kind = parameter.getKind();
        Type type = parameter.getAsmType();
        if (superKind == JvmMethodParameterKind.VALUE && kind == JvmMethodParameterKind.SUPER_CALL_PARAM) {
            // Stop when we reach the actual value parameters present in the code; they will be generated via ResolvedCall below
            break;
        }
        if (superKind == JvmMethodParameterKind.OUTER) {
            assert kind == JvmMethodParameterKind.OUTER || kind == JvmMethodParameterKind.SUPER_CALL_PARAM : String.format("Non-outer parameter incorrectly mapped to outer for %s: %s vs %s", constructorDescriptor, parameters, superParameters);
            // Super constructor requires OUTER parameter, but our OUTER instance may be different from what is expected by the super
            // constructor. We need to traverse our outer classes from the bottom up, to find the needed class. See innerExtendsOuter.kt
            ClassDescriptor outerForSuper = (ClassDescriptor) superConstructor.getContainingDeclaration().getContainingDeclaration();
            StackValue outer = codegen.generateThisOrOuter(outerForSuper, true, true);
            outer.put(outer.type, codegen.v);
            superIndex++;
        } else if (kind == JvmMethodParameterKind.SUPER_CALL_PARAM || kind == JvmMethodParameterKind.ENUM_NAME_OR_ORDINAL) {
            iv.load(offset, type);
            superIndex++;
        }
        offset += type.getSize();
    }
    if (isAnonymousObject(descriptor)) {
        List<JvmMethodParameterSignature> superValues = superParameters.subList(superIndex, superParameters.size());
        return new ObjectSuperCallArgumentGenerator(superValues, iv, offset);
    } else {
        return new CallBasedArgumentGenerator(codegen, codegen.defaultCallGenerator, superConstructor.getValueParameters(), superCallable.getValueParameterTypes());
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature) JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) JvmMethodParameterKind(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind) NotNull(org.jetbrains.annotations.NotNull) BindingContextUtils.getNotNull(org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)

Aggregations

Type (org.jetbrains.org.objectweb.asm.Type)104 KotlinType (org.jetbrains.kotlin.types.KotlinType)66 IElementType (com.intellij.psi.tree.IElementType)45 NotNull (org.jetbrains.annotations.NotNull)23 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)16 Label (org.jetbrains.org.objectweb.asm.Label)12 Type.getObjectType (org.jetbrains.org.objectweb.asm.Type.getObjectType)10 Method (org.jetbrains.org.objectweb.asm.commons.Method)9 Unit (kotlin.Unit)8 LocalVariableDescriptor (org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)7 ArrayList (java.util.ArrayList)5 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)5 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)5 PrimitiveType (org.jetbrains.kotlin.builtins.PrimitiveType)4 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)4 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 ScriptDescriptor (org.jetbrains.kotlin.descriptors.ScriptDescriptor)3 InOut (com.intellij.codeInspection.bytecodeAnalysis.Direction.InOut)2 FunctionClassDescriptor (org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor)2