From 0a2f808ec0a5a19590e860af969109f9a4ebc972 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Wed, 12 Sep 2012 20:48:12 +0300 Subject: [PATCH] test case and fixes for object and classes inner for enum entry --- .../org/jetbrains/jet/codegen/StackValue.java | 36 +++++++++---------- .../testData/codegen/enum/entrywithinner.kt | 20 +++++++++++ .../jetbrains/jet/codegen/EnumGenTest.java | 5 ++- 3 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 compiler/testData/codegen/enum/entrywithinner.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java index 270dda1164c..ad439250016 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java @@ -23,6 +23,7 @@ import org.jetbrains.asm4.Label; import org.jetbrains.asm4.Type; import org.jetbrains.asm4.commons.InstructionAdapter; import org.jetbrains.asm4.commons.Method; +import org.jetbrains.jet.codegen.binding.CodegenBinding; import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethod; import org.jetbrains.jet.codegen.state.GenerationState; import org.jetbrains.jet.codegen.state.JetTypeMapper; @@ -355,8 +356,21 @@ public abstract class StackValue { return receiver; } - public static StackValue singleton(ClassDescriptor aClass, JetTypeMapper typeMapper) { - return new Singleton(aClass, typeMapper.mapType(aClass.getDefaultType(), JetTypeMapperMode.VALUE)); + public static StackValue singleton(ClassDescriptor classDescriptor, JetTypeMapper typeMapper) { + final Type type = typeMapper.mapType(classDescriptor.getDefaultType(), JetTypeMapperMode.VALUE); + + final ClassKind kind = classDescriptor.getKind(); + if (kind == ClassKind.CLASS_OBJECT || kind == ClassKind.OBJECT) { + return field(type, JvmClassName.byInternalName(type.getInternalName()), "$instance", true); + } + else if (kind == ClassKind.ENUM_ENTRY) { + final JvmClassName owner = typeMapper.getBindingContext() + .get(CodegenBinding.FQN, classDescriptor.getContainingDeclaration().getContainingDeclaration()); + return field(type, owner, classDescriptor.getName().getName(), true); + } + else { + throw new UnsupportedOperationException(); + } } private static class None extends StackValue { @@ -1322,22 +1336,4 @@ public abstract class StackValue { } } } - - public static class Singleton extends StackValue { - private final ClassDescriptor classDescriptor; - - protected Singleton(ClassDescriptor classDescriptor, @NotNull Type type) { - super(type); - this.classDescriptor = classDescriptor; - } - - @Override - public void put(Type type, InstructionAdapter v) { - final ClassKind kind = classDescriptor.getKind(); - if (kind == ClassKind.CLASS_OBJECT || kind == ClassKind.OBJECT) { - v.getstatic(this.type.getInternalName(), "$instance", this.type.getDescriptor()); - coerce(type, v); - } - } - } } diff --git a/compiler/testData/codegen/enum/entrywithinner.kt b/compiler/testData/codegen/enum/entrywithinner.kt new file mode 100644 index 00000000000..bed94d2dc0d --- /dev/null +++ b/compiler/testData/codegen/enum/entrywithinner.kt @@ -0,0 +1,20 @@ +package test + +fun box() = IssueState.DEFAULT.ToString() + IssueState.FIXED.ToString() + +enum class IssueState { + DEFAULT { + class D { + val k = ToString() + } + } + FIXED { + override fun ToString() = "K" + + object D { + val k = ToString() + } + } + + open fun ToString() : String = "O" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/EnumGenTest.java b/compiler/tests/org/jetbrains/jet/codegen/EnumGenTest.java index 2aa96c14f67..531cd7c4799 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/EnumGenTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/EnumGenTest.java @@ -150,6 +150,9 @@ public class EnumGenTest extends CodegenTestCase { public void testKt2350() { blackBoxFile("regressions/kt2350.kt"); - System.out.println(generateToText()); + } + + public void testEntryWithInner() { + blackBoxFile("enum/entrywithinner.kt"); } }