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