represents a dynamic cast where we know that the result is always null.
For example:
struct A {
virtual ~A();
};
struct B final : A { };
struct C { };
bool f(B* b) {
return dynamic_cast<C*>(b);
}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129256
91177308-0d34-0410-b5e6-
96231b3b80d8
// These should not have an inheritance path.
case CK_BitCast:
case CK_Dynamic:
+ case CK_DynamicToNull:
case CK_ToUnion:
case CK_ArrayToPointerDecay:
case CK_FunctionToPointerDecay:
/// CK_Dynamic - A C++ dynamic_cast.
CK_Dynamic,
+ /// CK_DynamicToNull - A C++ dynamic_cast that can be proven to
+ /// always yield a null result.
+ CK_DynamicToNull,
+
/// CK_ToUnion - The GCC cast-to-union extension.
/// int -> union { int x; float y; }
/// float -> union { int x; float y; }
return "UncheckedDerivedToBase";
case CK_Dynamic:
return "Dynamic";
+ case CK_DynamicToNull:
+ return "DynamicToNull";
case CK_ToUnion:
return "ToUnion";
case CK_ArrayToPointerDecay:
case CK_DerivedToBase:
case CK_UncheckedDerivedToBase:
case CK_Dynamic:
+ case CK_DynamicToNull:
case CK_ToUnion:
case CK_ArrayToPointerDecay:
case CK_FunctionToPointerDecay:
case CK_DerivedToBase:
case CK_UncheckedDerivedToBase:
case CK_Dynamic:
+ case CK_DynamicToNull:
case CK_ToUnion:
case CK_ArrayToPointerDecay:
case CK_FunctionToPointerDecay:
return MakeAddrLValue(V, E->getType());
}
- case CK_Dynamic: {
+ case CK_Dynamic:
+ case CK_DynamicToNull: {
LValue LV = EmitLValue(E->getSubExpr());
llvm::Value *V = LV.getAddress();
const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
}
switch (E->getCastKind()) {
- case CK_Dynamic: {
+ case CK_Dynamic:
+ case CK_DynamicToNull: {
+
+ // FIXME: Actually handle DynamicToNull here.
assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
LValue LV = CGF.EmitCheckedLValue(E->getSubExpr());
// FIXME: Do we also need to handle property references here?
case CK_GetObjCProperty:
case CK_ToVoid:
case CK_Dynamic:
+ case CK_DynamicToNull:
case CK_ResolveUnknownAnyType:
return 0;
CE->path_begin(), CE->path_end(),
ShouldNullCheckClassCastValue(CE));
}
- case CK_Dynamic: {
+ case CK_Dynamic:
+ case CK_DynamicToNull: {
Value *V = Visit(const_cast<Expr*>(E));
const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE);
return CGF.EmitDynamicCast(V, DCE);
return;
}
+ // If the source class is marked 'final', and the destination class does not
+ // derive from the source class, then we know that the result is always null.
+ if (SrcRecord->getDecl()->hasAttr<FinalAttr>() &&
+ !Self.IsDerivedFrom(DestPointee, SrcPointee)) {
+ Kind = CK_DynamicToNull;
+ return;
+ }
+
// C++ 5.2.7p5
// Upcasts are resolved statically.
if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
// Various C++ casts that are not handled yet.
case CK_ResolveUnknownAnyType:
case CK_Dynamic:
+ case CK_DynamicToNull:
case CK_ToUnion:
case CK_BaseToDerived:
case CK_NullToMemberPointer: