ClassObject instance field refactoring

This commit is contained in:
Mikhael Bogdanov
2013-03-04 11:40:56 +04:00
parent 5db0f2132c
commit b3ec87f956
19 changed files with 235 additions and 48 deletions
@@ -37,6 +37,7 @@ import org.jetbrains.jet.lang.resolve.java.AsmTypeConstants;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmPrimitiveType;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ClassReceiver;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.lexer.JetTokens;
@@ -312,10 +313,18 @@ public class AsmUtil {
}
public static void genInitSingletonField(Type classAsmType, InstructionAdapter iv) {
iv.anew(classAsmType);
genInitSingletonField(classAsmType, JvmAbi.INSTANCE_FIELD, classAsmType, iv);
}
public static void genInitSingletonField(FieldInfo info, InstructionAdapter iv) {
genInitSingletonField(info.getOwnerType(), info.getFieldName(), info.getFieldType(), iv);
}
public static void genInitSingletonField(Type fieldOwnerType, String fieldName, Type fieldAsmType, InstructionAdapter iv) {
iv.anew(fieldAsmType);
iv.dup();
iv.invokespecial(classAsmType.getInternalName(), "<init>", "()V");
iv.putstatic(classAsmType.getInternalName(), JvmAbi.INSTANCE_FIELD, classAsmType.getDescriptor());
iv.invokespecial(fieldAsmType.getInternalName(), "<init>", "()V");
iv.putstatic(fieldOwnerType.getInternalName(), fieldName, fieldAsmType.getDescriptor());
}
public static void genStringBuilderConstructor(InstructionAdapter v) {
@@ -2126,7 +2126,8 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
v.load(0, OBJECT_TYPE);
}
else {
v.getstatic(exprType.getInternalName(), JvmAbi.INSTANCE_FIELD, exprType.getDescriptor());
FieldInfo info = FieldInfo.createForSingleton(classReceiverDeclarationDescriptor, typeMapper);
v.getstatic(info.getOwnerInternalName(), info.getFieldName(), info.getFieldType().getDescriptor());
}
StackValue.onStack(exprType).put(type, v);
}
@@ -0,0 +1,81 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.jet.codegen.state.JetTypeMapper;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassKind;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
public class FieldInfo {
private final Type fieldType;
private final Type ownerType;
private final String fieldName;
private FieldInfo(@NotNull Type ownerType, @NotNull Type fieldType, @NotNull String fieldName) {
this.ownerType = ownerType;
this.fieldType = fieldType;
this.fieldName = fieldName;
}
@NotNull
public Type getFieldType() {
return fieldType;
}
@NotNull
public Type getOwnerType() {
return ownerType;
}
@NotNull
public String getOwnerInternalName() {
return ownerType.getInternalName();
}
@NotNull
public String getFieldName() {
return fieldName;
}
@NotNull
public static FieldInfo createForSingleton(@NotNull ClassDescriptor fieldClassDescriptor, @NotNull JetTypeMapper typeMapper) {
ClassKind kind = fieldClassDescriptor.getKind();
if (kind != ClassKind.OBJECT && kind != ClassKind.CLASS_OBJECT && kind != ClassKind.ENUM_ENTRY) {
throw new UnsupportedOperationException();
}
Type fieldType = typeMapper.mapType(fieldClassDescriptor.getDefaultType());
ClassDescriptor ownerDescriptor = kind == ClassKind.OBJECT
? fieldClassDescriptor: DescriptorUtils.getParentOfType(fieldClassDescriptor, ClassDescriptor.class);
assert ownerDescriptor != null;
Type ownerType = typeMapper.mapType(ownerDescriptor.getDefaultType());
String fieldName = kind == ClassKind.ENUM_ENTRY
? fieldClassDescriptor.getName().getName()
: fieldClassDescriptor.getKind() == ClassKind.CLASS_OBJECT ? JvmAbi.CLASS_OBJECT_FIELD : JvmAbi.INSTANCE_FIELD;
return new FieldInfo(ownerType, fieldType, fieldName);
}
}
@@ -283,6 +283,11 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
annotationVisitor.visit(JvmStdlibNames.ABI_VERSION_NAME, JvmAbi.VERSION);
annotationVisitor.visitEnd();
if (descriptor.getKind() == ClassKind.CLASS_OBJECT) {
AnnotationVisitor classObjectVisitor = v.newAnnotation(JvmStdlibNames.JET_CLASS_OBJECT.getDescriptor(), true);
classObjectVisitor.visitEnd();
}
}
private JvmClassSignature signature() {
@@ -912,14 +917,22 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
private void generateFieldForSingleton() {
if (!(isNonLiteralObject(myClass) || descriptor.getKind() == ClassKind.CLASS_OBJECT)) return;
boolean hasClassObject = descriptor.getClassObjectDescriptor() != null;
boolean isEnumClass = DescriptorUtils.isEnumClass(descriptor);
v.newField(myClass, ACC_PUBLIC | ACC_STATIC | ACC_FINAL, JvmAbi.INSTANCE_FIELD, classAsmType.getDescriptor(), null, null);
if (!(isNonLiteralObject(myClass) || hasClassObject) || isEnumClass) return;
ClassDescriptor fieldTypeDescriptor = hasClassObject ? descriptor.getClassObjectDescriptor() : descriptor;
assert fieldTypeDescriptor != null;
final FieldInfo info = FieldInfo.createForSingleton(fieldTypeDescriptor, typeMapper);
JetClassOrObject original = hasClassObject ? ((JetClass)myClass).getClassObject().getObjectDeclaration() : myClass;
v.newField(original, ACC_PUBLIC | ACC_STATIC | ACC_FINAL, info.getFieldName(), info.getFieldType().getDescriptor(), null, null);
staticInitializerChunks.add(new CodeChunk() {
@Override
public void generate(InstructionAdapter iv) {
genInitSingletonField(classAsmType, iv);
genInitSingletonField(info, iv);
}
});
}
@@ -23,14 +23,12 @@ 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;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.java.JvmPrimitiveType;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue;
@@ -130,7 +128,8 @@ public abstract class StackValue {
return new CollectionElement(type, getter, setter, codegen, state);
}
public static StackValue field(Type type, JvmClassName owner, String name, boolean isStatic) {
@NotNull
public static StackValue field(@NotNull Type type, @NotNull JvmClassName owner, @NotNull String name, boolean isStatic) {
return new Field(type, owner, name, isStatic);
}
@@ -340,20 +339,8 @@ public abstract class StackValue {
}
public static StackValue singleton(ClassDescriptor classDescriptor, JetTypeMapper typeMapper) {
final Type type = typeMapper.mapType(classDescriptor.getDefaultType());
final ClassKind kind = classDescriptor.getKind();
if (kind == ClassKind.CLASS_OBJECT || kind == ClassKind.OBJECT) {
return field(type, JvmClassName.byInternalName(type.getInternalName()), JvmAbi.INSTANCE_FIELD, 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();
}
FieldInfo info = FieldInfo.createForSingleton(classDescriptor, typeMapper);
return field(info.getFieldType(), JvmClassName.byInternalName(info.getOwnerInternalName()), info.getFieldName(), true);
}
private static class None extends StackValue {