summaryrefslogtreecommitdiff
path: root/sources/scalac/symtab/classfile/CLRPackageParser.java
blob: f6578a4aff6342c4ad1bac97250d90a5f249b873 (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    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.io.File;

import ch.epfl.lamp.compiler.msil.*;
import scalac.symtab.Symbol;
import scalac.symtab.SymbolLoader;
import scalac.symtab.Scope;
import scalac.util.Debug;
import scalac.util.Name;
import scalac.util.NameTransformer;
import scalac.Global;

/**
 */
public class CLRPackageParser extends SymbolLoader {

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

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

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

    protected CLRClassParser completer;

    private CLRPackageParser(Global global) {
	super(global);
    }

    public static CLRPackageParser instance;
    public static CLRPackageParser create(Global global) {
	if (instance != null)
	    return instance;
	instance = new CLRPackageParser(global);
	instance.completer = new CLRClassParser(global, instance);
	instance.init();
	return instance;
    }

    Type[] types;

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

    private boolean initialized = false;
    public void init() {
	if (initialized) return;
	String[] asnames = scalac.util.ClassPath.parse(global.args.assemrefs.value);
	for (int i = 0; i < asnames.length; i++)
	    assemrefs.add(asnames[i]);
	Assembly mscorlib = findAssembly("mscorlib.dll");
	Type.initMSCORLIB(mscorlib);

	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");
	findAllAssemblies();

	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++)
		if (/*atypes[i].IsPublic && */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;
	}
	this.types = types;
	initialized = true;
    }


    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) {
	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

    protected final java.util.List assemblies = new LinkedList();

    protected final java.util.List assemrefs = new LinkedList();

    /** Load the assembly with the given 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 = new File(assemname);
	    if (!file.getName().equals(name))
		continue;
	    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 = 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;
    }

    /** Load the rest of the assemblies specified with the '-r' option
     */
    private void findAllAssemblies() {
	//System.out.println("assembly references left: " + assemrefs);
	for (Iterator assems = assemrefs.iterator(); assems.hasNext();) {
	    File f = new File((String)assems.next());
	    if (f.isFile()) {
		Assembly assem = Assembly.LoadFrom(f.getPath());
		if (assem != null) {
		    assemblies.add(assem);
		    //System.out.println("Loaded assembly " + assem);
		}
	    }
	    assems.remove();
	}
	assert assemrefs.isEmpty();
    }

    //##########################################################################
    // main functionality

    protected String doComplete(Symbol p) {
	// for future use
        return "!!!";
    }

    /**
     */
    public void importCLRTypes(Symbol p, Scope members, PackageParser pp) {
	if (p.isRoot()) {
	    for (int i = 0; i < types.length; i++) {
		int j = types[i].FullName.indexOf('.');
		if (j < 0) continue;
		String namespace = types[i].FullName.substring(0, j);
		importCLRNamespace(namespace, p, members, pp);
	    }
	    return;
	}
        String n1 = "";
        for (Symbol q = p; !q.isRoot(); q = q.owner()) {
            n1 = NameTransformer.decode(q.name) + "." + n1;
            if (q.module() == global.definitions.JAVA
                || q.module() == global.definitions.SCALA)
                return;
        }
	for (int i = 0; i < types.length; i++) {
	    String fullname = types[i].FullName;
	    if (!fullname.startsWith(n1))
		continue;
	    //System.out.println("importing type: " + fullname);
	    int j = n1.length();
	    int k = fullname.indexOf('.', j);
	    String name = fullname.substring(j, k < 0 ? fullname.length() : k);
	    Name n = Name.fromString(name);
	    if (k < 0) {
		// it's a class
                Symbol clazz = p.newLoadedClass(JAVA, n.toTypeName(), completer, members);
		map(clazz, types[i]);
	    } else {
		importCLRNamespace(name, p, members, pp);
	    }
	}
    }

    /** Imports a CLR namespace as a scala package.
     */
    protected void importCLRNamespace(String namespace, Symbol p,
				   Scope members, PackageParser pp)
    {
	Name n = Name.fromString(namespace);
	if (members.lookup(n) == Symbol.NONE) {
	    //System.out.println("importing namespace " + namespace);
	    p.newLoadedPackage(n, pp, members);
	}
    }

    //##########################################################################
}