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