Name imports fixed

This commit is contained in:
Andrey Breslav
2011-05-27 14:30:49 +04:00
parent b950a82fcd
commit e4df21c150
6 changed files with 78 additions and 61 deletions
@@ -99,7 +99,6 @@ public class TopDownAnalyzer {
declaration.accept(new JetVisitor() { declaration.accept(new JetVisitor() {
@Override @Override
public void visitNamespace(JetNamespace namespace) { public void visitNamespace(JetNamespace namespace) {
List<JetImportDirective> importDirectives = namespace.getImportDirectives();
String name = namespace.getName(); String name = namespace.getName();
if (name == null) { if (name == null) {
@@ -118,57 +117,10 @@ public class TopDownAnalyzer {
} }
namespaceDescriptors.put(namespace, namespaceDescriptor); 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); namespaceScopes.put(namespace, namespaceScope);
for (JetImportDirective importDirective : importDirectives) { processImports(namespace, namespaceScope, outerScope);
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);
}
}
}
collectNamespacesAndClassifiers(namespaceScope, namespaceDescriptor, namespace.getDeclarations()); 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<JetImportDirective> 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() { private void createTypeConstructors() {
for (Map.Entry<JetClass, MutableClassDescriptor> entry : classes.entrySet()) { for (Map.Entry<JetClass, MutableClassDescriptor> entry : classes.entrySet()) {
JetClass jetClass = entry.getKey(); JetClass jetClass = entry.getKey();
@@ -13,8 +13,6 @@ import java.util.*;
* @author abreslav * @author abreslav
*/ */
public class WritableScopeImpl extends WritableScopeWithImports { public class WritableScopeImpl extends WritableScopeWithImports {
@NotNull
private final ErrorHandler errorHandler;
@NotNull @NotNull
private final DeclarationDescriptor ownerDeclarationDescriptor; private final DeclarationDescriptor ownerDeclarationDescriptor;
@@ -37,9 +35,8 @@ public class WritableScopeImpl extends WritableScopeWithImports {
private JetType thisType; private JetType thisType;
public WritableScopeImpl(@NotNull JetScope scope, @NotNull DeclarationDescriptor owner, @NotNull ErrorHandler errorHandler) { public WritableScopeImpl(@NotNull JetScope scope, @NotNull DeclarationDescriptor owner, @NotNull ErrorHandler errorHandler) {
super(scope); super(scope, errorHandler);
this.ownerDeclarationDescriptor = owner; this.ownerDeclarationDescriptor = owner;
this.errorHandler = errorHandler;
} }
@NotNull @NotNull
@@ -2,6 +2,7 @@ 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.ErrorHandler;
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionGroup; import org.jetbrains.jet.lang.descriptors.FunctionGroup;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
@@ -19,9 +20,12 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
@Nullable @Nullable
private List<JetScope> imports; private List<JetScope> imports;
private WritableScope currentIndividualImportScope;
protected final ErrorHandler errorHandler;
public WritableScopeWithImports(@NotNull JetScope scope) { public WritableScopeWithImports(@NotNull JetScope scope, @NotNull ErrorHandler errorHandler) {
super(scope); super(scope);
this.errorHandler = errorHandler;
} }
public WritableScopeWithImports setDebugName(@NotNull String debugName) { public WritableScopeWithImports setDebugName(@NotNull String debugName) {
@@ -41,6 +45,7 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
@Override @Override
public void importScope(@NotNull JetScope imported) { public void importScope(@NotNull JetScope imported) {
getImports().add(0, imported); getImports().add(0, imported);
currentIndividualImportScope = null;
} }
@Override @Override
@@ -89,8 +94,18 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
return null; 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 @Override
public String toString() { public String toString() {
return debugName + " for " + getContainingDeclaration(); return debugName + " for " + getContainingDeclaration();
} }
} }
@@ -2,6 +2,7 @@ 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.ErrorHandler;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.*;
@@ -13,8 +14,8 @@ import java.util.Collection;
public class WriteThroughScope extends WritableScopeWithImports { public class WriteThroughScope extends WritableScopeWithImports {
private final WritableScope writableWorker; private final WritableScope writableWorker;
public WriteThroughScope(@NotNull JetScope outerScope, @NotNull WritableScope scope) { public WriteThroughScope(@NotNull JetScope outerScope, @NotNull WritableScope scope, @NotNull ErrorHandler errorHandler) {
super(outerScope); super(outerScope, errorHandler);
this.writableWorker = scope; this.writableWorker = scope;
} }
@@ -1407,9 +1407,9 @@ public class JetTypeInferrer {
else { else {
result = selectorReturnType; result = selectorReturnType;
} }
if (selectorExpression != null && result != null) { // if (selectorExpression != null && result != null) {
trace.recordExpressionType(selectorExpression, result); // trace.recordExpressionType(selectorExpression, result);
} // }
} }
} }
+1 -1
View File
@@ -43,7 +43,7 @@ fun test(l : java.util.List<Int>) {
c : java.lang.Comparable<Int>? c : java.lang.Comparable<Int>?
// Collections.sort<Integer>(new ArrayList<Integer>()) // Collections.sort<Integer>(new ArrayList<Integer>())
//new xxx.error>Class/error>() new xxx.<error>Class</error>()
} }