The expression "x in dict" is now a maybe type
[delight/core.git] / dmd / module.c
blobb436a6484fda75b21657bc02dff2f68134f43cfe
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.
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <assert.h>
15 #ifdef _MSC_VER
16 #include <malloc.h>
17 #endif
19 #if IN_GCC
20 #include "gdc_alloca.h"
21 #endif
23 #include "mem.h"
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 #include "init.h"
37 #define MARS 1
38 #include "html.h"
40 #ifdef IN_GCC
41 #include "d-dmd-gcc.h"
42 #endif
44 ClassDeclaration *Module::moduleinfo;
46 Module *Module::rootModule;
47 DsymbolTable *Module::modules;
48 Array Module::amodules;
50 Array Module::deferred; // deferred Dsymbol's needing semantic() run on them
51 unsigned Module::dprogress;
53 void Module::init()
55 modules = new DsymbolTable();
58 Module::Module(char *filename, Identifier *ident, int doDocComment, int doHdrGen, int isDltFile)
59 : Package(ident), isDltFile(isDltFile)
61 FileName *srcfilename;
62 FileName *cfilename;
63 FileName *hfilename;
64 FileName *objfilename;
65 FileName *symfilename;
67 // printf("Module::Module(filename = '%s', ident = '%s')\n", filename, ident->toChars());
68 this->arg = filename;
69 md = NULL;
70 errors = 0;
71 numlines = 0;
72 members = NULL;
73 isHtml = 0;
74 isDocFile = 0;
75 needmoduleinfo = 0;
76 #ifdef IN_GCC
77 strictlyneedmoduleinfo = 0;
78 #endif
79 insearch = 0;
80 searchCacheIdent = NULL;
81 searchCacheSymbol = NULL;
82 searchCacheFlags = 0;
83 semanticstarted = 0;
84 semanticdone = 0;
85 decldefs = NULL;
86 vmoduleinfo = NULL;
87 massert = NULL;
88 marray = NULL;
89 sictor = 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;
99 debuglevel = 0;
100 debugids = NULL;
101 debugidsNot = NULL;
102 versionlevel = 0;
103 versionids = NULL;
104 versionidsNot = NULL;
106 macrotable = NULL;
107 escapetable = NULL;
108 doppelganger = 0;
109 cov = NULL;
110 covb = NULL;
112 srcfilename = FileName::defaultExt(filename, global.dlt_ext);
113 if (!srcfilename->equalsExt(global.mars_ext) &&
114 !srcfilename->equalsExt(global.dlt_ext) &&
115 !srcfilename->equalsExt("dd"))
117 if (srcfilename->equalsExt("html") ||
118 srcfilename->equalsExt("htm") ||
119 srcfilename->equalsExt("xhtml"))
120 isHtml = 1;
121 else
122 { error("source file name '%s' must have .%s extension", srcfilename->toChars(), global.dlt_ext);
123 fatal();
127 char *argobj;
128 if (global.params.objname)
129 argobj = global.params.objname;
130 else if (global.params.preservePaths)
131 argobj = filename;
132 else
133 argobj = FileName::name(filename);
134 if (!FileName::absolute(argobj))
136 argobj = FileName::combine(global.params.objdir, argobj);
139 if (global.params.objname)
140 objfilename = new FileName(argobj, 0);
141 else
142 objfilename = FileName::forceExt(argobj, global.obj_ext);
144 symfilename = FileName::forceExt(filename, global.sym_ext);
146 srcfile = new File(srcfilename);
148 if (doDocComment)
150 setDocfile();
153 if (doHdrGen)
155 setHdrfile();
158 objfile = new File(objfilename);
159 symfile = new File(symfilename);
162 void Module::setDocfile()
164 FileName *docfilename;
165 char *argdoc;
167 if (global.params.docname)
168 argdoc = global.params.docname;
169 else if (global.params.preservePaths)
170 argdoc = (char *)arg;
171 else
172 argdoc = FileName::name((char *)arg);
173 if (!FileName::absolute(argdoc))
174 { //FileName::ensurePathExists(global.params.docdir);
175 argdoc = FileName::combine(global.params.docdir, argdoc);
177 if (global.params.docname)
178 docfilename = new FileName(argdoc, 0);
179 else
180 docfilename = FileName::forceExt(argdoc, global.doc_ext);
182 if (docfilename->equals(srcfile->name))
183 { error("Source file and documentation file have same name '%s'", srcfile->name->str);
184 fatal();
187 docfile = new File(docfilename);
190 void Module::setHdrfile()
192 FileName *hdrfilename;
193 char *arghdr;
195 if (global.params.hdrname)
196 arghdr = global.params.hdrname;
197 else if (global.params.preservePaths)
198 arghdr = (char *)arg;
199 else
200 arghdr = FileName::name((char *)arg);
201 if (!FileName::absolute(arghdr))
202 { //FileName::ensurePathExists(global.params.hdrdir);
203 arghdr = FileName::combine(global.params.hdrdir, arghdr);
205 if (global.params.hdrname)
206 hdrfilename = new FileName(arghdr, 0);
207 else
208 hdrfilename = FileName::forceExt(arghdr, global.hdr_ext);
210 if (hdrfilename->equals(srcfile->name))
211 { error("Source file and 'header' file have same name '%s'", srcfile->name->str);
212 fatal();
215 hdrfile = new File(hdrfilename);
218 void Module::deleteObjFile()
220 if (global.params.obj)
221 objfile->remove();
222 if (docfile)
223 docfile->remove();
226 Module::~Module()
230 char *Module::kind()
232 return "module";
235 Module *Module::load(Loc loc, Array *packages, Identifier *ident)
236 { Module *m;
237 char *filename;
238 bool dltSyntax = false;
240 //printf("Module::load(ident = '%s')\n", ident->toChars());
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)
249 OutBuffer buf;
250 int i;
252 for (i = 0; i < packages->dim; i++)
253 { Identifier *pid = (Identifier *)packages->data[i];
255 buf.writestring(pid->toChars());
256 #if _WIN32
257 buf.writeByte('\\');
258 #else
259 buf.writeByte('/');
260 #endif
262 buf.writestring(filename);
263 buf.writeByte(0);
264 filename = (char *)buf.extractData();
267 /* Search along global.path for a .dlt file, then a .di file, then a .d file.
269 char *result = NULL;
270 FileName *fdlt = FileName::forceExt(filename, global.dlt_ext);
271 FileName *fdi = FileName::forceExt(filename, global.hdr_ext);
272 FileName *fd = FileName::forceExt(filename, global.mars_ext);
273 char *sdlt = fdlt->toChars();
274 char *sdi = fdi->toChars();
275 char *sd = fd->toChars();
277 if (FileName::exists(sdlt))
279 result = sdlt;
280 dltSyntax = true;
282 else if (FileName::exists(sdi))
283 result = sdi;
284 else if (FileName::exists(sd))
285 result = sd;
286 else if (FileName::absolute(filename))
288 else if (!global.path)
290 else
292 for (size_t i = 0; i < global.path->dim; i++)
294 char *p = (char *)global.path->data[i];
295 char *n;
297 n = FileName::combine(p, sdlt);
298 if (FileName::exists(n))
299 { result = n;
300 dltSyntax = true;
301 break;
303 mem.free(n);
305 n = FileName::combine(p, sdi);
306 if (FileName::exists(n))
307 { result = n;
308 break;
310 mem.free(n);
312 n = FileName::combine(p, sd);
313 if (FileName::exists(n))
314 { result = n;
315 break;
317 mem.free(n);
321 m = new Module(filename, ident, 0, 0, dltSyntax);
322 m->loc = loc;
324 if (result)
325 m->srcfile = new File(result);
327 if (global.params.verbose)
329 printf("import ");
330 if (packages)
332 for (size_t i = 0; i < packages->dim; i++)
333 { Identifier *pid = (Identifier *)packages->data[i];
334 printf("%s.", pid->toChars());
337 printf("%s\t(%s)\n", ident->toChars(), m->srcfile->toChars());
340 m->read(loc);
341 m->parse();
343 #ifdef IN_GCC
344 d_gcc_magic_module(m);
345 #endif
347 return m;
350 void Module::read(Loc loc)
352 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars());
353 if (srcfile->read())
354 { error(loc, "cannot read file '%s'", srcfile->toChars());
355 fatal();
359 inline unsigned readwordLE(unsigned short *p)
361 #if __I86__
362 return *p;
363 #else
364 return (((unsigned char *)p)[1] << 8) | ((unsigned char *)p)[0];
365 #endif
368 inline unsigned readwordBE(unsigned short *p)
370 return (((unsigned char *)p)[0] << 8) | ((unsigned char *)p)[1];
373 inline unsigned readlongLE(unsigned *p)
375 #if __I86__
376 return *p;
377 #else
378 return ((unsigned char *)p)[0] |
379 (((unsigned char *)p)[1] << 8) |
380 (((unsigned char *)p)[2] << 16) |
381 (((unsigned char *)p)[3] << 24);
382 #endif
385 inline unsigned readlongBE(unsigned *p)
387 return ((unsigned char *)p)[3] |
388 (((unsigned char *)p)[2] << 8) |
389 (((unsigned char *)p)[1] << 16) |
390 (((unsigned char *)p)[0] << 24);
393 #if IN_GCC
394 void Module::parse(bool dump_source)
395 #else
396 void Module::parse()
397 #endif
398 { char *srcname;
399 unsigned char *buf;
400 unsigned buflen;
401 unsigned le;
402 unsigned bom;
404 //printf("Module::parse()\n");
406 srcname = srcfile->name->toChars();
407 //printf("Module::parse(srcname = '%s')\n", srcname);
409 buf = srcfile->buffer;
410 buflen = srcfile->len;
412 if (buflen >= 2)
414 /* Convert all non-UTF-8 formats to UTF-8.
415 * BOM : http://www.unicode.org/faq/utf_bom.html
416 * 00 00 FE FF UTF-32BE, big-endian
417 * FF FE 00 00 UTF-32LE, little-endian
418 * FE FF UTF-16BE, big-endian
419 * FF FE UTF-16LE, little-endian
420 * EF BB BF UTF-8
423 bom = 1; // assume there's a BOM
424 if (buf[0] == 0xFF && buf[1] == 0xFE)
426 if (buflen >= 4 && buf[2] == 0 && buf[3] == 0)
427 { // UTF-32LE
428 le = 1;
430 Lutf32:
431 OutBuffer dbuf;
432 unsigned *pu = (unsigned *)(buf);
433 unsigned *pumax = &pu[buflen / 4];
435 if (buflen & 3)
436 { error("odd length of UTF-32 char source %u", buflen);
437 fatal();
440 dbuf.reserve(buflen / 4);
441 for (pu += bom; pu < pumax; pu++)
442 { unsigned u;
444 u = le ? readlongLE(pu) : readlongBE(pu);
445 if (u & ~0x7F)
447 if (u > 0x10FFFF)
448 { error("UTF-32 value %08x greater than 0x10FFFF", u);
449 fatal();
451 dbuf.writeUTF8(u);
453 else
454 dbuf.writeByte(u);
456 dbuf.writeByte(0); // add 0 as sentinel for scanner
457 buflen = dbuf.offset - 1; // don't include sentinel in count
458 buf = (unsigned char *) dbuf.extractData();
460 else
461 { // UTF-16LE (X86)
462 // Convert it to UTF-8
463 le = 1;
465 Lutf16:
466 OutBuffer dbuf;
467 unsigned short *pu = (unsigned short *)(buf);
468 unsigned short *pumax = &pu[buflen / 2];
470 if (buflen & 1)
471 { error("odd length of UTF-16 char source %u", buflen);
472 fatal();
475 dbuf.reserve(buflen / 2);
476 for (pu += bom; pu < pumax; pu++)
477 { unsigned u;
479 u = le ? readwordLE(pu) : readwordBE(pu);
480 if (u & ~0x7F)
481 { if (u >= 0xD800 && u <= 0xDBFF)
482 { unsigned u2;
484 if (++pu > pumax)
485 { error("surrogate UTF-16 high value %04x at EOF", u);
486 fatal();
488 u2 = le ? readwordLE(pu) : readwordBE(pu);
489 if (u2 < 0xDC00 || u2 > 0xDFFF)
490 { error("surrogate UTF-16 low value %04x out of range", u2);
491 fatal();
493 u = (u - 0xD7C0) << 10;
494 u |= (u2 - 0xDC00);
496 else if (u >= 0xDC00 && u <= 0xDFFF)
497 { error("unpaired surrogate UTF-16 value %04x", u);
498 fatal();
500 else if (u == 0xFFFE || u == 0xFFFF)
501 { error("illegal UTF-16 value %04x", u);
502 fatal();
504 dbuf.writeUTF8(u);
506 else
507 dbuf.writeByte(u);
509 dbuf.writeByte(0); // add 0 as sentinel for scanner
510 buflen = dbuf.offset - 1; // don't include sentinel in count
511 buf = (unsigned char *) dbuf.extractData();
514 else if (buf[0] == 0xFE && buf[1] == 0xFF)
515 { // UTF-16BE
516 le = 0;
517 goto Lutf16;
519 else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF)
520 { // UTF-32BE
521 le = 0;
522 goto Lutf32;
524 else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF)
525 { // UTF-8
527 buf += 3;
528 buflen -= 3;
530 else
532 /* There is no BOM. Make use of Arcane Jill's insight that
533 * the first char of D source must be ASCII to
534 * figure out the encoding.
537 bom = 0;
538 if (buflen >= 4)
539 { if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0)
540 { // UTF-32LE
541 le = 1;
542 goto Lutf32;
544 else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0)
545 { // UTF-32BE
546 le = 0;
547 goto Lutf32;
550 if (buflen >= 2)
552 if (buf[1] == 0)
553 { // UTF-16LE
554 le = 1;
555 goto Lutf16;
557 else if (buf[0] == 0)
558 { // UTF-16BE
559 le = 0;
560 goto Lutf16;
564 // It's UTF-8
565 if (buf[0] >= 0x80)
566 { error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]);
567 fatal();
572 #ifdef IN_GCC
573 // dump utf-8 encoded source
574 if (dump_source)
575 { // %% srcname could contain a path ...
576 d_gcc_dump_source(srcname, "utf-8", buf, buflen);
578 #endif
580 /* If it starts with the string "Ddoc", then it's a documentation
581 * source file.
583 if (buflen >= 4 && memcmp(buf, "Ddoc", 4) == 0)
585 comment = buf + 4;
586 isDocFile = 1;
587 if (!docfile)
588 setDocfile();
589 return;
591 if (isHtml)
593 OutBuffer *dbuf = new OutBuffer();
594 Html h(srcname, buf, buflen);
595 h.extractCode(dbuf);
596 buf = dbuf->data;
597 buflen = dbuf->offset;
598 #ifdef IN_GCC
599 // dump extracted source
600 if (dump_source)
601 d_gcc_dump_source(srcname, "d.utf-8", buf, buflen);
602 #endif
605 if (isDltFile)
607 DltParser p(this, buf, buflen, docfile != NULL);
608 p.nextToken();
609 members = p.parseModule();
610 md = p.md;
611 numlines = p.loc.linnum;
613 else
615 Parser p(this, buf, buflen, docfile != NULL);
616 p.nextToken();
617 members = p.parseModule();
618 md = p.md;
619 numlines = p.loc.linnum;
622 DsymbolTable *dst;
624 if (md)
625 { this->ident = md->id;
626 dst = Package::resolve(md->packages, &this->parent, NULL);
628 else
630 dst = modules;
632 /* Check to see if module name is a valid identifier
634 if (!Lexer::isValidIdentifier(this->ident->toChars()))
635 error("has non-identifier characters in filename, use module declaration instead");
638 // Update global list of modules
639 if (!dst->insert(this))
641 if (md)
642 error(loc, "is in multiple packages %s", md->toChars());
643 else
644 error(loc, "is in multiple defined");
646 else
648 amodules.push(this);
652 static FuncDeclaration *addMainFunction(Dsymbol *mainClass) {
653 ClassDeclaration *klass = mainClass->isClassDeclaration();
654 if (klass == NULL) {
655 // Just ignore it then?
656 error("'Main' is not a class!");
657 return NULL;
660 Loc loc = mainClass->loc;
662 // Create a main(string[] args) function
663 Arguments *arguments = new Arguments();
665 Type *tstr = new TypeDArray(Type::tchar);
666 Type *targv = new TypeDArray(tstr);
668 Argument *a = new Argument(STCin, targv, Id::args, NULL);
669 arguments->push(a);
671 TypeFunction *ta = new TypeFunction(arguments, new Type(Tvoid, NULL), 0, LINKc);
672 FuncDeclaration *f = new FuncDeclaration(loc, 0, Id::main, STCundefined, ta);
674 f->fbody = new InjectorMainBody(loc, klass);
676 return f;
679 void Module::semantic()
680 { int i;
682 if (semanticstarted)
683 return;
685 Array *dlt_package = new Array();
686 dlt_package->push(Id::dlt);
688 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
689 semanticstarted = 1;
691 // Note that modules get their own scope, from scratch.
692 // This is so regardless of where in the syntax a module
693 // gets imported, it is unaffected by context.
694 Scope *sc = Scope::createGlobal(this); // create root scope
696 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);
698 // Find Main class
699 for (i = 0; i < members->dim; i++)
700 { Dsymbol *s;
702 s = (Dsymbol *)members->data[i];
704 if (s->ident && s->ident == Id::Main) {
705 Dsymbol *mainFn = addMainFunction(s);
706 if (!mainFn)
707 return;
708 members->push(mainFn);
710 Import *im = new Import(0, dlt_package, Id::_externals, NULL, true);
711 members->shift(im);
713 break;
717 // Add import of "object" if this module isn't "object"
718 if (ident != Id::object || parent)
720 Import *im = new Import(0, NULL, Id::object, NULL, 0);
721 members->shift(im);
723 /* If this is not dlt.core ... */
724 if (ident != Id::core || parent == NULL || parent->parent || parent->ident != Id::dlt) {
725 Import *im = new Import(0, dlt_package, Id::core, NULL, 0);
726 members->shift(im);
729 #ifdef IN_GCC
730 else
732 /* va_list is a pain. If va_list involves a struct, add the
733 struct declaration to the "object" module. This depends on
734 the GCC backend not naming the struct something that will
735 cause a conflict or define "va_list" without going through
736 std.stdarg. */
737 Type * t = d_gcc_builtin_va_list_d_type;
738 while (t) {
739 if (t->ty == Tstruct) {
740 StructDeclaration * sd = ((TypeStruct *) t)->sym;
741 sd->parent = this;
742 members->push(sd);
743 break;
745 t = t->next;
748 #endif
750 // Add all symbols into module's symbol table
751 symtab = new DsymbolTable();
752 for (i = 0; i < members->dim; i++)
753 { Dsymbol *s;
755 s = (Dsymbol *)members->data[i];
756 s->addMember(NULL, sc->scopesym, 1);
759 // Pass 1 semantic routines: do public side of the definition
760 for (i = 0; i < members->dim; i++)
761 { Dsymbol *s;
763 s = (Dsymbol *)members->data[i];
764 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
765 s->semantic(sc);
766 runDeferredSemantic();
769 sc = sc->pop();
770 sc->pop();
771 semanticdone = semanticstarted;
772 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
775 void Module::semantic2()
776 { int i;
778 if (deferred.dim)
780 for (int i = 0; i < deferred.dim; i++)
782 Dsymbol *sd = (Dsymbol *)deferred.data[i];
784 sd->error("unable to resolve forward reference in definition");
786 return;
788 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
789 if (semanticstarted >= 2)
790 return;
791 assert(semanticstarted == 1);
792 semanticstarted = 2;
794 // Note that modules get their own scope, from scratch.
795 // This is so regardless of where in the syntax a module
796 // gets imported, it is unaffected by context.
797 Scope *sc = Scope::createGlobal(this); // create root scope
798 //printf("Module = %p\n", sc.scopesym);
800 // Pass 2 semantic routines: do initializers and function bodies
801 for (i = 0; i < members->dim; i++)
802 { Dsymbol *s;
804 s = (Dsymbol *)members->data[i];
805 s->semantic2(sc);
808 sc = sc->pop();
809 sc->pop();
810 semanticdone = semanticstarted;
811 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
814 void Module::semantic3()
815 { int i;
817 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
818 if (semanticstarted >= 3)
819 return;
820 assert(semanticstarted == 2);
821 semanticstarted = 3;
823 // Note that modules get their own scope, from scratch.
824 // This is so regardless of where in the syntax a module
825 // gets imported, it is unaffected by context.
826 Scope *sc = Scope::createGlobal(this); // create root scope
827 //printf("Module = %p\n", sc.scopesym);
829 // Pass 3 semantic routines: do initializers and function bodies
830 for (i = 0; i < members->dim; i++)
831 { Dsymbol *s;
833 s = (Dsymbol *)members->data[i];
834 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
835 s->semantic3(sc);
838 sc = sc->pop();
839 sc->pop();
840 semanticdone = semanticstarted;
843 void Module::inlineScan()
844 { int i;
846 if (semanticstarted >= 4)
847 return;
848 assert(semanticstarted == 3);
849 semanticstarted = 4;
851 // Note that modules get their own scope, from scratch.
852 // This is so regardless of where in the syntax a module
853 // gets imported, it is unaffected by context.
854 //printf("Module = %p\n", sc.scopesym);
856 for (i = 0; i < members->dim; i++)
857 { Dsymbol *s;
859 s = (Dsymbol *)members->data[i];
860 //if (global.params.verbose)
861 //printf("inline scan symbol %s\n", s->toChars());
863 s->inlineScan();
865 semanticdone = semanticstarted;
868 /****************************************************
871 void Module::gensymfile()
873 OutBuffer buf;
874 HdrGenState hgs;
876 //printf("Module::gensymfile()\n");
878 buf.printf("// Sym file generated from '%s'", srcfile->toChars());
879 buf.writenl();
881 for (int i = 0; i < members->dim; i++)
882 { Dsymbol *s = (Dsymbol *)members->data[i];
884 s->toCBuffer(&buf, &hgs);
887 // Transfer image to file
888 symfile->setbuffer(buf.data, buf.offset);
889 buf.data = NULL;
891 symfile->writev();
894 /**********************************
895 * Determine if we need to generate an instance of ModuleInfo
896 * for this Module.
899 int Module::needModuleInfo()
901 return needmoduleinfo || global.params.cov;
904 Dsymbol *Module::search(Loc loc, Identifier *ident, int flags)
906 /* Since modules can be circularly referenced,
907 * need to stop infinite recursive searches.
910 //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch);
911 Dsymbol *s;
912 if (insearch)
913 s = NULL;
914 else if (searchCacheIdent == ident && searchCacheFlags == flags)
915 s = searchCacheSymbol;
916 else
918 insearch = 1;
919 s = ScopeDsymbol::search(loc, ident, flags);
920 insearch = 0;
922 searchCacheIdent = ident;
923 searchCacheSymbol = s;
924 searchCacheFlags = flags;
926 return s;
929 /*******************************************
930 * Can't run semantic on s now, try again later.
933 void Module::addDeferredSemantic(Dsymbol *s)
935 // Don't add it if it is already there
936 for (int i = 0; i < deferred.dim; i++)
938 Dsymbol *sd = (Dsymbol *)deferred.data[i];
940 if (sd == s)
941 return;
944 //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
945 deferred.push(s);
949 /******************************************
950 * Run semantic() on deferred symbols.
953 void Module::runDeferredSemantic()
955 size_t len;
957 static int nested;
958 if (nested)
959 return;
960 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
961 nested++;
965 dprogress = 0;
966 len = deferred.dim;
967 if (!len)
968 break;
970 Dsymbol **todo;
971 Dsymbol *tmp;
972 if (len == 1)
974 todo = &tmp;
976 else
978 todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *));
979 assert(todo);
981 memcpy(todo, deferred.data, len * sizeof(Dsymbol *));
982 deferred.setDim(0);
984 for (int i = 0; i < len; i++)
986 Dsymbol *s = todo[i];
988 s->semantic(NULL);
989 //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars());
991 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
992 } while (deferred.dim < len || dprogress); // while making progress
993 nested--;
994 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
997 /* =========================== ModuleDeclaration ===================== */
999 ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id)
1001 this->packages = packages;
1002 this->id = id;
1005 char *ModuleDeclaration::toChars()
1007 OutBuffer buf;
1008 int i;
1010 if (packages && packages->dim)
1012 for (i = 0; i < packages->dim; i++)
1013 { Identifier *pid = (Identifier *)packages->data[i];
1015 buf.writestring(pid->toChars());
1016 buf.writeByte('.');
1019 buf.writestring(id->toChars());
1020 buf.writeByte(0);
1021 return (char *)buf.extractData();
1024 /* =========================== Package ===================== */
1026 Package::Package(Identifier *ident)
1027 : ScopeDsymbol(ident)
1032 char *Package::kind()
1034 return "package";
1038 DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg)
1040 DsymbolTable *dst = Module::modules;
1041 Dsymbol *parent = NULL;
1043 //printf("Package::resolve()\n");
1044 if (ppkg)
1045 *ppkg = NULL;
1047 if (packages)
1048 { int i;
1050 for (i = 0; i < packages->dim; i++)
1051 { Identifier *pid = (Identifier *)packages->data[i];
1052 Dsymbol *p;
1054 p = dst->lookup(pid);
1055 if (!p)
1057 p = new Package(pid);
1058 dst->insert(p);
1059 p->parent = parent;
1060 ((ScopeDsymbol *)p)->symtab = new DsymbolTable();
1062 else
1064 assert(p->isPackage());
1065 if (p->isModule())
1066 { p->error("module and package have the same name");
1067 fatal();
1068 break;
1071 parent = p;
1072 dst = ((Package *)p)->symtab;
1073 if (ppkg && !*ppkg)
1074 *ppkg = (Package *)p;
1076 if (pparent)
1078 *pparent = parent;
1081 return dst;