From b4db23a7b371bc4871063b5ed359b1eab19d25b9 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Thu, 24 Dec 2015 12:45:44 +0300 Subject: [PATCH] Removed 'INSTANCE' field from companions --- .../codegen/ImplementationBodyCodegen.java | 37 +++++++++---------- .../kotlin/codegen/MemberCodegen.java | 2 +- .../ClassObjectField.java | 5 --- .../TraitClassObjectField.NoCompile.java | 4 -- .../publicField/CompanionObject.java | 5 --- .../bytecodeText/constValsGetterDeprecated.kt | 5 +-- .../tinyApp/outs/syntheticMethods.out | 1 - .../tinyApp/outs/syntheticMethodsSkip.out | 1 - 8 files changed, 20 insertions(+), 40 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java index 943c8b0f4d8..508491569ff 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java @@ -840,17 +840,16 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { } private void generateFieldForSingleton() { - if (isEnumEntry(descriptor)) return; + if (isEnumEntry(descriptor) || isCompanionObject(descriptor)) return; - boolean isCompanionObject = isCompanionObject(descriptor); - if (isNonCompanionObject(descriptor) || isCompanionObject) { + if (isNonCompanionObject(descriptor)) { StackValue.Field field = StackValue.singletonViaInstance(descriptor, typeMapper); v.newField(JvmDeclarationOriginKt.OtherOrigin(myClass), - ACC_PUBLIC | ACC_STATIC | ACC_FINAL | (isCompanionObject ? ACC_DEPRECATED : 0), + ACC_PUBLIC | ACC_STATIC | ACC_FINAL, field.name, field.type.getDescriptor(), null, null); if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return; - // Invoke the object constructor but ignore the result because INSTANCE$ will be initialized in the first line of + // Invoke the object constructor but ignore the result because INSTANCE will be initialized in the first line of InstructionAdapter v = createOrGetClInitCodegen().v; markLineNumberForElement(element, v); v.anew(classAsmType); @@ -869,12 +868,6 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { StackValue.Field field = StackValue.singleton(companionObjectDescriptor, typeMapper); v.newField(JvmDeclarationOriginKt.OtherOrigin(companionObject), ACC_PUBLIC | ACC_STATIC | ACC_FINAL, field.name, field.type.getDescriptor(), null, null); - - if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return; - - if (!isCompanionObjectWithBackingFieldsInOuter(companionObjectDescriptor)) { - generateCompanionObjectInitializer(companionObjectDescriptor); - } } private void generateCompanionObjectBackingFieldCopies() { @@ -926,13 +919,13 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { private void generateCompanionObjectInitializer(@NotNull ClassDescriptor companionObject) { ExpressionCodegen codegen = createOrGetClInitCodegen(); - //TODO: uncomment when DEPRECATED INSTANCE is removed - //FunctionDescriptor constructor = (FunctionDescriptor) context.accessibleDescriptor( - // CollectionsKt.single(companionObject.getConstructors()), /* superCallExpression = */ null - //); - //generateMethodCallTo(constructor, null, codegen.v); - //StackValue instance = StackValue.onStack(typeMapper.mapClass(companionObject)); - StackValue.singleton(companionObject, typeMapper).store(StackValue.singletonViaInstance(companionObject, typeMapper), codegen.v, true); + + FunctionDescriptor constructor = (FunctionDescriptor) context.accessibleDescriptor( + CollectionsKt.single(companionObject.getConstructors()), /* superCallExpression = */ null + ); + generateMethodCallTo(constructor, null, codegen.v); + StackValue instance = StackValue.onStack(typeMapper.mapClass(companionObject)); + StackValue.singleton(companionObject, typeMapper).store(instance, codegen.v, true); } private void generatePrimaryConstructor(final DelegationFieldsInfo delegationFieldsInfo) { @@ -1002,7 +995,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { generateDelegatorToConstructorCall(iv, codegen, constructorDescriptor, getDelegationConstructorCall(bindingContext, constructorDescriptor)); - if (isObject(descriptor)) { + if (isNonCompanionObject(descriptor)) { StackValue.singletonViaInstance(descriptor, typeMapper).store(StackValue.LOCAL_0, iv); } @@ -1027,9 +1020,13 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { curParam++; } + if (isCompanionObject(descriptor)) { + ImplementationBodyCodegen parentCodegen = (ImplementationBodyCodegen) getParentCodegen(); + parentCodegen.generateCompanionObjectInitializer(descriptor); + } + if (isCompanionObjectWithBackingFieldsInOuter(descriptor)) { final ImplementationBodyCodegen parentCodegen = (ImplementationBodyCodegen) getParentCodegen(); - parentCodegen.generateCompanionObjectInitializer(descriptor); generateInitializers(new Function0() { @Override public ExpressionCodegen invoke() { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java index d925d1a088e..91f5eea8a08 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java @@ -672,7 +672,7 @@ public abstract class MemberCodegen