1
3 package jminusminus;
4
5 import java.util.ArrayList;
6 import static jminusminus.CLConstants.*;
7
8
11
12 class JSuperConstruction extends JExpression {
13
14
15 private ArrayList<JExpression> arguments;
16
17
18 private Constructor constructor;
19
20
21 private Type[] argTypes;
22
23
27 private boolean properUseOfConstructor = false;
28
29
38
39 protected JSuperConstruction(int line, ArrayList<JExpression> arguments) {
40 super(line);
41 this.arguments = arguments;
42 }
43
44
48
49 public void markProperUseOfConstructor() {
50 properUseOfConstructor = true;
51 }
52
53
63
64 public JExpression analyze(Context context) {
65 type = Type.VOID;
66
67 argTypes = new Type[arguments.size()];
70 for (int i = 0; i < arguments.size(); i++) {
71 arguments.set(i, (JExpression) arguments.get(i).analyze(context));
72 argTypes[i] = arguments.get(i).type();
73 }
74
75 if (!properUseOfConstructor) {
76 JAST.compilationUnit.reportSemanticError(line(), "super"
77 + Type.argTypesAsString(argTypes)
78 + " must be first statement in the constructor's body.");
79 return this;
80 }
81
82 Type superClass = ((JTypeDecl) context.classContext.definition())
84 .thisType().superClass();
85 if (superClass == null) {
86 JAST.compilationUnit.reportSemanticError(line,
87 ((JTypeDecl) context.classContext.definition()).thisType()
88 + " has no super class.");
89 }
90 constructor = superClass.constructorFor(argTypes);
91
92 if (constructor == null) {
93 JAST.compilationUnit.reportSemanticError(line(),
94 "No such constructor: super"
95 + Type.argTypesAsString(argTypes));
96
97 }
98 return this;
99 }
100
101
109
110 public void codegen(CLEmitter output) {
111 output.addNoArgInstruction(ALOAD_0); for (JExpression argument : arguments) {
113 argument.codegen(output);
114 }
115 output.addMemberAccessInstruction(INVOKESPECIAL, constructor
116 .declaringType().jvmName(), "<init>", constructor
117 .toDescriptor());
118 }
119
120
123
124 public void writeToStdOut(PrettyPrinter p) {
125 p.printf("<JSuperConstruction line=\"%d\"/>\n", line());
126 p.indentRight();
127 if (arguments != null) {
128 p.println("<Arguments>");
129 for (JExpression argument : arguments) {
130 p.indentRight();
131 p.println("<Argument>");
132 p.indentRight();
133 argument.writeToStdOut(p);
134 p.indentLeft();
135 p.println("</Argument>");
136 p.indentLeft();
137 }
138 p.println("</Arguments>");
139 }
140 p.indentLeft();
141 p.println("</JSuperConstruction>");
142 }
143
144 }
145