Search in sources :

Example 11 with KotlinType

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

the class ExpressionTypingServices method getBodyExpressionType.

@NotNull
public KotlinType getBodyExpressionType(@NotNull BindingTrace trace, @NotNull LexicalScope outerScope, @NotNull DataFlowInfo dataFlowInfo, @NotNull KtDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor) {
    KtExpression bodyExpression = function.getBodyExpression();
    assert bodyExpression != null;
    LexicalScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(outerScope, functionDescriptor, trace, expressionTypingComponents.overloadChecker);
    ExpressionTypingContext context = ExpressionTypingContext.newContext(trace, functionInnerScope, dataFlowInfo, NO_EXPECTED_TYPE);
    KotlinTypeInfo typeInfo = expressionTypingFacade.getTypeInfo(bodyExpression, context, function.hasBlockBody());
    KotlinType type = typeInfo.getType();
    if (type != null) {
        return type;
    } else {
        return ErrorUtils.createErrorType("Error function type");
    }
}
Also used : LexicalScope(org.jetbrains.kotlin.resolve.scopes.LexicalScope) KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with KotlinType

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

the class ControlFlowAnalyzer method process.

public void process(@NotNull BodiesResolveContext c) {
    for (KtFile file : c.getFiles()) {
        checkDeclarationContainer(c, file);
    }
    for (KtClassOrObject aClass : c.getDeclaredClasses().keySet()) {
        checkDeclarationContainer(c, aClass);
    }
    for (KtScript script : c.getScripts().keySet()) {
        checkDeclarationContainer(c, script);
    }
    for (KtSecondaryConstructor constructor : c.getSecondaryConstructors().keySet()) {
        checkSecondaryConstructor(constructor);
    }
    for (Map.Entry<KtNamedFunction, SimpleFunctionDescriptor> entry : c.getFunctions().entrySet()) {
        KtNamedFunction function = entry.getKey();
        SimpleFunctionDescriptor functionDescriptor = entry.getValue();
        KotlinType expectedReturnType = !function.hasBlockBody() && !function.hasDeclaredReturnType() ? NO_EXPECTED_TYPE : functionDescriptor.getReturnType();
        checkFunction(c, function, expectedReturnType);
    }
    for (Map.Entry<KtProperty, PropertyDescriptor> entry : c.getProperties().entrySet()) {
        KtProperty property = entry.getKey();
        PropertyDescriptor propertyDescriptor = entry.getValue();
        checkProperty(c, property, propertyDescriptor);
    }
}
Also used : PropertyDescriptor(org.jetbrains.kotlin.descriptors.PropertyDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType) SimpleFunctionDescriptor(org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor) Map(java.util.Map)

Example 13 with KotlinType

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

the class InlineCodegen method createMethodNode.

@NotNull
static SMAPAndMethodNode createMethodNode(@NotNull final FunctionDescriptor functionDescriptor, @NotNull JvmMethodSignature jvmSignature, @NotNull ExpressionCodegen codegen, @NotNull CodegenContext context, boolean callDefault, @Nullable ResolvedCall<?> resolvedCall) {
    if (InlineCodegenUtil.isSpecialEnumMethod(functionDescriptor)) {
        assert resolvedCall != null : "Resolved call for " + functionDescriptor + " should be not null";
        Map<TypeParameterDescriptor, KotlinType> arguments = resolvedCall.getTypeArguments();
        assert arguments.size() == 1 : "Resolved call for " + functionDescriptor + " should have 1 type argument";
        MethodNode node = InlineCodegenUtil.createSpecialEnumMethodBody(codegen, functionDescriptor.getName().asString(), arguments.keySet().iterator().next().getDefaultType(), codegen.getState().getTypeMapper());
        return new SMAPAndMethodNode(node, SMAPParser.parseOrCreateDefault(null, null, "fake", -1, -1));
    } else if (CoroutineCodegenUtilKt.isBuiltInSuspendCoroutineOrReturnInJvm(functionDescriptor)) {
        return new SMAPAndMethodNode(CoroutineCodegenUtilKt.createMethodNodeForSuspendCoroutineOrReturn(functionDescriptor, codegen.getState().getTypeMapper()), SMAPParser.parseOrCreateDefault(null, null, "fake", -1, -1));
    }
    final GenerationState state = codegen.getState();
    final Method asmMethod = callDefault ? state.getTypeMapper().mapDefaultMethod(functionDescriptor, context.getContextKind()) : jvmSignature.getAsmMethod();
    MethodId methodId = new MethodId(DescriptorUtils.getFqNameSafe(functionDescriptor.getContainingDeclaration()), asmMethod);
    final CallableMemberDescriptor directMember = getDirectMemberAndCallableFromObject(functionDescriptor);
    if (!isBuiltInArrayIntrinsic(functionDescriptor) && !(directMember instanceof DeserializedCallableMemberDescriptor)) {
        return doCreateMethodNodeFromSource(functionDescriptor, jvmSignature, codegen, context, callDefault, state, asmMethod);
    }
    SMAPAndMethodNode resultInCache = InlineCacheKt.getOrPut(state.getInlineCache().getMethodNodeById(), methodId, new Function0<SMAPAndMethodNode>() {

        @Override
        public SMAPAndMethodNode invoke() {
            SMAPAndMethodNode result = doCreateMethodNodeFromCompiled(directMember, state, asmMethod);
            if (result == null) {
                throw new IllegalStateException("Couldn't obtain compiled function body for " + functionDescriptor);
            }
            return result;
        }
    });
    return resultInCache.copyWithNewNode(cloneMethodNode(resultInCache.getNode()));
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) Method(org.jetbrains.org.objectweb.asm.commons.Method) DeserializedCallableMemberDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) DeserializedCallableMemberDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with KotlinType

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

