From 4c2e58ddf02c54b6def0de3cbb7aacc7f6516409 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Fri, 23 Sep 2011 15:27:45 +0400 Subject: [PATCH] FunctionGroups eliminated --- .../codegen/intrinsics/IntrinsicMethods.java | 8 +- .../resolve/java/JavaClassDescriptor.java | 11 ++- .../resolve/java/JavaClassMembersScope.java | 7 +- .../resolve/java/JavaDescriptorResolver.java | 7 +- .../lang/resolve/java/JavaPackageScope.java | 12 ++- .../descriptors/CallableMemberDescriptor.java | 7 ++ .../jet/lang/descriptors/ClassDescriptor.java | 3 +- .../lang/descriptors/ClassDescriptorImpl.java | 17 ++-- .../descriptors/FunctionDescriptorImpl.java | 2 +- .../jet/lang/descriptors/FunctionGroup.java | 45 ---------- .../LazySubstitutingClassDescriptor.java | 3 +- .../LazySubstitutingFunctionGroup.java | 54 ----------- .../descriptors/MutableClassDescriptor.java | 16 +--- .../lang/descriptors/PropertyDescriptor.java | 2 +- .../descriptors/WritableFunctionGroup.java | 63 ------------- .../lang/resolve/AbstractScopeAdapter.java | 5 +- .../jet/lang/resolve/OverrideResolver.java | 77 ++++++++++++---- .../jet/lang/resolve/OverridingUtil.java | 14 +-- .../jet/lang/resolve/calls/CallResolver.java | 18 ++-- .../jet/lang/resolve/scopes/ChainedScope.java | 10 +-- .../jet/lang/resolve/scopes/JetScope.java | 3 +- .../jet/lang/resolve/scopes/JetScopeImpl.java | 5 +- .../resolve/scopes/SubstitutingScope.java | 90 +++++++++---------- .../resolve/scopes/WritableScopeImpl.java | 46 +++------- .../scopes/WritableScopeWithImports.java | 19 ++-- .../resolve/scopes/WriteThroughScope.java | 12 +-- .../jetbrains/jet/lang/types/ErrorUtils.java | 26 ++---- .../jet/lang/types/JetStandardClasses.java | 11 ++- .../jet/lang/types/JetStandardLibrary.java | 17 ++-- .../jetbrains/jet/lang/types/TypeUtils.java | 2 +- .../jetbrains/jet/util/CommonSuppliers.java | 8 +- .../jet/types/JetOverridingTest.java | 4 +- .../jet/types/JetTypeCheckerTest.java | 17 ++-- 33 files changed, 237 insertions(+), 404 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/descriptors/CallableMemberDescriptor.java delete mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/descriptors/FunctionGroup.java delete mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingFunctionGroup.java delete mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/descriptors/WritableFunctionGroup.java diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java index c1817e67cc1..c8226572073 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java @@ -51,7 +51,7 @@ public class IntrinsicMethods { declareIntrinsicFunction(type, "dec", 0, DEC); } - final FunctionGroup typeInfoFunctionGroup = stdlib.getTypeInfoFunctionGroup(); + final Set typeInfoFunctionGroup = stdlib.getTypeInfoFunctions(); declareOverload(typeInfoFunctionGroup, 0, TYPEINFO); declareOverload(typeInfoFunctionGroup, 1, VALUE_TYPEINFO); @@ -108,12 +108,12 @@ public class IntrinsicMethods { private void declareIntrinsicFunction(String className, String functionName, int arity, IntrinsicMethod implementation) { JetScope memberScope = getClassMemberScope(className); - final FunctionGroup group = memberScope.getFunctionGroup(functionName); + final Set group = memberScope.getFunctions(functionName); declareOverload(group, arity, implementation); } - private void declareOverload(FunctionGroup group, int arity, IntrinsicMethod implementation) { - for (FunctionDescriptor descriptor : group.getFunctionDescriptors()) { + private void declareOverload(Set group, int arity, IntrinsicMethod implementation) { + for (FunctionDescriptor descriptor : group) { if (descriptor.getValueParameters().size() == arity) { myMethods.put(descriptor.getOriginal(), implementation); } diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaClassDescriptor.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaClassDescriptor.java index cad5c8890bb..9049d015cbe 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaClassDescriptor.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaClassDescriptor.java @@ -1,5 +1,6 @@ package org.jetbrains.jet.lang.resolve.java; +import com.google.common.collect.Sets; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; @@ -12,6 +13,7 @@ import org.jetbrains.jet.lang.types.*; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Set; /** * @author abreslav @@ -21,7 +23,7 @@ public class JavaClassDescriptor extends MutableDeclarationDescriptor implements private TypeConstructor typeConstructor; private JavaClassMembersScope unsubstitutedMemberScope; private JetType classObjectType; - private final WritableFunctionGroup constructors = new WritableFunctionGroup(""); + private final Set constructors = Sets.newLinkedHashSet(); private Modality modality; private JetType superclassType; private final ClassKind kind; @@ -60,7 +62,7 @@ public class JavaClassDescriptor extends MutableDeclarationDescriptor implements } public void addConstructor(ConstructorDescriptor constructorDescriptor) { - this.constructors.addFunction(constructorDescriptor); + this.constructors.add(constructorDescriptor); } private TypeSubstitutor createTypeSubstitutor(List typeArguments) { @@ -92,10 +94,7 @@ public class JavaClassDescriptor extends MutableDeclarationDescriptor implements @NotNull @Override - public FunctionGroup getConstructors() { -// assert typeArguments.size() == typeConstructor.getParameters().size(); -// if (typeArguments.isEmpty()) return constructors; -// return new LazySubstitutingFunctionGroup(createTypeSubstitutor(typeArguments), constructors); + public Set getConstructors() { return constructors; } diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaClassMembersScope.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaClassMembersScope.java index 23fbbacaa7b..713f828c4f3 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaClassMembersScope.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaClassMembersScope.java @@ -12,6 +12,7 @@ import org.jetbrains.jet.lang.types.TypeSubstitutor; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; /** * @author abreslav @@ -21,7 +22,7 @@ public class JavaClassMembersScope implements JetScope { private final JavaSemanticServices semanticServices; private final boolean staticMembers; private final DeclarationDescriptor containingDeclaration; - private final Map functionGroups = Maps.newHashMap(); + private final Map> functionGroups = Maps.newHashMap(); private final Map variables = Maps.newHashMap(); private final Map classifiers = Maps.newHashMap(); private Collection allDescriptors; @@ -130,8 +131,8 @@ public class JavaClassMembersScope implements JetScope { @NotNull @Override - public FunctionGroup getFunctionGroup(@NotNull String name) { - FunctionGroup functionGroup = functionGroups.get(name); + public Set getFunctions(@NotNull String name) { + Set functionGroup = functionGroups.get(name); if (functionGroup == null) { functionGroup = semanticServices.getDescriptorResolver().resolveFunctionGroup( containingDeclaration, diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java index 5b875998ccb..2c25fc730fd 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java @@ -2,6 +2,7 @@ package org.jetbrains.jet.lang.resolve.java; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.search.GlobalSearchScope; @@ -302,8 +303,8 @@ public class JavaDescriptorResolver { } @NotNull - public FunctionGroup resolveFunctionGroup(@NotNull DeclarationDescriptor owner, @NotNull PsiClass psiClass, @Nullable ClassDescriptor classDescriptor, @NotNull String methodName, boolean staticMembers) { - WritableFunctionGroup writableFunctionGroup = new WritableFunctionGroup(methodName); + public Set resolveFunctionGroup(@NotNull DeclarationDescriptor owner, @NotNull PsiClass psiClass, @Nullable ClassDescriptor classDescriptor, @NotNull String methodName, boolean staticMembers) { + Set writableFunctionGroup = Sets.newLinkedHashSet(); final Collection signatures = psiClass.getVisibleSignatures(); TypeSubstitutor typeSubstitutor = createSubstitutorForGenericSupertypes(classDescriptor); for (HierarchicalMethodSignature signature: signatures) { @@ -317,7 +318,7 @@ public class JavaDescriptorResolver { FunctionDescriptor substitutedFunctionDescriptor = resolveMethodToFunctionDescriptor(owner, psiClass, typeSubstitutor, method); if (substitutedFunctionDescriptor != null) { - writableFunctionGroup.addFunction(substitutedFunctionDescriptor); + writableFunctionGroup.add(substitutedFunctionDescriptor); } } return writableFunctionGroup; diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java index 7c43e8a8bba..2b69b86e161 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPackageScope.java @@ -4,6 +4,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl; +import java.util.Collections; +import java.util.Set; + /** * @author abreslav */ @@ -31,13 +34,8 @@ public class JavaPackageScope extends JetScopeImpl { @NotNull @Override - public FunctionGroup getFunctionGroup(@NotNull String name) { -// ClassifierDescriptor classifier = getClassifier(name); -// if (classifier instanceof ClassDescriptor) { -// ClassDescriptor classDescriptor = (ClassDescriptor) classifier; -// return classDescriptor.getConstructors(); -// } - return FunctionGroup.EMPTY; + public Set getFunctions(@NotNull String name) { + return Collections.emptySet(); } @NotNull diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/CallableMemberDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/CallableMemberDescriptor.java new file mode 100644 index 00000000000..6fff75b56b7 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/CallableMemberDescriptor.java @@ -0,0 +1,7 @@ +package org.jetbrains.jet.lang.descriptors; + +/** + * @author abreslav + */ +public interface CallableMemberDescriptor extends CallableDescriptor, MemberDescriptor { +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptor.java index 5d0f349f42c..277676de44f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptor.java @@ -9,6 +9,7 @@ import org.jetbrains.jet.lang.types.TypeProjection; import org.jetbrains.jet.lang.types.TypeSubstitutor; import java.util.List; +import java.util.Set; /** * @author abreslav @@ -25,7 +26,7 @@ public interface ClassDescriptor extends ClassifierDescriptor { JetType getSuperclassType(); @NotNull - FunctionGroup getConstructors(); + Set getConstructors(); @Nullable ConstructorDescriptor getUnsubstitutedPrimaryConstructor(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorImpl.java index 77fe77579ac..c706a032eae 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ClassDescriptorImpl.java @@ -12,6 +12,7 @@ import org.jetbrains.jet.lang.types.*; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; /** * @author abreslav @@ -20,7 +21,7 @@ public class ClassDescriptorImpl extends DeclarationDescriptorImpl implements Cl private TypeConstructor typeConstructor; private JetScope memberDeclarations; - private FunctionGroup constructors; + private Set constructors; private ConstructorDescriptor primaryConstructor; private JetType superclassType; private ReceiverDescriptor implicitReceiver; @@ -36,7 +37,7 @@ public class ClassDescriptorImpl extends DeclarationDescriptorImpl implements Cl @NotNull List typeParameters, @NotNull Collection supertypes, @NotNull JetScope memberDeclarations, - @NotNull FunctionGroup constructors, + @NotNull Set constructors, @Nullable ConstructorDescriptor primaryConstructor) { return initialize(sealed, typeParameters, supertypes, memberDeclarations, constructors, primaryConstructor, getClassType(supertypes)); } @@ -45,7 +46,7 @@ public class ClassDescriptorImpl extends DeclarationDescriptorImpl implements Cl @NotNull List typeParameters, @NotNull Collection supertypes, @NotNull JetScope memberDeclarations, - @NotNull FunctionGroup constructors, + @NotNull Set constructors, @Nullable ConstructorDescriptor primaryConstructor, @Nullable JetType superclassType) { this.typeConstructor = new TypeConstructorImpl(this, getAnnotations(), sealed, getName(), typeParameters, supertypes); @@ -53,7 +54,6 @@ public class ClassDescriptorImpl extends DeclarationDescriptorImpl implements Cl this.constructors = constructors; this.primaryConstructor = primaryConstructor; this.superclassType = superclassType; -// assert !constructors.isEmpty() || primaryConstructor == null; return this; } @@ -103,13 +103,8 @@ public class ClassDescriptorImpl extends DeclarationDescriptorImpl implements Cl @NotNull @Override - public FunctionGroup getConstructors() { -// assert typeArguments.size() == getTypeConstructor().getParameters().size() : "Argument list length mismatch for " + getName(); -// if (typeArguments.size() == 0) { -// return constructors; -// } -// Map substitutionContext = TypeUtils.buildSubstitutionContext(getTypeConstructor().getParameters(), typeArguments); - return constructors;// LazySubstitutingFunctionGroup(TypeSubstitutor.create(substitutionContext), constructors); + public Set getConstructors() { + return constructors; } @NotNull diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/FunctionDescriptorImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/FunctionDescriptorImpl.java index 74fa8b21abe..df0e777739e 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/FunctionDescriptorImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/FunctionDescriptorImpl.java @@ -20,7 +20,7 @@ import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor /** * @author abreslav */ -public class FunctionDescriptorImpl extends DeclarationDescriptorImpl implements FunctionDescriptor { +public class FunctionDescriptorImpl extends DeclarationDescriptorImpl implements FunctionDescriptor, CallableMemberDescriptor { private List typeParameters; private List unsubstitutedValueParameters; diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/FunctionGroup.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/FunctionGroup.java deleted file mode 100644 index 1dcfc99cf83..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/FunctionGroup.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.jetbrains.jet.lang.descriptors; - -import org.jetbrains.annotations.NotNull; - -import java.util.Collections; -import java.util.Set; - -/** - * @author abreslav - */ -public interface FunctionGroup extends Named { - FunctionGroup EMPTY = new FunctionGroup() { - @NotNull - @Override - public String getName() { - return ""; - } - - @Override - public boolean isEmpty() { - return true; - } - - @NotNull - @Override - public Set getFunctionDescriptors() { - return Collections.emptySet(); - } - - @Override - public String toString() { - return "EMPTY"; - } - }; - - - @NotNull - @Override - String getName(); - - boolean isEmpty(); - - @NotNull - Set getFunctionDescriptors(); -} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingClassDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingClassDescriptor.java index 2c1f3e8bca4..b41605176ce 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingClassDescriptor.java @@ -10,6 +10,7 @@ import org.jetbrains.jet.lang.types.*; import java.util.Collection; import java.util.List; +import java.util.Set; /** * @author abreslav @@ -103,7 +104,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor { @NotNull @Override - public FunctionGroup getConstructors() { + public Set getConstructors() { throw new UnsupportedOperationException(); // TODO } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingFunctionGroup.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingFunctionGroup.java deleted file mode 100644 index c10e718d60c..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/LazySubstitutingFunctionGroup.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.jetbrains.jet.lang.descriptors; - -import com.google.common.collect.Sets; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.types.TypeSubstitutor; - -import java.util.Collections; -import java.util.Set; - -/** - * @author abreslav - */ -public class LazySubstitutingFunctionGroup implements FunctionGroup { - private final TypeSubstitutor substitutor; - private final FunctionGroup functionGroup; - private Set functionDescriptors; - - public LazySubstitutingFunctionGroup(TypeSubstitutor substitutor, FunctionGroup functionGroup) { - this.substitutor = substitutor; - this.functionGroup = functionGroup; - } - - @NotNull - @Override - public String getName() { - return functionGroup.getName(); - } - - @Override - public boolean isEmpty() { - return functionGroup.isEmpty(); - } - - @NotNull - @Override - public Set getFunctionDescriptors() { - if (functionDescriptors == null) { - if (substitutor.isEmpty()) { - functionDescriptors = functionGroup.getFunctionDescriptors(); - } - else { - Set functionDescriptorSet = Sets.newLinkedHashSet(); - for (FunctionDescriptor descriptor : functionGroup.getFunctionDescriptors()) { - FunctionDescriptor substitute = descriptor.substitute(substitutor); - if (substitute != null) { - functionDescriptorSet.add(substitute); - } - } - functionDescriptors = Collections.unmodifiableSet(functionDescriptorSet); - } - } - return functionDescriptors; - } -} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/MutableClassDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/MutableClassDescriptor.java index cf2103f43bf..f1a36827bad 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/MutableClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/MutableClassDescriptor.java @@ -22,7 +22,7 @@ import java.util.*; */ public class MutableClassDescriptor extends MutableDeclarationDescriptor implements ClassDescriptor, NamespaceLike { private ConstructorDescriptor primaryConstructor; - private final WritableFunctionGroup constructors = new WritableFunctionGroup(""); + private final Set constructors = Sets.newLinkedHashSet(); private final Set functions = Sets.newHashSet(); private final Set properties = Sets.newHashSet(); private List typeParameters = Lists.newArrayList(); @@ -84,7 +84,7 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme public void addConstructor(@NotNull ConstructorDescriptor constructorDescriptor) { assert constructorDescriptor.getContainingDeclaration() == this; // assert constructorDescriptor.getTypeParameters().size() == getTypeConstructor().getParameters().size(); - constructors.addFunction(constructorDescriptor); + constructors.add(constructorDescriptor); if (defaultType != null) { // constructorDescriptor.getTypeParameters().addAll(typeParameters); ((ConstructorDescriptorImpl) constructorDescriptor).setReturnType(getDefaultType()); @@ -168,7 +168,7 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme typeParameters, supertypes); scopeForMemberResolution.setImplicitReceiver(new ClassReceiver(this)); - for (FunctionDescriptor functionDescriptor : constructors.getFunctionDescriptors()) { + for (FunctionDescriptor functionDescriptor : constructors) { ((ConstructorDescriptorImpl) functionDescriptor).setReturnType(getDefaultType()); } } @@ -201,15 +201,7 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme @NotNull @Override - public FunctionGroup getConstructors() { - // TODO : Duplicates ClassDescriptorImpl -// assert typeArguments.size() == getTypeConstructor().getParameters().size(); - -// if (typeArguments.size() == 0) { -// return constructors; -// } -// Map substitutionContext = TypeUtils.buildSubstitutionContext(getTypeConstructor().getParameters(), typeArguments); -// return new LazySubstitutingFunctionGroup(TypeSubstitutor.create(substitutionContext), constructors); + public Set getConstructors() { return constructors; } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/PropertyDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/PropertyDescriptor.java index 40c15c61e9a..ee6592938a8 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/PropertyDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/PropertyDescriptor.java @@ -17,7 +17,7 @@ import java.util.Set; /** * @author abreslav */ -public class PropertyDescriptor extends VariableDescriptorImpl implements MemberDescriptor { +public class PropertyDescriptor extends VariableDescriptorImpl implements CallableMemberDescriptor { private final Modality modality; private final boolean isVar; diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/WritableFunctionGroup.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/WritableFunctionGroup.java deleted file mode 100644 index 1892b5984a1..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/WritableFunctionGroup.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.jetbrains.jet.lang.descriptors; - -import com.google.common.collect.Sets; -import org.jetbrains.annotations.NotNull; - -import java.util.Collections; -import java.util.Set; - -/** - * @author abreslav - */ -public class WritableFunctionGroup implements FunctionGroup { - private final String name; - private Set functionDescriptors; - private Set unmodifiableFunctionDescriptors; - - public WritableFunctionGroup(String name) { - this.name = name; - } - - @NotNull - @Override - public String getName() { - return name; - } - - @Override - public String toString() { - return "FunctionGroup{" + - "name='" + name + '\'' + - '}'; - } - - public void addFunction(@NotNull FunctionDescriptor functionDescriptor) { - getWritableFunctionDescriptors().add(functionDescriptor); - } - - @Override - @NotNull - public Set getFunctionDescriptors() { - if (unmodifiableFunctionDescriptors == null) { - unmodifiableFunctionDescriptors = Collections.unmodifiableSet(getWritableFunctionDescriptors()); - } - return unmodifiableFunctionDescriptors; - } - - private Set getWritableFunctionDescriptors() { - if (functionDescriptors == null) { - functionDescriptors = Sets.newLinkedHashSet(); - } - return functionDescriptors; - } - - @Override - public boolean isEmpty() { - return functionDescriptors == null || functionDescriptors.isEmpty(); - } - - public void addAllFunctions(@NotNull FunctionGroup functionGroup) { - if (functionGroup.isEmpty()) return; - getWritableFunctionDescriptors().addAll(functionGroup.getFunctionDescriptors()); - } -} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/AbstractScopeAdapter.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/AbstractScopeAdapter.java index f9624422740..2d5218a40a3 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/AbstractScopeAdapter.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/AbstractScopeAdapter.java @@ -7,6 +7,7 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope; import java.util.Collection; import java.util.List; +import java.util.Set; /** * @author abreslav @@ -28,8 +29,8 @@ public abstract class AbstractScopeAdapter implements JetScope { @NotNull @Override - public FunctionGroup getFunctionGroup(@NotNull String name) { - return getWorkerScope().getFunctionGroup(name); + public Set getFunctions(@NotNull String name) { + return getWorkerScope().getFunctions(name); } @Override diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverrideResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverrideResolver.java index d0371925643..e2f0e6aa178 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverrideResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverrideResolver.java @@ -1,5 +1,6 @@ package org.jetbrains.jet.lang.resolve; +import com.google.common.collect.Multimap; import com.google.common.collect.Sets; import com.intellij.lang.ASTNode; import com.intellij.psi.PsiElement; @@ -8,7 +9,9 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.JetTypeChecker; import org.jetbrains.jet.lexer.JetTokens; +import org.jetbrains.jet.util.CommonSuppliers; import java.util.Collection; import java.util.Map; @@ -63,8 +66,8 @@ public class OverrideResolver { @Nullable private FunctionDescriptor findFunctionOverridableBy(@NotNull FunctionDescriptor declaredFunction, @NotNull JetType supertype) { - FunctionGroup functionGroup = supertype.getMemberScope().getFunctionGroup(declaredFunction.getName()); - for (FunctionDescriptor functionDescriptor : functionGroup.getFunctionDescriptors()) { + Set functionGroup = supertype.getMemberScope().getFunctions(declaredFunction.getName()); + for (FunctionDescriptor functionDescriptor : functionGroup) { if (OverridingUtil.isOverridableBy(context.getSemanticServices().getTypeChecker(), functionDescriptor, declaredFunction).isSuccess()) { return functionDescriptor; } @@ -94,29 +97,67 @@ public class OverrideResolver { } protected void checkOverridesInAClass(MutableClassDescriptor classDescriptor, JetClassOrObject klass) { - - Set inheritedFunctions = Sets.newLinkedHashSet(); - Set inheritedProperties = Sets.newLinkedHashSet(); + Set inheritedFunctions = Sets.newLinkedHashSet(); for (JetType supertype : classDescriptor.getSupertypes()) { for (DeclarationDescriptor descriptor : supertype.getMemberScope().getAllDescriptors()) { - if (descriptor instanceof FunctionDescriptor) { - FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor; - inheritedFunctions.add(functionDescriptor); - } - else if (descriptor instanceof PropertyDescriptor) { - PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor; - inheritedProperties.add(propertyDescriptor); + if (descriptor instanceof CallableMemberDescriptor) { + CallableMemberDescriptor memberDescriptor = (CallableMemberDescriptor) descriptor; + inheritedFunctions.add(memberDescriptor); } } } - OverridingUtil.filterOverrides(inheritedFunctions); - OverridingUtil.filterOverrides(inheritedProperties); + Set filteredMembers = OverridingUtil.filterOverrides(inheritedFunctions); -// System.out.println(classDescriptor); -// println(inheritedFunctions); -// println(inheritedProperties); + Multimap factoredMembers = CommonSuppliers.newLinkedHashSetHashSetMultimap(); + JetTypeChecker typeChecker = context.getSemanticServices().getTypeChecker(); + for (CallableMemberDescriptor one : filteredMembers) { + if (factoredMembers.values().contains(one)) continue; + for (CallableMemberDescriptor another : filteredMembers) { + if (one == another) continue; + factoredMembers.put(one, one); + if (OverridingUtil.isOverridableBy(typeChecker, one, another).isSuccess() + || OverridingUtil.isOverridableBy(typeChecker, another, one).isSuccess()) { + factoredMembers.put(one, another); + } + } + } + Set mustBeOverridden = Sets.newLinkedHashSet(); + + for (CallableMemberDescriptor key : factoredMembers.keySet()) { + Collection mutuallyOverridable = factoredMembers.get(key); + +// System.out.println(key); +// println(mutuallyOverridable); + + int implementationCount = 0; + for (CallableMemberDescriptor member : mutuallyOverridable) { + if (member.getModality() != Modality.ABSTRACT) { + implementationCount++; + } + } + + if (implementationCount != 1) { + mustBeOverridden.addAll(mutuallyOverridable); + } + } + + Set actuallyOverridden = Sets.newHashSet(); + for (FunctionDescriptor declaredFunction : classDescriptor.getFunctions()) { + actuallyOverridden.addAll(declaredFunction.getOverriddenDescriptors()); + } + + for (PropertyDescriptor declaredProperty : classDescriptor.getProperties()) { + actuallyOverridden.addAll(declaredProperty.getOverriddenDescriptors()); + } + + mustBeOverridden.removeAll(actuallyOverridden); + + System.out.println(classDescriptor); + println(mustBeOverridden); + System.out.println("Actually overridden:"); + println(actuallyOverridden); for (FunctionDescriptor declaredFunction : classDescriptor.getFunctions()) { checkOverrideForFunction(declaredFunction); @@ -160,7 +201,7 @@ public class OverrideResolver { } } - private void println(Set inheritedProperties) { + private void println(Collection inheritedProperties) { for (CallableDescriptor inheritedProperty : inheritedProperties) { System.out.println(" " + inheritedProperty); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverridingUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverridingUtil.java index 2ca2700119e..087cbb7d6c4 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverridingUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverridingUtil.java @@ -114,7 +114,6 @@ public class OverridingUtil { TypeParameterDescriptor superTypeParameter = superTypeParameters.get(i); TypeParameterDescriptor subTypeParameter = subTypeParameters.get(i); - if (!JetTypeImpl.equalTypes(superTypeParameter.getBoundsAsType(), subTypeParameter.getBoundsAsType(), axioms)) { return OverrideCompatibilityInfo.boundsMismatch(superTypeParameter, subTypeParameter); } @@ -133,12 +132,13 @@ public class OverridingUtil { // TODO : Default values, varargs etc - TypeSubstitutor typeSubstitutor = TypeSubstitutor.create(substitutionContext); - JetType substitutedSuperReturnType = typeSubstitutor.substitute(superDescriptor.getReturnType(), Variance.OUT_VARIANCE); - assert substitutedSuperReturnType != null; - if (!typeChecker.isSubtypeOf(subDescriptor.getReturnType(), substitutedSuperReturnType)) { - return OverrideCompatibilityInfo.returnTypeMismatch(substitutedSuperReturnType, subDescriptor.getReturnType()); - } + // This code compares return types, but they are not a part of the signature, so this code does not belong here +// TypeSubstitutor typeSubstitutor = TypeSubstitutor.create(substitutionContext); +// JetType substitutedSuperReturnType = typeSubstitutor.substitute(superDescriptor.getReturnType(), Variance.OUT_VARIANCE); +// assert substitutedSuperReturnType != null; +// if (!typeChecker.isSubtypeOf(subDescriptor.getReturnType(), substitutedSuperReturnType)) { +// return OverrideCompatibilityInfo.returnTypeMismatch(substitutedSuperReturnType, subDescriptor.getReturnType()); +// } return OverrideCompatibilityInfo.success(); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CallResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CallResolver.java index d2649b587c6..f0423457999 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CallResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CallResolver.java @@ -138,7 +138,7 @@ public class CallResolver { DeclarationDescriptor declarationDescriptor = constructedType.getConstructor().getDeclarationDescriptor(); if (declarationDescriptor instanceof ClassDescriptor) { ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor; - Set constructors = classDescriptor.getConstructors().getFunctionDescriptors(); + Set constructors = classDescriptor.getConstructors(); if (constructors.isEmpty()) { // trace.getErrorHandler().genericError(reportAbsenceOn, "This class does not have a constructor"); trace.report(NO_CONSTRUCTOR.on(reportAbsenceOn)); @@ -159,7 +159,7 @@ public class CallResolver { ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration; - Set constructors = classDescriptor.getConstructors().getFunctionDescriptors(); + Set constructors = classDescriptor.getConstructors(); if (constructors.isEmpty()) { // trace.getErrorHandler().genericError(reportAbsenceOn, "This class does not have a constructor"); trace.report(NO_CONSTRUCTOR.on(reportAbsenceOn)); @@ -630,7 +630,7 @@ public class CallResolver { private List findCandidatesByExactSignature(JetScope scope, ReceiverDescriptor receiver, String name, List parameterTypes) { List result = Lists.newArrayList(); if (receiver != NO_RECEIVER) { - Set extensionFunctionDescriptors = scope.getFunctionGroup(name).getFunctionDescriptors(); + Set extensionFunctionDescriptors = scope.getFunctions(name); List nonlocal = Lists.newArrayList(); List local = Lists.newArrayList(); TaskPrioritizer.splitLexicallyLocalDescriptors(extensionFunctionDescriptors, scope.getContainingDeclaration(), local, nonlocal); @@ -640,7 +640,7 @@ public class CallResolver { return result; } - Set functionDescriptors = receiver.getType().getMemberScope().getFunctionGroup(name).getFunctionDescriptors(); + Set functionDescriptors = receiver.getType().getMemberScope().getFunctions(name); if (lookupExactSignature(functionDescriptors, parameterTypes, result)) { return result; @@ -649,7 +649,7 @@ public class CallResolver { return result; } else { - lookupExactSignature(scope.getFunctionGroup(name).getFunctionDescriptors(), parameterTypes, result); + lookupExactSignature(scope.getFunctions(name), parameterTypes, result); return result; } } @@ -696,7 +696,7 @@ public class CallResolver { @NotNull @Override protected Collection getNonExtensionsByName(JetScope scope, String name) { - Set functions = Sets.newLinkedHashSet(scope.getFunctionGroup(name).getFunctionDescriptors()); + Set functions = Sets.newLinkedHashSet(scope.getFunctions(name)); for (Iterator iterator = functions.iterator(); iterator.hasNext(); ) { FunctionDescriptor functionDescriptor = iterator.next(); if (functionDescriptor.getReceiver() != NO_RECEIVER) { @@ -713,7 +713,7 @@ public class CallResolver { @Override protected Collection getMembersByName(@NotNull ReceiverDescriptor receiver, String name) { JetScope receiverScope = receiver.getType().getMemberScope(); - Set members = Sets.newHashSet(receiverScope.getFunctionGroup(name).getFunctionDescriptors()); + Set members = Sets.newHashSet(receiverScope.getFunctions(name)); addConstructors(receiverScope, name, members); addVariableAsFunction(receiverScope, name, members, false); return members; @@ -722,7 +722,7 @@ public class CallResolver { @NotNull @Override protected Collection getExtensionsByName(JetScope scope, String name) { - Set extensionFunctions = Sets.newHashSet(scope.getFunctionGroup(name).getFunctionDescriptors()); + Set extensionFunctions = Sets.newHashSet(scope.getFunctions(name)); for (Iterator iterator = extensionFunctions.iterator(); iterator.hasNext(); ) { FunctionDescriptor descriptor = iterator.next(); if (descriptor.getReceiver() == NO_RECEIVER) { @@ -743,7 +743,7 @@ public class CallResolver { ClassifierDescriptor classifier = scope.getClassifier(name); if (classifier instanceof ClassDescriptor && !ErrorUtils.isError(classifier.getTypeConstructor())) { ClassDescriptor classDescriptor = (ClassDescriptor) classifier; - functions.addAll(classDescriptor.getConstructors().getFunctionDescriptors()); + functions.addAll(classDescriptor.getConstructors()); } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ChainedScope.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ChainedScope.java index 5b61003165e..6e457d2ebce 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ChainedScope.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/ChainedScope.java @@ -9,6 +9,7 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Set; /** * @author abreslav @@ -56,16 +57,15 @@ public class ChainedScope implements JetScope { @NotNull @Override - public FunctionGroup getFunctionGroup(@NotNull String name) { + public Set getFunctions(@NotNull String name) { if (scopeChain.length == 0) { - return FunctionGroup.EMPTY; + return Collections.emptySet(); } - WritableFunctionGroup result = new WritableFunctionGroup(name); + Set result = Sets.newLinkedHashSet(); for (JetScope jetScope : scopeChain) { - result.addAllFunctions(jetScope.getFunctionGroup(name)); + result.addAll(jetScope.getFunctions(name)); } - return result; } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/JetScope.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/JetScope.java index eb5bfb96890..6f4495e1bef 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/JetScope.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/JetScope.java @@ -7,6 +7,7 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import java.util.Collection; import java.util.List; +import java.util.Set; /** * @author abreslav @@ -35,7 +36,7 @@ public interface JetScope { VariableDescriptor getVariable(@NotNull String name); @NotNull - FunctionGroup getFunctionGroup(@NotNull String name); + Set getFunctions(@NotNull String name); @NotNull DeclarationDescriptor getContainingDeclaration(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.java index 8eeec10a018..b738d76a50c 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/JetScopeImpl.java @@ -7,6 +7,7 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Set; /** * @author abreslav @@ -35,8 +36,8 @@ public abstract class JetScopeImpl implements JetScope { @NotNull @Override - public FunctionGroup getFunctionGroup(@NotNull String name) { - return FunctionGroup.EMPTY; + public Set getFunctions(@NotNull String name) { + return Collections.emptySet(); } @NotNull diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/SubstitutingScope.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/SubstitutingScope.java index fb7497551f0..f09364216e1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/SubstitutingScope.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/SubstitutingScope.java @@ -8,9 +8,7 @@ import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import org.jetbrains.jet.lang.types.TypeSubstitutor; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @author abreslav @@ -20,7 +18,7 @@ public class SubstitutingScope implements JetScope { private final JetScope workerScope; private final TypeSubstitutor substitutor; - private Map functionGroups = null; + private Map substitutedDescriptors = null; private Collection allDescriptors = null; public SubstitutingScope(JetScope workerScope, @NotNull TypeSubstitutor substitutor) { @@ -28,26 +26,54 @@ public class SubstitutingScope implements JetScope { this.substitutor = substitutor; } - @Override - public VariableDescriptor getVariable(@NotNull String name) { - VariableDescriptor variable = workerScope.getVariable(name); - if (variable == null || substitutor.isEmpty()) { - return variable; + @Nullable + private D substitute(@Nullable D descriptor) { + if (descriptor == null) return null; + if (substitutor.isEmpty()) return descriptor; + + if (substitutedDescriptors == null) { + substitutedDescriptors = Maps.newHashMap(); } - return variable.substitute(substitutor); + DeclarationDescriptor substituted = substitutedDescriptors.get(descriptor); + if (substituted == null) { + substituted = descriptor.substitute(substitutor); + substitutedDescriptors.put(descriptor, substituted); + } + //noinspection unchecked + return (D) substituted; + } + + @NotNull + private Set substitute(@NotNull Set descriptors) { + if (substitutor.isEmpty()) return descriptors; + if (descriptors.isEmpty()) return descriptors; + + Set result = Sets.newHashSet(); + for (D descriptor : descriptors) { + D substitute = substitute(descriptor); + if (substitute != null) { + result.add(substitute); + } + } + + return result; + } + + @Override + public VariableDescriptor getVariable(@NotNull String name) { + return substitute(workerScope.getVariable(name)); } @Override public ClassifierDescriptor getClassifier(@NotNull String name) { - ClassifierDescriptor descriptor = workerScope.getClassifier(name); - if (descriptor == null) { - return null; - } - if (descriptor instanceof ClassDescriptor) { - return new LazySubstitutingClassDescriptor((ClassDescriptor) descriptor, substitutor); - } - throw new UnsupportedOperationException(); // TODO + return substitute(workerScope.getClassifier(name)); + } + + @NotNull + @Override + public Set getFunctions(@NotNull String name) { + return substitute(workerScope.getFunctions(name)); } @Override @@ -66,32 +92,6 @@ public class SubstitutingScope implements JetScope { throw new UnsupportedOperationException(); // TODO } - @NotNull - @Override - public FunctionGroup getFunctionGroup(@NotNull String name) { - if (substitutor.isEmpty()) { - return workerScope.getFunctionGroup(name); - } - if (functionGroups == null) { - functionGroups = Maps.newHashMap(); - } - FunctionGroup cachedGroup = functionGroups.get(name); - if (cachedGroup != null) { - return cachedGroup; - } - - FunctionGroup functionGroup = workerScope.getFunctionGroup(name); - FunctionGroup result; - if (functionGroup.isEmpty()) { - result = FunctionGroup.EMPTY; - } - else { - result = new LazySubstitutingFunctionGroup(substitutor, functionGroup); - } - functionGroups.put(name, result); - return result; - } - @NotNull @Override public DeclarationDescriptor getContainingDeclaration() { @@ -121,7 +121,7 @@ public class SubstitutingScope implements JetScope { if (allDescriptors == null) { allDescriptors = Sets.newHashSet(); for (DeclarationDescriptor descriptor : workerScope.getAllDescriptors()) { - DeclarationDescriptor substitute = descriptor.substitute(substitutor); + DeclarationDescriptor substitute = substitute(descriptor); assert substitute != null; allDescriptors.add(substitute); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeImpl.java index 9fa55238319..b63ee731480 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeImpl.java @@ -1,13 +1,12 @@ package org.jetbrains.jet.lang.resolve.scopes; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; +import com.google.common.collect.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; +import org.jetbrains.jet.util.CommonSuppliers; import java.util.*; @@ -27,7 +26,7 @@ public class WritableScopeImpl extends WritableScopeWithImports { private Map propertyDescriptorsByFieldNames; @Nullable - private Map functionGroups; + private SetMultimap functionGroups; @Nullable private Map variableClassOrNamespaceDescriptors; @@ -144,13 +143,6 @@ public class WritableScopeImpl extends WritableScopeWithImports { return (VariableDescriptor) descriptor; } -// if (implicitReceiver != null) { -// VariableDescriptor variable = getImplicitReceiver().getMemberScope().getVariable(name); -// if (variable != null) { -// return variable; -// } -// } - VariableDescriptor variableDescriptor = getWorkerScope().getVariable(name); if (variableDescriptor != null) { return variableDescriptor; @@ -159,45 +151,27 @@ public class WritableScopeImpl extends WritableScopeWithImports { } @NotNull - private Map getFunctionGroups() { + private SetMultimap getFunctionGroups() { if (functionGroups == null) { - functionGroups = new HashMap(); + functionGroups = CommonSuppliers.newLinkedHashSetHashSetMultimap(); } return functionGroups; } @Override public void addFunctionDescriptor(@NotNull FunctionDescriptor functionDescriptor) { - String name = functionDescriptor.getName(); - Map functionGroups = getFunctionGroups(); - - @Nullable - WritableFunctionGroup functionGroup = functionGroups.get(name); - if (functionGroup == null) { - functionGroup = new WritableFunctionGroup(name); - functionGroups.put(name, functionGroup); - } - functionGroup.addFunction(functionDescriptor); + getFunctionGroups().put(functionDescriptor.getName(), functionDescriptor); allDescriptors.add(functionDescriptor); } @Override @NotNull - public FunctionGroup getFunctionGroup(@NotNull String name) { - WritableFunctionGroup result = new WritableFunctionGroup(name); - - FunctionGroup functionGroup = getFunctionGroups().get(name); - if (functionGroup != null) { - result.addAllFunctions(functionGroup); - } - -// if (implicitReceiver != null) { -// result.addAllFunctions(getImplicitReceiver().getMemberScope().getFunctionGroup(name)); -// } + public Set getFunctions(@NotNull String name) { + Set result = Sets.newLinkedHashSet(getFunctionGroups().get(name)); - result.addAllFunctions(getWorkerScope().getFunctionGroup(name)); + result.addAll(getWorkerScope().getFunctions(name)); - result.addAllFunctions(super.getFunctionGroup(name)); + result.addAll(super.getFunctions(name)); return result; } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.java index 00dee3bc289..b88a8d17be3 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WritableScopeWithImports.java @@ -1,11 +1,17 @@ package org.jetbrains.jet.lang.resolve.scopes; +import com.google.common.collect.Sets; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.*; +import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; +import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; +import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Set; /** * @author abreslav @@ -58,16 +64,13 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement @NotNull @Override - public FunctionGroup getFunctionGroup(@NotNull String name) { + public Set getFunctions(@NotNull String name) { if (getImports().isEmpty()) { - return FunctionGroup.EMPTY; + return Collections.emptySet(); } - WritableFunctionGroup result = new WritableFunctionGroup(name); + Set result = Sets.newLinkedHashSet(); for (JetScope imported : getImports()) { - FunctionGroup importedFunctions = imported.getFunctionGroup(name); - if (!importedFunctions.isEmpty()) { - result.addAllFunctions(importedFunctions); - } + result.addAll(imported.getFunctions(name)); } return result; } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WriteThroughScope.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WriteThroughScope.java index e3082888951..fb5e6e6a8b5 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WriteThroughScope.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/scopes/WriteThroughScope.java @@ -1,12 +1,14 @@ package org.jetbrains.jet.lang.resolve.scopes; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import java.util.Collection; +import java.util.Set; /** * @author abreslav @@ -46,14 +48,14 @@ public class WriteThroughScope extends WritableScopeWithImports { @Override @NotNull - public FunctionGroup getFunctionGroup(@NotNull String name) { - WritableFunctionGroup result = new WritableFunctionGroup(name); + public Set getFunctions(@NotNull String name) { + Set result = Sets.newLinkedHashSet(); - result.addAllFunctions(writableWorker.getFunctionGroup(name)); + result.addAll(writableWorker.getFunctions(name)); - result.addAllFunctions(getWorkerScope().getFunctionGroup(name)); + result.addAll(getWorkerScope().getFunctions(name)); - result.addAllFunctions(super.getFunctionGroup(name)); // Imports + result.addAll(super.getFunctions(name)); // Imports return result; } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/ErrorUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/ErrorUtils.java index ebcff036fb7..e727617224f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/ErrorUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/ErrorUtils.java @@ -43,7 +43,7 @@ public class ErrorUtils { @NotNull @Override - public FunctionGroup getFunctionGroup(@NotNull String name) { + public Set getFunctions(@NotNull String name) { return ERROR_FUNCTION_GROUP; } @@ -77,33 +77,17 @@ public class ErrorUtils { }; - private static final FunctionGroup ERROR_FUNCTION_GROUP = new FunctionGroup() { - @NotNull - @Override - public String getName() { - return ""; - } - - @Override - public boolean isEmpty() { - return false; - } - - @NotNull - @Override - public Set getFunctionDescriptors() { - return Collections.singleton(createErrorFunction(0, Collections.emptyList())); - } - }; - private static final ClassDescriptorImpl ERROR_CLASS = new ClassDescriptorImpl(ERROR_MODULE, Collections.emptyList(), "") { @NotNull @Override - public FunctionGroup getConstructors() { + public Set getConstructors() { return ERROR_FUNCTION_GROUP; } }; + + private static final Set ERROR_FUNCTION_GROUP = Collections.singleton(createErrorFunction(0, Collections.emptyList())); private static final ConstructorDescriptor ERROR_CONSTRUCTOR = new ConstructorDescriptorImpl(ERROR_CLASS, Collections.emptyList(), true); + static { ERROR_CLASS.initialize( true, Collections.emptyList(), Collections.emptyList(), getErrorScope(), ERROR_FUNCTION_GROUP, ERROR_CONSTRUCTOR); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardClasses.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardClasses.java index 3ce7eb0d2d6..41774dc14c4 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardClasses.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardClasses.java @@ -48,7 +48,7 @@ public class JetStandardClasses { } }, JetScope.EMPTY, - FunctionGroup.EMPTY, + Collections.emptySet(), null, null ); @@ -71,7 +71,7 @@ public class JetStandardClasses { Collections.emptyList(), Collections.emptySet(), JetScope.EMPTY, - FunctionGroup.EMPTY, + Collections.emptySet(), null, null ); @@ -97,7 +97,6 @@ public class JetStandardClasses { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static final JetScope STUB = JetScope.EMPTY; - public static final FunctionGroup STUB_FG = FunctionGroup.EMPTY; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -124,7 +123,7 @@ public class JetStandardClasses { parameters, Collections.singleton(getAnyType()), STUB, - STUB_FG, + Collections.emptySet(), // TODO null); // TODO : constructor TUPLE_CONSTRUCTORS.add(TUPLE[i].getTypeConstructor()); } @@ -149,7 +148,7 @@ public class JetStandardClasses { FUNCTION[i] = function.initialize( false, createTypeParameters(i, function), - Collections.singleton(getAnyType()), STUB, FunctionGroup.EMPTY, null); + Collections.singleton(getAnyType()), STUB, Collections.emptySet(), null); FUNCTION_TYPE_CONSTRUCTORS.add(FUNCTION[i].getTypeConstructor()); ClassDescriptorImpl receiverFunction = new ClassDescriptorImpl( @@ -164,7 +163,7 @@ public class JetStandardClasses { RECEIVER_FUNCTION[i] = receiverFunction.initialize( false, parameters, - Collections.singleton(getAnyType()), STUB, FunctionGroup.EMPTY, null); + Collections.singleton(getAnyType()), STUB, Collections.emptySet(), null); RECEIVER_FUNCTION_TYPE_CONSTRUCTORS.add(RECEIVER_FUNCTION[i].getTypeConstructor()); } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java index 1ec692c6930..85a0476036e 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java @@ -6,7 +6,7 @@ import com.intellij.psi.PsiFileFactory; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; -import org.jetbrains.jet.lang.descriptors.FunctionGroup; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.psi.JetFile; @@ -21,6 +21,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.util.Collections; import java.util.List; +import java.util.Set; /** * @author abreslav @@ -60,7 +61,6 @@ public class JetStandardLibrary { private final ClassDescriptor arrayClass; private final ClassDescriptor iterableClass; private final ClassDescriptor typeInfoClass; - private final ClassDescriptor tuple0Class; private final JetType byteType; private final JetType nullableByteType; @@ -89,7 +89,7 @@ public class JetStandardLibrary { private final JetType nullableStringType; private final NamespaceDescriptor typeInfoNamespace; - private final FunctionGroup typeInfoFunction; + private final Set typeInfoFunction; private JetStandardLibrary(@NotNull Project project) { // TODO : review @@ -120,10 +120,9 @@ public class JetStandardLibrary { this.stringClass = (ClassDescriptor) libraryScope.getClassifier("String"); this.arrayClass = (ClassDescriptor) libraryScope.getClassifier("Array"); this.iterableClass = (ClassDescriptor) libraryScope.getClassifier("Iterable"); - this.tuple0Class = (ClassDescriptor) libraryScope.getClassifier("Tuple0"); typeInfoNamespace = libraryScope.getNamespace("typeinfo"); this.typeInfoClass = (ClassDescriptor) typeInfoNamespace.getMemberScope().getClassifier("TypeInfo"); - typeInfoFunction = typeInfoNamespace.getMemberScope().getFunctionGroup("typeinfo"); + typeInfoFunction = typeInfoNamespace.getMemberScope().getFunctions("typeinfo"); this.byteType = new JetTypeImpl(getByte()); this.charType = new JetTypeImpl(getChar()); @@ -134,7 +133,7 @@ public class JetStandardLibrary { this.doubleType = new JetTypeImpl(getDouble()); this.booleanType = new JetTypeImpl(getBoolean()); this.stringType = new JetTypeImpl(getString()); - this.tuple0Type = new JetTypeImpl(getTuple0()); + this.tuple0Type = new JetTypeImpl(JetStandardClasses.getTuple(0)); this.nullableByteType = TypeUtils.makeNullable(byteType); this.nullableCharType = TypeUtils.makeNullable(charType); @@ -210,10 +209,6 @@ public class JetStandardLibrary { return iterableClass; } - public ClassDescriptor getTuple0() { - return tuple0Class; - } - public NamespaceDescriptor getTypeInfoNamespace() { return typeInfoNamespace; } @@ -222,7 +217,7 @@ public class JetStandardLibrary { return typeInfoClass; } - public FunctionGroup getTypeInfoFunctionGroup() { + public Set getTypeInfoFunctions() { return typeInfoFunction; } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java index ca595ac72cf..5d5cd433cc5 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java @@ -237,7 +237,7 @@ public class TypeUtils { @NotNull public static Multimap buildDeepSubstitutionMultimap(@NotNull JetType type) { - Multimap fullSubstitution = Multimaps.newSetMultimap(Maps.>newHashMap(), CommonSuppliers.getLinkedHashSetSupplier()); + Multimap fullSubstitution = CommonSuppliers.newLinkedHashSetHashSetMultimap(); Map substitution = Maps.newHashMap(); TypeSubstitutor typeSubstitutor = TypeSubstitutor.create(substitution); // we use the mutability of the map here diff --git a/compiler/frontend/src/org/jetbrains/jet/util/CommonSuppliers.java b/compiler/frontend/src/org/jetbrains/jet/util/CommonSuppliers.java index b596e476a44..d467debb201 100644 --- a/compiler/frontend/src/org/jetbrains/jet/util/CommonSuppliers.java +++ b/compiler/frontend/src/org/jetbrains/jet/util/CommonSuppliers.java @@ -1,9 +1,9 @@ package org.jetbrains.jet.util; import com.google.common.base.Supplier; -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; +import com.google.common.collect.*; +import java.util.Collection; import java.util.List; import java.util.Set; @@ -36,4 +36,8 @@ public class CommonSuppliers { //noinspection unchecked return (Supplier>) LINKED_HASH_SET_SUPPLIER; } + + public static SetMultimap newLinkedHashSetHashSetMultimap() { + return Multimaps.newSetMultimap(Maps.>newHashMap(), CommonSuppliers.getLinkedHashSetSupplier()); + } } diff --git a/idea/tests/org/jetbrains/jet/types/JetOverridingTest.java b/idea/tests/org/jetbrains/jet/types/JetOverridingTest.java index d34a474936a..158958e0101 100644 --- a/idea/tests/org/jetbrains/jet/types/JetOverridingTest.java +++ b/idea/tests/org/jetbrains/jet/types/JetOverridingTest.java @@ -70,7 +70,7 @@ public class JetOverridingTest extends LightDaemonAnalyzerTestCase { "fun ab() : Int", "fun a() : Int"); - assertNotOverridable( + assertOverridable( "fun a() : Int", "fun a() : Any"); @@ -98,7 +98,7 @@ public class JetOverridingTest extends LightDaemonAnalyzerTestCase { "fun a(a : T1) : T1", "fun a(a : Y) : T"); - assertNotOverridable( + assertOverridable( "fun a(a : T1) : X", "fun a(a : T) : T"); diff --git a/idea/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java b/idea/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java index c5eee12fc5d..1903d2b13f1 100644 --- a/idea/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java +++ b/idea/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java @@ -610,18 +610,13 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase { @NotNull @Override - public FunctionGroup getFunctionGroup(@NotNull String name) { - WritableFunctionGroup writableFunctionGroup = new WritableFunctionGroup(name); -// ClassifierDescriptor classifier = getClassifier(name); -// if (classifier instanceof ClassDescriptor) { -// ClassDescriptor classDescriptor = (ClassDescriptor) classifier; -// writableFunctionGroup.addAllFunctions(classDescriptor.getConstructors()); -// } + public Set getFunctions(@NotNull String name) { + Set writableFunctionGroup = Sets.newLinkedHashSet(); ModuleDescriptor module = new ModuleDescriptor("TypeCheckerTest"); for (String funDecl : FUNCTION_DECLARATIONS) { FunctionDescriptor functionDescriptor = classDescriptorResolver.resolveFunctionDescriptor(module, this, JetPsiFactory.createFunction(getProject(), funDecl)); if (name.equals(functionDescriptor.getName())) { - writableFunctionGroup.addFunction(functionDescriptor); + writableFunctionGroup.add(functionDescriptor); } } return writableFunctionGroup; @@ -690,7 +685,7 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase { }); } - WritableFunctionGroup constructors = new WritableFunctionGroup(""); + Set constructors = Sets.newLinkedHashSet(); classDescriptor.initialize( !open, typeParameters, @@ -702,12 +697,12 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase { for (JetConstructor constructor : classElement.getSecondaryConstructors()) { ConstructorDescriptorImpl functionDescriptor = classDescriptorResolver.resolveSecondaryConstructorDescriptor(memberDeclarations, classDescriptor, constructor); functionDescriptor.setReturnType(classDescriptor.getDefaultType()); - constructors.addFunction(functionDescriptor); + constructors.add(functionDescriptor); } ConstructorDescriptorImpl primaryConstructorDescriptor = classDescriptorResolver.resolvePrimaryConstructorDescriptor(scope, classDescriptor, classElement); if (primaryConstructorDescriptor != null) { primaryConstructorDescriptor.setReturnType(classDescriptor.getDefaultType()); - constructors.addFunction(primaryConstructorDescriptor); + constructors.add(primaryConstructorDescriptor); classDescriptor.setPrimaryConstructor(primaryConstructorDescriptor); } return classDescriptor;