Search in sources :

Example 1 with DataFlowValue

use of org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue in project kotlin by JetBrains.

the class BasicExpressionTypingVisitor method visitBinaryWithTypeRHSExpression.

@Override
public KotlinTypeInfo visitBinaryWithTypeRHSExpression(@NotNull KtBinaryExpressionWithTypeRHS expression, ExpressionTypingContext context) {
    ExpressionTypingContext contextWithNoExpectedType = context.replaceExpectedType(NO_EXPECTED_TYPE).replaceContextDependency(INDEPENDENT);
    KtExpression left = expression.getLeft();
    KtTypeReference right = expression.getRight();
    if (right == null) {
        return facade.getTypeInfo(left, contextWithNoExpectedType).clearType();
    }
    IElementType operationType = expression.getOperationReference().getReferencedNameElementType();
    boolean allowBareTypes = BARE_TYPES_ALLOWED.contains(operationType);
    TypeResolutionContext typeResolutionContext = new TypeResolutionContext(context.scope, context.trace, true, allowBareTypes, context.isDebuggerContext);
    PossiblyBareType possiblyBareTarget = components.typeResolver.resolvePossiblyBareType(typeResolutionContext, right);
    KotlinTypeInfo typeInfo = facade.getTypeInfo(left, contextWithNoExpectedType);
    KotlinType subjectType = typeInfo.getType();
    KotlinType targetType = reconstructBareType(right, possiblyBareTarget, subjectType, context.trace, components.builtIns);
    if (subjectType != null) {
        checkBinaryWithTypeRHS(expression, contextWithNoExpectedType, targetType, subjectType);
        DataFlowInfo dataFlowInfo = typeInfo.getDataFlowInfo();
        if (operationType == AS_KEYWORD) {
            DataFlowValue value = createDataFlowValue(left, subjectType, context);
            typeInfo = typeInfo.replaceDataFlowInfo(dataFlowInfo.establishSubtyping(value, targetType, components.languageVersionSettings));
        }
    }
    KotlinType result = operationType == AS_SAFE ? TypeUtils.makeNullable(targetType) : targetType;
    KotlinTypeInfo resultTypeInfo = components.dataFlowAnalyzer.checkType(typeInfo.replaceType(result), expression, context);
    RttiExpressionInformation rttiInformation = new RttiExpressionInformation(expression.getLeft(), subjectType, result, operationType == AS_SAFE ? RttiOperation.SAFE_AS : RttiOperation.AS);
    for (RttiExpressionChecker checker : components.rttiExpressionCheckers) {
        checker.check(rttiInformation, expression, context.trace);
    }
    return resultTypeInfo;
}
Also used : DataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue) DataFlowValueFactory.createDataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory.createDataFlowValue) IElementType(com.intellij.psi.tree.IElementType) DataFlowInfo(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)

Example 2 with DataFlowValue

use of org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue in project kotlin by JetBrains.

the class BasicExpressionTypingVisitor method visitParenthesizedExpression.

@Override
public KotlinTypeInfo visitParenthesizedExpression(@NotNull KtParenthesizedExpression expression, ExpressionTypingContext context) {
    KtExpression innerExpression = expression.getExpression();
    if (innerExpression == null) {
        return TypeInfoFactoryKt.noTypeInfo(context);
    }
    KotlinTypeInfo result = facade.getTypeInfo(innerExpression, context.replaceScope(context.scope));
    KotlinType resultType = result.getType();
    if (resultType != null) {
        DataFlowValue innerValue = DataFlowValueFactory.createDataFlowValue(innerExpression, resultType, context);
        DataFlowValue resultValue = DataFlowValueFactory.createDataFlowValue(expression, resultType, context);
        result = result.replaceDataFlowInfo(result.getDataFlowInfo().assign(resultValue, innerValue, components.languageVersionSettings));
    }
    return result;
}
Also used : DataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue) DataFlowValueFactory.createDataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory.createDataFlowValue)

Example 3 with DataFlowValue

use of org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue in project kotlin by JetBrains.

the class BasicExpressionTypingVisitor method visitExclExclExpression.

