Fixed some lexing problems with DOS line-endings
[delight/core.git] / dmd2 / cast.c
blob95d88fe8b0de04009bcffb2609e148d5ec95ada5
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
372 if (this->type->equals(t))
373 return MATCHexact;
375 /* Allow implicit conversions from invariant to mutable|const,
376 * and mutable to invariant. It works because, after all, a null
377 * doesn't actually point to anything.
379 if (t->invariantOf()->equals(type->invariantOf()))
380 return MATCHconst;
382 // NULL implicitly converts to any pointer type or dynamic array
383 if (type->ty == Tpointer && type->nextOf()->ty == Tvoid)
385 if (t->ty == Ttypedef)
386 t = ((TypeTypedef *)t)->sym->basetype;
387 if (t->ty == Tpointer || t->ty == Tarray ||
388 t->ty == Taarray || t->ty == Tclass ||
389 t->ty == Tdelegate)
390 return committed ? MATCHconvert : MATCHexact;
392 return Expression::implicitConvTo(t);
395 MATCH StructLiteralExp::implicitConvTo(Type *t)
397 #if 0
398 printf("StructLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
399 toChars(), type->toChars(), t->toChars());
400 #endif
401 MATCH m = Expression::implicitConvTo(t);
402 if (m != MATCHnomatch)
403 return m;
404 if (type->ty == t->ty && type->ty == Tstruct &&
405 ((TypeStruct *)type)->sym == ((TypeStruct *)t)->sym)
407 m = MATCHconst;
408 for (int i = 0; i < elements->dim; i++)
409 { Expression *e = (Expression *)elements->data[i];
410 Type *te = e->type;
411 if (t->mod == 0)
412 te = te->mutableOf();
413 else
414 { assert(t->mod == MODinvariant);
415 te = te->invariantOf();
417 MATCH m2 = e->implicitConvTo(te);
418 //printf("\t%s => %s, match = %d\n", e->toChars(), te->toChars(), m2);
419 if (m2 < m)
420 m = m2;
423 return m;
426 MATCH StringExp::implicitConvTo(Type *t)
427 { MATCH m;
429 #if 0
430 printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
431 toChars(), committed, type->toChars(), t->toChars());
432 #endif
433 if (!committed)
435 if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
437 return MATCHnomatch;
439 if (type->ty == Tsarray || type->ty == Tarray || type->ty == Tpointer)
441 TY tyn = type->nextOf()->ty;
442 if (tyn == Tchar || tyn == Twchar || tyn == Tdchar)
443 { Type *tn;
444 MATCH m;
446 switch (t->ty)
448 case Tsarray:
449 if (type->ty == Tsarray)
451 if (((TypeSArray *)type)->dim->toInteger() !=
452 ((TypeSArray *)t)->dim->toInteger())
453 return MATCHnomatch;
454 TY tynto = t->nextOf()->ty;
455 if (tynto == Tchar || tynto == Twchar || tynto == Tdchar)
456 return MATCHexact;
458 case Tarray:
459 case Tpointer:
460 tn = t->nextOf();
461 m = MATCHexact;
462 if (type->nextOf()->mod != tn->mod)
463 { if (!tn->isConst())
464 return MATCHnomatch;
465 m = MATCHconst;
467 switch (tn->ty)
469 case Tchar:
470 case Twchar:
471 case Tdchar:
472 return m;
474 break;
479 return Expression::implicitConvTo(t);
480 #if 0
481 m = (MATCH)type->implicitConvTo(t);
482 if (m)
484 return m;
487 return MATCHnomatch;
488 #endif
491 MATCH ArrayLiteralExp::implicitConvTo(Type *t)
492 { MATCH result = MATCHexact;
494 #if 0
495 printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
496 toChars(), type->toChars(), t->toChars());
497 #endif
498 Type *typeb = type->toBasetype();
499 Type *tb = t->toBasetype();
500 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
501 (typeb->ty == Tarray || typeb->ty == Tsarray))
503 if (tb->ty == Tsarray)
504 { TypeSArray *tsa = (TypeSArray *)tb;
505 if (elements->dim != tsa->dim->toInteger())
506 result = MATCHnomatch;
509 for (int i = 0; i < elements->dim; i++)
510 { Expression *e = (Expression *)elements->data[i];
511 MATCH m = (MATCH)e->implicitConvTo(tb->nextOf());
512 if (m < result)
513 result = m; // remember worst match
514 if (result == MATCHnomatch)
515 break; // no need to check for worse
517 return result;
519 else
520 return Expression::implicitConvTo(t);
523 MATCH AssocArrayLiteralExp::implicitConvTo(Type *t)
524 { MATCH result = MATCHexact;
526 Type *typeb = type->toBasetype();
527 Type *tb = t->toBasetype();
528 if (tb->ty == Taarray && typeb->ty == Taarray)
530 for (size_t i = 0; i < keys->dim; i++)
531 { Expression *e = (Expression *)keys->data[i];
532 MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->index);
533 if (m < result)
534 result = m; // remember worst match
535 if (result == MATCHnomatch)
536 break; // no need to check for worse
537 e = (Expression *)values->data[i];
538 m = (MATCH)e->implicitConvTo(tb->nextOf());
539 if (m < result)
540 result = m; // remember worst match
541 if (result == MATCHnomatch)
542 break; // no need to check for worse
544 return result;
546 else
547 return Expression::implicitConvTo(t);
550 MATCH AddrExp::implicitConvTo(Type *t)
552 #if 0
553 printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
554 toChars(), type->toChars(), t->toChars());
555 #endif
556 MATCH result;
558 result = type->implicitConvTo(t);
559 //printf("\tresult = %d\n", result);
561 if (result == MATCHnomatch)
563 // Look for pointers to functions where the functions are overloaded.
565 t = t->toBasetype();
567 if (e1->op == TOKoverloadset &&
568 (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction)
569 { OverExp *eo = (OverExp *)e1;
570 FuncDeclaration *f = NULL;
571 for (int i = 0; i < eo->vars->a.dim; i++)
572 { Dsymbol *s = (Dsymbol *)eo->vars->a.data[i];
573 FuncDeclaration *f2 = s->isFuncDeclaration();
574 assert(f2);
575 if (f2->overloadExactMatch(t->nextOf()))
576 { if (f)
577 /* Error if match in more than one overload set,
578 * even if one is a 'better' match than the other.
580 ScopeDsymbol::multiplyDefined(loc, f, f2);
581 else
582 f = f2;
583 result = MATCHexact;
588 if (type->ty == Tpointer && type->nextOf()->ty == Tfunction &&
589 t->ty == Tpointer && t->nextOf()->ty == Tfunction &&
590 e1->op == TOKvar)
592 /* I don't think this can ever happen -
593 * it should have been
594 * converted to a SymOffExp.
596 assert(0);
597 VarExp *ve = (VarExp *)e1;
598 FuncDeclaration *f = ve->var->isFuncDeclaration();
599 if (f && f->overloadExactMatch(t->nextOf()))
600 result = MATCHexact;
603 //printf("\tresult = %d\n", result);
604 return result;
607 MATCH SymOffExp::implicitConvTo(Type *t)
609 #if 0
610 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
611 toChars(), type->toChars(), t->toChars());
612 #endif
613 MATCH result;
615 result = type->implicitConvTo(t);
616 //printf("\tresult = %d\n", result);
618 if (result == MATCHnomatch)
620 // Look for pointers to functions where the functions are overloaded.
621 FuncDeclaration *f;
623 t = t->toBasetype();
624 if (type->ty == Tpointer && type->nextOf()->ty == Tfunction &&
625 (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction)
627 f = var->isFuncDeclaration();
628 if (f)
629 { f = f->overloadExactMatch(t->nextOf());
630 if (f)
631 { if ((t->ty == Tdelegate && (f->needThis() || f->isNested())) ||
632 (t->ty == Tpointer && !(f->needThis() || f->isNested())))
634 result = MATCHexact;
640 //printf("\tresult = %d\n", result);
641 return result;
644 MATCH DelegateExp::implicitConvTo(Type *t)
646 #if 0
647 printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
648 toChars(), type->toChars(), t->toChars());
649 #endif
650 MATCH result;
652 result = type->implicitConvTo(t);
654 if (result == MATCHnomatch)
656 // Look for pointers to functions where the functions are overloaded.
657 FuncDeclaration *f;
659 t = t->toBasetype();
660 if (type->ty == Tdelegate && type->nextOf()->ty == Tfunction &&
661 t->ty == Tdelegate && t->nextOf()->ty == Tfunction)
663 if (func && func->overloadExactMatch(t->nextOf()))
664 result = MATCHexact;
667 return result;
670 MATCH CondExp::implicitConvTo(Type *t)
672 MATCH m1;
673 MATCH m2;
675 m1 = e1->implicitConvTo(t);
676 m2 = e2->implicitConvTo(t);
678 // Pick the worst match
679 return (m1 < m2) ? m1 : m2;
683 /* ==================== castTo ====================== */
685 /**************************************
686 * Do an explicit cast.
689 Expression *Expression::castTo(Scope *sc, Type *t)
691 //printf("Expression::castTo(this=%s, t=%s)\n", toChars(), t->toChars());
692 #if 0
693 printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
694 toChars(), type->toChars(), t->toChars());
695 #endif
696 if (type == t)
697 return this;
698 Expression *e = this;
699 Type *tb = t->toBasetype();
700 Type *typeb = type->toBasetype();
701 if (tb != typeb)
703 // Do (type *) cast of (type [dim])
704 if (tb->ty == Tpointer &&
705 typeb->ty == Tsarray
708 //printf("Converting [dim] to *\n");
710 if (typeb->size(loc) == 0)
711 e = new NullExp(loc);
712 else
713 e = new AddrExp(loc, e);
715 #if 0
716 else if (tb->ty == Tdelegate && type->ty != Tdelegate)
718 TypeDelegate *td = (TypeDelegate *)tb;
719 TypeFunction *tf = (TypeFunction *)td->nextOf();
720 return toDelegate(sc, tf->nextOf());
722 #endif
723 else
725 e = new CastExp(loc, e, tb);
728 else
730 e = e->copy(); // because of COW for assignment to e->type
732 assert(e != this);
733 e->type = t;
734 //printf("Returning: %s\n", e->toChars());
735 return e;
739 Expression *RealExp::castTo(Scope *sc, Type *t)
740 { Expression *e = this;
741 if (type != t)
743 if ((type->isreal() && t->isreal()) ||
744 (type->isimaginary() && t->isimaginary())
746 { e = copy();
747 e->type = t;
749 else
750 e = Expression::castTo(sc, t);
752 return e;
756 Expression *ComplexExp::castTo(Scope *sc, Type *t)
757 { Expression *e = this;
758 if (type != t)
760 if (type->iscomplex() && t->iscomplex())
761 { e = copy();
762 e->type = t;
764 else
765 e = Expression::castTo(sc, t);
767 return e;
771 Expression *NullExp::castTo(Scope *sc, Type *t)
772 { NullExp *e;
773 Type *tb;
775 //printf("NullExp::castTo(t = %p)\n", t);
776 if (type == t)
778 committed = 1;
779 return this;
781 e = (NullExp *)copy();
782 e->committed = 1;
783 tb = t->toBasetype();
784 e->type = type->toBasetype();
785 if (tb != e->type)
787 // NULL implicitly converts to any pointer type or dynamic array
788 if (e->type->ty == Tpointer && e->type->nextOf()->ty == Tvoid &&
789 (tb->ty == Tpointer || tb->ty == Tarray || tb->ty == Taarray ||
790 tb->ty == Tdelegate))
792 #if 0
793 if (tb->ty == Tdelegate)
794 { TypeDelegate *td = (TypeDelegate *)tb;
795 TypeFunction *tf = (TypeFunction *)td->nextOf();
797 if (!tf->varargs &&
798 !(tf->arguments && tf->arguments->dim)
801 return Expression::castTo(sc, t);
804 #endif
806 else
808 return e->Expression::castTo(sc, t);
811 e->type = t;
812 return e;
815 Expression *StringExp::castTo(Scope *sc, Type *t)
817 /* This follows copy-on-write; any changes to 'this'
818 * will result in a copy.
819 * The this->string member is considered immutable.
821 StringExp *se;
822 Type *tb;
823 int copied = 0;
825 //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed);
827 if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
829 error("cannot convert string literal to void*");
832 se = this;
833 if (!committed)
834 { se = (StringExp *)copy();
835 se->committed = 1;
836 copied = 1;
839 if (type == t)
841 return se;
844 tb = t->toBasetype();
845 //printf("\ttype = %s\n", type->toChars());
846 if (tb->ty == Tdelegate && type->toBasetype()->ty != Tdelegate)
847 return Expression::castTo(sc, t);
849 Type *typeb = type->toBasetype();
850 if (typeb == tb)
852 if (!copied)
853 { se = (StringExp *)copy();
854 copied = 1;
856 se->type = t;
857 return se;
860 if (tb->ty != Tsarray && tb->ty != Tarray && tb->ty != Tpointer)
861 { if (!copied)
862 { se = (StringExp *)copy();
863 copied = 1;
865 goto Lcast;
867 if (typeb->ty != Tsarray && typeb->ty != Tarray && typeb->ty != Tpointer)
868 { if (!copied)
869 { se = (StringExp *)copy();
870 copied = 1;
872 goto Lcast;
875 if (typeb->nextOf()->size() == tb->nextOf()->size())
877 if (!copied)
878 { se = (StringExp *)copy();
879 copied = 1;
881 if (tb->ty == Tsarray)
882 goto L2; // handle possible change in static array dimension
883 se->type = t;
884 return se;
887 if (committed)
888 goto Lcast;
890 #define X(tf,tt) ((tf) * 256 + (tt))
892 OutBuffer buffer;
893 size_t newlen = 0;
894 int tfty = typeb->nextOf()->toBasetype()->ty;
895 int ttty = tb->nextOf()->toBasetype()->ty;
896 switch (X(tfty, ttty))
898 case X(Tchar, Tchar):
899 case X(Twchar,Twchar):
900 case X(Tdchar,Tdchar):
901 break;
903 case X(Tchar, Twchar):
904 for (size_t u = 0; u < len;)
905 { unsigned c;
906 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
907 if (p)
908 error("%s", p);
909 else
910 buffer.writeUTF16(c);
912 newlen = buffer.offset / 2;
913 buffer.writeUTF16(0);
914 goto L1;
916 case X(Tchar, Tdchar):
917 for (size_t u = 0; u < len;)
918 { unsigned c;
919 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
920 if (p)
921 error("%s", p);
922 buffer.write4(c);
923 newlen++;
925 buffer.write4(0);
926 goto L1;
928 case X(Twchar,Tchar):
929 for (size_t u = 0; u < len;)
930 { unsigned c;
931 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
932 if (p)
933 error("%s", p);
934 else
935 buffer.writeUTF8(c);
937 newlen = buffer.offset;
938 buffer.writeUTF8(0);
939 goto L1;
941 case X(Twchar,Tdchar):
942 for (size_t u = 0; u < len;)
943 { unsigned c;
944 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
945 if (p)
946 error("%s", p);
947 buffer.write4(c);
948 newlen++;
950 buffer.write4(0);
951 goto L1;
953 case X(Tdchar,Tchar):
954 for (size_t u = 0; u < len; u++)
956 unsigned c = ((unsigned *)se->string)[u];
957 if (!utf_isValidDchar(c))
958 error("invalid UCS-32 char \\U%08x", c);
959 else
960 buffer.writeUTF8(c);
961 newlen++;
963 newlen = buffer.offset;
964 buffer.writeUTF8(0);
965 goto L1;
967 case X(Tdchar,Twchar):
968 for (size_t u = 0; u < len; u++)
970 unsigned c = ((unsigned *)se->string)[u];
971 if (!utf_isValidDchar(c))
972 error("invalid UCS-32 char \\U%08x", c);
973 else
974 buffer.writeUTF16(c);
975 newlen++;
977 newlen = buffer.offset / 2;
978 buffer.writeUTF16(0);
979 goto L1;
982 if (!copied)
983 { se = (StringExp *)copy();
984 copied = 1;
986 se->string = buffer.extractData();
987 se->len = newlen;
988 se->sz = tb->nextOf()->size();
989 break;
991 default:
992 assert(typeb->nextOf()->size() != tb->nextOf()->size());
993 goto Lcast;
996 #undef X
998 assert(copied);
1000 // See if need to truncate or extend the literal
1001 if (tb->ty == Tsarray)
1003 int dim2 = ((TypeSArray *)tb)->dim->toInteger();
1005 //printf("dim from = %d, to = %d\n", se->len, dim2);
1007 // Changing dimensions
1008 if (dim2 != se->len)
1010 // Copy when changing the string literal
1011 unsigned newsz = se->sz;
1012 void *s;
1013 int d;
1015 d = (dim2 < se->len) ? dim2 : se->len;
1016 s = (unsigned char *)mem.malloc((dim2 + 1) * newsz);
1017 memcpy(s, se->string, d * newsz);
1018 // Extend with 0, add terminating 0
1019 memset((char *)s + d * newsz, 0, (dim2 + 1 - d) * newsz);
1020 se->string = s;
1021 se->len = dim2;
1024 se->type = t;
1025 return se;
1027 Lcast:
1028 Expression *e = new CastExp(loc, se, t);
1029 e->type = t; // so semantic() won't be run on e
1030 return e;
1033 Expression *AddrExp::castTo(Scope *sc, Type *t)
1035 Type *tb;
1037 #if 0
1038 printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
1039 toChars(), type->toChars(), t->toChars());
1040 #endif
1041 Expression *e = this;
1043 tb = t->toBasetype();
1044 type = type->toBasetype();
1045 if (tb != type)
1047 // Look for pointers to functions where the functions are overloaded.
1049 if (e1->op == TOKoverloadset &&
1050 (t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction)
1051 { OverExp *eo = (OverExp *)e1;
1052 FuncDeclaration *f = NULL;
1053 for (int i = 0; i < eo->vars->a.dim; i++)
1054 { Dsymbol *s = (Dsymbol *)eo->vars->a.data[i];
1055 FuncDeclaration *f2 = s->isFuncDeclaration();
1056 assert(f2);
1057 if (f2->overloadExactMatch(t->nextOf()))
1058 { if (f)
1059 /* Error if match in more than one overload set,
1060 * even if one is a 'better' match than the other.
1062 ScopeDsymbol::multiplyDefined(loc, f, f2);
1063 else
1064 f = f2;
1067 if (f)
1068 { f->tookAddressOf = 1;
1069 SymOffExp *se = new SymOffExp(loc, f, 0, 0);
1070 se->semantic(sc);
1071 // Let SymOffExp::castTo() do the heavy lifting
1072 return se->castTo(sc, t);
1077 if (type->ty == Tpointer && type->nextOf()->ty == Tfunction &&
1078 tb->ty == Tpointer && tb->nextOf()->ty == Tfunction &&
1079 e1->op == TOKvar)
1081 VarExp *ve = (VarExp *)e1;
1082 FuncDeclaration *f = ve->var->isFuncDeclaration();
1083 if (f)
1085 assert(0); // should be SymOffExp instead
1086 f = f->overloadExactMatch(tb->nextOf());
1087 if (f)
1089 e = new VarExp(loc, f);
1090 e->type = f->type;
1091 e = new AddrExp(loc, e);
1092 e->type = t;
1093 return e;
1097 e = Expression::castTo(sc, t);
1099 e->type = t;
1100 return e;
1104 Expression *TupleExp::castTo(Scope *sc, Type *t)
1105 { TupleExp *e = (TupleExp *)copy();
1106 e->exps = (Expressions *)exps->copy();
1107 for (size_t i = 0; i < e->exps->dim; i++)
1108 { Expression *ex = (Expression *)e->exps->data[i];
1109 ex = ex->castTo(sc, t);
1110 e->exps->data[i] = (void *)ex;
1112 return e;
1116 Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t)
1118 #if 0
1119 printf("ArrayLiteralExp::castTo(this=%s, type=%s, => %s)\n",
1120 toChars(), type->toChars(), t->toChars());
1121 #endif
1122 if (type == t)
1123 return this;
1124 ArrayLiteralExp *e = this;
1125 Type *typeb = type->toBasetype();
1126 Type *tb = t->toBasetype();
1127 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
1128 (typeb->ty == Tarray || typeb->ty == Tsarray) &&
1129 // Not trying to convert non-void[] to void[]
1130 !(tb->nextOf()->toBasetype()->ty == Tvoid && typeb->nextOf()->toBasetype()->ty != Tvoid))
1132 if (tb->ty == Tsarray)
1133 { TypeSArray *tsa = (TypeSArray *)tb;
1134 if (elements->dim != tsa->dim->toInteger())
1135 goto L1;
1138 e = (ArrayLiteralExp *)copy();
1139 e->elements = (Expressions *)elements->copy();
1140 for (int i = 0; i < elements->dim; i++)
1141 { Expression *ex = (Expression *)elements->data[i];
1142 ex = ex->castTo(sc, tb->nextOf());
1143 e->elements->data[i] = (void *)ex;
1145 e->type = t;
1146 return e;
1148 if (tb->ty == Tpointer && typeb->ty == Tsarray)
1150 e = (ArrayLiteralExp *)copy();
1151 e->type = typeb->nextOf()->pointerTo();
1154 return e->Expression::castTo(sc, t);
1157 Expression *AssocArrayLiteralExp::castTo(Scope *sc, Type *t)
1159 if (type == t)
1160 return this;
1161 AssocArrayLiteralExp *e = this;
1162 Type *typeb = type->toBasetype();
1163 Type *tb = t->toBasetype();
1164 if (tb->ty == Taarray && typeb->ty == Taarray &&
1165 tb->nextOf()->toBasetype()->ty != Tvoid)
1167 e = (AssocArrayLiteralExp *)copy();
1168 e->keys = (Expressions *)keys->copy();
1169 e->values = (Expressions *)values->copy();
1170 assert(keys->dim == values->dim);
1171 for (size_t i = 0; i < keys->dim; i++)
1172 { Expression *ex = (Expression *)values->data[i];
1173 ex = ex->castTo(sc, tb->nextOf());
1174 e->values->data[i] = (void *)ex;
1176 ex = (Expression *)keys->data[i];
1177 ex = ex->castTo(sc, ((TypeAArray *)tb)->index);
1178 e->keys->data[i] = (void *)ex;
1180 e->type = t;
1181 return e;
1184 return e->Expression::castTo(sc, t);
1187 Expression *SymOffExp::castTo(Scope *sc, Type *t)
1189 #if 0
1190 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
1191 toChars(), type->toChars(), t->toChars());
1192 #endif
1193 if (type == t && hasOverloads == 0)
1194 return this;
1195 Expression *e;
1196 Type *tb = t->toBasetype();
1197 Type *typeb = type->toBasetype();
1198 if (tb != typeb)
1200 // Look for pointers to functions where the functions are overloaded.
1201 FuncDeclaration *f;
1203 if (hasOverloads &&
1204 typeb->ty == Tpointer && typeb->nextOf()->ty == Tfunction &&
1205 (tb->ty == Tpointer || tb->ty == Tdelegate) && tb->nextOf()->ty == Tfunction)
1207 f = var->isFuncDeclaration();
1208 if (f)
1210 f = f->overloadExactMatch(tb->nextOf());
1211 if (f)
1213 if (tb->ty == Tdelegate && f->needThis() && hasThis(sc))
1215 e = new DelegateExp(loc, new ThisExp(loc), f);
1216 e = e->semantic(sc);
1218 else if (tb->ty == Tdelegate && f->isNested())
1220 e = new DelegateExp(loc, new IntegerExp(0), f);
1221 e = e->semantic(sc);
1223 else
1225 e = new SymOffExp(loc, f, 0);
1226 e->type = t;
1228 f->tookAddressOf = 1;
1229 return e;
1233 e = Expression::castTo(sc, t);
1235 else
1236 { e = copy();
1237 e->type = t;
1238 ((SymOffExp *)e)->hasOverloads = 0;
1240 return e;
1243 Expression *DelegateExp::castTo(Scope *sc, Type *t)
1245 #if 0
1246 printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
1247 toChars(), type->toChars(), t->toChars());
1248 #endif
1249 static char msg[] = "cannot form delegate due to covariant return type";
1251 Expression *e = this;
1252 Type *tb = t->toBasetype();
1253 Type *typeb = type->toBasetype();
1254 if (tb != typeb)
1256 // Look for delegates to functions where the functions are overloaded.
1257 FuncDeclaration *f;
1259 if (typeb->ty == Tdelegate && typeb->nextOf()->ty == Tfunction &&
1260 tb->ty == Tdelegate && tb->nextOf()->ty == Tfunction)
1262 if (func)
1264 f = func->overloadExactMatch(tb->nextOf());
1265 if (f)
1266 { target_ptrdiff_t offset;
1267 if (f->tintro && f->tintro->nextOf()->isBaseOf(f->type->nextOf(), &offset) && offset)
1268 error("%s", msg);
1269 f->tookAddressOf = 1;
1270 e = new DelegateExp(loc, e1, f);
1271 e->type = t;
1272 return e;
1274 if (func->tintro)
1275 error("%s", msg);
1278 e = Expression::castTo(sc, t);
1280 else
1281 { target_ptrdiff_t offset;
1283 func->tookAddressOf = 1;
1284 if (func->tintro && func->tintro->nextOf()->isBaseOf(func->type->nextOf(), &offset) && offset)
1285 error("%s", msg);
1286 e = copy();
1287 e->type = t;
1289 return e;
1292 Expression *CondExp::castTo(Scope *sc, Type *t)
1294 Expression *e = this;
1296 if (type != t)
1298 if (1 || e1->op == TOKstring || e2->op == TOKstring)
1299 { e = new CondExp(loc, econd, e1->castTo(sc, t), e2->castTo(sc, t));
1300 e->type = t;
1302 else
1303 e = Expression::castTo(sc, t);
1305 return e;
1308 /* ==================== ====================== */
1310 /****************************************
1311 * Scale addition/subtraction to/from pointer.
1314 Expression *BinExp::scaleFactor(Scope *sc)
1315 { d_uns64 stride;
1316 Type *t1b = e1->type->toBasetype();
1317 Type *t2b = e2->type->toBasetype();
1319 if (t1b->ty == Tpointer && t2b->isintegral())
1320 { // Need to adjust operator by the stride
1321 // Replace (ptr + int) with (ptr + (int * stride))
1322 Type *t = Type::tptrdiff_t;
1324 stride = t1b->nextOf()->size(loc);
1325 if (!t->equals(t2b))
1326 e2 = e2->castTo(sc, t);
1327 e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t));
1328 e2->type = t;
1329 type = e1->type;
1331 else if (t2b->ty && t1b->isintegral())
1332 { // Need to adjust operator by the stride
1333 // Replace (int + ptr) with (ptr + (int * stride))
1334 Type *t = Type::tptrdiff_t;
1335 Expression *e;
1337 stride = t2b->nextOf()->size(loc);
1338 if (!t->equals(t1b))
1339 e = e1->castTo(sc, t);
1340 else
1341 e = e1;
1342 e = new MulExp(loc, e, new IntegerExp(0, stride, t));
1343 e->type = t;
1344 type = e2->type;
1345 e1 = e2;
1346 e2 = e;
1348 return this;
1351 /************************************
1352 * Bring leaves to common type.
1355 Expression *BinExp::typeCombine(Scope *sc)
1357 Type *t1;
1358 Type *t2;
1359 Type *t;
1360 TY ty;
1362 //printf("BinExp::typeCombine() %s\n", toChars());
1363 //dump(0);
1365 e1 = e1->integralPromotions(sc);
1366 e2 = e2->integralPromotions(sc);
1368 // BUG: do toBasetype()
1369 t1 = e1->type;
1370 t2 = e2->type;
1371 assert(t1);
1372 t = t1;
1374 //if (t1) printf("\tt1 = %s\n", t1->toChars());
1375 //if (t2) printf("\tt2 = %s\n", t2->toChars());
1376 #ifdef DEBUG
1377 if (!t2) printf("\te2 = '%s'\n", e2->toChars());
1378 #endif
1379 assert(t2);
1381 Type *t1b = t1->toBasetype();
1382 Type *t2b = t2->toBasetype();
1384 ty = (TY)Type::impcnvResult[t1b->ty][t2b->ty];
1385 if (ty != Terror)
1386 { TY ty1;
1387 TY ty2;
1389 ty1 = (TY)Type::impcnvType1[t1b->ty][t2b->ty];
1390 ty2 = (TY)Type::impcnvType2[t1b->ty][t2b->ty];
1392 if (t1b->ty == ty1) // if no promotions
1394 if (t1 == t2)
1396 if (!type)
1397 type = t1;
1398 return this;
1401 if (t1b == t2b)
1403 if (!type)
1404 type = t1b;
1405 return this;
1409 if (!type)
1410 type = Type::basic[ty];
1412 t1 = Type::basic[ty1];
1413 t2 = Type::basic[ty2];
1414 e1 = e1->castTo(sc, t1);
1415 e2 = e2->castTo(sc, t2);
1416 #if 0
1417 if (type != Type::basic[ty])
1418 { t = type;
1419 type = Type::basic[ty];
1420 return castTo(sc, t);
1422 #endif
1423 //printf("after typeCombine():\n");
1424 //dump(0);
1425 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
1426 return this;
1429 t1 = t1b;
1430 t2 = t2b;
1432 if (op == TOKcat)
1434 if ((t1->ty == Tsarray || t1->ty == Tarray) &&
1435 (t2->ty == Tsarray || t2->ty == Tarray) &&
1436 (t1->nextOf()->mod || t2->nextOf()->mod) &&
1437 (t1->nextOf()->mod != t2->nextOf()->mod)
1440 t1 = t1->nextOf()->mutableOf()->constOf()->arrayOf();
1441 t2 = t2->nextOf()->mutableOf()->constOf()->arrayOf();
1442 //t1 = t1->constOf();
1443 //t2 = t2->constOf();
1444 if (e1->op == TOKstring && !((StringExp *)e1)->committed)
1445 e1->type = t1;
1446 else
1447 e1 = e1->castTo(sc, t1);
1448 if (e2->op == TOKstring && !((StringExp *)e2)->committed)
1449 e2->type = t2;
1450 else
1451 e2 = e2->castTo(sc, t2);
1452 t = t1;
1453 goto Lagain;
1457 Lagain:
1458 if (t1 == t2)
1460 if ((t1->ty == Tstruct || t1->ty == Tclass) &&
1461 (op == TOKmin || op == TOKadd))
1462 goto Lincompatible;
1464 else if (t1->ty == Tpointer && t2->ty == Tpointer)
1466 // Bring pointers to compatible type
1467 Type *t1n = t1->nextOf();
1468 Type *t2n = t2->nextOf();
1470 if (t1n == t2n)
1472 else if (t1n->ty == Tvoid) // pointers to void are always compatible
1473 t = t2;
1474 else if (t2n->ty == Tvoid)
1476 else if (t1n->mod != t2n->mod)
1478 t1 = t1n->mutableOf()->constOf()->pointerTo();
1479 t2 = t2n->mutableOf()->constOf()->pointerTo();
1480 t = t1;
1481 goto Lagain;
1483 else if (t1n->ty == Tclass && t2n->ty == Tclass)
1484 { ClassDeclaration *cd1 = t1n->isClassHandle();
1485 ClassDeclaration *cd2 = t2n->isClassHandle();
1486 target_ptrdiff_t offset;
1488 if (cd1->isBaseOf(cd2, &offset))
1490 if (offset)
1491 e2 = e2->castTo(sc, t);
1493 else if (cd2->isBaseOf(cd1, &offset))
1495 t = t2;
1496 if (offset)
1497 e1 = e1->castTo(sc, t);
1499 else
1500 goto Lincompatible;
1502 else
1503 goto Lincompatible;
1505 else if ((t1->ty == Tsarray || t1->ty == Tarray) &&
1506 e2->op == TOKnull && t2->ty == Tpointer && t2->nextOf()->ty == Tvoid)
1507 { /* (T[n] op void*)
1508 * (T[] op void*)
1510 goto Lx1;
1512 else if ((t2->ty == Tsarray || t2->ty == Tarray) &&
1513 e1->op == TOKnull && t1->ty == Tpointer && t1->nextOf()->ty == Tvoid)
1514 { /* (void* op T[n])
1515 * (void* op T[])
1517 goto Lx2;
1519 else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2))
1521 goto Lt2;
1523 else if ((t2->ty == Tsarray || t2->ty == Tarray) && t2->implicitConvTo(t1))
1525 goto Lt1;
1527 /* If one is mutable and the other invariant, then retry
1528 * with both of them as const
1530 else if ((t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Tpointer) &&
1531 (t2->ty == Tsarray || t2->ty == Tarray || t2->ty == Tpointer) &&
1532 t1->nextOf()->mod != t2->nextOf()->mod
1535 if (t1->ty == Tpointer)
1536 t1 = t1->nextOf()->mutableOf()->constOf()->pointerTo();
1537 else
1538 t1 = t1->nextOf()->mutableOf()->constOf()->arrayOf();
1540 if (t2->ty == Tpointer)
1541 t2 = t2->nextOf()->mutableOf()->constOf()->pointerTo();
1542 else
1543 t2 = t2->nextOf()->mutableOf()->constOf()->arrayOf();
1544 t = t1;
1545 goto Lagain;
1547 else if (t1->ty == Tclass || t2->ty == Tclass)
1549 while (1)
1551 int i1 = e2->implicitConvTo(t1);
1552 int i2 = e1->implicitConvTo(t2);
1554 if (i1 && i2)
1556 // We have the case of class vs. void*, so pick class
1557 if (t1->ty == Tpointer)
1558 i1 = 0;
1559 else if (t2->ty == Tpointer)
1560 i2 = 0;
1563 if (i2)
1565 goto Lt2;
1567 else if (i1)
1569 goto Lt1;
1571 else if (t1->ty == Tclass && t2->ty == Tclass)
1572 { TypeClass *tc1 = (TypeClass *)t1;
1573 TypeClass *tc2 = (TypeClass *)t2;
1575 /* Pick 'tightest' type
1577 ClassDeclaration *cd1 = tc1->sym->baseClass;
1578 ClassDeclaration *cd2 = tc1->sym->baseClass;
1580 if (cd1 && cd2)
1581 { t1 = cd1->type;
1582 t2 = cd2->type;
1584 else if (cd1)
1585 t1 = cd1->type;
1586 else if (cd2)
1587 t2 = cd2->type;
1588 else
1589 goto Lincompatible;
1591 else
1592 goto Lincompatible;
1595 else if (t1->ty == Tstruct && t2->ty == Tstruct)
1597 if (((TypeStruct *)t1)->sym != ((TypeStruct *)t2)->sym)
1598 goto Lincompatible;
1600 else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2))
1602 goto Lt2;
1604 else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1))
1606 goto Lt1;
1608 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
1609 e2->implicitConvTo(t1->nextOf()->arrayOf()))
1611 Lx1:
1612 t = t1->nextOf()->arrayOf();
1613 e1 = e1->castTo(sc, t);
1614 e2 = e2->castTo(sc, t);
1616 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
1617 e1->implicitConvTo(t2->nextOf()->arrayOf()))
1619 Lx2:
1620 t = t2->nextOf()->arrayOf();
1621 e1 = e1->castTo(sc, t);
1622 e2 = e2->castTo(sc, t);
1624 else if (t1->isintegral() && t2->isintegral())
1626 assert(0);
1628 else
1630 Lincompatible:
1631 incompatibleTypes();
1633 Lret:
1634 if (!type)
1635 type = t;
1636 #if 0
1637 printf("-BinExp::typeCombine() %s\n", toChars());
1638 if (e1->type) printf("\tt1 = %s\n", e1->type->toChars());
1639 if (e2->type) printf("\tt2 = %s\n", e2->type->toChars());
1640 printf("\ttype = %s\n", type->toChars());
1641 #endif
1642 //dump(0);
1643 return this;
1646 Lt1:
1647 e2 = e2->castTo(sc, t1);
1648 t = t1;
1649 goto Lret;
1651 Lt2:
1652 e1 = e1->castTo(sc, t2);
1653 t = t2;
1654 goto Lret;
1657 /***********************************
1658 * Do integral promotions (convertchk).
1659 * Don't convert <array of> to <pointer to>
1662 Expression *Expression::integralPromotions(Scope *sc)
1664 Expression *e = this;
1666 //printf("integralPromotions %s %s\n", e->toChars(), e->type->toChars());
1667 switch (type->toBasetype()->ty)
1669 case Tvoid:
1670 error("void has no value");
1671 break;
1673 case Tint8:
1674 case Tuns8:
1675 case Tint16:
1676 case Tuns16:
1677 case Tbit:
1678 case Tbool:
1679 case Tchar:
1680 case Twchar:
1681 e = e->castTo(sc, Type::tint32);
1682 break;
1684 case Tdchar:
1685 e = e->castTo(sc, Type::tuns32);
1686 break;
1688 return e;