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++);
30 Hashtable singleimports;
32 NameDescriptor packages;
34 /** Parse the classes in this file */
35 public void parseFile(ParseNode pn, Set toanalyze,String sourcefile) {
36 singleimports= new Hashtable();
37 multiimports=new Vector();
38 ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
40 ParseNodeVector pnv=ipn.getChildren();
41 for(int i=0; i<pnv.size(); i++) {
42 ParseNode pnimport=pnv.elementAt(i);
43 NameDescriptor nd=parseName(pnimport.getChild("name"));
44 // TODO need to implement
45 if (isNode(pnimport,"import_single"))
46 if(!singleimports.containsKey(nd.getIdentifier())) {
47 //map name to full name (includes package/directory
48 singleimports.put(nd.getIdentifier(), nd.getPathFromRootToHere(nd.getIdentifier()));
50 throw new Error("Error: class " + nd.getIdentifier() + " is already more than once in a single type import inside "+sourcefile);
60 ParseNode ppn=pn.getChild("packages").getChild("package");
61 String packageName = null;
63 packageName = ppn.getChild("name").getChild("identifier").getTerminal();
65 ParseNode tpn=pn.getChild("type_declaration_list");
67 ParseNodeVector pnv = tpn.getChildren();
68 for (int i = 0; i < pnv.size(); i++) {
69 ParseNode type_pn = pnv.elementAt(i);
70 if (isEmpty(type_pn)) /* Skip the semicolon */
72 if (isNode(type_pn, "class_declaration")) {
73 ClassDescriptor cn = parseTypeDecl(type_pn, packageName);
74 cn.setImports(singleimports, multiimports);
75 cn.setSourceFileName(sourcefile);
76 parseInitializers(cn);
77 if (toanalyze != null)
80 // for inner classes/enum
81 HashSet tovisit = new HashSet();
82 Iterator it_icds = cn.getInnerClasses();
83 while (it_icds.hasNext()) {
84 tovisit.add(it_icds.next());
87 while (!tovisit.isEmpty()) {
88 ClassDescriptor cd = (ClassDescriptor) tovisit.iterator().next();
90 parseInitializers(cd);
91 if (toanalyze != null) {
94 cd.setSourceFileName(sourcefile);
97 Iterator it_ics = cd.getInnerClasses();
98 while (it_ics.hasNext()) {
99 tovisit.add(it_ics.next());
102 Iterator it_ienums = cd.getEnum();
103 while (it_ienums.hasNext()) {
104 ClassDescriptor iecd = (ClassDescriptor) it_ienums.next();
105 if (toanalyze != null) {
108 iecd.setSourceFileName(sourcefile);
109 state.addClass(iecd);
113 Iterator it_enums = cn.getEnum();
114 while (it_enums.hasNext()) {
115 ClassDescriptor ecd = (ClassDescriptor) it_enums.next();
116 if (toanalyze != null) {
119 ecd.setSourceFileName(sourcefile);
122 } else if (isNode(type_pn, "task_declaration")) {
123 TaskDescriptor td = parseTaskDecl(type_pn);
124 if (toanalyze != null)
127 } else if (isNode(type_pn, "interface_declaration")) {
128 // TODO add version for normal Java later
129 ClassDescriptor cn = parseInterfaceDecl(type_pn);
130 if (toanalyze != null)
132 cn.setSourceFileName(sourcefile);
136 Iterator it_enums = cn.getEnum();
137 while (it_enums.hasNext()) {
138 ClassDescriptor ecd = (ClassDescriptor) it_enums.next();
139 if (toanalyze != null) {
142 ecd.setSourceFileName(sourcefile);
145 } else if (isNode(type_pn, "enum_declaration")) {
146 // TODO add version for normal Java later
147 ClassDescriptor cn = parseEnumDecl(null, type_pn);
148 if (toanalyze != null)
150 cn.setSourceFileName(sourcefile);
153 throw new Error(type_pn.getLabel());
159 public void parseInitializers(ClassDescriptor cn){
160 Vector fv=cn.getFieldVec();
162 for(int i=0;i<fv.size();i++) {
163 FieldDescriptor fd=(FieldDescriptor)fv.get(i);
164 if(fd.getExpressionNode()!=null) {
165 Iterator methodit = cn.getMethods();
166 while(methodit.hasNext()){
167 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
168 if(currmd.isConstructor()){
169 BlockNode bn=state.getMethodBody(currmd);
170 NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
171 AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
172 bn.addBlockStatementAt(new BlockExpressionNode(an), pos);
180 private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {
181 ClassDescriptor ecd=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
184 ecd.setSurroundingClass(cn.getSymbol());
185 ecd.setSurrounding(cn);
188 if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
189 ecd.getSymbol().equals(TypeUtil.TagClass))) {
190 ecd.setSuper(TypeUtil.ObjectClass);
192 ecd.setModifiers(parseModifiersList(pn.getChild("modifiers")));
193 parseEnumBody(ecd, pn.getChild("enumbody"));
197 private void parseEnumBody(ClassDescriptor cn, ParseNode pn) {
198 ParseNode decls=pn.getChild("enum_constants_list");
200 ParseNodeVector pnv=decls.getChildren();
201 for(int i=0; i<pnv.size(); i++) {
202 ParseNode decl=pnv.elementAt(i);
203 if (isNode(decl,"enum_constant")) {
204 parseEnumConstant(cn,decl);
205 } else throw new Error();
210 private void parseEnumConstant(ClassDescriptor cn, ParseNode pn) {
211 cn.addEnumConstant(pn.getChild("name").getTerminal());
214 public ClassDescriptor parseInterfaceDecl(ParseNode pn) {
215 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
216 //cn.setAsInterface();
217 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
218 /* parse inherited interface name */
219 ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
220 ParseNodeVector pnv=snlist.getChildren();
221 for(int i=0; i<pnv.size(); i++) {
222 ParseNode decl=pnv.elementAt(i);
223 if (isNode(decl,"type")) {
224 NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
225 cn.addSuperInterface(nd.toString());
229 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
230 parseInterfaceBody(cn, pn.getChild("interfacebody"));
234 private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
235 assert(cn.isInterface());
236 ParseNode decls=pn.getChild("interface_member_declaration_list");
238 ParseNodeVector pnv=decls.getChildren();
239 for(int i=0; i<pnv.size(); i++) {
240 ParseNode decl=pnv.elementAt(i);
241 if (isNode(decl,"constant")) {
242 parseInterfaceConstant(cn,decl);
243 } else if (isNode(decl,"method")) {
244 parseInterfaceMethod(cn,decl.getChild("method_declaration"));
245 } else throw new Error();
250 private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
252 parseFieldDecl(cn,pn.getChild("field_declaration"));
258 private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
259 ParseNode headern=pn.getChild("header");
260 ParseNode bodyn=pn.getChild("body");
261 MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
262 md.getModifiers().addModifier(Modifiers.PUBLIC);
263 md.getModifiers().addModifier(Modifiers.ABSTRACT);
265 BlockNode bn=parseBlock(bodyn);
267 state.addTreeCode(md,bn);
269 // this is a hack for investigating new language features
270 // at the AST level, someday should evolve into a nice compiler
272 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
273 // md.getSymbol().equals( ***put your method in here like: "main" )
275 // bn.setStyle( BlockNode.NORMAL );
276 // System.out.println( bn.printNode( 0 ) );
279 } catch (Exception e) {
280 System.out.println("Error with method:"+md.getSymbol());
284 System.out.println("Error with method:"+md.getSymbol());
290 public TaskDescriptor parseTaskDecl(ParseNode pn) {
291 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
292 ParseNode bodyn=pn.getChild("body");
293 BlockNode bn=parseBlock(bodyn);
294 parseParameterList(td, pn);
295 state.addTreeCode(td,bn);
296 if (pn.getChild("flag_effects_list")!=null)
297 td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
301 public Vector parseFlags(ParseNode pn) {
302 Vector vfe=new Vector();
303 ParseNodeVector pnv=pn.getChildren();
304 for(int i=0; i<pnv.size(); i++) {
305 ParseNode fn=pnv.elementAt(i);
306 FlagEffects fe=parseFlagEffects(fn);
312 public FlagEffects parseFlagEffects(ParseNode pn) {
313 if (isNode(pn,"flag_effect")) {
314 String flagname=pn.getChild("name").getTerminal();
315 FlagEffects fe=new FlagEffects(flagname);
316 if (pn.getChild("flag_list")!=null)
317 parseFlagEffect(fe, pn.getChild("flag_list"));
318 if (pn.getChild("tag_list")!=null)
319 parseTagEffect(fe, pn.getChild("tag_list"));
321 } else throw new Error();
324 public void parseTagEffect(FlagEffects fes, ParseNode pn) {
325 ParseNodeVector pnv=pn.getChildren();
326 for(int i=0; i<pnv.size(); i++) {
327 ParseNode pn2=pnv.elementAt(i);
329 if (isNode(pn2,"not")) {
331 pn2=pn2.getChild("name");
333 String name=pn2.getTerminal();
334 fes.addTagEffect(new TagEffect(name,status));
338 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
339 ParseNodeVector pnv=pn.getChildren();
340 for(int i=0; i<pnv.size(); i++) {
341 ParseNode pn2=pnv.elementAt(i);
343 if (isNode(pn2,"not")) {
345 pn2=pn2.getChild("name");
347 String name=pn2.getTerminal();
348 fes.addEffect(new FlagEffect(name,status));
352 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
353 if (isNode(pn,"or")) {
354 ParseNodeVector pnv=pn.getChildren();
355 ParseNode left=pnv.elementAt(0);
356 ParseNode right=pnv.elementAt(1);
357 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
358 } else if (isNode(pn,"and")) {
359 ParseNodeVector pnv=pn.getChildren();
360 ParseNode left=pnv.elementAt(0);
361 ParseNode right=pnv.elementAt(1);
362 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
363 } else if (isNode(pn, "not")) {
364 ParseNodeVector pnv=pn.getChildren();
365 ParseNode left=pnv.elementAt(0);
366 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
368 } else if (isNode(pn,"name")) {
369 return new FlagNode(pn.getTerminal());
375 public Vector parseChecks(ParseNode pn) {
376 Vector ccs=new Vector();
377 ParseNodeVector pnv=pn.getChildren();
378 for(int i=0; i<pnv.size(); i++) {
379 ParseNode fn=pnv.elementAt(i);
380 ConstraintCheck cc=parseConstraintCheck(fn);
386 public ConstraintCheck parseConstraintCheck(ParseNode pn) {
387 if (isNode(pn,"cons_check")) {
388 String specname=pn.getChild("name").getChild("identifier").getTerminal();
389 Vector[] args=parseConsArgumentList(pn);
390 ConstraintCheck cc=new ConstraintCheck(specname);
391 for(int i=0; i<args[0].size(); i++) {
392 cc.addVariable((String)args[0].get(i));
393 cc.addArgument((ExpressionNode)args[1].get(i));
396 } else throw new Error();
399 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
402 ParseNode paramlist=pn.getChild("task_parameter_list");
405 ParseNodeVector pnv=paramlist.getChildren();
406 for(int i=0; i<pnv.size(); i++) {
407 ParseNode paramn=pnv.elementAt(i);
408 if(paramn.getChild("optional")!=null) {
410 paramn = paramn.getChild("optional").getFirstChild();
411 System.out.println("OPTIONAL FOUND!!!!!!!");
412 } else { optional = false;
413 System.out.println("NOT OPTIONAL");}
415 TypeDescriptor type=parseTypeDescriptor(paramn);
417 String paramname=paramn.getChild("single").getTerminal();
418 FlagExpressionNode fen=null;
419 if (paramn.getChild("flag")!=null)
420 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
422 ParseNode tagnode=paramn.getChild("tag");
424 TagExpressionList tel=null;
426 tel=parseTagExpressionList(tagnode);
429 td.addParameter(type,paramname,fen, tel, optional);
433 public TagExpressionList parseTagExpressionList(ParseNode pn) {
434 //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
435 //To test, feed in any input program that uses tags
436 ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
437 TagExpressionList tel=new TagExpressionList();
438 for(int i=0; i<pnv.size(); i++) {
439 ParseNode tn=pnv.elementAt(i);
440 String type=tn.getChild("type").getTerminal();
441 String name=tn.getChild("single").getTerminal();
442 tel.addTag(type, name);
447 public ClassDescriptor parseTypeDecl(ParseNode pn, String packageName) {
449 // if is in no package, then create a class descriptor with just the name.
450 // else add the package on and replace "." with "___________"
451 if(packageName == null) {
452 cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
454 String newClassname = packageName + "___________" + pn.getChild("name").getTerminal();
455 cn= new ClassDescriptor(packageName, newClassname.replaceAll("\\.", "___________") , false);
457 if (!isEmpty(pn.getChild("super").getTerminal())) {
458 /* parse superclass name */
459 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
460 NameDescriptor nd=parseName(snn);
461 cn.setSuper(nd.toString());
463 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
464 cn.getSymbol().equals(TypeUtil.TagClass)))
465 cn.setSuper(TypeUtil.ObjectClass);
467 // check inherited interfaces
468 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
469 /* parse inherited interface name */
470 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
471 ParseNodeVector pnv=snlist.getChildren();
472 for(int i=0; i<pnv.size(); i++) {
473 ParseNode decl=pnv.elementAt(i);
474 if (isNode(decl,"type")) {
475 NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
476 cn.addSuperInterface(nd.toString());
480 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
481 parseClassBody(cn, pn.getChild("classbody"));
483 boolean hasConstructor = false;
484 for(Iterator method_it=cn.getMethods(); method_it.hasNext();) {
485 MethodDescriptor md=(MethodDescriptor)method_it.next();
486 hasConstructor |= md.isConstructor();
488 if((!hasConstructor) && (!cn.isEnum())) {
489 // add a default constructor for this class
490 MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
491 cn.getSymbol(), false);
492 BlockNode bn=new BlockNode();
493 state.addTreeCode(md,bn);
494 md.setDefaultConstructor();
500 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
501 ParseNode decls=pn.getChild("class_body_declaration_list");
503 ParseNodeVector pnv=decls.getChildren();
504 for(int i=0; i<pnv.size(); i++) {
505 ParseNode decl=pnv.elementAt(i);
506 if (isNode(decl,"member")) {
507 parseClassMember(cn,decl);
508 } else if (isNode(decl,"constructor")) {
509 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
510 } else if (isNode(decl, "static_block")) {
511 parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
512 } else if (isNode(decl,"block")) {
513 } else if (isNode(decl,"location_order_declaration")) {
514 parseLocationOrder(cn,decl.getChild("location_order_list"));
515 } else throw new Error();
520 private void parseLocationOrder(ClassDescriptor cd, ParseNode pn) {
521 ParseNodeVector pnv = pn.getChildren();
522 Lattice<String> locOrder =
523 new Lattice<String>("_top_","_bottom_");
524 Set<String> spinLocSet=new HashSet<String>();
525 for (int i = 0; i < pnv.size(); i++) {
526 ParseNode loc = pnv.elementAt(i);
527 if(isNode(loc,"location_property")){
528 String spinLoc=loc.getChildren().elementAt(0).getLabel();
529 spinLocSet.add(spinLoc);
531 String lowerLoc=loc.getChildren().elementAt(0).getLabel();
532 String higherLoc= loc.getChildren().elementAt(1).getLabel();
533 locOrder.put(higherLoc, lowerLoc);
534 if (locOrder.isIntroducingCycle(higherLoc)) {
535 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
536 + " introduces a cycle.");
540 if(spinLocSet.size()>0){
541 //checking if location is actually defined in the hierarchy
542 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
543 String locID = (String) iterator.next();
544 if(!locOrder.containsKey(locID)){
545 throw new Error("Error: The spinning location '"+
546 locID + "' is not defined in the hierarchy of the class '"+cd +"'.");
549 state.addLocationPropertySet(cd, spinLocSet);
551 state.addLocationOrder(cd, locOrder);
554 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
555 ParseNode fieldnode=pn.getChild("field");
556 if (fieldnode!=null) {
557 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
560 ParseNode methodnode=pn.getChild("method");
561 if (methodnode!=null) {
562 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
565 ParseNode innerclassnode=pn.getChild("inner_class_declaration");
566 if (innerclassnode!=null) {
567 parseInnerClassDecl(cn,innerclassnode);
570 ParseNode enumnode=pn.getChild("enum_declaration");
571 if (enumnode!=null) {
572 parseEnumDecl(cn,enumnode);
575 ParseNode flagnode=pn.getChild("flag");
576 if (flagnode!=null) {
577 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
580 // in case there are empty node
581 ParseNode emptynode=pn.getChild("empty");
582 if(emptynode != null) {
588 private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
589 ClassDescriptor icn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
590 icn.setAsInnerClass();
591 icn.setSurroundingClass(cn.getSymbol());
592 icn.setSurrounding(cn);
593 cn.addInnerClass(icn);
594 if (!isEmpty(pn.getChild("super").getTerminal())) {
595 /* parse superclass name */
596 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
597 NameDescriptor nd=parseName(snn);
598 icn.setSuper(nd.toString());
600 if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
601 icn.getSymbol().equals(TypeUtil.TagClass)))
602 icn.setSuper(TypeUtil.ObjectClass);
604 // check inherited interfaces
605 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
606 /* parse inherited interface name */
607 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
608 ParseNodeVector pnv=snlist.getChildren();
609 for(int i=0; i<pnv.size(); i++) {
610 ParseNode decl=pnv.elementAt(i);
611 if (isNode(decl,"type")) {
612 NameDescriptor nd=parseName(decl.getChild("class").getChild("name"));
613 icn.addSuperInterface(nd.toString());
617 icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
618 if(!icn.isStatic()) {
619 throw new Error("Error: inner class " + icn.getSymbol() + " in Class " +
620 cn.getSymbol() + " is not a nested class and is not supported yet!");
622 parseClassBody(icn, pn.getChild("classbody"));
626 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
627 ParseNode tn=pn.getChild("type");
628 String type_st=tn.getTerminal();
629 if(type_st.equals("byte")) {
630 return state.getTypeDescriptor(TypeDescriptor.BYTE);
631 } else if(type_st.equals("short")) {
632 return state.getTypeDescriptor(TypeDescriptor.SHORT);
633 } else if(type_st.equals("boolean")) {
634 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
635 } else if(type_st.equals("int")) {
636 return state.getTypeDescriptor(TypeDescriptor.INT);
637 } else if(type_st.equals("long")) {
638 return state.getTypeDescriptor(TypeDescriptor.LONG);
639 } else if(type_st.equals("char")) {
640 return state.getTypeDescriptor(TypeDescriptor.CHAR);
641 } else if(type_st.equals("float")) {
642 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
643 } else if(type_st.equals("double")) {
644 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
645 } else if(type_st.equals("class")) {
646 ParseNode nn=tn.getChild("class");
647 return state.getTypeDescriptor(parseName(nn.getChild("name")));
648 } else if(type_st.equals("array")) {
649 ParseNode nn=tn.getChild("array");
650 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
651 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
652 for(int i=0; i<numdims.intValue(); i++)
653 td=td.makeArray(state);
656 System.out.println(pn.PPrint(2, true));
661 private NameDescriptor parseName(ParseNode nn) {
662 ParseNode base=nn.getChild("base");
663 ParseNode id=nn.getChild("identifier");
664 //TODO check for bugs with naming things like classes...
665 String terminal = (String) (singleimports.containsKey(id.getTerminal())?singleimports.get(id.getTerminal()):id.getTerminal());
667 return new NameDescriptor(terminal);
669 return new NameDescriptor(parseName(base.getChild("name")),terminal);
672 //only function difference between this and parseName() is that this
673 //does not look for a import mapping.
674 private NameDescriptor parseNameRaw(ParseNode nn) {
675 ParseNode base=nn.getChild("base");
676 ParseNode id=nn.getChild("identifier");
678 return new NameDescriptor(id.getTerminal());
680 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
683 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
684 String name=pn.getChild("name").getTerminal();
685 FlagDescriptor flag=new FlagDescriptor(name);
686 if (pn.getChild("external")!=null)
691 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
692 ParseNode mn=pn.getChild("modifier");
693 Modifiers m=parseModifiersList(mn);
694 if(cn.isInterface()) {
695 // TODO add version for normal Java later
696 // Can only be PUBLIC or STATIC or FINAL
697 if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
698 || (m.isSynchronized())) {
699 throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
701 m.addModifier(Modifiers.PUBLIC);
702 m.addModifier(Modifiers.STATIC);
703 m.addModifier(Modifiers.FINAL);
706 ParseNode tn=pn.getChild("type");
707 TypeDescriptor t=parseTypeDescriptor(tn);
708 assignAnnotationsToType(m,t);
709 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
710 ParseNodeVector pnv=vn.getChildren();
711 boolean isglobal=pn.getChild("global")!=null;
713 for(int i=0; i<pnv.size(); i++) {
714 ParseNode vardecl=pnv.elementAt(i);
715 ParseNode tmp=vardecl;
716 TypeDescriptor arrayt=t;
717 while (tmp.getChild("single")==null) {
718 arrayt=arrayt.makeArray(state);
719 tmp=tmp.getChild("array");
721 String identifier=tmp.getChild("single").getTerminal();
722 ParseNode epn=vardecl.getChild("initializer");
724 ExpressionNode en=null;
726 en=parseExpression(epn.getFirstChild());
727 en.setNumLine(epn.getFirstChild().getLine());
729 // for static field, the initializer should be considered as a
731 boolean isfirst = false;
732 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
734 // the first static block for this class
735 Modifiers m_i=new Modifiers();
736 m_i.addModifier(Modifiers.STATIC);
737 md = new MethodDescriptor(m_i, "staticblocks", false);
738 md.setAsStaticBlock();
744 cn.incStaticBlocks();
745 BlockNode bn=new BlockNode();
746 NameNode nn=new NameNode(new NameDescriptor(identifier));
747 nn.setNumLine(en.getNumLine());
748 AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
749 an.setNumLine(pn.getLine());
750 bn.addBlockStatement(new BlockExpressionNode(an));
752 state.addTreeCode(md,bn);
754 BlockNode obn = state.getMethodBody(md);
755 for(int ii = 0; ii < bn.size(); ii++) {
756 BlockStatementNode bsn = bn.get(ii);
757 obn.addBlockStatement(bsn);
759 state.addTreeCode(md, obn);
766 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
770 private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type){
771 Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
772 for(int i=0; i<annotations.size(); i++) {
773 // it only supports a marker annotation
774 AnnotationDescriptor an=annotations.elementAt(i);
775 type.addAnnotationMarker(an);
781 private ExpressionNode parseExpression(ParseNode pn) {
782 if (isNode(pn,"assignment"))
783 return parseAssignmentExpression(pn);
784 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
785 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
786 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
787 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
788 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
789 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
790 isNode(pn,"rightshift")||isNode(pn,"sub")||
791 isNode(pn,"urightshift")||isNode(pn,"sub")||
792 isNode(pn,"add")||isNode(pn,"mult")||
793 isNode(pn,"div")||isNode(pn,"mod")) {
794 ParseNodeVector pnv=pn.getChildren();
795 ParseNode left=pnv.elementAt(0);
796 ParseNode right=pnv.elementAt(1);
797 Operation op=new Operation(pn.getLabel());
798 OpNode on=new OpNode(parseExpression(left),parseExpression(right),op);
799 on.setNumLine(pn.getLine());
801 } else if (isNode(pn,"unaryplus")||
802 isNode(pn,"unaryminus")||
805 ParseNode left=pn.getFirstChild();
806 Operation op=new Operation(pn.getLabel());
807 OpNode on=new OpNode(parseExpression(left),op);
808 on.setNumLine(pn.getLine());
810 } else if (isNode(pn,"postinc")||
811 isNode(pn,"postdec")) {
812 ParseNode left=pn.getFirstChild();
813 AssignOperation op=new AssignOperation(pn.getLabel());
814 AssignmentNode an=new AssignmentNode(parseExpression(left),null,op);
815 an.setNumLine(pn.getLine());
818 } else if (isNode(pn,"preinc")||
819 isNode(pn,"predec")) {
820 ParseNode left=pn.getFirstChild();
821 AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
822 AssignmentNode an=new AssignmentNode(parseExpression(left),
823 new LiteralNode("integer",new Integer(1)),op);
824 an.setNumLine(pn.getLine());
826 } else if (isNode(pn,"literal")) {
827 String literaltype=pn.getTerminal();
828 ParseNode literalnode=pn.getChild(literaltype);
829 Object literal_obj=literalnode.getLiteral();
830 LiteralNode ln=new LiteralNode(literaltype, literal_obj);
831 ln.setNumLine(pn.getLine());
833 } else if (isNode(pn, "createobject")) {
834 TypeDescriptor td = parseTypeDescriptor(pn);
836 Vector args = parseArgumentList(pn);
837 boolean isglobal = pn.getChild("global") != null || pn.getChild("scratch") != null;
838 String disjointId = null;
839 if (pn.getChild("disjoint") != null) {
840 disjointId = pn.getChild("disjoint").getTerminal();
842 CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
843 con.setNumLine(pn.getLine());
844 for (int i = 0; i < args.size(); i++) {
845 con.addArgument((ExpressionNode) args.get(i));
847 /* Could have flag set or tag added here */
848 if (pn.getChild("flag_list") != null || pn.getChild("tag_list") != null) {
849 FlagEffects fe = new FlagEffects(null);
850 if (pn.getChild("flag_list") != null)
851 parseFlagEffect(fe, pn.getChild("flag_list"));
853 if (pn.getChild("tag_list") != null)
854 parseTagEffect(fe, pn.getChild("tag_list"));
855 con.addFlagEffects(fe);
859 } else if (isNode(pn,"createobjectcls")) {
860 //TODO::: FIX BUG!!! static fields in caller context need to become parameters
861 TypeDescriptor td=parseTypeDescriptor(pn);
863 ClassDescriptor cnnew=new ClassDescriptor(td.getSymbol()+"$"+innerCount, false);
864 cnnew.setSuper(td.getSymbol());
865 parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
866 Vector args=parseArgumentList(pn);
868 CreateObjectNode con=new CreateObjectNode(td, false, null);
869 con.setNumLine(pn.getLine());
870 for(int i=0; i<args.size(); i++) {
871 con.addArgument((ExpressionNode)args.get(i));
875 } else if (isNode(pn,"createarray")) {
876 //System.out.println(pn.PPrint(3,true));
877 boolean isglobal=pn.getChild("global")!=null||
878 pn.getChild("scratch")!=null;
879 String disjointId=null;
880 if( pn.getChild("disjoint") != null) {
881 disjointId = pn.getChild("disjoint").getTerminal();
883 TypeDescriptor td=parseTypeDescriptor(pn);
884 Vector args=parseDimExprs(pn);
886 if (pn.getChild("dims_opt").getLiteral()!=null)
887 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
888 for(int i=0; i<(args.size()+num); i++)
889 td=td.makeArray(state);
890 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
891 con.setNumLine(pn.getLine());
892 for(int i=0; i<args.size(); i++) {
893 con.addArgument((ExpressionNode)args.get(i));
896 } if (isNode(pn,"createarray2")) {
897 TypeDescriptor td=parseTypeDescriptor(pn);
899 if (pn.getChild("dims_opt").getLiteral()!=null)
900 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
901 for(int i=0; i<num; i++)
902 td=td.makeArray(state);
903 CreateObjectNode con=new CreateObjectNode(td, false, null);
904 con.setNumLine(pn.getLine());
905 ParseNode ipn = pn.getChild("initializer");
906 Vector initializers=parseVariableInitializerList(ipn);
907 ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
908 ain.setNumLine(pn.getLine());
909 con.addArrayInitializer(ain);
911 } else if (isNode(pn,"name")) {
912 NameDescriptor nd=parseName(pn);
913 NameNode nn=new NameNode(nd);
914 nn.setNumLine(pn.getLine());
916 } else if (isNode(pn,"this")) {
917 NameDescriptor nd=new NameDescriptor("this");
918 NameNode nn=new NameNode(nd);
919 nn.setNumLine(pn.getLine());
921 } else if (isNode(pn,"isavailable")) {
922 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
923 NameNode nn=new NameNode(nd);
924 nn.setNumLine(pn.getLine());
925 return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
926 } else if (isNode(pn,"methodinvoke1")) {
927 NameDescriptor nd=parseName(pn.getChild("name"));
928 Vector args=parseArgumentList(pn);
929 MethodInvokeNode min=new MethodInvokeNode(nd);
930 min.setNumLine(pn.getLine());
931 for(int i=0; i<args.size(); i++) {
932 min.addArgument((ExpressionNode)args.get(i));
935 } else if (isNode(pn,"methodinvoke2")) {
936 String methodid=pn.getChild("id").getTerminal();
937 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
938 Vector args=parseArgumentList(pn);
939 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
940 min.setNumLine(pn.getLine());
941 for(int i=0; i<args.size(); i++) {
942 min.addArgument((ExpressionNode)args.get(i));
945 } else if (isNode(pn,"fieldaccess")) {
946 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
947 String fieldname=pn.getChild("field").getTerminal();
949 FieldAccessNode fan=new FieldAccessNode(en,fieldname);
950 fan.setNumLine(pn.getLine());
952 } else if (isNode(pn,"arrayaccess")) {
953 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
954 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
955 ArrayAccessNode aan=new ArrayAccessNode(en,index);
956 aan.setNumLine(pn.getLine());
958 } else if (isNode(pn,"cast1")) {
960 CastNode cn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
961 cn.setNumLine(pn.getLine());
963 } catch (Exception e) {
964 System.out.println(pn.PPrint(1,true));
968 } else if (isNode(pn,"cast2")) {
969 CastNode cn=new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
970 cn.setNumLine(pn.getLine());
972 } else if (isNode(pn, "getoffset")) {
973 TypeDescriptor td=parseTypeDescriptor(pn);
974 String fieldname = pn.getChild("field").getTerminal();
975 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
976 return new OffsetNode(td, fieldname);
977 } else if (isNode(pn, "tert")) {
979 TertiaryNode tn=new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
980 parseExpression(pn.getChild("trueexpr").getFirstChild()),
981 parseExpression(pn.getChild("falseexpr").getFirstChild()) );
982 tn.setNumLine(pn.getLine());
985 } else if (isNode(pn, "instanceof")) {
986 ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
987 TypeDescriptor t=parseTypeDescriptor(pn);
988 InstanceOfNode ion=new InstanceOfNode(exp,t);
989 ion.setNumLine(pn.getLine());
991 } else if (isNode(pn, "array_initializer")) {
992 Vector initializers=parseVariableInitializerList(pn);
993 return new ArrayInitializerNode(initializers);
994 } else if (isNode(pn, "class_type")) {
995 TypeDescriptor td=parseTypeDescriptor(pn);
996 ClassTypeNode ctn=new ClassTypeNode(td);
997 ctn.setNumLine(pn.getLine());
999 } else if (isNode(pn, "empty")) {
1002 System.out.println("---------------------");
1003 System.out.println(pn.PPrint(3,true));
1008 private Vector parseDimExprs(ParseNode pn) {
1009 Vector arglist=new Vector();
1010 ParseNode an=pn.getChild("dim_exprs");
1011 if (an==null) /* No argument list */
1013 ParseNodeVector anv=an.getChildren();
1014 for(int i=0; i<anv.size(); i++) {
1015 arglist.add(parseExpression(anv.elementAt(i)));
1020 private Vector parseArgumentList(ParseNode pn) {
1021 Vector arglist=new Vector();
1022 ParseNode an=pn.getChild("argument_list");
1023 if (an==null) /* No argument list */
1025 ParseNodeVector anv=an.getChildren();
1026 for(int i=0; i<anv.size(); i++) {
1027 arglist.add(parseExpression(anv.elementAt(i)));
1032 private Vector[] parseConsArgumentList(ParseNode pn) {
1033 Vector arglist=new Vector();
1034 Vector varlist=new Vector();
1035 ParseNode an=pn.getChild("cons_argument_list");
1036 if (an==null) /* No argument list */
1037 return new Vector[] {varlist, arglist};
1038 ParseNodeVector anv=an.getChildren();
1039 for(int i=0; i<anv.size(); i++) {
1040 ParseNode cpn=anv.elementAt(i);
1041 ParseNode var=cpn.getChild("var");
1042 ParseNode exp=cpn.getChild("exp").getFirstChild();
1043 varlist.add(var.getTerminal());
1044 arglist.add(parseExpression(exp));
1046 return new Vector[] {varlist, arglist};
1049 private Vector parseVariableInitializerList(ParseNode pn) {
1050 Vector varInitList=new Vector();
1051 ParseNode vin=pn.getChild("var_init_list");
1052 if (vin==null) /* No argument list */
1054 ParseNodeVector vinv=vin.getChildren();
1055 for(int i=0; i<vinv.size(); i++) {
1056 varInitList.add(parseExpression(vinv.elementAt(i)));
1061 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
1062 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1063 ParseNodeVector pnv=pn.getChild("args").getChildren();
1065 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
1066 an.setNumLine(pn.getLine());
1071 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1072 ParseNode headern=pn.getChild("method_header");
1073 ParseNode bodyn=pn.getChild("body");
1074 MethodDescriptor md=parseMethodHeader(headern);
1076 BlockNode bn=parseBlock(bodyn);
1077 bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1079 state.addTreeCode(md,bn);
1081 // this is a hack for investigating new language features
1082 // at the AST level, someday should evolve into a nice compiler
1084 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
1085 // md.getSymbol().equals( ***put your method in here like: "main" )
1087 // bn.setStyle( BlockNode.NORMAL );
1088 // System.out.println( bn.printNode( 0 ) );
1091 } catch (Exception e) {
1092 System.out.println("Error with method:"+md.getSymbol());
1093 e.printStackTrace();
1096 System.out.println("Error with method:"+md.getSymbol());
1097 e.printStackTrace();
1102 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1103 ParseNode mn=pn.getChild("modifiers");
1104 Modifiers m=parseModifiersList(mn);
1105 ParseNode cdecl=pn.getChild("constructor_declarator");
1106 boolean isglobal=cdecl.getChild("global")!=null;
1107 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
1108 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1109 ParseNode paramnode=cdecl.getChild("parameters");
1110 parseParameterList(md,paramnode);
1111 ParseNode bodyn0=pn.getChild("body");
1112 ParseNode bodyn=bodyn0.getChild("constructor_body");
1115 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1116 bn=parseBlock(bodyn);
1119 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1120 ParseNode sin=bodyn.getChild("superinvoke");
1121 NameDescriptor nd=new NameDescriptor("super");
1122 Vector args=parseArgumentList(sin);
1123 MethodInvokeNode min=new MethodInvokeNode(nd);
1124 min.setNumLine(sin.getLine());
1125 for(int i=0; i<args.size(); i++) {
1126 min.addArgument((ExpressionNode)args.get(i));
1128 BlockExpressionNode ben=new BlockExpressionNode(min);
1129 bn.addFirstBlockStatement(ben);
1131 } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1132 ParseNode eci=bodyn.getChild("explconstrinv");
1133 NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1134 Vector args=parseArgumentList(eci);
1135 MethodInvokeNode min=new MethodInvokeNode(nd);
1136 min.setNumLine(eci.getLine());
1137 for(int i=0; i<args.size(); i++) {
1138 min.addArgument((ExpressionNode)args.get(i));
1140 BlockExpressionNode ben=new BlockExpressionNode(min);
1141 ben.setNumLine(eci.getLine());
1142 bn.addFirstBlockStatement(ben);
1144 state.addTreeCode(md,bn);
1147 private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1148 // Each class maintains one MethodDecscriptor which combines all its
1149 // static blocks in their declaration order
1150 boolean isfirst = false;
1151 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1153 // the first static block for this class
1154 Modifiers m_i=new Modifiers();
1155 m_i.addModifier(Modifiers.STATIC);
1156 md = new MethodDescriptor(m_i, "staticblocks", false);
1157 md.setAsStaticBlock();
1160 ParseNode bodyn=pn.getChild("body");
1164 cn.incStaticBlocks();
1166 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1167 bn=parseBlock(bodyn);
1171 state.addTreeCode(md,bn);
1173 BlockNode obn = state.getMethodBody(md);
1174 for(int i = 0; i < bn.size(); i++) {
1175 BlockStatementNode bsn = bn.get(i);
1176 obn.addBlockStatement(bsn);
1178 state.addTreeCode(md, obn);
1183 public BlockNode parseBlock(ParseNode pn) {
1184 this.m_taskexitnum = 0;
1185 if (pn==null||isEmpty(pn.getTerminal()))
1186 return new BlockNode();
1187 ParseNode bsn=pn.getChild("block_statement_list");
1188 return parseBlockHelper(bsn);
1191 private BlockNode parseBlockHelper(ParseNode pn) {
1192 ParseNodeVector pnv=pn.getChildren();
1193 BlockNode bn=new BlockNode();
1194 for(int i=0; i<pnv.size(); i++) {
1195 Vector bsv=parseBlockStatement(pnv.elementAt(i));
1196 for(int j=0; j<bsv.size(); j++) {
1197 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1203 public BlockNode parseSingleBlock(ParseNode pn) {
1204 BlockNode bn=new BlockNode();
1205 Vector bsv=parseBlockStatement(pn);
1206 for(int j=0; j<bsv.size(); j++) {
1207 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1209 bn.setStyle(BlockNode.NOBRACES);
1213 public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
1214 ParseNodeVector pnv=pn.getChildren();
1215 Vector bv=new Vector();
1216 for(int i=0; i<pnv.size(); i++) {
1217 bv.addAll(parseBlockStatement(pnv.elementAt(i)));
1222 public Vector parseBlockStatement(ParseNode pn) {
1223 Vector blockstatements=new Vector();
1224 if (isNode(pn,"tag_declaration")) {
1225 String name=pn.getChild("single").getTerminal();
1226 String type=pn.getChild("type").getTerminal();
1228 TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1229 tdn.setNumLine(pn.getLine());
1231 blockstatements.add(tdn);
1232 } else if (isNode(pn,"local_variable_declaration")) {
1234 ParseNode mn=pn.getChild("modifiers");
1235 TypeDescriptor t=parseTypeDescriptor(pn);
1237 Modifiers m=parseModifiersList(mn);
1238 assignAnnotationsToType(m, t);
1240 ParseNode vn=pn.getChild("variable_declarators_list");
1241 ParseNodeVector pnv=vn.getChildren();
1242 for(int i=0; i<pnv.size(); i++) {
1243 ParseNode vardecl=pnv.elementAt(i);
1246 ParseNode tmp=vardecl;
1247 TypeDescriptor arrayt=t;
1249 while (tmp.getChild("single")==null) {
1250 arrayt=arrayt.makeArray(state);
1251 tmp=tmp.getChild("array");
1253 String identifier=tmp.getChild("single").getTerminal();
1255 ParseNode epn=vardecl.getChild("initializer");
1258 ExpressionNode en=null;
1260 en=parseExpression(epn.getFirstChild());
1262 DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1263 dn.setNumLine(tmp.getLine());
1265 blockstatements.add(dn);
1267 } else if (isNode(pn,"nop")) {
1269 } else if (isNode(pn,"expression")) {
1270 BlockExpressionNode ben=new BlockExpressionNode(parseExpression(pn.getFirstChild()));
1271 ben.setNumLine(pn.getLine());
1272 blockstatements.add(ben);
1273 } else if (isNode(pn,"ifstatement")) {
1274 IfStatementNode isn=new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1275 parseSingleBlock(pn.getChild("statement").getFirstChild()),
1276 pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null);
1277 isn.setNumLine(pn.getLine());
1279 blockstatements.add(isn);
1280 } else if (isNode(pn,"switch_statement")) {
1281 // TODO add version for normal Java later
1282 SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1283 parseSingleBlock(pn.getChild("statement").getFirstChild()));
1284 ssn.setNumLine(pn.getLine());
1285 blockstatements.add(ssn);
1286 } else if (isNode(pn,"switch_block_list")) {
1287 // TODO add version for normal Java later
1288 ParseNodeVector pnv=pn.getChildren();
1289 for(int i=0; i<pnv.size(); i++) {
1290 ParseNode sblockdecl=pnv.elementAt(i);
1292 if(isNode(sblockdecl, "switch_block")) {
1293 ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1294 ParseNodeVector labelv=lpn.getChildren();
1295 Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1296 for(int j=0; j<labelv.size(); j++) {
1297 ParseNode labeldecl=labelv.elementAt(j);
1298 if(isNode(labeldecl, "switch_label")) {
1299 SwitchLabelNode sln=new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false);
1300 sln.setNumLine(labeldecl.getLine());
1301 slv.addElement(sln);
1302 } else if(isNode(labeldecl, "default_switch_label")) {
1303 SwitchLabelNode sln=new SwitchLabelNode(null, true);
1304 sln.setNumLine(labeldecl.getLine());
1305 slv.addElement(sln);
1309 SwitchBlockNode sbn=new SwitchBlockNode(slv,
1310 parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild()));
1311 sbn.setNumLine(sblockdecl.getLine());
1313 blockstatements.add(sbn);
1317 } else if (isNode(pn, "trycatchstatement")) {
1318 // TODO add version for normal Java later
1319 // Do not fully support exceptions now. Only make sure that if there are no
1320 // exceptions thrown, the execution is right
1321 ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1322 BlockNode bn=parseBlockHelper(tpn);
1323 blockstatements.add(new SubBlockNode(bn));
1325 ParseNode fbk = pn.getChild("finallyblock");
1327 ParseNode fpn = fbk.getFirstChild();
1328 BlockNode fbn=parseBlockHelper(fpn);
1329 blockstatements.add(new SubBlockNode(fbn));
1331 } else if (isNode(pn, "throwstatement")) {
1332 // TODO Simply return here
1333 //blockstatements.add(new ReturnNode());
1334 } else if (isNode(pn,"taskexit")) {
1336 if (pn.getChild("flag_effects_list")!=null)
1337 vfe=parseFlags(pn.getChild("flag_effects_list"));
1339 if (pn.getChild("cons_checks")!=null)
1340 ccs=parseChecks(pn.getChild("cons_checks"));
1341 TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1342 ten.setNumLine(pn.getLine());
1343 blockstatements.add(ten);
1344 } else if (isNode(pn,"atomic")) {
1345 BlockNode bn=parseBlockHelper(pn);
1346 AtomicNode an=new AtomicNode(bn);
1347 an.setNumLine(pn.getLine());
1348 blockstatements.add(an);
1349 } else if (isNode(pn,"synchronized")) {
1350 BlockNode bn=parseBlockHelper(pn.getChild("block"));
1351 ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
1352 SynchronizedNode sn=new SynchronizedNode(en, bn);
1353 sn.setNumLine(pn.getLine());
1354 blockstatements.add(sn);
1355 } else if (isNode(pn,"return")) {
1356 if (isEmpty(pn.getTerminal()))
1357 blockstatements.add(new ReturnNode());
1359 ExpressionNode en=parseExpression(pn.getFirstChild());
1360 ReturnNode rn=new ReturnNode(en);
1361 rn.setNumLine(pn.getLine());
1362 blockstatements.add(rn);
1364 } else if (isNode(pn,"block_statement_list")) {
1365 BlockNode bn=parseBlockHelper(pn);
1366 blockstatements.add(new SubBlockNode(bn));
1367 } else if (isNode(pn,"empty")) {
1369 } else if (isNode(pn,"statement_expression_list")) {
1370 ParseNodeVector pnv=pn.getChildren();
1371 BlockNode bn=new BlockNode();
1372 for(int i=0; i<pnv.size(); i++) {
1373 ExpressionNode en=parseExpression(pnv.elementAt(i));
1374 blockstatements.add(new BlockExpressionNode(en));
1376 bn.setStyle(BlockNode.EXPRLIST);
1377 } else if (isNode(pn,"forstatement")) {
1378 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
1379 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
1380 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1381 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1382 if(condition == null) {
1383 // no condition clause, make a 'true' expression as the condition
1384 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1386 LoopNode ln=new LoopNode(init,condition,update,body);
1387 ln.setNumLine(pn.getLine());
1388 blockstatements.add(ln);
1389 } else if (isNode(pn,"whilestatement")) {
1390 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1391 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1392 if(condition == null) {
1393 // no condition clause, make a 'true' expression as the condition
1394 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1396 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
1397 } else if (isNode(pn,"dowhilestatement")) {
1398 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1399 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1400 if(condition == null) {
1401 // no condition clause, make a 'true' expression as the condition
1402 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1404 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
1405 } else if (isNode(pn,"sese")) {
1406 ParseNode pnID=pn.getChild("identifier");
1408 if( pnID != null ) { stID=pnID.getFirstChild().getTerminal(); }
1409 SESENode start=new SESENode(stID);
1410 start.setNumLine(pn.getLine());
1411 SESENode end =new SESENode(stID);
1412 start.setEnd( end );
1413 end.setStart( start );
1414 blockstatements.add(start);
1415 blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1416 blockstatements.add(end);
1417 } else if (isNode(pn,"continue")) {
1418 ContinueBreakNode cbn=new ContinueBreakNode(false);
1419 cbn.setNumLine(pn.getLine());
1420 blockstatements.add(cbn);
1421 } else if (isNode(pn,"break")) {
1422 ContinueBreakNode cbn=new ContinueBreakNode(true);
1423 cbn.setNumLine(pn.getLine());
1424 blockstatements.add(cbn);
1426 } else if (isNode(pn,"genreach")) {
1427 String graphName = pn.getChild("graphName").getTerminal();
1428 blockstatements.add( new GenReachNode( graphName ) );
1430 } else if(isNode(pn,"labeledstatement")){
1431 String label = pn.getChild("name").getTerminal();
1432 BlockNode bn=parseSingleBlock(pn.getChild("statement").getFirstChild());
1434 blockstatements.add(new SubBlockNode(bn));
1436 System.out.println("---------------");
1437 System.out.println(pn.PPrint(3,true));
1440 return blockstatements;
1443 public MethodDescriptor parseMethodHeader(ParseNode pn) {
1444 ParseNode mn=pn.getChild("modifiers");
1445 Modifiers m=parseModifiersList(mn);
1447 ParseNode tn=pn.getChild("returntype");
1448 TypeDescriptor returntype;
1450 returntype=parseTypeDescriptor(tn);
1452 returntype=new TypeDescriptor(TypeDescriptor.VOID);
1454 ParseNode pmd=pn.getChild("method_declarator");
1455 String name=pmd.getChild("name").getTerminal();
1456 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1458 ParseNode paramnode=pmd.getChild("parameters");
1459 parseParameterList(md,paramnode);
1463 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1464 ParseNode paramlist=pn.getChild("formal_parameter_list");
1465 if (paramlist==null)
1467 ParseNodeVector pnv=paramlist.getChildren();
1468 for(int i=0; i<pnv.size(); i++) {
1469 ParseNode paramn=pnv.elementAt(i);
1471 if (isNode(paramn, "tag_parameter")) {
1472 String paramname=paramn.getChild("single").getTerminal();
1473 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1474 md.addTagParameter(type, paramname);
1477 TypeDescriptor type=parseTypeDescriptor(paramn);
1479 ParseNode tmp=paramn;
1480 while (tmp.getChild("single")==null) {
1481 type=type.makeArray(state);
1482 tmp=tmp.getChild("array");
1484 String paramname=tmp.getChild("single").getTerminal();
1486 md.addParameter(type, paramname);
1487 if(isNode(paramn, "annotation_parameter")){
1488 ParseNode bodynode=paramn.getChild("annotation_body");
1489 parseParameterAnnotation(bodynode,type);
1496 public Modifiers parseModifiersList(ParseNode pn) {
1497 Modifiers m=new Modifiers();
1498 ParseNode modlist=pn.getChild("modifier_list");
1499 if (modlist!=null) {
1500 ParseNodeVector pnv=modlist.getChildren();
1501 for(int i=0; i<pnv.size(); i++) {
1502 ParseNode modn=pnv.elementAt(i);
1503 if (isNode(modn,"public"))
1504 m.addModifier(Modifiers.PUBLIC);
1505 else if (isNode(modn,"protected"))
1506 m.addModifier(Modifiers.PROTECTED);
1507 else if (isNode(modn,"private"))
1508 m.addModifier(Modifiers.PRIVATE);
1509 else if (isNode(modn,"static"))
1510 m.addModifier(Modifiers.STATIC);
1511 else if (isNode(modn,"final"))
1512 m.addModifier(Modifiers.FINAL);
1513 else if (isNode(modn,"native"))
1514 m.addModifier(Modifiers.NATIVE);
1515 else if (isNode(modn,"synchronized"))
1516 m.addModifier(Modifiers.SYNCHRONIZED);
1517 else if (isNode(modn,"atomic"))
1518 m.addModifier(Modifiers.ATOMIC);
1519 else if (isNode(modn,"abstract"))
1520 m.addModifier(Modifiers.ABSTRACT);
1521 else if (isNode(modn,"volatile"))
1522 m.addModifier(Modifiers.VOLATILE);
1523 else if (isNode(modn,"transient"))
1524 m.addModifier(Modifiers.TRANSIENT);
1525 else if(isNode(modn,"annotation_list"))
1526 parseAnnotationList(modn,m);
1528 throw new Error("Unrecognized Modifier:"+modn.getLabel());}
1534 private void parseAnnotationList(ParseNode pn, Modifiers m) {
1535 ParseNodeVector pnv = pn.getChildren();
1536 for (int i = 0; i < pnv.size(); i++) {
1537 ParseNode body_list = pnv.elementAt(i);
1538 if (isNode(body_list, "annotation_body")) {
1539 ParseNode body_node = body_list.getFirstChild();
1540 if (isNode(body_node, "marker_annotation")) {
1541 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1542 } else if (isNode(body_node, "single_annotation")) {
1543 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1544 body_node.getChild("element_value").getTerminal()));
1545 } else if (isNode(body_node, "normal_annotation")) {
1546 throw new Error("Annotation with multiple data members is not supported yet.");
1552 private void parseParameterAnnotation(ParseNode body_list,TypeDescriptor type){
1553 ParseNode body_node = body_list.getFirstChild();
1554 if (isNode(body_node, "marker_annotation")) {
1555 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1556 } else if (isNode(body_node, "single_annotation")) {
1557 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1558 body_node.getChild("element_value").getTerminal()));
1559 } else if (isNode(body_node, "normal_annotation")) {
1560 throw new Error("Annotation with multiple data members is not supported yet.");
1564 private boolean isNode(ParseNode pn, String label) {
1565 if (pn.getLabel().equals(label))
1570 private static boolean isEmpty(ParseNode pn) {
1571 if (pn.getLabel().equals("empty"))
1577 private static boolean isEmpty(String s) {
1578 if (s.equals("empty"))
1584 /** Throw an exception if something is unexpected */
1585 private void check(ParseNode pn, String label) {
1587 throw new Error(pn+ "IE: Expected '" + label + "', got null");
1589 if (!pn.getLabel().equals(label)) {
1590 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");