DescriptorMatcher

This commit is contained in:
Stepan Koltsov
2012-06-16 06:44:15 +04:00
parent 7116beb95a
commit 3a9f6c0928
31 changed files with 360 additions and 83 deletions
@@ -20,6 +20,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.DescriptorPredicate;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
@@ -104,7 +105,7 @@ public abstract class AbstractScopeAdapter implements JetScope {
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
return getWorkerScope().getAllDescriptors();
public Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate) {
return getWorkerScope().getAllDescriptors(predicate);
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.scopes.DescriptorPredicate;
import org.jetbrains.jet.lang.types.JetType;
import javax.inject.Inject;
@@ -67,7 +68,7 @@ public class DelegationResolver {
JetDelegatorByExpressionSpecifier specifier = (JetDelegatorByExpressionSpecifier) delegationSpecifier;
JetType type = trace.get(BindingContext.TYPE, specifier.getTypeReference());
if (type != null) {
for (DeclarationDescriptor declarationDescriptor : type.getMemberScope().getAllDescriptors()) {
for (DeclarationDescriptor declarationDescriptor : type.getMemberScope().getAllDescriptors(DescriptorPredicate.callableMembers())) {
if (declarationDescriptor instanceof PropertyDescriptor) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) declarationDescriptor;
if (propertyDescriptor.getModality().isOverridable()) {
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.DescriptorPredicate;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
@@ -238,7 +239,7 @@ public class OverrideResolver {
private static List<CallableMemberDescriptor> getCallableMembersFromType(JetScope scope) {
List<CallableMemberDescriptor> r = Lists.newArrayList();
for (DeclarationDescriptor decl : scope.getAllDescriptors()) {
for (DeclarationDescriptor decl : scope.getAllDescriptors(DescriptorPredicate.all())) {
if (decl instanceof PropertyDescriptor || decl instanceof SimpleFunctionDescriptor) {
r.add((CallableMemberDescriptor) decl);
}
@@ -332,7 +333,7 @@ public class OverrideResolver {
public static Multimap<CallableMemberDescriptor, CallableMemberDescriptor> collectSuperMethods(MutableClassDescriptor classDescriptor) {
Set<CallableMemberDescriptor> inheritedFunctions = Sets.newLinkedHashSet();
for (JetType supertype : classDescriptor.getSupertypes()) {
for (DeclarationDescriptor descriptor : supertype.getMemberScope().getAllDescriptors()) {
for (DeclarationDescriptor descriptor : supertype.getMemberScope().getAllDescriptors(DescriptorPredicate.all())) {
if (descriptor instanceof CallableMemberDescriptor) {
CallableMemberDescriptor memberDescriptor = (CallableMemberDescriptor) descriptor;
inheritedFunctions.add(memberDescriptor);
@@ -16,6 +16,7 @@
package org.jetbrains.jet.lang.resolve.lazy;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
@@ -25,6 +26,8 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.DescriptorPredicate;
import org.jetbrains.jet.lang.resolve.scopes.DescriptorPredicateUtils;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
@@ -192,7 +195,9 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
public Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate) {
// TODO: cache only what matches predicate
for (JetDeclaration declaration : declarationProvider.getAllDeclarations()) {
if (declaration instanceof JetClassOrObject) {
JetClassOrObject classOrObject = (JetClassOrObject) declaration;
@@ -222,7 +227,8 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
}
addExtraDescriptors();
allDescriptorsComputed = true;
return allDescriptors;
return DescriptorPredicateUtils.filter(allDescriptors, predicate);
}
protected abstract void addExtraDescriptors();
@@ -30,6 +30,7 @@ import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.DescriptorResolver;
import org.jetbrains.jet.lang.resolve.OverrideResolver;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.DescriptorPredicate;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.DeferredType;
@@ -86,7 +87,7 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
public void conflict(@NotNull CallableMemberDescriptor fromSuper, @NotNull CallableMemberDescriptor fromCurrent) {
BindingTrace trace = resolveSession.getTrace();
JetDeclaration declaration = (JetDeclaration) BindingContextUtils.descriptorToDeclaration(trace.getBindingContext(),
fromCurrent);
fromCurrent);
assert declaration != null : "fromCurrent can not be a fake override";
trace.report(Errors.CONFLICTING_OVERLOADS.on(declaration, fromCurrent, fromCurrent.getContainingDeclaration().getName().getName()));
}
@@ -134,7 +135,7 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
@Override
protected void addExtraDescriptors() {
for (JetType supertype : thisDescriptor.getTypeConstructor().getSupertypes()) {
for (DeclarationDescriptor descriptor : supertype.getMemberScope().getAllDescriptors()) {
for (DeclarationDescriptor descriptor : supertype.getMemberScope().getAllDescriptors(DescriptorPredicate.callableMembers())) {
if (descriptor instanceof FunctionDescriptor) {
getFunctions(descriptor.getName());
}
@@ -157,11 +157,11 @@ public class ChainedScope implements JetScope {
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
public Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate) {
if (allDescriptors == null) {
allDescriptors = Sets.newHashSet();
for (JetScope scope : scopeChain) {
allDescriptors.addAll(scope.getAllDescriptors());
allDescriptors.addAll(scope.getAllDescriptors(DescriptorPredicate.all()));
}
}
return allDescriptors;
@@ -0,0 +1,216 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.scopes;
import com.google.common.base.Predicate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.name.Name;
/**
* @author Stepan Koltsov
*/
public abstract class DescriptorPredicate {
public boolean includeAll() {
return this instanceof All;
}
public abstract boolean includeName(@NotNull Name name);
public enum DescriptorKind {
CLASS,
CALLABLE_MEMBER,
CALLABLE_NON_MEMBER,
NAMESPACE,
OTHER,
;
public boolean isCallable() {
return this == CALLABLE_MEMBER || this == CALLABLE_NON_MEMBER;
}
}
public abstract boolean includeKind(@NotNull DescriptorKind kind);
private static DescriptorKind descriptorKind(@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof CallableMemberDescriptor) {
return DescriptorKind.CALLABLE_MEMBER;
}
else if (descriptor instanceof CallableDescriptor) {
return DescriptorKind.CALLABLE_NON_MEMBER;
}
else if (descriptor instanceof ClassDescriptor) {
return DescriptorKind.CLASS;
}
else if (descriptor instanceof NamespaceDescriptor) {
return DescriptorKind.NAMESPACE;
}
else {
return DescriptorKind.OTHER;
}
}
/**
* Should be called only if includeKind(CALLABLE_MEMBER) returned true.
*/
public abstract boolean includeExtension(boolean extension);
public boolean include(@NotNull DeclarationDescriptor descriptor) {
if (!includeKind(descriptorKind(descriptor))) {
return false;
}
if (descriptor instanceof CallableMemberDescriptor) {
CallableMemberDescriptor callable = (CallableMemberDescriptor) descriptor;
boolean extension = callable.getReceiverParameter().exists();
if (!includeExtension(extension)) {
return false;
}
}
if (!includeName(descriptor.getName())) {
return false;
}
return true;
}
private static class All extends DescriptorPredicate {
private static final All instance = new All();
@Override
public boolean include(@NotNull DeclarationDescriptor descriptor) {
return true;
}
@Override
public boolean includeKind(@NotNull DescriptorKind kind) {
return true;
}
@Override
public boolean includeName(@NotNull Name name) {
return true;
}
@Override
public boolean includeExtension(boolean extension) {
return true;
}
}
public static DescriptorPredicate all() {
return All.instance;
}
private static class HasName extends DescriptorPredicate {
@NotNull
private final Name required;
private HasName(@NotNull Name required) {
this.required = required;
}
@Override
public boolean includeKind(@NotNull DescriptorKind kind) {
return true;
}
@Override
public boolean includeExtension(boolean extension) {
return true;
}
@Override
public boolean include(@NotNull DeclarationDescriptor descriptor) {
return includeName(descriptor.getName());
}
@Override
public boolean includeName(@NotNull Name name) {
return required.equals(name);
}
}
public static DescriptorPredicate hasName(@NotNull Name name) {
return new HasName(name);
}
private static class CallableMembers extends DescriptorPredicate {
public static final CallableMembers instance = new CallableMembers();
@Override
public boolean includeName(@NotNull Name name) {
return true;
}
@Override
public boolean includeExtension(boolean extension) {
return true;
}
@Override
public boolean include(@NotNull DeclarationDescriptor descriptor) {
return descriptor instanceof CallableMemberDescriptor;
}
@Override
public boolean includeKind(@NotNull DescriptorKind kind) {
return kind == DescriptorKind.CALLABLE_MEMBER;
}
}
public static DescriptorPredicate callableMembers() {
return CallableMembers.instance;
}
private static class Extension extends CallableMembers {
private static final Extension instance = new Extension();
@Override
public boolean includeExtension(boolean extension) {
return extension;
}
}
public static DescriptorPredicate extension() {
return Extension.instance;
}
public Predicate<DeclarationDescriptor> asGuavaPredicate() {
return new Predicate<DeclarationDescriptor>() {
@Override
public boolean apply(@NotNull DeclarationDescriptor descriptor) {
return include(descriptor);
}
};
}
}
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.scopes;
import com.google.common.collect.Collections2;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import java.util.Collection;
/**
* @author Stepan Koltsov
*/
public class DescriptorPredicateUtils {
public static Collection<DeclarationDescriptor> filter(
@NotNull Collection<DeclarationDescriptor> descriptors,
@NotNull DescriptorPredicate predicate) {
if (predicate.includeAll()) {
return descriptors;
}
else {
return Collections2.filter(descriptors, predicate.asGuavaPredicate());
}
}
}
@@ -72,8 +72,9 @@ public class InnerClassesScopeWrapper extends JetScopeImpl {
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
Collection<DeclarationDescriptor> allDescriptors = actualScope.getAllDescriptors();
public Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate) {
// TODO: use better predicate
Collection<DeclarationDescriptor> allDescriptors = actualScope.getAllDescriptors(predicate);
return Collections2.filter(allDescriptors, new Predicate<DeclarationDescriptor>() {
@Override
public boolean apply(@Nullable DeclarationDescriptor descriptor) {
@@ -84,7 +84,7 @@ public interface JetScope {
* @return All visible descriptors from current scope.
*/
@NotNull
Collection<DeclarationDescriptor> getAllDescriptors();
Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate);
/**
* @return EFFECTIVE implicit receiver at this point (may be corresponding to an outer scope)
@@ -88,7 +88,7 @@ public abstract class JetScopeImpl implements JetScope {
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
public Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate) {
return Collections.emptyList();
}
@@ -53,18 +53,8 @@ public final class JetScopeUtils {
* @param scope Scope for query extensions.
* @return extension descriptors.
*/
@SuppressWarnings("unchecked")
public static Collection<CallableDescriptor> getAllExtensions(@NotNull JetScope scope) {
final Set<CallableDescriptor> result = Sets.newHashSet();
for (DeclarationDescriptor descriptor : scope.getAllDescriptors()) {
if (descriptor instanceof CallableDescriptor) {
CallableDescriptor callDescriptor = (CallableDescriptor) descriptor;
if (callDescriptor.getReceiverParameter().exists()) {
result.add(callDescriptor);
}
}
}
return result;
return (Collection<CallableDescriptor>) (Object) scope.getAllDescriptors(DescriptorPredicate.extension());
}
}
@@ -149,10 +149,10 @@ public class SubstitutingScope implements JetScope {
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
public Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate) {
if (allDescriptors == null) {
allDescriptors = Sets.newHashSet();
for (DeclarationDescriptor descriptor : workerScope.getAllDescriptors()) {
for (DeclarationDescriptor descriptor : workerScope.getAllDescriptors(DescriptorPredicate.all())) {
DeclarationDescriptor substitute = substitute(descriptor);
// assert substitute != null : descriptor;
if (substitute != null) {
@@ -126,7 +126,7 @@ public class WritableScopeImpl extends WritableScopeWithImports {
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
public Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate) {
checkMayRead();
if (!allDescriptorsDone) {
@@ -135,12 +135,12 @@ public class WritableScopeImpl extends WritableScopeWithImports {
// make sure no descriptors added to allDescriptors collection
changeLockLevel(LockLevel.READING);
allDescriptors.addAll(getWorkerScope().getAllDescriptors());
allDescriptors.addAll(getWorkerScope().getAllDescriptors(DescriptorPredicate.all()));
for (JetScope imported : getImports()) {
allDescriptors.addAll(imported.getAllDescriptors());
allDescriptors.addAll(imported.getAllDescriptors(DescriptorPredicate.all()));
}
}
return allDescriptors;
return DescriptorPredicateUtils.filter(allDescriptors, predicate);
}
@NotNull
@@ -283,19 +283,19 @@ public class WriteThroughScope extends WritableScopeWithImports {
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
public Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate) {
checkMayRead();
if (allDescriptors == null) {
allDescriptors = Lists.newArrayList();
allDescriptors.addAll(writableWorker.getAllDescriptors());
allDescriptors.addAll(getWorkerScope().getAllDescriptors());
allDescriptors.addAll(writableWorker.getAllDescriptors(DescriptorPredicate.all()));
allDescriptors.addAll(getWorkerScope().getAllDescriptors(DescriptorPredicate.all()));
for (JetScope imported : getImports()) {
allDescriptors.addAll(imported.getAllDescriptors());
allDescriptors.addAll(imported.getAllDescriptors(DescriptorPredicate.all()));
}
}
return allDescriptors;
return DescriptorPredicateUtils.filter(allDescriptors, predicate);
}
@NotNull
@@ -22,6 +22,7 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.DescriptorPredicate;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.error.ErrorSimpleFunctionDescriptorImpl;
@@ -112,7 +113,7 @@ public class ErrorUtils {
@NotNull
@Override
public Collection<DeclarationDescriptor> getAllDescriptors() {
public Collection<DeclarationDescriptor> getAllDescriptors(@NotNull DescriptorPredicate predicate) {
return Collections.emptyList();
}
@@ -499,6 +499,6 @@ public class JetStandardClasses {
@NotNull
public static Collection<DeclarationDescriptor> getAllStandardClasses() {
return STANDARD_CLASSES.getAllDescriptors();
return STANDARD_CLASSES.getAllDescriptors(DescriptorPredicate.all());
}
}