Skip to content

Commit

Permalink
[JVM] Retrieve module names using experimental KA API for code fragments
Browse files Browse the repository at this point in the history
At the moment, it's the only way to get actual module name for the
declaration when compiling code fragments from the IDEA debugger.
See KT-69226.

^KT-72500: Fixed
  • Loading branch information
grechkovlad authored and qodana-bot committed Dec 13, 2024
1 parent c30f087 commit 87b9ce0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ public interface KaModule {
*/
@KaExperimentalApi
public val moduleDescription: String
}

/**
* A module which consists of a set of source declarations inside a project.
*
* Generally, a main or test Source Set.
*/
public interface KaSourceModule : KaModule {
public val name: String

/**
* A stable binary name of module from the *Kotlin* point of view.
Expand All @@ -102,6 +93,15 @@ public interface KaSourceModule : KaModule {
@KaExperimentalApi
public val stableModuleName: String?
get() = null
}

/**
* A module which consists of a set of source declarations inside a project.
*
* Generally, a main or test Source Set.
*/
public interface KaSourceModule : KaModule {
public val name: String

@KaExperimentalApi
override val moduleDescription: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class LLFirModuleData private constructor(val ktModule: KaModule) : FirModuleDat
get() = boundSession?.let { it as LLFirSession }
?: LLFirSessionCache.getInstance(ktModule.project).getSession(ktModule, preferBinary = true)

override val stableModuleName: String?
get() = ktModule.stableModuleName

override fun equals(other: Any?): Boolean = this === other || other is LLFirModuleData && ktModule == other.ktModule
override fun hashCode(): Int = ktModule.hashCode()
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,14 @@ class Fir2IrLazyClass(
set(_) = error("We should never need to store metadata of external declarations.")

override val moduleName: String?
get() = fir.moduleName
get() {
fir.moduleName?.let { return it }
val moduleNameFromModuleData = fir.moduleData.stableModuleName ?: return null
require(moduleNameFromModuleData.startsWith("<") && moduleNameFromModuleData.endsWith(">")) {
"Stable module name is expected to be wrapped in '<>' brackets, but got `$moduleNameFromModuleData` instead"
}
return moduleNameFromModuleData.substring(1, moduleNameFromModuleData.length - 1)
}

override val isNewPlaceForBodyGeneration: Boolean
get() = fir.isNewPlaceForBodyGeneration == true
Expand Down
11 changes: 11 additions & 0 deletions compiler/fir/tree/src/org/jetbrains/kotlin/fir/FirModuleData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ abstract class FirModuleData : FirSessionComponent {
override fun toString(): String {
return "Module $name"
}

/**
* For the Analysis API-originated modules, returns the stable module name of the corresponding KaModule.
* See [org.jetbrains.kotlin.analysis.api.projectStructure.KaModule.stableModuleName]
*
* For others, returns null.
*/
abstract val stableModuleName: String?
}

class FirModuleDataImpl(
Expand All @@ -87,6 +95,9 @@ class FirModuleDataImpl(
get() = boundSession
?: error("module data ${this::class.simpleName}:${name} not bound to session")

override val stableModuleName: String?
get() = null

override val allDependsOnDependencies: List<FirModuleData> = topologicalSort(dependsOnDependencies) { it.dependsOnDependencies }
}

Expand Down

0 comments on commit 87b9ce0

Please sign in to comment.