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("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
] &&
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());
53 Expression
*e
= optimize(WANTflags
| WANTvalue
);
55 return e
->implicitCastTo(sc
, t
);
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);
70 * { static void fork(EDG dg) { dg(E.One); }
71 * alias void delegate(E) EDG;
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());
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
)
93 printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n",
94 toChars(), type
->toChars(), t
->toChars());
97 { error("%s is not an expression", toChars());
100 if (t
->ty
== Tbit
&& isBit())
102 Expression
*e
= optimize(WANTvalue
| WANTflags
);
104 { //printf("optimzed to %s\n", e->toChars());
105 return e
->implicitConvTo(t
);
107 MATCH match
= type
->implicitConvTo(t
);
111 Type
*tb
= t
->toBasetype();
112 if (tb
->ty
== Tdelegate
)
113 { TypeDelegate
*td
= (TypeDelegate
*)tb
;
114 TypeFunction
*tf
= (TypeFunction
*)td
->next
;
117 !(tf
->arguments
&& tf
->arguments
->dim
)
120 match
= type
->implicitConvTo(tf
->next
);
123 if (tf
->next
->toBasetype()->ty
== Tvoid
)
132 MATCH
IntegerExp::implicitConvTo(Type
*t
)
135 printf("IntegerExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
136 toChars(), type
->toChars(), t
->toChars());
141 enum TY ty
= type
->toBasetype()->ty
;
142 enum TY toty
= t
->toBasetype()->ty
;
144 if (type
->implicitConvTo(t
) == MATCHnomatch
&& t
->ty
== Tenum
)
158 value
= (signed char)value
;
169 value
= (short)value
;
193 // Only allow conversion if no change in value
198 if ((value
& 1) != value
)
203 if ((signed char)value
!= value
)
209 //printf("value = %llu %llu\n", (integer_t)(unsigned char)value, value);
210 if ((unsigned char)value
!= value
)
215 if ((short)value
!= value
)
220 if ((unsigned short)value
!= value
)
228 else if ((int)value
!= value
)
236 else if ((unsigned)value
!= value
)
241 if (value
> 0x10FFFFUL
)
246 if ((unsigned short)value
!= value
)
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
);
267 if ((d_uns64
) f
.toInt() != (d_uns64
) value
)
272 f
= real_t((d_int64
) value
);
274 if ((d_int64
) f
.toInt() != (d_int64
) value
)
283 if (type
->isunsigned())
291 f
= (float)(long long)value
;
292 if (f
!= (long long)value
)
301 if (type
->isunsigned())
309 f
= (double)(long long)value
;
310 if (f
!= (long long)value
)
318 volatile long double f
;
319 if (type
->isunsigned())
321 f
= (long double)value
;
327 f
= (long double)(long long)value
;
328 if (f
!= (long long)value
)
335 return Expression::implicitConvTo(t
);
338 //printf("MATCHconvert\n");
342 //printf("MATCHnomatch\n");
346 MATCH
NullExp::implicitConvTo(Type
*t
)
349 printf("NullExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
350 toChars(), type
->toChars(), t
->toChars());
353 if (this->type
->equals(t
))
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
||
363 return committed
? MATCHconvert
: MATCHexact
;
366 /* Null can only be implicitly converted to a maybe type */
372 return Expression::implicitConvTo(t
);
375 MATCH
StringExp::implicitConvTo(Type
*t
)
379 printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
380 toChars(), committed
, type
->toChars(), t
->toChars());
384 if (!committed
&& t
->ty
== Tpointer
&& t
->next
->ty
== Tvoid
)
388 if (type
->ty
== Tsarray
|| type
->ty
== Tarray
|| type
->ty
== Tpointer
)
390 if (type
->next
->ty
== Tchar
)
395 if (type
->ty
== Tsarray
&&
396 ((TypeSArray
*)type
)->dim
->toInteger() !=
397 ((TypeSArray
*)t
)->dim
->toInteger())
404 if (t
->next
->ty
== Tchar
)
406 else if (t
->next
->ty
== Twchar
)
408 else if (t
->next
->ty
== Tdchar
)
415 return Expression::implicitConvTo(t
);
417 m
= (MATCH
)type
->implicitConvTo(t
);
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
);
445 result
= m
; // remember worst match
446 if (result
== MATCHnomatch
)
447 break; // no need to check for worse
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
);
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
);
472 result
= m
; // remember worst match
473 if (result
== MATCHnomatch
)
474 break; // no need to check for worse
479 return Expression::implicitConvTo(t
);
482 MATCH
AddrExp::implicitConvTo(Type
*t
)
485 printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
486 toChars(), type
->toChars(), t
->toChars());
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.
500 if (type
->ty
== Tpointer
&& type
->next
->ty
== Tfunction
&&
501 t
->ty
== Tpointer
&& t
->next
->ty
== Tfunction
&&
505 f
= ve
->var
->isFuncDeclaration();
506 if (f
&& f
->overloadExactMatch(t
->next
))
510 //printf("\tresult = %d\n", result);
514 MATCH
SymOffExp::implicitConvTo(Type
*t
)
517 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
518 toChars(), type
->toChars(), t
->toChars());
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.
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
))
539 //printf("\tresult = %d\n", result);
543 MATCH
DelegateExp::implicitConvTo(Type
*t
)
546 printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
547 toChars(), type
->toChars(), t
->toChars());
551 result
= type
->implicitConvTo(t
);
555 // Look for pointers to functions where the functions are overloaded.
559 if (type
->ty
== Tdelegate
&& type
->next
->ty
== Tfunction
&&
560 t
->ty
== Tdelegate
&& t
->next
->ty
== Tfunction
)
562 if (func
&& func
->overloadExactMatch(t
->next
))
569 MATCH
CondExp::implicitConvTo(Type
*t
)
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());
592 printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
593 toChars(), type
->toChars(), t
->toChars());
597 Expression
*e
= this;
598 Type
*tb
= t
->toBasetype();
599 Type
*typeb
= type
->toBasetype();
602 // Do (type *) cast of (type [dim])
603 if (tb
->ty
== Tpointer
&&
607 //printf("Converting [dim] to *\n");
609 if (typeb
->size(loc
) == 0)
610 e
= new NullExp(loc
);
612 e
= new AddrExp(loc
, e
);
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());
624 e
= new CastExp(loc
, e
, tb
);
629 e
= e
->copy(); // because of COW for assignment to e->type
633 //printf("Returning: %s\n", e->toChars());
638 Expression
*RealExp::castTo(Scope
*sc
, Type
*t
)
639 { Expression
*e
= this;
642 if ((type
->isreal() && t
->isreal()) ||
643 (type
->isimaginary() && t
->isimaginary())
649 e
= Expression::castTo(sc
, t
);
655 Expression
*ComplexExp::castTo(Scope
*sc
, Type
*t
)
656 { Expression
*e
= this;
659 if (type
->iscomplex() && t
->iscomplex())
664 e
= Expression::castTo(sc
, t
);
670 Expression
*NullExp::castTo(Scope
*sc
, Type
*t
)
674 //printf("NullExp::castTo(t = %p)\n", t);
680 e
= (NullExp
*)copy();
682 tb
= t
->toBasetype();
683 e
->type
= type
->toBasetype();
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
))
692 if (tb
->ty
== Tdelegate
)
693 { TypeDelegate
*td
= (TypeDelegate
*)tb
;
694 TypeFunction
*tf
= (TypeFunction
*)td
->nextOf();
697 !(tf
->arguments
&& tf
->arguments
->dim
)
700 return Expression::castTo(sc
, t
);
707 return e
->Expression::castTo(sc
, t
);
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.
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*");
733 { se
= (StringExp
*)copy();
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();
752 { se
= (StringExp
*)copy();
759 if (tb
->ty
!= Tsarray
&& tb
->ty
!= Tarray
&& tb
->ty
!= Tpointer
)
761 { se
= (StringExp
*)copy();
766 if (typeb
->ty
!= Tsarray
&& typeb
->ty
!= Tarray
&& typeb
->ty
!= Tpointer
)
768 { se
= (StringExp
*)copy();
774 if (typeb
->nextOf()->size() == tb
->nextOf()->size())
777 { se
= (StringExp
*)copy();
780 if (tb
->ty
== Tsarray
)
781 goto L2
; // handle possible change in static array dimension
789 #define X(tf,tt) ((tf) * 256 + (tt))
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
):
802 case X(Tchar
, Twchar
):
803 for (size_t u
= 0; u
< len
;)
805 char *p
= utf_decodeChar((unsigned char *)se
->string
, len
, &u
, &c
);
809 buffer
.writeUTF16(c
);
811 newlen
= buffer
.offset
/ 2;
812 buffer
.writeUTF16(0);
815 case X(Tchar
, Tdchar
):
816 for (size_t u
= 0; u
< len
;)
818 char *p
= utf_decodeChar((unsigned char *)se
->string
, len
, &u
, &c
);
827 case X(Twchar
,Tchar
):
828 for (size_t u
= 0; u
< len
;)
830 char *p
= utf_decodeWchar((unsigned short *)se
->string
, len
, &u
, &c
);
836 newlen
= buffer
.offset
;
840 case X(Twchar
,Tdchar
):
841 for (size_t u
= 0; u
< len
;)
843 char *p
= utf_decodeWchar((unsigned short *)se
->string
, len
, &u
, &c
);
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
);
862 newlen
= buffer
.offset
;
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
);
873 buffer
.writeUTF16(c
);
876 newlen
= buffer
.offset
/ 2;
877 buffer
.writeUTF16(0);
882 { se
= (StringExp
*)copy();
885 se
->string
= buffer
.extractData();
887 se
->sz
= tb
->nextOf()->size();
891 assert(typeb
->nextOf()->size() != tb
->nextOf()->size());
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
909 // Copy when changing the string literal
910 unsigned newsz
= se
->sz
;
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
);
927 Expression
*e
= new CastExp(loc
, se
, t
);
928 e
->type
= t
; // so semantic() won't be run on e
932 Expression
*AddrExp::castTo(Scope
*sc
, Type
*t
)
937 printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
938 toChars(), type
->toChars(), t
->toChars());
940 Expression
*e
= this;
942 tb
= t
->toBasetype();
943 type
= type
->toBasetype();
946 // Look for pointers to functions where the functions are overloaded.
950 if (type
->ty
== Tpointer
&& type
->next
->ty
== Tfunction
&&
951 tb
->ty
== Tpointer
&& tb
->next
->ty
== Tfunction
&&
955 f
= ve
->var
->isFuncDeclaration();
958 f
= f
->overloadExactMatch(tb
->next
);
961 e
= new VarExp(loc
, f
);
963 e
= new AddrExp(loc
, e
);
969 e
= Expression::castTo(sc
, t
);
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
;
988 Expression
*ArrayLiteralExp::castTo(Scope
*sc
, Type
*t
)
991 printf("ArrayLiteralExp::castTo(this=%s, type=%s, t=%s)\n",
992 toChars(), type
->toChars(), t
->toChars());
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())
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
;
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
)
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
;
1056 return e
->Expression::castTo(sc
, t
);
1060 Expression
*SymOffExp::castTo(Scope
*sc
, Type
*t
)
1065 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
1066 toChars(), type
->toChars(), t
->toChars());
1068 Expression
*e
= this;
1070 tb
= t
->toBasetype();
1071 type
= type
->toBasetype();
1074 // Look for pointers to functions where the functions are overloaded.
1077 if (type
->ty
== Tpointer
&& type
->next
->ty
== Tfunction
&&
1078 tb
->ty
== Tpointer
&& tb
->next
->ty
== Tfunction
)
1080 f
= var
->isFuncDeclaration();
1083 f
= f
->overloadExactMatch(tb
->next
);
1086 e
= new SymOffExp(loc
, f
, 0);
1092 e
= Expression::castTo(sc
, t
);
1098 Expression
*DelegateExp::castTo(Scope
*sc
, Type
*t
)
1102 printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
1103 toChars(), type
->toChars(), t
->toChars());
1105 Expression
*e
= this;
1106 static char msg
[] = "cannot form delegate due to covariant return type";
1108 tb
= t
->toBasetype();
1109 type
= type
->toBasetype();
1112 // Look for delegates to functions where the functions are overloaded.
1115 if (type
->ty
== Tdelegate
&& type
->next
->ty
== Tfunction
&&
1116 tb
->ty
== Tdelegate
&& tb
->next
->ty
== Tfunction
)
1120 f
= func
->overloadExactMatch(tb
->next
);
1122 { target_ptrdiff_t offset
;
1123 if (f
->tintro
&& f
->tintro
->next
->isBaseOf(f
->type
->next
, &offset
) && offset
)
1125 e
= new DelegateExp(loc
, e1
, f
);
1133 e
= Expression::castTo(sc
, t
);
1136 { target_ptrdiff_t offset
;
1138 if (func
->tintro
&& func
->tintro
->next
->isBaseOf(func
->type
->next
, &offset
) && offset
)
1145 Expression
*CondExp::castTo(Scope
*sc
, Type
*t
)
1147 Expression
*e
= this;
1151 if (1 || e1
->op
== TOKstring
|| e2
->op
== TOKstring
)
1152 { e
= new CondExp(loc
, econd
, e1
->castTo(sc
, t
), e2
->castTo(sc
, t
));
1156 e
= Expression::castTo(sc
, t
);
1161 /* ==================== ====================== */
1163 /****************************************
1164 * Scale addition/subtraction to/from pointer.
1167 Expression
*BinExp::scaleFactor(Scope
*sc
)
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
));
1186 e2
= new MulExp(loc
, e2
, new IntegerExp(0, stride
, t
));
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
;
1196 stride
= t2b
->next
->size();
1197 if (!t
->equals(t1b
))
1198 e
= e1
->castTo(sc
, t
);
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
));
1205 e
= new MulExp(loc
, e
, new IntegerExp(0, stride
, t
));
1214 /************************************
1215 * Bring leaves to common type.
1218 Expression
*BinExp::typeCombine(Scope
*sc
)
1225 //printf("BinExp::typeCombine()\n");
1228 e1
= e1
->integralPromotions(sc
);
1229 e2
= e2
->integralPromotions(sc
);
1231 // BUG: do toBasetype()
1236 //if (t1) printf("\tt1 = %s\n", t1->toChars());
1237 //if (t2) printf("\tt2 = %s\n", t2->toChars());
1239 if (!t2
) printf("\te2 = '%s'\n", e2
->toChars());
1243 Type
*t1b
= t1
->toBasetype();
1244 Type
*t2b
= t2
->toBasetype();
1246 ty
= (TY
)Type::impcnvResult
[t1b
->ty
][t2b
->ty
];
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
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
);
1279 if (type
!= Type::basic
[ty
])
1281 type
= Type::basic
[ty
];
1282 return castTo(sc
, t
);
1285 //printf("after typeCombine():\n");
1287 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
1294 if ((t1
->ty
== Tstruct
|| t1
->ty
== Tclass
) &&
1295 (op
== TOKmin
|| op
== TOKadd
))
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;
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
;
1336 //if (t1n == t2n) *(char *)0 = 0;
1338 if (t1n
->ty
== Tvoid
) // pointers to void are always compatible
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
))
1350 e2
= e2
->castTo(sc
, t
);
1352 else if (cd2
->isBaseOf(cd1
, &offset
))
1356 e1
= e1
->castTo(sc
, t
);
1364 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) &&
1365 e2
->op
== TOKnull
&& t2
->ty
== Tpointer
&& t2
->next
->ty
== Tvoid
)
1369 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) &&
1370 e1
->op
== TOKnull
&& t1
->ty
== Tpointer
&& t1
->next
->ty
== Tvoid
)
1374 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) && t1
->implicitConvTo(t2
))
1378 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) && t2
->implicitConvTo(t1
))
1382 else if (t1
->ty
== Tclass
|| t2
->ty
== Tclass
)
1386 int i1
= e2
->implicitConvTo(t1
);
1387 int i2
= e1
->implicitConvTo(t2
);
1391 // We have the case of class vs. void*, so pick class
1392 if (t1
->ty
== Tpointer
)
1394 else if (t2
->ty
== Tpointer
)
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
;
1430 else if ((e1
->op
== TOKstring
|| e1
->op
== TOKnull
) && e1
->implicitConvTo(t2
))
1434 //else if (e2->op == TOKstring) { printf("test2\n"); }
1435 else if ((e2
->op
== TOKstring
|| e2
->op
== TOKnull
) && e2
->implicitConvTo(t1
))
1439 else if (t1
->ty
== Tsarray
&& t2
->ty
== Tsarray
&&
1440 e2
->implicitConvTo(t1
->next
->arrayOf()))
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()))
1451 t
= t2
->next
->arrayOf();
1452 e1
= e1
->castTo(sc
, t
);
1453 e2
= e2
->castTo(sc
, t
);
1458 incompatibleTypes();
1468 e2
= e2
->castTo(sc
, t1
);
1473 e1
= e1
->castTo(sc
, t2
);
1478 /***********************************
1479 * Do integral promotions (convertchk).
1480 * Don't convert <array of> to <pointer to>
1483 Expression
*Expression::integralPromotions(Scope
*sc
)
1487 switch (type
->toBasetype()->ty
)
1490 error("void has no value");
1501 e
= e
->castTo(sc
, Type::tint32
);
1505 e
= e
->castTo(sc
, Type::tuns32
);