diff options
author | mihaylov <mihaylov@epfl.ch> | 2004-02-23 18:45:52 +0000 |
---|---|---|
committer | mihaylov <mihaylov@epfl.ch> | 2004-02-23 18:45:52 +0000 |
commit | a634b2280f86d4884062c67b29879dc95eda8fd7 (patch) | |
tree | 7656516222f2c30a108b85f8539f2798c571f229 /sources/scalac/symtab | |
parent | b8d11d03ea509d75bab8601b38cfd820f43934ba (diff) | |
download | scala-a634b2280f86d4884062c67b29879dc95eda8fd7.tar.gz scala-a634b2280f86d4884062c67b29879dc95eda8fd7.tar.bz2 scala-a634b2280f86d4884062c67b29879dc95eda8fd7.zip |
- Major code cleanup.
- Use the modified msil.jar with the Is* properties implemented as
methods.
!!! Mobile users should update their copy of msil.jar from
~linuxsoft/apps/msil/msil.jar
Diffstat (limited to 'sources/scalac/symtab')
-rw-r--r-- | sources/scalac/symtab/classfile/CLRClassParser.java | 179 | ||||
-rw-r--r-- | sources/scalac/symtab/classfile/CLRPackageParser.java | 79 |
2 files changed, 160 insertions, 98 deletions
diff --git a/sources/scalac/symtab/classfile/CLRClassParser.java b/sources/scalac/symtab/classfile/CLRClassParser.java index 913484895e..57f0f3068e 100644 --- a/sources/scalac/symtab/classfile/CLRClassParser.java +++ b/sources/scalac/symtab/classfile/CLRClassParser.java @@ -12,8 +12,10 @@ import scalac.Global; import scalac.symtab.Symbol; import scalac.symtab.TermSymbol; import scalac.symtab.ClassSymbol; +import scalac.symtab.AliasTypeSymbol; import scalac.symtab.Scope; import scalac.symtab.Modifiers; +import scalac.symtab.Type.*; import scalac.util.Name; import scalac.util.Names; import scalac.util.Debug; @@ -27,6 +29,10 @@ public class CLRClassParser extends ClassParser { protected final CLRPackageParser importer; +// private static final int bindingFlags = BindingFlags.DeclaredOnly +// | BindingFlags.Instance | BindingFlags.Static +// | BindingFlags.Public | BindingFlags.NonPublic; + public CLRClassParser(Global global, CLRPackageParser importer) { super(global); this.importer = importer; @@ -34,23 +40,26 @@ public class CLRClassParser extends ClassParser { protected void doComplete(Symbol clazz) { try { doComplete0(clazz); } - catch (AssertionError e) { - System.err.println("While processing " + Debug.show(clazz)); - throw e; + catch (Throwable e) { + System.err.println("\nWhile processing " + Debug.show(clazz)); + e.printStackTrace(); + System.exit(1); } } protected void doComplete0(Symbol clazz) { clazz.owner().initialize(); //??? + long msec = System.currentTimeMillis(); + if (make == null) make = new JavaTypeCreator(global.definitions); - Type type = Type.GetType(clazz.fullNameString()); -// System.out.println("completing class " + clazz.fullNameString() -// + " from type " + type); + Type type = (Type)importer.getMember(clazz); + if (type == null) + type = Type.GetType(clazz.fullNameString()); clazz.flags = translateAttributes(type); - Type[] ifaces = type.GetInterfaces(); // FIXME: ensure only the declared interfaces are taken into account + Type[] ifaces = type.getInterfaces(); scalac.symtab.Type[] baseTypes = new scalac.symtab.Type[ifaces.length+1]; baseTypes[0] = type.BaseType == null ? global.definitions.ANYREF_TYPE() : getCLRType(type.BaseType); @@ -71,23 +80,66 @@ public class CLRClassParser extends ClassParser { staticsClass, scalac.symtab.Type.EMPTY_ARRAY)); } scalac.symtab.Type ctype = clazz.typeConstructor(); + + // import nested types + Type[] nestedTypes = type.getNestedTypes(); + for (int i = 0; i < nestedTypes.length; i++) { + Type ntype = nestedTypes[i]; + if (ntype.IsNestedPrivate() || ntype.IsNestedAssembly() + || ntype.IsNestedFamANDAssem()) + continue; + int j = ntype.FullName.lastIndexOf('.'); + String n = (j < 0 ? ntype.FullName : ntype.FullName.substring(j + 1)) + .replace('+', '#'); + Name classname = Name.fromString(n).toTypeName(); + Name aliasname = Name.fromString(ntype.Name).toTypeName(); + // put the class at the level of its outermost class + ClassSymbol nclazz = new ClassSymbol(classname, clazz.owner(), this); + importer.map(nclazz, ntype); + // create an alias in the module of the outer class + AliasTypeSymbol alias = + new AliasTypeSymbol(Position.NOPOS, aliasname, clazz.module(), + translateAttributes(ntype)); + nclazz.allConstructors().setInfo(staticsParser(nclazz)); + nclazz.module().setInfo(staticsParser(nclazz)); + // + alias.setInfo(scalac.symtab.Type + .typeRef(clazz.owner().thisType(), + nclazz, scalac.symtab.Type.EMPTY_ARRAY)); + alias.allConstructors() + .setInfo(MethodType(Symbol.EMPTY_ARRAY, nclazz.info())); +// statics.enter(nclazz); +// Scope.Entry e = statics.lookupEntry(clazz.module().name); +// if (e != Scope.Entry.NONE) +// statics.unlink(e); +// statics.enter(nclazz.module()); + + Scope.Entry e = statics.lookupEntry(alias.name); // Why is this ??? + if (e != Scope.Entry.NONE) + statics.unlink(e); + statics.enter(alias); + } + // read field information - int bindingFlags = BindingFlags.DeclaredOnly - | BindingFlags.Instance | BindingFlags.Static - | BindingFlags.Public | BindingFlags.NonPublic; - FieldInfo[] fields = type.GetFields(bindingFlags); + FieldInfo[] fields = type.getFields(); for (int i = 0; i < fields.length; i++) { + if (fields[i].IsPrivate() || fields[i].IsAssembly() + || fields[i].IsFamilyAndAssembly()) + continue; int mods = translateAttributes(fields[i]); Name name = Name.fromString(fields[i].Name); scalac.symtab.Type fieldType = getCLRType(fields[i].FieldType); - Symbol owner = fields[i].IsStatic ? staticsClass : clazz; + if (fields[i].IsLiteral()) + fieldType = make.constantType(fieldType, fields[i].getValue()); + Symbol owner = fields[i].IsStatic() ? staticsClass : clazz; Symbol field = new TermSymbol(Position.NOPOS, name, owner, mods); field.setFirstInfo(fieldType); - (fields[i].IsStatic ? statics : members).enterOrOverload(field); + (fields[i].IsStatic() ? statics : members).enterOrOverload(field); importer.map(field, fields[i]); } - PropertyInfo[] props = type.GetProperties(bindingFlags); + //PropertyInfo[] props = type.GetProperties(bindingFlags); + PropertyInfo[] props = type.getProperties(); for (int i = 0; i < props.length; i++) { MethodInfo getter = props[i].GetGetMethod(true); MethodInfo setter = props[i].GetSetMethod(true); @@ -97,22 +149,20 @@ public class CLRClassParser extends ClassParser { scalac.symtab.Type proptype = getCLSType(props[i].PropertyType); if (proptype == null) continue; -// if (type.FullName.equals("System.Collections.ArrayList")) -// System.out.println("getter found: " + getter); Name n = Name.fromString(props[i].Name); scalac.symtab.Type mtype = scalac.symtab.Type.PolyType(Symbol.EMPTY_ARRAY, proptype); int mods = translateAttributes(getter); - Symbol owner = getter.IsStatic ? staticsClass : clazz; + Symbol owner = getter.IsStatic() ? staticsClass : clazz; Symbol method = new TermSymbol(Position.NOPOS, n, owner, mods); setParamOwners(mtype, method); method.setFirstInfo(mtype); - (getter.IsStatic ? statics : members).enterOrOverload(method); + (getter.IsStatic() ? statics : members).enterOrOverload(method); importer.map(method, getter); if (setter == null) continue; - assert getter.IsStatic == setter.IsStatic; + assert getter.IsStatic() == setter.IsStatic(); assert setter.ReturnType == importer.VOID; mtype = methodType(setter, getCLSType(importer.VOID)); if (mtype == null) @@ -122,13 +172,17 @@ public class CLRClassParser extends ClassParser { method = new TermSymbol(Position.NOPOS, n, owner, mods); setParamOwners(mtype, method); method.setFirstInfo(mtype); - (setter.IsStatic ? statics : members).enterOrOverload(method); + (setter.IsStatic() ? statics : members).enterOrOverload(method); importer.map(method, setter); } - MethodInfo[] methods = type.GetMethods(bindingFlags); + //MethodInfo[] methods = type.GetMethods(bindingFlags); + MethodInfo[] methods = type.getMethods(); for (int i = 0; i < methods.length; i++) { - if (importer.getSymbol(methods[i]) != null) + if ((importer.getSymbol(methods[i]) != null) + || methods[i].IsPrivate() + || methods[i].IsAssembly() + || methods[i].IsFamilyAndAssembly()) continue; scalac.symtab.Type rettype = getCLSType(methods[i].ReturnType); if (rettype == null) @@ -143,25 +197,26 @@ public class CLRClassParser extends ClassParser { else if (name.equals("ToString")) n = Names.toString; else n = Name.fromString(name); int mods = translateAttributes(methods[i]); - Symbol owner = methods[i].IsStatic ? staticsClass : clazz; + Symbol owner = methods[i].IsStatic() ? staticsClass : clazz; Symbol method = new TermSymbol(Position.NOPOS, n, owner, mods); setParamOwners(mtype, method); method.setFirstInfo(mtype); - (methods[i].IsStatic ? statics : members).enterOrOverload(method); + (methods[i].IsStatic() ? statics : members).enterOrOverload(method); importer.map(method, methods[i]); } - ConstructorInfo[] constrs = type.GetConstructors(bindingFlags); + //ConstructorInfo[] constrs = type.GetConstructors(bindingFlags); + ConstructorInfo[] constrs = type.getConstructors(); for (int i = 0; i < constrs.length; i++) { - if (constrs[i].IsStatic || constrs[i].IsPrivate - || constrs[i].IsAssembly || constrs[i].IsFamilyAndAssembly) + if (constrs[i].IsStatic() || constrs[i].IsPrivate() + || constrs[i].IsAssembly() || constrs[i].IsFamilyAndAssembly()) continue; scalac.symtab.Type mtype = methodType(constrs[i], ctype); if (mtype == null) continue; Symbol constr = clazz.primaryConstructor(); if (constr.isInitialized()) constr = clazz.addConstructor(); - int mods = translateAttributes(methods[i]); + int mods = translateAttributes(constrs[i]); TermSymbol.newConstructor(clazz, mods).copyTo(constr); setParamOwners(mtype, constr); constr.setFirstInfo(mtype); @@ -178,19 +233,9 @@ public class CLRClassParser extends ClassParser { constr.flags |= Modifiers.PRIVATE; } - // import nested types - Type[] nestedTypes = type.GetNestedTypes(); - for (int i = 0; i < nestedTypes.length; i++) { - Name n = Name.fromString(nestedTypes[i].Name).toTypeName(); - ClassSymbol nclazz = new ClassSymbol(n, clazz, this); - nclazz.allConstructors().setInfo(staticsParser(nclazz)); - nclazz.module().setInfo(staticsParser(nclazz)); - members.enter(nclazz); - Scope.Entry e = members.lookupEntry(clazz.module().name); - if (e != Scope.Entry.NONE) - members.unlink(e); - members.enter(nclazz.module()); - } + global.operation("loaded class " + Debug.show(clazz) + + " from [" + type.Assembly.GetName().Name + "]" + type + + " in " + (System.currentTimeMillis() - msec) + "ms"); } /** Return a method type for */ @@ -221,7 +266,8 @@ public class CLRClassParser extends ClassParser { protected scalac.symtab.Type getClassType(Type type) { assert type != null; - scalac.symtab.Type res = make.classType(Name.fromString(type.FullName)); + scalac.symtab.Type res = + make.classType(Name.fromString(type.FullName.replace('+', '.'))); if (res == scalac.symtab.Type.ErrorType) global.error("unknown class reference " + type.FullName); return res; @@ -230,9 +276,11 @@ public class CLRClassParser extends ClassParser { protected scalac.symtab.Type getCLSType(Type type) { if (type == importer.BYTE || type == importer.USHORT || type == importer.UINT || type == importer.ULONG - || type.IsPointer - || (type.IsArray && getCLSType(type.GetElementType()) == null)) + || type.IsPointer() + || (type.IsArray() && getCLSType(type.GetElementType()) == null)) return null; + //Symbol s = importer.getSymbol(type); + //scalac.symtab.Type t = s != null ? s.typeConstructor() : getCLRType(type); return getCLRType(type); } @@ -259,51 +307,52 @@ public class CLRClassParser extends ClassParser { return make.floatType(); if (type == importer.DOUBLE) return make.doubleType(); - if (type.IsArray) + if (type.IsArray()) return make.arrayType(getCLRType(type.GetElementType())); - return getClassType(type); + Symbol s = importer.getSymbol(type); + return s != null ? s.typeConstructor() : getClassType(type); } protected static int translateAttributes(Type type) { - int mods = 0; - if (type.IsNotPublic || type.IsNestedPrivate - || type.IsNestedAssembly || type.IsNestedFamANDAssem) + int mods = Modifiers.JAVA; + if (type.IsNotPublic() || type.IsNestedPrivate() + || type.IsNestedAssembly() || type.IsNestedFamANDAssem()) mods |= Modifiers.PRIVATE; - else if (type.IsNestedFamily || type.IsNestedFamORAssem) + else if (type.IsNestedFamily() || type.IsNestedFamORAssem()) mods |= Modifiers.PROTECTED; - if (type.IsAbstract) + if (type.IsAbstract()) mods |= Modifiers.ABSTRACT; - if (type.IsSealed) + if (type.IsSealed()) mods |= Modifiers.FINAL; - if (type.IsInterface) + if (type.IsInterface()) mods |= Modifiers.INTERFACE | Modifiers.TRAIT | Modifiers.ABSTRACT; - return mods | Modifiers.JAVA; + return mods; } protected static int translateAttributes(FieldInfo field) { - int mods = 0; - if (field.IsPrivate || field.IsAssembly || field.IsFamilyAndAssembly) + int mods = Modifiers.JAVA; + if (field.IsPrivate() || field.IsAssembly() || field.IsFamilyAndAssembly()) mods |= Modifiers.PRIVATE; - else if (field.IsFamily || field.IsFamilyOrAssembly) + else if (field.IsFamily() || field.IsFamilyOrAssembly()) mods |= Modifiers.PROTECTED; - if (field.IsInitOnly) + if (field.IsInitOnly()) mods |= Modifiers.FINAL; else mods |= Modifiers.MUTABLE; - return mods | Modifiers.JAVA; + return mods; } - protected static int translateAttributes(MethodInfo method) { - int mods = 0; - if (method.IsPrivate || method.IsAssembly || method.IsFamilyAndAssembly) + protected static int translateAttributes(MethodBase method) { + int mods = Modifiers.JAVA; + if (method.IsPrivate() || method.IsAssembly() || method.IsFamilyAndAssembly()) mods |= Modifiers.PRIVATE; - else if (method.IsFamily || method.IsFamilyOrAssembly) + else if (method.IsFamily() || method.IsFamilyOrAssembly()) mods |= Modifiers.PROTECTED; - if (method.IsAbstract) + if (method.IsAbstract()) mods |= Modifiers.DEFERRED; - return mods | Modifiers.JAVA; + return mods; } } diff --git a/sources/scalac/symtab/classfile/CLRPackageParser.java b/sources/scalac/symtab/classfile/CLRPackageParser.java index 801197c925..7fec1879e5 100644 --- a/sources/scalac/symtab/classfile/CLRPackageParser.java +++ b/sources/scalac/symtab/classfile/CLRPackageParser.java @@ -78,27 +78,28 @@ public class CLRPackageParser extends MetadataParser { Assembly mscorlib = findAssembly("mscorlib.dll"); Type.initMSCORLIB(mscorlib); - BYTE = Type.GetType("System.SByte"); - UBYTE = Type.GetType("System.Byte"); - CHAR = Type.GetType("System.Char"); - SHORT = Type.GetType("System.Int16"); - USHORT = Type.GetType("System.UInt16"); - INT = Type.GetType("System.Int32"); - UINT = Type.GetType("System.UInt32"); - LONG = Type.GetType("System.Int64"); - ULONG = Type.GetType("System.UInt64"); - FLOAT = Type.GetType("System.Single"); - DOUBLE = Type.GetType("System.Double"); - BOOLEAN = Type.GetType("System.Boolean"); - VOID = Type.GetType("System.Void"); - - OBJECT = Type.GetType("System.Object"); - STRING = Type.GetType("System.String"); - STRING_ARRAY = Type.GetType("System.String[]"); + BYTE = getType("System.SByte"); + UBYTE = getType("System.Byte"); + CHAR = getType("System.Char"); + SHORT = getType("System.Int16"); + USHORT = getType("System.UInt16"); + INT = getType("System.Int32"); + UINT = getType("System.UInt32"); + LONG = getType("System.Int64"); + ULONG = getType("System.UInt64"); + FLOAT = getType("System.Single"); + DOUBLE = getType("System.Double"); + BOOLEAN = getType("System.Boolean"); + VOID = getType("System.Void"); + //ENUM = getType("System.Enum"); + + OBJECT = getType("System.Object"); + STRING = getType("System.String"); + STRING_ARRAY = getType("System.String[]"); findAssembly("vjslib.dll"); findAssembly("scala.dll"); - findAssembly("scalalib.dll"); + //findAssembly("scalalib.dll"); findAllAssemblies(); Type[] types = Type.EmptyTypes; @@ -115,22 +116,19 @@ public class CLRPackageParser extends MetadataParser { types = btypes; } this.types = types; - -// for (int i = 0; i < types.length; i++) -// System.out.println(types[i]); - initialized = true; } - java.util.Map syms2members = new HashMap(); - java.util.Map members2syms = new HashMap(); + private java.util.Map syms2members = new HashMap(); + private java.util.Map members2syms = new HashMap(); //########################################################################## public void map(Symbol sym, MemberInfo m) { syms2members.put(sym, m); members2syms.put(m, sym); + //System.out.println("" + sym + " => " + m); } public MemberInfo getMember(Symbol sym) { @@ -142,7 +140,9 @@ public class CLRPackageParser extends MetadataParser { } public Type getType(String name) { - return Type.GetType(name); + Type t = Type.GetType(name); + //assert t != null : name; + return t; } public Type mkArrayType(Type elemType) { @@ -158,33 +158,45 @@ public class CLRPackageParser extends MetadataParser { /** Load the assembly with the given name */ - Assembly findAssembly(String name) { + private Assembly findAssembly(String name) { // see if the assembly is referenced directly + File file = null; + Assembly assem = null; for (Iterator assems = assemrefs.iterator(); assems.hasNext();) { String assemname = (String)assems.next(); - File f = new File(assemname); - if (!f.getName().equals(name)) + file = new File(assemname); + if (!file.getName().equals(name)) continue; - Assembly assem = Assembly.LoadFrom(f.getPath()); + assem = Assembly.LoadFrom(file.getPath()); if (assem != null) { assems.remove(); assemblies.add(assem); return assem; } } + // look in directories specified with the '-r' option for (Iterator assems = assemrefs.iterator(); assems.hasNext();) { File d = new File((String)assems.next()); if (!d.isDirectory()) continue; - File f = new File(d, name); - if (f.exists()) { - Assembly assem = Assembly.LoadFrom(f.getPath()); + file = new File(d, name); + if (file.exists()) { + assem = Assembly.LoadFrom(file.getPath()); if (assem != null) { assemblies.add(assem); return assem; } } } + // try in the current directory + file = new File(".", name); + if (file.exists()) { + assem = Assembly.LoadFrom(file.getPath()); + if (assem != null) { + assemblies.add(assem); + return assem; + } + } global.fail("Cannot find assembly " + name + "; use the -r option to specify its location"); return null; @@ -192,7 +204,7 @@ public class CLRPackageParser extends MetadataParser { /** Load the rest of the assemblies specified with the '-r' option */ - void findAllAssemblies() { + private void findAllAssemblies() { //System.out.println("assembly references left: " + assemrefs); for (Iterator assems = assemrefs.iterator(); assems.hasNext();) { File f = new File((String)assems.next()); @@ -250,6 +262,7 @@ public class CLRPackageParser extends MetadataParser { if (e != Scope.Entry.NONE) members.unlink(e); members.enter(clazz.module()); + map(clazz, types[i]); } else { importCLRNamespace(name, p, members, pp); } |