Add substitution concept into JetType

In most cases it's just a map { Parameter_i => Argument_i }
Will be used when loading Raw types from Java.

Also add ClassDescriptor.getMemberScope by substitution.
This commit is contained in:
Denis Zharkov
2015-07-22 18:40:13 +03:00
parent 557450677f
commit 37420527f2
12 changed files with 87 additions and 2 deletions
@@ -271,6 +271,8 @@ class LazyJavaTypeResolver(
return (descriptor as ClassDescriptor).getMemberScope(getArguments())
}
override fun computeCustomSubstitution(): TypeSubstitution? = null
private val nullable = c.storageManager.createLazyValue l@ {
when (attr.flexibility) {
FLEXIBLE_LOWER_BOUND -> return@l false
@@ -22,6 +22,7 @@ import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.types.JetType;
import org.jetbrains.kotlin.types.TypeProjection;
import org.jetbrains.kotlin.types.TypeSubstitution;
import org.jetbrains.kotlin.types.TypeSubstitutor;
import java.util.Collection;
@@ -31,6 +32,9 @@ public interface ClassDescriptor extends ClassifierDescriptor, MemberDescriptor,
@NotNull
JetScope getMemberScope(@NotNull List<? extends TypeProjection> typeArguments);
@NotNull
JetScope getMemberScope(@NotNull TypeSubstitution typeSubstitution);
@NotNull
JetScope getUnsubstitutedMemberScope();
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.storage.StorageManager;
import org.jetbrains.kotlin.types.*;
import java.util.List;
import java.util.Map;
public abstract class AbstractClassDescriptor implements ClassDescriptor {
private final Name name;
@@ -94,6 +93,15 @@ public abstract class AbstractClassDescriptor implements ClassDescriptor {
return new SubstitutingScope(getUnsubstitutedMemberScope(), substitutor);
}
@NotNull
@Override
public JetScope getMemberScope(@NotNull TypeSubstitution typeSubstitution) {
if (typeSubstitution.isEmpty()) return getUnsubstitutedMemberScope();
TypeSubstitutor substitutor = TypeSubstitutor.create(typeSubstitution);
return new SubstitutingScope(getUnsubstitutedMemberScope(), substitutor);
}
@NotNull
@Override
public ClassDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
@@ -97,6 +97,16 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
return new SubstitutingScope(memberScope, getSubstitutor());
}
@NotNull
@Override
public JetScope getMemberScope(@NotNull TypeSubstitution typeSubstitution) {
JetScope memberScope = original.getMemberScope(typeSubstitution);
if (originalSubstitutor.isEmpty()) {
return memberScope;
}
return new SubstitutingScope(memberScope, getSubstitutor());
}
@NotNull
@Override
public JetScope getUnsubstitutedMemberScope() {
@@ -32,6 +32,10 @@ public abstract class AbstractLazyType(storageManager: StorageManager) : Abstrac
protected abstract fun computeArguments(): List<TypeProjection>
override fun getSubstitution() = computeCustomSubstitution() ?: IndexedParametersSubstitution(constructor, getArguments())
protected open fun computeCustomSubstitution(): TypeSubstitution? = null
private val memberScope = storageManager.createLazyValue { computeMemberScope() }
override fun getMemberScope() = memberScope()
@@ -39,6 +39,12 @@ public abstract class DelegatingType implements JetType {
return getDelegate().getArguments();
}
@NotNull
@Override
public TypeSubstitution getSubstitution() {
return getDelegate().getSubstitution();
}
@Override
public boolean isMarkedNullable() {
return getDelegate().isMarkedNullable();
@@ -314,6 +314,12 @@ public class ErrorUtils {
public JetScope getMemberScope(@NotNull List<? extends TypeProjection> typeArguments) {
return createErrorScope("Error scope for class " + getName() + " with arguments: " + typeArguments);
}
@NotNull
@Override
public JetScope getMemberScope(@NotNull TypeSubstitution typeSubstitution) {
return createErrorScope("Error scope for class " + getName() + " with arguments: " + typeSubstitution);
}
}
@NotNull
@@ -463,6 +469,12 @@ public class ErrorUtils {
return arguments;
}
@NotNull
@Override
public TypeSubstitution getSubstitution() {
return new IndexedParametersSubstitution(constructor, arguments);
}
@Override
public boolean isMarkedNullable() {
return false;
@@ -36,6 +36,9 @@ public interface JetType extends Annotated {
@ReadOnly
List<TypeProjection> getArguments();
@NotNull
TypeSubstitution getSubstitution();
boolean isMarkedNullable();
@NotNull
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
@@ -29,12 +30,14 @@ public final class JetTypeImpl extends AbstractJetType {
private final boolean nullable;
private final JetScope memberScope;
private final Annotations annotations;
private final TypeSubstitution substitution;
public JetTypeImpl(
@NotNull Annotations annotations,
@NotNull TypeConstructor constructor,
boolean nullable,
@NotNull List<? extends TypeProjection> arguments,
@Nullable TypeSubstitution substitution,
@NotNull JetScope memberScope
) {
this.annotations = annotations;
@@ -47,6 +50,17 @@ public final class JetTypeImpl extends AbstractJetType {
this.nullable = nullable;
this.arguments = arguments;
this.memberScope = memberScope;
this.substitution = substitution;
}
public JetTypeImpl(
@NotNull Annotations annotations,
@NotNull TypeConstructor constructor,
boolean nullable,
@NotNull List<? extends TypeProjection> arguments,
@NotNull JetScope memberScope
) {
this(annotations, constructor, nullable, arguments, null, memberScope);
}
@NotNull
@@ -55,6 +69,15 @@ public final class JetTypeImpl extends AbstractJetType {
return annotations;
}
@NotNull
@Override
public TypeSubstitution getSubstitution() {
if (substitution == null) {
return new IndexedParametersSubstitution(getConstructor(), getArguments());
}
return substitution;
}
@NotNull
@Override
public TypeConstructor getConstructor() {
@@ -65,6 +65,12 @@ public class TypeUtils {
throw new IllegalStateException(name);
}
@NotNull
@Override
public TypeSubstitution getSubstitution() {
throw new IllegalStateException(name);
}
@Override
public boolean isMarkedNullable() {
throw new IllegalStateException(name);
@@ -785,6 +791,12 @@ public class TypeUtils {
public Annotations getAnnotations() {
return delegate.getAnnotations();
}
@NotNull
@Override
public TypeSubstitution getSubstitution() {
return delegate.getSubstitution();
}
}
private static class NullableType extends AbstractTypeWithKnownNullability {
@@ -52,7 +52,6 @@ class DeserializedType(
c.components.annotationAndConstantLoader.loadTypeAnnotations(typeProto, c.nameResolver)
}
override fun isMarkedNullable(): Boolean = typeProto.getNullable()
private fun getTypeMemberScope(constructor: TypeConstructor, typeArguments: List<TypeProjection>): JetScope {
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl
import org.jetbrains.kotlin.resolve.scopes.UsageLocation
import org.jetbrains.kotlin.types.ErrorUtils.createErrorType
import org.jetbrains.kotlin.types.TypeProjection
import org.jetbrains.kotlin.types.TypeSubstitution
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.error.MissingDependencyErrorClass
import org.jetbrains.kotlin.utils.Printer
@@ -81,4 +82,5 @@ private class MissingDependencyErrorClassDescriptor(
override fun getUnsubstitutedMemberScope() = scope
override fun getMemberScope(typeArguments: List<TypeProjection?>) = scope
override fun getMemberScope(typeSubstitution: TypeSubstitution) = scope
}