Mercurial > projects > ldc
annotate dmd/module.c @ 550:cbe08531430f
Removed unimplemented switches.
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Tue, 26 Aug 2008 21:02:25 +0200 |
parents | 6aee82889553 |
children | 7824c21a58e3 |
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) && | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
115 !srcfilename->equalsExt(global.hdr_ext) && |
336 | 116 !srcfilename->equalsExt("dd")) |
117 { | |
118 if (srcfilename->equalsExt("html") || | |
119 srcfilename->equalsExt("htm") || | |
120 srcfilename->equalsExt("xhtml")) | |
121 isHtml = 1; | |
122 else | |
123 { error("source file name '%s' must have .%s extension", srcfilename->toChars(), global.mars_ext); | |
124 fatal(); | |
125 } | |
126 } | |
127 | |
128 char *argobj; | |
129 if (global.params.objname) | |
130 argobj = global.params.objname; | |
131 else if (global.params.preservePaths) | |
132 argobj = filename; | |
133 else | |
134 argobj = FileName::name(filename); | |
135 if (!FileName::absolute(argobj)) | |
136 { | |
137 argobj = FileName::combine(global.params.objdir, argobj); | |
138 } | |
139 | |
140 if (global.params.objname) | |
141 objfilename = new FileName(argobj, 0); | |
142 else | |
143 objfilename = FileName::forceExt(argobj, global.obj_ext); | |
144 | |
145 llfilename = FileName::forceExt(argobj, global.ll_ext); | |
146 bcfilename = FileName::forceExt(argobj, global.bc_ext); | |
147 | |
148 symfilename = FileName::forceExt(filename, global.sym_ext); | |
149 | |
150 srcfile = new File(srcfilename); | |
151 | |
152 if (doDocComment) | |
153 { | |
154 setDocfile(); | |
155 } | |
156 | |
157 if (doHdrGen) | |
158 { | |
159 setHdrfile(); | |
160 } | |
161 | |
162 objfile = new File(objfilename); | |
163 bcfile = new File(bcfilename); | |
164 llfile = new File(llfilename); | |
165 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
|
166 |
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 // 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
|
168 llvmForceLogging = false; |
336 | 169 } |
170 | |
171 void Module::setDocfile() | |
172 { | |
173 FileName *docfilename; | |
174 char *argdoc; | |
175 | |
176 if (global.params.docname) | |
177 argdoc = global.params.docname; | |
178 else if (global.params.preservePaths) | |
179 argdoc = (char *)arg; | |
180 else | |
181 argdoc = FileName::name((char *)arg); | |
182 if (!FileName::absolute(argdoc)) | |
183 { //FileName::ensurePathExists(global.params.docdir); | |
184 argdoc = FileName::combine(global.params.docdir, argdoc); | |
185 } | |
186 if (global.params.docname) | |
187 docfilename = new FileName(argdoc, 0); | |
188 else | |
189 docfilename = FileName::forceExt(argdoc, global.doc_ext); | |
190 | |
191 if (docfilename->equals(srcfile->name)) | |
192 { error("Source file and documentation file have same name '%s'", srcfile->name->str); | |
193 fatal(); | |
194 } | |
195 | |
196 docfile = new File(docfilename); | |
197 } | |
198 | |
199 void Module::setHdrfile() | |
200 { | |
201 FileName *hdrfilename; | |
202 char *arghdr; | |
203 | |
204 if (global.params.hdrname) | |
205 arghdr = global.params.hdrname; | |
206 else if (global.params.preservePaths) | |
207 arghdr = (char *)arg; | |
208 else | |
209 arghdr = FileName::name((char *)arg); | |
210 if (!FileName::absolute(arghdr)) | |
211 { //FileName::ensurePathExists(global.params.hdrdir); | |
212 arghdr = FileName::combine(global.params.hdrdir, arghdr); | |
213 } | |
214 if (global.params.hdrname) | |
215 hdrfilename = new FileName(arghdr, 0); | |
216 else | |
217 hdrfilename = FileName::forceExt(arghdr, global.hdr_ext); | |
218 | |
219 if (hdrfilename->equals(srcfile->name)) | |
220 { error("Source file and 'header' file have same name '%s'", srcfile->name->str); | |
221 fatal(); | |
222 } | |
223 | |
224 hdrfile = new File(hdrfilename); | |
225 } | |
226 | |
227 void Module::deleteObjFile() | |
228 { | |
229 if (global.params.obj) | |
230 objfile->remove(); | |
231 //if (global.params.llvmBC) | |
232 bcfile->remove(); | |
233 if (docfile) | |
234 docfile->remove(); | |
235 } | |
236 | |
237 Module::~Module() | |
238 { | |
239 } | |
240 | |
241 const char *Module::kind() | |
242 { | |
243 return "module"; | |
244 } | |
245 | |
246 Module *Module::load(Loc loc, Array *packages, Identifier *ident) | |
247 { Module *m; | |
248 char *filename; | |
249 | |
250 //printf("Module::load(ident = '%s')\n", ident->toChars()); | |
251 | |
252 // Build module filename by turning: | |
253 // foo.bar.baz | |
254 // into: | |
255 // foo\bar\baz | |
256 filename = ident->toChars(); | |
257 if (packages && packages->dim) | |
258 { | |
259 OutBuffer buf; | |
260 int i; | |
261 | |
262 for (i = 0; i < packages->dim; i++) | |
263 { Identifier *pid = (Identifier *)packages->data[i]; | |
264 | |
265 buf.writestring(pid->toChars()); | |
266 #if _WIN32 | |
267 buf.writeByte('\\'); | |
268 #else | |
269 buf.writeByte('/'); | |
270 #endif | |
271 } | |
272 buf.writestring(filename); | |
273 buf.writeByte(0); | |
274 filename = (char *)buf.extractData(); | |
275 } | |
276 | |
277 m = new Module(filename, ident, 0, 0); | |
278 m->loc = loc; | |
279 | |
280 /* Search along global.path for .di file, then .d file. | |
281 */ | |
282 char *result = NULL; | |
283 FileName *fdi = FileName::forceExt(filename, global.hdr_ext); | |
284 FileName *fd = FileName::forceExt(filename, global.mars_ext); | |
285 char *sdi = fdi->toChars(); | |
286 char *sd = fd->toChars(); | |
287 | |
288 if (FileName::exists(sdi)) | |
289 result = sdi; | |
290 else if (FileName::exists(sd)) | |
291 result = sd; | |
292 else if (FileName::absolute(filename)) | |
293 ; | |
294 else if (!global.path) | |
295 ; | |
296 else | |
297 { | |
298 for (size_t i = 0; i < global.path->dim; i++) | |
299 { | |
300 char *p = (char *)global.path->data[i]; | |
301 char *n = FileName::combine(p, sdi); | |
302 if (FileName::exists(n)) | |
303 { result = n; | |
304 break; | |
305 } | |
306 mem.free(n); | |
307 n = FileName::combine(p, sd); | |
308 if (FileName::exists(n)) | |
309 { result = n; | |
310 break; | |
311 } | |
312 mem.free(n); | |
313 } | |
314 } | |
315 if (result) | |
316 m->srcfile = new File(result); | |
317 | |
318 if (global.params.verbose) | |
319 { | |
320 printf("import "); | |
321 if (packages) | |
322 { | |
323 for (size_t i = 0; i < packages->dim; i++) | |
324 { Identifier *pid = (Identifier *)packages->data[i]; | |
325 printf("%s.", pid->toChars()); | |
326 } | |
327 } | |
328 printf("%s\t(%s)\n", ident->toChars(), m->srcfile->toChars()); | |
329 } | |
330 | |
331 m->read(loc); | |
332 m->parse(); | |
333 | |
334 #ifdef IN_GCC | |
335 d_gcc_magic_module(m); | |
336 #endif | |
337 | |
338 return m; | |
339 } | |
340 | |
341 void Module::read(Loc loc) | |
342 { | |
343 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars()); | |
344 if (srcfile->read()) | |
345 { error(loc, "cannot read file '%s'", srcfile->toChars()); | |
346 fatal(); | |
347 } | |
348 } | |
349 | |
350 inline unsigned readwordLE(unsigned short *p) | |
351 { | |
352 #if __I86__ | |
353 return *p; | |
354 #else | |
355 return (((unsigned char *)p)[1] << 8) | ((unsigned char *)p)[0]; | |
356 #endif | |
357 } | |
358 | |
359 inline unsigned readwordBE(unsigned short *p) | |
360 { | |
361 return (((unsigned char *)p)[0] << 8) | ((unsigned char *)p)[1]; | |
362 } | |
363 | |
364 inline unsigned readlongLE(unsigned *p) | |
365 { | |
366 #if __I86__ | |
367 return *p; | |
368 #else | |
369 return ((unsigned char *)p)[0] | | |
370 (((unsigned char *)p)[1] << 8) | | |
371 (((unsigned char *)p)[2] << 16) | | |
372 (((unsigned char *)p)[3] << 24); | |
373 #endif | |
374 } | |
375 | |
376 inline unsigned readlongBE(unsigned *p) | |
377 { | |
378 return ((unsigned char *)p)[3] | | |
379 (((unsigned char *)p)[2] << 8) | | |
380 (((unsigned char *)p)[1] << 16) | | |
381 (((unsigned char *)p)[0] << 24); | |
382 } | |
383 | |
384 #if IN_GCC | |
385 void Module::parse(bool dump_source) | |
386 #else | |
387 void Module::parse() | |
388 #endif | |
389 { char *srcname; | |
390 unsigned char *buf; | |
391 unsigned buflen; | |
392 unsigned le; | |
393 unsigned bom; | |
394 | |
395 //printf("Module::parse()\n"); | |
396 | |
397 srcname = srcfile->name->toChars(); | |
398 //printf("Module::parse(srcname = '%s')\n", srcname); | |
399 | |
400 buf = srcfile->buffer; | |
401 buflen = srcfile->len; | |
402 | |
403 if (buflen >= 2) | |
404 { | |
405 /* Convert all non-UTF-8 formats to UTF-8. | |
406 * BOM : http://www.unicode.org/faq/utf_bom.html | |
407 * 00 00 FE FF UTF-32BE, big-endian | |
408 * FF FE 00 00 UTF-32LE, little-endian | |
409 * FE FF UTF-16BE, big-endian | |
410 * FF FE UTF-16LE, little-endian | |
411 * EF BB BF UTF-8 | |
412 */ | |
413 | |
414 bom = 1; // assume there's a BOM | |
415 if (buf[0] == 0xFF && buf[1] == 0xFE) | |
416 { | |
417 if (buflen >= 4 && buf[2] == 0 && buf[3] == 0) | |
418 { // UTF-32LE | |
419 le = 1; | |
420 | |
421 Lutf32: | |
422 OutBuffer dbuf; | |
423 unsigned *pu = (unsigned *)(buf); | |
424 unsigned *pumax = &pu[buflen / 4]; | |
425 | |
426 if (buflen & 3) | |
427 { error("odd length of UTF-32 char source %u", buflen); | |
428 fatal(); | |
429 } | |
430 | |
431 dbuf.reserve(buflen / 4); | |
432 for (pu += bom; pu < pumax; pu++) | |
433 { unsigned u; | |
434 | |
435 u = le ? readlongLE(pu) : readlongBE(pu); | |
436 if (u & ~0x7F) | |
437 { | |
438 if (u > 0x10FFFF) | |
439 { error("UTF-32 value %08x greater than 0x10FFFF", u); | |
440 fatal(); | |
441 } | |
442 dbuf.writeUTF8(u); | |
443 } | |
444 else | |
445 dbuf.writeByte(u); | |
446 } | |
447 dbuf.writeByte(0); // add 0 as sentinel for scanner | |
448 buflen = dbuf.offset - 1; // don't include sentinel in count | |
449 buf = (unsigned char *) dbuf.extractData(); | |
450 } | |
451 else | |
452 { // UTF-16LE (X86) | |
453 // Convert it to UTF-8 | |
454 le = 1; | |
455 | |
456 Lutf16: | |
457 OutBuffer dbuf; | |
458 unsigned short *pu = (unsigned short *)(buf); | |
459 unsigned short *pumax = &pu[buflen / 2]; | |
460 | |
461 if (buflen & 1) | |
462 { error("odd length of UTF-16 char source %u", buflen); | |
463 fatal(); | |
464 } | |
465 | |
466 dbuf.reserve(buflen / 2); | |
467 for (pu += bom; pu < pumax; pu++) | |
468 { unsigned u; | |
469 | |
470 u = le ? readwordLE(pu) : readwordBE(pu); | |
471 if (u & ~0x7F) | |
472 { if (u >= 0xD800 && u <= 0xDBFF) | |
473 { unsigned u2; | |
474 | |
475 if (++pu > pumax) | |
476 { error("surrogate UTF-16 high value %04x at EOF", u); | |
477 fatal(); | |
478 } | |
479 u2 = le ? readwordLE(pu) : readwordBE(pu); | |
480 if (u2 < 0xDC00 || u2 > 0xDFFF) | |
481 { error("surrogate UTF-16 low value %04x out of range", u2); | |
482 fatal(); | |
483 } | |
484 u = (u - 0xD7C0) << 10; | |
485 u |= (u2 - 0xDC00); | |
486 } | |
487 else if (u >= 0xDC00 && u <= 0xDFFF) | |
488 { error("unpaired surrogate UTF-16 value %04x", u); | |
489 fatal(); | |
490 } | |
491 else if (u == 0xFFFE || u == 0xFFFF) | |
492 { error("illegal UTF-16 value %04x", u); | |
493 fatal(); | |
494 } | |
495 dbuf.writeUTF8(u); | |
496 } | |
497 else | |
498 dbuf.writeByte(u); | |
499 } | |
500 dbuf.writeByte(0); // add 0 as sentinel for scanner | |
501 buflen = dbuf.offset - 1; // don't include sentinel in count | |
502 buf = (unsigned char *) dbuf.extractData(); | |
503 } | |
504 } | |
505 else if (buf[0] == 0xFE && buf[1] == 0xFF) | |
506 { // UTF-16BE | |
507 le = 0; | |
508 goto Lutf16; | |
509 } | |
510 else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF) | |
511 { // UTF-32BE | |
512 le = 0; | |
513 goto Lutf32; | |
514 } | |
515 else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF) | |
516 { // UTF-8 | |
517 | |
518 buf += 3; | |
519 buflen -= 3; | |
520 } | |
521 else | |
522 { | |
523 /* There is no BOM. Make use of Arcane Jill's insight that | |
524 * the first char of D source must be ASCII to | |
525 * figure out the encoding. | |
526 */ | |
527 | |
528 bom = 0; | |
529 if (buflen >= 4) | |
530 { if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0) | |
531 { // UTF-32LE | |
532 le = 1; | |
533 goto Lutf32; | |
534 } | |
535 else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0) | |
536 { // UTF-32BE | |
537 le = 0; | |
538 goto Lutf32; | |
539 } | |
540 } | |
541 if (buflen >= 2) | |
542 { | |
543 if (buf[1] == 0) | |
544 { // UTF-16LE | |
545 le = 1; | |
546 goto Lutf16; | |
547 } | |
548 else if (buf[0] == 0) | |
549 { // UTF-16BE | |
550 le = 0; | |
551 goto Lutf16; | |
552 } | |
553 } | |
554 | |
555 // It's UTF-8 | |
556 if (buf[0] >= 0x80) | |
557 { error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]); | |
558 fatal(); | |
559 } | |
560 } | |
561 } | |
562 | |
563 #ifdef IN_GCC | |
564 // dump utf-8 encoded source | |
565 if (dump_source) | |
566 { // %% srcname could contain a path ... | |
567 d_gcc_dump_source(srcname, "utf-8", buf, buflen); | |
568 } | |
569 #endif | |
570 | |
571 /* If it starts with the string "Ddoc", then it's a documentation | |
572 * source file. | |
573 */ | |
574 if (buflen >= 4 && memcmp(buf, "Ddoc", 4) == 0) | |
575 { | |
576 comment = buf + 4; | |
577 isDocFile = 1; | |
578 if (!docfile) | |
579 setDocfile(); | |
580 return; | |
581 } | |
582 if (isHtml) | |
583 { | |
584 OutBuffer *dbuf = new OutBuffer(); | |
585 Html h(srcname, buf, buflen); | |
586 h.extractCode(dbuf); | |
587 buf = dbuf->data; | |
588 buflen = dbuf->offset; | |
589 #ifdef IN_GCC | |
590 // dump extracted source | |
591 if (dump_source) | |
592 d_gcc_dump_source(srcname, "d.utf-8", buf, buflen); | |
593 #endif | |
594 } | |
595 Parser p(this, buf, buflen, docfile != NULL); | |
596 p.nextToken(); | |
597 members = p.parseModule(); | |
598 md = p.md; | |
599 numlines = p.loc.linnum; | |
600 | |
601 DsymbolTable *dst; | |
602 | |
603 if (md) | |
604 { this->ident = md->id; | |
605 dst = Package::resolve(md->packages, &this->parent, NULL); | |
606 } | |
607 else | |
608 { | |
609 dst = modules; | |
610 | |
611 /* Check to see if module name is a valid identifier | |
612 */ | |
613 if (!Lexer::isValidIdentifier(this->ident->toChars())) | |
614 error("has non-identifier characters in filename, use module declaration instead"); | |
615 } | |
616 | |
617 // Update global list of modules | |
618 if (!dst->insert(this)) | |
619 { | |
620 if (md) | |
621 error(loc, "is in multiple packages %s", md->toChars()); | |
622 else | |
623 error(loc, "is in multiple defined"); | |
624 } | |
625 else | |
626 { | |
627 amodules.push(this); | |
628 } | |
629 } | |
630 | |
631 void Module::semantic() | |
632 { int i; | |
633 | |
634 if (semanticstarted) | |
635 return; | |
636 | |
637 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); | |
638 semanticstarted = 1; | |
639 | |
640 // Note that modules get their own scope, from scratch. | |
641 // This is so regardless of where in the syntax a module | |
642 // gets imported, it is unaffected by context. | |
643 Scope *sc = Scope::createGlobal(this); // create root scope | |
644 | |
645 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage); | |
646 | |
647 // Add import of "object" if this module isn't "object" | |
648 if (ident != Id::object) | |
649 { | |
650 Import *im = new Import(0, NULL, Id::object, NULL, 0); | |
651 members->shift(im); | |
652 } | |
653 | |
654 // Add all symbols into module's symbol table | |
655 symtab = new DsymbolTable(); | |
656 for (i = 0; i < members->dim; i++) | |
657 { Dsymbol *s; | |
658 | |
659 s = (Dsymbol *)members->data[i]; | |
660 s->addMember(NULL, sc->scopesym, 1); | |
661 } | |
662 | |
663 // Pass 1 semantic routines: do public side of the definition | |
664 for (i = 0; i < members->dim; i++) | |
665 { Dsymbol *s; | |
666 | |
667 s = (Dsymbol *)members->data[i]; | |
668 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars()); | |
669 s->semantic(sc); | |
670 runDeferredSemantic(); | |
671 } | |
672 | |
673 sc = sc->pop(); | |
674 sc->pop(); | |
675 semanticdone = semanticstarted; | |
676 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); | |
677 } | |
678 | |
679 void Module::semantic2() | |
680 { int i; | |
681 | |
682 if (deferred.dim) | |
683 { | |
684 for (int i = 0; i < deferred.dim; i++) | |
685 { | |
686 Dsymbol *sd = (Dsymbol *)deferred.data[i]; | |
687 | |
688 sd->error("unable to resolve forward reference in definition"); | |
689 } | |
690 return; | |
691 } | |
692 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent); | |
693 if (semanticstarted >= 2) | |
694 return; | |
695 assert(semanticstarted == 1); | |
696 semanticstarted = 2; | |
697 | |
698 // Note that modules get their own scope, from scratch. | |
699 // This is so regardless of where in the syntax a module | |
700 // gets imported, it is unaffected by context. | |
701 Scope *sc = Scope::createGlobal(this); // create root scope | |
702 //printf("Module = %p\n", sc.scopesym); | |
703 | |
704 // Pass 2 semantic routines: do initializers and function bodies | |
705 for (i = 0; i < members->dim; i++) | |
706 { Dsymbol *s; | |
707 | |
708 s = (Dsymbol *)members->data[i]; | |
709 s->semantic2(sc); | |
710 } | |
711 | |
712 sc = sc->pop(); | |
713 sc->pop(); | |
714 semanticdone = semanticstarted; | |
715 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent); | |
716 } | |
717 | |
718 void Module::semantic3() | |
719 { int i; | |
720 | |
721 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent); | |
722 if (semanticstarted >= 3) | |
723 return; | |
724 assert(semanticstarted == 2); | |
725 semanticstarted = 3; | |
726 | |
727 // Note that modules get their own scope, from scratch. | |
728 // This is so regardless of where in the syntax a module | |
729 // gets imported, it is unaffected by context. | |
730 Scope *sc = Scope::createGlobal(this); // create root scope | |
731 //printf("Module = %p\n", sc.scopesym); | |
732 | |
733 // Pass 3 semantic routines: do initializers and function bodies | |
734 for (i = 0; i < members->dim; i++) | |
735 { Dsymbol *s; | |
736 | |
737 s = (Dsymbol *)members->data[i]; | |
738 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars()); | |
739 s->semantic3(sc); | |
740 } | |
741 | |
742 sc = sc->pop(); | |
743 sc->pop(); | |
744 semanticdone = semanticstarted; | |
745 } | |
746 | |
747 void Module::inlineScan() | |
748 { int i; | |
749 | |
750 if (semanticstarted >= 4) | |
751 return; | |
752 assert(semanticstarted == 3); | |
753 semanticstarted = 4; | |
754 | |
755 // Note that modules get their own scope, from scratch. | |
756 // This is so regardless of where in the syntax a module | |
757 // gets imported, it is unaffected by context. | |
758 //printf("Module = %p\n", sc.scopesym); | |
759 | |
760 for (i = 0; i < members->dim; i++) | |
761 { Dsymbol *s; | |
762 | |
763 s = (Dsymbol *)members->data[i]; | |
764 //if (global.params.verbose) | |
765 //printf("inline scan symbol %s\n", s->toChars()); | |
766 | |
767 s->inlineScan(); | |
768 } | |
769 semanticdone = semanticstarted; | |
770 } | |
771 | |
772 /**************************************************** | |
773 */ | |
774 | |
775 void Module::gensymfile() | |
776 { | |
777 OutBuffer buf; | |
778 HdrGenState hgs; | |
779 | |
780 //printf("Module::gensymfile()\n"); | |
781 | |
782 buf.printf("// Sym file generated from '%s'", srcfile->toChars()); | |
783 buf.writenl(); | |
784 | |
785 for (int i = 0; i < members->dim; i++) | |
786 { Dsymbol *s = (Dsymbol *)members->data[i]; | |
787 | |
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 { | |
550
cbe08531430f
Removed unimplemented switches.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
510
diff
changeset
|
805 return needmoduleinfo; |
336 | 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 const 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 } |