script parameters

still a lot of things to do
This commit is contained in:
Stepan Koltsov
2012-05-28 20:30:23 +04:00
parent 6244404344
commit 30e44fdc5f
49 changed files with 833 additions and 108 deletions
@@ -125,6 +125,10 @@ public abstract class CodegenContext {
return new CodegenContexts.ConstructorContext(descriptor, getContextKind(), this, typeMapper);
}
public CodegenContext intoScript(ScriptDescriptor script) {
return new CodegenContexts.ScriptContext(script, OwnerKind.IMPLEMENTATION, this, closure);
}
public CodegenContexts.ClosureContext intoClosure(FunctionDescriptor funDescriptor, ClassDescriptor classDescriptor, String internalClassName, ClosureCodegen closureCodegen, JetTypeMapper typeMapper) {
return new CodegenContexts.ClosureContext(funDescriptor, classDescriptor, this, closureCodegen, internalClassName, typeMapper);
}
@@ -118,6 +118,27 @@ public class CodegenContexts {
}
}
public static class ScriptContext extends CodegenContext {
public ScriptContext(
@NotNull DeclarationDescriptor contextType,
@NotNull OwnerKind contextKind,
@Nullable CodegenContext parentContext,
@Nullable ObjectOrClosureCodegen closureCodegen) {
super(contextType, contextKind, parentContext, closureCodegen);
}
@Override
protected ClassDescriptor getThisDescriptor() {
return null;
}
@Override
public boolean isStatic() {
throw new IllegalStateException();
}
}
public static class ClassContext extends CodegenContext {
public ClassContext(ClassDescriptor contextType, OwnerKind contextKind, CodegenContext parentContext, JetTypeMapper typeMapper) {
super(contextType, contextKind, parentContext, null);
@@ -38,6 +38,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.utils.Progress;
import org.objectweb.asm.commons.Method;
import java.util.List;
@@ -54,6 +55,10 @@ public class GenerationState {
private final ClassBuilderMode classBuilderMode;
// out parameter
private Method scriptConstructorMethod;
public GenerationState(Project project, ClassBuilderFactory builderFactory, AnalyzeExhaust analyzeExhaust, List<JetFile> files) {
this(project, builderFactory, Progress.DEAF, analyzeExhaust, files, CompilerSpecialMode.REGULAR);
}
@@ -92,6 +97,14 @@ public class GenerationState {
return classBuilderMode;
}
public void setScriptConstructorMethod(@NotNull Method scriptConstructorMethod) {
this.scriptConstructorMethod = scriptConstructorMethod;
}
public Method getScriptConstructorMethod() {
return scriptConstructorMethod;
}
public ClassBuilder forClassImplementation(ClassDescriptor aClass) {
return getFactory().newVisitor(getInjector().getJetTypeMapper().mapType(aClass.getDefaultType(), MapTypeMode.IMPL).getInternalName() + ".class");
}
@@ -871,6 +871,27 @@ public class JetTypeMapper {
return signatureWriter.makeJvmMethodSignature("<init>");
}
@NotNull
public JvmMethodSignature mapScriptSignature(@NotNull ScriptDescriptor script) {
BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD, false);
writeFormalTypeParameters(Collections.<TypeParameterDescriptor>emptyList(), signatureWriter);
signatureWriter.writeParametersStart();
for (ValueParameterDescriptor valueParameter : script.getValueParameters()) {
signatureWriter.writeParameterType(JvmMethodParameterKind.VALUE);
mapType(valueParameter.getType(), signatureWriter, MapTypeMode.VALUE);
signatureWriter.writeParameterTypeEnd();
}
signatureWriter.writeParametersEnd();
signatureWriter.writeVoidReturn();
return signatureWriter.makeJvmMethodSignature("<init>");
}
public CallableMethod mapToCallableMethod(ConstructorDescriptor descriptor, OwnerKind kind, boolean hasThis0) {
final JvmMethodSignature method = mapConstructorSignature(descriptor, hasThis0);
MapTypeMode mapTypeMode = ownerKindToMapTypeMode(kind);
@@ -883,6 +904,7 @@ public class JetTypeMapper {
return new CallableMethod(owner, owner, owner, method, INVOKESPECIAL);
}
public static int getAccessModifiers(MemberDescriptor p, int defaultFlags) {
DeclarationDescriptor declaration = p.getContainingDeclaration();
if(CodegenUtil.isInterface(declaration)) {
@@ -17,7 +17,9 @@
package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.codegen.signature.JvmMethodSignature;
import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.psi.JetScript;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.JdkNames;
@@ -60,7 +62,7 @@ public class ScriptCodegen {
public void generate(CodegenContext context, JetScript scriptDeclaration) {
public void generate(CodegenContext outerContext, JetScript scriptDeclaration) {
ScriptDescriptor scriptDescriptor = (ScriptDescriptor) state.getBindingContext().get(BindingContext.SCRIPT, scriptDeclaration);
ClassBuilder classBuilder = classFileFactory.newVisitor("Script.class");
classBuilder.defineClass(scriptDeclaration,
@@ -75,7 +77,12 @@ public class ScriptCodegen {
classBuilder.newField(null, Opcodes.ACC_PUBLIC, LAST_EXPRESSION_VALUE_FIELD_NAME, blockType.getDescriptor(), null, null);
MethodVisitor mv = classBuilder.newMethod(scriptDeclaration, Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
JvmMethodSignature jvmSignature = jetTypeMapper.mapScriptSignature(scriptDescriptor);
state.setScriptConstructorMethod(jvmSignature.getAsmMethod());
MethodVisitor mv = classBuilder.newMethod(
scriptDeclaration, Opcodes.ACC_PUBLIC, jvmSignature.getAsmMethod().getName(), jvmSignature.getAsmMethod().getDescriptor(), null, null);
mv.visitCode();
@@ -85,8 +92,23 @@ public class ScriptCodegen {
instructionAdapter.invokespecial(JdkNames.JL_OBJECT.getInternalName(), "<init>", "()V");
instructionAdapter.load(0, Type.getObjectType("Script"));
StackValue stackValue = new ExpressionCodegen(mv, new FrameMap(), Type.VOID_TYPE, context, state).gen(scriptDeclaration.getBlockExpression());
CodegenContext context = outerContext.intoScript(scriptDescriptor);
FrameMap frameMap = context.prepareFrame(jetTypeMapper);
Type[] argTypes = jvmSignature.getAsmMethod().getArgumentTypes();
int add = 0;
for (int i = 0; i < scriptDescriptor.getValueParameters().size(); i++) {
ValueParameterDescriptor parameter = scriptDescriptor.getValueParameters().get(i);
frameMap.enter(parameter, argTypes[i+add].getSize());
}
StackValue stackValue = new ExpressionCodegen(mv, frameMap, Type.VOID_TYPE, context, state).gen(scriptDeclaration.getBlockExpression());
if (stackValue.type != Type.VOID_TYPE) {
stackValue.put(stackValue.type, instructionAdapter);
instructionAdapter.putfield("Script", LAST_EXPRESSION_VALUE_FIELD_NAME, blockType.getDescriptor());
}