Mercurial > projects > ddmd
annotate dmd/Module.d @ 77:ad4792a1cfd6
more D-ification container accessing
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Sun, 29 Aug 2010 14:36:55 +0100 |
parents | 7e0d548de9e6 |
children | 43073c7c7769 |
rev | line source |
---|---|
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 | |
72 | 396 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 397 { |
398 assert(false); | |
399 } | |
400 | |
72 | 401 override string kind() |
0 | 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: | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
459 OutBuffer dbuf = new OutBuffer(); |
0 | 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: | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
494 OutBuffer dbuf = new OutBuffer(); |
0 | 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 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
637 auto p = new Parser(this, buf, buflen, docfile !is null); |
0 | 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 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
696 auto im = new Import(Loc(0), null, Id.object, null, 0); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
697 members.shift(im); |
0 | 698 } |
699 | |
700 // Add all symbols into module's symbol table | |
701 symtab = new DsymbolTable(); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
702 foreach(s; members) |
0 | 703 { |
13 | 704 s.addMember(null, sc.scopesym, true); |
0 | 705 } |
706 | |
707 /* Set scope for the symbols so that if we forward reference | |
708 * a symbol, it can possibly be resolved on the spot. | |
709 * If this works out well, it can be extended to all modules | |
710 * before any semantic() on any of them. | |
711 */ | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
712 foreach(Dsymbol s; members) |
0 | 713 s.setScope(sc); |
714 | |
715 // Pass 1 semantic routines: do public side of the definition | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
716 foreach (Dsymbol s; members) |
0 | 717 { |
718 //writef("\tModule('%s'): '%s'.semantic()\n", toChars(), s.toChars()); | |
719 s.semantic(sc); | |
720 runDeferredSemantic(); | |
721 } | |
722 | |
723 sc = sc.pop(); | |
724 sc.pop(); // 2 pops because Scope.createGlobal() created 2 | |
725 semanticRun = semanticstarted; | |
726 //printf("-Module.semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); | |
727 } | |
728 | |
729 void semantic2() // pass 2 semantic analysis | |
730 { | |
731 if (deferred.dim) | |
732 { | |
733 for (int i = 0; i < deferred.dim; i++) | |
734 { | |
735 Dsymbol sd = cast(Dsymbol)deferred.data[i]; | |
736 | |
737 sd.error("unable to resolve forward reference in definition"); | |
738 } | |
739 return; | |
740 } | |
741 //printf("Module.semantic2('%s'): parent = %p\n", toChars(), parent); | |
742 if (semanticstarted >= 2) | |
743 return; | |
744 assert(semanticstarted == 1); | |
745 semanticstarted = 2; | |
746 | |
747 // Note that modules get their own scope, from scratch. | |
748 // This is so regardless of where in the syntax a module | |
749 // gets imported, it is unaffected by context. | |
750 Scope sc = Scope.createGlobal(this); // create root scope | |
751 //printf("Module = %p\n", sc.scopesym); | |
752 | |
753 // Pass 2 semantic routines: do initializers and function bodies | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
754 foreach(Dsymbol s; members) |
0 | 755 s.semantic2(sc); |
756 | |
757 sc = sc.pop(); | |
758 sc.pop(); | |
759 semanticRun = semanticstarted; | |
760 //printf("-Module.semantic2('%s'): parent = %p\n", toChars(), parent); | |
761 } | |
762 | |
763 void semantic3() // pass 3 semantic analysis | |
764 { | |
765 //printf("Module.semantic3('%s'): parent = %p\n", toChars(), parent); | |
766 if (semanticstarted >= 3) | |
767 return; | |
768 assert(semanticstarted == 2); | |
769 semanticstarted = 3; | |
770 | |
771 // Note that modules get their own scope, from scratch. | |
772 // This is so regardless of where in the syntax a module | |
773 // gets imported, it is unaffected by context. | |
774 Scope sc = Scope.createGlobal(this); // create root scope | |
775 //printf("Module = %p\n", sc.scopesym); | |
776 | |
777 // Pass 3 semantic routines: do initializers and function bodies | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
778 foreach(Dsymbol s; members) |
0 | 779 { |
780 //printf("Module %s: %s.semantic3()\n", toChars(), s.toChars()); | |
781 s.semantic3(sc); | |
782 } | |
783 | |
784 sc = sc.pop(); | |
785 sc.pop(); | |
786 semanticRun = semanticstarted; | |
787 } | |
788 | |
72 | 789 override void inlineScan() // scan for functions to inline |
0 | 790 { |
791 int i; | |
792 | |
793 if (semanticstarted >= 4) | |
794 return; | |
795 | |
796 assert(semanticstarted == 3); | |
797 semanticstarted = 4; | |
798 | |
799 // Note that modules get their own scope, from scratch. | |
800 // This is so regardless of where in the syntax a module | |
801 // gets imported, it is unaffected by context. | |
802 //printf("Module = %p\n", sc.scopesym); | |
803 | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
804 foreach(Dsymbol s; members) |
0 | 805 { |
806 //if (global.params.verbose) | |
807 //printf("inline scan symbol %s\n", s.toChars()); | |
808 s.inlineScan(); | |
809 } | |
810 | |
811 semanticRun = semanticstarted; | |
812 } | |
813 | |
814 void setHdrfile() // set hdrfile member | |
815 { | |
816 FileName hdrfilename; | |
817 string arghdr; | |
818 | |
819 if (global.params.hdrname) | |
820 arghdr = global.params.hdrname; | |
821 else if (global.params.preservePaths) | |
822 arghdr = arg; | |
823 else | |
824 arghdr = FileName.name(arg); | |
825 if (!FileName.absolute(arghdr)) | |
826 { | |
827 //FileName.ensurePathExists(global.params.hdrdir); | |
828 arghdr = FileName.combine(global.params.hdrdir, arghdr); | |
829 } | |
830 if (global.params.hdrname) | |
831 hdrfilename = new FileName(arghdr); | |
832 else | |
833 hdrfilename = FileName.forceExt(arghdr, global.hdr_ext); | |
834 | |
835 if (hdrfilename.str == srcfile.name.str) | |
836 { | |
837 error("Source file and 'header' file have same name '%s'", srcfile.name.str); | |
838 fatal(); | |
839 } | |
840 | |
841 hdrfile = new File(hdrfilename); | |
842 } | |
843 | |
844 version (_DH) { | |
845 void genhdrfile() // generate D import file | |
846 { | |
847 assert(false); | |
848 } | |
849 } | |
850 | |
851 /************************************** | |
852 * Generate .obj file for Module. | |
853 */ | |
854 void genobjfile(int multiobj) | |
855 { | |
856 //EEcontext *ee = env.getEEcontext(); | |
857 | |
858 //printf("Module.genobjfile(multiobj = %d) %s\n", multiobj, toChars()); | |
859 | |
860 lastmname = srcfile.toChars(); /// global mutation | |
861 | |
862 obj_initfile(toStringz(lastmname), null, toStringz(toPrettyChars())); | |
863 | |
864 eictor = null; | |
865 ictorlocalgot = null; | |
866 ector = null; | |
867 ectorgates.setDim(0); | |
868 edtor = null; | |
869 etest = null; | |
870 dtorcount = 0; | |
871 | |
872 if (doppelganger) | |
873 { | |
874 /* Generate a reference to the moduleinfo, so the module constructors | |
875 * and destructors get linked in. | |
876 */ | |
877 Module m = cast(Module)aimports.data[0]; | |
878 assert(m); | |
879 if (m.sictor || m.sctor || m.sdtor) | |
880 { | |
881 Symbol* s = m.toSymbol(); | |
882 //objextern(s); | |
883 //if (!s.Sxtrnnum) objextdef(s.Sident); | |
884 if (!s.Sxtrnnum) | |
885 { | |
886 //printf("%s\n", s.Sident); | |
887 static if (false) { | |
888 /* This should work, but causes optlink to fail in common/newlib.asm */ | |
889 objextdef(s.Sident); | |
890 } else { | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
891 version (ELFOBJ_OR_MACHOBJ) {///ELFOBJ || MACHOBJ |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
892 int nbytes = reftoident(Segment.DATA, Offset(Segment.DATA), s, 0, CF.CFoff); |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
893 Offset(Segment.DATA) += nbytes; |
0 | 894 } else { |
895 int nbytes = reftoident(Segment.DATA, Doffset, s, 0, CF.CFoff); | |
896 Doffset() += nbytes; | |
897 } | |
898 } | |
899 } | |
900 } | |
901 } | |
902 | |
903 if (global.params.cov) | |
904 { | |
905 /* Create coverage identifier: | |
906 * private uint[numlines] __coverage; | |
907 */ | |
908 cov = symbol_calloc("__coverage"); | |
909 cov.Stype = type_fake(TYM.TYint); | |
910 cov.Stype.Tmangle = mTYman.mTYman_c; | |
911 cov.Stype.Tcount++; | |
912 cov.Sclass = SC.SCstatic; | |
913 cov.Sfl = FL.FLdata; | |
914 version (ELFOBJ_OR_MACHOBJ) { | |
915 cov.Sseg = Segment.UDATA; | |
916 } | |
917 dtnzeros(&cov.Sdt, 4 * numlines); | |
918 outdata(cov); | |
919 slist_add(cov); | |
920 | |
4 | 921 covb = cast(uint*)GC.calloc(((numlines + 32) / 32) * (*covb).sizeof); |
0 | 922 } |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
923 |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
924 foreach(Dsymbol member; members) |
0 | 925 member.toObjFile(multiobj); |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
926 |
0 | 927 if (global.params.cov) |
928 { | |
929 /* Generate | |
930 * bit[numlines] __bcoverage; | |
931 */ | |
932 Symbol* bcov = symbol_calloc("__bcoverage"); | |
933 bcov.Stype = type_fake(TYM.TYuint); | |
934 bcov.Stype.Tcount++; | |
935 bcov.Sclass = SC.SCstatic; | |
936 bcov.Sfl = FL.FLdata; | |
937 version (ELFOBJ_OR_MACHOBJ) { | |
938 bcov.Sseg = Segment.DATA; | |
939 } | |
940 dtnbytes(&bcov.Sdt, (numlines + 32) / 32 * (*covb).sizeof, cast(char*)covb); | |
941 outdata(bcov); | |
942 | |
2 | 943 ///free(covb); |
0 | 944 covb = null; |
945 | |
946 /* Generate: | |
947 * _d_cover_register(uint[] __coverage, BitArray __bcoverage, string filename); | |
948 * and prepend it to the static constructor. | |
949 */ | |
950 | |
951 /* t will be the type of the functions generated: | |
952 * extern (C) void func(); | |
953 */ | |
954 type* t = type_alloc(TYM.TYnfunc); | |
955 t.Tflags |= TF.TFprototype | TF.TFfixed; | |
956 t.Tmangle = mTYman.mTYman_c; | |
957 t.Tnext = tsvoid; | |
958 tsvoid.Tcount++; | |
959 | |
960 sictor = toSymbolX("__modictor", SC.SCglobal, t, "FZv"); | |
961 cstate.CSpsymtab = &sictor.Sfunc.Flocsym; | |
962 localgot = ictorlocalgot; | |
963 elem* e; | |
964 | |
965 e = el_params(el_ptr(cov), el_long(TYM.TYuint, numlines), | |
966 el_ptr(bcov), el_long(TYM.TYuint, numlines), | |
967 toEfilename(), | |
968 null); | |
969 e = el_bin(OPER.OPcall, TYM.TYvoid, el_var(rtlsym[RTLSYM.RTLSYM_DCOVER]), e); | |
970 eictor = el_combine(e, eictor); | |
971 ictorlocalgot = localgot; | |
972 } | |
973 | |
974 // If coverage / static constructor / destructor / unittest calls | |
975 if (eictor || ector || ectorgates.dim || edtor || etest) | |
976 { | |
977 /* t will be the type of the functions generated: | |
978 * extern (C) void func(); | |
979 */ | |
980 type* t = type_alloc(TYM.TYnfunc); | |
981 t.Tflags |= TF.TFprototype | TF.TFfixed; | |
982 t.Tmangle = mTYman.mTYman_c; | |
983 t.Tnext = tsvoid; | |
984 tsvoid.Tcount++; | |
985 | |
986 static string moddeco = "FZv"; | |
987 | |
988 if (eictor) | |
989 { | |
990 localgot = ictorlocalgot; | |
991 | |
992 block* b = block_calloc(); | |
993 b.BC = BC.BCret; | |
994 b.Belem = eictor; | |
995 sictor.Sfunc.Fstartblock = b; | |
996 writefunc(sictor); | |
997 } | |
998 | |
999 if (ector || ectorgates.dim) | |
1000 { | |
1001 localgot = null; | |
1002 sctor = toSymbolX("__modctor", SC.SCglobal, t, moddeco); | |
1003 cstate.CSpsymtab = &sctor.Sfunc.Flocsym; | |
1004 | |
1005 for (int i = 0; i < ectorgates.dim; i++) | |
1006 { | |
1007 StaticDtorDeclaration f = cast(StaticDtorDeclaration)ectorgates.data[i]; | |
1008 | |
1009 Symbol* s = f.vgate.toSymbol(); | |
1010 elem* e = el_var(s); | |
1011 e = el_bin(OPER.OPaddass, TYM.TYint, e, el_long(TYM.TYint, 1)); | |
1012 ector = el_combine(ector, e); | |
1013 } | |
1014 | |
1015 block* b = block_calloc(); | |
1016 b.BC = BC.BCret; | |
1017 b.Belem = ector; | |
1018 sctor.Sfunc.Fstartblock = b; | |
1019 writefunc(sctor); | |
1020 version (STATICCTOR) { | |
1021 obj_staticctor(sctor, dtorcount, 1); | |
1022 } | |
1023 } | |
1024 | |
1025 if (edtor) | |
1026 { | |
1027 localgot = null; | |
1028 sdtor = toSymbolX("__moddtor", SC.SCglobal, t, moddeco); | |
1029 | |
1030 block* b = block_calloc(); | |
1031 b.BC = BC.BCret; | |
1032 b.Belem = edtor; | |
1033 sdtor.Sfunc.Fstartblock = b; | |
1034 writefunc(sdtor); | |
1035 } | |
1036 | |
1037 if (etest) | |
1038 { | |
1039 localgot = null; | |
1040 stest = toSymbolX("__modtest", SC.SCglobal, t, moddeco); | |
1041 | |
1042 block* b = block_calloc(); | |
1043 b.BC = BC.BCret; | |
1044 b.Belem = etest; | |
1045 stest.Sfunc.Fstartblock = b; | |
1046 writefunc(stest); | |
1047 } | |
1048 | |
1049 if (doppelganger) | |
1050 genmoduleinfo(); | |
1051 } | |
1052 | |
1053 if (doppelganger) | |
1054 { | |
1055 obj_termfile(); | |
1056 return; | |
1057 } | |
1058 | |
1059 if (global.params.multiobj) | |
1060 { /* This is necessary because the main .obj for this module is written | |
1061 * first, but determining whether marray or massert are needed is done | |
1062 * possibly later in the doppelganger modules. | |
1063 * Another way to fix it is do the main one last. | |
1064 */ | |
1065 toModuleAssert(); | |
1066 toModuleArray(); | |
1067 } | |
1068 | |
1069 // If module assert | |
1070 for (int i = 0; i < 2; i++) | |
1071 { | |
1072 Symbol* ma = i ? marray : massert; | |
1073 | |
1074 if (ma) | |
1075 { | |
1076 elem* elinnum; | |
1077 elem* efilename; | |
1078 | |
1079 localgot = null; | |
1080 | |
1081 // Call dassert(filename, line) | |
1082 // Get sole parameter, linnum | |
1083 { | |
1084 Symbol* sp; | |
1085 | |
1086 sp = symbol_calloc("linnum".ptr); | |
1087 sp.Stype = type_fake(TYM.TYint); | |
1088 sp.Stype.Tcount++; | |
1089 sp.Sclass = SC.SCfastpar; | |
1090 sp.Spreg = REG.AX; | |
1091 sp.Sflags &= ~SFL.SFLspill; | |
1092 sp.Sfl = FL.FLpara; // FLauto? | |
1093 cstate.CSpsymtab = &ma.Sfunc.Flocsym; | |
1094 symbol_add(sp); | |
1095 | |
1096 elinnum = el_var(sp); | |
1097 } | |
1098 | |
1099 efilename = toEmodulename(); | |
1100 | |
1101 elem *e = el_var(rtlsym[i ? RTLSYM.RTLSYM_DARRAY : RTLSYM.RTLSYM_DASSERT]); | |
1102 e = el_bin(OPER.OPcall, TYM.TYvoid, e, el_param(elinnum, efilename)); | |
1103 | |
1104 block* b = block_calloc(); | |
1105 b.BC = BC.BCret; | |
1106 b.Belem = e; | |
1107 ma.Sfunc.Fstartblock = b; | |
1108 ma.Sclass = SC.SCglobal; | |
1109 ma.Sfl = 0; | |
1110 writefunc(ma); | |
1111 } | |
1112 } | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
1113 |
0 | 1114 |
1115 static if (true) { | |
1116 // Always generate module info, because of templates and -cov | |
1117 if (1 || needModuleInfo()) | |
1118 genmoduleinfo(); | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
1119 } |
0 | 1120 |
1121 obj_termfile(); | |
1122 } | |
1123 | |
1124 void gensymfile() | |
1125 { | |
1126 assert(false); | |
1127 } | |
1128 | |
1129 void gendocfile() | |
1130 { | |
1131 assert(false); | |
1132 } | |
1133 | |
1134 /********************************** | |
1135 * Determine if we need to generate an instance of ModuleInfo | |
1136 * for this Module. | |
1137 */ | |
1138 bool needModuleInfo() | |
1139 { | |
1140 return needmoduleinfo || global.params.cov; | |
1141 } | |
1142 | |
72 | 1143 override Dsymbol search(Loc loc, Identifier ident, int flags) |
0 | 1144 { |
1145 /* Since modules can be circularly referenced, | |
1146 * need to stop infinite recursive searches. | |
1147 */ | |
1148 | |
1149 //printf("%s Module.search('%s', flags = %d) insearch = %d\n", toChars(), ident.toChars(), flags, insearch); | |
1150 Dsymbol s; | |
1151 if (insearch) | |
1152 s = null; | |
73 | 1153 else if (searchCacheIdent == ident && searchCacheFlags == flags && searchCacheSymbol) |
1154 { | |
0 | 1155 s = searchCacheSymbol; |
73 | 1156 //printf("%s Module.search('%s', flags = %d) insearch = %d searchCacheSymbol = %s\n", toChars(), ident.toChars(), flags, insearch, searchCacheSymbol ? searchCacheSymbol.toChars() : "null"); |
1157 } | |
0 | 1158 else |
1159 { | |
1160 insearch = 1; | |
1161 s = ScopeDsymbol.search(loc, ident, flags); | |
1162 insearch = 0; | |
1163 | |
1164 searchCacheIdent = ident; | |
1165 searchCacheSymbol = s; | |
1166 searchCacheFlags = flags; | |
1167 } | |
1168 return s; | |
1169 } | |
1170 | |
1171 void deleteObjFile() | |
1172 { | |
1173 if (global.params.obj) | |
1174 objfile.remove(); | |
1175 if (docfile) | |
1176 docfile.remove(); | |
1177 } | |
1178 | |
29
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1179 /******************************************* |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1180 * Can't run semantic on s now, try again later. |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1181 */ |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1182 void addDeferredSemantic(Dsymbol s) |
0 | 1183 { |
29
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1184 // Don't add it if it is already there |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1185 for (int i = 0; i < deferred.dim; i++) |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1186 { |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1187 Dsymbol sd = cast(Dsymbol)deferred.data[i]; |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1188 |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1189 if (sd == s) |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1190 return; |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1191 } |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1192 |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1193 //printf("Module::addDeferredSemantic('%s')\n", s->toChars()); |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1194 deferred.push(cast(void*)s); |
0 | 1195 } |
1196 | |
1197 /****************************************** | |
1198 * Run semantic() on deferred symbols. | |
1199 */ | |
1200 | |
1201 void runDeferredSemantic() | |
1202 { | |
1203 size_t len; | |
1204 | |
1205 static int nested; | |
1206 if (nested) | |
1207 return; | |
1208 //if (deferred.dim) printf("+Module.runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim); | |
1209 nested++; | |
1210 | |
1211 do | |
1212 { | |
1213 dprogress = 0; | |
1214 len = deferred.dim; | |
1215 if (!len) | |
1216 break; | |
1217 | |
1218 Dsymbol *todo; | |
1219 Dsymbol tmp; | |
1220 if (len == 1) | |
1221 { | |
1222 todo = &tmp; | |
1223 } | |
1224 else | |
1225 { | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1226 version(Bug4054) |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1227 todo = cast(Dsymbol*)GC.malloc(len * (Dsymbol*).sizeof); |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1228 else |
0 | 1229 todo = cast(Dsymbol*)alloca(len * (Dsymbol*).sizeof); |
1230 assert(todo); | |
1231 } | |
1232 memcpy(todo, deferred.data, len * (Dsymbol*).sizeof); | |
1233 deferred.setDim(0); | |
1234 | |
1235 for (int i = 0; i < len; i++) | |
1236 { | |
1237 Dsymbol s = todo[i]; | |
1238 | |
1239 s.semantic(null); | |
1240 //printf("deferred: %s, parent = %s\n", s.toChars(), s.parent.toChars()); | |
1241 } | |
1242 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress); | |
1243 } while (deferred.dim < len || dprogress); // while making progress | |
1244 nested--; | |
1245 //printf("-Module.runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim); | |
1246 } | |
1247 | |
1248 int imports(Module m) | |
1249 { | |
1250 assert(false); | |
1251 } | |
1252 | |
1253 // Back end | |
1254 | |
1255 int doppelganger; // sub-module | |
1256 Symbol* cov; // private uint[] __coverage; | |
1257 uint* covb; // bit array of valid code line numbers | |
1258 | |
1259 Symbol* sictor; // module order independent constructor | |
1260 Symbol* sctor; // module constructor | |
1261 Symbol* sdtor; // module destructor | |
1262 Symbol* stest; // module unit test | |
1263 | |
1264 Symbol* sfilename; // symbol for filename | |
1265 | |
1266 Symbol* massert; // module assert function | |
1267 Symbol* toModuleAssert() // get module assert function | |
1268 { | |
1269 if (!massert) | |
1270 { | |
1271 type* t; | |
1272 | |
1273 t = type_alloc(TYjfunc); | |
1274 t.Tflags |= TFprototype | TFfixed; | |
1275 t.Tmangle = mTYman_d; | |
1276 t.Tnext = tsvoid; | |
1277 tsvoid.Tcount++; | |
1278 | |
1279 massert = toSymbolX("__assert", SCextern, t, "FiZv"); | |
1280 massert.Sfl = FLextern; | |
1281 massert.Sflags |= SFLnodebug; | |
1282 slist_add(massert); | |
1283 } | |
1284 return massert; | |
1285 } | |
1286 | |
1287 Symbol* marray; // module array bounds function | |
1288 | |
1289 Symbol* toModuleArray() // get module array bounds function | |
1290 { | |
1291 if (!marray) | |
1292 { | |
1293 type* t; | |
1294 | |
1295 t = type_alloc(TYjfunc); | |
1296 t.Tflags |= TFprototype | TFfixed; | |
1297 t.Tmangle = mTYman_d; | |
1298 t.Tnext = tsvoid; | |
1299 tsvoid.Tcount++; | |
1300 | |
1301 marray = toSymbolX("__array", SCextern, t, "Z"); | |
1302 marray.Sfl = FLextern; | |
1303 marray.Sflags |= SFLnodebug; | |
1304 slist_add(marray); | |
1305 } | |
1306 return marray; | |
1307 } | |
1308 | |
1309 static Symbol* gencritsec() | |
1310 { | |
1311 assert(false); | |
1312 } | |
1313 | |
1314 elem* toEfilename() | |
1315 { | |
1316 elem* efilename; | |
1317 | |
1318 if (!sfilename) | |
1319 { | |
1320 dt_t* dt = null; | |
1321 | |
1322 string id = srcfile.toChars(); | |
1323 int len = id.length; | |
1324 dtdword(&dt, len); | |
1325 dtabytes(&dt,TYnptr, 0, len + 1, toStringz(id)); | |
1326 | |
1327 sfilename = symbol_generate(SCstatic,type_fake(TYdarray)); | |
1328 sfilename.Sdt = dt; | |
1329 sfilename.Sfl = FLdata; | |
1330 version (ELFOBJ) { | |
1331 sfilename.Sseg = Segment.CDATA; | |
1332 } | |
1333 version (MACHOBJ) { | |
1334 // Because of PIC and CDATA being in the _TEXT segment, cannot | |
1335 // have pointers in CDATA | |
1336 sfilename.Sseg = Segment.DATA; | |
1337 } | |
1338 outdata(sfilename); | |
1339 } | |
1340 | |
1341 efilename = el_var(sfilename); | |
1342 return efilename; | |
1343 } | |
1344 | |
1345 /************************************** | |
1346 * Generate elem that is a pointer to the module file name. | |
1347 */ | |
1348 elem* toEmodulename() | |
1349 { | |
1350 elem *efilename; | |
1351 | |
1352 // Get filename | |
1353 if (needModuleInfo()) | |
1354 { | |
1355 /* Class ModuleInfo is defined in std.moduleinfo. | |
1356 * The first member is the name of it, char name[], | |
1357 * which will be at offset 8. | |
1358 */ | |
1359 | |
1360 Symbol* si = toSymbol(); | |
1361 static if (true) { | |
1362 // Use this instead so -fPIC will work | |
1363 efilename = el_ptr(si); | |
1364 efilename = el_bin(OPadd, TYnptr, efilename, el_long(TYuint, 8)); | |
1365 efilename = el_una(OPind, TYdarray, efilename); | |
1366 } else { | |
1367 efilename = el_var(si); | |
1368 efilename.Ety = TYdarray; | |
1369 efilename.EV.sp.Voffset += 8; | |
1370 } | |
1371 } | |
1372 else // generate our own filename | |
1373 { | |
1374 efilename = toEfilename(); | |
1375 } | |
1376 return efilename; | |
1377 } | |
1378 | |
1379 /************************************* | |
1380 * Create the "ModuleInfo" symbol | |
1381 */ | |
72 | 1382 override Symbol* toSymbol() |
0 | 1383 { |
1384 if (!csym) | |
1385 { | |
1386 Symbol* s; | |
1387 static Classsym* scc; | |
1388 | |
1389 if (!scc) { | |
1390 scc = fake_classsym(Id.ClassInfo); | |
1391 } | |
1392 | |
1393 s = toSymbolX("__ModuleInfo", SC.SCextern, scc.Stype, "Z"); | |
1394 s.Sfl = FL.FLextern; | |
1395 s.Sflags |= SFL.SFLnodebug; | |
1396 csym = s; | |
1397 slist_add(s); | |
1398 } | |
1399 return csym; | |
1400 } | |
1401 | |
1402 // Put out instance of ModuleInfo for this Module | |
1403 void genmoduleinfo() | |
1404 { | |
1405 //printf("Module.genmoduleinfo() %s\n", toChars()); | |
1406 | |
1407 Symbol* msym = toSymbol(); | |
1408 | |
1409 //dumpSymbol(msym); | |
1410 | |
1411 uint offset; | |
1412 version (DMDV2) { | |
1413 uint sizeof_ModuleInfo = 18 * PTRSIZE; | |
1414 } else { | |
1415 uint sizeof_ModuleInfo = 14 * PTRSIZE; | |
1416 } | |
1417 | |
1418 ////////////////////////////////////////////// | |
1419 | |
1420 csym.Sclass = SC.SCglobal; | |
1421 | |
1422 csym.Sfl = FL.FLdata; | |
1423 | |
1424 /* The layout is: | |
1425 { | |
1426 void **vptr; | |
1427 monitor_t monitor; | |
1428 char[] name; // class name | |
1429 ModuleInfo importedModules[]; | |
1430 ClassInfo localClasses[]; | |
1431 uint flags; // initialization state | |
1432 void *ctor; | |
1433 void *dtor; | |
1434 void *unitTest; | |
1435 const(MemberInfo[]) function(string) xgetMembers; // module getMembers() function | |
1436 void *ictor; | |
1437 void*[4] reserved; | |
1438 } | |
1439 */ | |
1440 dt_t* dt = null; | |
1441 | |
1442 if (moduleinfo) | |
1443 dtxoff(&dt, moduleinfo.toVtblSymbol(), 0, TYM.TYnptr); // vtbl for ModuleInfo | |
1444 else | |
1445 { | |
1446 //printf("moduleinfo is null\n"); | |
1447 dtdword(&dt, 0); // BUG: should be an assert() | |
1448 } | |
1449 dtdword(&dt, 0); // monitor | |
1450 | |
1451 // name[] | |
1452 string name = toPrettyChars(); | |
1453 size_t namelen = name.length; | |
1454 dtdword(&dt, namelen); | |
1455 dtabytes(&dt, TYM.TYnptr, 0, namelen + 1, toStringz(name)); | |
1456 | |
1457 ClassDeclarations aclasses = new ClassDeclarations(); | |
1458 | |
1459 //printf("members.dim = %d\n", members.dim); | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
1460 foreach(Dsymbol member; members) |
0 | 1461 { |
1462 //printf("\tmember '%s'\n", member.toChars()); | |
1463 member.addLocalClass(aclasses); | |
1464 } | |
1465 | |
1466 // importedModules[] | |
1467 int aimports_dim = aimports.dim; | |
1468 for (int i = 0; i < aimports.dim; i++) | |
1469 { | |
1470 Module m = cast(Module)aimports.data[i]; | |
1471 if (!m.needModuleInfo()) | |
1472 aimports_dim--; | |
1473 } | |
1474 | |
1475 dtdword(&dt, aimports_dim); | |
1476 if (aimports_dim) | |
1477 dtxoff(&dt, csym, sizeof_ModuleInfo, TYM.TYnptr); | |
1478 else | |
1479 dtdword(&dt, 0); | |
1480 | |
1481 // localClasses[] | |
1482 dtdword(&dt, aclasses.dim); | |
1483 if (aclasses.dim) | |
1484 dtxoff(&dt, csym, sizeof_ModuleInfo + aimports_dim * PTRSIZE, TYM.TYnptr); | |
1485 else | |
1486 dtdword(&dt, 0); | |
1487 | |
1488 if (needmoduleinfo) | |
1489 dtdword(&dt, 8|0); // flags (4 means MIstandalone) | |
1490 else | |
1491 dtdword(&dt, 8|4); // flags (4 means MIstandalone) | |
1492 | |
1493 if (sctor) | |
1494 dtxoff(&dt, sctor, 0, TYM.TYnptr); | |
1495 else | |
1496 dtdword(&dt, 0); | |
1497 | |
1498 if (sdtor) | |
1499 dtxoff(&dt, sdtor, 0, TYM.TYnptr); | |
1500 else | |
1501 dtdword(&dt, 0); | |
1502 | |
1503 if (stest) | |
1504 dtxoff(&dt, stest, 0, TYM.TYnptr); | |
1505 else | |
1506 dtdword(&dt, 0); | |
1507 | |
1508 /// version (DMDV2) { | |
1509 FuncDeclaration sgetmembers = findGetMembers(); | |
1510 if (sgetmembers) | |
1511 dtxoff(&dt, sgetmembers.toSymbol(), 0, TYM.TYnptr); | |
1512 else | |
1513 /// } | |
1514 dtdword(&dt, 0); // xgetMembers | |
1515 | |
1516 if (sictor) | |
1517 dtxoff(&dt, sictor, 0, TYM.TYnptr); | |
1518 else | |
1519 dtdword(&dt, 0); | |
1520 | |
1521 version (DMDV2) { | |
1522 // void*[4] reserved; | |
1523 dtdword(&dt, 0); | |
1524 dtdword(&dt, 0); | |
1525 dtdword(&dt, 0); | |
1526 dtdword(&dt, 0); | |
1527 } | |
1528 ////////////////////////////////////////////// | |
1529 | |
1530 for (int i = 0; i < aimports.dim; i++) | |
1531 { | |
1532 Module m = cast(Module)aimports.data[i]; | |
1533 | |
1534 if (m.needModuleInfo()) | |
1535 { | |
1536 Symbol* s = m.toSymbol(); | |
1537 | |
1538 /* Weak references don't pull objects in from the library, | |
1539 * they resolve to 0 if not pulled in by something else. | |
1540 * Don't pull in a module just because it was imported. | |
1541 */ | |
1542 version (OMFOBJ) {// Optlink crashes with weak symbols at EIP 41AFE7, 402000 | |
1543 } else { | |
1544 s.Sflags |= SFL.SFLweak; | |
1545 } | |
1546 dtxoff(&dt, s, 0, TYM.TYnptr); | |
1547 } | |
1548 } | |
1549 | |
1550 for (int i = 0; i < aclasses.dim; i++) | |
1551 { | |
1552 ClassDeclaration cd = cast(ClassDeclaration)aclasses.data[i]; | |
1553 dtxoff(&dt, cd.toSymbol(), 0, TYM.TYnptr); | |
1554 } | |
1555 | |
1556 csym.Sdt = dt; | |
1557 version (ELFOBJ_OR_MACHOBJ) { | |
1558 // Cannot be CONST because the startup code sets flag bits in it | |
1559 csym.Sseg = Segment.DATA; | |
1560 } | |
1561 | |
1562 outdata(csym); | |
1563 | |
1564 ////////////////////////////////////////////// | |
1565 | |
1566 obj_moduleinfo(msym); | |
1567 } | |
1568 | |
72 | 1569 override Module isModule() { return this; } |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1570 } |