Introduce ClassDescriptor.declaredTypeParameters

Should be a subset of type constructor's parameters
This commit is contained in:
Denis Zharkov
2015-11-03 13:56:04 +03:00
parent 57094954a3
commit c9aa75259b
26 changed files with 130 additions and 70 deletions
@@ -112,6 +112,16 @@ class LazyJavaClassDescriptor(
}
}
private val declaredParameters = c.storageManager.createLazyValue {
jClass.typeParameters.map {
p ->
c.typeParameterResolver.resolveTypeParameter(p)
?: throw AssertionError("Parameter $p surely belongs to class $jClass, so it must be resolved")
}
}
override fun getDeclaredTypeParameters() = declaredParameters()
override fun getFunctionTypeForSamInterface(): KotlinType? = functionTypeForSamInterface()
override fun isCompanionObject() = false
@@ -121,11 +131,7 @@ class LazyJavaClassDescriptor(
private inner class LazyJavaClassTypeConstructor : AbstractClassTypeConstructor() {
private val parameters = c.storageManager.createLazyValue {
jClass.getTypeParameters().map {
p ->
c.typeParameterResolver.resolveTypeParameter(p)
?: throw AssertionError("Parameter $p surely belongs to class $jClass, so it must be resolved")
}
this@LazyJavaClassDescriptor.declaredTypeParameters
}
override fun getParameters(): List<TypeParameterDescriptor> = parameters()
@@ -66,6 +66,26 @@ public class FunctionClassDescriptor(
private val typeConstructor = FunctionTypeConstructor()
private val memberScope = FunctionClassScope(storageManager, this)
private val parameters: List<TypeParameterDescriptor>
init {
val result = ArrayList<TypeParameterDescriptor>()
fun typeParameter(variance: Variance, name: String) {
result.add(TypeParameterDescriptorImpl.createWithDefaultBound(
this@FunctionClassDescriptor, Annotations.EMPTY, false, variance, Name.identifier(name), result.size()
))
}
(1..arity).map { i ->
typeParameter(Variance.IN_VARIANCE, "P$i")
}
typeParameter(Variance.OUT_VARIANCE, "R")
parameters = result.toReadOnlyList()
}
override fun getContainingDeclaration() = containingDeclaration
override fun getStaticScope() = staticScope
@@ -86,26 +106,9 @@ public class FunctionClassDescriptor(
override fun getAnnotations() = Annotations.EMPTY
override fun getSource() = SourceElement.NO_SOURCE
override fun getDeclaredTypeParameters() = parameters
private inner class FunctionTypeConstructor : AbstractClassTypeConstructor() {
private val parameters: List<TypeParameterDescriptor>
init {
val result = ArrayList<TypeParameterDescriptor>()
fun typeParameter(variance: Variance, name: String) {
result.add(TypeParameterDescriptorImpl.createWithDefaultBound(
this@FunctionClassDescriptor, Annotations.EMPTY, false, variance, Name.identifier(name), result.size()
))
}
(1..arity).map { i ->
typeParameter(Variance.IN_VARIANCE, "P$i")
}
typeParameter(Variance.OUT_VARIANCE, "R")
parameters = result.toReadOnlyList()
}
private val supertypes = storageManager.createLazyValue {
val result = ArrayList<KotlinType>(2)
@@ -138,7 +141,7 @@ public class FunctionClassDescriptor(
result.toReadOnlyList()
}
override fun getParameters() = parameters
override fun getParameters() = this@FunctionClassDescriptor.parameters
override fun getSupertypes(): Collection<KotlinType> = supertypes()
@@ -56,7 +56,7 @@ public class FunctionInvokeDescriptor private constructor(
companion object Factory {
fun create(functionClass: FunctionClassDescriptor): FunctionInvokeDescriptor {
val typeParameters = functionClass.getTypeConstructor().getParameters()
val typeParameters = functionClass.declaredTypeParameters
val result = FunctionInvokeDescriptor(functionClass, null, CallableMemberDescriptor.Kind.DECLARATION)
result.initialize(
@@ -94,4 +94,13 @@ public interface ClassDescriptor extends ClassifierDescriptor, MemberDescriptor,
@Nullable
ConstructorDescriptor getUnsubstitutedPrimaryConstructor();
/**
* It may differ from 'typeConstructor.parameters' in current class is inner, 'typeConstructor.parameters' contains
* captured parameters from outer declaration.
* @return list of type parameters actually declared type parameters in current class
*/
@ReadOnly
@NotNull
List<TypeParameterDescriptor> getDeclaredTypeParameters();
}
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.types.TypeConstructorImpl;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class ClassDescriptorImpl extends ClassDescriptorBase {
@@ -147,4 +148,10 @@ public class ClassDescriptorImpl extends ClassDescriptorBase {
public String toString() {
return "class " + getName();
}
@NotNull
@Override
public List<TypeParameterDescriptor> getDeclaredTypeParameters() {
return Collections.emptyList();
}
}
@@ -39,10 +39,7 @@ import org.jetbrains.kotlin.types.TypeConstructor;
import org.jetbrains.kotlin.types.TypeConstructorImpl;
import org.jetbrains.kotlin.utils.Printer;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
private final TypeConstructor typeConstructor;
@@ -172,6 +169,12 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
return "enum entry " + getName();
}
@NotNull
@Override
public List<TypeParameterDescriptor> getDeclaredTypeParameters() {
return Collections.emptyList();
}
private class EnumEntryScope extends MemberScopeImpl {
private final MemoizedFunctionToNotNull<Name, Collection<FunctionDescriptor>> functions;
private final MemoizedFunctionToNotNull<Name, Collection<PropertyDescriptor>> properties;
@@ -33,7 +33,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
private final ClassDescriptor original;
private final TypeSubstitutor originalSubstitutor;
private TypeSubstitutor newSubstitutor;
private List<TypeParameterDescriptor> typeParameters;
private List<TypeParameterDescriptor> typeConstructorParameters;
private TypeConstructor typeConstructor;
public LazySubstitutingClassDescriptor(ClassDescriptor descriptor, TypeSubstitutor substitutor) {
@@ -48,9 +48,9 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
}
else {
List<TypeParameterDescriptor> originalTypeParameters = original.getTypeConstructor().getParameters();
typeParameters = new ArrayList<TypeParameterDescriptor>(originalTypeParameters.size());
typeConstructorParameters = new ArrayList<TypeParameterDescriptor>(originalTypeParameters.size());
newSubstitutor = DescriptorSubstitutor.substituteTypeParameters(
originalTypeParameters, originalSubstitutor.getSubstitution(), this, typeParameters
originalTypeParameters, originalSubstitutor.getSubstitution(), this, typeConstructorParameters
);
}
}
@@ -79,7 +79,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
originalTypeConstructor.getAnnotations(),
originalTypeConstructor.isFinal(),
originalTypeConstructor.toString(),
typeParameters,
typeConstructorParameters,
supertypes
);
}
@@ -248,4 +248,11 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
public SourceElement getSource() {
return SourceElement.NO_SOURCE;
}
@NotNull
@Override
public List<TypeParameterDescriptor> getDeclaredTypeParameters() {
getSubstitutor();
return typeConstructorParameters;
}
}
@@ -619,7 +619,7 @@ internal class DescriptorRendererImpl(
val classDescriptor = constructor.getContainingDeclaration()
builder.append(" ")
renderName(classDescriptor, builder)
renderTypeParameters(classDescriptor.getTypeConstructor().getParameters(), builder, false)
renderTypeParameters(classDescriptor.declaredTypeParameters, builder, false)
}
renderValueParameters(constructor.valueParameters, constructor.hasSynthesizedParameterNames(), builder)
@@ -792,7 +792,7 @@ internal class DescriptorRendererImpl(
if (isEnumEntry) return
val typeParameters = klass.typeConstructor.getParameters()
val typeParameters = klass.declaredTypeParameters
renderTypeParameters(typeParameters, builder, false)
if (!klass.kind.isSingleton && classWithPrimaryConstructor) {
@@ -166,6 +166,8 @@ public class DeserializedClassDescriptor(
override fun getSource() = sourceElement
override fun getDeclaredTypeParameters() = c.typeDeserializer.ownTypeParameters
private inner class DeserializedClassTypeConstructor : AbstractClassTypeConstructor() {
private val supertypes = c.storageManager.createLazyValue {
computeSupertypes()