Search in sources :

Example 26 with Type

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

the class ExpressionCodegen method generateExpressionMatch.

private StackValue generateExpressionMatch(StackValue expressionToMatch, KtExpression subjectExpression, KtExpression patternExpression) {
    if (expressionToMatch != null) {
        Type subjectType = expressionToMatch.type;
        markStartLineNumber(patternExpression);
        KotlinType condJetType = bindingContext.getType(patternExpression);
        Type condType;
        if (isNumberPrimitiveOrBoolean(subjectType)) {
            assert condJetType != null;
            condType = asmType(condJetType);
            if (!isNumberPrimitiveOrBoolean(condType)) {
                subjectType = boxType(subjectType);
            }
        } else {
            condType = OBJECT_TYPE;
        }
        return genEqualsForExpressionsPreferIEEE754Arithmetic(subjectExpression, patternExpression, KtTokens.EQEQ, subjectType, condType, expressionToMatch);
    } else {
        return gen(patternExpression);
    }
}
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 27 with Type

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

the class ExpressionCodegen method visitObjectLiteralExpression.

@Override
public StackValue visitObjectLiteralExpression(@NotNull KtObjectLiteralExpression expression, StackValue receiver) {
    final ObjectLiteralResult objectLiteralResult = generateObjectLiteral(expression);
    final ClassDescriptor classDescriptor = objectLiteralResult.classDescriptor;
    final Type type = typeMapper.mapType(classDescriptor);
    return StackValue.operation(type, new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            if (objectLiteralResult.wereReifiedMarkers) {
                ReifiedTypeInliner.putNeedClassReificationMarker(v);
            }
            v.anew(type);
            v.dup();
            pushClosureOnStack(classDescriptor, true, defaultCallGenerator, /* functionReferenceReceiver = */
            null);
            ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
            assert primaryConstructor != null : "There should be primary constructor for object literal";
            ResolvedCall<ConstructorDescriptor> superCall = getDelegationConstructorCall(bindingContext, primaryConstructor);
            if (superCall != null) {
                // For an anonymous object, we should also generate all non-default arguments that it captures for its super call
                ConstructorDescriptor superConstructor = superCall.getResultingDescriptor();
                ConstructorDescriptor constructorToCall = SamCodegenUtil.resolveSamAdapter(superConstructor);
                List<ValueParameterDescriptor> superValueParameters = superConstructor.getValueParameters();
                int params = superValueParameters.size();
                List<Type> superMappedTypes = typeMapper.mapToCallableMethod(constructorToCall, false).getValueParameterTypes();
                assert superMappedTypes.size() >= params : String.format("Incorrect number of mapped parameters vs arguments: %d < %d for %s", superMappedTypes.size(), params, classDescriptor);
                List<ResolvedValueArgument> valueArguments = new ArrayList<ResolvedValueArgument>(params);
                List<ValueParameterDescriptor> valueParameters = new ArrayList<ValueParameterDescriptor>(params);
                List<Type> mappedTypes = new ArrayList<Type>(params);
                for (ValueParameterDescriptor parameter : superValueParameters) {
                    ResolvedValueArgument argument = superCall.getValueArguments().get(parameter);
                    if (!(argument instanceof DefaultValueArgument)) {
                        valueArguments.add(argument);
                        valueParameters.add(parameter);
                        mappedTypes.add(superMappedTypes.get(parameter.getIndex()));
                    }
                }
                ArgumentGenerator argumentGenerator = new CallBasedArgumentGenerator(ExpressionCodegen.this, defaultCallGenerator, valueParameters, mappedTypes);
                argumentGenerator.generate(valueArguments, valueArguments, null);
            }
            Collection<ClassConstructorDescriptor> constructors = classDescriptor.getConstructors();
            assert constructors.size() == 1 : "Unexpected number of constructors for class: " + classDescriptor + " " + constructors;
            ConstructorDescriptor constructorDescriptor = CollectionsKt.single(constructors);
            Method constructor = typeMapper.mapAsmMethod(SamCodegenUtil.resolveSamAdapter(constructorDescriptor));
            v.invokespecial(type.getInternalName(), "<init>", constructor.getDescriptor(), false);
            return Unit.INSTANCE;
        }
    });
}
Also used : SamConstructorDescriptor(org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor) TypeAliasConstructorDescriptor(org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor) Method(org.jetbrains.org.objectweb.asm.commons.Method) 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 28 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 29 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 30 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)

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