Skip to content

Commit

Permalink
AA: rework reference element.
Browse files Browse the repository at this point in the history
* merge upstream changes for qualifiers from KtClassType.
* support KSDefNonNullReference
  • Loading branch information
neetopia committed Mar 13, 2023
1 parent a482031 commit 94203ed
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import org.jetbrains.kotlin.analysis.api.types.KtType

class KSCallableReferenceImpl private constructor(
private val ktFunctionalType: KtFunctionalType,
override val parent: KSNode
override val parent: KSNode?
) : KSCallableReference {
companion object : KSObjectCache<IdKeyPair<KtType, KSNode>, KSCallableReference>() {
fun getCached(ktFunctionalType: KtFunctionalType, parent: KSNode): KSCallableReference =
companion object : KSObjectCache<IdKeyPair<KtType, KSNode?>, KSCallableReference>() {
fun getCached(ktFunctionalType: KtFunctionalType, parent: KSNode?): KSCallableReference =
cache.getOrPut(IdKeyPair(ktFunctionalType, parent)) { KSCallableReferenceImpl(ktFunctionalType, parent) }
}
override val receiverType: KSTypeReference?
Expand All @@ -29,8 +29,8 @@ class KSCallableReferenceImpl private constructor(
get() = ktFunctionalType.typeArguments().map { KSTypeArgumentImpl.getCached(it, this) }

override val origin: Origin
get() = parent.origin
get() = parent?.origin ?: Origin.SYNTHETIC

override val location: Location
get() = parent.location
get() = parent?.location ?: NonExistLocation
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,77 @@
package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.IdKeyPair
import com.google.devtools.ksp.IdKeyTriple
import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.symbol.*
import org.jetbrains.kotlin.analysis.api.components.buildClassType
import org.jetbrains.kotlin.analysis.api.types.KtUsualClassType
import org.jetbrains.kotlin.analysis.api.types.KtClassType
import org.jetbrains.kotlin.analysis.api.types.KtClassTypeQualifier
import org.jetbrains.kotlin.analysis.api.types.KtTypeParameterType

class KSClassifierReferenceImpl private constructor(
internal val ktType: KtUsualClassType,
internal val ktType: KtClassType,
internal val index: Int,
override val parent: KSTypeReference?
) : KSClassifierReference {
companion object : KSObjectCache<IdKeyPair<KtUsualClassType, KSTypeReference?>, KSClassifierReferenceImpl>() {
fun getCached(ktType: KtUsualClassType, parent: KSTypeReference?) =
cache.getOrPut(IdKeyPair(ktType, parent)) { KSClassifierReferenceImpl(ktType, parent) }
companion object : KSObjectCache<IdKeyTriple<KtClassType, Int, KSTypeReference?>, KSClassifierReferenceImpl>() {
fun getCached(ktType: KtClassType, index: Int, parent: KSTypeReference?) =
cache.getOrPut(IdKeyTriple(ktType, index, parent)) { KSClassifierReferenceImpl(ktType, index, parent) }
}

private val classifierReference: KtClassTypeQualifier
get() = ktType.qualifiers[index]

override val qualifier: KSClassifierReference? by lazy {
ktType.classId.outerClassId?.let {
analyze {
buildClassType(it)
}
}?.let { getCached(it as KtUsualClassType, parent) }
if (index == 0) {
null
} else {
getCached(ktType, index - 1, parent)
}
}

override fun referencedName(): String {
return ktType.classId.asFqNameString()
return classifierReference.name.asString()
}

override val typeArguments: List<KSTypeArgument> by lazy {
ktType.typeArguments().map { KSTypeArgumentImpl.getCached(it, this) }
classifierReference.typeArguments.map { KSTypeArgumentImpl.getCached(it, this) }
}

override val origin: Origin = parent?.origin ?: Origin.SYNTHETIC

override val location: Location
get() = parent?.location ?: NonExistLocation

override fun toString(): String {
return referencedName()
}
}

class KSClassifierParameterImpl private constructor(
internal val ktType: KtTypeParameterType,
override val parent: KSTypeReference?
) : KSClassifierReference {
companion object : KSObjectCache<IdKeyPair<KtTypeParameterType, KSTypeReference?>, KSClassifierParameterImpl>() {
fun getCached(ktType: KtTypeParameterType, parent: KSTypeReference?) =
KSClassifierParameterImpl.cache.getOrPut(IdKeyPair(ktType, parent)) {
KSClassifierParameterImpl(ktType, parent)
}
}

override val qualifier: KSClassifierReference? = null

override fun referencedName(): String {
return ktType.name.asString()
}

override val typeArguments: List<KSTypeArgument>
get() = emptyList()
override val origin: Origin
get() = parent?.origin ?: Origin.SYNTHETIC
override val location: Location
get() = parent?.location ?: NonExistLocation

override fun toString(): String {
return referencedName()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.IdKeyPair
import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.symbol.*
import org.jetbrains.kotlin.analysis.api.types.KtDefinitelyNotNullType

class KSDefNonNullReferenceImpl private constructor(
val ktDefinitelyNotNullType: KtDefinitelyNotNullType,
override val parent: KSTypeReference?
) : KSDefNonNullReference {
companion object : KSObjectCache<IdKeyPair<KtDefinitelyNotNullType, KSTypeReference?>, KSDefNonNullReference>() {
fun getCached(ktType: KtDefinitelyNotNullType, parent: KSTypeReference?) =
KSDefNonNullReferenceImpl.cache
.getOrPut(IdKeyPair(ktType, parent)) { KSDefNonNullReferenceImpl(ktType, parent) }
}
override val enclosedType: KSClassifierReference by lazy {
ktDefinitelyNotNullType.original.toClassifierReference(parent) as KSClassifierReference
}
override val typeArguments: List<KSTypeArgument>
get() = emptyList()

override val origin: Origin = Origin.KOTLIN

override val location: Location
get() = parent?.location ?: NonExistLocation
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.google.devtools.ksp.impl.symbol.kotlin
import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.symbol.*

class KSDynamicReferenceImpl private constructor(override val parent: KSNode?) : KSDynamicReference {
class KSDynamicReferenceImpl private constructor(override val parent: KSNode) : KSDynamicReference {
companion object : KSObjectCache<KSTypeReference, KSDynamicReferenceImpl>() {
fun getCached(parent: KSTypeReference) = cache.getOrPut(parent) { KSDynamicReferenceImpl(parent) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.ExceptionMessage
import com.google.devtools.ksp.IdKeyTriple
import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.symbol.*
Expand All @@ -42,18 +41,7 @@ class KSTypeReferenceImpl private constructor(
if (parent == null || parent.origin == Origin.SYNTHETIC) {
null
} else {
when (ktType) {
is KtFunctionalType -> KSCallableReferenceImpl.getCached(ktType, this@KSTypeReferenceImpl)
is KtDynamicType -> KSDynamicReferenceImpl.getCached(this@KSTypeReferenceImpl)
is KtUsualClassType -> KSClassifierReferenceImpl.getCached(ktType, this@KSTypeReferenceImpl)
is KtFlexibleType -> KSClassifierReferenceImpl.getCached(
ktType.lowerBound as KtUsualClassType,
this@KSTypeReferenceImpl
)
is KtErrorType -> null
is KtTypeParameterType -> null
else -> throw IllegalStateException("Unexpected type element ${ktType.javaClass}, $ExceptionMessage")
}
ktType.toClassifierReference(this)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.ExceptionMessage
import com.google.devtools.ksp.getDocString
import com.google.devtools.ksp.impl.KSPCoreEnvironment
import com.google.devtools.ksp.impl.ResolverAAImpl
Expand Down Expand Up @@ -137,6 +138,19 @@ internal fun KtType.render(inFunctionType: Boolean = false): String {
}
}

internal fun KtType.toClassifierReference(parent: KSTypeReference?): KSReferenceElement? {
return when (val ktType = this) {
is KtFunctionalType -> KSCallableReferenceImpl.getCached(ktType, parent)
is KtDynamicType -> KSDynamicReferenceImpl.getCached(parent!!)
is KtUsualClassType -> KSClassifierReferenceImpl.getCached(ktType, ktType.qualifiers.size - 1, parent)
is KtFlexibleType -> ktType.lowerBound.toClassifierReference(parent)
is KtErrorType -> null
is KtTypeParameterType -> KSClassifierParameterImpl.getCached(ktType, parent)
is KtDefinitelyNotNullType -> KSDefNonNullReferenceImpl.getCached(ktType, parent)
else -> throw IllegalStateException("Unexpected type element ${ktType.javaClass}, $ExceptionMessage")
}
}

internal fun KSTypeArgument.toKtTypeProjection(): KtTypeProjection {
val variance = when (this.variance) {
com.google.devtools.ksp.symbol.Variance.INVARIANT -> Variance.INVARIANT
Expand Down

0 comments on commit 94203ed

Please sign in to comment.