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
@@ -302,7 +302,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
private JvmClassSignature signature() {
BothSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS);
typeMapper.writeFormalTypeParameters(descriptor.getTypeConstructor().getParameters(), sw);
typeMapper.writeFormalTypeParameters(descriptor.getDeclaredTypeParameters(), sw);
sw.writeSuperclass();
if (superClassType == null) {
@@ -143,6 +143,12 @@ public class MutableClassDescriptor extends ClassDescriptorBase implements Class
this.typeParameters = new ArrayList<TypeParameterDescriptor>(typeParameters);
}
@NotNull
@Override
public List<TypeParameterDescriptor> getDeclaredTypeParameters() {
return typeParameters;
}
public void createTypeConstructor() {
assert typeConstructor == null : typeConstructor;
this.typeConstructor = TypeConstructorImpl.createForClass(
@@ -195,7 +195,7 @@ public class TypeParameterBoundIsNotArrayChecker : DeclarationChecker {
bindingContext: BindingContext
) {
val typeParameters = (descriptor as? CallableDescriptor)?.typeParameters
?: (descriptor as? ClassDescriptor)?.typeConstructor?.parameters
?: (descriptor as? ClassDescriptor)?.declaredTypeParameters
?: return
for (typeParameter in typeParameters) {
@@ -222,7 +222,7 @@ public class ReifiedTypeParameterAnnotationChecker : DeclarationChecker {
}
if (descriptor is ClassDescriptor) {
checkTypeParameterDescriptorsAreNotReified(descriptor.getTypeConstructor().getParameters(), diagnosticHolder)
checkTypeParameterDescriptorsAreNotReified(descriptor.declaredTypeParameters, diagnosticHolder)
}
}
@@ -117,7 +117,7 @@ class SamAdapterFunctionsScope(storageManager: StorageManager) : BaseImportingSc
//TODO: non-inner classes
for (parent in ownerClass.parentsWithSelf) {
if (parent !is ClassDescriptor) break
sourceTypeParams += parent.typeConstructor.parameters
sourceTypeParams += parent.declaredTypeParameters
}
//TODO: duplicated parameter names
@@ -252,7 +252,7 @@ public class DeclarationsChecker {
EffectiveVisibility classVisibility = EffectiveVisibility.Companion.forClass(classDescriptor);
List<KtTypeParameter> typeParameterList = klass.getTypeParameters();
int i = 0;
for (TypeParameterDescriptor typeParameterDescriptor : classDescriptor.getTypeConstructor().getParameters()) {
for (TypeParameterDescriptor typeParameterDescriptor : classDescriptor.getDeclaredTypeParameters()) {
if (i >= typeParameterList.size()) return;
for (KotlinType upperBound : typeParameterDescriptor.getUpperBounds()) {
EffectiveVisibility upperBoundVisibility = EffectiveVisibility.Companion.forType(upperBound);
@@ -96,7 +96,7 @@ class VarianceChecker(private val trace: BindingTrace) {
val containingClass = descriptor.getContainingDeclaration()
if (containingClass !is ClassDescriptor) return true
return containingClass.getTypeConstructor().getParameters().all { it.getVariance() == INVARIANT }
return containingClass.typeConstructor.parameters.all { it.getVariance() == INVARIANT }
}
private fun recordPrivateToThis(descriptor: CallableMemberDescriptor) {
@@ -37,7 +37,7 @@ class ClassResolutionScopesSupport(
) {
private fun scopeWithGenerics(parent: LexicalScope, debugName: String): LexicalScopeImpl {
return LexicalScopeImpl(parent, classDescriptor, false, null, debugName) {
classDescriptor.typeConstructor.parameters.forEach { addClassifierDescriptor(it) }
classDescriptor.declaredTypeParameters.forEach { addClassifierDescriptor(it) }
}
}
@@ -101,9 +101,10 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
private final boolean isCompanionObject;
private final ClassResolutionScopesSupport resolutionScopesSupport;
private final NotNullLazyValue<List<TypeParameterDescriptor>> parameters;
public LazyClassDescriptor(
@NotNull LazyClassContext c,
@NotNull final LazyClassContext c,
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Name name,
@NotNull JetClassLikeInfo classLikeInfo
@@ -239,6 +240,30 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
return getOuterScope();
}
}, classLikeInfo.getPrimaryConstructorParameters());
this.parameters = c.getStorageManager().createLazyValue(new Function0<List<TypeParameterDescriptor>>() {
@Override
public List<TypeParameterDescriptor> invoke() {
JetClassLikeInfo classInfo = declarationProvider.getOwnerInfo();
KtTypeParameterList typeParameterList = classInfo.getTypeParameterList();
if (typeParameterList == null) return Collections.emptyList();
if (classInfo.getClassKind() == ClassKind.ENUM_CLASS) {
c.getTrace().report(TYPE_PARAMETERS_IN_ENUM.on(typeParameterList));
}
List<KtTypeParameter> typeParameters = typeParameterList.getParameters();
List<TypeParameterDescriptor> parameters = new ArrayList<TypeParameterDescriptor>(typeParameters.size());
for (int i = 0; i < typeParameters.size(); i++) {
parameters.add(new LazyTypeParameterDescriptor(c, LazyClassDescriptor.this, typeParameters.get(i), i));
}
return parameters;
}
});
}
// NOTE: Called from constructor!
@@ -489,6 +514,12 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
getVisibility();
}
@NotNull
@Override
public List<TypeParameterDescriptor> getDeclaredTypeParameters() {
return parameters.invoke();
}
private static class Supertypes {
@Mutable
public final Collection<KotlinType> trueSupertypes;
@@ -545,21 +576,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
private final NotNullLazyValue<List<TypeParameterDescriptor>> parameters = c.getStorageManager().createLazyValue(new Function0<List<TypeParameterDescriptor>>() {
@Override
public List<TypeParameterDescriptor> invoke() {
JetClassLikeInfo classInfo = declarationProvider.getOwnerInfo();
KtTypeParameterList typeParameterList = classInfo.getTypeParameterList();
if (typeParameterList == null) return Collections.emptyList();
if (classInfo.getClassKind() == ClassKind.ENUM_CLASS) {
c.getTrace().report(TYPE_PARAMETERS_IN_ENUM.on(typeParameterList));
}
List<KtTypeParameter> typeParameters = typeParameterList.getParameters();
List<TypeParameterDescriptor> parameters = new ArrayList<TypeParameterDescriptor>(typeParameters.size());
for (int i = 0; i < typeParameters.size(); i++) {
parameters.add(new LazyTypeParameterDescriptor(c, LazyClassDescriptor.this, typeParameters.get(i), i));
}
return parameters;
return LazyClassDescriptor.this.getDeclaredTypeParameters();
}
});
@@ -104,7 +104,7 @@ public class DescriptorSerializer {
new MutableTypeTable(),
false
);
for (TypeParameterDescriptor typeParameter : descriptor.getTypeConstructor().getParameters()) {
for (TypeParameterDescriptor typeParameter : descriptor.getDeclaredTypeParameters()) {
serializer.typeParameters.intern(typeParameter);
}
return serializer;
@@ -137,7 +137,7 @@ public class DescriptorSerializer {
builder.setFqName(getClassId(classDescriptor));
for (TypeParameterDescriptor typeParameterDescriptor : classDescriptor.getTypeConstructor().getParameters()) {
for (TypeParameterDescriptor typeParameterDescriptor : classDescriptor.getDeclaredTypeParameters()) {
builder.addTypeParameter(typeParameter(typeParameterDescriptor));
}
@@ -234,7 +234,7 @@ public class DescriptorValidator {
public Boolean visitClassDescriptor(
ClassDescriptor descriptor, DiagnosticCollector collector
) {
validateTypeParameters(collector, descriptor.getTypeConstructor().getParameters());
validateTypeParameters(collector, descriptor.getDeclaredTypeParameters());
Collection<KotlinType> supertypes = descriptor.getTypeConstructor().getSupertypes();
if (supertypes.isEmpty() && descriptor.getKind() != ClassKind.INTERFACE
@@ -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()
@@ -134,7 +134,7 @@ private fun getClassInnerScope(outerScope: LexicalScope, descriptor: ClassDescri
val headerScope = LexicalScopeImpl(outerScope, descriptor, false, descriptor.thisAsReceiverParameter,
"Class ${descriptor.getName()} header scope") {
for (typeParameter in descriptor.getTypeConstructor().getParameters()) {
for (typeParameter in descriptor.declaredTypeParameters) {
addClassifierDescriptor(typeParameter)
}
for (constructor in descriptor.getConstructors()) {
@@ -327,7 +327,7 @@ public class ResolveElementCache(
val declaration = jetTypeConstraint.getParentOfType<KtDeclaration>(true)!!
val descriptor = analyzer.resolveToDescriptor(declaration) as ClassDescriptor
for (parameterDescriptor in descriptor.getTypeConstructor().getParameters()) {
for (parameterDescriptor in descriptor.declaredTypeParameters) {
ForceResolveUtil.forceResolveAllContents<TypeParameterDescriptor>(parameterDescriptor)
}
@@ -166,7 +166,7 @@ class BasicLookupElementFactory(
}
is ClassDescriptor -> {
val typeParams = descriptor.getTypeConstructor().getParameters()
val typeParams = descriptor.declaredTypeParameters
if (includeClassTypeArguments && typeParams.isNotEmpty()) {
element = element.appendTailText(typeParams.map { it.getName().asString() }.joinToString(", ", "<", ">"), true)
}
@@ -295,7 +295,7 @@ class TypeInstantiationItems(
private val freeParameters: Collection<TypeParameterDescriptor>,
private val tail: Tail?) : InheritanceItemsSearcher {
private val baseHasTypeArgs = classDescriptor.typeConstructor.parameters.isNotEmpty()
private val baseHasTypeArgs = classDescriptor.declaredTypeParameters.isNotEmpty()
private val expectedType = KotlinTypeImpl.create(Annotations.EMPTY, classDescriptor, false, typeArgs)
private val expectedFuzzyType = FuzzyType(expectedType, freeParameters)
@@ -309,7 +309,7 @@ class TypeInstantiationItems(
if (!visibilityFilter(descriptor)) continue
var inheritorFuzzyType = FuzzyType(descriptor.defaultType, descriptor.typeConstructor.parameters)
val hasTypeArgs = descriptor.getTypeConstructor().getParameters().isNotEmpty()
val hasTypeArgs = descriptor.declaredTypeParameters.isNotEmpty()
if (hasTypeArgs || baseHasTypeArgs) {
val substitutor = inheritorFuzzyType.checkIsSubtypeOf(expectedFuzzyType) ?: continue
if (!substitutor.isEmpty) {
@@ -184,7 +184,7 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
val paramName = equalsFun.valueParameters.first().name!!.quoteIfNeeded()
var typeForCast = IdeDescriptorRenderers.SOURCE_CODE.renderClassifierName(classDescriptor)
val typeParams = classDescriptor.defaultType.constructor.parameters
val typeParams = classDescriptor.declaredTypeParameters
if (typeParams.isNotEmpty()) {
typeForCast += typeParams.map { "*" }.joinToString(prefix = "<", postfix = ">")
}
@@ -349,7 +349,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
assert (receiverClassDescriptor is JavaClassDescriptor) { "Unexpected receiver class: $receiverClassDescriptor" }
val projections = receiverClassDescriptor.getTypeConstructor().getParameters()
val projections = receiverClassDescriptor.declaredTypeParameters
.map { TypeProjectionImpl(it.getDefaultType()) }
val memberScope = receiverClassDescriptor.getMemberScope(projections)
@@ -47,7 +47,7 @@ class KotlinPullUpData(val sourceClass: KtClassOrObject,
val targetClassDescriptor = targetClass.getClassDescriptorIfAny(resolutionFacade)!!
val typeParametersInSourceClassContext by lazy {
sourceClassDescriptor.typeConstructor.parameters +
sourceClassDescriptor.declaredTypeParameters +
sourceClass.getResolutionScope(sourceClassContext, resolutionFacade)
.collectDescriptorsFiltered(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS)
.filterIsInstance<TypeParameterDescriptor>()