summaryrefslogtreecommitdiff
path: root/sources/scalac/symtab/classfile/CLRPackageParser.java
blob: f172e553d5b299d9d188c63c35fb9028395f7a60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.symtab.classfile;

import java.util.List;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Arrays;
import java.util.Comparator;

import java.io.File;

import scala.tools.util.AbstractFile;
import scala.tools.util.ByteArrayFile;

import scalac.Global;
import scalac.CompilerCommand;
import scalac.util.Debug;
import scalac.util.Name;
import scalac.util.NameTransformer;
import scalac.symtab.Modifiers;
import scalac.symtab.Symbol;
import scalac.symtab.SymbolLoader;
import scalac.symtab.Scope;
import scalac.symtab.SymbolNameWriter;

import ch.epfl.lamp.compiler.msil.*;
/**
 */
public class CLRPackageParser {

    //##########################################################################

    private static CLRPackageParser instance;

    /** Return the unique instance of the CLRPackageParser class */
    public static CLRPackageParser instance() {
	assert instance != null;
	return instance;
    }

    /** Initialize the CLRPackageParser */
    public static void init(CompilerCommand args) {
	instance = new CLRPackageParser(args);
    }

    //##########################################################################

    public final Type BYTE;
    public final Type UBYTE;
    public final Type CHAR;
    public final Type SHORT;
    public final Type USHORT;
    public final Type INT;
    public final Type UINT;
    public final Type LONG;
    public final Type ULONG;
    public final Type FLOAT;
    public final Type DOUBLE;
    public final Type BOOLEAN;
    public final Type VOID;
    public final Type ENUM;

    public final Type OBJECT;
    public final Type STRING;
    public final Type STRING_ARRAY;

    public final Type SCALA_SYMTAB_ATTR;

    private final SymbolNameWriter snw = new SymbolNameWriter();

    private Type[] types;

    private CLRPackageParser(CompilerCommand args) {
	scala.tools.util.ClassPath.addFilesInPath(
            assemrefs, args.assemrefs.value);
	Assembly mscorlib = findAssembly("mscorlib.dll");
	Type.initMSCORLIB(mscorlib);
	findAssembly("vjslib.dll");
	findAssembly("scala.dll");

	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[]");

	SCALA_SYMTAB_ATTR = Type.GetType("scala.support.SymtabAttribute");

	Type[] types = Type.EmptyTypes;
	Iterator as = assemblies.iterator();
	while (as.hasNext()) {
	    Type[] atypes = ((Assembly)as.next()).GetTypes();
	    int j = 0;
	    for (int i = 0; i < atypes.length; i++)
		// skip nested types
		if (!atypes[i].IsNotPublic() && atypes[i].DeclaringType == null)
		    atypes[j++] = atypes[i];
	    Type[] btypes = new Type[types.length + j];
	    System.arraycopy(types, 0, btypes, 0, types.length);
	    System.arraycopy(atypes, 0, btypes, types.length, j);
	    types = btypes;
	}
	 Comparator typeNameComparator = new Comparator() {
		 public int compare(Object o1, Object o2) {
		     Type t1 = (Type)o1;
		     Type t2 = (Type)o2;
		     return t1.FullName.compareTo(t2.FullName);
		 }
	     };

	Arrays.sort(types, typeNameComparator);
	this.types = types;
    }

    //##########################################################################

    private java.util.Map syms2members = new HashMap();
    private java.util.Map members2syms = new HashMap();

    public Type[] getTypes() { return types; }

    public void map(Symbol sym, MemberInfo m) {
	syms2members.put(sym, m);
	members2syms.put(m, sym);
    }

    public MemberInfo getMember(Symbol sym) {
	return (MemberInfo)syms2members.get(sym);
    }

    public Symbol getSymbol(MemberInfo m) {
	return (Symbol)members2syms.get(m);
    }

    public Type getType(String name) {
	Type t = Type.GetType(name);
	//assert t != null : name;
	return t;
    }

    public Type mkArrayType(Type elemType) {
	return getType(elemType.FullName + "[]");
    }

    //##########################################################################
    // assembly loading methods

    // a list of all loaded assemblies
    protected final java.util.List assemblies = new LinkedList();

    // a set of all directories and assembly files
    protected final Set/*<File>*/ assemrefs = new LinkedHashSet();

    /** Load the assembly with the given name
     */
    private Assembly findAssembly(String name) {
	// see if the assembly is referenced directly
	for (Iterator assems = assemrefs.iterator(); assems.hasNext();) {
	    File file = (File)assems.next();
	    if (!file.getName().equals(name))
		continue;
	    Assembly 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 = (File)assems.next();
	    if (!d.isDirectory())
		continue;
	    File file = new File(d, name);
	    if (file.exists()) {
		Assembly assem = Assembly.LoadFrom(file.getPath());
		if (assem != null) {
		    assemblies.add(assem);
		    return assem;
		}
	    }
	}
	// try in the current directory
	File file = new File(".", name);
	if (file.exists()) {
	    Assembly assem = Assembly.LoadFrom(file.getPath());
	    if (assem != null) {
		assemblies.add(assem);
		return assem;
	    }
	}
	System.err.println("Cannot find assembly " + name
			   + "; use the -r option to specify its location");
	throw Debug.abort();
    }

