import eclipse 3.1 M4 compiler
[org.ibex.tool.git] / src / org / ibex / tool / Compiler.java
index 382cf74..7c771a6 100644 (file)
@@ -385,171 +385,11 @@ public class Compiler {
         }
     }
 
-    // ClassLoader Wrappers ///////////////////////////////////////////////////
-
-    final static class LoadedNestedType implements IBinaryNestedType {
-        private Class c;
-        LoadedNestedType(Class c) { this.c = c; }
-        public char[] getName() { return name(c); }
-        public char[] getEnclosingTypeName() { return name(c.getDeclaringClass()); }
-        public int getModifiers() { return c.getModifiers(); }
-    }
-
-    final static class LoadedField implements IBinaryField {
-        private Field f;
-        private Constant c;
-
-        LoadedField(Field f) {
-            this.f = f;
-            int m = f.getModifiers();
-
-            c = Constant.NotAConstant;
-            if (Modifier.isFinal(m) && Modifier.isStatic(m)) {
-                try {
-                    Class type = f.getType();
-                    if (type == Boolean.TYPE) {
-                        c = new BooleanConstant(f.getBoolean(null));
-                    } else if (type == Byte.TYPE) {
-                        c = new ByteConstant(f.getByte(null));
-                    } else if (type == Character.TYPE) {
-                        c = new CharConstant(f.getChar(null));
-                    } else if (type == Double.TYPE) {
-                        c = new DoubleConstant(f.getDouble(null));
-                    } else if (type == Float.TYPE) {
-                        c = new FloatConstant(f.getFloat(null));
-                    } else if (type == Integer.TYPE) {
-                        c = new IntConstant(f.getInt(null));
-                    } else if (type == Long.TYPE) {
-                        c = new LongConstant(f.getLong(null));
-                    } else if (type == Short.TYPE) {
-                        c = new ShortConstant(f.getShort(null));
-                    } else if (type == String.class) {
-                        c = new StringConstant((String)f.get(null));
-                    }
-                } catch (IllegalAccessException e) {}
-            }
-        }
-        public Constant getConstant() { return c; }
-        public char[] getTypeName() { return typeName(f.getType()); }
-        public char[] getName() { return f.getName().toCharArray(); }
-        public int getModifiers() { return f.getModifiers(); }
-    }
-
-    final static class LoadedConstructor implements IBinaryMethod {
-        private Constructor c;
-        private char[][] ex;
-        private char[] desc;
-
-        LoadedConstructor(Constructor c) {
-            this.c = c;
-
-            Class[] exs = c.getExceptionTypes();
-            ex = new char[exs.length][];
-            for (int i=0; i < ex.length; i++) ex[i] = name(exs[i]);
-
-            desc = descriptor(c.getParameterTypes(), null);
-        }
-        public int getModifiers() { return c.getModifiers(); }
-        public char[] getSelector() { return c.getName().toCharArray(); }
-        public boolean isConstructor() { return true; }
-        public boolean isClinit() { return false; }
-        public char[][] getArgumentNames() { return null; }
-        public char[][] getExceptionTypeNames() { return ex; }
-        public char[] getMethodDescriptor() { return desc; }
-    }
-
-    final static class LoadedMethod implements IBinaryMethod {
-        private Method m;
-        private char[][] ex;
-        private char[] desc;
-
-        LoadedMethod(Method m) {
-            this.m = m;
-
-            Class[] exs = m.getExceptionTypes();
-            ex = new char[exs.length][];
-            for (int i=0; i < ex.length; i++) ex[i] = name(exs[i]);
-
-            desc = descriptor(m.getParameterTypes(), m.getReturnType());
-        }
-        public int getModifiers() { return m.getModifiers(); }
-        public char[] getSelector() { return m.getName().toCharArray(); }
-        public boolean isConstructor() { return false; }
-        public boolean isClinit() { return false; }
-        public char[][] getArgumentNames() { return null; } // FEATURE: does this do anything cool?
-        public char[][] getExceptionTypeNames() { return ex; }
-        public char[] getMethodDescriptor() { return desc; }
-    }
-
-    final static class LoadedClass implements IBinaryType {
-        private Class c;
-        private IBinaryField[] f;
-        private char[][] inf;
-        private IBinaryNestedType[] nested;
-        private IBinaryMethod[] meth = null;
-
-        LoadedClass(Class c) {
-            this.c = c;
-
-            Field[] fields = c.getDeclaredFields();
-            List flist = new ArrayList(fields.length);
-            for (int i=0; i < fields.length; i++)
-                if (!Modifier.isPrivate(fields[i].getModifiers()))
-                    flist.add(new LoadedField(fields[i]));
-            f = new IBinaryField[flist.size()];
-            flist.toArray(f);
-
-            Class[] interfaces = c.getInterfaces();
-            inf = new char[interfaces.length][];
-            for (int i=0; i < inf.length; i++) inf[i] = name(interfaces[i]);
-
-            Class[] classes = c.getClasses();
-            nested = new IBinaryNestedType[classes.length];
-            for (int i=0; i < nested.length; i++)
-                nested[i] = new LoadedNestedType(classes[i]);
-
-            Constructor[] constructors = c.getDeclaredConstructors();
-            Method[] methods = c.getDeclaredMethods();
-            if (methods.length + constructors.length > 0) {
-                List m = new ArrayList(methods.length + constructors.length);
-                for (int j=0; j < methods.length; j++)
-                    if (!Modifier.isPrivate(methods[j].getModifiers()))
-                        m.add(new LoadedMethod(methods[j]));
-                for (int j=0; j < constructors.length; j++)
-                    if (!Modifier.isPrivate(constructors[j].getModifiers()))
-                        m.add(new LoadedConstructor(constructors[j]));
-                meth = new IBinaryMethod[m.size()]; m.toArray(meth);
-            }
-        }
-
-        public char[] getName() { return name(c); }
-        public char[] getEnclosingTypeName() { return name(c.getDeclaringClass()); }
-        public char[] getSuperclassName() { return name(c.getSuperclass()); }
-        public IBinaryField[] getFields() { return f; }
-        public char[][] getInterfaceNames() { return inf; }
-        public IBinaryNestedType[] getMemberTypes() { return nested; }
-        public IBinaryMethod[] getMethods() { return meth; }
-        public int getModifiers() { return c.getModifiers(); }
-
-        public boolean isBinaryType() { return true; }
-        public boolean isClass() { return true; }
-        public boolean isInterface() { return c.isInterface(); }
-        public boolean isAnonymous() { return false; }
-        public boolean isLocal() { return false; } // FIXME
-        public boolean isMember() { return false; } // FIXME
-        public char[] sourceFileName() { return null; }
-        public char[] getFileName() { return null; }
-
-        public boolean equals(Object o) {
-            return o == this || super.equals(o) ||
-                   (o != null && o instanceof LoadedClass &&
-                    c.equals(((LoadedClass)o).c));
-        }
-    }
-
-
     // Compiler Parameters ////////////////////////////////////////////////////
 
