Fixed some lexing problems with DOS line-endings
[delight/core.git] / dmd / module.c
blob012e63d09422fc3e32ffc7b0f08943b2f3ae8474
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 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
686 semanticstarted = 1;
688 // Note that modules get their own scope, from scratch.
689 // This is so regardless of where in the syntax a module
690 // gets imported, it is unaffected by context.
691 Scope *sc = Scope::createGlobal(this); // create root scope
693 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);
695 // Find Main class
696 for (i = 0; i < members->dim; i++)
697 { Dsymbol *s;
699 s = (Dsymbol *)members->data[i];
701 if (s->ident && s->ident == Id::Main) {
702 Dsymbol *mainFn = addMainFunction(s);
703 if (!mainFn)
704 return;
705 members->push(mainFn);
707 Import *im = new Import(0, NULL, Id::_externals, NULL, true);
708 members->shift(im);
710 break;
714 // Add import of "object" if this module isn't "object"
715 if (ident != Id::object || parent)
717 Import *im = new Import(0, NULL, Id::object, NULL, 0);
718 members->shift(im);
720 /* If this is not dlt.core ... */
721 if (ident != Id::core || parent == NULL || parent->parent || parent->ident != Id::dlt) {
722 Array *dlt_package = new Array();
723 dlt_package->push(Id::dlt);
724 Import *im = new Import(0, dlt_package, Id::core, NULL, 0);
725 members->shift(im);
728 #ifdef IN_GCC
729 else
731 /* va_list is a pain. If va_list involves a struct, add the
732 struct declaration to the "object" module. This depends on
733 the GCC backend not naming the struct something that will
734 cause a conflict or define "va_list" without going through
735 std.stdarg. */
736 Type * t = d_gcc_builtin_va_list_d_type;
737 while (t) {
738 if (t->ty == Tstruct) {
739 StructDeclaration * sd = ((TypeStruct *) t)->sym;
740 sd->parent = this;
741 members->push(sd);
742 break;
744 t = t->next;
747 #endif
749 // Add all symbols into module's symbol table
750 symtab = new DsymbolTable();
751 for (i = 0; i < members->dim; i++)
752 { Dsymbol *s;
754 s = (Dsymbol *)members->data[i];
755 s->addMember(NULL, sc->scopesym, 1);
758 // Pass 1 semantic routines: do public side of the definition
759 for (i = 0; i < members->dim; i++)
760 { Dsymbol *s;
762 s = (Dsymbol *)members->data[i];
763 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
764 s->semantic(sc);
765 runDeferredSemantic();
768 sc = sc->pop();
769 sc->pop();
770 semanticdone = semanticstarted;
771 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
774 void Module::semantic2()
775 { int i;
777 if (deferred.dim)
779 for (int i = 0; i < deferred.dim; i++)
781 Dsymbol *sd = (Dsymbol *)deferred.data[i];
783 sd->error("unable to resolve forward reference in definition");
785 return;
787 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
788 if (semanticstarted >= 2)
789 return;
790 assert(semanticstarted == 1);
791 semanticstarted = 2;
793 // Note that modules get their own scope, from scratch.
794 // This is so regardless of where in the syntax a module
795 // gets imported, it is unaffected by context.
796 Scope *sc = Scope::createGlobal(this); // create root scope
797 //printf("Module = %p\n", sc.scopesym);
799 // Pass 2 semantic routines: do initializers and function bodies
800 for (i = 0; i < members->dim; i++)
801 { Dsymbol *s;
803 s = (Dsymbol *)members->data[i];
804 s->semantic2(sc);
807 sc = sc->pop();
808 sc->pop();
809 semanticdone = semanticstarted;
810 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
813 void Module::semantic3()
814 { int i;
816 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
817 if (semanticstarted >= 3)
818 return;
819 assert(semanticstarted == 2);
820 semanticstarted = 3;
822 // Note that modules get their own scope, from scratch.
823 // This is so regardless of where in the syntax a module
824 // gets imported, it is unaffected by context.
825 Scope *sc = Scope::createGlobal(this); // create root scope
826 //printf("Module = %p\n", sc.scopesym);
828 // Pass 3 semantic routines: do initializers and function bodies
829 for (i = 0; i < members->dim; i++)
830 { Dsymbol *s;
832 s = (Dsymbol *)members->data[i];
833 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
834 s->semantic3(sc);
837 sc = sc->pop();
838 sc->pop();
839 semanticdone = semanticstarted;
842 void Module::inlineScan()
843 { int i;
845 if (semanticstarted >= 4)
846 return;
847 assert(semanticstarted == 3);
848 semanticstarted = 4;
850 // Note that modules get their own scope, from scratch.
851 // This is so regardless of where in the syntax a module
852 // gets imported, it is unaffected by context.
853 //printf("Module = %p\n", sc.scopesym);
855 for (i = 0; i < members->dim; i++)
856 { Dsymbol *s;
858 s = (Dsymbol *)members->data[i];
859 //if (global.params.verbose)
860 //printf("inline scan symbol %s\n", s->toChars());
862 s->inlineScan();
864 semanticdone = semanticstarted;
867 /****************************************************
870 void Module::gensymfile()
872 OutBuffer buf;
873 HdrGenState hgs;
875 //printf("Module::gensymfile()\n");
877 buf.printf("// Sym file generated from '%s'", srcfile->toChars());
878 buf.writenl();
880 for (int i = 0; i < members->dim; i++)
881 { Dsymbol *s = (Dsymbol *)members->data[i];
883 s->toCBuffer(&buf, &hgs);
886 // Transfer image to file
887 symfile->setbuffer(buf.data, buf.offset);
888 buf.data = NULL;
890 symfile->writev();
893 /**********************************
894 * Determine if we need to generate an instance of ModuleInfo
895 * for this Module.
898 int Module::needModuleInfo()
900 return needmoduleinfo || global.params.cov;
903 Dsymbol *Module::search(Loc loc, Identifier *ident, int flags)
905 /* Since modules can be circularly referenced,
906 * need to stop infinite recursive searches.
909 //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch);
910 Dsymbol *s;
911 if (insearch)
912 s = NULL;
913 else if (searchCacheIdent == ident && searchCacheFlags == flags)
914 s = searchCacheSymbol;
915 else
917 insearch = 1;
918 s = ScopeDsymbol::search(loc, ident, flags);
919 insearch = 0;
921 searchCacheIdent = ident;
922 searchCacheSymbol = s;
923 searchCacheFlags = flags;
925 return s;
928 /*******************************************
929 * Can't run semantic on s now, try again later.
932 void Module::addDeferredSemantic(Dsymbol *s)
934 // Don't add it if it is already there
935 for (int i = 0; i < deferred.dim; i++)
937 Dsymbol *sd = (Dsymbol *)deferred.data[i];
939 if (sd == s)
940 return;
943 //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
944 deferred.push(s);
948 /******************************************
949 * Run semantic() on deferred symbols.
952 void Module::runDeferredSemantic()
954 size_t len;
956 static int nested;
957 if (nested)
958 return;
959 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
960 nested++;
964 dprogress = 0;
965 len = deferred.dim;
966 if (!len)
967 break;
969 Dsymbol **todo;
970 Dsymbol *tmp;
971 if (len == 1)
973 todo = &tmp;
975 else
977 todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *));
978 assert(todo);
980 memcpy(todo, deferred.data, len * sizeof(Dsymbol *));
981 deferred.setDim(0);
983 for (int i = 0; i < len; i++)
985 Dsymbol *s = todo[i];
987 s->semantic(NULL);
988 //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars());
990 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
991 } while (deferred.dim < len || dprogress); // while making progress
992 nested--;
993 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
996 /* =========================== ModuleDeclaration ===================== */
998 ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id)
1000 this->packages = packages;
1001 this->id = id;
1004 char *ModuleDeclaration::toChars()
1006 OutBuffer buf;
1007 int i;
1009 if (packages && packages->dim)
1011 for (i = 0; i < packages->dim; i++)
1012 { Identifier *pid = (Identifier *)packages->data[i];
1014 buf.writestring(pid->toChars());
1015 buf.writeByte('.');
1018 buf.writestring(id->toChars());
1019 buf.writeByte(0);
1020 return (char *)buf.extractData();
1023 /* =========================== Package ===================== */
1025 Package::Package(Identifier *ident)
1026 : ScopeDsymbol(ident)
1031 char *Package::kind()
1033 return "package";
1037 DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg)
1039 DsymbolTable *dst = Module::modules;
1040 Dsymbol *parent = NULL;
1042 //printf("Package::resolve()\n");
1043 if (ppkg)
1044 *ppkg = NULL;
1046 if (packages)
1047 { int i;
1049 for (i = 0; i < packages->dim; i++)
1050 { Identifier *pid = (Identifier *)packages->data[i];
1051 Dsymbol *p;
1053 p = dst->lookup(pid);
1054 if (!p)
1056 p = new Package(pid);
1057 dst->insert(p);
1058 p->parent = parent;
1059 ((ScopeDsymbol *)p)->symtab = new DsymbolTable();
1061 else
1063 assert(p->isPackage());
1064 if (p->isModule())
1065 { p->error("module and package have the same name");
1066 fatal();
1067 break;
1070 parent = p;
1071 dst = ((Package *)p)->symtab;
1072 if (ppkg && !*ppkg)
1073 *ppkg = (Package *)p;
1075 if (pparent)
1077 *pparent = parent;
1080 return dst;