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