getImplicitReceiverHierarchy() returns a List

This commit is contained in:
Andrey Breslav
2012-10-31 23:06:57 +04:00
parent 555ac07b55
commit a91a99e5be
16 changed files with 67 additions and 54 deletions
@@ -105,7 +105,7 @@ public final class TipsManager {
else {
Collection<DeclarationDescriptor> descriptorsSet = Sets.newHashSet();
List<ReceiverParameterDescriptor> result = JetScopeUtils.getImplicitReceiversHierarchy(resolutionScope);
List<ReceiverParameterDescriptor> result = resolutionScope.getImplicitReceiversHierarchy();
for (ReceiverParameterDescriptor receiverDescriptor : result) {
JetType receiverType = receiverDescriptor.getType();
@@ -153,7 +153,7 @@ public final class TipsManager {
) {
final Set<DeclarationDescriptor> descriptorsSet = Sets.newHashSet(descriptors);
final List<ReceiverParameterDescriptor> result = JetScopeUtils.getImplicitReceiversHierarchy(scope);
final List<ReceiverParameterDescriptor> result = scope.getImplicitReceiversHierarchy();
descriptorsSet.removeAll(
Collections2.filter(JetScopeUtils.getAllExtensions(scope), new Predicate<CallableDescriptor>() {
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@@ -243,10 +244,10 @@ public class MutableClassDescriptor extends MutableClassDescriptorLite implement
return classObjectDescriptor.getDefaultType().getMemberScope();
}
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
super.getImplicitReceiversHierarchy(result);
result.add(0, classObjectDescriptor.getThisAsReceiverParameter());
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return Collections.singletonList(classObjectDescriptor.getThisAsReceiverParameter());
}
});
@@ -34,9 +34,10 @@ public abstract class AbstractScopeAdapter implements JetScope {
@NotNull
protected abstract JetScope getWorkerScope();
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
getWorkerScope().getImplicitReceiversHierarchy(result);
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return getWorkerScope().getImplicitReceiversHierarchy();
}
@NotNull
@@ -226,7 +226,7 @@ public abstract class TaskPrioritizer {
private static <D extends CallableDescriptor> boolean setImpliedThis(@NotNull JetScope scope, ResolutionCandidate<D> candidate) {
ReceiverParameterDescriptor expectedThisObject = candidate.getDescriptor().getExpectedThisObject();
if (!expectedThisObject.exists()) return true;
List<ReceiverParameterDescriptor> receivers = JetScopeUtils.getImplicitReceiversHierarchy(scope);
List<ReceiverParameterDescriptor> receivers = scope.getImplicitReceiversHierarchy();
for (ReceiverParameterDescriptor receiver : receivers) {
if (JetTypeChecker.INSTANCE.isSubtypeOf(receiver.getType(), expectedThisObject.getType())) {
// TODO : Autocasts & nullability
@@ -260,12 +260,14 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
protected abstract void addExtraDescriptors();
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
ReceiverParameterDescriptor receiver = getImplicitReceiver();
if (receiver.exists()) {
result.add(receiver);
return Collections.singletonList(receiver);
}
return Collections.emptyList();
}
@NotNull
@@ -16,6 +16,7 @@
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.jet.lang.descriptors.*;
@@ -34,6 +35,7 @@ public class ChainedScope implements JetScope {
private final DeclarationDescriptor containingDeclaration;
private final JetScope[] scopeChain;
private Collection<DeclarationDescriptor> allDescriptors;
private List<ReceiverParameterDescriptor> implicitReceiverHierarchy;
public ChainedScope(DeclarationDescriptor containingDeclaration, JetScope... scopes) {
this.containingDeclaration = containingDeclaration;
@@ -114,11 +116,16 @@ public class ChainedScope implements JetScope {
return result;
}
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
for (JetScope jetScope : scopeChain) {
jetScope.getImplicitReceiversHierarchy(result);
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
if (implicitReceiverHierarchy == null) {
implicitReceiverHierarchy = Lists.newArrayList();
for (JetScope jetScope : scopeChain) {
implicitReceiverHierarchy.addAll(jetScope.getImplicitReceiversHierarchy());
}
}
return implicitReceiverHierarchy;
}
@NotNull
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
@@ -82,9 +83,10 @@ public class InnerClassesScopeWrapper extends AbstractScopeAdapter {
});
}
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
// Do nothing
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return Collections.emptyList();
}
@Override
@@ -86,9 +86,9 @@ public interface JetScope {
/**
* Adds receivers to the list in order of locality, so that the closest (the most local) receiver goes first
* @param result
*/
void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result);
@NotNull
List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy();
@NotNull
Collection<DeclarationDescriptor> getOwnDeclaredDescriptors();
@@ -85,8 +85,10 @@ public abstract class JetScopeImpl implements JetScope {
return Collections.emptyList();
}
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return Collections.emptyList();
}
@NotNull
@@ -36,22 +36,8 @@ import java.util.Set;
public final class JetScopeUtils {
private JetScopeUtils() {}
/**
* Get receivers in order of locality, so that the closest (the most local) receiver goes first
* A wrapper for {@link JetScope#getImplicitReceiversHierarchy(List)}
*
* @param scope Scope for getting receivers hierarchy.
* @return receivers hierarchy.
*/
@NotNull
public static List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy(@NotNull JetScope scope) {
List<ReceiverParameterDescriptor> descriptors = Lists.newArrayList();
scope.getImplicitReceiversHierarchy(descriptors);
return descriptors;
}
public static List<ReceiverDescriptor> getImplicitReceiversHierarchyValues(@NotNull JetScope scope) {
Collection<ReceiverParameterDescriptor> hierarchy = getImplicitReceiversHierarchy(scope);
Collection<ReceiverParameterDescriptor> hierarchy = scope.getImplicitReceiversHierarchy();
return Lists.newArrayList(
Collections2.transform(hierarchy,
@@ -118,8 +118,9 @@ public class SubstitutingScope implements JetScope {
return workerScope.getNamespace(name); // TODO
}
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
throw new UnsupportedOperationException(); // TODO
}
@@ -462,14 +462,13 @@ public class WritableScopeImpl extends WritableScopeWithImports {
}
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
checkMayRead();
super.getImplicitReceiversHierarchy(result);
protected List<ReceiverParameterDescriptor> computeImplicitReceiversHierarchy() {
List<ReceiverParameterDescriptor> implicitReceiverHierarchy = Lists.newArrayList();
if (implicitReceiver != null && implicitReceiver.exists()) {
result.add(0, implicitReceiver);
implicitReceiverHierarchy.add(implicitReceiver);
}
implicitReceiverHierarchy.addAll(super.computeImplicitReceiversHierarchy());
return implicitReceiverHierarchy;
}
// @SuppressWarnings({"NullableProblems"})
@@ -16,17 +16,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.name.Name;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;
/**
* @author abreslav
@@ -40,6 +37,7 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
private List<JetScope> imports;
private WritableScope currentIndividualImportScope;
protected final RedeclarationHandler redeclarationHandler;
private List<ReceiverParameterDescriptor> implicitReceiverHierarchy;
public WritableScopeWithImports(@NotNull JetScope scope, @NotNull RedeclarationHandler redeclarationHandler, @NotNull String debugName) {
super(scope);
@@ -101,17 +99,27 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
currentIndividualImportScope = null;
}
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
checkMayRead();
super.getImplicitReceiversHierarchy(result);
if (implicitReceiverHierarchy == null) {
implicitReceiverHierarchy = computeImplicitReceiversHierarchy();
}
return implicitReceiverHierarchy;
}
protected List<ReceiverParameterDescriptor> computeImplicitReceiversHierarchy() {
List<ReceiverParameterDescriptor> implicitReceiverHierarchy = Lists.newArrayList();
// Imported scopes come with their receivers
// Example: class member resolution scope imports a scope of it's class object
// members of the class object must be able to find it as an implicit receiver
for (JetScope scope : getImports()) {
scope.getImplicitReceiversHierarchy(result);
implicitReceiverHierarchy.addAll(scope.getImplicitReceiversHierarchy());
}
implicitReceiverHierarchy.addAll(super.getImplicitReceiversHierarchy());
return implicitReceiverHierarchy;
}
@NotNull
@@ -76,8 +76,10 @@ public class ErrorUtils {
return null; // TODO : review
}
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return Collections.emptyList();
}
@NotNull
@@ -42,7 +42,6 @@ import org.jetbrains.jet.lang.resolve.constants.StringValue;
import org.jetbrains.jet.lang.resolve.name.LabelName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.JetScopeUtils;
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
@@ -511,7 +510,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
expression.getInstanceReference(), expression.getTargetLabel(), context, thisReceiver, new LabelName(labelName));
}
else {
List<ReceiverParameterDescriptor> receivers = JetScopeUtils.getImplicitReceiversHierarchy(context.scope);
List<ReceiverParameterDescriptor> receivers = context.scope.getImplicitReceiversHierarchy();
if (onlyClassReceivers) {
for (ReceiverParameterDescriptor receiver : receivers) {
if (receiver.getContainingDeclaration() instanceof ClassDescriptor) {
@@ -16,6 +16,7 @@
package org.jetbrains.jet.types;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
@@ -32,7 +33,8 @@ import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.java.*;
import org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.*;
@@ -571,9 +573,10 @@ public class JetTypeCheckerTest extends JetLiteFixture {
private void assertType(String contextType, final String expression, String expectedType) {
final JetType thisType = makeType(contextType);
JetScope scope = new JetScopeAdapter(classDefinitions.BASIC_SCOPE) {
@NotNull
@Override
public void getImplicitReceiversHierarchy(@NotNull List<ReceiverParameterDescriptor> result) {
result.add(new ReceiverParameterDescriptorImpl(
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
return Lists.<ReceiverParameterDescriptor>newArrayList(new ReceiverParameterDescriptorImpl(
classDefinitions.BASIC_SCOPE.getContainingDeclaration(),
thisType,
new ExpressionReceiver(JetPsiFactory.createExpression(getProject(), expression), thisType)