Search in sources :

Example 11 with Type

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

the class FieldInfo method createForSingleton.

@NotNull
public static FieldInfo createForSingleton(@NotNull ClassDescriptor classDescriptor, @NotNull KotlinTypeMapper typeMapper) {
    if (!classDescriptor.getKind().isSingleton() || DescriptorUtils.isEnumEntry(classDescriptor)) {
        throw new UnsupportedOperationException("Can't create singleton field for class: " + classDescriptor);
    }
    if (isNonCompanionObject(classDescriptor) || CompanionObjectMapping.INSTANCE.isMappedIntrinsicCompanionObject(classDescriptor)) {
        return createSingletonViaInstance(classDescriptor, typeMapper);
    }
    ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class);
    assert ownerDescriptor != null : "Owner not found for class: " + classDescriptor;
    Type ownerType = typeMapper.mapType(ownerDescriptor);
    return new FieldInfo(ownerType, typeMapper.mapType(classDescriptor), classDescriptor.getName().asString(), true);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with Type

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

the class ExpressionCodegen method visitPostfixExpression.

@Override
public StackValue visitPostfixExpression(@NotNull final KtPostfixExpression expression, StackValue receiver) {
    if (expression.getOperationReference().getReferencedNameElementType() == KtTokens.EXCLEXCL) {
        final StackValue base = genQualified(receiver, expression.getBaseExpression());
        if (isPrimitive(base.type)) {
            return base;
        } else {
            return StackValue.operation(base.type, new Function1<InstructionAdapter, Unit>() {

                @Override
                public Unit invoke(InstructionAdapter v) {
                    base.put(base.type, v);
                    v.dup();
                    Label ok = new Label();
                    v.ifnonnull(ok);
                    v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "throwNpe", "()V", false);
                    v.mark(ok);
                    return null;
                }
            });
        }
    }
    DeclarationDescriptor originalOperation = bindingContext.get(REFERENCE_TARGET, expression.getOperationReference());
    String originalOperationName = originalOperation != null ? originalOperation.getName().asString() : null;
    final ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCallWithAssert(expression, bindingContext);
    DeclarationDescriptor op = resolvedCall.getResultingDescriptor();
    if (!(op instanceof FunctionDescriptor) || originalOperation == null) {
        throw new UnsupportedOperationException("Don't know how to generate this postfix expression: " + originalOperationName + " " + op);
    }
    final Type asmResultType = expressionType(expression);
    final Type asmBaseType = expressionType(expression.getBaseExpression());
    DeclarationDescriptor cls = op.getContainingDeclaration();
    final int increment;
    if (originalOperationName.equals("inc")) {
        increment = 1;
    } else if (originalOperationName.equals("dec")) {
        increment = -1;
    } else {
        throw new UnsupportedOperationException("Unsupported postfix operation: " + originalOperationName + " " + op);
    }
    final boolean isPrimitiveNumberClassDescriptor = isPrimitiveNumberClassDescriptor(cls);
    if (isPrimitiveNumberClassDescriptor && AsmUtil.isPrimitive(asmBaseType)) {
        KtExpression operand = expression.getBaseExpression();
        // Optimization for j = i++, when j and i are Int without any smart cast: we just work with primitive int
        if (operand instanceof KtReferenceExpression && asmResultType == Type.INT_TYPE && bindingContext.get(BindingContext.SMARTCAST, operand) == null) {
            int index = indexOfLocalNotDelegated((KtReferenceExpression) operand);
            if (index >= 0) {
                return StackValue.postIncrement(index, increment);
            }
        }
    }
    return StackValue.operation(asmBaseType, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            StackValue value = StackValue.complexWriteReadReceiver(gen(expression.getBaseExpression()));
            value.put(asmBaseType, v);
            AsmUtil.dup(v, asmBaseType);
            StackValue previousValue = StackValue.local(myFrameMap.enterTemp(asmBaseType), asmBaseType);
            previousValue.store(StackValue.onStack(asmBaseType), v);
            Type storeType;
            if (isPrimitiveNumberClassDescriptor && AsmUtil.isPrimitive(asmBaseType)) {
                genIncrement(asmBaseType, increment, v);
                storeType = asmBaseType;
            } else {
                StackValue result = invokeFunction(resolvedCall, StackValue.onStack(asmBaseType));
                result.put(result.type, v);
                storeType = result.type;
            }
            value.store(StackValue.onStack(storeType), v, true);
            previousValue.put(asmBaseType, v);
            myFrameMap.leaveTemp(asmBaseType);
            return Unit.INSTANCE;
        }
    });
}
Also used : Label(org.jetbrains.org.objectweb.asm.Label) Unit(kotlin.Unit) 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)

Example 13 with Type

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

the class ExpressionCodegen method generateIfExpression.

/* package */
StackValue generateIfExpression(@NotNull final KtIfExpression expression, final boolean isStatement) {
    final Type asmType = isStatement ? Type.VOID_TYPE : expressionTypeForBranchingOperation(expression);
    final StackValue condition = gen(expression.getCondition());
    final KtExpression thenExpression = expression.getThen();
    final KtExpression elseExpression = expression.getElse();
    if (isEmptyExpression(thenExpression)) {
        if (isEmptyExpression(elseExpression)) {
            return StackValue.coercion(condition, asmType);
        }
        return generateSingleBranchIf(condition, expression, elseExpression, false, isStatement);
    } else {
        if (isEmptyExpression(elseExpression)) {
            return generateSingleBranchIf(condition, expression, thenExpression, true, isStatement);
        }
    }
    return StackValue.operation(asmType, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            Label elseLabel = new Label();
            BranchedValue.Companion.condJump(condition, elseLabel, true, v);
            Label end = new Label();
            gen(thenExpression, asmType);
            v.goTo(end);
            v.mark(elseLabel);
            gen(elseExpression, asmType);
            markLineNumber(expression, isStatement);
            v.mark(end);
            return Unit.INSTANCE;
        }
    });
}
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 14 with Type

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

the class ExpressionCodegen method newArrayInstruction.

public void newArrayInstruction(@NotNull KotlinType arrayType) {
    if (KotlinBuiltIns.isArray(arrayType)) {
        KotlinType elementJetType = arrayType.getArguments().get(0).getType();
        putReifiedOperationMarkerIfTypeIsReifiedParameter(elementJetType, ReifiedTypeInliner.OperationKind.NEW_ARRAY);
        v.newarray(boxType(asmType(elementJetType)));
    } else {
        Type type = typeMapper.mapType(arrayType);
        v.newarray(correctElementType(type));
    }
}
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 15 with Type

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

the class ExpressionCodegen method applyIntrinsic.

@Nullable
private StackValue applyIntrinsic(DeclarationDescriptor descriptor, Class<? extends IntrinsicPropertyGetter> intrinsicType, ResolvedCall<?> resolvedCall, @NotNull StackValue receiver) {
    if (descriptor instanceof CallableMemberDescriptor) {
        CallableMemberDescriptor memberDescriptor = DescriptorUtils.unwrapFakeOverride((CallableMemberDescriptor) descriptor);
        IntrinsicMethod intrinsic = state.getIntrinsics().getIntrinsic(memberDescriptor);
        if (intrinsicType.isInstance(intrinsic)) {
            //TODO: intrinsic properties (see intermediateValueForProperty)
            Type returnType = typeMapper.mapType(memberDescriptor);
            return ((IntrinsicPropertyGetter) intrinsic).generate(resolvedCall, this, returnType, receiver);
        }
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Nullable(org.jetbrains.annotations.Nullable)

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