From 94e7017920dda8731a03d5311bbf970e8afa34e6 Mon Sep 17 00:00:00 2001 From: stephey Date: Wed, 20 Apr 2011 09:59:54 +0000 Subject: [PATCH] Work in progress for import packages. In this versio ONLY SINGLE IMPORTS and package declarations at a level OTHER THAN THE MAIN FILE work at the moment. A class Foo in package bar.tar would be internally renamed to bar___________tar___________Foo and that's basically how it works at the moment. Multi-imports (import on demand) to comeIR/ClassDescriptor.java --- Robust/src/IR/NameDescriptor.java | 17 +- Robust/src/IR/State.java | 5 + Robust/src/IR/Tree/BuildIR.java | 265 ++++++++++++++------------ Robust/src/IR/Tree/SemanticCheck.java | 215 +++++++++++---------- Robust/src/IR/TypeUtil.java | 23 +-- 5 files changed, 291 insertions(+), 234 deletions(-) diff --git a/Robust/src/IR/NameDescriptor.java b/Robust/src/IR/NameDescriptor.java index ded9d9e8..3814320e 100644 --- a/Robust/src/IR/NameDescriptor.java +++ b/Robust/src/IR/NameDescriptor.java @@ -5,13 +5,13 @@ public class NameDescriptor extends Descriptor { NameDescriptor nd; public NameDescriptor(NameDescriptor nd, String id) { super(nd.toString()+"."+id); - identifier=id; + identifier=getPathFromRootToHere(id); this.nd=nd; } public NameDescriptor(String id) { super(id); - identifier=id; + identifier=getPathFromRootToHere(id); nd=null; } @@ -29,7 +29,18 @@ public class NameDescriptor extends Descriptor { else return nd.getRoot(); } - + + public String getPathFromRootToHere(String id) { + String path = id; + NameDescriptor temp = this.nd; + while(temp!=null) { + path = temp.identifier + "___________" + path; + temp = temp.nd; + } + + return path; + } + public String toString() { if (nd==null) return identifier; diff --git a/Robust/src/IR/State.java b/Robust/src/IR/State.java index 6f4f8694..303d3e39 100644 --- a/Robust/src/IR/State.java +++ b/Robust/src/IR/State.java @@ -257,6 +257,11 @@ public class State { TypeDescriptor td=new TypeDescriptor(n); return td; } + + public static TypeDescriptor getTypeDescriptor(String n) { + TypeDescriptor td=new TypeDescriptor(n); + return td; + } public void addClass(ClassDescriptor tdn) { if (classes.contains(tdn.getSymbol())) diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index 9e2da3b6..0342f7bb 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -27,15 +27,14 @@ public class BuildIR { } } - Vector singleimports; + Hashtable singleimports; Vector multiimports; NameDescriptor packages; /** Parse the classes in this file */ public void parseFile(ParseNode pn, Set toanalyze,String sourcefile) { - singleimports=new Vector(); + singleimports= new Hashtable(); multiimports=new Vector(); - ParseNode ipn=pn.getChild("imports").getChild("import_decls_list"); if (ipn!=null) { ParseNodeVector pnv=ipn.getChildren(); @@ -44,105 +43,115 @@ public class BuildIR { NameDescriptor nd=parseName(pnimport.getChild("name")); // TODO need to implement if (isNode(pnimport,"import_single")) - singleimports.add(nd); + if(!singleimports.containsKey(nd.getIdentifier())) { + //map name to full name (includes package/directory + singleimports.put(nd.getIdentifier(), nd.getPathFromRootToHere(nd.getIdentifier())); + } else { + throw new Error("Error: class " + nd.getIdentifier() + " is already more than once in a single type import inside "+sourcefile); + } else + //TODO MULTI-IMPORTS! multiimports.add(nd); } } + + + ParseNode ppn=pn.getChild("packages").getChild("package"); + String packageName = null; if (ppn!=null) { - // TODO need to implement - packages=parseName(ppn.getChild("name")); + packageName = ppn.getChild("name").getChild("identifier").getTerminal(); } ParseNode tpn=pn.getChild("type_declaration_list"); - if (tpn!=null) { - ParseNodeVector pnv=tpn.getChildren(); - for(int i=0; i