|
@@ -1,15 +1,16 @@
|
1
|
1
|
package org.openzen.zenscript.javabytecode.compiler;
|
2
|
2
|
|
3
|
3
|
import org.openzen.zenscript.codemodel.expression.*;
|
4
|
|
-import org.openzen.zenscript.codemodel.statement.StatementVisitor;
|
|
4
|
+import org.openzen.zenscript.codemodel.member.DefinitionMember;
|
|
5
|
+import org.openzen.zenscript.javabytecode.JavaBytecodeImplementation;
|
|
6
|
+import org.openzen.zenscript.javabytecode.JavaFieldInfo;
|
|
7
|
+import org.openzen.zenscript.javabytecode.JavaMethodInfo;
|
5
|
8
|
|
6
|
9
|
public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
|
7
|
10
|
|
8
|
|
- final StatementVisitor<?> statementVisitor;
|
9
|
|
- private final JavaWriter javaWriter;
|
|
11
|
+ private final JavaStatementVisitor statementVisitor;
|
10
|
12
|
|
11
|
|
- public JavaExpressionVisitor(final JavaWriter javaWriter, StatementVisitor<?> statementVisitor) {
|
12
|
|
- this.javaWriter = javaWriter;
|
|
13
|
+ public JavaExpressionVisitor(JavaStatementVisitor statementVisitor) {
|
13
|
14
|
this.statementVisitor = statementVisitor;
|
14
|
15
|
}
|
15
|
16
|
|
|
@@ -34,7 +35,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
|
34
|
35
|
for (Expression argument : expression.arguments.arguments) {
|
35
|
36
|
argument.accept(this);
|
36
|
37
|
}
|
37
|
|
- expression.member.accept(new JavaMemberVisitor(javaWriter, this));
|
|
38
|
+ if (!checkAndExecuteByteCodeImplementation(expression.member) && !checkAndExecuteMethodInfo(expression.member))
|
|
39
|
+ throw new IllegalStateException("Call target has no method info!");
|
38
|
40
|
|
39
|
41
|
return null;
|
40
|
42
|
}
|
|
@@ -86,79 +88,79 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
|
86
|
88
|
|
87
|
89
|
@Override
|
88
|
90
|
public Void visitConstantBool(ConstantBoolExpression expression) {
|
89
|
|
- javaWriter.constant(expression.value);
|
|
91
|
+ getJavaWriter().constant(expression.value);
|
90
|
92
|
return null;
|
91
|
93
|
}
|
92
|
94
|
|
93
|
95
|
@Override
|
94
|
96
|
public Void visitConstantByte(ConstantByteExpression expression) {
|
95
|
|
- javaWriter.biPush(expression.value);
|
|
97
|
+ getJavaWriter().biPush(expression.value);
|
96
|
98
|
return null;
|
97
|
99
|
}
|
98
|
100
|
|
99
|
101
|
@Override
|
100
|
102
|
public Void visitConstantChar(ConstantCharExpression expression) {
|
101
|
|
- javaWriter.constant(expression.value);
|
|
103
|
+ getJavaWriter().constant(expression.value);
|
102
|
104
|
return null;
|
103
|
105
|
}
|
104
|
106
|
|
105
|
107
|
@Override
|
106
|
108
|
public Void visitConstantDouble(ConstantDoubleExpression expression) {
|
107
|
|
- javaWriter.constant(expression.value);
|
|
109
|
+ getJavaWriter().constant(expression.value);
|
108
|
110
|
return null;
|
109
|
111
|
}
|
110
|
112
|
|
111
|
113
|
@Override
|
112
|
114
|
public Void visitConstantFloat(ConstantFloatExpression expression) {
|
113
|
|
- javaWriter.constant(expression.value);
|
|
115
|
+ getJavaWriter().constant(expression.value);
|
114
|
116
|
return null;
|
115
|
117
|
}
|
116
|
118
|
|
117
|
119
|
@Override
|
118
|
120
|
public Void visitConstantInt(ConstantIntExpression expression) {
|
119
|
|
- javaWriter.constant(expression.value);
|
|
121
|
+ getJavaWriter().constant(expression.value);
|
120
|
122
|
return null;
|
121
|
123
|
}
|
122
|
124
|
|
123
|
125
|
@Override
|
124
|
126
|
public Void visitConstantLong(ConstantLongExpression expression) {
|
125
|
|
- javaWriter.constant(expression.value);
|
|
127
|
+ getJavaWriter().constant(expression.value);
|
126
|
128
|
return null;
|
127
|
129
|
}
|
128
|
130
|
|
129
|
131
|
@Override
|
130
|
132
|
public Void visitConstantSByte(ConstantSByteExpression expression) {
|
131
|
|
- javaWriter.constant(expression.value);
|
|
133
|
+ getJavaWriter().constant(expression.value);
|
132
|
134
|
return null;
|
133
|
135
|
}
|
134
|
136
|
|
135
|
137
|
@Override
|
136
|
138
|
public Void visitConstantShort(ConstantShortExpression expression) {
|
137
|
|
- javaWriter.siPush(expression.value);
|
|
139
|
+ getJavaWriter().siPush(expression.value);
|
138
|
140
|
return null;
|
139
|
141
|
}
|
140
|
142
|
|
141
|
143
|
@Override
|
142
|
144
|
public Void visitConstantString(ConstantStringExpression expression) {
|
143
|
|
- javaWriter.constant(expression.value);
|
|
145
|
+ getJavaWriter().constant(expression.value);
|
144
|
146
|
return null;
|
145
|
147
|
}
|
146
|
148
|
|
147
|
149
|
@Override
|
148
|
150
|
public Void visitConstantUInt(ConstantUIntExpression expression) {
|
149
|
|
- javaWriter.constant(expression.value);
|
|
151
|
+ getJavaWriter().constant(expression.value);
|
150
|
152
|
return null;
|
151
|
153
|
}
|
152
|
154
|
|
153
|
155
|
@Override
|
154
|
156
|
public Void visitConstantULong(ConstantULongExpression expression) {
|
155
|
|
- javaWriter.constant(expression.value);
|
|
157
|
+ getJavaWriter().constant(expression.value);
|
156
|
158
|
return null;
|
157
|
159
|
}
|
158
|
160
|
|
159
|
161
|
@Override
|
160
|
162
|
public Void visitConstantUShort(ConstantUShortExpression expression) {
|
161
|
|
- javaWriter.constant(expression.value);
|
|
163
|
+ getJavaWriter().constant(expression.value);
|
162
|
164
|
return null;
|
163
|
165
|
}
|
164
|
166
|
|
|
@@ -189,6 +191,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
|
189
|
191
|
|
190
|
192
|
@Override
|
191
|
193
|
public Void visitGetField(GetFieldExpression expression) {
|
|
194
|
+ if (!checkAndExecuteFieldInfo(expression.field, false))
|
|
195
|
+ throw new IllegalStateException("Missing field info on a field member!");
|
192
|
196
|
return null;
|
193
|
197
|
}
|
194
|
198
|
|
|
@@ -204,7 +208,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
|
204
|
208
|
|
205
|
209
|
@Override
|
206
|
210
|
public Void visitGetStaticField(GetStaticFieldExpression expression) {
|
207
|
|
- expression.field.accept(new JavaMemberVisitor(javaWriter, this));
|
|
211
|
+ if (!checkAndExecuteFieldInfo(expression.field, true))
|
|
212
|
+ throw new IllegalStateException("Missing field info on a field member!");
|
208
|
213
|
return null;
|
209
|
214
|
}
|
210
|
215
|
|
|
@@ -312,4 +317,59 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
|
312
|
317
|
public Void visitWrapOptional(WrapOptionalExpression expression) {
|
313
|
318
|
return null;
|
314
|
319
|
}
|
|
320
|
+
|
|
321
|
+ public JavaWriter getJavaWriter() {
|
|
322
|
+ return statementVisitor.getJavaWriter();
|
|
323
|
+ }
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+ //Will return true if a JavaBytecodeImplementation.class tag exists, and will compile that tag
|
|
327
|
+ private boolean checkAndExecuteByteCodeImplementation(DefinitionMember member) {
|
|
328
|
+ JavaBytecodeImplementation implementation = member.getTag(JavaBytecodeImplementation.class);
|
|
329
|
+ if (implementation != null) {
|
|
330
|
+ implementation.compile(getJavaWriter());
|
|
331
|
+ return true;
|
|
332
|
+ }
|
|
333
|
+ return false;
|
|
334
|
+ }
|
|
335
|
+
|
|
336
|
+ //Will return true if a JavaMethodInfo.class tag exists, and will compile that tag
|
|
337
|
+ private boolean checkAndExecuteMethodInfo(DefinitionMember member) {
|
|
338
|
+ JavaMethodInfo methodInfo = member.getTag(JavaMethodInfo.class);
|
|
339
|
+ if (methodInfo == null)
|
|
340
|
+ return false;
|
|
341
|
+ if (methodInfo.isStatic) {
|
|
342
|
+ getJavaWriter().invokeStatic(
|
|
343
|
+ methodInfo.javaClass.internalClassName,
|
|
344
|
+ methodInfo.name,
|
|
345
|
+ methodInfo.signature);
|
|
346
|
+ } else {
|
|
347
|
+ getJavaWriter().invokeVirtual(
|
|
348
|
+ methodInfo.javaClass.internalClassName,
|
|
349
|
+ methodInfo.name,
|
|
350
|
+ methodInfo.signature);
|
|
351
|
+ }
|
|
352
|
+ return true;
|
|
353
|
+ }
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+ //TODO: Should isStatic go to the fieldInfo or stay here?
|
|
357
|
+ //Will return true if a JavaFieldInfo.class tag exists, and will compile that tag
|
|
358
|
+ private boolean checkAndExecuteFieldInfo(DefinitionMember field, boolean isStatic) {
|
|
359
|
+ JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
|
|
360
|
+ if (fieldInfo == null)
|
|
361
|
+ return false;
|
|
362
|
+ if (isStatic) {
|
|
363
|
+ getJavaWriter().getStaticField(
|
|
364
|
+ fieldInfo.javaClass.internalClassName,
|
|
365
|
+ fieldInfo.name,
|
|
366
|
+ fieldInfo.signature);
|
|
367
|
+ } else {
|
|
368
|
+ getJavaWriter().getField(
|
|
369
|
+ fieldInfo.javaClass.internalClassName,
|
|
370
|
+ fieldInfo.name,
|
|
371
|
+ fieldInfo.signature);
|
|
372
|
+ }
|
|
373
|
+ return true;
|
|
374
|
+ }
|
315
|
375
|
}
|