many bug fixes

This commit is contained in:
Alex Tkachman
2011-09-24 22:33:36 +03:00
parent 44e729fd0f
commit eba70a1e39
8 changed files with 59 additions and 13 deletions
@@ -21,9 +21,10 @@ public class ClassCodegen {
public void generate(ClassContext parentContext, JetClassOrObject aClass) {
GenerationState.prepareAnonymousClasses((JetElement) aClass, state.getTypeMapper());
ClassDescriptor descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass);
generateImplementation(parentContext, aClass, OwnerKind.IMPLEMENTATION);
ClassDescriptor descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass);
final ClassContext contextForInners = parentContext.intoClass(descriptor, OwnerKind.IMPLEMENTATION);
for (JetDeclaration declaration : aClass.getDeclarations()) {
if (declaration instanceof JetClass && !(declaration instanceof JetEnumEntry)) {
@@ -24,4 +24,34 @@ public class CodegenUtil {
public static boolean isInterface(JetType type) {
return isInterface(type.getConstructor().getDeclarationDescriptor());
}
public static boolean isClassObject(DeclarationDescriptor descriptor) {
if(descriptor instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;
if(classDescriptor.getKind() == ClassKind.OBJECT) {
if(classDescriptor.getContainingDeclaration() instanceof ClassDescriptor) {
ClassDescriptor containingDeclaration = (ClassDescriptor) classDescriptor.getContainingDeclaration();
if(classDescriptor.getDefaultType().equals(containingDeclaration.getClassObjectType())) {
return true;
}
}
}
}
return false;
}
public static boolean hasThis0(ClassDescriptor classDescriptor) {
return getOuterClassDescriptor(classDescriptor) != null && !isClassObject(classDescriptor);
}
public static ClassDescriptor getOuterClassDescriptor(ClassDescriptor descriptor) {
DeclarationDescriptor outerDescriptor = descriptor.getContainingDeclaration();
while(outerDescriptor != null) {
if(outerDescriptor instanceof ClassDescriptor)
break;
outerDescriptor = outerDescriptor.getContainingDeclaration();
}
return (ClassDescriptor) outerDescriptor;
}
}
@@ -22,7 +22,7 @@ public class ConstructorFrameMap extends FrameMap {
ClassDescriptor classDescriptor = null;
if (descriptor != null) {
classDescriptor = descriptor.getContainingDeclaration();
if (classDescriptor.getContainingDeclaration() instanceof ClassDescriptor) {
if (CodegenUtil.hasThis0(classDescriptor)) {
myOuterThisIndex = enterTemp(); // outer class instance
}
}
@@ -550,7 +550,9 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
Type type = Type.getObjectType(descriptor.getClassname());
v.anew(type);
v.dup();
v.invokespecial(descriptor.getClassname(), "<init>", descriptor.getConstructor().getDescriptor());
v.load(0, JetTypeMapper.TYPE_OBJECT);
String jvmDescriptor = descriptor.getConstructor().getDescriptor().replace("(","(" + typeMapper.jetImplementationType((ClassDescriptor) contextType()));
v.invokespecial(descriptor.getClassname(), "<init>", jvmDescriptor);
return StackValue.onStack(type);
}
@@ -905,6 +907,9 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
if (declarationPsiElement instanceof PsiMethod || declarationPsiElement instanceof JetNamedFunction) {
callableMethod = typeMapper.mapToCallableMethod((PsiNamedElement) declarationPsiElement, null);
}
else if (fd instanceof VariableAsFunctionDescriptor) {
callableMethod = ClosureCodegen.asCallableMethod((FunctionDescriptor) fd);
}
else if (fd instanceof FunctionDescriptor) {
callableMethod = ClosureCodegen.asCallableMethod((FunctionDescriptor) fd);
}
@@ -940,7 +945,8 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
}
if (callableMethod.needsReceiverOnStack()) {
if (receiver == StackValue.none()) {
receiver = thisExpression();
ClassDescriptor receiverClass = callableMethod.getReceiverClass();
receiver = receiverClass == null ? thisExpression() : generateThisOrOuter(receiverClass);
}
receiver.put(JetTypeMapper.TYPE_OBJECT, v);
}
@@ -374,8 +374,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (myClass.getParent() instanceof JetClassObject) {
return null;
}
final DeclarationDescriptor outerDescriptor = descriptor.getContainingDeclaration();
return outerDescriptor instanceof ClassDescriptor ? (ClassDescriptor) outerDescriptor : null;
return CodegenUtil.getOuterClassDescriptor(descriptor);
}
private void generateDelegatorToConstructorCall(InstructionAdapter iv, ExpressionCodegen codegen, JetCallElement constructorCall,
@@ -568,9 +568,8 @@ public class JetTypeMapper {
List<ValueParameterDescriptor> parameters = descriptor.getOriginal().getValueParameters();
List<Type> parameterTypes = new ArrayList<Type>();
ClassDescriptor classDescriptor = descriptor.getContainingDeclaration();
final DeclarationDescriptor outerDescriptor = classDescriptor.getContainingDeclaration();
if (outerDescriptor instanceof ClassDescriptor && classDescriptor.getKind() != ClassKind.OBJECT) {
parameterTypes.add(jvmType((ClassDescriptor) outerDescriptor, OwnerKind.IMPLEMENTATION));
if (CodegenUtil.hasThis0(classDescriptor)) {
parameterTypes.add(jvmType(CodegenUtil.getOuterClassDescriptor(classDescriptor), OwnerKind.IMPLEMENTATION));
}
for (ValueParameterDescriptor parameter : parameters) {
final Type type = mapType(parameter.getOutType());
+13 -4
View File
@@ -2,17 +2,20 @@ trait ISized {
val size : Int
}
trait ReadOnlyArray<out T> : ISized, Iterable<T> {
trait ReadOnlyArray<out T> : ISized, java.lang.Iterable<T> {
fun get(index : Int) : T
override fun iterator () : Iterator<T> = object : Iterator<T> {
class MyIterator() : java.util.Iterator<T> {
private var index = 0
override fun hasNext() : Boolean = index < size
val next : T
get() = get(index++)
override fun next() : T = get(index++)
override fun remove () : Unit {}
}
override fun iterator() : java.util.Iterator<T> = MyIterator()
}
trait WriteOnlyArray<in T> : ISized {
@@ -40,5 +43,11 @@ fun box() : String {
a [0] = 10
a.set(1, 2, 13)
a [3] = 40
System.out?.println(a.iterator())
System.out?.println(a.iterator().hasNext())
// if(a.iterator() !is Iterator<Int>) return "fail";
// for(el in a) {
// System.out?.println(el)
// }
return "OK"
}
@@ -113,6 +113,7 @@ public class TypeInfoTest extends CodegenTestCase {
public void testClassObjectInTypeInfo() throws Exception {
loadFile();
System.out.println(generateToText());
Method foo = generateFunction();
JetObject jetObject = (JetObject) foo.invoke(null);
TypeInfo<?> typeInfo = jetObject.getTypeInfo();