Search in sources :

Example 76 with Type

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

the class ExpressionCodegen method generateObjectLiteral.

@NotNull
public ObjectLiteralResult generateObjectLiteral(@NotNull KtObjectLiteralExpression literal) {
    KtObjectDeclaration objectDeclaration = literal.getObjectDeclaration();
    ClassDescriptor classDescriptor = bindingContext.get(CLASS, objectDeclaration);
    assert classDescriptor != null;
    Type asmType = asmTypeForAnonymousClass(bindingContext, objectDeclaration);
    ClassBuilder classBuilder = state.getFactory().newVisitor(JvmDeclarationOriginKt.OtherOrigin(objectDeclaration, classDescriptor), asmType, literal.getContainingFile());
    ClassContext objectContext = context.intoAnonymousClass(classDescriptor, this, OwnerKind.IMPLEMENTATION);
    MemberCodegen literalCodegen = new ImplementationBodyCodegen(objectDeclaration, objectContext, classBuilder, state, getParentCodegen(), /* isLocal = */
    true);
    literalCodegen.generate();
    addReifiedParametersFromSignature(literalCodegen, classDescriptor);
    propagateChildReifiedTypeParametersUsages(literalCodegen.getReifiedTypeParametersUsages());
    return new ObjectLiteralResult(literalCodegen.getReifiedTypeParametersUsages().wereUsedReifiedParameters(), classDescriptor);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 77 with Type

use of org.jetbrains.org.objectweb.asm.Type 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 78 with Type

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

the class ExpressionCodegen method generateThisOrOuter.

@NotNull
public StackValue generateThisOrOuter(@NotNull ClassDescriptor calleeContainingClass, boolean isSuper, boolean forceOuter) {
    boolean isSingleton = calleeContainingClass.getKind().isSingleton();
    if (isSingleton) {
        if (calleeContainingClass.equals(context.getThisDescriptor()) && !CodegenUtilKt.isJvmStaticInObjectOrClass(context.getFunctionDescriptor())) {
            return StackValue.local(0, typeMapper.mapType(calleeContainingClass));
        } else if (isEnumEntry(calleeContainingClass)) {
            return StackValue.enumEntry(calleeContainingClass, typeMapper);
        } else {
            return StackValue.singleton(calleeContainingClass, typeMapper);
        }
    }
    CodegenContext cur = context;
    Type type = asmType(calleeContainingClass.getDefaultType());
    StackValue result = StackValue.local(0, type);
    boolean inStartConstructorContext = cur instanceof ConstructorContext;
    while (cur != null) {
        ClassDescriptor thisDescriptor = cur.getThisDescriptor();
        if (!isSuper && thisDescriptor == calleeContainingClass) {
            return result;
        }
        if (!forceOuter && isSuper && DescriptorUtils.isSubclass(thisDescriptor, calleeContainingClass)) {
            return castToRequiredTypeOfInterfaceIfNeeded(result, thisDescriptor, calleeContainingClass);
        }
        forceOuter = false;
        //for constructor super call we should access to outer instance through parameter in locals, in other cases through field for captured outer
        if (inStartConstructorContext) {
            result = cur.getOuterExpression(result, false);
            cur = getNotNullParentContextForMethod(cur);
            inStartConstructorContext = false;
        } else {
            cur = getNotNullParentContextForMethod(cur);
            result = cur.getOuterExpression(result, false);
        }
        cur = cur.getParentContext();
    }
    throw new UnsupportedOperationException();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 79 with Type

use of org.jetbrains.org.objectweb.asm.Type 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 80 with Type

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

the class ExpressionCodegen method addLeaveTaskToRemoveLocalVariableFromFrameMap.

private void addLeaveTaskToRemoveLocalVariableFromFrameMap(@NotNull KtVariableDeclaration statement, final Label blockEnd, @NotNull List<Function<StackValue, Void>> leaveTasks) {
    final VariableDescriptor variableDescriptor = getVariableDescriptorNotNull(statement);
    // They always will have special name
    if (variableDescriptor.getName().isSpecial())
        return;
    final Type type = getVariableType(variableDescriptor);
    final Label scopeStart = new Label();
    v.mark(scopeStart);
    leaveTasks.add(new Function<StackValue, Void>() {

        @Override
        public Void fun(StackValue answer) {
            if (isDelegatedLocalVariable(variableDescriptor)) {
                myFrameMap.leave(getDelegatedLocalVariableMetadata(variableDescriptor, bindingContext));
            }
            int index = myFrameMap.leave(variableDescriptor);
            if (isSharedVarType(type)) {
                v.aconst(null);
                v.store(index, OBJECT_TYPE);
            }
            v.visitLocalVariable(variableDescriptor.getName().asString(), type.getDescriptor(), null, scopeStart, blockEnd, index);
            return null;
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Label(org.jetbrains.org.objectweb.asm.Label) LocalVariableDescriptor(org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)

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