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