Rewrite BindingContextUtils.descriptorToDeclaration utilities family to use SourceElement for getting PSI

Remove DESCRIPTOR_TO_DECLARATION context key
This commit is contained in:
Pavel V. Talanov
2014-07-07 22:01:56 +04:00
parent 59e43020c3
commit 723b3d75c9
2 changed files with 33 additions and 84 deletions
@@ -158,7 +158,7 @@ public interface BindingContext {
backingFieldRequired = valueNotFound ? false : backingFieldRequired;
assert backingFieldRequired != null;
// TODO: user BindingContextAccessors
PsiElement declarationPsiElement = map.get(BindingContextUtils.DESCRIPTOR_TO_DECLARATION, propertyDescriptor);
PsiElement declarationPsiElement = BindingContextUtils.descriptorToDeclaration(null, propertyDescriptor);
if (declarationPsiElement instanceof JetParameter) {
JetParameter jetParameter = (JetParameter) declarationPsiElement;
return jetParameter.hasValOrVarNode() ||
@@ -193,35 +193,21 @@ public interface BindingContext {
}
};
WritableSlice<PsiElement, ClassDescriptor> CLASS =
Slices.<PsiElement, ClassDescriptor>sliceBuilder().setOpposite((WritableSlice) BindingContextUtils.DESCRIPTOR_TO_DECLARATION)
.build();
WritableSlice<PsiElement, ScriptDescriptor> SCRIPT =
Slices.<PsiElement, ScriptDescriptor>sliceBuilder().setOpposite((WritableSlice) BindingContextUtils.DESCRIPTOR_TO_DECLARATION)
.build();
WritableSlice<PsiElement, ClassDescriptor> CLASS = Slices.<PsiElement, ClassDescriptor>sliceBuilder().build();
WritableSlice<PsiElement, ScriptDescriptor> SCRIPT = Slices.<PsiElement, ScriptDescriptor>sliceBuilder().build();
WritableSlice<JetTypeParameter, TypeParameterDescriptor> TYPE_PARAMETER =
Slices.<JetTypeParameter, TypeParameterDescriptor>sliceBuilder()
.setOpposite((WritableSlice) BindingContextUtils.DESCRIPTOR_TO_DECLARATION).build();
Slices.<JetTypeParameter, TypeParameterDescriptor>sliceBuilder().build();
/**
* @see BindingContextUtils#recordFunctionDeclarationToDescriptor(BindingTrace, PsiElement, SimpleFunctionDescriptor)}
*/
WritableSlice<PsiElement, SimpleFunctionDescriptor> FUNCTION = Slices.<PsiElement, SimpleFunctionDescriptor>sliceBuilder()
.setOpposite((WritableSlice) BindingContextUtils.DESCRIPTOR_TO_DECLARATION).build();
WritableSlice<PsiElement, ConstructorDescriptor> CONSTRUCTOR = Slices.<PsiElement, ConstructorDescriptor>sliceBuilder()
.setOpposite((WritableSlice) BindingContextUtils.DESCRIPTOR_TO_DECLARATION).build();
WritableSlice<PsiElement, VariableDescriptor> VARIABLE =
Slices.<PsiElement, VariableDescriptor>sliceBuilder().setOpposite((WritableSlice) BindingContextUtils.DESCRIPTOR_TO_DECLARATION)
.build();
WritableSlice<JetParameter, VariableDescriptor> VALUE_PARAMETER = Slices.<JetParameter, VariableDescriptor>sliceBuilder()
.setOpposite((WritableSlice) BindingContextUtils.DESCRIPTOR_TO_DECLARATION).build();
WritableSlice<PsiElement, SimpleFunctionDescriptor> FUNCTION = Slices.<PsiElement, SimpleFunctionDescriptor>sliceBuilder().build();
WritableSlice<PsiElement, ConstructorDescriptor> CONSTRUCTOR = Slices.<PsiElement, ConstructorDescriptor>sliceBuilder().build();
WritableSlice<PsiElement, VariableDescriptor> VARIABLE = Slices.<PsiElement, VariableDescriptor>sliceBuilder().build();
WritableSlice<JetParameter, VariableDescriptor> VALUE_PARAMETER = Slices.<JetParameter, VariableDescriptor>sliceBuilder().build();
WritableSlice<JetPropertyAccessor, PropertyAccessorDescriptor> PROPERTY_ACCESSOR =
Slices.<JetPropertyAccessor, PropertyAccessorDescriptor>sliceBuilder()
.setOpposite((WritableSlice) BindingContextUtils.DESCRIPTOR_TO_DECLARATION).build();
// normalize value to getOriginal(value)
Slices.<JetPropertyAccessor, PropertyAccessorDescriptor>sliceBuilder().build();
WritableSlice<PsiElement, PropertyDescriptor> PRIMARY_CONSTRUCTOR_PARAMETER =
Slices.<PsiElement, PropertyDescriptor>sliceBuilder().setOpposite((WritableSlice) BindingContextUtils.DESCRIPTOR_TO_DECLARATION)
.build();
Slices.<PsiElement, PropertyDescriptor>sliceBuilder().build();
WritableSlice[] DECLARATIONS_TO_DESCRIPTORS = new WritableSlice[] {
CLASS, TYPE_PARAMETER, FUNCTION, CONSTRUCTOR, VARIABLE, VALUE_PARAMETER, PROPERTY_ACCESSOR,
@@ -18,7 +18,6 @@ package org.jetbrains.jet.lang.resolve;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
@@ -30,13 +29,16 @@ import org.jetbrains.jet.lang.resolve.bindingContextUtil.BindingContextUtilPacka
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.calls.model.VariableAsFunctionResolvedCall;
import org.jetbrains.jet.lang.resolve.source.SourcePackage;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.JetTypeInfo;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
import org.jetbrains.jet.util.slicedmap.Slices;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor.Kind.DECLARATION;
import static org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor.Kind.SYNTHESIZED;
@@ -47,28 +49,6 @@ public class BindingContextUtils {
private BindingContextUtils() {
}
private static final Slices.KeyNormalizer<DeclarationDescriptor> DECLARATION_DESCRIPTOR_NORMALIZER = new Slices.KeyNormalizer<DeclarationDescriptor>() {
@Override
public DeclarationDescriptor normalize(DeclarationDescriptor declarationDescriptor) {
if (declarationDescriptor instanceof CallableMemberDescriptor) {
CallableMemberDescriptor callable = (CallableMemberDescriptor) declarationDescriptor;
if (callable.getKind() != DECLARATION) {
throw new IllegalStateException("non-declaration descriptors should be filtered out earlier: " + callable);
}
}
//if (declarationDescriptor instanceof VariableAsFunctionDescriptor) {
// VariableAsFunctionDescriptor descriptor = (VariableAsFunctionDescriptor) declarationDescriptor;
// if (descriptor.getOriginal() != descriptor) {
// throw new IllegalStateException("original should be resolved earlier: " + descriptor);
// }
//}
return declarationDescriptor.getOriginal();
}
};
/*package*/ static final ReadOnlySlice<DeclarationDescriptor, PsiElement> DESCRIPTOR_TO_DECLARATION =
Slices.<DeclarationDescriptor, PsiElement>sliceBuilder().setKeyNormalizer(DECLARATION_DESCRIPTOR_NORMALIZER).setDebugName("DESCRIPTOR_TO_DECLARATION").build();
@Nullable
public static VariableDescriptor extractVariableDescriptorIfAny(@NotNull BindingContext bindingContext, @Nullable JetElement element, boolean onlyReference) {
DeclarationDescriptor descriptor = null;
@@ -115,13 +95,17 @@ public class BindingContextUtils {
}
@Nullable
private static PsiElement doGetDescriptorToDeclaration(@NotNull BindingContext context, @NotNull DeclarationDescriptor descriptor) {
return context.get(DESCRIPTOR_TO_DECLARATION, descriptor);
private static PsiElement doGetDescriptorToDeclaration(@NotNull DeclarationDescriptor descriptor) {
DeclarationDescriptor original = descriptor.getOriginal();
if (!(original instanceof DeclarationDescriptorWithSource)) {
return null;
}
return SourcePackage.getPsi(((DeclarationDescriptorWithSource) original).getSource());
}
// NOTE this is also used by KDoc
@Nullable
public static PsiElement descriptorToDeclaration(@NotNull BindingContext context, @NotNull DeclarationDescriptor descriptor) {
public static PsiElement descriptorToDeclaration(@Nullable BindingContext context, @NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof CallableMemberDescriptor) {
return callableDescriptorToDeclaration(context, (CallableMemberDescriptor) descriptor);
}
@@ -129,7 +113,7 @@ public class BindingContextUtils {
return classDescriptorToDeclaration(context, (ClassDescriptor) descriptor);
}
else {
return doGetDescriptorToDeclaration(context, descriptor);
return doGetDescriptorToDeclaration(descriptor);
}
}
@@ -149,28 +133,16 @@ public class BindingContextUtils {
}
@Nullable
public static PsiElement callableDescriptorToDeclaration(@NotNull BindingContext context, @NotNull CallableMemberDescriptor callable) {
if (callable.getKind() == SYNTHESIZED) {
CallableMemberDescriptor original = callable.getOriginal();
if (original instanceof SynthesizedCallableMemberDescriptor<?>) {
DeclarationDescriptor base = ((SynthesizedCallableMemberDescriptor<?>) original).getBaseForSynthesized();
return descriptorToDeclaration(context, base);
}
return null;
public static PsiElement callableDescriptorToDeclaration(@Nullable BindingContext context, @NotNull CallableMemberDescriptor callable) {
if (callable.getKind() == DECLARATION || callable.getKind() == SYNTHESIZED) {
return doGetDescriptorToDeclaration(callable);
}
if (callable.getKind() == DECLARATION) {
return doGetDescriptorToDeclaration(context, callable.getOriginal());
}
//TODO: should not use this method for fake_override and delegation
Set<? extends CallableMemberDescriptor> overriddenDescriptors = callable.getOverriddenDescriptors();
if (overriddenDescriptors.size() != 1) {
throw new IllegalStateException(
"Cannot find declaration: fake descriptor " + callable + " has more than one overridden descriptor:\n" +
StringUtil.join(overriddenDescriptors, ",\n"));
if (overriddenDescriptors.size() == 1) {
return callableDescriptorToDeclaration(overriddenDescriptors.iterator().next());
}
return callableDescriptorToDeclaration(context, overriddenDescriptors.iterator().next());
return null;
}
@NotNull
@@ -178,17 +150,8 @@ public class BindingContextUtils {
@NotNull BindingContext context,
@NotNull CallableMemberDescriptor callable
) {
if (callable.getKind() == SYNTHESIZED) {
CallableMemberDescriptor original = callable.getOriginal();
if (original instanceof SynthesizedCallableMemberDescriptor<?>) {
DeclarationDescriptor base = ((SynthesizedCallableMemberDescriptor<?>) original).getBaseForSynthesized();
return descriptorToDeclarations(context, base);
}
return Collections.emptyList();
}
if (callable.getKind() == DECLARATION) {
PsiElement psiElement = doGetDescriptorToDeclaration(context, callable);
if (callable.getKind() == DECLARATION || callable.getKind() == SYNTHESIZED) {
PsiElement psiElement = doGetDescriptorToDeclaration(callable);
return psiElement != null ? Lists.newArrayList(psiElement) : Lists.<PsiElement>newArrayList();
}
@@ -202,7 +165,7 @@ public class BindingContextUtils {
@Nullable
public static PsiElement classDescriptorToDeclaration(@NotNull BindingContext context, @NotNull ClassDescriptor clazz) {
return doGetDescriptorToDeclaration(context, clazz);
return doGetDescriptorToDeclaration(clazz);
}
public static void recordFunctionDeclarationToDescriptor(@NotNull BindingTrace trace,