Don't include dynamic arrays in non-null checks
[delight/core.git] / dmd2 / module.c
blob32b3b6ff1418bbb89e1b4f038d6b1e0c6c9e6240
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.mars_ext);
112 if (!srcfilename->equalsExt(global.mars_ext) &&
113 !srcfilename->equalsExt("dd"))
115 if (srcfilename->equalsExt("html") ||
116 srcfilename->equalsExt("htm") ||
117 srcfilename->equalsExt("xhtml"))
118 { if (!global.params.useDeprecated)
119 error("html source files is deprecated %s", srcfilename->toChars());
120 isHtml = 1;
122 else
123 { error("source file name '%s' must have .%s extension", srcfilename->toChars(), global.mars_ext);
124 fatal();
128 char *argobj;
129 if (global.params.objname)
130 argobj = global.params.objname;
131 else if (global.params.preservePaths)
132 argobj = filename;
133 else
134 argobj = FileName::name(filename);
135 if (!FileName::absolute(argobj))
137 argobj = FileName::combine(global.params.objdir, argobj);
140 if (global.params.objname)
141 objfilename = new FileName(argobj, 0);
142 else
143 objfilename = FileName::forceExt(argobj, global.obj_ext);
145 symfilename = FileName::forceExt(filename, global.sym_ext);
147 srcfile = new File(srcfilename);
149 if (doDocComment)
151 setDocfile();
154 if (doHdrGen)
156 setHdrfile();
159 objfile = new File(objfilename);
160 symfile = new File(symfilename);
163 void Module::setDocfile()
165 FileName *docfilename;
166 char *argdoc;
168 if (global.params.docname)
169 argdoc = global.params.docname;
170 else if (global.params.preservePaths)
171 argdoc = (char *)arg;
172 else
173 argdoc = FileName::name((char *)arg);
174 if (!FileName::absolute(argdoc))
175 { //FileName::ensurePathExists(global.params.docdir);
176 argdoc = FileName::combine(global.params.docdir, argdoc);
178 if (global.params.docname)
179 docfilename = new FileName(argdoc, 0);
180 else
181 docfilename = FileName::forceExt(argdoc, global.doc_ext);
183 if (docfilename->equals(srcfile->name))
184 { error("Source file and documentation file have same name '%s'", srcfile->name->str);
185 fatal();
188 docfile = new File(docfilename);
191 void Module::setHdrfile()
193 FileName *hdrfilename;
194 char *arghdr;
196 if (global.params.hdrname)
197 arghdr = global.params.hdrname;
198 else if (global.params.preservePaths)
199 arghdr = (char *)arg;
200 else
201 arghdr = FileName::name((char *)arg);
202 if (!FileName::absolute(arghdr))
203 { //FileName::ensurePathExists(global.params.hdrdir);
204 arghdr = FileName::combine(global.params.hdrdir, arghdr);
206 if (global.params.hdrname)
207 hdrfilename = new FileName(arghdr, 0);
208 else
209 hdrfilename = FileName::forceExt(arghdr, global.hdr_ext);
211 if (hdrfilename->equals(srcfile->name))
212 { error("Source file and 'header' file have same name '%s'", srcfile->name->str);
213 fatal();
216 hdrfile = new File(hdrfilename);
219 void Module::deleteObjFile()
221 if (global.params.obj)
222 objfile->remove();
223 if (docfile)
224 docfile->remove();
227 Module::~Module()
231 char *Module::kind()
233 return "module";
236 Module *Module::load(Loc loc, Array *packages, Identifier *ident)
237 { Module *m;
238 char *filename;
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 m = new Module(filename, ident, 0, 0);
268 m->loc = loc;
270 /* Search along global.path for .di file, then .d file.
272 char *result = NULL;
273 FileName *fdi = FileName::forceExt(filename, global.hdr_ext);
274 FileName *fd = FileName::forceExt(filename, global.mars_ext);
275 char *sdi = fdi->toChars();
276 char *sd = fd->toChars();
278 if (FileName::exists(sdi))
279 result = sdi;
280 else if (FileName::exists(sd))
281 result = sd;
282 else if (FileName::absolute(filename))
284 else if (!global.path)
286 else
288 for (size_t i = 0; i < global.path->dim; i++)
290 char *p = (char *)global.path->data[i];
291 char *n = FileName::combine(p, sdi);
292 if (FileName::exists(n))
293 { result = n;
294 break;
296 mem.free(n);
297 n = FileName::combine(p, sd);
298 if (FileName::exists(n))
299 { result = n;
300 break;
302 mem.free(n);
305 if (result)
306 m->srcfile = new File(result);
308 if (global.params.verbose)
310 printf("import ");
311 if (packages)
313 for (size_t i = 0; i < packages->dim; i++)
314 { Identifier *pid = (Identifier *)packages->data[i];
315 printf("%s.", pid->toChars());
318 printf("%s\t(%s)\n", ident->toChars(), m->srcfile->toChars());
321 m->read(loc);
322 m->parse();
324 #ifdef IN_GCC
325 d_gcc_magic_module(m);
326 #endif
328 return m;
331 void Module::read(Loc loc)
333 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars());
334 if (srcfile->read())
335 { error(loc, "cannot read file '%s'", srcfile->toChars());
336 fatal();
340 inline unsigned readwordLE(unsigned short *p)
342 #if __I86__
343 return *p;
344 #else
345 return (((unsigned char *)p)[1] << 8) | ((unsigned char *)p)[0];
346 #endif
349 inline unsigned readwordBE(unsigned short *p)
351 return (((unsigned char *)p)[0] << 8) | ((unsigned char *)p)[1];
354 inline unsigned readlongLE(unsigned *p)
356 #if __I86__
357 return *p;
358 #else
359 return ((unsigned char *)p)[0] |
360 (((unsigned char *)p)[1] << 8) |
361 (((unsigned char *)p)[2] << 16) |
362 (((unsigned char *)p)[3] << 24);
363 #endif
366 inline unsigned readlongBE(unsigned *p)
368 return ((unsigned char *)p)[3] |
369 (((unsigned char *)p)[2] << 8) |
370 (((unsigned char *)p)[1] << 16) |
371 (((unsigned char *)p)[0] << 24);
374 #if IN_GCC
375 void Module::parse(bool dump_source)
376 #else
377 void Module::parse()
378 #endif
379 { char *srcname;
380 unsigned char *buf;
381 unsigned buflen;
382 unsigned le;
383 unsigned bom;
385 //printf("Module::parse()\n");
387 srcname = srcfile->name->toChars();
388 //printf("Module::parse(srcname = '%s')\n", srcname);
390 buf = srcfile->buffer;
391 buflen = srcfile->len;
393 if (buflen >= 2)
395 /* Convert all non-UTF-8 formats to UTF-8.
396 * BOM : http://www.unicode.org/faq/utf_bom.html
397 * 00 00 FE FF UTF-32BE, big-endian
398 * FF FE 00 00 UTF-32LE, little-endian
399 * FE FF UTF-16BE, big-endian
400 * FF FE UTF-16LE, little-endian
401 * EF BB BF UTF-8
404 bom = 1; // assume there's a BOM
405 if (buf[0] == 0xFF && buf[1] == 0xFE)
407 if (buflen >= 4 && buf[2] == 0 && buf[3] == 0)
408 { // UTF-32LE
409 le = 1;
411 Lutf32:
412 OutBuffer dbuf;
413 unsigned *pu = (unsigned *)(buf);
414 unsigned *pumax = &pu[buflen / 4];
416 if (buflen & 3)
417 { error("odd length of UTF-32 char source %u", buflen);
418 fatal();
421 dbuf.reserve(buflen / 4);
422 for (pu += bom; pu < pumax; pu++)
423 { unsigned u;
425 u = le ? readlongLE(pu) : readlongBE(pu);
426 if (u & ~0x7F)
428 if (u > 0x10FFFF)
429 { error("UTF-32 value %08x greater than 0x10FFFF", u);
430 fatal();
432 dbuf.writeUTF8(u);
434 else
435 dbuf.writeByte(u);
437 dbuf.writeByte(0); // add 0 as sentinel for scanner
438 buflen = dbuf.offset - 1; // don't include sentinel in count
439 buf = (unsigned char *) dbuf.extractData();
441 else
442 { // UTF-16LE (X86)
443 // Convert it to UTF-8
444 le = 1;
446 Lutf16:
447 OutBuffer dbuf;
448 unsigned short *pu = (unsigned short *)(buf);
449 unsigned short *pumax = &pu[buflen / 2];
451 if (buflen & 1)
452 { error("odd length of UTF-16 char source %u", buflen);
453 fatal();
456 dbuf.reserve(buflen / 2);
457 for (pu += bom; pu < pumax; pu++)
458 { unsigned u;
460 u = le ? readwordLE(pu) : readwordBE(pu);
461 if (u & ~0x7F)
462 { if (u >= 0xD800 && u <= 0xDBFF)
463 { unsigned u2;
465 if (++pu > pumax)
466 { error("surrogate UTF-16 high value %04x at EOF", u);
467 fatal();
469 u2 = le ? readwordLE(pu) : readwordBE(pu);
470 if (u2 < 0xDC00 || u2 > 0xDFFF)
471 { error("surrogate UTF-16 low value %04x out of range", u2);
472 fatal();
474 u = (u - 0xD7C0) << 10;
475 u |= (u2 - 0xDC00);
477 else if (u >= 0xDC00 && u <= 0xDFFF)
478 { error("unpaired surrogate UTF-16 value %04x", u);
479 fatal();
481 else if (u == 0xFFFE || u == 0xFFFF)
482 { error("illegal UTF-16 value %04x", u);
483 fatal();
485 dbuf.writeUTF8(u);
487 else
488 dbuf.writeByte(u);
490 dbuf.writeByte(0); // add 0 as sentinel for scanner
491 buflen = dbuf.offset - 1; // don't include sentinel in count
492 buf = (unsigned char *) dbuf.extractData();
495 else if (buf[0] == 0xFE && buf[1] == 0xFF)
496 { // UTF-16BE
497 le = 0;
498 goto Lutf16;
500 else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF)
501 { // UTF-32BE
502 le = 0;
503 goto Lutf32;
505 else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF)
506 { // UTF-8
508 buf += 3;
509 buflen -= 3;
511 else
513 /* There is no BOM. Make use of Arcane Jill's insight that
514 * the first char of D source must be ASCII to
515 * figure out the encoding.
518 bom = 0;
519 if (buflen >= 4)
520 { if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0)
521 { // UTF-32LE
522 le = 1;
523 goto Lutf32;
525 else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0)
526 { // UTF-32BE
527 le = 0;
528 goto Lutf32;
531 if (buflen >= 2)
533 if (buf[1] == 0)
534 { // UTF-16LE
535 le = 1;
536 goto Lutf16;
538 else if (buf[0] == 0)
539 { // UTF-16BE
540 le = 0;
541 goto Lutf16;
545 // It's UTF-8
546 if (buf[0] >= 0x80)
547 { error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]);
548 fatal();
553 #ifdef IN_GCC
554 // dump utf-8 encoded source
555 if (dump_source)
556 { // %% srcname could contain a path ...
557 d_gcc_dump_source(srcname, "utf-8", buf, buflen);
559 #endif
561 /* If it starts with the string "Ddoc", then it's a documentation
562 * source file.
564 if (buflen >= 4 && memcmp(buf, "Ddoc", 4) == 0)
566 comment = buf + 4;
567 isDocFile = 1;
568 if (!docfile)
569 setDocfile();
570 return;
572 if (isHtml)
574 OutBuffer *dbuf = new OutBuffer();
575 Html h(srcname, buf, buflen);
576 h.extractCode(dbuf);
577 buf = dbuf->data;
578 buflen = dbuf->offset;
579 #ifdef IN_GCC
580 // dump extracted source
581 if (dump_source)
582 d_gcc_dump_source(srcname, "d.utf-8", buf, buflen);
583 #endif
585 Parser p(this, buf, buflen, docfile != NULL);
586 p.nextToken();
587 members = p.parseModule();
588 md = p.md;
589 numlines = p.loc.linnum;
591 DsymbolTable *dst;
593 if (md)
594 { this->ident = md->id;
595 dst = Package::resolve(md->packages, &this->parent, NULL);
597 else
599 dst = modules;
601 /* Check to see if module name is a valid identifier
603 if (!Lexer::isValidIdentifier(this->ident->toChars()))
604 error("has non-identifier characters in filename, use module declaration instead");
607 // Update global list of modules
608 if (!dst->insert(this))
610 if (md)
611 error(loc, "is in multiple packages %s", md->toChars());
612 else
613 error(loc, "is in multiple defined");
615 else
617 amodules.push(this);
621 void Module::semantic()
622 { int i;
624 if (semanticstarted)
625 return;
627 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
628 semanticstarted = 1;
630 // Note that modules get their own scope, from scratch.
631 // This is so regardless of where in the syntax a module
632 // gets imported, it is unaffected by context.
633 Scope *sc = Scope::createGlobal(this); // create root scope
635 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);
637 // Add import of "object" if this module isn't "object"
638 if (ident != Id::object)
640 Import *im = new Import(0, NULL, Id::object, NULL, 0);
641 members->shift(im);
643 #ifdef IN_GCC
644 else
646 /* va_list is a pain. If va_list involves a struct, add the
647 struct declaration to the "object" module. This depends on
648 the GCC backend not naming the struct something that will
649 cause a conflict or define "va_list" without going through
650 std.stdarg. */
651 Type * t = d_gcc_builtin_va_list_d_type;
652 while (t) {
653 if (t->ty == Tstruct) {
654 StructDeclaration * sd = ((TypeStruct *) t)->sym;
655 sd->parent = this;
656 members->push(sd);
657 break;
659 t = t->nextOf();
662 #endif
664 // Add all symbols into module's symbol table
665 symtab = new DsymbolTable();
666 for (i = 0; i < members->dim; i++)
667 { Dsymbol *s;
669 s = (Dsymbol *)members->data[i];
670 s->addMember(NULL, sc->scopesym, 1);
673 // Pass 1 semantic routines: do public side of the definition
674 for (i = 0; i < members->dim; i++)
675 { Dsymbol *s;
677 s = (Dsymbol *)members->data[i];
678 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
679 s->semantic(sc);
680 runDeferredSemantic();
683 sc = sc->pop();
684 sc->pop();
685 semanticdone = semanticstarted;
686 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
689 void Module::semantic2()
690 { int i;
692 if (deferred.dim)
694 for (int i = 0; i < deferred.dim; i++)
696 Dsymbol *sd = (Dsymbol *)deferred.data[i];
698 sd->error("unable to resolve forward reference in definition");
700 return;
702 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
703 if (semanticstarted >= 2)
704 return;
705 assert(semanticstarted == 1);
706 semanticstarted = 2;
708 // Note that modules get their own scope, from scratch.
709 // This is so regardless of where in the syntax a module
710 // gets imported, it is unaffected by context.
711 Scope *sc = Scope::createGlobal(this); // create root scope
712 //printf("Module = %p\n", sc.scopesym);
714 // Pass 2 semantic routines: do initializers and function bodies
715 for (i = 0; i < members->dim; i++)
716 { Dsymbol *s;
718 s = (Dsymbol *)members->data[i];
719 s->semantic2(sc);
722 sc = sc->pop();
723 sc->pop();
724 semanticdone = semanticstarted;
725 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
728 void Module::semantic3()
729 { int i;
731 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
732 if (semanticstarted >= 3)
733 return;
734 assert(semanticstarted == 2);
735 semanticstarted = 3;
737 // Note that modules get their own scope, from scratch.
738 // This is so regardless of where in the syntax a module
739 // gets imported, it is unaffected by context.
740 Scope *sc = Scope::createGlobal(this); // create root scope
741 //printf("Module = %p\n", sc.scopesym);
743 // Pass 3 semantic routines: do initializers and function bodies
744 for (i = 0; i < members->dim; i++)
745 { Dsymbol *s;
747 s = (Dsymbol *)members->data[i];
748 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
749 s->semantic3(sc);
752 sc = sc->pop();
753 sc->pop();
754 semanticdone = semanticstarted;
757 void Module::inlineScan()
758 { int i;
760 if (semanticstarted >= 4)
761 return;
762 assert(semanticstarted == 3);
763 semanticstarted = 4;
765 // Note that modules get their own scope, from scratch.
766 // This is so regardless of where in the syntax a module
767 // gets imported, it is unaffected by context.
768 //printf("Module = %p\n", sc.scopesym);
770 for (i = 0; i < members->dim; i++)
771 { Dsymbol *s;
773 s = (Dsymbol *)members->data[i];
774 //if (global.params.verbose)
775 //printf("inline scan symbol %s\n", s->toChars());
777 s->inlineScan();
779 semanticdone = semanticstarted;
782 /****************************************************
785 void Module::gensymfile()
787 OutBuffer buf;
788 HdrGenState hgs;
790 //printf("Module::gensymfile()\n");
792 buf.printf("// Sym file generated from '%s'", srcfile->toChars());
793 buf.writenl();
795 for (int i = 0; i < members->dim; i++)
796 { Dsymbol *s = (Dsymbol *)members->data[i];
798 s->toCBuffer(&buf, &hgs);
801 // Transfer image to file
802 symfile->setbuffer(buf.data, buf.offset);
803 buf.data = NULL;
805 symfile->writev();
808 /**********************************
809 * Determine if we need to generate an instance of ModuleInfo
810 * for this Module.
813 int Module::needModuleInfo()
815 return needmoduleinfo || global.params.cov;
818 Dsymbol *Module::search(Loc loc, Identifier *ident, int flags)
820 /* Since modules can be circularly referenced,
821 * need to stop infinite recursive searches.
824 //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch);
825 Dsymbol *s;
826 if (insearch)
827 s = NULL;
828 else if (searchCacheIdent == ident && searchCacheFlags == flags)
829 s = searchCacheSymbol;
830 else
832 insearch = 1;
833 s = ScopeDsymbol::search(loc, ident, flags);
834 insearch = 0;
836 searchCacheIdent = ident;
837 searchCacheSymbol = s;
838 searchCacheFlags = flags;
840 return s;
843 /*******************************************
844 * Can't run semantic on s now, try again later.
847 void Module::addDeferredSemantic(Dsymbol *s)
849 // Don't add it if it is already there
850 for (int i = 0; i < deferred.dim; i++)
852 Dsymbol *sd = (Dsymbol *)deferred.data[i];
854 if (sd == s)
855 return;
858 //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
859 deferred.push(s);
863 /******************************************
864 * Run semantic() on deferred symbols.
867 void Module::runDeferredSemantic()
869 size_t len;
871 static int nested;
872 if (nested)
873 return;
874 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
875 nested++;
879 dprogress = 0;
880 len = deferred.dim;
881 if (!len)
882 break;
884 Dsymbol **todo;
885 Dsymbol *tmp;
886 if (len == 1)
888 todo = &tmp;
890 else
892 todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *));
893 assert(todo);
895 memcpy(todo, deferred.data, len * sizeof(Dsymbol *));
896 deferred.setDim(0);
898 for (int i = 0; i < len; i++)
900 Dsymbol *s = todo[i];
902 s->semantic(NULL);
903 //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars());
905 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
906 } while (deferred.dim < len || dprogress); // while making progress
907 nested--;
908 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
911 /* =========================== ModuleDeclaration ===================== */
913 ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id)
915 this->packages = packages;
916 this->id = id;
919 char *ModuleDeclaration::toChars()
921 OutBuffer buf;
922 int i;
924 if (packages && packages->dim)
926 for (i = 0; i < packages->dim; i++)
927 { Identifier *pid = (Identifier *)packages->data[i];
929 buf.writestring(pid->toChars());
930 buf.writeByte('.');
933 buf.writestring(id->toChars());
934 buf.writeByte(0);
935 return (char *)buf.extractData();
938 /* =========================== Package ===================== */
940 Package::Package(Identifier *ident)
941 : ScopeDsymbol(ident)
946 char *Package::kind()
948 return "package";
952 DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg)
954 DsymbolTable *dst = Module::modules;
955 Dsymbol *parent = NULL;
957 //printf("Package::resolve()\n");
958 if (ppkg)
959 *ppkg = NULL;
961 if (packages)
962 { int i;
964 for (i = 0; i < packages->dim; i++)
965 { Identifier *pid = (Identifier *)packages->data[i];
966 Dsymbol *p;
968 p = dst->lookup(pid);
969 if (!p)
971 p = new Package(pid);
972 dst->insert(p);
973 p->parent = parent;
974 ((ScopeDsymbol *)p)->symtab = new DsymbolTable();
976 else
978 assert(p->isPackage());
979 if (p->isModule())
980 { p->error("module and package have the same name");
981 fatal();
982 break;
985 parent = p;
986 dst = ((Package *)p)->symtab;
987 if (ppkg && !*ppkg)
988 *ppkg = (Package *)p;
990 if (pparent)
992 *pparent = parent;
995 return dst;