| 1 |
// Copyright (c) 1999-2010 by Digital Mars |
|---|
| 2 |
// All Rights Reserved |
|---|
| 3 |
// written by Walter Bright |
|---|
| 4 |
// http://www.digitalmars.com |
|---|
| 5 |
// License for redistribution is by either the Artistic License |
|---|
| 6 |
// in artistic.txt, or the GNU General Public License in gnu.txt. |
|---|
| 7 |
// See the included readme.txt for details. |
|---|
| 8 |
|
|---|
| 9 |
#include <stdio.h> |
|---|
| 10 |
#include <assert.h> |
|---|
| 11 |
|
|---|
| 12 |
#include "rmem.h" |
|---|
| 13 |
|
|---|
| 14 |
#include "expression.h" |
|---|
| 15 |
#include "mtype.h" |
|---|
| 16 |
#include "utf.h" |
|---|
| 17 |
#include "declaration.h" |
|---|
| 18 |
#include "aggregate.h" |
|---|
| 19 |
|
|---|
| 20 |
/* ==================== implicitCast ====================== */ |
|---|
| 21 |
|
|---|
| 22 |
/************************************** |
|---|
| 23 |
* Do an implicit cast. |
|---|
| 24 |
* Issue error if it can't be done. |
|---|
| 25 |
*/ |
|---|
| 26 |
|
|---|
| 27 |
Expression *Expression::implicitCastTo(Scope *sc, Type *t) |
|---|
| 28 |
{ |
|---|
| 29 |
//printf("Expression::implicitCastTo(%s of type %s) => %s\n", toChars(), type->toChars(), t->toChars()); |
|---|
| 30 |
|
|---|
| 31 |
MATCH match = implicitConvTo(t); |
|---|
| 32 |
if (match) |
|---|
| 33 |
{ TY tyfrom = type->toBasetype()->ty; |
|---|
| 34 |
TY tyto = t->toBasetype()->ty; |
|---|
| 35 |
#if DMDV1 |
|---|
| 36 |
if (global.params.warnings && |
|---|
| 37 |
Type::impcnvWarn[tyfrom][tyto] && |
|---|
| 38 |
op != TOKint64) |
|---|
| 39 |
{ |
|---|
| 40 |
Expression *e = optimize(WANTflags | WANTvalue); |
|---|
| 41 |
|
|---|
| 42 |
if (e->op == TOKint64) |
|---|
| 43 |
return e->implicitCastTo(sc, t); |
|---|
| 44 |
if (tyfrom == Tint32 && |
|---|
| 45 |
(op == TOKadd || op == TOKmin || |
|---|
| 46 |
op == TOKand || op == TOKor || op == TOKxor) |
|---|
| 47 |
) |
|---|
| 48 |
{ |
|---|
| 49 |
/* This is really only a semi-kludge fix, |
|---|
| 50 |
* we really should look at the operands of op |
|---|
| 51 |
* and see if they are narrower types. |
|---|
| 52 |
* For example, b=b|b and b=b|7 and s=b+b should be allowed, |
|---|
| 53 |
* but b=b|i should be an error. |
|---|
| 54 |
*/ |
|---|
| 55 |
; |
|---|
| 56 |
} |
|---|
| 57 |
else |
|---|
| 58 |
{ |
|---|
| 59 |
warning("implicit conversion of expression (%s) of type %s to %s can cause loss of data", |
|---|
| 60 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 61 |
} |
|---|
| 62 |
} |
|---|
| 63 |
#endif |
|---|
| 64 |
#if DMDV2 |
|---|
| 65 |
if (match == MATCHconst && t == type->constOf()) |
|---|
| 66 |
{ |
|---|
| 67 |
Expression *e = copy(); |
|---|
| 68 |
e->type = t; |
|---|
| 69 |
return e; |
|---|
| 70 |
} |
|---|
| 71 |
#endif |
|---|
| 72 |
return castTo(sc, t); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
Expression *e = optimize(WANTflags | WANTvalue); |
|---|
| 76 |
if (e != this) |
|---|
| 77 |
return e->implicitCastTo(sc, t); |
|---|
| 78 |
|
|---|
| 79 |
#if 0 |
|---|
| 80 |
printf("ty = %d\n", type->ty); |
|---|
| 81 |
print(); |
|---|
| 82 |
type->print(); |
|---|
| 83 |
printf("to:\n"); |
|---|
| 84 |
t->print(); |
|---|
| 85 |
printf("%p %p type: %s to: %s\n", type->deco, t->deco, type->deco, t->deco); |
|---|
| 86 |
//printf("%p %p %p\n", type->nextOf()->arrayOf(), type, t); |
|---|
| 87 |
fflush(stdout); |
|---|
| 88 |
#endif |
|---|
| 89 |
if (t->ty != Terror && type->ty != Terror) |
|---|
| 90 |
{ |
|---|
| 91 |
if (!t->deco) |
|---|
| 92 |
{ /* Can happen with: |
|---|
| 93 |
* enum E { One } |
|---|
| 94 |
* class A |
|---|
| 95 |
* { static void fork(EDG dg) { dg(E.One); } |
|---|
| 96 |
* alias void delegate(E) EDG; |
|---|
| 97 |
* } |
|---|
| 98 |
* Should eventually make it work. |
|---|
| 99 |
*/ |
|---|
| 100 |
error("forward reference to type %s", t->toChars()); |
|---|
| 101 |
} |
|---|
| 102 |
else if (t->reliesOnTident()) |
|---|
| 103 |
error("forward reference to type %s", t->reliesOnTident()->toChars()); |
|---|
| 104 |
|
|---|
| 105 |
error("cannot implicitly convert expression (%s) of type %s to %s", |
|---|
| 106 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 107 |
} |
|---|
| 108 |
return new ErrorExp(); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
Expression *StringExp::implicitCastTo(Scope *sc, Type *t) |
|---|
| 112 |
{ |
|---|
| 113 |
//printf("StringExp::implicitCastTo(%s of type %s) => %s\n", toChars(), type->toChars(), t->toChars()); |
|---|
| 114 |
unsigned char committed = this->committed; |
|---|
| 115 |
Expression *e = Expression::implicitCastTo(sc, t); |
|---|
| 116 |
if (e->op == TOKstring) |
|---|
| 117 |
{ |
|---|
| 118 |
// Retain polysemous nature if it started out that way |
|---|
| 119 |
((StringExp *)e)->committed = committed; |
|---|
| 120 |
} |
|---|
| 121 |
return e; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
Expression *ErrorExp::implicitCastTo(Scope *sc, Type *t) |
|---|
| 125 |
{ |
|---|
| 126 |
return this; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
/******************************************* |
|---|
| 130 |
* Return !=0 if we can implicitly convert this to type t. |
|---|
| 131 |
* Don't do the actual cast. |
|---|
| 132 |
*/ |
|---|
| 133 |
|
|---|
| 134 |
MATCH Expression::implicitConvTo(Type *t) |
|---|
| 135 |
{ |
|---|
| 136 |
#if 0 |
|---|
| 137 |
printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n", |
|---|
| 138 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 139 |
#endif |
|---|
| 140 |
//static int nest; if (++nest == 10) halt(); |
|---|
| 141 |
if (!type) |
|---|
| 142 |
{ error("%s is not an expression", toChars()); |
|---|
| 143 |
type = Type::terror; |
|---|
| 144 |
} |
|---|
| 145 |
Expression *e = optimize(WANTvalue | WANTflags); |
|---|
| 146 |
if (e->type == t) |
|---|
| 147 |
return MATCHexact; |
|---|
| 148 |
if (e != this) |
|---|
| 149 |
{ //printf("\toptimized to %s of type %s\n", e->toChars(), e->type->toChars()); |
|---|
| 150 |
return e->implicitConvTo(t); |
|---|
| 151 |
} |
|---|
| 152 |
MATCH match = type->implicitConvTo(t); |
|---|
| 153 |
if (match != MATCHnomatch) |
|---|
| 154 |
return match; |
|---|
| 155 |
|
|---|
| 156 |
/* See if we can do integral narrowing conversions |
|---|
| 157 |
*/ |
|---|
| 158 |
if (type->isintegral() && t->isintegral() && |
|---|
| 159 |
type->isTypeBasic() && t->isTypeBasic()) |
|---|
| 160 |
{ IntRange ir = getIntRange(); |
|---|
| 161 |
if (ir.imax <= t->sizemask()) |
|---|
| 162 |
return MATCHconvert; |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
#if 0 |
|---|
| 166 |
Type *tb = t->toBasetype(); |
|---|
| 167 |
if (tb->ty == Tdelegate) |
|---|
| 168 |
{ TypeDelegate *td = (TypeDelegate *)tb; |
|---|
| 169 |
TypeFunction *tf = (TypeFunction *)td->nextOf(); |
|---|
| 170 |
|
|---|
| 171 |
if (!tf->varargs && |
|---|
| 172 |
!(tf->arguments && tf->arguments->dim) |
|---|
| 173 |
) |
|---|
| 174 |
{ |
|---|
| 175 |
match = type->implicitConvTo(tf->nextOf()); |
|---|
| 176 |
if (match) |
|---|
| 177 |
return match; |
|---|
| 178 |
if (tf->nextOf()->toBasetype()->ty == Tvoid) |
|---|
| 179 |
return MATCHconvert; |
|---|
| 180 |
} |
|---|
| 181 |
} |
|---|
| 182 |
#endif |
|---|
| 183 |
return MATCHnomatch; |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
MATCH IntegerExp::implicitConvTo(Type *t) |
|---|
| 188 |
{ |
|---|
| 189 |
#if 0 |
|---|
| 190 |
printf("IntegerExp::implicitConvTo(this=%s, type=%s, t=%s)\n", |
|---|
| 191 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 192 |
#endif |
|---|
| 193 |
MATCH m = type->implicitConvTo(t); |
|---|
| 194 |
if (m >= MATCHconst) |
|---|
| 195 |
return m; |
|---|
| 196 |
|
|---|
| 197 |
TY ty = type->toBasetype()->ty; |
|---|
| 198 |
TY toty = t->toBasetype()->ty; |
|---|
| 199 |
|
|---|
| 200 |
if (m == MATCHnomatch && t->ty == Tenum) |
|---|
| 201 |
goto Lno; |
|---|
| 202 |
|
|---|
| 203 |
switch (ty) |
|---|
| 204 |
{ |
|---|
| 205 |
case Tbit: |
|---|
| 206 |
case Tbool: |
|---|
| 207 |
value &= 1; |
|---|
| 208 |
ty = Tint32; |
|---|
| 209 |
break; |
|---|
| 210 |
|
|---|
| 211 |
case Tint8: |
|---|
| 212 |
value = (signed char)value; |
|---|
| 213 |
ty = Tint32; |
|---|
| 214 |
break; |
|---|
| 215 |
|
|---|
| 216 |
case Tchar: |
|---|
| 217 |
case Tuns8: |
|---|
| 218 |
value &= 0xFF; |
|---|
| 219 |
ty = Tint32; |
|---|
| 220 |
break; |
|---|
| 221 |
|
|---|
| 222 |
case Tint16: |
|---|
| 223 |
value = (short)value; |
|---|
| 224 |
ty = Tint32; |
|---|
| 225 |
break; |
|---|
| 226 |
|
|---|
| 227 |
case Tuns16: |
|---|
| 228 |
case Twchar: |
|---|
| 229 |
value &= 0xFFFF; |
|---|
| 230 |
ty = Tint32; |
|---|
| 231 |
break; |
|---|
| 232 |
|
|---|
| 233 |
case Tint32: |
|---|
| 234 |
value = (int)value; |
|---|
| 235 |
break; |
|---|
| 236 |
|
|---|
| 237 |
case Tuns32: |
|---|
| 238 |
case Tdchar: |
|---|
| 239 |
value &= 0xFFFFFFFF; |
|---|
| 240 |
ty = Tuns32; |
|---|
| 241 |
break; |
|---|
| 242 |
|
|---|
| 243 |
default: |
|---|
| 244 |
break; |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
// Only allow conversion if no change in value |
|---|
| 248 |
switch (toty) |
|---|
| 249 |
{ |
|---|
| 250 |
case Tbit: |
|---|
| 251 |
case Tbool: |
|---|
| 252 |
if ((value & 1) != value) |
|---|
| 253 |
goto Lno; |
|---|
| 254 |
goto Lyes; |
|---|
| 255 |
|
|---|
| 256 |
case Tint8: |
|---|
| 257 |
if ((signed char)value != value) |
|---|
| 258 |
goto Lno; |
|---|
| 259 |
goto Lyes; |
|---|
| 260 |
|
|---|
| 261 |
case Tchar: |
|---|
| 262 |
case Tuns8: |
|---|
| 263 |
//printf("value = %llu %llu\n", (dinteger_t)(unsigned char)value, value); |
|---|
| 264 |
if ((unsigned char)value != value) |
|---|
| 265 |
goto Lno; |
|---|
| 266 |
goto Lyes; |
|---|
| 267 |
|
|---|
| 268 |
case Tint16: |
|---|
| 269 |
if ((short)value != value) |
|---|
| 270 |
goto Lno; |
|---|
| 271 |
goto Lyes; |
|---|
| 272 |
|
|---|
| 273 |
case Tuns16: |
|---|
| 274 |
if ((unsigned short)value != value) |
|---|
| 275 |
goto Lno; |
|---|
| 276 |
goto Lyes; |
|---|
| 277 |
|
|---|
| 278 |
case Tint32: |
|---|
| 279 |
if (ty == Tuns32) |
|---|
| 280 |
{ |
|---|
| 281 |
} |
|---|
| 282 |
else if ((int)value != value) |
|---|
| 283 |
goto Lno; |
|---|
| 284 |
goto Lyes; |
|---|
| 285 |
|
|---|
| 286 |
case Tuns32: |
|---|
| 287 |
if (ty == Tint32) |
|---|
| 288 |
{ |
|---|
| 289 |
} |
|---|
| 290 |
else if ((unsigned)value != value) |
|---|
| 291 |
goto Lno; |
|---|
| 292 |
goto Lyes; |
|---|
| 293 |
|
|---|
| 294 |
case Tdchar: |
|---|
| 295 |
if (value > 0x10FFFFUL) |
|---|
| 296 |
goto Lno; |
|---|
| 297 |
goto Lyes; |
|---|
| 298 |
|
|---|
| 299 |
case Twchar: |
|---|
| 300 |
if ((unsigned short)value != value) |
|---|
| 301 |
goto Lno; |
|---|
| 302 |
goto Lyes; |
|---|
| 303 |
|
|---|
| 304 |
case Tfloat32: |
|---|
| 305 |
{ |
|---|
| 306 |
volatile float f; |
|---|
| 307 |
if (type->isunsigned()) |
|---|
| 308 |
{ |
|---|
| 309 |
f = (float)value; |
|---|
| 310 |
if (f != value) |
|---|
| 311 |
goto Lno; |
|---|
| 312 |
} |
|---|
| 313 |
else |
|---|
| 314 |
{ |
|---|
| 315 |
f = (float)(long long)value; |
|---|
| 316 |
if (f != (long long)value) |
|---|
| 317 |
goto Lno; |
|---|
| 318 |
} |
|---|
| 319 |
goto Lyes; |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
case Tfloat64: |
|---|
| 323 |
{ |
|---|
| 324 |
volatile double f; |
|---|
| 325 |
if (type->isunsigned()) |
|---|
| 326 |
{ |
|---|
| 327 |
f = (double)value; |
|---|
| 328 |
if (f != value) |
|---|
| 329 |
goto Lno; |
|---|
| 330 |
} |
|---|
| 331 |
else |
|---|
| 332 |
{ |
|---|
| 333 |
f = (double)(long long)value; |
|---|
| 334 |
if (f != (long long)value) |
|---|
| 335 |
goto Lno; |
|---|
| 336 |
} |
|---|
| 337 |
goto Lyes; |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
case Tfloat80: |
|---|
| 341 |
{ |
|---|
| 342 |
volatile long double f; |
|---|
| 343 |
if (type->isunsigned()) |
|---|
| 344 |
{ |
|---|
| 345 |
f = (long double)value; |
|---|
| 346 |
if (f != value) |
|---|
| 347 |
goto Lno; |
|---|
| 348 |
} |
|---|
| 349 |
else |
|---|
| 350 |
{ |
|---|
| 351 |
f = (long double)(long long)value; |
|---|
| 352 |
if (f != (long long)value) |
|---|
| 353 |
goto Lno; |
|---|
| 354 |
} |
|---|
| 355 |
goto Lyes; |
|---|
| 356 |
} |
|---|
| 357 |
|
|---|
| 358 |
case Tpointer: |
|---|
| 359 |
//printf("type = %s\n", type->toBasetype()->toChars()); |
|---|
| 360 |
//printf("t = %s\n", t->toBasetype()->toChars()); |
|---|
| 361 |
if (ty == Tpointer && |
|---|
| 362 |
type->toBasetype()->nextOf()->ty == t->toBasetype()->nextOf()->ty) |
|---|
| 363 |
{ /* Allow things like: |
|---|
| 364 |
* const char* P = cast(char *)3; |
|---|
| 365 |
* char* q = P; |
|---|
| 366 |
*/ |
|---|
| 367 |
goto Lyes; |
|---|
| 368 |
} |
|---|
| 369 |
break; |
|---|
| 370 |
} |
|---|
| 371 |
return Expression::implicitConvTo(t); |
|---|
| 372 |
|
|---|
| 373 |
Lyes: |
|---|
| 374 |
//printf("MATCHconvert\n"); |
|---|
| 375 |
return MATCHconvert; |
|---|
| 376 |
|
|---|
| 377 |
Lno: |
|---|
| 378 |
//printf("MATCHnomatch\n"); |
|---|
| 379 |
return MATCHnomatch; |
|---|
| 380 |
} |
|---|
| 381 |
|
|---|
| 382 |
MATCH NullExp::implicitConvTo(Type *t) |
|---|
| 383 |
{ |
|---|
| 384 |
#if 0 |
|---|
| 385 |
printf("NullExp::implicitConvTo(this=%s, type=%s, t=%s, committed = %d)\n", |
|---|
| 386 |
toChars(), type->toChars(), t->toChars(), committed); |
|---|
| 387 |
#endif |
|---|
| 388 |
if (this->type->equals(t)) |
|---|
| 389 |
return MATCHexact; |
|---|
| 390 |
|
|---|
| 391 |
/* Allow implicit conversions from invariant to mutable|const, |
|---|
| 392 |
* and mutable to invariant. It works because, after all, a null |
|---|
| 393 |
* doesn't actually point to anything. |
|---|
| 394 |
*/ |
|---|
| 395 |
if (t->invariantOf()->equals(type->invariantOf())) |
|---|
| 396 |
return MATCHconst; |
|---|
| 397 |
|
|---|
| 398 |
// NULL implicitly converts to any pointer type or dynamic array |
|---|
| 399 |
if (type->ty == Tpointer && type->nextOf()->ty == Tvoid) |
|---|
| 400 |
{ |
|---|
| 401 |
if (t->ty == Ttypedef) |
|---|
| 402 |
t = ((TypeTypedef *)t)->sym->basetype; |
|---|
| 403 |
if (t->ty == Tpointer || t->ty == Tarray || |
|---|
| 404 |
t->ty == Taarray || t->ty == Tclass || |
|---|
| 405 |
t->ty == Tdelegate) |
|---|
| 406 |
return committed ? MATCHconvert : MATCHexact; |
|---|
| 407 |
} |
|---|
| 408 |
return Expression::implicitConvTo(t); |
|---|
| 409 |
} |
|---|
| 410 |
|
|---|
| 411 |
#if DMDV2 |
|---|
| 412 |
MATCH StructLiteralExp::implicitConvTo(Type *t) |
|---|
| 413 |
{ |
|---|
| 414 |
#if 0 |
|---|
| 415 |
printf("StructLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n", |
|---|
| 416 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 417 |
#endif |
|---|
| 418 |
MATCH m = Expression::implicitConvTo(t); |
|---|
| 419 |
if (m != MATCHnomatch) |
|---|
| 420 |
return m; |
|---|
| 421 |
if (type->ty == t->ty && type->ty == Tstruct && |
|---|
| 422 |
((TypeStruct *)type)->sym == ((TypeStruct *)t)->sym) |
|---|
| 423 |
{ |
|---|
| 424 |
m = MATCHconst; |
|---|
| 425 |
for (int i = 0; i < elements->dim; i++) |
|---|
| 426 |
{ Expression *e = (Expression *)elements->data[i]; |
|---|
| 427 |
Type *te = e->type; |
|---|
| 428 |
te = te->castMod(t->mod); |
|---|
| 429 |
MATCH m2 = e->implicitConvTo(te); |
|---|
| 430 |
//printf("\t%s => %s, match = %d\n", e->toChars(), te->toChars(), m2); |
|---|
| 431 |
if (m2 < m) |
|---|
| 432 |
m = m2; |
|---|
| 433 |
} |
|---|
| 434 |
} |
|---|
| 435 |
return m; |
|---|
| 436 |
} |
|---|
| 437 |
#endif |
|---|
| 438 |
|
|---|
| 439 |
MATCH StringExp::implicitConvTo(Type *t) |
|---|
| 440 |
{ MATCH m; |
|---|
| 441 |
|
|---|
| 442 |
#if 0 |
|---|
| 443 |
printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n", |
|---|
| 444 |
toChars(), committed, type->toChars(), t->toChars()); |
|---|
| 445 |
#endif |
|---|
| 446 |
if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid) |
|---|
| 447 |
{ |
|---|
| 448 |
return MATCHnomatch; |
|---|
| 449 |
} |
|---|
| 450 |
if (type->ty == Tsarray || type->ty == Tarray || type->ty == Tpointer) |
|---|
| 451 |
{ |
|---|
| 452 |
TY tyn = type->nextOf()->ty; |
|---|
| 453 |
if (tyn == Tchar || tyn == Twchar || tyn == Tdchar) |
|---|
| 454 |
{ Type *tn; |
|---|
| 455 |
MATCH m; |
|---|
| 456 |
|
|---|
| 457 |
switch (t->ty) |
|---|
| 458 |
{ |
|---|
| 459 |
case Tsarray: |
|---|
| 460 |
if (type->ty == Tsarray) |
|---|
| 461 |
{ |
|---|
| 462 |
if (((TypeSArray *)type)->dim->toInteger() != |
|---|
| 463 |
((TypeSArray *)t)->dim->toInteger()) |
|---|
| 464 |
return MATCHnomatch; |
|---|
| 465 |
TY tynto = t->nextOf()->ty; |
|---|
| 466 |
if (tynto == tyn) |
|---|
| 467 |
return MATCHexact; |
|---|
| 468 |
if (!committed && (tynto == Tchar || tynto == Twchar || tynto == Tdchar)) |
|---|
| 469 |
return MATCHexact; |
|---|
| 470 |
} |
|---|
| 471 |
else if (type->ty == Tarray) |
|---|
| 472 |
{ |
|---|
| 473 |
if (length() > |
|---|
| 474 |
((TypeSArray *)t)->dim->toInteger()) |
|---|
| 475 |
return MATCHnomatch; |
|---|
| 476 |
TY tynto = t->nextOf()->ty; |
|---|
| 477 |
if (tynto == tyn) |
|---|
| 478 |
return MATCHexact; |
|---|
| 479 |
if (!committed && (tynto == Tchar || tynto == Twchar || tynto == Tdchar)) |
|---|
| 480 |
return MATCHexact; |
|---|
| 481 |
} |
|---|
| 482 |
case Tarray: |
|---|
| 483 |
case Tpointer: |
|---|
| 484 |
tn = t->nextOf(); |
|---|
| 485 |
m = MATCHexact; |
|---|
| 486 |
if (type->nextOf()->mod != tn->mod) |
|---|
| 487 |
{ if (!tn->isConst()) |
|---|
| 488 |
return MATCHnomatch; |
|---|
| 489 |
m = MATCHconst; |
|---|
| 490 |
} |
|---|
| 491 |
switch (tn->ty) |
|---|
| 492 |
{ |
|---|
| 493 |
case Tchar: |
|---|
| 494 |
case Twchar: |
|---|
| 495 |
case Tdchar: |
|---|
| 496 |
if (!committed) |
|---|
| 497 |
return m; |
|---|
| 498 |
break; |
|---|
| 499 |
} |
|---|
| 500 |
break; |
|---|
| 501 |
} |
|---|
| 502 |
} |
|---|
| 503 |
} |
|---|
| 504 |
return Expression::implicitConvTo(t); |
|---|
| 505 |
#if 0 |
|---|
| 506 |
m = (MATCH)type->implicitConvTo(t); |
|---|
| 507 |
if (m) |
|---|
| 508 |
{ |
|---|
| 509 |
return m; |
|---|
| 510 |
} |
|---|
| 511 |
|
|---|
| 512 |
return MATCHnomatch; |
|---|
| 513 |
#endif |
|---|
| 514 |
} |
|---|
| 515 |
|
|---|
| 516 |
MATCH ArrayLiteralExp::implicitConvTo(Type *t) |
|---|
| 517 |
{ MATCH result = MATCHexact; |
|---|
| 518 |
|
|---|
| 519 |
#if 0 |
|---|
| 520 |
printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n", |
|---|
| 521 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 522 |
#endif |
|---|
| 523 |
Type *typeb = type->toBasetype(); |
|---|
| 524 |
Type *tb = t->toBasetype(); |
|---|
| 525 |
if ((tb->ty == Tarray || tb->ty == Tsarray) && |
|---|
| 526 |
(typeb->ty == Tarray || typeb->ty == Tsarray)) |
|---|
| 527 |
{ |
|---|
| 528 |
if (tb->ty == Tsarray) |
|---|
| 529 |
{ TypeSArray *tsa = (TypeSArray *)tb; |
|---|
| 530 |
if (elements->dim != tsa->dim->toInteger()) |
|---|
| 531 |
result = MATCHnomatch; |
|---|
| 532 |
} |
|---|
| 533 |
|
|---|
| 534 |
for (int i = 0; i < elements->dim; i++) |
|---|
| 535 |
{ Expression *e = (Expression *)elements->data[i]; |
|---|
| 536 |
MATCH m = (MATCH)e->implicitConvTo(tb->nextOf()); |
|---|
| 537 |
if (m < result) |
|---|
| 538 |
result = m; // remember worst match |
|---|
| 539 |
if (result == MATCHnomatch) |
|---|
| 540 |
break; // no need to check for worse |
|---|
| 541 |
} |
|---|
| 542 |
return result; |
|---|
| 543 |
} |
|---|
| 544 |
else |
|---|
| 545 |
return Expression::implicitConvTo(t); |
|---|
| 546 |
} |
|---|
| 547 |
|
|---|
| 548 |
MATCH AssocArrayLiteralExp::implicitConvTo(Type *t) |
|---|
| 549 |
{ MATCH result = MATCHexact; |
|---|
| 550 |
|
|---|
| 551 |
Type *typeb = type->toBasetype(); |
|---|
| 552 |
Type *tb = t->toBasetype(); |
|---|
| 553 |
if (tb->ty == Taarray && typeb->ty == Taarray) |
|---|
| 554 |
{ |
|---|
| 555 |
for (size_t i = 0; i < keys->dim; i++) |
|---|
| 556 |
{ Expression *e = (Expression *)keys->data[i]; |
|---|
| 557 |
MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->index); |
|---|
| 558 |
if (m < result) |
|---|
| 559 |
result = m; // remember worst match |
|---|
| 560 |
if (result == MATCHnomatch) |
|---|
| 561 |
break; // no need to check for worse |
|---|
| 562 |
e = (Expression *)values->data[i]; |
|---|
| 563 |
m = (MATCH)e->implicitConvTo(tb->nextOf()); |
|---|
| 564 |
if (m < result) |
|---|
| 565 |
result = m; // remember worst match |
|---|
| 566 |
if (result == MATCHnomatch) |
|---|
| 567 |
break; // no need to check for worse |
|---|
| 568 |
} |
|---|
| 569 |
return result; |
|---|
| 570 |
} |
|---|
| 571 |
else |
|---|
| 572 |
return Expression::implicitConvTo(t); |
|---|
| 573 |
} |
|---|
| 574 |
|
|---|
| 575 |
MATCH AddrExp::implicitConvTo(Type *t) |
|---|
| 576 |
{ |
|---|
| 577 |
#if 0 |
|---|
| 578 |
printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n", |
|---|
| 579 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 580 |
#endif |
|---|
| 581 |
MATCH result; |
|---|
| 582 |
|
|---|
| 583 |
result = type->implicitConvTo(t); |
|---|
| 584 |
//printf("\tresult = %d\n", result); |
|---|
| 585 |
|
|---|
| 586 |
if (result == MATCHnomatch) |
|---|
| 587 |
{ |
|---|
| 588 |
// Look for pointers to functions where the functions are overloaded. |
|---|
| 589 |
|
|---|
| 590 |
t = t->toBasetype(); |
|---|
| 591 |
|
|---|
| 592 |
if (e1->op == TOKoverloadset && |
|---|
| 593 |
(t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction) |
|---|
| 594 |
{ OverExp *eo = (OverExp *)e1; |
|---|
| 595 |
FuncDeclaration *f = NULL; |
|---|
| 596 |
for (int i = 0; i < eo->vars->a.dim; i++) |
|---|
| 597 |
{ Dsymbol *s = (Dsymbol *)eo->vars->a.data[i]; |
|---|
| 598 |
FuncDeclaration *f2 = s->isFuncDeclaration(); |
|---|
| 599 |
assert(f2); |
|---|
| 600 |
if (f2->overloadExactMatch(t->nextOf())) |
|---|
| 601 |
{ if (f) |
|---|
| 602 |
/* Error if match in more than one overload set, |
|---|
| 603 |
* even if one is a 'better' match than the other. |
|---|
| 604 |
*/ |
|---|
| 605 |
ScopeDsymbol::multiplyDefined(loc, f, f2); |
|---|
| 606 |
else |
|---|
| 607 |
f = f2; |
|---|
| 608 |
result = MATCHexact; |
|---|
| 609 |
} |
|---|
| 610 |
} |
|---|
| 611 |
} |
|---|
| 612 |
|
|---|
| 613 |
if (type->ty == Tpointer && type->nextOf()->ty == Tfunction && |
|---|
| 614 |
t->ty == Tpointer && t->nextOf()->ty == Tfunction && |
|---|
| 615 |
e1->op == TOKvar) |
|---|
| 616 |
{ |
|---|
| 617 |
/* I don't think this can ever happen - |
|---|
| 618 |
* it should have been |
|---|
| 619 |
* converted to a SymOffExp. |
|---|
| 620 |
*/ |
|---|
| 621 |
assert(0); |
|---|
| 622 |
VarExp *ve = (VarExp *)e1; |
|---|
| 623 |
FuncDeclaration *f = ve->var->isFuncDeclaration(); |
|---|
| 624 |
if (f && f->overloadExactMatch(t->nextOf())) |
|---|
| 625 |
result = MATCHexact; |
|---|
| 626 |
} |
|---|
| 627 |
} |
|---|
| 628 |
//printf("\tresult = %d\n", result); |
|---|
| 629 |
return result; |
|---|
| 630 |
} |
|---|
| 631 |
|
|---|
| 632 |
MATCH SymOffExp::implicitConvTo(Type *t) |
|---|
| 633 |
{ |
|---|
| 634 |
#if 0 |
|---|
| 635 |
printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n", |
|---|
| 636 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 637 |
#endif |
|---|
| 638 |
MATCH result; |
|---|
| 639 |
|
|---|
| 640 |
result = type->implicitConvTo(t); |
|---|
| 641 |
//printf("\tresult = %d\n", result); |
|---|
| 642 |
|
|---|
| 643 |
if (result == MATCHnomatch) |
|---|
| 644 |
{ |
|---|
| 645 |
// Look for pointers to functions where the functions are overloaded. |
|---|
| 646 |
FuncDeclaration *f; |
|---|
| 647 |
|
|---|
| 648 |
t = t->toBasetype(); |
|---|
| 649 |
if (type->ty == Tpointer && type->nextOf()->ty == Tfunction && |
|---|
| 650 |
(t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction) |
|---|
| 651 |
{ |
|---|
| 652 |
f = var->isFuncDeclaration(); |
|---|
| 653 |
if (f) |
|---|
| 654 |
{ f = f->overloadExactMatch(t->nextOf()); |
|---|
| 655 |
if (f) |
|---|
| 656 |
{ if ((t->ty == Tdelegate && (f->needThis() || f->isNested())) || |
|---|
| 657 |
(t->ty == Tpointer && !(f->needThis() || f->isNested()))) |
|---|
| 658 |
{ |
|---|
| 659 |
result = MATCHexact; |
|---|
| 660 |
} |
|---|
| 661 |
} |
|---|
| 662 |
} |
|---|
| 663 |
} |
|---|
| 664 |
} |
|---|
| 665 |
//printf("\tresult = %d\n", result); |
|---|
| 666 |
return result; |
|---|
| 667 |
} |
|---|
| 668 |
|
|---|
| 669 |
MATCH DelegateExp::implicitConvTo(Type *t) |
|---|
| 670 |
{ |
|---|
| 671 |
#if 0 |
|---|
| 672 |
printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n", |
|---|
| 673 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 674 |
#endif |
|---|
| 675 |
MATCH result; |
|---|
| 676 |
|
|---|
| 677 |
result = type->implicitConvTo(t); |
|---|
| 678 |
|
|---|
| 679 |
if (result == MATCHnomatch) |
|---|
| 680 |
{ |
|---|
| 681 |
// Look for pointers to functions where the functions are overloaded. |
|---|
| 682 |
FuncDeclaration *f; |
|---|
| 683 |
|
|---|
| 684 |
t = t->toBasetype(); |
|---|
| 685 |
if (type->ty == Tdelegate && type->nextOf()->ty == Tfunction && |
|---|
| 686 |
t->ty == Tdelegate && t->nextOf()->ty == Tfunction) |
|---|
| 687 |
{ |
|---|
| 688 |
if (func && func->overloadExactMatch(t->nextOf())) |
|---|
| 689 |
result = MATCHexact; |
|---|
| 690 |
} |
|---|
| 691 |
} |
|---|
| 692 |
return result; |
|---|
| 693 |
} |
|---|
| 694 |
|
|---|
| 695 |
MATCH OrExp::implicitConvTo(Type *t) |
|---|
| 696 |
{ |
|---|
| 697 |
MATCH result = Expression::implicitConvTo(t); |
|---|
| 698 |
|
|---|
| 699 |
if (result == MATCHnomatch) |
|---|
| 700 |
{ |
|---|
| 701 |
MATCH m1 = e1->implicitConvTo(t); |
|---|
| 702 |
MATCH m2 = e2->implicitConvTo(t); |
|---|
| 703 |
|
|---|
| 704 |
// Pick the worst match |
|---|
| 705 |
result = (m1 < m2) ? m1 : m2; |
|---|
| 706 |
} |
|---|
| 707 |
return result; |
|---|
| 708 |
} |
|---|
| 709 |
|
|---|
| 710 |
MATCH XorExp::implicitConvTo(Type *t) |
|---|
| 711 |
{ |
|---|
| 712 |
MATCH result = Expression::implicitConvTo(t); |
|---|
| 713 |
|
|---|
| 714 |
if (result == MATCHnomatch) |
|---|
| 715 |
{ |
|---|
| 716 |
MATCH m1 = e1->implicitConvTo(t); |
|---|
| 717 |
MATCH m2 = e2->implicitConvTo(t); |
|---|
| 718 |
|
|---|
| 719 |
// Pick the worst match |
|---|
| 720 |
result = (m1 < m2) ? m1 : m2; |
|---|
| 721 |
} |
|---|
| 722 |
return result; |
|---|
| 723 |
} |
|---|
| 724 |
|
|---|
| 725 |
MATCH CondExp::implicitConvTo(Type *t) |
|---|
| 726 |
{ |
|---|
| 727 |
MATCH m1 = e1->implicitConvTo(t); |
|---|
| 728 |
MATCH m2 = e2->implicitConvTo(t); |
|---|
| 729 |
//printf("CondExp: m1 %d m2 %d\n", m1, m2); |
|---|
| 730 |
|
|---|
| 731 |
// Pick the worst match |
|---|
| 732 |
return (m1 < m2) ? m1 : m2; |
|---|
| 733 |
} |
|---|
| 734 |
|
|---|
| 735 |
MATCH CommaExp::implicitConvTo(Type *t) |
|---|
| 736 |
{ |
|---|
| 737 |
return e2->implicitConvTo(t); |
|---|
| 738 |
} |
|---|
| 739 |
|
|---|
| 740 |
MATCH CastExp::implicitConvTo(Type *t) |
|---|
| 741 |
{ |
|---|
| 742 |
#if 0 |
|---|
| 743 |
printf("CastExp::implicitConvTo(this=%s, type=%s, t=%s)\n", |
|---|
| 744 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 745 |
#endif |
|---|
| 746 |
MATCH result; |
|---|
| 747 |
|
|---|
| 748 |
result = type->implicitConvTo(t); |
|---|
| 749 |
|
|---|
| 750 |
if (result == MATCHnomatch) |
|---|
| 751 |
{ |
|---|
| 752 |
if (t->isintegral() && |
|---|
| 753 |
e1->type->isintegral() && |
|---|
| 754 |
e1->implicitConvTo(t) != MATCHnomatch) |
|---|
| 755 |
result = MATCHconvert; |
|---|
| 756 |
else |
|---|
| 757 |
result = Expression::implicitConvTo(t); |
|---|
| 758 |
} |
|---|
| 759 |
return result; |
|---|
| 760 |
} |
|---|
| 761 |
|
|---|
| 762 |
/* ==================== castTo ====================== */ |
|---|
| 763 |
|
|---|
| 764 |
/************************************** |
|---|
| 765 |
* Do an explicit cast. |
|---|
| 766 |
*/ |
|---|
| 767 |
|
|---|
| 768 |
Expression *Expression::castTo(Scope *sc, Type *t) |
|---|
| 769 |
{ |
|---|
| 770 |
//printf("Expression::castTo(this=%s, t=%s)\n", toChars(), t->toChars()); |
|---|
| 771 |
#if 0 |
|---|
| 772 |
printf("Expression::castTo(this=%s, type=%s, t=%s)\n", |
|---|
| 773 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 774 |
#endif |
|---|
| 775 |
if (type == t) |
|---|
| 776 |
return this; |
|---|
| 777 |
Expression *e = this; |
|---|
| 778 |
Type *tb = t->toBasetype(); |
|---|
| 779 |
Type *typeb = type->toBasetype(); |
|---|
| 780 |
if (tb != typeb) |
|---|
| 781 |
{ |
|---|
| 782 |
// Do (type *) cast of (type [dim]) |
|---|
| 783 |
if (tb->ty == Tpointer && |
|---|
| 784 |
typeb->ty == Tsarray |
|---|
| 785 |
) |
|---|
| 786 |
{ |
|---|
| 787 |
//printf("Converting [dim] to *\n"); |
|---|
| 788 |
|
|---|
| 789 |
if (typeb->size(loc) == 0) |
|---|
| 790 |
e = new NullExp(loc); |
|---|
| 791 |
else |
|---|
| 792 |
e = new AddrExp(loc, e); |
|---|
| 793 |
} |
|---|
| 794 |
#if 0 |
|---|
| 795 |
else if (tb->ty == Tdelegate && type->ty != Tdelegate) |
|---|
| 796 |
{ |
|---|
| 797 |
TypeDelegate *td = (TypeDelegate *)tb; |
|---|
| 798 |
TypeFunction *tf = (TypeFunction *)td->nextOf(); |
|---|
| 799 |
return toDelegate(sc, tf->nextOf()); |
|---|
| 800 |
} |
|---|
| 801 |
#endif |
|---|
| 802 |
else |
|---|
| 803 |
{ |
|---|
| 804 |
if (typeb->ty == Tstruct) |
|---|
| 805 |
{ TypeStruct *ts = (TypeStruct *)typeb; |
|---|
| 806 |
if (!(tb->ty == Tstruct && ts->sym == ((TypeStruct *)tb)->sym) && |
|---|
| 807 |
ts->sym->aliasthis) |
|---|
| 808 |
{ /* Forward the cast to our alias this member, rewrite to: |
|---|
| 809 |
* cast(to)e1.aliasthis |
|---|
| 810 |
*/ |
|---|
| 811 |
Expression *e1 = new DotIdExp(loc, this, ts->sym->aliasthis->ident); |
|---|
| 812 |
Expression *e = new CastExp(loc, e1, tb); |
|---|
| 813 |
e = e->semantic(sc); |
|---|
| 814 |
return e; |
|---|
| 815 |
} |
|---|
| 816 |
} |
|---|
| 817 |
else if (typeb->ty == Tclass) |
|---|
| 818 |
{ TypeClass *ts = (TypeClass *)typeb; |
|---|
| 819 |
if (ts->sym->aliasthis) |
|---|
| 820 |
{ |
|---|
| 821 |
if (tb->ty == Tclass) |
|---|
| 822 |
{ |
|---|
| 823 |
ClassDeclaration *cdfrom = typeb->isClassHandle(); |
|---|
| 824 |
ClassDeclaration *cdto = tb->isClassHandle(); |
|---|
| 825 |
int offset; |
|---|
| 826 |
if (cdto->isBaseOf(cdfrom, &offset)) |
|---|
| 827 |
goto L1; |
|---|
| 828 |
} |
|---|
| 829 |
/* Forward the cast to our alias this member, rewrite to: |
|---|
| 830 |
* cast(to)e1.aliasthis |
|---|
| 831 |
*/ |
|---|
| 832 |
Expression *e1 = new DotIdExp(loc, this, ts->sym->aliasthis->ident); |
|---|
| 833 |
Expression *e = new CastExp(loc, e1, tb); |
|---|
| 834 |
e = e->semantic(sc); |
|---|
| 835 |
return e; |
|---|
| 836 |
} |
|---|
| 837 |
L1: ; |
|---|
| 838 |
} |
|---|
| 839 |
e = new CastExp(loc, e, tb); |
|---|
| 840 |
} |
|---|
| 841 |
} |
|---|
| 842 |
else |
|---|
| 843 |
{ |
|---|
| 844 |
e = e->copy(); // because of COW for assignment to e->type |
|---|
| 845 |
} |
|---|
| 846 |
assert(e != this); |
|---|
| 847 |
e->type = t; |
|---|
| 848 |
//printf("Returning: %s\n", e->toChars()); |
|---|
| 849 |
return e; |
|---|
| 850 |
} |
|---|
| 851 |
|
|---|
| 852 |
|
|---|
| 853 |
Expression *RealExp::castTo(Scope *sc, Type *t) |
|---|
| 854 |
{ Expression *e = this; |
|---|
| 855 |
if (type != t) |
|---|
| 856 |
{ |
|---|
| 857 |
if ((type->isreal() && t->isreal()) || |
|---|
| 858 |
(type->isimaginary() && t->isimaginary()) |
|---|
| 859 |
) |
|---|
| 860 |
{ e = copy(); |
|---|
| 861 |
e->type = t; |
|---|
| 862 |
} |
|---|
| 863 |
else |
|---|
| 864 |
e = Expression::castTo(sc, t); |
|---|
| 865 |
} |
|---|
| 866 |
return e; |
|---|
| 867 |
} |
|---|
| 868 |
|
|---|
| 869 |
|
|---|
| 870 |
Expression *ComplexExp::castTo(Scope *sc, Type *t) |
|---|
| 871 |
{ Expression *e = this; |
|---|
| 872 |
if (type != t) |
|---|
| 873 |
{ |
|---|
| 874 |
if (type->iscomplex() && t->iscomplex()) |
|---|
| 875 |
{ e = copy(); |
|---|
| 876 |
e->type = t; |
|---|
| 877 |
} |
|---|
| 878 |
else |
|---|
| 879 |
e = Expression::castTo(sc, t); |
|---|
| 880 |
} |
|---|
| 881 |
return e; |
|---|
| 882 |
} |
|---|
| 883 |
|
|---|
| 884 |
|
|---|
| 885 |
Expression *NullExp::castTo(Scope *sc, Type *t) |
|---|
| 886 |
{ NullExp *e; |
|---|
| 887 |
Type *tb; |
|---|
| 888 |
|
|---|
| 889 |
//printf("NullExp::castTo(t = %p)\n", t); |
|---|
| 890 |
if (type == t) |
|---|
| 891 |
{ |
|---|
| 892 |
committed = 1; |
|---|
| 893 |
return this; |
|---|
| 894 |
} |
|---|
| 895 |
e = (NullExp *)copy(); |
|---|
| 896 |
e->committed = 1; |
|---|
| 897 |
tb = t->toBasetype(); |
|---|
| 898 |
e->type = type->toBasetype(); |
|---|
| 899 |
if (tb != e->type) |
|---|
| 900 |
{ |
|---|
| 901 |
// NULL implicitly converts to any pointer type or dynamic array |
|---|
| 902 |
if (e->type->ty == Tpointer && e->type->nextOf()->ty == Tvoid && |
|---|
| 903 |
(tb->ty == Tpointer || tb->ty == Tarray || tb->ty == Taarray || |
|---|
| 904 |
tb->ty == Tdelegate)) |
|---|
| 905 |
{ |
|---|
| 906 |
#if 0 |
|---|
| 907 |
if (tb->ty == Tdelegate) |
|---|
| 908 |
{ TypeDelegate *td = (TypeDelegate *)tb; |
|---|
| 909 |
TypeFunction *tf = (TypeFunction *)td->nextOf(); |
|---|
| 910 |
|
|---|
| 911 |
if (!tf->varargs && |
|---|
| 912 |
!(tf->arguments && tf->arguments->dim) |
|---|
| 913 |
) |
|---|
| 914 |
{ |
|---|
| 915 |
return Expression::castTo(sc, t); |
|---|
| 916 |
} |
|---|
| 917 |
} |
|---|
| 918 |
#endif |
|---|
| 919 |
} |
|---|
| 920 |
else |
|---|
| 921 |
{ |
|---|
| 922 |
return e->Expression::castTo(sc, t); |
|---|
| 923 |
} |
|---|
| 924 |
} |
|---|
| 925 |
e->type = t; |
|---|
| 926 |
return e; |
|---|
| 927 |
} |
|---|
| 928 |
|
|---|
| 929 |
Expression *StringExp::castTo(Scope *sc, Type *t) |
|---|
| 930 |
{ |
|---|
| 931 |
/* This follows copy-on-write; any changes to 'this' |
|---|
| 932 |
* will result in a copy. |
|---|
| 933 |
* The this->string member is considered immutable. |
|---|
| 934 |
*/ |
|---|
| 935 |
int copied = 0; |
|---|
| 936 |
|
|---|
| 937 |
//printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed); |
|---|
| 938 |
|
|---|
| 939 |
if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid) |
|---|
| 940 |
{ |
|---|
| 941 |
error("cannot convert string literal to void*"); |
|---|
| 942 |
return new ErrorExp(); |
|---|
| 943 |
} |
|---|
| 944 |
|
|---|
| 945 |
StringExp *se = this; |
|---|
| 946 |
if (!committed) |
|---|
| 947 |
{ se = (StringExp *)copy(); |
|---|
| 948 |
se->committed = 1; |
|---|
| 949 |
copied = 1; |
|---|
| 950 |
} |
|---|
| 951 |
|
|---|
| 952 |
if (type == t) |
|---|
| 953 |
{ |
|---|
| 954 |
return se; |
|---|
| 955 |
} |
|---|
| 956 |
|
|---|
| 957 |
Type *tb = t->toBasetype(); |
|---|
| 958 |
//printf("\ttype = %s\n", type->toChars()); |
|---|
| 959 |
if (tb->ty == Tdelegate && type->toBasetype()->ty != Tdelegate) |
|---|
| 960 |
return Expression::castTo(sc, t); |
|---|
| 961 |
|
|---|
| 962 |
Type *typeb = type->toBasetype(); |
|---|
| 963 |
if (typeb == tb) |
|---|
| 964 |
{ |
|---|
| 965 |
if (!copied) |
|---|
| 966 |
{ se = (StringExp *)copy(); |
|---|
| 967 |
copied = 1; |
|---|
| 968 |
} |
|---|
| 969 |
se->type = t; |
|---|
| 970 |
return se; |
|---|
| 971 |
} |
|---|
| 972 |
|
|---|
| 973 |
if (committed && tb->ty == Tsarray && typeb->ty == Tarray) |
|---|
| 974 |
{ |
|---|
| 975 |
se = (StringExp *)copy(); |
|---|
| 976 |
se->sz = tb->nextOf()->size(); |
|---|
| 977 |
se->len = (len * sz) / se->sz; |
|---|
| 978 |
se->committed = 1; |
|---|
| 979 |
se->type = t; |
|---|
| 980 |
return se; |
|---|
| 981 |
} |
|---|
| 982 |
|
|---|
| 983 |
if (tb->ty != Tsarray && tb->ty != Tarray && tb->ty != Tpointer) |
|---|
| 984 |
{ if (!copied) |
|---|
| 985 |
{ se = (StringExp *)copy(); |
|---|
| 986 |
copied = 1; |
|---|
| 987 |
} |
|---|
| 988 |
goto Lcast; |
|---|
| 989 |
} |
|---|
| 990 |
if (typeb->ty != Tsarray && typeb->ty != Tarray && typeb->ty != Tpointer) |
|---|
| 991 |
{ if (!copied) |
|---|
| 992 |
{ se = (StringExp *)copy(); |
|---|
| 993 |
copied = 1; |
|---|
| 994 |
} |
|---|
| 995 |
goto Lcast; |
|---|
| 996 |
} |
|---|
| 997 |
|
|---|
| 998 |
if (typeb->nextOf()->size() == tb->nextOf()->size()) |
|---|
| 999 |
{ |
|---|
| 1000 |
if (!copied) |
|---|
| 1001 |
{ se = (StringExp *)copy(); |
|---|
| 1002 |
copied = 1; |
|---|
| 1003 |
} |
|---|
| 1004 |
if (tb->ty == Tsarray) |
|---|
| 1005 |
goto L2; // handle possible change in static array dimension |
|---|
| 1006 |
se->type = t; |
|---|
| 1007 |
return se; |
|---|
| 1008 |
} |
|---|
| 1009 |
|
|---|
| 1010 |
if (committed) |
|---|
| 1011 |
goto Lcast; |
|---|
| 1012 |
|
|---|
| 1013 |
#define X(tf,tt) ((tf) * 256 + (tt)) |
|---|
| 1014 |
{ |
|---|
| 1015 |
OutBuffer buffer; |
|---|
| 1016 |
size_t newlen = 0; |
|---|
| 1017 |
int tfty = typeb->nextOf()->toBasetype()->ty; |
|---|
| 1018 |
int ttty = tb->nextOf()->toBasetype()->ty; |
|---|
| 1019 |
switch (X(tfty, ttty)) |
|---|
| 1020 |
{ |
|---|
| 1021 |
case X(Tchar, Tchar): |
|---|
| 1022 |
case X(Twchar,Twchar): |
|---|
| 1023 |
case X(Tdchar,Tdchar): |
|---|
| 1024 |
break; |
|---|
| 1025 |
|
|---|
| 1026 |
case X(Tchar, Twchar): |
|---|
| 1027 |
for (size_t u = 0; u < len;) |
|---|
| 1028 |
{ unsigned c; |
|---|
| 1029 |
const char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c); |
|---|
| 1030 |
if (p) |
|---|
| 1031 |
error("%s", p); |
|---|
| 1032 |
else |
|---|
| 1033 |
buffer.writeUTF16(c); |
|---|
| 1034 |
} |
|---|
| 1035 |
newlen = buffer.offset / 2; |
|---|
| 1036 |
buffer.writeUTF16(0); |
|---|
| 1037 |
goto L1; |
|---|
| 1038 |
|
|---|
| 1039 |
case X(Tchar, Tdchar): |
|---|
| 1040 |
for (size_t u = 0; u < len;) |
|---|
| 1041 |
{ unsigned c; |
|---|
| 1042 |
const char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c); |
|---|
| 1043 |
if (p) |
|---|
| 1044 |
error("%s", p); |
|---|
| 1045 |
buffer.write4(c); |
|---|
| 1046 |
newlen++; |
|---|
| 1047 |
} |
|---|
| 1048 |
buffer.write4(0); |
|---|
| 1049 |
goto L1; |
|---|
| 1050 |
|
|---|
| 1051 |
case X(Twchar,Tchar): |
|---|
| 1052 |
for (size_t u = 0; u < len;) |
|---|
| 1053 |
{ unsigned c; |
|---|
| 1054 |
const char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c); |
|---|
| 1055 |
if (p) |
|---|
| 1056 |
error("%s", p); |
|---|
| 1057 |
else |
|---|
| 1058 |
buffer.writeUTF8(c); |
|---|
| 1059 |
} |
|---|
| 1060 |
newlen = buffer.offset; |
|---|
| 1061 |
buffer.writeUTF8(0); |
|---|
| 1062 |
goto L1; |
|---|
| 1063 |
|
|---|
| 1064 |
case X(Twchar,Tdchar): |
|---|
| 1065 |
for (size_t u = 0; u < len;) |
|---|
| 1066 |
{ unsigned c; |
|---|
| 1067 |
const char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c); |
|---|
| 1068 |
if (p) |
|---|
| 1069 |
error("%s", p); |
|---|
| 1070 |
buffer.write4(c); |
|---|
| 1071 |
newlen++; |
|---|
| 1072 |
} |
|---|
| 1073 |
buffer.write4(0); |
|---|
| 1074 |
goto L1; |
|---|
| 1075 |
|
|---|
| 1076 |
case X(Tdchar,Tchar): |
|---|
| 1077 |
for (size_t u = 0; u < len; u++) |
|---|
| 1078 |
{ |
|---|
| 1079 |
unsigned c = ((unsigned *)se->string)[u]; |
|---|
| 1080 |
if (!utf_isValidDchar(c)) |
|---|
| 1081 |
error("invalid UCS-32 char \\U%08x", c); |
|---|
| 1082 |
else |
|---|
| 1083 |
buffer.writeUTF8(c); |
|---|
| 1084 |
newlen++; |
|---|
| 1085 |
} |
|---|
| 1086 |
newlen = buffer.offset; |
|---|
| 1087 |
buffer.writeUTF8(0); |
|---|
| 1088 |
goto L1; |
|---|
| 1089 |
|
|---|
| 1090 |
case X(Tdchar,Twchar): |
|---|
| 1091 |
for (size_t u = 0; u < len; u++) |
|---|
| 1092 |
{ |
|---|
| 1093 |
unsigned c = ((unsigned *)se->string)[u]; |
|---|
| 1094 |
if (!utf_isValidDchar(c)) |
|---|
| 1095 |
error("invalid UCS-32 char \\U%08x", c); |
|---|
| 1096 |
else |
|---|
| 1097 |
buffer.writeUTF16(c); |
|---|
| 1098 |
newlen++; |
|---|
| 1099 |
} |
|---|
| 1100 |
newlen = buffer.offset / 2; |
|---|
| 1101 |
buffer.writeUTF16(0); |
|---|
| 1102 |
goto L1; |
|---|
| 1103 |
|
|---|
| 1104 |
L1: |
|---|
| 1105 |
if (!copied) |
|---|
| 1106 |
{ se = (StringExp *)copy(); |
|---|
| 1107 |
copied = 1; |
|---|
| 1108 |
} |
|---|
| 1109 |
se->string = buffer.extractData(); |
|---|
| 1110 |
se->len = newlen; |
|---|
| 1111 |
se->sz = tb->nextOf()->size(); |
|---|
| 1112 |
break; |
|---|
| 1113 |
|
|---|
| 1114 |
default: |
|---|
| 1115 |
assert(typeb->nextOf()->size() != tb->nextOf()->size()); |
|---|
| 1116 |
goto Lcast; |
|---|
| 1117 |
} |
|---|
| 1118 |
} |
|---|
| 1119 |
#undef X |
|---|
| 1120 |
L2: |
|---|
| 1121 |
assert(copied); |
|---|
| 1122 |
|
|---|
| 1123 |
// See if need to truncate or extend the literal |
|---|
| 1124 |
if (tb->ty == Tsarray) |
|---|
| 1125 |
{ |
|---|
| 1126 |
int dim2 = ((TypeSArray *)tb)->dim->toInteger(); |
|---|
| 1127 |
|
|---|
| 1128 |
//printf("dim from = %d, to = %d\n", se->len, dim2); |
|---|
| 1129 |
|
|---|
| 1130 |
// Changing dimensions |
|---|
| 1131 |
if (dim2 != se->len) |
|---|
| 1132 |
{ |
|---|
| 1133 |
// Copy when changing the string literal |
|---|
| 1134 |
unsigned newsz = se->sz; |
|---|
| 1135 |
void *s; |
|---|
| 1136 |
int d; |
|---|
| 1137 |
|
|---|
| 1138 |
d = (dim2 < se->len) ? dim2 : se->len; |
|---|
| 1139 |
s = (unsigned char *)mem.malloc((dim2 + 1) * newsz); |
|---|
| 1140 |
memcpy(s, se->string, d * newsz); |
|---|
| 1141 |
// Extend with 0, add terminating 0 |
|---|
| 1142 |
memset((char *)s + d * newsz, 0, (dim2 + 1 - d) * newsz); |
|---|
| 1143 |
se->string = s; |
|---|
| 1144 |
se->len = dim2; |
|---|
| 1145 |
} |
|---|
| 1146 |
} |
|---|
| 1147 |
se->type = t; |
|---|
| 1148 |
return se; |
|---|
| 1149 |
|
|---|
| 1150 |
Lcast: |
|---|
| 1151 |
Expression *e = new CastExp(loc, se, t); |
|---|
| 1152 |
e->type = t; // so semantic() won't be run on e |
|---|
| 1153 |
return e; |
|---|
| 1154 |
} |
|---|
| 1155 |
|
|---|
| 1156 |
Expression *AddrExp::castTo(Scope *sc, Type *t) |
|---|
| 1157 |
{ |
|---|
| 1158 |
Type *tb; |
|---|
| 1159 |
|
|---|
| 1160 |
#if 0 |
|---|
| 1161 |
printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n", |
|---|
| 1162 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 1163 |
#endif |
|---|
| 1164 |
Expression *e = this; |
|---|
| 1165 |
|
|---|
| 1166 |
tb = t->toBasetype(); |
|---|
| 1167 |
type = type->toBasetype(); |
|---|
| 1168 |
if (tb != type) |
|---|
| 1169 |
{ |
|---|
| 1170 |
// Look for pointers to functions where the functions are overloaded. |
|---|
| 1171 |
|
|---|
| 1172 |
if (e1->op == TOKoverloadset && |
|---|
| 1173 |
(t->ty == Tpointer || t->ty == Tdelegate) && t->nextOf()->ty == Tfunction) |
|---|
| 1174 |
{ OverExp *eo = (OverExp *)e1; |
|---|
| 1175 |
FuncDeclaration *f = NULL; |
|---|
| 1176 |
for (int i = 0; i < eo->vars->a.dim; i++) |
|---|
| 1177 |
{ Dsymbol *s = (Dsymbol *)eo->vars->a.data[i]; |
|---|
| 1178 |
FuncDeclaration *f2 = s->isFuncDeclaration(); |
|---|
| 1179 |
assert(f2); |
|---|
| 1180 |
if (f2->overloadExactMatch(t->nextOf())) |
|---|
| 1181 |
{ if (f) |
|---|
| 1182 |
/* Error if match in more than one overload set, |
|---|
| 1183 |
* even if one is a 'better' match than the other. |
|---|
| 1184 |
*/ |
|---|
| 1185 |
ScopeDsymbol::multiplyDefined(loc, f, f2); |
|---|
| 1186 |
else |
|---|
| 1187 |
f = f2; |
|---|
| 1188 |
} |
|---|
| 1189 |
} |
|---|
| 1190 |
if (f) |
|---|
| 1191 |
{ f->tookAddressOf++; |
|---|
| 1192 |
SymOffExp *se = new SymOffExp(loc, f, 0, 0); |
|---|
| 1193 |
se->semantic(sc); |
|---|
| 1194 |
// Let SymOffExp::castTo() do the heavy lifting |
|---|
| 1195 |
return se->castTo(sc, t); |
|---|
| 1196 |
} |
|---|
| 1197 |
} |
|---|
| 1198 |
|
|---|
| 1199 |
|
|---|
| 1200 |
if (type->ty == Tpointer && type->nextOf()->ty == Tfunction && |
|---|
| 1201 |
tb->ty == Tpointer && tb->nextOf()->ty == Tfunction && |
|---|
| 1202 |
e1->op == TOKvar) |
|---|
| 1203 |
{ |
|---|
| 1204 |
VarExp *ve = (VarExp *)e1; |
|---|
| 1205 |
FuncDeclaration *f = ve->var->isFuncDeclaration(); |
|---|
| 1206 |
if (f) |
|---|
| 1207 |
{ |
|---|
| 1208 |
assert(0); // should be SymOffExp instead |
|---|
| 1209 |
f = f->overloadExactMatch(tb->nextOf()); |
|---|
| 1210 |
if (f) |
|---|
| 1211 |
{ |
|---|
| 1212 |
e = new VarExp(loc, f); |
|---|
| 1213 |
e->type = f->type; |
|---|
| 1214 |
e = new AddrExp(loc, e); |
|---|
| 1215 |
e->type = t; |
|---|
| 1216 |
return e; |
|---|
| 1217 |
} |
|---|
| 1218 |
} |
|---|
| 1219 |
} |
|---|
| 1220 |
e = Expression::castTo(sc, t); |
|---|
| 1221 |
} |
|---|
| 1222 |
e->type = t; |
|---|
| 1223 |
return e; |
|---|
| 1224 |
} |
|---|
| 1225 |
|
|---|
| 1226 |
|
|---|
| 1227 |
Expression *TupleExp::castTo(Scope *sc, Type *t) |
|---|
| 1228 |
{ TupleExp *e = (TupleExp *)copy(); |
|---|
| 1229 |
e->exps = (Expressions *)exps->copy(); |
|---|
| 1230 |
for (size_t i = 0; i < e->exps->dim; i++) |
|---|
| 1231 |
{ Expression *ex = (Expression *)e->exps->data[i]; |
|---|
| 1232 |
ex = ex->castTo(sc, t); |
|---|
| 1233 |
e->exps->data[i] = (void *)ex; |
|---|
| 1234 |
} |
|---|
| 1235 |
return e; |
|---|
| 1236 |
} |
|---|
| 1237 |
|
|---|
| 1238 |
|
|---|
| 1239 |
Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t) |
|---|
| 1240 |
{ |
|---|
| 1241 |
#if 0 |
|---|
| 1242 |
printf("ArrayLiteralExp::castTo(this=%s, type=%s, => %s)\n", |
|---|
| 1243 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 1244 |
#endif |
|---|
| 1245 |
if (type == t) |
|---|
| 1246 |
return this; |
|---|
| 1247 |
ArrayLiteralExp *e = this; |
|---|
| 1248 |
Type *typeb = type->toBasetype(); |
|---|
| 1249 |
Type *tb = t->toBasetype(); |
|---|
| 1250 |
if ((tb->ty == Tarray || tb->ty == Tsarray) && |
|---|
| 1251 |
(typeb->ty == Tarray || typeb->ty == Tsarray) && |
|---|
| 1252 |
// Not trying to convert non-void[] to void[] |
|---|
| 1253 |
!(tb->nextOf()->toBasetype()->ty == Tvoid && typeb->nextOf()->toBasetype()->ty != Tvoid)) |
|---|
| 1254 |
{ |
|---|
| 1255 |
if (tb->ty == Tsarray) |
|---|
| 1256 |
{ TypeSArray *tsa = (TypeSArray *)tb; |
|---|
| 1257 |
if (elements->dim != tsa->dim->toInteger()) |
|---|
| 1258 |
goto L1; |
|---|
| 1259 |
} |
|---|
| 1260 |
|
|---|
| 1261 |
e = (ArrayLiteralExp *)copy(); |
|---|
| 1262 |
e->elements = (Expressions *)elements->copy(); |
|---|
| 1263 |
for (int i = 0; i < elements->dim; i++) |
|---|
| 1264 |
{ Expression *ex = (Expression *)elements->data[i]; |
|---|
| 1265 |
ex = ex->castTo(sc, tb->nextOf()); |
|---|
| 1266 |
e->elements->data[i] = (void *)ex; |
|---|
| 1267 |
} |
|---|
| 1268 |
e->type = t; |
|---|
| 1269 |
return e; |
|---|
| 1270 |
} |
|---|
| 1271 |
if (tb->ty == Tpointer && typeb->ty == Tsarray) |
|---|
| 1272 |
{ |
|---|
| 1273 |
Type *tp = typeb->nextOf()->pointerTo(); |
|---|
| 1274 |
if (!tp->equals(e->type)) |
|---|
| 1275 |
{ e = (ArrayLiteralExp *)copy(); |
|---|
| 1276 |
e->type = tp; |
|---|
| 1277 |
} |
|---|
| 1278 |
} |
|---|
| 1279 |
L1: |
|---|
| 1280 |
return e->Expression::castTo(sc, t); |
|---|
| 1281 |
} |
|---|
| 1282 |
|
|---|
| 1283 |
Expression *AssocArrayLiteralExp::castTo(Scope *sc, Type *t) |
|---|
| 1284 |
{ |
|---|
| 1285 |
if (type == t) |
|---|
| 1286 |
return this; |
|---|
| 1287 |
AssocArrayLiteralExp *e = this; |
|---|
| 1288 |
Type *typeb = type->toBasetype(); |
|---|
| 1289 |
Type *tb = t->toBasetype(); |
|---|
| 1290 |
if (tb->ty == Taarray && typeb->ty == Taarray && |
|---|
| 1291 |
tb->nextOf()->toBasetype()->ty != Tvoid) |
|---|
| 1292 |
{ |
|---|
| 1293 |
e = (AssocArrayLiteralExp *)copy(); |
|---|
| 1294 |
e->keys = (Expressions *)keys->copy(); |
|---|
| 1295 |
e->values = (Expressions *)values->copy(); |
|---|
| 1296 |
assert(keys->dim == values->dim); |
|---|
| 1297 |
for (size_t i = 0; i < keys->dim; i++) |
|---|
| 1298 |
{ Expression *ex = (Expression *)values->data[i]; |
|---|
| 1299 |
ex = ex->castTo(sc, tb->nextOf()); |
|---|
| 1300 |
e->values->data[i] = (void *)ex; |
|---|
| 1301 |
|
|---|
| 1302 |
ex = (Expression *)keys->data[i]; |
|---|
| 1303 |
ex = ex->castTo(sc, ((TypeAArray *)tb)->index); |
|---|
| 1304 |
e->keys->data[i] = (void *)ex; |
|---|
| 1305 |
} |
|---|
| 1306 |
e->type = t; |
|---|
| 1307 |
return e; |
|---|
| 1308 |
} |
|---|
| 1309 |
L1: |
|---|
| 1310 |
return e->Expression::castTo(sc, t); |
|---|
| 1311 |
} |
|---|
| 1312 |
|
|---|
| 1313 |
Expression *SymOffExp::castTo(Scope *sc, Type *t) |
|---|
| 1314 |
{ |
|---|
| 1315 |
#if 0 |
|---|
| 1316 |
printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n", |
|---|
| 1317 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 1318 |
#endif |
|---|
| 1319 |
if (type == t && hasOverloads == 0) |
|---|
| 1320 |
return this; |
|---|
| 1321 |
Expression *e; |
|---|
| 1322 |
Type *tb = t->toBasetype(); |
|---|
| 1323 |
Type *typeb = type->toBasetype(); |
|---|
| 1324 |
if (tb != typeb) |
|---|
| 1325 |
{ |
|---|
| 1326 |
// Look for pointers to functions where the functions are overloaded. |
|---|
| 1327 |
FuncDeclaration *f; |
|---|
| 1328 |
|
|---|
| 1329 |
if (hasOverloads && |
|---|
| 1330 |
typeb->ty == Tpointer && typeb->nextOf()->ty == Tfunction && |
|---|
| 1331 |
(tb->ty == Tpointer || tb->ty == Tdelegate) && tb->nextOf()->ty == Tfunction) |
|---|
| 1332 |
{ |
|---|
| 1333 |
f = var->isFuncDeclaration(); |
|---|
| 1334 |
if (f) |
|---|
| 1335 |
{ |
|---|
| 1336 |
f = f->overloadExactMatch(tb->nextOf()); |
|---|
| 1337 |
if (f) |
|---|
| 1338 |
{ |
|---|
| 1339 |
if (tb->ty == Tdelegate) |
|---|
| 1340 |
{ |
|---|
| 1341 |
if (f->needThis() && hasThis(sc)) |
|---|
| 1342 |
{ |
|---|
| 1343 |
e = new DelegateExp(loc, new ThisExp(loc), f); |
|---|
| 1344 |
e = e->semantic(sc); |
|---|
| 1345 |
} |
|---|
| 1346 |
else if (f->isNested()) |
|---|
| 1347 |
{ |
|---|
| 1348 |
e = new DelegateExp(loc, new IntegerExp(0), f); |
|---|
| 1349 |
e = e->semantic(sc); |
|---|
| 1350 |
} |
|---|
| 1351 |
else if (f->needThis()) |
|---|
| 1352 |
{ error("no 'this' to create delegate for %s", f->toChars()); |
|---|
| 1353 |
return new ErrorExp(); |
|---|
| 1354 |
} |
|---|
| 1355 |
else |
|---|
| 1356 |
{ error("cannot cast from function pointer to delegate"); |
|---|
| 1357 |
return new ErrorExp(); |
|---|
| 1358 |
} |
|---|
| 1359 |
} |
|---|
| 1360 |
else |
|---|
| 1361 |
{ |
|---|
| 1362 |
e = new SymOffExp(loc, f, 0); |
|---|
| 1363 |
e->type = t; |
|---|
| 1364 |
} |
|---|
| 1365 |
#if DMDV2 |
|---|
| 1366 |
f->tookAddressOf++; |
|---|
| 1367 |
#endif |
|---|
| 1368 |
return e; |
|---|
| 1369 |
} |
|---|
| 1370 |
} |
|---|
| 1371 |
} |
|---|
| 1372 |
e = Expression::castTo(sc, t); |
|---|
| 1373 |
} |
|---|
| 1374 |
else |
|---|
| 1375 |
{ e = copy(); |
|---|
| 1376 |
e->type = t; |
|---|
| 1377 |
((SymOffExp *)e)->hasOverloads = 0; |
|---|
| 1378 |
} |
|---|
| 1379 |
return e; |
|---|
| 1380 |
} |
|---|
| 1381 |
|
|---|
| 1382 |
Expression *DelegateExp::castTo(Scope *sc, Type *t) |
|---|
| 1383 |
{ |
|---|
| 1384 |
#if 0 |
|---|
| 1385 |
printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n", |
|---|
| 1386 |
toChars(), type->toChars(), t->toChars()); |
|---|
| 1387 |
#endif |
|---|
| 1388 |
static char msg[] = "cannot form delegate due to covariant return type"; |
|---|
| 1389 |
|
|---|
| 1390 |
Expression *e = this; |
|---|
| 1391 |
Type *tb = t->toBasetype(); |
|---|
| 1392 |
Type *typeb = type->toBasetype(); |
|---|
| 1393 |
if (tb != typeb) |
|---|
| 1394 |
{ |
|---|
| 1395 |
// Look for delegates to functions where the functions are overloaded. |
|---|
| 1396 |
FuncDeclaration *f; |
|---|
| 1397 |
|
|---|
| 1398 |
if (typeb->ty == Tdelegate && typeb->nextOf()->ty == Tfunction && |
|---|
| 1399 |
tb->ty == Tdelegate && tb->nextOf()->ty == Tfunction) |
|---|
| 1400 |
{ |
|---|
| 1401 |
if (func) |
|---|
| 1402 |
{ |
|---|
| 1403 |
f = func->overloadExactMatch(tb->nextOf()); |
|---|
| 1404 |
if (f) |
|---|
| 1405 |
{ int offset; |
|---|
| 1406 |
if (f->tintro && f->tintro->nextOf()->isBaseOf(f->type->nextOf(), &offset) && offset) |
|---|
| 1407 |
error("%s", msg); |
|---|
| 1408 |
f->tookAddressOf++; |
|---|
| 1409 |
e = new DelegateExp(loc, e1, f); |
|---|
| 1410 |
e->type = t; |
|---|
| 1411 |
return e; |
|---|
| 1412 |
} |
|---|
| 1413 |
if (func->tintro) |
|---|
| 1414 |
error("%s", msg); |
|---|
| 1415 |
} |
|---|
| 1416 |
} |
|---|
| 1417 |
e = Expression::castTo(sc, t); |
|---|
| 1418 |
} |
|---|
| 1419 |
else |
|---|
| 1420 |
{ int offset; |
|---|
| 1421 |
|
|---|
| 1422 |
func->tookAddressOf++; |
|---|
| 1423 |
if (func->tintro && func->tintro->nextOf()->isBaseOf(func->type->nextOf(), &offset) && offset) |
|---|
| 1424 |
error("%s", msg); |
|---|
| 1425 |
e = copy(); |
|---|
| 1426 |
e->type = t; |
|---|
| 1427 |
} |
|---|
| 1428 |
return e; |
|---|
| 1429 |
} |
|---|
| 1430 |
|
|---|
| 1431 |
Expression *CondExp::castTo(Scope *sc, Type *t) |
|---|
| 1432 |
{ |
|---|
| 1433 |
Expression *e = this; |
|---|
| 1434 |
|
|---|
| 1435 |
if (type != t) |
|---|
| 1436 |
{ |
|---|
| 1437 |
if (1 || e1->op == TOKstring || e2->op == TOKstring) |
|---|
| 1438 |
{ e = new CondExp(loc, econd, e1->castTo(sc, t), e2->castTo(sc, t)); |
|---|
| 1439 |
e->type = t; |
|---|
| 1440 |
} |
|---|
| 1441 |
else |
|---|
| 1442 |
e = Expression::castTo(sc, t); |
|---|
| 1443 |
} |
|---|
| 1444 |
return e; |
|---|
| 1445 |
} |
|---|
| 1446 |
|
|---|
| 1447 |
Expression *CommaExp::castTo(Scope *sc, Type *t) |
|---|
| 1448 |
{ |
|---|
| 1449 |
Expression *e2c = e2->castTo(sc, t); |
|---|
| 1450 |
Expression *e; |
|---|
| 1451 |
|
|---|
| 1452 |
if (e2c != e2) |
|---|
| 1453 |
{ |
|---|
| 1454 |
e = new CommaExp(loc, e1, e2c); |
|---|
| 1455 |
e->type = e2c->type; |
|---|
| 1456 |
} |
|---|
| 1457 |
else |
|---|
| 1458 |
{ e = this; |
|---|
| 1459 |
e->type = e2->type; |
|---|
| 1460 |
} |
|---|
| 1461 |
return e; |
|---|
| 1462 |
} |
|---|
| 1463 |
|
|---|
| 1464 |
/* ==================== ====================== */ |
|---|
| 1465 |
|
|---|
| 1466 |
/**************************************** |
|---|
| 1467 |
* Scale addition/subtraction to/from pointer. |
|---|
| 1468 |
*/ |
|---|
| 1469 |
|
|---|
| 1470 |
Expression *BinExp::scaleFactor(Scope *sc) |
|---|
| 1471 |
{ d_uns64 stride; |
|---|
| 1472 |
Type *t1b = e1->type->toBasetype(); |
|---|
| 1473 |
Type *t2b = e2->type->toBasetype(); |
|---|
| 1474 |
|
|---|
| 1475 |
if (t1b->ty == Tpointer && t2b->isintegral()) |
|---|
| 1476 |
{ // Need to adjust operator by the stride |
|---|
| 1477 |
// Replace (ptr + int) with (ptr + (int * stride)) |
|---|
| 1478 |
Type *t = Type::tptrdiff_t; |
|---|
| 1479 |
|
|---|
| 1480 |
stride = t1b->nextOf()->size(loc); |
|---|
| 1481 |
if (!t->equals(t2b)) |
|---|
| 1482 |
e2 = e2->castTo(sc, t); |
|---|
| 1483 |
e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t)); |
|---|
| 1484 |
e2->type = t; |
|---|
| 1485 |
type = e1->type; |
|---|
| 1486 |
} |
|---|
| 1487 |
else if (t2b->ty == Tpointer && t1b->isintegral()) |
|---|
| 1488 |
{ // Need to adjust operator by the stride |
|---|
| 1489 |
// Replace (int + ptr) with (ptr + (int * stride)) |
|---|
| 1490 |
Type *t = Type::tptrdiff_t; |
|---|
| 1491 |
Expression *e; |
|---|
| 1492 |
|
|---|
| 1493 |
stride = t2b->nextOf()->size(loc); |
|---|
| 1494 |
if (!t->equals(t1b)) |
|---|
| 1495 |
e = e1->castTo(sc, t); |
|---|
| 1496 |
else |
|---|
| 1497 |
e = e1; |
|---|
| 1498 |
e = new MulExp(loc, e, new IntegerExp(0, stride, t)); |
|---|
| 1499 |
e->type = t; |
|---|
| 1500 |
type = e2->type; |
|---|
| 1501 |
e1 = e2; |
|---|
| 1502 |
e2 = e; |
|---|
| 1503 |
} |
|---|
| 1504 |
return this; |
|---|
| 1505 |
} |
|---|
| 1506 |
|
|---|
| 1507 |
/************************************** |
|---|
| 1508 |
* Return true if e is an empty array literal with dimensionality |
|---|
| 1509 |
* equal to or less than type of other array. |
|---|
| 1510 |
* [], [[]], [[[]]], etc. |
|---|
| 1511 |
* I.e., make sure that [1,2] is compatible with [], |
|---|
| 1512 |
* [[1,2]] is compatible with [[]], etc. |
|---|
| 1513 |
*/ |
|---|
| 1514 |
bool isVoidArrayLiteral(Expression *e, Type *other) |
|---|
| 1515 |
{ |
|---|
| 1516 |
while (e->op == TOKarrayliteral && e->type->ty == Tarray |
|---|
| 1517 |
&& (((ArrayLiteralExp *)e)->elements->dim == 1)) |
|---|
| 1518 |
{ |
|---|
| 1519 |
e = (Expression *)((ArrayLiteralExp *)e)->elements->data[0]; |
|---|
| 1520 |
if (other->ty == Tsarray || other->ty == Tarray) |
|---|
| 1521 |
other = other->nextOf(); |
|---|
| 1522 |
else |
|---|
| 1523 |
return false; |
|---|
| 1524 |
} |
|---|
| 1525 |
if (other->ty != Tsarray && other->ty != Tarray) |
|---|
| 1526 |
return false; |
|---|
| 1527 |
Type *t = e->type; |
|---|
| 1528 |
return (e->op == TOKarrayliteral && t->ty == Tarray && |
|---|
| 1529 |
t->nextOf()->ty == Tvoid && |
|---|
| 1530 |
((ArrayLiteralExp *)e)->elements->dim == 0); |
|---|
| 1531 |
} |
|---|
| 1532 |
|
|---|
| 1533 |
|
|---|
| 1534 |
/************************************** |
|---|
| 1535 |
* Combine types. |
|---|
| 1536 |
* Output: |
|---|
| 1537 |
* *pt merged type, if *pt is not NULL |
|---|
| 1538 |
* *pe1 rewritten e1 |
|---|
| 1539 |
* *pe2 rewritten e2 |
|---|
| 1540 |
* Returns: |
|---|
| 1541 |
* !=0 success |
|---|
| 1542 |
* 0 failed |
|---|
| 1543 |
*/ |
|---|
| 1544 |
|
|---|
| 1545 |
int typeMerge(Scope *sc, Expression *e, Type **pt, Expression **pe1, Expression **pe2) |
|---|
| 1546 |
{ |
|---|
| 1547 |
//printf("typeMerge() %s op %s\n", (*pe1)->toChars(), (*pe2)->toChars()); |
|---|
| 1548 |
//dump(0); |
|---|
| 1549 |
|
|---|
| 1550 |
Expression *e1 = (*pe1)->integralPromotions(sc); |
|---|
| 1551 |
Expression *e2 = (*pe2)->integralPromotions(sc); |
|---|
| 1552 |
|
|---|
| 1553 |
Type *t1 = e1->type; |
|---|
| 1554 |
Type *t2 = e2->type; |
|---|
| 1555 |
assert(t1); |
|---|
| 1556 |
Type *t = t1; |
|---|
| 1557 |
|
|---|
| 1558 |
//if (t1) printf("\tt1 = %s\n", t1->toChars()); |
|---|
| 1559 |
//if (t2) printf("\tt2 = %s\n", t2->toChars()); |
|---|
| 1560 |
#ifdef DEBUG |
|---|
| 1561 |
if (!t2) printf("\te2 = '%s'\n", e2->toChars()); |
|---|
| 1562 |
#endif |
|---|
| 1563 |
assert(t2); |
|---|
| 1564 |
|
|---|
| 1565 |
Type *t1b = t1->toBasetype(); |
|---|
| 1566 |
Type *t2b = t2->toBasetype(); |
|---|
| 1567 |
|
|---|
| 1568 |
TY ty = (TY)Type::impcnvResult[t1b->ty][t2b->ty]; |
|---|
| 1569 |
if (ty != Terror) |
|---|
| 1570 |
{ |
|---|
| 1571 |
TY ty1 = (TY)Type::impcnvType1[t1b->ty][t2b->ty]; |
|---|
| 1572 |
TY ty2 = (TY)Type::impcnvType2[t1b->ty][t2b->ty]; |
|---|
| 1573 |
|
|---|
| 1574 |
if (t1b->ty == ty1) // if no promotions |
|---|
| 1575 |
{ |
|---|
| 1576 |
if (t1 == t2) |
|---|
| 1577 |
{ |
|---|
| 1578 |
t = t1; |
|---|
| 1579 |
goto Lret; |
|---|
| 1580 |
} |
|---|
| 1581 |
|
|---|
| 1582 |
if (t1b == t2b) |
|---|
| 1583 |
{ |
|---|
| 1584 |
t = t1b; |
|---|
| 1585 |
goto Lret; |
|---|
| 1586 |
} |
|---|
| 1587 |
} |
|---|
| 1588 |
|
|---|
| 1589 |
t = Type::basic[ty]; |
|---|
| 1590 |
|
|---|
| 1591 |
t1 = Type::basic[ty1]; |
|---|
| 1592 |
t2 = Type::basic[ty2]; |
|---|
| 1593 |
e1 = e1->castTo(sc, t1); |
|---|
| 1594 |
e2 = e2->castTo(sc, t2); |
|---|
| 1595 |
//printf("after typeCombine():\n"); |
|---|
| 1596 |
//dump(0); |
|---|
| 1597 |
//printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2); |
|---|
| 1598 |
goto Lret; |
|---|
| 1599 |
} |
|---|
| 1600 |
|
|---|
| 1601 |
t1 = t1b; |
|---|
| 1602 |
t2 = t2b; |
|---|
| 1603 |
|
|---|
| 1604 |
Lagain: |
|---|
| 1605 |
if (t1 == t2) |
|---|
| 1606 |
{ |
|---|
| 1607 |
} |
|---|
| 1608 |
else if (t1->ty == Tpointer && t2->ty == Tpointer) |
|---|
| 1609 |
{ |
|---|
| 1610 |
// Bring pointers to compatible type |
|---|
| 1611 |
Type *t1n = t1->nextOf(); |
|---|
| 1612 |
Type *t2n = t2->nextOf(); |
|---|
| 1613 |
|
|---|
| 1614 |
if (t1n == t2n) |
|---|
| 1615 |
; |
|---|
| 1616 |
else if (t1n->ty == Tvoid) // pointers to void are always compatible |
|---|
| 1617 |
t = t2; |
|---|
| 1618 |
else if (t2n->ty == Tvoid) |
|---|
| 1619 |
; |
|---|
| 1620 |
else if (t1n->mod != t2n->mod) |
|---|
| 1621 |
{ |
|---|
| 1622 |
t1 = t1n->mutableOf()->constOf()->pointerTo(); |
|---|
| 1623 |
t2 = t2n->mutableOf()->constOf()->pointerTo(); |
|---|
| 1624 |
t = t1; |
|---|
| 1625 |
goto Lagain; |
|---|
| 1626 |
} |
|---|
| 1627 |
else if (t1n->ty == Tclass && t2n->ty == Tclass) |
|---|
| 1628 |
{ ClassDeclaration *cd1 = t1n->isClassHandle(); |
|---|
| 1629 |
ClassDeclaration *cd2 = t2n->isClassHandle(); |
|---|
| 1630 |
int offset; |
|---|
| 1631 |
|
|---|
| 1632 |
if (cd1->isBaseOf(cd2, &offset)) |
|---|
| 1633 |
{ |
|---|
| 1634 |
if (offset) |
|---|
| 1635 |
e2 = e2->castTo(sc, t); |
|---|
| 1636 |
} |
|---|
| 1637 |
else if (cd2->isBaseOf(cd1, &offset)) |
|---|
| 1638 |
{ |
|---|
| 1639 |
t = t2; |
|---|
| 1640 |
if (offset) |
|---|
| 1641 |
e1 = e1->castTo(sc, t); |
|---|
| 1642 |
} |
|---|
| 1643 |
else |
|---|
| 1644 |
goto Lincompatible; |
|---|
| 1645 |
} |
|---|
| 1646 |
else |
|---|
| 1647 |
goto Lincompatible; |
|---|
| 1648 |
} |
|---|
| 1649 |
else if ((t1->ty == Tsarray || t1->ty == Tarray) && |
|---|
| 1650 |
(e2->op == TOKnull && t2->ty == Tpointer && t2->nextOf()->ty == Tvoid || |
|---|
| 1651 |
// if e2 is void[] |
|---|
| 1652 |
e2->op == TOKarrayliteral && t2->ty == Tsarray && t2->nextOf()->ty == Tvoid && ((TypeSArray *)t2)->dim->toInteger() == 0 || |
|---|
| 1653 |
isVoidArrayLiteral(e2, t1)) |
|---|
| 1654 |
) |
|---|
| 1655 |
{ /* (T[n] op void*) => T[] |
|---|
| 1656 |
* (T[] op void*) => T[] |
|---|
| 1657 |
* (T[n] op void[0]) => T[] |
|---|
| 1658 |
* (T[] op void[0]) => T[] |
|---|
| 1659 |
* (T[n] op void[]) => T[] |
|---|
| 1660 |
* (T[] op void[]) => T[] |
|---|
| 1661 |
*/ |
|---|
| 1662 |
goto Lx1; |
|---|
| 1663 |
} |
|---|
| 1664 |
else if ((t2->ty == Tsarray || t2->ty == Tarray) && |
|---|
| 1665 |
(e1->op == TOKnull && t1->ty == Tpointer && t1->nextOf()->ty == Tvoid || |
|---|
| 1666 |
e1->op == TOKarrayliteral && t1->ty == Tsarray && t1->nextOf()->ty == Tvoid && ((TypeSArray *)t1)->dim->toInteger() == 0 || |
|---|
| 1667 |
isVoidArrayLiteral(e1, t2)) |
|---|
| 1668 |
) |
|---|
| 1669 |
{ /* (void* op T[n]) => T[] |
|---|
| 1670 |
* (void* op T[]) => T[] |
|---|
| 1671 |
* (void[0] op T[n]) => T[] |
|---|
| 1672 |
* (void[0] op T[]) => T[] |
|---|
| 1673 |
* (void[] op T[n]) => T[] |
|---|
| 1674 |
* (void[] op T[]) => T[] |
|---|
| 1675 |
*/ |
|---|
| 1676 |
goto Lx2; |
|---|
| 1677 |
} |
|---|
| 1678 |
else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2)) |
|---|
| 1679 |
{ |
|---|
| 1680 |
goto Lt2; |
|---|
| 1681 |
} |
|---|
| 1682 |
else if ((t2->ty == Tsarray || t2->ty == Tarray) && t2->implicitConvTo(t1)) |
|---|
| 1683 |
{ |
|---|
| 1684 |
goto Lt1; |
|---|
| 1685 |
} |
|---|
| 1686 |
/* If one is mutable and the other invariant, then retry |
|---|
| 1687 |
* with both of them as const |
|---|
| 1688 |
*/ |
|---|
| 1689 |
else if ((t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Tpointer) && |
|---|
| 1690 |
(t2->ty == Tsarray || t2->ty == Tarray || t2->ty == Tpointer) && |
|---|
| 1691 |
t1->nextOf()->mod != t2->nextOf()->mod |
|---|
| 1692 |
) |
|---|
| 1693 |
{ unsigned char mod = MODmerge(t1->nextOf()->mod, t2->nextOf()->mod); |
|---|
| 1694 |
|
|---|
| 1695 |
if (t1->ty == Tpointer) |
|---|
| 1696 |
t1 = t1->nextOf()->castMod(mod)->pointerTo(); |
|---|
| 1697 |
else |
|---|
| 1698 |
t1 = t1->nextOf()->castMod(mod)->arrayOf(); |
|---|
| 1699 |
|
|---|
| 1700 |
if (t2->ty == Tpointer) |
|---|
| 1701 |
t2 = t2->nextOf()->castMod(mod)->pointerTo(); |
|---|
| 1702 |
else |
|---|
| 1703 |
t2 = t2->nextOf()->castMod(mod)->arrayOf(); |
|---|
| 1704 |
t = t1; |
|---|
| 1705 |
goto Lagain; |
|---|
| 1706 |
} |
|---|
| 1707 |
else if (t1->ty == Tclass || t2->ty == Tclass) |
|---|
| 1708 |
{ |
|---|
| 1709 |
if (t1->mod != t2->mod) |
|---|
| 1710 |
{ unsigned char mod = MODmerge(t1->mod, t2->mod); |
|---|
| 1711 |
t1 = t1->castMod(mod); |
|---|
| 1712 |
t2 = t2->castMod(mod); |
|---|
| 1713 |
t = t1; |
|---|
| 1714 |
goto Lagain; |
|---|
| 1715 |
} |
|---|
| 1716 |
|
|---|
| 1717 |
while (1) |
|---|
| 1718 |
{ |
|---|
| 1719 |
int i1 = e2->implicitConvTo(t1); |
|---|
| 1720 |
int i2 = e1->implicitConvTo(t2); |
|---|
| 1721 |
|
|---|
| 1722 |
if (i1 && i2) |
|---|
| 1723 |
{ |
|---|
| 1724 |
// We have the case of class vs. void*, so pick class |
|---|
| 1725 |
if (t1->ty == Tpointer) |
|---|
| 1726 |
i1 = 0; |
|---|
| 1727 |
else if (t2->ty == Tpointer) |
|---|
| 1728 |
i2 = 0; |
|---|
| 1729 |
} |
|---|
| 1730 |
|
|---|
| 1731 |
if (i2) |
|---|
| 1732 |
{ |
|---|
| 1733 |
goto Lt2; |
|---|
| 1734 |
} |
|---|
| 1735 |
else if (i1) |
|---|
| 1736 |
{ |
|---|
| 1737 |
goto Lt1; |
|---|
| 1738 |
} |
|---|
| 1739 |
else if (t1->ty == Tclass && t2->ty == Tclass) |
|---|
| 1740 |
{ TypeClass *tc1 = (TypeClass *)t1; |
|---|
| 1741 |
TypeClass *tc2 = (TypeClass *)t2; |
|---|
| 1742 |
|
|---|
| 1743 |
/* Pick 'tightest' type |
|---|
| 1744 |
*/ |
|---|
| 1745 |
ClassDeclaration *cd1 = tc1->sym->baseClass; |
|---|
| 1746 |
ClassDeclaration *cd2 = tc2->sym->baseClass; |
|---|
| 1747 |
|
|---|
| 1748 |
if (cd1 && cd2) |
|---|
| 1749 |
{ t1 = cd1->type; |
|---|
| 1750 |
t2 = cd2->type; |
|---|
| 1751 |
} |
|---|
| 1752 |
else if (cd1) |
|---|
| 1753 |
t1 = cd1->type; |
|---|
| 1754 |
else if (cd2) |
|---|
| 1755 |
t2 = cd2->type; |
|---|
| 1756 |
else |
|---|
| 1757 |
goto Lincompatible; |
|---|
| 1758 |
} |
|---|
| 1759 |
else |
|---|
| 1760 |
goto Lincompatible; |
|---|
| 1761 |
} |
|---|
| 1762 |
} |
|---|
| 1763 |
else if (t1->ty == Tstruct && t2->ty == Tstruct) |
|---|
| 1764 |
{ |
|---|
| 1765 |
if (((TypeStruct *)t1)->sym != ((TypeStruct *)t2)->sym) |
|---|
| 1766 |
goto Lincompatible; |
|---|
| 1767 |
} |
|---|
| 1768 |
else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2)) |
|---|
| 1769 |
{ |
|---|
| 1770 |
goto Lt2; |
|---|
| 1771 |
} |
|---|
| 1772 |
else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1)) |
|---|
| 1773 |
{ |
|---|
| 1774 |
goto Lt1; |
|---|
| 1775 |
} |
|---|
| 1776 |
else if (t1->ty == Tsarray && t2->ty == Tsarray && |
|---|
| 1777 |
e2->implicitConvTo(t1->nextOf()->arrayOf())) |
|---|
| 1778 |
{ |
|---|
| 1779 |
Lx1: |
|---|
| 1780 |
t = t1->nextOf()->arrayOf(); // T[] |
|---|
| 1781 |
e1 = e1->castTo(sc, t); |
|---|
| 1782 |
e2 = e2->castTo(sc, t); |
|---|
| 1783 |
} |
|---|
| 1784 |
else if (t1->ty == Tsarray && t2->ty == Tsarray && |
|---|
| 1785 |
e1->implicitConvTo(t2->nextOf()->arrayOf())) |
|---|
| 1786 |
{ |
|---|
| 1787 |
Lx2: |
|---|
| 1788 |
t = t2->nextOf()->arrayOf(); |
|---|
| 1789 |
e1 = e1->castTo(sc, t); |
|---|
| 1790 |
e2 = e2->castTo(sc, t); |
|---|
| 1791 |
} |
|---|
| 1792 |
else if (t1->isintegral() && t2->isintegral()) |
|---|
| 1793 |
{ |
|---|
| 1794 |
assert(0); |
|---|
| 1795 |
} |
|---|
| 1796 |
else if (e1->isArrayOperand() && t1->ty == Tarray && |
|---|
| 1797 |
e2->implicitConvTo(t1->nextOf())) |
|---|
| 1798 |
{ // T[] op T |
|---|
| 1799 |
e2 = e2->castTo(sc, t1->nextOf()); |
|---|
| 1800 |
t = t1->nextOf()->arrayOf(); |
|---|
| 1801 |
} |
|---|
| 1802 |
else if (e2->isArrayOperand() && t2->ty == Tarray && |
|---|
| 1803 |
e1->implicitConvTo(t2->nextOf())) |
|---|
| 1804 |
{ // T op T[] |
|---|
| 1805 |
e1 = e1->castTo(sc, t2->nextOf()); |
|---|
| 1806 |
t = t2->nextOf()->arrayOf(); |
|---|
| 1807 |
|
|---|
| 1808 |
//printf("test %s\n", e->toChars()); |
|---|
| 1809 |
e1 = e1->optimize(WANTvalue); |
|---|
| 1810 |
if (e && e->isCommutative() && e1->isConst()) |
|---|
| 1811 |
{ /* Swap operands to minimize number of functions generated |
|---|
| 1812 |
*/ |
|---|
| 1813 |
//printf("swap %s\n", e->toChars()); |
|---|
| 1814 |
Expression *tmp = e1; |
|---|
| 1815 |
e1 = e2; |
|---|
| 1816 |
e2 = tmp; |
|---|
| 1817 |
} |
|---|
| 1818 |
} |
|---|
| 1819 |
else |
|---|
| 1820 |
{ |
|---|
| 1821 |
Lincompatible: |
|---|
| 1822 |
return 0; |
|---|
| 1823 |
} |
|---|
| 1824 |
Lret: |
|---|
| 1825 |
if (!*pt) |
|---|
| 1826 |
*pt = t; |
|---|
| 1827 |
*pe1 = e1; |
|---|
| 1828 |
*pe2 = e2; |
|---|
| 1829 |
#if 0 |
|---|
| 1830 |
printf("-typeMerge() %s op %s\n", e1->toChars(), e2->toChars()); |
|---|
| 1831 |
if (e1->type) printf("\tt1 = %s\n", e1->type->toChars()); |
|---|
| 1832 |
if (e2->type) printf("\tt2 = %s\n", e2->type->toChars()); |
|---|
| 1833 |
printf("\ttype = %s\n", t->toChars()); |
|---|
| 1834 |
#endif |
|---|
| 1835 |
//dump(0); |
|---|
| 1836 |
return 1; |
|---|
| 1837 |
|
|---|
| 1838 |
|
|---|
| 1839 |
Lt1: |
|---|
| 1840 |
e2 = e2->castTo(sc, t1); |
|---|
| 1841 |
t = t1; |
|---|
| 1842 |
goto Lret; |
|---|
| 1843 |
|
|---|
| 1844 |
Lt2: |
|---|
| 1845 |
e1 = e1->castTo(sc, t2); |
|---|
| 1846 |
t = t2; |
|---|
| 1847 |
goto Lret; |
|---|
| 1848 |
} |
|---|
| 1849 |
|
|---|
| 1850 |
/************************************ |
|---|
| 1851 |
* Bring leaves to common type. |
|---|
| 1852 |
*/ |
|---|
| 1853 |
|
|---|
| 1854 |
Expression *BinExp::typeCombine(Scope *sc) |
|---|
| 1855 |
{ |
|---|
| 1856 |
Type *t1 = e1->type->toBasetype(); |
|---|
| 1857 |
Type *t2 = e2->type->toBasetype(); |
|---|
| 1858 |
|
|---|
| 1859 |
if (op == TOKmin || op == TOKadd) |
|---|
| 1860 |
{ |
|---|
| 1861 |
// struct+struct, where the structs are the same type, and class+class are errors |
|---|
| 1862 |
if (t1->ty == Tstruct) |
|---|
| 1863 |
{ |
|---|
| 1864 |
if (t2->ty == Tstruct && |
|---|
| 1865 |
((TypeStruct *)t1)->sym == ((TypeStruct *)t2)->sym) |
|---|
| 1866 |
goto Lerror; |
|---|
| 1867 |
} |
|---|
| 1868 |
else if (t1->ty == Tclass) |
|---|
| 1869 |
{ |
|---|
| 1870 |
if (t2->ty == Tclass) |
|---|
| 1871 |
goto Lerror; |
|---|
| 1872 |
} |
|---|
| 1873 |
} |
|---|
| 1874 |
|
|---|
| 1875 |
if (!typeMerge(sc, this, &type, &e1, &e2)) |
|---|
| 1876 |
goto Lerror; |
|---|
| 1877 |
return this; |
|---|
| 1878 |
|
|---|
| 1879 |
Lerror: |
|---|
| 1880 |
incompatibleTypes(); |
|---|
| 1881 |
type = Type::terror; |
|---|
| 1882 |
e1 = new ErrorExp(); |
|---|
| 1883 |
e2 = new ErrorExp(); |
|---|
| 1884 |
return new ErrorExp(); |
|---|
| 1885 |
} |
|---|
| 1886 |
|
|---|
| 1887 |
/*********************************** |
|---|
| 1888 |
* Do integral promotions (convertchk). |
|---|
| 1889 |
* Don't convert <array of> to <pointer to> |
|---|
| 1890 |
*/ |
|---|
| 1891 |
|
|---|
| 1892 |
Expression *Expression::integralPromotions(Scope *sc) |
|---|
| 1893 |
{ |
|---|
| 1894 |
Expression *e = this; |
|---|
| 1895 |
|
|---|
| 1896 |
//printf("integralPromotions %s %s\n", e->toChars(), e->type->toChars()); |
|---|
| 1897 |
switch (type->toBasetype()->ty) |
|---|
| 1898 |
{ |
|---|
| 1899 |
case Tvoid: |
|---|
| 1900 |
error("void has no value"); |
|---|
| 1901 |
return new ErrorExp(); |
|---|
| 1902 |
|
|---|
| 1903 |
case Tint8: |
|---|
| 1904 |
case Tuns8: |
|---|
| 1905 |
case Tint16: |
|---|
| 1906 |
case Tuns16: |
|---|
| 1907 |
case Tbit: |
|---|
| 1908 |
case Tbool: |
|---|
| 1909 |
case Tchar: |
|---|
| 1910 |
case Twchar: |
|---|
| 1911 |
e = e->castTo(sc, Type::tint32); |
|---|
| 1912 |
break; |
|---|
| 1913 |
|
|---|
| 1914 |
case Tdchar: |
|---|
| 1915 |
e = e->castTo(sc, Type::tuns32); |
|---|
| 1916 |
break; |
|---|
| 1917 |
} |
|---|
| 1918 |
return e; |
|---|
| 1919 |
} |
|---|
| 1920 |
|
|---|
| 1921 |
/*********************************** |
|---|
| 1922 |
* See if both types are arrays that can be compared |
|---|
| 1923 |
* for equality. Return !=0 if so. |
|---|
| 1924 |
* If they are arrays, but incompatible, issue error. |
|---|
| 1925 |
* This is to enable comparing things like an immutable |
|---|
| 1926 |
* array with a mutable one. |
|---|
| 1927 |
*/ |
|---|
| 1928 |
|
|---|
| 1929 |
int arrayTypeCompatible(Loc loc, Type *t1, Type *t2) |
|---|
| 1930 |
{ |
|---|
| 1931 |
t1 = t1->toBasetype(); |
|---|
| 1932 |
t2 = t2->toBasetype(); |
|---|
| 1933 |
|
|---|
| 1934 |
if ((t1->ty == Tarray || t1->ty == Tsarray || t1->ty == Tpointer) && |
|---|
| 1935 |
(t2->ty == Tarray || t2->ty == Tsarray || t2->ty == Tpointer)) |
|---|
| 1936 |
{ |
|---|
| 1937 |
if (t1->nextOf()->implicitConvTo(t2->nextOf()) < MATCHconst && |
|---|
| 1938 |
t2->nextOf()->implicitConvTo(t1->nextOf()) < MATCHconst && |
|---|
| 1939 |
(t1->nextOf()->ty != Tvoid && t2->nextOf()->ty != Tvoid)) |
|---|
| 1940 |
{ |
|---|
| 1941 |
error(loc, "array equality comparison type mismatch, %s vs %s", t1->toChars(), t2->toChars()); |
|---|
| 1942 |
} |
|---|
| 1943 |
return 1; |
|---|
| 1944 |
} |
|---|
| 1945 |
return 0; |
|---|
| 1946 |
} |
|---|
| 1947 |
|
|---|
| 1948 |
/*********************************** |
|---|
| 1949 |
* See if both types are arrays that can be compared |
|---|
| 1950 |
* for equality without any casting. Return !=0 if so. |
|---|
| 1951 |
* This is to enable comparing things like an immutable |
|---|
| 1952 |
* array with a mutable one. |
|---|
| 1953 |
*/ |
|---|
| 1954 |
int arrayTypeCompatibleWithoutCasting(Loc loc, Type *t1, Type *t2) |
|---|
| 1955 |
{ |
|---|
| 1956 |
t1 = t1->toBasetype(); |
|---|
| 1957 |
t2 = t2->toBasetype(); |
|---|
| 1958 |
|
|---|
| 1959 |
if ((t1->ty == Tarray || t1->ty == Tsarray || t1->ty == Tpointer) && |
|---|
| 1960 |
t2->ty == t1->ty) |
|---|
| 1961 |
{ |
|---|
| 1962 |
if (t1->nextOf()->implicitConvTo(t2->nextOf()) >= MATCHconst || |
|---|
| 1963 |
t2->nextOf()->implicitConvTo(t1->nextOf()) >= MATCHconst) |
|---|
| 1964 |
return 1; |
|---|
| 1965 |
} |
|---|
| 1966 |
return 0; |
|---|
| 1967 |
} |
|---|
| 1968 |
|
|---|
| 1969 |
|
|---|
| 1970 |
/******************************************************************/ |
|---|
| 1971 |
|
|---|
| 1972 |
/* Determine the integral ranges of an expression. |
|---|
| 1973 |
* This is used to determine if implicit narrowing conversions will |
|---|
| 1974 |
* be allowed. |
|---|
| 1975 |
*/ |
|---|
| 1976 |
|
|---|
| 1977 |
uinteger_t getMask(uinteger_t v) |
|---|
| 1978 |
{ |
|---|
| 1979 |
uinteger_t u = 0; |
|---|
| 1980 |
if (v >= 0x80) |
|---|
| 1981 |
u = 0xFF; |
|---|
| 1982 |
while (u < v) |
|---|
| 1983 |
u = (u << 1) | 1; |
|---|
| 1984 |
return u; |
|---|
| 1985 |
} |
|---|
| 1986 |
|
|---|
| 1987 |
IntRange Expression::getIntRange() |
|---|
| 1988 |
{ |
|---|
| 1989 |
IntRange ir; |
|---|
| 1990 |
ir.imin = 0; |
|---|
| 1991 |
if (type->isintegral()) |
|---|
| 1992 |
ir.imax = type->sizemask(); |
|---|
| 1993 |
else |
|---|
| 1994 |
ir.imax = 0xFFFFFFFFFFFFFFFFULL; // assume the worst |
|---|
| 1995 |
return ir; |
|---|
| 1996 |
} |
|---|
| 1997 |
|
|---|
| 1998 |
IntRange IntegerExp::getIntRange() |
|---|
| 1999 |
{ |
|---|
| 2000 |
IntRange ir; |
|---|
| 2001 |
ir.imin = value & type->sizemask(); |
|---|
| 2002 |
ir.imax = ir.imin; |
|---|
| 2003 |
return ir; |
|---|
| 2004 |
} |
|---|
| 2005 |
|
|---|
| 2006 |
IntRange CastExp::getIntRange() |
|---|
| 2007 |
{ |
|---|
| 2008 |
IntRange ir; |
|---|
| 2009 |
ir = e1->getIntRange(); |
|---|
| 2010 |
// Do sign extension |
|---|
| 2011 |
switch (e1->type->toBasetype()->ty) |
|---|
| 2012 |
{ |
|---|
| 2013 |
case Tint8: |
|---|
| 2014 |
if (ir.imax & 0x80) |
|---|
| 2015 |
ir.imax |= 0xFFFFFFFFFFFFFF00ULL; |
|---|
| 2016 |
break; |
|---|
| 2017 |
case Tint16: |
|---|
| 2018 |
if (ir.imax & 0x8000) |
|---|
| 2019 |
ir.imax |= 0xFFFFFFFFFFFF0000ULL; |
|---|
| 2020 |
break; |
|---|
| 2021 |
case Tint32: |
|---|
| 2022 |
if (ir.imax & 0x80000000) |
|---|
| 2023 |
ir.imax |= 0xFFFFFFFF00000000ULL; |
|---|
| 2024 |
break; |
|---|
| 2025 |
} |
|---|
| 2026 |
if (type->isintegral()) |
|---|
| 2027 |
{ |
|---|
| 2028 |
ir.imin &= type->sizemask(); |
|---|
| 2029 |
ir.imax &= type->sizemask(); |
|---|
| 2030 |
} |
|---|
| 2031 |
//printf("CastExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax); |
|---|
| 2032 |
return ir; |
|---|
| 2033 |
} |
|---|
| 2034 |
|
|---|
| 2035 |
IntRange DivExp::getIntRange() |
|---|
| 2036 |
{ |
|---|
| 2037 |
IntRange ir; |
|---|
| 2038 |
IntRange ir1 = e1->getIntRange(); |
|---|
| 2039 |
IntRange ir2 = e2->getIntRange(); |
|---|
| 2040 |
|
|---|
| 2041 |
if (!(e1->type->isunsigned() || ir1.imax < 0x8000000000000000ULL) && |
|---|
| 2042 |
!(e2->type->isunsigned() || ir2.imax < 0x8000000000000000ULL)) |
|---|
| 2043 |
{ |
|---|
| 2044 |
return Expression::getIntRange(); |
|---|
| 2045 |
} |
|---|
| 2046 |
|
|---|
| 2047 |
if (ir2.imax == 0 || ir2.imin == 0) |
|---|
| 2048 |
return Expression::getIntRange(); |
|---|
| 2049 |
|
|---|
| 2050 |
ir.imin = ir1.imin / ir2.imax; |
|---|
| 2051 |
ir.imax = ir1.imax / ir2.imin; |
|---|
| 2052 |
|
|---|
| 2053 |
ir.imin &= type->sizemask(); |
|---|
| 2054 |
ir.imax &= type->sizemask(); |
|---|
| 2055 |
|
|---|
| 2056 |
//printf("DivExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax); |
|---|
| 2057 |
//e1->dump(0); |
|---|
| 2058 |
|
|---|
| 2059 |
return ir; |
|---|
| 2060 |
} |
|---|
| 2061 |
|
|---|
| 2062 |
IntRange AndExp::getIntRange() |
|---|
| 2063 |
{ |
|---|
| 2064 |
IntRange ir; |
|---|
| 2065 |
IntRange ir1 = e1->getIntRange(); |
|---|
| 2066 |
IntRange ir2 = e2->getIntRange(); |
|---|
| 2067 |
|
|---|
| 2068 |
ir.imin = ir1.imin; |
|---|
| 2069 |
if (ir2.imin < ir.imin) |
|---|
| 2070 |
ir.imin = ir2.imin; |
|---|
| 2071 |
|
|---|
| 2072 |
ir.imax = ir1.imax; |
|---|
| 2073 |
if (ir2.imax > ir.imax) |
|---|
| 2074 |
ir.imax = ir2.imax; |
|---|
| 2075 |
|
|---|
| 2076 |
uinteger_t u; |
|---|
| 2077 |
|
|---|
| 2078 |
u = getMask(ir1.imax); |
|---|
| 2079 |
ir.imin &= u; |
|---|
| 2080 |
ir.imax &= u; |
|---|
| 2081 |
|
|---|
| 2082 |
u = getMask(ir2.imax); |
|---|
| 2083 |
ir.imin &= u; |
|---|
| 2084 |
ir.imax &= u; |
|---|
| 2085 |
|
|---|
| 2086 |
ir.imin &= type->sizemask(); |
|---|
| 2087 |
ir.imax &= type->sizemask(); |
|---|
| 2088 |
|
|---|
| 2089 |
//printf("AndExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax); |
|---|
| 2090 |
//e1->dump(0); |
|---|
| 2091 |
|
|---|
| 2092 |
return ir; |
|---|
| 2093 |
} |
|---|
| 2094 |
|
|---|
| 2095 |
IntRange OrExp::getIntRange() |
|---|
| 2096 |
{ |
|---|
| 2097 |
IntRange ir; |
|---|
| 2098 |
IntRange ir1 = e1->getIntRange(); |
|---|
| 2099 |
IntRange ir2 = e2->getIntRange(); |
|---|
| 2100 |
|
|---|
| 2101 |
ir.imin = ir1.imin; |
|---|
| 2102 |
if (ir2.imin < ir.imin) |
|---|
| 2103 |
ir.imin = ir2.imin; |
|---|
| 2104 |
|
|---|
| 2105 |
ir.imax = ir1.imax; |
|---|
| 2106 |
if (ir2.imax > ir.imax) |
|---|
| 2107 |
ir.imax = ir2.imax; |
|---|
| 2108 |
|
|---|
| 2109 |
ir.imin &= type->sizemask(); |
|---|
| 2110 |
ir.imax &= type->sizemask(); |
|---|
| 2111 |
|
|---|
| 2112 |
//printf("OrExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax); |
|---|
| 2113 |
//e1->dump(0); |
|---|
| 2114 |
|
|---|
| 2115 |
return ir; |
|---|
| 2116 |
} |
|---|
| 2117 |
|
|---|
| 2118 |
IntRange XorExp::getIntRange() |
|---|
| 2119 |
{ |
|---|
| 2120 |
IntRange ir; |
|---|
| 2121 |
IntRange ir1 = e1->getIntRange(); |
|---|
| 2122 |
IntRange ir2 = e2->getIntRange(); |
|---|
| 2123 |
|
|---|
| 2124 |
ir.imin = ir1.imin; |
|---|
| 2125 |
if (ir2.imin < ir.imin) |
|---|
| 2126 |
ir.imin = ir2.imin; |
|---|
| 2127 |
|
|---|
| 2128 |
ir.imax = ir1.imax; |
|---|
| 2129 |
if (ir2.imax > ir.imax) |
|---|
| 2130 |
ir.imax = ir2.imax; |
|---|
| 2131 |
|
|---|
| 2132 |
ir.imin &= type->sizemask(); |
|---|
| 2133 |
ir.imax &= type->sizemask(); |
|---|
| 2134 |
|
|---|
| 2135 |
//printf("XorExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax); |
|---|
| 2136 |
//e1->dump(0); |
|---|
| 2137 |
|
|---|
| 2138 |
return ir; |
|---|
| 2139 |
} |
|---|
| 2140 |
|
|---|
| 2141 |
IntRange ShlExp::getIntRange() |
|---|
| 2142 |
{ |
|---|
| 2143 |
IntRange ir; |
|---|
| 2144 |
IntRange ir1 = e1->getIntRange(); |
|---|
| 2145 |
IntRange ir2 = e2->getIntRange(); |
|---|
| 2146 |
|
|---|
| 2147 |
ir.imin = getMask(ir1.imin) << ir2.imin; |
|---|
| 2148 |
ir.imax = getMask(ir1.imax) << ir2.imax; |
|---|
| 2149 |
|
|---|
| 2150 |
ir.imin &= type->sizemask(); |
|---|
| 2151 |
ir.imax &= type->sizemask(); |
|---|
| 2152 |
|
|---|
| 2153 |
//printf("ShlExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax); |
|---|
| 2154 |
//e1->dump(0); |
|---|
| 2155 |
|
|---|
| 2156 |
return ir; |
|---|
| 2157 |
} |
|---|
| 2158 |
|
|---|
| 2159 |
IntRange ShrExp::getIntRange() |
|---|
| 2160 |
{ |
|---|
| 2161 |
if (!e1->type->isunsigned()) |
|---|
| 2162 |
return Expression::getIntRange(); |
|---|
| 2163 |
|
|---|
| 2164 |
IntRange ir; |
|---|
| 2165 |
IntRange ir1 = e1->getIntRange(); |
|---|
| 2166 |
IntRange ir2 = e2->getIntRange(); |
|---|
| 2167 |
|
|---|
| 2168 |
ir.imin = ir1.imin >> ir2.imax; |
|---|
| 2169 |
ir.imax = ir1.imax >> ir2.imin; |
|---|
| 2170 |
|
|---|
| 2171 |
ir.imin &= type->sizemask(); |
|---|
| 2172 |
ir.imax &= type->sizemask(); |
|---|
| 2173 |
|
|---|
| 2174 |
//printf("ShrExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax); |
|---|
| 2175 |
//e1->dump(0); |
|---|
| 2176 |
|
|---|
| 2177 |
return ir; |
|---|
| 2178 |
} |
|---|
| 2179 |
|
|---|
| 2180 |
IntRange UshrExp::getIntRange() |
|---|
| 2181 |
{ |
|---|
| 2182 |
IntRange ir; |
|---|
| 2183 |
IntRange ir1 = e1->getIntRange(); |
|---|
| 2184 |
IntRange ir2 = e2->getIntRange(); |
|---|
| 2185 |
|
|---|
| 2186 |
ir.imin = ir1.imin >> ir2.imax; |
|---|
| 2187 |
ir.imax = ir1.imax >> ir2.imin; |
|---|
| 2188 |
|
|---|
| 2189 |
ir.imin &= type->sizemask(); |
|---|
| 2190 |
ir.imax &= type->sizemask(); |
|---|
| 2191 |
|
|---|
| 2192 |
//printf("UshrExp: imin = x%llx, imax = x%llx\n", ir.imin, ir.imax); |
|---|
| 2193 |
//e1->dump(0); |
|---|
| 2194 |
|
|---|
| 2195 |
return ir; |
|---|
| 2196 |
} |
|---|
| 2197 |
|
|---|
| 2198 |
IntRange CommaExp::getIntRange() |
|---|
| 2199 |
{ |
|---|
| 2200 |
return e2->getIntRange(); |
|---|
| 2201 |
} |
|---|