0
|
1 module dmd.Module;
|
|
2
|
|
3 import dmd.Package;
|
|
4 import dmd.DsymbolTable;
|
|
5 import dmd.backend.TYM;
|
|
6 import dmd.Array;
|
|
7 import dmd.StaticDtorDeclaration;
|
|
8 import dmd.Scope;
|
|
9 import dmd.Id;
|
|
10 import dmd.Import;
|
|
11 import dmd.ClassDeclaration;
|
|
12 import dmd.ModuleDeclaration;
|
|
13 import dmd.File;
|
|
14 import dmd.Identifier;
|
|
15 import dmd.Dsymbol;
|
|
16 import dmd.ModuleInfoDeclaration;
|
|
17 import dmd.FuncDeclaration;
|
|
18 import dmd.Loc;
|
|
19 import dmd.Macro;
|
|
20 import dmd.Escape;
|
|
21 import dmd.OutBuffer;
|
|
22 import dmd.HdrGenState;
|
|
23 import dmd.ArrayTypes;
|
|
24 import dmd.FileName;
|
|
25 import dmd.Global;
|
|
26 import dmd.Parser;
|
|
27 import dmd.Lexer;
|
|
28 import dmd.Util;
|
|
29 import dmd.String;
|
|
30 import dmd.ScopeDsymbol;
|
|
31 import dmd.Type;
|
|
32 import dmd.backend.TYPE;
|
|
33 import dmd.backend.Cstate;
|
|
34 import dmd.backend.OPER;
|
|
35 import dmd.backend.REG;
|
|
36 import dmd.backend.Symbol;
|
|
37 import dmd.backend.elem;
|
|
38 import dmd.backend.mTYman;
|
|
39 import dmd.backend.Util;
|
|
40 import dmd.backend.SC;
|
|
41 import dmd.backend.FL;
|
|
42 import dmd.backend.SFL;
|
|
43 import dmd.backend.TF;
|
|
44 import dmd.backend.RTLSYM;
|
|
45 import dmd.backend.BC;
|
|
46 import dmd.backend.block;
|
|
47 import dmd.backend.targ_types;
|
|
48 import dmd.backend.dt_t;
|
|
49 import dmd.backend.TYM;
|
|
50 import dmd.backend.Util;
|
|
51 import dmd.backend.Classsym;
|
|
52 import dmd.backend.glue;
|
|
53 import dmd.backend.LIST;
|
|
54 import dmd.codegen.Util;
|
|
55
|
|
56 import core.stdc.string;
|
|
57 import core.stdc.stdlib;
|
|
58
|
4
|
59 import core.memory;
|
2
|
60
|
0
|
61 uint readwordLE(ushort* p)
|
|
62 {
|
|
63 version (__I86__) {
|
|
64 return *p;
|
|
65 } else {
|
|
66 return ((cast(ubyte*)p)[1] << 8) | (cast(ubyte*)p)[0];
|
|
67 }
|
|
68 }
|
|
69
|
|
70 uint readwordBE(ushort* p)
|
|
71 {
|
|
72 return ((cast(ubyte*)p)[0] << 8) | (cast(ubyte*)p)[1];
|
|
73 }
|
|
74
|
|
75 uint readlongLE(uint* p)
|
|
76 {
|
|
77 version (__I86__) {
|
|
78 return *p;
|
|
79 } else {
|
|
80 return (cast(ubyte*)p)[0] |
|
|
81 ((cast(ubyte*)p)[1] << 8) |
|
|
82 ((cast(ubyte*)p)[2] << 16) |
|
|
83 ((cast(ubyte*)p)[3] << 24);
|
|
84 }
|
|
85 }
|
|
86
|
|
87 uint readlongBE(uint* p)
|
|
88 {
|
|
89 return (cast(ubyte*)p)[3] |
|
|
90 ((cast(ubyte*)p)[2] << 8) |
|
|
91 ((cast(ubyte*)p)[1] << 16) |
|
|
92 ((cast(ubyte*)p)[0] << 24);
|
|
93 }
|
|
94
|
|
95 /* Segments */
|
|
96 enum Segment {
|
|
97 CODE = 1, /* code segment */
|
|
98 DATA = 2, /* initialized data */
|
|
99 CDATA = 3, /* constant data */
|
|
100 UDATA = 4, /* uninitialized data */
|
|
101 UNKNOWN = -1, /* unknown segment */
|
|
102 }
|
|
103
|
|
104 struct seg_data
|
|
105 {
|
|
106 int SDseg; // omf file segment index
|
|
107 targ_size_t SDoffset; // starting offset for data
|
|
108
|
|
109 bool isfarseg;
|
|
110 int seg; // segment number
|
|
111 int lnameidx; // lname idx of segment name
|
|
112 int classidx; // lname idx of class name
|
|
113 uint attr; // segment attribute
|
|
114 targ_size_t origsize; // original size
|
|
115 int seek; // seek position in output file
|
|
116 }
|
|
117
|
|
118 extern (C) extern __gshared seg_data** SegData;
|
|
119
|
|
120 ref targ_size_t Offset(Segment seg) {
|
|
121 return SegData[seg].SDoffset;
|
|
122 }
|
|
123
|
|
124 ref targ_size_t Doffset() {
|
|
125 return SegData[Segment.DATA].SDoffset;
|
|
126 }
|
|
127
|
|
128 ref targ_size_t CDoffset() {
|
|
129 return SegData[Segment.CDATA].SDoffset;
|
|
130 }
|
|
131
|
|
132 ref targ_size_t UDoffset() {
|
|
133 return SegData[Segment.UDATA].SDoffset;
|
|
134 }
|
|
135
|
|
136 enum CF {
|
|
137 CFes = 1, // generate an ES: segment override for this instr
|
|
138 CFjmp16 = 2, // need 16 bit jump offset (long branch)
|
|
139 CFtarg = 4, // this code is the target of a jump
|
|
140 CFseg = 8, // get segment of immediate value
|
|
141 CFoff = 0x10, // get offset of immediate value
|
|
142 CFss = 0x20, // generate an SS: segment override (not with
|
|
143 // CFes at the same time, though!)
|
|
144 CFpsw = 0x40, // we need the flags result after this instruction
|
|
145 CFopsize = 0x80, // prefix with operand size
|
|
146 CFaddrsize = 0x100, // prefix with address size
|
|
147 CFds = 0x200, // need DS override (not with es, ss, or cs )
|
|
148 CFcs = 0x400, // need CS override
|
|
149 CFfs = 0x800, // need FS override
|
|
150 CFgs = (CFcs | CFfs), // need GS override
|
|
151 CFwait = 0x1000, // If I32 it indicates when to output a WAIT
|
|
152 CFselfrel = 0x2000, // if self-relative
|
|
153 CFunambig = 0x4000, // indicates cannot be accessed by other addressing
|
|
154 // modes
|
|
155 CFtarg2 = 0x8000, // like CFtarg, but we can't optimize this away
|
|
156 CFvolatile = 0x10000, // volatile reference, do not schedule
|
|
157 CFclassinit = 0x20000, // class init code
|
|
158
|
|
159 CFSEG = (CFes | CFss | CFds | CFcs | CFfs | CFgs),
|
|
160 CFPREFIX = (CFSEG | CFopsize | CFaddrsize),
|
|
161 }
|
|
162
|
|
163 class Module : Package
|
|
164 {
|
|
165 static Module rootModule;
|
|
166 static DsymbolTable modules; // symbol table of all modules
|
|
167 static Array amodules; // array of all modules
|
|
168 static Array deferred; // deferred Dsymbol's needing semantic() run on them
|
|
169 static uint dprogress; // progress resolving the deferred list
|
|
170
|
|
171 static void init()
|
|
172 {
|
|
173 modules = new DsymbolTable();
|
|
174 amodules = new Array();
|
|
175 deferred = new Array();
|
|
176 }
|
|
177
|
|
178 static ClassDeclaration moduleinfo;
|
|
179
|
|
180
|
|
181 string arg; // original argument name
|
|
182 ModuleDeclaration md; // if !null, the contents of the ModuleDeclaration declaration
|
|
183 File srcfile; // input source file
|
|
184 File objfile; // output .obj file
|
|
185 File hdrfile; // 'header' file
|
|
186 File symfile; // output symbol file
|
|
187 File docfile; // output documentation file
|
|
188 uint errors; // if any errors in file
|
|
189 uint numlines; // number of lines in source file
|
|
190 int isHtml; // if it is an HTML file
|
|
191 int isDocFile; // if it is a documentation input file, not D source
|
|
192 int needmoduleinfo; /// TODO: change to bool
|
|
193 version (IN_GCC) {
|
|
194 int strictlyneedmoduleinfo;
|
|
195 }
|
|
196
|
|
197 int selfimports; // 0: don't know, 1: does not, 2: does
|
|
198 int selfImports() // returns !=0 if module imports itself
|
|
199 {
|
|
200 assert(false);
|
|
201 }
|
|
202
|
|
203 int insearch;
|
|
204 Identifier searchCacheIdent;
|
|
205 Dsymbol searchCacheSymbol; // cached value of search
|
|
206 int searchCacheFlags; // cached flags
|
|
207
|
|
208 int semanticstarted; // has semantic() been started?
|
|
209 int semanticRun; // has semantic() been done?
|
|
210 int root; // != 0 if this is a 'root' module,
|
|
211 // i.e. a module that will be taken all the
|
|
212 // way to an object file
|
|
213 Module importedFrom; // module from command line we're imported from,
|
|
214 // i.e. a module that will be taken all the
|
|
215 // way to an object file
|
|
216
|
|
217 Array decldefs; // top level declarations for this Module
|
|
218
|
|
219 Array aimports; // all imported modules
|
|
220
|
|
221 ModuleInfoDeclaration vmoduleinfo;
|
|
222
|
|
223 uint debuglevel; // debug level
|
|
224 Array debugids; // debug identifiers
|
|
225 Array debugidsNot; // forward referenced debug identifiers
|
|
226
|
|
227 uint versionlevel; // version level
|
|
228 Array versionids; // version identifiers
|
|
229 Array versionidsNot; // forward referenced version identifiers
|
|
230
|
|
231 Macro macrotable; // document comment macros
|
|
232 Escape escapetable; // document comment escapes
|
|
233 bool safe; // TRUE if module is marked as 'safe'
|
|
234
|
|
235 this(string filename, Identifier ident, int doDocComment, int doHdrGen)
|
|
236 {
|
|
237 super(ident);
|
|
238 FileName objfilename;
|
|
239
|
|
240 aimports = new Array();
|
|
241
|
|
242 //writef("Module.Module(filename = '%s', ident = '%s')\n", filename, ident.toChars());
|
|
243 this.arg = filename;
|
|
244
|
|
245 FileName srcfilename = FileName.defaultExt(filename, global.mars_ext);
|
|
246 if (!srcfilename.equalsExt(global.mars_ext) &&
|
|
247 !srcfilename.equalsExt(global.hdr_ext) &&
|
|
248 !srcfilename.equalsExt("dd"))
|
|
249 {
|
|
250 if (srcfilename.equalsExt("html") ||
|
|
251 srcfilename.equalsExt("htm") ||
|
|
252 srcfilename.equalsExt("xhtml"))
|
|
253 {
|
|
254 if (!global.params.useDeprecated)
|
|
255 error("html source files is deprecated %s", srcfilename.toChars());
|
|
256 isHtml = 1;
|
|
257 }
|
|
258 else
|
|
259 {
|
|
260 error("source file name '%s' must have .%s extension", srcfilename.toChars(), global.mars_ext);
|
|
261 fatal();
|
|
262 }
|
|
263 }
|
|
264
|
|
265 string argobj;
|
|
266 if (global.params.objname)
|
|
267 argobj = global.params.objname;
|
|
268 else if (global.params.preservePaths)
|
|
269 argobj = filename;
|
|
270 else
|
|
271 argobj = FileName.name(filename);
|
|
272 if (!FileName.absolute(argobj))
|
|
273 {
|
|
274 argobj = FileName.combine(global.params.objdir, argobj);
|
|
275 }
|
|
276
|
|
277 if (global.params.objname)
|
|
278 objfilename = new FileName(argobj);
|
|
279 else
|
|
280 objfilename = FileName.forceExt(argobj, global.obj_ext);
|
|
281
|
|
282 FileName symfilename = FileName.forceExt(filename, global.sym_ext);
|
|
283
|
|
284 srcfile = new File(srcfilename);
|
|
285
|
|
286 if (doDocComment) {
|
|
287 setDocfile();
|
|
288 }
|
|
289
|
|
290 if (doHdrGen) {
|
|
291 setHdrfile();
|
|
292 }
|
|
293
|
|
294 objfile = new File(objfilename);
|
|
295 symfile = new File(symfilename);
|
|
296 }
|
|
297
|
|
298 static Module load(Loc loc, Array packages, Identifier ident)
|
|
299 {
|
|
300 Module m;
|
|
301 string filename;
|
|
302
|
|
303 //writef("Module.load(ident = '%s')\n", ident.toChars());
|
|
304
|
|
305 // Build module filename by turning:
|
|
306 // foo.bar.baz
|
|
307 // into:
|
|
308 // foo\bar\baz
|
|
309 filename = ident.toChars();
|
|
310 if (packages && packages.dim)
|
|
311 {
|
|
312 scope OutBuffer buf = new OutBuffer();
|
|
313 int i;
|
|
314
|
|
315 for (i = 0; i < packages.dim; i++)
|
|
316 {
|
|
317 Identifier pid = cast(Identifier)packages.data[i];
|
|
318
|
|
319 buf.writestring(pid.toChars());
|
|
320 version (_WIN32) {
|
|
321 buf.writeByte('\\');
|
|
322 } else {
|
|
323 buf.writeByte('/');
|
|
324 }
|
|
325 }
|
|
326 buf.writestring(filename);
|
|
327 filename = buf.extractString();
|
|
328 }
|
|
329
|
|
330 m = new Module(filename, ident, 0, 0);
|
|
331 m.loc = loc;
|
|
332
|
|
333 /* Search along global.path for .di file, then .d file.
|
|
334 */
|
|
335 string result = null;
|
|
336 FileName fdi = FileName.forceExt(filename, global.hdr_ext);
|
|
337 FileName fd = FileName.forceExt(filename, global.mars_ext);
|
|
338 string sdi = fdi.toChars();
|
|
339 string sd = fd.toChars();
|
|
340
|
|
341 if (FileName.exists(sdi)) {
|
|
342 result = sdi;
|
|
343 } else if (FileName.exists(sd)) {
|
|
344 result = sd;
|
|
345 } else if (FileName.absolute(filename)) {
|
|
346 ;
|
|
347 }
|
|
348 else
|
|
349 {
|
|
350 foreach (p; global.path)
|
|
351 {
|
|
352 string n = FileName.combine(p, sdi);
|
|
353
|
|
354 if (FileName.exists(n))
|
|
355 {
|
|
356 result = n;
|
|
357 break;
|
|
358 }
|
|
359
|
|
360 n = FileName.combine(p, sd);
|
|
361 if (FileName.exists(n))
|
|
362 {
|
|
363 result = n;
|
|
364 break;
|
|
365 }
|
|
366 }
|
|
367 }
|
|
368
|
|
369 if (result)
|
|
370 m.srcfile = new File(result);
|
|
371
|
|
372 if (global.params.verbose)
|
|
373 {
|
|
374 writef("import ");
|
|
375 if (packages)
|
|
376 {
|
|
377 for (size_t i = 0; i < packages.dim; i++)
|
|
378 {
|
|
379 Identifier pid = cast(Identifier)packages.data[i];
|
|
380 writef("%s.", pid.toChars());
|
|
381 }
|
|
382 }
|
|
383 writef("%s\t(%s)\n", ident.toChars(), m.srcfile.toChars());
|
|
384 }
|
|
385
|
|
386 m.read(loc);
|
|
387 m.parse();
|
|
388
|
|
389 version (IN_GCC) {
|
|
390 d_gcc_magic_module(m);
|
|
391 }
|
|
392
|
|
393 return m;
|
|
394 }
|
|
395
|
|
396 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
397 {
|
|
398 assert(false);
|
|
399 }
|
|
400
|
|
401 string kind()
|
|
402 {
|
|
403 return "module";
|
|
404 }
|
|
405
|
|
406 void setDocfile() // set docfile member
|
|
407 {
|
|
408 assert(false);
|
|
409 }
|
|
410
|
|
411 void read(Loc loc) // read file
|
|
412 {
|
|
413 //writef("Module.read('%s') file '%s'\n", toChars(), srcfile.toChars());
|
|
414 if (srcfile.read())
|
|
415 {
|
|
416 error(loc, "cannot read file '%s'", srcfile.toChars());
|
|
417 fatal();
|
|
418 }
|
|
419 }
|
|
420
|
|
421 version (IN_GCC) {
|
|
422 void parse(bool dump_source = false) // syntactic parse
|
|
423 {
|
|
424 assert(false);
|
|
425 }
|
|
426 } else {
|
|
427 void parse() // syntactic parse
|
|
428 {
|
|
429 uint le;
|
|
430 uint bom;
|
|
431
|
|
432 //printf("Module.parse()\n");
|
|
433
|
|
434 string srcname = srcfile.name.toChars();
|
|
435 //printf("Module.parse(srcname = '%s')\n", srcname);
|
|
436
|
|
437 ubyte* buf = srcfile.buffer;
|
|
438 uint buflen = srcfile.len;
|
|
439
|
|
440 if (buflen >= 2)
|
|
441 {
|
|
442 /* Convert all non-UTF-8 formats to UTF-8.
|
|
443 * BOM : http://www.unicode.org/faq/utf_bom.html
|
|
444 * 00 00 FE FF UTF-32BE, big-endian
|
|
445 * FF FE 00 00 UTF-32LE, little-endian
|
|
446 * FE FF UTF-16BE, big-endian
|
|
447 * FF FE UTF-16LE, little-endian
|
|
448 * EF BB BF UTF-8
|
|
449 */
|
|
450
|
|
451 bom = 1; // assume there's a BOM
|
|
452 if (buf[0] == 0xFF && buf[1] == 0xFE)
|
|
453 {
|
|
454 if (buflen >= 4 && buf[2] == 0 && buf[3] == 0)
|
|
455 { // UTF-32LE
|
|
456 le = 1;
|
|
457
|
|
458 Lutf32:
|
|
459 OutBuffer dbuf;
|
|
460 uint* pu = cast(uint*)buf;
|
|
461 uint* pumax = &pu[buflen / 4];
|
|
462
|
|
463 if (buflen & 3)
|
|
464 { error("odd length of UTF-32 char source %u", buflen);
|
|
465 fatal();
|
|
466 }
|
|
467
|
|
468 dbuf.reserve(buflen / 4);
|
|
469 for (pu += bom; pu < pumax; pu++)
|
|
470 {
|
|
471 uint u = le ? readlongLE(pu) : readlongBE(pu);
|
|
472 if (u & ~0x7F)
|
|
473 {
|
|
474 if (u > 0x10FFFF)
|
|
475 { error("UTF-32 value %08x greater than 0x10FFFF", u);
|
|
476 fatal();
|
|
477 }
|
|
478 dbuf.writeUTF8(u);
|
|
479 }
|
|
480 else
|
|
481 dbuf.writeByte(u);
|
|
482 }
|
|
483 dbuf.writeByte(0); // add 0 as sentinel for scanner
|
|
484 buflen = dbuf.offset - 1; // don't include sentinel in count
|
|
485 buf = cast(ubyte*) dbuf.extractData();
|
|
486 }
|
|
487 else
|
|
488 {
|
|
489 // UTF-16LE (X86)
|
|
490 // Convert it to UTF-8
|
|
491 le = 1;
|
|
492
|
|
493 Lutf16:
|
|
494 OutBuffer dbuf;
|
|
495 ushort* pu = cast(ushort*)(buf);
|
|
496 ushort *pumax = &pu[buflen / 2];
|
|
497
|
|
498 if (buflen & 1)
|
|
499 { error("odd length of UTF-16 char source %u", buflen);
|
|
500 fatal();
|
|
501 }
|
|
502
|
|
503 dbuf.reserve(buflen / 2);
|
|
504 for (pu += bom; pu < pumax; pu++)
|
|
505 {
|
|
506 uint u = le ? readwordLE(pu) : readwordBE(pu);
|
|
507 if (u & ~0x7F)
|
|
508 {
|
|
509 if (u >= 0xD800 && u <= 0xDBFF)
|
|
510 { uint u2;
|
|
511
|
|
512 if (++pu > pumax)
|
|
513 { error("surrogate UTF-16 high value %04x at EOF", u);
|
|
514 fatal();
|
|
515 }
|
|
516 u2 = le ? readwordLE(pu) : readwordBE(pu);
|
|
517 if (u2 < 0xDC00 || u2 > 0xDFFF)
|
|
518 { error("surrogate UTF-16 low value %04x out of range", u2);
|
|
519 fatal();
|
|
520 }
|
|
521 u = (u - 0xD7C0) << 10;
|
|
522 u |= (u2 - 0xDC00);
|
|
523 }
|
|
524 else if (u >= 0xDC00 && u <= 0xDFFF)
|
|
525 {
|
|
526 error("unpaired surrogate UTF-16 value %04x", u);
|
|
527 fatal();
|
|
528 }
|
|
529 else if (u == 0xFFFE || u == 0xFFFF)
|
|
530 {
|
|
531 error("illegal UTF-16 value %04x", u);
|
|
532 fatal();
|
|
533 }
|
|
534 dbuf.writeUTF8(u);
|
|
535 }
|
|
536 else
|
|
537 dbuf.writeByte(u);
|
|
538 }
|
|
539 dbuf.writeByte(0); // add 0 as sentinel for scanner
|
|
540 buflen = dbuf.offset - 1; // don't include sentinel in count
|
|
541 buf = cast(ubyte*) dbuf.extractData();
|
|
542 }
|
|
543 }
|
|
544 else if (buf[0] == 0xFE && buf[1] == 0xFF)
|
|
545 { // UTF-16BE
|
|
546 le = 0;
|
|
547 goto Lutf16;
|
|
548 }
|
|
549 else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF)
|
|
550 { // UTF-32BE
|
|
551 le = 0;
|
|
552 goto Lutf32;
|
|
553 }
|
|
554 else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF)
|
|
555 { // UTF-8
|
|
556
|
|
557 buf += 3;
|
|
558 buflen -= 3;
|
|
559 }
|
|
560 else
|
|
561 {
|
|
562 /* There is no BOM. Make use of Arcane Jill's insight that
|
|
563 * the first char of D source must be ASCII to
|
|
564 * figure out the encoding.
|
|
565 */
|
|
566
|
|
567 bom = 0;
|
|
568 if (buflen >= 4)
|
|
569 { if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0)
|
|
570 { // UTF-32LE
|
|
571 le = 1;
|
|
572 goto Lutf32;
|
|
573 }
|
|
574 else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0)
|
|
575 { // UTF-32BE
|
|
576 le = 0;
|
|
577 goto Lutf32;
|
|
578 }
|
|
579 }
|
|
580 if (buflen >= 2)
|
|
581 {
|
|
582 if (buf[1] == 0)
|
|
583 { // UTF-16LE
|
|
584 le = 1;
|
|
585 goto Lutf16;
|
|
586 }
|
|
587 else if (buf[0] == 0)
|
|
588 { // UTF-16BE
|
|
589 le = 0;
|
|
590 goto Lutf16;
|
|
591 }
|
|
592 }
|
|
593
|
|
594 // It's UTF-8
|
|
595 if (buf[0] >= 0x80)
|
|
596 { error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]);
|
|
597 fatal();
|
|
598 }
|
|
599 }
|
|
600 }
|
|
601
|
|
602 version (IN_GCC) {
|
|
603 // dump utf-8 encoded source
|
|
604 if (dump_source)
|
|
605 { // %% srcname could contain a path ...
|
|
606 d_gcc_dump_source(srcname, "utf-8", buf, buflen);
|
|
607 }
|
|
608 }
|
|
609
|
|
610 /* If it starts with the string "Ddoc", then it's a documentation
|
|
611 * source file.
|
|
612 */
|
|
613 if (buflen >= 4 && memcmp(buf, "Ddoc".ptr, 4) == 0)
|
|
614 {
|
|
615 comment = buf + 4;
|
|
616 isDocFile = 1;
|
|
617 if (!docfile)
|
|
618 setDocfile();
|
|
619 return;
|
|
620 }
|
|
621 if (isHtml)
|
|
622 {
|
|
623 assert(false);
|
|
624 ///OutBuffer dbuf = new OutBuffer();
|
|
625 ///Html h = new Html(srcname, buf, buflen);
|
|
626 ///h.extractCode(dbuf);
|
|
627 ///buf = dbuf.data;
|
|
628 ///buflen = dbuf.offset;
|
|
629
|
|
630 version (IN_GCC) {
|
|
631 // dump extracted source
|
|
632 ///if (dump_source)
|
|
633 /// d_gcc_dump_source(srcname, "d.utf-8", buf, buflen);
|
|
634 }
|
|
635 }
|
|
636
|
|
637 Parser p = new Parser(this, buf, buflen, docfile !is null);
|
|
638 p.nextToken();
|
|
639 members = p.parseModule();
|
|
640 md = p.md;
|
|
641 numlines = p.loc.linnum;
|
|
642
|
|
643 DsymbolTable dst;
|
|
644
|
|
645 if (md !is null)
|
|
646 {
|
|
647 this.ident = md.id;
|
|
648 this.safe = md.safe;
|
|
649 dst = super.resolve(md.packages, &this.parent, null);
|
|
650 }
|
|
651 else
|
|
652 {
|
|
653 dst = modules;
|
|
654
|
|
655 /* Check to see if module name is a valid identifier
|
|
656 */
|
|
657 if (!Lexer.isValidIdentifier(this.ident.toChars()))
|
|
658 error("has non-identifier characters in filename, use module declaration instead");
|
|
659 }
|
|
660
|
|
661 // Update global list of modules
|
|
662 if (!dst.insert(this))
|
|
663 {
|
|
664 if (md)
|
|
665 error(loc, "is in multiple packages %s", md.toChars());
|
|
666 else
|
|
667 error(loc, "is in multiple defined");
|
|
668 }
|
|
669 else
|
|
670 {
|
|
671 amodules.push(cast(void*)this);
|
|
672 }
|
|
673 }
|
|
674 }
|
|
675
|
|
676 void semantic() // semantic analysis
|
|
677 {
|
|
678 int i;
|
|
679
|
|
680 if (semanticstarted)
|
|
681 return;
|
|
682
|
|
683 //printf("+Module.semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
|
|
684 semanticstarted = 1;
|
|
685
|
|
686 // Note that modules get their own scope, from scratch.
|
|
687 // This is so regardless of where in the syntax a module
|
|
688 // gets imported, it is unaffected by context.
|
|
689 Scope sc = Scope.createGlobal(this); // create root scope
|
|
690
|
|
691 //printf("Module = %p, linkage = %d\n", sc.scopesym, sc.linkage);
|
|
692
|
|
693 // Add import of "object" if this module isn't "object"
|
|
694 if (ident !is Id.object)
|
|
695 {
|
|
696 Import im = new Import(Loc(0), null, Id.object, null, 0);
|
|
697 members.shift(cast(void*)im);
|
|
698 }
|
|
699
|
|
700 // Add all symbols into module's symbol table
|
|
701 symtab = new DsymbolTable();
|
|
702 for (i = 0; i < members.dim; i++)
|
|
703 {
|
|
704 Dsymbol s = cast(Dsymbol)members.data[i];
|
|
705 s.addMember(null, sc.scopesym, 1);
|
|
706 }
|
|
707
|
|
708 /* Set scope for the symbols so that if we forward reference
|
|
709 * a symbol, it can possibly be resolved on the spot.
|
|
710 * If this works out well, it can be extended to all modules
|
|
711 * before any semantic() on any of them.
|
|
712 */
|
|
713 for (i = 0; i < members.dim; i++)
|
|
714 {
|
|
715 Dsymbol s = cast(Dsymbol)members.data[i];
|
|
716 s.setScope(sc);
|
|
717 }
|
|
718
|
|
719 // Pass 1 semantic routines: do public side of the definition
|
|
720 for (i = 0; i < members.dim; i++)
|
|
721 {
|
|
722 Dsymbol s = cast(Dsymbol)members.data[i];
|
|
723
|
|
724 //writef("\tModule('%s'): '%s'.semantic()\n", toChars(), s.toChars());
|
|
725 s.semantic(sc);
|
|
726 runDeferredSemantic();
|
|
727 }
|
|
728
|
|
729 sc = sc.pop();
|
|
730 sc.pop(); // 2 pops because Scope.createGlobal() created 2
|
|
731 semanticRun = semanticstarted;
|
|
732 //printf("-Module.semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
|
|
733 }
|
|
734
|
|
735 void semantic2() // pass 2 semantic analysis
|
|
736 {
|
|
737 if (deferred.dim)
|
|
738 {
|
|
739 for (int i = 0; i < deferred.dim; i++)
|
|
740 {
|
|
741 Dsymbol sd = cast(Dsymbol)deferred.data[i];
|
|
742
|
|
743 sd.error("unable to resolve forward reference in definition");
|
|
744 }
|
|
745 return;
|
|
746 }
|
|
747 //printf("Module.semantic2('%s'): parent = %p\n", toChars(), parent);
|
|
748 if (semanticstarted >= 2)
|
|
749 return;
|
|
750 assert(semanticstarted == 1);
|
|
751 semanticstarted = 2;
|
|
752
|
|
753 // Note that modules get their own scope, from scratch.
|
|
754 // This is so regardless of where in the syntax a module
|
|
755 // gets imported, it is unaffected by context.
|
|
756 Scope sc = Scope.createGlobal(this); // create root scope
|
|
757 //printf("Module = %p\n", sc.scopesym);
|
|
758
|
|
759 // Pass 2 semantic routines: do initializers and function bodies
|
|
760 for (int i = 0; i < members.dim; i++)
|
|
761 {
|
|
762 Dsymbol s = cast(Dsymbol)members.data[i];
|
|
763 s.semantic2(sc);
|
|
764 }
|
|
765
|
|
766 sc = sc.pop();
|
|
767 sc.pop();
|
|
768 semanticRun = semanticstarted;
|
|
769 //printf("-Module.semantic2('%s'): parent = %p\n", toChars(), parent);
|
|
770 }
|
|
771
|
|
772 void semantic3() // pass 3 semantic analysis
|
|
773 {
|
|
774 //printf("Module.semantic3('%s'): parent = %p\n", toChars(), parent);
|
|
775 if (semanticstarted >= 3)
|
|
776 return;
|
|
777 assert(semanticstarted == 2);
|
|
778 semanticstarted = 3;
|
|
779
|
|
780 // Note that modules get their own scope, from scratch.
|
|
781 // This is so regardless of where in the syntax a module
|
|
782 // gets imported, it is unaffected by context.
|
|
783 Scope sc = Scope.createGlobal(this); // create root scope
|
|
784 //printf("Module = %p\n", sc.scopesym);
|
|
785
|
|
786 // Pass 3 semantic routines: do initializers and function bodies
|
|
787 for (int i = 0; i < members.dim; i++)
|
|
788 {
|
|
789 Dsymbol s = cast(Dsymbol)members.data[i];
|
|
790 //printf("Module %s: %s.semantic3()\n", toChars(), s.toChars());
|
|
791 s.semantic3(sc);
|
|
792 }
|
|
793
|
|
794 sc = sc.pop();
|
|
795 sc.pop();
|
|
796 semanticRun = semanticstarted;
|
|
797 }
|
|
798
|
|
799 void inlineScan() // scan for functions to inline
|
|
800 {
|
|
801 int i;
|
|
802
|
|
803 if (semanticstarted >= 4)
|
|
804 return;
|
|
805
|
|
806 assert(semanticstarted == 3);
|
|
807 semanticstarted = 4;
|
|
808
|
|
809 // Note that modules get their own scope, from scratch.
|
|
810 // This is so regardless of where in the syntax a module
|
|
811 // gets imported, it is unaffected by context.
|
|
812 //printf("Module = %p\n", sc.scopesym);
|
|
813
|
|
814 for (i = 0; i < members.dim; i++)
|
|
815 {
|
|
816 Dsymbol s = cast(Dsymbol)members.data[i];
|
|
817 //if (global.params.verbose)
|
|
818 //printf("inline scan symbol %s\n", s.toChars());
|
|
819 s.inlineScan();
|
|
820 }
|
|
821
|
|
822 semanticRun = semanticstarted;
|
|
823 }
|
|
824
|
|
825 void setHdrfile() // set hdrfile member
|
|
826 {
|
|
827 FileName hdrfilename;
|
|
828 string arghdr;
|
|
829
|
|
830 if (global.params.hdrname)
|
|
831 arghdr = global.params.hdrname;
|
|
832 else if (global.params.preservePaths)
|
|
833 arghdr = arg;
|
|
834 else
|
|
835 arghdr = FileName.name(arg);
|
|
836 if (!FileName.absolute(arghdr))
|
|
837 {
|
|
838 //FileName.ensurePathExists(global.params.hdrdir);
|
|
839 arghdr = FileName.combine(global.params.hdrdir, arghdr);
|
|
840 }
|
|
841 if (global.params.hdrname)
|
|
842 hdrfilename = new FileName(arghdr);
|
|
843 else
|
|
844 hdrfilename = FileName.forceExt(arghdr, global.hdr_ext);
|
|
845
|
|
846 if (hdrfilename.str == srcfile.name.str)
|
|
847 {
|
|
848 error("Source file and 'header' file have same name '%s'", srcfile.name.str);
|
|
849 fatal();
|
|
850 }
|
|
851
|
|
852 hdrfile = new File(hdrfilename);
|
|
853 }
|
|
854
|
|
855 version (_DH) {
|
|
856 void genhdrfile() // generate D import file
|
|
857 {
|
|
858 assert(false);
|
|
859 }
|
|
860 }
|
|
861
|
|
862 /**************************************
|
|
863 * Generate .obj file for Module.
|
|
864 */
|
|
865 void genobjfile(int multiobj)
|
|
866 {
|
|
867 //EEcontext *ee = env.getEEcontext();
|
|
868
|
|
869 //printf("Module.genobjfile(multiobj = %d) %s\n", multiobj, toChars());
|
|
870
|
|
871 lastmname = srcfile.toChars(); /// global mutation
|
|
872
|
|
873 obj_initfile(toStringz(lastmname), null, toStringz(toPrettyChars()));
|
|
874
|
|
875 eictor = null;
|
|
876 ictorlocalgot = null;
|
|
877 ector = null;
|
|
878 ectorgates.setDim(0);
|
|
879 edtor = null;
|
|
880 etest = null;
|
|
881 dtorcount = 0;
|
|
882
|
|
883 if (doppelganger)
|
|
884 {
|
|
885 /* Generate a reference to the moduleinfo, so the module constructors
|
|
886 * and destructors get linked in.
|
|
887 */
|
|
888 Module m = cast(Module)aimports.data[0];
|
|
889 assert(m);
|
|
890 if (m.sictor || m.sctor || m.sdtor)
|
|
891 {
|
|
892 Symbol* s = m.toSymbol();
|
|
893 //objextern(s);
|
|
894 //if (!s.Sxtrnnum) objextdef(s.Sident);
|
|
895 if (!s.Sxtrnnum)
|
|
896 {
|
|
897 //printf("%s\n", s.Sident);
|
|
898 static if (false) {
|
|
899 /* This should work, but causes optlink to fail in common/newlib.asm */
|
|
900 objextdef(s.Sident);
|
|
901 } else {
|
|
902 version (XXX) {///ELFOBJ || MACHOBJ
|
|
903 int nbytes = reftoident(DATA, Offset(DATA), s, 0, CFoff);
|
|
904 Offset(DATA) += nbytes;
|
|
905 } else {
|
|
906 int nbytes = reftoident(Segment.DATA, Doffset, s, 0, CF.CFoff);
|
|
907 Doffset() += nbytes;
|
|
908 }
|
|
909 }
|
|
910 }
|
|
911 }
|
|
912 }
|
|
913
|
|
914 if (global.params.cov)
|
|
915 {
|
|
916 /* Create coverage identifier:
|
|
917 * private uint[numlines] __coverage;
|
|
918 */
|
|
919 cov = symbol_calloc("__coverage");
|
|
920 cov.Stype = type_fake(TYM.TYint);
|
|
921 cov.Stype.Tmangle = mTYman.mTYman_c;
|
|
922 cov.Stype.Tcount++;
|
|
923 cov.Sclass = SC.SCstatic;
|
|
924 cov.Sfl = FL.FLdata;
|
|
925 version (ELFOBJ_OR_MACHOBJ) {
|
|
926 cov.Sseg = Segment.UDATA;
|
|
927 }
|
|
928 dtnzeros(&cov.Sdt, 4 * numlines);
|
|
929 outdata(cov);
|
|
930 slist_add(cov);
|
|
931
|
4
|
932 covb = cast(uint*)GC.calloc(((numlines + 32) / 32) * (*covb).sizeof);
|
0
|
933 }
|
|
934
|
|
935 for (int i = 0; i < members.dim; i++)
|
|
936 {
|
|
937 Dsymbol member = cast(Dsymbol)members.data[i];
|
|
938 member.toObjFile(multiobj);
|
|
939 }
|
|
940
|
|
941 if (global.params.cov)
|
|
942 {
|
|
943 /* Generate
|
|
944 * bit[numlines] __bcoverage;
|
|
945 */
|
|
946 Symbol* bcov = symbol_calloc("__bcoverage");
|
|
947 bcov.Stype = type_fake(TYM.TYuint);
|
|
948 bcov.Stype.Tcount++;
|
|
949 bcov.Sclass = SC.SCstatic;
|
|
950 bcov.Sfl = FL.FLdata;
|
|
951 version (ELFOBJ_OR_MACHOBJ) {
|
|
952 bcov.Sseg = Segment.DATA;
|
|
953 }
|
|
954 dtnbytes(&bcov.Sdt, (numlines + 32) / 32 * (*covb).sizeof, cast(char*)covb);
|
|
955 outdata(bcov);
|
|
956
|
2
|
957 ///free(covb);
|
0
|
958 covb = null;
|
|
959
|
|
960 /* Generate:
|
|
961 * _d_cover_register(uint[] __coverage, BitArray __bcoverage, string filename);
|
|
962 * and prepend it to the static constructor.
|
|
963 */
|
|
964
|
|
965 /* t will be the type of the functions generated:
|
|
966 * extern (C) void func();
|
|
967 */
|
|
968 type* t = type_alloc(TYM.TYnfunc);
|
|
969 t.Tflags |= TF.TFprototype | TF.TFfixed;
|
|
970 t.Tmangle = mTYman.mTYman_c;
|
|
971 t.Tnext = tsvoid;
|
|
972 tsvoid.Tcount++;
|
|
973
|
|
974 sictor = toSymbolX("__modictor", SC.SCglobal, t, "FZv");
|
|
975 cstate.CSpsymtab = &sictor.Sfunc.Flocsym;
|
|
976 localgot = ictorlocalgot;
|
|
977 elem* e;
|
|
978
|
|
979 e = el_params(el_ptr(cov), el_long(TYM.TYuint, numlines),
|
|
980 el_ptr(bcov), el_long(TYM.TYuint, numlines),
|
|
981 toEfilename(),
|
|
982 null);
|
|
983 e = el_bin(OPER.OPcall, TYM.TYvoid, el_var(rtlsym[RTLSYM.RTLSYM_DCOVER]), e);
|
|
984 eictor = el_combine(e, eictor);
|
|
985 ictorlocalgot = localgot;
|
|
986 }
|
|
987
|
|
988 // If coverage / static constructor / destructor / unittest calls
|
|
989 if (eictor || ector || ectorgates.dim || edtor || etest)
|
|
990 {
|
|
991 /* t will be the type of the functions generated:
|
|
992 * extern (C) void func();
|
|
993 */
|
|
994 type* t = type_alloc(TYM.TYnfunc);
|
|
995 t.Tflags |= TF.TFprototype | TF.TFfixed;
|
|
996 t.Tmangle = mTYman.mTYman_c;
|
|
997 t.Tnext = tsvoid;
|
|
998 tsvoid.Tcount++;
|
|
999
|
|
1000 static string moddeco = "FZv";
|
|
1001
|
|
1002 if (eictor)
|
|
1003 {
|
|
1004 localgot = ictorlocalgot;
|
|
1005
|
|
1006 block* b = block_calloc();
|
|
1007 b.BC = BC.BCret;
|
|
1008 b.Belem = eictor;
|
|
1009 sictor.Sfunc.Fstartblock = b;
|
|
1010 writefunc(sictor);
|
|
1011 }
|
|
1012
|
|
1013 if (ector || ectorgates.dim)
|
|
1014 {
|
|
1015 localgot = null;
|
|
1016 sctor = toSymbolX("__modctor", SC.SCglobal, t, moddeco);
|
|
1017 cstate.CSpsymtab = &sctor.Sfunc.Flocsym;
|
|
1018
|
|
1019 for (int i = 0; i < ectorgates.dim; i++)
|
|
1020 {
|
|
1021 StaticDtorDeclaration f = cast(StaticDtorDeclaration)ectorgates.data[i];
|
|
1022
|
|
1023 Symbol* s = f.vgate.toSymbol();
|
|
1024 elem* e = el_var(s);
|
|
1025 e = el_bin(OPER.OPaddass, TYM.TYint, e, el_long(TYM.TYint, 1));
|
|
1026 ector = el_combine(ector, e);
|
|
1027 }
|
|
1028
|
|
1029 block* b = block_calloc();
|
|
1030 b.BC = BC.BCret;
|
|
1031 b.Belem = ector;
|
|
1032 sctor.Sfunc.Fstartblock = b;
|
|
1033 writefunc(sctor);
|
|
1034 version (STATICCTOR) {
|
|
1035 obj_staticctor(sctor, dtorcount, 1);
|
|
1036 }
|
|
1037 }
|
|
1038
|
|
1039 if (edtor)
|
|
1040 {
|
|
1041 localgot = null;
|
|
1042 sdtor = toSymbolX("__moddtor", SC.SCglobal, t, moddeco);
|
|
1043
|
|
1044 block* b = block_calloc();
|
|
1045 b.BC = BC.BCret;
|
|
1046 b.Belem = edtor;
|
|
1047 sdtor.Sfunc.Fstartblock = b;
|
|
1048 writefunc(sdtor);
|
|
1049 }
|
|
1050
|
|
1051 if (etest)
|
|
1052 {
|
|
1053 localgot = null;
|
|
1054 stest = toSymbolX("__modtest", SC.SCglobal, t, moddeco);
|
|
1055
|
|
1056 block* b = block_calloc();
|
|
1057 b.BC = BC.BCret;
|
|
1058 b.Belem = etest;
|
|
1059 stest.Sfunc.Fstartblock = b;
|
|
1060 writefunc(stest);
|
|
1061 }
|
|
1062
|
|
1063 if (doppelganger)
|
|
1064 genmoduleinfo();
|
|
1065 }
|
|
1066
|
|
1067 if (doppelganger)
|
|
1068 {
|
|
1069 obj_termfile();
|
|
1070 return;
|
|
1071 }
|
|
1072
|
|
1073 if (global.params.multiobj)
|
|
1074 { /* This is necessary because the main .obj for this module is written
|
|
1075 * first, but determining whether marray or massert are needed is done
|
|
1076 * possibly later in the doppelganger modules.
|
|
1077 * Another way to fix it is do the main one last.
|
|
1078 */
|
|
1079 toModuleAssert();
|
|
1080 toModuleArray();
|
|
1081 }
|
|
1082
|
|
1083 // If module assert
|
|
1084 for (int i = 0; i < 2; i++)
|
|
1085 {
|
|
1086 Symbol* ma = i ? marray : massert;
|
|
1087
|
|
1088 if (ma)
|
|
1089 {
|
|
1090 elem* elinnum;
|
|
1091 elem* efilename;
|
|
1092
|
|
1093 localgot = null;
|
|
1094
|
|
1095 // Call dassert(filename, line)
|
|
1096 // Get sole parameter, linnum
|
|
1097 {
|
|
1098 Symbol* sp;
|
|
1099
|
|
1100 sp = symbol_calloc("linnum".ptr);
|
|
1101 sp.Stype = type_fake(TYM.TYint);
|
|
1102 sp.Stype.Tcount++;
|
|
1103 sp.Sclass = SC.SCfastpar;
|
|
1104 sp.Spreg = REG.AX;
|
|
1105 sp.Sflags &= ~SFL.SFLspill;
|
|
1106 sp.Sfl = FL.FLpara; // FLauto?
|
|
1107 cstate.CSpsymtab = &ma.Sfunc.Flocsym;
|
|
1108 symbol_add(sp);
|
|
1109
|
|
1110 elinnum = el_var(sp);
|
|
1111 }
|
|
1112
|
|
1113 efilename = toEmodulename();
|
|
1114
|
|
1115 elem *e = el_var(rtlsym[i ? RTLSYM.RTLSYM_DARRAY : RTLSYM.RTLSYM_DASSERT]);
|
|
1116 e = el_bin(OPER.OPcall, TYM.TYvoid, e, el_param(elinnum, efilename));
|
|
1117
|
|
1118 block* b = block_calloc();
|
|
1119 b.BC = BC.BCret;
|
|
1120 b.Belem = e;
|
|
1121 ma.Sfunc.Fstartblock = b;
|
|
1122 ma.Sclass = SC.SCglobal;
|
|
1123 ma.Sfl = 0;
|
|
1124 writefunc(ma);
|
|
1125 }
|
|
1126 }
|
|
1127
|
|
1128 static if (true) {
|
|
1129 // Always generate module info, because of templates and -cov
|
|
1130 if (1 || needModuleInfo())
|
|
1131 genmoduleinfo();
|
|
1132 }
|
|
1133
|
|
1134 obj_termfile();
|
|
1135 }
|
|
1136
|
|
1137 void gensymfile()
|
|
1138 {
|
|
1139 assert(false);
|
|
1140 }
|
|
1141
|
|
1142 void gendocfile()
|
|
1143 {
|
|
1144 assert(false);
|
|
1145 }
|
|
1146
|
|
1147 /**********************************
|
|
1148 * Determine if we need to generate an instance of ModuleInfo
|
|
1149 * for this Module.
|
|
1150 */
|
|
1151 bool needModuleInfo()
|
|
1152 {
|
|
1153 return needmoduleinfo || global.params.cov;
|
|
1154 }
|
|
1155
|
|
1156 Dsymbol search(Loc loc, Identifier ident, int flags)
|
|
1157 {
|
|
1158 /* Since modules can be circularly referenced,
|
|
1159 * need to stop infinite recursive searches.
|
|
1160 */
|
|
1161
|
|
1162 //printf("%s Module.search('%s', flags = %d) insearch = %d\n", toChars(), ident.toChars(), flags, insearch);
|
|
1163 Dsymbol s;
|
|
1164 if (insearch)
|
|
1165 s = null;
|
|
1166 else if (searchCacheIdent is ident && searchCacheFlags == flags)
|
|
1167 s = searchCacheSymbol;
|
|
1168 else
|
|
1169 {
|
|
1170 insearch = 1;
|
|
1171 s = ScopeDsymbol.search(loc, ident, flags);
|
|
1172 insearch = 0;
|
|
1173
|
|
1174 searchCacheIdent = ident;
|
|
1175 searchCacheSymbol = s;
|
|
1176 searchCacheFlags = flags;
|
|
1177 }
|
|
1178 return s;
|
|
1179 }
|
|
1180
|
|
1181 void deleteObjFile()
|
|
1182 {
|
|
1183 if (global.params.obj)
|
|
1184 objfile.remove();
|
|
1185 if (docfile)
|
|
1186 docfile.remove();
|
|
1187 }
|
|
1188
|
|
1189 void addDeferredSemantic(Dsymbol s)
|
|
1190 {
|
|
1191 assert(false);
|
|
1192 }
|
|
1193
|
|
1194 /******************************************
|
|
1195 * Run semantic() on deferred symbols.
|
|
1196 */
|
|
1197
|
|
1198 void runDeferredSemantic()
|
|
1199 {
|
|
1200 size_t len;
|
|
1201
|
|
1202 static int nested;
|
|
1203 if (nested)
|
|
1204 return;
|
|
1205 //if (deferred.dim) printf("+Module.runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
|
|
1206 nested++;
|
|
1207
|
|
1208 do
|
|
1209 {
|
|
1210 dprogress = 0;
|
|
1211 len = deferred.dim;
|
|
1212 if (!len)
|
|
1213 break;
|
|
1214
|
|
1215 Dsymbol *todo;
|
|
1216 Dsymbol tmp;
|
|
1217 if (len == 1)
|
|
1218 {
|
|
1219 todo = &tmp;
|
|
1220 }
|
|
1221 else
|
|
1222 {
|
|
1223 todo = cast(Dsymbol*)alloca(len * (Dsymbol*).sizeof);
|
|
1224 assert(todo);
|
|
1225 }
|
|
1226 memcpy(todo, deferred.data, len * (Dsymbol*).sizeof);
|
|
1227 deferred.setDim(0);
|
|
1228
|
|
1229 for (int i = 0; i < len; i++)
|
|
1230 {
|
|
1231 Dsymbol s = todo[i];
|
|
1232
|
|
1233 s.semantic(null);
|
|
1234 //printf("deferred: %s, parent = %s\n", s.toChars(), s.parent.toChars());
|
|
1235 }
|
|
1236 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
|
|
1237 } while (deferred.dim < len || dprogress); // while making progress
|
|
1238 nested--;
|
|
1239 //printf("-Module.runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
|
|
1240 }
|
|
1241
|
|
1242 int imports(Module m)
|
|
1243 {
|
|
1244 assert(false);
|
|
1245 }
|
|
1246
|
|
1247 // Back end
|
|
1248
|
|
1249 int doppelganger; // sub-module
|
|
1250 Symbol* cov; // private uint[] __coverage;
|
|
1251 uint* covb; // bit array of valid code line numbers
|
|
1252
|
|
1253 Symbol* sictor; // module order independent constructor
|
|
1254 Symbol* sctor; // module constructor
|
|
1255 Symbol* sdtor; // module destructor
|
|
1256 Symbol* stest; // module unit test
|
|
1257
|
|
1258 Symbol* sfilename; // symbol for filename
|
|
1259
|
|
1260 Symbol* massert; // module assert function
|
|
1261 Symbol* toModuleAssert() // get module assert function
|
|
1262 {
|
|
1263 if (!massert)
|
|
1264 {
|
|
1265 type* t;
|
|
1266
|
|
1267 t = type_alloc(TYjfunc);
|
|
1268 t.Tflags |= TFprototype | TFfixed;
|
|
1269 t.Tmangle = mTYman_d;
|
|
1270 t.Tnext = tsvoid;
|
|
1271 tsvoid.Tcount++;
|
|
1272
|
|
1273 massert = toSymbolX("__assert", SCextern, t, "FiZv");
|
|
1274 massert.Sfl = FLextern;
|
|
1275 massert.Sflags |= SFLnodebug;
|
|
1276 slist_add(massert);
|
|
1277 }
|
|
1278 return massert;
|
|
1279 }
|
|
1280
|
|
1281 Symbol* marray; // module array bounds function
|
|
1282
|
|
1283 Symbol* toModuleArray() // get module array bounds function
|
|
1284 {
|
|
1285 if (!marray)
|
|
1286 {
|
|
1287 type* t;
|
|
1288
|
|
1289 t = type_alloc(TYjfunc);
|
|
1290 t.Tflags |= TFprototype | TFfixed;
|
|
1291 t.Tmangle = mTYman_d;
|
|
1292 t.Tnext = tsvoid;
|
|
1293 tsvoid.Tcount++;
|
|
1294
|
|
1295 marray = toSymbolX("__array", SCextern, t, "Z");
|
|
1296 marray.Sfl = FLextern;
|
|
1297 marray.Sflags |= SFLnodebug;
|
|
1298 slist_add(marray);
|
|
1299 }
|
|
1300 return marray;
|
|
1301 }
|
|
1302
|
|
1303 static Symbol* gencritsec()
|
|
1304 {
|
|
1305 assert(false);
|
|
1306 }
|
|
1307
|
|
1308 elem* toEfilename()
|
|
1309 {
|
|
1310 elem* efilename;
|
|
1311
|
|
1312 if (!sfilename)
|
|
1313 {
|
|
1314 dt_t* dt = null;
|
|
1315
|
|
1316 string id = srcfile.toChars();
|
|
1317 int len = id.length;
|
|
1318 dtdword(&dt, len);
|
|
1319 dtabytes(&dt,TYnptr, 0, len + 1, toStringz(id));
|
|
1320
|
|
1321 sfilename = symbol_generate(SCstatic,type_fake(TYdarray));
|
|
1322 sfilename.Sdt = dt;
|
|
1323 sfilename.Sfl = FLdata;
|
|
1324 version (ELFOBJ) {
|
|
1325 sfilename.Sseg = Segment.CDATA;
|
|
1326 }
|
|
1327 version (MACHOBJ) {
|
|
1328 // Because of PIC and CDATA being in the _TEXT segment, cannot
|
|
1329 // have pointers in CDATA
|
|
1330 sfilename.Sseg = Segment.DATA;
|
|
1331 }
|
|
1332 outdata(sfilename);
|
|
1333 }
|
|
1334
|
|
1335 efilename = el_var(sfilename);
|
|
1336 return efilename;
|
|
1337 }
|
|
1338
|
|
1339 /**************************************
|
|
1340 * Generate elem that is a pointer to the module file name.
|
|
1341 */
|
|
1342 elem* toEmodulename()
|
|
1343 {
|
|
1344 elem *efilename;
|
|
1345
|
|
1346 // Get filename
|
|
1347 if (needModuleInfo())
|
|
1348 {
|
|
1349 /* Class ModuleInfo is defined in std.moduleinfo.
|
|
1350 * The first member is the name of it, char name[],
|
|
1351 * which will be at offset 8.
|
|
1352 */
|
|
1353
|
|
1354 Symbol* si = toSymbol();
|
|
1355 static if (true) {
|
|
1356 // Use this instead so -fPIC will work
|
|
1357 efilename = el_ptr(si);
|
|
1358 efilename = el_bin(OPadd, TYnptr, efilename, el_long(TYuint, 8));
|
|
1359 efilename = el_una(OPind, TYdarray, efilename);
|
|
1360 } else {
|
|
1361 efilename = el_var(si);
|
|
1362 efilename.Ety = TYdarray;
|
|
1363 efilename.EV.sp.Voffset += 8;
|
|
1364 }
|
|
1365 }
|
|
1366 else // generate our own filename
|
|
1367 {
|
|
1368 efilename = toEfilename();
|
|
1369 }
|
|
1370 return efilename;
|
|
1371 }
|
|
1372
|
|
1373 /*************************************
|
|
1374 * Create the "ModuleInfo" symbol
|
|
1375 */
|
|
1376 Symbol* toSymbol()
|
|
1377 {
|
|
1378 if (!csym)
|
|
1379 {
|
|
1380 Symbol* s;
|
|
1381 static Classsym* scc;
|
|
1382
|
|
1383 if (!scc) {
|
|
1384 scc = fake_classsym(Id.ClassInfo);
|
|
1385 }
|
|
1386
|
|
1387 s = toSymbolX("__ModuleInfo", SC.SCextern, scc.Stype, "Z");
|
|
1388 s.Sfl = FL.FLextern;
|
|
1389 s.Sflags |= SFL.SFLnodebug;
|
|
1390 csym = s;
|
|
1391 slist_add(s);
|
|
1392 }
|
|
1393 return csym;
|
|
1394 }
|
|
1395
|
|
1396 // Put out instance of ModuleInfo for this Module
|
|
1397 void genmoduleinfo()
|
|
1398 {
|
|
1399 //printf("Module.genmoduleinfo() %s\n", toChars());
|
|
1400
|
|
1401 Symbol* msym = toSymbol();
|
|
1402
|
|
1403 //dumpSymbol(msym);
|
|
1404
|
|
1405 uint offset;
|
|
1406 version (DMDV2) {
|
|
1407 uint sizeof_ModuleInfo = 18 * PTRSIZE;
|
|
1408 } else {
|
|
1409 uint sizeof_ModuleInfo = 14 * PTRSIZE;
|
|
1410 }
|
|
1411
|
|
1412 //////////////////////////////////////////////
|
|
1413
|
|
1414 csym.Sclass = SC.SCglobal;
|
|
1415
|
|
1416 csym.Sfl = FL.FLdata;
|
|
1417
|
|
1418 /* The layout is:
|
|
1419 {
|
|
1420 void **vptr;
|
|
1421 monitor_t monitor;
|
|
1422 char[] name; // class name
|
|
1423 ModuleInfo importedModules[];
|
|
1424 ClassInfo localClasses[];
|
|
1425 uint flags; // initialization state
|
|
1426 void *ctor;
|
|
1427 void *dtor;
|
|
1428 void *unitTest;
|
|
1429 const(MemberInfo[]) function(string) xgetMembers; // module getMembers() function
|
|
1430 void *ictor;
|
|
1431 void*[4] reserved;
|
|
1432 }
|
|
1433 */
|
|
1434 dt_t* dt = null;
|
|
1435
|
|
1436 if (moduleinfo)
|
|
1437 dtxoff(&dt, moduleinfo.toVtblSymbol(), 0, TYM.TYnptr); // vtbl for ModuleInfo
|
|
1438 else
|
|
1439 {
|
|
1440 //printf("moduleinfo is null\n");
|
|
1441 dtdword(&dt, 0); // BUG: should be an assert()
|
|
1442 }
|
|
1443 dtdword(&dt, 0); // monitor
|
|
1444
|
|
1445 // name[]
|
|
1446 string name = toPrettyChars();
|
|
1447 size_t namelen = name.length;
|
|
1448 dtdword(&dt, namelen);
|
|
1449 dtabytes(&dt, TYM.TYnptr, 0, namelen + 1, toStringz(name));
|
|
1450
|
|
1451 ClassDeclarations aclasses = new ClassDeclarations();
|
|
1452
|
|
1453 //printf("members.dim = %d\n", members.dim);
|
|
1454 for (int i = 0; i < members.dim; i++)
|
|
1455 {
|
|
1456 Dsymbol member = cast(Dsymbol)members.data[i];
|
|
1457
|
|
1458 //printf("\tmember '%s'\n", member.toChars());
|
|
1459 member.addLocalClass(aclasses);
|
|
1460 }
|
|
1461
|
|
1462 // importedModules[]
|
|
1463 int aimports_dim = aimports.dim;
|
|
1464 for (int i = 0; i < aimports.dim; i++)
|
|
1465 {
|
|
1466 Module m = cast(Module)aimports.data[i];
|
|
1467 if (!m.needModuleInfo())
|
|
1468 aimports_dim--;
|
|
1469 }
|
|
1470
|
|
1471 dtdword(&dt, aimports_dim);
|
|
1472 if (aimports_dim)
|
|
1473 dtxoff(&dt, csym, sizeof_ModuleInfo, TYM.TYnptr);
|
|
1474 else
|
|
1475 dtdword(&dt, 0);
|
|
1476
|
|
1477 // localClasses[]
|
|
1478 dtdword(&dt, aclasses.dim);
|
|
1479 if (aclasses.dim)
|
|
1480 dtxoff(&dt, csym, sizeof_ModuleInfo + aimports_dim * PTRSIZE, TYM.TYnptr);
|
|
1481 else
|
|
1482 dtdword(&dt, 0);
|
|
1483
|
|
1484 if (needmoduleinfo)
|
|
1485 dtdword(&dt, 8|0); // flags (4 means MIstandalone)
|
|
1486 else
|
|
1487 dtdword(&dt, 8|4); // flags (4 means MIstandalone)
|
|
1488
|
|
1489 if (sctor)
|
|
1490 dtxoff(&dt, sctor, 0, TYM.TYnptr);
|
|
1491 else
|
|
1492 dtdword(&dt, 0);
|
|
1493
|
|
1494 if (sdtor)
|
|
1495 dtxoff(&dt, sdtor, 0, TYM.TYnptr);
|
|
1496 else
|
|
1497 dtdword(&dt, 0);
|
|
1498
|
|
1499 if (stest)
|
|
1500 dtxoff(&dt, stest, 0, TYM.TYnptr);
|
|
1501 else
|
|
1502 dtdword(&dt, 0);
|
|
1503
|
|
1504 /// version (DMDV2) {
|
|
1505 FuncDeclaration sgetmembers = findGetMembers();
|
|
1506 if (sgetmembers)
|
|
1507 dtxoff(&dt, sgetmembers.toSymbol(), 0, TYM.TYnptr);
|
|
1508 else
|
|
1509 /// }
|
|
1510 dtdword(&dt, 0); // xgetMembers
|
|
1511
|
|
1512 if (sictor)
|
|
1513 dtxoff(&dt, sictor, 0, TYM.TYnptr);
|
|
1514 else
|
|
1515 dtdword(&dt, 0);
|
|
1516
|
|
1517 version (DMDV2) {
|
|
1518 // void*[4] reserved;
|
|
1519 dtdword(&dt, 0);
|
|
1520 dtdword(&dt, 0);
|
|
1521 dtdword(&dt, 0);
|
|
1522 dtdword(&dt, 0);
|
|
1523 }
|
|
1524 //////////////////////////////////////////////
|
|
1525
|
|
1526 for (int i = 0; i < aimports.dim; i++)
|
|
1527 {
|
|
1528 Module m = cast(Module)aimports.data[i];
|
|
1529
|
|
1530 if (m.needModuleInfo())
|
|
1531 {
|
|
1532 Symbol* s = m.toSymbol();
|
|
1533
|
|
1534 /* Weak references don't pull objects in from the library,
|
|
1535 * they resolve to 0 if not pulled in by something else.
|
|
1536 * Don't pull in a module just because it was imported.
|
|
1537 */
|
|
1538 version (OMFOBJ) {// Optlink crashes with weak symbols at EIP 41AFE7, 402000
|
|
1539 } else {
|
|
1540 s.Sflags |= SFL.SFLweak;
|
|
1541 }
|
|
1542 dtxoff(&dt, s, 0, TYM.TYnptr);
|
|
1543 }
|
|
1544 }
|
|
1545
|
|
1546 for (int i = 0; i < aclasses.dim; i++)
|
|
1547 {
|
|
1548 ClassDeclaration cd = cast(ClassDeclaration)aclasses.data[i];
|
|
1549 dtxoff(&dt, cd.toSymbol(), 0, TYM.TYnptr);
|
|
1550 }
|
|
1551
|
|
1552 csym.Sdt = dt;
|
|
1553 version (ELFOBJ_OR_MACHOBJ) {
|
|
1554 // Cannot be CONST because the startup code sets flag bits in it
|
|
1555 csym.Sseg = Segment.DATA;
|
|
1556 }
|
|
1557
|
|
1558 outdata(csym);
|
|
1559
|
|
1560 //////////////////////////////////////////////
|
|
1561
|
|
1562 obj_moduleinfo(msym);
|
|
1563 }
|
|
1564
|
|
1565 Module isModule() { return this; }
|
|
1566 } |