Don't merge TypeMaybe if it depends on an unresolved identifier
[delight/core.git] / dmd / cast.c
blobbe53ef50a780eb96e1f0a21d88a6c6c9279f97d8
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("implicitCastTo(%s) => %s\n", type->toChars(), t->toChars());
35 if (implicitConvTo(t))
37 if (global.params.warnings &&
38 Type::impcnvWarn[type->toBasetype()->ty][t->toBasetype()->ty] &&
39 op != TOKint64)
41 Expression *e = optimize(WANTflags | WANTvalue);
43 if (e->op == TOKint64)
44 return e->implicitCastTo(sc, t);
46 fprintf(stdmsg, "warning - ");
47 error("implicit conversion of expression (%s) of type %s to %s can cause loss of data",
48 toChars(), type->toChars(), t->toChars());
50 return castTo(sc, t);
53 Expression *e = optimize(WANTflags | WANTvalue);
54 if (e != this)
55 return e->implicitCastTo(sc, t);
57 #if 0
58 print();
59 type->print();
60 printf("to:\n");
61 t->print();
62 printf("%p %p type: %s to: %s\n", type->deco, t->deco, type->deco, t->deco);
63 //printf("%p %p %p\n", type->next->arrayOf(), type, t);
64 fflush(stdout);
65 #endif
66 if (!t->deco)
67 { /* Can happen with:
68 * enum E { One }
69 * class A
70 * { static void fork(EDG dg) { dg(E.One); }
71 * alias void delegate(E) EDG;
72 * }
73 * Should eventually make it work.
75 error("forward reference to type %s", t->toChars());
77 else if (t->reliesOnTident())
78 error("forward reference to type %s", t->reliesOnTident()->toChars());
80 error("cannot implicitly convert expression (%s) of type %s to %s",
81 toChars(), type->toChars(), t->toChars());
82 return castTo(sc, t);
85 /*******************************************
86 * Return !=0 if we can implicitly convert this to type t.
87 * Don't do the actual cast.
90 MATCH Expression::implicitConvTo(Type *t)
92 #if 0
93 printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n",
94 toChars(), type->toChars(), t->toChars());
95 #endif
96 if (!type)
97 { error("%s is not an expression", toChars());
98 type = Type::terror;
100 if (t->ty == Tbit && isBit())
101 return MATCHconvert;
102 Expression *e = optimize(WANTvalue | WANTflags);
103 if (e != this)
104 { //printf("optimzed to %s\n", e->toChars());
105 return e->implicitConvTo(t);
107 MATCH match = type->implicitConvTo(t);
108 if (match)
109 return match;
110 #if 0
111 Type *tb = t->toBasetype();
112 if (tb->ty == Tdelegate)
113 { TypeDelegate *td = (TypeDelegate *)tb;
114 TypeFunction *tf = (TypeFunction *)td->next;
116 if (!tf->varargs &&
117 !(tf->arguments && tf->arguments->dim)
120 match = type->implicitConvTo(tf->next);
121 if (match)
122 return match;
123 if (tf->next->toBasetype()->ty == Tvoid)
124 return MATCHconvert;
127 #endif
128 return MATCHnomatch;
132 MATCH IntegerExp::implicitConvTo(Type *t)
134 #if 0
135 printf("IntegerExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
136 toChars(), type->toChars(), t->toChars());
137 #endif
138 if (type->equals(t))
139 return MATCHexact;
141 enum TY ty = type->toBasetype()->ty;
142 enum TY toty = t->toBasetype()->ty;
144 if (type->implicitConvTo(t) == MATCHnomatch && t->ty == Tenum)
146 return MATCHnomatch;
149 switch (ty)
151 case Tbit:
152 case Tbool:
153 value &= 1;
154 ty = Tint32;
155 break;
157 case Tint8:
158 value = (signed char)value;
159 ty = Tint32;
160 break;
162 case Tchar:
163 case Tuns8:
164 value &= 0xFF;
165 ty = Tint32;
166 break;
168 case Tint16:
169 value = (short)value;
170 ty = Tint32;
171 break;
173 case Tuns16:
174 case Twchar:
175 value &= 0xFFFF;
176 ty = Tint32;
177 break;
179 case Tint32:
180 value = (int)value;
181 break;
183 case Tuns32:
184 case Tdchar:
185 value &= 0xFFFFFFFF;
186 ty = Tuns32;
187 break;
189 default:
190 break;
193 // Only allow conversion if no change in value
194 switch (toty)
196 case Tbit:
197 case Tbool:
198 if ((value & 1) != value)
199 goto Lno;
200 goto Lyes;
202 case Tint8:
203 if ((signed char)value != value)
204 goto Lno;
205 goto Lyes;
207 case Tchar:
208 case Tuns8:
209 //printf("value = %llu %llu\n", (integer_t)(unsigned char)value, value);
210 if ((unsigned char)value != value)
211 goto Lno;
212 goto Lyes;
214 case Tint16:
215 if ((short)value != value)
216 goto Lno;
217 goto Lyes;
219 case Tuns16:
220 if ((unsigned short)value != value)
221 goto Lno;
222 goto Lyes;
224 case Tint32:
225 if (ty == Tuns32)
228 else if ((int)value != value)
229 goto Lno;
230 goto Lyes;
232 case Tuns32:
233 if (ty == Tint32)
236 else if ((unsigned)value != value)
237 goto Lno;
238 goto Lyes;
240 case Tdchar:
241 if (value > 0x10FFFFUL)
242 goto Lno;
243 goto Lyes;
245 case Twchar:
246 if ((unsigned short)value != value)
247 goto Lno;
248 goto Lyes;
250 #if IN_GCC
251 case Tfloat32:
252 case Tfloat64:
253 case Tfloat80:
255 real_t::MyMode mode;
256 real_t f;
257 switch (toty)
259 case Tfloat32: mode = real_t::Float; break;
260 case Tfloat64: mode = real_t::Double; break;
261 case Tfloat80: mode = real_t::LongDouble; break;
263 if (type->isunsigned())
265 f = real_t((d_uns64) value);
266 f = f.convert(mode);
267 if ((d_uns64) f.toInt() != (d_uns64) value)
268 goto Lno;
270 else
272 f = real_t((d_int64) value);
273 f = f.convert(mode);
274 if ((d_int64) f.toInt() != (d_int64) value)
275 goto Lno;
277 goto Lyes;
279 #else
280 case Tfloat32:
282 volatile float f;
283 if (type->isunsigned())
285 f = (float)value;
286 if (f != value)
287 goto Lno;
289 else
291 f = (float)(long long)value;
292 if (f != (long long)value)
293 goto Lno;
295 goto Lyes;
298 case Tfloat64:
300 volatile double f;
301 if (type->isunsigned())
303 f = (double)value;
304 if (f != value)
305 goto Lno;
307 else
309 f = (double)(long long)value;
310 if (f != (long long)value)
311 goto Lno;
313 goto Lyes;
316 case Tfloat80:
318 volatile long double f;
319 if (type->isunsigned())
321 f = (long double)value;
322 if (f != value)
323 goto Lno;
325 else
327 f = (long double)(long long)value;
328 if (f != (long long)value)
329 goto Lno;
331 goto Lyes;
333 #endif
335 return Expression::implicitConvTo(t);
337 Lyes:
338 //printf("MATCHconvert\n");
339 return MATCHconvert;
341 Lno:
342 //printf("MATCHnomatch\n");
343 return MATCHnomatch;
346 MATCH NullExp::implicitConvTo(Type *t)
348 #if 0
349 printf("NullExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
350 toChars(), type->toChars(), t->toChars());
351 #endif
353 if (this->type->equals(t))
354 return MATCHexact;
355 // NULL implicitly converts to any pointer type or dynamic array
356 if (type->ty == Tpointer && type->next->ty == Tvoid)
358 if (t->ty == Ttypedef)
359 t = ((TypeTypedef *)t)->sym->basetype;
360 if (t->ty == Tarray ||
361 t->ty == Taarray || t->ty == Tmaybe ||
362 t->ty == Tdelegate)
363 return committed ? MATCHconvert : MATCHexact;
366 /* Null can only be implicitly converted to a maybe type */
367 if (t->ty != Tmaybe)
368 return MATCHnomatch;
370 t = t->next;
372 return Expression::implicitConvTo(t);
375 MATCH StringExp::implicitConvTo(Type *t)
376 { MATCH m;
378 #if 0
379 printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
380 toChars(), committed, type->toChars(), t->toChars());
381 #endif
382 if (!committed)
384 if (!committed && t->ty == Tpointer && t->next->ty == Tvoid)
386 return MATCHnomatch;
388 if (type->ty == Tsarray || type->ty == Tarray || type->ty == Tpointer)
390 if (type->next->ty == Tchar)
392 switch (t->ty)
394 case Tsarray:
395 if (type->ty == Tsarray &&
396 ((TypeSArray *)type)->dim->toInteger() !=
397 ((TypeSArray *)t)->dim->toInteger())
398 return MATCHnomatch;
399 goto L1;
400 case Tarray:
401 goto L1;
402 case Tpointer:
404 if (t->next->ty == Tchar)
405 return MATCHexact;
406 else if (t->next->ty == Twchar)
407 return MATCHexact;
408 else if (t->next->ty == Tdchar)
409 return MATCHexact;
410 break;
415 return Expression::implicitConvTo(t);
416 #if 0
417 m = (MATCH)type->implicitConvTo(t);
418 if (m)
420 return m;
423 return MATCHnomatch;
424 #endif
427 MATCH ArrayLiteralExp::implicitConvTo(Type *t)
428 { MATCH result = MATCHexact;
430 Type *typeb = type->toBasetype();
431 Type *tb = t->toBasetype();
432 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
433 (typeb->ty == Tarray || typeb->ty == Tsarray))
435 if (tb->ty == Tsarray)
436 { TypeSArray *tsa = (TypeSArray *)tb;
437 if (elements->dim != tsa->dim->toInteger())
438 result = MATCHnomatch;
441 for (int i = 0; i < elements->dim; i++)
442 { Expression *e = (Expression *)elements->data[i];
443 MATCH m = (MATCH)e->implicitConvTo(tb->next);
444 if (m < result)
445 result = m; // remember worst match
446 if (result == MATCHnomatch)
447 break; // no need to check for worse
449 return result;
451 else
452 return Expression::implicitConvTo(t);
455 MATCH AssocArrayLiteralExp::implicitConvTo(Type *t)
456 { MATCH result = MATCHexact;
458 Type *typeb = type->toBasetype();
459 Type *tb = t->toBasetype();
460 if (tb->ty == Taarray && typeb->ty == Taarray)
462 for (size_t i = 0; i < keys->dim; i++)
463 { Expression *e = (Expression *)keys->data[i];
464 MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->key);
465 if (m < result)
466 result = m; // remember worst match
467 if (result == MATCHnomatch)
468 break; // no need to check for worse
469 e = (Expression *)values->data[i];
470 m = (MATCH)e->implicitConvTo(tb->next);
471 if (m < result)
472 result = m; // remember worst match
473 if (result == MATCHnomatch)
474 break; // no need to check for worse
476 return result;
478 else
479 return Expression::implicitConvTo(t);
482 MATCH AddrExp::implicitConvTo(Type *t)
484 #if 0
485 printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
486 toChars(), type->toChars(), t->toChars());
487 #endif
488 MATCH result;
490 result = type->implicitConvTo(t);
491 //printf("\tresult = %d\n", result);
493 if (result == MATCHnomatch)
495 // Look for pointers to functions where the functions are overloaded.
496 VarExp *ve;
497 FuncDeclaration *f;
499 t = t->toBasetype();
500 if (type->ty == Tpointer && type->next->ty == Tfunction &&
501 t->ty == Tpointer && t->next->ty == Tfunction &&
502 e1->op == TOKvar)
504 ve = (VarExp *)e1;
505 f = ve->var->isFuncDeclaration();
506 if (f && f->overloadExactMatch(t->next))
507 result = MATCHexact;
510 //printf("\tresult = %d\n", result);
511 return result;
514 MATCH SymOffExp::implicitConvTo(Type *t)
516 #if 0
517 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
518 toChars(), type->toChars(), t->toChars());
519 #endif
520 MATCH result;
522 result = type->implicitConvTo(t);
523 //printf("\tresult = %d\n", result);
525 if (result == MATCHnomatch)
527 // Look for pointers to functions where the functions are overloaded.
528 FuncDeclaration *f;
530 t = t->toBasetype();
531 if (type->ty == Tpointer && type->next->ty == Tfunction &&
532 t->ty == Tpointer && t->next->ty == Tfunction)
534 f = var->isFuncDeclaration();
535 if (f && f->overloadExactMatch(t->next))
536 result = MATCHexact;
539 //printf("\tresult = %d\n", result);
540 return result;
543 MATCH DelegateExp::implicitConvTo(Type *t)
545 #if 0
546 printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
547 toChars(), type->toChars(), t->toChars());
548 #endif
549 MATCH result;
551 result = type->implicitConvTo(t);
553 if (result == 0)
555 // Look for pointers to functions where the functions are overloaded.
556 FuncDeclaration *f;
558 t = t->toBasetype();
559 if (type->ty == Tdelegate && type->next->ty == Tfunction &&
560 t->ty == Tdelegate && t->next->ty == Tfunction)
562 if (func && func->overloadExactMatch(t->next))
563 result = MATCHexact;
566 return result;
569 MATCH CondExp::implicitConvTo(Type *t)
571 MATCH m1;
572 MATCH m2;
574 m1 = e1->implicitConvTo(t);
575 m2 = e2->implicitConvTo(t);
577 // Pick the worst match
578 return (m1 < m2) ? m1 : m2;
582 /* ==================== castTo ====================== */
584 /**************************************
585 * Do an explicit cast.
588 Expression *Expression::castTo(Scope *sc, Type *t)
590 //printf("Expression::castTo(this=%s, t=%s)\n", toChars(), t->toChars());
591 #if 0
592 printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
593 toChars(), type->toChars(), t->toChars());
594 #endif
595 if (type == t)
596 return this;
597 Expression *e = this;
598 Type *tb = t->toBasetype();
599 Type *typeb = type->toBasetype();
600 if (tb != typeb)
602 // Do (type *) cast of (type [dim])
603 if (tb->ty == Tpointer &&
604 typeb->ty == Tsarray
607 //printf("Converting [dim] to *\n");
609 if (typeb->size(loc) == 0)
610 e = new NullExp(loc);
611 else
612 e = new AddrExp(loc, e);
614 #if 0
615 else if (tb->ty == Tdelegate && type->ty != Tdelegate)
617 TypeDelegate *td = (TypeDelegate *)tb;
618 TypeFunction *tf = (TypeFunction *)td->nextOf();
619 return toDelegate(sc, tf->nextOf());
621 #endif
622 else
624 e = new CastExp(loc, e, tb);
627 else
629 e = e->copy(); // because of COW for assignment to e->type
631 assert(e != this);
632 e->type = t;
633 //printf("Returning: %s\n", e->toChars());
634 return e;
638 Expression *RealExp::castTo(Scope *sc, Type *t)
639 { Expression *e = this;
640 if (type != t)
642 if ((type->isreal() && t->isreal()) ||
643 (type->isimaginary() && t->isimaginary())
645 { e = copy();
646 e->type = t;
648 else
649 e = Expression::castTo(sc, t);
651 return e;
655 Expression *ComplexExp::castTo(Scope *sc, Type *t)
656 { Expression *e = this;
657 if (type != t)
659 if (type->iscomplex() && t->iscomplex())
660 { e = copy();
661 e->type = t;
663 else
664 e = Expression::castTo(sc, t);
666 return e;
670 Expression *NullExp::castTo(Scope *sc, Type *t)
671 { NullExp *e;
672 Type *tb;
674 //printf("NullExp::castTo(t = %p)\n", t);
675 if (type == t)
677 committed = 1;
678 return this;
680 e = (NullExp *)copy();
681 e->committed = 1;
682 tb = t->toBasetype();
683 e->type = type->toBasetype();
684 if (tb != e->type)
686 // NULL implicitly converts to any pointer type or dynamic array
687 if (e->type->ty == Tpointer && e->type->nextOf()->ty == Tvoid &&
688 (tb->ty == Tpointer || tb->ty == Tarray || tb->ty == Taarray ||
689 tb->ty == Tdelegate))
691 #if 0
692 if (tb->ty == Tdelegate)
693 { TypeDelegate *td = (TypeDelegate *)tb;
694 TypeFunction *tf = (TypeFunction *)td->nextOf();
696 if (!tf->varargs &&
697 !(tf->arguments && tf->arguments->dim)
700 return Expression::castTo(sc, t);
703 #endif
705 else
707 return e->Expression::castTo(sc, t);
710 e->type = t;
711 return e;
714 Expression *StringExp::castTo(Scope *sc, Type *t)
716 /* This follows copy-on-write; any changes to 'this'
717 * will result in a copy.
718 * The this->string member is considered immutable.
720 StringExp *se;
721 Type *tb;
722 int copied = 0;
724 //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed);
726 if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
728 error("cannot convert string literal to void*");
731 se = this;
732 if (!committed)
733 { se = (StringExp *)copy();
734 se->committed = 1;
735 copied = 1;
738 if (type == t)
740 return se;
743 tb = t->toBasetype();
744 //printf("\ttype = %s\n", type->toChars());
745 if (tb->ty == Tdelegate && type->toBasetype()->ty != Tdelegate)
746 return Expression::castTo(sc, t);
748 Type *typeb = type->toBasetype();
749 if (typeb == tb)
751 if (!copied)
752 { se = (StringExp *)copy();
753 copied = 1;
755 se->type = t;
756 return se;
759 if (tb->ty != Tsarray && tb->ty != Tarray && tb->ty != Tpointer)
760 { if (!copied)
761 { se = (StringExp *)copy();
762 copied = 1;
764 goto Lcast;
766 if (typeb->ty != Tsarray && typeb->ty != Tarray && typeb->ty != Tpointer)
767 { if (!copied)
768 { se = (StringExp *)copy();
769 copied = 1;
771 goto Lcast;
774 if (typeb->nextOf()->size() == tb->nextOf()->size())
776 if (!copied)
777 { se = (StringExp *)copy();
778 copied = 1;
780 if (tb->ty == Tsarray)
781 goto L2; // handle possible change in static array dimension
782 se->type = t;
783 return se;
786 if (committed)
787 goto Lcast;
789 #define X(tf,tt) ((tf) * 256 + (tt))
791 OutBuffer buffer;
792 size_t newlen = 0;
793 int tfty = typeb->nextOf()->toBasetype()->ty;
794 int ttty = tb->nextOf()->toBasetype()->ty;
795 switch (X(tfty, ttty))
797 case X(Tchar, Tchar):
798 case X(Twchar,Twchar):
799 case X(Tdchar,Tdchar):
800 break;
802 case X(Tchar, Twchar):
803 for (size_t u = 0; u < len;)
804 { unsigned c;
805 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
806 if (p)
807 error("%s", p);
808 else
809 buffer.writeUTF16(c);
811 newlen = buffer.offset / 2;
812 buffer.writeUTF16(0);
813 goto L1;
815 case X(Tchar, Tdchar):
816 for (size_t u = 0; u < len;)
817 { unsigned c;
818 char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
819 if (p)
820 error("%s", p);
821 buffer.write4(c);
822 newlen++;
824 buffer.write4(0);
825 goto L1;
827 case X(Twchar,Tchar):
828 for (size_t u = 0; u < len;)
829 { unsigned c;
830 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
831 if (p)
832 error("%s", p);
833 else
834 buffer.writeUTF8(c);
836 newlen = buffer.offset;
837 buffer.writeUTF8(0);
838 goto L1;
840 case X(Twchar,Tdchar):
841 for (size_t u = 0; u < len;)
842 { unsigned c;
843 char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
844 if (p)
845 error("%s", p);
846 buffer.write4(c);
847 newlen++;
849 buffer.write4(0);
850 goto L1;
852 case X(Tdchar,Tchar):
853 for (size_t u = 0; u < len; u++)
855 unsigned c = ((unsigned *)se->string)[u];
856 if (!utf_isValidDchar(c))
857 error("invalid UCS-32 char \\U%08x", c);
858 else
859 buffer.writeUTF8(c);
860 newlen++;
862 newlen = buffer.offset;
863 buffer.writeUTF8(0);
864 goto L1;
866 case X(Tdchar,Twchar):
867 for (size_t u = 0; u < len; u++)
869 unsigned c = ((unsigned *)se->string)[u];
870 if (!utf_isValidDchar(c))
871 error("invalid UCS-32 char \\U%08x", c);
872 else
873 buffer.writeUTF16(c);
874 newlen++;
876 newlen = buffer.offset / 2;
877 buffer.writeUTF16(0);
878 goto L1;
881 if (!copied)
882 { se = (StringExp *)copy();
883 copied = 1;
885 se->string = buffer.extractData();
886 se->len = newlen;
887 se->sz = tb->nextOf()->size();
888 break;
890 default:
891 assert(typeb->nextOf()->size() != tb->nextOf()->size());
892 goto Lcast;
895 #undef X
897 assert(copied);
899 // See if need to truncate or extend the literal
900 if (tb->ty == Tsarray)
902 int dim2 = ((TypeSArray *)tb)->dim->toInteger();
904 //printf("dim from = %d, to = %d\n", se->len, dim2);
906 // Changing dimensions
907 if (dim2 != se->len)
909 // Copy when changing the string literal
910 unsigned newsz = se->sz;
911 void *s;
912 int d;
914 d = (dim2 < se->len) ? dim2 : se->len;
915 s = (unsigned char *)mem.malloc((dim2 + 1) * newsz);
916 memcpy(s, se->string, d * newsz);
917 // Extend with 0, add terminating 0
918 memset((char *)s + d * newsz, 0, (dim2 + 1 - d) * newsz);
919 se->string = s;
920 se->len = dim2;
923 se->type = t;
924 return se;
926 Lcast:
927 Expression *e = new CastExp(loc, se, t);
928 e->type = t; // so semantic() won't be run on e
929 return e;
932 Expression *AddrExp::castTo(Scope *sc, Type *t)
934 Type *tb;
936 #if 0
937 printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
938 toChars(), type->toChars(), t->toChars());
939 #endif
940 Expression *e = this;
942 tb = t->toBasetype();
943 type = type->toBasetype();
944 if (tb != type)
946 // Look for pointers to functions where the functions are overloaded.
947 VarExp *ve;
948 FuncDeclaration *f;
950 if (type->ty == Tpointer && type->next->ty == Tfunction &&
951 tb->ty == Tpointer && tb->next->ty == Tfunction &&
952 e1->op == TOKvar)
954 ve = (VarExp *)e1;
955 f = ve->var->isFuncDeclaration();
956 if (f)
958 f = f->overloadExactMatch(tb->next);
959 if (f)
961 e = new VarExp(loc, f);
962 e->type = f->type;
963 e = new AddrExp(loc, e);
964 e->type = t;
965 return e;
969 e = Expression::castTo(sc, t);
971 e->type = t;
972 return e;
976 Expression *TupleExp::castTo(Scope *sc, Type *t)
977 { TupleExp *e = (TupleExp *)copy();
978 e->exps = (Expressions *)exps->copy();
979 for (size_t i = 0; i < e->exps->dim; i++)
980 { Expression *ex = (Expression *)e->exps->data[i];
981 ex = ex->castTo(sc, t);
982 e->exps->data[i] = (void *)ex;
984 return e;
988 Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t)
990 #if 0
991 printf("ArrayLiteralExp::castTo(this=%s, type=%s, t=%s)\n",
992 toChars(), type->toChars(), t->toChars());
993 #endif
994 if (type == t)
995 return this;
996 ArrayLiteralExp *e = this;
997 Type *typeb = type->toBasetype();
998 Type *tb = t->toBasetype();
999 if ((tb->ty == Tarray || tb->ty == Tsarray) &&
1000 (typeb->ty == Tarray || typeb->ty == Tsarray) &&
1001 // Not trying to convert non-void[] to void[]
1002 !(tb->nextOf()->toBasetype()->ty == Tvoid && typeb->nextOf()->toBasetype()->ty != Tvoid))
1004 if (tb->ty == Tsarray)
1005 { TypeSArray *tsa = (TypeSArray *)tb;
1006 if (elements->dim != tsa->dim->toInteger())
1007 goto L1;
1010 e = (ArrayLiteralExp *)copy();
1011 e->elements = (Expressions *)elements->copy();
1012 for (int i = 0; i < elements->dim; i++)
1013 { Expression *ex = (Expression *)elements->data[i];
1014 ex = ex->castTo(sc, tb->nextOf());
1015 e->elements->data[i] = (void *)ex;
1017 e->type = t;
1018 return e;
1020 if (tb->ty == Tpointer && typeb->ty == Tsarray)
1022 e = (ArrayLiteralExp *)copy();
1023 e->type = typeb->nextOf()->pointerTo();
1026 return e->Expression::castTo(sc, t);
1029 Expression *AssocArrayLiteralExp::castTo(Scope *sc, Type *t)
1031 if (type == t)
1032 return this;
1033 AssocArrayLiteralExp *e = this;
1034 Type *typeb = type->toBasetype();
1035 Type *tb = t->toBasetype();
1036 if (tb->ty == Taarray && typeb->ty == Taarray &&
1037 tb->nextOf()->toBasetype()->ty != Tvoid)
1039 e = (AssocArrayLiteralExp *)copy();
1040 e->keys = (Expressions *)keys->copy();
1041 e->values = (Expressions *)values->copy();
1042 assert(keys->dim == values->dim);
1043 for (size_t i = 0; i < keys->dim; i++)
1044 { Expression *ex = (Expression *)values->data[i];
1045 ex = ex->castTo(sc, tb->nextOf());
1046 e->values->data[i] = (void *)ex;
1048 ex = (Expression *)keys->data[i];
1049 ex = ex->castTo(sc, ((TypeAArray *)tb)->index);
1050 e->keys->data[i] = (void *)ex;
1052 e->type = t;
1053 return e;
1056 return e->Expression::castTo(sc, t);
1060 Expression *SymOffExp::castTo(Scope *sc, Type *t)
1062 Type *tb;
1064 #if 0
1065 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
1066 toChars(), type->toChars(), t->toChars());
1067 #endif
1068 Expression *e = this;
1070 tb = t->toBasetype();
1071 type = type->toBasetype();
1072 if (tb != type)
1074 // Look for pointers to functions where the functions are overloaded.
1075 FuncDeclaration *f;
1077 if (type->ty == Tpointer && type->next->ty == Tfunction &&
1078 tb->ty == Tpointer && tb->next->ty == Tfunction)
1080 f = var->isFuncDeclaration();
1081 if (f)
1083 f = f->overloadExactMatch(tb->next);
1084 if (f)
1086 e = new SymOffExp(loc, f, 0);
1087 e->type = t;
1088 return e;
1092 e = Expression::castTo(sc, t);
1094 e->type = t;
1095 return e;
1098 Expression *DelegateExp::castTo(Scope *sc, Type *t)
1100 Type *tb;
1101 #if 0
1102 printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
1103 toChars(), type->toChars(), t->toChars());
1104 #endif
1105 Expression *e = this;
1106 static char msg[] = "cannot form delegate due to covariant return type";
1108 tb = t->toBasetype();
1109 type = type->toBasetype();
1110 if (tb != type)
1112 // Look for delegates to functions where the functions are overloaded.
1113 FuncDeclaration *f;
1115 if (type->ty == Tdelegate && type->next->ty == Tfunction &&
1116 tb->ty == Tdelegate && tb->next->ty == Tfunction)
1118 if (func)
1120 f = func->overloadExactMatch(tb->next);
1121 if (f)
1122 { target_ptrdiff_t offset;
1123 if (f->tintro && f->tintro->next->isBaseOf(f->type->next, &offset) && offset)
1124 error("%s", msg);
1125 e = new DelegateExp(loc, e1, f);
1126 e->type = t;
1127 return e;
1129 if (func->tintro)
1130 error("%s", msg);
1133 e = Expression::castTo(sc, t);
1135 else
1136 { target_ptrdiff_t offset;
1138 if (func->tintro && func->tintro->next->isBaseOf(func->type->next, &offset) && offset)
1139 error("%s", msg);
1141 e->type = t;
1142 return e;
1145 Expression *CondExp::castTo(Scope *sc, Type *t)
1147 Expression *e = this;
1149 if (type != t)
1151 if (1 || e1->op == TOKstring || e2->op == TOKstring)
1152 { e = new CondExp(loc, econd, e1->castTo(sc, t), e2->castTo(sc, t));
1153 e->type = t;
1155 else
1156 e = Expression::castTo(sc, t);
1158 return e;
1161 /* ==================== ====================== */
1163 /****************************************
1164 * Scale addition/subtraction to/from pointer.
1167 Expression *BinExp::scaleFactor(Scope *sc)
1168 { d_uns64 stride;
1169 Type *t1b = e1->type->toBasetype();
1170 Type *t2b = e2->type->toBasetype();
1172 if (t1b->ty == Tpointer && t2b->isintegral())
1173 { // Need to adjust operator by the stride
1174 // Replace (ptr + int) with (ptr + (int * stride))
1175 Type *t = Type::tptrdiff_t;
1177 stride = t1b->next->size();
1178 if (!t->equals(t2b))
1179 e2 = e2->castTo(sc, t);
1180 if (t1b->next->isbit())
1181 // BUG: should add runtime check for misaligned offsets
1182 // This perhaps should be done by rewriting as &p[i]
1183 // and letting back end do it.
1184 e2 = new UshrExp(loc, e2, new IntegerExp(0, 3, t));
1185 else
1186 e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t));
1187 e2->type = t;
1188 type = e1->type;
1190 else if (t2b->ty && t1b->isintegral())
1191 { // Need to adjust operator by the stride
1192 // Replace (int + ptr) with (ptr + (int * stride))
1193 Type *t = Type::tptrdiff_t;
1194 Expression *e;
1196 stride = t2b->next->size();
1197 if (!t->equals(t1b))
1198 e = e1->castTo(sc, t);
1199 else
1200 e = e1;
1201 if (t2b->next->isbit())
1202 // BUG: should add runtime check for misaligned offsets
1203 e = new UshrExp(loc, e, new IntegerExp(0, 3, t));
1204 else
1205 e = new MulExp(loc, e, new IntegerExp(0, stride, t));
1206 e->type = t;
1207 type = e2->type;
1208 e1 = e2;
1209 e2 = e;
1211 return this;
1214 /************************************
1215 * Bring leaves to common type.
1218 Expression *BinExp::typeCombine(Scope *sc)
1220 Type *t1;
1221 Type *t2;
1222 Type *t;
1223 TY ty;
1225 //printf("BinExp::typeCombine()\n");
1226 //dump(0);
1228 e1 = e1->integralPromotions(sc);
1229 e2 = e2->integralPromotions(sc);
1231 // BUG: do toBasetype()
1232 t1 = e1->type;
1233 t2 = e2->type;
1234 assert(t1);
1236 //if (t1) printf("\tt1 = %s\n", t1->toChars());
1237 //if (t2) printf("\tt2 = %s\n", t2->toChars());
1238 #ifdef DEBUG
1239 if (!t2) printf("\te2 = '%s'\n", e2->toChars());
1240 #endif
1241 assert(t2);
1243 Type *t1b = t1->toBasetype();
1244 Type *t2b = t2->toBasetype();
1246 ty = (TY)Type::impcnvResult[t1b->ty][t2b->ty];
1247 if (ty != Terror)
1248 { TY ty1;
1249 TY ty2;
1251 ty1 = (TY)Type::impcnvType1[t1b->ty][t2b->ty];
1252 ty2 = (TY)Type::impcnvType2[t1b->ty][t2b->ty];
1254 if (t1b->ty == ty1) // if no promotions
1256 if (t1 == t2)
1258 if (!type)
1259 type = t1;
1260 return this;
1263 if (t1b == t2b)
1265 if (!type)
1266 type = t1b;
1267 return this;
1271 if (!type)
1272 type = Type::basic[ty];
1274 t1 = Type::basic[ty1];
1275 t2 = Type::basic[ty2];
1276 e1 = e1->castTo(sc, t1);
1277 e2 = e2->castTo(sc, t2);
1278 #if 0
1279 if (type != Type::basic[ty])
1280 { t = type;
1281 type = Type::basic[ty];
1282 return castTo(sc, t);
1284 #endif
1285 //printf("after typeCombine():\n");
1286 //dump(0);
1287 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
1288 return this;
1291 t = t1;
1292 if (t1 == t2)
1294 if ((t1->ty == Tstruct || t1->ty == Tclass) &&
1295 (op == TOKmin || op == TOKadd))
1296 goto Lincompatible;
1298 else if (t1->isintegral() && t2->isintegral())
1300 printf("t1 = %s, t2 = %s\n", t1->toChars(), t2->toChars());
1301 int sz1 = t1->size();
1302 int sz2 = t2->size();
1303 int sign1 = t1->isunsigned() == 0;
1304 int sign2 = t2->isunsigned() == 0;
1306 if (sign1 == sign2)
1308 if (sz1 < sz2)
1309 goto Lt2;
1310 else
1311 goto Lt1;
1313 if (!sign1)
1315 if (sz1 >= sz2)
1316 goto Lt1;
1317 else
1318 goto Lt2;
1320 else
1322 if (sz2 >= sz1)
1323 goto Lt2;
1324 else
1325 goto Lt1;
1328 else if (t1->ty == Tpointer && t2->ty == Tpointer)
1330 // Bring pointers to compatible type
1331 Type *t1n = t1->next;
1332 Type *t2n = t2->next;
1334 //t1->print();
1335 //t2->print();
1336 //if (t1n == t2n) *(char *)0 = 0;
1337 assert(t1n != t2n);
1338 if (t1n->ty == Tvoid) // pointers to void are always compatible
1339 t = t2;
1340 else if (t2n->ty == Tvoid)
1342 else if (t1n->ty == Tclass && t2n->ty == Tclass)
1343 { ClassDeclaration *cd1 = t1n->isClassHandle();
1344 ClassDeclaration *cd2 = t2n->isClassHandle();
1345 target_ptrdiff_t offset;
1347 if (cd1->isBaseOf(cd2, &offset))
1349 if (offset)
1350 e2 = e2->castTo(sc, t);
1352 else if (cd2->isBaseOf(cd1, &offset))
1354 t = t2;
1355 if (offset)
1356 e1 = e1->castTo(sc, t);
1358 else
1359 goto Lincompatible;
1361 else
1362 goto Lincompatible;
1364 else if ((t1->ty == Tsarray || t1->ty == Tarray) &&
1365 e2->op == TOKnull && t2->ty == Tpointer && t2->next->ty == Tvoid)
1367 goto Lx1;
1369 else if ((t2->ty == Tsarray || t2->ty == Tarray) &&
1370 e1->op == TOKnull && t1->ty == Tpointer && t1->next->ty == Tvoid)
1372 goto Lx2;
1374 else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2))
1376 goto Lt2;
1378 else if ((t2->ty == Tsarray || t2->ty == Tarray) && t2->implicitConvTo(t1))
1380 goto Lt1;
1382 else if (t1->ty == Tclass || t2->ty == Tclass)
1384 while (1)
1386 int i1 = e2->implicitConvTo(t1);
1387 int i2 = e1->implicitConvTo(t2);
1389 if (i1 && i2)
1391 // We have the case of class vs. void*, so pick class
1392 if (t1->ty == Tpointer)
1393 i1 = 0;
1394 else if (t2->ty == Tpointer)
1395 i2 = 0;
1398 if (i2)
1400 goto Lt2;
1402 else if (i1)
1404 goto Lt1;
1406 else if (t1->ty == Tclass && t2->ty == Tclass)
1407 { TypeClass *tc1 = (TypeClass *)t1;
1408 TypeClass *tc2 = (TypeClass *)t2;
1410 /* Pick 'tightest' type
1412 ClassDeclaration *cd1 = tc1->sym->baseClass;
1413 ClassDeclaration *cd2 = tc1->sym->baseClass;
1415 if (cd1 && cd2)
1416 { t1 = cd1->type;
1417 t2 = cd2->type;
1419 else if (cd1)
1420 t1 = cd1->type;
1421 else if (cd2)
1422 t2 = cd2->type;
1423 else
1424 goto Lincompatible;
1426 else
1427 goto Lincompatible;
1430 else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2))
1432 goto Lt2;
1434 //else if (e2->op == TOKstring) { printf("test2\n"); }
1435 else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1))
1437 goto Lt1;
1439 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
1440 e2->implicitConvTo(t1->next->arrayOf()))
1442 Lx1:
1443 t = t1->next->arrayOf();
1444 e1 = e1->castTo(sc, t);
1445 e2 = e2->castTo(sc, t);
1447 else if (t1->ty == Tsarray && t2->ty == Tsarray &&
1448 e1->implicitConvTo(t2->next->arrayOf()))
1450 Lx2:
1451 t = t2->next->arrayOf();
1452 e1 = e1->castTo(sc, t);
1453 e2 = e2->castTo(sc, t);
1455 else
1457 Lincompatible:
1458 incompatibleTypes();
1460 Lret:
1461 if (!type)
1462 type = t;
1463 //dump(0);
1464 return this;
1467 Lt1:
1468 e2 = e2->castTo(sc, t1);
1469 t = t1;
1470 goto Lret;
1472 Lt2:
1473 e1 = e1->castTo(sc, t2);
1474 t = t2;
1475 goto Lret;
1478 /***********************************
1479 * Do integral promotions (convertchk).
1480 * Don't convert <array of> to <pointer to>
1483 Expression *Expression::integralPromotions(Scope *sc)
1484 { Expression *e;
1486 e = this;
1487 switch (type->toBasetype()->ty)
1489 case Tvoid:
1490 error("void has no value");
1491 break;
1493 case Tint8:
1494 case Tuns8:
1495 case Tint16:
1496 case Tuns16:
1497 case Tbit:
1498 case Tbool:
1499 case Tchar:
1500 case Twchar:
1501 e = e->castTo(sc, Type::tint32);
1502 break;
1504 case Tdchar:
1505 e = e->castTo(sc, Type::tuns32);
1506 break;
1508 return e;