10 private int m_taskexitnum;
12 public BuildIR(State state) {
14 this.m_taskexitnum = 0;
17 public void buildtree(ParseNode pn, Set toanalyze) {
18 parseFile(pn, toanalyze);
23 NameDescriptor packages;
25 /** Parse the classes in this file */
26 public void parseFile(ParseNode pn, Set toanalyze) {
27 singleimports=new Vector();
28 multiimports=new Vector();
30 ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
32 ParseNodeVector pnv=ipn.getChildren();
33 for(int i=0; i<pnv.size(); i++) {
34 ParseNode pnimport=pnv.elementAt(i);
35 NameDescriptor nd=parseName(pnimport.getChild("name"));
36 if (isNode(pnimport,"import_single"))
37 singleimports.add(nd);
42 ParseNode ppn=pn.getChild("packages").getChild("package");
44 packages=parseName(ppn.getChild("name"));
46 ParseNode tpn=pn.getChild("type_declaration_list");
48 ParseNodeVector pnv=tpn.getChildren();
49 for(int i=0; i<pnv.size(); i++) {
50 ParseNode type_pn=pnv.elementAt(i);
51 if (isEmpty(type_pn)) /* Skip the semicolon */
53 if (isNode(type_pn,"class_declaration")) {
54 ClassDescriptor cn=parseTypeDecl(type_pn);
60 // TODO add version for normal Java later
61 HashSet tovisit = new HashSet();
62 Iterator it_icds = cn.getInnerClasses();
63 while(it_icds.hasNext()) {
64 tovisit.add(it_icds.next());
67 while(!tovisit.isEmpty()) {
68 ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
71 if(toanalyze != null) {
76 Iterator it_ics = cd.getInnerClasses();
77 while(it_ics.hasNext()) {
78 tovisit.add(it_ics.next());
82 } else if (isNode(type_pn,"task_declaration")) {
83 TaskDescriptor td=parseTaskDecl(type_pn);
87 } else if ((state.MGC) && isNode(type_pn,"interface_declaration")) {
88 // TODO add version for normal Java later
89 ClassDescriptor cn = parseInterfaceDecl(type_pn);
94 throw new Error(type_pn.getLabel());
100 public ClassDescriptor parseInterfaceDecl(ParseNode pn) {
101 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
102 //cn.setAsInterface();
103 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
104 /* parse inherited interface name */
105 ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
106 ParseNodeVector pnv=snlist.getChildren();
107 for(int i=0; i<pnv.size(); i++) {
108 ParseNode decl=pnv.elementAt(i);
109 if (isNode(decl,"type")) {
110 NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
111 cn.addSuperInterface(nd.toString());
115 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
116 parseInterfaceBody(cn, pn.getChild("interfacebody"));
120 private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
121 assert(cn.isInterface());
122 ParseNode decls=pn.getChild("interface_member_declaration_list");
124 ParseNodeVector pnv=decls.getChildren();
125 for(int i=0; i<pnv.size(); i++) {
126 ParseNode decl=pnv.elementAt(i);
127 if (isNode(decl,"constant")) {
128 parseInterfaceConstant(cn,decl);
129 } else if (isNode(decl,"method")) {
130 parseInterfaceMethod(cn,decl.getChild("method_declaration"));
131 } else throw new Error();
136 private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
138 parseFieldDecl(cn,pn.getChild("field_declaration"));
144 private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
145 ParseNode headern=pn.getChild("header");
146 ParseNode bodyn=pn.getChild("body");
147 MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
148 md.getModifiers().addModifier(Modifiers.PUBLIC);
149 md.getModifiers().addModifier(Modifiers.ABSTRACT);
151 BlockNode bn=parseBlock(bodyn);
153 state.addTreeCode(md,bn);
155 // this is a hack for investigating new language features
156 // at the AST level, someday should evolve into a nice compiler
158 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
159 // md.getSymbol().equals( ***put your method in here like: "main" )
161 // bn.setStyle( BlockNode.NORMAL );
162 // System.out.println( bn.printNode( 0 ) );
165 } catch (Exception e) {
166 System.out.println("Error with method:"+md.getSymbol());
170 System.out.println("Error with method:"+md.getSymbol());
176 public TaskDescriptor parseTaskDecl(ParseNode pn) {
177 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
178 ParseNode bodyn=pn.getChild("body");
179 BlockNode bn=parseBlock(bodyn);
180 parseParameterList(td, pn);
181 state.addTreeCode(td,bn);
182 if (pn.getChild("flag_effects_list")!=null)
183 td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
187 public Vector parseFlags(ParseNode pn) {
188 Vector vfe=new Vector();
189 ParseNodeVector pnv=pn.getChildren();
190 for(int i=0; i<pnv.size(); i++) {
191 ParseNode fn=pnv.elementAt(i);
192 FlagEffects fe=parseFlagEffects(fn);
198 public FlagEffects parseFlagEffects(ParseNode pn) {
199 if (isNode(pn,"flag_effect")) {
200 String flagname=pn.getChild("name").getTerminal();
201 FlagEffects fe=new FlagEffects(flagname);
202 if (pn.getChild("flag_list")!=null)
203 parseFlagEffect(fe, pn.getChild("flag_list"));
204 if (pn.getChild("tag_list")!=null)
205 parseTagEffect(fe, pn.getChild("tag_list"));
207 } else throw new Error();
210 public void parseTagEffect(FlagEffects fes, ParseNode pn) {
211 ParseNodeVector pnv=pn.getChildren();
212 for(int i=0; i<pnv.size(); i++) {
213 ParseNode pn2=pnv.elementAt(i);
215 if (isNode(pn2,"not")) {
217 pn2=pn2.getChild("name");
219 String name=pn2.getTerminal();
220 fes.addTagEffect(new TagEffect(name,status));
224 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
225 ParseNodeVector pnv=pn.getChildren();
226 for(int i=0; i<pnv.size(); i++) {
227 ParseNode pn2=pnv.elementAt(i);
229 if (isNode(pn2,"not")) {
231 pn2=pn2.getChild("name");
233 String name=pn2.getTerminal();
234 fes.addEffect(new FlagEffect(name,status));
238 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
239 if (isNode(pn,"or")) {
240 ParseNodeVector pnv=pn.getChildren();
241 ParseNode left=pnv.elementAt(0);
242 ParseNode right=pnv.elementAt(1);
243 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
244 } else if (isNode(pn,"and")) {
245 ParseNodeVector pnv=pn.getChildren();
246 ParseNode left=pnv.elementAt(0);
247 ParseNode right=pnv.elementAt(1);
248 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
249 } else if (isNode(pn, "not")) {
250 ParseNodeVector pnv=pn.getChildren();
251 ParseNode left=pnv.elementAt(0);
252 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
254 } else if (isNode(pn,"name")) {
255 return new FlagNode(pn.getTerminal());
261 public Vector parseChecks(ParseNode pn) {
262 Vector ccs=new Vector();
263 ParseNodeVector pnv=pn.getChildren();
264 for(int i=0; i<pnv.size(); i++) {
265 ParseNode fn=pnv.elementAt(i);
266 ConstraintCheck cc=parseConstraintCheck(fn);
272 public ConstraintCheck parseConstraintCheck(ParseNode pn) {
273 if (isNode(pn,"cons_check")) {
274 String specname=pn.getChild("name").getChild("identifier").getTerminal();
275 Vector[] args=parseConsArgumentList(pn);
276 ConstraintCheck cc=new ConstraintCheck(specname);
277 for(int i=0; i<args[0].size(); i++) {
278 cc.addVariable((String)args[0].get(i));
279 cc.addArgument((ExpressionNode)args[1].get(i));
282 } else throw new Error();
285 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
288 ParseNode paramlist=pn.getChild("task_parameter_list");
291 ParseNodeVector pnv=paramlist.getChildren();
292 for(int i=0; i<pnv.size(); i++) {
293 ParseNode paramn=pnv.elementAt(i);
294 if(paramn.getChild("optional")!=null) {
296 paramn = paramn.getChild("optional").getFirstChild();
297 System.out.println("OPTIONAL FOUND!!!!!!!");
298 } else { optional = false;
299 System.out.println("NOT OPTIONAL");}
301 TypeDescriptor type=parseTypeDescriptor(paramn);
303 String paramname=paramn.getChild("single").getTerminal();
304 FlagExpressionNode fen=null;
305 if (paramn.getChild("flag")!=null)
306 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
308 ParseNode tagnode=paramn.getChild("tag");
310 TagExpressionList tel=null;
312 tel=parseTagExpressionList(tagnode);
315 td.addParameter(type,paramname,fen, tel, optional);
319 public TagExpressionList parseTagExpressionList(ParseNode pn) {
320 //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
321 //To test, feed in any input program that uses tags
322 ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
323 TagExpressionList tel=new TagExpressionList();
324 for(int i=0; i<pnv.size(); i++) {
325 ParseNode tn=pnv.elementAt(i);
326 String type=tn.getChild("type").getTerminal();
327 String name=tn.getChild("single").getTerminal();
328 tel.addTag(type, name);
333 public ClassDescriptor parseTypeDecl(ParseNode pn) {
334 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
335 if (!isEmpty(pn.getChild("super").getTerminal())) {
336 /* parse superclass name */
337 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
338 NameDescriptor nd=parseName(snn);
339 cn.setSuper(nd.toString());
341 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
342 cn.getSymbol().equals(TypeUtil.TagClass)))
343 cn.setSuper(TypeUtil.ObjectClass);
345 // check inherited interfaces
346 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
347 /* parse inherited interface name */
348 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
349 ParseNodeVector pnv=snlist.getChildren();
350 for(int i=0; i<pnv.size(); i++) {
351 ParseNode decl=pnv.elementAt(i);
352 if (isNode(decl,"type")) {
353 NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
354 cn.addSuperInterface(nd.toString());
358 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
359 parseClassBody(cn, pn.getChild("classbody"));
363 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
364 ParseNode decls=pn.getChild("class_body_declaration_list");
366 ParseNodeVector pnv=decls.getChildren();
367 for(int i=0; i<pnv.size(); i++) {
368 ParseNode decl=pnv.elementAt(i);
369 if (isNode(decl,"member")) {
370 parseClassMember(cn,decl);
371 } else if (isNode(decl,"constructor")) {
372 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
373 } else if (isNode(decl, "static_block")) {
375 // TODO add version for normal Java later
376 parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
378 throw new Error("Static blocks not implemented");
380 } else if (isNode(decl,"block")) {
381 } else throw new Error();
386 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
387 ParseNode fieldnode=pn.getChild("field");
388 if (fieldnode!=null) {
389 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
392 ParseNode methodnode=pn.getChild("method");
393 if (methodnode!=null) {
394 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
397 ParseNode innerclassnode=pn.getChild("inner_class_declaration");
398 if (innerclassnode!=null) {
400 parseInnerClassDecl(cn,innerclassnode);
403 // TODO add version for noraml Java later
404 throw new Error("Error: inner class in Class " + cn.getSymbol() + " is not supported yet");
407 ParseNode flagnode=pn.getChild("flag");
408 if (flagnode!=null) {
409 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
415 private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
416 ClassDescriptor icn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
417 icn.setAsInnerClass();
418 icn.setSurroundingClass(cn.getSymbol());
419 icn.setSurrounding(cn);
420 cn.addInnerClass(icn);
421 if (!isEmpty(pn.getChild("super").getTerminal())) {
422 /* parse superclass name */
423 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
424 NameDescriptor nd=parseName(snn);
425 icn.setSuper(nd.toString());
427 if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
428 icn.getSymbol().equals(TypeUtil.TagClass)))
429 icn.setSuper(TypeUtil.ObjectClass);
431 // check inherited interfaces
432 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
433 /* parse inherited interface name */
434 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
435 ParseNodeVector pnv=snlist.getChildren();
436 for(int i=0; i<pnv.size(); i++) {
437 ParseNode decl=pnv.elementAt(i);
438 if (isNode(decl,"type")) {
439 NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
440 icn.addSuperInterface(nd.toString());
444 icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
445 if(!icn.isStatic()) {
446 throw new Error("Error: inner class " + icn.getSymbol() + " in Class " +
447 cn.getSymbol() + " is not a nested class and is not supported yet!");
449 parseClassBody(icn, pn.getChild("classbody"));
453 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
454 ParseNode tn=pn.getChild("type");
455 String type_st=tn.getTerminal();
456 if(type_st.equals("byte")) {
457 return state.getTypeDescriptor(TypeDescriptor.BYTE);
458 } else if(type_st.equals("short")) {
459 return state.getTypeDescriptor(TypeDescriptor.SHORT);
460 } else if(type_st.equals("boolean")) {
461 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
462 } else if(type_st.equals("int")) {
463 return state.getTypeDescriptor(TypeDescriptor.INT);
464 } else if(type_st.equals("long")) {
465 return state.getTypeDescriptor(TypeDescriptor.LONG);
466 } else if(type_st.equals("char")) {
467 return state.getTypeDescriptor(TypeDescriptor.CHAR);
468 } else if(type_st.equals("float")) {
469 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
470 } else if(type_st.equals("double")) {
471 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
472 } else if(type_st.equals("class")) {
473 ParseNode nn=tn.getChild("class");
474 return state.getTypeDescriptor(parseName(nn.getChild("name")));
475 } else if(type_st.equals("array")) {
476 ParseNode nn=tn.getChild("array");
477 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
478 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
479 for(int i=0; i<numdims.intValue(); i++)
480 td=td.makeArray(state);
483 System.out.println(pn.PPrint(2, true));
488 private NameDescriptor parseName(ParseNode nn) {
489 ParseNode base=nn.getChild("base");
490 ParseNode id=nn.getChild("identifier");
492 return new NameDescriptor(id.getTerminal());
493 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
497 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
498 String name=pn.getChild("name").getTerminal();
499 FlagDescriptor flag=new FlagDescriptor(name);
500 if (pn.getChild("external")!=null)
505 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
506 ParseNode mn=pn.getChild("modifier");
507 Modifiers m=parseModifiersList(mn);
508 if((state.MGC) && cn.isInterface()) {
509 // TODO add version for normal Java later
510 // Can only be PUBLIC or STATIC or FINAL
511 if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
512 || (m.isSynchronized())) {
513 throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
515 m.addModifier(Modifiers.PUBLIC);
516 m.addModifier(Modifiers.STATIC);
517 m.addModifier(Modifiers.FINAL);
520 ParseNode tn=pn.getChild("type");
521 TypeDescriptor t=parseTypeDescriptor(tn);
522 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
523 ParseNodeVector pnv=vn.getChildren();
524 boolean isglobal=pn.getChild("global")!=null;
526 for(int i=0; i<pnv.size(); i++) {
527 ParseNode vardecl=pnv.elementAt(i);
528 ParseNode tmp=vardecl;
529 TypeDescriptor arrayt=t;
530 while (tmp.getChild("single")==null) {
531 arrayt=arrayt.makeArray(state);
532 tmp=tmp.getChild("array");
534 String identifier=tmp.getChild("single").getTerminal();
535 ParseNode epn=vardecl.getChild("initializer");
537 ExpressionNode en=null;
539 en=parseExpression(epn.getFirstChild());
541 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
545 private ExpressionNode parseExpression(ParseNode pn) {
546 if (isNode(pn,"assignment"))
547 return parseAssignmentExpression(pn);
548 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
549 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
550 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
551 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
552 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
553 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
554 isNode(pn,"rightshift")||isNode(pn,"sub")||
555 isNode(pn,"urightshift")||isNode(pn,"sub")||
556 isNode(pn,"add")||isNode(pn,"mult")||
557 isNode(pn,"div")||isNode(pn,"mod")) {
558 ParseNodeVector pnv=pn.getChildren();
559 ParseNode left=pnv.elementAt(0);
560 ParseNode right=pnv.elementAt(1);
561 Operation op=new Operation(pn.getLabel());
562 return new OpNode(parseExpression(left),parseExpression(right),op);
563 } else if (isNode(pn,"unaryplus")||
564 isNode(pn,"unaryminus")||
567 ParseNode left=pn.getFirstChild();
568 Operation op=new Operation(pn.getLabel());
569 return new OpNode(parseExpression(left),op);
570 } else if (isNode(pn,"postinc")||
571 isNode(pn,"postdec")) {
572 ParseNode left=pn.getFirstChild();
573 AssignOperation op=new AssignOperation(pn.getLabel());
574 return new AssignmentNode(parseExpression(left),null,op);
576 } else if (isNode(pn,"preinc")||
577 isNode(pn,"predec")) {
578 ParseNode left=pn.getFirstChild();
579 AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
580 return new AssignmentNode(parseExpression(left),
581 new LiteralNode("integer",new Integer(1)),op);
582 } else if (isNode(pn,"literal")) {
583 String literaltype=pn.getTerminal();
584 ParseNode literalnode=pn.getChild(literaltype);
585 Object literal_obj=literalnode.getLiteral();
586 return new LiteralNode(literaltype, literal_obj);
587 } else if (isNode(pn,"createobject")) {
588 TypeDescriptor td=parseTypeDescriptor(pn);
590 Vector args=parseArgumentList(pn);
591 boolean isglobal=pn.getChild("global")!=null||
592 pn.getChild("scratch")!=null;
593 String disjointId=null;
594 if( pn.getChild("disjoint") != null) {
595 disjointId = pn.getChild("disjoint").getTerminal();
597 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
598 for(int i=0; i<args.size(); i++) {
599 con.addArgument((ExpressionNode)args.get(i));
601 /* Could have flag set or tag added here */
602 if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
603 FlagEffects fe=new FlagEffects(null);
604 if (pn.getChild("flag_list")!=null)
605 parseFlagEffect(fe, pn.getChild("flag_list"));
607 if (pn.getChild("tag_list")!=null)
608 parseTagEffect(fe, pn.getChild("tag_list"));
609 con.addFlagEffects(fe);
613 } else if (isNode(pn,"createarray")) {
614 //System.out.println(pn.PPrint(3,true));
615 boolean isglobal=pn.getChild("global")!=null||
616 pn.getChild("scratch")!=null;
617 String disjointId=null;
618 if( pn.getChild("disjoint") != null) {
619 disjointId = pn.getChild("disjoint").getTerminal();
621 TypeDescriptor td=parseTypeDescriptor(pn);
622 Vector args=parseDimExprs(pn);
624 if (pn.getChild("dims_opt").getLiteral()!=null)
625 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
626 for(int i=0; i<(args.size()+num); i++)
627 td=td.makeArray(state);
628 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
629 for(int i=0; i<args.size(); i++) {
630 con.addArgument((ExpressionNode)args.get(i));
633 } else if (isNode(pn,"name")) {
634 NameDescriptor nd=parseName(pn);
635 return new NameNode(nd);
636 } else if (isNode(pn,"this")) {
637 NameDescriptor nd=new NameDescriptor("this");
638 return new NameNode(nd);
639 } else if (isNode(pn,"isavailable")) {
640 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
641 return new OpNode(new NameNode(nd),null,new Operation(Operation.ISAVAILABLE));
642 } else if (isNode(pn,"methodinvoke1")) {
643 NameDescriptor nd=parseName(pn.getChild("name"));
644 Vector args=parseArgumentList(pn);
645 MethodInvokeNode min=new MethodInvokeNode(nd);
646 for(int i=0; i<args.size(); i++) {
647 min.addArgument((ExpressionNode)args.get(i));
650 } else if (isNode(pn,"methodinvoke2")) {
651 String methodid=pn.getChild("id").getTerminal();
652 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
653 Vector args=parseArgumentList(pn);
654 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
655 for(int i=0; i<args.size(); i++) {
656 min.addArgument((ExpressionNode)args.get(i));
659 } else if (isNode(pn,"fieldaccess")) {
660 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
661 String fieldname=pn.getChild("field").getTerminal();
662 return new FieldAccessNode(en,fieldname);
663 } else if (isNode(pn,"arrayaccess")) {
664 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
665 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
666 return new ArrayAccessNode(en,index);
667 } else if (isNode(pn,"cast1")) {
669 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
670 } catch (Exception e) {
671 System.out.println(pn.PPrint(1,true));
675 } else if (isNode(pn,"cast2")) {
676 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
677 } else if (isNode(pn, "getoffset")) {
678 TypeDescriptor td=parseTypeDescriptor(pn);
679 String fieldname = pn.getChild("field").getTerminal();
680 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
681 return new OffsetNode(td, fieldname);
682 } else if (isNode(pn, "tert")) {
683 return new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
684 parseExpression(pn.getChild("trueexpr").getFirstChild()),
685 parseExpression(pn.getChild("falseexpr").getFirstChild()) );
686 } else if (isNode(pn, "instanceof")) {
687 ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
688 TypeDescriptor t=parseTypeDescriptor(pn);
689 return new InstanceOfNode(exp,t);
690 } else if (isNode(pn, "array_initializer")) {
691 System.out.println( "Array initializers not implemented yet." );
693 //TypeDescriptor td=parseTypeDescriptor(pn);
694 //Vector initializers=parseVariableInitializerList(pn);
695 //return new ArrayInitializerNode(td, initializers);
697 System.out.println("---------------------");
698 System.out.println(pn.PPrint(3,true));
703 private Vector parseDimExprs(ParseNode pn) {
704 Vector arglist=new Vector();
705 ParseNode an=pn.getChild("dim_exprs");
706 if (an==null) /* No argument list */
708 ParseNodeVector anv=an.getChildren();
709 for(int i=0; i<anv.size(); i++) {
710 arglist.add(parseExpression(anv.elementAt(i)));
715 private Vector parseArgumentList(ParseNode pn) {
716 Vector arglist=new Vector();
717 ParseNode an=pn.getChild("argument_list");
718 if (an==null) /* No argument list */
720 ParseNodeVector anv=an.getChildren();
721 for(int i=0; i<anv.size(); i++) {
722 arglist.add(parseExpression(anv.elementAt(i)));
727 private Vector[] parseConsArgumentList(ParseNode pn) {
728 Vector arglist=new Vector();
729 Vector varlist=new Vector();
730 ParseNode an=pn.getChild("cons_argument_list");
731 if (an==null) /* No argument list */
732 return new Vector[] {varlist, arglist};
733 ParseNodeVector anv=an.getChildren();
734 for(int i=0; i<anv.size(); i++) {
735 ParseNode cpn=anv.elementAt(i);
736 ParseNode var=cpn.getChild("var");
737 ParseNode exp=cpn.getChild("exp").getFirstChild();
738 varlist.add(var.getTerminal());
739 arglist.add(parseExpression(exp));
741 return new Vector[] {varlist, arglist};
744 private Vector parseVariableInitializerList(ParseNode pn) {
745 Vector varInitList=new Vector();
746 ParseNode vin=pn.getChild("variable_init_list");
747 if (vin==null) /* No argument list */
749 ParseNodeVector vinv=vin.getChildren();
750 for(int i=0; i<vinv.size(); i++) {
751 varInitList.add(parseExpression(vinv.elementAt(i)));
756 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
757 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
758 ParseNodeVector pnv=pn.getChild("args").getChildren();
760 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
765 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
766 ParseNode headern=pn.getChild("method_header");
767 ParseNode bodyn=pn.getChild("body");
768 MethodDescriptor md=parseMethodHeader(headern);
770 BlockNode bn=parseBlock(bodyn);
772 state.addTreeCode(md,bn);
774 // this is a hack for investigating new language features
775 // at the AST level, someday should evolve into a nice compiler
777 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
778 // md.getSymbol().equals( ***put your method in here like: "main" )
780 // bn.setStyle( BlockNode.NORMAL );
781 // System.out.println( bn.printNode( 0 ) );
784 } catch (Exception e) {
785 System.out.println("Error with method:"+md.getSymbol());
789 System.out.println("Error with method:"+md.getSymbol());
795 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
796 ParseNode mn=pn.getChild("modifiers");
797 Modifiers m=parseModifiersList(mn);
798 ParseNode cdecl=pn.getChild("constructor_declarator");
799 boolean isglobal=cdecl.getChild("global")!=null;
800 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
801 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
802 ParseNode paramnode=cdecl.getChild("parameters");
803 parseParameterList(md,paramnode);
804 ParseNode bodyn0=pn.getChild("body");
805 ParseNode bodyn=bodyn0.getChild("constructor_body");
808 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
809 bn=parseBlock(bodyn);
812 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
813 ParseNode sin=bodyn.getChild("superinvoke");
814 NameDescriptor nd=new NameDescriptor("super");
815 Vector args=parseArgumentList(sin);
816 MethodInvokeNode min=new MethodInvokeNode(nd);
817 for(int i=0; i<args.size(); i++) {
818 min.addArgument((ExpressionNode)args.get(i));
820 BlockExpressionNode ben=new BlockExpressionNode(min);
821 bn.addFirstBlockStatement(ben);
823 } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
824 ParseNode eci=bodyn.getChild("explconstrinv");
825 NameDescriptor nd=new NameDescriptor(cn.getSymbol());
826 Vector args=parseArgumentList(eci);
827 MethodInvokeNode min=new MethodInvokeNode(nd);
828 for(int i=0; i<args.size(); i++) {
829 min.addArgument((ExpressionNode)args.get(i));
831 BlockExpressionNode ben=new BlockExpressionNode(min);
832 bn.addFirstBlockStatement(ben);
834 state.addTreeCode(md,bn);
837 private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
838 // Each class maintains one MethodDecscriptor which combines all its
839 // static blocks in their declaration order
840 boolean isfirst = false;
841 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().get("staticblocks");
843 // the first static block for this class
844 Modifiers m=new Modifiers();
845 m.addModifier(Modifiers.STATIC);
846 md = new MethodDescriptor(m, "staticblocks", false);
847 md.setAsStaticBlock();
850 ParseNode bodyn=pn.getChild("body");
854 cn.incStaticBlocks();
856 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
857 bn=parseBlock(bodyn);
861 state.addTreeCode(md,bn);
863 BlockNode obn = state.getMethodBody(md);
864 for(int i = 0; i < bn.size(); i++) {
865 BlockStatementNode bsn = bn.get(i);
866 obn.addBlockStatement(bsn);
868 //TODO state.addTreeCode(md, obn);
873 public BlockNode parseBlock(ParseNode pn) {
874 this.m_taskexitnum = 0;
875 if (pn==null||isEmpty(pn.getTerminal()))
876 return new BlockNode();
877 ParseNode bsn=pn.getChild("block_statement_list");
878 return parseBlockHelper(bsn);
881 private BlockNode parseBlockHelper(ParseNode pn) {
882 ParseNodeVector pnv=pn.getChildren();
883 BlockNode bn=new BlockNode();
884 for(int i=0; i<pnv.size(); i++) {
885 Vector bsv=parseBlockStatement(pnv.elementAt(i));
886 for(int j=0; j<bsv.size(); j++) {
887 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
893 public BlockNode parseSingleBlock(ParseNode pn) {
894 BlockNode bn=new BlockNode();
895 Vector bsv=parseBlockStatement(pn);
896 for(int j=0; j<bsv.size(); j++) {
897 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
899 bn.setStyle(BlockNode.NOBRACES);
903 public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
904 ParseNodeVector pnv=pn.getChildren();
905 Vector bv=new Vector();
906 for(int i=0; i<pnv.size(); i++) {
907 bv.addAll(parseBlockStatement(pnv.elementAt(i)));
912 public Vector parseBlockStatement(ParseNode pn) {
913 Vector blockstatements=new Vector();
914 if (isNode(pn,"tag_declaration")) {
915 String name=pn.getChild("single").getTerminal();
916 String type=pn.getChild("type").getTerminal();
918 blockstatements.add(new TagDeclarationNode(name, type));
919 } else if (isNode(pn,"local_variable_declaration")) {
920 TypeDescriptor t=parseTypeDescriptor(pn);
921 ParseNode vn=pn.getChild("variable_declarators_list");
922 ParseNodeVector pnv=vn.getChildren();
923 for(int i=0; i<pnv.size(); i++) {
924 ParseNode vardecl=pnv.elementAt(i);
927 ParseNode tmp=vardecl;
928 TypeDescriptor arrayt=t;
929 while (tmp.getChild("single")==null) {
930 arrayt=arrayt.makeArray(state);
931 tmp=tmp.getChild("array");
933 String identifier=tmp.getChild("single").getTerminal();
935 ParseNode epn=vardecl.getChild("initializer");
938 ExpressionNode en=null;
940 en=parseExpression(epn.getFirstChild());
942 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
944 } else if (isNode(pn,"nop")) {
946 } else if (isNode(pn,"expression")) {
947 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
948 } else if (isNode(pn,"ifstatement")) {
949 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
950 parseSingleBlock(pn.getChild("statement").getFirstChild()),
951 pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null));
952 } else if (isNode(pn,"taskexit")) {
954 if (pn.getChild("flag_effects_list")!=null)
955 vfe=parseFlags(pn.getChild("flag_effects_list"));
957 if (pn.getChild("cons_checks")!=null)
958 ccs=parseChecks(pn.getChild("cons_checks"));
960 blockstatements.add(new TaskExitNode(vfe, ccs, this.m_taskexitnum++));
961 } else if (isNode(pn,"atomic")) {
962 BlockNode bn=parseBlockHelper(pn);
963 blockstatements.add(new AtomicNode(bn));
964 } else if (isNode(pn,"synchronized")) {
965 BlockNode bn=parseBlockHelper(pn.getChild("block"));
966 ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
967 blockstatements.add(new SynchronizedNode(en, bn));
968 } else if (isNode(pn,"return")) {
969 if (isEmpty(pn.getTerminal()))
970 blockstatements.add(new ReturnNode());
972 ExpressionNode en=parseExpression(pn.getFirstChild());
973 blockstatements.add(new ReturnNode(en));
975 } else if (isNode(pn,"block_statement_list")) {
976 BlockNode bn=parseBlockHelper(pn);
977 blockstatements.add(new SubBlockNode(bn));
978 } else if (isNode(pn,"empty")) {
980 } else if (isNode(pn,"statement_expression_list")) {
981 ParseNodeVector pnv=pn.getChildren();
982 BlockNode bn=new BlockNode();
983 for(int i=0; i<pnv.size(); i++) {
984 ExpressionNode en=parseExpression(pnv.elementAt(i));
985 blockstatements.add(new BlockExpressionNode(en));
987 bn.setStyle(BlockNode.EXPRLIST);
988 } else if (isNode(pn,"forstatement")) {
989 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
990 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
991 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
992 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
993 blockstatements.add(new LoopNode(init,condition,update,body));
994 } else if (isNode(pn,"whilestatement")) {
995 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
996 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
997 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
998 } else if (isNode(pn,"dowhilestatement")) {
999 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1000 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1001 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
1002 } else if (isNode(pn,"sese")) {
1003 ParseNode pnID=pn.getChild("identifier");
1005 if( pnID != null ) { stID=pnID.getFirstChild().getTerminal(); }
1006 SESENode start=new SESENode(stID);
1007 SESENode end =new SESENode(stID);
1008 start.setEnd( end );
1009 end.setStart( start );
1010 blockstatements.add(start);
1011 blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1012 blockstatements.add(end);
1013 } else if (isNode(pn,"continue")) {
1014 blockstatements.add(new ContinueBreakNode(false));
1015 } else if (isNode(pn,"break")) {
1016 blockstatements.add(new ContinueBreakNode(true));
1018 } else if (isNode(pn,"genreach")) {
1019 String graphName = pn.getChild("graphName").getTerminal();
1020 blockstatements.add( new GenReachNode( graphName ) );
1023 System.out.println("---------------");
1024 System.out.println(pn.PPrint(3,true));
1027 return blockstatements;
1030 public MethodDescriptor parseMethodHeader(ParseNode pn) {
1031 ParseNode mn=pn.getChild("modifiers");
1032 Modifiers m=parseModifiersList(mn);
1034 ParseNode tn=pn.getChild("returntype");
1035 TypeDescriptor returntype;
1037 returntype=parseTypeDescriptor(tn);
1039 returntype=new TypeDescriptor(TypeDescriptor.VOID);
1041 ParseNode pmd=pn.getChild("method_declarator");
1042 String name=pmd.getChild("name").getTerminal();
1043 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1045 ParseNode paramnode=pmd.getChild("parameters");
1046 parseParameterList(md,paramnode);
1050 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1051 ParseNode paramlist=pn.getChild("formal_parameter_list");
1052 if (paramlist==null)
1054 ParseNodeVector pnv=paramlist.getChildren();
1055 for(int i=0; i<pnv.size(); i++) {
1056 ParseNode paramn=pnv.elementAt(i);
1058 if (isNode(paramn, "tag_parameter")) {
1059 String paramname=paramn.getChild("single").getTerminal();
1060 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1061 md.addTagParameter(type, paramname);
1063 TypeDescriptor type=parseTypeDescriptor(paramn);
1065 ParseNode tmp=paramn;
1066 while (tmp.getChild("single")==null) {
1067 type=type.makeArray(state);
1068 tmp=tmp.getChild("array");
1070 String paramname=tmp.getChild("single").getTerminal();
1072 md.addParameter(type, paramname);
1077 public Modifiers parseModifiersList(ParseNode pn) {
1078 Modifiers m=new Modifiers();
1079 ParseNode modlist=pn.getChild("modifier_list");
1080 if (modlist!=null) {
1081 ParseNodeVector pnv=modlist.getChildren();
1082 for(int i=0; i<pnv.size(); i++) {
1083 ParseNode modn=pnv.elementAt(i);
1084 if (isNode(modn,"public"))
1085 m.addModifier(Modifiers.PUBLIC);
1086 else if (isNode(modn,"protected"))
1087 m.addModifier(Modifiers.PROTECTED);
1088 else if (isNode(modn,"private"))
1089 m.addModifier(Modifiers.PRIVATE);
1090 else if (isNode(modn,"static"))
1091 m.addModifier(Modifiers.STATIC);
1092 else if (isNode(modn,"final"))
1093 m.addModifier(Modifiers.FINAL);
1094 else if (isNode(modn,"native"))
1095 m.addModifier(Modifiers.NATIVE);
1096 else if (isNode(modn,"synchronized"))
1097 m.addModifier(Modifiers.SYNCHRONIZED);
1098 else if (isNode(modn,"atomic"))
1099 m.addModifier(Modifiers.ATOMIC);
1100 else if (isNode(modn,"abstract"))
1101 m.addModifier(Modifiers.ABSTRACT);
1102 else throw new Error("Unrecognized Modifier");
1108 private boolean isNode(ParseNode pn, String label) {
1109 if (pn.getLabel().equals(label))
1114 private static boolean isEmpty(ParseNode pn) {
1115 if (pn.getLabel().equals("empty"))
1121 private static boolean isEmpty(String s) {
1122 if (s.equals("empty"))
1128 /** Throw an exception if something is unexpected */
1129 private void check(ParseNode pn, String label) {
1131 throw new Error(pn+ "IE: Expected '" + label + "', got null");
1133 if (!pn.getLabel().equals(label)) {
1134 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");