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