Allow log_ statements outside of a class
[delight/core.git] / dmd / module.c
blobf60af9278dcc99d5971115d28990b0dbe95a499f
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!");
659 Loc loc = mainClass->loc;
661 // Create a main(string[] args) function
662 Arguments *arguments = new Arguments();
664 Type *tstr = new TypeDArray(Type::tchar);
665 Type *targv = new TypeDArray(tstr);
667 Argument *a = new Argument(STCin, targv, Id::args, NULL);
668 arguments->push(a);
670 TypeFunction *ta = new TypeFunction(arguments, new Type(Tvoid, NULL), 0, LINKc);
671 FuncDeclaration *f = new FuncDeclaration(loc, 0, Id::main, STCundefined, ta);
673 f->fbody = new InjectorMainBody(loc, klass);
675 return f;
678 void Module::semantic()
679 { int i;
681 if (semanticstarted)
682 return;
684 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
685 semanticstarted = 1;
687 // Note that modules get their own scope, from scratch.
688 // This is so regardless of where in the syntax a module
689 // gets imported, it is unaffected by context.
690 Scope *sc = Scope::createGlobal(this); // create root scope
692 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);
694 // Add import of "object" if this module isn't "object"
695 if (ident != Id::object)
697 Import *im = new Import(0, NULL, Id::object, NULL, 0);
698 members->shift(im);
699 // For logging
700 if (ident != Id::_externals) {
701 Import *im = new Import(0, NULL, Id::_externals, NULL, true);
702 members->shift(im);
705 #ifdef IN_GCC
706 else
708 /* va_list is a pain. If va_list involves a struct, add the
709 struct declaration to the "object" module. This depends on
710 the GCC backend not naming the struct something that will
711 cause a conflict or define "va_list" without going through
712 std.stdarg. */
713 Type * t = d_gcc_builtin_va_list_d_type;
714 while (t) {
715 if (t->ty == Tstruct) {
716 StructDeclaration * sd = ((TypeStruct *) t)->sym;
717 sd->parent = this;
718 members->push(sd);
719 break;
721 t = t->next;
724 #endif
726 // Find Main class
727 for (i = 0; i < members->dim; i++)
728 { Dsymbol *s;
730 s = (Dsymbol *)members->data[i];
732 if (s->ident && s->ident == Id::Main) {
733 Dsymbol *mainFn = addMainFunction(s);
734 if (!mainFn)
735 return;
736 members->push(mainFn);
738 break;
742 // Add all symbols into module's symbol table
743 symtab = new DsymbolTable();
744 for (i = 0; i < members->dim; i++)
745 { Dsymbol *s;
747 s = (Dsymbol *)members->data[i];
748 s->addMember(NULL, sc->scopesym, 1);
751 // Pass 1 semantic routines: do public side of the definition
752 for (i = 0; i < members->dim; i++)
753 { Dsymbol *s;
755 s = (Dsymbol *)members->data[i];
756 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
757 s->semantic(sc);
758 runDeferredSemantic();
761 sc = sc->pop();
762 sc->pop();
763 semanticdone = semanticstarted;
764 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
767 void Module::semantic2()
768 { int i;
770 if (deferred.dim)
772 for (int i = 0; i < deferred.dim; i++)
774 Dsymbol *sd = (Dsymbol *)deferred.data[i];
776 sd->error("unable to resolve forward reference in definition");
778 return;
780 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
781 if (semanticstarted >= 2)
782 return;
783 assert(semanticstarted == 1);
784 semanticstarted = 2;
786 // Note that modules get their own scope, from scratch.
787 // This is so regardless of where in the syntax a module
788 // gets imported, it is unaffected by context.
789 Scope *sc = Scope::createGlobal(this); // create root scope
790 //printf("Module = %p\n", sc.scopesym);
792 // Pass 2 semantic routines: do initializers and function bodies
793 for (i = 0; i < members->dim; i++)
794 { Dsymbol *s;
796 s = (Dsymbol *)members->data[i];
797 s->semantic2(sc);
800 sc = sc->pop();
801 sc->pop();
802 semanticdone = semanticstarted;
803 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
806 void Module::semantic3()
807 { int i;
809 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
810 if (semanticstarted >= 3)
811 return;
812 assert(semanticstarted == 2);
813 semanticstarted = 3;
815 // Note that modules get their own scope, from scratch.
816 // This is so regardless of where in the syntax a module
817 // gets imported, it is unaffected by context.
818 Scope *sc = Scope::createGlobal(this); // create root scope
819 //printf("Module = %p\n", sc.scopesym);
821 // Pass 3 semantic routines: do initializers and function bodies
822 for (i = 0; i < members->dim; i++)
823 { Dsymbol *s;
825 s = (Dsymbol *)members->data[i];
826 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
827 s->semantic3(sc);
830 sc = sc->pop();
831 sc->pop();
832 semanticdone = semanticstarted;
835 void Module::inlineScan()
836 { int i;
838 if (semanticstarted >= 4)
839 return;
840 assert(semanticstarted == 3);
841 semanticstarted = 4;
843 // Note that modules get their own scope, from scratch.
844 // This is so regardless of where in the syntax a module
845 // gets imported, it is unaffected by context.
846 //printf("Module = %p\n", sc.scopesym);
848 for (i = 0; i < members->dim; i++)
849 { Dsymbol *s;
851 s = (Dsymbol *)members->data[i];
852 //if (global.params.verbose)
853 //printf("inline scan symbol %s\n", s->toChars());
855 s->inlineScan();
857 semanticdone = semanticstarted;
860 /****************************************************
863 void Module::gensymfile()
865 OutBuffer buf;
866 HdrGenState hgs;
868 //printf("Module::gensymfile()\n");
870 buf.printf("// Sym file generated from '%s'", srcfile->toChars());
871 buf.writenl();
873 for (int i = 0; i < members->dim; i++)
874 { Dsymbol *s = (Dsymbol *)members->data[i];
876 s->toCBuffer(&buf, &hgs);
879 // Transfer image to file
880 symfile->setbuffer(buf.data, buf.offset);
881 buf.data = NULL;
883 symfile->writev();
886 /**********************************
887 * Determine if we need to generate an instance of ModuleInfo
888 * for this Module.
891 int Module::needModuleInfo()
893 return needmoduleinfo || global.params.cov;
896 Dsymbol *Module::search(Loc loc, Identifier *ident, int flags)
898 /* Since modules can be circularly referenced,
899 * need to stop infinite recursive searches.
902 //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch);
903 Dsymbol *s;
904 if (insearch)
905 s = NULL;
906 else if (searchCacheIdent == ident && searchCacheFlags == flags)
907 s = searchCacheSymbol;
908 else
910 insearch = 1;
911 s = ScopeDsymbol::search(loc, ident, flags);
912 insearch = 0;
914 searchCacheIdent = ident;
915 searchCacheSymbol = s;
916 searchCacheFlags = flags;
918 return s;
921 /*******************************************
922 * Can't run semantic on s now, try again later.
925 void Module::addDeferredSemantic(Dsymbol *s)
927 // Don't add it if it is already there
928 for (int i = 0; i < deferred.dim; i++)
930 Dsymbol *sd = (Dsymbol *)deferred.data[i];
932 if (sd == s)
933 return;
936 //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
937 deferred.push(s);
941 /******************************************
942 * Run semantic() on deferred symbols.
945 void Module::runDeferredSemantic()
947 size_t len;
949 static int nested;
950 if (nested)
951 return;
952 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
953 nested++;
957 dprogress = 0;
958 len = deferred.dim;
959 if (!len)
960 break;
962 Dsymbol **todo;
963 Dsymbol *tmp;
964 if (len == 1)
966 todo = &tmp;
968 else
970 todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *));
971 assert(todo);
973 memcpy(todo, deferred.data, len * sizeof(Dsymbol *));
974 deferred.setDim(0);
976 for (int i = 0; i < len; i++)
978 Dsymbol *s = todo[i];
980 s->semantic(NULL);
981 //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars());
983 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
984 } while (deferred.dim < len || dprogress); // while making progress
985 nested--;
986 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
989 /* =========================== ModuleDeclaration ===================== */
991 ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id)
993 this->packages = packages;
994 this->id = id;
997 char *ModuleDeclaration::toChars()
999 OutBuffer buf;
1000 int i;
1002 if (packages && packages->dim)
1004 for (i = 0; i < packages->dim; i++)
1005 { Identifier *pid = (Identifier *)packages->data[i];
1007 buf.writestring(pid->toChars());
1008 buf.writeByte('.');
1011 buf.writestring(id->toChars());
1012 buf.writeByte(0);
1013 return (char *)buf.extractData();
1016 /* =========================== Package ===================== */
1018 Package::Package(Identifier *ident)
1019 : ScopeDsymbol(ident)
1024 char *Package::kind()
1026 return "package";
1030 DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg)
1032 DsymbolTable *dst = Module::modules;
1033 Dsymbol *parent = NULL;
1035 //printf("Package::resolve()\n");
1036 if (ppkg)
1037 *ppkg = NULL;
1039 if (packages)
1040 { int i;
1042 for (i = 0; i < packages->dim; i++)
1043 { Identifier *pid = (Identifier *)packages->data[i];
1044 Dsymbol *p;
1046 p = dst->lookup(pid);
1047 if (!p)
1049 p = new Package(pid);
1050 dst->insert(p);
1051 p->parent = parent;
1052 ((ScopeDsymbol *)p)->symtab = new DsymbolTable();
1054 else
1056 assert(p->isPackage());
1057 if (p->isModule())
1058 { p->error("module and package have the same name");
1059 fatal();
1060 break;
1063 parent = p;
1064 dst = ((Package *)p)->symtab;
1065 if (ppkg && !*ppkg)
1066 *ppkg = (Package *)p;
1068 if (pparent)
1070 *pparent = parent;
1073 return dst;