KT-1136 proper generation of accessors

This commit is contained in:
Alex Tkachman
2012-01-25 13:29:16 +02:00
parent c9cba3e1cc
commit 4633e1c7f4
7 changed files with 144 additions and 90 deletions
@@ -40,19 +40,19 @@ public abstract class ClassBodyCodegen {
this.v = v; this.v = v;
} }
public final void generate(@Nullable HashMap<DeclarationDescriptor, DeclarationDescriptor> accessors) { public final void generate() {
generateDeclaration(); generateDeclaration();
generateClassBody(); generateClassBody();
generateSyntheticParts(accessors); generateSyntheticParts();
generateStaticInitializer(); generateStaticInitializer();
} }
protected abstract void generateDeclaration(); protected abstract void generateDeclaration();
protected void generateSyntheticParts(HashMap<DeclarationDescriptor, DeclarationDescriptor> accessors) { protected void generateSyntheticParts() {
} }
private void generateClassBody() { private void generateClassBody() {
@@ -51,11 +51,12 @@ public class ClassCodegen {
private void generateImplementation(CodegenContext context, JetClassOrObject aClass, OwnerKind kind, HashMap<DeclarationDescriptor, DeclarationDescriptor> accessors, ClassBuilder classBuilder) { private void generateImplementation(CodegenContext context, JetClassOrObject aClass, OwnerKind kind, HashMap<DeclarationDescriptor, DeclarationDescriptor> accessors, ClassBuilder classBuilder) {
ClassDescriptor descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass); ClassDescriptor descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass);
CodegenContext classContext = context.intoClass(descriptor, kind, state.getTypeMapper()); CodegenContext classContext = context.intoClass(descriptor, kind, state.getTypeMapper());
new ImplementationBodyCodegen(aClass, classContext, classBuilder, state).generate(accessors); classContext.copyAccessors(accessors);
new ImplementationBodyCodegen(aClass, classContext, classBuilder, state).generate();
if(aClass instanceof JetClass && ((JetClass)aClass).isTrait()) { if(aClass instanceof JetClass && ((JetClass)aClass).isTrait()) {
ClassBuilder traitBuilder = state.forTraitImplementation(descriptor); ClassBuilder traitBuilder = state.forTraitImplementation(descriptor);
new TraitImplBodyCodegen(aClass, context.intoClass(descriptor, OwnerKind.TRAIT_IMPL, state.getTypeMapper()), traitBuilder, state).generate(null); new TraitImplBodyCodegen(aClass, context.intoClass(descriptor, OwnerKind.TRAIT_IMPL, state.getTypeMapper()), traitBuilder, state).generate();
traitBuilder.done(); traitBuilder.done();
} }
} }
@@ -264,6 +264,15 @@ public abstract class CodegenContext {
public abstract boolean isStatic(); public abstract boolean isStatic();
public void copyAccessors(HashMap<DeclarationDescriptor, DeclarationDescriptor> accessors) {
if(accessors != null) {
if(this.accessors == null) {
this.accessors = new HashMap<DeclarationDescriptor,DeclarationDescriptor>();
}
this.accessors.putAll(accessors);
}
}
public abstract static class ReceiverContext extends CodegenContext { public abstract static class ReceiverContext extends CodegenContext {
final CallableDescriptor receiverDescriptor; final CallableDescriptor receiverDescriptor;
@@ -145,7 +145,7 @@ public class GenerationState {
closure.name = nameAndVisitor.getFirst(); closure.name = nameAndVisitor.getFirst();
final CodegenContext objectContext = closure.context.intoAnonymousClass(closure, getBindingContext().get(BindingContext.CLASS, objectDeclaration), OwnerKind.IMPLEMENTATION, typeMapper); final CodegenContext objectContext = closure.context.intoAnonymousClass(closure, getBindingContext().get(BindingContext.CLASS, objectDeclaration), OwnerKind.IMPLEMENTATION, typeMapper);
new ImplementationBodyCodegen(objectDeclaration, objectContext, nameAndVisitor.getSecond(), this).generate(null); new ImplementationBodyCodegen(objectDeclaration, objectContext, nameAndVisitor.getSecond(), this).generate();
ConstructorDescriptor constructorDescriptor = closure.state.getBindingContext().get(BindingContext.CONSTRUCTOR, objectDeclaration); ConstructorDescriptor constructorDescriptor = closure.state.getBindingContext().get(BindingContext.CONSTRUCTOR, objectDeclaration);
CallableMethod callableMethod = closure.state.getTypeMapper().mapToCallableMethod(constructorDescriptor, OwnerKind.IMPLEMENTATION); CallableMethod callableMethod = closure.state.getTypeMapper().mapToCallableMethod(constructorDescriptor, OwnerKind.IMPLEMENTATION);
@@ -206,10 +206,9 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
} }
@Override @Override
protected void generateSyntheticParts(HashMap<DeclarationDescriptor, DeclarationDescriptor> accessors) { protected void generateSyntheticParts() {
generateFieldForObjectInstance(); generateFieldForObjectInstance();
generateFieldForClassObject(); generateFieldForClassObject();
generateAccessors(accessors);
try { try {
generatePrimaryConstructor(); generatePrimaryConstructor();
@@ -222,11 +221,19 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
} }
generateGetTypeInfo(); generateGetTypeInfo();
generateAccessors();
} }
private void generateAccessors(HashMap<DeclarationDescriptor, DeclarationDescriptor> accessors) { private void generateAccessors() {
if(accessors != null) { if(context.accessors != null) {
for (Map.Entry<DeclarationDescriptor, DeclarationDescriptor> entry : accessors.entrySet()) { for (Map.Entry<DeclarationDescriptor, DeclarationDescriptor> entry : context.accessors.entrySet()) {
genAccessor(entry);
}
}
}
private void genAccessor(Map.Entry<DeclarationDescriptor, DeclarationDescriptor> entry) {
if(entry.getValue() instanceof FunctionDescriptor) { if(entry.getValue() instanceof FunctionDescriptor) {
FunctionDescriptor bridge = (FunctionDescriptor) entry.getValue(); FunctionDescriptor bridge = (FunctionDescriptor) entry.getValue();
FunctionDescriptor original = (FunctionDescriptor) entry.getKey(); FunctionDescriptor original = (FunctionDescriptor) entry.getKey();
@@ -280,8 +287,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
} }
} }
if(bridge.isVar())
{ {
Method method = typeMapper.mapSetterSignature(bridge, OwnerKind.IMPLEMENTATION).getJvmMethodSignature().getAsmMethod(); Method method = typeMapper.mapSetterSignature(bridge, OwnerKind.IMPLEMENTATION).getJvmMethodSignature().getAsmMethod();
JvmPropertyAccessorSignature originalSignature2 = typeMapper.mapSetterSignature(original, OwnerKind.IMPLEMENTATION); JvmPropertyAccessorSignature originalSignature2 = typeMapper.mapSetterSignature(original, OwnerKind.IMPLEMENTATION);
Method originalMethod = originalSignature2.getJvmMethodSignature().getAsmMethod(); Method originalMethod = originalSignature2.getJvmMethodSignature().getAsmMethod();
@@ -314,8 +321,6 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
} }
}
}
private void generateFieldForObjectInstance() { private void generateFieldForObjectInstance() {
if (isNonLiteralObject()) { if (isNonLiteralObject()) {
@@ -0,0 +1,35 @@
import java.util.ArrayList
public class SomeClass() {
class Inner {
val copy = list
}
private val list = ArrayList<String>()
var status : Throwable? = null
private val workerThread = object : Thread() {
public override fun run() {
try {
list.add("123")
list.add("33")
Inner().copy.add("444")
}
catch(t: Throwable) {
status = t
}
}
}
{
workerThread.start()
workerThread.join()
}
}
public fun box():String {
var obj = SomeClass()
return if(obj.status == null) "OK" else {
obj.status?.printStackTrace()
"failed"
}
}
@@ -30,4 +30,8 @@ public class ObjectGenTest extends CodegenTestCase {
public void testKt640() throws Exception { public void testKt640() throws Exception {
blackBoxFile("regressions/kt640.jet"); blackBoxFile("regressions/kt640.jet");
} }
public void testKt1136() throws Exception {
blackBoxFile("regressions/kt1136.kt");
}
} }