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)
603 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
604 ParseNode decls=pn.getChild("class_body_declaration_list");
606 ParseNodeVector pnv=decls.getChildren();
607 for(int i=0; i<pnv.size(); i++) {
608 ParseNode decl=pnv.elementAt(i);
609 if (isNode(decl,"member")) {
610 parseClassMember(cn,decl);
611 } else if (isNode(decl,"constructor")) {
612 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
613 } else if (isNode(decl, "static_block")) {
614 parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
615 } else if (isNode(decl,"block")) {
616 } else throw new Error();
621 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
622 ParseNode fieldnode=pn.getChild("field");
623 if (fieldnode!=null) {
624 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
627 ParseNode methodnode=pn.getChild("method");
628 if (methodnode!=null) {
629 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
632 ParseNode innerclassnode=pn.getChild("inner_class_declaration");
633 if (innerclassnode!=null) {
634 parseInnerClassDecl(cn,innerclassnode);
637 ParseNode innerinterfacenode=pn.getChild("interface_declaration");
638 if (innerinterfacenode!=null) {
639 parseInterfaceDecl(innerinterfacenode, cn);
643 ParseNode enumnode=pn.getChild("enum_declaration");
644 if (enumnode!=null) {
645 ClassDescriptor ecn=parseEnumDecl(cn,enumnode);
648 ParseNode flagnode=pn.getChild("flag");
649 if (flagnode!=null) {
650 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
653 // in case there are empty node
654 ParseNode emptynode=pn.getChild("empty");
655 if(emptynode != null) {
658 System.out.println("Unrecognized node:"+pn.PPrint(2,true));
662 //10/9/2011 changed this function to enable creation of default constructor for inner classes.
663 //the change was refactoring this function with the corresponding version for normal classes. sganapat
664 private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
665 String basename=pn.getChild("name").getTerminal();
666 String classname=cn.getClassName()+"$"+basename;
668 if (cn.getPackage()==null)
669 cn.getSingleImportMappings().put(basename,classname);
671 cn.getSingleImportMappings().put(basename,cn.getPackage()+"."+classname);
673 ClassDescriptor icn=new ClassDescriptor(cn.getPackage(), classname, false);
675 icn.setImports(mandatoryImports, multiimports);
676 icn.setAsInnerClass();
677 icn.setSurroundingClass(cn.getSymbol());
678 icn.setSurrounding(cn);
679 cn.addInnerClass(icn);
681 if (!isEmpty(pn.getChild("super").getTerminal())) {
682 /* parse superclass name */
683 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
684 NameDescriptor nd=parseClassName(snn);
685 icn.setSuper(nd.toString());
687 if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
688 icn.getSymbol().equals(TypeUtil.TagClass)))
689 icn.setSuper(TypeUtil.ObjectClass);
691 // check inherited interfaces
692 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
693 /* parse inherited interface name */
694 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
695 ParseNodeVector pnv=snlist.getChildren();
696 for(int i=0; i<pnv.size(); i++) {
697 ParseNode decl=pnv.elementAt(i);
698 if (isNode(decl,"type")) {
699 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
700 icn.addSuperInterface(nd.toString());
704 icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
706 parseClassBody(icn, pn.getChild("classbody"));
708 boolean hasConstructor = false;
709 for(Iterator method_it=icn.getMethods(); method_it.hasNext(); ) {
710 MethodDescriptor md=(MethodDescriptor)method_it.next();
711 hasConstructor |= md.isConstructor();
713 //sganapat adding change to allow proper construction of inner class objects
714 if((!hasConstructor) && (!icn.isEnum())) {
715 // add a default constructor for this class
716 MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
717 icn.getSymbol(), false);
718 BlockNode bn=new BlockNode();
719 state.addTreeCode(md,bn);
720 md.setDefaultConstructor();
725 if (analyzeset != null)
727 icn.setSourceFileName(currsourcefile);
733 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
734 ParseNode tn=pn.getChild("type");
735 String type_st=tn.getTerminal();
736 if(type_st.equals("byte")) {
737 return state.getTypeDescriptor(TypeDescriptor.BYTE);
738 } else if(type_st.equals("short")) {
739 return state.getTypeDescriptor(TypeDescriptor.SHORT);
740 } else if(type_st.equals("boolean")) {
741 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
742 } else if(type_st.equals("int")) {
743 return state.getTypeDescriptor(TypeDescriptor.INT);
744 } else if(type_st.equals("long")) {
745 return state.getTypeDescriptor(TypeDescriptor.LONG);
746 } else if(type_st.equals("char")) {
747 return state.getTypeDescriptor(TypeDescriptor.CHAR);
748 } else if(type_st.equals("float")) {
749 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
750 } else if(type_st.equals("double")) {
751 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
752 } else if(type_st.equals("class")) {
753 ParseNode nn=tn.getChild("class");
754 return state.getTypeDescriptor(parseClassName(nn.getChild("name")));
755 } else if(type_st.equals("array")) {
756 ParseNode nn=tn.getChild("array");
757 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
758 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
759 for(int i=0; i<numdims.intValue(); i++)
760 td=td.makeArray(state);
763 System.out.println(pn.PPrint(2, true));
768 //Needed to separate out top level call since if a base exists,
769 //we do not want to apply our resolveName function (i.e. deal with imports)
770 //otherwise, if base == null, we do just want to resolve name.
771 private NameDescriptor parseClassName(ParseNode nn) {
772 ParseNode base=nn.getChild("base");
773 ParseNode id=nn.getChild("identifier");
774 String classname = id.getTerminal();
776 return new NameDescriptor(resolveName(classname));
778 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
781 private NameDescriptor parseClassNameRecursive(ParseNode nn) {
782 ParseNode base=nn.getChild("base");
783 ParseNode id=nn.getChild("identifier");
784 String classname = id.getTerminal();
786 return new NameDescriptor(classname);
788 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
791 //This will get the mapping of a terminal class name
792 //to a canonical classname (with imports/package locations in them)
793 private String resolveName(String terminal) {
795 if(mandatoryImports.containsKey(terminal)) {
796 return (String) mandatoryImports.get(terminal);
798 if(multiimports.containsKey(terminal)) {
800 Object o = multiimports.get(terminal);
801 if(o instanceof Error) {
802 throw new Error("Class " + terminal + " is ambiguous. Cause: more than 1 package import contain the same class.");
804 //At this point, if we found a unique class
805 //we can treat it as a single, mandatory import.
806 mandatoryImports.put(terminal, o);
815 //only function difference between this and parseName() is that this
816 //does not look for a import mapping.
817 private NameDescriptor parseName(ParseNode nn) {
818 ParseNode base=nn.getChild("base");
819 ParseNode id=nn.getChild("identifier");
821 return new NameDescriptor(id.getTerminal());
823 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
826 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
827 String name=pn.getChild("name").getTerminal();
828 FlagDescriptor flag=new FlagDescriptor(name);
829 if (pn.getChild("external")!=null)
834 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
835 ParseNode mn=pn.getChild("modifier");
836 Modifiers m=parseModifiersList(mn);
837 if(cn.isInterface()) {
838 // TODO add version for normal Java later
839 // Can only be PUBLIC or STATIC or FINAL
840 if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
841 || (m.isSynchronized())) {
842 throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
844 m.addModifier(Modifiers.PUBLIC);
845 m.addModifier(Modifiers.STATIC);
846 m.addModifier(Modifiers.FINAL);
849 ParseNode tn=pn.getChild("type");
850 TypeDescriptor t=parseTypeDescriptor(tn);
851 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
852 ParseNodeVector pnv=vn.getChildren();
853 boolean isglobal=pn.getChild("global")!=null;
855 for(int i=0; i<pnv.size(); i++) {
856 ParseNode vardecl=pnv.elementAt(i);
857 ParseNode tmp=vardecl;
858 TypeDescriptor arrayt=t;
859 while (tmp.getChild("single")==null) {
860 arrayt=arrayt.makeArray(state);
861 tmp=tmp.getChild("array");
863 String identifier=tmp.getChild("single").getTerminal();
864 ParseNode epn=vardecl.getChild("initializer");
866 ExpressionNode en=null;
868 en=parseExpression(epn.getFirstChild());
869 en.setNumLine(epn.getFirstChild().getLine());
871 // for static field, the initializer should be considered as a
873 boolean isfirst = false;
874 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
876 // the first static block for this class
877 Modifiers m_i=new Modifiers();
878 m_i.addModifier(Modifiers.STATIC);
879 md = new MethodDescriptor(m_i, "staticblocks", false);
880 md.setAsStaticBlock();
886 cn.incStaticBlocks();
887 BlockNode bn=new BlockNode();
888 NameNode nn=new NameNode(new NameDescriptor(identifier));
889 nn.setNumLine(en.getNumLine());
890 AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
891 an.setNumLine(pn.getLine());
892 bn.addBlockStatement(new BlockExpressionNode(an));
894 state.addTreeCode(md,bn);
896 BlockNode obn = state.getMethodBody(md);
897 for(int ii = 0; ii < bn.size(); ii++) {
898 BlockStatementNode bsn = bn.get(ii);
899 obn.addBlockStatement(bsn);
901 state.addTreeCode(md, obn);
908 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
909 assignAnnotationsToType(m,arrayt);
913 private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type) {
914 Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
915 for(int i=0; i<annotations.size(); i++) {
916 // it only supports a marker annotation
917 AnnotationDescriptor an=annotations.elementAt(i);
918 type.addAnnotationMarker(an);
924 private ExpressionNode parseExpression(ParseNode pn) {
925 if (isNode(pn,"assignment"))
926 return parseAssignmentExpression(pn);
927 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
928 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
929 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
930 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
931 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
932 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
933 isNode(pn,"rightshift")||isNode(pn,"sub")||
934 isNode(pn,"urightshift")||isNode(pn,"sub")||
935 isNode(pn,"add")||isNode(pn,"mult")||
936 isNode(pn,"div")||isNode(pn,"mod")) {
937 ParseNodeVector pnv=pn.getChildren();
938 ParseNode left=pnv.elementAt(0);
939 ParseNode right=pnv.elementAt(1);
940 Operation op=new Operation(pn.getLabel());
941 OpNode on=new OpNode(parseExpression(left),parseExpression(right),op);
942 on.setNumLine(pn.getLine());
944 } else if (isNode(pn,"unaryplus")||
945 isNode(pn,"unaryminus")||
948 ParseNode left=pn.getFirstChild();
949 Operation op=new Operation(pn.getLabel());
950 OpNode on=new OpNode(parseExpression(left),op);
951 on.setNumLine(pn.getLine());
953 } else if (isNode(pn,"postinc")||
954 isNode(pn,"postdec")) {
955 ParseNode left=pn.getFirstChild();
956 AssignOperation op=new AssignOperation(pn.getLabel());
957 AssignmentNode an=new AssignmentNode(parseExpression(left),null,op);
958 an.setNumLine(pn.getLine());
961 } else if (isNode(pn,"preinc")||
962 isNode(pn,"predec")) {
963 ParseNode left=pn.getFirstChild();
964 AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
965 AssignmentNode an=new AssignmentNode(parseExpression(left),
966 new LiteralNode("integer",new Integer(1)),op);
967 an.setNumLine(pn.getLine());
969 } else if (isNode(pn,"literal")) {
970 String literaltype=pn.getTerminal();
971 ParseNode literalnode=pn.getChild(literaltype);
972 Object literal_obj=literalnode.getLiteral();
973 LiteralNode ln=new LiteralNode(literaltype, literal_obj);
974 ln.setNumLine(pn.getLine());
976 } else if (isNode(pn, "createobject")) {
977 TypeDescriptor td = parseTypeDescriptor(pn);
979 Vector args = parseArgumentList(pn);
980 boolean isglobal = pn.getChild("global") != null || pn.getChild("scratch") != null;
981 String disjointId = null;
982 if (pn.getChild("disjoint") != null) {
983 disjointId = pn.getChild("disjoint").getTerminal();
985 CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
986 con.setNumLine(pn.getLine());
987 for (int i = 0; i < args.size(); i++) {
988 con.addArgument((ExpressionNode) args.get(i));
990 /* Could have flag set or tag added here */
991 if (pn.getChild("flag_list") != null || pn.getChild("tag_list") != null) {
992 FlagEffects fe = new FlagEffects(null);
993 if (pn.getChild("flag_list") != null)
994 parseFlagEffect(fe, pn.getChild("flag_list"));
996 if (pn.getChild("tag_list") != null)
997 parseTagEffect(fe, pn.getChild("tag_list"));
998 con.addFlagEffects(fe);
1002 } else if (isNode(pn,"createobjectcls")) {
1003 //TODO::: FIX BUG!!! static fields in caller context need to become parameters
1004 //TODO::: caller context need to be passed in here
1005 TypeDescriptor td=parseTypeDescriptor(pn);
1007 ClassDescriptor cnnew=new ClassDescriptor(packageName,td.getSymbol()+"$"+innerCount, false);
1009 cnnew.setImports(mandatoryImports, multiimports);
1010 cnnew.setSuper(td.getSymbol());
1012 parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
1013 TypeDescriptor tdnew=state.getTypeDescriptor(cnnew.getSymbol());
1015 Vector args=parseArgumentList(pn);
1017 CreateObjectNode con=new CreateObjectNode(tdnew, false, null);
1018 con.setNumLine(pn.getLine());
1019 for(int i=0; i<args.size(); i++) {
1020 con.addArgument((ExpressionNode)args.get(i));
1024 if (analyzeset != null)
1025 analyzeset.add(cnnew);
1026 cnnew.setSourceFileName(currsourcefile);
1027 state.addClass(cnnew);
1030 } else if (isNode(pn,"createarray")) {
1031 //System.out.println(pn.PPrint(3,true));
1032 boolean isglobal=pn.getChild("global")!=null||
1033 pn.getChild("scratch")!=null;
1034 String disjointId=null;
1035 if( pn.getChild("disjoint") != null) {
1036 disjointId = pn.getChild("disjoint").getTerminal();
1038 TypeDescriptor td=parseTypeDescriptor(pn);
1039 Vector args=parseDimExprs(pn);
1041 if (pn.getChild("dims_opt").getLiteral()!=null)
1042 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1043 for(int i=0; i<(args.size()+num); i++)
1044 td=td.makeArray(state);
1045 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
1046 con.setNumLine(pn.getLine());
1047 for(int i=0; i<args.size(); i++) {
1048 con.addArgument((ExpressionNode)args.get(i));
1052 if (isNode(pn,"createarray2")) {
1053 TypeDescriptor td=parseTypeDescriptor(pn);
1055 if (pn.getChild("dims_opt").getLiteral()!=null)
1056 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1057 for(int i=0; i<num; i++)
1058 td=td.makeArray(state);
1059 CreateObjectNode con=new CreateObjectNode(td, false, null);
1060 con.setNumLine(pn.getLine());
1061 ParseNode ipn = pn.getChild("initializer");
1062 Vector initializers=parseVariableInitializerList(ipn);
1063 ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
1064 ain.setNumLine(pn.getLine());
1065 con.addArrayInitializer(ain);
1067 } else if (isNode(pn,"name")) {
1068 NameDescriptor nd=parseName(pn);
1069 NameNode nn=new NameNode(nd);
1070 nn.setNumLine(pn.getLine());
1072 } else if (isNode(pn,"this")) {
1073 NameDescriptor nd=new NameDescriptor("this");
1074 NameNode nn=new NameNode(nd);
1075 nn.setNumLine(pn.getLine());
1077 } else if (isNode(pn,"parentclass")) {
1078 NameDescriptor nd=new NameDescriptor("this");
1079 NameNode nn=new NameNode(nd);
1080 nn.setNumLine(pn.getLine());
1082 FieldAccessNode fan=new FieldAccessNode(nn,"this$parent");
1083 fan.setNumLine(pn.getLine());
1085 } else if (isNode(pn,"isavailable")) {
1086 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
1087 NameNode nn=new NameNode(nd);
1088 nn.setNumLine(pn.getLine());
1089 return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
1090 } else if (isNode(pn,"methodinvoke1")) {
1091 NameDescriptor nd=parseName(pn.getChild("name"));
1092 Vector args=parseArgumentList(pn);
1093 MethodInvokeNode min=new MethodInvokeNode(nd);
1094 min.setNumLine(pn.getLine());
1095 for(int i=0; i<args.size(); i++) {
1096 min.addArgument((ExpressionNode)args.get(i));
1099 } else if (isNode(pn,"methodinvoke2")) {
1100 String methodid=pn.getChild("id").getTerminal();
1101 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
1102 Vector args=parseArgumentList(pn);
1103 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
1104 min.setNumLine(pn.getLine());
1105 for(int i=0; i<args.size(); i++) {
1106 min.addArgument((ExpressionNode)args.get(i));
1109 } else if (isNode(pn,"fieldaccess")) {
1110 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1111 String fieldname=pn.getChild("field").getTerminal();
1113 FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1114 fan.setNumLine(pn.getLine());
1116 } else if (isNode(pn,"arrayaccess")) {
1117 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1118 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
1119 ArrayAccessNode aan=new ArrayAccessNode(en,index);
1120 aan.setNumLine(pn.getLine());
1122 } else if (isNode(pn,"cast1")) {
1124 CastNode cn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
1125 cn.setNumLine(pn.getLine());
1127 } catch (Exception e) {
1128 System.out.println(pn.PPrint(1,true));
1129 e.printStackTrace();
1132 } else if (isNode(pn,"cast2")) {
1133 CastNode cn=new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
1134 cn.setNumLine(pn.getLine());
1136 } else if (isNode(pn, "getoffset")) {
1137 TypeDescriptor td=parseTypeDescriptor(pn);
1138 String fieldname = pn.getChild("field").getTerminal();
1139 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
1140 return new OffsetNode(td, fieldname);
1141 } else if (isNode(pn, "tert")) {
1143 TertiaryNode tn=new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
1144 parseExpression(pn.getChild("trueexpr").getFirstChild()),
1145 parseExpression(pn.getChild("falseexpr").getFirstChild()) );
1146 tn.setNumLine(pn.getLine());
1149 } else if (isNode(pn, "instanceof")) {
1150 ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
1151 TypeDescriptor t=parseTypeDescriptor(pn);
1152 InstanceOfNode ion=new InstanceOfNode(exp,t);
1153 ion.setNumLine(pn.getLine());
1155 } else if (isNode(pn, "array_initializer")) {
1156 Vector initializers=parseVariableInitializerList(pn);
1157 return new ArrayInitializerNode(initializers);
1158 } else if (isNode(pn, "class_type")) {
1159 TypeDescriptor td=parseTypeDescriptor(pn);
1160 ClassTypeNode ctn=new ClassTypeNode(td);
1161 ctn.setNumLine(pn.getLine());
1163 } else if (isNode(pn, "empty")) {
1166 System.out.println("---------------------");
1167 System.out.println(pn.PPrint(3,true));
1172 private Vector parseDimExprs(ParseNode pn) {
1173 Vector arglist=new Vector();
1174 ParseNode an=pn.getChild("dim_exprs");
1175 if (an==null) /* No argument list */
1177 ParseNodeVector anv=an.getChildren();
1178 for(int i=0; i<anv.size(); i++) {
1179 arglist.add(parseExpression(anv.elementAt(i)));
1184 private Vector parseArgumentList(ParseNode pn) {
1185 Vector arglist=new Vector();
1186 ParseNode an=pn.getChild("argument_list");
1187 if (an==null) /* No argument list */
1189 ParseNodeVector anv=an.getChildren();
1190 for(int i=0; i<anv.size(); i++) {
1191 arglist.add(parseExpression(anv.elementAt(i)));
1196 private Vector[] parseConsArgumentList(ParseNode pn) {
1197 Vector arglist=new Vector();
1198 Vector varlist=new Vector();
1199 ParseNode an=pn.getChild("cons_argument_list");
1200 if (an==null) /* No argument list */
1201 return new Vector[] {varlist, arglist};
1202 ParseNodeVector anv=an.getChildren();
1203 for(int i=0; i<anv.size(); i++) {
1204 ParseNode cpn=anv.elementAt(i);
1205 ParseNode var=cpn.getChild("var");
1206 ParseNode exp=cpn.getChild("exp").getFirstChild();
1207 varlist.add(var.getTerminal());
1208 arglist.add(parseExpression(exp));
1210 return new Vector[] {varlist, arglist};
1213 private Vector parseVariableInitializerList(ParseNode pn) {
1214 Vector varInitList=new Vector();
1215 ParseNode vin=pn.getChild("var_init_list");
1216 if (vin==null) /* No argument list */
1218 ParseNodeVector vinv=vin.getChildren();
1219 for(int i=0; i<vinv.size(); i++) {
1220 varInitList.add(parseExpression(vinv.elementAt(i)));
1225 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
1226 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1227 ParseNodeVector pnv=pn.getChild("args").getChildren();
1229 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
1230 an.setNumLine(pn.getLine());
1235 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1236 ParseNode headern=pn.getChild("method_header");
1237 ParseNode bodyn=pn.getChild("body");
1238 MethodDescriptor md=parseMethodHeader(headern);
1240 BlockNode bn=parseBlock(bodyn);
1241 bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1243 state.addTreeCode(md,bn);
1245 // this is a hack for investigating new language features
1246 // at the AST level, someday should evolve into a nice compiler
1248 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
1249 // md.getSymbol().equals( ***put your method in here like: "main" )
1251 // bn.setStyle( BlockNode.NORMAL );
1252 // System.out.println( bn.printNode( 0 ) );
1255 } catch (Exception e) {
1256 System.out.println("Error with method:"+md.getSymbol());
1257 e.printStackTrace();
1260 System.out.println("Error with method:"+md.getSymbol());
1261 e.printStackTrace();
1266 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1267 ParseNode mn=pn.getChild("modifiers");
1268 Modifiers m=parseModifiersList(mn);
1269 ParseNode cdecl=pn.getChild("constructor_declarator");
1270 boolean isglobal=cdecl.getChild("global")!=null;
1271 String name=resolveName(cn.getSymbol());
1272 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1273 ParseNode paramnode=cdecl.getChild("parameters");
1274 parseParameterList(md,paramnode);
1275 ParseNode bodyn0=pn.getChild("body");
1276 ParseNode bodyn=bodyn0.getChild("constructor_body");
1279 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1280 bn=parseBlock(bodyn);
1283 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1284 ParseNode sin=bodyn.getChild("superinvoke");
1285 NameDescriptor nd=new NameDescriptor("super");
1286 Vector args=parseArgumentList(sin);
1287 MethodInvokeNode min=new MethodInvokeNode(nd);
1288 min.setNumLine(sin.getLine());
1289 for(int i=0; i<args.size(); i++) {
1290 min.addArgument((ExpressionNode)args.get(i));
1292 BlockExpressionNode ben=new BlockExpressionNode(min);
1293 bn.addFirstBlockStatement(ben);
1295 } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1296 ParseNode eci=bodyn.getChild("explconstrinv");
1297 NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1298 Vector args=parseArgumentList(eci);
1299 MethodInvokeNode min=new MethodInvokeNode(nd);
1300 min.setNumLine(eci.getLine());
1301 for(int i=0; i<args.size(); i++) {
1302 min.addArgument((ExpressionNode)args.get(i));
1304 BlockExpressionNode ben=new BlockExpressionNode(min);
1305 ben.setNumLine(eci.getLine());
1306 bn.addFirstBlockStatement(ben);
1308 state.addTreeCode(md,bn);
1311 private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1312 // Each class maintains one MethodDecscriptor which combines all its
1313 // static blocks in their declaration order
1314 boolean isfirst = false;
1315 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1317 // the first static block for this class
1318 Modifiers m_i=new Modifiers();
1319 m_i.addModifier(Modifiers.STATIC);
1320 md = new MethodDescriptor(m_i, "staticblocks", false);
1321 md.setAsStaticBlock();
1324 ParseNode bodyn=pn.getChild("body");
1328 cn.incStaticBlocks();
1330 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1331 bn=parseBlock(bodyn);
1335 state.addTreeCode(md,bn);
1337 BlockNode obn = state.getMethodBody(md);
1338 for(int i = 0; i < bn.size(); i++) {
1339 BlockStatementNode bsn = bn.get(i);
1340 obn.addBlockStatement(bsn);
1342 state.addTreeCode(md, obn);
1347 public BlockNode parseBlock(ParseNode pn) {
1348 this.m_taskexitnum = 0;
1349 if (pn==null||isEmpty(pn.getTerminal()))
1350 return new BlockNode();
1351 ParseNode bsn=pn.getChild("block_statement_list");
1352 return parseBlockHelper(bsn);
1355 private BlockNode parseBlockHelper(ParseNode pn) {
1356 ParseNodeVector pnv=pn.getChildren();
1357 BlockNode bn=new BlockNode();
1358 for(int i=0; i<pnv.size(); i++) {
1359 Vector bsv=parseBlockStatement(pnv.elementAt(i));
1360 for(int j=0; j<bsv.size(); j++) {
1361 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1367 public BlockNode parseSingleBlock(ParseNode pn, String label){
1368 BlockNode bn=new BlockNode();
1369 Vector bsv=parseBlockStatement(pn,label);
1370 for(int j=0; j<bsv.size(); j++) {
1371 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1373 bn.setStyle(BlockNode.NOBRACES);
1377 public BlockNode parseSingleBlock(ParseNode pn) {
1378 return parseSingleBlock(pn,null);
1381 public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
1382 ParseNodeVector pnv=pn.getChildren();
1383 Vector bv=new Vector();
1384 for(int i=0; i<pnv.size(); i++) {
1385 bv.addAll(parseBlockStatement(pnv.elementAt(i)));
1390 public Vector parseBlockStatement(ParseNode pn){
1391 return parseBlockStatement(pn,null);
1394 public Vector parseBlockStatement(ParseNode pn, String label) {
1395 Vector blockstatements=new Vector();
1396 if (isNode(pn,"tag_declaration")) {
1397 String name=pn.getChild("single").getTerminal();
1398 String type=pn.getChild("type").getTerminal();
1400 TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1401 tdn.setNumLine(pn.getLine());
1403 blockstatements.add(tdn);
1404 } else if (isNode(pn,"local_variable_declaration")) {
1406 TypeDescriptor t=parseTypeDescriptor(pn);
1407 ParseNode vn=pn.getChild("variable_declarators_list");
1408 ParseNodeVector pnv=vn.getChildren();
1409 for(int i=0; i<pnv.size(); i++) {
1410 ParseNode vardecl=pnv.elementAt(i);
1413 ParseNode tmp=vardecl;
1414 TypeDescriptor arrayt=t;
1416 while (tmp.getChild("single")==null) {
1417 arrayt=arrayt.makeArray(state);
1418 tmp=tmp.getChild("array");
1420 String identifier=tmp.getChild("single").getTerminal();
1422 ParseNode epn=vardecl.getChild("initializer");
1425 ExpressionNode en=null;
1427 en=parseExpression(epn.getFirstChild());
1429 DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1430 dn.setNumLine(tmp.getLine());
1432 ParseNode mn=pn.getChild("modifiers");
1434 // here, modifers parse node has the list of annotations
1435 Modifiers m=parseModifiersList(mn);
1436 assignAnnotationsToType(m, arrayt);
1439 blockstatements.add(dn);
1441 } else if (isNode(pn,"nop")) {
1443 } else if (isNode(pn,"expression")) {
1444 BlockExpressionNode ben=new BlockExpressionNode(parseExpression(pn.getFirstChild()));
1445 ben.setNumLine(pn.getLine());
1446 blockstatements.add(ben);
1447 } else if (isNode(pn,"ifstatement")) {
1448 IfStatementNode isn=new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1449 parseSingleBlock(pn.getChild("statement").getFirstChild()),
1450 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null);
1451 isn.setNumLine(pn.getLine());
1453 blockstatements.add(isn);
1454 } else if (isNode(pn,"switch_statement")) {
1455 // TODO add version for normal Java later
1456 SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1457 parseSingleBlock(pn.getChild("statement").getFirstChild()));
1458 ssn.setNumLine(pn.getLine());
1459 blockstatements.add(ssn);
1460 } else if (isNode(pn,"switch_block_list")) {
1461 // TODO add version for normal Java later
1462 ParseNodeVector pnv=pn.getChildren();
1463 for(int i=0; i<pnv.size(); i++) {
1464 ParseNode sblockdecl=pnv.elementAt(i);
1466 if(isNode(sblockdecl, "switch_block")) {
1467 ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1468 ParseNodeVector labelv=lpn.getChildren();
1469 Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1470 for(int j=0; j<labelv.size(); j++) {
1471 ParseNode labeldecl=labelv.elementAt(j);
1472 if(isNode(labeldecl, "switch_label")) {
1473 SwitchLabelNode sln=new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false);
1474 sln.setNumLine(labeldecl.getLine());
1475 slv.addElement(sln);
1476 } else if(isNode(labeldecl, "default_switch_label")) {
1477 SwitchLabelNode sln=new SwitchLabelNode(null, true);
1478 sln.setNumLine(labeldecl.getLine());
1479 slv.addElement(sln);
1483 SwitchBlockNode sbn=new SwitchBlockNode(slv,
1484 parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild()));
1485 sbn.setNumLine(sblockdecl.getLine());
1487 blockstatements.add(sbn);
1491 } else if (isNode(pn, "trycatchstatement")) {
1492 // TODO add version for normal Java later
1493 // Do not fully support exceptions now. Only make sure that if there are no
1494 // exceptions thrown, the execution is right
1495 ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1496 BlockNode bn=parseBlockHelper(tpn);
1497 blockstatements.add(new SubBlockNode(bn));
1499 ParseNode fbk = pn.getChild("finallyblock");
1501 ParseNode fpn = fbk.getFirstChild();
1502 BlockNode fbn=parseBlockHelper(fpn);
1503 blockstatements.add(new SubBlockNode(fbn));
1505 } else if (isNode(pn, "throwstatement")) {
1506 // TODO Simply return here
1507 //blockstatements.add(new ReturnNode());
1508 } else if (isNode(pn,"taskexit")) {
1510 if (pn.getChild("flag_effects_list")!=null)
1511 vfe=parseFlags(pn.getChild("flag_effects_list"));
1513 if (pn.getChild("cons_checks")!=null)
1514 ccs=parseChecks(pn.getChild("cons_checks"));
1515 TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1516 ten.setNumLine(pn.getLine());
1517 blockstatements.add(ten);
1518 } else if (isNode(pn,"atomic")) {
1519 BlockNode bn=parseBlockHelper(pn);
1520 AtomicNode an=new AtomicNode(bn);
1521 an.setNumLine(pn.getLine());
1522 blockstatements.add(an);
1523 } else if (isNode(pn,"synchronized")) {
1524 BlockNode bn=parseBlockHelper(pn.getChild("block"));
1525 ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
1526 SynchronizedNode sn=new SynchronizedNode(en, bn);
1527 sn.setNumLine(pn.getLine());
1528 blockstatements.add(sn);
1529 } else if (isNode(pn,"return")) {
1530 if (isEmpty(pn.getTerminal()))
1531 blockstatements.add(new ReturnNode());
1533 ExpressionNode en=parseExpression(pn.getFirstChild());
1534 ReturnNode rn=new ReturnNode(en);
1535 rn.setNumLine(pn.getLine());
1536 blockstatements.add(rn);
1538 } else if (isNode(pn,"block_statement_list")) {
1539 BlockNode bn=parseBlockHelper(pn);
1540 blockstatements.add(new SubBlockNode(bn));
1541 } else if (isNode(pn,"empty")) {
1543 } else if (isNode(pn,"statement_expression_list")) {
1544 ParseNodeVector pnv=pn.getChildren();
1545 BlockNode bn=new BlockNode();
1546 for(int i=0; i<pnv.size(); i++) {
1547 ExpressionNode en=parseExpression(pnv.elementAt(i));
1548 blockstatements.add(new BlockExpressionNode(en));
1550 bn.setStyle(BlockNode.EXPRLIST);
1551 } else if (isNode(pn,"forstatement")) {
1552 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
1553 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
1554 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1555 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1556 if(condition == null) {
1557 // no condition clause, make a 'true' expression as the condition
1558 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1560 LoopNode ln=new LoopNode(init,condition,update,body,label);
1561 ln.setNumLine(pn.getLine());
1562 blockstatements.add(ln);
1563 } else if (isNode(pn,"whilestatement")) {
1564 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1565 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1566 if(condition == null) {
1567 // no condition clause, make a 'true' expression as the condition
1568 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1570 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP,label));
1571 } else if (isNode(pn,"dowhilestatement")) {
1572 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1573 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1574 if(condition == null) {
1575 // no condition clause, make a 'true' expression as the condition
1576 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1578 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP,label));
1579 } else if (isNode(pn,"sese")) {
1580 ParseNode pnID=pn.getChild("identifier");
1582 if( pnID != null ) {
1583 stID=pnID.getFirstChild().getTerminal();
1585 SESENode start=new SESENode(stID);
1586 start.setNumLine(pn.getLine());
1587 SESENode end =new SESENode(stID);
1589 end.setStart(start);
1590 blockstatements.add(start);
1591 blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1592 blockstatements.add(end);
1593 } else if (isNode(pn,"continue")) {
1594 ContinueBreakNode cbn=new ContinueBreakNode(false);
1595 cbn.setNumLine(pn.getLine());
1596 blockstatements.add(cbn);
1597 } else if (isNode(pn,"break")) {
1598 ContinueBreakNode cbn=new ContinueBreakNode(true);
1599 cbn.setNumLine(pn.getLine());
1600 blockstatements.add(cbn);
1601 ParseNode idopt_pn=pn.getChild("identifier_opt");
1602 ParseNode name_pn=idopt_pn.getChild("name");
1603 // name_pn.getTerminal() gives you the label
1605 } else if (isNode(pn,"genreach")) {
1606 String graphName = pn.getChild("graphName").getTerminal();
1607 blockstatements.add(new GenReachNode(graphName) );
1609 } else if (isNode(pn,"gen_def_reach")) {
1610 String outputName = pn.getChild("outputName").getTerminal();
1611 blockstatements.add(new GenDefReachNode(outputName) );
1613 } else if(isNode(pn,"labeledstatement")) {
1614 String labeledstatement = pn.getChild("name").getTerminal();
1615 BlockNode bn=parseSingleBlock(pn.getChild("statement").getFirstChild(),labeledstatement);
1616 blockstatements.add(new SubBlockNode(bn));
1618 System.out.println("---------------");
1619 System.out.println(pn.PPrint(3,true));
1622 return blockstatements;
1625 public MethodDescriptor parseMethodHeader(ParseNode pn) {
1626 ParseNode mn=pn.getChild("modifiers");
1627 Modifiers m=parseModifiersList(mn);
1629 ParseNode tn=pn.getChild("returntype");
1630 TypeDescriptor returntype;
1632 returntype=parseTypeDescriptor(tn);
1634 returntype=new TypeDescriptor(TypeDescriptor.VOID);
1636 ParseNode pmd=pn.getChild("method_declarator");
1637 String name=pmd.getChild("name").getTerminal();
1638 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1640 ParseNode paramnode=pmd.getChild("parameters");
1641 parseParameterList(md,paramnode);
1645 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1646 ParseNode paramlist=pn.getChild("formal_parameter_list");
1647 if (paramlist==null)
1649 ParseNodeVector pnv=paramlist.getChildren();
1650 for(int i=0; i<pnv.size(); i++) {
1651 ParseNode paramn=pnv.elementAt(i);
1653 if (isNode(paramn, "tag_parameter")) {
1654 String paramname=paramn.getChild("single").getTerminal();
1655 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1656 md.addTagParameter(type, paramname);
1659 TypeDescriptor type=parseTypeDescriptor(paramn);
1661 ParseNode tmp=paramn;
1662 while (tmp.getChild("single")==null) {
1663 type=type.makeArray(state);
1664 tmp=tmp.getChild("array");
1666 String paramname=tmp.getChild("single").getTerminal();
1668 md.addParameter(type, paramname);
1669 if(isNode(paramn, "annotation_parameter")) {
1670 ParseNode listnode=paramn.getChild("annotation_list");
1671 parseParameterAnnotation(listnode,type);
1678 public Modifiers parseModifiersList(ParseNode pn) {
1679 Modifiers m=new Modifiers();
1680 ParseNode modlist=pn.getChild("modifier_list");
1681 if (modlist!=null) {
1682 ParseNodeVector pnv=modlist.getChildren();
1683 for(int i=0; i<pnv.size(); i++) {
1684 ParseNode modn=pnv.elementAt(i);
1685 if (isNode(modn,"public"))
1686 m.addModifier(Modifiers.PUBLIC);
1687 else if (isNode(modn,"protected"))
1688 m.addModifier(Modifiers.PROTECTED);
1689 else if (isNode(modn,"private"))
1690 m.addModifier(Modifiers.PRIVATE);
1691 else if (isNode(modn,"static"))
1692 m.addModifier(Modifiers.STATIC);
1693 else if (isNode(modn,"final"))
1694 m.addModifier(Modifiers.FINAL);
1695 else if (isNode(modn,"native"))
1696 m.addModifier(Modifiers.NATIVE);
1697 else if (isNode(modn,"synchronized"))
1698 m.addModifier(Modifiers.SYNCHRONIZED);
1699 else if (isNode(modn,"atomic"))
1700 m.addModifier(Modifiers.ATOMIC);
1701 else if (isNode(modn,"abstract"))
1702 m.addModifier(Modifiers.ABSTRACT);
1703 else if (isNode(modn,"volatile"))
1704 m.addModifier(Modifiers.VOLATILE);
1705 else if (isNode(modn,"transient"))
1706 m.addModifier(Modifiers.TRANSIENT);
1707 else if(isNode(modn,"annotation_list"))
1708 parseAnnotationList(modn,m);
1710 throw new Error("Unrecognized Modifier:"+modn.getLabel());
1717 private void parseAnnotationList(ParseNode pn, Modifiers m) {
1718 ParseNodeVector pnv = pn.getChildren();
1719 for (int i = 0; i < pnv.size(); i++) {
1720 ParseNode body_list = pnv.elementAt(i);
1721 if (isNode(body_list, "annotation_body")) {
1722 ParseNode body_node = body_list.getFirstChild();
1723 if (isNode(body_node, "marker_annotation")) {
1724 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1725 } else if (isNode(body_node, "single_annotation")) {
1726 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1727 body_node.getChild("element_value").getTerminal()));
1728 } else if (isNode(body_node, "normal_annotation")) {
1729 throw new Error("Annotation with multiple data members is not supported yet.");
1735 private void parseParameterAnnotation(ParseNode pn,TypeDescriptor type) {
1736 ParseNodeVector pnv = pn.getChildren();
1737 for (int i = 0; i < pnv.size(); i++) {
1738 ParseNode body_list = pnv.elementAt(i);
1739 if (isNode(body_list, "annotation_body")) {
1740 ParseNode body_node = body_list.getFirstChild();
1741 if (isNode(body_node, "marker_annotation")) {
1742 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1743 } else if (isNode(body_node, "single_annotation")) {
1744 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1745 body_node.getChild("element_value").getTerminal()));
1746 } else if (isNode(body_node, "normal_annotation")) {
1747 throw new Error("Annotation with multiple data members is not supported yet.");
1753 private boolean isNode(ParseNode pn, String label) {
1754 if (pn.getLabel().equals(label))
1759 private static boolean isEmpty(ParseNode pn) {
1760 if (pn.getLabel().equals("empty"))
1766 private static boolean isEmpty(String s) {
1767 if (s.equals("empty"))
1773 /** Throw an exception if something is unexpected */
1774 private void check(ParseNode pn, String label) {
1776 throw new Error(pn+ "IE: Expected '" + label + "', got null");
1778 if (!pn.getLabel().equals(label)) {
1779 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");