Mercurial > projects > ldc
annotate dmd/mars.c @ 1637:a6a839bcc18a
Change template linkage from weak_odr to linkonce_odr. Much better, fixes #363.
author | Matti Niemenmaa <matti.niemenmaa+hg@iki.fi> |
---|---|
date | Mon, 08 Mar 2010 19:05:12 +0200 |
parents | 44b145be2ef5 |
children | 9bf06e02070b |
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 |
1630
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1628
diff
changeset
|
3 // Copyright (c) 1999-2010 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> | |
1630
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1628
diff
changeset
|
16 #if IN_LLVM |
159 | 17 #include <string> |
18 #include <cstdarg> | |
1630
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1628
diff
changeset
|
19 #endif |
159 | 20 |
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
|
21 #if POSIX |
159 | 22 #include <errno.h> |
432 | 23 #elif _WIN32 |
24 #include <windows.h> | |
159 | 25 #endif |
26 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
27 #include "rmem.h" |
159 | 28 #include "root.h" |
29 | |
30 #include "mars.h" | |
31 #include "module.h" | |
32 #include "mtype.h" | |
33 #include "id.h" | |
34 #include "cond.h" | |
35 #include "expression.h" | |
36 #include "lexer.h" | |
1587 | 37 #include "json.h" |
159 | 38 |
1630
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1628
diff
changeset
|
39 #if IN_LLVM |
1064
f0b6549055ab
Make LDC work with LLVM trunk (s/LinkOnceLinkage/LinkOnceOdrLinkage/)
Frits van Bommel <fvbommel wxs.nl>
parents:
988
diff
changeset
|
40 #include "gen/revisions.h" |
1630
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1628
diff
changeset
|
41 #endif |
788
dce4b4ea2aee
Print llvm package string in version header. See #128.
Christian Kamm <kamm incasoftware de>
parents:
785
diff
changeset
|
42 |
159 | 43 Global global; |
44 | |
45 Global::Global() | |
46 { | |
47 mars_ext = "d"; | |
48 sym_ext = "d"; | |
49 hdr_ext = "di"; | |
50 doc_ext = "html"; | |
51 ddoc_ext = "ddoc"; | |
1587 | 52 json_ext = "json"; |
1630
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1628
diff
changeset
|
53 map_ext = "map"; |
159 | 54 |
663
6aaa3d3c1183
First part of rename to LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
55 // LDC |
159 | 56 ll_ext = "ll"; |
57 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
|
58 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
|
59 obj_ext = "o"; |
374
1f20b9f7de1b
Fix nativeobj extension for Windows.
Christian Kamm <kamm incasoftware de>
parents:
366
diff
changeset
|
60 #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
|
61 obj_ext_alt = "obj"; |
159 | 62 #endif |
63 | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
64 copyright = "Copyright (c) 1999-2009 by Digital Mars and Tomas Lindquist Olsen"; |
159 | 65 written = "written by Walter Bright and Tomas Lindquist Olsen"; |
1630
44b145be2ef5
Merge dmd 1.056.
Robert Clipsham <robert@octarineparrot.com>
parents:
1628
diff
changeset
|
66 version = "v1.056"; |
853
82ad6c0c601c
Add Elrood's patch to output LDC and LLVM source revs.
Christian Kamm <kamm incasoftware de>
parents:
849
diff
changeset
|
67 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
|
68 llvm_version = LLVM_REV_STR; |
159 | 69 global.structalign = 8; |
70 | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
71 // 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
|
72 // 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
|
73 // 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
|
74 // 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
|
75 // 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
|
76 // may run before this one. |
159 | 77 } |
78 | |
79 char *Loc::toChars() const | |
80 { | |
81 OutBuffer buf; | |
82 | |
83 if (filename) | |
84 { | |
85 buf.printf("%s", filename); | |
86 } | |
87 | |
88 if (linnum) | |
89 buf.printf("(%d)", linnum); | |
90 buf.writeByte(0); | |
91 return (char *)buf.extractData(); | |
92 } | |
93 | |
94 Loc::Loc(Module *mod, unsigned linnum) | |
95 { | |
96 this->linnum = linnum; | |
97 this->filename = mod ? mod->srcfile->toChars() : NULL; | |
98 } | |
99 | |
1587 | 100 bool Loc::equals(const Loc& loc) |
101 { | |
102 return linnum == loc.linnum && FileName::equals(filename, loc.filename); | |
103 } | |
104 | |
159 | 105 /************************************** |
106 * Print error message and exit. | |
107 */ | |
108 | |
109 void error(Loc loc, const char *format, ...) | |
110 { | |
111 va_list ap; | |
112 va_start(ap, format); | |
113 verror(loc, format, ap); | |
114 va_end( ap ); | |
115 } | |
116 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
117 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
|
118 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
119 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
|
120 { |
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
|
121 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
|
122 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
|
123 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
|
124 va_end( ap ); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
125 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
126 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
127 |
159 | 128 void verror(Loc loc, const char *format, va_list ap) |
129 { | |
130 if (!global.gag) | |
131 { | |
132 char *p = loc.toChars(); | |
133 | |
134 if (*p) | |
135 fprintf(stdmsg, "%s: ", p); | |
136 mem.free(p); | |
137 | |
138 fprintf(stdmsg, "Error: "); | |
139 vfprintf(stdmsg, format, ap); | |
140 fprintf(stdmsg, "\n"); | |
141 fflush(stdmsg); | |
142 } | |
143 global.errors++; | |
144 } | |
145 | |
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
|
146 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
|
147 { |
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 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
|
149 { |
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 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
|
151 |
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 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
|
153 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
|
154 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
|
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 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
|
157 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
|
158 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
|
159 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
|
160 } |
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
|
161 } |
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
|
162 |
159 | 163 /*************************************** |
164 * Call this after printing out fatal error messages to clean up and exit | |
165 * the compiler. | |
166 */ | |
167 | |
168 void fatal() | |
169 { | |
170 #if 0 | |
171 halt(); | |
172 #endif | |
173 exit(EXIT_FAILURE); | |
174 } | |
175 | |
176 /************************************** | |
177 * Try to stop forgetting to remove the breakpoints from | |
178 * release builds. | |
179 */ | |
180 void halt() | |
181 { | |
182 #ifdef DEBUG | |
183 *(char*)0=0; | |
184 #endif | |
185 } | |
186 | |
187 /*********************************** | |
188 * Parse and append contents of environment variable envvar | |
189 * to argc and argv[]. | |
190 * The string is separated into arguments, processing \ and ". | |
191 */ | |
192 | |
193 void getenv_setargv(const char *envvar, int *pargc, char** *pargv) | |
194 { | |
195 char *p; | |
196 | |
197 int instring; | |
198 int slash; | |
199 char c; | |
200 | |
1587 | 201 char *env = getenv(envvar); |
159 | 202 if (!env) |
203 return; | |
204 | |
205 env = mem.strdup(env); // create our own writable copy | |
206 | |
1587 | 207 int argc = *pargc; |
208 Array *argv = new Array(); | |
159 | 209 argv->setDim(argc); |
210 | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
211 int argc_left = 0; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
212 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
|
213 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
|
214 // 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
|
215 global.params.run = true; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
216 // 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
|
217 argc_left = argc - i; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
218 argc = i; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
219 *pargv = &(*pargv)[i]; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
220 argv->setDim(i); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
221 break; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
222 } else { |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
223 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
|
224 } |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
225 } |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
226 // 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
|
227 argv->push((char*)""); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
228 argc++; |
159 | 229 |
1587 | 230 int j = 1; // leave argv[0] alone |
159 | 231 while (1) |
232 { | |
1587 | 233 int wildcard = 1; // do wildcard expansion |
159 | 234 switch (*env) |
235 { | |
236 case ' ': | |
237 case '\t': | |
238 env++; | |
239 break; | |
240 | |
241 case 0: | |
242 goto Ldone; | |
243 | |
244 case '"': | |
245 wildcard = 0; | |
246 default: | |
247 argv->push(env); // append | |
248 //argv->insert(j, env); // insert at position j | |
249 j++; | |
250 argc++; | |
251 p = env; | |
252 slash = 0; | |
253 instring = 0; | |
254 c = 0; | |
255 | |
256 while (1) | |
257 { | |
258 c = *env++; | |
259 switch (c) | |
260 { | |
261 case '"': | |
262 p -= (slash >> 1); | |
263 if (slash & 1) | |
264 { p--; | |
265 goto Laddc; | |
266 } | |
267 instring ^= 1; | |
268 slash = 0; | |
269 continue; | |
270 | |
271 case ' ': | |
272 case '\t': | |
273 if (instring) | |
274 goto Laddc; | |
275 *p = 0; | |
276 //if (wildcard) | |
277 //wildcardexpand(); // not implemented | |
278 break; | |
279 | |
280 case '\\': | |
281 slash++; | |
282 *p++ = c; | |
283 continue; | |
284 | |
285 case 0: | |
286 *p = 0; | |
287 //if (wildcard) | |
288 //wildcardexpand(); // not implemented | |
289 goto Ldone; | |
290 | |
291 default: | |
292 Laddc: | |
293 slash = 0; | |
294 *p++ = c; | |
295 continue; | |
296 } | |
297 break; | |
298 } | |
299 } | |
300 } | |
301 | |
302 Ldone: | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
303 assert(argc == argv->dim); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
304 argv->reserve(argc_left); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
305 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
|
306 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
|
307 |
159 | 308 *pargc = argc; |
309 *pargv = (char **)argv->data; | |
310 } |