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 // 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
;
388 if (t
->ty
== Tarray
||
389 t
->ty
== Taarray
|| t
->ty
== Tmaybe
||
391 return committed
? MATCHconvert
: MATCHexact
;
394 /* Null can only be implicitly converted to a maybe type */
400 return Expression::implicitConvTo(t
);
403 MATCH
StructLiteralExp::implicitConvTo(Type
*t
)
406 printf("StructLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
407 toChars(), type
->toChars(), t
->toChars());
409 MATCH m
= Expression::implicitConvTo(t
);
410 if (m
!= MATCHnomatch
)
412 if (type
->ty
== t
->ty
&& type
->ty
== Tstruct
&&
413 ((TypeStruct
*)type
)->sym
== ((TypeStruct
*)t
)->sym
)
416 for (int i
= 0; i
< elements
->dim
; i
++)
417 { Expression
*e
= (Expression
*)elements
->data
[i
];
420 te
= te
->mutableOf();
422 { assert(t
->mod
== MODinvariant
);
423 te
= te
->invariantOf();
425 MATCH m2
= e
->implicitConvTo(te
);
426 //printf("\t%s => %s, match = %d\n", e->toChars(), te->toChars(), m2);
434 MATCH
StringExp::implicitConvTo(Type
*t
)
438 printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
439 toChars(), committed
, type
->toChars(), t
->toChars());
443 if (!committed
&& t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tvoid
)
447 if (type
->ty
== Tsarray
|| type
->ty
== Tarray
|| type
->ty
== Tpointer
)
449 TY tyn
= type
->nextOf()->ty
;
450 if (tyn
== Tchar
|| tyn
== Twchar
|| tyn
== Tdchar
)
457 if (type
->ty
== Tsarray
)
459 if (((TypeSArray
*)type
)->dim
->toInteger() !=
460 ((TypeSArray
*)t
)->dim
->toInteger())
462 TY tynto
= t
->nextOf()->ty
;
463 if (tynto
== Tchar
|| tynto
== Twchar
|| tynto
== Tdchar
)
470 if (type
->nextOf()->mod
!= tn
->mod
)
471 { if (!tn
->isConst())
487 return Expression::implicitConvTo(t
);
489 m
= (MATCH
)type
->implicitConvTo(t
);
499 MATCH
ArrayLiteralExp::implicitConvTo(Type
*t
)
500 { MATCH result
= MATCHexact
;
503 printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
504 toChars(), type
->toChars(), t
->toChars());
506 Type
*typeb
= type
->toBasetype();
507 Type
*tb
= t
->toBasetype();
508 if ((tb
->ty
== Tarray
|| tb
->ty
== Tsarray
) &&
509 (typeb
->ty
== Tarray
|| typeb
->ty
== Tsarray
))
511 if (tb
->ty
== Tsarray
)
512 { TypeSArray
*tsa
= (TypeSArray
*)tb
;
513 if (elements
->dim
!= tsa
->dim
->toInteger())
514 result
= MATCHnomatch
;
517 for (int i
= 0; i
< elements
->dim
; i
++)
518 { Expression
*e
= (Expression
*)elements
->data
[i
];
519 MATCH m
= (MATCH
)e
->implicitConvTo(tb
->nextOf());
521 result
= m
; // remember worst match
522 if (result
== MATCHnomatch
)
523 break; // no need to check for worse
528 return Expression::implicitConvTo(t
);
531 MATCH
AssocArrayLiteralExp::implicitConvTo(Type
*t
)
532 { MATCH result
= MATCHexact
;
534 Type
*typeb
= type
->toBasetype();
535 Type
*tb
= t
->toBasetype();
536 if (tb
->ty
== Taarray
&& typeb
->ty
== Taarray
)
538 for (size_t i
= 0; i
< keys
->dim
; i
++)
539 { Expression
*e
= (Expression
*)keys
->data
[i
];
540 MATCH m
= (MATCH
)e
->implicitConvTo(((TypeAArray
*)tb
)->index
);
542 result
= m
; // remember worst match
543 if (result
== MATCHnomatch
)
544 break; // no need to check for worse
545 e
= (Expression
*)values
->data
[i
];
546 m
= (MATCH
)e
->implicitConvTo(tb
->nextOf());
548 result
= m
; // remember worst match
549 if (result
== MATCHnomatch
)
550 break; // no need to check for worse
555 return Expression::implicitConvTo(t
);
558 MATCH
AddrExp::implicitConvTo(Type
*t
)
561 printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
562 toChars(), type
->toChars(), t
->toChars());
566 result
= type
->implicitConvTo(t
);
567 //printf("\tresult = %d\n", result);
569 if (result
== MATCHnomatch
)
571 // Look for pointers to functions where the functions are overloaded.
575 if (e1
->op
== TOKoverloadset
&&
576 (t
->ty
== Tpointer
|| t
->ty
== Tdelegate
) && t
->nextOf()->ty
== Tfunction
)
577 { OverExp
*eo
= (OverExp
*)e1
;
578 FuncDeclaration
*f
= NULL
;
579 for (int i
= 0; i
< eo
->vars
->a
.dim
; i
++)
580 { Dsymbol
*s
= (Dsymbol
*)eo
->vars
->a
.data
[i
];
581 FuncDeclaration
*f2
= s
->isFuncDeclaration();
583 if (f2
->overloadExactMatch(t
->nextOf()))
585 /* Error if match in more than one overload set,
586 * even if one is a 'better' match than the other.
588 ScopeDsymbol::multiplyDefined(loc
, f
, f2
);
596 if (type
->ty
== Tpointer
&& type
->nextOf()->ty
== Tfunction
&&
597 t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tfunction
&&
600 /* I don't think this can ever happen -
601 * it should have been
602 * converted to a SymOffExp.
605 VarExp
*ve
= (VarExp
*)e1
;
606 FuncDeclaration
*f
= ve
->var
->isFuncDeclaration();
607 if (f
&& f
->overloadExactMatch(t
->nextOf()))
611 //printf("\tresult = %d\n", result);
615 MATCH
SymOffExp::implicitConvTo(Type
*t
)
618 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
619 toChars(), type
->toChars(), t
->toChars());
623 result
= type
->implicitConvTo(t
);
624 //printf("\tresult = %d\n", result);
626 if (result
== MATCHnomatch
)
628 // Look for pointers to functions where the functions are overloaded.
632 if (type
->ty
== Tpointer
&& type
->nextOf()->ty
== Tfunction
&&
633 (t
->ty
== Tpointer
|| t
->ty
== Tdelegate
) && t
->nextOf()->ty
== Tfunction
)
635 f
= var
->isFuncDeclaration();
637 { f
= f
->overloadExactMatch(t
->nextOf());
639 { if ((t
->ty
== Tdelegate
&& (f
->needThis() || f
->isNested())) ||
640 (t
->ty
== Tpointer
&& !(f
->needThis() || f
->isNested())))
648 //printf("\tresult = %d\n", result);
652 MATCH
DelegateExp::implicitConvTo(Type
*t
)
655 printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
656 toChars(), type
->toChars(), t
->toChars());
660 result
= type
->implicitConvTo(t
);
662 if (result
== MATCHnomatch
)
664 // Look for pointers to functions where the functions are overloaded.
668 if (type
->ty
== Tdelegate
&& type
->nextOf()->ty
== Tfunction
&&
669 t
->ty
== Tdelegate
&& t
->nextOf()->ty
== Tfunction
)
671 if (func
&& func
->overloadExactMatch(t
->nextOf()))
678 MATCH
CondExp::implicitConvTo(Type
*t
)
683 m1
= e1
->implicitConvTo(t
);
684 m2
= e2
->implicitConvTo(t
);
686 // Pick the worst match
687 return (m1
< m2
) ? m1
: m2
;
691 /* ==================== castTo ====================== */
693 /**************************************
694 * Do an explicit cast.
697 Expression
*Expression::castTo(Scope
*sc
, Type
*t
)
699 //printf("Expression::castTo(this=%s, t=%s)\n", toChars(), t->toChars());
701 printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
702 toChars(), type
->toChars(), t
->toChars());
706 Expression
*e
= this;
707 Type
*tb
= t
->toBasetype();
708 Type
*typeb
= type
->toBasetype();
711 // Do (type *) cast of (type [dim])
712 if (tb
->ty
== Tpointer
&&
716 //printf("Converting [dim] to *\n");
718 if (typeb
->size(loc
) == 0)
719 e
= new NullExp(loc
);
721 e
= new AddrExp(loc
, e
);
724 else if (tb
->ty
== Tdelegate
&& type
->ty
!= Tdelegate
)
726 TypeDelegate
*td
= (TypeDelegate
*)tb
;
727 TypeFunction
*tf
= (TypeFunction
*)td
->nextOf();
728 return toDelegate(sc
, tf
->nextOf());
733 e
= new CastExp(loc
, e
, tb
);
738 e
= e
->copy(); // because of COW for assignment to e->type
742 //printf("Returning: %s\n", e->toChars());
747 Expression
*RealExp::castTo(Scope
*sc
, Type
*t
)
748 { Expression
*e
= this;
751 if ((type
->isreal() && t
->isreal()) ||
752 (type
->isimaginary() && t
->isimaginary())
758 e
= Expression::castTo(sc
, t
);
764 Expression
*ComplexExp::castTo(Scope
*sc
, Type
*t
)
765 { Expression
*e
= this;
768 if (type
->iscomplex() && t
->iscomplex())
773 e
= Expression::castTo(sc
, t
);
779 Expression
*NullExp::castTo(Scope
*sc
, Type
*t
)
783 //printf("NullExp::castTo(t = %p)\n", t);
789 e
= (NullExp
*)copy();
791 tb
= t
->toBasetype();
792 e
->type
= type
->toBasetype();
795 // NULL implicitly converts to any pointer type or dynamic array
796 if (e
->type
->ty
== Tpointer
&& e
->type
->nextOf()->ty
== Tvoid
&&
797 (tb
->ty
== Tpointer
|| tb
->ty
== Tarray
|| tb
->ty
== Taarray
||
798 tb
->ty
== Tdelegate
))
801 if (tb
->ty
== Tdelegate
)
802 { TypeDelegate
*td
= (TypeDelegate
*)tb
;
803 TypeFunction
*tf
= (TypeFunction
*)td
->nextOf();
806 !(tf
->arguments
&& tf
->arguments
->dim
)
809 return Expression::castTo(sc
, t
);
816 return e
->Expression::castTo(sc
, t
);
823 Expression
*StringExp::castTo(Scope
*sc
, Type
*t
)
825 /* This follows copy-on-write; any changes to 'this'
826 * will result in a copy.
827 * The this->string member is considered immutable.
833 //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed);
835 if (!committed
&& t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tvoid
)
837 error("cannot convert string literal to void*");
842 { se
= (StringExp
*)copy();
852 tb
= t
->toBasetype();
853 //printf("\ttype = %s\n", type->toChars());
854 if (tb
->ty
== Tdelegate
&& type
->toBasetype()->ty
!= Tdelegate
)
855 return Expression::castTo(sc
, t
);
857 Type
*typeb
= type
->toBasetype();
861 { se
= (StringExp
*)copy();
868 if (tb
->ty
!= Tsarray
&& tb
->ty
!= Tarray
&& tb
->ty
!= Tpointer
)
870 { se
= (StringExp
*)copy();
875 if (typeb
->ty
!= Tsarray
&& typeb
->ty
!= Tarray
&& typeb
->ty
!= Tpointer
)
877 { se
= (StringExp
*)copy();
883 if (typeb
->nextOf()->size() == tb
->nextOf()->size())
886 { se
= (StringExp
*)copy();
889 if (tb
->ty
== Tsarray
)
890 goto L2
; // handle possible change in static array dimension
898 #define X(tf,tt) ((tf) * 256 + (tt))
902 int tfty
= typeb
->nextOf()->toBasetype()->ty
;
903 int ttty
= tb
->nextOf()->toBasetype()->ty
;
904 switch (X(tfty
, ttty
))
906 case X(Tchar
, Tchar
):
907 case X(Twchar
,Twchar
):
908 case X(Tdchar
,Tdchar
):
911 case X(Tchar
, Twchar
):
912 for (size_t u
= 0; u
< len
;)
914 char *p
= utf_decodeChar((unsigned char *)se
->string
, len
, &u
, &c
);
918 buffer
.writeUTF16(c
);
920 newlen
= buffer
.offset
/ 2;
921 buffer
.writeUTF16(0);
924 case X(Tchar
, Tdchar
):
925 for (size_t u
= 0; u
< len
;)
927 char *p
= utf_decodeChar((unsigned char *)se
->string
, len
, &u
, &c
);
936 case X(Twchar
,Tchar
):
937 for (size_t u
= 0; u
< len
;)
939 char *p
= utf_decodeWchar((unsigned short *)se
->string
, len
, &u
, &c
);
945 newlen
= buffer
.offset
;
949 case X(Twchar
,Tdchar
):
950 for (size_t u
= 0; u
< len
;)
952 char *p
= utf_decodeWchar((unsigned short *)se
->string
, len
, &u
, &c
);
961 case X(Tdchar
,Tchar
):
962 for (size_t u
= 0; u
< len
; u
++)
964 unsigned c
= ((unsigned *)se
->string
)[u
];
965 if (!utf_isValidDchar(c
))
966 error("invalid UCS-32 char \\U%08x", c
);
971 newlen
= buffer
.offset
;
975 case X(Tdchar
,Twchar
):
976 for (size_t u
= 0; u
< len
; u
++)
978 unsigned c
= ((unsigned *)se
->string
)[u
];
979 if (!utf_isValidDchar(c
))
980 error("invalid UCS-32 char \\U%08x", c
);
982 buffer
.writeUTF16(c
);
985 newlen
= buffer
.offset
/ 2;
986 buffer
.writeUTF16(0);
991 { se
= (StringExp
*)copy();
994 se
->string
= buffer
.extractData();
996 se
->sz
= tb
->nextOf()->size();
1000 assert(typeb
->nextOf()->size() != tb
->nextOf()->size());
1008 // See if need to truncate or extend the literal
1009 if (tb
->ty
== Tsarray
)
1011 int dim2
= ((TypeSArray
*)tb
)->dim
->toInteger();
1013 //printf("dim from = %d, to = %d\n", se->len, dim2);
1015 // Changing dimensions
1016 if (dim2
!= se
->len
)
1018 // Copy when changing the string literal
1019 unsigned newsz
= se
->sz
;
1023 d
= (dim2
< se
->len
) ? dim2
: se
->len
;
1024 s
= (unsigned char *)mem
.malloc((dim2
+ 1) * newsz
);
1025 memcpy(s
, se
->string
, d
* newsz
);
1026 // Extend with 0, add terminating 0
1027 memset((char *)s
+ d
* newsz
, 0, (dim2
+ 1 - d
) * newsz
);
1036 Expression
*e
= new CastExp(loc
, se
, t
);
1037 e
->type
= t
; // so semantic() won't be run on e
1041 Expression
*AddrExp::castTo(Scope
*sc
, Type
*t
)
1046 printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
1047 toChars(), type
->toChars(), t
->toChars());
1049 Expression
*e
= this;
1051 tb
= t
->toBasetype();
1052 type
= type
->toBasetype();
1055 // Look for pointers to functions where the functions are overloaded.
1057 if (e1
->op
== TOKoverloadset
&&
1058 (t
->ty
== Tpointer
|| t
->ty
== Tdelegate
) && t
->nextOf()->ty
== Tfunction
)
1059 { OverExp
*eo
= (OverExp
*)e1
;
1060 FuncDeclaration
*f
= NULL
;
1061 for (int i
= 0; i
< eo
->vars
->a
.dim
; i
++)
1062 { Dsymbol
*s
= (Dsymbol
*)eo
->vars
->a
.data
[i
];
1063 FuncDeclaration
*f2
= s
->isFuncDeclaration();
1065 if (f2
->overloadExactMatch(t
->nextOf()))
1067 /* Error if match in more than one overload set,
1068 * even if one is a 'better' match than the other.
1070 ScopeDsymbol::multiplyDefined(loc
, f
, f2
);
1076 { f
->tookAddressOf
= 1;
1077 SymOffExp
*se
= new SymOffExp(loc
, f
, 0, 0);
1079 // Let SymOffExp::castTo() do the heavy lifting
1080 return se
->castTo(sc
, t
);
1085 if (type
->ty
== Tpointer
&& type
->nextOf()->ty
== Tfunction
&&
1086 tb
->ty
== Tpointer
&& tb
->nextOf()->ty
== Tfunction
&&
1089 VarExp
*ve
= (VarExp
*)e1
;
1090 FuncDeclaration
*f
= ve
->var
->isFuncDeclaration();
1093 assert(0); // should be SymOffExp instead
1094 f
= f
->overloadExactMatch(tb
->nextOf());
1097 e
= new VarExp(loc
, f
);
1099 e
= new AddrExp(loc
, e
);
1105 e
= Expression::castTo(sc
, t
);
1112 Expression
*TupleExp::castTo(Scope
*sc
, Type
*t
)
1113 { TupleExp
*e
= (TupleExp
*)copy();
1114 e
->exps
= (Expressions
*)exps
->copy();
1115 for (size_t i
= 0; i
< e
->exps
->dim
; i
++)
1116 { Expression
*ex
= (Expression
*)e
->exps
->data
[i
];
1117 ex
= ex
->castTo(sc
, t
);
1118 e
->exps
->data
[i
] = (void *)ex
;
1124 Expression
*ArrayLiteralExp::castTo(Scope
*sc
, Type
*t
)
1127 printf("ArrayLiteralExp::castTo(this=%s, type=%s, => %s)\n",
1128 toChars(), type
->toChars(), t
->toChars());
1132 ArrayLiteralExp
*e
= this;
1133 Type
*typeb
= type
->toBasetype();
1134 Type
*tb
= t
->toBasetype();
1135 if ((tb
->ty
== Tarray
|| tb
->ty
== Tsarray
) &&
1136 (typeb
->ty
== Tarray
|| typeb
->ty
== Tsarray
) &&
1137 // Not trying to convert non-void[] to void[]
1138 !(tb
->nextOf()->toBasetype()->ty
== Tvoid
&& typeb
->nextOf()->toBasetype()->ty
!= Tvoid
))
1140 if (tb
->ty
== Tsarray
)
1141 { TypeSArray
*tsa
= (TypeSArray
*)tb
;
1142 if (elements
->dim
!= tsa
->dim
->toInteger())
1146 e
= (ArrayLiteralExp
*)copy();
1147 e
->elements
= (Expressions
*)elements
->copy();
1148 for (int i
= 0; i
< elements
->dim
; i
++)
1149 { Expression
*ex
= (Expression
*)elements
->data
[i
];
1150 ex
= ex
->castTo(sc
, tb
->nextOf());
1151 e
->elements
->data
[i
] = (void *)ex
;
1156 if (tb
->ty
== Tpointer
&& typeb
->ty
== Tsarray
)
1158 e
= (ArrayLiteralExp
*)copy();
1159 e
->type
= typeb
->nextOf()->pointerTo();
1162 return e
->Expression::castTo(sc
, t
);
1165 Expression
*AssocArrayLiteralExp::castTo(Scope
*sc
, Type
*t
)
1169 AssocArrayLiteralExp
*e
= this;
1170 Type
*typeb
= type
->toBasetype();
1171 Type
*tb
= t
->toBasetype();
1172 if (tb
->ty
== Taarray
&& typeb
->ty
== Taarray
&&
1173 tb
->nextOf()->toBasetype()->ty
!= Tvoid
)
1175 e
= (AssocArrayLiteralExp
*)copy();
1176 e
->keys
= (Expressions
*)keys
->copy();
1177 e
->values
= (Expressions
*)values
->copy();
1178 assert(keys
->dim
== values
->dim
);
1179 for (size_t i
= 0; i
< keys
->dim
; i
++)
1180 { Expression
*ex
= (Expression
*)values
->data
[i
];
1181 ex
= ex
->castTo(sc
, tb
->nextOf());
1182 e
->values
->data
[i
] = (void *)ex
;
1184 ex
= (Expression
*)keys
->data
[i
];
1185 ex
= ex
->castTo(sc
, ((TypeAArray
*)tb
)->index
);
1186 e
->keys
->data
[i
] = (void *)ex
;
1192 return e
->Expression::castTo(sc
, t
);
1195 Expression
*SymOffExp::castTo(Scope
*sc
, Type
*t
)
1198 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
1199 toChars(), type
->toChars(), t
->toChars());
1201 if (type
== t
&& hasOverloads
== 0)
1204 Type
*tb
= t
->toBasetype();
1205 Type
*typeb
= type
->toBasetype();
1208 // Look for pointers to functions where the functions are overloaded.
1212 typeb
->ty
== Tpointer
&& typeb
->nextOf()->ty
== Tfunction
&&
1213 (tb
->ty
== Tpointer
|| tb
->ty
== Tdelegate
) && tb
->nextOf()->ty
== Tfunction
)
1215 f
= var
->isFuncDeclaration();
1218 f
= f
->overloadExactMatch(tb
->nextOf());
1221 if (tb
->ty
== Tdelegate
&& f
->needThis() && hasThis(sc
))
1223 e
= new DelegateExp(loc
, new ThisExp(loc
), f
);
1224 e
= e
->semantic(sc
);
1226 else if (tb
->ty
== Tdelegate
&& f
->isNested())
1228 e
= new DelegateExp(loc
, new IntegerExp(0), f
);
1229 e
= e
->semantic(sc
);
1233 e
= new SymOffExp(loc
, f
, 0);
1236 f
->tookAddressOf
= 1;
1241 e
= Expression::castTo(sc
, t
);
1246 ((SymOffExp
*)e
)->hasOverloads
= 0;
1251 Expression
*DelegateExp::castTo(Scope
*sc
, Type
*t
)
1254 printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
1255 toChars(), type
->toChars(), t
->toChars());
1257 static char msg
[] = "cannot form delegate due to covariant return type";
1259 Expression
*e
= this;
1260 Type
*tb
= t
->toBasetype();
1261 Type
*typeb
= type
->toBasetype();
1264 // Look for delegates to functions where the functions are overloaded.
1267 if (typeb
->ty
== Tdelegate
&& typeb
->nextOf()->ty
== Tfunction
&&
1268 tb
->ty
== Tdelegate
&& tb
->nextOf()->ty
== Tfunction
)
1272 f
= func
->overloadExactMatch(tb
->nextOf());
1274 { target_ptrdiff_t offset
;
1275 if (f
->tintro
&& f
->tintro
->nextOf()->isBaseOf(f
->type
->nextOf(), &offset
) && offset
)
1277 f
->tookAddressOf
= 1;
1278 e
= new DelegateExp(loc
, e1
, f
);
1286 e
= Expression::castTo(sc
, t
);
1289 { target_ptrdiff_t offset
;
1291 func
->tookAddressOf
= 1;
1292 if (func
->tintro
&& func
->tintro
->nextOf()->isBaseOf(func
->type
->nextOf(), &offset
) && offset
)
1300 Expression
*CondExp::castTo(Scope
*sc
, Type
*t
)
1302 Expression
*e
= this;
1306 if (1 || e1
->op
== TOKstring
|| e2
->op
== TOKstring
)
1307 { e
= new CondExp(loc
, econd
, e1
->castTo(sc
, t
), e2
->castTo(sc
, t
));
1311 e
= Expression::castTo(sc
, t
);
1316 /* ==================== ====================== */
1318 /****************************************
1319 * Scale addition/subtraction to/from pointer.
1322 Expression
*BinExp::scaleFactor(Scope
*sc
)
1324 Type
*t1b
= e1
->type
->toBasetype();
1325 Type
*t2b
= e2
->type
->toBasetype();
1327 if (t1b
->ty
== Tpointer
&& t2b
->isintegral())
1328 { // Need to adjust operator by the stride
1329 // Replace (ptr + int) with (ptr + (int * stride))
1330 Type
*t
= Type::tptrdiff_t
;
1332 stride
= t1b
->nextOf()->size(loc
);
1333 if (!t
->equals(t2b
))
1334 e2
= e2
->castTo(sc
, t
);
1335 e2
= new MulExp(loc
, e2
, new IntegerExp(0, stride
, t
));
1339 else if (t2b
->ty
&& t1b
->isintegral())
1340 { // Need to adjust operator by the stride
1341 // Replace (int + ptr) with (ptr + (int * stride))
1342 Type
*t
= Type::tptrdiff_t
;
1345 stride
= t2b
->nextOf()->size(loc
);
1346 if (!t
->equals(t1b
))
1347 e
= e1
->castTo(sc
, t
);
1350 e
= new MulExp(loc
, e
, new IntegerExp(0, stride
, t
));
1359 /************************************
1360 * Bring leaves to common type.
1363 Expression
*BinExp::typeCombine(Scope
*sc
)
1370 //printf("BinExp::typeCombine() %s\n", toChars());
1373 e1
= e1
->integralPromotions(sc
);
1374 e2
= e2
->integralPromotions(sc
);
1376 // BUG: do toBasetype()
1382 //if (t1) printf("\tt1 = %s\n", t1->toChars());
1383 //if (t2) printf("\tt2 = %s\n", t2->toChars());
1385 if (!t2
) printf("\te2 = '%s'\n", e2
->toChars());
1389 Type
*t1b
= t1
->toBasetype();
1390 Type
*t2b
= t2
->toBasetype();
1392 ty
= (TY
)Type::impcnvResult
[t1b
->ty
][t2b
->ty
];
1397 ty1
= (TY
)Type::impcnvType1
[t1b
->ty
][t2b
->ty
];
1398 ty2
= (TY
)Type::impcnvType2
[t1b
->ty
][t2b
->ty
];
1400 if (t1b
->ty
== ty1
) // if no promotions
1418 type
= Type::basic
[ty
];
1420 t1
= Type::basic
[ty1
];
1421 t2
= Type::basic
[ty2
];
1422 e1
= e1
->castTo(sc
, t1
);
1423 e2
= e2
->castTo(sc
, t2
);
1425 if (type
!= Type::basic
[ty
])
1427 type
= Type::basic
[ty
];
1428 return castTo(sc
, t
);
1431 //printf("after typeCombine():\n");
1433 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
1442 if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) &&
1443 (t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) &&
1444 (t1
->nextOf()->mod
|| t2
->nextOf()->mod
) &&
1445 (t1
->nextOf()->mod
!= t2
->nextOf()->mod
)
1448 t1
= t1
->nextOf()->mutableOf()->constOf()->arrayOf();
1449 t2
= t2
->nextOf()->mutableOf()->constOf()->arrayOf();
1450 //t1 = t1->constOf();
1451 //t2 = t2->constOf();
1452 if (e1
->op
== TOKstring
&& !((StringExp
*)e1
)->committed
)
1455 e1
= e1
->castTo(sc
, t1
);
1456 if (e2
->op
== TOKstring
&& !((StringExp
*)e2
)->committed
)
1459 e2
= e2
->castTo(sc
, t2
);
1468 if ((t1
->ty
== Tstruct
|| t1
->ty
== Tclass
) &&
1469 (op
== TOKmin
|| op
== TOKadd
))
1472 else if (t1
->ty
== Tpointer
&& t2
->ty
== Tpointer
)
1474 // Bring pointers to compatible type
1475 Type
*t1n
= t1
->nextOf();
1476 Type
*t2n
= t2
->nextOf();
1480 else if (t1n
->ty
== Tvoid
) // pointers to void are always compatible
1482 else if (t2n
->ty
== Tvoid
)
1484 else if (t1n
->mod
!= t2n
->mod
)
1486 t1
= t1n
->mutableOf()->constOf()->pointerTo();
1487 t2
= t2n
->mutableOf()->constOf()->pointerTo();
1491 else if (t1n
->ty
== Tclass
&& t2n
->ty
== Tclass
)
1492 { ClassDeclaration
*cd1
= t1n
->isClassHandle();
1493 ClassDeclaration
*cd2
= t2n
->isClassHandle();
1494 target_ptrdiff_t offset
;
1496 if (cd1
->isBaseOf(cd2
, &offset
))
1499 e2
= e2
->castTo(sc
, t
);
1501 else if (cd2
->isBaseOf(cd1
, &offset
))
1505 e1
= e1
->castTo(sc
, t
);
1513 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) &&
1514 e2
->op
== TOKnull
&& t2
->ty
== Tpointer
&& t2
->nextOf()->ty
== Tvoid
)
1515 { /* (T[n] op void*)
1520 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) &&
1521 e1
->op
== TOKnull
&& t1
->ty
== Tpointer
&& t1
->nextOf()->ty
== Tvoid
)
1522 { /* (void* op T[n])
1527 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) && t1
->implicitConvTo(t2
))
1531 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) && t2
->implicitConvTo(t1
))
1535 /* If one is mutable and the other invariant, then retry
1536 * with both of them as const
1538 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
|| t1
->ty
== Tpointer
) &&
1539 (t2
->ty
== Tsarray
|| t2
->ty
== Tarray
|| t2
->ty
== Tpointer
) &&
1540 t1
->nextOf()->mod
!= t2
->nextOf()->mod
1543 if (t1
->ty
== Tpointer
)
1544 t1
= t1
->nextOf()->mutableOf()->constOf()->pointerTo();
1546 t1
= t1
->nextOf()->mutableOf()->constOf()->arrayOf();
1548 if (t2
->ty
== Tpointer
)
1549 t2
= t2
->nextOf()->mutableOf()->constOf()->pointerTo();
1551 t2
= t2
->nextOf()->mutableOf()->constOf()->arrayOf();
1555 else if (t1
->ty
== Tclass
|| t2
->ty
== Tclass
)
1559 int i1
= e2
->implicitConvTo(t1
);
1560 int i2
= e1
->implicitConvTo(t2
);
1564 // We have the case of class vs. void*, so pick class
1565 if (t1
->ty
== Tpointer
)
1567 else if (t2
->ty
== Tpointer
)
1579 else if (t1
->ty
== Tclass
&& t2
->ty
== Tclass
)
1580 { TypeClass
*tc1
= (TypeClass
*)t1
;
1581 TypeClass
*tc2
= (TypeClass
*)t2
;
1583 /* Pick 'tightest' type
1585 ClassDeclaration
*cd1
= tc1
->sym
->baseClass
;
1586 ClassDeclaration
*cd2
= tc1
->sym
->baseClass
;
1603 else if (t1
->ty
== Tstruct
&& t2
->ty
== Tstruct
)
1605 if (((TypeStruct
*)t1
)->sym
!= ((TypeStruct
*)t2
)->sym
)
1608 else if ((e1
->op
== TOKstring
|| e1
->op
== TOKnull
) && e1
->implicitConvTo(t2
))
1612 else if ((e2
->op
== TOKstring
|| e2
->op
== TOKnull
) && e2
->implicitConvTo(t1
))
1616 else if (t1
->ty
== Tsarray
&& t2
->ty
== Tsarray
&&
1617 e2
->implicitConvTo(t1
->nextOf()->arrayOf()))
1620 t
= t1
->nextOf()->arrayOf();
1621 e1
= e1
->castTo(sc
, t
);
1622 e2
= e2
->castTo(sc
, t
);
1624 else if (t1
->ty
== Tsarray
&& t2
->ty
== Tsarray
&&
1625 e1
->implicitConvTo(t2
->nextOf()->arrayOf()))
1628 t
= t2
->nextOf()->arrayOf();
1629 e1
= e1
->castTo(sc
, t
);
1630 e2
= e2
->castTo(sc
, t
);
1632 else if (t1
->isintegral() && t2
->isintegral())
1639 incompatibleTypes();
1645 printf("-BinExp::typeCombine() %s\n", toChars());
1646 if (e1
->type
) printf("\tt1 = %s\n", e1
->type
->toChars());
1647 if (e2
->type
) printf("\tt2 = %s\n", e2
->type
->toChars());
1648 printf("\ttype = %s\n", type
->toChars());
1655 e2
= e2
->castTo(sc
, t1
);
1660 e1
= e1
->castTo(sc
, t2
);
1665 /***********************************
1666 * Do integral promotions (convertchk).
1667 * Don't convert <array of> to <pointer to>
1670 Expression
*Expression::integralPromotions(Scope
*sc
)
1672 Expression
*e
= this;
1674 //printf("integralPromotions %s %s\n", e->toChars(), e->type->toChars());
1675 switch (type
->toBasetype()->ty
)
1678 error("void has no value");
1689 e
= e
->castTo(sc
, Type::tint32
);
1693 e
= e
->castTo(sc
, Type::tuns32
);