DtoCast: Implement struct repainting casts

The assoiative array case seems to be mostly triggered by some
remaining parts of the old shared/immutable AA type canonicalization
issue.
This commit is contained in:
David Nadlinger 2016-01-30 21:38:37 +01:00
parent 10d17e9c92
commit 994da2e98f

View file

@ -580,6 +580,23 @@ DValue *DtoCastVector(Loc &loc, DValue *val, Type *to) {
fatal(); fatal();
} }
DValue *DtoCastStruct(Loc &loc, DValue *val, Type *to) {
Type *const totype = to->toBasetype();
if (totype->ty == Tstruct) {
// This a cast to repaint a struct to another type, which the language
// allows for identical layouts (opCast() and so on have been lowered
// earlier by the frontend).
llvm::Value *rv = val->getRVal();
llvm::Value *result =
DtoBitCast(rv, DtoType(to)->getPointerTo(), rv->getName() + ".repaint");
return new DImValue(to, result);
}
error(loc, "Internal Compiler Error: Invalid struct cast from '%s' to '%s'",
val->getType()->toChars(), to->toChars());
fatal();
}
DValue *DtoCast(Loc &loc, DValue *val, Type *to) { DValue *DtoCast(Loc &loc, DValue *val, Type *to) {
Type *fromtype = val->getType()->toBasetype(); Type *fromtype = val->getType()->toBasetype();
Type *totype = to->toBasetype(); Type *totype = to->toBasetype();
@ -633,13 +650,17 @@ DValue *DtoCast(Loc &loc, DValue *val, Type *to) {
return DtoCastPtr(loc, val, to); return DtoCastPtr(loc, val, to);
case Tdelegate: case Tdelegate:
return DtoCastDelegate(loc, val, to); return DtoCastDelegate(loc, val, to);
case Tstruct:
return DtoCastStruct(loc, val, to);
case Tnull: case Tnull:
return DtoNullValue(to, loc); return DtoNullValue(to, loc);
default: case Taarray:
if (fromtype->ty == totype->ty) { if (totype->ty == Taarray) {
// FIXME: This silently discards aggregate casts! // Do nothing, the types will match up anyway.
return val; return new DImValue(to, val->getRVal());
} }
// fall-through
default:
error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(), error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(),
to->toChars()); to->toChars());
fatal(); fatal();