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