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