Mercurial > projects > ldc
annotate dmd/mars.c @ 1622:a542ef277a84
Merge DMD r316: bugzilla 3628 can't cast null to int
---
dmd/constfold.c | 2 +-
dmd/optimize.c | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)
author | Leandro Lucarella <llucax@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 15:18:22 -0300 |
parents | 8f50a13d09a0 |
children | 6c36e3f49b28 |
rev | line source |
---|---|
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
1 |
159 | 2 // Compiler implementation of the D programming language |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3 // Copyright (c) 1999-2009 by Digital Mars |
159 | 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 <ctype.h> | |
14 #include <assert.h> | |
15 #include <limits.h> | |
16 #include <string> | |
17 #include <cstdarg> | |
18 | |
571
cbd6c8073a32
Changed all '#if linux || __APPLE__' to '#if POSIX' so we can support other platforms too, thanx for the suggestion anders.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
568
diff
changeset
|
19 #if POSIX |
159 | 20 #include <errno.h> |
432 | 21 #elif _WIN32 |
22 #include <windows.h> | |
159 | 23 #endif |
24 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
25 #include "rmem.h" |
159 | 26 #include "root.h" |
27 | |
28 #include "mars.h" | |
29 #include "module.h" | |
30 #include "mtype.h" | |
31 #include "id.h" | |
32 #include "cond.h" | |
33 #include "expression.h" | |
34 #include "lexer.h" | |
1587 | 35 #include "json.h" |
159 | 36 |
1064
f0b6549055ab
Make LDC work with LLVM trunk (s/LinkOnceLinkage/LinkOnceOdrLinkage/)
Frits van Bommel <fvbommel wxs.nl>
parents:
988
diff
changeset
|
37 #include "gen/revisions.h" |
788
dce4b4ea2aee
Print llvm package string in version header. See #128.
Christian Kamm <kamm incasoftware de>
parents:
785
diff
changeset
|
38 |
159 | 39 Global global; |
40 | |
41 Global::Global() | |
42 { | |
43 mars_ext = "d"; | |
44 sym_ext = "d"; | |
45 hdr_ext = "di"; | |
46 doc_ext = "html"; | |
47 ddoc_ext = "ddoc"; | |
1587 | 48 json_ext = "json"; |
159 | 49 |
663
6aaa3d3c1183
First part of rename to LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
50 // LDC |
159 | 51 ll_ext = "ll"; |
52 bc_ext = "bc"; | |
675
bfe5229f9d8e
Disable bc output by default. Remove -dis. Add -output-bc, -output-ll, -output-s.
Christian Kamm <kamm incasoftware de>
parents:
663
diff
changeset
|
53 s_ext = "s"; |
708
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
700
diff
changeset
|
54 obj_ext = "o"; |
374
1f20b9f7de1b
Fix nativeobj extension for Windows.
Christian Kamm <kamm incasoftware de>
parents:
366
diff
changeset
|
55 #if _WIN32 |
708
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
700
diff
changeset
|
56 obj_ext_alt = "obj"; |
159 | 57 #endif |
58 | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
59 copyright = "Copyright (c) 1999-2009 by Digital Mars and Tomas Lindquist Olsen"; |
159 | 60 written = "written by Walter Bright and Tomas Lindquist Olsen"; |
1613
8f50a13d09a0
Merge DMD r286: remove dead code
Leandro Lucarella <llucax@gmail.com>
parents:
1603
diff
changeset
|
61 version = "v1.054"; |
853
82ad6c0c601c
Add Elrood's patch to output LDC and LLVM source revs.
Christian Kamm <kamm incasoftware de>
parents:
849
diff
changeset
|
62 ldc_version = LDC_REV; |
1064
f0b6549055ab
Make LDC work with LLVM trunk (s/LinkOnceLinkage/LinkOnceOdrLinkage/)
Frits van Bommel <fvbommel wxs.nl>
parents:
988
diff
changeset
|
63 llvm_version = LLVM_REV_STR; |
159 | 64 global.structalign = 8; |
65 | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
66 // This should only be used as a global, so the other fields are |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
67 // automatically initialized to zero when the program is loaded. |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
68 // In particular, DO NOT zero-initialize .params here (like DMD |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
69 // does) because command-line options initialize some of those |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
70 // fields to non-zero defaults, and do so from constructors that |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
71 // may run before this one. |
159 | 72 } |
73 | |
74 char *Loc::toChars() const | |
75 { | |
76 OutBuffer buf; | |
77 | |
78 if (filename) | |
79 { | |
80 buf.printf("%s", filename); | |
81 } | |
82 | |
83 if (linnum) | |
84 buf.printf("(%d)", linnum); | |
85 buf.writeByte(0); | |
86 return (char *)buf.extractData(); | |
87 } | |
88 | |
89 Loc::Loc(Module *mod, unsigned linnum) | |
90 { | |
91 this->linnum = linnum; | |
92 this->filename = mod ? mod->srcfile->toChars() : NULL; | |
93 } | |
94 | |
1587 | 95 bool Loc::equals(const Loc& loc) |
96 { | |
97 return linnum == loc.linnum && FileName::equals(filename, loc.filename); | |
98 } | |
99 | |
159 | 100 /************************************** |
101 * Print error message and exit. | |
102 */ | |
103 | |
104 void error(Loc loc, const char *format, ...) | |
105 { | |
106 va_list ap; | |
107 va_start(ap, format); | |
108 verror(loc, format, ap); | |
109 va_end( ap ); | |
110 } | |
111 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
112 void warning(Loc loc, const char *format, ...) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
113 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
114 if (global.params.warnings && !global.gag) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
115 { |
1124
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
116 va_list ap; |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
117 va_start(ap, format); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
118 vwarning(loc, format, ap); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
119 va_end( ap ); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
120 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
121 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
122 |
159 | 123 void verror(Loc loc, const char *format, va_list ap) |
124 { | |
125 if (!global.gag) | |
126 { | |
127 char *p = loc.toChars(); | |
128 | |
129 if (*p) | |
130 fprintf(stdmsg, "%s: ", p); | |
131 mem.free(p); | |
132 | |
133 fprintf(stdmsg, "Error: "); | |
134 vfprintf(stdmsg, format, ap); | |
135 fprintf(stdmsg, "\n"); | |
136 fflush(stdmsg); | |
137 } | |
138 global.errors++; | |
139 } | |
140 | |
1124
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
141 void vwarning(Loc loc, const char *format, va_list ap) |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
142 { |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
143 if (global.params.warnings && !global.gag) |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
144 { |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
145 char *p = loc.toChars(); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
146 |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
147 if (*p) |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
148 fprintf(stdmsg, "%s: ", p); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
149 mem.free(p); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
150 |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
151 fprintf(stdmsg, "Warning: "); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
152 vfprintf(stdmsg, format, ap); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
153 fprintf(stdmsg, "\n"); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
154 fflush(stdmsg); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
155 } |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
156 } |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
157 |
159 | 158 /*************************************** |
159 * Call this after printing out fatal error messages to clean up and exit | |
160 * the compiler. | |
161 */ | |
162 | |
163 void fatal() | |
164 { | |
165 #if 0 | |
166 halt(); | |
167 #endif | |
168 exit(EXIT_FAILURE); | |
169 } | |
170 | |
171 /************************************** | |
172 * Try to stop forgetting to remove the breakpoints from | |
173 * release builds. | |
174 */ | |
175 void halt() | |
176 { | |
177 #ifdef DEBUG | |
178 *(char*)0=0; | |
179 #endif | |
180 } | |
181 | |
182 /*********************************** | |
183 * Parse and append contents of environment variable envvar | |
184 * to argc and argv[]. | |
185 * The string is separated into arguments, processing \ and ". | |
186 */ | |
187 | |
188 void getenv_setargv(const char *envvar, int *pargc, char** *pargv) | |
189 { | |
190 char *p; | |
191 | |
192 int instring; | |
193 int slash; | |
194 char c; | |
195 | |
1587 | 196 char *env = getenv(envvar); |
159 | 197 if (!env) |
198 return; | |
199 | |
200 env = mem.strdup(env); // create our own writable copy | |
201 | |
1587 | 202 int argc = *pargc; |
203 Array *argv = new Array(); | |
159 | 204 argv->setDim(argc); |
205 | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
206 int argc_left = 0; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
207 for (int i = 0; i < argc; i++) { |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
208 if (!strcmp((*pargv)[i], "-run") || !strcmp((*pargv)[i], "--run")) { |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
209 // HACK: set flag to indicate we saw '-run' here |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
210 global.params.run = true; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
211 // Don't eat -run yet so the program arguments don't get changed |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
212 argc_left = argc - i; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
213 argc = i; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
214 *pargv = &(*pargv)[i]; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
215 argv->setDim(i); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
216 break; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
217 } else { |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
218 argv->data[i] = (void *)(*pargv)[i]; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
219 } |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
220 } |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
221 // HACK to stop required values from command line being drawn from DFLAGS |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
222 argv->push((char*)""); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
223 argc++; |
159 | 224 |
1587 | 225 int j = 1; // leave argv[0] alone |
159 | 226 while (1) |
227 { | |
1587 | 228 int wildcard = 1; // do wildcard expansion |
159 | 229 switch (*env) |
230 { | |
231 case ' ': | |
232 case '\t': | |
233 env++; | |
234 break; | |
235 | |
236 case 0: | |
237 goto Ldone; | |
238 | |
239 case '"': | |
240 wildcard = 0; | |
241 default: | |
242 argv->push(env); // append | |
243 //argv->insert(j, env); // insert at position j | |
244 j++; | |
245 argc++; | |
246 p = env; | |
247 slash = 0; | |
248 instring = 0; | |
249 c = 0; | |
250 | |
251 while (1) | |
252 { | |
253 c = *env++; | |
254 switch (c) | |
255 { | |
256 case '"': | |
257 p -= (slash >> 1); | |
258 if (slash & 1) | |
259 { p--; | |
260 goto Laddc; | |
261 } | |
262 instring ^= 1; | |
263 slash = 0; | |
264 continue; | |
265 | |
266 case ' ': | |
267 case '\t': | |
268 if (instring) | |
269 goto Laddc; | |
270 *p = 0; | |
271 //if (wildcard) | |
272 //wildcardexpand(); // not implemented | |
273 break; | |
274 | |
275 case '\\': | |
276 slash++; | |
277 *p++ = c; | |
278 continue; | |
279 | |
280 case 0: | |
281 *p = 0; | |
282 //if (wildcard) | |
283 //wildcardexpand(); // not implemented | |
284 goto Ldone; | |
285 | |
286 default: | |
287 Laddc: | |
288 slash = 0; | |
289 *p++ = c; | |
290 continue; | |
291 } | |
292 break; | |
293 } | |
294 } | |
295 } | |
296 | |
297 Ldone: | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
298 assert(argc == argv->dim); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
299 argv->reserve(argc_left); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
300 for (int i = 0; i < argc_left; i++) |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
301 argv->data[argc++] = (void *)(*pargv)[i]; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
302 |
159 | 303 *pargc = argc; |
304 *pargv = (char **)argv->data; | |
305 } |