@@ -210,10 +210,6 @@ open class KotlinUsesExtractor(
210
210
// `typeArgs` can be null to describe a raw generic type.
211
211
// For non-generic types it will be zero-length list.
212
212
fun useClassInstance (c : IrClass , typeArgs : List <IrTypeArgument >? , inReceiverContext : Boolean = false): UseClassInstanceResult {
213
- if (c.isAnonymousObject) {
214
- logger.error(" Unexpected access to anonymous class instance" )
215
- }
216
-
217
213
val substituteClass = getJavaEquivalentClass(c)
218
214
219
215
val extractClass = substituteClass ? : c
@@ -418,10 +414,11 @@ open class KotlinUsesExtractor(
418
414
}
419
415
420
416
val fqName = replacedClass.fqNameWhenAvailable
421
- val signature = if (fqName == null ) {
417
+ val signature = if (replacedClass.isAnonymousObject) {
418
+ null
419
+ } else if (fqName == null ) {
422
420
logger.error(" Unable to find signature/fqName for ${replacedClass.name} " )
423
- // TODO: Should we return null here instead?
424
- " <no signature available>"
421
+ null
425
422
} else {
426
423
fqName.asString()
427
424
}
@@ -465,22 +462,14 @@ open class KotlinUsesExtractor(
465
462
}
466
463
}
467
464
468
- fun useAnonymousClass (c : IrClass ) =
465
+ private fun useAnonymousClass (c : IrClass ) =
469
466
tw.lm.anonymousTypeMapping.getOrPut(c) {
470
467
TypeResults (
471
468
TypeResult (tw.getFreshIdLabel<DbClass >(), " " , " " ),
472
469
TypeResult (fakeKotlinType(), " TODO" , " TODO" )
473
470
)
474
471
}
475
472
476
- fun getExistingAnonymousClassLabel (c : IrClass ): Label <out DbType >? {
477
- if (! c.isAnonymousObject){
478
- return null
479
- }
480
-
481
- return tw.lm.anonymousTypeMapping[c]?.javaResult?.id
482
- }
483
-
484
473
fun fakeKotlinType (): Label <out DbKt_type > {
485
474
val fakeKotlinPackageId: Label <DbPackage > = tw.getLabelFor(" @\" FakeKotlinPackage\" " , {
486
475
tw.writePackages(it, " fake.kotlin" )
@@ -497,16 +486,6 @@ open class KotlinUsesExtractor(
497
486
// `args` can be null to describe a raw generic type.
498
487
// For non-generic types it will be zero-length list.
499
488
fun useSimpleTypeClass (c : IrClass , args : List <IrTypeArgument >? , hasQuestionMark : Boolean ): TypeResults {
500
- if (c.isAnonymousObject) {
501
- args?.let {
502
- if (it.isNotEmpty() && ! isUnspecialised(c, it, logger)) {
503
- logger.error(" Unexpected specialised instance of generic anonymous class" )
504
- }
505
- }
506
-
507
- return useAnonymousClass(c)
508
- }
509
-
510
489
val classInstanceResult = useClassInstance(c, args)
511
490
val javaClassId = classInstanceResult.typeResult.id
512
491
val kotlinQualClassName = getUnquotedClassLabel(c, args).classLabel
@@ -795,7 +774,7 @@ open class KotlinUsesExtractor(
795
774
extractFileClass(dp)
796
775
}
797
776
is IrClass ->
798
- if (classTypeArguments != null && ! dp.isAnonymousObject ) {
777
+ if (classTypeArguments != null ) {
799
778
useClassInstance(dp, classTypeArguments, inReceiverContext).typeResult.id
800
779
} else {
801
780
val replacedType = tryReplaceParcelizeRawType(dp)
@@ -1319,6 +1298,12 @@ open class KotlinUsesExtractor(
1319
1298
}
1320
1299
} ? : f
1321
1300
1301
+ fun isPrivate (d : IrDeclaration ) =
1302
+ when (d) {
1303
+ is IrDeclarationWithVisibility -> d.visibility.let { it == DescriptorVisibilities .PRIVATE || it == DescriptorVisibilities .PRIVATE_TO_THIS }
1304
+ else -> false
1305
+ }
1306
+
1322
1307
fun <T : DbCallable > useFunction (f : IrFunction , classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? = null, noReplace : Boolean = false): Label <out T > {
1323
1308
return useFunction(f, null , classTypeArgsIncludingOuterClasses, noReplace)
1324
1309
}
@@ -1330,14 +1315,29 @@ open class KotlinUsesExtractor(
1330
1315
}
1331
1316
val javaFun = kotlinFunctionToJavaEquivalent(f, noReplace)
1332
1317
val label = getFunctionLabel(javaFun, parentId, classTypeArgsIncludingOuterClasses)
1333
- val id: Label <T > = tw.getLabelFor(label)
1318
+ val id: Label <T > = tw.getLabelFor(label) {
1319
+ extractPrivateSpecialisedDeclaration(f, classTypeArgsIncludingOuterClasses)
1320
+ }
1334
1321
if (isExternalDeclaration(javaFun)) {
1335
1322
extractFunctionLaterIfExternalFileMember(javaFun)
1336
1323
extractExternalEnclosingClassLater(javaFun)
1337
1324
}
1338
1325
return id
1339
1326
}
1340
1327
1328
+ private fun extractPrivateSpecialisedDeclaration (d : IrDeclaration , classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? ) {
1329
+ // Note here `classTypeArgsIncludingOuterClasses` being null doesn't signify a raw receiver type but rather that no type args were supplied.
1330
+ // This is because a call to a private method can only be observed inside Kotlin code, and Kotlin can't represent raw types.
1331
+ if (this is KotlinFileExtractor && isPrivate(d) && classTypeArgsIncludingOuterClasses != null && classTypeArgsIncludingOuterClasses.isNotEmpty()) {
1332
+ d.parent.let {
1333
+ when (it) {
1334
+ is IrClass -> this .extractDeclarationPrototype(d, useClassInstance(it, classTypeArgsIncludingOuterClasses).typeResult.id, classTypeArgsIncludingOuterClasses)
1335
+ else -> logger.warnElement(" Unable to extract specialised declaration that isn't a member of a class" , d)
1336
+ }
1337
+ }
1338
+ }
1339
+ }
1340
+
1341
1341
fun getTypeArgumentLabel (
1342
1342
arg : IrTypeArgument
1343
1343
): TypeResultWithoutSignature <DbReftype > {
@@ -1393,20 +1393,24 @@ open class KotlinUsesExtractor(
1393
1393
private fun getUnquotedClassLabel (c : IrClass , argsIncludingOuterClasses : List <IrTypeArgument >? ): ClassLabelResults {
1394
1394
val pkg = c.packageFqName?.asString() ? : " "
1395
1395
val cls = c.name.asString()
1396
- val label = when (val parent = c.parent) {
1397
- is IrClass -> {
1398
- " ${getUnquotedClassLabel(parent, listOf ()).classLabel} \$ $cls "
1399
- }
1400
- is IrFunction -> {
1401
- " {${useFunction<DbMethod >(parent)} }.$cls "
1402
- }
1403
- is IrField -> {
1404
- " {${useField(parent)} }.$cls "
1405
- }
1406
- else -> {
1407
- if (pkg.isEmpty()) cls else " $pkg .$cls "
1408
- }
1409
- }
1396
+ val label =
1397
+ if (c.isAnonymousObject)
1398
+ " {${useAnonymousClass(c).javaResult.id} }"
1399
+ else
1400
+ when (val parent = c.parent) {
1401
+ is IrClass -> {
1402
+ " ${getUnquotedClassLabel(parent, listOf ()).classLabel} \$ $cls "
1403
+ }
1404
+ is IrFunction -> {
1405
+ " {${useFunction<DbMethod >(parent)} }.$cls "
1406
+ }
1407
+ is IrField -> {
1408
+ " {${useField(parent)} }.$cls "
1409
+ }
1410
+ else -> {
1411
+ if (pkg.isEmpty()) cls else " $pkg .$cls "
1412
+ }
1413
+ }
1410
1414
1411
1415
val reorderedArgs = orderTypeArgsLeftToRight(c, argsIncludingOuterClasses)
1412
1416
val typeArgLabels = reorderedArgs?.map { getTypeArgumentLabel(it) }
@@ -1417,31 +1421,24 @@ open class KotlinUsesExtractor(
1417
1421
" "
1418
1422
else
1419
1423
typeArgLabels.takeLast(c.typeParameters.size).joinToString(prefix = " <" , postfix = " >" , separator = " ," ) { it.shortName }
1424
+ val shortNamePrefix = if (c.isAnonymousObject) " " else cls
1420
1425
1421
1426
return ClassLabelResults (
1422
1427
label + (typeArgLabels?.joinToString(separator = " " ) { " ;{${it.id} }" } ? : " <>" ),
1423
- cls + typeArgsShortName
1428
+ shortNamePrefix + typeArgsShortName
1424
1429
)
1425
1430
}
1426
1431
1427
1432
// `args` can be null to describe a raw generic type.
1428
1433
// For non-generic types it will be zero-length list.
1429
1434
fun getClassLabel (c : IrClass , argsIncludingOuterClasses : List <IrTypeArgument >? ): ClassLabelResults {
1430
- if (c.isAnonymousObject) {
1431
- logger.error(" Label generation should not be requested for an anonymous class" )
1432
- }
1433
-
1434
1435
val unquotedLabel = getUnquotedClassLabel(c, argsIncludingOuterClasses)
1435
1436
return ClassLabelResults (
1436
1437
" @\" class;${unquotedLabel.classLabel} \" " ,
1437
1438
unquotedLabel.shortName)
1438
1439
}
1439
1440
1440
1441
fun useClassSource (c : IrClass ): Label <out DbClassorinterface > {
1441
- if (c.isAnonymousObject) {
1442
- return useAnonymousClass(c).javaResult.id.cast<DbClass >()
1443
- }
1444
-
1445
1442
// For source classes, the label doesn't include any type arguments
1446
1443
val classTypeResult = addClassLabel(c, listOf ())
1447
1444
return classTypeResult.id
@@ -1686,8 +1683,11 @@ open class KotlinUsesExtractor(
1686
1683
}
1687
1684
}
1688
1685
1689
- fun useProperty (p : IrProperty , parentId : Label <out DbElement >, classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? ): Label <out DbKt_property > =
1690
- tw.getLabelFor<DbKt_property >(getPropertyLabel(p, parentId, classTypeArgsIncludingOuterClasses)).also { extractPropertyLaterIfExternalFileMember(p) }
1686
+ fun useProperty (p : IrProperty , parentId : Label <out DbElement >, classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? ) =
1687
+ tw.getLabelFor<DbKt_property >(getPropertyLabel(p, parentId, classTypeArgsIncludingOuterClasses)) {
1688
+ extractPropertyLaterIfExternalFileMember(p)
1689
+ extractPrivateSpecialisedDeclaration(p, classTypeArgsIncludingOuterClasses)
1690
+ }
1691
1691
1692
1692
fun getEnumEntryLabel (ee : IrEnumEntry ): String {
1693
1693
val parentId = useDeclarationParent(ee.parent, false )
0 commit comments