9 private int m_taskexitnum;
11 public BuildIR(State state) {
13 this.m_taskexitnum = 0;
16 public void buildtree(ParseNode pn, Set toanalyze, String sourcefile) {
17 parseFile(pn, toanalyze,sourcefile);
19 // numering the interfaces
21 Iterator it_classes = state.getClassSymbolTable().getValueSet().iterator();
22 while(it_classes.hasNext()) {
23 ClassDescriptor cd = (ClassDescriptor)it_classes.next();
24 if(cd.isInterface()) {
25 cd.setInterfaceId(if_num++);
32 NameDescriptor packages;
34 /** Parse the classes in this file */
35 public void parseFile(ParseNode pn, Set toanalyze,String sourcefile) {
36 singleimports=new Vector();
37 multiimports=new Vector();
39 ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
41 ParseNodeVector pnv=ipn.getChildren();
42 for(int i=0; i<pnv.size(); i++) {
43 ParseNode pnimport=pnv.elementAt(i);
44 NameDescriptor nd=parseName(pnimport.getChild("name"));
45 if (isNode(pnimport,"import_single"))
46 singleimports.add(nd);
51 ParseNode ppn=pn.getChild("packages").getChild("package");
53 packages=parseName(ppn.getChild("name"));
55 ParseNode tpn=pn.getChild("type_declaration_list");
57 ParseNodeVector pnv=tpn.getChildren();
58 for(int i=0; i<pnv.size(); i++) {
59 ParseNode type_pn=pnv.elementAt(i);
60 if (isEmpty(type_pn)) /* Skip the semicolon */
62 if (isNode(type_pn,"class_declaration")) {
63 ClassDescriptor cn=parseTypeDecl(type_pn);
64 cn.setSourceFileName(sourcefile);
65 parseInitializers(cn);
69 // for inner classes/enum
70 HashSet tovisit = new HashSet();
71 Iterator it_icds = cn.getInnerClasses();
72 while(it_icds.hasNext()) {
73 tovisit.add(it_icds.next());
76 while(!tovisit.isEmpty()) {
77 ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
79 parseInitializers(cd);
80 if(toanalyze != null) {
83 cd.setSourceFileName(sourcefile);
86 Iterator it_ics = cd.getInnerClasses();
87 while(it_ics.hasNext()) {
88 tovisit.add(it_ics.next());
91 Iterator it_ienums = cd.getEnum();
92 while(it_ienums.hasNext()) {
93 ClassDescriptor iecd = (ClassDescriptor)it_ienums.next();
94 if(toanalyze != null) {
97 iecd.setSourceFileName(sourcefile);
102 Iterator it_enums = cn.getEnum();
103 while(it_enums.hasNext()) {
104 ClassDescriptor ecd = (ClassDescriptor)it_enums.next();
105 if(toanalyze != null) {
108 ecd.setSourceFileName(sourcefile);
111 } else if (isNode(type_pn,"task_declaration")) {
112 TaskDescriptor td=parseTaskDecl(type_pn);
116 } else if (isNode(type_pn,"interface_declaration")) {
117 // TODO add version for normal Java later
118 ClassDescriptor cn = parseInterfaceDecl(type_pn);
121 cn.setSourceFileName(sourcefile);
125 Iterator it_enums = cn.getEnum();
126 while(it_enums.hasNext()) {
127 ClassDescriptor ecd = (ClassDescriptor)it_enums.next();
128 if(toanalyze != null) {
131 ecd.setSourceFileName(sourcefile);
134 } else if (isNode(type_pn,"enum_declaration")) {
135 // TODO add version for normal Java later
136 ClassDescriptor cn = parseEnumDecl(null, type_pn);
139 cn.setSourceFileName(sourcefile);
142 throw new Error(type_pn.getLabel());
148 public void parseInitializers(ClassDescriptor cn){
149 Vector fv=cn.getFieldVec();
151 for(int i=0;i<fv.size();i++) {
152 FieldDescriptor fd=(FieldDescriptor)fv.get(i);
153 if(fd.getExpressionNode()!=null) {
154 Iterator methodit = cn.getMethods();
155 while(methodit.hasNext()){
156 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
157 if(currmd.isConstructor()){
158 BlockNode bn=state.getMethodBody(currmd);
159 NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
160 AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
161 bn.addBlockStatementAt(new BlockExpressionNode(an), pos);
169 private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {
170 ClassDescriptor ecd=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
173 ecd.setSurroundingClass(cn.getSymbol());
174 ecd.setSurrounding(cn);
177 if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
178 ecd.getSymbol().equals(TypeUtil.TagClass))) {
179 ecd.setSuper(TypeUtil.ObjectClass);
181 ecd.setModifiers(parseModifiersList(pn.getChild("modifiers")));
182 parseEnumBody(ecd, pn.getChild("enumbody"));
186 private void parseEnumBody(ClassDescriptor cn, ParseNode pn) {
187 ParseNode decls=pn.getChild("enum_constants_list");
189 ParseNodeVector pnv=decls.getChildren();
190 for(int i=0; i<pnv.size(); i++) {
191 ParseNode decl=pnv.elementAt(i);
192 if (isNode(decl,"enum_constant")) {
193 parseEnumConstant(cn,decl);
194 } else throw new Error();
199 private void parseEnumConstant(ClassDescriptor cn, ParseNode pn) {
200 cn.addEnumConstant(pn.getChild("name").getTerminal());
203 public ClassDescriptor parseInterfaceDecl(ParseNode pn) {
204 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
205 //cn.setAsInterface();
206 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
207 /* parse inherited interface name */
208 ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
209 ParseNodeVector pnv=snlist.getChildren();
210 for(int i=0; i<pnv.size(); i++) {
211 ParseNode decl=pnv.elementAt(i);
212 if (isNode(decl,"type")) {
213 NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
214 cn.addSuperInterface(nd.toString());
218 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
219 parseInterfaceBody(cn, pn.getChild("interfacebody"));
223 private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
224 assert(cn.isInterface());
225 ParseNode decls=pn.getChild("interface_member_declaration_list");
227 ParseNodeVector pnv=decls.getChildren();
228 for(int i=0; i<pnv.size(); i++) {
229 ParseNode decl=pnv.elementAt(i);
230 if (isNode(decl,"constant")) {
231 parseInterfaceConstant(cn,decl);
232 } else if (isNode(decl,"method")) {
233 parseInterfaceMethod(cn,decl.getChild("method_declaration"));
234 } else throw new Error();
239 private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
241 parseFieldDecl(cn,pn.getChild("field_declaration"));
247 private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
248 ParseNode headern=pn.getChild("header");
249 ParseNode bodyn=pn.getChild("body");
250 MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
251 md.getModifiers().addModifier(Modifiers.PUBLIC);
252 md.getModifiers().addModifier(Modifiers.ABSTRACT);
254 BlockNode bn=parseBlock(bodyn);
256 state.addTreeCode(md,bn);
258 // this is a hack for investigating new language features
259 // at the AST level, someday should evolve into a nice compiler
261 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
262 // md.getSymbol().equals( ***put your method in here like: "main" )
264 // bn.setStyle( BlockNode.NORMAL );
265 // System.out.println( bn.printNode( 0 ) );
268 } catch (Exception e) {
269 System.out.println("Error with method:"+md.getSymbol());
273 System.out.println("Error with method:"+md.getSymbol());
279 public TaskDescriptor parseTaskDecl(ParseNode pn) {
280 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
281 ParseNode bodyn=pn.getChild("body");
282 BlockNode bn=parseBlock(bodyn);
283 parseParameterList(td, pn);
284 state.addTreeCode(td,bn);
285 if (pn.getChild("flag_effects_list")!=null)
286 td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
290 public Vector parseFlags(ParseNode pn) {
291 Vector vfe=new Vector();
292 ParseNodeVector pnv=pn.getChildren();
293 for(int i=0; i<pnv.size(); i++) {
294 ParseNode fn=pnv.elementAt(i);
295 FlagEffects fe=parseFlagEffects(fn);
301 public FlagEffects parseFlagEffects(ParseNode pn) {
302 if (isNode(pn,"flag_effect")) {
303 String flagname=pn.getChild("name").getTerminal();
304 FlagEffects fe=new FlagEffects(flagname);
305 if (pn.getChild("flag_list")!=null)
306 parseFlagEffect(fe, pn.getChild("flag_list"));
307 if (pn.getChild("tag_list")!=null)
308 parseTagEffect(fe, pn.getChild("tag_list"));
310 } else throw new Error();
313 public void parseTagEffect(FlagEffects fes, ParseNode pn) {
314 ParseNodeVector pnv=pn.getChildren();
315 for(int i=0; i<pnv.size(); i++) {
316 ParseNode pn2=pnv.elementAt(i);
318 if (isNode(pn2,"not")) {
320 pn2=pn2.getChild("name");
322 String name=pn2.getTerminal();
323 fes.addTagEffect(new TagEffect(name,status));
327 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
328 ParseNodeVector pnv=pn.getChildren();
329 for(int i=0; i<pnv.size(); i++) {
330 ParseNode pn2=pnv.elementAt(i);
332 if (isNode(pn2,"not")) {
334 pn2=pn2.getChild("name");
336 String name=pn2.getTerminal();
337 fes.addEffect(new FlagEffect(name,status));
341 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
342 if (isNode(pn,"or")) {
343 ParseNodeVector pnv=pn.getChildren();
344 ParseNode left=pnv.elementAt(0);
345 ParseNode right=pnv.elementAt(1);
346 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
347 } else if (isNode(pn,"and")) {
348 ParseNodeVector pnv=pn.getChildren();
349 ParseNode left=pnv.elementAt(0);
350 ParseNode right=pnv.elementAt(1);
351 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
352 } else if (isNode(pn, "not")) {
353 ParseNodeVector pnv=pn.getChildren();
354 ParseNode left=pnv.elementAt(0);
355 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
357 } else if (isNode(pn,"name")) {
358 return new FlagNode(pn.getTerminal());
364 public Vector parseChecks(ParseNode pn) {
365 Vector ccs=new Vector();
366 ParseNodeVector pnv=pn.getChildren();
367 for(int i=0; i<pnv.size(); i++) {
368 ParseNode fn=pnv.elementAt(i);
369 ConstraintCheck cc=parseConstraintCheck(fn);
375 public ConstraintCheck parseConstraintCheck(ParseNode pn) {
376 if (isNode(pn,"cons_check")) {
377 String specname=pn.getChild("name").getChild("identifier").getTerminal();
378 Vector[] args=parseConsArgumentList(pn);
379 ConstraintCheck cc=new ConstraintCheck(specname);
380 for(int i=0; i<args[0].size(); i++) {
381 cc.addVariable((String)args[0].get(i));
382 cc.addArgument((ExpressionNode)args[1].get(i));
385 } else throw new Error();
388 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
391 ParseNode paramlist=pn.getChild("task_parameter_list");
394 ParseNodeVector pnv=paramlist.getChildren();
395 for(int i=0; i<pnv.size(); i++) {
396 ParseNode paramn=pnv.elementAt(i);
397 if(paramn.getChild("optional")!=null) {
399 paramn = paramn.getChild("optional").getFirstChild();
400 System.out.println("OPTIONAL FOUND!!!!!!!");
401 } else { optional = false;
402 System.out.println("NOT OPTIONAL");}
404 TypeDescriptor type=parseTypeDescriptor(paramn);
406 String paramname=paramn.getChild("single").getTerminal();
407 FlagExpressionNode fen=null;
408 if (paramn.getChild("flag")!=null)
409 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
411 ParseNode tagnode=paramn.getChild("tag");
413 TagExpressionList tel=null;
415 tel=parseTagExpressionList(tagnode);
418 td.addParameter(type,paramname,fen, tel, optional);
422 public TagExpressionList parseTagExpressionList(ParseNode pn) {
423 //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
424 //To test, feed in any input program that uses tags
425 ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
426 TagExpressionList tel=new TagExpressionList();
427 for(int i=0; i<pnv.size(); i++) {
428 ParseNode tn=pnv.elementAt(i);
429 String type=tn.getChild("type").getTerminal();
430 String name=tn.getChild("single").getTerminal();
431 tel.addTag(type, name);
436 public ClassDescriptor parseTypeDecl(ParseNode pn) {
437 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
438 if (!isEmpty(pn.getChild("super").getTerminal())) {
439 /* parse superclass name */
440 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
441 NameDescriptor nd=parseName(snn);
442 cn.setSuper(nd.toString());
444 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
445 cn.getSymbol().equals(TypeUtil.TagClass)))
446 cn.setSuper(TypeUtil.ObjectClass);
448 // check inherited interfaces
449 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
450 /* parse inherited interface name */
451 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
452 ParseNodeVector pnv=snlist.getChildren();
453 for(int i=0; i<pnv.size(); i++) {
454 ParseNode decl=pnv.elementAt(i);
455 if (isNode(decl,"type")) {
456 NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
457 cn.addSuperInterface(nd.toString());
461 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
462 parseClassBody(cn, pn.getChild("classbody"));
464 boolean hasConstructor = false;
465 for(Iterator method_it=cn.getMethods(); method_it.hasNext();) {
466 MethodDescriptor md=(MethodDescriptor)method_it.next();
467 hasConstructor |= md.isConstructor();
469 if((!hasConstructor) && (!cn.isEnum())) {
470 // add a default constructor for this class
471 MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
472 cn.getSymbol(), false);
473 BlockNode bn=new BlockNode();
474 state.addTreeCode(md,bn);
475 md.setDefaultConstructor();
481 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
482 ParseNode decls=pn.getChild("class_body_declaration_list");
484 ParseNodeVector pnv=decls.getChildren();
485 for(int i=0; i<pnv.size(); i++) {
486 ParseNode decl=pnv.elementAt(i);
487 if (isNode(decl,"member")) {
488 parseClassMember(cn,decl);
489 } else if (isNode(decl,"constructor")) {
490 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
491 } else if (isNode(decl, "static_block")) {
492 parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
493 } else if (isNode(decl,"block")) {
494 } else if (isNode(decl,"location_order_declaration")) {
495 parseLocationOrder(cn,decl.getChild("location_order_list"));
496 } else throw new Error();
501 private void parseLocationOrder(ClassDescriptor cd, ParseNode pn) {
502 ParseNodeVector pnv = pn.getChildren();
503 Lattice<String> locOrder =
504 new Lattice<String>("_top_","_bottom_");
505 Set<String> spinLocSet=new HashSet<String>();
506 for (int i = 0; i < pnv.size(); i++) {
507 ParseNode loc = pnv.elementAt(i);
508 if(isNode(loc,"location_property")){
509 String spinLoc=loc.getChildren().elementAt(0).getLabel();
510 spinLocSet.add(spinLoc);
512 String lowerLoc=loc.getChildren().elementAt(0).getLabel();
513 String higherLoc= loc.getChildren().elementAt(1).getLabel();
514 locOrder.put(higherLoc, lowerLoc);
515 if (locOrder.isIntroducingCycle(higherLoc)) {
516 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
517 + " introduces a cycle.");
521 if(spinLocSet.size()>0){
522 //checking if location is actually defined in the hierarchy
523 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
524 String locID = (String) iterator.next();
525 if(!locOrder.containsKey(locID)){
526 throw new Error("Error: The spinning location '"+
527 locID + "' is not defined in the hierarchy of the class '"+cd +"'.");
530 state.addLocationPropertySet(cd, spinLocSet);
532 state.addLocationOrder(cd, locOrder);
535 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
536 ParseNode fieldnode=pn.getChild("field");
537 if (fieldnode!=null) {
538 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
541 ParseNode methodnode=pn.getChild("method");
542 if (methodnode!=null) {
543 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
546 ParseNode innerclassnode=pn.getChild("inner_class_declaration");
547 if (innerclassnode!=null) {
548 parseInnerClassDecl(cn,innerclassnode);
551 ParseNode enumnode=pn.getChild("enum_declaration");
552 if (enumnode!=null) {
553 parseEnumDecl(cn,enumnode);
556 ParseNode flagnode=pn.getChild("flag");
557 if (flagnode!=null) {
558 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
561 // in case there are empty node
562 ParseNode emptynode=pn.getChild("empty");
563 if(emptynode != null) {
569 private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
570 ClassDescriptor icn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
571 icn.setAsInnerClass();
572 icn.setSurroundingClass(cn.getSymbol());
573 icn.setSurrounding(cn);
574 cn.addInnerClass(icn);
575 if (!isEmpty(pn.getChild("super").getTerminal())) {
576 /* parse superclass name */
577 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
578 NameDescriptor nd=parseName(snn);
579 icn.setSuper(nd.toString());
581 if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
582 icn.getSymbol().equals(TypeUtil.TagClass)))
583 icn.setSuper(TypeUtil.ObjectClass);
585 // check inherited interfaces
586 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
587 /* parse inherited interface name */
588 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
589 ParseNodeVector pnv=snlist.getChildren();
590 for(int i=0; i<pnv.size(); i++) {
591 ParseNode decl=pnv.elementAt(i);
592 if (isNode(decl,"type")) {
593 NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
594 icn.addSuperInterface(nd.toString());
598 icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
599 if(!icn.isStatic()) {
600 throw new Error("Error: inner class " + icn.getSymbol() + " in Class " +
601 cn.getSymbol() + " is not a nested class and is not supported yet!");
603 parseClassBody(icn, pn.getChild("classbody"));
607 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
608 ParseNode tn=pn.getChild("type");
609 String type_st=tn.getTerminal();
610 if(type_st.equals("byte")) {
611 return state.getTypeDescriptor(TypeDescriptor.BYTE);
612 } else if(type_st.equals("short")) {
613 return state.getTypeDescriptor(TypeDescriptor.SHORT);
614 } else if(type_st.equals("boolean")) {
615 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
616 } else if(type_st.equals("int")) {
617 return state.getTypeDescriptor(TypeDescriptor.INT);
618 } else if(type_st.equals("long")) {
619 return state.getTypeDescriptor(TypeDescriptor.LONG);
620 } else if(type_st.equals("char")) {
621 return state.getTypeDescriptor(TypeDescriptor.CHAR);
622 } else if(type_st.equals("float")) {
623 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
624 } else if(type_st.equals("double")) {
625 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
626 } else if(type_st.equals("class")) {
627 ParseNode nn=tn.getChild("class");
628 return state.getTypeDescriptor(parseName(nn.getChild("name")));
629 } else if(type_st.equals("array")) {
630 ParseNode nn=tn.getChild("array");
631 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
632 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
633 for(int i=0; i<numdims.intValue(); i++)
634 td=td.makeArray(state);
637 System.out.println(pn.PPrint(2, true));
642 private NameDescriptor parseName(ParseNode nn) {
643 ParseNode base=nn.getChild("base");
644 ParseNode id=nn.getChild("identifier");
646 return new NameDescriptor(id.getTerminal());
647 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
651 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
652 String name=pn.getChild("name").getTerminal();
653 FlagDescriptor flag=new FlagDescriptor(name);
654 if (pn.getChild("external")!=null)
659 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
660 ParseNode mn=pn.getChild("modifier");
661 Modifiers m=parseModifiersList(mn);
662 if(cn.isInterface()) {
663 // TODO add version for normal Java later
664 // Can only be PUBLIC or STATIC or FINAL
665 if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
666 || (m.isSynchronized())) {
667 throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
669 m.addModifier(Modifiers.PUBLIC);
670 m.addModifier(Modifiers.STATIC);
671 m.addModifier(Modifiers.FINAL);
674 ParseNode tn=pn.getChild("type");
675 TypeDescriptor t=parseTypeDescriptor(tn);
676 assignAnnotationsToType(m,t);
677 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
678 ParseNodeVector pnv=vn.getChildren();
679 boolean isglobal=pn.getChild("global")!=null;
681 for(int i=0; i<pnv.size(); i++) {
682 ParseNode vardecl=pnv.elementAt(i);
683 ParseNode tmp=vardecl;
684 TypeDescriptor arrayt=t;
685 while (tmp.getChild("single")==null) {
686 arrayt=arrayt.makeArray(state);
687 tmp=tmp.getChild("array");
689 String identifier=tmp.getChild("single").getTerminal();
690 ParseNode epn=vardecl.getChild("initializer");
692 ExpressionNode en=null;
694 en=parseExpression(epn.getFirstChild());
695 en.setNumLine(epn.getFirstChild().getLine());
697 // for static field, the initializer should be considered as a
699 boolean isfirst = false;
700 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
702 // the first static block for this class
703 Modifiers m_i=new Modifiers();
704 m_i.addModifier(Modifiers.STATIC);
705 md = new MethodDescriptor(m_i, "staticblocks", false);
706 md.setAsStaticBlock();
712 cn.incStaticBlocks();
713 BlockNode bn=new BlockNode();
714 NameNode nn=new NameNode(new NameDescriptor(identifier));
715 nn.setNumLine(en.getNumLine());
716 AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
717 an.setNumLine(pn.getLine());
718 bn.addBlockStatement(new BlockExpressionNode(an));
720 state.addTreeCode(md,bn);
722 BlockNode obn = state.getMethodBody(md);
723 for(int ii = 0; ii < bn.size(); ii++) {
724 BlockStatementNode bsn = bn.get(ii);
725 obn.addBlockStatement(bsn);
727 state.addTreeCode(md, obn);
734 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
738 private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type){
739 Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
740 for(int i=0; i<annotations.size(); i++) {
741 // it only supports a marker annotation
742 AnnotationDescriptor an=annotations.elementAt(i);
743 type.addAnnotationMarker(an);
749 private ExpressionNode parseExpression(ParseNode pn) {
750 if (isNode(pn,"assignment"))
751 return parseAssignmentExpression(pn);
752 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
753 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
754 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
755 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
756 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
757 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
758 isNode(pn,"rightshift")||isNode(pn,"sub")||
759 isNode(pn,"urightshift")||isNode(pn,"sub")||
760 isNode(pn,"add")||isNode(pn,"mult")||
761 isNode(pn,"div")||isNode(pn,"mod")) {
762 ParseNodeVector pnv=pn.getChildren();
763 ParseNode left=pnv.elementAt(0);
764 ParseNode right=pnv.elementAt(1);
765 Operation op=new Operation(pn.getLabel());
766 OpNode on=new OpNode(parseExpression(left),parseExpression(right),op);
767 on.setNumLine(pn.getLine());
769 } else if (isNode(pn,"unaryplus")||
770 isNode(pn,"unaryminus")||
773 ParseNode left=pn.getFirstChild();
774 Operation op=new Operation(pn.getLabel());
775 OpNode on=new OpNode(parseExpression(left),op);
776 on.setNumLine(pn.getLine());
778 } else if (isNode(pn,"postinc")||
779 isNode(pn,"postdec")) {
780 ParseNode left=pn.getFirstChild();
781 AssignOperation op=new AssignOperation(pn.getLabel());
782 AssignmentNode an=new AssignmentNode(parseExpression(left),null,op);
783 an.setNumLine(pn.getLine());
786 } else if (isNode(pn,"preinc")||
787 isNode(pn,"predec")) {
788 ParseNode left=pn.getFirstChild();
789 AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
790 AssignmentNode an=new AssignmentNode(parseExpression(left),
791 new LiteralNode("integer",new Integer(1)),op);
792 an.setNumLine(pn.getLine());
794 } else if (isNode(pn,"literal")) {
795 String literaltype=pn.getTerminal();
796 ParseNode literalnode=pn.getChild(literaltype);
797 Object literal_obj=literalnode.getLiteral();
798 LiteralNode ln=new LiteralNode(literaltype, literal_obj);
799 ln.setNumLine(pn.getLine());
801 } else if (isNode(pn,"createobject")) {
802 TypeDescriptor td=parseTypeDescriptor(pn);
804 Vector args=parseArgumentList(pn);
805 boolean isglobal=pn.getChild("global")!=null||
806 pn.getChild("scratch")!=null;
807 String disjointId=null;
808 if( pn.getChild("disjoint") != null) {
809 disjointId = pn.getChild("disjoint").getTerminal();
811 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
812 con.setNumLine(pn.getLine());
813 for(int i=0; i<args.size(); i++) {
814 con.addArgument((ExpressionNode)args.get(i));
816 /* Could have flag set or tag added here */
817 if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
818 FlagEffects fe=new FlagEffects(null);
819 if (pn.getChild("flag_list")!=null)
820 parseFlagEffect(fe, pn.getChild("flag_list"));
822 if (pn.getChild("tag_list")!=null)
823 parseTagEffect(fe, pn.getChild("tag_list"));
824 con.addFlagEffects(fe);
828 } else if (isNode(pn,"createobjectcls")) {
829 //TODO::: FIX BUG!!! static fields in caller context need to become parameters
830 TypeDescriptor td=parseTypeDescriptor(pn);
832 ClassDescriptor cnnew=new ClassDescriptor(td.getSymbol()+"$"+innerCount, false);
833 cnnew.setSuper(td.getSymbol());
834 parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
835 Vector args=parseArgumentList(pn);
837 CreateObjectNode con=new CreateObjectNode(td, false, null);
838 con.setNumLine(pn.getLine());
839 for(int i=0; i<args.size(); i++) {
840 con.addArgument((ExpressionNode)args.get(i));
844 } else if (isNode(pn,"createarray")) {
845 //System.out.println(pn.PPrint(3,true));
846 boolean isglobal=pn.getChild("global")!=null||
847 pn.getChild("scratch")!=null;
848 String disjointId=null;
849 if( pn.getChild("disjoint") != null) {
850 disjointId = pn.getChild("disjoint").getTerminal();
852 TypeDescriptor td=parseTypeDescriptor(pn);
853 Vector args=parseDimExprs(pn);
855 if (pn.getChild("dims_opt").getLiteral()!=null)
856 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
857 for(int i=0; i<(args.size()+num); i++)
858 td=td.makeArray(state);
859 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
860 con.setNumLine(pn.getLine());
861 for(int i=0; i<args.size(); i++) {
862 con.addArgument((ExpressionNode)args.get(i));
865 } if (isNode(pn,"createarray2")) {
866 TypeDescriptor td=parseTypeDescriptor(pn);
868 if (pn.getChild("dims_opt").getLiteral()!=null)
869 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
870 for(int i=0; i<num; i++)
871 td=td.makeArray(state);
872 CreateObjectNode con=new CreateObjectNode(td, false, null);
873 con.setNumLine(pn.getLine());
874 ParseNode ipn = pn.getChild("initializer");
875 Vector initializers=parseVariableInitializerList(ipn);
876 ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
877 ain.setNumLine(pn.getLine());
878 con.addArrayInitializer(ain);
880 } else if (isNode(pn,"name")) {
881 NameDescriptor nd=parseName(pn);
882 NameNode nn=new NameNode(nd);
883 nn.setNumLine(pn.getLine());
885 } else if (isNode(pn,"this")) {
886 NameDescriptor nd=new NameDescriptor("this");
887 NameNode nn=new NameNode(nd);
888 nn.setNumLine(pn.getLine());
890 } else if (isNode(pn,"isavailable")) {
891 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
892 NameNode nn=new NameNode(nd);
893 nn.setNumLine(pn.getLine());
894 return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
895 } else if (isNode(pn,"methodinvoke1")) {
896 NameDescriptor nd=parseName(pn.getChild("name"));
897 Vector args=parseArgumentList(pn);
898 MethodInvokeNode min=new MethodInvokeNode(nd);
899 min.setNumLine(pn.getLine());
900 for(int i=0; i<args.size(); i++) {
901 min.addArgument((ExpressionNode)args.get(i));
904 } else if (isNode(pn,"methodinvoke2")) {
905 String methodid=pn.getChild("id").getTerminal();
906 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
907 Vector args=parseArgumentList(pn);
908 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
909 min.setNumLine(pn.getLine());
910 for(int i=0; i<args.size(); i++) {
911 min.addArgument((ExpressionNode)args.get(i));
914 } else if (isNode(pn,"fieldaccess")) {
915 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
916 String fieldname=pn.getChild("field").getTerminal();
918 FieldAccessNode fan=new FieldAccessNode(en,fieldname);
919 fan.setNumLine(pn.getLine());
921 } else if (isNode(pn,"arrayaccess")) {
922 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
923 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
924 ArrayAccessNode aan=new ArrayAccessNode(en,index);
925 aan.setNumLine(pn.getLine());
927 } else if (isNode(pn,"cast1")) {
929 CastNode cn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
930 cn.setNumLine(pn.getLine());
932 } catch (Exception e) {
933 System.out.println(pn.PPrint(1,true));
937 } else if (isNode(pn,"cast2")) {
938 CastNode cn=new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
939 cn.setNumLine(pn.getLine());
941 } else if (isNode(pn, "getoffset")) {
942 TypeDescriptor td=parseTypeDescriptor(pn);
943 String fieldname = pn.getChild("field").getTerminal();
944 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
945 return new OffsetNode(td, fieldname);
946 } else if (isNode(pn, "tert")) {
948 TertiaryNode tn=new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
949 parseExpression(pn.getChild("trueexpr").getFirstChild()),
950 parseExpression(pn.getChild("falseexpr").getFirstChild()) );
951 tn.setNumLine(pn.getLine());
954 } else if (isNode(pn, "instanceof")) {
955 ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
956 TypeDescriptor t=parseTypeDescriptor(pn);
957 InstanceOfNode ion=new InstanceOfNode(exp,t);
958 ion.setNumLine(pn.getLine());
960 } else if (isNode(pn, "array_initializer")) {
961 Vector initializers=parseVariableInitializerList(pn);
962 return new ArrayInitializerNode(initializers);
963 } else if (isNode(pn, "class_type")) {
964 TypeDescriptor td=parseTypeDescriptor(pn);
965 ClassTypeNode ctn=new ClassTypeNode(td);
966 ctn.setNumLine(pn.getLine());
968 } else if (isNode(pn, "empty")) {
971 System.out.println("---------------------");
972 System.out.println(pn.PPrint(3,true));
977 private Vector parseDimExprs(ParseNode pn) {
978 Vector arglist=new Vector();
979 ParseNode an=pn.getChild("dim_exprs");
980 if (an==null) /* No argument list */
982 ParseNodeVector anv=an.getChildren();
983 for(int i=0; i<anv.size(); i++) {
984 arglist.add(parseExpression(anv.elementAt(i)));
989 private Vector parseArgumentList(ParseNode pn) {
990 Vector arglist=new Vector();
991 ParseNode an=pn.getChild("argument_list");
992 if (an==null) /* No argument list */
994 ParseNodeVector anv=an.getChildren();
995 for(int i=0; i<anv.size(); i++) {
996 arglist.add(parseExpression(anv.elementAt(i)));
1001 private Vector[] parseConsArgumentList(ParseNode pn) {
1002 Vector arglist=new Vector();
1003 Vector varlist=new Vector();
1004 ParseNode an=pn.getChild("cons_argument_list");
1005 if (an==null) /* No argument list */
1006 return new Vector[] {varlist, arglist};
1007 ParseNodeVector anv=an.getChildren();
1008 for(int i=0; i<anv.size(); i++) {
1009 ParseNode cpn=anv.elementAt(i);
1010 ParseNode var=cpn.getChild("var");
1011 ParseNode exp=cpn.getChild("exp").getFirstChild();
1012 varlist.add(var.getTerminal());
1013 arglist.add(parseExpression(exp));
1015 return new Vector[] {varlist, arglist};
1018 private Vector parseVariableInitializerList(ParseNode pn) {
1019 Vector varInitList=new Vector();
1020 ParseNode vin=pn.getChild("var_init_list");
1021 if (vin==null) /* No argument list */
1023 ParseNodeVector vinv=vin.getChildren();
1024 for(int i=0; i<vinv.size(); i++) {
1025 varInitList.add(parseExpression(vinv.elementAt(i)));
1030 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
1031 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1032 ParseNodeVector pnv=pn.getChild("args").getChildren();
1034 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
1035 an.setNumLine(pn.getLine());
1040 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1041 ParseNode headern=pn.getChild("method_header");
1042 ParseNode bodyn=pn.getChild("body");
1043 MethodDescriptor md=parseMethodHeader(headern);
1045 BlockNode bn=parseBlock(bodyn);
1046 bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1048 state.addTreeCode(md,bn);
1050 // this is a hack for investigating new language features
1051 // at the AST level, someday should evolve into a nice compiler
1053 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
1054 // md.getSymbol().equals( ***put your method in here like: "main" )
1056 // bn.setStyle( BlockNode.NORMAL );
1057 // System.out.println( bn.printNode( 0 ) );
1060 } catch (Exception e) {
1061 System.out.println("Error with method:"+md.getSymbol());
1062 e.printStackTrace();
1065 System.out.println("Error with method:"+md.getSymbol());
1066 e.printStackTrace();
1071 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1072 ParseNode mn=pn.getChild("modifiers");
1073 Modifiers m=parseModifiersList(mn);
1074 ParseNode cdecl=pn.getChild("constructor_declarator");
1075 boolean isglobal=cdecl.getChild("global")!=null;
1076 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
1077 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1078 ParseNode paramnode=cdecl.getChild("parameters");
1079 parseParameterList(md,paramnode);
1080 ParseNode bodyn0=pn.getChild("body");
1081 ParseNode bodyn=bodyn0.getChild("constructor_body");
1084 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1085 bn=parseBlock(bodyn);
1088 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1089 ParseNode sin=bodyn.getChild("superinvoke");
1090 NameDescriptor nd=new NameDescriptor("super");
1091 Vector args=parseArgumentList(sin);
1092 MethodInvokeNode min=new MethodInvokeNode(nd);
1093 min.setNumLine(sin.getLine());
1094 for(int i=0; i<args.size(); i++) {
1095 min.addArgument((ExpressionNode)args.get(i));
1097 BlockExpressionNode ben=new BlockExpressionNode(min);
1098 bn.addFirstBlockStatement(ben);
1100 } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1101 ParseNode eci=bodyn.getChild("explconstrinv");
1102 NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1103 Vector args=parseArgumentList(eci);
1104 MethodInvokeNode min=new MethodInvokeNode(nd);
1105 min.setNumLine(eci.getLine());
1106 for(int i=0; i<args.size(); i++) {
1107 min.addArgument((ExpressionNode)args.get(i));
1109 BlockExpressionNode ben=new BlockExpressionNode(min);
1110 ben.setNumLine(eci.getLine());
1111 bn.addFirstBlockStatement(ben);
1113 state.addTreeCode(md,bn);
1116 private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1117 // Each class maintains one MethodDecscriptor which combines all its
1118 // static blocks in their declaration order
1119 boolean isfirst = false;
1120 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1122 // the first static block for this class
1123 Modifiers m_i=new Modifiers();
1124 m_i.addModifier(Modifiers.STATIC);
1125 md = new MethodDescriptor(m_i, "staticblocks", false);
1126 md.setAsStaticBlock();
1129 ParseNode bodyn=pn.getChild("body");
1133 cn.incStaticBlocks();
1135 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1136 bn=parseBlock(bodyn);
1140 state.addTreeCode(md,bn);
1142 BlockNode obn = state.getMethodBody(md);
1143 for(int i = 0; i < bn.size(); i++) {
1144 BlockStatementNode bsn = bn.get(i);
1145 obn.addBlockStatement(bsn);
1147 state.addTreeCode(md, obn);
1152 public BlockNode parseBlock(ParseNode pn) {
1153 this.m_taskexitnum = 0;
1154 if (pn==null||isEmpty(pn.getTerminal()))
1155 return new BlockNode();
1156 ParseNode bsn=pn.getChild("block_statement_list");
1157 return parseBlockHelper(bsn);
1160 private BlockNode parseBlockHelper(ParseNode pn) {
1161 ParseNodeVector pnv=pn.getChildren();
1162 BlockNode bn=new BlockNode();
1163 for(int i=0; i<pnv.size(); i++) {
1164 Vector bsv=parseBlockStatement(pnv.elementAt(i));
1165 for(int j=0; j<bsv.size(); j++) {
1166 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1172 public BlockNode parseSingleBlock(ParseNode pn) {
1173 BlockNode bn=new BlockNode();
1174 Vector bsv=parseBlockStatement(pn);
1175 for(int j=0; j<bsv.size(); j++) {
1176 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1178 bn.setStyle(BlockNode.NOBRACES);
1182 public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
1183 ParseNodeVector pnv=pn.getChildren();
1184 Vector bv=new Vector();
1185 for(int i=0; i<pnv.size(); i++) {
1186 bv.addAll(parseBlockStatement(pnv.elementAt(i)));
1191 public Vector parseBlockStatement(ParseNode pn) {
1192 Vector blockstatements=new Vector();
1193 if (isNode(pn,"tag_declaration")) {
1194 String name=pn.getChild("single").getTerminal();
1195 String type=pn.getChild("type").getTerminal();
1197 TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1198 tdn.setNumLine(pn.getLine());
1200 blockstatements.add(tdn);
1201 } else if (isNode(pn,"local_variable_declaration")) {
1203 ParseNode mn=pn.getChild("modifiers");
1204 TypeDescriptor t=parseTypeDescriptor(pn);
1206 Modifiers m=parseModifiersList(mn);
1207 assignAnnotationsToType(m, t);
1209 ParseNode vn=pn.getChild("variable_declarators_list");
1210 ParseNodeVector pnv=vn.getChildren();
1211 for(int i=0; i<pnv.size(); i++) {
1212 ParseNode vardecl=pnv.elementAt(i);
1215 ParseNode tmp=vardecl;
1216 TypeDescriptor arrayt=t;
1218 while (tmp.getChild("single")==null) {
1219 arrayt=arrayt.makeArray(state);
1220 tmp=tmp.getChild("array");
1222 String identifier=tmp.getChild("single").getTerminal();
1224 ParseNode epn=vardecl.getChild("initializer");
1227 ExpressionNode en=null;
1229 en=parseExpression(epn.getFirstChild());
1231 DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1232 dn.setNumLine(tmp.getLine());
1234 blockstatements.add(dn);
1236 } else if (isNode(pn,"nop")) {
1238 } else if (isNode(pn,"expression")) {
1239 BlockExpressionNode ben=new BlockExpressionNode(parseExpression(pn.getFirstChild()));
1240 ben.setNumLine(pn.getLine());
1241 blockstatements.add(ben);
1242 } else if (isNode(pn,"ifstatement")) {
1243 IfStatementNode isn=new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1244 parseSingleBlock(pn.getChild("statement").getFirstChild()),
1245 pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null);
1246 isn.setNumLine(pn.getLine());
1248 blockstatements.add(isn);
1249 } else if (isNode(pn,"switch_statement")) {
1250 // TODO add version for normal Java later
1251 SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1252 parseSingleBlock(pn.getChild("statement").getFirstChild()));
1253 ssn.setNumLine(pn.getLine());
1254 blockstatements.add(ssn);
1255 } else if (isNode(pn,"switch_block_list")) {
1256 // TODO add version for normal Java later
1257 ParseNodeVector pnv=pn.getChildren();
1258 for(int i=0; i<pnv.size(); i++) {
1259 ParseNode sblockdecl=pnv.elementAt(i);
1261 if(isNode(sblockdecl, "switch_block")) {
1262 ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1263 ParseNodeVector labelv=lpn.getChildren();
1264 Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1265 for(int j=0; j<labelv.size(); j++) {
1266 ParseNode labeldecl=labelv.elementAt(j);
1267 if(isNode(labeldecl, "switch_label")) {
1268 SwitchLabelNode sln=new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false);
1269 sln.setNumLine(labeldecl.getLine());
1270 slv.addElement(sln);
1271 } else if(isNode(labeldecl, "default_switch_label")) {
1272 SwitchLabelNode sln=new SwitchLabelNode(null, true);
1273 sln.setNumLine(labeldecl.getLine());
1274 slv.addElement(sln);
1278 SwitchBlockNode sbn=new SwitchBlockNode(slv,
1279 parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild()));
1280 sbn.setNumLine(sblockdecl.getLine());
1282 blockstatements.add(sbn);
1286 } else if (isNode(pn, "trycatchstatement")) {
1287 // TODO add version for normal Java later
1288 // Do not fully support exceptions now. Only make sure that if there are no
1289 // exceptions thrown, the execution is right
1290 ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1291 BlockNode bn=parseBlockHelper(tpn);
1292 blockstatements.add(new SubBlockNode(bn));
1294 ParseNode fbk = pn.getChild("finallyblock");
1296 ParseNode fpn = fbk.getFirstChild();
1297 BlockNode fbn=parseBlockHelper(fpn);
1298 blockstatements.add(new SubBlockNode(fbn));
1300 } else if (isNode(pn, "throwstatement")) {
1301 // TODO Simply return here
1302 //blockstatements.add(new ReturnNode());
1303 } else if (isNode(pn,"taskexit")) {
1305 if (pn.getChild("flag_effects_list")!=null)
1306 vfe=parseFlags(pn.getChild("flag_effects_list"));
1308 if (pn.getChild("cons_checks")!=null)
1309 ccs=parseChecks(pn.getChild("cons_checks"));
1310 TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1311 ten.setNumLine(pn.getLine());
1312 blockstatements.add(ten);
1313 } else if (isNode(pn,"atomic")) {
1314 BlockNode bn=parseBlockHelper(pn);
1315 AtomicNode an=new AtomicNode(bn);
1316 an.setNumLine(pn.getLine());
1317 blockstatements.add(an);
1318 } else if (isNode(pn,"synchronized")) {
1319 BlockNode bn=parseBlockHelper(pn.getChild("block"));
1320 ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
1321 SynchronizedNode sn=new SynchronizedNode(en, bn);
1322 sn.setNumLine(pn.getLine());
1323 blockstatements.add(sn);
1324 } else if (isNode(pn,"return")) {
1325 if (isEmpty(pn.getTerminal()))
1326 blockstatements.add(new ReturnNode());
1328 ExpressionNode en=parseExpression(pn.getFirstChild());
1329 ReturnNode rn=new ReturnNode(en);
1330 rn.setNumLine(pn.getLine());
1331 blockstatements.add(rn);
1333 } else if (isNode(pn,"block_statement_list")) {
1334 BlockNode bn=parseBlockHelper(pn);
1335 blockstatements.add(new SubBlockNode(bn));
1336 } else if (isNode(pn,"empty")) {
1338 } else if (isNode(pn,"statement_expression_list")) {
1339 ParseNodeVector pnv=pn.getChildren();
1340 BlockNode bn=new BlockNode();
1341 for(int i=0; i<pnv.size(); i++) {
1342 ExpressionNode en=parseExpression(pnv.elementAt(i));
1343 blockstatements.add(new BlockExpressionNode(en));
1345 bn.setStyle(BlockNode.EXPRLIST);
1346 } else if (isNode(pn,"forstatement")) {
1347 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
1348 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
1349 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1350 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1351 if(condition == null) {
1352 // no condition clause, make a 'true' expression as the condition
1353 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1355 LoopNode ln=new LoopNode(init,condition,update,body);
1356 ln.setNumLine(pn.getLine());
1357 blockstatements.add(ln);
1358 } else if (isNode(pn,"whilestatement")) {
1359 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1360 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1361 if(condition == null) {
1362 // no condition clause, make a 'true' expression as the condition
1363 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1365 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
1366 } else if (isNode(pn,"dowhilestatement")) {
1367 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1368 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1369 if(condition == null) {
1370 // no condition clause, make a 'true' expression as the condition
1371 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1373 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
1374 } else if (isNode(pn,"sese")) {
1375 ParseNode pnID=pn.getChild("identifier");
1377 if( pnID != null ) { stID=pnID.getFirstChild().getTerminal(); }
1378 SESENode start=new SESENode(stID);
1379 start.setNumLine(pn.getLine());
1380 SESENode end =new SESENode(stID);
1381 start.setEnd( end );
1382 end.setStart( start );
1383 blockstatements.add(start);
1384 blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1385 blockstatements.add(end);
1386 } else if (isNode(pn,"continue")) {
1387 ContinueBreakNode cbn=new ContinueBreakNode(false);
1388 cbn.setNumLine(pn.getLine());
1389 blockstatements.add(cbn);
1390 } else if (isNode(pn,"break")) {
1391 ContinueBreakNode cbn=new ContinueBreakNode(true);
1392 cbn.setNumLine(pn.getLine());
1393 blockstatements.add(cbn);
1395 } else if (isNode(pn,"genreach")) {
1396 String graphName = pn.getChild("graphName").getTerminal();
1397 blockstatements.add( new GenReachNode( graphName ) );
1400 System.out.println("---------------");
1401 System.out.println(pn.PPrint(3,true));
1404 return blockstatements;
1407 public MethodDescriptor parseMethodHeader(ParseNode pn) {
1408 ParseNode mn=pn.getChild("modifiers");
1409 Modifiers m=parseModifiersList(mn);
1411 ParseNode tn=pn.getChild("returntype");
1412 TypeDescriptor returntype;
1414 returntype=parseTypeDescriptor(tn);
1416 returntype=new TypeDescriptor(TypeDescriptor.VOID);
1418 ParseNode pmd=pn.getChild("method_declarator");
1419 String name=pmd.getChild("name").getTerminal();
1420 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1422 ParseNode paramnode=pmd.getChild("parameters");
1423 parseParameterList(md,paramnode);
1427 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1428 ParseNode paramlist=pn.getChild("formal_parameter_list");
1429 if (paramlist==null)
1431 ParseNodeVector pnv=paramlist.getChildren();
1432 for(int i=0; i<pnv.size(); i++) {
1433 ParseNode paramn=pnv.elementAt(i);
1435 if (isNode(paramn, "tag_parameter")) {
1436 String paramname=paramn.getChild("single").getTerminal();
1437 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1438 md.addTagParameter(type, paramname);
1440 TypeDescriptor type=parseTypeDescriptor(paramn);
1442 ParseNode tmp=paramn;
1443 while (tmp.getChild("single")==null) {
1444 type=type.makeArray(state);
1445 tmp=tmp.getChild("array");
1447 String paramname=tmp.getChild("single").getTerminal();
1449 md.addParameter(type, paramname);
1454 public Modifiers parseModifiersList(ParseNode pn) {
1455 Modifiers m=new Modifiers();
1456 ParseNode modlist=pn.getChild("modifier_list");
1457 if (modlist!=null) {
1458 ParseNodeVector pnv=modlist.getChildren();
1459 for(int i=0; i<pnv.size(); i++) {
1460 ParseNode modn=pnv.elementAt(i);
1461 if (isNode(modn,"public"))
1462 m.addModifier(Modifiers.PUBLIC);
1463 else if (isNode(modn,"protected"))
1464 m.addModifier(Modifiers.PROTECTED);
1465 else if (isNode(modn,"private"))
1466 m.addModifier(Modifiers.PRIVATE);
1467 else if (isNode(modn,"static"))
1468 m.addModifier(Modifiers.STATIC);
1469 else if (isNode(modn,"final"))
1470 m.addModifier(Modifiers.FINAL);
1471 else if (isNode(modn,"native"))
1472 m.addModifier(Modifiers.NATIVE);
1473 else if (isNode(modn,"synchronized"))
1474 m.addModifier(Modifiers.SYNCHRONIZED);
1475 else if (isNode(modn,"atomic"))
1476 m.addModifier(Modifiers.ATOMIC);
1477 else if (isNode(modn,"abstract"))
1478 m.addModifier(Modifiers.ABSTRACT);
1479 else if (isNode(modn,"volatile"))
1480 m.addModifier(Modifiers.VOLATILE);
1481 else if (isNode(modn,"transient"))
1482 m.addModifier(Modifiers.TRANSIENT);
1483 else if(isNode(modn,"annotation_list"))
1484 parseAnnotationList(modn,m);
1486 throw new Error("Unrecognized Modifier:"+modn.getLabel());}
1492 private void parseAnnotationList(ParseNode pn, Modifiers m) {
1493 ParseNodeVector pnv = pn.getChildren();
1494 for (int i = 0; i < pnv.size(); i++) {
1495 ParseNode body_list = pnv.elementAt(i);
1496 if (isNode(body_list, "annotation_body")) {
1497 ParseNode body_node = body_list.getFirstChild();
1498 if (isNode(body_node, "marker_annotation")) {
1499 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1500 } else if (isNode(body_node, "single_annotation")) {
1501 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1502 body_node.getChild("element_value").getTerminal()));
1503 } else if (isNode(body_node, "normal_annotation")) {
1504 throw new Error("Annotation with multiple data members is not supported yet.");
1510 private boolean isNode(ParseNode pn, String label) {
1511 if (pn.getLabel().equals(label))
1516 private static boolean isEmpty(ParseNode pn) {
1517 if (pn.getLabel().equals("empty"))
1523 private static boolean isEmpty(String s) {
1524 if (s.equals("empty"))
1530 /** Throw an exception if something is unexpected */
1531 private void check(ParseNode pn, String label) {
1533 throw new Error(pn+ "IE: Expected '" + label + "', got null");
1535 if (!pn.getLabel().equals(label)) {
1536 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");