d577cbb115e6a9eded0a984449ac9160af9c8bdc
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3 import java.util.*;
4
5
6 public class BuildIR {
7   State state;
8
9   private int m_taskexitnum;
10
11   public BuildIR(State state) {
12     this.state=state;
13     this.m_taskexitnum = 0;
14   }
15
16   public void buildtree() {
17     for(Iterator it=state.parsetrees.iterator(); it.hasNext();) {
18       ParseNode pn=(ParseNode)it.next();
19       parseFile(pn);
20     }
21   }
22
23   /** Parse the classes in this file */
24   public void parseFile(ParseNode pn) {
25     NameDescriptor packages;
26     Vector singleimports=new Vector();
27     Vector multiimports=new Vector();
28
29     ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
30     if (ipn!=null) {
31       ParseNodeVector pnv=ipn.getChildren();
32       for(int i=0; i<pnv.size(); i++) {
33         ParseNode pnimport=pnv.elementAt(i);
34         NameDescriptor nd=parseName(pnimport.getChild("name"));
35         if (isNode(pnimport,"import_single"))
36           singleimports.add(nd);
37         else
38           multiimports.add(nd);
39       }
40     }
41     ParseNode ppn=pn.getChild("packages").getChild("package");
42     if (ppn!=null) {
43       packages=parseName(pn.getChild("name"));
44     }
45     ParseNode tpn=pn.getChild("type_declaration_list");
46     if (tpn!=null) {
47       ParseNodeVector pnv=tpn.getChildren();
48       for(int i=0; i<pnv.size(); i++) {
49         ParseNode type_pn=pnv.elementAt(i);
50         if (isEmpty(type_pn))         /* Skip the semicolon */
51           continue;
52         if (isNode(type_pn,"class_declaration")) {
53           ClassDescriptor cn=parseTypeDecl(type_pn);
54           state.addClass(cn);
55         } else if (isNode(type_pn,"task_declaration")) {
56           TaskDescriptor td=parseTaskDecl(type_pn);
57           state.addTask(td);
58         } else {
59           throw new Error(type_pn.getLabel());
60         }
61       }
62     }
63   }
64
65   public TaskDescriptor parseTaskDecl(ParseNode pn) {
66     TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
67     ParseNode bodyn=pn.getChild("body");
68     BlockNode bn=parseBlock(bodyn);
69     parseParameterList(td, pn);
70     state.addTreeCode(td,bn);
71     if (pn.getChild("flag_effects_list")!=null)
72       td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
73     return td;
74   }
75
76   public Vector parseFlags(ParseNode pn) {
77     Vector vfe=new Vector();
78     ParseNodeVector pnv=pn.getChildren();
79     for(int i=0; i<pnv.size(); i++) {
80       ParseNode fn=pnv.elementAt(i);
81       FlagEffects fe=parseFlagEffects(fn);
82       vfe.add(fe);
83     }
84     return vfe;
85   }
86
87   public FlagEffects parseFlagEffects(ParseNode pn) {
88     if (isNode(pn,"flag_effect")) {
89       String flagname=pn.getChild("name").getTerminal();
90       FlagEffects fe=new FlagEffects(flagname);
91       if (pn.getChild("flag_list")!=null)
92         parseFlagEffect(fe, pn.getChild("flag_list"));
93       if (pn.getChild("tag_list")!=null)
94         parseTagEffect(fe, pn.getChild("tag_list"));
95       return fe;
96     } else throw new Error();
97   }
98
99   public void parseTagEffect(FlagEffects fes, ParseNode pn) {
100     ParseNodeVector pnv=pn.getChildren();
101     for(int i=0; i<pnv.size(); i++) {
102       ParseNode pn2=pnv.elementAt(i);
103       boolean status=true;
104       if (isNode(pn2,"not")) {
105         status=false;
106         pn2=pn2.getChild("name");
107       }
108       String name=pn2.getTerminal();
109       fes.addTagEffect(new TagEffect(name,status));
110     }
111   }
112
113   public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
114     ParseNodeVector pnv=pn.getChildren();
115     for(int i=0; i<pnv.size(); i++) {
116       ParseNode pn2=pnv.elementAt(i);
117       boolean status=true;
118       if (isNode(pn2,"not")) {
119         status=false;
120         pn2=pn2.getChild("name");
121       }
122       String name=pn2.getTerminal();
123       fes.addEffect(new FlagEffect(name,status));
124     }
125   }
126
127   public FlagExpressionNode parseFlagExpression(ParseNode pn) {
128     if (isNode(pn,"or")) {
129       ParseNodeVector pnv=pn.getChildren();
130       ParseNode left=pnv.elementAt(0);
131       ParseNode right=pnv.elementAt(1);
132       return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
133     } else if (isNode(pn,"and")) {
134       ParseNodeVector pnv=pn.getChildren();
135       ParseNode left=pnv.elementAt(0);
136       ParseNode right=pnv.elementAt(1);
137       return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
138     } else if (isNode(pn, "not")) {
139       ParseNodeVector pnv=pn.getChildren();
140       ParseNode left=pnv.elementAt(0);
141       return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
142
143     } else if (isNode(pn,"name")) {
144       return new FlagNode(pn.getTerminal());
145     } else {
146       throw new Error();
147     }
148   }
149
150   public Vector parseChecks(ParseNode pn) {
151     Vector ccs=new Vector();
152     ParseNodeVector pnv=pn.getChildren();
153     for(int i=0; i<pnv.size(); i++) {
154       ParseNode fn=pnv.elementAt(i);
155       ConstraintCheck cc=parseConstraintCheck(fn);
156       ccs.add(cc);
157     }
158     return ccs;
159   }
160
161   public ConstraintCheck parseConstraintCheck(ParseNode pn) {
162     if (isNode(pn,"cons_check")) {
163       String specname=pn.getChild("name").getChild("identifier").getTerminal();
164       Vector[] args=parseConsArgumentList(pn);
165       ConstraintCheck cc=new ConstraintCheck(specname);
166       for(int i=0; i<args[0].size(); i++) {
167         cc.addVariable((String)args[0].get(i));
168         cc.addArgument((ExpressionNode)args[1].get(i));
169       }
170       return cc;
171     } else throw new Error();
172   }
173
174   public void parseParameterList(TaskDescriptor td, ParseNode pn) {
175
176     boolean optional;
177     ParseNode paramlist=pn.getChild("task_parameter_list");
178     if (paramlist==null)
179       return;
180     ParseNodeVector pnv=paramlist.getChildren();
181     for(int i=0; i<pnv.size(); i++) {
182       ParseNode paramn=pnv.elementAt(i);
183       if(paramn.getChild("optional")!=null) {
184         optional = true;
185         paramn = paramn.getChild("optional").getFirstChild();
186         System.out.println("OPTIONAL FOUND!!!!!!!");
187       } else { optional = false;
188                System.out.println("NOT OPTIONAL");}
189
190       TypeDescriptor type=parseTypeDescriptor(paramn);
191
192       String paramname=paramn.getChild("single").getTerminal();
193       FlagExpressionNode fen=null;
194       if (paramn.getChild("flag")!=null)
195         fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
196
197       ParseNode tagnode=paramn.getChild("tag");
198
199       TagExpressionList tel=null;
200       if (tagnode!=null) {
201         tel=parseTagExpressionList(tagnode);
202       }
203
204       td.addParameter(type,paramname,fen, tel, optional);
205     }
206   }
207
208   public TagExpressionList parseTagExpressionList(ParseNode pn) {
209     //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
210     //To test, feed in any input program that uses tags
211     ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
212     TagExpressionList tel=new TagExpressionList();
213     for(int i=0; i<pnv.size(); i++) {
214       ParseNode tn=pnv.elementAt(i);
215       String type=tn.getChild("type").getTerminal();
216       String name=tn.getChild("single").getTerminal();
217       tel.addTag(type, name);
218     }
219     return tel;
220   }
221
222   public ClassDescriptor parseTypeDecl(ParseNode pn) {
223     ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
224     if (!isEmpty(pn.getChild("super").getTerminal())) {
225       /* parse superclass name */
226       ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
227       NameDescriptor nd=parseName(snn);
228       cn.setSuper(nd.toString());
229     } else {
230       if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
231             cn.getSymbol().equals(TypeUtil.TagClass)))
232         cn.setSuper(TypeUtil.ObjectClass);
233     }
234     cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
235     parseClassBody(cn, pn.getChild("classbody"));
236     return cn;
237   }
238
239   private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
240     ParseNode decls=pn.getChild("class_body_declaration_list");
241     if (decls!=null) {
242       ParseNodeVector pnv=decls.getChildren();
243       for(int i=0; i<pnv.size(); i++) {
244         ParseNode decl=pnv.elementAt(i);
245         if (isNode(decl,"member")) {
246           parseClassMember(cn,decl);
247         } else if (isNode(decl,"constructor")) {
248           parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
249         } else if (isNode(decl,"block")) {
250         } else throw new Error();
251       }
252     }
253   }
254
255   private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
256     ParseNode fieldnode=pn.getChild("field");
257
258     if (fieldnode!=null) {
259       parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
260       return;
261     }
262     ParseNode methodnode=pn.getChild("method");
263     if (methodnode!=null) {
264       parseMethodDecl(cn,methodnode.getChild("method_declaration"));
265       return;
266     }
267     ParseNode flagnode=pn.getChild("flag");
268     if (flagnode!=null) {
269       parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
270       return;
271     }
272     throw new Error();
273   }
274
275   private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
276     ParseNode tn=pn.getChild("type");
277     String type_st=tn.getTerminal();
278     if(type_st.equals("byte")) {
279       return state.getTypeDescriptor(TypeDescriptor.BYTE);
280     } else if(type_st.equals("short")) {
281       return state.getTypeDescriptor(TypeDescriptor.SHORT);
282     } else if(type_st.equals("boolean")) {
283       return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
284     } else if(type_st.equals("int")) {
285       return state.getTypeDescriptor(TypeDescriptor.INT);
286     } else if(type_st.equals("long")) {
287       return state.getTypeDescriptor(TypeDescriptor.LONG);
288     } else if(type_st.equals("char")) {
289       return state.getTypeDescriptor(TypeDescriptor.CHAR);
290     } else if(type_st.equals("float")) {
291       return state.getTypeDescriptor(TypeDescriptor.FLOAT);
292     } else if(type_st.equals("double")) {
293       return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
294     } else if(type_st.equals("class")) {
295       ParseNode nn=tn.getChild("class");
296       return state.getTypeDescriptor(parseName(nn.getChild("name")));
297     } else if(type_st.equals("array")) {
298       ParseNode nn=tn.getChild("array");
299       TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
300       Integer numdims=(Integer)nn.getChild("dims").getLiteral();
301       for(int i=0; i<numdims.intValue(); i++)
302         td=td.makeArray(state);
303       return td;
304     } else {
305       throw new Error();
306     }
307   }
308
309   private NameDescriptor parseName(ParseNode nn) {
310     ParseNode base=nn.getChild("base");
311     ParseNode id=nn.getChild("identifier");
312     if (base==null)
313       return new NameDescriptor(id.getTerminal());
314     return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
315
316   }
317
318   private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
319     String name=pn.getChild("name").getTerminal();
320     FlagDescriptor flag=new FlagDescriptor(name);
321     if (pn.getChild("external")!=null)
322       flag.makeExternal();
323     cn.addFlag(flag);
324   }
325
326   private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
327     ParseNode mn=pn.getChild("modifier");
328     Modifiers m=parseModifiersList(mn);
329
330     ParseNode tn=pn.getChild("type");
331     TypeDescriptor t=parseTypeDescriptor(tn);
332     ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
333     ParseNodeVector pnv=vn.getChildren();
334     boolean isglobal=pn.getChild("global")!=null;
335
336     for(int i=0; i<pnv.size(); i++) {
337       ParseNode vardecl=pnv.elementAt(i);
338       ParseNode tmp=vardecl;
339       TypeDescriptor arrayt=t;
340       while (tmp.getChild("single")==null) {
341         arrayt=arrayt.makeArray(state);
342         tmp=tmp.getChild("array");
343       }
344       String identifier=tmp.getChild("single").getTerminal();
345       ParseNode epn=vardecl.getChild("initializer");
346
347       ExpressionNode en=null;
348       if (epn!=null)
349         en=parseExpression(epn.getFirstChild());
350
351       cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
352     }
353   }
354
355   private ExpressionNode parseExpression(ParseNode pn) {
356     if (isNode(pn,"assignment"))
357       return parseAssignmentExpression(pn);
358     else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
359              isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
360              isNode(pn,"bitwise_and")||isNode(pn,"equal")||
361              isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
362              isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
363              isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
364              isNode(pn,"rightshift")||isNode(pn,"sub")||
365              isNode(pn,"urightshift")||isNode(pn,"sub")||
366              isNode(pn,"add")||isNode(pn,"mult")||
367              isNode(pn,"div")||isNode(pn,"mod")) {
368       ParseNodeVector pnv=pn.getChildren();
369       ParseNode left=pnv.elementAt(0);
370       ParseNode right=pnv.elementAt(1);
371       Operation op=new Operation(pn.getLabel());
372       return new OpNode(parseExpression(left),parseExpression(right),op);
373     } else if (isNode(pn,"unaryplus")||
374                isNode(pn,"unaryminus")||
375                isNode(pn,"not")||
376                isNode(pn,"comp")) {
377       ParseNode left=pn.getFirstChild();
378       Operation op=new Operation(pn.getLabel());
379       return new OpNode(parseExpression(left),op);
380     } else if (isNode(pn,"postinc")||
381                isNode(pn,"postdec")) {
382       ParseNode left=pn.getFirstChild();
383       AssignOperation op=new AssignOperation(pn.getLabel());
384       return new AssignmentNode(parseExpression(left),null,op);
385
386     } else if (isNode(pn,"preinc")||
387                isNode(pn,"predec")) {
388       ParseNode left=pn.getFirstChild();
389       AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
390       return new AssignmentNode(parseExpression(left),
391                                 new LiteralNode("integer",new Integer(1)),op);
392     } else if (isNode(pn,"literal")) {
393       String literaltype=pn.getTerminal();
394       ParseNode literalnode=pn.getChild(literaltype);
395       Object literal_obj=literalnode.getLiteral();
396       return new LiteralNode(literaltype, literal_obj);
397     } else if (isNode(pn,"createobject")) {
398       TypeDescriptor td=parseTypeDescriptor(pn);
399       Vector args=parseArgumentList(pn);
400       boolean isglobal=pn.getChild("global")!=null;
401       CreateObjectNode con=new CreateObjectNode(td, isglobal);
402       for(int i=0; i<args.size(); i++) {
403         con.addArgument((ExpressionNode)args.get(i));
404       }
405       /* Could have flag set or tag added here */
406       if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
407         FlagEffects fe=new FlagEffects(null);
408         if (pn.getChild("flag_list")!=null)
409           parseFlagEffect(fe, pn.getChild("flag_list"));
410
411         if (pn.getChild("tag_list")!=null)
412           parseTagEffect(fe, pn.getChild("tag_list"));
413         con.addFlagEffects(fe);
414       }
415
416       return con;
417     } else if (isNode(pn,"createarray")) {
418       //System.out.println(pn.PPrint(3,true));
419       boolean isglobal=pn.getChild("global")!=null;
420       TypeDescriptor td=parseTypeDescriptor(pn);
421       Vector args=parseDimExprs(pn);
422       int num=0;
423       if (pn.getChild("dims_opt").getLiteral()!=null)
424         num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
425       for(int i=0; i<(args.size()+num); i++)
426         td=td.makeArray(state);
427       CreateObjectNode con=new CreateObjectNode(td, isglobal);
428       for(int i=0; i<args.size(); i++) {
429         con.addArgument((ExpressionNode)args.get(i));
430       }
431       return con;
432     } else if (isNode(pn,"name")) {
433       NameDescriptor nd=parseName(pn);
434       return new NameNode(nd);
435     } else if (isNode(pn,"this")) {
436       NameDescriptor nd=new NameDescriptor("this");
437       return new NameNode(nd);
438     } else if (isNode(pn,"isavailable")) {
439       NameDescriptor nd=new NameDescriptor(pn.getTerminal());
440       return new OpNode(new NameNode(nd),null,new Operation(Operation.ISAVAILABLE));
441     } else if (isNode(pn,"methodinvoke1")) {
442       NameDescriptor nd=parseName(pn.getChild("name"));
443       Vector args=parseArgumentList(pn);
444       MethodInvokeNode min=new MethodInvokeNode(nd);
445       for(int i=0; i<args.size(); i++) {
446         min.addArgument((ExpressionNode)args.get(i));
447       }
448       return min;
449     } else if (isNode(pn,"methodinvoke2")) {
450       String methodid=pn.getChild("id").getTerminal();
451       ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
452       Vector args=parseArgumentList(pn);
453       MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
454       for(int i=0; i<args.size(); i++) {
455         min.addArgument((ExpressionNode)args.get(i));
456       }
457       return min;
458     } else if (isNode(pn,"fieldaccess")) {
459       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
460       String fieldname=pn.getChild("field").getTerminal();
461       return new FieldAccessNode(en,fieldname);
462     } else if (isNode(pn,"arrayaccess")) {
463       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
464       ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
465       return new ArrayAccessNode(en,index);
466     } else if (isNode(pn,"cast1")) {
467       return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
468     } else if (isNode(pn,"cast2")) {
469       return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
470     } else if (isNode(pn, "getoffset")) {
471       TypeDescriptor td=parseTypeDescriptor(pn);
472       String fieldname = pn.getChild("field").getTerminal();
473       //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ "  fieldname= " + fieldname);
474       return new OffsetNode(td, fieldname);
475     } else {
476       System.out.println("---------------------");
477       System.out.println(pn.PPrint(3,true));
478       throw new Error();
479     }
480   }
481
482   private Vector parseDimExprs(ParseNode pn) {
483     Vector arglist=new Vector();
484     ParseNode an=pn.getChild("dim_exprs");
485     if (an==null)       /* No argument list */
486       return arglist;
487     ParseNodeVector anv=an.getChildren();
488     for(int i=0; i<anv.size(); i++) {
489       arglist.add(parseExpression(anv.elementAt(i)));
490     }
491     return arglist;
492   }
493
494   private Vector parseArgumentList(ParseNode pn) {
495     Vector arglist=new Vector();
496     ParseNode an=pn.getChild("argument_list");
497     if (an==null)       /* No argument list */
498       return arglist;
499     ParseNodeVector anv=an.getChildren();
500     for(int i=0; i<anv.size(); i++) {
501       arglist.add(parseExpression(anv.elementAt(i)));
502     }
503     return arglist;
504   }
505
506   private Vector[] parseConsArgumentList(ParseNode pn) {
507     Vector arglist=new Vector();
508     Vector varlist=new Vector();
509     ParseNode an=pn.getChild("cons_argument_list");
510     if (an==null)       /* No argument list */
511       return new Vector[] {varlist, arglist};
512     ParseNodeVector anv=an.getChildren();
513     for(int i=0; i<anv.size(); i++) {
514       ParseNode cpn=anv.elementAt(i);
515       ParseNode var=cpn.getChild("var");
516       ParseNode exp=cpn.getChild("exp").getFirstChild();
517       varlist.add(var.getTerminal());
518       arglist.add(parseExpression(exp));
519     }
520     return new Vector[] {varlist, arglist};
521   }
522
523   private ExpressionNode parseAssignmentExpression(ParseNode pn) {
524     AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
525     ParseNodeVector pnv=pn.getChild("args").getChildren();
526
527     AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
528     return an;
529   }
530
531
532   private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
533     ParseNode headern=pn.getChild("method_header");
534     ParseNode bodyn=pn.getChild("body");
535     MethodDescriptor md=parseMethodHeader(headern);
536     BlockNode bn=parseBlock(bodyn);
537     cn.addMethod(md);
538     state.addTreeCode(md,bn);
539   }
540
541   private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
542     ParseNode mn=pn.getChild("modifiers");
543     Modifiers m=parseModifiersList(mn);
544     ParseNode cdecl=pn.getChild("constructor_declarator");
545     boolean isglobal=cdecl.getChild("global")!=null;
546     String name=cdecl.getChild("name").getChild("identifier").getTerminal();
547     MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
548     ParseNode paramnode=cdecl.getChild("parameters");
549     parseParameterList(md,paramnode);
550     ParseNode bodyn0=pn.getChild("body");
551     ParseNode bodyn=bodyn0.getChild("constructor_body");
552     cn.addMethod(md);
553     BlockNode bn=null;
554     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
555       bn=parseBlock(bodyn);
556     else
557       bn=new BlockNode();
558     if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
559       ParseNode sin=bodyn.getChild("superinvoke");
560       NameDescriptor nd=new NameDescriptor("super");
561       Vector args=parseArgumentList(sin);
562       MethodInvokeNode min=new MethodInvokeNode(nd);
563       for(int i=0; i<args.size(); i++) {
564         min.addArgument((ExpressionNode)args.get(i));
565       }
566       BlockExpressionNode ben=new BlockExpressionNode(min);
567       bn.addFirstBlockStatement(ben);
568     }
569     state.addTreeCode(md,bn);
570   }
571
572   public BlockNode parseBlock(ParseNode pn) {
573     this.m_taskexitnum = 0;
574     if (pn==null||isEmpty(pn.getTerminal()))
575       return new BlockNode();
576     ParseNode bsn=pn.getChild("block_statement_list");
577     return parseBlockHelper(bsn);
578   }
579
580   private BlockNode parseBlockHelper(ParseNode pn) {
581     ParseNodeVector pnv=pn.getChildren();
582     BlockNode bn=new BlockNode();
583     for(int i=0; i<pnv.size(); i++) {
584       Vector bsv=parseBlockStatement(pnv.elementAt(i));
585       for(int j=0; j<bsv.size(); j++) {
586         bn.addBlockStatement((BlockStatementNode)bsv.get(j));
587       }
588     }
589     return bn;
590   }
591
592   public BlockNode parseSingleBlock(ParseNode pn) {
593     BlockNode bn=new BlockNode();
594     Vector bsv=parseBlockStatement(pn);
595     for(int j=0; j<bsv.size(); j++) {
596       bn.addBlockStatement((BlockStatementNode)bsv.get(j));
597     }
598     bn.setStyle(BlockNode.NOBRACES);
599     return bn;
600   }
601
602   public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
603     ParseNodeVector pnv=pn.getChildren();
604     Vector bv=new Vector();
605     for(int i=0; i<pnv.size(); i++) {
606       bv.addAll(parseBlockStatement(pnv.elementAt(i)));
607     }
608     return bv;
609   }
610
611   public Vector parseBlockStatement(ParseNode pn) {
612     Vector blockstatements=new Vector();
613     if (isNode(pn,"tag_declaration")) {
614       String name=pn.getChild("single").getTerminal();
615       String type=pn.getChild("type").getTerminal();
616
617       blockstatements.add(new TagDeclarationNode(name, type));
618     } else if (isNode(pn,"local_variable_declaration")) {
619       TypeDescriptor t=parseTypeDescriptor(pn);
620       ParseNode vn=pn.getChild("variable_declarators_list");
621       ParseNodeVector pnv=vn.getChildren();
622       for(int i=0; i<pnv.size(); i++) {
623         ParseNode vardecl=pnv.elementAt(i);
624
625
626         ParseNode tmp=vardecl;
627         TypeDescriptor arrayt=t;
628         while (tmp.getChild("single")==null) {
629           arrayt=arrayt.makeArray(state);
630           tmp=tmp.getChild("array");
631         }
632         String identifier=tmp.getChild("single").getTerminal();
633
634         ParseNode epn=vardecl.getChild("initializer");
635
636
637         ExpressionNode en=null;
638         if (epn!=null)
639           en=parseExpression(epn.getFirstChild());
640
641         blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
642       }
643     } else if (isNode(pn,"nop")) {
644       /* Do Nothing */
645     } else if (isNode(pn,"expression")) {
646       blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
647     } else if (isNode(pn,"ifstatement")) {
648       blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
649                                               parseSingleBlock(pn.getChild("statement").getFirstChild()),
650                                               pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null));
651     } else if (isNode(pn,"taskexit")) {
652       Vector vfe=null;
653       if (pn.getChild("flag_effects_list")!=null)
654         vfe=parseFlags(pn.getChild("flag_effects_list"));
655       Vector ccs=null;
656       if (pn.getChild("cons_checks")!=null)
657         ccs=parseChecks(pn.getChild("cons_checks"));
658
659       blockstatements.add(new TaskExitNode(vfe, ccs, this.m_taskexitnum++));
660     } else if (isNode(pn,"atomic")) {
661       BlockNode bn=parseBlockHelper(pn);
662       blockstatements.add(new AtomicNode(bn));
663     } else if (isNode(pn,"return")) {
664       if (isEmpty(pn.getTerminal()))
665         blockstatements.add(new ReturnNode());
666       else {
667         ExpressionNode en=parseExpression(pn.getFirstChild());
668         blockstatements.add(new ReturnNode(en));
669       }
670     } else if (isNode(pn,"block_statement_list")) {
671       BlockNode bn=parseBlockHelper(pn);
672       blockstatements.add(new SubBlockNode(bn));
673     } else if (isNode(pn,"empty")) {
674       /* nop */
675     } else if (isNode(pn,"statement_expression_list")) {
676       ParseNodeVector pnv=pn.getChildren();
677       BlockNode bn=new BlockNode();
678       for(int i=0; i<pnv.size(); i++) {
679         ExpressionNode en=parseExpression(pnv.elementAt(i));
680         blockstatements.add(new BlockExpressionNode(en));
681       }
682       bn.setStyle(BlockNode.EXPRLIST);
683     } else if (isNode(pn,"forstatement")) {
684       BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
685       BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
686       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
687       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
688       blockstatements.add(new LoopNode(init,condition,update,body));
689     } else if (isNode(pn,"whilestatement")) {
690       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
691       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
692       blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
693     } else if (isNode(pn,"dowhilestatement")) {
694       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
695       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
696       blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
697     } else if (isNode(pn,"sese")) {
698       SESENode start=new SESENode();
699       SESENode end  =new SESENode();
700       start.setEnd( end   );
701       end.setStart( start );
702       blockstatements.add(start);
703       blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
704       blockstatements.add(end);
705     } else {
706       System.out.println("---------------");
707       System.out.println(pn.PPrint(3,true));
708       throw new Error();
709     }
710     return blockstatements;
711   }
712
713   public MethodDescriptor parseMethodHeader(ParseNode pn) {
714     ParseNode mn=pn.getChild("modifiers");
715     Modifiers m=parseModifiersList(mn);
716
717     ParseNode tn=pn.getChild("returntype");
718     TypeDescriptor returntype;
719     if (tn!=null)
720       returntype=parseTypeDescriptor(tn);
721     else
722       returntype=new TypeDescriptor(TypeDescriptor.VOID);
723
724     ParseNode pmd=pn.getChild("method_declarator");
725     String name=pmd.getChild("name").getTerminal();
726     MethodDescriptor md=new MethodDescriptor(m, returntype, name);
727
728     ParseNode paramnode=pmd.getChild("parameters");
729     parseParameterList(md,paramnode);
730     return md;
731   }
732
733   public void parseParameterList(MethodDescriptor md, ParseNode pn) {
734     ParseNode paramlist=pn.getChild("formal_parameter_list");
735     if (paramlist==null)
736       return;
737     ParseNodeVector pnv=paramlist.getChildren();
738     for(int i=0; i<pnv.size(); i++) {
739       ParseNode paramn=pnv.elementAt(i);
740
741       if (isNode(paramn, "tag_parameter")) {
742         String paramname=paramn.getChild("single").getTerminal();
743         TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
744         md.addTagParameter(type, paramname);
745       } else {
746         TypeDescriptor type=parseTypeDescriptor(paramn);
747
748         ParseNode tmp=paramn;
749         while (tmp.getChild("single")==null) {
750           type=type.makeArray(state);
751           tmp=tmp.getChild("array");
752         }
753         String paramname=tmp.getChild("single").getTerminal();
754
755         md.addParameter(type, paramname);
756       }
757     }
758   }
759
760   public Modifiers parseModifiersList(ParseNode pn) {
761     Modifiers m=new Modifiers();
762     ParseNode modlist=pn.getChild("modifier_list");
763     if (modlist!=null) {
764       ParseNodeVector pnv=modlist.getChildren();
765       for(int i=0; i<pnv.size(); i++) {
766         ParseNode modn=pnv.elementAt(i);
767         if (isNode(modn,"public"))
768           m.addModifier(Modifiers.PUBLIC);
769         else if (isNode(modn,"protected"))
770           m.addModifier(Modifiers.PROTECTED);
771         else if (isNode(modn,"private"))
772           m.addModifier(Modifiers.PRIVATE);
773         else if (isNode(modn,"static"))
774           m.addModifier(Modifiers.STATIC);
775         else if (isNode(modn,"final"))
776           m.addModifier(Modifiers.FINAL);
777         else if (isNode(modn,"native"))
778           m.addModifier(Modifiers.NATIVE);
779         else if (isNode(modn,"synchronized"))
780           m.addModifier(Modifiers.SYNCHRONIZED);
781         else if (isNode(modn,"atomic"))
782           m.addModifier(Modifiers.ATOMIC);
783         else throw new Error("Unrecognized Modifier");
784       }
785     }
786     return m;
787   }
788
789   private boolean isNode(ParseNode pn, String label) {
790     if (pn.getLabel().equals(label))
791       return true;
792     else return false;
793   }
794
795   private static boolean isEmpty(ParseNode pn) {
796     if (pn.getLabel().equals("empty"))
797       return true;
798     else
799       return false;
800   }
801
802   private static boolean isEmpty(String s) {
803     if (s.equals("empty"))
804       return true;
805     else
806       return false;
807   }
808
809   /** Throw an exception if something is unexpected */
810   private void check(ParseNode pn, String label) {
811     if (pn == null) {
812       throw new Error(pn+ "IE: Expected '" + label + "', got null");
813     }
814     if (!pn.getLabel().equals(label)) {
815       throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
816     }
817   }
818 }