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