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"
41 #include "d-dmd-gcc.h"
44 ClassDeclaration
*Module::moduleinfo
;
46 Module
*Module::rootModule
;
47 DsymbolTable
*Module::modules
;
48 Array
Module::amodules
;
50 Array
Module::deferred
; // deferred Dsymbol's needing semantic() run on them
51 unsigned Module::dprogress
;
55 modules
= new DsymbolTable();
58 Module::Module(char *filename
, Identifier
*ident
, int doDocComment
, int doHdrGen
, int isDltFile
)
59 : Package(ident
), isDltFile(isDltFile
)
61 FileName
*srcfilename
;
64 FileName
*objfilename
;
65 FileName
*symfilename
;
67 // printf("Module::Module(filename = '%s', ident = '%s')\n", filename, ident->toChars());
77 strictlyneedmoduleinfo
= 0;
80 searchCacheIdent
= NULL
;
81 searchCacheSymbol
= NULL
;
104 versionidsNot
= NULL
;
112 srcfilename
= FileName::defaultExt(filename
, global
.dlt_ext
);
113 if (!srcfilename
->equalsExt(global
.mars_ext
) &&
114 !srcfilename
->equalsExt(global
.dlt_ext
) &&
115 !srcfilename
->equalsExt("dd"))
117 if (srcfilename
->equalsExt("html") ||
118 srcfilename
->equalsExt("htm") ||
119 srcfilename
->equalsExt("xhtml"))
122 { error("source file name '%s' must have .%s extension", srcfilename
->toChars(), global
.dlt_ext
);
128 if (global
.params
.objname
)
129 argobj
= global
.params
.objname
;
130 else if (global
.params
.preservePaths
)
133 argobj
= FileName::name(filename
);
134 if (!FileName::absolute(argobj
))
136 argobj
= FileName::combine(global
.params
.objdir
, argobj
);
139 if (global
.params
.objname
)
140 objfilename
= new FileName(argobj
, 0);
142 objfilename
= FileName::forceExt(argobj
, global
.obj_ext
);
144 symfilename
= FileName::forceExt(filename
, global
.sym_ext
);
146 srcfile
= new File(srcfilename
);
158 objfile
= new File(objfilename
);
159 symfile
= new File(symfilename
);
162 void Module::setDocfile()
164 FileName
*docfilename
;
167 if (global
.params
.docname
)
168 argdoc
= global
.params
.docname
;
169 else if (global
.params
.preservePaths
)
170 argdoc
= (char *)arg
;
172 argdoc
= FileName::name((char *)arg
);
173 if (!FileName::absolute(argdoc
))
174 { //FileName::ensurePathExists(global.params.docdir);
175 argdoc
= FileName::combine(global
.params
.docdir
, argdoc
);
177 if (global
.params
.docname
)
178 docfilename
= new FileName(argdoc
, 0);
180 docfilename
= FileName::forceExt(argdoc
, global
.doc_ext
);
182 if (docfilename
->equals(srcfile
->name
))
183 { error("Source file and documentation file have same name '%s'", srcfile
->name
->str
);
187 docfile
= new File(docfilename
);
190 void Module::setHdrfile()
192 FileName
*hdrfilename
;
195 if (global
.params
.hdrname
)
196 arghdr
= global
.params
.hdrname
;
197 else if (global
.params
.preservePaths
)
198 arghdr
= (char *)arg
;
200 arghdr
= FileName::name((char *)arg
);
201 if (!FileName::absolute(arghdr
))
202 { //FileName::ensurePathExists(global.params.hdrdir);
203 arghdr
= FileName::combine(global
.params
.hdrdir
, arghdr
);
205 if (global
.params
.hdrname
)
206 hdrfilename
= new FileName(arghdr
, 0);
208 hdrfilename
= FileName::forceExt(arghdr
, global
.hdr_ext
);
210 if (hdrfilename
->equals(srcfile
->name
))
211 { error("Source file and 'header' file have same name '%s'", srcfile
->name
->str
);
215 hdrfile
= new File(hdrfilename
);
218 void Module::deleteObjFile()
220 if (global
.params
.obj
)
235 Module
*Module::load(Loc loc
, Array
*packages
, Identifier
*ident
)
238 bool dltSyntax
= false;
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 /* 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
))
282 else if (FileName::exists(sdi
))
284 else if (FileName::exists(sd
))
286 else if (FileName::absolute(filename
))
288 else if (!global
.path
)
292 for (size_t i
= 0; i
< global
.path
->dim
; i
++)
294 char *p
= (char *)global
.path
->data
[i
];
297 n
= FileName::combine(p
, sdlt
);
298 if (FileName::exists(n
))
305 n
= FileName::combine(p
, sdi
);
306 if (FileName::exists(n
))
312 n
= FileName::combine(p
, sd
);
313 if (FileName::exists(n
))
321 m
= new Module(filename
, ident
, 0, 0, dltSyntax
);
325 m
->srcfile
= new File(result
);
327 if (global
.params
.verbose
)
332 for (size_t i
= 0; i
< packages
->dim
; i
++)
333 { Identifier
*pid
= (Identifier
*)packages
->data
[i
];
334 printf("%s.", pid
->toChars());
337 printf("%s\t(%s)\n", ident
->toChars(), m
->srcfile
->toChars());
344 d_gcc_magic_module(m
);
350 void Module::read(Loc loc
)
352 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars());
354 { error(loc
, "cannot read file '%s'", srcfile
->toChars());
359 inline unsigned readwordLE(unsigned short *p
)
364 return (((unsigned char *)p
)[1] << 8) | ((unsigned char *)p
)[0];
368 inline unsigned readwordBE(unsigned short *p
)
370 return (((unsigned char *)p
)[0] << 8) | ((unsigned char *)p
)[1];
373 inline unsigned readlongLE(unsigned *p
)
378 return ((unsigned char *)p
)[0] |
379 (((unsigned char *)p
)[1] << 8) |
380 (((unsigned char *)p
)[2] << 16) |
381 (((unsigned char *)p
)[3] << 24);
385 inline unsigned readlongBE(unsigned *p
)
387 return ((unsigned char *)p
)[3] |
388 (((unsigned char *)p
)[2] << 8) |
389 (((unsigned char *)p
)[1] << 16) |
390 (((unsigned char *)p
)[0] << 24);
394 void Module::parse(bool dump_source
)
404 //printf("Module::parse()\n");
406 srcname
= srcfile
->name
->toChars();
407 //printf("Module::parse(srcname = '%s')\n", srcname);
409 buf
= srcfile
->buffer
;
410 buflen
= srcfile
->len
;
414 /* Convert all non-UTF-8 formats to UTF-8.
415 * BOM : http://www.unicode.org/faq/utf_bom.html
416 * 00 00 FE FF UTF-32BE, big-endian
417 * FF FE 00 00 UTF-32LE, little-endian
418 * FE FF UTF-16BE, big-endian
419 * FF FE UTF-16LE, little-endian
423 bom
= 1; // assume there's a BOM
424 if (buf
[0] == 0xFF && buf
[1] == 0xFE)
426 if (buflen
>= 4 && buf
[2] == 0 && buf
[3] == 0)
432 unsigned *pu
= (unsigned *)(buf
);
433 unsigned *pumax
= &pu
[buflen
/ 4];
436 { error("odd length of UTF-32 char source %u", buflen
);
440 dbuf
.reserve(buflen
/ 4);
441 for (pu
+= bom
; pu
< pumax
; pu
++)
444 u
= le
? readlongLE(pu
) : readlongBE(pu
);
448 { error("UTF-32 value %08x greater than 0x10FFFF", u
);
456 dbuf
.writeByte(0); // add 0 as sentinel for scanner
457 buflen
= dbuf
.offset
- 1; // don't include sentinel in count
458 buf
= (unsigned char *) dbuf
.extractData();
462 // Convert it to UTF-8
467 unsigned short *pu
= (unsigned short *)(buf
);
468 unsigned short *pumax
= &pu
[buflen
/ 2];
471 { error("odd length of UTF-16 char source %u", buflen
);
475 dbuf
.reserve(buflen
/ 2);
476 for (pu
+= bom
; pu
< pumax
; pu
++)
479 u
= le
? readwordLE(pu
) : readwordBE(pu
);
481 { if (u
>= 0xD800 && u
<= 0xDBFF)
485 { error("surrogate UTF-16 high value %04x at EOF", u
);
488 u2
= le
? readwordLE(pu
) : readwordBE(pu
);
489 if (u2
< 0xDC00 || u2
> 0xDFFF)
490 { error("surrogate UTF-16 low value %04x out of range", u2
);
493 u
= (u
- 0xD7C0) << 10;
496 else if (u
>= 0xDC00 && u
<= 0xDFFF)
497 { error("unpaired surrogate UTF-16 value %04x", u
);
500 else if (u
== 0xFFFE || u
== 0xFFFF)
501 { error("illegal UTF-16 value %04x", u
);
509 dbuf
.writeByte(0); // add 0 as sentinel for scanner
510 buflen
= dbuf
.offset
- 1; // don't include sentinel in count
511 buf
= (unsigned char *) dbuf
.extractData();
514 else if (buf
[0] == 0xFE && buf
[1] == 0xFF)
519 else if (buflen
>= 4 && buf
[0] == 0 && buf
[1] == 0 && buf
[2] == 0xFE && buf
[3] == 0xFF)
524 else if (buflen
>= 3 && buf
[0] == 0xEF && buf
[1] == 0xBB && buf
[2] == 0xBF)
532 /* There is no BOM. Make use of Arcane Jill's insight that
533 * the first char of D source must be ASCII to
534 * figure out the encoding.
539 { if (buf
[1] == 0 && buf
[2] == 0 && buf
[3] == 0)
544 else if (buf
[0] == 0 && buf
[1] == 0 && buf
[2] == 0)
557 else if (buf
[0] == 0)
566 { error("source file must start with BOM or ASCII character, not \\x%02X", buf
[0]);
573 // dump utf-8 encoded source
575 { // %% srcname could contain a path ...
576 d_gcc_dump_source(srcname
, "utf-8", buf
, buflen
);
580 /* If it starts with the string "Ddoc", then it's a documentation
583 if (buflen
>= 4 && memcmp(buf
, "Ddoc", 4) == 0)
593 OutBuffer
*dbuf
= new OutBuffer();
594 Html
h(srcname
, buf
, buflen
);
597 buflen
= dbuf
->offset
;
599 // dump extracted source
601 d_gcc_dump_source(srcname
, "d.utf-8", buf
, buflen
);
607 DltParser
p(this, buf
, buflen
, docfile
!= NULL
);
609 members
= p
.parseModule();
611 numlines
= p
.loc
.linnum
;
615 Parser
p(this, buf
, buflen
, docfile
!= NULL
);
617 members
= p
.parseModule();
619 numlines
= p
.loc
.linnum
;
625 { this->ident
= md
->id
;
626 dst
= Package::resolve(md
->packages
, &this->parent
, NULL
);
632 /* Check to see if module name is a valid identifier
634 if (!Lexer::isValidIdentifier(this->ident
->toChars()))
635 error("has non-identifier characters in filename, use module declaration instead");
638 // Update global list of modules
639 if (!dst
->insert(this))
642 error(loc
, "is in multiple packages %s", md
->toChars());
644 error(loc
, "is in multiple defined");
652 static FuncDeclaration
*addMainFunction(Dsymbol
*mainClass
) {
653 ClassDeclaration
*klass
= mainClass
->isClassDeclaration();
655 // Just ignore it then?
656 error("'Main' is not a class!");
660 Loc loc
= mainClass
->loc
;
662 // Create a main(string[] args) function
663 Arguments
*arguments
= new Arguments();
665 Type
*tstr
= new TypeDArray(Type::tchar
);
666 Type
*targv
= new TypeDArray(tstr
);
668 Argument
*a
= new Argument(STCin
, targv
, Id::args
, NULL
);
671 TypeFunction
*ta
= new TypeFunction(arguments
, new Type(Tvoid
, NULL
), 0, LINKc
);
672 FuncDeclaration
*f
= new FuncDeclaration(loc
, 0, Id::main
, STCundefined
, ta
);
674 f
->fbody
= new InjectorMainBody(loc
, klass
);
679 void Module::semantic()
685 Array
*dlt_package
= new Array();
686 dlt_package
->push(Id::dlt
);
688 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
691 // Note that modules get their own scope, from scratch.
692 // This is so regardless of where in the syntax a module
693 // gets imported, it is unaffected by context.
694 Scope
*sc
= Scope::createGlobal(this); // create root scope
696 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);
699 for (i
= 0; i
< members
->dim
; i
++)
702 s
= (Dsymbol
*)members
->data
[i
];
704 if (s
->ident
&& s
->ident
== Id::Main
) {
705 Dsymbol
*mainFn
= addMainFunction(s
);
708 members
->push(mainFn
);
710 Import
*im
= new Import(0, dlt_package
, Id::_externals
, NULL
, true);
717 // Add import of "object" if this module isn't "object"
718 if (ident
!= Id::object
|| parent
)
720 Import
*im
= new Import(0, NULL
, Id::object
, NULL
, 0);
723 /* If this is not dlt.core ... */
724 if (ident
!= Id::core
|| parent
== NULL
|| parent
->parent
|| parent
->ident
!= Id::dlt
) {
725 Import
*im
= new Import(0, dlt_package
, Id::core
, NULL
, 0);
732 /* va_list is a pain. If va_list involves a struct, add the
733 struct declaration to the "object" module. This depends on
734 the GCC backend not naming the struct something that will
735 cause a conflict or define "va_list" without going through
737 Type
* t
= d_gcc_builtin_va_list_d_type
;
739 if (t
->ty
== Tstruct
) {
740 StructDeclaration
* sd
= ((TypeStruct
*) t
)->sym
;
750 // Add all symbols into module's symbol table
751 symtab
= new DsymbolTable();
752 for (i
= 0; i
< members
->dim
; i
++)
755 s
= (Dsymbol
*)members
->data
[i
];
756 s
->addMember(NULL
, sc
->scopesym
, 1);
759 // Pass 1 semantic routines: do public side of the definition
760 for (i
= 0; i
< members
->dim
; i
++)
763 s
= (Dsymbol
*)members
->data
[i
];
764 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
766 runDeferredSemantic();
771 semanticdone
= semanticstarted
;
772 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
775 void Module::semantic2()
780 for (int i
= 0; i
< deferred
.dim
; i
++)
782 Dsymbol
*sd
= (Dsymbol
*)deferred
.data
[i
];
784 sd
->error("unable to resolve forward reference in definition");
788 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
789 if (semanticstarted
>= 2)
791 assert(semanticstarted
== 1);
794 // Note that modules get their own scope, from scratch.
795 // This is so regardless of where in the syntax a module
796 // gets imported, it is unaffected by context.
797 Scope
*sc
= Scope::createGlobal(this); // create root scope
798 //printf("Module = %p\n", sc.scopesym);
800 // Pass 2 semantic routines: do initializers and function bodies
801 for (i
= 0; i
< members
->dim
; i
++)
804 s
= (Dsymbol
*)members
->data
[i
];
810 semanticdone
= semanticstarted
;
811 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
814 void Module::semantic3()
817 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
818 if (semanticstarted
>= 3)
820 assert(semanticstarted
== 2);
823 // Note that modules get their own scope, from scratch.
824 // This is so regardless of where in the syntax a module
825 // gets imported, it is unaffected by context.
826 Scope
*sc
= Scope::createGlobal(this); // create root scope
827 //printf("Module = %p\n", sc.scopesym);
829 // Pass 3 semantic routines: do initializers and function bodies
830 for (i
= 0; i
< members
->dim
; i
++)
833 s
= (Dsymbol
*)members
->data
[i
];
834 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
840 semanticdone
= semanticstarted
;
843 void Module::inlineScan()
846 if (semanticstarted
>= 4)
848 assert(semanticstarted
== 3);
851 // Note that modules get their own scope, from scratch.
852 // This is so regardless of where in the syntax a module
853 // gets imported, it is unaffected by context.
854 //printf("Module = %p\n", sc.scopesym);
856 for (i
= 0; i
< members
->dim
; i
++)
859 s
= (Dsymbol
*)members
->data
[i
];
860 //if (global.params.verbose)
861 //printf("inline scan symbol %s\n", s->toChars());
865 semanticdone
= semanticstarted
;
868 /****************************************************
871 void Module::gensymfile()
876 //printf("Module::gensymfile()\n");
878 buf
.printf("// Sym file generated from '%s'", srcfile
->toChars());
881 for (int i
= 0; i
< members
->dim
; i
++)
882 { Dsymbol
*s
= (Dsymbol
*)members
->data
[i
];
884 s
->toCBuffer(&buf
, &hgs
);
887 // Transfer image to file
888 symfile
->setbuffer(buf
.data
, buf
.offset
);
894 /**********************************
895 * Determine if we need to generate an instance of ModuleInfo
899 int Module::needModuleInfo()
901 return needmoduleinfo
|| global
.params
.cov
;
904 Dsymbol
*Module::search(Loc loc
, Identifier
*ident
, int flags
)
906 /* Since modules can be circularly referenced,
907 * need to stop infinite recursive searches.
910 //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch);
914 else if (searchCacheIdent
== ident
&& searchCacheFlags
== flags
)
915 s
= searchCacheSymbol
;
919 s
= ScopeDsymbol::search(loc
, ident
, flags
);
922 searchCacheIdent
= ident
;
923 searchCacheSymbol
= s
;
924 searchCacheFlags
= flags
;
929 /*******************************************
930 * Can't run semantic on s now, try again later.
933 void Module::addDeferredSemantic(Dsymbol
*s
)
935 // Don't add it if it is already there
936 for (int i
= 0; i
< deferred
.dim
; i
++)
938 Dsymbol
*sd
= (Dsymbol
*)deferred
.data
[i
];
944 //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
949 /******************************************
950 * Run semantic() on deferred symbols.
953 void Module::runDeferredSemantic()
960 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
978 todo
= (Dsymbol
**)alloca(len
* sizeof(Dsymbol
*));
981 memcpy(todo
, deferred
.data
, len
* sizeof(Dsymbol
*));
984 for (int i
= 0; i
< len
; i
++)
986 Dsymbol
*s
= todo
[i
];
989 //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars());
991 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
992 } while (deferred
.dim
< len
|| dprogress
); // while making progress
994 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
997 /* =========================== ModuleDeclaration ===================== */
999 ModuleDeclaration::ModuleDeclaration(Array
*packages
, Identifier
*id
)
1001 this->packages
= packages
;
1005 char *ModuleDeclaration::toChars()
1010 if (packages
&& packages
->dim
)
1012 for (i
= 0; i
< packages
->dim
; i
++)
1013 { Identifier
*pid
= (Identifier
*)packages
->data
[i
];
1015 buf
.writestring(pid
->toChars());
1019 buf
.writestring(id
->toChars());
1021 return (char *)buf
.extractData();
1024 /* =========================== Package ===================== */
1026 Package::Package(Identifier
*ident
)
1027 : ScopeDsymbol(ident
)
1032 char *Package::kind()
1038 DsymbolTable
*Package::resolve(Array
*packages
, Dsymbol
**pparent
, Package
**ppkg
)
1040 DsymbolTable
*dst
= Module::modules
;
1041 Dsymbol
*parent
= NULL
;
1043 //printf("Package::resolve()\n");
1050 for (i
= 0; i
< packages
->dim
; i
++)
1051 { Identifier
*pid
= (Identifier
*)packages
->data
[i
];
1054 p
= dst
->lookup(pid
);
1057 p
= new Package(pid
);
1060 ((ScopeDsymbol
*)p
)->symtab
= new DsymbolTable();
1064 assert(p
->isPackage());
1066 { p
->error("module and package have the same name");
1072 dst
= ((Package
*)p
)->symtab
;
1074 *ppkg
= (Package
*)p
;