using guice to wire TopDownAnalyzer beans

This commit is contained in:
Stepan Koltsov
2012-03-07 04:09:12 +04:00
parent 406c81fbb3
commit c3e2fc947d
32 changed files with 596 additions and 376 deletions
+11
View File
@@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="guice-3.0">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/guice/aopalliance.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/guice/guice-3.0.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/guice/javax.inject.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
+1
View File
@@ -8,6 +8,7 @@
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" /> <orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
<orderEntry type="library" name="guice-3.0" level="project" />
</component> </component>
</module> </module>
@@ -18,8 +18,6 @@ package org.jetbrains.jet.lang;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.DescriptorResolver;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices; import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
@@ -49,16 +47,6 @@ public class JetSemanticServices {
return standardLibrary; return standardLibrary;
} }
@NotNull
public DescriptorResolver getClassDescriptorResolver(BindingTrace trace) {
return new DescriptorResolver(this, trace);
}
@NotNull
public ExpressionTypingServices getTypeInferrerServices(@NotNull BindingTrace trace) {
return new ExpressionTypingServices(this, trace);
}
@NotNull @NotNull
public JetTypeChecker getTypeChecker() { public JetTypeChecker getTypeChecker() {
return typeChecker; return typeChecker;
@@ -36,6 +36,7 @@ import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices; import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
import javax.inject.Inject;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -47,46 +48,49 @@ import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE;
*/ */
public class AnnotationResolver { public class AnnotationResolver {
private final BindingTrace trace; private ExpressionTypingServices expressionTypingServices;
private final JetSemanticServices semanticServices; private CallResolver callResolver;
private final CallResolver callResolver;
public AnnotationResolver(JetSemanticServices semanticServices, BindingTrace trace) { @Inject
this.trace = trace; public void setExpressionTypingServices(ExpressionTypingServices expressionTypingServices) {
this.callResolver = new CallResolver(semanticServices, DataFlowInfo.EMPTY); this.expressionTypingServices = expressionTypingServices;
this.semanticServices = semanticServices; }
@Inject
public void setCallResolver(CallResolver.Context callResolverContext) {
this.callResolver = new CallResolver(callResolverContext, DataFlowInfo.EMPTY);
} }
@NotNull @NotNull
public List<AnnotationDescriptor> resolveAnnotations(@NotNull JetScope scope, @Nullable JetModifierList modifierList) { public List<AnnotationDescriptor> resolveAnnotations(@NotNull JetScope scope, @Nullable JetModifierList modifierList, BindingTrace trace) {
if (modifierList == null) { if (modifierList == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
return resolveAnnotations(scope, modifierList.getAnnotationEntries()); return resolveAnnotations(scope, modifierList.getAnnotationEntries(), trace);
} }
@NotNull @NotNull
public List<AnnotationDescriptor> resolveAnnotations(@NotNull JetScope scope, @NotNull List<JetAnnotationEntry> annotationEntryElements) { public List<AnnotationDescriptor> resolveAnnotations(@NotNull JetScope scope, @NotNull List<JetAnnotationEntry> annotationEntryElements, BindingTrace trace) {
if (annotationEntryElements.isEmpty()) return Collections.emptyList(); if (annotationEntryElements.isEmpty()) return Collections.emptyList();
List<AnnotationDescriptor> result = Lists.newArrayList(); List<AnnotationDescriptor> result = Lists.newArrayList();
for (JetAnnotationEntry entryElement : annotationEntryElements) { for (JetAnnotationEntry entryElement : annotationEntryElements) {
AnnotationDescriptor descriptor = new AnnotationDescriptor(); AnnotationDescriptor descriptor = new AnnotationDescriptor();
resolveAnnotationStub(scope, entryElement, descriptor); resolveAnnotationStub(scope, entryElement, descriptor, trace);
result.add(descriptor); result.add(descriptor);
} }
return result; return result;
} }
public void resolveAnnotationStub(@NotNull JetScope scope, @NotNull JetAnnotationEntry entryElement, public void resolveAnnotationStub(@NotNull JetScope scope, @NotNull JetAnnotationEntry entryElement,
@NotNull AnnotationDescriptor descriptor) { @NotNull AnnotationDescriptor descriptor, BindingTrace trace) {
OverloadResolutionResults<FunctionDescriptor> results = resolveType(scope, entryElement, descriptor); OverloadResolutionResults<FunctionDescriptor> results = resolveType(scope, entryElement, descriptor, trace);
resolveArguments(results, descriptor); resolveArguments(results, descriptor, trace);
} }
@NotNull @NotNull
private OverloadResolutionResults<FunctionDescriptor> resolveType(@NotNull JetScope scope, private OverloadResolutionResults<FunctionDescriptor> resolveType(@NotNull JetScope scope,
@NotNull JetAnnotationEntry entryElement, @NotNull JetAnnotationEntry entryElement,
@NotNull AnnotationDescriptor descriptor) { @NotNull AnnotationDescriptor descriptor, BindingTrace trace) {
OverloadResolutionResults<FunctionDescriptor> results = callResolver.resolveCall(trace, scope, CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, entryElement), NO_EXPECTED_TYPE); OverloadResolutionResults<FunctionDescriptor> results = callResolver.resolveCall(trace, scope, CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, entryElement), NO_EXPECTED_TYPE);
JetType annotationType = results.getResultingDescriptor().getReturnType(); JetType annotationType = results.getResultingDescriptor().getReturnType();
if (results.isSuccess()) { if (results.isSuccess()) {
@@ -98,7 +102,7 @@ public class AnnotationResolver {
} }
private void resolveArguments(@NotNull OverloadResolutionResults<FunctionDescriptor> results, private void resolveArguments(@NotNull OverloadResolutionResults<FunctionDescriptor> results,
@NotNull AnnotationDescriptor descriptor) { @NotNull AnnotationDescriptor descriptor, BindingTrace trace) {
List<CompileTimeConstant<?>> arguments = Lists.newArrayList(); List<CompileTimeConstant<?>> arguments = Lists.newArrayList();
for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> descriptorToArgument : for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> descriptorToArgument :
results.getResultingCall().getValueArguments().entrySet()) { results.getResultingCall().getValueArguments().entrySet()) {
@@ -106,19 +110,18 @@ public class AnnotationResolver {
List<JetExpression> argumentExpressions = descriptorToArgument.getValue().getArgumentExpressions(); List<JetExpression> argumentExpressions = descriptorToArgument.getValue().getArgumentExpressions();
ValueParameterDescriptor parameterDescriptor = descriptorToArgument.getKey(); ValueParameterDescriptor parameterDescriptor = descriptorToArgument.getKey();
for (JetExpression argument : argumentExpressions) { for (JetExpression argument : argumentExpressions) {
arguments.add(resolveAnnotationArgument(argument, parameterDescriptor.getType())); arguments.add(resolveAnnotationArgument(argument, parameterDescriptor.getType(), trace));
} }
} }
descriptor.setValueArguments(arguments); descriptor.setValueArguments(arguments);
} }
@Nullable @Nullable
public CompileTimeConstant<?> resolveAnnotationArgument(@NotNull JetExpression expression, @NotNull final JetType expectedType) { public CompileTimeConstant<?> resolveAnnotationArgument(@NotNull JetExpression expression, @NotNull final JetType expectedType, final BindingTrace trace) {
JetVisitor<CompileTimeConstant<?>, Void> visitor = new JetVisitor<CompileTimeConstant<?>, Void>() { JetVisitor<CompileTimeConstant<?>, Void> visitor = new JetVisitor<CompileTimeConstant<?>, Void>() {
@Override @Override
public CompileTimeConstant<?> visitConstantExpression(JetConstantExpression expression, Void nothing) { public CompileTimeConstant<?> visitConstantExpression(JetConstantExpression expression, Void nothing) {
ExpressionTypingServices typeInferrerServices = semanticServices.getTypeInferrerServices(trace); JetType type = expressionTypingServices.getType(JetScope.EMPTY, expression, expectedType, DataFlowInfo.EMPTY, trace);
JetType type = typeInferrerServices.getType(JetScope.EMPTY, expression, expectedType, DataFlowInfo.EMPTY);
if (type == null) { if (type == null) {
// TODO: // TODO:
// trace.report(ANNOTATION_PARAMETER_SHOULD_BE_CONSTANT.on(expression)); // trace.report(ANNOTATION_PARAMETER_SHOULD_BE_CONSTANT.on(expression));
@@ -161,15 +164,15 @@ public class AnnotationResolver {
} }
@NotNull @NotNull
public List<AnnotationDescriptor> createAnnotationStubs(@Nullable JetModifierList modifierList) { public List<AnnotationDescriptor> createAnnotationStubs(@Nullable JetModifierList modifierList, BindingTrace trace) {
if (modifierList == null) { if (modifierList == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
return createAnnotationStubs(modifierList.getAnnotationEntries()); return createAnnotationStubs(modifierList.getAnnotationEntries(), trace);
} }
@NotNull @NotNull
public List<AnnotationDescriptor> createAnnotationStubs(List<JetAnnotationEntry> annotations) { public List<AnnotationDescriptor> createAnnotationStubs(List<JetAnnotationEntry> annotations, BindingTrace trace) {
List<AnnotationDescriptor> result = Lists.newArrayList(); List<AnnotationDescriptor> result = Lists.newArrayList();
for (JetAnnotationEntry annotation : annotations) { for (JetAnnotationEntry annotation : annotations) {
AnnotationDescriptor annotationDescriptor = new AnnotationDescriptor(); AnnotationDescriptor annotationDescriptor = new AnnotationDescriptor();
@@ -21,6 +21,7 @@ import com.google.common.collect.Sets;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.util.containers.Queue; import com.intellij.util.containers.Queue;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.calls.CallMaker; import org.jetbrains.jet.lang.resolve.calls.CallMaker;
@@ -39,6 +40,7 @@ import org.jetbrains.jet.util.Box;
import org.jetbrains.jet.util.lazy.ReenteringLazyValueComputationException; import org.jetbrains.jet.util.lazy.ReenteringLazyValueComputationException;
import org.jetbrains.jet.util.slicedmap.WritableSlice; import org.jetbrains.jet.util.slicedmap.WritableSlice;
import javax.inject.Inject;
import java.util.*; import java.util.*;
import static org.jetbrains.jet.lang.diagnostics.Errors.*; import static org.jetbrains.jet.lang.diagnostics.Errors.*;
@@ -49,13 +51,25 @@ import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE;
* @author abreslav * @author abreslav
*/ */
public class BodyResolver { public class BodyResolver {
@NotNull
private final TopDownAnalysisContext context; private final TopDownAnalysisContext context;
@NotNull
private final JetSemanticServices semanticServices;
@NotNull
private final DescriptorResolver descriptorResolver;
@NotNull
private final ExpressionTypingServices expressionTypingServices;
@NotNull
private final CallResolver.Context callResolverContext;
private final ObservableBindingTrace trace; @Inject
public BodyResolver(@NotNull TopDownAnalysisContext context,
public BodyResolver(TopDownAnalysisContext context) { @NotNull JetSemanticServices semanticServices, @NotNull DescriptorResolver descriptorResolver, @NotNull ExpressionTypingServices expressionTypingServices, CallResolver.Context callResolverContext) {
this.context = context; this.context = context;
this.trace = context.getTrace(); this.semanticServices = semanticServices;
this.descriptorResolver = descriptorResolver;
this.expressionTypingServices = expressionTypingServices;
this.callResolverContext = callResolverContext;
} }
public void resolveBehaviorDeclarationBodies() { public void resolveBehaviorDeclarationBodies() {
@@ -90,8 +104,8 @@ public class BodyResolver {
final ConstructorDescriptor primaryConstructor = descriptor.getUnsubstitutedPrimaryConstructor(); final ConstructorDescriptor primaryConstructor = descriptor.getUnsubstitutedPrimaryConstructor();
final JetScope scopeForConstructor = primaryConstructor == null final JetScope scopeForConstructor = primaryConstructor == null
? null ? null
: FunctionDescriptorUtil.getFunctionInnerScope(descriptor.getScopeForSupertypeResolution(), primaryConstructor, trace); : FunctionDescriptorUtil.getFunctionInnerScope(descriptor.getScopeForSupertypeResolution(), primaryConstructor, context.getTrace());
final ExpressionTypingServices typeInferrer = context.getSemanticServices().getTypeInferrerServices(trace); // TODO : flow final ExpressionTypingServices typeInferrer = expressionTypingServices; // TODO : flow
final Map<JetTypeReference, JetType> supertypes = Maps.newLinkedHashMap(); final Map<JetTypeReference, JetType> supertypes = Maps.newLinkedHashMap();
JetVisitorVoid visitor = new JetVisitorVoid() { JetVisitorVoid visitor = new JetVisitorVoid() {
@@ -121,8 +135,8 @@ public class BodyResolver {
JetScope scope = scopeForConstructor == null JetScope scope = scopeForConstructor == null
? descriptor.getScopeForMemberResolution() ? descriptor.getScopeForMemberResolution()
: scopeForConstructor; : scopeForConstructor;
JetType type = typeInferrer.getType(scope, delegateExpression, NO_EXPECTED_TYPE); JetType type = typeInferrer.getType(scope, delegateExpression, NO_EXPECTED_TYPE, context.getTrace());
if (type != null && supertype != null && !context.getSemanticServices().getTypeChecker().isSubtypeOf(type, supertype)) { if (type != null && supertype != null && !semanticServices.getTypeChecker().isSubtypeOf(type, supertype)) {
context.getTrace().report(TYPE_MISMATCH.on(delegateExpression, supertype, type)); context.getTrace().report(TYPE_MISMATCH.on(delegateExpression, supertype, type));
} }
} }
@@ -141,7 +155,7 @@ public class BodyResolver {
assert descriptor.getKind() == ClassKind.TRAIT; assert descriptor.getKind() == ClassKind.TRAIT;
return; return;
} }
OverloadResolutionResults<FunctionDescriptor> results = new CallResolver(context.getSemanticServices(), DataFlowInfo.EMPTY).resolveCall( OverloadResolutionResults<FunctionDescriptor> results = new CallResolver(callResolverContext, DataFlowInfo.EMPTY).resolveCall(
context.getTrace(), scopeForConstructor, context.getTrace(), scopeForConstructor,
CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, call), NO_EXPECTED_TYPE); CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, call), NO_EXPECTED_TYPE);
if (results.isSuccess()) { if (results.isSuccess()) {
@@ -261,9 +275,8 @@ public class BodyResolver {
ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor(); ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
assert primaryConstructor != null; assert primaryConstructor != null;
final JetScope scopeForInitializers = classDescriptor.getScopeForInitializers(); final JetScope scopeForInitializers = classDescriptor.getScopeForInitializers();
ExpressionTypingServices typeInferrer = context.getSemanticServices().getTypeInferrerServices(trace);
for (JetClassInitializer anonymousInitializer : anonymousInitializers) { for (JetClassInitializer anonymousInitializer : anonymousInitializers) {
typeInferrer.getType(scopeForInitializers, anonymousInitializer.getBody(), NO_EXPECTED_TYPE); expressionTypingServices.getType(scopeForInitializers, anonymousInitializer.getBody(), NO_EXPECTED_TYPE, context.getTrace());
} }
} }
else { else {
@@ -298,12 +311,12 @@ public class BodyResolver {
private void resolveSecondaryConstructorBody(JetSecondaryConstructor declaration, final ConstructorDescriptor descriptor) { private void resolveSecondaryConstructorBody(JetSecondaryConstructor declaration, final ConstructorDescriptor descriptor) {
if (!context.completeAnalysisNeeded(declaration)) return; if (!context.completeAnalysisNeeded(declaration)) return;
MutableClassDescriptor classDescriptor = (MutableClassDescriptor) descriptor.getContainingDeclaration(); MutableClassDescriptor classDescriptor = (MutableClassDescriptor) descriptor.getContainingDeclaration();
final JetScope scopeForSupertypeInitializers = FunctionDescriptorUtil.getFunctionInnerScope(classDescriptor.getScopeForSupertypeResolution(), descriptor, trace); final JetScope scopeForSupertypeInitializers = FunctionDescriptorUtil.getFunctionInnerScope(classDescriptor.getScopeForSupertypeResolution(), descriptor, context.getTrace());
//contains only constructor parameters //contains only constructor parameters
final JetScope scopeForConstructorBody = FunctionDescriptorUtil.getFunctionInnerScope(classDescriptor.getScopeForInitializers(), descriptor, trace); final JetScope scopeForConstructorBody = FunctionDescriptorUtil.getFunctionInnerScope(classDescriptor.getScopeForInitializers(), descriptor, context.getTrace());
//contains members & backing fields //contains members & backing fields
final CallResolver callResolver = new CallResolver(context.getSemanticServices(), DataFlowInfo.EMPTY); // TODO: dataFlowInfo final CallResolver callResolver = new CallResolver(callResolverContext, DataFlowInfo.EMPTY); // TODO: dataFlowInfo
PsiElement nameElement = declaration.getNameNode().getPsi(); PsiElement nameElement = declaration.getNameNode().getPsi();
if (classDescriptor.getUnsubstitutedPrimaryConstructor() == null) { if (classDescriptor.getUnsubstitutedPrimaryConstructor() == null) {
@@ -363,9 +376,8 @@ public class BodyResolver {
} }
JetExpression bodyExpression = declaration.getBodyExpression(); JetExpression bodyExpression = declaration.getBodyExpression();
if (bodyExpression != null) { if (bodyExpression != null) {
ExpressionTypingServices typeInferrer = context.getSemanticServices().getTypeInferrerServices(trace);
typeInferrer.checkFunctionReturnType(scopeForConstructorBody, declaration, descriptor, JetStandardClasses.getUnitType()); expressionTypingServices.checkFunctionReturnType(scopeForConstructorBody, declaration, descriptor, JetStandardClasses.getUnitType(), context.getTrace());
} }
checkDefaultParameterValues(declaration.getValueParameters(), descriptor.getValueParameters(), scopeForConstructorBody); checkDefaultParameterValues(declaration.getValueParameters(), descriptor.getValueParameters(), scopeForConstructorBody);
@@ -424,8 +436,8 @@ public class BodyResolver {
private JetScope makeScopeForPropertyAccessor(@NotNull JetPropertyAccessor accessor, PropertyDescriptor propertyDescriptor) { private JetScope makeScopeForPropertyAccessor(@NotNull JetPropertyAccessor accessor, PropertyDescriptor propertyDescriptor) {
JetScope declaringScope = context.getDeclaringScopes().get(accessor); JetScope declaringScope = context.getDeclaringScopes().get(accessor);
JetScope propertyDeclarationInnerScope = context.getDescriptorResolver().getPropertyDeclarationInnerScope( JetScope propertyDeclarationInnerScope = descriptorResolver.getPropertyDeclarationInnerScope(
declaringScope, propertyDescriptor, propertyDescriptor.getTypeParameters(), propertyDescriptor.getReceiverParameter()); declaringScope, propertyDescriptor, propertyDescriptor.getTypeParameters(), propertyDescriptor.getReceiverParameter(), context.getTrace());
WritableScope accessorScope = new WritableScopeImpl(propertyDeclarationInnerScope, declaringScope.getContainingDeclaration(), new TraceBasedRedeclarationHandler(context.getTrace())).setDebugName("Accessor scope"); WritableScope accessorScope = new WritableScopeImpl(propertyDeclarationInnerScope, declaringScope.getContainingDeclaration(), new TraceBasedRedeclarationHandler(context.getTrace())).setDebugName("Accessor scope");
accessorScope.changeLockLevel(WritableScope.LockLevel.READING); accessorScope.changeLockLevel(WritableScope.LockLevel.READING);
@@ -451,7 +463,7 @@ public class BodyResolver {
} }
private ObservableBindingTrace createFieldTrackingTrace(final PropertyDescriptor propertyDescriptor) { private ObservableBindingTrace createFieldTrackingTrace(final PropertyDescriptor propertyDescriptor) {
return new ObservableBindingTrace(trace).addHandler(BindingContext.REFERENCE_TARGET, new ObservableBindingTrace.RecordHandler<JetReferenceExpression, DeclarationDescriptor>() { return new ObservableBindingTrace(context.getTrace()).addHandler(BindingContext.REFERENCE_TARGET, new ObservableBindingTrace.RecordHandler<JetReferenceExpression, DeclarationDescriptor>() {
@Override @Override
public void handleRecord(WritableSlice<JetReferenceExpression, DeclarationDescriptor> slice, JetReferenceExpression expression, DeclarationDescriptor descriptor) { public void handleRecord(WritableSlice<JetReferenceExpression, DeclarationDescriptor> slice, JetReferenceExpression expression, DeclarationDescriptor descriptor) {
if (expression instanceof JetSimpleNameExpression) { if (expression instanceof JetSimpleNameExpression) {
@@ -459,7 +471,7 @@ public class BodyResolver {
if (simpleNameExpression.getReferencedNameElementType() == JetTokens.FIELD_IDENTIFIER) { if (simpleNameExpression.getReferencedNameElementType() == JetTokens.FIELD_IDENTIFIER) {
// This check may be considered redundant as long as $x is only accessible from accessors to $x // This check may be considered redundant as long as $x is only accessible from accessors to $x
if (descriptor == propertyDescriptor) { // TODO : original? if (descriptor == propertyDescriptor) { // TODO : original?
trace.record(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor); // TODO: this context.getTrace()? context.getTrace().record(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor); // TODO: this context.getTrace()?
} }
} }
} }
@@ -469,9 +481,8 @@ public class BodyResolver {
private void resolvePropertyInitializer(JetProperty property, PropertyDescriptor propertyDescriptor, JetExpression initializer, JetScope scope) { private void resolvePropertyInitializer(JetProperty property, PropertyDescriptor propertyDescriptor, JetExpression initializer, JetScope scope) {
//JetFlowInformationProvider flowInformationProvider = context.getDescriptorResolver().computeFlowData(property, initializer); // TODO : flow JET-15 //JetFlowInformationProvider flowInformationProvider = context.getDescriptorResolver().computeFlowData(property, initializer); // TODO : flow JET-15
ExpressionTypingServices typeInferrer = context.getSemanticServices().getTypeInferrerServices(trace);
JetType expectedTypeForInitializer = property.getPropertyTypeRef() != null ? propertyDescriptor.getType() : NO_EXPECTED_TYPE; JetType expectedTypeForInitializer = property.getPropertyTypeRef() != null ? propertyDescriptor.getType() : NO_EXPECTED_TYPE;
JetType type = typeInferrer.getType(context.getDescriptorResolver().getPropertyDeclarationInnerScope(scope, propertyDescriptor, propertyDescriptor.getTypeParameters(), propertyDescriptor.getReceiverParameter()), initializer, expectedTypeForInitializer); JetType type = expressionTypingServices.getType(descriptorResolver.getPropertyDeclarationInnerScope(scope, propertyDescriptor, propertyDescriptor.getTypeParameters(), propertyDescriptor.getReceiverParameter(), context.getTrace()), initializer, expectedTypeForInitializer, context.getTrace());
// //
// JetType expectedType = propertyDescriptor.getInType(); // JetType expectedType = propertyDescriptor.getInType();
// if (expectedType == null) { // if (expectedType == null) {
@@ -493,7 +504,7 @@ public class BodyResolver {
JetScope declaringScope = this.context.getDeclaringScopes().get(declaration); JetScope declaringScope = this.context.getDeclaringScopes().get(declaration);
assert declaringScope != null; assert declaringScope != null;
resolveFunctionBody(trace, declaration, descriptor, declaringScope); resolveFunctionBody(context.getTrace(), declaration, descriptor, declaringScope);
assert descriptor.getReturnType() != null; assert descriptor.getReturnType() != null;
} }
@@ -509,9 +520,7 @@ public class BodyResolver {
JetExpression bodyExpression = function.getBodyExpression(); JetExpression bodyExpression = function.getBodyExpression();
JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(declaringScope, functionDescriptor, trace); JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(declaringScope, functionDescriptor, trace);
if (bodyExpression != null) { if (bodyExpression != null) {
ExpressionTypingServices typeInferrer = context.getSemanticServices().getTypeInferrerServices(trace); expressionTypingServices.checkFunctionReturnType(functionInnerScope, function, functionDescriptor, trace);
typeInferrer.checkFunctionReturnType(functionInnerScope, function, functionDescriptor);
} }
List<JetParameter> valueParameters = function.getValueParameters(); List<JetParameter> valueParameters = function.getValueParameters();
@@ -523,14 +532,13 @@ public class BodyResolver {
} }
private void checkDefaultParameterValues(List<JetParameter> valueParameters, List<ValueParameterDescriptor> valueParameterDescriptors, JetScope declaringScope) { private void checkDefaultParameterValues(List<JetParameter> valueParameters, List<ValueParameterDescriptor> valueParameterDescriptors, JetScope declaringScope) {
ExpressionTypingServices typeInferrer = context.getSemanticServices().getTypeInferrerServices(trace);
for (int i = 0; i < valueParameters.size(); i++) { for (int i = 0; i < valueParameters.size(); i++) {
ValueParameterDescriptor valueParameterDescriptor = valueParameterDescriptors.get(i); ValueParameterDescriptor valueParameterDescriptor = valueParameterDescriptors.get(i);
if (valueParameterDescriptor.hasDefaultValue()) { if (valueParameterDescriptor.hasDefaultValue()) {
JetParameter jetParameter = valueParameters.get(i); JetParameter jetParameter = valueParameters.get(i);
JetExpression defaultValue = jetParameter.getDefaultValue(); JetExpression defaultValue = jetParameter.getDefaultValue();
if (defaultValue != null) { if (defaultValue != null) {
typeInferrer.getType(declaringScope, defaultValue, valueParameterDescriptor.getType()); expressionTypingServices.getType(declaringScope, defaultValue, valueParameterDescriptor.getType(), context.getTrace());
} }
} }
} }
@@ -22,6 +22,7 @@ import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope; import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import javax.inject.Inject;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -34,9 +35,10 @@ public class DeclarationResolver {
private final AnnotationResolver annotationResolver; private final AnnotationResolver annotationResolver;
private final TopDownAnalysisContext context; private final TopDownAnalysisContext context;
public DeclarationResolver(TopDownAnalysisContext context) { @Inject
public DeclarationResolver(AnnotationResolver annotationResolver, TopDownAnalysisContext context) {
this.annotationResolver = annotationResolver;
this.context = context; this.context = context;
this.annotationResolver = new AnnotationResolver(context.getSemanticServices(), context.getTrace());
} }
public void process() { public void process() {
@@ -59,7 +61,6 @@ public class DeclarationResolver {
} }
private void resolveAnnotationStubsOnClassesAndConstructors() { private void resolveAnnotationStubsOnClassesAndConstructors() {
AnnotationResolver annotationResolver = new AnnotationResolver(context.getSemanticServices(), context.getTrace());
for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) { for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
JetClass jetClass = entry.getKey(); JetClass jetClass = entry.getKey();
MutableClassDescriptor descriptor = entry.getValue(); MutableClassDescriptor descriptor = entry.getValue();
@@ -75,7 +76,7 @@ public class DeclarationResolver {
private void resolveAnnotationsForClassOrObject(AnnotationResolver annotationResolver, JetClassOrObject jetClass, MutableClassDescriptor descriptor) { private void resolveAnnotationsForClassOrObject(AnnotationResolver annotationResolver, JetClassOrObject jetClass, MutableClassDescriptor descriptor) {
JetModifierList modifierList = jetClass.getModifierList(); JetModifierList modifierList = jetClass.getModifierList();
if (modifierList != null) { if (modifierList != null) {
descriptor.getAnnotations().addAll(annotationResolver.resolveAnnotations(descriptor.getScopeForSupertypeResolution(), modifierList.getAnnotationEntries())); descriptor.getAnnotations().addAll(annotationResolver.resolveAnnotations(descriptor.getScopeForSupertypeResolution(), modifierList.getAnnotationEntries(), context.getTrace()));
} }
} }
@@ -120,7 +121,7 @@ public class DeclarationResolver {
declaration.accept(new JetVisitorVoid() { declaration.accept(new JetVisitorVoid() {
@Override @Override
public void visitNamedFunction(JetNamedFunction function) { public void visitNamedFunction(JetNamedFunction function) {
SimpleFunctionDescriptor functionDescriptor = context.getDescriptorResolver().resolveFunctionDescriptor(namespaceLike, scopeForFunctions, function); SimpleFunctionDescriptor functionDescriptor = context.getDescriptorResolver().resolveFunctionDescriptor(namespaceLike, scopeForFunctions, function, context.getTrace());
namespaceLike.addFunctionDescriptor(functionDescriptor); namespaceLike.addFunctionDescriptor(functionDescriptor);
context.getFunctions().put(function, functionDescriptor); context.getFunctions().put(function, functionDescriptor);
context.getDeclaringScopes().put(function, scopeForFunctions); context.getDeclaringScopes().put(function, scopeForFunctions);
@@ -128,7 +129,7 @@ public class DeclarationResolver {
@Override @Override
public void visitProperty(JetProperty property) { public void visitProperty(JetProperty property) {
PropertyDescriptor propertyDescriptor = context.getDescriptorResolver().resolvePropertyDescriptor(namespaceLike, scopeForPropertyInitializers, property); PropertyDescriptor propertyDescriptor = context.getDescriptorResolver().resolvePropertyDescriptor(namespaceLike, scopeForPropertyInitializers, property, context.getTrace());
namespaceLike.addPropertyDescriptor(propertyDescriptor); namespaceLike.addPropertyDescriptor(propertyDescriptor);
context.getProperties().put(property, propertyDescriptor); context.getProperties().put(property, propertyDescriptor);
context.getDeclaringScopes().put(property, scopeForPropertyInitializers); context.getDeclaringScopes().put(property, scopeForPropertyInitializers);
@@ -142,7 +143,7 @@ public class DeclarationResolver {
@Override @Override
public void visitObjectDeclaration(JetObjectDeclaration declaration) { public void visitObjectDeclaration(JetObjectDeclaration declaration) {
PropertyDescriptor propertyDescriptor = context.getDescriptorResolver().resolveObjectDeclarationAsPropertyDescriptor(namespaceLike, declaration, context.getObjects().get(declaration)); PropertyDescriptor propertyDescriptor = context.getDescriptorResolver().resolveObjectDeclarationAsPropertyDescriptor(namespaceLike, declaration, context.getObjects().get(declaration), context.getTrace());
namespaceLike.addPropertyDescriptor(propertyDescriptor); namespaceLike.addPropertyDescriptor(propertyDescriptor);
} }
@@ -151,7 +152,7 @@ public class DeclarationResolver {
if (enumEntry.getPrimaryConstructorParameterList() == null) { if (enumEntry.getPrimaryConstructorParameterList() == null) {
MutableClassDescriptorLite classObjectDescriptor = ((MutableClassDescriptor) namespaceLike).getClassObjectDescriptor(); MutableClassDescriptorLite classObjectDescriptor = ((MutableClassDescriptor) namespaceLike).getClassObjectDescriptor();
assert classObjectDescriptor != null; assert classObjectDescriptor != null;
PropertyDescriptor propertyDescriptor = context.getDescriptorResolver().resolveObjectDeclarationAsPropertyDescriptor(classObjectDescriptor, enumEntry, context.getClasses().get(enumEntry)); PropertyDescriptor propertyDescriptor = context.getDescriptorResolver().resolveObjectDeclarationAsPropertyDescriptor(classObjectDescriptor, enumEntry, context.getClasses().get(enumEntry), context.getTrace());
classObjectDescriptor.addPropertyDescriptor(propertyDescriptor); classObjectDescriptor.addPropertyDescriptor(propertyDescriptor);
} }
} }
@@ -170,13 +171,13 @@ public class DeclarationResolver {
// TODO : not all the parameters are real properties // TODO : not all the parameters are real properties
JetScope memberScope = classDescriptor.getScopeForSupertypeResolution(); JetScope memberScope = classDescriptor.getScopeForSupertypeResolution();
ConstructorDescriptor constructorDescriptor = context.getDescriptorResolver().resolvePrimaryConstructorDescriptor(memberScope, classDescriptor, klass); ConstructorDescriptor constructorDescriptor = context.getDescriptorResolver().resolvePrimaryConstructorDescriptor(memberScope, classDescriptor, klass, context.getTrace());
for (JetParameter parameter : klass.getPrimaryConstructorParameters()) { for (JetParameter parameter : klass.getPrimaryConstructorParameters()) {
if (parameter.getValOrVarNode() != null) { if (parameter.getValOrVarNode() != null) {
PropertyDescriptor propertyDescriptor = context.getDescriptorResolver().resolvePrimaryConstructorParameterToAProperty( PropertyDescriptor propertyDescriptor = context.getDescriptorResolver().resolvePrimaryConstructorParameterToAProperty(
classDescriptor, classDescriptor,
memberScope, memberScope,
parameter parameter, context.getTrace()
); );
classDescriptor.addPropertyDescriptor(propertyDescriptor); classDescriptor.addPropertyDescriptor(propertyDescriptor);
context.getPrimaryConstructorParameterProperties().add(propertyDescriptor); context.getPrimaryConstructorParameterProperties().add(propertyDescriptor);
@@ -194,7 +195,7 @@ public class DeclarationResolver {
ConstructorDescriptor constructorDescriptor = context.getDescriptorResolver().resolveSecondaryConstructorDescriptor( ConstructorDescriptor constructorDescriptor = context.getDescriptorResolver().resolveSecondaryConstructorDescriptor(
classDescriptor.getScopeForMemberResolution(), classDescriptor.getScopeForMemberResolution(),
classDescriptor, classDescriptor,
constructor); constructor, context.getTrace());
classDescriptor.addConstructor(constructorDescriptor, context.getTrace()); classDescriptor.addConstructor(constructorDescriptor, context.getTrace());
context.getConstructors().put(constructor, constructorDescriptor); context.getConstructors().put(constructor, constructorDescriptor);
context.getDeclaringScopes().put(constructor, classDescriptor.getScopeForMemberLookup()); context.getDeclaringScopes().put(constructor, classDescriptor.getScopeForMemberLookup());
@@ -34,12 +34,14 @@ import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ExtensionReceiver; import org.jetbrains.jet.lang.resolve.scopes.receivers.ExtensionReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses; import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.util.lazy.LazyValue; import org.jetbrains.jet.util.lazy.LazyValue;
import org.jetbrains.jet.util.lazy.LazyValueWithDefault; import org.jetbrains.jet.util.lazy.LazyValueWithDefault;
import javax.inject.Inject;
import java.util.*; import java.util.*;
import static org.jetbrains.jet.lang.diagnostics.Errors.*; import static org.jetbrains.jet.lang.diagnostics.Errors.*;
@@ -48,28 +50,39 @@ import static org.jetbrains.jet.lang.diagnostics.Errors.*;
* @author abreslav * @author abreslav
*/ */
public class DescriptorResolver { public class DescriptorResolver {
private final JetSemanticServices semanticServices; private JetSemanticServices semanticServices;
private final TypeResolver typeResolver; private TypeResolver typeResolver;
private final TypeResolver typeResolverNotCheckingBounds; private AnnotationResolver annotationResolver;
private final BindingTrace trace; private ExpressionTypingServices expressionTypingServices;
private final AnnotationResolver annotationResolver;
public DescriptorResolver(JetSemanticServices semanticServices, BindingTrace trace) { @Inject
public void setSemanticServices(JetSemanticServices semanticServices) {
this.semanticServices = semanticServices; this.semanticServices = semanticServices;
this.typeResolver = new TypeResolver(semanticServices, trace, true);
this.typeResolverNotCheckingBounds = new TypeResolver(semanticServices, trace, false);
this.trace = trace;
this.annotationResolver = new AnnotationResolver(semanticServices, trace);
} }
public void resolveMutableClassDescriptor(@NotNull JetClass classElement, @NotNull MutableClassDescriptor descriptor) { @Inject
public void setTypeResolver(TypeResolver typeResolver) {
this.typeResolver = typeResolver;
}
@Inject
public void setAnnotationResolver(AnnotationResolver annotationResolver) {
this.annotationResolver = annotationResolver;
}
@Inject
public void setExpressionTypingServices(ExpressionTypingServices expressionTypingServices) {
this.expressionTypingServices = expressionTypingServices;
}
public void resolveMutableClassDescriptor(@NotNull JetClass classElement, @NotNull MutableClassDescriptor descriptor, BindingTrace trace) {
// TODO : Where-clause // TODO : Where-clause
List<TypeParameterDescriptor> typeParameters = Lists.newArrayList(); List<TypeParameterDescriptor> typeParameters = Lists.newArrayList();
int index = 0; int index = 0;
for (JetTypeParameter typeParameter : classElement.getTypeParameters()) { for (JetTypeParameter typeParameter : classElement.getTypeParameters()) {
TypeParameterDescriptor typeParameterDescriptor = TypeParameterDescriptor.createForFurtherModification( TypeParameterDescriptor typeParameterDescriptor = TypeParameterDescriptor.createForFurtherModification(
descriptor, descriptor,
annotationResolver.createAnnotationStubs(typeParameter.getModifierList()), annotationResolver.createAnnotationStubs(typeParameter.getModifierList(), trace),
!typeParameter.hasModifier(JetTokens.ERASED_KEYWORD), !typeParameter.hasModifier(JetTokens.ERASED_KEYWORD),
typeParameter.getVariance(), typeParameter.getVariance(),
JetPsiUtil.safeName(typeParameter.getName()), JetPsiUtil.safeName(typeParameter.getName()),
@@ -87,16 +100,16 @@ public class DescriptorResolver {
trace.record(BindingContext.CLASS, classElement, descriptor); trace.record(BindingContext.CLASS, classElement, descriptor);
} }
public void resolveSupertypes(@NotNull JetClassOrObject jetClass, @NotNull MutableClassDescriptor descriptor) { public void resolveSupertypes(@NotNull JetClassOrObject jetClass, @NotNull MutableClassDescriptor descriptor, BindingTrace trace) {
List<JetDelegationSpecifier> delegationSpecifiers = jetClass.getDelegationSpecifiers(); List<JetDelegationSpecifier> delegationSpecifiers = jetClass.getDelegationSpecifiers();
if (delegationSpecifiers.isEmpty()) { if (delegationSpecifiers.isEmpty()) {
descriptor.addSupertype(getDefaultSupertype(jetClass)); descriptor.addSupertype(getDefaultSupertype(jetClass, trace));
} }
else { else {
Collection<JetType> supertypes = resolveDelegationSpecifiers( Collection<JetType> supertypes = resolveDelegationSpecifiers(
descriptor.getScopeForSupertypeResolution(), descriptor.getScopeForSupertypeResolution(),
delegationSpecifiers, delegationSpecifiers,
typeResolverNotCheckingBounds); typeResolver, trace, false);
for (JetType supertype : supertypes) { for (JetType supertype : supertypes) {
descriptor.addSupertype(supertype); descriptor.addSupertype(supertype);
} }
@@ -104,7 +117,7 @@ public class DescriptorResolver {
} }
private JetType getDefaultSupertype(JetClassOrObject jetClass) { private JetType getDefaultSupertype(JetClassOrObject jetClass, BindingTrace trace) {
// TODO : beautify // TODO : beautify
if (jetClass instanceof JetEnumEntry) { if (jetClass instanceof JetEnumEntry) {
JetClassOrObject parent = PsiTreeUtil.getParentOfType(jetClass, JetClassOrObject.class); JetClassOrObject parent = PsiTreeUtil.getParentOfType(jetClass, JetClassOrObject.class);
@@ -120,7 +133,7 @@ public class DescriptorResolver {
return JetStandardClasses.getAnyType(); return JetStandardClasses.getAnyType();
} }
public Collection<JetType> resolveDelegationSpecifiers(JetScope extensibleScope, List<JetDelegationSpecifier> delegationSpecifiers, @NotNull TypeResolver resolver) { public Collection<JetType> resolveDelegationSpecifiers(JetScope extensibleScope, List<JetDelegationSpecifier> delegationSpecifiers, @NotNull TypeResolver resolver, BindingTrace trace, boolean checkBounds) {
if (delegationSpecifiers.isEmpty()) { if (delegationSpecifiers.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} }
@@ -128,7 +141,7 @@ public class DescriptorResolver {
for (JetDelegationSpecifier delegationSpecifier : delegationSpecifiers) { for (JetDelegationSpecifier delegationSpecifier : delegationSpecifiers) {
JetTypeReference typeReference = delegationSpecifier.getTypeReference(); JetTypeReference typeReference = delegationSpecifier.getTypeReference();
if (typeReference != null) { if (typeReference != null) {
result.add(resolver.resolveType(extensibleScope, typeReference)); result.add(resolver.resolveType(extensibleScope, typeReference, trace, checkBounds));
JetTypeElement typeElement = typeReference.getTypeElement(); JetTypeElement typeElement = typeReference.getTypeElement();
while (typeElement instanceof JetNullableType) { while (typeElement instanceof JetNullableType) {
JetNullableType nullableType = (JetNullableType) typeElement; JetNullableType nullableType = (JetNullableType) typeElement;
@@ -153,19 +166,19 @@ public class DescriptorResolver {
} }
@NotNull @NotNull
public SimpleFunctionDescriptor resolveFunctionDescriptor(DeclarationDescriptor containingDescriptor, final JetScope scope, final JetNamedFunction function) { public SimpleFunctionDescriptor resolveFunctionDescriptor(DeclarationDescriptor containingDescriptor, final JetScope scope, final JetNamedFunction function, final BindingTrace trace) {
final SimpleFunctionDescriptorImpl functionDescriptor = new SimpleFunctionDescriptorImpl( final SimpleFunctionDescriptorImpl functionDescriptor = new SimpleFunctionDescriptorImpl(
containingDescriptor, containingDescriptor,
annotationResolver.resolveAnnotations(scope, function.getModifierList()), annotationResolver.resolveAnnotations(scope, function.getModifierList(), trace),
JetPsiUtil.safeName(function.getName()), JetPsiUtil.safeName(function.getName()),
CallableMemberDescriptor.Kind.DECLARATION CallableMemberDescriptor.Kind.DECLARATION
); );
WritableScope innerScope = new WritableScopeImpl(scope, functionDescriptor, new TraceBasedRedeclarationHandler(trace)).setDebugName("Function descriptor header scope"); WritableScope innerScope = new WritableScopeImpl(scope, functionDescriptor, new TraceBasedRedeclarationHandler(trace)).setDebugName("Function descriptor header scope");
innerScope.addLabeledDeclaration(functionDescriptor); innerScope.addLabeledDeclaration(functionDescriptor);
List<TypeParameterDescriptor> typeParameterDescriptors = resolveTypeParameters(functionDescriptor, innerScope, function.getTypeParameters()); List<TypeParameterDescriptor> typeParameterDescriptors = resolveTypeParameters(functionDescriptor, innerScope, function.getTypeParameters(), trace);
innerScope.changeLockLevel(WritableScope.LockLevel.BOTH); innerScope.changeLockLevel(WritableScope.LockLevel.BOTH);
resolveGenericBounds(function, innerScope, typeParameterDescriptors); resolveGenericBounds(function, innerScope, typeParameterDescriptors, trace);
JetType receiverType = null; JetType receiverType = null;
JetTypeReference receiverTypeRef = function.getReceiverTypeRef(); JetTypeReference receiverTypeRef = function.getReceiverTypeRef();
@@ -174,17 +187,17 @@ public class DescriptorResolver {
function.hasTypeParameterListBeforeFunctionName() function.hasTypeParameterListBeforeFunctionName()
? innerScope ? innerScope
: scope; : scope;
receiverType = typeResolver.resolveType(scopeForReceiver, receiverTypeRef); receiverType = typeResolver.resolveType(scopeForReceiver, receiverTypeRef, trace, true);
} }
List<ValueParameterDescriptor> valueParameterDescriptors = resolveValueParameters(functionDescriptor, innerScope, function.getValueParameters()); List<ValueParameterDescriptor> valueParameterDescriptors = resolveValueParameters(functionDescriptor, innerScope, function.getValueParameters(), trace);
innerScope.changeLockLevel(WritableScope.LockLevel.READING); innerScope.changeLockLevel(WritableScope.LockLevel.READING);
JetTypeReference returnTypeRef = function.getReturnTypeRef(); JetTypeReference returnTypeRef = function.getReturnTypeRef();
JetType returnType; JetType returnType;
if (returnTypeRef != null) { if (returnTypeRef != null) {
returnType = typeResolver.resolveType(innerScope, returnTypeRef); returnType = typeResolver.resolveType(innerScope, returnTypeRef, trace, true);
} }
else if (function.hasBlockBody()) { else if (function.hasBlockBody()) {
returnType = JetStandardClasses.getUnitType(); returnType = JetStandardClasses.getUnitType();
@@ -196,7 +209,7 @@ public class DescriptorResolver {
@Override @Override
protected JetType compute() { protected JetType compute() {
//JetFlowInformationProvider flowInformationProvider = computeFlowData(function, bodyExpression); //JetFlowInformationProvider flowInformationProvider = computeFlowData(function, bodyExpression);
return semanticServices.getTypeInferrerServices(trace).inferFunctionReturnType(scope, function, functionDescriptor); return expressionTypingServices.inferFunctionReturnType(scope, function, functionDescriptor, trace);
} }
}); });
} }
@@ -236,7 +249,7 @@ public class DescriptorResolver {
} }
@NotNull @NotNull
private List<ValueParameterDescriptor> resolveValueParameters(FunctionDescriptor functionDescriptor, WritableScope parameterScope, List<JetParameter> valueParameters) { private List<ValueParameterDescriptor> resolveValueParameters(FunctionDescriptor functionDescriptor, WritableScope parameterScope, List<JetParameter> valueParameters, BindingTrace trace) {
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(); List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
for (int i = 0, valueParametersSize = valueParameters.size(); i < valueParametersSize; i++) { for (int i = 0, valueParametersSize = valueParameters.size(); i < valueParametersSize; i++) {
JetParameter valueParameter = valueParameters.get(i); JetParameter valueParameter = valueParameters.get(i);
@@ -247,10 +260,10 @@ public class DescriptorResolver {
trace.report(VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION.on(valueParameter)); trace.report(VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION.on(valueParameter));
type = ErrorUtils.createErrorType("Type annotation was missing"); type = ErrorUtils.createErrorType("Type annotation was missing");
} else { } else {
type = typeResolver.resolveType(parameterScope, typeReference); type = typeResolver.resolveType(parameterScope, typeReference, trace, true);
} }
ValueParameterDescriptor valueParameterDescriptor = resolveValueParameterDescriptor(functionDescriptor, valueParameter, i, type); ValueParameterDescriptor valueParameterDescriptor = resolveValueParameterDescriptor(functionDescriptor, valueParameter, i, type, trace);
parameterScope.addVariableDescriptor(valueParameterDescriptor); parameterScope.addVariableDescriptor(valueParameterDescriptor);
result.add(valueParameterDescriptor); result.add(valueParameterDescriptor);
} }
@@ -258,7 +271,7 @@ public class DescriptorResolver {
} }
@NotNull @NotNull
public MutableValueParameterDescriptor resolveValueParameterDescriptor(DeclarationDescriptor declarationDescriptor, JetParameter valueParameter, int index, JetType type) { public MutableValueParameterDescriptor resolveValueParameterDescriptor(DeclarationDescriptor declarationDescriptor, JetParameter valueParameter, int index, JetType type, BindingTrace trace) {
JetType varargElementType = null; JetType varargElementType = null;
JetType variableType = type; JetType variableType = type;
if (valueParameter.hasModifier(JetTokens.VARARG_KEYWORD)) { if (valueParameter.hasModifier(JetTokens.VARARG_KEYWORD)) {
@@ -268,7 +281,7 @@ public class DescriptorResolver {
MutableValueParameterDescriptor valueParameterDescriptor = new ValueParameterDescriptorImpl( MutableValueParameterDescriptor valueParameterDescriptor = new ValueParameterDescriptorImpl(
declarationDescriptor, declarationDescriptor,
index, index,
annotationResolver.createAnnotationStubs(valueParameter.getModifierList()), annotationResolver.createAnnotationStubs(valueParameter.getModifierList(), trace),
JetPsiUtil.safeName(valueParameter.getName()), JetPsiUtil.safeName(valueParameter.getName()),
valueParameter.isMutable(), valueParameter.isMutable(),
variableType, variableType,
@@ -290,23 +303,23 @@ public class DescriptorResolver {
} }
} }
public List<TypeParameterDescriptor> resolveTypeParameters(DeclarationDescriptor containingDescriptor, WritableScope extensibleScope, List<JetTypeParameter> typeParameters) { public List<TypeParameterDescriptor> resolveTypeParameters(DeclarationDescriptor containingDescriptor, WritableScope extensibleScope, List<JetTypeParameter> typeParameters, BindingTrace trace) {
List<TypeParameterDescriptor> result = new ArrayList<TypeParameterDescriptor>(); List<TypeParameterDescriptor> result = new ArrayList<TypeParameterDescriptor>();
for (int i = 0, typeParametersSize = typeParameters.size(); i < typeParametersSize; i++) { for (int i = 0, typeParametersSize = typeParameters.size(); i < typeParametersSize; i++) {
JetTypeParameter typeParameter = typeParameters.get(i); JetTypeParameter typeParameter = typeParameters.get(i);
result.add(resolveTypeParameter(containingDescriptor, extensibleScope, typeParameter, i)); result.add(resolveTypeParameter(containingDescriptor, extensibleScope, typeParameter, i, trace));
} }
return result; return result;
} }
private TypeParameterDescriptor resolveTypeParameter(DeclarationDescriptor containingDescriptor, WritableScope extensibleScope, JetTypeParameter typeParameter, int index) { private TypeParameterDescriptor resolveTypeParameter(DeclarationDescriptor containingDescriptor, WritableScope extensibleScope, JetTypeParameter typeParameter, int index, BindingTrace trace) {
// JetTypeReference extendsBound = typeParameter.getExtendsBound(); // JetTypeReference extendsBound = typeParameter.getExtendsBound();
// JetType bound = extendsBound == null // JetType bound = extendsBound == null
// ? JetStandardClasses.getDefaultBound() // ? JetStandardClasses.getDefaultBound()
// : typeResolver.resolveType(extensibleScope, extendsBound); // : typeResolver.resolveType(extensibleScope, extendsBound);
TypeParameterDescriptor typeParameterDescriptor = TypeParameterDescriptor.createForFurtherModification( TypeParameterDescriptor typeParameterDescriptor = TypeParameterDescriptor.createForFurtherModification(
containingDescriptor, containingDescriptor,
annotationResolver.createAnnotationStubs(typeParameter.getModifierList()), annotationResolver.createAnnotationStubs(typeParameter.getModifierList(), trace),
!typeParameter.hasModifier(JetTokens.ERASED_KEYWORD), !typeParameter.hasModifier(JetTokens.ERASED_KEYWORD),
typeParameter.getVariance(), typeParameter.getVariance(),
JetPsiUtil.safeName(typeParameter.getName()), JetPsiUtil.safeName(typeParameter.getName()),
@@ -318,7 +331,7 @@ public class DescriptorResolver {
return typeParameterDescriptor; return typeParameterDescriptor;
} }
public void resolveGenericBounds(@NotNull JetTypeParameterListOwner declaration, JetScope scope, List<TypeParameterDescriptor> parameters) { public void resolveGenericBounds(@NotNull JetTypeParameterListOwner declaration, JetScope scope, List<TypeParameterDescriptor> parameters, BindingTrace trace) {
List<JetTypeParameter> typeParameters = declaration.getTypeParameters(); List<JetTypeParameter> typeParameters = declaration.getTypeParameters();
Map<String, TypeParameterDescriptor> parameterByName = Maps.newHashMap(); Map<String, TypeParameterDescriptor> parameterByName = Maps.newHashMap();
for (int i = 0, typeParametersSize = typeParameters.size(); i < typeParametersSize; i++) { for (int i = 0, typeParametersSize = typeParameters.size(); i < typeParametersSize; i++) {
@@ -327,7 +340,7 @@ public class DescriptorResolver {
parameterByName.put(typeParameterDescriptor.getName(), typeParameterDescriptor); parameterByName.put(typeParameterDescriptor.getName(), typeParameterDescriptor);
JetTypeReference extendsBound = jetTypeParameter.getExtendsBound(); JetTypeReference extendsBound = jetTypeParameter.getExtendsBound();
if (extendsBound != null) { if (extendsBound != null) {
typeParameterDescriptor.addUpperBound(resolveAndCheckUpperBoundType(extendsBound, scope, false)); typeParameterDescriptor.addUpperBound(resolveAndCheckUpperBoundType(extendsBound, scope, false, trace));
} }
} }
for (JetTypeConstraint constraint : declaration.getTypeConstaints()) { for (JetTypeConstraint constraint : declaration.getTypeConstaints()) {
@@ -341,7 +354,7 @@ public class DescriptorResolver {
} }
TypeParameterDescriptor typeParameterDescriptor = parameterByName.get(referencedName); TypeParameterDescriptor typeParameterDescriptor = parameterByName.get(referencedName);
JetTypeReference boundTypeReference = constraint.getBoundTypeReference(); JetTypeReference boundTypeReference = constraint.getBoundTypeReference();
JetType bound = boundTypeReference != null ? resolveAndCheckUpperBoundType(boundTypeReference, scope, constraint.isClassObjectContraint()) : null; JetType bound = boundTypeReference != null ? resolveAndCheckUpperBoundType(boundTypeReference, scope, constraint.isClassObjectContraint(), trace) : null;
if (typeParameterDescriptor == null) { if (typeParameterDescriptor == null) {
// To tell the user that we look only for locally defined type parameters // To tell the user that we look only for locally defined type parameters
ClassifierDescriptor classifier = scope.getClassifier(referencedName); ClassifierDescriptor classifier = scope.getClassifier(referencedName);
@@ -388,8 +401,8 @@ public class DescriptorResolver {
} }
} }
private JetType resolveAndCheckUpperBoundType(@NotNull JetTypeReference upperBound, @NotNull JetScope scope, boolean classObjectConstaint) { private JetType resolveAndCheckUpperBoundType(@NotNull JetTypeReference upperBound, @NotNull JetScope scope, boolean classObjectConstaint, BindingTrace trace) {
JetType jetType = typeResolverNotCheckingBounds.resolveType(scope, upperBound); JetType jetType = typeResolver.resolveType(scope, upperBound, trace, false);
if (!TypeUtils.canHaveSubtypes(semanticServices.getTypeChecker(), jetType)) { if (!TypeUtils.canHaveSubtypes(semanticServices.getTypeChecker(), jetType)) {
if (classObjectConstaint) { if (classObjectConstaint) {
trace.report(FINAL_CLASS_OBJECT_UPPER_BOUND.on(upperBound, jetType)); trace.report(FINAL_CLASS_OBJECT_UPPER_BOUND.on(upperBound, jetType));
@@ -402,16 +415,16 @@ public class DescriptorResolver {
} }
@NotNull @NotNull
public VariableDescriptor resolveLocalVariableDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope scope, @NotNull JetParameter parameter) { public VariableDescriptor resolveLocalVariableDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope scope, @NotNull JetParameter parameter, BindingTrace trace) {
JetType type = resolveParameterType(scope, parameter); JetType type = resolveParameterType(scope, parameter, trace);
return resolveLocalVariableDescriptor(containingDeclaration, parameter, type); return resolveLocalVariableDescriptor(containingDeclaration, parameter, type, trace);
} }
private JetType resolveParameterType(JetScope scope, JetParameter parameter) { private JetType resolveParameterType(JetScope scope, JetParameter parameter, BindingTrace trace) {
JetTypeReference typeReference = parameter.getTypeReference(); JetTypeReference typeReference = parameter.getTypeReference();
JetType type; JetType type;
if (typeReference != null) { if (typeReference != null) {
type = typeResolver.resolveType(scope, typeReference); type = typeResolver.resolveType(scope, typeReference, trace, true);
} }
else { else {
// Error is reported by the parser // Error is reported by the parser
@@ -420,10 +433,10 @@ public class DescriptorResolver {
return type; return type;
} }
public VariableDescriptor resolveLocalVariableDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetParameter parameter, @NotNull JetType type) { public VariableDescriptor resolveLocalVariableDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetParameter parameter, @NotNull JetType type, BindingTrace trace) {
VariableDescriptor variableDescriptor = new LocalVariableDescriptor( VariableDescriptor variableDescriptor = new LocalVariableDescriptor(
containingDeclaration, containingDeclaration,
annotationResolver.createAnnotationStubs(parameter.getModifierList()), annotationResolver.createAnnotationStubs(parameter.getModifierList(), trace),
JetPsiUtil.safeName(parameter.getName()), JetPsiUtil.safeName(parameter.getName()),
type, type,
parameter.isMutable()); parameter.isMutable());
@@ -432,19 +445,19 @@ public class DescriptorResolver {
} }
@NotNull @NotNull
public VariableDescriptor resolveLocalVariableDescriptor(DeclarationDescriptor containingDeclaration, JetScope scope, JetProperty property, DataFlowInfo dataFlowInfo) { public VariableDescriptor resolveLocalVariableDescriptor(DeclarationDescriptor containingDeclaration, JetScope scope, JetProperty property, DataFlowInfo dataFlowInfo, BindingTrace trace) {
VariableDescriptorImpl variableDescriptor = resolveLocalVariableDescriptorWithType(containingDeclaration, property, null); VariableDescriptorImpl variableDescriptor = resolveLocalVariableDescriptorWithType(containingDeclaration, property, null, trace);
JetType type = getVariableType(scope, property, dataFlowInfo, false); // For a local variable the type must not be deferred JetType type = getVariableType(scope, property, dataFlowInfo, false, trace); // For a local variable the type must not be deferred
variableDescriptor.setOutType(type); variableDescriptor.setOutType(type);
return variableDescriptor; return variableDescriptor;
} }
@NotNull @NotNull
public VariableDescriptorImpl resolveLocalVariableDescriptorWithType(DeclarationDescriptor containingDeclaration, JetProperty property, JetType type) { public VariableDescriptorImpl resolveLocalVariableDescriptorWithType(DeclarationDescriptor containingDeclaration, JetProperty property, JetType type, BindingTrace trace) {
VariableDescriptorImpl variableDescriptor = new LocalVariableDescriptor( VariableDescriptorImpl variableDescriptor = new LocalVariableDescriptor(
containingDeclaration, containingDeclaration,
annotationResolver.createAnnotationStubs(property.getModifierList()), annotationResolver.createAnnotationStubs(property.getModifierList(), trace),
JetPsiUtil.safeName(property.getName()), JetPsiUtil.safeName(property.getName()),
type, type,
property.isVar()); property.isVar());
@@ -454,26 +467,26 @@ public class DescriptorResolver {
@NotNull @NotNull
public VariableDescriptor resolveObjectDeclaration(@NotNull DeclarationDescriptor containingDeclaration, public VariableDescriptor resolveObjectDeclaration(@NotNull DeclarationDescriptor containingDeclaration,
@NotNull JetClassOrObject objectDeclaration, @NotNull JetClassOrObject objectDeclaration,
@NotNull ClassDescriptor classDescriptor) { @NotNull ClassDescriptor classDescriptor, BindingTrace trace) {
boolean isProperty = (containingDeclaration instanceof NamespaceDescriptor) boolean isProperty = (containingDeclaration instanceof NamespaceDescriptor)
|| (containingDeclaration instanceof ClassDescriptor); || (containingDeclaration instanceof ClassDescriptor);
if (isProperty) { if (isProperty) {
return resolveObjectDeclarationAsPropertyDescriptor(containingDeclaration, objectDeclaration, classDescriptor); return resolveObjectDeclarationAsPropertyDescriptor(containingDeclaration, objectDeclaration, classDescriptor, trace);
} else { } else {
return resolveObjectDeclarationAsLocalVariable(containingDeclaration, objectDeclaration, classDescriptor); return resolveObjectDeclarationAsLocalVariable(containingDeclaration, objectDeclaration, classDescriptor, trace);
} }
} }
@NotNull @NotNull
public PropertyDescriptor resolveObjectDeclarationAsPropertyDescriptor(@NotNull DeclarationDescriptor containingDeclaration, public PropertyDescriptor resolveObjectDeclarationAsPropertyDescriptor(@NotNull DeclarationDescriptor containingDeclaration,
@NotNull JetClassOrObject objectDeclaration, @NotNull JetClassOrObject objectDeclaration,
@NotNull ClassDescriptor classDescriptor) { @NotNull ClassDescriptor classDescriptor, BindingTrace trace) {
JetModifierList modifierList = objectDeclaration.getModifierList(); JetModifierList modifierList = objectDeclaration.getModifierList();
Visibility visibility = resolveVisibilityFromModifiers(objectDeclaration.getModifierList()); Visibility visibility = resolveVisibilityFromModifiers(objectDeclaration.getModifierList());
PropertyDescriptor propertyDescriptor = new PropertyDescriptor( PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
containingDeclaration, containingDeclaration,
annotationResolver.createAnnotationStubs(modifierList), annotationResolver.createAnnotationStubs(modifierList, trace),
Modality.FINAL, Modality.FINAL,
visibility, visibility,
false, false,
@@ -492,11 +505,11 @@ public class DescriptorResolver {
@NotNull @NotNull
private VariableDescriptor resolveObjectDeclarationAsLocalVariable(@NotNull DeclarationDescriptor containingDeclaration, private VariableDescriptor resolveObjectDeclarationAsLocalVariable(@NotNull DeclarationDescriptor containingDeclaration,
@NotNull JetClassOrObject objectDeclaration, @NotNull JetClassOrObject objectDeclaration,
@NotNull ClassDescriptor classDescriptor) { @NotNull ClassDescriptor classDescriptor, BindingTrace trace) {
VariableDescriptorImpl variableDescriptor = new LocalVariableDescriptor( VariableDescriptorImpl variableDescriptor = new LocalVariableDescriptor(
containingDeclaration, containingDeclaration,
annotationResolver.createAnnotationStubs(objectDeclaration.getModifierList()), annotationResolver.createAnnotationStubs(objectDeclaration.getModifierList(), trace),
JetPsiUtil.safeName(objectDeclaration.getName()), JetPsiUtil.safeName(objectDeclaration.getName()),
classDescriptor.getDefaultType(), classDescriptor.getDefaultType(),
/*isVar =*/ false); /*isVar =*/ false);
@@ -508,8 +521,8 @@ public class DescriptorResolver {
} }
public JetScope getPropertyDeclarationInnerScope(@NotNull JetScope outerScope, public JetScope getPropertyDeclarationInnerScope(@NotNull JetScope outerScope,
@NotNull PropertyDescriptor propertyDescriptor, List<TypeParameterDescriptor> typeParameters, @NotNull PropertyDescriptor propertyDescriptor, List<TypeParameterDescriptor> typeParameters,
ReceiverDescriptor receiver) { ReceiverDescriptor receiver, BindingTrace trace) {
WritableScopeImpl result = new WritableScopeImpl(outerScope, propertyDescriptor, new TraceBasedRedeclarationHandler(trace)).setDebugName("Property declaration inner scope"); WritableScopeImpl result = new WritableScopeImpl(outerScope, propertyDescriptor, new TraceBasedRedeclarationHandler(trace)).setDebugName("Property declaration inner scope");
for (TypeParameterDescriptor typeParameterDescriptor : typeParameters) { for (TypeParameterDescriptor typeParameterDescriptor : typeParameters) {
result.addTypeParameterDescriptor(typeParameterDescriptor); result.addTypeParameterDescriptor(typeParameterDescriptor);
@@ -522,7 +535,7 @@ public class DescriptorResolver {
} }
@NotNull @NotNull
public PropertyDescriptor resolvePropertyDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope scope, JetProperty property) { public PropertyDescriptor resolvePropertyDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope scope, JetProperty property, BindingTrace trace) {
JetModifierList modifierList = property.getModifierList(); JetModifierList modifierList = property.getModifierList();
boolean isVar = property.isVar(); boolean isVar = property.isVar();
@@ -531,7 +544,7 @@ public class DescriptorResolver {
Modality defaultModality = getDefaultModality(containingDeclaration, hasBody); Modality defaultModality = getDefaultModality(containingDeclaration, hasBody);
PropertyDescriptor propertyDescriptor = new PropertyDescriptor( PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
containingDeclaration, containingDeclaration,
annotationResolver.resolveAnnotations(scope, modifierList), annotationResolver.resolveAnnotations(scope, modifierList, trace),
resolveModalityFromModifiers(property.getModifierList(), defaultModality), resolveModalityFromModifiers(property.getModifierList(), defaultModality),
resolveVisibilityFromModifiers(property.getModifierList()), resolveVisibilityFromModifiers(property.getModifierList()),
isVar, isVar,
@@ -552,15 +565,15 @@ public class DescriptorResolver {
} }
else { else {
WritableScope writableScope = new WritableScopeImpl(scope, containingDeclaration, new TraceBasedRedeclarationHandler(trace)).setDebugName("Scope with type parameters of a property"); WritableScope writableScope = new WritableScopeImpl(scope, containingDeclaration, new TraceBasedRedeclarationHandler(trace)).setDebugName("Scope with type parameters of a property");
typeParameterDescriptors = resolveTypeParameters(containingDeclaration, writableScope, typeParameters); typeParameterDescriptors = resolveTypeParameters(containingDeclaration, writableScope, typeParameters, trace);
writableScope.changeLockLevel(WritableScope.LockLevel.READING); writableScope.changeLockLevel(WritableScope.LockLevel.READING);
resolveGenericBounds(property, writableScope, typeParameterDescriptors); resolveGenericBounds(property, writableScope, typeParameterDescriptors, trace);
scopeWithTypeParameters = writableScope; scopeWithTypeParameters = writableScope;
} }
JetTypeReference receiverTypeRef = property.getReceiverTypeRef(); JetTypeReference receiverTypeRef = property.getReceiverTypeRef();
if (receiverTypeRef != null) { if (receiverTypeRef != null) {
receiverType = typeResolver.resolveType(scopeWithTypeParameters, receiverTypeRef); receiverType = typeResolver.resolveType(scopeWithTypeParameters, receiverTypeRef, trace, true);
} }
} }
@@ -568,14 +581,14 @@ public class DescriptorResolver {
? ReceiverDescriptor.NO_RECEIVER ? ReceiverDescriptor.NO_RECEIVER
: new ExtensionReceiver(propertyDescriptor, receiverType); : new ExtensionReceiver(propertyDescriptor, receiverType);
JetScope propertyScope = getPropertyDeclarationInnerScope(scope, propertyDescriptor, typeParameterDescriptors, receiverDescriptor); JetScope propertyScope = getPropertyDeclarationInnerScope(scope, propertyDescriptor, typeParameterDescriptors, receiverDescriptor, trace);
JetType type = getVariableType(propertyScope, property, DataFlowInfo.EMPTY, true); JetType type = getVariableType(propertyScope, property, DataFlowInfo.EMPTY, true, trace);
propertyDescriptor.setType(type, typeParameterDescriptors, DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration), receiverDescriptor); propertyDescriptor.setType(type, typeParameterDescriptors, DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration), receiverDescriptor);
PropertyGetterDescriptor getter = resolvePropertyGetterDescriptor(scopeWithTypeParameters, property, propertyDescriptor); PropertyGetterDescriptor getter = resolvePropertyGetterDescriptor(scopeWithTypeParameters, property, propertyDescriptor, trace);
PropertySetterDescriptor setter = resolvePropertySetterDescriptor(scopeWithTypeParameters, property, propertyDescriptor); PropertySetterDescriptor setter = resolvePropertySetterDescriptor(scopeWithTypeParameters, property, propertyDescriptor, trace);
propertyDescriptor.initialize(getter, setter); propertyDescriptor.initialize(getter, setter);
@@ -599,7 +612,7 @@ public class DescriptorResolver {
} }
@NotNull @NotNull
private JetType getVariableType(@NotNull final JetScope scope, @NotNull final JetProperty property, @NotNull final DataFlowInfo dataFlowInfo, boolean allowDeferred) { private JetType getVariableType(@NotNull final JetScope scope, @NotNull final JetProperty property, @NotNull final DataFlowInfo dataFlowInfo, boolean allowDeferred, final BindingTrace trace) {
// TODO : receiver? // TODO : receiver?
JetTypeReference propertyTypeRef = property.getPropertyTypeRef(); JetTypeReference propertyTypeRef = property.getPropertyTypeRef();
@@ -616,7 +629,7 @@ public class DescriptorResolver {
LazyValue<JetType> lazyValue = new LazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) { LazyValue<JetType> lazyValue = new LazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
@Override @Override
protected JetType compute() { protected JetType compute() {
return semanticServices.getTypeInferrerServices(trace).safeGetType(scope, initializer, TypeUtils.NO_EXPECTED_TYPE, dataFlowInfo); return expressionTypingServices.safeGetType(scope, initializer, TypeUtils.NO_EXPECTED_TYPE, dataFlowInfo, trace);
} }
}; };
if (allowDeferred) { if (allowDeferred) {
@@ -627,7 +640,7 @@ public class DescriptorResolver {
} }
} }
} else { } else {
return typeResolver.resolveType(scope, propertyTypeRef); return typeResolver.resolveType(scope, propertyTypeRef, trace, true);
} }
} }
@@ -676,11 +689,11 @@ public class DescriptorResolver {
} }
@Nullable @Nullable
private PropertySetterDescriptor resolvePropertySetterDescriptor(@NotNull JetScope scope, @NotNull JetProperty property, @NotNull PropertyDescriptor propertyDescriptor) { private PropertySetterDescriptor resolvePropertySetterDescriptor(@NotNull JetScope scope, @NotNull JetProperty property, @NotNull PropertyDescriptor propertyDescriptor, BindingTrace trace) {
JetPropertyAccessor setter = property.getSetter(); JetPropertyAccessor setter = property.getSetter();
PropertySetterDescriptor setterDescriptor = null; PropertySetterDescriptor setterDescriptor = null;
if (setter != null) { if (setter != null) {
List<AnnotationDescriptor> annotations = annotationResolver.resolveAnnotations(scope, setter.getModifierList()); List<AnnotationDescriptor> annotations = annotationResolver.resolveAnnotations(scope, setter.getModifierList(), trace);
JetParameter parameter = setter.getParameter(); JetParameter parameter = setter.getParameter();
setterDescriptor = new PropertySetterDescriptor( setterDescriptor = new PropertySetterDescriptor(
@@ -701,7 +714,7 @@ public class DescriptorResolver {
type = propertyDescriptor.getType(); // TODO : this maybe unknown at this point type = propertyDescriptor.getType(); // TODO : this maybe unknown at this point
} }
else { else {
type = typeResolver.resolveType(scope, typeReference); type = typeResolver.resolveType(scope, typeReference, trace, true);
JetType inType = propertyDescriptor.getType(); JetType inType = propertyDescriptor.getType();
if (inType != null) { if (inType != null) {
if (!TypeUtils.equalTypes(type, inType)) { if (!TypeUtils.equalTypes(type, inType)) {
@@ -713,7 +726,7 @@ public class DescriptorResolver {
} }
} }
MutableValueParameterDescriptor valueParameterDescriptor = resolveValueParameterDescriptor(setterDescriptor, parameter, 0, type); MutableValueParameterDescriptor valueParameterDescriptor = resolveValueParameterDescriptor(setterDescriptor, parameter, 0, type, trace);
setterDescriptor.initialize(valueParameterDescriptor); setterDescriptor.initialize(valueParameterDescriptor);
} }
else { else {
@@ -746,17 +759,17 @@ public class DescriptorResolver {
} }
@Nullable @Nullable
private PropertyGetterDescriptor resolvePropertyGetterDescriptor(@NotNull JetScope scope, @NotNull JetProperty property, @NotNull PropertyDescriptor propertyDescriptor) { private PropertyGetterDescriptor resolvePropertyGetterDescriptor(@NotNull JetScope scope, @NotNull JetProperty property, @NotNull PropertyDescriptor propertyDescriptor, BindingTrace trace) {
PropertyGetterDescriptor getterDescriptor; PropertyGetterDescriptor getterDescriptor;
JetPropertyAccessor getter = property.getGetter(); JetPropertyAccessor getter = property.getGetter();
if (getter != null) { if (getter != null) {
List<AnnotationDescriptor> annotations = annotationResolver.resolveAnnotations(scope, getter.getModifierList()); List<AnnotationDescriptor> annotations = annotationResolver.resolveAnnotations(scope, getter.getModifierList(), trace);
JetType outType = propertyDescriptor.getType(); JetType outType = propertyDescriptor.getType();
JetType returnType = outType; JetType returnType = outType;
JetTypeReference returnTypeReference = getter.getReturnTypeReference(); JetTypeReference returnTypeReference = getter.getReturnTypeReference();
if (returnTypeReference != null) { if (returnTypeReference != null) {
returnType = typeResolver.resolveType(scope, returnTypeReference); returnType = typeResolver.resolveType(scope, returnTypeReference, trace, true);
if (outType != null && !TypeUtils.equalTypes(returnType, outType)) { if (outType != null && !TypeUtils.equalTypes(returnType, outType)) {
trace.report(WRONG_GETTER_RETURN_TYPE.on(returnTypeReference, propertyDescriptor.getReturnType())); trace.report(WRONG_GETTER_RETURN_TYPE.on(returnTypeReference, propertyDescriptor.getReturnType()));
} }
@@ -786,8 +799,8 @@ public class DescriptorResolver {
} }
@NotNull @NotNull
public ConstructorDescriptorImpl resolveSecondaryConstructorDescriptor(@NotNull JetScope scope, @NotNull ClassDescriptor classDescriptor, @NotNull JetSecondaryConstructor constructor) { public ConstructorDescriptorImpl resolveSecondaryConstructorDescriptor(@NotNull JetScope scope, @NotNull ClassDescriptor classDescriptor, @NotNull JetSecondaryConstructor constructor, BindingTrace trace) {
return createConstructorDescriptor(scope, classDescriptor, false, constructor.getModifierList(), constructor, classDescriptor.getTypeConstructor().getParameters(), constructor.getValueParameters()); return createConstructorDescriptor(scope, classDescriptor, false, constructor.getModifierList(), constructor, classDescriptor.getTypeConstructor().getParameters(), constructor.getValueParameters(), trace);
} }
@NotNull @NotNull
@@ -797,10 +810,10 @@ public class DescriptorResolver {
boolean isPrimary, boolean isPrimary,
@Nullable JetModifierList modifierList, @Nullable JetModifierList modifierList,
@NotNull JetDeclaration declarationToTrace, @NotNull JetDeclaration declarationToTrace,
List<TypeParameterDescriptor> typeParameters, @NotNull List<JetParameter> valueParameters) { List<TypeParameterDescriptor> typeParameters, @NotNull List<JetParameter> valueParameters, BindingTrace trace) {
ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl( ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
classDescriptor, classDescriptor,
annotationResolver.resolveAnnotations(scope, modifierList), annotationResolver.resolveAnnotations(scope, modifierList, trace),
isPrimary isPrimary
); );
trace.record(BindingContext.CONSTRUCTOR, declarationToTrace, constructorDescriptor); trace.record(BindingContext.CONSTRUCTOR, declarationToTrace, constructorDescriptor);
@@ -811,12 +824,12 @@ public class DescriptorResolver {
resolveValueParameters( resolveValueParameters(
constructorDescriptor, constructorDescriptor,
parameterScope, parameterScope,
valueParameters), valueParameters, trace),
resolveVisibilityFromModifiers(modifierList)); resolveVisibilityFromModifiers(modifierList));
} }
@Nullable @Nullable
public ConstructorDescriptorImpl resolvePrimaryConstructorDescriptor(@NotNull JetScope scope, @NotNull ClassDescriptor classDescriptor, @NotNull JetClass classElement) { public ConstructorDescriptorImpl resolvePrimaryConstructorDescriptor(@NotNull JetScope scope, @NotNull ClassDescriptor classDescriptor, @NotNull JetClass classElement, BindingTrace trace) {
if (classDescriptor.getKind() == ClassKind.ENUM_ENTRY && !classElement.hasPrimaryConstructor()) return null; if (classDescriptor.getKind() == ClassKind.ENUM_ENTRY && !classElement.hasPrimaryConstructor()) return null;
return createConstructorDescriptor( return createConstructorDescriptor(
scope, scope,
@@ -824,15 +837,15 @@ public class DescriptorResolver {
true, true,
classElement.getPrimaryConstructorModifierList(), classElement.getPrimaryConstructorModifierList(),
classElement, classElement,
classDescriptor.getTypeConstructor().getParameters(), classElement.getPrimaryConstructorParameters()); classDescriptor.getTypeConstructor().getParameters(), classElement.getPrimaryConstructorParameters(), trace);
} }
@NotNull @NotNull
public PropertyDescriptor resolvePrimaryConstructorParameterToAProperty( public PropertyDescriptor resolvePrimaryConstructorParameterToAProperty(
@NotNull ClassDescriptor classDescriptor, @NotNull ClassDescriptor classDescriptor,
@NotNull JetScope scope, @NotNull JetScope scope,
@NotNull JetParameter parameter) { @NotNull JetParameter parameter, BindingTrace trace) {
JetType type = resolveParameterType(scope, parameter); JetType type = resolveParameterType(scope, parameter, trace);
String name = parameter.getName(); String name = parameter.getName();
boolean isMutable = parameter.isMutable(); boolean isMutable = parameter.isMutable();
JetModifierList modifierList = parameter.getModifierList(); JetModifierList modifierList = parameter.getModifierList();
@@ -846,7 +859,7 @@ public class DescriptorResolver {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor( PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
classDescriptor, classDescriptor,
annotationResolver.resolveAnnotations(scope, modifierList), annotationResolver.resolveAnnotations(scope, modifierList, trace),
resolveModalityFromModifiers(parameter.getModifierList(), Modality.FINAL), resolveModalityFromModifiers(parameter.getModifierList(), Modality.FINAL),
resolveVisibilityFromModifiers(parameter.getModifierList()), resolveVisibilityFromModifiers(parameter.getModifierList()),
isMutable, isMutable,
@@ -866,7 +879,7 @@ public class DescriptorResolver {
return propertyDescriptor; return propertyDescriptor;
} }
public void checkBounds(@NotNull JetTypeReference typeReference, @NotNull JetType type) { public void checkBounds(@NotNull JetTypeReference typeReference, @NotNull JetType type, BindingTrace trace) {
if (ErrorUtils.isErrorType(type)) return; if (ErrorUtils.isErrorType(type)) return;
JetTypeElement typeElement = typeReference.getTypeElement(); JetTypeElement typeElement = typeReference.getTypeElement();
@@ -886,10 +899,10 @@ public class DescriptorResolver {
if (argumentTypeReference == null) continue; if (argumentTypeReference == null) continue;
JetType typeArgument = arguments.get(i).getType(); JetType typeArgument = arguments.get(i).getType();
checkBounds(argumentTypeReference, typeArgument); checkBounds(argumentTypeReference, typeArgument, trace);
TypeParameterDescriptor typeParameterDescriptor = parameters.get(i); TypeParameterDescriptor typeParameterDescriptor = parameters.get(i);
checkBounds(argumentTypeReference, typeArgument, typeParameterDescriptor, substitutor); checkBounds(argumentTypeReference, typeArgument, typeParameterDescriptor, substitutor, trace);
} }
} }
@@ -897,7 +910,7 @@ public class DescriptorResolver {
@NotNull JetTypeReference argumentTypeReference, @NotNull JetTypeReference argumentTypeReference,
@NotNull JetType typeArgument, @NotNull JetType typeArgument,
@NotNull TypeParameterDescriptor typeParameterDescriptor, @NotNull TypeParameterDescriptor typeParameterDescriptor,
@NotNull TypeSubstitutor substitutor) { @NotNull TypeSubstitutor substitutor, BindingTrace trace) {
for (JetType bound : typeParameterDescriptor.getUpperBounds()) { for (JetType bound : typeParameterDescriptor.getUpperBounds()) {
JetType substitutedBound = substitutor.safeSubstitute(bound, Variance.INVARIANT); JetType substitutedBound = substitutor.safeSubstitute(bound, Variance.INVARIANT);
if (!semanticServices.getTypeChecker().isSubtypeOf(typeArgument, substitutedBound)) { if (!semanticServices.getTypeChecker().isSubtypeOf(typeArgument, substitutedBound)) {
@@ -22,17 +22,35 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.descriptors.ClassKind;
import org.jetbrains.jet.lang.resolve.scopes.*; import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetImportDirective;
import org.jetbrains.jet.lang.psi.JetQualifiedExpression;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import javax.inject.Inject;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.jetbrains.jet.lang.diagnostics.Errors.*; import static org.jetbrains.jet.lang.diagnostics.Errors.CANNOT_BE_IMPORTED;
import static org.jetbrains.jet.lang.diagnostics.Errors.CANNOT_IMPORT_FROM_ELEMENT;
import static org.jetbrains.jet.lang.diagnostics.Errors.NO_CLASS_OBJECT;
import static org.jetbrains.jet.lang.diagnostics.Errors.UNRESOLVED_REFERENCE;
import static org.jetbrains.jet.lang.diagnostics.Errors.UNSUPPORTED;
import static org.jetbrains.jet.lang.diagnostics.Errors.USELESS_HIDDEN_IMPORT;
import static org.jetbrains.jet.lang.diagnostics.Errors.USELESS_SIMPLE_IMPORT;
/** /**
* @author abreslav * @author abreslav
@@ -41,6 +59,7 @@ import static org.jetbrains.jet.lang.diagnostics.Errors.*;
public class ImportsResolver { public class ImportsResolver {
private final TopDownAnalysisContext context; private final TopDownAnalysisContext context;
@Inject
public ImportsResolver(@NotNull TopDownAnalysisContext context) { public ImportsResolver(@NotNull TopDownAnalysisContext context) {
this.context = context; this.context = context;
} }
@@ -19,6 +19,9 @@ package org.jetbrains.jet.lang.resolve;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -26,8 +29,15 @@ import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
import org.jetbrains.jet.lang.resolve.calls.OverloadingConflictResolver;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope; import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.defaults.DefaultPicoContainer;
import org.picocontainer.defaults.SetterInjectionComponentAdapter;
import org.picocontainer.defaults.SetterInjectionComponentAdapterFactory;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Map; import java.util.Map;
@@ -36,14 +46,28 @@ import java.util.Set;
/** /**
* @author abreslav * @author abreslav
*/ */
/*package*/ class TopDownAnalysisContext { public class TopDownAnalysisContext {
//private final MutablePicoContainer picoContainer;
private final ObservableBindingTrace trace; private final ObservableBindingTrace trace;
private final JetSemanticServices semanticServices;
private final Configuration configuration; private final Configuration configuration;
@NotNull
private final DescriptorResolver descriptorResolver; private final DescriptorResolver descriptorResolver;
@NotNull
private final ImportsResolver importsResolver; private final ImportsResolver importsResolver;
@NotNull
private final BodyResolver bodyResolver;
@NotNull
private final DeclarationResolver declarationResolver;
@NotNull
private final CallResolver.Context callResolverContext;
@NotNull
private final TypeResolver typeResolver;
@NotNull
private final ExpressionTypingServices expressionTypingServices;
private final Map<JetClass, MutableClassDescriptor> classes = Maps.newLinkedHashMap(); private final Map<JetClass, MutableClassDescriptor> classes = Maps.newLinkedHashMap();
private final Map<JetObjectDeclaration, MutableClassDescriptor> objects = Maps.newLinkedHashMap(); private final Map<JetObjectDeclaration, MutableClassDescriptor> objects = Maps.newLinkedHashMap();
protected final Map<JetFile, WritableScope> namespaceScopes = Maps.newHashMap(); protected final Map<JetFile, WritableScope> namespaceScopes = Maps.newHashMap();
@@ -61,11 +85,26 @@ import java.util.Set;
private boolean analyzingBootstrapLibrary = false; private boolean analyzingBootstrapLibrary = false;
private boolean declaredLocally; private boolean declaredLocally;
public TopDownAnalysisContext(JetSemanticServices semanticServices, BindingTrace trace, Predicate<PsiFile> analyzeCompletely, @NotNull Configuration configuration, boolean declaredLocally) { public TopDownAnalysisContext(final JetSemanticServices semanticServices, final BindingTrace trace, Predicate<PsiFile> analyzeCompletely, @NotNull Configuration configuration, boolean declaredLocally) {
class TdacModule extends AbstractModule {
@Override
protected void configure() {
bind(JetSemanticServices.class).toInstance(semanticServices);
bind(TopDownAnalysisContext.class).toInstance(TopDownAnalysisContext.this);
}
}
Injector injector = Guice.createInjector(new TdacModule());
this.importsResolver = injector.getInstance(ImportsResolver.class);
this.bodyResolver = injector.getInstance(BodyResolver.class);
this.declarationResolver = injector.getInstance(DeclarationResolver.class);
this.callResolverContext = injector.getInstance(CallResolver.Context.class);
this.typeResolver = injector.getInstance(TypeResolver.class);
this.expressionTypingServices = injector.getInstance(ExpressionTypingServices.class);
this.descriptorResolver = injector.getInstance(DescriptorResolver.class);
this.trace = new ObservableBindingTrace(trace); this.trace = new ObservableBindingTrace(trace);
this.semanticServices = semanticServices;
this.descriptorResolver = semanticServices.getClassDescriptorResolver(trace);
this.importsResolver = new ImportsResolver(this);
this.analyzeCompletely = analyzeCompletely; this.analyzeCompletely = analyzeCompletely;
this.configuration = configuration; this.configuration = configuration;
this.declaredLocally = declaredLocally; this.declaredLocally = declaredLocally;
@@ -106,14 +145,11 @@ import java.util.Set;
return result; return result;
} }
@NotNull
public ObservableBindingTrace getTrace() { public ObservableBindingTrace getTrace() {
return trace; return trace;
} }
public JetSemanticServices getSemanticServices() {
return semanticServices;
}
public DescriptorResolver getDescriptorResolver() { public DescriptorResolver getDescriptorResolver() {
return descriptorResolver; return descriptorResolver;
} }
@@ -122,6 +158,11 @@ import java.util.Set;
return importsResolver; return importsResolver;
} }
@NotNull
public BodyResolver getBodyResolver() {
return bodyResolver;
}
public Map<JetClass, MutableClassDescriptor> getClasses() { public Map<JetClass, MutableClassDescriptor> getClasses() {
return classes; return classes;
} }
@@ -166,4 +207,24 @@ import java.util.Set;
public boolean isDeclaredLocally() { public boolean isDeclaredLocally() {
return declaredLocally; return declaredLocally;
} }
@NotNull
public DeclarationResolver getDeclarationResolver() {
return declarationResolver;
}
@NotNull
public CallResolver.Context getCallResolverContext() {
return callResolverContext;
}
@NotNull
public TypeResolver getTypeResolver() {
return typeResolver;
}
@NotNull
public ExpressionTypingServices getExpressionTypingServices() {
return expressionTypingServices;
}
} }
@@ -80,13 +80,13 @@ public class TopDownAnalyzer {
context.debug("Enter"); context.debug("Enter");
new TypeHierarchyResolver(context).process(outerScope, owner, declarations); new TypeHierarchyResolver(context).process(outerScope, owner, declarations);
new DeclarationResolver(context).process(); context.getDeclarationResolver().process();
new DelegationResolver(context).process(); new DelegationResolver(context).process();
new OverrideResolver(context).process(); new OverrideResolver(context).process();
lockScopes(context); lockScopes(context);
new OverloadResolver(context).process(); new OverloadResolver(context).process();
if (!context.analyzingBootstrapLibrary()) { if (!context.analyzingBootstrapLibrary()) {
new BodyResolver(context).resolveBehaviorDeclarationBodies(); context.getBodyResolver().resolveBehaviorDeclarationBodies();
new ControlFlowAnalyzer(context, flowDataTraceFactory).process(); new ControlFlowAnalyzer(context, flowDataTraceFactory).process();
new DeclarationsChecker(context).process(); new DeclarationsChecker(context).process();
} }
@@ -19,7 +19,6 @@ package org.jetbrains.jet.lang.resolve;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner; import com.intellij.psi.PsiNameIdentifierOwner;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -260,7 +259,7 @@ public class TypeHierarchyResolver {
for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) { for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
JetClass jetClass = entry.getKey(); JetClass jetClass = entry.getKey();
MutableClassDescriptor descriptor = entry.getValue(); MutableClassDescriptor descriptor = entry.getValue();
context.getDescriptorResolver().resolveMutableClassDescriptor(jetClass, descriptor); context.getDescriptorResolver().resolveMutableClassDescriptor(jetClass, descriptor, context.getTrace());
descriptor.createTypeConstructor(); descriptor.createTypeConstructor();
} }
for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry : context.getObjects().entrySet()) { for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry : context.getObjects().entrySet()) {
@@ -277,13 +276,13 @@ public class TypeHierarchyResolver {
for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) { for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
JetClass jetClass = entry.getKey(); JetClass jetClass = entry.getKey();
MutableClassDescriptor descriptor = entry.getValue(); MutableClassDescriptor descriptor = entry.getValue();
context.getDescriptorResolver().resolveGenericBounds(jetClass, descriptor.getScopeForSupertypeResolution(), descriptor.getTypeConstructor().getParameters()); context.getDescriptorResolver().resolveGenericBounds(jetClass, descriptor.getScopeForSupertypeResolution(), descriptor.getTypeConstructor().getParameters(), context.getTrace());
context.getDescriptorResolver().resolveSupertypes(jetClass, descriptor); context.getDescriptorResolver().resolveSupertypes(jetClass, descriptor, context.getTrace());
} }
for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry : context.getObjects().entrySet()) { for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry : context.getObjects().entrySet()) {
JetClassOrObject jetClass = entry.getKey(); JetClassOrObject jetClass = entry.getKey();
MutableClassDescriptor descriptor = entry.getValue(); MutableClassDescriptor descriptor = entry.getValue();
context.getDescriptorResolver().resolveSupertypes(jetClass, descriptor); context.getDescriptorResolver().resolveSupertypes(jetClass, descriptor, context.getTrace());
} }
} }
@@ -478,7 +477,7 @@ public class TypeHierarchyResolver {
if (typeReference != null) { if (typeReference != null) {
JetType type = context.getTrace().getBindingContext().get(TYPE, typeReference); JetType type = context.getTrace().getBindingContext().get(TYPE, typeReference);
if (type != null) { if (type != null) {
context.getDescriptorResolver().checkBounds(typeReference, type); context.getDescriptorResolver().checkBounds(typeReference, type, context.getTrace());
} }
} }
} }
@@ -488,7 +487,7 @@ public class TypeHierarchyResolver {
if (extendsBound != null) { if (extendsBound != null) {
JetType type = context.getTrace().getBindingContext().get(TYPE, extendsBound); JetType type = context.getTrace().getBindingContext().get(TYPE, extendsBound);
if (type != null) { if (type != null) {
context.getDescriptorResolver().checkBounds(extendsBound, type); context.getDescriptorResolver().checkBounds(extendsBound, type, context.getTrace());
} }
} }
} }
@@ -498,7 +497,7 @@ public class TypeHierarchyResolver {
if (extendsBound != null) { if (extendsBound != null) {
JetType type = context.getTrace().getBindingContext().get(TYPE, extendsBound); JetType type = context.getTrace().getBindingContext().get(TYPE, extendsBound);
if (type != null) { if (type != null) {
context.getDescriptorResolver().checkBounds(extendsBound, type); context.getDescriptorResolver().checkBounds(extendsBound, type, context.getTrace());
} }
} }
} }
@@ -18,24 +18,44 @@ package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetFunctionType;
import org.jetbrains.jet.lang.psi.JetNullableType;
import org.jetbrains.jet.lang.psi.JetParameter;
import org.jetbrains.jet.lang.psi.JetProjectionKind;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.jet.lang.psi.JetTupleType;
import org.jetbrains.jet.lang.psi.JetTypeElement;
import org.jetbrains.jet.lang.psi.JetTypeProjection;
import org.jetbrains.jet.lang.psi.JetTypeReference;
import org.jetbrains.jet.lang.psi.JetUserType;
import org.jetbrains.jet.lang.psi.JetVisitorVoid;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.LazyScopeAdapter; import org.jetbrains.jet.lang.resolve.scopes.LazyScopeAdapter;
import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.JetTypeImpl;
import org.jetbrains.jet.lang.types.TypeConstructor;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lang.types.Variance;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses; import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import org.jetbrains.jet.util.lazy.LazyValue; import org.jetbrains.jet.util.lazy.LazyValue;
import javax.inject.Inject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static org.jetbrains.jet.lang.diagnostics.Errors.*; import static org.jetbrains.jet.lang.diagnostics.Errors.UNRESOLVED_REFERENCE;
import static org.jetbrains.jet.lang.diagnostics.Errors.UNSUPPORTED;
import static org.jetbrains.jet.lang.diagnostics.Errors.WRONG_NUMBER_OF_TYPE_ARGUMENTS;
import static org.jetbrains.jet.lang.resolve.BindingContext.REFERENCE_TARGET; import static org.jetbrains.jet.lang.resolve.BindingContext.REFERENCE_TARGET;
/** /**
@@ -43,27 +63,28 @@ import static org.jetbrains.jet.lang.resolve.BindingContext.REFERENCE_TARGET;
*/ */
public class TypeResolver { public class TypeResolver {
private final JetSemanticServices semanticServices; private AnnotationResolver annotationResolver;
private final BindingTrace trace; private DescriptorResolver descriptorResolver;
private final boolean checkBounds;
private final AnnotationResolver annotationResolver;
public TypeResolver(JetSemanticServices semanticServices, BindingTrace trace, boolean checkBounds) { @Inject
this.semanticServices = semanticServices; public void setDescriptorResolver(DescriptorResolver descriptorResolver) {
this.trace = trace; this.descriptorResolver = descriptorResolver;
this.checkBounds = checkBounds; }
this.annotationResolver = new AnnotationResolver(semanticServices, trace);
@Inject
public void setAnnotationResolver(AnnotationResolver annotationResolver) {
this.annotationResolver = annotationResolver;
} }
@NotNull @NotNull
public JetType resolveType(@NotNull final JetScope scope, @NotNull final JetTypeReference typeReference) { public JetType resolveType(@NotNull final JetScope scope, @NotNull final JetTypeReference typeReference, BindingTrace trace, boolean checkBounds) {
JetType cachedType = trace.getBindingContext().get(BindingContext.TYPE, typeReference); JetType cachedType = trace.getBindingContext().get(BindingContext.TYPE, typeReference);
if (cachedType != null) return cachedType; if (cachedType != null) return cachedType;
final List<AnnotationDescriptor> annotations = annotationResolver.createAnnotationStubs(typeReference.getAnnotations()); final List<AnnotationDescriptor> annotations = annotationResolver.createAnnotationStubs(typeReference.getAnnotations(), trace);
JetTypeElement typeElement = typeReference.getTypeElement(); JetTypeElement typeElement = typeReference.getTypeElement();
JetType type = resolveTypeElement(scope, annotations, typeElement, false); JetType type = resolveTypeElement(scope, annotations, typeElement, false, trace, checkBounds);
trace.record(BindingContext.TYPE, typeReference, type); trace.record(BindingContext.TYPE, typeReference, type);
return type; return type;
@@ -71,7 +92,7 @@ public class TypeResolver {
@NotNull @NotNull
private JetType resolveTypeElement(final JetScope scope, final List<AnnotationDescriptor> annotations, private JetType resolveTypeElement(final JetScope scope, final List<AnnotationDescriptor> annotations,
JetTypeElement typeElement, final boolean nullable) { JetTypeElement typeElement, final boolean nullable, final BindingTrace trace, final boolean checkBounds) {
final JetType[] result = new JetType[1]; final JetType[] result = new JetType[1];
if (typeElement != null) { if (typeElement != null) {
@@ -84,9 +105,9 @@ public class TypeResolver {
return; return;
} }
ClassifierDescriptor classifierDescriptor = resolveClass(scope, type); ClassifierDescriptor classifierDescriptor = resolveClass(scope, type, trace);
if (classifierDescriptor == null) { if (classifierDescriptor == null) {
resolveTypeProjections(scope, ErrorUtils.createErrorType("No type").getConstructor(), type.getTypeArguments()); resolveTypeProjections(scope, ErrorUtils.createErrorType("No type").getConstructor(), type.getTypeArguments(), trace, checkBounds);
return; return;
} }
@@ -95,7 +116,7 @@ public class TypeResolver {
trace.record(BindingContext.REFERENCE_TARGET, referenceExpression, typeParameterDescriptor); trace.record(BindingContext.REFERENCE_TARGET, referenceExpression, typeParameterDescriptor);
JetScope scopeForTypeParameter = getScopeForTypeParameter(typeParameterDescriptor); JetScope scopeForTypeParameter = getScopeForTypeParameter(typeParameterDescriptor, checkBounds);
if (scopeForTypeParameter instanceof ErrorUtils.ErrorScope) { if (scopeForTypeParameter instanceof ErrorUtils.ErrorScope) {
result[0] = ErrorUtils.createErrorType("?"); result[0] = ErrorUtils.createErrorType("?");
} else { } else {
@@ -108,14 +129,14 @@ public class TypeResolver {
); );
} }
resolveTypeProjections(scope, ErrorUtils.createErrorType("No type").getConstructor(), type.getTypeArguments()); resolveTypeProjections(scope, ErrorUtils.createErrorType("No type").getConstructor(), type.getTypeArguments(), trace, checkBounds);
} }
else if (classifierDescriptor instanceof ClassDescriptor) { else if (classifierDescriptor instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) classifierDescriptor; ClassDescriptor classDescriptor = (ClassDescriptor) classifierDescriptor;
trace.record(BindingContext.REFERENCE_TARGET, referenceExpression, classifierDescriptor); trace.record(BindingContext.REFERENCE_TARGET, referenceExpression, classifierDescriptor);
TypeConstructor typeConstructor = classifierDescriptor.getTypeConstructor(); TypeConstructor typeConstructor = classifierDescriptor.getTypeConstructor();
List<TypeProjection> arguments = resolveTypeProjections(scope, typeConstructor, type.getTypeArguments()); List<TypeProjection> arguments = resolveTypeProjections(scope, typeConstructor, type.getTypeArguments(), trace, checkBounds);
List<TypeParameterDescriptor> parameters = typeConstructor.getParameters(); List<TypeParameterDescriptor> parameters = typeConstructor.getParameters();
int expectedArgumentCount = parameters.size(); int expectedArgumentCount = parameters.size();
int actualArgumentCount = arguments.size(); int actualArgumentCount = arguments.size();
@@ -145,7 +166,7 @@ public class TypeResolver {
JetTypeReference typeReference = type.getTypeArguments().get(i).getTypeReference(); JetTypeReference typeReference = type.getTypeArguments().get(i).getTypeReference();
if (typeReference != null) { if (typeReference != null) {
semanticServices.getClassDescriptorResolver(trace).checkBounds(typeReference, argument, parameter, substitutor); descriptorResolver.checkBounds(typeReference, argument, parameter, substitutor, trace);
} }
} }
} }
@@ -156,29 +177,29 @@ public class TypeResolver {
@Override @Override
public void visitNullableType(JetNullableType nullableType) { public void visitNullableType(JetNullableType nullableType) {
result[0] = resolveTypeElement(scope, annotations, nullableType.getInnerType(), true); result[0] = resolveTypeElement(scope, annotations, nullableType.getInnerType(), true, trace, checkBounds);
} }
@Override @Override
public void visitTupleType(JetTupleType type) { public void visitTupleType(JetTupleType type) {
// TODO labels // TODO labels
result[0] = JetStandardClasses.getTupleType(resolveTypes(scope, type.getComponentTypeRefs())); result[0] = JetStandardClasses.getTupleType(resolveTypes(scope, type.getComponentTypeRefs(), trace, checkBounds));
} }
@Override @Override
public void visitFunctionType(JetFunctionType type) { public void visitFunctionType(JetFunctionType type) {
JetTypeReference receiverTypeRef = type.getReceiverTypeRef(); JetTypeReference receiverTypeRef = type.getReceiverTypeRef();
JetType receiverType = receiverTypeRef == null ? null : resolveType(scope, receiverTypeRef); JetType receiverType = receiverTypeRef == null ? null : resolveType(scope, receiverTypeRef, trace, checkBounds);
List<JetType> parameterTypes = new ArrayList<JetType>(); List<JetType> parameterTypes = new ArrayList<JetType>();
for (JetParameter parameter : type.getParameters()) { for (JetParameter parameter : type.getParameters()) {
parameterTypes.add(resolveType(scope, parameter.getTypeReference())); parameterTypes.add(resolveType(scope, parameter.getTypeReference(), trace, checkBounds));
} }
JetTypeReference returnTypeRef = type.getReturnTypeRef(); JetTypeReference returnTypeRef = type.getReturnTypeRef();
JetType returnType; JetType returnType;
if (returnTypeRef != null) { if (returnTypeRef != null) {
returnType = resolveType(scope, returnTypeRef); returnType = resolveType(scope, returnTypeRef, trace, checkBounds);
} }
else { else {
returnType = JetStandardClasses.getUnitType(); returnType = JetStandardClasses.getUnitType();
@@ -202,7 +223,7 @@ public class TypeResolver {
return result[0]; return result[0];
} }
private JetScope getScopeForTypeParameter(final TypeParameterDescriptor typeParameterDescriptor) { private JetScope getScopeForTypeParameter(final TypeParameterDescriptor typeParameterDescriptor, boolean checkBounds) {
if (checkBounds) { if (checkBounds) {
return typeParameterDescriptor.getUpperBoundsAsType().getMemberScope(); return typeParameterDescriptor.getUpperBoundsAsType().getMemberScope();
} }
@@ -216,16 +237,16 @@ public class TypeResolver {
} }
} }
private List<JetType> resolveTypes(JetScope scope, List<JetTypeReference> argumentElements) { private List<JetType> resolveTypes(JetScope scope, List<JetTypeReference> argumentElements, BindingTrace trace, boolean checkBounds) {
final List<JetType> arguments = new ArrayList<JetType>(); final List<JetType> arguments = new ArrayList<JetType>();
for (JetTypeReference argumentElement : argumentElements) { for (JetTypeReference argumentElement : argumentElements) {
arguments.add(resolveType(scope, argumentElement)); arguments.add(resolveType(scope, argumentElement, trace, checkBounds));
} }
return arguments; return arguments;
} }
@NotNull @NotNull
private List<TypeProjection> resolveTypeProjections(JetScope scope, TypeConstructor constructor, List<JetTypeProjection> argumentElements) { private List<TypeProjection> resolveTypeProjections(JetScope scope, TypeConstructor constructor, List<JetTypeProjection> argumentElements, BindingTrace trace, boolean checkBounds) {
final List<TypeProjection> arguments = new ArrayList<TypeProjection>(); final List<TypeProjection> arguments = new ArrayList<TypeProjection>();
for (int i = 0, argumentElementsSize = argumentElements.size(); i < argumentElementsSize; i++) { for (int i = 0, argumentElementsSize = argumentElements.size(); i < argumentElementsSize; i++) {
JetTypeProjection argumentElement = argumentElements.get(i); JetTypeProjection argumentElement = argumentElements.get(i);
@@ -244,7 +265,7 @@ public class TypeResolver {
} }
else { else {
// TODO : handle the Foo<in *> case // TODO : handle the Foo<in *> case
type = resolveType(scope, argumentElement.getTypeReference()); type = resolveType(scope, argumentElement.getTypeReference(), trace, checkBounds);
Variance kind = null; Variance kind = null;
switch (projectionKind) { switch (projectionKind) {
case IN: case IN:
@@ -265,8 +286,8 @@ public class TypeResolver {
} }
@Nullable @Nullable
public ClassifierDescriptor resolveClass(JetScope scope, JetUserType userType) { public ClassifierDescriptor resolveClass(JetScope scope, JetUserType userType, BindingTrace trace) {
ClassifierDescriptor classifierDescriptor = resolveClassWithoutErrorReporting(scope, userType); ClassifierDescriptor classifierDescriptor = resolveClassWithoutErrorReporting(scope, userType, trace);
if (classifierDescriptor == null) { if (classifierDescriptor == null) {
trace.report(UNRESOLVED_REFERENCE.on(userType.getReferenceExpression())); trace.report(UNRESOLVED_REFERENCE.on(userType.getReferenceExpression()));
@@ -279,7 +300,7 @@ public class TypeResolver {
} }
@Nullable @Nullable
private ClassifierDescriptor resolveClassWithoutErrorReporting(JetScope scope, JetUserType userType) { private ClassifierDescriptor resolveClassWithoutErrorReporting(JetScope scope, JetUserType userType, BindingTrace trace) {
JetSimpleNameExpression expression = userType.getReferenceExpression(); JetSimpleNameExpression expression = userType.getReferenceExpression();
if (expression == null) { if (expression == null) {
return null; return null;
@@ -292,12 +313,12 @@ public class TypeResolver {
if (userType.isAbsoluteInRootNamespace()) { if (userType.isAbsoluteInRootNamespace()) {
classifierDescriptor = JetModuleUtil.getRootNamespaceType(userType).getMemberScope().getClassifier(referencedName); classifierDescriptor = JetModuleUtil.getRootNamespaceType(userType).getMemberScope().getClassifier(referencedName);
trace.record(BindingContext.RESOLUTION_SCOPE, userType.getReferenceExpression(), trace.record(BindingContext.RESOLUTION_SCOPE, userType.getReferenceExpression(),
JetModuleUtil.getRootNamespaceType(userType).getMemberScope()); JetModuleUtil.getRootNamespaceType(userType).getMemberScope());
} }
else { else {
JetUserType qualifier = userType.getQualifier(); JetUserType qualifier = userType.getQualifier();
if (qualifier != null) { if (qualifier != null) {
scope = resolveClassLookupScope(scope, qualifier); scope = resolveClassLookupScope(scope, qualifier, trace);
} }
if (scope == null) { if (scope == null) {
return ErrorUtils.getErrorClass(); return ErrorUtils.getErrorClass();
@@ -310,8 +331,8 @@ public class TypeResolver {
} }
@Nullable @Nullable
private JetScope resolveClassLookupScope(JetScope scope, JetUserType userType) { private JetScope resolveClassLookupScope(JetScope scope, JetUserType userType, BindingTrace trace) {
ClassifierDescriptor classifierDescriptor = resolveClassWithoutErrorReporting(scope, userType); ClassifierDescriptor classifierDescriptor = resolveClassWithoutErrorReporting(scope, userType, trace);
if (classifierDescriptor instanceof ClassDescriptor) { if (classifierDescriptor instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) classifierDescriptor; ClassDescriptor classDescriptor = (ClassDescriptor) classifierDescriptor;
JetType classObjectType = classDescriptor.getClassObjectType(); JetType classObjectType = classDescriptor.getClassObjectType();
@@ -320,7 +341,7 @@ public class TypeResolver {
} }
} }
NamespaceDescriptor namespaceDescriptor = resolveNamespace(scope, userType); NamespaceDescriptor namespaceDescriptor = resolveNamespace(scope, userType, trace);
if (namespaceDescriptor == null) { if (namespaceDescriptor == null) {
trace.report(UNRESOLVED_REFERENCE.on(userType.getReferenceExpression())); trace.report(UNRESOLVED_REFERENCE.on(userType.getReferenceExpression()));
return null; return null;
@@ -329,15 +350,15 @@ public class TypeResolver {
} }
@Nullable @Nullable
private NamespaceDescriptor resolveNamespace(JetScope scope, JetUserType userType) { private NamespaceDescriptor resolveNamespace(JetScope scope, JetUserType userType, BindingTrace trace) {
if (userType.isAbsoluteInRootNamespace()) { if (userType.isAbsoluteInRootNamespace()) {
return resolveNamespace(JetModuleUtil.getRootNamespaceType(userType).getMemberScope(), userType); return resolveNamespace(JetModuleUtil.getRootNamespaceType(userType).getMemberScope(), userType, trace);
} }
JetUserType qualifier = userType.getQualifier(); JetUserType qualifier = userType.getQualifier();
NamespaceDescriptor namespace; NamespaceDescriptor namespace;
if (qualifier != null) { if (qualifier != null) {
NamespaceDescriptor domain = resolveNamespace(scope, qualifier); NamespaceDescriptor domain = resolveNamespace(scope, qualifier, trace);
if (domain == null) { if (domain == null) {
return null; return null;
} }
@@ -19,6 +19,7 @@ package org.jetbrains.jet.lang.resolve.calls;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -33,6 +34,7 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver; import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices; import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
import org.jetbrains.jet.lang.types.expressions.OperatorConventions; import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses; import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
@@ -56,14 +58,46 @@ import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE;
public class CallResolver { public class CallResolver {
private static final JetType DONT_CARE = ErrorUtils.createErrorTypeWithCustomDebugName("DONT_CARE"); private static final JetType DONT_CARE = ErrorUtils.createErrorTypeWithCustomDebugName("DONT_CARE");
private final JetSemanticServices semanticServices; private final JetTypeChecker typeChecker = JetTypeChecker.INSTANCE;
private final OverloadingConflictResolver overloadingConflictResolver; private final OverloadingConflictResolver overloadingConflictResolver;
private final DataFlowInfo dataFlowInfo; private final DataFlowInfo dataFlowInfo;
private final TypeResolver typeResolver;
public CallResolver(JetSemanticServices semanticServices, DataFlowInfo dataFlowInfo) { public static class Context {
this.semanticServices = semanticServices; public OverloadingConflictResolver overloadingConflictResolver;
this.overloadingConflictResolver = new OverloadingConflictResolver(semanticServices); public DescriptorResolver descriptorResolver;
public TypeResolver typeResolver;
public ExpressionTypingServices expressionTypingServices;
@Inject
public void setOverloadingConflictResolver(OverloadingConflictResolver overloadingConflictResolver) {
this.overloadingConflictResolver = overloadingConflictResolver;
}
@Inject
public void setDescriptorResolver(DescriptorResolver descriptorResolver) {
this.descriptorResolver = descriptorResolver;
}
@Inject
public void setTypeResolver(TypeResolver typeResolver) {
this.typeResolver = typeResolver;
}
@Inject
public void setExpressionTypingServices(ExpressionTypingServices expressionTypingServices) {
this.expressionTypingServices = expressionTypingServices;
}
}
private final Context context;
public CallResolver(Context context, DataFlowInfo dataFlowInfo) {
this.context = context;
this.dataFlowInfo = dataFlowInfo; this.dataFlowInfo = dataFlowInfo;
this.overloadingConflictResolver = context.overloadingConflictResolver;
this.typeResolver = context.typeResolver;
} }
@NotNull @NotNull
@@ -165,7 +199,7 @@ public class CallResolver {
} }
JetTypeReference typeReference = expression.getTypeReference(); JetTypeReference typeReference = expression.getTypeReference();
assert typeReference != null; assert typeReference != null;
JetType constructedType = new TypeResolver(semanticServices, trace, true).resolveType(scope, typeReference); JetType constructedType = typeResolver.resolveType(scope, typeReference, trace, true);
DeclarationDescriptor declarationDescriptor = constructedType.getConstructor().getDeclarationDescriptor(); DeclarationDescriptor declarationDescriptor = constructedType.getConstructor().getDeclarationDescriptor();
if (declarationDescriptor instanceof ClassDescriptor) { if (declarationDescriptor instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor; ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor;
@@ -199,8 +233,7 @@ public class CallResolver {
} }
else if (calleeExpression != null) { else if (calleeExpression != null) {
// Here we handle the case where the callee expression must be something of type function, e.g. (foo.bar())(1, 2) // Here we handle the case where the callee expression must be something of type function, e.g. (foo.bar())(1, 2)
ExpressionTypingServices typingServices = new ExpressionTypingServices(semanticServices, trace); JetType calleeType = context.expressionTypingServices.safeGetType(scope, calleeExpression, NO_EXPECTED_TYPE, trace); // We are actually expecting a function, but there seems to be no easy way of expressing this
JetType calleeType = typingServices.safeGetType(scope, calleeExpression, NO_EXPECTED_TYPE); // We are actually expecting a function, but there seems to be no easy way of expressing this
if (!JetStandardClasses.isFunctionType(calleeType)) { if (!JetStandardClasses.isFunctionType(calleeType)) {
// checkTypesWithNoCallee(trace, scope, call); // checkTypesWithNoCallee(trace, scope, call);
@@ -544,8 +577,7 @@ public class CallResolver {
// and throw the results away // and throw the results away
// We'll type check the arguments later, with the inferred types expected // We'll type check the arguments later, with the inferred types expected
TemporaryBindingTrace traceForUnknown = TemporaryBindingTrace.create(temporaryTrace); TemporaryBindingTrace traceForUnknown = TemporaryBindingTrace.create(temporaryTrace);
ExpressionTypingServices temporaryServices = new ExpressionTypingServices(semanticServices, traceForUnknown); JetType type = context.expressionTypingServices.getType(scope, expression, substituteDontCare.substitute(valueParameterDescriptor.getType(), Variance.INVARIANT), traceForUnknown);
JetType type = temporaryServices.getType(scope, expression, substituteDontCare.substitute(valueParameterDescriptor.getType(), Variance.INVARIANT));
if (type != null && !ErrorUtils.isErrorType(type)) { if (type != null && !ErrorUtils.isErrorType(type)) {
constraintSystem.addSubtypingConstraint(VALUE_ARGUMENT.assertSubtyping(type, effectiveExpectedType)); constraintSystem.addSubtypingConstraint(VALUE_ARGUMENT.assertSubtyping(type, effectiveExpectedType));
} }
@@ -604,7 +636,7 @@ public class CallResolver {
} }
JetTypeReference typeReference = projection.getTypeReference(); JetTypeReference typeReference = projection.getTypeReference();
if (typeReference != null) { if (typeReference != null) {
typeArguments.add(new TypeResolver(semanticServices, temporaryTrace, true).resolveType(scope, typeReference)); typeArguments.add(typeResolver.resolveType(scope, typeReference, trace, true));
} }
} }
int expectedTypeArgumentCount = candidate.getTypeParameters().size(); int expectedTypeArgumentCount = candidate.getTypeParameters().size();
@@ -693,20 +725,19 @@ public class CallResolver {
} }
private void checkTypesWithNoCallee(BindingTrace trace, JetScope scope, Call call) { private void checkTypesWithNoCallee(BindingTrace trace, JetScope scope, Call call) {
ExpressionTypingServices typeInferrerServices = new ExpressionTypingServices(semanticServices, trace);
for (ValueArgument valueArgument : call.getValueArguments()) { for (ValueArgument valueArgument : call.getValueArguments()) {
JetExpression argumentExpression = valueArgument.getArgumentExpression(); JetExpression argumentExpression = valueArgument.getArgumentExpression();
if (argumentExpression != null) { if (argumentExpression != null) {
typeInferrerServices.getType(scope, argumentExpression, NO_EXPECTED_TYPE); context.expressionTypingServices.getType(scope, argumentExpression, NO_EXPECTED_TYPE, trace);
} }
} }
for (JetExpression expression : call.getFunctionLiteralArguments()) { for (JetExpression expression : call.getFunctionLiteralArguments()) {
typeInferrerServices.getType(scope, expression, NO_EXPECTED_TYPE); context.expressionTypingServices.getType(scope, expression, NO_EXPECTED_TYPE, trace);
} }
for (JetTypeProjection typeProjection : call.getTypeArguments()) { for (JetTypeProjection typeProjection : call.getTypeArguments()) {
new TypeResolver(semanticServices, trace, true).resolveType(scope, typeProjection.getTypeReference()); typeResolver.resolveType(scope, typeProjection.getTypeReference(), trace, true);
} }
} }
@@ -752,7 +783,7 @@ public class CallResolver {
JetType effectiveReceiverArgumentType = safeAccess JetType effectiveReceiverArgumentType = safeAccess
? TypeUtils.makeNotNullable(receiverArgumentType) ? TypeUtils.makeNotNullable(receiverArgumentType)
: receiverArgumentType; : receiverArgumentType;
if (!semanticServices.getTypeChecker().isSubtypeOf(effectiveReceiverArgumentType, receiverParameter.getType())) { if (!typeChecker.isSubtypeOf(effectiveReceiverArgumentType, receiverParameter.getType())) {
tracing.wrongReceiverType(candidateCall.getTrace(), receiverParameter, receiverArgument); tracing.wrongReceiverType(candidateCall.getTrace(), receiverParameter, receiverArgument);
result = OTHER_ERROR; result = OTHER_ERROR;
} }
@@ -775,12 +806,12 @@ public class CallResolver {
List<JetExpression> argumentExpressions = resolvedArgument.getArgumentExpressions(); List<JetExpression> argumentExpressions = resolvedArgument.getArgumentExpressions();
for (JetExpression argumentExpression : argumentExpressions) { for (JetExpression argumentExpression : argumentExpressions) {
ExpressionTypingServices temporaryServices = new ExpressionTypingServices(semanticServices, candidateCall.getTrace()); ExpressionTypingServices temporaryServices = context.expressionTypingServices;
JetType type = temporaryServices.getType(scope, argumentExpression, parameterType, dataFlowInfo); JetType type = temporaryServices.getType(scope, argumentExpression, parameterType, dataFlowInfo, candidateCall.getTrace());
if (type == null || ErrorUtils.isErrorType(type)) { if (type == null || ErrorUtils.isErrorType(type)) {
candidateCall.argumentHasNoType(); candidateCall.argumentHasNoType();
} }
else if (!semanticServices.getTypeChecker().isSubtypeOf(type, parameterType)) { else if (!typeChecker.isSubtypeOf(type, parameterType)) {
// VariableDescriptor variableDescriptor = AutoCastUtils.getVariableDescriptorFromSimpleName(temporaryTrace.getBindingContext(), argumentExpression); // VariableDescriptor variableDescriptor = AutoCastUtils.getVariableDescriptorFromSimpleName(temporaryTrace.getBindingContext(), argumentExpression);
// if (variableDescriptor != null) { // if (variableDescriptor != null) {
// JetType autoCastType = null; // JetType autoCastType = null;
@@ -935,7 +966,7 @@ public class CallResolver {
JetType typeArgument = typeArguments.get(i); JetType typeArgument = typeArguments.get(i);
JetTypeReference typeReference = jetTypeArguments.get(i).getTypeReference(); JetTypeReference typeReference = jetTypeArguments.get(i).getTypeReference();
assert typeReference != null; assert typeReference != null;
semanticServices.getClassDescriptorResolver(trace).checkBounds(typeReference, typeArgument, typeParameterDescriptor, substitutor); this.context.descriptorResolver.checkBounds(typeReference, typeArgument, typeParameterDescriptor, substitutor, trace);
} }
} }
@@ -1003,7 +1034,7 @@ public class CallResolver {
ReceiverDescriptor functionReceiver = functionDescriptor.getReceiverParameter(); ReceiverDescriptor functionReceiver = functionDescriptor.getReceiverParameter();
if (!functionReceiver.exists()) continue; if (!functionReceiver.exists()) continue;
if (!functionDescriptor.getTypeParameters().isEmpty()) continue; if (!functionDescriptor.getTypeParameters().isEmpty()) continue;
if (!semanticServices.getTypeChecker().isSubtypeOf(receiver.getType(), functionReceiver.getType())) continue; if (!typeChecker.isSubtypeOf(receiver.getType(), functionReceiver.getType())) continue;
if (!checkValueParameters(functionDescriptor, parameterTypes))continue; if (!checkValueParameters(functionDescriptor, parameterTypes))continue;
result.add(resolvedCall); result.add(resolvedCall);
found = true; found = true;
@@ -30,6 +30,7 @@ import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils; import org.jetbrains.jet.lang.types.TypeUtils;
import javax.inject.Inject;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -40,6 +41,7 @@ public class OverloadingConflictResolver {
private final JetSemanticServices semanticServices; private final JetSemanticServices semanticServices;
@Inject
public OverloadingConflictResolver(@NotNull JetSemanticServices semanticServices) { public OverloadingConflictResolver(@NotNull JetSemanticServices semanticServices) {
this.semanticServices = semanticServices; this.semanticServices = semanticServices;
} }
@@ -53,6 +53,7 @@ public class DataFlowInfo {
public static DataFlowInfo EMPTY = new DataFlowInfo(ImmutableMap.<DataFlowValue, Nullability>of(), Multimaps.newListMultimap(Collections.<DataFlowValue, Collection<JetType>>emptyMap(), CommonSuppliers.<JetType>getArrayListSupplier())); public static DataFlowInfo EMPTY = new DataFlowInfo(ImmutableMap.<DataFlowValue, Nullability>of(), Multimaps.newListMultimap(Collections.<DataFlowValue, Collection<JetType>>emptyMap(), CommonSuppliers.<JetType>getArrayListSupplier()));
private final ImmutableMap<DataFlowValue, Nullability> nullabilityInfo; private final ImmutableMap<DataFlowValue, Nullability> nullabilityInfo;
/** Also immutable */
private final ListMultimap<DataFlowValue, JetType> typeInfo; private final ListMultimap<DataFlowValue, JetType> typeInfo;
private DataFlowInfo(ImmutableMap<DataFlowValue, Nullability> nullabilityInfo, ListMultimap<DataFlowValue, JetType> typeInfo) { private DataFlowInfo(ImmutableMap<DataFlowValue, Nullability> nullabilityInfo, ListMultimap<DataFlowValue, JetType> typeInfo) {
@@ -185,7 +185,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
JetTypeReference right = expression.getRight(); JetTypeReference right = expression.getRight();
JetType result = null; JetType result = null;
if (right != null) { if (right != null) {
JetType targetType = context.getTypeResolver().resolveType(context.scope, right); JetType targetType = context.getTypeResolver().resolveType(context.scope, right, context.trace, true);
if (isTypeFlexible(expression.getLeft())) { if (isTypeFlexible(expression.getLeft())) {
TemporaryBindingTrace temporaryTraceWithExpectedType = TemporaryBindingTrace.create(context.trace); TemporaryBindingTrace temporaryTraceWithExpectedType = TemporaryBindingTrace.create(context.trace);
@@ -346,7 +346,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
List<JetExpression> entries = expression.getEntries(); List<JetExpression> entries = expression.getEntries();
List<JetType> types = new ArrayList<JetType>(); List<JetType> types = new ArrayList<JetType>();
for (JetExpression entry : entries) { for (JetExpression entry : entries) {
types.add(context.getServices().safeGetType(context.scope, entry, NO_EXPECTED_TYPE)); // TODO types.add(context.getServices().safeGetType(context.scope, entry, NO_EXPECTED_TYPE, context.trace)); // TODO
} }
if (context.expectedType != NO_EXPECTED_TYPE && JetStandardClasses.isTupleType(context.expectedType)) { if (context.expectedType != NO_EXPECTED_TYPE && JetStandardClasses.isTupleType(context.expectedType)) {
List<JetType> enrichedTypes = checkArgumentTypes(types, entries, context.expectedType.getArguments(), context); List<JetType> enrichedTypes = checkArgumentTypes(types, entries, context.expectedType.getArguments(), context);
@@ -417,15 +417,15 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
JetUserType userType = (JetUserType) typeElement; JetUserType userType = (JetUserType) typeElement;
// This may be just a superclass name even if the superclass is generic // This may be just a superclass name even if the superclass is generic
if (userType.getTypeArguments().isEmpty()) { if (userType.getTypeArguments().isEmpty()) {
classifierCandidate = context.getTypeResolver().resolveClass(context.scope, userType); classifierCandidate = context.getTypeResolver().resolveClass(context.scope, userType, context.trace);
} }
else { else {
supertype = context.getTypeResolver().resolveType(context.scope, superTypeQualifier); supertype = context.getTypeResolver().resolveType(context.scope, superTypeQualifier, context.trace, true);
redundantTypeArguments = userType.getTypeArgumentList(); redundantTypeArguments = userType.getTypeArgumentList();
} }
} }
else { else {
supertype = context.getTypeResolver().resolveType(context.scope, superTypeQualifier); supertype = context.getTypeResolver().resolveType(context.scope, superTypeQualifier, context.trace, true);
} }
if (supertype != null) { if (supertype != null) {
@@ -505,7 +505,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
} }
public JetType visitBlockExpression(JetBlockExpression expression, ExpressionTypingContext context, boolean isStatement) { public JetType visitBlockExpression(JetBlockExpression expression, ExpressionTypingContext context, boolean isStatement) {
return context.getServices().getBlockReturnedType(context.scope, expression, isStatement ? CoercionStrategy.COERCION_TO_UNIT : CoercionStrategy.NO_COERCION, context); return context.getServices().getBlockReturnedType(context.scope, expression, isStatement ? CoercionStrategy.COERCION_TO_UNIT : CoercionStrategy.NO_COERCION, context, context.trace);
} }
@Override @Override
@@ -99,16 +99,16 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(context.scope, functionDescriptor, context.trace); JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(context.scope, functionDescriptor, context.trace);
JetTypeReference returnTypeRef = functionLiteral.getReturnTypeRef(); JetTypeReference returnTypeRef = functionLiteral.getReturnTypeRef();
if (returnTypeRef != null) { if (returnTypeRef != null) {
returnType = context.getTypeResolver().resolveType(context.scope, returnTypeRef); returnType = context.getTypeResolver().resolveType(context.scope, returnTypeRef, context.trace, true);
context.getServices().checkFunctionReturnType(expression, context.replaceScope(functionInnerScope). context.getServices().checkFunctionReturnType(expression, context.replaceScope(functionInnerScope).
replaceExpectedType(returnType).replaceExpectedReturnType(returnType).replaceDataFlowInfo(context.dataFlowInfo)); replaceExpectedType(returnType).replaceExpectedReturnType(returnType).replaceDataFlowInfo(context.dataFlowInfo), context.trace);
} }
else { else {
if (functionTypeExpected) { if (functionTypeExpected) {
returnType = JetStandardClasses.getReturnTypeFromFunctionType(expectedType); returnType = JetStandardClasses.getReturnTypeFromFunctionType(expectedType);
} }
returnType = context.getServices().getBlockReturnedType(functionInnerScope, bodyExpression, CoercionStrategy.COERCION_TO_UNIT, returnType = context.getServices().getBlockReturnedType(functionInnerScope, bodyExpression, CoercionStrategy.COERCION_TO_UNIT,
context.replaceExpectedType(returnType).replaceExpectedReturnType(returnType)); context.replaceExpectedType(returnType).replaceExpectedReturnType(returnType), context.trace);
} }
JetType safeReturnType = returnType == null ? ErrorUtils.createErrorType("<return type>") : returnType; JetType safeReturnType = returnType == null ? ErrorUtils.createErrorType("<return type>") : returnType;
functionDescriptor.setReturnType(safeReturnType); functionDescriptor.setReturnType(safeReturnType);
@@ -143,7 +143,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
} }
} }
else { else {
effectiveReceiverType = context.getTypeResolver().resolveType(context.scope, receiverTypeRef); effectiveReceiverType = context.getTypeResolver().resolveType(context.scope, receiverTypeRef, context.trace, true);
} }
functionDescriptor.initialize(effectiveReceiverType, NO_RECEIVER, Collections.<TypeParameterDescriptor>emptyList(), valueParameterDescriptors, null, Modality.FINAL, Visibility.LOCAL); functionDescriptor.initialize(effectiveReceiverType, NO_RECEIVER, Collections.<TypeParameterDescriptor>emptyList(), valueParameterDescriptors, null, Modality.FINAL, Visibility.LOCAL);
context.trace.record(BindingContext.FUNCTION, expression, functionDescriptor); context.trace.record(BindingContext.FUNCTION, expression, functionDescriptor);
@@ -174,7 +174,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
JetType type; JetType type;
if (typeReference != null) { if (typeReference != null) {
type = context.getTypeResolver().resolveType(context.scope, typeReference); type = context.getTypeResolver().resolveType(context.scope, typeReference, context.trace, true);
} }
else { else {
if (expectedValueParameters != null && i < expectedValueParameters.size()) { if (expectedValueParameters != null && i < expectedValueParameters.size()) {
@@ -185,7 +185,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
type = ErrorUtils.createErrorType("Cannot be inferred"); type = ErrorUtils.createErrorType("Cannot be inferred");
} }
} }
ValueParameterDescriptor valueParameterDescriptor = context.getDescriptorResolver().resolveValueParameterDescriptor(functionDescriptor, declaredParameter, i, type); ValueParameterDescriptor valueParameterDescriptor = context.getDescriptorResolver().resolveValueParameterDescriptor(functionDescriptor, declaredParameter, i, type, context.trace);
valueParameterDescriptors.add(valueParameterDescriptor); valueParameterDescriptors.add(valueParameterDescriptor);
} }
} }
@@ -92,7 +92,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
if (elseBranch == null) { if (elseBranch == null) {
if (thenBranch != null) { if (thenBranch != null) {
JetType type = context.getServices().getBlockReturnedTypeWithWritableScope(thenScope, Collections.singletonList(thenBranch), CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(thenInfo)); JetType type = context.getServices().getBlockReturnedTypeWithWritableScope(thenScope, Collections.singletonList(thenBranch), CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(thenInfo), context.trace);
if (type != null && JetStandardClasses.isNothing(type)) { if (type != null && JetStandardClasses.isNothing(type)) {
facade.setResultingDataFlowInfo(elseInfo); facade.setResultingDataFlowInfo(elseInfo);
} }
@@ -101,15 +101,15 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
return null; return null;
} }
if (thenBranch == null) { if (thenBranch == null) {
JetType type = context.getServices().getBlockReturnedTypeWithWritableScope(elseScope, Collections.singletonList(elseBranch), CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(elseInfo)); JetType type = context.getServices().getBlockReturnedTypeWithWritableScope(elseScope, Collections.singletonList(elseBranch), CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(elseInfo), context.trace);
if (type != null && JetStandardClasses.isNothing(type)) { if (type != null && JetStandardClasses.isNothing(type)) {
facade.setResultingDataFlowInfo(thenInfo); facade.setResultingDataFlowInfo(thenInfo);
} }
return DataFlowUtils.checkImplicitCast(DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, contextWithExpectedType), expression, contextWithExpectedType, isStatement); return DataFlowUtils.checkImplicitCast(DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, contextWithExpectedType), expression, contextWithExpectedType, isStatement);
} }
CoercionStrategy coercionStrategy = isStatement ? CoercionStrategy.COERCION_TO_UNIT : CoercionStrategy.NO_COERCION; CoercionStrategy coercionStrategy = isStatement ? CoercionStrategy.COERCION_TO_UNIT : CoercionStrategy.NO_COERCION;
JetType thenType = context.getServices().getBlockReturnedTypeWithWritableScope(thenScope, Collections.singletonList(thenBranch), coercionStrategy, contextWithExpectedType.replaceDataFlowInfo(thenInfo)); JetType thenType = context.getServices().getBlockReturnedTypeWithWritableScope(thenScope, Collections.singletonList(thenBranch), coercionStrategy, contextWithExpectedType.replaceDataFlowInfo(thenInfo), context.trace);
JetType elseType = context.getServices().getBlockReturnedTypeWithWritableScope(elseScope, Collections.singletonList(elseBranch), coercionStrategy, contextWithExpectedType.replaceDataFlowInfo(elseInfo)); JetType elseType = context.getServices().getBlockReturnedTypeWithWritableScope(elseScope, Collections.singletonList(elseBranch), coercionStrategy, contextWithExpectedType.replaceDataFlowInfo(elseInfo), context.trace);
JetType result; JetType result;
if (thenType == null) { if (thenType == null) {
@@ -150,7 +150,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
if (body != null) { if (body != null) {
WritableScopeImpl scopeToExtend = newWritableScopeImpl(context).setDebugName("Scope extended in while's condition"); WritableScopeImpl scopeToExtend = newWritableScopeImpl(context).setDebugName("Scope extended in while's condition");
DataFlowInfo conditionInfo = condition == null ? context.dataFlowInfo : DataFlowUtils.extractDataFlowInfoFromCondition(condition, true, scopeToExtend, context); DataFlowInfo conditionInfo = condition == null ? context.dataFlowInfo : DataFlowUtils.extractDataFlowInfoFromCondition(condition, true, scopeToExtend, context);
context.getServices().getBlockReturnedTypeWithWritableScope(scopeToExtend, Collections.singletonList(body), CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(conditionInfo)); context.getServices().getBlockReturnedTypeWithWritableScope(scopeToExtend, Collections.singletonList(body), CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(conditionInfo), context.trace);
} }
if (!containsBreak(expression, context)) { if (!containsBreak(expression, context)) {
facade.setResultingDataFlowInfo(DataFlowUtils.extractDataFlowInfoFromCondition(condition, false, null, context)); facade.setResultingDataFlowInfo(DataFlowUtils.extractDataFlowInfoFromCondition(condition, false, null, context));
@@ -197,7 +197,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
if (!function.getFunctionLiteral().hasParameterSpecification()) { if (!function.getFunctionLiteral().hasParameterSpecification()) {
WritableScope writableScope = newWritableScopeImpl(context).setDebugName("do..while body scope"); WritableScope writableScope = newWritableScopeImpl(context).setDebugName("do..while body scope");
conditionScope = writableScope; conditionScope = writableScope;
context.getServices().getBlockReturnedTypeWithWritableScope(writableScope, function.getFunctionLiteral().getBodyExpression().getStatements(), CoercionStrategy.NO_COERCION, context); context.getServices().getBlockReturnedTypeWithWritableScope(writableScope, function.getFunctionLiteral().getBodyExpression().getStatements(), CoercionStrategy.NO_COERCION, context, context.trace);
context.trace.record(BindingContext.BLOCK, function); context.trace.record(BindingContext.BLOCK, function);
} else { } else {
facade.getType(body, context.replaceScope(context.scope)); facade.getType(body, context.replaceScope(context.scope));
@@ -213,7 +213,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
else { else {
block = Collections.<JetElement>singletonList(body); block = Collections.<JetElement>singletonList(body);
} }
context.getServices().getBlockReturnedTypeWithWritableScope(writableScope, block, CoercionStrategy.NO_COERCION, context); context.getServices().getBlockReturnedTypeWithWritableScope(writableScope, block, CoercionStrategy.NO_COERCION, context, context.trace);
} }
JetExpression condition = expression.getCondition(); JetExpression condition = expression.getCondition();
checkCondition(conditionScope, condition, context); checkCondition(conditionScope, condition, context);
@@ -248,7 +248,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
JetTypeReference typeReference = loopParameter.getTypeReference(); JetTypeReference typeReference = loopParameter.getTypeReference();
VariableDescriptor variableDescriptor; VariableDescriptor variableDescriptor;
if (typeReference != null) { if (typeReference != null) {
variableDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptor(context.scope.getContainingDeclaration(), context.scope, loopParameter); variableDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptor(context.scope.getContainingDeclaration(), context.scope, loopParameter, context.trace);
JetType actualParameterType = variableDescriptor.getType(); JetType actualParameterType = variableDescriptor.getType();
if (expectedParameterType != null && if (expectedParameterType != null &&
actualParameterType != null && actualParameterType != null &&
@@ -260,7 +260,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
if (expectedParameterType == null) { if (expectedParameterType == null) {
expectedParameterType = ErrorUtils.createErrorType("Error"); expectedParameterType = ErrorUtils.createErrorType("Error");
} }
variableDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptor(context.scope.getContainingDeclaration(), loopParameter, expectedParameterType); variableDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptor(context.scope.getContainingDeclaration(), loopParameter, expectedParameterType, context.trace);
} }
{ {
@@ -277,7 +277,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
JetExpression body = expression.getBody(); JetExpression body = expression.getBody();
if (body != null) { if (body != null) {
context.getServices().getBlockReturnedTypeWithWritableScope(loopScope, Collections.singletonList(body), CoercionStrategy.NO_COERCION, context); context.getServices().getBlockReturnedTypeWithWritableScope(loopScope, Collections.singletonList(body), CoercionStrategy.NO_COERCION, context, context.trace);
} }
return DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, contextWithExpectedType); return DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, contextWithExpectedType);
@@ -401,7 +401,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
JetParameter catchParameter = catchClause.getCatchParameter(); JetParameter catchParameter = catchClause.getCatchParameter();
JetExpression catchBody = catchClause.getCatchBody(); JetExpression catchBody = catchClause.getCatchBody();
if (catchParameter != null) { if (catchParameter != null) {
VariableDescriptor variableDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptor(context.scope.getContainingDeclaration(), context.scope, catchParameter); VariableDescriptor variableDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptor(context.scope.getContainingDeclaration(), context.scope, catchParameter, context.trace);
JetType throwableType = context.semanticServices.getStandardLibrary().getThrowable().getDefaultType(); JetType throwableType = context.semanticServices.getStandardLibrary().getThrowable().getDefaultType();
DataFlowUtils.checkType(variableDescriptor.getType(), catchParameter, context.replaceExpectedType(throwableType)); DataFlowUtils.checkType(variableDescriptor.getType(), catchParameter, context.replaceExpectedType(throwableType));
if (catchBody != null) { if (catchBody != null) {
@@ -44,8 +44,10 @@ import java.util.Map;
* @author abreslav * @author abreslav
*/ */
/*package*/ class ExpressionTypingContext { /*package*/ class ExpressionTypingContext {
@NotNull @NotNull
public static ExpressionTypingContext newContext( public static ExpressionTypingContext newContext(
@NotNull CallResolver.Context context,
@NotNull Project project, @NotNull Project project,
@NotNull JetSemanticServices semanticServices, @NotNull JetSemanticServices semanticServices,
@NotNull Map<JetPattern, DataFlowInfo> patternsToDataFlowInfo, @NotNull Map<JetPattern, DataFlowInfo> patternsToDataFlowInfo,
@@ -57,7 +59,7 @@ import java.util.Map;
@NotNull JetType expectedType, @NotNull JetType expectedType,
@NotNull JetType expectedReturnType, @NotNull JetType expectedReturnType,
boolean namespacesAllowed) { boolean namespacesAllowed) {
return new ExpressionTypingContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, return new ExpressionTypingContext(project, context, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists,
labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed); labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
} }
@@ -73,6 +75,7 @@ import java.util.Map;
// } // }
// //
public final Project project; public final Project project;
public final CallResolver.Context context;
public final JetSemanticServices semanticServices; public final JetSemanticServices semanticServices;
public final BindingTrace trace; public final BindingTrace trace;
public final JetScope scope; public final JetScope scope;
@@ -89,13 +92,11 @@ import java.util.Map;
public final boolean namespacesAllowed; public final boolean namespacesAllowed;
private CallResolver callResolver; private CallResolver callResolver;
private TypeResolver typeResolver;
private DescriptorResolver descriptorResolver;
private ExpressionTypingServices services;
private CompileTimeConstantResolver compileTimeConstantResolver; private CompileTimeConstantResolver compileTimeConstantResolver;
private ExpressionTypingContext( private ExpressionTypingContext(
@NotNull Project project, @NotNull Project project,
@NotNull CallResolver.Context context,
@NotNull JetSemanticServices semanticServices, @NotNull JetSemanticServices semanticServices,
@NotNull Map<JetPattern, DataFlowInfo> patternsToDataFlowInfo, @NotNull Map<JetPattern, DataFlowInfo> patternsToDataFlowInfo,
@NotNull Map<JetPattern, List<VariableDescriptor>> patternsToBoundVariableLists, @NotNull Map<JetPattern, List<VariableDescriptor>> patternsToBoundVariableLists,
@@ -107,6 +108,7 @@ import java.util.Map;
@NotNull JetType expectedReturnType, @NotNull JetType expectedReturnType,
boolean namespacesAllowed) { boolean namespacesAllowed) {
this.project = project; this.project = project;
this.context = context;
this.trace = trace; this.trace = trace;
this.patternsToBoundVariableLists = patternsToBoundVariableLists; this.patternsToBoundVariableLists = patternsToBoundVariableLists;
this.patternsToDataFlowInfo = patternsToDataFlowInfo; this.patternsToDataFlowInfo = patternsToDataFlowInfo;
@@ -122,66 +124,57 @@ import java.util.Map;
@NotNull @NotNull
public ExpressionTypingContext replaceNamespacesAllowed(boolean namespacesAllowed) { public ExpressionTypingContext replaceNamespacesAllowed(boolean namespacesAllowed) {
if (namespacesAllowed == this.namespacesAllowed) return this; if (namespacesAllowed == this.namespacesAllowed) return this;
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed); return newContext(context, project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
} }
@NotNull @NotNull
public ExpressionTypingContext replaceDataFlowInfo(DataFlowInfo newDataFlowInfo) { public ExpressionTypingContext replaceDataFlowInfo(DataFlowInfo newDataFlowInfo) {
if (newDataFlowInfo == dataFlowInfo) return this; if (newDataFlowInfo == dataFlowInfo) return this;
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, newDataFlowInfo, expectedType, expectedReturnType, namespacesAllowed); return newContext(context, project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, newDataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
} }
public ExpressionTypingContext replaceExpectedType(@Nullable JetType newExpectedType) { public ExpressionTypingContext replaceExpectedType(@Nullable JetType newExpectedType) {
if (newExpectedType == null) return replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE); if (newExpectedType == null) return replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE);
if (expectedType == newExpectedType) return this; if (expectedType == newExpectedType) return this;
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, newExpectedType, expectedReturnType, namespacesAllowed); return newContext(context, project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, newExpectedType, expectedReturnType, namespacesAllowed);
} }
public ExpressionTypingContext replaceExpectedReturnType(@Nullable JetType newExpectedReturnType) { public ExpressionTypingContext replaceExpectedReturnType(@Nullable JetType newExpectedReturnType) {
if (newExpectedReturnType == null) return replaceExpectedReturnType(TypeUtils.NO_EXPECTED_TYPE); if (newExpectedReturnType == null) return replaceExpectedReturnType(TypeUtils.NO_EXPECTED_TYPE);
if (expectedReturnType == newExpectedReturnType) return this; if (expectedReturnType == newExpectedReturnType) return this;
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, expectedType, newExpectedReturnType, namespacesAllowed); return newContext(context, project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, expectedType, newExpectedReturnType, namespacesAllowed);
} }
public ExpressionTypingContext replaceBindingTrace(@NotNull BindingTrace newTrace) { public ExpressionTypingContext replaceBindingTrace(@NotNull BindingTrace newTrace) {
if (newTrace == trace) return this; if (newTrace == trace) return this;
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, newTrace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed); return newContext(context, project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, newTrace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
} }
@NotNull @NotNull
public ExpressionTypingContext replaceScope(@NotNull JetScope newScope) { public ExpressionTypingContext replaceScope(@NotNull JetScope newScope) {
if (newScope == scope) return this; if (newScope == scope) return this;
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, newScope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed); return newContext(context, project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, newScope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
} }
///////////// LAZY ACCESSORS ///////////// LAZY ACCESSORS
public CallResolver getCallResolver() { public CallResolver getCallResolver() {
if (callResolver == null) { if (callResolver == null) {
callResolver = new CallResolver(semanticServices, dataFlowInfo); callResolver = new CallResolver(context, dataFlowInfo);
} }
return callResolver; return callResolver;
} }
public ExpressionTypingServices getServices() { public ExpressionTypingServices getServices() {
if (services == null) { return context.expressionTypingServices;
services = new ExpressionTypingServices(semanticServices, trace);
}
return services;
} }
public TypeResolver getTypeResolver() { public TypeResolver getTypeResolver() {
if (typeResolver == null) { return context.typeResolver;
typeResolver = new TypeResolver(semanticServices, trace, true);
}
return typeResolver;
} }
public DescriptorResolver getDescriptorResolver() { public DescriptorResolver getDescriptorResolver() {
if (descriptorResolver == null) { return context.descriptorResolver;
descriptorResolver = semanticServices.getClassDescriptorResolver(trace);
}
return descriptorResolver;
} }
public CompileTimeConstantResolver getCompileTimeConstantResolver() { public CompileTimeConstantResolver getCompileTimeConstantResolver() {
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.*; import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo; import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope; import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
@@ -39,6 +40,7 @@ import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
import javax.inject.Inject;
import java.util.*; import java.util.*;
import static org.jetbrains.jet.lang.diagnostics.Errors.TYPE_MISMATCH; import static org.jetbrains.jet.lang.diagnostics.Errors.TYPE_MISMATCH;
@@ -51,23 +53,28 @@ import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE;
* @author abreslav * @author abreslav
*/ */
public class ExpressionTypingServices { public class ExpressionTypingServices {
private final JetSemanticServices semanticServices; private JetSemanticServices semanticServices;
private final BindingTrace trace; private CallResolver.Context expressionTypingContextContext;
private final ExpressionTypingFacade expressionTypingFacade = ExpressionTypingVisitorDispatcher.create(); private final ExpressionTypingFacade expressionTypingFacade = ExpressionTypingVisitorDispatcher.create();
public ExpressionTypingServices(JetSemanticServices semanticServices, BindingTrace trace) { @Inject
public void setSemanticServices(JetSemanticServices semanticServices) {
this.semanticServices = semanticServices; this.semanticServices = semanticServices;
this.trace = trace; }
@Inject
public void setExpressionTypingContextContext(CallResolver.Context expressionTypingContextContext) {
this.expressionTypingContextContext = expressionTypingContextContext;
} }
@NotNull @NotNull
public JetType safeGetType(@NotNull JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType) { public JetType safeGetType(@NotNull JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, BindingTrace trace) {
return safeGetType(scope, expression, expectedType, DataFlowInfo.EMPTY); return safeGetType(scope, expression, expectedType, DataFlowInfo.EMPTY, trace);
} }
public JetType safeGetType(@NotNull JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull DataFlowInfo dataFlowInfo) { public JetType safeGetType(@NotNull JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull DataFlowInfo dataFlowInfo, BindingTrace trace) {
JetType type = getType(scope, expression, expectedType, dataFlowInfo); JetType type = getType(scope, expression, expectedType, dataFlowInfo, trace);
if (type != null) { if (type != null) {
return type; return type;
} }
@@ -75,13 +82,14 @@ public class ExpressionTypingServices {
} }
@Nullable @Nullable
public JetType getType(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType) { public JetType getType(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, BindingTrace trace) {
return getType(scope, expression, expectedType, DataFlowInfo.EMPTY); return getType(scope, expression, expectedType, DataFlowInfo.EMPTY, trace);
} }
@Nullable @Nullable
public JetType getType(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull DataFlowInfo dataFlowInfo) { public JetType getType(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull DataFlowInfo dataFlowInfo, BindingTrace trace) {
ExpressionTypingContext context = ExpressionTypingContext.newContext( ExpressionTypingContext context = ExpressionTypingContext.newContext(
expressionTypingContextContext,
expression.getProject(), expression.getProject(),
semanticServices, semanticServices,
new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(), new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
@@ -90,8 +98,9 @@ public class ExpressionTypingServices {
return expressionTypingFacade.getType(expression, context); return expressionTypingFacade.getType(expression, context);
} }
public JetType getTypeWithNamespaces(@NotNull final JetScope scope, @NotNull JetExpression expression) { public JetType getTypeWithNamespaces(@NotNull final JetScope scope, @NotNull JetExpression expression, BindingTrace trace) {
ExpressionTypingContext context = ExpressionTypingContext.newContext( ExpressionTypingContext context = ExpressionTypingContext.newContext(
expressionTypingContextContext,
expression.getProject(), expression.getProject(),
semanticServices, semanticServices,
new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(), new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
@@ -102,7 +111,7 @@ public class ExpressionTypingServices {
} }
@NotNull @NotNull
public JetType inferFunctionReturnType(@NotNull JetScope outerScope, JetDeclarationWithBody function, FunctionDescriptor functionDescriptor) { public JetType inferFunctionReturnType(@NotNull JetScope outerScope, JetDeclarationWithBody function, FunctionDescriptor functionDescriptor, BindingTrace trace) {
Map<JetExpression, JetType> typeMap = collectReturnedExpressionsWithTypes(trace, outerScope, function, functionDescriptor); Map<JetExpression, JetType> typeMap = collectReturnedExpressionsWithTypes(trace, outerScope, function, functionDescriptor);
Collection<JetType> types = typeMap.values(); Collection<JetType> types = typeMap.values();
return types.isEmpty() return types.isEmpty()
@@ -111,20 +120,20 @@ public class ExpressionTypingServices {
} }
public void checkFunctionReturnType(@NotNull JetScope functionInnerScope, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor) { public void checkFunctionReturnType(@NotNull JetScope functionInnerScope, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, BindingTrace trace) {
checkFunctionReturnType(functionInnerScope, function, functionDescriptor, DataFlowInfo.EMPTY, null); checkFunctionReturnType(functionInnerScope, function, functionDescriptor, DataFlowInfo.EMPTY, null, trace);
} }
public void checkFunctionReturnType(@NotNull JetScope functionInnerScope, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @Nullable JetType expectedReturnType) { public void checkFunctionReturnType(@NotNull JetScope functionInnerScope, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @Nullable JetType expectedReturnType, BindingTrace trace) {
checkFunctionReturnType(functionInnerScope, function, functionDescriptor, DataFlowInfo.EMPTY, expectedReturnType); checkFunctionReturnType(functionInnerScope, function, functionDescriptor, DataFlowInfo.EMPTY, expectedReturnType, trace);
} }
///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////
/*package*/ void checkFunctionReturnType(@NotNull JetScope functionInnerScope, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @NotNull DataFlowInfo dataFlowInfo) { /*package*/ void checkFunctionReturnType(@NotNull JetScope functionInnerScope, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @NotNull DataFlowInfo dataFlowInfo, BindingTrace trace) {
checkFunctionReturnType(functionInnerScope, function, functionDescriptor, dataFlowInfo, null); checkFunctionReturnType(functionInnerScope, function, functionDescriptor, dataFlowInfo, null, trace);
} }
/*package*/ void checkFunctionReturnType(@NotNull JetScope functionInnerScope, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @NotNull DataFlowInfo dataFlowInfo, @Nullable JetType expectedReturnType) { /*package*/ void checkFunctionReturnType(@NotNull JetScope functionInnerScope, @NotNull JetDeclarationWithBody function, @NotNull FunctionDescriptor functionDescriptor, @NotNull DataFlowInfo dataFlowInfo, @Nullable JetType expectedReturnType, BindingTrace trace) {
if (expectedReturnType == null) { if (expectedReturnType == null) {
expectedReturnType = functionDescriptor.getReturnType(); expectedReturnType = functionDescriptor.getReturnType();
if (!function.hasBlockBody() && !function.hasDeclaredReturnType()) { if (!function.hasBlockBody() && !function.hasDeclaredReturnType()) {
@@ -132,13 +141,14 @@ public class ExpressionTypingServices {
} }
} }
checkFunctionReturnType(function, ExpressionTypingContext.newContext( checkFunctionReturnType(function, ExpressionTypingContext.newContext(
expressionTypingContextContext,
function.getProject(), function.getProject(),
semanticServices, new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(), semanticServices, new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, functionInnerScope, dataFlowInfo, NO_EXPECTED_TYPE, expectedReturnType, false trace, functionInnerScope, dataFlowInfo, NO_EXPECTED_TYPE, expectedReturnType, false
)); ), trace);
} }
/*package*/ void checkFunctionReturnType(JetDeclarationWithBody function, ExpressionTypingContext context) { /*package*/ void checkFunctionReturnType(JetDeclarationWithBody function, ExpressionTypingContext context, BindingTrace trace) {
JetExpression bodyExpression = function.getBodyExpression(); JetExpression bodyExpression = function.getBodyExpression();
if (bodyExpression == null) return; if (bodyExpression == null) return;
@@ -152,7 +162,7 @@ public class ExpressionTypingServices {
JetFunctionLiteralExpression functionLiteralExpression = (JetFunctionLiteralExpression) function; JetFunctionLiteralExpression functionLiteralExpression = (JetFunctionLiteralExpression) function;
JetBlockExpression blockExpression = functionLiteralExpression.getBodyExpression(); JetBlockExpression blockExpression = functionLiteralExpression.getBodyExpression();
assert blockExpression != null; assert blockExpression != null;
getBlockReturnedType(newContext.scope, blockExpression, CoercionStrategy.COERCION_TO_UNIT, context); getBlockReturnedType(newContext.scope, blockExpression, CoercionStrategy.COERCION_TO_UNIT, context, trace);
} }
else { else {
expressionTypingFacade.getType(bodyExpression, newContext, !blockBody); expressionTypingFacade.getType(bodyExpression, newContext, !blockBody);
@@ -160,7 +170,7 @@ public class ExpressionTypingServices {
} }
@Nullable @Nullable
/*package*/ JetType getBlockReturnedType(@NotNull JetScope outerScope, @NotNull JetBlockExpression expression, @NotNull CoercionStrategy coercionStrategyForLastExpression, ExpressionTypingContext context) { /*package*/ JetType getBlockReturnedType(@NotNull JetScope outerScope, @NotNull JetBlockExpression expression, @NotNull CoercionStrategy coercionStrategyForLastExpression, ExpressionTypingContext context, BindingTrace trace) {
List<JetElement> block = expression.getStatements(); List<JetElement> block = expression.getStatements();
if (block.isEmpty()) { if (block.isEmpty()) {
return DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, context); return DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, context);
@@ -169,7 +179,7 @@ public class ExpressionTypingServices {
DeclarationDescriptor containingDescriptor = outerScope.getContainingDeclaration(); DeclarationDescriptor containingDescriptor = outerScope.getContainingDeclaration();
WritableScope scope = new WritableScopeImpl(outerScope, containingDescriptor, new TraceBasedRedeclarationHandler(context.trace)).setDebugName("getBlockReturnedType"); WritableScope scope = new WritableScopeImpl(outerScope, containingDescriptor, new TraceBasedRedeclarationHandler(context.trace)).setDebugName("getBlockReturnedType");
scope.changeLockLevel(WritableScope.LockLevel.BOTH); scope.changeLockLevel(WritableScope.LockLevel.BOTH);
return getBlockReturnedTypeWithWritableScope(scope, block, coercionStrategyForLastExpression, context); return getBlockReturnedTypeWithWritableScope(scope, block, coercionStrategyForLastExpression, context, trace);
} }
private Map<JetExpression, JetType> collectReturnedExpressionsWithTypes( private Map<JetExpression, JetType> collectReturnedExpressionsWithTypes(
@@ -180,8 +190,10 @@ public class ExpressionTypingServices {
JetExpression bodyExpression = function.getBodyExpression(); JetExpression bodyExpression = function.getBodyExpression();
assert bodyExpression != null; assert bodyExpression != null;
JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(outerScope, functionDescriptor, trace); JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(outerScope, functionDescriptor, trace);
expressionTypingFacade.getType(bodyExpression, ExpressionTypingContext.newContext(function.getProject(), semanticServices, new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(), expressionTypingFacade.getType(bodyExpression, ExpressionTypingContext.newContext(
trace, functionInnerScope, DataFlowInfo.EMPTY, NO_EXPECTED_TYPE, FORBIDDEN, false), !function.hasBlockBody()); expressionTypingContextContext,
function.getProject(), semanticServices, new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, functionInnerScope, DataFlowInfo.EMPTY, NO_EXPECTED_TYPE, FORBIDDEN, false), !function.hasBlockBody());
//todo function literals //todo function literals
final Collection<JetExpression> returnedExpressions = Lists.newArrayList(); final Collection<JetExpression> returnedExpressions = Lists.newArrayList();
if (function.hasBlockBody()) { if (function.hasBlockBody()) {
@@ -225,7 +237,7 @@ public class ExpressionTypingServices {
return typeMap; return typeMap;
} }
/*package*/ JetType getBlockReturnedTypeWithWritableScope(@NotNull WritableScope scope, @NotNull List<? extends JetElement> block, @NotNull CoercionStrategy coercionStrategyForLastExpression, ExpressionTypingContext context) { /*package*/ JetType getBlockReturnedTypeWithWritableScope(@NotNull WritableScope scope, @NotNull List<? extends JetElement> block, @NotNull CoercionStrategy coercionStrategyForLastExpression, ExpressionTypingContext context, BindingTrace trace) {
if (block.isEmpty()) { if (block.isEmpty()) {
return JetStandardClasses.getUnitType(); return JetStandardClasses.getUnitType();
} }
@@ -309,7 +321,8 @@ public class ExpressionTypingServices {
} }
private ExpressionTypingContext createContext(ExpressionTypingContext oldContext, BindingTrace trace, WritableScope scope, DataFlowInfo dataFlowInfo, JetType expectedType, JetType expectedReturnType) { private ExpressionTypingContext createContext(ExpressionTypingContext oldContext, BindingTrace trace, WritableScope scope, DataFlowInfo dataFlowInfo, JetType expectedType, JetType expectedReturnType) {
return ExpressionTypingContext.newContext(oldContext.project, oldContext.semanticServices, oldContext.patternsToDataFlowInfo, oldContext.patternsToBoundVariableLists, oldContext.labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, oldContext.namespacesAllowed); return ExpressionTypingContext.newContext(
expressionTypingContextContext, oldContext.project, oldContext.semanticServices, oldContext.patternsToDataFlowInfo, oldContext.patternsToBoundVariableLists, oldContext.labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, oldContext.namespacesAllowed);
} }
private ObservableBindingTrace makeTraceInterceptingTypeMismatch(final BindingTrace trace, final JetExpression expressionToWatch, final boolean[] mismatchFound) { private ObservableBindingTrace makeTraceInterceptingTypeMismatch(final BindingTrace trace, final JetExpression expressionToWatch, final boolean[] mismatchFound) {
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.*; import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo; import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.calls.inference.*; import org.jetbrains.jet.lang.resolve.calls.inference.*;
import org.jetbrains.jet.lang.resolve.scopes.*; import org.jetbrains.jet.lang.resolve.scopes.*;
@@ -148,10 +149,12 @@ public class ExpressionTypingUtils {
return expression; return expression;
} }
public static boolean isVariableIterable(@NotNull Project project, @NotNull VariableDescriptor variableDescriptor, @NotNull JetScope scope) { public static boolean isVariableIterable(@NotNull CallResolver.Context expressionTypingContextContext,
@NotNull Project project, @NotNull VariableDescriptor variableDescriptor, @NotNull JetScope scope) {
JetExpression expression = JetPsiFactory.createExpression(project, "fake"); JetExpression expression = JetPsiFactory.createExpression(project, "fake");
ExpressionReceiver expressionReceiver = new ExpressionReceiver(expression, variableDescriptor.getType()); ExpressionReceiver expressionReceiver = new ExpressionReceiver(expression, variableDescriptor.getType());
ExpressionTypingContext context = ExpressionTypingContext.newContext( ExpressionTypingContext context = ExpressionTypingContext.newContext(
expressionTypingContextContext,
project, project,
JetSemanticServices.createSemanticServices(project), JetSemanticServices.createSemanticServices(project),
new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, DataFlowInfo>(),
@@ -83,7 +83,7 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
ClassDescriptor classDescriptor = context.trace.getBindingContext().get(BindingContext.CLASS, declaration); ClassDescriptor classDescriptor = context.trace.getBindingContext().get(BindingContext.CLASS, declaration);
if (classDescriptor != null) { if (classDescriptor != null) {
VariableDescriptor variableDescriptor = context.getDescriptorResolver() VariableDescriptor variableDescriptor = context.getDescriptorResolver()
.resolveObjectDeclaration(scope.getContainingDeclaration(), declaration, classDescriptor); .resolveObjectDeclaration(scope.getContainingDeclaration(), declaration, classDescriptor, context.trace);
scope.addVariableDescriptor(variableDescriptor); scope.addVariableDescriptor(variableDescriptor);
} }
return DataFlowUtils.checkStatementType(declaration, context); return DataFlowUtils.checkStatementType(declaration, context);
@@ -106,7 +106,7 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
context.trace.report(LOCAL_VARIABLE_WITH_SETTER.on(setter)); context.trace.report(LOCAL_VARIABLE_WITH_SETTER.on(setter));
} }
VariableDescriptor propertyDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptor(scope.getContainingDeclaration(), scope, property, context.dataFlowInfo); VariableDescriptor propertyDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptor(scope.getContainingDeclaration(), scope, property, context.dataFlowInfo, context.trace);
JetExpression initializer = property.getInitializer(); JetExpression initializer = property.getInitializer();
if (property.getPropertyTypeRef() != null && initializer != null) { if (property.getPropertyTypeRef() != null && initializer != null) {
JetType outType = propertyDescriptor.getType(); JetType outType = propertyDescriptor.getType();
@@ -126,10 +126,10 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
@Override @Override
public JetType visitNamedFunction(JetNamedFunction function, ExpressionTypingContext context) { public JetType visitNamedFunction(JetNamedFunction function, ExpressionTypingContext context) {
SimpleFunctionDescriptor functionDescriptor = context.getDescriptorResolver().resolveFunctionDescriptor(scope.getContainingDeclaration(), scope, function); SimpleFunctionDescriptor functionDescriptor = context.getDescriptorResolver().resolveFunctionDescriptor(scope.getContainingDeclaration(), scope, function, context.trace);
scope.addFunctionDescriptor(functionDescriptor); scope.addFunctionDescriptor(functionDescriptor);
JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(context.scope, functionDescriptor, context.trace); JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(context.scope, functionDescriptor, context.trace);
context.getServices().checkFunctionReturnType(functionInnerScope, function, functionDescriptor, context.dataFlowInfo); context.getServices().checkFunctionReturnType(functionInnerScope, function, functionDescriptor, context.dataFlowInfo, context.trace);
return DataFlowUtils.checkStatementType(function, context); return DataFlowUtils.checkStatementType(function, context);
} }
@@ -74,7 +74,7 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
// TODO :change scope according to the bound value in the when header // TODO :change scope according to the bound value in the when header
final JetExpression subjectExpression = expression.getSubjectExpression(); final JetExpression subjectExpression = expression.getSubjectExpression();
final JetType subjectType = subjectExpression != null ? context.getServices().safeGetType(context.scope, subjectExpression, TypeUtils.NO_EXPECTED_TYPE) : ErrorUtils.createErrorType("Unknown type"); final JetType subjectType = subjectExpression != null ? context.getServices().safeGetType(context.scope, subjectExpression, TypeUtils.NO_EXPECTED_TYPE, context.trace) : ErrorUtils.createErrorType("Unknown type");
final DataFlowValue variableDescriptor = subjectExpression != null ? DataFlowValueFactory.INSTANCE.createDataFlowValue(subjectExpression, subjectType, context.trace.getBindingContext()) : DataFlowValue.NULL; final DataFlowValue variableDescriptor = subjectExpression != null ? DataFlowValueFactory.INSTANCE.createDataFlowValue(subjectExpression, subjectType, context.trace.getBindingContext()) : DataFlowValue.NULL;
// TODO : exhaustive patterns // TODO : exhaustive patterns
@@ -115,7 +115,7 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
if (bodyExpression != null) { if (bodyExpression != null) {
ExpressionTypingContext newContext = contextWithExpectedType.replaceScope(scopeToExtend).replaceDataFlowInfo(newDataFlowInfo); ExpressionTypingContext newContext = contextWithExpectedType.replaceScope(scopeToExtend).replaceDataFlowInfo(newDataFlowInfo);
CoercionStrategy coercionStrategy = isStatement ? CoercionStrategy.COERCION_TO_UNIT : CoercionStrategy.NO_COERCION; CoercionStrategy coercionStrategy = isStatement ? CoercionStrategy.COERCION_TO_UNIT : CoercionStrategy.NO_COERCION;
JetType type = context.getServices().getBlockReturnedTypeWithWritableScope(scopeToExtend, Collections.singletonList(bodyExpression), coercionStrategy, newContext); JetType type = context.getServices().getBlockReturnedTypeWithWritableScope(scopeToExtend, Collections.singletonList(bodyExpression), coercionStrategy, newContext, context.trace);
if (type != null) { if (type != null) {
expressionTypes.add(type); expressionTypes.add(type);
} }
@@ -180,7 +180,7 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
public void visitTypePattern(JetTypePattern typePattern) { public void visitTypePattern(JetTypePattern typePattern) {
JetTypeReference typeReference = typePattern.getTypeReference(); JetTypeReference typeReference = typePattern.getTypeReference();
if (typeReference == null) return; if (typeReference == null) return;
JetType type = context.getTypeResolver().resolveType(context.scope, typeReference); JetType type = context.getTypeResolver().resolveType(context.scope, typeReference, context.trace, true);
checkTypeCompatibility(type, subjectType, typePattern); checkTypeCompatibility(type, subjectType, typePattern);
result.set(context.dataFlowInfo.establishSubtyping(subjectVariables, type)); result.set(context.dataFlowInfo.establishSubtyping(subjectVariables, type));
} }
@@ -251,8 +251,8 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
public void visitBindingPattern(JetBindingPattern pattern) { public void visitBindingPattern(JetBindingPattern pattern) {
JetProperty variableDeclaration = pattern.getVariableDeclaration(); JetProperty variableDeclaration = pattern.getVariableDeclaration();
JetTypeReference propertyTypeRef = variableDeclaration.getPropertyTypeRef(); JetTypeReference propertyTypeRef = variableDeclaration.getPropertyTypeRef();
JetType type = propertyTypeRef == null ? subjectType : context.getTypeResolver().resolveType(context.scope, propertyTypeRef); JetType type = propertyTypeRef == null ? subjectType : context.getTypeResolver().resolveType(context.scope, propertyTypeRef, context.trace, true);
VariableDescriptor variableDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptorWithType(context.scope.getContainingDeclaration(), variableDeclaration, type); VariableDescriptor variableDescriptor = context.getDescriptorResolver().resolveLocalVariableDescriptorWithType(context.scope.getContainingDeclaration(), variableDeclaration, type, context.trace);
scopeToExtend.addVariableDescriptor(variableDescriptor); scopeToExtend.addVariableDescriptor(variableDescriptor);
if (propertyTypeRef != null) { if (propertyTypeRef != null) {
if (!context.semanticServices.getTypeChecker().isSubtypeOf(subjectType, type)) { if (!context.semanticServices.getTypeChecker().isSubtypeOf(subjectType, type)) {
@@ -16,15 +16,19 @@
package org.jetbrains.jet.resolve; package org.jetbrains.jet.resolve;
import com.google.common.base.Predicates;
import com.intellij.openapi.application.PathManager; import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.psi.JavaPsiFacade; import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass; import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiMethod;
import junit.framework.Test; import junit.framework.Test;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestCaseBuilder; import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
@@ -32,6 +36,7 @@ import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingTraceContext; import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.TopDownAnalysisContext;
import org.jetbrains.jet.lang.resolve.calls.CallResolver; import org.jetbrains.jet.lang.resolve.calls.CallResolver;
import org.jetbrains.jet.lang.resolve.calls.OverloadResolutionResults; import org.jetbrains.jet.lang.resolve.calls.OverloadResolutionResults;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall; import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
@@ -135,7 +140,10 @@ public class JetResolveTest extends ExtensibleResolveTestCase {
List<JetType> parameterTypeList = Arrays.asList(parameterType); List<JetType> parameterTypeList = Arrays.asList(parameterType);
// JetTypeInferrer.Services typeInferrerServices = JetSemanticServices.createSemanticServices(getProject()).getTypeInferrerServices(new BindingTraceContext()); // JetTypeInferrer.Services typeInferrerServices = JetSemanticServices.createSemanticServices(getProject()).getTypeInferrerServices(new BindingTraceContext());
CallResolver callResolver = new CallResolver(JetSemanticServices.createSemanticServices(getProject()), DataFlowInfo.EMPTY); JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(getProject());
TopDownAnalysisContext analysisContext = new TopDownAnalysisContext(semanticServices, JetTestUtils.DUMMY_EXCEPTION_ON_ERROR_TRACE, Predicates.<PsiFile>alwaysTrue(), Configuration.EMPTY, false);
CallResolver callResolver = new CallResolver(analysisContext.getCallResolverContext(), DataFlowInfo.EMPTY);
OverloadResolutionResults<FunctionDescriptor> functions = callResolver.resolveExactSignature( OverloadResolutionResults<FunctionDescriptor> functions = callResolver.resolveExactSignature(
classDescriptor.getMemberScope(typeArguments), ReceiverDescriptor.NO_RECEIVER, name, parameterTypeList); classDescriptor.getMemberScope(typeArguments), ReceiverDescriptor.NO_RECEIVER, name, parameterTypeList);
for (ResolvedCall<? extends FunctionDescriptor> resolvedCall : functions.getResultingCalls()) { for (ResolvedCall<? extends FunctionDescriptor> resolvedCall : functions.getResultingCalls()) {
@@ -16,13 +16,17 @@
package org.jetbrains.jet.types; package org.jetbrains.jet.types;
import com.google.common.base.Predicates;
import com.intellij.psi.PsiFile;
import org.jetbrains.jet.JetLiteFixture; import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorResolver; import org.jetbrains.jet.lang.resolve.DescriptorResolver;
import org.jetbrains.jet.lang.resolve.TopDownAnalysisContext;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler; import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
@@ -52,7 +56,8 @@ public class JetDefaultModalityModifiersTest extends JetLiteFixture {
public void setUp() throws Exception { public void setUp() throws Exception {
JetStandardLibrary library = JetStandardLibrary.getInstance(); JetStandardLibrary library = JetStandardLibrary.getInstance();
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(library); JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(library);
descriptorResolver = semanticServices.getClassDescriptorResolver(JetTestUtils.DUMMY_EXCEPTION_ON_ERROR_TRACE); TopDownAnalysisContext analysisContext = new TopDownAnalysisContext(semanticServices, JetTestUtils.DUMMY_EXCEPTION_ON_ERROR_TRACE, Predicates.<PsiFile>alwaysTrue(), Configuration.EMPTY, false);
descriptorResolver = analysisContext.getDescriptorResolver();
scope = createScope(library.getLibraryScope()); scope = createScope(library.getLibraryScope());
} }
@@ -72,7 +77,7 @@ public class JetDefaultModalityModifiersTest extends JetLiteFixture {
private MutableClassDescriptor createClassDescriptor(ClassKind kind, JetClass aClass) { private MutableClassDescriptor createClassDescriptor(ClassKind kind, JetClass aClass) {
MutableClassDescriptor classDescriptor = new MutableClassDescriptor(JetTestUtils.DUMMY_TRACE, root, scope, kind); MutableClassDescriptor classDescriptor = new MutableClassDescriptor(JetTestUtils.DUMMY_TRACE, root, scope, kind);
descriptorResolver.resolveMutableClassDescriptor(aClass, classDescriptor); descriptorResolver.resolveMutableClassDescriptor(aClass, classDescriptor, JetTestUtils.DUMMY_TRACE);
return classDescriptor; return classDescriptor;
} }
@@ -90,7 +95,7 @@ public class JetDefaultModalityModifiersTest extends JetLiteFixture {
List<JetDeclaration> declarations = aClass.getDeclarations(); List<JetDeclaration> declarations = aClass.getDeclarations();
JetNamedFunction function = (JetNamedFunction) declarations.get(0); JetNamedFunction function = (JetNamedFunction) declarations.get(0);
SimpleFunctionDescriptor functionDescriptor = descriptorResolver.resolveFunctionDescriptor(classDescriptor, scope, function); SimpleFunctionDescriptor functionDescriptor = descriptorResolver.resolveFunctionDescriptor(classDescriptor, scope, function, JetTestUtils.DUMMY_TRACE);
assertEquals(expectedFunctionModality, functionDescriptor.getModality()); assertEquals(expectedFunctionModality, functionDescriptor.getModality());
} }
@@ -101,7 +106,7 @@ public class JetDefaultModalityModifiersTest extends JetLiteFixture {
List<JetDeclaration> declarations = aClass.getDeclarations(); List<JetDeclaration> declarations = aClass.getDeclarations();
JetProperty property = (JetProperty) declarations.get(0); JetProperty property = (JetProperty) declarations.get(0);
PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePropertyDescriptor(classDescriptor, scope, property); PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePropertyDescriptor(classDescriptor, scope, property, JetTestUtils.DUMMY_TRACE);
assertEquals(expectedPropertyModality, propertyDescriptor.getModality()); assertEquals(expectedPropertyModality, propertyDescriptor.getModality());
} }
@@ -113,7 +118,7 @@ public class JetDefaultModalityModifiersTest extends JetLiteFixture {
List<JetDeclaration> declarations = aClass.getDeclarations(); List<JetDeclaration> declarations = aClass.getDeclarations();
JetProperty property = (JetProperty) declarations.get(0); JetProperty property = (JetProperty) declarations.get(0);
PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePropertyDescriptor(classDescriptor, scope, property); PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePropertyDescriptor(classDescriptor, scope, property, JetTestUtils.DUMMY_TRACE);
PropertyAccessorDescriptor propertyAccessor = isGetter PropertyAccessorDescriptor propertyAccessor = isGetter
? propertyDescriptor.getGetter() ? propertyDescriptor.getGetter()
: propertyDescriptor.getSetter(); : propertyDescriptor.getSetter();
@@ -16,9 +16,12 @@
package org.jetbrains.jet.types; package org.jetbrains.jet.types;
import com.google.common.base.Predicates;
import com.intellij.psi.PsiFile;
import org.jetbrains.jet.JetLiteFixture; import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.JetTestCaseBuilder; import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
@@ -26,6 +29,7 @@ import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.JetPsiFactory; import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.resolve.DescriptorResolver; import org.jetbrains.jet.lang.resolve.DescriptorResolver;
import org.jetbrains.jet.lang.resolve.OverloadUtil; import org.jetbrains.jet.lang.resolve.OverloadUtil;
import org.jetbrains.jet.lang.resolve.TopDownAnalysisContext;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
/** /**
@@ -43,7 +47,8 @@ public class JetOverloadTest extends JetLiteFixture {
super.setUp(); super.setUp();
library = JetStandardLibrary.getInstance(); library = JetStandardLibrary.getInstance();
semanticServices = JetSemanticServices.createSemanticServices(library); semanticServices = JetSemanticServices.createSemanticServices(library);
descriptorResolver = semanticServices.getClassDescriptorResolver(JetTestUtils.DUMMY_TRACE); TopDownAnalysisContext analysisContext = new TopDownAnalysisContext(semanticServices, JetTestUtils.DUMMY_TRACE, Predicates.<PsiFile>alwaysTrue(), Configuration.EMPTY, false);
descriptorResolver = analysisContext.getDescriptorResolver();
} }
@Override @Override
@@ -171,7 +176,7 @@ public class JetOverloadTest extends JetLiteFixture {
private FunctionDescriptor makeFunction(String funDecl) { private FunctionDescriptor makeFunction(String funDecl) {
JetNamedFunction function = JetPsiFactory.createFunction(getProject(), funDecl); JetNamedFunction function = JetPsiFactory.createFunction(getProject(), funDecl);
return descriptorResolver.resolveFunctionDescriptor(root, library.getLibraryScope(), function); return descriptorResolver.resolveFunctionDescriptor(root, library.getLibraryScope(), function, JetTestUtils.DUMMY_TRACE);
} }
} }
@@ -16,9 +16,12 @@
package org.jetbrains.jet.types; package org.jetbrains.jet.types;
import com.google.common.base.Predicates;
import com.intellij.psi.PsiFile;
import org.jetbrains.jet.JetLiteFixture; import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.JetTestCaseBuilder; import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
@@ -26,6 +29,7 @@ import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.JetPsiFactory; import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.resolve.DescriptorResolver; import org.jetbrains.jet.lang.resolve.DescriptorResolver;
import org.jetbrains.jet.lang.resolve.OverridingUtil; import org.jetbrains.jet.lang.resolve.OverridingUtil;
import org.jetbrains.jet.lang.resolve.TopDownAnalysisContext;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
/** /**
@@ -43,7 +47,8 @@ public class JetOverridingTest extends JetLiteFixture {
super.setUp(); super.setUp();
library = JetStandardLibrary.getInstance(); library = JetStandardLibrary.getInstance();
semanticServices = JetSemanticServices.createSemanticServices(library); semanticServices = JetSemanticServices.createSemanticServices(library);
descriptorResolver = semanticServices.getClassDescriptorResolver(JetTestUtils.DUMMY_TRACE); TopDownAnalysisContext analysisContext = new TopDownAnalysisContext(semanticServices, JetTestUtils.DUMMY_TRACE, Predicates.<PsiFile>alwaysTrue(), Configuration.EMPTY, false);
descriptorResolver = analysisContext.getDescriptorResolver();
} }
@Override @Override
@@ -161,6 +166,6 @@ public class JetOverridingTest extends JetLiteFixture {
private FunctionDescriptor makeFunction(String funDecl) { private FunctionDescriptor makeFunction(String funDecl) {
JetNamedFunction function = JetPsiFactory.createFunction(getProject(), funDecl); JetNamedFunction function = JetPsiFactory.createFunction(getProject(), funDecl);
return descriptorResolver.resolveFunctionDescriptor(root, library.getLibraryScope(), function); return descriptorResolver.resolveFunctionDescriptor(root, library.getLibraryScope(), function, JetTestUtils.DUMMY_TRACE);
} }
} }
@@ -16,13 +16,16 @@
package org.jetbrains.jet.types; package org.jetbrains.jet.types;
import com.google.common.base.Predicates;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetLiteFixture; import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.JetTestCaseBuilder; import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
@@ -53,6 +56,7 @@ public class JetTypeCheckerTest extends JetLiteFixture {
private DescriptorResolver descriptorResolver; private DescriptorResolver descriptorResolver;
private JetScope scopeWithImports; private JetScope scopeWithImports;
private TypeResolver typeResolver; private TypeResolver typeResolver;
private TopDownAnalysisContext analysisContext;
public JetTypeCheckerTest() { public JetTypeCheckerTest() {
super(""); super("");
@@ -64,9 +68,12 @@ public class JetTypeCheckerTest extends JetLiteFixture {
library = JetStandardLibrary.getInstance(); library = JetStandardLibrary.getInstance();
semanticServices = JetSemanticServices.createSemanticServices(library); semanticServices = JetSemanticServices.createSemanticServices(library);
classDefinitions = new ClassDefinitions(); classDefinitions = new ClassDefinitions();
descriptorResolver = semanticServices.getClassDescriptorResolver(JetTestUtils.DUMMY_TRACE);
analysisContext = new TopDownAnalysisContext(semanticServices, JetTestUtils.DUMMY_TRACE, Predicates.<PsiFile>alwaysTrue(), Configuration.EMPTY, false);
descriptorResolver = analysisContext.getDescriptorResolver();
scopeWithImports = addImports(classDefinitions.BASIC_SCOPE); scopeWithImports = addImports(classDefinitions.BASIC_SCOPE);
typeResolver = new TypeResolver(semanticServices, JetTestUtils.DUMMY_TRACE, true); typeResolver = analysisContext.getTypeResolver();
} }
@Override @Override
@@ -540,14 +547,14 @@ public class JetTypeCheckerTest extends JetLiteFixture {
private void assertType(String expression, JetType expectedType) { private void assertType(String expression, JetType expectedType) {
Project project = getProject(); Project project = getProject();
JetExpression jetExpression = JetPsiFactory.createExpression(project, expression); JetExpression jetExpression = JetPsiFactory.createExpression(project, expression);
JetType type = semanticServices.getTypeInferrerServices(JetTestUtils.DUMMY_TRACE).getType(scopeWithImports, jetExpression, TypeUtils.NO_EXPECTED_TYPE); JetType type = analysisContext.getExpressionTypingServices().getType(scopeWithImports, jetExpression, TypeUtils.NO_EXPECTED_TYPE, JetTestUtils.DUMMY_TRACE);
assertTrue(type + " != " + expectedType, type.equals(expectedType)); assertTrue(type + " != " + expectedType, type.equals(expectedType));
} }
private void assertErrorType(String expression) { private void assertErrorType(String expression) {
Project project = getProject(); Project project = getProject();
JetExpression jetExpression = JetPsiFactory.createExpression(project, expression); JetExpression jetExpression = JetPsiFactory.createExpression(project, expression);
JetType type = semanticServices.getTypeInferrerServices(JetTestUtils.DUMMY_TRACE).safeGetType(scopeWithImports, jetExpression, TypeUtils.NO_EXPECTED_TYPE); JetType type = analysisContext.getExpressionTypingServices().safeGetType(scopeWithImports, jetExpression, TypeUtils.NO_EXPECTED_TYPE, JetTestUtils.DUMMY_TRACE);
assertTrue("Error type expected but " + type + " returned", ErrorUtils.isErrorType(type)); assertTrue("Error type expected but " + type + " returned", ErrorUtils.isErrorType(type));
} }
@@ -570,7 +577,7 @@ public class JetTypeCheckerTest extends JetLiteFixture {
private void assertType(JetScope scope, String expression, String expectedTypeStr) { private void assertType(JetScope scope, String expression, String expectedTypeStr) {
Project project = getProject(); Project project = getProject();
JetExpression jetExpression = JetPsiFactory.createExpression(project, expression); JetExpression jetExpression = JetPsiFactory.createExpression(project, expression);
JetType type = semanticServices.getTypeInferrerServices(JetTestUtils.DUMMY_TRACE).getType(addImports(scope), jetExpression, TypeUtils.NO_EXPECTED_TYPE); JetType type = analysisContext.getExpressionTypingServices().getType(addImports(scope), jetExpression, TypeUtils.NO_EXPECTED_TYPE, JetTestUtils.DUMMY_TRACE);
JetType expectedType = expectedTypeStr == null ? null : makeType(expectedTypeStr); JetType expectedType = expectedTypeStr == null ? null : makeType(expectedTypeStr);
assertEquals(expectedType, type); assertEquals(expectedType, type);
} }
@@ -590,7 +597,7 @@ public class JetTypeCheckerTest extends JetLiteFixture {
} }
private JetType makeType(JetScope scope, String typeStr) { private JetType makeType(JetScope scope, String typeStr) {
return new TypeResolver(semanticServices, JetTestUtils.DUMMY_TRACE, true).resolveType(scope, JetPsiFactory.createType(getProject(), typeStr)); return analysisContext.getTypeResolver().resolveType(scope, JetPsiFactory.createType(getProject(), typeStr), JetTestUtils.DUMMY_TRACE, true);
} }
private class ClassDefinitions { private class ClassDefinitions {
@@ -657,7 +664,7 @@ public class JetTypeCheckerTest extends JetLiteFixture {
Set<FunctionDescriptor> writableFunctionGroup = Sets.newLinkedHashSet(); Set<FunctionDescriptor> writableFunctionGroup = Sets.newLinkedHashSet();
ModuleDescriptor module = new ModuleDescriptor("TypeCheckerTest"); ModuleDescriptor module = new ModuleDescriptor("TypeCheckerTest");
for (String funDecl : FUNCTION_DECLARATIONS) { for (String funDecl : FUNCTION_DECLARATIONS) {
FunctionDescriptor functionDescriptor = descriptorResolver.resolveFunctionDescriptor(module, this, JetPsiFactory.createFunction(getProject(), funDecl)); FunctionDescriptor functionDescriptor = descriptorResolver.resolveFunctionDescriptor(module, this, JetPsiFactory.createFunction(getProject(), funDecl), JetTestUtils.DUMMY_TRACE);
if (name.equals(functionDescriptor.getName())) { if (name.equals(functionDescriptor.getName())) {
writableFunctionGroup.add(functionDescriptor); writableFunctionGroup.add(functionDescriptor);
} }
@@ -682,14 +689,14 @@ public class JetTypeCheckerTest extends JetLiteFixture {
// This call has side-effects on the parameterScope (fills it in) // This call has side-effects on the parameterScope (fills it in)
List<TypeParameterDescriptor> typeParameters List<TypeParameterDescriptor> typeParameters
= descriptorResolver.resolveTypeParameters(classDescriptor, parameterScope, classElement.getTypeParameters()); = descriptorResolver.resolveTypeParameters(classDescriptor, parameterScope, classElement.getTypeParameters(), JetTestUtils.DUMMY_TRACE);
descriptorResolver.resolveGenericBounds(classElement, parameterScope, typeParameters); descriptorResolver.resolveGenericBounds(classElement, parameterScope, typeParameters, JetTestUtils.DUMMY_TRACE);
List<JetDelegationSpecifier> delegationSpecifiers = classElement.getDelegationSpecifiers(); List<JetDelegationSpecifier> delegationSpecifiers = classElement.getDelegationSpecifiers();
// TODO : assuming that the hierarchy is acyclic // TODO : assuming that the hierarchy is acyclic
Collection<JetType> supertypes = delegationSpecifiers.isEmpty() Collection<JetType> supertypes = delegationSpecifiers.isEmpty()
? Collections.singleton(JetStandardClasses.getAnyType()) ? Collections.singleton(JetStandardClasses.getAnyType())
: descriptorResolver.resolveDelegationSpecifiers(parameterScope, delegationSpecifiers, typeResolver); : descriptorResolver.resolveDelegationSpecifiers(parameterScope, delegationSpecifiers, typeResolver, JetTestUtils.DUMMY_TRACE, true);
// for (JetType supertype: supertypes) { // for (JetType supertype: supertypes) {
// if (supertype.getConstructor().isSealed()) { // if (supertype.getConstructor().isSealed()) {
// trace.getErrorHandler().genericError(classElement.getNameAsDeclaration().getNode(), "Class " + classElement.getName() + " can not extend final type " + supertype); // trace.getErrorHandler().genericError(classElement.getNameAsDeclaration().getNode(), "Class " + classElement.getName() + " can not extend final type " + supertype);
@@ -706,7 +713,7 @@ public class JetTypeCheckerTest extends JetLiteFixture {
@Override @Override
public void visitProperty(JetProperty property) { public void visitProperty(JetProperty property) {
if (property.getPropertyTypeRef() != null) { if (property.getPropertyTypeRef() != null) {
memberDeclarations.addPropertyDescriptor(descriptorResolver.resolvePropertyDescriptor(classDescriptor, parameterScope, property)); memberDeclarations.addPropertyDescriptor(descriptorResolver.resolvePropertyDescriptor(classDescriptor, parameterScope, property, JetTestUtils.DUMMY_TRACE));
} else { } else {
// TODO : Caution: a cyclic dependency possible // TODO : Caution: a cyclic dependency possible
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@@ -716,7 +723,7 @@ public class JetTypeCheckerTest extends JetLiteFixture {
@Override @Override
public void visitNamedFunction(JetNamedFunction function) { public void visitNamedFunction(JetNamedFunction function) {
if (function.getReturnTypeRef() != null) { if (function.getReturnTypeRef() != null) {
memberDeclarations.addFunctionDescriptor(descriptorResolver.resolveFunctionDescriptor(classDescriptor, parameterScope, function)); memberDeclarations.addFunctionDescriptor(descriptorResolver.resolveFunctionDescriptor(classDescriptor, parameterScope, function, JetTestUtils.DUMMY_TRACE));
} else { } else {
// TODO : Caution: a cyclic dependency possible // TODO : Caution: a cyclic dependency possible
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@@ -740,11 +747,11 @@ public class JetTypeCheckerTest extends JetLiteFixture {
null null
); );
for (JetSecondaryConstructor constructor : classElement.getSecondaryConstructors()) { for (JetSecondaryConstructor constructor : classElement.getSecondaryConstructors()) {
ConstructorDescriptorImpl functionDescriptor = descriptorResolver.resolveSecondaryConstructorDescriptor(memberDeclarations, classDescriptor, constructor); ConstructorDescriptorImpl functionDescriptor = descriptorResolver.resolveSecondaryConstructorDescriptor(memberDeclarations, classDescriptor, constructor, JetTestUtils.DUMMY_TRACE);
functionDescriptor.setReturnType(classDescriptor.getDefaultType()); functionDescriptor.setReturnType(classDescriptor.getDefaultType());
constructors.add(functionDescriptor); constructors.add(functionDescriptor);
} }
ConstructorDescriptorImpl primaryConstructorDescriptor = descriptorResolver.resolvePrimaryConstructorDescriptor(scope, classDescriptor, classElement); ConstructorDescriptorImpl primaryConstructorDescriptor = descriptorResolver.resolvePrimaryConstructorDescriptor(scope, classDescriptor, classElement, JetTestUtils.DUMMY_TRACE);
if (primaryConstructorDescriptor != null) { if (primaryConstructorDescriptor != null) {
primaryConstructorDescriptor.setReturnType(classDescriptor.getDefaultType()); primaryConstructorDescriptor.setReturnType(classDescriptor.getDefaultType());
constructors.add(primaryConstructorDescriptor); constructors.add(primaryConstructorDescriptor);
+1
View File
@@ -27,6 +27,7 @@
<orderEntry type="library" scope="PROVIDED" name="junit-plugin" level="project" /> <orderEntry type="library" scope="PROVIDED" name="junit-plugin" level="project" />
<orderEntry type="module" module-name="j2k" /> <orderEntry type="module" module-name="j2k" />
<orderEntry type="module" module-name="js.translator" /> <orderEntry type="module" module-name="js.translator" />
<orderEntry type="library" name="guice-3.0" level="project" />
</component> </component>
</module> </module>
@@ -16,6 +16,9 @@
package org.jetbrains.jet.plugin.liveTemplates.macro; package org.jetbrains.jet.plugin.liveTemplates.macro;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.codeInsight.template.Expression; import com.intellij.codeInsight.template.Expression;
@@ -30,10 +33,14 @@ import com.intellij.psi.PsiNamedElement;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.compiler.TipsManager; import org.jetbrains.jet.compiler.TipsManager;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.TopDownAnalysisContext;
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.plugin.compiler.WholeProjectAnalyzerFacade; import org.jetbrains.jet.plugin.compiler.WholeProjectAnalyzerFacade;
@@ -51,7 +58,7 @@ public abstract class BaseJetVariableMacro extends Macro {
private JetNamedDeclaration[] getVariables(Expression[] params, ExpressionContext context) { private JetNamedDeclaration[] getVariables(Expression[] params, ExpressionContext context) {
if (params.length != 0) return null; if (params.length != 0) return null;
Project project = context.getProject(); final Project project = context.getProject();
PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiDocumentManager.getInstance(project).commitAllDocuments();
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument()); PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
@@ -66,11 +73,19 @@ public abstract class BaseJetVariableMacro extends Macro {
return null; return null;
} }
class TdacModule extends AbstractModule {
@Override
protected void configure() {
bind(JetSemanticServices.class).toInstance(JetSemanticServices.createSemanticServices(project));
}
}
CallResolver.Context callResolverContext = Guice.createInjector(new TdacModule()).getInstance(CallResolver.Context.class);
List<VariableDescriptor> filteredDescriptors = new ArrayList<VariableDescriptor>(); List<VariableDescriptor> filteredDescriptors = new ArrayList<VariableDescriptor>();
for (DeclarationDescriptor declarationDescriptor : scope.getAllDescriptors()) { for (DeclarationDescriptor declarationDescriptor : scope.getAllDescriptors()) {
if (declarationDescriptor instanceof VariableDescriptor) { if (declarationDescriptor instanceof VariableDescriptor) {
VariableDescriptor variableDescriptor = (VariableDescriptor) declarationDescriptor; VariableDescriptor variableDescriptor = (VariableDescriptor) declarationDescriptor;
if (isSuitable(variableDescriptor, scope, project)) { if (isSuitable(variableDescriptor, scope, project, callResolverContext)) {
filteredDescriptors.add(variableDescriptor); filteredDescriptors.add(variableDescriptor);
} }
} }
@@ -89,7 +104,7 @@ public abstract class BaseJetVariableMacro extends Macro {
return declarations.toArray(new JetNamedDeclaration[declarations.size()]); return declarations.toArray(new JetNamedDeclaration[declarations.size()]);
} }
protected abstract boolean isSuitable(@NotNull VariableDescriptor variableDescriptor, @NotNull JetScope scope, @NotNull Project project); protected abstract boolean isSuitable(@NotNull VariableDescriptor variableDescriptor, @NotNull JetScope scope, @NotNull Project project, CallResolver.Context callResolverContext);
@Nullable @Nullable
private static JetExpression findContextExpression(PsiFile psiFile, int startOffset) { private static JetExpression findContextExpression(PsiFile psiFile, int startOffset) {
@@ -19,6 +19,7 @@ package org.jetbrains.jet.plugin.liveTemplates.macro;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.JetBundle;
@@ -38,7 +39,7 @@ public class JetAnyVariableMacro extends BaseJetVariableMacro {
} }
@Override @Override
protected boolean isSuitable(@NotNull VariableDescriptor variableDescriptor, @NotNull JetScope scope, @NotNull Project project) { protected boolean isSuitable(@NotNull VariableDescriptor variableDescriptor, @NotNull JetScope scope, @NotNull Project project, CallResolver.Context callResolverContext) {
return true; return true;
} }
} }
@@ -19,6 +19,7 @@ package org.jetbrains.jet.plugin.liveTemplates.macro;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingUtils; import org.jetbrains.jet.lang.types.expressions.ExpressionTypingUtils;
import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.JetBundle;
@@ -28,6 +29,10 @@ import org.jetbrains.jet.plugin.JetBundle;
* @since 2/7/12 * @since 2/7/12
*/ */
public class JetIterableVariableMacro extends BaseJetVariableMacro { public class JetIterableVariableMacro extends BaseJetVariableMacro {
public JetIterableVariableMacro() {
}
@Override @Override
public String getName() { public String getName() {
return "kotlinIterableVariable"; return "kotlinIterableVariable";
@@ -39,7 +44,7 @@ public class JetIterableVariableMacro extends BaseJetVariableMacro {
} }
@Override @Override
protected boolean isSuitable(@NotNull VariableDescriptor variableDescriptor, @NotNull JetScope scope, @NotNull Project project) { protected boolean isSuitable(@NotNull VariableDescriptor variableDescriptor, @NotNull JetScope scope, @NotNull Project project, CallResolver.Context callResolverContext) {
return ExpressionTypingUtils.isVariableIterable(project, variableDescriptor, scope); return ExpressionTypingUtils.isVariableIterable(callResolverContext, project, variableDescriptor, scope);
} }
} }