2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * http://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * http://www.boost.org/LICENSE_1_0.txt
8 * https://github.com/D-Programming-Language/dmd/blob/master/src/cast.c
11 #include "root/dsystem.h" // mem{set|cpy}()
12 #include "root/rmem.h"
15 #include "expression.h"
18 #include "declaration.h"
19 #include "aggregate.h"
26 FuncDeclaration
*isFuncAddress(Expression
*e
, bool *hasOverloads
= NULL
);
27 bool isCommutative(TOK op
);
28 MOD
MODmerge(MOD mod1
, MOD mod2
);
29 Expression
*semantic(Expression
*e
, Scope
*sc
);
31 /* ==================== implicitCast ====================== */
33 /**************************************
34 * Do an implicit cast.
35 * Issue error if it can't be done.
39 Expression
*implicitCastTo(Expression
*e
, Scope
*sc
, Type
*t
)
41 class ImplicitCastTo
: public Visitor
48 ImplicitCastTo(Scope
*sc
, Type
*t
)
54 void visit(Expression
*e
)
56 //printf("Expression::implicitCastTo(%s of type %s) => %s\n", e->toChars(), e->type->toChars(), t->toChars());
58 MATCH match
= e
->implicitConvTo(t
);
61 if (match
== MATCHconst
&&
62 (e
->type
->constConv(t
) ||
63 (!e
->isLvalue() && e
->type
->equivalent(t
))))
65 /* Do not emit CastExp for const conversions and
66 * unique conversions on rvalue.
72 result
= e
->castTo(sc
, t
);
76 result
= e
->optimize(WANTvalue
);
83 if (t
->ty
!= Terror
&& e
->type
->ty
!= Terror
)
87 e
->error("forward reference to type %s", t
->toChars());
91 //printf("type %p ty %d deco %p\n", type, type->ty, type->deco);
92 //type = type->semantic(loc, sc);
93 //printf("type %s t %s\n", type->deco, t->deco);
94 e
->error("cannot implicitly convert expression (%s) of type %s to %s",
95 e
->toChars(), e
->type
->toChars(), t
->toChars());
98 result
= new ErrorExp();
101 void visit(StringExp
*e
)
103 //printf("StringExp::implicitCastTo(%s of type %s) => %s\n", e->toChars(), e->type->toChars(), t->toChars());
104 visit((Expression
*)e
);
105 if (result
->op
== TOKstring
)
107 // Retain polysemous nature if it started out that way
108 ((StringExp
*)result
)->committed
= e
->committed
;
112 void visit(ErrorExp
*e
)
117 void visit(FuncExp
*e
)
119 //printf("FuncExp::implicitCastTo type = %p %s, t = %s\n", e->type, e->type ? e->type->toChars() : NULL, t->toChars());
121 if (e
->matchType(t
, sc
, &fe
) > MATCHnomatch
)
126 visit((Expression
*)e
);
129 void visit(ArrayLiteralExp
*e
)
131 visit((Expression
*)e
);
133 Type
*tb
= result
->type
->toBasetype();
134 if (tb
->ty
== Tarray
)
135 semanticTypeInfo(sc
, ((TypeDArray
*)tb
)->next
);
138 void visit(SliceExp
*e
)
140 visit((Expression
*)e
);
141 if (result
->op
!= TOKslice
)
144 e
= (SliceExp
*)result
;
145 if (e
->e1
->op
== TOKarrayliteral
)
147 ArrayLiteralExp
*ale
= (ArrayLiteralExp
*)e
->e1
;
148 Type
*tb
= t
->toBasetype();
150 if (tb
->ty
== Tsarray
)
151 tx
= tb
->nextOf()->sarrayOf(ale
->elements
? ale
->elements
->dim
: 0);
153 tx
= tb
->nextOf()->arrayOf();
154 e
->e1
= ale
->implicitCastTo(sc
, tx
);
159 ImplicitCastTo
v(sc
, t
);
164 /*******************************************
165 * Return MATCH level of implicitly converting e to type t.
166 * Don't do the actual cast; don't change e.
169 MATCH
implicitConvTo(Expression
*e
, Type
*t
)
171 class ImplicitConvTo
: public Visitor
177 ImplicitConvTo(Type
*t
)
180 result
= MATCHnomatch
;
183 void visit(Expression
*e
)
185 //static int nest; if (++nest == 10) halt();
186 if (t
== Type::terror
)
190 e
->error("%s is not an expression", e
->toChars());
191 e
->type
= Type::terror
;
193 Expression
*ex
= e
->optimize(WANTvalue
);
194 if (ex
->type
->equals(t
))
201 //printf("\toptimized to %s of type %s\n", e->toChars(), e->type->toChars());
202 result
= ex
->implicitConvTo(t
);
205 MATCH match
= e
->type
->implicitConvTo(t
);
206 if (match
!= MATCHnomatch
)
212 /* See if we can do integral narrowing conversions
214 if (e
->type
->isintegral() && t
->isintegral() &&
215 e
->type
->isTypeBasic() && t
->isTypeBasic())
217 IntRange src
= getIntRange(e
);
218 IntRange target
= IntRange::fromType(t
);
219 if (target
.contains(src
))
221 result
= MATCHconvert
;
228 * Given expression e of type t, see if we can implicitly convert e
229 * to type tprime, where tprime is type t with mod bits added.
233 static MATCH
implicitMod(Expression
*e
, Type
*t
, MOD mod
)
236 if (t
->ty
== Tpointer
)
237 tprime
= t
->nextOf()->castMod(mod
)->pointerTo();
238 else if (t
->ty
== Tarray
)
239 tprime
= t
->nextOf()->castMod(mod
)->arrayOf();
240 else if (t
->ty
== Tsarray
)
241 tprime
= t
->nextOf()->castMod(mod
)->sarrayOf(t
->size() / t
->nextOf()->size());
243 tprime
= t
->castMod(mod
);
245 return e
->implicitConvTo(tprime
);
248 static MATCH
implicitConvToAddMin(BinExp
*e
, Type
*t
)
250 /* Is this (ptr +- offset)? If so, then ask ptr
251 * if the conversion can be done.
252 * This is to support doing things like implicitly converting a mutable unique
253 * pointer to an immutable pointer.
256 Type
*typeb
= e
->type
->toBasetype();
257 Type
*tb
= t
->toBasetype();
258 if (typeb
->ty
!= Tpointer
|| tb
->ty
!= Tpointer
)
261 Type
*t1b
= e
->e1
->type
->toBasetype();
262 Type
*t2b
= e
->e2
->type
->toBasetype();
263 if (t1b
->ty
== Tpointer
&& t2b
->isintegral() &&
268 MATCH m
= e
->e1
->implicitConvTo(t
);
269 return (m
> MATCHconst
) ? MATCHconst
: m
;
271 if (t2b
->ty
== Tpointer
&& t1b
->isintegral() &&
275 MATCH m
= e
->e2
->implicitConvTo(t
);
276 return (m
> MATCHconst
) ? MATCHconst
: m
;
282 void visit(AddExp
*e
)
284 visit((Expression
*)e
);
285 if (result
== MATCHnomatch
)
286 result
= implicitConvToAddMin(e
, t
);
289 void visit(MinExp
*e
)
291 visit((Expression
*)e
);
292 if (result
== MATCHnomatch
)
293 result
= implicitConvToAddMin(e
, t
);
296 void visit(IntegerExp
*e
)
298 MATCH m
= e
->type
->implicitConvTo(t
);
305 TY ty
= e
->type
->toBasetype()->ty
;
306 TY toty
= t
->toBasetype()->ty
;
309 if (m
== MATCHnomatch
&& t
->ty
== Tenum
)
312 if (t
->ty
== Tvector
)
314 TypeVector
*tv
= (TypeVector
*)t
;
315 TypeBasic
*tb
= tv
->elementType();
341 // Only allow conversion if no change in value
342 dinteger_t value
= e
->toInteger();
346 if ((value
& 1) != value
)
351 if (ty
== Tuns64
&& value
& ~0x7FUL
)
353 else if ((signed char)value
!= (sinteger_t
)value
)
358 if ((oldty
== Twchar
|| oldty
== Tdchar
) && value
> 0x7F)
362 //printf("value = %llu %llu\n", (dinteger_t)(unsigned char)value, value);
363 if ((unsigned char)value
!= value
)
368 if (ty
== Tuns64
&& value
& ~0x7FFFUL
)
370 else if ((short)value
!= (sinteger_t
)value
)
375 if (oldty
== Tdchar
&& value
> 0xD7FF && value
< 0xE000)
379 if ((unsigned short)value
!= value
)
387 else if (ty
== Tuns64
&& value
& ~0x7FFFFFFFUL
)
389 else if ((int)value
!= (sinteger_t
)value
)
397 else if ((unsigned)value
!= value
)
402 if (value
> 0x10FFFFUL
)
409 if (e
->type
->isunsigned())
417 f
= (float)(sinteger_t
)value
;
418 if (f
!= (sinteger_t
)value
)
427 if (e
->type
->isunsigned())
435 f
= (double)(sinteger_t
)value
;
436 if (f
!= (sinteger_t
)value
)
444 volatile_longdouble f
;
445 if (e
->type
->isunsigned())
448 if ((dinteger_t
)f
!= value
) // isn't this a noop, because the compiler prefers ld
453 f
= ldouble((sinteger_t
)value
);
454 if ((sinteger_t
)f
!= (sinteger_t
)value
)
461 //printf("type = %s\n", type->toBasetype()->toChars());
462 //printf("t = %s\n", t->toBasetype()->toChars());
463 if (ty
== Tpointer
&&
464 e
->type
->toBasetype()->nextOf()->ty
== t
->toBasetype()->nextOf()->ty
)
466 /* Allow things like:
467 * const char* P = cast(char *)3;
475 visit((Expression
*)e
);
479 //printf("MATCHconvert\n");
480 result
= MATCHconvert
;
483 void visit(ErrorExp
*)
488 void visit(NullExp
*e
)
490 if (e
->type
->equals(t
))
496 /* Allow implicit conversions from immutable to mutable|const,
497 * and mutable to immutable. It works because, after all, a null
498 * doesn't actually point to anything.
500 if (t
->equivalent(e
->type
))
506 visit((Expression
*)e
);
509 void visit(StructLiteralExp
*e
)
511 visit((Expression
*)e
);
512 if (result
!= MATCHnomatch
)
514 if (e
->type
->ty
== t
->ty
&& e
->type
->ty
== Tstruct
&&
515 ((TypeStruct
*)e
->type
)->sym
== ((TypeStruct
*)t
)->sym
)
518 for (size_t i
= 0; i
< e
->elements
->dim
; i
++)
520 Expression
*el
= (*e
->elements
)[i
];
524 te
= e
->sd
->fields
[i
]->type
->addMod(t
->mod
);
525 MATCH m2
= el
->implicitConvTo(te
);
526 //printf("\t%s => %s, match = %d\n", el->toChars(), te->toChars(), m2);
533 void visit(StringExp
*e
)
535 if (!e
->committed
&& t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tvoid
)
538 if (e
->type
->ty
== Tsarray
|| e
->type
->ty
== Tarray
|| e
->type
->ty
== Tpointer
)
540 TY tyn
= e
->type
->nextOf()->ty
;
541 if (tyn
== Tchar
|| tyn
== Twchar
|| tyn
== Tdchar
)
546 if (e
->type
->ty
== Tsarray
)
548 TY tynto
= t
->nextOf()->ty
;
551 if (((TypeSArray
*)e
->type
)->dim
->toInteger() ==
552 ((TypeSArray
*)t
)->dim
->toInteger())
558 if (tynto
== Tchar
|| tynto
== Twchar
|| tynto
== Tdchar
)
560 if (e
->committed
&& tynto
!= tyn
)
562 size_t fromlen
= e
->numberOfCodeUnits(tynto
);
563 size_t tolen
= (size_t)((TypeSArray
*)t
)->dim
->toInteger();
566 if (tolen
!= fromlen
)
568 // implicit length extending
569 result
= MATCHconvert
;
573 if (!e
->committed
&& (tynto
== Tchar
|| tynto
== Twchar
|| tynto
== Tdchar
))
579 else if (e
->type
->ty
== Tarray
)
581 TY tynto
= t
->nextOf()->ty
;
582 if (tynto
== Tchar
|| tynto
== Twchar
|| tynto
== Tdchar
)
584 if (e
->committed
&& tynto
!= tyn
)
586 size_t fromlen
= e
->numberOfCodeUnits(tynto
);
587 size_t tolen
= (size_t)((TypeSArray
*)t
)->dim
->toInteger();
590 if (tolen
!= fromlen
)
592 // implicit length extending
593 result
= MATCHconvert
;
602 if (!e
->committed
&& (tynto
== Tchar
|| tynto
== Twchar
|| tynto
== Tdchar
))
611 Type
*tn
= t
->nextOf();
612 MATCH m
= MATCHexact
;
613 if (e
->type
->nextOf()->mod
!= tn
->mod
)
624 if (e
->postfix
== 'w' || e
->postfix
== 'd')
629 if (e
->postfix
!= 'w')
634 if (e
->postfix
!= 'd')
645 visit((Expression
*)e
);
648 void visit(ArrayLiteralExp
*e
)
650 Type
*typeb
= e
->type
->toBasetype();
651 Type
*tb
= t
->toBasetype();
652 if ((tb
->ty
== Tarray
|| tb
->ty
== Tsarray
) &&
653 (typeb
->ty
== Tarray
|| typeb
->ty
== Tsarray
))
656 Type
*typen
= typeb
->nextOf()->toBasetype();
658 if (tb
->ty
== Tsarray
)
660 TypeSArray
*tsa
= (TypeSArray
*)tb
;
661 if (e
->elements
->dim
!= tsa
->dim
->toInteger())
662 result
= MATCHnomatch
;
665 Type
*telement
= tb
->nextOf();
666 if (!e
->elements
->dim
)
668 if (typen
->ty
!= Tvoid
)
669 result
= typen
->implicitConvTo(telement
);
675 MATCH m
= e
->basis
->implicitConvTo(telement
);
679 for (size_t i
= 0; i
< e
->elements
->dim
; i
++)
681 Expression
*el
= (*e
->elements
)[i
];
682 if (result
== MATCHnomatch
)
686 MATCH m
= el
->implicitConvTo(telement
);
688 result
= m
; // remember worst match
693 result
= e
->type
->implicitConvTo(t
);
697 else if (tb
->ty
== Tvector
&&
698 (typeb
->ty
== Tarray
|| typeb
->ty
== Tsarray
))
701 // Convert array literal to vector type
702 TypeVector
*tv
= (TypeVector
*)tb
;
703 TypeSArray
*tbase
= (TypeSArray
*)tv
->basetype
;
704 assert(tbase
->ty
== Tsarray
);
705 const size_t edim
= e
->elements
->dim
;
706 const size_t tbasedim
= tbase
->dim
->toInteger();
709 result
= MATCHnomatch
;
713 Type
*telement
= tv
->elementType();
716 Expression
*el
= typeb
->nextOf()->defaultInitLiteral(e
->loc
);
717 MATCH m
= el
->implicitConvTo(telement
);
719 result
= m
; // remember worst match
721 for (size_t i
= 0; i
< edim
; i
++)
723 Expression
*el
= (*e
->elements
)[i
];
724 MATCH m
= el
->implicitConvTo(telement
);
726 result
= m
; // remember worst match
727 if (result
== MATCHnomatch
)
728 break; // no need to check for worse
733 visit((Expression
*)e
);
736 void visit(AssocArrayLiteralExp
*e
)
738 Type
*typeb
= e
->type
->toBasetype();
739 Type
*tb
= t
->toBasetype();
740 if (tb
->ty
== Taarray
&& typeb
->ty
== Taarray
)
743 for (size_t i
= 0; i
< e
->keys
->dim
; i
++)
745 Expression
*el
= (*e
->keys
)[i
];
746 MATCH m
= el
->implicitConvTo(((TypeAArray
*)tb
)->index
);
748 result
= m
; // remember worst match
749 if (result
== MATCHnomatch
)
750 break; // no need to check for worse
751 el
= (*e
->values
)[i
];
752 m
= el
->implicitConvTo(tb
->nextOf());
754 result
= m
; // remember worst match
755 if (result
== MATCHnomatch
)
756 break; // no need to check for worse
761 visit((Expression
*)e
);
764 void visit(CallExp
*e
)
766 visit((Expression
*)e
);
767 if (result
!= MATCHnomatch
)
770 /* Allow the result of strongly pure functions to
771 * convert to immutable
773 if (e
->f
&& e
->f
->isolateReturn())
775 result
= e
->type
->immutableOf()->implicitConvTo(t
);
776 if (result
> MATCHconst
) // Match level is MATCHconst at best.
781 /* Conversion is 'const' conversion if:
782 * 1. function is pure (weakly pure is ok)
783 * 2. implicit conversion only fails because of mod bits
784 * 3. each function parameter can be implicitly converted to the mod bits
786 Type
*tx
= e
->f
? e
->f
->type
: e
->e1
->type
;
787 tx
= tx
->toBasetype();
788 if (tx
->ty
!= Tfunction
)
790 TypeFunction
*tf
= (TypeFunction
*)tx
;
792 if (tf
->purity
== PUREimpure
)
794 if (e
->f
&& e
->f
->isNested())
797 /* See if fail only because of mod bits.
799 * Bugzilla 14155: All pure functions can access global immutable data.
800 * So the returned pointer may refer an immutable global data,
801 * and then the returned pointer that points non-mutable object
802 * cannot be unique pointer.
806 * static this() { g = 1; }
807 * const(int*) foo() pure { return &g; }
809 * immutable(int*) ip = foo(); // OK
810 * int* mp = foo(); // should be disallowed
813 if (e
->type
->immutableOf()->implicitConvTo(t
) < MATCHconst
&&
814 e
->type
->addMod(MODshared
)->implicitConvTo(t
) < MATCHconst
&&
815 e
->type
->implicitConvTo(t
->addMod(MODshared
)) < MATCHconst
)
819 // Allow a conversion to immutable type, or
820 // conversions of mutable types between thread-local and shared.
822 /* Get mod bits of what we're converting to
824 Type
*tb
= t
->toBasetype();
830 Type
*ti
= getIndirection(t
);
835 return; // not sure what to do with this
837 /* Apply mod bits to each function parameter,
838 * and see if we can convert the function argument to the modded type
841 size_t nparams
= Parameter::dim(tf
->parameters
);
842 size_t j
= (tf
->linkage
== LINKd
&& tf
->varargs
== 1); // if TypeInfoArray was prepended
843 if (e
->e1
->op
== TOKdotvar
)
845 /* Treat 'this' as just another function argument
847 DotVarExp
*dve
= (DotVarExp
*)e
->e1
;
848 Type
*targ
= dve
->e1
->type
;
849 if (targ
->constConv(targ
->castMod(mod
)) == MATCHnomatch
)
852 for (size_t i
= j
; i
< e
->arguments
->dim
; ++i
)
854 Expression
*earg
= (*e
->arguments
)[i
];
855 Type
*targ
= earg
->type
->toBasetype();
858 Parameter
*fparam
= Parameter::getNth(tf
->parameters
, i
- j
);
859 if (fparam
->storageClass
& STClazy
)
860 return; // not sure what to do with this
861 Type
*tparam
= fparam
->type
;
864 if (fparam
->storageClass
& (STCout
| STCref
))
866 if (targ
->constConv(tparam
->castMod(mod
)) == MATCHnomatch
)
872 if (implicitMod(earg
, targ
, mod
) == MATCHnomatch
)
881 void visit(AddrExp
*e
)
883 result
= e
->type
->implicitConvTo(t
);
884 //printf("\tresult = %d\n", result);
886 if (result
!= MATCHnomatch
)
889 // Look for pointers to functions where the functions are overloaded.
893 if (e
->e1
->op
== TOKoverloadset
&&
894 (t
->ty
== Tpointer
|| t
->ty
== Tdelegate
) && t
->nextOf()->ty
== Tfunction
)
896 OverExp
*eo
= (OverExp
*)e
->e1
;
897 FuncDeclaration
*f
= NULL
;
898 for (size_t i
= 0; i
< eo
->vars
->a
.dim
; i
++)
900 Dsymbol
*s
= eo
->vars
->a
[i
];
901 FuncDeclaration
*f2
= s
->isFuncDeclaration();
903 if (f2
->overloadExactMatch(t
->nextOf()))
907 /* Error if match in more than one overload set,
908 * even if one is a 'better' match than the other.
910 ScopeDsymbol::multiplyDefined(e
->loc
, f
, f2
);
919 if (e
->type
->ty
== Tpointer
&& e
->type
->nextOf()->ty
== Tfunction
&&
920 t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tfunction
&&
923 /* I don't think this can ever happen -
924 * it should have been
925 * converted to a SymOffExp.
930 //printf("\tresult = %d\n", result);
933 void visit(SymOffExp
*e
)
935 result
= e
->type
->implicitConvTo(t
);
936 //printf("\tresult = %d\n", result);
937 if (result
!= MATCHnomatch
)
940 // Look for pointers to functions where the functions are overloaded.
942 if (e
->type
->ty
== Tpointer
&& e
->type
->nextOf()->ty
== Tfunction
&&
943 (t
->ty
== Tpointer
|| t
->ty
== Tdelegate
) && t
->nextOf()->ty
== Tfunction
)
945 if (FuncDeclaration
*f
= e
->var
->isFuncDeclaration())
947 f
= f
->overloadExactMatch(t
->nextOf());
950 if ((t
->ty
== Tdelegate
&& (f
->needThis() || f
->isNested())) ||
951 (t
->ty
== Tpointer
&& !(f
->needThis() || f
->isNested())))
958 //printf("\tresult = %d\n", result);
961 void visit(DelegateExp
*e
)
963 result
= e
->type
->implicitConvTo(t
);
964 if (result
!= MATCHnomatch
)
967 // Look for pointers to functions where the functions are overloaded.
969 if (e
->type
->ty
== Tdelegate
&&
972 if (e
->func
&& e
->func
->overloadExactMatch(t
->nextOf()))
977 void visit(FuncExp
*e
)
979 //printf("FuncExp::implicitConvTo type = %p %s, t = %s\n", e->type, e->type ? e->type->toChars() : NULL, t->toChars());
980 MATCH m
= e
->matchType(t
, NULL
, NULL
, 1);
981 if (m
> MATCHnomatch
)
986 visit((Expression
*)e
);
991 visit((Expression
*)e
);
992 if (result
!= MATCHnomatch
)
995 MATCH m1
= e
->e1
->implicitConvTo(t
);
996 MATCH m2
= e
->e2
->implicitConvTo(t
);
998 // Pick the worst match
999 result
= (m1
< m2
) ? m1
: m2
;
1002 void visit(XorExp
*e
)
1004 visit((Expression
*)e
);
1005 if (result
!= MATCHnomatch
)
1008 MATCH m1
= e
->e1
->implicitConvTo(t
);
1009 MATCH m2
= e
->e2
->implicitConvTo(t
);
1011 // Pick the worst match
1012 result
= (m1
< m2
) ? m1
: m2
;
1015 void visit(CondExp
*e
)
1017 MATCH m1
= e
->e1
->implicitConvTo(t
);
1018 MATCH m2
= e
->e2
->implicitConvTo(t
);
1019 //printf("CondExp: m1 %d m2 %d\n", m1, m2);
1021 // Pick the worst match
1022 result
= (m1
< m2
) ? m1
: m2
;
1025 void visit(CommaExp
*e
)
1027 e
->e2
->accept(this);
1030 void visit(CastExp
*e
)
1032 result
= e
->type
->implicitConvTo(t
);
1033 if (result
!= MATCHnomatch
)
1036 if (t
->isintegral() &&
1037 e
->e1
->type
->isintegral() &&
1038 e
->e1
->implicitConvTo(t
) != MATCHnomatch
)
1039 result
= MATCHconvert
;
1041 visit((Expression
*)e
);
1044 void visit(NewExp
*e
)
1046 visit((Expression
*)e
);
1047 if (result
!= MATCHnomatch
)
1050 /* Calling new() is like calling a pure function. We can implicitly convert the
1051 * return from new() to t using the same algorithm as in CallExp, with the function
1052 * 'arguments' being:
1057 * 'member' and 'allocator' need to be pure.
1060 /* See if fail only because of mod bits
1062 if (e
->type
->immutableOf()->implicitConvTo(t
->immutableOf()) == MATCHnomatch
)
1065 /* Get mod bits of what we're converting to
1067 Type
*tb
= t
->toBasetype();
1069 if (Type
*ti
= getIndirection(t
))
1072 return; // not sure what to do with this
1074 /* Apply mod bits to each argument,
1075 * and see if we can convert the argument to the modded type
1080 /* Treat 'this' as just another function argument
1082 Type
*targ
= e
->thisexp
->type
;
1083 if (targ
->constConv(targ
->castMod(mod
)) == MATCHnomatch
)
1087 /* Check call to 'allocator', then 'member'
1089 FuncDeclaration
*fd
= e
->allocator
;
1090 for (int count
= 0; count
< 2; ++count
, (fd
= e
->member
))
1094 if (fd
->errors
|| fd
->type
->ty
!= Tfunction
)
1096 TypeFunction
*tf
= (TypeFunction
*)fd
->type
;
1097 if (tf
->purity
== PUREimpure
)
1100 if (fd
== e
->member
)
1102 if (e
->type
->immutableOf()->implicitConvTo(t
) < MATCHconst
&&
1103 e
->type
->addMod(MODshared
)->implicitConvTo(t
) < MATCHconst
&&
1104 e
->type
->implicitConvTo(t
->addMod(MODshared
)) < MATCHconst
)
1108 // Allow a conversion to immutable type, or
1109 // conversions of mutable types between thread-local and shared.
1112 Expressions
*args
= (fd
== e
->allocator
) ? e
->newargs
: e
->arguments
;
1114 size_t nparams
= Parameter::dim(tf
->parameters
);
1115 size_t j
= (tf
->linkage
== LINKd
&& tf
->varargs
== 1); // if TypeInfoArray was prepended
1116 for (size_t i
= j
; i
< e
->arguments
->dim
; ++i
)
1118 Expression
*earg
= (*args
)[i
];
1119 Type
*targ
= earg
->type
->toBasetype();
1120 if (i
- j
< nparams
)
1122 Parameter
*fparam
= Parameter::getNth(tf
->parameters
, i
- j
);
1123 if (fparam
->storageClass
& STClazy
)
1124 return; // not sure what to do with this
1125 Type
*tparam
= fparam
->type
;
1128 if (fparam
->storageClass
& (STCout
| STCref
))
1130 if (targ
->constConv(tparam
->castMod(mod
)) == MATCHnomatch
)
1136 if (implicitMod(earg
, targ
, mod
) == MATCHnomatch
)
1141 /* If no 'member', then construction is by simple assignment,
1142 * and just straight check 'arguments'
1144 if (!e
->member
&& e
->arguments
)
1146 for (size_t i
= 0; i
< e
->arguments
->dim
; ++i
)
1148 Expression
*earg
= (*e
->arguments
)[i
];
1149 if (!earg
) // Bugzilla 14853: if it's on overlapped field
1151 Type
*targ
= earg
->type
->toBasetype();
1152 if (implicitMod(earg
, targ
, mod
) == MATCHnomatch
)
1157 /* Consider the .init expression as an argument
1159 Type
*ntb
= e
->newtype
->toBasetype();
1160 if (ntb
->ty
== Tarray
)
1161 ntb
= ntb
->nextOf()->toBasetype();
1162 if (ntb
->ty
== Tstruct
)
1164 // Don't allow nested structs - uplevel reference may not be convertible
1165 StructDeclaration
*sd
= ((TypeStruct
*)ntb
)->sym
;
1166 sd
->size(e
->loc
); // resolve any forward references
1170 if (ntb
->isZeroInit(e
->loc
))
1172 /* Zeros are implicitly convertible, except for special cases.
1174 if (ntb
->ty
== Tclass
)
1176 /* With new() must look at the class instance initializer.
1178 ClassDeclaration
*cd
= ((TypeClass
*)ntb
)->sym
;
1180 cd
->size(e
->loc
); // resolve any forward references
1183 return; // uplevel reference may not be convertible
1185 assert(!cd
->isInterfaceDeclaration());
1189 static bool convertible(Loc loc
, ClassDeclaration
*cd
, MOD mod
)
1191 for (size_t i
= 0; i
< cd
->fields
.dim
; i
++)
1193 VarDeclaration
*v
= cd
->fields
[i
];
1194 Initializer
*init
= v
->_init
;
1197 if (init
->isVoidInitializer())
1199 else if (ExpInitializer
*ei
= init
->isExpInitializer())
1201 Type
*tb
= v
->type
->toBasetype();
1202 if (implicitMod(ei
->exp
, tb
, mod
) == MATCHnomatch
)
1207 /* Enhancement: handle StructInitializer and ArrayInitializer
1212 else if (!v
->type
->isZeroInit(loc
))
1215 return cd
->baseClass
? convertible(loc
, cd
->baseClass
, mod
) : true;
1219 if (!ClassCheck::convertible(e
->loc
, cd
, mod
))
1225 Expression
*earg
= e
->newtype
->defaultInitLiteral(e
->loc
);
1226 Type
*targ
= e
->newtype
->toBasetype();
1228 if (implicitMod(earg
, targ
, mod
) == MATCHnomatch
)
1234 result
= MATCHconst
;
1237 void visit(SliceExp
*e
)
1239 //printf("SliceExp::implicitConvTo e = %s, type = %s\n", e->toChars(), e->type->toChars());
1240 visit((Expression
*)e
);
1241 if (result
!= MATCHnomatch
)
1244 Type
*tb
= t
->toBasetype();
1245 Type
*typeb
= e
->type
->toBasetype();
1246 if (tb
->ty
== Tsarray
&& typeb
->ty
== Tarray
)
1248 typeb
= toStaticArrayType(e
);
1250 result
= typeb
->implicitConvTo(t
);
1254 /* If the only reason it won't convert is because of the mod bits,
1255 * then test for conversion by seeing if e1 can be converted with those
1258 Type
*t1b
= e
->e1
->type
->toBasetype();
1259 if (tb
->ty
== Tarray
&& typeb
->equivalent(tb
))
1261 Type
*tbn
= tb
->nextOf();
1264 /* If e->e1 is dynamic array or pointer, the uniqueness of e->e1
1265 * is equivalent with the uniqueness of the referred data. And in here
1266 * we can have arbitrary typed reference for that.
1268 if (t1b
->ty
== Tarray
)
1269 tx
= tbn
->arrayOf();
1270 if (t1b
->ty
== Tpointer
)
1271 tx
= tbn
->pointerTo();
1273 /* If e->e1 is static array, at least it should be an rvalue.
1274 * If not, e->e1 is a reference, and its uniqueness does not link
1275 * to the uniqueness of the referred data.
1277 if (t1b
->ty
== Tsarray
&& !e
->e1
->isLvalue())
1278 tx
= tbn
->sarrayOf(t1b
->size() / tbn
->size());
1282 result
= e
->e1
->implicitConvTo(tx
);
1283 if (result
> MATCHconst
) // Match level is MATCHconst at best.
1284 result
= MATCHconst
;
1288 // Enhancement 10724
1289 if (tb
->ty
== Tpointer
&& e
->e1
->op
== TOKstring
)
1290 e
->e1
->accept(this);
1294 ImplicitConvTo
v(t
);
1299 Type
*toStaticArrayType(SliceExp
*e
)
1301 if (e
->lwr
&& e
->upr
)
1303 // For the following code to work, e should be optimized beforehand.
1304 // (eg. $ in lwr and upr should be already resolved, if possible)
1305 Expression
*lwr
= e
->lwr
->optimize(WANTvalue
);
1306 Expression
*upr
= e
->upr
->optimize(WANTvalue
);
1307 if (lwr
->isConst() && upr
->isConst())
1309 size_t len
= (size_t)(upr
->toUInteger() - lwr
->toUInteger());
1310 return e
->type
->toBasetype()->nextOf()->sarrayOf(len
);
1315 Type
*t1b
= e
->e1
->type
->toBasetype();
1316 if (t1b
->ty
== Tsarray
)
1322 /* ==================== castTo ====================== */
1324 /**************************************
1325 * Do an explicit cast.
1326 * Assume that the 'this' expression does not have any indirections.
1329 Expression
*castTo(Expression
*e
, Scope
*sc
, Type
*t
)
1331 class CastTo
: public Visitor
1338 CastTo(Scope
*sc
, Type
*t
)
1344 void visit(Expression
*e
)
1346 //printf("Expression::castTo(this=%s, t=%s)\n", e->toChars(), t->toChars());
1347 if (e
->type
->equals(t
))
1352 if (e
->op
== TOKvar
)
1354 VarDeclaration
*v
= ((VarExp
*)e
)->var
->isVarDeclaration();
1355 if (v
&& v
->storage_class
& STCmanifest
)
1357 result
= e
->ctfeInterpret();
1358 result
= result
->castTo(sc
, t
);
1363 Type
*tob
= t
->toBasetype();
1364 Type
*t1b
= e
->type
->toBasetype();
1365 if (tob
->equals(t1b
))
1367 result
= e
->copy(); // because of COW for assignment to e->type
1372 /* Make semantic error against invalid cast between concrete types.
1373 * Assume that 'e' is never be any placeholder expressions.
1374 * The result of these checks should be consistent with CastExp::toElem().
1378 const bool tob_isFV
= (tob
->ty
== Tstruct
|| tob
->ty
== Tsarray
|| tob
->ty
== Tvector
);
1379 const bool t1b_isFV
= (t1b
->ty
== Tstruct
|| t1b
->ty
== Tsarray
|| t1b
->ty
== Tvector
);
1381 // Fat Reference types
1382 const bool tob_isFR
= (tob
->ty
== Tarray
|| tob
->ty
== Tdelegate
);
1383 const bool t1b_isFR
= (t1b
->ty
== Tarray
|| t1b
->ty
== Tdelegate
);
1386 const bool tob_isR
= (tob_isFR
|| tob
->ty
== Tpointer
|| tob
->ty
== Taarray
|| tob
->ty
== Tclass
);
1387 const bool t1b_isR
= (t1b_isFR
|| t1b
->ty
== Tpointer
|| t1b
->ty
== Taarray
|| t1b
->ty
== Tclass
);
1389 // Arithmetic types (== valueable basic types)
1390 const bool tob_isA
= ((tob
->isintegral() || tob
->isfloating()) && tob
->ty
!= Tvector
);
1391 const bool t1b_isA
= ((t1b
->isintegral() || t1b
->isfloating()) && t1b
->ty
!= Tvector
);
1393 if (AggregateDeclaration
*t1ad
= isAggregate(t1b
))
1395 AggregateDeclaration
*toad
= isAggregate(tob
);
1396 if (t1ad
!= toad
&& t1ad
->aliasthis
)
1398 if (t1b
->ty
== Tclass
&& tob
->ty
== Tclass
)
1400 ClassDeclaration
*t1cd
= t1b
->isClassHandle();
1401 ClassDeclaration
*tocd
= tob
->isClassHandle();
1403 if (tocd
->isBaseOf(t1cd
, &offset
))
1407 /* Forward the cast to our alias this member, rewrite to:
1408 * cast(to)e1.aliasthis
1410 result
= resolveAliasThis(sc
, e
);
1411 result
= result
->castTo(sc
, t
);
1415 else if (tob
->ty
== Tvector
&& t1b
->ty
!= Tvector
)
1417 //printf("test1 e = %s, e->type = %s, tob = %s\n", e->toChars(), e->type->toChars(), tob->toChars());
1418 TypeVector
*tv
= (TypeVector
*)tob
;
1419 result
= new CastExp(e
->loc
, e
, tv
->elementType());
1420 result
= new VectorExp(e
->loc
, result
, tob
);
1421 result
= ::semantic(result
, sc
);
1424 else if (tob
->ty
!= Tvector
&& t1b
->ty
== Tvector
)
1426 // T[n] <-- __vector(U[m])
1427 if (tob
->ty
== Tsarray
)
1429 if (t1b
->size(e
->loc
) == tob
->size(e
->loc
))
1434 else if (t1b
->implicitConvTo(tob
) == MATCHconst
&& t
->equals(e
->type
->constOf()))
1441 // arithmetic values vs. other arithmetic values
1442 // arithmetic values vs. T*
1443 if ((tob_isA
&& (t1b_isA
|| t1b
->ty
== Tpointer
)) ||
1444 (t1b_isA
&& (tob_isA
|| tob
->ty
== Tpointer
)))
1449 // arithmetic values vs. references or fat values
1450 if ((tob_isA
&& (t1b_isR
|| t1b_isFV
)) ||
1451 (t1b_isA
&& (tob_isR
|| tob_isFV
)))
1456 // Bugzlla 3133: A cast between fat values is possible only when the sizes match.
1457 if (tob_isFV
&& t1b_isFV
)
1459 if (t1b
->size(e
->loc
) == tob
->size(e
->loc
))
1461 e
->error("cannot cast expression %s of type %s to %s because of different sizes",
1462 e
->toChars(), e
->type
->toChars(), t
->toChars());
1463 result
= new ErrorExp();
1467 // Fat values vs. null or references
1468 if ((tob_isFV
&& (t1b
->ty
== Tnull
|| t1b_isR
)) ||
1469 (t1b_isFV
&& (tob
->ty
== Tnull
|| tob_isR
)))
1471 if (tob
->ty
== Tpointer
&& t1b
->ty
== Tsarray
)
1474 // cast(U*)sa; // ==> cast(U*)sa.ptr;
1475 result
= new AddrExp(e
->loc
, e
, t
);
1478 if (tob
->ty
== Tarray
&& t1b
->ty
== Tsarray
)
1481 // cast(U[])sa; // ==> cast(U[])sa[];
1482 d_uns64 fsize
= t1b
->nextOf()->size();
1483 d_uns64 tsize
= tob
->nextOf()->size();
1484 if ((((TypeSArray
*)t1b
)->dim
->toInteger() * fsize
) % tsize
!= 0)
1486 // copied from sarray_toDarray() in e2ir.c
1487 e
->error("cannot cast expression %s of type %s to %s since sizes don't line up",
1488 e
->toChars(), e
->type
->toChars(), t
->toChars());
1489 result
= new ErrorExp();
1497 /* For references, any reinterpret casts are allowed to same 'ty' type.
1499 * R1 function(P1) to R2 function(P2)
1500 * R1 delegate(P1) to R2 delegate(P2)
1503 * class/interface A to B (will be a dynamic cast if possible)
1505 if (tob
->ty
== t1b
->ty
&& tob_isR
&& t1b_isR
)
1508 // typeof(null) <-- non-null references or values
1509 if (tob
->ty
== Tnull
&& t1b
->ty
!= Tnull
)
1510 goto Lfail
; // Bugzilla 14629
1511 // typeof(null) --> non-null references or arithmetic values
1512 if (t1b
->ty
== Tnull
&& tob
->ty
!= Tnull
)
1515 // Check size mismatch of references.
1516 // Tarray and Tdelegate are (void*).sizeof*2, but others have (void*).sizeof.
1517 if ((tob_isFR
&& t1b_isR
) || (t1b_isFR
&& tob_isR
))
1519 if (tob
->ty
== Tpointer
&& t1b
->ty
== Tarray
)
1522 // cast(U*)da; // ==> cast(U*)da.ptr;
1525 if (tob
->ty
== Tpointer
&& t1b
->ty
== Tdelegate
)
1527 // void delegate() dg;
1528 // cast(U*)dg; // ==> cast(U*)dg.ptr;
1529 // Note that it happens even when U is a Tfunction!
1530 e
->deprecation("casting from %s to %s is deprecated", e
->type
->toChars(), t
->toChars());
1536 if (t1b
->ty
== Tvoid
&& tob
->ty
!= Tvoid
)
1539 e
->error("cannot cast expression %s of type %s to %s",
1540 e
->toChars(), e
->type
->toChars(), t
->toChars());
1541 result
= new ErrorExp();
1546 result
= new CastExp(e
->loc
, e
, t
);
1547 result
->type
= t
; // Don't call semantic()
1548 //printf("Returning: %s\n", result->toChars());
1551 void visit(ErrorExp
*e
)
1556 void visit(RealExp
*e
)
1558 if (!e
->type
->equals(t
))
1560 if ((e
->type
->isreal() && t
->isreal()) ||
1561 (e
->type
->isimaginary() && t
->isimaginary())
1568 visit((Expression
*)e
);
1574 void visit(ComplexExp
*e
)
1576 if (!e
->type
->equals(t
))
1578 if (e
->type
->iscomplex() && t
->iscomplex())
1584 visit((Expression
*)e
);
1590 void visit(NullExp
*e
)
1592 //printf("NullExp::castTo(t = %s) %s\n", t->toChars(), toChars());
1593 visit((Expression
*)e
);
1594 if (result
->op
== TOKnull
)
1596 NullExp
*ex
= (NullExp
*)result
;
1602 void visit(StructLiteralExp
*e
)
1604 visit((Expression
*)e
);
1605 if (result
->op
== TOKstructliteral
)
1606 ((StructLiteralExp
*)result
)->stype
= t
; // commit type
1609 void visit(StringExp
*e
)
1611 /* This follows copy-on-write; any changes to 'this'
1612 * will result in a copy.
1613 * The this->string member is considered immutable.
1617 //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), e->toChars(), e->committed);
1619 if (!e
->committed
&& t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tvoid
)
1621 e
->error("cannot convert string literal to void*");
1622 result
= new ErrorExp();
1629 se
= (StringExp
*)e
->copy();
1634 if (e
->type
->equals(t
))
1640 Type
*tb
= t
->toBasetype();
1641 //printf("\ttype = %s\n", e->type->toChars());
1642 if (tb
->ty
== Tdelegate
&& e
->type
->toBasetype()->ty
!= Tdelegate
)
1644 visit((Expression
*)e
);
1648 Type
*typeb
= e
->type
->toBasetype();
1649 if (typeb
->equals(tb
))
1653 se
= (StringExp
*)e
->copy();
1661 /* Handle reinterpret casts:
1662 * cast(wchar[3])"abcd"c --> [\u6261, \u6463, \u0000]
1663 * cast(wchar[2])"abcd"c --> [\u6261, \u6463]
1664 * cast(wchar[1])"abcd"c --> [\u6261]
1666 if (e
->committed
&& tb
->ty
== Tsarray
&& typeb
->ty
== Tarray
)
1668 se
= (StringExp
*)e
->copy();
1669 d_uns64 szx
= tb
->nextOf()->size();
1671 se
->sz
= (unsigned char)szx
;
1672 se
->len
= (size_t)((TypeSArray
*)tb
)->dim
->toInteger();
1676 /* Assure space for terminating 0
1678 if ((se
->len
+ 1) * se
->sz
> (e
->len
+ 1) * e
->sz
)
1680 void *s
= (void *)mem
.xmalloc((se
->len
+ 1) * se
->sz
);
1681 memcpy(s
, se
->string
, se
->len
* se
->sz
);
1682 memset((char *)s
+ se
->len
* se
->sz
, 0, se
->sz
);
1689 if (tb
->ty
!= Tsarray
&& tb
->ty
!= Tarray
&& tb
->ty
!= Tpointer
)
1693 se
= (StringExp
*)e
->copy();
1698 if (typeb
->ty
!= Tsarray
&& typeb
->ty
!= Tarray
&& typeb
->ty
!= Tpointer
)
1702 se
= (StringExp
*)e
->copy();
1708 if (typeb
->nextOf()->size() == tb
->nextOf()->size())
1712 se
= (StringExp
*)e
->copy();
1715 if (tb
->ty
== Tsarray
)
1716 goto L2
; // handle possible change in static array dimension
1725 #define X(tf,tt) ((int)(tf) * 256 + (int)(tt))
1729 int tfty
= typeb
->nextOf()->toBasetype()->ty
;
1730 int ttty
= tb
->nextOf()->toBasetype()->ty
;
1731 switch (X(tfty
, ttty
))
1733 case X(Tchar
, Tchar
):
1734 case X(Twchar
,Twchar
):
1735 case X(Tdchar
,Tdchar
):
1738 case X(Tchar
, Twchar
):
1739 for (size_t u
= 0; u
< e
->len
;)
1742 const char *p
= utf_decodeChar((utf8_t
*)se
->string
, e
->len
, &u
, &c
);
1746 buffer
.writeUTF16(c
);
1748 newlen
= buffer
.offset
/ 2;
1749 buffer
.writeUTF16(0);
1752 case X(Tchar
, Tdchar
):
1753 for (size_t u
= 0; u
< e
->len
;)
1756 const char *p
= utf_decodeChar((utf8_t
*)se
->string
, e
->len
, &u
, &c
);
1765 case X(Twchar
,Tchar
):
1766 for (size_t u
= 0; u
< e
->len
;)
1769 const char *p
= utf_decodeWchar((unsigned short *)se
->string
, e
->len
, &u
, &c
);
1773 buffer
.writeUTF8(c
);
1775 newlen
= buffer
.offset
;
1776 buffer
.writeUTF8(0);
1779 case X(Twchar
,Tdchar
):
1780 for (size_t u
= 0; u
< e
->len
;)
1783 const char *p
= utf_decodeWchar((unsigned short *)se
->string
, e
->len
, &u
, &c
);
1792 case X(Tdchar
,Tchar
):
1793 for (size_t u
= 0; u
< e
->len
; u
++)
1795 unsigned c
= ((unsigned *)se
->string
)[u
];
1796 if (!utf_isValidDchar(c
))
1797 e
->error("invalid UCS-32 char \\U%08x", c
);
1799 buffer
.writeUTF8(c
);
1802 newlen
= buffer
.offset
;
1803 buffer
.writeUTF8(0);
1806 case X(Tdchar
,Twchar
):
1807 for (size_t u
= 0; u
< e
->len
; u
++)
1809 unsigned c
= ((unsigned *)se
->string
)[u
];
1810 if (!utf_isValidDchar(c
))
1811 e
->error("invalid UCS-32 char \\U%08x", c
);
1813 buffer
.writeUTF16(c
);
1816 newlen
= buffer
.offset
/ 2;
1817 buffer
.writeUTF16(0);
1823 se
= (StringExp
*)e
->copy();
1826 se
->string
= buffer
.extractData();
1830 d_uns64 szx
= tb
->nextOf()->size();
1832 se
->sz
= (unsigned char)szx
;
1837 assert(typeb
->nextOf()->size() != tb
->nextOf()->size());
1845 // See if need to truncate or extend the literal
1846 if (tb
->ty
== Tsarray
)
1848 size_t dim2
= (size_t)((TypeSArray
*)tb
)->dim
->toInteger();
1850 //printf("dim from = %d, to = %d\n", (int)se->len, (int)dim2);
1852 // Changing dimensions
1853 if (dim2
!= se
->len
)
1855 // Copy when changing the string literal
1856 size_t newsz
= se
->sz
;
1857 size_t d
= (dim2
< se
->len
) ? dim2
: se
->len
;
1858 void *s
= (void *)mem
.xmalloc((dim2
+ 1) * newsz
);
1859 memcpy(s
, se
->string
, d
* newsz
);
1860 // Extend with 0, add terminating 0
1861 memset((char *)s
+ d
* newsz
, 0, (dim2
+ 1 - d
) * newsz
);
1871 result
= new CastExp(e
->loc
, se
, t
);
1872 result
->type
= t
; // so semantic() won't be run on e
1875 void visit(AddrExp
*e
)
1881 tb
= t
->toBasetype();
1882 e
->type
= e
->type
->toBasetype();
1883 if (!tb
->equals(e
->type
))
1885 // Look for pointers to functions where the functions are overloaded.
1887 if (e
->e1
->op
== TOKoverloadset
&&
1888 (t
->ty
== Tpointer
|| t
->ty
== Tdelegate
) && t
->nextOf()->ty
== Tfunction
)
1890 OverExp
*eo
= (OverExp
*)e
->e1
;
1891 FuncDeclaration
*f
= NULL
;
1892 for (size_t i
= 0; i
< eo
->vars
->a
.dim
; i
++)
1894 Dsymbol
*s
= eo
->vars
->a
[i
];
1895 FuncDeclaration
*f2
= s
->isFuncDeclaration();
1897 if (f2
->overloadExactMatch(t
->nextOf()))
1901 /* Error if match in more than one overload set,
1902 * even if one is a 'better' match than the other.
1904 ScopeDsymbol::multiplyDefined(e
->loc
, f
, f2
);
1913 SymOffExp
*se
= new SymOffExp(e
->loc
, f
, 0, false);
1915 // Let SymOffExp::castTo() do the heavy lifting
1921 if (e
->type
->ty
== Tpointer
&& e
->type
->nextOf()->ty
== Tfunction
&&
1922 tb
->ty
== Tpointer
&& tb
->nextOf()->ty
== Tfunction
&&
1923 e
->e1
->op
== TOKvar
)
1925 VarExp
*ve
= (VarExp
*)e
->e1
;
1926 FuncDeclaration
*f
= ve
->var
->isFuncDeclaration();
1929 assert(f
->isImportedSymbol());
1930 f
= f
->overloadExactMatch(tb
->nextOf());
1933 result
= new VarExp(e
->loc
, f
, false);
1934 result
->type
= f
->type
;
1935 result
= new AddrExp(e
->loc
, result
, t
);
1941 if (FuncDeclaration
*f
= isFuncAddress(e
))
1943 if (f
->checkForwardRef(e
->loc
))
1945 result
= new ErrorExp();
1950 visit((Expression
*)e
);
1955 void visit(TupleExp
*e
)
1957 if (e
->type
->equals(t
))
1963 TupleExp
*te
= (TupleExp
*)e
->copy();
1964 te
->e0
= e
->e0
? e
->e0
->copy() : NULL
;
1965 te
->exps
= (Expressions
*)e
->exps
->copy();
1966 for (size_t i
= 0; i
< te
->exps
->dim
; i
++)
1968 Expression
*ex
= (*te
->exps
)[i
];
1969 ex
= ex
->castTo(sc
, t
);
1970 (*te
->exps
)[i
] = ex
;
1974 /* Questionable behavior: In here, result->type is not set to t.
1976 * TypeTuple!(int, int) values;
1977 * auto values2 = cast(long)values;
1978 * // typeof(values2) == TypeTuple!(int, int) !!
1980 * Only when the casted tuple is immediately expanded, it would work.
1981 * auto arr = [cast(long)values];
1982 * // typeof(arr) == long[]
1986 void visit(ArrayLiteralExp
*e
)
1993 ArrayLiteralExp
*ae
= e
;
1994 Type
*typeb
= e
->type
->toBasetype();
1995 Type
*tb
= t
->toBasetype();
1996 if ((tb
->ty
== Tarray
|| tb
->ty
== Tsarray
) &&
1997 (typeb
->ty
== Tarray
|| typeb
->ty
== Tsarray
))
1999 if (tb
->nextOf()->toBasetype()->ty
== Tvoid
&& typeb
->nextOf()->toBasetype()->ty
!= Tvoid
)
2001 // Don't do anything to cast non-void[] to void[]
2003 else if (typeb
->ty
== Tsarray
&& typeb
->nextOf()->toBasetype()->ty
== Tvoid
)
2005 // Don't do anything for casting void[n] to others
2009 if (tb
->ty
== Tsarray
)
2011 TypeSArray
*tsa
= (TypeSArray
*)tb
;
2012 if (e
->elements
->dim
!= tsa
->dim
->toInteger())
2016 ae
= (ArrayLiteralExp
*)e
->copy();
2018 ae
->basis
= e
->basis
->castTo(sc
, tb
->nextOf());
2019 ae
->elements
= e
->elements
->copy();
2020 for (size_t i
= 0; i
< e
->elements
->dim
; i
++)
2022 Expression
*ex
= (*e
->elements
)[i
];
2025 ex
= ex
->castTo(sc
, tb
->nextOf());
2026 (*ae
->elements
)[i
] = ex
;
2033 else if (tb
->ty
== Tpointer
&& typeb
->ty
== Tsarray
)
2035 Type
*tp
= typeb
->nextOf()->pointerTo();
2036 if (!tp
->equals(ae
->type
))
2038 ae
= (ArrayLiteralExp
*)e
->copy();
2042 else if (tb
->ty
== Tvector
&&
2043 (typeb
->ty
== Tarray
|| typeb
->ty
== Tsarray
))
2045 // Convert array literal to vector type
2046 TypeVector
*tv
= (TypeVector
*)tb
;
2047 TypeSArray
*tbase
= (TypeSArray
*)tv
->basetype
;
2048 assert(tbase
->ty
== Tsarray
);
2049 const size_t edim
= e
->elements
->dim
;
2050 const size_t tbasedim
= tbase
->dim
->toInteger();
2051 if (edim
> tbasedim
)
2054 ae
= (ArrayLiteralExp
*)e
->copy();
2055 ae
->type
= tbase
; // Bugzilla 12642
2056 ae
->elements
= e
->elements
->copy();
2057 Type
*telement
= tv
->elementType();
2058 for (size_t i
= 0; i
< edim
; i
++)
2060 Expression
*ex
= (*e
->elements
)[i
];
2061 ex
= ex
->castTo(sc
, telement
);
2062 (*ae
->elements
)[i
] = ex
;
2064 // Fill in the rest with the default initializer
2065 ae
->elements
->setDim(tbasedim
);
2066 for (size_t i
= edim
; i
< tbasedim
; i
++)
2068 Expression
*ex
= typeb
->nextOf()->defaultInitLiteral(e
->loc
);
2069 ex
= ex
->castTo(sc
, telement
);
2070 (*ae
->elements
)[i
] = ex
;
2072 Expression
*ev
= new VectorExp(e
->loc
, ae
, tb
);
2073 ev
= ::semantic(ev
, sc
);
2078 visit((Expression
*)ae
);
2081 void visit(AssocArrayLiteralExp
*e
)
2088 Type
*typeb
= e
->type
->toBasetype();
2089 Type
*tb
= t
->toBasetype();
2090 if (tb
->ty
== Taarray
&& typeb
->ty
== Taarray
&&
2091 tb
->nextOf()->toBasetype()->ty
!= Tvoid
)
2093 AssocArrayLiteralExp
*ae
= (AssocArrayLiteralExp
*)e
->copy();
2094 ae
->keys
= e
->keys
->copy();
2095 ae
->values
= e
->values
->copy();
2096 assert(e
->keys
->dim
== e
->values
->dim
);
2097 for (size_t i
= 0; i
< e
->keys
->dim
; i
++)
2099 Expression
*ex
= (*e
->values
)[i
];
2100 ex
= ex
->castTo(sc
, tb
->nextOf());
2101 (*ae
->values
)[i
] = ex
;
2104 ex
= ex
->castTo(sc
, ((TypeAArray
*)tb
)->index
);
2105 (*ae
->keys
)[i
] = ex
;
2111 visit((Expression
*)e
);
2114 void visit(SymOffExp
*e
)
2116 if (e
->type
== t
&& !e
->hasOverloads
)
2121 Type
*tb
= t
->toBasetype();
2122 Type
*typeb
= e
->type
->toBasetype();
2124 if (tb
->equals(typeb
))
2128 ((SymOffExp
*)result
)->hasOverloads
= false;
2132 // Look for pointers to functions where the functions are overloaded.
2133 if (e
->hasOverloads
&&
2134 typeb
->ty
== Tpointer
&& typeb
->nextOf()->ty
== Tfunction
&&
2135 (tb
->ty
== Tpointer
|| tb
->ty
== Tdelegate
) && tb
->nextOf()->ty
== Tfunction
)
2137 FuncDeclaration
*f
= e
->var
->isFuncDeclaration();
2138 f
= f
? f
->overloadExactMatch(tb
->nextOf()) : NULL
;
2141 if (tb
->ty
== Tdelegate
)
2143 if (f
->needThis() && hasThis(sc
))
2145 result
= new DelegateExp(e
->loc
, new ThisExp(e
->loc
), f
, false);
2146 result
= ::semantic(result
, sc
);
2148 else if (f
->isNested())
2150 result
= new DelegateExp(e
->loc
, new IntegerExp(0), f
, false);
2151 result
= ::semantic(result
, sc
);
2153 else if (f
->needThis())
2155 e
->error("no 'this' to create delegate for %s", f
->toChars());
2156 result
= new ErrorExp();
2161 e
->error("cannot cast from function pointer to delegate");
2162 result
= new ErrorExp();
2168 result
= new SymOffExp(e
->loc
, f
, 0, false);
2176 if (FuncDeclaration
*f
= isFuncAddress(e
))
2178 if (f
->checkForwardRef(e
->loc
))
2180 result
= new ErrorExp();
2185 visit((Expression
*)e
);
2188 void visit(DelegateExp
*e
)
2190 static const char msg
[] = "cannot form delegate due to covariant return type";
2192 Type
*tb
= t
->toBasetype();
2193 Type
*typeb
= e
->type
->toBasetype();
2194 if (!tb
->equals(typeb
) || e
->hasOverloads
)
2196 // Look for delegates to functions where the functions are overloaded.
2197 if (typeb
->ty
== Tdelegate
&&
2198 tb
->ty
== Tdelegate
)
2202 FuncDeclaration
*f
= e
->func
->overloadExactMatch(tb
->nextOf());
2206 if (f
->tintro
&& f
->tintro
->nextOf()->isBaseOf(f
->type
->nextOf(), &offset
) && offset
)
2207 e
->error("%s", msg
);
2208 if (f
!= e
->func
) // if address not already marked as taken
2210 result
= new DelegateExp(e
->loc
, e
->e1
, f
, false);
2214 if (e
->func
->tintro
)
2215 e
->error("%s", msg
);
2219 if (FuncDeclaration
*f
= isFuncAddress(e
))
2221 if (f
->checkForwardRef(e
->loc
))
2223 result
= new ErrorExp();
2228 visit((Expression
*)e
);
2233 e
->func
->tookAddressOf
++;
2234 if (e
->func
->tintro
&& e
->func
->tintro
->nextOf()->isBaseOf(e
->func
->type
->nextOf(), &offset
) && offset
)
2235 e
->error("%s", msg
);
2241 void visit(FuncExp
*e
)
2243 //printf("FuncExp::castTo type = %s, t = %s\n", e->type->toChars(), t->toChars());
2245 if (e
->matchType(t
, sc
, &fe
, 1) > MATCHnomatch
)
2250 visit((Expression
*)e
);
2253 void visit(CondExp
*e
)
2255 if (!e
->type
->equals(t
))
2257 result
= new CondExp(e
->loc
, e
->econd
, e
->e1
->castTo(sc
, t
), e
->e2
->castTo(sc
, t
));
2264 void visit(CommaExp
*e
)
2266 Expression
*e2c
= e
->e2
->castTo(sc
, t
);
2270 result
= new CommaExp(e
->loc
, e
->e1
, e2c
);
2271 result
->type
= e2c
->type
;
2276 result
->type
= e
->e2
->type
;
2280 void visit(SliceExp
*e
)
2282 //printf("SliceExp::castTo e = %s, type = %s, t = %s\n", e->toChars(), e->type->toChars(), t->toChars());
2283 Type
*typeb
= e
->type
->toBasetype();
2284 Type
*tb
= t
->toBasetype();
2285 if (e
->type
->equals(t
) || typeb
->ty
!= Tarray
||
2286 (tb
->ty
!= Tarray
&& tb
->ty
!= Tsarray
))
2288 visit((Expression
*)e
);
2292 if (tb
->ty
== Tarray
)
2294 if (typeb
->nextOf()->equivalent(tb
->nextOf()))
2296 // T[] to const(T)[]
2302 visit((Expression
*)e
);
2307 // Handle the cast from Tarray to Tsarray with CT-known slicing
2309 TypeSArray
*tsa
= (TypeSArray
*)toStaticArrayType(e
);
2310 if (tsa
&& tsa
->size(e
->loc
) == tb
->size(e
->loc
))
2312 /* Match if the sarray sizes are equal:
2313 * T[a .. b] to const(T)[b-a]
2314 * T[a .. b] to U[dim] if (T.sizeof*(b-a) == U.sizeof*dim)
2316 * If a SliceExp has Tsarray, it will become lvalue.
2317 * That's handled in SliceExp::isLvalue and toLvalue
2323 if (tsa
&& tsa
->dim
->equals(((TypeSArray
*)tb
)->dim
))
2325 /* Match if the dimensions are equal
2326 * with the implicit conversion of e->e1:
2327 * cast(float[2]) [2.0, 1.0, 0.0][0..2];
2329 Type
*t1b
= e
->e1
->type
->toBasetype();
2330 if (t1b
->ty
== Tsarray
)
2331 t1b
= tb
->nextOf()->sarrayOf(((TypeSArray
*)t1b
)->dim
->toInteger());
2332 else if (t1b
->ty
== Tarray
)
2333 t1b
= tb
->nextOf()->arrayOf();
2334 else if (t1b
->ty
== Tpointer
)
2335 t1b
= tb
->nextOf()->pointerTo();
2338 if (e
->e1
->implicitConvTo(t1b
) > MATCHnomatch
)
2340 Expression
*e1x
= e
->e1
->implicitCastTo(sc
, t1b
);
2341 assert(e1x
->op
!= TOKerror
);
2342 e
= (SliceExp
*)e
->copy();
2349 e
->error("cannot cast expression %s of type %s to %s",
2350 e
->toChars(), tsa
? tsa
->toChars() : e
->type
->toChars(),
2352 result
= new ErrorExp();
2361 /* ==================== inferType ====================== */
2363 /****************************************
2364 * Set type inference target
2366 * flag 1: don't put an error when inference fails
2369 Expression
*inferType(Expression
*e
, Type
*t
, int flag
)
2371 class InferType
: public Visitor
2378 InferType(Type
*t
, int flag
)
2385 void visit(Expression
*e
)
2390 void visit(ArrayLiteralExp
*ale
)
2392 Type
*tb
= t
->toBasetype();
2393 if (tb
->ty
== Tarray
|| tb
->ty
== Tsarray
)
2395 Type
*tn
= tb
->nextOf();
2397 ale
->basis
= inferType(ale
->basis
, tn
, flag
);
2398 for (size_t i
= 0; i
< ale
->elements
->dim
; i
++)
2400 Expression
*e
= (*ale
->elements
)[i
];
2403 e
= inferType(e
, tn
, flag
);
2404 (*ale
->elements
)[i
] = e
;
2411 void visit(AssocArrayLiteralExp
*aale
)
2413 Type
*tb
= t
->toBasetype();
2414 if (tb
->ty
== Taarray
)
2416 TypeAArray
*taa
= (TypeAArray
*)tb
;
2417 Type
*ti
= taa
->index
;
2418 Type
*tv
= taa
->nextOf();
2419 for (size_t i
= 0; i
< aale
->keys
->dim
; i
++)
2421 Expression
*e
= (*aale
->keys
)[i
];
2424 e
= inferType(e
, ti
, flag
);
2425 (*aale
->keys
)[i
] = e
;
2428 for (size_t i
= 0; i
< aale
->values
->dim
; i
++)
2430 Expression
*e
= (*aale
->values
)[i
];
2433 e
= inferType(e
, tv
, flag
);
2434 (*aale
->values
)[i
] = e
;
2441 void visit(FuncExp
*fe
)
2443 //printf("FuncExp::inferType('%s'), to=%s\n", fe->type ? fe->type->toChars() : "null", t->toChars());
2444 if (t
->ty
== Tdelegate
||
2445 (t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tfunction
))
2452 void visit(CondExp
*ce
)
2454 Type
*tb
= t
->toBasetype();
2455 ce
->e1
= inferType(ce
->e1
, tb
, flag
);
2456 ce
->e2
= inferType(ce
->e2
, tb
, flag
);
2464 InferType
v(t
, flag
);
2469 /* ==================== ====================== */
2471 /****************************************
2472 * Scale addition/subtraction to/from pointer.
2475 Expression
*scaleFactor(BinExp
*be
, Scope
*sc
)
2477 Type
*t1b
= be
->e1
->type
->toBasetype();
2478 Type
*t2b
= be
->e2
->type
->toBasetype();
2481 if (t1b
->ty
== Tpointer
&& t2b
->isintegral())
2483 // Need to adjust operator by the stride
2484 // Replace (ptr + int) with (ptr + (int * stride))
2485 Type
*t
= Type::tptrdiff_t
;
2487 d_uns64 stride
= t1b
->nextOf()->size(be
->loc
);
2488 if (!t
->equals(t2b
))
2489 be
->e2
= be
->e2
->castTo(sc
, t
);
2491 be
->e2
= new MulExp(be
->loc
, be
->e2
, new IntegerExp(Loc(), stride
, t
));
2493 be
->type
= be
->e1
->type
;
2495 else if (t2b
->ty
== Tpointer
&& t1b
->isintegral())
2497 // Need to adjust operator by the stride
2498 // Replace (int + ptr) with (ptr + (int * stride))
2499 Type
*t
= Type::tptrdiff_t
;
2502 d_uns64 stride
= t2b
->nextOf()->size(be
->loc
);
2503 if (!t
->equals(t1b
))
2504 e
= be
->e1
->castTo(sc
, t
);
2508 e
= new MulExp(be
->loc
, e
, new IntegerExp(Loc(), stride
, t
));
2510 be
->type
= be
->e2
->type
;
2517 if (sc
->func
&& !sc
->intypeof
)
2519 eoff
= eoff
->optimize(WANTvalue
);
2520 if (eoff
->op
== TOKint64
&& eoff
->toInteger() == 0)
2522 else if (sc
->func
->setUnsafe())
2524 be
->error("pointer arithmetic not allowed in @safe functions");
2525 return new ErrorExp();
2532 /**************************************
2533 * Return true if e is an empty array literal with dimensionality
2534 * equal to or less than type of other array.
2535 * [], [[]], [[[]]], etc.
2536 * I.e., make sure that [1,2] is compatible with [],
2537 * [[1,2]] is compatible with [[]], etc.
2539 bool isVoidArrayLiteral(Expression
*e
, Type
*other
)
2541 while (e
->op
== TOKarrayliteral
&& e
->type
->ty
== Tarray
2542 && (((ArrayLiteralExp
*)e
)->elements
->dim
== 1))
2544 ArrayLiteralExp
*ale
= (ArrayLiteralExp
*)e
;
2545 e
= ale
->getElement(0);
2546 if (other
->ty
== Tsarray
|| other
->ty
== Tarray
)
2547 other
= other
->nextOf();
2551 if (other
->ty
!= Tsarray
&& other
->ty
!= Tarray
)
2554 return (e
->op
== TOKarrayliteral
&& t
->ty
== Tarray
&&
2555 t
->nextOf()->ty
== Tvoid
&&
2556 ((ArrayLiteralExp
*)e
)->elements
->dim
== 0);
2559 // used by deduceType()
2560 Type
*rawTypeMerge(Type
*t1
, Type
*t2
)
2564 if (t1
->equivalent(t2
))
2565 return t1
->castMod(MODmerge(t1
->mod
, t2
->mod
));
2567 Type
*t1b
= t1
->toBasetype();
2568 Type
*t2b
= t2
->toBasetype();
2569 if (t1b
->equals(t2b
))
2571 if (t1b
->equivalent(t2b
))
2572 return t1b
->castMod(MODmerge(t1b
->mod
, t2b
->mod
));
2574 TY ty
= (TY
)impcnvResult
[t1b
->ty
][t2b
->ty
];
2576 return Type::basic
[ty
];
2581 /**************************************
2584 * *pt merged type, if *pt is not NULL
2592 bool typeMerge(Scope
*sc
, TOK op
, Type
**pt
, Expression
**pe1
, Expression
**pe2
)
2594 //printf("typeMerge() %s op %s\n", (*pe1)->toChars(), (*pe2)->toChars());
2597 Expression
*e1
= *pe1
;
2598 Expression
*e2
= *pe2
;
2599 Type
*t1b
= e1
->type
->toBasetype();
2600 Type
*t2b
= e2
->type
->toBasetype();
2602 if (op
!= TOKquestion
||
2603 (t1b
->ty
!= t2b
->ty
&& (t1b
->isTypeBasic() && t2b
->isTypeBasic())))
2605 e1
= integralPromotions(e1
, sc
);
2606 e2
= integralPromotions(e2
, sc
);
2609 Type
*t1
= e1
->type
;
2610 Type
*t2
= e2
->type
;
2614 /* The start type of alias this type recursion.
2615 * In following case, we should save A, and stop recursion
2616 * if it appears again.
2617 * X -> Y -> [A] -> B -> A -> B -> ...
2622 //if (t1) printf("\tt1 = %s\n", t1->toChars());
2623 //if (t2) printf("\tt2 = %s\n", t2->toChars());
2626 if (t1
->mod
!= t2
->mod
&&
2627 t1
->ty
== Tenum
&& t2
->ty
== Tenum
&&
2628 ((TypeEnum
*)t1
)->sym
== ((TypeEnum
*)t2
)->sym
)
2630 unsigned char mod
= MODmerge(t1
->mod
, t2
->mod
);
2631 t1
= t1
->castMod(mod
);
2632 t2
= t2
->castMod(mod
);
2636 t1b
= t1
->toBasetype();
2637 t2b
= t2
->toBasetype();
2639 TY ty
= (TY
)impcnvResult
[t1b
->ty
][t2b
->ty
];
2642 TY ty1
= (TY
)impcnvType1
[t1b
->ty
][t2b
->ty
];
2643 TY ty2
= (TY
)impcnvType2
[t1b
->ty
][t2b
->ty
];
2645 if (t1b
->ty
== ty1
) // if no promotions
2653 if (t1b
->equals(t2b
))
2660 t
= Type::basic
[ty
];
2662 t1
= Type::basic
[ty1
];
2663 t2
= Type::basic
[ty2
];
2664 e1
= e1
->castTo(sc
, t1
);
2665 e2
= e2
->castTo(sc
, t2
);
2666 //printf("after typeCombine():\n");
2668 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
2675 if (t1
->ty
== Ttuple
|| t2
->ty
== Ttuple
)
2680 // merging can not result in new enum type
2684 else if ((t1
->ty
== Tpointer
&& t2
->ty
== Tpointer
) ||
2685 (t1
->ty
== Tdelegate
&& t2
->ty
== Tdelegate
))
2687 // Bring pointers to compatible type
2688 Type
*t1n
= t1
->nextOf();
2689 Type
*t2n
= t2
->nextOf();
2691 if (t1n
->equals(t2n
))
2693 else if (t1n
->ty
== Tvoid
) // pointers to void are always compatible
2695 else if (t2n
->ty
== Tvoid
)
2697 else if (t1
->implicitConvTo(t2
))
2701 else if (t2
->implicitConvTo(t1
))
2705 else if (t1n
->ty
== Tfunction
&& t2n
->ty
== Tfunction
)
2707 TypeFunction
*tf1
= (TypeFunction
*)t1n
;
2708 TypeFunction
*tf2
= (TypeFunction
*)t2n
;
2712 TypeFunction
*d
= (TypeFunction
*)tf1
->syntaxCopy();
2714 if (tf1
->purity
!= tf2
->purity
)
2715 d
->purity
= PUREimpure
;
2716 assert(d
->purity
!= PUREfwdref
);
2718 d
->isnothrow
= (tf1
->isnothrow
&& tf2
->isnothrow
);
2719 d
->isnogc
= (tf1
->isnogc
&& tf2
->isnogc
);
2721 if (tf1
->trust
== tf2
->trust
)
2722 d
->trust
= tf1
->trust
;
2723 else if (tf1
->trust
<= TRUSTsystem
|| tf2
->trust
<= TRUSTsystem
)
2724 d
->trust
= TRUSTsystem
;
2726 d
->trust
= TRUSTtrusted
;
2729 if (t1
->ty
== Tdelegate
)
2731 tx
= new TypeDelegate(d
);
2734 tx
= d
->pointerTo();
2736 tx
= tx
->semantic(e1
->loc
, sc
);
2738 if (t1
->implicitConvTo(tx
) && t2
->implicitConvTo(tx
))
2741 e1
= e1
->castTo(sc
, t
);
2742 e2
= e2
->castTo(sc
, t
);
2747 else if (t1n
->mod
!= t2n
->mod
)
2749 if (!t1n
->isImmutable() && !t2n
->isImmutable() && t1n
->isShared() != t2n
->isShared())
2751 unsigned char mod
= MODmerge(t1n
->mod
, t2n
->mod
);
2752 t1
= t1n
->castMod(mod
)->pointerTo();
2753 t2
= t2n
->castMod(mod
)->pointerTo();
2757 else if (t1n
->ty
== Tclass
&& t2n
->ty
== Tclass
)
2759 ClassDeclaration
*cd1
= t1n
->isClassHandle();
2760 ClassDeclaration
*cd2
= t2n
->isClassHandle();
2763 if (cd1
->isBaseOf(cd2
, &offset
))
2766 e2
= e2
->castTo(sc
, t
);
2768 else if (cd2
->isBaseOf(cd1
, &offset
))
2772 e1
= e1
->castTo(sc
, t
);
2779 t1
= t1n
->constOf()->pointerTo();
2780 t2
= t2n
->constOf()->pointerTo();
2781 if (t1
->implicitConvTo(t2
))
2785 else if (t2
->implicitConvTo(t1
))
2792 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) &&
2793 ((e2
->op
== TOKnull
&& t2
->ty
== Tpointer
&& t2
->nextOf()->ty
== Tvoid
) ||
2794 (e2
->op
== TOKarrayliteral
&& t2
->ty
== Tsarray
&& t2
->nextOf()->ty
== Tvoid
&& ((TypeSArray
*)t2
)->dim
->toInteger() == 0) ||
2795 (isVoidArrayLiteral(e2
, t1
)))
2798 /* (T[n] op void*) => T[]
2799 * (T[] op void*) => T[]
2800 * (T[n] op void[0]) => T[]
2801 * (T[] op void[0]) => T[]
2802 * (T[n] op void[]) => T[]
2803 * (T[] op void[]) => T[]
2807 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) &&
2808 ((e1
->op
== TOKnull
&& t1
->ty
== Tpointer
&& t1
->nextOf()->ty
== Tvoid
) ||
2809 (e1
->op
== TOKarrayliteral
&& t1
->ty
== Tsarray
&& t1
->nextOf()->ty
== Tvoid
&& ((TypeSArray
*)t1
)->dim
->toInteger() == 0) ||
2810 (isVoidArrayLiteral(e1
, t2
)))
2813 /* (void* op T[n]) => T[]
2814 * (void* op T[]) => T[]
2815 * (void[0] op T[n]) => T[]
2816 * (void[0] op T[]) => T[]
2817 * (void[] op T[n]) => T[]
2818 * (void[] op T[]) => T[]
2822 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) &&
2823 (m
= t1
->implicitConvTo(t2
)) != MATCHnomatch
)
2825 // Bugzilla 7285: Tsarray op [x, y, ...] should to be Tsarray
2826 // Bugzilla 14737: Tsarray ~ [x, y, ...] should to be Tarray
2827 if (t1
->ty
== Tsarray
&& e2
->op
== TOKarrayliteral
&& op
!= TOKcat
)
2829 if (m
== MATCHconst
&&
2830 (op
== TOKaddass
|| op
== TOKminass
|| op
== TOKmulass
||
2831 op
== TOKdivass
|| op
== TOKmodass
|| op
== TOKpowass
||
2832 op
== TOKandass
|| op
== TOKorass
|| op
== TOKxorass
)
2835 // Don't make the lvalue const
2841 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) && t2
->implicitConvTo(t1
))
2843 // Bugzilla 7285 & 14737
2844 if (t2
->ty
== Tsarray
&& e1
->op
== TOKarrayliteral
&& op
!= TOKcat
)
2848 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
|| t1
->ty
== Tpointer
) &&
2849 (t2
->ty
== Tsarray
|| t2
->ty
== Tarray
|| t2
->ty
== Tpointer
) &&
2850 t1
->nextOf()->mod
!= t2
->nextOf()->mod
2853 /* If one is mutable and the other invariant, then retry
2854 * with both of them as const
2856 Type
*t1n
= t1
->nextOf();
2857 Type
*t2n
= t2
->nextOf();
2859 if (e1
->op
== TOKnull
&& e2
->op
!= TOKnull
)
2861 else if (e1
->op
!= TOKnull
&& e2
->op
== TOKnull
)
2863 else if (!t1n
->isImmutable() && !t2n
->isImmutable() && t1n
->isShared() != t2n
->isShared())
2866 mod
= MODmerge(t1n
->mod
, t2n
->mod
);
2868 if (t1
->ty
== Tpointer
)
2869 t1
= t1n
->castMod(mod
)->pointerTo();
2871 t1
= t1n
->castMod(mod
)->arrayOf();
2873 if (t2
->ty
== Tpointer
)
2874 t2
= t2n
->castMod(mod
)->pointerTo();
2876 t2
= t2n
->castMod(mod
)->arrayOf();
2880 else if (t1
->ty
== Tclass
&& t2
->ty
== Tclass
)
2882 if (t1
->mod
!= t2
->mod
)
2885 if (e1
->op
== TOKnull
&& e2
->op
!= TOKnull
)
2887 else if (e1
->op
!= TOKnull
&& e2
->op
== TOKnull
)
2889 else if (!t1
->isImmutable() && !t2
->isImmutable() && t1
->isShared() != t2
->isShared())
2892 mod
= MODmerge(t1
->mod
, t2
->mod
);
2893 t1
= t1
->castMod(mod
);
2894 t2
= t2
->castMod(mod
);
2900 else if (t1
->ty
== Tclass
|| t2
->ty
== Tclass
)
2905 MATCH i1
= e2
->implicitConvTo(t1
);
2906 MATCH i2
= e1
->implicitConvTo(t2
);
2910 // We have the case of class vs. void*, so pick class
2911 if (t1
->ty
== Tpointer
)
2913 else if (t2
->ty
== Tpointer
)
2919 e2
= e2
->castTo(sc
, t2
);
2924 e1
= e1
->castTo(sc
, t1
);
2927 else if (t1
->ty
== Tclass
&& t2
->ty
== Tclass
)
2929 TypeClass
*tc1
= (TypeClass
*)t1
;
2930 TypeClass
*tc2
= (TypeClass
*)t2
;
2932 /* Pick 'tightest' type
2934 ClassDeclaration
*cd1
= tc1
->sym
->baseClass
;
2935 ClassDeclaration
*cd2
= tc2
->sym
->baseClass
;
2939 t1
= cd1
->type
->castMod(t1
->mod
);
2940 t2
= cd2
->type
->castMod(t2
->mod
);
2949 else if (t1
->ty
== Tstruct
&& ((TypeStruct
*)t1
)->sym
->aliasthis
)
2951 if (att1
&& e1
->type
== att1
)
2953 if (!att1
&& e1
->type
->checkAliasThisRec())
2955 //printf("att tmerge(c || c) e1 = %s\n", e1->type->toChars());
2956 e1
= resolveAliasThis(sc
, e1
);
2960 else if (t2
->ty
== Tstruct
&& ((TypeStruct
*)t2
)->sym
->aliasthis
)
2962 if (att2
&& e2
->type
== att2
)
2964 if (!att2
&& e2
->type
->checkAliasThisRec())
2966 //printf("att tmerge(c || c) e2 = %s\n", e2->type->toChars());
2967 e2
= resolveAliasThis(sc
, e2
);
2975 else if (t1
->ty
== Tstruct
&& t2
->ty
== Tstruct
)
2977 if (t1
->mod
!= t2
->mod
)
2979 if (!t1
->isImmutable() && !t2
->isImmutable() && t1
->isShared() != t2
->isShared())
2981 unsigned char mod
= MODmerge(t1
->mod
, t2
->mod
);
2982 t1
= t1
->castMod(mod
);
2983 t2
= t2
->castMod(mod
);
2988 TypeStruct
*ts1
= (TypeStruct
*)t1
;
2989 TypeStruct
*ts2
= (TypeStruct
*)t2
;
2990 if (ts1
->sym
!= ts2
->sym
)
2992 if (!ts1
->sym
->aliasthis
&& !ts2
->sym
->aliasthis
)
2995 MATCH i1
= MATCHnomatch
;
2996 MATCH i2
= MATCHnomatch
;
2998 Expression
*e1b
= NULL
;
2999 Expression
*e2b
= NULL
;
3000 if (ts2
->sym
->aliasthis
)
3002 if (att2
&& e2
->type
== att2
)
3004 if (!att2
&& e2
->type
->checkAliasThisRec())
3006 //printf("att tmerge(s && s) e2 = %s\n", e2->type->toChars());
3007 e2b
= resolveAliasThis(sc
, e2
);
3008 i1
= e2b
->implicitConvTo(t1
);
3010 if (ts1
->sym
->aliasthis
)
3012 if (att1
&& e1
->type
== att1
)
3014 if (!att1
&& e1
->type
->checkAliasThisRec())
3016 //printf("att tmerge(s && s) e1 = %s\n", e1->type->toChars());
3017 e1b
= resolveAliasThis(sc
, e1
);
3018 i2
= e1b
->implicitConvTo(t2
);
3031 t1
= e1b
->type
->toBasetype();
3036 t2
= e2b
->type
->toBasetype();
3042 else if (t1
->ty
== Tstruct
|| t2
->ty
== Tstruct
)
3044 if (t1
->ty
== Tstruct
&& ((TypeStruct
*)t1
)->sym
->aliasthis
)
3046 if (att1
&& e1
->type
== att1
)
3048 if (!att1
&& e1
->type
->checkAliasThisRec())
3050 //printf("att tmerge(s || s) e1 = %s\n", e1->type->toChars());
3051 e1
= resolveAliasThis(sc
, e1
);
3056 if (t2
->ty
== Tstruct
&& ((TypeStruct
*)t2
)->sym
->aliasthis
)
3058 if (att2
&& e2
->type
== att2
)
3060 if (!att2
&& e2
->type
->checkAliasThisRec())
3062 //printf("att tmerge(s || s) e2 = %s\n", e2->type->toChars());
3063 e2
= resolveAliasThis(sc
, e2
);
3070 else if ((e1
->op
== TOKstring
|| e1
->op
== TOKnull
) && e1
->implicitConvTo(t2
))
3074 else if ((e2
->op
== TOKstring
|| e2
->op
== TOKnull
) && e2
->implicitConvTo(t1
))
3078 else if (t1
->ty
== Tsarray
&& t2
->ty
== Tsarray
&&
3079 e2
->implicitConvTo(t1
->nextOf()->arrayOf()))
3082 t
= t1
->nextOf()->arrayOf(); // T[]
3083 e1
= e1
->castTo(sc
, t
);
3084 e2
= e2
->castTo(sc
, t
);
3086 else if (t1
->ty
== Tsarray
&& t2
->ty
== Tsarray
&&
3087 e1
->implicitConvTo(t2
->nextOf()->arrayOf()))
3090 t
= t2
->nextOf()->arrayOf();
3091 e1
= e1
->castTo(sc
, t
);
3092 e2
= e2
->castTo(sc
, t
);
3094 else if (t1
->ty
== Tvector
&& t2
->ty
== Tvector
)
3096 // Bugzilla 13841, all vector types should have no common types between
3097 // different vectors, even though their sizes are same.
3098 TypeVector
*tv1
= (TypeVector
*)t1
;
3099 TypeVector
*tv2
= (TypeVector
*)t2
;
3100 if (!tv1
->basetype
->equals(tv2
->basetype
))
3105 else if (t1
->ty
== Tvector
&& t2
->ty
!= Tvector
&&
3106 e2
->implicitConvTo(t1
))
3108 e2
= e2
->castTo(sc
, t1
);
3113 else if (t2
->ty
== Tvector
&& t1
->ty
!= Tvector
&&
3114 e1
->implicitConvTo(t2
))
3116 e1
= e1
->castTo(sc
, t2
);
3121 else if (t1
->isintegral() && t2
->isintegral())
3123 if (t1
->ty
!= t2
->ty
)
3125 if (t1
->ty
== Tvector
|| t2
->ty
== Tvector
)
3127 e1
= integralPromotions(e1
, sc
);
3128 e2
= integralPromotions(e2
, sc
);
3133 assert(t1
->ty
== t2
->ty
);
3135 if (!t1
->isImmutable() && !t2
->isImmutable() && t1
->isShared() != t2
->isShared())
3137 unsigned char mod
= MODmerge(t1
->mod
, t2
->mod
);
3139 t1
= t1
->castMod(mod
);
3140 t2
= t2
->castMod(mod
);
3142 e1
= e1
->castTo(sc
, t
);
3143 e2
= e2
->castTo(sc
, t
);
3146 else if (t1
->ty
== Tnull
&& t2
->ty
== Tnull
)
3148 unsigned char mod
= MODmerge(t1
->mod
, t2
->mod
);
3150 t
= t1
->castMod(mod
);
3151 e1
= e1
->castTo(sc
, t
);
3152 e2
= e2
->castTo(sc
, t
);
3155 else if (t2
->ty
== Tnull
&&
3156 (t1
->ty
== Tpointer
|| t1
->ty
== Taarray
|| t1
->ty
== Tarray
))
3160 else if (t1
->ty
== Tnull
&&
3161 (t2
->ty
== Tpointer
|| t2
->ty
== Taarray
|| t2
->ty
== Tarray
))
3165 else if (t1
->ty
== Tarray
&& isBinArrayOp(op
) && isArrayOpOperand(e1
))
3167 if (e2
->implicitConvTo(t1
->nextOf()))
3171 e2
= e2
->castTo(sc
, t1
->nextOf());
3172 t
= t1
->nextOf()->arrayOf();
3174 else if (t1
->nextOf()->implicitConvTo(e2
->type
))
3176 // (cast(T)U)[] op T (Bugzilla 12780)
3177 // e1 is left as U[], it will be handled in arrayOp() later.
3178 t
= e2
->type
->arrayOf();
3180 else if (t2
->ty
== Tarray
&& isArrayOpOperand(e2
))
3182 if (t1
->nextOf()->implicitConvTo(t2
->nextOf()))
3184 // (cast(T)U)[] op T[] (Bugzilla 12780)
3185 // e1 is left as U[], it will be handled in arrayOp() later.
3186 t
= t2
->nextOf()->arrayOf();
3188 else if (t2
->nextOf()->implicitConvTo(t1
->nextOf()))
3190 // T[] op (cast(T)U)[] (Bugzilla 12780)
3191 // e2 is left as U[], it will be handled in arrayOp() later.
3192 t
= t1
->nextOf()->arrayOf();
3200 else if (t2
->ty
== Tarray
&& isBinArrayOp(op
) && isArrayOpOperand(e2
))
3202 if (e1
->implicitConvTo(t2
->nextOf()))
3206 e1
= e1
->castTo(sc
, t2
->nextOf());
3207 t
= t2
->nextOf()->arrayOf();
3209 else if (t2
->nextOf()->implicitConvTo(e1
->type
))
3211 // T op (cast(T)U)[] (Bugzilla 12780)
3212 // e2 is left as U[], it will be handled in arrayOp() later.
3213 t
= e1
->type
->arrayOf();
3218 //printf("test %s\n", Token::toChars(op));
3219 e1
= e1
->optimize(WANTvalue
);
3220 if (isCommutative(op
) && e1
->isConst())
3222 /* Swap operands to minimize number of functions generated
3224 //printf("swap %s\n", Token::toChars(op));
3225 Expression
*tmp
= e1
;
3245 e2
= e2
->castTo(sc
, t1
);
3250 e1
= e1
->castTo(sc
, t2
);
3255 /************************************
3256 * Bring leaves to common type.
3257 * Returns ErrorExp if error occurs. otherwise returns NULL.
3260 Expression
*typeCombine(BinExp
*be
, Scope
*sc
)
3262 Type
*t1
= be
->e1
->type
->toBasetype();
3263 Type
*t2
= be
->e2
->type
->toBasetype();
3265 if (be
->op
== TOKmin
|| be
->op
== TOKadd
)
3267 // struct+struct, and class+class are errors
3268 if (t1
->ty
== Tstruct
&& t2
->ty
== Tstruct
)
3270 else if (t1
->ty
== Tclass
&& t2
->ty
== Tclass
)
3272 else if (t1
->ty
== Taarray
&& t2
->ty
== Taarray
)
3276 if (!typeMerge(sc
, be
->op
, &be
->type
, &be
->e1
, &be
->e2
))
3278 // If the types have no value, return an error
3279 if (be
->e1
->op
== TOKerror
)
3281 if (be
->e2
->op
== TOKerror
)
3286 Expression
*ex
= be
->incompatibleTypes();
3287 if (ex
->op
== TOKerror
)
3289 return new ErrorExp();
3292 /***********************************
3293 * Do integral promotions (convertchk).
3294 * Don't convert <array of> to <pointer to>
3297 Expression
*integralPromotions(Expression
*e
, Scope
*sc
)
3299 //printf("integralPromotions %s %s\n", e->toChars(), e->type->toChars());
3300 switch (e
->type
->toBasetype()->ty
)
3303 e
->error("void has no value");
3304 return new ErrorExp();
3313 e
= e
->castTo(sc
, Type::tint32
);
3317 e
= e
->castTo(sc
, Type::tuns32
);
3325 /***********************************
3326 * See if both types are arrays that can be compared
3327 * for equality. Return true if so.
3328 * If they are arrays, but incompatible, issue error.
3329 * This is to enable comparing things like an immutable
3330 * array with a mutable one.
3333 bool arrayTypeCompatible(Loc loc
, Type
*t1
, Type
*t2
)
3335 t1
= t1
->toBasetype()->merge2();
3336 t2
= t2
->toBasetype()->merge2();
3338 if ((t1
->ty
== Tarray
|| t1
->ty
== Tsarray
|| t1
->ty
== Tpointer
) &&
3339 (t2
->ty
== Tarray
|| t2
->ty
== Tsarray
|| t2
->ty
== Tpointer
))
3341 if (t1
->nextOf()->implicitConvTo(t2
->nextOf()) < MATCHconst
&&
3342 t2
->nextOf()->implicitConvTo(t1
->nextOf()) < MATCHconst
&&
3343 (t1
->nextOf()->ty
!= Tvoid
&& t2
->nextOf()->ty
!= Tvoid
))
3345 error(loc
, "array equality comparison type mismatch, %s vs %s", t1
->toChars(), t2
->toChars());
3352 /***********************************
3353 * See if both types are arrays that can be compared
3354 * for equality without any casting. Return true if so.
3355 * This is to enable comparing things like an immutable
3356 * array with a mutable one.
3358 bool arrayTypeCompatibleWithoutCasting(Type
*t1
, Type
*t2
)
3360 t1
= t1
->toBasetype();
3361 t2
= t2
->toBasetype();
3363 if ((t1
->ty
== Tarray
|| t1
->ty
== Tsarray
|| t1
->ty
== Tpointer
) &&
3366 if (t1
->nextOf()->implicitConvTo(t2
->nextOf()) >= MATCHconst
||
3367 t2
->nextOf()->implicitConvTo(t1
->nextOf()) >= MATCHconst
)
3373 /******************************************************************/
3375 /* Determine the integral ranges of an expression.
3376 * This is used to determine if implicit narrowing conversions will
3380 IntRange
getIntRange(Expression
*e
)
3382 class IntRangeVisitor
: public Visitor
3385 static uinteger_t
getMask(uinteger_t v
)
3387 // Ref: http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
3397 // The algorithms for &, |, ^ are not yet the best! Sometimes they will produce
3398 // not the tightest bound. See
3399 // https://github.com/D-Programming-Language/dmd/pull/116
3401 static IntRange
unsignedBitwiseAnd(const IntRange
& a
, const IntRange
& b
)
3403 // the DiffMasks stores the mask of bits which are variable in the range.
3404 uinteger_t aDiffMask
= getMask(a
.imin
.value
^ a
.imax
.value
);
3405 uinteger_t bDiffMask
= getMask(b
.imin
.value
^ b
.imax
.value
);
3406 // Since '&' computes the digitwise-minimum, the we could set all varying
3407 // digits to 0 to get a lower bound, and set all varying digits to 1 to get
3410 result
.imin
.value
= (a
.imin
.value
& ~aDiffMask
) & (b
.imin
.value
& ~bDiffMask
);
3411 result
.imax
.value
= (a
.imax
.value
| aDiffMask
) & (b
.imax
.value
| bDiffMask
);
3412 // Sometimes the upper bound is overestimated. The upper bound will never
3413 // exceed the input.
3414 if (result
.imax
.value
> a
.imax
.value
)
3415 result
.imax
.value
= a
.imax
.value
;
3416 if (result
.imax
.value
> b
.imax
.value
)
3417 result
.imax
.value
= b
.imax
.value
;
3418 result
.imin
.negative
= result
.imax
.negative
= a
.imin
.negative
&& b
.imin
.negative
;
3421 static IntRange
unsignedBitwiseOr(const IntRange
& a
, const IntRange
& b
)
3423 // the DiffMasks stores the mask of bits which are variable in the range.
3424 uinteger_t aDiffMask
= getMask(a
.imin
.value
^ a
.imax
.value
);
3425 uinteger_t bDiffMask
= getMask(b
.imin
.value
^ b
.imax
.value
);
3426 // The imax algorithm by Adam D. Ruppe.
3427 // http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=108796
3429 result
.imin
.value
= (a
.imin
.value
& ~aDiffMask
) | (b
.imin
.value
& ~bDiffMask
);
3430 result
.imax
.value
= a
.imax
.value
| b
.imax
.value
| getMask(a
.imax
.value
& b
.imax
.value
);
3431 // Sometimes the lower bound is underestimated. The lower bound will never
3432 // less than the input.
3433 if (result
.imin
.value
< a
.imin
.value
)
3434 result
.imin
.value
= a
.imin
.value
;
3435 if (result
.imin
.value
< b
.imin
.value
)
3436 result
.imin
.value
= b
.imin
.value
;
3437 result
.imin
.negative
= result
.imax
.negative
= a
.imin
.negative
|| b
.imin
.negative
;
3440 static IntRange
unsignedBitwiseXor(const IntRange
& a
, const IntRange
& b
)
3442 // the DiffMasks stores the mask of bits which are variable in the range.
3443 uinteger_t aDiffMask
= getMask(a
.imin
.value
^ a
.imax
.value
);
3444 uinteger_t bDiffMask
= getMask(b
.imin
.value
^ b
.imax
.value
);
3446 result
.imin
.value
= (a
.imin
.value
^ b
.imin
.value
) & ~(aDiffMask
| bDiffMask
);
3447 result
.imax
.value
= (a
.imax
.value
^ b
.imax
.value
) | (aDiffMask
| bDiffMask
);
3448 result
.imin
.negative
= result
.imax
.negative
= a
.imin
.negative
!= b
.imin
.negative
;
3455 void visit(Expression
*e
)
3457 range
= IntRange::fromType(e
->type
);
3460 void visit(IntegerExp
*e
)
3462 range
= IntRange(SignExtendedNumber(e
->getInteger())).cast(e
->type
);
3465 void visit(CastExp
*e
)
3467 range
= getIntRange(e
->e1
).cast(e
->type
);
3470 void visit(AddExp
*e
)
3472 IntRange ir1
= getIntRange(e
->e1
);
3473 IntRange ir2
= getIntRange(e
->e2
);
3474 range
= IntRange(ir1
.imin
+ ir2
.imin
, ir1
.imax
+ ir2
.imax
).cast(e
->type
);
3477 void visit(MinExp
*e
)
3479 IntRange ir1
= getIntRange(e
->e1
);
3480 IntRange ir2
= getIntRange(e
->e2
);
3481 range
= IntRange(ir1
.imin
- ir2
.imax
, ir1
.imax
- ir2
.imin
).cast(e
->type
);
3484 void visit(DivExp
*e
)
3486 IntRange ir1
= getIntRange(e
->e1
);
3487 IntRange ir2
= getIntRange(e
->e2
);
3489 // Should we ignore the possibility of div-by-0???
3490 if (ir2
.containsZero())
3492 visit((Expression
*)e
);
3496 // [a,b] / [c,d] = [min (a/c, a/d, b/c, b/d), max (a/c, a/d, b/c, b/d)]
3497 SignExtendedNumber bdy
[4];
3498 bdy
[0] = ir1
.imin
/ ir2
.imin
;
3499 bdy
[1] = ir1
.imin
/ ir2
.imax
;
3500 bdy
[2] = ir1
.imax
/ ir2
.imin
;
3501 bdy
[3] = ir1
.imax
/ ir2
.imax
;
3502 range
= IntRange::fromNumbers4(bdy
).cast(e
->type
);
3505 void visit(MulExp
*e
)
3507 IntRange ir1
= getIntRange(e
->e1
);
3508 IntRange ir2
= getIntRange(e
->e2
);
3510 // [a,b] * [c,d] = [min (ac, ad, bc, bd), max (ac, ad, bc, bd)]
3511 SignExtendedNumber bdy
[4];
3512 bdy
[0] = ir1
.imin
* ir2
.imin
;
3513 bdy
[1] = ir1
.imin
* ir2
.imax
;
3514 bdy
[2] = ir1
.imax
* ir2
.imin
;
3515 bdy
[3] = ir1
.imax
* ir2
.imax
;
3516 range
= IntRange::fromNumbers4(bdy
).cast(e
->type
);
3519 void visit(ModExp
*e
)
3521 IntRange irNum
= getIntRange(e
->e1
);
3522 IntRange irDen
= getIntRange(e
->e2
).absNeg();
3525 due to the rules of D (C)'s % operator, we need to consider the cases
3526 separately in different range of signs.
3528 case 1. [500, 1700] % [7, 23] (numerator is always positive)
3530 case 2. [-500, 1700] % [7, 23] (numerator can be negative)
3532 case 3. [-1700, -500] % [7, 23] (numerator is always negative)
3535 the number 22 is the maximum absolute value in the denomator's range. We
3536 don't care about divide by zero.
3539 // Modding on 0 is invalid anyway.
3540 if (!irDen
.imin
.negative
)
3542 visit((Expression
*)e
);
3547 irDen
.imax
= -irDen
.imin
;
3549 if (!irNum
.imin
.negative
)
3550 irNum
.imin
.value
= 0;
3551 else if (irNum
.imin
< irDen
.imin
)
3552 irNum
.imin
= irDen
.imin
;
3554 if (irNum
.imax
.negative
)
3556 irNum
.imax
.negative
= false;
3557 irNum
.imax
.value
= 0;
3559 else if (irNum
.imax
> irDen
.imax
)
3560 irNum
.imax
= irDen
.imax
;
3562 range
= irNum
.cast(e
->type
);
3565 void visit(AndExp
*e
)
3567 IntRange ir1
= getIntRange(e
->e1
);
3568 IntRange ir2
= getIntRange(e
->e2
);
3570 IntRange ir1neg
, ir1pos
, ir2neg
, ir2pos
;
3571 bool has1neg
, has1pos
, has2neg
, has2pos
;
3573 ir1
.splitBySign(ir1neg
, has1neg
, ir1pos
, has1pos
);
3574 ir2
.splitBySign(ir2neg
, has2neg
, ir2pos
, has2pos
);
3577 bool hasResult
= false;
3578 if (has1pos
&& has2pos
)
3579 result
.unionOrAssign(unsignedBitwiseAnd(ir1pos
, ir2pos
), hasResult
);
3580 if (has1pos
&& has2neg
)
3581 result
.unionOrAssign(unsignedBitwiseAnd(ir1pos
, ir2neg
), hasResult
);
3582 if (has1neg
&& has2pos
)
3583 result
.unionOrAssign(unsignedBitwiseAnd(ir1neg
, ir2pos
), hasResult
);
3584 if (has1neg
&& has2neg
)
3585 result
.unionOrAssign(unsignedBitwiseAnd(ir1neg
, ir2neg
), hasResult
);
3587 range
= result
.cast(e
->type
);
3590 void visit(OrExp
*e
)
3592 IntRange ir1
= getIntRange(e
->e1
);
3593 IntRange ir2
= getIntRange(e
->e2
);
3595 IntRange ir1neg
, ir1pos
, ir2neg
, ir2pos
;
3596 bool has1neg
, has1pos
, has2neg
, has2pos
;
3598 ir1
.splitBySign(ir1neg
, has1neg
, ir1pos
, has1pos
);
3599 ir2
.splitBySign(ir2neg
, has2neg
, ir2pos
, has2pos
);
3602 bool hasResult
= false;
3603 if (has1pos
&& has2pos
)
3604 result
.unionOrAssign(unsignedBitwiseOr(ir1pos
, ir2pos
), hasResult
);
3605 if (has1pos
&& has2neg
)
3606 result
.unionOrAssign(unsignedBitwiseOr(ir1pos
, ir2neg
), hasResult
);
3607 if (has1neg
&& has2pos
)
3608 result
.unionOrAssign(unsignedBitwiseOr(ir1neg
, ir2pos
), hasResult
);
3609 if (has1neg
&& has2neg
)
3610 result
.unionOrAssign(unsignedBitwiseOr(ir1neg
, ir2neg
), hasResult
);
3613 range
= result
.cast(e
->type
);
3616 void visit(XorExp
*e
)
3618 IntRange ir1
= getIntRange(e
->e1
);
3619 IntRange ir2
= getIntRange(e
->e2
);
3621 IntRange ir1neg
, ir1pos
, ir2neg
, ir2pos
;
3622 bool has1neg
, has1pos
, has2neg
, has2pos
;
3624 ir1
.splitBySign(ir1neg
, has1neg
, ir1pos
, has1pos
);
3625 ir2
.splitBySign(ir2neg
, has2neg
, ir2pos
, has2pos
);
3628 bool hasResult
= false;
3629 if (has1pos
&& has2pos
)
3630 result
.unionOrAssign(unsignedBitwiseXor(ir1pos
, ir2pos
), hasResult
);
3631 if (has1pos
&& has2neg
)
3632 result
.unionOrAssign(unsignedBitwiseXor(ir1pos
, ir2neg
), hasResult
);
3633 if (has1neg
&& has2pos
)
3634 result
.unionOrAssign(unsignedBitwiseXor(ir1neg
, ir2pos
), hasResult
);
3635 if (has1neg
&& has2neg
)
3636 result
.unionOrAssign(unsignedBitwiseXor(ir1neg
, ir2neg
), hasResult
);
3639 range
= result
.cast(e
->type
);
3642 void visit(ShlExp
*e
)
3644 IntRange ir1
= getIntRange(e
->e1
);
3645 IntRange ir2
= getIntRange(e
->e2
);
3647 if (ir2
.imin
.negative
)
3648 ir2
= IntRange(SignExtendedNumber(0), SignExtendedNumber(64));
3650 SignExtendedNumber lower
= ir1
.imin
<< (ir1
.imin
.negative
? ir2
.imax
: ir2
.imin
);
3651 SignExtendedNumber upper
= ir1
.imax
<< (ir1
.imax
.negative
? ir2
.imin
: ir2
.imax
);
3653 range
= IntRange(lower
, upper
).cast(e
->type
);
3656 void visit(ShrExp
*e
)
3658 IntRange ir1
= getIntRange(e
->e1
);
3659 IntRange ir2
= getIntRange(e
->e2
);
3661 if (ir2
.imin
.negative
)
3662 ir2
= IntRange(SignExtendedNumber(0), SignExtendedNumber(64));
3664 SignExtendedNumber lower
= ir1
.imin
>> (ir1
.imin
.negative
? ir2
.imin
: ir2
.imax
);
3665 SignExtendedNumber upper
= ir1
.imax
>> (ir1
.imax
.negative
? ir2
.imax
: ir2
.imin
);
3667 range
= IntRange(lower
, upper
).cast(e
->type
);
3670 void visit(UshrExp
*e
)
3672 IntRange ir1
= getIntRange(e
->e1
).castUnsigned(e
->e1
->type
);
3673 IntRange ir2
= getIntRange(e
->e2
);
3675 if (ir2
.imin
.negative
)
3676 ir2
= IntRange(SignExtendedNumber(0), SignExtendedNumber(64));
3678 range
= IntRange(ir1
.imin
>> ir2
.imax
, ir1
.imax
>> ir2
.imin
).cast(e
->type
);
3681 void visit(AssignExp
*e
)
3683 range
= getIntRange(e
->e2
).cast(e
->type
);
3686 void visit(CondExp
*e
)
3688 // No need to check e->econd; assume caller has called optimize()
3689 IntRange ir1
= getIntRange(e
->e1
);
3690 IntRange ir2
= getIntRange(e
->e2
);
3691 range
= ir1
.unionWith(ir2
).cast(e
->type
);
3694 void visit(VarExp
*e
)
3697 VarDeclaration
* vd
= e
->var
->isVarDeclaration();
3698 if (vd
&& vd
->range
)
3699 range
= vd
->range
->cast(e
->type
);
3700 else if (vd
&& vd
->_init
&& !vd
->type
->isMutable() &&
3701 (ie
= vd
->getConstInitializer()) != NULL
)
3704 visit((Expression
*)e
);
3707 void visit(CommaExp
*e
)
3709 e
->e2
->accept(this);
3712 void visit(ComExp
*e
)
3714 IntRange ir
= getIntRange(e
->e1
);
3715 range
= IntRange(SignExtendedNumber(~ir
.imax
.value
, !ir
.imax
.negative
),
3716 SignExtendedNumber(~ir
.imin
.value
, !ir
.imin
.negative
)).cast(e
->type
);
3719 void visit(NegExp
*e
)
3721 IntRange ir
= getIntRange(e
->e1
);
3722 range
= IntRange(-ir
.imax
, -ir
.imin
).cast(e
->type
);