Merged Delight changes to D1 into D2
[delight/core.git] / dmd2 / cast.c
blob8569419107d65e11cb6738e35441d373b5312e3b
2 // Copyright (c) 1999-2008 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.
10 #include <stdio.h>
11 #include <assert.h>
13 #if _WIN32 || IN_GCC
14 #include "mem.h"
15 #else
16 #include "../root/mem.h"
17 #endif
19 #include "expression.h"
20 #include "mtype.h"
21 #include "utf.h"
22 #include "declaration.h"
23 #include "aggregate.h"
25 /* ==================== implicitCast ====================== */
27 /**************************************
28 * Do an implicit cast.
29 * Issue error if it can't be done.
32 Expression *Expression::implicitCastTo(Scope *sc, Type *t)
34 //printf("Expression::implicitCastTo(%s of type %s) => %s\n", toChars(), type->toChars(), t->toChars());
36 MATCH match = implicitConvTo(t);
37 if (match)
39 if (global.params.warnings &&
40 Type::impcnvWarn[type->toBasetype()->ty][t->toBasetype()->ty] &&
41 op != TOKint64)
43 Expression *e = optimize(WANTflags | WANTvalue);
45 if (e->op == TOKint64)
46 return e->implicitCastTo(sc, t);
48 fprintf(stdmsg, "warning - ");
49 error("implicit conversion of expression (%s) of type %s to %s can cause loss of data",
50 toChars(), type->toChars(), t->toChars());
52 if (match == MATCHconst && t == type->constOf())
54 Expression *e = copy();
55 e->type = t;
56 return e;
58 return castTo(sc, t);
61 Expression *e = optimize(WANTflags | WANTvalue);
62 if (e != this)
63 return e->implicitCastTo(sc, t);
65 #if 0
66 printf("ty = %d\n", type->ty);
67 print();
68 type->print();
69 printf("to:\n");
70 t->print();
71 printf("%p %p type: %s to: %s\n", type->deco, t->deco, type->deco, t->deco);
72 //printf("%p %p %p\n", type->nextOf()->arrayOf(), type, t);
73 fflush(stdout);
74 #endif
75 if (!t->deco)
76 { /* Can happen with:
77 * enum E { One }
78 * class A
79 * { static void fork(EDG dg) { dg(E.One); }
80 * alias void delegate(E) EDG;
81 * }
82 * Should eventually make it work.
84 error("forward reference to type %s", t->toChars());
86 else if (t->reliesOnTident())
87 error("forward reference to type %s", t->reliesOnTident()->toChars());
89 error("cannot implicitly convert expression (%s) of type %s to %s",
90 toChars(), type->toChars(), t->toChars());
91 return castTo(sc, t);
94 /*******************************************
95 * Return !=0 if we can implicitly convert this to type t.
96 * Don't do the actual cast.
99 MATCH Expression::implicitConvTo(Type *t)
101 #if 0
102 printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n",
103 toChars(), type->toChars(), t->toChars());
104 #endif
105 //static int nest; if (++nest == 10) halt();
106 if (!type)
107 { error("%s is not an expression", toChars());
108 type = Type::terror;
110 Expression *e = optimize(WANTvalue | WANTflags);
111 if (e->type == t)
112 return MATCHexact;
113 if (e != this)
114 { //printf("\toptimized to %s of type %s\n", e->toChars(), e->type->toChars());
115 return e->implicitConvTo(t);
117 MATCH match = type->implicitConvTo(t);
118 if (match != MATCHnomatch)
119 return match;
120 #if 0
121 Type *tb = t->toBasetype();
122 if (tb->ty == Tdelegate)
123 { TypeDelegate *td = (TypeDelegate *)tb;
124 TypeFunction *tf = (TypeFunction *)td->nextOf();
126 if (!tf->varargs &&
127 !(tf->arguments && tf->arguments->dim)
130 match = type->implicitConvTo(tf->nextOf());
131 if (match)
132 return match;
133 if (tf->nextOf()->toBasetype()->ty == Tvoid)
134 return MATCHconvert;
137 #endif
138 return MATCHnomatch;
142 MATCH IntegerExp::implicitConvTo(Type *t)
144 #if 0
145 printf("IntegerExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
146 toChars(), type->toChars(), t->toChars());
147 #endif
148 MATCH m = type->implicitConvTo(t);
149 if (m >= MATCHconst)
150 return m;
152 TY ty = type->toBasetype()->ty;
153 TY toty = t->toBasetype()->ty;
155 if (m == MATCHnomatch && t->ty == Tenum)
156 goto Lno;
158 switch (ty)
160 case Tbit:
161 case Tbool:
162 value &= 1;
163 ty = Tint32;
164 break;
166 case Tint8:
167 value = (signed char)value;
168 ty = Tint32;
169 break;
171 case Tchar:
172 case Tuns8:
173 value &= 0xFF;
174 ty = Tint32;
175 break;
177 case Tint16:
178 value = (short)value;
179 ty = Tint32;
180 break;
182 case Tuns16:
183 case Twchar:
184 value &= 0xFFFF;
185 ty = Tint32;
186 break;
188 case Tint32:
189 value = (int)value;
190 break;
192 case Tuns32:
193 case Tdchar:
194 value &= 0xFFFFFFFF;
195 ty = Tuns32;
196 break;
198 default:
199 break;
202 // Only allow conversion if no change in value
203 switch (toty)
205 case Tbit:
206 case Tbool:
207 if ((value & 1) != value)
208 goto Lno;
209 goto Lyes;
211 case Tint8:
212 if ((signed char)value != value)
213 goto Lno;
214 goto Lyes;
216 case Tchar:
217 case Tuns8:
218 //printf("value = %llu %llu\n", (integer_t)(unsigned char)value, value);
219 if ((unsigned char)value != value)
220 goto Lno;
221 goto Lyes;
223 case Tint16:
224 if ((short)value != value)
225 goto Lno;
226 goto Lyes;
228 case Tuns16:
229 if ((unsigned short)value != value)
230 goto Lno;
231 goto Lyes;
233 case Tint32:
234 if (ty == Tuns32)
237 else if ((int)value != value)
238 goto Lno;
239 goto Lyes;
241 case Tuns32:
242 if (ty == Tint32)
245 else if ((unsigned)value != value)
246 goto Lno;
247 goto Lyes;
249 case Tdchar:
250 if (value > 0x10FFFFUL)
251 goto Lno;
252 goto Lyes;
254 case Twchar:
255 if ((unsigned short)value != value)
256 goto Lno;
257 goto Lyes;
259 #if IN_GCC
260 case Tfloat32:
261 case Tfloat64:
262 case Tfloat80:
264 real_t::MyMode mode;
265 real_t f;
266 switch (toty)
268 case Tfloat32: mode = real_t::Float; break;
269 case Tfloat64: mode = real_t::Double; break;
270 case Tfloat80: mode = real_t::LongDouble; break;
272 if (type->isunsigned())
274 f = real_t((d_uns64) value);
275 f = f.convert(mode);
276 if ((d_uns64) f.toInt() != (d_uns64) value)
277 goto Lno;
279 else
281 f = real_t((d_int64) value);
282 f = f.convert(mode);
283 if ((d_int64) f.toInt() != (d_int64) value)
284 goto Lno;
286 goto Lyes;
288 #else
289 case Tfloat32:
291 volatile float f;
292 if (type->isunsigned())
294 f = (float)value;
295 if (f != value)
296 goto Lno;
298 else
300 f = (float)(long long)value;
301 if (f != (long long)value)
302 goto Lno;
304 goto Lyes;
307 case Tfloat64:
309 volatile double f;
310 if (type->isunsigned())
312 f = (double)value;
313 if (f != value)
314 goto Lno;
316 else
318 f = (double)(long long)value;
319 if (f != (long long)value)
320 goto Lno;
322 goto Lyes;
325 case Tfloat80:
327 volatile long double f;
328 if (type->isunsigned())
330 f = (long double)value;
331 if (f != value)
332 goto Lno;
334 else
336 f = (long double)(long long)value;
337 if (f != (long long)value)
338 goto Lno;
340 goto Lyes;
343 case Tpointer:
344 //printf("type = %s\n", type->toBasetype()->toChars());
345 //printf("t = %s\n", t->toBasetype()->toChars());
346 if (ty == Tpointer &&
347 type->toBasetype()->nextOf()->ty == t->toBasetype()->nextOf()->ty)
348 { /* Allow things like:
349 * const char* P = cast(char *)3;
350 * char* q = P;
352 goto Lyes;
354 break;
355 #endif
357 return Expression::implicitConvTo(t);
359 Lyes:
360 return MATCHconvert;
362 Lno:
363 return MATCHnomatch;
366 MATCH NullExp::implicitConvTo(Type *t)
368 #if 0
369 printf("NullExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
370 toChars(), type->toChars(), t->toChars());
371 #endif
373 if (this->type->equals(t))
374 return MATCHexact;
376 /* Allow implicit conversions from invariant to mutable|const,
377 * and mutable to invariant. It works because, after all, a null
378 * doesn't actually point to anything.
380 if (t->invariantOf()->equals(type->invariantOf()))
381 return MATCHconst;
383 // NULL implicitly converts to any pointer type or dynamic array
384 if (type->ty == Tpointer && type->nextOf()->ty == Tvoid)
386 if (t->ty == Ttypedef)
387 t = ((TypeTypedef *)t)->sym->basetype;
388 if (t->ty == Tarray ||
389 t->ty == Taarray || t->ty == Tmaybe ||
390 t->ty == Tdelegate)
391 return committed ? MATCHconvert : MATCHexact;
394 /* Null can only be implicitly converted to a maybe type */
395 if (t->ty != Tmaybe)
396 return MATCHnomatch;
398 t = t->nextOf();
400 return Expression::implicitConvTo(t);
403 MATCH StructLiteralExp::implicitConvTo(Type *t)
405 #if 0
406 printf("StructLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
407 toChars(), type->toChars(), t->toChars());
408 #endif
409 MATCH m = Expression::implicitConvTo(t);
410 if (m != MATCHnomatch)
411 return m;
412 if (type->ty == t->ty && type->ty == Tstruct &&
413 ((TypeStruct *)type)->sym == ((TypeStruct *)t)->sym)
415 m = MATCHconst;
416 for (int i = 0; i < elements->dim; i++)
417 { Expression *e = (Expression *)elements->data[i];
418 Type *te = e->type;
419 if (t->mod == 0)
420 te = te->mutableOf();
421 else
422 { assert(t->mod == MODinvariant);
423 te = te->invariantOf();
425 MATCH m2 = e->implicitConvTo(te);
426 //printf("\t%s => %s, match = %d\n", e->toChars(), te->toChars(), m2);
427 if (m2 < m)
428 m = m2;
431 return m;
434 MATCH StringExp::implicitConvTo(Type *t)
435 { MATCH m;
437 #if 0
438 printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
439 toChars(), committed, type->toChars(), t->toChars());
440 #endif
441 if (!committed)
443 if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
445 return MATCHnomatch;
447 if (type->ty == Tsarray || type->ty == Tarray || type->ty == Tpointer)
449 TY tyn = type->nextOf()->ty;
450 if (tyn == Tchar || tyn == Twchar || tyn == Tdchar)
451 { Type *tn;
452 MATCH m;
454 switch (t->ty)
456 case Tsarray:
457 if (type->ty == Tsarray)
459 if (((TypeSArray *)type)->dim->toInteger() !=
460 ((TypeSArray *)t)->dim->toInteger())
461 return MATCHnomatch;
462 TY tynto = t->nextOf()->ty;
463 if (tynto == Tchar || tynto == Twchar || tynto == Tdchar)
464 return MATCHexact;
466 case Tarray:
467 case Tpointer:
468 tn = t->nextOf();
469 m = MATCHexact;
470 if (type->nextOf()->mod != tn->mod)
471 { if (!tn->isConst())
472 return MATCHnomatch;
473 m = MATCHconst;
475 switch (tn->ty)
477 case Tchar:
478 case Twchar:
479 case Tdchar:
480 return m;
482 break;
487 return Expression::implicitConvTo(t);
488 #if 0
489 m = (MATCH)type->implicitConvTo(t);
490 if (m)
492 return m;
495 return MATCHnomatch;
496 #endif
499 MATCH ArrayLiteralExp::implicitConvTo(Type *t)
500 { MATCH result = MATCHexact;
502 #if 0
503 printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
504 toChars(), type->toChars(), t->toChars());
505 #endif
506 Type *typeb = type->toBasetype();
507 Type *tb = t->toBasetype();
508 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
509 (typeb->ty == Tarray || typeb->ty == Tsarray))
511 if (tb->ty == Tsarray)
512 { TypeSArray *tsa = (TypeSArray *)tb;
513 if (elements->dim != tsa->dim->toInteger())
514 result = MATCHnomatch;
517 for (int i = 0; i < elements->dim; i++)
518 { Expression *e = (Expression *)elements->data[i];
519 MATCH m = (MATCH)e->implicitConvTo(tb->nextOf());
520 if (m < result)
521 result = m; // remember worst match
522 if (result == MATCHnomatch)
523 break; // no need to check for worse
525 return result;
527 else
528 return Expression::implicitConvTo(t);
531 MATCH AssocArrayLiteralExp::implicitConvTo(Type *t)
532 { MATCH result = MATCHexact;
534 Type *typeb = type->toBasetype();
535 Type *tb = t->toBasetype();
536 if (tb->ty == Taarray && typeb->ty == Taarray)
538 for (size_t i = 0; i < keys->dim; i++)
539 { Expression *e = (Expression *)keys->data[i];
540 MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->index);
541 if (m < result)
542 result = m; // remember worst match
543 if (result == MATCHnomatch)
544 break; // no need to check for worse
545 e = (Expression *)values->data[i];
546 m = (MATCH)e->implicitConvTo(tb->nextOf());
547 if (m < result)
548 result = m; // remember worst match
549 if (result == MATCHnomatch)
550 break; // no need to check for worse
552 return result;
554 else
555 return Expression::implicitConvTo(t);
558 MATCH AddrExp::implicitConvTo(Type *t)
560 #if 0
561 printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
562 toChars(), type->toChars(), t->toChars());
563 #endif
564 MATCH result;
566 result = type->implicitConvTo(t);
567 //printf("\tresult = %d\n", result);
569 if (result == MATCHnomatch)
571 // Look for pointers to functions where the functions are overloaded.
573 t = t->toBasetype();
575 if (e1->op == TOKoverloadset &&
576 (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction)
577 { OverExp *eo = (OverExp *)e1;
578 FuncDeclaration *f = NULL;
579 for (int i = 0; i < eo->vars->a.dim; i++)
580 { Dsymbol *s = (Dsymbol *)eo->vars->a.data[i];
581 FuncDeclaration *f2 = s->isFuncDeclaration();
582 assert(f2);
583 if (f2->overloadExactMatch(t->nextOf()))
584 { if (f)
585 /* Error if match in more than one overload set,
586 * even if one is a 'better' match than the other.
588 ScopeDsymbol::multiplyDefined(loc, f, f2);
589 else
590 f = f2;
591 result = MATCHexact;
596 if (type->ty == Tpointer && type->nextOf()->ty == Tfunction &&
597 t->ty == Tpointer && t->nextOf()->ty == Tfunction &&
598 e1->op == TOKvar)
600 /* I don't think this can ever happen -
601 * it should have been
602 * converted to a SymOffExp.
604 assert(0);
605 VarExp *ve = (VarExp *)e1;
606 FuncDeclaration *f = ve->var->isFuncDeclaration();
607 if (f && f->overloadExactMatch(t->nextOf()))
608 result = MATCHexact;
611 //printf("\tresult = %d\n", result);
612 return result;
615 MATCH SymOffExp::implicitConvTo(Type *t)
617 #if 0
618 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
619 toChars(), type->toChars(), t->toChars());
620 #endif
621 MATCH result;
623 result = type->implicitConvTo(t);
624 //printf("\tresult = %d\n", result);
626 if (result == MATCHnomatch)
628 // Look for pointers to functions where the functions are overloaded.
629 FuncDeclaration *f;
631 t = t->toBasetype();
632 if (type->ty == Tpointer && type->nextOf()->ty == Tfunction &&
633 (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction)
635 f = var->isFuncDeclaration();
636 if (f)
637 { f = f->overloadExactMatch(t->nextOf());
638 if (f)
639 { if ((t->ty == Tdelegate && (f->needThis() || f->isNested())) ||
640 (t->ty == Tpointer && !(f->needThis() || f->isNested())))
642 result = MATCHexact;
648 //printf("\tresult = %d\n", result);
649 return result;
652 MATCH DelegateExp::implicitConvTo(Type *t)
654 #if 0
655 printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
656 toChars(), type->toChars(), t->toChars());
657 #endif
658 MATCH result;
660 result = type->implicitConvTo(t);
662 if (result == MATCHnomatch)
664 // Look for pointers to functions where the functions are overloaded.
665 FuncDeclaration *f;
667 t = t->toBasetype();
668 if (type->ty == Tdelegate && type->nextOf()->ty == Tfunction &&
669 t->ty == Tdelegate && t->nextOf()->ty == Tfunction)
671 if (func && func->overloadExactMatch(t->nextOf()))
672 result = MATCHexact;
675 return result;
678 MATCH CondExp::implicitConvTo(Type *t)
680 MATCH m1;
681 MATCH m2;
683 m1 = e1->implicitConvTo(t);
684 m2 = e2->implicitConvTo(t);
686 // Pick the worst match
687 return (m1 < m2) ? m1 : m2;
691 /* ==================== castTo ====================== */
693 /**************************************
694 * Do an explicit cast.
697 Expression *Expression::castTo(Scope *sc, Type *t)
699 //printf("Expression::castTo(this=%s, t=%s)\n", toChars(), t->toChars());
700 #if 0
701 printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
702 toChars(), type->toChars(), t->toChars());
703 #endif
704 if (type == t)
705 return this;
706 Expression *e = this;
707 Type *tb = t->toBasetype();
708 Type *typeb = type->toBasetype();
709 if (tb != typeb)
711 // Do (type *) cast of (type [dim])
712 if (tb->ty == Tpointer &&
713 typeb->ty == Tsarray
716 //printf("Converting [dim] to *\n");
718 if (typeb->size(loc) == 0)
719 e = new NullExp(loc);
720 else
721 e = new AddrExp(loc, e);
723 #if 0
724 else if (tb->ty == Tdelegate && type->ty != Tdelegate)
726 TypeDelegate *td = (TypeDelegate *)tb;
727 TypeFunction *tf = (TypeFunction *)td->nextOf();
728 return toDelegate(sc, tf->nextOf());
730 #endif
731 else
733 e = new CastExp(loc, e, tb);
736 else
738 e = e->copy(); // because of COW for assignment to e->type
740 assert(e != this);
741 e->type = t;
742 //printf("Returning: %s\n", e->toChars());
743 return e;
747 Expression *RealExp::castTo(Scope *sc, Type *t)
748 { Expression *e = this;
749 if (type != t)
751 if ((type->isreal() && t->isreal()) ||
752 (type->isimaginary() && t->isimaginary())
754 { e = copy();
755 e->type = t;
757 else
758 e = Expression::castTo(sc, t);
760 return e;
764 Expression *ComplexExp::castTo(Scope *sc, Type *t)
765 { Expression *e = this;
766 if (type != t)
768 if (type->iscomplex() && t->iscomplex())
769 { e = copy();
770 e->type = t;
772 else
773 e = Expression::castTo(sc, t);
775 return e;
779 Expression *NullExp::castTo(Scope *sc, Type *t)
780 { NullExp *e;
781 Type *tb;
783 //printf("NullExp::castTo(t = %p)\n", t);
784 if (type == t)
786 committed = 1;
787 return this;
789 e = (NullExp *)copy();
790 e->committed = 1;
791 tb = t->toBasetype();
792 e->type = type->toBasetype();
793 if (tb != e->type)
795 // NULL implicitly converts to any pointer type or dynamic array
796 if (e->type->ty == Tpointer && e->type->nextOf()->ty == Tvoid &&
797 (tb->ty == Tpointer || tb->ty == Tarray || tb->ty == Taarray ||
798 tb->ty == Tdelegate))
800 #if 0
801 if (tb->ty == Tdelegate)
802 { TypeDelegate *td = (TypeDelegate *)tb;
803 TypeFunction *tf = (TypeFunction *)td->nextOf();
805 if (!tf->varargs &&
806 !(tf->arguments && tf->arguments->dim)
809 return Expression::castTo(sc, t);
812 #endif
814 else
816 return e->Expression::castTo(sc, t);
819 e->type = t;
820 return e;
823 Expression *StringExp::castTo(Scope *sc, Type *t)
825 /* This follows copy-on-write; any changes to 'this'
826 * will result in a copy.
827 * The this->string member is considered immutable.
829 StringExp *se;
830 Type *tb;
831 int copied = 0;
833 //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed);
835 if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
837 error("cannot convert string literal to void*");
840 se = this;
841 if (!committed)
842 { se = (StringExp *)copy();
843 se->committed = 1;
844 copied = 1;
847 if (type == t)
849 return se;
852 tb = t->toBasetype();
853 //printf("\ttype = %s\n", type->toChars());
854 if (tb->ty == Tdelegate && type->toBasetype()->ty != Tdelegate)
855 return Expression::castTo(sc, t);
857 Type *typeb = type->toBasetype();
858 if (typeb == tb)
860 if (!copied)
861 { se = (StringExp *)copy();
862 copied = 1;
864 se->type = t;
865 return se;
868 if (tb->ty != Tsarray && tb->ty != Tarray && tb->ty != Tpointer)
869 { if (!copied)
870 { se = (StringExp *)copy();
871 copied = 1;
873 goto Lcast;
875 if (typeb->ty != Tsarray && typeb->ty != Tarray && typeb->ty != Tpointer)
876 { if (!copied)
877 { se = (StringExp *)copy();
878 copied = 1;
880 goto Lcast;
883 if (typeb->nextOf()->size() == tb->nextOf()->size())
885 if (!copied)
886 { se = (StringExp *)copy();
887 copied = 1;
889 if (tb->ty == Tsarray)
890 goto L2; // handle possible change in static array dimension
891 se->type = t;
892 return se;
895 if (committed)
896 goto Lcast;
898 #define X(tf,tt) ((tf) * 256 + (tt))
900 OutBuffer buffer;
901 size_t newlen = 0;
902 int tfty = typeb->nextOf()->toBasetype()->ty;
903 int ttty = tb->nextOf()->toBasetype()->ty;
904 switch (X(tfty, ttty))
906 case X(Tchar, Tchar):
907 case X(Twchar,Twchar):
908 case X(Tdchar,Tdchar):
909 break;
911 case X(Tchar, Twchar):
912 for (size_t u = 0; u < len;)
913 { unsigned c;
914 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
915 if (p)
916 error("%s", p);
917 else
918 buffer.writeUTF16(c);
920 newlen = buffer.offset / 2;
921 buffer.writeUTF16(0);
922 goto L1;
924 case X(Tchar, Tdchar):
925 for (size_t u = 0; u < len;)
926 { unsigned c;
927 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
928 if (p)
929 error("%s", p);
930 buffer.write4(c);
931 newlen++;
933 buffer.write4(0);
934 goto L1;
936 case X(Twchar,Tchar):
937 for (size_t u = 0; u < len;)
938 { unsigned c;
939 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
940 if (p)
941 error("%s", p);
942 else
943 buffer.writeUTF8(c);
945 newlen = buffer.offset;
946 buffer.writeUTF8(0);
947 goto L1;
949 case X(Twchar,Tdchar):
950 for (size_t u = 0; u < len;)
951 { unsigned c;
952 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
953 if (p)
954 error("%s", p);
955 buffer.write4(c);
956 newlen++;
958 buffer.write4(0);
959 goto L1;
961 case X(Tdchar,Tchar):
962 for (size_t u = 0; u < len; u++)
964 unsigned c = ((unsigned *)se->string)[u];
965 if (!utf_isValidDchar(c))
966 error("invalid UCS-32 char \\U%08x", c);
967 else
968 buffer.writeUTF8(c);
969 newlen++;
971 newlen = buffer.offset;
972 buffer.writeUTF8(0);
973 goto L1;
975 case X(Tdchar,Twchar):
976 for (size_t u = 0; u < len; u++)
978 unsigned c = ((unsigned *)se->string)[u];
979 if (!utf_isValidDchar(c))
980 error("invalid UCS-32 char \\U%08x", c);
981 else
982 buffer.writeUTF16(c);
983 newlen++;
985 newlen = buffer.offset / 2;
986 buffer.writeUTF16(0);
987 goto L1;
990 if (!copied)
991 { se = (StringExp *)copy();
992 copied = 1;
994 se->string = buffer.extractData();
995 se->len = newlen;
996 se->sz = tb->nextOf()->size();
997 break;
999 default:
1000 assert(typeb->nextOf()->size() != tb->nextOf()->size());
1001 goto Lcast;
1004 #undef X
1006 assert(copied);
1008 // See if need to truncate or extend the literal
1009 if (tb->ty == Tsarray)
1011 int dim2 = ((TypeSArray *)tb)->dim->toInteger();
1013 //printf("dim from = %d, to = %d\n", se->len, dim2);
1015 // Changing dimensions
1016 if (dim2 != se->len)
1018 // Copy when changing the string literal
1019 unsigned newsz = se->sz;
1020 void *s;
1021 int d;
1023 d = (dim2 < se->len) ? dim2 : se->len;
1024 s = (unsigned char *)mem.malloc((dim2 + 1) * newsz);
1025 memcpy(s, se->string, d * newsz);
1026 // Extend with 0, add terminating 0
1027 memset((char *)s + d * newsz, 0, (dim2 + 1 - d) * newsz);
1028 se->string = s;
1029 se->len = dim2;
1032 se->type = t;
1033 return se;
1035 Lcast:
1036 Expression *e = new CastExp(loc, se, t);
1037 e->type = t; // so semantic() won't be run on e
1038 return e;
1041 Expression *AddrExp::castTo(Scope *sc, Type *t)
1043 Type *tb;
1045 #if 0
1046 printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
1047 toChars(), type->toChars(), t->toChars());
1048 #endif
1049 Expression *e = this;
1051 tb = t->toBasetype();
1052 type = type->toBasetype();
1053 if (tb != type)
1055 // Look for pointers to functions where the functions are overloaded.
1057 if (e1->op == TOKoverloadset &&
1058 (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction)
1059 { OverExp *eo = (OverExp *)e1;
1060 FuncDeclaration *f = NULL;
1061 for (int i = 0; i < eo->vars->a.dim; i++)
1062 { Dsymbol *s = (Dsymbol *)eo->vars->a.data[i];
1063 FuncDeclaration *f2 = s->isFuncDeclaration();
1064 assert(f2);
1065 if (f2->overloadExactMatch(t->nextOf()))
1066 { if (f)
1067 /* Error if match in more than one overload set,
1068 * even if one is a 'better' match than the other.
1070 ScopeDsymbol::multiplyDefined(loc, f, f2);
1071 else
1072 f = f2;
1075 if (f)
1076 { f->tookAddressOf = 1;
1077 SymOffExp *se = new SymOffExp(loc, f, 0, 0);
1078 se->semantic(sc);
1079 // Let SymOffExp::castTo() do the heavy lifting
1080 return se->castTo(sc, t);
1085 if (type->ty == Tpointer && type->nextOf()->ty == Tfunction &&
1086 tb->ty == Tpointer && tb->nextOf()->ty == Tfunction &&
1087 e1->op == TOKvar)
1089 VarExp *ve = (VarExp *)e1;
1090 FuncDeclaration *f = ve->var->isFuncDeclaration();
1091 if (f)
1093 assert(0); // should be SymOffExp instead
1094 f = f->overloadExactMatch(tb->nextOf());
1095 if (f)
1097 e = new VarExp(loc, f);
1098 e->type = f->type;
1099 e = new AddrExp(loc, e);
1100 e->type = t;
1101 return e;
1105 e = Expression::castTo(sc, t);
1107 e->type = t;
1108 return e;
1112 Expression *TupleExp::castTo(Scope *sc, Type *t)
1113 { TupleExp *e = (TupleExp *)copy();
1114 e->exps = (Expressions *)exps->copy();
1115 for (size_t i = 0; i < e->exps->dim; i++)
1116 { Expression *ex = (Expression *)e->exps->data[i];
1117 ex = ex->castTo(sc, t);
1118 e->exps->data[i] = (void *)ex;
1120 return e;
1124 Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t)
1126 #if 0
1127 printf("ArrayLiteralExp::castTo(this=%s, type=%s, => %s)\n",
1128 toChars(), type->toChars(), t->toChars());
1129 #endif
1130 if (type == t)
1131 return this;
1132 ArrayLiteralExp *e = this;
1133 Type *typeb = type->toBasetype();
1134 Type *tb = t->toBasetype();
1135 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
1136 (typeb->ty == Tarray || typeb->ty == Tsarray) &&
1137 // Not trying to convert non-void[] to void[]
1138 !(tb->nextOf()->toBasetype()->ty == Tvoid && typeb->nextOf()->toBasetype()->ty != Tvoid))
1140 if (tb->ty == Tsarray)
1141 { TypeSArray *tsa = (TypeSArray *)tb;
1142 if (elements->dim != tsa->dim->toInteger())
1143 goto L1;
1146 e = (ArrayLiteralExp *)copy();
1147 e->elements = (Expressions *)elements->copy();
1148 for (int i = 0; i < elements->dim; i++)
1149 { Expression *ex = (Expression *)elements->data[i];
1150 ex = ex->castTo(sc, tb->nextOf());
1151 e->elements->data[i] = (void *)ex;
1153 e->type = t;
1154 return e;
1156 if (tb->ty == Tpointer && typeb->ty == Tsarray)
1158 e = (ArrayLiteralExp *)copy();
1159 e->type = typeb->nextOf()->pointerTo();
1162 return e->Expression::castTo(sc, t);
1165 Expression *AssocArrayLiteralExp::castTo(Scope *sc, Type *t)
1167 if (type == t)
1168 return this;
1169 AssocArrayLiteralExp *e = this;
1170 Type *typeb = type->toBasetype();
1171 Type *tb = t->toBasetype();
1172 if (tb->ty == Taarray && typeb->ty == Taarray &&
1173 tb->nextOf()->toBasetype()->ty != Tvoid)
1175 e = (AssocArrayLiteralExp *)copy();
1176 e->keys = (Expressions *)keys->copy();
1177 e->values = (Expressions *)values->copy();
1178 assert(keys->dim == values->dim);
1179 for (size_t i = 0; i < keys->dim; i++)
1180 { Expression *ex = (Expression *)values->data[i];
1181 ex = ex->castTo(sc, tb->nextOf());
1182 e->values->data[i] = (void *)ex;
1184 ex = (Expression *)keys->data[i];
1185 ex = ex->castTo(sc, ((TypeAArray *)tb)->index);
1186 e->keys->data[i] = (void *)ex;
1188 e->type = t;
1189 return e;
1192 return e->Expression::castTo(sc, t);
1195 Expression *SymOffExp::castTo(Scope *sc, Type *t)
1197 #if 0
1198 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
1199 toChars(), type->toChars(), t->toChars());
1200 #endif
1201 if (type == t && hasOverloads == 0)
1202 return this;
1203 Expression *e;
1204 Type *tb = t->toBasetype();
1205 Type *typeb = type->toBasetype();
1206 if (tb != typeb)
1208 // Look for pointers to functions where the functions are overloaded.
1209 FuncDeclaration *f;
1211 if (hasOverloads &&
1212 typeb->ty == Tpointer && typeb->nextOf()->ty == Tfunction &&
1213 (tb->ty == Tpointer || tb->ty == Tdelegate) && tb->nextOf()->ty == Tfunction)
1215 f = var->isFuncDeclaration();
1216 if (f)
1218 f = f->overloadExactMatch(tb->nextOf());
1219 if (f)
1221 if (tb->ty == Tdelegate && f->needThis() && hasThis(sc))
1223 e = new DelegateExp(loc, new ThisExp(loc), f);
1224 e = e->semantic(sc);
1226 else if (tb->ty == Tdelegate && f->isNested())
1228 e = new DelegateExp(loc, new IntegerExp(0), f);
1229 e = e->semantic(sc);
1231 else
1233 e = new SymOffExp(loc, f, 0);
1234 e->type = t;
1236 f->tookAddressOf = 1;
1237 return e;
1241 e = Expression::castTo(sc, t);
1243 else
1244 { e = copy();
1245 e->type = t;
1246 ((SymOffExp *)e)->hasOverloads = 0;
1248 return e;
1251 Expression *DelegateExp::castTo(Scope *sc, Type *t)
1253 #if 0
1254 printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
1255 toChars(), type->toChars(), t->toChars());
1256 #endif
1257 static char msg[] = "cannot form delegate due to covariant return type";
1259 Expression *e = this;
1260 Type *tb = t->toBasetype();
1261 Type *typeb = type->toBasetype();
1262 if (tb != typeb)
1264 // Look for delegates to functions where the functions are overloaded.
1265 FuncDeclaration *f;
1267 if (typeb->ty == Tdelegate && typeb->nextOf()->ty == Tfunction &&
1268 tb->ty == Tdelegate && tb->nextOf()->ty == Tfunction)
1270 if (func)
1272 f = func->overloadExactMatch(tb->nextOf());
1273 if (f)
1274 { target_ptrdiff_t offset;
1275 if (f->tintro && f->tintro->nextOf()->isBaseOf(f->type->nextOf(), &offset) && offset)
1276 error("%s", msg);
1277 f->tookAddressOf = 1;
1278 e = new DelegateExp(loc, e1, f);
1279 e->type = t;
1280 return e;
1282 if (func->tintro)
1283 error("%s", msg);
1286 e = Expression::castTo(sc, t);
1288 else
1289 { target_ptrdiff_t offset;
1291 func->tookAddressOf = 1;
1292 if (func->tintro && func->tintro->nextOf()->isBaseOf(func->type->nextOf(), &offset) && offset)
1293 error("%s", msg);
1294 e = copy();
1295 e->type = t;
1297 return e;
1300 Expression *CondExp::castTo(Scope *sc, Type *t)
1302 Expression *e = this;
1304 if (type != t)
1306 if (1 || e1->op == TOKstring || e2->op == TOKstring)
1307 { e = new CondExp(loc, econd, e1->castTo(sc, t), e2->castTo(sc, t));
1308 e->type = t;
1310 else
1311 e = Expression::castTo(sc, t);
1313 return e;
1316 /* ==================== ====================== */
1318 /****************************************
1319 * Scale addition/subtraction to/from pointer.
1322 Expression *BinExp::scaleFactor(Scope *sc)
1323 { d_uns64 stride;
1324 Type *t1b = e1->type->toBasetype();
1325 Type *t2b = e2->type->toBasetype();
1327 if (t1b->ty == Tpointer && t2b->isintegral())
1328 { // Need to adjust operator by the stride
1329 // Replace (ptr + int) with (ptr + (int * stride))
1330 Type *t = Type::tptrdiff_t;
1332 stride = t1b->nextOf()->size(loc);
1333 if (!t->equals(t2b))
1334 e2 = e2->castTo(sc, t);
1335 e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t));
1336 e2->type = t;
1337 type = e1->type;
1339 else if (t2b->ty && t1b->isintegral())
1340 { // Need to adjust operator by the stride
1341 // Replace (int + ptr) with (ptr + (int * stride))
1342 Type *t = Type::tptrdiff_t;
1343 Expression *e;
1345 stride = t2b->nextOf()->size(loc);
1346 if (!t->equals(t1b))
1347 e = e1->castTo(sc, t);
1348 else
1349 e = e1;
1350 e = new MulExp(loc, e, new IntegerExp(0, stride, t));
1351 e->type = t;
1352 type = e2->type;
1353 e1 = e2;
1354 e2 = e;
1356 return this;
1359 /************************************
1360 * Bring leaves to common type.
1363 Expression *BinExp::typeCombine(Scope *sc)
1365 Type *t1;
1366 Type *t2;
1367 Type *t;
1368 TY ty;
1370 //printf("BinExp::typeCombine() %s\n", toChars());
1371 //dump(0);
1373 e1 = e1->integralPromotions(sc);
1374 e2 = e2->integralPromotions(sc);
1376 // BUG: do toBasetype()
1377 t1 = e1->type;
1378 t2 = e2->type;
1379 assert(t1);
1380 t = t1;
1382 //if (t1) printf("\tt1 = %s\n", t1->toChars());
1383 //if (t2) printf("\tt2 = %s\n", t2->toChars());
1384 #ifdef DEBUG
1385 if (!t2) printf("\te2 = '%s'\n", e2->toChars());
1386 #endif
1387 assert(t2);
1389 Type *t1b = t1->toBasetype();
1390 Type *t2b = t2->toBasetype();
1392 ty = (TY)Type::impcnvResult[t1b->ty][t2b->ty];
1393 if (ty != Terror)
1394 { TY ty1;
1395 TY ty2;
1397 ty1 = (TY)Type::impcnvType1[t1b->ty][t2b->ty];
1398 ty2 = (TY)Type::impcnvType2[t1b->ty][t2b->ty];
1400 if (t1b->ty == ty1) // if no promotions
1402 if (t1 == t2)
1404 if (!type)
1405 type = t1;
1406 return this;
1409 if (t1b == t2b)
1411 if (!type)
1412 type = t1b;
1413 return this;
1417 if (!type)
1418 type = Type::basic[ty];
1420 t1 = Type::basic[ty1];
1421 t2 = Type::basic[ty2];
1422 e1 = e1->castTo(sc, t1);
1423 e2 = e2->castTo(sc, t2);
1424 #if 0
1425 if (type != Type::basic[ty])
1426 { t = type;
1427 type = Type::basic[ty];
1428 return castTo(sc, t);
1430 #endif
1431 //printf("after typeCombine():\n");
1432 //dump(0);
1433 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
1434 return this;
1437 t1 = t1b;
1438 t2 = t2b;
1440 if (op == TOKcat)
1442 if ((t1->ty == Tsarray || t1->ty == Tarray) &&
1443 (t2->ty == Tsarray || t2->ty == Tarray) &&
1444 (t1->nextOf()->mod || t2->nextOf()->mod) &&
1445 (t1->nextOf()->mod != t2->nextOf()->mod)
1448 t1 = t1->nextOf()->mutableOf()->constOf()->arrayOf();
1449 t2 = t2->nextOf()->mutableOf()->constOf()->arrayOf();
1450 //t1 = t1->constOf();
1451 //t2 = t2->constOf();
1452 if (e1->op == TOKstring && !((StringExp *)e1)->committed)
1453 e1->type = t1;
1454 else
1455 e1 = e1->castTo(sc, t1);
1456 if (e2->op == TOKstring && !((StringExp *)e2)->committed)
1457 e2->type = t2;
1458 else
1459 e2 = e2->castTo(sc, t2);
1460 t = t1;
1461 goto Lagain;
1465 Lagain:
1466 if (t1 == t2)
1468 if ((t1->ty == Tstruct || t1->ty == Tclass) &&
1469 (op == TOKmin || op == TOKadd))
1470 goto Lincompatible;
1472 else if (t1->ty == Tpointer && t2->ty == Tpointer)
1474 // Bring pointers to compatible type
1475 Type *t1n = t1->nextOf();
1476 Type *t2n = t2->nextOf();
1478 if (t1n == t2n)
1480 else if (t1n->ty == Tvoid) // pointers to void are always compatible
1481 t = t2;
1482 else if (t2n->ty == Tvoid)
1484 else if (t1n->mod != t2n->mod)
1486 t1 = t1n->mutableOf()->constOf()->pointerTo();
1487 t2 = t2n->mutableOf()->constOf()->pointerTo();
1488 t = t1;
1489 goto Lagain;
1491 else if (t1n->ty == Tclass && t2n->ty == Tclass)
1492 { ClassDeclaration *cd1 = t1n->isClassHandle();
1493 ClassDeclaration *cd2 = t2n->isClassHandle();
1494 target_ptrdiff_t offset;
1496 if (cd1->isBaseOf(cd2, &offset))
1498 if (offset)
1499 e2 = e2->castTo(sc, t);
1501 else if (cd2->isBaseOf(cd1, &offset))
1503 t = t2;
1504 if (offset)
1505 e1 = e1->castTo(sc, t);
1507 else
1508 goto Lincompatible;
1510 else
1511 goto Lincompatible;
1513 else if ((t1->ty == Tsarray || t1->ty == Tarray) &&
1514 e2->op == TOKnull && t2->ty == Tpointer && t2->nextOf()->ty == Tvoid)
1515 { /* (T[n] op void*)
1516 * (T[] op void*)
1518 goto Lx1;
1520 else if ((t2->ty == Tsarray || t2->ty == Tarray) &&
1521 e1->op == TOKnull && t1->ty == Tpointer && t1->nextOf()->ty == Tvoid)
1522 { /* (void* op T[n])
1523 * (void* op T[])
1525 goto Lx2;
1527 else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2))
1529 goto Lt2;
1531 else if ((t2->ty == Tsarray || t2->ty == Tarray) && t2->implicitConvTo(t1))
1533 goto Lt1;
1535 /* If one is mutable and the other invariant, then retry
1536 * with both of them as const
1538 else if ((t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Tpointer) &&
1539 (t2->ty == Tsarray || t2->ty == Tarray || t2->ty == Tpointer) &&
1540 t1->nextOf()->mod != t2->nextOf()->mod
1543 if (t1->ty == Tpointer)
1544 t1 = t1->nextOf()->mutableOf()->constOf()->pointerTo();
1545 else
1546 t1 = t1->nextOf()->mutableOf()->constOf()->arrayOf();
1548 if (t2->ty == Tpointer)
1549 t2 = t2->nextOf()->mutableOf()->constOf()->pointerTo();
1550 else
1551 t2 = t2->nextOf()->mutableOf()->constOf()->arrayOf();
1552 t = t1;
1553 goto Lagain;
1555 else if (t1->ty == Tclass || t2->ty == Tclass)
1557 while (1)
1559 int i1 = e2->implicitConvTo(t1);
1560 int i2 = e1->implicitConvTo(t2);
1562 if (i1 && i2)
1564 // We have the case of class vs. void*, so pick class
1565 if (t1->ty == Tpointer)
1566 i1 = 0;
1567 else if (t2->ty == Tpointer)
1568 i2 = 0;
1571 if (i2)
1573 goto Lt2;
1575 else if (i1)
1577 goto Lt1;
1579 else if (t1->ty == Tclass && t2->ty == Tclass)
1580 { TypeClass *tc1 = (TypeClass *)t1;
1581 TypeClass *tc2 = (TypeClass *)t2;
1583 /* Pick 'tightest' type
1585 ClassDeclaration *cd1 = tc1->sym->baseClass;
1586 ClassDeclaration *cd2 = tc1->sym->baseClass;
1588 if (cd1 && cd2)
1589 { t1 = cd1->type;
1590 t2 = cd2->type;
1592 else if (cd1)
1593 t1 = cd1->type;
1594 else if (cd2)
1595 t2 = cd2->type;
1596 else
1597 goto Lincompatible;
1599 else
1600 goto Lincompatible;
1603 else if (t1->ty == Tstruct && t2->ty == Tstruct)
1605 if (((TypeStruct *)t1)->sym != ((TypeStruct *)t2)->sym)
1606 goto Lincompatible;
1608 else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2))
1610 goto Lt2;
1612 else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1))
1614 goto Lt1;
1616 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
1617 e2->implicitConvTo(t1->nextOf()->arrayOf()))
1619 Lx1:
1620 t = t1->nextOf()->arrayOf();
1621 e1 = e1->castTo(sc, t);
1622 e2 = e2->castTo(sc, t);
1624 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
1625 e1->implicitConvTo(t2->nextOf()->arrayOf()))
1627 Lx2:
1628 t = t2->nextOf()->arrayOf();
1629 e1 = e1->castTo(sc, t);
1630 e2 = e2->castTo(sc, t);
1632 else if (t1->isintegral() && t2->isintegral())
1634 assert(0);
1636 else
1638 Lincompatible:
1639 incompatibleTypes();
1641 Lret:
1642 if (!type)
1643 type = t;
1644 #if 0
1645 printf("-BinExp::typeCombine() %s\n", toChars());
1646 if (e1->type) printf("\tt1 = %s\n", e1->type->toChars());
1647 if (e2->type) printf("\tt2 = %s\n", e2->type->toChars());
1648 printf("\ttype = %s\n", type->toChars());
1649 #endif
1650 //dump(0);
1651 return this;
1654 Lt1:
1655 e2 = e2->castTo(sc, t1);
1656 t = t1;
1657 goto Lret;
1659 Lt2:
1660 e1 = e1->castTo(sc, t2);
1661 t = t2;
1662 goto Lret;
1665 /***********************************
1666 * Do integral promotions (convertchk).
1667 * Don't convert <array of> to <pointer to>
1670 Expression *Expression::integralPromotions(Scope *sc)
1672 Expression *e = this;
1674 //printf("integralPromotions %s %s\n", e->toChars(), e->type->toChars());
1675 switch (type->toBasetype()->ty)
1677 case Tvoid:
1678 error("void has no value");
1679 break;
1681 case Tint8:
1682 case Tuns8:
1683 case Tint16:
1684 case Tuns16:
1685 case Tbit:
1686 case Tbool:
1687 case Tchar:
1688 case Twchar:
1689 e = e->castTo(sc, Type::tint32);
1690 break;
1692 case Tdchar:
1693 e = e->castTo(sc, Type::tuns32);
1694 break;
1696 return e;