Search in sources :

Example 1 with LexicalScope

use of org.jetbrains.kotlin.resolve.scopes.LexicalScope 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 2 with LexicalScope

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

the class KotlinOverloadTest method makeFunction.

private FunctionDescriptor makeFunction(String funDecl) {
    KtNamedFunction function = KtPsiFactoryKt.KtPsiFactory(getProject()).createFunction(funDecl);
    LexicalScope scope = TypeTestUtilsKt.builtInPackageAsLexicalScope(module);
    return functionDescriptorResolver.resolveFunctionDescriptor(module, scope, function, KotlinTestUtils.DUMMY_TRACE, DataFlowInfoFactory.EMPTY);
}
Also used : LexicalScope(org.jetbrains.kotlin.resolve.scopes.LexicalScope) KtNamedFunction(org.jetbrains.kotlin.psi.KtNamedFunction)

Example 3 with LexicalScope

use of org.jetbrains.kotlin.resolve.scopes.LexicalScope 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 4 with LexicalScope

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

the class ResolveSession method createAnnotations.

private LazyAnnotations createAnnotations(KtFile file, List<KtAnnotationEntry> annotationEntries) {
    LexicalScope scope = fileScopeProvider.getFileResolutionScope(file);
    LazyAnnotationsContextImpl lazyAnnotationContext = new LazyAnnotationsContextImpl(annotationResolver, storageManager, trace, scope);
    return new LazyAnnotations(lazyAnnotationContext, annotationEntries);
}
Also used : LexicalScope(org.jetbrains.kotlin.resolve.scopes.LexicalScope) LazyAnnotations(org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotations) LazyAnnotationsContextImpl(org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationsContextImpl)

Example 5 with LexicalScope

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

the class CallExpressionTranslator method translateJsCode.

@NotNull
private JsNode translateJsCode() {
    List<? extends ValueArgument> arguments = expression.getValueArguments();
    KtExpression argumentExpression = arguments.get(0).getArgumentExpression();
    assert argumentExpression != null;
    List<JsStatement> statements = parseJsCode(argumentExpression);
    int size = statements.size();
    JsNode node;
    if (size == 0) {
        node = JsLiteral.NULL;
    } else if (size > 1) {
        node = new JsBlock(statements);
    } else {
        JsStatement resultStatement = statements.get(0);
        if (resultStatement instanceof JsExpressionStatement) {
            node = ((JsExpressionStatement) resultStatement).getExpression();
        } else {
            node = resultStatement;
        }
    }
    LexicalScope lexicalScope = context().bindingContext().get(BindingContextSlicesJsKt.LEXICAL_SCOPE_FOR_JS, resolvedCall);
    Map<JsName, JsExpression> replacements = new HashMap<JsName, JsExpression>();
    if (lexicalScope != null) {
        Set<JsName> references = CollectUtilsKt.collectUsedNames(node);
        references.removeAll(CollectUtilsKt.collectDefinedNames(node));
        for (JsName name : references) {
            VariableDescriptor variable = getVariableByName(lexicalScope, Name.identifier(name.getIdent()));
            if (variable != null) {
                replacements.put(name, ReferenceTranslator.translateAsValueReference(variable, context()));
            }
        }
        if (!replacements.isEmpty()) {
            node = RewriteUtilsKt.replaceNames(node, replacements);
        }
    }
    return node;
}
Also used : LexicalScope(org.jetbrains.kotlin.resolve.scopes.LexicalScope) KtExpression(org.jetbrains.kotlin.psi.KtExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LexicalScope (org.jetbrains.kotlin.resolve.scopes.LexicalScope)8 NotNull (org.jetbrains.annotations.NotNull)2 KtNamedFunction (org.jetbrains.kotlin.psi.KtNamedFunction)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Nullable (org.jetbrains.annotations.Nullable)1 ReceiverParameterDescriptorImpl (org.jetbrains.kotlin.descriptors.impl.ReceiverParameterDescriptorImpl)1 KtExpression (org.jetbrains.kotlin.psi.KtExpression)1 DataFlowInfo (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)1 LazyAnnotations (org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotations)1 LazyAnnotationsContextImpl (org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationsContextImpl)1 LexicalScopeImpl (org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl)1 LexicalWritableScope (org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope)1 TransientReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1