className moved out of calculatedClosure/MutableClosure

This commit is contained in:
Alex Tkachman
2012-08-29 10:22:21 +03:00
parent 018a1f8c85
commit 5bf2831db7
8 changed files with 39 additions and 52 deletions
@@ -1042,7 +1042,9 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
assert constructorDescriptor != null;
CallableMethod constructor = typeMapper.mapToCallableMethod(constructorDescriptor, closure);
final JvmClassName name = closure.getClassName();
final JvmClassName name = bindingContext.get(FQN, constructorDescriptor.getContainingDeclaration());
assert name != null;
Type type = name.getAsmType();
v.anew(type);
v.dup();
@@ -98,7 +98,8 @@ public class ScriptCodegen {
(CodegenContext.ScriptContext) CodegenContext.STATIC
.intoScript(scriptDescriptor, classDescriptorForScript);
JvmClassName className = classNameForClassDescriptor(bindingContext, classDescriptorForScript);
JvmClassName className = bindingContext.get(FQN, classDescriptorForScript);
assert className != null;
ClassBuilder classBuilder = classFileFactory.newVisitor(className.getInternalName() + ".class");
classBuilder.defineClass(scriptDeclaration,
@@ -144,7 +145,8 @@ public class ScriptCodegen {
InstructionAdapter instructionAdapter = new InstructionAdapter(mv);
JvmClassName className = classNameForClassDescriptor(bindingContext, classDescriptorForScript);
JvmClassName className = bindingContext.get(FQN, classDescriptorForScript);
assert className != null;
instructionAdapter.load(0, className.getAsmType());
instructionAdapter.invokespecial("java/lang/Object", "<init>", "()V");
@@ -37,9 +37,6 @@ public interface CalculatedClosure {
@Nullable
ClassDescriptor getEnclosingClass();
@NotNull
JvmClassName getClassName();
@Nullable
JetDelegatorToSuperCall getSuperCall();
@@ -17,7 +17,6 @@
package org.jetbrains.jet.codegen.context;
import com.intellij.util.containers.Stack;
import org.jetbrains.jet.codegen.CodegenUtil;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.psi.*;
@@ -132,15 +131,10 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
final boolean trivial = enumEntry.getDeclarations().isEmpty();
if (!trivial) {
bindingTrace.record(ENUM_ENTRY_CLASS_NEED_SUBCLASS, descriptor);
classStack.push(descriptor);
nameStack.push(peekFromStack(nameStack) + "$" + descriptor.getName().getName());
super.visitEnumEntry(enumEntry);
nameStack.pop();
classStack.pop();
}
else {
super.visitEnumEntry(enumEntry);
bindingTrace.record(FQN, descriptor, bindingTrace.get(FQN, peekFromStack(classStack)));
}
}
@@ -215,7 +209,8 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
recordClosure(bindingTrace, expression.getObjectDeclaration(), classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), false);
classStack.push(classDescriptor);
nameStack.push(classNameForClassDescriptor(bindingContext, classDescriptor).getInternalName());
//noinspection ConstantConditions
nameStack.push(bindingContext.get(FQN, classDescriptor).getInternalName());
super.visitObjectLiteralExpression(expression);
nameStack.pop();
classStack.pop();
@@ -77,9 +77,8 @@ public class CodegenBinding {
@NotNull
public static JvmClassName classNameForScriptDescriptor(BindingContext bindingContext, @NotNull ScriptDescriptor scriptDescriptor) {
final ClassDescriptor classDescriptor = bindingContext.get(CLASS_FOR_FUNCTION, scriptDescriptor);
final CalculatedClosure closure = bindingContext.get(CLOSURE, classDescriptor);
assert closure != null;
return closure.getClassName();
//noinspection ConstantConditions
return bindingContext.get(FQN, classDescriptor);
}
@NotNull
@@ -96,11 +95,6 @@ public class CodegenBinding {
return closure == null ? null : closure.getEnclosingClass();
}
public static JvmClassName classNameForClassDescriptor(BindingContext bindingContext, ClassDescriptor descriptor) {
final CalculatedClosure closure = bindingContext.get(CLOSURE, descriptor);
return closure == null ? null : closure.getClassName();
}
public static JvmClassName classNameForAnonymousClass(BindingContext bindingContext, JetElement expression) {
if (expression instanceof JetObjectLiteralExpression) {
JetObjectLiteralExpression jetObjectLiteralExpression = (JetObjectLiteralExpression) expression;
@@ -113,7 +107,7 @@ public class CodegenBinding {
assert functionDescriptor != null;
descriptor = bindingContext.get(CLASS_FOR_FUNCTION, functionDescriptor);
}
return classNameForClassDescriptor(bindingContext, descriptor);
return bindingContext.get(FQN, descriptor);
}
public static void registerClassNameForScript(
@@ -174,8 +168,9 @@ public class CodegenBinding {
}
}
final MutableClosure closure = new MutableClosure(superCall, enclosing, name, enclosingReceiver);
final MutableClosure closure = new MutableClosure(superCall, enclosing, enclosingReceiver);
bindingTrace.record(FQN, classDescriptor, name);
bindingTrace.record(CLOSURE, classDescriptor, closure);
// TODO: this is temporary before we have proper inner classes
@@ -295,16 +290,8 @@ public class CodegenBinding {
}
}
}
else if (klass.getKind() == ClassKind.ENUM_ENTRY) {
if (enumEntryNeedSubclass(bindingTrace.getBindingContext(), klass)) {
return getJvmInternalName(bindingTrace, klass.getContainingDeclaration()).getInternalName() + "$" + klass.getName().getName();
}
else {
return getJvmInternalName(bindingTrace, klass.getContainingDeclaration()).getInternalName();
}
}
JvmClassName name = classNameForClassDescriptor(bindingTrace.getBindingContext(), (ClassDescriptor) descriptor);
JvmClassName name = bindingTrace.getBindingContext().get(FQN, descriptor);
if (name != null) {
return name.getInternalName();
}
@@ -22,6 +22,7 @@ import org.jetbrains.asm4.Type;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
@@ -32,6 +33,7 @@ import java.util.Map;
import static org.jetbrains.jet.codegen.JetTypeMapper.OBJECT_TYPE;
import static org.jetbrains.jet.codegen.context.CodegenBinding.CLASS_FOR_FUNCTION;
import static org.jetbrains.jet.codegen.context.CodegenBinding.FQN;
/*
* @author max
@@ -275,7 +277,7 @@ public abstract class CodegenContext {
for (LocalLookup.LocalLookupCase aCase : LocalLookup.LocalLookupCase.values()) {
if (aCase.isCase(d, state)) {
StackValue innerValue = aCase.innerValue(d, enclosingLocalLookup, state, closure);
StackValue innerValue = aCase.innerValue(d, enclosingLocalLookup, state, closure, state.getBindingContext().get(FQN, getThisDescriptor()));
if (innerValue == null) {
break;
}
@@ -41,7 +41,12 @@ public interface LocalLookup {
}
@Override
public StackValue innerValue(DeclarationDescriptor d, LocalLookup localLookup, GenerationState state, MutableClosure closure) {
public StackValue innerValue(
DeclarationDescriptor d,
LocalLookup localLookup,
GenerationState state,
MutableClosure closure, JvmClassName className
) {
VariableDescriptor vd = (VariableDescriptor) d;
final boolean idx = localLookup != null && localLookup.lookupLocal(vd);
@@ -53,8 +58,8 @@ public interface LocalLookup {
final String fieldName = "$" + vd.getName();
StackValue innerValue = sharedVarType != null
? StackValue.fieldForSharedVar(localType, closure.getClassName(), fieldName)
: StackValue.field(type, closure.getClassName(), fieldName, false);
? StackValue.fieldForSharedVar(localType, className, fieldName)
: StackValue.field(type, className, fieldName, false);
closure.recordField(fieldName, type);
closure.captureVariable(new EnclosedValueDescriptor(d, innerValue, type));
@@ -70,7 +75,13 @@ public interface LocalLookup {
}
@Override
public StackValue innerValue(DeclarationDescriptor d, LocalLookup localLookup, GenerationState state, MutableClosure closure) {
public StackValue innerValue(
DeclarationDescriptor d,
LocalLookup localLookup,
GenerationState state,
MutableClosure closure,
JvmClassName className
) {
FunctionDescriptor vd = (FunctionDescriptor) d;
final boolean idx = localLookup.lookupLocal(vd);
@@ -81,7 +92,7 @@ public interface LocalLookup {
Type localType = cn.getAsmType();
final String fieldName = "$" + vd.getName();
StackValue innerValue = StackValue.field(localType, closure.getClassName(), fieldName, false);
StackValue innerValue = StackValue.field(localType, className, fieldName, false);
closure.recordField(fieldName, localType);
closure.captureVariable(new EnclosedValueDescriptor(d, innerValue, localType));
@@ -101,13 +112,13 @@ public interface LocalLookup {
DeclarationDescriptor d,
LocalLookup enclosingLocalLookup,
GenerationState state,
MutableClosure closure
MutableClosure closure, JvmClassName className
) {
if (closure.getEnclosingReceiverDescriptor() != d) return null;
final JetType receiverType = ((CallableDescriptor) d).getReceiverParameter().getType();
Type type = state.getInjector().getJetTypeMapper().mapType(receiverType, MapTypeMode.VALUE);
StackValue innerValue = StackValue.field(type, closure.getClassName(), CodegenUtil.RECEIVER$0, false);
StackValue innerValue = StackValue.field(type, className, CodegenUtil.RECEIVER$0, false);
closure.setCaptureReceiver();
return innerValue;
@@ -126,7 +137,8 @@ public interface LocalLookup {
DeclarationDescriptor d,
LocalLookup localLookup,
GenerationState state,
MutableClosure closure
MutableClosure closure,
JvmClassName className
);
public StackValue outerValue(EnclosedValueDescriptor d, ExpressionCodegen expressionCodegen) {
@@ -38,8 +38,6 @@ public final class MutableClosure implements CalculatedClosure {
private final ClassDescriptor enclosingClass;
private final CallableDescriptor enclosingReceiverDescriptor;
private final JvmClassName name;
private boolean captureThis;
private boolean captureReceiver;
@@ -49,13 +47,11 @@ public final class MutableClosure implements CalculatedClosure {
MutableClosure(
JetDelegatorToSuperCall superCall,
ClassDescriptor enclosingClass,
JvmClassName className,
CallableDescriptor enclosingReceiverDescriptor
) {
this.superCall = superCall;
this.enclosingClass = enclosingClass;
this.enclosingReceiverDescriptor = enclosingReceiverDescriptor;
this.name = className;
}
@Nullable
@@ -64,12 +60,6 @@ public final class MutableClosure implements CalculatedClosure {
return enclosingClass;
}
@NotNull
@Override
public JvmClassName getClassName() {
return name;
}
@Override
public JetDelegatorToSuperCall getSuperCall() {
return superCall;