private KotlinTypeInfo visitExclExclExpression(@NotNull KtUnaryExpression expression, @NotNull ExpressionTypingContext context) {
    KtExpression baseExpression = expression.getBaseExpression();
    assert baseExpression != null;
    KtSimpleNameExpression operationSign = expression.getOperationReference();
    assert operationSign.getReferencedNameElementType() == KtTokens.EXCLEXCL;
    // TODO: something must be done for not to lose safe call chain information here
    // See also CallExpressionResolver.getSimpleNameExpressionTypeInfo, .getQualifiedExpressionTypeInfo
    Call call = createCallForSpecialConstruction(expression, expression.getOperationReference(), Collections.singletonList(baseExpression));
    components.controlStructureTypingUtils.resolveSpecialConstructionAsCall(call, ResolveConstruct.EXCL_EXCL, Collections.singletonList("baseExpr"), Collections.singletonList(true), context, null);
    KotlinTypeInfo baseTypeInfo = BindingContextUtils.getRecordedTypeInfo(baseExpression, context.trace.getBindingContext());
    if (ArgumentTypeResolver.isFunctionLiteralArgument(baseExpression, context)) {
        context.trace.report(NOT_NULL_ASSERTION_ON_LAMBDA_EXPRESSION.on(operationSign));
        if (baseTypeInfo == null) {
            return TypeInfoFactoryKt.createTypeInfo(ErrorUtils.createErrorType("Unresolved lambda expression"), context);
        }
        return baseTypeInfo;
    }
    assert baseTypeInfo != null : "Base expression was not processed: " + expression;
    KotlinType baseType = baseTypeInfo.getType();
    if (baseType == null) {
        return baseTypeInfo;
    }
    DataFlowInfo dataFlowInfo = baseTypeInfo.getDataFlowInfo();
    if (isKnownToBeNotNull(baseExpression, context) && !baseType.isError()) {
        context.trace.report(UNNECESSARY_NOT_NULL_ASSERTION.on(operationSign, TypeUtils.makeNotNullable(baseType)));
    } else {
        DataFlowValue value = createDataFlowValue(baseExpression, baseType, context);
        baseTypeInfo = baseTypeInfo.replaceDataFlowInfo(dataFlowInfo.disequate(value, DataFlowValue.nullValue(components.builtIns), components.languageVersionSettings));
    }
    KotlinType resultingType = TypeUtils.makeNotNullable(baseType);
    if (context.contextDependency == DEPENDENT) {
        return baseTypeInfo.replaceType(resultingType);
    }
    // The call to checkType() is only needed here to execute additionalTypeCheckers, hence the NO_EXPECTED_TYPE
    return components.dataFlowAnalyzer.checkType(baseTypeInfo.replaceType(resultingType), expression, context.replaceExpectedType(NO_EXPECTED_TYPE));
}
Also used : ResolvedCall(org.jetbrains.kotlin.resolve.calls.model.ResolvedCall) DataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue) DataFlowValueFactory.createDataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory.createDataFlowValue) DataFlowInfo(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)

Example 4 with DataFlowValue

use of org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue in project kotlin by JetBrains.

the class BasicExpressionTypingVisitor method checkNull.

private static void checkNull(@NotNull KtSimpleNameExpression expression, @NotNull ExpressionTypingContext context, @Nullable KotlinType type) {
    // Receivers are normally analyzed at resolve, with an exception of KT-10175
    if (type != null && !type.isError() && !isLValueOrUnsafeReceiver(expression)) {
        DataFlowValue dataFlowValue = DataFlowValueFactory.createDataFlowValue(expression, type, context);
        Nullability nullability = context.dataFlowInfo.getStableNullability(dataFlowValue);
        if (!nullability.canBeNonNull() && nullability.canBeNull()) {
            if (isDangerousWithNull(expression, context)) {
                context.trace.report(ALWAYS_NULL.on(expression));
            } else {
                context.trace.record(SMARTCAST_NULL, expression);
            }
        }
    }
}
Also used : DataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue) DataFlowValueFactory.createDataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory.createDataFlowValue) Nullability(org.jetbrains.kotlin.resolve.calls.smartcasts.Nullability)

Example 5 with DataFlowValue

use of org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue in project kotlin by JetBrains.

the class BasicExpressionTypingVisitor method establishSubtypingForTypeRHS.

@NotNull
private static DataFlowInfo establishSubtypingForTypeRHS(@NotNull KtBinaryExpressionWithTypeRHS left, @NotNull DataFlowInfo dataFlowInfo, @NotNull ExpressionTypingContext context, @NotNull LanguageVersionSettings languageVersionSettings) {
    IElementType operationType = left.getOperationReference().getReferencedNameElementType();
    if (operationType == AS_SAFE) {
        KtExpression underSafeAs = left.getLeft();
        KotlinType underSafeAsType = context.trace.getType(underSafeAs);
        if (underSafeAsType != null) {
            DataFlowValue underSafeAsValue = createDataFlowValue(underSafeAs, underSafeAsType, context);
            KotlinType targetType = context.trace.get(BindingContext.TYPE, left.getRight());
            if (targetType != null) {
                return dataFlowInfo.establishSubtyping(underSafeAsValue, targetType, languageVersionSettings);
            }
        }
    }
    return dataFlowInfo;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) DataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue) DataFlowValueFactory.createDataFlowValue(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory.createDataFlowValue) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DataFlowValue (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue)10 DataFlowValueFactory.createDataFlowValue (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory.createDataFlowValue)7 DataFlowInfo (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)6 ResolvedCall (org.jetbrains.kotlin.resolve.calls.model.ResolvedCall)4 IElementType (com.intellij.psi.tree.IElementType)3 NotNull (org.jetbrains.annotations.NotNull)3 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 Name (org.jetbrains.kotlin.name.Name)1 BindingContext (org.jetbrains.kotlin.resolve.BindingContext)1 CallPosition (org.jetbrains.kotlin.resolve.calls.context.CallPosition)1 MutableDataFlowInfoForArguments (org.jetbrains.kotlin.resolve.calls.model.MutableDataFlowInfoForArguments)1 Nullability (org.jetbrains.kotlin.resolve.calls.smartcasts.Nullability)1 LexicalWritableScope (org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope)1 ExpressionReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver)1 ControlStructureTypingUtils.createDataFlowInfoForArgumentsForIfCall (org.jetbrains.kotlin.types.expressions.ControlStructureTypingUtils.createDataFlowInfoForArgumentsForIfCall)1