Detect when the input ends in .dlt and use DltParser
[delight/core.git] / dmd / module.c
blobec9b324739f0627a180957dda94d7413abc225e7
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"
36 #define MARS 1
37 #include "html.h"
39 #ifdef IN_GCC
40 #include "d-dmd-gcc.h"
41 #endif
43 ClassDeclaration *Module::moduleinfo;
45 Module *Module::rootModule;
46 DsymbolTable *Module::modules;
47 Array Module::amodules;
49 Array Module::deferred; // deferred Dsymbol's needing semantic() run on them
50 unsigned Module::dprogress;
52 void Module::init()
54 modules = new DsymbolTable();
57 Module::Module(char *filename, Identifier *ident, int doDocComment, int doHdrGen, int isDltFile)
58 : Package(ident), isDltFile(isDltFile)
60 FileName *srcfilename;
61 FileName *cfilename;
62 FileName *hfilename;
63 FileName *objfilename;
64 FileName *symfilename;
66 // printf("Module::Module(filename = '%s', ident = '%s')\n", filename, ident->toChars());
67 this->arg = filename;
68 md = NULL;
69 errors = 0;
70 numlines = 0;
71 members = NULL;
72 isHtml = 0;
73 isDocFile = 0;
74 needmoduleinfo = 0;
75 #ifdef IN_GCC
76 strictlyneedmoduleinfo = 0;
77 #endif
78 insearch = 0;
79 searchCacheIdent = NULL;
80 searchCacheSymbol = NULL;
81 searchCacheFlags = 0;
82 semanticstarted = 0;
83 semanticdone = 0;
84 decldefs = NULL;
85 vmoduleinfo = NULL;
86 massert = NULL;
87 marray = NULL;
88 sictor = NULL;
89 sctor = NULL;
90 sdtor = NULL;
91 stest = NULL;
92 sfilename = NULL;
93 root = 0;
94 importedFrom = NULL;
95 srcfile = NULL;
96 docfile = NULL;
98 debuglevel = 0;
99 debugids = NULL;
100 debugidsNot = NULL;
101 versionlevel = 0;
102 versionids = NULL;
103 versionidsNot = NULL;
105 macrotable = NULL;
106 escapetable = NULL;
107 doppelganger = 0;
108 cov = NULL;
109 covb = NULL;
111 srcfilename = FileName::defaultExt(filename, global.dlt_ext);
112 if (!srcfilename->equalsExt(global.mars_ext) &&
113 !srcfilename->equalsExt(global.dlt_ext) &&
114 !srcfilename->equalsExt("dd"))
116 if (srcfilename->equalsExt("html") ||
117 srcfilename->equalsExt("htm") ||
118 srcfilename->equalsExt("xhtml"))
119 isHtml = 1;
120 else
121 { error("source file name '%s' must have .%s extension", srcfilename->toChars(), global.dlt_ext);
122 fatal();
126 char *argobj;
127 if (global.params.objname)
128 argobj = global.params.objname;
129 else if (global.params.preservePaths)
130 argobj = filename;
131 else
132 argobj = FileName::name(filename);
133 if (!FileName::absolute(argobj))
135 argobj = FileName::combine(global.params.objdir, argobj);
138 if (global.params.objname)
139 objfilename = new FileName(argobj, 0);
140 else
141 objfilename = FileName::forceExt(argobj, global.obj_ext);
143 symfilename = FileName::forceExt(filename, global.sym_ext);
145 srcfile = new File(srcfilename);
147 if (doDocComment)
149 setDocfile();
152 if (doHdrGen)
154 setHdrfile();
157 objfile = new File(objfilename);
158 symfile = new File(symfilename);
161 void Module::setDocfile()
163 FileName *docfilename;
164 char *argdoc;
166 if (global.params.docname)
167 argdoc = global.params.docname;
168 else if (global.params.preservePaths)
169 argdoc = (char *)arg;
170 else
171 argdoc = FileName::name((char *)arg);
172 if (!FileName::absolute(argdoc))
173 { //FileName::ensurePathExists(global.params.docdir);
174 argdoc = FileName::combine(global.params.docdir, argdoc);
176 if (global.params.docname)
177 docfilename = new FileName(argdoc, 0);
178 else
179 docfilename = FileName::forceExt(argdoc, global.doc_ext);
181 if (docfilename->equals(srcfile->name))
182 { error("Source file and documentation file have same name '%s'", srcfile->name->str);
183 fatal();
186 docfile = new File(docfilename);
189 void Module::setHdrfile()
191 FileName *hdrfilename;
192 char *arghdr;
194 if (global.params.hdrname)
195 arghdr = global.params.hdrname;
196 else if (global.params.preservePaths)
197 arghdr = (char *)arg;
198 else
199 arghdr = FileName::name((char *)arg);
200 if (!FileName::absolute(arghdr))
201 { //FileName::ensurePathExists(global.params.hdrdir);
202 arghdr = FileName::combine(global.params.hdrdir, arghdr);
204 if (global.params.hdrname)
205 hdrfilename = new FileName(arghdr, 0);
206 else
207 hdrfilename = FileName::forceExt(arghdr, global.hdr_ext);
209 if (hdrfilename->equals(srcfile->name))
210 { error("Source file and 'header' file have same name '%s'", srcfile->name->str);
211 fatal();
214 hdrfile = new File(hdrfilename);
217 void Module::deleteObjFile()
219 if (global.params.obj)
220 objfile->remove();
221 if (docfile)
222 docfile->remove();
225 Module::~Module()
229 char *Module::kind()
231 return "module";
234 Module *Module::load(Loc loc, Array *packages, Identifier *ident)
235 { Module *m;
236 char *filename;
237 bool dltSyntax = false;
239 //printf("Module::load(ident = '%s')\n", ident->toChars());
241 // Build module filename by turning:
242 // foo.bar.baz
243 // into:
244 // foo\bar\baz
245 filename = ident->toChars();
246 if (packages && packages->dim)
248 OutBuffer buf;
249 int i;
251 for (i = 0; i < packages->dim; i++)
252 { Identifier *pid = (Identifier *)packages->data[i];
254 buf.writestring(pid->toChars());
255 #if _WIN32
256 buf.writeByte('\\');
257 #else
258 buf.writeByte('/');
259 #endif
261 buf.writestring(filename);
262 buf.writeByte(0);
263 filename = (char *)buf.extractData();
266 /* Search along global.path for a .dlt file, then a .di file, then a .d file.
268 char *result = NULL;
269 FileName *fdlt = FileName::forceExt(filename, global.dlt_ext);
270 FileName *fdi = FileName::forceExt(filename, global.hdr_ext);
271 FileName *fd = FileName::forceExt(filename, global.mars_ext);
272 char *sdlt = fdlt->toChars();
273 char *sdi = fdi->toChars();
274 char *sd = fd->toChars();
276 if (FileName::exists(sdlt))
278 result = sdlt;
279 dltSyntax = true;
281 else if (FileName::exists(sdi))
282 result = sdi;
283 else if (FileName::exists(sd))
284 result = sd;
285 else if (FileName::absolute(filename))
287 else if (!global.path)
289 else
291 for (size_t i = 0; i < global.path->dim; i++)
293 char *p = (char *)global.path->data[i];
294 char *n;
296 n = FileName::combine(p, sdlt);
297 if (FileName::exists(n))
298 { result = n;
299 dltSyntax = true;
300 break;
302 mem.free(n);
304 n = FileName::combine(p, sdi);
305 if (FileName::exists(n))
306 { result = n;
307 break;
309 mem.free(n);
311 n = FileName::combine(p, sd);
312 if (FileName::exists(n))
313 { result = n;
314 break;
316 mem.free(n);
320 m = new Module(filename, ident, 0, 0, dltSyntax);
321 m->loc = loc;
323 if (result)
324 m->srcfile = new File(result);
326 if (global.params.verbose)
328 printf("import ");
329 if (packages)
331 for (size_t i = 0; i < packages->dim; i++)
332 { Identifier *pid = (Identifier *)packages->data[i];
333 printf("%s.", pid->toChars());
336 printf("%s\t(%s)\n", ident->toChars(), m->srcfile->toChars());
339 m->read(loc);
340 m->parse();
342 #ifdef IN_GCC
343 d_gcc_magic_module(m);
344 #endif
346 return m;
349 void Module::read(Loc loc)
351 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars());
352 if (srcfile->read())
353 { error(loc, "cannot read file '%s'", srcfile->toChars());
354 fatal();
358 inline unsigned readwordLE(unsigned short *p)
360 #if __I86__
361 return *p;
362 #else
363 return (((unsigned char *)p)[1] << 8) | ((unsigned char *)p)[0];
364 #endif
367 inline unsigned readwordBE(unsigned short *p)
369 return (((unsigned char *)p)[0] << 8) | ((unsigned char *)p)[1];
372 inline unsigned readlongLE(unsigned *p)
374 #if __I86__
375 return *p;
376 #else
377 return ((unsigned char *)p)[0] |
378 (((unsigned char *)p)[1] << 8) |
379 (((unsigned char *)p)[2] << 16) |
380 (((unsigned char *)p)[3] << 24);
381 #endif
384 inline unsigned readlongBE(unsigned *p)
386 return ((unsigned char *)p)[3] |
387 (((unsigned char *)p)[2] << 8) |
388 (((unsigned char *)p)[1] << 16) |
389 (((unsigned char *)p)[0] << 24);
392 #if IN_GCC
393 void Module::parse(bool dump_source)
394 #else
395 void Module::parse()
396 #endif
397 { char *srcname;
398 unsigned char *buf;
399 unsigned buflen;
400 unsigned le;
401 unsigned bom;
403 //printf("Module::parse()\n");
405 srcname = srcfile->name->toChars();
406 //printf("Module::parse(srcname = '%s')\n", srcname);
408 buf = srcfile->buffer;
409 buflen = srcfile->len;
411 if (buflen >= 2)
413 /* Convert all non-UTF-8 formats to UTF-8.
414 * BOM : http://www.unicode.org/faq/utf_bom.html
415 * 00 00 FE FF UTF-32BE, big-endian
416 * FF FE 00 00 UTF-32LE, little-endian
417 * FE FF UTF-16BE, big-endian
418 * FF FE UTF-16LE, little-endian
419 * EF BB BF UTF-8
422 bom = 1; // assume there's a BOM
423 if (buf[0] == 0xFF && buf[1] == 0xFE)
425 if (buflen >= 4 && buf[2] == 0 && buf[3] == 0)
426 { // UTF-32LE
427 le = 1;
429 Lutf32:
430 OutBuffer dbuf;
431 unsigned *pu = (unsigned *)(buf);
432 unsigned *pumax = &pu[buflen / 4];
434 if (buflen & 3)
435 { error("odd length of UTF-32 char source %u", buflen);
436 fatal();
439 dbuf.reserve(buflen / 4);
440 for (pu += bom; pu < pumax; pu++)
441 { unsigned u;
443 u = le ? readlongLE(pu) : readlongBE(pu);
444 if (u & ~0x7F)
446 if (u > 0x10FFFF)
447 { error("UTF-32 value %08x greater than 0x10FFFF", u);
448 fatal();
450 dbuf.writeUTF8(u);
452 else
453 dbuf.writeByte(u);
455 dbuf.writeByte(0); // add 0 as sentinel for scanner
456 buflen = dbuf.offset - 1; // don't include sentinel in count
457 buf = (unsigned char *) dbuf.extractData();
459 else
460 { // UTF-16LE (X86)
461 // Convert it to UTF-8
462 le = 1;
464 Lutf16:
465 OutBuffer dbuf;
466 unsigned short *pu = (unsigned short *)(buf);
467 unsigned short *pumax = &pu[buflen / 2];
469 if (buflen & 1)
470 { error("odd length of UTF-16 char source %u", buflen);
471 fatal();
474 dbuf.reserve(buflen / 2);
475 for (pu += bom; pu < pumax; pu++)
476 { unsigned u;
478 u = le ? readwordLE(pu) : readwordBE(pu);
479 if (u & ~0x7F)
480 { if (u >= 0xD800 && u <= 0xDBFF)
481 { unsigned u2;
483 if (++pu > pumax)
484 { error("surrogate UTF-16 high value %04x at EOF", u);
485 fatal();
487 u2 = le ? readwordLE(pu) : readwordBE(pu);
488 if (u2 < 0xDC00 || u2 > 0xDFFF)
489 { error("surrogate UTF-16 low value %04x out of range", u2);
490 fatal();
492 u = (u - 0xD7C0) << 10;
493 u |= (u2 - 0xDC00);
495 else if (u >= 0xDC00 && u <= 0xDFFF)
496 { error("unpaired surrogate UTF-16 value %04x", u);
497 fatal();
499 else if (u == 0xFFFE || u == 0xFFFF)
500 { error("illegal UTF-16 value %04x", u);
501 fatal();
503 dbuf.writeUTF8(u);
505 else
506 dbuf.writeByte(u);
508 dbuf.writeByte(0); // add 0 as sentinel for scanner
509 buflen = dbuf.offset - 1; // don't include sentinel in count
510 buf = (unsigned char *) dbuf.extractData();
513 else if (buf[0] == 0xFE && buf[1] == 0xFF)
514 { // UTF-16BE
515 le = 0;
516 goto Lutf16;
518 else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF)
519 { // UTF-32BE
520 le = 0;
521 goto Lutf32;
523 else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF)
524 { // UTF-8
526 buf += 3;
527 buflen -= 3;
529 else
531 /* There is no BOM. Make use of Arcane Jill's insight that
532 * the first char of D source must be ASCII to
533 * figure out the encoding.
536 bom = 0;
537 if (buflen >= 4)
538 { if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0)
539 { // UTF-32LE
540 le = 1;
541 goto Lutf32;
543 else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0)
544 { // UTF-32BE
545 le = 0;
546 goto Lutf32;
549 if (buflen >= 2)
551 if (buf[1] == 0)
552 { // UTF-16LE
553 le = 1;
554 goto Lutf16;
556 else if (buf[0] == 0)
557 { // UTF-16BE
558 le = 0;
559 goto Lutf16;
563 // It's UTF-8
564 if (buf[0] >= 0x80)
565 { error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]);
566 fatal();
571 #ifdef IN_GCC
572 // dump utf-8 encoded source
573 if (dump_source)
574 { // %% srcname could contain a path ...
575 d_gcc_dump_source(srcname, "utf-8", buf, buflen);
577 #endif
579 /* If it starts with the string "Ddoc", then it's a documentation
580 * source file.
582 if (buflen >= 4 && memcmp(buf, "Ddoc", 4) == 0)
584 comment = buf + 4;
585 isDocFile = 1;
586 if (!docfile)
587 setDocfile();
588 return;
590 if (isHtml)
592 OutBuffer *dbuf = new OutBuffer();
593 Html h(srcname, buf, buflen);
594 h.extractCode(dbuf);
595 buf = dbuf->data;
596 buflen = dbuf->offset;
597 #ifdef IN_GCC
598 // dump extracted source
599 if (dump_source)
600 d_gcc_dump_source(srcname, "d.utf-8", buf, buflen);
601 #endif
604 if (isDltFile)
606 DltParser p(this, buf, buflen, docfile != NULL);
607 p.nextToken();
608 members = p.parseModule();
609 md = p.md;
610 numlines = p.loc.linnum;
612 else
614 Parser p(this, buf, buflen, docfile != NULL);
615 p.nextToken();
616 members = p.parseModule();
617 md = p.md;
618 numlines = p.loc.linnum;
621 DsymbolTable *dst;
623 if (md)
624 { this->ident = md->id;
625 dst = Package::resolve(md->packages, &this->parent, NULL);
627 else
629 dst = modules;
631 /* Check to see if module name is a valid identifier
633 if (!Lexer::isValidIdentifier(this->ident->toChars()))
634 error("has non-identifier characters in filename, use module declaration instead");
637 // Update global list of modules
638 if (!dst->insert(this))
640 if (md)
641 error(loc, "is in multiple packages %s", md->toChars());
642 else
643 error(loc, "is in multiple defined");
645 else
647 amodules.push(this);
651 void Module::semantic()
652 { int i;
654 if (semanticstarted)
655 return;
657 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
658 semanticstarted = 1;
660 // Note that modules get their own scope, from scratch.
661 // This is so regardless of where in the syntax a module
662 // gets imported, it is unaffected by context.
663 Scope *sc = Scope::createGlobal(this); // create root scope
665 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);
667 // Add import of "object" if this module isn't "object"
668 if (ident != Id::object)
670 Import *im = new Import(0, NULL, Id::object, NULL, 0);
671 members->shift(im);
673 #ifdef IN_GCC
674 else
676 /* va_list is a pain. If va_list involves a struct, add the
677 struct declaration to the "object" module. This depends on
678 the GCC backend not naming the struct something that will
679 cause a conflict or define "va_list" without going through
680 std.stdarg. */
681 Type * t = d_gcc_builtin_va_list_d_type;
682 while (t) {
683 if (t->ty == Tstruct) {
684 StructDeclaration * sd = ((TypeStruct *) t)->sym;
685 sd->parent = this;
686 members->push(sd);
687 break;
689 t = t->next;
692 #endif
694 // Add all symbols into module's symbol table
695 symtab = new DsymbolTable();
696 for (i = 0; i < members->dim; i++)
697 { Dsymbol *s;
699 s = (Dsymbol *)members->data[i];
700 s->addMember(NULL, sc->scopesym, 1);
703 // Pass 1 semantic routines: do public side of the definition
704 for (i = 0; i < members->dim; i++)
705 { Dsymbol *s;
707 s = (Dsymbol *)members->data[i];
708 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
709 s->semantic(sc);
710 runDeferredSemantic();
713 sc = sc->pop();
714 sc->pop();
715 semanticdone = semanticstarted;
716 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
719 void Module::semantic2()
720 { int i;
722 if (deferred.dim)
724 for (int i = 0; i < deferred.dim; i++)
726 Dsymbol *sd = (Dsymbol *)deferred.data[i];
728 sd->error("unable to resolve forward reference in definition");
730 return;
732 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
733 if (semanticstarted >= 2)
734 return;
735 assert(semanticstarted == 1);
736 semanticstarted = 2;
738 // Note that modules get their own scope, from scratch.
739 // This is so regardless of where in the syntax a module
740 // gets imported, it is unaffected by context.
741 Scope *sc = Scope::createGlobal(this); // create root scope
742 //printf("Module = %p\n", sc.scopesym);
744 // Pass 2 semantic routines: do initializers and function bodies
745 for (i = 0; i < members->dim; i++)
746 { Dsymbol *s;
748 s = (Dsymbol *)members->data[i];
749 s->semantic2(sc);
752 sc = sc->pop();
753 sc->pop();
754 semanticdone = semanticstarted;
755 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
758 void Module::semantic3()
759 { int i;
761 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
762 if (semanticstarted >= 3)
763 return;
764 assert(semanticstarted == 2);
765 semanticstarted = 3;
767 // Note that modules get their own scope, from scratch.
768 // This is so regardless of where in the syntax a module
769 // gets imported, it is unaffected by context.
770 Scope *sc = Scope::createGlobal(this); // create root scope
771 //printf("Module = %p\n", sc.scopesym);
773 // Pass 3 semantic routines: do initializers and function bodies
774 for (i = 0; i < members->dim; i++)
775 { Dsymbol *s;
777 s = (Dsymbol *)members->data[i];
778 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
779 s->semantic3(sc);
782 sc = sc->pop();
783 sc->pop();
784 semanticdone = semanticstarted;
787 void Module::inlineScan()
788 { int i;
790 if (semanticstarted >= 4)
791 return;
792 assert(semanticstarted == 3);
793 semanticstarted = 4;
795 // Note that modules get their own scope, from scratch.
796 // This is so regardless of where in the syntax a module
797 // gets imported, it is unaffected by context.
798 //printf("Module = %p\n", sc.scopesym);
800 for (i = 0; i < members->dim; i++)
801 { Dsymbol *s;
803 s = (Dsymbol *)members->data[i];
804 //if (global.params.verbose)
805 //printf("inline scan symbol %s\n", s->toChars());
807 s->inlineScan();
809 semanticdone = semanticstarted;
812 /****************************************************
815 void Module::gensymfile()
817 OutBuffer buf;
818 HdrGenState hgs;
820 //printf("Module::gensymfile()\n");
822 buf.printf("// Sym file generated from '%s'", srcfile->toChars());
823 buf.writenl();
825 for (int i = 0; i < members->dim; i++)
826 { Dsymbol *s = (Dsymbol *)members->data[i];
828 s->toCBuffer(&buf, &hgs);
831 // Transfer image to file
832 symfile->setbuffer(buf.data, buf.offset);
833 buf.data = NULL;
835 symfile->writev();
838 /**********************************
839 * Determine if we need to generate an instance of ModuleInfo
840 * for this Module.
843 int Module::needModuleInfo()
845 return needmoduleinfo || global.params.cov;
848 Dsymbol *Module::search(Loc loc, Identifier *ident, int flags)
850 /* Since modules can be circularly referenced,
851 * need to stop infinite recursive searches.
854 //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch);
855 Dsymbol *s;
856 if (insearch)
857 s = NULL;
858 else if (searchCacheIdent == ident && searchCacheFlags == flags)
859 s = searchCacheSymbol;
860 else
862 insearch = 1;
863 s = ScopeDsymbol::search(loc, ident, flags);
864 insearch = 0;
866 searchCacheIdent = ident;
867 searchCacheSymbol = s;
868 searchCacheFlags = flags;
870 return s;
873 /*******************************************
874 * Can't run semantic on s now, try again later.
877 void Module::addDeferredSemantic(Dsymbol *s)
879 // Don't add it if it is already there
880 for (int i = 0; i < deferred.dim; i++)
882 Dsymbol *sd = (Dsymbol *)deferred.data[i];
884 if (sd == s)
885 return;
888 //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
889 deferred.push(s);
893 /******************************************
894 * Run semantic() on deferred symbols.
897 void Module::runDeferredSemantic()
899 size_t len;
901 static int nested;
902 if (nested)
903 return;
904 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
905 nested++;
909 dprogress = 0;
910 len = deferred.dim;
911 if (!len)
912 break;
914 Dsymbol **todo;
915 Dsymbol *tmp;
916 if (len == 1)
918 todo = &tmp;
920 else
922 todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *));
923 assert(todo);
925 memcpy(todo, deferred.data, len * sizeof(Dsymbol *));
926 deferred.setDim(0);
928 for (int i = 0; i < len; i++)
930 Dsymbol *s = todo[i];
932 s->semantic(NULL);
933 //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars());
935 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
936 } while (deferred.dim < len || dprogress); // while making progress
937 nested--;
938 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
941 /* =========================== ModuleDeclaration ===================== */
943 ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id)
945 this->packages = packages;
946 this->id = id;
949 char *ModuleDeclaration::toChars()
951 OutBuffer buf;
952 int i;
954 if (packages && packages->dim)
956 for (i = 0; i < packages->dim; i++)
957 { Identifier *pid = (Identifier *)packages->data[i];
959 buf.writestring(pid->toChars());
960 buf.writeByte('.');
963 buf.writestring(id->toChars());
964 buf.writeByte(0);
965 return (char *)buf.extractData();
968 /* =========================== Package ===================== */
970 Package::Package(Identifier *ident)
971 : ScopeDsymbol(ident)
976 char *Package::kind()
978 return "package";
982 DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg)
984 DsymbolTable *dst = Module::modules;
985 Dsymbol *parent = NULL;
987 //printf("Package::resolve()\n");
988 if (ppkg)
989 *ppkg = NULL;
991 if (packages)
992 { int i;
994 for (i = 0; i < packages->dim; i++)
995 { Identifier *pid = (Identifier *)packages->data[i];
996 Dsymbol *p;
998 p = dst->lookup(pid);
999 if (!p)
1001 p = new Package(pid);
1002 dst->insert(p);
1003 p->parent = parent;
1004 ((ScopeDsymbol *)p)->symtab = new DsymbolTable();
1006 else
1008 assert(p->isPackage());
1009 if (p->isModule())
1010 { p->error("module and package have the same name");
1011 fatal();
1012 break;
1015 parent = p;
1016 dst = ((Package *)p)->symtab;
1017 if (ppkg && !*ppkg)
1018 *ppkg = (Package *)p;
1020 if (pparent)
1022 *pparent = parent;
1025 return dst;