Mercurial > projects > ldc
annotate dmd/module.c @ 486:a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Reimplemented support for nested functions/class using a new approach.
Added error on taking address of intrinsic.
Fixed problems with the ->syntaxCopy of TypeFunction delegate exp.
Removed DtoDType and replaced all uses with ->toBasetype() instead.
Removed unused inplace stuff.
Fixed a bunch of issues in the runtime unittests, not complete yet.
Added mini tests.
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Sun, 10 Aug 2008 08:37:38 +0200 |
parents | aaade6ded589 |
children | 6aee82889553 |
rev | line source |
---|---|
336 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2007 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <assert.h> | |
14 | |
15 #if _MSC_VER || __MINGW32__ | |
16 #include <malloc.h> | |
17 #endif | |
18 | |
19 #if IN_GCC | |
20 #include "gdc_alloca.h" | |
21 #endif | |
22 | |
23 #include "mem.h" | |
24 | |
25 #include "mars.h" | |
26 #include "module.h" | |
27 #include "parse.h" | |
28 #include "scope.h" | |
29 #include "identifier.h" | |
30 #include "id.h" | |
31 #include "import.h" | |
32 #include "dsymbol.h" | |
33 #include "hdrgen.h" | |
34 #include "lexer.h" | |
35 | |
36 #define MARS 1 | |
37 #include "html.h" | |
38 | |
39 #ifdef IN_GCC | |
40 #include "d-dmd-gcc.h" | |
41 #endif | |
42 | |
43 ClassDeclaration *Module::moduleinfo; | |
44 | |
45 Module *Module::rootModule; | |
46 DsymbolTable *Module::modules; | |
47 Array Module::amodules; | |
48 | |
49 Array Module::deferred; // deferred Dsymbol's needing semantic() run on them | |
50 unsigned Module::dprogress; | |
51 | |
52 void Module::init() | |
53 { | |
54 modules = new DsymbolTable(); | |
55 } | |
56 | |
57 Module::Module(char *filename, Identifier *ident, int doDocComment, int doHdrGen) | |
58 : Package(ident) | |
59 { | |
60 FileName *srcfilename; | |
61 FileName *cfilename; | |
62 FileName *hfilename; | |
63 FileName *objfilename; | |
64 FileName *llfilename; | |
65 FileName *bcfilename; | |
66 FileName *symfilename; | |
67 | |
68 // printf("Module::Module(filename = '%s', ident = '%s')\n", filename, ident->toChars()); | |
69 this->arg = filename; | |
70 md = NULL; | |
71 errors = 0; | |
72 numlines = 0; | |
73 members = NULL; | |
74 isHtml = 0; | |
75 isDocFile = 0; | |
76 needmoduleinfo = 0; | |
77 #ifdef IN_GCC | |
78 strictlyneedmoduleinfo = 0; | |
79 #endif | |
80 insearch = 0; | |
81 searchCacheIdent = NULL; | |
82 searchCacheSymbol = NULL; | |
83 searchCacheFlags = 0; | |
84 semanticstarted = 0; | |
85 semanticdone = 0; | |
86 decldefs = NULL; | |
87 vmoduleinfo = NULL; | |
88 massert = NULL; | |
89 marray = NULL; | |
90 sictor = NULL; | |
91 sctor = NULL; | |
92 sdtor = NULL; | |
93 stest = NULL; | |
94 sfilename = NULL; | |
95 root = 0; | |
96 importedFrom = NULL; | |
97 srcfile = NULL; | |
98 docfile = NULL; | |
99 | |
100 debuglevel = 0; | |
101 debugids = NULL; | |
102 debugidsNot = NULL; | |
103 versionlevel = 0; | |
104 versionids = NULL; | |
105 versionidsNot = NULL; | |
106 | |
107 macrotable = NULL; | |
108 escapetable = NULL; | |
109 doppelganger = 0; | |
110 cov = NULL; | |
111 covb = NULL; | |
112 | |
113 srcfilename = FileName::defaultExt(filename, global.mars_ext); | |
114 if (!srcfilename->equalsExt(global.mars_ext) && | |
115 !srcfilename->equalsExt("dd")) | |
116 { | |
117 if (srcfilename->equalsExt("html") || | |
118 srcfilename->equalsExt("htm") || | |
119 srcfilename->equalsExt("xhtml")) | |
120 isHtml = 1; | |
121 else | |
122 { error("source file name '%s' must have .%s extension", srcfilename->toChars(), global.mars_ext); | |
123 fatal(); | |
124 } | |
125 } | |
126 | |
127 char *argobj; | |
128 if (global.params.objname) | |
129 argobj = global.params.objname; | |
130 else if (global.params.preservePaths) | |
131 argobj = filename; | |
132 else | |
133 argobj = FileName::name(filename); | |
134 if (!FileName::absolute(argobj)) | |
135 { | |
136 argobj = FileName::combine(global.params.objdir, argobj); | |
137 } | |
138 | |
139 if (global.params.objname) | |
140 objfilename = new FileName(argobj, 0); | |
141 else | |
142 objfilename = FileName::forceExt(argobj, global.obj_ext); | |
143 | |
144 llfilename = FileName::forceExt(argobj, global.ll_ext); | |
145 bcfilename = FileName::forceExt(argobj, global.bc_ext); | |
146 | |
147 symfilename = FileName::forceExt(filename, global.sym_ext); | |
148 | |
149 srcfile = new File(srcfilename); | |
150 | |
151 if (doDocComment) | |
152 { | |
153 setDocfile(); | |
154 } | |
155 | |
156 if (doHdrGen) | |
157 { | |
158 setHdrfile(); | |
159 } | |
160 | |
161 objfile = new File(objfilename); | |
162 bcfile = new File(bcfilename); | |
163 llfile = new File(llfilename); | |
164 symfile = new File(symfilename); | |
486
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
165 |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
166 // LLVMDC |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
167 llvmForceLogging = false; |
336 | 168 } |
169 | |
170 void Module::setDocfile() | |
171 { | |
172 FileName *docfilename; | |
173 char *argdoc; | |
174 | |
175 if (global.params.docname) | |
176 argdoc = global.params.docname; | |
177 else if (global.params.preservePaths) | |
178 argdoc = (char *)arg; | |
179 else | |
180 argdoc = FileName::name((char *)arg); | |
181 if (!FileName::absolute(argdoc)) | |
182 { //FileName::ensurePathExists(global.params.docdir); | |
183 argdoc = FileName::combine(global.params.docdir, argdoc); | |
184 } | |
185 if (global.params.docname) | |
186 docfilename = new FileName(argdoc, 0); | |
187 else | |
188 docfilename = FileName::forceExt(argdoc, global.doc_ext); | |
189 | |
190 if (docfilename->equals(srcfile->name)) | |
191 { error("Source file and documentation file have same name '%s'", srcfile->name->str); | |
192 fatal(); | |
193 } | |
194 | |
195 docfile = new File(docfilename); | |
196 } | |
197 | |
198 void Module::setHdrfile() | |
199 { | |
200 FileName *hdrfilename; | |
201 char *arghdr; | |
202 | |
203 if (global.params.hdrname) | |
204 arghdr = global.params.hdrname; | |
205 else if (global.params.preservePaths) | |
206 arghdr = (char *)arg; | |
207 else | |
208 arghdr = FileName::name((char *)arg); | |
209 if (!FileName::absolute(arghdr)) | |
210 { //FileName::ensurePathExists(global.params.hdrdir); | |
211 arghdr = FileName::combine(global.params.hdrdir, arghdr); | |
212 } | |
213 if (global.params.hdrname) | |
214 hdrfilename = new FileName(arghdr, 0); | |
215 else | |
216 hdrfilename = FileName::forceExt(arghdr, global.hdr_ext); | |
217 | |
218 if (hdrfilename->equals(srcfile->name)) | |
219 { error("Source file and 'header' file have same name '%s'", srcfile->name->str); | |
220 fatal(); | |
221 } | |
222 | |
223 hdrfile = new File(hdrfilename); | |
224 } | |
225 | |
226 void Module::deleteObjFile() | |
227 { | |
228 if (global.params.obj) | |
229 objfile->remove(); | |
230 //if (global.params.llvmBC) | |
231 bcfile->remove(); | |
232 if (docfile) | |
233 docfile->remove(); | |
234 } | |
235 | |
236 Module::~Module() | |
237 { | |
238 } | |
239 | |
240 const char *Module::kind() | |
241 { | |
242 return "module"; | |
243 } | |
244 | |
245 Module *Module::load(Loc loc, Array *packages, Identifier *ident) | |
246 { Module *m; | |
247 char *filename; | |
248 | |
249 //printf("Module::load(ident = '%s')\n", ident->toChars()); | |
250 | |
251 // Build module filename by turning: | |
252 // foo.bar.baz | |
253 // into: | |
254 // foo\bar\baz | |
255 filename = ident->toChars(); | |
256 if (packages && packages->dim) | |
257 { | |
258 OutBuffer buf; | |
259 int i; | |
260 | |
261 for (i = 0; i < packages->dim; i++) | |
262 { Identifier *pid = (Identifier *)packages->data[i]; | |
263 | |
264 buf.writestring(pid->toChars()); | |
265 #if _WIN32 | |
266 buf.writeByte('\\'); | |
267 #else | |
268 buf.writeByte('/'); | |
269 #endif | |
270 } | |
271 buf.writestring(filename); | |
272 buf.writeByte(0); | |
273 filename = (char *)buf.extractData(); | |
274 } | |
275 | |
276 m = new Module(filename, ident, 0, 0); | |
277 m->loc = loc; | |
278 | |
279 /* Search along global.path for .di file, then .d file. | |
280 */ | |
281 char *result = NULL; | |
282 FileName *fdi = FileName::forceExt(filename, global.hdr_ext); | |
283 FileName *fd = FileName::forceExt(filename, global.mars_ext); | |
284 char *sdi = fdi->toChars(); | |
285 char *sd = fd->toChars(); | |
286 | |
287 if (FileName::exists(sdi)) | |
288 result = sdi; | |
289 else if (FileName::exists(sd)) | |
290 result = sd; | |
291 else if (FileName::absolute(filename)) | |
292 ; | |
293 else if (!global.path) | |
294 ; | |
295 else | |
296 { | |
297 for (size_t i = 0; i < global.path->dim; i++) | |
298 { | |
299 char *p = (char *)global.path->data[i]; | |
300 char *n = FileName::combine(p, sdi); | |
301 if (FileName::exists(n)) | |
302 { result = n; | |
303 break; | |
304 } | |
305 mem.free(n); | |
306 n = FileName::combine(p, sd); | |
307 if (FileName::exists(n)) | |
308 { result = n; | |
309 break; | |
310 } | |
311 mem.free(n); | |
312 } | |
313 } | |
314 if (result) | |
315 m->srcfile = new File(result); | |
316 | |
317 if (global.params.verbose) | |
318 { | |
319 printf("import "); | |
320 if (packages) | |
321 { | |
322 for (size_t i = 0; i < packages->dim; i++) | |
323 { Identifier *pid = (Identifier *)packages->data[i]; | |
324 printf("%s.", pid->toChars()); | |
325 } | |
326 } | |
327 printf("%s\t(%s)\n", ident->toChars(), m->srcfile->toChars()); | |
328 } | |
329 | |
330 m->read(loc); | |
331 m->parse(); | |
332 | |
333 #ifdef IN_GCC | |
334 d_gcc_magic_module(m); | |
335 #endif | |
336 | |
337 return m; | |
338 } | |
339 | |
340 void Module::read(Loc loc) | |
341 { | |
342 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars()); | |
343 if (srcfile->read()) | |
344 { error(loc, "cannot read file '%s'", srcfile->toChars()); | |
345 fatal(); | |
346 } | |
347 } | |
348 | |
349 inline unsigned readwordLE(unsigned short *p) | |
350 { | |
351 #if __I86__ | |
352 return *p; | |
353 #else | |
354 return (((unsigned char *)p)[1] << 8) | ((unsigned char *)p)[0]; | |
355 #endif | |
356 } | |
357 | |
358 inline unsigned readwordBE(unsigned short *p) | |
359 { | |
360 return (((unsigned char *)p)[0] << 8) | ((unsigned char *)p)[1]; | |
361 } | |
362 | |
363 inline unsigned readlongLE(unsigned *p) | |
364 { | |
365 #if __I86__ | |
366 return *p; | |
367 #else | |
368 return ((unsigned char *)p)[0] | | |
369 (((unsigned char *)p)[1] << 8) | | |
370 (((unsigned char *)p)[2] << 16) | | |
371 (((unsigned char *)p)[3] << 24); | |
372 #endif | |
373 } | |
374 | |
375 inline unsigned readlongBE(unsigned *p) | |
376 { | |
377 return ((unsigned char *)p)[3] | | |
378 (((unsigned char *)p)[2] << 8) | | |
379 (((unsigned char *)p)[1] << 16) | | |
380 (((unsigned char *)p)[0] << 24); | |
381 } | |
382 | |
383 #if IN_GCC | |
384 void Module::parse(bool dump_source) | |
385 #else | |
386 void Module::parse() | |
387 #endif | |
388 { char *srcname; | |
389 unsigned char *buf; | |
390 unsigned buflen; | |
391 unsigned le; | |
392 unsigned bom; | |
393 | |
394 //printf("Module::parse()\n"); | |
395 | |
396 srcname = srcfile->name->toChars(); | |
397 //printf("Module::parse(srcname = '%s')\n", srcname); | |
398 | |
399 buf = srcfile->buffer; | |
400 buflen = srcfile->len; | |
401 | |
402 if (buflen >= 2) | |
403 { | |
404 /* Convert all non-UTF-8 formats to UTF-8. | |
405 * BOM : http://www.unicode.org/faq/utf_bom.html | |
406 * 00 00 FE FF UTF-32BE, big-endian | |
407 * FF FE 00 00 UTF-32LE, little-endian | |
408 * FE FF UTF-16BE, big-endian | |
409 * FF FE UTF-16LE, little-endian | |
410 * EF BB BF UTF-8 | |
411 */ | |
412 | |
413 bom = 1; // assume there's a BOM | |
414 if (buf[0] == 0xFF && buf[1] == 0xFE) | |
415 { | |
416 if (buflen >= 4 && buf[2] == 0 && buf[3] == 0) | |
417 { // UTF-32LE | |
418 le = 1; | |
419 | |
420 Lutf32: | |
421 OutBuffer dbuf; | |
422 unsigned *pu = (unsigned *)(buf); | |
423 unsigned *pumax = &pu[buflen / 4]; | |
424 | |
425 if (buflen & 3) | |
426 { error("odd length of UTF-32 char source %u", buflen); | |
427 fatal(); | |
428 } | |
429 | |
430 dbuf.reserve(buflen / 4); | |
431 for (pu += bom; pu < pumax; pu++) | |
432 { unsigned u; | |
433 | |
434 u = le ? readlongLE(pu) : readlongBE(pu); | |
435 if (u & ~0x7F) | |
436 { | |
437 if (u > 0x10FFFF) | |
438 { error("UTF-32 value %08x greater than 0x10FFFF", u); | |
439 fatal(); | |
440 } | |
441 dbuf.writeUTF8(u); | |
442 } | |
443 else | |
444 dbuf.writeByte(u); | |
445 } | |
446 dbuf.writeByte(0); // add 0 as sentinel for scanner | |
447 buflen = dbuf.offset - 1; // don't include sentinel in count | |
448 buf = (unsigned char *) dbuf.extractData(); | |
449 } | |
450 else | |
451 { // UTF-16LE (X86) | |
452 // Convert it to UTF-8 | |
453 le = 1; | |
454 | |
455 Lutf16: | |
456 OutBuffer dbuf; | |
457 unsigned short *pu = (unsigned short *)(buf); | |
458 unsigned short *pumax = &pu[buflen / 2]; | |
459 | |
460 if (buflen & 1) | |
461 { error("odd length of UTF-16 char source %u", buflen); | |
462 fatal(); | |
463 } | |
464 | |
465 dbuf.reserve(buflen / 2); | |
466 for (pu += bom; pu < pumax; pu++) | |
467 { unsigned u; | |
468 | |
469 u = le ? readwordLE(pu) : readwordBE(pu); | |
470 if (u & ~0x7F) | |
471 { if (u >= 0xD800 && u <= 0xDBFF) | |
472 { unsigned u2; | |
473 | |
474 if (++pu > pumax) | |
475 { error("surrogate UTF-16 high value %04x at EOF", u); | |
476 fatal(); | |
477 } | |
478 u2 = le ? readwordLE(pu) : readwordBE(pu); | |
479 if (u2 < 0xDC00 || u2 > 0xDFFF) | |
480 { error("surrogate UTF-16 low value %04x out of range", u2); | |
481 fatal(); | |
482 } | |
483 u = (u - 0xD7C0) << 10; | |
484 u |= (u2 - 0xDC00); | |
485 } | |
486 else if (u >= 0xDC00 && u <= 0xDFFF) | |
487 { error("unpaired surrogate UTF-16 value %04x", u); | |
488 fatal(); | |
489 } | |
490 else if (u == 0xFFFE || u == 0xFFFF) | |
491 { error("illegal UTF-16 value %04x", u); | |
492 fatal(); | |
493 } | |
494 dbuf.writeUTF8(u); | |
495 } | |
496 else | |
497 dbuf.writeByte(u); | |
498 } | |
499 dbuf.writeByte(0); // add 0 as sentinel for scanner | |
500 buflen = dbuf.offset - 1; // don't include sentinel in count | |
501 buf = (unsigned char *) dbuf.extractData(); | |
502 } | |
503 } | |
504 else if (buf[0] == 0xFE && buf[1] == 0xFF) | |
505 { // UTF-16BE | |
506 le = 0; | |
507 goto Lutf16; | |
508 } | |
509 else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF) | |
510 { // UTF-32BE | |
511 le = 0; | |
512 goto Lutf32; | |
513 } | |
514 else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF) | |
515 { // UTF-8 | |
516 | |
517 buf += 3; | |
518 buflen -= 3; | |
519 } | |
520 else | |
521 { | |
522 /* There is no BOM. Make use of Arcane Jill's insight that | |
523 * the first char of D source must be ASCII to | |
524 * figure out the encoding. | |
525 */ | |
526 | |
527 bom = 0; | |
528 if (buflen >= 4) | |
529 { if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0) | |
530 { // UTF-32LE | |
531 le = 1; | |
532 goto Lutf32; | |
533 } | |
534 else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0) | |
535 { // UTF-32BE | |
536 le = 0; | |
537 goto Lutf32; | |
538 } | |
539 } | |
540 if (buflen >= 2) | |
541 { | |
542 if (buf[1] == 0) | |
543 { // UTF-16LE | |
544 le = 1; | |
545 goto Lutf16; | |
546 } | |
547 else if (buf[0] == 0) | |
548 { // UTF-16BE | |
549 le = 0; | |
550 goto Lutf16; | |
551 } | |
552 } | |
553 | |
554 // It's UTF-8 | |
555 if (buf[0] >= 0x80) | |
556 { error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]); | |
557 fatal(); | |
558 } | |
559 } | |
560 } | |
561 | |
562 #ifdef IN_GCC | |
563 // dump utf-8 encoded source | |
564 if (dump_source) | |
565 { // %% srcname could contain a path ... | |
566 d_gcc_dump_source(srcname, "utf-8", buf, buflen); | |
567 } | |
568 #endif | |
569 | |
570 /* If it starts with the string "Ddoc", then it's a documentation | |
571 * source file. | |
572 */ | |
573 if (buflen >= 4 && memcmp(buf, "Ddoc", 4) == 0) | |
574 { | |
575 comment = buf + 4; | |
576 isDocFile = 1; | |
577 if (!docfile) | |
578 setDocfile(); | |
579 return; | |
580 } | |
581 if (isHtml) | |
582 { | |
583 OutBuffer *dbuf = new OutBuffer(); | |
584 Html h(srcname, buf, buflen); | |
585 h.extractCode(dbuf); | |
586 buf = dbuf->data; | |
587 buflen = dbuf->offset; | |
588 #ifdef IN_GCC | |
589 // dump extracted source | |
590 if (dump_source) | |
591 d_gcc_dump_source(srcname, "d.utf-8", buf, buflen); | |
592 #endif | |
593 } | |
594 Parser p(this, buf, buflen, docfile != NULL); | |
595 p.nextToken(); | |
596 members = p.parseModule(); | |
597 md = p.md; | |
598 numlines = p.loc.linnum; | |
599 | |
600 DsymbolTable *dst; | |
601 | |
602 if (md) | |
603 { this->ident = md->id; | |
604 dst = Package::resolve(md->packages, &this->parent, NULL); | |
605 } | |
606 else | |
607 { | |
608 dst = modules; | |
609 | |
610 /* Check to see if module name is a valid identifier | |
611 */ | |
612 if (!Lexer::isValidIdentifier(this->ident->toChars())) | |
613 error("has non-identifier characters in filename, use module declaration instead"); | |
614 } | |
615 | |
616 // Update global list of modules | |
617 if (!dst->insert(this)) | |
618 { | |
619 if (md) | |
620 error(loc, "is in multiple packages %s", md->toChars()); | |
621 else | |
622 error(loc, "is in multiple defined"); | |
623 } | |
624 else | |
625 { | |
626 amodules.push(this); | |
627 } | |
628 } | |
629 | |
630 void Module::semantic() | |
631 { int i; | |
632 | |
633 if (semanticstarted) | |
634 return; | |
635 | |
636 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); | |
637 semanticstarted = 1; | |
638 | |
639 // Note that modules get their own scope, from scratch. | |
640 // This is so regardless of where in the syntax a module | |
641 // gets imported, it is unaffected by context. | |
642 Scope *sc = Scope::createGlobal(this); // create root scope | |
643 | |
644 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage); | |
645 | |
646 // Add import of "object" if this module isn't "object" | |
647 if (ident != Id::object) | |
648 { | |
649 Import *im = new Import(0, NULL, Id::object, NULL, 0); | |
650 members->shift(im); | |
651 } | |
652 | |
653 // Add all symbols into module's symbol table | |
654 symtab = new DsymbolTable(); | |
655 for (i = 0; i < members->dim; i++) | |
656 { Dsymbol *s; | |
657 | |
658 s = (Dsymbol *)members->data[i]; | |
659 s->addMember(NULL, sc->scopesym, 1); | |
660 } | |
661 | |
662 // Pass 1 semantic routines: do public side of the definition | |
663 for (i = 0; i < members->dim; i++) | |
664 { Dsymbol *s; | |
665 | |
666 s = (Dsymbol *)members->data[i]; | |
667 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars()); | |
668 s->semantic(sc); | |
669 runDeferredSemantic(); | |
670 } | |
671 | |
672 sc = sc->pop(); | |
673 sc->pop(); | |
674 semanticdone = semanticstarted; | |
675 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); | |
676 } | |
677 | |
678 void Module::semantic2() | |
679 { int i; | |
680 | |
681 if (deferred.dim) | |
682 { | |
683 for (int i = 0; i < deferred.dim; i++) | |
684 { | |
685 Dsymbol *sd = (Dsymbol *)deferred.data[i]; | |
686 | |
687 sd->error("unable to resolve forward reference in definition"); | |
688 } | |
689 return; | |
690 } | |
691 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent); | |
692 if (semanticstarted >= 2) | |
693 return; | |
694 assert(semanticstarted == 1); | |
695 semanticstarted = 2; | |
696 | |
697 // Note that modules get their own scope, from scratch. | |
698 // This is so regardless of where in the syntax a module | |
699 // gets imported, it is unaffected by context. | |
700 Scope *sc = Scope::createGlobal(this); // create root scope | |
701 //printf("Module = %p\n", sc.scopesym); | |
702 | |
703 // Pass 2 semantic routines: do initializers and function bodies | |
704 for (i = 0; i < members->dim; i++) | |
705 { Dsymbol *s; | |
706 | |
707 s = (Dsymbol *)members->data[i]; | |
708 s->semantic2(sc); | |
709 } | |
710 | |
711 sc = sc->pop(); | |
712 sc->pop(); | |
713 semanticdone = semanticstarted; | |
714 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent); | |
715 } | |
716 | |
717 void Module::semantic3() | |
718 { int i; | |
719 | |
720 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent); | |
721 if (semanticstarted >= 3) | |
722 return; | |
723 assert(semanticstarted == 2); | |
724 semanticstarted = 3; | |
725 | |
726 // Note that modules get their own scope, from scratch. | |
727 // This is so regardless of where in the syntax a module | |
728 // gets imported, it is unaffected by context. | |
729 Scope *sc = Scope::createGlobal(this); // create root scope | |
730 //printf("Module = %p\n", sc.scopesym); | |
731 | |
732 // Pass 3 semantic routines: do initializers and function bodies | |
733 for (i = 0; i < members->dim; i++) | |
734 { Dsymbol *s; | |
735 | |
736 s = (Dsymbol *)members->data[i]; | |
737 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars()); | |
738 s->semantic3(sc); | |
739 } | |
740 | |
741 sc = sc->pop(); | |
742 sc->pop(); | |
743 semanticdone = semanticstarted; | |
744 } | |
745 | |
746 void Module::inlineScan() | |
747 { int i; | |
748 | |
749 if (semanticstarted >= 4) | |
750 return; | |
751 assert(semanticstarted == 3); | |
752 semanticstarted = 4; | |
753 | |
754 // Note that modules get their own scope, from scratch. | |
755 // This is so regardless of where in the syntax a module | |
756 // gets imported, it is unaffected by context. | |
757 //printf("Module = %p\n", sc.scopesym); | |
758 | |
759 for (i = 0; i < members->dim; i++) | |
760 { Dsymbol *s; | |
761 | |
762 s = (Dsymbol *)members->data[i]; | |
763 //if (global.params.verbose) | |
764 //printf("inline scan symbol %s\n", s->toChars()); | |
765 | |
766 s->inlineScan(); | |
767 } | |
768 semanticdone = semanticstarted; | |
769 } | |
770 | |
771 /**************************************************** | |
772 */ | |
773 | |
774 void Module::gensymfile() | |
775 { | |
776 OutBuffer buf; | |
777 HdrGenState hgs; | |
778 | |
779 //printf("Module::gensymfile()\n"); | |
780 | |
781 buf.printf("// Sym file generated from '%s'", srcfile->toChars()); | |
782 buf.writenl(); | |
783 | |
784 for (int i = 0; i < members->dim; i++) | |
785 { Dsymbol *s = (Dsymbol *)members->data[i]; | |
786 | |
787 s->toCBuffer(&buf, &hgs); | |
788 } | |
789 | |
790 // Transfer image to file | |
791 symfile->setbuffer(buf.data, buf.offset); | |
792 buf.data = NULL; | |
793 | |
794 symfile->writev(); | |
795 } | |
796 | |
797 /********************************** | |
798 * Determine if we need to generate an instance of ModuleInfo | |
799 * for this Module. | |
800 */ | |
801 | |
802 int Module::needModuleInfo() | |
803 { | |
804 return needmoduleinfo || global.params.cov; | |
805 } | |
806 | |
807 Dsymbol *Module::search(Loc loc, Identifier *ident, int flags) | |
808 { | |
809 /* Since modules can be circularly referenced, | |
810 * need to stop infinite recursive searches. | |
811 */ | |
812 | |
813 //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch); | |
814 Dsymbol *s; | |
815 if (insearch) | |
816 s = NULL; | |
817 else if (searchCacheIdent == ident && searchCacheFlags == flags) | |
818 s = searchCacheSymbol; | |
819 else | |
820 { | |
821 insearch = 1; | |
822 s = ScopeDsymbol::search(loc, ident, flags); | |
823 insearch = 0; | |
824 | |
825 searchCacheIdent = ident; | |
826 searchCacheSymbol = s; | |
827 searchCacheFlags = flags; | |
828 } | |
829 return s; | |
830 } | |
831 | |
832 /******************************************* | |
833 * Can't run semantic on s now, try again later. | |
834 */ | |
835 | |
836 void Module::addDeferredSemantic(Dsymbol *s) | |
837 { | |
838 // Don't add it if it is already there | |
839 for (int i = 0; i < deferred.dim; i++) | |
840 { | |
841 Dsymbol *sd = (Dsymbol *)deferred.data[i]; | |
842 | |
843 if (sd == s) | |
844 return; | |
845 } | |
846 | |
847 //printf("Module::addDeferredSemantic('%s')\n", s->toChars()); | |
848 deferred.push(s); | |
849 } | |
850 | |
851 | |
852 /****************************************** | |
853 * Run semantic() on deferred symbols. | |
854 */ | |
855 | |
856 void Module::runDeferredSemantic() | |
857 { | |
858 size_t len; | |
859 | |
860 static int nested; | |
861 if (nested) | |
862 return; | |
863 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim); | |
864 nested++; | |
865 | |
866 do | |
867 { | |
868 dprogress = 0; | |
869 len = deferred.dim; | |
870 if (!len) | |
871 break; | |
872 | |
873 Dsymbol **todo; | |
874 Dsymbol *tmp; | |
875 if (len == 1) | |
876 { | |
877 todo = &tmp; | |
878 } | |
879 else | |
880 { | |
881 todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *)); | |
882 assert(todo); | |
883 } | |
884 memcpy(todo, deferred.data, len * sizeof(Dsymbol *)); | |
885 deferred.setDim(0); | |
886 | |
887 for (int i = 0; i < len; i++) | |
888 { | |
889 Dsymbol *s = todo[i]; | |
890 | |
891 s->semantic(NULL); | |
892 //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars()); | |
893 } | |
894 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress); | |
895 } while (deferred.dim < len || dprogress); // while making progress | |
896 nested--; | |
897 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim); | |
898 } | |
899 | |
900 /* =========================== ModuleDeclaration ===================== */ | |
901 | |
902 ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id) | |
903 { | |
904 this->packages = packages; | |
905 this->id = id; | |
906 } | |
907 | |
908 char *ModuleDeclaration::toChars() | |
909 { | |
910 OutBuffer buf; | |
911 int i; | |
912 | |
913 if (packages && packages->dim) | |
914 { | |
915 for (i = 0; i < packages->dim; i++) | |
916 { Identifier *pid = (Identifier *)packages->data[i]; | |
917 | |
918 buf.writestring(pid->toChars()); | |
919 buf.writeByte('.'); | |
920 } | |
921 } | |
922 buf.writestring(id->toChars()); | |
923 buf.writeByte(0); | |
924 return (char *)buf.extractData(); | |
925 } | |
926 | |
927 /* =========================== Package ===================== */ | |
928 | |
929 Package::Package(Identifier *ident) | |
930 : ScopeDsymbol(ident) | |
931 { | |
932 } | |
933 | |
934 | |
935 const char *Package::kind() | |
936 { | |
937 return "package"; | |
938 } | |
939 | |
940 | |
941 DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg) | |
942 { | |
943 DsymbolTable *dst = Module::modules; | |
944 Dsymbol *parent = NULL; | |
945 | |
946 //printf("Package::resolve()\n"); | |
947 if (ppkg) | |
948 *ppkg = NULL; | |
949 | |
950 if (packages) | |
951 { int i; | |
952 | |
953 for (i = 0; i < packages->dim; i++) | |
954 { Identifier *pid = (Identifier *)packages->data[i]; | |
955 Dsymbol *p; | |
956 | |
957 p = dst->lookup(pid); | |
958 if (!p) | |
959 { | |
960 p = new Package(pid); | |
961 dst->insert(p); | |
962 p->parent = parent; | |
963 ((ScopeDsymbol *)p)->symtab = new DsymbolTable(); | |
964 } | |
965 else | |
966 { | |
967 assert(p->isPackage()); | |
968 if (p->isModule()) | |
969 { p->error("module and package have the same name"); | |
970 fatal(); | |
971 break; | |
972 } | |
973 } | |
974 parent = p; | |
975 dst = ((Package *)p)->symtab; | |
976 if (ppkg && !*ppkg) | |
977 *ppkg = (Package *)p; | |
978 } | |
979 if (pparent) | |
980 { | |
981 *pparent = parent; | |
982 } | |
983 } | |
984 return dst; | |
985 } |