Search in sources :

Example 31 with Type

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

the class ExpressionCodegen method intermediateValueForProperty.

public StackValue.Property intermediateValueForProperty(@NotNull PropertyDescriptor propertyDescriptor, boolean forceField, boolean syntheticBackingField, @Nullable ClassDescriptor superCallTarget, boolean skipAccessorsForPrivateFieldInOuterClass, @NotNull StackValue receiver, @Nullable ResolvedCall resolvedCall) {
    if (propertyDescriptor instanceof SyntheticJavaPropertyDescriptor) {
        return intermediateValueForSyntheticExtensionProperty((SyntheticJavaPropertyDescriptor) propertyDescriptor, receiver);
    }
    DeclarationDescriptor containingDeclaration = propertyDescriptor.getContainingDeclaration();
    FieldAccessorKind fieldAccessorKind = FieldAccessorKind.NORMAL;
    boolean isBackingFieldInClassCompanion = JvmAbi.isPropertyWithBackingFieldInOuterClass(propertyDescriptor);
    if (isBackingFieldInClassCompanion && (forceField || propertyDescriptor.isConst() && Visibilities.isPrivate(propertyDescriptor.getVisibility()))) {
        fieldAccessorKind = FieldAccessorKind.IN_CLASS_COMPANION;
    } else if (syntheticBackingField && context.getFirstCrossInlineOrNonInlineContext().getParentContext().getContextDescriptor() != containingDeclaration) {
        fieldAccessorKind = FieldAccessorKind.FIELD_FROM_LOCAL;
    }
    boolean isStaticBackingField = DescriptorUtils.isStaticDeclaration(propertyDescriptor) || AsmUtil.isInstancePropertyWithStaticBackingField(propertyDescriptor);
    boolean isSuper = superCallTarget != null;
    boolean isExtensionProperty = propertyDescriptor.getExtensionReceiverParameter() != null;
    KotlinType delegateType = JvmCodegenUtil.getPropertyDelegateType(propertyDescriptor, bindingContext);
    boolean isDelegatedProperty = delegateType != null;
    CallableMethod callableGetter = null;
    CallableMethod callableSetter = null;
    CodegenContext backingFieldContext = getBackingFieldContext(fieldAccessorKind, containingDeclaration);
    DeclarationDescriptor ownerDescriptor = containingDeclaration;
    boolean skipPropertyAccessors;
    PropertyDescriptor originalPropertyDescriptor = DescriptorUtils.unwrapFakeOverride(propertyDescriptor);
    if (fieldAccessorKind != FieldAccessorKind.NORMAL) {
        int flags = AsmUtil.getVisibilityForBackingField(propertyDescriptor, isDelegatedProperty);
        boolean isInlinedConst = propertyDescriptor.isConst() && state.getShouldInlineConstVals();
        skipPropertyAccessors = isInlinedConst || (flags & ACC_PRIVATE) == 0 || skipAccessorsForPrivateFieldInOuterClass;
        if (!skipPropertyAccessors) {
            //noinspection ConstantConditions
            propertyDescriptor = (PropertyDescriptor) backingFieldContext.getAccessor(propertyDescriptor, fieldAccessorKind, delegateType, superCallTarget);
            assert propertyDescriptor instanceof AccessorForPropertyBackingField : "Unexpected accessor descriptor: " + propertyDescriptor;
            ownerDescriptor = propertyDescriptor;
        }
    } else {
        if (!isBackingFieldInClassCompanion) {
            ownerDescriptor = propertyDescriptor;
        }
        skipPropertyAccessors = forceField;
    }
    if (!skipPropertyAccessors) {
        if (!couldUseDirectAccessToProperty(propertyDescriptor, true, isDelegatedProperty, context, state.getShouldInlineConstVals())) {
            propertyDescriptor = context.getAccessorForSuperCallIfNeeded(propertyDescriptor, superCallTarget, state);
            propertyDescriptor = context.accessibleDescriptor(propertyDescriptor, superCallTarget);
            PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
            if (getter != null && !isConstOrHasJvmFieldAnnotation(propertyDescriptor)) {
                callableGetter = typeMapper.mapToCallableMethod(getter, isSuper);
            }
        }
        if (propertyDescriptor.isVar()) {
            PropertySetterDescriptor setter = propertyDescriptor.getSetter();
            if (setter != null && !couldUseDirectAccessToProperty(propertyDescriptor, false, isDelegatedProperty, context, state.getShouldInlineConstVals()) && !isConstOrHasJvmFieldAnnotation(propertyDescriptor)) {
                callableSetter = typeMapper.mapToCallableMethod(setter, isSuper);
            }
        }
    }
    if (!isStaticBackingField) {
        propertyDescriptor = DescriptorUtils.unwrapFakeOverride(propertyDescriptor);
    }
    Type backingFieldOwner = typeMapper.mapOwner(ownerDescriptor);
    String fieldName;
    if (isExtensionProperty && !isDelegatedProperty) {
        fieldName = null;
    } else if (originalPropertyDescriptor.getContainingDeclaration() == backingFieldContext.getContextDescriptor()) {
        assert backingFieldContext instanceof FieldOwnerContext : "Actual context is " + backingFieldContext + " but should be instance of FieldOwnerContext";
        fieldName = ((FieldOwnerContext) backingFieldContext).getFieldName(propertyDescriptor, isDelegatedProperty);
    } else {
        fieldName = KotlinTypeMapper.mapDefaultFieldName(propertyDescriptor, isDelegatedProperty);
    }
    return StackValue.property(propertyDescriptor, backingFieldOwner, typeMapper.mapType(isDelegatedProperty && forceField ? delegateType : propertyDescriptor.getOriginal().getType()), isStaticBackingField, fieldName, callableGetter, callableSetter, receiver, this, resolvedCall);
}
Also used : SyntheticJavaPropertyDescriptor(org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor) SyntheticJavaPropertyDescriptor(org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType) IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 32 with Type

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

