From 00949e9967acbaaf8d31ac886b75eb0c37037b99 Mon Sep 17 00:00:00 2001 From: Ian Lynagh Date: Mon, 23 Sep 2024 19:11:16 +0100 Subject: [PATCH] Kotlin: Fix build with 2.1.0-Beta1 --- .../src/main/kotlin/KotlinFileExtractor.kt | 35 ++++--------------- .../src/main/kotlin/KotlinUsesExtractor.kt | 12 ++++--- .../src/main/kotlin/MetaAnnotationSupport.kt | 8 +---- .../src/main/kotlin/utils/JvmNames.kt | 4 +-- .../src/main/kotlin/utils/TypeSubstitution.kt | 2 +- .../utils/versions/v_1_5_0/CodeQLIrConst.kt | 5 +++ .../utils/versions/v_1_5_0/CodeQLIsRoot.kt | 5 +++ .../kotlin/utils/versions/v_1_5_0/Types.kt | 2 +- .../kotlin/utils/versions/v_1_6_20/Types.kt | 2 +- .../versions/v_2_1_0-Beta1/CodeQLIrConst.kt | 5 +++ .../versions/v_2_1_0-Beta1/CodeQLIsRoot.kt | 5 +++ 11 files changed, 39 insertions(+), 46 deletions(-) create mode 100644 java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/CodeQLIrConst.kt create mode 100644 java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/CodeQLIsRoot.kt create mode 100644 java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_1_0-Beta1/CodeQLIrConst.kt create mode 100644 java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_1_0-Beta1/CodeQLIsRoot.kt diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt index 0ca8cb4af09a..db73cace1ac8 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt @@ -27,30 +27,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection -import org.jetbrains.kotlin.ir.util.companionObject -import org.jetbrains.kotlin.ir.util.constructors -import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable -import org.jetbrains.kotlin.ir.util.hasAnnotation -import org.jetbrains.kotlin.ir.util.hasInterfaceParent -import org.jetbrains.kotlin.ir.util.isAnnotationClass -import org.jetbrains.kotlin.ir.util.isAnonymousObject -import org.jetbrains.kotlin.ir.util.isFakeOverride -import org.jetbrains.kotlin.ir.util.isFunctionOrKFunction -import org.jetbrains.kotlin.ir.util.isInterface -import org.jetbrains.kotlin.ir.util.isLocal -import org.jetbrains.kotlin.ir.util.isNonCompanionObject -import org.jetbrains.kotlin.ir.util.isObject -import org.jetbrains.kotlin.ir.util.isSuspend -import org.jetbrains.kotlin.ir.util.isSuspendFunctionOrKFunction -import org.jetbrains.kotlin.ir.util.isVararg -import org.jetbrains.kotlin.ir.util.kotlinFqName -import org.jetbrains.kotlin.ir.util.packageFqName -import org.jetbrains.kotlin.ir.util.parentAsClass -import org.jetbrains.kotlin.ir.util.parentClassOrNull -import org.jetbrains.kotlin.ir.util.parents -import org.jetbrains.kotlin.ir.util.primaryConstructor -import org.jetbrains.kotlin.ir.util.render -import org.jetbrains.kotlin.ir.util.target +import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.load.java.JvmAnnotationNames import org.jetbrains.kotlin.load.java.NOT_NULL_ANNOTATIONS import org.jetbrains.kotlin.load.java.NULLABLE_ANNOTATIONS @@ -826,7 +803,7 @@ open class KotlinFileExtractor( fun exprId() = tw.getLabelFor("@\"annotationExpr;{$parent};$idx\"") return when (v) { - is IrConst<*> -> { + is CodeQLIrConst<*> -> { extractConstant(v, parent, idx, null, null, overrideId = exprId()) } is IrGetEnumValue -> { @@ -5999,7 +5976,7 @@ open class KotlinFileExtractor( extractExpressionExpr(a, callable, id, i, exprParent.enclosingStmt) } } - is IrConst<*> -> { + is CodeQLIrConst<*> -> { val exprParent = parent.expr(e, callable) extractConstant( e, @@ -6211,9 +6188,9 @@ open class KotlinFileExtractor( if ( (isAndAnd || isOrOr) && e.branches.size == 2 && - (e.branches[1].condition as? IrConst<*>)?.value == true && + (e.branches[1].condition as? CodeQLIrConst<*>)?.value == true && (e.branches[if (e.origin == IrStatementOrigin.ANDAND) 1 else 0].result - as? IrConst<*>) + as? CodeQLIrConst<*>) ?.value == isOrOr ) { @@ -6869,7 +6846,7 @@ open class KotlinFileExtractor( } private fun extractConstant( - e: IrConst<*>, + e: CodeQLIrConst<*>, parent: Label, idx: Int, enclosingCallable: Label?, diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt b/java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt index 3be94d65690f..be41c0ea70d0 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt @@ -5,7 +5,7 @@ import com.github.codeql.utils.versions.* import com.semmle.extractor.java.OdasaOutput import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext import org.jetbrains.kotlin.backend.common.ir.* -import org.jetbrains.kotlin.backend.jvm.ir.propertyIfAccessor +import org.jetbrains.kotlin.backend.jvm.ir.* import org.jetbrains.kotlin.codegen.JvmCodegenUtil import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.load.kotlin.getJvmModuleNameForDeserializedDescripto import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.NameUtils import org.jetbrains.kotlin.name.SpecialNames +import org.jetbrains.kotlin.resolve.descriptorUtil.propertyIfAccessor import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.util.OperatorNameConventions @@ -84,7 +85,7 @@ open class KotlinUsesExtractor( } private fun extractFileClass(fqName: FqName): Label { - val pkg = if (fqName.isRoot()) "" else fqName.parent().asString() + val pkg = if (fqName.codeQlIsRoot()) "" else fqName.parent().asString() val jvmName = fqName.shortName().asString() return extractFileClass(pkg, jvmName) } @@ -906,7 +907,7 @@ open class KotlinUsesExtractor( return arrayInfo.componentTypeResults } owner is IrClass -> { - val args = if (s.isRawType()) null else s.arguments + val args = if (s.codeQlIsRawType()) null else s.arguments return useSimpleTypeClass(owner, args, s.isNullable()) } @@ -1232,9 +1233,10 @@ open class KotlinUsesExtractor( // false if it has `@JvmSuppressWildcards(false)`, // and null if the annotation is not present. @Suppress("UNCHECKED_CAST") - private fun getWildcardSuppressionDirective(t: IrAnnotationContainer) = + private fun getWildcardSuppressionDirective(t: IrAnnotationContainer): Boolean? = t.getAnnotation(jvmWildcardSuppressionAnnotation)?.let { - (it.getValueArgument(0) as? IrConst)?.value ?: true + @Suppress("USELESS_CAST") // `as? Boolean` is not needed for Kotlin < 2.1 + (it.getValueArgument(0) as? CodeQLIrConst)?.value as? Boolean ?: true } private fun addJavaLoweringArgumentWildcards( diff --git a/java/kotlin-extractor/src/main/kotlin/MetaAnnotationSupport.kt b/java/kotlin-extractor/src/main/kotlin/MetaAnnotationSupport.kt index 2dc7382bf166..e591eed3eca6 100644 --- a/java/kotlin-extractor/src/main/kotlin/MetaAnnotationSupport.kt +++ b/java/kotlin-extractor/src/main/kotlin/MetaAnnotationSupport.kt @@ -27,13 +27,7 @@ import org.jetbrains.kotlin.ir.expressions.IrClassReference import org.jetbrains.kotlin.ir.expressions.IrConstructorCall import org.jetbrains.kotlin.ir.expressions.IrGetEnumValue import org.jetbrains.kotlin.ir.expressions.IrVararg -import org.jetbrains.kotlin.ir.expressions.impl.IrClassReferenceImpl -import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl -import org.jetbrains.kotlin.ir.expressions.impl.IrGetEnumValueImpl -import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl -import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl -import org.jetbrains.kotlin.ir.expressions.impl.IrReturnImpl -import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl +import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.types.typeWith import org.jetbrains.kotlin.ir.util.constructedClass diff --git a/java/kotlin-extractor/src/main/kotlin/utils/JvmNames.kt b/java/kotlin-extractor/src/main/kotlin/utils/JvmNames.kt index b6b51fde5430..ec7c17b39fc6 100644 --- a/java/kotlin-extractor/src/main/kotlin/utils/JvmNames.kt +++ b/java/kotlin-extractor/src/main/kotlin/utils/JvmNames.kt @@ -1,12 +1,12 @@ package com.github.codeql.utils import com.github.codeql.utils.versions.allOverriddenIncludingSelf +import com.github.codeql.utils.versions.CodeQLIrConst import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction -import org.jetbrains.kotlin.ir.expressions.IrConst import org.jetbrains.kotlin.ir.expressions.IrConstructorCall import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable @@ -82,7 +82,7 @@ fun getJvmName(container: IrAnnotationContainer): String? { if (owner is IrClass) { val aPkg = owner.packageFqName?.asString() val name = owner.name.asString() - if (aPkg == "kotlin.jvm" && name == "JvmName" && v is IrConst<*>) { + if (aPkg == "kotlin.jvm" && name == "JvmName" && v is CodeQLIrConst<*>) { val value = v.value if (value is String) { return value diff --git a/java/kotlin-extractor/src/main/kotlin/utils/TypeSubstitution.kt b/java/kotlin-extractor/src/main/kotlin/utils/TypeSubstitution.kt index 91cd5ec9f13d..6f8d0d33f373 100644 --- a/java/kotlin-extractor/src/main/kotlin/utils/TypeSubstitution.kt +++ b/java/kotlin-extractor/src/main/kotlin/utils/TypeSubstitution.kt @@ -15,7 +15,7 @@ import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl import org.jetbrains.kotlin.ir.expressions.IrConstructorCall -import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl +import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.impl.DescriptorlessExternalPackageFragmentSymbol import org.jetbrains.kotlin.ir.types.* diff --git a/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/CodeQLIrConst.kt b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/CodeQLIrConst.kt new file mode 100644 index 000000000000..0f8f6bda71ae --- /dev/null +++ b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/CodeQLIrConst.kt @@ -0,0 +1,5 @@ +package com.github.codeql.utils.versions + +import org.jetbrains.kotlin.ir.expressions.IrConst + +typealias CodeQLIrConst = IrConst diff --git a/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/CodeQLIsRoot.kt b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/CodeQLIsRoot.kt new file mode 100644 index 000000000000..bf523ca88215 --- /dev/null +++ b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/CodeQLIsRoot.kt @@ -0,0 +1,5 @@ +package com.github.codeql.utils.versions + +import org.jetbrains.kotlin.name.FqName + +fun FqName.codeQlIsRoot() = this.isRoot() diff --git a/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/Types.kt b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/Types.kt index 630d649907ca..61a580e2a279 100644 --- a/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/Types.kt +++ b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_5_0/Types.kt @@ -3,4 +3,4 @@ package com.github.codeql.utils.versions import org.jetbrains.kotlin.backend.jvm.codegen.isRawType import org.jetbrains.kotlin.ir.types.IrSimpleType -fun IrSimpleType.isRawType() = this.isRawType() +fun IrSimpleType.codeQlIsRawType() = this.isRawType() diff --git a/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_6_20/Types.kt b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_6_20/Types.kt index d44e44074005..7db5d520b29f 100644 --- a/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_6_20/Types.kt +++ b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_1_6_20/Types.kt @@ -3,4 +3,4 @@ package com.github.codeql.utils.versions import org.jetbrains.kotlin.backend.jvm.ir.isRawType import org.jetbrains.kotlin.ir.types.IrSimpleType -fun IrSimpleType.isRawType() = this.isRawType() +fun IrSimpleType.codeQlIsRawType() = this.isRawType() diff --git a/java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_1_0-Beta1/CodeQLIrConst.kt b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_1_0-Beta1/CodeQLIrConst.kt new file mode 100644 index 000000000000..60ff588cd97f --- /dev/null +++ b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_1_0-Beta1/CodeQLIrConst.kt @@ -0,0 +1,5 @@ +package com.github.codeql.utils.versions + +import org.jetbrains.kotlin.ir.expressions.IrConst + +typealias CodeQLIrConst = IrConst diff --git a/java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_1_0-Beta1/CodeQLIsRoot.kt b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_1_0-Beta1/CodeQLIsRoot.kt new file mode 100644 index 000000000000..e8eff805f21a --- /dev/null +++ b/java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_1_0-Beta1/CodeQLIsRoot.kt @@ -0,0 +1,5 @@ +package com.github.codeql.utils.versions + +import org.jetbrains.kotlin.name.FqName + +fun FqName.codeQlIsRoot() = this.isRoot