resolveSupertypes() refactored
* ClassDescriptor passed explicitly, psi-based checks and trace reading eliminated * some clarifying renames/extractions made
This commit is contained in:
@@ -48,7 +48,6 @@ import javax.inject.Inject;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.jetbrains.jet.lang.diagnostics.Errors.*;
|
import static org.jetbrains.jet.lang.diagnostics.Errors.*;
|
||||||
import static org.jetbrains.jet.lang.resolve.BindingContext.CLASS;
|
|
||||||
import static org.jetbrains.jet.lang.resolve.BindingContext.CONSTRUCTOR;
|
import static org.jetbrains.jet.lang.resolve.BindingContext.CONSTRUCTOR;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -112,43 +111,46 @@ public class DescriptorResolver {
|
|||||||
@NotNull MutableClassDescriptor descriptor,
|
@NotNull MutableClassDescriptor descriptor,
|
||||||
BindingTrace trace
|
BindingTrace trace
|
||||||
) {
|
) {
|
||||||
for (JetType supertype : resolveSupertypes(descriptor.getScopeForSupertypeResolution(), jetClass, trace)) {
|
for (JetType supertype : resolveSupertypes(descriptor.getScopeForSupertypeResolution(), descriptor, jetClass, trace)) {
|
||||||
descriptor.addSupertype(supertype);
|
descriptor.addSupertype(supertype);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<JetType> resolveSupertypes(@NotNull JetScope scope, @NotNull JetClassOrObject jetClass, BindingTrace trace) {
|
public List<JetType> resolveSupertypes(
|
||||||
List<JetType> result = Lists.newArrayList();
|
@NotNull JetScope scope,
|
||||||
|
@NotNull ClassDescriptor classDescriptor,
|
||||||
|
@NotNull JetClassOrObject jetClass,
|
||||||
|
BindingTrace trace
|
||||||
|
) {
|
||||||
|
List<JetType> supertypes = Lists.newArrayList();
|
||||||
List<JetDelegationSpecifier> delegationSpecifiers = jetClass.getDelegationSpecifiers();
|
List<JetDelegationSpecifier> delegationSpecifiers = jetClass.getDelegationSpecifiers();
|
||||||
boolean isEnum = jetClass instanceof JetClass && ((JetClass) jetClass).hasModifier(JetTokens.ENUM_KEYWORD);
|
boolean isEnum = classDescriptor.getKind() == ClassKind.ENUM_CLASS;
|
||||||
if (delegationSpecifiers.isEmpty()) {
|
if (delegationSpecifiers.isEmpty()) {
|
||||||
if (!isEnum) {
|
if (!isEnum) {
|
||||||
result.add(getDefaultSupertype(jetClass, trace));
|
supertypes.add(getDefaultSupertype(jetClass, trace));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Collection<JetType> supertypes = resolveDelegationSpecifiers(
|
Collection<JetType> declaredSupertypes = resolveDelegationSpecifiers(
|
||||||
scope,
|
scope,
|
||||||
delegationSpecifiers,
|
delegationSpecifiers,
|
||||||
typeResolver, trace, false);
|
typeResolver, trace, false);
|
||||||
for (JetType supertype : supertypes) {
|
supertypes.addAll(declaredSupertypes);
|
||||||
result.add(supertype);
|
}
|
||||||
|
if (isEnum && !containsClass(supertypes)) {
|
||||||
|
supertypes.add(0, JetStandardLibrary.getInstance().getEnumType(classDescriptor.getDefaultType()));
|
||||||
|
}
|
||||||
|
return supertypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean containsClass(Collection<JetType> result) {
|
||||||
|
for (JetType type : result) {
|
||||||
|
ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
|
||||||
|
if (descriptor instanceof ClassDescriptor && ((ClassDescriptor) descriptor).getKind() != ClassKind.TRAIT) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isEnum) {
|
return false;
|
||||||
boolean hasSuper = false;
|
|
||||||
for (JetType type : result) {
|
|
||||||
ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
|
|
||||||
if (descriptor instanceof ClassDescriptor && ((ClassDescriptor) descriptor).getKind() != ClassKind.TRAIT) {
|
|
||||||
hasSuper = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!hasSuper) {
|
|
||||||
ClassDescriptor classDescriptor = trace.getBindingContext().get(CLASS, jetClass);
|
|
||||||
result.add(0, JetStandardLibrary.getInstance().getEnumType(classDescriptor.getDefaultType()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JetType getDefaultSupertype(JetClassOrObject jetClass, BindingTrace trace) {
|
private JetType getDefaultSupertype(JetClassOrObject jetClass, BindingTrace trace) {
|
||||||
|
|||||||
@@ -340,7 +340,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
|
|||||||
else {
|
else {
|
||||||
List<JetType> allSupertypes = resolveSession.getInjector().getDescriptorResolver()
|
List<JetType> allSupertypes = resolveSession.getInjector().getDescriptorResolver()
|
||||||
.resolveSupertypes(getScopeForClassHeaderResolution(),
|
.resolveSupertypes(getScopeForClassHeaderResolution(),
|
||||||
classOrObject,
|
LazyClassDescriptor.this, classOrObject,
|
||||||
resolveSession.getTrace());
|
resolveSession.getTrace());
|
||||||
List<JetType> validSupertypes = Lists.newArrayList(Collections2.filter(allSupertypes, VALID_SUPERTYPE));
|
List<JetType> validSupertypes = Lists.newArrayList(Collections2.filter(allSupertypes, VALID_SUPERTYPE));
|
||||||
this.supertypes = validSupertypes;
|
this.supertypes = validSupertypes;
|
||||||
|
|||||||
Reference in New Issue
Block a user