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