Skip to content

Commit 45c8522

Browse files
committed
Implement Kotlin 1.7 support
- Implement getIrStubFromDescriptor for Kotlin 1.7 - Stop using ClassSymbol.signature, which is now only populated for classes built from Kotlin, and noteworthily is null for primitive and other internally-synthesised types.
1 parent 43d449f commit 45c8522

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

java/kotlin-extractor/src/main/kotlin/PrimitiveTypeInfo.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package com.github.codeql
22

33
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
4+
import org.jetbrains.kotlin.builtins.StandardNames
45
import org.jetbrains.kotlin.ir.declarations.IrClass
6+
import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
57
import org.jetbrains.kotlin.ir.types.IrSimpleType
6-
import org.jetbrains.kotlin.ir.types.IdSignatureValues
7-
import org.jetbrains.kotlin.ir.util.IdSignature
8+
import org.jetbrains.kotlin.ir.types.classOrNull
89
import org.jetbrains.kotlin.name.FqName
910

1011
class PrimitiveTypeMapping(val logger: Logger, val pluginContext: IrPluginContext) {
11-
fun getPrimitiveInfo(s: IrSimpleType) = mapping[s.classifier.signature]
12+
fun getPrimitiveInfo(s: IrSimpleType) =
13+
s.classOrNull?.let {
14+
if ((it.owner.parent as? IrPackageFragment)?.fqName == StandardNames.BUILT_INS_PACKAGE_FQ_NAME)
15+
mapping[it.owner.name]
16+
else
17+
null
18+
}
1219

1320
data class PrimitiveTypeInfo(
1421
val primitiveName: String?,
@@ -60,25 +67,25 @@ class PrimitiveTypeMapping(val logger: Logger, val pluginContext: IrPluginContex
6067
val javaLangVoid = findClass("java.lang.Void", kotlinNothing)
6168

6269
mapOf(
63-
IdSignatureValues._byte to PrimitiveTypeInfo("byte", true, javaLangByte, "kotlin", "Byte"),
64-
IdSignatureValues._short to PrimitiveTypeInfo("short", true, javaLangShort, "kotlin", "Short"),
65-
IdSignatureValues._int to PrimitiveTypeInfo("int", true, javaLangInteger, "kotlin", "Int"),
66-
IdSignatureValues._long to PrimitiveTypeInfo("long", true, javaLangLong, "kotlin", "Long"),
70+
StandardNames.FqNames._byte.shortName() to PrimitiveTypeInfo("byte", true, javaLangByte, "kotlin", "Byte"),
71+
StandardNames.FqNames._short.shortName() to PrimitiveTypeInfo("short", true, javaLangShort, "kotlin", "Short"),
72+
StandardNames.FqNames._int.shortName() to PrimitiveTypeInfo("int", true, javaLangInteger, "kotlin", "Int"),
73+
StandardNames.FqNames._long.shortName() to PrimitiveTypeInfo("long", true, javaLangLong, "kotlin", "Long"),
6774

68-
IdSignatureValues.uByte to PrimitiveTypeInfo("byte", true, kotlinUByte, "kotlin", "UByte"),
69-
IdSignatureValues.uShort to PrimitiveTypeInfo("short", true, kotlinUShort, "kotlin", "UShort"),
70-
IdSignatureValues.uInt to PrimitiveTypeInfo("int", true, kotlinUInt, "kotlin", "UInt"),
71-
IdSignatureValues.uLong to PrimitiveTypeInfo("long", true, kotlinULong, "kotlin", "ULong"),
75+
StandardNames.FqNames.uByteFqName.shortName() to PrimitiveTypeInfo("byte", true, kotlinUByte, "kotlin", "UByte"),
76+
StandardNames.FqNames.uShortFqName.shortName() to PrimitiveTypeInfo("short", true, kotlinUShort, "kotlin", "UShort"),
77+
StandardNames.FqNames.uIntFqName.shortName() to PrimitiveTypeInfo("int", true, kotlinUInt, "kotlin", "UInt"),
78+
StandardNames.FqNames.uLongFqName.shortName() to PrimitiveTypeInfo("long", true, kotlinULong, "kotlin", "ULong"),
7279

73-
IdSignatureValues._double to PrimitiveTypeInfo("double", true, javaLangDouble, "kotlin", "Double"),
74-
IdSignatureValues._float to PrimitiveTypeInfo("float", true, javaLangFloat, "kotlin", "Float"),
80+
StandardNames.FqNames._double.shortName() to PrimitiveTypeInfo("double", true, javaLangDouble, "kotlin", "Double"),
81+
StandardNames.FqNames._float.shortName() to PrimitiveTypeInfo("float", true, javaLangFloat, "kotlin", "Float"),
7582

76-
IdSignatureValues._boolean to PrimitiveTypeInfo("boolean", true, javaLangBoolean, "kotlin", "Boolean"),
83+
StandardNames.FqNames._boolean.shortName() to PrimitiveTypeInfo("boolean", true, javaLangBoolean, "kotlin", "Boolean"),
7784

78-
IdSignatureValues._char to PrimitiveTypeInfo("char", true, javaLangCharacter, "kotlin", "Char"),
85+
StandardNames.FqNames._char.shortName() to PrimitiveTypeInfo("char", true, javaLangCharacter, "kotlin", "Char"),
7986

80-
IdSignatureValues.unit to PrimitiveTypeInfo("void", false, kotlinUnit, "kotlin", "Unit"),
81-
IdSignatureValues.nothing to PrimitiveTypeInfo(null, true, javaLangVoid, "kotlin", "Nothing"),
87+
StandardNames.FqNames.unit.shortName() to PrimitiveTypeInfo("void", false, kotlinUnit, "kotlin", "Unit"),
88+
StandardNames.FqNames.nothing.shortName() to PrimitiveTypeInfo(null, true, javaLangVoid, "kotlin", "Nothing"),
8289
)
8390
}()
8491
}
Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
package com.github.codeql.utils.versions
22

33
import com.github.codeql.KotlinUsesExtractor
4+
import org.jetbrains.kotlin.backend.common.serialization.DescriptorByIdSignatureFinderImpl
5+
import org.jetbrains.kotlin.idea.MainFunctionDetector
6+
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
7+
import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmDescriptorMangler
48
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
9+
import org.jetbrains.kotlin.ir.util.SymbolTable
10+
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorImpl
511

6-
fun <TIrStub> KotlinUsesExtractor.getIrStubFromDescriptor(generateStub: (DeclarationStubGenerator) -> TIrStub) : TIrStub? {
7-
logger.error("Descriptors not yet supported for Kotlin 1.7")
8-
return null
9-
}
12+
@OptIn(ObsoleteDescriptorBasedAPI::class)
13+
fun <TIrStub> KotlinUsesExtractor.getIrStubFromDescriptor(generateStub: (DeclarationStubGenerator) -> TIrStub) : TIrStub? =
14+
(pluginContext.symbolTable as? SymbolTable) ?.let {
15+
// Copying the construction seen in JvmIrLinker.kt
16+
val mangler = JvmDescriptorMangler(MainFunctionDetector(pluginContext.bindingContext, pluginContext.languageVersionSettings))
17+
val descriptorFinder = DescriptorByIdSignatureFinderImpl(
18+
pluginContext.moduleDescriptor,
19+
mangler,
20+
DescriptorByIdSignatureFinderImpl.LookupMode.MODULE_ONLY
21+
)
22+
val stubGenerator = DeclarationStubGeneratorImpl(pluginContext.moduleDescriptor, it, pluginContext.irBuiltIns, descriptorFinder)
23+
generateStub(stubGenerator)
24+
} ?: run {
25+
logger.error("Plugin context has no symbol table, couldn't get IR stub")
26+
null
27+
}

0 commit comments

Comments
 (0)