Search in sources :

Example 26 with InstructionAdapter

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

the class ClosureCodegen method generateFunctionReferenceMethods.

// TODO: ImplementationBodyCodegen.markLineNumberForSyntheticFunction?
private void generateFunctionReferenceMethods(@NotNull FunctionDescriptor descriptor) {
    int flags = ACC_PUBLIC | ACC_FINAL;
    boolean generateBody = state.getClassBuilderMode().generateBodies;
    {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, flags, "getOwner", Type.getMethodDescriptor(K_DECLARATION_CONTAINER_TYPE), null, null);
        if (generateBody) {
            mv.visitCode();
            InstructionAdapter iv = new InstructionAdapter(mv);
            generateCallableReferenceDeclarationContainer(iv, descriptor, state);
            iv.areturn(K_DECLARATION_CONTAINER_TYPE);
            FunctionCodegen.endVisit(iv, "function reference getOwner", element);
        }
    }
    {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, flags, "getName", Type.getMethodDescriptor(JAVA_STRING_TYPE), null, null);
        if (generateBody) {
            mv.visitCode();
            InstructionAdapter iv = new InstructionAdapter(mv);
            iv.aconst(descriptor.getName().asString());
            iv.areturn(JAVA_STRING_TYPE);
            FunctionCodegen.endVisit(iv, "function reference getName", element);
        }
    }
    {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, flags, "getSignature", Type.getMethodDescriptor(JAVA_STRING_TYPE), null, null);
        if (generateBody) {
            mv.visitCode();
            InstructionAdapter iv = new InstructionAdapter(mv);
            PropertyReferenceCodegen.generateCallableReferenceSignature(iv, descriptor, state);
            iv.areturn(JAVA_STRING_TYPE);
            FunctionCodegen.endVisit(iv, "function reference getSignature", element);
        }
    }
}
Also used : InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 27 with InstructionAdapter

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

the class ExpressionCodegen method visitBinaryWithTypeRHSExpression.

