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