a12dcf0a36aa0595d9c70d0598a4969b12f15c0b
[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, md, md.isStatic()||md.isStaticBlock(), 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, null, false, 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()||icd.getInStaticContext()) {
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                 if(icd.isStatic() || icd.getInStaticContext()) {
687                   continue;
688                 }
689                 parseFieldDecl( icd, theNode );         
690                 /*if( true ) {
691                         SymbolTable fieldTable = icd.getFieldTable();
692                         //System.out.println( fieldTable.toString() );
693                 }*/
694                 icd.setInnerDepth( depth + 1 );
695                 addOuterClassReferences( icd, depth + 1 );      
696         }
697 }
698
699   private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
700     ParseNode decls=pn.getChild("class_body_declaration_list");
701     if (decls!=null) {
702       ParseNodeVector pnv=decls.getChildren();
703       for(int i=0; i<pnv.size(); i++) {
704         ParseNode decl=pnv.elementAt(i);
705         if (isNode(decl,"member")) {
706           parseClassMember(cn,decl);
707         } else if (isNode(decl,"constructor")) {
708           parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
709         } else if (isNode(decl, "static_block")) {
710           parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
711         } else if (isNode(decl,"block")) {
712         } else throw new Error();
713       }
714     }
715   }
716
717   private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
718     ParseNode fieldnode=pn.getChild("field");
719     if (fieldnode!=null) {
720       //System.out.println( pn.PPrint( 0, true ) );
721       parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
722       return;
723     }
724     ParseNode methodnode=pn.getChild("method");
725     if (methodnode!=null) {
726       parseMethodDecl(cn,methodnode.getChild("method_declaration"));
727       return;
728     }
729     ParseNode innerclassnode=pn.getChild("inner_class_declaration");
730     if (innerclassnode!=null) {
731       parseInnerClassDecl(cn,innerclassnode);
732       return;
733     }
734      ParseNode innerinterfacenode=pn.getChild("interface_declaration");
735     if (innerinterfacenode!=null) {
736       parseInterfaceDecl(innerinterfacenode, cn);
737       return;
738     }
739
740     ParseNode enumnode=pn.getChild("enum_declaration");
741     if (enumnode!=null) {
742       ClassDescriptor ecn=parseEnumDecl(cn,enumnode);
743       return;
744     }
745     ParseNode flagnode=pn.getChild("flag");
746     if (flagnode!=null) {
747       parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
748       return;
749     }
750     // in case there are empty node
751     ParseNode emptynode=pn.getChild("empty");
752     if(emptynode != null) {
753       return;
754     }
755     System.out.println("Unrecognized node:"+pn.PPrint(2,true));
756     throw new Error();
757   }
758
759 //10/9/2011 changed this function to enable creation of default constructor for inner classes.
760 //the change was refactoring this function with the corresponding version for normal classes. sganapat
761   private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
762     String basename=pn.getChild("name").getTerminal();
763     String classname=cn.getClassName()+"$"+basename;
764
765     if (cn.getPackage()==null)
766       cn.getSingleImportMappings().put(basename,classname);
767     else
768       cn.getSingleImportMappings().put(basename,cn.getPackage()+"."+classname);
769     
770     ClassDescriptor icn=new ClassDescriptor(cn.getPackage(), classname, false);
771     pushChainMaps();
772     icn.setImports(mandatoryImports, multiimports);
773     icn.setSurroundingClass(cn.getSymbol());
774     icn.setSurrounding(cn);
775     cn.addInnerClass(icn);
776
777      if (!isEmpty(pn.getChild("super").getTerminal())) {
778       /* parse superclass name */
779       ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
780       NameDescriptor nd=parseClassName(snn);
781       icn.setSuper(nd.toString());
782     } else {
783       if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
784             icn.getSymbol().equals(TypeUtil.TagClass)))
785         icn.setSuper(TypeUtil.ObjectClass);
786     }
787     // check inherited interfaces
788     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
789       /* parse inherited interface name */
790       ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
791       ParseNodeVector pnv=snlist.getChildren();
792       for(int i=0; i<pnv.size(); i++) {
793         ParseNode decl=pnv.elementAt(i);
794         if (isNode(decl,"type")) {
795           NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
796           icn.addSuperInterface(nd.toString());
797         }
798       }
799     }
800     icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
801
802    if (!icn.isStatic())
803      icn.setAsInnerClass();
804
805     parseClassBody(icn, pn.getChild("classbody"));
806
807     boolean hasConstructor = false;
808     for(Iterator method_it=icn.getMethods(); method_it.hasNext(); ) {
809       MethodDescriptor md=(MethodDescriptor)method_it.next();
810       hasConstructor |= md.isConstructor();
811     }
812 //sganapat adding change to allow proper construction of inner class objects
813     if((!hasConstructor) && (!icn.isEnum())) {
814       // add a default constructor for this class
815       MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
816                                                  icn.getSymbol(), false);
817       BlockNode bn=new BlockNode();
818       state.addTreeCode(md,bn);
819       md.setDefaultConstructor();
820       icn.addMethod(md);
821    }
822     popChainMaps();
823
824      if (analyzeset != null)
825       analyzeset.add(icn);
826     icn.setSourceFileName(currsourcefile);
827     state.addClass(icn);
828
829     return icn;
830   }
831
832   private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
833     ParseNode tn=pn.getChild("type");
834     String type_st=tn.getTerminal();
835     if(type_st.equals("byte")) {
836       return state.getTypeDescriptor(TypeDescriptor.BYTE);
837     } else if(type_st.equals("short")) {
838       return state.getTypeDescriptor(TypeDescriptor.SHORT);
839     } else if(type_st.equals("boolean")) {
840       return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
841     } else if(type_st.equals("int")) {
842       return state.getTypeDescriptor(TypeDescriptor.INT);
843     } else if(type_st.equals("long")) {
844       return state.getTypeDescriptor(TypeDescriptor.LONG);
845     } else if(type_st.equals("char")) {
846       return state.getTypeDescriptor(TypeDescriptor.CHAR);
847     } else if(type_st.equals("float")) {
848       return state.getTypeDescriptor(TypeDescriptor.FLOAT);
849     } else if(type_st.equals("double")) {
850       return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
851     } else if(type_st.equals("class")) {
852       ParseNode nn=tn.getChild("class");
853       return state.getTypeDescriptor(parseClassName(nn.getChild("name")));
854     } else if(type_st.equals("array")) {
855       ParseNode nn=tn.getChild("array");
856       TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
857       Integer numdims=(Integer)nn.getChild("dims").getLiteral();
858       for(int i=0; i<numdims.intValue(); i++)
859         td=td.makeArray(state);
860       return td;
861     } else {
862       System.out.println(pn.PPrint(2, true));
863       throw new Error();
864     }
865   }
866
867   //Needed to separate out top level call since if a base exists,
868   //we do not want to apply our resolveName function (i.e. deal with imports)
869   //otherwise, if base == null, we do just want to resolve name.
870   private NameDescriptor parseClassName(ParseNode nn) {
871     
872
873     ParseNode base=nn.getChild("base");
874     ParseNode id=nn.getChild("identifier");
875     String classname = id.getTerminal();
876     if (base==null) {
877       return new NameDescriptor(resolveName(classname));
878     }
879     return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
880   }
881
882   private NameDescriptor parseClassNameRecursive(ParseNode nn) {
883     ParseNode base=nn.getChild("base");
884     ParseNode id=nn.getChild("identifier");
885     String classname = id.getTerminal();
886     if (base==null) {
887       return new NameDescriptor(classname);
888     }
889     return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
890   }
891
892   //This will get the mapping of a terminal class name
893   //to a canonical classname (with imports/package locations in them)
894   private String resolveName(String terminal) {
895     
896     if(mandatoryImports.containsKey(terminal)) {
897       return (String) mandatoryImports.get(terminal);
898     } else {
899       if(multiimports.containsKey(terminal)) {
900         //Test for error
901         Object o = multiimports.get(terminal);
902         if(o instanceof Error) {
903           throw new Error("Class " + terminal + " is ambiguous. Cause: more than 1 package import contain the same class.");
904         } else {
905           //At this point, if we found a unique class
906           //we can treat it as a single, mandatory import.
907           mandatoryImports.put(terminal, o);
908           return (String) o;
909         }
910       }
911     }
912
913     return terminal;
914   }
915
916   //only function difference between this and parseName() is that this
917   //does not look for a import mapping.
918   private NameDescriptor parseName(ParseNode nn) {
919     ParseNode base=nn.getChild("base");
920     ParseNode id=nn.getChild("identifier");
921     if (base==null) {
922       return new NameDescriptor(id.getTerminal());
923     }
924     return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
925   }
926
927   private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
928     String name=pn.getChild("name").getTerminal();
929     FlagDescriptor flag=new FlagDescriptor(name);
930     if (pn.getChild("external")!=null)
931       flag.makeExternal();
932     cn.addFlag(flag);
933   }
934
935   private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
936     ParseNode mn=pn.getChild("modifier");
937     Modifiers m=parseModifiersList(mn);
938     if(cn.isInterface()) {
939       // TODO add version for normal Java later
940       // Can only be PUBLIC or STATIC or FINAL
941       if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
942          || (m.isSynchronized())) {
943         throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
944       }
945       m.addModifier(Modifiers.PUBLIC);
946       m.addModifier(Modifiers.STATIC);
947       m.addModifier(Modifiers.FINAL);
948     }
949
950     ParseNode tn=pn.getChild("type");
951     TypeDescriptor t=parseTypeDescriptor(tn);
952     ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
953     ParseNodeVector pnv=vn.getChildren();
954     boolean isglobal=pn.getChild("global")!=null;
955
956     for(int i=0; i<pnv.size(); i++) {
957       ParseNode vardecl=pnv.elementAt(i);
958       ParseNode tmp=vardecl;
959       TypeDescriptor arrayt=t;
960        if( this.isRunningRecursiveInnerClass && false )
961         {       
962                 System.out.println( "the length of the list is " + String.valueOf( pnv.size() ) );
963                 System.out.println( "\n the parse node is \n" + tmp.PPrint( 0, true ) );
964         }
965       while (tmp.getChild("single")==null) {
966         arrayt=arrayt.makeArray(state);
967         tmp=tmp.getChild("array");
968       }
969       String identifier=tmp.getChild("single").getTerminal();
970       ParseNode epn=vardecl.getChild("initializer");
971
972       ExpressionNode en=null;
973       
974       if (epn!=null) {
975         en=parseExpression(cn, null, m.isStatic(), epn.getFirstChild());
976         en.setNumLine(epn.getFirstChild().getLine());
977         if(m.isStatic()) {
978           // for static field, the initializer should be considered as a
979           // static block
980           boolean isfirst = false;
981           MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
982           if(md == null) {
983             // the first static block for this class
984             Modifiers m_i=new Modifiers();
985             m_i.addModifier(Modifiers.STATIC);
986             md = new MethodDescriptor(m_i, "staticblocks", false);
987             md.setAsStaticBlock();
988             isfirst = true;
989           }
990           if(isfirst) {
991             cn.addMethod(md);
992           }
993           cn.incStaticBlocks();
994           BlockNode bn=new BlockNode();
995           NameNode nn=new NameNode(new NameDescriptor(identifier));
996           nn.setNumLine(en.getNumLine());
997           AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
998           an.setNumLine(pn.getLine());
999           bn.addBlockStatement(new BlockExpressionNode(an));
1000           if(isfirst) {
1001             state.addTreeCode(md,bn);
1002           } else {
1003             BlockNode obn = state.getMethodBody(md);
1004             for(int ii = 0; ii < bn.size(); ii++) {
1005               BlockStatementNode bsn = bn.get(ii);
1006               obn.addBlockStatement(bsn);
1007             }
1008             state.addTreeCode(md, obn);
1009             bn = null;
1010           }
1011           en = null;
1012         }
1013       }
1014
1015       cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
1016       assignAnnotationsToType(m,arrayt);
1017     }
1018   }
1019
1020   private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type) {
1021     Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
1022     for(int i=0; i<annotations.size(); i++) {
1023       // it only supports a marker annotation
1024       AnnotationDescriptor an=annotations.elementAt(i);
1025       type.addAnnotationMarker(an);
1026     }
1027   }
1028
1029   int innerCount=0;
1030
1031   private ExpressionNode parseExpression(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1032     if (isNode(pn,"assignment"))
1033         {
1034           //System.out.println( "parsing a field decl in my class that has assignment in initialization " + pn.PPrint( 0, true ) + "\n");
1035       return parseAssignmentExpression(cn, md, isStaticContext, pn);
1036         }
1037     else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
1038              isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
1039              isNode(pn,"bitwise_and")||isNode(pn,"equal")||
1040              isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
1041              isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
1042              isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
1043              isNode(pn,"rightshift")||isNode(pn,"sub")||
1044              isNode(pn,"urightshift")||isNode(pn,"sub")||
1045              isNode(pn,"add")||isNode(pn,"mult")||
1046              isNode(pn,"div")||isNode(pn,"mod")) {
1047       ParseNodeVector pnv=pn.getChildren();
1048       ParseNode left=pnv.elementAt(0);
1049       ParseNode right=pnv.elementAt(1);
1050       Operation op=new Operation(pn.getLabel());
1051       OpNode on=new OpNode(parseExpression(cn, md, isStaticContext, left),parseExpression(cn, md, isStaticContext,  right),op);
1052       on.setNumLine(pn.getLine());
1053       return on;
1054     } else if (isNode(pn,"unaryplus")||
1055                isNode(pn,"unaryminus")||
1056                isNode(pn,"not")||
1057                isNode(pn,"comp")) {
1058       ParseNode left=pn.getFirstChild();
1059       Operation op=new Operation(pn.getLabel());
1060       OpNode on=new OpNode(parseExpression(cn, md, isStaticContext, left),op);
1061       on.setNumLine(pn.getLine());
1062       return on;
1063     } else if (isNode(pn,"postinc")||
1064                isNode(pn,"postdec")) {
1065       ParseNode left=pn.getFirstChild();
1066       AssignOperation op=new AssignOperation(pn.getLabel());
1067       AssignmentNode an=new AssignmentNode(parseExpression(cn, md, isStaticContext, left),null,op);
1068       an.setNumLine(pn.getLine());
1069       return an;
1070
1071     } else if (isNode(pn,"preinc")||
1072                isNode(pn,"predec")) {
1073       ParseNode left=pn.getFirstChild();
1074       AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
1075       AssignmentNode an=new AssignmentNode(parseExpression(cn, md, isStaticContext, left),
1076                                            new LiteralNode("integer",new Integer(1)),op);
1077       an.setNumLine(pn.getLine());
1078       return an;
1079     } else if (isNode(pn,"literal")) {
1080       String literaltype=pn.getTerminal();
1081       ParseNode literalnode=pn.getChild(literaltype);
1082       Object literal_obj=literalnode.getLiteral();
1083       LiteralNode ln=new LiteralNode(literaltype, literal_obj);
1084       ln.setNumLine(pn.getLine());
1085       return ln;
1086     } else if (isNode(pn, "createobject")) {
1087       TypeDescriptor td = parseTypeDescriptor(pn);
1088
1089       Vector args = parseArgumentList(cn, md, isStaticContext, pn);
1090       boolean isglobal = pn.getChild("global") != null || pn.getChild("scratch") != null;
1091       String disjointId = null;
1092       if (pn.getChild("disjoint") != null) {
1093         disjointId = pn.getChild("disjoint").getTerminal();
1094       }
1095       ParseNode idChild = (pn.getChild( "id" ));
1096       ParseNode baseChild = (pn.getChild( "base" ));
1097       
1098       CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
1099       if( null != idChild && null != idChild.getFirstChild() ) {
1100         idChild = idChild.getFirstChild();
1101         //System.out.println( "\nThe object passed has this expression " + idChild.PPrint( 0, true ) );
1102         ExpressionNode en = parseExpression(cn, md, isStaticContext, idChild ); 
1103         //System.out.println( "\nThe object passed has this expression " + en.printNode( 0 ) );
1104         con.setSurroundingExpression( en );
1105       }
1106       else if( null != baseChild  && null != baseChild.getFirstChild()  ) {
1107         baseChild = baseChild.getFirstChild();
1108         //System.out.println( "\nThe object passed has this expression " + baseChild.PPrint( 0, true ) );
1109         ExpressionNode en = parseExpression(cn, md, isStaticContext, baseChild ); 
1110         //System.out.println( "\nThe object passed has this expression " + en.printNode( 0 ) );
1111         con.setSurroundingExpression( en );     
1112       }
1113       con.setNumLine(pn.getLine());
1114       for (int i = 0; i < args.size(); i++) {
1115         con.addArgument((ExpressionNode) args.get(i));
1116       }
1117       /* Could have flag set or tag added here */
1118       if (pn.getChild("flag_list") != null || pn.getChild("tag_list") != null) {
1119         FlagEffects fe = new FlagEffects(null);
1120         if (pn.getChild("flag_list") != null)
1121           parseFlagEffect(fe, pn.getChild("flag_list"));
1122
1123         if (pn.getChild("tag_list") != null)
1124           parseTagEffect(fe, pn.getChild("tag_list"));
1125         con.addFlagEffects(fe);
1126       }
1127
1128       return con;
1129     } else if (isNode(pn,"createobjectcls")) {
1130       TypeDescriptor td=parseTypeDescriptor(pn);
1131       innerCount++;
1132       ClassDescriptor cnnew=new ClassDescriptor(packageName,td.getSymbol()+"$"+innerCount, false);
1133       pushChainMaps();
1134       cnnew.setImports(mandatoryImports, multiimports);
1135       cnnew.setSuper(td.getSymbol());
1136       cnnew.setInline();
1137       // the inline anonymous class does not have modifiers, it cannot be static 
1138       // it is always implicit final
1139       cnnew.setModifiers(new Modifiers(Modifiers.FINAL));
1140       cnnew.setAsInnerClass();
1141       cnnew.setSurroundingClass(cn.getSymbol());
1142       cnnew.setSurrounding(cn);
1143       cn.addInnerClass(cnnew);
1144       // Note that if the inner class is declared in a static context, it does NOT 
1145       // have lexically enclosing instances.
1146       if((null!=md)&&(md.isStatic()||md.isStaticBlock())) {
1147         cnnew.setSurroundingBlock(md);
1148         cnnew.setInStaticContext();
1149       } else if (isStaticContext) {
1150         cnnew.setInStaticContext();
1151       }
1152       parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
1153       boolean hasConstructor = false;
1154       for(Iterator method_it=cnnew.getMethods(); method_it.hasNext(); ) {
1155           MethodDescriptor cmd=(MethodDescriptor)method_it.next();
1156           hasConstructor |= cmd.isConstructor();
1157       }
1158       if(hasConstructor) {
1159           // anonymous class should not have explicit constructors
1160           throw new Error("Error! Anonymous class " + cnnew.getSymbol() + " in " + cn.getSymbol() + " has explicit constructors!");
1161       } else if(!cnnew.isEnum()) {
1162           // add a default constructor for this class
1163           MethodDescriptor cmd = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL),cnnew.getSymbol(), false);
1164           BlockNode bn=new BlockNode();
1165           state.addTreeCode(cmd,bn);
1166           cmd.setDefaultConstructor();
1167           cnnew.addMethod(cmd);
1168       }
1169       TypeDescriptor tdnew=state.getTypeDescriptor(cnnew.getSymbol());
1170
1171       Vector args=parseArgumentList(cn, md, isStaticContext, pn);
1172       ParseNode idChild = pn.getChild( "id" );
1173       ParseNode baseChild = pn.getChild( "base" );
1174       //System.out.println("\n to print idchild and basechild for ");
1175       /*if( null != idChild )
1176         System.out.println( "\n trying to create an inner class and the id child passed is "  + idChild.PPrint( 0, true ) );
1177       if( null != baseChild )
1178         System.out.println( "\n trying to create an inner class and the base child passed is "  + baseChild.PPrint( 0, true ) );*/
1179       CreateObjectNode con=new CreateObjectNode(tdnew, false, null);
1180       con.setNumLine(pn.getLine());
1181       for(int i=0; i<args.size(); i++) {
1182         con.addArgument((ExpressionNode)args.get(i));
1183       }
1184       popChainMaps();
1185
1186       if (analyzeset != null)
1187         analyzeset.add(cnnew);
1188       cnnew.setSourceFileName(currsourcefile);
1189       state.addClass(cnnew);
1190
1191       return con;
1192     } else if (isNode(pn,"createarray")) {
1193       //System.out.println(pn.PPrint(3,true));
1194       boolean isglobal=pn.getChild("global")!=null||
1195                         pn.getChild("scratch")!=null;
1196       String disjointId=null;
1197       if( pn.getChild("disjoint") != null) {
1198         disjointId = pn.getChild("disjoint").getTerminal();
1199       }
1200       TypeDescriptor td=parseTypeDescriptor(pn);
1201       Vector args=parseDimExprs(cn, md, isStaticContext, pn);
1202       int num=0;
1203       if (pn.getChild("dims_opt").getLiteral()!=null)
1204         num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1205       for(int i=0; i<(args.size()+num); i++)
1206         td=td.makeArray(state);
1207       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
1208       con.setNumLine(pn.getLine());
1209       for(int i=0; i<args.size(); i++) {
1210         con.addArgument((ExpressionNode)args.get(i));
1211       }
1212       return con;
1213     }
1214     if (isNode(pn,"createarray2")) {
1215       TypeDescriptor td=parseTypeDescriptor(pn);
1216       int num=0;
1217       if (pn.getChild("dims_opt").getLiteral()!=null)
1218         num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1219       for(int i=0; i<num; i++)
1220         td=td.makeArray(state);
1221       CreateObjectNode con=new CreateObjectNode(td, false, null);
1222       con.setNumLine(pn.getLine());
1223       ParseNode ipn = pn.getChild("initializer");
1224       Vector initializers=parseVariableInitializerList(cn, md, isStaticContext, ipn);
1225       ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
1226       ain.setNumLine(pn.getLine());
1227       con.addArrayInitializer(ain);
1228       return con;
1229     } else if (isNode(pn,"name")) {
1230       NameDescriptor nd=parseName(pn);
1231       NameNode nn=new NameNode(nd);
1232       nn.setNumLine(pn.getLine());
1233       return nn;
1234     } else if (isNode(pn,"this")) {
1235       NameDescriptor nd=new NameDescriptor("this");
1236       NameNode nn=new NameNode(nd);
1237       nn.setNumLine(pn.getLine());
1238       return nn;
1239     } else if (isNode(pn,"parentclass")) {
1240       NameDescriptor nd=new NameDescriptor(pn.getChild("name").getFirstChild().getFirstChild().getTerminal());
1241       NameNode nn=new NameNode(nd);
1242       nn.setNumLine(pn.getLine());
1243         //because inner classes pass right thru......
1244       FieldAccessNode fan=new FieldAccessNode(nn,"this");
1245       fan.setNumLine(pn.getLine());
1246       return fan;
1247     } else if (isNode(pn,"isavailable")) {
1248       NameDescriptor nd=new NameDescriptor(pn.getTerminal());
1249       NameNode nn=new NameNode(nd);
1250       nn.setNumLine(pn.getLine());
1251       return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
1252     } else if (isNode(pn,"methodinvoke1")) {
1253       NameDescriptor nd=parseName(pn.getChild("name"));
1254       Vector args=parseArgumentList(cn, md, isStaticContext, pn);
1255       MethodInvokeNode min=new MethodInvokeNode(nd);
1256       min.setNumLine(pn.getLine());
1257       for(int i=0; i<args.size(); i++) {
1258         min.addArgument((ExpressionNode)args.get(i));
1259       }
1260       return min;
1261     } else if (isNode(pn,"methodinvoke2")) {
1262       String methodid=pn.getChild("id").getTerminal();
1263       ExpressionNode exp=parseExpression(cn, md, isStaticContext, pn.getChild("base").getFirstChild());
1264       Vector args=parseArgumentList(cn, md, isStaticContext, pn);
1265       MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
1266       min.setNumLine(pn.getLine());
1267       for(int i=0; i<args.size(); i++) {
1268         min.addArgument((ExpressionNode)args.get(i));
1269       }
1270       return min;
1271     } else if (isNode(pn,"fieldaccess")) {
1272       ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getChild("base").getFirstChild());
1273       String fieldname=pn.getChild("field").getTerminal();
1274
1275       FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1276       fan.setNumLine(pn.getLine());
1277       return fan;
1278     } else if (isNode(pn,"superfieldaccess")) {
1279         ExpressionNode en=new NameNode(new NameDescriptor("super"));
1280         String fieldname=pn.getChild("field").getTerminal();
1281
1282         FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1283         fan.setNumLine(pn.getLine());
1284         return fan;
1285     } else if (isNode(pn,"supernamefieldaccess")) {
1286         ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getChild("base").getFirstChild());
1287         ExpressionNode exp = new FieldAccessNode(en, "super");
1288         exp.setNumLine(pn.getLine());
1289         String fieldname=pn.getChild("field").getTerminal();
1290
1291         FieldAccessNode fan=new FieldAccessNode(exp,fieldname);
1292         fan.setNumLine(pn.getLine());
1293         return fan;
1294     } else if (isNode(pn,"arrayaccess")) {
1295       ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getChild("base").getFirstChild());
1296       ExpressionNode index=parseExpression(cn, md, isStaticContext, pn.getChild("index").getFirstChild());
1297       ArrayAccessNode aan=new ArrayAccessNode(en,index);
1298       aan.setNumLine(pn.getLine());
1299       return aan;
1300     } else if (isNode(pn,"cast1")) {
1301       try {
1302         CastNode castn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(cn, md, isStaticContext, pn.getChild("exp").getFirstChild()));
1303         castn.setNumLine(pn.getLine());
1304         return castn;
1305       } catch (Exception e) {
1306         System.out.println(pn.PPrint(1,true));
1307         e.printStackTrace();
1308         throw new Error();
1309       }
1310     } else if (isNode(pn,"cast2")) {
1311       CastNode castn=new CastNode(parseExpression(cn, md, isStaticContext, pn.getChild("type").getFirstChild()),parseExpression(cn, md, isStaticContext, pn.getChild("exp").getFirstChild()));
1312       castn.setNumLine(pn.getLine());
1313       return castn;
1314     } else if (isNode(pn, "getoffset")) {
1315       TypeDescriptor td=parseTypeDescriptor(pn);
1316       String fieldname = pn.getChild("field").getTerminal();
1317       //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ "  fieldname= " + fieldname);
1318       return new OffsetNode(td, fieldname);
1319     } else if (isNode(pn, "tert")) {
1320
1321       TertiaryNode tn=new TertiaryNode(parseExpression(cn, md, isStaticContext, pn.getChild("cond").getFirstChild()),
1322                                        parseExpression(cn, md, isStaticContext, pn.getChild("trueexpr").getFirstChild()),
1323                                        parseExpression(cn, md, isStaticContext, pn.getChild("falseexpr").getFirstChild()) );
1324       tn.setNumLine(pn.getLine());
1325
1326       return tn;
1327     } else if (isNode(pn, "instanceof")) {
1328       ExpressionNode exp=parseExpression(cn, md, isStaticContext, pn.getChild("exp").getFirstChild());
1329       TypeDescriptor t=parseTypeDescriptor(pn);
1330       InstanceOfNode ion=new InstanceOfNode(exp,t);
1331       ion.setNumLine(pn.getLine());
1332       return ion;
1333     } else if (isNode(pn, "array_initializer")) {
1334       Vector initializers=parseVariableInitializerList(cn, md, isStaticContext, pn);
1335       return new ArrayInitializerNode(initializers);
1336     } else if (isNode(pn, "class_type")) {
1337       TypeDescriptor td=parseTypeDescriptor(pn);
1338       ClassTypeNode ctn=new ClassTypeNode(td);
1339       ctn.setNumLine(pn.getLine());
1340       return ctn;
1341     } else if (isNode(pn, "empty")) {
1342       return null;
1343     } else {
1344       System.out.println("---------------------");
1345       System.out.println(pn.PPrint(3,true));
1346       throw new Error();
1347     }
1348   }
1349
1350   private Vector parseDimExprs(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1351     Vector arglist=new Vector();
1352     ParseNode an=pn.getChild("dim_exprs");
1353     if (an==null)       /* No argument list */
1354       return arglist;
1355     ParseNodeVector anv=an.getChildren();
1356     for(int i=0; i<anv.size(); i++) {
1357       arglist.add(parseExpression(cn, md, isStaticContext, anv.elementAt(i)));
1358     }
1359     return arglist;
1360   }
1361
1362   private Vector parseArgumentList(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1363     Vector arglist=new Vector();
1364     ParseNode an=pn.getChild("argument_list");
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[] parseConsArgumentList(ParseNode pn) {
1375     Vector arglist=new Vector();
1376     Vector varlist=new Vector();
1377     ParseNode an=pn.getChild("cons_argument_list");
1378     if (an==null)       /* No argument list */
1379       return new Vector[] {varlist, arglist};
1380     ParseNodeVector anv=an.getChildren();
1381     for(int i=0; i<anv.size(); i++) {
1382       ParseNode cpn=anv.elementAt(i);
1383       ParseNode var=cpn.getChild("var");
1384       ParseNode exp=cpn.getChild("exp").getFirstChild();
1385       varlist.add(var.getTerminal());
1386       arglist.add(parseExpression(null, null, false, exp));
1387     }
1388     return new Vector[] {varlist, arglist};
1389   }
1390
1391   private Vector parseVariableInitializerList(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1392     Vector varInitList=new Vector();
1393     ParseNode vin=pn.getChild("var_init_list");
1394     if (vin==null)       /* No argument list */
1395       return varInitList;
1396     ParseNodeVector vinv=vin.getChildren();
1397     for(int i=0; i<vinv.size(); i++) {
1398       varInitList.add(parseExpression(cn, md, isStaticContext, vinv.elementAt(i)));
1399     }
1400     return varInitList;
1401   }
1402
1403   private ExpressionNode parseAssignmentExpression(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1404     AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1405     ParseNodeVector pnv=pn.getChild("args").getChildren();
1406
1407     AssignmentNode an=new AssignmentNode(parseExpression(cn, md, isStaticContext, pnv.elementAt(0)),parseExpression(cn, md, isStaticContext, pnv.elementAt(1)),ao);
1408     an.setNumLine(pn.getLine());
1409     return an;
1410   }
1411
1412
1413   private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1414     ParseNode headern=pn.getChild("method_header");
1415     ParseNode bodyn=pn.getChild("body");
1416     MethodDescriptor md=parseMethodHeader(headern);
1417     try {
1418       BlockNode bn=parseBlock(cn, md, md.isStatic()||md.isStaticBlock(), bodyn);
1419       bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1420       cn.addMethod(md);
1421       state.addTreeCode(md,bn);
1422
1423       // this is a hack for investigating new language features
1424       // at the AST level, someday should evolve into a nice compiler
1425       // option *wink*
1426       //if( cn.getSymbol().equals( ***put a class in here like:     "Test" ) &&
1427       //    md.getSymbol().equals( ***put your method in here like: "main" )
1428       //) {
1429       //  bn.setStyle( BlockNode.NORMAL );
1430       //  System.out.println( bn.printNode( 0 ) );
1431       //}
1432
1433     } catch (Exception e) {
1434       System.out.println("Error with method:"+md.getSymbol());
1435       e.printStackTrace();
1436       throw new Error();
1437     } catch (Error e) {
1438       System.out.println("Error with method:"+md.getSymbol());
1439       e.printStackTrace();
1440       throw new Error();
1441     }
1442   }
1443
1444   private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1445     ParseNode mn=pn.getChild("modifiers");
1446     Modifiers m=parseModifiersList(mn);
1447     ParseNode cdecl=pn.getChild("constructor_declarator");
1448     boolean isglobal=cdecl.getChild("global")!=null;
1449     String name=resolveName(cn.getSymbol());
1450     MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1451     ParseNode paramnode=cdecl.getChild("parameters");
1452     parseParameterList(md,paramnode);
1453     ParseNode bodyn0=pn.getChild("body");
1454     ParseNode bodyn=bodyn0.getChild("constructor_body");
1455     cn.addMethod(md);
1456     BlockNode bn=null;
1457     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1458       bn=parseBlock(cn, md, md.isStatic()||md.isStaticBlock(), bodyn);
1459     else
1460       bn=new BlockNode();
1461     if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1462       ParseNode sin=bodyn.getChild("superinvoke");
1463       NameDescriptor nd=new NameDescriptor("super");
1464       Vector args=parseArgumentList(cn, md, true, sin);
1465       MethodInvokeNode min=new MethodInvokeNode(nd);
1466       min.setNumLine(sin.getLine());
1467       for(int i=0; i<args.size(); i++) {
1468         min.addArgument((ExpressionNode)args.get(i));
1469       }
1470       BlockExpressionNode ben=new BlockExpressionNode(min);
1471       bn.addFirstBlockStatement(ben);
1472
1473     } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1474       ParseNode eci=bodyn.getChild("explconstrinv");
1475       NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1476       Vector args=parseArgumentList(cn, md, true, eci);
1477       MethodInvokeNode min=new MethodInvokeNode(nd);
1478       min.setNumLine(eci.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       ben.setNumLine(eci.getLine());
1484       bn.addFirstBlockStatement(ben);
1485     }
1486     state.addTreeCode(md,bn);
1487   }
1488
1489   private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1490     // Each class maintains one MethodDecscriptor which combines all its
1491     // static blocks in their declaration order
1492     boolean isfirst = false;
1493     MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1494     if(md == null) {
1495       // the first static block for this class
1496       Modifiers m_i=new Modifiers();
1497       m_i.addModifier(Modifiers.STATIC);
1498       md = new MethodDescriptor(m_i, "staticblocks", false);
1499       md.setAsStaticBlock();
1500       isfirst = true;
1501     }
1502     ParseNode bodyn=pn.getChild("body");
1503     if(isfirst) {
1504       cn.addMethod(md);
1505     }
1506     cn.incStaticBlocks();
1507     BlockNode bn=null;
1508     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1509       bn=parseBlock(cn, md, true, bodyn);
1510     else
1511       bn=new BlockNode();
1512     if(isfirst) {
1513       state.addTreeCode(md,bn);
1514     } else {
1515       BlockNode obn = state.getMethodBody(md);
1516       for(int i = 0; i < bn.size(); i++) {
1517         BlockStatementNode bsn = bn.get(i);
1518         obn.addBlockStatement(bsn);
1519       }
1520       state.addTreeCode(md, obn);
1521       bn = null;
1522     }
1523   }
1524
1525   public BlockNode parseBlock(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1526     this.m_taskexitnum = 0;
1527     if (pn==null||isEmpty(pn.getTerminal()))
1528       return new BlockNode();
1529     ParseNode bsn=pn.getChild("block_statement_list");
1530     return parseBlockHelper(cn, md, isStaticContext, bsn);
1531   }
1532
1533   private BlockNode parseBlockHelper(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1534     ParseNodeVector pnv=pn.getChildren();
1535     BlockNode bn=new BlockNode();
1536     for(int i=0; i<pnv.size(); i++) {
1537       Vector bsv=parseBlockStatement(cn, md, isStaticContext, pnv.elementAt(i));
1538       for(int j=0; j<bsv.size(); j++) {
1539         bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1540       }
1541     }
1542     return bn;
1543   }
1544
1545   public BlockNode parseSingleBlock(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn, String label){
1546     BlockNode bn=new BlockNode();
1547     Vector bsv=parseBlockStatement(cn, md, isStaticContext, pn,label);
1548     for(int j=0; j<bsv.size(); j++) {
1549       bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1550     }
1551     bn.setStyle(BlockNode.NOBRACES);
1552     return bn;
1553   }
1554   
1555   public BlockNode parseSingleBlock(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn) {
1556     return parseSingleBlock(cn, md, isStaticContext, pn, null);
1557   }
1558
1559   public Vector parseSESEBlock(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, Vector parentbs, ParseNode pn) {
1560     ParseNodeVector pnv=pn.getChildren();
1561     Vector bv=new Vector();
1562     for(int i=0; i<pnv.size(); i++) {
1563       bv.addAll(parseBlockStatement(cn, md, isStaticContext, pnv.elementAt(i)));
1564     }
1565     return bv;
1566   }
1567   
1568   public Vector parseBlockStatement(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn){
1569     return parseBlockStatement(cn, md, isStaticContext, pn,null);
1570   }
1571
1572   public Vector parseBlockStatement(ClassDescriptor cn, MethodDescriptor md, boolean isStaticContext, ParseNode pn, String label) {
1573     Vector blockstatements=new Vector();
1574     if (isNode(pn,"tag_declaration")) {
1575       String name=pn.getChild("single").getTerminal();
1576       String type=pn.getChild("type").getTerminal();
1577
1578       TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1579       tdn.setNumLine(pn.getLine());
1580
1581       blockstatements.add(tdn);
1582     } else if (isNode(pn,"local_variable_declaration")) {
1583
1584       TypeDescriptor t=parseTypeDescriptor(pn);
1585       ParseNode vn=pn.getChild("variable_declarators_list");
1586       ParseNodeVector pnv=vn.getChildren();
1587       for(int i=0; i<pnv.size(); i++) {
1588         ParseNode vardecl=pnv.elementAt(i);
1589
1590
1591         ParseNode tmp=vardecl;
1592         TypeDescriptor arrayt=t;
1593
1594         while (tmp.getChild("single")==null) {
1595           arrayt=arrayt.makeArray(state);
1596           tmp=tmp.getChild("array");
1597         }
1598         String identifier=tmp.getChild("single").getTerminal();
1599
1600         ParseNode epn=vardecl.getChild("initializer");
1601
1602
1603         ExpressionNode en=null;
1604         if (epn!=null)
1605           en=parseExpression(cn, md, isStaticContext, epn.getFirstChild());
1606
1607         DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1608         dn.setNumLine(tmp.getLine());
1609         
1610         ParseNode mn=pn.getChild("modifiers");
1611         if(mn!=null) {
1612           // here, modifers parse node has the list of annotations
1613           Modifiers m=parseModifiersList(mn);
1614           assignAnnotationsToType(m, arrayt);
1615         }
1616
1617         blockstatements.add(dn);
1618       }
1619     } else if (isNode(pn,"nop")) {
1620       /* Do Nothing */
1621     } else if (isNode(pn,"expression")) {
1622       BlockExpressionNode ben=new BlockExpressionNode(parseExpression(cn, md, isStaticContext, pn.getFirstChild()));
1623       ben.setNumLine(pn.getLine());
1624       blockstatements.add(ben);
1625     } else if (isNode(pn,"ifstatement")) {
1626       IfStatementNode isn=new IfStatementNode(parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild()),
1627                                               parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild()),
1628                                               pn.getChild("else_statement")!=null?parseSingleBlock(cn, md, isStaticContext, pn.getChild("else_statement").getFirstChild()):null);
1629       isn.setNumLine(pn.getLine());
1630
1631       blockstatements.add(isn);
1632     } else if (isNode(pn,"switch_statement")) {
1633       // TODO add version for normal Java later
1634       SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild()),
1635                                                       parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild()));
1636       ssn.setNumLine(pn.getLine());
1637       blockstatements.add(ssn);
1638     } else if (isNode(pn,"switch_block_list")) {
1639       // TODO add version for normal Java later
1640       ParseNodeVector pnv=pn.getChildren();
1641       for(int i=0; i<pnv.size(); i++) {
1642         ParseNode sblockdecl=pnv.elementAt(i);
1643
1644         if(isNode(sblockdecl, "switch_block")) {
1645           ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1646           ParseNodeVector labelv=lpn.getChildren();
1647           Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1648           for(int j=0; j<labelv.size(); j++) {
1649             ParseNode labeldecl=labelv.elementAt(j);
1650             if(isNode(labeldecl, "switch_label")) {
1651               SwitchLabelNode sln=new SwitchLabelNode(parseExpression(cn, md, isStaticContext, labeldecl.getChild("constant_expression").getFirstChild()), false);
1652               sln.setNumLine(labeldecl.getLine());
1653               slv.addElement(sln);
1654             } else if(isNode(labeldecl, "default_switch_label")) {
1655               SwitchLabelNode sln=new SwitchLabelNode(null, true);
1656               sln.setNumLine(labeldecl.getLine());
1657               slv.addElement(sln);
1658             }
1659           }
1660
1661           SwitchBlockNode sbn=new SwitchBlockNode(slv,
1662                                                   parseSingleBlock(cn, md, isStaticContext, sblockdecl.getChild("switch_statements").getFirstChild()));
1663           sbn.setNumLine(sblockdecl.getLine());
1664
1665           blockstatements.add(sbn);
1666
1667         }
1668       }
1669     } else if (isNode(pn, "trycatchstatement")) {
1670       // TODO add version for normal Java later
1671       // Do not fully support exceptions now. Only make sure that if there are no
1672       // exceptions thrown, the execution is right
1673       ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1674       BlockNode bn=parseBlockHelper(cn, md, isStaticContext, tpn);
1675       blockstatements.add(new SubBlockNode(bn));
1676
1677       ParseNode fbk = pn.getChild("finallyblock");
1678       if(fbk != null) {
1679         ParseNode fpn = fbk.getFirstChild();
1680         BlockNode fbn=parseBlockHelper(cn, md, isStaticContext, fpn);
1681         blockstatements.add(new SubBlockNode(fbn));
1682       }
1683     } else if (isNode(pn, "throwstatement")) {
1684       // TODO Simply return here
1685       //blockstatements.add(new ReturnNode());
1686     } else if (isNode(pn,"taskexit")) {
1687       Vector vfe=null;
1688       if (pn.getChild("flag_effects_list")!=null)
1689         vfe=parseFlags(pn.getChild("flag_effects_list"));
1690       Vector ccs=null;
1691       if (pn.getChild("cons_checks")!=null)
1692         ccs=parseChecks(pn.getChild("cons_checks"));
1693       TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1694       ten.setNumLine(pn.getLine());
1695       blockstatements.add(ten);
1696     } else if (isNode(pn,"atomic")) {
1697       BlockNode bn=parseBlockHelper(cn, md, isStaticContext, pn);
1698       AtomicNode an=new AtomicNode(bn);
1699       an.setNumLine(pn.getLine());
1700       blockstatements.add(an);
1701     } else if (isNode(pn,"synchronized")) {
1702       BlockNode bn=parseBlockHelper(cn, md, isStaticContext, pn.getChild("block"));
1703       ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getChild("expr").getFirstChild());
1704       SynchronizedNode sn=new SynchronizedNode(en, bn);
1705       sn.setNumLine(pn.getLine());
1706       blockstatements.add(sn);
1707     } else if (isNode(pn,"return")) {
1708       if (isEmpty(pn.getTerminal()))
1709         blockstatements.add(new ReturnNode());
1710       else {
1711         ExpressionNode en=parseExpression(cn, md, isStaticContext, pn.getFirstChild());
1712         ReturnNode rn=new ReturnNode(en);
1713         rn.setNumLine(pn.getLine());
1714         blockstatements.add(rn);
1715       }
1716     } else if (isNode(pn,"block_statement_list")) {
1717       BlockNode bn=parseBlockHelper(cn, md, isStaticContext, pn);
1718       blockstatements.add(new SubBlockNode(bn));
1719     } else if (isNode(pn,"empty")) {
1720       /* nop */
1721     } else if (isNode(pn,"statement_expression_list")) {
1722       ParseNodeVector pnv=pn.getChildren();
1723       BlockNode bn=new BlockNode();
1724       for(int i=0; i<pnv.size(); i++) {
1725         ExpressionNode en=parseExpression(cn, md, isStaticContext, pnv.elementAt(i));
1726         blockstatements.add(new BlockExpressionNode(en));
1727       }
1728       bn.setStyle(BlockNode.EXPRLIST);
1729     } else if (isNode(pn,"forstatement")) {
1730       BlockNode init=parseSingleBlock(cn, md, isStaticContext, pn.getChild("initializer").getFirstChild());
1731       BlockNode update=parseSingleBlock(cn, md, isStaticContext, pn.getChild("update").getFirstChild());
1732       ExpressionNode condition=parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild());
1733       BlockNode body=parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild());
1734       if(condition == null) {
1735         // no condition clause, make a 'true' expression as the condition
1736         condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1737       }
1738       LoopNode ln=new LoopNode(init,condition,update,body,label);
1739       ln.setNumLine(pn.getLine());
1740       blockstatements.add(ln);
1741     } else if (isNode(pn,"whilestatement")) {
1742       ExpressionNode condition=parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild());
1743       BlockNode body=parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild());
1744       if(condition == null) {
1745         // no condition clause, make a 'true' expression as the condition
1746         condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1747       }
1748       blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP,label));
1749     } else if (isNode(pn,"dowhilestatement")) {
1750       ExpressionNode condition=parseExpression(cn, md, isStaticContext, pn.getChild("condition").getFirstChild());
1751       BlockNode body=parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild());
1752       if(condition == null) {
1753         // no condition clause, make a 'true' expression as the condition
1754         condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1755       }
1756       blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP,label));
1757     } else if (isNode(pn,"sese")) {
1758       ParseNode pnID=pn.getChild("identifier");
1759       String stID=null;
1760       if( pnID != null ) {
1761         stID=pnID.getFirstChild().getTerminal();
1762       }
1763       SESENode start=new SESENode(stID);
1764       start.setNumLine(pn.getLine());
1765       SESENode end  =new SESENode(stID);
1766       start.setEnd(end);
1767       end.setStart(start);
1768       blockstatements.add(start);
1769       blockstatements.addAll(parseSESEBlock(cn, md, isStaticContext, blockstatements,pn.getChild("body").getFirstChild()));
1770       blockstatements.add(end);
1771     } else if (isNode(pn,"continue")) {
1772       ContinueBreakNode cbn=new ContinueBreakNode(false);
1773       cbn.setNumLine(pn.getLine());
1774       blockstatements.add(cbn);
1775     } else if (isNode(pn,"break")) {
1776       ContinueBreakNode cbn=new ContinueBreakNode(true);
1777       cbn.setNumLine(pn.getLine());
1778       blockstatements.add(cbn);
1779       ParseNode idopt_pn=pn.getChild("identifier_opt");
1780       ParseNode name_pn=idopt_pn.getChild("name");
1781       // name_pn.getTerminal() gives you the label
1782
1783     } else if (isNode(pn,"genreach")) {
1784       String graphName = pn.getChild("graphName").getTerminal();
1785       blockstatements.add(new GenReachNode(graphName) );
1786
1787     } else if (isNode(pn,"gen_def_reach")) {
1788       String outputName = pn.getChild("outputName").getTerminal();
1789       blockstatements.add(new GenDefReachNode(outputName) );
1790
1791     } else if(isNode(pn,"labeledstatement")) {
1792       String labeledstatement = pn.getChild("name").getTerminal();
1793       BlockNode bn=parseSingleBlock(cn, md, isStaticContext, pn.getChild("statement").getFirstChild(),labeledstatement);
1794       blockstatements.add(new SubBlockNode(bn));
1795     } else {
1796       System.out.println("---------------");
1797       System.out.println(pn.PPrint(3,true));
1798       throw new Error();
1799     }
1800     return blockstatements;
1801   }
1802
1803   public MethodDescriptor parseMethodHeader(ParseNode pn) {
1804     ParseNode mn=pn.getChild("modifiers");
1805     Modifiers m=parseModifiersList(mn);
1806
1807     ParseNode tn=pn.getChild("returntype");
1808     TypeDescriptor returntype;
1809     if (tn!=null)
1810       returntype=parseTypeDescriptor(tn);
1811     else
1812       returntype=new TypeDescriptor(TypeDescriptor.VOID);
1813
1814     ParseNode pmd=pn.getChild("method_declarator");
1815     String name=pmd.getChild("name").getTerminal();
1816     MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1817
1818     ParseNode paramnode=pmd.getChild("parameters");
1819     parseParameterList(md,paramnode);
1820     return md;
1821   }
1822
1823   public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1824     ParseNode paramlist=pn.getChild("formal_parameter_list");
1825     if (paramlist==null)
1826       return;
1827     ParseNodeVector pnv=paramlist.getChildren();
1828     for(int i=0; i<pnv.size(); i++) {
1829       ParseNode paramn=pnv.elementAt(i);
1830
1831       if (isNode(paramn, "tag_parameter")) {
1832         String paramname=paramn.getChild("single").getTerminal();
1833         TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1834         md.addTagParameter(type, paramname);
1835       } else  {
1836
1837         TypeDescriptor type=parseTypeDescriptor(paramn);
1838
1839         ParseNode tmp=paramn;
1840         while (tmp.getChild("single")==null) {
1841           type=type.makeArray(state);
1842           tmp=tmp.getChild("array");
1843         }
1844         String paramname=tmp.getChild("single").getTerminal();
1845
1846         md.addParameter(type, paramname);
1847         if(isNode(paramn, "annotation_parameter")) {
1848           ParseNode listnode=paramn.getChild("annotation_list");
1849           parseParameterAnnotation(listnode,type);
1850         }
1851
1852       }
1853     }
1854   }
1855
1856   public Modifiers parseModifiersList(ParseNode pn) {
1857     Modifiers m=new Modifiers();
1858     ParseNode modlist=pn.getChild("modifier_list");
1859     if (modlist!=null) {
1860       ParseNodeVector pnv=modlist.getChildren();
1861       for(int i=0; i<pnv.size(); i++) {
1862         ParseNode modn=pnv.elementAt(i);
1863         if (isNode(modn,"public"))
1864           m.addModifier(Modifiers.PUBLIC);
1865         else if (isNode(modn,"protected"))
1866           m.addModifier(Modifiers.PROTECTED);
1867         else if (isNode(modn,"private"))
1868           m.addModifier(Modifiers.PRIVATE);
1869         else if (isNode(modn,"static"))
1870           m.addModifier(Modifiers.STATIC);
1871         else if (isNode(modn,"final"))
1872           m.addModifier(Modifiers.FINAL);
1873         else if (isNode(modn,"native"))
1874           m.addModifier(Modifiers.NATIVE);
1875         else if (isNode(modn,"synchronized"))
1876           m.addModifier(Modifiers.SYNCHRONIZED);
1877         else if (isNode(modn,"atomic"))
1878           m.addModifier(Modifiers.ATOMIC);
1879         else if (isNode(modn,"abstract"))
1880           m.addModifier(Modifiers.ABSTRACT);
1881         else if (isNode(modn,"volatile"))
1882           m.addModifier(Modifiers.VOLATILE);
1883         else if (isNode(modn,"transient"))
1884           m.addModifier(Modifiers.TRANSIENT);
1885         else if(isNode(modn,"annotation_list"))
1886           parseAnnotationList(modn,m);
1887         else {
1888           throw new Error("Unrecognized Modifier:"+modn.getLabel());
1889         }
1890       }
1891     }
1892     return m;
1893   }
1894
1895   private void parseAnnotationList(ParseNode pn, Modifiers m) {
1896     ParseNodeVector pnv = pn.getChildren();
1897     for (int i = 0; i < pnv.size(); i++) {
1898       ParseNode body_list = pnv.elementAt(i);
1899       if (isNode(body_list, "annotation_body")) {
1900         ParseNode body_node = body_list.getFirstChild();
1901         if (isNode(body_node, "marker_annotation")) {
1902           m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1903         } else if (isNode(body_node, "single_annotation")) {
1904           m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1905                                                    body_node.getChild("element_value").getTerminal()));
1906         } else if (isNode(body_node, "normal_annotation")) {
1907           throw new Error("Annotation with multiple data members is not supported yet.");
1908         }
1909       }
1910     }
1911   }
1912
1913   private void parseParameterAnnotation(ParseNode pn,TypeDescriptor type) {
1914     ParseNodeVector pnv = pn.getChildren();
1915     for (int i = 0; i < pnv.size(); i++) {
1916       ParseNode body_list = pnv.elementAt(i);
1917       if (isNode(body_list, "annotation_body")) {
1918         ParseNode body_node = body_list.getFirstChild();
1919         if (isNode(body_node, "marker_annotation")) {
1920           type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1921         } else if (isNode(body_node, "single_annotation")) {
1922           type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1923                                                    body_node.getChild("element_value").getTerminal()));
1924         } else if (isNode(body_node, "normal_annotation")) {
1925           throw new Error("Annotation with multiple data members is not supported yet.");
1926         }
1927       }
1928     }
1929   }
1930
1931   private boolean isNode(ParseNode pn, String label) {
1932     if (pn.getLabel().equals(label))
1933       return true;
1934     else return false;
1935   }
1936
1937   private static boolean isEmpty(ParseNode pn) {
1938     if (pn.getLabel().equals("empty"))
1939       return true;
1940     else
1941       return false;
1942   }
1943
1944   private static boolean isEmpty(String s) {
1945     if (s.equals("empty"))
1946       return true;
1947     else
1948       return false;
1949   }
1950
1951   /** Throw an exception if something is unexpected */
1952   private void check(ParseNode pn, String label) {
1953     if (pn == null) {
1954       throw new Error(pn+ "IE: Expected '" + label + "', got null");
1955     }
1956     if (!pn.getLabel().equals(label)) {
1957       throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
1958     }
1959   }
1960 }