Search in sources :

Example 6 with LexicalWritableScope

use of org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope in project kotlin by JetBrains.

the class ControlStructureTypingVisitor method visitDoWhileExpression.

public KotlinTypeInfo visitDoWhileExpression(KtDoWhileExpression expression, ExpressionTypingContext contextWithExpectedType, boolean isStatement) {
    if (!isStatement)
        return components.dataFlowAnalyzer.illegalStatementType(expression, contextWithExpectedType, facade);
    ExpressionTypingContext context = contextWithExpectedType.replaceExpectedType(NO_EXPECTED_TYPE).replaceContextDependency(INDEPENDENT);
    KtExpression body = expression.getBody();
    LexicalScope conditionScope = context.scope;
    // Preliminary analysis
    PreliminaryLoopVisitor loopVisitor = PreliminaryLoopVisitor.visitLoop(expression);
    context = context.replaceDataFlowInfo(loopVisitor.clearDataFlowInfoForAssignedLocalVariables(context.dataFlowInfo, components.languageVersionSettings));
    // Here we must record data flow information at the end of the body (or at the first jump, to be precise) and
    // .and it with entrance data flow information, because do-while body is executed at least once
    // See KT-6283
    KotlinTypeInfo bodyTypeInfo;
    if (body instanceof KtLambdaExpression) {
        // As a matter of fact, function literal is always unused at this point
        bodyTypeInfo = facade.getTypeInfo(body, context.replaceScope(context.scope));
    } else if (body != null) {
        LexicalWritableScope writableScope = newWritableScopeImpl(context, LexicalScopeKind.DO_WHILE_BODY, components.overloadChecker);
        conditionScope = writableScope;
        List<KtExpression> block;
        if (body instanceof KtBlockExpression) {
            block = ((KtBlockExpression) body).getStatements();
        } else {
            block = Collections.singletonList(body);
        }
        bodyTypeInfo = components.expressionTypingServices.getBlockReturnedTypeWithWritableScope(writableScope, block, CoercionStrategy.NO_COERCION, context);
    } else {
        bodyTypeInfo = TypeInfoFactoryKt.noTypeInfo(context);
    }
    KtExpression condition = expression.getCondition();
    DataFlowInfo conditionDataFlowInfo = checkCondition(conditionScope, condition, context);
    DataFlowInfo dataFlowInfo;
    // Without jumps out, condition is entered and false, with jumps out, we know nothing about it
    if (!containsJumpOutOfLoop(expression, context)) {
        dataFlowInfo = components.dataFlowAnalyzer.extractDataFlowInfoFromCondition(condition, false, context).and(conditionDataFlowInfo);
    } else {
        dataFlowInfo = context.dataFlowInfo;
    }
    // If it's a function literal, it appears always unused so it's no matter what we do at this point
    if (body != null) {
        // We should take data flow info from the first jump point,
        // but without affecting changing variables
        dataFlowInfo = dataFlowInfo.and(loopVisitor.clearDataFlowInfoForAssignedLocalVariables(bodyTypeInfo.getJumpFlowInfo(), components.languageVersionSettings));
    }
    return components.dataFlowAnalyzer.checkType(bodyTypeInfo.replaceType(components.builtIns.getUnitType()), expression, contextWithExpectedType).replaceDataFlowInfo(dataFlowInfo);
}
Also used : LexicalScope(org.jetbrains.kotlin.resolve.scopes.LexicalScope) LexicalWritableScope(org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope) ArrayList(java.util.ArrayList) List(java.util.List) DataFlowInfo(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)

Example 7 with LexicalWritableScope

use of org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope in project kotlin by JetBrains.

the class ControlStructureTypingVisitor method visitForExpression.

