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