cf43c80c96a53861a7aaf83b5d14a683a5f99304
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3
4 import java.util.*;
5
6
7 public class BuildIR {
8   State state;
9
10   private int m_taskexitnum;
11
12   public BuildIR(State state) {
13     this.state=state;
14     this.m_taskexitnum = 0;
15   }
16
17   public void buildtree(ParseNode pn, Set toanalyze) {
18     parseFile(pn, toanalyze);
19   }
20
21   Vector singleimports;
22   Vector multiimports;
23   NameDescriptor packages;
24
25   /** Parse the classes in this file */
26   public void parseFile(ParseNode pn, Set toanalyze) {
27     singleimports=new Vector();
28     multiimports=new Vector();
29
30     ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
31     if (ipn!=null) {
32       ParseNodeVector pnv=ipn.getChildren();
33       for(int i=0; i<pnv.size(); i++) {
34         ParseNode pnimport=pnv.elementAt(i);
35         NameDescriptor nd=parseName(pnimport.getChild("name"));
36         if (isNode(pnimport,"import_single"))
37           singleimports.add(nd);
38         else
39           multiimports.add(nd);
40       }
41     }
42     ParseNode ppn=pn.getChild("packages").getChild("package");
43     if (ppn!=null) {
44       packages=parseName(ppn.getChild("name"));
45     }
46     ParseNode tpn=pn.getChild("type_declaration_list");
47     if (tpn!=null) {
48       ParseNodeVector pnv=tpn.getChildren();
49       for(int i=0; i<pnv.size(); i++) {
50         ParseNode type_pn=pnv.elementAt(i);
51         if (isEmpty(type_pn))         /* Skip the semicolon */
52           continue;
53         if (isNode(type_pn,"class_declaration")) {
54           ClassDescriptor cn=parseTypeDecl(type_pn);
55           if (toanalyze!=null)
56             toanalyze.add(cn);
57           state.addClass(cn);
58       // for inner classes
59       if(state.MGC) {
60         // TODO add version for normal Java later
61         HashSet tovisit = new HashSet();
62         Iterator it_icds = cn.getInnerClasses();
63         while(it_icds.hasNext()) {
64           tovisit.add(it_icds.next());
65         }
66         
67         while(!tovisit.isEmpty()) {
68           ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
69           tovisit.remove(cd);
70           
71           if(toanalyze != null) {
72             toanalyze.add(cd);
73           }
74           state.addClass(cd);
75           
76           Iterator it_ics = cd.getInnerClasses();
77           while(it_ics.hasNext()) {
78             tovisit.add(it_ics.next());
79           }
80         }
81       }
82         } else if (isNode(type_pn,"task_declaration")) {
83           TaskDescriptor td=parseTaskDecl(type_pn);
84           if (toanalyze!=null)
85             toanalyze.add(td);
86           state.addTask(td);
87         } else if ((state.MGC) && isNode(type_pn,"interface_declaration")) {
88       // TODO add version for normal Java later
89       ClassDescriptor cn = parseInterfaceDecl(type_pn);
90       if (toanalyze!=null)
91         toanalyze.add(cn);
92       state.addClass(cn);
93     } else {
94           throw new Error(type_pn.getLabel());
95         }
96       }
97     }
98   }
99   
100   public ClassDescriptor parseInterfaceDecl(ParseNode pn) {
101     ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
102     //cn.setAsInterface();
103     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
104       /* parse inherited interface name */
105       ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
106       ParseNodeVector pnv=snlist.getChildren();
107       for(int i=0; i<pnv.size(); i++) {
108         ParseNode decl=pnv.elementAt(i);
109         if (isNode(decl,"type")) {
110           NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
111           cn.addSuperInterface(nd.toString());
112         }
113       }
114     }
115     cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
116     parseInterfaceBody(cn, pn.getChild("interfacebody"));
117     return cn;
118   }
119   
120   private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
121     assert(cn.isInterface());
122     ParseNode decls=pn.getChild("interface_member_declaration_list");
123     if (decls!=null) {
124       ParseNodeVector pnv=decls.getChildren();
125       for(int i=0; i<pnv.size(); i++) {
126         ParseNode decl=pnv.elementAt(i);
127         if (isNode(decl,"constant")) {
128           parseInterfaceConstant(cn,decl);
129         } else if (isNode(decl,"method")) {
130           parseInterfaceMethod(cn,decl.getChild("method_declaration"));
131         } else throw new Error();
132       }
133     }
134   }
135   
136   private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
137     if (pn!=null) {
138       parseFieldDecl(cn,pn.getChild("field_declaration"));
139       return;
140     }
141     throw new Error();
142   }
143   
144   private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
145     ParseNode headern=pn.getChild("header");
146     ParseNode bodyn=pn.getChild("body");
147     MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
148     md.getModifiers().addModifier(Modifiers.PUBLIC);
149     md.getModifiers().addModifier(Modifiers.ABSTRACT);
150     try {
151       BlockNode bn=parseBlock(bodyn);
152       cn.addMethod(md);
153       state.addTreeCode(md,bn);
154
155       // this is a hack for investigating new language features
156       // at the AST level, someday should evolve into a nice compiler
157       // option *wink*
158       //if( cn.getSymbol().equals( ***put a class in here like:     "Test" ) &&
159       //    md.getSymbol().equals( ***put your method in here like: "main" ) 
160       //) {
161       //  bn.setStyle( BlockNode.NORMAL );
162       //  System.out.println( bn.printNode( 0 ) );
163       //}
164
165     } catch (Exception e) {
166       System.out.println("Error with method:"+md.getSymbol());
167       e.printStackTrace();
168       throw new Error();
169     } catch (Error e) {
170       System.out.println("Error with method:"+md.getSymbol());
171       e.printStackTrace();
172       throw new Error();
173     }
174   }
175
176   public TaskDescriptor parseTaskDecl(ParseNode pn) {
177     TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
178     ParseNode bodyn=pn.getChild("body");
179     BlockNode bn=parseBlock(bodyn);
180     parseParameterList(td, pn);
181     state.addTreeCode(td,bn);
182     if (pn.getChild("flag_effects_list")!=null)
183       td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
184     return td;
185   }
186
187   public Vector parseFlags(ParseNode pn) {
188     Vector vfe=new Vector();
189     ParseNodeVector pnv=pn.getChildren();
190     for(int i=0; i<pnv.size(); i++) {
191       ParseNode fn=pnv.elementAt(i);
192       FlagEffects fe=parseFlagEffects(fn);
193       vfe.add(fe);
194     }
195     return vfe;
196   }
197
198   public FlagEffects parseFlagEffects(ParseNode pn) {
199     if (isNode(pn,"flag_effect")) {
200       String flagname=pn.getChild("name").getTerminal();
201       FlagEffects fe=new FlagEffects(flagname);
202       if (pn.getChild("flag_list")!=null)
203         parseFlagEffect(fe, pn.getChild("flag_list"));
204       if (pn.getChild("tag_list")!=null)
205         parseTagEffect(fe, pn.getChild("tag_list"));
206       return fe;
207     } else throw new Error();
208   }
209
210   public void parseTagEffect(FlagEffects fes, ParseNode pn) {
211     ParseNodeVector pnv=pn.getChildren();
212     for(int i=0; i<pnv.size(); i++) {
213       ParseNode pn2=pnv.elementAt(i);
214       boolean status=true;
215       if (isNode(pn2,"not")) {
216         status=false;
217         pn2=pn2.getChild("name");
218       }
219       String name=pn2.getTerminal();
220       fes.addTagEffect(new TagEffect(name,status));
221     }
222   }
223
224   public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
225     ParseNodeVector pnv=pn.getChildren();
226     for(int i=0; i<pnv.size(); i++) {
227       ParseNode pn2=pnv.elementAt(i);
228       boolean status=true;
229       if (isNode(pn2,"not")) {
230         status=false;
231         pn2=pn2.getChild("name");
232       }
233       String name=pn2.getTerminal();
234       fes.addEffect(new FlagEffect(name,status));
235     }
236   }
237
238   public FlagExpressionNode parseFlagExpression(ParseNode pn) {
239     if (isNode(pn,"or")) {
240       ParseNodeVector pnv=pn.getChildren();
241       ParseNode left=pnv.elementAt(0);
242       ParseNode right=pnv.elementAt(1);
243       return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
244     } else if (isNode(pn,"and")) {
245       ParseNodeVector pnv=pn.getChildren();
246       ParseNode left=pnv.elementAt(0);
247       ParseNode right=pnv.elementAt(1);
248       return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
249     } else if (isNode(pn, "not")) {
250       ParseNodeVector pnv=pn.getChildren();
251       ParseNode left=pnv.elementAt(0);
252       return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
253
254     } else if (isNode(pn,"name")) {
255       return new FlagNode(pn.getTerminal());
256     } else {
257       throw new Error();
258     }
259   }
260
261   public Vector parseChecks(ParseNode pn) {
262     Vector ccs=new Vector();
263     ParseNodeVector pnv=pn.getChildren();
264     for(int i=0; i<pnv.size(); i++) {
265       ParseNode fn=pnv.elementAt(i);
266       ConstraintCheck cc=parseConstraintCheck(fn);
267       ccs.add(cc);
268     }
269     return ccs;
270   }
271
272   public ConstraintCheck parseConstraintCheck(ParseNode pn) {
273     if (isNode(pn,"cons_check")) {
274       String specname=pn.getChild("name").getChild("identifier").getTerminal();
275       Vector[] args=parseConsArgumentList(pn);
276       ConstraintCheck cc=new ConstraintCheck(specname);
277       for(int i=0; i<args[0].size(); i++) {
278         cc.addVariable((String)args[0].get(i));
279         cc.addArgument((ExpressionNode)args[1].get(i));
280       }
281       return cc;
282     } else throw new Error();
283   }
284
285   public void parseParameterList(TaskDescriptor td, ParseNode pn) {
286
287     boolean optional;
288     ParseNode paramlist=pn.getChild("task_parameter_list");
289     if (paramlist==null)
290       return;
291     ParseNodeVector pnv=paramlist.getChildren();
292     for(int i=0; i<pnv.size(); i++) {
293       ParseNode paramn=pnv.elementAt(i);
294       if(paramn.getChild("optional")!=null) {
295         optional = true;
296         paramn = paramn.getChild("optional").getFirstChild();
297         System.out.println("OPTIONAL FOUND!!!!!!!");
298       } else { optional = false;
299                System.out.println("NOT OPTIONAL");}
300
301       TypeDescriptor type=parseTypeDescriptor(paramn);
302
303       String paramname=paramn.getChild("single").getTerminal();
304       FlagExpressionNode fen=null;
305       if (paramn.getChild("flag")!=null)
306         fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
307
308       ParseNode tagnode=paramn.getChild("tag");
309
310       TagExpressionList tel=null;
311       if (tagnode!=null) {
312         tel=parseTagExpressionList(tagnode);
313       }
314
315       td.addParameter(type,paramname,fen, tel, optional);
316     }
317   }
318
319   public TagExpressionList parseTagExpressionList(ParseNode pn) {
320     //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
321     //To test, feed in any input program that uses tags
322     ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
323     TagExpressionList tel=new TagExpressionList();
324     for(int i=0; i<pnv.size(); i++) {
325       ParseNode tn=pnv.elementAt(i);
326       String type=tn.getChild("type").getTerminal();
327       String name=tn.getChild("single").getTerminal();
328       tel.addTag(type, name);
329     }
330     return tel;
331   }
332
333   public ClassDescriptor parseTypeDecl(ParseNode pn) {
334     ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
335     if (!isEmpty(pn.getChild("super").getTerminal())) {
336       /* parse superclass name */
337       ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
338       NameDescriptor nd=parseName(snn);
339       cn.setSuper(nd.toString());
340     } else {
341       if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
342             cn.getSymbol().equals(TypeUtil.TagClass)))
343         cn.setSuper(TypeUtil.ObjectClass);
344     }
345     // check inherited interfaces
346     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
347       /* parse inherited interface name */
348       ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
349       ParseNodeVector pnv=snlist.getChildren();
350       for(int i=0; i<pnv.size(); i++) {
351         ParseNode decl=pnv.elementAt(i);
352         if (isNode(decl,"type")) {
353           NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
354           cn.addSuperInterface(nd.toString());
355         }
356       }
357     }
358     cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
359     parseClassBody(cn, pn.getChild("classbody"));
360     return cn;
361   }
362
363   private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
364     ParseNode decls=pn.getChild("class_body_declaration_list");
365     if (decls!=null) {
366       ParseNodeVector pnv=decls.getChildren();
367       for(int i=0; i<pnv.size(); i++) {
368         ParseNode decl=pnv.elementAt(i);
369         if (isNode(decl,"member")) {
370           parseClassMember(cn,decl);
371         } else if (isNode(decl,"constructor")) {
372           parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
373         } else if (isNode(decl, "static_block")) {
374       if(state.MGC) {
375         // TODO add version for normal Java later
376       parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
377       } else {
378         throw new Error("Static blocks not implemented");
379       }
380     } else if (isNode(decl,"block")) {
381         } else throw new Error();
382       }
383     }
384   }
385   
386   private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
387     ParseNode fieldnode=pn.getChild("field");
388     if (fieldnode!=null) {
389       parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
390       return;
391     }
392     ParseNode methodnode=pn.getChild("method");
393     if (methodnode!=null) {
394       parseMethodDecl(cn,methodnode.getChild("method_declaration"));
395       return;
396     }
397     ParseNode innerclassnode=pn.getChild("inner_class_declaration");
398     if (innerclassnode!=null) {
399       if(state.MGC){
400         parseInnerClassDecl(cn,innerclassnode);
401         return;
402       } else {
403         // TODO add version for noraml Java later
404         throw new Error("Error: inner class in Class " + cn.getSymbol() + " is not supported yet");
405       }
406     }
407     ParseNode flagnode=pn.getChild("flag");
408     if (flagnode!=null) {
409       parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
410       return;
411     }
412     throw new Error();
413   }
414   
415   private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
416     ClassDescriptor icn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
417     icn.setAsInnerClass();
418     icn.setSurroundingClass(cn.getSymbol());
419     icn.setSurrounding(cn);
420     cn.addInnerClass(icn);
421     if (!isEmpty(pn.getChild("super").getTerminal())) {
422       /* parse superclass name */
423       ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
424       NameDescriptor nd=parseName(snn);
425       icn.setSuper(nd.toString());
426     } else {
427       if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
428           icn.getSymbol().equals(TypeUtil.TagClass)))
429         icn.setSuper(TypeUtil.ObjectClass);
430     }
431     // check inherited interfaces
432     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
433       /* parse inherited interface name */
434       ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
435       ParseNodeVector pnv=snlist.getChildren();
436       for(int i=0; i<pnv.size(); i++) {
437         ParseNode decl=pnv.elementAt(i);
438         if (isNode(decl,"type")) {
439           NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
440           icn.addSuperInterface(nd.toString());
441         }
442       }
443     }
444     icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
445     if(!icn.isStatic()) {
446       throw new Error("Error: inner class " + icn.getSymbol() + " in Class " + 
447           cn.getSymbol() + " is not a nested class and is not supported yet!");
448     }
449     parseClassBody(icn, pn.getChild("classbody"));
450     return icn;
451   }
452
453   private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
454     ParseNode tn=pn.getChild("type");
455     String type_st=tn.getTerminal();
456     if(type_st.equals("byte")) {
457       return state.getTypeDescriptor(TypeDescriptor.BYTE);
458     } else if(type_st.equals("short")) {
459       return state.getTypeDescriptor(TypeDescriptor.SHORT);
460     } else if(type_st.equals("boolean")) {
461       return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
462     } else if(type_st.equals("int")) {
463       return state.getTypeDescriptor(TypeDescriptor.INT);
464     } else if(type_st.equals("long")) {
465       return state.getTypeDescriptor(TypeDescriptor.LONG);
466     } else if(type_st.equals("char")) {
467       return state.getTypeDescriptor(TypeDescriptor.CHAR);
468     } else if(type_st.equals("float")) {
469       return state.getTypeDescriptor(TypeDescriptor.FLOAT);
470     } else if(type_st.equals("double")) {
471       return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
472     } else if(type_st.equals("class")) {
473       ParseNode nn=tn.getChild("class");
474       return state.getTypeDescriptor(parseName(nn.getChild("name")));
475     } else if(type_st.equals("array")) {
476       ParseNode nn=tn.getChild("array");
477       TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
478       Integer numdims=(Integer)nn.getChild("dims").getLiteral();
479       for(int i=0; i<numdims.intValue(); i++)
480         td=td.makeArray(state);
481       return td;
482     } else {
483       System.out.println(pn.PPrint(2, true));
484       throw new Error();
485     }
486   }
487
488   private NameDescriptor parseName(ParseNode nn) {
489     ParseNode base=nn.getChild("base");
490     ParseNode id=nn.getChild("identifier");
491     if (base==null)
492       return new NameDescriptor(id.getTerminal());
493     return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
494
495   }
496
497   private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
498     String name=pn.getChild("name").getTerminal();
499     FlagDescriptor flag=new FlagDescriptor(name);
500     if (pn.getChild("external")!=null)
501       flag.makeExternal();
502     cn.addFlag(flag);
503   }
504
505   private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
506     ParseNode mn=pn.getChild("modifier");
507     Modifiers m=parseModifiersList(mn);
508     if((state.MGC) && cn.isInterface()) {
509       // TODO add version for normal Java later
510       // Can only be PUBLIC or STATIC or FINAL
511       if((m.isAbstract()) || (m.isAtomic()) || (m.isNative()) 
512           || (m.isSynchronized())) {
513         throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
514       }
515       m.addModifier(Modifiers.PUBLIC);
516       m.addModifier(Modifiers.STATIC);
517       m.addModifier(Modifiers.FINAL);
518     }
519
520     ParseNode tn=pn.getChild("type");
521     TypeDescriptor t=parseTypeDescriptor(tn);
522     ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
523     ParseNodeVector pnv=vn.getChildren();
524     boolean isglobal=pn.getChild("global")!=null;
525
526     for(int i=0; i<pnv.size(); i++) {
527       ParseNode vardecl=pnv.elementAt(i);
528       ParseNode tmp=vardecl;
529       TypeDescriptor arrayt=t;
530       while (tmp.getChild("single")==null) {
531         arrayt=arrayt.makeArray(state);
532         tmp=tmp.getChild("array");
533       }
534       String identifier=tmp.getChild("single").getTerminal();
535       ParseNode epn=vardecl.getChild("initializer");
536
537       ExpressionNode en=null;
538       if (epn!=null)
539         en=parseExpression(epn.getFirstChild());
540
541       cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
542     }
543   }
544
545   private ExpressionNode parseExpression(ParseNode pn) {
546     if (isNode(pn,"assignment"))
547       return parseAssignmentExpression(pn);
548     else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
549              isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
550              isNode(pn,"bitwise_and")||isNode(pn,"equal")||
551              isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
552              isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
553              isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
554              isNode(pn,"rightshift")||isNode(pn,"sub")||
555              isNode(pn,"urightshift")||isNode(pn,"sub")||
556              isNode(pn,"add")||isNode(pn,"mult")||
557              isNode(pn,"div")||isNode(pn,"mod")) {
558       ParseNodeVector pnv=pn.getChildren();
559       ParseNode left=pnv.elementAt(0);
560       ParseNode right=pnv.elementAt(1);
561       Operation op=new Operation(pn.getLabel());
562       return new OpNode(parseExpression(left),parseExpression(right),op);
563     } else if (isNode(pn,"unaryplus")||
564                isNode(pn,"unaryminus")||
565                isNode(pn,"not")||
566                isNode(pn,"comp")) {
567       ParseNode left=pn.getFirstChild();
568       Operation op=new Operation(pn.getLabel());
569       return new OpNode(parseExpression(left),op);
570     } else if (isNode(pn,"postinc")||
571                isNode(pn,"postdec")) {
572       ParseNode left=pn.getFirstChild();
573       AssignOperation op=new AssignOperation(pn.getLabel());
574       return new AssignmentNode(parseExpression(left),null,op);
575
576     } else if (isNode(pn,"preinc")||
577                isNode(pn,"predec")) {
578       ParseNode left=pn.getFirstChild();
579       AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
580       return new AssignmentNode(parseExpression(left),
581                                 new LiteralNode("integer",new Integer(1)),op);
582     } else if (isNode(pn,"literal")) {
583       String literaltype=pn.getTerminal();
584       ParseNode literalnode=pn.getChild(literaltype);
585       Object literal_obj=literalnode.getLiteral();
586       return new LiteralNode(literaltype, literal_obj);
587     } else if (isNode(pn,"createobject")) {
588       TypeDescriptor td=parseTypeDescriptor(pn);
589       
590       Vector args=parseArgumentList(pn);
591       boolean isglobal=pn.getChild("global")!=null||
592         pn.getChild("scratch")!=null;
593       String disjointId=null;
594       if( pn.getChild("disjoint") != null) {
595         disjointId = pn.getChild("disjoint").getTerminal();
596       }
597       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
598       for(int i=0; i<args.size(); i++) {
599         con.addArgument((ExpressionNode)args.get(i));
600       }
601       /* Could have flag set or tag added here */
602       if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
603         FlagEffects fe=new FlagEffects(null);
604         if (pn.getChild("flag_list")!=null)
605           parseFlagEffect(fe, pn.getChild("flag_list"));
606
607         if (pn.getChild("tag_list")!=null)
608           parseTagEffect(fe, pn.getChild("tag_list"));
609         con.addFlagEffects(fe);
610       }
611
612       return con;
613     } else if (isNode(pn,"createarray")) {
614       //System.out.println(pn.PPrint(3,true));
615       boolean isglobal=pn.getChild("global")!=null||
616         pn.getChild("scratch")!=null;
617       String disjointId=null;
618       if( pn.getChild("disjoint") != null) {
619         disjointId = pn.getChild("disjoint").getTerminal();
620       }
621       TypeDescriptor td=parseTypeDescriptor(pn);
622       Vector args=parseDimExprs(pn);
623       int num=0;
624       if (pn.getChild("dims_opt").getLiteral()!=null)
625         num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
626       for(int i=0; i<(args.size()+num); i++)
627         td=td.makeArray(state);
628       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
629       for(int i=0; i<args.size(); i++) {
630         con.addArgument((ExpressionNode)args.get(i));
631       }
632       return con;
633     } else if (isNode(pn,"name")) {
634       NameDescriptor nd=parseName(pn);
635       return new NameNode(nd);
636     } else if (isNode(pn,"this")) {
637       NameDescriptor nd=new NameDescriptor("this");
638       return new NameNode(nd);
639     } else if (isNode(pn,"isavailable")) {
640       NameDescriptor nd=new NameDescriptor(pn.getTerminal());
641       return new OpNode(new NameNode(nd),null,new Operation(Operation.ISAVAILABLE));
642     } else if (isNode(pn,"methodinvoke1")) {
643       NameDescriptor nd=parseName(pn.getChild("name"));
644       Vector args=parseArgumentList(pn);
645       MethodInvokeNode min=new MethodInvokeNode(nd);
646       for(int i=0; i<args.size(); i++) {
647         min.addArgument((ExpressionNode)args.get(i));
648       }
649       return min;
650     } else if (isNode(pn,"methodinvoke2")) {
651       String methodid=pn.getChild("id").getTerminal();
652       ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
653       Vector args=parseArgumentList(pn);
654       MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
655       for(int i=0; i<args.size(); i++) {
656         min.addArgument((ExpressionNode)args.get(i));
657       }
658       return min;
659     } else if (isNode(pn,"fieldaccess")) {
660       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
661       String fieldname=pn.getChild("field").getTerminal();
662       return new FieldAccessNode(en,fieldname);
663     } else if (isNode(pn,"arrayaccess")) {
664       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
665       ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
666       return new ArrayAccessNode(en,index);
667     } else if (isNode(pn,"cast1")) {
668       try {
669         return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
670       } catch (Exception e) {
671         System.out.println(pn.PPrint(1,true));
672         e.printStackTrace();
673         throw new Error();
674       }
675     } else if (isNode(pn,"cast2")) {
676       return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
677     } else if (isNode(pn, "getoffset")) {
678       TypeDescriptor td=parseTypeDescriptor(pn);
679       String fieldname = pn.getChild("field").getTerminal();
680       //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ "  fieldname= " + fieldname);
681       return new OffsetNode(td, fieldname);
682     } else if (isNode(pn, "tert")) {
683       return new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
684                               parseExpression(pn.getChild("trueexpr").getFirstChild()),
685                               parseExpression(pn.getChild("falseexpr").getFirstChild()) );
686     } else if (isNode(pn, "instanceof")) {
687       ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
688       TypeDescriptor t=parseTypeDescriptor(pn);
689       return new InstanceOfNode(exp,t);
690     } else if (isNode(pn, "array_initializer")) {
691       System.out.println( "Array initializers not implemented yet." );
692       throw new Error();
693       //TypeDescriptor td=parseTypeDescriptor(pn);      
694       //Vector initializers=parseVariableInitializerList(pn);
695       //return new ArrayInitializerNode(td, initializers);
696     } else {
697       System.out.println("---------------------");
698       System.out.println(pn.PPrint(3,true));
699       throw new Error();
700     }
701   }
702
703   private Vector parseDimExprs(ParseNode pn) {
704     Vector arglist=new Vector();
705     ParseNode an=pn.getChild("dim_exprs");
706     if (an==null)       /* No argument list */
707       return arglist;
708     ParseNodeVector anv=an.getChildren();
709     for(int i=0; i<anv.size(); i++) {
710       arglist.add(parseExpression(anv.elementAt(i)));
711     }
712     return arglist;
713   }
714
715   private Vector parseArgumentList(ParseNode pn) {
716     Vector arglist=new Vector();
717     ParseNode an=pn.getChild("argument_list");
718     if (an==null)       /* No argument list */
719       return arglist;
720     ParseNodeVector anv=an.getChildren();
721     for(int i=0; i<anv.size(); i++) {
722       arglist.add(parseExpression(anv.elementAt(i)));
723     }
724     return arglist;
725   }
726
727   private Vector[] parseConsArgumentList(ParseNode pn) {
728     Vector arglist=new Vector();
729     Vector varlist=new Vector();
730     ParseNode an=pn.getChild("cons_argument_list");
731     if (an==null)       /* No argument list */
732       return new Vector[] {varlist, arglist};
733     ParseNodeVector anv=an.getChildren();
734     for(int i=0; i<anv.size(); i++) {
735       ParseNode cpn=anv.elementAt(i);
736       ParseNode var=cpn.getChild("var");
737       ParseNode exp=cpn.getChild("exp").getFirstChild();
738       varlist.add(var.getTerminal());
739       arglist.add(parseExpression(exp));
740     }
741     return new Vector[] {varlist, arglist};
742   }
743
744   private Vector parseVariableInitializerList(ParseNode pn) {
745     Vector varInitList=new Vector();
746     ParseNode vin=pn.getChild("variable_init_list");
747     if (vin==null)       /* No argument list */
748       return varInitList;
749     ParseNodeVector vinv=vin.getChildren();
750     for(int i=0; i<vinv.size(); i++) {
751       varInitList.add(parseExpression(vinv.elementAt(i)));
752     }
753     return varInitList;
754   }
755
756   private ExpressionNode parseAssignmentExpression(ParseNode pn) {
757     AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
758     ParseNodeVector pnv=pn.getChild("args").getChildren();
759
760     AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
761     return an;
762   }
763
764
765   private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
766     ParseNode headern=pn.getChild("method_header");
767     ParseNode bodyn=pn.getChild("body");
768     MethodDescriptor md=parseMethodHeader(headern);
769     try {
770       BlockNode bn=parseBlock(bodyn);
771       cn.addMethod(md);
772       state.addTreeCode(md,bn);
773
774       // this is a hack for investigating new language features
775       // at the AST level, someday should evolve into a nice compiler
776       // option *wink*
777       //if( cn.getSymbol().equals( ***put a class in here like:     "Test" ) &&
778       //    md.getSymbol().equals( ***put your method in here like: "main" ) 
779       //) {
780       //  bn.setStyle( BlockNode.NORMAL );
781       //  System.out.println( bn.printNode( 0 ) );
782       //}
783
784     } catch (Exception e) {
785       System.out.println("Error with method:"+md.getSymbol());
786       e.printStackTrace();
787       throw new Error();
788     } catch (Error e) {
789       System.out.println("Error with method:"+md.getSymbol());
790       e.printStackTrace();
791       throw new Error();
792     }
793   }
794
795   private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
796     ParseNode mn=pn.getChild("modifiers");
797     Modifiers m=parseModifiersList(mn);
798     ParseNode cdecl=pn.getChild("constructor_declarator");
799     boolean isglobal=cdecl.getChild("global")!=null;
800     String name=cdecl.getChild("name").getChild("identifier").getTerminal();
801     MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
802     ParseNode paramnode=cdecl.getChild("parameters");
803     parseParameterList(md,paramnode);
804     ParseNode bodyn0=pn.getChild("body");
805     ParseNode bodyn=bodyn0.getChild("constructor_body");
806     cn.addMethod(md);
807     BlockNode bn=null;
808     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
809       bn=parseBlock(bodyn);
810     else
811       bn=new BlockNode();
812     if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
813       ParseNode sin=bodyn.getChild("superinvoke");
814       NameDescriptor nd=new NameDescriptor("super");
815       Vector args=parseArgumentList(sin);
816       MethodInvokeNode min=new MethodInvokeNode(nd);
817       for(int i=0; i<args.size(); i++) {
818         min.addArgument((ExpressionNode)args.get(i));
819       }
820       BlockExpressionNode ben=new BlockExpressionNode(min);
821       bn.addFirstBlockStatement(ben);
822
823     } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
824       ParseNode eci=bodyn.getChild("explconstrinv");
825       NameDescriptor nd=new NameDescriptor(cn.getSymbol());
826       Vector args=parseArgumentList(eci);
827       MethodInvokeNode min=new MethodInvokeNode(nd);
828       for(int i=0; i<args.size(); i++) {
829         min.addArgument((ExpressionNode)args.get(i));
830       }
831       BlockExpressionNode ben=new BlockExpressionNode(min);
832       bn.addFirstBlockStatement(ben);
833     }
834     state.addTreeCode(md,bn);
835   }
836   
837   private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
838     // Each class maintains one MethodDecscriptor which combines all its 
839     // static blocks in their declaration order
840     boolean isfirst = false;
841     MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().get("staticblocks");
842     if(md == null) {
843       // the first static block for this class
844       Modifiers m=new Modifiers();
845       m.addModifier(Modifiers.STATIC);
846       md = new MethodDescriptor(m, "staticblocks", false);
847       md.setAsStaticBlock();
848       isfirst = true;
849     }
850     ParseNode bodyn=pn.getChild("body");
851     if(isfirst) {
852       cn.addMethod(md);
853     }
854     cn.incStaticBlocks();
855     BlockNode bn=null;
856     if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
857       bn=parseBlock(bodyn);
858     else
859       bn=new BlockNode();
860     if(isfirst) {
861       state.addTreeCode(md,bn);
862     } else {
863       BlockNode obn = state.getMethodBody(md);
864       for(int i = 0; i < bn.size(); i++) {
865         BlockStatementNode bsn = bn.get(i);
866         obn.addBlockStatement(bsn);
867       }
868       //TODO state.addTreeCode(md, obn);
869       bn = null;
870     }
871   }
872
873   public BlockNode parseBlock(ParseNode pn) {
874     this.m_taskexitnum = 0;
875     if (pn==null||isEmpty(pn.getTerminal()))
876       return new BlockNode();
877     ParseNode bsn=pn.getChild("block_statement_list");
878     return parseBlockHelper(bsn);
879   }
880
881   private BlockNode parseBlockHelper(ParseNode pn) {
882     ParseNodeVector pnv=pn.getChildren();
883     BlockNode bn=new BlockNode();
884     for(int i=0; i<pnv.size(); i++) {
885       Vector bsv=parseBlockStatement(pnv.elementAt(i));
886       for(int j=0; j<bsv.size(); j++) {
887         bn.addBlockStatement((BlockStatementNode)bsv.get(j));
888       }
889     }
890     return bn;
891   }
892
893   public BlockNode parseSingleBlock(ParseNode pn) {
894     BlockNode bn=new BlockNode();
895     Vector bsv=parseBlockStatement(pn);
896     for(int j=0; j<bsv.size(); j++) {
897       bn.addBlockStatement((BlockStatementNode)bsv.get(j));
898     }
899     bn.setStyle(BlockNode.NOBRACES);
900     return bn;
901   }
902
903   public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
904     ParseNodeVector pnv=pn.getChildren();
905     Vector bv=new Vector();
906     for(int i=0; i<pnv.size(); i++) {
907       bv.addAll(parseBlockStatement(pnv.elementAt(i)));
908     }
909     return bv;
910   }
911
912   public Vector parseBlockStatement(ParseNode pn) {
913     Vector blockstatements=new Vector();
914     if (isNode(pn,"tag_declaration")) {
915       String name=pn.getChild("single").getTerminal();
916       String type=pn.getChild("type").getTerminal();
917
918       blockstatements.add(new TagDeclarationNode(name, type));
919     } else if (isNode(pn,"local_variable_declaration")) {
920       TypeDescriptor t=parseTypeDescriptor(pn);
921       ParseNode vn=pn.getChild("variable_declarators_list");
922       ParseNodeVector pnv=vn.getChildren();
923       for(int i=0; i<pnv.size(); i++) {
924         ParseNode vardecl=pnv.elementAt(i);
925
926
927         ParseNode tmp=vardecl;
928         TypeDescriptor arrayt=t;
929         while (tmp.getChild("single")==null) {
930           arrayt=arrayt.makeArray(state);
931           tmp=tmp.getChild("array");
932         }
933         String identifier=tmp.getChild("single").getTerminal();
934
935         ParseNode epn=vardecl.getChild("initializer");
936
937
938         ExpressionNode en=null;
939         if (epn!=null)
940           en=parseExpression(epn.getFirstChild());
941
942         blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
943       }
944     } else if (isNode(pn,"nop")) {
945       /* Do Nothing */
946     } else if (isNode(pn,"expression")) {
947       blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
948     } else if (isNode(pn,"ifstatement")) {
949       blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
950                                               parseSingleBlock(pn.getChild("statement").getFirstChild()),
951                                               pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null));
952     } else if (isNode(pn,"taskexit")) {
953       Vector vfe=null;
954       if (pn.getChild("flag_effects_list")!=null)
955         vfe=parseFlags(pn.getChild("flag_effects_list"));
956       Vector ccs=null;
957       if (pn.getChild("cons_checks")!=null)
958         ccs=parseChecks(pn.getChild("cons_checks"));
959
960       blockstatements.add(new TaskExitNode(vfe, ccs, this.m_taskexitnum++));
961     } else if (isNode(pn,"atomic")) {
962       BlockNode bn=parseBlockHelper(pn);
963       blockstatements.add(new AtomicNode(bn));
964     } else if (isNode(pn,"synchronized")) {
965       BlockNode bn=parseBlockHelper(pn.getChild("block"));
966       ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
967       blockstatements.add(new SynchronizedNode(en, bn));
968     } else if (isNode(pn,"return")) {
969       if (isEmpty(pn.getTerminal()))
970         blockstatements.add(new ReturnNode());
971       else {
972         ExpressionNode en=parseExpression(pn.getFirstChild());
973         blockstatements.add(new ReturnNode(en));
974       }
975     } else if (isNode(pn,"block_statement_list")) {
976       BlockNode bn=parseBlockHelper(pn);
977       blockstatements.add(new SubBlockNode(bn));
978     } else if (isNode(pn,"empty")) {
979       /* nop */
980     } else if (isNode(pn,"statement_expression_list")) {
981       ParseNodeVector pnv=pn.getChildren();
982       BlockNode bn=new BlockNode();
983       for(int i=0; i<pnv.size(); i++) {
984         ExpressionNode en=parseExpression(pnv.elementAt(i));
985         blockstatements.add(new BlockExpressionNode(en));
986       }
987       bn.setStyle(BlockNode.EXPRLIST);
988     } else if (isNode(pn,"forstatement")) {
989       BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
990       BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
991       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
992       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
993       blockstatements.add(new LoopNode(init,condition,update,body));
994     } else if (isNode(pn,"whilestatement")) {
995       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
996       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
997       blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
998     } else if (isNode(pn,"dowhilestatement")) {
999       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1000       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1001       blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
1002     } else if (isNode(pn,"sese")) {
1003       ParseNode pnID=pn.getChild("identifier");
1004       String stID=null;
1005       if( pnID != null ) { stID=pnID.getFirstChild().getTerminal(); }
1006       SESENode start=new SESENode(stID);
1007       SESENode end  =new SESENode(stID);
1008       start.setEnd( end   );
1009       end.setStart( start );
1010       blockstatements.add(start);
1011       blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1012       blockstatements.add(end);
1013     } else if (isNode(pn,"continue")) {
1014       blockstatements.add(new ContinueBreakNode(false));
1015     } else if (isNode(pn,"break")) {
1016       blockstatements.add(new ContinueBreakNode(true));
1017
1018     } else if (isNode(pn,"genreach")) {
1019       String graphName = pn.getChild("graphName").getTerminal();
1020       blockstatements.add( new GenReachNode( graphName ) );
1021
1022     } else {
1023       System.out.println("---------------");
1024       System.out.println(pn.PPrint(3,true));
1025       throw new Error();
1026     }
1027     return blockstatements;
1028   }
1029
1030   public MethodDescriptor parseMethodHeader(ParseNode pn) {
1031     ParseNode mn=pn.getChild("modifiers");
1032     Modifiers m=parseModifiersList(mn);
1033
1034     ParseNode tn=pn.getChild("returntype");
1035     TypeDescriptor returntype;
1036     if (tn!=null)
1037       returntype=parseTypeDescriptor(tn);
1038     else
1039       returntype=new TypeDescriptor(TypeDescriptor.VOID);
1040
1041     ParseNode pmd=pn.getChild("method_declarator");
1042     String name=pmd.getChild("name").getTerminal();
1043     MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1044
1045     ParseNode paramnode=pmd.getChild("parameters");
1046     parseParameterList(md,paramnode);
1047     return md;
1048   }
1049
1050   public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1051     ParseNode paramlist=pn.getChild("formal_parameter_list");
1052     if (paramlist==null)
1053       return;
1054     ParseNodeVector pnv=paramlist.getChildren();
1055     for(int i=0; i<pnv.size(); i++) {
1056       ParseNode paramn=pnv.elementAt(i);
1057
1058       if (isNode(paramn, "tag_parameter")) {
1059         String paramname=paramn.getChild("single").getTerminal();
1060         TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1061         md.addTagParameter(type, paramname);
1062       } else {
1063         TypeDescriptor type=parseTypeDescriptor(paramn);
1064
1065         ParseNode tmp=paramn;
1066         while (tmp.getChild("single")==null) {
1067           type=type.makeArray(state);
1068           tmp=tmp.getChild("array");
1069         }
1070         String paramname=tmp.getChild("single").getTerminal();
1071
1072         md.addParameter(type, paramname);
1073       }
1074     }
1075   }
1076
1077   public Modifiers parseModifiersList(ParseNode pn) {
1078     Modifiers m=new Modifiers();
1079     ParseNode modlist=pn.getChild("modifier_list");
1080     if (modlist!=null) {
1081       ParseNodeVector pnv=modlist.getChildren();
1082       for(int i=0; i<pnv.size(); i++) {
1083         ParseNode modn=pnv.elementAt(i);
1084         if (isNode(modn,"public"))
1085           m.addModifier(Modifiers.PUBLIC);
1086         else if (isNode(modn,"protected"))
1087           m.addModifier(Modifiers.PROTECTED);
1088         else if (isNode(modn,"private"))
1089           m.addModifier(Modifiers.PRIVATE);
1090         else if (isNode(modn,"static"))
1091           m.addModifier(Modifiers.STATIC);
1092         else if (isNode(modn,"final"))
1093           m.addModifier(Modifiers.FINAL);
1094         else if (isNode(modn,"native"))
1095           m.addModifier(Modifiers.NATIVE);
1096         else if (isNode(modn,"synchronized"))
1097           m.addModifier(Modifiers.SYNCHRONIZED);
1098         else if (isNode(modn,"atomic"))
1099           m.addModifier(Modifiers.ATOMIC);
1100     else if (isNode(modn,"abstract"))
1101       m.addModifier(Modifiers.ABSTRACT);
1102         else throw new Error("Unrecognized Modifier");
1103       }
1104     }
1105     return m;
1106   }
1107
1108   private boolean isNode(ParseNode pn, String label) {
1109     if (pn.getLabel().equals(label))
1110       return true;
1111     else return false;
1112   }
1113
1114   private static boolean isEmpty(ParseNode pn) {
1115     if (pn.getLabel().equals("empty"))
1116       return true;
1117     else
1118       return false;
1119   }
1120
1121   private static boolean isEmpty(String s) {
1122     if (s.equals("empty"))
1123       return true;
1124     else
1125       return false;
1126   }
1127
1128   /** Throw an exception if something is unexpected */
1129   private void check(ParseNode pn, String label) {
1130     if (pn == null) {
1131       throw new Error(pn+ "IE: Expected '" + label + "', got null");
1132     }
1133     if (!pn.getLabel().equals(label)) {
1134       throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
1135     }
1136   }
1137 }