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