public KotlinTypeInfo visitForExpression(KtForExpression expression, ExpressionTypingContext contextWithExpectedType, boolean isStatement) {
    if (!isStatement)
        return components.dataFlowAnalyzer.illegalStatementType(expression, contextWithExpectedType, facade);
    ExpressionTypingContext context = contextWithExpectedType.replaceExpectedType(NO_EXPECTED_TYPE).replaceContextDependency(INDEPENDENT);
    // Preliminary analysis
    PreliminaryLoopVisitor loopVisitor = PreliminaryLoopVisitor.visitLoop(expression);
    context = context.replaceDataFlowInfo(loopVisitor.clearDataFlowInfoForAssignedLocalVariables(context.dataFlowInfo, components.languageVersionSettings));
    KtExpression loopRange = expression.getLoopRange();
    KotlinType expectedParameterType = null;
    KotlinTypeInfo loopRangeInfo;
    if (loopRange != null) {
        ExpressionReceiver loopRangeReceiver = getExpressionReceiver(facade, loopRange, context.replaceScope(context.scope));
        loopRangeInfo = facade.getTypeInfo(loopRange, context);
        if (loopRangeReceiver != null) {
            expectedParameterType = components.forLoopConventionsChecker.checkIterableConvention(loopRangeReceiver, context);
        }
    } else {
        loopRangeInfo = TypeInfoFactoryKt.noTypeInfo(context);
    }
    LexicalWritableScope loopScope = newWritableScopeImpl(context, LexicalScopeKind.FOR, components.overloadChecker);
    KtParameter loopParameter = expression.getLoopParameter();
    if (loopParameter != null) {
        VariableDescriptor variableDescriptor = createLoopParameterDescriptor(loopParameter, expectedParameterType, context);
        ModifiersChecker.ModifiersCheckingProcedure modifiersCheckingProcedure = components.modifiersChecker.withTrace(context.trace);
        modifiersCheckingProcedure.checkModifiersForLocalDeclaration(loopParameter, variableDescriptor);
        components.identifierChecker.checkDeclaration(loopParameter, context.trace);
        loopScope.addVariableDescriptor(variableDescriptor);
        KtDestructuringDeclaration multiParameter = loopParameter.getDestructuringDeclaration();
        if (multiParameter != null) {
            KotlinType elementType = expectedParameterType == null ? ErrorUtils.createErrorType("Loop range has no type") : expectedParameterType;
            TransientReceiver iteratorNextAsReceiver = new TransientReceiver(elementType);
            components.annotationResolver.resolveAnnotationsWithArguments(loopScope, loopParameter.getModifierList(), context.trace);
            components.destructuringDeclarationResolver.defineLocalVariablesFromDestructuringDeclaration(loopScope, multiParameter, iteratorNextAsReceiver, loopRange, context);
            modifiersCheckingProcedure.checkModifiersForDestructuringDeclaration(multiParameter);
            components.identifierChecker.checkDeclaration(multiParameter, context.trace);
        }
    }
    KtExpression body = expression.getBody();
    KotlinTypeInfo bodyTypeInfo;
    if (body != null) {
        bodyTypeInfo = components.expressionTypingServices.getBlockReturnedTypeWithWritableScope(loopScope, Collections.singletonList(body), CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(loopRangeInfo.getDataFlowInfo()));
    } else {
        bodyTypeInfo = loopRangeInfo;
    }
    return components.dataFlowAnalyzer.checkType(bodyTypeInfo.replaceType(components.builtIns.getUnitType()), expression, contextWithExpectedType).replaceDataFlowInfo(loopRangeInfo.getDataFlowInfo());
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) ExpressionReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver) LexicalWritableScope(org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope) ModifiersChecker(org.jetbrains.kotlin.resolve.ModifiersChecker) TransientReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver)

Aggregations

LexicalWritableScope (org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope)7 DataFlowInfo (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)4 KotlinType (org.jetbrains.kotlin.types.KotlinType)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 ModifiersChecker (org.jetbrains.kotlin.resolve.ModifiersChecker)2 List (java.util.List)1 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)1 ScriptDescriptor (org.jetbrains.kotlin.descriptors.ScriptDescriptor)1 BindingContext (org.jetbrains.kotlin.resolve.BindingContext)1 MutableDataFlowInfoForArguments (org.jetbrains.kotlin.resolve.calls.model.MutableDataFlowInfoForArguments)1 ResolvedCall (org.jetbrains.kotlin.resolve.calls.model.ResolvedCall)1 DataFlowValue (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue)1 LexicalScope (org.jetbrains.kotlin.resolve.scopes.LexicalScope)1 TraceBasedLocalRedeclarationChecker (org.jetbrains.kotlin.resolve.scopes.TraceBasedLocalRedeclarationChecker)1 ExpressionReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver)1 TransientReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver)1 ControlStructureTypingUtils.createDataFlowInfoForArgumentsForIfCall (org.jetbrains.kotlin.types.expressions.ControlStructureTypingUtils.createDataFlowInfoForArgumentsForIfCall)1