Search in sources :

Example 91 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class ClosureCodegen method generateDeclaration.

@Override
protected void generateDeclaration() {
    JvmSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS);
    if (samType != null) {
        typeMapper.writeFormalTypeParameters(samType.getType().getConstructor().getParameters(), sw);
    }
    sw.writeSuperclass();
    superClassAsmType = typeMapper.mapSupertype(superClassType, sw);
    sw.writeSuperclassEnd();
    String[] superInterfaceAsmTypes = new String[superInterfaceTypes.size()];
    for (int i = 0; i < superInterfaceTypes.size(); i++) {
        KotlinType superInterfaceType = superInterfaceTypes.get(i);
        sw.writeInterface();
        superInterfaceAsmTypes[i] = typeMapper.mapSupertype(superInterfaceType, sw).getInternalName();
        sw.writeInterfaceEnd();
    }
    v.defineClass(element, state.getClassFileVersion(), ACC_FINAL | ACC_SUPER | visibilityFlag, asmType.getInternalName(), sw.makeJavaGenericSignature(), superClassAsmType.getInternalName(), superInterfaceAsmTypes);
    InlineCodegenUtil.initDefaultSourceMappingIfNeeded(context, this, state);
    v.visitSource(element.getContainingFile().getName(), null);
}
Also used : JvmSignatureWriter(org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter) BothSignatureWriter(org.jetbrains.kotlin.codegen.signature.BothSignatureWriter) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 92 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class ExpressionCodegen method getOrCreateCallGenerator.

@NotNull
CallGenerator getOrCreateCallGenerator(@NotNull ResolvedCall<?> resolvedCall, @NotNull CallableDescriptor descriptor) {
    Map<TypeParameterDescriptor, KotlinType> typeArguments = getTypeArgumentsForResolvedCall(resolvedCall, descriptor);
    TypeParameterMappings mappings = new TypeParameterMappings();
    for (Map.Entry<TypeParameterDescriptor, KotlinType> entry : typeArguments.entrySet()) {
        TypeParameterDescriptor key = entry.getKey();
        KotlinType type = entry.getValue();
        boolean isReified = key.isReified() || InlineUtil.isArrayConstructorWithLambda(resolvedCall.getResultingDescriptor());
        Pair<TypeParameterDescriptor, ReificationArgument> typeParameterAndReificationArgument = extractReificationArgument(type);
        if (typeParameterAndReificationArgument == null) {
            KotlinType approximatedType = CapturedTypeApproximationKt.approximateCapturedTypes(entry.getValue()).getUpper();
            // type is not generic
            JvmSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.TYPE);
            Type asmType = typeMapper.mapTypeParameter(approximatedType, signatureWriter);
            mappings.addParameterMappingToType(key.getName().getIdentifier(), approximatedType, asmType, signatureWriter.toString(), isReified);
        } else {
            mappings.addParameterMappingForFurtherReification(key.getName().getIdentifier(), type, typeParameterAndReificationArgument.getSecond(), isReified);
        }
    }
    return getOrCreateCallGenerator(descriptor, resolvedCall.getCall().getCallElement(), mappings, false);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) JvmSignatureWriter(org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter) BothSignatureWriter(org.jetbrains.kotlin.codegen.signature.BothSignatureWriter) KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 93 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType 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 94 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class PropertyCodegen method generateBackingField.

