Search in sources :

Example 16 with KotlinType

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

the class DataFlowAnalyzer method typeHasOverriddenEquals.

private static boolean typeHasOverriddenEquals(@NotNull KotlinType type, @NotNull KtElement lookupElement) {
    Collection<SimpleFunctionDescriptor> members = type.getMemberScope().getContributedFunctions(OperatorNameConventions.EQUALS, new KotlinLookupLocation(lookupElement));
    for (FunctionDescriptor member : members) {
        KotlinType returnType = member.getReturnType();
        if (returnType == null || !KotlinBuiltIns.isBoolean(returnType))
            continue;
        if (member.getValueParameters().size() != 1)
            continue;
        KotlinType parameterType = member.getValueParameters().iterator().next().getType();
        if (!KotlinBuiltIns.isNullableAny(parameterType))
            continue;
        FunctionDescriptor fromSuperClass = getOverriddenDescriptorFromClass(member);
        if (fromSuperClass == null)
            return false;
        ClassifierDescriptor superClassDescriptor = (ClassifierDescriptor) fromSuperClass.getContainingDeclaration();
        // We should have override fun in class other than Any (to prove unknown behaviour)
        return !KotlinBuiltIns.isAnyOrNullableAny(superClassDescriptor.getDefaultType());
    }
    return false;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KotlinLookupLocation(org.jetbrains.kotlin.incremental.KotlinLookupLocation)

Example 17 with KotlinType

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

the class DataFlowAnalyzer method recordExpectedType.

public void recordExpectedType(@NotNull BindingTrace trace, @NotNull KtExpression expression, @NotNull KotlinType expectedType) {
    if (expectedType != NO_EXPECTED_TYPE) {
        KotlinType normalizeExpectedType = expectedType == UNIT_EXPECTED_TYPE ? builtIns.getUnitType() : expectedType;
        trace.record(BindingContext.EXPECTED_EXPRESSION_TYPE, expression, normalizeExpectedType);
    }
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 18 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 19 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 20 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)

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