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