NamespaceLikeBuilder instead of NamespaceLike

This commit is contained in:
Stepan Koltsov
2012-03-14 18:01:18 +04:00
parent 34c4808c76
commit 1127a23a1b
8 changed files with 37 additions and 66 deletions
@@ -48,15 +48,6 @@ public class MutableClassDescriptor extends MutableClassDescriptorLite {
public MutableClassDescriptor(@NotNull BindingTrace trace, @NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope outerScope, ClassKind kind) { public MutableClassDescriptor(@NotNull BindingTrace trace, @NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope outerScope, ClassKind kind) {
super(containingDeclaration, kind); super(containingDeclaration, kind);
if (containingDeclaration instanceof ClassDescriptor
|| containingDeclaration instanceof NamespaceLike
|| containingDeclaration instanceof ModuleDescriptor
|| containingDeclaration instanceof FunctionDescriptor)
{
} else {
throw new IllegalStateException();
}
RedeclarationHandler redeclarationHandler = RedeclarationHandler.DO_NOTHING; RedeclarationHandler redeclarationHandler = RedeclarationHandler.DO_NOTHING;
setScopeForMemberLookup(new WritableScopeImpl(JetScope.EMPTY, this, redeclarationHandler).setDebugName("MemberLookup").changeLockLevel(WritableScope.LockLevel.BOTH)); setScopeForMemberLookup(new WritableScopeImpl(JetScope.EMPTY, this, redeclarationHandler).setDebugName("MemberLookup").changeLockLevel(WritableScope.LockLevel.BOTH));
@@ -79,6 +79,11 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
} }
@NotNull
@Override
public DeclarationDescriptor getOwnerForChildren() {
return this;
}
@Override @Override
public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptorLite classObjectDescriptor) { public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptorLite classObjectDescriptor) {
@@ -38,6 +38,12 @@ public class NamespaceDescriptorImpl extends AbstractNamespaceDescriptorImpl imp
this.memberScope = memberScope; this.memberScope = memberScope;
} }
@NotNull
@Override
public DeclarationDescriptor getOwnerForChildren() {
return this;
}
@Override @Override
@NotNull @NotNull
public WritableScope getMemberScope() { public WritableScope getMemberScope() {
@@ -26,53 +26,10 @@ import java.util.List;
/** /**
* @author abreslav * @author abreslav
*/ */
public interface NamespaceLike extends DeclarationDescriptor { public interface NamespaceLike {
abstract class Adapter implements NamespaceLike { @NotNull
private final DeclarationDescriptor descriptor; DeclarationDescriptor getOwnerForChildren();
public Adapter(DeclarationDescriptor descriptor) {
this.descriptor = descriptor;
}
@Override
@NotNull
public DeclarationDescriptor getOriginal() {
return descriptor.getOriginal();
}
@Override
@Nullable
public DeclarationDescriptor getContainingDeclaration() {
return descriptor.getContainingDeclaration();
}
@Override
public DeclarationDescriptor substitute(TypeSubstitutor substitutor) {
return descriptor.substitute(substitutor);
}
@Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return descriptor.accept(visitor, data);
}
@Override
public void acceptVoid(DeclarationDescriptorVisitor<Void, Void> visitor) {
descriptor.acceptVoid(visitor);
}
@Override
public List<AnnotationDescriptor> getAnnotations() {
return descriptor.getAnnotations();
}
@Override
@NotNull
public String getName() {
return descriptor.getName();
}
}
@Nullable @Nullable
NamespaceDescriptorImpl getNamespace(String name); NamespaceDescriptorImpl getNamespace(String name);
@@ -102,7 +102,7 @@ public class AnalyzingUtils {
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory,
@NotNull BindingTraceContext bindingTraceContext) { @NotNull BindingTraceContext bindingTraceContext) {
ModuleDescriptor owner = new ModuleDescriptor("<module>"); final ModuleDescriptor owner = new ModuleDescriptor("<module>");
final WritableScope scope = new WritableScopeImpl( final WritableScope scope = new WritableScopeImpl(
JetScope.EMPTY, owner, JetScope.EMPTY, owner,
@@ -136,7 +136,13 @@ public class AnalyzingUtils {
}); });
TopDownAnalyzer.process(project, bindingTraceContext, scope, TopDownAnalyzer.process(project, bindingTraceContext, scope,
new NamespaceLike.Adapter(owner) { new NamespaceLike() {
@NotNull
@Override
public DeclarationDescriptor getOwnerForChildren() {
return owner;
}
@Override @Override
public NamespaceDescriptorImpl getNamespace(String name) { public NamespaceDescriptorImpl getNamespace(String name) {
@@ -144,7 +144,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 = descriptorResolver.resolveFunctionDescriptor(namespaceLike, scopeForFunctions, function, context.getTrace()); SimpleFunctionDescriptor functionDescriptor = descriptorResolver.resolveFunctionDescriptor(namespaceLike.getOwnerForChildren(), 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);
@@ -152,7 +152,7 @@ public class DeclarationResolver {
@Override @Override
public void visitProperty(JetProperty property) { public void visitProperty(JetProperty property) {
PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePropertyDescriptor(namespaceLike, scopeForPropertyInitializers, property, context.getTrace()); PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePropertyDescriptor(namespaceLike.getOwnerForChildren(), 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);
@@ -166,7 +166,7 @@ public class DeclarationResolver {
@Override @Override
public void visitObjectDeclaration(JetObjectDeclaration declaration) { public void visitObjectDeclaration(JetObjectDeclaration declaration) {
PropertyDescriptor propertyDescriptor = descriptorResolver.resolveObjectDeclarationAsPropertyDescriptor(namespaceLike, declaration, context.getObjects().get(declaration), context.getTrace()); PropertyDescriptor propertyDescriptor = descriptorResolver.resolveObjectDeclarationAsPropertyDescriptor(namespaceLike.getOwnerForChildren(), declaration, context.getObjects().get(declaration), context.getTrace());
namespaceLike.addPropertyDescriptor(propertyDescriptor); namespaceLike.addPropertyDescriptor(propertyDescriptor);
} }
@@ -169,9 +169,15 @@ public class TopDownAnalyzer {
@NotNull Project project, @NotNull Project project,
@NotNull final BindingTrace trace, @NotNull final BindingTrace trace,
@NotNull JetScope outerScope, @NotNull JetScope outerScope,
@NotNull DeclarationDescriptor containingDeclaration, @NotNull final DeclarationDescriptor containingDeclaration,
@NotNull JetObjectDeclaration object) { @NotNull JetObjectDeclaration object) {
process(project, trace, outerScope, new NamespaceLike.Adapter(containingDeclaration) { process(project, trace, outerScope, new NamespaceLike() {
@NotNull
@Override
public DeclarationDescriptor getOwnerForChildren() {
return containingDeclaration;
}
@Override @Override
public NamespaceDescriptorImpl getNamespace(String name) { public NamespaceDescriptorImpl getNamespace(String name) {
@@ -123,7 +123,7 @@ public class TypeHierarchyResolver {
@Override @Override
public void visitClass(JetClass klass) { public void visitClass(JetClass klass) {
MutableClassDescriptor mutableClassDescriptor = new MutableClassDescriptor(context.getTrace(), owner, outerScope, getClassKind(klass)); MutableClassDescriptor mutableClassDescriptor = new MutableClassDescriptor(context.getTrace(), owner.getOwnerForChildren(), outerScope, getClassKind(klass));
context.getTrace().record(FQNAME_TO_CLASS_DESCRIPTOR, JetPsiUtil.getFQName(klass), mutableClassDescriptor); context.getTrace().record(FQNAME_TO_CLASS_DESCRIPTOR, JetPsiUtil.getFQName(klass), mutableClassDescriptor);
if (klass.hasModifier(JetTokens.ENUM_KEYWORD)) { if (klass.hasModifier(JetTokens.ENUM_KEYWORD)) {
@@ -166,7 +166,7 @@ public class TypeHierarchyResolver {
} }
private MutableClassDescriptor createClassDescriptorForObject(@NotNull JetClassOrObject declaration, @NotNull NamespaceLike owner, JetScope scope, ClassKind classKind) { private MutableClassDescriptor createClassDescriptorForObject(@NotNull JetClassOrObject declaration, @NotNull NamespaceLike owner, JetScope scope, ClassKind classKind) {
MutableClassDescriptor mutableClassDescriptor = new MutableClassDescriptor(context.getTrace(), owner, scope, classKind) { MutableClassDescriptor mutableClassDescriptor = new MutableClassDescriptor(context.getTrace(), owner.getOwnerForChildren(), scope, classKind) {
@Override @Override
public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptorLite classObjectDescriptor) { public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptorLite classObjectDescriptor) {
return ClassObjectStatus.NOT_ALLOWED; return ClassObjectStatus.NOT_ALLOWED;
@@ -237,7 +237,7 @@ public class TypeHierarchyResolver {
currentOwner = namespaceDescriptor; currentOwner = namespaceDescriptor;
context.getTrace().record(REFERENCE_TARGET, nameExpression, currentOwner); context.getTrace().record(REFERENCE_TARGET, nameExpression, currentOwner.getOwnerForChildren());
context.getTrace().record(RESOLUTION_SCOPE, nameExpression, outerScope); context.getTrace().record(RESOLUTION_SCOPE, nameExpression, outerScope);
outerScope = namespaceDescriptor.getMemberScope(); outerScope = namespaceDescriptor.getMemberScope();
@@ -254,7 +254,7 @@ public class TypeHierarchyResolver {
NamespaceDescriptorImpl namespaceDescriptor = owner.getNamespace(name); NamespaceDescriptorImpl namespaceDescriptor = owner.getNamespace(name);
if (namespaceDescriptor == null) { if (namespaceDescriptor == null) {
namespaceDescriptor = new NamespaceDescriptorImpl( namespaceDescriptor = new NamespaceDescriptorImpl(
owner.getOriginal(), owner.getOwnerForChildren(),
Collections.<AnnotationDescriptor>emptyList(), // TODO: annotations Collections.<AnnotationDescriptor>emptyList(), // TODO: annotations
name name
); );