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();
46 //add java.lang as our default multi-import
47 this.addMultiImport(sourcefile, "java.lang", false);
50 ParseNode ipn = pn.getChild("imports").getChild("import_decls_list");
52 ParseNodeVector pnv = ipn.getChildren();
53 for (int i = 0; i < pnv.size(); i++) {
54 ParseNode pnimport = pnv.elementAt(i);
55 NameDescriptor nd = parseName(pnimport.getChild("name"));
56 if (isNode(pnimport, "import_single")) {
57 if (!mandatoryImports.containsKey(nd.getIdentifier())) {
58 // map name to full name (includes package/directory
59 mandatoryImports.put(nd.getIdentifier(), nd.getPathFromRootToHere());
61 throw new Error("An ambiguous class "+ nd.getIdentifier() +" has been found. It is included for " +
62 ((String)mandatoryImports.get(nd.getIdentifier())) + " and " +
63 nd.getPathFromRootToHere());
67 addMultiImport(sourcefile, nd.getPathFromRootToHere(), false);
72 ParseNode ppn=pn.getChild("packages").getChild("package");
73 String packageName = null;
75 NameDescriptor nd = parseClassName(ppn.getChild("name"));
76 packageName = nd.getPathFromRootToHere();
77 //Trick -> import the package directory as a multi-import and it'll
78 //automatically recognize files in the same directory.
79 addMultiImport(sourcefile, packageName, true);
82 ParseNode tpn=pn.getChild("type_declaration_list");
84 ParseNodeVector pnv = tpn.getChildren();
85 for (int i = 0; i < pnv.size(); i++) {
86 ParseNode type_pn = pnv.elementAt(i);
87 if (isEmpty(type_pn)) /* Skip the semicolon */
89 if (isNode(type_pn, "class_declaration")) {
90 ClassDescriptor cn = parseTypeDecl(type_pn, packageName);
91 cn.setSourceFileName(sourcefile);
92 parseInitializers(cn);
93 if (toanalyze != null)
96 // for inner classes/enum
97 HashSet tovisit = new HashSet();
98 Iterator it_icds = cn.getInnerClasses();
99 while (it_icds.hasNext()) {
100 tovisit.add(it_icds.next());
103 while (!tovisit.isEmpty()) {
104 ClassDescriptor cd = (ClassDescriptor) tovisit.iterator().next();
106 parseInitializers(cd);
107 if (toanalyze != null) {
110 cd.setSourceFileName(sourcefile);
113 Iterator it_ics = cd.getInnerClasses();
114 while (it_ics.hasNext()) {
115 tovisit.add(it_ics.next());
118 Iterator it_ienums = cd.getEnum();
119 while (it_ienums.hasNext()) {
120 ClassDescriptor iecd = (ClassDescriptor) it_ienums.next();
121 if (toanalyze != null) {
124 iecd.setSourceFileName(sourcefile);
125 state.addClass(iecd);
129 Iterator it_enums = cn.getEnum();
130 while (it_enums.hasNext()) {
131 ClassDescriptor ecd = (ClassDescriptor) it_enums.next();
132 if (toanalyze != null) {
135 ecd.setSourceFileName(sourcefile);
138 } else if (isNode(type_pn, "task_declaration")) {
139 TaskDescriptor td = parseTaskDecl(type_pn);
140 if (toanalyze != null)
143 } else if (isNode(type_pn, "interface_declaration")) {
144 // TODO add version for normal Java later
145 ClassDescriptor cn = parseInterfaceDecl(type_pn, packageName);
146 if (toanalyze != null)
148 cn.setSourceFileName(sourcefile);
152 Iterator it_enums = cn.getEnum();
153 while (it_enums.hasNext()) {
154 ClassDescriptor ecd = (ClassDescriptor) it_enums.next();
155 if (toanalyze != null) {
158 ecd.setSourceFileName(sourcefile);
161 } else if (isNode(type_pn, "enum_declaration")) {
162 // TODO add version for normal Java later
163 ClassDescriptor cn = parseEnumDecl(null, type_pn);
164 if (toanalyze != null)
166 cn.setSourceFileName(sourcefile);
169 throw new Error(type_pn.getLabel());
175 //This kind of breaks away from tradition a little bit by doing the file checks here
176 // instead of in Semantic check, but doing it here is easier because we have a mapping early on
177 // if I wait until semantic check, I have to change ALL the type descriptors to match the new
178 // mapping and that's both ugly and tedious.
179 private void addMultiImport(String currentSource, String importPath, boolean isPackageDirectory) {
180 boolean found = false;
181 for (int j = 0; j < state.classpath.size(); j++) {
182 String path = (String) state.classpath.get(j);
183 File folder = new File(path, importPath.replace('.', '/'));
184 if (folder.exists()) {
186 for (String file : folder.list()) {
187 // if the file is of type *.java add to multiImport list.
188 if (file.lastIndexOf('.') != -1 && file.substring(file.lastIndexOf('.')).equalsIgnoreCase(".java")) {
189 String classname = file.substring(0, file.length() - 5);
190 // single imports have precedence over multi-imports
191 if (!mandatoryImports.containsKey(classname)) {
192 //package files have precedence over multi-imports.
193 if (multiimports.containsKey(classname) && !isPackageDirectory) {
194 // put error in for later, in case we try to import
195 multiimports.put(classname, new Error("Error: class " + classname + " is defined more than once in a multi-import in " + currentSource));
197 multiimports.put(classname, importPath + "." + classname);
207 throw new Error("Import package " + importPath + " in " + currentSource
208 + " cannot be resolved.");
212 public void parseInitializers(ClassDescriptor cn){
213 Vector fv=cn.getFieldVec();
215 for(int i=0;i<fv.size();i++) {
216 FieldDescriptor fd=(FieldDescriptor)fv.get(i);
217 if(fd.getExpressionNode()!=null) {
218 Iterator methodit = cn.getMethods();
219 while(methodit.hasNext()){
220 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
221 if(currmd.isConstructor()){
222 BlockNode bn=state.getMethodBody(currmd);
223 NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
224 AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
225 bn.addBlockStatementAt(new BlockExpressionNode(an), pos);
233 private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {
234 ClassDescriptor ecd=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
235 ecd.setImports(mandatoryImports);
238 ecd.setSurroundingClass(cn.getSymbol());
239 ecd.setSurrounding(cn);
242 if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
243 ecd.getSymbol().equals(TypeUtil.TagClass))) {
244 ecd.setSuper(TypeUtil.ObjectClass);
246 ecd.setModifiers(parseModifiersList(pn.getChild("modifiers")));
247 parseEnumBody(ecd, pn.getChild("enumbody"));
251 private void parseEnumBody(ClassDescriptor cn, ParseNode pn) {
252 ParseNode decls=pn.getChild("enum_constants_list");
254 ParseNodeVector pnv=decls.getChildren();
255 for(int i=0; i<pnv.size(); i++) {
256 ParseNode decl=pnv.elementAt(i);
257 if (isNode(decl,"enum_constant")) {
258 parseEnumConstant(cn,decl);
259 } else throw new Error();
264 private void parseEnumConstant(ClassDescriptor cn, ParseNode pn) {
265 cn.addEnumConstant(pn.getChild("name").getTerminal());
268 public ClassDescriptor parseInterfaceDecl(ParseNode pn, String packageName) {
270 if(packageName == null) {
271 cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
273 String newClassname = packageName + "." + pn.getChild("name").getTerminal();
274 cn= new ClassDescriptor(packageName, newClassname, true);
277 cn.setImports(mandatoryImports);
278 //cn.setAsInterface();
279 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
280 /* parse inherited interface name */
281 ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
282 ParseNodeVector pnv=snlist.getChildren();
283 for(int i=0; i<pnv.size(); i++) {
284 ParseNode decl=pnv.elementAt(i);
285 if (isNode(decl,"type")) {
286 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
287 cn.addSuperInterface(nd.toString());
291 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
292 parseInterfaceBody(cn, pn.getChild("interfacebody"));
296 private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
297 assert(cn.isInterface());
298 ParseNode decls=pn.getChild("interface_member_declaration_list");
300 ParseNodeVector pnv=decls.getChildren();
301 for(int i=0; i<pnv.size(); i++) {
302 ParseNode decl=pnv.elementAt(i);
303 if (isNode(decl,"constant")) {
304 parseInterfaceConstant(cn,decl);
305 } else if (isNode(decl,"method")) {
306 parseInterfaceMethod(cn,decl.getChild("method_declaration"));
307 } else throw new Error();
312 private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
314 parseFieldDecl(cn,pn.getChild("field_declaration"));
320 private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
321 ParseNode headern=pn.getChild("header");
322 ParseNode bodyn=pn.getChild("body");
323 MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
324 md.getModifiers().addModifier(Modifiers.PUBLIC);
325 md.getModifiers().addModifier(Modifiers.ABSTRACT);
327 BlockNode bn=parseBlock(bodyn);
329 state.addTreeCode(md,bn);
331 // this is a hack for investigating new language features
332 // at the AST level, someday should evolve into a nice compiler
334 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
335 // md.getSymbol().equals( ***put your method in here like: "main" )
337 // bn.setStyle( BlockNode.NORMAL );
338 // System.out.println( bn.printNode( 0 ) );
341 } catch (Exception e) {
342 System.out.println("Error with method:"+md.getSymbol());
346 System.out.println("Error with method:"+md.getSymbol());
352 public TaskDescriptor parseTaskDecl(ParseNode pn) {
353 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
354 ParseNode bodyn=pn.getChild("body");
355 BlockNode bn=parseBlock(bodyn);
356 parseParameterList(td, pn);
357 state.addTreeCode(td,bn);
358 if (pn.getChild("flag_effects_list")!=null)
359 td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
363 public Vector parseFlags(ParseNode pn) {
364 Vector vfe=new Vector();
365 ParseNodeVector pnv=pn.getChildren();
366 for(int i=0; i<pnv.size(); i++) {
367 ParseNode fn=pnv.elementAt(i);
368 FlagEffects fe=parseFlagEffects(fn);
374 public FlagEffects parseFlagEffects(ParseNode pn) {
375 if (isNode(pn,"flag_effect")) {
376 String flagname=pn.getChild("name").getTerminal();
377 FlagEffects fe=new FlagEffects(flagname);
378 if (pn.getChild("flag_list")!=null)
379 parseFlagEffect(fe, pn.getChild("flag_list"));
380 if (pn.getChild("tag_list")!=null)
381 parseTagEffect(fe, pn.getChild("tag_list"));
383 } else throw new Error();
386 public void parseTagEffect(FlagEffects fes, ParseNode pn) {
387 ParseNodeVector pnv=pn.getChildren();
388 for(int i=0; i<pnv.size(); i++) {
389 ParseNode pn2=pnv.elementAt(i);
391 if (isNode(pn2,"not")) {
393 pn2=pn2.getChild("name");
395 String name=pn2.getTerminal();
396 fes.addTagEffect(new TagEffect(name,status));
400 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
401 ParseNodeVector pnv=pn.getChildren();
402 for(int i=0; i<pnv.size(); i++) {
403 ParseNode pn2=pnv.elementAt(i);
405 if (isNode(pn2,"not")) {
407 pn2=pn2.getChild("name");
409 String name=pn2.getTerminal();
410 fes.addEffect(new FlagEffect(name,status));
414 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
415 if (isNode(pn,"or")) {
416 ParseNodeVector pnv=pn.getChildren();
417 ParseNode left=pnv.elementAt(0);
418 ParseNode right=pnv.elementAt(1);
419 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
420 } else if (isNode(pn,"and")) {
421 ParseNodeVector pnv=pn.getChildren();
422 ParseNode left=pnv.elementAt(0);
423 ParseNode right=pnv.elementAt(1);
424 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
425 } else if (isNode(pn, "not")) {
426 ParseNodeVector pnv=pn.getChildren();
427 ParseNode left=pnv.elementAt(0);
428 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
430 } else if (isNode(pn,"name")) {
431 return new FlagNode(pn.getTerminal());
437 public Vector parseChecks(ParseNode pn) {
438 Vector ccs=new Vector();
439 ParseNodeVector pnv=pn.getChildren();
440 for(int i=0; i<pnv.size(); i++) {
441 ParseNode fn=pnv.elementAt(i);
442 ConstraintCheck cc=parseConstraintCheck(fn);
448 public ConstraintCheck parseConstraintCheck(ParseNode pn) {
449 if (isNode(pn,"cons_check")) {
450 String specname=pn.getChild("name").getChild("identifier").getTerminal();
451 Vector[] args=parseConsArgumentList(pn);
452 ConstraintCheck cc=new ConstraintCheck(specname);
453 for(int i=0; i<args[0].size(); i++) {
454 cc.addVariable((String)args[0].get(i));
455 cc.addArgument((ExpressionNode)args[1].get(i));
458 } else throw new Error();
461 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
464 ParseNode paramlist=pn.getChild("task_parameter_list");
467 ParseNodeVector pnv=paramlist.getChildren();
468 for(int i=0; i<pnv.size(); i++) {
469 ParseNode paramn=pnv.elementAt(i);
470 if(paramn.getChild("optional")!=null) {
472 paramn = paramn.getChild("optional").getFirstChild();
473 System.out.println("OPTIONAL FOUND!!!!!!!");
474 } else { optional = false;
475 System.out.println("NOT OPTIONAL");}
477 TypeDescriptor type=parseTypeDescriptor(paramn);
479 String paramname=paramn.getChild("single").getTerminal();
480 FlagExpressionNode fen=null;
481 if (paramn.getChild("flag")!=null)
482 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
484 ParseNode tagnode=paramn.getChild("tag");
486 TagExpressionList tel=null;
488 tel=parseTagExpressionList(tagnode);
491 td.addParameter(type,paramname,fen, tel, optional);
495 public TagExpressionList parseTagExpressionList(ParseNode pn) {
496 //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
497 //To test, feed in any input program that uses tags
498 ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
499 TagExpressionList tel=new TagExpressionList();
500 for(int i=0; i<pnv.size(); i++) {
501 ParseNode tn=pnv.elementAt(i);
502 String type=tn.getChild("type").getTerminal();
503 String name=tn.getChild("single").getTerminal();
504 tel.addTag(type, name);
509 public ClassDescriptor parseTypeDecl(ParseNode pn, String packageName) {
511 // if is in no package, then create a class descriptor with just the name.
512 // else add the package on
513 if(packageName == null) {
514 cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
516 String newClassname = packageName + "." + pn.getChild("name").getTerminal();
517 cn= new ClassDescriptor(packageName, newClassname, false);
519 cn.setImports(mandatoryImports);
520 if (!isEmpty(pn.getChild("super").getTerminal())) {
521 /* parse superclass name */
522 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
523 NameDescriptor nd=parseClassName(snn);
524 cn.setSuper(nd.toString());
526 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
527 cn.getSymbol().equals(TypeUtil.TagClass)))
528 cn.setSuper(TypeUtil.ObjectClass);
530 // check inherited interfaces
531 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
532 /* parse inherited interface name */
533 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
534 ParseNodeVector pnv=snlist.getChildren();
535 for(int i=0; i<pnv.size(); i++) {
536 ParseNode decl=pnv.elementAt(i);
537 if (isNode(decl,"type")) {
538 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
539 cn.addSuperInterface(nd.toString());
543 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
544 parseClassBody(cn, pn.getChild("classbody"));
546 boolean hasConstructor = false;
547 for(Iterator method_it=cn.getMethods(); method_it.hasNext();) {
548 MethodDescriptor md=(MethodDescriptor)method_it.next();
549 hasConstructor |= md.isConstructor();
551 if((!hasConstructor) && (!cn.isEnum())) {
552 // add a default constructor for this class
553 MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
554 cn.getSymbol(), false);
555 BlockNode bn=new BlockNode();
556 state.addTreeCode(md,bn);
557 md.setDefaultConstructor();
563 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
564 ParseNode decls=pn.getChild("class_body_declaration_list");
566 ParseNodeVector pnv=decls.getChildren();
567 for(int i=0; i<pnv.size(); i++) {
568 ParseNode decl=pnv.elementAt(i);
569 if (isNode(decl,"member")) {
570 parseClassMember(cn,decl);
571 } else if (isNode(decl,"constructor")) {
572 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
573 } else if (isNode(decl, "static_block")) {
574 parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
575 } else if (isNode(decl,"block")) {
576 } else if (isNode(decl,"location_order_declaration")) {
577 parseLocationOrder(cn,decl.getChild("location_order_list"));
578 } else throw new Error();
583 private void parseLocationOrder(ClassDescriptor cd, ParseNode pn) {
584 ParseNodeVector pnv = pn.getChildren();
585 Lattice<String> locOrder =
586 new Lattice<String>("_top_","_bottom_");
587 Set<String> spinLocSet=new HashSet<String>();
588 for (int i = 0; i < pnv.size(); i++) {
589 ParseNode loc = pnv.elementAt(i);
590 if(isNode(loc,"location_property")){
591 String spinLoc=loc.getChildren().elementAt(0).getLabel();
592 spinLocSet.add(spinLoc);
594 String lowerLoc=loc.getChildren().elementAt(0).getLabel();
595 String higherLoc= loc.getChildren().elementAt(1).getLabel();
596 locOrder.put(higherLoc, lowerLoc);
597 if (locOrder.isIntroducingCycle(higherLoc)) {
598 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
599 + " introduces a cycle.");
603 if(spinLocSet.size()>0){
604 //checking if location is actually defined in the hierarchy
605 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
606 String locID = (String) iterator.next();
607 if(!locOrder.containsKey(locID)){
608 throw new Error("Error: The spinning location '"+
609 locID + "' is not defined in the hierarchy of the class '"+cd +"'.");
612 state.addLocationPropertySet(cd, spinLocSet);
614 state.addLocationOrder(cd, locOrder);
617 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
618 ParseNode fieldnode=pn.getChild("field");
619 if (fieldnode!=null) {
620 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
623 ParseNode methodnode=pn.getChild("method");
624 if (methodnode!=null) {
625 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
628 ParseNode innerclassnode=pn.getChild("inner_class_declaration");
629 if (innerclassnode!=null) {
630 parseInnerClassDecl(cn,innerclassnode);
633 ParseNode enumnode=pn.getChild("enum_declaration");
634 if (enumnode!=null) {
635 parseEnumDecl(cn,enumnode);
638 ParseNode flagnode=pn.getChild("flag");
639 if (flagnode!=null) {
640 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
643 // in case there are empty node
644 ParseNode emptynode=pn.getChild("empty");
645 if(emptynode != null) {
651 private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
652 ClassDescriptor icn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
653 icn.setImports(mandatoryImports);
654 icn.setAsInnerClass();
655 icn.setSurroundingClass(cn.getSymbol());
656 icn.setSurrounding(cn);
657 cn.addInnerClass(icn);
658 if (!isEmpty(pn.getChild("super").getTerminal())) {
659 /* parse superclass name */
660 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
661 NameDescriptor nd=parseClassName(snn);
662 icn.setSuper(nd.toString());
664 if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
665 icn.getSymbol().equals(TypeUtil.TagClass)))
666 icn.setSuper(TypeUtil.ObjectClass);
668 // check inherited interfaces
669 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
670 /* parse inherited interface name */
671 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
672 ParseNodeVector pnv=snlist.getChildren();
673 for(int i=0; i<pnv.size(); i++) {
674 ParseNode decl=pnv.elementAt(i);
675 if (isNode(decl,"type")) {
676 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
677 icn.addSuperInterface(nd.toString());
681 icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
682 if(!icn.isStatic()) {
683 throw new Error("Error: inner class " + icn.getSymbol() + " in Class " +
684 cn.getSymbol() + " is not a nested class and is not supported yet!");
686 parseClassBody(icn, pn.getChild("classbody"));
690 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
691 ParseNode tn=pn.getChild("type");
692 String type_st=tn.getTerminal();
693 if(type_st.equals("byte")) {
694 return state.getTypeDescriptor(TypeDescriptor.BYTE);
695 } else if(type_st.equals("short")) {
696 return state.getTypeDescriptor(TypeDescriptor.SHORT);
697 } else if(type_st.equals("boolean")) {
698 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
699 } else if(type_st.equals("int")) {
700 return state.getTypeDescriptor(TypeDescriptor.INT);
701 } else if(type_st.equals("long")) {
702 return state.getTypeDescriptor(TypeDescriptor.LONG);
703 } else if(type_st.equals("char")) {
704 return state.getTypeDescriptor(TypeDescriptor.CHAR);
705 } else if(type_st.equals("float")) {
706 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
707 } else if(type_st.equals("double")) {
708 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
709 } else if(type_st.equals("class")) {
710 ParseNode nn=tn.getChild("class");
711 return state.getTypeDescriptor(parseClassName(nn.getChild("name")));
712 } else if(type_st.equals("array")) {
713 ParseNode nn=tn.getChild("array");
714 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
715 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
716 for(int i=0; i<numdims.intValue(); i++)
717 td=td.makeArray(state);
720 System.out.println(pn.PPrint(2, true));
725 //Needed to separate out top level call since if a base exists,
726 //we do not want to apply our resolveName function (i.e. deal with imports)
727 //otherwise, if base == null, we do just want to resolve name.
728 private NameDescriptor parseClassName(ParseNode nn) {
729 ParseNode base=nn.getChild("base");
730 ParseNode id=nn.getChild("identifier");
731 String classname = id.getTerminal();
733 return new NameDescriptor(resolveName(classname));
735 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
738 private NameDescriptor parseClassNameRecursive(ParseNode nn) {
739 ParseNode base=nn.getChild("base");
740 ParseNode id=nn.getChild("identifier");
741 String classname = id.getTerminal();
743 return new NameDescriptor(classname);
745 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
748 //This will get the mapping of a terminal class name
749 //to a canonical classname (with imports/package locations in them)
750 private String resolveName(String terminal) {
751 if(mandatoryImports.containsKey(terminal)) {
752 return (String) mandatoryImports.get(terminal);
754 if(multiimports.containsKey(terminal)) {
756 Object o = multiimports.get(terminal);
757 if(o instanceof Error) {
758 throw new Error("Class " + terminal + " is ambiguous. Cause: more than 1 package import contain the same class.");
760 //At this point, if we found a unique class
761 //we can treat it as a single, mandatory import.
762 mandatoryImports.put(terminal, o);
771 //only function difference between this and parseName() is that this
772 //does not look for a import mapping.
773 private NameDescriptor parseName(ParseNode nn) {
774 ParseNode base=nn.getChild("base");
775 ParseNode id=nn.getChild("identifier");
777 return new NameDescriptor(id.getTerminal());
779 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
782 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
783 String name=pn.getChild("name").getTerminal();
784 FlagDescriptor flag=new FlagDescriptor(name);
785 if (pn.getChild("external")!=null)
790 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
791 ParseNode mn=pn.getChild("modifier");
792 Modifiers m=parseModifiersList(mn);
793 if(cn.isInterface()) {
794 // TODO add version for normal Java later
795 // Can only be PUBLIC or STATIC or FINAL
796 if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
797 || (m.isSynchronized())) {
798 throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
800 m.addModifier(Modifiers.PUBLIC);
801 m.addModifier(Modifiers.STATIC);
802 m.addModifier(Modifiers.FINAL);
805 ParseNode tn=pn.getChild("type");
806 TypeDescriptor t=parseTypeDescriptor(tn);
807 assignAnnotationsToType(m,t);
808 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
809 ParseNodeVector pnv=vn.getChildren();
810 boolean isglobal=pn.getChild("global")!=null;
812 for(int i=0; i<pnv.size(); i++) {
813 ParseNode vardecl=pnv.elementAt(i);
814 ParseNode tmp=vardecl;
815 TypeDescriptor arrayt=t;
816 while (tmp.getChild("single")==null) {
817 arrayt=arrayt.makeArray(state);
818 tmp=tmp.getChild("array");
820 String identifier=tmp.getChild("single").getTerminal();
821 ParseNode epn=vardecl.getChild("initializer");
823 ExpressionNode en=null;
825 en=parseExpression(epn.getFirstChild());
826 en.setNumLine(epn.getFirstChild().getLine());
828 // for static field, the initializer should be considered as a
830 boolean isfirst = false;
831 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
833 // the first static block for this class
834 Modifiers m_i=new Modifiers();
835 m_i.addModifier(Modifiers.STATIC);
836 md = new MethodDescriptor(m_i, "staticblocks", false);
837 md.setAsStaticBlock();
843 cn.incStaticBlocks();
844 BlockNode bn=new BlockNode();
845 NameNode nn=new NameNode(new NameDescriptor(identifier));
846 nn.setNumLine(en.getNumLine());
847 AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
848 an.setNumLine(pn.getLine());
849 bn.addBlockStatement(new BlockExpressionNode(an));
851 state.addTreeCode(md,bn);
853 BlockNode obn = state.getMethodBody(md);
854 for(int ii = 0; ii < bn.size(); ii++) {
855 BlockStatementNode bsn = bn.get(ii);
856 obn.addBlockStatement(bsn);
858 state.addTreeCode(md, obn);
865 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
869 private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type){
870 Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
871 for(int i=0; i<annotations.size(); i++) {
872 // it only supports a marker annotation
873 AnnotationDescriptor an=annotations.elementAt(i);
874 type.addAnnotationMarker(an);
880 private ExpressionNode parseExpression(ParseNode pn) {
881 if (isNode(pn,"assignment"))
882 return parseAssignmentExpression(pn);
883 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
884 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
885 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
886 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
887 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
888 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
889 isNode(pn,"rightshift")||isNode(pn,"sub")||
890 isNode(pn,"urightshift")||isNode(pn,"sub")||
891 isNode(pn,"add")||isNode(pn,"mult")||
892 isNode(pn,"div")||isNode(pn,"mod")) {
893 ParseNodeVector pnv=pn.getChildren();
894 ParseNode left=pnv.elementAt(0);
895 ParseNode right=pnv.elementAt(1);
896 Operation op=new Operation(pn.getLabel());
897 OpNode on=new OpNode(parseExpression(left),parseExpression(right),op);
898 on.setNumLine(pn.getLine());
900 } else if (isNode(pn,"unaryplus")||
901 isNode(pn,"unaryminus")||
904 ParseNode left=pn.getFirstChild();
905 Operation op=new Operation(pn.getLabel());
906 OpNode on=new OpNode(parseExpression(left),op);
907 on.setNumLine(pn.getLine());
909 } else if (isNode(pn,"postinc")||
910 isNode(pn,"postdec")) {
911 ParseNode left=pn.getFirstChild();
912 AssignOperation op=new AssignOperation(pn.getLabel());
913 AssignmentNode an=new AssignmentNode(parseExpression(left),null,op);
914 an.setNumLine(pn.getLine());
917 } else if (isNode(pn,"preinc")||
918 isNode(pn,"predec")) {
919 ParseNode left=pn.getFirstChild();
920 AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
921 AssignmentNode an=new AssignmentNode(parseExpression(left),
922 new LiteralNode("integer",new Integer(1)),op);
923 an.setNumLine(pn.getLine());
925 } else if (isNode(pn,"literal")) {
926 String literaltype=pn.getTerminal();
927 ParseNode literalnode=pn.getChild(literaltype);
928 Object literal_obj=literalnode.getLiteral();
929 LiteralNode ln=new LiteralNode(literaltype, literal_obj);
930 ln.setNumLine(pn.getLine());
932 } else if (isNode(pn, "createobject")) {
933 TypeDescriptor td = parseTypeDescriptor(pn);
935 Vector args = parseArgumentList(pn);
936 boolean isglobal = pn.getChild("global") != null || pn.getChild("scratch") != null;
937 String disjointId = null;
938 if (pn.getChild("disjoint") != null) {
939 disjointId = pn.getChild("disjoint").getTerminal();
941 CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
942 con.setNumLine(pn.getLine());
943 for (int i = 0; i < args.size(); i++) {
944 con.addArgument((ExpressionNode) args.get(i));
946 /* Could have flag set or tag added here */
947 if (pn.getChild("flag_list") != null || pn.getChild("tag_list") != null) {
948 FlagEffects fe = new FlagEffects(null);
949 if (pn.getChild("flag_list") != null)
950 parseFlagEffect(fe, pn.getChild("flag_list"));
952 if (pn.getChild("tag_list") != null)
953 parseTagEffect(fe, pn.getChild("tag_list"));
954 con.addFlagEffects(fe);
958 } else if (isNode(pn,"createobjectcls")) {
959 //TODO::: FIX BUG!!! static fields in caller context need to become parameters
960 TypeDescriptor td=parseTypeDescriptor(pn);
962 ClassDescriptor cnnew=new ClassDescriptor(td.getSymbol()+"$"+innerCount, false);
963 cnnew.setImports(mandatoryImports);
964 cnnew.setSuper(td.getSymbol());
965 parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
966 Vector args=parseArgumentList(pn);
968 CreateObjectNode con=new CreateObjectNode(td, false, null);
969 con.setNumLine(pn.getLine());
970 for(int i=0; i<args.size(); i++) {
971 con.addArgument((ExpressionNode)args.get(i));
975 } else if (isNode(pn,"createarray")) {
976 //System.out.println(pn.PPrint(3,true));
977 boolean isglobal=pn.getChild("global")!=null||
978 pn.getChild("scratch")!=null;
979 String disjointId=null;
980 if( pn.getChild("disjoint") != null) {
981 disjointId = pn.getChild("disjoint").getTerminal();
983 TypeDescriptor td=parseTypeDescriptor(pn);
984 Vector args=parseDimExprs(pn);
986 if (pn.getChild("dims_opt").getLiteral()!=null)
987 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
988 for(int i=0; i<(args.size()+num); i++)
989 td=td.makeArray(state);
990 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
991 con.setNumLine(pn.getLine());
992 for(int i=0; i<args.size(); i++) {
993 con.addArgument((ExpressionNode)args.get(i));
996 } if (isNode(pn,"createarray2")) {
997 TypeDescriptor td=parseTypeDescriptor(pn);
999 if (pn.getChild("dims_opt").getLiteral()!=null)
1000 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1001 for(int i=0; i<num; i++)
1002 td=td.makeArray(state);
1003 CreateObjectNode con=new CreateObjectNode(td, false, null);
1004 con.setNumLine(pn.getLine());
1005 ParseNode ipn = pn.getChild("initializer");
1006 Vector initializers=parseVariableInitializerList(ipn);
1007 ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
1008 ain.setNumLine(pn.getLine());
1009 con.addArrayInitializer(ain);
1011 } else if (isNode(pn,"name")) {
1012 NameDescriptor nd=parseName(pn);
1013 NameNode nn=new NameNode(nd);
1014 nn.setNumLine(pn.getLine());
1016 } else if (isNode(pn,"this")) {
1017 NameDescriptor nd=new NameDescriptor("this");
1018 NameNode nn=new NameNode(nd);
1019 nn.setNumLine(pn.getLine());
1021 } else if (isNode(pn,"isavailable")) {
1022 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
1023 NameNode nn=new NameNode(nd);
1024 nn.setNumLine(pn.getLine());
1025 return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
1026 } else if (isNode(pn,"methodinvoke1")) {
1027 NameDescriptor nd=parseName(pn.getChild("name"));
1028 Vector args=parseArgumentList(pn);
1029 MethodInvokeNode min=new MethodInvokeNode(nd);
1030 min.setNumLine(pn.getLine());
1031 for(int i=0; i<args.size(); i++) {
1032 min.addArgument((ExpressionNode)args.get(i));
1035 } else if (isNode(pn,"methodinvoke2")) {
1036 String methodid=pn.getChild("id").getTerminal();
1037 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
1038 Vector args=parseArgumentList(pn);
1039 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
1040 min.setNumLine(pn.getLine());
1041 for(int i=0; i<args.size(); i++) {
1042 min.addArgument((ExpressionNode)args.get(i));
1045 } else if (isNode(pn,"fieldaccess")) {
1046 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1047 String fieldname=pn.getChild("field").getTerminal();
1049 FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1050 fan.setNumLine(pn.getLine());
1052 } else if (isNode(pn,"arrayaccess")) {
1053 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1054 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
1055 ArrayAccessNode aan=new ArrayAccessNode(en,index);
1056 aan.setNumLine(pn.getLine());
1058 } else if (isNode(pn,"cast1")) {
1060 CastNode cn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
1061 cn.setNumLine(pn.getLine());
1063 } catch (Exception e) {
1064 System.out.println(pn.PPrint(1,true));
1065 e.printStackTrace();
1068 } else if (isNode(pn,"cast2")) {
1069 CastNode cn=new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
1070 cn.setNumLine(pn.getLine());
1072 } else if (isNode(pn, "getoffset")) {
1073 TypeDescriptor td=parseTypeDescriptor(pn);
1074 String fieldname = pn.getChild("field").getTerminal();
1075 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
1076 return new OffsetNode(td, fieldname);
1077 } else if (isNode(pn, "tert")) {
1079 TertiaryNode tn=new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
1080 parseExpression(pn.getChild("trueexpr").getFirstChild()),
1081 parseExpression(pn.getChild("falseexpr").getFirstChild()) );
1082 tn.setNumLine(pn.getLine());
1085 } else if (isNode(pn, "instanceof")) {
1086 ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
1087 TypeDescriptor t=parseTypeDescriptor(pn);
1088 InstanceOfNode ion=new InstanceOfNode(exp,t);
1089 ion.setNumLine(pn.getLine());
1091 } else if (isNode(pn, "array_initializer")) {
1092 Vector initializers=parseVariableInitializerList(pn);
1093 return new ArrayInitializerNode(initializers);
1094 } else if (isNode(pn, "class_type")) {
1095 TypeDescriptor td=parseTypeDescriptor(pn);
1096 ClassTypeNode ctn=new ClassTypeNode(td);
1097 ctn.setNumLine(pn.getLine());
1099 } else if (isNode(pn, "empty")) {
1102 System.out.println("---------------------");
1103 System.out.println(pn.PPrint(3,true));
1108 private Vector parseDimExprs(ParseNode pn) {
1109 Vector arglist=new Vector();
1110 ParseNode an=pn.getChild("dim_exprs");
1111 if (an==null) /* No argument list */
1113 ParseNodeVector anv=an.getChildren();
1114 for(int i=0; i<anv.size(); i++) {
1115 arglist.add(parseExpression(anv.elementAt(i)));
1120 private Vector parseArgumentList(ParseNode pn) {
1121 Vector arglist=new Vector();
1122 ParseNode an=pn.getChild("argument_list");
1123 if (an==null) /* No argument list */
1125 ParseNodeVector anv=an.getChildren();
1126 for(int i=0; i<anv.size(); i++) {
1127 arglist.add(parseExpression(anv.elementAt(i)));
1132 private Vector[] parseConsArgumentList(ParseNode pn) {
1133 Vector arglist=new Vector();
1134 Vector varlist=new Vector();
1135 ParseNode an=pn.getChild("cons_argument_list");
1136 if (an==null) /* No argument list */
1137 return new Vector[] {varlist, arglist};
1138 ParseNodeVector anv=an.getChildren();
1139 for(int i=0; i<anv.size(); i++) {
1140 ParseNode cpn=anv.elementAt(i);
1141 ParseNode var=cpn.getChild("var");
1142 ParseNode exp=cpn.getChild("exp").getFirstChild();
1143 varlist.add(var.getTerminal());
1144 arglist.add(parseExpression(exp));
1146 return new Vector[] {varlist, arglist};
1149 private Vector parseVariableInitializerList(ParseNode pn) {
1150 Vector varInitList=new Vector();
1151 ParseNode vin=pn.getChild("var_init_list");
1152 if (vin==null) /* No argument list */
1154 ParseNodeVector vinv=vin.getChildren();
1155 for(int i=0; i<vinv.size(); i++) {
1156 varInitList.add(parseExpression(vinv.elementAt(i)));
1161 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
1162 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1163 ParseNodeVector pnv=pn.getChild("args").getChildren();
1165 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
1166 an.setNumLine(pn.getLine());
1171 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1172 ParseNode headern=pn.getChild("method_header");
1173 ParseNode bodyn=pn.getChild("body");
1174 MethodDescriptor md=parseMethodHeader(headern);
1176 BlockNode bn=parseBlock(bodyn);
1177 bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1179 state.addTreeCode(md,bn);
1181 // this is a hack for investigating new language features
1182 // at the AST level, someday should evolve into a nice compiler
1184 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
1185 // md.getSymbol().equals( ***put your method in here like: "main" )
1187 // bn.setStyle( BlockNode.NORMAL );
1188 // System.out.println( bn.printNode( 0 ) );
1191 } catch (Exception e) {
1192 System.out.println("Error with method:"+md.getSymbol());
1193 e.printStackTrace();
1196 System.out.println("Error with method:"+md.getSymbol());
1197 e.printStackTrace();
1202 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1203 ParseNode mn=pn.getChild("modifiers");
1204 Modifiers m=parseModifiersList(mn);
1205 ParseNode cdecl=pn.getChild("constructor_declarator");
1206 boolean isglobal=cdecl.getChild("global")!=null;
1207 String name=resolveName(cn.getSymbol());
1208 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1209 ParseNode paramnode=cdecl.getChild("parameters");
1210 parseParameterList(md,paramnode);
1211 ParseNode bodyn0=pn.getChild("body");
1212 ParseNode bodyn=bodyn0.getChild("constructor_body");
1215 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1216 bn=parseBlock(bodyn);
1219 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1220 ParseNode sin=bodyn.getChild("superinvoke");
1221 NameDescriptor nd=new NameDescriptor("super");
1222 Vector args=parseArgumentList(sin);
1223 MethodInvokeNode min=new MethodInvokeNode(nd);
1224 min.setNumLine(sin.getLine());
1225 for(int i=0; i<args.size(); i++) {
1226 min.addArgument((ExpressionNode)args.get(i));
1228 BlockExpressionNode ben=new BlockExpressionNode(min);
1229 bn.addFirstBlockStatement(ben);
1231 } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1232 ParseNode eci=bodyn.getChild("explconstrinv");
1233 NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1234 Vector args=parseArgumentList(eci);
1235 MethodInvokeNode min=new MethodInvokeNode(nd);
1236 min.setNumLine(eci.getLine());
1237 for(int i=0; i<args.size(); i++) {
1238 min.addArgument((ExpressionNode)args.get(i));
1240 BlockExpressionNode ben=new BlockExpressionNode(min);
1241 ben.setNumLine(eci.getLine());
1242 bn.addFirstBlockStatement(ben);
1244 state.addTreeCode(md,bn);
1247 private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1248 // Each class maintains one MethodDecscriptor which combines all its
1249 // static blocks in their declaration order
1250 boolean isfirst = false;
1251 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1253 // the first static block for this class
1254 Modifiers m_i=new Modifiers();
1255 m_i.addModifier(Modifiers.STATIC);
1256 md = new MethodDescriptor(m_i, "staticblocks", false);
1257 md.setAsStaticBlock();
1260 ParseNode bodyn=pn.getChild("body");
1264 cn.incStaticBlocks();
1266 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1267 bn=parseBlock(bodyn);
1271 state.addTreeCode(md,bn);
1273 BlockNode obn = state.getMethodBody(md);
1274 for(int i = 0; i < bn.size(); i++) {
1275 BlockStatementNode bsn = bn.get(i);
1276 obn.addBlockStatement(bsn);
1278 state.addTreeCode(md, obn);
1283 public BlockNode parseBlock(ParseNode pn) {
1284 this.m_taskexitnum = 0;
1285 if (pn==null||isEmpty(pn.getTerminal()))
1286 return new BlockNode();
1287 ParseNode bsn=pn.getChild("block_statement_list");
1288 return parseBlockHelper(bsn);
1291 private BlockNode parseBlockHelper(ParseNode pn) {
1292 ParseNodeVector pnv=pn.getChildren();
1293 BlockNode bn=new BlockNode();
1294 for(int i=0; i<pnv.size(); i++) {
1295 Vector bsv=parseBlockStatement(pnv.elementAt(i));
1296 for(int j=0; j<bsv.size(); j++) {
1297 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1303 public BlockNode parseSingleBlock(ParseNode pn) {
1304 BlockNode bn=new BlockNode();
1305 Vector bsv=parseBlockStatement(pn);
1306 for(int j=0; j<bsv.size(); j++) {
1307 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1309 bn.setStyle(BlockNode.NOBRACES);
1313 public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
1314 ParseNodeVector pnv=pn.getChildren();
1315 Vector bv=new Vector();
1316 for(int i=0; i<pnv.size(); i++) {
1317 bv.addAll(parseBlockStatement(pnv.elementAt(i)));
1322 public Vector parseBlockStatement(ParseNode pn) {
1323 Vector blockstatements=new Vector();
1324 if (isNode(pn,"tag_declaration")) {
1325 String name=pn.getChild("single").getTerminal();
1326 String type=pn.getChild("type").getTerminal();
1328 TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1329 tdn.setNumLine(pn.getLine());
1331 blockstatements.add(tdn);
1332 } else if (isNode(pn,"local_variable_declaration")) {
1334 ParseNode mn=pn.getChild("modifiers");
1335 TypeDescriptor t=parseTypeDescriptor(pn);
1337 Modifiers m=parseModifiersList(mn);
1338 assignAnnotationsToType(m, t);
1340 ParseNode vn=pn.getChild("variable_declarators_list");
1341 ParseNodeVector pnv=vn.getChildren();
1342 for(int i=0; i<pnv.size(); i++) {
1343 ParseNode vardecl=pnv.elementAt(i);
1346 ParseNode tmp=vardecl;
1347 TypeDescriptor arrayt=t;
1349 while (tmp.getChild("single")==null) {
1350 arrayt=arrayt.makeArray(state);
1351 tmp=tmp.getChild("array");
1353 String identifier=tmp.getChild("single").getTerminal();
1355 ParseNode epn=vardecl.getChild("initializer");
1358 ExpressionNode en=null;
1360 en=parseExpression(epn.getFirstChild());
1362 DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1363 dn.setNumLine(tmp.getLine());
1365 blockstatements.add(dn);
1367 } else if (isNode(pn,"nop")) {
1369 } else if (isNode(pn,"expression")) {
1370 BlockExpressionNode ben=new BlockExpressionNode(parseExpression(pn.getFirstChild()));
1371 ben.setNumLine(pn.getLine());
1372 blockstatements.add(ben);
1373 } else if (isNode(pn,"ifstatement")) {
1374 IfStatementNode isn=new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1375 parseSingleBlock(pn.getChild("statement").getFirstChild()),
1376 pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null);
1377 isn.setNumLine(pn.getLine());
1379 blockstatements.add(isn);
1380 } else if (isNode(pn,"switch_statement")) {
1381 // TODO add version for normal Java later
1382 SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1383 parseSingleBlock(pn.getChild("statement").getFirstChild()));
1384 ssn.setNumLine(pn.getLine());
1385 blockstatements.add(ssn);
1386 } else if (isNode(pn,"switch_block_list")) {
1387 // TODO add version for normal Java later
1388 ParseNodeVector pnv=pn.getChildren();
1389 for(int i=0; i<pnv.size(); i++) {
1390 ParseNode sblockdecl=pnv.elementAt(i);
1392 if(isNode(sblockdecl, "switch_block")) {
1393 ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1394 ParseNodeVector labelv=lpn.getChildren();
1395 Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1396 for(int j=0; j<labelv.size(); j++) {
1397 ParseNode labeldecl=labelv.elementAt(j);
1398 if(isNode(labeldecl, "switch_label")) {
1399 SwitchLabelNode sln=new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false);
1400 sln.setNumLine(labeldecl.getLine());
1401 slv.addElement(sln);
1402 } else if(isNode(labeldecl, "default_switch_label")) {
1403 SwitchLabelNode sln=new SwitchLabelNode(null, true);
1404 sln.setNumLine(labeldecl.getLine());
1405 slv.addElement(sln);
1409 SwitchBlockNode sbn=new SwitchBlockNode(slv,
1410 parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild()));
1411 sbn.setNumLine(sblockdecl.getLine());
1413 blockstatements.add(sbn);
1417 } else if (isNode(pn, "trycatchstatement")) {
1418 // TODO add version for normal Java later
1419 // Do not fully support exceptions now. Only make sure that if there are no
1420 // exceptions thrown, the execution is right
1421 ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1422 BlockNode bn=parseBlockHelper(tpn);
1423 blockstatements.add(new SubBlockNode(bn));
1425 ParseNode fbk = pn.getChild("finallyblock");
1427 ParseNode fpn = fbk.getFirstChild();
1428 BlockNode fbn=parseBlockHelper(fpn);
1429 blockstatements.add(new SubBlockNode(fbn));
1431 } else if (isNode(pn, "throwstatement")) {
1432 // TODO Simply return here
1433 //blockstatements.add(new ReturnNode());
1434 } else if (isNode(pn,"taskexit")) {
1436 if (pn.getChild("flag_effects_list")!=null)
1437 vfe=parseFlags(pn.getChild("flag_effects_list"));
1439 if (pn.getChild("cons_checks")!=null)
1440 ccs=parseChecks(pn.getChild("cons_checks"));
1441 TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1442 ten.setNumLine(pn.getLine());
1443 blockstatements.add(ten);
1444 } else if (isNode(pn,"atomic")) {
1445 BlockNode bn=parseBlockHelper(pn);
1446 AtomicNode an=new AtomicNode(bn);
1447 an.setNumLine(pn.getLine());
1448 blockstatements.add(an);
1449 } else if (isNode(pn,"synchronized")) {
1450 BlockNode bn=parseBlockHelper(pn.getChild("block"));
1451 ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
1452 SynchronizedNode sn=new SynchronizedNode(en, bn);
1453 sn.setNumLine(pn.getLine());
1454 blockstatements.add(sn);
1455 } else if (isNode(pn,"return")) {
1456 if (isEmpty(pn.getTerminal()))
1457 blockstatements.add(new ReturnNode());
1459 ExpressionNode en=parseExpression(pn.getFirstChild());
1460 ReturnNode rn=new ReturnNode(en);
1461 rn.setNumLine(pn.getLine());
1462 blockstatements.add(rn);
1464 } else if (isNode(pn,"block_statement_list")) {
1465 BlockNode bn=parseBlockHelper(pn);
1466 blockstatements.add(new SubBlockNode(bn));
1467 } else if (isNode(pn,"empty")) {
1469 } else if (isNode(pn,"statement_expression_list")) {
1470 ParseNodeVector pnv=pn.getChildren();
1471 BlockNode bn=new BlockNode();
1472 for(int i=0; i<pnv.size(); i++) {
1473 ExpressionNode en=parseExpression(pnv.elementAt(i));
1474 blockstatements.add(new BlockExpressionNode(en));
1476 bn.setStyle(BlockNode.EXPRLIST);
1477 } else if (isNode(pn,"forstatement")) {
1478 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
1479 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
1480 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1481 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1482 if(condition == null) {
1483 // no condition clause, make a 'true' expression as the condition
1484 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1486 LoopNode ln=new LoopNode(init,condition,update,body);
1487 ln.setNumLine(pn.getLine());
1488 blockstatements.add(ln);
1489 } else if (isNode(pn,"whilestatement")) {
1490 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1491 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1492 if(condition == null) {
1493 // no condition clause, make a 'true' expression as the condition
1494 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1496 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
1497 } else if (isNode(pn,"dowhilestatement")) {
1498 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1499 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1500 if(condition == null) {
1501 // no condition clause, make a 'true' expression as the condition
1502 condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
1504 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
1505 } else if (isNode(pn,"sese")) {
1506 ParseNode pnID=pn.getChild("identifier");
1508 if( pnID != null ) { stID=pnID.getFirstChild().getTerminal(); }
1509 SESENode start=new SESENode(stID);
1510 start.setNumLine(pn.getLine());
1511 SESENode end =new SESENode(stID);
1512 start.setEnd( end );
1513 end.setStart( start );
1514 blockstatements.add(start);
1515 blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1516 blockstatements.add(end);
1517 } else if (isNode(pn,"continue")) {
1518 ContinueBreakNode cbn=new ContinueBreakNode(false);
1519 cbn.setNumLine(pn.getLine());
1520 blockstatements.add(cbn);
1521 } else if (isNode(pn,"break")) {
1522 ContinueBreakNode cbn=new ContinueBreakNode(true);
1523 cbn.setNumLine(pn.getLine());
1524 blockstatements.add(cbn);
1525 ParseNode idopt_pn=pn.getChild("identifier_opt");
1526 ParseNode name_pn=idopt_pn.getChild("name");
1527 // name_pn.getTerminal() gives you the label
1528 } else if (isNode(pn,"genreach")) {
1529 String graphName = pn.getChild("graphName").getTerminal();
1530 blockstatements.add( new GenReachNode( graphName ) );
1532 } else if(isNode(pn,"labeledstatement")){
1533 String label = pn.getChild("name").getTerminal();
1534 BlockNode bn=parseSingleBlock(pn.getChild("statement").getFirstChild());
1536 blockstatements.add(new SubBlockNode(bn));
1538 System.out.println("---------------");
1539 System.out.println(pn.PPrint(3,true));
1542 return blockstatements;
1545 public MethodDescriptor parseMethodHeader(ParseNode pn) {
1546 ParseNode mn=pn.getChild("modifiers");
1547 Modifiers m=parseModifiersList(mn);
1549 ParseNode tn=pn.getChild("returntype");
1550 TypeDescriptor returntype;
1552 returntype=parseTypeDescriptor(tn);
1554 returntype=new TypeDescriptor(TypeDescriptor.VOID);
1556 ParseNode pmd=pn.getChild("method_declarator");
1557 String name=pmd.getChild("name").getTerminal();
1558 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1560 ParseNode paramnode=pmd.getChild("parameters");
1561 parseParameterList(md,paramnode);
1565 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1566 ParseNode paramlist=pn.getChild("formal_parameter_list");
1567 if (paramlist==null)
1569 ParseNodeVector pnv=paramlist.getChildren();
1570 for(int i=0; i<pnv.size(); i++) {
1571 ParseNode paramn=pnv.elementAt(i);
1573 if (isNode(paramn, "tag_parameter")) {
1574 String paramname=paramn.getChild("single").getTerminal();
1575 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1576 md.addTagParameter(type, paramname);
1579 TypeDescriptor type=parseTypeDescriptor(paramn);
1581 ParseNode tmp=paramn;
1582 while (tmp.getChild("single")==null) {
1583 type=type.makeArray(state);
1584 tmp=tmp.getChild("array");
1586 String paramname=tmp.getChild("single").getTerminal();
1588 md.addParameter(type, paramname);
1589 if(isNode(paramn, "annotation_parameter")){
1590 ParseNode bodynode=paramn.getChild("annotation_body");
1591 parseParameterAnnotation(bodynode,type);
1598 public Modifiers parseModifiersList(ParseNode pn) {
1599 Modifiers m=new Modifiers();
1600 ParseNode modlist=pn.getChild("modifier_list");
1601 if (modlist!=null) {
1602 ParseNodeVector pnv=modlist.getChildren();
1603 for(int i=0; i<pnv.size(); i++) {
1604 ParseNode modn=pnv.elementAt(i);
1605 if (isNode(modn,"public"))
1606 m.addModifier(Modifiers.PUBLIC);
1607 else if (isNode(modn,"protected"))
1608 m.addModifier(Modifiers.PROTECTED);
1609 else if (isNode(modn,"private"))
1610 m.addModifier(Modifiers.PRIVATE);
1611 else if (isNode(modn,"static"))
1612 m.addModifier(Modifiers.STATIC);
1613 else if (isNode(modn,"final"))
1614 m.addModifier(Modifiers.FINAL);
1615 else if (isNode(modn,"native"))
1616 m.addModifier(Modifiers.NATIVE);
1617 else if (isNode(modn,"synchronized"))
1618 m.addModifier(Modifiers.SYNCHRONIZED);
1619 else if (isNode(modn,"atomic"))
1620 m.addModifier(Modifiers.ATOMIC);
1621 else if (isNode(modn,"abstract"))
1622 m.addModifier(Modifiers.ABSTRACT);
1623 else if (isNode(modn,"volatile"))
1624 m.addModifier(Modifiers.VOLATILE);
1625 else if (isNode(modn,"transient"))
1626 m.addModifier(Modifiers.TRANSIENT);
1627 else if(isNode(modn,"annotation_list"))
1628 parseAnnotationList(modn,m);
1630 throw new Error("Unrecognized Modifier:"+modn.getLabel());}
1636 private void parseAnnotationList(ParseNode pn, Modifiers m) {
1637 ParseNodeVector pnv = pn.getChildren();
1638 for (int i = 0; i < pnv.size(); i++) {
1639 ParseNode body_list = pnv.elementAt(i);
1640 if (isNode(body_list, "annotation_body")) {
1641 ParseNode body_node = body_list.getFirstChild();
1642 if (isNode(body_node, "marker_annotation")) {
1643 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1644 } else if (isNode(body_node, "single_annotation")) {
1645 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1646 body_node.getChild("element_value").getTerminal()));
1647 } else if (isNode(body_node, "normal_annotation")) {
1648 throw new Error("Annotation with multiple data members is not supported yet.");
1654 private void parseParameterAnnotation(ParseNode body_list,TypeDescriptor type){
1655 ParseNode body_node = body_list.getFirstChild();
1656 if (isNode(body_node, "marker_annotation")) {
1657 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1658 } else if (isNode(body_node, "single_annotation")) {
1659 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1660 body_node.getChild("element_value").getTerminal()));
1661 } else if (isNode(body_node, "normal_annotation")) {
1662 throw new Error("Annotation with multiple data members is not supported yet.");
1666 private boolean isNode(ParseNode pn, String label) {
1667 if (pn.getLabel().equals(label))
1672 private static boolean isEmpty(ParseNode pn) {
1673 if (pn.getLabel().equals("empty"))
1679 private static boolean isEmpty(String s) {
1680 if (s.equals("empty"))
1686 /** Throw an exception if something is unexpected */
1687 private void check(ParseNode pn, String label) {
1689 throw new Error(pn+ "IE: Expected '" + label + "', got null");
1691 if (!pn.getLabel().equals(label)) {
1692 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");