comparison dmd/constfold.c @ 19:788401029ecf trunk

[svn r23] * Updated to DMD 1.021
author lindquist
date Thu, 04 Oct 2007 03:42:56 +0200
parents c53b6e3fe49a
children 3cfcb944304e
comparison
equal deleted inserted replaced
18:c05ef76f1c20 19:788401029ecf
666 666
667 //printf("Equal(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); 667 //printf("Equal(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
668 668
669 assert(op == TOKequal || op == TOKnotequal); 669 assert(op == TOKequal || op == TOKnotequal);
670 670
671 if (e1->op == TOKstring && e2->op == TOKstring) 671 if (e1->op == TOKnull)
672 {
673 if (e2->op == TOKnull)
674 cmp = 1;
675 else if (e2->op == TOKstring)
676 { StringExp *es2 = (StringExp *)e2;
677 cmp = (0 == es2->len);
678 }
679 else if (e2->op == TOKarrayliteral)
680 { ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
681 cmp = !es2->elements || (0 == es2->elements->dim);
682 }
683 else
684 return EXP_CANT_INTERPRET;
685 }
686 else if (e2->op == TOKnull)
687 {
688 if (e1->op == TOKstring)
689 { StringExp *es1 = (StringExp *)e1;
690 cmp = (0 == es1->len);
691 }
692 else if (e1->op == TOKarrayliteral)
693 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
694 cmp = !es1->elements || (0 == es1->elements->dim);
695 }
696 else
697 return EXP_CANT_INTERPRET;
698 }
699 else if (e1->op == TOKstring && e2->op == TOKstring)
672 { StringExp *es1 = (StringExp *)e1; 700 { StringExp *es1 = (StringExp *)e1;
673 StringExp *es2 = (StringExp *)e2; 701 StringExp *es2 = (StringExp *)e2;
674 702
675 assert(es1->sz == es2->sz); 703 assert(es1->sz == es2->sz);
676 if (es1->len == es2->len && 704 if (es1->len == es2->len &&
790 Expression *Identity(enum TOK op, Type *type, Expression *e1, Expression *e2) 818 Expression *Identity(enum TOK op, Type *type, Expression *e1, Expression *e2)
791 { Expression *e; 819 { Expression *e;
792 Loc loc = e1->loc; 820 Loc loc = e1->loc;
793 int cmp; 821 int cmp;
794 822
795 if (e1->op == TOKsymoff && e2->op == TOKsymoff) 823 if (e1->op == TOKnull && e2->op == TOKnull)
824 {
825 cmp = 1;
826 }
827 else if (e1->op == TOKsymoff && e2->op == TOKsymoff)
796 { 828 {
797 SymOffExp *es1 = (SymOffExp *)e1; 829 SymOffExp *es1 = (SymOffExp *)e1;
798 SymOffExp *es2 = (SymOffExp *)e2; 830 SymOffExp *es2 = (SymOffExp *)e2;
799 831
800 cmp = (es1->var == es2->var && es1->offset == es2->offset); 832 cmp = (es1->var == es2->var && es1->offset == es2->offset);
1049 e = new StructLiteralExp(loc, sd, elements); 1081 e = new StructLiteralExp(loc, sd, elements);
1050 e->type = type; 1082 e->type = type;
1051 } 1083 }
1052 else 1084 else
1053 { 1085 {
1054 error("cannot cast %s to %s", e1->type->toChars(), type->toChars()); 1086 error(loc, "cannot cast %s to %s", e1->type->toChars(), type->toChars());
1055 e = new IntegerExp(loc, 0, type); 1087 e = new IntegerExp(loc, 0, type);
1056 } 1088 }
1057 return e; 1089 return e;
1058 } 1090 }
1059 1091
1243 Loc loc = e1->loc; 1275 Loc loc = e1->loc;
1244 Type *t; 1276 Type *t;
1245 1277
1246 //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); 1278 //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
1247 1279
1248 if (e1->op == TOKnull && e2->op == TOKint64) 1280 if (e1->op == TOKnull && (e2->op == TOKint64 || e2->op == TOKstructliteral))
1249 { e = e2; 1281 { e = e2;
1250 goto L2; 1282 goto L2;
1251 } 1283 }
1252 else if (e1->op == TOKint64 && e2->op == TOKnull) 1284 else if ((e1->op == TOKint64 || e1->op == TOKstructliteral) && e2->op == TOKnull)
1253 { e = e1; 1285 { e = e1;
1254 L2: 1286 L2:
1255 Type *tn = e->type->toBasetype(); 1287 Type *tn = e->type->toBasetype();
1256 if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar) 1288 if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar)
1257 { 1289 {
1379 } 1411 }
1380 else 1412 else
1381 e->type = type; 1413 e->type = type;
1382 } 1414 }
1383 else if (e1->op == TOKarrayliteral && 1415 else if (e1->op == TOKarrayliteral &&
1384 e1->type->toBasetype()->next->equals(e2->type)) 1416 e1->type->toBasetype()->nextOf()->equals(e2->type))
1385 { 1417 {
1386 ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; 1418 ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
1387 1419
1388 es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy()); 1420 es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy());
1389 es1->elements->push(e2); 1421 es1->elements->push(e2);
1396 } 1428 }
1397 else 1429 else
1398 e->type = type; 1430 e->type = type;
1399 } 1431 }
1400 else if (e2->op == TOKarrayliteral && 1432 else if (e2->op == TOKarrayliteral &&
1401 e2->type->toBasetype()->next->equals(e1->type)) 1433 e2->type->toBasetype()->nextOf()->equals(e1->type))
1402 { 1434 {
1403 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; 1435 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
1404 1436
1405 es2 = new ArrayLiteralExp(es2->loc, (Expressions *)es2->elements->copy()); 1437 es2 = new ArrayLiteralExp(es2->loc, (Expressions *)es2->elements->copy());
1406 es2->elements->shift(e1); 1438 es2->elements->shift(e1);
1423 else if (e1->op == TOKstring && e2->op == TOKnull) 1455 else if (e1->op == TOKstring && e2->op == TOKnull)
1424 { e = e1; 1456 { e = e1;
1425 t = e2->type; 1457 t = e2->type;
1426 L1: 1458 L1:
1427 Type *tb = t->toBasetype(); 1459 Type *tb = t->toBasetype();
1428 if (tb->ty == Tarray && tb->next->equals(e->type)) 1460 if (tb->ty == Tarray && tb->nextOf()->equals(e->type))
1429 { Expressions *expressions = new Expressions(); 1461 { Expressions *expressions = new Expressions();
1430 expressions->push(e); 1462 expressions->push(e);
1431 e = new ArrayLiteralExp(loc, expressions); 1463 e = new ArrayLiteralExp(loc, expressions);
1432 e->type = t; 1464 e->type = t;
1433 } 1465 }