Skip to content

Commit 4a7ec1b

Browse files
committed
Kotlin: Support compiler API changes in 2.5.0
Kotlin 2.5 removed `JvmCodegenUtil` and `IrPluginContext.moduleDescriptor`, and `IrExternalPackageFragmentImpl` now needs an `IrModuleFragment`. Add two compatibility functions, with the previous behaviour in v_1_8_0 and the new one in v_2_5_0: - getCurrentJvmModuleName, used by KotlinUsesExtractor.getJvmModuleName for the name of the module being compiled. For 2.5 it applies the old JvmCodegenUtil.getModuleName logic to the module fragment's descriptor. KotlinExtractorGlobalState now holds the module fragment to support this. - createExternalPackageFragment, used to make the synthetic `kotlin.internal.ir` package for RawTypeAnnotation. For 2.5 it passes IrErrorModuleFragment, as the compiler's own JvmIrSpecialAnnotationSymbolProvider does. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 38400d2f-9060-4b63-8536-73ce77bb5c91 X
1 parent bdc58ee commit 4a7ec1b

7 files changed

Lines changed: 62 additions & 6 deletions

File tree

‎java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class KotlinExtractorExtension(
164164
} ?: "kotlin-extractor/src"
165165
)
166166
srcDir.mkdirs()
167-
val globalExtensionState = KotlinExtractorGlobalState()
167+
val globalExtensionState = KotlinExtractorGlobalState(moduleFragment)
168168
moduleFragment.files.mapIndexed { index: Int, file: IrFile ->
169169
val fileExtractionProblems = FileExtractionProblems(invocationExtractionProblems)
170170
val fileTrapWriter = tw.makeSourceFileTrapWriter(file, true)
@@ -264,7 +264,7 @@ class KotlinExtractorExtension(
264264
}
265265
}
266266

267-
class KotlinExtractorGlobalState {
267+
class KotlinExtractorGlobalState(val moduleFragment: IrModuleFragment) {
268268
// These three record mappings of classes, functions and fields that should be replaced wherever
269269
// they are found.
270270
// As of now these are only used to fix IR generated by the Gradle Android Extensions plugin,

‎java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.semmle.extractor.java.OdasaOutput
66
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
77
import org.jetbrains.kotlin.backend.common.ir.*
88
import org.jetbrains.kotlin.backend.jvm.ir.*
9-
import org.jetbrains.kotlin.codegen.JvmCodegenUtil
109
import org.jetbrains.kotlin.descriptors.*
1110
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
1211
import org.jetbrains.kotlin.ir.declarations.*
@@ -1055,7 +1054,7 @@ open class KotlinUsesExtractor(
10551054
private fun getJvmModuleName(f: IrFunction) =
10561055
NameUtils.sanitizeAsJavaIdentifier(
10571056
getJvmModuleNameForDeserializedDescriptor(f.descriptor)
1058-
?: JvmCodegenUtil.getModuleName(pluginContext.moduleDescriptor)
1057+
?: getCurrentJvmModuleName(pluginContext, globalExtensionState.moduleFragment)
10591058
)
10601059

10611060
fun getFunctionShortName(f: IrFunction): FunctionNames {

‎java/kotlin-extractor/src/main/kotlin/utils/TypeSubstitution.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.ir.builders.declarations.buildClass
1212
import org.jetbrains.kotlin.ir.declarations.IrClass
1313
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
1414
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
15-
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
1615
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
1716
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
1817
import org.jetbrains.kotlin.ir.expressions.impl.*
@@ -177,7 +176,7 @@ object RawTypeAnnotation {
177176
val annotationConstructor: IrConstructorCall by lazy {
178177
val irInternalPackage = FqName("kotlin.internal.ir")
179178
val parent =
180-
IrExternalPackageFragmentImpl(
179+
createExternalPackageFragment(
181180
DescriptorlessExternalPackageFragmentSymbol(),
182181
irInternalPackage
183182
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
4+
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
5+
import org.jetbrains.kotlin.ir.symbols.IrExternalPackageFragmentSymbol
6+
import org.jetbrains.kotlin.name.FqName
7+
8+
fun createExternalPackageFragment(
9+
symbol: IrExternalPackageFragmentSymbol,
10+
fqName: FqName
11+
): IrExternalPackageFragment = IrExternalPackageFragmentImpl(symbol, fqName)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
4+
import org.jetbrains.kotlin.codegen.JvmCodegenUtil
5+
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
6+
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
7+
8+
@OptIn(ObsoleteDescriptorBasedAPI::class)
9+
@Suppress("UNUSED_PARAMETER")
10+
fun getCurrentJvmModuleName(
11+
pluginContext: IrPluginContext,
12+
moduleFragment: IrModuleFragment
13+
): String =
14+
JvmCodegenUtil.getModuleName(pluginContext.moduleDescriptor)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
4+
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
5+
import org.jetbrains.kotlin.ir.symbols.IrExternalPackageFragmentSymbol
6+
import org.jetbrains.kotlin.ir.util.IrErrorModuleFragment
7+
import org.jetbrains.kotlin.name.FqName
8+
9+
// The fragment now needs a module. As in the compiler's own
10+
// `JvmIrSpecialAnnotationSymbolProvider`, a placeholder module is sufficient for the synthetic
11+
// packages we create.
12+
fun createExternalPackageFragment(
13+
symbol: IrExternalPackageFragmentSymbol,
14+
fqName: FqName
15+
): IrExternalPackageFragment =
16+
IrExternalPackageFragmentImpl(symbol, fqName, IrErrorModuleFragment)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
4+
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
5+
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
6+
7+
// `JvmCodegenUtil` and `IrPluginContext.moduleDescriptor` no longer exist, so this mirrors what
8+
// `JvmCodegenUtil.getModuleName(pluginContext.moduleDescriptor)` used to do.
9+
@OptIn(ObsoleteDescriptorBasedAPI::class)
10+
@Suppress("UNUSED_PARAMETER")
11+
fun getCurrentJvmModuleName(
12+
pluginContext: IrPluginContext,
13+
moduleFragment: IrModuleFragment
14+
): String {
15+
val module = moduleFragment.descriptor
16+
return (module.stableName ?: module.name).asString().removeSurrounding("<", ">")
17+
}

0 commit comments

Comments
 (0)