From e4df21c15054fcd2c97d32abcad65b4f460ce17b Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Fri, 27 May 2011 14:30:49 +0400 Subject: [PATCH] Name imports fixed --- .../jet/lang/resolve/TopDownAnalyzer.java | 104 +++++++++--------- .../jet/lang/resolve/WritableScopeImpl.java | 5 +- .../resolve/WritableScopeWithImports.java | 17 ++- .../jet/lang/resolve/WriteThroughScope.java | 5 +- .../jet/lang/types/JetTypeInferrer.java | 6 +- idea/testData/checker/ResolveToJava.jet | 2 +- 6 files changed, 78 insertions(+), 61 deletions(-) diff --git a/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java b/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java index 703f05c0e76..a1aad553b7c 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java @@ -99,7 +99,6 @@ public class TopDownAnalyzer { declaration.accept(new JetVisitor() { @Override public void visitNamespace(JetNamespace namespace) { - List importDirectives = namespace.getImportDirectives(); String name = namespace.getName(); if (name == null) { @@ -118,57 +117,10 @@ public class TopDownAnalyzer { } namespaceDescriptors.put(namespace, namespaceDescriptor); - WritableScope namespaceScope = new WriteThroughScope(outerScope, (WritableScope) namespaceDescriptor.getMemberScope()); + WriteThroughScope namespaceScope = new WriteThroughScope(outerScope, namespaceDescriptor.getMemberScope(), trace.getErrorHandler()); namespaceScopes.put(namespace, namespaceScope); - for (JetImportDirective importDirective : importDirectives) { - if (importDirective.isAbsoluteInRootNamespace()) { - throw new UnsupportedOperationException(); - } - if (importDirective.isAllUnder()) { - JetExpression importedReference = importDirective.getImportedReference(); - if (importedReference != null) { - JetType type = semanticServices.getTypeInferrer(trace, JetFlowInformationProvider.THROW_EXCEPTION).getTypeWithNamespaces(namespaceScope, importedReference, false); - if (type != null) { - namespaceScope.importScope(type.getMemberScope()); - } - } - } else { - ClassifierDescriptor classifierDescriptor = null; - JetSimpleNameExpression referenceExpression = null; - - JetExpression importedReference = importDirective.getImportedReference(); - if (importedReference instanceof JetDotQualifiedExpression) { - JetDotQualifiedExpression reference = (JetDotQualifiedExpression) importedReference; - JetType type = semanticServices.getTypeInferrer(trace, JetFlowInformationProvider.THROW_EXCEPTION).getTypeWithNamespaces(namespaceScope, reference.getReceiverExpression(), false); - JetExpression selectorExpression = reference.getSelectorExpression(); - if (selectorExpression != null) { - referenceExpression = (JetSimpleNameExpression) selectorExpression; - String referencedName = referenceExpression.getReferencedName(); - if (type != null && referencedName != null) { - classifierDescriptor = type.getMemberScope().getClassifier(referencedName); - } - } - } - else { - assert importedReference instanceof JetSimpleNameExpression; - referenceExpression = (JetSimpleNameExpression) importedReference; - - String referencedName = referenceExpression.getReferencedName(); - if (referencedName != null) { - classifierDescriptor = outerScope.getClassifier(referencedName); - } - } - - if (classifierDescriptor != null) { - trace.recordReferenceResolution(referenceExpression, classifierDescriptor); - - String aliasName = importDirective.getAliasName(); - String importedClassifierName = aliasName != null ? aliasName : classifierDescriptor.getName(); - namespaceScope.addClassifierAlias(importedClassifierName, classifierDescriptor); - } - } - } + processImports(namespace, namespaceScope, outerScope); collectNamespacesAndClassifiers(namespaceScope, namespaceDescriptor, namespace.getDeclarations()); } @@ -200,6 +152,58 @@ public class TopDownAnalyzer { } } + private void processImports(@NotNull JetNamespace namespace, @NotNull WriteThroughScope namespaceScope, @NotNull JetScope outerScope) { + List importDirectives = namespace.getImportDirectives(); + for (JetImportDirective importDirective : importDirectives) { + if (importDirective.isAbsoluteInRootNamespace()) { + throw new UnsupportedOperationException(); + } + if (importDirective.isAllUnder()) { + JetExpression importedReference = importDirective.getImportedReference(); + if (importedReference != null) { + JetType type = semanticServices.getTypeInferrer(trace, JetFlowInformationProvider.THROW_EXCEPTION).getTypeWithNamespaces(namespaceScope, importedReference, false); + if (type != null) { + namespaceScope.importScope(type.getMemberScope()); + } + } + } else { + ClassifierDescriptor classifierDescriptor = null; + JetSimpleNameExpression referenceExpression = null; + + JetExpression importedReference = importDirective.getImportedReference(); + if (importedReference instanceof JetDotQualifiedExpression) { + JetDotQualifiedExpression reference = (JetDotQualifiedExpression) importedReference; + JetType type = semanticServices.getTypeInferrer(trace, JetFlowInformationProvider.THROW_EXCEPTION).getTypeWithNamespaces(namespaceScope, reference.getReceiverExpression(), false); + JetExpression selectorExpression = reference.getSelectorExpression(); + if (selectorExpression != null) { + referenceExpression = (JetSimpleNameExpression) selectorExpression; + String referencedName = referenceExpression.getReferencedName(); + if (type != null && referencedName != null) { + classifierDescriptor = type.getMemberScope().getClassifier(referencedName); + } + } + } + else { + assert importedReference instanceof JetSimpleNameExpression; + referenceExpression = (JetSimpleNameExpression) importedReference; + + String referencedName = referenceExpression.getReferencedName(); + if (referencedName != null) { + classifierDescriptor = outerScope.getClassifier(referencedName); + } + } + + if (classifierDescriptor != null) { + trace.recordReferenceResolution(referenceExpression, classifierDescriptor); + + String aliasName = importDirective.getAliasName(); + String importedClassifierName = aliasName != null ? aliasName : classifierDescriptor.getName(); + namespaceScope.importClassifierAlias(importedClassifierName, classifierDescriptor); + } + } + } + } + private void createTypeConstructors() { for (Map.Entry entry : classes.entrySet()) { JetClass jetClass = entry.getKey(); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/WritableScopeImpl.java b/idea/src/org/jetbrains/jet/lang/resolve/WritableScopeImpl.java index 78479fafb1a..a9295fcf212 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/WritableScopeImpl.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/WritableScopeImpl.java @@ -13,8 +13,6 @@ import java.util.*; * @author abreslav */ public class WritableScopeImpl extends WritableScopeWithImports { - @NotNull - private final ErrorHandler errorHandler; @NotNull private final DeclarationDescriptor ownerDeclarationDescriptor; @@ -37,9 +35,8 @@ public class WritableScopeImpl extends WritableScopeWithImports { private JetType thisType; public WritableScopeImpl(@NotNull JetScope scope, @NotNull DeclarationDescriptor owner, @NotNull ErrorHandler errorHandler) { - super(scope); + super(scope, errorHandler); this.ownerDeclarationDescriptor = owner; - this.errorHandler = errorHandler; } @NotNull diff --git a/idea/src/org/jetbrains/jet/lang/resolve/WritableScopeWithImports.java b/idea/src/org/jetbrains/jet/lang/resolve/WritableScopeWithImports.java index 381ab81ecbd..9316b0e3d85 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/WritableScopeWithImports.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/WritableScopeWithImports.java @@ -2,6 +2,7 @@ package org.jetbrains.jet.lang.resolve; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.ErrorHandler; import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionGroup; import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; @@ -19,9 +20,12 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement @Nullable private List imports; + private WritableScope currentIndividualImportScope; + protected final ErrorHandler errorHandler; - public WritableScopeWithImports(@NotNull JetScope scope) { + public WritableScopeWithImports(@NotNull JetScope scope, @NotNull ErrorHandler errorHandler) { super(scope); + this.errorHandler = errorHandler; } public WritableScopeWithImports setDebugName(@NotNull String debugName) { @@ -41,6 +45,7 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement @Override public void importScope(@NotNull JetScope imported) { getImports().add(0, imported); + currentIndividualImportScope = null; } @Override @@ -89,8 +94,18 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement return null; } + public void importClassifierAlias(@NotNull String importedClassifierName, @NotNull ClassifierDescriptor classifierDescriptor) { + if (currentIndividualImportScope == null) { + WritableScopeImpl writableScope = new WritableScopeImpl(JetScope.EMPTY, getContainingDeclaration(), ErrorHandler.DO_NOTHING); + importScope(writableScope); + currentIndividualImportScope = writableScope; + } + currentIndividualImportScope.addClassifierAlias(importedClassifierName, classifierDescriptor); + } + @Override public String toString() { return debugName + " for " + getContainingDeclaration(); } + } diff --git a/idea/src/org/jetbrains/jet/lang/resolve/WriteThroughScope.java b/idea/src/org/jetbrains/jet/lang/resolve/WriteThroughScope.java index 2e46c632b8a..091c0bb80fa 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/WriteThroughScope.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/WriteThroughScope.java @@ -2,6 +2,7 @@ package org.jetbrains.jet.lang.resolve; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.ErrorHandler; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.types.*; @@ -13,8 +14,8 @@ import java.util.Collection; public class WriteThroughScope extends WritableScopeWithImports { private final WritableScope writableWorker; - public WriteThroughScope(@NotNull JetScope outerScope, @NotNull WritableScope scope) { - super(outerScope); + public WriteThroughScope(@NotNull JetScope outerScope, @NotNull WritableScope scope, @NotNull ErrorHandler errorHandler) { + super(outerScope, errorHandler); this.writableWorker = scope; } diff --git a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java index 5c949ae5242..773b4e76270 100644 --- a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java +++ b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java @@ -1407,9 +1407,9 @@ public class JetTypeInferrer { else { result = selectorReturnType; } - if (selectorExpression != null && result != null) { - trace.recordExpressionType(selectorExpression, result); - } +// if (selectorExpression != null && result != null) { +// trace.recordExpressionType(selectorExpression, result); +// } } } diff --git a/idea/testData/checker/ResolveToJava.jet b/idea/testData/checker/ResolveToJava.jet index ca8cbd774dd..b8271d51043 100644 --- a/idea/testData/checker/ResolveToJava.jet +++ b/idea/testData/checker/ResolveToJava.jet @@ -43,7 +43,7 @@ fun test(l : java.util.List) { c : java.lang.Comparable? // Collections.sort(new ArrayList()) - //new xxx.error>Class/error>() + new xxx.Class() }