Skip to content

Commit 12eab3d

Browse files
committed
Kotlin: Specialise findSubType to IrDeclaration
We only use it on that type, and this makes the uses a bit quieter.
1 parent 4cd90a1 commit 12eab3d

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ open class KotlinFileExtractor(
15681568
}
15691569
}
15701570

1571-
private fun findFunction(cls: IrClass, name: String): IrFunction? = cls.declarations.findSubType<IrDeclaration,IrFunction> { it.name.asString() == name }
1571+
private fun findFunction(cls: IrClass, name: String): IrFunction? = cls.declarations.findSubType<IrFunction> { it.name.asString() == name }
15721572

15731573
val jvmIntrinsicsClass by lazy {
15741574
val result = pluginContext.referenceClass(FqName("kotlin.jvm.internal.Intrinsics"))?.owner
@@ -1625,7 +1625,7 @@ open class KotlinFileExtractor(
16251625
}
16261626

16271627
val stringValueOfObjectMethod by lazy {
1628-
val result = javaLangString?.declarations?.findSubType<IrDeclaration,IrFunction> {
1628+
val result = javaLangString?.declarations?.findSubType<IrFunction> {
16291629
it.name.asString() == "valueOf" &&
16301630
it.valueParameters.size == 1 &&
16311631
it.valueParameters[0].type == pluginContext.irBuiltIns.anyNType
@@ -1637,7 +1637,7 @@ open class KotlinFileExtractor(
16371637
}
16381638

16391639
val objectCloneMethod by lazy {
1640-
val result = javaLangObject?.declarations?.findSubType<IrDeclaration,IrFunction> {
1640+
val result = javaLangObject?.declarations?.findSubType<IrFunction> {
16411641
it.name.asString() == "clone"
16421642
}
16431643
if (result == null) {
@@ -1653,7 +1653,7 @@ open class KotlinFileExtractor(
16531653
}
16541654

16551655
val kotlinNoWhenBranchMatchedConstructor by lazy {
1656-
val result = kotlinNoWhenBranchMatchedExn?.declarations?.findSubType<IrDeclaration,IrConstructor> {
1656+
val result = kotlinNoWhenBranchMatchedExn?.declarations?.findSubType<IrConstructor> {
16571657
it.valueParameters.isEmpty()
16581658
}
16591659
if (result == null) {
@@ -1775,7 +1775,7 @@ open class KotlinFileExtractor(
17751775
return
17761776
}
17771777

1778-
val func = ((c.getTypeArgument(0) as? IrSimpleType)?.classifier?.owner as? IrClass)?.declarations?.findSubType<IrDeclaration,IrFunction> { it.name.asString() == fnName }
1778+
val func = ((c.getTypeArgument(0) as? IrSimpleType)?.classifier?.owner as? IrClass)?.declarations?.findSubType<IrFunction> { it.name.asString() == fnName }
17791779
if (func == null) {
17801780
logger.errorElement("Couldn't find function $fnName on enum type", c)
17811781
return
@@ -2225,7 +2225,7 @@ open class KotlinFileExtractor(
22252225
logger.errorElement("Argument to dataClassArrayMemberToString not a class", c)
22262226
return
22272227
}
2228-
val realCallee = javaUtilArrays?.declarations?.findSubType<IrDeclaration,IrFunction> { decl ->
2228+
val realCallee = javaUtilArrays?.declarations?.findSubType<IrFunction> { decl ->
22292229
decl.name.asString() == "toString" && decl.valueParameters.size == 1 &&
22302230
decl.valueParameters[0].type.classOrNull?.let { it == realArrayClass } == true
22312231
}
@@ -2252,7 +2252,7 @@ open class KotlinFileExtractor(
22522252
logger.errorElement("Argument to dataClassArrayMemberHashCode not a class", c)
22532253
return
22542254
}
2255-
val realCallee = javaUtilArrays?.declarations?.findSubType<IrDeclaration,IrFunction> { decl ->
2255+
val realCallee = javaUtilArrays?.declarations?.findSubType<IrFunction> { decl ->
22562256
decl.name.asString() == "hashCode" && decl.valueParameters.size == 1 &&
22572257
decl.valueParameters[0].type.classOrNull?.let { it == realArrayClass } == true
22582258
}
@@ -4363,7 +4363,7 @@ open class KotlinFileExtractor(
43634363
return
43644364
}
43654365

4366-
val invokeMethod = functionType.classOrNull?.owner?.declarations?.findSubType<IrDeclaration,IrFunction> { it.name.asString() == OperatorNameConventions.INVOKE.asString()}
4366+
val invokeMethod = functionType.classOrNull?.owner?.declarations?.findSubType<IrFunction> { it.name.asString() == OperatorNameConventions.INVOKE.asString()}
43674367
if (invokeMethod == null) {
43684368
logger.errorElement("Couldn't find `invoke` method on functional interface.", e)
43694369
return
@@ -4374,7 +4374,7 @@ open class KotlinFileExtractor(
43744374
logger.errorElement("Expected to find SAM conversion to IrClass. Found '${typeOwner.javaClass}' instead. Can't implement SAM interface.", e)
43754375
return
43764376
}
4377-
val samMember = typeOwner.declarations.findSubType<IrDeclaration,IrFunction> { it is IrOverridableMember && it.modality == Modality.ABSTRACT }
4377+
val samMember = typeOwner.declarations.findSubType<IrFunction> { it is IrOverridableMember && it.modality == Modality.ABSTRACT }
43784378
if (samMember == null) {
43794379
logger.errorElement("Couldn't find SAM member in type '${typeOwner.kotlinFqName.asString()}'. Can't implement SAM interface.", e)
43804380
return
@@ -4563,7 +4563,7 @@ open class KotlinFileExtractor(
45634563
val superCallId = tw.getFreshIdLabel<DbSuperconstructorinvocationstmt>()
45644564
tw.writeStmts_superconstructorinvocationstmt(superCallId, constructorBlockId, 0, ids.constructor)
45654565

4566-
val baseConstructor = baseClass.owner.declarations.findSubType<IrDeclaration,IrFunction> { it.symbol is IrConstructorSymbol }
4566+
val baseConstructor = baseClass.owner.declarations.findSubType<IrFunction> { it.symbol is IrConstructorSymbol }
45674567
val baseConstructorId = useFunction<DbConstructor>(baseConstructor as IrFunction)
45684568

45694569
tw.writeHasLocation(superCallId, locId)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ open class KotlinUsesExtractor(
326326
if (replacementClass === parentClass)
327327
return f
328328
return globalExtensionState.syntheticToRealFunctionMap.getOrPut(f) {
329-
val result = replacementClass.declarations.findSubType<IrDeclaration,IrSimpleFunction> { replacementDecl ->
329+
val result = replacementClass.declarations.findSubType<IrSimpleFunction> { replacementDecl ->
330330
replacementDecl is IrSimpleFunction && replacementDecl.name == f.name && replacementDecl.valueParameters.size == f.valueParameters.size && replacementDecl.valueParameters.zip(f.valueParameters).all {
331331
erase(it.first.type) == erase(it.second.type)
332332
}
@@ -351,7 +351,7 @@ open class KotlinUsesExtractor(
351351
if (replacementClass === parentClass)
352352
return f
353353
return globalExtensionState.syntheticToRealFieldMap.getOrPut(f) {
354-
val result = replacementClass.declarations.findSubType<IrDeclaration,IrField> { replacementDecl ->
354+
val result = replacementClass.declarations.findSubType<IrField> { replacementDecl ->
355355
replacementDecl.name == f.name
356356
}
357357
if (result == null) {
@@ -1097,7 +1097,7 @@ open class KotlinUsesExtractor(
10971097
return f.returnType
10981098
}
10991099

1100-
val otherKeySet = parentClass.declarations.findSubType<IrDeclaration,IrFunction> { it.name.asString() == "keySet" && it.valueParameters.size == 1 }
1100+
val otherKeySet = parentClass.declarations.findSubType<IrFunction> { it.name.asString() == "keySet" && it.valueParameters.size == 1 }
11011101
?: return f.returnType
11021102

11031103
return otherKeySet.returnType.codeQlWithHasQuestionMark(false)
@@ -1177,7 +1177,7 @@ open class KotlinUsesExtractor(
11771177
getJavaEquivalentClass(parentClass)?.let { javaClass ->
11781178
if (javaClass != parentClass)
11791179
// Look for an exact type match...
1180-
javaClass.declarations.findSubType<IrDeclaration,IrFunction> { decl ->
1180+
javaClass.declarations.findSubType<IrFunction> { decl ->
11811181
decl.name == f.name &&
11821182
decl.valueParameters.size == f.valueParameters.size &&
11831183
// Note matching by classifier not the whole type so that generic arguments are allowed to differ,
@@ -1193,7 +1193,7 @@ open class KotlinUsesExtractor(
11931193
} ?:
11941194
// Or check property accessors:
11951195
if (f.isAccessor) {
1196-
val prop = javaClass.declarations.findSubType<IrDeclaration,IrProperty> { decl ->
1196+
val prop = javaClass.declarations.findSubType<IrProperty> { decl ->
11971197
decl.name == (f.propertyIfAccessor as IrProperty).name
11981198
}
11991199
if (prop?.getter?.name == f.name)

java/kotlin-extractor/src/main/kotlin/utils/Iterable.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.github.codeql
22

3+
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
4+
35
/**
4-
* This behaves the same as Iterable<T>.find, but requires
6+
* This behaves the same as Iterable<IrDeclaration>.find, but requires
57
* that the value found is of the subtype S, and it casts
68
* the result for you appropriately.
79
*/
8-
inline fun <T,reified S: T> Iterable<T>.findSubType(
10+
inline fun <reified S: IrDeclaration> Iterable<IrDeclaration>.findSubType(
911
predicate: (S) -> Boolean
1012
): S? {
1113
return this.find { it is S && predicate(it) } as S?

0 commit comments

Comments
 (0)