-
Notifications
You must be signed in to change notification settings - Fork 0
/
InheritancePrinter.java
235 lines (206 loc) · 6.29 KB
/
InheritancePrinter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package xtc.oop;
import java.lang.*;
/* Imports based on src/xtc/lang/CPrinter.java */
import java.util.Iterator;
import java.util.LinkedList;
import xtc.tree.LineMarker;
import xtc.tree.Node;
import xtc.tree.GNode;
import xtc.tree.Pragma;
import xtc.tree.Printer;
import xtc.tree.SourceIdentity;
import xtc.tree.Token;
import xtc.tree.Visitor;
/* End imports based on src/xtc/lang/CPrinter.java */
/**
* A visitor that goes through our custom Inheritance tree.
* It prints out the information in the tree.
*/
public class InheritancePrinter extends Visitor {
private static final boolean VERBOSE = true;
/** The printer for this C printer. */
protected Printer printer;
/** the root of the tree. */
public GNode root;
/** The datalayout nodes. */
public GNode dataLayout;
private String packageName;
private String className;
private String javaClassName;
private boolean isFirstVTMethod = true;
/**
* Constructor for Inheritance Printer
*
* @param p The printer to which we are prinitng.
*/
public InheritancePrinter(Printer p){
this.printer = p;
printer.register(this);
}
/***************************************************************/
/******************** Visitor Methods ************************/
/***************************************************************/
/** Visiting Header Declaration and printing it. */
public void visitHeaderDeclaration(GNode n){
packageName = n.getString(0); //null if no package
if (packageName != null){
printer.pln("namespace " + n.getString(0) + " {");
className = n.getString(1);
visit(n);
printer.pln("}").pln();
}
else{
className = n.getString(1);
visit(n);
}
}
/** Visiting DataLayout Node and printing it. */
public void visitDataLayout(GNode n){
dataLayout = n;
printer.pln("struct __" + className + ";");
printer.p("struct __" + className + "_VT;").pln(); //
printer.p("typedef __rt::Ptr<__" + className + "> " + className + ";").pln();
printer.p("struct __" + className + " {").pln();
visit(n);
printer.pln("};").pln();
}
/** Visiting Field Declaration and printing it. */
public void visitFieldDeclaration(GNode n){
visit(n);
printer.p(n.getString(1)).p(" ").p(n.getString(2));
printer.pln(";");
}
/** Visiting Constructor Declaration and printing it. */
public void visitConstructorDeclaration(GNode n){
printer.pln("__" + n.getString(0) + "();");
if (n.getNode(1).size() == 0){
printer.pln("static " + className + " init(" + className + ");");
}
else {
printer.p("static " + className + " init(" + className + ", ");
printer.p(n.getNode(1));
printer.pln(");");
}
}
/** Visiting DataLayout Method Declaration and printing it. */
public void visitDataLayoutMethodDeclaration(GNode n){
if (!(n.get(0) == null)) {
printer.p(n.getNode(0));
}
if (!(n.get(1) == null)) {
if (n.getString(1).equals("int")){
printer.p("int32_t").p(" ");
}
else{
printer.p(n.getString(1)).p(" ");
}
}
String methodName = n.getString(2);
if (methodName.equals("main")){
printer.pln("main(__rt::Ptr<__rt::Array<String> > args);");
return;
}
printer.p(methodName);
printer.p("(");
printer.p(n.getNode(4));
if ((n.getNode(4).size()==0) && !(n.getString(2).equals("__class"))){
printer.p(className);
}
printer.pln(");");
}
/** Visiting Vtable and printing it. */
public void visitVTable(GNode n){
printer.pln("struct __" + className + "_VT {");
printer.pln("Class __isa;");
printer.pln("void (*__delete)(__" + className + "*);");
new Visitor(){
public void visitVTableMethodDeclaration(GNode n){
if(!(n.getString(2).equals("__isa"))){
if (n.getString(1) != null) {
printer.p(n.getString(1) + " (*");
}
else {
printer.p("void" + " (*");
}
printer.p(n.getString(2));
printer.p(")(");
//printer.p(className);
printer.p(n.getNode(4));
if ((n.getNode(4).size()==0) && !(n.getString(2).equals("__class"))){
printer.p(className);
}
printer.p(");");
printer.pln();
}
}
public void visit(Node n) {
for (Object o : n) if (o instanceof Node) dispatch((Node) o);
}
}.dispatch(n);
printer.pln("__" + className + "_VT()");
printer.p(": ");
visit(n);
printer.pln("{}");
printer.pln("};");
printer.pln();
}
/** Visiting Vtable(The function pointers) and printing it. */
public void visitVTableMethodDeclaration(GNode n){
if (isFirstVTMethod) {
printer.p("__isa(__" + className + "::__class())");
printer.p(",");
printer.pln();
printer.p("__delete(&__rt::__delete<__" + className + ">)");
isFirstVTMethod = false;
}
else{
printer.p(",");
printer.pln();
printer.p(n.getString(2)).p("(");
if (n.get(1) != null) {
printer.p("(");
if(n.getString(1).equals("int")){
printer.p("int32_t");
}
else{
printer.p(n.getString(1));
}
printer.p("(*)(").p(n.getNode(4));
}
if (n.getNode(4).size()==0){
printer.p(className);
}
printer.p("))");
printer.p(" &__" + n.getString(3) + "::" + n.getString(2));
printer.p(")");
}
}
/** Visiting Modifiers and printing it. */
public void visitModifiers(GNode n){
if (n.size() == 1) printer.p(n.getString(0)).p(" ");
}
/** Visiting Parameters and printing it. */
public void visitParameters(GNode n){
for (int x = 0; x < n.size() ; x++){
if (n.getString(x).equals("int")){
printer.p("int32_t");
}
else{
printer.p(n.getString(x));
}
if (x != (n.size()-1)){
printer.p(",");
}
}
}
/***************************************************************/
/******************** Helper Methods *************************/
/***************************************************************/
public void visit(Node n) {
for (Object o : n) if (o instanceof Node) dispatch((Node) o);
}
/** Print verbose debug messages into output file */
private void v(String s){
if(VERBOSE) printer.pln(s);
}
}