diff --git a/idea/src/org/jetbrains/jet/codegen/ClassBodyCodegen.java b/idea/src/org/jetbrains/jet/codegen/ClassBodyCodegen.java index 8b70b2f2ad5..cdf5e5d319f 100644 --- a/idea/src/org/jetbrains/jet/codegen/ClassBodyCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/ClassBodyCodegen.java @@ -8,6 +8,9 @@ import org.jetbrains.jet.lang.types.JetStandardLibrary; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.Opcodes; +import java.util.Collections; +import java.util.List; + /** * @author max * @author yole @@ -16,12 +19,12 @@ public abstract class ClassBodyCodegen { protected final BindingContext bindingContext; protected final JetStandardLibrary stdlib; protected final JetTypeMapper typeMapper; - protected final JetClass myClass; + protected final JetClassOrObject myClass; protected final OwnerKind kind; protected final ClassDescriptor descriptor; protected final ClassVisitor v; - public ClassBodyCodegen(BindingContext bindingContext, JetStandardLibrary stdlib, JetClass aClass, OwnerKind kind, ClassVisitor v) { + public ClassBodyCodegen(BindingContext bindingContext, JetStandardLibrary stdlib, JetClassOrObject aClass, OwnerKind kind, ClassVisitor v) { this.bindingContext = bindingContext; this.stdlib = stdlib; this.typeMapper = new JetTypeMapper(stdlib, bindingContext); @@ -47,7 +50,7 @@ public abstract class ClassBodyCodegen { } private void generateClassBody() { - final FunctionCodegen functionCodegen = new FunctionCodegen(myClass, v, stdlib, bindingContext); + final FunctionCodegen functionCodegen = new FunctionCodegen((JetDeclaration) myClass, v, stdlib, bindingContext); final PropertyCodegen propertyCodegen = new PropertyCodegen(v, stdlib, bindingContext, functionCodegen); for (JetDeclaration declaration : myClass.getDeclarations()) { @@ -63,7 +66,11 @@ public abstract class ClassBodyCodegen { } } - for (JetParameter p : myClass.getPrimaryConstructorParameters()) { + generatePrimaryConstructorProperties(propertyCodegen); + } + + private void generatePrimaryConstructorProperties(PropertyCodegen propertyCodegen) { + for (JetParameter p : getPrimaryConstructorParameters()) { if (p.getValOrVarNode() != null) { PropertyDescriptor propertyDescriptor = bindingContext.getPropertyDescriptor(p); if (propertyDescriptor != null) { @@ -80,4 +87,11 @@ public abstract class ClassBodyCodegen { } } + protected List getPrimaryConstructorParameters() { + if (myClass instanceof JetClass) { + return ((JetClass) myClass).getPrimaryConstructorParameters(); + } + return Collections.emptyList(); + } + } diff --git a/idea/src/org/jetbrains/jet/codegen/ClassCodegen.java b/idea/src/org/jetbrains/jet/codegen/ClassCodegen.java index 4bf390d8532..6595f171e96 100644 --- a/idea/src/org/jetbrains/jet/codegen/ClassCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/ClassCodegen.java @@ -3,7 +3,9 @@ package org.jetbrains.jet.codegen; import com.intellij.openapi.project.Project; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.psi.JetClass; +import org.jetbrains.jet.lang.psi.JetClassOrObject; import org.jetbrains.jet.lang.psi.JetDeclaration; +import org.jetbrains.jet.lang.psi.JetObjectDeclaration; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.JetStandardLibrary; import org.objectweb.asm.ClassVisitor; @@ -17,21 +19,22 @@ public class ClassCodegen { private final Project project; private final BindingContext bindingContext; private final Codegens factory; - private final JetTypeMapper typeMapper; public ClassCodegen(Project project, Codegens factory, BindingContext bindingContext) { this.project = project; this.factory = factory; this.bindingContext = bindingContext; - - final JetStandardLibrary standardLibrary = JetStandardLibrary.getJetStandardLibrary(project); - typeMapper = new JetTypeMapper(standardLibrary, bindingContext); } - public void generate(JetClass aClass) { - generateInterface(aClass); - generateImplementation(aClass, OwnerKind.IMPLEMENTATION); - generateImplementation(aClass, OwnerKind.DELEGATING_IMPLEMENTATION); + public void generate(JetClassOrObject aClass) { + if (aClass instanceof JetObjectDeclaration) { + generateImplementation(aClass, OwnerKind.IMPLEMENTATION); + } + else { + generateInterface(aClass); + generateImplementation(aClass, OwnerKind.IMPLEMENTATION); + generateImplementation(aClass, OwnerKind.DELEGATING_IMPLEMENTATION); + } for (JetDeclaration declaration : aClass.getDeclarations()) { if (declaration instanceof JetClass) { @@ -40,14 +43,16 @@ public class ClassCodegen { } } - private void generateInterface(JetClass aClass) { + private void generateInterface(JetClassOrObject aClass) { final ClassVisitor visitor = factory.forClassInterface(bindingContext.getClassDescriptor(aClass)); new InterfaceBodyCodegen(bindingContext, JetStandardLibrary.getJetStandardLibrary(project), aClass, visitor).generate(); } - private void generateImplementation(JetClass aClass, OwnerKind kind) { + private void generateImplementation(JetClassOrObject aClass, OwnerKind kind) { ClassDescriptor descriptor = bindingContext.getClassDescriptor(aClass); - ClassVisitor v = kind == OwnerKind.IMPLEMENTATION ? factory.forClassImplementation(descriptor) : factory.forClassDelegatingImplementation(descriptor); + ClassVisitor v = kind == OwnerKind.IMPLEMENTATION + ? factory.forClassImplementation(descriptor) + : factory.forClassDelegatingImplementation(descriptor); new ImplementationBodyCodegen(bindingContext, JetStandardLibrary.getJetStandardLibrary(project), aClass, kind, v).generate(); } diff --git a/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 85aabb3e97d..6b3650925f0 100644 --- a/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -597,6 +597,14 @@ public class ExpressionCodegen extends JetVisitor { v.arraylength(); myStack.push(StackValue.onStack(Type.INT_TYPE)); } + else if (declaration instanceof JetObjectDeclarationName) { + JetObjectDeclaration objectDeclaration = PsiTreeUtil.getParentOfType(declaration, JetObjectDeclaration.class); + ClassDescriptor classDescriptor = bindingContext.getClassDescriptor(objectDeclaration); + myStack.push(StackValue.field(typeMapper.jvmType(classDescriptor, OwnerKind.IMPLEMENTATION), + typeMapper.jvmName(classDescriptor, OwnerKind.IMPLEMENTATION), + "$instance", + true)); + } else { boolean isStatic = container instanceof NamespaceDescriptorImpl; final boolean directToField = expression.getReferencedNameElementType() == JetTokens.FIELD_IDENTIFIER; @@ -614,9 +622,10 @@ public class ExpressionCodegen extends JetVisitor { } public StackValue intermediateValueForProperty(PropertyDescriptor propertyDescriptor, final boolean directToField) { - boolean isStatic = propertyDescriptor.getContainingDeclaration() instanceof NamespaceDescriptorImpl; + DeclarationDescriptor containingDeclaration = propertyDescriptor.getContainingDeclaration(); + boolean isStatic = containingDeclaration instanceof NamespaceDescriptorImpl; final JetType outType = propertyDescriptor.getOutType(); - boolean isInsideClass = propertyDescriptor.getContainingDeclaration() == contextType; + boolean isInsideClass = containingDeclaration == contextType; Method getter; Method setter; if (directToField) { @@ -628,17 +637,18 @@ public class ExpressionCodegen extends JetVisitor { setter = isInsideClass && propertyDescriptor.getSetter() == null ? null : typeMapper.mapSetterSignature(propertyDescriptor); } - String fieldOwner; - String interfaceOwner; + String owner; + boolean isInterface; if (isInsideClass || isStatic) { - fieldOwner = interfaceOwner = typeMapper.getOwner(propertyDescriptor, contextKind); + owner = typeMapper.getOwner(propertyDescriptor, contextKind); + isInterface = false; } else { - fieldOwner = null; - interfaceOwner = typeMapper.getOwner(propertyDescriptor, OwnerKind.INTERFACE); + owner = typeMapper.getOwner(propertyDescriptor, OwnerKind.INTERFACE); + isInterface = !(containingDeclaration instanceof ClassDescriptor && ((ClassDescriptor) containingDeclaration).isObject()); } - return StackValue.property(propertyDescriptor.getName(), fieldOwner, interfaceOwner, typeMapper.mapType(outType), isStatic, getter, setter); + return StackValue.property(propertyDescriptor.getName(), owner, typeMapper.mapType(outType), isStatic, isInterface, getter, setter); } @Nullable diff --git a/idea/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java b/idea/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java index 47fcc302105..62a252d7c80 100644 --- a/idea/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java @@ -12,16 +12,15 @@ import org.objectweb.asm.Type; import org.objectweb.asm.commons.InstructionAdapter; import org.objectweb.asm.commons.Method; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; /** * @author max * @author yole */ public class ImplementationBodyCodegen extends ClassBodyCodegen { - public ImplementationBodyCodegen(BindingContext bindingContext, JetStandardLibrary stdlib, JetClass aClass, OwnerKind kind, ClassVisitor v) { + public ImplementationBodyCodegen(BindingContext bindingContext, JetStandardLibrary stdlib, JetClassOrObject aClass, + OwnerKind kind, ClassVisitor v) { super(bindingContext, stdlib, aClass, kind, v); } @@ -29,17 +28,25 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { protected void generateDeclaration() { String superClass = getSuperClass(); - final String defaultInterfaceName = JetTypeMapper.jvmNameForInterface(descriptor); + List interfaces = new ArrayList(); + interfaces.add("jet/JetObject"); + if (!(myClass instanceof JetObjectDeclaration)) { + interfaces.add(JetTypeMapper.jvmNameForInterface(descriptor)); + } v.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, - JetTypeMapper.jetJvmName(descriptor, kind), + jvmName(), null, superClass, - new String[]{"jet/JetObject", defaultInterfaceName} + interfaces.toArray(new String[interfaces.size()]) ); } - private String getSuperClass() { + private String jvmName() { + return JetTypeMapper.jetJvmName(descriptor, kind); + } + + protected String getSuperClass() { List delegationSpecifiers = myClass.getDelegationSpecifiers(); if (delegationSpecifiers.isEmpty()) return "java/lang/Object"; @@ -59,6 +66,11 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { int typeinfoStatic = descriptor.getTypeConstructor().getParameters().size() > 0 ? 0 : Opcodes.ACC_STATIC; v.visitField(Opcodes.ACC_PRIVATE | typeinfoStatic, "$typeInfo", "Ljet/typeinfo/TypeInfo;", null, null); + if (myClass instanceof JetObjectDeclaration) { + Type type = JetTypeMapper.jetImplementationType(descriptor); + v.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "$instance", type.getDescriptor(), null, null); + } + generateStaticInitializer(); try { @@ -71,17 +83,25 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { generateGetTypeInfo(); } - private void generatePrimaryConstructor() { - ConstructorDescriptor constructorDescriptor = bindingContext.getConstructorDescriptor(myClass); - if (constructorDescriptor == null) return; + protected void generatePrimaryConstructor() { + ConstructorDescriptor constructorDescriptor = bindingContext.getConstructorDescriptor((JetElement) myClass); + if (constructorDescriptor == null && !(myClass instanceof JetObjectDeclaration)) return; - Method method = typeMapper.mapConstructorSignature(constructorDescriptor, kind); + Method method; + if (myClass instanceof JetObjectDeclaration) { + method = new Method("", Type.VOID_TYPE, new Type[0]); + } + else { + method = typeMapper.mapConstructorSignature(constructorDescriptor, kind); + } int flags = Opcodes.ACC_PUBLIC; // TODO final MethodVisitor mv = v.visitMethod(flags, "", method.getDescriptor(), null, null); mv.visitCode(); Type[] argTypes = method.getArgumentTypes(); - List paramDescrs = constructorDescriptor.getUnsubstitutedValueParameters(); + List paramDescrs = constructorDescriptor != null + ? constructorDescriptor.getUnsubstitutedValueParameters() + : Collections.emptyList(); FrameMap frameMap = new FrameMap(); frameMap.enterTemp(); // this @@ -161,7 +181,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { int n = 0; for (JetDelegationSpecifier specifier : specifiers) { boolean delegateOnStack = specifier instanceof JetDelegatorToSuperCall && n > 0 || - specifier instanceof JetDelegatorByExpressionSpecifier ; + specifier instanceof JetDelegatorByExpressionSpecifier; if (delegateOnStack) { iv.load(0, classType); @@ -233,7 +253,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { generateInitializers(codegen, iv); int curParam = 0; - List constructorParameters = myClass.getPrimaryConstructorParameters(); + List constructorParameters = getPrimaryConstructorParameters(); for (JetParameter parameter : constructorParameters) { if (parameter.getValOrVarNode() != null) { VariableDescriptor descriptor = paramDescrs.get(curParam); @@ -250,7 +270,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { mv.visitEnd(); } - private void generateTypeInfoInitializer(int firstTypeParameter, int typeParamCount, InstructionAdapter iv) { + protected void generateTypeInfoInitializer(int firstTypeParameter, int typeParamCount, InstructionAdapter iv) { iv.load(0, JetTypeMapper.TYPE_OBJECT); iv.anew(JetTypeMapper.TYPE_TYPEINFO); iv.dup(); @@ -269,7 +289,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { iv.putfield(typeMapper.jvmName(descriptor, OwnerKind.IMPLEMENTATION), "$typeInfo", "Ljet/typeinfo/TypeInfo;"); } - private void generateInitializers(ExpressionCodegen codegen, InstructionAdapter iv) { + protected void generateInitializers(ExpressionCodegen codegen, InstructionAdapter iv) { for (JetDeclaration declaration : myClass.getDeclarations()) { if (declaration instanceof JetProperty) { final PropertyDescriptor propertyDescriptor = (PropertyDescriptor) bindingContext.getVariableDescriptor((JetProperty) declaration); @@ -289,7 +309,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { } } - private void generateDelegates(JetClass inClass, JetClass toClass, OwnerKind kind, Set overriden) { + protected void generateDelegates(JetClassOrObject inClass, JetClass toClass, OwnerKind kind, Set overriden) { final FunctionCodegen functionCodegen = new FunctionCodegen(toClass, v, stdlib, bindingContext); final PropertyCodegen propertyCodegen = new PropertyCodegen(v, stdlib, bindingContext, functionCodegen); @@ -318,7 +338,9 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { } private void generateStaticInitializer() { - if (descriptor.getTypeConstructor().getParameters().size() > 0) { + boolean needTypeInfo = descriptor.getTypeConstructor().getParameters().size() == 0; + boolean needInstance = myClass instanceof JetObjectDeclaration; + if (!needTypeInfo && !needInstance) { // we will have a dynamic type info field return; } @@ -327,8 +349,18 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { mv.visitCode(); InstructionAdapter v = new InstructionAdapter(mv); - ClassCodegen.newTypeInfo(v, Type.getObjectType(JetTypeMapper.jvmNameForInterface(descriptor))); - v.putstatic(JetTypeMapper.jvmNameForImplementation(descriptor), "$typeInfo", "Ljet/typeinfo/TypeInfo;"); + + if (needTypeInfo) { + ClassCodegen.newTypeInfo(v, typeMapper.jvmType(descriptor, OwnerKind.INTERFACE)); + v.putstatic(JetTypeMapper.jvmNameForImplementation(descriptor), "$typeInfo", "Ljet/typeinfo/TypeInfo;"); + } + if (needInstance) { + String name = jvmName(); + v.anew(Type.getObjectType(name)); + v.dup(); + v.invokespecial(name, "", "()V"); + v.putstatic(name, "$instance", JetTypeMapper.jetImplementationType(descriptor).getDescriptor()); + } mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); diff --git a/idea/src/org/jetbrains/jet/codegen/InterfaceBodyCodegen.java b/idea/src/org/jetbrains/jet/codegen/InterfaceBodyCodegen.java index d720062b7b4..5514043d665 100644 --- a/idea/src/org/jetbrains/jet/codegen/InterfaceBodyCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/InterfaceBodyCodegen.java @@ -3,7 +3,7 @@ package org.jetbrains.jet.codegen; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; -import org.jetbrains.jet.lang.psi.JetClass; +import org.jetbrains.jet.lang.psi.JetClassOrObject; import org.jetbrains.jet.lang.psi.JetDelegationSpecifier; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.JetStandardLibrary; @@ -19,7 +19,7 @@ import java.util.Set; * @author yole */ public class InterfaceBodyCodegen extends ClassBodyCodegen { - public InterfaceBodyCodegen(BindingContext bindingContext, JetStandardLibrary stdlib, JetClass aClass, ClassVisitor v) { + public InterfaceBodyCodegen(BindingContext bindingContext, JetStandardLibrary stdlib, JetClassOrObject aClass, ClassVisitor v) { super(bindingContext, stdlib, aClass, OwnerKind.INTERFACE, v); } diff --git a/idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java b/idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java index 2e1c58c79c9..de54ff43817 100644 --- a/idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java +++ b/idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java @@ -53,6 +53,9 @@ public class JetTypeMapper { } public static String jetJvmName(ClassDescriptor jetClass, OwnerKind kind) { + if (jetClass.isObject()) { + return jvmNameForImplementation(jetClass); + } if (kind == OwnerKind.INTERFACE) { return jvmNameForInterface(jetClass); } @@ -118,7 +121,13 @@ public class JetTypeMapper { } else if (descriptor.getContainingDeclaration() instanceof ClassDescriptor) { ClassDescriptor classDescriptor = (ClassDescriptor) descriptor.getContainingDeclaration(); - owner = jvmName(classDescriptor, kind instanceof OwnerKind.DelegateKind ? OwnerKind.INTERFACE : kind); + if (kind instanceof OwnerKind.DelegateKind) { + kind = OwnerKind.INTERFACE; + } + else if (classDescriptor.isObject()) { + kind = OwnerKind.IMPLEMENTATION; + } + owner = jvmName(classDescriptor, kind); } else { throw new UnsupportedOperationException("don't know how to generate owner for parent " + descriptor.getContainingDeclaration()); diff --git a/idea/src/org/jetbrains/jet/codegen/NamespaceCodegen.java b/idea/src/org/jetbrains/jet/codegen/NamespaceCodegen.java index f8bbb64d0a5..8f7b3489733 100644 --- a/idea/src/org/jetbrains/jet/codegen/NamespaceCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/NamespaceCodegen.java @@ -62,8 +62,8 @@ public class NamespaceCodegen { throw new RuntimeException("Failed to generate function " + declaration.getName(), e); } } - else if (declaration instanceof JetClass) { - classCodegen.generate((JetClass) declaration); + else if (declaration instanceof JetClassOrObject) { + classCodegen.generate((JetClassOrObject) declaration); } } } diff --git a/idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java b/idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java index 36b9876354a..545b113470a 100644 --- a/idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java +++ b/idea/src/org/jetbrains/jet/codegen/PropertyCodegen.java @@ -108,7 +108,7 @@ public class PropertyCodegen { } private static boolean isExternallyAccessible(JetProperty p) { - return p.hasModifier(JetTokens.PUBLIC_KEYWORD); + return !p.hasModifier(JetTokens.PRIVATE_KEYWORD); } private void generateSetter(JetProperty p, OwnerKind kind, PropertyDescriptor propertyDescriptor) { diff --git a/idea/src/org/jetbrains/jet/codegen/StackValue.java b/idea/src/org/jetbrains/jet/codegen/StackValue.java index 066cc83926c..6dbbe94b8a9 100644 --- a/idea/src/org/jetbrains/jet/codegen/StackValue.java +++ b/idea/src/org/jetbrains/jet/codegen/StackValue.java @@ -71,8 +71,8 @@ public abstract class StackValue { return new Field(type, owner, name, isStatic); } - public static StackValue property(String name, String fieldOwner, String interfaceOwner, Type type, boolean isStatic, Method getter, Method setter) { - return new Property(name, fieldOwner, interfaceOwner, getter, setter, isStatic, type); + public static StackValue property(String name, String owner, Type type, boolean isStatic, boolean isInterface, Method getter, Method setter) { + return new Property(name, owner, getter, setter, isStatic, isInterface, type); } private static void box(final Type type, final Type toType, InstructionAdapter v) { @@ -391,37 +391,37 @@ public abstract class StackValue { private final String name; private final Method getter; private final Method setter; + private final String owner; private final boolean isStatic; - private final String fieldOwner; - private final String interfaceOwner; + private final boolean isInterface; - public Property(String name, String fieldOwner, String interfaceOwner, Method getter, Method setter, boolean aStatic, Type type) { + public Property(String name, String owner, Method getter, Method setter, boolean aStatic, boolean isInterface, Type type) { super(type); this.name = name; - this.fieldOwner = fieldOwner; - this.interfaceOwner = interfaceOwner; + this.owner = owner; this.getter = getter; this.setter = setter; isStatic = aStatic; + this.isInterface = isInterface; } @Override public void put(Type type, InstructionAdapter v) { if (getter == null) { - v.visitFieldInsn(isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD, fieldOwner, name, this.type.getDescriptor()); + v.visitFieldInsn(isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD, owner, name, this.type.getDescriptor()); } else { - v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : fieldOwner == null ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, interfaceOwner, getter.getName(), getter.getDescriptor()); + v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, owner, getter.getName(), getter.getDescriptor()); } } @Override public void store(InstructionAdapter v) { if (setter == null) { - v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, fieldOwner, name, this.type.getDescriptor()); + v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, owner, name, this.type.getDescriptor()); } else { - v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : fieldOwner == null ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, interfaceOwner, setter.getName(), setter.getDescriptor()); + v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, owner, setter.getName(), setter.getDescriptor()); } } } diff --git a/idea/testData/codegen/objects/simpleObject.jet b/idea/testData/codegen/objects/simpleObject.jet new file mode 100644 index 00000000000..5b78c077a6e --- /dev/null +++ b/idea/testData/codegen/objects/simpleObject.jet @@ -0,0 +1,7 @@ +object A { + val x: Int = 610 +} + +fun box() : String { + return if (A.x != 610) "fail" else "OK" +} diff --git a/idea/tests/org/jetbrains/jet/codegen/CodegenTestCase.java b/idea/tests/org/jetbrains/jet/codegen/CodegenTestCase.java index 8e7f4f832a4..29d32ce1b0d 100644 --- a/idea/tests/org/jetbrains/jet/codegen/CodegenTestCase.java +++ b/idea/tests/org/jetbrains/jet/codegen/CodegenTestCase.java @@ -68,6 +68,9 @@ public abstract class CodegenTestCase extends LightCodeInsightFixtureTestCase { String actual; try { actual = blackBox(); + } catch (NoClassDefFoundError e) { + System.out.println(generateToText()); + throw e; } catch (Exception e) { System.out.println(generateToText()); throw e; diff --git a/idea/tests/org/jetbrains/jet/codegen/ObjectGenTest.java b/idea/tests/org/jetbrains/jet/codegen/ObjectGenTest.java new file mode 100644 index 00000000000..16e69b485cb --- /dev/null +++ b/idea/tests/org/jetbrains/jet/codegen/ObjectGenTest.java @@ -0,0 +1,10 @@ +package org.jetbrains.jet.codegen; + +/** + * @author yole + */ +public class ObjectGenTest extends CodegenTestCase { + public void testSimpleObject() throws Exception { + blackBoxFile("objects/simpleObject.jet"); + } +}