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 Hashtable mandatoryImports;
36 //maps class names in file to full name
37 //Note may map a name to an ERROR.
38 Hashtable multiimports;
39 NameDescriptor packages;
41 /** Parse the classes in this file */
42 public void parseFile(ParseNode pn, Set toanalyze, String sourcefile) {
43 mandatoryImports = new Hashtable();
44 multiimports = new Hashtable();
47 //add java.lang as our default multi-import
48 this.addMultiImport(sourcefile, "java.lang", false);
51 ParseNode ipn = pn.getChild("imports").getChild("import_decls_list");
52 if ((ipn != null) && !state.MGC) {
53 ParseNodeVector pnv = ipn.getChildren();
54 for (int i = 0; i < pnv.size(); i++) {
55 ParseNode pnimport = pnv.elementAt(i);
56 NameDescriptor nd = parseName(pnimport.getChild("name"));
57 if (isNode(pnimport, "import_single")) {
58 if (!mandatoryImports.containsKey(nd.getIdentifier())) {
59 // map name to full name (includes package/directory
60 mandatoryImports.put(nd.getIdentifier(), nd.getPathFromRootToHere());
62 throw new Error("An ambiguous class "+ nd.getIdentifier() +" has been found. It is included for " +
63 ((String)mandatoryImports.get(nd.getIdentifier())) + " and " +
64 nd.getPathFromRootToHere());
67 addMultiImport(sourcefile, nd.getPathFromRootToHere(), false);
72 ParseNode ppn=pn.getChild("packages").getChild("package");
73 String packageName = null;
74 if ((ppn!=null) && !state.MGC){
75 NameDescriptor nd = parseClassName(ppn.getChild("name"));
76 packageName = nd.getPathFromRootToHere();
77 //Trick -> import the package directory as a multi-import and it'll
78 //automatically recognize files in the same directory.
79 addMultiImport(sourcefile, packageName, true);
82 ParseNode tpn=pn.getChild("type_declaration_list");
84 ParseNodeVector pnv = tpn.getChildren();
85 for (int i = 0; i < pnv.size(); i++) {
86 ParseNode type_pn = pnv.elementAt(i);
87 if (isEmpty(type_pn)) /* Skip the semicolon */
89 if (isNode(type_pn, "class_declaration")) {
90 ClassDescriptor cn = parseTypeDecl(type_pn, packageName);
91 cn.setSourceFileName(sourcefile);
92 parseInitializers(cn);
93 if (toanalyze != null)
96 // for inner classes/enum
97 HashSet tovisit = new HashSet();
98 Iterator it_icds = cn.getInnerClasses();
99 while (it_icds.hasNext()) {
100 tovisit.add(it_icds.next());
103 while (!tovisit.isEmpty()) {
104 ClassDescriptor cd = (ClassDescriptor) tovisit.iterator().next();
106 parseInitializers(cd);
107 if (toanalyze != null) {
110 cd.setSourceFileName(sourcefile);
113 Iterator it_ics = cd.getInnerClasses();
114 while (it_ics.hasNext()) {
115 tovisit.add(it_ics.next());
118 Iterator it_ienums = cd.getEnum();
119 while (it_ienums.hasNext()) {
120 ClassDescriptor iecd = (ClassDescriptor) it_ienums.next();
121 if (toanalyze != null) {
124 iecd.setSourceFileName(sourcefile);
125 state.addClass(iecd);
129 Iterator it_enums = cn.getEnum();
130 while (it_enums.hasNext()) {
131 ClassDescriptor ecd = (ClassDescriptor) it_enums.next();
132 if (toanalyze != null) {
135 ecd.setSourceFileName(sourcefile);
138 } else if (isNode(type_pn, "task_declaration")) {
139 TaskDescriptor td = parseTaskDecl(type_pn);
140 if (toanalyze != null)
143 } else if (isNode(type_pn, "interface_declaration")) {
144 // TODO add version for normal Java later
145 ClassDescriptor cn = parseInterfaceDecl(type_pn, packageName);
146 if (toanalyze != null)
148 cn.setSourceFileName(sourcefile);
152 Iterator it_enums = cn.getEnum();
153 while (it_enums.hasNext()) {
154 ClassDescriptor ecd = (ClassDescriptor) it_enums.next();
155 if (toanalyze != null) {
158 ecd.setSourceFileName(sourcefile);
161 } else if (isNode(type_pn, "enum_declaration")) {
162 // TODO add version for normal Java later
163 ClassDescriptor cn = parseEnumDecl(null, type_pn);
164 if (toanalyze != null)
166 cn.setSourceFileName(sourcefile);
168 } else if(isNode(type_pn,"annotation_type_declaration")) {
169 ClassDescriptor cn=parseAnnotationTypeDecl(type_pn);
170 if (toanalyze != null)
172 cn.setSourceFileName(sourcefile);
175 throw new Error(type_pn.getLabel());
183 //This kind of breaks away from tradition a little bit by doing the file checks here
184 // instead of in Semantic check, but doing it here is easier because we have a mapping early on
185 // if I wait until semantic check, I have to change ALL the type descriptors to match the new
186 // mapping and that's both ugly and tedious.
187 private void addMultiImport(String currentSource, String importPath, boolean isPackageDirectory) {
188 boolean found = false;
189 for (int j = 0; j < state.classpath.size(); j++) {
190 String path = (String) state.classpath.get(j);
191 File folder = new File(path, importPath.replace('.', '/'));
192 if (folder.exists()) {
194 for (String file : folder.list()) {
195 // if the file is of type *.java add to multiImport list.
196 if (file.lastIndexOf('.') != -1 && file.substring(file.lastIndexOf('.')).equalsIgnoreCase(".java")) {
197 String classname = file.substring(0, file.length() - 5);
198 // single imports have precedence over multi-imports
199 if (!mandatoryImports.containsKey(classname)) {
200 //package files have precedence over multi-imports.
201 if (multiimports.containsKey(classname) && !isPackageDirectory) {
202 // put error in for later, in case we try to import
203 multiimports.put(classname, new Error("Error: class " + classname + " is defined more than once in a multi-import in " + currentSource));
205 multiimports.put(classname, importPath + "." + classname);
215 throw new Error("Import package " + importPath + " in " + currentSource
216 + " cannot be resolved.");
220 public void parseInitializers(ClassDescriptor cn) {
221 Vector fv=cn.getFieldVec();
223 for(int i=0; i<fv.size(); i++) {
224 FieldDescriptor fd=(FieldDescriptor)fv.get(i);
225 if(fd.getExpressionNode()!=null) {
226 Iterator methodit = cn.getMethods();
227 while(methodit.hasNext()) {
228 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
229 if(currmd.isConstructor()) {
230 BlockNode bn=state.getMethodBody(currmd);
231 NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
232 AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
233 bn.addBlockStatementAt(new BlockExpressionNode(an), pos);
241 private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {
242 ClassDescriptor ecd=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
243 ecd.setImports(mandatoryImports, multiimports);
246 ecd.setSurroundingClass(cn.getSymbol());
247 ecd.setSurrounding(cn);
250 if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
251 ecd.getSymbol().equals(TypeUtil.TagClass))) {
252 ecd.setSuper(TypeUtil.ObjectClass);
254 ecd.setModifiers(parseModifiersList(pn.getChild("modifiers")));
255 parseEnumBody(ecd, pn.getChild("enumbody"));
259 private void parseEnumBody(ClassDescriptor cn, ParseNode pn) {
260 ParseNode decls=pn.getChild("enum_constants_list");
262 ParseNodeVector pnv=decls.getChildren();
263 for(int i=0; i<pnv.size(); i++) {
264 ParseNode decl=pnv.elementAt(i);
265 if (isNode(decl,"enum_constant")) {
266 parseEnumConstant(cn,decl);
267 } else throw new Error();
272 private void parseEnumConstant(ClassDescriptor cn, ParseNode pn) {
273 cn.addEnumConstant(pn.getChild("name").getTerminal());
276 private ClassDescriptor parseAnnotationTypeDecl(ParseNode pn) {
277 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
278 cn.setImports(mandatoryImports, multiimports);
279 ParseNode modifiers=pn.getChild("modifiers");
280 if(modifiers!=null) {
281 cn.setModifiers(parseModifiersList(modifiers));
283 parseAnnotationTypeBody(cn,pn.getChild("body"));
287 private void parseAnnotationTypeBody(ClassDescriptor cn, ParseNode pn) {
288 ParseNode list_node=pn.getChild("annotation_type_element_list");
289 if(list_node!=null) {
290 ParseNodeVector pnv = list_node.getChildren();
291 for (int i = 0; i < pnv.size(); i++) {
292 ParseNode element_node = pnv.elementAt(i);
293 if (isNode(element_node, "annotation_type_element_declaration")) {
294 ParseNodeVector elementProps = element_node.getChildren();
295 String identifier=null;
296 TypeDescriptor type=null;
297 Modifiers modifiers=new Modifiers();
298 for(int eidx=0; eidx<elementProps.size(); eidx++) {
299 ParseNode prop_node=elementProps.elementAt(eidx);
300 if(isNode(prop_node,"name")) {
301 identifier=prop_node.getTerminal();
302 } else if(isNode(prop_node,"type")) {
303 type=parseTypeDescriptor(prop_node);
304 } else if(isNode(prop_node,"modifier")) {
305 modifiers=parseModifiersList(prop_node);
308 cn.addField(new FieldDescriptor(modifiers, type, identifier, null, false));
314 public ClassDescriptor parseInterfaceDecl(ParseNode pn, String packageName) {
316 if(packageName == null) {
317 cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
319 String newClassname = packageName + "." + pn.getChild("name").getTerminal();
320 cn= new ClassDescriptor(packageName, newClassname, true);
323 cn.setImports(mandatoryImports, multiimports);
324 //cn.setAsInterface();
325 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
326 /* parse inherited interface name */
327 ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
328 ParseNodeVector pnv=snlist.getChildren();
329 for(int i=0; i<pnv.size(); i++) {
330 ParseNode decl=pnv.elementAt(i);
331 if (isNode(decl,"type")) {
332 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
333 cn.addSuperInterface(nd.toString());
337 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
338 parseInterfaceBody(cn, pn.getChild("interfacebody"));
342 private void parseInterfaceBody(ClassDescriptor cn, ParseNode pn) {
343 assert(cn.isInterface());
344 ParseNode decls=pn.getChild("interface_member_declaration_list");
346 ParseNodeVector pnv=decls.getChildren();
347 for(int i=0; i<pnv.size(); i++) {
348 ParseNode decl=pnv.elementAt(i);
349 if (isNode(decl,"constant")) {
350 parseInterfaceConstant(cn,decl);
351 } else if (isNode(decl,"method")) {
352 parseInterfaceMethod(cn,decl.getChild("method_declaration"));
353 } else throw new Error();
360 private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
362 parseFieldDecl(cn,pn.getChild("field_declaration"));
368 private void parseInterfaceMethod(ClassDescriptor cn, ParseNode pn) {
369 ParseNode headern=pn.getChild("header");
370 ParseNode bodyn=pn.getChild("body");
371 MethodDescriptor md=parseMethodHeader(headern.getChild("method_header"));
372 md.getModifiers().addModifier(Modifiers.PUBLIC);
373 md.getModifiers().addModifier(Modifiers.ABSTRACT);
375 BlockNode bn=parseBlock(bodyn);
377 state.addTreeCode(md,bn);
379 // this is a hack for investigating new language features
380 // at the AST level, someday should evolve into a nice compiler
382 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
383 // md.getSymbol().equals( ***put your method in here like: "main" )
385 // bn.setStyle( BlockNode.NORMAL );
386 // System.out.println( bn.printNode( 0 ) );
389 } catch (Exception e) {
390 System.out.println("Error with method:"+md.getSymbol());
394 System.out.println("Error with method:"+md.getSymbol());
400 public TaskDescriptor parseTaskDecl(ParseNode pn) {
401 TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
402 ParseNode bodyn=pn.getChild("body");
403 BlockNode bn=parseBlock(bodyn);
404 parseParameterList(td, pn);
405 state.addTreeCode(td,bn);
406 if (pn.getChild("flag_effects_list")!=null)
407 td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
411 public Vector parseFlags(ParseNode pn) {
412 Vector vfe=new Vector();
413 ParseNodeVector pnv=pn.getChildren();
414 for(int i=0; i<pnv.size(); i++) {
415 ParseNode fn=pnv.elementAt(i);
416 FlagEffects fe=parseFlagEffects(fn);
422 public FlagEffects parseFlagEffects(ParseNode pn) {
423 if (isNode(pn,"flag_effect")) {
424 String flagname=pn.getChild("name").getTerminal();
425 FlagEffects fe=new FlagEffects(flagname);
426 if (pn.getChild("flag_list")!=null)
427 parseFlagEffect(fe, pn.getChild("flag_list"));
428 if (pn.getChild("tag_list")!=null)
429 parseTagEffect(fe, pn.getChild("tag_list"));
431 } else throw new Error();
434 public void parseTagEffect(FlagEffects fes, ParseNode pn) {
435 ParseNodeVector pnv=pn.getChildren();
436 for(int i=0; i<pnv.size(); i++) {
437 ParseNode pn2=pnv.elementAt(i);
439 if (isNode(pn2,"not")) {
441 pn2=pn2.getChild("name");
443 String name=pn2.getTerminal();
444 fes.addTagEffect(new TagEffect(name,status));
448 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
449 ParseNodeVector pnv=pn.getChildren();
450 for(int i=0; i<pnv.size(); i++) {
451 ParseNode pn2=pnv.elementAt(i);
453 if (isNode(pn2,"not")) {
455 pn2=pn2.getChild("name");
457 String name=pn2.getTerminal();
458 fes.addEffect(new FlagEffect(name,status));
462 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
463 if (isNode(pn,"or")) {
464 ParseNodeVector pnv=pn.getChildren();
465 ParseNode left=pnv.elementAt(0);
466 ParseNode right=pnv.elementAt(1);
467 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
468 } else if (isNode(pn,"and")) {
469 ParseNodeVector pnv=pn.getChildren();
470 ParseNode left=pnv.elementAt(0);
471 ParseNode right=pnv.elementAt(1);
472 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
473 } else if (isNode(pn, "not")) {
474 ParseNodeVector pnv=pn.getChildren();
475 ParseNode left=pnv.elementAt(0);
476 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
478 } else if (isNode(pn,"name")) {
479 return new FlagNode(pn.getTerminal());
485 public Vector parseChecks(ParseNode pn) {
486 Vector ccs=new Vector();
487 ParseNodeVector pnv=pn.getChildren();
488 for(int i=0; i<pnv.size(); i++) {
489 ParseNode fn=pnv.elementAt(i);
490 ConstraintCheck cc=parseConstraintCheck(fn);
496 public ConstraintCheck parseConstraintCheck(ParseNode pn) {
497 if (isNode(pn,"cons_check")) {
498 String specname=pn.getChild("name").getChild("identifier").getTerminal();
499 Vector[] args=parseConsArgumentList(pn);
500 ConstraintCheck cc=new ConstraintCheck(specname);
501 for(int i=0; i<args[0].size(); i++) {
502 cc.addVariable((String)args[0].get(i));
503 cc.addArgument((ExpressionNode)args[1].get(i));
506 } else throw new Error();
509 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
512 ParseNode paramlist=pn.getChild("task_parameter_list");
515 ParseNodeVector pnv=paramlist.getChildren();
516 for(int i=0; i<pnv.size(); i++) {
517 ParseNode paramn=pnv.elementAt(i);
518 if(paramn.getChild("optional")!=null) {
520 paramn = paramn.getChild("optional").getFirstChild();
521 System.out.println("OPTIONAL FOUND!!!!!!!");
522 } else { optional = false;
523 System.out.println("NOT OPTIONAL"); }
525 TypeDescriptor type=parseTypeDescriptor(paramn);
527 String paramname=paramn.getChild("single").getTerminal();
528 FlagExpressionNode fen=null;
529 if (paramn.getChild("flag")!=null)
530 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
532 ParseNode tagnode=paramn.getChild("tag");
534 TagExpressionList tel=null;
536 tel=parseTagExpressionList(tagnode);
539 td.addParameter(type,paramname,fen, tel, optional);
543 public TagExpressionList parseTagExpressionList(ParseNode pn) {
544 //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
545 //To test, feed in any input program that uses tags
546 ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
547 TagExpressionList tel=new TagExpressionList();
548 for(int i=0; i<pnv.size(); i++) {
549 ParseNode tn=pnv.elementAt(i);
550 String type=tn.getChild("type").getTerminal();
551 String name=tn.getChild("single").getTerminal();
552 tel.addTag(type, name);
557 public ClassDescriptor parseTypeDecl(ParseNode pn, String packageName) {
559 // if is in no package, then create a class descriptor with just the name.
560 // else add the package on
561 if(packageName == null) {
562 cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
564 String newClassname = packageName + "." + pn.getChild("name").getTerminal();
565 cn= new ClassDescriptor(packageName, newClassname, false);
567 cn.setImports(mandatoryImports, multiimports);
568 if (!isEmpty(pn.getChild("super").getTerminal())) {
569 /* parse superclass name */
570 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
571 NameDescriptor nd=parseClassName(snn);
572 cn.setSuper(nd.toString());
574 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
575 cn.getSymbol().equals(TypeUtil.TagClass)))
576 cn.setSuper(TypeUtil.ObjectClass);
578 // check inherited interfaces
579 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
580 /* parse inherited interface name */
581 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
582 ParseNodeVector pnv=snlist.getChildren();
583 for(int i=0; i<pnv.size(); i++) {
584 ParseNode decl=pnv.elementAt(i);
585 if (isNode(decl,"type")) {
586 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
587 cn.addSuperInterface(nd.toString());
591 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
592 parseClassBody(cn, pn.getChild("classbody"));
594 boolean hasConstructor = false;
595 for(Iterator method_it=cn.getMethods(); method_it.hasNext(); ) {
596 MethodDescriptor md=(MethodDescriptor)method_it.next();
597 hasConstructor |= md.isConstructor();
599 if((!hasConstructor) && (!cn.isEnum())) {
600 // add a default constructor for this class
601 MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
602 cn.getSymbol(), false);
603 BlockNode bn=new BlockNode();
604 state.addTreeCode(md,bn);
605 md.setDefaultConstructor();
611 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
612 ParseNode decls=pn.getChild("class_body_declaration_list");
614 ParseNodeVector pnv=decls.getChildren();
615 for(int i=0; i<pnv.size(); i++) {
616 ParseNode decl=pnv.elementAt(i);
617 if (isNode(decl,"member")) {
618 parseClassMember(cn,decl);
619 } else if (isNode(decl,"constructor")) {
620 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
621 } else if (isNode(decl, "static_block")) {
622 parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
623 } else if (isNode(decl,"block")) {
624 } else throw new Error();
629 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
630 ParseNode fieldnode=pn.getChild("field");
631 if (fieldnode!=null) {
632 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
635 ParseNode methodnode=pn.getChild("method");
636 if (methodnode!=null) {
637 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
640 ParseNode innerclassnode=pn.getChild("inner_class_declaration");
641 if (innerclassnode!=null) {
642 parseInnerClassDecl(cn,innerclassnode);
645 ParseNode enumnode=pn.getChild("enum_declaration");
646 if (enumnode!=null) {
647 parseEnumDecl(cn,enumnode);
650 ParseNode flagnode=pn.getChild("flag");
651 if (flagnode!=null) {
652 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
655 // in case there are empty node
656 ParseNode emptynode=pn.getChild("empty");
657 if(emptynode != null) {
663 private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) {
664 ClassDescriptor icn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
665 icn.setImports(mandatoryImports, multiimports);
666 icn.setAsInnerClass();
667 icn.setSurroundingClass(cn.getSymbol());
668 icn.setSurrounding(cn);
669 cn.addInnerClass(icn);
670 if (!isEmpty(pn.getChild("super").getTerminal())) {
671 /* parse superclass name */
672 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
673 NameDescriptor nd=parseClassName(snn);
674 icn.setSuper(nd.toString());
676 if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)||
677 icn.getSymbol().equals(TypeUtil.TagClass)))
678 icn.setSuper(TypeUtil.ObjectClass);
680 // check inherited interfaces
681 if (!isEmpty(pn.getChild("superIF").getTerminal())) {
682 /* parse inherited interface name */
683 ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list");
684 ParseNodeVector pnv=snlist.getChildren();
685 for(int i=0; i<pnv.size(); i++) {
686 ParseNode decl=pnv.elementAt(i);
687 if (isNode(decl,"type")) {
688 NameDescriptor nd=parseClassName(decl.getChild("class").getChild("name"));
689 icn.addSuperInterface(nd.toString());
693 icn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
694 if(!icn.isStatic()) {
695 throw new Error("Error: inner class " + icn.getSymbol() + " in Class " +
696 cn.getSymbol() + " is not a nested class and is not supported yet!");
698 parseClassBody(icn, pn.getChild("classbody"));
702 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
703 ParseNode tn=pn.getChild("type");
704 String type_st=tn.getTerminal();
705 if(type_st.equals("byte")) {
706 return state.getTypeDescriptor(TypeDescriptor.BYTE);
707 } else if(type_st.equals("short")) {
708 return state.getTypeDescriptor(TypeDescriptor.SHORT);
709 } else if(type_st.equals("boolean")) {
710 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
711 } else if(type_st.equals("int")) {
712 return state.getTypeDescriptor(TypeDescriptor.INT);
713 } else if(type_st.equals("long")) {
714 return state.getTypeDescriptor(TypeDescriptor.LONG);
715 } else if(type_st.equals("char")) {
716 return state.getTypeDescriptor(TypeDescriptor.CHAR);
717 } else if(type_st.equals("float")) {
718 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
719 } else if(type_st.equals("double")) {
720 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
721 } else if(type_st.equals("class")) {
722 ParseNode nn=tn.getChild("class");
723 return state.getTypeDescriptor(parseClassName(nn.getChild("name")));
724 } else if(type_st.equals("array")) {
725 ParseNode nn=tn.getChild("array");
726 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
727 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
728 for(int i=0; i<numdims.intValue(); i++)
729 td=td.makeArray(state);
732 System.out.println(pn.PPrint(2, true));
737 //Needed to separate out top level call since if a base exists,
738 //we do not want to apply our resolveName function (i.e. deal with imports)
739 //otherwise, if base == null, we do just want to resolve name.
740 private NameDescriptor parseClassName(ParseNode nn) {
741 ParseNode base=nn.getChild("base");
742 ParseNode id=nn.getChild("identifier");
743 String classname = id.getTerminal();
745 return new NameDescriptor(resolveName(classname));
747 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
750 private NameDescriptor parseClassNameRecursive(ParseNode nn) {
751 ParseNode base=nn.getChild("base");
752 ParseNode id=nn.getChild("identifier");
753 String classname = id.getTerminal();
755 return new NameDescriptor(classname);
757 return new NameDescriptor(parseClassNameRecursive(base.getChild("name")),classname);
760 //This will get the mapping of a terminal class name
761 //to a canonical classname (with imports/package locations in them)
762 private String resolveName(String terminal) {
764 if(mandatoryImports.containsKey(terminal)) {
765 return (String) mandatoryImports.get(terminal);
767 if(multiimports.containsKey(terminal)) {
769 Object o = multiimports.get(terminal);
770 if(o instanceof Error) {
771 throw new Error("Class " + terminal + " is ambiguous. Cause: more than 1 package import contain the same class.");
773 //At this point, if we found a unique class
774 //we can treat it as a single, mandatory import.
775 mandatoryImports.put(terminal, o);
784 //only function difference between this and parseName() is that this
785 //does not look for a import mapping.
786 private NameDescriptor parseName(ParseNode nn) {
787 ParseNode base=nn.getChild("base");
788 ParseNode id=nn.getChild("identifier");
790 return new NameDescriptor(id.getTerminal());
792 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
795 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
796 String name=pn.getChild("name").getTerminal();
797 FlagDescriptor flag=new FlagDescriptor(name);
798 if (pn.getChild("external")!=null)
803 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
804 ParseNode mn=pn.getChild("modifier");
805 Modifiers m=parseModifiersList(mn);
806 if(cn.isInterface()) {
807 // TODO add version for normal Java later
808 // Can only be PUBLIC or STATIC or FINAL
809 if((m.isAbstract()) || (m.isAtomic()) || (m.isNative())
810 || (m.isSynchronized())) {
811 throw new Error("Error: field in Interface " + cn.getSymbol() + "can only be PUBLIC or STATIC or FINAL");
813 m.addModifier(Modifiers.PUBLIC);
814 m.addModifier(Modifiers.STATIC);
815 m.addModifier(Modifiers.FINAL);
818 ParseNode tn=pn.getChild("type");
819 TypeDescriptor t=parseTypeDescriptor(tn);
820 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
821 ParseNodeVector pnv=vn.getChildren();
822 boolean isglobal=pn.getChild("global")!=null;
824 for(int i=0; i<pnv.size(); i++) {
825 ParseNode vardecl=pnv.elementAt(i);
826 ParseNode tmp=vardecl;
827 TypeDescriptor arrayt=t;
828 while (tmp.getChild("single")==null) {
829 arrayt=arrayt.makeArray(state);
830 tmp=tmp.getChild("array");
832 String identifier=tmp.getChild("single").getTerminal();
833 ParseNode epn=vardecl.getChild("initializer");
835 ExpressionNode en=null;
837 en=parseExpression(epn.getFirstChild());
838 en.setNumLine(epn.getFirstChild().getLine());
840 // for static field, the initializer should be considered as a
842 boolean isfirst = false;
843 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
845 // the first static block for this class
846 Modifiers m_i=new Modifiers();
847 m_i.addModifier(Modifiers.STATIC);
848 md = new MethodDescriptor(m_i, "staticblocks", false);
849 md.setAsStaticBlock();
855 cn.incStaticBlocks();
856 BlockNode bn=new BlockNode();
857 NameNode nn=new NameNode(new NameDescriptor(identifier));
858 nn.setNumLine(en.getNumLine());
859 AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
860 an.setNumLine(pn.getLine());
861 bn.addBlockStatement(new BlockExpressionNode(an));
863 state.addTreeCode(md,bn);
865 BlockNode obn = state.getMethodBody(md);
866 for(int ii = 0; ii < bn.size(); ii++) {
867 BlockStatementNode bsn = bn.get(ii);
868 obn.addBlockStatement(bsn);
870 state.addTreeCode(md, obn);
877 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
878 assignAnnotationsToType(m,arrayt);
882 private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type) {
883 Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
884 for(int i=0; i<annotations.size(); i++) {
885 // it only supports a marker annotation
886 AnnotationDescriptor an=annotations.elementAt(i);
887 type.addAnnotationMarker(an);
893 private ExpressionNode parseExpression(ParseNode pn) {
894 if (isNode(pn,"assignment"))
895 return parseAssignmentExpression(pn);
896 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
897 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
898 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
899 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
900 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
901 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
902 isNode(pn,"rightshift")||isNode(pn,"sub")||
903 isNode(pn,"urightshift")||isNode(pn,"sub")||
904 isNode(pn,"add")||isNode(pn,"mult")||
905 isNode(pn,"div")||isNode(pn,"mod")) {
906 ParseNodeVector pnv=pn.getChildren();
907 ParseNode left=pnv.elementAt(0);
908 ParseNode right=pnv.elementAt(1);
909 Operation op=new Operation(pn.getLabel());
910 OpNode on=new OpNode(parseExpression(left),parseExpression(right),op);
911 on.setNumLine(pn.getLine());
913 } else if (isNode(pn,"unaryplus")||
914 isNode(pn,"unaryminus")||
917 ParseNode left=pn.getFirstChild();
918 Operation op=new Operation(pn.getLabel());
919 OpNode on=new OpNode(parseExpression(left),op);
920 on.setNumLine(pn.getLine());
922 } else if (isNode(pn,"postinc")||
923 isNode(pn,"postdec")) {
924 ParseNode left=pn.getFirstChild();
925 AssignOperation op=new AssignOperation(pn.getLabel());
926 AssignmentNode an=new AssignmentNode(parseExpression(left),null,op);
927 an.setNumLine(pn.getLine());
930 } else if (isNode(pn,"preinc")||
931 isNode(pn,"predec")) {
932 ParseNode left=pn.getFirstChild();
933 AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
934 AssignmentNode an=new AssignmentNode(parseExpression(left),
935 new LiteralNode("integer",new Integer(1)),op);
936 an.setNumLine(pn.getLine());
938 } else if (isNode(pn,"literal")) {
939 String literaltype=pn.getTerminal();
940 ParseNode literalnode=pn.getChild(literaltype);
941 Object literal_obj=literalnode.getLiteral();
942 LiteralNode ln=new LiteralNode(literaltype, literal_obj);
943 ln.setNumLine(pn.getLine());
945 } else if (isNode(pn, "createobject")) {
946 TypeDescriptor td = parseTypeDescriptor(pn);
948 Vector args = parseArgumentList(pn);
949 boolean isglobal = pn.getChild("global") != null || pn.getChild("scratch") != null;
950 String disjointId = null;
951 if (pn.getChild("disjoint") != null) {
952 disjointId = pn.getChild("disjoint").getTerminal();
954 CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
955 con.setNumLine(pn.getLine());
956 for (int i = 0; i < args.size(); i++) {
957 con.addArgument((ExpressionNode) args.get(i));
959 /* Could have flag set or tag added here */
960 if (pn.getChild("flag_list") != null || pn.getChild("tag_list") != null) {
961 FlagEffects fe = new FlagEffects(null);
962 if (pn.getChild("flag_list") != null)
963 parseFlagEffect(fe, pn.getChild("flag_list"));
965 if (pn.getChild("tag_list") != null)
966 parseTagEffect(fe, pn.getChild("tag_list"));
967 con.addFlagEffects(fe);
971 } else if (isNode(pn,"createobjectcls")) {
972 //TODO::: FIX BUG!!! static fields in caller context need to become parameters
973 TypeDescriptor td=parseTypeDescriptor(pn);
975 ClassDescriptor cnnew=new ClassDescriptor(td.getSymbol()+"$"+innerCount, false);
976 cnnew.setImports(mandatoryImports, multiimports);
977 cnnew.setSuper(td.getSymbol());
978 parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
979 Vector args=parseArgumentList(pn);
981 CreateObjectNode con=new CreateObjectNode(td, false, null);
982 con.setNumLine(pn.getLine());
983 for(int i=0; i<args.size(); i++) {
984 con.addArgument((ExpressionNode)args.get(i));
988 } else if (isNode(pn,"createarray")) {
989 //System.out.println(pn.PPrint(3,true));
990 boolean isglobal=pn.getChild("global")!=null||
991 pn.getChild("scratch")!=null;
992 String disjointId=null;
993 if( pn.getChild("disjoint") != null) {
994 disjointId = pn.getChild("disjoint").getTerminal();
996 TypeDescriptor td=parseTypeDescriptor(pn);
997 Vector args=parseDimExprs(pn);
999 if (pn.getChild("dims_opt").getLiteral()!=null)
1000 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1001 for(int i=0; i<(args.size()+num); i++)
1002 td=td.makeArray(state);
1003 CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
1004 con.setNumLine(pn.getLine());
1005 for(int i=0; i<args.size(); i++) {
1006 con.addArgument((ExpressionNode)args.get(i));
1010 if (isNode(pn,"createarray2")) {
1011 TypeDescriptor td=parseTypeDescriptor(pn);
1013 if (pn.getChild("dims_opt").getLiteral()!=null)
1014 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
1015 for(int i=0; i<num; i++)
1016 td=td.makeArray(state);
1017 CreateObjectNode con=new CreateObjectNode(td, false, null);
1018 con.setNumLine(pn.getLine());
1019 ParseNode ipn = pn.getChild("initializer");
1020 Vector initializers=parseVariableInitializerList(ipn);
1021 ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
1022 ain.setNumLine(pn.getLine());
1023 con.addArrayInitializer(ain);
1025 } else if (isNode(pn,"name")) {
1026 NameDescriptor nd=parseName(pn);
1027 NameNode nn=new NameNode(nd);
1028 nn.setNumLine(pn.getLine());
1030 } else if (isNode(pn,"this")) {
1031 NameDescriptor nd=new NameDescriptor("this");
1032 NameNode nn=new NameNode(nd);
1033 nn.setNumLine(pn.getLine());
1035 } else if (isNode(pn,"isavailable")) {
1036 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
1037 NameNode nn=new NameNode(nd);
1038 nn.setNumLine(pn.getLine());
1039 return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
1040 } else if (isNode(pn,"methodinvoke1")) {
1041 NameDescriptor nd=parseName(pn.getChild("name"));
1042 Vector args=parseArgumentList(pn);
1043 MethodInvokeNode min=new MethodInvokeNode(nd);
1044 min.setNumLine(pn.getLine());
1045 for(int i=0; i<args.size(); i++) {
1046 min.addArgument((ExpressionNode)args.get(i));
1049 } else if (isNode(pn,"methodinvoke2")) {
1050 String methodid=pn.getChild("id").getTerminal();
1051 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
1052 Vector args=parseArgumentList(pn);
1053 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
1054 min.setNumLine(pn.getLine());
1055 for(int i=0; i<args.size(); i++) {
1056 min.addArgument((ExpressionNode)args.get(i));
1059 } else if (isNode(pn,"fieldaccess")) {
1060 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1061 String fieldname=pn.getChild("field").getTerminal();
1063 FieldAccessNode fan=new FieldAccessNode(en,fieldname);
1064 fan.setNumLine(pn.getLine());
1066 } else if (isNode(pn,"arrayaccess")) {
1067 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
1068 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
1069 ArrayAccessNode aan=new ArrayAccessNode(en,index);
1070 aan.setNumLine(pn.getLine());
1072 } else if (isNode(pn,"cast1")) {
1074 CastNode cn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
1075 cn.setNumLine(pn.getLine());
1077 } catch (Exception e) {
1078 System.out.println(pn.PPrint(1,true));
1079 e.printStackTrace();
1082 } else if (isNode(pn,"cast2")) {
1083 CastNode cn=new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
1084 cn.setNumLine(pn.getLine());
1086 } else if (isNode(pn, "getoffset")) {
1087 TypeDescriptor td=parseTypeDescriptor(pn);
1088 String fieldname = pn.getChild("field").getTerminal();
1089 //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ " fieldname= " + fieldname);
1090 return new OffsetNode(td, fieldname);
1091 } else if (isNode(pn, "tert")) {
1093 TertiaryNode tn=new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
1094 parseExpression(pn.getChild("trueexpr").getFirstChild()),
1095 parseExpression(pn.getChild("falseexpr").getFirstChild()) );
1096 tn.setNumLine(pn.getLine());
1099 } else if (isNode(pn, "instanceof")) {
1100 ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
1101 TypeDescriptor t=parseTypeDescriptor(pn);
1102 InstanceOfNode ion=new InstanceOfNode(exp,t);
1103 ion.setNumLine(pn.getLine());
1105 } else if (isNode(pn, "array_initializer")) {
1106 Vector initializers=parseVariableInitializerList(pn);
1107 return new ArrayInitializerNode(initializers);
1108 } else if (isNode(pn, "class_type")) {
1109 TypeDescriptor td=parseTypeDescriptor(pn);
1110 ClassTypeNode ctn=new ClassTypeNode(td);
1111 ctn.setNumLine(pn.getLine());
1113 } else if (isNode(pn, "empty")) {
1116 System.out.println("---------------------");
1117 System.out.println(pn.PPrint(3,true));
1122 private Vector parseDimExprs(ParseNode pn) {
1123 Vector arglist=new Vector();
1124 ParseNode an=pn.getChild("dim_exprs");
1125 if (an==null) /* No argument list */
1127 ParseNodeVector anv=an.getChildren();
1128 for(int i=0; i<anv.size(); i++) {
1129 arglist.add(parseExpression(anv.elementAt(i)));
1134 private Vector parseArgumentList(ParseNode pn) {
1135 Vector arglist=new Vector();
1136 ParseNode an=pn.getChild("argument_list");
1137 if (an==null) /* No argument list */
1139 ParseNodeVector anv=an.getChildren();
1140 for(int i=0; i<anv.size(); i++) {
1141 arglist.add(parseExpression(anv.elementAt(i)));
1146 private Vector[] parseConsArgumentList(ParseNode pn) {
1147 Vector arglist=new Vector();
1148 Vector varlist=new Vector();
1149 ParseNode an=pn.getChild("cons_argument_list");
1150 if (an==null) /* No argument list */
1151 return new Vector[] {varlist, arglist};
1152 ParseNodeVector anv=an.getChildren();
1153 for(int i=0; i<anv.size(); i++) {
1154 ParseNode cpn=anv.elementAt(i);
1155 ParseNode var=cpn.getChild("var");
1156 ParseNode exp=cpn.getChild("exp").getFirstChild();
1157 varlist.add(var.getTerminal());
1158 arglist.add(parseExpression(exp));
1160 return new Vector[] {varlist, arglist};
1163 private Vector parseVariableInitializerList(ParseNode pn) {
1164 Vector varInitList=new Vector();
1165 ParseNode vin=pn.getChild("var_init_list");
1166 if (vin==null) /* No argument list */
1168 ParseNodeVector vinv=vin.getChildren();
1169 for(int i=0; i<vinv.size(); i++) {
1170 varInitList.add(parseExpression(vinv.elementAt(i)));
1175 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
1176 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
1177 ParseNodeVector pnv=pn.getChild("args").getChildren();
1179 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
1180 an.setNumLine(pn.getLine());
1185 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
1186 ParseNode headern=pn.getChild("method_header");
1187 ParseNode bodyn=pn.getChild("body");
1188 MethodDescriptor md=parseMethodHeader(headern);
1190 BlockNode bn=parseBlock(bodyn);
1191 bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
1193 state.addTreeCode(md,bn);
1195 // this is a hack for investigating new language features
1196 // at the AST level, someday should evolve into a nice compiler
1198 //if( cn.getSymbol().equals( ***put a class in here like: "Test" ) &&
1199 // md.getSymbol().equals( ***put your method in here like: "main" )
1201 // bn.setStyle( BlockNode.NORMAL );
1202 // System.out.println( bn.printNode( 0 ) );
1205 } catch (Exception e) {
1206 System.out.println("Error with method:"+md.getSymbol());
1207 e.printStackTrace();
1210 System.out.println("Error with method:"+md.getSymbol());
1211 e.printStackTrace();
1216 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
1217 ParseNode mn=pn.getChild("modifiers");
1218 Modifiers m=parseModifiersList(mn);
1219 ParseNode cdecl=pn.getChild("constructor_declarator");
1220 boolean isglobal=cdecl.getChild("global")!=null;
1221 String name=resolveName(cn.getSymbol());
1222 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
1223 ParseNode paramnode=cdecl.getChild("parameters");
1224 parseParameterList(md,paramnode);
1225 ParseNode bodyn0=pn.getChild("body");
1226 ParseNode bodyn=bodyn0.getChild("constructor_body");
1229 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1230 bn=parseBlock(bodyn);
1233 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
1234 ParseNode sin=bodyn.getChild("superinvoke");
1235 NameDescriptor nd=new NameDescriptor("super");
1236 Vector args=parseArgumentList(sin);
1237 MethodInvokeNode min=new MethodInvokeNode(nd);
1238 min.setNumLine(sin.getLine());
1239 for(int i=0; i<args.size(); i++) {
1240 min.addArgument((ExpressionNode)args.get(i));
1242 BlockExpressionNode ben=new BlockExpressionNode(min);
1243 bn.addFirstBlockStatement(ben);
1245 } else if (bodyn!=null&&bodyn.getChild("explconstrinv")!=null) {
1246 ParseNode eci=bodyn.getChild("explconstrinv");
1247 NameDescriptor nd=new NameDescriptor(cn.getSymbol());
1248 Vector args=parseArgumentList(eci);
1249 MethodInvokeNode min=new MethodInvokeNode(nd);
1250 min.setNumLine(eci.getLine());
1251 for(int i=0; i<args.size(); i++) {
1252 min.addArgument((ExpressionNode)args.get(i));
1254 BlockExpressionNode ben=new BlockExpressionNode(min);
1255 ben.setNumLine(eci.getLine());
1256 bn.addFirstBlockStatement(ben);
1258 state.addTreeCode(md,bn);
1261 private void parseStaticBlockDecl(ClassDescriptor cn, ParseNode pn) {
1262 // Each class maintains one MethodDecscriptor which combines all its
1263 // static blocks in their declaration order
1264 boolean isfirst = false;
1265 MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
1267 // the first static block for this class
1268 Modifiers m_i=new Modifiers();
1269 m_i.addModifier(Modifiers.STATIC);
1270 md = new MethodDescriptor(m_i, "staticblocks", false);
1271 md.setAsStaticBlock();
1274 ParseNode bodyn=pn.getChild("body");
1278 cn.incStaticBlocks();
1280 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
1281 bn=parseBlock(bodyn);
1285 state.addTreeCode(md,bn);
1287 BlockNode obn = state.getMethodBody(md);
1288 for(int i = 0; i < bn.size(); i++) {
1289 BlockStatementNode bsn = bn.get(i);
1290 obn.addBlockStatement(bsn);
1292 state.addTreeCode(md, obn);
1297 public BlockNode parseBlock(ParseNode pn) {
1298 this.m_taskexitnum = 0;
1299 if (pn==null||isEmpty(pn.getTerminal()))
1300 return new BlockNode();
1301 ParseNode bsn=pn.getChild("block_statement_list");
1302 return parseBlockHelper(bsn);
1305 private BlockNode parseBlockHelper(ParseNode pn) {
1306 ParseNodeVector pnv=pn.getChildren();
1307 BlockNode bn=new BlockNode();
1308 for(int i=0; i<pnv.size(); i++) {
1309 Vector bsv=parseBlockStatement(pnv.elementAt(i));
1310 for(int j=0; j<bsv.size(); j++) {
1311 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1317 public BlockNode parseSingleBlock(ParseNode pn) {
1318 BlockNode bn=new BlockNode();
1319 Vector bsv=parseBlockStatement(pn);
1320 for(int j=0; j<bsv.size(); j++) {
1321 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
1323 bn.setStyle(BlockNode.NOBRACES);
1327 public Vector parseSESEBlock(Vector parentbs, ParseNode pn) {
1328 ParseNodeVector pnv=pn.getChildren();
1329 Vector bv=new Vector();
1330 for(int i=0; i<pnv.size(); i++) {
1331 bv.addAll(parseBlockStatement(pnv.elementAt(i)));
1336 public Vector parseBlockStatement(ParseNode pn) {
1337 Vector blockstatements=new Vector();
1338 if (isNode(pn,"tag_declaration")) {
1339 String name=pn.getChild("single").getTerminal();
1340 String type=pn.getChild("type").getTerminal();
1342 TagDeclarationNode tdn=new TagDeclarationNode(name, type);
1343 tdn.setNumLine(pn.getLine());
1345 blockstatements.add(tdn);
1346 } else if (isNode(pn,"local_variable_declaration")) {
1348 TypeDescriptor t=parseTypeDescriptor(pn);
1349 ParseNode vn=pn.getChild("variable_declarators_list");
1350 ParseNodeVector pnv=vn.getChildren();
1351 for(int i=0; i<pnv.size(); i++) {
1352 ParseNode vardecl=pnv.elementAt(i);
1355 ParseNode tmp=vardecl;
1356 TypeDescriptor arrayt=t;
1358 while (tmp.getChild("single")==null) {
1359 arrayt=arrayt.makeArray(state);
1360 tmp=tmp.getChild("array");
1362 String identifier=tmp.getChild("single").getTerminal();
1364 ParseNode epn=vardecl.getChild("initializer");
1367 ExpressionNode en=null;
1369 en=parseExpression(epn.getFirstChild());
1371 DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
1372 dn.setNumLine(tmp.getLine());
1374 ParseNode mn=pn.getChild("modifiers");
1376 // here, modifers parse node has the list of annotations
1377 Modifiers m=parseModifiersList(mn);
1378 assignAnnotationsToType(m, arrayt);
1381 blockstatements.add(dn);
1383 } else if (isNode(pn,"nop")) {
1385 } else if (isNode(pn,"expression")) {
1386 BlockExpressionNode ben=new BlockExpressionNode(parseExpression(pn.getFirstChild()));
1387 ben.setNumLine(pn.getLine());
1388 blockstatements.add(ben);
1389 } else if (isNode(pn,"ifstatement")) {
1390 IfStatementNode isn=new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1391 parseSingleBlock(pn.getChild("statement").getFirstChild()),
1392 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null);
1393 isn.setNumLine(pn.getLine());
1395 blockstatements.add(isn);
1396 } else if (isNode(pn,"switch_statement")) {
1397 // TODO add version for normal Java later
1398 SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
1399 parseSingleBlock(pn.getChild("statement").getFirstChild()));
1400 ssn.setNumLine(pn.getLine());
1401 blockstatements.add(ssn);
1402 } else if (isNode(pn,"switch_block_list")) {
1403 // TODO add version for normal Java later
1404 ParseNodeVector pnv=pn.getChildren();
1405 for(int i=0; i<pnv.size(); i++) {
1406 ParseNode sblockdecl=pnv.elementAt(i);
1408 if(isNode(sblockdecl, "switch_block")) {
1409 ParseNode lpn=sblockdecl.getChild("switch_labels").getChild("switch_label_list");
1410 ParseNodeVector labelv=lpn.getChildren();
1411 Vector<SwitchLabelNode> slv = new Vector<SwitchLabelNode>();
1412 for(int j=0; j<labelv.size(); j++) {
1413 ParseNode labeldecl=labelv.elementAt(j);
1414 if(isNode(labeldecl, "switch_label")) {
1415 SwitchLabelNode sln=new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false);
1416 sln.setNumLine(labeldecl.getLine());
1417 slv.addElement(sln);
1418 } else if(isNode(labeldecl, "default_switch_label")) {
1419 SwitchLabelNode sln=new SwitchLabelNode(null, true);
1420 sln.setNumLine(labeldecl.getLine());
1421 slv.addElement(sln);
1425 SwitchBlockNode sbn=new SwitchBlockNode(slv,
1426 parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild()));
1427 sbn.setNumLine(sblockdecl.getLine());
1429 blockstatements.add(sbn);
1433 } else if (isNode(pn, "trycatchstatement")) {
1434 // TODO add version for normal Java later
1435 // Do not fully support exceptions now. Only make sure that if there are no
1436 // exceptions thrown, the execution is right
1437 ParseNode tpn = pn.getChild("tryblock").getFirstChild();
1438 BlockNode bn=parseBlockHelper(tpn);
1439 blockstatements.add(new SubBlockNode(bn));
1441 ParseNode fbk = pn.getChild("finallyblock");
1443 ParseNode fpn = fbk.getFirstChild();
1444 BlockNode fbn=parseBlockHelper(fpn);
1445 blockstatements.add(new SubBlockNode(fbn));
1447 } else if (isNode(pn, "throwstatement")) {
1448 // TODO Simply return here
1449 //blockstatements.add(new ReturnNode());
1450 } else if (isNode(pn,"taskexit")) {
1452 if (pn.getChild("flag_effects_list")!=null)
1453 vfe=parseFlags(pn.getChild("flag_effects_list"));
1455 if (pn.getChild("cons_checks")!=null)
1456 ccs=parseChecks(pn.getChild("cons_checks"));
1457 TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
1458 ten.setNumLine(pn.getLine());
1459 blockstatements.add(ten);
1460 } else if (isNode(pn,"atomic")) {
1461 BlockNode bn=parseBlockHelper(pn);
1462 AtomicNode an=new AtomicNode(bn);
1463 an.setNumLine(pn.getLine());
1464 blockstatements.add(an);
1465 } else if (isNode(pn,"synchronized")) {
1466 BlockNode bn=parseBlockHelper(pn.getChild("block"));
1467 ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
1468 SynchronizedNode sn=new SynchronizedNode(en, bn);
1469 sn.setNumLine(pn.getLine());
1470 blockstatements.add(sn);
1471 } else if (isNode(pn,"return")) {
1472 if (isEmpty(pn.getTerminal()))
1473 blockstatements.add(new ReturnNode());
1475 ExpressionNode en=parseExpression(pn.getFirstChild());
1476 ReturnNode rn=new ReturnNode(en);
1477 rn.setNumLine(pn.getLine());
1478 blockstatements.add(rn);
1480 } else if (isNode(pn,"block_statement_list")) {
1481 BlockNode bn=parseBlockHelper(pn);
1482 blockstatements.add(new SubBlockNode(bn));
1483 } else if (isNode(pn,"empty")) {
1485 } else if (isNode(pn,"statement_expression_list")) {
1486 ParseNodeVector pnv=pn.getChildren();
1487 BlockNode bn=new BlockNode();
1488 for(int i=0; i<pnv.size(); i++) {
1489 ExpressionNode en=parseExpression(pnv.elementAt(i));
1490 blockstatements.add(new BlockExpressionNode(en));
1492 bn.setStyle(BlockNode.EXPRLIST);
1493 } else if (isNode(pn,"forstatement")) {
1494 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
1495 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
1496 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1497 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1498 if(condition == null) {
1499 // no condition clause, make a 'true' expression as the condition
1500 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1502 LoopNode ln=new LoopNode(init,condition,update,body);
1503 ln.setNumLine(pn.getLine());
1504 blockstatements.add(ln);
1505 } else if (isNode(pn,"whilestatement")) {
1506 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1507 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1508 if(condition == null) {
1509 // no condition clause, make a 'true' expression as the condition
1510 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1512 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
1513 } else if (isNode(pn,"dowhilestatement")) {
1514 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
1515 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
1516 if(condition == null) {
1517 // no condition clause, make a 'true' expression as the condition
1518 condition = (ExpressionNode) new LiteralNode("boolean", new Boolean(true));
1520 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
1521 } else if (isNode(pn,"sese")) {
1522 ParseNode pnID=pn.getChild("identifier");
1524 if( pnID != null ) {
1525 stID=pnID.getFirstChild().getTerminal();
1527 SESENode start=new SESENode(stID);
1528 start.setNumLine(pn.getLine());
1529 SESENode end =new SESENode(stID);
1531 end.setStart(start);
1532 blockstatements.add(start);
1533 blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
1534 blockstatements.add(end);
1535 } else if (isNode(pn,"continue")) {
1536 ContinueBreakNode cbn=new ContinueBreakNode(false);
1537 cbn.setNumLine(pn.getLine());
1538 blockstatements.add(cbn);
1539 } else if (isNode(pn,"break")) {
1540 ContinueBreakNode cbn=new ContinueBreakNode(true);
1541 cbn.setNumLine(pn.getLine());
1542 blockstatements.add(cbn);
1543 ParseNode idopt_pn=pn.getChild("identifier_opt");
1544 ParseNode name_pn=idopt_pn.getChild("name");
1545 // name_pn.getTerminal() gives you the label
1546 } else if (isNode(pn,"genreach")) {
1547 String graphName = pn.getChild("graphName").getTerminal();
1548 blockstatements.add(new GenReachNode(graphName) );
1550 } else if(isNode(pn,"labeledstatement")) {
1551 String label = pn.getChild("name").getTerminal();
1552 BlockNode bn=parseSingleBlock(pn.getChild("statement").getFirstChild());
1554 blockstatements.add(new SubBlockNode(bn));
1556 System.out.println("---------------");
1557 System.out.println(pn.PPrint(3,true));
1560 return blockstatements;
1563 public MethodDescriptor parseMethodHeader(ParseNode pn) {
1564 ParseNode mn=pn.getChild("modifiers");
1565 Modifiers m=parseModifiersList(mn);
1567 ParseNode tn=pn.getChild("returntype");
1568 TypeDescriptor returntype;
1570 returntype=parseTypeDescriptor(tn);
1572 returntype=new TypeDescriptor(TypeDescriptor.VOID);
1574 ParseNode pmd=pn.getChild("method_declarator");
1575 String name=pmd.getChild("name").getTerminal();
1576 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
1578 ParseNode paramnode=pmd.getChild("parameters");
1579 parseParameterList(md,paramnode);
1583 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
1584 ParseNode paramlist=pn.getChild("formal_parameter_list");
1585 if (paramlist==null)
1587 ParseNodeVector pnv=paramlist.getChildren();
1588 for(int i=0; i<pnv.size(); i++) {
1589 ParseNode paramn=pnv.elementAt(i);
1591 if (isNode(paramn, "tag_parameter")) {
1592 String paramname=paramn.getChild("single").getTerminal();
1593 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
1594 md.addTagParameter(type, paramname);
1597 TypeDescriptor type=parseTypeDescriptor(paramn);
1599 ParseNode tmp=paramn;
1600 while (tmp.getChild("single")==null) {
1601 type=type.makeArray(state);
1602 tmp=tmp.getChild("array");
1604 String paramname=tmp.getChild("single").getTerminal();
1606 md.addParameter(type, paramname);
1607 if(isNode(paramn, "annotation_parameter")) {
1608 ParseNode bodynode=paramn.getChild("annotation_body");
1609 parseParameterAnnotation(bodynode,type);
1616 public Modifiers parseModifiersList(ParseNode pn) {
1617 Modifiers m=new Modifiers();
1618 ParseNode modlist=pn.getChild("modifier_list");
1619 if (modlist!=null) {
1620 ParseNodeVector pnv=modlist.getChildren();
1621 for(int i=0; i<pnv.size(); i++) {
1622 ParseNode modn=pnv.elementAt(i);
1623 if (isNode(modn,"public"))
1624 m.addModifier(Modifiers.PUBLIC);
1625 else if (isNode(modn,"protected"))
1626 m.addModifier(Modifiers.PROTECTED);
1627 else if (isNode(modn,"private"))
1628 m.addModifier(Modifiers.PRIVATE);
1629 else if (isNode(modn,"static"))
1630 m.addModifier(Modifiers.STATIC);
1631 else if (isNode(modn,"final"))
1632 m.addModifier(Modifiers.FINAL);
1633 else if (isNode(modn,"native"))
1634 m.addModifier(Modifiers.NATIVE);
1635 else if (isNode(modn,"synchronized"))
1636 m.addModifier(Modifiers.SYNCHRONIZED);
1637 else if (isNode(modn,"atomic"))
1638 m.addModifier(Modifiers.ATOMIC);
1639 else if (isNode(modn,"abstract"))
1640 m.addModifier(Modifiers.ABSTRACT);
1641 else if (isNode(modn,"volatile"))
1642 m.addModifier(Modifiers.VOLATILE);
1643 else if (isNode(modn,"transient"))
1644 m.addModifier(Modifiers.TRANSIENT);
1645 else if(isNode(modn,"annotation_list"))
1646 parseAnnotationList(modn,m);
1648 throw new Error("Unrecognized Modifier:"+modn.getLabel());
1655 private void parseAnnotationList(ParseNode pn, Modifiers m) {
1656 ParseNodeVector pnv = pn.getChildren();
1657 for (int i = 0; i < pnv.size(); i++) {
1658 ParseNode body_list = pnv.elementAt(i);
1659 if (isNode(body_list, "annotation_body")) {
1660 ParseNode body_node = body_list.getFirstChild();
1661 if (isNode(body_node, "marker_annotation")) {
1662 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1663 } else if (isNode(body_node, "single_annotation")) {
1664 m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1665 body_node.getChild("element_value").getTerminal()));
1666 } else if (isNode(body_node, "normal_annotation")) {
1667 throw new Error("Annotation with multiple data members is not supported yet.");
1673 private void parseParameterAnnotation(ParseNode body_list,TypeDescriptor type) {
1674 ParseNode body_node = body_list.getFirstChild();
1675 if (isNode(body_node, "marker_annotation")) {
1676 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
1677 } else if (isNode(body_node, "single_annotation")) {
1678 type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
1679 body_node.getChild("element_value").getTerminal()));
1680 } else if (isNode(body_node, "normal_annotation")) {
1681 throw new Error("Annotation with multiple data members is not supported yet.");
1685 private boolean isNode(ParseNode pn, String label) {
1686 if (pn.getLabel().equals(label))
1691 private static boolean isEmpty(ParseNode pn) {
1692 if (pn.getLabel().equals("empty"))
1698 private static boolean isEmpty(String s) {
1699 if (s.equals("empty"))
1705 /** Throw an exception if something is unexpected */
1706 private void check(ParseNode pn, String label) {
1708 throw new Error(pn+ "IE: Expected '" + label + "', got null");
1710 if (!pn.getLabel().equals(label)) {
1711 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");