From f6e0c5e5d5d3d03ba8a274bb96409f9808188ccd Mon Sep 17 00:00:00 2001 From: Jiaxiang Chen Date: Tue, 16 Jul 2024 09:51:54 -0700 Subject: [PATCH] UPDATE_AA_VERSION: 2.1.0-dev-1329 --- gradle.properties | 2 +- kotlin-analysis-api/build.gradle.kts | 1 - .../devtools/ksp/impl/IncrementalContextAA.kt | 102 ++--- .../ksp/impl/KotlinSymbolProcessing.kt | 65 +-- ...KtAlwaysAccessibleLifeTimeTokenProvider.kt | 11 - .../devtools/ksp/impl/ResolverAAImpl.kt | 51 +-- .../impl/symbol/java/KSAnnotationJavaImpl.kt | 22 +- .../kotlin/AbstractKSDeclarationImpl.kt | 18 +- .../impl/symbol/kotlin/KSAnnotationImpl.kt | 23 +- .../kotlin/KSClassDeclarationEnumEntryImpl.kt | 16 +- .../symbol/kotlin/KSClassDeclarationImpl.kt | 48 +-- .../impl/symbol/kotlin/KSExpectActualImpl.kt | 11 +- .../ksp/impl/symbol/kotlin/KSFileImpl.kt | 28 +- .../ksp/impl/symbol/kotlin/KSFileJavaImpl.kt | 2 +- .../kotlin/KSFunctionDeclarationImpl.kt | 49 +-- .../ksp/impl/symbol/kotlin/KSFunctionImpl.kt | 13 +- .../symbol/kotlin/KSPropertyAccessorImpl.kt | 27 +- .../kotlin/KSPropertyDeclarationImpl.kt | 34 +- .../kotlin/KSPropertyDeclarationJavaImpl.kt | 20 +- .../ksp/impl/symbol/kotlin/KSTypeImpl.kt | 59 +-- .../resolved/KSAnnotationResolvedImpl.kt | 26 +- .../synthetic/KSSyntheticAnnotations.kt | 2 + .../devtools/ksp/impl/symbol/kotlin/util.kt | 381 +++++++++--------- .../standalone/IncrementalJavaFileManager.kt | 2 +- ...ementalKotlinDeclarationProviderFactory.kt | 2 +- ...IncrementalKotlinPackageProviderFactory.kt | 2 +- .../ksp/standalone/KspLibraryModuleBuilder.kt | 15 +- .../ksp/standalone/KspSourceModuleBuilder.kt | 10 +- .../KspStandaloneDirectInheritorsProvider.kt | 18 +- 29 files changed, 542 insertions(+), 518 deletions(-) delete mode 100644 kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KtAlwaysAccessibleLifeTimeTokenProvider.kt diff --git a/gradle.properties b/gradle.properties index 9e95f6fc3b..f8288af75c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ junit5Version=5.8.2 junitPlatformVersion=1.8.2 googleTruthVersion=1.1 -aaKotlinBaseVersion=2.0.20-dev-6501 +aaKotlinBaseVersion=2.1.0-dev-1329 aaIntellijVersion=233.13135.103 aaGuavaVersion=33.2.0-jre aaAsmVersion=9.0 diff --git a/kotlin-analysis-api/build.gradle.kts b/kotlin-analysis-api/build.gradle.kts index 82bb69a847..bf6962c300 100644 --- a/kotlin-analysis-api/build.gradle.kts +++ b/kotlin-analysis-api/build.gradle.kts @@ -61,7 +61,6 @@ dependencies { "org.jetbrains.kotlin:high-level-api-for-ide", "org.jetbrains.kotlin:low-level-api-fir-for-ide", "org.jetbrains.kotlin:analysis-api-platform-interface-for-ide", - "org.jetbrains.kotlin:analysis-project-structure-for-ide", "org.jetbrains.kotlin:symbol-light-classes-for-ide", "org.jetbrains.kotlin:analysis-api-standalone-for-ide", "org.jetbrains.kotlin:high-level-api-impl-base-for-ide", diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/IncrementalContextAA.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/IncrementalContextAA.kt index 7f3766df3b..4effeef2d3 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/IncrementalContextAA.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/IncrementalContextAA.kt @@ -34,20 +34,22 @@ import com.google.devtools.ksp.symbol.KSNode import com.google.devtools.ksp.symbol.Origin import com.intellij.psi.PsiJavaFile import com.intellij.util.containers.MultiMap -import org.jetbrains.kotlin.analysis.api.symbols.KtFunctionLikeSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtJavaFieldSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtPropertySymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol -import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers -import org.jetbrains.kotlin.analysis.api.types.KtCapturedType -import org.jetbrains.kotlin.analysis.api.types.KtDefinitelyNotNullType -import org.jetbrains.kotlin.analysis.api.types.KtDynamicType -import org.jetbrains.kotlin.analysis.api.types.KtErrorType -import org.jetbrains.kotlin.analysis.api.types.KtFlexibleType -import org.jetbrains.kotlin.analysis.api.types.KtIntersectionType -import org.jetbrains.kotlin.analysis.api.types.KtNonErrorClassType -import org.jetbrains.kotlin.analysis.api.types.KtType -import org.jetbrains.kotlin.analysis.api.types.KtTypeParameterType +import org.jetbrains.kotlin.analysis.api.KaExperimentalApi +import org.jetbrains.kotlin.analysis.api.symbols.KaFunctionSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaJavaFieldSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaPropertySymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaSymbol +import org.jetbrains.kotlin.analysis.api.symbols.markers.KaDeclarationContainerSymbol +import org.jetbrains.kotlin.analysis.api.symbols.typeParameters +import org.jetbrains.kotlin.analysis.api.types.KaCapturedType +import org.jetbrains.kotlin.analysis.api.types.KaClassType +import org.jetbrains.kotlin.analysis.api.types.KaDefinitelyNotNullType +import org.jetbrains.kotlin.analysis.api.types.KaDynamicType +import org.jetbrains.kotlin.analysis.api.types.KaErrorType +import org.jetbrains.kotlin.analysis.api.types.KaFlexibleType +import org.jetbrains.kotlin.analysis.api.types.KaIntersectionType +import org.jetbrains.kotlin.analysis.api.types.KaType +import org.jetbrains.kotlin.analysis.api.types.KaTypeParameterType import org.jetbrains.kotlin.incremental.IncrementalCompilationContext import org.jetbrains.kotlin.incremental.LookupStorage import org.jetbrains.kotlin.incremental.LookupSymbol @@ -104,47 +106,48 @@ class IncrementalContextAA( return map } - private fun recordWithArgs(type: KtType, file: PsiJavaFile) { + private fun recordWithArgs(type: KaType, file: PsiJavaFile) { type.typeArguments().forEach { it.type?.let { recordWithArgs(it, file) } } when (type) { - is KtNonErrorClassType -> { + is KaClassType -> { val fqn = type.classId.asFqNameString() recordLookup(file, fqn) } - is KtFlexibleType -> { + is KaFlexibleType -> { recordWithArgs(type.lowerBound, file) recordWithArgs(type.upperBound, file) } - is KtIntersectionType -> { + is KaIntersectionType -> { type.conjuncts.forEach { recordWithArgs(it, file) } } - is KtCapturedType -> { + is KaCapturedType -> { type.projection.type?.let { recordWithArgs(it, file) } } - is KtDefinitelyNotNullType -> { + is KaDefinitelyNotNullType -> { recordWithArgs(type.original, file) } - is KtErrorType, is KtDynamicType, is KtTypeParameterType -> {} + is KaErrorType, is KaDynamicType, is KaTypeParameterType -> {} } } - fun recordLookup(type: KtType, context: KSNode?) { + fun recordLookup(type: KaType, context: KSNode?) { val file = (context?.containingFile as? KSFileJavaImpl)?.psi ?: return recordWithArgs(type, file) } - private fun recordLookupForDeclaration(symbol: KtSymbol, file: PsiJavaFile) { + @OptIn(KaExperimentalApi::class) + private fun recordLookupForDeclaration(symbol: KaSymbol, file: PsiJavaFile) { when (symbol) { - is KtJavaFieldSymbol -> recordWithArgs(symbol.returnType, file) - is KtPropertySymbol -> recordWithArgs(symbol.returnType, file) - is KtFunctionLikeSymbol -> { + is KaJavaFieldSymbol -> recordWithArgs(symbol.returnType, file) + is KaPropertySymbol -> recordWithArgs(symbol.returnType, file) + is KaFunctionSymbol -> { recordWithArgs(symbol.returnType, file) symbol.valueParameters.forEach { recordWithArgs(it.returnType, file) } symbol.typeParameters.forEach { @@ -168,15 +171,15 @@ class IncrementalContextAA( recordLookupForDeclaration(symbol, file) } - internal fun recordLookupForGetAll(supers: List, predicate: (KtSymbol) -> Boolean) { - val visited: MutableSet = mutableSetOf() + internal fun recordLookupForGetAll(supers: List, predicate: (KaSymbol) -> Boolean) { + val visited: MutableSet = mutableSetOf() analyze { supers.forEach { recordLookupWithSupertypes(it, visited) { type, file -> - if (type is KtNonErrorClassType) { - (type.classSymbol as? KtSymbolWithMembers)?.let { - val declared = it.getDeclaredMemberScope().getAllSymbols() + - it.getStaticDeclaredMemberScope().getAllSymbols() + if (type is KaClassType) { + (type.symbol as? KaDeclarationContainerSymbol)?.let { + val declared = it.declaredMemberScope.declarations + + it.staticDeclaredMemberScope.declarations declared.forEach { if (predicate(it)) recordLookupForDeclaration(it, file) @@ -188,28 +191,29 @@ class IncrementalContextAA( } } - private val KtSymbol.psiJavaFile: PsiJavaFile? + private val KaSymbol.psiJavaFile: PsiJavaFile? get() = psi?.containingFile as? PsiJavaFile - private val KtType.psiJavaFiles: List + private val KaType.psiJavaFiles: List get() { return when (this) { - is KtNonErrorClassType -> symbol.psiJavaFile?.let { listOf(it) } ?: emptyList() - is KtFlexibleType -> lowerBound.psiJavaFiles + upperBound.psiJavaFiles - is KtIntersectionType -> conjuncts.flatMap { it.psiJavaFiles } - is KtCapturedType -> projection.type?.psiJavaFiles ?: emptyList() - is KtDefinitelyNotNullType -> original.psiJavaFiles - is KtErrorType, is KtDynamicType, is KtTypeParameterType -> emptyList() + is KaClassType -> symbol.psiJavaFile?.let { listOf(it) } ?: emptyList() + is KaFlexibleType -> lowerBound.psiJavaFiles + upperBound.psiJavaFiles + is KaIntersectionType -> conjuncts.flatMap { it.psiJavaFiles } + is KaCapturedType -> projection.type?.psiJavaFiles ?: emptyList() + is KaDefinitelyNotNullType -> original.psiJavaFiles + is KaErrorType, is KaDynamicType, is KaTypeParameterType -> emptyList() + else -> TODO() } } - fun recordLookupWithSupertypes(ktType: KtType, visited: MutableSet, extra: (KtType, PsiJavaFile) -> Unit) { + fun recordLookupWithSupertypes(ktType: KaType, visited: MutableSet, extra: (KaType, PsiJavaFile) -> Unit) { analyze { - fun record(type: KtType) { + fun record(type: KaType) { if (type in visited) return visited.add(type) - for (superType in type.getDirectSuperTypes()) { + for (superType in type.directSupertypes(false)) { for (file in type.psiJavaFiles) { recordWithArgs(superType, file) } @@ -224,23 +228,23 @@ class IncrementalContextAA( } } -internal fun recordLookup(ktType: KtType, context: KSNode?) = +internal fun recordLookup(ktType: KaType, context: KSNode?) = ResolverAAImpl.instance.incrementalContext.recordLookup(ktType, context) -internal fun recordLookupWithSupertypes(ktType: KtType, extra: (KtType, PsiJavaFile) -> Unit = { _, _ -> }) = +internal fun recordLookupWithSupertypes(ktType: KaType, extra: (KaType, PsiJavaFile) -> Unit = { _, _ -> }) = ResolverAAImpl.instance.incrementalContext.recordLookupWithSupertypes(ktType, mutableSetOf(), extra) internal fun recordLookupForPropertyOrMethod(declaration: KSDeclaration) = ResolverAAImpl.instance.incrementalContext.recordLookupForPropertyOrMethod(declaration) -internal fun recordLookupForGetAllProperties(supers: List) = +internal fun recordLookupForGetAllProperties(supers: List) = ResolverAAImpl.instance.incrementalContext.recordLookupForGetAll(supers) { - it is KtPropertySymbol || it is KtJavaFieldSymbol + it is KaPropertySymbol || it is KaJavaFieldSymbol } -internal fun recordLookupForGetAllFunctions(supers: List) = +internal fun recordLookupForGetAllFunctions(supers: List) = ResolverAAImpl.instance.incrementalContext.recordLookupForGetAll(supers) { - it is KtFunctionLikeSymbol + it is KaFunctionSymbol } internal fun recordGetSealedSubclasses(classDeclaration: KSClassDeclaration) { diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt index 4b1f3a21d6..0c322f5504 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt @@ -54,38 +54,39 @@ import com.intellij.psi.PsiManager import com.intellij.psi.PsiTreeChangeAdapter import com.intellij.psi.PsiTreeChangeListener import com.intellij.psi.search.GlobalSearchScope -import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals +import org.jetbrains.kotlin.analysis.api.KaExperimentalApi +import org.jetbrains.kotlin.analysis.api.KaImplementationDetail import org.jetbrains.kotlin.analysis.api.platform.KotlinMessageBusProvider +import org.jetbrains.kotlin.analysis.api.platform.KotlinPlatformSettings import org.jetbrains.kotlin.analysis.api.platform.KotlinProjectMessageBusProvider import org.jetbrains.kotlin.analysis.api.platform.declarations.* -import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinLifetimeTokenProvider +import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeTokenFactory +import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinLifetimeTokenFactory import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinGlobalModificationService import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinModificationTrackerFactory import org.jetbrains.kotlin.analysis.api.platform.packages.KotlinPackagePartProviderFactory import org.jetbrains.kotlin.analysis.api.platform.packages.KotlinPackageProviderFactory import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinByModulesResolutionScopeProvider import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeProvider -import org.jetbrains.kotlin.analysis.api.resolve.extensions.KtResolveExtensionProvider -import org.jetbrains.kotlin.analysis.api.session.KtAnalysisSessionProvider +import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaSourceModule +import org.jetbrains.kotlin.analysis.api.resolve.extensions.KaResolveExtensionProvider +import org.jetbrains.kotlin.analysis.api.session.KaSessionProvider import org.jetbrains.kotlin.analysis.api.standalone.KotlinStaticPackagePartProviderFactory import org.jetbrains.kotlin.analysis.api.standalone.StandaloneAnalysisAPISession +import org.jetbrains.kotlin.analysis.api.standalone.base.KotlinStandalonePlatformSettings import org.jetbrains.kotlin.analysis.api.standalone.base.declarations.KotlinStandaloneAnnotationsResolverFactory import org.jetbrains.kotlin.analysis.api.standalone.base.declarations.KotlinStandaloneDeclarationProviderMerger import org.jetbrains.kotlin.analysis.api.standalone.base.modification.KotlinStandaloneGlobalModificationService import org.jetbrains.kotlin.analysis.api.standalone.base.modification.KotlinStandaloneModificationTrackerFactory -import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.FirStandaloneServiceRegistrar -import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.KtStaticProjectStructureProvider -import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.LLFirStandaloneLibrarySymbolProviderFactory -import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.StandaloneProjectFactory +import org.jetbrains.kotlin.analysis.api.standalone.base.projectStructure.FirStandaloneServiceRegistrar +import org.jetbrains.kotlin.analysis.api.standalone.base.projectStructure.StandaloneProjectFactory import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getFirResolveSession -import org.jetbrains.kotlin.analysis.low.level.api.fir.project.structure.LLFirLibrarySymbolProviderFactory import org.jetbrains.kotlin.analysis.low.level.api.fir.providers.LLSealedInheritorsProvider -import org.jetbrains.kotlin.analysis.project.structure.KtModule -import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule import org.jetbrains.kotlin.analysis.project.structure.builder.KtModuleBuilder import org.jetbrains.kotlin.analysis.project.structure.builder.KtModuleProviderBuilder import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtSdkModule -import org.jetbrains.kotlin.analysis.project.structure.impl.KtSourceModuleImpl +import org.jetbrains.kotlin.analysis.project.structure.impl.KaSourceModuleImpl import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoots import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreApplicationEnvironmentMode import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreProjectEnvironment @@ -142,12 +143,12 @@ class KotlinSymbolProcessing( setupIdeaStandaloneExecution() } - @OptIn(KtAnalysisApiInternals::class) + @OptIn(KaExperimentalApi::class) private fun createAASession( compilerConfiguration: CompilerConfiguration, applicationDisposable: Disposable = Disposer.newDisposable("StandaloneAnalysisAPISession.application"), projectDisposable: Disposable = Disposer.newDisposable("StandaloneAnalysisAPISession.project"), - ): Triple> { + ): Triple> { val kotlinCoreProjectEnvironment: KotlinCoreProjectEnvironment = StandaloneProjectFactory.createProjectEnvironment( projectDisposable, @@ -160,8 +161,8 @@ class KotlinSymbolProcessing( CoreApplicationEnvironment.registerExtensionPoint( project.extensionArea, - KtResolveExtensionProvider.EP_NAME.name, - KtResolveExtensionProvider::class.java + KaResolveExtensionProvider.EP_NAME.name, + KaResolveExtensionProvider::class.java ) // replaces buildKtModuleProviderByCompilerConfiguration(compilerConfiguration) @@ -196,7 +197,7 @@ class KotlinSymbolProcessing( buildKtSdkModule { this.platform = platform addBinaryRootsFromJdkHome(jdkHome.toPath(), isJre = false) - sdkName = "JDK for $moduleName" + libraryName = "JDK for $moduleName" } ) } @@ -226,8 +227,8 @@ class KotlinSymbolProcessing( }.build() // register services and build session - val ktModuleProviderImpl = projectStructureProvider as KtStaticProjectStructureProvider - val modules = ktModuleProviderImpl.allKtModules + val ktModuleProviderImpl = projectStructureProvider + val modules = ktModuleProviderImpl.allModules val allSourceFiles = ktModuleProviderImpl.allSourceFiles StandaloneProjectFactory.registerServicesForProjectEnvironment( kotlinCoreProjectEnvironment, @@ -245,10 +246,6 @@ class KotlinSymbolProcessing( createPackagePartProvider, ) - project.registerService( - LLFirLibrarySymbolProviderFactory::class.java, - LLFirStandaloneLibrarySymbolProviderFactory::class.java - ) CoreApplicationEnvironment.registerExtensionPoint( project.extensionArea, PsiTreeChangeListener.EP.name, PsiTreeChangeAdapter::class.java ) @@ -265,7 +262,6 @@ class KotlinSymbolProcessing( } // TODO: org.jetbrains.kotlin.analysis.providers.impl.KotlinStatic* - @OptIn(KtAnalysisApiInternals::class) private fun registerProjectServices( kotlinCoreProjectEnvironment: KotlinCoreProjectEnvironment, ktFiles: List, @@ -293,8 +289,8 @@ class KotlinSymbolProcessing( KotlinStandaloneGlobalModificationService::class.java ) registerService( - KotlinLifetimeTokenProvider::class.java, - KtAlwaysAccessibleLifeTimeTokenProvider::class.java + KotlinLifetimeTokenFactory::class.java, + KotlinAlwaysAccessibleLifetimeTokenFactory::class.java ) // Despite being a static implementation, this is only used by IDE tests @@ -329,18 +325,23 @@ class KotlinSymbolProcessing( KotlinPackagePartProviderFactory::class.java, KotlinStaticPackagePartProviderFactory(packagePartProvider) ) + + registerService( + KotlinPlatformSettings::class.java, + KotlinStandalonePlatformSettings() + ) } } private fun prepareAllKSFiles( kotlinCoreProjectEnvironment: KotlinCoreProjectEnvironment, - modules: List, + modules: List, javaFileManager: IncrementalJavaFileManager?, ): List { val project = kotlinCoreProjectEnvironment.project val ktFiles = mutableSetOf() val javaFiles = mutableSetOf() - modules.filterIsInstance().forEach { + modules.filterIsInstance().forEach { it.sourceRoots.forEach { when (it) { is KtFile -> ktFiles.add(it) @@ -410,7 +411,7 @@ class KotlinSymbolProcessing( } // TODO: performance - @OptIn(KtAnalysisApiInternals::class) + @OptIn(KaImplementationDetail::class) fun execute(): ExitCode { // TODO: CompilerConfiguration is deprecated. val compilerConfiguration: CompilerConfiguration = CompilerConfiguration().apply { @@ -453,7 +454,7 @@ class KotlinSymbolProcessing( val psiManager = PsiManager.getInstance(project) val providers: List = symbolProcessorProviders // KspModuleBuilder ensures this is always a KtSourceModule - ResolverAAImpl.ktModule = modules.single() as KtSourceModule + ResolverAAImpl.ktModule = modules.single() as KaSourceModule // Initializing environments val javaFileManager = if (kspConfig is KSPJvmConfig) { @@ -543,7 +544,7 @@ class KotlinSymbolProcessing( // Drop caches KotlinGlobalModificationService.getInstance(project).publishGlobalSourceModuleStateModification() - KtAnalysisSessionProvider.getInstance(project).clearCaches() + KaSessionProvider.getInstance(project).clearCaches() psiManager.dropResolveCaches() psiManager.dropPsiCaches() @@ -632,9 +633,11 @@ fun String?.toKotlinVersion(): KotlinVersion { // Workaround for ShadowJar's minimize, whose configuration isn't very flexible. /* ktlint-disable */ +@OptIn(KaImplementationDetail::class) internal val DEAR_SHADOW_JAR_PLEASE_DO_NOT_REMOVE_THESE = listOf( kotlinx.coroutines.debug.internal.DebugProbesImpl::class.java, org.jetbrains.kotlin.analysis.api.impl.base.java.source.JavaElementSourceWithSmartPointerFactory::class.java, + org.jetbrains.kotlin.analysis.api.impl.base.projectStructure.KaBaseModuleProvider::class.java, org.jetbrains.kotlin.analysis.api.impl.base.references.HLApiReferenceProviderService::class.java, org.jetbrains.kotlin.analysis.api.fir.KaFirSessionProvider::class.java, org.jetbrains.kotlin.analysis.api.fir.references.ReadWriteAccessCheckerFirImpl::class.java, diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KtAlwaysAccessibleLifeTimeTokenProvider.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KtAlwaysAccessibleLifeTimeTokenProvider.kt deleted file mode 100644 index 163f87860a..0000000000 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KtAlwaysAccessibleLifeTimeTokenProvider.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.google.devtools.ksp.impl - -import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeTokenFactory -import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeTokenFactory -import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinLifetimeTokenProvider - -public class KtAlwaysAccessibleLifeTimeTokenProvider : KotlinLifetimeTokenProvider() { - override fun getLifetimeTokenFactory(): KtLifetimeTokenFactory { - return KotlinAlwaysAccessibleLifetimeTokenFactory - } -} diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/ResolverAAImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/ResolverAAImpl.kt index 96a9d76f9f..66f24b5c9e 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/ResolverAAImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/ResolverAAImpl.kt @@ -197,7 +197,7 @@ class ResolverAAImpl( (declaration as? KSClassDeclarationImpl)?.let { analyze { if ( - it.ktClassOrObjectSymbol.getStaticMemberScope() + it.ktClassOrObjectSymbol.staticMemberScope .getAllSymbols().contains(declaration.ktDeclarationSymbol) ) modifiers.add(Modifier.JAVA_STATIC) @@ -307,10 +307,10 @@ class ResolverAAImpl( return (findClass(KSNameImpl.getCached(name.getQualifier())) as? KtNamedClassOrObjectSymbol)?.let { analyze { ( - it.getStaticMemberScope().getClassifierSymbols { it.asString() == simpleName }.singleOrNull() - ?: it.getMemberScope().getClassifierSymbols { it.asString() == simpleName }.singleOrNull() - ) as? KtNamedClassOrObjectSymbol - ?: it.getStaticMemberScope().getCallableSymbols { it.asString() == simpleName }.singleOrNull() + it.staticMemberScope.classifiers { it.asString() == simpleName }.singleOrNull() + ?: it.memberScope.classifiers { it.asString() == simpleName }.singleOrNull() + ) as? KaNamedClassSymbol + ?: it.staticMemberScope.callables { it.asString() == simpleName }.singleOrNull() as? KtEnumEntrySymbol } } @@ -332,17 +332,17 @@ class ResolverAAImpl( var packages = listOf(analysisSession.ROOT_PACKAGE_SYMBOL) for (curName in packageNames) { packages = packages - .flatMap { it.getPackageScope().getPackageSymbols { it.asString() == curName } } + .flatMap { it.packageScope.getPackageSymbols { it.asString() == curName } } .distinct() } packages.flatMap { - it.getPackageScope().getAllSymbols().distinct().mapNotNull { symbol -> + it.packageScope.declarations.distinct().mapNotNull { symbol -> when (symbol) { - is KtNamedClassOrObjectSymbol -> KSClassDeclarationImpl.getCached(symbol) - is KtFunctionLikeSymbol -> KSFunctionDeclarationImpl.getCached(symbol) - is KtPropertySymbol -> KSPropertyDeclarationImpl.getCached(symbol) - is KtTypeAliasSymbol -> KSTypeAliasImpl.getCached(symbol) - is KtJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(symbol) + is KaNamedClassSymbol -> KSClassDeclarationImpl.getCached(symbol) + is KaFunctionSymbol -> KSFunctionDeclarationImpl.getCached(symbol) + is KaPropertySymbol -> KSPropertyDeclarationImpl.getCached(symbol) + is KaTypeAliasSymbol -> KSTypeAliasImpl.getCached(symbol) + is KaJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(symbol) else -> null } } @@ -454,13 +454,14 @@ class ResolverAAImpl( } } + @OptIn(KaExperimentalApi::class) override fun getJvmCheckedException(function: KSFunctionDeclaration): Sequence { return when (function.origin) { Origin.JAVA -> { val psi = (function as KSFunctionDeclarationImpl).ktFunctionSymbol.psi as PsiMethod psi.throwsList.referencedTypes.asSequence().mapNotNull { analyze { - it.asKtType(psi)?.let { KSTypeImpl.getCached(it) } + it.asKaType(psi)?.let { KSTypeImpl.getCached(it) } } } } @@ -510,7 +511,7 @@ class ResolverAAImpl( "set" } val mangledName = if (accessor.modifiers.contains(Modifier.INTERNAL)) { - "\$${ktModule.moduleName}" + "\$${ktModule.name}" } else "" return "${prefix}${accessor.receiver.simpleName.asString().capitalize()}$mangledName" } @@ -525,7 +526,7 @@ class ResolverAAImpl( return it.name } val mangledName = if (declaration.modifiers.contains(Modifier.INTERNAL)) { - "\$${ktModule.moduleName}" + "\$${ktModule.name}" } else "" return declaration.simpleName.asString() + mangledName } @@ -542,7 +543,7 @@ class ResolverAAImpl( private fun getOwnerJvmClassNameHelper(declaration: KSDeclaration): String? { return declaration.closestClassDeclaration()?.let { // Find classId for JvmClassName for member callables. - (it as? KSClassDeclarationImpl)?.ktClassOrObjectSymbol?.classIdIfNonLocal + (it as? KSClassDeclarationImpl)?.ktClassOrObjectSymbol?.classId ?.asString()?.replace(".", "$")?.replace("/", ".") } ?: declaration.containingFile?.let { // Find containing file facade class name from file symbol @@ -575,8 +576,8 @@ class ResolverAAImpl( } else { val topLevelResult = ( analyze { - getTopLevelCallableSymbols(FqName(qualifier), Name.identifier(propertyName)).singleOrNull { - it is KtPropertySymbol + findTopLevelCallables(FqName(qualifier), Name.identifier(propertyName)).singleOrNull { + it is KaPropertySymbol }?.toKSDeclaration() as? KSPropertyDeclaration } ) @@ -671,9 +672,10 @@ class ResolverAAImpl( }.map { it.packageName.asString() } } + @OptIn(KaExperimentalApi::class) @KspExperimental override fun getModuleName(): KSName { - return KSNameImpl.getCached((ktModule.stableModuleName ?: ktModule.moduleName).removeSurrounding("<", ">")) + return KSNameImpl.getCached((ktModule.stableModuleName ?: ktModule.name).removeSurrounding("<", ">")) } @KspExperimental @@ -716,8 +718,8 @@ class ResolverAAImpl( recordLookupForPropertyOrMethod(overrider) recordLookupForPropertyOrMethod(overridee) return analyze { - overriderSymbol.getAllOverriddenSymbols().contains(overrideeSymbol) || - overriderSymbol.getIntersectionOverriddenSymbols().contains(overrideeSymbol) + overriderSymbol.allOverriddenSymbols.contains(overrideeSymbol) || + overriderSymbol.intersectionOverriddenSymbols.contains(overrideeSymbol) } } @@ -759,10 +761,11 @@ class ResolverAAImpl( } } + @OptIn(KaExperimentalApi::class) internal fun mapToJvmSignatureInternal(ksAnnotated: KSAnnotated): String? { fun KtType.toSignature(): String { return analyze { - this@toSignature.mapTypeToJvmType().descriptor.let { + this@toSignature.mapToJvmType().descriptor.let { when (it) { "Ljava.lang.Void;" -> "Ljava/lang/Void;" "Lkotlin/Unit;" -> "V" @@ -832,6 +835,7 @@ class ResolverAAImpl( } } + @OptIn(KaExperimentalApi::class) internal fun computeAsMemberOf(property: KSPropertyDeclaration, containing: KSType): KSType { val declaredIn = property.closestClassDeclaration() ?: throw IllegalArgumentException( @@ -845,7 +849,7 @@ class ResolverAAImpl( recordLookupForPropertyOrMethod(property) val isSubTypeOf = analyze { (declaredIn.asStarProjectedType() as? KSTypeImpl)?.type?.let { - containing.type.isSubTypeOf(it) + containing.type.isSubtypeOf(it) } ?: false } if (!isSubTypeOf) { @@ -879,6 +883,7 @@ class ResolverAAImpl( } } + @OptIn(KaExperimentalApi::class) internal fun computeAsMemberOf(function: KSFunctionDeclaration, containing: KSType): KSFunction { val propertyDeclaredIn = function.closestClassDeclaration() ?: throw IllegalArgumentException( diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/java/KSAnnotationJavaImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/java/KSAnnotationJavaImpl.kt index 867e7e9d62..27f7ebb1de 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/java/KSAnnotationJavaImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/java/KSAnnotationJavaImpl.kt @@ -38,12 +38,11 @@ import com.intellij.psi.PsiPrimitiveType import com.intellij.psi.PsiReference import com.intellij.psi.PsiType import com.intellij.psi.impl.compiled.ClsClassImpl -import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals -import org.jetbrains.kotlin.analysis.api.annotations.KtNamedAnnotationValue -import org.jetbrains.kotlin.analysis.api.annotations.KtUnsupportedAnnotationValue -import org.jetbrains.kotlin.analysis.api.components.buildClassType +import org.jetbrains.kotlin.analysis.api.KaImplementationDetail +import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaBaseNamedAnnotationValue +import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaUnsupportedAnnotationValueImpl import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeToken -import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaClassSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin import org.jetbrains.kotlin.analysis.api.types.KtType import org.jetbrains.kotlin.name.ClassId @@ -70,7 +69,7 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation, o override val arguments: List by lazy { val annotationConstructor = analyze { - (type.classifierSymbol() as? KtClassOrObjectSymbol)?.getMemberScope()?.constructors?.singleOrNull() + (type.classifierSymbol() as? KaClassSymbol)?.memberScope?.constructors?.singleOrNull() } val presentArgs = psi.parameterList.attributes.mapIndexed { index, it -> val name = it.name ?: annotationConstructor?.valueParameters?.getOrNull(index)?.name?.asString() @@ -92,10 +91,10 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation, o presentArgs + defaultArguments.filter { it.name?.asString() !in presentValueArgumentNames } } - @OptIn(KtAnalysisApiInternals::class) + @OptIn(KaImplementationDetail::class) override val defaultArguments: List by lazy { analyze { - (type.classifierSymbol() as? KtClassOrObjectSymbol)?.getMemberScope()?.constructors?.singleOrNull() + (type.classifierSymbol() as? KaClassSymbol)?.memberScope?.constructors?.singleOrNull() ?.let { symbol -> // ClsClassImpl means psi is decompiled psi. if ( @@ -123,17 +122,16 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation, o symbol.valueParameters.map { valueParameterSymbol -> valueParameterSymbol.getDefaultValue().let { constantValue -> KSValueArgumentImpl.getCached( - KtNamedAnnotationValue( + KaBaseNamedAnnotationValue( valueParameterSymbol.name, // null will be returned as the `constantValue` for non array annotation values. // fallback to unsupported annotation value to indicate such use cases. // when seeing unsupported annotation value we return `null` for the value. // which might still be incorrect but there might not be a perfect way. constantValue - ?: KtUnsupportedAnnotationValue( + ?: KaUnsupportedAnnotationValueImpl( KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project) - ), - KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project) + ) ), Origin.SYNTHETIC ) diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/AbstractKSDeclarationImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/AbstractKSDeclarationImpl.kt index 44113ef70d..0a686dd04a 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/AbstractKSDeclarationImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/AbstractKSDeclarationImpl.kt @@ -32,6 +32,7 @@ import com.google.devtools.ksp.symbol.Origin import com.intellij.psi.PsiClass import com.intellij.psi.PsiJvmModifiersOwner import com.intellij.psi.PsiModifierListOwner +import org.jetbrains.kotlin.analysis.api.KaExperimentalApi import org.jetbrains.kotlin.analysis.api.symbols.* import org.jetbrains.kotlin.analysis.api.symbols.markers.KtNamedSymbol import org.jetbrains.kotlin.analysis.utils.printer.parentOfType @@ -58,10 +59,10 @@ abstract class AbstractKSDeclarationImpl(val ktDeclarationSymbol: KtDeclarationS override val modifiers: Set by lazy { if (origin == Origin.JAVA_LIB || origin == Origin.KOTLIN_LIB || origin == Origin.SYNTHETIC) { when (ktDeclarationSymbol) { - is KtPropertySymbol -> ktDeclarationSymbol.toModifiers() - is KtClassOrObjectSymbol -> ktDeclarationSymbol.toModifiers() - is KtFunctionLikeSymbol -> ktDeclarationSymbol.toModifiers() - is KtJavaFieldSymbol -> ktDeclarationSymbol.toModifiers() + is KaPropertySymbol -> ktDeclarationSymbol.toModifiers() + is KaClassSymbol -> ktDeclarationSymbol.toModifiers() + is KaFunctionSymbol -> ktDeclarationSymbol.toModifiers() + is KaJavaFieldSymbol -> ktDeclarationSymbol.toModifiers() else -> throw IllegalStateException("Unexpected symbol type ${ktDeclarationSymbol.javaClass}") } } else { @@ -84,8 +85,8 @@ abstract class AbstractKSDeclarationImpl(val ktDeclarationSymbol: KtDeclarationS } else { // top level declaration when (ktDeclarationSymbol) { - is KtClassLikeSymbol -> ktDeclarationSymbol.classIdIfNonLocal?.packageFqName?.asString() - is KtCallableSymbol -> ktDeclarationSymbol.callableIdIfNonLocal?.packageName?.asString() + is KaClassLikeSymbol -> ktDeclarationSymbol.classId?.packageFqName?.asString() + is KaCallableSymbol -> ktDeclarationSymbol.callableId?.packageName?.asString() else -> null }?.let { KSNameImpl.getCached(it) } // null -> non top level declaration, find in parent @@ -94,6 +95,7 @@ abstract class AbstractKSDeclarationImpl(val ktDeclarationSymbol: KtDeclarationS } } + @OptIn(KaExperimentalApi::class) override val typeParameters: List by lazy { ktDeclarationSymbol.typeParameters.map { KSTypeParameterImpl.getCached(it) } } @@ -104,9 +106,9 @@ abstract class AbstractKSDeclarationImpl(val ktDeclarationSymbol: KtDeclarationS override val parent: KSNode? by lazy { analyze { - ktDeclarationSymbol.getContainingSymbol()?.let { + ktDeclarationSymbol.containingSymbol?.let { ktDeclarationSymbol.getContainingKSSymbol() - } ?: (ktDeclarationSymbol.psi?.parentOfType())?.getNamedClassSymbol()?.let { + } ?: (ktDeclarationSymbol.psi?.parentOfType())?.namedClassSymbol?.let { KSClassDeclarationImpl.getCached(it) } ?: ktDeclarationSymbol.toContainingFile() } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSAnnotationImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSAnnotationImpl.kt index e6859900b1..acf8649bef 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSAnnotationImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSAnnotationImpl.kt @@ -28,10 +28,10 @@ import com.intellij.psi.PsiAnnotationMethod import com.intellij.psi.PsiArrayInitializerMemberValue import com.intellij.psi.PsiClass import com.intellij.psi.impl.compiled.ClsClassImpl -import org.jetbrains.kotlin.analysis.api.KaAnalysisApiInternals -import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplicationWithArgumentsInfo -import org.jetbrains.kotlin.analysis.api.annotations.KtNamedAnnotationValue -import org.jetbrains.kotlin.analysis.api.annotations.KtUnsupportedAnnotationValue +import org.jetbrains.kotlin.analysis.api.KaImplementationDetail +import org.jetbrains.kotlin.analysis.api.annotations.KaAnnotation +import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaBaseNamedAnnotationValue +import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaUnsupportedAnnotationValueImpl import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeToken import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.* @@ -41,13 +41,13 @@ import org.jetbrains.kotlin.psi.KtAnnotationEntry class KSAnnotationImpl private constructor( private val ktAnnotationEntry: KtAnnotationEntry, override val parent: KSNode?, - private val resolveToAnnotationApplication: () -> KtAnnotationApplicationWithArgumentsInfo + private val resolveToAnnotationApplication: () -> KaAnnotation ) : KSAnnotation { companion object : KSObjectCache, KSAnnotationImpl>() { fun getCached( ktAnnotationEntry: KtAnnotationEntry, parent: KSNode? = null, - resolveToAnnotationApplication: () -> KtAnnotationApplicationWithArgumentsInfo + resolveToAnnotationApplication: () -> KaAnnotation ) = cache.getOrPut(IdKeyPair(ktAnnotationEntry, parent)) { KSAnnotationImpl(ktAnnotationEntry, parent, resolveToAnnotationApplication) @@ -78,7 +78,7 @@ class KSAnnotationImpl private constructor( presentArgs + absentArgs } - @OptIn(KaAnalysisApiInternals::class) + @OptIn(KaImplementationDetail::class) override val defaultArguments: List by lazy { analyze { annotationApplication.classId?.toKtClassSymbol()?.let { symbol -> @@ -101,17 +101,16 @@ class KSAnnotationImpl private constructor( } } } else { - symbol.getMemberScope().constructors.singleOrNull()?.let { + symbol.memberScope.constructors.singleOrNull()?.let { it.valueParameters.map { valueParameterSymbol -> valueParameterSymbol.getDefaultValue().let { constantValue -> KSValueArgumentImpl.getCached( - KtNamedAnnotationValue( + KaBaseNamedAnnotationValue( valueParameterSymbol.name, constantValue - ?: KtUnsupportedAnnotationValue( + ?: KaUnsupportedAnnotationValueImpl( KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project) - ), - KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project) + ) ), Origin.SYNTHETIC ) diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationEnumEntryImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationEnumEntryImpl.kt index 0123581dfe..5ed1a266af 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationEnumEntryImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationEnumEntryImpl.kt @@ -20,15 +20,15 @@ import com.google.devtools.ksp.symbol.KSTypeParameter import com.google.devtools.ksp.symbol.KSTypeReference import com.google.devtools.ksp.symbol.KSVisitor import com.google.devtools.ksp.symbol.Location -import org.jetbrains.kotlin.analysis.api.symbols.KtEnumEntrySymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtNamedClassOrObjectSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaEnumEntrySymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaNamedClassSymbol -class KSClassDeclarationEnumEntryImpl private constructor(private val ktEnumEntrySymbol: KtEnumEntrySymbol) : +class KSClassDeclarationEnumEntryImpl private constructor(private val ktEnumEntrySymbol: KaEnumEntrySymbol) : KSClassDeclaration, AbstractKSDeclarationImpl(ktEnumEntrySymbol), KSExpectActual by KSExpectActualImpl(ktEnumEntrySymbol) { - companion object : KSObjectCache() { - fun getCached(ktEnumEntrySymbol: KtEnumEntrySymbol) = + companion object : KSObjectCache() { + fun getCached(ktEnumEntrySymbol: KaEnumEntrySymbol) = cache.getOrPut(ktEnumEntrySymbol) { KSClassDeclarationEnumEntryImpl(ktEnumEntrySymbol) } } @@ -87,8 +87,8 @@ class KSClassDeclarationEnumEntryImpl private constructor(private val ktEnumEntr override val parentDeclaration: KSDeclaration? by lazy { analyze { ( - ktEnumEntrySymbol.getContainingSymbol() - as? KtNamedClassOrObjectSymbol + ktEnumEntrySymbol.containingSymbol + as? KaNamedClassSymbol )?.let { KSClassDeclarationImpl.getCached(it) } } } @@ -103,7 +103,7 @@ class KSClassDeclarationEnumEntryImpl private constructor(private val ktEnumEntr override val parent: KSNode by lazy { analyze { - (ktEnumEntrySymbol.getContainingSymbol() as KtNamedClassOrObjectSymbol) + (ktEnumEntrySymbol.containingSymbol as KaNamedClassSymbol) .let { KSClassDeclarationImpl.getCached(it) } } } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationImpl.kt index bacba093af..d38ea21022 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationImpl.kt @@ -28,33 +28,32 @@ import com.google.devtools.ksp.impl.recordLookupForGetAllFunctions import com.google.devtools.ksp.impl.recordLookupForGetAllProperties import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl 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.components.buildTypeParameterType +import org.jetbrains.kotlin.analysis.api.KaExperimentalApi +import org.jetbrains.kotlin.analysis.api.KaImplementationDetail +import org.jetbrains.kotlin.analysis.api.impl.base.types.KaBaseStarTypeProjection import org.jetbrains.kotlin.analysis.api.symbols.* -import org.jetbrains.kotlin.descriptors.java.JavaVisibilities import org.jetbrains.kotlin.psi.KtClassOrObject -class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSymbol: KtClassOrObjectSymbol) : +class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSymbol: KaClassSymbol) : KSClassDeclaration, AbstractKSDeclarationImpl(ktClassOrObjectSymbol), KSExpectActual by KSExpectActualImpl(ktClassOrObjectSymbol) { - companion object : KSObjectCache() { - fun getCached(ktClassOrObjectSymbol: KtClassOrObjectSymbol) = + companion object : KSObjectCache() { + fun getCached(ktClassOrObjectSymbol: KaClassSymbol) = cache.getOrPut(ktClassOrObjectSymbol) { KSClassDeclarationImpl(ktClassOrObjectSymbol) } } override val qualifiedName: KSName? by lazy { - ktClassOrObjectSymbol.classIdIfNonLocal?.asFqNameString()?.let { KSNameImpl.getCached(it) } + ktClassOrObjectSymbol.classId?.asFqNameString()?.let { KSNameImpl.getCached(it) } } override val classKind: ClassKind by lazy { when (ktClassOrObjectSymbol.classKind) { - KtClassKind.CLASS -> ClassKind.CLASS - KtClassKind.ENUM_CLASS -> ClassKind.ENUM_CLASS - KtClassKind.ANNOTATION_CLASS -> ClassKind.ANNOTATION_CLASS - KtClassKind.INTERFACE -> ClassKind.INTERFACE - KtClassKind.COMPANION_OBJECT, KtClassKind.ANONYMOUS_OBJECT, KtClassKind.OBJECT -> ClassKind.OBJECT + KaClassKind.CLASS -> ClassKind.CLASS + KaClassKind.ENUM_CLASS -> ClassKind.ENUM_CLASS + KaClassKind.ANNOTATION_CLASS -> ClassKind.ANNOTATION_CLASS + KaClassKind.INTERFACE -> ClassKind.INTERFACE + KaClassKind.COMPANION_OBJECT, KaClassKind.ANONYMOUS_OBJECT, KaClassKind.OBJECT -> ClassKind.OBJECT } } @@ -63,7 +62,7 @@ class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSym null } else { analyze { - ktClassOrObjectSymbol.getMemberScope().constructors.singleOrNull { it.isPrimary }?.let { + ktClassOrObjectSymbol.memberScope.constructors.singleOrNull { it.isPrimary }?.let { KSFunctionDeclarationImpl.getCached(it) } } @@ -99,15 +98,15 @@ class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSym } override val isCompanionObject: Boolean by lazy { - ktClassOrObjectSymbol.classKind == KtClassKind.COMPANION_OBJECT + ktClassOrObjectSymbol.classKind == KaClassKind.COMPANION_OBJECT } override fun getSealedSubclasses(): Sequence { if (!modifiers.contains(Modifier.SEALED)) return emptySequence() recordGetSealedSubclasses(this) - return (ktClassOrObjectSymbol as? KtNamedClassOrObjectSymbol)?.let { + return (ktClassOrObjectSymbol as? KaNamedClassSymbol)?.let { analyze { - it.getSealedClassInheritors().map { getCached(it) }.asSequence() + it.sealedClassInheritors.map { getCached(it) }.asSequence() } } ?: emptySequence() } @@ -147,15 +146,16 @@ class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSym } } + @OptIn(KaExperimentalApi::class, KaImplementationDetail::class) override fun asStarProjectedType(): KSType { return analyze { KSTypeImpl.getCached( - analysisSession.buildClassType(ktClassOrObjectSymbol) { + useSiteSession.buildClassType(ktClassOrObjectSymbol) { var current: KSNode? = this@KSClassDeclarationImpl while (current is KSClassDeclarationImpl) { current.ktClassOrObjectSymbol.typeParameters.forEach { argument( - KtStarTypeProjection( + KaBaseStarTypeProjection( (current as KSClassDeclarationImpl).ktClassOrObjectSymbol.token ) ) @@ -180,7 +180,7 @@ class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSym if (origin == Origin.JAVA && classKind != ClassKind.ANNOTATION_CLASS) { buildList { decls.forEach { decl -> - if (decl is KSPropertyDeclarationImpl && decl.ktPropertySymbol is KtSyntheticJavaPropertySymbol) { + if (decl is KSPropertyDeclarationImpl && decl.ktPropertySymbol is KaSyntheticJavaPropertySymbol) { decl.getter?.let { add( KSFunctionDeclarationImpl.getCached( @@ -208,11 +208,11 @@ class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSym } } -internal fun KtClassOrObjectSymbol.toModifiers(): Set { +internal fun KaClassSymbol.toModifiers(): Set { val result = mutableSetOf() - if (this is KtNamedClassOrObjectSymbol) { + if (this is KaNamedClassSymbol) { result.add(modality.toModifier()) - if (visibility != JavaVisibilities.PackageVisibility) { + if (visibility != KaSymbolVisibility.PACKAGE_PRIVATE) { result.add(visibility.toModifier()) } if (isFun) { @@ -231,7 +231,7 @@ internal fun KtClassOrObjectSymbol.toModifiers(): Set { result.add(Modifier.INNER) } } - if (classKind == KtClassKind.ENUM_CLASS) { + if (classKind == KaClassKind.ENUM_CLASS) { result.add(Modifier.ENUM) } return result diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSExpectActualImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSExpectActualImpl.kt index 4b88fa799d..ea0322abc3 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSExpectActualImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSExpectActualImpl.kt @@ -18,21 +18,22 @@ package com.google.devtools.ksp.impl.symbol.kotlin import com.google.devtools.ksp.symbol.KSDeclaration import com.google.devtools.ksp.symbol.KSExpectActual -import org.jetbrains.kotlin.analysis.api.symbols.KtDeclarationSymbol -import org.jetbrains.kotlin.analysis.api.symbols.markers.KtPossibleMultiplatformSymbol +import org.jetbrains.kotlin.analysis.api.KaExperimentalApi +import org.jetbrains.kotlin.analysis.api.symbols.KaDeclarationSymbol -class KSExpectActualImpl(private val declarationSymbol: KtDeclarationSymbol) : KSExpectActual { +class KSExpectActualImpl(private val declarationSymbol: KaDeclarationSymbol) : KSExpectActual { override val isActual: Boolean - get() = (declarationSymbol as? KtPossibleMultiplatformSymbol)?.isActual == true + get() = declarationSymbol.isActual override val isExpect: Boolean - get() = (declarationSymbol as? KtPossibleMultiplatformSymbol)?.isExpect == true + get() = declarationSymbol.isExpect // TODO: not possible in new KMP model, returning empty sequence for now. override fun findActuals(): Sequence { return emptySequence() } + @OptIn(KaExperimentalApi::class) override fun findExpects(): Sequence { return if (!isActual) { emptySequence() diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFileImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFileImpl.kt index bb861b3527..84464f16b6 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFileImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFileImpl.kt @@ -28,16 +28,16 @@ import com.google.devtools.ksp.symbol.KSVisitor import com.google.devtools.ksp.symbol.Location import com.google.devtools.ksp.symbol.Origin import com.intellij.psi.PsiFile -import org.jetbrains.kotlin.analysis.api.symbols.KtFileSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtFunctionSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtNamedClassOrObjectSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtPropertySymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtTypeAliasSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaFileSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaNamedClassSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaNamedFunctionSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaPropertySymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaTypeAliasSymbol import org.jetbrains.kotlin.psi.KtFile -class KSFileImpl private constructor(internal val ktFileSymbol: KtFileSymbol) : KSFile, Deferrable { - companion object : KSObjectCache() { - fun getCached(ktFileSymbol: KtFileSymbol) = cache.getOrPut(ktFileSymbol) { KSFileImpl(ktFileSymbol) } +class KSFileImpl private constructor(internal val ktFileSymbol: KaFileSymbol) : KSFile, Deferrable { + companion object : KSObjectCache() { + fun getCached(ktFileSymbol: KaFileSymbol) = cache.getOrPut(ktFileSymbol) { KSFileImpl(ktFileSymbol) } } private val psi: PsiFile @@ -60,12 +60,12 @@ class KSFileImpl private constructor(internal val ktFileSymbol: KtFileSymbol) : override val declarations: Sequence by lazy { analyze { - ktFileSymbol.getFileScope().getAllSymbols().map { + ktFileSymbol.fileScope.declarations.map { when (it) { - is KtNamedClassOrObjectSymbol -> KSClassDeclarationImpl.getCached(it) - is KtFunctionSymbol -> KSFunctionDeclarationImpl.getCached(it) - is KtPropertySymbol -> KSPropertyDeclarationImpl.getCached(it) - is KtTypeAliasSymbol -> KSTypeAliasImpl.getCached(it) + is KaNamedClassSymbol -> KSClassDeclarationImpl.getCached(it) + is KaNamedFunctionSymbol -> KSFunctionDeclarationImpl.getCached(it) + is KaPropertySymbol -> KSPropertyDeclarationImpl.getCached(it) + is KaTypeAliasSymbol -> KSTypeAliasImpl.getCached(it) else -> throw IllegalStateException("Unhandled ${it.javaClass}") } } @@ -96,7 +96,7 @@ class KSFileImpl private constructor(internal val ktFileSymbol: KtFileSymbol) : val psi = this.psi return Restorable { when (psi) { - is KtFile -> analyze { getCached(psi.getFileSymbol()) } + is KtFile -> analyze { getCached(psi.symbol) } else -> throw IllegalStateException("Unhandled psi file type ${psi.javaClass}") } } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFileJavaImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFileJavaImpl.kt index fca42d769c..e9c02ba433 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFileJavaImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFileJavaImpl.kt @@ -43,7 +43,7 @@ class KSFileJavaImpl private constructor(val psi: PsiJavaFile) : KSFile, Deferra override val declarations: Sequence by lazy { psi.classes.asSequence().mapNotNull { psi -> analyze { - psi.getNamedClassSymbol()?.let { KSClassDeclarationImpl.getCached(it) } + psi.namedClassSymbol?.let { KSClassDeclarationImpl.getCached(it) } } } } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFunctionDeclarationImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFunctionDeclarationImpl.kt index 0792963af3..2cf888c763 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFunctionDeclarationImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFunctionDeclarationImpl.kt @@ -29,20 +29,21 @@ import com.intellij.psi.PsiClass import com.intellij.psi.PsiMethod import org.jetbrains.kotlin.analysis.api.symbols.* import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolKind -import org.jetbrains.kotlin.descriptors.Modality -import org.jetbrains.kotlin.descriptors.java.JavaVisibilities import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.KtFunction -class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbol: KtFunctionLikeSymbol) : +class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbol: KaFunctionSymbol) : KSFunctionDeclaration, AbstractKSDeclarationImpl(ktFunctionSymbol), KSExpectActual by KSExpectActualImpl(ktFunctionSymbol) { - companion object : KSObjectCache() { - fun getCached(ktFunctionSymbol: KtFunctionLikeSymbol) = + companion object : KSObjectCache() { + fun getCached(ktFunctionSymbol: KaFunctionSymbol) = cache.getOrPut(ktFunctionSymbol) { KSFunctionDeclarationImpl(ktFunctionSymbol) } } + // TODO: upstream deprecated `KtSymbolKind` in favor of `KaSymbolLocation` but they are + // semantically different as the new location does not suggest if the function is a lambda or not. + // This is a breaking change in upstream deprecation. override val functionKind: FunctionKind by lazy { when (ktFunctionSymbol.symbolKind) { KtSymbolKind.CLASS_MEMBER -> FunctionKind.MEMBER @@ -53,7 +54,7 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo } override val isAbstract: Boolean by lazy { - (ktFunctionSymbol as? KtFunctionSymbol)?.modality == Modality.ABSTRACT + (ktFunctionSymbol as? KaNamedFunctionSymbol)?.modality == KaSymbolModality.ABSTRACT } override val modifiers: Set by lazy { @@ -102,7 +103,7 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo (ktFunctionSymbol.psiIfSource() as? KtFunction)?.typeReference?.let { KSTypeReferenceImpl.getCached(it, this) } ?: analyze { // Constructors - if (ktFunctionSymbol is KtConstructorSymbol) { + if (ktFunctionSymbol is KaConstructorSymbol) { ((parentDeclaration as KSClassDeclaration).asStarProjectedType() as KSTypeImpl).type } else { ktFunctionSymbol.returnType @@ -120,11 +121,11 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo } recordLookupForPropertyOrMethod(this) return analyze { - if (ktFunctionSymbol is KtPropertyAccessorSymbol) { + if (ktFunctionSymbol is KaPropertyAccessorSymbol) { (parentDeclaration as? KSPropertyDeclarationImpl)?.ktPropertySymbol } else { ktFunctionSymbol - }?.getDirectlyOverriddenSymbols()?.firstOrNull()?.unwrapFakeOverrides?.toKSDeclaration() + }?.directlyOverriddenSymbols?.firstOrNull()?.fakeOverrideOriginal?.toKSDeclaration() }?.also { recordLookupForPropertyOrMethod(it) } } @@ -134,15 +135,15 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo override val simpleName: KSName by lazy { when (ktFunctionSymbol) { - is KtFunctionSymbol -> KSNameImpl.getCached(ktFunctionSymbol.name.asString()) - is KtPropertyAccessorSymbol -> KSNameImpl.getCached((ktFunctionSymbol.psi as PsiMethod).name) - is KtConstructorSymbol -> KSNameImpl.getCached("") + is KaNamedFunctionSymbol -> KSNameImpl.getCached(ktFunctionSymbol.name.asString()) + is KaPropertyAccessorSymbol -> KSNameImpl.getCached((ktFunctionSymbol.psi as PsiMethod).name) + is KaConstructorSymbol -> KSNameImpl.getCached("") else -> throw IllegalStateException("Unexpected function symbol type ${ktFunctionSymbol.javaClass}") } } override val qualifiedName: KSName? by lazy { - ktFunctionSymbol.callableIdIfNonLocal?.asSingleFqName()?.asString()?.let { KSNameImpl.getCached(it) } + ktFunctionSymbol.callableId?.asSingleFqName()?.asString()?.let { KSNameImpl.getCached(it) } } override fun accept(visitor: KSVisitor, data: D): R { @@ -156,7 +157,7 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo } else { psi.bodyBlockExpression?.statements?.asSequence()?.filterIsInstance()?.mapNotNull { analyze { - it.getSymbol().toKSDeclaration() + it.symbol.toKSDeclaration() } } ?: emptySequence() } @@ -165,7 +166,7 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo override val origin: Origin by lazy { // Override origin for java synthetic constructors. if ( - ktFunctionSymbol.origin == KtSymbolOrigin.JAVA_SOURCE && + ktFunctionSymbol.origin == KaSymbolOrigin.JAVA_SOURCE && (ktFunctionSymbol.psi == null || ktFunctionSymbol.psi is PsiClass) ) { Origin.SYNTHETIC @@ -212,20 +213,20 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo } } -internal fun KtFunctionLikeSymbol.toModifiers(): Set { +internal fun KaFunctionSymbol.toModifiers(): Set { val result = mutableSetOf() when (this) { - is KtConstructorSymbol -> { - if (visibility != JavaVisibilities.PackageVisibility) { + is KaConstructorSymbol -> { + if (visibility != KaSymbolVisibility.PACKAGE_PRIVATE) { result.add(visibility.toModifier()) } result.add(Modifier.FINAL) } - is KtFunctionSymbol -> { - if (visibility != JavaVisibilities.PackageVisibility) { + is KaNamedFunctionSymbol -> { + if (visibility != KaSymbolVisibility.PACKAGE_PRIVATE) { result.add(visibility.toModifier()) } - if (!isStatic || modality != Modality.OPEN) { + if (!isStatic || modality != KaSymbolModality.OPEN) { result.add(modality.toModifier()) } if (isExternal) { @@ -251,11 +252,11 @@ internal fun KtFunctionLikeSymbol.toModifiers(): Set { result.add(Modifier.OVERRIDE) } } - is KtPropertyAccessorSymbol -> { - if (visibility != JavaVisibilities.PackageVisibility) { + is KaPropertyAccessorSymbol -> { + if (visibility != KaSymbolVisibility.PACKAGE_PRIVATE) { result.add(visibility.toModifier()) } - if (modality != Modality.OPEN) { + if (modality != KaSymbolModality.OPEN) { result.add(modality.toModifier()) } } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFunctionImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFunctionImpl.kt index f0f15d3b42..79a6aa70fd 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFunctionImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFunctionImpl.kt @@ -20,11 +20,13 @@ package com.google.devtools.ksp.impl.symbol.kotlin import com.google.devtools.ksp.symbol.KSFunction import com.google.devtools.ksp.symbol.KSType import com.google.devtools.ksp.symbol.KSTypeParameter -import org.jetbrains.kotlin.analysis.api.signatures.KtFunctionLikeSignature -import org.jetbrains.kotlin.analysis.api.symbols.KtDeclarationSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtFunctionLikeSymbol +import org.jetbrains.kotlin.analysis.api.KaExperimentalApi +import org.jetbrains.kotlin.analysis.api.signatures.KaFunctionSignature +import org.jetbrains.kotlin.analysis.api.symbols.KaDeclarationSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaFunctionSymbol +import org.jetbrains.kotlin.analysis.api.symbols.typeParameters -class KSFunctionImpl(val ktFunctionLikeSymbol: KtFunctionLikeSignature) : KSFunction { +class KSFunctionImpl(val ktFunctionLikeSymbol: KaFunctionSignature) : KSFunction { override val returnType: KSType? by lazy { ktFunctionLikeSymbol.returnType.let { KSTypeImpl.getCached(it) } @@ -34,8 +36,9 @@ class KSFunctionImpl(val ktFunctionLikeSymbol: KtFunctionLikeSignature by lazy { - (ktFunctionLikeSymbol as? KtDeclarationSymbol)?.typeParameters?.map { KSTypeParameterImpl.getCached(it) } + (ktFunctionLikeSymbol as? KaDeclarationSymbol)?.typeParameters?.map { KSTypeParameterImpl.getCached(it) } ?: emptyList() } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyAccessorImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyAccessorImpl.kt index 292a2726e9..7df08b6464 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyAccessorImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyAccessorImpl.kt @@ -22,10 +22,9 @@ import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSAnnotationResolvedI import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl import com.google.devtools.ksp.impl.symbol.util.toKSModifiers import com.google.devtools.ksp.symbol.* -import org.jetbrains.kotlin.analysis.api.annotations.annotations -import org.jetbrains.kotlin.analysis.api.symbols.KtPropertyAccessorSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtPropertyGetterSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtPropertySetterSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaPropertyAccessorSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaPropertyGetterSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaPropertySetterSymbol import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.KtModifierListOwner @@ -33,7 +32,7 @@ import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyAccessor abstract class KSPropertyAccessorImpl( - internal val ktPropertyAccessorSymbol: KtPropertyAccessorSymbol, + internal val ktPropertyAccessorSymbol: KaPropertyAccessorSymbol, override val receiver: KSPropertyDeclaration ) : KSPropertyAccessor, Deferrable { @@ -91,7 +90,7 @@ abstract class KSPropertyAccessorImpl( } else { psi.bodyBlockExpression?.statements?.asSequence()?.filterIsInstance()?.mapNotNull { analyze { - it.getSymbol().toKSDeclaration() + it.symbol.toKSDeclaration() } } ?: emptySequence() } @@ -100,10 +99,10 @@ abstract class KSPropertyAccessorImpl( class KSPropertySetterImpl private constructor( owner: KSPropertyDeclaration, - setter: KtPropertySetterSymbol + setter: KaPropertySetterSymbol ) : KSPropertyAccessorImpl(setter, owner), KSPropertySetter { - companion object : KSObjectCache, KSPropertySetterImpl>() { - fun getCached(owner: KSPropertyDeclaration, setter: KtPropertySetterSymbol) = + companion object : KSObjectCache, KSPropertySetterImpl>() { + fun getCached(owner: KSPropertyDeclaration, setter: KaPropertySetterSymbol) = cache.getOrPut(Pair(owner, setter)) { KSPropertySetterImpl(owner, setter) } } @@ -123,17 +122,17 @@ class KSPropertySetterImpl private constructor( val other = (receiver as Deferrable).defer() ?: return null return ktPropertyAccessorSymbol.defer { val owner = other.restore() ?: return@defer null - getCached(owner as KSPropertyDeclaration, it as KtPropertySetterSymbol) + getCached(owner as KSPropertyDeclaration, it as KaPropertySetterSymbol) } } } class KSPropertyGetterImpl private constructor( owner: KSPropertyDeclaration, - getter: KtPropertyGetterSymbol + getter: KaPropertyGetterSymbol ) : KSPropertyAccessorImpl(getter, owner), KSPropertyGetter { - companion object : KSObjectCache, KSPropertyGetterImpl>() { - fun getCached(owner: KSPropertyDeclaration, getter: KtPropertyGetterSymbol) = + companion object : KSObjectCache, KSPropertyGetterImpl>() { + fun getCached(owner: KSPropertyDeclaration, getter: KaPropertyGetterSymbol) = cache.getOrPut(Pair(owner, getter)) { KSPropertyGetterImpl(owner, getter) } } @@ -155,7 +154,7 @@ class KSPropertyGetterImpl private constructor( val other = (receiver as Deferrable).defer() ?: return null return ktPropertyAccessorSymbol.defer { val owner = other.restore() ?: return@defer null - getCached(owner as KSPropertyDeclaration, it as KtPropertyGetterSymbol) + getCached(owner as KSPropertyDeclaration, it as KaPropertyGetterSymbol) } } } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyDeclarationImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyDeclarationImpl.kt index 072758de47..2ad5b20ce3 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyDeclarationImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyDeclarationImpl.kt @@ -31,26 +31,27 @@ import com.google.devtools.ksp.impl.symbol.util.BinaryClassInfoCache import com.google.devtools.ksp.symbol.* import com.intellij.psi.PsiClass import org.jetbrains.kotlin.analysis.api.KaConstantInitializerValue -import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplication +import org.jetbrains.kotlin.analysis.api.KaExperimentalApi +import org.jetbrains.kotlin.analysis.api.annotations.KaAnnotation import org.jetbrains.kotlin.analysis.api.fir.symbols.KaFirKotlinPropertySymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtKotlinPropertySymbol -import org.jetbrains.kotlin.analysis.api.symbols.KtPropertySymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaKotlinPropertySymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaPropertySymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolModality +import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolVisibility import org.jetbrains.kotlin.analysis.api.symbols.receiverType -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.load.kotlin.JvmPackagePartSource import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement import org.jetbrains.kotlin.psi.KtAnnotationEntry import org.jetbrains.kotlin.psi.KtProperty -class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbol: KtPropertySymbol) : +class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbol: KaPropertySymbol) : KSPropertyDeclaration, AbstractKSDeclarationImpl(ktPropertySymbol), KSExpectActual by KSExpectActualImpl(ktPropertySymbol) { - companion object : KSObjectCache() { - fun getCached(ktPropertySymbol: KtPropertySymbol) = + companion object : KSObjectCache() { + fun getCached(ktPropertySymbol: KaPropertySymbol) = cache.getOrPut(ktPropertySymbol) { KSPropertyDeclarationImpl(ktPropertySymbol) } } @@ -130,13 +131,14 @@ class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbo !ktPropertySymbol.isVal } + @OptIn(KaExperimentalApi::class) override val hasBackingField: Boolean by lazy { if (origin == Origin.KOTLIN_LIB || origin == Origin.JAVA_LIB) { when { ktPropertySymbol.receiverParameter != null -> false ktPropertySymbol.initializer is KaConstantInitializerValue -> true - (ktPropertySymbol as? KtKotlinPropertySymbol)?.isLateInit == true -> true - ktPropertySymbol.modality == Modality.ABSTRACT -> false + (ktPropertySymbol as? KaKotlinPropertySymbol)?.isLateInit == true -> true + ktPropertySymbol.modality == KaSymbolModality.ABSTRACT -> false else -> { val classId = when ( val containerSource = @@ -170,7 +172,7 @@ class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbo } recordLookupForPropertyOrMethod(this) return analyze { - ktPropertySymbol.getDirectlyOverriddenSymbols().firstOrNull() + ktPropertySymbol.directlyOverriddenSymbols.firstOrNull() ?.unwrapFakeOverrides?.toKSDeclaration() as? KSPropertyDeclaration }?.also { recordLookupForPropertyOrMethod(it) } } @@ -192,7 +194,7 @@ class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbo } } -internal fun KtAnnotationApplication.isUseSiteTargetAnnotation(): Boolean { +internal fun KaAnnotation.isUseSiteTargetAnnotation(): Boolean { return this.useSiteTarget?.let { it == AnnotationUseSiteTarget.PROPERTY_GETTER || it == AnnotationUseSiteTarget.PROPERTY_SETTER || @@ -209,9 +211,9 @@ internal fun KtAnnotationEntry.isUseSiteTargetAnnotation(): Boolean { it == AnnotationUseSiteTarget.FIELD } ?: false } -internal fun KtPropertySymbol.toModifiers(): Set { +internal fun KaPropertySymbol.toModifiers(): Set { val result = mutableSetOf() - if (visibility != JavaVisibilities.PackageVisibility) { + if (visibility != KaSymbolVisibility.PACKAGE_PRIVATE) { result.add(visibility.toModifier()) } if (isOverride) { @@ -222,11 +224,11 @@ internal fun KtPropertySymbol.toModifiers(): Set { result.add(Modifier.FINAL) } // Analysis API returns open for static members which should be ignored. - if (!isStatic || modality != Modality.OPEN) { + if (!isStatic || modality != KaSymbolModality.OPEN) { result.add(modality.toModifier()) } - if (this is KtKotlinPropertySymbol) { + if (this is KaKotlinPropertySymbol) { if (isLateInit) { result.add(Modifier.LATEINIT) } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyDeclarationJavaImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyDeclarationJavaImpl.kt index 6e9e77b5d0..c2291d274b 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyDeclarationJavaImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSPropertyDeclarationJavaImpl.kt @@ -5,16 +5,16 @@ import com.google.devtools.ksp.common.impl.KSNameImpl import com.google.devtools.ksp.impl.ResolverAAImpl import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl import com.google.devtools.ksp.symbol.* -import org.jetbrains.kotlin.analysis.api.symbols.KtJavaFieldSymbol -import org.jetbrains.kotlin.descriptors.Modality -import org.jetbrains.kotlin.descriptors.java.JavaVisibilities +import org.jetbrains.kotlin.analysis.api.symbols.KaJavaFieldSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolModality +import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolVisibility -class KSPropertyDeclarationJavaImpl private constructor(val ktJavaFieldSymbol: KtJavaFieldSymbol) : +class KSPropertyDeclarationJavaImpl private constructor(val ktJavaFieldSymbol: KaJavaFieldSymbol) : KSPropertyDeclaration, AbstractKSDeclarationImpl(ktJavaFieldSymbol), KSExpectActual by KSExpectActualImpl(ktJavaFieldSymbol) { - companion object : KSObjectCache() { - fun getCached(ktJavaFieldSymbol: KtJavaFieldSymbol): KSPropertyDeclaration = + companion object : KSObjectCache() { + fun getCached(ktJavaFieldSymbol: KaJavaFieldSymbol): KSPropertyDeclaration = cache.getOrPut(ktJavaFieldSymbol) { KSPropertyDeclarationJavaImpl(ktJavaFieldSymbol) } } override val getter: KSPropertyGetter? @@ -56,7 +56,7 @@ class KSPropertyDeclarationJavaImpl private constructor(val ktJavaFieldSymbol: K } override val packageName: KSName - get() = KSNameImpl.getCached(ktJavaFieldSymbol.callableIdIfNonLocal?.packageName?.asString() ?: "") + get() = KSNameImpl.getCached(ktJavaFieldSymbol.callableId?.packageName?.asString() ?: "") override val origin: Origin get() = mapAAOrigin(ktJavaFieldSymbol) @@ -70,9 +70,9 @@ class KSPropertyDeclarationJavaImpl private constructor(val ktJavaFieldSymbol: K } } -internal fun KtJavaFieldSymbol.toModifiers(): Set { +internal fun KaJavaFieldSymbol.toModifiers(): Set { val result = mutableSetOf() - if (visibility != JavaVisibilities.PackageVisibility) { + if (visibility != KaSymbolVisibility.PACKAGE_PRIVATE) { result.add(visibility.toModifier()) } if (isStatic) { @@ -80,7 +80,7 @@ internal fun KtJavaFieldSymbol.toModifiers(): Set { result.add(Modifier.FINAL) } // Analysis API returns open for static members which should be ignored. - if (!isStatic || modality != Modality.OPEN) { + if (!isStatic || modality != KaSymbolModality.OPEN) { result.add(modality.toModifier()) } return result diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSTypeImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSTypeImpl.kt index b98b11a0d1..36192e9e85 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSTypeImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSTypeImpl.kt @@ -30,14 +30,15 @@ import com.google.devtools.ksp.symbol.KSDeclaration import com.google.devtools.ksp.symbol.KSType import com.google.devtools.ksp.symbol.KSTypeArgument import com.google.devtools.ksp.symbol.Nullability -import org.jetbrains.kotlin.analysis.api.KtStarTypeProjection -import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationsList +import org.jetbrains.kotlin.analysis.api.KaImplementationDetail +import org.jetbrains.kotlin.analysis.api.annotations.KaAnnotationList +import org.jetbrains.kotlin.analysis.api.impl.base.types.KaBaseStarTypeProjection import org.jetbrains.kotlin.analysis.api.symbols.* import org.jetbrains.kotlin.analysis.api.types.* -class KSTypeImpl private constructor(internal val type: KtType) : KSType { - companion object : KSObjectCache, KSTypeImpl>() { - fun getCached(type: KtType): KSTypeImpl = cache.getOrPut(IdKeyPair(type, type.annotationsList)) { +class KSTypeImpl private constructor(internal val type: KaType) : KSType { + companion object : KSObjectCache, KSTypeImpl>() { + fun getCached(type: KaType): KSTypeImpl = cache.getOrPut(IdKeyPair(type, type.annotations)) { KSTypeImpl(type) } } @@ -45,29 +46,29 @@ class KSTypeImpl private constructor(internal val type: KtType) : KSType { private fun KtType.toDeclaration(): KSDeclaration { return analyze { when (this@toDeclaration) { - is KtNonErrorClassType -> { + is KaClassType -> { when (val symbol = this@toDeclaration.classSymbol) { - is KtTypeAliasSymbol -> KSTypeAliasImpl.getCached(symbol) - is KtClassOrObjectSymbol -> KSClassDeclarationImpl.getCached(symbol) + is KaTypeAliasSymbol -> KSTypeAliasImpl.getCached(symbol) + is KaClassSymbol -> KSClassDeclarationImpl.getCached(symbol) } } - is KtTypeParameterType -> KSTypeParameterImpl.getCached(symbol) - is KtClassErrorType -> KSErrorTypeClassDeclaration(this@KSTypeImpl) - is KtFlexibleType -> + is KaTypeParameterType -> KSTypeParameterImpl.getCached(symbol) + is KaClassErrorType -> KSErrorTypeClassDeclaration(this@KSTypeImpl) + is KaFlexibleType -> type.lowerBoundIfFlexible().toDeclaration() - is KtDefinitelyNotNullType -> this@toDeclaration.original.toDeclaration() + is KaDefinitelyNotNullType -> this@toDeclaration.original.toDeclaration() else -> KSErrorTypeClassDeclaration(this@KSTypeImpl) } } } override val declaration: KSDeclaration by lazy { - (type as? KaFunctionalType)?.abbreviatedSymbol()?.toKSDeclaration() ?: type.toDeclaration() + (type as? KaFunctionType)?.abbreviatedSymbol()?.toKSDeclaration() ?: type.toDeclaration() } override val nullability: Nullability by lazy { when { - type is KtFlexibleType && type.lowerBound.nullability != type.upperBound.nullability -> Nullability.PLATFORM + type is KaFlexibleType && type.lowerBound.nullability != type.upperBound.nullability -> Nullability.PLATFORM analyze { type.canBeNull } -> Nullability.NULLABLE else -> Nullability.NOT_NULL } @@ -77,18 +78,19 @@ class KSTypeImpl private constructor(internal val type: KtType) : KSType { if (ResolverAAImpl.instance.isJavaRawType(this)) { emptyList() } else { - if (type is KtFlexibleType) { + if (type is KaFlexibleType) { type.upperBound.typeArguments().map { KSTypeArgumentResolvedImpl.getCached(it) } } else { - (type as? KtNonErrorClassType)?.typeArguments()?.map { KSTypeArgumentResolvedImpl.getCached(it) } + (type as? KaClassType)?.typeArguments()?.map { KSTypeArgumentResolvedImpl.getCached(it) } ?: emptyList() } } } + @OptIn(KaImplementationDetail::class) override val annotations: Sequence get() = type.annotations() + - if (type is KtFunctionalType && type.receiverType != null) { + if (type is KaFunctionType && type.receiverType != null) { sequenceOf( KSAnnotationResolvedImpl.getCached(getExtensionFunctionTypeAnnotation(type.annotations.size)) ) @@ -101,7 +103,7 @@ class KSTypeImpl private constructor(internal val type: KtType) : KSType { return false } recordLookupWithSupertypes((that as KSTypeImpl).type) - return type.isAssignableFrom((that as KSTypeImpl).type) + return type.isAssignableFrom(that.type) } override fun isMutabilityFlexible(): Boolean { @@ -126,34 +128,35 @@ class KSTypeImpl private constructor(internal val type: KtType) : KSType { return getCached(type.replace(arguments.map { it.toKtTypeProjection() })) } + @OptIn(KaImplementationDetail::class) override fun starProjection(): KSType { - return getCached(type.replace(List(type.typeArguments().size) { KtStarTypeProjection(type.token) })) + return getCached(type.replace(List(type.typeArguments().size) { KaBaseStarTypeProjection(type.token) })) } override fun makeNullable(): KSType { return analyze { - getCached(type.withNullability(KtTypeNullability.NULLABLE)) + getCached(type.withNullability(KaTypeNullability.NULLABLE)) } } override fun makeNotNullable(): KSType { return analyze { - getCached(type.withNullability(KtTypeNullability.NON_NULLABLE)) + getCached(type.withNullability(KaTypeNullability.NON_NULLABLE)) } } override val isMarkedNullable: Boolean - get() = type.nullability == KtTypeNullability.NULLABLE + get() = type.nullability == KaTypeNullability.NULLABLE override val isError: Boolean // TODO: non exist type returns KtNonErrorClassType, check upstream for KtClassErrorType usage. - get() = type is KtErrorType || type.classifierSymbol() == null + get() = type is KaErrorType || type.classifierSymbol() == null override val isFunctionType: Boolean - get() = type is KtFunctionalType && !type.isSuspend + get() = type is KaFunctionType && !type.isSuspend override val isSuspendFunctionType: Boolean - get() = type is KtFunctionalType && type.isSuspend + get() = type is KaFunctionType && type.isSuspend override fun hashCode(): Int { return type.toAbbreviatedType().hashCode() @@ -164,7 +167,7 @@ class KSTypeImpl private constructor(internal val type: KtType) : KSType { return false } return analyze { - type.isEqualTo(other.type) + type.semanticallyEquals(other.type) } } @@ -173,10 +176,10 @@ class KSTypeImpl private constructor(internal val type: KtType) : KSType { } } -internal fun KtType.toAbbreviatedType(): KtType { +internal fun KaType.toAbbreviatedType(): KaType { val symbol = this.classifierSymbol() return when (symbol) { - is KtTypeAliasSymbol -> symbol.expandedType.toAbbreviatedType() + is KaTypeAliasSymbol -> symbol.expandedType.toAbbreviatedType() else -> this } } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSAnnotationResolvedImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSAnnotationResolvedImpl.kt index 4de2f2056d..cc6d273af0 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSAnnotationResolvedImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSAnnotationResolvedImpl.kt @@ -24,22 +24,21 @@ import com.intellij.psi.PsiAnnotationMethod import com.intellij.psi.PsiArrayInitializerMemberValue import com.intellij.psi.PsiClass import com.intellij.psi.impl.compiled.ClsClassImpl -import org.jetbrains.kotlin.analysis.api.KaAnalysisApiInternals -import org.jetbrains.kotlin.analysis.api.annotations.KaNamedAnnotationValue -import org.jetbrains.kotlin.analysis.api.annotations.KaUnsupportedAnnotationValue -import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplicationWithArgumentsInfo -import org.jetbrains.kotlin.analysis.api.components.buildClassType +import org.jetbrains.kotlin.analysis.api.KaImplementationDetail +import org.jetbrains.kotlin.analysis.api.annotations.KaAnnotation +import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaBaseNamedAnnotationValue +import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaUnsupportedAnnotationValueImpl import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeToken import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolOrigin import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.* class KSAnnotationResolvedImpl private constructor( - private val annotationApplication: KtAnnotationApplicationWithArgumentsInfo, + private val annotationApplication: KaAnnotation, override val parent: KSNode? ) : KSAnnotation { companion object : - KSObjectCache, KSAnnotationResolvedImpl>() { - fun getCached(annotationApplication: KtAnnotationApplicationWithArgumentsInfo, parent: KSNode? = null) = + KSObjectCache, KSAnnotationResolvedImpl>() { + fun getCached(annotationApplication: KaAnnotation, parent: KSNode? = null) = cache.getOrPut(IdKeyPair(annotationApplication, parent)) { KSAnnotationResolvedImpl(annotationApplication, parent) } @@ -61,7 +60,7 @@ class KSAnnotationResolvedImpl private constructor( presentArgs + absentArgs } - @OptIn(KaAnalysisApiInternals::class) + @OptIn(KaImplementationDetail::class) override val defaultArguments: List by lazy { analyze { annotationApplication.classId?.toKtClassSymbol()?.let { symbol -> @@ -84,17 +83,16 @@ class KSAnnotationResolvedImpl private constructor( } } } else { - symbol.getMemberScope().constructors.singleOrNull()?.let { + symbol.memberScope.constructors.singleOrNull()?.let { it.valueParameters.map { valueParameterSymbol -> valueParameterSymbol.getDefaultValue().let { constantValue -> KSValueArgumentImpl.getCached( - KaNamedAnnotationValue( + KaBaseNamedAnnotationValue( valueParameterSymbol.name, constantValue - ?: KaUnsupportedAnnotationValue( + ?: KaUnsupportedAnnotationValueImpl( KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project) - ), - KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project) + ) ), Origin.SYNTHETIC ) diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/synthetic/KSSyntheticAnnotations.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/synthetic/KSSyntheticAnnotations.kt index 3c7ba27a9a..2e7f47fa87 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/synthetic/KSSyntheticAnnotations.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/synthetic/KSSyntheticAnnotations.kt @@ -1,10 +1,12 @@ package com.google.devtools.ksp.impl.symbol.kotlin.synthetic import com.google.devtools.ksp.impl.ResolverAAImpl +import org.jetbrains.kotlin.analysis.api.KaImplementationDetail import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaAnnotationImpl import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeToken import org.jetbrains.kotlin.name.ClassId +@OptIn(KaImplementationDetail::class) fun getExtensionFunctionTypeAnnotation(index: Int) = KaAnnotationImpl( ClassId.fromString(ExtensionFunctionType::class.qualifiedName!!), null, diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt index c536a2836f..196e1fe3e7 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt @@ -16,7 +16,6 @@ */ @file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") -@file:OptIn(KtAnalysisApiInternals::class, KtAnalysisApiInternals::class) package com.google.devtools.ksp.impl.symbol.kotlin @@ -33,32 +32,30 @@ import com.google.devtools.ksp.symbol.* import com.intellij.psi.PsiElement import com.intellij.psi.PsiJavaFile import com.intellij.psi.impl.compiled.ClsMemberImpl -import org.jetbrains.kotlin.analysis.api.KaAnalysisNonPublicApi -import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals -import org.jetbrains.kotlin.analysis.api.KtAnalysisSession -import org.jetbrains.kotlin.analysis.api.KtStarTypeProjection -import org.jetbrains.kotlin.analysis.api.KtTypeArgumentWithVariance -import org.jetbrains.kotlin.analysis.api.KtTypeProjection +import org.jetbrains.kotlin.analysis.api.KaExperimentalApi +import org.jetbrains.kotlin.analysis.api.KaImplementationDetail +import org.jetbrains.kotlin.analysis.api.KaNonPublicApi +import org.jetbrains.kotlin.analysis.api.KaSession import org.jetbrains.kotlin.analysis.api.analyze import org.jetbrains.kotlin.analysis.api.annotations.* -import org.jetbrains.kotlin.analysis.api.components.KtSubstitutorBuilder -import org.jetbrains.kotlin.analysis.api.components.buildClassType -import org.jetbrains.kotlin.analysis.api.components.buildTypeParameterType +import org.jetbrains.kotlin.analysis.api.components.KaSubstitutorBuilder import org.jetbrains.kotlin.analysis.api.fir.KaSymbolByFirBuilder import org.jetbrains.kotlin.analysis.api.fir.evaluate.FirAnnotationValueConverter import org.jetbrains.kotlin.analysis.api.fir.symbols.KaFirValueParameterSymbol import org.jetbrains.kotlin.analysis.api.fir.types.KaFirFunctionalType import org.jetbrains.kotlin.analysis.api.fir.types.KaFirType import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaAnnotationImpl +import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaArrayAnnotationValueImpl +import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaClassLiteralAnnotationValueImpl +import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaNestedAnnotationAnnotationValueImpl +import org.jetbrains.kotlin.analysis.api.impl.base.types.KaBaseStarTypeProjection +import org.jetbrains.kotlin.analysis.api.impl.base.types.KaBaseTypeArgumentWithVariance import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeToken +import org.jetbrains.kotlin.analysis.api.projectStructure.KaLibraryModule import org.jetbrains.kotlin.analysis.api.symbols.* -import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers +import org.jetbrains.kotlin.analysis.api.symbols.markers.KaDeclarationContainerSymbol import org.jetbrains.kotlin.analysis.api.types.* -import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap -import org.jetbrains.kotlin.descriptors.Modality -import org.jetbrains.kotlin.descriptors.Visibilities -import org.jetbrains.kotlin.descriptors.java.JavaVisibilities import org.jetbrains.kotlin.fir.declarations.getTargetType import org.jetbrains.kotlin.fir.expressions.FirAnnotation import org.jetbrains.kotlin.fir.expressions.FirArrayLiteral @@ -99,35 +96,39 @@ import org.jetbrains.org.objectweb.asm.MethodVisitor import org.jetbrains.org.objectweb.asm.Opcodes internal val ktSymbolOriginToOrigin = mapOf( - KtSymbolOrigin.JAVA_SOURCE to Origin.JAVA, - KtSymbolOrigin.JAVA_LIBRARY to Origin.JAVA_LIB, - KtSymbolOrigin.SOURCE to Origin.KOTLIN, - KtSymbolOrigin.SAM_CONSTRUCTOR to Origin.SYNTHETIC, - KtSymbolOrigin.SOURCE_MEMBER_GENERATED to Origin.SYNTHETIC, - KtSymbolOrigin.DELEGATED to Origin.SYNTHETIC, - KtSymbolOrigin.PROPERTY_BACKING_FIELD to Origin.KOTLIN, - KtSymbolOrigin.JAVA_SYNTHETIC_PROPERTY to Origin.SYNTHETIC, - KtSymbolOrigin.INTERSECTION_OVERRIDE to Origin.KOTLIN, + KaSymbolOrigin.JAVA_SOURCE to Origin.JAVA, + KaSymbolOrigin.JAVA_LIBRARY to Origin.JAVA_LIB, + KaSymbolOrigin.SOURCE to Origin.KOTLIN, + KaSymbolOrigin.SAM_CONSTRUCTOR to Origin.SYNTHETIC, + KaSymbolOrigin.SOURCE_MEMBER_GENERATED to Origin.SYNTHETIC, + KaSymbolOrigin.DELEGATED to Origin.SYNTHETIC, + KaSymbolOrigin.PROPERTY_BACKING_FIELD to Origin.KOTLIN, + KaSymbolOrigin.JAVA_SYNTHETIC_PROPERTY to Origin.SYNTHETIC, + KaSymbolOrigin.INTERSECTION_OVERRIDE to Origin.KOTLIN, // TODO: distinguish between kotlin library and java library. - KtSymbolOrigin.LIBRARY to Origin.KOTLIN_LIB, - KtSymbolOrigin.SUBSTITUTION_OVERRIDE to Origin.JAVA_LIB + KaSymbolOrigin.LIBRARY to Origin.KOTLIN_LIB, + KaSymbolOrigin.SUBSTITUTION_OVERRIDE to Origin.JAVA_LIB ) -internal fun mapAAOrigin(ktSymbol: KtSymbol): Origin { +internal fun mapAAOrigin(ktSymbol: KaSymbol): Origin { val symbolOrigin = ktSymbolOriginToOrigin[ktSymbol.origin] ?: throw IllegalStateException("unhandled origin ${ktSymbol.origin.name}") return if (symbolOrigin == Origin.JAVA && ktSymbol.psi?.containingFile?.fileType?.isBinary == true) { Origin.JAVA_LIB } else { - if (ktSymbol.psi == null && analyze { ktSymbol.getContainingModule() is KtLibraryModule }) { - Origin.KOTLIN_LIB + if (ktSymbol.psi == null) { + if (analyze { ktSymbol.containingModule is KaLibraryModule }) { + Origin.KOTLIN_LIB + } else { + Origin.SYNTHETIC + } } else { symbolOrigin } } } -internal fun KtAnnotationApplicationWithArgumentsInfo.render(): String { +internal fun KaAnnotation.render(): String { return buildString { append("@") if (this@render.useSiteTarget != null) { @@ -140,81 +141,85 @@ internal fun KtAnnotationApplicationWithArgumentsInfo.render(): String { } } -internal fun KtAnnotationValue.render(): String { +internal fun KaAnnotationValue.render(): String { return when (this) { - is KtAnnotationApplicationValue -> annotationValue.render() - is KtArrayAnnotationValue -> values.joinToString(",", "{", "}") { it.render() } - is KtConstantAnnotationValue -> constantValue.renderAsKotlinConstant() - is KtEnumEntryAnnotationValue -> callableId.toString() - is KtKClassAnnotationValue -> { + is KaAnnotationValue.NestedAnnotationValue -> annotation.render() + is KaAnnotationValue.ArrayValue -> values.joinToString(",", "{", "}") { it.render() } + is KaAnnotationValue.ConstantValue -> value.render() + is KaAnnotationValue.EnumEntryValue -> callableId.toString() + is KaAnnotationValue.ClassLiteralValue -> { val type = KSTypeImpl.getCached(type) // KSTypeImpl takes care of the error types, if applicable if (type.isError) type.toString() else "$type::class" } - is KtUnsupportedAnnotationValue -> throw IllegalStateException("Unsupported annotation value: $this") + is KaAnnotationValue.UnsupportedValue -> throw IllegalStateException("Unsupported annotation value: $this") } } -@OptIn(KaAnalysisNonPublicApi::class) -internal fun KtType.render(inFunctionType: Boolean = false): String { +@OptIn(KaNonPublicApi::class) +internal fun KaType.render(inFunctionType: Boolean = false): String { return buildString { annotations.forEach { append("[${it.render()}] ") } append( when (this@render) { - is KtNonErrorClassType -> buildString { + is KaClassType -> buildString { val symbol = this@render.classifierSymbol() - if (symbol is KtTypeAliasSymbol) { + if (symbol is KaTypeAliasSymbol) { if (!inFunctionType) { append("[typealias ${symbol.name.asString()}]") } else { append(this@render.toAbbreviatedType().render(inFunctionType)) } } else { - append(classSymbol.name?.asString()) + append(this@render.symbol.name?.asString()) if (typeArguments().isNotEmpty()) { typeArguments().joinToString(separator = ", ", prefix = "<", postfix = ">") { when (it) { - is KtStarTypeProjection -> "*" - is KtTypeArgumentWithVariance -> + is KaStarTypeProjection -> "*" + is KaTypeArgumentWithVariance -> "${it.variance}" + (if (it.variance != Variance.INVARIANT) " " else "") + - it.type.render(this@render is KtFunctionalType) + it.type.render(this@render is KaFunctionType) + + else -> throw IllegalStateException("Unhandled type argument type ${it.javaClass}") } }.also { append(it) } } } } - is KtClassErrorType -> KSErrorType(qualifiers.joinToString(".") { it.name.asString() }).toString() - is KtTypeErrorType -> KSErrorType(presentableText).toString() - is KtCapturedType -> asStringForDebugging() - is KtDefinitelyNotNullType -> original.render(inFunctionType) + " & Any" - is KtDynamicType -> "" - is KtFlexibleType -> "(${lowerBound.render(inFunctionType)}..${upperBound.render(inFunctionType)})" - is KtIntersectionType -> + is KaClassErrorType -> KSErrorType(qualifiers.joinToString(".") { it.name.asString() }).toString() + is KaErrorType -> KSErrorType(presentableText).toString() + is KaCapturedType -> this@render.toString() + is KaDefinitelyNotNullType -> original.render(inFunctionType) + " & Any" + is KaDynamicType -> "" + is KaFlexibleType -> "(${lowerBound.render(inFunctionType)}..${upperBound.render(inFunctionType)})" + is KaIntersectionType -> this@render.conjuncts .joinToString(separator = " & ", prefix = "(", postfix = ")") { it.render(inFunctionType) } - is KtTypeParameterType -> name.asString() - } + if (nullability == KtTypeNullability.NULLABLE) "?" else "" + is KaTypeParameterType -> name.asString() + else -> throw IllegalStateException("Unhandled type ${this@render.javaClass}") + } + if (nullability == KaTypeNullability.NULLABLE) "?" else "" ) } } -internal fun KtType.toClassifierReference(parent: KSTypeReference?): KSReferenceElement? { +internal fun KaType.toClassifierReference(parent: KSTypeReference?): KSReferenceElement? { return when (val ktType = this) { - is KtFunctionalType -> KSCallableReferenceImpl.getCached(ktType, parent) - is KtDynamicType -> KSDynamicReferenceImpl.getCached(parent!!) - is KtUsualClassType -> KSClassifierReferenceResolvedImpl.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) + is KaFunctionType -> KSCallableReferenceImpl.getCached(ktType, parent) + is KaDynamicType -> KSDynamicReferenceImpl.getCached(parent!!) + is KaUsualClassType -> KSClassifierReferenceResolvedImpl.getCached(ktType, ktType.qualifiers.size - 1, parent) + is KaFlexibleType -> ktType.lowerBound.toClassifierReference(parent) + is KaErrorType -> null + is KaTypeParameterType -> KSClassifierParameterImpl.getCached(ktType, parent) + is KaDefinitelyNotNullType -> KSDefNonNullReferenceImpl.getCached(ktType, parent) else -> throw IllegalStateException("Unexpected type element ${ktType.javaClass}, $ExceptionMessage") } } -internal fun KSTypeArgument.toKtTypeProjection(): KtTypeProjection { +@OptIn(KaImplementationDetail::class) +internal fun KSTypeArgument.toKtTypeProjection(): KaTypeProjection { val variance = when (this.variance) { com.google.devtools.ksp.symbol.Variance.INVARIANT -> Variance.INVARIANT com.google.devtools.ksp.symbol.Variance.COVARIANT -> Variance.OUT_VARIANCE @@ -223,11 +228,11 @@ internal fun KSTypeArgument.toKtTypeProjection(): KtTypeProjection { } val argType = (this.type?.resolve() as? KSTypeImpl)?.type // TODO: maybe make a singleton of alwaysAccessibleLifetimeToken? - val alwaysAccessibleLifetimeToken = KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project!!) + val alwaysAccessibleLifetimeToken = KotlinAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project) return if (argType == null || variance == null) { - KtStarTypeProjection(alwaysAccessibleLifetimeToken) + KaBaseStarTypeProjection(alwaysAccessibleLifetimeToken) } else { - KtTypeArgumentWithVariance(argType, variance, alwaysAccessibleLifetimeToken) + KaBaseTypeArgumentWithVariance(argType, variance, alwaysAccessibleLifetimeToken) } } @@ -240,67 +245,69 @@ internal fun PsiElement?.toLocation(): Location { return FileLocation(file.virtualFile.path, document.getLineNumber(this.textOffset) + 1) } -internal fun KtSymbol.toContainingFile(): KSFile? { +internal fun KaSymbol.toContainingFile(): KSFile? { return when (val psi = this.psi) { is KtElement -> analyze { - KSFileImpl.getCached(psi.containingKtFile.getFileSymbol()) + KSFileImpl.getCached(psi.containingKtFile.symbol) } is PsiElement -> KSFileJavaImpl.getCached(psi.containingFile as PsiJavaFile) else -> null } } -internal fun KtSymbol.toDocString(): String? = this.psi?.getDocString() +internal fun KaSymbol.toDocString(): String? = this.psi?.getDocString() -internal inline fun analyze(crossinline action: KtAnalysisSession.() -> R): R { +internal inline fun analyze(crossinline action: KaSession.() -> R): R { return analyze(ResolverAAImpl.ktModule, action) } -internal fun KtSymbolWithMembers.declarations(): Sequence { +internal fun KaDeclarationContainerSymbol.declarations(): Sequence { return analyze { this@declarations.let { - it.getDeclaredMemberScope().getAllSymbols() + it.getStaticDeclaredMemberScope().getAllSymbols() + it.declaredMemberScope.declarations + it.staticDeclaredMemberScope.declarations }.distinct().map { symbol -> when (symbol) { - is KtNamedClassOrObjectSymbol -> KSClassDeclarationImpl.getCached(symbol) - is KtFunctionLikeSymbol -> KSFunctionDeclarationImpl.getCached(symbol) - is KtPropertySymbol -> KSPropertyDeclarationImpl.getCached(symbol) - is KtEnumEntrySymbol -> KSClassDeclarationEnumEntryImpl.getCached(symbol) - is KtJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(symbol) + is KaNamedClassSymbol -> KSClassDeclarationImpl.getCached(symbol) + is KaFunctionSymbol -> KSFunctionDeclarationImpl.getCached(symbol) + is KaPropertySymbol -> KSPropertyDeclarationImpl.getCached(symbol) + is KaEnumEntrySymbol -> KSClassDeclarationEnumEntryImpl.getCached(symbol) + is KaJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(symbol) else -> throw IllegalStateException() } }.memoized() } } -internal fun KtSymbolWithMembers.getAllProperties(): Sequence { +@OptIn(KaExperimentalApi::class) +internal fun KaDeclarationContainerSymbol.getAllProperties(): Sequence { return analyze { - this@getAllProperties.getMemberScope().getCallableSymbols() + this@getAllProperties.memberScope.callables { true } .filter { - it.isVisibleInClass(this@getAllProperties as KtClassOrObjectSymbol) || - it.getContainingSymbol() == this@getAllProperties + it.isVisibleInClass(this@getAllProperties as KaClassSymbol) || + it.containingSymbol == this@getAllProperties } .mapNotNull { callableSymbol -> when (callableSymbol) { - is KtPropertySymbol -> KSPropertyDeclarationImpl.getCached(callableSymbol) - is KtJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(callableSymbol) + is KaPropertySymbol -> KSPropertyDeclarationImpl.getCached(callableSymbol) + is KaJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(callableSymbol) else -> null } } } } -internal fun KtSymbolWithMembers.getAllFunctions(): Sequence { +@OptIn(KaExperimentalApi::class) +internal fun KaDeclarationContainerSymbol.getAllFunctions(): Sequence { return analyze { - this@getAllFunctions.getMemberScope().let { it.getCallableSymbols() + it.constructors } + this@getAllFunctions.memberScope.let { it.callables { true } + it.constructors } .filter { - it.isVisibleInClass(this@getAllFunctions as KtClassOrObjectSymbol) || - it.getContainingSymbol() == this@getAllFunctions + it.isVisibleInClass(this@getAllFunctions as KaClassSymbol) || + it.containingSymbol == this@getAllFunctions } .mapNotNull { callableSymbol -> // TODO: replace with single safe cast if no more implementations of KSFunctionDeclaration is added. when (callableSymbol) { - is KtFunctionLikeSymbol -> KSFunctionDeclarationImpl.getCached(callableSymbol) + is KaFunctionSymbol -> KSFunctionDeclarationImpl.getCached(callableSymbol) else -> null } } @@ -323,68 +330,69 @@ internal fun KtAnnotated.annotations( } } -internal fun KtSymbol.getContainingKSSymbol(): KSDeclaration? { +internal fun KaSymbol.getContainingKSSymbol(): KSDeclaration? { return analyze { - when (val containingSymbol = this@getContainingKSSymbol.getContainingSymbol()) { - is KtNamedClassOrObjectSymbol -> KSClassDeclarationImpl.getCached(containingSymbol) - is KtFunctionLikeSymbol -> KSFunctionDeclarationImpl.getCached(containingSymbol) - is KtPropertySymbol -> KSPropertyDeclarationImpl.getCached(containingSymbol) + when (val containingSymbol = this@getContainingKSSymbol.containingSymbol) { + is KaNamedClassSymbol -> KSClassDeclarationImpl.getCached(containingSymbol) + is KaFunctionSymbol -> KSFunctionDeclarationImpl.getCached(containingSymbol) + is KaPropertySymbol -> KSPropertyDeclarationImpl.getCached(containingSymbol) else -> null } } } -internal fun KtSymbol.toKSDeclaration(): KSDeclaration? = this.toKSNode() as? KSDeclaration +internal fun KaSymbol.toKSDeclaration(): KSDeclaration? = this.toKSNode() as? KSDeclaration -internal fun KtSymbol.toKSNode(): KSNode { +internal fun KaSymbol.toKSNode(): KSNode { return when (this) { - is KtPropertySymbol -> KSPropertyDeclarationImpl.getCached(this) - is KtNamedClassOrObjectSymbol -> KSClassDeclarationImpl.getCached(this) - is KtFunctionLikeSymbol -> KSFunctionDeclarationImpl.getCached(this) - is KtTypeAliasSymbol -> KSTypeAliasImpl.getCached(this) - is KtJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(this) - is KtFileSymbol -> KSFileImpl.getCached(this) - is KtEnumEntrySymbol -> KSClassDeclarationEnumEntryImpl.getCached(this) - is KtTypeParameterSymbol -> KSTypeParameterImpl.getCached(this) - is KtLocalVariableSymbol -> KSPropertyDeclarationLocalVariableImpl.getCached(this) + is KaPropertySymbol -> KSPropertyDeclarationImpl.getCached(this) + is KaNamedClassSymbol -> KSClassDeclarationImpl.getCached(this) + is KaFunctionSymbol -> KSFunctionDeclarationImpl.getCached(this) + is KaTypeAliasSymbol -> KSTypeAliasImpl.getCached(this) + is KaJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(this) + is KaFileSymbol -> KSFileImpl.getCached(this) + is KaEnumEntrySymbol -> KSClassDeclarationEnumEntryImpl.getCached(this) + is KaTypeParameterSymbol -> KSTypeParameterImpl.getCached(this) + is KaLocalVariableSymbol -> KSPropertyDeclarationLocalVariableImpl.getCached(this) else -> throw IllegalStateException("Unexpected class for KtSymbol: ${this.javaClass}") } } -internal fun ClassId.toKtClassSymbol(): KtClassOrObjectSymbol? { +internal fun ClassId.toKtClassSymbol(): KaClassSymbol? { return analyze { if (this@toKtClassSymbol.isLocal) { - this@toKtClassSymbol.outerClassId?.toKtClassSymbol()?.getDeclaredMemberScope()?.classifiers { + this@toKtClassSymbol.outerClassId?.toKtClassSymbol()?.declaredMemberScope?.classifiers { it.asString() == this@toKtClassSymbol.shortClassName.asString() - }?.singleOrNull() as? KtClassOrObjectSymbol + }?.singleOrNull() as? KaClassSymbol } else { - getClassOrObjectSymbolByClassId(this@toKtClassSymbol) + findClass(this@toKtClassSymbol) } } } -internal fun ClassId.toTypeAlias(): KtTypeAliasSymbol? { +internal fun ClassId.toTypeAlias(): KaTypeAliasSymbol? { return analyze { - getTypeAliasByClassId(this@toTypeAlias) + findTypeAlias(this@toTypeAlias) } } -internal fun KtType.classifierSymbol(): KtClassifierSymbol? { +internal fun KaType.classifierSymbol(): KaClassifierSymbol? { return try { when (this) { - is KtTypeParameterType -> this.symbol + is KaTypeParameterType -> this.symbol // TODO: upstream is not exposing enough information for captured types. - is KtCapturedType -> TODO("fix in upstream") - is KtClassErrorType, is KtTypeErrorType -> null - is KtFunctionalType -> (this as? KaFirFunctionalType)?.abbreviatedSymbol() ?: symbol - is KtUsualClassType -> classSymbol - is KtDefinitelyNotNullType -> original.classifierSymbol() - is KtDynamicType -> null + is KaCapturedType -> TODO("fix in upstream") + is KaClassErrorType, is KaErrorType -> null + is KaFunctionType -> (this as? KaFirFunctionalType)?.abbreviatedSymbol() ?: symbol + is KaUsualClassType -> symbol + is KaDefinitelyNotNullType -> original.classifierSymbol() + is KaDynamicType -> null // flexible types have 2 bounds, using lower bound for a safer approximation. // TODO: find a case where lower bound is not appropriate. - is KtFlexibleType -> lowerBound.classifierSymbol() + is KaFlexibleType -> lowerBound.classifierSymbol() // TODO: KSP does not support intersection type. - is KtIntersectionType -> null + is KaIntersectionType -> null + else -> throw IllegalStateException("Unexpected type ${this.javaClass}") } } catch (e: KotlinExceptionWithAttachments) { // The implementation for getting symbols from a type throws an excpetion @@ -393,21 +401,21 @@ internal fun KtType.classifierSymbol(): KtClassifierSymbol? { } } -internal fun KtType.typeArguments(): List { - return if (this is KtFlexibleType) { +internal fun KaType.typeArguments(): List { + return if (this is KaFlexibleType) { this.lowerBound } else { this - }.let { (it as? KtNonErrorClassType)?.qualifiers?.reversed()?.flatMap { it.typeArguments } ?: emptyList() } + }.let { (it as? KaClassType)?.qualifiers?.reversed()?.flatMap { it.typeArguments } ?: emptyList() } } internal fun KSAnnotated.findAnnotationFromUseSiteTarget(): Sequence { return when (this) { is KSPropertyGetter -> (this.receiver as? AbstractKSDeclarationImpl)?.let { - it.originalAnnotations.asSequence().filter { it.useSiteTarget == AnnotationUseSiteTarget.GET } + it.originalAnnotations.filter { it.useSiteTarget == AnnotationUseSiteTarget.GET } } is KSPropertySetter -> (this.receiver as? AbstractKSDeclarationImpl)?.let { - it.originalAnnotations.asSequence().filter { it.useSiteTarget == AnnotationUseSiteTarget.SET } + it.originalAnnotations.filter { it.useSiteTarget == AnnotationUseSiteTarget.SET } } is KSValueParameter -> { var parent = this.parent @@ -432,7 +440,7 @@ internal fun KSAnnotated.findAnnotationFromUseSiteTarget(): Sequence { +internal fun KSAnnotated.findAnnotationApplicationFromUseSiteTarget(): Sequence { return when (this) { is KSPropertyGetter -> (this.receiver as? AbstractKSDeclarationImpl)?.let { it.ktDeclarationSymbol.annotations.filter { @@ -447,7 +455,7 @@ internal fun KSAnnotated.findAnnotationApplicationFromUseSiteTarget(): Sequence< is KSValueParameter -> { var parent = this.parent // TODO: eliminate annotationsFromParents to make this fully sequence. - val annotationsFromParents = mutableListOf() + val annotationsFromParents = mutableListOf() (parent as? KSPropertyAccessorImpl)?.let { annotationsFromParents.addAll( it.ktPropertyAccessorSymbol.annotations @@ -473,23 +481,23 @@ internal fun KSAnnotated.findAnnotationApplicationFromUseSiteTarget(): Sequence< } ?: emptySequence() } -internal fun org.jetbrains.kotlin.descriptors.Visibility.toModifier(): Modifier { +internal fun KaSymbolVisibility.toModifier(): Modifier { return when (this) { - Visibilities.Public -> Modifier.PUBLIC - Visibilities.Private -> Modifier.PRIVATE - Visibilities.Internal -> Modifier.INTERNAL - Visibilities.Protected, JavaVisibilities.ProtectedAndPackage, JavaVisibilities.ProtectedStaticVisibility -> + KaSymbolVisibility.PUBLIC -> Modifier.PUBLIC + KaSymbolVisibility.PRIVATE -> Modifier.PRIVATE + KaSymbolVisibility.INTERNAL -> Modifier.INTERNAL + KaSymbolVisibility.PROTECTED, KaSymbolVisibility.PACKAGE_PROTECTED -> Modifier.PROTECTED else -> Modifier.PUBLIC } } -internal fun Modality.toModifier(): Modifier { +internal fun KaSymbolModality.toModifier(): Modifier { return when (this) { - Modality.FINAL -> Modifier.FINAL - Modality.ABSTRACT -> Modifier.ABSTRACT - Modality.OPEN -> Modifier.OPEN - Modality.SEALED -> Modifier.SEALED + KaSymbolModality.FINAL -> Modifier.FINAL + KaSymbolModality.ABSTRACT -> Modifier.ABSTRACT + KaSymbolModality.OPEN -> Modifier.OPEN + KaSymbolModality.SEALED -> Modifier.SEALED } } @@ -501,11 +509,11 @@ internal inline fun KSNode.findParentOfType(): KSNode? { return result } -internal fun KtAnnotationValue.toValue(): Any? = when (this) { - is KtArrayAnnotationValue -> this.values.map { it.toValue() } - is KtAnnotationApplicationValue -> KSAnnotationResolvedImpl.getCached(this.annotationValue) +internal fun KaAnnotationValue.toValue(): Any? = when (this) { + is KaAnnotationValue.ArrayValue -> this.values.map { it.toValue() } + is KaAnnotationValue.NestedAnnotationValue -> KSAnnotationResolvedImpl.getCached(this.annotation) // TODO: Enum entry should return a type, use declaration as a placeholder. - is KtEnumEntryAnnotationValue -> this.callableId?.classId?.let { + is KaAnnotationValue.EnumEntryValue -> this.callableId?.classId?.let { analyze { it.toKtClassSymbol()?.let { it.declarations().filterIsInstance().singleOrNull { @@ -514,18 +522,18 @@ internal fun KtAnnotationValue.toValue(): Any? = when (this) { } } } ?: KSErrorType - is KtKClassAnnotationValue -> { + is KaAnnotationValue.ClassLiteralValue -> { KSTypeImpl.getCached(this@toValue.type) } - is KtConstantAnnotationValue -> this.constantValue.value - is KtUnsupportedAnnotationValue -> null + is KaAnnotationValue.ConstantValue -> this.value.value + is KaAnnotationValue.UnsupportedValue -> null } -@OptIn(SymbolInternals::class) -internal fun KtValueParameterSymbol.getDefaultValue(): KtAnnotationValue? { - fun FirExpression.toValue(builder: KaSymbolByFirBuilder): KtAnnotationValue? { +@OptIn(SymbolInternals::class, KaImplementationDetail::class, KaExperimentalApi::class) +internal fun KaValueParameterSymbol.getDefaultValue(): KaAnnotationValue? { + fun FirExpression.toValue(builder: KaSymbolByFirBuilder): KaAnnotationValue? { if (this is FirAnnotation) { - return KtAnnotationApplicationValue( + return KaNestedAnnotationAnnotationValueImpl( KaAnnotationImpl( JavaToKotlinClassMap.mapJavaToKotlinIncludingClassMapping( ClassId.fromString( @@ -544,7 +552,7 @@ internal fun KtValueParameterSymbol.getDefaultValue(): KtAnnotationValue? { ) } if (this is FirArrayLiteral) { - return KtArrayAnnotationValue(argumentList.arguments.mapNotNull { it.toValue(builder) }, null, token) + return KaArrayAnnotationValueImpl(argumentList.arguments.mapNotNull { it.toValue(builder) }, null, token) } return if (this is FirGetClassCall) { var coneType = this.getTargetType()?.fullyExpandedType(builder.rootSession) @@ -558,7 +566,7 @@ internal fun KtValueParameterSymbol.getDefaultValue(): KtAnnotationValue? { val classId = JavaToKotlinClassMap .mapJavaToKotlinIncludingClassMapping(coneType.lookupTag.classId.asSingleFqName()) val type = builder.typeBuilder.buildKtType(coneType).convertToKotlinType() - KtKClassAnnotationValue(type, classId, null, token) + KaClassLiteralAnnotationValueImpl(type, classId, null, token) } else { null } @@ -626,25 +634,26 @@ internal fun KtValueParameterSymbol.getDefaultValue(): KtAnnotationValue? { } } -internal fun fillInDeepSubstitutor(context: KtType, substitutorBuilder: KtSubstitutorBuilder) { - if (context !is KtNonErrorClassType) { +@OptIn(KaExperimentalApi::class) +internal fun fillInDeepSubstitutor(context: KaType, substitutorBuilder: KaSubstitutorBuilder) { + if (context !is KaClassType) { return } - val parameters = context.classSymbol.typeParameters - val arguments = context.ownTypeArguments + val parameters = context.symbol.typeParameters + val arguments = context.typeArguments if (parameters.size != arguments.size) { throw IllegalStateException("invalid substitution for $context") } parameters.zip(arguments).forEach { substitutorBuilder.substitution(it.first, it.second.type ?: it.first.upperBounds.first()) } - (context.classSymbol as? KtClassOrObjectSymbol)?.superTypes?.forEach { + (context.symbol as? KaClassSymbol)?.superTypes?.forEach { fillInDeepSubstitutor(it, substitutorBuilder) } } -internal fun KtSymbol.psiIfSource(): PsiElement? { - return if (origin == KtSymbolOrigin.SOURCE || origin == KtSymbolOrigin.JAVA_SOURCE && toContainingFile() != null) { +internal fun KaSymbol.psiIfSource(): PsiElement? { + return if (origin == KaSymbolOrigin.SOURCE || origin == KaSymbolOrigin.JAVA_SOURCE && toContainingFile() != null) { psi } else { null @@ -659,7 +668,7 @@ fun interface Deferrable { fun defer(): Restorable? } -fun T.defer(restore: (T) -> KSAnnotated?): Restorable? { +fun T.defer(restore: (T) -> KSAnnotated?): Restorable? { val ptr = analyze { with(this) { this@defer.createPointer() } } return Restorable { analyze { @@ -673,29 +682,29 @@ fun ClassId.toKSName() = KSNameImpl.getCached(asSingleFqName().toString()) // Only need to map java types if type declaration has origin of java // or kotlin functional type (in the case of Java functional type). -internal fun KtClassLikeSymbol.shouldMapToKotlinForAssignabilityCheck(): Boolean { - return this.origin == KtSymbolOrigin.JAVA_SOURCE || - this.origin == KtSymbolOrigin.JAVA_LIBRARY || - this.classIdIfNonLocal?.packageFqName?.asString() == "kotlin.jvm.functions" +internal fun KaClassLikeSymbol.shouldMapToKotlinForAssignabilityCheck(): Boolean { + return this.origin == KaSymbolOrigin.JAVA_SOURCE || + this.origin == KaSymbolOrigin.JAVA_LIBRARY || + this.classId?.packageFqName?.asString() == "kotlin.jvm.functions" } // recursively replace type & type argument to map java types into kotlin types. -internal fun KtType.convertToKotlinType(): KtType { - if (this !is KtNonErrorClassType) { +internal fun KaType.convertToKotlinType(): KaType { + if (this !is KaClassType) { return this } - val declaration = this.classifierSymbol() as? KtClassLikeSymbol ?: return this - if (declaration.classIdIfNonLocal == null) { + val declaration = this.classifierSymbol() as? KaClassLikeSymbol ?: return this + if (declaration.classId == null) { return this } - val base = if (declaration.classIdIfNonLocal != null && declaration.shouldMapToKotlinForAssignabilityCheck()) { - JavaToKotlinClassMap.mapJavaToKotlin(declaration.classIdIfNonLocal!!.asSingleFqName())?.toKtClassSymbol() + val base = if (declaration.classId != null && declaration.shouldMapToKotlinForAssignabilityCheck()) { + JavaToKotlinClassMap.mapJavaToKotlin(declaration.classId!!.asSingleFqName())?.toKtClassSymbol() ?: declaration } else declaration return analyze { buildClassType(base) { this@convertToKotlinType.typeArguments().forEach { typeProjection -> - if (typeProjection is KtTypeArgumentWithVariance) { + if (typeProjection is KaTypeArgumentWithVariance) { argument(typeProjection.type.convertToKotlinType(), typeProjection.variance) } else { argument(typeProjection) @@ -706,38 +715,39 @@ internal fun KtType.convertToKotlinType(): KtType { } } -internal fun KtType.isAssignableFrom(that: KtType): Boolean { - return if (that is KtFlexibleType) { +internal fun KaType.isAssignableFrom(that: KaType): Boolean { + return if (that is KaFlexibleType) { this.isAssignableFrom(that.upperBound) || this.isAssignableFrom(that.lowerBound) } else { analyze { - that.convertToKotlinType().isSubTypeOf(this@isAssignableFrom.convertToKotlinType()) + this@isAssignableFrom.convertToKotlinType() + that.convertToKotlinType().isSubtypeOf(this@isAssignableFrom.convertToKotlinType()) } } } // TODO: fix flexible type creation once upstream available. -internal fun KtType.replace(newArgs: List): KtType { +internal fun KaType.replace(newArgs: List): KaType { require(newArgs.isEmpty() || newArgs.size == this.typeArguments().size) return analyze { when (val symbol = classifierSymbol()) { - is KtClassLikeSymbol -> analysisSession.buildClassType(symbol) { + is KaClassLikeSymbol -> useSiteSession.buildClassType(symbol) { newArgs.forEach { arg -> argument(arg) } nullability = this@replace.nullability } // No need to copy nullability for type parameters // because it is overridden to be always nullable in compiler. - is KtTypeParameterSymbol -> analysisSession.buildTypeParameterType(symbol) + is KaTypeParameterSymbol -> useSiteSession.buildTypeParameterType(symbol) else -> throw IllegalStateException("Unexpected type ${this@replace}") } } } internal fun getVarianceForWildcard( - parameter: KtTypeParameterSymbol, - projection: KtTypeProjection, + parameter: KaTypeParameterSymbol, + projection: KaTypeProjection, mode: TypeMappingMode ): Variance { - val projectionKind = if (projection is KtTypeArgumentWithVariance) { + val projectionKind = if (projection is KaTypeArgumentWithVariance) { projection.variance } else { Variance.INVARIANT @@ -750,7 +760,7 @@ internal fun getVarianceForWildcard( return Variance.INVARIANT } if (projectionKind == Variance.INVARIANT || projectionKind == parameterVariance) { - if (mode.skipDeclarationSiteWildcardsIfPossible && projection !is KtStarTypeProjection) { + if (mode.skipDeclarationSiteWildcardsIfPossible && projection !is KaStarTypeProjection) { val coneType = (projection.type as KaFirType).coneType // TODO: fix most precise covariant argument case. if (parameterVariance == Variance.OUT_VARIANCE) { @@ -765,12 +775,13 @@ internal fun getVarianceForWildcard( return Variance.OUT_VARIANCE } -internal fun KtType.toWildcard(mode: TypeMappingMode): KtType { +@OptIn(KaExperimentalApi::class) +internal fun KaType.toWildcard(mode: TypeMappingMode): KtType { val parameters = this.classifierSymbol()?.typeParameters ?: emptyList() val args = this.typeArguments() return analyze { when (this@toWildcard) { - is KtNonErrorClassType -> { + is KaClassType -> { // TODO: missing annotations from original type. buildClassType(this@toWildcard.expandedSymbol!!) { parameters.zip(args).map { (param, arg) -> @@ -779,17 +790,17 @@ internal fun KtType.toWildcard(mode: TypeMappingMode): KtType { val genericMode = argMode.toGenericArgumentMode( getEffectiveVariance( param.variance, - (arg as? KtTypeArgumentWithVariance)?.variance ?: Variance.INVARIANT + (arg as? KaTypeArgumentWithVariance)?.variance ?: Variance.INVARIANT ) ) val argType = - arg.type ?: analysisSession.builtinTypes.ANY.withNullability(KtTypeNullability.NULLABLE) + arg.type ?: useSiteSession.builtinTypes.any.withNullability(KaTypeNullability.NULLABLE) argument(argType.toWildcard(genericMode), variance) } nullability = this@toWildcard.nullability } } - is KtTypeParameterType -> { + is KaTypeParameterType -> { buildTypeParameterType(this@toWildcard.symbol) } else -> throw IllegalStateException("Unexpected type ${this@toWildcard}") @@ -863,7 +874,7 @@ internal fun TypeMappingMode.updateFromAnnotations( } } -internal fun KaFunctionalType.abbreviatedSymbol(): KtTypeAliasSymbol? { +internal fun KaFunctionType.abbreviatedSymbol(): KaTypeAliasSymbol? { val classId = (this as? KaFirFunctionalType)?.coneType?.abbreviatedType?.classId ?: return null return classId.toTypeAlias() } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalJavaFileManager.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalJavaFileManager.kt index fed119ab34..2cf738d6d5 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalJavaFileManager.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalJavaFileManager.kt @@ -6,7 +6,7 @@ import com.intellij.openapi.roots.PackageIndex import com.intellij.psi.PsiJavaFile import com.intellij.psi.impl.file.impl.JavaFileManager import com.intellij.psi.search.ProjectScope -import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.StandaloneProjectFactory +import org.jetbrains.kotlin.analysis.api.standalone.base.projectStructure.StandaloneProjectFactory import org.jetbrains.kotlin.analysis.project.structure.KtModule import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalKotlinDeclarationProviderFactory.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalKotlinDeclarationProviderFactory.kt index 367a27e421..633f5b4006 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalKotlinDeclarationProviderFactory.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalKotlinDeclarationProviderFactory.kt @@ -14,7 +14,7 @@ import org.jetbrains.kotlin.psi.KtTypeAlias class IncrementalKotlinDeclarationProviderFactory( private val project: Project, -) : KotlinDeclarationProviderFactory() { +) : KotlinDeclarationProviderFactory { private val staticFactories: MutableList = mutableListOf() override fun createDeclarationProvider( diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalKotlinPackageProviderFactory.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalKotlinPackageProviderFactory.kt index 2241399942..935a8c3db1 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalKotlinPackageProviderFactory.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/IncrementalKotlinPackageProviderFactory.kt @@ -10,7 +10,7 @@ import org.jetbrains.kotlin.psi.KtFile class IncrementalKotlinPackageProviderFactory( private val project: Project, -) : KotlinPackageProviderFactory() { +) : KotlinPackageProviderFactory { private val staticFactories: MutableList = mutableListOf() override fun createPackageProvider(searchScope: GlobalSearchScope): KotlinPackageProvider { diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspLibraryModuleBuilder.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspLibraryModuleBuilder.kt index a372bf9f2b..30e6da1c31 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspLibraryModuleBuilder.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspLibraryModuleBuilder.kt @@ -19,12 +19,12 @@ package com.google.devtools.ksp.standalone import com.intellij.psi.search.ProjectScope -import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule -import org.jetbrains.kotlin.analysis.project.structure.KtLibrarySourceModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaLibraryModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaLibrarySourceModule import org.jetbrains.kotlin.analysis.project.structure.builder.KtBinaryModuleBuilder import org.jetbrains.kotlin.analysis.project.structure.builder.KtModuleBuilderDsl import org.jetbrains.kotlin.analysis.project.structure.builder.KtModuleProviderBuilder -import org.jetbrains.kotlin.analysis.project.structure.impl.KtLibraryModuleImpl +import org.jetbrains.kotlin.analysis.project.structure.impl.KaLibraryModuleImpl import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreProjectEnvironment import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind @@ -35,12 +35,12 @@ class KspLibraryModuleBuilder( private val kotlinCoreProjectEnvironment: KotlinCoreProjectEnvironment ) : KtBinaryModuleBuilder() { public lateinit var libraryName: String - public var librarySources: KtLibrarySourceModule? = null + public var librarySources: KaLibrarySourceModule? = null - override fun build(): KtLibraryModule { + override fun build(): KaLibraryModule { val binaryRoots = getBinaryRoots() val contentScope = ProjectScope.getLibrariesScope(kotlinCoreProjectEnvironment.project) - return KtLibraryModuleImpl( + return KaLibraryModuleImpl( directRegularDependencies, directDependsOnDependencies, directFriendDependencies, @@ -50,12 +50,13 @@ class KspLibraryModuleBuilder( binaryRoots, libraryName, librarySources, + false ) } } @OptIn(ExperimentalContracts::class) -inline fun KtModuleProviderBuilder.buildKspLibraryModule(init: KspLibraryModuleBuilder.() -> Unit): KtLibraryModule { +inline fun KtModuleProviderBuilder.buildKspLibraryModule(init: KspLibraryModuleBuilder.() -> Unit): KaLibraryModule { contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspSourceModuleBuilder.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspSourceModuleBuilder.kt index 5b75d55151..f6de866431 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspSourceModuleBuilder.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspSourceModuleBuilder.kt @@ -23,11 +23,11 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiManager import com.intellij.psi.search.GlobalSearchScope -import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaSourceModule import org.jetbrains.kotlin.analysis.project.structure.builder.KtModuleBuilder import org.jetbrains.kotlin.analysis.project.structure.builder.KtModuleBuilderDsl import org.jetbrains.kotlin.analysis.project.structure.builder.KtModuleProviderBuilder -import org.jetbrains.kotlin.analysis.project.structure.impl.KtSourceModuleImpl +import org.jetbrains.kotlin.analysis.project.structure.impl.KaSourceModuleImpl import org.jetbrains.kotlin.analysis.project.structure.impl.collectSourceFilePaths import org.jetbrains.kotlin.analysis.project.structure.impl.hasSuitableExtensionToAnalyse import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreProjectEnvironment @@ -59,12 +59,12 @@ class KspModuleBuilder( sourceRoots.addAll(paths) } - override fun build(): KtSourceModule { + override fun build(): KaSourceModule { val virtualFiles = collectVirtualFilesByRoots() val psiManager = PsiManager.getInstance(kotlinCoreProjectEnvironment.project) val psiFiles = virtualFiles.mapNotNull { psiManager.findFile(it) } val contentScope = IncrementalGlobalSearchScope(kotlinCoreProjectEnvironment.project, virtualFiles) - return KtSourceModuleImpl( + return KaSourceModuleImpl( directRegularDependencies, directDependsOnDependencies, directFriendDependencies, @@ -114,7 +114,7 @@ class IncrementalGlobalSearchScope( @OptIn(ExperimentalContracts::class) public inline fun KtModuleProviderBuilder.buildKspSourceModule( init: KspModuleBuilder.() -> Unit -): KtSourceModule { +): KaSourceModule { contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspStandaloneDirectInheritorsProvider.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspStandaloneDirectInheritorsProvider.kt index eeef6fb485..3cfee6a4b1 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspStandaloneDirectInheritorsProvider.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspStandaloneDirectInheritorsProvider.kt @@ -2,15 +2,17 @@ package com.google.devtools.ksp.standalone import com.intellij.openapi.project.Project import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.analysis.api.KaImplementationDetail +import org.jetbrains.kotlin.analysis.api.KaPlatformInterface import org.jetbrains.kotlin.analysis.api.fir.utils.isSubClassOf import org.jetbrains.kotlin.analysis.api.platform.declarations.KotlinDeclarationProviderFactory import org.jetbrains.kotlin.analysis.api.platform.declarations.KotlinDirectInheritorsProvider +import org.jetbrains.kotlin.analysis.api.projectStructure.KaDanglingFileModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaModuleProvider import org.jetbrains.kotlin.analysis.api.standalone.base.declarations.KotlinStandaloneDeclarationProviderFactory import org.jetbrains.kotlin.analysis.low.level.api.fir.LLFirInternals import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirSessionCache -import org.jetbrains.kotlin.analysis.project.structure.KtDanglingFileModule -import org.jetbrains.kotlin.analysis.project.structure.KtModule -import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider import org.jetbrains.kotlin.fir.declarations.FirClass import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider import org.jetbrains.kotlin.fir.symbols.SymbolInternals @@ -33,6 +35,7 @@ class KspStandaloneDirectInheritorsProvider(private val project: Project) : Kotl ) } + @OptIn(KaPlatformInterface::class) override fun getDirectKotlinInheritors( ktClass: KtClass, scope: GlobalSearchScope, @@ -58,9 +61,9 @@ class KspStandaloneDirectInheritorsProvider(private val project: Project) : Kotl // Note that this means we don't support providing inheritors based on the dangling file yet, for example if an inheritor was added // or removed only in the dangling file. val baseKtModule = when ( - val ktModule = ProjectStructureProvider.getModule(project, ktClass, contextualModule = null) + val ktModule = KaModuleProvider.getModule(project, ktClass, useSiteModule = null) ) { - is KtDanglingFileModule -> ktModule.contextModule + is KaDanglingFileModule -> ktModule.contextModule else -> ktModule } @@ -78,6 +81,7 @@ class KspStandaloneDirectInheritorsProvider(private val project: Project) : Kotl } } + @OptIn(KaImplementationDetail::class) private fun isValidInheritor( candidate: KtClassOrObject, baseFirClass: FirClass, @@ -93,7 +97,7 @@ class KspStandaloneDirectInheritorsProvider(private val project: Project) : Kotl } val candidateClassId = candidate.getClassId() ?: return false - val candidateKtModule = ProjectStructureProvider.getModule(project, candidate, contextualModule = null) + val candidateKtModule = KaModuleProvider.getModule(project, candidate, useSiteModule = null) val candidateFirSymbol = candidate.toFirSymbol(candidateClassId, candidateKtModule) ?: return false val candidateFirClass = candidateFirSymbol.fir as? FirClass ?: return false @@ -105,7 +109,7 @@ class KspStandaloneDirectInheritorsProvider(private val project: Project) : Kotl ) } - private fun KtClassOrObject.toFirSymbol(classId: ClassId, ktModule: KtModule): FirClassLikeSymbol<*>? { + private fun KtClassOrObject.toFirSymbol(classId: ClassId, ktModule: KaModule): FirClassLikeSymbol<*>? { val session = LLFirSessionCache.getInstance(project).getSession(ktModule, preferBinary = true) return session.symbolProvider.getClassLikeSymbolByClassId(classId) }