From 50269bf1272d9a033dc65eab243ac13c3c6dd96c Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Tue, 20 Aug 2024 14:11:14 +0200 Subject: [PATCH] [Wasm] Prepare k2 ide for wasm ^KT-64984 --- ...inaryOriginLibrarySymbolProviderFactory.kt | 15 +++ .../LLLibrarySymbolProviderFactory.kt | 9 ++ .../api/fir/sessions/LLFirSessionCache.kt | 2 + .../fir/sessions/LLFirWasmSessionFactory.kt | 98 +++++++++++++++++++ ...LLStubBasedLibrarySymbolProviderFactory.kt | 20 ++++ .../cli/common/FirSessionConstructionUtils.kt | 1 + .../fir/session/FirWasmSessionFactory.kt | 25 +++-- .../frontend/fir/TestFirWasmSessionFactory.kt | 2 + 8 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/sessions/LLFirWasmSessionFactory.kt diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/projectStructure/LLBinaryOriginLibrarySymbolProviderFactory.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/projectStructure/LLBinaryOriginLibrarySymbolProviderFactory.kt index 0fa2803096cb7..edc3668db3018 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/projectStructure/LLBinaryOriginLibrarySymbolProviderFactory.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/projectStructure/LLBinaryOriginLibrarySymbolProviderFactory.kt @@ -123,6 +123,21 @@ class LLBinaryOriginLibrarySymbolProviderFactory(private val project: Project) : ) } + override fun createWasmLibrarySymbolProvider( + session: FirSession, + moduleData: LLFirModuleData, + kotlinScopeProvider: FirKotlinScopeProvider, + moduleDataProvider: SingleModuleDataProvider, + scope: GlobalSearchScope, + isFallbackDependenciesProvider: Boolean, + ): List { + val kLibs = moduleData.getLibraryKLibs() + + return listOf( + KlibBasedSymbolProvider(session, moduleDataProvider, kotlinScopeProvider, kLibs) + ) + } + override fun createBuiltinsSymbolProvider( session: FirSession, moduleData: LLFirModuleData, diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/projectStructure/LLLibrarySymbolProviderFactory.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/projectStructure/LLLibrarySymbolProviderFactory.kt index 834e70c83fae3..121c52bda28c3 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/projectStructure/LLLibrarySymbolProviderFactory.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/projectStructure/LLLibrarySymbolProviderFactory.kt @@ -61,6 +61,15 @@ internal interface LLLibrarySymbolProviderFactory { isFallbackDependenciesProvider: Boolean, ): List + fun createWasmLibrarySymbolProvider( + session: FirSession, + moduleData: LLFirModuleData, + kotlinScopeProvider: FirKotlinScopeProvider, + moduleDataProvider: SingleModuleDataProvider, + scope: GlobalSearchScope, + isFallbackDependenciesProvider: Boolean, + ): List + fun createBuiltinsSymbolProvider( session: FirSession, moduleData: LLFirModuleData, diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/sessions/LLFirSessionCache.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/sessions/LLFirSessionCache.kt index 57e488633bc1a..547d0baf43644 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/sessions/LLFirSessionCache.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/sessions/LLFirSessionCache.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.fir.PrivateSessionConstructor import org.jetbrains.kotlin.fir.session.registerModuleData import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.platform.JsPlatform +import org.jetbrains.kotlin.platform.WasmPlatform import org.jetbrains.kotlin.platform.jvm.JvmPlatform import org.jetbrains.kotlin.platform.jvm.JvmPlatforms import org.jetbrains.kotlin.platform.konan.NativePlatform @@ -255,6 +256,7 @@ class LLFirSessionCache(private val project: Project) : Disposable { return when { targetPlatform.all { it is JvmPlatform } -> LLFirJvmSessionFactory(project) targetPlatform.all { it is JsPlatform } -> LLFirJsSessionFactory(project) + targetPlatform.all { it is WasmPlatform } -> LLFirWasmSessionFactory(project) targetPlatform.all { it is NativePlatform } -> LLFirNativeSessionFactory(project) else -> LLFirCommonSessionFactory(project) } diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/sessions/LLFirWasmSessionFactory.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/sessions/LLFirWasmSessionFactory.kt new file mode 100644 index 0000000000000..e308022a0b1ef --- /dev/null +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/sessions/LLFirWasmSessionFactory.kt @@ -0,0 +1,98 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.analysis.low.level.api.fir.sessions + +import com.intellij.openapi.project.Project +import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.analysis.low.level.api.fir.projectStructure.LLLibrarySymbolProviderFactory +import org.jetbrains.kotlin.analysis.low.level.api.fir.projectStructure.LLFirModuleData +import org.jetbrains.kotlin.analysis.low.level.api.fir.providers.LLFirModuleWithDependenciesSymbolProvider +import org.jetbrains.kotlin.analysis.api.projectStructure.KaDanglingFileModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaLibraryModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaSourceModule +import org.jetbrains.kotlin.fir.BuiltinTypes +import org.jetbrains.kotlin.fir.SessionConfiguration +import org.jetbrains.kotlin.fir.deserialization.SingleModuleDataProvider +import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolProvider +import org.jetbrains.kotlin.fir.scopes.FirKotlinScopeProvider + +@OptIn(SessionConfiguration::class) +internal class LLFirWasmSessionFactory(project: Project) : LLFirAbstractSessionFactory(project) { + override fun createSourcesSession(module: KaSourceModule): LLFirSourcesSession { + return doCreateSourcesSession(module) { context -> + register( + FirSymbolProvider::class, + LLFirModuleWithDependenciesSymbolProvider( + this, + providers = listOfNotNull( + context.firProvider.symbolProvider, + context.switchableExtensionDeclarationsSymbolProvider, + context.syntheticFunctionInterfaceProvider, + ), + context.dependencyProvider, + ) + ) + } + } + + override fun createLibrarySession(module: KaModule): LLFirLibraryOrLibrarySourceResolvableModuleSession { + return doCreateLibrarySession(module) { context -> + register( + FirSymbolProvider::class, + LLFirModuleWithDependenciesSymbolProvider( + this, + providers = listOf( + context.firProvider.symbolProvider, + ), + context.dependencyProvider, + ) + ) + } + } + + override fun createBinaryLibrarySession(module: KaLibraryModule): LLFirLibrarySession { + return doCreateBinaryLibrarySession(module) { + } + } + + override fun createDanglingFileSession(module: KaDanglingFileModule, contextSession: LLFirSession): LLFirSession { + return doCreateDanglingFileSession(module, contextSession) { + register( + FirSymbolProvider::class, + LLFirModuleWithDependenciesSymbolProvider( + this, + providers = listOfNotNull( + firProvider.symbolProvider, + switchableExtensionDeclarationsSymbolProvider, + syntheticFunctionInterfaceProvider, + ), + dependencyProvider, + ) + ) + } + } + + override fun createProjectLibraryProvidersForScope( + session: LLFirSession, + moduleData: LLFirModuleData, + kotlinScopeProvider: FirKotlinScopeProvider, + project: Project, + builtinTypes: BuiltinTypes, + scope: GlobalSearchScope, + isFallbackDependenciesProvider: Boolean, + ): List { + val moduleDataProvider = SingleModuleDataProvider(moduleData) + return LLLibrarySymbolProviderFactory.fromSettings(project).createWasmLibrarySymbolProvider( + session, + moduleData, + kotlinScopeProvider, + moduleDataProvider, + scope, + isFallbackDependenciesProvider, + ) + } +} diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/stubBased/deserialization/LLStubBasedLibrarySymbolProviderFactory.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/stubBased/deserialization/LLStubBasedLibrarySymbolProviderFactory.kt index 83e4d15a88e2a..3b6a1c72cb032 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/stubBased/deserialization/LLStubBasedLibrarySymbolProviderFactory.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/stubBased/deserialization/LLStubBasedLibrarySymbolProviderFactory.kt @@ -125,6 +125,26 @@ internal class LLStubBasedLibrarySymbolProviderFactory(private val project: Proj ) } + override fun createWasmLibrarySymbolProvider( + session: FirSession, + moduleData: LLFirModuleData, + kotlinScopeProvider: FirKotlinScopeProvider, + moduleDataProvider: SingleModuleDataProvider, + scope: GlobalSearchScope, + isFallbackDependenciesProvider: Boolean, + ): List { + return listOf( + createStubBasedFirSymbolProviderForKotlinNativeMetadataFiles( + project, + scope, + session, + moduleDataProvider, + kotlinScopeProvider, + isFallbackDependenciesProvider, + ), + ) + } + override fun createBuiltinsSymbolProvider( session: FirSession, moduleData: LLFirModuleData, diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/FirSessionConstructionUtils.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/common/FirSessionConstructionUtils.kt index 0818bbec01419..e493cc7ec52fa 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/FirSessionConstructionUtils.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/FirSessionConstructionUtils.kt @@ -241,6 +241,7 @@ fun prepareWasmSessions( libraryList.moduleDataProvider, extensionRegistrars, configuration.languageVersionSettings, + configuration.wasmTarget, ) } ) { _, moduleData, sessionProvider, sessionConfigurator -> diff --git a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/FirWasmSessionFactory.kt b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/FirWasmSessionFactory.kt index bd45657a61ccd..0dd12547b8c08 100644 --- a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/FirWasmSessionFactory.kt +++ b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/FirWasmSessionFactory.kt @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.wasm.resolve.WasmPlatformAnalyzerServices import org.jetbrains.kotlin.wasm.resolve.WasmWasiPlatformAnalyzerServices @OptIn(SessionConfiguration::class) -object FirWasmSessionFactory : FirAbstractSessionFactory() { +object FirWasmSessionFactory : FirAbstractSessionFactory() { // ==================================== Library session ==================================== @@ -37,9 +37,10 @@ object FirWasmSessionFactory : FirAbstractSessionFactory, languageVersionSettings: LanguageVersionSettings = LanguageVersionSettingsImpl.DEFAULT, + wasmTarget: WasmTarget, ): FirSession = createLibrarySession( mainModuleName, - context = null, + Context(wasmTarget), sessionProvider, moduleDataProvider, languageVersionSettings, @@ -57,8 +58,9 @@ object FirWasmSessionFactory : FirAbstractSessionFactory declaredMemberScope } } - override fun FirSession.registerLibrarySessionComponents(c: Nothing?) { + override fun FirSession.registerLibrarySessionComponents(c: FirWasmSessionFactory.Context) { registerDefaultComponents() + registerWasmComponents(c.wasmTarget) } // ==================================== Platform session ==================================== @@ -73,10 +75,9 @@ object FirWasmSessionFactory : FirAbstractSessionFactory Unit ): FirSession { - val context = Context(wasmTarget) return createModuleBasedSession( moduleData, - context, + Context(wasmTarget), sessionProvider, extensionRegistrars, languageVersionSettings, @@ -115,10 +116,16 @@ object FirWasmSessionFactory : FirAbstractSessionFactory WasmPlatformAnalyzerServices + WasmTarget.WASI -> WasmWasiPlatformAnalyzerServices + } + register(FirDefaultImportProviderHolder::class, FirDefaultImportProviderHolder(analyzerServices)) } // ==================================== Common parts ==================================== diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/TestFirWasmSessionFactory.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/TestFirWasmSessionFactory.kt index 81a5d29372547..2eb44b1433278 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/TestFirWasmSessionFactory.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/TestFirWasmSessionFactory.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.test.model.TestModule import org.jetbrains.kotlin.test.services.TestServices import org.jetbrains.kotlin.test.services.configuration.WasmEnvironmentConfigurator import org.jetbrains.kotlin.wasm.config.WasmConfigurationKeys +import org.jetbrains.kotlin.wasm.config.wasmTarget import java.io.File object TestFirWasmSessionFactory { @@ -49,6 +50,7 @@ object TestFirWasmSessionFactory { moduleDataProvider, extensionRegistrars, languageVersionSettings, + configuration.wasmTarget, ) }