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