    /** Load the rest of the assemblies specified with the '-r' option
     */
    private void findAllAssemblies() {
	for (Iterator assems = assemrefs.iterator(); assems.hasNext();) {
	    File f = (File)assems.next();
	    if (f.isFile()) {
		Assembly assem = Assembly.LoadFrom(f.getPath());
		if (assem != null) {
		    assemblies.add(assem);
		}
	    }
	    assems.remove();
	}
	assert assemrefs.isEmpty();
    }

    //##########################################################################
    // collect the members contained in a given namespace

    private Symbol pakage;
    private HashMap/*<String,Type>*/ typesMap;
    private Set/*<String>*/ namespacesSet;

    private static final String[] BANNED = new String[] {
	"scala.AnyVal", "scala.Array", "scala.Boolean", "scala.Byte",
	"scala.Char", "scala.Double", "scala.Float", "scala.Function0",
	"scala.Function1", "scala.Function2", "scala.Function3",
	"scala.Function4", "scala.Function5", "scala.Function6",
	"scala.Function7", "scala.Function8", "scala.Function9", "scala.Int",
	"scala.Long", "scala.MatchError", "scala.Ref", "scala.ScalaObject",
	"scala.Short", "scala.Type", "scala.Unit", "scala.runtime.NativeLoop",
	"scala.runtime.ResultOrException", "scala.runtime.RunTime",
	"java.lang.Object", "java.lang.String", "java.lang.CharSequence",
	"java.lang.StringBuffer", "java.lang.Byte", "java.lang.Float",
	"java.lang.Double", "java.lang.Cloneable"
    };
    private static final Set BANNED_TYPES = new HashSet();
    static {
	for (int i = 0; i < BANNED.length; i++) {
	    BANNED_TYPES.add(BANNED[i]);
	}
    }

    /** Find the position of the first type whose name starts with
     *  the given prefix; return the length of the types array if no match
     *  is found so the result can be used to terminate loop conditions
     */
    private int findFirst(String prefix) {
	int m = 0, n = types.length - 1;
	while (m < n) {
	    int l = (m + n) / 2;
	    int res = types[l].FullName.compareTo(prefix);
	    if (res < 0) m = l + 1;
	    else n = l;
	}
	return types[m].FullName.startsWith(prefix) ? m : types.length;
    }

    /** Collects the members contained in the given Scala package (namespace)
     */
    private void enumerateMembers(Symbol pakage) {
	if (this.pakage == pakage)
	    return;
	this.pakage = pakage;
	typesMap = new HashMap();
	namespacesSet = new LinkedHashSet();

	String namespace = pakage.isRoot() ? "" : snw.toString(pakage) + ".";
	int nl = namespace.length();
	for (int i = findFirst(namespace);
	     i < types.length && types[i].FullName.startsWith(namespace);
	     i++)
	{
	    Type type = types[i];
	    if (BANNED_TYPES.contains(type.FullName)) {
		//System.out.println("Skipping CLR type " + type);
		continue;
	    }
	    int k = type.FullName.indexOf(".", nl);
	    if (k < 0) {
		typesMap.put(type.Name, type);
	    } else {
		namespacesSet.add(type.Namespace.substring(nl, k));
	    }
	}
    }

    /** return a mapping from type names to types contained
     *  in the given Scala package (namespace)
     */
    HashMap getTypes(Symbol pakage) {
	enumerateMembers(pakage);
	return typesMap;
    }

    /** Returns a set of the namespaces contained
     *  in the given Scala package (namespace)
     */
    Set getNamespaces(Symbol pakage) {
	enumerateMembers(pakage);
	return namespacesSet;
    }

    boolean shouldLoadClassfile(Symbol pakage, String name) {
	String fullname = snw.toString(pakage) + "." + name;
	return BANNED_TYPES.contains(fullname);
    }

    /** Imports a CLR type in a scala package (only called from PackageParser)
     */
    void importType(Type type, Symbol pakage, Scope members) {
	// discard top level types
	if (type.Namespace.equals("")) {
	    return;
	}

	assert snw.toString(pakage).equals(type.Namespace)
	    : Debug.show(pakage) + " << " + type.FullName;
	Object[] symtab_attr=type.GetCustomAttributes(SCALA_SYMTAB_ATTR, false);
	AbstractFile symtab = null;
	for (int l = 0; l < symtab_attr.length; l++) {
	    MemberInfo.Attribute a = (MemberInfo.Attribute)symtab_attr[l];
	    if (a.GetType() == SCALA_SYMTAB_ATTR) {
		symtab = new ByteArrayFile
		    (type.FullName, type.Assembly.GetName().toString(),
		     a.getValue());
		break;
	    }
	}
	SymbolLoader loader = symtab == null ? completer()
	    : new SymblParser(Global.instance, symtab);

	Name classname = Name.fromString(type.Name).toTypeName();
	Symbol clazz =
	    pakage.newLoadedClass(Modifiers.JAVA, classname, loader, members);
	Type moduleType = getType(type.FullName + "$");
	map(clazz, type);
// 	map(clazz, moduleType != null ? moduleType : type);
    }

    private CLRClassParser completer;
    private CLRClassParser completer() {
	if (completer == null)
	    completer = new CLRClassParser(Global.instance, this);
	return completer;
    }


    //##########################################################################
} // CLRPackageParser