work towards turning MGC enhancements on all the time
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3
4 import java.util.*;
5
6
7 public class BuildIR {
8   State state;
9
10   private int m_taskexitnum;
11
12   public BuildIR(State state) {
13     this.state=state;
14     this.m_taskexitnum = 0;
15   }
16
17   public void buildtree(ParseNode pn, Set toanalyze) {
18     parseFile(pn, toanalyze);
19   }
20
21   Vector singleimports;
22   Vector multiimports;
23   NameDescriptor packages;
24
25   /** Parse the classes in this file */
26   public void parseFile(ParseNode pn, Set toanalyze) {
27     singleimports=new Vector();
28     multiimports=new Vector();
29     
30     ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
31     if (ipn!=null) {
32       ParseNodeVector pnv=ipn.getChildren();
33       for(int i=0; i<pnv.size(); i++) {
34         ParseNode pnimport=pnv.elementAt(i);
35         NameDescriptor nd=parseName(pnimport.getChild("name"));
36         if (isNode(pnimport,"import_single"))
37           singleimports.add(nd);
38         else
39           multiimports.add(nd);
40       }
41     }
42     ParseNode ppn=pn.getChild("packages").getChild("package");
43     if (ppn!=null) {
44       packages=parseName(ppn.getChild("name"));
45     }
46     ParseNode tpn=pn.getChild("type_declaration_list");
47     if (tpn!=null) {
48       ParseNodeVector pnv=tpn.getChildren();
49       for(int i=0; i<pnv.size(); i++) {
50         ParseNode type_pn=pnv.elementAt(i);
51         if (isEmpty(type_pn))         /* Skip the semicolon */
52           continue;
53         if (isNode(type_pn,"class_declaration")) {
54           ClassDescriptor cn=parseTypeDecl(type_pn);
55           parseInitializers(cn);
56           if (toanalyze!=null)
57             toanalyze.add(cn);
58           state.addClass(cn);
59           // for inner classes/enum
60           HashSet tovisit = new HashSet();
61           Iterator it_icds = cn.getInnerClasses();
62           while(it_icds.hasNext()) {
63             tovisit.add(it_icds.next());
64           }
65           
66           while(!tovisit.isEmpty()) {
67             ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
68             tovisit.remove(cd);
69             parseInitializers(cd);
70             if(toanalyze != null) {
71               toanalyze.add(cd);
72             }
73             state.addClass(cd);
74             
75             Iterator it_ics = cd.getInnerClasses();
76             while(it_ics.hasNext()) {
77               tovisit.add(it_ics.next());
78             }
79             
80             Iterator it_ienums = cd.getEnum();
81             while(it_ienums.hasNext()) {
82               ClassDescriptor iecd = (ClassDescriptor)it_ienums.next();
83               if(toanalyze != null) {
84                 toanalyze.add(iecd);
85               }
86               state.addClass(iecd);
87             }
88           }
89           
90           Iterator it_enums = cn.getEnum();
91           while(it_enums.hasNext()) {
92             ClassDescriptor ecd = (ClassDescriptor)it_enums.next();
93             if(toanalyze != null) {
94               toanalyze.add(ecd);
95             }
96             state.addClass(ecd);
97           }
98         } else if (isNode(type_pn,"task_declaration")) {
99           TaskDescriptor td=parseTaskDecl(type_pn);
100           if (toanalyze!=null)
101             toanalyze.add(td);
102           state.addTask(td);
103         } else if (isNode(type_pn,"interface_declaration")) {
104           // TODO add version for normal Java later
105           ClassDescriptor cn = parseInterfaceDecl(type_pn);
106           if (toanalyze!=null)
107             toanalyze.add(cn);
108           state.addClass(cn);
109           
110           // for enum
111           Iterator it_enums = cn.getEnum();
112           while(it_enums.hasNext()) {
113             ClassDescriptor ecd = (ClassDescriptor)it_enums.next();
114             if(toanalyze != null) {
115               toanalyze.add(ecd);
116             }
117             state.addClass(ecd);
118           }
119         } else if (isNode(type_pn,"enum_declaration")) {
120           // TODO add version for normal Java later
121           ClassDescriptor cn = parseEnumDecl(null, type_pn);
122           if (toanalyze!=null)
123             toanalyze.add(cn);
124           state.addClass(cn);
125         } else {
126           throw new Error(type_pn.getLabel());
127         }
128       }
129     }
130   }
131
132  public void parseInitializers(ClassDescriptor cn){
133         Vector fv=cn.getFieldVec();
134         for(int i=0;i<fv.size();i++) {
135             FieldDescriptor fd=(FieldDescriptor)fv.get(i);
136             if(fd.getExpressionNode()!=null) {
137                 Iterator methodit = cn.getMethods();
138                 while(methodit.hasNext()){
139                     MethodDescriptor currmd=(MethodDescriptor)methodit.next();
140                     if(currmd.isConstructor()){
141                         BlockNode bn=state.getMethodBody(currmd);
142                         NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
143                         AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
144                         bn.addFirstBlockStatement(new BlockExpressionNode(an));                 
145                     }
146                 }
147             }
148         }
149     }  
150
151   private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {
152     ClassDescriptor ecd=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
153     ecd.setAsEnum();
154     if(cn != null) {
155       ecd.setSurroundingClass(cn.getSymbol());
156       ecd.setSurrounding(cn);
157       cn.addEnum(ecd);
158     }
159     if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
160         ecd.getSymbol().equals(TypeUtil.TagClass))) {
161       ecd.setSuper(TypeUtil.ObjectClass);
162     }
163     ecd.setModifiers(parseModifiersList(pn.getChild("modifiers")));
164     parseEnumBody(ecd, pn.getChild("enumbody"));
165     return ecd;
166   }
167   
168   private void parseEnumBody(ClassDescriptor cn, ParseNode pn) {
169     ParseNode decls=pn.getChild("enum_constants_list");
170     if (decls!=null) {
171       ParseNodeVector pnv=decls.getChildren();
172       for(int i=0; i<pnv.size(); i++) {
173         ParseNode decl=pnv.elementAt(i);
174         if (isNode(decl,"enum_constant")) {
175           parseEnumConstant(cn,decl);
176         } else throw new Error();
177       }
178     }
179   }
180   
181   private void parseEnumConstant(ClassDescriptor cn, ParseNode pn) {
182     cn.addEnumConstant(pn.getChild("name").getTerminal());
183   }
184   
185   public ClassDescriptor parseInterfaceDecl(ParseNode pn) {
186     ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
187     //cn.setAsInterface();
188     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
189       /* parse inherited interface name */
190       ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
191       ParseNodeVector pnv=snlist.getChildren();
192       for(int i=0; i<pnv.size(); i++) {
193         ParseNode decl=pnv.elementAt(i);
194         if (isNode(decl,"type")) {
195           NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
196           cn.addSuperInterface(nd.toString());
197         }
198       }
199     }
200     cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
201     parseInterfaceBody(cn, pn.getChild("interfacebody"));
202     return cn;
203   }
204   
205   private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
206     assert(cn.isInterface());
207     ParseNode decls=pn.getChild("interface_member_declaration_list");
208     if (decls!=null) {
209       ParseNodeVector pnv=decls.getChildren();
210       for(int i=0; i<pnv.size(); i++) {
211         ParseNode decl=pnv.elementAt(i);
212         if (isNode(decl,"constant")) {
213           parseInterfaceConstant(cn,decl);
214         } else if (isNode(decl,"method")) {
215           parseInterfaceMethod(cn,decl.getChild("method_declaration"));
216         } else throw new Error();
217       }
218     }
219   }
220   
221   private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
222     if (pn!=null) {
223       parseFieldDecl(cn,pn.getChild("field_declaration"));
224       return;
225     }
226     throw new Error();
227   }
228   
229   private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
230     ParseNode headern=pn.getChild("header");
231     ParseNode bodyn=pn.getChild("body");
232     MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
233     md.getModifiers().addModifier(Modifiers.PUBLIC);
234     md.getModifiers().addModifier(Modifiers.ABSTRACT);
235     try {
236       BlockNode bn=parseBlock(bodyn);
237       cn.addMethod(md);
238       state.addTreeCode(md,bn);
239
240       // this is a hack for investigating new language features
241       // at the AST level, someday should evolve into a nice compiler
242       // option *wink*
243       //if( cn.getSymbol().equals( ***put a class in here like:     "Test" ) &&
244       //    md.getSymbol().equals( ***put your method in here like: "main" ) 
245       //) {
246       //  bn.setStyle( BlockNode.NORMAL );
247       //  System.out.println( bn.printNode( 0 ) );
248       //}
249
250     } catch (Exception e) {
251       System.out.println("Error with method:"+md.getSymbol());
252       e.printStackTrace();
253       throw new Error();
254     } catch (Error e) {
255       System.out.println("Error with method:"+md.getSymbol());
256       e.printStackTrace();
257       throw new Error();
258     }
259   }
260
261   public TaskDescriptor parseTaskDecl(ParseNode pn) {
262     TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
263     ParseNode bodyn=pn.getChild("body");
264     BlockNode bn=parseBlock(bodyn);
265     parseParameterList(td, pn);
266     state.addTreeCode(td,bn);
267     if (pn.getChild("flag_effects_list")!=null)
268       td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
269     return td;
270   }
271
272   public Vector parseFlags(ParseNode pn) {
273     Vector vfe=new Vector();
274     ParseNodeVector pnv=pn.getChildren();
275     for(int i=0; i<pnv.size(); i++) {
276       ParseNode fn=pnv.elementAt(i);
277       FlagEffects fe=parseFlagEffects(fn);
278       vfe.add(fe);
279     }
280     return vfe;
281   }
282
283   public FlagEffects parseFlagEffects(ParseNode pn) {
284     if (isNode(pn,"flag_effect")) {
285       String flagname=pn.getChild("name").getTerminal();
286       FlagEffects fe=new FlagEffects(flagname);
287       if (pn.getChild("flag_list")!=null)
288         parseFlagEffect(fe, pn.getChild("flag_list"));
289       if (pn.getChild("tag_list")!=null)
290         parseTagEffect(fe, pn.getChild("tag_list"));
291       return fe;
292     } else throw new Error();
293   }
294
295   public void parseTagEffect(FlagEffects fes, ParseNode pn) {
296     ParseNodeVector pnv=pn.getChildren();
297     for(int i=0; i<pnv.size(); i++) {
298       ParseNode pn2=pnv.elementAt(i);
299       boolean status=true;
300       if (isNode(pn2,"not")) {
301         status=false;
302         pn2=pn2.getChild("name");
303       }
304       String name=pn2.getTerminal();
305       fes.addTagEffect(new TagEffect(name,status));
306     }
307   }
308
309   public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
310     ParseNodeVector pnv=pn.getChildren();
311     for(int i=0; i<pnv.size(); i++) {
312       ParseNode pn2=pnv.elementAt(i);
313       boolean status=true;
314       if (isNode(pn2,"not")) {
315         status=false;
316         pn2=pn2.getChild("name");
317       }
318       String name=pn2.getTerminal();
319       fes.addEffect(new FlagEffect(name,status));
320     }
321   }
322
323   public FlagExpressionNode parseFlagExpression(ParseNode pn) {
324     if (isNode(pn,"or")) {
325       ParseNodeVector pnv=pn.getChildren();
326       ParseNode left=pnv.elementAt(0);
327       ParseNode right=pnv.elementAt(1);
328       return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
329     } else if (isNode(pn,"and")) {
330       ParseNodeVector pnv=pn.getChildren();
331       ParseNode left=pnv.elementAt(0);
332       ParseNode right=pnv.elementAt(1);
333       return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
334     } else if (isNode(pn, "not")) {
335       ParseNodeVector pnv=pn.getChildren();
336       ParseNode left=pnv.elementAt(0);
337       return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
338
339     } else if (isNode(pn,"name")) {
340       return new FlagNode(pn.getTerminal());
341     } else {
342       throw new Error();
343     }
344   }
345
346   public Vector parseChecks(ParseNode pn) {
347     Vector ccs=new Vector();
348     ParseNodeVector pnv=pn.getChildren();
349     for(int i=0; i<pnv.size(); i++) {
350       ParseNode fn=pnv.elementAt(i);
351       ConstraintCheck cc=parseConstraintCheck(fn);
352       ccs.add(cc);
353     }
354     return ccs;
355   }
356
357   public ConstraintCheck parseConstraintCheck(ParseNode pn) {
358     if (isNode(pn,"cons_check")) {
359       String specname=pn.getChild("name").getChild("identifier").getTerminal();
360       Vector[] args=parseConsArgumentList(pn);
361       ConstraintCheck cc=new ConstraintCheck(specname);
362       for(int i=0; i<args[0].size(); i++) {
363         cc.addVariable((String)args[0].get(i));
364         cc.addArgument((ExpressionNode)args[1].get(i));
365       }
366       return cc;
367     } else throw new Error();
368   }
369
370   public void parseParameterList(TaskDescriptor td, ParseNode pn) {
371
372     boolean optional;
373     ParseNode paramlist=pn.getChild("task_parameter_list");
374     if (paramlist==null)
375       return;
376     ParseNodeVector pnv=paramlist.getChildren();
377     for(int i=0; i<pnv.size(); i++) {
378       ParseNode paramn=pnv.elementAt(i);
379       if(paramn.getChild("optional")!=null) {
380         optional = true;
381         paramn = paramn.getChild("optional").getFirstChild();
382         System.out.println("OPTIONAL FOUND!!!!!!!");
383       } else { optional = false;
384                System.out.println("NOT OPTIONAL");}
385
386       TypeDescriptor type=parseTypeDescriptor(paramn);
387
388       String paramname=paramn.getChild("single").getTerminal();
389       FlagExpressionNode fen=null;
390       if (paramn.getChild("flag")!=null)
391         fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
392
393       ParseNode tagnode=paramn.getChild("tag");
394
395       TagExpressionList tel=null;
396       if (tagnode!=null) {
397         tel=parseTagExpressionList(tagnode);
398       }
399
400       td.addParameter(type,paramname,fen, tel, optional);
401     }
402   }
403
404   public TagExpressionList parseTagExpressionList(ParseNode pn) {
405     //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
406     //To test, feed in any input program that uses tags
407     ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
408     TagExpressionList tel=new TagExpressionList();
409     for(int i=0; i<pnv.size(); i++) {
410       ParseNode tn=pnv.elementAt(i);
411       String type=tn.getChild("type").getTerminal();
412       String name=tn.getChild("single").getTerminal();
413       tel.addTag(type, name);
414     }
415     return tel;
416   }
417
418   public ClassDescriptor parseTypeDecl(ParseNode pn) {
419     ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
420     if (!isEmpty(pn.getChild("super").getTerminal())) {
421       /* parse superclass name */
422       ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
423       NameDescriptor nd=parseName(snn);
424       cn.setSuper(nd.toString());
425     } else {
426       if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
427             cn.getSymbol().equals(TypeUtil.TagClass)))
428         cn.setSuper(TypeUtil.ObjectClass);
429     }
430     // check inherited interfaces
431     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
432       /* parse inherited interface name */
433       ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
434       ParseNodeVector pnv=snlist.getChildren();
435       for(int i=0; i<pnv.size(); i++) {
436         ParseNode decl=pnv.elementAt(i);
437         if (isNode(decl,"type")) {
438           NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
439           cn.addSuperInterface(nd.toString());
440         }
441       }
442     }
443     cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
444     parseClassBody(cn, pn.getChild("classbody"));
445     
446     boolean hasConstructor = false;
447     for(Iterator method_it=cn.getMethods(); method_it.hasNext();) {
448       MethodDescriptor md=(MethodDescriptor)method_it.next();
449       hasConstructor |= md.isConstructor();
450     }
451     if((!hasConstructor) && (!cn.isEnum())) {
452       // add a default constructor for this class
453       MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
454           cn.getSymbol(), false);
455       BlockNode bn=new BlockNode();
456       state.addTreeCode(md,bn);
457       md.setDefaultConstructor();
458       cn.addMethod(md);
459     }
460     return cn;
461   }
462
463   private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
464     ParseNode decls=pn.getChild("class_body_declaration_list");
465     if (decls!=null) {
466       ParseNodeVector pnv=decls.getChildren();
467       for(int i=0; i<pnv.size(); i++) {
468         ParseNode decl=pnv.elementAt(i);
469         if (isNode(decl,"member")) {
470           parseClassMember(cn,decl);
471         } else if (isNode(decl,"constructor")) {
472           parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
473         } else if (isNode(decl, "static_block")) {
474           parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
475         } else if (isNode(decl,"block")) {
476         } else throw new Error();
477       }
478     }
479   }
480   
481   private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
482     ParseNode fieldnode=pn.getChild("field");
483     if (fieldnode!=null) {
484       parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
485       return;
486     }
487     ParseNode methodnode=pn.getChild("method");
488     if (methodnode!=null) {
489       parseMethodDecl(cn,methodnode.getChild("method_declaration"));
490       return;
491     }
492     ParseNode innerclassnode=pn.getChild("inner_class_declaration");
493     if (innerclassnode!=null) {
494       parseInnerClassDecl(cn,innerclassnode);
495       return;
496     }
497     ParseNode enumnode=pn.getChild("enum_declaration");
498     if (enumnode!=null) {
499       parseEnumDecl(cn,enumnode);
500     }
501     ParseNode flagnode=pn.getChild("flag");
502     if (flagnode!=null) {
503       parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
504       return;
505     }
506     // in case there are empty node
507     ParseNode emptynode=pn.getChild("empty");
508     if(emptynode != null) {
509       return;
510     }
511     throw new Error();
512   }
513   
514   private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
515     ClassDescriptor icn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
516     icn.setAsInnerClass();
517     icn.setSurroundingClass(cn.getSymbol());
518     icn.setSurrounding(cn);
519     cn.addInnerClass(icn);
520     if (!isEmpty(pn.getChild("super").getTerminal())) {
521       /* parse superclass name */
522       ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
523       NameDescriptor nd=parseName(snn);
524       icn.setSuper(nd.toString());
525     } else {
526       if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
527           icn.getSymbol().equals(TypeUtil.TagClass)))
528         icn.setSuper(TypeUtil.ObjectClass);
529     }
530     // check inherited interfaces
531     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
532       /* parse inherited interface name */
533       ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
534       ParseNodeVector pnv=snlist.getChildren();
535       for(int i=0; i<pnv.size(); i++) {
536         ParseNode decl=pnv.elementAt(i);
537         if (isNode(decl,"type")) {
538           NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
539           icn.addSuperInterface(nd.toString());
540         }
541       }
542     }
543     icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
544     if(!icn.isStatic()) {
545       throw new Error("Error: inner class " + icn.getSymbol() + " in Class " + 
546           cn.getSymbol() + " is not a nested class and is not supported yet!");
547     }
548     parseClassBody(icn, pn.getChild("classbody"));
549     return icn;
550   }
551
552   private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
553     ParseNode tn=pn.getChild("type");
554     String type_st=tn.getTerminal();
555     if(type_st.equals("byte")) {
556       return state.getTypeDescriptor(TypeDescriptor.BYTE);
557     } else if(type_st.equals("short")) {
558       return state.getTypeDescriptor(TypeDescriptor.SHORT);
559     } else if(type_st.equals("boolean")) {
560       return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
561     } else if(type_st.equals("int")) {
562       return state.getTypeDescriptor(TypeDescriptor.INT);
563     } else if(type_st.equals("long")) {
564       return state.getTypeDescriptor(TypeDescriptor.LONG);
565     } else if(type_st.equals("char")) {
566       return state.getTypeDescriptor(TypeDescriptor.CHAR);
567     } else if(type_st.equals("float")) {
568       return state.getTypeDescriptor(TypeDescriptor.FLOAT);
569     } else if(type_st.equals("double")) {
570       return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
571     } else if(type_st.equals("class")) {
572       ParseNode nn=tn.getChild("class");
573       return state.getTypeDescriptor(parseName(nn.getChild("name")));
574     } else if(type_st.equals("array")) {
575       ParseNode nn=tn.getChild("array");
576       TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
577       Integer numdims=(Integer)nn.getChild("dims").getLiteral();
578       for(int i=0; i<numdims.intValue(); i++)
579         td=td.makeArray(state);
580       return td;
581     } else {
582       System.out.println(pn.PPrint(2, true));
583       throw new Error();
584     }
585   }
586
587   private NameDescriptor parseName(ParseNode nn) {
588     ParseNode base=nn.getChild("base");
589     ParseNode id=nn.getChild("identifier");
590     if (base==null)
591       return new NameDescriptor(id.getTerminal());
592     return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
593
594   }
595
596   private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
597     String name=pn.getChild("name").getTerminal();
598     FlagDescriptor flag=new FlagDescriptor(name);
599     if (pn.getChild("external")!=null)
600       flag.makeExternal();
601     cn.addFlag(flag);
602   }
603
604   private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
605     ParseNode mn=pn.getChild("modifier");
606     Modifiers m=parseModifiersList(mn);
607     if(cn.isInterface()) {
608       // TODO add version for normal Java later
609       // Can only be PUBLIC or STATIC or FINAL
610       if((m.isAbstract()) || (m.isAtomic()) || (m.isNative()) 
611           || (m.isSynchronized())) {
612         throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
613       }
614       m.addModifier(Modifiers.PUBLIC);
615       m.addModifier(Modifiers.STATIC);
616       m.addModifier(Modifiers.FINAL);
617     }
618
619     ParseNode tn=pn.getChild("type");
620     TypeDescriptor t=parseTypeDescriptor(tn);
621     ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
622     ParseNodeVector pnv=vn.getChildren();
623     boolean isglobal=pn.getChild("global")!=null;
624
625     for(int i=0; i<pnv.size(); i++) {
626       ParseNode vardecl=pnv.elementAt(i);
627       ParseNode tmp=vardecl;
628       TypeDescriptor arrayt=t;
629       while (tmp.getChild("single")==null) {
630         arrayt=arrayt.makeArray(state);
631         tmp=tmp.getChild("array");
632       }
633       String identifier=tmp.getChild("single").getTerminal();
634       ParseNode epn=vardecl.getChild("initializer");
635
636       ExpressionNode en=null;
637       if (epn!=null) {
638         en=parseExpression(epn.getFirstChild());
639         if(m.isStatic()) {
640           // for static field, the initializer should be considered as a 
641           // static block
642           boolean isfirst = false;
643           MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
644           if(md == null) {
645             // the first static block for this class
646             Modifiers m_i=new Modifiers();
647             m_i.addModifier(Modifiers.STATIC);
648             md = new MethodDescriptor(m_i, "staticblocks", false);
649             md.setAsStaticBlock();
650             isfirst = true;
651           }
652           if(isfirst) {
653             cn.addMethod(md);
654           }
655           cn.incStaticBlocks();
656           BlockNode bn=new BlockNode();
657           NameNode nn=new NameNode(new NameDescriptor(identifier));
658           AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
659           bn.addBlockStatement(new BlockExpressionNode(an));
660           if(isfirst) {
661             state.addTreeCode(md,bn);
662           } else {
663             BlockNode obn = state.getMethodBody(md);
664             for(int ii = 0; ii < bn.size(); ii++) {
665               BlockStatementNode bsn = bn.get(ii);
666               obn.addBlockStatement(bsn);
667             }
668             state.addTreeCode(md, obn);
669             bn = null;
670           }
671           en = null;
672         }
673       }
674
675       cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
676     }
677   }
678
679   private ExpressionNode parseExpression(ParseNode pn) {
680     if (isNode(pn,"assignment"))
681       return parseAssignmentExpression(pn);
682     else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
683              isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
684              isNode(pn,"bitwise_and")||isNode(pn,"equal")||
685              isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
686              isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
687              isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
688              isNode(pn,"rightshift")||isNode(pn,"sub")||
689              isNode(pn,"urightshift")||isNode(pn,"sub")||
690              isNode(pn,"add")||isNode(pn,"mult")||
691              isNode(pn,"div")||isNode(pn,"mod")) {
692       ParseNodeVector pnv=pn.getChildren();
693       ParseNode left=pnv.elementAt(0);
694       ParseNode right=pnv.elementAt(1);
695       Operation op=new Operation(pn.getLabel());
696       return new OpNode(parseExpression(left),parseExpression(right),op);
697     } else if (isNode(pn,"unaryplus")||
698                isNode(pn,"unaryminus")||
699                isNode(pn,"not")||
700                isNode(pn,"comp")) {
701       ParseNode left=pn.getFirstChild();
702       Operation op=new Operation(pn.getLabel());
703       return new OpNode(parseExpression(left),op);
704     } else if (isNode(pn,"postinc")||
705                isNode(pn,"postdec")) {
706       ParseNode left=pn.getFirstChild();
707       AssignOperation op=new AssignOperation(pn.getLabel());
708       return new AssignmentNode(parseExpression(left),null,op);
709
710     } else if (isNode(pn,"preinc")||
711                isNode(pn,"predec")) {
712       ParseNode left=pn.getFirstChild();
713       AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
714       return new AssignmentNode(parseExpression(left),
715                                 new LiteralNode("integer",new Integer(1)),op);
716     } else if (isNode(pn,"literal")) {
717       String literaltype=pn.getTerminal();
718       ParseNode literalnode=pn.getChild(literaltype);
719       Object literal_obj=literalnode.getLiteral();
720       return new LiteralNode(literaltype, literal_obj);
721     } else if (isNode(pn,"createobject")) {
722       TypeDescriptor td=parseTypeDescriptor(pn);
723       
724       Vector args=parseArgumentList(pn);
725       boolean isglobal=pn.getChild("global")!=null||
726         pn.getChild("scratch")!=null;
727       String disjointId=null;
728       if( pn.getChild("disjoint") != null) {
729         disjointId = pn.getChild("disjoint").getTerminal();
730       }
731       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
732       for(int i=0; i<args.size(); i++) {
733         con.addArgument((ExpressionNode)args.get(i));
734       }
735       /* Could have flag set or tag added here */
736       if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
737         FlagEffects fe=new FlagEffects(null);
738         if (pn.getChild("flag_list")!=null)
739           parseFlagEffect(fe, pn.getChild("flag_list"));
740
741         if (pn.getChild("tag_list")!=null)
742           parseTagEffect(fe, pn.getChild("tag_list"));
743         con.addFlagEffects(fe);
744       }
745
746       return con;
747     } else if (isNode(pn,"createarray")) {
748       //System.out.println(pn.PPrint(3,true));
749       boolean isglobal=pn.getChild("global")!=null||
750         pn.getChild("scratch")!=null;
751       String disjointId=null;
752       if( pn.getChild("disjoint") != null) {
753         disjointId = pn.getChild("disjoint").getTerminal();
754       }
755       TypeDescriptor td=parseTypeDescriptor(pn);
756       Vector args=parseDimExprs(pn);
757       int num=0;
758       if (pn.getChild("dims_opt").getLiteral()!=null)
759         num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
760       for(int i=0; i<(args.size()+num); i++)
761         td=td.makeArray(state);
762       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
763       for(int i=0; i<args.size(); i++) {
764         con.addArgument((ExpressionNode)args.get(i));
765       }
766       return con;
767     } if (isNode(pn,"createarray2")) {
768       TypeDescriptor td=parseTypeDescriptor(pn);
769       int num=0;
770       if (pn.getChild("dims_opt").getLiteral()!=null)
771     num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
772       for(int i=0; i<num; i++)
773     td=td.makeArray(state);
774       CreateObjectNode con=new CreateObjectNode(td, false, null);
775       // TODO array initializers
776       ParseNode ipn = pn.getChild("initializer");     
777       Vector initializers=parseVariableInitializerList(ipn);
778       ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
779       con.addArrayInitializer(ain);
780       return con;
781     } else if (isNode(pn,"name")) {
782       NameDescriptor nd=parseName(pn);
783       return new NameNode(nd);
784     } else if (isNode(pn,"this")) {
785       NameDescriptor nd=new NameDescriptor("this");
786       return new NameNode(nd);
787     } else if (isNode(pn,"isavailable")) {
788       NameDescriptor nd=new NameDescriptor(pn.getTerminal());
789       return new OpNode(new NameNode(nd),null,new Operation(Operation.ISAVAILABLE));
790     } else if (isNode(pn,"methodinvoke1")) {
791       NameDescriptor nd=parseName(pn.getChild("name"));
792       Vector args=parseArgumentList(pn);
793       MethodInvokeNode min=new MethodInvokeNode(nd);
794       for(int i=0; i<args.size(); i++) {
795         min.addArgument((ExpressionNode)args.get(i));
796       }
797       return min;
798     } else if (isNode(pn,"methodinvoke2")) {
799       String methodid=pn.getChild("id").getTerminal();
800       ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
801       Vector args=parseArgumentList(pn);
802       MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
803       for(int i=0; i<args.size(); i++) {
804         min.addArgument((ExpressionNode)args.get(i));
805       }
806       return min;
807     } else if (isNode(pn,"fieldaccess")) {
808       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
809       String fieldname=pn.getChild("field").getTerminal();
810       return new FieldAccessNode(en,fieldname);
811     } else if (isNode(pn,"arrayaccess")) {
812       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
813       ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
814       return new ArrayAccessNode(en,index);
815     } else if (isNode(pn,"cast1")) {
816       try {
817         return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
818       } catch (Exception e) {
819         System.out.println(pn.PPrint(1,true));
820         e.printStackTrace();
821         throw new Error();
822       }
823     } else if (isNode(pn,"cast2")) {
824       return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
825     } else if (isNode(pn, "getoffset")) {
826       TypeDescriptor td=parseTypeDescriptor(pn);
827       String fieldname = pn.getChild("field").getTerminal();
828       //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ "  fieldname= " + fieldname);
829       return new OffsetNode(td, fieldname);
830     } else if (isNode(pn, "tert")) {
831       return new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
832                               parseExpression(pn.getChild("trueexpr").getFirstChild()),
833                               parseExpression(pn.getChild("falseexpr").getFirstChild()) );
834     } else if (isNode(pn, "instanceof")) {
835       ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
836       TypeDescriptor t=parseTypeDescriptor(pn);
837       return new InstanceOfNode(exp,t);
838     } else if (isNode(pn, "array_initializer")) {  
839       Vector initializers=parseVariableInitializerList(pn);
840       return new ArrayInitializerNode(initializers);
841     } else if (isNode(pn, "class_type")) {
842       TypeDescriptor td=parseTypeDescriptor(pn);
843       return new ClassTypeNode(td);
844     } else if (isNode(pn, "empty")) {
845       return null;
846     } else {
847       System.out.println("---------------------");
848       System.out.println(pn.PPrint(3,true));
849       throw new Error();
850     }
851   }
852
853   private Vector parseDimExprs(ParseNode pn) {
854     Vector arglist=new Vector();
855     ParseNode an=pn.getChild("dim_exprs");
856     if (an==null)       /* No argument list */
857       return arglist;
858     ParseNodeVector anv=an.getChildren();
859     for(int i=0; i<anv.size(); i++) {
860       arglist.add(parseExpression(anv.elementAt(i)));
861     }
862     return arglist;
863   }
864
865   private Vector parseArgumentList(ParseNode pn) {
866     Vector arglist=new Vector();
867     ParseNode an=pn.getChild("argument_list");
868     if (an==null)       /* No argument list */
869       return arglist;
870     ParseNodeVector anv=an.getChildren();
871     for(int i=0; i<anv.size(); i++) {
872       arglist.add(parseExpression(anv.elementAt(i)));
873     }
874     return arglist;
875   }
876
877   private Vector[] parseConsArgumentList(ParseNode pn) {
878     Vector arglist=new Vector();
879     Vector varlist=new Vector();
880     ParseNode an=pn.getChild("cons_argument_list");
881     if (an==null)       /* No argument list */
882       return new Vector[] {varlist, arglist};
883     ParseNodeVector anv=an.getChildren();
884     for(int i=0; i<anv.size(); i++) {
885       ParseNode cpn=anv.elementAt(i);
886       ParseNode var=cpn.getChild("var");
887       ParseNode exp=cpn.getChild("exp").getFirstChild();
888       varlist.add(var.getTerminal());
889       arglist.add(parseExpression(exp));
890     }
891     return new Vector[] {varlist, arglist};
892   }
893
894   private Vector parseVariableInitializerList(ParseNode pn) {
895     Vector varInitList=new Vector();
896     ParseNode vin=pn.getChild("var_init_list");
897     if (vin==null)       /* No argument list */
898       return varInitList;
899     ParseNodeVector vinv=vin.getChildren();
900     for(int i=0; i<vinv.size(); i++) {
901       varInitList.add(parseExpression(vinv.elementAt(i)));
902     }
903     return varInitList;
904   }
905
906   private ExpressionNode parseAssignmentExpression(ParseNode pn) {
907     AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
908     ParseNodeVector pnv=pn.getChild("args").getChildren();
909
910     AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
911     return an;
912   }
913
914
915   private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
916     ParseNode headern=pn.getChild("method_header");
917     ParseNode bodyn=pn.getChild("body");
918     MethodDescriptor md=parseMethodHeader(headern);
919     try {
920       BlockNode bn=parseBlock(bodyn);
921       cn.addMethod(md);
922       state.addTreeCode(md,bn);
923
924       // this is a hack for investigating new language features
925       // at the AST level, someday should evolve into a nice compiler
926       // option *wink*
927       //if( cn.getSymbol().equals( ***put a class in here like:     "Test" ) &&
928       //    md.getSymbol().equals( ***put your method in here like: "main" ) 
929       //) {
930       //  bn.setStyle( BlockNode.NORMAL );
931       //  System.out.println( bn.printNode( 0 ) );
932       //}
933
934     } catch (Exception e) {
935       System.out.println("Error with method:"+md.getSymbol());
936       e.printStackTrace();
937       throw new Error();
938     } catch (Error e) {
939       System.out.println("Error with method:"+md.getSymbol());
940       e.printStackTrace();
941       throw new Error();
942     }
943   }
944
945   private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
946     ParseNode mn=pn.getChild("modifiers");
947     Modifiers m=parseModifiersList(mn);
948     ParseNode cdecl=pn.getChild("constructor_declarator");
949     boolean isglobal=cdecl.getChild("global")!=null;
950     String name=cdecl.getChild("name").getChild("identifier").getTerminal();
951     MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
952     ParseNode paramnode=cdecl.getChild("parameters");
953     parseParameterList(md,paramnode);
954     ParseNode bodyn0=pn.getChild("body");
955     ParseNode bodyn=bodyn0.getChild("constructor_body");
956     cn.addMethod(md);
957     BlockNode bn=null;
958     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
959       bn=parseBlock(bodyn);
960     else
961       bn=new BlockNode();
962     if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
963       ParseNode sin=bodyn.getChild("superinvoke");
964       NameDescriptor nd=new NameDescriptor("super");
965       Vector args=parseArgumentList(sin);
966       MethodInvokeNode min=new MethodInvokeNode(nd);
967       for(int i=0; i<args.size(); i++) {
968         min.addArgument((ExpressionNode)args.get(i));
969       }
970       BlockExpressionNode ben=new BlockExpressionNode(min);
971       bn.addFirstBlockStatement(ben);
972
973     } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
974       ParseNode eci=bodyn.getChild("explconstrinv");
975       NameDescriptor nd=new NameDescriptor(cn.getSymbol());
976       Vector args=parseArgumentList(eci);
977       MethodInvokeNode min=new MethodInvokeNode(nd);
978       for(int i=0; i<args.size(); i++) {
979         min.addArgument((ExpressionNode)args.get(i));
980       }
981       BlockExpressionNode ben=new BlockExpressionNode(min);
982       bn.addFirstBlockStatement(ben);
983     }
984     state.addTreeCode(md,bn);
985   }
986   
987   private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
988     // Each class maintains one MethodDecscriptor which combines all its 
989     // static blocks in their declaration order
990     boolean isfirst = false;
991     MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
992     if(md == null) {
993       // the first static block for this class
994       Modifiers m_i=new Modifiers();
995       m_i.addModifier(Modifiers.STATIC);
996       md = new MethodDescriptor(m_i, "staticblocks", false);
997       md.setAsStaticBlock();
998       isfirst = true;
999     }
1000     ParseNode bodyn=pn.getChild("body");
1001     if(isfirst) {
1002       cn.addMethod(md);
1003     }
1004     cn.incStaticBlocks();
1005     BlockNode bn=null;
1006     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1007       bn=parseBlock(bodyn);
1008     else
1009       bn=new BlockNode();
1010     if(isfirst) {
1011       state.addTreeCode(md,bn);
1012     } else {
1013       BlockNode obn = state.getMethodBody(md);
1014       for(int i = 0; i < bn.size(); i++) {
1015         BlockStatementNode bsn = bn.get(i);
1016         obn.addBlockStatement(bsn);
1017       }
1018       state.addTreeCode(md, obn);
1019       bn = null;
1020     }
1021   }
1022
1023   public BlockNode parseBlock(ParseNode pn) {
1024     this.m_taskexitnum = 0;
1025     if (pn==null||isEmpty(pn.getTerminal()))
1026       return new BlockNode();
1027     ParseNode bsn=pn.getChild("block_statement_list");
1028     return parseBlockHelper(bsn);
1029   }
1030
1031   private BlockNode parseBlockHelper(ParseNode pn) {
1032     ParseNodeVector pnv=pn.getChildren();
1033     BlockNode bn=new BlockNode();
1034     for(int i=0; i<pnv.size(); i++) {
1035       Vector bsv=parseBlockStatement(pnv.elementAt(i));
1036       for(int j=0; j<bsv.size(); j++) {
1037         bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1038       }
1039     }
1040     return bn;
1041   }
1042
1043   public BlockNode parseSingleBlock(ParseNode pn) {
1044     BlockNode bn=new BlockNode();
1045     Vector bsv=parseBlockStatement(pn);
1046     for(int j=0; j<bsv.size(); j++) {
1047       bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1048     }
1049     bn.setStyle(BlockNode.NOBRACES);
1050     return bn;
1051   }
1052
1053   public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
1054     ParseNodeVector pnv=pn.getChildren();
1055     Vector bv=new Vector();
1056     for(int i=0; i<pnv.size(); i++) {
1057       bv.addAll(parseBlockStatement(pnv.elementAt(i)));
1058     }
1059     return bv;
1060   }
1061
1062   public Vector parseBlockStatement(ParseNode pn) {
1063     Vector blockstatements=new Vector();
1064     if (isNode(pn,"tag_declaration")) {
1065       String name=pn.getChild("single").getTerminal();
1066       String type=pn.getChild("type").getTerminal();
1067
1068       blockstatements.add(new TagDeclarationNode(name, type));
1069     } else if (isNode(pn,"local_variable_declaration")) {
1070       TypeDescriptor t=parseTypeDescriptor(pn);
1071       ParseNode vn=pn.getChild("variable_declarators_list");
1072       ParseNodeVector pnv=vn.getChildren();
1073       for(int i=0; i<pnv.size(); i++) {
1074         ParseNode vardecl=pnv.elementAt(i);
1075
1076
1077         ParseNode tmp=vardecl;
1078         TypeDescriptor arrayt=t;
1079         while (tmp.getChild("single")==null) {
1080           arrayt=arrayt.makeArray(state);
1081           tmp=tmp.getChild("array");
1082         }
1083         String identifier=tmp.getChild("single").getTerminal();
1084
1085         ParseNode epn=vardecl.getChild("initializer");
1086
1087
1088         ExpressionNode en=null;
1089         if (epn!=null)
1090           en=parseExpression(epn.getFirstChild());
1091
1092         blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
1093       }
1094     } else if (isNode(pn,"nop")) {
1095       /* Do Nothing */
1096     } else if (isNode(pn,"expression")) {
1097       blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
1098     } else if (isNode(pn,"ifstatement")) {
1099       blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1100                                               parseSingleBlock(pn.getChild("statement").getFirstChild()),
1101                                               pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null));
1102     } else if (isNode(pn,"switch_statement")) {
1103       // TODO add version for normal Java later
1104       blockstatements.add(new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1105           parseSingleBlock(pn.getChild("statement").getFirstChild())));
1106     } else if (isNode(pn,"switch_block_list")) {
1107       // TODO add version for normal Java later
1108       ParseNodeVector pnv=pn.getChildren();
1109       for(int i=0; i<pnv.size(); i++) {
1110         ParseNode sblockdecl=pnv.elementAt(i);
1111         
1112         if(isNode(sblockdecl, "switch_block")) {
1113           ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1114           ParseNodeVector labelv=lpn.getChildren();
1115           Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1116           for(int j=0; j<labelv.size(); j++) {
1117             ParseNode labeldecl=labelv.elementAt(j);
1118             if(isNode(labeldecl, "switch_label")) {
1119               slv.addElement(new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false));
1120             } else if(isNode(labeldecl, "default_switch_label")) {
1121               slv.addElement(new SwitchLabelNode(null, true));
1122             }
1123           }
1124           
1125           blockstatements.add(new SwitchBlockNode(slv, 
1126               parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild())));
1127           
1128         }
1129       }
1130     } else if (isNode(pn, "trycatchstatement")) {
1131       // TODO add version for normal Java later
1132       // Do not fully support exceptions now. Only make sure that if there are no
1133       // exceptions thrown, the execution is right
1134       ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1135       BlockNode bn=parseBlockHelper(tpn);
1136       blockstatements.add(new SubBlockNode(bn));
1137       
1138       ParseNode fbk = pn.getChild("finallyblock");
1139       if(fbk != null) {
1140         ParseNode fpn = fbk.getFirstChild();
1141         BlockNode fbn=parseBlockHelper(fpn);
1142         blockstatements.add(new SubBlockNode(fbn));
1143       }
1144     } else if (isNode(pn, "throwstatement")) {
1145       // TODO Simply return here
1146       //blockstatements.add(new ReturnNode());
1147     } else if (isNode(pn,"taskexit")) {
1148       Vector vfe=null;
1149       if (pn.getChild("flag_effects_list")!=null)
1150         vfe=parseFlags(pn.getChild("flag_effects_list"));
1151       Vector ccs=null;
1152       if (pn.getChild("cons_checks")!=null)
1153         ccs=parseChecks(pn.getChild("cons_checks"));
1154
1155       blockstatements.add(new TaskExitNode(vfe, ccs, this.m_taskexitnum++));
1156     } else if (isNode(pn,"atomic")) {
1157       BlockNode bn=parseBlockHelper(pn);
1158       blockstatements.add(new AtomicNode(bn));
1159     } else if (isNode(pn,"synchronized")) {
1160       BlockNode bn=parseBlockHelper(pn.getChild("block"));
1161       ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
1162       blockstatements.add(new SynchronizedNode(en, bn));
1163     } else if (isNode(pn,"return")) {
1164       if (isEmpty(pn.getTerminal()))
1165         blockstatements.add(new ReturnNode());
1166       else {
1167         ExpressionNode en=parseExpression(pn.getFirstChild());
1168         blockstatements.add(new ReturnNode(en));
1169       }
1170     } else if (isNode(pn,"block_statement_list")) {
1171       BlockNode bn=parseBlockHelper(pn);
1172       blockstatements.add(new SubBlockNode(bn));
1173     } else if (isNode(pn,"empty")) {
1174       /* nop */
1175     } else if (isNode(pn,"statement_expression_list")) {
1176       ParseNodeVector pnv=pn.getChildren();
1177       BlockNode bn=new BlockNode();
1178       for(int i=0; i<pnv.size(); i++) {
1179         ExpressionNode en=parseExpression(pnv.elementAt(i));
1180         blockstatements.add(new BlockExpressionNode(en));
1181       }
1182       bn.setStyle(BlockNode.EXPRLIST);
1183     } else if (isNode(pn,"forstatement")) {
1184       BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
1185       BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
1186       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1187       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1188       if(condition == null) {
1189         // no condition clause, make a 'true' expression as the condition
1190         condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1191       }
1192       blockstatements.add(new LoopNode(init,condition,update,body));
1193     } else if (isNode(pn,"whilestatement")) {
1194       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1195       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1196       if(condition == null) {
1197         // no condition clause, make a 'true' expression as the condition
1198         condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1199       }
1200       blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
1201     } else if (isNode(pn,"dowhilestatement")) {
1202       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1203       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1204       if(condition == null) {
1205         // no condition clause, make a 'true' expression as the condition
1206         condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1207       }
1208       blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
1209     } else if (isNode(pn,"sese")) {
1210       ParseNode pnID=pn.getChild("identifier");
1211       String stID=null;
1212       if( pnID != null ) { stID=pnID.getFirstChild().getTerminal(); }
1213       SESENode start=new SESENode(stID);
1214       SESENode end  =new SESENode(stID);
1215       start.setEnd( end   );
1216       end.setStart( start );
1217       blockstatements.add(start);
1218       blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1219       blockstatements.add(end);
1220     } else if (isNode(pn,"continue")) {
1221       blockstatements.add(new ContinueBreakNode(false));
1222     } else if (isNode(pn,"break")) {
1223       blockstatements.add(new ContinueBreakNode(true));
1224
1225     } else if (isNode(pn,"genreach")) {
1226       String graphName = pn.getChild("graphName").getTerminal();
1227       blockstatements.add( new GenReachNode( graphName ) );
1228
1229     } else {
1230       System.out.println("---------------");
1231       System.out.println(pn.PPrint(3,true));
1232       throw new Error();
1233     }
1234     return blockstatements;
1235   }
1236
1237   public MethodDescriptor parseMethodHeader(ParseNode pn) {
1238     ParseNode mn=pn.getChild("modifiers");
1239     Modifiers m=parseModifiersList(mn);
1240
1241     ParseNode tn=pn.getChild("returntype");
1242     TypeDescriptor returntype;
1243     if (tn!=null)
1244       returntype=parseTypeDescriptor(tn);
1245     else
1246       returntype=new TypeDescriptor(TypeDescriptor.VOID);
1247
1248     ParseNode pmd=pn.getChild("method_declarator");
1249     String name=pmd.getChild("name").getTerminal();
1250     MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1251
1252     ParseNode paramnode=pmd.getChild("parameters");
1253     parseParameterList(md,paramnode);
1254     return md;
1255   }
1256
1257   public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1258     ParseNode paramlist=pn.getChild("formal_parameter_list");
1259     if (paramlist==null)
1260       return;
1261     ParseNodeVector pnv=paramlist.getChildren();
1262     for(int i=0; i<pnv.size(); i++) {
1263       ParseNode paramn=pnv.elementAt(i);
1264
1265       if (isNode(paramn, "tag_parameter")) {
1266         String paramname=paramn.getChild("single").getTerminal();
1267         TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1268         md.addTagParameter(type, paramname);
1269       } else {
1270         TypeDescriptor type=parseTypeDescriptor(paramn);
1271
1272         ParseNode tmp=paramn;
1273         while (tmp.getChild("single")==null) {
1274           type=type.makeArray(state);
1275           tmp=tmp.getChild("array");
1276         }
1277         String paramname=tmp.getChild("single").getTerminal();
1278
1279         md.addParameter(type, paramname);
1280       }
1281     }
1282   }
1283
1284   public Modifiers parseModifiersList(ParseNode pn) {
1285     Modifiers m=new Modifiers();
1286     ParseNode modlist=pn.getChild("modifier_list");
1287     if (modlist!=null) {
1288       ParseNodeVector pnv=modlist.getChildren();
1289       for(int i=0; i<pnv.size(); i++) {
1290         ParseNode modn=pnv.elementAt(i);
1291         if (isNode(modn,"public"))
1292           m.addModifier(Modifiers.PUBLIC);
1293         else if (isNode(modn,"protected"))
1294           m.addModifier(Modifiers.PROTECTED);
1295         else if (isNode(modn,"private"))
1296           m.addModifier(Modifiers.PRIVATE);
1297         else if (isNode(modn,"static"))
1298           m.addModifier(Modifiers.STATIC);
1299         else if (isNode(modn,"final"))
1300           m.addModifier(Modifiers.FINAL);
1301         else if (isNode(modn,"native"))
1302           m.addModifier(Modifiers.NATIVE);
1303         else if (isNode(modn,"synchronized"))
1304           m.addModifier(Modifiers.SYNCHRONIZED);
1305         else if (isNode(modn,"atomic"))
1306           m.addModifier(Modifiers.ATOMIC);
1307     else if (isNode(modn,"abstract"))
1308       m.addModifier(Modifiers.ABSTRACT);
1309     else if (isNode(modn,"volatile"))
1310       m.addModifier(Modifiers.VOLATILE);
1311     else if (isNode(modn,"transient"))
1312       m.addModifier(Modifiers.TRANSIENT);
1313         else throw new Error("Unrecognized Modifier");
1314       }
1315     }
1316     return m;
1317   }
1318
1319   private boolean isNode(ParseNode pn, String label) {
1320     if (pn.getLabel().equals(label))
1321       return true;
1322     else return false;
1323   }
1324
1325   private static boolean isEmpty(ParseNode pn) {
1326     if (pn.getLabel().equals("empty"))
1327       return true;
1328     else
1329       return false;
1330   }
1331
1332   private static boolean isEmpty(String s) {
1333     if (s.equals("empty"))
1334       return true;
1335     else
1336       return false;
1337   }
1338
1339   /** Throw an exception if something is unexpected */
1340   private void check(ParseNode pn, String label) {
1341     if (pn == null) {
1342       throw new Error(pn+ "IE: Expected '" + label + "', got null");
1343     }
1344     if (!pn.getLabel().equals(label)) {
1345       throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
1346     }
1347   }
1348 }