Skip to content

Commit 7c5a490

Browse files
authored
Merge pull request #9468 from igfoo/igfoo/overloads
Kotlin: Put overloads together
2 parents bdae353 + 6055aaf commit 7c5a490

File tree

4 files changed

+62
-64
lines changed

4 files changed

+62
-64
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class ExternalDeclExtractor(val logger: FileLogger, val invocationTrapFile: Stri
1919
val externalDeclsDone = HashSet<IrDeclaration>()
2020
val externalDeclWorkList = ArrayList<Pair<IrDeclaration, String>>()
2121

22+
val propertySignature = ";property"
23+
val fieldSignature = ";field"
24+
2225
fun extractLater(d: IrDeclaration, signature: String): Boolean {
2326
if (d !is IrClass && !isExternalFileClassMember(d)) {
2427
logger.errorElement("External declaration is neither a class, nor a top-level declaration", d)
@@ -28,13 +31,8 @@ class ExternalDeclExtractor(val logger: FileLogger, val invocationTrapFile: Stri
2831
if (ret) externalDeclWorkList.add(Pair(d, signature))
2932
return ret
3033
}
31-
32-
val propertySignature = ";property"
33-
val fieldSignature = ";field"
34-
3534
fun extractLater(p: IrProperty) = extractLater(p, propertySignature)
3635
fun extractLater(f: IrField) = extractLater(f, fieldSignature)
37-
3836
fun extractLater(c: IrClass) = extractLater(c, "")
3937

4038
fun extractExternalClasses() {

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -975,35 +975,6 @@ open class KotlinUsesExtractor(
975975
return getFunctionLabel(f, null, classTypeArgsIncludingOuterClasses)
976976
}
977977

978-
fun getAdjustedReturnType(f: IrFunction) : IrType {
979-
// The return type of `java.util.concurrent.ConcurrentHashMap<K,V>.keySet/0` is defined as `Set<K>` in the stubs inside the Android SDK.
980-
// This does not match the Java SDK return type: `ConcurrentHashMap.KeySetView<K,V>`, so it's adjusted here.
981-
// This is a deliberate change in the Android SDK: https://github.com/AndroidSDKSources/android-sdk-sources-for-api-level-31/blob/2c56b25f619575bea12f9c5520ed2259620084ac/java/util/concurrent/ConcurrentHashMap.java#L1244-L1249
982-
// The annotation on the source is not visible in the android.jar, so we can't make the change based on that.
983-
// TODO: there are other instances of `dalvik.annotation.codegen.CovariantReturnType` in the Android SDK, we should handle those too if they cause DB inconsistencies
984-
val parentClass = f.parentClassOrNull
985-
if (parentClass == null ||
986-
parentClass.fqNameWhenAvailable?.asString() != "java.util.concurrent.ConcurrentHashMap" ||
987-
getFunctionShortName(f).nameInDB != "keySet" ||
988-
f.valueParameters.isNotEmpty() ||
989-
f.returnType.classFqName?.asString() != "kotlin.collections.MutableSet") {
990-
return f.returnType
991-
}
992-
993-
val otherKeySet = parentClass.declarations.filterIsInstance<IrFunction>().find { it.name.asString() == "keySet" && it.valueParameters.size == 1 }
994-
?: return f.returnType
995-
996-
return otherKeySet.returnType.codeQlWithHasQuestionMark(false)
997-
}
998-
999-
@OptIn(ObsoleteDescriptorBasedAPI::class)
1000-
fun getJavaMethod(f: IrFunction) = (f.descriptor.source as? JavaSourceElement)?.javaElement as? JavaMethod
1001-
1002-
fun hasWildcardSuppressionAnnotation(d: IrDeclaration) =
1003-
d.hasAnnotation(jvmWildcardSuppressionAnnotaton) ||
1004-
// Note not using `parentsWithSelf` as that only works if `d` is an IrDeclarationParent
1005-
d.parents.any { (it as? IrAnnotationContainer)?.hasAnnotation(jvmWildcardSuppressionAnnotaton) == true }
1006-
1007978
/*
1008979
* There are some pairs of classes (e.g. `kotlin.Throwable` and
1009980
* `java.lang.Throwable`) which are really just 2 different names
@@ -1110,6 +1081,35 @@ open class KotlinUsesExtractor(
11101081
return "@\"$prefix;{$parentId}.$name($paramTypeIds){$returnTypeId}${typeArgSuffix}\""
11111082
}
11121083

1084+
fun getAdjustedReturnType(f: IrFunction) : IrType {
1085+
// The return type of `java.util.concurrent.ConcurrentHashMap<K,V>.keySet/0` is defined as `Set<K>` in the stubs inside the Android SDK.
1086+
// This does not match the Java SDK return type: `ConcurrentHashMap.KeySetView<K,V>`, so it's adjusted here.
1087+
// This is a deliberate change in the Android SDK: https://github.com/AndroidSDKSources/android-sdk-sources-for-api-level-31/blob/2c56b25f619575bea12f9c5520ed2259620084ac/java/util/concurrent/ConcurrentHashMap.java#L1244-L1249
1088+
// The annotation on the source is not visible in the android.jar, so we can't make the change based on that.
1089+
// TODO: there are other instances of `dalvik.annotation.codegen.CovariantReturnType` in the Android SDK, we should handle those too if they cause DB inconsistencies
1090+
val parentClass = f.parentClassOrNull
1091+
if (parentClass == null ||
1092+
parentClass.fqNameWhenAvailable?.asString() != "java.util.concurrent.ConcurrentHashMap" ||
1093+
getFunctionShortName(f).nameInDB != "keySet" ||
1094+
f.valueParameters.isNotEmpty() ||
1095+
f.returnType.classFqName?.asString() != "kotlin.collections.MutableSet") {
1096+
return f.returnType
1097+
}
1098+
1099+
val otherKeySet = parentClass.declarations.filterIsInstance<IrFunction>().find { it.name.asString() == "keySet" && it.valueParameters.size == 1 }
1100+
?: return f.returnType
1101+
1102+
return otherKeySet.returnType.codeQlWithHasQuestionMark(false)
1103+
}
1104+
1105+
@OptIn(ObsoleteDescriptorBasedAPI::class)
1106+
fun getJavaMethod(f: IrFunction) = (f.descriptor.source as? JavaSourceElement)?.javaElement as? JavaMethod
1107+
1108+
fun hasWildcardSuppressionAnnotation(d: IrDeclaration) =
1109+
d.hasAnnotation(jvmWildcardSuppressionAnnotaton) ||
1110+
// Note not using `parentsWithSelf` as that only works if `d` is an IrDeclarationParent
1111+
d.parents.any { (it as? IrAnnotationContainer)?.hasAnnotation(jvmWildcardSuppressionAnnotaton) == true }
1112+
11131113
protected fun IrFunction.isLocalFunction(): Boolean {
11141114
return this.visibility == DescriptorVisibilities.LOCAL
11151115
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,6 @@ open class FileTrapWriter (
276276
fun getLocation(e: IrElement): Label<DbLocation> {
277277
return getLocation(e.startOffset, e.endOffset)
278278
}
279-
/**
280-
* Gets a label for the location representing the whole of this file.
281-
*/
282-
fun getWholeFileLocation(): Label<DbLocation> {
283-
return getWholeFileLocation(fileId)
284-
}
285279
/**
286280
* Gets a label for the location corresponding to `startOffset` and
287281
* `endOffset` within this file.
@@ -303,6 +297,12 @@ open class FileTrapWriter (
303297
// to be 0.
304298
return "file://$filePath"
305299
}
300+
/**
301+
* Gets a label for the location representing the whole of this file.
302+
*/
303+
fun getWholeFileLocation(): Label<DbLocation> {
304+
return getWholeFileLocation(fileId)
305+
}
306306
}
307307

308308
/**

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@ fun IrType.substituteTypeArguments(params: List<IrTypeParameter>, arguments: Lis
3737
else -> this
3838
}
3939

40+
fun IrSimpleType.substituteTypeArguments(substitutionMap: Map<IrTypeParameterSymbol, IrTypeArgument>): IrSimpleType {
41+
if (substitutionMap.isEmpty()) return this
42+
43+
val newArguments = arguments.map {
44+
if (it is IrTypeProjection) {
45+
val itType = it.type
46+
if (itType is IrSimpleType) {
47+
subProjectedType(substitutionMap, itType, it.variance)
48+
} else {
49+
it
50+
}
51+
} else {
52+
it
53+
}
54+
}
55+
56+
return IrSimpleTypeImpl(
57+
classifier,
58+
hasQuestionMark,
59+
newArguments,
60+
annotations
61+
)
62+
}
63+
4064
/**
4165
* Returns true if substituting `innerVariance T` into the context `outerVariance []` discards all knowledge about
4266
* what T could be.
@@ -76,30 +100,6 @@ private fun subProjectedType(substitutionMap: Map<IrTypeParameterSymbol, IrTypeA
76100
}
77101
} ?: makeTypeProjection(t.substituteTypeArguments(substitutionMap), outerVariance)
78102

79-
fun IrSimpleType.substituteTypeArguments(substitutionMap: Map<IrTypeParameterSymbol, IrTypeArgument>): IrSimpleType {
80-
if (substitutionMap.isEmpty()) return this
81-
82-
val newArguments = arguments.map {
83-
if (it is IrTypeProjection) {
84-
val itType = it.type
85-
if (itType is IrSimpleType) {
86-
subProjectedType(substitutionMap, itType, it.variance)
87-
} else {
88-
it
89-
}
90-
} else {
91-
it
92-
}
93-
}
94-
95-
return IrSimpleTypeImpl(
96-
classifier,
97-
hasQuestionMark,
98-
newArguments,
99-
annotations
100-
)
101-
}
102-
103103
fun IrTypeArgument.upperBound(context: IrPluginContext) =
104104
when(this) {
105105
is IrStarProjection -> context.irBuiltIns.anyNType

0 commit comments

Comments
 (0)