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");
}
}
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);
}
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);
}
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);
}
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;
}
Aggregations