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);
636 ParseNode innerinterfacenode=pn.getChild("interface_declaration");
637 if (innerinterfacenode!=null) {
638 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) {
659 private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
660 String basename=pn.getChild("name").getTerminal();
661 String classname=cn.getClassName()+"$"+basename;
663 if (cn.getPackage()==null)
664 cn.getSingleImportMappings().put(basename,classname);
666 cn.getSingleImportMappings().put(basename,cn.getPackage()+"."+classname);
668 ClassDescriptor icn=new ClassDescriptor(cn.getPackage(), classname, false);
670 icn.setImports(mandatoryImports, multiimports);
671 icn.setAsInnerClass();
672 icn.setSurroundingClass(cn.getSymbol());
673 icn.setSurrounding(cn);
674 cn.addInnerClass(icn);
675 if (!isEmpty(pn.getChild("super").getTerminal())) {
676 /* parse superclass name */
677 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
678 NameDescriptor nd=parseClassName(snn);
679 icn.setSuper(nd.toString());
681 if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
682 icn.getSymbol().equals(TypeUtil.TagClass)))
683 icn.setSuper(TypeUtil.ObjectClass);
685 // check inherited interfaces
686 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
687 /* parse inherited interface name */
688 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
689 ParseNodeVector pnv=snlist.getChildren();
690 for(int i=0; i<pnv.size(); i++) {
691 ParseNode decl=pnv.elementAt(i);
692 if (isNode(decl,"type")) {
693 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
694 icn.addSuperInterface(nd.toString());
698 icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
700 parseClassBody(icn, pn.getChild("classbody"));
702 if (analyzeset != null)
704 icn.setSourceFileName(currsourcefile);
710 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
711 ParseNode tn=pn.getChild("type");
712 String type_st=tn.getTerminal();
713 if(type_st.equals("byte")) {
714 return state.getTypeDescriptor(TypeDescriptor.BYTE);
715 } else if(type_st.equals("short")) {
716 return state.getTypeDescriptor(TypeDescriptor.SHORT);
717 } else if(type_st.equals("boolean")) {
718 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
719 } else if(type_st.equals("int")) {
720 return state.getTypeDescriptor(TypeDescriptor.INT);
721 } else if(type_st.equals("long")) {
722 return state.getTypeDescriptor(TypeDescriptor.LONG);
723 } else if(type_st.equals("char")) {
724 return state.getTypeDescriptor(TypeDescriptor.CHAR);
725 } else if(type_st.equals("float")) {
726 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
727 } else if(type_st.equals("double")) {
728 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
729 } else if(type_st.equals("class")) {
730 ParseNode nn=tn.getChild("class");
731 return state.getTypeDescriptor(parseClassName(nn.getChild("name")));
732 } else if(type_st.equals("array")) {
733 ParseNode nn=tn.getChild("array");
734 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
735 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
736 for(int i=0; i<numdims.intValue(); i++)
737 td=td.makeArray(state);
740 System.out.println(pn.PPrint(2, true));
745 //Needed to separate out top level call since if a base exists,
746 //we do not want to apply our resolveName function (i.e. deal with imports)
747 //otherwise, if base == null, we do just want to resolve name.
748 private NameDescriptor parseClassName(ParseNode nn) {
749 ParseNode base=nn.getChild("base");
750 ParseNode id=nn.getChild("identifier");
751 String classname = id.getTerminal();
753 return new NameDescriptor(resolveName(classname));
755 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
758 private NameDescriptor parseClassNameRecursive(ParseNode nn) {
759 ParseNode base=nn.getChild("base");
760 ParseNode id=nn.getChild("identifier");
761 String classname = id.getTerminal();
763 return new NameDescriptor(classname);
765 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
768 //This will get the mapping of a terminal class name
769 //to a canonical classname (with imports/package locations in them)
770 private String resolveName(String terminal) {
772 if(mandatoryImports.containsKey(terminal)) {
773 return (String) mandatoryImports.get(terminal);
775 if(multiimports.containsKey(terminal)) {
777 Object o = multiimports.get(terminal);
778 if(o instanceof Error) {
779 throw new Error("Class " + terminal + " is ambiguous. Cause: more than 1 package import contain the same class.");
781 //At this point, if we found a unique class
782 //we can treat it as a single, mandatory import.
783 mandatoryImports.put(terminal, o);
792 //only function difference between this and parseName() is that this
793 //does not look for a import mapping.
794 private NameDescriptor parseName(ParseNode nn) {
795 ParseNode base=nn.getChild("base");
796 ParseNode id=nn.getChild("identifier");
798 return new NameDescriptor(id.getTerminal());
800 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
803 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
804 String name=pn.getChild("name").getTerminal();
805 FlagDescriptor flag=new FlagDescriptor(name);
806 if (pn.getChild("external")!=null)
811 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
812 ParseNode mn=pn.getChild("modifier");
813 Modifiers m=parseModifiersList(mn);
814 if(cn.isInterface()) {
815 // TODO add version for normal Java later
816 // Can only be PUBLIC or STATIC or FINAL
817 if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
818 || (m.isSynchronized())) {
819 throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
821 m.addModifier(Modifiers.PUBLIC);
822 m.addModifier(Modifiers.STATIC);
823 m.addModifier(Modifiers.FINAL);
826 ParseNode tn=pn.getChild("type");
827 TypeDescriptor t=parseTypeDescriptor(tn);
828 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
829 ParseNodeVector pnv=vn.getChildren();
830 boolean isglobal=pn.getChild("global")!=null;
832 for(int i=0; i<pnv.size(); i++) {
833 ParseNode vardecl=pnv.elementAt(i);
834 ParseNode tmp=vardecl;
835 TypeDescriptor arrayt=t;
836 while (tmp.getChild("single")==null) {
837 arrayt=arrayt.makeArray(state);
838 tmp=tmp.getChild("array");
840 String identifier=tmp.getChild("single").getTerminal();
841 ParseNode epn=vardecl.getChild("initializer");
843 ExpressionNode en=null;
845 en=parseExpression(epn.getFirstChild());
846 en.setNumLine(epn.getFirstChild().getLine());
848 // for static field, the initializer should be considered as a
850 boolean isfirst = false;
851 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
853 // the first static block for this class
854 Modifiers m_i=new Modifiers();
855 m_i.addModifier(Modifiers.STATIC);
856 md = new MethodDescriptor(m_i, "staticblocks", false);
857 md.setAsStaticBlock();
863 cn.incStaticBlocks();
864 BlockNode bn=new BlockNode();
865 NameNode nn=new NameNode(new NameDescriptor(identifier));
866 nn.setNumLine(en.getNumLine());
867 AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
868 an.setNumLine(pn.getLine());
869 bn.addBlockStatement(new BlockExpressionNode(an));
871 state.addTreeCode(md,bn);
873 BlockNode obn = state.getMethodBody(md);
874 for(int ii = 0; ii < bn.size(); ii++) {
875 BlockStatementNode bsn = bn.get(ii);
876 obn.addBlockStatement(bsn);
878 state.addTreeCode(md, obn);
885 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
886 assignAnnotationsToType(m,arrayt);
890 private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type) {
891 Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
892 for(int i=0; i<annotations.size(); i++) {
893 // it only supports a marker annotation
894 AnnotationDescriptor an=annotations.elementAt(i);
895 type.addAnnotationMarker(an);
901 private ExpressionNode parseExpression(ParseNode pn) {
902 if (isNode(pn,"assignment"))
903 return parseAssignmentExpression(pn);
904 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
905 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
906 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
907 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
908 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
909 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
910 isNode(pn,"rightshift")||isNode(pn,"sub")||
911 isNode(pn,"urightshift")||isNode(pn,"sub")||
912 isNode(pn,"add")||isNode(pn,"mult")||
913 isNode(pn,"div")||isNode(pn,"mod")) {
914 ParseNodeVector pnv=pn.getChildren();
915 ParseNode left=pnv.elementAt(0);
916 ParseNode right=pnv.elementAt(1);
917 Operation op=new Operation(pn.getLabel());
918 OpNode on=new OpNode(parseExpression(left),parseExpression(right),op);
919 on.setNumLine(pn.getLine());
921 } else if (isNode(pn,"unaryplus")||
922 isNode(pn,"unaryminus")||
925 ParseNode left=pn.getFirstChild();
926 Operation op=new Operation(pn.getLabel());
927 OpNode on=new OpNode(parseExpression(left),op);
928 on.setNumLine(pn.getLine());
930 } else if (isNode(pn,"postinc")||
931 isNode(pn,"postdec")) {
932 ParseNode left=pn.getFirstChild();
933 AssignOperation op=new AssignOperation(pn.getLabel());
934 AssignmentNode an=new AssignmentNode(parseExpression(left),null,op);
935 an.setNumLine(pn.getLine());
938 } else if (isNode(pn,"preinc")||
939 isNode(pn,"predec")) {
940 ParseNode left=pn.getFirstChild();
941 AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
942 AssignmentNode an=new AssignmentNode(parseExpression(left),
943 new LiteralNode("integer",new Integer(1)),op);
944 an.setNumLine(pn.getLine());
946 } else if (isNode(pn,"literal")) {
947 String literaltype=pn.getTerminal();
948 ParseNode literalnode=pn.getChild(literaltype);
949 Object literal_obj=literalnode.getLiteral();
950 LiteralNode ln=new LiteralNode(literaltype, literal_obj);
951 ln.setNumLine(pn.getLine());
953 } else if (isNode(pn, "createobject")) {
954 TypeDescriptor td = parseTypeDescriptor(pn);
956 Vector args = parseArgumentList(pn);
957 boolean isglobal = pn.getChild("global") != null || pn.getChild("scratch") != null;
958 String disjointId = null;
959 if (pn.getChild("disjoint") != null) {
960 disjointId = pn.getChild("disjoint").getTerminal();
962 CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
963 con.setNumLine(pn.getLine());
964 for (int i = 0; i < args.size(); i++) {
965 con.addArgument((ExpressionNode) args.get(i));
967 /* Could have flag set or tag added here */
968 if (pn.getChild("flag_list") != null || pn.getChild("tag_list") != null) {
969 FlagEffects fe = new FlagEffects(null);
970 if (pn.getChild("flag_list") != null)
971 parseFlagEffect(fe, pn.getChild("flag_list"));
973 if (pn.getChild("tag_list") != null)
974 parseTagEffect(fe, pn.getChild("tag_list"));
975 con.addFlagEffects(fe);
979 } else if (isNode(pn,"createobjectcls")) {
980 //TODO::: FIX BUG!!! static fields in caller context need to become parameters
981 TypeDescriptor td=parseTypeDescriptor(pn);
983 ClassDescriptor cnnew=new ClassDescriptor(packageName,td.getSymbol()+"$"+innerCount, false);
985 cnnew.setImports(mandatoryImports, multiimports);
986 cnnew.setSuper(td.getSymbol());
987 parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
988 Vector args=parseArgumentList(pn);
990 CreateObjectNode con=new CreateObjectNode(td, false, null);
991 con.setNumLine(pn.getLine());
992 for(int i=0; i<args.size(); i++) {
993 con.addArgument((ExpressionNode)args.get(i));
997 if (analyzeset != null)
998 analyzeset.add(cnnew);
999 cnnew.setSourceFileName(currsourcefile);
1000 state.addClass(cnnew);
1003 } else if (isNode(pn,"createarray")) {
1004 //System.out.println(pn.PPrint(3,true));
1005 boolean isglobal=pn.getChild("global")!=null||
1006 pn.getChild("scratch")!=null;
1007 String disjointId=null;
1008 if( pn.getChild("disjoint") != null) {
1009 disjointId = pn.getChild("disjoint").getTerminal();
1011 TypeDescriptor td=parseTypeDescriptor(pn);
1012 Vector args=parseDimExprs(pn);
1014 if (pn.getChild("dims_opt").getLiteral()!=null)
1015 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1016 for(int i=0; i<(args.size()+num); i++)
1017 td=td.makeArray(state);
1018 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
1019 con.setNumLine(pn.getLine());
1020 for(int i=0; i<args.size(); i++) {
1021 con.addArgument((ExpressionNode)args.get(i));
1025 if (isNode(pn,"createarray2")) {
1026 TypeDescriptor td=parseTypeDescriptor(pn);
1028 if (pn.getChild("dims_opt").getLiteral()!=null)
1029 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1030 for(int i=0; i<num; i++)
1031 td=td.makeArray(state);
1032 CreateObjectNode con=new CreateObjectNode(td, false, null);
1033 con.setNumLine(pn.getLine());
1034 ParseNode ipn = pn.getChild("initializer");
1035 Vector initializers=parseVariableInitializerList(ipn);
1036 ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
1037 ain.setNumLine(pn.getLine());
1038 con.addArrayInitializer(ain);
1040 } else if (isNode(pn,"name")) {
1041 NameDescriptor nd=parseName(pn);
1042 NameNode nn=new NameNode(nd);
1043 nn.setNumLine(pn.getLine());
1045 } else if (isNode(pn,"this")) {
1046 NameDescriptor nd=new NameDescriptor("this");
1047 NameNode nn=new NameNode(nd);
1048 nn.setNumLine(pn.getLine());
1050 } else if (isNode(pn,"parentclass")) {
1051 NameDescriptor nd=new NameDescriptor("this");
1052 NameNode nn=new NameNode(nd);
1053 nn.setNumLine(pn.getLine());
1055 FieldAccessNode fan=new FieldAccessNode(nn,"this$parent");
1056 fan.setNumLine(pn.getLine());
1058 } else if (isNode(pn,"isavailable")) {
1059 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
1060 NameNode nn=new NameNode(nd);
1061 nn.setNumLine(pn.getLine());
1062 return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
1063 } else if (isNode(pn,"methodinvoke1")) {
1064 NameDescriptor nd=parseName(pn.getChild("name"));
1065 Vector args=parseArgumentList(pn);
1066 MethodInvokeNode min=new MethodInvokeNode(nd);
1067 min.setNumLine(pn.getLine());
1068 for(int i=0; i<args.size(); i++) {
1069 min.addArgument((ExpressionNode)args.get(i));
1072 } else if (isNode(pn,"methodinvoke2")) {
1073 String methodid=pn.getChild("id").getTerminal();
1074 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
1075 Vector args=parseArgumentList(pn);
1076 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
1077 min.setNumLine(pn.getLine());
1078 for(int i=0; i<args.size(); i++) {
1079 min.addArgument((ExpressionNode)args.get(i));
1082 } else if (isNode(pn,"fieldaccess")) {
1083 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1084 String fieldname=pn.getChild("field").getTerminal();
1086 FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1087 fan.setNumLine(pn.getLine());
1089 } else if (isNode(pn,"arrayaccess")) {
1090 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1091 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
1092 ArrayAccessNode aan=new ArrayAccessNode(en,index);
1093 aan.setNumLine(pn.getLine());
1095 } else if (isNode(pn,"cast1")) {
1097 CastNode cn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
1098 cn.setNumLine(pn.getLine());
1100 } catch (Exception e) {
1101 System.out.println(pn.PPrint(1,true));
1102 e.printStackTrace();
1105 } else if (isNode(pn,"cast2")) {
1106 CastNode cn=new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
1107 cn.setNumLine(pn.getLine());
1109 } else if (isNode(pn, "getoffset")) {
1110 TypeDescriptor td=parseTypeDescriptor(pn);
1111 String fieldname = pn.getChild("field").getTerminal();
1112 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
1113 return new OffsetNode(td, fieldname);
1114 } else if (isNode(pn, "tert")) {
1116 TertiaryNode tn=new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
1117 parseExpression(pn.getChild("trueexpr").getFirstChild()),
1118 parseExpression(pn.getChild("falseexpr").getFirstChild()) );
1119 tn.setNumLine(pn.getLine());
1122 } else if (isNode(pn, "instanceof")) {
1123 ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
1124 TypeDescriptor t=parseTypeDescriptor(pn);
1125 InstanceOfNode ion=new InstanceOfNode(exp,t);
1126 ion.setNumLine(pn.getLine());
1128 } else if (isNode(pn, "array_initializer")) {
1129 Vector initializers=parseVariableInitializerList(pn);
1130 return new ArrayInitializerNode(initializers);
1131 } else if (isNode(pn, "class_type")) {
1132 TypeDescriptor td=parseTypeDescriptor(pn);
1133 ClassTypeNode ctn=new ClassTypeNode(td);
1134 ctn.setNumLine(pn.getLine());
1136 } else if (isNode(pn, "empty")) {
1139 System.out.println("---------------------");
1140 System.out.println(pn.PPrint(3,true));
1145 private Vector parseDimExprs(ParseNode pn) {
1146 Vector arglist=new Vector();
1147 ParseNode an=pn.getChild("dim_exprs");
1148 if (an==null) /* No argument list */
1150 ParseNodeVector anv=an.getChildren();
1151 for(int i=0; i<anv.size(); i++) {
1152 arglist.add(parseExpression(anv.elementAt(i)));
1157 private Vector parseArgumentList(ParseNode pn) {
1158 Vector arglist=new Vector();
1159 ParseNode an=pn.getChild("argument_list");
1160 if (an==null) /* No argument list */
1162 ParseNodeVector anv=an.getChildren();
1163 for(int i=0; i<anv.size(); i++) {
1164 arglist.add(parseExpression(anv.elementAt(i)));
1169 private Vector[] parseConsArgumentList(ParseNode pn) {
1170 Vector arglist=new Vector();
1171 Vector varlist=new Vector();
1172 ParseNode an=pn.getChild("cons_argument_list");
1173 if (an==null) /* No argument list */
1174 return new Vector[] {varlist, arglist};
1175 ParseNodeVector anv=an.getChildren();
1176 for(int i=0; i<anv.size(); i++) {
1177 ParseNode cpn=anv.elementAt(i);
1178 ParseNode var=cpn.getChild("var");
1179 ParseNode exp=cpn.getChild("exp").getFirstChild();
1180 varlist.add(var.getTerminal());
1181 arglist.add(parseExpression(exp));
1183 return new Vector[] {varlist, arglist};
1186 private Vector parseVariableInitializerList(ParseNode pn) {
1187 Vector varInitList=new Vector();
1188 ParseNode vin=pn.getChild("var_init_list");
1189 if (vin==null) /* No argument list */
1191 ParseNodeVector vinv=vin.getChildren();
1192 for(int i=0; i<vinv.size(); i++) {
1193 varInitList.add(parseExpression(vinv.elementAt(i)));
1198 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
1199 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1200 ParseNodeVector pnv=pn.getChild("args").getChildren();
1202 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
1203 an.setNumLine(pn.getLine());
1208 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1209 ParseNode headern=pn.getChild("method_header");
1210 ParseNode bodyn=pn.getChild("body");
1211 MethodDescriptor md=parseMethodHeader(headern);
1213 BlockNode bn=parseBlock(bodyn);
1214 bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1216 state.addTreeCode(md,bn);
1218 // this is a hack for investigating new language features
1219 // at the AST level, someday should evolve into a nice compiler
1221 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
1222 // md.getSymbol().equals( ***put your method in here like: "main" )
1224 // bn.setStyle( BlockNode.NORMAL );
1225 // System.out.println( bn.printNode( 0 ) );
1228 } catch (Exception e) {
1229 System.out.println("Error with method:"+md.getSymbol());
1230 e.printStackTrace();
1233 System.out.println("Error with method:"+md.getSymbol());
1234 e.printStackTrace();
1239 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1240 ParseNode mn=pn.getChild("modifiers");
1241 Modifiers m=parseModifiersList(mn);
1242 ParseNode cdecl=pn.getChild("constructor_declarator");
1243 boolean isglobal=cdecl.getChild("global")!=null;
1244 String name=resolveName(cn.getSymbol());
1245 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1246 ParseNode paramnode=cdecl.getChild("parameters");
1247 parseParameterList(md,paramnode);
1248 ParseNode bodyn0=pn.getChild("body");
1249 ParseNode bodyn=bodyn0.getChild("constructor_body");
1252 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1253 bn=parseBlock(bodyn);
1256 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1257 ParseNode sin=bodyn.getChild("superinvoke");
1258 NameDescriptor nd=new NameDescriptor("super");
1259 Vector args=parseArgumentList(sin);
1260 MethodInvokeNode min=new MethodInvokeNode(nd);
1261 min.setNumLine(sin.getLine());
1262 for(int i=0; i<args.size(); i++) {
1263 min.addArgument((ExpressionNode)args.get(i));
1265 BlockExpressionNode ben=new BlockExpressionNode(min);
1266 bn.addFirstBlockStatement(ben);
1268 } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1269 ParseNode eci=bodyn.getChild("explconstrinv");
1270 NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1271 Vector args=parseArgumentList(eci);
1272 MethodInvokeNode min=new MethodInvokeNode(nd);
1273 min.setNumLine(eci.getLine());
1274 for(int i=0; i<args.size(); i++) {
1275 min.addArgument((ExpressionNode)args.get(i));
1277 BlockExpressionNode ben=new BlockExpressionNode(min);
1278 ben.setNumLine(eci.getLine());
1279 bn.addFirstBlockStatement(ben);
1281 state.addTreeCode(md,bn);
1284 private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1285 // Each class maintains one MethodDecscriptor which combines all its
1286 // static blocks in their declaration order
1287 boolean isfirst = false;
1288 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1290 // the first static block for this class
1291 Modifiers m_i=new Modifiers();
1292 m_i.addModifier(Modifiers.STATIC);
1293 md = new MethodDescriptor(m_i, "staticblocks", false);
1294 md.setAsStaticBlock();
1297 ParseNode bodyn=pn.getChild("body");
1301 cn.incStaticBlocks();
1303 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1304 bn=parseBlock(bodyn);
1308 state.addTreeCode(md,bn);
1310 BlockNode obn = state.getMethodBody(md);
1311 for(int i = 0; i < bn.size(); i++) {
1312 BlockStatementNode bsn = bn.get(i);
1313 obn.addBlockStatement(bsn);
1315 state.addTreeCode(md, obn);
1320 public BlockNode parseBlock(ParseNode pn) {
1321 this.m_taskexitnum = 0;
1322 if (pn==null||isEmpty(pn.getTerminal()))
1323 return new BlockNode();
1324 ParseNode bsn=pn.getChild("block_statement_list");
1325 return parseBlockHelper(bsn);
1328 private BlockNode parseBlockHelper(ParseNode pn) {
1329 ParseNodeVector pnv=pn.getChildren();
1330 BlockNode bn=new BlockNode();
1331 for(int i=0; i<pnv.size(); i++) {
1332 Vector bsv=parseBlockStatement(pnv.elementAt(i));
1333 for(int j=0; j<bsv.size(); j++) {
1334 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1340 public BlockNode parseSingleBlock(ParseNode pn) {
1341 BlockNode bn=new BlockNode();
1342 Vector bsv=parseBlockStatement(pn);
1343 for(int j=0; j<bsv.size(); j++) {
1344 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1346 bn.setStyle(BlockNode.NOBRACES);
1350 public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
1351 ParseNodeVector pnv=pn.getChildren();
1352 Vector bv=new Vector();
1353 for(int i=0; i<pnv.size(); i++) {
1354 bv.addAll(parseBlockStatement(pnv.elementAt(i)));
1359 public Vector parseBlockStatement(ParseNode pn) {
1360 Vector blockstatements=new Vector();
1361 if (isNode(pn,"tag_declaration")) {
1362 String name=pn.getChild("single").getTerminal();
1363 String type=pn.getChild("type").getTerminal();
1365 TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1366 tdn.setNumLine(pn.getLine());
1368 blockstatements.add(tdn);
1369 } else if (isNode(pn,"local_variable_declaration")) {
1371 TypeDescriptor t=parseTypeDescriptor(pn);
1372 ParseNode vn=pn.getChild("variable_declarators_list");
1373 ParseNodeVector pnv=vn.getChildren();
1374 for(int i=0; i<pnv.size(); i++) {
1375 ParseNode vardecl=pnv.elementAt(i);
1378 ParseNode tmp=vardecl;
1379 TypeDescriptor arrayt=t;
1381 while (tmp.getChild("single")==null) {
1382 arrayt=arrayt.makeArray(state);
1383 tmp=tmp.getChild("array");
1385 String identifier=tmp.getChild("single").getTerminal();
1387 ParseNode epn=vardecl.getChild("initializer");
1390 ExpressionNode en=null;
1392 en=parseExpression(epn.getFirstChild());
1394 DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1395 dn.setNumLine(tmp.getLine());
1397 ParseNode mn=pn.getChild("modifiers");
1399 // here, modifers parse node has the list of annotations
1400 Modifiers m=parseModifiersList(mn);
1401 assignAnnotationsToType(m, arrayt);
1404 blockstatements.add(dn);
1406 } else if (isNode(pn,"nop")) {
1408 } else if (isNode(pn,"expression")) {
1409 BlockExpressionNode ben=new BlockExpressionNode(parseExpression(pn.getFirstChild()));
1410 ben.setNumLine(pn.getLine());
1411 blockstatements.add(ben);
1412 } else if (isNode(pn,"ifstatement")) {
1413 IfStatementNode isn=new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1414 parseSingleBlock(pn.getChild("statement").getFirstChild()),
1415 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null);
1416 isn.setNumLine(pn.getLine());
1418 blockstatements.add(isn);
1419 } else if (isNode(pn,"switch_statement")) {
1420 // TODO add version for normal Java later
1421 SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1422 parseSingleBlock(pn.getChild("statement").getFirstChild()));
1423 ssn.setNumLine(pn.getLine());
1424 blockstatements.add(ssn);
1425 } else if (isNode(pn,"switch_block_list")) {
1426 // TODO add version for normal Java later
1427 ParseNodeVector pnv=pn.getChildren();
1428 for(int i=0; i<pnv.size(); i++) {
1429 ParseNode sblockdecl=pnv.elementAt(i);
1431 if(isNode(sblockdecl, "switch_block")) {
1432 ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1433 ParseNodeVector labelv=lpn.getChildren();
1434 Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1435 for(int j=0; j<labelv.size(); j++) {
1436 ParseNode labeldecl=labelv.elementAt(j);
1437 if(isNode(labeldecl, "switch_label")) {
1438 SwitchLabelNode sln=new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false);
1439 sln.setNumLine(labeldecl.getLine());
1440 slv.addElement(sln);
1441 } else if(isNode(labeldecl, "default_switch_label")) {
1442 SwitchLabelNode sln=new SwitchLabelNode(null, true);
1443 sln.setNumLine(labeldecl.getLine());
1444 slv.addElement(sln);
1448 SwitchBlockNode sbn=new SwitchBlockNode(slv,
1449 parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild()));
1450 sbn.setNumLine(sblockdecl.getLine());
1452 blockstatements.add(sbn);
1456 } else if (isNode(pn, "trycatchstatement")) {
1457 // TODO add version for normal Java later
1458 // Do not fully support exceptions now. Only make sure that if there are no
1459 // exceptions thrown, the execution is right
1460 ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1461 BlockNode bn=parseBlockHelper(tpn);
1462 blockstatements.add(new SubBlockNode(bn));
1464 ParseNode fbk = pn.getChild("finallyblock");
1466 ParseNode fpn = fbk.getFirstChild();
1467 BlockNode fbn=parseBlockHelper(fpn);
1468 blockstatements.add(new SubBlockNode(fbn));
1470 } else if (isNode(pn, "throwstatement")) {
1471 // TODO Simply return here
1472 //blockstatements.add(new ReturnNode());
1473 } else if (isNode(pn,"taskexit")) {
1475 if (pn.getChild("flag_effects_list")!=null)
1476 vfe=parseFlags(pn.getChild("flag_effects_list"));
1478 if (pn.getChild("cons_checks")!=null)
1479 ccs=parseChecks(pn.getChild("cons_checks"));
1480 TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1481 ten.setNumLine(pn.getLine());
1482 blockstatements.add(ten);
1483 } else if (isNode(pn,"atomic")) {
1484 BlockNode bn=parseBlockHelper(pn);
1485 AtomicNode an=new AtomicNode(bn);
1486 an.setNumLine(pn.getLine());
1487 blockstatements.add(an);
1488 } else if (isNode(pn,"synchronized")) {
1489 BlockNode bn=parseBlockHelper(pn.getChild("block"));
1490 ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
1491 SynchronizedNode sn=new SynchronizedNode(en, bn);
1492 sn.setNumLine(pn.getLine());
1493 blockstatements.add(sn);
1494 } else if (isNode(pn,"return")) {
1495 if (isEmpty(pn.getTerminal()))
1496 blockstatements.add(new ReturnNode());
1498 ExpressionNode en=parseExpression(pn.getFirstChild());
1499 ReturnNode rn=new ReturnNode(en);
1500 rn.setNumLine(pn.getLine());
1501 blockstatements.add(rn);
1503 } else if (isNode(pn,"block_statement_list")) {
1504 BlockNode bn=parseBlockHelper(pn);
1505 blockstatements.add(new SubBlockNode(bn));
1506 } else if (isNode(pn,"empty")) {
1508 } else if (isNode(pn,"statement_expression_list")) {
1509 ParseNodeVector pnv=pn.getChildren();
1510 BlockNode bn=new BlockNode();
1511 for(int i=0; i<pnv.size(); i++) {
1512 ExpressionNode en=parseExpression(pnv.elementAt(i));
1513 blockstatements.add(new BlockExpressionNode(en));
1515 bn.setStyle(BlockNode.EXPRLIST);
1516 } else if (isNode(pn,"forstatement")) {
1517 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
1518 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
1519 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1520 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1521 if(condition == null) {
1522 // no condition clause, make a 'true' expression as the condition
1523 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1525 LoopNode ln=new LoopNode(init,condition,update,body);
1526 ln.setNumLine(pn.getLine());
1527 blockstatements.add(ln);
1528 } else if (isNode(pn,"whilestatement")) {
1529 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1530 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1531 if(condition == null) {
1532 // no condition clause, make a 'true' expression as the condition
1533 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1535 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
1536 } else if (isNode(pn,"dowhilestatement")) {
1537 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1538 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1539 if(condition == null) {
1540 // no condition clause, make a 'true' expression as the condition
1541 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1543 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
1544 } else if (isNode(pn,"sese")) {
1545 ParseNode pnID=pn.getChild("identifier");
1547 if( pnID != null ) {
1548 stID=pnID.getFirstChild().getTerminal();
1550 SESENode start=new SESENode(stID);
1551 start.setNumLine(pn.getLine());
1552 SESENode end =new SESENode(stID);
1554 end.setStart(start);
1555 blockstatements.add(start);
1556 blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1557 blockstatements.add(end);
1558 } else if (isNode(pn,"continue")) {
1559 ContinueBreakNode cbn=new ContinueBreakNode(false);
1560 cbn.setNumLine(pn.getLine());
1561 blockstatements.add(cbn);
1562 } else if (isNode(pn,"break")) {
1563 ContinueBreakNode cbn=new ContinueBreakNode(true);
1564 cbn.setNumLine(pn.getLine());
1565 blockstatements.add(cbn);
1566 ParseNode idopt_pn=pn.getChild("identifier_opt");
1567 ParseNode name_pn=idopt_pn.getChild("name");
1568 // name_pn.getTerminal() gives you the label
1569 } else if (isNode(pn,"genreach")) {
1570 String graphName = pn.getChild("graphName").getTerminal();
1571 blockstatements.add(new GenReachNode(graphName) );
1573 } else if(isNode(pn,"labeledstatement")) {
1574 String label = pn.getChild("name").getTerminal();
1575 BlockNode bn=parseSingleBlock(pn.getChild("statement").getFirstChild());
1577 blockstatements.add(new SubBlockNode(bn));
1579 System.out.println("---------------");
1580 System.out.println(pn.PPrint(3,true));
1583 return blockstatements;
1586 public MethodDescriptor parseMethodHeader(ParseNode pn) {
1587 ParseNode mn=pn.getChild("modifiers");
1588 Modifiers m=parseModifiersList(mn);
1590 ParseNode tn=pn.getChild("returntype");
1591 TypeDescriptor returntype;
1593 returntype=parseTypeDescriptor(tn);
1595 returntype=new TypeDescriptor(TypeDescriptor.VOID);
1597 ParseNode pmd=pn.getChild("method_declarator");
1598 String name=pmd.getChild("name").getTerminal();
1599 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1601 ParseNode paramnode=pmd.getChild("parameters");
1602 parseParameterList(md,paramnode);
1606 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1607 ParseNode paramlist=pn.getChild("formal_parameter_list");
1608 if (paramlist==null)
1610 ParseNodeVector pnv=paramlist.getChildren();
1611 for(int i=0; i<pnv.size(); i++) {
1612 ParseNode paramn=pnv.elementAt(i);
1614 if (isNode(paramn, "tag_parameter")) {
1615 String paramname=paramn.getChild("single").getTerminal();
1616 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1617 md.addTagParameter(type, paramname);
1620 TypeDescriptor type=parseTypeDescriptor(paramn);
1622 ParseNode tmp=paramn;
1623 while (tmp.getChild("single")==null) {
1624 type=type.makeArray(state);
1625 tmp=tmp.getChild("array");
1627 String paramname=tmp.getChild("single").getTerminal();
1629 md.addParameter(type, paramname);
1630 if(isNode(paramn, "annotation_parameter")) {
1631 ParseNode bodynode=paramn.getChild("annotation_body");
1632 parseParameterAnnotation(bodynode,type);
1639 public Modifiers parseModifiersList(ParseNode pn) {
1640 Modifiers m=new Modifiers();
1641 ParseNode modlist=pn.getChild("modifier_list");
1642 if (modlist!=null) {
1643 ParseNodeVector pnv=modlist.getChildren();
1644 for(int i=0; i<pnv.size(); i++) {
1645 ParseNode modn=pnv.elementAt(i);
1646 if (isNode(modn,"public"))
1647 m.addModifier(Modifiers.PUBLIC);
1648 else if (isNode(modn,"protected"))
1649 m.addModifier(Modifiers.PROTECTED);
1650 else if (isNode(modn,"private"))
1651 m.addModifier(Modifiers.PRIVATE);
1652 else if (isNode(modn,"static"))
1653 m.addModifier(Modifiers.STATIC);
1654 else if (isNode(modn,"final"))
1655 m.addModifier(Modifiers.FINAL);
1656 else if (isNode(modn,"native"))
1657 m.addModifier(Modifiers.NATIVE);
1658 else if (isNode(modn,"synchronized"))
1659 m.addModifier(Modifiers.SYNCHRONIZED);
1660 else if (isNode(modn,"atomic"))
1661 m.addModifier(Modifiers.ATOMIC);
1662 else if (isNode(modn,"abstract"))
1663 m.addModifier(Modifiers.ABSTRACT);
1664 else if (isNode(modn,"volatile"))
1665 m.addModifier(Modifiers.VOLATILE);
1666 else if (isNode(modn,"transient"))
1667 m.addModifier(Modifiers.TRANSIENT);
1668 else if(isNode(modn,"annotation_list"))
1669 parseAnnotationList(modn,m);
1671 throw new Error("Unrecognized Modifier:"+modn.getLabel());
1678 private void parseAnnotationList(ParseNode pn, Modifiers m) {
1679 ParseNodeVector pnv = pn.getChildren();
1680 for (int i = 0; i < pnv.size(); i++) {
1681 ParseNode body_list = pnv.elementAt(i);
1682 if (isNode(body_list, "annotation_body")) {
1683 ParseNode body_node = body_list.getFirstChild();
1684 if (isNode(body_node, "marker_annotation")) {
1685 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1686 } else if (isNode(body_node, "single_annotation")) {
1687 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1688 body_node.getChild("element_value").getTerminal()));
1689 } else if (isNode(body_node, "normal_annotation")) {
1690 throw new Error("Annotation with multiple data members is not supported yet.");
1696 private void parseParameterAnnotation(ParseNode body_list,TypeDescriptor type) {
1697 ParseNode body_node = body_list.getFirstChild();
1698 if (isNode(body_node, "marker_annotation")) {
1699 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1700 } else if (isNode(body_node, "single_annotation")) {
1701 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1702 body_node.getChild("element_value").getTerminal()));
1703 } else if (isNode(body_node, "normal_annotation")) {
1704 throw new Error("Annotation with multiple data members is not supported yet.");
1708 private boolean isNode(ParseNode pn, String label) {
1709 if (pn.getLabel().equals(label))
1714 private static boolean isEmpty(ParseNode pn) {
1715 if (pn.getLabel().equals("empty"))
1721 private static boolean isEmpty(String s) {
1722 if (s.equals("empty"))
1728 /** Throw an exception if something is unexpected */
1729 private void check(ParseNode pn, String label) {
1731 throw new Error(pn+ "IE: Expected '" + label + "', got null");
1733 if (!pn.getLabel().equals(label)) {
1734 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");