X-Git-Url: http://plrg.eecs.uci.edu/git/?p=IRC.git;a=blobdiff_plain;f=Robust%2Fsrc%2FIR%2FTree%2FBuildIR.java;h=9785ebbbab5d46b5cfe372f4a9a7523f8aad984d;hp=e31d6d14f50b06db2bb234f4bc5e30d5aa99a7de;hb=eb17be02c22191b3fc7bdc335d9434ada68278de;hpb=c94e3d181dec110fc0bd7071a555631cd2a3863d diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index e31d6d14..9785ebbb 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -17,23 +17,23 @@ public class BuildIR { public void buildtree(ParseNode pn, Set toanalyze, String sourcefile) { parseFile(pn, toanalyze, sourcefile); - + // numering the interfaces int if_num = 0; Iterator it_classes = state.getClassSymbolTable().getValueSet().iterator(); while(it_classes.hasNext()) { ClassDescriptor cd = (ClassDescriptor)it_classes.next(); if(cd.isInterface()) { - cd.setInterfaceId(if_num++); + cd.setInterfaceId(if_num++); } } } //This is all single imports and a subset of the - //multi imports that have been resolved. + //multi imports that have been resolved. Hashtable mandatoryImports; //maps class names in file to full name - //Note may map a name to an ERROR. + //Note may map a name to an ERROR. Hashtable multiimports; NameDescriptor packages; @@ -41,145 +41,144 @@ public class BuildIR { public void parseFile(ParseNode pn, Set toanalyze, String sourcefile) { mandatoryImports = new Hashtable(); multiimports = new Hashtable(); - + if(state.JNI) { //add java.lang as our default multi-import this.addMultiImport(sourcefile, "java.lang", false); } - + ParseNode ipn = pn.getChild("imports").getChild("import_decls_list"); if (ipn != null) { ParseNodeVector pnv = ipn.getChildren(); for (int i = 0; i < pnv.size(); i++) { - ParseNode pnimport = pnv.elementAt(i); - NameDescriptor nd = parseName(pnimport.getChild("name")); - if (isNode(pnimport, "import_single")) { - if (!mandatoryImports.containsKey(nd.getIdentifier())) { - // map name to full name (includes package/directory - mandatoryImports.put(nd.getIdentifier(), nd.getPathFromRootToHere()); - } else { - throw new Error("An ambiguous class "+ nd.getIdentifier() +" has been found. It is included for " + - ((String)mandatoryImports.get(nd.getIdentifier())) + " and " + - nd.getPathFromRootToHere()); - } - } - else { - addMultiImport(sourcefile, nd.getPathFromRootToHere(), false); - } + ParseNode pnimport = pnv.elementAt(i); + NameDescriptor nd = parseName(pnimport.getChild("name")); + if (isNode(pnimport, "import_single")) { + if (!mandatoryImports.containsKey(nd.getIdentifier())) { + // map name to full name (includes package/directory + mandatoryImports.put(nd.getIdentifier(), nd.getPathFromRootToHere()); + } else { + throw new Error("An ambiguous class "+ nd.getIdentifier() +" has been found. It is included for " + + ((String)mandatoryImports.get(nd.getIdentifier())) + " and " + + nd.getPathFromRootToHere()); + } + } else { + addMultiImport(sourcefile, nd.getPathFromRootToHere(), false); + } } } - + ParseNode ppn=pn.getChild("packages").getChild("package"); String packageName = null; if (ppn!=null) { NameDescriptor nd = parseClassName(ppn.getChild("name")); - packageName = nd.getPathFromRootToHere(); - //Trick -> import the package directory as a multi-import and it'll + packageName = nd.getPathFromRootToHere(); + //Trick -> import the package directory as a multi-import and it'll //automatically recognize files in the same directory. addMultiImport(sourcefile, packageName, true); } - + ParseNode tpn=pn.getChild("type_declaration_list"); if (tpn != null) { ParseNodeVector pnv = tpn.getChildren(); for (int i = 0; i < pnv.size(); i++) { - ParseNode type_pn = pnv.elementAt(i); - if (isEmpty(type_pn)) /* Skip the semicolon */ - continue; - if (isNode(type_pn, "class_declaration")) { - ClassDescriptor cn = parseTypeDecl(type_pn, packageName); - cn.setSourceFileName(sourcefile); - parseInitializers(cn); - if (toanalyze != null) - toanalyze.add(cn); - state.addClass(cn); - // for inner classes/enum - HashSet tovisit = new HashSet(); - Iterator it_icds = cn.getInnerClasses(); - while (it_icds.hasNext()) { - tovisit.add(it_icds.next()); - } - - while (!tovisit.isEmpty()) { - ClassDescriptor cd = (ClassDescriptor) tovisit.iterator().next(); - tovisit.remove(cd); - parseInitializers(cd); - if (toanalyze != null) { - toanalyze.add(cd); - } - cd.setSourceFileName(sourcefile); - state.addClass(cd); - - Iterator it_ics = cd.getInnerClasses(); - while (it_ics.hasNext()) { - tovisit.add(it_ics.next()); - } - - Iterator it_ienums = cd.getEnum(); - while (it_ienums.hasNext()) { - ClassDescriptor iecd = (ClassDescriptor) it_ienums.next(); - if (toanalyze != null) { - toanalyze.add(iecd); - } - iecd.setSourceFileName(sourcefile); - state.addClass(iecd); - } - } - - Iterator it_enums = cn.getEnum(); - while (it_enums.hasNext()) { - ClassDescriptor ecd = (ClassDescriptor) it_enums.next(); - if (toanalyze != null) { - toanalyze.add(ecd); - } - ecd.setSourceFileName(sourcefile); - state.addClass(ecd); - } - } else if (isNode(type_pn, "task_declaration")) { - TaskDescriptor td = parseTaskDecl(type_pn); - if (toanalyze != null) - toanalyze.add(td); - state.addTask(td); - } else if (isNode(type_pn, "interface_declaration")) { - // TODO add version for normal Java later - ClassDescriptor cn = parseInterfaceDecl(type_pn, packageName); - if (toanalyze != null) - toanalyze.add(cn); - cn.setSourceFileName(sourcefile); - state.addClass(cn); - - // for enum - Iterator it_enums = cn.getEnum(); - while (it_enums.hasNext()) { - ClassDescriptor ecd = (ClassDescriptor) it_enums.next(); - if (toanalyze != null) { - toanalyze.add(ecd); - } - ecd.setSourceFileName(sourcefile); - state.addClass(ecd); - } - } else if (isNode(type_pn, "enum_declaration")) { - // TODO add version for normal Java later - ClassDescriptor cn = parseEnumDecl(null, type_pn); - if (toanalyze != null) - toanalyze.add(cn); - cn.setSourceFileName(sourcefile); - state.addClass(cn); - } else if(isNode(type_pn,"annotation_type_declaration")){ - ClassDescriptor cn=parseAnnotationTypeDecl(type_pn); - if (toanalyze != null) - toanalyze.add(cn); - cn.setSourceFileName(sourcefile); - state.addClass(cn); - } else { - throw new Error(type_pn.getLabel()); - } + ParseNode type_pn = pnv.elementAt(i); + if (isEmpty(type_pn)) /* Skip the semicolon */ + continue; + if (isNode(type_pn, "class_declaration")) { + ClassDescriptor cn = parseTypeDecl(type_pn, packageName); + cn.setSourceFileName(sourcefile); + parseInitializers(cn); + if (toanalyze != null) + toanalyze.add(cn); + state.addClass(cn); + // for inner classes/enum + HashSet tovisit = new HashSet(); + Iterator it_icds = cn.getInnerClasses(); + while (it_icds.hasNext()) { + tovisit.add(it_icds.next()); + } + + while (!tovisit.isEmpty()) { + ClassDescriptor cd = (ClassDescriptor) tovisit.iterator().next(); + tovisit.remove(cd); + parseInitializers(cd); + if (toanalyze != null) { + toanalyze.add(cd); + } + cd.setSourceFileName(sourcefile); + state.addClass(cd); + + Iterator it_ics = cd.getInnerClasses(); + while (it_ics.hasNext()) { + tovisit.add(it_ics.next()); + } + + Iterator it_ienums = cd.getEnum(); + while (it_ienums.hasNext()) { + ClassDescriptor iecd = (ClassDescriptor) it_ienums.next(); + if (toanalyze != null) { + toanalyze.add(iecd); + } + iecd.setSourceFileName(sourcefile); + state.addClass(iecd); + } + } + + Iterator it_enums = cn.getEnum(); + while (it_enums.hasNext()) { + ClassDescriptor ecd = (ClassDescriptor) it_enums.next(); + if (toanalyze != null) { + toanalyze.add(ecd); + } + ecd.setSourceFileName(sourcefile); + state.addClass(ecd); + } + } else if (isNode(type_pn, "task_declaration")) { + TaskDescriptor td = parseTaskDecl(type_pn); + if (toanalyze != null) + toanalyze.add(td); + state.addTask(td); + } else if (isNode(type_pn, "interface_declaration")) { + // TODO add version for normal Java later + ClassDescriptor cn = parseInterfaceDecl(type_pn, packageName); + if (toanalyze != null) + toanalyze.add(cn); + cn.setSourceFileName(sourcefile); + state.addClass(cn); + + // for enum + Iterator it_enums = cn.getEnum(); + while (it_enums.hasNext()) { + ClassDescriptor ecd = (ClassDescriptor) it_enums.next(); + if (toanalyze != null) { + toanalyze.add(ecd); + } + ecd.setSourceFileName(sourcefile); + state.addClass(ecd); + } + } else if (isNode(type_pn, "enum_declaration")) { + // TODO add version for normal Java later + ClassDescriptor cn = parseEnumDecl(null, type_pn); + if (toanalyze != null) + toanalyze.add(cn); + cn.setSourceFileName(sourcefile); + state.addClass(cn); + } else if(isNode(type_pn,"annotation_type_declaration")) { + ClassDescriptor cn=parseAnnotationTypeDecl(type_pn); + if (toanalyze != null) + toanalyze.add(cn); + cn.setSourceFileName(sourcefile); + state.addClass(cn); + } else { + throw new Error(type_pn.getLabel()); + } } } } - - - + + + //This kind of breaks away from tradition a little bit by doing the file checks here // instead of in Semantic check, but doing it here is easier because we have a mapping early on // if I wait until semantic check, I have to change ALL the type descriptors to match the new @@ -190,43 +189,43 @@ public class BuildIR { String path = (String) state.classpath.get(j); File folder = new File(path, importPath.replace('.', '/')); if (folder.exists()) { - found = true; - for (String file : folder.list()) { - // if the file is of type *.java add to multiImport list. - if (file.lastIndexOf('.') != -1 && file.substring(file.lastIndexOf('.')).equalsIgnoreCase(".java")) { - String classname = file.substring(0, file.length() - 5); - // single imports have precedence over multi-imports - if (!mandatoryImports.containsKey(classname)) { - //package files have precedence over multi-imports. - if (multiimports.containsKey(classname) && !isPackageDirectory) { - // put error in for later, in case we try to import - multiimports.put(classname, new Error("Error: class " + classname + " is defined more than once in a multi-import in " + currentSource)); - } else { - multiimports.put(classname, importPath + "." + classname); - } - } - } - } + found = true; + for (String file : folder.list()) { + // if the file is of type *.java add to multiImport list. + if (file.lastIndexOf('.') != -1 && file.substring(file.lastIndexOf('.')).equalsIgnoreCase(".java")) { + String classname = file.substring(0, file.length() - 5); + // single imports have precedence over multi-imports + if (!mandatoryImports.containsKey(classname)) { + //package files have precedence over multi-imports. + if (multiimports.containsKey(classname) && !isPackageDirectory) { + // put error in for later, in case we try to import + multiimports.put(classname, new Error("Error: class " + classname + " is defined more than once in a multi-import in " + currentSource)); + } else { + multiimports.put(classname, importPath + "." + classname); + } + } + } + } } } - - + + if(!found) { throw new Error("Import package " + importPath + " in " + currentSource - + " cannot be resolved."); + + " cannot be resolved."); } } - public void parseInitializers(ClassDescriptor cn){ + public void parseInitializers(ClassDescriptor cn) { Vector fv=cn.getFieldVec(); int pos = 0; - for(int i=0;i locOrder = - new Lattice("_top_","_bottom_"); + new Lattice("_top_","_bottom_"); Set spinLocSet=new HashSet(); for (int i = 0; i < pnv.size(); i++) { ParseNode loc = pnv.elementAt(i); - if(isNode(loc,"location_property")){ - String spinLoc=loc.getChildren().elementAt(0).getLabel(); - spinLocSet.add(spinLoc); + if(isNode(loc,"location_property")) { + String spinLoc=loc.getChildren().elementAt(0).getLabel(); + spinLocSet.add(spinLoc); } else { - String lowerLoc=loc.getChildren().elementAt(0).getLabel(); - String higherLoc= loc.getChildren().elementAt(1).getLabel(); - locOrder.put(higherLoc, lowerLoc); - if (locOrder.isIntroducingCycle(higherLoc)) { - throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc - + " introduces a cycle."); - } + String lowerLoc=loc.getChildren().elementAt(0).getLabel(); + String higherLoc= loc.getChildren().elementAt(1).getLabel(); + locOrder.put(higherLoc, lowerLoc); + if (locOrder.isIntroducingCycle(higherLoc)) { + throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc + + " introduces a cycle."); + } } } - if(spinLocSet.size()>0){ + if(spinLocSet.size()>0) { //checking if location is actually defined in the hierarchy - for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) { - String locID = (String) iterator.next(); - if(!locOrder.containsKey(locID)){ - throw new Error("Error: The spinning location '"+ - locID + "' is not defined in the hierarchy of the class '"+cd +"'."); - } + for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext(); ) { + String locID = (String) iterator.next(); + if(!locOrder.containsKey(locID)) { + throw new Error("Error: The spinning location '"+ + locID + "' is not defined in the hierarchy of the class '"+cd +"'."); + } } state.addLocationPropertySet(cd, spinLocSet); } state.addLocationOrder(cd, locOrder); } - + private void parseClassMember(ClassDescriptor cn, ParseNode pn) { ParseNode fieldnode=pn.getChild("field"); if (fieldnode!=null) { @@ -695,7 +694,7 @@ public class BuildIR { } throw new Error(); } - + private ClassDescriptor parseInnerClassDecl(ClassDescriptor cn, ParseNode pn) { ClassDescriptor icn=new ClassDescriptor(pn.getChild("name").getTerminal(), false); icn.setImports(mandatoryImports); @@ -710,8 +709,8 @@ public class BuildIR { icn.setSuper(nd.toString()); } else { if (!(icn.getSymbol().equals(TypeUtil.ObjectClass)|| - icn.getSymbol().equals(TypeUtil.TagClass))) - icn.setSuper(TypeUtil.ObjectClass); + icn.getSymbol().equals(TypeUtil.TagClass))) + icn.setSuper(TypeUtil.ObjectClass); } // check inherited interfaces if (!isEmpty(pn.getChild("superIF").getTerminal())) { @@ -719,17 +718,17 @@ public class BuildIR { ParseNode snlist=pn.getChild("superIF").getChild("interface_type_list"); ParseNodeVector pnv=snlist.getChildren(); for(int i=0; i annotations=modifiers.getAnnotations(); for(int i=0; i slv = new Vector(); - for(int j=0; j slv = new Vector(); + for(int j=0; j