the class InlineCodegenUtil method createSpecialEnumMethodBody.

public static MethodNode createSpecialEnumMethodBody(@NotNull ExpressionCodegen codegen, @NotNull String name, @NotNull KotlinType type, @NotNull KotlinTypeMapper typeMapper) {
    boolean isValueOf = "enumValueOf".equals(name);
    Type invokeType = typeMapper.mapType(type);
    String desc = getSpecialEnumFunDescriptor(invokeType, isValueOf);
    MethodNode node = new MethodNode(API, Opcodes.ACC_STATIC, "fake", desc, null, null);
    codegen.putReifiedOperationMarkerIfTypeIsReifiedParameter(type, ReifiedTypeInliner.OperationKind.ENUM_REIFIED, new InstructionAdapter(node));
    if (isValueOf) {
        node.visitInsn(Opcodes.ACONST_NULL);
        node.visitVarInsn(Opcodes.ALOAD, 0);
        node.visitMethodInsn(Opcodes.INVOKESTATIC, ENUM_TYPE.getInternalName(), "valueOf", Type.getMethodDescriptor(ENUM_TYPE, JAVA_CLASS_TYPE, AsmTypes.JAVA_STRING_TYPE), false);
    } else {
        node.visitInsn(Opcodes.ICONST_0);
        node.visitTypeInsn(Opcodes.ANEWARRAY, ENUM_TYPE.getInternalName());
    }
    node.visitInsn(Opcodes.ARETURN);
    node.visitMaxs(isValueOf ? 3 : 2, isValueOf ? 1 : 0);
    return node;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)

Example 15 with KotlinType

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

the class CodegenAnnotatingVisitor method recordLocalVariablePropertyMetadata.

private void recordLocalVariablePropertyMetadata(LocalVariableDescriptor variableDescriptor) {
    KotlinType delegateType = JvmCodegenUtil.getPropertyDelegateType(variableDescriptor, bindingContext);
    if (delegateType == null)
        return;
    LocalVariableDescriptor delegateVariableDescriptor = new LocalVariableDescriptor(variableDescriptor.getContainingDeclaration(), Annotations.Companion.getEMPTY(), variableDescriptor.getName(), delegateType, false, false, SourceElement.NO_SOURCE);
    bindingTrace.record(LOCAL_VARIABLE_DELEGATE, variableDescriptor, delegateVariableDescriptor);
    LocalVariableDescriptor metadataVariableDescriptor = new LocalVariableDescriptor(variableDescriptor.getContainingDeclaration(), Annotations.Companion.getEMPTY(), Name.identifier(variableDescriptor.getName().asString() + "$metadata"), ReflectionTypes.Companion.createKPropertyStarType(DescriptorUtilsKt.getModule(variableDescriptor)), false, false, SourceElement.NO_SOURCE);
    bindingTrace.record(LOCAL_VARIABLE_PROPERTY_METADATA, variableDescriptor, metadataVariableDescriptor);
}
Also used : LocalVariableDescriptor(org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType)

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