Massive commit...
[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(ParseNode pn) {
17       parseFile(pn);
18   }
19
20   Vector singleimports;
21   Vector multiimports;
22   NameDescriptor packages;
23
24   /** Parse the classes in this file */
25   public void parseFile(ParseNode pn) {
26     singleimports=new Vector();
27     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       
400       Vector args=parseArgumentList(pn);
401       boolean isglobal=pn.getChild("global")!=null;
402       String disjointId=null;
403       if( pn.getChild("disjoint") != null) {
404         disjointId = pn.getChild("disjoint").getTerminal();
405       }
406       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
407       for(int i=0; i<args.size(); i++) {
408         con.addArgument((ExpressionNode)args.get(i));
409       }
410       /* Could have flag set or tag added here */
411       if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
412         FlagEffects fe=new FlagEffects(null);
413         if (pn.getChild("flag_list")!=null)
414           parseFlagEffect(fe, pn.getChild("flag_list"));
415
416         if (pn.getChild("tag_list")!=null)
417           parseTagEffect(fe, pn.getChild("tag_list"));
418         con.addFlagEffects(fe);
419       }
420
421       return con;
422     } else if (isNode(pn,"createarray")) {
423       //System.out.println(pn.PPrint(3,true));
424       boolean isglobal=pn.getChild("global")!=null;
425       String disjointId=null;
426       if( pn.getChild("disjoint") != null) {
427         disjointId = pn.getChild("disjoint").getTerminal();
428       }
429       TypeDescriptor td=parseTypeDescriptor(pn);
430       Vector args=parseDimExprs(pn);
431       int num=0;
432       if (pn.getChild("dims_opt").getLiteral()!=null)
433         num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
434       for(int i=0; i<(args.size()+num); i++)
435         td=td.makeArray(state);
436       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
437       for(int i=0; i<args.size(); i++) {
438         con.addArgument((ExpressionNode)args.get(i));
439       }
440       return con;
441     } else if (isNode(pn,"name")) {
442       NameDescriptor nd=parseName(pn);
443       return new NameNode(nd);
444     } else if (isNode(pn,"this")) {
445       NameDescriptor nd=new NameDescriptor("this");
446       return new NameNode(nd);
447     } else if (isNode(pn,"isavailable")) {
448       NameDescriptor nd=new NameDescriptor(pn.getTerminal());
449       return new OpNode(new NameNode(nd),null,new Operation(Operation.ISAVAILABLE));
450     } else if (isNode(pn,"methodinvoke1")) {
451       NameDescriptor nd=parseName(pn.getChild("name"));
452       Vector args=parseArgumentList(pn);
453       MethodInvokeNode min=new MethodInvokeNode(nd);
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,"methodinvoke2")) {
459       String methodid=pn.getChild("id").getTerminal();
460       ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
461       Vector args=parseArgumentList(pn);
462       MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
463       for(int i=0; i<args.size(); i++) {
464         min.addArgument((ExpressionNode)args.get(i));
465       }
466       return min;
467     } else if (isNode(pn,"fieldaccess")) {
468       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
469       String fieldname=pn.getChild("field").getTerminal();
470       return new FieldAccessNode(en,fieldname);
471     } else if (isNode(pn,"arrayaccess")) {
472       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
473       ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
474       return new ArrayAccessNode(en,index);
475     } else if (isNode(pn,"cast1")) {
476       return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
477     } else if (isNode(pn,"cast2")) {
478       return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
479     } else if (isNode(pn, "getoffset")) {
480       TypeDescriptor td=parseTypeDescriptor(pn);
481       String fieldname = pn.getChild("field").getTerminal();
482       //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ "  fieldname= " + fieldname);
483       return new OffsetNode(td, fieldname);
484     } else if (isNode(pn, "tert")) {
485       return new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
486                               parseExpression(pn.getChild("trueexpr").getFirstChild()),
487                               parseExpression(pn.getChild("falseexpr").getFirstChild()) );
488     } else if (isNode(pn, "instanceof")) {
489       ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
490       TypeDescriptor t=parseTypeDescriptor(pn);
491       return new InstanceOfNode(exp,t);
492     } else {
493       System.out.println("---------------------");
494       System.out.println(pn.PPrint(3,true));
495       throw new Error();
496     }
497   }
498
499   private Vector parseDimExprs(ParseNode pn) {
500     Vector arglist=new Vector();
501     ParseNode an=pn.getChild("dim_exprs");
502     if (an==null)       /* No argument list */
503       return arglist;
504     ParseNodeVector anv=an.getChildren();
505     for(int i=0; i<anv.size(); i++) {
506       arglist.add(parseExpression(anv.elementAt(i)));
507     }
508     return arglist;
509   }
510
511   private Vector parseArgumentList(ParseNode pn) {
512     Vector arglist=new Vector();
513     ParseNode an=pn.getChild("argument_list");
514     if (an==null)       /* No argument list */
515       return arglist;
516     ParseNodeVector anv=an.getChildren();
517     for(int i=0; i<anv.size(); i++) {
518       arglist.add(parseExpression(anv.elementAt(i)));
519     }
520     return arglist;
521   }
522
523   private Vector[] parseConsArgumentList(ParseNode pn) {
524     Vector arglist=new Vector();
525     Vector varlist=new Vector();
526     ParseNode an=pn.getChild("cons_argument_list");
527     if (an==null)       /* No argument list */
528       return new Vector[] {varlist, arglist};
529     ParseNodeVector anv=an.getChildren();
530     for(int i=0; i<anv.size(); i++) {
531       ParseNode cpn=anv.elementAt(i);
532       ParseNode var=cpn.getChild("var");
533       ParseNode exp=cpn.getChild("exp").getFirstChild();
534       varlist.add(var.getTerminal());
535       arglist.add(parseExpression(exp));
536     }
537     return new Vector[] {varlist, arglist};
538   }
539
540   private ExpressionNode parseAssignmentExpression(ParseNode pn) {
541     AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
542     ParseNodeVector pnv=pn.getChild("args").getChildren();
543
544     AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
545     return an;
546   }
547
548
549   private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
550     ParseNode headern=pn.getChild("method_header");
551     ParseNode bodyn=pn.getChild("body");
552     MethodDescriptor md=parseMethodHeader(headern);
553     BlockNode bn=parseBlock(bodyn);
554     cn.addMethod(md);
555     state.addTreeCode(md,bn);
556   }
557
558   private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
559     ParseNode mn=pn.getChild("modifiers");
560     Modifiers m=parseModifiersList(mn);
561     ParseNode cdecl=pn.getChild("constructor_declarator");
562     boolean isglobal=cdecl.getChild("global")!=null;
563     String name=cdecl.getChild("name").getChild("identifier").getTerminal();
564     MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
565     ParseNode paramnode=cdecl.getChild("parameters");
566     parseParameterList(md,paramnode);
567     ParseNode bodyn0=pn.getChild("body");
568     ParseNode bodyn=bodyn0.getChild("constructor_body");
569     cn.addMethod(md);
570     BlockNode bn=null;
571     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
572       bn=parseBlock(bodyn);
573     else
574       bn=new BlockNode();
575     if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
576       ParseNode sin=bodyn.getChild("superinvoke");
577       NameDescriptor nd=new NameDescriptor("super");
578       Vector args=parseArgumentList(sin);
579       MethodInvokeNode min=new MethodInvokeNode(nd);
580       for(int i=0; i<args.size(); i++) {
581         min.addArgument((ExpressionNode)args.get(i));
582       }
583       BlockExpressionNode ben=new BlockExpressionNode(min);
584       bn.addFirstBlockStatement(ben);
585     }
586     state.addTreeCode(md,bn);
587   }
588
589   public BlockNode parseBlock(ParseNode pn) {
590     this.m_taskexitnum = 0;
591     if (pn==null||isEmpty(pn.getTerminal()))
592       return new BlockNode();
593     ParseNode bsn=pn.getChild("block_statement_list");
594     return parseBlockHelper(bsn);
595   }
596
597   private BlockNode parseBlockHelper(ParseNode pn) {
598     ParseNodeVector pnv=pn.getChildren();
599     BlockNode bn=new BlockNode();
600     for(int i=0; i<pnv.size(); i++) {
601       Vector bsv=parseBlockStatement(pnv.elementAt(i));
602       for(int j=0; j<bsv.size(); j++) {
603         bn.addBlockStatement((BlockStatementNode)bsv.get(j));
604       }
605     }
606     return bn;
607   }
608
609   public BlockNode parseSingleBlock(ParseNode pn) {
610     BlockNode bn=new BlockNode();
611     Vector bsv=parseBlockStatement(pn);
612     for(int j=0; j<bsv.size(); j++) {
613       bn.addBlockStatement((BlockStatementNode)bsv.get(j));
614     }
615     bn.setStyle(BlockNode.NOBRACES);
616     return bn;
617   }
618
619   public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
620     ParseNodeVector pnv=pn.getChildren();
621     Vector bv=new Vector();
622     for(int i=0; i<pnv.size(); i++) {
623       bv.addAll(parseBlockStatement(pnv.elementAt(i)));
624     }
625     return bv;
626   }
627
628   public Vector parseBlockStatement(ParseNode pn) {
629     Vector blockstatements=new Vector();
630     if (isNode(pn,"tag_declaration")) {
631       String name=pn.getChild("single").getTerminal();
632       String type=pn.getChild("type").getTerminal();
633
634       blockstatements.add(new TagDeclarationNode(name, type));
635     } else if (isNode(pn,"local_variable_declaration")) {
636       TypeDescriptor t=parseTypeDescriptor(pn);
637       ParseNode vn=pn.getChild("variable_declarators_list");
638       ParseNodeVector pnv=vn.getChildren();
639       for(int i=0; i<pnv.size(); i++) {
640         ParseNode vardecl=pnv.elementAt(i);
641
642
643         ParseNode tmp=vardecl;
644         TypeDescriptor arrayt=t;
645         while (tmp.getChild("single")==null) {
646           arrayt=arrayt.makeArray(state);
647           tmp=tmp.getChild("array");
648         }
649         String identifier=tmp.getChild("single").getTerminal();
650
651         ParseNode epn=vardecl.getChild("initializer");
652
653
654         ExpressionNode en=null;
655         if (epn!=null)
656           en=parseExpression(epn.getFirstChild());
657
658         blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
659       }
660     } else if (isNode(pn,"nop")) {
661       /* Do Nothing */
662     } else if (isNode(pn,"expression")) {
663       blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
664     } else if (isNode(pn,"ifstatement")) {
665       blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
666                                               parseSingleBlock(pn.getChild("statement").getFirstChild()),
667                                               pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null));
668     } else if (isNode(pn,"taskexit")) {
669       Vector vfe=null;
670       if (pn.getChild("flag_effects_list")!=null)
671         vfe=parseFlags(pn.getChild("flag_effects_list"));
672       Vector ccs=null;
673       if (pn.getChild("cons_checks")!=null)
674         ccs=parseChecks(pn.getChild("cons_checks"));
675
676       blockstatements.add(new TaskExitNode(vfe, ccs, this.m_taskexitnum++));
677     } else if (isNode(pn,"atomic")) {
678       BlockNode bn=parseBlockHelper(pn);
679       blockstatements.add(new AtomicNode(bn));
680     } else if (isNode(pn,"return")) {
681       if (isEmpty(pn.getTerminal()))
682         blockstatements.add(new ReturnNode());
683       else {
684         ExpressionNode en=parseExpression(pn.getFirstChild());
685         blockstatements.add(new ReturnNode(en));
686       }
687     } else if (isNode(pn,"block_statement_list")) {
688       BlockNode bn=parseBlockHelper(pn);
689       blockstatements.add(new SubBlockNode(bn));
690     } else if (isNode(pn,"empty")) {
691       /* nop */
692     } else if (isNode(pn,"statement_expression_list")) {
693       ParseNodeVector pnv=pn.getChildren();
694       BlockNode bn=new BlockNode();
695       for(int i=0; i<pnv.size(); i++) {
696         ExpressionNode en=parseExpression(pnv.elementAt(i));
697         blockstatements.add(new BlockExpressionNode(en));
698       }
699       bn.setStyle(BlockNode.EXPRLIST);
700     } else if (isNode(pn,"forstatement")) {
701       BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
702       BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
703       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
704       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
705       blockstatements.add(new LoopNode(init,condition,update,body));
706     } else if (isNode(pn,"whilestatement")) {
707       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
708       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
709       blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
710     } else if (isNode(pn,"dowhilestatement")) {
711       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
712       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
713       blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
714     } else if (isNode(pn,"sese")) {
715       SESENode start=new SESENode();
716       SESENode end  =new SESENode();
717       start.setEnd( end   );
718       end.setStart( start );
719       blockstatements.add(start);
720       blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
721       blockstatements.add(end);
722     } else if (isNode(pn,"continue")) {
723         blockstatements.add(new ContinueBreakNode(false));
724     } else if (isNode(pn,"break")) {
725         blockstatements.add(new ContinueBreakNode(true));
726
727     } else {
728       System.out.println("---------------");
729       System.out.println(pn.PPrint(3,true));
730       throw new Error();
731     }
732     return blockstatements;
733   }
734
735   public MethodDescriptor parseMethodHeader(ParseNode pn) {
736     ParseNode mn=pn.getChild("modifiers");
737     Modifiers m=parseModifiersList(mn);
738
739     ParseNode tn=pn.getChild("returntype");
740     TypeDescriptor returntype;
741     if (tn!=null)
742       returntype=parseTypeDescriptor(tn);
743     else
744       returntype=new TypeDescriptor(TypeDescriptor.VOID);
745
746     ParseNode pmd=pn.getChild("method_declarator");
747     String name=pmd.getChild("name").getTerminal();
748     MethodDescriptor md=new MethodDescriptor(m, returntype, name);
749
750     ParseNode paramnode=pmd.getChild("parameters");
751     parseParameterList(md,paramnode);
752     return md;
753   }
754
755   public void parseParameterList(MethodDescriptor md, ParseNode pn) {
756     ParseNode paramlist=pn.getChild("formal_parameter_list");
757     if (paramlist==null)
758       return;
759     ParseNodeVector pnv=paramlist.getChildren();
760     for(int i=0; i<pnv.size(); i++) {
761       ParseNode paramn=pnv.elementAt(i);
762
763       if (isNode(paramn, "tag_parameter")) {
764         String paramname=paramn.getChild("single").getTerminal();
765         TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
766         md.addTagParameter(type, paramname);
767       } else {
768         TypeDescriptor type=parseTypeDescriptor(paramn);
769
770         ParseNode tmp=paramn;
771         while (tmp.getChild("single")==null) {
772           type=type.makeArray(state);
773           tmp=tmp.getChild("array");
774         }
775         String paramname=tmp.getChild("single").getTerminal();
776
777         md.addParameter(type, paramname);
778       }
779     }
780   }
781
782   public Modifiers parseModifiersList(ParseNode pn) {
783     Modifiers m=new Modifiers();
784     ParseNode modlist=pn.getChild("modifier_list");
785     if (modlist!=null) {
786       ParseNodeVector pnv=modlist.getChildren();
787       for(int i=0; i<pnv.size(); i++) {
788         ParseNode modn=pnv.elementAt(i);
789         if (isNode(modn,"public"))
790           m.addModifier(Modifiers.PUBLIC);
791         else if (isNode(modn,"protected"))
792           m.addModifier(Modifiers.PROTECTED);
793         else if (isNode(modn,"private"))
794           m.addModifier(Modifiers.PRIVATE);
795         else if (isNode(modn,"static"))
796           m.addModifier(Modifiers.STATIC);
797         else if (isNode(modn,"final"))
798           m.addModifier(Modifiers.FINAL);
799         else if (isNode(modn,"native"))
800           m.addModifier(Modifiers.NATIVE);
801         else if (isNode(modn,"synchronized"))
802           m.addModifier(Modifiers.SYNCHRONIZED);
803         else if (isNode(modn,"atomic"))
804           m.addModifier(Modifiers.ATOMIC);
805         else throw new Error("Unrecognized Modifier");
806       }
807     }
808     return m;
809   }
810
811   private boolean isNode(ParseNode pn, String label) {
812     if (pn.getLabel().equals(label))
813       return true;
814     else return false;
815   }
816
817   private static boolean isEmpty(ParseNode pn) {
818     if (pn.getLabel().equals("empty"))
819       return true;
820     else
821       return false;
822   }
823
824   private static boolean isEmpty(String s) {
825     if (s.equals("empty"))
826       return true;
827     else
828       return false;
829   }
830
831   /** Throw an exception if something is unexpected */
832   private void check(ParseNode pn, String label) {
833     if (pn == null) {
834       throw new Error(pn+ "IE: Expected '" + label + "', got null");
835     }
836     if (!pn.getLabel().equals(label)) {
837       throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
838     }
839   }
840 }