1
3 package jminusminus;
4
5 import java.util.ArrayList;
6 import static jminusminus.CLConstants.*;
7
8
12
13 class JNewArrayOp extends JExpression {
14
15
16 private Type typeSpec;
17
18
19 private ArrayList<JExpression> dimExprs;
20
21
31
32 public JNewArrayOp(int line, Type typeSpec, ArrayList<JExpression> dimExprs) {
33 super(line);
34 this.typeSpec = typeSpec;
35 this.dimExprs = dimExprs;
36 }
37
38
46
47 public JExpression analyze(Context context) {
48 type = typeSpec.resolve(context);
49 for (int i = 0; i < dimExprs.size(); i++) {
50 dimExprs.set(i, dimExprs.get(i).analyze(context));
51 dimExprs.get(i).type().mustMatchExpected(line, Type.INT);
52 }
53 return this;
54 }
55
56
64
65 public void codegen(CLEmitter output) {
66 for (JExpression dimExpr : dimExprs) {
68 dimExpr.codegen(output);
69 }
70
71 if (dimExprs.size() == 1) {
73 output.addArrayInstruction(
74 type.componentType().isReference() ? ANEWARRAY : NEWARRAY,
75 type.componentType().jvmName());
76 } else {
77 output.addMULTIANEWARRAYInstruction(type.toDescriptor(), dimExprs
78 .size());
79 }
80 }
81
82
85
86 public void writeToStdOut(PrettyPrinter p) {
87 p.printf("<JNewArrayOp line=\"%d\" type=\"%s\"/>\n", line(),
88 ((type == null) ? "" : type.toString()));
89 p.indentRight();
90 p.println("<Dimensions>");
91 if (dimExprs != null) {
92 p.indentRight();
93 for (JExpression dimExpr : dimExprs) {
94 p.println("<Dimension>");
95 p.indentRight();
96 dimExpr.writeToStdOut(p);
97 p.indentLeft();
98 p.println("</Dimension>");
99 }
100 p.indentLeft();
101 }
102 p.println("</Dimensions>");
103 p.indentLeft();
104 p.println("</JNewArrayOp>");
105 }
106
107 }
108