Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 178f128

Browse files
smowtonigfoo
authored andcommitted
Function labels: include <n> suffix (where n is the number of function type parameters)
This matches the Java extractor's behaviour.
1 parent 448b3d3 commit 178f128

2 files changed

Lines changed: 35 additions & 22 deletions

File tree

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ open class KotlinFileExtractor(
4646
is IrFunction -> {
4747
@Suppress("UNCHECKED_CAST")
4848
val parentId = useDeclarationParent(declaration.parent, false) as Label<DbReftype>
49-
extractFunctionIfReal(declaration, parentId)
49+
extractFunctionIfReal(declaration, parentId, true, null, listOf())
5050
}
5151
is IrAnonymousInitializer -> {
5252
// Leaving this intentionally empty. init blocks are extracted during class extraction.
5353
}
5454
is IrProperty -> {
5555
@Suppress("UNCHECKED_CAST")
5656
val parentId = useDeclarationParent(declaration.parent, false) as Label<DbReftype>
57-
extractProperty(declaration, parentId)
57+
extractProperty(declaration, parentId, true, null, listOf())
5858
}
5959
is IrEnumEntry -> {
6060
@Suppress("UNCHECKED_CAST")
@@ -216,8 +216,8 @@ open class KotlinFileExtractor(
216216

217217
c.declarations.map {
218218
when(it) {
219-
is IrFunction -> extractFunctionIfReal(it, id, false, typeParamSubstitution)
220-
is IrProperty -> extractProperty(it, id, false, typeParamSubstitution)
219+
is IrFunction -> extractFunctionIfReal(it, id, false, typeParamSubstitution, typeArgs)
220+
is IrProperty -> extractProperty(it, id, false, typeParamSubstitution, typeArgs)
221221
else -> {}
222222
}
223223
}
@@ -374,7 +374,7 @@ open class KotlinFileExtractor(
374374
}
375375

376376
// add method:
377-
val obinitLabel = getFunctionLabel(c, "<obinit>", listOf(), pluginContext.irBuiltIns.unitType, extensionReceiverParameter = null)
377+
val obinitLabel = getFunctionLabel(c, "<obinit>", listOf(), pluginContext.irBuiltIns.unitType, extensionReceiverParameter = null, functionTypeParameters = listOf(), classTypeArguments = listOf())
378378
val obinitId = tw.getLabelFor<DbMethod>(obinitLabel)
379379
val returnType = useType(pluginContext.irBuiltIns.unitType)
380380
tw.writeMethods(obinitId, "<obinit>", "<obinit>()", returnType.javaResult.id, returnType.kotlinResult.id, parentId, obinitId)
@@ -442,13 +442,13 @@ open class KotlinFileExtractor(
442442
}
443443
}
444444

445-
fun extractFunctionIfReal(f: IrFunction, parentId: Label<out DbReftype>, extractBody: Boolean = true, typeSubstitution: TypeSubstitution? = null) {
445+
fun extractFunctionIfReal(f: IrFunction, parentId: Label<out DbReftype>, extractBody: Boolean, typeSubstitution: TypeSubstitution?, classTypeArgs: List<IrTypeArgument>?) {
446446
if (f.origin == IrDeclarationOrigin.FAKE_OVERRIDE)
447447
return
448-
extractFunction(f, parentId, extractBody, typeSubstitution)
448+
extractFunction(f, parentId, extractBody, typeSubstitution, classTypeArgs)
449449
}
450450

451-
fun extractFunction(f: IrFunction, parentId: Label<out DbReftype>, extractBody: Boolean = true, typeSubstitution: TypeSubstitution? = null): Label<out DbCallable> {
451+
fun extractFunction(f: IrFunction, parentId: Label<out DbReftype>, extractBody: Boolean, typeSubstitution: TypeSubstitution?, classTypeArgs: List<IrTypeArgument>?): Label<out DbCallable> {
452452
declarationStack.push(f)
453453

454454
f.typeParameters.map { extractTypeParameter(it) }
@@ -459,7 +459,7 @@ open class KotlinFileExtractor(
459459
if (f.isLocalFunction())
460460
getLocallyVisibleFunctionLabels(f).function
461461
else
462-
useFunction<DbCallable>(f, parentId)
462+
useFunction<DbCallable>(f, parentId, classTypeArgs)
463463

464464
val extReceiver = f.extensionReceiverParameter
465465
val idxOffset = if (extReceiver != null) 1 else 0
@@ -543,7 +543,7 @@ open class KotlinFileExtractor(
543543
return id
544544
}
545545

546-
fun extractProperty(p: IrProperty, parentId: Label<out DbReftype>, extractBackingField: Boolean = true, typeSubstitution: TypeSubstitution? = null) {
546+
fun extractProperty(p: IrProperty, parentId: Label<out DbReftype>, extractBackingField: Boolean, typeSubstitution: TypeSubstitution?, classTypeArgs: List<IrTypeArgument>?) {
547547
val id = useProperty(p, parentId)
548548
val locId = tw.getLocation(p)
549549
tw.writeKtProperties(id, p.name.asString())
@@ -555,7 +555,7 @@ open class KotlinFileExtractor(
555555

556556
if(getter != null) {
557557
@Suppress("UNCHECKED_CAST")
558-
val getterId = extractFunction(getter, parentId, extractBackingField, typeSubstitution) as Label<out DbMethod>
558+
val getterId = extractFunction(getter, parentId, extractBackingField, typeSubstitution, classTypeArgs) as Label<out DbMethod>
559559
tw.writeKtPropertyGetters(id, getterId)
560560
} else {
561561
if (p.modality != Modality.FINAL || !isExternalDeclaration(p)) {
@@ -568,7 +568,7 @@ open class KotlinFileExtractor(
568568
logger.warnElement(Severity.ErrorSevere, "!isVar property with a setter", p)
569569
}
570570
@Suppress("UNCHECKED_CAST")
571-
val setterId = extractFunction(setter, parentId, extractBackingField, typeSubstitution) as Label<out DbMethod>
571+
val setterId = extractFunction(setter, parentId, extractBackingField, typeSubstitution, classTypeArgs) as Label<out DbMethod>
572572
tw.writeKtPropertySetters(id, setterId)
573573
} else {
574574
if (p.isVar && !isExternalDeclaration(p)) {
@@ -1493,7 +1493,7 @@ open class KotlinFileExtractor(
14931493
val id = tw.getFreshIdLabel<DbMethodaccess>()
14941494
val type = useType(e.type)
14951495
val locId = tw.getLocation(e)
1496-
val methodLabel = getFunctionLabel(irCallable.parent, "<obinit>", listOf(), e.type, null)
1496+
val methodLabel = getFunctionLabel(irCallable.parent, "<obinit>", listOf(), e.type, null, functionTypeParameters = listOf(), classTypeArguments = listOf())
14971497
val methodId = tw.getLabelFor<DbMethod>(methodLabel)
14981498
tw.writeExprs_methodaccess(id, type.javaResult.id, type.kotlinResult.id, exprParent.parent, exprParent.idx)
14991499
tw.writeHasLocation(id, locId)
@@ -2536,7 +2536,7 @@ open class KotlinFileExtractor(
25362536
val id = extractGeneratedClass(ids, superTypes, tw.getLocation(localFunction), localFunction)
25372537

25382538
// Extract local function as a member
2539-
extractFunctionIfReal(localFunction, id)
2539+
extractFunctionIfReal(localFunction, id, true, null, listOf())
25402540

25412541
return id
25422542
}

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,13 @@ class X {
567567
return f.name.asString()
568568
}
569569

570+
// This excludes class type parameters that show up in (at least) constructors' typeParameters list.
571+
fun getFunctionTypeParameters(f: IrFunction): List<IrTypeParameter> {
572+
return if (f is IrConstructor) f.typeParameters else f.typeParameters.filter { it.parent == f }
573+
}
574+
570575
fun getFunctionLabel(f: IrFunction, classTypeArguments: List<IrTypeArgument>? = null) : String {
571-
return getFunctionLabel(f.parent, getFunctionShortName(f), f.valueParameters, f.returnType, f.extensionReceiverParameter, classTypeArguments)
576+
return getFunctionLabel(f.parent, getFunctionShortName(f), f.valueParameters, f.returnType, f.extensionReceiverParameter, getFunctionTypeParameters(f), classTypeArguments)
572577
}
573578

574579
fun getFunctionLabel(
@@ -577,21 +582,24 @@ class X {
577582
parameters: List<IrValueParameter>,
578583
returnType: IrType,
579584
extensionReceiverParameter: IrValueParameter?,
585+
functionTypeParameters: List<IrTypeParameter>,
580586
classTypeArguments: List<IrTypeArgument>? = null
581587
): String {
582588
val parentId = useDeclarationParent(parent, false, classTypeArguments, true)
583-
return getFunctionLabel(parentId, name, parameters, returnType, extensionReceiverParameter)
589+
return getFunctionLabel(parentId, name, parameters, returnType, extensionReceiverParameter, functionTypeParameters, classTypeArguments)
584590
}
585591

586-
fun getFunctionLabel(f: IrFunction, parentId: Label<out DbElement>) =
587-
getFunctionLabel(parentId, getFunctionShortName(f), f.valueParameters, f.returnType, f.extensionReceiverParameter)
592+
fun getFunctionLabel(f: IrFunction, parentId: Label<out DbElement>, classTypeArguments: List<IrTypeArgument>?) =
593+
getFunctionLabel(parentId, getFunctionShortName(f), f.valueParameters, f.returnType, f.extensionReceiverParameter, getFunctionTypeParameters(f), classTypeArguments)
588594

589595
fun getFunctionLabel(
590596
parentId: Label<out DbElement>,
591597
name: String,
592598
parameters: List<IrValueParameter>,
593599
returnType: IrType,
594-
extensionReceiverParameter: IrValueParameter?
600+
extensionReceiverParameter: IrValueParameter?,
601+
functionTypeParameters: List<IrTypeParameter>,
602+
classTypeArguments: List<IrTypeArgument>?
595603
): String {
596604
val allParams = if (extensionReceiverParameter == null) {
597605
parameters
@@ -603,7 +611,12 @@ class X {
603611
val paramTypeIds = allParams.joinToString { "{${useType(erase(it.type)).javaResult.id}}" }
604612
val labelReturnType = if (name == "<init>") pluginContext.irBuiltIns.unitType else erase(returnType)
605613
val returnTypeId = useType(labelReturnType, TypeContext.RETURN).javaResult.id
606-
return "@\"callable;{$parentId}.$name($paramTypeIds){$returnTypeId}\""
614+
// This suffix is added to generic methods (and constructors) to match the Java extractor's behaviour.
615+
// Comments in that extractor indicates it didn't want the label of the callable to clash with the raw
616+
// method (and presumably that disambiguation is never needed when the method belongs to a parameterized
617+
// instance of a generic class), but as of now I don't know when the raw method would be referred to.
618+
val typeArgSuffix = if (functionTypeParameters.isNotEmpty() && classTypeArguments.isNullOrEmpty()) "<${functionTypeParameters.size}>" else "";
619+
return "@\"callable;{$parentId}.$name($paramTypeIds){$returnTypeId}${typeArgSuffix}\""
607620
}
608621

609622
protected fun IrFunction.isLocalFunction(): Boolean {
@@ -665,8 +678,8 @@ class X {
665678
}
666679
}
667680

668-
fun <T: DbCallable> useFunction(f: IrFunction, parentId: Label<out DbElement>) =
669-
useFunctionCommon<T>(f, getFunctionLabel(f, parentId))
681+
fun <T: DbCallable> useFunction(f: IrFunction, parentId: Label<out DbElement>, classTypeArguments: List<IrTypeArgument>?) =
682+
useFunctionCommon<T>(f, getFunctionLabel(f, parentId, classTypeArguments))
670683

671684
fun getTypeArgumentLabel(
672685
arg: IrTypeArgument

0 commit comments

Comments
 (0)