Rename, convert and optimize CompositeTypeSubstitution

- Get rid of storing array of inner substituions
- Rename is important as it wasn't actually composition
This commit is contained in:
Denis Zharkov
2015-07-24 11:50:42 +03:00
parent 70bf4d2faf
commit 557450677f
4 changed files with 17 additions and 38 deletions
@@ -170,7 +170,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
@Override
public ClassDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
if (substitutor.isEmpty()) return this;
return new LazySubstitutingClassDescriptor(this, TypeSubstitutor.create(substitutor.getSubstitution(), getSubstitutor().getSubstitution()));
return new LazySubstitutingClassDescriptor(this, TypeSubstitutor.createChainedSubstitutor(substitutor.getSubstitution(), getSubstitutor().getSubstitution()));
}
@Override
@@ -61,7 +61,7 @@ public class DescriptorSubstitutor {
result.add(substituted);
}
TypeSubstitutor substitutor = TypeSubstitutor.create(
TypeSubstitutor substitutor = TypeSubstitutor.createChainedSubstitutor(
originalSubstitution, TypeConstructorSubstitution.createByConstructorsMap(mutableSubstitution)
);
@@ -17,45 +17,24 @@
package org.jetbrains.kotlin.types;
import org.jetbrains.annotations.NotNull;
import kotlin.platform.platformStatic
public class CompositeTypeSubstitution extends TypeSubstitution {
private final TypeSubstitution[] inner;
public class DisjointKeysUnionTypeSubstitution private constructor(
private val first: TypeSubstitution,
private val second: TypeSubstitution
) : TypeSubstitution() {
companion object {
platformStatic public fun create(first: TypeSubstitution, second: TypeSubstitution): TypeSubstitution {
if (first.isEmpty()) return second
if (second.isEmpty()) return first
public CompositeTypeSubstitution(@NotNull TypeSubstitution... inner) {
this.inner = inner;
}
@Override
public TypeProjection get(@NotNull JetType key) {
for (TypeSubstitution substitution : inner) {
TypeProjection value = substitution.get(key);
if (value != null) return value;
return DisjointKeysUnionTypeSubstitution(first, second)
}
return null;
}
@Override
public boolean isEmpty() {
for (TypeSubstitution substitution : inner) {
if (!substitution.isEmpty()) return false;
}
return true;
}
override fun get(key: JetType) = first[key] ?: second[key]
@Override
public boolean approximateCapturedTypes() {
for (TypeSubstitution substitution : inner) {
if (substitution.approximateCapturedTypes()) return true;
}
return false;
}
override fun isEmpty() = false
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (TypeSubstitution substitution : inner) {
builder.append(substitution).append(" * ");
}
return builder.toString();
}
override fun approximateCapturedTypes() = first.approximateCapturedTypes() || second.approximateCapturedTypes()
}
@@ -46,8 +46,8 @@ public class TypeSubstitutor {
}
@NotNull
public static TypeSubstitutor create(@NotNull TypeSubstitution... substitutions) {
return create(new CompositeTypeSubstitution(substitutions));
public static TypeSubstitutor createChainedSubstitutor(@NotNull TypeSubstitution first, @NotNull TypeSubstitution second) {
return create(DisjointKeysUnionTypeSubstitution.create(first, second));
}
@NotNull