1
|
1
|
|
2 // Copyright (c) 1999-2006 by Digital Mars
|
|
3 // All Rights Reserved
|
|
4 // written by Walter Bright
|
|
5 // http://www.digitalmars.com
|
|
6 // License for redistribution is by either the Artistic License
|
|
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
8 // See the included readme.txt for details.
|
|
9
|
|
10 #include <stdio.h>
|
|
11 #include <assert.h>
|
|
12
|
|
13 #if _WIN32 || IN_GCC || IN_LLVM
|
|
14 #include "mem.h"
|
|
15 #else
|
|
16 #include "../root/mem.h"
|
|
17 #endif
|
|
18
|
|
19 #include "expression.h"
|
|
20 #include "mtype.h"
|
|
21 #include "utf.h"
|
|
22 #include "declaration.h"
|
|
23 #include "aggregate.h"
|
|
24
|
|
25 /* ==================== implicitCast ====================== */
|
|
26
|
|
27 /**************************************
|
|
28 * Do an implicit cast.
|
|
29 * Issue error if it can't be done.
|
|
30 */
|
|
31
|
|
32 Expression *Expression::implicitCastTo(Scope *sc, Type *t)
|
|
33 {
|
|
34 //printf("implicitCastTo(%s) => %s\n", type->toChars(), t->toChars());
|
|
35 if (implicitConvTo(t))
|
|
36 {
|
|
37 if (global.params.warnings &&
|
|
38 Type::impcnvWarn[type->toBasetype()->ty][t->toBasetype()->ty] &&
|
|
39 op != TOKint64)
|
|
40 {
|
|
41 Expression *e = optimize(WANTflags | WANTvalue);
|
|
42
|
|
43 if (e->op == TOKint64)
|
|
44 return e->implicitCastTo(sc, t);
|
|
45
|
|
46 fprintf(stdmsg, "warning - ");
|
|
47 error("implicit conversion of expression (%s) of type %s to %s can cause loss of data",
|
|
48 toChars(), type->toChars(), t->toChars());
|
|
49 }
|
|
50 return castTo(sc, t);
|
|
51 }
|
|
52
|
|
53 Expression *e = optimize(WANTflags | WANTvalue);
|
|
54 if (e != this)
|
|
55 return e->implicitCastTo(sc, t);
|
|
56
|
|
57 #if 0
|
|
58 print();
|
|
59 type->print();
|
|
60 printf("to:\n");
|
|
61 t->print();
|
|
62 printf("%p %p type: %s to: %s\n", type->deco, t->deco, type->deco, t->deco);
|
|
63 //printf("%p %p %p\n", type->next->arrayOf(), type, t);
|
|
64 fflush(stdout);
|
|
65 #endif
|
|
66 if (!t->deco)
|
|
67 { /* Can happen with:
|
|
68 * enum E { One }
|
|
69 * class A
|
|
70 * { static void fork(EDG dg) { dg(E.One); }
|
|
71 * alias void delegate(E) EDG;
|
|
72 * }
|
|
73 * Should eventually make it work.
|
|
74 */
|
|
75 error("forward reference to type %s", t->toChars());
|
|
76 }
|
|
77 else if (t->reliesOnTident())
|
|
78 error("forward reference to type %s", t->reliesOnTident()->toChars());
|
|
79
|
|
80 error("cannot implicitly convert expression (%s) of type %s to %s",
|
|
81 toChars(), type->toChars(), t->toChars());
|
|
82 return castTo(sc, t);
|
|
83 }
|
|
84
|
|
85 /*******************************************
|
|
86 * Return !=0 if we can implicitly convert this to type t.
|
|
87 * Don't do the actual cast.
|
|
88 */
|
|
89
|
|
90 MATCH Expression::implicitConvTo(Type *t)
|
|
91 {
|
|
92 #if 0
|
|
93 printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n",
|
|
94 toChars(), type->toChars(), t->toChars());
|
|
95 #endif
|
|
96 if (!type)
|
|
97 { error("%s is not an expression", toChars());
|
|
98 type = Type::terror;
|
|
99 }
|
|
100 if (t->ty == Tbit && isBit())
|
|
101 return MATCHconvert;
|
|
102 Expression *e = optimize(WANTvalue | WANTflags);
|
|
103 if (e != this)
|
|
104 { //printf("optimzed to %s\n", e->toChars());
|
|
105 return e->implicitConvTo(t);
|
|
106 }
|
|
107 MATCH match = type->implicitConvTo(t);
|
|
108 if (match)
|
|
109 return match;
|
|
110 #if 0
|
|
111 Type *tb = t->toBasetype();
|
|
112 if (tb->ty == Tdelegate)
|
|
113 { TypeDelegate *td = (TypeDelegate *)tb;
|
|
114 TypeFunction *tf = (TypeFunction *)td->next;
|
|
115
|
|
116 if (!tf->varargs &&
|
|
117 !(tf->arguments && tf->arguments->dim)
|
|
118 )
|
|
119 {
|
|
120 match = type->implicitConvTo(tf->next);
|
|
121 if (match)
|
|
122 return match;
|
|
123 if (tf->next->toBasetype()->ty == Tvoid)
|
|
124 return MATCHconvert;
|
|
125 }
|
|
126 }
|
|
127 #endif
|
|
128 return MATCHnomatch;
|
|
129 }
|
|
130
|
|
131
|
|
132 MATCH IntegerExp::implicitConvTo(Type *t)
|
|
133 {
|
|
134 #if 0
|
|
135 printf("IntegerExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
|
|
136 toChars(), type->toChars(), t->toChars());
|
|
137 #endif
|
|
138 if (type->equals(t))
|
|
139 return MATCHexact;
|
|
140
|
|
141 enum TY ty = type->toBasetype()->ty;
|
|
142 enum TY toty = t->toBasetype()->ty;
|
|
143
|
|
144 if (type->implicitConvTo(t) == MATCHnomatch && t->ty == Tenum)
|
|
145 {
|
|
146 return MATCHnomatch;
|
|
147 }
|
|
148
|
|
149 switch (ty)
|
|
150 {
|
|
151 case Tbit:
|
|
152 case Tbool:
|
|
153 value &= 1;
|
|
154 ty = Tint32;
|
|
155 break;
|
|
156
|
|
157 case Tint8:
|
|
158 value = (signed char)value;
|
|
159 ty = Tint32;
|
|
160 break;
|
|
161
|
|
162 case Tchar:
|
|
163 case Tuns8:
|
|
164 value &= 0xFF;
|
|
165 ty = Tint32;
|
|
166 break;
|
|
167
|
|
168 case Tint16:
|
|
169 value = (short)value;
|
|
170 ty = Tint32;
|
|
171 break;
|
|
172
|
|
173 case Tuns16:
|
|
174 case Twchar:
|
|
175 value &= 0xFFFF;
|
|
176 ty = Tint32;
|
|
177 break;
|
|
178
|
|
179 case Tint32:
|
|
180 value = (int)value;
|
|
181 break;
|
|
182
|
|
183 case Tuns32:
|
|
184 case Tdchar:
|
|
185 value &= 0xFFFFFFFF;
|
|
186 ty = Tuns32;
|
|
187 break;
|
|
188
|
|
189 default:
|
|
190 break;
|
|
191 }
|
|
192
|
|
193 // Only allow conversion if no change in value
|
|
194 switch (toty)
|
|
195 {
|
|
196 case Tbit:
|
|
197 case Tbool:
|
|
198 if ((value & 1) != value)
|
|
199 goto Lno;
|
|
200 goto Lyes;
|
|
201
|
|
202 case Tint8:
|
|
203 if ((signed char)value != value)
|
|
204 goto Lno;
|
|
205 goto Lyes;
|
|
206
|
|
207 case Tchar:
|
|
208 case Tuns8:
|
|
209 //printf("value = %llu %llu\n", (integer_t)(unsigned char)value, value);
|
|
210 if ((unsigned char)value != value)
|
|
211 goto Lno;
|
|
212 goto Lyes;
|
|
213
|
|
214 case Tint16:
|
|
215 if ((short)value != value)
|
|
216 goto Lno;
|
|
217 goto Lyes;
|
|
218
|
|
219 case Tuns16:
|
|
220 if ((unsigned short)value != value)
|
|
221 goto Lno;
|
|
222 goto Lyes;
|
|
223
|
|
224 case Tint32:
|
|
225 if (ty == Tuns32)
|
|
226 {
|
|
227 }
|
|
228 else if ((int)value != value)
|
|
229 goto Lno;
|
|
230 goto Lyes;
|
|
231
|
|
232 case Tuns32:
|
|
233 if (ty == Tint32)
|
|
234 {
|
|
235 }
|
|
236 else if ((unsigned)value != value)
|
|
237 goto Lno;
|
|
238 goto Lyes;
|
|
239
|
|
240 case Tdchar:
|
|
241 if (value > 0x10FFFFUL)
|
|
242 goto Lno;
|
|
243 goto Lyes;
|
|
244
|
|
245 case Twchar:
|
|
246 if ((unsigned short)value != value)
|
|
247 goto Lno;
|
|
248 goto Lyes;
|
|
249
|
|
250 case Tfloat32:
|
|
251 {
|
|
252 volatile float f;
|
|
253 if (type->isunsigned())
|
|
254 {
|
|
255 f = (float)value;
|
|
256 if (f != value)
|
|
257 goto Lno;
|
|
258 }
|
|
259 else
|
|
260 {
|
|
261 f = (float)(long long)value;
|
|
262 if (f != (long long)value)
|
|
263 goto Lno;
|
|
264 }
|
|
265 goto Lyes;
|
|
266 }
|
|
267
|
|
268 case Tfloat64:
|
|
269 {
|
|
270 volatile double f;
|
|
271 if (type->isunsigned())
|
|
272 {
|
|
273 f = (double)value;
|
|
274 if (f != value)
|
|
275 goto Lno;
|
|
276 }
|
|
277 else
|
|
278 {
|
|
279 f = (double)(long long)value;
|
|
280 if (f != (long long)value)
|
|
281 goto Lno;
|
|
282 }
|
|
283 goto Lyes;
|
|
284 }
|
|
285
|
|
286 case Tfloat80:
|
|
287 {
|
|
288 volatile long double f;
|
|
289 if (type->isunsigned())
|
|
290 {
|
|
291 f = (long double)value;
|
|
292 if (f != value)
|
|
293 goto Lno;
|
|
294 }
|
|
295 else
|
|
296 {
|
|
297 f = (long double)(long long)value;
|
|
298 if (f != (long long)value)
|
|
299 goto Lno;
|
|
300 }
|
|
301 goto Lyes;
|
|
302 }
|
|
303 }
|
|
304 return Expression::implicitConvTo(t);
|
|
305
|
|
306 Lyes:
|
|
307 //printf("MATCHconvert\n");
|
|
308 return MATCHconvert;
|
|
309
|
|
310 Lno:
|
|
311 //printf("MATCHnomatch\n");
|
|
312 return MATCHnomatch;
|
|
313 }
|
|
314
|
|
315 MATCH NullExp::implicitConvTo(Type *t)
|
|
316 {
|
|
317 #if 0
|
|
318 printf("NullExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
|
|
319 toChars(), type->toChars(), t->toChars());
|
|
320 #endif
|
|
321 if (this->type->equals(t))
|
|
322 return MATCHexact;
|
|
323 // NULL implicitly converts to any pointer type or dynamic array
|
|
324 if (type->ty == Tpointer && type->next->ty == Tvoid)
|
|
325 {
|
|
326 if (t->ty == Ttypedef)
|
|
327 t = ((TypeTypedef *)t)->sym->basetype;
|
|
328 if (t->ty == Tpointer || t->ty == Tarray ||
|
|
329 t->ty == Taarray || t->ty == Tclass ||
|
|
330 t->ty == Tdelegate)
|
|
331 return committed ? MATCHconvert : MATCHexact;
|
|
332 }
|
|
333 return Expression::implicitConvTo(t);
|
|
334 }
|
|
335
|
|
336 MATCH StringExp::implicitConvTo(Type *t)
|
|
337 { MATCH m;
|
|
338
|
|
339 #if 0
|
|
340 printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
|
|
341 toChars(), committed, type->toChars(), t->toChars());
|
|
342 #endif
|
|
343 if (!committed)
|
|
344 {
|
|
345 if (!committed && t->ty == Tpointer && t->next->ty == Tvoid)
|
|
346 {
|
|
347 return MATCHnomatch;
|
|
348 }
|
|
349 if (type->ty == Tsarray || type->ty == Tarray || type->ty == Tpointer)
|
|
350 {
|
|
351 if (type->next->ty == Tchar)
|
|
352 {
|
|
353 switch (t->ty)
|
|
354 {
|
|
355 case Tsarray:
|
|
356 if (type->ty == Tsarray &&
|
|
357 ((TypeSArray *)type)->dim->toInteger() !=
|
|
358 ((TypeSArray *)t)->dim->toInteger())
|
|
359 return MATCHnomatch;
|
|
360 goto L1;
|
|
361 case Tarray:
|
|
362 goto L1;
|
|
363 case Tpointer:
|
|
364 L1:
|
|
365 if (t->next->ty == Tchar)
|
|
366 return MATCHexact;
|
|
367 else if (t->next->ty == Twchar)
|
|
368 return MATCHexact;
|
|
369 else if (t->next->ty == Tdchar)
|
|
370 return MATCHexact;
|
|
371 break;
|
|
372 }
|
|
373 }
|
|
374 }
|
|
375 }
|
|
376 return Expression::implicitConvTo(t);
|
|
377 #if 0
|
|
378 m = (MATCH)type->implicitConvTo(t);
|
|
379 if (m)
|
|
380 {
|
|
381 return m;
|
|
382 }
|
|
383
|
|
384 return MATCHnomatch;
|
|
385 #endif
|
|
386 }
|
|
387
|
|
388 MATCH ArrayLiteralExp::implicitConvTo(Type *t)
|
|
389 { MATCH result = MATCHexact;
|
|
390
|
|
391 Type *typeb = type->toBasetype();
|
|
392 Type *tb = t->toBasetype();
|
|
393 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
|
|
394 (typeb->ty == Tarray || typeb->ty == Tsarray))
|
|
395 {
|
|
396 if (tb->ty == Tsarray)
|
|
397 { TypeSArray *tsa = (TypeSArray *)tb;
|
|
398 if (elements->dim != tsa->dim->toInteger())
|
|
399 result = MATCHnomatch;
|
|
400 }
|
|
401
|
|
402 for (int i = 0; i < elements->dim; i++)
|
|
403 { Expression *e = (Expression *)elements->data[i];
|
|
404 MATCH m = (MATCH)e->implicitConvTo(tb->next);
|
|
405 if (m < result)
|
|
406 result = m; // remember worst match
|
|
407 if (result == MATCHnomatch)
|
|
408 break; // no need to check for worse
|
|
409 }
|
|
410 return result;
|
|
411 }
|
|
412 else
|
|
413 return Expression::implicitConvTo(t);
|
|
414 }
|
|
415
|
|
416 MATCH AssocArrayLiteralExp::implicitConvTo(Type *t)
|
|
417 { MATCH result = MATCHexact;
|
|
418
|
|
419 Type *typeb = type->toBasetype();
|
|
420 Type *tb = t->toBasetype();
|
|
421 if (tb->ty == Taarray && typeb->ty == Taarray)
|
|
422 {
|
|
423 for (size_t i = 0; i < keys->dim; i++)
|
|
424 { Expression *e = (Expression *)keys->data[i];
|
|
425 MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->key);
|
|
426 if (m < result)
|
|
427 result = m; // remember worst match
|
|
428 if (result == MATCHnomatch)
|
|
429 break; // no need to check for worse
|
|
430 e = (Expression *)values->data[i];
|
|
431 m = (MATCH)e->implicitConvTo(tb->next);
|
|
432 if (m < result)
|
|
433 result = m; // remember worst match
|
|
434 if (result == MATCHnomatch)
|
|
435 break; // no need to check for worse
|
|
436 }
|
|
437 return result;
|
|
438 }
|
|
439 else
|
|
440 return Expression::implicitConvTo(t);
|
|
441 }
|
|
442
|
|
443 MATCH AddrExp::implicitConvTo(Type *t)
|
|
444 {
|
|
445 #if 0
|
|
446 printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
|
|
447 toChars(), type->toChars(), t->toChars());
|
|
448 #endif
|
|
449 MATCH result;
|
|
450
|
|
451 result = type->implicitConvTo(t);
|
|
452 //printf("\tresult = %d\n", result);
|
|
453
|
|
454 if (result == MATCHnomatch)
|
|
455 {
|
|
456 // Look for pointers to functions where the functions are overloaded.
|
|
457 VarExp *ve;
|
|
458 FuncDeclaration *f;
|
|
459
|
|
460 t = t->toBasetype();
|
|
461 if (type->ty == Tpointer && type->next->ty == Tfunction &&
|
|
462 t->ty == Tpointer && t->next->ty == Tfunction &&
|
|
463 e1->op == TOKvar)
|
|
464 {
|
|
465 ve = (VarExp *)e1;
|
|
466 f = ve->var->isFuncDeclaration();
|
|
467 if (f && f->overloadExactMatch(t->next))
|
|
468 result = MATCHexact;
|
|
469 }
|
|
470 }
|
|
471 //printf("\tresult = %d\n", result);
|
|
472 return result;
|
|
473 }
|
|
474
|
|
475 MATCH SymOffExp::implicitConvTo(Type *t)
|
|
476 {
|
|
477 #if 0
|
|
478 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
|
|
479 toChars(), type->toChars(), t->toChars());
|
|
480 #endif
|
|
481 MATCH result;
|
|
482
|
|
483 result = type->implicitConvTo(t);
|
|
484 //printf("\tresult = %d\n", result);
|
|
485
|
|
486 if (result == MATCHnomatch)
|
|
487 {
|
|
488 // Look for pointers to functions where the functions are overloaded.
|
|
489 FuncDeclaration *f;
|
|
490
|
|
491 t = t->toBasetype();
|
|
492 if (type->ty == Tpointer && type->next->ty == Tfunction &&
|
|
493 t->ty == Tpointer && t->next->ty == Tfunction)
|
|
494 {
|
|
495 f = var->isFuncDeclaration();
|
|
496 if (f && f->overloadExactMatch(t->next))
|
|
497 result = MATCHexact;
|
|
498 }
|
|
499 }
|
|
500 //printf("\tresult = %d\n", result);
|
|
501 return result;
|
|
502 }
|
|
503
|
|
504 MATCH DelegateExp::implicitConvTo(Type *t)
|
|
505 {
|
|
506 #if 0
|
|
507 printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
|
|
508 toChars(), type->toChars(), t->toChars());
|
|
509 #endif
|
|
510 MATCH result;
|
|
511
|
|
512 result = type->implicitConvTo(t);
|
|
513
|
|
514 if (result == 0)
|
|
515 {
|
|
516 // Look for pointers to functions where the functions are overloaded.
|
|
517 FuncDeclaration *f;
|
|
518
|
|
519 t = t->toBasetype();
|
|
520 if (type->ty == Tdelegate && type->next->ty == Tfunction &&
|
|
521 t->ty == Tdelegate && t->next->ty == Tfunction)
|
|
522 {
|
|
523 if (func && func->overloadExactMatch(t->next))
|
|
524 result = MATCHexact;
|
|
525 }
|
|
526 }
|
|
527 return result;
|
|
528 }
|
|
529
|
|
530 MATCH CondExp::implicitConvTo(Type *t)
|
|
531 {
|
|
532 MATCH m1;
|
|
533 MATCH m2;
|
|
534
|
|
535 m1 = e1->implicitConvTo(t);
|
|
536 m2 = e2->implicitConvTo(t);
|
|
537
|
|
538 // Pick the worst match
|
|
539 return (m1 < m2) ? m1 : m2;
|
|
540 }
|
|
541
|
|
542
|
|
543 /* ==================== castTo ====================== */
|
|
544
|
|
545 /**************************************
|
|
546 * Do an explicit cast.
|
|
547 */
|
|
548
|
|
549 Expression *Expression::castTo(Scope *sc, Type *t)
|
|
550 { Expression *e;
|
|
551 Type *tb;
|
|
552
|
|
553 //printf("Expression::castTo(this=%s, t=%s)\n", toChars(), t->toChars());
|
|
554 #if 0
|
|
555 printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
|
|
556 toChars(), type->toChars(), t->toChars());
|
|
557 #endif
|
|
558 e = this;
|
|
559 tb = t->toBasetype();
|
|
560 type = type->toBasetype();
|
|
561 if (tb != type)
|
|
562 {
|
|
563 if (tb->ty == Tbit && isBit())
|
|
564 ;
|
|
565
|
|
566 // Do (type *) cast of (type [dim])
|
|
567 else if (tb->ty == Tpointer &&
|
|
568 type->ty == Tsarray
|
|
569 )
|
|
570 {
|
|
571 //printf("Converting [dim] to *\n");
|
|
572
|
|
573 if (type->size(loc) == 0)
|
|
574 e = new NullExp(loc);
|
|
575 else
|
|
576 e = new AddrExp(loc, e);
|
|
577 }
|
|
578 #if 0
|
|
579 else if (tb->ty == Tdelegate && type->ty != Tdelegate)
|
|
580 {
|
|
581 TypeDelegate *td = (TypeDelegate *)tb;
|
|
582 TypeFunction *tf = (TypeFunction *)td->next;
|
|
583 return toDelegate(sc, tf->next);
|
|
584 }
|
|
585 #endif
|
|
586 else
|
|
587 {
|
|
588 e = new CastExp(loc, e, tb);
|
|
589 }
|
|
590 }
|
|
591 e->type = t;
|
|
592 //printf("Returning: %s\n", e->toChars());
|
|
593 return e;
|
|
594 }
|
|
595
|
|
596
|
|
597 Expression *RealExp::castTo(Scope *sc, Type *t)
|
|
598 {
|
|
599 if (type->isreal() && t->isreal())
|
|
600 type = t;
|
|
601 else if (type->isimaginary() && t->isimaginary())
|
|
602 type = t;
|
|
603 else
|
|
604 return Expression::castTo(sc, t);
|
|
605 return this;
|
|
606 }
|
|
607
|
|
608
|
|
609 Expression *ComplexExp::castTo(Scope *sc, Type *t)
|
|
610 {
|
|
611 if (type->iscomplex() && t->iscomplex())
|
|
612 type = t;
|
|
613 else
|
|
614 return Expression::castTo(sc, t);
|
|
615 return this;
|
|
616 }
|
|
617
|
|
618
|
|
619 Expression *NullExp::castTo(Scope *sc, Type *t)
|
|
620 { Expression *e;
|
|
621 Type *tb;
|
|
622
|
|
623 //printf("NullExp::castTo(t = %p)\n", t);
|
|
624 committed = 1;
|
|
625 e = this;
|
|
626 tb = t->toBasetype();
|
|
627 type = type->toBasetype();
|
|
628 if (tb != type)
|
|
629 {
|
|
630 // NULL implicitly converts to any pointer type or dynamic array
|
|
631 if (type->ty == Tpointer && type->next->ty == Tvoid &&
|
|
632 (tb->ty == Tpointer || tb->ty == Tarray || tb->ty == Taarray ||
|
|
633 tb->ty == Tdelegate))
|
|
634 {
|
|
635 #if 0
|
|
636 if (tb->ty == Tdelegate)
|
|
637 { TypeDelegate *td = (TypeDelegate *)tb;
|
|
638 TypeFunction *tf = (TypeFunction *)td->next;
|
|
639
|
|
640 if (!tf->varargs &&
|
|
641 !(tf->arguments && tf->arguments->dim)
|
|
642 )
|
|
643 {
|
|
644 return Expression::castTo(sc, t);
|
|
645 }
|
|
646 }
|
|
647 #endif
|
|
648 }
|
|
649 else
|
|
650 {
|
|
651 return Expression::castTo(sc, t);
|
|
652 //e = new CastExp(loc, e, tb);
|
|
653 }
|
|
654 }
|
|
655 e->type = t;
|
|
656 return e;
|
|
657 }
|
|
658
|
|
659 Expression *StringExp::castTo(Scope *sc, Type *t)
|
|
660 {
|
19
|
661 /* This follows copy-on-write; any changes to 'this'
|
|
662 * will result in a copy.
|
|
663 * The this->string member is considered immutable.
|
|
664 */
|
1
|
665 StringExp *se;
|
|
666 Type *tb;
|
19
|
667 int copied = 0;
|
1
|
668
|
|
669 //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed);
|
|
670
|
19
|
671 if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
|
1
|
672 {
|
|
673 error("cannot convert string literal to void*");
|
|
674 }
|
|
675
|
19
|
676 se = this;
|
|
677 if (!committed)
|
|
678 { se = (StringExp *)copy();
|
|
679 se->committed = 1;
|
|
680 copied = 1;
|
|
681 }
|
|
682
|
|
683 if (type == t)
|
|
684 {
|
|
685 return se;
|
|
686 }
|
|
687
|
1
|
688 tb = t->toBasetype();
|
|
689 //printf("\ttype = %s\n", type->toChars());
|
|
690 if (tb->ty == Tdelegate && type->toBasetype()->ty != Tdelegate)
|
|
691 return Expression::castTo(sc, t);
|
|
692
|
19
|
693 Type *typeb = type->toBasetype();
|
|
694 if (typeb == tb)
|
1
|
695 {
|
19
|
696 if (!copied)
|
|
697 { se = (StringExp *)copy();
|
|
698 copied = 1;
|
|
699 }
|
|
700 se->type = t;
|
1
|
701 return se;
|
|
702 }
|
|
703
|
|
704 if (tb->ty != Tsarray && tb->ty != Tarray && tb->ty != Tpointer)
|
19
|
705 { if (!copied)
|
|
706 { se = (StringExp *)copy();
|
|
707 copied = 1;
|
|
708 }
|
1
|
709 goto Lcast;
|
|
710 }
|
19
|
711 if (typeb->ty != Tsarray && typeb->ty != Tarray && typeb->ty != Tpointer)
|
|
712 { if (!copied)
|
|
713 { se = (StringExp *)copy();
|
|
714 copied = 1;
|
1
|
715 }
|
|
716 goto Lcast;
|
|
717 }
|
|
718
|
19
|
719 if (typeb->nextOf()->size() == tb->nextOf()->size())
|
|
720 {
|
|
721 if (!copied)
|
|
722 { se = (StringExp *)copy();
|
|
723 copied = 1;
|
|
724 }
|
|
725 if (tb->ty == Tsarray)
|
|
726 goto L2; // handle possible change in static array dimension
|
|
727 se->type = t;
|
|
728 return se;
|
|
729 }
|
1
|
730
|
19
|
731 if (committed)
|
|
732 goto Lcast;
|
1
|
733
|
|
734 #define X(tf,tt) ((tf) * 256 + (tt))
|
|
735 {
|
|
736 OutBuffer buffer;
|
19
|
737 size_t newlen = 0;
|
|
738 int tfty = typeb->nextOf()->toBasetype()->ty;
|
|
739 int ttty = tb->nextOf()->toBasetype()->ty;
|
1
|
740 switch (X(tfty, ttty))
|
|
741 {
|
|
742 case X(Tchar, Tchar):
|
|
743 case X(Twchar,Twchar):
|
|
744 case X(Tdchar,Tdchar):
|
|
745 break;
|
|
746
|
|
747 case X(Tchar, Twchar):
|
19
|
748 for (size_t u = 0; u < len;)
|
|
749 { unsigned c;
|
|
750 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
|
1
|
751 if (p)
|
|
752 error("%s", p);
|
|
753 else
|
|
754 buffer.writeUTF16(c);
|
|
755 }
|
|
756 newlen = buffer.offset / 2;
|
|
757 buffer.writeUTF16(0);
|
|
758 goto L1;
|
|
759
|
|
760 case X(Tchar, Tdchar):
|
19
|
761 for (size_t u = 0; u < len;)
|
|
762 { unsigned c;
|
|
763 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
|
1
|
764 if (p)
|
|
765 error("%s", p);
|
|
766 buffer.write4(c);
|
|
767 newlen++;
|
|
768 }
|
|
769 buffer.write4(0);
|
|
770 goto L1;
|
|
771
|
|
772 case X(Twchar,Tchar):
|
19
|
773 for (size_t u = 0; u < len;)
|
|
774 { unsigned c;
|
|
775 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
|
1
|
776 if (p)
|
|
777 error("%s", p);
|
|
778 else
|
|
779 buffer.writeUTF8(c);
|
|
780 }
|
|
781 newlen = buffer.offset;
|
|
782 buffer.writeUTF8(0);
|
|
783 goto L1;
|
|
784
|
|
785 case X(Twchar,Tdchar):
|
19
|
786 for (size_t u = 0; u < len;)
|
|
787 { unsigned c;
|
|
788 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
|
1
|
789 if (p)
|
|
790 error("%s", p);
|
|
791 buffer.write4(c);
|
|
792 newlen++;
|
|
793 }
|
|
794 buffer.write4(0);
|
|
795 goto L1;
|
|
796
|
|
797 case X(Tdchar,Tchar):
|
19
|
798 for (size_t u = 0; u < len; u++)
|
1
|
799 {
|
19
|
800 unsigned c = ((unsigned *)se->string)[u];
|
1
|
801 if (!utf_isValidDchar(c))
|
|
802 error("invalid UCS-32 char \\U%08x", c);
|
|
803 else
|
|
804 buffer.writeUTF8(c);
|
|
805 newlen++;
|
|
806 }
|
|
807 newlen = buffer.offset;
|
|
808 buffer.writeUTF8(0);
|
|
809 goto L1;
|
|
810
|
|
811 case X(Tdchar,Twchar):
|
19
|
812 for (size_t u = 0; u < len; u++)
|
1
|
813 {
|
19
|
814 unsigned c = ((unsigned *)se->string)[u];
|
1
|
815 if (!utf_isValidDchar(c))
|
|
816 error("invalid UCS-32 char \\U%08x", c);
|
|
817 else
|
|
818 buffer.writeUTF16(c);
|
|
819 newlen++;
|
|
820 }
|
|
821 newlen = buffer.offset / 2;
|
|
822 buffer.writeUTF16(0);
|
|
823 goto L1;
|
|
824
|
|
825 L1:
|
19
|
826 if (!copied)
|
|
827 { se = (StringExp *)copy();
|
|
828 copied = 1;
|
|
829 }
|
1
|
830 se->string = buffer.extractData();
|
|
831 se->len = newlen;
|
19
|
832 se->sz = tb->nextOf()->size();
|
1
|
833 break;
|
|
834
|
|
835 default:
|
19
|
836 assert(typeb->nextOf()->size() != tb->nextOf()->size());
|
1
|
837 goto Lcast;
|
|
838 }
|
|
839 }
|
|
840 #undef X
|
19
|
841 L2:
|
|
842 assert(copied);
|
1
|
843
|
|
844 // See if need to truncate or extend the literal
|
|
845 if (tb->ty == Tsarray)
|
|
846 {
|
|
847 int dim2 = ((TypeSArray *)tb)->dim->toInteger();
|
|
848
|
|
849 //printf("dim from = %d, to = %d\n", se->len, dim2);
|
|
850
|
|
851 // Changing dimensions
|
|
852 if (dim2 != se->len)
|
|
853 {
|
19
|
854 // Copy when changing the string literal
|
1
|
855 unsigned newsz = se->sz;
|
19
|
856 void *s;
|
|
857 int d;
|
1
|
858
|
19
|
859 d = (dim2 < se->len) ? dim2 : se->len;
|
|
860 s = (unsigned char *)mem.malloc((dim2 + 1) * newsz);
|
|
861 memcpy(s, se->string, d * newsz);
|
|
862 // Extend with 0, add terminating 0
|
|
863 memset((char *)s + d * newsz, 0, (dim2 + 1 - d) * newsz);
|
|
864 se->string = s;
|
|
865 se->len = dim2;
|
1
|
866 }
|
|
867 }
|
|
868 se->type = t;
|
|
869 return se;
|
|
870
|
|
871 Lcast:
|
|
872 Expression *e = new CastExp(loc, se, t);
|
19
|
873 e->type = t; // so semantic() won't be run on e
|
1
|
874 return e;
|
|
875 }
|
|
876
|
|
877 Expression *AddrExp::castTo(Scope *sc, Type *t)
|
|
878 {
|
|
879 Type *tb;
|
|
880
|
|
881 #if 0
|
|
882 printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
|
|
883 toChars(), type->toChars(), t->toChars());
|
|
884 #endif
|
|
885 Expression *e = this;
|
|
886
|
|
887 tb = t->toBasetype();
|
|
888 type = type->toBasetype();
|
|
889 if (tb != type)
|
|
890 {
|
|
891 // Look for pointers to functions where the functions are overloaded.
|
|
892 VarExp *ve;
|
|
893 FuncDeclaration *f;
|
|
894
|
|
895 if (type->ty == Tpointer && type->next->ty == Tfunction &&
|
|
896 tb->ty == Tpointer && tb->next->ty == Tfunction &&
|
|
897 e1->op == TOKvar)
|
|
898 {
|
|
899 ve = (VarExp *)e1;
|
|
900 f = ve->var->isFuncDeclaration();
|
|
901 if (f)
|
|
902 {
|
|
903 f = f->overloadExactMatch(tb->next);
|
|
904 if (f)
|
|
905 {
|
|
906 e = new VarExp(loc, f);
|
|
907 e->type = f->type;
|
|
908 e = new AddrExp(loc, e);
|
|
909 e->type = t;
|
|
910 return e;
|
|
911 }
|
|
912 }
|
|
913 }
|
|
914 e = Expression::castTo(sc, t);
|
|
915 }
|
|
916 e->type = t;
|
|
917 return e;
|
|
918 }
|
|
919
|
|
920
|
|
921 Expression *TupleExp::castTo(Scope *sc, Type *t)
|
|
922 {
|
|
923 for (size_t i = 0; i < exps->dim; i++)
|
|
924 { Expression *e = (Expression *)exps->data[i];
|
|
925 e = e->castTo(sc, t);
|
|
926 exps->data[i] = (void *)e;
|
|
927 }
|
|
928 return this;
|
|
929 }
|
|
930
|
|
931
|
|
932 Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t)
|
|
933 {
|
|
934 Type *typeb = type->toBasetype();
|
|
935 Type *tb = t->toBasetype();
|
|
936 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
|
|
937 (typeb->ty == Tarray || typeb->ty == Tsarray) &&
|
|
938 tb->next->toBasetype()->ty != Tvoid)
|
|
939 {
|
|
940 if (tb->ty == Tsarray)
|
|
941 { TypeSArray *tsa = (TypeSArray *)tb;
|
|
942 if (elements->dim != tsa->dim->toInteger())
|
|
943 goto L1;
|
|
944 }
|
|
945
|
|
946 for (int i = 0; i < elements->dim; i++)
|
|
947 { Expression *e = (Expression *)elements->data[i];
|
|
948 e = e->castTo(sc, tb->next);
|
|
949 elements->data[i] = (void *)e;
|
|
950 }
|
|
951 type = t;
|
|
952 return this;
|
|
953 }
|
|
954 if (tb->ty == Tpointer && typeb->ty == Tsarray)
|
|
955 {
|
|
956 type = typeb->next->pointerTo();
|
|
957 }
|
|
958 L1:
|
|
959 return Expression::castTo(sc, t);
|
|
960 }
|
|
961
|
|
962 Expression *AssocArrayLiteralExp::castTo(Scope *sc, Type *t)
|
|
963 {
|
|
964 Type *typeb = type->toBasetype();
|
|
965 Type *tb = t->toBasetype();
|
|
966 if (tb->ty == Taarray && typeb->ty == Taarray &&
|
|
967 tb->next->toBasetype()->ty != Tvoid)
|
|
968 {
|
|
969 assert(keys->dim == values->dim);
|
|
970 for (size_t i = 0; i < keys->dim; i++)
|
|
971 { Expression *e = (Expression *)values->data[i];
|
|
972 e = e->castTo(sc, tb->next);
|
|
973 values->data[i] = (void *)e;
|
|
974
|
|
975 e = (Expression *)keys->data[i];
|
|
976 e = e->castTo(sc, ((TypeAArray *)tb)->key);
|
|
977 keys->data[i] = (void *)e;
|
|
978 }
|
|
979 type = t;
|
|
980 return this;
|
|
981 }
|
|
982 L1:
|
|
983 return Expression::castTo(sc, t);
|
|
984 }
|
|
985
|
|
986 Expression *SymOffExp::castTo(Scope *sc, Type *t)
|
|
987 {
|
|
988 Type *tb;
|
|
989
|
|
990 #if 0
|
|
991 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
|
|
992 toChars(), type->toChars(), t->toChars());
|
|
993 #endif
|
|
994 Expression *e = this;
|
|
995
|
|
996 tb = t->toBasetype();
|
|
997 type = type->toBasetype();
|
|
998 if (tb != type)
|
|
999 {
|
|
1000 // Look for pointers to functions where the functions are overloaded.
|
|
1001 FuncDeclaration *f;
|
|
1002
|
|
1003 if (type->ty == Tpointer && type->next->ty == Tfunction &&
|
|
1004 tb->ty == Tpointer && tb->next->ty == Tfunction)
|
|
1005 {
|
|
1006 f = var->isFuncDeclaration();
|
|
1007 if (f)
|
|
1008 {
|
|
1009 f = f->overloadExactMatch(tb->next);
|
|
1010 if (f)
|
|
1011 {
|
|
1012 e = new SymOffExp(loc, f, 0);
|
|
1013 e->type = t;
|
|
1014 return e;
|
|
1015 }
|
|
1016 }
|
|
1017 }
|
|
1018 e = Expression::castTo(sc, t);
|
|
1019 }
|
|
1020 e->type = t;
|
|
1021 return e;
|
|
1022 }
|
|
1023
|
|
1024 Expression *DelegateExp::castTo(Scope *sc, Type *t)
|
|
1025 {
|
|
1026 Type *tb;
|
|
1027 #if 0
|
|
1028 printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
|
|
1029 toChars(), type->toChars(), t->toChars());
|
|
1030 #endif
|
|
1031 Expression *e = this;
|
|
1032 static char msg[] = "cannot form delegate due to covariant return type";
|
|
1033
|
|
1034 tb = t->toBasetype();
|
|
1035 type = type->toBasetype();
|
|
1036 if (tb != type)
|
|
1037 {
|
|
1038 // Look for delegates to functions where the functions are overloaded.
|
|
1039 FuncDeclaration *f;
|
|
1040
|
|
1041 if (type->ty == Tdelegate && type->next->ty == Tfunction &&
|
|
1042 tb->ty == Tdelegate && tb->next->ty == Tfunction)
|
|
1043 {
|
|
1044 if (func)
|
|
1045 {
|
|
1046 f = func->overloadExactMatch(tb->next);
|
|
1047 if (f)
|
|
1048 { int offset;
|
|
1049 if (f->tintro && f->tintro->next->isBaseOf(f->type->next, &offset) && offset)
|
|
1050 error("%s", msg);
|
|
1051 e = new DelegateExp(loc, e1, f);
|
|
1052 e->type = t;
|
|
1053 return e;
|
|
1054 }
|
|
1055 if (func->tintro)
|
|
1056 error("%s", msg);
|
|
1057 }
|
|
1058 }
|
|
1059 e = Expression::castTo(sc, t);
|
|
1060 }
|
|
1061 else
|
|
1062 { int offset;
|
|
1063
|
|
1064 if (func->tintro && func->tintro->next->isBaseOf(func->type->next, &offset) && offset)
|
|
1065 error("%s", msg);
|
|
1066 }
|
|
1067 e->type = t;
|
|
1068 return e;
|
|
1069 }
|
|
1070
|
|
1071 Expression *CondExp::castTo(Scope *sc, Type *t)
|
|
1072 {
|
|
1073 Expression *e = this;
|
|
1074
|
|
1075 if (type != t)
|
|
1076 {
|
|
1077 if (1 || e1->op == TOKstring || e2->op == TOKstring)
|
|
1078 { e = new CondExp(loc, econd, e1->castTo(sc, t), e2->castTo(sc, t));
|
|
1079 e->type = t;
|
|
1080 }
|
|
1081 else
|
|
1082 e = Expression::castTo(sc, t);
|
|
1083 }
|
|
1084 return e;
|
|
1085 }
|
|
1086
|
|
1087 /* ==================== ====================== */
|
|
1088
|
|
1089 /****************************************
|
|
1090 * Scale addition/subtraction to/from pointer.
|
|
1091 */
|
|
1092
|
|
1093 Expression *BinExp::scaleFactor(Scope *sc)
|
|
1094 { d_uns64 stride;
|
|
1095 Type *t1b = e1->type->toBasetype();
|
|
1096 Type *t2b = e2->type->toBasetype();
|
|
1097
|
|
1098 if (t1b->ty == Tpointer && t2b->isintegral())
|
|
1099 { // Need to adjust operator by the stride
|
|
1100 // Replace (ptr + int) with (ptr + (int * stride))
|
|
1101 Type *t = Type::tptrdiff_t;
|
|
1102
|
|
1103 stride = t1b->next->size();
|
|
1104 if (!t->equals(t2b))
|
|
1105 e2 = e2->castTo(sc, t);
|
|
1106 // LLVMDC: llvm uses typesafe pointer arithmetic
|
|
1107 #if !IN_LLVM
|
|
1108 if (t1b->next->isbit())
|
|
1109 // BUG: should add runtime check for misaligned offsets
|
|
1110 // This perhaps should be done by rewriting as &p[i]
|
|
1111 // and letting back end do it.
|
|
1112 e2 = new UshrExp(loc, e2, new IntegerExp(0, 3, t));
|
|
1113 else
|
|
1114 e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t));
|
|
1115 #endif
|
|
1116 e2->type = t;
|
|
1117 type = e1->type;
|
|
1118 }
|
|
1119 else if (t2b->ty && t1b->isintegral())
|
|
1120 { // Need to adjust operator by the stride
|
|
1121 // Replace (int + ptr) with (ptr + (int * stride))
|
|
1122 Type *t = Type::tptrdiff_t;
|
|
1123 Expression *e;
|
|
1124
|
|
1125 stride = t2b->next->size();
|
|
1126 if (!t->equals(t1b))
|
|
1127 e = e1->castTo(sc, t);
|
|
1128 else
|
|
1129 e = e1;
|
|
1130 if (t2b->next->isbit())
|
|
1131 // BUG: should add runtime check for misaligned offsets
|
|
1132 e = new UshrExp(loc, e, new IntegerExp(0, 3, t));
|
|
1133 else
|
|
1134 e = new MulExp(loc, e, new IntegerExp(0, stride, t));
|
|
1135 e->type = t;
|
|
1136 type = e2->type;
|
|
1137 e1 = e2;
|
|
1138 e2 = e;
|
|
1139 }
|
|
1140 return this;
|
|
1141 }
|
|
1142
|
|
1143 /************************************
|
|
1144 * Bring leaves to common type.
|
|
1145 */
|
|
1146
|
|
1147 Expression *BinExp::typeCombine(Scope *sc)
|
|
1148 {
|
|
1149 Type *t1;
|
|
1150 Type *t2;
|
|
1151 Type *t;
|
|
1152 TY ty;
|
|
1153
|
|
1154 //printf("BinExp::typeCombine()\n");
|
|
1155 //dump(0);
|
|
1156
|
|
1157 e1 = e1->integralPromotions(sc);
|
|
1158 e2 = e2->integralPromotions(sc);
|
|
1159
|
|
1160 // BUG: do toBasetype()
|
|
1161 t1 = e1->type;
|
|
1162 t2 = e2->type;
|
|
1163 assert(t1);
|
|
1164
|
|
1165 //if (t1) printf("\tt1 = %s\n", t1->toChars());
|
|
1166 //if (t2) printf("\tt2 = %s\n", t2->toChars());
|
|
1167 #ifdef DEBUG
|
|
1168 if (!t2) printf("\te2 = '%s'\n", e2->toChars());
|
|
1169 #endif
|
|
1170 assert(t2);
|
|
1171
|
|
1172 Type *t1b = t1->toBasetype();
|
|
1173 Type *t2b = t2->toBasetype();
|
|
1174
|
|
1175 ty = (TY)Type::impcnvResult[t1b->ty][t2b->ty];
|
|
1176 if (ty != Terror)
|
|
1177 { TY ty1;
|
|
1178 TY ty2;
|
|
1179
|
|
1180 ty1 = (TY)Type::impcnvType1[t1b->ty][t2b->ty];
|
|
1181 ty2 = (TY)Type::impcnvType2[t1b->ty][t2b->ty];
|
|
1182
|
|
1183 if (t1b->ty == ty1) // if no promotions
|
|
1184 {
|
|
1185 if (t1 == t2)
|
|
1186 {
|
|
1187 if (!type)
|
|
1188 type = t1;
|
|
1189 return this;
|
|
1190 }
|
|
1191
|
|
1192 if (t1b == t2b)
|
|
1193 {
|
|
1194 if (!type)
|
|
1195 type = t1b;
|
|
1196 return this;
|
|
1197 }
|
|
1198 }
|
|
1199
|
|
1200 if (!type)
|
|
1201 type = Type::basic[ty];
|
|
1202
|
|
1203 t1 = Type::basic[ty1];
|
|
1204 t2 = Type::basic[ty2];
|
|
1205 e1 = e1->castTo(sc, t1);
|
|
1206 e2 = e2->castTo(sc, t2);
|
|
1207 #if 0
|
|
1208 if (type != Type::basic[ty])
|
|
1209 { t = type;
|
|
1210 type = Type::basic[ty];
|
|
1211 return castTo(sc, t);
|
|
1212 }
|
|
1213 #endif
|
|
1214 //printf("after typeCombine():\n");
|
|
1215 //dump(0);
|
|
1216 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
|
|
1217 return this;
|
|
1218 }
|
|
1219
|
|
1220 t = t1;
|
|
1221 if (t1 == t2)
|
|
1222 {
|
|
1223 if ((t1->ty == Tstruct || t1->ty == Tclass) &&
|
|
1224 (op == TOKmin || op == TOKadd))
|
|
1225 goto Lincompatible;
|
|
1226 }
|
|
1227 else if (t1->isintegral() && t2->isintegral())
|
|
1228 {
|
|
1229 printf("t1 = %s, t2 = %s\n", t1->toChars(), t2->toChars());
|
|
1230 int sz1 = t1->size();
|
|
1231 int sz2 = t2->size();
|
|
1232 int sign1 = t1->isunsigned() == 0;
|
|
1233 int sign2 = t2->isunsigned() == 0;
|
|
1234
|
|
1235 if (sign1 == sign2)
|
|
1236 {
|
|
1237 if (sz1 < sz2)
|
|
1238 goto Lt2;
|
|
1239 else
|
|
1240 goto Lt1;
|
|
1241 }
|
|
1242 if (!sign1)
|
|
1243 {
|
|
1244 if (sz1 >= sz2)
|
|
1245 goto Lt1;
|
|
1246 else
|
|
1247 goto Lt2;
|
|
1248 }
|
|
1249 else
|
|
1250 {
|
|
1251 if (sz2 >= sz1)
|
|
1252 goto Lt2;
|
|
1253 else
|
|
1254 goto Lt1;
|
|
1255 }
|
|
1256 }
|
|
1257 else if (t1->ty == Tpointer && t2->ty == Tpointer)
|
|
1258 {
|
|
1259 // Bring pointers to compatible type
|
|
1260 Type *t1n = t1->next;
|
|
1261 Type *t2n = t2->next;
|
|
1262
|
|
1263 //t1->print();
|
|
1264 //t2->print();
|
|
1265 //if (t1n == t2n) *(char *)0 = 0;
|
|
1266 assert(t1n != t2n);
|
|
1267 if (t1n->ty == Tvoid) // pointers to void are always compatible
|
|
1268 t = t2;
|
|
1269 else if (t2n->ty == Tvoid)
|
|
1270 ;
|
|
1271 else if (t1n->ty == Tclass && t2n->ty == Tclass)
|
|
1272 { ClassDeclaration *cd1 = t1n->isClassHandle();
|
|
1273 ClassDeclaration *cd2 = t2n->isClassHandle();
|
|
1274 int offset;
|
|
1275
|
|
1276 if (cd1->isBaseOf(cd2, &offset))
|
|
1277 {
|
|
1278 if (offset)
|
|
1279 e2 = e2->castTo(sc, t);
|
|
1280 }
|
|
1281 else if (cd2->isBaseOf(cd1, &offset))
|
|
1282 {
|
|
1283 t = t2;
|
|
1284 if (offset)
|
|
1285 e1 = e1->castTo(sc, t);
|
|
1286 }
|
|
1287 else
|
|
1288 goto Lincompatible;
|
|
1289 }
|
|
1290 else
|
|
1291 goto Lincompatible;
|
|
1292 }
|
|
1293 else if ((t1->ty == Tsarray || t1->ty == Tarray) &&
|
|
1294 e2->op == TOKnull && t2->ty == Tpointer && t2->next->ty == Tvoid)
|
|
1295 {
|
|
1296 goto Lx1;
|
|
1297 }
|
|
1298 else if ((t2->ty == Tsarray || t2->ty == Tarray) &&
|
|
1299 e1->op == TOKnull && t1->ty == Tpointer && t1->next->ty == Tvoid)
|
|
1300 {
|
|
1301 goto Lx2;
|
|
1302 }
|
|
1303 else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2))
|
|
1304 {
|
|
1305 goto Lt2;
|
|
1306 }
|
|
1307 else if ((t2->ty == Tsarray || t2->ty == Tarray) && t2->implicitConvTo(t1))
|
|
1308 {
|
|
1309 goto Lt1;
|
|
1310 }
|
|
1311 else if (t1->ty == Tclass || t2->ty == Tclass)
|
|
1312 { int i1;
|
|
1313 int i2;
|
|
1314
|
|
1315 i1 = e2->implicitConvTo(t1);
|
|
1316 i2 = e1->implicitConvTo(t2);
|
|
1317
|
|
1318 if (i1 && i2)
|
|
1319 {
|
|
1320 // We have the case of class vs. void*, so pick class
|
|
1321 if (t1->ty == Tpointer)
|
|
1322 i1 = 0;
|
|
1323 else if (t2->ty == Tpointer)
|
|
1324 i2 = 0;
|
|
1325 }
|
|
1326
|
|
1327 if (i2)
|
|
1328 {
|
|
1329 goto Lt2;
|
|
1330 }
|
|
1331 else if (i1)
|
|
1332 {
|
|
1333 goto Lt1;
|
|
1334 }
|
|
1335 else
|
|
1336 goto Lincompatible;
|
|
1337 }
|
|
1338 else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2))
|
|
1339 {
|
|
1340 goto Lt2;
|
|
1341 }
|
|
1342 //else if (e2->op == TOKstring) { printf("test2\n"); }
|
|
1343 else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1))
|
|
1344 {
|
|
1345 goto Lt1;
|
|
1346 }
|
|
1347 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
|
|
1348 e2->implicitConvTo(t1->next->arrayOf()))
|
|
1349 {
|
|
1350 Lx1:
|
|
1351 t = t1->next->arrayOf();
|
|
1352 e1 = e1->castTo(sc, t);
|
|
1353 e2 = e2->castTo(sc, t);
|
|
1354 }
|
|
1355 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
|
|
1356 e1->implicitConvTo(t2->next->arrayOf()))
|
|
1357 {
|
|
1358 Lx2:
|
|
1359 t = t2->next->arrayOf();
|
|
1360 e1 = e1->castTo(sc, t);
|
|
1361 e2 = e2->castTo(sc, t);
|
|
1362 }
|
|
1363 else
|
|
1364 {
|
|
1365 Lincompatible:
|
|
1366 incompatibleTypes();
|
|
1367 }
|
|
1368 Lret:
|
|
1369 if (!type)
|
|
1370 type = t;
|
|
1371 //dump(0);
|
|
1372 return this;
|
|
1373
|
|
1374
|
|
1375 Lt1:
|
|
1376 e2 = e2->castTo(sc, t1);
|
|
1377 t = t1;
|
|
1378 goto Lret;
|
|
1379
|
|
1380 Lt2:
|
|
1381 e1 = e1->castTo(sc, t2);
|
|
1382 t = t2;
|
|
1383 goto Lret;
|
|
1384 }
|
|
1385
|
|
1386 /***********************************
|
|
1387 * Do integral promotions (convertchk).
|
|
1388 * Don't convert <array of> to <pointer to>
|
|
1389 */
|
|
1390
|
|
1391 Expression *Expression::integralPromotions(Scope *sc)
|
|
1392 { Expression *e;
|
|
1393
|
|
1394 e = this;
|
|
1395 switch (type->toBasetype()->ty)
|
|
1396 {
|
|
1397 case Tvoid:
|
|
1398 error("void has no value");
|
|
1399 break;
|
|
1400
|
|
1401 case Tint8:
|
|
1402 case Tuns8:
|
|
1403 case Tint16:
|
|
1404 case Tuns16:
|
|
1405 case Tbit:
|
|
1406 case Tbool:
|
|
1407 case Tchar:
|
|
1408 case Twchar:
|
|
1409 e = e->castTo(sc, Type::tint32);
|
|
1410 break;
|
|
1411
|
|
1412 case Tdchar:
|
|
1413 e = e->castTo(sc, Type::tuns32);
|
|
1414 break;
|
|
1415 }
|
|
1416 return e;
|
|
1417 }
|
|
1418
|