11 private int m_taskexitnum;
13 public BuildIR(State state) {
15 this.m_taskexitnum = 0;
18 public void buildtree(ParseNode pn, Set toanalyze, String sourcefile) {
19 parseFile(pn, toanalyze, sourcefile);
21 // numering the interfaces
23 Iterator it_classes = state.getClassSymbolTable().getValueSet().iterator();
24 while(it_classes.hasNext()) {
25 ClassDescriptor cd = (ClassDescriptor)it_classes.next();
26 if(cd.isInterface()) {
27 cd.setInterfaceId(if_num++);
32 //This is all single imports and a subset of the
33 //multi imports that have been resolved.
34 Hashtable mandatoryImports;
35 //maps class names in file to full name
36 //Note may map a name to an ERROR.
37 Hashtable multiimports;
38 NameDescriptor packages;
40 /** Parse the classes in this file */
41 public void parseFile(ParseNode pn, Set toanalyze, String sourcefile) {
42 mandatoryImports = new Hashtable();
43 multiimports = new Hashtable();
45 ParseNode ppn=pn.getChild("packages").getChild("package");
46 String packageName = null;
48 NameDescriptor nd = parseClassName(ppn.getChild("name"));
49 packageName = nd.getPathFromRootToHere();
51 //Trick -> import the package directory as a multi-import and it'll
52 //automatically recognize files in the same directory.
53 for (int i = 0; i < state.classpath.size(); i++) {
54 String path = (String) state.classpath.get(i);
55 File folder = new File(path, nd.getPathFromRootToHere().replace('.', '/'));
57 for (String file : folder.list()) {
58 // if the file is of type *.java add to multiImport list.
59 if (file.lastIndexOf('.') != -1
60 && file.substring(file.lastIndexOf('.')).equalsIgnoreCase(".java")) {
61 String classname = file.substring(0, file.length() - 5);
62 // single imports have precedence over multi-imports
63 if (!mandatoryImports.containsKey(classname)) {
64 if (multiimports.containsKey(classname)) {
65 // put error in for later, in case we try to import.
66 multiimports.put(classname, new Error("Error: class " + nd.getIdentifier()
67 + " is defined more than once in a multi-import in " + sourcefile));
69 multiimports.put(classname, nd.getPathFromRootToHere() + "." + classname);
78 ParseNode ipn = pn.getChild("imports").getChild("import_decls_list");
80 ParseNodeVector pnv = ipn.getChildren();
81 for (int i = 0; i < pnv.size(); i++) {
82 ParseNode pnimport = pnv.elementAt(i);
83 NameDescriptor nd = parseName(pnimport.getChild("name"));
84 if (isNode(pnimport, "import_single"))
85 if (!mandatoryImports.containsKey(nd.getIdentifier())) {
86 // map name to full name (includes package/directory
87 mandatoryImports.put(nd.getIdentifier(), nd.getPathFromRootToHere());
89 throw new Error("An ambiguous class "+ nd.getIdentifier() +" has been found. It is included for " +
90 ((String)mandatoryImports.get(nd.getIdentifier())) + " and " +
91 nd.getPathFromRootToHere());
94 // This kind of breaks away from tradition a little bit by doing the file checks here
95 // instead of in Semantic check, but doing it here is easier because we have a mapping early on
96 // if I wait until semantic check, I have to change ALL the type descriptors to match the new
97 // mapping and that's both ugly and tedious.
98 for (int j = 0; j < state.classpath.size(); j++) {
99 String path = (String) state.classpath.get(j);
100 File folder = new File(path, nd.getPathFromRootToHere().replace('.', '/'));
101 if (folder.exists()) {
102 for (String file : folder.list()) {
103 // if the file is of type *.java add to multiImport list.
104 if (file.lastIndexOf('.') != -1
105 && file.substring(file.lastIndexOf('.')).equalsIgnoreCase(".java")) {
106 String classname = file.substring(0, file.length() - 5);
107 // single imports have precedence over multi-imports
108 if (!mandatoryImports.containsKey(classname)) {
109 if (multiimports.containsKey(classname)) {
110 // put error in for later, in case we try to import.
111 multiimports.put(classname, new Error("Error: class " + nd.getIdentifier()
112 + " is defined more than once in a multi-import in " + sourcefile));
114 multiimports.put(classname, nd.getPathFromRootToHere() + "." + classname);
120 throw new Error("Import package " + folder.getAbsolutePath() + " in " + sourcefile
121 + " cannot be resolved.");
128 ParseNode tpn=pn.getChild("type_declaration_list");
130 ParseNodeVector pnv = tpn.getChildren();
131 for (int i = 0; i < pnv.size(); i++) {
132 ParseNode type_pn = pnv.elementAt(i);
133 if (isEmpty(type_pn)) /* Skip the semicolon */
135 if (isNode(type_pn, "class_declaration")) {
136 ClassDescriptor cn = parseTypeDecl(type_pn, packageName);
137 cn.setSourceFileName(sourcefile);
138 parseInitializers(cn);
139 if (toanalyze != null)
142 // for inner classes/enum
143 HashSet tovisit = new HashSet();
144 Iterator it_icds = cn.getInnerClasses();
145 while (it_icds.hasNext()) {
146 tovisit.add(it_icds.next());
149 while (!tovisit.isEmpty()) {
150 ClassDescriptor cd = (ClassDescriptor) tovisit.iterator().next();
152 parseInitializers(cd);
153 if (toanalyze != null) {
156 cd.setSourceFileName(sourcefile);
159 Iterator it_ics = cd.getInnerClasses();
160 while (it_ics.hasNext()) {
161 tovisit.add(it_ics.next());
164 Iterator it_ienums = cd.getEnum();
165 while (it_ienums.hasNext()) {
166 ClassDescriptor iecd = (ClassDescriptor) it_ienums.next();
167 if (toanalyze != null) {
170 iecd.setSourceFileName(sourcefile);
171 state.addClass(iecd);
175 Iterator it_enums = cn.getEnum();
176 while (it_enums.hasNext()) {
177 ClassDescriptor ecd = (ClassDescriptor) it_enums.next();
178 if (toanalyze != null) {
181 ecd.setSourceFileName(sourcefile);
184 } else if (isNode(type_pn, "task_declaration")) {
185 TaskDescriptor td = parseTaskDecl(type_pn);
186 if (toanalyze != null)
189 } else if (isNode(type_pn, "interface_declaration")) {
190 // TODO add version for normal Java later
191 ClassDescriptor cn = parseInterfaceDecl(type_pn, packageName);
192 if (toanalyze != null)
194 cn.setSourceFileName(sourcefile);
198 Iterator it_enums = cn.getEnum();
199 while (it_enums.hasNext()) {
200 ClassDescriptor ecd = (ClassDescriptor) it_enums.next();
201 if (toanalyze != null) {
204 ecd.setSourceFileName(sourcefile);
207 } else if (isNode(type_pn, "enum_declaration")) {
208 // TODO add version for normal Java later
209 ClassDescriptor cn = parseEnumDecl(null, type_pn);
210 if (toanalyze != null)
212 cn.setSourceFileName(sourcefile);
215 throw new Error(type_pn.getLabel());
221 public void parseInitializers(ClassDescriptor cn){
222 Vector fv=cn.getFieldVec();
224 for(int i=0;i<fv.size();i++) {
225 FieldDescriptor fd=(FieldDescriptor)fv.get(i);
226 if(fd.getExpressionNode()!=null) {
227 Iterator methodit = cn.getMethods();
228 while(methodit.hasNext()){
229 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
230 if(currmd.isConstructor()){
231 BlockNode bn=state.getMethodBody(currmd);
232 NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
233 AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
234 bn.addBlockStatementAt(new BlockExpressionNode(an), pos);
242 private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {
243 ClassDescriptor ecd=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
244 ecd.setImports(mandatoryImports);
247 ecd.setSurroundingClass(cn.getSymbol());
248 ecd.setSurrounding(cn);
251 if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
252 ecd.getSymbol().equals(TypeUtil.TagClass))) {
253 ecd.setSuper(TypeUtil.ObjectClass);
255 ecd.setModifiers(parseModifiersList(pn.getChild("modifiers")));
256 parseEnumBody(ecd, pn.getChild("enumbody"));
260 private void parseEnumBody(ClassDescriptor cn, ParseNode pn) {
261 ParseNode decls=pn.getChild("enum_constants_list");
263 ParseNodeVector pnv=decls.getChildren();
264 for(int i=0; i<pnv.size(); i++) {
265 ParseNode decl=pnv.elementAt(i);
266 if (isNode(decl,"enum_constant")) {
267 parseEnumConstant(cn,decl);
268 } else throw new Error();
273 private void parseEnumConstant(ClassDescriptor cn, ParseNode pn) {
274 cn.addEnumConstant(pn.getChild("name").getTerminal());
277 public ClassDescriptor parseInterfaceDecl(ParseNode pn, String packageName) {
279 if(packageName == null) {
280 cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
282 String newClassname = packageName + "." + pn.getChild("name").getTerminal();
283 cn= new ClassDescriptor(packageName, newClassname, true);
286 cn.setImports(mandatoryImports);
287 //cn.setAsInterface();
288 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
289 /* parse inherited interface name */
290 ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
291 ParseNodeVector pnv=snlist.getChildren();
292 for(int i=0; i<pnv.size(); i++) {
293 ParseNode decl=pnv.elementAt(i);
294 if (isNode(decl,"type")) {
295 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
296 cn.addSuperInterface(nd.toString());
300 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
301 parseInterfaceBody(cn, pn.getChild("interfacebody"));
305 private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
306 assert(cn.isInterface());
307 ParseNode decls=pn.getChild("interface_member_declaration_list");
309 ParseNodeVector pnv=decls.getChildren();
310 for(int i=0; i<pnv.size(); i++) {
311 ParseNode decl=pnv.elementAt(i);
312 if (isNode(decl,"constant")) {
313 parseInterfaceConstant(cn,decl);
314 } else if (isNode(decl,"method")) {
315 parseInterfaceMethod(cn,decl.getChild("method_declaration"));
316 } else throw new Error();
321 private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
323 parseFieldDecl(cn,pn.getChild("field_declaration"));
329 private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
330 ParseNode headern=pn.getChild("header");
331 ParseNode bodyn=pn.getChild("body");
332 MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
333 md.getModifiers().addModifier(Modifiers.PUBLIC);
334 md.getModifiers().addModifier(Modifiers.ABSTRACT);
336 BlockNode bn=parseBlock(bodyn);
338 state.addTreeCode(md,bn);
340 // this is a hack for investigating new language features
341 // at the AST level, someday should evolve into a nice compiler
343 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
344 // md.getSymbol().equals( ***put your method in here like: "main" )
346 // bn.setStyle( BlockNode.NORMAL );
347 // System.out.println( bn.printNode( 0 ) );
350 } catch (Exception e) {
351 System.out.println("Error with method:"+md.getSymbol());
355 System.out.println("Error with method:"+md.getSymbol());
361 public TaskDescriptor parseTaskDecl(ParseNode pn) {
362 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
363 ParseNode bodyn=pn.getChild("body");
364 BlockNode bn=parseBlock(bodyn);
365 parseParameterList(td, pn);
366 state.addTreeCode(td,bn);
367 if (pn.getChild("flag_effects_list")!=null)
368 td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
372 public Vector parseFlags(ParseNode pn) {
373 Vector vfe=new Vector();
374 ParseNodeVector pnv=pn.getChildren();
375 for(int i=0; i<pnv.size(); i++) {
376 ParseNode fn=pnv.elementAt(i);
377 FlagEffects fe=parseFlagEffects(fn);
383 public FlagEffects parseFlagEffects(ParseNode pn) {
384 if (isNode(pn,"flag_effect")) {
385 String flagname=pn.getChild("name").getTerminal();
386 FlagEffects fe=new FlagEffects(flagname);
387 if (pn.getChild("flag_list")!=null)
388 parseFlagEffect(fe, pn.getChild("flag_list"));
389 if (pn.getChild("tag_list")!=null)
390 parseTagEffect(fe, pn.getChild("tag_list"));
392 } else throw new Error();
395 public void parseTagEffect(FlagEffects fes, ParseNode pn) {
396 ParseNodeVector pnv=pn.getChildren();
397 for(int i=0; i<pnv.size(); i++) {
398 ParseNode pn2=pnv.elementAt(i);
400 if (isNode(pn2,"not")) {
402 pn2=pn2.getChild("name");
404 String name=pn2.getTerminal();
405 fes.addTagEffect(new TagEffect(name,status));
409 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
410 ParseNodeVector pnv=pn.getChildren();
411 for(int i=0; i<pnv.size(); i++) {
412 ParseNode pn2=pnv.elementAt(i);
414 if (isNode(pn2,"not")) {
416 pn2=pn2.getChild("name");
418 String name=pn2.getTerminal();
419 fes.addEffect(new FlagEffect(name,status));
423 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
424 if (isNode(pn,"or")) {
425 ParseNodeVector pnv=pn.getChildren();
426 ParseNode left=pnv.elementAt(0);
427 ParseNode right=pnv.elementAt(1);
428 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
429 } else if (isNode(pn,"and")) {
430 ParseNodeVector pnv=pn.getChildren();
431 ParseNode left=pnv.elementAt(0);
432 ParseNode right=pnv.elementAt(1);
433 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
434 } else if (isNode(pn, "not")) {
435 ParseNodeVector pnv=pn.getChildren();
436 ParseNode left=pnv.elementAt(0);
437 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
439 } else if (isNode(pn,"name")) {
440 return new FlagNode(pn.getTerminal());
446 public Vector parseChecks(ParseNode pn) {
447 Vector ccs=new Vector();
448 ParseNodeVector pnv=pn.getChildren();
449 for(int i=0; i<pnv.size(); i++) {
450 ParseNode fn=pnv.elementAt(i);
451 ConstraintCheck cc=parseConstraintCheck(fn);
457 public ConstraintCheck parseConstraintCheck(ParseNode pn) {
458 if (isNode(pn,"cons_check")) {
459 String specname=pn.getChild("name").getChild("identifier").getTerminal();
460 Vector[] args=parseConsArgumentList(pn);
461 ConstraintCheck cc=new ConstraintCheck(specname);
462 for(int i=0; i<args[0].size(); i++) {
463 cc.addVariable((String)args[0].get(i));
464 cc.addArgument((ExpressionNode)args[1].get(i));
467 } else throw new Error();
470 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
473 ParseNode paramlist=pn.getChild("task_parameter_list");
476 ParseNodeVector pnv=paramlist.getChildren();
477 for(int i=0; i<pnv.size(); i++) {
478 ParseNode paramn=pnv.elementAt(i);
479 if(paramn.getChild("optional")!=null) {
481 paramn = paramn.getChild("optional").getFirstChild();
482 System.out.println("OPTIONAL FOUND!!!!!!!");
483 } else { optional = false;
484 System.out.println("NOT OPTIONAL");}
486 TypeDescriptor type=parseTypeDescriptor(paramn);
488 String paramname=paramn.getChild("single").getTerminal();
489 FlagExpressionNode fen=null;
490 if (paramn.getChild("flag")!=null)
491 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
493 ParseNode tagnode=paramn.getChild("tag");
495 TagExpressionList tel=null;
497 tel=parseTagExpressionList(tagnode);
500 td.addParameter(type,paramname,fen, tel, optional);
504 public TagExpressionList parseTagExpressionList(ParseNode pn) {
505 //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
506 //To test, feed in any input program that uses tags
507 ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
508 TagExpressionList tel=new TagExpressionList();
509 for(int i=0; i<pnv.size(); i++) {
510 ParseNode tn=pnv.elementAt(i);
511 String type=tn.getChild("type").getTerminal();
512 String name=tn.getChild("single").getTerminal();
513 tel.addTag(type, name);
518 public ClassDescriptor parseTypeDecl(ParseNode pn, String packageName) {
520 // if is in no package, then create a class descriptor with just the name.
521 // else add the package on
522 if(packageName == null) {
523 cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
525 String newClassname = packageName + "." + pn.getChild("name").getTerminal();
526 cn= new ClassDescriptor(packageName, newClassname, false);
528 cn.setImports(mandatoryImports);
529 if (!isEmpty(pn.getChild("super").getTerminal())) {
530 /* parse superclass name */
531 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
532 NameDescriptor nd=parseClassName(snn);
533 cn.setSuper(nd.toString());
535 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
536 cn.getSymbol().equals(TypeUtil.TagClass)))
537 cn.setSuper(TypeUtil.ObjectClass);
539 // check inherited interfaces
540 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
541 /* parse inherited interface name */
542 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
543 ParseNodeVector pnv=snlist.getChildren();
544 for(int i=0; i<pnv.size(); i++) {
545 ParseNode decl=pnv.elementAt(i);
546 if (isNode(decl,"type")) {
547 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
548 cn.addSuperInterface(nd.toString());
552 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
553 parseClassBody(cn, pn.getChild("classbody"));
555 boolean hasConstructor = false;
556 for(Iterator method_it=cn.getMethods(); method_it.hasNext();) {
557 MethodDescriptor md=(MethodDescriptor)method_it.next();
558 hasConstructor |= md.isConstructor();
560 if((!hasConstructor) && (!cn.isEnum())) {
561 // add a default constructor for this class
562 MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
563 cn.getSymbol(), false);
564 BlockNode bn=new BlockNode();
565 state.addTreeCode(md,bn);
566 md.setDefaultConstructor();
572 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
573 ParseNode decls=pn.getChild("class_body_declaration_list");
575 ParseNodeVector pnv=decls.getChildren();
576 for(int i=0; i<pnv.size(); i++) {
577 ParseNode decl=pnv.elementAt(i);
578 if (isNode(decl,"member")) {
579 parseClassMember(cn,decl);
580 } else if (isNode(decl,"constructor")) {
581 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
582 } else if (isNode(decl, "static_block")) {
583 parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
584 } else if (isNode(decl,"block")) {
585 } else if (isNode(decl,"location_order_declaration")) {
586 parseLocationOrder(cn,decl.getChild("location_order_list"));
587 } else throw new Error();
592 private void parseLocationOrder(ClassDescriptor cd, ParseNode pn) {
593 ParseNodeVector pnv = pn.getChildren();
594 Lattice<String> locOrder =
595 new Lattice<String>("_top_","_bottom_");
596 Set<String> spinLocSet=new HashSet<String>();
597 for (int i = 0; i < pnv.size(); i++) {
598 ParseNode loc = pnv.elementAt(i);
599 if(isNode(loc,"location_property")){
600 String spinLoc=loc.getChildren().elementAt(0).getLabel();
601 spinLocSet.add(spinLoc);
603 String lowerLoc=loc.getChildren().elementAt(0).getLabel();
604 String higherLoc= loc.getChildren().elementAt(1).getLabel();
605 locOrder.put(higherLoc, lowerLoc);
606 if (locOrder.isIntroducingCycle(higherLoc)) {
607 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
608 + " introduces a cycle.");
612 if(spinLocSet.size()>0){
613 //checking if location is actually defined in the hierarchy
614 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
615 String locID = (String) iterator.next();
616 if(!locOrder.containsKey(locID)){
617 throw new Error("Error: The spinning location '"+
618 locID + "' is not defined in the hierarchy of the class '"+cd +"'.");
621 state.addLocationPropertySet(cd, spinLocSet);
623 state.addLocationOrder(cd, locOrder);
626 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
627 ParseNode fieldnode=pn.getChild("field");
628 if (fieldnode!=null) {
629 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
632 ParseNode methodnode=pn.getChild("method");
633 if (methodnode!=null) {
634 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
637 ParseNode innerclassnode=pn.getChild("inner_class_declaration");
638 if (innerclassnode!=null) {
639 parseInnerClassDecl(cn,innerclassnode);
642 ParseNode enumnode=pn.getChild("enum_declaration");
643 if (enumnode!=null) {
644 parseEnumDecl(cn,enumnode);
647 ParseNode flagnode=pn.getChild("flag");
648 if (flagnode!=null) {
649 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
652 // in case there are empty node
653 ParseNode emptynode=pn.getChild("empty");
654 if(emptynode != null) {
660 private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
661 ClassDescriptor icn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
662 icn.setImports(mandatoryImports);
663 icn.setAsInnerClass();
664 icn.setSurroundingClass(cn.getSymbol());
665 icn.setSurrounding(cn);
666 cn.addInnerClass(icn);
667 if (!isEmpty(pn.getChild("super").getTerminal())) {
668 /* parse superclass name */
669 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
670 NameDescriptor nd=parseClassName(snn);
671 icn.setSuper(nd.toString());
673 if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
674 icn.getSymbol().equals(TypeUtil.TagClass)))
675 icn.setSuper(TypeUtil.ObjectClass);
677 // check inherited interfaces
678 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
679 /* parse inherited interface name */
680 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
681 ParseNodeVector pnv=snlist.getChildren();
682 for(int i=0; i<pnv.size(); i++) {
683 ParseNode decl=pnv.elementAt(i);
684 if (isNode(decl,"type")) {
685 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
686 icn.addSuperInterface(nd.toString());
690 icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
691 if(!icn.isStatic()) {
692 throw new Error("Error: inner class " + icn.getSymbol() + " in Class " +
693 cn.getSymbol() + " is not a nested class and is not supported yet!");
695 parseClassBody(icn, pn.getChild("classbody"));
699 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
700 ParseNode tn=pn.getChild("type");
701 String type_st=tn.getTerminal();
702 if(type_st.equals("byte")) {
703 return state.getTypeDescriptor(TypeDescriptor.BYTE);
704 } else if(type_st.equals("short")) {
705 return state.getTypeDescriptor(TypeDescriptor.SHORT);
706 } else if(type_st.equals("boolean")) {
707 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
708 } else if(type_st.equals("int")) {
709 return state.getTypeDescriptor(TypeDescriptor.INT);
710 } else if(type_st.equals("long")) {
711 return state.getTypeDescriptor(TypeDescriptor.LONG);
712 } else if(type_st.equals("char")) {
713 return state.getTypeDescriptor(TypeDescriptor.CHAR);
714 } else if(type_st.equals("float")) {
715 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
716 } else if(type_st.equals("double")) {
717 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
718 } else if(type_st.equals("class")) {
719 ParseNode nn=tn.getChild("class");
720 return state.getTypeDescriptor(parseClassName(nn.getChild("name")));
721 } else if(type_st.equals("array")) {
722 ParseNode nn=tn.getChild("array");
723 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
724 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
725 for(int i=0; i<numdims.intValue(); i++)
726 td=td.makeArray(state);
729 System.out.println(pn.PPrint(2, true));
734 private NameDescriptor parseClassName(ParseNode nn) {
735 ParseNode base=nn.getChild("base");
736 ParseNode id=nn.getChild("identifier");
737 String classname = resolveName(id.getTerminal());
739 return new NameDescriptor(classname);
741 return new NameDescriptor(parseClassName(base.getChild("name")),classname);
744 //This will get the mapping of a terminal class name
745 //to a canonical classname (with imports/package locations in them)
746 private String resolveName(String terminal) {
747 if(mandatoryImports.containsKey(terminal)) {
748 return (String) mandatoryImports.get(terminal);
750 if(multiimports.containsKey(terminal)) {
752 Object o = multiimports.get(terminal);
753 if(o instanceof Error) {
754 throw new Error("Class " + terminal + " is ambiguous. Cause: more than 1 package import contain the same class.");
756 //At this point, if we found a unique class
757 //we can treat it as a single, mandatory import.
758 mandatoryImports.put(terminal, o);
767 //only function difference between this and parseName() is that this
768 //does not look for a import mapping.
769 private NameDescriptor parseName(ParseNode nn) {
770 ParseNode base=nn.getChild("base");
771 ParseNode id=nn.getChild("identifier");
773 return new NameDescriptor(id.getTerminal());
775 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
778 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
779 String name=pn.getChild("name").getTerminal();
780 FlagDescriptor flag=new FlagDescriptor(name);
781 if (pn.getChild("external")!=null)
786 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
787 ParseNode mn=pn.getChild("modifier");
788 Modifiers m=parseModifiersList(mn);
789 if(cn.isInterface()) {
790 // TODO add version for normal Java later
791 // Can only be PUBLIC or STATIC or FINAL
792 if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
793 || (m.isSynchronized())) {
794 throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
796 m.addModifier(Modifiers.PUBLIC);
797 m.addModifier(Modifiers.STATIC);
798 m.addModifier(Modifiers.FINAL);
801 ParseNode tn=pn.getChild("type");
802 TypeDescriptor t=parseTypeDescriptor(tn);
803 assignAnnotationsToType(m,t);
804 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
805 ParseNodeVector pnv=vn.getChildren();
806 boolean isglobal=pn.getChild("global")!=null;
808 for(int i=0; i<pnv.size(); i++) {
809 ParseNode vardecl=pnv.elementAt(i);
810 ParseNode tmp=vardecl;
811 TypeDescriptor arrayt=t;
812 while (tmp.getChild("single")==null) {
813 arrayt=arrayt.makeArray(state);
814 tmp=tmp.getChild("array");
816 String identifier=tmp.getChild("single").getTerminal();
817 ParseNode epn=vardecl.getChild("initializer");
819 ExpressionNode en=null;
821 en=parseExpression(epn.getFirstChild());
822 en.setNumLine(epn.getFirstChild().getLine());
824 // for static field, the initializer should be considered as a
826 boolean isfirst = false;
827 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
829 // the first static block for this class
830 Modifiers m_i=new Modifiers();
831 m_i.addModifier(Modifiers.STATIC);
832 md = new MethodDescriptor(m_i, "staticblocks", false);
833 md.setAsStaticBlock();
839 cn.incStaticBlocks();
840 BlockNode bn=new BlockNode();
841 NameNode nn=new NameNode(new NameDescriptor(identifier));
842 nn.setNumLine(en.getNumLine());
843 AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
844 an.setNumLine(pn.getLine());
845 bn.addBlockStatement(new BlockExpressionNode(an));
847 state.addTreeCode(md,bn);
849 BlockNode obn = state.getMethodBody(md);
850 for(int ii = 0; ii < bn.size(); ii++) {
851 BlockStatementNode bsn = bn.get(ii);
852 obn.addBlockStatement(bsn);
854 state.addTreeCode(md, obn);
861 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
865 private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type){
866 Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
867 for(int i=0; i<annotations.size(); i++) {
868 // it only supports a marker annotation
869 AnnotationDescriptor an=annotations.elementAt(i);
870 type.addAnnotationMarker(an);
876 private ExpressionNode parseExpression(ParseNode pn) {
877 if (isNode(pn,"assignment"))
878 return parseAssignmentExpression(pn);
879 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
880 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
881 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
882 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
883 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
884 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
885 isNode(pn,"rightshift")||isNode(pn,"sub")||
886 isNode(pn,"urightshift")||isNode(pn,"sub")||
887 isNode(pn,"add")||isNode(pn,"mult")||
888 isNode(pn,"div")||isNode(pn,"mod")) {
889 ParseNodeVector pnv=pn.getChildren();
890 ParseNode left=pnv.elementAt(0);
891 ParseNode right=pnv.elementAt(1);
892 Operation op=new Operation(pn.getLabel());
893 OpNode on=new OpNode(parseExpression(left),parseExpression(right),op);
894 on.setNumLine(pn.getLine());
896 } else if (isNode(pn,"unaryplus")||
897 isNode(pn,"unaryminus")||
900 ParseNode left=pn.getFirstChild();
901 Operation op=new Operation(pn.getLabel());
902 OpNode on=new OpNode(parseExpression(left),op);
903 on.setNumLine(pn.getLine());
905 } else if (isNode(pn,"postinc")||
906 isNode(pn,"postdec")) {
907 ParseNode left=pn.getFirstChild();
908 AssignOperation op=new AssignOperation(pn.getLabel());
909 AssignmentNode an=new AssignmentNode(parseExpression(left),null,op);
910 an.setNumLine(pn.getLine());
913 } else if (isNode(pn,"preinc")||
914 isNode(pn,"predec")) {
915 ParseNode left=pn.getFirstChild();
916 AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
917 AssignmentNode an=new AssignmentNode(parseExpression(left),
918 new LiteralNode("integer",new Integer(1)),op);
919 an.setNumLine(pn.getLine());
921 } else if (isNode(pn,"literal")) {
922 String literaltype=pn.getTerminal();
923 ParseNode literalnode=pn.getChild(literaltype);
924 Object literal_obj=literalnode.getLiteral();
925 LiteralNode ln=new LiteralNode(literaltype, literal_obj);
926 ln.setNumLine(pn.getLine());
928 } else if (isNode(pn, "createobject")) {
929 TypeDescriptor td = parseTypeDescriptor(pn);
931 Vector args = parseArgumentList(pn);
932 boolean isglobal = pn.getChild("global") != null || pn.getChild("scratch") != null;
933 String disjointId = null;
934 if (pn.getChild("disjoint") != null) {
935 disjointId = pn.getChild("disjoint").getTerminal();
937 CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
938 con.setNumLine(pn.getLine());
939 for (int i = 0; i < args.size(); i++) {
940 con.addArgument((ExpressionNode) args.get(i));
942 /* Could have flag set or tag added here */
943 if (pn.getChild("flag_list") != null || pn.getChild("tag_list") != null) {
944 FlagEffects fe = new FlagEffects(null);
945 if (pn.getChild("flag_list") != null)
946 parseFlagEffect(fe, pn.getChild("flag_list"));
948 if (pn.getChild("tag_list") != null)
949 parseTagEffect(fe, pn.getChild("tag_list"));
950 con.addFlagEffects(fe);
954 } else if (isNode(pn,"createobjectcls")) {
955 //TODO::: FIX BUG!!! static fields in caller context need to become parameters
956 TypeDescriptor td=parseTypeDescriptor(pn);
958 ClassDescriptor cnnew=new ClassDescriptor(td.getSymbol()+"$"+innerCount, false);
959 cnnew.setImports(mandatoryImports);
960 cnnew.setSuper(td.getSymbol());
961 parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
962 Vector args=parseArgumentList(pn);
964 CreateObjectNode con=new CreateObjectNode(td, false, null);
965 con.setNumLine(pn.getLine());
966 for(int i=0; i<args.size(); i++) {
967 con.addArgument((ExpressionNode)args.get(i));
971 } else if (isNode(pn,"createarray")) {
972 //System.out.println(pn.PPrint(3,true));
973 boolean isglobal=pn.getChild("global")!=null||
974 pn.getChild("scratch")!=null;
975 String disjointId=null;
976 if( pn.getChild("disjoint") != null) {
977 disjointId = pn.getChild("disjoint").getTerminal();
979 TypeDescriptor td=parseTypeDescriptor(pn);
980 Vector args=parseDimExprs(pn);
982 if (pn.getChild("dims_opt").getLiteral()!=null)
983 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
984 for(int i=0; i<(args.size()+num); i++)
985 td=td.makeArray(state);
986 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
987 con.setNumLine(pn.getLine());
988 for(int i=0; i<args.size(); i++) {
989 con.addArgument((ExpressionNode)args.get(i));
992 } if (isNode(pn,"createarray2")) {
993 TypeDescriptor td=parseTypeDescriptor(pn);
995 if (pn.getChild("dims_opt").getLiteral()!=null)
996 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
997 for(int i=0; i<num; i++)
998 td=td.makeArray(state);
999 CreateObjectNode con=new CreateObjectNode(td, false, null);
1000 con.setNumLine(pn.getLine());
1001 ParseNode ipn = pn.getChild("initializer");
1002 Vector initializers=parseVariableInitializerList(ipn);
1003 ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
1004 ain.setNumLine(pn.getLine());
1005 con.addArrayInitializer(ain);
1007 } else if (isNode(pn,"name")) {
1008 NameDescriptor nd=parseName(pn);
1009 NameNode nn=new NameNode(nd);
1010 nn.setNumLine(pn.getLine());
1012 } else if (isNode(pn,"this")) {
1013 NameDescriptor nd=new NameDescriptor("this");
1014 NameNode nn=new NameNode(nd);
1015 nn.setNumLine(pn.getLine());
1017 } else if (isNode(pn,"isavailable")) {
1018 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
1019 NameNode nn=new NameNode(nd);
1020 nn.setNumLine(pn.getLine());
1021 return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
1022 } else if (isNode(pn,"methodinvoke1")) {
1023 NameDescriptor nd=parseName(pn.getChild("name"));
1024 Vector args=parseArgumentList(pn);
1025 MethodInvokeNode min=new MethodInvokeNode(nd);
1026 min.setNumLine(pn.getLine());
1027 for(int i=0; i<args.size(); i++) {
1028 min.addArgument((ExpressionNode)args.get(i));
1031 } else if (isNode(pn,"methodinvoke2")) {
1032 String methodid=pn.getChild("id").getTerminal();
1033 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
1034 Vector args=parseArgumentList(pn);
1035 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
1036 min.setNumLine(pn.getLine());
1037 for(int i=0; i<args.size(); i++) {
1038 min.addArgument((ExpressionNode)args.get(i));
1041 } else if (isNode(pn,"fieldaccess")) {
1042 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1043 String fieldname=pn.getChild("field").getTerminal();
1045 FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1046 fan.setNumLine(pn.getLine());
1048 } else if (isNode(pn,"arrayaccess")) {
1049 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1050 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
1051 ArrayAccessNode aan=new ArrayAccessNode(en,index);
1052 aan.setNumLine(pn.getLine());
1054 } else if (isNode(pn,"cast1")) {
1056 CastNode cn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
1057 cn.setNumLine(pn.getLine());
1059 } catch (Exception e) {
1060 System.out.println(pn.PPrint(1,true));
1061 e.printStackTrace();
1064 } else if (isNode(pn,"cast2")) {
1065 CastNode cn=new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
1066 cn.setNumLine(pn.getLine());
1068 } else if (isNode(pn, "getoffset")) {
1069 TypeDescriptor td=parseTypeDescriptor(pn);
1070 String fieldname = pn.getChild("field").getTerminal();
1071 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
1072 return new OffsetNode(td, fieldname);
1073 } else if (isNode(pn, "tert")) {
1075 TertiaryNode tn=new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
1076 parseExpression(pn.getChild("trueexpr").getFirstChild()),
1077 parseExpression(pn.getChild("falseexpr").getFirstChild()) );
1078 tn.setNumLine(pn.getLine());
1081 } else if (isNode(pn, "instanceof")) {
1082 ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
1083 TypeDescriptor t=parseTypeDescriptor(pn);
1084 InstanceOfNode ion=new InstanceOfNode(exp,t);
1085 ion.setNumLine(pn.getLine());
1087 } else if (isNode(pn, "array_initializer")) {
1088 Vector initializers=parseVariableInitializerList(pn);
1089 return new ArrayInitializerNode(initializers);
1090 } else if (isNode(pn, "class_type")) {
1091 TypeDescriptor td=parseTypeDescriptor(pn);
1092 ClassTypeNode ctn=new ClassTypeNode(td);
1093 ctn.setNumLine(pn.getLine());
1095 } else if (isNode(pn, "empty")) {
1098 System.out.println("---------------------");
1099 System.out.println(pn.PPrint(3,true));
1104 private Vector parseDimExprs(ParseNode pn) {
1105 Vector arglist=new Vector();
1106 ParseNode an=pn.getChild("dim_exprs");
1107 if (an==null) /* No argument list */
1109 ParseNodeVector anv=an.getChildren();
1110 for(int i=0; i<anv.size(); i++) {
1111 arglist.add(parseExpression(anv.elementAt(i)));
1116 private Vector parseArgumentList(ParseNode pn) {
1117 Vector arglist=new Vector();
1118 ParseNode an=pn.getChild("argument_list");
1119 if (an==null) /* No argument list */
1121 ParseNodeVector anv=an.getChildren();
1122 for(int i=0; i<anv.size(); i++) {
1123 arglist.add(parseExpression(anv.elementAt(i)));
1128 private Vector[] parseConsArgumentList(ParseNode pn) {
1129 Vector arglist=new Vector();
1130 Vector varlist=new Vector();
1131 ParseNode an=pn.getChild("cons_argument_list");
1132 if (an==null) /* No argument list */
1133 return new Vector[] {varlist, arglist};
1134 ParseNodeVector anv=an.getChildren();
1135 for(int i=0; i<anv.size(); i++) {
1136 ParseNode cpn=anv.elementAt(i);
1137 ParseNode var=cpn.getChild("var");
1138 ParseNode exp=cpn.getChild("exp").getFirstChild();
1139 varlist.add(var.getTerminal());
1140 arglist.add(parseExpression(exp));
1142 return new Vector[] {varlist, arglist};
1145 private Vector parseVariableInitializerList(ParseNode pn) {
1146 Vector varInitList=new Vector();
1147 ParseNode vin=pn.getChild("var_init_list");
1148 if (vin==null) /* No argument list */
1150 ParseNodeVector vinv=vin.getChildren();
1151 for(int i=0; i<vinv.size(); i++) {
1152 varInitList.add(parseExpression(vinv.elementAt(i)));
1157 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
1158 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1159 ParseNodeVector pnv=pn.getChild("args").getChildren();
1161 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
1162 an.setNumLine(pn.getLine());
1167 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1168 ParseNode headern=pn.getChild("method_header");
1169 ParseNode bodyn=pn.getChild("body");
1170 MethodDescriptor md=parseMethodHeader(headern);
1172 BlockNode bn=parseBlock(bodyn);
1173 bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1175 state.addTreeCode(md,bn);
1177 // this is a hack for investigating new language features
1178 // at the AST level, someday should evolve into a nice compiler
1180 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
1181 // md.getSymbol().equals( ***put your method in here like: "main" )
1183 // bn.setStyle( BlockNode.NORMAL );
1184 // System.out.println( bn.printNode( 0 ) );
1187 } catch (Exception e) {
1188 System.out.println("Error with method:"+md.getSymbol());
1189 e.printStackTrace();
1192 System.out.println("Error with method:"+md.getSymbol());
1193 e.printStackTrace();
1198 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1199 ParseNode mn=pn.getChild("modifiers");
1200 Modifiers m=parseModifiersList(mn);
1201 ParseNode cdecl=pn.getChild("constructor_declarator");
1202 boolean isglobal=cdecl.getChild("global")!=null;
1203 String name=resolveName(cn.getSymbol());
1204 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1205 ParseNode paramnode=cdecl.getChild("parameters");
1206 parseParameterList(md,paramnode);
1207 ParseNode bodyn0=pn.getChild("body");
1208 ParseNode bodyn=bodyn0.getChild("constructor_body");
1211 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1212 bn=parseBlock(bodyn);
1215 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1216 ParseNode sin=bodyn.getChild("superinvoke");
1217 NameDescriptor nd=new NameDescriptor("super");
1218 Vector args=parseArgumentList(sin);
1219 MethodInvokeNode min=new MethodInvokeNode(nd);
1220 min.setNumLine(sin.getLine());
1221 for(int i=0; i<args.size(); i++) {
1222 min.addArgument((ExpressionNode)args.get(i));
1224 BlockExpressionNode ben=new BlockExpressionNode(min);
1225 bn.addFirstBlockStatement(ben);
1227 } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1228 ParseNode eci=bodyn.getChild("explconstrinv");
1229 NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1230 Vector args=parseArgumentList(eci);
1231 MethodInvokeNode min=new MethodInvokeNode(nd);
1232 min.setNumLine(eci.getLine());
1233 for(int i=0; i<args.size(); i++) {
1234 min.addArgument((ExpressionNode)args.get(i));
1236 BlockExpressionNode ben=new BlockExpressionNode(min);
1237 ben.setNumLine(eci.getLine());
1238 bn.addFirstBlockStatement(ben);
1240 state.addTreeCode(md,bn);
1243 private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1244 // Each class maintains one MethodDecscriptor which combines all its
1245 // static blocks in their declaration order
1246 boolean isfirst = false;
1247 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1249 // the first static block for this class
1250 Modifiers m_i=new Modifiers();
1251 m_i.addModifier(Modifiers.STATIC);
1252 md = new MethodDescriptor(m_i, "staticblocks", false);
1253 md.setAsStaticBlock();
1256 ParseNode bodyn=pn.getChild("body");
1260 cn.incStaticBlocks();
1262 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1263 bn=parseBlock(bodyn);
1267 state.addTreeCode(md,bn);
1269 BlockNode obn = state.getMethodBody(md);
1270 for(int i = 0; i < bn.size(); i++) {
1271 BlockStatementNode bsn = bn.get(i);
1272 obn.addBlockStatement(bsn);
1274 state.addTreeCode(md, obn);
1279 public BlockNode parseBlock(ParseNode pn) {
1280 this.m_taskexitnum = 0;
1281 if (pn==null||isEmpty(pn.getTerminal()))
1282 return new BlockNode();
1283 ParseNode bsn=pn.getChild("block_statement_list");
1284 return parseBlockHelper(bsn);
1287 private BlockNode parseBlockHelper(ParseNode pn) {
1288 ParseNodeVector pnv=pn.getChildren();
1289 BlockNode bn=new BlockNode();
1290 for(int i=0; i<pnv.size(); i++) {
1291 Vector bsv=parseBlockStatement(pnv.elementAt(i));
1292 for(int j=0; j<bsv.size(); j++) {
1293 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1299 public BlockNode parseSingleBlock(ParseNode pn) {
1300 BlockNode bn=new BlockNode();
1301 Vector bsv=parseBlockStatement(pn);
1302 for(int j=0; j<bsv.size(); j++) {
1303 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1305 bn.setStyle(BlockNode.NOBRACES);
1309 public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
1310 ParseNodeVector pnv=pn.getChildren();
1311 Vector bv=new Vector();
1312 for(int i=0; i<pnv.size(); i++) {
1313 bv.addAll(parseBlockStatement(pnv.elementAt(i)));
1318 public Vector parseBlockStatement(ParseNode pn) {
1319 Vector blockstatements=new Vector();
1320 if (isNode(pn,"tag_declaration")) {
1321 String name=pn.getChild("single").getTerminal();
1322 String type=pn.getChild("type").getTerminal();
1324 TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1325 tdn.setNumLine(pn.getLine());
1327 blockstatements.add(tdn);
1328 } else if (isNode(pn,"local_variable_declaration")) {
1330 ParseNode mn=pn.getChild("modifiers");
1331 TypeDescriptor t=parseTypeDescriptor(pn);
1333 Modifiers m=parseModifiersList(mn);
1334 assignAnnotationsToType(m, t);
1336 ParseNode vn=pn.getChild("variable_declarators_list");
1337 ParseNodeVector pnv=vn.getChildren();
1338 for(int i=0; i<pnv.size(); i++) {
1339 ParseNode vardecl=pnv.elementAt(i);
1342 ParseNode tmp=vardecl;
1343 TypeDescriptor arrayt=t;
1345 while (tmp.getChild("single")==null) {
1346 arrayt=arrayt.makeArray(state);
1347 tmp=tmp.getChild("array");
1349 String identifier=tmp.getChild("single").getTerminal();
1351 ParseNode epn=vardecl.getChild("initializer");
1354 ExpressionNode en=null;
1356 en=parseExpression(epn.getFirstChild());
1358 DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1359 dn.setNumLine(tmp.getLine());
1361 blockstatements.add(dn);
1363 } else if (isNode(pn,"nop")) {
1365 } else if (isNode(pn,"expression")) {
1366 BlockExpressionNode ben=new BlockExpressionNode(parseExpression(pn.getFirstChild()));
1367 ben.setNumLine(pn.getLine());
1368 blockstatements.add(ben);
1369 } else if (isNode(pn,"ifstatement")) {
1370 IfStatementNode isn=new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1371 parseSingleBlock(pn.getChild("statement").getFirstChild()),
1372 pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null);
1373 isn.setNumLine(pn.getLine());
1375 blockstatements.add(isn);
1376 } else if (isNode(pn,"switch_statement")) {
1377 // TODO add version for normal Java later
1378 SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1379 parseSingleBlock(pn.getChild("statement").getFirstChild()));
1380 ssn.setNumLine(pn.getLine());
1381 blockstatements.add(ssn);
1382 } else if (isNode(pn,"switch_block_list")) {
1383 // TODO add version for normal Java later
1384 ParseNodeVector pnv=pn.getChildren();
1385 for(int i=0; i<pnv.size(); i++) {
1386 ParseNode sblockdecl=pnv.elementAt(i);
1388 if(isNode(sblockdecl, "switch_block")) {
1389 ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1390 ParseNodeVector labelv=lpn.getChildren();
1391 Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1392 for(int j=0; j<labelv.size(); j++) {
1393 ParseNode labeldecl=labelv.elementAt(j);
1394 if(isNode(labeldecl, "switch_label")) {
1395 SwitchLabelNode sln=new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false);
1396 sln.setNumLine(labeldecl.getLine());
1397 slv.addElement(sln);
1398 } else if(isNode(labeldecl, "default_switch_label")) {
1399 SwitchLabelNode sln=new SwitchLabelNode(null, true);
1400 sln.setNumLine(labeldecl.getLine());
1401 slv.addElement(sln);
1405 SwitchBlockNode sbn=new SwitchBlockNode(slv,
1406 parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild()));
1407 sbn.setNumLine(sblockdecl.getLine());
1409 blockstatements.add(sbn);
1413 } else if (isNode(pn, "trycatchstatement")) {
1414 // TODO add version for normal Java later
1415 // Do not fully support exceptions now. Only make sure that if there are no
1416 // exceptions thrown, the execution is right
1417 ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1418 BlockNode bn=parseBlockHelper(tpn);
1419 blockstatements.add(new SubBlockNode(bn));
1421 ParseNode fbk = pn.getChild("finallyblock");
1423 ParseNode fpn = fbk.getFirstChild();
1424 BlockNode fbn=parseBlockHelper(fpn);
1425 blockstatements.add(new SubBlockNode(fbn));
1427 } else if (isNode(pn, "throwstatement")) {
1428 // TODO Simply return here
1429 //blockstatements.add(new ReturnNode());
1430 } else if (isNode(pn,"taskexit")) {
1432 if (pn.getChild("flag_effects_list")!=null)
1433 vfe=parseFlags(pn.getChild("flag_effects_list"));
1435 if (pn.getChild("cons_checks")!=null)
1436 ccs=parseChecks(pn.getChild("cons_checks"));
1437 TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1438 ten.setNumLine(pn.getLine());
1439 blockstatements.add(ten);
1440 } else if (isNode(pn,"atomic")) {
1441 BlockNode bn=parseBlockHelper(pn);
1442 AtomicNode an=new AtomicNode(bn);
1443 an.setNumLine(pn.getLine());
1444 blockstatements.add(an);
1445 } else if (isNode(pn,"synchronized")) {
1446 BlockNode bn=parseBlockHelper(pn.getChild("block"));
1447 ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
1448 SynchronizedNode sn=new SynchronizedNode(en, bn);
1449 sn.setNumLine(pn.getLine());
1450 blockstatements.add(sn);
1451 } else if (isNode(pn,"return")) {
1452 if (isEmpty(pn.getTerminal()))
1453 blockstatements.add(new ReturnNode());
1455 ExpressionNode en=parseExpression(pn.getFirstChild());
1456 ReturnNode rn=new ReturnNode(en);
1457 rn.setNumLine(pn.getLine());
1458 blockstatements.add(rn);
1460 } else if (isNode(pn,"block_statement_list")) {
1461 BlockNode bn=parseBlockHelper(pn);
1462 blockstatements.add(new SubBlockNode(bn));
1463 } else if (isNode(pn,"empty")) {
1465 } else if (isNode(pn,"statement_expression_list")) {
1466 ParseNodeVector pnv=pn.getChildren();
1467 BlockNode bn=new BlockNode();
1468 for(int i=0; i<pnv.size(); i++) {
1469 ExpressionNode en=parseExpression(pnv.elementAt(i));
1470 blockstatements.add(new BlockExpressionNode(en));
1472 bn.setStyle(BlockNode.EXPRLIST);
1473 } else if (isNode(pn,"forstatement")) {
1474 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
1475 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
1476 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1477 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1478 if(condition == null) {
1479 // no condition clause, make a 'true' expression as the condition
1480 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1482 LoopNode ln=new LoopNode(init,condition,update,body);
1483 ln.setNumLine(pn.getLine());
1484 blockstatements.add(ln);
1485 } else if (isNode(pn,"whilestatement")) {
1486 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1487 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1488 if(condition == null) {
1489 // no condition clause, make a 'true' expression as the condition
1490 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1492 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
1493 } else if (isNode(pn,"dowhilestatement")) {
1494 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1495 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1496 if(condition == null) {
1497 // no condition clause, make a 'true' expression as the condition
1498 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1500 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
1501 } else if (isNode(pn,"sese")) {
1502 ParseNode pnID=pn.getChild("identifier");
1504 if( pnID != null ) { stID=pnID.getFirstChild().getTerminal(); }
1505 SESENode start=new SESENode(stID);
1506 start.setNumLine(pn.getLine());
1507 SESENode end =new SESENode(stID);
1508 start.setEnd( end );
1509 end.setStart( start );
1510 blockstatements.add(start);
1511 blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1512 blockstatements.add(end);
1513 } else if (isNode(pn,"continue")) {
1514 ContinueBreakNode cbn=new ContinueBreakNode(false);
1515 cbn.setNumLine(pn.getLine());
1516 blockstatements.add(cbn);
1517 } else if (isNode(pn,"break")) {
1518 ContinueBreakNode cbn=new ContinueBreakNode(true);
1519 cbn.setNumLine(pn.getLine());
1520 blockstatements.add(cbn);
1521 ParseNode idopt_pn=pn.getChild("identifier_opt");
1522 ParseNode name_pn=idopt_pn.getChild("name");
1523 // name_pn.getTerminal() gives you the label
1524 } else if (isNode(pn,"genreach")) {
1525 String graphName = pn.getChild("graphName").getTerminal();
1526 blockstatements.add( new GenReachNode( graphName ) );
1528 } else if(isNode(pn,"labeledstatement")){
1529 String label = pn.getChild("name").getTerminal();
1530 BlockNode bn=parseSingleBlock(pn.getChild("statement").getFirstChild());
1532 blockstatements.add(new SubBlockNode(bn));
1534 System.out.println("---------------");
1535 System.out.println(pn.PPrint(3,true));
1538 return blockstatements;
1541 public MethodDescriptor parseMethodHeader(ParseNode pn) {
1542 ParseNode mn=pn.getChild("modifiers");
1543 Modifiers m=parseModifiersList(mn);
1545 ParseNode tn=pn.getChild("returntype");
1546 TypeDescriptor returntype;
1548 returntype=parseTypeDescriptor(tn);
1550 returntype=new TypeDescriptor(TypeDescriptor.VOID);
1552 ParseNode pmd=pn.getChild("method_declarator");
1553 String name=pmd.getChild("name").getTerminal();
1554 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1556 ParseNode paramnode=pmd.getChild("parameters");
1557 parseParameterList(md,paramnode);
1561 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1562 ParseNode paramlist=pn.getChild("formal_parameter_list");
1563 if (paramlist==null)
1565 ParseNodeVector pnv=paramlist.getChildren();
1566 for(int i=0; i<pnv.size(); i++) {
1567 ParseNode paramn=pnv.elementAt(i);
1569 if (isNode(paramn, "tag_parameter")) {
1570 String paramname=paramn.getChild("single").getTerminal();
1571 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1572 md.addTagParameter(type, paramname);
1575 TypeDescriptor type=parseTypeDescriptor(paramn);
1577 ParseNode tmp=paramn;
1578 while (tmp.getChild("single")==null) {
1579 type=type.makeArray(state);
1580 tmp=tmp.getChild("array");
1582 String paramname=tmp.getChild("single").getTerminal();
1584 md.addParameter(type, paramname);
1585 if(isNode(paramn, "annotation_parameter")){
1586 ParseNode bodynode=paramn.getChild("annotation_body");
1587 parseParameterAnnotation(bodynode,type);
1594 public Modifiers parseModifiersList(ParseNode pn) {
1595 Modifiers m=new Modifiers();
1596 ParseNode modlist=pn.getChild("modifier_list");
1597 if (modlist!=null) {
1598 ParseNodeVector pnv=modlist.getChildren();
1599 for(int i=0; i<pnv.size(); i++) {
1600 ParseNode modn=pnv.elementAt(i);
1601 if (isNode(modn,"public"))
1602 m.addModifier(Modifiers.PUBLIC);
1603 else if (isNode(modn,"protected"))
1604 m.addModifier(Modifiers.PROTECTED);
1605 else if (isNode(modn,"private"))
1606 m.addModifier(Modifiers.PRIVATE);
1607 else if (isNode(modn,"static"))
1608 m.addModifier(Modifiers.STATIC);
1609 else if (isNode(modn,"final"))
1610 m.addModifier(Modifiers.FINAL);
1611 else if (isNode(modn,"native"))
1612 m.addModifier(Modifiers.NATIVE);
1613 else if (isNode(modn,"synchronized"))
1614 m.addModifier(Modifiers.SYNCHRONIZED);
1615 else if (isNode(modn,"atomic"))
1616 m.addModifier(Modifiers.ATOMIC);
1617 else if (isNode(modn,"abstract"))
1618 m.addModifier(Modifiers.ABSTRACT);
1619 else if (isNode(modn,"volatile"))
1620 m.addModifier(Modifiers.VOLATILE);
1621 else if (isNode(modn,"transient"))
1622 m.addModifier(Modifiers.TRANSIENT);
1623 else if(isNode(modn,"annotation_list"))
1624 parseAnnotationList(modn,m);
1626 throw new Error("Unrecognized Modifier:"+modn.getLabel());}
1632 private void parseAnnotationList(ParseNode pn, Modifiers m) {
1633 ParseNodeVector pnv = pn.getChildren();
1634 for (int i = 0; i < pnv.size(); i++) {
1635 ParseNode body_list = pnv.elementAt(i);
1636 if (isNode(body_list, "annotation_body")) {
1637 ParseNode body_node = body_list.getFirstChild();
1638 if (isNode(body_node, "marker_annotation")) {
1639 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1640 } else if (isNode(body_node, "single_annotation")) {
1641 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1642 body_node.getChild("element_value").getTerminal()));
1643 } else if (isNode(body_node, "normal_annotation")) {
1644 throw new Error("Annotation with multiple data members is not supported yet.");
1650 private void parseParameterAnnotation(ParseNode body_list,TypeDescriptor type){
1651 ParseNode body_node = body_list.getFirstChild();
1652 if (isNode(body_node, "marker_annotation")) {
1653 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1654 } else if (isNode(body_node, "single_annotation")) {
1655 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1656 body_node.getChild("element_value").getTerminal()));
1657 } else if (isNode(body_node, "normal_annotation")) {
1658 throw new Error("Annotation with multiple data members is not supported yet.");
1662 private boolean isNode(ParseNode pn, String label) {
1663 if (pn.getLabel().equals(label))
1668 private static boolean isEmpty(ParseNode pn) {
1669 if (pn.getLabel().equals("empty"))
1675 private static boolean isEmpty(String s) {
1676 if (s.equals("empty"))
1682 /** Throw an exception if something is unexpected */
1683 private void check(ParseNode pn, String label) {
1685 throw new Error(pn+ "IE: Expected '" + label + "', got null");
1687 if (!pn.getLabel().equals(label)) {
1688 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");