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