Instance field generation in objects

This commit is contained in:
Michael Bogdanov
2015-10-08 12:22:45 +03:00
parent 53ced57c42
commit 60d1736b97
25 changed files with 87 additions and 55 deletions
@@ -17,37 +17,34 @@
package org.jetbrains.kotlin.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.CompanionObjectMapping;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.codegen.state.JetTypeMapper;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.load.java.JvmAbi;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform;
import org.jetbrains.org.objectweb.asm.Type;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isNonCompanionObject;
public class FieldInfo {
private static final CompanionObjectMapping COMPANION_OBJECT_MAPPING = new CompanionObjectMapping(JvmPlatform.INSTANCE$.getBuiltIns());
@NotNull
public static FieldInfo createForCompanionSingleton(@NotNull ClassDescriptor companionObject, @NotNull JetTypeMapper typeMapper) {
ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(companionObject, ClassDescriptor.class);
assert ownerDescriptor != null : "Owner not found for class: " + companionObject;
Type ownerType = typeMapper.mapType(ownerDescriptor);
return new FieldInfo(ownerType, typeMapper.mapType(companionObject), companionObject.getName().asString(), true);
}
@NotNull
public static FieldInfo createForSingleton(@NotNull ClassDescriptor classDescriptor, @NotNull JetTypeMapper typeMapper) {
return createForSingleton(classDescriptor, typeMapper, false);
}
@NotNull
public static FieldInfo createForSingleton(@NotNull ClassDescriptor classDescriptor, @NotNull JetTypeMapper typeMapper, boolean oldSingleton) {
if (!classDescriptor.getKind().isSingleton()) {
throw new UnsupportedOperationException("Can't create singleton field for class: " + classDescriptor);
}
if (isNonCompanionObject(classDescriptor) || COMPANION_OBJECT_MAPPING.hasMappingToObject(classDescriptor)) {
Type type = typeMapper.mapType(classDescriptor);
return new FieldInfo(type, type, JvmAbi.INSTANCE_FIELD, true);
}
else {
ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class);
assert ownerDescriptor != null : "Owner not found for class: " + classDescriptor;
Type ownerType = typeMapper.mapType(ownerDescriptor);
return new FieldInfo(ownerType, typeMapper.mapType(classDescriptor), classDescriptor.getName().asString(), true);
}
Type type = typeMapper.mapType(classDescriptor);
return new FieldInfo(type, type, oldSingleton ? JvmAbi.DEPRECATED_INSTANCE_FIELD : JvmAbi.INSTANCE_FIELD, true);
}
@NotNull
@@ -984,12 +984,17 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
private void generateFieldForSingleton() {
if (isEnumEntry(descriptor) || isCompanionObject(descriptor)) return;
if (isEnumEntry(descriptor)) return;
if (isNonCompanionObject(descriptor)) {
if (isObject(descriptor)) {
StackValue.Field field = StackValue.singleton(descriptor, typeMapper);
v.newField(OtherOrigin(myClass), ACC_PUBLIC | ACC_STATIC | ACC_FINAL, field.name, field.type.getDescriptor(), null, null);
if (isNonCompanionObject(descriptor)) {
StackValue.Field oldField = StackValue.oldSingleton(descriptor, typeMapper);
v.newField(OtherOrigin(myClass), ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_DEPRECATED, oldField.name, oldField.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 <init>
@@ -1007,7 +1012,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
JetObjectDeclaration companionObject = CollectionsKt.firstOrNull(((JetClass) myClass).getCompanionObjects());
assert companionObject != null : "Companion object not found: " + myClass.getText();
StackValue.Field field = StackValue.singleton(companionObjectDescriptor, typeMapper);
StackValue.Field field = StackValue.singletonForCompanion(companionObjectDescriptor, typeMapper);
v.newField(OtherOrigin(companionObject), ACC_PUBLIC | ACC_STATIC | ACC_FINAL, field.name, field.type.getDescriptor(), null, null);
if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return;
@@ -1065,13 +1070,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
private void generateCompanionObjectInitializer(@NotNull ClassDescriptor companionObject) {
ExpressionCodegen codegen = createOrGetClInitCodegen();
FunctionDescriptor constructor = (FunctionDescriptor) context.accessibleDescriptor(
CollectionsKt.single(companionObject.getConstructors()), /* superCallExpression = */ null
);
generateMethodCallTo(constructor, null, codegen.v);
codegen.v.dup();
StackValue instance = StackValue.onStack(typeMapper.mapClass(companionObject));
StackValue.singleton(companionObject, typeMapper).store(instance, codegen.v, true);
StackValue.singletonForCompanion(companionObject, typeMapper)
.store(StackValue.singleton(companionObject, typeMapper), codegen.v, true);
}
private void generatePrimaryConstructor(final DelegationFieldsInfo delegationFieldsInfo) {
@@ -1138,8 +1138,11 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
generateDelegatorToConstructorCall(iv, codegen, constructorDescriptor,
getDelegationConstructorCall(bindingContext, constructorDescriptor));
if (isNonCompanionObject(descriptor)) {
if (isObject(descriptor)) {
StackValue.singleton(descriptor, typeMapper).store(StackValue.LOCAL_0, iv);
if (isNonCompanionObject(descriptor)) {
StackValue.oldSingleton(descriptor, typeMapper).store(StackValue.LOCAL_0, iv);
}
}
for (JetDelegationSpecifier specifier : myClass.getDelegationSpecifiers()) {
@@ -163,7 +163,7 @@ public class PackageCodegen {
return Collections.emptyList();
}
List<DeserializedCallableMemberDescriptor> callables = Lists.newArrayList();
for (DeclarationDescriptor member : packageFragment.getMemberScope().getDescriptors(DescriptorKindFilter.CALLABLES, JetScope.ALL_NAME_FILTER)) {
for (DeclarationDescriptor member : packageFragment.getMemberScope().getDescriptors(DescriptorKindFilter.CALLABLES, JetScope.Companion.getALL_NAME_FILTER())) {
if (member instanceof DeserializedCallableMemberDescriptor) {
callables.add((DeserializedCallableMemberDescriptor) member);
}
@@ -576,6 +576,14 @@ public abstract class StackValue {
return field(FieldInfo.createForSingleton(classDescriptor, typeMapper));
}
public static Field singletonForCompanion(ClassDescriptor companionObject, JetTypeMapper typeMapper) {
return field(FieldInfo.createForCompanionSingleton(companionObject, typeMapper));
}
public static Field oldSingleton(ClassDescriptor classDescriptor, JetTypeMapper typeMapper) {
return field(FieldInfo.createForSingleton(classDescriptor, typeMapper, true));
}
public static StackValue operation(Type type, Function1<InstructionAdapter, Unit> lambda) {
return new OperationStackValue(type, lambda);
}
@@ -348,7 +348,7 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
@NotNull LazyPackageDescriptor current
) {
result.add(current);
for (FqName subPackage : packageFragmentProvider.getSubPackagesOf(current.getFqName(), JetScope.ALL_NAME_FILTER)) {
for (FqName subPackage : packageFragmentProvider.getSubPackagesOf(current.getFqName(), JetScope.Companion.getALL_NAME_FILTER())) {
LazyPackageDescriptor fragment = getPackageFragment(subPackage);
assert fragment != null : "Couldn't find fragment for " + subPackage;
collectAllPackages(result, fragment);
@@ -7,6 +7,8 @@ public final class ClassObjectField {
public ClassObjectField() { /* compiled code */ }
public static final class Companion {
public static final ClassObjectField.Companion INSTANCE;
@org.jetbrains.annotations.Nullable
public final java.lang.String getX() { /* compiled code */ }
@@ -5,8 +5,9 @@ public interface TraitClassObjectField {
static final class Companion {
@org.jetbrains.annotations.Nullable
private final java.lang.String x = "";
private final java.lang.String y = "";
private static final java.lang.String x = "";
private static final java.lang.String y = "";
public static final TraitClassObjectField.Companion INSTANCE;
/**
* @deprecated
@@ -6,6 +6,8 @@ public final class C {
public C() { /* compiled code */ }
public static final class Companion {
public static final C.Companion INSTANCE;
private final java.lang.String getFoo() { /* compiled code */ }
private Companion() { /* compiled code */ }
@@ -0,0 +1,4 @@
public class B {
public static int a = A.INSTANCE$.getC();
public static int b = A.INSTANCE$.foo();
}
@@ -0,0 +1,10 @@
import B
object A {
val c = 1
fun foo() = 4
}
fun box(): String {
return if (B.a == 1 && B.b == 4) "OK" else "${B.a} ${B.b}"
}
@@ -17,6 +17,6 @@ fun test() {
(::local)()
}
// 3 GETSTATIC ConstClosureOptimizationKt\$test\$1\.INSTANCE\$
// 1 GETSTATIC ConstClosureOptimizationKt\$test\$2\.INSTANCE\$
// 1 GETSTATIC ConstClosureOptimizationKt\$test\$3\.INSTANCE\$
// 3 GETSTATIC ConstClosureOptimizationKt\$test\$1\.INSTANCE
// 1 GETSTATIC ConstClosureOptimizationKt\$test\$2\.INSTANCE
// 1 GETSTATIC ConstClosureOptimizationKt\$test\$3\.INSTANCE
@@ -16,4 +16,4 @@ class B {
3 others are for getCONST_VAL
*/
// 5 DEPRECATED
// 6 DEPRECATED
@@ -4,4 +4,4 @@ class A {
}
}
// A and companion object constructor call
// 3 ALOAD 0
// 4 ALOAD 0
@@ -4,5 +4,5 @@ class A {
}
}
// A and companion object constructor call
// 3 ALOAD 0
// 4 ALOAD 0
// 1 synthetic access\$getR
@@ -2,5 +2,5 @@ object A {
val r: Int = 1
}
// Field initialized in constant pool
// A super constructor call and INSTANCE$ put
// 2 ALOAD 0
// A super constructor call, INSTANCE and INSTANCE$ put
// 3 ALOAD 0
@@ -53,6 +53,12 @@ public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodege
doTestWithJava(fileName);
}
@TestMetadata("deprecatedFieldForObject")
public void testDeprecatedFieldForObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithJava/deprecatedFieldForObject/");
doTestWithJava(fileName);
}
@TestMetadata("inline")
public void testInline() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithJava/inline/");
@@ -49,7 +49,8 @@ public final class JvmAbi {
public static final String PROPERTY_METADATA_ARRAY_NAME = "$propertyMetadata";
public static final String ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = "$annotations";
public static final String INSTANCE_FIELD = "INSTANCE$";
public static final String INSTANCE_FIELD = "INSTANCE";
public static final String DEPRECATED_INSTANCE_FIELD = "INSTANCE$";
public static final String KOTLIN_CLASS_FIELD_NAME = "$kotlinClass";
public static final String KOTLIN_PACKAGE_FIELD_NAME = "$kotlinPackage";
@@ -14,10 +14,8 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.descriptors
package org.jetbrains.kotlin.descriptors;
public interface SourceFile {
companion object {
val NO_SOURCE_FILE = object : SourceFile {}
}
SourceFile NO_SOURCE_FILE = new SourceFile() {};
}
@@ -61,6 +61,6 @@ public class AnnotationDescriptorImpl implements AnnotationDescriptor {
@Override
public String toString() {
return DescriptorRenderer.FQ_NAMES_IN_TYPES.renderAnnotation(this, null);
return DescriptorRenderer.Companion.getFQ_NAMES_IN_TYPES().renderAnnotation(this, null);
}
}
@@ -59,7 +59,7 @@ public abstract class DeclarationDescriptorImpl extends AnnotatedImpl implements
@NotNull
public static String toString(@NotNull DeclarationDescriptor descriptor) {
try {
return DescriptorRenderer.DEBUG_TEXT.render(descriptor) +
return DescriptorRenderer.Companion.getDEBUG_TEXT().render(descriptor) +
"[" + descriptor.getClass().getSimpleName() + "@" + Integer.toHexString(System.identityHashCode(descriptor)) + "]";
} catch (Throwable e) {
// DescriptionRenderer may throw if this is not yet completely initialized
@@ -624,7 +624,7 @@ public class DescriptorUtils {
result.add(fqName);
}
for (DeclarationDescriptor descriptor : packageView.getMemberScope().getDescriptors(DescriptorKindFilter.PACKAGES, JetScope.ALL_NAME_FILTER)) {
for (DeclarationDescriptor descriptor : packageView.getMemberScope().getDescriptors(DescriptorKindFilter.PACKAGES, JetScope.Companion.getALL_NAME_FILTER())) {
if (descriptor instanceof PackageViewDescriptor) {
getSubPackagesFqNames((PackageViewDescriptor) descriptor, result);
}
@@ -63,7 +63,7 @@ public abstract class AbstractJetType implements JetType {
for (AnnotationWithTarget annotationWithTarget : getAnnotations().getAllAnnotations()) {
sb.append("[");
sb.append(DescriptorRenderer.DEBUG_TEXT.renderAnnotation(
sb.append(DescriptorRenderer.Companion.getDEBUG_TEXT().renderAnnotation(
annotationWithTarget.getAnnotation(), annotationWithTarget.getTarget()));
sb.append("] ");
}
@@ -109,9 +109,9 @@ public abstract class BaseJetVariableMacro extends Macro {
private static Collection<DeclarationDescriptor> getAllVariables(JetScope scope) {
Collection<DeclarationDescriptor> result = ContainerUtil.newArrayList();
result.addAll(scope.getDescriptors(DescriptorKindFilter.VARIABLES, JetScope.ALL_NAME_FILTER));
result.addAll(scope.getDescriptors(DescriptorKindFilter.VARIABLES, JetScope.Companion.getALL_NAME_FILTER()));
for (ReceiverParameterDescriptor implicitReceiver : scope.getImplicitReceiversHierarchy()) {
result.addAll(implicitReceiver.getType().getMemberScope().getDescriptors(DescriptorKindFilter.VARIABLES, JetScope.ALL_NAME_FILTER));
result.addAll(implicitReceiver.getType().getMemberScope().getDescriptors(DescriptorKindFilter.VARIABLES, JetScope.Companion.getALL_NAME_FILTER()));
}
return result;
}
@@ -2,8 +2,8 @@ package test;
class C {
void foo() {
Utils.INSTANCE$.foo1(Utils.staticField);
Utils.staticField += Utils.INSTANCE$.foo2();
PureUtils.INSTANCE$.foo1(PureUtils.INSTANCE$.foo2())
Utils.INSTANCE.foo1(Utils.staticField);
Utils.staticField += Utils.INSTANCE.foo2();
PureUtils.INSTANCE.foo1(PureUtils.INSTANCE.foo2())
}
}
@@ -176,7 +176,7 @@ public class ManglingUtils {
if (jetScope != null) {
final String finalNameToCompare = nameToCompare;
Collection<DeclarationDescriptor> declarations = jetScope.getDescriptors(DescriptorKindFilter.CALLABLES, JetScope.ALL_NAME_FILTER);
Collection<DeclarationDescriptor> declarations = jetScope.getDescriptors(DescriptorKindFilter.CALLABLES, JetScope.Companion.getALL_NAME_FILTER());
List<CallableDescriptor> overloadedFunctions =
CollectionsKt.flatMap(declarations, new Function1<DeclarationDescriptor, Iterable<? extends CallableDescriptor>>() {
@Override