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
.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());
123 { error("source file name '%s' must have .%s extension", srcfilename
->toChars(), global
.mars_ext
);
129 if (global
.params
.objname
)
130 argobj
= global
.params
.objname
;
131 else if (global
.params
.preservePaths
)
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);
143 objfilename
= FileName::forceExt(argobj
, global
.obj_ext
);
145 symfilename
= FileName::forceExt(filename
, global
.sym_ext
);
147 srcfile
= new File(srcfilename
);
159 objfile
= new File(objfilename
);
160 symfile
= new File(symfilename
);
163 void Module::setDocfile()
165 FileName
*docfilename
;
168 if (global
.params
.docname
)
169 argdoc
= global
.params
.docname
;
170 else if (global
.params
.preservePaths
)
171 argdoc
= (char *)arg
;
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);
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
);
188 docfile
= new File(docfilename
);
191 void Module::setHdrfile()
193 FileName
*hdrfilename
;
196 if (global
.params
.hdrname
)
197 arghdr
= global
.params
.hdrname
;
198 else if (global
.params
.preservePaths
)
199 arghdr
= (char *)arg
;
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);
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
);
216 hdrfile
= new File(hdrfilename
);
219 void Module::deleteObjFile()
221 if (global
.params
.obj
)
236 Module
*Module::load(Loc loc
, Array
*packages
, Identifier
*ident
)
240 //printf("Module::load(ident = '%s')\n", ident->toChars());
242 // Build module filename by turning:
246 filename
= ident
->toChars();
247 if (packages
&& packages
->dim
)
252 for (i
= 0; i
< packages
->dim
; i
++)
253 { Identifier
*pid
= (Identifier
*)packages
->data
[i
];
255 buf
.writestring(pid
->toChars());
262 buf
.writestring(filename
);
264 filename
= (char *)buf
.extractData();
267 m
= new Module(filename
, ident
, 0, 0);
270 /* Search along global.path for .di file, then .d file.
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
))
280 else if (FileName::exists(sd
))
282 else if (FileName::absolute(filename
))
284 else if (!global
.path
)
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
))
297 n
= FileName::combine(p
, sd
);
298 if (FileName::exists(n
))
306 m
->srcfile
= new File(result
);
308 if (global
.params
.verbose
)
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());
325 d_gcc_magic_module(m
);
331 void Module::read(Loc loc
)
333 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars());
335 { error(loc
, "cannot read file '%s'", srcfile
->toChars());
340 inline unsigned readwordLE(unsigned short *p
)
345 return (((unsigned char *)p
)[1] << 8) | ((unsigned char *)p
)[0];
349 inline unsigned readwordBE(unsigned short *p
)
351 return (((unsigned char *)p
)[0] << 8) | ((unsigned char *)p
)[1];
354 inline unsigned readlongLE(unsigned *p
)
359 return ((unsigned char *)p
)[0] |
360 (((unsigned char *)p
)[1] << 8) |
361 (((unsigned char *)p
)[2] << 16) |
362 (((unsigned char *)p
)[3] << 24);
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);
375 void Module::parse(bool dump_source
)
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
;
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
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)
413 unsigned *pu
= (unsigned *)(buf
);
414 unsigned *pumax
= &pu
[buflen
/ 4];
417 { error("odd length of UTF-32 char source %u", buflen
);
421 dbuf
.reserve(buflen
/ 4);
422 for (pu
+= bom
; pu
< pumax
; pu
++)
425 u
= le
? readlongLE(pu
) : readlongBE(pu
);
429 { error("UTF-32 value %08x greater than 0x10FFFF", 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();
443 // Convert it to UTF-8
448 unsigned short *pu
= (unsigned short *)(buf
);
449 unsigned short *pumax
= &pu
[buflen
/ 2];
452 { error("odd length of UTF-16 char source %u", buflen
);
456 dbuf
.reserve(buflen
/ 2);
457 for (pu
+= bom
; pu
< pumax
; pu
++)
460 u
= le
? readwordLE(pu
) : readwordBE(pu
);
462 { if (u
>= 0xD800 && u
<= 0xDBFF)
466 { error("surrogate UTF-16 high value %04x at EOF", u
);
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
);
474 u
= (u
- 0xD7C0) << 10;
477 else if (u
>= 0xDC00 && u
<= 0xDFFF)
478 { error("unpaired surrogate UTF-16 value %04x", u
);
481 else if (u
== 0xFFFE || u
== 0xFFFF)
482 { error("illegal UTF-16 value %04x", 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)
500 else if (buflen
>= 4 && buf
[0] == 0 && buf
[1] == 0 && buf
[2] == 0xFE && buf
[3] == 0xFF)
505 else if (buflen
>= 3 && buf
[0] == 0xEF && buf
[1] == 0xBB && buf
[2] == 0xBF)
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.
520 { if (buf
[1] == 0 && buf
[2] == 0 && buf
[3] == 0)
525 else if (buf
[0] == 0 && buf
[1] == 0 && buf
[2] == 0)
538 else if (buf
[0] == 0)
547 { error("source file must start with BOM or ASCII character, not \\x%02X", buf
[0]);
554 // dump utf-8 encoded source
556 { // %% srcname could contain a path ...
557 d_gcc_dump_source(srcname
, "utf-8", buf
, buflen
);
561 /* If it starts with the string "Ddoc", then it's a documentation
564 if (buflen
>= 4 && memcmp(buf
, "Ddoc", 4) == 0)
574 OutBuffer
*dbuf
= new OutBuffer();
575 Html
h(srcname
, buf
, buflen
);
578 buflen
= dbuf
->offset
;
580 // dump extracted source
582 d_gcc_dump_source(srcname
, "d.utf-8", buf
, buflen
);
585 Parser
p(this, buf
, buflen
, docfile
!= NULL
);
587 members
= p
.parseModule();
589 numlines
= p
.loc
.linnum
;
594 { this->ident
= md
->id
;
595 dst
= Package::resolve(md
->packages
, &this->parent
, NULL
);
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))
611 error(loc
, "is in multiple packages %s", md
->toChars());
613 error(loc
, "is in multiple defined");
621 void Module::semantic()
627 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
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);
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
651 Type
* t
= d_gcc_builtin_va_list_d_type
;
653 if (t
->ty
== Tstruct
) {
654 StructDeclaration
* sd
= ((TypeStruct
*) t
)->sym
;
664 // Add all symbols into module's symbol table
665 symtab
= new DsymbolTable();
666 for (i
= 0; i
< members
->dim
; i
++)
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
++)
677 s
= (Dsymbol
*)members
->data
[i
];
678 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
680 runDeferredSemantic();
685 semanticdone
= semanticstarted
;
686 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
689 void Module::semantic2()
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");
702 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
703 if (semanticstarted
>= 2)
705 assert(semanticstarted
== 1);
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
++)
718 s
= (Dsymbol
*)members
->data
[i
];
724 semanticdone
= semanticstarted
;
725 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
728 void Module::semantic3()
731 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
732 if (semanticstarted
>= 3)
734 assert(semanticstarted
== 2);
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
++)
747 s
= (Dsymbol
*)members
->data
[i
];
748 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
754 semanticdone
= semanticstarted
;
757 void Module::inlineScan()
760 if (semanticstarted
>= 4)
762 assert(semanticstarted
== 3);
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
++)
773 s
= (Dsymbol
*)members
->data
[i
];
774 //if (global.params.verbose)
775 //printf("inline scan symbol %s\n", s->toChars());
779 semanticdone
= semanticstarted
;
782 /****************************************************
785 void Module::gensymfile()
790 //printf("Module::gensymfile()\n");
792 buf
.printf("// Sym file generated from '%s'", srcfile
->toChars());
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
);
808 /**********************************
809 * Determine if we need to generate an instance of ModuleInfo
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);
828 else if (searchCacheIdent
== ident
&& searchCacheFlags
== flags
)
829 s
= searchCacheSymbol
;
833 s
= ScopeDsymbol::search(loc
, ident
, flags
);
836 searchCacheIdent
= ident
;
837 searchCacheSymbol
= s
;
838 searchCacheFlags
= flags
;
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
];
858 //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
863 /******************************************
864 * Run semantic() on deferred symbols.
867 void Module::runDeferredSemantic()
874 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
892 todo
= (Dsymbol
**)alloca(len
* sizeof(Dsymbol
*));
895 memcpy(todo
, deferred
.data
, len
* sizeof(Dsymbol
*));
898 for (int i
= 0; i
< len
; i
++)
900 Dsymbol
*s
= todo
[i
];
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
908 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
911 /* =========================== ModuleDeclaration ===================== */
913 ModuleDeclaration::ModuleDeclaration(Array
*packages
, Identifier
*id
)
915 this->packages
= packages
;
919 char *ModuleDeclaration::toChars()
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());
933 buf
.writestring(id
->toChars());
935 return (char *)buf
.extractData();
938 /* =========================== Package ===================== */
940 Package::Package(Identifier
*ident
)
941 : ScopeDsymbol(ident
)
946 char *Package::kind()
952 DsymbolTable
*Package::resolve(Array
*packages
, Dsymbol
**pparent
, Package
**ppkg
)
954 DsymbolTable
*dst
= Module::modules
;
955 Dsymbol
*parent
= NULL
;
957 //printf("Package::resolve()\n");
964 for (i
= 0; i
< packages
->dim
; i
++)
965 { Identifier
*pid
= (Identifier
*)packages
->data
[i
];
968 p
= dst
->lookup(pid
);
971 p
= new Package(pid
);
974 ((ScopeDsymbol
*)p
)->symtab
= new DsymbolTable();
978 assert(p
->isPackage());
980 { p
->error("module and package have the same name");
986 dst
= ((Package
*)p
)->symtab
;
988 *ppkg
= (Package
*)p
;