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