@Override
public StackValue visitBinaryWithTypeRHSExpression(@NotNull KtBinaryExpressionWithTypeRHS expression, StackValue receiver) {
    KtExpression left = expression.getLeft();
    final IElementType opToken = expression.getOperationReference().getReferencedNameElementType();
    final KotlinType rightType = bindingContext.get(TYPE, expression.getRight());
    assert rightType != null;
    final StackValue value = genQualified(receiver, left);
    return StackValue.operation(boxType(asmType(rightType)), new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            value.put(boxType(value.type), v);
            if (value.type == Type.VOID_TYPE) {
                StackValue.putUnitInstance(v);
            }
            boolean safeAs = opToken == KtTokens.AS_SAFE;
            Type type = boxType(asmType(rightType));
            if (TypeUtils.isReifiedTypeParameter(rightType)) {
                putReifiedOperationMarkerIfTypeIsReifiedParameter(rightType, safeAs ? ReifiedTypeInliner.OperationKind.SAFE_AS : ReifiedTypeInliner.OperationKind.AS);
                v.checkcast(type);
                return Unit.INSTANCE;
            }
            CodegenUtilKt.generateAsCast(v, rightType, type, safeAs);
            return Unit.INSTANCE;
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) KotlinType(org.jetbrains.kotlin.types.KotlinType) Unit(kotlin.Unit)

Example 28 with InstructionAdapter

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

the class ExpressionCodegen method generateElvis.

private StackValue generateElvis(@NotNull final KtBinaryExpression expression) {
    KtExpression left = expression.getLeft();
    final Type exprType = expressionType(expression);
    final Type leftType = expressionType(left);
    final Label ifNull = new Label();
    assert left != null : "left expression in elvis should be not null: " + expression.getText();
    final StackValue value = generateExpressionWithNullFallback(left, ifNull);
    if (isPrimitive(leftType)) {
        return value;
    }
    return StackValue.operation(exprType, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            value.put(value.type, v);
            v.dup();
            v.ifnull(ifNull);
            StackValue.onStack(leftType).put(exprType, v);
            Label end = new Label();
            v.goTo(end);
            v.mark(ifNull);
            v.pop();
            gen(expression.getRight(), exprType);
            v.mark(end);
            return null;
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) Label(org.jetbrains.org.objectweb.asm.Label) Unit(kotlin.Unit)

Example 29 with InstructionAdapter

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

the class ExpressionCodegen method genSamInterfaceValue.

@Nullable
private StackValue genSamInterfaceValue(@NotNull KtExpression probablyParenthesizedExpression, @NotNull final KtVisitor<StackValue, StackValue> visitor) {
    final KtExpression expression = KtPsiUtil.deparenthesize(probablyParenthesizedExpression);
    final SamType samType = bindingContext.get(SAM_VALUE, probablyParenthesizedExpression);
    if (samType == null || expression == null)
        return null;
    if (expression instanceof KtLambdaExpression) {
        return genClosure(((KtLambdaExpression) expression).getFunctionLiteral(), samType);
    }
    if (expression instanceof KtNamedFunction) {
        return genClosure((KtNamedFunction) expression, samType);
    }
    final Type asmType = state.getSamWrapperClasses().getSamWrapperClass(samType, expression.getContainingKtFile(), this);
    return StackValue.operation(asmType, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            v.anew(asmType);
            v.dup();
            Type functionType = typeMapper.mapType(samType.getKotlinFunctionType());
            expression.accept(visitor, StackValue.none()).put(functionType, v);
            Label ifNonNull = new Label();
            Label afterAll = new Label();
            v.dup();
            v.ifnonnull(ifNonNull);
            // if null: pop function value and wrapper objects, put null
            v.pop();
            v.pop2();
            v.aconst(null);
            v.goTo(afterAll);
            v.mark(ifNonNull);
            v.invokespecial(asmType.getInternalName(), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, functionType), false);
            v.mark(afterAll);
            return null;
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) Label(org.jetbrains.org.objectweb.asm.Label) Unit(kotlin.Unit) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with InstructionAdapter

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

the class PropertyCodegen method invokeDelegatedPropertyConventionMethodWithReceiver.

public static StackValue invokeDelegatedPropertyConventionMethodWithReceiver(@NotNull ExpressionCodegen codegen, @NotNull KotlinTypeMapper typeMapper, @NotNull ResolvedCall<FunctionDescriptor> resolvedCall, final int indexInPropertyMetadataArray, int propertyMetadataArgumentIndex, @Nullable StackValue receiver, @NotNull PropertyDescriptor propertyDescriptor) {
    final Type owner = JvmAbi.isPropertyWithBackingFieldInOuterClass(propertyDescriptor) ? codegen.getState().getTypeMapper().mapOwner(propertyDescriptor) : getDelegatedPropertyMetadataOwner(codegen, typeMapper);
    codegen.tempVariables.put(resolvedCall.getCall().getValueArguments().get(propertyMetadataArgumentIndex).asElement(), new StackValue(K_PROPERTY_TYPE) {

        @Override
        public void putSelector(@NotNull Type type, @NotNull InstructionAdapter v) {
            Field array = StackValue.field(Type.getType("[" + K_PROPERTY_TYPE), owner, JvmAbi.DELEGATED_PROPERTIES_ARRAY_NAME, true, StackValue.none());
            StackValue.arrayElement(K_PROPERTY_TYPE, array, StackValue.constant(indexInPropertyMetadataArray, Type.INT_TYPE)).put(type, v);
        }
    });
    return codegen.invokeFunction(resolvedCall, receiver);
}
Also used : AsmUtil.getVisibilityForBackingField(org.jetbrains.kotlin.codegen.AsmUtil.getVisibilityForBackingField) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type(org.jetbrains.org.objectweb.asm.Type) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)

Aggregations

InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)35 KotlinType (org.jetbrains.kotlin.types.KotlinType)23 Type (org.jetbrains.org.objectweb.asm.Type)16 IElementType (com.intellij.psi.tree.IElementType)10 Unit (kotlin.Unit)10 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)7 Label (org.jetbrains.org.objectweb.asm.Label)5 Method (org.jetbrains.org.objectweb.asm.commons.Method)5 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)4 NotNull (org.jetbrains.annotations.NotNull)3 Type.getObjectType (org.jetbrains.org.objectweb.asm.Type.getObjectType)3 Nullable (org.jetbrains.annotations.Nullable)2 PrimitiveType (org.jetbrains.kotlin.builtins.PrimitiveType)2 JvmPrimitiveType (org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType)2 JvmMethodSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature)2 TypeUtils.isNullableType (org.jetbrains.kotlin.types.TypeUtils.isNullableType)2 TraceMethodVisitor (org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor)2 Method (java.lang.reflect.Method)1 KotlinBuiltIns (org.jetbrains.kotlin.builtins.KotlinBuiltIns)1 AsmUtil.getVisibilityForBackingField (org.jetbrains.kotlin.codegen.AsmUtil.getVisibilityForBackingField)1