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