Renamed language to dlt (Delight)
[delight/core.git] / dmd / module.c
blob9d9c6a5735deb301175c1cd33cfcc8525b11d0e2
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)
58 : Package(ident)
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.dlt_ext) &&
113 !srcfilename->equalsExt("dd"))
115 if (srcfilename->equalsExt("html") ||
116 srcfilename->equalsExt("htm") ||
117 srcfilename->equalsExt("xhtml"))
118 isHtml = 1;
119 else
120 { error("source file name '%s' must have .%s extension", srcfilename->toChars(), global.dlt_ext);
121 fatal();
125 char *argobj;
126 if (global.params.objname)
127 argobj = global.params.objname;
128 else if (global.params.preservePaths)
129 argobj = filename;
130 else
131 argobj = FileName::name(filename);
132 if (!FileName::absolute(argobj))
134 argobj = FileName::combine(global.params.objdir, argobj);
137 if (global.params.objname)
138 objfilename = new FileName(argobj, 0);
139 else
140 objfilename = FileName::forceExt(argobj, global.obj_ext);
142 symfilename = FileName::forceExt(filename, global.sym_ext);
144 srcfile = new File(srcfilename);
146 if (doDocComment)
148 setDocfile();
151 if (doHdrGen)
153 setHdrfile();
156 objfile = new File(objfilename);
157 symfile = new File(symfilename);
160 void Module::setDocfile()
162 FileName *docfilename;
163 char *argdoc;
165 if (global.params.docname)
166 argdoc = global.params.docname;
167 else if (global.params.preservePaths)
168 argdoc = (char *)arg;
169 else
170 argdoc = FileName::name((char *)arg);
171 if (!FileName::absolute(argdoc))
172 { //FileName::ensurePathExists(global.params.docdir);
173 argdoc = FileName::combine(global.params.docdir, argdoc);
175 if (global.params.docname)
176 docfilename = new FileName(argdoc, 0);
177 else
178 docfilename = FileName::forceExt(argdoc, global.doc_ext);
180 if (docfilename->equals(srcfile->name))
181 { error("Source file and documentation file have same name '%s'", srcfile->name->str);
182 fatal();
185 docfile = new File(docfilename);
188 void Module::setHdrfile()
190 FileName *hdrfilename;
191 char *arghdr;
193 if (global.params.hdrname)
194 arghdr = global.params.hdrname;
195 else if (global.params.preservePaths)
196 arghdr = (char *)arg;
197 else
198 arghdr = FileName::name((char *)arg);
199 if (!FileName::absolute(arghdr))
200 { //FileName::ensurePathExists(global.params.hdrdir);
201 arghdr = FileName::combine(global.params.hdrdir, arghdr);
203 if (global.params.hdrname)
204 hdrfilename = new FileName(arghdr, 0);
205 else
206 hdrfilename = FileName::forceExt(arghdr, global.hdr_ext);
208 if (hdrfilename->equals(srcfile->name))
209 { error("Source file and 'header' file have same name '%s'", srcfile->name->str);
210 fatal();
213 hdrfile = new File(hdrfilename);
216 void Module::deleteObjFile()
218 if (global.params.obj)
219 objfile->remove();
220 if (docfile)
221 docfile->remove();
224 Module::~Module()
228 char *Module::kind()
230 return "module";
233 Module *Module::load(Loc loc, Array *packages, Identifier *ident)
234 { Module *m;
235 char *filename;
237 //printf("Module::load(ident = '%s')\n", ident->toChars());
239 // Build module filename by turning:
240 // foo.bar.baz
241 // into:
242 // foo\bar\baz
243 filename = ident->toChars();
244 if (packages && packages->dim)
246 OutBuffer buf;
247 int i;
249 for (i = 0; i < packages->dim; i++)
250 { Identifier *pid = (Identifier *)packages->data[i];
252 buf.writestring(pid->toChars());
253 #if _WIN32
254 buf.writeByte('\\');
255 #else
256 buf.writeByte('/');
257 #endif
259 buf.writestring(filename);
260 buf.writeByte(0);
261 filename = (char *)buf.extractData();
264 m = new Module(filename, ident, 0, 0);
265 m->loc = loc;
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))
278 result = sdlt;
279 else if (FileName::exists(sdi))
280 result = sdi;
281 else if (FileName::exists(sd))
282 result = sd;
283 else if (FileName::absolute(filename))
285 else if (!global.path)
287 else
289 for (size_t i = 0; i < global.path->dim; i++)
291 char *p = (char *)global.path->data[i];
292 char *n;
294 n = FileName::combine(p, sdlt);
295 if (FileName::exists(n))
296 { result = n;
297 break;
299 mem.free(n);
301 n = FileName::combine(p, sdi);
302 if (FileName::exists(n))
303 { result = n;
304 break;
306 mem.free(n);
308 n = FileName::combine(p, sd);
309 if (FileName::exists(n))
310 { result = n;
311 break;
313 mem.free(n);
316 if (result)
317 m->srcfile = new File(result);
319 if (global.params.verbose)
321 printf("import ");
322 if (packages)
324 for (size_t i = 0; i < packages->dim; i++)
325 { Identifier *pid = (Identifier *)packages->data[i];
326 printf("%s.", pid->toChars());
329 printf("%s\t(%s)\n", ident->toChars(), m->srcfile->toChars());
332 m->read(loc);
333 m->parse();
335 #ifdef IN_GCC
336 d_gcc_magic_module(m);
337 #endif
339 return m;
342 void Module::read(Loc loc)
344 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars());
345 if (srcfile->read())
346 { error(loc, "cannot read file '%s'", srcfile->toChars());
347 fatal();
351 inline unsigned readwordLE(unsigned short *p)
353 #if __I86__
354 return *p;
355 #else
356 return (((unsigned char *)p)[1] << 8) | ((unsigned char *)p)[0];
357 #endif
360 inline unsigned readwordBE(unsigned short *p)
362 return (((unsigned char *)p)[0] << 8) | ((unsigned char *)p)[1];
365 inline unsigned readlongLE(unsigned *p)
367 #if __I86__
368 return *p;
369 #else
370 return ((unsigned char *)p)[0] |
371 (((unsigned char *)p)[1] << 8) |
372 (((unsigned char *)p)[2] << 16) |
373 (((unsigned char *)p)[3] << 24);
374 #endif
377 inline unsigned readlongBE(unsigned *p)
379 return ((unsigned char *)p)[3] |
380 (((unsigned char *)p)[2] << 8) |
381 (((unsigned char *)p)[1] << 16) |
382 (((unsigned char *)p)[0] << 24);
385 #if IN_GCC
386 void Module::parse(bool dump_source)
387 #else
388 void Module::parse()
389 #endif
390 { char *srcname;
391 unsigned char *buf;
392 unsigned buflen;
393 unsigned le;
394 unsigned bom;
396 //printf("Module::parse()\n");
398 srcname = srcfile->name->toChars();
399 //printf("Module::parse(srcname = '%s')\n", srcname);
401 buf = srcfile->buffer;
402 buflen = srcfile->len;
404 if (buflen >= 2)
406 /* Convert all non-UTF-8 formats to UTF-8.
407 * BOM : http://www.unicode.org/faq/utf_bom.html
408 * 00 00 FE FF UTF-32BE, big-endian
409 * FF FE 00 00 UTF-32LE, little-endian
410 * FE FF UTF-16BE, big-endian
411 * FF FE UTF-16LE, little-endian
412 * EF BB BF UTF-8
415 bom = 1; // assume there's a BOM
416 if (buf[0] == 0xFF && buf[1] == 0xFE)
418 if (buflen >= 4 && buf[2] == 0 && buf[3] == 0)
419 { // UTF-32LE
420 le = 1;
422 Lutf32:
423 OutBuffer dbuf;
424 unsigned *pu = (unsigned *)(buf);
425 unsigned *pumax = &pu[buflen / 4];
427 if (buflen & 3)
428 { error("odd length of UTF-32 char source %u", buflen);
429 fatal();
432 dbuf.reserve(buflen / 4);
433 for (pu += bom; pu < pumax; pu++)
434 { unsigned u;
436 u = le ? readlongLE(pu) : readlongBE(pu);
437 if (u & ~0x7F)
439 if (u > 0x10FFFF)
440 { error("UTF-32 value %08x greater than 0x10FFFF", u);
441 fatal();
443 dbuf.writeUTF8(u);
445 else
446 dbuf.writeByte(u);
448 dbuf.writeByte(0); // add 0 as sentinel for scanner
449 buflen = dbuf.offset - 1; // don't include sentinel in count
450 buf = (unsigned char *) dbuf.extractData();
452 else
453 { // UTF-16LE (X86)
454 // Convert it to UTF-8
455 le = 1;
457 Lutf16:
458 OutBuffer dbuf;
459 unsigned short *pu = (unsigned short *)(buf);
460 unsigned short *pumax = &pu[buflen / 2];
462 if (buflen & 1)
463 { error("odd length of UTF-16 char source %u", buflen);
464 fatal();
467 dbuf.reserve(buflen / 2);
468 for (pu += bom; pu < pumax; pu++)
469 { unsigned u;
471 u = le ? readwordLE(pu) : readwordBE(pu);
472 if (u & ~0x7F)
473 { if (u >= 0xD800 && u <= 0xDBFF)
474 { unsigned u2;
476 if (++pu > pumax)
477 { error("surrogate UTF-16 high value %04x at EOF", u);
478 fatal();
480 u2 = le ? readwordLE(pu) : readwordBE(pu);
481 if (u2 < 0xDC00 || u2 > 0xDFFF)
482 { error("surrogate UTF-16 low value %04x out of range", u2);
483 fatal();
485 u = (u - 0xD7C0) << 10;
486 u |= (u2 - 0xDC00);
488 else if (u >= 0xDC00 && u <= 0xDFFF)
489 { error("unpaired surrogate UTF-16 value %04x", u);
490 fatal();
492 else if (u == 0xFFFE || u == 0xFFFF)
493 { error("illegal UTF-16 value %04x", u);
494 fatal();
496 dbuf.writeUTF8(u);
498 else
499 dbuf.writeByte(u);
501 dbuf.writeByte(0); // add 0 as sentinel for scanner
502 buflen = dbuf.offset - 1; // don't include sentinel in count
503 buf = (unsigned char *) dbuf.extractData();
506 else if (buf[0] == 0xFE && buf[1] == 0xFF)
507 { // UTF-16BE
508 le = 0;
509 goto Lutf16;
511 else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF)
512 { // UTF-32BE
513 le = 0;
514 goto Lutf32;
516 else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF)
517 { // UTF-8
519 buf += 3;
520 buflen -= 3;
522 else
524 /* There is no BOM. Make use of Arcane Jill's insight that
525 * the first char of D source must be ASCII to
526 * figure out the encoding.
529 bom = 0;
530 if (buflen >= 4)
531 { if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0)
532 { // UTF-32LE
533 le = 1;
534 goto Lutf32;
536 else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0)
537 { // UTF-32BE
538 le = 0;
539 goto Lutf32;
542 if (buflen >= 2)
544 if (buf[1] == 0)
545 { // UTF-16LE
546 le = 1;
547 goto Lutf16;
549 else if (buf[0] == 0)
550 { // UTF-16BE
551 le = 0;
552 goto Lutf16;
556 // It's UTF-8
557 if (buf[0] >= 0x80)
558 { error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]);
559 fatal();
564 #ifdef IN_GCC
565 // dump utf-8 encoded source
566 if (dump_source)
567 { // %% srcname could contain a path ...
568 d_gcc_dump_source(srcname, "utf-8", buf, buflen);
570 #endif
572 /* If it starts with the string "Ddoc", then it's a documentation
573 * source file.
575 if (buflen >= 4 && memcmp(buf, "Ddoc", 4) == 0)
577 comment = buf + 4;
578 isDocFile = 1;
579 if (!docfile)
580 setDocfile();
581 return;
583 if (isHtml)
585 OutBuffer *dbuf = new OutBuffer();
586 Html h(srcname, buf, buflen);
587 h.extractCode(dbuf);
588 buf = dbuf->data;
589 buflen = dbuf->offset;
590 #ifdef IN_GCC
591 // dump extracted source
592 if (dump_source)
593 d_gcc_dump_source(srcname, "d.utf-8", buf, buflen);
594 #endif
596 Parser p(this, buf, buflen, docfile != NULL);
597 p.nextToken();
598 members = p.parseModule();
599 md = p.md;
600 numlines = p.loc.linnum;
602 DsymbolTable *dst;
604 if (md)
605 { this->ident = md->id;
606 dst = Package::resolve(md->packages, &this->parent, NULL);
608 else
610 dst = modules;
612 /* Check to see if module name is a valid identifier
614 if (!Lexer::isValidIdentifier(this->ident->toChars()))
615 error("has non-identifier characters in filename, use module declaration instead");
618 // Update global list of modules
619 if (!dst->insert(this))
621 if (md)
622 error(loc, "is in multiple packages %s", md->toChars());
623 else
624 error(loc, "is in multiple defined");
626 else
628 amodules.push(this);
632 void Module::semantic()
633 { int i;
635 if (semanticstarted)
636 return;
638 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
639 semanticstarted = 1;
641 // Note that modules get their own scope, from scratch.
642 // This is so regardless of where in the syntax a module
643 // gets imported, it is unaffected by context.
644 Scope *sc = Scope::createGlobal(this); // create root scope
646 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);
648 // Add import of "object" if this module isn't "object"
649 if (ident != Id::object)
651 Import *im = new Import(0, NULL, Id::object, NULL, 0);
652 members->shift(im);
654 #ifdef IN_GCC
655 else
657 /* va_list is a pain. If va_list involves a struct, add the
658 struct declaration to the "object" module. This depends on
659 the GCC backend not naming the struct something that will
660 cause a conflict or define "va_list" without going through
661 std.stdarg. */
662 Type * t = d_gcc_builtin_va_list_d_type;
663 while (t) {
664 if (t->ty == Tstruct) {
665 StructDeclaration * sd = ((TypeStruct *) t)->sym;
666 sd->parent = this;
667 members->push(sd);
668 break;
670 t = t->next;
673 #endif
675 // Add all symbols into module's symbol table
676 symtab = new DsymbolTable();
677 for (i = 0; i < members->dim; i++)
678 { Dsymbol *s;
680 s = (Dsymbol *)members->data[i];
681 s->addMember(NULL, sc->scopesym, 1);
684 // Pass 1 semantic routines: do public side of the definition
685 for (i = 0; i < members->dim; i++)
686 { Dsymbol *s;
688 s = (Dsymbol *)members->data[i];
689 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
690 s->semantic(sc);
691 runDeferredSemantic();
694 sc = sc->pop();
695 sc->pop();
696 semanticdone = semanticstarted;
697 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
700 void Module::semantic2()
701 { int i;
703 if (deferred.dim)
705 for (int i = 0; i < deferred.dim; i++)
707 Dsymbol *sd = (Dsymbol *)deferred.data[i];
709 sd->error("unable to resolve forward reference in definition");
711 return;
713 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
714 if (semanticstarted >= 2)
715 return;
716 assert(semanticstarted == 1);
717 semanticstarted = 2;
719 // Note that modules get their own scope, from scratch.
720 // This is so regardless of where in the syntax a module
721 // gets imported, it is unaffected by context.
722 Scope *sc = Scope::createGlobal(this); // create root scope
723 //printf("Module = %p\n", sc.scopesym);
725 // Pass 2 semantic routines: do initializers and function bodies
726 for (i = 0; i < members->dim; i++)
727 { Dsymbol *s;
729 s = (Dsymbol *)members->data[i];
730 s->semantic2(sc);
733 sc = sc->pop();
734 sc->pop();
735 semanticdone = semanticstarted;
736 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
739 void Module::semantic3()
740 { int i;
742 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
743 if (semanticstarted >= 3)
744 return;
745 assert(semanticstarted == 2);
746 semanticstarted = 3;
748 // Note that modules get their own scope, from scratch.
749 // This is so regardless of where in the syntax a module
750 // gets imported, it is unaffected by context.
751 Scope *sc = Scope::createGlobal(this); // create root scope
752 //printf("Module = %p\n", sc.scopesym);
754 // Pass 3 semantic routines: do initializers and function bodies
755 for (i = 0; i < members->dim; i++)
756 { Dsymbol *s;
758 s = (Dsymbol *)members->data[i];
759 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
760 s->semantic3(sc);
763 sc = sc->pop();
764 sc->pop();
765 semanticdone = semanticstarted;
768 void Module::inlineScan()
769 { int i;
771 if (semanticstarted >= 4)
772 return;
773 assert(semanticstarted == 3);
774 semanticstarted = 4;
776 // Note that modules get their own scope, from scratch.
777 // This is so regardless of where in the syntax a module
778 // gets imported, it is unaffected by context.
779 //printf("Module = %p\n", sc.scopesym);
781 for (i = 0; i < members->dim; i++)
782 { Dsymbol *s;
784 s = (Dsymbol *)members->data[i];
785 //if (global.params.verbose)
786 //printf("inline scan symbol %s\n", s->toChars());
788 s->inlineScan();
790 semanticdone = semanticstarted;
793 /****************************************************
796 void Module::gensymfile()
798 OutBuffer buf;
799 HdrGenState hgs;
801 //printf("Module::gensymfile()\n");
803 buf.printf("// Sym file generated from '%s'", srcfile->toChars());
804 buf.writenl();
806 for (int i = 0; i < members->dim; i++)
807 { Dsymbol *s = (Dsymbol *)members->data[i];
809 s->toCBuffer(&buf, &hgs);
812 // Transfer image to file
813 symfile->setbuffer(buf.data, buf.offset);
814 buf.data = NULL;
816 symfile->writev();
819 /**********************************
820 * Determine if we need to generate an instance of ModuleInfo
821 * for this Module.
824 int Module::needModuleInfo()
826 return needmoduleinfo || global.params.cov;
829 Dsymbol *Module::search(Loc loc, Identifier *ident, int flags)
831 /* Since modules can be circularly referenced,
832 * need to stop infinite recursive searches.
835 //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch);
836 Dsymbol *s;
837 if (insearch)
838 s = NULL;
839 else if (searchCacheIdent == ident && searchCacheFlags == flags)
840 s = searchCacheSymbol;
841 else
843 insearch = 1;
844 s = ScopeDsymbol::search(loc, ident, flags);
845 insearch = 0;
847 searchCacheIdent = ident;
848 searchCacheSymbol = s;
849 searchCacheFlags = flags;
851 return s;
854 /*******************************************
855 * Can't run semantic on s now, try again later.
858 void Module::addDeferredSemantic(Dsymbol *s)
860 // Don't add it if it is already there
861 for (int i = 0; i < deferred.dim; i++)
863 Dsymbol *sd = (Dsymbol *)deferred.data[i];
865 if (sd == s)
866 return;
869 //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
870 deferred.push(s);
874 /******************************************
875 * Run semantic() on deferred symbols.
878 void Module::runDeferredSemantic()
880 size_t len;
882 static int nested;
883 if (nested)
884 return;
885 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
886 nested++;
890 dprogress = 0;
891 len = deferred.dim;
892 if (!len)
893 break;
895 Dsymbol **todo;
896 Dsymbol *tmp;
897 if (len == 1)
899 todo = &tmp;
901 else
903 todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *));
904 assert(todo);
906 memcpy(todo, deferred.data, len * sizeof(Dsymbol *));
907 deferred.setDim(0);
909 for (int i = 0; i < len; i++)
911 Dsymbol *s = todo[i];
913 s->semantic(NULL);
914 //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars());
916 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
917 } while (deferred.dim < len || dprogress); // while making progress
918 nested--;
919 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
922 /* =========================== ModuleDeclaration ===================== */
924 ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id)
926 this->packages = packages;
927 this->id = id;
930 char *ModuleDeclaration::toChars()
932 OutBuffer buf;
933 int i;
935 if (packages && packages->dim)
937 for (i = 0; i < packages->dim; i++)
938 { Identifier *pid = (Identifier *)packages->data[i];
940 buf.writestring(pid->toChars());
941 buf.writeByte('.');
944 buf.writestring(id->toChars());
945 buf.writeByte(0);
946 return (char *)buf.extractData();
949 /* =========================== Package ===================== */
951 Package::Package(Identifier *ident)
952 : ScopeDsymbol(ident)
957 char *Package::kind()
959 return "package";
963 DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg)
965 DsymbolTable *dst = Module::modules;
966 Dsymbol *parent = NULL;
968 //printf("Package::resolve()\n");
969 if (ppkg)
970 *ppkg = NULL;
972 if (packages)
973 { int i;
975 for (i = 0; i < packages->dim; i++)
976 { Identifier *pid = (Identifier *)packages->data[i];
977 Dsymbol *p;
979 p = dst->lookup(pid);
980 if (!p)
982 p = new Package(pid);
983 dst->insert(p);
984 p->parent = parent;
985 ((ScopeDsymbol *)p)->symtab = new DsymbolTable();
987 else
989 assert(p->isPackage());
990 if (p->isModule())
991 { p->error("module and package have the same name");
992 fatal();
993 break;
996 parent = p;
997 dst = ((Package *)p)->symtab;
998 if (ppkg && !*ppkg)
999 *ppkg = (Package *)p;
1001 if (pparent)
1003 *pparent = parent;
1006 return dst;