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