Skip to content

Commit

Permalink
[Wasm] Prepare k2 ide for wasm
Browse files Browse the repository at this point in the history
^KT-64984
  • Loading branch information
ilgonmic authored and qodana-bot committed Sep 5, 2024
1 parent 2608d3a commit 50269bf
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<FirSymbolProvider> {
val kLibs = moduleData.getLibraryKLibs()

return listOf(
KlibBasedSymbolProvider(session, moduleDataProvider, kotlinScopeProvider, kLibs)
)
}

override fun createBuiltinsSymbolProvider(
session: FirSession,
moduleData: LLFirModuleData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ internal interface LLLibrarySymbolProviderFactory {
isFallbackDependenciesProvider: Boolean,
): List<FirSymbolProvider>

fun createWasmLibrarySymbolProvider(
session: FirSession,
moduleData: LLFirModuleData,
kotlinScopeProvider: FirKotlinScopeProvider,
moduleDataProvider: SingleModuleDataProvider,
scope: GlobalSearchScope,
isFallbackDependenciesProvider: Boolean,
): List<FirSymbolProvider>

fun createBuiltinsSymbolProvider(
session: FirSession,
moduleData: LLFirModuleData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FirSymbolProvider> {
val moduleDataProvider = SingleModuleDataProvider(moduleData)
return LLLibrarySymbolProviderFactory.fromSettings(project).createWasmLibrarySymbolProvider(
session,
moduleData,
kotlinScopeProvider,
moduleDataProvider,
scope,
isFallbackDependenciesProvider,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<FirSymbolProvider> {
return listOf(
createStubBasedFirSymbolProviderForKotlinNativeMetadataFiles(
project,
scope,
session,
moduleDataProvider,
kotlinScopeProvider,
isFallbackDependenciesProvider,
),
)
}

override fun createBuiltinsSymbolProvider(
session: FirSession,
moduleData: LLFirModuleData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ fun <F> prepareWasmSessions(
libraryList.moduleDataProvider,
extensionRegistrars,
configuration.languageVersionSettings,
configuration.wasmTarget,
)
}
) { _, moduleData, sessionProvider, sessionConfigurator ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.wasm.resolve.WasmPlatformAnalyzerServices
import org.jetbrains.kotlin.wasm.resolve.WasmWasiPlatformAnalyzerServices

@OptIn(SessionConfiguration::class)
object FirWasmSessionFactory : FirAbstractSessionFactory<Nothing?, FirWasmSessionFactory.Context>() {
object FirWasmSessionFactory : FirAbstractSessionFactory<FirWasmSessionFactory.Context, FirWasmSessionFactory.Context>() {

// ==================================== Library session ====================================

Expand All @@ -37,9 +37,10 @@ object FirWasmSessionFactory : FirAbstractSessionFactory<Nothing?, FirWasmSessio
moduleDataProvider: ModuleDataProvider,
extensionRegistrars: List<FirExtensionRegistrar>,
languageVersionSettings: LanguageVersionSettings = LanguageVersionSettingsImpl.DEFAULT,
wasmTarget: WasmTarget,
): FirSession = createLibrarySession(
mainModuleName,
context = null,
Context(wasmTarget),
sessionProvider,
moduleDataProvider,
languageVersionSettings,
Expand All @@ -57,8 +58,9 @@ object FirWasmSessionFactory : FirAbstractSessionFactory<Nothing?, FirWasmSessio
return FirKotlinScopeProvider { _, declaredMemberScope, _, _, _ -> declaredMemberScope }
}

override fun FirSession.registerLibrarySessionComponents(c: Nothing?) {
override fun FirSession.registerLibrarySessionComponents(c: FirWasmSessionFactory.Context) {
registerDefaultComponents()
registerWasmComponents(c.wasmTarget)
}

// ==================================== Platform session ====================================
Expand All @@ -73,10 +75,9 @@ object FirWasmSessionFactory : FirAbstractSessionFactory<Nothing?, FirWasmSessio
icData: KlibIcData? = null,
init: FirSessionConfigurator.() -> Unit
): FirSession {
val context = Context(wasmTarget)
return createModuleBasedSession(
moduleData,
context,
Context(wasmTarget),
sessionProvider,
extensionRegistrars,
languageVersionSettings,
Expand Down Expand Up @@ -115,10 +116,16 @@ object FirWasmSessionFactory : FirAbstractSessionFactory<Nothing?, FirWasmSessio

override fun FirSession.registerSourceSessionComponents(c: Context) {
registerDefaultComponents()
register(
FirDefaultImportProviderHolder::class,
FirDefaultImportProviderHolder(if (c.wasmTarget == WasmTarget.JS) WasmPlatformAnalyzerServices else WasmWasiPlatformAnalyzerServices)
)
registerWasmComponents(c.wasmTarget)
}

@OptIn(SessionConfiguration::class)
fun FirSession.registerWasmComponents(wasmTarget: WasmTarget) {
val analyzerServices = when (wasmTarget) {
WasmTarget.JS -> WasmPlatformAnalyzerServices
WasmTarget.WASI -> WasmWasiPlatformAnalyzerServices
}
register(FirDefaultImportProviderHolder::class, FirDefaultImportProviderHolder(analyzerServices))
}

// ==================================== Common parts ====================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -49,6 +50,7 @@ object TestFirWasmSessionFactory {
moduleDataProvider,
extensionRegistrars,
languageVersionSettings,
configuration.wasmTarget,
)
}

Expand Down

0 comments on commit 50269bf

Please sign in to comment.