private void generateBackingField(KtNamedDeclaration element, PropertyDescriptor propertyDescriptor, boolean isDelegate, KotlinType kotlinType, Object defaultValue, Annotations annotations) {
    int modifiers = getDeprecatedAccessFlag(propertyDescriptor);
    for (AnnotationCodegen.JvmFlagAnnotation flagAnnotation : AnnotationCodegen.FIELD_FLAGS) {
        if (flagAnnotation.hasAnnotation(propertyDescriptor.getOriginal())) {
            modifiers |= flagAnnotation.getJvmFlag();
        }
    }
    if (kind == OwnerKind.PACKAGE) {
        modifiers |= ACC_STATIC;
    }
    if (!propertyDescriptor.isLateInit() && (!propertyDescriptor.isVar() || isDelegate)) {
        modifiers |= ACC_FINAL;
    }
    if (AnnotationUtilKt.hasJvmSyntheticAnnotation(propertyDescriptor)) {
        modifiers |= ACC_SYNTHETIC;
    }
    Type type = typeMapper.mapType(kotlinType);
    ClassBuilder builder = v;
    FieldOwnerContext backingFieldContext = context;
    if (AsmUtil.isInstancePropertyWithStaticBackingField(propertyDescriptor)) {
        modifiers |= ACC_STATIC;
        if (JvmAbi.isPropertyWithBackingFieldInOuterClass(propertyDescriptor)) {
            ImplementationBodyCodegen codegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
            builder = codegen.v;
            backingFieldContext = codegen.context;
        }
    }
    modifiers |= getVisibilityForBackingField(propertyDescriptor, isDelegate);
    if (AsmUtil.isPropertyWithBackingFieldCopyInOuterClass(propertyDescriptor)) {
        ImplementationBodyCodegen parentBodyCodegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
        parentBodyCodegen.addCompanionObjectPropertyToCopy(propertyDescriptor, defaultValue);
    }
    String name = backingFieldContext.getFieldName(propertyDescriptor, isDelegate);
    v.getSerializationBindings().put(FIELD_FOR_PROPERTY, propertyDescriptor, Pair.create(type, name));
    FieldVisitor fv = builder.newField(JvmDeclarationOriginKt.OtherOrigin(element, propertyDescriptor), modifiers, name, type.getDescriptor(), isDelegate ? null : typeMapper.mapFieldSignature(kotlinType, propertyDescriptor), defaultValue);
    Annotated fieldAnnotated = new AnnotatedWithFakeAnnotations(propertyDescriptor, annotations);
    AnnotationCodegen.forField(fv, memberCodegen, typeMapper).genAnnotations(fieldAnnotated, type, isDelegate ? AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD : AnnotationUseSiteTarget.FIELD);
}
Also used : Annotated(org.jetbrains.kotlin.descriptors.annotations.Annotated) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type(org.jetbrains.org.objectweb.asm.Type) AnnotatedWithFakeAnnotations(org.jetbrains.kotlin.codegen.annotation.AnnotatedWithFakeAnnotations) FieldVisitor(org.jetbrains.org.objectweb.asm.FieldVisitor)

Example 95 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class PropertyCodegen method getDelegateTypeForProperty.

@NotNull
private KotlinType getDelegateTypeForProperty(@NotNull KtProperty p, @NotNull PropertyDescriptor propertyDescriptor) {
    KotlinType delegateType = null;
    ResolvedCall<FunctionDescriptor> provideDelegateResolvedCall = bindingContext.get(BindingContext.PROVIDE_DELEGATE_RESOLVED_CALL, propertyDescriptor);
    KtExpression delegateExpression = p.getDelegateExpression();
    if (provideDelegateResolvedCall != null) {
        delegateType = provideDelegateResolvedCall.getResultingDescriptor().getReturnType();
    } else if (delegateExpression != null) {
        delegateType = bindingContext.getType(delegateExpression);
    }
    if (delegateType == null) {
        // Delegation convention is unresolved
        delegateType = ErrorUtils.createErrorType("Delegate type");
    }
    return delegateType;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

KotlinType (org.jetbrains.kotlin.types.KotlinType)110 NotNull (org.jetbrains.annotations.NotNull)34 IElementType (com.intellij.psi.tree.IElementType)16 Type (org.jetbrains.org.objectweb.asm.Type)16 Nullable (org.jetbrains.annotations.Nullable)10 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)7 PsiElement (com.intellij.psi.PsiElement)6 Name (org.jetbrains.kotlin.name.Name)6 ArrayList (java.util.ArrayList)4 KtExpression (org.jetbrains.kotlin.psi.KtExpression)4 Map (java.util.Map)3 BothSignatureWriter (org.jetbrains.kotlin.codegen.signature.BothSignatureWriter)3 JvmSignatureWriter (org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter)3 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)3 LocalVariableDescriptor (org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)3 DataFlowInfo (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)3 ExpressionReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver)3 PrimitiveType (org.jetbrains.kotlin.builtins.PrimitiveType)2 CallableDescriptor (org.jetbrains.kotlin.descriptors.CallableDescriptor)2 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)2