2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2007 by Digital Mars
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.
20 #include "gdc_alloca.h"
29 #include "identifier.h"
40 #include "d-dmd-gcc.h"
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
;
54 modules
= new DsymbolTable();
57 Module::Module(char *filename
, Identifier
*ident
, int doDocComment
, int doHdrGen
)
60 FileName
*srcfilename
;
63 FileName
*objfilename
;
64 FileName
*symfilename
;
66 // printf("Module::Module(filename = '%s', ident = '%s')\n", filename, ident->toChars());
76 strictlyneedmoduleinfo
= 0;
79 searchCacheIdent
= NULL
;
80 searchCacheSymbol
= NULL
;
103 versionidsNot
= 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"))
120 { error("source file name '%s' must have .%s extension", srcfilename
->toChars(), global
.dlt_ext
);
126 if (global
.params
.objname
)
127 argobj
= global
.params
.objname
;
128 else if (global
.params
.preservePaths
)
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);
140 objfilename
= FileName::forceExt(argobj
, global
.obj_ext
);
142 symfilename
= FileName::forceExt(filename
, global
.sym_ext
);
144 srcfile
= new File(srcfilename
);
156 objfile
= new File(objfilename
);
157 symfile
= new File(symfilename
);
160 void Module::setDocfile()
162 FileName
*docfilename
;
165 if (global
.params
.docname
)
166 argdoc
= global
.params
.docname
;
167 else if (global
.params
.preservePaths
)
168 argdoc
= (char *)arg
;
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);
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
);
185 docfile
= new File(docfilename
);
188 void Module::setHdrfile()
190 FileName
*hdrfilename
;
193 if (global
.params
.hdrname
)
194 arghdr
= global
.params
.hdrname
;
195 else if (global
.params
.preservePaths
)
196 arghdr
= (char *)arg
;
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);
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
);
213 hdrfile
= new File(hdrfilename
);
216 void Module::deleteObjFile()
218 if (global
.params
.obj
)
233 Module
*Module::load(Loc loc
, Array
*packages
, Identifier
*ident
)
237 //printf("Module::load(ident = '%s')\n", ident->toChars());
239 // Build module filename by turning:
243 filename
= ident
->toChars();
244 if (packages
&& packages
->dim
)
249 for (i
= 0; i
< packages
->dim
; i
++)
250 { Identifier
*pid
= (Identifier
*)packages
->data
[i
];
252 buf
.writestring(pid
->toChars());
259 buf
.writestring(filename
);
261 filename
= (char *)buf
.extractData();
264 m
= new Module(filename
, ident
, 0, 0);
267 /* Search along global.path for a .dlt file, then a .di file, then a .d file.
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 else if (FileName::exists(sdi
))
281 else if (FileName::exists(sd
))
283 else if (FileName::absolute(filename
))
285 else if (!global
.path
)
289 for (size_t i
= 0; i
< global
.path
->dim
; i
++)
291 char *p
= (char *)global
.path
->data
[i
];
294 n
= FileName::combine(p
, sdlt
);
295 if (FileName::exists(n
))
301 n
= FileName::combine(p
, sdi
);
302 if (FileName::exists(n
))
308 n
= FileName::combine(p
, sd
);
309 if (FileName::exists(n
))
317 m
->srcfile
= new File(result
);
319 if (global
.params
.verbose
)
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());
336 d_gcc_magic_module(m
);
342 void Module::read(Loc loc
)
344 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars());
346 { error(loc
, "cannot read file '%s'", srcfile
->toChars());
351 inline unsigned readwordLE(unsigned short *p
)
356 return (((unsigned char *)p
)[1] << 8) | ((unsigned char *)p
)[0];
360 inline unsigned readwordBE(unsigned short *p
)
362 return (((unsigned char *)p
)[0] << 8) | ((unsigned char *)p
)[1];
365 inline unsigned readlongLE(unsigned *p
)
370 return ((unsigned char *)p
)[0] |
371 (((unsigned char *)p
)[1] << 8) |
372 (((unsigned char *)p
)[2] << 16) |
373 (((unsigned char *)p
)[3] << 24);
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);
386 void Module::parse(bool dump_source
)
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
;
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
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)
424 unsigned *pu
= (unsigned *)(buf
);
425 unsigned *pumax
= &pu
[buflen
/ 4];
428 { error("odd length of UTF-32 char source %u", buflen
);
432 dbuf
.reserve(buflen
/ 4);
433 for (pu
+= bom
; pu
< pumax
; pu
++)
436 u
= le
? readlongLE(pu
) : readlongBE(pu
);
440 { error("UTF-32 value %08x greater than 0x10FFFF", 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();
454 // Convert it to UTF-8
459 unsigned short *pu
= (unsigned short *)(buf
);
460 unsigned short *pumax
= &pu
[buflen
/ 2];
463 { error("odd length of UTF-16 char source %u", buflen
);
467 dbuf
.reserve(buflen
/ 2);
468 for (pu
+= bom
; pu
< pumax
; pu
++)
471 u
= le
? readwordLE(pu
) : readwordBE(pu
);
473 { if (u
>= 0xD800 && u
<= 0xDBFF)
477 { error("surrogate UTF-16 high value %04x at EOF", u
);
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
);
485 u
= (u
- 0xD7C0) << 10;
488 else if (u
>= 0xDC00 && u
<= 0xDFFF)
489 { error("unpaired surrogate UTF-16 value %04x", u
);
492 else if (u
== 0xFFFE || u
== 0xFFFF)
493 { error("illegal UTF-16 value %04x", 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)
511 else if (buflen
>= 4 && buf
[0] == 0 && buf
[1] == 0 && buf
[2] == 0xFE && buf
[3] == 0xFF)
516 else if (buflen
>= 3 && buf
[0] == 0xEF && buf
[1] == 0xBB && buf
[2] == 0xBF)
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.
531 { if (buf
[1] == 0 && buf
[2] == 0 && buf
[3] == 0)
536 else if (buf
[0] == 0 && buf
[1] == 0 && buf
[2] == 0)
549 else if (buf
[0] == 0)
558 { error("source file must start with BOM or ASCII character, not \\x%02X", buf
[0]);
565 // dump utf-8 encoded source
567 { // %% srcname could contain a path ...
568 d_gcc_dump_source(srcname
, "utf-8", buf
, buflen
);
572 /* If it starts with the string "Ddoc", then it's a documentation
575 if (buflen
>= 4 && memcmp(buf
, "Ddoc", 4) == 0)
585 OutBuffer
*dbuf
= new OutBuffer();
586 Html
h(srcname
, buf
, buflen
);
589 buflen
= dbuf
->offset
;
591 // dump extracted source
593 d_gcc_dump_source(srcname
, "d.utf-8", buf
, buflen
);
596 Parser
p(this, buf
, buflen
, docfile
!= NULL
);
598 members
= p
.parseModule();
600 numlines
= p
.loc
.linnum
;
605 { this->ident
= md
->id
;
606 dst
= Package::resolve(md
->packages
, &this->parent
, NULL
);
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))
622 error(loc
, "is in multiple packages %s", md
->toChars());
624 error(loc
, "is in multiple defined");
632 void Module::semantic()
638 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
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);
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
662 Type
* t
= d_gcc_builtin_va_list_d_type
;
664 if (t
->ty
== Tstruct
) {
665 StructDeclaration
* sd
= ((TypeStruct
*) t
)->sym
;
675 // Add all symbols into module's symbol table
676 symtab
= new DsymbolTable();
677 for (i
= 0; i
< members
->dim
; i
++)
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
++)
688 s
= (Dsymbol
*)members
->data
[i
];
689 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
691 runDeferredSemantic();
696 semanticdone
= semanticstarted
;
697 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
700 void Module::semantic2()
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");
713 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
714 if (semanticstarted
>= 2)
716 assert(semanticstarted
== 1);
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
++)
729 s
= (Dsymbol
*)members
->data
[i
];
735 semanticdone
= semanticstarted
;
736 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
739 void Module::semantic3()
742 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
743 if (semanticstarted
>= 3)
745 assert(semanticstarted
== 2);
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
++)
758 s
= (Dsymbol
*)members
->data
[i
];
759 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
765 semanticdone
= semanticstarted
;
768 void Module::inlineScan()
771 if (semanticstarted
>= 4)
773 assert(semanticstarted
== 3);
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
++)
784 s
= (Dsymbol
*)members
->data
[i
];
785 //if (global.params.verbose)
786 //printf("inline scan symbol %s\n", s->toChars());
790 semanticdone
= semanticstarted
;
793 /****************************************************
796 void Module::gensymfile()
801 //printf("Module::gensymfile()\n");
803 buf
.printf("// Sym file generated from '%s'", srcfile
->toChars());
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
);
819 /**********************************
820 * Determine if we need to generate an instance of ModuleInfo
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);
839 else if (searchCacheIdent
== ident
&& searchCacheFlags
== flags
)
840 s
= searchCacheSymbol
;
844 s
= ScopeDsymbol::search(loc
, ident
, flags
);
847 searchCacheIdent
= ident
;
848 searchCacheSymbol
= s
;
849 searchCacheFlags
= flags
;
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
];
869 //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
874 /******************************************
875 * Run semantic() on deferred symbols.
878 void Module::runDeferredSemantic()
885 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
903 todo
= (Dsymbol
**)alloca(len
* sizeof(Dsymbol
*));
906 memcpy(todo
, deferred
.data
, len
* sizeof(Dsymbol
*));
909 for (int i
= 0; i
< len
; i
++)
911 Dsymbol
*s
= todo
[i
];
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
919 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
922 /* =========================== ModuleDeclaration ===================== */
924 ModuleDeclaration::ModuleDeclaration(Array
*packages
, Identifier
*id
)
926 this->packages
= packages
;
930 char *ModuleDeclaration::toChars()
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());
944 buf
.writestring(id
->toChars());
946 return (char *)buf
.extractData();
949 /* =========================== Package ===================== */
951 Package::Package(Identifier
*ident
)
952 : ScopeDsymbol(ident
)
957 char *Package::kind()
963 DsymbolTable
*Package::resolve(Array
*packages
, Dsymbol
**pparent
, Package
**ppkg
)
965 DsymbolTable
*dst
= Module::modules
;
966 Dsymbol
*parent
= NULL
;
968 //printf("Package::resolve()\n");
975 for (i
= 0; i
< packages
->dim
; i
++)
976 { Identifier
*pid
= (Identifier
*)packages
->data
[i
];
979 p
= dst
->lookup(pid
);
982 p
= new Package(pid
);
985 ((ScopeDsymbol
*)p
)->symtab
= new DsymbolTable();
989 assert(p
->isPackage());
991 { p
->error("module and package have the same name");
997 dst
= ((Package
*)p
)->symtab
;
999 *ppkg
= (Package
*)p
;