Got rid of useless nested class.
This commit is contained in:
@@ -26,6 +26,7 @@ import org.jetbrains.jet.lang.diagnostics.Errors;
|
|||||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||||
import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
|
import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
|
||||||
|
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
|
||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -33,6 +34,8 @@ import java.util.Collection;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getFQName;
|
||||||
|
|
||||||
public class OverloadResolver {
|
public class OverloadResolver {
|
||||||
private TopDownAnalysisContext context;
|
private TopDownAnalysisContext context;
|
||||||
private BindingTrace trace;
|
private BindingTrace trace;
|
||||||
@@ -55,93 +58,65 @@ public class OverloadResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void checkOverloads() {
|
private void checkOverloads() {
|
||||||
Pair<MultiMap<ClassDescriptor, ConstructorDescriptor>, MultiMap<Key, ConstructorDescriptor>> pair = constructorsGrouped();
|
MultiMap<ClassDescriptor, ConstructorDescriptor> inClasses = MultiMap.create();
|
||||||
MultiMap<ClassDescriptor, ConstructorDescriptor> inClasses = pair.first;
|
MultiMap<FqNameUnsafe, ConstructorDescriptor> inPackages = MultiMap.create();
|
||||||
MultiMap<Key, ConstructorDescriptor> inNamespaces = pair.second;
|
fillGroupedConstructors(inClasses, inPackages);
|
||||||
|
|
||||||
for (Map.Entry<JetClassOrObject, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
|
for (Map.Entry<JetClassOrObject, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
|
||||||
checkOverloadsInAClass(entry.getValue(), entry.getKey(), inClasses.get(entry.getValue()));
|
checkOverloadsInAClass(entry.getValue(), entry.getKey(), inClasses.get(entry.getValue()));
|
||||||
}
|
}
|
||||||
checkOverloadsInANamespace(inNamespaces);
|
checkOverloadsInPackages(inPackages);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Key extends Pair<String, Name> {
|
private void fillGroupedConstructors(
|
||||||
Key(String packageName, Name name) {
|
@NotNull MultiMap<ClassDescriptor, ConstructorDescriptor> inClasses,
|
||||||
super(packageName, name);
|
@NotNull MultiMap<FqNameUnsafe, ConstructorDescriptor> inPackages
|
||||||
}
|
) {
|
||||||
|
|
||||||
Key(PackageFragmentDescriptor fragment, Name name) {
|
|
||||||
this(fragment.getFqName().asString(), name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getNamespace() {
|
|
||||||
return first;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Name getFunctionName() {
|
|
||||||
return second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private Pair<MultiMap<ClassDescriptor, ConstructorDescriptor>, MultiMap<Key, ConstructorDescriptor>>
|
|
||||||
constructorsGrouped()
|
|
||||||
{
|
|
||||||
MultiMap<ClassDescriptor, ConstructorDescriptor> inClasses = MultiMap.create();
|
|
||||||
MultiMap<Key, ConstructorDescriptor> inPackages = MultiMap.create();
|
|
||||||
|
|
||||||
for (MutableClassDescriptor klass : context.getClasses().values()) {
|
for (MutableClassDescriptor klass : context.getClasses().values()) {
|
||||||
if (klass.getKind().isSingleton()) {
|
if (klass.getKind().isSingleton()) {
|
||||||
// Constructors of singletons aren't callable from the code, so they shouldn't participate in overload name checking
|
// Constructors of singletons aren't callable from the code, so they shouldn't participate in overload name checking
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
DeclarationDescriptor containingDeclaration = klass.getContainingDeclaration();
|
DeclarationDescriptor containingDeclaration = klass.getContainingDeclaration();
|
||||||
if (containingDeclaration instanceof PackageFragmentDescriptor) {
|
if (containingDeclaration instanceof ClassDescriptor) {
|
||||||
PackageFragmentDescriptor packageFragment = (PackageFragmentDescriptor) containingDeclaration;
|
|
||||||
inPackages.put(new Key(packageFragment, klass.getName()), klass.getConstructors());
|
|
||||||
}
|
|
||||||
else if (containingDeclaration instanceof ClassDescriptor) {
|
|
||||||
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
|
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
|
||||||
inClasses.put(classDescriptor, klass.getConstructors());
|
inClasses.put(classDescriptor, klass.getConstructors());
|
||||||
}
|
}
|
||||||
|
else if (containingDeclaration instanceof PackageFragmentDescriptor) {
|
||||||
|
inPackages.put(getFQName(klass), klass.getConstructors());
|
||||||
|
}
|
||||||
else if (!(containingDeclaration instanceof FunctionDescriptor)) {
|
else if (!(containingDeclaration instanceof FunctionDescriptor)) {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Pair.create(inClasses, inPackages);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkOverloadsInANamespace(MultiMap<Key, ConstructorDescriptor> inNamespaces) {
|
private void checkOverloadsInPackages(MultiMap<FqNameUnsafe, ConstructorDescriptor> inPackages) {
|
||||||
|
|
||||||
MultiMap<Key, CallableMemberDescriptor> functionsByName = MultiMap.create();
|
MultiMap<FqNameUnsafe, CallableMemberDescriptor> functionsByName = MultiMap.create();
|
||||||
|
|
||||||
for (SimpleFunctionDescriptor function : context.getFunctions().values()) {
|
for (SimpleFunctionDescriptor function : context.getFunctions().values()) {
|
||||||
DeclarationDescriptor containingDeclaration = function.getContainingDeclaration();
|
if (function.getContainingDeclaration() instanceof PackageFragmentDescriptor) {
|
||||||
if (containingDeclaration instanceof PackageFragmentDescriptor) {
|
functionsByName.putValue(getFQName(function), function);
|
||||||
PackageFragmentDescriptor packageFragment = (PackageFragmentDescriptor) containingDeclaration;
|
|
||||||
functionsByName.putValue(new Key(packageFragment, function.getName()), function);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (PropertyDescriptor property : context.getProperties().values()) {
|
for (PropertyDescriptor property : context.getProperties().values()) {
|
||||||
DeclarationDescriptor containingDeclaration = property.getContainingDeclaration();
|
if (property.getContainingDeclaration() instanceof PackageFragmentDescriptor) {
|
||||||
if (containingDeclaration instanceof PackageFragmentDescriptor) {
|
functionsByName.putValue(getFQName(property), property);
|
||||||
PackageFragmentDescriptor packageFragment = (PackageFragmentDescriptor) containingDeclaration;
|
|
||||||
functionsByName.putValue(new Key(packageFragment, property.getName()), property);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Map.Entry<Key, Collection<ConstructorDescriptor>> entry : inNamespaces.entrySet()) {
|
for (Map.Entry<FqNameUnsafe, Collection<ConstructorDescriptor>> entry : inPackages.entrySet()) {
|
||||||
functionsByName.putValues(entry.getKey(), entry.getValue());
|
functionsByName.putValues(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Map.Entry<Key, Collection<CallableMemberDescriptor>> e : functionsByName.entrySet()) {
|
for (Map.Entry<FqNameUnsafe, Collection<CallableMemberDescriptor>> e : functionsByName.entrySet()) {
|
||||||
checkOverloadsWithSameName(e.getKey().getFunctionName(), e.getValue(), e.getKey().getNamespace());
|
checkOverloadsWithSameName(e.getValue(), e.getKey().parent().asString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String nameForErrorMessage(ClassDescriptor classDescriptor, JetClassOrObject jetClass) {
|
private static String nameForErrorMessage(ClassDescriptor classDescriptor, JetClassOrObject jetClass) {
|
||||||
String name = jetClass.getName();
|
String name = jetClass.getName();
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
return name;
|
return name;
|
||||||
@@ -178,16 +153,19 @@ public class OverloadResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Map.Entry<Name, Collection<CallableMemberDescriptor>> e : functionsByName.entrySet()) {
|
for (Map.Entry<Name, Collection<CallableMemberDescriptor>> e : functionsByName.entrySet()) {
|
||||||
checkOverloadsWithSameName(e.getKey(), e.getValue(), nameForErrorMessage(classDescriptor, klass));
|
checkOverloadsWithSameName(e.getValue(), nameForErrorMessage(classDescriptor, klass));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kotlin has no secondary constructors at this time
|
// Kotlin has no secondary constructors at this time
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkOverloadsWithSameName(@NotNull Name name, Collection<CallableMemberDescriptor> functions, @NotNull String functionContainer) {
|
private void checkOverloadsWithSameName(
|
||||||
|
Collection<CallableMemberDescriptor> functions,
|
||||||
|
@NotNull String functionContainer
|
||||||
|
) {
|
||||||
if (functions.size() == 1) {
|
if (functions.size() == 1) {
|
||||||
// microoptimization
|
// micro-optimization
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Set<Pair<JetDeclaration, CallableMemberDescriptor>> redeclarations = findRedeclarations(functions);
|
Set<Pair<JetDeclaration, CallableMemberDescriptor>> redeclarations = findRedeclarations(functions);
|
||||||
|
|||||||
Reference in New Issue
Block a user