diff --git a/idea/src/org/jetbrains/jet/lang/annotations/JetPsiChecker.java b/idea/src/org/jetbrains/jet/lang/annotations/JetPsiChecker.java index 49740db4b1d..098fa1bc513 100644 --- a/idea/src/org/jetbrains/jet/lang/annotations/JetPsiChecker.java +++ b/idea/src/org/jetbrains/jet/lang/annotations/JetPsiChecker.java @@ -62,11 +62,11 @@ public class JetPsiChecker implements Annotator { }); } catch (ProcessCanceledException e) { - // Canceled. We are fine + throw e; } catch (Throwable e) { // TODO - holder.createErrorAnnotation(new TextRange(0, 1), e.getClass().getCanonicalName() + ": " + e.getMessage()); + holder.createErrorAnnotation(element, e.getClass().getCanonicalName() + ": " + e.getMessage()); e.printStackTrace(); } } diff --git a/idea/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java b/idea/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java index e04979f9305..fc7b3558ef3 100644 --- a/idea/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java +++ b/idea/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java @@ -98,6 +98,10 @@ public class JetExpressionParsing extends AbstractJetParsing { } }, +// COLON_AS(COLON, AS_KEYWORD) { +// +// }, + MULTIPLICATIVE(MUL, DIV, PERC) { @Override public void parseHigherPrecedence(JetExpressionParsing parser) { diff --git a/idea/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java b/idea/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java index 2f2cb3dd038..55285838a38 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java @@ -25,9 +25,12 @@ public class AnalyzingUtils { BindingTraceContext bindingTraceContext = new BindingTraceContext(); JavaSemanticServices javaSemanticServices = new JavaSemanticServices(project, semanticServices, bindingTraceContext); - WritableScope scope = new WritableScope(semanticServices.getStandardLibrary().getLibraryScope()); - scope.importScope(new JavaPackageScope("", javaSemanticServices)); - scope.importScope(new JavaPackageScope("java.lang", javaSemanticServices)); + JetScope libraryScope = semanticServices.getStandardLibrary().getLibraryScope(); + WritableScope scope = new WritableScope(libraryScope, libraryScope.getContainingDeclaration()); +// scope.importScope(javaSemanticServices.getDescriptorResolver().resolveNamespace("").getMemberScope()); +// scope.importScope(javaSemanticServices.getDescriptorResolver().resolveNamespace("java.lang").getMemberScope()); + scope.importScope(new JavaPackageScope("", null, javaSemanticServices)); + scope.importScope(new JavaPackageScope("java.lang", null, javaSemanticServices)); new TopDownAnalyzer(semanticServices, bindingTraceContext).process(scope, namespace); return bindingTraceContext; diff --git a/idea/src/org/jetbrains/jet/lang/resolve/BindingContext.java b/idea/src/org/jetbrains/jet/lang/resolve/BindingContext.java index c69afd88962..a4bcbbf1260 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/BindingContext.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/BindingContext.java @@ -22,4 +22,5 @@ public interface BindingContext { Type resolveTypeReference(JetTypeReference typeReference); PsiElement resolveToDeclarationPsiElement(JetReferenceExpression referenceExpression); + PsiElement getDeclarationPsiElement(DeclarationDescriptor descriptor); } diff --git a/idea/src/org/jetbrains/jet/lang/resolve/BindingTraceContext.java b/idea/src/org/jetbrains/jet/lang/resolve/BindingTraceContext.java index f4a870fabaa..f53c4b63d42 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/BindingTraceContext.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/BindingTraceContext.java @@ -101,4 +101,8 @@ public class BindingTraceContext extends BindingTrace implements BindingContext return descriptorToDeclarations.get(declarationDescriptor.getOriginal()); } + @Override + public PsiElement getDeclarationPsiElement(DeclarationDescriptor descriptor) { + return descriptorToDeclarations.get(descriptor); + } } diff --git a/idea/src/org/jetbrains/jet/lang/resolve/ClassDescriptorResolver.java b/idea/src/org/jetbrains/jet/lang/resolve/ClassDescriptorResolver.java index 9fb1dbb0bc0..535c7215413 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/ClassDescriptorResolver.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/ClassDescriptorResolver.java @@ -26,11 +26,15 @@ public class ClassDescriptorResolver { @Nullable public ClassDescriptor resolveClassDescriptor(@NotNull JetScope scope, @NotNull JetClass classElement) { - WritableScope parameterScope = new WritableScope(scope); + ClassDescriptorImpl classDescriptor = new ClassDescriptorImpl( + scope.getContainingDeclaration(), + AttributeResolver.INSTANCE.resolveAttributes(classElement.getModifierList()), + classElement.getName()); + WritableScope parameterScope = new WritableScope(scope, classDescriptor); // This call has side-effects on the parameterScope (fills it in) List typeParameters - = resolveTypeParameters(parameterScope, classElement.getTypeParameters()); + = resolveTypeParameters(classDescriptor, parameterScope, classElement.getTypeParameters()); List delegationSpecifiers = classElement.getDelegationSpecifiers(); // TODO : assuming that the hierarchy is acyclic @@ -38,25 +42,24 @@ public class ClassDescriptorResolver { ? Collections.singleton(JetStandardClasses.getAnyType()) : resolveTypes(parameterScope, delegationSpecifiers); boolean open = classElement.hasModifier(JetTokens.OPEN_KEYWORD); - WritableScope members = resolveMembers(classElement, typeParameters, scope, parameterScope, superclasses); + WritableScope members = resolveMembers(classDescriptor, classElement, typeParameters, scope, parameterScope, superclasses); - return new ClassDescriptorImpl( - AttributeResolver.INSTANCE.resolveAttributes(classElement.getModifierList()), + return classDescriptor.initialize( !open, - classElement.getName(), typeParameters, superclasses, members ); } - @Nullable public void resolveMutableClassDescriptor(@NotNull JetScope scope, @NotNull JetClass classElement, @NotNull MutableClassDescriptor descriptor) { + descriptor.setName(classElement.getName()); + WritableScope parameterScope = descriptor.getUnsubstitutedMemberScope(); // This call has side-effects on the parameterScope (fills it in) List typeParameters - = resolveTypeParameters(parameterScope, classElement.getTypeParameters()); + = resolveTypeParameters(descriptor, parameterScope, classElement.getTypeParameters()); List delegationSpecifiers = classElement.getDelegationSpecifiers(); // TODO : assuming that the hierarchy is acyclic @@ -78,13 +81,14 @@ public class ClassDescriptorResolver { } private WritableScope resolveMembers( + final ClassDescriptor classDescriptor, final JetClass classElement, List typeParameters, final JetScope outerScope, final JetScope typeParameterScope, final Collection supertypes) { - final WritableScope memberDeclarations = new WritableScope(typeParameterScope); + final WritableScope memberDeclarations = new WritableScope(typeParameterScope, classDescriptor); List declarations = classElement.getDeclarations(); for (JetDeclaration declaration : declarations) { @@ -92,7 +96,7 @@ public class ClassDescriptorResolver { @Override public void visitProperty(JetProperty property) { if (property.getPropertyTypeRef() != null) { - memberDeclarations.addPropertyDescriptor(resolvePropertyDescriptor(typeParameterScope, property)); + memberDeclarations.addPropertyDescriptor(resolvePropertyDescriptor(classDescriptor, typeParameterScope, property)); } else { // TODO : Caution: a cyclic dependency possible throw new UnsupportedOperationException(); @@ -102,7 +106,7 @@ public class ClassDescriptorResolver { @Override public void visitFunction(JetFunction function) { if (function.getReturnTypeRef() != null) { - memberDeclarations.addFunctionDescriptor(resolveFunctionDescriptor(typeParameterScope, function)); + memberDeclarations.addFunctionDescriptor(resolveFunctionDescriptor(classDescriptor, typeParameterScope, function)); } else { // TODO : Caution: a cyclic dependency possible throw new UnsupportedOperationException(); @@ -120,11 +124,17 @@ public class ClassDescriptorResolver { } @NotNull - public FunctionDescriptor resolveFunctionDescriptor(JetScope scope, JetFunction function) { - WritableScope parameterScope = new WritableScope(scope); + public FunctionDescriptor resolveFunctionDescriptor(DeclarationDescriptor containingDescriptor, JetScope scope, JetFunction function) { + FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl( + containingDescriptor, + AttributeResolver.INSTANCE.resolveAttributes(function.getModifierList()), + function.getName() + ); + WritableScope parameterScope = new WritableScope(scope, functionDescriptor); + // The two calls below have side-effects on parameterScope - List typeParameterDescriptors = resolveTypeParameters(parameterScope, function.getTypeParameters()); - List valueParameterDescriptors = resolveValueParameters(parameterScope, function.getValueParameters()); + List typeParameterDescriptors = resolveTypeParameters(functionDescriptor, parameterScope, function.getTypeParameters()); + List valueParameterDescriptors = resolveValueParameters(functionDescriptor, parameterScope, function.getValueParameters()); Type returnType; JetTypeReference returnTypeRef = function.getReturnTypeRef(); @@ -138,20 +148,16 @@ public class ClassDescriptorResolver { returnType = semanticServices.getTypeInferrer().safeGetType(parameterScope, bodyExpression, function.hasBlockBody()); } - FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl( - null, - AttributeResolver.INSTANCE.resolveAttributes(function.getModifierList()), - function.getName(), + functionDescriptor.initialize( typeParameterDescriptors, valueParameterDescriptors, - returnType - ); + returnType); trace.recordDeclarationResolution(function, functionDescriptor); return functionDescriptor; } - private List resolveValueParameters(WritableScope parameterScope, List valueParameters) { + private List resolveValueParameters(FunctionDescriptorImpl functionDescriptor, WritableScope parameterScope, List valueParameters) { List result = new ArrayList(); for (int i = 0, valueParametersSize = valueParameters.size(); i < valueParametersSize; i++) { JetParameter valueParameter = valueParameters.get(i); @@ -165,6 +171,7 @@ public class ClassDescriptorResolver { type = typeResolver.resolveType(parameterScope, typeReference); } ValueParameterDescriptor valueParameterDescriptor = new ValueParameterDescriptorImpl( + functionDescriptor, i, AttributeResolver.INSTANCE.resolveAttributes(valueParameter.getModifierList()), valueParameter.getName(), @@ -181,18 +188,19 @@ public class ClassDescriptorResolver { return result; } - public List resolveTypeParameters(WritableScope extensibleScope, List typeParameters) { + public List resolveTypeParameters(DeclarationDescriptor containingDescriptor, WritableScope extensibleScope, List typeParameters) { // TODO : When-clause List result = new ArrayList(); for (JetTypeParameter typeParameter : typeParameters) { - result.add(resolveTypeParameter(extensibleScope, typeParameter)); + result.add(resolveTypeParameter(containingDescriptor, extensibleScope, typeParameter)); } return result; } - private TypeParameterDescriptor resolveTypeParameter(WritableScope extensibleScope, JetTypeParameter typeParameter) { + private TypeParameterDescriptor resolveTypeParameter(DeclarationDescriptor containingDescriptor, WritableScope extensibleScope, JetTypeParameter typeParameter) { JetTypeReference extendsBound = typeParameter.getExtendsBound(); TypeParameterDescriptor typeParameterDescriptor = new TypeParameterDescriptor( + containingDescriptor, AttributeResolver.INSTANCE.resolveAttributes(typeParameter.getModifierList()), typeParameter.getVariance(), typeParameter.getName(), @@ -222,14 +230,15 @@ public class ClassDescriptorResolver { } @NotNull - public PropertyDescriptor resolvePropertyDescriptor(@NotNull JetScope scope, @NotNull JetParameter parameter) { + public PropertyDescriptor resolvePropertyDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope scope, @NotNull JetParameter parameter) { return new PropertyDescriptorImpl( + containingDeclaration, AttributeResolver.INSTANCE.resolveAttributes(parameter.getModifierList()), parameter.getName(), typeResolver.resolveType(scope, parameter.getTypeReference())); } - public PropertyDescriptor resolvePropertyDescriptor(@NotNull JetScope scope, JetProperty property) { + public PropertyDescriptor resolvePropertyDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope scope, JetProperty property) { // TODO : receiver? JetTypeReference propertyTypeRef = property.getPropertyTypeRef(); @@ -244,6 +253,7 @@ public class ClassDescriptorResolver { } return new PropertyDescriptorImpl( + containingDeclaration, AttributeResolver.INSTANCE.resolveAttributes(property.getModifierList()), property.getName(), type); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/JetScope.java b/idea/src/org/jetbrains/jet/lang/resolve/JetScope.java index ea0bdf54f23..003a88afa45 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/JetScope.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/JetScope.java @@ -8,7 +8,13 @@ import org.jetbrains.jet.lang.types.*; * @author abreslav */ public interface JetScope { - JetScope EMPTY = new JetScopeImpl() {}; + JetScope EMPTY = new JetScopeImpl() { + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + throw new UnsupportedOperationException(); // TODO + } + }; @Nullable ClassDescriptor getClass(@NotNull String name); @@ -30,4 +36,7 @@ public interface JetScope { @NotNull FunctionGroup getFunctionGroup(@NotNull String name); + + @NotNull + DeclarationDescriptor getContainingDeclaration(); } diff --git a/idea/src/org/jetbrains/jet/lang/resolve/JetScopeAdapter.java b/idea/src/org/jetbrains/jet/lang/resolve/JetScopeAdapter.java index 39ade6dbdb8..dea56873dce 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/JetScopeAdapter.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/JetScopeAdapter.java @@ -50,4 +50,9 @@ public class JetScopeAdapter implements JetScope { return scope.getExtension(name); } + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return scope.getContainingDeclaration(); + } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/lang/resolve/LazySubstitutingClassDescriptor.java b/idea/src/org/jetbrains/jet/lang/resolve/LazySubstitutingClassDescriptor.java index 6bdb5e63c09..1b66444f6d8 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/LazySubstitutingClassDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/LazySubstitutingClassDescriptor.java @@ -28,6 +28,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor { throw new UnsupportedOperationException(); // TODO } + @NotNull @Override public JetScope getMemberScope(List typeArguments) { JetScope memberScope = original.getMemberScope(typeArguments); @@ -47,11 +48,18 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor { return original.getName(); } + @NotNull @Override public DeclarationDescriptor getOriginal() { return original.getOriginal(); } + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return original.getContainingDeclaration(); + } + @Override public R accept(DeclarationDescriptorVisitor visitor, D data) { return visitor.visitClassDescriptor(this, data); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/MutableClassDescriptor.java b/idea/src/org/jetbrains/jet/lang/resolve/MutableClassDescriptor.java index bd21327f760..cc2c6f1dba7 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/MutableClassDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/MutableClassDescriptor.java @@ -14,7 +14,7 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme private TypeConstructor typeConstructor; public MutableClassDescriptor(@NotNull JetScope outerScope) { - this.unsubstitutedMemberScope = new WritableScope(outerScope); + this.unsubstitutedMemberScope = new WritableScope(outerScope, this); } @NotNull @@ -27,6 +27,7 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme this.typeConstructor = typeConstructor; } + @NotNull @Override public JetScope getMemberScope(List typeArguments) { List typeParameters = getTypeConstructor().getParameters(); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/MutableDeclarationDescriptor.java b/idea/src/org/jetbrains/jet/lang/resolve/MutableDeclarationDescriptor.java index 1554228f518..b69c53dc9d7 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/MutableDeclarationDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/MutableDeclarationDescriptor.java @@ -1,5 +1,7 @@ package org.jetbrains.jet.lang.resolve; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.types.Attribute; import org.jetbrains.jet.lang.types.DeclarationDescriptor; import org.jetbrains.jet.lang.types.DeclarationDescriptorVisitor; @@ -11,6 +13,7 @@ import java.util.List; */ public abstract class MutableDeclarationDescriptor implements DeclarationDescriptor { private String name; + private DeclarationDescriptor containingDeclaration; @Override public List getAttributes() { @@ -26,11 +29,22 @@ public abstract class MutableDeclarationDescriptor implements DeclarationDescrip this.name = name; } + @NotNull @Override public DeclarationDescriptor getOriginal() { return this; } + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return containingDeclaration; + } + + public void setContainingDeclaration(@Nullable DeclarationDescriptor containingDeclaration) { + this.containingDeclaration = containingDeclaration; + } + @Override public void acceptVoid(DeclarationDescriptorVisitor visitor) { accept(visitor, null); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/ScopeWithReceiver.java b/idea/src/org/jetbrains/jet/lang/resolve/ScopeWithReceiver.java index 8a9763272a2..a182213a354 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/ScopeWithReceiver.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/ScopeWithReceiver.java @@ -47,4 +47,10 @@ public class ScopeWithReceiver extends JetScopeImpl { public Type getThisType() { return receiverTypeScope.getThisType(); } + + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return outerScope.getContainingDeclaration(); + } } diff --git a/idea/src/org/jetbrains/jet/lang/resolve/SubstitutingScope.java b/idea/src/org/jetbrains/jet/lang/resolve/SubstitutingScope.java index 0a73b23edcf..b2e6972a175 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/SubstitutingScope.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/SubstitutingScope.java @@ -43,7 +43,7 @@ public class SubstitutingScope implements JetScope { @Override public NamespaceDescriptor getNamespace(@NotNull String name) { - throw new UnsupportedOperationException(); // TODO + return workerScope.getNamespace(name); // TODO } @Override @@ -66,4 +66,10 @@ public class SubstitutingScope implements JetScope { } return new LazySubstitutingFunctionGroup(substitutionContext, functionGroup); } + + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return workerScope.getContainingDeclaration(); + } } diff --git a/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java b/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java index 2bfe0a5e1c2..d01f258c4bf 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java @@ -32,7 +32,7 @@ public class TopDownAnalyzer { } public void process(@NotNull JetScope outerScope, @NotNull List declarations) { - final WritableScope toplevelScope = new WritableScope(outerScope); + final WritableScope toplevelScope = new WritableScope(outerScope, outerScope.getContainingDeclaration()); // TODO ?! trace.setToplevelScope(toplevelScope); // TODO : this is a hack collectTypeDeclarators(toplevelScope, declarations); resolveTypeDeclarations(); @@ -57,7 +57,19 @@ public class TopDownAnalyzer { public void visitNamespace(JetNamespace namespace) { List importDirectives = namespace.getImportDirectives(); - WritableScope namespaceScope = new WritableScope(declaringScope); + String name = namespace.getName(); + NamespaceDescriptor namespaceDescriptor = declaringScope.getNamespace(name); + if (namespaceDescriptor == null) { + namespaceDescriptor = new NamespaceDescriptor( + declaringScope.getContainingDeclaration(), + Collections.emptyList(), // TODO + name + ); + declaringScope.addNamespace(namespaceDescriptor); + trace.recordDeclarationResolution(namespace, namespaceDescriptor); + } + + WritableScope namespaceScope = new WritableScope(declaringScope, namespaceDescriptor); namespaceScopes.put(namespace, namespaceScope); for (JetImportDirective importDirective : importDirectives) { @@ -165,7 +177,7 @@ public class TopDownAnalyzer { private void processFunction(@NotNull WritableScope declaringScope, JetFunction function) { declaringScopes.put(function, declaringScope); - FunctionDescriptor descriptor = classDescriptorResolver.resolveFunctionDescriptor(declaringScope, function); + FunctionDescriptor descriptor = classDescriptorResolver.resolveFunctionDescriptor(declaringScope.getContainingDeclaration(), declaringScope, function); declaringScope.addFunctionDescriptor(descriptor); functions.put(function, descriptor); trace.recordDeclarationResolution(function, descriptor); @@ -173,7 +185,7 @@ public class TopDownAnalyzer { private void processProperty(WritableScope declaringScope, JetProperty property) { declaringScopes.put(property, declaringScope); - PropertyDescriptor descriptor = classDescriptorResolver.resolvePropertyDescriptor(declaringScope, property); + PropertyDescriptor descriptor = classDescriptorResolver.resolvePropertyDescriptor(declaringScope.getContainingDeclaration(), declaringScope, property); declaringScope.addPropertyDescriptor(descriptor); trace.recordDeclarationResolution(property, descriptor); } @@ -192,7 +204,7 @@ public class TopDownAnalyzer { WritableScope declaringScope = declaringScopes.get(function); assert declaringScope != null; - WritableScope parameterScope = new WritableScope(declaringScope); + WritableScope parameterScope = new WritableScope(declaringScope, descriptor); for (TypeParameterDescriptor typeParameter : descriptor.getTypeParameters()) { parameterScope.addTypeParameterDescriptor(typeParameter); } diff --git a/idea/src/org/jetbrains/jet/lang/resolve/WritableScope.java b/idea/src/org/jetbrains/jet/lang/resolve/WritableScope.java index a72f56e645b..548aba5e8ef 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/WritableScope.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/WritableScope.java @@ -22,12 +22,24 @@ public class WritableScope extends JetScopeAdapter { @Nullable private Map classDescriptors; @Nullable + private Map namespaceDescriptors; + @Nullable private Type thisType; @Nullable private List imports; - public WritableScope(JetScope scope) { + @NotNull + private final DeclarationDescriptor ownerDeclarationDescriptor; + + public WritableScope(JetScope scope, @NotNull DeclarationDescriptor owner) { super(scope); + this.ownerDeclarationDescriptor = owner; + } + + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return ownerDeclarationDescriptor; } public void importScope(JetScope imported) { @@ -160,7 +172,6 @@ public class WritableScope extends JetScopeAdapter { public void addClassAlias(String name, ClassDescriptor classDescriptor) { Map classDescriptors = getClassDescriptors(); if (classDescriptors.put(name, classDescriptor) != null) { - throw new UnsupportedOperationException("Class redeclared: " + classDescriptor.getName()); } } @@ -190,8 +201,26 @@ public class WritableScope extends JetScopeAdapter { return thisType; } + @NotNull + public Map getNamespaceDescriptors() { + if (namespaceDescriptors == null) { + namespaceDescriptors = new HashMap(); + } + return namespaceDescriptors; + } + + public void addNamespace(NamespaceDescriptor namespaceDescriptor) { + NamespaceDescriptor oldValue = getNamespaceDescriptors().put(namespaceDescriptor.getName(), namespaceDescriptor); + if (oldValue != null) { + throw new UnsupportedOperationException("Namespace redeclared: " + namespaceDescriptor.getName()); + } + } + @Override public NamespaceDescriptor getNamespace(@NotNull String name) { + NamespaceDescriptor namespaceDescriptor = getNamespaceDescriptors().get(name); + if (namespaceDescriptor != null) return namespaceDescriptor; + NamespaceDescriptor namespace = super.getNamespace(name); if (namespace != null) return namespace; for (JetScope imported : getImports()) { diff --git a/idea/src/org/jetbrains/jet/lang/resolve/java/JavaClassMembersScope.java b/idea/src/org/jetbrains/jet/lang/resolve/java/JavaClassMembersScope.java index 7f4f9854ff4..646514c330a 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/java/JavaClassMembersScope.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/java/JavaClassMembersScope.java @@ -15,13 +15,21 @@ public class JavaClassMembersScope implements JetScope { private final PsiClass psiClass; private final JavaSemanticServices semanticServices; private final boolean staticMembers; + private final DeclarationDescriptor containingDeclaration; - public JavaClassMembersScope(PsiClass psiClass, JavaSemanticServices semanticServices, boolean staticMembers) { + public JavaClassMembersScope(@NotNull DeclarationDescriptor classDescriptor, PsiClass psiClass, JavaSemanticServices semanticServices, boolean staticMembers) { + this.containingDeclaration = classDescriptor; this.psiClass = psiClass; this.semanticServices = semanticServices; this.staticMembers = staticMembers; } + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return containingDeclaration; + } + @Override public ClassDescriptor getClass(@NotNull String name) { throw new UnsupportedOperationException(); // TODO @@ -36,6 +44,7 @@ public class JavaClassMembersScope implements JetScope { } PropertyDescriptorImpl propertyDescriptor = new PropertyDescriptorImpl( + containingDeclaration, Collections.emptyList(), field.getName(), semanticServices.getTypeTransformer().transform(field.getType())); @@ -55,13 +64,16 @@ public class JavaClassMembersScope implements JetScope { if (!name.equals(method.getName())) { continue; } - PsiParameter[] parameters = method.getParameterList().getParameters(); + final PsiParameter[] parameters = method.getParameterList().getParameters(); + FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl( - null, + JavaDescriptorResolver.JAVA_ROOT, Collections.emptyList(), // TODO - name, + name + ); + functionDescriptor.initialize( Collections.emptyList(), // TODO - semanticServices.getDescriptorResolver().resolveParameterDescriptors(parameters), + semanticServices.getDescriptorResolver().resolveParameterDescriptors(functionDescriptor, parameters), semanticServices.getTypeTransformer().transform(method.getReturnType()) ); semanticServices.getTrace().recordDeclarationResolution(method, functionDescriptor); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java b/idea/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java index b260eeab7fe..55e244c75d9 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java @@ -14,6 +14,13 @@ import java.util.*; */ public class JavaDescriptorResolver { + /*package*/ static final DeclarationDescriptor JAVA_ROOT = new DeclarationDescriptorImpl(null, Collections.emptyList(), "") { + @Override + public R accept(DeclarationDescriptorVisitor visitor, D data) { + throw new UnsupportedOperationException(); // TODO + } + }; + protected final Map classDescriptorCache = new HashMap(); protected final Map namespaceDescriptorCache = new HashMap(); protected final JavaPsiFacade javaFacade; @@ -51,16 +58,20 @@ public class JavaDescriptorResolver { return classDescriptor; } - private ClassDescriptor createJavaClassDescriptor(@NotNull PsiClass psiClass) { + private ClassDescriptor createJavaClassDescriptor(@NotNull final PsiClass psiClass) { String name = psiClass.getName(); PsiModifierList modifierList = psiClass.getModifierList(); ClassDescriptorImpl classDescriptor = new ClassDescriptorImpl( + JAVA_ROOT, Collections.emptyList(), // TODO + name + ); + classDescriptor.initialize( + // TODO modifierList == null ? false : modifierList.hasModifierProperty(PsiModifier.FINAL), - name, Collections.emptyList(), getSupertypes(psiClass), - new JavaClassMembersScope(psiClass, semanticServices, false) + new JavaClassMembersScope(classDescriptor, psiClass, semanticServices, false) ); semanticServices.getTrace().recordDeclarationResolution(psiClass, classDescriptor); return classDescriptor; @@ -104,29 +115,32 @@ public class JavaDescriptorResolver { private NamespaceDescriptor createJavaNamespaceDescriptor(PsiPackage psiPackage) { NamespaceDescriptor namespaceDescriptor = new NamespaceDescriptor( + JAVA_ROOT, Collections.emptyList(), // TODO - psiPackage.getName(), - new JavaPackageScope(psiPackage.getQualifiedName(), semanticServices) + psiPackage.getName() ); + namespaceDescriptor.initialize(new JavaPackageScope(psiPackage.getQualifiedName(), namespaceDescriptor, semanticServices)); semanticServices.getTrace().recordDeclarationResolution(psiPackage, namespaceDescriptor); return namespaceDescriptor; } - private NamespaceDescriptor createJavaNamespaceDescriptor(@NotNull PsiClass psiClass) { + private NamespaceDescriptor createJavaNamespaceDescriptor(@NotNull final PsiClass psiClass) { NamespaceDescriptor namespaceDescriptor = new NamespaceDescriptor( + JAVA_ROOT, Collections.emptyList(), // TODO - psiClass.getName(), - new JavaClassMembersScope(psiClass, semanticServices, true) + psiClass.getName() ); + namespaceDescriptor.initialize(new JavaClassMembersScope(namespaceDescriptor, psiClass, semanticServices, true)); semanticServices.getTrace().recordDeclarationResolution(psiClass, namespaceDescriptor); return namespaceDescriptor; } - public List resolveParameterDescriptors(PsiParameter[] parameters) { + public List resolveParameterDescriptors(DeclarationDescriptor containingDeclaration, PsiParameter[] parameters) { List result = new ArrayList(); for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) { PsiParameter parameter = parameters[i]; result.add(new ValueParameterDescriptorImpl( + containingDeclaration, i, Collections.emptyList(), // TODO parameter.getName(), diff --git a/idea/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java b/idea/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java index 640b5f9ab0d..f0d4518ed69 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java @@ -3,6 +3,7 @@ package org.jetbrains.jet.lang.resolve.java; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.resolve.JetScopeImpl; import org.jetbrains.jet.lang.types.ClassDescriptor; +import org.jetbrains.jet.lang.types.DeclarationDescriptor; import org.jetbrains.jet.lang.types.NamespaceDescriptor; /** @@ -11,11 +12,13 @@ import org.jetbrains.jet.lang.types.NamespaceDescriptor; public class JavaPackageScope extends JetScopeImpl { private final JavaSemanticServices semanticServices; - private String packagePrefix; + private final DeclarationDescriptor containingDescriptor; + private final String packagePrefix; - public JavaPackageScope(@NotNull String packageFQN, JavaSemanticServices semanticServices) { + public JavaPackageScope(@NotNull String packageFQN, DeclarationDescriptor containingDescriptor, JavaSemanticServices semanticServices) { this.semanticServices = semanticServices; this.packagePrefix = packageFQN.isEmpty() ? "" : packageFQN + "."; + this.containingDescriptor = containingDescriptor; } @Override @@ -28,6 +31,12 @@ public class JavaPackageScope extends JetScopeImpl { return semanticServices.getDescriptorResolver().resolveNamespace(getQualifiedName(name)); } + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return containingDescriptor; + } + private String getQualifiedName(String name) { return packagePrefix + name; } diff --git a/idea/src/org/jetbrains/jet/lang/types/ClassDescriptor.java b/idea/src/org/jetbrains/jet/lang/types/ClassDescriptor.java index fd69ca5fe28..25dae46940b 100644 --- a/idea/src/org/jetbrains/jet/lang/types/ClassDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/types/ClassDescriptor.java @@ -12,5 +12,10 @@ public interface ClassDescriptor extends DeclarationDescriptor { @NotNull TypeConstructor getTypeConstructor(); + @NotNull JetScope getMemberScope(List typeArguments); + + @Override + @NotNull + DeclarationDescriptor getContainingDeclaration(); } diff --git a/idea/src/org/jetbrains/jet/lang/types/ClassDescriptorImpl.java b/idea/src/org/jetbrains/jet/lang/types/ClassDescriptorImpl.java index 5d2e80e53c4..bb567ff592d 100644 --- a/idea/src/org/jetbrains/jet/lang/types/ClassDescriptorImpl.java +++ b/idea/src/org/jetbrains/jet/lang/types/ClassDescriptorImpl.java @@ -5,7 +5,6 @@ import org.jetbrains.jet.lang.resolve.JetScope; import org.jetbrains.jet.lang.resolve.SubstitutingScope; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Map; @@ -13,22 +12,30 @@ import java.util.Map; * @author abreslav */ public class ClassDescriptorImpl extends DeclarationDescriptorImpl implements ClassDescriptor { - private final TypeConstructor typeConstructor; - private final JetScope memberDeclarations; + private TypeConstructor typeConstructor; + + private JetScope memberDeclarations; public ClassDescriptorImpl( - List attributes, boolean sealed, - String name, List typeParameters, - Collection superclasses, JetScope memberDeclarations) { - super(attributes, name); - this.typeConstructor = new TypeConstructor(attributes, sealed, name, typeParameters, superclasses); - this.memberDeclarations = memberDeclarations; + @NotNull DeclarationDescriptor containingDeclaration, + List attributes, + String name) { + super(containingDeclaration, attributes, name); } - public ClassDescriptorImpl(String name, JetScope memberDeclarations) { - this(Collections.emptyList(), true, - name, Collections.emptyList(), - Collections.singleton(JetStandardClasses.getAnyType()), memberDeclarations); +// public ClassDescriptorImpl(@NotNull DeclarationDescriptor containingDeclaration, String name, JetScope memberDeclarations) { +// this(containingDeclaration, Collections.emptyList(), name); +// this.initialize(Collections.emptyList(), true, +// name, Collections.emptyList(), +// Collections.singleton(JetStandardClasses.getAnyType()), memberDeclarations); +// } +// + public final ClassDescriptorImpl initialize(boolean sealed, + List typeParameters, + Collection superclasses, JetScope memberDeclarations) { + this.typeConstructor = new TypeConstructor(getAttributes(), sealed, getName(), typeParameters, superclasses); + this.memberDeclarations = memberDeclarations; + return this; } @Override @@ -38,6 +45,7 @@ public class ClassDescriptorImpl extends DeclarationDescriptorImpl implements Cl } @Override + @NotNull public JetScope getMemberScope(List typeArguments) { if (typeConstructor.getParameters().isEmpty()) { return memberDeclarations; diff --git a/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptor.java b/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptor.java index ae3d1957214..2f5fed12a87 100644 --- a/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptor.java @@ -1,5 +1,8 @@ package org.jetbrains.jet.lang.types; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + /** * @author abreslav */ @@ -10,8 +13,13 @@ public interface DeclarationDescriptor extends Annotated, Named { * or of the element itself). * returns this object if the current descriptor is original itself */ + @NotNull DeclarationDescriptor getOriginal(); + + @Nullable + DeclarationDescriptor getContainingDeclaration(); + R accept(DeclarationDescriptorVisitor visitor, D data); void acceptVoid(DeclarationDescriptorVisitor visitor); } diff --git a/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorImpl.java b/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorImpl.java index 974d254bcd7..8a3c6a1a91e 100644 --- a/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorImpl.java +++ b/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorImpl.java @@ -1,5 +1,7 @@ package org.jetbrains.jet.lang.types; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.resolve.DescriptorUtil; import java.util.List; @@ -10,10 +12,12 @@ import java.util.List; public abstract class DeclarationDescriptorImpl extends AnnotatedImpl implements Named, DeclarationDescriptor { private final String name; + private final DeclarationDescriptor containingDeclaration; - public DeclarationDescriptorImpl(List attributes, String name) { + public DeclarationDescriptorImpl(@Nullable DeclarationDescriptor containingDeclaration, List attributes, String name) { super(attributes); this.name = name; + this.containingDeclaration = containingDeclaration; } @Override @@ -21,11 +25,18 @@ public abstract class DeclarationDescriptorImpl extends AnnotatedImpl implements return name; } + @NotNull @Override public DeclarationDescriptor getOriginal() { return this; } + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return containingDeclaration; + } + @Override public void acceptVoid(DeclarationDescriptorVisitor visitor) { accept(visitor, null); diff --git a/idea/src/org/jetbrains/jet/lang/types/ErrorType.java b/idea/src/org/jetbrains/jet/lang/types/ErrorType.java index e26f595c8e9..df37c68408d 100644 --- a/idea/src/org/jetbrains/jet/lang/types/ErrorType.java +++ b/idea/src/org/jetbrains/jet/lang/types/ErrorType.java @@ -48,6 +48,12 @@ public class ErrorType { throw new UnsupportedOperationException(); // TODO } + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + throw new UnsupportedOperationException(); // TODO + } + }; private ErrorType() {} diff --git a/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptor.java b/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptor.java index 0e0c46bd32a..2fa493408e5 100644 --- a/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptor.java @@ -1,7 +1,6 @@ package org.jetbrains.jet.lang.types; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.List; @@ -18,6 +17,10 @@ public interface FunctionDescriptor extends DeclarationDescriptor { @NotNull Type getUnsubstitutedReturnType(); - @Nullable + @NotNull FunctionDescriptor getOriginal(); + + @Override + @NotNull + DeclarationDescriptor getContainingDeclaration(); } diff --git a/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptorImpl.java b/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptorImpl.java index a8d8adbd16f..61fa2ac707f 100644 --- a/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptorImpl.java +++ b/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptorImpl.java @@ -1,7 +1,6 @@ package org.jetbrains.jet.lang.types; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.List; @@ -9,24 +8,34 @@ import java.util.List; * @author abreslav */ public class FunctionDescriptorImpl extends DeclarationDescriptorImpl implements FunctionDescriptor { - @NotNull - private final List typeParameters; - @NotNull - private final List unsubstitutedValueParameters; - @NotNull - private final Type unsubstitutedReturnType; - @Nullable + + private List typeParameters; + + private List unsubstitutedValueParameters; + + private Type unsubstitutedReturnType; private final FunctionDescriptor original; public FunctionDescriptorImpl( - @Nullable FunctionDescriptor original, + @NotNull DeclarationDescriptor containingDeclaration, @NotNull List attributes, - String name, + String name) { + super(containingDeclaration, attributes, name); + this.original = this; + } + + public FunctionDescriptorImpl( + @NotNull FunctionDescriptor original, + @NotNull List attributes, + String name) { + super(original.getContainingDeclaration(), attributes, name); + this.original = original; + } + + public final void initialize( @NotNull List typeParameters, @NotNull List unsubstitutedValueParameters, @NotNull Type unsubstitutedReturnType) { - super(attributes, name); - this.original = original == null ? this : original; this.typeParameters = typeParameters; this.unsubstitutedValueParameters = unsubstitutedValueParameters; this.unsubstitutedReturnType = unsubstitutedReturnType; @@ -50,6 +59,7 @@ public class FunctionDescriptorImpl extends DeclarationDescriptorImpl implements return unsubstitutedReturnType; } + @NotNull @Override public FunctionDescriptor getOriginal() { return original; diff --git a/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptorUtil.java b/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptorUtil.java index ece6b9d6b40..4d19292029b 100644 --- a/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptorUtil.java +++ b/idea/src/org/jetbrains/jet/lang/types/FunctionDescriptorUtil.java @@ -38,7 +38,7 @@ public class FunctionDescriptorUtil { } @NotNull - public static List getSubstitutedValueParameters(@NotNull FunctionDescriptor functionDescriptor, @NotNull List typeArguments) { + public static List getSubstitutedValueParameters(FunctionDescriptor substitutedDescriptor, @NotNull FunctionDescriptor functionDescriptor, @NotNull List typeArguments) { List result = new ArrayList(); Map context = createSubstitutionContext(functionDescriptor, typeArguments); List unsubstitutedValueParameters = functionDescriptor.getUnsubstitutedValueParameters(); @@ -46,6 +46,7 @@ public class FunctionDescriptorUtil { ValueParameterDescriptor unsubstitutedValueParameter = unsubstitutedValueParameters.get(i); // TODO : Lazy? result.add(new ValueParameterDescriptorImpl( + substitutedDescriptor, i, unsubstitutedValueParameter.getAttributes(), unsubstitutedValueParameter.getName(), @@ -81,21 +82,16 @@ public class FunctionDescriptorUtil { if (functionDescriptor.getTypeParameters().isEmpty()) { return functionDescriptor; } - return new FunctionDescriptorImpl( + FunctionDescriptorImpl substitutedDescriptor = new FunctionDescriptorImpl( functionDescriptor, // TODO : substitute functionDescriptor.getAttributes(), - functionDescriptor.getName(), + functionDescriptor.getName()); + substitutedDescriptor.initialize( Collections.emptyList(), // TODO : questionable - getSubstitutedValueParameters(functionDescriptor, typeArguments), + getSubstitutedValueParameters(substitutedDescriptor, functionDescriptor, typeArguments), getSubstitutedReturnType(functionDescriptor, typeArguments) ); - } - - public static FunctionDescriptor getOriginal(FunctionDescriptor descriptor) { - while (descriptor.getOriginal() != null) { - descriptor = descriptor.getOriginal(); - } - return descriptor; + return substitutedDescriptor; } } diff --git a/idea/src/org/jetbrains/jet/lang/types/JetStandardClasses.java b/idea/src/org/jetbrains/jet/lang/types/JetStandardClasses.java index a7cce356886..07572e9b467 100644 --- a/idea/src/org/jetbrains/jet/lang/types/JetStandardClasses.java +++ b/idea/src/org/jetbrains/jet/lang/types/JetStandardClasses.java @@ -19,10 +19,13 @@ public class JetStandardClasses { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private static ClassDescriptor NOTHING_CLASS = new ClassDescriptorImpl( + private static NamespaceDescriptor STANDARD_CLASSES_NAMESPACE = new NamespaceDescriptor(null, Collections.emptyList(), "jet"); + + private static final ClassDescriptor NOTHING_CLASS = new ClassDescriptorImpl( + STANDARD_CLASSES_NAMESPACE, Collections.emptyList(), + "Nothing").initialize( true, - "Nothing", Collections.emptyList(), new AbstractCollection() { @Override @@ -53,9 +56,10 @@ public class JetStandardClasses { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private static final ClassDescriptor ANY = new ClassDescriptorImpl( + STANDARD_CLASSES_NAMESPACE, Collections.emptyList(), + "Any").initialize( false, - "Any", Collections.emptyList(), Collections.emptySet(), JetScope.EMPTY @@ -76,16 +80,19 @@ public class JetStandardClasses { static { for (int i = 0; i < TUPLE_COUNT; i++) { List parameters = new ArrayList(); + ClassDescriptorImpl classDescriptor = new ClassDescriptorImpl( + STANDARD_CLASSES_NAMESPACE, + Collections.emptyList(), + "Tuple" + i); for (int j = 0; j < i; j++) { parameters.add(new TypeParameterDescriptor( + classDescriptor, Collections.emptyList(), Variance.OUT_VARIANCE, "T" + j, Collections.singleton(getNullableAnyType()))); } - TUPLE[i] = new ClassDescriptorImpl( - Collections.emptyList(), + TUPLE[i] = classDescriptor.initialize( true, - "Tuple" + i, parameters, Collections.singleton(getAnyType()), STUB); } @@ -100,36 +107,49 @@ public class JetStandardClasses { static { for (int i = 0; i < FUNCTION_COUNT; i++) { - List parameters = new ArrayList(); - for (int j = 0; j < i; j++) { - parameters.add(new TypeParameterDescriptor( - Collections.emptyList(), - Variance.IN_VARIANCE, "P" + j, - Collections.singleton(getNullableAnyType()))); - } - parameters.add(new TypeParameterDescriptor( - Collections.emptyList(), - Variance.OUT_VARIANCE, "R", - Collections.singleton(getNullableAnyType()))); - FUNCTION[i] = new ClassDescriptorImpl( + ClassDescriptorImpl function = new ClassDescriptorImpl( + STANDARD_CLASSES_NAMESPACE, Collections.emptyList(), + "Function" + i); + FUNCTION[i] = function.initialize( false, - "Function" + i, - parameters, + createTypeParameters(i, function), Collections.singleton(getAnyType()), STUB); + + ClassDescriptorImpl receiverFunction = new ClassDescriptorImpl( + STANDARD_CLASSES_NAMESPACE, + Collections.emptyList(), + "ReceiverFunction" + i); + List parameters = createTypeParameters(i, receiverFunction); parameters.add(0, new TypeParameterDescriptor( + receiverFunction, Collections.emptyList(), - Variance.IN_VARIANCE, "T", - Collections.singleton(getNullableAnyType()))); - RECEIVER_FUNCTION[i] = new ClassDescriptorImpl( - Collections.emptyList(), + Variance.IN_VARIANCE, "T", + Collections.singleton(getNullableAnyType()))); + RECEIVER_FUNCTION[i] = receiverFunction.initialize( false, - "ReceiverFunction" + i, parameters, Collections.singleton(getAnyType()), STUB); } } + private static List createTypeParameters(int parameterCount, ClassDescriptorImpl function) { + List parameters = new ArrayList(); + for (int j = 0; j < parameterCount; j++) { + parameters.add(new TypeParameterDescriptor( + function, + Collections.emptyList(), + Variance.IN_VARIANCE, "P" + j, + Collections.singleton(getNullableAnyType()))); + } + parameters.add(new TypeParameterDescriptor( + function, + Collections.emptyList(), + Variance.OUT_VARIANCE, "R", + Collections.singleton(getNullableAnyType()))); + return parameters; + } + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private static final Type UNIT_TYPE = new TypeImpl(getTuple(0)); @@ -140,7 +160,7 @@ public class JetStandardClasses { /*package*/ static final JetScope STANDARD_CLASSES; static { - WritableScope writableScope = new WritableScope(JetScope.EMPTY); + WritableScope writableScope = new WritableScope(JetScope.EMPTY, STANDARD_CLASSES_NAMESPACE); STANDARD_CLASSES = writableScope; writableScope.addClassAlias("Unit", getTuple(0)); diff --git a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java index 2d2d7ca169e..25b3e7e9dd0 100644 --- a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java +++ b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java @@ -69,6 +69,8 @@ public class JetTypeInferrer { return; } + FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl(scope.getContainingDeclaration(), Collections.emptyList(), ""); + JetTypeReference returnTypeRef = expression.getReturnTypeRef(); JetTypeReference receiverTypeRef = expression.getReceiverTypeRef(); @@ -87,7 +89,7 @@ public class JetTypeInferrer { if (typeReference == null) { throw new UnsupportedOperationException("Type inference for parameters is not implemented yet"); } - PropertyDescriptor propertyDescriptor = classDescriptorResolver.resolvePropertyDescriptor(scope, parameter); + PropertyDescriptor propertyDescriptor = classDescriptorResolver.resolvePropertyDescriptor(functionDescriptor, scope, parameter); parameterDescriptors.put(parameter.getName(), propertyDescriptor); parameterTypes.add(propertyDescriptor.getType()); } @@ -95,7 +97,7 @@ public class JetTypeInferrer { if (returnTypeRef != null) { returnType = typeResolver.resolveType(scope, returnTypeRef); } else { - WritableScope writableScope = new WritableScope(scope); + WritableScope writableScope = new WritableScope(scope, functionDescriptor); for (PropertyDescriptor propertyDescriptor : parameterDescriptors.values()) { writableScope.addPropertyDescriptor(propertyDescriptor); } @@ -147,6 +149,10 @@ public class JetTypeInferrer { @Override public void visitReturnExpression(JetReturnExpression expression) { + JetExpression returnedExpression = expression.getReturnedExpression(); + if (returnedExpression != null) { + getType(scope, returnedExpression, false); + } result[0] = JetStandardClasses.getNothingType(); } @@ -535,12 +541,13 @@ public class JetTypeInferrer { if (block.isEmpty()) { return JetStandardClasses.getUnitType(); } else { - WritableScope scope = new WritableScope(outerScope); + DeclarationDescriptor containingDescriptor = outerScope.getContainingDeclaration(); + WritableScope scope = new WritableScope(outerScope, containingDescriptor); for (JetElement statement : block) { // TODO: consider other declarations if (statement instanceof JetProperty) { JetProperty property = (JetProperty) statement; - PropertyDescriptor propertyDescriptor = classDescriptorResolver.resolvePropertyDescriptor(scope, property); + PropertyDescriptor propertyDescriptor = classDescriptorResolver.resolvePropertyDescriptor(containingDescriptor, scope, property); scope.addPropertyDescriptor(propertyDescriptor); trace.recordDeclarationResolution(property, propertyDescriptor); } diff --git a/idea/src/org/jetbrains/jet/lang/types/LazySubstitutedPropertyDescriptorImpl.java b/idea/src/org/jetbrains/jet/lang/types/LazySubstitutedPropertyDescriptorImpl.java index 845b9f6d8e5..cc9b1b0c1ed 100644 --- a/idea/src/org/jetbrains/jet/lang/types/LazySubstitutedPropertyDescriptorImpl.java +++ b/idea/src/org/jetbrains/jet/lang/types/LazySubstitutedPropertyDescriptorImpl.java @@ -37,11 +37,18 @@ public class LazySubstitutedPropertyDescriptorImpl implements PropertyDescriptor return propertyDescriptor.getName(); } + @NotNull @Override public DeclarationDescriptor getOriginal() { return propertyDescriptor.getOriginal(); } + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return propertyDescriptor.getContainingDeclaration(); + } + @Override public R accept(DeclarationDescriptorVisitor visitor, D data) { return visitor.visitPropertyDescriptor(this, data); diff --git a/idea/src/org/jetbrains/jet/lang/types/LazySubstitutingFunctionDescriptor.java b/idea/src/org/jetbrains/jet/lang/types/LazySubstitutingFunctionDescriptor.java index 1c48ccebdff..b9c88ed8ca0 100644 --- a/idea/src/org/jetbrains/jet/lang/types/LazySubstitutingFunctionDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/types/LazySubstitutingFunctionDescriptor.java @@ -25,6 +25,7 @@ public class LazySubstitutingFunctionDescriptor implements FunctionDescriptor { for (TypeParameterDescriptor parameterDescriptor : functionDescriptor.getTypeParameters()) { // TODO : lazy? result.add(new TypeParameterDescriptor( + this, parameterDescriptor.getAttributes(), parameterDescriptor.getVariance(), parameterDescriptor.getName(), @@ -41,6 +42,7 @@ public class LazySubstitutingFunctionDescriptor implements FunctionDescriptor { for (int i = 0, unsubstitutedValueParametersSize = unsubstitutedValueParameters.size(); i < unsubstitutedValueParametersSize; i++) { ValueParameterDescriptor parameterDescriptor = unsubstitutedValueParameters.get(i); result.add(new ValueParameterDescriptorImpl( + this, i, parameterDescriptor.getAttributes(), parameterDescriptor.getName(), @@ -58,6 +60,7 @@ public class LazySubstitutingFunctionDescriptor implements FunctionDescriptor { return TypeSubstitutor.INSTANCE.substitute(substitutionContext, functionDescriptor.getUnsubstitutedReturnType(), Variance.OUT_VARIANCE); } + @NotNull @Override public FunctionDescriptor getOriginal() { return functionDescriptor.getOriginal(); @@ -74,6 +77,12 @@ public class LazySubstitutingFunctionDescriptor implements FunctionDescriptor { return functionDescriptor.getName(); } + @NotNull + @Override + public DeclarationDescriptor getContainingDeclaration() { + return functionDescriptor.getContainingDeclaration(); + } + @Override public R accept(DeclarationDescriptorVisitor visitor, D data) { return visitor.visitFunctionDescriptor(this, data); diff --git a/idea/src/org/jetbrains/jet/lang/types/NamespaceDescriptor.java b/idea/src/org/jetbrains/jet/lang/types/NamespaceDescriptor.java index 32503a91da8..6bf932dec9c 100644 --- a/idea/src/org/jetbrains/jet/lang/types/NamespaceDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/types/NamespaceDescriptor.java @@ -1,5 +1,7 @@ package org.jetbrains.jet.lang.types; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.resolve.JetScope; import java.util.List; @@ -9,17 +11,23 @@ import java.util.List; */ public class NamespaceDescriptor extends DeclarationDescriptorImpl { private NamespaceType namespaceType; - private final JetScope memberScope; - public NamespaceDescriptor(List attributes, String name, JetScope memberScope) { - super(attributes, name); + private JetScope memberScope; + + public NamespaceDescriptor(@Nullable DeclarationDescriptor containingDeclaration, List attributes, String name) { + super(containingDeclaration, attributes, name); + } + + public void initialize(@NotNull JetScope memberScope) { this.memberScope = memberScope; } + @NotNull public JetScope getMemberScope() { return memberScope; } + @NotNull public NamespaceType getNamespaceType() { if (namespaceType == null) { namespaceType = new NamespaceType(getName(), memberScope); diff --git a/idea/src/org/jetbrains/jet/lang/types/PropertyDescriptor.java b/idea/src/org/jetbrains/jet/lang/types/PropertyDescriptor.java index 08b7e154e0c..3c7ae7d14e9 100644 --- a/idea/src/org/jetbrains/jet/lang/types/PropertyDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/types/PropertyDescriptor.java @@ -1,10 +1,14 @@ package org.jetbrains.jet.lang.types; -import org.jetbrains.jet.lang.psi.JetDeclaration; +import org.jetbrains.annotations.NotNull; /** * @author abreslav */ public interface PropertyDescriptor extends DeclarationDescriptor { Type getType(); + + @Override + @NotNull + DeclarationDescriptor getContainingDeclaration(); } diff --git a/idea/src/org/jetbrains/jet/lang/types/PropertyDescriptorImpl.java b/idea/src/org/jetbrains/jet/lang/types/PropertyDescriptorImpl.java index 81430a61157..2fb75ddf74a 100644 --- a/idea/src/org/jetbrains/jet/lang/types/PropertyDescriptorImpl.java +++ b/idea/src/org/jetbrains/jet/lang/types/PropertyDescriptorImpl.java @@ -1,5 +1,7 @@ package org.jetbrains.jet.lang.types; +import org.jetbrains.annotations.NotNull; + import java.util.List; /** @@ -8,8 +10,8 @@ import java.util.List; public class PropertyDescriptorImpl extends DeclarationDescriptorImpl implements PropertyDescriptor { private Type type; - public PropertyDescriptorImpl(List attributes, String name, Type type) { - super(attributes, name); + public PropertyDescriptorImpl(@NotNull DeclarationDescriptor containingDeclaration, List attributes, String name, Type type) { + super(containingDeclaration, attributes, name); this.type = type; } diff --git a/idea/src/org/jetbrains/jet/lang/types/TypeParameterDescriptor.java b/idea/src/org/jetbrains/jet/lang/types/TypeParameterDescriptor.java index 07bb6efba43..0935e497969 100644 --- a/idea/src/org/jetbrains/jet/lang/types/TypeParameterDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/types/TypeParameterDescriptor.java @@ -1,5 +1,7 @@ package org.jetbrains.jet.lang.types; +import org.jetbrains.annotations.NotNull; + import java.util.Collections; import java.util.List; import java.util.Set; @@ -12,8 +14,8 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl { private final Set upperBounds; private final TypeConstructor typeConstructor; - public TypeParameterDescriptor(List attributes, Variance variance, String name, Set upperBounds) { - super(attributes, name); + public TypeParameterDescriptor(@NotNull DeclarationDescriptor containingDeclaration, List attributes, Variance variance, String name, Set upperBounds) { + super(containingDeclaration, attributes, name); this.variance = variance; this.upperBounds = upperBounds; // TODO: Should we actually pass the attributes on to the type constructor? @@ -25,8 +27,8 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl { upperBounds); } - public TypeParameterDescriptor(List attributes, Variance variance, String name) { - this(attributes, variance, name, Collections.singleton(JetStandardClasses.getNullableAnyType())); + public TypeParameterDescriptor(@NotNull DeclarationDescriptor containingDeclaration, List attributes, Variance variance, String name) { + this(containingDeclaration, attributes, variance, name, Collections.singleton(JetStandardClasses.getNullableAnyType())); } public Variance getVariance() { diff --git a/idea/src/org/jetbrains/jet/lang/types/ValueParameterDescriptorImpl.java b/idea/src/org/jetbrains/jet/lang/types/ValueParameterDescriptorImpl.java index b335fb624c1..fef96c859a3 100644 --- a/idea/src/org/jetbrains/jet/lang/types/ValueParameterDescriptorImpl.java +++ b/idea/src/org/jetbrains/jet/lang/types/ValueParameterDescriptorImpl.java @@ -1,8 +1,6 @@ package org.jetbrains.jet.lang.types; -import com.intellij.psi.PsiElement; -import org.jetbrains.jet.lang.psi.JetDeclaration; -import org.jetbrains.jet.lang.psi.JetParameterList; +import org.jetbrains.annotations.NotNull; import java.util.List; @@ -14,8 +12,8 @@ public class ValueParameterDescriptorImpl extends PropertyDescriptorImpl impleme private final boolean isVararg; private final int index; - public ValueParameterDescriptorImpl(int index, List attributes, String name, Type type, boolean hasDefaultValue, boolean isVararg) { - super(attributes, name, type); + public ValueParameterDescriptorImpl(@NotNull DeclarationDescriptor containingDeclaration, int index, List attributes, String name, Type type, boolean hasDefaultValue, boolean isVararg) { + super(containingDeclaration, attributes, name, type); this.index = index; this.hasDefaultValue = hasDefaultValue; this.isVararg = isVararg; diff --git a/idea/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java b/idea/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java index 1dab2c374a8..12c722656dc 100644 --- a/idea/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java +++ b/idea/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java @@ -542,7 +542,7 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase { public FunctionGroup getFunctionGroup(@NotNull String name) { WritableFunctionGroup writableFunctionGroup = new WritableFunctionGroup(name); for (String funDecl : FUNCTION_DECLARATIONS) { - FunctionDescriptor functionDescriptor = classDescriptorResolver.resolveFunctionDescriptor(this, JetChangeUtil.createFunction(getProject(), funDecl)); + FunctionDescriptor functionDescriptor = classDescriptorResolver.resolveFunctionDescriptor(JetStandardClasses.getAny(), this, JetChangeUtil.createFunction(getProject(), funDecl)); if (name.equals(functionDescriptor.getName())) { writableFunctionGroup.addFunction(functionDescriptor); }