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