Mercurial > projects > ldc
annotate dmd/module.c @ 1578:1dee66f6ec0b
Removed a chunk of code in favour of a shorter and more portable method
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Tue, 08 Sep 2009 11:21:30 +0100 |
parents | 68dea5bae9e9 |
children | def7a1d494fd |
rev | line source |
---|---|
336 | 1 |
2 // Compiler implementation of the D programming language | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
3 // Copyright (c) 1999-2009 by Digital Mars |
336 | 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. | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <assert.h> | |
14 | |
872
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
751
diff
changeset
|
15 #if (defined (__SVR4) && defined (__sun)) |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
751
diff
changeset
|
16 #include <alloca.h> |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
751
diff
changeset
|
17 #endif |
aa953cc960b6
Apply BlueZeniX's patch for OpenSolaris compatibility. Fixes #158.
Christian Kamm <kamm incasoftware de>
parents:
751
diff
changeset
|
18 |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
19 #if defined(_MSC_VER) || defined(__MINGW32__) |
336 | 20 #include <malloc.h> |
21 #endif | |
22 | |
23 #if IN_GCC | |
24 #include "gdc_alloca.h" | |
25 #endif | |
26 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
27 #include "rmem.h" |
336 | 28 |
29 #include "mars.h" | |
30 #include "module.h" | |
31 #include "parse.h" | |
32 #include "scope.h" | |
33 #include "identifier.h" | |
34 #include "id.h" | |
35 #include "import.h" | |
36 #include "dsymbol.h" | |
37 #include "hdrgen.h" | |
38 #include "lexer.h" | |
39 | |
40 #define MARS 1 | |
41 #include "html.h" | |
42 | |
43 #ifdef IN_GCC | |
44 #include "d-dmd-gcc.h" | |
45 #endif | |
46 | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
47 |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
48 #include "llvm/Support/CommandLine.h" |
1564
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
49 #include <map> |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
50 |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
51 static llvm::cl::opt<bool> preservePaths("op", |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
52 llvm::cl::desc("Do not strip paths from source file"), |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
53 llvm::cl::ZeroOrMore); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
54 |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
55 static llvm::cl::opt<bool> fqnNames("oq", |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
56 llvm::cl::desc("Write object files with fully qualified names"), |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
57 llvm::cl::ZeroOrMore); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
58 |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
59 |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
60 |
336 | 61 ClassDeclaration *Module::moduleinfo; |
62 | |
63 Module *Module::rootModule; | |
64 DsymbolTable *Module::modules; | |
65 Array Module::amodules; | |
66 | |
67 Array Module::deferred; // deferred Dsymbol's needing semantic() run on them | |
68 unsigned Module::dprogress; | |
69 | |
70 void Module::init() | |
71 { | |
72 modules = new DsymbolTable(); | |
73 } | |
74 | |
75 Module::Module(char *filename, Identifier *ident, int doDocComment, int doHdrGen) | |
76 : Package(ident) | |
77 { | |
78 FileName *srcfilename; | |
79 | |
80 // printf("Module::Module(filename = '%s', ident = '%s')\n", filename, ident->toChars()); | |
81 this->arg = filename; | |
82 md = NULL; | |
83 errors = 0; | |
84 numlines = 0; | |
85 members = NULL; | |
86 isHtml = 0; | |
87 isDocFile = 0; | |
88 needmoduleinfo = 0; | |
89 #ifdef IN_GCC | |
90 strictlyneedmoduleinfo = 0; | |
91 #endif | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
92 selfimports = 0; |
336 | 93 insearch = 0; |
94 searchCacheIdent = NULL; | |
95 searchCacheSymbol = NULL; | |
96 searchCacheFlags = 0; | |
97 semanticstarted = 0; | |
98 semanticdone = 0; | |
99 decldefs = NULL; | |
100 vmoduleinfo = NULL; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
101 #if IN_DMD |
336 | 102 massert = NULL; |
103 marray = NULL; | |
104 sictor = NULL; | |
105 sctor = NULL; | |
106 sdtor = NULL; | |
107 stest = NULL; | |
108 sfilename = NULL; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
109 #endif |
336 | 110 root = 0; |
111 importedFrom = NULL; | |
112 srcfile = NULL; | |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
113 objfile = NULL; |
336 | 114 docfile = NULL; |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
115 hdrfile = NULL; |
336 | 116 |
117 debuglevel = 0; | |
118 debugids = NULL; | |
119 debugidsNot = NULL; | |
120 versionlevel = 0; | |
121 versionids = NULL; | |
122 versionidsNot = NULL; | |
123 | |
124 macrotable = NULL; | |
125 escapetable = NULL; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
126 #if IN_DMD |
336 | 127 doppelganger = 0; |
128 cov = NULL; | |
129 covb = NULL; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
130 #endif |
336 | 131 |
132 srcfilename = FileName::defaultExt(filename, global.mars_ext); | |
133 if (!srcfilename->equalsExt(global.mars_ext) && | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
134 !srcfilename->equalsExt(global.hdr_ext) && |
336 | 135 !srcfilename->equalsExt("dd")) |
136 { | |
137 if (srcfilename->equalsExt("html") || | |
138 srcfilename->equalsExt("htm") || | |
139 srcfilename->equalsExt("xhtml")) | |
140 isHtml = 1; | |
141 else | |
142 { error("source file name '%s' must have .%s extension", srcfilename->toChars(), global.mars_ext); | |
143 fatal(); | |
144 } | |
145 } | |
146 srcfile = new File(srcfilename); | |
147 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
643
diff
changeset
|
148 // LDC |
486
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
149 llvmForceLogging = false; |
604
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
150 this->doDocComment = doDocComment; |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
151 this->doHdrGen = doHdrGen; |
336 | 152 } |
153 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
154 File* Module::buildFilePath(const char* forcename, const char* path, const char* ext) |
336 | 155 { |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
156 const char *argobj; |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
157 if (forcename) |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
158 argobj = forcename; |
336 | 159 else |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
160 { |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
161 if (preservePaths) |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
162 argobj = (char*)this->arg; |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
163 else |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
164 argobj = FileName::name((char*)this->arg); |
336 | 165 |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
957
diff
changeset
|
166 if (fqnNames) |
643 | 167 { |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
168 if(md) |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
169 argobj = FileName::replaceName((char*)argobj, md->toChars()); |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
170 else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
171 argobj = FileName::replaceName((char*)argobj, toChars()); |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
172 |
643 | 173 // add ext, otherwise forceExt will make nested.module into nested.bc |
174 size_t len = strlen(argobj); | |
175 size_t extlen = strlen(ext); | |
176 char* s = (char *)alloca(len + 1 + extlen + 1); | |
177 memcpy(s, argobj, len); | |
178 s[len] = '.'; | |
179 memcpy(s + len + 1, ext, extlen + 1); | |
180 s[len+1+extlen] = 0; | |
181 argobj = s; | |
182 } | |
336 | 183 } |
184 | |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
185 if (!FileName::absolute(argobj)) |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
186 { |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
187 argobj = FileName::combine(path, argobj); |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
188 } |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
189 |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
190 FileName::ensurePathExists(FileName::path(argobj)); |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
191 |
708
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
192 // always append the extension! otherwise hard to make output switches consistent |
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
193 // if (forcename) |
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
194 // return new File(argobj); |
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
195 // else |
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
196 // allow for .o and .obj on windows |
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
197 #if _WIN32 |
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
198 if (ext == global.params.objdir && FileName::ext(argobj) |
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
199 && stricmp(FileName::ext(argobj), global.obj_ext_alt) == 0) |
1257
7af860e4f403
Changes for mingw to compile properly
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
1195
diff
changeset
|
200 return new File((char*)argobj); |
708
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
201 #endif |
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
675
diff
changeset
|
202 return new File(FileName::forceExt(argobj, ext)); |
336 | 203 } |
204 | |
1564
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
205 // LDC |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
206 static void check_and_add_output_file(Module* NewMod, const std::string& str) |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
207 { |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
208 typedef std::map<std::string, Module*> map_t; |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
209 static map_t files; |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
210 |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
211 map_t::iterator i = files.find(str); |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
212 if (i != files.end()) |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
213 { |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
214 Module* ThisMod = i->second; |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
215 error("Output file '%s' for module '%s' collides with previous module '%s'. See the -oq option", |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
216 str.c_str(), NewMod->toPrettyChars(), ThisMod->toPrettyChars()); |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
217 fatal(); |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
218 } |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
219 files.insert(std::make_pair(str, NewMod)); |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
220 } |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
221 |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
222 void Module::buildTargetFiles() |
336 | 223 { |
604
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
224 if(objfile && |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
225 (!doDocComment || docfile) && |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
226 (!doHdrGen || hdrfile)) |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
227 return; |
336 | 228 |
604
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
229 if(!objfile) |
675
bfe5229f9d8e
Disable bc output by default. Remove -dis. Add -output-bc, -output-ll, -output-s.
Christian Kamm <kamm incasoftware de>
parents:
664
diff
changeset
|
230 objfile = Module::buildFilePath(global.params.objname, global.params.objdir, global.obj_ext); |
604
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
231 if(doDocComment && !docfile) |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
232 docfile = Module::buildFilePath(global.params.docname, global.params.docdir, global.doc_ext); |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
233 if(doHdrGen && !hdrfile) |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
234 hdrfile = Module::buildFilePath(global.params.hdrname, global.params.hdrdir, global.hdr_ext); |
598
13ff06605226
To prevert source-overwriting in the future, forbit output files with the same
Christian Kamm <kamm incasoftware de>
parents:
597
diff
changeset
|
235 |
13ff06605226
To prevert source-overwriting in the future, forbit output files with the same
Christian Kamm <kamm incasoftware de>
parents:
597
diff
changeset
|
236 // safety check: never allow obj, doc or hdr file to have the source file's name |
604
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
237 if(stricmp(FileName::name(objfile->name->str), FileName::name((char*)this->arg)) == 0) |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
238 { |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
239 error("Output object files with the same name as the source file are forbidden"); |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
240 fatal(); |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
241 } |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
242 if(docfile && stricmp(FileName::name(docfile->name->str), FileName::name((char*)this->arg)) == 0) |
598
13ff06605226
To prevert source-overwriting in the future, forbit output files with the same
Christian Kamm <kamm incasoftware de>
parents:
597
diff
changeset
|
243 { |
604
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
244 error("Output doc files with the same name as the source file are forbidden"); |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
245 fatal(); |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
246 } |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
247 if(hdrfile && stricmp(FileName::name(hdrfile->name->str), FileName::name((char*)this->arg)) == 0) |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
248 { |
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
249 error("Output header files with the same name as the source file are forbidden"); |
598
13ff06605226
To prevert source-overwriting in the future, forbit output files with the same
Christian Kamm <kamm incasoftware de>
parents:
597
diff
changeset
|
250 fatal(); |
13ff06605226
To prevert source-overwriting in the future, forbit output files with the same
Christian Kamm <kamm incasoftware de>
parents:
597
diff
changeset
|
251 } |
1564
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
252 |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
253 // LDC |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
254 // another safety check to make sure we don't overwrite previous output files |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
255 check_and_add_output_file(this, objfile->name->str); |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
256 if (docfile) |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
257 check_and_add_output_file(this, docfile->name->str); |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
258 if (hdrfile) |
68dea5bae9e9
Added check and error in case two same named modules - with different packages and compiled at once - would overwrite each other's output files.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1257
diff
changeset
|
259 check_and_add_output_file(this, hdrfile->name->str); |
336 | 260 } |
261 | |
262 void Module::deleteObjFile() | |
263 { | |
264 if (global.params.obj) | |
265 objfile->remove(); | |
266 //if (global.params.llvmBC) | |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
267 //bcfile->remove(); |
604
a30fc28e8f23
Make creating and deleting of doc and hdr files dependent on whether doc and
Christian Kamm <kamm incasoftware de>
parents:
598
diff
changeset
|
268 if (doDocComment && docfile) |
336 | 269 docfile->remove(); |
270 } | |
271 | |
272 Module::~Module() | |
273 { | |
274 } | |
275 | |
276 const char *Module::kind() | |
277 { | |
278 return "module"; | |
279 } | |
280 | |
281 Module *Module::load(Loc loc, Array *packages, Identifier *ident) | |
282 { Module *m; | |
283 char *filename; | |
284 | |
285 //printf("Module::load(ident = '%s')\n", ident->toChars()); | |
286 | |
287 // Build module filename by turning: | |
288 // foo.bar.baz | |
289 // into: | |
290 // foo\bar\baz | |
291 filename = ident->toChars(); | |
292 if (packages && packages->dim) | |
293 { | |
294 OutBuffer buf; | |
295 int i; | |
296 | |
297 for (i = 0; i < packages->dim; i++) | |
298 { Identifier *pid = (Identifier *)packages->data[i]; | |
299 | |
300 buf.writestring(pid->toChars()); | |
301 #if _WIN32 | |
302 buf.writeByte('\\'); | |
303 #else | |
304 buf.writeByte('/'); | |
305 #endif | |
306 } | |
307 buf.writestring(filename); | |
308 buf.writeByte(0); | |
309 filename = (char *)buf.extractData(); | |
310 } | |
311 | |
312 m = new Module(filename, ident, 0, 0); | |
313 m->loc = loc; | |
314 | |
315 /* Search along global.path for .di file, then .d file. | |
316 */ | |
317 char *result = NULL; | |
318 FileName *fdi = FileName::forceExt(filename, global.hdr_ext); | |
319 FileName *fd = FileName::forceExt(filename, global.mars_ext); | |
320 char *sdi = fdi->toChars(); | |
321 char *sd = fd->toChars(); | |
322 | |
323 if (FileName::exists(sdi)) | |
324 result = sdi; | |
325 else if (FileName::exists(sd)) | |
326 result = sd; | |
327 else if (FileName::absolute(filename)) | |
328 ; | |
329 else if (!global.path) | |
330 ; | |
331 else | |
332 { | |
333 for (size_t i = 0; i < global.path->dim; i++) | |
334 { | |
335 char *p = (char *)global.path->data[i]; | |
336 char *n = FileName::combine(p, sdi); | |
337 if (FileName::exists(n)) | |
338 { result = n; | |
339 break; | |
340 } | |
341 mem.free(n); | |
342 n = FileName::combine(p, sd); | |
343 if (FileName::exists(n)) | |
344 { result = n; | |
345 break; | |
346 } | |
347 mem.free(n); | |
348 } | |
349 } | |
350 if (result) | |
351 m->srcfile = new File(result); | |
352 | |
353 if (global.params.verbose) | |
354 { | |
355 printf("import "); | |
356 if (packages) | |
357 { | |
358 for (size_t i = 0; i < packages->dim; i++) | |
359 { Identifier *pid = (Identifier *)packages->data[i]; | |
360 printf("%s.", pid->toChars()); | |
361 } | |
362 } | |
363 printf("%s\t(%s)\n", ident->toChars(), m->srcfile->toChars()); | |
364 } | |
365 | |
366 m->read(loc); | |
367 m->parse(); | |
368 | |
369 #ifdef IN_GCC | |
370 d_gcc_magic_module(m); | |
371 #endif | |
372 | |
373 return m; | |
374 } | |
375 | |
376 void Module::read(Loc loc) | |
377 { | |
378 //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars()); | |
379 if (srcfile->read()) | |
380 { error(loc, "cannot read file '%s'", srcfile->toChars()); | |
381 fatal(); | |
382 } | |
383 } | |
384 | |
385 inline unsigned readwordLE(unsigned short *p) | |
386 { | |
387 #if __I86__ | |
388 return *p; | |
389 #else | |
390 return (((unsigned char *)p)[1] << 8) | ((unsigned char *)p)[0]; | |
391 #endif | |
392 } | |
393 | |
394 inline unsigned readwordBE(unsigned short *p) | |
395 { | |
396 return (((unsigned char *)p)[0] << 8) | ((unsigned char *)p)[1]; | |
397 } | |
398 | |
399 inline unsigned readlongLE(unsigned *p) | |
400 { | |
401 #if __I86__ | |
402 return *p; | |
403 #else | |
404 return ((unsigned char *)p)[0] | | |
405 (((unsigned char *)p)[1] << 8) | | |
406 (((unsigned char *)p)[2] << 16) | | |
407 (((unsigned char *)p)[3] << 24); | |
408 #endif | |
409 } | |
410 | |
411 inline unsigned readlongBE(unsigned *p) | |
412 { | |
413 return ((unsigned char *)p)[3] | | |
414 (((unsigned char *)p)[2] << 8) | | |
415 (((unsigned char *)p)[1] << 16) | | |
416 (((unsigned char *)p)[0] << 24); | |
417 } | |
418 | |
419 #if IN_GCC | |
420 void Module::parse(bool dump_source) | |
421 #else | |
422 void Module::parse() | |
423 #endif | |
424 { char *srcname; | |
425 unsigned char *buf; | |
426 unsigned buflen; | |
427 unsigned le; | |
428 unsigned bom; | |
429 | |
430 //printf("Module::parse()\n"); | |
431 | |
432 srcname = srcfile->name->toChars(); | |
433 //printf("Module::parse(srcname = '%s')\n", srcname); | |
434 | |
435 buf = srcfile->buffer; | |
436 buflen = srcfile->len; | |
437 | |
438 if (buflen >= 2) | |
439 { | |
440 /* Convert all non-UTF-8 formats to UTF-8. | |
441 * BOM : http://www.unicode.org/faq/utf_bom.html | |
442 * 00 00 FE FF UTF-32BE, big-endian | |
443 * FF FE 00 00 UTF-32LE, little-endian | |
444 * FE FF UTF-16BE, big-endian | |
445 * FF FE UTF-16LE, little-endian | |
446 * EF BB BF UTF-8 | |
447 */ | |
448 | |
449 bom = 1; // assume there's a BOM | |
450 if (buf[0] == 0xFF && buf[1] == 0xFE) | |
451 { | |
452 if (buflen >= 4 && buf[2] == 0 && buf[3] == 0) | |
453 { // UTF-32LE | |
454 le = 1; | |
455 | |
456 Lutf32: | |
457 OutBuffer dbuf; | |
458 unsigned *pu = (unsigned *)(buf); | |
459 unsigned *pumax = &pu[buflen / 4]; | |
460 | |
461 if (buflen & 3) | |
462 { error("odd length of UTF-32 char source %u", buflen); | |
463 fatal(); | |
464 } | |
465 | |
466 dbuf.reserve(buflen / 4); | |
467 for (pu += bom; pu < pumax; pu++) | |
468 { unsigned u; | |
469 | |
470 u = le ? readlongLE(pu) : readlongBE(pu); | |
471 if (u & ~0x7F) | |
472 { | |
473 if (u > 0x10FFFF) | |
474 { error("UTF-32 value %08x greater than 0x10FFFF", u); | |
475 fatal(); | |
476 } | |
477 dbuf.writeUTF8(u); | |
478 } | |
479 else | |
480 dbuf.writeByte(u); | |
481 } | |
482 dbuf.writeByte(0); // add 0 as sentinel for scanner | |
483 buflen = dbuf.offset - 1; // don't include sentinel in count | |
484 buf = (unsigned char *) dbuf.extractData(); | |
485 } | |
486 else | |
487 { // UTF-16LE (X86) | |
488 // Convert it to UTF-8 | |
489 le = 1; | |
490 | |
491 Lutf16: | |
492 OutBuffer dbuf; | |
493 unsigned short *pu = (unsigned short *)(buf); | |
494 unsigned short *pumax = &pu[buflen / 2]; | |
495 | |
496 if (buflen & 1) | |
497 { error("odd length of UTF-16 char source %u", buflen); | |
498 fatal(); | |
499 } | |
500 | |
501 dbuf.reserve(buflen / 2); | |
502 for (pu += bom; pu < pumax; pu++) | |
503 { unsigned u; | |
504 | |
505 u = le ? readwordLE(pu) : readwordBE(pu); | |
506 if (u & ~0x7F) | |
507 { if (u >= 0xD800 && u <= 0xDBFF) | |
508 { unsigned u2; | |
509 | |
510 if (++pu > pumax) | |
511 { error("surrogate UTF-16 high value %04x at EOF", u); | |
512 fatal(); | |
513 } | |
514 u2 = le ? readwordLE(pu) : readwordBE(pu); | |
515 if (u2 < 0xDC00 || u2 > 0xDFFF) | |
516 { error("surrogate UTF-16 low value %04x out of range", u2); | |
517 fatal(); | |
518 } | |
519 u = (u - 0xD7C0) << 10; | |
520 u |= (u2 - 0xDC00); | |
521 } | |
522 else if (u >= 0xDC00 && u <= 0xDFFF) | |
523 { error("unpaired surrogate UTF-16 value %04x", u); | |
524 fatal(); | |
525 } | |
526 else if (u == 0xFFFE || u == 0xFFFF) | |
527 { error("illegal UTF-16 value %04x", u); | |
528 fatal(); | |
529 } | |
530 dbuf.writeUTF8(u); | |
531 } | |
532 else | |
533 dbuf.writeByte(u); | |
534 } | |
535 dbuf.writeByte(0); // add 0 as sentinel for scanner | |
536 buflen = dbuf.offset - 1; // don't include sentinel in count | |
537 buf = (unsigned char *) dbuf.extractData(); | |
538 } | |
539 } | |
540 else if (buf[0] == 0xFE && buf[1] == 0xFF) | |
541 { // UTF-16BE | |
542 le = 0; | |
543 goto Lutf16; | |
544 } | |
545 else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF) | |
546 { // UTF-32BE | |
547 le = 0; | |
548 goto Lutf32; | |
549 } | |
550 else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF) | |
551 { // UTF-8 | |
552 | |
553 buf += 3; | |
554 buflen -= 3; | |
555 } | |
556 else | |
557 { | |
558 /* There is no BOM. Make use of Arcane Jill's insight that | |
559 * the first char of D source must be ASCII to | |
560 * figure out the encoding. | |
561 */ | |
562 | |
563 bom = 0; | |
564 if (buflen >= 4) | |
565 { if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0) | |
566 { // UTF-32LE | |
567 le = 1; | |
568 goto Lutf32; | |
569 } | |
570 else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0) | |
571 { // UTF-32BE | |
572 le = 0; | |
573 goto Lutf32; | |
574 } | |
575 } | |
576 if (buflen >= 2) | |
577 { | |
578 if (buf[1] == 0) | |
579 { // UTF-16LE | |
580 le = 1; | |
581 goto Lutf16; | |
582 } | |
583 else if (buf[0] == 0) | |
584 { // UTF-16BE | |
585 le = 0; | |
586 goto Lutf16; | |
587 } | |
588 } | |
589 | |
590 // It's UTF-8 | |
591 if (buf[0] >= 0x80) | |
592 { error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]); | |
593 fatal(); | |
594 } | |
595 } | |
596 } | |
597 | |
598 #ifdef IN_GCC | |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
599 // dump utf-8 encoded source |
336 | 600 if (dump_source) |
601 { // %% srcname could contain a path ... | |
602 d_gcc_dump_source(srcname, "utf-8", buf, buflen); | |
603 } | |
604 #endif | |
605 | |
606 /* If it starts with the string "Ddoc", then it's a documentation | |
607 * source file. | |
608 */ | |
609 if (buflen >= 4 && memcmp(buf, "Ddoc", 4) == 0) | |
610 { | |
611 comment = buf + 4; | |
612 isDocFile = 1; | |
613 return; | |
614 } | |
615 if (isHtml) | |
616 { | |
617 OutBuffer *dbuf = new OutBuffer(); | |
618 Html h(srcname, buf, buflen); | |
619 h.extractCode(dbuf); | |
620 buf = dbuf->data; | |
621 buflen = dbuf->offset; | |
622 #ifdef IN_GCC | |
623 // dump extracted source | |
624 if (dump_source) | |
625 d_gcc_dump_source(srcname, "d.utf-8", buf, buflen); | |
626 #endif | |
627 } | |
628 Parser p(this, buf, buflen, docfile != NULL); | |
629 p.nextToken(); | |
630 members = p.parseModule(); | |
631 md = p.md; | |
632 numlines = p.loc.linnum; | |
633 | |
634 DsymbolTable *dst; | |
635 | |
636 if (md) | |
637 { this->ident = md->id; | |
638 dst = Package::resolve(md->packages, &this->parent, NULL); | |
639 } | |
640 else | |
641 { | |
642 dst = modules; | |
643 | |
644 /* Check to see if module name is a valid identifier | |
645 */ | |
646 if (!Lexer::isValidIdentifier(this->ident->toChars())) | |
647 error("has non-identifier characters in filename, use module declaration instead"); | |
648 } | |
649 | |
650 // Update global list of modules | |
651 if (!dst->insert(this)) | |
652 { | |
653 if (md) | |
654 error(loc, "is in multiple packages %s", md->toChars()); | |
655 else | |
656 error(loc, "is in multiple defined"); | |
657 } | |
658 else | |
659 { | |
660 amodules.push(this); | |
661 } | |
662 } | |
663 | |
751
dc8b8b7ea0c1
Fix compile-time warnings. Adjust include in d-asm-i386.
Christian Kamm <kamm incasoftware de>
parents:
708
diff
changeset
|
664 void Module::semantic(Scope* unused_sc) |
336 | 665 { int i; |
666 | |
667 if (semanticstarted) | |
668 return; | |
669 | |
670 //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); | |
671 semanticstarted = 1; | |
672 | |
673 // Note that modules get their own scope, from scratch. | |
674 // This is so regardless of where in the syntax a module | |
675 // gets imported, it is unaffected by context. | |
676 Scope *sc = Scope::createGlobal(this); // create root scope | |
677 | |
678 //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage); | |
679 | |
680 // Add import of "object" if this module isn't "object" | |
681 if (ident != Id::object) | |
682 { | |
683 Import *im = new Import(0, NULL, Id::object, NULL, 0); | |
684 members->shift(im); | |
685 } | |
686 | |
687 // Add all symbols into module's symbol table | |
688 symtab = new DsymbolTable(); | |
689 for (i = 0; i < members->dim; i++) | |
690 { Dsymbol *s; | |
691 | |
692 s = (Dsymbol *)members->data[i]; | |
693 s->addMember(NULL, sc->scopesym, 1); | |
694 } | |
695 | |
696 // Pass 1 semantic routines: do public side of the definition | |
697 for (i = 0; i < members->dim; i++) | |
698 { Dsymbol *s; | |
699 | |
700 s = (Dsymbol *)members->data[i]; | |
701 //printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars()); | |
702 s->semantic(sc); | |
703 runDeferredSemantic(); | |
704 } | |
705 | |
706 sc = sc->pop(); | |
707 sc->pop(); | |
708 semanticdone = semanticstarted; | |
709 //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); | |
710 } | |
711 | |
751
dc8b8b7ea0c1
Fix compile-time warnings. Adjust include in d-asm-i386.
Christian Kamm <kamm incasoftware de>
parents:
708
diff
changeset
|
712 void Module::semantic2(Scope* unused_sc) |
336 | 713 { int i; |
714 | |
715 if (deferred.dim) | |
716 { | |
717 for (int i = 0; i < deferred.dim; i++) | |
718 { | |
719 Dsymbol *sd = (Dsymbol *)deferred.data[i]; | |
720 | |
721 sd->error("unable to resolve forward reference in definition"); | |
722 } | |
723 return; | |
724 } | |
725 //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent); | |
726 if (semanticstarted >= 2) | |
727 return; | |
728 assert(semanticstarted == 1); | |
729 semanticstarted = 2; | |
730 | |
731 // Note that modules get their own scope, from scratch. | |
732 // This is so regardless of where in the syntax a module | |
733 // gets imported, it is unaffected by context. | |
734 Scope *sc = Scope::createGlobal(this); // create root scope | |
735 //printf("Module = %p\n", sc.scopesym); | |
736 | |
737 // Pass 2 semantic routines: do initializers and function bodies | |
738 for (i = 0; i < members->dim; i++) | |
739 { Dsymbol *s; | |
740 | |
741 s = (Dsymbol *)members->data[i]; | |
742 s->semantic2(sc); | |
743 } | |
744 | |
745 sc = sc->pop(); | |
746 sc->pop(); | |
747 semanticdone = semanticstarted; | |
748 //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent); | |
749 } | |
750 | |
751
dc8b8b7ea0c1
Fix compile-time warnings. Adjust include in d-asm-i386.
Christian Kamm <kamm incasoftware de>
parents:
708
diff
changeset
|
751 void Module::semantic3(Scope* unused_sc) |
336 | 752 { int i; |
753 | |
754 //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent); | |
755 if (semanticstarted >= 3) | |
756 return; | |
757 assert(semanticstarted == 2); | |
758 semanticstarted = 3; | |
759 | |
760 // Note that modules get their own scope, from scratch. | |
761 // This is so regardless of where in the syntax a module | |
762 // gets imported, it is unaffected by context. | |
763 Scope *sc = Scope::createGlobal(this); // create root scope | |
764 //printf("Module = %p\n", sc.scopesym); | |
765 | |
766 // Pass 3 semantic routines: do initializers and function bodies | |
767 for (i = 0; i < members->dim; i++) | |
768 { Dsymbol *s; | |
769 | |
770 s = (Dsymbol *)members->data[i]; | |
771 //printf("Module %s: %s.semantic3()\n", toChars(), s->toChars()); | |
772 s->semantic3(sc); | |
773 } | |
774 | |
775 sc = sc->pop(); | |
776 sc->pop(); | |
777 semanticdone = semanticstarted; | |
778 } | |
779 | |
780 void Module::inlineScan() | |
781 { int i; | |
782 | |
783 if (semanticstarted >= 4) | |
784 return; | |
785 assert(semanticstarted == 3); | |
786 semanticstarted = 4; | |
787 | |
788 // Note that modules get their own scope, from scratch. | |
789 // This is so regardless of where in the syntax a module | |
790 // gets imported, it is unaffected by context. | |
791 //printf("Module = %p\n", sc.scopesym); | |
792 | |
793 for (i = 0; i < members->dim; i++) | |
794 { Dsymbol *s; | |
795 | |
796 s = (Dsymbol *)members->data[i]; | |
797 //if (global.params.verbose) | |
798 //printf("inline scan symbol %s\n", s->toChars()); | |
799 | |
800 s->inlineScan(); | |
801 } | |
802 semanticdone = semanticstarted; | |
803 } | |
804 | |
805 /**************************************************** | |
806 */ | |
807 | |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
808 // is this used anywhere? |
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
809 /* |
336 | 810 void Module::gensymfile() |
811 { | |
812 OutBuffer buf; | |
813 HdrGenState hgs; | |
814 | |
815 //printf("Module::gensymfile()\n"); | |
816 | |
817 buf.printf("// Sym file generated from '%s'", srcfile->toChars()); | |
818 buf.writenl(); | |
819 | |
820 for (int i = 0; i < members->dim; i++) | |
821 { Dsymbol *s = (Dsymbol *)members->data[i]; | |
822 | |
823 s->toCBuffer(&buf, &hgs); | |
824 } | |
825 | |
826 // Transfer image to file | |
827 symfile->setbuffer(buf.data, buf.offset); | |
828 buf.data = NULL; | |
829 | |
830 symfile->writev(); | |
580
7824c21a58e3
Restructure path handling a bit. Fixes #66.
Christian Kamm <kamm incasoftware de>
parents:
550
diff
changeset
|
831 }*/ |
336 | 832 |
833 /********************************** | |
834 * Determine if we need to generate an instance of ModuleInfo | |
835 * for this Module. | |
836 */ | |
837 | |
838 int Module::needModuleInfo() | |
839 { | |
550
cbe08531430f
Removed unimplemented switches.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
510
diff
changeset
|
840 return needmoduleinfo; |
336 | 841 } |
842 | |
843 Dsymbol *Module::search(Loc loc, Identifier *ident, int flags) | |
844 { | |
845 /* Since modules can be circularly referenced, | |
846 * need to stop infinite recursive searches. | |
847 */ | |
848 | |
849 //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch); | |
850 Dsymbol *s; | |
851 if (insearch) | |
852 s = NULL; | |
853 else if (searchCacheIdent == ident && searchCacheFlags == flags) | |
854 s = searchCacheSymbol; | |
855 else | |
856 { | |
857 insearch = 1; | |
858 s = ScopeDsymbol::search(loc, ident, flags); | |
859 insearch = 0; | |
860 | |
861 searchCacheIdent = ident; | |
862 searchCacheSymbol = s; | |
863 searchCacheFlags = flags; | |
864 } | |
865 return s; | |
866 } | |
867 | |
868 /******************************************* | |
869 * Can't run semantic on s now, try again later. | |
870 */ | |
871 | |
872 void Module::addDeferredSemantic(Dsymbol *s) | |
873 { | |
874 // Don't add it if it is already there | |
875 for (int i = 0; i < deferred.dim; i++) | |
876 { | |
877 Dsymbol *sd = (Dsymbol *)deferred.data[i]; | |
878 | |
879 if (sd == s) | |
880 return; | |
881 } | |
882 | |
883 //printf("Module::addDeferredSemantic('%s')\n", s->toChars()); | |
884 deferred.push(s); | |
885 } | |
886 | |
887 | |
888 /****************************************** | |
889 * Run semantic() on deferred symbols. | |
890 */ | |
891 | |
892 void Module::runDeferredSemantic() | |
893 { | |
894 size_t len; | |
895 | |
896 static int nested; | |
897 if (nested) | |
898 return; | |
899 //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim); | |
900 nested++; | |
901 | |
902 do | |
903 { | |
904 dprogress = 0; | |
905 len = deferred.dim; | |
906 if (!len) | |
907 break; | |
908 | |
909 Dsymbol **todo; | |
910 Dsymbol *tmp; | |
911 if (len == 1) | |
912 { | |
913 todo = &tmp; | |
914 } | |
915 else | |
916 { | |
917 todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *)); | |
918 assert(todo); | |
919 } | |
920 memcpy(todo, deferred.data, len * sizeof(Dsymbol *)); | |
921 deferred.setDim(0); | |
922 | |
923 for (int i = 0; i < len; i++) | |
924 { | |
925 Dsymbol *s = todo[i]; | |
926 | |
927 s->semantic(NULL); | |
928 //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars()); | |
929 } | |
930 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress); | |
931 } while (deferred.dim < len || dprogress); // while making progress | |
932 nested--; | |
933 //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim); | |
934 } | |
935 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
936 /************************************ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
937 * Recursively look at every module this module imports, |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
938 * return TRUE if it imports m. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
939 * Can be used to detect circular imports. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
940 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
941 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
942 int Module::imports(Module *m) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
943 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
944 //printf("%s Module::imports(%s)\n", toChars(), m->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
945 int aimports_dim = aimports.dim; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
946 #if 0 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
947 for (int i = 0; i < aimports.dim; i++) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
948 { Module *mi = (Module *)aimports.data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
949 printf("\t[%d] %s\n", i, mi->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
950 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
951 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
952 for (int i = 0; i < aimports.dim; i++) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
953 { Module *mi = (Module *)aimports.data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
954 if (mi == m) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
955 return TRUE; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
956 if (!mi->insearch) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
957 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
958 mi->insearch = 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
959 int r = mi->imports(m); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
960 if (r) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
961 return r; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
962 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
963 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
964 return FALSE; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
965 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
872
diff
changeset
|
966 |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
967 /************************************* |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
968 * Return !=0 if module imports itself. |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
969 */ |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
970 |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
971 int Module::selfImports() |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
972 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
973 //printf("Module::selfImports() %s\n", toChars()); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
974 if (!selfimports) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
975 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
976 for (int i = 0; i < amodules.dim; i++) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
977 { Module *mi = (Module *)amodules.data[i]; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
978 //printf("\t[%d] %s\n", i, mi->toChars()); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
979 mi->insearch = 0; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
980 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
981 |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
982 selfimports = imports(this) + 1; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
983 |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
984 for (int i = 0; i < amodules.dim; i++) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
985 { Module *mi = (Module *)amodules.data[i]; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
986 //printf("\t[%d] %s\n", i, mi->toChars()); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
987 mi->insearch = 0; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
988 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
989 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
990 return selfimports - 1; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
991 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
992 |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
986
diff
changeset
|
993 |
336 | 994 /* =========================== ModuleDeclaration ===================== */ |
995 | |
996 ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id) | |
997 { | |
998 this->packages = packages; | |
999 this->id = id; | |
1000 } | |
1001 | |
1002 char *ModuleDeclaration::toChars() | |
1003 { | |
1004 OutBuffer buf; | |
1005 int i; | |
1006 | |
1007 if (packages && packages->dim) | |
1008 { | |
1009 for (i = 0; i < packages->dim; i++) | |
1010 { Identifier *pid = (Identifier *)packages->data[i]; | |
1011 | |
1012 buf.writestring(pid->toChars()); | |
1013 buf.writeByte('.'); | |
1014 } | |
1015 } | |
1016 buf.writestring(id->toChars()); | |
1017 buf.writeByte(0); | |
1018 return (char *)buf.extractData(); | |
1019 } | |
1020 | |
1021 /* =========================== Package ===================== */ | |
1022 | |
1023 Package::Package(Identifier *ident) | |
1024 : ScopeDsymbol(ident) | |
1025 { | |
1026 } | |
1027 | |
1028 | |
1029 const char *Package::kind() | |
1030 { | |
1031 return "package"; | |
1032 } | |
1033 | |
1034 | |
1035 DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg) | |
1036 { | |
1037 DsymbolTable *dst = Module::modules; | |
1038 Dsymbol *parent = NULL; | |
1039 | |
1040 //printf("Package::resolve()\n"); | |
1041 if (ppkg) | |
1042 *ppkg = NULL; | |
1043 | |
1044 if (packages) | |
1045 { int i; | |
1046 | |
1047 for (i = 0; i < packages->dim; i++) | |
1048 { Identifier *pid = (Identifier *)packages->data[i]; | |
1049 Dsymbol *p; | |
1050 | |
1051 p = dst->lookup(pid); | |
1052 if (!p) | |
1053 { | |
1054 p = new Package(pid); | |
1055 dst->insert(p); | |
1056 p->parent = parent; | |
1057 ((ScopeDsymbol *)p)->symtab = new DsymbolTable(); | |
1058 } | |
1059 else | |
1060 { | |
1061 assert(p->isPackage()); | |
1062 if (p->isModule()) | |
1063 { p->error("module and package have the same name"); | |
1064 fatal(); | |
1065 break; | |
1066 } | |
1067 } | |
1068 parent = p; | |
1069 dst = ((Package *)p)->symtab; | |
1070 if (ppkg && !*ppkg) | |
1071 *ppkg = (Package *)p; | |
1072 } | |
1073 if (pparent) | |
1074 { | |
1075 *pparent = parent; | |
1076 } | |
1077 } | |
1078 return dst; | |
1079 } |