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