the class ExpressionCodegen method generateComparison.

private StackValue generateComparison(KtBinaryExpression expression, StackValue receiver) {
    ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCallWithAssert(expression, bindingContext);
    KtExpression left = expression.getLeft();
    KtExpression right = expression.getRight();
    Type type;
    StackValue leftValue;
    StackValue rightValue;
    Type leftType = expressionType(left);
    Type rightType = expressionType(right);
    TypeAndNullability left754Type = calcTypeForIEEE754ArithmeticIfNeeded(left);
    TypeAndNullability right754Type = calcTypeForIEEE754ArithmeticIfNeeded(right);
    Callable callable = resolveToCallable((FunctionDescriptor) resolvedCall.getResultingDescriptor(), false, resolvedCall);
    boolean is754Arithmetic = left754Type != null && right754Type != null && left754Type.type.equals(right754Type.type);
    if (callable instanceof IntrinsicCallable && ((isPrimitive(leftType) && isPrimitive(rightType)) || is754Arithmetic)) {
        type = is754Arithmetic ? left754Type.type : comparisonOperandType(leftType, rightType);
        leftValue = gen(left);
        rightValue = gen(right);
    } else {
        type = Type.INT_TYPE;
        leftValue = invokeFunction(resolvedCall, receiver);
        rightValue = StackValue.constant(0, type);
    }
    return StackValue.cmp(expression.getOperationToken(), type, leftValue, rightValue);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 33 with Type

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

the class ExpressionCodegen method putLocalVariableIntoFrameMap.

private void putLocalVariableIntoFrameMap(@NotNull KtVariableDeclaration statement) {
    VariableDescriptor variableDescriptor = getVariableDescriptorNotNull(statement);
    // They always will have special name
    if (variableDescriptor.getName().isSpecial())
        return;
    Type type = getVariableType(variableDescriptor);
    int index = myFrameMap.enter(variableDescriptor, type);
    if (isDelegatedLocalVariable(variableDescriptor)) {
        myFrameMap.enter(getDelegatedLocalVariableMetadata(variableDescriptor, bindingContext), AsmTypes.K_PROPERTY0_TYPE);
    }
    if (isSharedVarType(type)) {
        markLineNumber(statement, false);
        v.anew(type);
        v.dup();
        v.invokespecial(type.getInternalName(), "<init>", "()V", false);
        v.store(index, OBJECT_TYPE);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) LocalVariableDescriptor(org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)

Example 34 with Type

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

the class ExpressionCodegen method generateWhenExpression.

public StackValue generateWhenExpression(final KtWhenExpression expression, final boolean isStatement) {
    final KtExpression expr = expression.getSubjectExpression();
    final Type subjectType = expressionType(expr);
    final Type resultType = isStatement ? Type.VOID_TYPE : expressionTypeForBranchingOperation(expression);
    return StackValue.operation(resultType, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            SwitchCodegen switchCodegen = SwitchCodegenUtil.buildAppropriateSwitchCodegenIfPossible(expression, isStatement, CodegenUtil.isExhaustive(bindingContext, expression, isStatement), ExpressionCodegen.this);
            if (switchCodegen != null) {
                switchCodegen.generate();
                return Unit.INSTANCE;
            }
            int subjectLocal = expr != null ? myFrameMap.enterTemp(subjectType) : -1;
            if (subjectLocal != -1) {
                gen(expr, subjectType);
                tempVariables.put(expr, StackValue.local(subjectLocal, subjectType));
                v.store(subjectLocal, subjectType);
            }
            Label end = new Label();
            boolean hasElse = KtPsiUtil.checkWhenExpressionHasSingleElse(expression);
            Label nextCondition = null;
            for (KtWhenEntry whenEntry : expression.getEntries()) {
                if (nextCondition != null) {
                    v.mark(nextCondition);
                }
                nextCondition = new Label();
                FrameMap.Mark mark = myFrameMap.mark();
                Label thisEntry = new Label();
                if (!whenEntry.isElse()) {
                    KtWhenCondition[] conditions = whenEntry.getConditions();
                    for (int i = 0; i < conditions.length; i++) {
                        StackValue conditionValue = generateWhenCondition(expr, subjectType, subjectLocal, conditions[i]);
                        BranchedValue.Companion.condJump(conditionValue, nextCondition, true, v);
                        if (i < conditions.length - 1) {
                            v.goTo(thisEntry);
                            v.mark(nextCondition);
                            nextCondition = new Label();
                        }
                    }
                }
                v.visitLabel(thisEntry);
                gen(whenEntry.getExpression(), resultType);
                mark.dropTo();
                if (!whenEntry.isElse()) {
                    v.goTo(end);
                }
            }
            if (!hasElse && nextCondition != null) {
                v.mark(nextCondition);
                putUnitInstanceOntoStackForNonExhaustiveWhen(expression, isStatement);
            }
            markLineNumber(expression, isStatement);
            v.mark(end);
            myFrameMap.leaveTemp(subjectType);
            tempVariables.remove(expr);
            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) SwitchCodegen(org.jetbrains.kotlin.codegen.when.SwitchCodegen)

Example 35 with Type

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

the class ScriptCodegen method createScriptCodegen.

public static ScriptCodegen createScriptCodegen(@NotNull KtScript declaration, @NotNull GenerationState state, @NotNull CodegenContext parentContext) {
    BindingContext bindingContext = state.getBindingContext();
    ScriptDescriptor scriptDescriptor = bindingContext.get(BindingContext.SCRIPT, declaration);
    assert scriptDescriptor != null;
    Type classType = state.getTypeMapper().mapType(scriptDescriptor);
    ClassBuilder builder = state.getFactory().newVisitor(JvmDeclarationOriginKt.OtherOrigin(declaration, scriptDescriptor), classType, declaration.getContainingFile());
    List<ScriptDescriptor> earlierScripts = state.getReplSpecific().getEarlierScriptsForReplInterpreter();
    ScriptContext scriptContext = parentContext.intoScript(scriptDescriptor, earlierScripts == null ? Collections.<ScriptDescriptor>emptyList() : earlierScripts, scriptDescriptor, state.getTypeMapper());
    return new ScriptCodegen(declaration, state, scriptContext, builder);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ScriptContext(org.jetbrains.kotlin.codegen.context.ScriptContext) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) ScriptDescriptor(org.jetbrains.kotlin.descriptors.ScriptDescriptor)

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