@@ -210,10 +210,6 @@ open class KotlinUsesExtractor(
210210 // `typeArgs` can be null to describe a raw generic type.
211211 // For non-generic types it will be zero-length list.
212212 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-
217213 val substituteClass = getJavaEquivalentClass(c)
218214
219215 val extractClass = substituteClass ? : c
@@ -418,10 +414,11 @@ open class KotlinUsesExtractor(
418414 }
419415
420416 val fqName = replacedClass.fqNameWhenAvailable
421- val signature = if (fqName == null ) {
417+ val signature = if (replacedClass.isAnonymousObject) {
418+ null
419+ } else if (fqName == null ) {
422420 logger.error(" Unable to find signature/fqName for ${replacedClass.name} " )
423- // TODO: Should we return null here instead?
424- " <no signature available>"
421+ null
425422 } else {
426423 fqName.asString()
427424 }
@@ -465,22 +462,14 @@ open class KotlinUsesExtractor(
465462 }
466463 }
467464
468- fun useAnonymousClass (c : IrClass ) =
465+ private fun useAnonymousClass (c : IrClass ) =
469466 tw.lm.anonymousTypeMapping.getOrPut(c) {
470467 TypeResults (
471468 TypeResult (tw.getFreshIdLabel<DbClass >(), " " , " " ),
472469 TypeResult (fakeKotlinType(), " TODO" , " TODO" )
473470 )
474471 }
475472
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-
484473 fun fakeKotlinType (): Label <out DbKt_type > {
485474 val fakeKotlinPackageId: Label <DbPackage > = tw.getLabelFor(" @\" FakeKotlinPackage\" " , {
486475 tw.writePackages(it, " fake.kotlin" )
@@ -497,16 +486,6 @@ open class KotlinUsesExtractor(
497486 // `args` can be null to describe a raw generic type.
498487 // For non-generic types it will be zero-length list.
499488 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-
510489 val classInstanceResult = useClassInstance(c, args)
511490 val javaClassId = classInstanceResult.typeResult.id
512491 val kotlinQualClassName = getUnquotedClassLabel(c, args).classLabel
@@ -795,7 +774,7 @@ open class KotlinUsesExtractor(
795774 extractFileClass(dp)
796775 }
797776 is IrClass ->
798- if (classTypeArguments != null && ! dp.isAnonymousObject ) {
777+ if (classTypeArguments != null ) {
799778 useClassInstance(dp, classTypeArguments, inReceiverContext).typeResult.id
800779 } else {
801780 val replacedType = tryReplaceParcelizeRawType(dp)
@@ -1319,6 +1298,12 @@ open class KotlinUsesExtractor(
13191298 }
13201299 } ? : f
13211300
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+
13221307 fun <T : DbCallable > useFunction (f : IrFunction , classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? = null, noReplace : Boolean = false): Label <out T > {
13231308 return useFunction(f, null , classTypeArgsIncludingOuterClasses, noReplace)
13241309 }
@@ -1330,14 +1315,29 @@ open class KotlinUsesExtractor(
13301315 }
13311316 val javaFun = kotlinFunctionToJavaEquivalent(f, noReplace)
13321317 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+ }
13341321 if (isExternalDeclaration(javaFun)) {
13351322 extractFunctionLaterIfExternalFileMember(javaFun)
13361323 extractExternalEnclosingClassLater(javaFun)
13371324 }
13381325 return id
13391326 }
13401327
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+
13411341 fun getTypeArgumentLabel (
13421342 arg : IrTypeArgument
13431343 ): TypeResultWithoutSignature <DbReftype > {
@@ -1393,20 +1393,24 @@ open class KotlinUsesExtractor(
13931393 private fun getUnquotedClassLabel (c : IrClass , argsIncludingOuterClasses : List <IrTypeArgument >? ): ClassLabelResults {
13941394 val pkg = c.packageFqName?.asString() ? : " "
13951395 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+ }
14101414
14111415 val reorderedArgs = orderTypeArgsLeftToRight(c, argsIncludingOuterClasses)
14121416 val typeArgLabels = reorderedArgs?.map { getTypeArgumentLabel(it) }
@@ -1417,31 +1421,24 @@ open class KotlinUsesExtractor(
14171421 " "
14181422 else
14191423 typeArgLabels.takeLast(c.typeParameters.size).joinToString(prefix = " <" , postfix = " >" , separator = " ," ) { it.shortName }
1424+ val shortNamePrefix = if (c.isAnonymousObject) " " else cls
14201425
14211426 return ClassLabelResults (
14221427 label + (typeArgLabels?.joinToString(separator = " " ) { " ;{${it.id} }" } ? : " <>" ),
1423- cls + typeArgsShortName
1428+ shortNamePrefix + typeArgsShortName
14241429 )
14251430 }
14261431
14271432 // `args` can be null to describe a raw generic type.
14281433 // For non-generic types it will be zero-length list.
14291434 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-
14341435 val unquotedLabel = getUnquotedClassLabel(c, argsIncludingOuterClasses)
14351436 return ClassLabelResults (
14361437 " @\" class;${unquotedLabel.classLabel} \" " ,
14371438 unquotedLabel.shortName)
14381439 }
14391440
14401441 fun useClassSource (c : IrClass ): Label <out DbClassorinterface > {
1441- if (c.isAnonymousObject) {
1442- return useAnonymousClass(c).javaResult.id.cast<DbClass >()
1443- }
1444-
14451442 // For source classes, the label doesn't include any type arguments
14461443 val classTypeResult = addClassLabel(c, listOf ())
14471444 return classTypeResult.id
@@ -1686,8 +1683,11 @@ open class KotlinUsesExtractor(
16861683 }
16871684 }
16881685
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+ }
16911691
16921692 fun getEnumEntryLabel (ee : IrEnumEntry ): String {
16931693 val parentId = useDeclarationParent(ee.parent, false )
0 commit comments