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