Makefile.in: host_alias to host
[delight/core.git] / dmd2 / cast.c
blob79492b351c5ee76d8cb909772db30757d288d8e1
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 // (void *) 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;
389 // Can always convert to these
390 if (t->ty == Taarray || t->ty == Tmaybe ||
391 t->ty == Tdelegate)
392 return committed ? MATCHconvert : MATCHexact;
394 // Converting to dynamic arrays, pointers and classes not allowed in Delight
395 if (dUnchecked && (t->ty == Tarray || t->ty == Tpointer || t->ty == Tclass))
396 return committed ? MATCHconvert : MATCHexact;
399 if (!dUnchecked)
401 /* In Delight, null can only be implicitly converted to a maybe type */
402 if (t->ty != Tmaybe)
403 return MATCHnomatch;
405 t = t->nextOf();
408 return Expression::implicitConvTo(t);
411 MATCH StructLiteralExp::implicitConvTo(Type *t)
413 #if 0
414 printf("StructLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
415 toChars(), type->toChars(), t->toChars());
416 #endif
417 MATCH m = Expression::implicitConvTo(t);
418 if (m != MATCHnomatch)
419 return m;
420 if (type->ty == t->ty && type->ty == Tstruct &&
421 ((TypeStruct *)type)->sym == ((TypeStruct *)t)->sym)
423 m = MATCHconst;
424 for (int i = 0; i < elements->dim; i++)
425 { Expression *e = (Expression *)elements->data[i];
426 Type *te = e->type;
427 if (t->mod == 0)
428 te = te->mutableOf();
429 else
430 { assert(t->mod == MODinvariant);
431 te = te->invariantOf();
433 MATCH m2 = e->implicitConvTo(te);
434 //printf("\t%s => %s, match = %d\n", e->toChars(), te->toChars(), m2);
435 if (m2 < m)
436 m = m2;
439 return m;
442 MATCH StringExp::implicitConvTo(Type *t)
443 { MATCH m;
445 #if 0
446 printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
447 toChars(), committed, type->toChars(), t->toChars());
448 #endif
449 if (!committed)
451 if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
453 return MATCHnomatch;
455 if (type->ty == Tsarray || type->ty == Tarray || type->ty == Tpointer)
457 TY tyn = type->nextOf()->ty;
458 if (tyn == Tchar || tyn == Twchar || tyn == Tdchar)
459 { Type *tn;
460 MATCH m;
462 switch (t->ty)
464 case Tsarray:
465 if (type->ty == Tsarray)
467 if (((TypeSArray *)type)->dim->toInteger() !=
468 ((TypeSArray *)t)->dim->toInteger())
469 return MATCHnomatch;
470 TY tynto = t->nextOf()->ty;
471 if (tynto == Tchar || tynto == Twchar || tynto == Tdchar)
472 return MATCHexact;
474 case Tarray:
475 case Tpointer:
476 tn = t->nextOf();
477 m = MATCHexact;
478 if (type->nextOf()->mod != tn->mod)
479 { if (!tn->isConst())
480 return MATCHnomatch;
481 m = MATCHconst;
483 switch (tn->ty)
485 case Tchar:
486 case Twchar:
487 case Tdchar:
488 return m;
490 break;
495 return Expression::implicitConvTo(t);
496 #if 0
497 m = (MATCH)type->implicitConvTo(t);
498 if (m)
500 return m;
503 return MATCHnomatch;
504 #endif
507 MATCH ArrayLiteralExp::implicitConvTo(Type *t)
508 { MATCH result = MATCHexact;
510 #if 0
511 printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
512 toChars(), type->toChars(), t->toChars());
513 #endif
514 Type *typeb = type->toBasetype();
515 Type *tb = t->toBasetype();
516 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
517 (typeb->ty == Tarray || typeb->ty == Tsarray))
519 if (tb->ty == Tsarray)
520 { TypeSArray *tsa = (TypeSArray *)tb;
521 if (elements->dim != tsa->dim->toInteger())
522 result = MATCHnomatch;
525 for (int i = 0; i < elements->dim; i++)
526 { Expression *e = (Expression *)elements->data[i];
527 MATCH m = (MATCH)e->implicitConvTo(tb->nextOf());
528 if (m < result)
529 result = m; // remember worst match
530 if (result == MATCHnomatch)
531 break; // no need to check for worse
533 return result;
535 else
536 return Expression::implicitConvTo(t);
539 MATCH AssocArrayLiteralExp::implicitConvTo(Type *t)
540 { MATCH result = MATCHexact;
542 Type *typeb = type->toBasetype();
543 Type *tb = t->toBasetype();
544 if (tb->ty == Taarray && typeb->ty == Taarray)
546 for (size_t i = 0; i < keys->dim; i++)
547 { Expression *e = (Expression *)keys->data[i];
548 MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->index);
549 if (m < result)
550 result = m; // remember worst match
551 if (result == MATCHnomatch)
552 break; // no need to check for worse
553 e = (Expression *)values->data[i];
554 m = (MATCH)e->implicitConvTo(tb->nextOf());
555 if (m < result)
556 result = m; // remember worst match
557 if (result == MATCHnomatch)
558 break; // no need to check for worse
560 return result;
562 else
563 return Expression::implicitConvTo(t);
566 MATCH AddrExp::implicitConvTo(Type *t)
568 #if 0
569 printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
570 toChars(), type->toChars(), t->toChars());
571 #endif
572 MATCH result;
574 result = type->implicitConvTo(t);
575 //printf("\tresult = %d\n", result);
577 if (result == MATCHnomatch)
579 // Look for pointers to functions where the functions are overloaded.
581 t = t->toBasetype();
583 if (e1->op == TOKoverloadset &&
584 (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction)
585 { OverExp *eo = (OverExp *)e1;
586 FuncDeclaration *f = NULL;
587 for (int i = 0; i < eo->vars->a.dim; i++)
588 { Dsymbol *s = (Dsymbol *)eo->vars->a.data[i];
589 FuncDeclaration *f2 = s->isFuncDeclaration();
590 assert(f2);
591 if (f2->overloadExactMatch(t->nextOf()))
592 { if (f)
593 /* Error if match in more than one overload set,
594 * even if one is a 'better' match than the other.
596 ScopeDsymbol::multiplyDefined(loc, f, f2);
597 else
598 f = f2;
599 result = MATCHexact;
604 if (type->ty == Tpointer && type->nextOf()->ty == Tfunction &&
605 t->ty == Tpointer && t->nextOf()->ty == Tfunction &&
606 e1->op == TOKvar)
608 /* I don't think this can ever happen -
609 * it should have been
610 * converted to a SymOffExp.
612 assert(0);
613 VarExp *ve = (VarExp *)e1;
614 FuncDeclaration *f = ve->var->isFuncDeclaration();
615 if (f && f->overloadExactMatch(t->nextOf()))
616 result = MATCHexact;
619 //printf("\tresult = %d\n", result);
620 return result;
623 MATCH SymOffExp::implicitConvTo(Type *t)
625 #if 0
626 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
627 toChars(), type->toChars(), t->toChars());
628 #endif
629 MATCH result;
631 result = type->implicitConvTo(t);
632 //printf("\tresult = %d\n", result);
634 if (result == MATCHnomatch)
636 // Look for pointers to functions where the functions are overloaded.
637 FuncDeclaration *f;
639 t = t->toBasetype();
640 if (type->ty == Tpointer && type->nextOf()->ty == Tfunction &&
641 (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction)
643 f = var->isFuncDeclaration();
644 if (f)
645 { f = f->overloadExactMatch(t->nextOf());
646 if (f)
647 { if ((t->ty == Tdelegate && (f->needThis() || f->isNested())) ||
648 (t->ty == Tpointer && !(f->needThis() || f->isNested())))
650 result = MATCHexact;
656 //printf("\tresult = %d\n", result);
657 return result;
660 MATCH DelegateExp::implicitConvTo(Type *t)
662 #if 0
663 printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
664 toChars(), type->toChars(), t->toChars());
665 #endif
666 MATCH result;
668 result = type->implicitConvTo(t);
670 if (result == MATCHnomatch)
672 // Look for pointers to functions where the functions are overloaded.
673 FuncDeclaration *f;
675 t = t->toBasetype();
676 if (type->ty == Tdelegate && type->nextOf()->ty == Tfunction &&
677 t->ty == Tdelegate && t->nextOf()->ty == Tfunction)
679 if (func && func->overloadExactMatch(t->nextOf()))
680 result = MATCHexact;
683 return result;
686 MATCH CondExp::implicitConvTo(Type *t)
688 MATCH m1;
689 MATCH m2;
691 m1 = e1->implicitConvTo(t);
692 m2 = e2->implicitConvTo(t);
694 // Pick the worst match
695 return (m1 < m2) ? m1 : m2;
699 /* ==================== castTo ====================== */
701 /**************************************
702 * Do an explicit cast.
705 Expression *Expression::castTo(Scope *sc, Type *t)
707 //printf("Expression::castTo(this=%s, t=%s)\n", toChars(), t->toChars());
708 #if 0
709 printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
710 toChars(), type->toChars(), t->toChars());
711 #endif
712 if (type == t)
713 return this;
714 Expression *e = this;
715 Type *tb = t->toBasetype();
716 Type *typeb = type->toBasetype();
717 if (tb != typeb)
719 // Do (type *) cast of (type [dim])
720 if (tb->ty == Tpointer &&
721 typeb->ty == Tsarray
724 //printf("Converting [dim] to *\n");
726 if (typeb->size(loc) == 0)
727 e = new NullExp(loc);
728 else
729 e = new AddrExp(loc, e);
731 #if 0
732 else if (tb->ty == Tdelegate && type->ty != Tdelegate)
734 TypeDelegate *td = (TypeDelegate *)tb;
735 TypeFunction *tf = (TypeFunction *)td->nextOf();
736 return toDelegate(sc, tf->nextOf());
738 #endif
739 else
741 e = new CastExp(loc, e, tb);
744 else
746 e = e->copy(); // because of COW for assignment to e->type
748 assert(e != this);
749 e->type = t;
750 //printf("Returning: %s\n", e->toChars());
751 return e;
755 Expression *RealExp::castTo(Scope *sc, Type *t)
756 { Expression *e = this;
757 if (type != t)
759 if ((type->isreal() && t->isreal()) ||
760 (type->isimaginary() && t->isimaginary())
762 { e = copy();
763 e->type = t;
765 else
766 e = Expression::castTo(sc, t);
768 return e;
772 Expression *ComplexExp::castTo(Scope *sc, Type *t)
773 { Expression *e = this;
774 if (type != t)
776 if (type->iscomplex() && t->iscomplex())
777 { e = copy();
778 e->type = t;
780 else
781 e = Expression::castTo(sc, t);
783 return e;
787 Expression *NullExp::castTo(Scope *sc, Type *t)
788 { NullExp *e;
789 Type *tb;
791 //printf("NullExp::castTo(t = %p)\n", t);
792 if (type == t)
794 committed = 1;
795 return this;
797 e = (NullExp *)copy();
798 e->committed = 1;
799 tb = t->toBasetype();
800 e->type = type->toBasetype();
801 if (tb != e->type)
803 // NULL implicitly converts to any pointer type or dynamic array
804 if (e->type->ty == Tpointer && e->type->nextOf()->ty == Tvoid &&
805 (tb->ty == Tpointer || tb->ty == Tarray || tb->ty == Taarray ||
806 tb->ty == Tdelegate))
808 #if 0
809 if (tb->ty == Tdelegate)
810 { TypeDelegate *td = (TypeDelegate *)tb;
811 TypeFunction *tf = (TypeFunction *)td->nextOf();
813 if (!tf->varargs &&
814 !(tf->arguments && tf->arguments->dim)
817 return Expression::castTo(sc, t);
820 #endif
822 else
824 return e->Expression::castTo(sc, t);
827 e->type = t;
828 return e;
831 Expression *StringExp::castTo(Scope *sc, Type *t)
833 /* This follows copy-on-write; any changes to 'this'
834 * will result in a copy.
835 * The this->string member is considered immutable.
837 StringExp *se;
838 Type *tb;
839 int copied = 0;
841 //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed);
843 if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
845 error("cannot convert string literal to void*");
848 se = this;
849 if (!committed)
850 { se = (StringExp *)copy();
851 se->committed = 1;
852 copied = 1;
855 if (type == t)
857 return se;
860 tb = t->toBasetype();
861 //printf("\ttype = %s\n", type->toChars());
862 if (tb->ty == Tdelegate && type->toBasetype()->ty != Tdelegate)
863 return Expression::castTo(sc, t);
865 Type *typeb = type->toBasetype();
866 if (typeb == tb)
868 if (!copied)
869 { se = (StringExp *)copy();
870 copied = 1;
872 se->type = t;
873 return se;
876 if (tb->ty != Tsarray && tb->ty != Tarray && tb->ty != Tpointer)
877 { if (!copied)
878 { se = (StringExp *)copy();
879 copied = 1;
881 goto Lcast;
883 if (typeb->ty != Tsarray && typeb->ty != Tarray && typeb->ty != Tpointer)
884 { if (!copied)
885 { se = (StringExp *)copy();
886 copied = 1;
888 goto Lcast;
891 if (typeb->nextOf()->size() == tb->nextOf()->size())
893 if (!copied)
894 { se = (StringExp *)copy();
895 copied = 1;
897 if (tb->ty == Tsarray)
898 goto L2; // handle possible change in static array dimension
899 se->type = t;
900 return se;
903 if (committed)
904 goto Lcast;
906 #define X(tf,tt) ((tf) * 256 + (tt))
908 OutBuffer buffer;
909 size_t newlen = 0;
910 int tfty = typeb->nextOf()->toBasetype()->ty;
911 int ttty = tb->nextOf()->toBasetype()->ty;
912 switch (X(tfty, ttty))
914 case X(Tchar, Tchar):
915 case X(Twchar,Twchar):
916 case X(Tdchar,Tdchar):
917 break;
919 case X(Tchar, Twchar):
920 for (size_t u = 0; u < len;)
921 { unsigned c;
922 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
923 if (p)
924 error("%s", p);
925 else
926 buffer.writeUTF16(c);
928 newlen = buffer.offset / 2;
929 buffer.writeUTF16(0);
930 goto L1;
932 case X(Tchar, Tdchar):
933 for (size_t u = 0; u < len;)
934 { unsigned c;
935 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
936 if (p)
937 error("%s", p);
938 buffer.write4(c);
939 newlen++;
941 buffer.write4(0);
942 goto L1;
944 case X(Twchar,Tchar):
945 for (size_t u = 0; u < len;)
946 { unsigned c;
947 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
948 if (p)
949 error("%s", p);
950 else
951 buffer.writeUTF8(c);
953 newlen = buffer.offset;
954 buffer.writeUTF8(0);
955 goto L1;
957 case X(Twchar,Tdchar):
958 for (size_t u = 0; u < len;)
959 { unsigned c;
960 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
961 if (p)
962 error("%s", p);
963 buffer.write4(c);
964 newlen++;
966 buffer.write4(0);
967 goto L1;
969 case X(Tdchar,Tchar):
970 for (size_t u = 0; u < len; u++)
972 unsigned c = ((unsigned *)se->string)[u];
973 if (!utf_isValidDchar(c))
974 error("invalid UCS-32 char \\U%08x", c);
975 else
976 buffer.writeUTF8(c);
977 newlen++;
979 newlen = buffer.offset;
980 buffer.writeUTF8(0);
981 goto L1;
983 case X(Tdchar,Twchar):
984 for (size_t u = 0; u < len; u++)
986 unsigned c = ((unsigned *)se->string)[u];
987 if (!utf_isValidDchar(c))
988 error("invalid UCS-32 char \\U%08x", c);
989 else
990 buffer.writeUTF16(c);
991 newlen++;
993 newlen = buffer.offset / 2;
994 buffer.writeUTF16(0);
995 goto L1;
998 if (!copied)
999 { se = (StringExp *)copy();
1000 copied = 1;
1002 se->string = buffer.extractData();
1003 se->len = newlen;
1004 se->sz = tb->nextOf()->size();
1005 break;
1007 default:
1008 assert(typeb->nextOf()->size() != tb->nextOf()->size());
1009 goto Lcast;
1012 #undef X
1014 assert(copied);
1016 // See if need to truncate or extend the literal
1017 if (tb->ty == Tsarray)
1019 int dim2 = ((TypeSArray *)tb)->dim->toInteger();
1021 //printf("dim from = %d, to = %d\n", se->len, dim2);
1023 // Changing dimensions
1024 if (dim2 != se->len)
1026 // Copy when changing the string literal
1027 unsigned newsz = se->sz;
1028 void *s;
1029 int d;
1031 d = (dim2 < se->len) ? dim2 : se->len;
1032 s = (unsigned char *)mem.malloc((dim2 + 1) * newsz);
1033 memcpy(s, se->string, d * newsz);
1034 // Extend with 0, add terminating 0
1035 memset((char *)s + d * newsz, 0, (dim2 + 1 - d) * newsz);
1036 se->string = s;
1037 se->len = dim2;
1040 se->type = t;
1041 return se;
1043 Lcast:
1044 Expression *e = new CastExp(loc, se, t);
1045 e->type = t; // so semantic() won't be run on e
1046 return e;
1049 Expression *AddrExp::castTo(Scope *sc, Type *t)
1051 Type *tb;
1053 #if 0
1054 printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
1055 toChars(), type->toChars(), t->toChars());
1056 #endif
1057 Expression *e = this;
1059 tb = t->toBasetype();
1060 type = type->toBasetype();
1061 if (tb != type)
1063 // Look for pointers to functions where the functions are overloaded.
1065 if (e1->op == TOKoverloadset &&
1066 (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction)
1067 { OverExp *eo = (OverExp *)e1;
1068 FuncDeclaration *f = NULL;
1069 for (int i = 0; i < eo->vars->a.dim; i++)
1070 { Dsymbol *s = (Dsymbol *)eo->vars->a.data[i];
1071 FuncDeclaration *f2 = s->isFuncDeclaration();
1072 assert(f2);
1073 if (f2->overloadExactMatch(t->nextOf()))
1074 { if (f)
1075 /* Error if match in more than one overload set,
1076 * even if one is a 'better' match than the other.
1078 ScopeDsymbol::multiplyDefined(loc, f, f2);
1079 else
1080 f = f2;
1083 if (f)
1084 { f->tookAddressOf = 1;
1085 SymOffExp *se = new SymOffExp(loc, f, 0, 0);
1086 se->semantic(sc);
1087 // Let SymOffExp::castTo() do the heavy lifting
1088 return se->castTo(sc, t);
1093 if (type->ty == Tpointer && type->nextOf()->ty == Tfunction &&
1094 tb->ty == Tpointer && tb->nextOf()->ty == Tfunction &&
1095 e1->op == TOKvar)
1097 VarExp *ve = (VarExp *)e1;
1098 FuncDeclaration *f = ve->var->isFuncDeclaration();
1099 if (f)
1101 assert(0); // should be SymOffExp instead
1102 f = f->overloadExactMatch(tb->nextOf());
1103 if (f)
1105 e = new VarExp(loc, f);
1106 e->type = f->type;
1107 e = new AddrExp(loc, e);
1108 e->type = t;
1109 return e;
1113 e = Expression::castTo(sc, t);
1115 e->type = t;
1116 return e;
1120 Expression *TupleExp::castTo(Scope *sc, Type *t)
1121 { TupleExp *e = (TupleExp *)copy();
1122 e->exps = (Expressions *)exps->copy();
1123 for (size_t i = 0; i < e->exps->dim; i++)
1124 { Expression *ex = (Expression *)e->exps->data[i];
1125 ex = ex->castTo(sc, t);
1126 e->exps->data[i] = (void *)ex;
1128 return e;
1132 Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t)
1134 #if 0
1135 printf("ArrayLiteralExp::castTo(this=%s, type=%s, => %s)\n",
1136 toChars(), type->toChars(), t->toChars());
1137 #endif
1138 if (type == t)
1139 return this;
1140 ArrayLiteralExp *e = this;
1141 Type *typeb = type->toBasetype();
1142 Type *tb = t->toBasetype();
1143 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
1144 (typeb->ty == Tarray || typeb->ty == Tsarray) &&
1145 // Not trying to convert non-void[] to void[]
1146 !(tb->nextOf()->toBasetype()->ty == Tvoid && typeb->nextOf()->toBasetype()->ty != Tvoid))
1148 if (tb->ty == Tsarray)
1149 { TypeSArray *tsa = (TypeSArray *)tb;
1150 if (elements->dim != tsa->dim->toInteger())
1151 goto L1;
1154 e = (ArrayLiteralExp *)copy();
1155 e->elements = (Expressions *)elements->copy();
1156 for (int i = 0; i < elements->dim; i++)
1157 { Expression *ex = (Expression *)elements->data[i];
1158 ex = ex->castTo(sc, tb->nextOf());
1159 e->elements->data[i] = (void *)ex;
1161 e->type = t;
1162 return e;
1164 if (tb->ty == Tpointer && typeb->ty == Tsarray)
1166 e = (ArrayLiteralExp *)copy();
1167 e->type = typeb->nextOf()->pointerTo();
1170 return e->Expression::castTo(sc, t);
1173 Expression *AssocArrayLiteralExp::castTo(Scope *sc, Type *t)
1175 if (type == t)
1176 return this;
1177 AssocArrayLiteralExp *e = this;
1178 Type *typeb = type->toBasetype();
1179 Type *tb = t->toBasetype();
1180 if (tb->ty == Taarray && typeb->ty == Taarray &&
1181 tb->nextOf()->toBasetype()->ty != Tvoid)
1183 e = (AssocArrayLiteralExp *)copy();
1184 e->keys = (Expressions *)keys->copy();
1185 e->values = (Expressions *)values->copy();
1186 assert(keys->dim == values->dim);
1187 for (size_t i = 0; i < keys->dim; i++)
1188 { Expression *ex = (Expression *)values->data[i];
1189 ex = ex->castTo(sc, tb->nextOf());
1190 e->values->data[i] = (void *)ex;
1192 ex = (Expression *)keys->data[i];
1193 ex = ex->castTo(sc, ((TypeAArray *)tb)->index);
1194 e->keys->data[i] = (void *)ex;
1196 e->type = t;
1197 return e;
1200 return e->Expression::castTo(sc, t);
1203 Expression *SymOffExp::castTo(Scope *sc, Type *t)
1205 #if 0
1206 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
1207 toChars(), type->toChars(), t->toChars());
1208 #endif
1209 if (type == t && hasOverloads == 0)
1210 return this;
1211 Expression *e;
1212 Type *tb = t->toBasetype();
1213 Type *typeb = type->toBasetype();
1214 if (tb != typeb)
1216 // Look for pointers to functions where the functions are overloaded.
1217 FuncDeclaration *f;
1219 if (hasOverloads &&
1220 typeb->ty == Tpointer && typeb->nextOf()->ty == Tfunction &&
1221 (tb->ty == Tpointer || tb->ty == Tdelegate) && tb->nextOf()->ty == Tfunction)
1223 f = var->isFuncDeclaration();
1224 if (f)
1226 f = f->overloadExactMatch(tb->nextOf());
1227 if (f)
1229 if (tb->ty == Tdelegate && f->needThis() && hasThis(sc))
1231 e = new DelegateExp(loc, new ThisExp(loc), f);
1232 e = e->semantic(sc);
1234 else if (tb->ty == Tdelegate && f->isNested())
1236 e = new DelegateExp(loc, new IntegerExp(0), f);
1237 e = e->semantic(sc);
1239 else
1241 e = new SymOffExp(loc, f, 0);
1242 e->type = t;
1244 f->tookAddressOf = 1;
1245 return e;
1249 e = Expression::castTo(sc, t);
1251 else
1252 { e = copy();
1253 e->type = t;
1254 ((SymOffExp *)e)->hasOverloads = 0;
1256 return e;
1259 Expression *DelegateExp::castTo(Scope *sc, Type *t)
1261 #if 0
1262 printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
1263 toChars(), type->toChars(), t->toChars());
1264 #endif
1265 static char msg[] = "cannot form delegate due to covariant return type";
1267 Expression *e = this;
1268 Type *tb = t->toBasetype();
1269 Type *typeb = type->toBasetype();
1270 if (tb != typeb)
1272 // Look for delegates to functions where the functions are overloaded.
1273 FuncDeclaration *f;
1275 if (typeb->ty == Tdelegate && typeb->nextOf()->ty == Tfunction &&
1276 tb->ty == Tdelegate && tb->nextOf()->ty == Tfunction)
1278 if (func)
1280 f = func->overloadExactMatch(tb->nextOf());
1281 if (f)
1282 { target_ptrdiff_t offset;
1283 if (f->tintro && f->tintro->nextOf()->isBaseOf(f->type->nextOf(), &offset) && offset)
1284 error("%s", msg);
1285 f->tookAddressOf = 1;
1286 e = new DelegateExp(loc, e1, f);
1287 e->type = t;
1288 return e;
1290 if (func->tintro)
1291 error("%s", msg);
1294 e = Expression::castTo(sc, t);
1296 else
1297 { target_ptrdiff_t offset;
1299 func->tookAddressOf = 1;
1300 if (func->tintro && func->tintro->nextOf()->isBaseOf(func->type->nextOf(), &offset) && offset)
1301 error("%s", msg);
1302 e = copy();
1303 e->type = t;
1305 return e;
1308 Expression *CondExp::castTo(Scope *sc, Type *t)
1310 Expression *e = this;
1312 if (type != t)
1314 if (1 || e1->op == TOKstring || e2->op == TOKstring)
1315 { e = new CondExp(loc, econd, e1->castTo(sc, t), e2->castTo(sc, t));
1316 e->type = t;
1318 else
1319 e = Expression::castTo(sc, t);
1321 return e;
1324 /* ==================== ====================== */
1326 /****************************************
1327 * Scale addition/subtraction to/from pointer.
1330 Expression *BinExp::scaleFactor(Scope *sc)
1331 { d_uns64 stride;
1332 Type *t1b = e1->type->toBasetype();
1333 Type *t2b = e2->type->toBasetype();
1335 if (t1b->ty == Tpointer && t2b->isintegral())
1336 { // Need to adjust operator by the stride
1337 // Replace (ptr + int) with (ptr + (int * stride))
1338 Type *t = Type::tptrdiff_t;
1340 stride = t1b->nextOf()->size(loc);
1341 if (!t->equals(t2b))
1342 e2 = e2->castTo(sc, t);
1343 e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t));
1344 e2->type = t;
1345 type = e1->type;
1347 else if (t2b->ty && t1b->isintegral())
1348 { // Need to adjust operator by the stride
1349 // Replace (int + ptr) with (ptr + (int * stride))
1350 Type *t = Type::tptrdiff_t;
1351 Expression *e;
1353 stride = t2b->nextOf()->size(loc);
1354 if (!t->equals(t1b))
1355 e = e1->castTo(sc, t);
1356 else
1357 e = e1;
1358 e = new MulExp(loc, e, new IntegerExp(0, stride, t));
1359 e->type = t;
1360 type = e2->type;
1361 e1 = e2;
1362 e2 = e;
1364 return this;
1367 /************************************
1368 * Bring leaves to common type.
1371 Expression *BinExp::typeCombine(Scope *sc)
1373 Type *t1;
1374 Type *t2;
1375 Type *t;
1376 TY ty;
1378 //printf("BinExp::typeCombine() %s\n", toChars());
1379 //dump(0);
1381 e1 = e1->integralPromotions(sc);
1382 e2 = e2->integralPromotions(sc);
1384 // BUG: do toBasetype()
1385 t1 = e1->type;
1386 t2 = e2->type;
1387 assert(t1);
1388 t = t1;
1390 //if (t1) printf("\tt1 = %s\n", t1->toChars());
1391 //if (t2) printf("\tt2 = %s\n", t2->toChars());
1392 #ifdef DEBUG
1393 if (!t2) printf("\te2 = '%s'\n", e2->toChars());
1394 #endif
1395 assert(t2);
1397 Type *t1b = t1->toBasetype();
1398 Type *t2b = t2->toBasetype();
1400 ty = (TY)Type::impcnvResult[t1b->ty][t2b->ty];
1401 if (ty != Terror)
1402 { TY ty1;
1403 TY ty2;
1405 ty1 = (TY)Type::impcnvType1[t1b->ty][t2b->ty];
1406 ty2 = (TY)Type::impcnvType2[t1b->ty][t2b->ty];
1408 if (t1b->ty == ty1) // if no promotions
1410 if (t1 == t2)
1412 if (!type)
1413 type = t1;
1414 return this;
1417 if (t1b == t2b)
1419 if (!type)
1420 type = t1b;
1421 return this;
1425 if (!type)
1426 type = Type::basic[ty];
1428 t1 = Type::basic[ty1];
1429 t2 = Type::basic[ty2];
1430 e1 = e1->castTo(sc, t1);
1431 e2 = e2->castTo(sc, t2);
1432 #if 0
1433 if (type != Type::basic[ty])
1434 { t = type;
1435 type = Type::basic[ty];
1436 return castTo(sc, t);
1438 #endif
1439 //printf("after typeCombine():\n");
1440 //dump(0);
1441 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
1442 return this;
1445 t1 = t1b;
1446 t2 = t2b;
1448 if (op == TOKcat)
1450 if ((t1->ty == Tsarray || t1->ty == Tarray) &&
1451 (t2->ty == Tsarray || t2->ty == Tarray) &&
1452 (t1->nextOf()->mod || t2->nextOf()->mod) &&
1453 (t1->nextOf()->mod != t2->nextOf()->mod)
1456 t1 = t1->nextOf()->mutableOf()->constOf()->arrayOf();
1457 t2 = t2->nextOf()->mutableOf()->constOf()->arrayOf();
1458 //t1 = t1->constOf();
1459 //t2 = t2->constOf();
1460 if (e1->op == TOKstring && !((StringExp *)e1)->committed)
1461 e1->type = t1;
1462 else
1463 e1 = e1->castTo(sc, t1);
1464 if (e2->op == TOKstring && !((StringExp *)e2)->committed)
1465 e2->type = t2;
1466 else
1467 e2 = e2->castTo(sc, t2);
1468 t = t1;
1469 goto Lagain;
1473 Lagain:
1474 if (t1 == t2)
1476 if ((t1->ty == Tstruct || t1->ty == Tclass) &&
1477 (op == TOKmin || op == TOKadd))
1478 goto Lincompatible;
1480 else if (t1->ty == Tpointer && t2->ty == Tpointer)
1482 // Bring pointers to compatible type
1483 Type *t1n = t1->nextOf();
1484 Type *t2n = t2->nextOf();
1486 if (t1n == t2n)
1488 else if (t1n->ty == Tvoid) // pointers to void are always compatible
1489 t = t2;
1490 else if (t2n->ty == Tvoid)
1492 else if (t1n->mod != t2n->mod)
1494 t1 = t1n->mutableOf()->constOf()->pointerTo();
1495 t2 = t2n->mutableOf()->constOf()->pointerTo();
1496 t = t1;
1497 goto Lagain;
1499 else if (t1n->ty == Tclass && t2n->ty == Tclass)
1500 { ClassDeclaration *cd1 = t1n->isClassHandle();
1501 ClassDeclaration *cd2 = t2n->isClassHandle();
1502 target_ptrdiff_t offset;
1504 if (cd1->isBaseOf(cd2, &offset))
1506 if (offset)
1507 e2 = e2->castTo(sc, t);
1509 else if (cd2->isBaseOf(cd1, &offset))
1511 t = t2;
1512 if (offset)
1513 e1 = e1->castTo(sc, t);
1515 else
1516 goto Lincompatible;
1518 else
1519 goto Lincompatible;
1521 else if ((t1->ty == Tsarray || t1->ty == Tarray) &&
1522 e2->op == TOKnull && t2->ty == Tpointer && t2->nextOf()->ty == Tvoid)
1523 { /* (T[n] op void*)
1524 * (T[] op void*)
1526 goto Lx1;
1528 else if ((t2->ty == Tsarray || t2->ty == Tarray) &&
1529 e1->op == TOKnull && t1->ty == Tpointer && t1->nextOf()->ty == Tvoid)
1530 { /* (void* op T[n])
1531 * (void* op T[])
1533 goto Lx2;
1535 else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2))
1537 goto Lt2;
1539 else if ((t2->ty == Tsarray || t2->ty == Tarray) && t2->implicitConvTo(t1))
1541 goto Lt1;
1543 /* If one is mutable and the other invariant, then retry
1544 * with both of them as const
1546 else if ((t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Tpointer) &&
1547 (t2->ty == Tsarray || t2->ty == Tarray || t2->ty == Tpointer) &&
1548 t1->nextOf()->mod != t2->nextOf()->mod
1551 if (t1->ty == Tpointer)
1552 t1 = t1->nextOf()->mutableOf()->constOf()->pointerTo();
1553 else
1554 t1 = t1->nextOf()->mutableOf()->constOf()->arrayOf();
1556 if (t2->ty == Tpointer)
1557 t2 = t2->nextOf()->mutableOf()->constOf()->pointerTo();
1558 else
1559 t2 = t2->nextOf()->mutableOf()->constOf()->arrayOf();
1560 t = t1;
1561 goto Lagain;
1563 else if (t1->ty == Tclass || t2->ty == Tclass)
1565 while (1)
1567 int i1 = e2->implicitConvTo(t1);
1568 int i2 = e1->implicitConvTo(t2);
1570 if (i1 && i2)
1572 // We have the case of class vs. void*, so pick class
1573 if (t1->ty == Tpointer)
1574 i1 = 0;
1575 else if (t2->ty == Tpointer)
1576 i2 = 0;
1579 if (i2)
1581 goto Lt2;
1583 else if (i1)
1585 goto Lt1;
1587 else if (t1->ty == Tclass && t2->ty == Tclass)
1588 { TypeClass *tc1 = (TypeClass *)t1;
1589 TypeClass *tc2 = (TypeClass *)t2;
1591 /* Pick 'tightest' type
1593 ClassDeclaration *cd1 = tc1->sym->baseClass;
1594 ClassDeclaration *cd2 = tc1->sym->baseClass;
1596 if (cd1 && cd2)
1597 { t1 = cd1->type;
1598 t2 = cd2->type;
1600 else if (cd1)
1601 t1 = cd1->type;
1602 else if (cd2)
1603 t2 = cd2->type;
1604 else
1605 goto Lincompatible;
1607 else
1608 goto Lincompatible;
1611 else if (t1->ty == Tstruct && t2->ty == Tstruct)
1613 if (((TypeStruct *)t1)->sym != ((TypeStruct *)t2)->sym)
1614 goto Lincompatible;
1616 else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2))
1618 goto Lt2;
1620 else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1))
1622 goto Lt1;
1624 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
1625 e2->implicitConvTo(t1->nextOf()->arrayOf()))
1627 Lx1:
1628 t = t1->nextOf()->arrayOf();
1629 e1 = e1->castTo(sc, t);
1630 e2 = e2->castTo(sc, t);
1632 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
1633 e1->implicitConvTo(t2->nextOf()->arrayOf()))
1635 Lx2:
1636 t = t2->nextOf()->arrayOf();
1637 e1 = e1->castTo(sc, t);
1638 e2 = e2->castTo(sc, t);
1640 else if (t1->isintegral() && t2->isintegral())
1642 assert(0);
1644 else
1646 Lincompatible:
1647 incompatibleTypes();
1649 Lret:
1650 if (!type)
1651 type = t;
1652 #if 0
1653 printf("-BinExp::typeCombine() %s\n", toChars());
1654 if (e1->type) printf("\tt1 = %s\n", e1->type->toChars());
1655 if (e2->type) printf("\tt2 = %s\n", e2->type->toChars());
1656 printf("\ttype = %s\n", type->toChars());
1657 #endif
1658 //dump(0);
1659 return this;
1662 Lt1:
1663 e2 = e2->castTo(sc, t1);
1664 t = t1;
1665 goto Lret;
1667 Lt2:
1668 e1 = e1->castTo(sc, t2);
1669 t = t2;
1670 goto Lret;
1673 /***********************************
1674 * Do integral promotions (convertchk).
1675 * Don't convert <array of> to <pointer to>
1678 Expression *Expression::integralPromotions(Scope *sc)
1680 Expression *e = this;
1682 //printf("integralPromotions %s %s\n", e->toChars(), e->type->toChars());
1683 switch (type->toBasetype()->ty)
1685 case Tvoid:
1686 error("void has no value");
1687 break;
1689 case Tint8:
1690 case Tuns8:
1691 case Tint16:
1692 case Tuns16:
1693 case Tbit:
1694 case Tbool:
1695 case Tchar:
1696 case Twchar:
1697 e = e->castTo(sc, Type::tint32);
1698 break;
1700 case Tdchar:
1701 e = e->castTo(sc, Type::tuns32);
1702 break;
1704 return e;