Skip to content

Commit

Permalink
use psi based type reference to defer type resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
neetopia committed Aug 24, 2023
1 parent e43fde1 commit 7ed007e
Show file tree
Hide file tree
Showing 21 changed files with 498 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.kotlin.KSTypeImpl
import com.google.devtools.ksp.impl.symbol.kotlin.KSTypeReferenceImpl
import com.google.devtools.ksp.impl.symbol.kotlin.KSValueArgumentImpl
import com.google.devtools.ksp.impl.symbol.kotlin.analyze
import com.google.devtools.ksp.impl.symbol.kotlin.classifierSymbol
import com.google.devtools.ksp.impl.symbol.kotlin.getDefaultValue
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.impl.symbol.kotlin.toKSDeclaration
import com.google.devtools.ksp.impl.symbol.kotlin.toKtClassSymbol
import com.google.devtools.ksp.impl.symbol.kotlin.toLocation
Expand Down Expand Up @@ -53,7 +53,7 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation) :
}

override val annotationType: KSTypeReference by lazy {
KSTypeReferenceImpl.getCached(type, this)
KSTypeReferenceResolvedImpl.getCached(type, this)
}

override val arguments: List<KSValueArgument> by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.google.devtools.ksp.IdKeyPair
import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.impl.symbol.java.KSValueArgumentLiteImpl
import com.google.devtools.ksp.impl.symbol.java.calcValue
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.processing.impl.KSNameImpl
import com.google.devtools.ksp.symbol.*
import com.intellij.psi.PsiClass
Expand All @@ -42,7 +43,7 @@ class KSAnnotationImpl private constructor(

override val annotationType: KSTypeReference by lazy {
analyze {
KSTypeReferenceImpl.getCached(buildClassType(annotationApplication.classId!!))
KSTypeReferenceResolvedImpl.getCached(buildClassType(annotationApplication.classId!!))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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.impl.symbol.kotlin.resolved.KSTypeArgumentResolvedImpl
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.symbol.*
import org.jetbrains.kotlin.analysis.api.types.KtFunctionalType
import org.jetbrains.kotlin.analysis.api.types.KtType
Expand All @@ -15,18 +17,18 @@ class KSCallableReferenceImpl private constructor(
cache.getOrPut(IdKeyPair(ktFunctionalType, parent)) { KSCallableReferenceImpl(ktFunctionalType, parent) }
}
override val receiverType: KSTypeReference?
get() = ktFunctionalType.receiverType?.let { KSTypeReferenceImpl.getCached(it) }
get() = ktFunctionalType.receiverType?.let { KSTypeReferenceResolvedImpl.getCached(it) }

override val functionParameters: List<KSValueParameter>
get() = ktFunctionalType.parameterTypes.map {
KSValueParameterLiteImpl.getCached(it, this@KSCallableReferenceImpl)
}

override val returnType: KSTypeReference
get() = KSTypeReferenceImpl.getCached(ktFunctionalType.returnType)
get() = KSTypeReferenceResolvedImpl.getCached(ktFunctionalType.returnType)

override val typeArguments: List<KSTypeArgument>
get() = ktFunctionalType.typeArguments().map { KSTypeArgumentImpl.getCached(it, this) }
get() = ktFunctionalType.typeArguments().map { KSTypeArgumentResolvedImpl.getCached(it, this) }

override val origin: Origin
get() = parent?.origin ?: Origin.SYNTHETIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.processing.impl.KSNameImpl
import com.google.devtools.ksp.processing.impl.KSTypeReferenceSyntheticImpl
import com.google.devtools.ksp.symbol.*
import org.jetbrains.kotlin.analysis.api.KtStarTypeProjection
import org.jetbrains.kotlin.analysis.api.components.buildClassType
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.descriptors.java.JavaVisibilities
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtObjectDeclaration

class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSymbol: KtClassOrObjectSymbol) :
Expand Down Expand Up @@ -62,9 +66,15 @@ class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSym
}

override val superTypes: Sequence<KSTypeReference> by lazy {
analyze {
(ktClassOrObjectSymbol.psiIfSource() as? KtClassOrObject)?.superTypeListEntries?.map {
KSTypeReferenceImpl.getCached(it.typeReference!!)
}?.asSequence()?.ifEmpty {
sequenceOf(
KSTypeReferenceSyntheticImpl.getCached(ResolverAAImpl.instance.builtIns.anyType, this)
)
} ?: analyze {
val supers = ktClassOrObjectSymbol.superTypes.mapIndexed { index, type ->
KSTypeReferenceImpl.getCached(type, this@KSClassDeclarationImpl, index)
KSTypeReferenceResolvedImpl.getCached(type, this@KSClassDeclarationImpl, index)
}
// AA is returning additional kotlin.Any for java classes, explicitly extending kotlin.Any will result in
// compile error, therefore filtering by name should work.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,77 +1,62 @@
/*
* Copyright 2023 Google LLC
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.types.KtClassType
import org.jetbrains.kotlin.analysis.api.types.KtClassTypeQualifier
import org.jetbrains.kotlin.analysis.api.types.KtTypeParameterType
import com.google.devtools.ksp.findParentOfType
import com.google.devtools.ksp.symbol.KSClassifierReference
import com.google.devtools.ksp.symbol.KSNode
import com.google.devtools.ksp.symbol.KSTypeArgument
import com.google.devtools.ksp.symbol.Location
import com.google.devtools.ksp.symbol.Origin
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.psi.KtUserType

class KSClassifierReferenceImpl private constructor(
internal val ktType: KtClassType,
internal val index: Int,
override val parent: KSTypeReference?
) : KSClassifierReference {
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) }
class KSClassifierReferenceImpl private constructor(val ktUserType: KtUserType) : KSClassifierReference {
companion object : KSObjectCache<KtUserType, KSClassifierReferenceImpl>() {
fun getCached(ktUserType: KtUserType) = cache.getOrPut(ktUserType) { KSClassifierReferenceImpl(ktUserType) }
}

private val classifierReference: KtClassTypeQualifier
get() = ktType.qualifiers[index]
override val origin = Origin.KOTLIN

override val qualifier: KSClassifierReference? by lazy {
if (index == 0) {
null
} else {
getCached(ktType, index - 1, parent)
}
override val location: Location by lazy {
ktUserType.toLocation()
}

override fun referencedName(): String {
return classifierReference.name.asString()
override val parent: KSNode? by lazy {
ktUserType.findParentOfType<KtTypeReference>()?.let { KSTypeReferenceImpl.getCached(it) }
}

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

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()
return ktUserType.referencedName ?: ""
}

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()
override val qualifier: KSClassifierReference? by lazy {
if (ktUserType.qualifier == null) {
null
} else {
KSClassifierReferenceImpl.getCached(ktUserType.qualifier!!)
}
}

override fun toString() = referencedName()
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.*
import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.processing.impl.KSNameImpl
import com.google.devtools.ksp.symbol.*
import com.intellij.psi.PsiClass
Expand Down Expand Up @@ -74,22 +75,25 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo
if (!ktFunctionSymbol.isExtension) {
null
} else {
ktFunctionSymbol.receiverType?.let {
KSTypeReferenceImpl.getCached(it, this@KSFunctionDeclarationImpl)
}
(ktFunctionSymbol.psiIfSource() as? KtFunction)?.receiverTypeReference
?.let { KSTypeReferenceImpl.getCached(it) }
?: ktFunctionSymbol.receiverType?.let {
KSTypeReferenceResolvedImpl.getCached(it, this@KSFunctionDeclarationImpl)
}
}
}
}

override val returnType: KSTypeReference? by lazy {
analyze {
// Constructors
if (ktFunctionSymbol is KtConstructorSymbol) {
((parentDeclaration as KSClassDeclaration).asStarProjectedType() as KSTypeImpl).type
} else {
ktFunctionSymbol.returnType
}.let { KSTypeReferenceImpl.getCached(it, this@KSFunctionDeclarationImpl) }
}
(ktFunctionSymbol.psiIfSource() as? KtFunction)?.typeReference?.let { KSTypeReferenceImpl.getCached(it) }
?: analyze {
// Constructors
if (ktFunctionSymbol is KtConstructorSymbol) {
((parentDeclaration as KSClassDeclaration).asStarProjectedType() as KSTypeImpl).type
} else {
ktFunctionSymbol.returnType
}.let { KSTypeReferenceResolvedImpl.getCached(it, this@KSFunctionDeclarationImpl) }
}
}

override val parameters: List<KSValueParameter> by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.toKSModifiers
import org.jetbrains.kotlin.analysis.api.annotations.annotations
Expand All @@ -27,6 +28,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtPropertySetterSymbol
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor

abstract class KSPropertyAccessorImpl(
Expand Down Expand Up @@ -125,7 +127,9 @@ class KSPropertyGetterImpl private constructor(
}

override val returnType: KSTypeReference? by lazy {
KSTypeReferenceImpl.getCached(getter.returnType, this@KSPropertyGetterImpl)
((owner as? KSPropertyDeclarationImpl)?.ktPropertySymbol?.psiIfSource() as? KtProperty)?.typeReference
?.let { KSTypeReferenceImpl.getCached(it) }
?: KSTypeReferenceResolvedImpl.getCached(getter.returnType, this@KSPropertyGetterImpl)
}

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.google.devtools.ksp.impl.symbol.kotlin
import com.google.devtools.ksp.BinaryClassInfoCache
import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.processing.impl.KSNameImpl
import com.google.devtools.ksp.symbol.*
import com.intellij.psi.PsiClass
Expand All @@ -32,6 +33,7 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.descriptors.java.JavaVisibilities
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
import org.jetbrains.kotlin.psi.KtProperty

class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbol: KtPropertySymbol) :
KSPropertyDeclaration,
Expand Down Expand Up @@ -85,11 +87,15 @@ class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbo
}

override val extensionReceiver: KSTypeReference? by lazy {
ktPropertySymbol.receiverType?.let { KSTypeReferenceImpl.getCached(it, this@KSPropertyDeclarationImpl) }
(ktPropertySymbol.psiIfSource() as? KtProperty)?.receiverTypeReference
?.let { KSTypeReferenceImpl.getCached(it) }
?: ktPropertySymbol.receiverType
?.let { KSTypeReferenceResolvedImpl.getCached(it, this@KSPropertyDeclarationImpl) }
}

override val type: KSTypeReference by lazy {
KSTypeReferenceImpl.getCached(ktPropertySymbol.returnType, this@KSPropertyDeclarationImpl)
(ktPropertySymbol.psiIfSource() as? KtProperty)?.typeReference?.let { KSTypeReferenceImpl.getCached(it) }
?: KSTypeReferenceResolvedImpl.getCached(ktPropertySymbol.returnType, this@KSPropertyDeclarationImpl)
}

override val isMutable: Boolean by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.processing.impl.KSNameImpl
import com.google.devtools.ksp.symbol.*
import org.jetbrains.kotlin.analysis.api.symbols.KtJavaFieldSymbol
Expand All @@ -27,7 +28,7 @@ class KSPropertyDeclarationJavaImpl private constructor(private val ktJavaFieldS
get() = null

override val type: KSTypeReference by lazy {
KSTypeReferenceImpl.getCached(ktJavaFieldSymbol.returnType, this@KSPropertyDeclarationJavaImpl)
KSTypeReferenceResolvedImpl.getCached(ktJavaFieldSymbol.returnType, this@KSPropertyDeclarationJavaImpl)
}

override val isMutable: Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.processing.impl.KSNameImpl
import com.google.devtools.ksp.symbol.KSExpectActual
import com.google.devtools.ksp.symbol.KSName
Expand Down Expand Up @@ -29,7 +30,7 @@ class KSPropertyDeclarationLocalVariableImpl private constructor(
override val extensionReceiver: KSTypeReference? = null

override val type: KSTypeReference by lazy {
KSTypeReferenceImpl.getCached(ktLocalVariableSymbol.returnType)
KSTypeReferenceResolvedImpl.getCached(ktLocalVariableSymbol.returnType)
}

override val isMutable: Boolean = !ktLocalVariableSymbol.isVal
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.KSObjectCache
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.processing.impl.KSNameImpl
import com.google.devtools.ksp.symbol.KSExpectActual
import com.google.devtools.ksp.symbol.KSName
Expand All @@ -40,7 +41,7 @@ class KSTypeAliasImpl private constructor(private val ktTypeAliasSymbol: KtTypeA
}

override val type: KSTypeReference by lazy {
KSTypeReferenceImpl.getCached(ktTypeAliasSymbol.expandedType, this)
KSTypeReferenceResolvedImpl.getCached(ktTypeAliasSymbol.expandedType, this)
}

override val simpleName: KSName
Expand Down
Loading

0 comments on commit 7ed007e

Please sign in to comment.