comparison dmd/constfold.c @ 1333:6bae1c30480f

Backport D2 CTFE cast behavior to allow cast(char[])char[n] to succeed.
author Christian Kamm <kamm incasoftware de>
date Sun, 10 May 2009 15:42:23 +0200
parents e961851fb8be
children 8026319762be
comparison
equal deleted inserted replaced
1332:de7fad0ad243 1333:6bae1c30480f
1047 //printf("Cast(type = %s, to = %s, e1 = %s)\n", type->toChars(), to->toChars(), e1->toChars()); 1047 //printf("Cast(type = %s, to = %s, e1 = %s)\n", type->toChars(), to->toChars(), e1->toChars());
1048 //printf("e1->type = %s\n", e1->type->toChars()); 1048 //printf("e1->type = %s\n", e1->type->toChars());
1049 if (type->equals(e1->type) && to->equals(type)) 1049 if (type->equals(e1->type) && to->equals(type))
1050 return e1; 1050 return e1;
1051 1051
1052 Type *tb = to->toBasetype();
1053 Type *typeb = type->toBasetype();
1054
1055 // LDC: ported from D2 to allow char[] ~ char[n] arguments in CTFE
1056 if (e1->op == TOKstring)
1057 {
1058 if (tb->ty == Tarray && typeb->ty == Tarray &&
1059 tb->nextOf()->size() == typeb->nextOf()->size())
1060 {
1061 return expType(to, e1);
1062 }
1063 }
1064
1052 if (e1->isConst() != 1) 1065 if (e1->isConst() != 1)
1053 return EXP_CANT_INTERPRET; 1066 return EXP_CANT_INTERPRET;
1054 1067
1055 Type *tb = to->toBasetype();
1056 if (tb->ty == Tbool) 1068 if (tb->ty == Tbool)
1057 e = new IntegerExp(loc, e1->toInteger() != 0, type); 1069 e = new IntegerExp(loc, e1->toInteger() != 0, type);
1058 else if (type->isintegral()) 1070 else if (type->isintegral())
1059 { 1071 {
1060 if (e1->type->isfloating()) 1072 if (e1->type->isfloating())
1061 { dinteger_t result; 1073 { dinteger_t result;
1062 real_t r = e1->toReal(); 1074 real_t r = e1->toReal();
1063 1075
1064 switch (type->toBasetype()->ty) 1076 switch (typeb->ty)
1065 { 1077 {
1066 case Tint8: result = (d_int8)r; break; 1078 case Tint8: result = (d_int8)r; break;
1067 case Tchar: 1079 case Tchar:
1068 case Tuns8: result = (d_uns8)r; break; 1080 case Tuns8: result = (d_uns8)r; break;
1069 case Tint16: result = (d_int16)r; break; 1081 case Tint16: result = (d_int16)r; break;
1124 e->type = type; 1136 e->type = type;
1125 } 1137 }
1126 else 1138 else
1127 { 1139 {
1128 error(loc, "cannot cast %s to %s", e1->type->toChars(), type->toChars()); 1140 error(loc, "cannot cast %s to %s", e1->type->toChars(), type->toChars());
1129 e = new IntegerExp(loc, 0, type); 1141 e = new IntegerExp(loc, 0, Type::tint32);
1130 } 1142 }
1131 return e; 1143 return e;
1132 } 1144 }
1133 1145
1134 1146