modified compiler files for adding new keyword "getoffset" and adding
[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 parseBlockStatement(ParseNode pn) {
603     Vector blockstatements=new Vector();
604     if (isNode(pn,"tag_declaration")) {
605       String name=pn.getChild("single").getTerminal();
606       String type=pn.getChild("type").getTerminal();
607
608       blockstatements.add(new TagDeclarationNode(name, type));
609     } else if (isNode(pn,"local_variable_declaration")) {
610       TypeDescriptor t=parseTypeDescriptor(pn);
611       ParseNode vn=pn.getChild("variable_declarators_list");
612       ParseNodeVector pnv=vn.getChildren();
613       for(int i=0; i<pnv.size(); i++) {
614         ParseNode vardecl=pnv.elementAt(i);
615
616
617         ParseNode tmp=vardecl;
618         TypeDescriptor arrayt=t;
619         while (tmp.getChild("single")==null) {
620           arrayt=arrayt.makeArray(state);
621           tmp=tmp.getChild("array");
622         }
623         String identifier=tmp.getChild("single").getTerminal();
624
625         ParseNode epn=vardecl.getChild("initializer");
626
627
628         ExpressionNode en=null;
629         if (epn!=null)
630           en=parseExpression(epn.getFirstChild());
631
632         blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
633       }
634     } else if (isNode(pn,"nop")) {
635       /* Do Nothing */
636     } else if (isNode(pn,"expression")) {
637       blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
638     } else if (isNode(pn,"ifstatement")) {
639       blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
640                                               parseSingleBlock(pn.getChild("statement").getFirstChild()),
641                                               pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null));
642     } else if (isNode(pn,"taskexit")) {
643       Vector vfe=null;
644       if (pn.getChild("flag_effects_list")!=null)
645         vfe=parseFlags(pn.getChild("flag_effects_list"));
646       Vector ccs=null;
647       if (pn.getChild("cons_checks")!=null)
648         ccs=parseChecks(pn.getChild("cons_checks"));
649
650       blockstatements.add(new TaskExitNode(vfe, ccs, this.m_taskexitnum++));
651     } else if (isNode(pn,"atomic")) {
652       BlockNode bn=parseBlockHelper(pn);
653       blockstatements.add(new AtomicNode(bn));
654     } else if (isNode(pn,"return")) {
655       if (isEmpty(pn.getTerminal()))
656         blockstatements.add(new ReturnNode());
657       else {
658         ExpressionNode en=parseExpression(pn.getFirstChild());
659         blockstatements.add(new ReturnNode(en));
660       }
661     } else if (isNode(pn,"block_statement_list")) {
662       BlockNode bn=parseBlockHelper(pn);
663       blockstatements.add(new SubBlockNode(bn));
664     } else if (isNode(pn,"empty")) {
665       /* nop */
666     } else if (isNode(pn,"statement_expression_list")) {
667       ParseNodeVector pnv=pn.getChildren();
668       BlockNode bn=new BlockNode();
669       for(int i=0; i<pnv.size(); i++) {
670         ExpressionNode en=parseExpression(pnv.elementAt(i));
671         blockstatements.add(new BlockExpressionNode(en));
672       }
673       bn.setStyle(BlockNode.EXPRLIST);
674     } else if (isNode(pn,"forstatement")) {
675       BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
676       BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
677       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
678       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
679       blockstatements.add(new LoopNode(init,condition,update,body));
680     } else if (isNode(pn,"whilestatement")) {
681       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
682       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
683       blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
684     } else if (isNode(pn,"dowhilestatement")) {
685       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
686       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
687       blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
688     } else {
689       System.out.println("---------------");
690       System.out.println(pn.PPrint(3,true));
691       throw new Error();
692     }
693     return blockstatements;
694   }
695
696   public MethodDescriptor parseMethodHeader(ParseNode pn) {
697     ParseNode mn=pn.getChild("modifiers");
698     Modifiers m=parseModifiersList(mn);
699
700     ParseNode tn=pn.getChild("returntype");
701     TypeDescriptor returntype;
702     if (tn!=null)
703       returntype=parseTypeDescriptor(tn);
704     else
705       returntype=new TypeDescriptor(TypeDescriptor.VOID);
706
707     ParseNode pmd=pn.getChild("method_declarator");
708     String name=pmd.getChild("name").getTerminal();
709     MethodDescriptor md=new MethodDescriptor(m, returntype, name);
710
711     ParseNode paramnode=pmd.getChild("parameters");
712     parseParameterList(md,paramnode);
713     return md;
714   }
715
716   public void parseParameterList(MethodDescriptor md, ParseNode pn) {
717     ParseNode paramlist=pn.getChild("formal_parameter_list");
718     if (paramlist==null)
719       return;
720     ParseNodeVector pnv=paramlist.getChildren();
721     for(int i=0; i<pnv.size(); i++) {
722       ParseNode paramn=pnv.elementAt(i);
723
724       if (isNode(paramn, "tag_parameter")) {
725         String paramname=paramn.getChild("single").getTerminal();
726         TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
727         md.addTagParameter(type, paramname);
728       } else {
729         TypeDescriptor type=parseTypeDescriptor(paramn);
730
731         ParseNode tmp=paramn;
732         while (tmp.getChild("single")==null) {
733           type=type.makeArray(state);
734           tmp=tmp.getChild("array");
735         }
736         String paramname=tmp.getChild("single").getTerminal();
737
738         md.addParameter(type, paramname);
739       }
740     }
741   }
742
743   public Modifiers parseModifiersList(ParseNode pn) {
744     Modifiers m=new Modifiers();
745     ParseNode modlist=pn.getChild("modifier_list");
746     if (modlist!=null) {
747       ParseNodeVector pnv=modlist.getChildren();
748       for(int i=0; i<pnv.size(); i++) {
749         ParseNode modn=pnv.elementAt(i);
750         if (isNode(modn,"public"))
751           m.addModifier(Modifiers.PUBLIC);
752         else if (isNode(modn,"protected"))
753           m.addModifier(Modifiers.PROTECTED);
754         else if (isNode(modn,"private"))
755           m.addModifier(Modifiers.PRIVATE);
756         else if (isNode(modn,"static"))
757           m.addModifier(Modifiers.STATIC);
758         else if (isNode(modn,"final"))
759           m.addModifier(Modifiers.FINAL);
760         else if (isNode(modn,"native"))
761           m.addModifier(Modifiers.NATIVE);
762         else if (isNode(modn,"synchronized"))
763           m.addModifier(Modifiers.SYNCHRONIZED);
764         else if (isNode(modn,"atomic"))
765           m.addModifier(Modifiers.ATOMIC);
766         else throw new Error("Unrecognized Modifier");
767       }
768     }
769     return m;
770   }
771
772   private boolean isNode(ParseNode pn, String label) {
773     if (pn.getLabel().equals(label))
774       return true;
775     else return false;
776   }
777
778   private static boolean isEmpty(ParseNode pn) {
779     if (pn.getLabel().equals("empty"))
780       return true;
781     else
782       return false;
783   }
784
785   private static boolean isEmpty(String s) {
786     if (s.equals("empty"))
787       return true;
788     else
789       return false;
790   }
791
792   /** Throw an exception if something is unexpected */
793   private void check(ParseNode pn, String label) {
794     if (pn == null) {
795       throw new Error(pn+ "IE: Expected '" + label + "', got null");
796     }
797     if (!pn.getLabel().equals(label)) {
798       throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
799     }
800   }
801 }