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