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