Added imports resolve from objects and variables
This commit is contained in:
+123
@@ -0,0 +1,123 @@
|
||||
package org.jetbrains.jet.lang.descriptors;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.TypeSubstitutor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author svtk
|
||||
*/
|
||||
public class FunctionDescriptorWithImplicitReceiver implements FunctionDescriptor {
|
||||
|
||||
private FunctionDescriptor functionDescriptor;
|
||||
private DeclarationDescriptor implicitReceiver;
|
||||
|
||||
public FunctionDescriptorWithImplicitReceiver(@NotNull FunctionDescriptor functionDescriptor, @NotNull DeclarationDescriptor implicitReceiver) {
|
||||
this.functionDescriptor = functionDescriptor;
|
||||
this.implicitReceiver = implicitReceiver;
|
||||
}
|
||||
|
||||
public FunctionDescriptor getFunctionDescriptor() {
|
||||
return functionDescriptor;
|
||||
}
|
||||
|
||||
public DeclarationDescriptor getImplicitReceiver() {
|
||||
return implicitReceiver;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public DeclarationDescriptor getContainingDeclaration() {
|
||||
return functionDescriptor.getContainingDeclaration();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public FunctionDescriptor getOriginal() {
|
||||
return functionDescriptor.getOriginal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FunctionDescriptor substitute(TypeSubstitutor substitutor) {
|
||||
return functionDescriptor.substitute(substitutor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Set<? extends FunctionDescriptor> getOverriddenDescriptors() {
|
||||
return functionDescriptor.getOverriddenDescriptors();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public FunctionDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract) {
|
||||
return functionDescriptor.copy(newOwner, makeNonAbstract);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ReceiverDescriptor getReceiverParameter() {
|
||||
return ReceiverDescriptor.NO_RECEIVER;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ReceiverDescriptor getExpectedThisObject() {
|
||||
return ReceiverDescriptor.NO_RECEIVER;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<TypeParameterDescriptor> getTypeParameters() {
|
||||
return functionDescriptor.getTypeParameters();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JetType getReturnType() {
|
||||
return functionDescriptor.getReturnType();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<ValueParameterDescriptor> getValueParameters() {
|
||||
return functionDescriptor.getValueParameters();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Modality getModality() {
|
||||
return functionDescriptor.getModality();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Visibility getVisibility() {
|
||||
return functionDescriptor.getVisibility();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
|
||||
return functionDescriptor.accept(visitor, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acceptVoid(DeclarationDescriptorVisitor<Void, Void> visitor) {
|
||||
functionDescriptor.acceptVoid(visitor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AnnotationDescriptor> getAnnotations() {
|
||||
return functionDescriptor.getAnnotations();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return functionDescriptor.getName();
|
||||
}
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
package org.jetbrains.jet.lang.descriptors;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.TypeSubstitutor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author svtk
|
||||
*/
|
||||
public class VariableDescriptorWithImplicitReceiver implements VariableDescriptor {
|
||||
private VariableDescriptor variableDescriptor;
|
||||
private DeclarationDescriptor implicitReceiver;
|
||||
|
||||
public VariableDescriptorWithImplicitReceiver(VariableDescriptor variableDescriptor, DeclarationDescriptor implicitReceiver) {
|
||||
this.variableDescriptor = variableDescriptor;
|
||||
this.implicitReceiver = implicitReceiver;
|
||||
}
|
||||
|
||||
public VariableDescriptor getVariableDescriptor() {
|
||||
return variableDescriptor;
|
||||
}
|
||||
|
||||
public DeclarationDescriptor getImplicitReceiver() {
|
||||
return implicitReceiver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JetType getOutType() {
|
||||
return variableDescriptor.getOutType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JetType getInType() {
|
||||
return variableDescriptor.getInType();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public DeclarationDescriptor getContainingDeclaration() {
|
||||
return variableDescriptor.getContainingDeclaration();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ReceiverDescriptor getReceiverParameter() {
|
||||
return ReceiverDescriptor.NO_RECEIVER;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ReceiverDescriptor getExpectedThisObject() {
|
||||
return ReceiverDescriptor.NO_RECEIVER;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<TypeParameterDescriptor> getTypeParameters() {
|
||||
return variableDescriptor.getTypeParameters();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JetType getReturnType() {
|
||||
return variableDescriptor.getReturnType();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CallableDescriptor getOriginal() {
|
||||
return variableDescriptor.getOriginal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public VariableDescriptor substitute(TypeSubstitutor substitutor) {
|
||||
return variableDescriptor.substitute(substitutor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
|
||||
return variableDescriptor.accept(visitor, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acceptVoid(DeclarationDescriptorVisitor<Void, Void> visitor) {
|
||||
variableDescriptor.acceptVoid(visitor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<ValueParameterDescriptor> getValueParameters() {
|
||||
return variableDescriptor.getValueParameters();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Set<? extends CallableDescriptor> getOverriddenDescriptors() {
|
||||
return variableDescriptor.getOverriddenDescriptors();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVar() {
|
||||
return variableDescriptor.isVar();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AnnotationDescriptor> getAnnotations() {
|
||||
return variableDescriptor.getAnnotations();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return variableDescriptor.getName();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.jetbrains.jet.lang.resolve;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Collections2;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -101,19 +103,17 @@ public class ImportsResolver {
|
||||
if (firstPhase) {
|
||||
if (descriptor instanceof ClassifierDescriptor) {
|
||||
namespaceScope.importClassifierAlias(aliasName, (ClassifierDescriptor) descriptor);
|
||||
return;
|
||||
}
|
||||
if (descriptor instanceof NamespaceDescriptor) {
|
||||
namespaceScope.importNamespaceAlias(aliasName, (NamespaceDescriptor) descriptor);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (descriptor instanceof FunctionDescriptor) {
|
||||
namespaceScope.importFunctionAlias(aliasName, (FunctionDescriptor) descriptor);
|
||||
return;
|
||||
}
|
||||
if (descriptor instanceof PropertyDescriptor) {
|
||||
namespaceScope.importVariableAlias(aliasName, (PropertyDescriptor) descriptor);
|
||||
if (descriptor instanceof VariableDescriptor) {
|
||||
namespaceScope.importVariableAlias(aliasName, (VariableDescriptor) descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,7 +167,7 @@ public class ImportsResolver {
|
||||
trace.report(NO_CLASS_OBJECT.on(classReference, classDescriptor));
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return lookupDescriptorsForSimpleNameReference(memberReference, classObjectType.getMemberScope());
|
||||
return addImplicitReceiver(lookupDescriptorsForSimpleNameReference(memberReference, classObjectType.getMemberScope()), classDescriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -176,9 +176,25 @@ public class ImportsResolver {
|
||||
|
||||
JetType variableType = variableDescriptor.getReturnType();
|
||||
if (variableType == null) return Collections.emptyList();
|
||||
return lookupDescriptorsForSimpleNameReference(memberReference, variableType.getMemberScope());
|
||||
return addImplicitReceiver(lookupDescriptorsForSimpleNameReference(memberReference, variableType.getMemberScope()), variableDescriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Collection<DeclarationDescriptor> addImplicitReceiver(@NotNull Collection<DeclarationDescriptor> descriptors, @NotNull final DeclarationDescriptor implicitReceiver) {
|
||||
return Collections2.transform(descriptors, new Function<DeclarationDescriptor, DeclarationDescriptor>() {
|
||||
@Override
|
||||
public DeclarationDescriptor apply(@Nullable DeclarationDescriptor descriptor) {
|
||||
if (descriptor instanceof FunctionDescriptor) {
|
||||
return new FunctionDescriptorWithImplicitReceiver((FunctionDescriptor) descriptor, implicitReceiver);
|
||||
}
|
||||
if (descriptor instanceof VariableDescriptor) {
|
||||
return new VariableDescriptorWithImplicitReceiver((VariableDescriptor) descriptor, implicitReceiver);
|
||||
}
|
||||
return descriptor;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Collection<DeclarationDescriptor> lookupDescriptorsForSimpleNameReference(@NotNull JetSimpleNameExpression referenceExpression, @NotNull JetScope outerScope) {
|
||||
List<DeclarationDescriptor> descriptors = Lists.newArrayList();
|
||||
|
||||
@@ -21,7 +21,7 @@ public interface WritableScope extends JetScope {
|
||||
|
||||
void addVariableDescriptor(@NotNull VariableDescriptor variableDescriptor);
|
||||
|
||||
void addPropertyDescriptor(@NotNull PropertyDescriptor propertyDescriptor);
|
||||
void addPropertyDescriptor(@NotNull VariableDescriptor propertyDescriptor);
|
||||
|
||||
void addFunctionDescriptor(@NotNull FunctionDescriptor functionDescriptor);
|
||||
|
||||
@@ -52,5 +52,5 @@ public interface WritableScope extends JetScope {
|
||||
|
||||
void importFunctionAlias(@NotNull String aliasName, @NotNull FunctionDescriptor functionDescriptor);
|
||||
|
||||
void importVariableAlias(@NotNull String aliasName, @NotNull PropertyDescriptor variableDescriptor);
|
||||
void importVariableAlias(@NotNull String aliasName, @NotNull VariableDescriptor variableDescriptor);
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ public class WritableScopeImpl extends WritableScopeWithImports {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void importVariableAlias(@NotNull String aliasName, @NotNull PropertyDescriptor variableDescriptor) {
|
||||
public void importVariableAlias(@NotNull String aliasName, @NotNull VariableDescriptor variableDescriptor) {
|
||||
checkMayWrite();
|
||||
|
||||
addPropertyDescriptor(variableDescriptor);
|
||||
@@ -173,7 +173,7 @@ public class WritableScopeImpl extends WritableScopeWithImports {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPropertyDescriptor(@NotNull PropertyDescriptor propertyDescriptor) {
|
||||
public void addPropertyDescriptor(@NotNull VariableDescriptor propertyDescriptor) {
|
||||
addVariableDescriptor(propertyDescriptor, true);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -193,7 +193,7 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public void importVariableAlias(@NotNull String aliasName, @NotNull PropertyDescriptor variableDescriptor) {
|
||||
public void importVariableAlias(@NotNull String aliasName, @NotNull VariableDescriptor variableDescriptor) {
|
||||
checkMayWrite();
|
||||
|
||||
getCurrentIndividualImportScope().addVariableAlias(aliasName, variableDescriptor);
|
||||
|
||||
@@ -139,7 +139,7 @@ public class WriteThroughScope extends WritableScopeWithImports {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPropertyDescriptor(@NotNull PropertyDescriptor propertyDescriptor) {
|
||||
public void addPropertyDescriptor(@NotNull VariableDescriptor propertyDescriptor) {
|
||||
checkMayWrite();
|
||||
|
||||
writableWorker.addPropertyDescriptor(propertyDescriptor);
|
||||
|
||||
@@ -7,10 +7,20 @@ import b.ext //extension function
|
||||
import b.value //property
|
||||
import b.C.bar //function from class object
|
||||
import b.C.cValue //property from class object
|
||||
import b.constant.fff //function from val
|
||||
import b.constant.dValue //property from val
|
||||
import b.E.f //val from class object
|
||||
|
||||
fun test(arg: B) {
|
||||
foo(value)
|
||||
arg.ext()
|
||||
|
||||
bar()
|
||||
foo(cValue)
|
||||
|
||||
fff(dValue)
|
||||
|
||||
f.f()
|
||||
}
|
||||
|
||||
//FILE:b.kt
|
||||
@@ -18,7 +28,7 @@ package b
|
||||
|
||||
class B() {}
|
||||
|
||||
fun foo(<!UNUSED_PARAMETER!>i<!>: Int) {}
|
||||
fun foo(i: Int) = i
|
||||
|
||||
fun B.ext() {}
|
||||
|
||||
@@ -29,4 +39,21 @@ class C() {
|
||||
fun bar() {}
|
||||
val cValue = 1
|
||||
}
|
||||
}
|
||||
|
||||
class D() {
|
||||
fun fff(s: String) = s
|
||||
val dValue = "w"
|
||||
}
|
||||
|
||||
val constant = D()
|
||||
|
||||
class E() {
|
||||
class object {
|
||||
val f = F()
|
||||
}
|
||||
}
|
||||
|
||||
class F() {
|
||||
fun f() {}
|
||||
}
|
||||
Reference in New Issue
Block a user