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