c8efb8e7bc29898770e26fdbb7b1bb8a90733140
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3 import Util.Lattice;
4 import Util.Pair;
5
6 import java.io.File;
7 import java.util.*;
8 import java.io.*;
9 import java.lang.Throwable;
10 public class BuildIR {
11   State state;
12   private boolean isRunningRecursiveInnerClass;
13   private int m_taskexitnum;
14
15   public BuildIR(State state) {
16     this.state=state;
17     this.m_taskexitnum = 0;
18     this.isRunningRecursiveInnerClass = false;
19   }
20
21   public void buildtree(ParseNode pn, Set toanalyze, String sourcefile) {
22     parseFile(pn, toanalyze, sourcefile);
23
24     // numering the interfaces
25     int if_num = 0;
26     Iterator it_classes = state.getClassSymbolTable().getValueSet().iterator();
27     while(it_classes.hasNext()) {
28       ClassDescriptor cd = (ClassDescriptor)it_classes.next();
29       if(cd.isInterface()) {
30         cd.setInterfaceId(if_num++);
31       }
32     }
33   }
34
35   //This is all single imports and a subset of the
36   //multi imports that have been resolved.
37   ChainHashMap mandatoryImports;
38   //maps class names in file to full name
39   //Note may map a name to an ERROR.
40   ChainHashMap multiimports;
41   String packageName;
42
43   String currsourcefile;
44   Set analyzeset;
45
46   void pushChainMaps() {
47     mandatoryImports=mandatoryImports.makeChild();
48     multiimports=multiimports.makeChild();
49   }
50   
51   void popChainMaps() {
52     mandatoryImports=mandatoryImports.getParent();
53     multiimports=multiimports.getParent();
54   }
55
56   /** Parse the classes in this file */
57   public void parseFile(ParseNode pn, Set toanalyze, String sourcefile) {
58     mandatoryImports = new ChainHashMap();
59     multiimports = new ChainHashMap();
60     currsourcefile=sourcefile;
61     analyzeset=toanalyze;
62
63     if(state.JNI) {
64       //add java.lang as our default multi-import
65       this.addMultiImport(sourcefile, "java.lang", false);
66     }
67
68     ParseNode ipn = pn.getChild("imports").getChild("import_decls_list");
69     if ((ipn != null) && !state.MGC) {
70       ParseNodeVector pnv = ipn.getChildren();
71       for (int i = 0; i < pnv.size(); i++) {
72         ParseNode pnimport = pnv.elementAt(i);
73         NameDescriptor nd = parseName(pnimport.getChild("name"));
74         if (isNode(pnimport, "import_single")) {
75           if (!mandatoryImports.containsKey(nd.getIdentifier())) {
76             // map name to full name (includes package/directory
77             mandatoryImports.put(nd.getIdentifier(), nd.getPathFromRootToHere());
78           } else {
79             throw new Error("An ambiguous class "+ nd.getIdentifier() +" has been found. It is included for " +
80                             ((String)mandatoryImports.get(nd.getIdentifier())) + " and " +
81                             nd.getPathFromRootToHere());
82           }
83         } else {
84           addMultiImport(sourcefile, nd.getPathFromRootToHere(), false);
85         }
86       }
87     }
88
89     ParseNode ppn=pn.getChild("packages").getChild("package");
90     packageName = null;
91     if ((ppn!=null) && !state.MGC){
92       NameDescriptor nd = parseClassName(ppn.getChild("name"));
93       packageName = nd.getPathFromRootToHere();
94       //Trick -> import the package directory as a multi-import and it'll
95       //automatically recognize files in the same directory.
96       addMultiImport(sourcefile, packageName, true);
97     }
98
99     ParseNode tpn=pn.getChild("type_declaration_list");
100     if (tpn != null) {
101       ParseNodeVector pnv = tpn.getChildren();
102       for (int i = 0; i < pnv.size(); i++) {
103         ParseNode type_pn = pnv.elementAt(i);
104         if (isEmpty(type_pn)) /* Skip the semicolon */
105           continue;
106         if (isNode(type_pn, "class_declaration")) {
107           ClassDescriptor cn = parseTypeDecl(type_pn);
108           parseInitializers(cn);
109
110           // for inner classes/enum
111           HashSet tovisit = new HashSet();
112           Iterator it_icds = cn.getInnerClasses();
113           while (it_icds.hasNext()) {
114             tovisit.add(it_icds.next());
115           }
116
117           while (!tovisit.isEmpty()) {
118             ClassDescriptor cd = (ClassDescriptor) tovisit.iterator().next();
119             tovisit.remove(cd);
120             parseInitializers(cd);
121
122             Iterator it_ics = cd.getInnerClasses();
123             while (it_ics.hasNext()) {
124               tovisit.add(it_ics.next());
125             }
126           }
127         } else if (isNode(type_pn, "task_declaration")) {
128           TaskDescriptor td = parseTaskDecl(type_pn);
129           if (toanalyze != null)
130             toanalyze.add(td);
131           state.addTask(td);
132         } else if (isNode(type_pn, "interface_declaration")) {
133           // TODO add version for normal Java later
134           ClassDescriptor cn = parseInterfaceDecl(type_pn, null);
135         } else if (isNode(type_pn, "enum_declaration")) {
136           // TODO add version for normal Java later
137           ClassDescriptor cn = parseEnumDecl(null, type_pn);
138
139         } else if(isNode(type_pn,"annotation_type_declaration")) {
140           ClassDescriptor cn=parseAnnotationTypeDecl(type_pn);
141         } else {
142           throw new Error(type_pn.getLabel());
143         }
144       }
145     }
146   }
147
148
149   //This kind of breaks away from tradition a little bit by doing the file checks here
150   // instead of in Semantic check, but doing it here is easier because we have a mapping early on
151   // if I wait until semantic check, I have to change ALL the type descriptors to match the new
152   // mapping and that's both ugly and tedious.
153   private void addMultiImport(String currentSource, String importPath, boolean isPackageDirectory) {
154     boolean found = false;
155     for (int j = 0; j < state.classpath.size(); j++) {
156       String path = (String) state.classpath.get(j);
157       File folder = new File(path, importPath.replace('.', '/'));
158       if (folder.exists()) {
159         found = true;
160         for (String file : folder.list()) {
161           // if the file is of type *.java add to multiImport list.
162           if (file.lastIndexOf('.') != -1 && file.substring(file.lastIndexOf('.')).equalsIgnoreCase(".java")) {
163             String classname = file.substring(0, file.length() - 5);
164             // single imports have precedence over multi-imports
165             if (!mandatoryImports.containsKey(classname)) {
166               //package files have precedence over multi-imports.
167               if (multiimports.containsKey(classname)  && !isPackageDirectory) {
168                 // put error in for later, in case we try to import
169                 multiimports.put(classname, new Error("Error: class " + classname + " is defined more than once in a multi-import in " + currentSource));
170               } else {
171                 multiimports.put(classname, importPath + "." + classname);
172               }
173             }
174           }
175         }
176       }
177     }
178
179     if(!found) {
180       throw new Error("Import package " + importPath + " in  " + currentSource
181                       + " cannot be resolved.");
182     }
183   }
184
185   public void parseInitializers(ClassDescriptor cn) {
186     Vector fv=cn.getFieldVec();
187     Iterator methodit = cn.getMethods();
188     HashMap<MethodDescriptor, Integer> md2pos = new HashMap<MethodDescriptor, Integer>();
189     while(methodit.hasNext()) {
190       MethodDescriptor currmd=(MethodDescriptor)methodit.next();
191       if(currmd.isConstructor()) {
192         BlockNode bn=state.getMethodBody(currmd);
193         // if there are super(...) invokation, the initializers should be invoked after that
194         int i = 0;
195         for(; i < bn.size(); i++) {
196           if(Kind.BlockExpressionNode==bn.get(i).kind()
197                   &&(((BlockExpressionNode)bn.get(i)).getExpression() instanceof MethodInvokeNode)
198                   &&((MethodInvokeNode)(((BlockExpressionNode)bn.get(i)).getExpression())).getMethodName().equals("super")) {
199             break;
200           }
201         }
202         if(i==bn.size()) {
203           md2pos.put(currmd, 0);
204         } else {
205           md2pos.put(currmd, i+1);
206         }
207       }
208     }
209     int pos = 0;
210     for(int i=0; i<fv.size(); i++) {
211       FieldDescriptor fd=(FieldDescriptor)fv.get(i);
212       if(fd.getExpressionNode()!=null) {
213         methodit = cn.getMethods();
214         while(methodit.hasNext()) {
215           MethodDescriptor currmd=(MethodDescriptor)methodit.next();
216           if(currmd.isConstructor()) {
217             int offset = md2pos.get(currmd);
218             BlockNode bn=state.getMethodBody(currmd);
219             NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
220             AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
221             bn.addBlockStatementAt(new BlockExpressionNode(an), pos+offset);
222           }
223         }
224         pos++;
225       }
226     }
227   }
228   
229   private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {    
230     String basename=pn.getChild("name").getTerminal();
231     String classname=(cn!=null)?cn.getClassName()+"$"+basename:basename;
232     ClassDescriptor ecd=new ClassDescriptor(cn!=null?cn.getPackage():null, classname, false);
233     
234     if (cn!=null) {
235       if (packageName==null)
236         cn.getSingleImportMappings().put(basename,classname);
237       else
238         cn.getSingleImportMappings().put(basename,packageName+"."+classname);
239     }
240
241     pushChainMaps();
242     ecd.setImports(mandatoryImports, multiimports);
243     ecd.setAsEnum();
244     if(cn != null) {
245       ecd.setSurroundingClass(cn.getSymbol());
246       ecd.setSurrounding(cn);
247       cn.addEnum(ecd);
248     }
249     if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
250           ecd.getSymbol().equals(TypeUtil.TagClass))) {
251       ecd.setSuper(TypeUtil.ObjectClass);
252     }
253     ecd.setModifiers(parseModifiersList(pn.getChild("modifiers")));
254     parseEnumBody(ecd, pn.getChild("enumbody"));
255
256     addClass2State(ecd);
257
258     popChainMaps();
259     return ecd;
260   }
261
262   private void parseEnumBody(ClassDescriptor cn, ParseNode pn) {
263     ParseNode decls=pn.getChild("enum_constants_list");
264     if (decls!=null) {
265       ParseNodeVector pnv=decls.getChildren();
266       for(int i=0; i<pnv.size(); i++) {
267         ParseNode decl=pnv.elementAt(i);
268         if (isNode(decl,"enum_constant")) {
269           parseEnumConstant(cn,decl);
270         } else throw new Error();
271       }
272     }
273   }
274
275   private void parseEnumConstant(ClassDescriptor cn, ParseNode pn) {
276     cn.addEnumConstant(pn.getChild("name").getTerminal());
277   }
278
279   private ClassDescriptor parseAnnotationTypeDecl(ParseNode pn) {
280     ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
281     pushChainMaps();
282     cn.setImports(mandatoryImports, multiimports);
283     ParseNode modifiers=pn.getChild("modifiers");
284     if(modifiers!=null) {
285       cn.setModifiers(parseModifiersList(modifiers));
286     }
287     parseAnnotationTypeBody(cn,pn.getChild("body"));
288     popChainMaps();
289
290     addClass2State(cn);
291
292     return cn;
293   }
294
295   private void parseAnnotationTypeBody(ClassDescriptor cn, ParseNode pn) {
296     ParseNode list_node=pn.getChild("annotation_type_element_list");
297     if(list_node!=null) {
298       ParseNodeVector pnv = list_node.getChildren();
299       for (int i = 0; i < pnv.size(); i++) {
300         ParseNode element_node = pnv.elementAt(i);
301         if (isNode(element_node, "annotation_type_element_declaration")) {
302           ParseNodeVector elementProps = element_node.getChildren();
303           String identifier=null;
304           TypeDescriptor type=null;
305           Modifiers modifiers=new Modifiers();
306           for(int eidx=0; eidx<elementProps.size(); eidx++) {
307             ParseNode prop_node=elementProps.elementAt(eidx);
308             if(isNode(prop_node,"name")) {
309               identifier=prop_node.getTerminal();
310             } else if(isNode(prop_node,"type")) {
311               type=parseTypeDescriptor(prop_node);
312             } else if(isNode(prop_node,"modifier")) {
313               modifiers=parseModifiersList(prop_node);
314             }
315           }
316           cn.addField(new FieldDescriptor(modifiers, type, identifier, null, false));
317         }
318       }
319     }
320   }
321
322   public ClassDescriptor parseInterfaceDecl(ParseNode pn, ClassDescriptor outerclass) {
323     String basename=pn.getChild("name").getTerminal();
324     String classname=((outerclass==null)?"":(outerclass.getClassName()+"$"))+basename;
325     if (outerclass!=null) {
326       if (packageName==null)
327         outerclass.getSingleImportMappings().put(basename,classname);
328       else
329         outerclass.getSingleImportMappings().put(basename,packageName+"."+classname);
330     }
331     ClassDescriptor cn= new ClassDescriptor(packageName, classname, true);
332
333     pushChainMaps();
334     cn.setImports(mandatoryImports, multiimports);
335     //cn.setAsInterface();
336     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
337       /* parse inherited interface name */
338       ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
339       ParseNodeVector pnv=snlist.getChildren();
340       for(int i=0; i<pnv.size(); i++) {
341         ParseNode decl=pnv.elementAt(i);
342         if (isNode(decl,"type")) {
343           NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
344           cn.addSuperInterface(nd.toString());
345         } else if (isNode(decl, "interface_declaration")) {
346           ClassDescriptor innercn = parseInterfaceDecl(decl, cn);
347         } else throw new Error();
348       }
349     }
350     cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
351     parseInterfaceBody(cn, pn.getChild("interfacebody"));
352     addClass2State(cn);
353     popChainMaps();
354     return cn;
355   }
356
357   private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
358     assert(cn.isInterface());
359     ParseNode decls=pn.getChild("interface_member_declaration_list");
360     if (decls!=null) {
361       ParseNodeVector pnv=decls.getChildren();
362       for(int i=0; i<pnv.size(); i++) {
363         ParseNode decl=pnv.elementAt(i);
364         if (isNode(decl,"constant")) {
365           parseInterfaceConstant(cn,decl);
366         } else if (isNode(decl,"method")) {
367           parseInterfaceMethod(cn,decl.getChild("method_declaration"));
368         } else if (isNode(decl, "interface_declaration")) {
369           parseInterfaceDecl(decl, cn);
370         } else throw new Error(decl.PPrint(2, true));
371       }
372     }
373   }
374
375
376
377   private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
378     if (pn!=null) {
379       parseFieldDecl(cn,pn.getChild("field_declaration"));
380       return;
381     }
382     throw new Error();
383   }
384
385   private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
386     ParseNode headern=pn.getChild("header");
387     ParseNode bodyn=pn.getChild("body");
388     MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
389     md.getModifiers().addModifier(Modifiers.PUBLIC);
390     md.getModifiers().addModifier(Modifiers.ABSTRACT);
391     try {
392       BlockNode bn=parseBlock(cn, md, md.isStatic()||md.isStaticBlock(), bodyn);
393       cn.addMethod(md);
394       state.addTreeCode(md,bn);
395     } catch (Exception e) {
396       System.out.println("Error with method:"+md.getSymbol());
397       e.printStackTrace();
398       throw new Error();
399     } catch (Error e) {
400       System.out.println("Error with method:"+md.getSymbol());
401       e.printStackTrace();
402       throw new Error();
403     }
404   }
405
406   public TaskDescriptor parseTaskDecl(ParseNode pn) {
407     TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
408     ParseNode bodyn=pn.getChild("body");
409     BlockNode bn=parseBlock(null, null, false, bodyn);
410     parseParameterList(td, pn);
411     state.addTreeCode(td,bn);
412     if (pn.getChild("flag_effects_list")!=null)
413       td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
414     return td;
415   }
416
417   public Vector parseFlags(ParseNode pn) {
418     Vector vfe=new Vector();
419     ParseNodeVector pnv=pn.getChildren();
420     for(int i=0; i<pnv.size(); i++) {
421       ParseNode fn=pnv.elementAt(i);
422       FlagEffects fe=parseFlagEffects(fn);
423       vfe.add(fe);
424     }
425     return vfe;
426   }
427
428   public FlagEffects parseFlagEffects(ParseNode pn) {
429     if (isNode(pn,"flag_effect")) {
430       String flagname=pn.getChild("name").getTerminal();
431       FlagEffects fe=new FlagEffects(flagname);
432       if (pn.getChild("flag_list")!=null)
433         parseFlagEffect(fe, pn.getChild("flag_list"));
434       if (pn.getChild("tag_list")!=null)
435         parseTagEffect(fe, pn.getChild("tag_list"));
436       return fe;
437     } else throw new Error();
438   }
439
440   public void parseTagEffect(FlagEffects fes, ParseNode pn) {
441     ParseNodeVector pnv=pn.getChildren();
442     for(int i=0; i<pnv.size(); i++) {
443       ParseNode pn2=pnv.elementAt(i);
444       boolean status=true;
445       if (isNode(pn2,"not")) {
446         status=false;
447         pn2=pn2.getChild("name");
448       }
449       String name=pn2.getTerminal();
450       fes.addTagEffect(new TagEffect(name,status));
451     }
452   }
453
454   public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
455     ParseNodeVector pnv=pn.getChildren();
456     for(int i=0; i<pnv.size(); i++) {
457       ParseNode pn2=pnv.elementAt(i);
458       boolean status=true;
459       if (isNode(pn2,"not")) {
460         status=false;
461         pn2=pn2.getChild("name");
462       }
463       String name=pn2.getTerminal();
464       fes.addEffect(new FlagEffect(name,status));
465     }
466   }
467
468   public FlagExpressionNode parseFlagExpression(ParseNode pn) {
469     if (isNode(pn,"or")) {
470       ParseNodeVector pnv=pn.getChildren();
471       ParseNode left=pnv.elementAt(0);
472       ParseNode right=pnv.elementAt(1);
473       return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
474     } else if (isNode(pn,"and")) {
475       ParseNodeVector pnv=pn.getChildren();
476       ParseNode left=pnv.elementAt(0);
477       ParseNode right=pnv.elementAt(1);
478       return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
479     } else if (isNode(pn, "not")) {
480       ParseNodeVector pnv=pn.getChildren();
481       ParseNode left=pnv.elementAt(0);
482       return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
483
484     } else if (isNode(pn,"name")) {
485       return new FlagNode(pn.getTerminal());
486     } else {
487       throw new Error();
488     }
489   }
490
491   public Vector parseChecks(ParseNode pn) {
492     Vector ccs=new Vector();
493     ParseNodeVector pnv=pn.getChildren();
494     for(int i=0; i<pnv.size(); i++) {
495       ParseNode fn=pnv.elementAt(i);
496       ConstraintCheck cc=parseConstraintCheck(fn);
497       ccs.add(cc);
498     }
499     return ccs;
500   }
501
502   public ConstraintCheck parseConstraintCheck(ParseNode pn) {
503     if (isNode(pn,"cons_check")) {
504       String specname=pn.getChild("name").getChild("identifier").getTerminal();
505       Vector[] args=parseConsArgumentList(pn);
506       ConstraintCheck cc=new ConstraintCheck(specname);
507       for(int i=0; i<args[0].size(); i++) {
508         cc.addVariable((String)args[0].get(i));
509         cc.addArgument((ExpressionNode)args[1].get(i));
510       }
511       return cc;
512     } else throw new Error();
513   }
514
515   public void parseParameterList(TaskDescriptor td, ParseNode pn) {
516
517     boolean optional;
518     ParseNode paramlist=pn.getChild("task_parameter_list");
519     if (paramlist==null)
520       return;
521     ParseNodeVector pnv=paramlist.getChildren();
522     for(int i=0; i<pnv.size(); i++) {
523       ParseNode paramn=pnv.elementAt(i);
524       if(paramn.getChild("optional")!=null) {
525         optional = true;
526         paramn = paramn.getChild("optional").getFirstChild();
527         System.out.println("OPTIONAL FOUND!!!!!!!");
528       } else { optional = false;
529                System.out.println("NOT OPTIONAL"); }
530
531       TypeDescriptor type=parseTypeDescriptor(paramn);
532
533       String paramname=paramn.getChild("single").getTerminal();
534       FlagExpressionNode fen=null;
535       if (paramn.getChild("flag")!=null)
536         fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
537
538       ParseNode tagnode=paramn.getChild("tag");
539
540       TagExpressionList tel=null;
541       if (tagnode!=null) {
542         tel=parseTagExpressionList(tagnode);
543       }
544
545       td.addParameter(type,paramname,fen, tel, optional);
546     }
547   }
548
549   public TagExpressionList parseTagExpressionList(ParseNode pn) {
550     //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
551     //To test, feed in any input program that uses tags
552     ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
553     TagExpressionList tel=new TagExpressionList();
554     for(int i=0; i<pnv.size(); i++) {
555       ParseNode tn=pnv.elementAt(i);
556       String type=tn.getChild("type").getTerminal();
557       String name=tn.getChild("single").getTerminal();
558       tel.addTag(type, name);
559     }
560     return tel;
561   }
562
563   private void addClass2State(ClassDescriptor cn) {
564     if (analyzeset != null)
565       analyzeset.add(cn);
566     cn.setSourceFileName(currsourcefile);
567     state.addClass(cn);
568     // create this$n representing a final reference to the next surrounding class. each inner class should have whatever inner class
569     // pointers the surrounding class has + a pointer to the surrounding class.
570     if( true )
571     {
572       this.isRunningRecursiveInnerClass = true; //fOR dEBUGGING PURPOSES IN ORDER TO DUMP STRINGS WHILE IN THIS CODE PATH
573       addOuterClassReferences( cn, cn, 0 );
574       addOuterClassParam( cn, cn, 0 );
575       this.isRunningRecursiveInnerClass = false;
576     }
577   }
578   
579   public ClassDescriptor parseTypeDecl(ParseNode pn) {
580     ClassDescriptor cn=new ClassDescriptor(packageName, pn.getChild("name").getTerminal(), false);
581     pushChainMaps();
582     cn.setImports(mandatoryImports, multiimports);
583     if (!isEmpty(pn.getChild("super").getTerminal())) {
584       /* parse superclass name */
585       ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
586       NameDescriptor nd=parseClassName(snn);
587       cn.setSuper(nd.toString());
588     } else {
589       if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
590             cn.getSymbol().equals(TypeUtil.TagClass)))
591         cn.setSuper(TypeUtil.ObjectClass);
592     }
593     // check inherited interfaces
594     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
595       /* parse inherited interface name */
596       ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
597       ParseNodeVector pnv=snlist.getChildren();
598       for(int i=0; i<pnv.size(); i++) {
599         ParseNode decl=pnv.elementAt(i);
600         if (isNode(decl,"type")) {
601           NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
602           cn.addSuperInterface(nd.toString());
603         }
604       }
605     }
606     cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
607     parseClassBody(cn, pn.getChild("classbody"));
608
609     boolean hasConstructor = false;
610     for(Iterator method_it=cn.getMethods(); method_it.hasNext(); ) {
611       MethodDescriptor md=(MethodDescriptor)method_it.next();
612       hasConstructor |= md.isConstructor();
613     }
614     if((!hasConstructor) && (!cn.isEnum())) {
615       // add a default constructor for this class
616       MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
617                                                  cn.getSymbol(), false);
618       BlockNode bn=new BlockNode();
619       state.addTreeCode(md,bn);
620       md.setDefaultConstructor();
621       cn.addMethod(md);
622     }
623
624
625     popChainMaps();
626
627     cn.setSourceFileName(currsourcefile);
628     
629     addClass2State(cn);
630
631     return cn;
632   }
633
634 private void initializeOuterMember( MethodDescriptor md, String fieldName, String formalParameter ) {
635          BlockNode obn = state.getMethodBody(md);
636          NameNode nn=new NameNode( new NameDescriptor( fieldName ) );
637          NameNode fn = new NameNode ( new NameDescriptor( formalParameter ) );
638           //nn.setNumLine(en.getNumLine())
639          AssignmentNode an=new AssignmentNode(nn,fn,new AssignOperation(1));
640          //an.setNumLine(pn.getLine());
641          obn.addFirstBlockStatement(new BlockExpressionNode(an));
642         // System.out.print( "The code inserted is : " + obn.printNode( 0 ) + "\n" );
643          state.addTreeCode(md, obn);
644 }
645
646 private void addOuterClassParam( ClassDescriptor cn, ClassDescriptor ocn, int depth )
647 {
648         Iterator nullCheckItr = cn.getInnerClasses();
649         if( false == nullCheckItr.hasNext() )
650                 return;
651
652         //create a typedescriptor of type cn
653         TypeDescriptor theTypeDesc = new TypeDescriptor( ocn );
654         
655         for(Iterator it=cn.getInnerClasses(); it.hasNext(); ) {
656                 ClassDescriptor icd=(ClassDescriptor)it.next();
657                 if(icd.isStatic()||icd.getInStaticContext()) {
658                     continue;
659                 }
660                 
661                 //iterate over all ctors of I.Cs and add a new param
662                 for(Iterator method_it=icd.getMethods(); method_it.hasNext(); ) {
663                          MethodDescriptor md=(MethodDescriptor)method_it.next();
664                          if( md.isConstructor() ){
665                                 md.addParameter( theTypeDesc, "surrounding___DOLLAR___" + String.valueOf(depth) );      
666                                 initializeOuterMember( md, "this___DOLLAR___" + String.valueOf( depth ), "surrounding___DOLLAR___" + String.valueOf(depth) );
667                                 //System.out.println( "The added param is " + md.toString() + "\n" );
668                         }
669                 }
670                 addOuterClassParam( icd, ocn, depth + 1 );
671                 
672         }
673         
674 }
675 private void addOuterClassReferences( ClassDescriptor cn, ClassDescriptor ocn, int depth )
676 {
677         //SYMBOLTABLE does not have a length or empty method, hence could not define a hasInnerClasses method in classDescriptor
678         Iterator nullCheckItr = cn.getInnerClasses();
679         if( false == nullCheckItr.hasNext() )
680                 return;
681
682         String tempCopy = ocn.getClassName();
683         //MESSY HACK FOLLOWS
684         int i = 0;
685
686         ParseNode theNode = new ParseNode( "field_declaration" );
687         theNode.addChild("modifier").addChild( new ParseNode( "modifier_list" ) ).addChild("final");
688         ParseNode theTypeNode = new ParseNode("type");
689         ParseNode tempChildNode = theTypeNode.addChild("class").addChild( "name" );
690                 //tempChildNode.addChild("base").addChild( new ParseNode("empty") );
691         tempChildNode.addChild("identifier").addChild ( tempCopy );
692         theNode.addChild("type").addChild( theTypeNode );
693         ParseNode variableDeclaratorID = new ParseNode("single");
694         String theStr = "this___DOLLAR___" + String.valueOf( depth );
695         variableDeclaratorID.addChild( theStr );
696         ParseNode variableDeclarator = new ParseNode( "variable_declarator" );
697         variableDeclarator.addChild( variableDeclaratorID );
698         ParseNode variableDeclaratorList = new ParseNode("variable_declarators_list");
699         variableDeclaratorList.addChild( variableDeclarator );
700         theNode.addChild("variables").addChild( variableDeclaratorList );
701
702         for(Iterator it=cn.getInnerClasses(); it.hasNext(); ) {
703                 ClassDescriptor icd=(ClassDescriptor)it.next();
704                 if(icd.isStatic() || icd.getInStaticContext()) {
705                   continue;
706                 }
707                 parseFieldDecl( icd, theNode );         
708                 /*if( true ) {
709                         SymbolTable fieldTable = icd.getFieldTable();
710                         //System.out.println( fieldTable.toString() );
711                 }*/
712                 icd.setInnerDepth( depth + 1 );
713                 addOuterClassReferences( icd, ocn, depth + 1 ); 
714         }
715 }
716
717   private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
718     ParseNode decls=pn.getChild("class_body_declaration_list");
719     if (decls!=null) {
720       ParseNodeVector pnv=decls.getChildren();
721       for(int i=0; i<pnv.size(); i++) {
722         ParseNode decl=pnv.elementAt(i);
723         if (isNode(decl,"member")) {
724           parseClassMember(cn,decl);
725         } else if (isNode(decl,"constructor")) {
726           parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
727         } else if (isNode(decl, "static_block")) {
728           parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
729         } else if (isNode(decl,"block")) {
730         } else throw new Error();
731       }
732     }
733   }
734
735   private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
736     ParseNode fieldnode=pn.getChild("field");
737     if (fieldnode!=null) {
738       //System.out.println( pn.PPrint( 0, true ) );
739       parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
740       return;
741     }
742     ParseNode methodnode=pn.getChild("method");
743     if (methodnode!=null) {
744       parseMethodDecl(cn,methodnode.getChild("method_declaration"));
745       return;
746     }
747     ParseNode innerclassnode=pn.getChild("inner_class_declaration");
748     if (innerclassnode!=null) {
749       parseInnerClassDecl(cn,innerclassnode);
750       return;
751     }
752      ParseNode innerinterfacenode=pn.getChild("interface_declaration");
753     if (innerinterfacenode!=null) {
754       parseInterfaceDecl(innerinterfacenode, cn);
755       return;
756     }
757
758     ParseNode enumnode=pn.getChild("enum_declaration");
759     if (enumnode!=null) {
760       ClassDescriptor ecn=parseEnumDecl(cn,enumnode);
761       return;
762     }
763     ParseNode flagnode=pn.getChild("flag");
764     if (flagnode!=null) {
765       parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
766       return;
767     }
768     // in case there are empty node
769     ParseNode emptynode=pn.getChild("empty");
770     if(emptynode != null) {
771       return;
772     }
773     System.out.println("Unrecognized node:"+pn.PPrint(2,true));
774     throw new Error();
775   }
776
777 //10/9/2011 changed this function to enable creation of default constructor for inner classes.
778 //the change was refactoring this function with the corresponding version for normal classes. sganapat
779   private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
780     String basename=pn.getChild("name").getTerminal();
781     String classname=cn.getClassName()+"$"+basename;
782
783     if (cn.getPackage()==null)
784       cn.getSingleImportMappings().put(basename,classname);
785     else
786       cn.getSingleImportMappings().put(basename,cn.getPackage()+"."+classname);
787     
788     ClassDescriptor icn=new ClassDescriptor(cn.getPackage(), classname, false);
789     pushChainMaps();
790     icn.setImports(mandatoryImports, multiimports);
791     icn.setSurroundingClass(cn.getSymbol());
792     icn.setSurrounding(cn);
793     cn.addInnerClass(icn);
794
795      if (!isEmpty(pn.getChild("super").getTerminal())) {
796       /* parse superclass name */
797       ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
798       NameDescriptor nd=parseClassName(snn);
799       icn.setSuper(nd.toString());
800     } else {
801       if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
802             icn.getSymbol().equals(TypeUtil.TagClass)))
803         icn.setSuper(TypeUtil.ObjectClass);
804     }
805     // check inherited interfaces
806     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
807       /* parse inherited interface name */
808       ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
809       ParseNodeVector pnv=snlist.getChildren();
810       for(int i=0; i<pnv.size(); i++) {
811         ParseNode decl=pnv.elementAt(i);
812         if (isNode(decl,"type")) {
813           NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
814           icn.addSuperInterface(nd.toString());
815         }
816       }
817     }
818     icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
819
820    if (!icn.isStatic())
821      icn.setAsInnerClass();
822
823     parseClassBody(icn, pn.getChild("classbody"));
824
825     boolean hasConstructor = false;
826     for(Iterator method_it=icn.getMethods(); method_it.hasNext(); ) {
827       MethodDescriptor md=(MethodDescriptor)method_it.next();
828       hasConstructor |= md.isConstructor();
829     }
830 //sganapat adding change to allow proper construction of inner class objects
831     if((!hasConstructor) && (!icn.isEnum())) {
832       // add a default constructor for this class
833       MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
834                                                  icn.getSymbol(), false);
835       BlockNode bn=new BlockNode();
836       state.addTreeCode(md,bn);
837       md.setDefaultConstructor();
838       icn.addMethod(md);
839    }
840     popChainMaps();
841
842     addClass2State(icn);
843
844     return icn;
845   }
846
847   private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
848     ParseNode tn=pn.getChild("type");
849     String type_st=tn.getTerminal();
850     if(type_st.equals("byte")) {
851       return state.getTypeDescriptor(TypeDescriptor.BYTE);
852     } else if(type_st.equals("short")) {
853       return state.getTypeDescriptor(TypeDescriptor.SHORT);
854     } else if(type_st.equals("boolean")) {
855       return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
856     } else if(type_st.equals("int")) {
857       return state.getTypeDescriptor(TypeDescriptor.INT);
858     } else if(type_st.equals("long")) {
859       return state.getTypeDescriptor(TypeDescriptor.LONG);
860     } else if(type_st.equals("char")) {
861       return state.getTypeDescriptor(TypeDescriptor.CHAR);
862     } else if(type_st.equals("float")) {
863       return state.getTypeDescriptor(TypeDescriptor.FLOAT);
864     } else if(type_st.equals("double")) {
865       return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
866     } else if(type_st.equals("class")) {
867       ParseNode nn=tn.getChild("class");
868       return state.getTypeDescriptor(parseClassName(nn.getChild("name")));
869     } else if(type_st.equals("array")) {
870       ParseNode nn=tn.getChild("array");
871       TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
872       Integer numdims=(Integer)nn.getChild("dims").getLiteral();
873       for(int i=0; i<numdims.intValue(); i++)
874         td=td.makeArray(state);
875       return td;
876     } else {
877       System.out.println(pn.PPrint(2, true));
878       throw new Error();
879     }
880   }
881
882   //Needed to separate out top level call since if a base exists,
883   //we do not want to apply our resolveName function (i.e. deal with imports)
884   //otherwise, if base == null, we do just want to resolve name.
885   private NameDescriptor parseClassName(ParseNode nn) {
886     
887
888     ParseNode base=nn.getChild("base");
889     ParseNode id=nn.getChild("identifier");
890     String classname = id.getTerminal();
891     if (base==null) {
892       return new NameDescriptor(resolveName(classname));
893     }
894     return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
895   }
896
897   private NameDescriptor parseClassNameRecursive(ParseNode nn) {
898     ParseNode base=nn.getChild("base");
899     ParseNode id=nn.getChild("identifier");
900     String classname = id.getTerminal();
901     if (base==null) {
902       return new NameDescriptor(classname);
903     }
904     return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
905   }
906
907   //This will get the mapping of a terminal class name
908   //to a canonical classname (with imports/package locations in them)
909   private String resolveName(String terminal) {
910     
911     if(mandatoryImports.containsKey(terminal)) {
912       return (String) mandatoryImports.get(terminal);
913     } else {
914       if(multiimports.containsKey(terminal)) {
915         //Test for error
916         Object o = multiimports.get(terminal);
917         if(o instanceof Error) {
918           throw new Error("Class " + terminal + " is ambiguous. Cause: more than 1 package import contain the same class.");
919         } else {
920           //At this point, if we found a unique class
921           //we can treat it as a single, mandatory import.
922           mandatoryImports.put(terminal, o);
923           return (String) o;
924         }
925       }
926     }
927
928     return terminal;
929   }
930
931   //only function difference between this and parseName() is that this
932   //does not look for a import mapping.
933   private NameDescriptor parseName(ParseNode nn) {
934     ParseNode base=nn.getChild("base");
935     ParseNode id=nn.getChild("identifier");
936     if (base==null) {
937       return new NameDescriptor(id.getTerminal());
938     }
939     return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
940   }
941
942   private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
943     String name=pn.getChild("name").getTerminal();
944     FlagDescriptor flag=new FlagDescriptor(name);
945     if (pn.getChild("external")!=null)
946       flag.makeExternal();
947     cn.addFlag(flag);
948   }
949
950   private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
951     ParseNode mn=pn.getChild("modifier");
952     Modifiers m=parseModifiersList(mn);
953     if(cn.isInterface()) {
954       // TODO add version for normal Java later
955       // Can only be PUBLIC or STATIC or FINAL
956       if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
957          || (m.isSynchronized())) {
958         throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
959       }
960       m.addModifier(Modifiers.PUBLIC);
961       m.addModifier(Modifiers.STATIC);
962       m.addModifier(Modifiers.FINAL);
963     }
964
965     ParseNode tn=pn.getChild("type");
966     TypeDescriptor t=parseTypeDescriptor(tn);
967     ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
968     ParseNodeVector pnv=vn.getChildren();
969     boolean isglobal=pn.getChild("global")!=null;
970
971     for(int i=0; i<pnv.size(); i++) {
972       ParseNode vardecl=pnv.elementAt(i);
973       ParseNode tmp=vardecl;
974       TypeDescriptor arrayt=t;
975        if( this.isRunningRecursiveInnerClass && false )
976         {       
977                 System.out.println( "the length of the list is " + String.valueOf( pnv.size() ) );
978                 System.out.println( "\n the parse node is \n" + tmp.PPrint( 0, true ) );
979         }
980       while (tmp.getChild("single")==null) {
981         arrayt=arrayt.makeArray(state);
982         tmp=tmp.getChild("array");
983       }
984       String identifier=tmp.getChild("single").getTerminal();
985       ParseNode epn=vardecl.getChild("initializer");
986
987       ExpressionNode en=null;
988       
989       if (epn!=null) {
990         en=parseExpression(cn, null, m.isStatic(), epn.getFirstChild());
991         en.setNumLine(epn.getFirstChild().getLine());
992         if(m.isStatic()) {
993           // for static field, the initializer should be considered as a
994           // static block
995           boolean isfirst = false;
996           MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
997           if(md == null) {
998             // the first static block for this class
999             Modifiers m_i=new Modifiers();
1000             m_i.addModifier(Modifiers.STATIC);
1001             md = new MethodDescriptor(m_i, "staticblocks", false);
1002             md.setAsStaticBlock();
1003             isfirst = true;
1004           }
1005           if(isfirst) {
1006             cn.addMethod(md);
1007           }
1008           cn.incStaticBlocks();
1009           BlockNode bn=new BlockNode();
1010           NameNode nn=new NameNode(new NameDescriptor(identifier));
1011           nn.setNumLine(en.getNumLine());
1012           AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
1013           an.setNumLine(pn.getLine());
1014           bn.addBlockStatement(new BlockExpressionNode(an));
1015           if(isfirst) {
1016             state.addTreeCode(md,bn);
1017           } else {
1018             BlockNode obn = state.getMethodBody(md);
1019             for(int ii = 0; ii < bn.size(); ii++) {
1020               BlockStatementNode bsn = bn.get(ii);
1021               obn.addBlockStatement(bsn);
1022             }
1023             state.addTreeCode(md, obn);
1024             bn = null;
1025           }
1026           en = null;
1027         }
1028       }
1029
1030       cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
1031       assignAnnotationsToType(m,arrayt);
1032     }
1033   }
1034
1035   private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type) {
1036     Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
1037     for(int i=0; i<annotations.size(); i++) {
1038       // it only supports a marker annotation
1039       AnnotationDescriptor an=annotations.elementAt(i);
1040       type.addAnnotationMarker(an);
1041     }
1042   }
1043
1044   int innerCount=0;
1045
1046   private ExpressionNode parseExpression(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1047     if (isNode(pn,"assignment"))
1048         {
1049           //System.out.println( "parsing a field decl in my class that has assignment in initialization " + pn.PPrint( 0, true ) + "\n");
1050       return parseAssignmentExpression(cn, md, isStaticContext, pn);
1051         }
1052     else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
1053              isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
1054              isNode(pn,"bitwise_and")||isNode(pn,"equal")||
1055              isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
1056              isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
1057              isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
1058              isNode(pn,"rightshift")||isNode(pn,"sub")||
1059              isNode(pn,"urightshift")||isNode(pn,"sub")||
1060              isNode(pn,"add")||isNode(pn,"mult")||
1061              isNode(pn,"div")||isNode(pn,"mod")) {
1062       ParseNodeVector pnv=pn.getChildren();
1063       ParseNode left=pnv.elementAt(0);
1064       ParseNode right=pnv.elementAt(1);
1065       Operation op=new Operation(pn.getLabel());
1066       OpNode on=new OpNode(parseExpression(cn, md, isStaticContext, left),parseExpression(cn, md, isStaticContext,  right),op);
1067       on.setNumLine(pn.getLine());
1068       return on;
1069     } else if (isNode(pn,"unaryplus")||
1070                isNode(pn,"unaryminus")||
1071                isNode(pn,"not")||
1072                isNode(pn,"comp")) {
1073       ParseNode left=pn.getFirstChild();
1074       Operation op=new Operation(pn.getLabel());
1075       OpNode on=new OpNode(parseExpression(cn, md, isStaticContext, left),op);
1076       on.setNumLine(pn.getLine());
1077       return on;
1078     } else if (isNode(pn,"postinc")||
1079                isNode(pn,"postdec")) {
1080       ParseNode left=pn.getFirstChild();
1081       AssignOperation op=new AssignOperation(pn.getLabel());
1082       AssignmentNode an=new AssignmentNode(parseExpression(cn, md, isStaticContext, left),null,op);
1083       an.setNumLine(pn.getLine());
1084       return an;
1085
1086     } else if (isNode(pn,"preinc")||
1087                isNode(pn,"predec")) {
1088       ParseNode left=pn.getFirstChild();
1089       AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
1090       AssignmentNode an=new AssignmentNode(parseExpression(cn, md, isStaticContext, left),
1091                                            new LiteralNode("integer",new Integer(1)),op);
1092       an.setNumLine(pn.getLine());
1093       return an;
1094     } else if (isNode(pn,"literal")) {
1095       String literaltype=pn.getTerminal();
1096       ParseNode literalnode=pn.getChild(literaltype);
1097       Object literal_obj=literalnode.getLiteral();
1098       LiteralNode ln=new LiteralNode(literaltype, literal_obj);
1099       ln.setNumLine(pn.getLine());
1100       return ln;
1101     } else if (isNode(pn, "createobject")) {
1102       TypeDescriptor td = parseTypeDescriptor(pn);
1103
1104       Vector args = parseArgumentList(cn, md, isStaticContext, pn);
1105       boolean isglobal = pn.getChild("global") != null || pn.getChild("scratch") != null;
1106       String disjointId = null;
1107       if (pn.getChild("disjoint") != null) {
1108         disjointId = pn.getChild("disjoint").getTerminal();
1109       }
1110       ParseNode idChild = (pn.getChild( "id" ));
1111       ParseNode baseChild = (pn.getChild( "base" ));
1112       
1113       CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
1114       if( null != idChild && null != idChild.getFirstChild() ) {
1115         idChild = idChild.getFirstChild();
1116         //System.out.println( "\nThe object passed has this expression " + idChild.PPrint( 0, true ) );
1117         ExpressionNode en = parseExpression(cn, md, isStaticContext, idChild ); 
1118         //System.out.println( "\nThe object passed has this expression " + en.printNode( 0 ) );
1119         con.setSurroundingExpression( en );
1120       }
1121       else if( null != baseChild  && null != baseChild.getFirstChild()  ) {
1122         baseChild = baseChild.getFirstChild();
1123         //System.out.println( "\nThe object passed has this expression " + baseChild.PPrint( 0, true ) );
1124         ExpressionNode en = parseExpression(cn, md, isStaticContext, baseChild ); 
1125         //System.out.println( "\nThe object passed has this expression " + en.printNode( 0 ) );
1126         con.setSurroundingExpression( en );     
1127       }
1128       con.setNumLine(pn.getLine());
1129       for (int i = 0; i < args.size(); i++) {
1130         con.addArgument((ExpressionNode) args.get(i));
1131       }
1132       /* Could have flag set or tag added here */
1133       if (pn.getChild("flag_list") != null || pn.getChild("tag_list") != null) {
1134         FlagEffects fe = new FlagEffects(null);
1135         if (pn.getChild("flag_list") != null)
1136           parseFlagEffect(fe, pn.getChild("flag_list"));
1137
1138         if (pn.getChild("tag_list") != null)
1139           parseTagEffect(fe, pn.getChild("tag_list"));
1140         con.addFlagEffects(fe);
1141       }
1142
1143       return con;
1144     } else if (isNode(pn,"createobjectcls")) {
1145       TypeDescriptor td=parseTypeDescriptor(pn);
1146       innerCount++;
1147       ClassDescriptor cnnew=new ClassDescriptor(packageName,td.getSymbol()+"$"+innerCount, false);
1148       pushChainMaps();
1149       cnnew.setImports(mandatoryImports, multiimports);
1150       cnnew.setSuper(td.getSymbol());
1151       cnnew.setInline();
1152       // the inline anonymous class does not have modifiers, it cannot be static 
1153       // it is always implicit final
1154       cnnew.setModifiers(new Modifiers(Modifiers.FINAL));
1155       cnnew.setAsInnerClass();
1156       cnnew.setSurroundingClass(cn.getSymbol());
1157       cnnew.setSurrounding(cn);
1158       cn.addInnerClass(cnnew);
1159       // Note that if the inner class is declared in a static context, it does NOT 
1160       // have lexically enclosing instances.
1161       if((null!=md)&&(md.isStatic()||md.isStaticBlock())) {
1162         cnnew.setSurroundingBlock(md);
1163         cnnew.setInStaticContext();
1164       } else if (isStaticContext) {
1165         cnnew.setInStaticContext();
1166       }
1167       parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
1168       boolean hasConstructor = false;
1169       for(Iterator method_it=cnnew.getMethods(); method_it.hasNext(); ) {
1170           MethodDescriptor cmd=(MethodDescriptor)method_it.next();
1171           hasConstructor |= cmd.isConstructor();
1172       }
1173       if(hasConstructor) {
1174           // anonymous class should not have explicit constructors
1175           throw new Error("Error! Anonymous class " + cnnew.getSymbol() + " in " + cn.getSymbol() + " has explicit constructors!");
1176       } else if(!cnnew.isEnum()) {
1177           // add a default constructor for this class
1178           MethodDescriptor cmd = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL),cnnew.getSymbol(), false);
1179           BlockNode bn=new BlockNode();
1180           state.addTreeCode(cmd,bn);
1181           cmd.setDefaultConstructor();
1182           cnnew.addMethod(cmd);
1183       }
1184       TypeDescriptor tdnew=state.getTypeDescriptor(cnnew.getSymbol());
1185
1186       Vector args=parseArgumentList(cn, md, isStaticContext, pn);
1187       ParseNode idChild = pn.getChild( "id" );
1188       ParseNode baseChild = pn.getChild( "base" );
1189       //System.out.println("\n to print idchild and basechild for ");
1190       /*if( null != idChild )
1191         System.out.println( "\n trying to create an inner class and the id child passed is "  + idChild.PPrint( 0, true ) );
1192       if( null != baseChild )
1193         System.out.println( "\n trying to create an inner class and the base child passed is "  + baseChild.PPrint( 0, true ) );*/
1194       CreateObjectNode con=new CreateObjectNode(tdnew, false, null);
1195       con.setNumLine(pn.getLine());
1196       for(int i=0; i<args.size(); i++) {
1197         con.addArgument((ExpressionNode)args.get(i));
1198       }
1199       popChainMaps();
1200
1201       addClass2State(cnnew);
1202
1203       return con;
1204     } else if (isNode(pn,"createarray")) {
1205       //System.out.println(pn.PPrint(3,true));
1206       boolean isglobal=pn.getChild("global")!=null||
1207                         pn.getChild("scratch")!=null;
1208       String disjointId=null;
1209       if( pn.getChild("disjoint") != null) {
1210         disjointId = pn.getChild("disjoint").getTerminal();
1211       }
1212       TypeDescriptor td=parseTypeDescriptor(pn);
1213       Vector args=parseDimExprs(cn, md, isStaticContext, pn);
1214       int num=0;
1215       if (pn.getChild("dims_opt").getLiteral()!=null)
1216         num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1217       for(int i=0; i<(args.size()+num); i++)
1218         td=td.makeArray(state);
1219       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
1220       con.setNumLine(pn.getLine());
1221       for(int i=0; i<args.size(); i++) {
1222         con.addArgument((ExpressionNode)args.get(i));
1223       }
1224       return con;
1225     }
1226     if (isNode(pn,"createarray2")) {
1227       TypeDescriptor td=parseTypeDescriptor(pn);
1228       int num=0;
1229       if (pn.getChild("dims_opt").getLiteral()!=null)
1230         num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1231       for(int i=0; i<num; i++)
1232         td=td.makeArray(state);
1233       CreateObjectNode con=new CreateObjectNode(td, false, null);
1234       con.setNumLine(pn.getLine());
1235       ParseNode ipn = pn.getChild("initializer");
1236       Vector initializers=parseVariableInitializerList(cn, md, isStaticContext, ipn);
1237       ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
1238       ain.setNumLine(pn.getLine());
1239       con.addArrayInitializer(ain);
1240       return con;
1241     } else if (isNode(pn,"name")) {
1242       NameDescriptor nd=parseName(pn);
1243       NameNode nn=new NameNode(nd);
1244       nn.setNumLine(pn.getLine());
1245       return nn;
1246     } else if (isNode(pn,"this")) {
1247       NameDescriptor nd=new NameDescriptor("this");
1248       NameNode nn=new NameNode(nd);
1249       nn.setNumLine(pn.getLine());
1250       return nn;
1251     } else if (isNode(pn,"parentclass")) {
1252       NameDescriptor nd=new NameDescriptor(pn.getChild("name").getFirstChild().getFirstChild().getTerminal());
1253       NameNode nn=new NameNode(nd);
1254       nn.setNumLine(pn.getLine());
1255         //because inner classes pass right thru......
1256       FieldAccessNode fan=new FieldAccessNode(nn,"this");
1257       fan.setNumLine(pn.getLine());
1258       return fan;
1259     } else if (isNode(pn,"isavailable")) {
1260       NameDescriptor nd=new NameDescriptor(pn.getTerminal());
1261       NameNode nn=new NameNode(nd);
1262       nn.setNumLine(pn.getLine());
1263       return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
1264     } else if (isNode(pn,"methodinvoke1")) {
1265       NameDescriptor nd=parseName(pn.getChild("name"));
1266       Vector args=parseArgumentList(cn, md, isStaticContext, pn);
1267       MethodInvokeNode min=new MethodInvokeNode(nd);
1268       min.setNumLine(pn.getLine());
1269       for(int i=0; i<args.size(); i++) {
1270         min.addArgument((ExpressionNode)args.get(i));
1271       }
1272       return min;
1273     } else if (isNode(pn,"methodinvoke2")) {
1274       String methodid=pn.getChild("id").getTerminal();
1275       ExpressionNode exp=parseExpression(cn, md, isStaticContext, pn.getChild("base").getFirstChild());
1276       Vector args=parseArgumentList(cn, md, isStaticContext, pn);
1277       MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
1278       min.setNumLine(pn.getLine());
1279       for(int i=0; i<args.size(); i++) {
1280         min.addArgument((ExpressionNode)args.get(i));
1281       }
1282       return min;
1283     } else if (isNode(pn,"fieldaccess")) {
1284       ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getChild("base").getFirstChild());
1285       String fieldname=pn.getChild("field").getTerminal();
1286
1287       FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1288       fan.setNumLine(pn.getLine());
1289       return fan;
1290     } else if (isNode(pn,"superfieldaccess")) {
1291         ExpressionNode en=new NameNode(new NameDescriptor("super"));
1292         String fieldname=pn.getChild("field").getTerminal();
1293
1294         FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1295         fan.setNumLine(pn.getLine());
1296         return fan;
1297     } else if (isNode(pn,"supernamefieldaccess")) {
1298         ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getChild("base").getFirstChild());
1299         ExpressionNode exp = new FieldAccessNode(en, "super");
1300         exp.setNumLine(pn.getLine());
1301         String fieldname=pn.getChild("field").getTerminal();
1302
1303         FieldAccessNode fan=new FieldAccessNode(exp,fieldname);
1304         fan.setNumLine(pn.getLine());
1305         return fan;
1306     } else if (isNode(pn,"arrayaccess")) {
1307       ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getChild("base").getFirstChild());
1308       ExpressionNode index=parseExpression(cn, md, isStaticContext, pn.getChild("index").getFirstChild());
1309       ArrayAccessNode aan=new ArrayAccessNode(en,index);
1310       aan.setNumLine(pn.getLine());
1311       return aan;
1312     } else if (isNode(pn,"cast1")) {
1313       try {
1314         CastNode castn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(cn, md, isStaticContext, pn.getChild("exp").getFirstChild()));
1315         castn.setNumLine(pn.getLine());
1316         return castn;
1317       } catch (Exception e) {
1318         System.out.println(pn.PPrint(1,true));
1319         e.printStackTrace();
1320         throw new Error();
1321       }
1322     } else if (isNode(pn,"cast2")) {
1323       CastNode castn=new CastNode(parseExpression(cn, md, isStaticContext, pn.getChild("type").getFirstChild()),parseExpression(cn, md, isStaticContext, pn.getChild("exp").getFirstChild()));
1324       castn.setNumLine(pn.getLine());
1325       return castn;
1326     } else if (isNode(pn, "getoffset")) {
1327       TypeDescriptor td=parseTypeDescriptor(pn);
1328       String fieldname = pn.getChild("field").getTerminal();
1329       //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ "  fieldname= " + fieldname);
1330       return new OffsetNode(td, fieldname);
1331     } else if (isNode(pn, "tert")) {
1332
1333       TertiaryNode tn=new TertiaryNode(parseExpression(cn, md, isStaticContext, pn.getChild("cond").getFirstChild()),
1334                                        parseExpression(cn, md, isStaticContext, pn.getChild("trueexpr").getFirstChild()),
1335                                        parseExpression(cn, md, isStaticContext, pn.getChild("falseexpr").getFirstChild()) );
1336       tn.setNumLine(pn.getLine());
1337
1338       return tn;
1339     } else if (isNode(pn, "instanceof")) {
1340       ExpressionNode exp=parseExpression(cn, md, isStaticContext, pn.getChild("exp").getFirstChild());
1341       TypeDescriptor t=parseTypeDescriptor(pn);
1342       InstanceOfNode ion=new InstanceOfNode(exp,t);
1343       ion.setNumLine(pn.getLine());
1344       return ion;
1345     } else if (isNode(pn, "array_initializer")) {
1346       Vector initializers=parseVariableInitializerList(cn, md, isStaticContext, pn);
1347       return new ArrayInitializerNode(initializers);
1348     } else if (isNode(pn, "class_type")) {
1349       TypeDescriptor td=parseTypeDescriptor(pn);
1350       ClassTypeNode ctn=new ClassTypeNode(td);
1351       ctn.setNumLine(pn.getLine());
1352       return ctn;
1353     } else if (isNode(pn, "empty")) {
1354       return null;
1355     } else {
1356       System.out.println("---------------------");
1357       System.out.println(pn.PPrint(3,true));
1358       throw new Error();
1359     }
1360   }
1361
1362   private Vector parseDimExprs(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1363     Vector arglist=new Vector();
1364     ParseNode an=pn.getChild("dim_exprs");
1365     if (an==null)       /* No argument list */
1366       return arglist;
1367     ParseNodeVector anv=an.getChildren();
1368     for(int i=0; i<anv.size(); i++) {
1369       arglist.add(parseExpression(cn, md, isStaticContext, anv.elementAt(i)));
1370     }
1371     return arglist;
1372   }
1373
1374   private Vector parseArgumentList(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1375     Vector arglist=new Vector();
1376     ParseNode an=pn.getChild("argument_list");
1377     if (an==null)       /* No argument list */
1378       return arglist;
1379     ParseNodeVector anv=an.getChildren();
1380     for(int i=0; i<anv.size(); i++) {
1381       arglist.add(parseExpression(cn, md, isStaticContext, anv.elementAt(i)));
1382     }
1383     return arglist;
1384   }
1385
1386   private Vector[] parseConsArgumentList(ParseNode pn) {
1387     Vector arglist=new Vector();
1388     Vector varlist=new Vector();
1389     ParseNode an=pn.getChild("cons_argument_list");
1390     if (an==null)       /* No argument list */
1391       return new Vector[] {varlist, arglist};
1392     ParseNodeVector anv=an.getChildren();
1393     for(int i=0; i<anv.size(); i++) {
1394       ParseNode cpn=anv.elementAt(i);
1395       ParseNode var=cpn.getChild("var");
1396       ParseNode exp=cpn.getChild("exp").getFirstChild();
1397       varlist.add(var.getTerminal());
1398       arglist.add(parseExpression(null, null, false, exp));
1399     }
1400     return new Vector[] {varlist, arglist};
1401   }
1402
1403   private Vector parseVariableInitializerList(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1404     Vector varInitList=new Vector();
1405     ParseNode vin=pn.getChild("var_init_list");
1406     if (vin==null)       /* No argument list */
1407       return varInitList;
1408     ParseNodeVector vinv=vin.getChildren();
1409     for(int i=0; i<vinv.size(); i++) {
1410       varInitList.add(parseExpression(cn, md, isStaticContext, vinv.elementAt(i)));
1411     }
1412     return varInitList;
1413   }
1414
1415   private ExpressionNode parseAssignmentExpression(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1416     AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1417     ParseNodeVector pnv=pn.getChild("args").getChildren();
1418
1419     AssignmentNode an=new AssignmentNode(parseExpression(cn, md, isStaticContext, pnv.elementAt(0)),parseExpression(cn, md, isStaticContext, pnv.elementAt(1)),ao);
1420     an.setNumLine(pn.getLine());
1421     return an;
1422   }
1423
1424
1425   private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1426     ParseNode headern=pn.getChild("method_header");
1427     ParseNode bodyn=pn.getChild("body");
1428     MethodDescriptor md=parseMethodHeader(headern);
1429     try {
1430       BlockNode bn=parseBlock(cn, md, md.isStatic()||md.isStaticBlock(), bodyn);
1431       bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1432       cn.addMethod(md);
1433       state.addTreeCode(md,bn);
1434
1435       // this is a hack for investigating new language features
1436       // at the AST level, someday should evolve into a nice compiler
1437       // option *wink*
1438       //if( cn.getSymbol().equals( ***put a class in here like:     "Test" ) &&
1439       //    md.getSymbol().equals( ***put your method in here like: "main" )
1440       //) {
1441       //  bn.setStyle( BlockNode.NORMAL );
1442       //  System.out.println( bn.printNode( 0 ) );
1443       //}
1444
1445     } catch (Exception e) {
1446       System.out.println("Error with method:"+md.getSymbol());
1447       e.printStackTrace();
1448       throw new Error();
1449     } catch (Error e) {
1450       System.out.println("Error with method:"+md.getSymbol());
1451       e.printStackTrace();
1452       throw new Error();
1453     }
1454   }
1455
1456   private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1457     ParseNode mn=pn.getChild("modifiers");
1458     Modifiers m=parseModifiersList(mn);
1459     ParseNode cdecl=pn.getChild("constructor_declarator");
1460     boolean isglobal=cdecl.getChild("global")!=null;
1461     String name=resolveName(cn.getSymbol());
1462     MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1463     ParseNode paramnode=cdecl.getChild("parameters");
1464     parseParameterList(md,paramnode);
1465     ParseNode bodyn0=pn.getChild("body");
1466     ParseNode bodyn=bodyn0.getChild("constructor_body");
1467     cn.addMethod(md);
1468     BlockNode bn=null;
1469     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1470       bn=parseBlock(cn, md, md.isStatic()||md.isStaticBlock(), bodyn);
1471     else
1472       bn=new BlockNode();
1473     if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1474       ParseNode sin=bodyn.getChild("superinvoke");
1475       NameDescriptor nd=new NameDescriptor("super");
1476       Vector args=parseArgumentList(cn, md, true, sin);
1477       MethodInvokeNode min=new MethodInvokeNode(nd);
1478       min.setNumLine(sin.getLine());
1479       for(int i=0; i<args.size(); i++) {
1480         min.addArgument((ExpressionNode)args.get(i));
1481       }
1482       BlockExpressionNode ben=new BlockExpressionNode(min);
1483       bn.addFirstBlockStatement(ben);
1484
1485     } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1486       ParseNode eci=bodyn.getChild("explconstrinv");
1487       NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1488       Vector args=parseArgumentList(cn, md, true, eci);
1489       MethodInvokeNode min=new MethodInvokeNode(nd);
1490       min.setNumLine(eci.getLine());
1491       for(int i=0; i<args.size(); i++) {
1492         min.addArgument((ExpressionNode)args.get(i));
1493       }
1494       BlockExpressionNode ben=new BlockExpressionNode(min);
1495       ben.setNumLine(eci.getLine());
1496       bn.addFirstBlockStatement(ben);
1497     }
1498     state.addTreeCode(md,bn);
1499   }
1500
1501   private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1502     // Each class maintains one MethodDecscriptor which combines all its
1503     // static blocks in their declaration order
1504     boolean isfirst = false;
1505     MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1506     if(md == null) {
1507       // the first static block for this class
1508       Modifiers m_i=new Modifiers();
1509       m_i.addModifier(Modifiers.STATIC);
1510       md = new MethodDescriptor(m_i, "staticblocks", false);
1511       md.setAsStaticBlock();
1512       isfirst = true;
1513     }
1514     ParseNode bodyn=pn.getChild("body");
1515     if(isfirst) {
1516       cn.addMethod(md);
1517     }
1518     cn.incStaticBlocks();
1519     BlockNode bn=null;
1520     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1521       bn=parseBlock(cn, md, true, bodyn);
1522     else
1523       bn=new BlockNode();
1524     if(isfirst) {
1525       state.addTreeCode(md,bn);
1526     } else {
1527       BlockNode obn = state.getMethodBody(md);
1528       for(int i = 0; i < bn.size(); i++) {
1529         BlockStatementNode bsn = bn.get(i);
1530         obn.addBlockStatement(bsn);
1531       }
1532       state.addTreeCode(md, obn);
1533       bn = null;
1534     }
1535   }
1536
1537   public BlockNode parseBlock(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1538     this.m_taskexitnum = 0;
1539     if (pn==null||isEmpty(pn.getTerminal()))
1540       return new BlockNode();
1541     ParseNode bsn=pn.getChild("block_statement_list");
1542     return parseBlockHelper(cn, md, isStaticContext, bsn);
1543   }
1544
1545   private BlockNode parseBlockHelper(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1546     ParseNodeVector pnv=pn.getChildren();
1547     BlockNode bn=new BlockNode();
1548     for(int i=0; i<pnv.size(); i++) {
1549       Vector bsv=parseBlockStatement(cn, md, isStaticContext, pnv.elementAt(i));
1550       for(int j=0; j<bsv.size(); j++) {
1551         bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1552       }
1553     }
1554     return bn;
1555   }
1556
1557   public BlockNode parseSingleBlock(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn, String label){
1558     BlockNode bn=new BlockNode();
1559     Vector bsv=parseBlockStatement(cn, md, isStaticContext, pn,label);
1560     for(int j=0; j<bsv.size(); j++) {
1561       bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1562     }
1563     bn.setStyle(BlockNode.NOBRACES);
1564     return bn;
1565   }
1566   
1567   public BlockNode parseSingleBlock(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1568     return parseSingleBlock(cn, md, isStaticContext, pn, null);
1569   }
1570
1571   public Vector parseSESEBlock(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, Vector parentbs, ParseNode pn) {
1572     ParseNodeVector pnv=pn.getChildren();
1573     Vector bv=new Vector();
1574     for(int i=0; i<pnv.size(); i++) {
1575       bv.addAll(parseBlockStatement(cn, md, isStaticContext, pnv.elementAt(i)));
1576     }
1577     return bv;
1578   }
1579   
1580   public Vector parseBlockStatement(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn){
1581     return parseBlockStatement(cn, md, isStaticContext, pn,null);
1582   }
1583
1584   public Vector parseBlockStatement(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn, String label) {
1585     Vector blockstatements=new Vector();
1586     if (isNode(pn,"tag_declaration")) {
1587       String name=pn.getChild("single").getTerminal();
1588       String type=pn.getChild("type").getTerminal();
1589
1590       TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1591       tdn.setNumLine(pn.getLine());
1592
1593       blockstatements.add(tdn);
1594     } else if (isNode(pn,"local_variable_declaration")) {
1595
1596       TypeDescriptor t=parseTypeDescriptor(pn);
1597       ParseNode vn=pn.getChild("variable_declarators_list");
1598       ParseNodeVector pnv=vn.getChildren();
1599       for(int i=0; i<pnv.size(); i++) {
1600         ParseNode vardecl=pnv.elementAt(i);
1601
1602
1603         ParseNode tmp=vardecl;
1604         TypeDescriptor arrayt=t;
1605
1606         while (tmp.getChild("single")==null) {
1607           arrayt=arrayt.makeArray(state);
1608           tmp=tmp.getChild("array");
1609         }
1610         String identifier=tmp.getChild("single").getTerminal();
1611
1612         ParseNode epn=vardecl.getChild("initializer");
1613
1614
1615         ExpressionNode en=null;
1616         if (epn!=null)
1617           en=parseExpression(cn, md, isStaticContext, epn.getFirstChild());
1618
1619         DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1620         dn.setNumLine(tmp.getLine());
1621         
1622         ParseNode mn=pn.getChild("modifiers");
1623         if(mn!=null) {
1624           // here, modifers parse node has the list of annotations
1625           Modifiers m=parseModifiersList(mn);
1626           assignAnnotationsToType(m, arrayt);
1627         }
1628
1629         blockstatements.add(dn);
1630       }
1631     } else if (isNode(pn,"nop")) {
1632       /* Do Nothing */
1633     } else if (isNode(pn,"expression")) {
1634       BlockExpressionNode ben=new BlockExpressionNode(parseExpression(cn, md, isStaticContext, pn.getFirstChild()));
1635       ben.setNumLine(pn.getLine());
1636       blockstatements.add(ben);
1637     } else if (isNode(pn,"ifstatement")) {
1638       IfStatementNode isn=new IfStatementNode(parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild()),
1639                                               parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild()),
1640                                               pn.getChild("else_statement")!=null?parseSingleBlock(cn, md, isStaticContext, pn.getChild("else_statement").getFirstChild()):null);
1641       isn.setNumLine(pn.getLine());
1642
1643       blockstatements.add(isn);
1644     } else if (isNode(pn,"switch_statement")) {
1645       // TODO add version for normal Java later
1646       SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild()),
1647                                                       parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild()));
1648       ssn.setNumLine(pn.getLine());
1649       blockstatements.add(ssn);
1650     } else if (isNode(pn,"switch_block_list")) {
1651       // TODO add version for normal Java later
1652       ParseNodeVector pnv=pn.getChildren();
1653       for(int i=0; i<pnv.size(); i++) {
1654         ParseNode sblockdecl=pnv.elementAt(i);
1655
1656         if(isNode(sblockdecl, "switch_block")) {
1657           ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1658           ParseNodeVector labelv=lpn.getChildren();
1659           Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1660           for(int j=0; j<labelv.size(); j++) {
1661             ParseNode labeldecl=labelv.elementAt(j);
1662             if(isNode(labeldecl, "switch_label")) {
1663               SwitchLabelNode sln=new SwitchLabelNode(parseExpression(cn, md, isStaticContext, labeldecl.getChild("constant_expression").getFirstChild()), false);
1664               sln.setNumLine(labeldecl.getLine());
1665               slv.addElement(sln);
1666             } else if(isNode(labeldecl, "default_switch_label")) {
1667               SwitchLabelNode sln=new SwitchLabelNode(null, true);
1668               sln.setNumLine(labeldecl.getLine());
1669               slv.addElement(sln);
1670             }
1671           }
1672
1673           SwitchBlockNode sbn=new SwitchBlockNode(slv,
1674                                                   parseSingleBlock(cn, md, isStaticContext, sblockdecl.getChild("switch_statements").getFirstChild()));
1675           sbn.setNumLine(sblockdecl.getLine());
1676
1677           blockstatements.add(sbn);
1678
1679         }
1680       }
1681     } else if (isNode(pn, "trycatchstatement")) {
1682       // TODO add version for normal Java later
1683       // Do not fully support exceptions now. Only make sure that if there are no
1684       // exceptions thrown, the execution is right
1685       ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1686       BlockNode bn=parseBlockHelper(cn, md, isStaticContext, tpn);
1687       blockstatements.add(new SubBlockNode(bn));
1688
1689       ParseNode fbk = pn.getChild("finallyblock");
1690       if(fbk != null) {
1691         ParseNode fpn = fbk.getFirstChild();
1692         BlockNode fbn=parseBlockHelper(cn, md, isStaticContext, fpn);
1693         blockstatements.add(new SubBlockNode(fbn));
1694       }
1695     } else if (isNode(pn, "throwstatement")) {
1696       // TODO Simply return here
1697       //blockstatements.add(new ReturnNode());
1698     } else if (isNode(pn,"taskexit")) {
1699       Vector vfe=null;
1700       if (pn.getChild("flag_effects_list")!=null)
1701         vfe=parseFlags(pn.getChild("flag_effects_list"));
1702       Vector ccs=null;
1703       if (pn.getChild("cons_checks")!=null)
1704         ccs=parseChecks(pn.getChild("cons_checks"));
1705       TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1706       ten.setNumLine(pn.getLine());
1707       blockstatements.add(ten);
1708     } else if (isNode(pn,"atomic")) {
1709       BlockNode bn=parseBlockHelper(cn, md, isStaticContext, pn);
1710       AtomicNode an=new AtomicNode(bn);
1711       an.setNumLine(pn.getLine());
1712       blockstatements.add(an);
1713     } else if (isNode(pn,"synchronized")) {
1714       BlockNode bn=parseBlockHelper(cn, md, isStaticContext, pn.getChild("block"));
1715       ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getChild("expr").getFirstChild());
1716       SynchronizedNode sn=new SynchronizedNode(en, bn);
1717       sn.setNumLine(pn.getLine());
1718       blockstatements.add(sn);
1719     } else if (isNode(pn,"return")) {
1720       if (isEmpty(pn.getTerminal()))
1721         blockstatements.add(new ReturnNode());
1722       else {
1723         ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getFirstChild());
1724         ReturnNode rn=new ReturnNode(en);
1725         rn.setNumLine(pn.getLine());
1726         blockstatements.add(rn);
1727       }
1728     } else if (isNode(pn,"block_statement_list")) {
1729       BlockNode bn=parseBlockHelper(cn, md, isStaticContext, pn);
1730       blockstatements.add(new SubBlockNode(bn));
1731     } else if (isNode(pn,"empty")) {
1732       /* nop */
1733     } else if (isNode(pn,"statement_expression_list")) {
1734       ParseNodeVector pnv=pn.getChildren();
1735       BlockNode bn=new BlockNode();
1736       for(int i=0; i<pnv.size(); i++) {
1737         ExpressionNode en=parseExpression(cn, md, isStaticContext, pnv.elementAt(i));
1738         blockstatements.add(new BlockExpressionNode(en));
1739       }
1740       bn.setStyle(BlockNode.EXPRLIST);
1741     } else if (isNode(pn,"forstatement")) {
1742       BlockNode init=parseSingleBlock(cn, md, isStaticContext, pn.getChild("initializer").getFirstChild());
1743       BlockNode update=parseSingleBlock(cn, md, isStaticContext, pn.getChild("update").getFirstChild());
1744       ExpressionNode condition=parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild());
1745       BlockNode body=parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild());
1746       if(condition == null) {
1747         // no condition clause, make a 'true' expression as the condition
1748         condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1749       }
1750       LoopNode ln=new LoopNode(init,condition,update,body,label);
1751       ln.setNumLine(pn.getLine());
1752       blockstatements.add(ln);
1753     } else if (isNode(pn,"whilestatement")) {
1754       ExpressionNode condition=parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild());
1755       BlockNode body=parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild());
1756       if(condition == null) {
1757         // no condition clause, make a 'true' expression as the condition
1758         condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1759       }
1760       blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP,label));
1761     } else if (isNode(pn,"dowhilestatement")) {
1762       ExpressionNode condition=parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild());
1763       BlockNode body=parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild());
1764       if(condition == null) {
1765         // no condition clause, make a 'true' expression as the condition
1766         condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1767       }
1768       blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP,label));
1769     } else if (isNode(pn,"sese")) {
1770       ParseNode pnID=pn.getChild("identifier");
1771       String stID=null;
1772       if( pnID != null ) {
1773         stID=pnID.getFirstChild().getTerminal();
1774       }
1775       SESENode start=new SESENode(stID);
1776       start.setNumLine(pn.getLine());
1777       SESENode end  =new SESENode(stID);
1778       start.setEnd(end);
1779       end.setStart(start);
1780       blockstatements.add(start);
1781       blockstatements.addAll(parseSESEBlock(cn, md, isStaticContext, blockstatements,pn.getChild("body").getFirstChild()));
1782       blockstatements.add(end);
1783     } else if (isNode(pn,"continue")) {
1784       ContinueBreakNode cbn=new ContinueBreakNode(false);
1785       cbn.setNumLine(pn.getLine());
1786       blockstatements.add(cbn);
1787     } else if (isNode(pn,"break")) {
1788       ContinueBreakNode cbn=new ContinueBreakNode(true);
1789       cbn.setNumLine(pn.getLine());
1790       blockstatements.add(cbn);
1791       ParseNode idopt_pn=pn.getChild("identifier_opt");
1792       ParseNode name_pn=idopt_pn.getChild("name");
1793       // name_pn.getTerminal() gives you the label
1794
1795     } else if (isNode(pn,"genreach")) {
1796       String graphName = pn.getChild("graphName").getTerminal();
1797       blockstatements.add(new GenReachNode(graphName) );
1798
1799     } else if (isNode(pn,"gen_def_reach")) {
1800       String outputName = pn.getChild("outputName").getTerminal();
1801       blockstatements.add(new GenDefReachNode(outputName) );
1802
1803     } else if(isNode(pn,"labeledstatement")) {
1804       String labeledstatement = pn.getChild("name").getTerminal();
1805       BlockNode bn=parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild(),labeledstatement);
1806       blockstatements.add(new SubBlockNode(bn));
1807     } else {
1808       System.out.println("---------------");
1809       System.out.println(pn.PPrint(3,true));
1810       throw new Error();
1811     }
1812     return blockstatements;
1813   }
1814
1815   public MethodDescriptor parseMethodHeader(ParseNode pn) {
1816     ParseNode mn=pn.getChild("modifiers");
1817     Modifiers m=parseModifiersList(mn);
1818
1819     ParseNode tn=pn.getChild("returntype");
1820     TypeDescriptor returntype;
1821     if (tn!=null)
1822       returntype=parseTypeDescriptor(tn);
1823     else
1824       returntype=new TypeDescriptor(TypeDescriptor.VOID);
1825
1826     ParseNode pmd=pn.getChild("method_declarator");
1827     String name=pmd.getChild("name").getTerminal();
1828     MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1829
1830     ParseNode paramnode=pmd.getChild("parameters");
1831     parseParameterList(md,paramnode);
1832     return md;
1833   }
1834
1835   public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1836     ParseNode paramlist=pn.getChild("formal_parameter_list");
1837     if (paramlist==null)
1838       return;
1839     ParseNodeVector pnv=paramlist.getChildren();
1840     for(int i=0; i<pnv.size(); i++) {
1841       ParseNode paramn=pnv.elementAt(i);
1842
1843       if (isNode(paramn, "tag_parameter")) {
1844         String paramname=paramn.getChild("single").getTerminal();
1845         TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1846         md.addTagParameter(type, paramname);
1847       } else  {
1848
1849         TypeDescriptor type=parseTypeDescriptor(paramn);
1850
1851         ParseNode tmp=paramn;
1852         while (tmp.getChild("single")==null) {
1853           type=type.makeArray(state);
1854           tmp=tmp.getChild("array");
1855         }
1856         String paramname=tmp.getChild("single").getTerminal();
1857
1858         md.addParameter(type, paramname);
1859         if(isNode(paramn, "annotation_parameter")) {
1860           ParseNode listnode=paramn.getChild("annotation_list");
1861           parseParameterAnnotation(listnode,type);
1862         }
1863
1864       }
1865     }
1866   }
1867
1868   public Modifiers parseModifiersList(ParseNode pn) {
1869     Modifiers m=new Modifiers();
1870     ParseNode modlist=pn.getChild("modifier_list");
1871     if (modlist!=null) {
1872       ParseNodeVector pnv=modlist.getChildren();
1873       for(int i=0; i<pnv.size(); i++) {
1874         ParseNode modn=pnv.elementAt(i);
1875         if (isNode(modn,"public"))
1876           m.addModifier(Modifiers.PUBLIC);
1877         else if (isNode(modn,"protected"))
1878           m.addModifier(Modifiers.PROTECTED);
1879         else if (isNode(modn,"private"))
1880           m.addModifier(Modifiers.PRIVATE);
1881         else if (isNode(modn,"static"))
1882           m.addModifier(Modifiers.STATIC);
1883         else if (isNode(modn,"final"))
1884           m.addModifier(Modifiers.FINAL);
1885         else if (isNode(modn,"native"))
1886           m.addModifier(Modifiers.NATIVE);
1887         else if (isNode(modn,"synchronized"))
1888           m.addModifier(Modifiers.SYNCHRONIZED);
1889         else if (isNode(modn,"atomic"))
1890           m.addModifier(Modifiers.ATOMIC);
1891         else if (isNode(modn,"abstract"))
1892           m.addModifier(Modifiers.ABSTRACT);
1893         else if (isNode(modn,"volatile"))
1894           m.addModifier(Modifiers.VOLATILE);
1895         else if (isNode(modn,"transient"))
1896           m.addModifier(Modifiers.TRANSIENT);
1897         else if(isNode(modn,"annotation_list"))
1898           parseAnnotationList(modn,m);
1899         else {
1900           throw new Error("Unrecognized Modifier:"+modn.getLabel());
1901         }
1902       }
1903     }
1904     return m;
1905   }
1906
1907   private void parseAnnotationList(ParseNode pn, Modifiers m) {
1908     ParseNodeVector pnv = pn.getChildren();
1909     for (int i = 0; i < pnv.size(); i++) {
1910       ParseNode body_list = pnv.elementAt(i);
1911       if (isNode(body_list, "annotation_body")) {
1912         ParseNode body_node = body_list.getFirstChild();
1913         if (isNode(body_node, "marker_annotation")) {
1914           m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1915         } else if (isNode(body_node, "single_annotation")) {
1916           m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1917                                                    body_node.getChild("element_value").getTerminal()));
1918         } else if (isNode(body_node, "normal_annotation")) {
1919           throw new Error("Annotation with multiple data members is not supported yet.");
1920         }
1921       }
1922     }
1923   }
1924
1925   private void parseParameterAnnotation(ParseNode pn,TypeDescriptor type) {
1926     ParseNodeVector pnv = pn.getChildren();
1927     for (int i = 0; i < pnv.size(); i++) {
1928       ParseNode body_list = pnv.elementAt(i);
1929       if (isNode(body_list, "annotation_body")) {
1930         ParseNode body_node = body_list.getFirstChild();
1931         if (isNode(body_node, "marker_annotation")) {
1932           type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1933         } else if (isNode(body_node, "single_annotation")) {
1934           type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1935                                                    body_node.getChild("element_value").getTerminal()));
1936         } else if (isNode(body_node, "normal_annotation")) {
1937           throw new Error("Annotation with multiple data members is not supported yet.");
1938         }
1939       }
1940     }
1941   }
1942
1943   private boolean isNode(ParseNode pn, String label) {
1944     if (pn.getLabel().equals(label))
1945       return true;
1946     else return false;
1947   }
1948
1949   private static boolean isEmpty(ParseNode pn) {
1950     if (pn.getLabel().equals("empty"))
1951       return true;
1952     else
1953       return false;
1954   }
1955
1956   private static boolean isEmpty(String s) {
1957     if (s.equals("empty"))
1958       return true;
1959     else
1960       return false;
1961   }
1962
1963   /** Throw an exception if something is unexpected */
1964   private void check(ParseNode pn, String label) {
1965     if (pn == null) {
1966       throw new Error(pn+ "IE: Expected '" + label + "', got null");
1967     }
1968     if (!pn.getLabel().equals(label)) {
1969       throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
1970     }
1971   }
1972 }