adding support for consistency checking
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3 import java.util.*;
4
5
6 public class BuildIR {
7     State state;
8     public BuildIR(State state) {
9         this.state=state;
10     }
11     public void buildtree() {
12         for(Iterator it=state.parsetrees.iterator();it.hasNext();) {
13             ParseNode pn=(ParseNode)it.next();
14             parseFile(pn);
15         }
16     }
17
18     /** Parse the classes in this file */
19     public void parseFile(ParseNode pn) {
20         ParseNode tpn=pn.getChild("type_declaration_list");
21         if (tpn!=null) {
22             ParseNodeVector pnv=tpn.getChildren();
23             for(int i=0;i<pnv.size();i++) {
24                 ParseNode type_pn=pnv.elementAt(i);
25                 if (isEmpty(type_pn)) /* Skip the semicolon */
26                     continue;
27                 if (isNode(type_pn,"class_declaration")) {
28                     ClassDescriptor cn=parseTypeDecl(type_pn);
29                     state.addClass(cn);
30                 } else if (isNode(type_pn,"task_declaration")) {
31                     TaskDescriptor td=parseTaskDecl(type_pn);
32                     state.addTask(td);
33                 } else {
34                     throw new Error(type_pn.getLabel());
35                 }
36             }
37         }
38     }
39
40     public TaskDescriptor parseTaskDecl(ParseNode pn) {
41         TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
42         ParseNode bodyn=pn.getChild("body");
43         BlockNode bn=parseBlock(bodyn);
44         parseParameterList(td, pn);
45         state.addTreeCode(td,bn);
46         if (pn.getChild("flag_effects_list")!=null)
47             td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
48         return td;
49     }
50
51     public Vector parseFlags(ParseNode pn) {
52         Vector vfe=new Vector();
53         ParseNodeVector pnv=pn.getChildren();
54         for(int i=0;i<pnv.size();i++) {
55             ParseNode fn=pnv.elementAt(i);
56             FlagEffects fe=parseFlagEffects(fn);
57             vfe.add(fe);
58         }
59         return vfe;
60     }
61
62     public FlagEffects parseFlagEffects(ParseNode pn) {
63         if (isNode(pn,"flag_effect")) {
64             String flagname=pn.getChild("name").getTerminal();
65             FlagEffects fe=new FlagEffects(flagname);
66             parseFlagEffect(fe, pn.getChild("flag_list"));
67             return fe;
68         } else throw new Error();
69     }
70
71     public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
72         ParseNodeVector pnv=pn.getChildren();
73         for(int i=0;i<pnv.size();i++) {
74             ParseNode pn2=pnv.elementAt(i);
75             boolean status=true;
76             if (isNode(pn2,"not")) {
77                 status=false;
78                 pn2=pn2.getChild("name");
79             }
80             String name=pn2.getTerminal();
81             fes.addEffect(new FlagEffect(name,status));
82         }
83     }
84
85     public FlagExpressionNode parseFlagExpression(ParseNode pn) {
86         if (pn.getChild("or")!=null) {
87             ParseNodeVector pnv=pn.getChild("or").getChildren();
88             ParseNode left=pnv.elementAt(0);
89             ParseNode right=pnv.elementAt(1);
90             return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
91         } else if (pn.getChild("and")!=null) {
92             ParseNodeVector pnv=pn.getChild("and").getChildren();
93             ParseNode left=pnv.elementAt(0);
94             ParseNode right=pnv.elementAt(1);
95             return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
96         } else if (pn.getChild("not")!=null) {
97             ParseNodeVector pnv=pn.getChild("not").getChildren();
98             ParseNode left=pnv.elementAt(0);
99             return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));           
100
101         } else if (pn.getChild("name")!=null) {
102             return new FlagNode(pn.getChild("name").getTerminal());
103         } else throw new Error();
104     }
105
106     public Vector parseChecks(ParseNode pn) {
107         Vector ccs=new Vector();
108         ParseNodeVector pnv=pn.getChildren();
109         for(int i=0;i<pnv.size();i++) {
110             ParseNode fn=pnv.elementAt(i);
111             ConstraintCheck cc=parseConstraintCheck(fn);
112             ccs.add(cc);
113         }
114         return ccs;
115     }
116
117     public ConstraintCheck parseConstraintCheck(ParseNode pn) {
118         if (isNode(pn,"cons_check")) {
119             String varname=pn.getChild("name").getTerminal();
120             String specname=pn.getChild("spec").getTerminal();
121             ConstraintCheck cc=new ConstraintCheck(varname, specname);
122             return cc;
123         } else throw new Error();
124     }
125
126     public void parseParameterList(TaskDescriptor td, ParseNode pn) {
127         ParseNode paramlist=pn.getChild("task_parameter_list");
128         if (paramlist==null)
129             return;
130          ParseNodeVector pnv=paramlist.getChildren();
131          for(int i=0;i<pnv.size();i++) {
132              ParseNode paramn=pnv.elementAt(i);
133              TypeDescriptor type=parseTypeDescriptor(paramn);
134
135              ParseNode tmp=paramn;
136              while (tmp.getChild("single")==null) {
137                  type=type.makeArray(state);
138                  tmp=tmp.getChild("array");
139              }
140              String paramname=tmp.getChild("single").getTerminal();
141              FlagExpressionNode fen=parseFlagExpression(paramn.getChild("flag"));
142              
143              td.addParameter(type,paramname,fen);
144          }
145     }
146
147     public ClassDescriptor parseTypeDecl(ParseNode pn) {
148         ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
149         if (!isEmpty(pn.getChild("super").getTerminal())) {
150             /* parse superclass name */
151             ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
152             NameDescriptor nd=parseName(snn);
153             cn.setSuper(nd.toString());
154         } else {
155             if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
156                 cn.setSuper(TypeUtil.ObjectClass);
157         }
158         cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
159         parseClassBody(cn, pn.getChild("classbody"));
160         return cn;
161     }
162
163     private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
164         ParseNode decls=pn.getChild("class_body_declaration_list");
165         if (decls!=null) {
166             ParseNodeVector pnv=decls.getChildren();
167             for(int i=0;i<pnv.size();i++) {
168                 ParseNode decl=pnv.elementAt(i);
169                 if (isNode(decl,"member")) {
170                     parseClassMember(cn,decl);
171                 } else if (isNode(decl,"constructor")) {
172                     parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
173                 } else if (isNode(decl,"block")) {
174                 } else throw new Error();
175             }
176         }
177     }
178
179     private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
180         ParseNode fieldnode=pn.getChild("field");
181
182         if (fieldnode!=null) {
183             parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
184             return;
185         }
186         ParseNode methodnode=pn.getChild("method");
187         if (methodnode!=null) {
188             parseMethodDecl(cn,methodnode.getChild("method_declaration"));
189             return;
190         }
191         ParseNode flagnode=pn.getChild("flag");
192         if (flagnode!=null) {
193             parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
194             return;
195         }
196         throw new Error();
197     }
198
199     private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
200         ParseNode tn=pn.getChild("type");
201
202         String type_st=tn.getTerminal();
203         if(type_st.equals("byte")) {
204             return state.getTypeDescriptor(TypeDescriptor.BYTE);
205         } else if(type_st.equals("short")) {
206             return state.getTypeDescriptor(TypeDescriptor.SHORT);
207         } else if(type_st.equals("boolean")) {
208             return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
209         } else if(type_st.equals("int")) {
210             return state.getTypeDescriptor(TypeDescriptor.INT);
211         } else if(type_st.equals("long")) {
212             return state.getTypeDescriptor(TypeDescriptor.LONG);
213         } else if(type_st.equals("char")) {
214             return state.getTypeDescriptor(TypeDescriptor.CHAR);
215         } else if(type_st.equals("float")) {
216             return state.getTypeDescriptor(TypeDescriptor.FLOAT);
217         } else if(type_st.equals("double")) {
218             return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
219         } else if(type_st.equals("class")) {
220             ParseNode nn=tn.getChild("class");
221             return state.getTypeDescriptor(parseName(nn.getChild("name")));
222         } else if(type_st.equals("array")) {
223             ParseNode nn=tn.getChild("array");
224             TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
225             Integer numdims=(Integer)nn.getChild("dims").getLiteral();
226             for(int i=0;i<numdims.intValue();i++)
227                 td=td.makeArray(state);
228             return td;
229         } else {
230             throw new Error();
231         }
232     }
233
234     private NameDescriptor parseName(ParseNode nn) {
235         ParseNode base=nn.getChild("base");
236         ParseNode id=nn.getChild("identifier");
237         if (base==null)
238             return new NameDescriptor(id.getTerminal());
239         return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
240         
241     }
242
243     private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
244         String name=pn.getChild("name").getTerminal();
245         cn.addFlag(new FlagDescriptor(name));
246     }
247
248     private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
249         ParseNode mn=pn.getChild("modifier");
250         Modifiers m=parseModifiersList(mn);
251
252         ParseNode tn=pn.getChild("type");
253         TypeDescriptor t=parseTypeDescriptor(tn);
254         ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
255         ParseNodeVector pnv=vn.getChildren();
256         for(int i=0;i<pnv.size();i++) {
257             ParseNode vardecl=pnv.elementAt(i);
258
259             
260             ParseNode tmp=vardecl;
261             TypeDescriptor arrayt=t;
262             while (tmp.getChild("single")==null) {
263                 arrayt=arrayt.makeArray(state);
264                 tmp=tmp.getChild("array");
265             }
266             String identifier=tmp.getChild("single").getTerminal();
267
268             ParseNode epn=vardecl.getChild("initializer");
269             
270             ExpressionNode en=null;
271             if (epn!=null)
272                 en=parseExpression(epn.getFirstChild());
273   
274             cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
275         }
276         
277     }
278
279     private ExpressionNode parseExpression(ParseNode pn) {
280         if (isNode(pn,"assignment"))
281             return parseAssignmentExpression(pn);
282         else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
283                  isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
284                  isNode(pn,"bitwise_and")||isNode(pn,"equal")||
285                  isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
286                  isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
287                  isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
288                  isNode(pn,"rightshift")||isNode(pn,"sub")||
289                  isNode(pn,"add")||isNode(pn,"mult")||
290                  isNode(pn,"div")||isNode(pn,"mod")) {
291             ParseNodeVector pnv=pn.getChildren();
292             ParseNode left=pnv.elementAt(0);
293             ParseNode right=pnv.elementAt(1);
294             Operation op=new Operation(pn.getLabel());
295             return new OpNode(parseExpression(left),parseExpression(right),op);
296         } else if (isNode(pn,"unaryplus")||
297                    isNode(pn,"unaryminus")||
298                    isNode(pn,"not")) {
299             ParseNode left=pn.getFirstChild();
300             Operation op=new Operation(pn.getLabel());
301             return new OpNode(parseExpression(left),op);
302         } else if (isNode(pn,"postinc")||
303                    isNode(pn,"postdec")) {
304             ParseNode left=pn.getFirstChild();
305             AssignOperation op=new AssignOperation(pn.getLabel());
306             return new AssignmentNode(parseExpression(left),null,op);
307
308         } else if (isNode(pn,"preinc")||
309                    isNode(pn,"predec")) {
310             ParseNode left=pn.getFirstChild();
311             AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
312             return new AssignmentNode(parseExpression(left),
313                                       new LiteralNode("integer",new Integer(1)),op);
314         } else if (isNode(pn,"literal")) {
315             String literaltype=pn.getTerminal();
316             ParseNode literalnode=pn.getChild(literaltype);
317             Object literal_obj=literalnode.getLiteral();
318             return new LiteralNode(literaltype, literal_obj);
319         } else if (isNode(pn,"createobject")) {
320             TypeDescriptor td=parseTypeDescriptor(pn);
321             Vector args=parseArgumentList(pn);
322             CreateObjectNode con=new CreateObjectNode(td);
323             for(int i=0;i<args.size();i++) {
324                 con.addArgument((ExpressionNode)args.get(i));
325             }
326             /* Could have flag set here */
327             if (pn.getChild("flag_list")!=null) {
328                 FlagEffects fe=new FlagEffects(null);
329                 parseFlagEffect(fe, pn.getChild("flag_list"));
330                 con.addFlagEffects(fe);
331             }
332             return con;
333         } else if (isNode(pn,"createarray")) {
334             System.out.println(pn.PPrint(3,true));
335             TypeDescriptor td=parseTypeDescriptor(pn);
336             Vector args=parseDimExprs(pn);
337             int num=0;
338             if (pn.getChild("dims_opt").getLiteral()!=null)
339                 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
340             for(int i=0;i<(args.size()+num);i++)
341                 td=td.makeArray(state);
342             CreateObjectNode con=new CreateObjectNode(td);
343             for(int i=0;i<args.size();i++) {
344                 con.addArgument((ExpressionNode)args.get(i));
345             }
346             return con;
347         } else if (isNode(pn,"name")) {
348             NameDescriptor nd=parseName(pn);
349             return new NameNode(nd);
350         } else if (isNode(pn,"this")) {
351             NameDescriptor nd=new NameDescriptor("this");
352             return new NameNode(nd);
353         } else if (isNode(pn,"methodinvoke1")) {
354             NameDescriptor nd=parseName(pn.getChild("name"));
355             Vector args=parseArgumentList(pn);
356             MethodInvokeNode min=new MethodInvokeNode(nd);
357             for(int i=0;i<args.size();i++) {
358                 min.addArgument((ExpressionNode)args.get(i));
359             }
360             return min;
361         } else if (isNode(pn,"methodinvoke2")) {
362             String methodid=pn.getChild("id").getTerminal();
363             ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
364             Vector args=parseArgumentList(pn);
365             MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
366             for(int i=0;i<args.size();i++) {
367                 min.addArgument((ExpressionNode)args.get(i));
368             }
369             return min;
370         } else if (isNode(pn,"fieldaccess")) { 
371             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());         String fieldname=pn.getChild("field").getTerminal();
372             return new FieldAccessNode(en,fieldname);
373         } else if (isNode(pn,"arrayaccess")) { 
374             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
375             ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
376             return new ArrayAccessNode(en,index);
377         } else if (isNode(pn,"cast1")) { 
378             return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
379         } else if (isNode(pn,"cast2")) { 
380             return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
381         } else {
382             System.out.println("---------------------");
383             System.out.println(pn.PPrint(3,true));
384             throw new Error();
385         }
386     }
387
388     private Vector parseDimExprs(ParseNode pn) {
389         Vector arglist=new Vector();
390         ParseNode an=pn.getChild("dim_exprs");
391         if (an==null)   /* No argument list */
392             return arglist;
393         ParseNodeVector anv=an.getChildren();
394         for(int i=0;i<anv.size();i++) {
395             arglist.add(parseExpression(anv.elementAt(i)));
396         }
397         return arglist;
398     }
399
400     private Vector parseArgumentList(ParseNode pn) {
401         Vector arglist=new Vector();
402         ParseNode an=pn.getChild("argument_list");
403         if (an==null)   /* No argument list */
404             return arglist;
405         ParseNodeVector anv=an.getChildren();
406         for(int i=0;i<anv.size();i++) {
407             arglist.add(parseExpression(anv.elementAt(i)));
408         }
409         return arglist;
410     }
411
412     private ExpressionNode parseAssignmentExpression(ParseNode pn) {
413         AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
414         ParseNodeVector pnv=pn.getChild("args").getChildren();
415         
416         AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
417         return an;
418     }
419
420
421     private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
422         ParseNode headern=pn.getChild("method_header");
423         ParseNode bodyn=pn.getChild("body");
424         MethodDescriptor md=parseMethodHeader(headern);
425         BlockNode bn=parseBlock(bodyn);
426         cn.addMethod(md);
427         state.addTreeCode(md,bn);
428     }
429
430     private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
431         ParseNode mn=pn.getChild("modifiers");
432         Modifiers m=parseModifiersList(mn);
433         ParseNode cdecl=pn.getChild("constructor_declarator");
434         String name=cdecl.getChild("name").getChild("identifier").getTerminal();
435         MethodDescriptor md=new MethodDescriptor(m, name);
436         ParseNode paramnode=cdecl.getChild("parameters");
437         parseParameterList(md,paramnode);
438         ParseNode bodyn0=pn.getChild("body");
439         ParseNode bodyn=bodyn0.getChild("constructor_body");
440         cn.addMethod(md);
441         BlockNode bn=parseBlock(bodyn);
442         state.addTreeCode(md,bn);
443     }
444
445     public BlockNode parseBlock(ParseNode pn) {
446         if (pn==null||isEmpty(pn.getTerminal()))
447             return new BlockNode();
448         ParseNode bsn=pn.getChild("block_statement_list");
449         return parseBlockHelper(bsn);
450     }
451     
452     private BlockNode parseBlockHelper(ParseNode pn) {
453         ParseNodeVector pnv=pn.getChildren();
454         BlockNode bn=new BlockNode();
455         for(int i=0;i<pnv.size();i++) {
456             Vector bsv=parseBlockStatement(pnv.elementAt(i));
457             for(int j=0;j<bsv.size();j++) {
458                 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
459             }
460         }
461         return bn;
462     }
463
464     public BlockNode parseSingleBlock(ParseNode pn) {
465         BlockNode bn=new BlockNode();
466         Vector bsv=parseBlockStatement(pn);
467         for(int j=0;j<bsv.size();j++) {
468             bn.addBlockStatement((BlockStatementNode)bsv.get(j));
469         }
470         bn.setStyle(BlockNode.NOBRACES);
471         return bn;
472     }
473
474     public Vector parseBlockStatement(ParseNode pn) {
475         Vector blockstatements=new Vector();
476         if (isNode(pn,"local_variable_declaration")) {
477             TypeDescriptor t=parseTypeDescriptor(pn);
478             ParseNode vn=pn.getChild("variable_declarators_list");
479             ParseNodeVector pnv=vn.getChildren();
480             for(int i=0;i<pnv.size();i++) {
481                 ParseNode vardecl=pnv.elementAt(i);
482
483             
484                 ParseNode tmp=vardecl;
485                 TypeDescriptor arrayt=t;
486                 while (tmp.getChild("single")==null) {
487                     arrayt=arrayt.makeArray(state);
488                     tmp=tmp.getChild("array");
489                 }
490                 String identifier=tmp.getChild("single").getTerminal();
491                 
492                 ParseNode epn=vardecl.getChild("initializer");
493             
494
495                 ExpressionNode en=null;
496                 if (epn!=null)
497                     en=parseExpression(epn.getFirstChild());
498                 
499                 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
500             }
501         } else if (isNode(pn,"nop")) {
502             /* Do Nothing */
503         } else if (isNode(pn,"expression")) {
504             blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
505         } else if (isNode(pn,"ifstatement")) {
506             blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
507                                        parseSingleBlock(pn.getChild("statement").getFirstChild()),
508                                        pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
509         } else if (isNode(pn,"taskexit")) {
510             Vector vfe=null;
511             if (pn.getChild("flag_effects_list")!=null)
512                 vfe=parseFlags(pn.getChild("flag_effects_list"));
513             Vector ccs=null;
514             if (pn.getChild("cons_checks")!=null)
515                 ccs=parseChecks(pn.getChild("cons_checks"));
516             
517             blockstatements.add(new TaskExitNode(vfe, ccs));
518         } else if (isNode(pn,"return")) {
519             if (isEmpty(pn.getTerminal()))
520                 blockstatements.add(new ReturnNode());
521             else {
522                 ExpressionNode en=parseExpression(pn.getFirstChild());
523                 blockstatements.add(new ReturnNode(en));
524             }
525         } else if (isNode(pn,"block_statement_list")) {
526             BlockNode bn=parseBlockHelper(pn);
527             blockstatements.add(new SubBlockNode(bn));
528         } else if (isNode(pn,"empty")) {
529             /* nop */
530         } else if (isNode(pn,"statement_expression_list")) {
531             ParseNodeVector pnv=pn.getChildren();
532             BlockNode bn=new BlockNode();
533             for(int i=0;i<pnv.size();i++) {
534                 ExpressionNode en=parseExpression(pnv.elementAt(i));
535                 blockstatements.add(new BlockExpressionNode(en));
536             }
537             bn.setStyle(BlockNode.EXPRLIST);
538         } else if (isNode(pn,"forstatement")) {
539             BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
540             BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
541             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
542             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
543             blockstatements.add(new LoopNode(init,condition,update,body));
544         } else if (isNode(pn,"whilestatement")) {
545             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
546             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
547             blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
548         } else if (isNode(pn,"dowhilestatement")) {
549             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
550             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
551             blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
552         } else {
553             System.out.println("---------------");
554             System.out.println(pn.PPrint(3,true));
555             throw new Error();
556         }
557         return blockstatements;
558     }
559
560     public MethodDescriptor parseMethodHeader(ParseNode pn) {
561         ParseNode mn=pn.getChild("modifiers");
562         Modifiers m=parseModifiersList(mn);
563         
564         ParseNode tn=pn.getChild("returntype");
565         TypeDescriptor returntype;
566         if (tn!=null) 
567             returntype=parseTypeDescriptor(tn);
568         else
569             returntype=new TypeDescriptor(TypeDescriptor.VOID);
570
571         ParseNode pmd=pn.getChild("method_declarator");
572         String name=pmd.getChild("name").getTerminal();
573         MethodDescriptor md=new MethodDescriptor(m, returntype, name);
574        
575         ParseNode paramnode=pmd.getChild("parameters");
576         parseParameterList(md,paramnode);
577         return md;
578     }
579
580     public void parseParameterList(MethodDescriptor md, ParseNode pn) {
581         ParseNode paramlist=pn.getChild("formal_parameter_list");
582         if (paramlist==null)
583             return;
584          ParseNodeVector pnv=paramlist.getChildren();
585          for(int i=0;i<pnv.size();i++) {
586              ParseNode paramn=pnv.elementAt(i);
587              TypeDescriptor type=parseTypeDescriptor(paramn);
588
589              ParseNode tmp=paramn;
590              while (tmp.getChild("single")==null) {
591                  type=type.makeArray(state);
592                  tmp=tmp.getChild("array");
593              }
594              String paramname=tmp.getChild("single").getTerminal();
595             
596              md.addParameter(type,paramname);
597          }
598     }
599
600     public Modifiers parseModifiersList(ParseNode pn) {
601         Modifiers m=new Modifiers();
602         ParseNode modlist=pn.getChild("modifier_list");
603         if (modlist!=null) {
604             ParseNodeVector pnv=modlist.getChildren();
605             for(int i=0;i<pnv.size();i++) {
606                 ParseNode modn=pnv.elementAt(i);
607                 if (isNode(modn,"public"))
608                     m.addModifier(Modifiers.PUBLIC);
609                 if (isNode(modn,"protected"))
610                     m.addModifier(Modifiers.PROTECTED);
611                 if (isNode(modn,"private"))
612                     m.addModifier(Modifiers.PRIVATE);
613                 if (isNode(modn,"static"))
614                     m.addModifier(Modifiers.STATIC);
615                 if (isNode(modn,"final"))
616                     m.addModifier(Modifiers.FINAL);
617                 if (isNode(modn,"native"))
618                     m.addModifier(Modifiers.NATIVE);
619             }
620         }
621         return m;
622     }
623
624     private boolean isNode(ParseNode pn, String label) {
625         if (pn.getLabel().equals(label))
626             return true;
627         else return false;
628     }
629
630     private static boolean isEmpty(ParseNode pn) {
631         if (pn.getLabel().equals("empty"))
632             return true;
633         else
634             return false;
635     }
636
637     private static boolean isEmpty(String s) {
638         if (s.equals("empty"))
639             return true;
640         else
641             return false;
642     }
643
644     /** Throw an exception if something is unexpected */
645     private void check(ParseNode pn, String label) {
646         if (pn == null) {
647             throw new Error(pn+ "IE: Expected '" + label + "', got null");
648         }
649         if (! pn.getLabel().equals(label)) {
650             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
651         }
652     }
653 }