12 private int m_taskexitnum;
14 public BuildIR(State state) {
16 this.m_taskexitnum = 0;
19 public void buildtree(ParseNode pn, Set toanalyze, String sourcefile) {
20 parseFile(pn, toanalyze, sourcefile);
22 // numering the interfaces
24 Iterator it_classes = state.getClassSymbolTable().getValueSet().iterator();
25 while(it_classes.hasNext()) {
26 ClassDescriptor cd = (ClassDescriptor)it_classes.next();
27 if(cd.isInterface()) {
28 cd.setInterfaceId(if_num++);
33 //This is all single imports and a subset of the
34 //multi imports that have been resolved.
35 ChainHashMap mandatoryImports;
36 //maps class names in file to full name
37 //Note may map a name to an ERROR.
38 ChainHashMap multiimports;
41 String currsourcefile;
44 void pushChainMaps() {
45 mandatoryImports=mandatoryImports.makeChild();
46 multiimports=multiimports.makeChild();
50 mandatoryImports=mandatoryImports.getParent();
51 multiimports=multiimports.getParent();
54 /** Parse the classes in this file */
55 public void parseFile(ParseNode pn, Set toanalyze, String sourcefile) {
56 mandatoryImports = new ChainHashMap();
57 multiimports = new ChainHashMap();
58 currsourcefile=sourcefile;
62 //add java.lang as our default multi-import
63 this.addMultiImport(sourcefile, "java.lang", false);
66 ParseNode ipn = pn.getChild("imports").getChild("import_decls_list");
67 if ((ipn != null) && !state.MGC) {
68 ParseNodeVector pnv = ipn.getChildren();
69 for (int i = 0; i < pnv.size(); i++) {
70 ParseNode pnimport = pnv.elementAt(i);
71 NameDescriptor nd = parseName(pnimport.getChild("name"));
72 if (isNode(pnimport, "import_single")) {
73 if (!mandatoryImports.containsKey(nd.getIdentifier())) {
74 // map name to full name (includes package/directory
75 mandatoryImports.put(nd.getIdentifier(), nd.getPathFromRootToHere());
77 throw new Error("An ambiguous class "+ nd.getIdentifier() +" has been found. It is included for " +
78 ((String)mandatoryImports.get(nd.getIdentifier())) + " and " +
79 nd.getPathFromRootToHere());
82 addMultiImport(sourcefile, nd.getPathFromRootToHere(), false);
87 ParseNode ppn=pn.getChild("packages").getChild("package");
89 if ((ppn!=null) && !state.MGC){
90 NameDescriptor nd = parseClassName(ppn.getChild("name"));
91 packageName = nd.getPathFromRootToHere();
92 //Trick -> import the package directory as a multi-import and it'll
93 //automatically recognize files in the same directory.
94 addMultiImport(sourcefile, packageName, true);
97 ParseNode tpn=pn.getChild("type_declaration_list");
99 ParseNodeVector pnv = tpn.getChildren();
100 for (int i = 0; i < pnv.size(); i++) {
101 ParseNode type_pn = pnv.elementAt(i);
102 if (isEmpty(type_pn)) /* Skip the semicolon */
104 if (isNode(type_pn, "class_declaration")) {
105 ClassDescriptor cn = parseTypeDecl(type_pn);
106 parseInitializers(cn);
108 // for inner classes/enum
109 HashSet tovisit = new HashSet();
110 Iterator it_icds = cn.getInnerClasses();
111 while (it_icds.hasNext()) {
112 tovisit.add(it_icds.next());
115 while (!tovisit.isEmpty()) {
116 ClassDescriptor cd = (ClassDescriptor) tovisit.iterator().next();
118 parseInitializers(cd);
120 Iterator it_ics = cd.getInnerClasses();
121 while (it_ics.hasNext()) {
122 tovisit.add(it_ics.next());
125 } else if (isNode(type_pn, "task_declaration")) {
126 TaskDescriptor td = parseTaskDecl(type_pn);
127 if (toanalyze != null)
130 } else if (isNode(type_pn, "interface_declaration")) {
131 // TODO add version for normal Java later
132 ClassDescriptor cn = parseInterfaceDecl(type_pn, null);
133 } else if (isNode(type_pn, "enum_declaration")) {
134 // TODO add version for normal Java later
135 ClassDescriptor cn = parseEnumDecl(null, type_pn);
137 } else if(isNode(type_pn,"annotation_type_declaration")) {
138 ClassDescriptor cn=parseAnnotationTypeDecl(type_pn);
140 throw new Error(type_pn.getLabel());
147 //This kind of breaks away from tradition a little bit by doing the file checks here
148 // instead of in Semantic check, but doing it here is easier because we have a mapping early on
149 // if I wait until semantic check, I have to change ALL the type descriptors to match the new
150 // mapping and that's both ugly and tedious.
151 private void addMultiImport(String currentSource, String importPath, boolean isPackageDirectory) {
152 boolean found = false;
153 for (int j = 0; j < state.classpath.size(); j++) {
154 String path = (String) state.classpath.get(j);
155 File folder = new File(path, importPath.replace('.', '/'));
156 if (folder.exists()) {
158 for (String file : folder.list()) {
159 // if the file is of type *.java add to multiImport list.
160 if (file.lastIndexOf('.') != -1 && file.substring(file.lastIndexOf('.')).equalsIgnoreCase(".java")) {
161 String classname = file.substring(0, file.length() - 5);
162 // single imports have precedence over multi-imports
163 if (!mandatoryImports.containsKey(classname)) {
164 //package files have precedence over multi-imports.
165 if (multiimports.containsKey(classname) && !isPackageDirectory) {
166 // put error in for later, in case we try to import
167 multiimports.put(classname, new Error("Error: class " + classname + " is defined more than once in a multi-import in " + currentSource));
169 multiimports.put(classname, importPath + "." + classname);
178 throw new Error("Import package " + importPath + " in " + currentSource
179 + " cannot be resolved.");
183 public void parseInitializers(ClassDescriptor cn) {
184 Vector fv=cn.getFieldVec();
186 for(int i=0; i<fv.size(); i++) {
187 FieldDescriptor fd=(FieldDescriptor)fv.get(i);
188 if(fd.getExpressionNode()!=null) {
189 Iterator methodit = cn.getMethods();
190 while(methodit.hasNext()) {
191 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
192 if(currmd.isConstructor()) {
193 BlockNode bn=state.getMethodBody(currmd);
194 NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
195 AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
196 bn.addBlockStatementAt(new BlockExpressionNode(an), pos);
204 private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {
205 String basename=pn.getChild("name").getTerminal();
206 String classname=(cn!=null)?cn.getClassName()+"$"+basename:basename;
207 ClassDescriptor ecd=new ClassDescriptor(cn!=null?cn.getPackage():null, classname, false);
210 if (packageName==null)
211 cn.getSingleImportMappings().put(basename,classname);
213 cn.getSingleImportMappings().put(basename,packageName+"."+classname);
217 ecd.setImports(mandatoryImports, multiimports);
220 ecd.setSurroundingClass(cn.getSymbol());
221 ecd.setSurrounding(cn);
224 if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
225 ecd.getSymbol().equals(TypeUtil.TagClass))) {
226 ecd.setSuper(TypeUtil.ObjectClass);
228 ecd.setModifiers(parseModifiersList(pn.getChild("modifiers")));
229 parseEnumBody(ecd, pn.getChild("enumbody"));
231 if (analyzeset != null)
233 ecd.setSourceFileName(currsourcefile);
240 private void parseEnumBody(ClassDescriptor cn, ParseNode pn) {
241 ParseNode decls=pn.getChild("enum_constants_list");
243 ParseNodeVector pnv=decls.getChildren();
244 for(int i=0; i<pnv.size(); i++) {
245 ParseNode decl=pnv.elementAt(i);
246 if (isNode(decl,"enum_constant")) {
247 parseEnumConstant(cn,decl);
248 } else throw new Error();
253 private void parseEnumConstant(ClassDescriptor cn, ParseNode pn) {
254 cn.addEnumConstant(pn.getChild("name").getTerminal());
257 private ClassDescriptor parseAnnotationTypeDecl(ParseNode pn) {
258 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
260 cn.setImports(mandatoryImports, multiimports);
261 ParseNode modifiers=pn.getChild("modifiers");
262 if(modifiers!=null) {
263 cn.setModifiers(parseModifiersList(modifiers));
265 parseAnnotationTypeBody(cn,pn.getChild("body"));
268 if (analyzeset != null)
270 cn.setSourceFileName(currsourcefile);
276 private void parseAnnotationTypeBody(ClassDescriptor cn, ParseNode pn) {
277 ParseNode list_node=pn.getChild("annotation_type_element_list");
278 if(list_node!=null) {
279 ParseNodeVector pnv = list_node.getChildren();
280 for (int i = 0; i < pnv.size(); i++) {
281 ParseNode element_node = pnv.elementAt(i);
282 if (isNode(element_node, "annotation_type_element_declaration")) {
283 ParseNodeVector elementProps = element_node.getChildren();
284 String identifier=null;
285 TypeDescriptor type=null;
286 Modifiers modifiers=new Modifiers();
287 for(int eidx=0; eidx<elementProps.size(); eidx++) {
288 ParseNode prop_node=elementProps.elementAt(eidx);
289 if(isNode(prop_node,"name")) {
290 identifier=prop_node.getTerminal();
291 } else if(isNode(prop_node,"type")) {
292 type=parseTypeDescriptor(prop_node);
293 } else if(isNode(prop_node,"modifier")) {
294 modifiers=parseModifiersList(prop_node);
297 cn.addField(new FieldDescriptor(modifiers, type, identifier, null, false));
303 public ClassDescriptor parseInterfaceDecl(ParseNode pn, ClassDescriptor outerclass) {
304 String basename=pn.getChild("name").getTerminal();
305 String classname=((outerclass==null)?"":(outerclass.getClassName()+"$"))+basename;
306 if (outerclass!=null) {
307 if (packageName==null)
308 outerclass.getSingleImportMappings().put(basename,classname);
310 outerclass.getSingleImportMappings().put(basename,packageName+"."+classname);
312 ClassDescriptor cn= new ClassDescriptor(packageName, classname, true);
315 cn.setImports(mandatoryImports, multiimports);
316 //cn.setAsInterface();
317 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
318 /* parse inherited interface name */
319 ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
320 ParseNodeVector pnv=snlist.getChildren();
321 for(int i=0; i<pnv.size(); i++) {
322 ParseNode decl=pnv.elementAt(i);
323 if (isNode(decl,"type")) {
324 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
325 cn.addSuperInterface(nd.toString());
326 } else if (isNode(decl, "interface_declaration")) {
327 ClassDescriptor innercn = parseInterfaceDecl(decl, cn);
328 } else throw new Error();
331 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
332 parseInterfaceBody(cn, pn.getChild("interfacebody"));
333 if (analyzeset != null)
335 cn.setSourceFileName(currsourcefile);
341 private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
342 assert(cn.isInterface());
343 ParseNode decls=pn.getChild("interface_member_declaration_list");
345 ParseNodeVector pnv=decls.getChildren();
346 for(int i=0; i<pnv.size(); i++) {
347 ParseNode decl=pnv.elementAt(i);
348 if (isNode(decl,"constant")) {
349 parseInterfaceConstant(cn,decl);
350 } else if (isNode(decl,"method")) {
351 parseInterfaceMethod(cn,decl.getChild("method_declaration"));
352 } else if (isNode(decl, "interface_declaration")) {
353 parseInterfaceDecl(decl, cn);
354 } else throw new Error(decl.PPrint(2, true));
361 private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
363 parseFieldDecl(cn,pn.getChild("field_declaration"));
369 private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
370 ParseNode headern=pn.getChild("header");
371 ParseNode bodyn=pn.getChild("body");
372 MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
373 md.getModifiers().addModifier(Modifiers.PUBLIC);
374 md.getModifiers().addModifier(Modifiers.ABSTRACT);
376 BlockNode bn=parseBlock(bodyn);
378 state.addTreeCode(md,bn);
379 } catch (Exception e) {
380 System.out.println("Error with method:"+md.getSymbol());
384 System.out.println("Error with method:"+md.getSymbol());
390 public TaskDescriptor parseTaskDecl(ParseNode pn) {
391 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
392 ParseNode bodyn=pn.getChild("body");
393 BlockNode bn=parseBlock(bodyn);
394 parseParameterList(td, pn);
395 state.addTreeCode(td,bn);
396 if (pn.getChild("flag_effects_list")!=null)
397 td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
401 public Vector parseFlags(ParseNode pn) {
402 Vector vfe=new Vector();
403 ParseNodeVector pnv=pn.getChildren();
404 for(int i=0; i<pnv.size(); i++) {
405 ParseNode fn=pnv.elementAt(i);
406 FlagEffects fe=parseFlagEffects(fn);
412 public FlagEffects parseFlagEffects(ParseNode pn) {
413 if (isNode(pn,"flag_effect")) {
414 String flagname=pn.getChild("name").getTerminal();
415 FlagEffects fe=new FlagEffects(flagname);
416 if (pn.getChild("flag_list")!=null)
417 parseFlagEffect(fe, pn.getChild("flag_list"));
418 if (pn.getChild("tag_list")!=null)
419 parseTagEffect(fe, pn.getChild("tag_list"));
421 } else throw new Error();
424 public void parseTagEffect(FlagEffects fes, ParseNode pn) {
425 ParseNodeVector pnv=pn.getChildren();
426 for(int i=0; i<pnv.size(); i++) {
427 ParseNode pn2=pnv.elementAt(i);
429 if (isNode(pn2,"not")) {
431 pn2=pn2.getChild("name");
433 String name=pn2.getTerminal();
434 fes.addTagEffect(new TagEffect(name,status));
438 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
439 ParseNodeVector pnv=pn.getChildren();
440 for(int i=0; i<pnv.size(); i++) {
441 ParseNode pn2=pnv.elementAt(i);
443 if (isNode(pn2,"not")) {
445 pn2=pn2.getChild("name");
447 String name=pn2.getTerminal();
448 fes.addEffect(new FlagEffect(name,status));
452 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
453 if (isNode(pn,"or")) {
454 ParseNodeVector pnv=pn.getChildren();
455 ParseNode left=pnv.elementAt(0);
456 ParseNode right=pnv.elementAt(1);
457 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
458 } else if (isNode(pn,"and")) {
459 ParseNodeVector pnv=pn.getChildren();
460 ParseNode left=pnv.elementAt(0);
461 ParseNode right=pnv.elementAt(1);
462 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
463 } else if (isNode(pn, "not")) {
464 ParseNodeVector pnv=pn.getChildren();
465 ParseNode left=pnv.elementAt(0);
466 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
468 } else if (isNode(pn,"name")) {
469 return new FlagNode(pn.getTerminal());
475 public Vector parseChecks(ParseNode pn) {
476 Vector ccs=new Vector();
477 ParseNodeVector pnv=pn.getChildren();
478 for(int i=0; i<pnv.size(); i++) {
479 ParseNode fn=pnv.elementAt(i);
480 ConstraintCheck cc=parseConstraintCheck(fn);
486 public ConstraintCheck parseConstraintCheck(ParseNode pn) {
487 if (isNode(pn,"cons_check")) {
488 String specname=pn.getChild("name").getChild("identifier").getTerminal();
489 Vector[] args=parseConsArgumentList(pn);
490 ConstraintCheck cc=new ConstraintCheck(specname);
491 for(int i=0; i<args[0].size(); i++) {
492 cc.addVariable((String)args[0].get(i));
493 cc.addArgument((ExpressionNode)args[1].get(i));
496 } else throw new Error();
499 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
502 ParseNode paramlist=pn.getChild("task_parameter_list");
505 ParseNodeVector pnv=paramlist.getChildren();
506 for(int i=0; i<pnv.size(); i++) {
507 ParseNode paramn=pnv.elementAt(i);
508 if(paramn.getChild("optional")!=null) {
510 paramn = paramn.getChild("optional").getFirstChild();
511 System.out.println("OPTIONAL FOUND!!!!!!!");
512 } else { optional = false;
513 System.out.println("NOT OPTIONAL"); }
515 TypeDescriptor type=parseTypeDescriptor(paramn);
517 String paramname=paramn.getChild("single").getTerminal();
518 FlagExpressionNode fen=null;
519 if (paramn.getChild("flag")!=null)
520 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
522 ParseNode tagnode=paramn.getChild("tag");
524 TagExpressionList tel=null;
526 tel=parseTagExpressionList(tagnode);
529 td.addParameter(type,paramname,fen, tel, optional);
533 public TagExpressionList parseTagExpressionList(ParseNode pn) {
534 //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
535 //To test, feed in any input program that uses tags
536 ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
537 TagExpressionList tel=new TagExpressionList();
538 for(int i=0; i<pnv.size(); i++) {
539 ParseNode tn=pnv.elementAt(i);
540 String type=tn.getChild("type").getTerminal();
541 String name=tn.getChild("single").getTerminal();
542 tel.addTag(type, name);
547 public ClassDescriptor parseTypeDecl(ParseNode pn) {
548 ClassDescriptor cn=new ClassDescriptor(packageName, pn.getChild("name").getTerminal(), false);
550 cn.setImports(mandatoryImports, multiimports);
551 if (!isEmpty(pn.getChild("super").getTerminal())) {
552 /* parse superclass name */
553 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
554 NameDescriptor nd=parseClassName(snn);
555 cn.setSuper(nd.toString());
557 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
558 cn.getSymbol().equals(TypeUtil.TagClass)))
559 cn.setSuper(TypeUtil.ObjectClass);
561 // check inherited interfaces
562 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
563 /* parse inherited interface name */
564 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
565 ParseNodeVector pnv=snlist.getChildren();
566 for(int i=0; i<pnv.size(); i++) {
567 ParseNode decl=pnv.elementAt(i);
568 if (isNode(decl,"type")) {
569 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
570 cn.addSuperInterface(nd.toString());
574 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
575 parseClassBody(cn, pn.getChild("classbody"));
577 boolean hasConstructor = false;
578 for(Iterator method_it=cn.getMethods(); method_it.hasNext(); ) {
579 MethodDescriptor md=(MethodDescriptor)method_it.next();
580 hasConstructor |= md.isConstructor();
582 if((!hasConstructor) && (!cn.isEnum())) {
583 // add a default constructor for this class
584 MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
585 cn.getSymbol(), false);
586 BlockNode bn=new BlockNode();
587 state.addTreeCode(md,bn);
588 md.setDefaultConstructor();
593 cn.setSourceFileName(currsourcefile);
594 if (analyzeset != null)
601 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
602 ParseNode decls=pn.getChild("class_body_declaration_list");
604 ParseNodeVector pnv=decls.getChildren();
605 for(int i=0; i<pnv.size(); i++) {
606 ParseNode decl=pnv.elementAt(i);
607 if (isNode(decl,"member")) {
608 parseClassMember(cn,decl);
609 } else if (isNode(decl,"constructor")) {
610 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
611 } else if (isNode(decl, "static_block")) {
612 parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
613 } else if (isNode(decl,"block")) {
614 } else throw new Error();
619 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
620 ParseNode fieldnode=pn.getChild("field");
621 if (fieldnode!=null) {
622 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
625 ParseNode methodnode=pn.getChild("method");
626 if (methodnode!=null) {
627 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
630 ParseNode innerclassnode=pn.getChild("inner_class_declaration");
631 if (innerclassnode!=null) {
632 parseInnerClassDecl(cn,innerclassnode);
635 ParseNode innerinterfacenode=pn.getChild("interface_declaration");
636 if (innerinterfacenode!=null) {
637 parseInterfaceDecl(innerinterfacenode, cn);
641 ParseNode enumnode=pn.getChild("enum_declaration");
642 if (enumnode!=null) {
643 ClassDescriptor ecn=parseEnumDecl(cn,enumnode);
646 ParseNode flagnode=pn.getChild("flag");
647 if (flagnode!=null) {
648 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
651 // in case there are empty node
652 ParseNode emptynode=pn.getChild("empty");
653 if(emptynode != null) {
656 System.out.println("Unrecognized node:"+pn.PPrint(2,true));
660 private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
661 String basename=pn.getChild("name").getTerminal();
662 String classname=cn.getClassName()+"$"+basename;
664 if (cn.getPackage()==null)
665 cn.getSingleImportMappings().put(basename,classname);
667 cn.getSingleImportMappings().put(basename,cn.getPackage()+"."+classname);
669 ClassDescriptor icn=new ClassDescriptor(cn.getPackage(), classname, false);
671 icn.setImports(mandatoryImports, multiimports);
672 icn.setAsInnerClass();
673 icn.setSurroundingClass(cn.getSymbol());
674 icn.setSurrounding(cn);
675 cn.addInnerClass(icn);
676 if (!isEmpty(pn.getChild("super").getTerminal())) {
677 /* parse superclass name */
678 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
679 NameDescriptor nd=parseClassName(snn);
680 icn.setSuper(nd.toString());
682 if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
683 icn.getSymbol().equals(TypeUtil.TagClass)))
684 icn.setSuper(TypeUtil.ObjectClass);
686 // check inherited interfaces
687 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
688 /* parse inherited interface name */
689 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
690 ParseNodeVector pnv=snlist.getChildren();
691 for(int i=0; i<pnv.size(); i++) {
692 ParseNode decl=pnv.elementAt(i);
693 if (isNode(decl,"type")) {
694 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
695 icn.addSuperInterface(nd.toString());
699 icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
701 parseClassBody(icn, pn.getChild("classbody"));
703 if (analyzeset != null)
705 icn.setSourceFileName(currsourcefile);
711 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
712 ParseNode tn=pn.getChild("type");
713 String type_st=tn.getTerminal();
714 if(type_st.equals("byte")) {
715 return state.getTypeDescriptor(TypeDescriptor.BYTE);
716 } else if(type_st.equals("short")) {
717 return state.getTypeDescriptor(TypeDescriptor.SHORT);
718 } else if(type_st.equals("boolean")) {
719 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
720 } else if(type_st.equals("int")) {
721 return state.getTypeDescriptor(TypeDescriptor.INT);
722 } else if(type_st.equals("long")) {
723 return state.getTypeDescriptor(TypeDescriptor.LONG);
724 } else if(type_st.equals("char")) {
725 return state.getTypeDescriptor(TypeDescriptor.CHAR);
726 } else if(type_st.equals("float")) {
727 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
728 } else if(type_st.equals("double")) {
729 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
730 } else if(type_st.equals("class")) {
731 ParseNode nn=tn.getChild("class");
732 return state.getTypeDescriptor(parseClassName(nn.getChild("name")));
733 } else if(type_st.equals("array")) {
734 ParseNode nn=tn.getChild("array");
735 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
736 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
737 for(int i=0; i<numdims.intValue(); i++)
738 td=td.makeArray(state);
741 System.out.println(pn.PPrint(2, true));
746 //Needed to separate out top level call since if a base exists,
747 //we do not want to apply our resolveName function (i.e. deal with imports)
748 //otherwise, if base == null, we do just want to resolve name.
749 private NameDescriptor parseClassName(ParseNode nn) {
750 ParseNode base=nn.getChild("base");
751 ParseNode id=nn.getChild("identifier");
752 String classname = id.getTerminal();
754 return new NameDescriptor(resolveName(classname));
756 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
759 private NameDescriptor parseClassNameRecursive(ParseNode nn) {
760 ParseNode base=nn.getChild("base");
761 ParseNode id=nn.getChild("identifier");
762 String classname = id.getTerminal();
764 return new NameDescriptor(classname);
766 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
769 //This will get the mapping of a terminal class name
770 //to a canonical classname (with imports/package locations in them)
771 private String resolveName(String terminal) {
773 if(mandatoryImports.containsKey(terminal)) {
774 return (String) mandatoryImports.get(terminal);
776 if(multiimports.containsKey(terminal)) {
778 Object o = multiimports.get(terminal);
779 if(o instanceof Error) {
780 throw new Error("Class " + terminal + " is ambiguous. Cause: more than 1 package import contain the same class.");
782 //At this point, if we found a unique class
783 //we can treat it as a single, mandatory import.
784 mandatoryImports.put(terminal, o);
793 //only function difference between this and parseName() is that this
794 //does not look for a import mapping.
795 private NameDescriptor parseName(ParseNode nn) {
796 ParseNode base=nn.getChild("base");
797 ParseNode id=nn.getChild("identifier");
799 return new NameDescriptor(id.getTerminal());
801 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
804 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
805 String name=pn.getChild("name").getTerminal();
806 FlagDescriptor flag=new FlagDescriptor(name);
807 if (pn.getChild("external")!=null)
812 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
813 ParseNode mn=pn.getChild("modifier");
814 Modifiers m=parseModifiersList(mn);
815 if(cn.isInterface()) {
816 // TODO add version for normal Java later
817 // Can only be PUBLIC or STATIC or FINAL
818 if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
819 || (m.isSynchronized())) {
820 throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
822 m.addModifier(Modifiers.PUBLIC);
823 m.addModifier(Modifiers.STATIC);
824 m.addModifier(Modifiers.FINAL);
827 ParseNode tn=pn.getChild("type");
828 TypeDescriptor t=parseTypeDescriptor(tn);
829 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
830 ParseNodeVector pnv=vn.getChildren();
831 boolean isglobal=pn.getChild("global")!=null;
833 for(int i=0; i<pnv.size(); i++) {
834 ParseNode vardecl=pnv.elementAt(i);
835 ParseNode tmp=vardecl;
836 TypeDescriptor arrayt=t;
837 while (tmp.getChild("single")==null) {
838 arrayt=arrayt.makeArray(state);
839 tmp=tmp.getChild("array");
841 String identifier=tmp.getChild("single").getTerminal();
842 ParseNode epn=vardecl.getChild("initializer");
844 ExpressionNode en=null;
846 en=parseExpression(epn.getFirstChild());
847 en.setNumLine(epn.getFirstChild().getLine());
849 // for static field, the initializer should be considered as a
851 boolean isfirst = false;
852 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
854 // the first static block for this class
855 Modifiers m_i=new Modifiers();
856 m_i.addModifier(Modifiers.STATIC);
857 md = new MethodDescriptor(m_i, "staticblocks", false);
858 md.setAsStaticBlock();
864 cn.incStaticBlocks();
865 BlockNode bn=new BlockNode();
866 NameNode nn=new NameNode(new NameDescriptor(identifier));
867 nn.setNumLine(en.getNumLine());
868 AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
869 an.setNumLine(pn.getLine());
870 bn.addBlockStatement(new BlockExpressionNode(an));
872 state.addTreeCode(md,bn);
874 BlockNode obn = state.getMethodBody(md);
875 for(int ii = 0; ii < bn.size(); ii++) {
876 BlockStatementNode bsn = bn.get(ii);
877 obn.addBlockStatement(bsn);
879 state.addTreeCode(md, obn);
886 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
887 assignAnnotationsToType(m,arrayt);
891 private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type) {
892 Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
893 for(int i=0; i<annotations.size(); i++) {
894 // it only supports a marker annotation
895 AnnotationDescriptor an=annotations.elementAt(i);
896 type.addAnnotationMarker(an);
902 private ExpressionNode parseExpression(ParseNode pn) {
903 if (isNode(pn,"assignment"))
904 return parseAssignmentExpression(pn);
905 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
906 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
907 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
908 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
909 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
910 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
911 isNode(pn,"rightshift")||isNode(pn,"sub")||
912 isNode(pn,"urightshift")||isNode(pn,"sub")||
913 isNode(pn,"add")||isNode(pn,"mult")||
914 isNode(pn,"div")||isNode(pn,"mod")) {
915 ParseNodeVector pnv=pn.getChildren();
916 ParseNode left=pnv.elementAt(0);
917 ParseNode right=pnv.elementAt(1);
918 Operation op=new Operation(pn.getLabel());
919 OpNode on=new OpNode(parseExpression(left),parseExpression(right),op);
920 on.setNumLine(pn.getLine());
922 } else if (isNode(pn,"unaryplus")||
923 isNode(pn,"unaryminus")||
926 ParseNode left=pn.getFirstChild();
927 Operation op=new Operation(pn.getLabel());
928 OpNode on=new OpNode(parseExpression(left),op);
929 on.setNumLine(pn.getLine());
931 } else if (isNode(pn,"postinc")||
932 isNode(pn,"postdec")) {
933 ParseNode left=pn.getFirstChild();
934 AssignOperation op=new AssignOperation(pn.getLabel());
935 AssignmentNode an=new AssignmentNode(parseExpression(left),null,op);
936 an.setNumLine(pn.getLine());
939 } else if (isNode(pn,"preinc")||
940 isNode(pn,"predec")) {
941 ParseNode left=pn.getFirstChild();
942 AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
943 AssignmentNode an=new AssignmentNode(parseExpression(left),
944 new LiteralNode("integer",new Integer(1)),op);
945 an.setNumLine(pn.getLine());
947 } else if (isNode(pn,"literal")) {
948 String literaltype=pn.getTerminal();
949 ParseNode literalnode=pn.getChild(literaltype);
950 Object literal_obj=literalnode.getLiteral();
951 LiteralNode ln=new LiteralNode(literaltype, literal_obj);
952 ln.setNumLine(pn.getLine());
954 } else if (isNode(pn, "createobject")) {
955 TypeDescriptor td = parseTypeDescriptor(pn);
957 Vector args = parseArgumentList(pn);
958 boolean isglobal = pn.getChild("global") != null || pn.getChild("scratch") != null;
959 String disjointId = null;
960 if (pn.getChild("disjoint") != null) {
961 disjointId = pn.getChild("disjoint").getTerminal();
963 CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
964 con.setNumLine(pn.getLine());
965 for (int i = 0; i < args.size(); i++) {
966 con.addArgument((ExpressionNode) args.get(i));
968 /* Could have flag set or tag added here */
969 if (pn.getChild("flag_list") != null || pn.getChild("tag_list") != null) {
970 FlagEffects fe = new FlagEffects(null);
971 if (pn.getChild("flag_list") != null)
972 parseFlagEffect(fe, pn.getChild("flag_list"));
974 if (pn.getChild("tag_list") != null)
975 parseTagEffect(fe, pn.getChild("tag_list"));
976 con.addFlagEffects(fe);
980 } else if (isNode(pn,"createobjectcls")) {
981 //TODO::: FIX BUG!!! static fields in caller context need to become parameters
982 //TODO::: caller context need to be passed in here
983 TypeDescriptor td=parseTypeDescriptor(pn);
985 ClassDescriptor cnnew=new ClassDescriptor(packageName,td.getSymbol()+"$"+innerCount, false);
987 cnnew.setImports(mandatoryImports, multiimports);
988 cnnew.setSuper(td.getSymbol());
990 parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
991 TypeDescriptor tdnew=state.getTypeDescriptor(cnnew.getSymbol());
993 Vector args=parseArgumentList(pn);
995 CreateObjectNode con=new CreateObjectNode(tdnew, false, null);
996 con.setNumLine(pn.getLine());
997 for(int i=0; i<args.size(); i++) {
998 con.addArgument((ExpressionNode)args.get(i));
1002 if (analyzeset != null)
1003 analyzeset.add(cnnew);
1004 cnnew.setSourceFileName(currsourcefile);
1005 state.addClass(cnnew);
1008 } else if (isNode(pn,"createarray")) {
1009 //System.out.println(pn.PPrint(3,true));
1010 boolean isglobal=pn.getChild("global")!=null||
1011 pn.getChild("scratch")!=null;
1012 String disjointId=null;
1013 if( pn.getChild("disjoint") != null) {
1014 disjointId = pn.getChild("disjoint").getTerminal();
1016 TypeDescriptor td=parseTypeDescriptor(pn);
1017 Vector args=parseDimExprs(pn);
1019 if (pn.getChild("dims_opt").getLiteral()!=null)
1020 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1021 for(int i=0; i<(args.size()+num); i++)
1022 td=td.makeArray(state);
1023 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
1024 con.setNumLine(pn.getLine());
1025 for(int i=0; i<args.size(); i++) {
1026 con.addArgument((ExpressionNode)args.get(i));
1030 if (isNode(pn,"createarray2")) {
1031 TypeDescriptor td=parseTypeDescriptor(pn);
1033 if (pn.getChild("dims_opt").getLiteral()!=null)
1034 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1035 for(int i=0; i<num; i++)
1036 td=td.makeArray(state);
1037 CreateObjectNode con=new CreateObjectNode(td, false, null);
1038 con.setNumLine(pn.getLine());
1039 ParseNode ipn = pn.getChild("initializer");
1040 Vector initializers=parseVariableInitializerList(ipn);
1041 ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
1042 ain.setNumLine(pn.getLine());
1043 con.addArrayInitializer(ain);
1045 } else if (isNode(pn,"name")) {
1046 NameDescriptor nd=parseName(pn);
1047 NameNode nn=new NameNode(nd);
1048 nn.setNumLine(pn.getLine());
1050 } else if (isNode(pn,"this")) {
1051 NameDescriptor nd=new NameDescriptor("this");
1052 NameNode nn=new NameNode(nd);
1053 nn.setNumLine(pn.getLine());
1055 } else if (isNode(pn,"parentclass")) {
1056 NameDescriptor nd=new NameDescriptor("this");
1057 NameNode nn=new NameNode(nd);
1058 nn.setNumLine(pn.getLine());
1060 FieldAccessNode fan=new FieldAccessNode(nn,"this$parent");
1061 fan.setNumLine(pn.getLine());
1063 } else if (isNode(pn,"isavailable")) {
1064 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
1065 NameNode nn=new NameNode(nd);
1066 nn.setNumLine(pn.getLine());
1067 return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
1068 } else if (isNode(pn,"methodinvoke1")) {
1069 NameDescriptor nd=parseName(pn.getChild("name"));
1070 Vector args=parseArgumentList(pn);
1071 MethodInvokeNode min=new MethodInvokeNode(nd);
1072 min.setNumLine(pn.getLine());
1073 for(int i=0; i<args.size(); i++) {
1074 min.addArgument((ExpressionNode)args.get(i));
1077 } else if (isNode(pn,"methodinvoke2")) {
1078 String methodid=pn.getChild("id").getTerminal();
1079 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
1080 Vector args=parseArgumentList(pn);
1081 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
1082 min.setNumLine(pn.getLine());
1083 for(int i=0; i<args.size(); i++) {
1084 min.addArgument((ExpressionNode)args.get(i));
1087 } else if (isNode(pn,"fieldaccess")) {
1088 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1089 String fieldname=pn.getChild("field").getTerminal();
1091 FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1092 fan.setNumLine(pn.getLine());
1094 } else if (isNode(pn,"arrayaccess")) {
1095 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1096 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
1097 ArrayAccessNode aan=new ArrayAccessNode(en,index);
1098 aan.setNumLine(pn.getLine());
1100 } else if (isNode(pn,"cast1")) {
1102 CastNode cn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
1103 cn.setNumLine(pn.getLine());
1105 } catch (Exception e) {
1106 System.out.println(pn.PPrint(1,true));
1107 e.printStackTrace();
1110 } else if (isNode(pn,"cast2")) {
1111 CastNode cn=new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
1112 cn.setNumLine(pn.getLine());
1114 } else if (isNode(pn, "getoffset")) {
1115 TypeDescriptor td=parseTypeDescriptor(pn);
1116 String fieldname = pn.getChild("field").getTerminal();
1117 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
1118 return new OffsetNode(td, fieldname);
1119 } else if (isNode(pn, "tert")) {
1121 TertiaryNode tn=new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
1122 parseExpression(pn.getChild("trueexpr").getFirstChild()),
1123 parseExpression(pn.getChild("falseexpr").getFirstChild()) );
1124 tn.setNumLine(pn.getLine());
1127 } else if (isNode(pn, "instanceof")) {
1128 ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
1129 TypeDescriptor t=parseTypeDescriptor(pn);
1130 InstanceOfNode ion=new InstanceOfNode(exp,t);
1131 ion.setNumLine(pn.getLine());
1133 } else if (isNode(pn, "array_initializer")) {
1134 Vector initializers=parseVariableInitializerList(pn);
1135 return new ArrayInitializerNode(initializers);
1136 } else if (isNode(pn, "class_type")) {
1137 TypeDescriptor td=parseTypeDescriptor(pn);
1138 ClassTypeNode ctn=new ClassTypeNode(td);
1139 ctn.setNumLine(pn.getLine());
1141 } else if (isNode(pn, "empty")) {
1144 System.out.println("---------------------");
1145 System.out.println(pn.PPrint(3,true));
1150 private Vector parseDimExprs(ParseNode pn) {
1151 Vector arglist=new Vector();
1152 ParseNode an=pn.getChild("dim_exprs");
1153 if (an==null) /* No argument list */
1155 ParseNodeVector anv=an.getChildren();
1156 for(int i=0; i<anv.size(); i++) {
1157 arglist.add(parseExpression(anv.elementAt(i)));
1162 private Vector parseArgumentList(ParseNode pn) {
1163 Vector arglist=new Vector();
1164 ParseNode an=pn.getChild("argument_list");
1165 if (an==null) /* No argument list */
1167 ParseNodeVector anv=an.getChildren();
1168 for(int i=0; i<anv.size(); i++) {
1169 arglist.add(parseExpression(anv.elementAt(i)));
1174 private Vector[] parseConsArgumentList(ParseNode pn) {
1175 Vector arglist=new Vector();
1176 Vector varlist=new Vector();
1177 ParseNode an=pn.getChild("cons_argument_list");
1178 if (an==null) /* No argument list */
1179 return new Vector[] {varlist, arglist};
1180 ParseNodeVector anv=an.getChildren();
1181 for(int i=0; i<anv.size(); i++) {
1182 ParseNode cpn=anv.elementAt(i);
1183 ParseNode var=cpn.getChild("var");
1184 ParseNode exp=cpn.getChild("exp").getFirstChild();
1185 varlist.add(var.getTerminal());
1186 arglist.add(parseExpression(exp));
1188 return new Vector[] {varlist, arglist};
1191 private Vector parseVariableInitializerList(ParseNode pn) {
1192 Vector varInitList=new Vector();
1193 ParseNode vin=pn.getChild("var_init_list");
1194 if (vin==null) /* No argument list */
1196 ParseNodeVector vinv=vin.getChildren();
1197 for(int i=0; i<vinv.size(); i++) {
1198 varInitList.add(parseExpression(vinv.elementAt(i)));
1203 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
1204 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1205 ParseNodeVector pnv=pn.getChild("args").getChildren();
1207 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
1208 an.setNumLine(pn.getLine());
1213 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1214 ParseNode headern=pn.getChild("method_header");
1215 ParseNode bodyn=pn.getChild("body");
1216 MethodDescriptor md=parseMethodHeader(headern);
1218 BlockNode bn=parseBlock(bodyn);
1219 bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1221 state.addTreeCode(md,bn);
1223 // this is a hack for investigating new language features
1224 // at the AST level, someday should evolve into a nice compiler
1226 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
1227 // md.getSymbol().equals( ***put your method in here like: "main" )
1229 // bn.setStyle( BlockNode.NORMAL );
1230 // System.out.println( bn.printNode( 0 ) );
1233 } catch (Exception e) {
1234 System.out.println("Error with method:"+md.getSymbol());
1235 e.printStackTrace();
1238 System.out.println("Error with method:"+md.getSymbol());
1239 e.printStackTrace();
1244 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1245 ParseNode mn=pn.getChild("modifiers");
1246 Modifiers m=parseModifiersList(mn);
1247 ParseNode cdecl=pn.getChild("constructor_declarator");
1248 boolean isglobal=cdecl.getChild("global")!=null;
1249 String name=resolveName(cn.getSymbol());
1250 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1251 ParseNode paramnode=cdecl.getChild("parameters");
1252 parseParameterList(md,paramnode);
1253 ParseNode bodyn0=pn.getChild("body");
1254 ParseNode bodyn=bodyn0.getChild("constructor_body");
1257 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1258 bn=parseBlock(bodyn);
1261 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1262 ParseNode sin=bodyn.getChild("superinvoke");
1263 NameDescriptor nd=new NameDescriptor("super");
1264 Vector args=parseArgumentList(sin);
1265 MethodInvokeNode min=new MethodInvokeNode(nd);
1266 min.setNumLine(sin.getLine());
1267 for(int i=0; i<args.size(); i++) {
1268 min.addArgument((ExpressionNode)args.get(i));
1270 BlockExpressionNode ben=new BlockExpressionNode(min);
1271 bn.addFirstBlockStatement(ben);
1273 } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1274 ParseNode eci=bodyn.getChild("explconstrinv");
1275 NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1276 Vector args=parseArgumentList(eci);
1277 MethodInvokeNode min=new MethodInvokeNode(nd);
1278 min.setNumLine(eci.getLine());
1279 for(int i=0; i<args.size(); i++) {
1280 min.addArgument((ExpressionNode)args.get(i));
1282 BlockExpressionNode ben=new BlockExpressionNode(min);
1283 ben.setNumLine(eci.getLine());
1284 bn.addFirstBlockStatement(ben);
1286 state.addTreeCode(md,bn);
1289 private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1290 // Each class maintains one MethodDecscriptor which combines all its
1291 // static blocks in their declaration order
1292 boolean isfirst = false;
1293 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1295 // the first static block for this class
1296 Modifiers m_i=new Modifiers();
1297 m_i.addModifier(Modifiers.STATIC);
1298 md = new MethodDescriptor(m_i, "staticblocks", false);
1299 md.setAsStaticBlock();
1302 ParseNode bodyn=pn.getChild("body");
1306 cn.incStaticBlocks();
1308 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1309 bn=parseBlock(bodyn);
1313 state.addTreeCode(md,bn);
1315 BlockNode obn = state.getMethodBody(md);
1316 for(int i = 0; i < bn.size(); i++) {
1317 BlockStatementNode bsn = bn.get(i);
1318 obn.addBlockStatement(bsn);
1320 state.addTreeCode(md, obn);
1325 public BlockNode parseBlock(ParseNode pn) {
1326 this.m_taskexitnum = 0;
1327 if (pn==null||isEmpty(pn.getTerminal()))
1328 return new BlockNode();
1329 ParseNode bsn=pn.getChild("block_statement_list");
1330 return parseBlockHelper(bsn);
1333 private BlockNode parseBlockHelper(ParseNode pn) {
1334 ParseNodeVector pnv=pn.getChildren();
1335 BlockNode bn=new BlockNode();
1336 for(int i=0; i<pnv.size(); i++) {
1337 Vector bsv=parseBlockStatement(pnv.elementAt(i));
1338 for(int j=0; j<bsv.size(); j++) {
1339 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1345 public BlockNode parseSingleBlock(ParseNode pn, String label){
1346 BlockNode bn=new BlockNode();
1347 Vector bsv=parseBlockStatement(pn,label);
1348 for(int j=0; j<bsv.size(); j++) {
1349 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1351 bn.setStyle(BlockNode.NOBRACES);
1355 public BlockNode parseSingleBlock(ParseNode pn) {
1356 return parseSingleBlock(pn,null);
1359 public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
1360 ParseNodeVector pnv=pn.getChildren();
1361 Vector bv=new Vector();
1362 for(int i=0; i<pnv.size(); i++) {
1363 bv.addAll(parseBlockStatement(pnv.elementAt(i)));
1368 public Vector parseBlockStatement(ParseNode pn){
1369 return parseBlockStatement(pn,null);
1372 public Vector parseBlockStatement(ParseNode pn, String label) {
1373 Vector blockstatements=new Vector();
1374 if (isNode(pn,"tag_declaration")) {
1375 String name=pn.getChild("single").getTerminal();
1376 String type=pn.getChild("type").getTerminal();
1378 TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1379 tdn.setNumLine(pn.getLine());
1381 blockstatements.add(tdn);
1382 } else if (isNode(pn,"local_variable_declaration")) {
1384 TypeDescriptor t=parseTypeDescriptor(pn);
1385 ParseNode vn=pn.getChild("variable_declarators_list");
1386 ParseNodeVector pnv=vn.getChildren();
1387 for(int i=0; i<pnv.size(); i++) {
1388 ParseNode vardecl=pnv.elementAt(i);
1391 ParseNode tmp=vardecl;
1392 TypeDescriptor arrayt=t;
1394 while (tmp.getChild("single")==null) {
1395 arrayt=arrayt.makeArray(state);
1396 tmp=tmp.getChild("array");
1398 String identifier=tmp.getChild("single").getTerminal();
1400 ParseNode epn=vardecl.getChild("initializer");
1403 ExpressionNode en=null;
1405 en=parseExpression(epn.getFirstChild());
1407 DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1408 dn.setNumLine(tmp.getLine());
1410 ParseNode mn=pn.getChild("modifiers");
1412 // here, modifers parse node has the list of annotations
1413 Modifiers m=parseModifiersList(mn);
1414 assignAnnotationsToType(m, arrayt);
1417 blockstatements.add(dn);
1419 } else if (isNode(pn,"nop")) {
1421 } else if (isNode(pn,"expression")) {
1422 BlockExpressionNode ben=new BlockExpressionNode(parseExpression(pn.getFirstChild()));
1423 ben.setNumLine(pn.getLine());
1424 blockstatements.add(ben);
1425 } else if (isNode(pn,"ifstatement")) {
1426 IfStatementNode isn=new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1427 parseSingleBlock(pn.getChild("statement").getFirstChild()),
1428 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null);
1429 isn.setNumLine(pn.getLine());
1431 blockstatements.add(isn);
1432 } else if (isNode(pn,"switch_statement")) {
1433 // TODO add version for normal Java later
1434 SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1435 parseSingleBlock(pn.getChild("statement").getFirstChild()));
1436 ssn.setNumLine(pn.getLine());
1437 blockstatements.add(ssn);
1438 } else if (isNode(pn,"switch_block_list")) {
1439 // TODO add version for normal Java later
1440 ParseNodeVector pnv=pn.getChildren();
1441 for(int i=0; i<pnv.size(); i++) {
1442 ParseNode sblockdecl=pnv.elementAt(i);
1444 if(isNode(sblockdecl, "switch_block")) {
1445 ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1446 ParseNodeVector labelv=lpn.getChildren();
1447 Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1448 for(int j=0; j<labelv.size(); j++) {
1449 ParseNode labeldecl=labelv.elementAt(j);
1450 if(isNode(labeldecl, "switch_label")) {
1451 SwitchLabelNode sln=new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false);
1452 sln.setNumLine(labeldecl.getLine());
1453 slv.addElement(sln);
1454 } else if(isNode(labeldecl, "default_switch_label")) {
1455 SwitchLabelNode sln=new SwitchLabelNode(null, true);
1456 sln.setNumLine(labeldecl.getLine());
1457 slv.addElement(sln);
1461 SwitchBlockNode sbn=new SwitchBlockNode(slv,
1462 parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild()));
1463 sbn.setNumLine(sblockdecl.getLine());
1465 blockstatements.add(sbn);
1469 } else if (isNode(pn, "trycatchstatement")) {
1470 // TODO add version for normal Java later
1471 // Do not fully support exceptions now. Only make sure that if there are no
1472 // exceptions thrown, the execution is right
1473 ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1474 BlockNode bn=parseBlockHelper(tpn);
1475 blockstatements.add(new SubBlockNode(bn));
1477 ParseNode fbk = pn.getChild("finallyblock");
1479 ParseNode fpn = fbk.getFirstChild();
1480 BlockNode fbn=parseBlockHelper(fpn);
1481 blockstatements.add(new SubBlockNode(fbn));
1483 } else if (isNode(pn, "throwstatement")) {
1484 // TODO Simply return here
1485 //blockstatements.add(new ReturnNode());
1486 } else if (isNode(pn,"taskexit")) {
1488 if (pn.getChild("flag_effects_list")!=null)
1489 vfe=parseFlags(pn.getChild("flag_effects_list"));
1491 if (pn.getChild("cons_checks")!=null)
1492 ccs=parseChecks(pn.getChild("cons_checks"));
1493 TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1494 ten.setNumLine(pn.getLine());
1495 blockstatements.add(ten);
1496 } else if (isNode(pn,"atomic")) {
1497 BlockNode bn=parseBlockHelper(pn);
1498 AtomicNode an=new AtomicNode(bn);
1499 an.setNumLine(pn.getLine());
1500 blockstatements.add(an);
1501 } else if (isNode(pn,"synchronized")) {
1502 BlockNode bn=parseBlockHelper(pn.getChild("block"));
1503 ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
1504 SynchronizedNode sn=new SynchronizedNode(en, bn);
1505 sn.setNumLine(pn.getLine());
1506 blockstatements.add(sn);
1507 } else if (isNode(pn,"return")) {
1508 if (isEmpty(pn.getTerminal()))
1509 blockstatements.add(new ReturnNode());
1511 ExpressionNode en=parseExpression(pn.getFirstChild());
1512 ReturnNode rn=new ReturnNode(en);
1513 rn.setNumLine(pn.getLine());
1514 blockstatements.add(rn);
1516 } else if (isNode(pn,"block_statement_list")) {
1517 BlockNode bn=parseBlockHelper(pn);
1518 blockstatements.add(new SubBlockNode(bn));
1519 } else if (isNode(pn,"empty")) {
1521 } else if (isNode(pn,"statement_expression_list")) {
1522 ParseNodeVector pnv=pn.getChildren();
1523 BlockNode bn=new BlockNode();
1524 for(int i=0; i<pnv.size(); i++) {
1525 ExpressionNode en=parseExpression(pnv.elementAt(i));
1526 blockstatements.add(new BlockExpressionNode(en));
1528 bn.setStyle(BlockNode.EXPRLIST);
1529 } else if (isNode(pn,"forstatement")) {
1530 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
1531 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
1532 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1533 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1534 if(condition == null) {
1535 // no condition clause, make a 'true' expression as the condition
1536 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1538 LoopNode ln=new LoopNode(init,condition,update,body,label);
1539 ln.setNumLine(pn.getLine());
1540 blockstatements.add(ln);
1541 } else if (isNode(pn,"whilestatement")) {
1542 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1543 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1544 if(condition == null) {
1545 // no condition clause, make a 'true' expression as the condition
1546 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1548 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP,label));
1549 } else if (isNode(pn,"dowhilestatement")) {
1550 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1551 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1552 if(condition == null) {
1553 // no condition clause, make a 'true' expression as the condition
1554 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1556 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP,label));
1557 } else if (isNode(pn,"sese")) {
1558 ParseNode pnID=pn.getChild("identifier");
1560 if( pnID != null ) {
1561 stID=pnID.getFirstChild().getTerminal();
1563 SESENode start=new SESENode(stID);
1564 start.setNumLine(pn.getLine());
1565 SESENode end =new SESENode(stID);
1567 end.setStart(start);
1568 blockstatements.add(start);
1569 blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1570 blockstatements.add(end);
1571 } else if (isNode(pn,"continue")) {
1572 ContinueBreakNode cbn=new ContinueBreakNode(false);
1573 cbn.setNumLine(pn.getLine());
1574 blockstatements.add(cbn);
1575 } else if (isNode(pn,"break")) {
1576 ContinueBreakNode cbn=new ContinueBreakNode(true);
1577 cbn.setNumLine(pn.getLine());
1578 blockstatements.add(cbn);
1579 ParseNode idopt_pn=pn.getChild("identifier_opt");
1580 ParseNode name_pn=idopt_pn.getChild("name");
1581 // name_pn.getTerminal() gives you the label
1583 } else if (isNode(pn,"genreach")) {
1584 String graphName = pn.getChild("graphName").getTerminal();
1585 blockstatements.add(new GenReachNode(graphName) );
1587 } else if (isNode(pn,"gen_def_reach")) {
1588 String outputName = pn.getChild("outputName").getTerminal();
1589 blockstatements.add(new GenDefReachNode(outputName) );
1591 } else if(isNode(pn,"labeledstatement")) {
1592 String labeledstatement = pn.getChild("name").getTerminal();
1593 BlockNode bn=parseSingleBlock(pn.getChild("statement").getFirstChild(),labeledstatement);
1594 blockstatements.add(new SubBlockNode(bn));
1596 System.out.println("---------------");
1597 System.out.println(pn.PPrint(3,true));
1600 return blockstatements;
1603 public MethodDescriptor parseMethodHeader(ParseNode pn) {
1604 ParseNode mn=pn.getChild("modifiers");
1605 Modifiers m=parseModifiersList(mn);
1607 ParseNode tn=pn.getChild("returntype");
1608 TypeDescriptor returntype;
1610 returntype=parseTypeDescriptor(tn);
1612 returntype=new TypeDescriptor(TypeDescriptor.VOID);
1614 ParseNode pmd=pn.getChild("method_declarator");
1615 String name=pmd.getChild("name").getTerminal();
1616 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1618 ParseNode paramnode=pmd.getChild("parameters");
1619 parseParameterList(md,paramnode);
1623 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1624 ParseNode paramlist=pn.getChild("formal_parameter_list");
1625 if (paramlist==null)
1627 ParseNodeVector pnv=paramlist.getChildren();
1628 for(int i=0; i<pnv.size(); i++) {
1629 ParseNode paramn=pnv.elementAt(i);
1631 if (isNode(paramn, "tag_parameter")) {
1632 String paramname=paramn.getChild("single").getTerminal();
1633 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1634 md.addTagParameter(type, paramname);
1637 TypeDescriptor type=parseTypeDescriptor(paramn);
1639 ParseNode tmp=paramn;
1640 while (tmp.getChild("single")==null) {
1641 type=type.makeArray(state);
1642 tmp=tmp.getChild("array");
1644 String paramname=tmp.getChild("single").getTerminal();
1646 md.addParameter(type, paramname);
1647 if(isNode(paramn, "annotation_parameter")) {
1648 ParseNode listnode=paramn.getChild("annotation_list");
1649 parseParameterAnnotation(listnode,type);
1656 public Modifiers parseModifiersList(ParseNode pn) {
1657 Modifiers m=new Modifiers();
1658 ParseNode modlist=pn.getChild("modifier_list");
1659 if (modlist!=null) {
1660 ParseNodeVector pnv=modlist.getChildren();
1661 for(int i=0; i<pnv.size(); i++) {
1662 ParseNode modn=pnv.elementAt(i);
1663 if (isNode(modn,"public"))
1664 m.addModifier(Modifiers.PUBLIC);
1665 else if (isNode(modn,"protected"))
1666 m.addModifier(Modifiers.PROTECTED);
1667 else if (isNode(modn,"private"))
1668 m.addModifier(Modifiers.PRIVATE);
1669 else if (isNode(modn,"static"))
1670 m.addModifier(Modifiers.STATIC);
1671 else if (isNode(modn,"final"))
1672 m.addModifier(Modifiers.FINAL);
1673 else if (isNode(modn,"native"))
1674 m.addModifier(Modifiers.NATIVE);
1675 else if (isNode(modn,"synchronized"))
1676 m.addModifier(Modifiers.SYNCHRONIZED);
1677 else if (isNode(modn,"atomic"))
1678 m.addModifier(Modifiers.ATOMIC);
1679 else if (isNode(modn,"abstract"))
1680 m.addModifier(Modifiers.ABSTRACT);
1681 else if (isNode(modn,"volatile"))
1682 m.addModifier(Modifiers.VOLATILE);
1683 else if (isNode(modn,"transient"))
1684 m.addModifier(Modifiers.TRANSIENT);
1685 else if(isNode(modn,"annotation_list"))
1686 parseAnnotationList(modn,m);
1688 throw new Error("Unrecognized Modifier:"+modn.getLabel());
1695 private void parseAnnotationList(ParseNode pn, Modifiers m) {
1696 ParseNodeVector pnv = pn.getChildren();
1697 for (int i = 0; i < pnv.size(); i++) {
1698 ParseNode body_list = pnv.elementAt(i);
1699 if (isNode(body_list, "annotation_body")) {
1700 ParseNode body_node = body_list.getFirstChild();
1701 if (isNode(body_node, "marker_annotation")) {
1702 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1703 } else if (isNode(body_node, "single_annotation")) {
1704 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1705 body_node.getChild("element_value").getTerminal()));
1706 } else if (isNode(body_node, "normal_annotation")) {
1707 throw new Error("Annotation with multiple data members is not supported yet.");
1713 private void parseParameterAnnotation(ParseNode pn,TypeDescriptor type) {
1714 ParseNodeVector pnv = pn.getChildren();
1715 for (int i = 0; i < pnv.size(); i++) {
1716 ParseNode body_list = pnv.elementAt(i);
1717 if (isNode(body_list, "annotation_body")) {
1718 ParseNode body_node = body_list.getFirstChild();
1719 if (isNode(body_node, "marker_annotation")) {
1720 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1721 } else if (isNode(body_node, "single_annotation")) {
1722 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1723 body_node.getChild("element_value").getTerminal()));
1724 } else if (isNode(body_node, "normal_annotation")) {
1725 throw new Error("Annotation with multiple data members is not supported yet.");
1731 private boolean isNode(ParseNode pn, String label) {
1732 if (pn.getLabel().equals(label))
1737 private static boolean isEmpty(ParseNode pn) {
1738 if (pn.getLabel().equals("empty"))
1744 private static boolean isEmpty(String s) {
1745 if (s.equals("empty"))
1751 /** Throw an exception if something is unexpected */
1752 private void check(ParseNode pn, String label) {
1754 throw new Error(pn+ "IE: Expected '" + label + "', got null");
1756 if (!pn.getLabel().equals(label)) {
1757 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");