Search in sources :

Example 61 with KotlinType

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

the class ExpressionTypingVisitorForStatements method visitAssignment.

@NotNull
protected KotlinTypeInfo visitAssignment(KtBinaryExpression expression, ExpressionTypingContext contextWithExpectedType) {
    ExpressionTypingContext context = contextWithExpectedType.replaceExpectedType(NO_EXPECTED_TYPE).replaceScope(scope).replaceContextDependency(INDEPENDENT);
    KtExpression leftOperand = expression.getLeft();
    if (leftOperand instanceof KtAnnotatedExpression) {
        basic.resolveAnnotationsOnExpression((KtAnnotatedExpression) leftOperand, context);
    }
    KtExpression left = deparenthesize(leftOperand);
    KtExpression right = expression.getRight();
    if (left instanceof KtArrayAccessExpression) {
        KtArrayAccessExpression arrayAccessExpression = (KtArrayAccessExpression) left;
        if (right == null)
            return TypeInfoFactoryKt.noTypeInfo(context);
        KotlinTypeInfo typeInfo = basic.resolveArrayAccessSetMethod(arrayAccessExpression, right, context, context.trace);
        basic.checkLValue(context.trace, context, arrayAccessExpression, right, expression);
        return typeInfo.replaceType(checkAssignmentType(typeInfo.getType(), expression, contextWithExpectedType));
    }
    KotlinTypeInfo leftInfo = ExpressionTypingUtils.getTypeInfoOrNullType(left, context, facade);
    KotlinType expectedType = refineTypeFromPropertySetterIfPossible(context.trace.getBindingContext(), leftOperand, leftInfo.getType());
    DataFlowInfo dataFlowInfo = leftInfo.getDataFlowInfo();
    KotlinTypeInfo resultInfo;
    if (right != null) {
        resultInfo = facade.getTypeInfo(right, context.replaceDataFlowInfo(dataFlowInfo).replaceExpectedType(expectedType).replaceCallPosition(new CallPosition.PropertyAssignment(leftOperand)));
        dataFlowInfo = resultInfo.getDataFlowInfo();
        KotlinType rightType = resultInfo.getType();
        if (left != null && expectedType != null && rightType != null) {
            DataFlowValue leftValue = DataFlowValueFactory.createDataFlowValue(left, expectedType, context);
            DataFlowValue rightValue = DataFlowValueFactory.createDataFlowValue(right, rightType, context);
            // We cannot say here anything new about rightValue except it has the same value as leftValue
            resultInfo = resultInfo.replaceDataFlowInfo(dataFlowInfo.assign(leftValue, rightValue, components.languageVersionSettings));
        }
    } else {
        resultInfo = leftInfo;
    }
    if (expectedType != null && leftOperand != null) {
        //if expectedType == null, some other error has been generated
        basic.checkLValue(context.trace, context, leftOperand, right, expression);
    }
    return resultInfo.replaceType(components.dataFlowAnalyzer.checkStatementType(expression, contextWithExpectedType));
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) CallPosition(org.jetbrains.kotlin.resolve.calls.context.CallPosition) DataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue) DataFlowInfo(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with KotlinType

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

the class ForLoopConventionsChecker method checkIterableConvention.

@Nullable
public KotlinType checkIterableConvention(@NotNull ExpressionReceiver loopRange, @NotNull ExpressionTypingContext context) {
    KtExpression loopRangeExpression = loopRange.getExpression();
    // Make a fake call loopRange.iterator(), and try to resolve it
    OverloadResolutionResults<FunctionDescriptor> iteratorResolutionResults = fakeCallResolver.resolveFakeCall(context, loopRange, OperatorNameConventions.ITERATOR, loopRangeExpression, loopRangeExpression, FakeCallKind.ITERATOR, Collections.<KtExpression>emptyList());
    if (!iteratorResolutionResults.isSuccess())
        return null;
    ResolvedCall<FunctionDescriptor> iteratorResolvedCall = iteratorResolutionResults.getResultingCall();
    context.trace.record(LOOP_RANGE_ITERATOR_RESOLVED_CALL, loopRangeExpression, iteratorResolvedCall);
    FunctionDescriptor iteratorFunction = iteratorResolvedCall.getResultingDescriptor();
    checkIfOperatorModifierPresent(loopRangeExpression, iteratorFunction, context.trace);
    KotlinType iteratorType = iteratorFunction.getReturnType();
    //noinspection ConstantConditions
    KotlinType hasNextType = checkConventionForIterator(context, loopRangeExpression, iteratorType, OperatorNameConventions.HAS_NEXT, HAS_NEXT_FUNCTION_AMBIGUITY, HAS_NEXT_MISSING, HAS_NEXT_FUNCTION_NONE_APPLICABLE, LOOP_RANGE_HAS_NEXT_RESOLVED_CALL);
    if (hasNextType != null && !builtIns.isBooleanOrSubtype(hasNextType)) {
        context.trace.report(HAS_NEXT_FUNCTION_TYPE_MISMATCH.on(loopRangeExpression, hasNextType));
    }
    return checkConventionForIterator(context, loopRangeExpression, iteratorType, OperatorNameConventions.NEXT, NEXT_AMBIGUITY, NEXT_MISSING, NEXT_NONE_APPLICABLE, LOOP_RANGE_NEXT_RESOLVED_CALL);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 63 with KotlinType

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

the class AnnotationResolverImpl method resolveAnnotationType.

@Override
@NotNull
public KotlinType resolveAnnotationType(@NotNull LexicalScope scope, @NotNull KtAnnotationEntry entryElement, @NotNull BindingTrace trace) {
    KtTypeReference typeReference = entryElement.getTypeReference();
    if (typeReference == null) {
        return ErrorUtils.createErrorType("No type reference: " + entryElement.getText());
    }
    KotlinType type = typeResolver.resolveType(scope, typeReference, trace, true);
    if (!(type.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor)) {
        return ErrorUtils.createErrorType("Not an annotation: " + type);
    }
    return type;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 64 with KotlinType

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

the class CompileTimeConstantUtils method checkConstructorParametersType.

public static void checkConstructorParametersType(@NotNull List<KtParameter> parameters, @NotNull BindingTrace trace) {
    for (KtParameter parameter : parameters) {
        VariableDescriptor parameterDescriptor = trace.getBindingContext().get(VALUE_PARAMETER, parameter);
        if (parameterDescriptor == null)
            continue;
        KotlinType parameterType = parameterDescriptor.getType();
        KtTypeReference typeReference = parameter.getTypeReference();
        if (typeReference != null) {
            if (parameterType.isMarkedNullable()) {
                trace.report(NULLABLE_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
            } else if (!isAcceptableTypeForAnnotationParameter(parameterType)) {
                trace.report(INVALID_TYPE_OF_ANNOTATION_MEMBER.on(typeReference));
            }
        }
    }
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KtParameter(org.jetbrains.kotlin.psi.KtParameter) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) VariableDescriptor(org.jetbrains.kotlin.descriptors.VariableDescriptor)

Example 65 with KotlinType

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

the class ControlFlowAnalyzer method checkProperty.

private void checkProperty(@NotNull BodiesResolveContext c, KtProperty property, PropertyDescriptor propertyDescriptor) {
    for (KtPropertyAccessor accessor : property.getAccessors()) {
        PropertyAccessorDescriptor accessorDescriptor = accessor.isGetter() ? propertyDescriptor.getGetter() : propertyDescriptor.getSetter();
        assert accessorDescriptor != null : "no property accessor descriptor " + accessor.getText();
        KotlinType returnType = accessorDescriptor.getReturnType();
        checkFunction(c, accessor, returnType);
    }
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) PropertyAccessorDescriptor(org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor)

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