|
@@ -4,12 +4,19 @@ import org.objectweb.asm.ClassWriter;
|
4
|
4
|
import org.objectweb.asm.Opcodes;
|
5
|
5
|
import org.objectweb.asm.Type;
|
6
|
6
|
import org.openzen.zenscript.codemodel.definition.*;
|
|
7
|
+import org.openzen.zenscript.codemodel.expression.Expression;
|
|
8
|
+import org.openzen.zenscript.codemodel.expression.ExpressionVisitor;
|
7
|
9
|
import org.openzen.zenscript.codemodel.member.ConstructorMember;
|
|
10
|
+import org.openzen.zenscript.codemodel.member.EnumConstantMember;
|
8
|
11
|
import org.openzen.zenscript.codemodel.member.FieldMember;
|
9
|
12
|
import org.openzen.zenscript.codemodel.member.IDefinitionMember;
|
10
|
|
-import org.openzen.zenscript.javabytecode.JavaClassInfo;
|
11
|
|
-import org.openzen.zenscript.javabytecode.JavaFieldInfo;
|
|
13
|
+import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
|
|
14
|
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
|
|
15
|
+import org.openzen.zenscript.javabytecode.JavaEnumInfo;
|
|
16
|
+import org.openzen.zenscript.javabytecode.compiler.JavaExpressionVisitor;
|
|
17
|
+import org.openzen.zenscript.javabytecode.compiler.JavaStatementVisitor;
|
12
|
18
|
import org.openzen.zenscript.javabytecode.compiler.JavaTypeClassVisitor;
|
|
19
|
+import org.openzen.zenscript.javabytecode.compiler.JavaWriter;
|
13
|
20
|
|
14
|
21
|
public class JavaDefinitionVisitor implements DefinitionVisitor<byte[]> {
|
15
|
22
|
|
|
@@ -18,58 +25,159 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<byte[]> {
|
18
|
25
|
public byte[] visitClass(ClassDefinition definition) {
|
19
|
26
|
|
20
|
27
|
|
21
|
|
- Type superType;
|
|
28
|
+ final Type superType;
|
22
|
29
|
if (definition.superType == null)
|
23
|
30
|
superType = Type.getType(Object.class);
|
24
|
31
|
else
|
25
|
32
|
superType = Type.getType(definition.superType.accept(JavaTypeClassVisitor.INSTANCE));
|
26
|
33
|
|
27
|
34
|
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
|
28
|
|
- String signature = null;
|
29
|
|
- /* TODO: Calculate signature from generic parameters
|
30
|
|
- if(!definition.genericParameters.isEmpty()) {
|
31
|
|
- StringBuilder signatureBuilder = new StringBuilder();
|
32
|
|
- for (TypeParameter genericParameter : definition.genericParameters) {
|
33
|
35
|
|
34
|
|
- }
|
35
|
|
- }
|
36
|
|
- */
|
|
36
|
+ //TODO: Calculate signature from generic parameters
|
|
37
|
+ //TODO: Interfaces?
|
|
38
|
+ String signature = null;
|
37
|
39
|
|
38
|
40
|
|
|
41
|
+ writer.visit(Opcodes.V1_8, definition.modifiers, definition.name, signature, superType.getInternalName(), null);
|
39
|
42
|
for (IDefinitionMember member : definition.members) {
|
40
|
43
|
if (member instanceof ConstructorMember) {
|
41
|
|
- for (IDefinitionMember member2 : definition.members) {
|
42
|
|
- if (member2 instanceof FieldMember) {
|
43
|
|
- if (!member2.hasTag(JavaClassInfo.class))
|
44
|
|
- member2.setTag(JavaFieldInfo.class, new JavaFieldInfo(new JavaClassInfo(definition.name), ((FieldMember) member2).name, Type.getDescriptor(((FieldMember) member2).type.accept(JavaTypeClassVisitor.INSTANCE))));
|
45
|
|
- if (!member.hasTag(JavaInitializedVariables.class))
|
46
|
|
- member.setTag(JavaInitializedVariables.class, new JavaInitializedVariables(definition.name));
|
47
|
|
- member.getTag(JavaInitializedVariables.class).fields.add((FieldMember) member2);
|
48
|
|
-
|
49
|
|
-
|
|
44
|
+ final ConstructorMember constructorMember = (ConstructorMember) member;
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+ constructorMember.body.add(0, new ExpressionStatement(constructorMember.position, new Expression(constructorMember.position, BasicTypeID.VOID) {
|
|
48
|
+ @Override
|
|
49
|
+ public <T> T accept(ExpressionVisitor<T> visitor) {
|
|
50
|
+ if (visitor instanceof JavaExpressionVisitor) {
|
|
51
|
+ JavaWriter javaWriter = ((JavaExpressionVisitor) visitor).getJavaWriter();
|
|
52
|
+ javaWriter.loadObject(0);
|
|
53
|
+ //TODO Super constructor?
|
|
54
|
+ javaWriter.invokeSpecial(Type.getInternalName(Object.class), "<init>", "()V");
|
|
55
|
+ for (final IDefinitionMember definitionMember : definition.members) {
|
|
56
|
+ if (definitionMember instanceof FieldMember && ((FieldMember) definitionMember).initializer != null) {
|
|
57
|
+ final FieldMember field = (FieldMember) definitionMember;
|
|
58
|
+ javaWriter.loadObject(0);
|
|
59
|
+ field.initializer.accept(visitor);
|
|
60
|
+ javaWriter.putField(definition.name, field.name, Type.getDescriptor(field.type.accept(JavaTypeClassVisitor.INSTANCE)));
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+ }
|
|
64
|
+ return null;
|
50
|
65
|
}
|
51
|
|
- }
|
|
66
|
+ }));
|
52
|
67
|
}
|
53
|
|
- }
|
54
|
|
-
|
55
|
|
- writer.visit(Opcodes.V1_8, definition.modifiers, definition.name, signature, superType.getInternalName(), null);
|
56
|
|
- for (IDefinitionMember member : definition.members) {
|
57
|
68
|
member.accept(new JavaMemberVisitor(writer, definition.name));
|
58
|
|
- }
|
59
|
69
|
|
60
|
70
|
|
|
71
|
+ }
|
61
|
72
|
writer.visitEnd();
|
62
|
73
|
return writer.toByteArray();
|
63
|
74
|
}
|
64
|
75
|
|
65
|
76
|
@Override
|
66
|
77
|
public byte[] visitInterface(InterfaceDefinition definition) {
|
67
|
|
- return null;
|
|
78
|
+ ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
|
|
79
|
+
|
|
80
|
+ //TODO: Calculate signature from generic parameters
|
|
81
|
+ //TODO: Extending Interfaces?
|
|
82
|
+ String signature = null;
|
|
83
|
+ writer.visit(Opcodes.V1_8, definition.modifiers | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT, definition.name, signature, Type.getInternalName(Object.class), null);
|
|
84
|
+ for (IDefinitionMember member : definition.members) {
|
|
85
|
+ member.accept(new JavaMemberVisitor(writer, definition.name));
|
|
86
|
+ }
|
|
87
|
+ writer.visitEnd();
|
|
88
|
+ return writer.toByteArray();
|
68
|
89
|
}
|
69
|
90
|
|
70
|
91
|
@Override
|
71
|
92
|
public byte[] visitEnum(EnumDefinition definition) {
|
72
|
|
- return null;
|
|
93
|
+ final Type superType;
|
|
94
|
+ if (definition.superType == null)
|
|
95
|
+ superType = Type.getType(Object.class);
|
|
96
|
+ else
|
|
97
|
+ superType = Type.getType(definition.superType.accept(JavaTypeClassVisitor.INSTANCE));
|
|
98
|
+
|
|
99
|
+ ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
|
|
100
|
+
|
|
101
|
+ writer.visit(Opcodes.V1_8, Opcodes.ACC_ENUM | Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL, definition.name, "Ljava/lang/Enum<L" + definition.name + ";>;", superType.getInternalName(), null);
|
|
102
|
+
|
|
103
|
+ for (IDefinitionMember member : definition.members) {
|
|
104
|
+ if (member instanceof EnumConstantMember) {
|
|
105
|
+ EnumConstantMember constantMember = (EnumConstantMember) member;
|
|
106
|
+ writer.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_ENUM, constantMember.name, "L" + definition.name + ";", null, null).visitEnd();
|
|
107
|
+ } else if (member instanceof ConstructorMember) {
|
|
108
|
+ member.setTag(JavaEnumInfo.class, null);
|
|
109
|
+ }
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ final JavaWriter clinitWriter = new JavaWriter(writer, true, Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
|
|
113
|
+ final JavaStatementVisitor clinitVisitor = new JavaStatementVisitor(clinitWriter, true);
|
|
114
|
+ clinitVisitor.start();
|
|
115
|
+ int constantCount = 0;
|
|
116
|
+ for (IDefinitionMember member : definition.members) {
|
|
117
|
+ if (member instanceof ConstructorMember) {
|
|
118
|
+ final ConstructorMember constructorMember = (ConstructorMember) member;
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+ constructorMember.body.add(0, new ExpressionStatement(constructorMember.position, new Expression(constructorMember.position, BasicTypeID.VOID) {
|
|
122
|
+ @Override
|
|
123
|
+ public <T> T accept(ExpressionVisitor<T> visitor) {
|
|
124
|
+ if (visitor instanceof JavaExpressionVisitor) {
|
|
125
|
+ JavaWriter javaWriter = ((JavaExpressionVisitor) visitor).getJavaWriter();
|
|
126
|
+ javaWriter.getVisitor().newLocal(Type.getType(String.class));
|
|
127
|
+ javaWriter.getVisitor().newLocal(Type.getType(int.class));
|
|
128
|
+ javaWriter.loadObject(0);
|
|
129
|
+ javaWriter.loadObject(1);
|
|
130
|
+ javaWriter.loadInt(2);
|
|
131
|
+ //javaWriter.invokeSpecial(Type.getInternalName(Enum.class), "<init>", "(Ljava/lang/String;I)V");
|
|
132
|
+ }
|
|
133
|
+ return null;
|
|
134
|
+ }
|
|
135
|
+ }));
|
|
136
|
+ } else if (member instanceof EnumConstantMember) {
|
|
137
|
+ ++constantCount;
|
|
138
|
+ member.setTag(JavaEnumInfo.class, new JavaEnumInfo(clinitVisitor));
|
|
139
|
+ }
|
|
140
|
+ member.accept(new JavaMemberVisitor(writer, definition.name));
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ clinitWriter.constant(constantCount);
|
|
144
|
+ clinitWriter.newArray(Type.getType("L" + definition.name + ";"));
|
|
145
|
+
|
|
146
|
+ for (IDefinitionMember member : definition.members) {
|
|
147
|
+ if(!(member instanceof EnumConstantMember)) continue;
|
|
148
|
+ final EnumConstantMember constantMember = (EnumConstantMember) member;
|
|
149
|
+ clinitWriter.dup();
|
|
150
|
+ clinitWriter.constant(constantMember.value);
|
|
151
|
+ clinitWriter.getStaticField(definition.name, constantMember.name, "L" + definition.name + ";");
|
|
152
|
+ clinitWriter.arrayStore(Type.getType("L" + definition.name + ";"));
|
|
153
|
+ }
|
|
154
|
+ clinitWriter.putStaticField(definition.name, "$VALUES", "[L" + definition.name + ";");
|
|
155
|
+ clinitVisitor.end();
|
|
156
|
+
|
|
157
|
+ //Enum Stuff(required!)
|
|
158
|
+ writer.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC, "$VALUES", "[L" + definition.name + ";", null, null).visitEnd();
|
|
159
|
+
|
|
160
|
+ JavaWriter valuesWriter = new JavaWriter(writer, true, Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, "values", "()[L" + definition.name + ";", null, null);
|
|
161
|
+ valuesWriter.start();
|
|
162
|
+ valuesWriter.getStaticField(definition.name, "$VALUES", "[L" + definition.name + ";");
|
|
163
|
+ valuesWriter.invokeVirtual("[L" + definition.name + ";", "clone", "()Ljava/lang/Object;");
|
|
164
|
+ valuesWriter.checkCast("[L" + definition.name + ";");
|
|
165
|
+ valuesWriter.returnObject();
|
|
166
|
+ valuesWriter.end();
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+ JavaWriter valueOfWriter = new JavaWriter(writer, true, Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, "valueOf", "(Ljava/lang/String;)L" + definition.name + ";", null, null);
|
|
170
|
+ valueOfWriter.start();
|
|
171
|
+ valueOfWriter.invokeStatic("java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
|
|
172
|
+ valueOfWriter.loadObject(0);
|
|
173
|
+ valueOfWriter.invokeStatic("java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;");
|
|
174
|
+ valueOfWriter.checkCast("L" + definition.name + ";");
|
|
175
|
+ valueOfWriter.returnObject();
|
|
176
|
+ valueOfWriter.end();
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+ writer.visitEnd();
|
|
180
|
+ return writer.toByteArray();
|
73
|
181
|
}
|
74
|
182
|
|
75
|
183
|
@Override
|