From f2635289b1923866843e1dd47423bd6ca74c2cb1 Mon Sep 17 00:00:00 2001 From: Paweł Marks Date: Fri, 21 Aug 2020 16:29:39 +0200 Subject: Replace SelfType with family of RecursiveTypes --- .../dokka/analysis/TypeReferenceFactory.kt | 28 ++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'kotlin-analysis') diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/TypeReferenceFactory.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/TypeReferenceFactory.kt index e07672d4..aae1f189 100644 --- a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/TypeReferenceFactory.kt +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/TypeReferenceFactory.kt @@ -12,7 +12,7 @@ import org.jetbrains.kotlin.types.TypeProjection fun TypeReference.Companion.from(d: ReceiverParameterDescriptor): TypeReference? = when (d.value) { - is ExtensionReceiver -> fromPossiblyNullable(d.type) + is ExtensionReceiver -> fromPossiblyNullable(d.type, emptyList()) else -> run { println("Unknown value type for $d") null @@ -20,31 +20,35 @@ fun TypeReference.Companion.from(d: ReceiverParameterDescriptor): TypeReference? } fun TypeReference.Companion.from(d: ValueParameterDescriptor): TypeReference? = - fromPossiblyNullable(d.type) + fromPossiblyNullable(d.type, emptyList()) fun TypeReference.Companion.from(p: PsiClass) = TypeReference -private fun TypeReference.Companion.fromPossiblyNullable(t: KotlinType, self: KotlinType? = null): TypeReference = - from(t, self).let { if (t.isMarkedNullable) Nullable(it) else it } +private fun TypeReference.Companion.fromPossiblyNullable(t: KotlinType, paramTrace: List): TypeReference = + fromPossiblyRecursive(t, paramTrace).let { if (t.isMarkedNullable) Nullable(it) else it } -private fun TypeReference.Companion.from(t: KotlinType, self: KotlinType? = null): TypeReference = - if (self is KotlinType && self.constructor == t.constructor && self.arguments == t.arguments) - SelfType - else when (val d = t.constructor.declarationDescriptor) { +private fun TypeReference.Companion.fromPossiblyRecursive(t: KotlinType, paramTrace: List): TypeReference = + paramTrace.indexOfFirst { it.constructor == t.constructor && it.arguments == t.arguments } + .takeIf { it >= 0 } + ?.let(::RecursiveType) + ?: from(t, paramTrace) + +private fun TypeReference.Companion.from(t: KotlinType, paramTrace: List): TypeReference = + when (val d = t.constructor.declarationDescriptor) { is TypeParameterDescriptor -> TypeParam( - d.upperBounds.map { fromPossiblyNullable(it, self ?: t) } + d.upperBounds.map { fromPossiblyNullable(it, paramTrace + t) } ) else -> TypeConstructor( t.constructorName.orEmpty(), - t.arguments.map { fromProjection(it, self) } + t.arguments.map { fromProjection(it, paramTrace) } ) } -private fun TypeReference.Companion.fromProjection(t: TypeProjection, r: KotlinType? = null): TypeReference = +private fun TypeReference.Companion.fromProjection(t: TypeProjection, paramTrace: List): TypeReference = if (t.isStarProjection) { StarProjection } else { - fromPossiblyNullable(t.type, r) + fromPossiblyNullable(t.type, paramTrace) } private val KotlinType.constructorName -- cgit