9 private int m_taskexitnum;
11 public BuildIR(State state) {
13 this.m_taskexitnum = 0;
16 public void buildtree() {
17 for(Iterator it=state.parsetrees.iterator(); it.hasNext();) {
18 ParseNode pn=(ParseNode)it.next();
23 /** Parse the classes in this file */
24 public void parseFile(ParseNode pn) {
25 NameDescriptor packages;
26 Vector singleimports=new Vector();
27 Vector multiimports=new Vector();
29 ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
31 ParseNodeVector pnv=ipn.getChildren();
32 for(int i=0; i<pnv.size(); i++) {
33 ParseNode pnimport=pnv.elementAt(i);
34 NameDescriptor nd=parseName(pnimport.getChild("name"));
35 if (isNode(pnimport,"import_single"))
36 singleimports.add(nd);
41 ParseNode ppn=pn.getChild("packages").getChild("package");
43 packages=parseName(pn.getChild("name"));
45 ParseNode tpn=pn.getChild("type_declaration_list");
47 ParseNodeVector pnv=tpn.getChildren();
48 for(int i=0; i<pnv.size(); i++) {
49 ParseNode type_pn=pnv.elementAt(i);
50 if (isEmpty(type_pn)) /* Skip the semicolon */
52 if (isNode(type_pn,"class_declaration")) {
53 ClassDescriptor cn=parseTypeDecl(type_pn);
55 } else if (isNode(type_pn,"task_declaration")) {
56 TaskDescriptor td=parseTaskDecl(type_pn);
59 throw new Error(type_pn.getLabel());
65 public TaskDescriptor parseTaskDecl(ParseNode pn) {
66 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
67 ParseNode bodyn=pn.getChild("body");
68 BlockNode bn=parseBlock(bodyn);
69 parseParameterList(td, pn);
70 state.addTreeCode(td,bn);
71 if (pn.getChild("flag_effects_list")!=null)
72 td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
76 public Vector parseFlags(ParseNode pn) {
77 Vector vfe=new Vector();
78 ParseNodeVector pnv=pn.getChildren();
79 for(int i=0; i<pnv.size(); i++) {
80 ParseNode fn=pnv.elementAt(i);
81 FlagEffects fe=parseFlagEffects(fn);
87 public FlagEffects parseFlagEffects(ParseNode pn) {
88 if (isNode(pn,"flag_effect")) {
89 String flagname=pn.getChild("name").getTerminal();
90 FlagEffects fe=new FlagEffects(flagname);
91 if (pn.getChild("flag_list")!=null)
92 parseFlagEffect(fe, pn.getChild("flag_list"));
93 if (pn.getChild("tag_list")!=null)
94 parseTagEffect(fe, pn.getChild("tag_list"));
96 } else throw new Error();
99 public void parseTagEffect(FlagEffects fes, ParseNode pn) {
100 ParseNodeVector pnv=pn.getChildren();
101 for(int i=0; i<pnv.size(); i++) {
102 ParseNode pn2=pnv.elementAt(i);
104 if (isNode(pn2,"not")) {
106 pn2=pn2.getChild("name");
108 String name=pn2.getTerminal();
109 fes.addTagEffect(new TagEffect(name,status));
113 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
114 ParseNodeVector pnv=pn.getChildren();
115 for(int i=0; i<pnv.size(); i++) {
116 ParseNode pn2=pnv.elementAt(i);
118 if (isNode(pn2,"not")) {
120 pn2=pn2.getChild("name");
122 String name=pn2.getTerminal();
123 fes.addEffect(new FlagEffect(name,status));
127 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
128 if (isNode(pn,"or")) {
129 ParseNodeVector pnv=pn.getChildren();
130 ParseNode left=pnv.elementAt(0);
131 ParseNode right=pnv.elementAt(1);
132 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
133 } else if (isNode(pn,"and")) {
134 ParseNodeVector pnv=pn.getChildren();
135 ParseNode left=pnv.elementAt(0);
136 ParseNode right=pnv.elementAt(1);
137 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
138 } else if (isNode(pn, "not")) {
139 ParseNodeVector pnv=pn.getChildren();
140 ParseNode left=pnv.elementAt(0);
141 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
143 } else if (isNode(pn,"name")) {
144 return new FlagNode(pn.getTerminal());
150 public Vector parseChecks(ParseNode pn) {
151 Vector ccs=new Vector();
152 ParseNodeVector pnv=pn.getChildren();
153 for(int i=0; i<pnv.size(); i++) {
154 ParseNode fn=pnv.elementAt(i);
155 ConstraintCheck cc=parseConstraintCheck(fn);
161 public ConstraintCheck parseConstraintCheck(ParseNode pn) {
162 if (isNode(pn,"cons_check")) {
163 String specname=pn.getChild("name").getChild("identifier").getTerminal();
164 Vector[] args=parseConsArgumentList(pn);
165 ConstraintCheck cc=new ConstraintCheck(specname);
166 for(int i=0; i<args[0].size(); i++) {
167 cc.addVariable((String)args[0].get(i));
168 cc.addArgument((ExpressionNode)args[1].get(i));
171 } else throw new Error();
174 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
177 ParseNode paramlist=pn.getChild("task_parameter_list");
180 ParseNodeVector pnv=paramlist.getChildren();
181 for(int i=0; i<pnv.size(); i++) {
182 ParseNode paramn=pnv.elementAt(i);
183 if(paramn.getChild("optional")!=null) {
185 paramn = paramn.getChild("optional").getFirstChild();
186 System.out.println("OPTIONAL FOUND!!!!!!!");
187 } else { optional = false;
188 System.out.println("NOT OPTIONAL");}
190 TypeDescriptor type=parseTypeDescriptor(paramn);
192 String paramname=paramn.getChild("single").getTerminal();
193 FlagExpressionNode fen=null;
194 if (paramn.getChild("flag")!=null)
195 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
197 ParseNode tagnode=paramn.getChild("tag");
199 TagExpressionList tel=null;
201 tel=parseTagExpressionList(tagnode);
204 td.addParameter(type,paramname,fen, tel, optional);
208 public TagExpressionList parseTagExpressionList(ParseNode pn) {
209 //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
210 //To test, feed in any input program that uses tags
211 ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
212 TagExpressionList tel=new TagExpressionList();
213 for(int i=0; i<pnv.size(); i++) {
214 ParseNode tn=pnv.elementAt(i);
215 String type=tn.getChild("type").getTerminal();
216 String name=tn.getChild("single").getTerminal();
217 tel.addTag(type, name);
222 public ClassDescriptor parseTypeDecl(ParseNode pn) {
223 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
224 if (!isEmpty(pn.getChild("super").getTerminal())) {
225 /* parse superclass name */
226 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
227 NameDescriptor nd=parseName(snn);
228 cn.setSuper(nd.toString());
230 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
231 cn.getSymbol().equals(TypeUtil.TagClass)))
232 cn.setSuper(TypeUtil.ObjectClass);
234 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
235 parseClassBody(cn, pn.getChild("classbody"));
239 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
240 ParseNode decls=pn.getChild("class_body_declaration_list");
242 ParseNodeVector pnv=decls.getChildren();
243 for(int i=0; i<pnv.size(); i++) {
244 ParseNode decl=pnv.elementAt(i);
245 if (isNode(decl,"member")) {
246 parseClassMember(cn,decl);
247 } else if (isNode(decl,"constructor")) {
248 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
249 } else if (isNode(decl,"block")) {
250 } else throw new Error();
255 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
256 ParseNode fieldnode=pn.getChild("field");
258 if (fieldnode!=null) {
259 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
262 ParseNode methodnode=pn.getChild("method");
263 if (methodnode!=null) {
264 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
267 ParseNode flagnode=pn.getChild("flag");
268 if (flagnode!=null) {
269 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
275 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
276 ParseNode tn=pn.getChild("type");
277 String type_st=tn.getTerminal();
278 if(type_st.equals("byte")) {
279 return state.getTypeDescriptor(TypeDescriptor.BYTE);
280 } else if(type_st.equals("short")) {
281 return state.getTypeDescriptor(TypeDescriptor.SHORT);
282 } else if(type_st.equals("boolean")) {
283 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
284 } else if(type_st.equals("int")) {
285 return state.getTypeDescriptor(TypeDescriptor.INT);
286 } else if(type_st.equals("long")) {
287 return state.getTypeDescriptor(TypeDescriptor.LONG);
288 } else if(type_st.equals("char")) {
289 return state.getTypeDescriptor(TypeDescriptor.CHAR);
290 } else if(type_st.equals("float")) {
291 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
292 } else if(type_st.equals("double")) {
293 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
294 } else if(type_st.equals("class")) {
295 ParseNode nn=tn.getChild("class");
296 return state.getTypeDescriptor(parseName(nn.getChild("name")));
297 } else if(type_st.equals("array")) {
298 ParseNode nn=tn.getChild("array");
299 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
300 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
301 for(int i=0; i<numdims.intValue(); i++)
302 td=td.makeArray(state);
309 private NameDescriptor parseName(ParseNode nn) {
310 ParseNode base=nn.getChild("base");
311 ParseNode id=nn.getChild("identifier");
313 return new NameDescriptor(id.getTerminal());
314 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
318 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
319 String name=pn.getChild("name").getTerminal();
320 FlagDescriptor flag=new FlagDescriptor(name);
321 if (pn.getChild("external")!=null)
326 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
327 ParseNode mn=pn.getChild("modifier");
328 Modifiers m=parseModifiersList(mn);
330 ParseNode tn=pn.getChild("type");
331 TypeDescriptor t=parseTypeDescriptor(tn);
332 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
333 ParseNodeVector pnv=vn.getChildren();
334 boolean isglobal=pn.getChild("global")!=null;
336 for(int i=0; i<pnv.size(); i++) {
337 ParseNode vardecl=pnv.elementAt(i);
338 ParseNode tmp=vardecl;
339 TypeDescriptor arrayt=t;
340 while (tmp.getChild("single")==null) {
341 arrayt=arrayt.makeArray(state);
342 tmp=tmp.getChild("array");
344 String identifier=tmp.getChild("single").getTerminal();
345 ParseNode epn=vardecl.getChild("initializer");
347 ExpressionNode en=null;
349 en=parseExpression(epn.getFirstChild());
351 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
355 private ExpressionNode parseExpression(ParseNode pn) {
356 if (isNode(pn,"assignment"))
357 return parseAssignmentExpression(pn);
358 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
359 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
360 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
361 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
362 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
363 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
364 isNode(pn,"rightshift")||isNode(pn,"sub")||
365 isNode(pn,"urightshift")||isNode(pn,"sub")||
366 isNode(pn,"add")||isNode(pn,"mult")||
367 isNode(pn,"div")||isNode(pn,"mod")) {
368 ParseNodeVector pnv=pn.getChildren();
369 ParseNode left=pnv.elementAt(0);
370 ParseNode right=pnv.elementAt(1);
371 Operation op=new Operation(pn.getLabel());
372 return new OpNode(parseExpression(left),parseExpression(right),op);
373 } else if (isNode(pn,"unaryplus")||
374 isNode(pn,"unaryminus")||
377 ParseNode left=pn.getFirstChild();
378 Operation op=new Operation(pn.getLabel());
379 return new OpNode(parseExpression(left),op);
380 } else if (isNode(pn,"postinc")||
381 isNode(pn,"postdec")) {
382 ParseNode left=pn.getFirstChild();
383 AssignOperation op=new AssignOperation(pn.getLabel());
384 return new AssignmentNode(parseExpression(left),null,op);
386 } else if (isNode(pn,"preinc")||
387 isNode(pn,"predec")) {
388 ParseNode left=pn.getFirstChild();
389 AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
390 return new AssignmentNode(parseExpression(left),
391 new LiteralNode("integer",new Integer(1)),op);
392 } else if (isNode(pn,"literal")) {
393 String literaltype=pn.getTerminal();
394 ParseNode literalnode=pn.getChild(literaltype);
395 Object literal_obj=literalnode.getLiteral();
396 return new LiteralNode(literaltype, literal_obj);
397 } else if (isNode(pn,"createobject")) {
398 TypeDescriptor td=parseTypeDescriptor(pn);
399 Vector args=parseArgumentList(pn);
400 boolean isglobal=pn.getChild("global")!=null;
401 CreateObjectNode con=new CreateObjectNode(td, isglobal);
402 for(int i=0; i<args.size(); i++) {
403 con.addArgument((ExpressionNode)args.get(i));
405 /* Could have flag set or tag added here */
406 if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
407 FlagEffects fe=new FlagEffects(null);
408 if (pn.getChild("flag_list")!=null)
409 parseFlagEffect(fe, pn.getChild("flag_list"));
411 if (pn.getChild("tag_list")!=null)
412 parseTagEffect(fe, pn.getChild("tag_list"));
413 con.addFlagEffects(fe);
417 } else if (isNode(pn,"createarray")) {
418 //System.out.println(pn.PPrint(3,true));
419 boolean isglobal=pn.getChild("global")!=null;
420 TypeDescriptor td=parseTypeDescriptor(pn);
421 Vector args=parseDimExprs(pn);
423 if (pn.getChild("dims_opt").getLiteral()!=null)
424 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
425 for(int i=0; i<(args.size()+num); i++)
426 td=td.makeArray(state);
427 CreateObjectNode con=new CreateObjectNode(td, isglobal);
428 for(int i=0; i<args.size(); i++) {
429 con.addArgument((ExpressionNode)args.get(i));
432 } else if (isNode(pn,"name")) {
433 NameDescriptor nd=parseName(pn);
434 return new NameNode(nd);
435 } else if (isNode(pn,"this")) {
436 NameDescriptor nd=new NameDescriptor("this");
437 return new NameNode(nd);
438 } else if (isNode(pn,"isavailable")) {
439 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
440 return new OpNode(new NameNode(nd),null,new Operation(Operation.ISAVAILABLE));
441 } else if (isNode(pn,"methodinvoke1")) {
442 NameDescriptor nd=parseName(pn.getChild("name"));
443 Vector args=parseArgumentList(pn);
444 MethodInvokeNode min=new MethodInvokeNode(nd);
445 for(int i=0; i<args.size(); i++) {
446 min.addArgument((ExpressionNode)args.get(i));
449 } else if (isNode(pn,"methodinvoke2")) {
450 String methodid=pn.getChild("id").getTerminal();
451 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
452 Vector args=parseArgumentList(pn);
453 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
454 for(int i=0; i<args.size(); i++) {
455 min.addArgument((ExpressionNode)args.get(i));
458 } else if (isNode(pn,"fieldaccess")) {
459 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
460 String fieldname=pn.getChild("field").getTerminal();
461 return new FieldAccessNode(en,fieldname);
462 } else if (isNode(pn,"arrayaccess")) {
463 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
464 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
465 return new ArrayAccessNode(en,index);
466 } else if (isNode(pn,"cast1")) {
467 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
468 } else if (isNode(pn,"cast2")) {
469 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
470 } else if (isNode(pn, "getoffset")) {
471 TypeDescriptor td=parseTypeDescriptor(pn);
472 String fieldname = pn.getChild("field").getTerminal();
473 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
474 return new OffsetNode(td, fieldname);
476 System.out.println("---------------------");
477 System.out.println(pn.PPrint(3,true));
482 private Vector parseDimExprs(ParseNode pn) {
483 Vector arglist=new Vector();
484 ParseNode an=pn.getChild("dim_exprs");
485 if (an==null) /* No argument list */
487 ParseNodeVector anv=an.getChildren();
488 for(int i=0; i<anv.size(); i++) {
489 arglist.add(parseExpression(anv.elementAt(i)));
494 private Vector parseArgumentList(ParseNode pn) {
495 Vector arglist=new Vector();
496 ParseNode an=pn.getChild("argument_list");
497 if (an==null) /* No argument list */
499 ParseNodeVector anv=an.getChildren();
500 for(int i=0; i<anv.size(); i++) {
501 arglist.add(parseExpression(anv.elementAt(i)));
506 private Vector[] parseConsArgumentList(ParseNode pn) {
507 Vector arglist=new Vector();
508 Vector varlist=new Vector();
509 ParseNode an=pn.getChild("cons_argument_list");
510 if (an==null) /* No argument list */
511 return new Vector[] {varlist, arglist};
512 ParseNodeVector anv=an.getChildren();
513 for(int i=0; i<anv.size(); i++) {
514 ParseNode cpn=anv.elementAt(i);
515 ParseNode var=cpn.getChild("var");
516 ParseNode exp=cpn.getChild("exp").getFirstChild();
517 varlist.add(var.getTerminal());
518 arglist.add(parseExpression(exp));
520 return new Vector[] {varlist, arglist};
523 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
524 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
525 ParseNodeVector pnv=pn.getChild("args").getChildren();
527 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
532 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
533 ParseNode headern=pn.getChild("method_header");
534 ParseNode bodyn=pn.getChild("body");
535 MethodDescriptor md=parseMethodHeader(headern);
536 BlockNode bn=parseBlock(bodyn);
538 state.addTreeCode(md,bn);
541 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
542 ParseNode mn=pn.getChild("modifiers");
543 Modifiers m=parseModifiersList(mn);
544 ParseNode cdecl=pn.getChild("constructor_declarator");
545 boolean isglobal=cdecl.getChild("global")!=null;
546 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
547 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
548 ParseNode paramnode=cdecl.getChild("parameters");
549 parseParameterList(md,paramnode);
550 ParseNode bodyn0=pn.getChild("body");
551 ParseNode bodyn=bodyn0.getChild("constructor_body");
554 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
555 bn=parseBlock(bodyn);
558 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
559 ParseNode sin=bodyn.getChild("superinvoke");
560 NameDescriptor nd=new NameDescriptor("super");
561 Vector args=parseArgumentList(sin);
562 MethodInvokeNode min=new MethodInvokeNode(nd);
563 for(int i=0; i<args.size(); i++) {
564 min.addArgument((ExpressionNode)args.get(i));
566 BlockExpressionNode ben=new BlockExpressionNode(min);
567 bn.addFirstBlockStatement(ben);
569 state.addTreeCode(md,bn);
572 public BlockNode parseBlock(ParseNode pn) {
573 this.m_taskexitnum = 0;
574 if (pn==null||isEmpty(pn.getTerminal()))
575 return new BlockNode();
576 ParseNode bsn=pn.getChild("block_statement_list");
577 return parseBlockHelper(bsn);
580 private BlockNode parseBlockHelper(ParseNode pn) {
581 ParseNodeVector pnv=pn.getChildren();
582 BlockNode bn=new BlockNode();
583 for(int i=0; i<pnv.size(); i++) {
584 Vector bsv=parseBlockStatement(pnv.elementAt(i));
585 for(int j=0; j<bsv.size(); j++) {
586 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
592 public BlockNode parseSingleBlock(ParseNode pn) {
593 BlockNode bn=new BlockNode();
594 Vector bsv=parseBlockStatement(pn);
595 for(int j=0; j<bsv.size(); j++) {
596 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
598 bn.setStyle(BlockNode.NOBRACES);
602 public Vector parseBlockStatement(ParseNode pn) {
603 Vector blockstatements=new Vector();
604 if (isNode(pn,"tag_declaration")) {
605 String name=pn.getChild("single").getTerminal();
606 String type=pn.getChild("type").getTerminal();
608 blockstatements.add(new TagDeclarationNode(name, type));
609 } else if (isNode(pn,"local_variable_declaration")) {
610 TypeDescriptor t=parseTypeDescriptor(pn);
611 ParseNode vn=pn.getChild("variable_declarators_list");
612 ParseNodeVector pnv=vn.getChildren();
613 for(int i=0; i<pnv.size(); i++) {
614 ParseNode vardecl=pnv.elementAt(i);
617 ParseNode tmp=vardecl;
618 TypeDescriptor arrayt=t;
619 while (tmp.getChild("single")==null) {
620 arrayt=arrayt.makeArray(state);
621 tmp=tmp.getChild("array");
623 String identifier=tmp.getChild("single").getTerminal();
625 ParseNode epn=vardecl.getChild("initializer");
628 ExpressionNode en=null;
630 en=parseExpression(epn.getFirstChild());
632 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
634 } else if (isNode(pn,"nop")) {
636 } else if (isNode(pn,"expression")) {
637 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
638 } else if (isNode(pn,"ifstatement")) {
639 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
640 parseSingleBlock(pn.getChild("statement").getFirstChild()),
641 pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null));
642 } else if (isNode(pn,"taskexit")) {
644 if (pn.getChild("flag_effects_list")!=null)
645 vfe=parseFlags(pn.getChild("flag_effects_list"));
647 if (pn.getChild("cons_checks")!=null)
648 ccs=parseChecks(pn.getChild("cons_checks"));
650 blockstatements.add(new TaskExitNode(vfe, ccs, this.m_taskexitnum++));
651 } else if (isNode(pn,"atomic")) {
652 BlockNode bn=parseBlockHelper(pn);
653 blockstatements.add(new AtomicNode(bn));
654 } else if (isNode(pn,"return")) {
655 if (isEmpty(pn.getTerminal()))
656 blockstatements.add(new ReturnNode());
658 ExpressionNode en=parseExpression(pn.getFirstChild());
659 blockstatements.add(new ReturnNode(en));
661 } else if (isNode(pn,"block_statement_list")) {
662 BlockNode bn=parseBlockHelper(pn);
663 blockstatements.add(new SubBlockNode(bn));
664 } else if (isNode(pn,"empty")) {
666 } else if (isNode(pn,"statement_expression_list")) {
667 ParseNodeVector pnv=pn.getChildren();
668 BlockNode bn=new BlockNode();
669 for(int i=0; i<pnv.size(); i++) {
670 ExpressionNode en=parseExpression(pnv.elementAt(i));
671 blockstatements.add(new BlockExpressionNode(en));
673 bn.setStyle(BlockNode.EXPRLIST);
674 } else if (isNode(pn,"forstatement")) {
675 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
676 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
677 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
678 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
679 blockstatements.add(new LoopNode(init,condition,update,body));
680 } else if (isNode(pn,"whilestatement")) {
681 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
682 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
683 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
684 } else if (isNode(pn,"dowhilestatement")) {
685 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
686 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
687 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
689 System.out.println("---------------");
690 System.out.println(pn.PPrint(3,true));
693 return blockstatements;
696 public MethodDescriptor parseMethodHeader(ParseNode pn) {
697 ParseNode mn=pn.getChild("modifiers");
698 Modifiers m=parseModifiersList(mn);
700 ParseNode tn=pn.getChild("returntype");
701 TypeDescriptor returntype;
703 returntype=parseTypeDescriptor(tn);
705 returntype=new TypeDescriptor(TypeDescriptor.VOID);
707 ParseNode pmd=pn.getChild("method_declarator");
708 String name=pmd.getChild("name").getTerminal();
709 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
711 ParseNode paramnode=pmd.getChild("parameters");
712 parseParameterList(md,paramnode);
716 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
717 ParseNode paramlist=pn.getChild("formal_parameter_list");
720 ParseNodeVector pnv=paramlist.getChildren();
721 for(int i=0; i<pnv.size(); i++) {
722 ParseNode paramn=pnv.elementAt(i);
724 if (isNode(paramn, "tag_parameter")) {
725 String paramname=paramn.getChild("single").getTerminal();
726 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
727 md.addTagParameter(type, paramname);
729 TypeDescriptor type=parseTypeDescriptor(paramn);
731 ParseNode tmp=paramn;
732 while (tmp.getChild("single")==null) {
733 type=type.makeArray(state);
734 tmp=tmp.getChild("array");
736 String paramname=tmp.getChild("single").getTerminal();
738 md.addParameter(type, paramname);
743 public Modifiers parseModifiersList(ParseNode pn) {
744 Modifiers m=new Modifiers();
745 ParseNode modlist=pn.getChild("modifier_list");
747 ParseNodeVector pnv=modlist.getChildren();
748 for(int i=0; i<pnv.size(); i++) {
749 ParseNode modn=pnv.elementAt(i);
750 if (isNode(modn,"public"))
751 m.addModifier(Modifiers.PUBLIC);
752 else if (isNode(modn,"protected"))
753 m.addModifier(Modifiers.PROTECTED);
754 else if (isNode(modn,"private"))
755 m.addModifier(Modifiers.PRIVATE);
756 else if (isNode(modn,"static"))
757 m.addModifier(Modifiers.STATIC);
758 else if (isNode(modn,"final"))
759 m.addModifier(Modifiers.FINAL);
760 else if (isNode(modn,"native"))
761 m.addModifier(Modifiers.NATIVE);
762 else if (isNode(modn,"synchronized"))
763 m.addModifier(Modifiers.SYNCHRONIZED);
764 else if (isNode(modn,"atomic"))
765 m.addModifier(Modifiers.ATOMIC);
766 else throw new Error("Unrecognized Modifier");
772 private boolean isNode(ParseNode pn, String label) {
773 if (pn.getLabel().equals(label))
778 private static boolean isEmpty(ParseNode pn) {
779 if (pn.getLabel().equals("empty"))
785 private static boolean isEmpty(String s) {
786 if (s.equals("empty"))
792 /** Throw an exception if something is unexpected */
793 private void check(ParseNode pn, String label) {
795 throw new Error(pn+ "IE: Expected '" + label + "', got null");
797 if (!pn.getLabel().equals(label)) {
798 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");