Generate unique parameter names in LocalDeclarationsLowering

This commit is contained in:
pyos
2019-10-04 14:41:28 +02:00
committed by max-kammerer
parent 3b2843fe7a
commit cd47c11efd
14 changed files with 87 additions and 10 deletions
@@ -549,6 +549,7 @@ class LocalDeclarationsLowering(
oldDeclaration: IrFunction,
newDeclaration: IrFunction
) = ArrayList<IrValueParameter>(capturedValues.size + oldDeclaration.valueParameters.size).apply {
val generatedNames = mutableSetOf<Name>()
capturedValues.mapIndexedTo(this) { i, capturedValue ->
val parameterDescriptor = WrappedValueParameterDescriptor()
val p = capturedValue.owner
@@ -558,7 +559,7 @@ class LocalDeclarationsLowering(
if (p.descriptor is ReceiverParameterDescriptor && newDeclaration is IrConstructor)
BOUND_RECEIVER_PARAMETER else BOUND_VALUE_PARAMETER,
IrValueParameterSymbolImpl(parameterDescriptor),
suggestNameForCapturedValue(p),
suggestNameForCapturedValue(p, generatedNames),
i,
p.type,
null,
@@ -665,13 +666,13 @@ class LocalDeclarationsLowering(
private fun createFieldsForCapturedValues(localClassContext: LocalClassContext) {
val classDeclaration = localClassContext.declaration
val generatedNames = mutableSetOf<Name>()
localClassContext.closure.capturedValues.forEach { capturedValue ->
val irField = createFieldForCapturedValue(
classDeclaration.startOffset,
classDeclaration.endOffset,
suggestNameForCapturedValue(capturedValue.owner),
suggestNameForCapturedValue(capturedValue.owner, generatedNames),
Visibilities.PRIVATE,
classDeclaration,
capturedValue.owner.type
@@ -689,12 +690,22 @@ class LocalDeclarationsLowering(
}
}
private fun suggestNameForCapturedValue(declaration: IrValueDeclaration): Name =
if (declaration.name.isSpecial) {
val oldNameStr = declaration.name.asString()
oldNameStr.substring(1, oldNameStr.length - 1).synthesizedName
private fun Name.stripSpecialMarkers(): String =
if (isSpecial) asString().substring(1, asString().length - 1) else asString()
private fun suggestNameForCapturedValue(declaration: IrValueDeclaration, existing: MutableSet<Name>): Name {
val base = if (declaration.name.isSpecial) {
val oldName = declaration.name.stripSpecialMarkers()
val parentName = (declaration.parent as? IrDeclarationWithName)?.name?.stripSpecialMarkers()
if (parentName != null) "$oldName$$parentName" else oldName
} else
declaration.name.asString().synthesizedName
declaration.name.asString()
var chosen = base.synthesizedName
var suffix = 0
while (!existing.add(chosen))
chosen = "$base$${++suffix}".synthesizedName
return chosen
}
private fun collectClosureForLocalDeclarations() {
@@ -0,0 +1,6 @@
fun String.f(x: String): String {
fun String.g() = { this@f + this@g }()
return x.g()
}
fun box() = "O".f("K")
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// FILE: 1.kt
// SKIP_INLINE_CHECK_IN: inlineFun$default
package test
@@ -0,0 +1,11 @@
// FILE: 1.kt
inline fun f(g: () -> String) = g()
// FILE: 2.kt
class A(val x: String) {
inner class B(val y: String) {
fun h() = f { x + y }
}
}
fun box() = A("O").B("K").h()
@@ -19,4 +19,10 @@ fun box(): String {
// One instance of each is in kotlin.Metadata.d2
// 1 \(X\)
// JVM_TEMPLATES
// 1 \(Y\)
// JVM_IR_TEMPLATES
// 5 \(Y\)
// 4 \$this\$\(Y\)
@@ -3786,6 +3786,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiver.kt");
}
@TestMetadata("captureExtensionReceiverX2.kt")
public void testCaptureExtensionReceiverX2() throws Exception {
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiverX2.kt");
}
@TestMetadata("capturedLocalGenericFun.kt")
public void testCapturedLocalGenericFun() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedLocalGenericFun.kt");
@@ -1799,6 +1799,11 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/innerClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("captureThisAndOuter.kt")
public void testCaptureThisAndOuter() throws Exception {
runTest("compiler/testData/codegen/boxInline/innerClasses/captureThisAndOuter.kt");
}
@TestMetadata("innerLambda.kt")
public void testInnerLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/innerClasses/innerLambda.kt");
@@ -1799,6 +1799,11 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/innerClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("captureThisAndOuter.kt")
public void testCaptureThisAndOuter() throws Exception {
runTest("compiler/testData/codegen/boxInline/innerClasses/captureThisAndOuter.kt");
}
@TestMetadata("innerLambda.kt")
public void testInnerLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/innerClasses/innerLambda.kt");
@@ -3786,6 +3786,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiver.kt");
}
@TestMetadata("captureExtensionReceiverX2.kt")
public void testCaptureExtensionReceiverX2() throws Exception {
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiverX2.kt");
}
@TestMetadata("capturedLocalGenericFun.kt")
public void testCapturedLocalGenericFun() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedLocalGenericFun.kt");
@@ -3766,6 +3766,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiver.kt");
}
@TestMetadata("captureExtensionReceiverX2.kt")
public void testCaptureExtensionReceiverX2() throws Exception {
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiverX2.kt");
}
@TestMetadata("capturedLocalGenericFun.kt")
public void testCapturedLocalGenericFun() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedLocalGenericFun.kt");
@@ -1799,6 +1799,11 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/innerClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@TestMetadata("captureThisAndOuter.kt")
public void testCaptureThisAndOuter() throws Exception {
runTest("compiler/testData/codegen/boxInline/innerClasses/captureThisAndOuter.kt");
}
@TestMetadata("innerLambda.kt")
public void testInnerLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/innerClasses/innerLambda.kt");
@@ -1799,6 +1799,11 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/innerClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@TestMetadata("captureThisAndOuter.kt")
public void testCaptureThisAndOuter() throws Exception {
runTest("compiler/testData/codegen/boxInline/innerClasses/captureThisAndOuter.kt");
}
@TestMetadata("innerLambda.kt")
public void testInnerLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/innerClasses/innerLambda.kt");
@@ -3121,6 +3121,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiver.kt");
}
@TestMetadata("captureExtensionReceiverX2.kt")
public void testCaptureExtensionReceiverX2() throws Exception {
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiverX2.kt");
}
@TestMetadata("capturedLocalGenericFun.kt")
public void testCapturedLocalGenericFun() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedLocalGenericFun.kt");
@@ -3121,6 +3121,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiver.kt");
}
@TestMetadata("captureExtensionReceiverX2.kt")
public void testCaptureExtensionReceiverX2() throws Exception {
runTest("compiler/testData/codegen/box/closures/captureExtensionReceiverX2.kt");
}
@TestMetadata("capturedLocalGenericFun.kt")
public void testCapturedLocalGenericFun() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedLocalGenericFun.kt");