changes
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3 import java.util.Vector;
4
5 public class BuildIR {
6     State state;
7     public BuildIR(State state) {
8         this.state=state;
9     }
10     public void buildtree() {
11         ParseNode pn=state.parsetree;
12         parseFile(pn);
13     }
14
15     /** Parse the classes in this file */
16     public void parseFile(ParseNode pn) {
17         ParseNode tpn=pn.getChild("type_declaration_list");
18         if (tpn!=null) {
19             ParseNodeVector pnv=tpn.getChildren();
20             for(int i=0;i<pnv.size();i++) {
21                 ParseNode type_pn=pnv.elementAt(i);
22                 if (isEmpty(type_pn)) /* Skip the semicolon */
23                     continue;
24                 ClassDescriptor cn=parseTypeDecl(type_pn);
25                 state.addClass(cn);
26             }
27         }
28     }
29
30     public ClassDescriptor parseTypeDecl(ParseNode pn) {
31         if (isNode(pn, "class_declaration")) {
32             ClassDescriptor cn=new ClassDescriptor();
33             cn.setName(pn.getChild("name").getTerminal());
34             if (!isEmpty(pn.getChild("super").getTerminal())) {
35                 /* parse superclass name */
36             }
37             cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
38             parseClassBody(cn, pn.getChild("classbody"));
39             return cn;
40         } else throw new Error();
41     }
42
43     private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
44         ParseNode decls=pn.getChild("class_body_declaration_list");
45         if (decls!=null) {
46             ParseNodeVector pnv=decls.getChildren();
47             for(int i=0;i<pnv.size();i++) {
48                 ParseNode decl=pnv.elementAt(i);
49                 if (isNode(decl,"member")) {
50                     parseClassMember(cn,decl);
51                 } else if (isNode(decl,"constructor")) {
52                 } else if (isNode(decl,"block")) {
53                 } else throw new Error();
54             }
55         }
56     }
57
58     private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
59         ParseNode fieldnode=pn.getChild("field");
60
61         if (fieldnode!=null) {
62             parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
63             return;
64         }
65         ParseNode methodnode=pn.getChild("method");
66         if (methodnode!=null) {
67             parseMethodDecl(cn,methodnode.getChild("method_declaration"));
68             return;
69         }
70         throw new Error();
71     }
72
73     private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
74         ParseNode tn=pn.getChild("type");
75         String type_st=tn.getTerminal();
76         if(type_st.equals("byte")) {
77             return state.getTypeDescriptor(TypeDescriptor.BYTE);
78         } else if(type_st.equals("short")) {
79             return state.getTypeDescriptor(TypeDescriptor.SHORT);
80         } else if(type_st.equals("boolean")) {
81             return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
82         } else if(type_st.equals("int")) {
83             return state.getTypeDescriptor(TypeDescriptor.INT);
84         } else if(type_st.equals("long")) {
85             return state.getTypeDescriptor(TypeDescriptor.LONG);
86         } else if(type_st.equals("char")) {
87             return state.getTypeDescriptor(TypeDescriptor.CHAR);
88         } else if(type_st.equals("float")) {
89             return state.getTypeDescriptor(TypeDescriptor.FLOAT);
90         } else if(type_st.equals("double")) {
91             return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
92         } else if(type_st.equals("class")) {
93             ParseNode nn=tn.getChild("class");
94             return state.getTypeDescriptor(parseName(nn.getChild("name")));
95         } else {
96             throw new Error();
97         }
98     }
99
100     private NameDescriptor parseName(ParseNode nn) {
101         ParseNode base=nn.getChild("base");
102         ParseNode id=nn.getChild("identifier");
103         if (base==null)
104             return new NameDescriptor(id.getTerminal());
105         return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
106         
107     }
108
109     private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
110         ParseNode mn=pn.getChild("modifier");
111         Modifiers m=parseModifiersList(mn);
112
113         ParseNode tn=pn.getChild("type");
114         TypeDescriptor t=parseTypeDescriptor(tn);
115         ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
116         ParseNodeVector pnv=vn.getChildren();
117         for(int i=0;i<pnv.size();i++) {
118             ParseNode vardecl=pnv.elementAt(i);
119             String identifier=vardecl.getChild("single").getTerminal();
120             ParseNode epn=vardecl.getChild("initializer");
121             
122             ExpressionNode en=null;
123             if (epn!=null)
124                 en=parseExpression(epn.getFirstChild());
125   
126             cn.addField(new FieldDescriptor(m,t,identifier, en));
127         }
128         
129     }
130
131     private ExpressionNode parseExpression(ParseNode pn) {
132         if (isNode(pn,"assignment"))
133             return parseAssignmentExpression(pn);
134         else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
135                  isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
136                  isNode(pn,"bitwise_and")||isNode(pn,"equal")||
137                  isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
138                  isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
139                  isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
140                  isNode(pn,"rightshift")||isNode(pn,"sub")||
141                  isNode(pn,"add")||isNode(pn,"mult")||
142                  isNode(pn,"div")||isNode(pn,"mod")) {
143             ParseNodeVector pnv=pn.getChildren();
144             ParseNode left=pnv.elementAt(0);
145             ParseNode right=pnv.elementAt(1);
146             Operation op=new Operation(pn.getLabel());
147             return new OpNode(parseExpression(left),parseExpression(right),op);
148         } else if (isNode(pn,"unaryplus")||
149                    isNode(pn,"unaryminus")||
150                    isNode(pn,"postinc")||
151                    isNode(pn,"postdec")||
152                    isNode(pn,"preinc")||
153                    isNode(pn,"predec")) {
154             ParseNode left=pn.getFirstChild();
155             Operation op=new Operation(pn.getLabel());
156             return new OpNode(parseExpression(left),op);
157         } else if (isNode(pn,"literal")) {
158             String literaltype=pn.getTerminal();
159             ParseNode literalnode=pn.getChild(literaltype);
160             Object literal_obj=literalnode.getLiteral();
161             return new LiteralNode(literaltype, literal_obj);
162         } else if (isNode(pn,"createobject")) {
163             TypeDescriptor td=parseTypeDescriptor(pn);
164             Vector args=parseArgumentList(pn);
165             CreateObjectNode con=new CreateObjectNode(td);
166             for(int i=0;i<args.size();i++) {
167                 con.addArgument((ExpressionNode)args.get(i));
168             }
169             return con;
170         } else if (isNode(pn,"name")) {
171             NameDescriptor nd=parseName(pn);
172             return new NameNode(nd);
173         } else if (isNode(pn,"this")) {
174             NameDescriptor nd=new NameDescriptor("this");
175             return new NameNode(nd);
176         } else if (isNode(pn,"methodinvoke1")) {
177             NameDescriptor nd=parseName(pn.getChild("name"));
178             Vector args=parseArgumentList(pn);
179             MethodInvokeNode min=new MethodInvokeNode(nd);
180             for(int i=0;i<args.size();i++) {
181                 min.addArgument((ExpressionNode)args.get(i));
182             }
183             return min;
184         } else if (isNode(pn,"methodinvoke2")) {
185             String methodid=pn.getChild("id").getTerminal();
186             ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
187             Vector args=parseArgumentList(pn);
188             MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
189             for(int i=0;i<args.size();i++) {
190                 min.addArgument((ExpressionNode)args.get(i));
191             }
192             return min;
193         } else if (isNode(pn,"fieldaccess")) { 
194             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
195             String fieldname=pn.getChild("field").getTerminal();
196             return new FieldAccessNode(en,fieldname);
197         } else if (isNode(pn,"cast1")) { 
198             return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
199         } else if (isNode(pn,"cast2")) { 
200             return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
201         } else {
202             System.out.println("---------------------");
203             System.out.println(pn.PPrint(3,true));
204             throw new Error();
205         }
206     }
207
208     private Vector parseArgumentList(ParseNode pn) {
209         Vector arglist=new Vector();
210         ParseNode an=pn.getChild("argument_list");
211         if (an==null)   /* No argument list */
212             return arglist;
213         ParseNodeVector anv=an.getChildren();
214         for(int i=0;i<anv.size();i++) {
215             arglist.add(parseExpression(anv.elementAt(i)));
216         }
217         return arglist;
218     }
219
220     private ExpressionNode parseAssignmentExpression(ParseNode pn) {
221         AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
222         ParseNodeVector pnv=pn.getChild("args").getChildren();
223         
224         AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
225         return an;
226     }
227
228
229     private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
230         ParseNode headern=pn.getChild("method_header");
231         ParseNode bodyn=pn.getChild("body");
232         MethodDescriptor md=parseMethodHeader(headern);
233         BlockNode bn=parseBlock(bodyn);
234         cn.addMethod(md);
235         state.addTreeCode(md,bn);
236     }
237
238     public BlockNode parseBlock(ParseNode pn) {
239         if (isEmpty(pn.getTerminal()))
240             return new BlockNode();
241         ParseNode bsn=pn.getChild("block_statement_list");
242         return parseBlockHelper(bsn);
243     }
244     
245     private BlockNode parseBlockHelper(ParseNode pn) {
246         ParseNodeVector pnv=pn.getChildren();
247         BlockNode bn=new BlockNode();
248         for(int i=0;i<pnv.size();i++) {
249             Vector bsv=parseBlockStatement(pnv.elementAt(i));
250             for(int j=0;j<bsv.size();j++) {
251                 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
252             }
253         }
254         return bn;
255     }
256
257     public BlockNode parseSingleBlock(ParseNode pn) {
258         BlockNode bn=new BlockNode();
259         Vector bsv=parseBlockStatement(pn);
260         for(int j=0;j<bsv.size();j++) {
261             bn.addBlockStatement((BlockStatementNode)bsv.get(j));
262         }
263         bn.setStyle(BlockNode.NOBRACES);
264         return bn;
265     }
266
267     public Vector parseBlockStatement(ParseNode pn) {
268         Vector blockstatements=new Vector();
269         if (isNode(pn,"local_variable_declaration")) {
270             TypeDescriptor t=parseTypeDescriptor(pn);
271             ParseNode vn=pn.getChild("variable_declarators_list");
272             ParseNodeVector pnv=vn.getChildren();
273             for(int i=0;i<pnv.size();i++) {
274                 ParseNode vardecl=pnv.elementAt(i);
275                 String identifier=vardecl.getChild("single").getTerminal();
276                 ParseNode epn=vardecl.getChild("initializer");
277                 
278                 ExpressionNode en=null;
279                 if (epn!=null)
280                     en=parseExpression(epn.getFirstChild());
281                 
282                 blockstatements.add(new DeclarationNode(new VarDescriptor(t,identifier),en));
283             }
284         } else if (isNode(pn,"nop")) {
285             /* Do Nothing */
286         } else if (isNode(pn,"expression")) {
287             blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
288         } else if (isNode(pn,"ifstatement")) {
289             blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
290                                        parseSingleBlock(pn.getChild("statement").getFirstChild()),
291                                        pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
292         } else if (isNode(pn,"return")) {
293             if (isEmpty(pn.getTerminal()))
294                 blockstatements.add(new ReturnNode());
295             else {
296                 ExpressionNode en=parseExpression(pn.getFirstChild());
297                 blockstatements.add(new ReturnNode(en));
298             }
299         } else if (isNode(pn,"block_statement_list")) {
300             BlockNode bn=parseBlockHelper(pn);
301             blockstatements.add(new SubBlockNode(bn));
302         } else if (isNode(pn,"empty")) {
303             /* nop */
304         } else if (isNode(pn,"statement_expression_list")) {
305             ParseNodeVector pnv=pn.getChildren();
306             BlockNode bn=new BlockNode();
307             for(int i=0;i<pnv.size();i++) {
308                 ExpressionNode en=parseExpression(pnv.elementAt(i));
309                 blockstatements.add(new BlockExpressionNode(en));
310             }
311             bn.setStyle(BlockNode.EXPRLIST);
312         } else if (isNode(pn,"forstatement")) {
313             BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
314             BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
315             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
316             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
317             blockstatements.add(new LoopNode(init,condition,update,body));
318         } else {
319             System.out.println("---------------");
320             System.out.println(pn.PPrint(3,true));
321             throw new Error();
322         }
323         return blockstatements;
324     }
325
326     public MethodDescriptor parseMethodHeader(ParseNode pn) {
327         ParseNode mn=pn.getChild("modifiers");
328         Modifiers m=parseModifiersList(mn);
329         
330         ParseNode tn=pn.getChild("returntype");
331         TypeDescriptor returntype;
332         if (tn!=null) 
333             returntype=parseTypeDescriptor(tn);
334         else
335             returntype=new TypeDescriptor(TypeDescriptor.VOID);
336
337         ParseNode pmd=pn.getChild("method_declarator");
338         String name=pmd.getChild("name").getTerminal();
339         MethodDescriptor md=new MethodDescriptor(m, returntype, name);
340        
341         ParseNode paramnode=pmd.getChild("parameters");
342         parseParameterList(md,paramnode);
343         return md;
344     }
345
346     public void parseParameterList(MethodDescriptor md, ParseNode pn) {
347         ParseNode paramlist=pn.getChild("formal_parameter_list");
348         if (paramlist==null)
349             return;
350          ParseNodeVector pnv=paramlist.getChildren();
351          for(int i=0;i<pnv.size();i++) {
352              ParseNode paramn=pnv.elementAt(i);
353              TypeDescriptor type=parseTypeDescriptor(paramn);
354              String paramname=paramn.getChild("single").getTerminal();
355              md.addParameter(type,paramname);
356          }
357     }
358
359     public Modifiers parseModifiersList(ParseNode pn) {
360         Modifiers m=new Modifiers();
361         ParseNode modlist=pn.getChild("modifier_list");
362         if (modlist!=null) {
363             ParseNodeVector pnv=modlist.getChildren();
364             for(int i=0;i<pnv.size();i++) {
365                 ParseNode modn=pnv.elementAt(i);
366                 if (isNode(modn,"public"))
367                     m.addModifier(Modifiers.PUBLIC);
368                 if (isNode(modn,"protected"))
369                     m.addModifier(Modifiers.PROTECTED);
370                 if (isNode(modn,"private"))
371                     m.addModifier(Modifiers.PRIVATE);
372                 if (isNode(modn,"static"))
373                     m.addModifier(Modifiers.STATIC);
374                 if (isNode(modn,"final"))
375                     m.addModifier(Modifiers.FINAL);
376                 if (isNode(modn,"native"))
377                     m.addModifier(Modifiers.NATIVE);
378             }
379         }
380         return m;
381     }
382
383     private boolean isNode(ParseNode pn, String label) {
384         if (pn.getLabel().equals(label))
385             return true;
386         else return false;
387     }
388
389     private static boolean isEmpty(ParseNode pn) {
390         if (pn.getLabel().equals("empty"))
391             return true;
392         else
393             return false;
394     }
395
396     private static boolean isEmpty(String s) {
397         if (s.equals("empty"))
398             return true;
399         else
400             return false;
401     }
402
403     /** Throw an exception if something is unexpected */
404     private void check(ParseNode pn, String label) {
405         if (pn == null) {
406             throw new Error(pn+ "IE: Expected '" + label + "', got null");
407         }
408         if (! pn.getLabel().equals(label)) {
409             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
410         }
411     }
412 }