|
@@ -3,27 +3,34 @@ package org.openzen.zenscript.javabytecode.compiler.definitions;
|
3
|
3
|
import org.objectweb.asm.ClassWriter;
|
4
|
4
|
import org.objectweb.asm.Opcodes;
|
5
|
5
|
import org.objectweb.asm.Type;
|
|
6
|
+import org.openzen.zenscript.codemodel.FunctionParameter;
|
6
|
7
|
import org.openzen.zenscript.codemodel.definition.*;
|
7
|
8
|
import org.openzen.zenscript.codemodel.expression.Expression;
|
8
|
9
|
import org.openzen.zenscript.codemodel.expression.ExpressionVisitor;
|
|
10
|
+import org.openzen.zenscript.codemodel.generic.TypeParameter;
|
9
|
11
|
import org.openzen.zenscript.codemodel.member.ConstructorMember;
|
10
|
12
|
import org.openzen.zenscript.codemodel.member.EnumConstantMember;
|
11
|
13
|
import org.openzen.zenscript.codemodel.member.FieldMember;
|
12
|
14
|
import org.openzen.zenscript.codemodel.member.IDefinitionMember;
|
13
|
15
|
import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
|
|
16
|
+import org.openzen.zenscript.codemodel.statement.ReturnStatement;
|
|
17
|
+import org.openzen.zenscript.codemodel.statement.Statement;
|
14
|
18
|
import org.openzen.zenscript.codemodel.type.BasicTypeID;
|
|
19
|
+import org.openzen.zenscript.codemodel.type.ITypeID;
|
|
20
|
+import org.openzen.zenscript.javabytecode.JavaClassInfo;
|
15
|
21
|
import org.openzen.zenscript.javabytecode.JavaEnumInfo;
|
16
|
|
-import org.openzen.zenscript.javabytecode.compiler.JavaExpressionVisitor;
|
17
|
|
-import org.openzen.zenscript.javabytecode.compiler.JavaStatementVisitor;
|
18
|
|
-import org.openzen.zenscript.javabytecode.compiler.JavaTypeClassVisitor;
|
19
|
|
-import org.openzen.zenscript.javabytecode.compiler.JavaWriter;
|
|
22
|
+import org.openzen.zenscript.javabytecode.JavaMethodInfo;
|
|
23
|
+import org.openzen.zenscript.javabytecode.TestIsStaticInfo;
|
|
24
|
+import org.openzen.zenscript.javabytecode.compiler.*;
|
|
25
|
+
|
|
26
|
+import java.util.Iterator;
|
20
|
27
|
|
21
|
28
|
public class JavaDefinitionVisitor implements DefinitionVisitor<byte[]> {
|
22
|
29
|
|
23
|
30
|
|
24
|
|
- private final ClassWriter outerWriter;
|
|
31
|
+ private final JavaClassWriter outerWriter;
|
25
|
32
|
|
26
|
|
- public JavaDefinitionVisitor(ClassWriter outerWriter) {
|
|
33
|
+ public JavaDefinitionVisitor(JavaClassWriter outerWriter) {
|
27
|
34
|
|
28
|
35
|
this.outerWriter = outerWriter;
|
29
|
36
|
}
|
|
@@ -38,7 +45,7 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<byte[]> {
|
38
|
45
|
else
|
39
|
46
|
superType = Type.getType(definition.superType.accept(JavaTypeClassVisitor.INSTANCE));
|
40
|
47
|
|
41
|
|
- ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
|
|
48
|
+ JavaClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
|
42
|
49
|
|
43
|
50
|
//TODO: Calculate signature from generic parameters
|
44
|
51
|
//TODO: Interfaces?
|
|
@@ -151,7 +158,7 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<byte[]> {
|
151
|
158
|
clinitWriter.newArray(Type.getType("L" + definition.name + ";"));
|
152
|
159
|
|
153
|
160
|
for (IDefinitionMember member : definition.members) {
|
154
|
|
- if(!(member instanceof EnumConstantMember)) continue;
|
|
161
|
+ if (!(member instanceof EnumConstantMember)) continue;
|
155
|
162
|
final EnumConstantMember constantMember = (EnumConstantMember) member;
|
156
|
163
|
clinitWriter.dup();
|
157
|
164
|
clinitWriter.constant(constantMember.value);
|
|
@@ -194,6 +201,36 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<byte[]> {
|
194
|
201
|
|
195
|
202
|
@Override
|
196
|
203
|
public byte[] visitFunction(FunctionDefinition definition) {
|
|
204
|
+ final String signature = CompilerUtils.calcSign(definition.header, false);
|
|
205
|
+ final JavaWriter writer = new JavaWriter(outerWriter, true, CompilerUtils.calcAccess(definition.modifiers) | Opcodes.ACC_STATIC, definition.name, CompilerUtils.calcDesc(definition.header, false), signature, null);
|
|
206
|
+ final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(writer);
|
|
207
|
+ statementVisitor.start();
|
|
208
|
+ final Iterator<Statement> statementIterator = definition.statements.iterator();
|
|
209
|
+
|
|
210
|
+ //TODO this is dirty, fix when there is a way of knowing if a parameter is static or not
|
|
211
|
+ for (FunctionParameter parameter : definition.header.parameters) {
|
|
212
|
+ parameter.index -= 1;
|
|
213
|
+ }
|
|
214
|
+ while (statementIterator.hasNext()) {
|
|
215
|
+ final Statement statement = statementIterator.next();
|
|
216
|
+ statement.accept(statementVisitor);
|
|
217
|
+ if (!statementIterator.hasNext() && !(statement instanceof ReturnStatement)) {
|
|
218
|
+ ITypeID type = definition.header.returnType;
|
|
219
|
+ if (CompilerUtils.isPrimitive(type))
|
|
220
|
+ writer.iConst0();
|
|
221
|
+ else if (type != BasicTypeID.VOID)
|
|
222
|
+ writer.aConstNull();
|
|
223
|
+ writer.returnType(type.accept(JavaTypeVisitor.INSTANCE));
|
|
224
|
+ }
|
|
225
|
+
|
|
226
|
+ }
|
|
227
|
+
|
|
228
|
+ statementVisitor.end();
|
|
229
|
+
|
|
230
|
+ final JavaMethodInfo methodInfo = new JavaMethodInfo(new JavaClassInfo(CompilerUtils.calcClasName(definition.position)), definition.name, signature, true);
|
|
231
|
+
|
|
232
|
+ definition.setTag(JavaMethodInfo.class, methodInfo);
|
|
233
|
+ definition.caller.setTag(JavaMethodInfo.class, methodInfo);
|
197
|
234
|
return null;
|
198
|
235
|
}
|
199
|
236
|
|