2 // Copyright (c) 1999-2008 by Digital Mars
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.
16 #include "../root/mem.h"
19 #include "expression.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
);
39 if (global
.params
.warnings
&&
40 Type::impcnvWarn
[type
->toBasetype()->ty
][t
->toBasetype()->ty
] &&
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();
61 Expression
*e
= optimize(WANTflags
| WANTvalue
);
63 return e
->implicitCastTo(sc
, t
);
66 printf("ty = %d\n", type
->ty
);
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);
79 * { static void fork(EDG dg) { dg(E.One); }
80 * alias void delegate(E) EDG;
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());
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
)
102 printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n",
103 toChars(), type
->toChars(), t
->toChars());
105 //static int nest; if (++nest == 10) halt();
107 { error("%s is not an expression", toChars());
110 Expression
*e
= optimize(WANTvalue
| WANTflags
);
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
)
121 Type
*tb
= t
->toBasetype();
122 if (tb
->ty
== Tdelegate
)
123 { TypeDelegate
*td
= (TypeDelegate
*)tb
;
124 TypeFunction
*tf
= (TypeFunction
*)td
->nextOf();
127 !(tf
->arguments
&& tf
->arguments
->dim
)
130 match
= type
->implicitConvTo(tf
->nextOf());
133 if (tf
->nextOf()->toBasetype()->ty
== Tvoid
)
142 MATCH
IntegerExp::implicitConvTo(Type
*t
)
145 printf("IntegerExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
146 toChars(), type
->toChars(), t
->toChars());
148 MATCH m
= type
->implicitConvTo(t
);
152 TY ty
= type
->toBasetype()->ty
;
153 TY toty
= t
->toBasetype()->ty
;
155 if (m
== MATCHnomatch
&& t
->ty
== Tenum
)
167 value
= (signed char)value
;
178 value
= (short)value
;
202 // Only allow conversion if no change in value
207 if ((value
& 1) != value
)
212 if ((signed char)value
!= value
)
218 //printf("value = %llu %llu\n", (integer_t)(unsigned char)value, value);
219 if ((unsigned char)value
!= value
)
224 if ((short)value
!= value
)
229 if ((unsigned short)value
!= value
)
237 else if ((int)value
!= value
)
245 else if ((unsigned)value
!= value
)
250 if (value
> 0x10FFFFUL
)
255 if ((unsigned short)value
!= value
)
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
);
276 if ((d_uns64
) f
.toInt() != (d_uns64
) value
)
281 f
= real_t((d_int64
) value
);
283 if ((d_int64
) f
.toInt() != (d_int64
) value
)
292 if (type
->isunsigned())
300 f
= (float)(long long)value
;
301 if (f
!= (long long)value
)
310 if (type
->isunsigned())
318 f
= (double)(long long)value
;
319 if (f
!= (long long)value
)
327 volatile long double f
;
328 if (type
->isunsigned())
330 f
= (long double)value
;
336 f
= (long double)(long long)value
;
337 if (f
!= (long long)value
)
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;
357 return Expression::implicitConvTo(t
);
366 MATCH
NullExp::implicitConvTo(Type
*t
)
369 printf("NullExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
370 toChars(), type
->toChars(), t
->toChars());
373 if (this->type
->equals(t
))
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()))
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
||
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
;
401 /* In Delight, null can only be implicitly converted to a maybe type */
408 return Expression::implicitConvTo(t
);
411 MATCH
StructLiteralExp::implicitConvTo(Type
*t
)
414 printf("StructLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
415 toChars(), type
->toChars(), t
->toChars());
417 MATCH m
= Expression::implicitConvTo(t
);
418 if (m
!= MATCHnomatch
)
420 if (type
->ty
== t
->ty
&& type
->ty
== Tstruct
&&
421 ((TypeStruct
*)type
)->sym
== ((TypeStruct
*)t
)->sym
)
424 for (int i
= 0; i
< elements
->dim
; i
++)
425 { Expression
*e
= (Expression
*)elements
->data
[i
];
428 te
= te
->mutableOf();
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);
442 MATCH
StringExp::implicitConvTo(Type
*t
)
446 printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
447 toChars(), committed
, type
->toChars(), t
->toChars());
451 if (!committed
&& t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tvoid
)
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
)
465 if (type
->ty
== Tsarray
)
467 if (((TypeSArray
*)type
)->dim
->toInteger() !=
468 ((TypeSArray
*)t
)->dim
->toInteger())
470 TY tynto
= t
->nextOf()->ty
;
471 if (tynto
== Tchar
|| tynto
== Twchar
|| tynto
== Tdchar
)
478 if (type
->nextOf()->mod
!= tn
->mod
)
479 { if (!tn
->isConst())
495 return Expression::implicitConvTo(t
);
497 m
= (MATCH
)type
->implicitConvTo(t
);
507 MATCH
ArrayLiteralExp::implicitConvTo(Type
*t
)
508 { MATCH result
= MATCHexact
;
511 printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
512 toChars(), type
->toChars(), t
->toChars());
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());
529 result
= m
; // remember worst match
530 if (result
== MATCHnomatch
)
531 break; // no need to check for worse
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
);
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());
556 result
= m
; // remember worst match
557 if (result
== MATCHnomatch
)
558 break; // no need to check for worse
563 return Expression::implicitConvTo(t
);
566 MATCH
AddrExp::implicitConvTo(Type
*t
)
569 printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
570 toChars(), type
->toChars(), t
->toChars());
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.
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();
591 if (f2
->overloadExactMatch(t
->nextOf()))
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
);
604 if (type
->ty
== Tpointer
&& type
->nextOf()->ty
== Tfunction
&&
605 t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tfunction
&&
608 /* I don't think this can ever happen -
609 * it should have been
610 * converted to a SymOffExp.
613 VarExp
*ve
= (VarExp
*)e1
;
614 FuncDeclaration
*f
= ve
->var
->isFuncDeclaration();
615 if (f
&& f
->overloadExactMatch(t
->nextOf()))
619 //printf("\tresult = %d\n", result);
623 MATCH
SymOffExp::implicitConvTo(Type
*t
)
626 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
627 toChars(), type
->toChars(), t
->toChars());
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.
640 if (type
->ty
== Tpointer
&& type
->nextOf()->ty
== Tfunction
&&
641 (t
->ty
== Tpointer
|| t
->ty
== Tdelegate
) && t
->nextOf()->ty
== Tfunction
)
643 f
= var
->isFuncDeclaration();
645 { f
= f
->overloadExactMatch(t
->nextOf());
647 { if ((t
->ty
== Tdelegate
&& (f
->needThis() || f
->isNested())) ||
648 (t
->ty
== Tpointer
&& !(f
->needThis() || f
->isNested())))
656 //printf("\tresult = %d\n", result);
660 MATCH
DelegateExp::implicitConvTo(Type
*t
)
663 printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
664 toChars(), type
->toChars(), t
->toChars());
668 result
= type
->implicitConvTo(t
);
670 if (result
== MATCHnomatch
)
672 // Look for pointers to functions where the functions are overloaded.
676 if (type
->ty
== Tdelegate
&& type
->nextOf()->ty
== Tfunction
&&
677 t
->ty
== Tdelegate
&& t
->nextOf()->ty
== Tfunction
)
679 if (func
&& func
->overloadExactMatch(t
->nextOf()))
686 MATCH
CondExp::implicitConvTo(Type
*t
)
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());
709 printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
710 toChars(), type
->toChars(), t
->toChars());
714 Expression
*e
= this;
715 Type
*tb
= t
->toBasetype();
716 Type
*typeb
= type
->toBasetype();
719 // Do (type *) cast of (type [dim])
720 if (tb
->ty
== Tpointer
&&
724 //printf("Converting [dim] to *\n");
726 if (typeb
->size(loc
) == 0)
727 e
= new NullExp(loc
);
729 e
= new AddrExp(loc
, e
);
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());
741 e
= new CastExp(loc
, e
, tb
);
746 e
= e
->copy(); // because of COW for assignment to e->type
750 //printf("Returning: %s\n", e->toChars());
755 Expression
*RealExp::castTo(Scope
*sc
, Type
*t
)
756 { Expression
*e
= this;
759 if ((type
->isreal() && t
->isreal()) ||
760 (type
->isimaginary() && t
->isimaginary())
766 e
= Expression::castTo(sc
, t
);
772 Expression
*ComplexExp::castTo(Scope
*sc
, Type
*t
)
773 { Expression
*e
= this;
776 if (type
->iscomplex() && t
->iscomplex())
781 e
= Expression::castTo(sc
, t
);
787 Expression
*NullExp::castTo(Scope
*sc
, Type
*t
)
791 //printf("NullExp::castTo(t = %p)\n", t);
797 e
= (NullExp
*)copy();
799 tb
= t
->toBasetype();
800 e
->type
= type
->toBasetype();
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
))
809 if (tb
->ty
== Tdelegate
)
810 { TypeDelegate
*td
= (TypeDelegate
*)tb
;
811 TypeFunction
*tf
= (TypeFunction
*)td
->nextOf();
814 !(tf
->arguments
&& tf
->arguments
->dim
)
817 return Expression::castTo(sc
, t
);
824 return e
->Expression::castTo(sc
, t
);
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.
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*");
850 { se
= (StringExp
*)copy();
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();
869 { se
= (StringExp
*)copy();
876 if (tb
->ty
!= Tsarray
&& tb
->ty
!= Tarray
&& tb
->ty
!= Tpointer
)
878 { se
= (StringExp
*)copy();
883 if (typeb
->ty
!= Tsarray
&& typeb
->ty
!= Tarray
&& typeb
->ty
!= Tpointer
)
885 { se
= (StringExp
*)copy();
891 if (typeb
->nextOf()->size() == tb
->nextOf()->size())
894 { se
= (StringExp
*)copy();
897 if (tb
->ty
== Tsarray
)
898 goto L2
; // handle possible change in static array dimension
906 #define X(tf,tt) ((tf) * 256 + (tt))
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
):
919 case X(Tchar
, Twchar
):
920 for (size_t u
= 0; u
< len
;)
922 char *p
= utf_decodeChar((unsigned char *)se
->string
, len
, &u
, &c
);
926 buffer
.writeUTF16(c
);
928 newlen
= buffer
.offset
/ 2;
929 buffer
.writeUTF16(0);
932 case X(Tchar
, Tdchar
):
933 for (size_t u
= 0; u
< len
;)
935 char *p
= utf_decodeChar((unsigned char *)se
->string
, len
, &u
, &c
);
944 case X(Twchar
,Tchar
):
945 for (size_t u
= 0; u
< len
;)
947 char *p
= utf_decodeWchar((unsigned short *)se
->string
, len
, &u
, &c
);
953 newlen
= buffer
.offset
;
957 case X(Twchar
,Tdchar
):
958 for (size_t u
= 0; u
< len
;)
960 char *p
= utf_decodeWchar((unsigned short *)se
->string
, len
, &u
, &c
);
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
);
979 newlen
= buffer
.offset
;
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
);
990 buffer
.writeUTF16(c
);
993 newlen
= buffer
.offset
/ 2;
994 buffer
.writeUTF16(0);
999 { se
= (StringExp
*)copy();
1002 se
->string
= buffer
.extractData();
1004 se
->sz
= tb
->nextOf()->size();
1008 assert(typeb
->nextOf()->size() != tb
->nextOf()->size());
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
;
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
);
1044 Expression
*e
= new CastExp(loc
, se
, t
);
1045 e
->type
= t
; // so semantic() won't be run on e
1049 Expression
*AddrExp::castTo(Scope
*sc
, Type
*t
)
1054 printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
1055 toChars(), type
->toChars(), t
->toChars());
1057 Expression
*e
= this;
1059 tb
= t
->toBasetype();
1060 type
= type
->toBasetype();
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();
1073 if (f2
->overloadExactMatch(t
->nextOf()))
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
);
1084 { f
->tookAddressOf
= 1;
1085 SymOffExp
*se
= new SymOffExp(loc
, f
, 0, 0);
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
&&
1097 VarExp
*ve
= (VarExp
*)e1
;
1098 FuncDeclaration
*f
= ve
->var
->isFuncDeclaration();
1101 assert(0); // should be SymOffExp instead
1102 f
= f
->overloadExactMatch(tb
->nextOf());
1105 e
= new VarExp(loc
, f
);
1107 e
= new AddrExp(loc
, e
);
1113 e
= Expression::castTo(sc
, t
);
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
;
1132 Expression
*ArrayLiteralExp::castTo(Scope
*sc
, Type
*t
)
1135 printf("ArrayLiteralExp::castTo(this=%s, type=%s, => %s)\n",
1136 toChars(), type
->toChars(), t
->toChars());
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())
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
;
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
)
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
;
1200 return e
->Expression::castTo(sc
, t
);
1203 Expression
*SymOffExp::castTo(Scope
*sc
, Type
*t
)
1206 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
1207 toChars(), type
->toChars(), t
->toChars());
1209 if (type
== t
&& hasOverloads
== 0)
1212 Type
*tb
= t
->toBasetype();
1213 Type
*typeb
= type
->toBasetype();
1216 // Look for pointers to functions where the functions are overloaded.
1220 typeb
->ty
== Tpointer
&& typeb
->nextOf()->ty
== Tfunction
&&
1221 (tb
->ty
== Tpointer
|| tb
->ty
== Tdelegate
) && tb
->nextOf()->ty
== Tfunction
)
1223 f
= var
->isFuncDeclaration();
1226 f
= f
->overloadExactMatch(tb
->nextOf());
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
);
1241 e
= new SymOffExp(loc
, f
, 0);
1244 f
->tookAddressOf
= 1;
1249 e
= Expression::castTo(sc
, t
);
1254 ((SymOffExp
*)e
)->hasOverloads
= 0;
1259 Expression
*DelegateExp::castTo(Scope
*sc
, Type
*t
)
1262 printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
1263 toChars(), type
->toChars(), t
->toChars());
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();
1272 // Look for delegates to functions where the functions are overloaded.
1275 if (typeb
->ty
== Tdelegate
&& typeb
->nextOf()->ty
== Tfunction
&&
1276 tb
->ty
== Tdelegate
&& tb
->nextOf()->ty
== Tfunction
)
1280 f
= func
->overloadExactMatch(tb
->nextOf());
1282 { target_ptrdiff_t offset
;
1283 if (f
->tintro
&& f
->tintro
->nextOf()->isBaseOf(f
->type
->nextOf(), &offset
) && offset
)
1285 f
->tookAddressOf
= 1;
1286 e
= new DelegateExp(loc
, e1
, f
);
1294 e
= Expression::castTo(sc
, t
);
1297 { target_ptrdiff_t offset
;
1299 func
->tookAddressOf
= 1;
1300 if (func
->tintro
&& func
->tintro
->nextOf()->isBaseOf(func
->type
->nextOf(), &offset
) && offset
)
1308 Expression
*CondExp::castTo(Scope
*sc
, Type
*t
)
1310 Expression
*e
= this;
1314 if (1 || e1
->op
== TOKstring
|| e2
->op
== TOKstring
)
1315 { e
= new CondExp(loc
, econd
, e1
->castTo(sc
, t
), e2
->castTo(sc
, t
));
1319 e
= Expression::castTo(sc
, t
);
1324 /* ==================== ====================== */
1326 /****************************************
1327 * Scale addition/subtraction to/from pointer.
1330 Expression
*BinExp::scaleFactor(Scope
*sc
)
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
));
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
;
1353 stride
= t2b
->nextOf()->size(loc
);
1354 if (!t
->equals(t1b
))
1355 e
= e1
->castTo(sc
, t
);
1358 e
= new MulExp(loc
, e
, new IntegerExp(0, stride
, t
));
1367 /************************************
1368 * Bring leaves to common type.
1371 Expression
*BinExp::typeCombine(Scope
*sc
)
1378 //printf("BinExp::typeCombine() %s\n", toChars());
1381 e1
= e1
->integralPromotions(sc
);
1382 e2
= e2
->integralPromotions(sc
);
1384 // BUG: do toBasetype()
1390 //if (t1) printf("\tt1 = %s\n", t1->toChars());
1391 //if (t2) printf("\tt2 = %s\n", t2->toChars());
1393 if (!t2
) printf("\te2 = '%s'\n", e2
->toChars());
1397 Type
*t1b
= t1
->toBasetype();
1398 Type
*t2b
= t2
->toBasetype();
1400 ty
= (TY
)Type::impcnvResult
[t1b
->ty
][t2b
->ty
];
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
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
);
1433 if (type
!= Type::basic
[ty
])
1435 type
= Type::basic
[ty
];
1436 return castTo(sc
, t
);
1439 //printf("after typeCombine():\n");
1441 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
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
)
1463 e1
= e1
->castTo(sc
, t1
);
1464 if (e2
->op
== TOKstring
&& !((StringExp
*)e2
)->committed
)
1467 e2
= e2
->castTo(sc
, t2
);
1476 if ((t1
->ty
== Tstruct
|| t1
->ty
== Tclass
) &&
1477 (op
== TOKmin
|| op
== TOKadd
))
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();
1488 else if (t1n
->ty
== Tvoid
) // pointers to void are always compatible
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();
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
))
1507 e2
= e2
->castTo(sc
, t
);
1509 else if (cd2
->isBaseOf(cd1
, &offset
))
1513 e1
= e1
->castTo(sc
, t
);
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*)
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])
1535 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) && t1
->implicitConvTo(t2
))
1539 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) && t2
->implicitConvTo(t1
))
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();
1554 t1
= t1
->nextOf()->mutableOf()->constOf()->arrayOf();
1556 if (t2
->ty
== Tpointer
)
1557 t2
= t2
->nextOf()->mutableOf()->constOf()->pointerTo();
1559 t2
= t2
->nextOf()->mutableOf()->constOf()->arrayOf();
1563 else if (t1
->ty
== Tclass
|| t2
->ty
== Tclass
)
1567 int i1
= e2
->implicitConvTo(t1
);
1568 int i2
= e1
->implicitConvTo(t2
);
1572 // We have the case of class vs. void*, so pick class
1573 if (t1
->ty
== Tpointer
)
1575 else if (t2
->ty
== Tpointer
)
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
;
1611 else if (t1
->ty
== Tstruct
&& t2
->ty
== Tstruct
)
1613 if (((TypeStruct
*)t1
)->sym
!= ((TypeStruct
*)t2
)->sym
)
1616 else if ((e1
->op
== TOKstring
|| e1
->op
== TOKnull
) && e1
->implicitConvTo(t2
))
1620 else if ((e2
->op
== TOKstring
|| e2
->op
== TOKnull
) && e2
->implicitConvTo(t1
))
1624 else if (t1
->ty
== Tsarray
&& t2
->ty
== Tsarray
&&
1625 e2
->implicitConvTo(t1
->nextOf()->arrayOf()))
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()))
1636 t
= t2
->nextOf()->arrayOf();
1637 e1
= e1
->castTo(sc
, t
);
1638 e2
= e2
->castTo(sc
, t
);
1640 else if (t1
->isintegral() && t2
->isintegral())
1647 incompatibleTypes();
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());
1663 e2
= e2
->castTo(sc
, t1
);
1668 e1
= e1
->castTo(sc
, t2
);
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
)
1686 error("void has no value");
1697 e
= e
->castTo(sc
, Type::tint32
);
1701 e
= e
->castTo(sc
, Type::tuns32
);