1
3 package jminusminus;
4
5 import java.util.ArrayList;
6
7 import static jminusminus.CLConstants.*;
8
9
12 class JArrayInitializer extends JExpression {
13 private ArrayList<JExpression> initials;
15
16
23 public JArrayInitializer(int line, Type type, ArrayList<JExpression> initials) {
24 super(line);
25 this.type = type;
26 this.initials = initials;
27 }
28
29
32 public JExpression analyze(Context context) {
33 type = type.resolve(context);
34 if (!type.isArray()) {
35 JAST.compilationUnit.reportSemanticError(line, "Cannot initialize a " + type.toString()
36 + " with an array sequence {...}");
37 return this;
38 }
39 Type componentType = type.componentType();
40 for (int i = 0; i < initials.size(); i++) {
41 JExpression initial = initials.get(i);
42 initials.set(i, initial = initial.analyze(context));
43 if (!(initial instanceof JArrayInitializer)) {
44 initial.type().mustMatchExpected(line, componentType);
45 }
46 }
47 return this;
48 }
49
50
53 public void codegen(CLEmitter output) {
54 Type componentType = type.componentType();
55
56 (new JLiteralInt(line, String.valueOf(initials.size()))).codegen(output);
58
59 output.addArrayInstruction(componentType.isReference() ? ANEWARRAY : NEWARRAY,
61 componentType.jvmName());
62
63 for (int i = 0; i < initials.size(); i++) {
65 JExpression initial = initials.get(i);
66
67 output.addNoArgInstruction(DUP);
69
70 (new JLiteralInt(line, String.valueOf(i))).codegen(output);
72
73 initial.codegen(output);
75
76 if (componentType == Type.INT) {
78 output.addNoArgInstruction(IASTORE);
79 } else if (componentType == Type.BOOLEAN) {
80 output.addNoArgInstruction(BASTORE);
81 } else if (componentType == Type.CHAR) {
82 output.addNoArgInstruction(CASTORE);
83 } else if (!componentType.isPrimitive()) {
84 output.addNoArgInstruction(AASTORE);
85 }
86 }
87 }
88
89
92 public void toJSON(JSONElement json) {
93 JSONElement e = new JSONElement();
94 json.addChild("JArrayInitializer:" + line, e);
95 if (initials != null) {
96 for (JExpression initial : initials) {
97 initial.toJSON(e);
98 }
99 }
100 }
101 }
102