Mercurial > projects > ldc
comparison dmd/cast.c @ 510:6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Thu, 14 Aug 2008 06:55:41 +0200 |
parents | aaade6ded589 |
children | eef8ac26c66c |
comparison
equal
deleted
inserted
replaced
509:337554fd34f1 | 510:6aee82889553 |
---|---|
44 return e->implicitCastTo(sc, t); | 44 return e->implicitCastTo(sc, t); |
45 | 45 |
46 warning("%s: implicit conversion of expression (%s) of type %s to %s can cause loss of data", | 46 warning("%s: implicit conversion of expression (%s) of type %s to %s can cause loss of data", |
47 loc.toChars(), toChars(), type->toChars(), t->toChars()); | 47 loc.toChars(), toChars(), type->toChars(), t->toChars()); |
48 } | 48 } |
49 #if DMDV2 | |
50 if (match == MATCHconst && t == type->constOf()) | |
51 { | |
52 Expression *e = copy(); | |
53 e->type = t; | |
54 return e; | |
55 } | |
56 #endif | |
49 return castTo(sc, t); | 57 return castTo(sc, t); |
50 } | 58 } |
51 | 59 |
52 Expression *e = optimize(WANTflags | WANTvalue); | 60 Expression *e = optimize(WANTflags | WANTvalue); |
53 if (e != this) | 61 if (e != this) |
54 return e->implicitCastTo(sc, t); | 62 return e->implicitCastTo(sc, t); |
55 | 63 |
56 #if 0 | 64 #if 0 |
65 printf("ty = %d\n", type->ty); | |
57 print(); | 66 print(); |
58 type->print(); | 67 type->print(); |
59 printf("to:\n"); | 68 printf("to:\n"); |
60 t->print(); | 69 t->print(); |
61 printf("%p %p type: %s to: %s\n", type->deco, t->deco, type->deco, t->deco); | 70 printf("%p %p type: %s to: %s\n", type->deco, t->deco, type->deco, t->deco); |
62 //printf("%p %p %p\n", type->next->arrayOf(), type, t); | 71 //printf("%p %p %p\n", type->nextOf()->arrayOf(), type, t); |
63 fflush(stdout); | 72 fflush(stdout); |
64 #endif | 73 #endif |
65 if (!t->deco) | 74 if (!t->deco) |
66 { /* Can happen with: | 75 { /* Can happen with: |
67 * enum E { One } | 76 * enum E { One } |
90 { | 99 { |
91 #if 0 | 100 #if 0 |
92 printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n", | 101 printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n", |
93 toChars(), type->toChars(), t->toChars()); | 102 toChars(), type->toChars(), t->toChars()); |
94 #endif | 103 #endif |
104 //static int nest; if (++nest == 10) halt(); | |
95 if (!type) | 105 if (!type) |
96 { error("%s is not an expression", toChars()); | 106 { error("%s is not an expression", toChars()); |
97 type = Type::terror; | 107 type = Type::terror; |
98 } | 108 } |
99 if (t->ty == Tbit && isBit()) | 109 if (t->ty == Tbit && isBit()) |
108 return match; | 118 return match; |
109 #if 0 | 119 #if 0 |
110 Type *tb = t->toBasetype(); | 120 Type *tb = t->toBasetype(); |
111 if (tb->ty == Tdelegate) | 121 if (tb->ty == Tdelegate) |
112 { TypeDelegate *td = (TypeDelegate *)tb; | 122 { TypeDelegate *td = (TypeDelegate *)tb; |
113 TypeFunction *tf = (TypeFunction *)td->next; | 123 TypeFunction *tf = (TypeFunction *)td->nextOf(); |
114 | 124 |
115 if (!tf->varargs && | 125 if (!tf->varargs && |
116 !(tf->arguments && tf->arguments->dim) | 126 !(tf->arguments && tf->arguments->dim) |
117 ) | 127 ) |
118 { | 128 { |
119 match = type->implicitConvTo(tf->next); | 129 match = type->implicitConvTo(tf->nextOf()); |
120 if (match) | 130 if (match) |
121 return match; | 131 return match; |
122 if (tf->next->toBasetype()->ty == Tvoid) | 132 if (tf->nextOf()->toBasetype()->ty == Tvoid) |
123 return MATCHconvert; | 133 return MATCHconvert; |
124 } | 134 } |
125 } | 135 } |
126 #endif | 136 #endif |
127 return MATCHnomatch; | 137 return MATCHnomatch; |
329 t->ty == Tdelegate) | 339 t->ty == Tdelegate) |
330 return committed ? MATCHconvert : MATCHexact; | 340 return committed ? MATCHconvert : MATCHexact; |
331 } | 341 } |
332 return Expression::implicitConvTo(t); | 342 return Expression::implicitConvTo(t); |
333 } | 343 } |
344 | |
345 #if DMDV2 | |
346 MATCH StructLiteralExp::implicitConvTo(Type *t) | |
347 { | |
348 #if 0 | |
349 printf("StructLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n", | |
350 toChars(), type->toChars(), t->toChars()); | |
351 #endif | |
352 MATCH m = Expression::implicitConvTo(t); | |
353 if (m != MATCHnomatch) | |
354 return m; | |
355 if (type->ty == t->ty && type->ty == Tstruct && | |
356 ((TypeStruct *)type)->sym == ((TypeStruct *)t)->sym) | |
357 { | |
358 m = MATCHconst; | |
359 for (int i = 0; i < elements->dim; i++) | |
360 { Expression *e = (Expression *)elements->data[i]; | |
361 Type *te = e->type; | |
362 if (t->mod == 0) | |
363 te = te->mutableOf(); | |
364 else | |
365 { assert(t->mod == MODinvariant); | |
366 te = te->invariantOf(); | |
367 } | |
368 MATCH m2 = e->implicitConvTo(te); | |
369 //printf("\t%s => %s, match = %d\n", e->toChars(), te->toChars(), m2); | |
370 if (m2 < m) | |
371 m = m2; | |
372 } | |
373 } | |
374 return m; | |
375 } | |
376 #endif | |
334 | 377 |
335 MATCH StringExp::implicitConvTo(Type *t) | 378 MATCH StringExp::implicitConvTo(Type *t) |
336 { MATCH m; | 379 { MATCH m; |
337 | 380 |
338 #if 0 | 381 #if 0 |
385 } | 428 } |
386 | 429 |
387 MATCH ArrayLiteralExp::implicitConvTo(Type *t) | 430 MATCH ArrayLiteralExp::implicitConvTo(Type *t) |
388 { MATCH result = MATCHexact; | 431 { MATCH result = MATCHexact; |
389 | 432 |
433 #if 0 | |
434 printf("ArrayLiteralExp::implicitConvTo(this=%s, type=%s, t=%s)\n", | |
435 toChars(), type->toChars(), t->toChars()); | |
436 #endif | |
390 Type *typeb = type->toBasetype(); | 437 Type *typeb = type->toBasetype(); |
391 Type *tb = t->toBasetype(); | 438 Type *tb = t->toBasetype(); |
392 if ((tb->ty == Tarray || tb->ty == Tsarray) && | 439 if ((tb->ty == Tarray || tb->ty == Tsarray) && |
393 (typeb->ty == Tarray || typeb->ty == Tsarray)) | 440 (typeb->ty == Tarray || typeb->ty == Tsarray)) |
394 { | 441 { |
398 result = MATCHnomatch; | 445 result = MATCHnomatch; |
399 } | 446 } |
400 | 447 |
401 for (int i = 0; i < elements->dim; i++) | 448 for (int i = 0; i < elements->dim; i++) |
402 { Expression *e = (Expression *)elements->data[i]; | 449 { Expression *e = (Expression *)elements->data[i]; |
403 MATCH m = (MATCH)e->implicitConvTo(tb->next); | 450 MATCH m = (MATCH)e->implicitConvTo(tb->nextOf()); |
404 if (m < result) | 451 if (m < result) |
405 result = m; // remember worst match | 452 result = m; // remember worst match |
406 if (result == MATCHnomatch) | 453 if (result == MATCHnomatch) |
407 break; // no need to check for worse | 454 break; // no need to check for worse |
408 } | 455 } |
425 if (m < result) | 472 if (m < result) |
426 result = m; // remember worst match | 473 result = m; // remember worst match |
427 if (result == MATCHnomatch) | 474 if (result == MATCHnomatch) |
428 break; // no need to check for worse | 475 break; // no need to check for worse |
429 e = (Expression *)values->data[i]; | 476 e = (Expression *)values->data[i]; |
430 m = (MATCH)e->implicitConvTo(tb->next); | 477 m = (MATCH)e->implicitConvTo(tb->nextOf()); |
431 if (m < result) | 478 if (m < result) |
432 result = m; // remember worst match | 479 result = m; // remember worst match |
433 if (result == MATCHnomatch) | 480 if (result == MATCHnomatch) |
434 break; // no need to check for worse | 481 break; // no need to check for worse |
435 } | 482 } |
514 { | 561 { |
515 // Look for pointers to functions where the functions are overloaded. | 562 // Look for pointers to functions where the functions are overloaded. |
516 FuncDeclaration *f; | 563 FuncDeclaration *f; |
517 | 564 |
518 t = t->toBasetype(); | 565 t = t->toBasetype(); |
519 if (type->ty == Tdelegate && type->next->ty == Tfunction && | 566 if (type->ty == Tdelegate && type->nextOf()->ty == Tfunction && |
520 t->ty == Tdelegate && t->next->ty == Tfunction) | 567 t->ty == Tdelegate && t->nextOf()->ty == Tfunction) |
521 { | 568 { |
522 if (func && func->overloadExactMatch(t->next)) | 569 if (func && func->overloadExactMatch(t->nextOf())) |
523 result = MATCHexact; | 570 result = MATCHexact; |
524 } | 571 } |
525 } | 572 } |
526 return result; | 573 return result; |
527 } | 574 } |
946 | 993 |
947 | 994 |
948 Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t) | 995 Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t) |
949 { | 996 { |
950 #if 0 | 997 #if 0 |
951 printf("ArrayLiteralExp::castTo(this=%s, type=%s, t=%s)\n", | 998 printf("ArrayLiteralExp::castTo(this=%s, type=%s, => %s)\n", |
952 toChars(), type->toChars(), t->toChars()); | 999 toChars(), type->toChars(), t->toChars()); |
953 #endif | 1000 #endif |
954 if (type == t) | 1001 if (type == t) |
955 return this; | 1002 return this; |
956 ArrayLiteralExp *e = this; | 1003 ArrayLiteralExp *e = this; |
1013 return e; | 1060 return e; |
1014 } | 1061 } |
1015 L1: | 1062 L1: |
1016 return e->Expression::castTo(sc, t); | 1063 return e->Expression::castTo(sc, t); |
1017 } | 1064 } |
1018 | |
1019 | 1065 |
1020 Expression *SymOffExp::castTo(Scope *sc, Type *t) | 1066 Expression *SymOffExp::castTo(Scope *sc, Type *t) |
1021 { | 1067 { |
1022 Type *tb; | 1068 Type *tb; |
1023 | 1069 |
1132 if (t1b->ty == Tpointer && t2b->isintegral()) | 1178 if (t1b->ty == Tpointer && t2b->isintegral()) |
1133 { // Need to adjust operator by the stride | 1179 { // Need to adjust operator by the stride |
1134 // Replace (ptr + int) with (ptr + (int * stride)) | 1180 // Replace (ptr + int) with (ptr + (int * stride)) |
1135 Type *t = Type::tptrdiff_t; | 1181 Type *t = Type::tptrdiff_t; |
1136 | 1182 |
1137 stride = t1b->next->size(); | 1183 stride = t1b->nextOf()->size(loc); |
1138 if (!t->equals(t2b)) | 1184 if (!t->equals(t2b)) |
1139 e2 = e2->castTo(sc, t); | 1185 e2 = e2->castTo(sc, t); |
1140 // LLVMDC: llvm uses typesafe pointer arithmetic | 1186 // LLVMDC: llvm uses typesafe pointer arithmetic |
1141 #if !IN_LLVM | 1187 #if !IN_LLVM |
1142 if (t1b->next->isbit()) | 1188 if (t1b->next->isbit()) |
1154 { // Need to adjust operator by the stride | 1200 { // Need to adjust operator by the stride |
1155 // Replace (int + ptr) with (ptr + (int * stride)) | 1201 // Replace (int + ptr) with (ptr + (int * stride)) |
1156 Type *t = Type::tptrdiff_t; | 1202 Type *t = Type::tptrdiff_t; |
1157 Expression *e; | 1203 Expression *e; |
1158 | 1204 |
1159 stride = t2b->next->size(); | 1205 stride = t2b->nextOf()->size(loc); |
1160 if (!t->equals(t1b)) | 1206 if (!t->equals(t1b)) |
1161 e = e1->castTo(sc, t); | 1207 e = e1->castTo(sc, t); |
1162 else | 1208 else |
1163 e = e1; | 1209 e = e1; |
1164 #if !IN_LLVM | 1210 #if !IN_LLVM |
1325 } | 1371 } |
1326 else | 1372 else |
1327 goto Lincompatible; | 1373 goto Lincompatible; |
1328 } | 1374 } |
1329 else if ((t1->ty == Tsarray || t1->ty == Tarray) && | 1375 else if ((t1->ty == Tsarray || t1->ty == Tarray) && |
1330 e2->op == TOKnull && t2->ty == Tpointer && t2->next->ty == Tvoid) | 1376 e2->op == TOKnull && t2->ty == Tpointer && t2->nextOf()->ty == Tvoid) |
1331 { | 1377 { /* (T[n] op void*) |
1378 * (T[] op void*) | |
1379 */ | |
1332 goto Lx1; | 1380 goto Lx1; |
1333 } | 1381 } |
1334 else if ((t2->ty == Tsarray || t2->ty == Tarray) && | 1382 else if ((t2->ty == Tsarray || t2->ty == Tarray) && |
1335 e1->op == TOKnull && t1->ty == Tpointer && t1->next->ty == Tvoid) | 1383 e1->op == TOKnull && t1->ty == Tpointer && t1->nextOf()->ty == Tvoid) |
1336 { | 1384 { /* (void* op T[n]) |
1385 * (void* op T[]) | |
1386 */ | |
1337 goto Lx2; | 1387 goto Lx2; |
1338 } | 1388 } |
1339 else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2)) | 1389 else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2)) |
1340 { | 1390 { |
1341 goto Lt2; | 1391 goto Lt2; |
1373 TypeClass *tc2 = (TypeClass *)t2; | 1423 TypeClass *tc2 = (TypeClass *)t2; |
1374 | 1424 |
1375 /* Pick 'tightest' type | 1425 /* Pick 'tightest' type |
1376 */ | 1426 */ |
1377 ClassDeclaration *cd1 = tc1->sym->baseClass; | 1427 ClassDeclaration *cd1 = tc1->sym->baseClass; |
1378 ClassDeclaration *cd2 = tc1->sym->baseClass; | 1428 ClassDeclaration *cd2 = tc2->sym->baseClass; |
1379 | 1429 |
1380 if (cd1 && cd2) | 1430 if (cd1 && cd2) |
1381 { t1 = cd1->type; | 1431 { t1 = cd1->type; |
1382 t2 = cd2->type; | 1432 t2 = cd2->type; |
1383 } | 1433 } |
1400 else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1)) | 1450 else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1)) |
1401 { | 1451 { |
1402 goto Lt1; | 1452 goto Lt1; |
1403 } | 1453 } |
1404 else if (t1->ty == Tsarray && t2->ty == Tsarray && | 1454 else if (t1->ty == Tsarray && t2->ty == Tsarray && |
1405 e2->implicitConvTo(t1->next->arrayOf())) | 1455 e2->implicitConvTo(t1->nextOf()->arrayOf())) |
1406 { | 1456 { |
1407 Lx1: | 1457 Lx1: |
1408 t = t1->next->arrayOf(); | 1458 t = t1->nextOf()->arrayOf(); |
1409 e1 = e1->castTo(sc, t); | 1459 e1 = e1->castTo(sc, t); |
1410 e2 = e2->castTo(sc, t); | 1460 e2 = e2->castTo(sc, t); |
1411 } | 1461 } |
1412 else if (t1->ty == Tsarray && t2->ty == Tsarray && | 1462 else if (t1->ty == Tsarray && t2->ty == Tsarray && |
1413 e1->implicitConvTo(t2->next->arrayOf())) | 1463 e1->implicitConvTo(t2->nextOf()->arrayOf())) |
1414 { | 1464 { |
1415 Lx2: | 1465 Lx2: |
1416 t = t2->next->arrayOf(); | 1466 t = t2->nextOf()->arrayOf(); |
1417 e1 = e1->castTo(sc, t); | 1467 e1 = e1->castTo(sc, t); |
1418 e2 = e2->castTo(sc, t); | 1468 e2 = e2->castTo(sc, t); |
1469 } | |
1470 else if (t1->isintegral() && t2->isintegral()) | |
1471 { | |
1472 assert(0); | |
1473 } | |
1474 else if (e1->op == TOKslice && t1->ty == Tarray && | |
1475 e2->implicitConvTo(t1->nextOf())) | |
1476 { // T[] op T | |
1477 e2 = e2->castTo(sc, t1->nextOf()); | |
1478 t = t1->nextOf()->arrayOf(); | |
1479 } | |
1480 else if (e2->op == TOKslice && t2->ty == Tarray && | |
1481 e1->implicitConvTo(t2->nextOf())) | |
1482 { // T op T[] | |
1483 e1 = e1->castTo(sc, t2->nextOf()); | |
1484 t = t2->nextOf()->arrayOf(); | |
1485 | |
1486 //printf("test %s\n", e->toChars()); | |
1487 e1 = e1->optimize(WANTvalue); | |
1488 if (isCommutative() && e1->isConst()) | |
1489 { /* Swap operands to minimize number of functions generated | |
1490 */ | |
1491 //printf("swap %s\n", e->toChars()); | |
1492 Expression *tmp = e1; | |
1493 e1 = e2; | |
1494 e2 = tmp; | |
1495 } | |
1419 } | 1496 } |
1420 else | 1497 else |
1421 { | 1498 { |
1422 Lincompatible: | 1499 Lincompatible: |
1423 incompatibleTypes(); | 1500 incompatibleTypes(); |
1444 * Do integral promotions (convertchk). | 1521 * Do integral promotions (convertchk). |
1445 * Don't convert <array of> to <pointer to> | 1522 * Don't convert <array of> to <pointer to> |
1446 */ | 1523 */ |
1447 | 1524 |
1448 Expression *Expression::integralPromotions(Scope *sc) | 1525 Expression *Expression::integralPromotions(Scope *sc) |
1449 { Expression *e; | 1526 { |
1450 | 1527 Expression *e = this; |
1451 e = this; | 1528 |
1529 //printf("integralPromotions %s %s\n", e->toChars(), e->type->toChars()); | |
1452 switch (type->toBasetype()->ty) | 1530 switch (type->toBasetype()->ty) |
1453 { | 1531 { |
1454 case Tvoid: | 1532 case Tvoid: |
1455 error("void has no value"); | 1533 error("void has no value"); |
1456 break; | 1534 break; |