Descriptors have containing declarations

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