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