|
@@ -7,19 +7,20 @@ import org.objectweb.asm.Type;
|
7
|
7
|
import org.openzen.zenscript.codemodel.FunctionParameter;
|
8
|
8
|
import org.openzen.zenscript.codemodel.HighLevelDefinition;
|
9
|
9
|
import org.openzen.zenscript.codemodel.Modifiers;
|
|
10
|
+import org.openzen.zenscript.codemodel.annotations.NativeTag;
|
10
|
11
|
import org.openzen.zenscript.codemodel.definition.EnumDefinition;
|
11
|
12
|
import org.openzen.zenscript.codemodel.expression.Expression;
|
|
13
|
+import org.openzen.zenscript.codemodel.generic.TypeParameter;
|
12
|
14
|
import org.openzen.zenscript.codemodel.member.*;
|
|
15
|
+import org.openzen.zenscript.codemodel.type.StoredType;
|
13
|
16
|
import org.openzen.zenscript.javabytecode.compiler.*;
|
14
|
17
|
|
|
18
|
+import java.util.ArrayList;
|
|
19
|
+import java.util.Arrays;
|
15
|
20
|
import java.util.Collections;
|
16
|
21
|
import java.util.List;
|
17
|
22
|
import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
|
18
|
|
-import org.openzen.zenscript.javashared.JavaClass;
|
19
|
|
-import org.openzen.zenscript.javashared.JavaCompiledModule;
|
20
|
|
-import org.openzen.zenscript.javashared.JavaField;
|
21
|
|
-import org.openzen.zenscript.javashared.JavaImplementation;
|
22
|
|
-import org.openzen.zenscript.javashared.JavaMethod;
|
|
23
|
+import org.openzen.zenscript.javashared.*;
|
23
|
24
|
|
24
|
25
|
public class JavaMemberVisitor implements MemberVisitor<Void> {
|
25
|
26
|
private final ClassWriter writer;
|
|
@@ -67,6 +68,22 @@ public class JavaMemberVisitor implements MemberVisitor<Void> {
|
67
|
68
|
final JavaWriter constructorWriter = new JavaWriter(member.position, writer, method, definition, context.getMethodSignature(member.header), null);
|
68
|
69
|
constructorWriter.label(constructorStart);
|
69
|
70
|
CompilerUtils.tagConstructorParameters(context, javaModule, member.definition, member.header, isEnum);
|
|
71
|
+ if(isEnum) {
|
|
72
|
+ constructorWriter.nameParameter(0, "name");
|
|
73
|
+ constructorWriter.nameParameter(0, "index");
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ for (TypeParameter typeParameter : definition.typeParameters) {
|
|
77
|
+ constructorWriter.nameParameter(0, "typeof" + typeParameter.name);
|
|
78
|
+ constructorWriter.nameVariable(
|
|
79
|
+ javaModule.getTypeParameterInfo(typeParameter).parameterIndex,
|
|
80
|
+ "typeOf" + typeParameter.name,
|
|
81
|
+ constructorStart,
|
|
82
|
+ constructorEnd,
|
|
83
|
+ Type.getType(Class.class)
|
|
84
|
+ );
|
|
85
|
+ }
|
|
86
|
+
|
70
|
87
|
for (FunctionParameter parameter : member.header.parameters) {
|
71
|
88
|
constructorWriter.nameVariable(
|
72
|
89
|
javaModule.getParameterInfo(parameter).index,
|
|
@@ -95,6 +112,17 @@ public class JavaMemberVisitor implements MemberVisitor<Void> {
|
95
|
112
|
}
|
96
|
113
|
}
|
97
|
114
|
|
|
115
|
+ for (TypeParameter typeParameter : definition.typeParameters) {
|
|
116
|
+ final JavaTypeParameterInfo typeParameterInfo = javaModule.getTypeParameterInfo(typeParameter);
|
|
117
|
+ final JavaField field = typeParameterInfo.field;
|
|
118
|
+
|
|
119
|
+ //Init from Constructor
|
|
120
|
+ final int parameterIndex = typeParameterInfo.parameterIndex;
|
|
121
|
+ constructorWriter.loadObject(0);
|
|
122
|
+ constructorWriter.loadObject(parameterIndex);
|
|
123
|
+ constructorWriter.putField(field);
|
|
124
|
+ }
|
|
125
|
+
|
98
|
126
|
if (member.body != null) {
|
99
|
127
|
member.body.accept(statementVisitor);
|
100
|
128
|
}
|
|
@@ -156,21 +184,105 @@ public class JavaMemberVisitor implements MemberVisitor<Void> {
|
156
|
184
|
|
157
|
185
|
@Override
|
158
|
186
|
public Void visitGetter(GetterMember member) {
|
|
187
|
+ if (member.hasTag(NativeTag.class)) {
|
|
188
|
+ return null;
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ final String descriptor = context.getMethodDescriptor(member.getHeader());
|
|
192
|
+ final String signature = context.getMethodSignature(member.getHeader(), true);
|
|
193
|
+
|
|
194
|
+ final Label methodStart = new Label();
|
|
195
|
+ final Label methodEnd = new Label();
|
|
196
|
+
|
|
197
|
+ final JavaMethod method = context.getJavaMethod(member);
|
|
198
|
+ final JavaWriter methodWriter = new JavaWriter(member.position, this.writer, true, method, definition, false, signature, descriptor, new String[0]);
|
|
199
|
+
|
|
200
|
+ methodWriter.label(methodStart);
|
|
201
|
+ final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, javaModule, methodWriter);
|
|
202
|
+ statementVisitor.start();
|
|
203
|
+ member.body.accept(statementVisitor);
|
|
204
|
+ methodWriter.label(methodEnd);
|
|
205
|
+ statementVisitor.end();
|
|
206
|
+
|
159
|
207
|
return null;
|
160
|
208
|
}
|
161
|
209
|
|
162
|
210
|
@Override
|
163
|
211
|
public Void visitSetter(SetterMember member) {
|
|
212
|
+ final String signature = context.getMethodSignature(member.getHeader());
|
|
213
|
+ final String description = context.getMethodDescriptor(member.getHeader());
|
|
214
|
+
|
|
215
|
+ final Label methodStart = new Label();
|
|
216
|
+ final Label methodEnd = new Label();
|
|
217
|
+
|
|
218
|
+ final JavaMethod javaMethod = context.getJavaMethod(member);
|
|
219
|
+ final JavaWriter methodWriter = new JavaWriter(member.position, writer, true, javaMethod, member.definition, false, signature, description, new String[0]);
|
|
220
|
+ methodWriter.label(methodStart);
|
|
221
|
+
|
|
222
|
+ //in script you use $ but the parameter is named "value", which to choose?
|
|
223
|
+ //final String name = member.parameter.name;
|
|
224
|
+ final String name = "$";
|
|
225
|
+ methodWriter.nameVariable(1, name, methodStart, methodEnd, context.getType(member.getType()));
|
|
226
|
+ methodWriter.nameParameter(0, name);
|
|
227
|
+
|
|
228
|
+ javaModule.setParameterInfo(member.parameter, new JavaParameterInfo(1, context.getDescriptor(member.getType())));
|
|
229
|
+
|
|
230
|
+ final JavaStatementVisitor javaStatementVisitor = new JavaStatementVisitor(context, javaModule, methodWriter);
|
|
231
|
+ javaStatementVisitor.start();
|
|
232
|
+ member.body.accept(javaStatementVisitor);
|
|
233
|
+ javaStatementVisitor.end();
|
|
234
|
+ methodWriter.label(methodEnd);
|
|
235
|
+
|
164
|
236
|
return null;
|
165
|
237
|
}
|
166
|
238
|
|
167
|
239
|
@Override
|
168
|
240
|
public Void visitOperator(OperatorMember member) {
|
169
|
|
- return null;
|
|
241
|
+
|
|
242
|
+ final JavaMethod javaMethod = context.getJavaMethod(member);
|
|
243
|
+ final MethodMember methodMember = new MethodMember(member.position, member.definition, member.getEffectiveModifiers(), javaMethod.name, member.header, member.builtin);
|
|
244
|
+ methodMember.body = member.body;
|
|
245
|
+ methodMember.annotations = member.annotations;
|
|
246
|
+ javaModule.setMethodInfo(methodMember, javaMethod);
|
|
247
|
+
|
|
248
|
+ return methodMember.accept(this);
|
170
|
249
|
}
|
171
|
250
|
|
172
|
251
|
@Override
|
173
|
252
|
public Void visitCaster(CasterMember member) {
|
|
253
|
+ final JavaMethod javaMethod = context.getJavaMethod(member);
|
|
254
|
+ if(javaMethod == null || !javaMethod.compile) {
|
|
255
|
+ return null;
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+ final ArrayList<TypeParameter> typeParameters = new ArrayList<>(Arrays.asList(this.definition.typeParameters));
|
|
259
|
+
|
|
260
|
+ CompilerUtils.tagMethodParameters(context, javaModule, member.getHeader(), false, typeParameters);
|
|
261
|
+ member.toType.type.extractTypeParameters(typeParameters);
|
|
262
|
+
|
|
263
|
+ final String methodSignature = context.getMethodSignature(member.getHeader());
|
|
264
|
+ final String methodDescriptor = context.getMethodDescriptor(member.getHeader());
|
|
265
|
+
|
|
266
|
+ final Label methodStart = new Label();
|
|
267
|
+ final Label methodEnd = new Label();
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+ final JavaWriter methodWriter = new JavaWriter(member.position, writer, true, javaMethod, member.definition, false, methodSignature, methodDescriptor, new String[0]);
|
|
271
|
+
|
|
272
|
+ methodWriter.label(methodStart);
|
|
273
|
+
|
|
274
|
+ int i = 1;
|
|
275
|
+ for (TypeParameter typeParameter : typeParameters) {
|
|
276
|
+ final String name = "typeOf" + typeParameter.name;
|
|
277
|
+ methodWriter.nameVariable(i, name, methodStart, methodEnd, Type.getType(Class.class));
|
|
278
|
+ methodWriter.nameParameter(0, name);
|
|
279
|
+ }
|
|
280
|
+
|
|
281
|
+ final JavaStatementVisitor javaStatementVisitor = new JavaStatementVisitor(context, javaModule, methodWriter);
|
|
282
|
+ javaStatementVisitor.start();
|
|
283
|
+ member.body.accept(javaStatementVisitor);
|
|
284
|
+ javaStatementVisitor.end();
|
|
285
|
+ methodWriter.label(methodEnd);
|
174
|
286
|
return null;
|
175
|
287
|
}
|
176
|
288
|
|
|
@@ -181,7 +293,14 @@ public class JavaMemberVisitor implements MemberVisitor<Void> {
|
181
|
293
|
|
182
|
294
|
@Override
|
183
|
295
|
public Void visitCaller(CallerMember member) {
|
184
|
|
- return null;
|
|
296
|
+ //It's gonna be a method anyways, so why not reuse the code ^^
|
|
297
|
+ final JavaMethod javaMethod = context.getJavaMethod(member);
|
|
298
|
+ final MethodMember call = new MethodMember(member.position, member.definition, member.getEffectiveModifiers(), javaMethod.name, member.header, member.builtin);
|
|
299
|
+ call.body = member.body;
|
|
300
|
+ call.annotations = member.annotations;
|
|
301
|
+
|
|
302
|
+ javaModule.setMethodInfo(call, javaMethod);
|
|
303
|
+ return call.accept(this);
|
185
|
304
|
}
|
186
|
305
|
|
187
|
306
|
@Override
|