+    // FEATURE: may be able to use this to block access to APIs generated for stack objects
+    final AccessRestriction access = new AccessRestriction(null, null, null, null);
+
     /** Used by compiler to resolve classes. */
     final INameEnvironment env = new INameEnvironment() {
         public NameEnvironmentAnswer findType(char[][] c) { return findType(name(c), pack(c)); }
@@ -557,12 +397,23 @@ public class Compiler {
             if (veryverbose) System.out.print("findType: "+ str(p, '.') + "."+new String(n));
 
             try {
-                Class c = Class.forName(str(p, '.') + '.' + new String(n));
+                String classname = new String(n);
+                Class c = Class.forName(str(p, '.') + '.' + classname);
                 if (veryverbose) System.out.println("  found in classloader: "+ c);
                 IBinaryType b = (IBinaryType)loaded.get(c);
-                if (b == null) loaded.put(c, b = new LoadedClass(c));
-                return new NameEnvironmentAnswer(b);
-            } catch (ClassNotFoundException e) {}
+                if (b == null) loaded.put(c, b = new ClassFileReader(
+                    read(c.getResourceAsStream(classname)),
+                    (str(p, '/') + '/' + classname).toCharArray()));
+                return new NameEnvironmentAnswer(b, access);
+            } catch (ClassNotFoundException e) {
+            } catch (ClassFormatException e) {
+                e.printStackTrace();
+                throw new Error("ClassFormatException reading system class: " +
+                    str(p, '.')+new String(n));
+            } catch (IOException e) {
+                e.printStackTrace();
+                throw new Error("IOException reading system class: "+str(p, '.')+new String(n));
+            }
 
             // cut out searches for java.* packages in sources list
             if (p.length > 0 && eq(p[0], "java".toCharArray())) {
@@ -576,16 +427,16 @@ public class Compiler {
                     if (eq(n, s.n) && eq(p, s.p)) {
                         if (s.reader != null) {
                             if (veryverbose) System.out.println("  found reader");
-                            return new NameEnvironmentAnswer(s.reader);
+                            return new NameEnvironmentAnswer(s.reader, access);
                         }
                         if (s.compiled != null) {
                             if (veryverbose) System.out.println("  found compiled, new reader");
                             s.reader = new ClassFileReader(s.compiled,
-                                s.orig.getName().toCharArray(), true);
-                            return new NameEnvironmentAnswer(s.reader);
+                                s.orig.getName().toCharArray());
+                            return new NameEnvironmentAnswer(s.reader, access);
                         }
                         if (veryverbose) System.out.println("  found unit");
-                        return new NameEnvironmentAnswer(sources[i].unit);
+                        return new NameEnvironmentAnswer(sources[i].unit, access);
                     }
                 }
             } catch (ClassFormatException e) {
@@ -703,7 +554,7 @@ public class Compiler {
 
     /** Convert source file path into class name block.
      *  eg. in:  java/lang/String.java -> { {java} {lang} {String} } */
-    private char[][] classname(String name) {
+    private static char[][] classname(String name) {
         String delim = name.indexOf('/') == -1 ? "." : "/";
         name = name.trim();
         if (name.endsWith(".java")) name = name.substring(0, name.length() - 5);
@@ -801,4 +652,23 @@ public class Compiler {
         return sb.toString().toCharArray();
     }
 
+    private static byte[] read(InputStream in) throws IOException {
+        byte[] bytes = new byte[16];
+        int off = 0, ret;
+        while ((ret = in.read(bytes, off, bytes.length - off)) != -1) {
+            off += ret;
+            if (off == bytes.length) {
+                byte[] nb = new byte[bytes.length * 2];
+                System.arraycopy(bytes, 0, nb, 0, bytes.length);
+                bytes = nb;
+            }
+        }
+        if (off != bytes.length) {
+            byte[] nb = new byte[off];
+            System.arraycopy(bytes, 0, nb, 0, off);
+            bytes = nb;
+        }
+        return bytes;
+    }
+
 }