JVM_IR, codegen: handle names for classes within local classes

This commit is contained in:
Georgy Bronnikov
2019-10-02 14:55:47 +03:00
parent cd251458bd
commit 4b5877f3b5
9 changed files with 61 additions and 26 deletions
@@ -46,9 +46,26 @@ class IrTypeMapper(private val context: JvmBackendContext) : KotlinTypeMapperBas
error("Unknown descriptor: $classifier")
}
fun classInternalName(irClass: IrClass): String =
context.getLocalClassInfo(irClass)?.internalName
?: JvmCodegenUtil.sanitizeNameIfNeeded(computeInternalName(irClass), context.state.languageVersionSettings)
fun classInternalName(irClass: IrClass): String {
context.getLocalClassInfo(irClass)?.internalName?.let { return it }
val className = SpecialNames.safeIdentifier(irClass.name).identifier
val internalName = when (val parent = irClass.parent) {
is IrPackageFragment -> {
val fqName = parent.fqName
val prefix = if (fqName.isRoot) "" else fqName.asString().replace('.', '/') + "/"
prefix + className
}
is IrClass -> {
classInternalName(parent) + "$" + className
}
else -> error(
"Local class should have its name computed in InventNamesForLocalClasses: ${irClass.fqNameWhenAvailable}\n" +
"Ensure that any lowering that transforms elements with local class info (classes, function references) " +
"invokes `copyAttributes` on the transformed element."
)
}
return JvmCodegenUtil.sanitizeNameIfNeeded(internalName, context.state.languageVersionSettings)
}
fun writeFormalTypeParameters(irParameters: List<IrTypeParameter>, sw: JvmSignatureWriter) {
if (sw.skipGenericSignature()) return
@@ -148,25 +165,6 @@ class IrTypeMapper(private val context: JvmBackendContext) : KotlinTypeMapperBas
}
}
private fun computeInternalName(klass: IrClass): String {
val className = SpecialNames.safeIdentifier(klass.name).identifier
return when (val parent = klass.parent) {
is IrPackageFragment -> {
val fqName = parent.fqName
val prefix = if (fqName.isRoot) "" else fqName.asString().replace('.', '/') + "/"
prefix + className
}
is IrClass -> {
computeInternalName(parent) + "$" + className
}
else -> error(
"Local class should have its name computed in InventNamesForLocalClasses: ${klass.fqNameWhenAvailable}\n" +
"Ensure that any lowering that transforms elements with local class info (classes, function references) " +
"invokes `copyAttributes` on the transformed element."
)
}
}
// Copied from KotlinTypeMapper.writeGenericType.
private fun JvmSignatureWriter.writeGenericType(type: IrSimpleType, asmType: Type, mode: TypeMappingMode) {
if (skipGenericSignature() || hasNothingInNonContravariantPosition(type) || type.arguments.isEmpty()) {
+16
View File
@@ -0,0 +1,16 @@
enum class E {
OK, NOT_OK
}
interface I {
fun f(e: E): String
}
val obj = object: I {
override fun f(e: E) = when(e) {
E.OK -> "OK"
E.NOT_OK -> "NOT OK"
}
}
fun box() = obj.f(E.OK)
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// FILE: test.kt
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// FILE: test.kt
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
@@ -10562,6 +10562,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/enum/valueof.kt");
}
@TestMetadata("whenInObject.kt")
public void testWhenInObject() throws Exception {
runTest("compiler/testData/codegen/box/enum/whenInObject.kt");
}
@TestMetadata("compiler/testData/codegen/box/enum/defaultCtor")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -10562,6 +10562,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/enum/valueof.kt");
}
@TestMetadata("whenInObject.kt")
public void testWhenInObject() throws Exception {
runTest("compiler/testData/codegen/box/enum/whenInObject.kt");
}
@TestMetadata("compiler/testData/codegen/box/enum/defaultCtor")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -9447,6 +9447,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/enum/valueof.kt");
}
@TestMetadata("whenInObject.kt")
public void testWhenInObject() throws Exception {
runTest("compiler/testData/codegen/box/enum/whenInObject.kt");
}
@TestMetadata("compiler/testData/codegen/box/enum/defaultCtor")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -8187,6 +8187,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/enum/valueof.kt");
}
@TestMetadata("whenInObject.kt")
public void testWhenInObject() throws Exception {
runTest("compiler/testData/codegen/box/enum/whenInObject.kt");
}
@TestMetadata("compiler/testData/codegen/box/enum/defaultCtor")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -9272,6 +9272,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/enum/valueof.kt");
}
@TestMetadata("whenInObject.kt")
public void testWhenInObject() throws Exception {
runTest("compiler/testData/codegen/box/enum/whenInObject.kt");
}
@TestMetadata("compiler/testData/codegen/box/enum/defaultCtor")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)