Mercurial > projects > ldc
annotate dmd/constfold.c @ 1372:229e02867307
Fix format-string bugs by adding __attribute__((__format__)) in all applicable
places and fixing all warnings my gcc produced.
Among other things, this should fix several segfaults (including one I just
ran into).
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Sun, 17 May 2009 00:15:25 +0200 |
parents | 8026319762be |
children | def7a1d494fd |
rev | line source |
---|---|
336 | 1 |
2 // Compiler implementation of the D programming language | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
3 // Copyright (c) 1999-2009 by Digital Mars |
336 | 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 #include <math.h> | |
15 | |
16 #if __DMC__ | |
17 #include <complex.h> | |
18 #endif | |
19 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
735
diff
changeset
|
20 #include "rmem.h" |
336 | 21 #include "root.h" |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
22 #include "port.h" |
336 | 23 |
24 #include "mtype.h" | |
25 #include "expression.h" | |
26 #include "aggregate.h" | |
27 #include "declaration.h" | |
28 | |
29 #ifdef IN_GCC | |
30 #include "d-gcc-real.h" | |
31 | |
32 /* %% fix? */ | |
33 extern "C" bool real_isnan (const real_t *); | |
34 #endif | |
35 | |
36 static real_t zero; // work around DMC bug for now | |
37 | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
38 #if __FreeBSD__ |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
39 #define fmodl fmod // hack for now, fix later |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
40 #endif |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
41 |
336 | 42 #define LOG 0 |
43 | |
44 Expression *expType(Type *type, Expression *e) | |
45 { | |
46 if (type != e->type) | |
47 { | |
48 e = e->copy(); | |
49 e->type = type; | |
50 } | |
51 return e; | |
52 } | |
53 | |
54 /* ================================== isConst() ============================== */ | |
55 | |
56 int Expression::isConst() | |
57 { | |
58 //printf("Expression::isConst(): %s\n", toChars()); | |
59 return 0; | |
60 } | |
61 | |
62 int IntegerExp::isConst() | |
63 { | |
64 return 1; | |
65 } | |
66 | |
67 int RealExp::isConst() | |
68 { | |
69 return 1; | |
70 } | |
71 | |
72 int ComplexExp::isConst() | |
73 { | |
74 return 1; | |
75 } | |
76 | |
77 int SymOffExp::isConst() | |
78 { | |
79 return 2; | |
80 } | |
81 | |
82 /* =============================== constFold() ============================== */ | |
83 | |
84 /* The constFold() functions were redundant with the optimize() ones, | |
85 * and so have been folded in with them. | |
86 */ | |
87 | |
88 /* ========================================================================== */ | |
89 | |
90 Expression *Neg(Type *type, Expression *e1) | |
91 { Expression *e; | |
92 Loc loc = e1->loc; | |
93 | |
94 if (e1->type->isreal()) | |
95 { | |
96 e = new RealExp(loc, -e1->toReal(), type); | |
97 } | |
98 else if (e1->type->isimaginary()) | |
99 { | |
100 e = new RealExp(loc, -e1->toImaginary(), type); | |
101 } | |
102 else if (e1->type->iscomplex()) | |
103 { | |
104 e = new ComplexExp(loc, -e1->toComplex(), type); | |
105 } | |
106 else | |
107 e = new IntegerExp(loc, -e1->toInteger(), type); | |
108 return e; | |
109 } | |
110 | |
111 Expression *Com(Type *type, Expression *e1) | |
112 { Expression *e; | |
113 Loc loc = e1->loc; | |
114 | |
115 e = new IntegerExp(loc, ~e1->toInteger(), type); | |
116 return e; | |
117 } | |
118 | |
119 Expression *Not(Type *type, Expression *e1) | |
120 { Expression *e; | |
121 Loc loc = e1->loc; | |
122 | |
123 e = new IntegerExp(loc, e1->isBool(0), type); | |
124 return e; | |
125 } | |
126 | |
127 Expression *Bool(Type *type, Expression *e1) | |
128 { Expression *e; | |
129 Loc loc = e1->loc; | |
130 | |
131 e = new IntegerExp(loc, e1->isBool(1), type); | |
132 return e; | |
133 } | |
134 | |
135 Expression *Add(Type *type, Expression *e1, Expression *e2) | |
136 { Expression *e; | |
137 Loc loc = e1->loc; | |
138 | |
139 #if LOG | |
140 printf("Add(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | |
141 #endif | |
142 if (type->isreal()) | |
143 { | |
144 e = new RealExp(loc, e1->toReal() + e2->toReal(), type); | |
145 } | |
146 else if (type->isimaginary()) | |
147 { | |
148 e = new RealExp(loc, e1->toImaginary() + e2->toImaginary(), type); | |
149 } | |
150 else if (type->iscomplex()) | |
151 { | |
152 // This rigamarole is necessary so that -0.0 doesn't get | |
153 // converted to +0.0 by doing an extraneous add with +0.0 | |
154 complex_t c1; | |
155 real_t r1; | |
156 real_t i1; | |
157 | |
158 complex_t c2; | |
159 real_t r2; | |
160 real_t i2; | |
161 | |
162 complex_t v; | |
163 int x; | |
164 | |
165 if (e1->type->isreal()) | |
166 { r1 = e1->toReal(); | |
167 x = 0; | |
168 } | |
169 else if (e1->type->isimaginary()) | |
170 { i1 = e1->toImaginary(); | |
171 x = 3; | |
172 } | |
173 else | |
174 { c1 = e1->toComplex(); | |
175 x = 6; | |
176 } | |
177 | |
178 if (e2->type->isreal()) | |
179 { r2 = e2->toReal(); | |
180 } | |
181 else if (e2->type->isimaginary()) | |
182 { i2 = e2->toImaginary(); | |
183 x += 1; | |
184 } | |
185 else | |
186 { c2 = e2->toComplex(); | |
187 x += 2; | |
188 } | |
189 | |
190 switch (x) | |
191 { | |
192 #if __DMC__ | |
193 case 0+0: v = (complex_t) (r1 + r2); break; | |
194 case 0+1: v = r1 + i2 * I; break; | |
195 case 0+2: v = r1 + c2; break; | |
196 case 3+0: v = i1 * I + r2; break; | |
197 case 3+1: v = (complex_t) ((i1 + i2) * I); break; | |
198 case 3+2: v = i1 * I + c2; break; | |
199 case 6+0: v = c1 + r2; break; | |
200 case 6+1: v = c1 + i2 * I; break; | |
201 case 6+2: v = c1 + c2; break; | |
202 #else | |
203 case 0+0: v = complex_t(r1 + r2, 0); break; | |
204 case 0+1: v = complex_t(r1, i2); break; | |
205 case 0+2: v = complex_t(r1 + creall(c2), cimagl(c2)); break; | |
206 case 3+0: v = complex_t(r2, i1); break; | |
207 case 3+1: v = complex_t(0, i1 + i2); break; | |
208 case 3+2: v = complex_t(creall(c2), i1 + cimagl(c2)); break; | |
209 case 6+0: v = complex_t(creall(c1) + r2, cimagl(c2)); break; | |
210 case 6+1: v = complex_t(creall(c1), cimagl(c1) + i2); break; | |
211 case 6+2: v = c1 + c2; break; | |
212 #endif | |
213 default: assert(0); | |
214 } | |
215 e = new ComplexExp(loc, v, type); | |
216 } | |
217 else if (e1->op == TOKsymoff) | |
218 { | |
219 SymOffExp *soe = (SymOffExp *)e1; | |
220 e = new SymOffExp(loc, soe->var, soe->offset + e2->toInteger()); | |
221 e->type = type; | |
222 } | |
223 else if (e2->op == TOKsymoff) | |
224 { | |
225 SymOffExp *soe = (SymOffExp *)e2; | |
226 e = new SymOffExp(loc, soe->var, soe->offset + e1->toInteger()); | |
227 e->type = type; | |
228 } | |
229 else | |
230 e = new IntegerExp(loc, e1->toInteger() + e2->toInteger(), type); | |
231 return e; | |
232 } | |
233 | |
234 | |
235 Expression *Min(Type *type, Expression *e1, Expression *e2) | |
236 { Expression *e; | |
237 Loc loc = e1->loc; | |
238 | |
239 if (type->isreal()) | |
240 { | |
241 e = new RealExp(loc, e1->toReal() - e2->toReal(), type); | |
242 } | |
243 else if (type->isimaginary()) | |
244 { | |
245 e = new RealExp(loc, e1->toImaginary() - e2->toImaginary(), type); | |
246 } | |
247 else if (type->iscomplex()) | |
248 { | |
249 // This rigamarole is necessary so that -0.0 doesn't get | |
250 // converted to +0.0 by doing an extraneous add with +0.0 | |
251 complex_t c1; | |
252 real_t r1; | |
253 real_t i1; | |
254 | |
255 complex_t c2; | |
256 real_t r2; | |
257 real_t i2; | |
258 | |
259 complex_t v; | |
260 int x; | |
261 | |
262 if (e1->type->isreal()) | |
263 { r1 = e1->toReal(); | |
264 x = 0; | |
265 } | |
266 else if (e1->type->isimaginary()) | |
267 { i1 = e1->toImaginary(); | |
268 x = 3; | |
269 } | |
270 else | |
271 { c1 = e1->toComplex(); | |
272 x = 6; | |
273 } | |
274 | |
275 if (e2->type->isreal()) | |
276 { r2 = e2->toReal(); | |
277 } | |
278 else if (e2->type->isimaginary()) | |
279 { i2 = e2->toImaginary(); | |
280 x += 1; | |
281 } | |
282 else | |
283 { c2 = e2->toComplex(); | |
284 x += 2; | |
285 } | |
286 | |
287 switch (x) | |
288 { | |
289 #if __DMC__ | |
290 case 0+0: v = (complex_t) (r1 - r2); break; | |
291 case 0+1: v = r1 - i2 * I; break; | |
292 case 0+2: v = r1 - c2; break; | |
293 case 3+0: v = i1 * I - r2; break; | |
294 case 3+1: v = (complex_t) ((i1 - i2) * I); break; | |
295 case 3+2: v = i1 * I - c2; break; | |
296 case 6+0: v = c1 - r2; break; | |
297 case 6+1: v = c1 - i2 * I; break; | |
298 case 6+2: v = c1 - c2; break; | |
299 #else | |
300 case 0+0: v = complex_t(r1 - r2, 0); break; | |
301 case 0+1: v = complex_t(r1, -i2); break; | |
302 case 0+2: v = complex_t(r1 - creall(c2), -cimagl(c2)); break; | |
303 case 3+0: v = complex_t(-r2, i1); break; | |
304 case 3+1: v = complex_t(0, i1 - i2); break; | |
305 case 3+2: v = complex_t(-creall(c2), i1 - cimagl(c2)); break; | |
306 case 6+0: v = complex_t(creall(c1) - r2, cimagl(c1)); break; | |
307 case 6+1: v = complex_t(creall(c1), cimagl(c1) - i2); break; | |
308 case 6+2: v = c1 - c2; break; | |
309 #endif | |
310 default: assert(0); | |
311 } | |
312 e = new ComplexExp(loc, v, type); | |
313 } | |
314 else if (e1->op == TOKsymoff) | |
315 { | |
316 SymOffExp *soe = (SymOffExp *)e1; | |
317 e = new SymOffExp(loc, soe->var, soe->offset - e2->toInteger()); | |
318 e->type = type; | |
319 } | |
320 else | |
321 { | |
322 e = new IntegerExp(loc, e1->toInteger() - e2->toInteger(), type); | |
323 } | |
324 return e; | |
325 } | |
326 | |
327 Expression *Mul(Type *type, Expression *e1, Expression *e2) | |
328 { Expression *e; | |
329 Loc loc = e1->loc; | |
330 | |
331 if (type->isfloating()) | |
332 { complex_t c; | |
333 #ifdef IN_GCC | |
334 real_t r; | |
335 #else | |
336 d_float80 r; | |
337 #endif | |
338 | |
339 if (e1->type->isreal()) | |
340 { | |
341 #if __DMC__ | |
342 c = e1->toReal() * e2->toComplex(); | |
343 #else | |
344 r = e1->toReal(); | |
345 c = e2->toComplex(); | |
346 c = complex_t(r * creall(c), r * cimagl(c)); | |
347 #endif | |
348 } | |
349 else if (e1->type->isimaginary()) | |
350 { | |
351 #if __DMC__ | |
352 c = e1->toImaginary() * I * e2->toComplex(); | |
353 #else | |
354 r = e1->toImaginary(); | |
355 c = e2->toComplex(); | |
356 c = complex_t(-r * cimagl(c), r * creall(c)); | |
357 #endif | |
358 } | |
359 else if (e2->type->isreal()) | |
360 { | |
361 #if __DMC__ | |
362 c = e2->toReal() * e1->toComplex(); | |
363 #else | |
364 r = e2->toReal(); | |
365 c = e1->toComplex(); | |
366 c = complex_t(r * creall(c), r * cimagl(c)); | |
367 #endif | |
368 } | |
369 else if (e2->type->isimaginary()) | |
370 { | |
371 #if __DMC__ | |
372 c = e1->toComplex() * e2->toImaginary() * I; | |
373 #else | |
374 r = e2->toImaginary(); | |
375 c = e1->toComplex(); | |
376 c = complex_t(-r * cimagl(c), r * creall(c)); | |
377 #endif | |
378 } | |
379 else | |
380 c = e1->toComplex() * e2->toComplex(); | |
381 | |
382 if (type->isreal()) | |
383 e = new RealExp(loc, creall(c), type); | |
384 else if (type->isimaginary()) | |
385 e = new RealExp(loc, cimagl(c), type); | |
386 else if (type->iscomplex()) | |
387 e = new ComplexExp(loc, c, type); | |
388 else | |
389 assert(0); | |
390 } | |
391 else | |
392 { | |
393 e = new IntegerExp(loc, e1->toInteger() * e2->toInteger(), type); | |
394 } | |
395 return e; | |
396 } | |
397 | |
398 Expression *Div(Type *type, Expression *e1, Expression *e2) | |
399 { Expression *e; | |
400 Loc loc = e1->loc; | |
401 | |
402 if (type->isfloating()) | |
403 { complex_t c; | |
404 #ifdef IN_GCC | |
405 real_t r; | |
406 #else | |
407 d_float80 r; | |
408 #endif | |
409 | |
410 //e1->type->print(); | |
411 //e2->type->print(); | |
412 if (e2->type->isreal()) | |
413 { | |
414 if (e1->type->isreal()) | |
415 { | |
416 e = new RealExp(loc, e1->toReal() / e2->toReal(), type); | |
417 return e; | |
418 } | |
419 #if __DMC__ | |
420 //r = e2->toReal(); | |
421 //c = e1->toComplex(); | |
422 //printf("(%Lg + %Lgi) / %Lg\n", creall(c), cimagl(c), r); | |
423 | |
424 c = e1->toComplex() / e2->toReal(); | |
425 #else | |
426 r = e2->toReal(); | |
427 c = e1->toComplex(); | |
428 c = complex_t(creall(c) / r, cimagl(c) / r); | |
429 #endif | |
430 } | |
431 else if (e2->type->isimaginary()) | |
432 { | |
433 #if __DMC__ | |
434 //r = e2->toImaginary(); | |
435 //c = e1->toComplex(); | |
436 //printf("(%Lg + %Lgi) / %Lgi\n", creall(c), cimagl(c), r); | |
437 | |
438 c = e1->toComplex() / (e2->toImaginary() * I); | |
439 #else | |
440 r = e2->toImaginary(); | |
441 c = e1->toComplex(); | |
442 c = complex_t(cimagl(c) / r, -creall(c) / r); | |
443 #endif | |
444 } | |
445 else | |
446 { | |
447 c = e1->toComplex() / e2->toComplex(); | |
448 } | |
449 | |
450 if (type->isreal()) | |
451 e = new RealExp(loc, creall(c), type); | |
452 else if (type->isimaginary()) | |
453 e = new RealExp(loc, cimagl(c), type); | |
454 else if (type->iscomplex()) | |
455 e = new ComplexExp(loc, c, type); | |
456 else | |
457 assert(0); | |
458 } | |
459 else | |
460 { sinteger_t n1; | |
461 sinteger_t n2; | |
462 sinteger_t n; | |
463 | |
464 n1 = e1->toInteger(); | |
465 n2 = e2->toInteger(); | |
466 if (n2 == 0) | |
467 { e2->error("divide by 0"); | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
468 e2 = new IntegerExp(loc, 1, e2->type); |
336 | 469 n2 = 1; |
470 } | |
471 if (e1->type->isunsigned() || e2->type->isunsigned()) | |
472 n = ((d_uns64) n1) / ((d_uns64) n2); | |
473 else | |
474 n = n1 / n2; | |
475 e = new IntegerExp(loc, n, type); | |
476 } | |
477 return e; | |
478 } | |
479 | |
480 Expression *Mod(Type *type, Expression *e1, Expression *e2) | |
481 { Expression *e; | |
482 Loc loc = e1->loc; | |
483 | |
484 if (type->isfloating()) | |
485 { | |
486 complex_t c; | |
487 | |
488 if (e2->type->isreal()) | |
489 { real_t r2 = e2->toReal(); | |
490 | |
491 #ifdef __DMC__ | |
492 c = fmodl(e1->toReal(), r2) + fmodl(e1->toImaginary(), r2) * I; | |
493 #elif defined(IN_GCC) | |
494 c = complex_t(e1->toReal() % r2, e1->toImaginary() % r2); | |
735
eee9efe5b51f
Attempt at getting LLVM to provide a proper target data layout. Should assert now if things are borked.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
734
diff
changeset
|
495 #elif (defined(__FreeBSD__) && __FreeBSD_version < 800000) || defined(__arm__) || defined(__thumb__) |
734
6dcab994ddc3
Fold in mandel's patch for ARM from #106
Christian Kamm <kamm incasoftware de>
parents:
717
diff
changeset
|
496 // freebsd is kinda messed up. the STABLE branch doesn't support C99's fmodl !?! |
6dcab994ddc3
Fold in mandel's patch for ARM from #106
Christian Kamm <kamm incasoftware de>
parents:
717
diff
changeset
|
497 // arm also doesn't like fmodl |
6dcab994ddc3
Fold in mandel's patch for ARM from #106
Christian Kamm <kamm incasoftware de>
parents:
717
diff
changeset
|
498 c = complex_t(fmod(e1->toReal(), r2), fmod(e1->toImaginary(), r2)); |
336 | 499 #else |
500 c = complex_t(fmodl(e1->toReal(), r2), fmodl(e1->toImaginary(), r2)); | |
501 #endif | |
502 } | |
503 else if (e2->type->isimaginary()) | |
504 { real_t i2 = e2->toImaginary(); | |
505 | |
506 #ifdef __DMC__ | |
507 c = fmodl(e1->toReal(), i2) + fmodl(e1->toImaginary(), i2) * I; | |
508 #elif defined(IN_GCC) | |
509 c = complex_t(e1->toReal() % i2, e1->toImaginary() % i2); | |
735
eee9efe5b51f
Attempt at getting LLVM to provide a proper target data layout. Should assert now if things are borked.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
734
diff
changeset
|
510 #elif (defined(__FreeBSD__) && __FreeBSD_version < 800000) || defined(__arm__) || defined(__thumb__) |
637
29dc68c949b0
Applied the FreeBSD patch from Ralith, closes ticket #95 , slightly changed but basically the same. Thanx Ralith :)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
511 // freebsd is kinda messed up. the STABLE branch doesn't support C99's fmodl !?! |
734
6dcab994ddc3
Fold in mandel's patch for ARM from #106
Christian Kamm <kamm incasoftware de>
parents:
717
diff
changeset
|
512 // arm also doesn't like fmodl |
6dcab994ddc3
Fold in mandel's patch for ARM from #106
Christian Kamm <kamm incasoftware de>
parents:
717
diff
changeset
|
513 c = complex_t(fmod(e1->toReal(), i2), fmod(e1->toImaginary(), i2)); |
336 | 514 #else |
515 c = complex_t(fmodl(e1->toReal(), i2), fmodl(e1->toImaginary(), i2)); | |
516 #endif | |
517 } | |
518 else | |
519 assert(0); | |
520 | |
521 if (type->isreal()) | |
522 e = new RealExp(loc, creall(c), type); | |
523 else if (type->isimaginary()) | |
524 e = new RealExp(loc, cimagl(c), type); | |
525 else if (type->iscomplex()) | |
526 e = new ComplexExp(loc, c, type); | |
527 else | |
528 assert(0); | |
529 } | |
530 else | |
531 { sinteger_t n1; | |
532 sinteger_t n2; | |
533 sinteger_t n; | |
534 | |
535 n1 = e1->toInteger(); | |
536 n2 = e2->toInteger(); | |
537 if (n2 == 0) | |
538 { e2->error("divide by 0"); | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
539 e2 = new IntegerExp(loc, 1, e2->type); |
336 | 540 n2 = 1; |
541 } | |
542 if (e1->type->isunsigned() || e2->type->isunsigned()) | |
543 n = ((d_uns64) n1) % ((d_uns64) n2); | |
544 else | |
545 n = n1 % n2; | |
546 e = new IntegerExp(loc, n, type); | |
547 } | |
548 return e; | |
549 } | |
550 | |
551 Expression *Shl(Type *type, Expression *e1, Expression *e2) | |
552 { Expression *e; | |
553 Loc loc = e1->loc; | |
554 | |
555 e = new IntegerExp(loc, e1->toInteger() << e2->toInteger(), type); | |
556 return e; | |
557 } | |
558 | |
559 Expression *Shr(Type *type, Expression *e1, Expression *e2) | |
560 { Expression *e; | |
561 Loc loc = e1->loc; | |
562 unsigned count; | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
563 dinteger_t value; |
336 | 564 |
565 value = e1->toInteger(); | |
566 count = e2->toInteger(); | |
567 switch (e1->type->toBasetype()->ty) | |
568 { | |
569 case Tint8: | |
570 value = (d_int8)(value) >> count; | |
571 break; | |
572 | |
573 case Tuns8: | |
574 value = (d_uns8)(value) >> count; | |
575 break; | |
576 | |
577 case Tint16: | |
578 value = (d_int16)(value) >> count; | |
579 break; | |
580 | |
581 case Tuns16: | |
582 value = (d_uns16)(value) >> count; | |
583 break; | |
584 | |
585 case Tint32: | |
586 value = (d_int32)(value) >> count; | |
587 break; | |
588 | |
589 case Tuns32: | |
590 value = (d_uns32)(value) >> count; | |
591 break; | |
592 | |
593 case Tint64: | |
594 value = (d_int64)(value) >> count; | |
595 break; | |
596 | |
597 case Tuns64: | |
598 value = (d_uns64)(value) >> count; | |
599 break; | |
600 | |
601 default: | |
602 assert(0); | |
603 } | |
604 e = new IntegerExp(loc, value, type); | |
605 return e; | |
606 } | |
607 | |
608 Expression *Ushr(Type *type, Expression *e1, Expression *e2) | |
609 { Expression *e; | |
610 Loc loc = e1->loc; | |
611 unsigned count; | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
612 dinteger_t value; |
336 | 613 |
614 value = e1->toInteger(); | |
615 count = e2->toInteger(); | |
616 switch (e1->type->toBasetype()->ty) | |
617 { | |
618 case Tint8: | |
619 case Tuns8: | |
620 assert(0); // no way to trigger this | |
621 value = (value & 0xFF) >> count; | |
622 break; | |
623 | |
624 case Tint16: | |
625 case Tuns16: | |
626 assert(0); // no way to trigger this | |
627 value = (value & 0xFFFF) >> count; | |
628 break; | |
629 | |
630 case Tint32: | |
631 case Tuns32: | |
632 value = (value & 0xFFFFFFFF) >> count; | |
633 break; | |
634 | |
635 case Tint64: | |
636 case Tuns64: | |
637 value = (d_uns64)(value) >> count; | |
638 break; | |
639 | |
640 default: | |
641 assert(0); | |
642 } | |
643 e = new IntegerExp(loc, value, type); | |
644 return e; | |
645 } | |
646 | |
647 Expression *And(Type *type, Expression *e1, Expression *e2) | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
648 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
649 Expression *e; |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
650 e = new IntegerExp(e1->loc, e1->toInteger() & e2->toInteger(), type); |
336 | 651 return e; |
652 } | |
653 | |
654 Expression *Or(Type *type, Expression *e1, Expression *e2) | |
655 { Expression *e; | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
656 e = new IntegerExp(e1->loc, e1->toInteger() | e2->toInteger(), type); |
336 | 657 return e; |
658 } | |
659 | |
660 Expression *Xor(Type *type, Expression *e1, Expression *e2) | |
661 { Expression *e; | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
662 e = new IntegerExp(e1->loc, e1->toInteger() ^ e2->toInteger(), type); |
336 | 663 return e; |
664 } | |
665 | |
666 /* Also returns EXP_CANT_INTERPRET if cannot be computed. | |
667 */ | |
668 Expression *Equal(enum TOK op, Type *type, Expression *e1, Expression *e2) | |
669 { Expression *e; | |
670 Loc loc = e1->loc; | |
671 int cmp; | |
672 real_t r1; | |
673 real_t r2; | |
674 | |
675 //printf("Equal(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | |
676 | |
677 assert(op == TOKequal || op == TOKnotequal); | |
678 | |
679 if (e1->op == TOKnull) | |
680 { | |
681 if (e2->op == TOKnull) | |
682 cmp = 1; | |
683 else if (e2->op == TOKstring) | |
684 { StringExp *es2 = (StringExp *)e2; | |
685 cmp = (0 == es2->len); | |
686 } | |
687 else if (e2->op == TOKarrayliteral) | |
688 { ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
689 cmp = !es2->elements || (0 == es2->elements->dim); | |
690 } | |
691 else | |
692 return EXP_CANT_INTERPRET; | |
693 } | |
694 else if (e2->op == TOKnull) | |
695 { | |
696 if (e1->op == TOKstring) | |
697 { StringExp *es1 = (StringExp *)e1; | |
698 cmp = (0 == es1->len); | |
699 } | |
700 else if (e1->op == TOKarrayliteral) | |
701 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; | |
702 cmp = !es1->elements || (0 == es1->elements->dim); | |
703 } | |
704 else | |
705 return EXP_CANT_INTERPRET; | |
706 } | |
707 else if (e1->op == TOKstring && e2->op == TOKstring) | |
708 { StringExp *es1 = (StringExp *)e1; | |
709 StringExp *es2 = (StringExp *)e2; | |
710 | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
711 if (es1->sz != es2->sz) |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
712 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
713 assert(global.errors); |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
714 return EXP_CANT_INTERPRET; |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
715 } |
336 | 716 if (es1->len == es2->len && |
717 memcmp(es1->string, es2->string, es1->sz * es1->len) == 0) | |
718 cmp = 1; | |
719 else | |
720 cmp = 0; | |
721 } | |
722 else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral) | |
723 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; | |
724 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
725 | |
726 if ((!es1->elements || !es1->elements->dim) && | |
727 (!es2->elements || !es2->elements->dim)) | |
728 cmp = 1; // both arrays are empty | |
729 else if (!es1->elements || !es2->elements) | |
730 cmp = 0; | |
731 else if (es1->elements->dim != es2->elements->dim) | |
732 cmp = 0; | |
733 else | |
734 { | |
735 for (size_t i = 0; i < es1->elements->dim; i++) | |
736 { Expression *ee1 = (Expression *)es1->elements->data[i]; | |
737 Expression *ee2 = (Expression *)es2->elements->data[i]; | |
738 | |
739 Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2); | |
740 if (v == EXP_CANT_INTERPRET) | |
741 return EXP_CANT_INTERPRET; | |
742 cmp = v->toInteger(); | |
743 if (cmp == 0) | |
744 break; | |
745 } | |
746 } | |
747 } | |
748 else if (e1->op == TOKarrayliteral && e2->op == TOKstring) | |
749 { // Swap operands and use common code | |
750 Expression *e = e1; | |
751 e1 = e2; | |
752 e2 = e; | |
753 goto Lsa; | |
754 } | |
755 else if (e1->op == TOKstring && e2->op == TOKarrayliteral) | |
756 { | |
757 Lsa: | |
758 StringExp *es1 = (StringExp *)e1; | |
759 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
760 size_t dim1 = es1->len; | |
761 size_t dim2 = es2->elements ? es2->elements->dim : 0; | |
762 if (dim1 != dim2) | |
763 cmp = 0; | |
764 else | |
765 { | |
766 for (size_t i = 0; i < dim1; i++) | |
767 { | |
768 uinteger_t c = es1->charAt(i); | |
769 Expression *ee2 = (Expression *)es2->elements->data[i]; | |
770 if (ee2->isConst() != 1) | |
771 return EXP_CANT_INTERPRET; | |
772 cmp = (c == ee2->toInteger()); | |
773 if (cmp == 0) | |
774 break; | |
775 } | |
776 } | |
777 } | |
778 else if (e1->op == TOKstructliteral && e2->op == TOKstructliteral) | |
779 { StructLiteralExp *es1 = (StructLiteralExp *)e1; | |
780 StructLiteralExp *es2 = (StructLiteralExp *)e2; | |
781 | |
782 if (es1->sd != es2->sd) | |
783 cmp = 0; | |
784 else if ((!es1->elements || !es1->elements->dim) && | |
785 (!es2->elements || !es2->elements->dim)) | |
786 cmp = 1; // both arrays are empty | |
787 else if (!es1->elements || !es2->elements) | |
788 cmp = 0; | |
789 else if (es1->elements->dim != es2->elements->dim) | |
790 cmp = 0; | |
791 else | |
792 { | |
793 cmp = 1; | |
794 for (size_t i = 0; i < es1->elements->dim; i++) | |
795 { Expression *ee1 = (Expression *)es1->elements->data[i]; | |
796 Expression *ee2 = (Expression *)es2->elements->data[i]; | |
797 | |
798 if (ee1 == ee2) | |
799 continue; | |
800 if (!ee1 || !ee2) | |
801 { cmp = 0; | |
802 break; | |
803 } | |
804 Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2); | |
805 if (v == EXP_CANT_INTERPRET) | |
806 return EXP_CANT_INTERPRET; | |
807 cmp = v->toInteger(); | |
808 if (cmp == 0) | |
809 break; | |
810 } | |
811 } | |
812 } | |
813 #if 0 // Should handle this | |
814 else if (e1->op == TOKarrayliteral && e2->op == TOKstring) | |
815 { | |
816 } | |
817 #endif | |
818 else if (e1->isConst() != 1 || e2->isConst() != 1) | |
819 return EXP_CANT_INTERPRET; | |
820 else if (e1->type->isreal()) | |
821 { | |
822 r1 = e1->toReal(); | |
823 r2 = e2->toReal(); | |
824 goto L1; | |
825 } | |
826 else if (e1->type->isimaginary()) | |
827 { | |
828 r1 = e1->toImaginary(); | |
829 r2 = e2->toImaginary(); | |
830 L1: | |
831 #if __DMC__ | |
832 cmp = (r1 == r2); | |
833 #else | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
834 if (Port::isNan(r1) || Port::isNan(r2)) // if unordered |
336 | 835 { |
836 cmp = 0; | |
837 } | |
838 else | |
839 { | |
840 cmp = (r1 == r2); | |
841 } | |
842 #endif | |
843 } | |
844 else if (e1->type->iscomplex()) | |
845 { | |
846 cmp = e1->toComplex() == e2->toComplex(); | |
847 } | |
848 else if (e1->type->isintegral()) | |
849 { | |
850 cmp = (e1->toInteger() == e2->toInteger()); | |
851 } | |
852 else | |
853 return EXP_CANT_INTERPRET; | |
854 if (op == TOKnotequal) | |
855 cmp ^= 1; | |
856 e = new IntegerExp(loc, cmp, type); | |
857 return e; | |
858 } | |
859 | |
860 Expression *Identity(enum TOK op, Type *type, Expression *e1, Expression *e2) | |
861 { Expression *e; | |
862 Loc loc = e1->loc; | |
863 int cmp; | |
864 | |
865 if (e1->op == TOKnull && e2->op == TOKnull) | |
866 { | |
867 cmp = 1; | |
868 } | |
869 else if (e1->op == TOKsymoff && e2->op == TOKsymoff) | |
870 { | |
871 SymOffExp *es1 = (SymOffExp *)e1; | |
872 SymOffExp *es2 = (SymOffExp *)e2; | |
873 | |
874 cmp = (es1->var == es2->var && es1->offset == es2->offset); | |
875 } | |
876 else if (e1->isConst() == 1 && e2->isConst() == 1) | |
877 return Equal((op == TOKidentity) ? TOKequal : TOKnotequal, | |
878 type, e1, e2); | |
879 else | |
880 assert(0); | |
881 if (op == TOKnotidentity) | |
882 cmp ^= 1; | |
883 return new IntegerExp(loc, cmp, type); | |
884 } | |
885 | |
886 | |
887 Expression *Cmp(enum TOK op, Type *type, Expression *e1, Expression *e2) | |
888 { Expression *e; | |
889 Loc loc = e1->loc; | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
890 dinteger_t n; |
336 | 891 real_t r1; |
892 real_t r2; | |
893 | |
894 if (e1->type->isreal()) | |
895 { | |
896 r1 = e1->toReal(); | |
897 r2 = e2->toReal(); | |
898 goto L1; | |
899 } | |
900 else if (e1->type->isimaginary()) | |
901 { | |
902 r1 = e1->toImaginary(); | |
903 r2 = e2->toImaginary(); | |
904 L1: | |
905 #if __DMC__ | |
906 // DMC is the only compiler I know of that handles NAN arguments | |
907 // correctly in comparisons. | |
908 switch (op) | |
909 { | |
910 case TOKlt: n = r1 < r2; break; | |
911 case TOKle: n = r1 <= r2; break; | |
912 case TOKgt: n = r1 > r2; break; | |
913 case TOKge: n = r1 >= r2; break; | |
914 | |
915 case TOKleg: n = r1 <>= r2; break; | |
916 case TOKlg: n = r1 <> r2; break; | |
917 case TOKunord: n = r1 !<>= r2; break; | |
918 case TOKue: n = r1 !<> r2; break; | |
919 case TOKug: n = r1 !<= r2; break; | |
920 case TOKuge: n = r1 !< r2; break; | |
921 case TOKul: n = r1 !>= r2; break; | |
922 case TOKule: n = r1 !> r2; break; | |
923 | |
924 default: | |
925 assert(0); | |
926 } | |
927 #else | |
928 // Don't rely on compiler, handle NAN arguments separately | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
929 if (Port::isNan(r1) || Port::isNan(r2)) // if unordered |
336 | 930 { |
931 switch (op) | |
932 { | |
933 case TOKlt: n = 0; break; | |
934 case TOKle: n = 0; break; | |
935 case TOKgt: n = 0; break; | |
936 case TOKge: n = 0; break; | |
937 | |
938 case TOKleg: n = 0; break; | |
939 case TOKlg: n = 0; break; | |
940 case TOKunord: n = 1; break; | |
941 case TOKue: n = 1; break; | |
942 case TOKug: n = 1; break; | |
943 case TOKuge: n = 1; break; | |
944 case TOKul: n = 1; break; | |
945 case TOKule: n = 1; break; | |
946 | |
947 default: | |
948 assert(0); | |
949 } | |
950 } | |
951 else | |
952 { | |
953 switch (op) | |
954 { | |
955 case TOKlt: n = r1 < r2; break; | |
956 case TOKle: n = r1 <= r2; break; | |
957 case TOKgt: n = r1 > r2; break; | |
958 case TOKge: n = r1 >= r2; break; | |
959 | |
960 case TOKleg: n = 1; break; | |
961 case TOKlg: n = r1 != r2; break; | |
962 case TOKunord: n = 0; break; | |
963 case TOKue: n = r1 == r2; break; | |
964 case TOKug: n = r1 > r2; break; | |
965 case TOKuge: n = r1 >= r2; break; | |
966 case TOKul: n = r1 < r2; break; | |
967 case TOKule: n = r1 <= r2; break; | |
968 | |
969 default: | |
970 assert(0); | |
971 } | |
972 } | |
973 #endif | |
974 } | |
975 else if (e1->type->iscomplex()) | |
976 { | |
977 assert(0); | |
978 } | |
979 else | |
980 { sinteger_t n1; | |
981 sinteger_t n2; | |
982 | |
983 n1 = e1->toInteger(); | |
984 n2 = e2->toInteger(); | |
985 if (e1->type->isunsigned() || e2->type->isunsigned()) | |
986 { | |
987 switch (op) | |
988 { | |
989 case TOKlt: n = ((d_uns64) n1) < ((d_uns64) n2); break; | |
990 case TOKle: n = ((d_uns64) n1) <= ((d_uns64) n2); break; | |
991 case TOKgt: n = ((d_uns64) n1) > ((d_uns64) n2); break; | |
992 case TOKge: n = ((d_uns64) n1) >= ((d_uns64) n2); break; | |
993 | |
994 case TOKleg: n = 1; break; | |
995 case TOKlg: n = ((d_uns64) n1) != ((d_uns64) n2); break; | |
996 case TOKunord: n = 0; break; | |
997 case TOKue: n = ((d_uns64) n1) == ((d_uns64) n2); break; | |
998 case TOKug: n = ((d_uns64) n1) > ((d_uns64) n2); break; | |
999 case TOKuge: n = ((d_uns64) n1) >= ((d_uns64) n2); break; | |
1000 case TOKul: n = ((d_uns64) n1) < ((d_uns64) n2); break; | |
1001 case TOKule: n = ((d_uns64) n1) <= ((d_uns64) n2); break; | |
1002 | |
1003 default: | |
1004 assert(0); | |
1005 } | |
1006 } | |
1007 else | |
1008 { | |
1009 switch (op) | |
1010 { | |
1011 case TOKlt: n = n1 < n2; break; | |
1012 case TOKle: n = n1 <= n2; break; | |
1013 case TOKgt: n = n1 > n2; break; | |
1014 case TOKge: n = n1 >= n2; break; | |
1015 | |
1016 case TOKleg: n = 1; break; | |
1017 case TOKlg: n = n1 != n2; break; | |
1018 case TOKunord: n = 0; break; | |
1019 case TOKue: n = n1 == n2; break; | |
1020 case TOKug: n = n1 > n2; break; | |
1021 case TOKuge: n = n1 >= n2; break; | |
1022 case TOKul: n = n1 < n2; break; | |
1023 case TOKule: n = n1 <= n2; break; | |
1024 | |
1025 default: | |
1026 assert(0); | |
1027 } | |
1028 } | |
1029 } | |
1030 e = new IntegerExp(loc, n, type); | |
1031 return e; | |
1032 } | |
1033 | |
1034 /* Also returns EXP_CANT_INTERPRET if cannot be computed. | |
1035 * to: type to cast to | |
1036 * type: type to paint the result | |
1037 */ | |
1038 | |
1039 Expression *Cast(Type *type, Type *to, Expression *e1) | |
1040 { Expression *e = EXP_CANT_INTERPRET; | |
1041 Loc loc = e1->loc; | |
1042 | |
1043 //printf("Cast(type = %s, to = %s, e1 = %s)\n", type->toChars(), to->toChars(), e1->toChars()); | |
1044 //printf("e1->type = %s\n", e1->type->toChars()); | |
1045 if (type->equals(e1->type) && to->equals(type)) | |
1046 return e1; | |
1047 | |
1333
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1048 Type *tb = to->toBasetype(); |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1049 Type *typeb = type->toBasetype(); |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1050 |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1051 // LDC: ported from D2 to allow char[] ~ char[n] arguments in CTFE |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1052 if (e1->op == TOKstring) |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1053 { |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1054 if (tb->ty == Tarray && typeb->ty == Tarray && |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1055 tb->nextOf()->size() == typeb->nextOf()->size()) |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1056 { |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1057 return expType(to, e1); |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1058 } |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1059 } |
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1060 |
336 | 1061 if (e1->isConst() != 1) |
1062 return EXP_CANT_INTERPRET; | |
1063 | |
1064 if (tb->ty == Tbool) | |
1065 e = new IntegerExp(loc, e1->toInteger() != 0, type); | |
1066 else if (type->isintegral()) | |
1067 { | |
1068 if (e1->type->isfloating()) | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
1069 { dinteger_t result; |
336 | 1070 real_t r = e1->toReal(); |
1071 | |
1333
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1072 switch (typeb->ty) |
336 | 1073 { |
1074 case Tint8: result = (d_int8)r; break; | |
1075 case Tchar: | |
1076 case Tuns8: result = (d_uns8)r; break; | |
1077 case Tint16: result = (d_int16)r; break; | |
1078 case Twchar: | |
1079 case Tuns16: result = (d_uns16)r; break; | |
1080 case Tint32: result = (d_int32)r; break; | |
1081 case Tdchar: | |
1082 case Tuns32: result = (d_uns32)r; break; | |
1083 case Tint64: result = (d_int64)r; break; | |
1084 case Tuns64: result = (d_uns64)r; break; | |
1085 default: | |
1086 assert(0); | |
1087 } | |
1088 | |
1089 e = new IntegerExp(loc, result, type); | |
1090 } | |
1091 else if (type->isunsigned()) | |
1092 e = new IntegerExp(loc, e1->toUInteger(), type); | |
1093 else | |
1094 e = new IntegerExp(loc, e1->toInteger(), type); | |
1095 } | |
1096 else if (tb->isreal()) | |
1097 { real_t value = e1->toReal(); | |
1098 | |
1099 e = new RealExp(loc, value, type); | |
1100 } | |
1101 else if (tb->isimaginary()) | |
1102 { real_t value = e1->toImaginary(); | |
1103 | |
1104 e = new RealExp(loc, value, type); | |
1105 } | |
1106 else if (tb->iscomplex()) | |
1107 { complex_t value = e1->toComplex(); | |
1108 | |
1109 e = new ComplexExp(loc, value, type); | |
1110 } | |
1111 else if (tb->isscalar()) | |
1112 e = new IntegerExp(loc, e1->toInteger(), type); | |
1113 else if (tb->ty == Tvoid) | |
1114 e = EXP_CANT_INTERPRET; | |
1115 else if (tb->ty == Tstruct && e1->op == TOKint64) | |
1116 { // Struct = 0; | |
1117 StructDeclaration *sd = tb->toDsymbol(NULL)->isStructDeclaration(); | |
1118 assert(sd); | |
1119 Expressions *elements = new Expressions; | |
1120 for (size_t i = 0; i < sd->fields.dim; i++) | |
1121 { Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
1122 VarDeclaration *v = s->isVarDeclaration(); | |
1123 assert(v); | |
1124 | |
1125 Expression *exp = new IntegerExp(0); | |
1126 exp = Cast(v->type, v->type, exp); | |
1127 if (exp == EXP_CANT_INTERPRET) | |
1128 return exp; | |
1129 elements->push(exp); | |
1130 } | |
1131 e = new StructLiteralExp(loc, sd, elements); | |
1132 e->type = type; | |
1133 } | |
1134 else | |
1135 { | |
1136 error(loc, "cannot cast %s to %s", e1->type->toChars(), type->toChars()); | |
1333
6bae1c30480f
Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
Christian Kamm <kamm incasoftware de>
parents:
1195
diff
changeset
|
1137 e = new IntegerExp(loc, 0, Type::tint32); |
336 | 1138 } |
1139 return e; | |
1140 } | |
1141 | |
1142 | |
1143 Expression *ArrayLength(Type *type, Expression *e1) | |
1144 { Expression *e; | |
1145 Loc loc = e1->loc; | |
1146 | |
1147 if (e1->op == TOKstring) | |
1148 { StringExp *es1 = (StringExp *)e1; | |
1149 | |
1150 e = new IntegerExp(loc, es1->len, type); | |
1151 } | |
1152 else if (e1->op == TOKarrayliteral) | |
1153 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; | |
1154 size_t dim; | |
1155 | |
1156 dim = ale->elements ? ale->elements->dim : 0; | |
1157 e = new IntegerExp(loc, dim, type); | |
1158 } | |
1159 else if (e1->op == TOKassocarrayliteral) | |
1160 { AssocArrayLiteralExp *ale = (AssocArrayLiteralExp *)e1; | |
1161 size_t dim = ale->keys->dim; | |
1162 | |
1163 e = new IntegerExp(loc, dim, type); | |
1164 } | |
1165 else | |
1166 e = EXP_CANT_INTERPRET; | |
1167 return e; | |
1168 } | |
1169 | |
1170 /* Also return EXP_CANT_INTERPRET if this fails | |
1171 */ | |
1172 Expression *Index(Type *type, Expression *e1, Expression *e2) | |
1173 { Expression *e = EXP_CANT_INTERPRET; | |
1174 Loc loc = e1->loc; | |
1175 | |
1176 //printf("Index(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | |
1177 assert(e1->type); | |
1178 if (e1->op == TOKstring && e2->op == TOKint64) | |
1179 { StringExp *es1 = (StringExp *)e1; | |
1180 uinteger_t i = e2->toInteger(); | |
1181 | |
1182 if (i >= es1->len) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
735
diff
changeset
|
1183 e1->error("string index %ju is out of bounds [0 .. %zu]", i, es1->len); |
336 | 1184 else |
1185 { unsigned value = es1->charAt(i); | |
1186 e = new IntegerExp(loc, value, type); | |
1187 } | |
1188 } | |
1189 else if (e1->type->toBasetype()->ty == Tsarray && e2->op == TOKint64) | |
1190 { TypeSArray *tsa = (TypeSArray *)e1->type->toBasetype(); | |
1191 uinteger_t length = tsa->dim->toInteger(); | |
1192 uinteger_t i = e2->toInteger(); | |
1193 | |
1194 if (i >= length) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
735
diff
changeset
|
1195 { e2->error("array index %ju is out of bounds %s[0 .. %ju]", i, e1->toChars(), length); |
336 | 1196 } |
1197 else if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2)) | |
1198 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; | |
1199 e = (Expression *)ale->elements->data[i]; | |
1200 e->type = type; | |
1201 } | |
1202 } | |
1203 else if (e1->type->toBasetype()->ty == Tarray && e2->op == TOKint64) | |
1204 { | |
1205 uinteger_t i = e2->toInteger(); | |
1206 | |
1207 if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2)) | |
1208 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; | |
1209 if (i >= ale->elements->dim) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
735
diff
changeset
|
1210 { e2->error("array index %ju is out of bounds %s[0 .. %u]", i, e1->toChars(), ale->elements->dim); |
336 | 1211 } |
1212 else | |
1213 { e = (Expression *)ale->elements->data[i]; | |
1214 e->type = type; | |
1215 } | |
1216 } | |
1217 } | |
1218 else if (e1->op == TOKassocarrayliteral && !e1->checkSideEffect(2)) | |
1219 { | |
1220 AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e1; | |
1221 /* Search the keys backwards, in case there are duplicate keys | |
1222 */ | |
1223 for (size_t i = ae->keys->dim; i;) | |
1224 { | |
1225 i--; | |
1226 Expression *ekey = (Expression *)ae->keys->data[i]; | |
1227 Expression *ex = Equal(TOKequal, Type::tbool, ekey, e2); | |
1228 if (ex == EXP_CANT_INTERPRET) | |
1229 return ex; | |
1230 if (ex->isBool(TRUE)) | |
1231 { e = (Expression *)ae->values->data[i]; | |
1232 e->type = type; | |
1233 break; | |
1234 } | |
1235 } | |
1236 } | |
1237 return e; | |
1238 } | |
1239 | |
1240 /* Also return EXP_CANT_INTERPRET if this fails | |
1241 */ | |
1242 Expression *Slice(Type *type, Expression *e1, Expression *lwr, Expression *upr) | |
1243 { Expression *e = EXP_CANT_INTERPRET; | |
1244 Loc loc = e1->loc; | |
1245 | |
1246 #if LOG | |
1247 printf("Slice()\n"); | |
1248 if (lwr) | |
1249 { printf("\te1 = %s\n", e1->toChars()); | |
1250 printf("\tlwr = %s\n", lwr->toChars()); | |
1251 printf("\tupr = %s\n", upr->toChars()); | |
1252 } | |
1253 #endif | |
1254 if (e1->op == TOKstring && lwr->op == TOKint64 && upr->op == TOKint64) | |
1255 { StringExp *es1 = (StringExp *)e1; | |
1256 uinteger_t ilwr = lwr->toInteger(); | |
1257 uinteger_t iupr = upr->toInteger(); | |
1258 | |
1259 if (iupr > es1->len || ilwr > iupr) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
735
diff
changeset
|
1260 e1->error("string slice [%ju .. %ju] is out of bounds", ilwr, iupr); |
336 | 1261 else |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
1262 { dinteger_t value; |
336 | 1263 void *s; |
1264 size_t len = iupr - ilwr; | |
1265 int sz = es1->sz; | |
1266 StringExp *es; | |
1267 | |
1268 s = mem.malloc((len + 1) * sz); | |
1269 memcpy((unsigned char *)s, (unsigned char *)es1->string + ilwr * sz, len * sz); | |
1270 memset((unsigned char *)s + len * sz, 0, sz); | |
1271 | |
1272 es = new StringExp(loc, s, len, es1->postfix); | |
1273 es->sz = sz; | |
1274 es->committed = 1; | |
1275 es->type = type; | |
1276 e = es; | |
1277 } | |
1278 } | |
1279 else if (e1->op == TOKarrayliteral && | |
1280 lwr->op == TOKint64 && upr->op == TOKint64 && | |
1281 !e1->checkSideEffect(2)) | |
1282 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; | |
1283 uinteger_t ilwr = lwr->toInteger(); | |
1284 uinteger_t iupr = upr->toInteger(); | |
1285 | |
1286 if (iupr > es1->elements->dim || ilwr > iupr) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
735
diff
changeset
|
1287 e1->error("array slice [%ju .. %ju] is out of bounds", ilwr, iupr); |
336 | 1288 else |
1289 { | |
1290 Expressions *elements = new Expressions(); | |
1291 elements->setDim(iupr - ilwr); | |
1292 memcpy(elements->data, | |
1293 es1->elements->data + ilwr, | |
1294 (iupr - ilwr) * sizeof(es1->elements->data[0])); | |
1295 e = new ArrayLiteralExp(e1->loc, elements); | |
1296 e->type = type; | |
1297 } | |
1298 } | |
1299 return e; | |
1300 } | |
1301 | |
1302 /* Also return EXP_CANT_INTERPRET if this fails | |
1303 */ | |
1304 Expression *Cat(Type *type, Expression *e1, Expression *e2) | |
1305 { Expression *e = EXP_CANT_INTERPRET; | |
1306 Loc loc = e1->loc; | |
1307 Type *t; | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1308 Type *t1 = e1->type->toBasetype(); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1309 Type *t2 = e2->type->toBasetype(); |
336 | 1310 |
1311 //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
1312 //printf("\tt1 = %s, t2 = %s\n", t1->toChars(), t2->toChars()); |
336 | 1313 |
1314 if (e1->op == TOKnull && (e2->op == TOKint64 || e2->op == TOKstructliteral)) | |
1315 { e = e2; | |
1316 goto L2; | |
1317 } | |
1318 else if ((e1->op == TOKint64 || e1->op == TOKstructliteral) && e2->op == TOKnull) | |
1319 { e = e1; | |
1320 L2: | |
1321 Type *tn = e->type->toBasetype(); | |
1322 if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar) | |
1323 { | |
1324 // Create a StringExp | |
1325 void *s; | |
1326 StringExp *es; | |
1327 size_t len = 1; | |
1328 int sz = tn->size(); | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
1329 dinteger_t v = e->toInteger(); |
336 | 1330 |
1331 s = mem.malloc((len + 1) * sz); | |
1332 memcpy((unsigned char *)s, &v, sz); | |
1333 | |
1334 // Add terminating 0 | |
1335 memset((unsigned char *)s + len * sz, 0, sz); | |
1336 | |
1337 es = new StringExp(loc, s, len); | |
1338 es->sz = sz; | |
1339 es->committed = 1; | |
1340 e = es; | |
1341 } | |
1342 else | |
1343 { // Create an ArrayLiteralExp | |
1344 Expressions *elements = new Expressions(); | |
1345 elements->push(e); | |
1346 e = new ArrayLiteralExp(e->loc, elements); | |
1347 } | |
1348 e->type = type; | |
1349 return e; | |
1350 } | |
1351 else if (e1->op == TOKstring && e2->op == TOKstring) | |
1352 { | |
1353 // Concatenate the strings | |
1354 void *s; | |
1355 StringExp *es1 = (StringExp *)e1; | |
1356 StringExp *es2 = (StringExp *)e2; | |
1357 StringExp *es; | |
1358 Type *t; | |
1359 size_t len = es1->len + es2->len; | |
1360 int sz = es1->sz; | |
1361 | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1362 if (sz != es2->sz) |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1363 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1364 /* Can happen with: |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1365 * auto s = "foo"d ~ "bar"c; |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1366 */ |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1367 assert(global.errors); |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1368 return e; |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1369 } |
336 | 1370 s = mem.malloc((len + 1) * sz); |
1371 memcpy(s, es1->string, es1->len * sz); | |
1372 memcpy((unsigned char *)s + es1->len * sz, es2->string, es2->len * sz); | |
1373 | |
1374 // Add terminating 0 | |
1375 memset((unsigned char *)s + len * sz, 0, sz); | |
1376 | |
1377 es = new StringExp(loc, s, len); | |
1378 es->sz = sz; | |
1379 es->committed = es1->committed | es2->committed; | |
1380 if (es1->committed) | |
1381 t = es1->type; | |
1382 else | |
1383 t = es2->type; | |
1384 es->type = type; | |
1385 e = es; | |
1386 } | |
1387 else if (e1->op == TOKstring && e2->op == TOKint64) | |
1388 { | |
1389 // Concatenate the strings | |
1390 void *s; | |
1391 StringExp *es1 = (StringExp *)e1; | |
1392 StringExp *es; | |
1393 Type *t; | |
1394 size_t len = es1->len + 1; | |
1395 int sz = es1->sz; | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
1396 dinteger_t v = e2->toInteger(); |
336 | 1397 |
1398 s = mem.malloc((len + 1) * sz); | |
1399 memcpy(s, es1->string, es1->len * sz); | |
1400 memcpy((unsigned char *)s + es1->len * sz, &v, sz); | |
1401 | |
1402 // Add terminating 0 | |
1403 memset((unsigned char *)s + len * sz, 0, sz); | |
1404 | |
1405 es = new StringExp(loc, s, len); | |
1406 es->sz = sz; | |
1407 es->committed = es1->committed; | |
1408 t = es1->type; | |
1409 es->type = type; | |
1410 e = es; | |
1411 } | |
1412 else if (e1->op == TOKint64 && e2->op == TOKstring) | |
1413 { | |
1414 // Concatenate the strings | |
1415 void *s; | |
1416 StringExp *es2 = (StringExp *)e2; | |
1417 StringExp *es; | |
1418 Type *t; | |
1419 size_t len = 1 + es2->len; | |
1420 int sz = es2->sz; | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1103
diff
changeset
|
1421 dinteger_t v = e1->toInteger(); |
336 | 1422 |
1423 s = mem.malloc((len + 1) * sz); | |
1424 memcpy((unsigned char *)s, &v, sz); | |
1425 memcpy((unsigned char *)s + sz, es2->string, es2->len * sz); | |
1426 | |
1427 // Add terminating 0 | |
1428 memset((unsigned char *)s + len * sz, 0, sz); | |
1429 | |
1430 es = new StringExp(loc, s, len); | |
1431 es->sz = sz; | |
1432 es->committed = es2->committed; | |
1433 t = es2->type; | |
1434 es->type = type; | |
1435 e = es; | |
1436 } | |
1437 else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral && | |
1438 e1->type->equals(e2->type)) | |
1439 { | |
1440 // Concatenate the arrays | |
1441 ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; | |
1442 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
1443 | |
1444 es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy()); | |
1445 es1->elements->insert(es1->elements->dim, es2->elements); | |
1446 e = es1; | |
1447 | |
1448 if (type->toBasetype()->ty == Tsarray) | |
1449 { | |
1450 e->type = new TypeSArray(e1->type->toBasetype()->next, new IntegerExp(0, es1->elements->dim, Type::tindex)); | |
1451 e->type = e->type->semantic(loc, NULL); | |
1452 } | |
1453 else | |
1454 e->type = type; | |
1455 } | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1456 else if (e1->op == TOKarrayliteral && e2->op == TOKnull && |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1457 t1->nextOf()->equals(t2->nextOf())) |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1458 { |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1459 e = e1; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1460 goto L3; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1461 } |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1462 else if (e1->op == TOKnull && e2->op == TOKarrayliteral && |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1463 t1->nextOf()->equals(t2->nextOf())) |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1464 { |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1465 e = e2; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1466 L3: |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1467 // Concatenate the array with null |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1468 ArrayLiteralExp *es = (ArrayLiteralExp *)e; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1469 |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1470 es = new ArrayLiteralExp(es->loc, (Expressions *)es->elements->copy()); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1471 e = es; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1472 |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1473 if (type->toBasetype()->ty == Tsarray) |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1474 { |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1475 e->type = new TypeSArray(t1->nextOf(), new IntegerExp(loc, es->elements->dim, Type::tindex)); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1476 e->type = e->type->semantic(loc, NULL); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1477 } |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1478 else |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1479 e->type = type; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
658
diff
changeset
|
1480 } |
336 | 1481 else if ((e1->op == TOKarrayliteral || e1->op == TOKnull) && |
1482 e1->type->toBasetype()->nextOf()->equals(e2->type)) | |
1483 { | |
1484 ArrayLiteralExp *es1; | |
1485 if (e1->op == TOKarrayliteral) | |
1486 { es1 = (ArrayLiteralExp *)e1; | |
1487 es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy()); | |
1488 es1->elements->push(e2); | |
1489 } | |
1490 else | |
1491 { | |
1492 es1 = new ArrayLiteralExp(e1->loc, e2); | |
1493 } | |
1494 e = es1; | |
1495 | |
1496 if (type->toBasetype()->ty == Tsarray) | |
1497 { | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
1498 e->type = new TypeSArray(e2->type, new IntegerExp(loc, es1->elements->dim, Type::tindex)); |
336 | 1499 e->type = e->type->semantic(loc, NULL); |
1500 } | |
1501 else | |
1502 e->type = type; | |
1503 } | |
1504 else if (e2->op == TOKarrayliteral && | |
1505 e2->type->toBasetype()->nextOf()->equals(e1->type)) | |
1506 { | |
1507 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
1508 | |
1509 es2 = new ArrayLiteralExp(es2->loc, (Expressions *)es2->elements->copy()); | |
1510 es2->elements->shift(e1); | |
1511 e = es2; | |
1512 | |
1513 if (type->toBasetype()->ty == Tsarray) | |
1514 { | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1333
diff
changeset
|
1515 e->type = new TypeSArray(e1->type, new IntegerExp(loc, es2->elements->dim, Type::tindex)); |
336 | 1516 e->type = e->type->semantic(loc, NULL); |
1517 } | |
1518 else | |
1519 e->type = type; | |
1520 } | |
1521 else if (e1->op == TOKnull && e2->op == TOKstring) | |
1522 { | |
1523 t = e1->type; | |
1524 e = e2; | |
1525 goto L1; | |
1526 } | |
1527 else if (e1->op == TOKstring && e2->op == TOKnull) | |
1528 { e = e1; | |
1529 t = e2->type; | |
1530 L1: | |
1531 Type *tb = t->toBasetype(); | |
1532 if (tb->ty == Tarray && tb->nextOf()->equals(e->type)) | |
1533 { Expressions *expressions = new Expressions(); | |
1534 expressions->push(e); | |
1535 e = new ArrayLiteralExp(loc, expressions); | |
1536 e->type = t; | |
1537 } | |
1538 if (!e->type->equals(type)) | |
1539 { StringExp *se = (StringExp *)e->copy(); | |
1540 e = se->castTo(NULL, type); | |
1541 } | |
1542 } | |
1543 return e; | |
1544 } | |
1545 | |
1546 Expression *Ptr(Type *type, Expression *e1) | |
1547 { | |
1548 //printf("Ptr(e1 = %s)\n", e1->toChars()); | |
1549 if (e1->op == TOKadd) | |
1550 { AddExp *ae = (AddExp *)e1; | |
1551 if (ae->e1->op == TOKaddress && ae->e2->op == TOKint64) | |
1552 { AddrExp *ade = (AddrExp *)ae->e1; | |
1553 if (ade->e1->op == TOKstructliteral) | |
1554 { StructLiteralExp *se = (StructLiteralExp *)ade->e1; | |
1555 unsigned offset = ae->e2->toInteger(); | |
1556 Expression *e = se->getField(type, offset); | |
1557 if (!e) | |
1558 e = EXP_CANT_INTERPRET; | |
1559 return e; | |
1560 } | |
1561 } | |
1562 } | |
1563 return EXP_CANT_INTERPRET; | |
1564 } | |
1565 |