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());
372 if (this->type
->equals(t
))
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()))
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
||
390 return committed
? MATCHconvert
: MATCHexact
;
392 return Expression::implicitConvTo(t
);
395 MATCH
StructLiteralExp::implicitConvTo(Type
*t
)
398 printf("StructLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
399 toChars(), type
->toChars(), t
->toChars());
401 MATCH m
= Expression::implicitConvTo(t
);
402 if (m
!= MATCHnomatch
)
404 if (type
->ty
== t
->ty
&& type
->ty
== Tstruct
&&
405 ((TypeStruct
*)type
)->sym
== ((TypeStruct
*)t
)->sym
)
408 for (int i
= 0; i
< elements
->dim
; i
++)
409 { Expression
*e
= (Expression
*)elements
->data
[i
];
412 te
= te
->mutableOf();
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);
426 MATCH
StringExp::implicitConvTo(Type
*t
)
430 printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
431 toChars(), committed
, type
->toChars(), t
->toChars());
435 if (!committed
&& t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tvoid
)
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
)
449 if (type
->ty
== Tsarray
)
451 if (((TypeSArray
*)type
)->dim
->toInteger() !=
452 ((TypeSArray
*)t
)->dim
->toInteger())
454 TY tynto
= t
->nextOf()->ty
;
455 if (tynto
== Tchar
|| tynto
== Twchar
|| tynto
== Tdchar
)
462 if (type
->nextOf()->mod
!= tn
->mod
)
463 { if (!tn
->isConst())
479 return Expression::implicitConvTo(t
);
481 m
= (MATCH
)type
->implicitConvTo(t
);
491 MATCH
ArrayLiteralExp::implicitConvTo(Type
*t
)
492 { MATCH result
= MATCHexact
;
495 printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
496 toChars(), type
->toChars(), t
->toChars());
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());
513 result
= m
; // remember worst match
514 if (result
== MATCHnomatch
)
515 break; // no need to check for worse
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
);
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());
540 result
= m
; // remember worst match
541 if (result
== MATCHnomatch
)
542 break; // no need to check for worse
547 return Expression::implicitConvTo(t
);
550 MATCH
AddrExp::implicitConvTo(Type
*t
)
553 printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
554 toChars(), type
->toChars(), t
->toChars());
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.
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();
575 if (f2
->overloadExactMatch(t
->nextOf()))
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
);
588 if (type
->ty
== Tpointer
&& type
->nextOf()->ty
== Tfunction
&&
589 t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tfunction
&&
592 /* I don't think this can ever happen -
593 * it should have been
594 * converted to a SymOffExp.
597 VarExp
*ve
= (VarExp
*)e1
;
598 FuncDeclaration
*f
= ve
->var
->isFuncDeclaration();
599 if (f
&& f
->overloadExactMatch(t
->nextOf()))
603 //printf("\tresult = %d\n", result);
607 MATCH
SymOffExp::implicitConvTo(Type
*t
)
610 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
611 toChars(), type
->toChars(), t
->toChars());
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.
624 if (type
->ty
== Tpointer
&& type
->nextOf()->ty
== Tfunction
&&
625 (t
->ty
== Tpointer
|| t
->ty
== Tdelegate
) && t
->nextOf()->ty
== Tfunction
)
627 f
= var
->isFuncDeclaration();
629 { f
= f
->overloadExactMatch(t
->nextOf());
631 { if ((t
->ty
== Tdelegate
&& (f
->needThis() || f
->isNested())) ||
632 (t
->ty
== Tpointer
&& !(f
->needThis() || f
->isNested())))
640 //printf("\tresult = %d\n", result);
644 MATCH
DelegateExp::implicitConvTo(Type
*t
)
647 printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
648 toChars(), type
->toChars(), t
->toChars());
652 result
= type
->implicitConvTo(t
);
654 if (result
== MATCHnomatch
)
656 // Look for pointers to functions where the functions are overloaded.
660 if (type
->ty
== Tdelegate
&& type
->nextOf()->ty
== Tfunction
&&
661 t
->ty
== Tdelegate
&& t
->nextOf()->ty
== Tfunction
)
663 if (func
&& func
->overloadExactMatch(t
->nextOf()))
670 MATCH
CondExp::implicitConvTo(Type
*t
)
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());
693 printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
694 toChars(), type
->toChars(), t
->toChars());
698 Expression
*e
= this;
699 Type
*tb
= t
->toBasetype();
700 Type
*typeb
= type
->toBasetype();
703 // Do (type *) cast of (type [dim])
704 if (tb
->ty
== Tpointer
&&
708 //printf("Converting [dim] to *\n");
710 if (typeb
->size(loc
) == 0)
711 e
= new NullExp(loc
);
713 e
= new AddrExp(loc
, e
);
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());
725 e
= new CastExp(loc
, e
, tb
);
730 e
= e
->copy(); // because of COW for assignment to e->type
734 //printf("Returning: %s\n", e->toChars());
739 Expression
*RealExp::castTo(Scope
*sc
, Type
*t
)
740 { Expression
*e
= this;
743 if ((type
->isreal() && t
->isreal()) ||
744 (type
->isimaginary() && t
->isimaginary())
750 e
= Expression::castTo(sc
, t
);
756 Expression
*ComplexExp::castTo(Scope
*sc
, Type
*t
)
757 { Expression
*e
= this;
760 if (type
->iscomplex() && t
->iscomplex())
765 e
= Expression::castTo(sc
, t
);
771 Expression
*NullExp::castTo(Scope
*sc
, Type
*t
)
775 //printf("NullExp::castTo(t = %p)\n", t);
781 e
= (NullExp
*)copy();
783 tb
= t
->toBasetype();
784 e
->type
= type
->toBasetype();
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
))
793 if (tb
->ty
== Tdelegate
)
794 { TypeDelegate
*td
= (TypeDelegate
*)tb
;
795 TypeFunction
*tf
= (TypeFunction
*)td
->nextOf();
798 !(tf
->arguments
&& tf
->arguments
->dim
)
801 return Expression::castTo(sc
, t
);
808 return e
->Expression::castTo(sc
, t
);
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.
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*");
834 { se
= (StringExp
*)copy();
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();
853 { se
= (StringExp
*)copy();
860 if (tb
->ty
!= Tsarray
&& tb
->ty
!= Tarray
&& tb
->ty
!= Tpointer
)
862 { se
= (StringExp
*)copy();
867 if (typeb
->ty
!= Tsarray
&& typeb
->ty
!= Tarray
&& typeb
->ty
!= Tpointer
)
869 { se
= (StringExp
*)copy();
875 if (typeb
->nextOf()->size() == tb
->nextOf()->size())
878 { se
= (StringExp
*)copy();
881 if (tb
->ty
== Tsarray
)
882 goto L2
; // handle possible change in static array dimension
890 #define X(tf,tt) ((tf) * 256 + (tt))
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
):
903 case X(Tchar
, Twchar
):
904 for (size_t u
= 0; u
< len
;)
906 char *p
= utf_decodeChar((unsigned char *)se
->string
, len
, &u
, &c
);
910 buffer
.writeUTF16(c
);
912 newlen
= buffer
.offset
/ 2;
913 buffer
.writeUTF16(0);
916 case X(Tchar
, Tdchar
):
917 for (size_t u
= 0; u
< len
;)
919 char *p
= utf_decodeChar((unsigned char *)se
->string
, len
, &u
, &c
);
928 case X(Twchar
,Tchar
):
929 for (size_t u
= 0; u
< len
;)
931 char *p
= utf_decodeWchar((unsigned short *)se
->string
, len
, &u
, &c
);
937 newlen
= buffer
.offset
;
941 case X(Twchar
,Tdchar
):
942 for (size_t u
= 0; u
< len
;)
944 char *p
= utf_decodeWchar((unsigned short *)se
->string
, len
, &u
, &c
);
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
);
963 newlen
= buffer
.offset
;
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
);
974 buffer
.writeUTF16(c
);
977 newlen
= buffer
.offset
/ 2;
978 buffer
.writeUTF16(0);
983 { se
= (StringExp
*)copy();
986 se
->string
= buffer
.extractData();
988 se
->sz
= tb
->nextOf()->size();
992 assert(typeb
->nextOf()->size() != tb
->nextOf()->size());
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
;
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
);
1028 Expression
*e
= new CastExp(loc
, se
, t
);
1029 e
->type
= t
; // so semantic() won't be run on e
1033 Expression
*AddrExp::castTo(Scope
*sc
, Type
*t
)
1038 printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
1039 toChars(), type
->toChars(), t
->toChars());
1041 Expression
*e
= this;
1043 tb
= t
->toBasetype();
1044 type
= type
->toBasetype();
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();
1057 if (f2
->overloadExactMatch(t
->nextOf()))
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
);
1068 { f
->tookAddressOf
= 1;
1069 SymOffExp
*se
= new SymOffExp(loc
, f
, 0, 0);
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
&&
1081 VarExp
*ve
= (VarExp
*)e1
;
1082 FuncDeclaration
*f
= ve
->var
->isFuncDeclaration();
1085 assert(0); // should be SymOffExp instead
1086 f
= f
->overloadExactMatch(tb
->nextOf());
1089 e
= new VarExp(loc
, f
);
1091 e
= new AddrExp(loc
, e
);
1097 e
= Expression::castTo(sc
, t
);
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
;
1116 Expression
*ArrayLiteralExp::castTo(Scope
*sc
, Type
*t
)
1119 printf("ArrayLiteralExp::castTo(this=%s, type=%s, => %s)\n",
1120 toChars(), type
->toChars(), t
->toChars());
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())
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
;
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
)
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
;
1184 return e
->Expression::castTo(sc
, t
);
1187 Expression
*SymOffExp::castTo(Scope
*sc
, Type
*t
)
1190 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
1191 toChars(), type
->toChars(), t
->toChars());
1193 if (type
== t
&& hasOverloads
== 0)
1196 Type
*tb
= t
->toBasetype();
1197 Type
*typeb
= type
->toBasetype();
1200 // Look for pointers to functions where the functions are overloaded.
1204 typeb
->ty
== Tpointer
&& typeb
->nextOf()->ty
== Tfunction
&&
1205 (tb
->ty
== Tpointer
|| tb
->ty
== Tdelegate
) && tb
->nextOf()->ty
== Tfunction
)
1207 f
= var
->isFuncDeclaration();
1210 f
= f
->overloadExactMatch(tb
->nextOf());
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
);
1225 e
= new SymOffExp(loc
, f
, 0);
1228 f
->tookAddressOf
= 1;
1233 e
= Expression::castTo(sc
, t
);
1238 ((SymOffExp
*)e
)->hasOverloads
= 0;
1243 Expression
*DelegateExp::castTo(Scope
*sc
, Type
*t
)
1246 printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
1247 toChars(), type
->toChars(), t
->toChars());
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();
1256 // Look for delegates to functions where the functions are overloaded.
1259 if (typeb
->ty
== Tdelegate
&& typeb
->nextOf()->ty
== Tfunction
&&
1260 tb
->ty
== Tdelegate
&& tb
->nextOf()->ty
== Tfunction
)
1264 f
= func
->overloadExactMatch(tb
->nextOf());
1266 { target_ptrdiff_t offset
;
1267 if (f
->tintro
&& f
->tintro
->nextOf()->isBaseOf(f
->type
->nextOf(), &offset
) && offset
)
1269 f
->tookAddressOf
= 1;
1270 e
= new DelegateExp(loc
, e1
, f
);
1278 e
= Expression::castTo(sc
, t
);
1281 { target_ptrdiff_t offset
;
1283 func
->tookAddressOf
= 1;
1284 if (func
->tintro
&& func
->tintro
->nextOf()->isBaseOf(func
->type
->nextOf(), &offset
) && offset
)
1292 Expression
*CondExp::castTo(Scope
*sc
, Type
*t
)
1294 Expression
*e
= this;
1298 if (1 || e1
->op
== TOKstring
|| e2
->op
== TOKstring
)
1299 { e
= new CondExp(loc
, econd
, e1
->castTo(sc
, t
), e2
->castTo(sc
, t
));
1303 e
= Expression::castTo(sc
, t
);
1308 /* ==================== ====================== */
1310 /****************************************
1311 * Scale addition/subtraction to/from pointer.
1314 Expression
*BinExp::scaleFactor(Scope
*sc
)
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
));
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
;
1337 stride
= t2b
->nextOf()->size(loc
);
1338 if (!t
->equals(t1b
))
1339 e
= e1
->castTo(sc
, t
);
1342 e
= new MulExp(loc
, e
, new IntegerExp(0, stride
, t
));
1351 /************************************
1352 * Bring leaves to common type.
1355 Expression
*BinExp::typeCombine(Scope
*sc
)
1362 //printf("BinExp::typeCombine() %s\n", toChars());
1365 e1
= e1
->integralPromotions(sc
);
1366 e2
= e2
->integralPromotions(sc
);
1368 // BUG: do toBasetype()
1374 //if (t1) printf("\tt1 = %s\n", t1->toChars());
1375 //if (t2) printf("\tt2 = %s\n", t2->toChars());
1377 if (!t2
) printf("\te2 = '%s'\n", e2
->toChars());
1381 Type
*t1b
= t1
->toBasetype();
1382 Type
*t2b
= t2
->toBasetype();
1384 ty
= (TY
)Type::impcnvResult
[t1b
->ty
][t2b
->ty
];
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
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
);
1417 if (type
!= Type::basic
[ty
])
1419 type
= Type::basic
[ty
];
1420 return castTo(sc
, t
);
1423 //printf("after typeCombine():\n");
1425 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
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
)
1447 e1
= e1
->castTo(sc
, t1
);
1448 if (e2
->op
== TOKstring
&& !((StringExp
*)e2
)->committed
)
1451 e2
= e2
->castTo(sc
, t2
);
1460 if ((t1
->ty
== Tstruct
|| t1
->ty
== Tclass
) &&
1461 (op
== TOKmin
|| op
== TOKadd
))
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();
1472 else if (t1n
->ty
== Tvoid
) // pointers to void are always compatible
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();
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
))
1491 e2
= e2
->castTo(sc
, t
);
1493 else if (cd2
->isBaseOf(cd1
, &offset
))
1497 e1
= e1
->castTo(sc
, t
);
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*)
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])
1519 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) && t1
->implicitConvTo(t2
))
1523 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) && t2
->implicitConvTo(t1
))
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();
1538 t1
= t1
->nextOf()->mutableOf()->constOf()->arrayOf();
1540 if (t2
->ty
== Tpointer
)
1541 t2
= t2
->nextOf()->mutableOf()->constOf()->pointerTo();
1543 t2
= t2
->nextOf()->mutableOf()->constOf()->arrayOf();
1547 else if (t1
->ty
== Tclass
|| t2
->ty
== Tclass
)
1551 int i1
= e2
->implicitConvTo(t1
);
1552 int i2
= e1
->implicitConvTo(t2
);
1556 // We have the case of class vs. void*, so pick class
1557 if (t1
->ty
== Tpointer
)
1559 else if (t2
->ty
== Tpointer
)
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
;
1595 else if (t1
->ty
== Tstruct
&& t2
->ty
== Tstruct
)
1597 if (((TypeStruct
*)t1
)->sym
!= ((TypeStruct
*)t2
)->sym
)
1600 else if ((e1
->op
== TOKstring
|| e1
->op
== TOKnull
) && e1
->implicitConvTo(t2
))
1604 else if ((e2
->op
== TOKstring
|| e2
->op
== TOKnull
) && e2
->implicitConvTo(t1
))
1608 else if (t1
->ty
== Tsarray
&& t2
->ty
== Tsarray
&&
1609 e2
->implicitConvTo(t1
->nextOf()->arrayOf()))
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()))
1620 t
= t2
->nextOf()->arrayOf();
1621 e1
= e1
->castTo(sc
, t
);
1622 e2
= e2
->castTo(sc
, t
);
1624 else if (t1
->isintegral() && t2
->isintegral())
1631 incompatibleTypes();
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());
1647 e2
= e2
->castTo(sc
, t1
);
1652 e1
= e1
->castTo(sc
, t2
);
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
)
1670 error("void has no value");
1681 e
= e
->castTo(sc
, Type::tint32
);
1685 e
= e
->castTo(sc
, Type::tuns32
);