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 {
@@ -17,6 +17,8 @@
package org.jetbrains.jet.lang.resolve.java;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassKind;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -25,7 +27,7 @@ public class JvmAbi {
* This constant is used to identify binary format (class file) versions
* If you change class file metadata format and/or naming conventions, please increase this number
*/
public static final int VERSION = 2;
public static final int VERSION = 3;
public static final String TRAIT_IMPL_CLASS_NAME = "$TImpl";
public static final String TRAIT_IMPL_SUFFIX = "$" + TRAIT_IMPL_CLASS_NAME;
@@ -37,7 +39,8 @@ public class JvmAbi {
public static final String CLASS_OBJECT_CLASS_NAME = "object";
public static final String CLASS_OBJECT_SUFFIX = "$" + CLASS_OBJECT_CLASS_NAME;
public static final String INSTANCE_FIELD = "$instance";
public static final String INSTANCE_FIELD = "instance$";
public static final String CLASS_OBJECT_FIELD = "object$";
public static final String RECEIVER_PARAMETER = "$receiver";
public static final JvmClassName JETBRAINS_NOT_NULL_ANNOTATION =
@@ -70,6 +70,8 @@ public class JvmStdlibNames {
public static final JvmClassName JET_CLASS = JvmClassName.byFqNameWithoutInnerClasses("jet.runtime.typeinfo.JetClass");
public static final JvmClassName JET_CLASS_OBJECT = JvmClassName.byFqNameWithoutInnerClasses("jet.runtime.typeinfo.JetClassObject");
public static final JvmClassName JET_PACKAGE_CLASS = JvmClassName.byFqNameWithoutInnerClasses("jet.runtime.typeinfo.JetPackageClass");
public static final String ABI_VERSION_NAME = "abiVersion";
@@ -0,0 +1,44 @@
/*
* 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.lang.resolve.java.kt;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
import org.jetbrains.jet.lang.resolve.java.resolver.JavaAnnotationResolver;
public class JetClassObjectAnnotation extends PsiAnnotationWithAbiVersion {
private static final JetClassObjectAnnotation NULL_ANNOTATION = new JetClassObjectAnnotation(null);
static {
NULL_ANNOTATION.checkInitialized();
}
private JetClassObjectAnnotation(@Nullable PsiAnnotation psiAnnotation) {
super(psiAnnotation);
}
@NotNull
public static JetClassObjectAnnotation get(@NotNull PsiClass psiClass) {
PsiAnnotation annotation =
JavaAnnotationResolver.findOwnAnnotation(psiClass, JvmStdlibNames.JET_CLASS_OBJECT.getFqName().getFqName());
return annotation != null ? new JetClassObjectAnnotation(annotation) : NULL_ANNOTATION;
}
}
@@ -28,6 +28,7 @@ import org.jetbrains.jet.lang.resolve.java.DescriptorResolverUtils;
import org.jetbrains.jet.lang.resolve.java.JavaSemanticServices;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.descriptor.ClassDescriptorFromJvmBytecode;
import org.jetbrains.jet.lang.resolve.java.kt.JetClassObjectAnnotation;
import org.jetbrains.jet.lang.resolve.java.provider.ClassPsiDeclarationProvider;
import org.jetbrains.jet.lang.resolve.java.provider.PsiDeclarationProviderFactory;
import org.jetbrains.jet.lang.resolve.java.scope.JavaClassNonStaticMembersScope;
@@ -160,7 +161,7 @@ public final class JavaClassObjectResolver {
@Nullable
private static PsiClass getClassObjectPsiClass(@NotNull PsiClass ownerClass) {
for (PsiClass inner : ownerClass.getInnerClasses()) {
if (inner.getName().equals(JvmAbi.CLASS_OBJECT_CLASS_NAME)) {
if (JetClassObjectAnnotation.get(inner).isDefined()) {
return inner;
}
}
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.kt.JetClassObjectAnnotation;
import org.jetbrains.jet.lang.resolve.java.provider.ClassPsiDeclarationProvider;
import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -65,7 +66,7 @@ public final class JavaInnerClassResolver {
}
private static boolean shouldBeIgnored(PsiClass innerPsiClass) {
return innerPsiClass.getName().equals(JvmAbi.CLASS_OBJECT_CLASS_NAME);
return JetClassObjectAnnotation.get(innerPsiClass).isDefined();
}
@NotNull
+2 -2
View File
@@ -1,4 +1,4 @@
WARNING: $TESTDATA_DIR$/wrongAbiVersion.kt: (3, 9) Parameter 'x' is never used
ERROR: $TESTDATA_DIR$/wrongAbiVersionLib/wrong/WrongPackage.java: (3, 1) Class 'wrong.WrongPackage' was compiled with an incompatible version of Kotlin. Its ABI version is -1, expected ABI version is 2
ERROR: $TESTDATA_DIR$/wrongAbiVersionLib/ClassWithWrongAbiVersion.java: (3, 1) Class 'ClassWithWrongAbiVersion' was compiled with an incompatible version of Kotlin. Its ABI version is -1, expected ABI version is 2
ERROR: $TESTDATA_DIR$/wrongAbiVersionLib/wrong/WrongPackage.java: (3, 1) Class 'wrong.WrongPackage' was compiled with an incompatible version of Kotlin. Its ABI version is -1, expected ABI version is 3
ERROR: $TESTDATA_DIR$/wrongAbiVersionLib/ClassWithWrongAbiVersion.java: (3, 1) Class 'ClassWithWrongAbiVersion' was compiled with an incompatible version of Kotlin. Its ABI version is -1, expected ABI version is 3
COMPILATION_ERROR
@@ -0,0 +1,19 @@
var global = 0;
class C {
class object {
{
global = 1;
}
}
}
fun box(): String {
if (global != 0) {
return "fail1: global = $global"
}
val c = C()
if (global == 1) return "OK" else return "fail2: global = $global"
}
@@ -1,17 +1,17 @@
class ClassObject {
void accessToClassObject() {
WithClassObject.object.$instance.foo();
WithClassObject.object.$instance.getValue();
WithClassObject.object.$instance.getValueWithGetter();
WithClassObject.object.$instance.getVariable();
WithClassObject.object.$instance.setVariable(0);
WithClassObject.object.$instance.getVariableWithAccessors();
WithClassObject.object.$instance.setVariableWithAccessors(0);
WithClassObject.object$.foo();
WithClassObject.object$.getValue();
WithClassObject.object$.getValueWithGetter();
WithClassObject.object$.getVariable();
WithClassObject.object$.setVariable(0);
WithClassObject.object$.getVariableWithAccessors();
WithClassObject.object$.setVariableWithAccessors(0);
}
void accessToPackageObject() {
PackageInner.$instance.foo();
PackageInner.$instance.getValue();
PackageInner.instance$.foo();
PackageInner.instance$.getValue();
}
void accessToInnerClass() {
@@ -462,6 +462,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest("compiler/testData/codegen/box/classes/classObject.kt");
}
@TestMetadata("classObjectAsStaticInitializer.kt")
public void testClassObjectAsStaticInitializer() throws Exception {
doTest("compiler/testData/codegen/box/classes/classObjectAsStaticInitializer.kt");
}
@TestMetadata("classObjectField.kt")
public void testClassObjectField() throws Exception {
doTest("compiler/testData/codegen/box/classes/classObjectField.kt");
@@ -1,12 +1,12 @@
class ClassObject {
void foo() {
WithClassObject.object.$instance.getValue();
WithClassObject.object.$instance.getValue();
WithClassObject.object.$instance.foo();
WithClassObject.object.$instance.getValueWithGetter();
WithClassObject.object.$instance.getVariable();
WithClassObject.object.$instance.setVariable(0);
WithClassObject.object.$instance.getVariableWithAccessors();
WithClassObject.object.$instance.setVariableWithAccessors(0);
WithClassObject.object$.getValue();
WithClassObject.object$.getValue();
WithClassObject.object$.foo();
WithClassObject.object$.getValueWithGetter();
WithClassObject.object$.getVariable();
WithClassObject.object$.setVariable(0);
WithClassObject.object$.getVariableWithAccessors();
WithClassObject.object$.setVariableWithAccessors(0);
}
}
@@ -128,7 +128,7 @@ public class JetJavaFacadeTest extends LightCodeInsightFixtureTestCase {
assertEquals("foo.TheClass.object", classObjectClass.getQualifiedName());
assertTrue(classObjectClass.hasModifierProperty(PsiModifier.STATIC));
final PsiField instance = classObjectClass.findFieldByName(JvmAbi.INSTANCE_FIELD, false);
final PsiField instance = theClass.findFieldByName(JvmAbi.CLASS_OBJECT_FIELD, false);
assertNotNull(instance);
assertEquals("foo.TheClass.object", instance.getType().getCanonicalText());
assertTrue(instance.hasModifierProperty(PsiModifier.PUBLIC));
@@ -0,0 +1,21 @@
/*
* 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 jet.runtime.typeinfo;
public @interface JetClassObject {
}