]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/dmd/dcast.c
2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2019 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
);
989 void visit(AndExp
*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(OrExp
*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(XorExp
*e
)
1017 visit((Expression
*)e
);
1018 if (result
!= MATCHnomatch
)
1021 MATCH m1
= e
->e1
->implicitConvTo(t
);
1022 MATCH m2
= e
->e2
->implicitConvTo(t
);
1024 // Pick the worst match
1025 result
= (m1
< m2
) ? m1
: m2
;
1028 void visit(CondExp
*e
)
1030 MATCH m1
= e
->e1
->implicitConvTo(t
);
1031 MATCH m2
= e
->e2
->implicitConvTo(t
);
1032 //printf("CondExp: m1 %d m2 %d\n", m1, m2);
1034 // Pick the worst match
1035 result
= (m1
< m2
) ? m1
: m2
;
1038 void visit(CommaExp
*e
)
1040 e
->e2
->accept(this);
1043 void visit(CastExp
*e
)
1045 result
= e
->type
->implicitConvTo(t
);
1046 if (result
!= MATCHnomatch
)
1049 if (t
->isintegral() &&
1050 e
->e1
->type
->isintegral() &&
1051 e
->e1
->implicitConvTo(t
) != MATCHnomatch
)
1052 result
= MATCHconvert
;
1054 visit((Expression
*)e
);
1057 void visit(NewExp
*e
)
1059 visit((Expression
*)e
);
1060 if (result
!= MATCHnomatch
)
1063 /* Calling new() is like calling a pure function. We can implicitly convert the
1064 * return from new() to t using the same algorithm as in CallExp, with the function
1065 * 'arguments' being:
1070 * 'member' and 'allocator' need to be pure.
1073 /* See if fail only because of mod bits
1075 if (e
->type
->immutableOf()->implicitConvTo(t
->immutableOf()) == MATCHnomatch
)
1078 /* Get mod bits of what we're converting to
1080 Type
*tb
= t
->toBasetype();
1082 if (Type
*ti
= getIndirection(t
))
1085 return; // not sure what to do with this
1087 /* Apply mod bits to each argument,
1088 * and see if we can convert the argument to the modded type
1093 /* Treat 'this' as just another function argument
1095 Type
*targ
= e
->thisexp
->type
;
1096 if (targ
->constConv(targ
->castMod(mod
)) == MATCHnomatch
)
1100 /* Check call to 'allocator', then 'member'
1102 FuncDeclaration
*fd
= e
->allocator
;
1103 for (int count
= 0; count
< 2; ++count
, (fd
= e
->member
))
1107 if (fd
->errors
|| fd
->type
->ty
!= Tfunction
)
1109 TypeFunction
*tf
= (TypeFunction
*)fd
->type
;
1110 if (tf
->purity
== PUREimpure
)
1113 if (fd
== e
->member
)
1115 if (e
->type
->immutableOf()->implicitConvTo(t
) < MATCHconst
&&
1116 e
->type
->addMod(MODshared
)->implicitConvTo(t
) < MATCHconst
&&
1117 e
->type
->implicitConvTo(t
->addMod(MODshared
)) < MATCHconst
)
1121 // Allow a conversion to immutable type, or
1122 // conversions of mutable types between thread-local and shared.
1125 Expressions
*args
= (fd
== e
->allocator
) ? e
->newargs
: e
->arguments
;
1127 size_t nparams
= Parameter::dim(tf
->parameters
);
1128 size_t j
= (tf
->linkage
== LINKd
&& tf
->varargs
== 1); // if TypeInfoArray was prepended
1129 for (size_t i
= j
; i
< e
->arguments
->dim
; ++i
)
1131 Expression
*earg
= (*args
)[i
];
1132 Type
*targ
= earg
->type
->toBasetype();
1133 if (i
- j
< nparams
)
1135 Parameter
*fparam
= Parameter::getNth(tf
->parameters
, i
- j
);
1136 if (fparam
->storageClass
& STClazy
)
1137 return; // not sure what to do with this
1138 Type
*tparam
= fparam
->type
;
1141 if (fparam
->storageClass
& (STCout
| STCref
))
1143 if (targ
->constConv(tparam
->castMod(mod
)) == MATCHnomatch
)
1149 if (implicitMod(earg
, targ
, mod
) == MATCHnomatch
)
1154 /* If no 'member', then construction is by simple assignment,
1155 * and just straight check 'arguments'
1157 if (!e
->member
&& e
->arguments
)
1159 for (size_t i
= 0; i
< e
->arguments
->dim
; ++i
)
1161 Expression
*earg
= (*e
->arguments
)[i
];
1162 if (!earg
) // Bugzilla 14853: if it's on overlapped field
1164 Type
*targ
= earg
->type
->toBasetype();
1165 if (implicitMod(earg
, targ
, mod
) == MATCHnomatch
)
1170 /* Consider the .init expression as an argument
1172 Type
*ntb
= e
->newtype
->toBasetype();
1173 if (ntb
->ty
== Tarray
)
1174 ntb
= ntb
->nextOf()->toBasetype();
1175 if (ntb
->ty
== Tstruct
)
1177 // Don't allow nested structs - uplevel reference may not be convertible
1178 StructDeclaration
*sd
= ((TypeStruct
*)ntb
)->sym
;
1179 sd
->size(e
->loc
); // resolve any forward references
1183 if (ntb
->isZeroInit(e
->loc
))
1185 /* Zeros are implicitly convertible, except for special cases.
1187 if (ntb
->ty
== Tclass
)
1189 /* With new() must look at the class instance initializer.
1191 ClassDeclaration
*cd
= ((TypeClass
*)ntb
)->sym
;
1193 cd
->size(e
->loc
); // resolve any forward references
1196 return; // uplevel reference may not be convertible
1198 assert(!cd
->isInterfaceDeclaration());
1202 static bool convertible(Loc loc
, ClassDeclaration
*cd
, MOD mod
)
1204 for (size_t i
= 0; i
< cd
->fields
.dim
; i
++)
1206 VarDeclaration
*v
= cd
->fields
[i
];
1207 Initializer
*init
= v
->_init
;
1210 if (init
->isVoidInitializer())
1212 else if (ExpInitializer
*ei
= init
->isExpInitializer())
1214 Type
*tb
= v
->type
->toBasetype();
1215 if (implicitMod(ei
->exp
, tb
, mod
) == MATCHnomatch
)
1220 /* Enhancement: handle StructInitializer and ArrayInitializer
1225 else if (!v
->type
->isZeroInit(loc
))
1228 return cd
->baseClass
? convertible(loc
, cd
->baseClass
, mod
) : true;
1232 if (!ClassCheck::convertible(e
->loc
, cd
, mod
))
1238 Expression
*earg
= e
->newtype
->defaultInitLiteral(e
->loc
);
1239 Type
*targ
= e
->newtype
->toBasetype();
1241 if (implicitMod(earg
, targ
, mod
) == MATCHnomatch
)
1247 result
= MATCHconst
;
1250 void visit(SliceExp
*e
)
1252 //printf("SliceExp::implicitConvTo e = %s, type = %s\n", e->toChars(), e->type->toChars());
1253 visit((Expression
*)e
);
1254 if (result
!= MATCHnomatch
)
1257 Type
*tb
= t
->toBasetype();
1258 Type
*typeb
= e
->type
->toBasetype();
1259 if (tb
->ty
== Tsarray
&& typeb
->ty
== Tarray
)
1261 typeb
= toStaticArrayType(e
);
1263 result
= typeb
->implicitConvTo(t
);
1267 /* If the only reason it won't convert is because of the mod bits,
1268 * then test for conversion by seeing if e1 can be converted with those
1271 Type
*t1b
= e
->e1
->type
->toBasetype();
1272 if (tb
->ty
== Tarray
&& typeb
->equivalent(tb
))
1274 Type
*tbn
= tb
->nextOf();
1277 /* If e->e1 is dynamic array or pointer, the uniqueness of e->e1
1278 * is equivalent with the uniqueness of the referred data. And in here
1279 * we can have arbitrary typed reference for that.
1281 if (t1b
->ty
== Tarray
)
1282 tx
= tbn
->arrayOf();
1283 if (t1b
->ty
== Tpointer
)
1284 tx
= tbn
->pointerTo();
1286 /* If e->e1 is static array, at least it should be an rvalue.
1287 * If not, e->e1 is a reference, and its uniqueness does not link
1288 * to the uniqueness of the referred data.
1290 if (t1b
->ty
== Tsarray
&& !e
->e1
->isLvalue())
1291 tx
= tbn
->sarrayOf(t1b
->size() / tbn
->size());
1295 result
= e
->e1
->implicitConvTo(tx
);
1296 if (result
> MATCHconst
) // Match level is MATCHconst at best.
1297 result
= MATCHconst
;
1301 // Enhancement 10724
1302 if (tb
->ty
== Tpointer
&& e
->e1
->op
== TOKstring
)
1303 e
->e1
->accept(this);
1307 ImplicitConvTo
v(t
);
1312 Type
*toStaticArrayType(SliceExp
*e
)
1314 if (e
->lwr
&& e
->upr
)
1316 // For the following code to work, e should be optimized beforehand.
1317 // (eg. $ in lwr and upr should be already resolved, if possible)
1318 Expression
*lwr
= e
->lwr
->optimize(WANTvalue
);
1319 Expression
*upr
= e
->upr
->optimize(WANTvalue
);
1320 if (lwr
->isConst() && upr
->isConst())
1322 size_t len
= (size_t)(upr
->toUInteger() - lwr
->toUInteger());
1323 return e
->type
->toBasetype()->nextOf()->sarrayOf(len
);
1328 Type
*t1b
= e
->e1
->type
->toBasetype();
1329 if (t1b
->ty
== Tsarray
)
1335 /* ==================== castTo ====================== */
1337 /**************************************
1338 * Do an explicit cast.
1339 * Assume that the 'this' expression does not have any indirections.
1342 Expression
*castTo(Expression
*e
, Scope
*sc
, Type
*t
)
1344 class CastTo
: public Visitor
1351 CastTo(Scope
*sc
, Type
*t
)
1357 void visit(Expression
*e
)
1359 //printf("Expression::castTo(this=%s, t=%s)\n", e->toChars(), t->toChars());
1360 if (e
->type
->equals(t
))
1365 if (e
->op
== TOKvar
)
1367 VarDeclaration
*v
= ((VarExp
*)e
)->var
->isVarDeclaration();
1368 if (v
&& v
->storage_class
& STCmanifest
)
1370 result
= e
->ctfeInterpret();
1371 result
= result
->castTo(sc
, t
);
1376 Type
*tob
= t
->toBasetype();
1377 Type
*t1b
= e
->type
->toBasetype();
1378 if (tob
->equals(t1b
))
1380 result
= e
->copy(); // because of COW for assignment to e->type
1385 /* Make semantic error against invalid cast between concrete types.
1386 * Assume that 'e' is never be any placeholder expressions.
1387 * The result of these checks should be consistent with CastExp::toElem().
1391 const bool tob_isFV
= (tob
->ty
== Tstruct
|| tob
->ty
== Tsarray
|| tob
->ty
== Tvector
);
1392 const bool t1b_isFV
= (t1b
->ty
== Tstruct
|| t1b
->ty
== Tsarray
|| t1b
->ty
== Tvector
);
1394 // Fat Reference types
1395 const bool tob_isFR
= (tob
->ty
== Tarray
|| tob
->ty
== Tdelegate
);
1396 const bool t1b_isFR
= (t1b
->ty
== Tarray
|| t1b
->ty
== Tdelegate
);
1399 const bool tob_isR
= (tob_isFR
|| tob
->ty
== Tpointer
|| tob
->ty
== Taarray
|| tob
->ty
== Tclass
);
1400 const bool t1b_isR
= (t1b_isFR
|| t1b
->ty
== Tpointer
|| t1b
->ty
== Taarray
|| t1b
->ty
== Tclass
);
1402 // Arithmetic types (== valueable basic types)
1403 const bool tob_isA
= ((tob
->isintegral() || tob
->isfloating()) && tob
->ty
!= Tvector
);
1404 const bool t1b_isA
= ((t1b
->isintegral() || t1b
->isfloating()) && t1b
->ty
!= Tvector
);
1406 if (AggregateDeclaration
*t1ad
= isAggregate(t1b
))
1408 AggregateDeclaration
*toad
= isAggregate(tob
);
1409 if (t1ad
!= toad
&& t1ad
->aliasthis
)
1411 if (t1b
->ty
== Tclass
&& tob
->ty
== Tclass
)
1413 ClassDeclaration
*t1cd
= t1b
->isClassHandle();
1414 ClassDeclaration
*tocd
= tob
->isClassHandle();
1416 if (tocd
->isBaseOf(t1cd
, &offset
))
1420 /* Forward the cast to our alias this member, rewrite to:
1421 * cast(to)e1.aliasthis
1423 result
= resolveAliasThis(sc
, e
);
1424 result
= result
->castTo(sc
, t
);
1428 else if (tob
->ty
== Tvector
&& t1b
->ty
!= Tvector
)
1430 //printf("test1 e = %s, e->type = %s, tob = %s\n", e->toChars(), e->type->toChars(), tob->toChars());
1431 TypeVector
*tv
= (TypeVector
*)tob
;
1432 result
= new CastExp(e
->loc
, e
, tv
->elementType());
1433 result
= new VectorExp(e
->loc
, result
, tob
);
1434 result
= ::semantic(result
, sc
);
1437 else if (tob
->ty
!= Tvector
&& t1b
->ty
== Tvector
)
1439 // T[n] <-- __vector(U[m])
1440 if (tob
->ty
== Tsarray
)
1442 if (t1b
->size(e
->loc
) == tob
->size(e
->loc
))
1447 else if (t1b
->implicitConvTo(tob
) == MATCHconst
&& t
->equals(e
->type
->constOf()))
1454 // arithmetic values vs. other arithmetic values
1455 // arithmetic values vs. T*
1456 if ((tob_isA
&& (t1b_isA
|| t1b
->ty
== Tpointer
)) ||
1457 (t1b_isA
&& (tob_isA
|| tob
->ty
== Tpointer
)))
1462 // arithmetic values vs. references or fat values
1463 if ((tob_isA
&& (t1b_isR
|| t1b_isFV
)) ||
1464 (t1b_isA
&& (tob_isR
|| tob_isFV
)))
1469 // Bugzlla 3133: A cast between fat values is possible only when the sizes match.
1470 if (tob_isFV
&& t1b_isFV
)
1472 if (t1b
->size(e
->loc
) == tob
->size(e
->loc
))
1474 e
->error("cannot cast expression %s of type %s to %s because of different sizes",
1475 e
->toChars(), e
->type
->toChars(), t
->toChars());
1476 result
= new ErrorExp();
1480 // Fat values vs. null or references
1481 if ((tob_isFV
&& (t1b
->ty
== Tnull
|| t1b_isR
)) ||
1482 (t1b_isFV
&& (tob
->ty
== Tnull
|| tob_isR
)))
1484 if (tob
->ty
== Tpointer
&& t1b
->ty
== Tsarray
)
1487 // cast(U*)sa; // ==> cast(U*)sa.ptr;
1488 result
= new AddrExp(e
->loc
, e
, t
);
1491 if (tob
->ty
== Tarray
&& t1b
->ty
== Tsarray
)
1494 // cast(U[])sa; // ==> cast(U[])sa[];
1495 d_uns64 fsize
= t1b
->nextOf()->size();
1496 d_uns64 tsize
= tob
->nextOf()->size();
1497 if ((((TypeSArray
*)t1b
)->dim
->toInteger() * fsize
) % tsize
!= 0)
1499 // copied from sarray_toDarray() in e2ir.c
1500 e
->error("cannot cast expression %s of type %s to %s since sizes don't line up",
1501 e
->toChars(), e
->type
->toChars(), t
->toChars());
1502 result
= new ErrorExp();
1510 /* For references, any reinterpret casts are allowed to same 'ty' type.
1512 * R1 function(P1) to R2 function(P2)
1513 * R1 delegate(P1) to R2 delegate(P2)
1516 * class/interface A to B (will be a dynamic cast if possible)
1518 if (tob
->ty
== t1b
->ty
&& tob_isR
&& t1b_isR
)
1521 // typeof(null) <-- non-null references or values
1522 if (tob
->ty
== Tnull
&& t1b
->ty
!= Tnull
)
1523 goto Lfail
; // Bugzilla 14629
1524 // typeof(null) --> non-null references or arithmetic values
1525 if (t1b
->ty
== Tnull
&& tob
->ty
!= Tnull
)
1528 // Check size mismatch of references.
1529 // Tarray and Tdelegate are (void*).sizeof*2, but others have (void*).sizeof.
1530 if ((tob_isFR
&& t1b_isR
) || (t1b_isFR
&& tob_isR
))
1532 if (tob
->ty
== Tpointer
&& t1b
->ty
== Tarray
)
1535 // cast(U*)da; // ==> cast(U*)da.ptr;
1538 if (tob
->ty
== Tpointer
&& t1b
->ty
== Tdelegate
)
1540 // void delegate() dg;
1541 // cast(U*)dg; // ==> cast(U*)dg.ptr;
1542 // Note that it happens even when U is a Tfunction!
1543 e
->deprecation("casting from %s to %s is deprecated", e
->type
->toChars(), t
->toChars());
1549 if (t1b
->ty
== Tvoid
&& tob
->ty
!= Tvoid
)
1552 e
->error("cannot cast expression %s of type %s to %s",
1553 e
->toChars(), e
->type
->toChars(), t
->toChars());
1554 result
= new ErrorExp();
1559 result
= new CastExp(e
->loc
, e
, t
);
1560 result
->type
= t
; // Don't call semantic()
1561 //printf("Returning: %s\n", result->toChars());
1564 void visit(ErrorExp
*e
)
1569 void visit(RealExp
*e
)
1571 if (!e
->type
->equals(t
))
1573 if ((e
->type
->isreal() && t
->isreal()) ||
1574 (e
->type
->isimaginary() && t
->isimaginary())
1581 visit((Expression
*)e
);
1587 void visit(ComplexExp
*e
)
1589 if (!e
->type
->equals(t
))
1591 if (e
->type
->iscomplex() && t
->iscomplex())
1597 visit((Expression
*)e
);
1603 void visit(NullExp
*e
)
1605 //printf("NullExp::castTo(t = %s) %s\n", t->toChars(), toChars());
1606 visit((Expression
*)e
);
1607 if (result
->op
== TOKnull
)
1609 NullExp
*ex
= (NullExp
*)result
;
1615 void visit(StructLiteralExp
*e
)
1617 visit((Expression
*)e
);
1618 if (result
->op
== TOKstructliteral
)
1619 ((StructLiteralExp
*)result
)->stype
= t
; // commit type
1622 void visit(StringExp
*e
)
1624 /* This follows copy-on-write; any changes to 'this'
1625 * will result in a copy.
1626 * The this->string member is considered immutable.
1630 //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), e->toChars(), e->committed);
1632 if (!e
->committed
&& t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tvoid
)
1634 e
->error("cannot convert string literal to void*");
1635 result
= new ErrorExp();
1642 se
= (StringExp
*)e
->copy();
1647 if (e
->type
->equals(t
))
1653 Type
*tb
= t
->toBasetype();
1654 //printf("\ttype = %s\n", e->type->toChars());
1655 if (tb
->ty
== Tdelegate
&& e
->type
->toBasetype()->ty
!= Tdelegate
)
1657 visit((Expression
*)e
);
1661 Type
*typeb
= e
->type
->toBasetype();
1662 if (typeb
->equals(tb
))
1666 se
= (StringExp
*)e
->copy();
1674 /* Handle reinterpret casts:
1675 * cast(wchar[3])"abcd"c --> [\u6261, \u6463, \u0000]
1676 * cast(wchar[2])"abcd"c --> [\u6261, \u6463]
1677 * cast(wchar[1])"abcd"c --> [\u6261]
1679 if (e
->committed
&& tb
->ty
== Tsarray
&& typeb
->ty
== Tarray
)
1681 se
= (StringExp
*)e
->copy();
1682 d_uns64 szx
= tb
->nextOf()->size();
1684 se
->sz
= (unsigned char)szx
;
1685 se
->len
= (size_t)((TypeSArray
*)tb
)->dim
->toInteger();
1689 /* Assure space for terminating 0
1691 if ((se
->len
+ 1) * se
->sz
> (e
->len
+ 1) * e
->sz
)
1693 void *s
= (void *)mem
.xmalloc((se
->len
+ 1) * se
->sz
);
1694 memcpy(s
, se
->string
, se
->len
* se
->sz
);
1695 memset((char *)s
+ se
->len
* se
->sz
, 0, se
->sz
);
1702 if (tb
->ty
!= Tsarray
&& tb
->ty
!= Tarray
&& tb
->ty
!= Tpointer
)
1706 se
= (StringExp
*)e
->copy();
1711 if (typeb
->ty
!= Tsarray
&& typeb
->ty
!= Tarray
&& typeb
->ty
!= Tpointer
)
1715 se
= (StringExp
*)e
->copy();
1721 if (typeb
->nextOf()->size() == tb
->nextOf()->size())
1725 se
= (StringExp
*)e
->copy();
1728 if (tb
->ty
== Tsarray
)
1729 goto L2
; // handle possible change in static array dimension
1738 #define X(tf,tt) ((int)(tf) * 256 + (int)(tt))
1742 int tfty
= typeb
->nextOf()->toBasetype()->ty
;
1743 int ttty
= tb
->nextOf()->toBasetype()->ty
;
1744 switch (X(tfty
, ttty
))
1746 case X(Tchar
, Tchar
):
1747 case X(Twchar
,Twchar
):
1748 case X(Tdchar
,Tdchar
):
1751 case X(Tchar
, Twchar
):
1752 for (size_t u
= 0; u
< e
->len
;)
1755 const char *p
= utf_decodeChar((utf8_t
*)se
->string
, e
->len
, &u
, &c
);
1759 buffer
.writeUTF16(c
);
1761 newlen
= buffer
.offset
/ 2;
1762 buffer
.writeUTF16(0);
1765 case X(Tchar
, Tdchar
):
1766 for (size_t u
= 0; u
< e
->len
;)
1769 const char *p
= utf_decodeChar((utf8_t
*)se
->string
, e
->len
, &u
, &c
);
1778 case X(Twchar
,Tchar
):
1779 for (size_t u
= 0; u
< e
->len
;)
1782 const char *p
= utf_decodeWchar((unsigned short *)se
->string
, e
->len
, &u
, &c
);
1786 buffer
.writeUTF8(c
);
1788 newlen
= buffer
.offset
;
1789 buffer
.writeUTF8(0);
1792 case X(Twchar
,Tdchar
):
1793 for (size_t u
= 0; u
< e
->len
;)
1796 const char *p
= utf_decodeWchar((unsigned short *)se
->string
, e
->len
, &u
, &c
);
1805 case X(Tdchar
,Tchar
):
1806 for (size_t u
= 0; u
< e
->len
; u
++)
1808 unsigned c
= ((unsigned *)se
->string
)[u
];
1809 if (!utf_isValidDchar(c
))
1810 e
->error("invalid UCS-32 char \\U%08x", c
);
1812 buffer
.writeUTF8(c
);
1815 newlen
= buffer
.offset
;
1816 buffer
.writeUTF8(0);
1819 case X(Tdchar
,Twchar
):
1820 for (size_t u
= 0; u
< e
->len
; u
++)
1822 unsigned c
= ((unsigned *)se
->string
)[u
];
1823 if (!utf_isValidDchar(c
))
1824 e
->error("invalid UCS-32 char \\U%08x", c
);
1826 buffer
.writeUTF16(c
);
1829 newlen
= buffer
.offset
/ 2;
1830 buffer
.writeUTF16(0);
1836 se
= (StringExp
*)e
->copy();
1839 se
->string
= buffer
.extractData();
1843 d_uns64 szx
= tb
->nextOf()->size();
1845 se
->sz
= (unsigned char)szx
;
1850 assert(typeb
->nextOf()->size() != tb
->nextOf()->size());
1858 // See if need to truncate or extend the literal
1859 if (tb
->ty
== Tsarray
)
1861 size_t dim2
= (size_t)((TypeSArray
*)tb
)->dim
->toInteger();
1863 //printf("dim from = %d, to = %d\n", (int)se->len, (int)dim2);
1865 // Changing dimensions
1866 if (dim2
!= se
->len
)
1868 // Copy when changing the string literal
1869 size_t newsz
= se
->sz
;
1870 size_t d
= (dim2
< se
->len
) ? dim2
: se
->len
;
1871 void *s
= (void *)mem
.xmalloc((dim2
+ 1) * newsz
);
1872 memcpy(s
, se
->string
, d
* newsz
);
1873 // Extend with 0, add terminating 0
1874 memset((char *)s
+ d
* newsz
, 0, (dim2
+ 1 - d
) * newsz
);
1884 result
= new CastExp(e
->loc
, se
, t
);
1885 result
->type
= t
; // so semantic() won't be run on e
1888 void visit(AddrExp
*e
)
1894 tb
= t
->toBasetype();
1895 e
->type
= e
->type
->toBasetype();
1896 if (!tb
->equals(e
->type
))
1898 // Look for pointers to functions where the functions are overloaded.
1900 if (e
->e1
->op
== TOKoverloadset
&&
1901 (t
->ty
== Tpointer
|| t
->ty
== Tdelegate
) && t
->nextOf()->ty
== Tfunction
)
1903 OverExp
*eo
= (OverExp
*)e
->e1
;
1904 FuncDeclaration
*f
= NULL
;
1905 for (size_t i
= 0; i
< eo
->vars
->a
.dim
; i
++)
1907 Dsymbol
*s
= eo
->vars
->a
[i
];
1908 FuncDeclaration
*f2
= s
->isFuncDeclaration();
1910 if (f2
->overloadExactMatch(t
->nextOf()))
1914 /* Error if match in more than one overload set,
1915 * even if one is a 'better' match than the other.
1917 ScopeDsymbol::multiplyDefined(e
->loc
, f
, f2
);
1926 SymOffExp
*se
= new SymOffExp(e
->loc
, f
, 0, false);
1928 // Let SymOffExp::castTo() do the heavy lifting
1934 if (e
->type
->ty
== Tpointer
&& e
->type
->nextOf()->ty
== Tfunction
&&
1935 tb
->ty
== Tpointer
&& tb
->nextOf()->ty
== Tfunction
&&
1936 e
->e1
->op
== TOKvar
)
1938 VarExp
*ve
= (VarExp
*)e
->e1
;
1939 FuncDeclaration
*f
= ve
->var
->isFuncDeclaration();
1942 assert(f
->isImportedSymbol());
1943 f
= f
->overloadExactMatch(tb
->nextOf());
1946 result
= new VarExp(e
->loc
, f
, false);
1947 result
->type
= f
->type
;
1948 result
= new AddrExp(e
->loc
, result
, t
);
1954 if (FuncDeclaration
*f
= isFuncAddress(e
))
1956 if (f
->checkForwardRef(e
->loc
))
1958 result
= new ErrorExp();
1963 visit((Expression
*)e
);
1968 void visit(TupleExp
*e
)
1970 if (e
->type
->equals(t
))
1976 TupleExp
*te
= (TupleExp
*)e
->copy();
1977 te
->e0
= e
->e0
? e
->e0
->copy() : NULL
;
1978 te
->exps
= (Expressions
*)e
->exps
->copy();
1979 for (size_t i
= 0; i
< te
->exps
->dim
; i
++)
1981 Expression
*ex
= (*te
->exps
)[i
];
1982 ex
= ex
->castTo(sc
, t
);
1983 (*te
->exps
)[i
] = ex
;
1987 /* Questionable behavior: In here, result->type is not set to t.
1989 * TypeTuple!(int, int) values;
1990 * auto values2 = cast(long)values;
1991 * // typeof(values2) == TypeTuple!(int, int) !!
1993 * Only when the casted tuple is immediately expanded, it would work.
1994 * auto arr = [cast(long)values];
1995 * // typeof(arr) == long[]
1999 void visit(ArrayLiteralExp
*e
)
2006 ArrayLiteralExp
*ae
= e
;
2007 Type
*typeb
= e
->type
->toBasetype();
2008 Type
*tb
= t
->toBasetype();
2009 if ((tb
->ty
== Tarray
|| tb
->ty
== Tsarray
) &&
2010 (typeb
->ty
== Tarray
|| typeb
->ty
== Tsarray
))
2012 if (tb
->nextOf()->toBasetype()->ty
== Tvoid
&& typeb
->nextOf()->toBasetype()->ty
!= Tvoid
)
2014 // Don't do anything to cast non-void[] to void[]
2016 else if (typeb
->ty
== Tsarray
&& typeb
->nextOf()->toBasetype()->ty
== Tvoid
)
2018 // Don't do anything for casting void[n] to others
2022 if (tb
->ty
== Tsarray
)
2024 TypeSArray
*tsa
= (TypeSArray
*)tb
;
2025 if (e
->elements
->dim
!= tsa
->dim
->toInteger())
2029 ae
= (ArrayLiteralExp
*)e
->copy();
2031 ae
->basis
= e
->basis
->castTo(sc
, tb
->nextOf());
2032 ae
->elements
= e
->elements
->copy();
2033 for (size_t i
= 0; i
< e
->elements
->dim
; i
++)
2035 Expression
*ex
= (*e
->elements
)[i
];
2038 ex
= ex
->castTo(sc
, tb
->nextOf());
2039 (*ae
->elements
)[i
] = ex
;
2046 else if (tb
->ty
== Tpointer
&& typeb
->ty
== Tsarray
)
2048 Type
*tp
= typeb
->nextOf()->pointerTo();
2049 if (!tp
->equals(ae
->type
))
2051 ae
= (ArrayLiteralExp
*)e
->copy();
2055 else if (tb
->ty
== Tvector
&&
2056 (typeb
->ty
== Tarray
|| typeb
->ty
== Tsarray
))
2058 // Convert array literal to vector type
2059 TypeVector
*tv
= (TypeVector
*)tb
;
2060 TypeSArray
*tbase
= (TypeSArray
*)tv
->basetype
;
2061 assert(tbase
->ty
== Tsarray
);
2062 const size_t edim
= e
->elements
->dim
;
2063 const size_t tbasedim
= tbase
->dim
->toInteger();
2064 if (edim
> tbasedim
)
2067 ae
= (ArrayLiteralExp
*)e
->copy();
2068 ae
->type
= tbase
; // Bugzilla 12642
2069 ae
->elements
= e
->elements
->copy();
2070 Type
*telement
= tv
->elementType();
2071 for (size_t i
= 0; i
< edim
; i
++)
2073 Expression
*ex
= (*e
->elements
)[i
];
2074 ex
= ex
->castTo(sc
, telement
);
2075 (*ae
->elements
)[i
] = ex
;
2077 // Fill in the rest with the default initializer
2078 ae
->elements
->setDim(tbasedim
);
2079 for (size_t i
= edim
; i
< tbasedim
; i
++)
2081 Expression
*ex
= typeb
->nextOf()->defaultInitLiteral(e
->loc
);
2082 ex
= ex
->castTo(sc
, telement
);
2083 (*ae
->elements
)[i
] = ex
;
2085 Expression
*ev
= new VectorExp(e
->loc
, ae
, tb
);
2086 ev
= ::semantic(ev
, sc
);
2091 visit((Expression
*)ae
);
2094 void visit(AssocArrayLiteralExp
*e
)
2101 Type
*typeb
= e
->type
->toBasetype();
2102 Type
*tb
= t
->toBasetype();
2103 if (tb
->ty
== Taarray
&& typeb
->ty
== Taarray
&&
2104 tb
->nextOf()->toBasetype()->ty
!= Tvoid
)
2106 AssocArrayLiteralExp
*ae
= (AssocArrayLiteralExp
*)e
->copy();
2107 ae
->keys
= e
->keys
->copy();
2108 ae
->values
= e
->values
->copy();
2109 assert(e
->keys
->dim
== e
->values
->dim
);
2110 for (size_t i
= 0; i
< e
->keys
->dim
; i
++)
2112 Expression
*ex
= (*e
->values
)[i
];
2113 ex
= ex
->castTo(sc
, tb
->nextOf());
2114 (*ae
->values
)[i
] = ex
;
2117 ex
= ex
->castTo(sc
, ((TypeAArray
*)tb
)->index
);
2118 (*ae
->keys
)[i
] = ex
;
2124 visit((Expression
*)e
);
2127 void visit(SymOffExp
*e
)
2129 if (e
->type
== t
&& !e
->hasOverloads
)
2134 Type
*tb
= t
->toBasetype();
2135 Type
*typeb
= e
->type
->toBasetype();
2137 if (tb
->equals(typeb
))
2141 ((SymOffExp
*)result
)->hasOverloads
= false;
2145 // Look for pointers to functions where the functions are overloaded.
2146 if (e
->hasOverloads
&&
2147 typeb
->ty
== Tpointer
&& typeb
->nextOf()->ty
== Tfunction
&&
2148 (tb
->ty
== Tpointer
|| tb
->ty
== Tdelegate
) && tb
->nextOf()->ty
== Tfunction
)
2150 FuncDeclaration
*f
= e
->var
->isFuncDeclaration();
2151 f
= f
? f
->overloadExactMatch(tb
->nextOf()) : NULL
;
2154 if (tb
->ty
== Tdelegate
)
2156 if (f
->needThis() && hasThis(sc
))
2158 result
= new DelegateExp(e
->loc
, new ThisExp(e
->loc
), f
, false);
2159 result
= ::semantic(result
, sc
);
2161 else if (f
->isNested())
2163 result
= new DelegateExp(e
->loc
, new IntegerExp(0), f
, false);
2164 result
= ::semantic(result
, sc
);
2166 else if (f
->needThis())
2168 e
->error("no 'this' to create delegate for %s", f
->toChars());
2169 result
= new ErrorExp();
2174 e
->error("cannot cast from function pointer to delegate");
2175 result
= new ErrorExp();
2181 result
= new SymOffExp(e
->loc
, f
, 0, false);
2189 if (FuncDeclaration
*f
= isFuncAddress(e
))
2191 if (f
->checkForwardRef(e
->loc
))
2193 result
= new ErrorExp();
2198 visit((Expression
*)e
);
2201 void visit(DelegateExp
*e
)
2203 static const char msg
[] = "cannot form delegate due to covariant return type";
2205 Type
*tb
= t
->toBasetype();
2206 Type
*typeb
= e
->type
->toBasetype();
2207 if (!tb
->equals(typeb
) || e
->hasOverloads
)
2209 // Look for delegates to functions where the functions are overloaded.
2210 if (typeb
->ty
== Tdelegate
&&
2211 tb
->ty
== Tdelegate
)
2215 FuncDeclaration
*f
= e
->func
->overloadExactMatch(tb
->nextOf());
2219 if (f
->tintro
&& f
->tintro
->nextOf()->isBaseOf(f
->type
->nextOf(), &offset
) && offset
)
2220 e
->error("%s", msg
);
2221 if (f
!= e
->func
) // if address not already marked as taken
2223 result
= new DelegateExp(e
->loc
, e
->e1
, f
, false);
2227 if (e
->func
->tintro
)
2228 e
->error("%s", msg
);
2232 if (FuncDeclaration
*f
= isFuncAddress(e
))
2234 if (f
->checkForwardRef(e
->loc
))
2236 result
= new ErrorExp();
2241 visit((Expression
*)e
);
2246 e
->func
->tookAddressOf
++;
2247 if (e
->func
->tintro
&& e
->func
->tintro
->nextOf()->isBaseOf(e
->func
->type
->nextOf(), &offset
) && offset
)
2248 e
->error("%s", msg
);
2254 void visit(FuncExp
*e
)
2256 //printf("FuncExp::castTo type = %s, t = %s\n", e->type->toChars(), t->toChars());
2258 if (e
->matchType(t
, sc
, &fe
, 1) > MATCHnomatch
)
2263 visit((Expression
*)e
);
2266 void visit(CondExp
*e
)
2268 if (!e
->type
->equals(t
))
2270 result
= new CondExp(e
->loc
, e
->econd
, e
->e1
->castTo(sc
, t
), e
->e2
->castTo(sc
, t
));
2277 void visit(CommaExp
*e
)
2279 Expression
*e2c
= e
->e2
->castTo(sc
, t
);
2283 result
= new CommaExp(e
->loc
, e
->e1
, e2c
);
2284 result
->type
= e2c
->type
;
2289 result
->type
= e
->e2
->type
;
2293 void visit(SliceExp
*e
)
2295 //printf("SliceExp::castTo e = %s, type = %s, t = %s\n", e->toChars(), e->type->toChars(), t->toChars());
2296 Type
*typeb
= e
->type
->toBasetype();
2297 Type
*tb
= t
->toBasetype();
2298 if (e
->type
->equals(t
) || typeb
->ty
!= Tarray
||
2299 (tb
->ty
!= Tarray
&& tb
->ty
!= Tsarray
))
2301 visit((Expression
*)e
);
2305 if (tb
->ty
== Tarray
)
2307 if (typeb
->nextOf()->equivalent(tb
->nextOf()))
2309 // T[] to const(T)[]
2315 visit((Expression
*)e
);
2320 // Handle the cast from Tarray to Tsarray with CT-known slicing
2322 TypeSArray
*tsa
= (TypeSArray
*)toStaticArrayType(e
);
2323 if (tsa
&& tsa
->size(e
->loc
) == tb
->size(e
->loc
))
2325 /* Match if the sarray sizes are equal:
2326 * T[a .. b] to const(T)[b-a]
2327 * T[a .. b] to U[dim] if (T.sizeof*(b-a) == U.sizeof*dim)
2329 * If a SliceExp has Tsarray, it will become lvalue.
2330 * That's handled in SliceExp::isLvalue and toLvalue
2336 if (tsa
&& tsa
->dim
->equals(((TypeSArray
*)tb
)->dim
))
2338 /* Match if the dimensions are equal
2339 * with the implicit conversion of e->e1:
2340 * cast(float[2]) [2.0, 1.0, 0.0][0..2];
2342 Type
*t1b
= e
->e1
->type
->toBasetype();
2343 if (t1b
->ty
== Tsarray
)
2344 t1b
= tb
->nextOf()->sarrayOf(((TypeSArray
*)t1b
)->dim
->toInteger());
2345 else if (t1b
->ty
== Tarray
)
2346 t1b
= tb
->nextOf()->arrayOf();
2347 else if (t1b
->ty
== Tpointer
)
2348 t1b
= tb
->nextOf()->pointerTo();
2351 if (e
->e1
->implicitConvTo(t1b
) > MATCHnomatch
)
2353 Expression
*e1x
= e
->e1
->implicitCastTo(sc
, t1b
);
2354 assert(e1x
->op
!= TOKerror
);
2355 e
= (SliceExp
*)e
->copy();
2362 e
->error("cannot cast expression %s of type %s to %s",
2363 e
->toChars(), tsa
? tsa
->toChars() : e
->type
->toChars(),
2365 result
= new ErrorExp();
2374 /* ==================== inferType ====================== */
2376 /****************************************
2377 * Set type inference target
2379 * flag 1: don't put an error when inference fails
2382 Expression
*inferType(Expression
*e
, Type
*t
, int flag
)
2384 class InferType
: public Visitor
2391 InferType(Type
*t
, int flag
)
2398 void visit(Expression
*e
)
2403 void visit(ArrayLiteralExp
*ale
)
2405 Type
*tb
= t
->toBasetype();
2406 if (tb
->ty
== Tarray
|| tb
->ty
== Tsarray
)
2408 Type
*tn
= tb
->nextOf();
2410 ale
->basis
= inferType(ale
->basis
, tn
, flag
);
2411 for (size_t i
= 0; i
< ale
->elements
->dim
; i
++)
2413 Expression
*e
= (*ale
->elements
)[i
];
2416 e
= inferType(e
, tn
, flag
);
2417 (*ale
->elements
)[i
] = e
;
2424 void visit(AssocArrayLiteralExp
*aale
)
2426 Type
*tb
= t
->toBasetype();
2427 if (tb
->ty
== Taarray
)
2429 TypeAArray
*taa
= (TypeAArray
*)tb
;
2430 Type
*ti
= taa
->index
;
2431 Type
*tv
= taa
->nextOf();
2432 for (size_t i
= 0; i
< aale
->keys
->dim
; i
++)
2434 Expression
*e
= (*aale
->keys
)[i
];
2437 e
= inferType(e
, ti
, flag
);
2438 (*aale
->keys
)[i
] = e
;
2441 for (size_t i
= 0; i
< aale
->values
->dim
; i
++)
2443 Expression
*e
= (*aale
->values
)[i
];
2446 e
= inferType(e
, tv
, flag
);
2447 (*aale
->values
)[i
] = e
;
2454 void visit(FuncExp
*fe
)
2456 //printf("FuncExp::inferType('%s'), to=%s\n", fe->type ? fe->type->toChars() : "null", t->toChars());
2457 if (t
->ty
== Tdelegate
||
2458 (t
->ty
== Tpointer
&& t
->nextOf()->ty
== Tfunction
))
2465 void visit(CondExp
*ce
)
2467 Type
*tb
= t
->toBasetype();
2468 ce
->e1
= inferType(ce
->e1
, tb
, flag
);
2469 ce
->e2
= inferType(ce
->e2
, tb
, flag
);
2477 InferType
v(t
, flag
);
2482 /* ==================== ====================== */
2484 /****************************************
2485 * Scale addition/subtraction to/from pointer.
2488 Expression
*scaleFactor(BinExp
*be
, Scope
*sc
)
2490 Type
*t1b
= be
->e1
->type
->toBasetype();
2491 Type
*t2b
= be
->e2
->type
->toBasetype();
2494 if (t1b
->ty
== Tpointer
&& t2b
->isintegral())
2496 // Need to adjust operator by the stride
2497 // Replace (ptr + int) with (ptr + (int * stride))
2498 Type
*t
= Type::tptrdiff_t
;
2500 d_uns64 stride
= t1b
->nextOf()->size(be
->loc
);
2501 if (!t
->equals(t2b
))
2502 be
->e2
= be
->e2
->castTo(sc
, t
);
2504 be
->e2
= new MulExp(be
->loc
, be
->e2
, new IntegerExp(Loc(), stride
, t
));
2506 be
->type
= be
->e1
->type
;
2508 else if (t2b
->ty
== Tpointer
&& t1b
->isintegral())
2510 // Need to adjust operator by the stride
2511 // Replace (int + ptr) with (ptr + (int * stride))
2512 Type
*t
= Type::tptrdiff_t
;
2515 d_uns64 stride
= t2b
->nextOf()->size(be
->loc
);
2516 if (!t
->equals(t1b
))
2517 e
= be
->e1
->castTo(sc
, t
);
2521 e
= new MulExp(be
->loc
, e
, new IntegerExp(Loc(), stride
, t
));
2523 be
->type
= be
->e2
->type
;
2530 if (sc
->func
&& !sc
->intypeof
)
2532 eoff
= eoff
->optimize(WANTvalue
);
2533 if (eoff
->op
== TOKint64
&& eoff
->toInteger() == 0)
2535 else if (sc
->func
->setUnsafe())
2537 be
->error("pointer arithmetic not allowed in @safe functions");
2538 return new ErrorExp();
2545 /**************************************
2546 * Return true if e is an empty array literal with dimensionality
2547 * equal to or less than type of other array.
2548 * [], [[]], [[[]]], etc.
2549 * I.e., make sure that [1,2] is compatible with [],
2550 * [[1,2]] is compatible with [[]], etc.
2552 bool isVoidArrayLiteral(Expression
*e
, Type
*other
)
2554 while (e
->op
== TOKarrayliteral
&& e
->type
->ty
== Tarray
2555 && (((ArrayLiteralExp
*)e
)->elements
->dim
== 1))
2557 ArrayLiteralExp
*ale
= (ArrayLiteralExp
*)e
;
2558 e
= ale
->getElement(0);
2559 if (other
->ty
== Tsarray
|| other
->ty
== Tarray
)
2560 other
= other
->nextOf();
2564 if (other
->ty
!= Tsarray
&& other
->ty
!= Tarray
)
2567 return (e
->op
== TOKarrayliteral
&& t
->ty
== Tarray
&&
2568 t
->nextOf()->ty
== Tvoid
&&
2569 ((ArrayLiteralExp
*)e
)->elements
->dim
== 0);
2572 // used by deduceType()
2573 Type
*rawTypeMerge(Type
*t1
, Type
*t2
)
2577 if (t1
->equivalent(t2
))
2578 return t1
->castMod(MODmerge(t1
->mod
, t2
->mod
));
2580 Type
*t1b
= t1
->toBasetype();
2581 Type
*t2b
= t2
->toBasetype();
2582 if (t1b
->equals(t2b
))
2584 if (t1b
->equivalent(t2b
))
2585 return t1b
->castMod(MODmerge(t1b
->mod
, t2b
->mod
));
2587 TY ty
= (TY
)impcnvResult
[t1b
->ty
][t2b
->ty
];
2589 return Type::basic
[ty
];
2594 /**************************************
2597 * *pt merged type, if *pt is not NULL
2605 bool typeMerge(Scope
*sc
, TOK op
, Type
**pt
, Expression
**pe1
, Expression
**pe2
)
2607 //printf("typeMerge() %s op %s\n", (*pe1)->toChars(), (*pe2)->toChars());
2610 Expression
*e1
= *pe1
;
2611 Expression
*e2
= *pe2
;
2612 Type
*t1b
= e1
->type
->toBasetype();
2613 Type
*t2b
= e2
->type
->toBasetype();
2615 if (op
!= TOKquestion
||
2616 (t1b
->ty
!= t2b
->ty
&& (t1b
->isTypeBasic() && t2b
->isTypeBasic())))
2618 e1
= integralPromotions(e1
, sc
);
2619 e2
= integralPromotions(e2
, sc
);
2622 Type
*t1
= e1
->type
;
2623 Type
*t2
= e2
->type
;
2627 /* The start type of alias this type recursion.
2628 * In following case, we should save A, and stop recursion
2629 * if it appears again.
2630 * X -> Y -> [A] -> B -> A -> B -> ...
2635 //if (t1) printf("\tt1 = %s\n", t1->toChars());
2636 //if (t2) printf("\tt2 = %s\n", t2->toChars());
2639 if (t1
->mod
!= t2
->mod
&&
2640 t1
->ty
== Tenum
&& t2
->ty
== Tenum
&&
2641 ((TypeEnum
*)t1
)->sym
== ((TypeEnum
*)t2
)->sym
)
2643 unsigned char mod
= MODmerge(t1
->mod
, t2
->mod
);
2644 t1
= t1
->castMod(mod
);
2645 t2
= t2
->castMod(mod
);
2649 t1b
= t1
->toBasetype();
2650 t2b
= t2
->toBasetype();
2652 TY ty
= (TY
)impcnvResult
[t1b
->ty
][t2b
->ty
];
2655 TY ty1
= (TY
)impcnvType1
[t1b
->ty
][t2b
->ty
];
2656 TY ty2
= (TY
)impcnvType2
[t1b
->ty
][t2b
->ty
];
2658 if (t1b
->ty
== ty1
) // if no promotions
2666 if (t1b
->equals(t2b
))
2673 t
= Type::basic
[ty
];
2675 t1
= Type::basic
[ty1
];
2676 t2
= Type::basic
[ty2
];
2677 e1
= e1
->castTo(sc
, t1
);
2678 e2
= e2
->castTo(sc
, t2
);
2679 //printf("after typeCombine():\n");
2681 //printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
2688 if (t1
->ty
== Ttuple
|| t2
->ty
== Ttuple
)
2693 // merging can not result in new enum type
2697 else if ((t1
->ty
== Tpointer
&& t2
->ty
== Tpointer
) ||
2698 (t1
->ty
== Tdelegate
&& t2
->ty
== Tdelegate
))
2700 // Bring pointers to compatible type
2701 Type
*t1n
= t1
->nextOf();
2702 Type
*t2n
= t2
->nextOf();
2704 if (t1n
->equals(t2n
))
2706 else if (t1n
->ty
== Tvoid
) // pointers to void are always compatible
2708 else if (t2n
->ty
== Tvoid
)
2710 else if (t1
->implicitConvTo(t2
))
2714 else if (t2
->implicitConvTo(t1
))
2718 else if (t1n
->ty
== Tfunction
&& t2n
->ty
== Tfunction
)
2720 TypeFunction
*tf1
= (TypeFunction
*)t1n
;
2721 TypeFunction
*tf2
= (TypeFunction
*)t2n
;
2725 TypeFunction
*d
= (TypeFunction
*)tf1
->syntaxCopy();
2727 if (tf1
->purity
!= tf2
->purity
)
2728 d
->purity
= PUREimpure
;
2729 assert(d
->purity
!= PUREfwdref
);
2731 d
->isnothrow
= (tf1
->isnothrow
&& tf2
->isnothrow
);
2732 d
->isnogc
= (tf1
->isnogc
&& tf2
->isnogc
);
2734 if (tf1
->trust
== tf2
->trust
)
2735 d
->trust
= tf1
->trust
;
2736 else if (tf1
->trust
<= TRUSTsystem
|| tf2
->trust
<= TRUSTsystem
)
2737 d
->trust
= TRUSTsystem
;
2739 d
->trust
= TRUSTtrusted
;
2742 if (t1
->ty
== Tdelegate
)
2744 tx
= new TypeDelegate(d
);
2747 tx
= d
->pointerTo();
2749 tx
= tx
->semantic(e1
->loc
, sc
);
2751 if (t1
->implicitConvTo(tx
) && t2
->implicitConvTo(tx
))
2754 e1
= e1
->castTo(sc
, t
);
2755 e2
= e2
->castTo(sc
, t
);
2760 else if (t1n
->mod
!= t2n
->mod
)
2762 if (!t1n
->isImmutable() && !t2n
->isImmutable() && t1n
->isShared() != t2n
->isShared())
2764 unsigned char mod
= MODmerge(t1n
->mod
, t2n
->mod
);
2765 t1
= t1n
->castMod(mod
)->pointerTo();
2766 t2
= t2n
->castMod(mod
)->pointerTo();
2770 else if (t1n
->ty
== Tclass
&& t2n
->ty
== Tclass
)
2772 ClassDeclaration
*cd1
= t1n
->isClassHandle();
2773 ClassDeclaration
*cd2
= t2n
->isClassHandle();
2776 if (cd1
->isBaseOf(cd2
, &offset
))
2779 e2
= e2
->castTo(sc
, t
);
2781 else if (cd2
->isBaseOf(cd1
, &offset
))
2785 e1
= e1
->castTo(sc
, t
);
2792 t1
= t1n
->constOf()->pointerTo();
2793 t2
= t2n
->constOf()->pointerTo();
2794 if (t1
->implicitConvTo(t2
))
2798 else if (t2
->implicitConvTo(t1
))
2805 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) &&
2806 ((e2
->op
== TOKnull
&& t2
->ty
== Tpointer
&& t2
->nextOf()->ty
== Tvoid
) ||
2807 (e2
->op
== TOKarrayliteral
&& t2
->ty
== Tsarray
&& t2
->nextOf()->ty
== Tvoid
&& ((TypeSArray
*)t2
)->dim
->toInteger() == 0) ||
2808 (isVoidArrayLiteral(e2
, t1
)))
2811 /* (T[n] op void*) => T[]
2812 * (T[] op void*) => T[]
2813 * (T[n] op void[0]) => T[]
2814 * (T[] op void[0]) => T[]
2815 * (T[n] op void[]) => T[]
2816 * (T[] op void[]) => T[]
2820 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) &&
2821 ((e1
->op
== TOKnull
&& t1
->ty
== Tpointer
&& t1
->nextOf()->ty
== Tvoid
) ||
2822 (e1
->op
== TOKarrayliteral
&& t1
->ty
== Tsarray
&& t1
->nextOf()->ty
== Tvoid
&& ((TypeSArray
*)t1
)->dim
->toInteger() == 0) ||
2823 (isVoidArrayLiteral(e1
, t2
)))
2826 /* (void* op T[n]) => T[]
2827 * (void* op T[]) => T[]
2828 * (void[0] op T[n]) => T[]
2829 * (void[0] op T[]) => T[]
2830 * (void[] op T[n]) => T[]
2831 * (void[] op T[]) => T[]
2835 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
) &&
2836 (m
= t1
->implicitConvTo(t2
)) != MATCHnomatch
)
2838 // Bugzilla 7285: Tsarray op [x, y, ...] should to be Tsarray
2839 // Bugzilla 14737: Tsarray ~ [x, y, ...] should to be Tarray
2840 if (t1
->ty
== Tsarray
&& e2
->op
== TOKarrayliteral
&& op
!= TOKcat
)
2842 if (m
== MATCHconst
&&
2843 (op
== TOKaddass
|| op
== TOKminass
|| op
== TOKmulass
||
2844 op
== TOKdivass
|| op
== TOKmodass
|| op
== TOKpowass
||
2845 op
== TOKandass
|| op
== TOKorass
|| op
== TOKxorass
)
2848 // Don't make the lvalue const
2854 else if ((t2
->ty
== Tsarray
|| t2
->ty
== Tarray
) && t2
->implicitConvTo(t1
))
2856 // Bugzilla 7285 & 14737
2857 if (t2
->ty
== Tsarray
&& e1
->op
== TOKarrayliteral
&& op
!= TOKcat
)
2861 else if ((t1
->ty
== Tsarray
|| t1
->ty
== Tarray
|| t1
->ty
== Tpointer
) &&
2862 (t2
->ty
== Tsarray
|| t2
->ty
== Tarray
|| t2
->ty
== Tpointer
) &&
2863 t1
->nextOf()->mod
!= t2
->nextOf()->mod
2866 /* If one is mutable and the other invariant, then retry
2867 * with both of them as const
2869 Type
*t1n
= t1
->nextOf();
2870 Type
*t2n
= t2
->nextOf();
2872 if (e1
->op
== TOKnull
&& e2
->op
!= TOKnull
)
2874 else if (e1
->op
!= TOKnull
&& e2
->op
== TOKnull
)
2876 else if (!t1n
->isImmutable() && !t2n
->isImmutable() && t1n
->isShared() != t2n
->isShared())
2879 mod
= MODmerge(t1n
->mod
, t2n
->mod
);
2881 if (t1
->ty
== Tpointer
)
2882 t1
= t1n
->castMod(mod
)->pointerTo();
2884 t1
= t1n
->castMod(mod
)->arrayOf();
2886 if (t2
->ty
== Tpointer
)
2887 t2
= t2n
->castMod(mod
)->pointerTo();
2889 t2
= t2n
->castMod(mod
)->arrayOf();
2893 else if (t1
->ty
== Tclass
&& t2
->ty
== Tclass
)
2895 if (t1
->mod
!= t2
->mod
)
2898 if (e1
->op
== TOKnull
&& e2
->op
!= TOKnull
)
2900 else if (e1
->op
!= TOKnull
&& e2
->op
== TOKnull
)
2902 else if (!t1
->isImmutable() && !t2
->isImmutable() && t1
->isShared() != t2
->isShared())
2905 mod
= MODmerge(t1
->mod
, t2
->mod
);
2906 t1
= t1
->castMod(mod
);
2907 t2
= t2
->castMod(mod
);
2913 else if (t1
->ty
== Tclass
|| t2
->ty
== Tclass
)
2918 MATCH i1
= e2
->implicitConvTo(t1
);
2919 MATCH i2
= e1
->implicitConvTo(t2
);
2923 // We have the case of class vs. void*, so pick class
2924 if (t1
->ty
== Tpointer
)
2926 else if (t2
->ty
== Tpointer
)
2932 e2
= e2
->castTo(sc
, t2
);
2937 e1
= e1
->castTo(sc
, t1
);
2940 else if (t1
->ty
== Tclass
&& t2
->ty
== Tclass
)
2942 TypeClass
*tc1
= (TypeClass
*)t1
;
2943 TypeClass
*tc2
= (TypeClass
*)t2
;
2945 /* Pick 'tightest' type
2947 ClassDeclaration
*cd1
= tc1
->sym
->baseClass
;
2948 ClassDeclaration
*cd2
= tc2
->sym
->baseClass
;
2952 t1
= cd1
->type
->castMod(t1
->mod
);
2953 t2
= cd2
->type
->castMod(t2
->mod
);
2962 else if (t1
->ty
== Tstruct
&& ((TypeStruct
*)t1
)->sym
->aliasthis
)
2964 if (att1
&& e1
->type
== att1
)
2966 if (!att1
&& e1
->type
->checkAliasThisRec())
2968 //printf("att tmerge(c || c) e1 = %s\n", e1->type->toChars());
2969 e1
= resolveAliasThis(sc
, e1
);
2973 else if (t2
->ty
== Tstruct
&& ((TypeStruct
*)t2
)->sym
->aliasthis
)
2975 if (att2
&& e2
->type
== att2
)
2977 if (!att2
&& e2
->type
->checkAliasThisRec())
2979 //printf("att tmerge(c || c) e2 = %s\n", e2->type->toChars());
2980 e2
= resolveAliasThis(sc
, e2
);
2988 else if (t1
->ty
== Tstruct
&& t2
->ty
== Tstruct
)
2990 if (t1
->mod
!= t2
->mod
)
2992 if (!t1
->isImmutable() && !t2
->isImmutable() && t1
->isShared() != t2
->isShared())
2994 unsigned char mod
= MODmerge(t1
->mod
, t2
->mod
);
2995 t1
= t1
->castMod(mod
);
2996 t2
= t2
->castMod(mod
);
3001 TypeStruct
*ts1
= (TypeStruct
*)t1
;
3002 TypeStruct
*ts2
= (TypeStruct
*)t2
;
3003 if (ts1
->sym
!= ts2
->sym
)
3005 if (!ts1
->sym
->aliasthis
&& !ts2
->sym
->aliasthis
)
3008 MATCH i1
= MATCHnomatch
;
3009 MATCH i2
= MATCHnomatch
;
3011 Expression
*e1b
= NULL
;
3012 Expression
*e2b
= NULL
;
3013 if (ts2
->sym
->aliasthis
)
3015 if (att2
&& e2
->type
== att2
)
3017 if (!att2
&& e2
->type
->checkAliasThisRec())
3019 //printf("att tmerge(s && s) e2 = %s\n", e2->type->toChars());
3020 e2b
= resolveAliasThis(sc
, e2
);
3021 i1
= e2b
->implicitConvTo(t1
);
3023 if (ts1
->sym
->aliasthis
)
3025 if (att1
&& e1
->type
== att1
)
3027 if (!att1
&& e1
->type
->checkAliasThisRec())
3029 //printf("att tmerge(s && s) e1 = %s\n", e1->type->toChars());
3030 e1b
= resolveAliasThis(sc
, e1
);
3031 i2
= e1b
->implicitConvTo(t2
);
3044 t1
= e1b
->type
->toBasetype();
3049 t2
= e2b
->type
->toBasetype();
3055 else if (t1
->ty
== Tstruct
|| t2
->ty
== Tstruct
)
3057 if (t1
->ty
== Tstruct
&& ((TypeStruct
*)t1
)->sym
->aliasthis
)
3059 if (att1
&& e1
->type
== att1
)
3061 if (!att1
&& e1
->type
->checkAliasThisRec())
3063 //printf("att tmerge(s || s) e1 = %s\n", e1->type->toChars());
3064 e1
= resolveAliasThis(sc
, e1
);
3069 if (t2
->ty
== Tstruct
&& ((TypeStruct
*)t2
)->sym
->aliasthis
)
3071 if (att2
&& e2
->type
== att2
)
3073 if (!att2
&& e2
->type
->checkAliasThisRec())
3075 //printf("att tmerge(s || s) e2 = %s\n", e2->type->toChars());
3076 e2
= resolveAliasThis(sc
, e2
);
3083 else if ((e1
->op
== TOKstring
|| e1
->op
== TOKnull
) && e1
->implicitConvTo(t2
))
3087 else if ((e2
->op
== TOKstring
|| e2
->op
== TOKnull
) && e2
->implicitConvTo(t1
))
3091 else if (t1
->ty
== Tsarray
&& t2
->ty
== Tsarray
&&
3092 e2
->implicitConvTo(t1
->nextOf()->arrayOf()))
3095 t
= t1
->nextOf()->arrayOf(); // T[]
3096 e1
= e1
->castTo(sc
, t
);
3097 e2
= e2
->castTo(sc
, t
);
3099 else if (t1
->ty
== Tsarray
&& t2
->ty
== Tsarray
&&
3100 e1
->implicitConvTo(t2
->nextOf()->arrayOf()))
3103 t
= t2
->nextOf()->arrayOf();
3104 e1
= e1
->castTo(sc
, t
);
3105 e2
= e2
->castTo(sc
, t
);
3107 else if (t1
->ty
== Tvector
&& t2
->ty
== Tvector
)
3109 // Bugzilla 13841, all vector types should have no common types between
3110 // different vectors, even though their sizes are same.
3111 TypeVector
*tv1
= (TypeVector
*)t1
;
3112 TypeVector
*tv2
= (TypeVector
*)t2
;
3113 if (!tv1
->basetype
->equals(tv2
->basetype
))
3118 else if (t1
->ty
== Tvector
&& t2
->ty
!= Tvector
&&
3119 e2
->implicitConvTo(t1
))
3121 e2
= e2
->castTo(sc
, t1
);
3126 else if (t2
->ty
== Tvector
&& t1
->ty
!= Tvector
&&
3127 e1
->implicitConvTo(t2
))
3129 e1
= e1
->castTo(sc
, t2
);
3134 else if (t1
->isintegral() && t2
->isintegral())
3136 if (t1
->ty
!= t2
->ty
)
3138 if (t1
->ty
== Tvector
|| t2
->ty
== Tvector
)
3140 e1
= integralPromotions(e1
, sc
);
3141 e2
= integralPromotions(e2
, sc
);
3146 assert(t1
->ty
== t2
->ty
);
3148 if (!t1
->isImmutable() && !t2
->isImmutable() && t1
->isShared() != t2
->isShared())
3150 unsigned char mod
= MODmerge(t1
->mod
, t2
->mod
);
3152 t1
= t1
->castMod(mod
);
3153 t2
= t2
->castMod(mod
);
3155 e1
= e1
->castTo(sc
, t
);
3156 e2
= e2
->castTo(sc
, t
);
3159 else if (t1
->ty
== Tnull
&& t2
->ty
== Tnull
)
3161 unsigned char mod
= MODmerge(t1
->mod
, t2
->mod
);
3163 t
= t1
->castMod(mod
);
3164 e1
= e1
->castTo(sc
, t
);
3165 e2
= e2
->castTo(sc
, t
);
3168 else if (t2
->ty
== Tnull
&&
3169 (t1
->ty
== Tpointer
|| t1
->ty
== Taarray
|| t1
->ty
== Tarray
))
3173 else if (t1
->ty
== Tnull
&&
3174 (t2
->ty
== Tpointer
|| t2
->ty
== Taarray
|| t2
->ty
== Tarray
))
3178 else if (t1
->ty
== Tarray
&& isBinArrayOp(op
) && isArrayOpOperand(e1
))
3180 if (e2
->implicitConvTo(t1
->nextOf()))
3184 e2
= e2
->castTo(sc
, t1
->nextOf());
3185 t
= t1
->nextOf()->arrayOf();
3187 else if (t1
->nextOf()->implicitConvTo(e2
->type
))
3189 // (cast(T)U)[] op T (Bugzilla 12780)
3190 // e1 is left as U[], it will be handled in arrayOp() later.
3191 t
= e2
->type
->arrayOf();
3193 else if (t2
->ty
== Tarray
&& isArrayOpOperand(e2
))
3195 if (t1
->nextOf()->implicitConvTo(t2
->nextOf()))
3197 // (cast(T)U)[] op T[] (Bugzilla 12780)
3198 // e1 is left as U[], it will be handled in arrayOp() later.
3199 t
= t2
->nextOf()->arrayOf();
3201 else if (t2
->nextOf()->implicitConvTo(t1
->nextOf()))
3203 // T[] op (cast(T)U)[] (Bugzilla 12780)
3204 // e2 is left as U[], it will be handled in arrayOp() later.
3205 t
= t1
->nextOf()->arrayOf();
3213 else if (t2
->ty
== Tarray
&& isBinArrayOp(op
) && isArrayOpOperand(e2
))
3215 if (e1
->implicitConvTo(t2
->nextOf()))
3219 e1
= e1
->castTo(sc
, t2
->nextOf());
3220 t
= t2
->nextOf()->arrayOf();
3222 else if (t2
->nextOf()->implicitConvTo(e1
->type
))
3224 // T op (cast(T)U)[] (Bugzilla 12780)
3225 // e2 is left as U[], it will be handled in arrayOp() later.
3226 t
= e1
->type
->arrayOf();
3231 //printf("test %s\n", Token::toChars(op));
3232 e1
= e1
->optimize(WANTvalue
);
3233 if (isCommutative(op
) && e1
->isConst())
3235 /* Swap operands to minimize number of functions generated
3237 //printf("swap %s\n", Token::toChars(op));
3238 Expression
*tmp
= e1
;
3258 e2
= e2
->castTo(sc
, t1
);
3263 e1
= e1
->castTo(sc
, t2
);
3268 /************************************
3269 * Bring leaves to common type.
3270 * Returns ErrorExp if error occurs. otherwise returns NULL.
3273 Expression
*typeCombine(BinExp
*be
, Scope
*sc
)
3275 Type
*t1
= be
->e1
->type
->toBasetype();
3276 Type
*t2
= be
->e2
->type
->toBasetype();
3278 if (be
->op
== TOKmin
|| be
->op
== TOKadd
)
3280 // struct+struct, and class+class are errors
3281 if (t1
->ty
== Tstruct
&& t2
->ty
== Tstruct
)
3283 else if (t1
->ty
== Tclass
&& t2
->ty
== Tclass
)
3285 else if (t1
->ty
== Taarray
&& t2
->ty
== Taarray
)
3289 if (!typeMerge(sc
, be
->op
, &be
->type
, &be
->e1
, &be
->e2
))
3291 // If the types have no value, return an error
3292 if (be
->e1
->op
== TOKerror
)
3294 if (be
->e2
->op
== TOKerror
)
3299 Expression
*ex
= be
->incompatibleTypes();
3300 if (ex
->op
== TOKerror
)
3302 return new ErrorExp();
3305 /***********************************
3306 * Do integral promotions (convertchk).
3307 * Don't convert <array of> to <pointer to>
3310 Expression
*integralPromotions(Expression
*e
, Scope
*sc
)
3312 //printf("integralPromotions %s %s\n", e->toChars(), e->type->toChars());
3313 switch (e
->type
->toBasetype()->ty
)
3316 e
->error("void has no value");
3317 return new ErrorExp();
3326 e
= e
->castTo(sc
, Type::tint32
);
3330 e
= e
->castTo(sc
, Type::tuns32
);
3338 /***********************************
3339 * See if both types are arrays that can be compared
3340 * for equality. Return true if so.
3341 * If they are arrays, but incompatible, issue error.
3342 * This is to enable comparing things like an immutable
3343 * array with a mutable one.
3346 bool arrayTypeCompatible(Loc loc
, Type
*t1
, Type
*t2
)
3348 t1
= t1
->toBasetype()->merge2();
3349 t2
= t2
->toBasetype()->merge2();
3351 if ((t1
->ty
== Tarray
|| t1
->ty
== Tsarray
|| t1
->ty
== Tpointer
) &&
3352 (t2
->ty
== Tarray
|| t2
->ty
== Tsarray
|| t2
->ty
== Tpointer
))
3354 if (t1
->nextOf()->implicitConvTo(t2
->nextOf()) < MATCHconst
&&
3355 t2
->nextOf()->implicitConvTo(t1
->nextOf()) < MATCHconst
&&
3356 (t1
->nextOf()->ty
!= Tvoid
&& t2
->nextOf()->ty
!= Tvoid
))
3358 error(loc
, "array equality comparison type mismatch, %s vs %s", t1
->toChars(), t2
->toChars());
3365 /***********************************
3366 * See if both types are arrays that can be compared
3367 * for equality without any casting. Return true if so.
3368 * This is to enable comparing things like an immutable
3369 * array with a mutable one.
3371 bool arrayTypeCompatibleWithoutCasting(Type
*t1
, Type
*t2
)
3373 t1
= t1
->toBasetype();
3374 t2
= t2
->toBasetype();
3376 if ((t1
->ty
== Tarray
|| t1
->ty
== Tsarray
|| t1
->ty
== Tpointer
) &&
3379 if (t1
->nextOf()->implicitConvTo(t2
->nextOf()) >= MATCHconst
||
3380 t2
->nextOf()->implicitConvTo(t1
->nextOf()) >= MATCHconst
)
3386 /******************************************************************/
3388 /* Determine the integral ranges of an expression.
3389 * This is used to determine if implicit narrowing conversions will
3393 IntRange
getIntRange(Expression
*e
)
3395 class IntRangeVisitor
: public Visitor
3400 void visit(Expression
*e
)
3402 range
= IntRange::fromType(e
->type
);
3405 void visit(IntegerExp
*e
)
3407 range
= IntRange(SignExtendedNumber(e
->getInteger())).cast(e
->type
);
3410 void visit(CastExp
*e
)
3412 range
= getIntRange(e
->e1
).cast(e
->type
);
3415 void visit(AddExp
*e
)
3417 IntRange ir1
= getIntRange(e
->e1
);
3418 IntRange ir2
= getIntRange(e
->e2
);
3419 range
= (ir1
+ ir2
).cast(e
->type
);
3422 void visit(MinExp
*e
)
3424 IntRange ir1
= getIntRange(e
->e1
);
3425 IntRange ir2
= getIntRange(e
->e2
);
3426 range
= (ir1
- ir2
).cast(e
->type
);
3429 void visit(DivExp
*e
)
3431 IntRange ir1
= getIntRange(e
->e1
);
3432 IntRange ir2
= getIntRange(e
->e2
);
3434 range
= (ir1
/ ir2
).cast(e
->type
);
3437 void visit(MulExp
*e
)
3439 IntRange ir1
= getIntRange(e
->e1
);
3440 IntRange ir2
= getIntRange(e
->e2
);
3442 range
= (ir1
* ir2
).cast(e
->type
);
3445 void visit(ModExp
*e
)
3447 IntRange ir1
= getIntRange(e
->e1
);
3448 IntRange ir2
= getIntRange(e
->e2
);
3450 // Modding on 0 is invalid anyway.
3451 if (!ir2
.absNeg().imin
.negative
)
3453 visit((Expression
*)e
);
3456 range
= (ir1
% ir2
).cast(e
->type
);
3459 void visit(AndExp
*e
)
3462 bool hasResult
= false;
3463 result
.unionOrAssign(getIntRange(e
->e1
) & getIntRange(e
->e2
), hasResult
);
3466 range
= result
.cast(e
->type
);
3469 void visit(OrExp
*e
)
3472 bool hasResult
= false;
3473 result
.unionOrAssign(getIntRange(e
->e1
) | getIntRange(e
->e2
), hasResult
);
3476 range
= result
.cast(e
->type
);
3479 void visit(XorExp
*e
)
3482 bool hasResult
= false;
3483 result
.unionOrAssign(getIntRange(e
->e1
) ^ getIntRange(e
->e2
), hasResult
);
3486 range
= result
.cast(e
->type
);
3489 void visit(ShlExp
*e
)
3491 IntRange ir1
= getIntRange(e
->e1
);
3492 IntRange ir2
= getIntRange(e
->e2
);
3494 range
= (ir1
<< ir2
).cast(e
->type
);
3497 void visit(ShrExp
*e
)
3499 IntRange ir1
= getIntRange(e
->e1
);
3500 IntRange ir2
= getIntRange(e
->e2
);
3502 range
= (ir1
>> ir2
).cast(e
->type
);
3505 void visit(UshrExp
*e
)
3507 IntRange ir1
= getIntRange(e
->e1
).castUnsigned(e
->e1
->type
);
3508 IntRange ir2
= getIntRange(e
->e2
);
3510 range
= (ir1
>> ir2
).cast(e
->type
);
3513 void visit(AssignExp
*e
)
3515 range
= getIntRange(e
->e2
).cast(e
->type
);
3518 void visit(CondExp
*e
)
3520 // No need to check e->econd; assume caller has called optimize()
3521 IntRange ir1
= getIntRange(e
->e1
);
3522 IntRange ir2
= getIntRange(e
->e2
);
3523 range
= ir1
.unionWith(ir2
).cast(e
->type
);
3526 void visit(VarExp
*e
)
3529 VarDeclaration
* vd
= e
->var
->isVarDeclaration();
3530 if (vd
&& vd
->range
)
3531 range
= vd
->range
->cast(e
->type
);
3532 else if (vd
&& vd
->_init
&& !vd
->type
->isMutable() &&
3533 (ie
= vd
->getConstInitializer()) != NULL
)
3536 visit((Expression
*)e
);
3539 void visit(CommaExp
*e
)
3541 e
->e2
->accept(this);
3544 void visit(ComExp
*e
)
3546 IntRange ir
= getIntRange(e
->e1
);
3547 range
= IntRange(SignExtendedNumber(~ir
.imax
.value
, !ir
.imax
.negative
),
3548 SignExtendedNumber(~ir
.imin
.value
, !ir
.imin
.negative
)).cast(e
->type
);
3551 void visit(NegExp
*e
)
3553 IntRange ir
= getIntRange(e
->e1
);
3554 range
= (-ir
).cast(e
->type
);