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