Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 1 | //===--- SemaNamedCast.cpp - Semantic Analysis for Named Casts ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ named casts. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "SemaInherit.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 19 | #include <set> |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 22 | enum TryStaticCastResult { |
| 23 | TSC_NotApplicable, ///< The cast method is not applicable. |
| 24 | TSC_Success, ///< The cast method is appropriate and successful. |
| 25 | TSC_Failed ///< The cast method is appropriate, but failed. A |
| 26 | ///< diagnostic has been emitted. |
| 27 | }; |
| 28 | |
| 29 | static void CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType, |
| 30 | const SourceRange &OpRange, |
| 31 | const SourceRange &DestRange); |
| 32 | static void CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType, |
| 33 | const SourceRange &OpRange, |
| 34 | const SourceRange &DestRange); |
| 35 | static void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType, |
| 36 | const SourceRange &OpRange); |
| 37 | static void CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType, |
| 38 | const SourceRange &OpRange, |
| 39 | const SourceRange &DestRange); |
| 40 | |
| 41 | static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType); |
| 42 | static TryStaticCastResult TryStaticReferenceDowncast( |
| 43 | Sema &Self, Expr *SrcExpr, QualType DestType, const SourceRange &OpRange); |
| 44 | static TryStaticCastResult TryStaticPointerDowncast( |
| 45 | Sema &Self, QualType SrcType, QualType DestType, const SourceRange &OpRange); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 46 | static TryStaticCastResult TryStaticMemberPointerUpcast( |
| 47 | Sema &Self, QualType SrcType, QualType DestType, const SourceRange &OpRange); |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 48 | static TryStaticCastResult TryStaticDowncast(Sema &Self, QualType SrcType, |
| 49 | QualType DestType, |
| 50 | const SourceRange &OpRange, |
| 51 | QualType OrigSrcType, |
| 52 | QualType OrigDestType); |
| 53 | static TryStaticCastResult TryStaticImplicitCast(Sema &Self, Expr *SrcExpr, |
| 54 | QualType DestType, |
| 55 | const SourceRange &OpRange); |
| 56 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 57 | /// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 58 | Action::OwningExprResult |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 59 | Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, |
| 60 | SourceLocation LAngleBracketLoc, TypeTy *Ty, |
| 61 | SourceLocation RAngleBracketLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 62 | SourceLocation LParenLoc, ExprArg E, |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 63 | SourceLocation RParenLoc) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 64 | Expr *Ex = (Expr*)E.release(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 65 | QualType DestType = QualType::getFromOpaquePtr(Ty); |
| 66 | SourceRange OpRange(OpLoc, RParenLoc); |
| 67 | SourceRange DestRange(LAngleBracketLoc, RAngleBracketLoc); |
| 68 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 69 | // If the type is dependent, we won't do the semantic analysis now. |
| 70 | // FIXME: should we check this in a more fine-grained manner? |
| 71 | bool TypeDependent = DestType->isDependentType() || Ex->isTypeDependent(); |
| 72 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 73 | switch (Kind) { |
| 74 | default: assert(0 && "Unknown C++ cast!"); |
| 75 | |
| 76 | case tok::kw_const_cast: |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 77 | if (!TypeDependent) |
| 78 | CheckConstCast(*this, Ex, DestType, OpRange, DestRange); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 79 | return Owned(new (Context) CXXConstCastExpr(DestType.getNonReferenceType(), |
| 80 | Ex, DestType, OpLoc)); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 81 | |
| 82 | case tok::kw_dynamic_cast: |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 83 | if (!TypeDependent) |
| 84 | CheckDynamicCast(*this, Ex, DestType, OpRange, DestRange); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 85 | return Owned(new (Context)CXXDynamicCastExpr(DestType.getNonReferenceType(), |
| 86 | Ex, DestType, OpLoc)); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 87 | |
| 88 | case tok::kw_reinterpret_cast: |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 89 | if (!TypeDependent) |
| 90 | CheckReinterpretCast(*this, Ex, DestType, OpRange, DestRange); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 91 | return Owned(new (Context) CXXReinterpretCastExpr( |
| 92 | DestType.getNonReferenceType(), |
| 93 | Ex, DestType, OpLoc)); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 94 | |
| 95 | case tok::kw_static_cast: |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 96 | if (!TypeDependent) |
| 97 | CheckStaticCast(*this, Ex, DestType, OpRange); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 98 | return Owned(new (Context) CXXStaticCastExpr(DestType.getNonReferenceType(), |
| 99 | Ex, DestType, OpLoc)); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 102 | return ExprError(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid. |
| 106 | /// Refer to C++ 5.2.11 for details. const_cast is typically used in code |
| 107 | /// like this: |
| 108 | /// const char *str = "literal"; |
| 109 | /// legacy_function(const_cast\<char*\>(str)); |
| 110 | void |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 111 | CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType, |
| 112 | const SourceRange &OpRange, const SourceRange &DestRange) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 113 | { |
| 114 | QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType(); |
| 115 | |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 116 | DestType = Self.Context.getCanonicalType(DestType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 117 | QualType SrcType = SrcExpr->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 118 | if (const LValueReferenceType *DestTypeTmp = |
| 119 | DestType->getAsLValueReferenceType()) { |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 120 | if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 121 | // Cannot cast non-lvalue to lvalue reference type. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 122 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 123 | << "const_cast" << OrigDestType << SrcExpr->getSourceRange(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
| 127 | // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2 |
| 128 | // [...] if a pointer to T1 can be [cast] to the type pointer to T2. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 129 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); |
| 130 | SrcType = Self.Context.getPointerType(SrcType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 131 | } else { |
| 132 | // C++ 5.2.11p1: Otherwise, the result is an rvalue and the |
| 133 | // lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 134 | // conversions are performed on the expression. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 135 | Self.DefaultFunctionArrayConversion(SrcExpr); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 136 | SrcType = SrcExpr->getType(); |
| 137 | } |
| 138 | |
Sebastian Redl | f20269b | 2009-01-26 22:19:12 +0000 | [diff] [blame] | 139 | // C++ 5.2.11p5: For a const_cast involving pointers to data members [...] |
| 140 | // the rules for const_cast are the same as those used for pointers. |
| 141 | |
| 142 | if (!DestType->isPointerType() && !DestType->isMemberPointerType()) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 143 | // Cannot cast to non-pointer, non-reference type. Note that, if DestType |
| 144 | // was a reference type, we converted it to a pointer above. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 145 | // The status of rvalue references isn't entirely clear, but it looks like |
| 146 | // conversion to them is simply invalid. |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 147 | // C++ 5.2.11p3: For two pointer types [...] |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 148 | Self.Diag(OpRange.getBegin(), diag::err_bad_const_cast_dest) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 149 | << OrigDestType << DestRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 150 | return; |
| 151 | } |
Sebastian Redl | f20269b | 2009-01-26 22:19:12 +0000 | [diff] [blame] | 152 | if (DestType->isFunctionPointerType() || |
| 153 | DestType->isMemberFunctionPointerType()) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 154 | // Cannot cast direct function pointers. |
| 155 | // C++ 5.2.11p2: [...] where T is any object type or the void type [...] |
| 156 | // T is the ultimate pointee of source and target type. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 157 | Self.Diag(OpRange.getBegin(), diag::err_bad_const_cast_dest) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 158 | << OrigDestType << DestRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 161 | SrcType = Self.Context.getCanonicalType(SrcType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 162 | |
| 163 | // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are |
| 164 | // completely equal. |
| 165 | // FIXME: const_cast should probably not be able to convert between pointers |
| 166 | // to different address spaces. |
| 167 | // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers |
| 168 | // in multi-level pointers may change, but the level count must be the same, |
| 169 | // as must be the final pointee type. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 170 | while (SrcType != DestType && |
| 171 | Self.UnwrapSimilarPointerTypes(SrcType, DestType)) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 172 | SrcType = SrcType.getUnqualifiedType(); |
| 173 | DestType = DestType.getUnqualifiedType(); |
| 174 | } |
| 175 | |
| 176 | // Doug Gregor said to disallow this until users complain. |
| 177 | #if 0 |
| 178 | // If we end up with constant arrays of equal size, unwrap those too. A cast |
| 179 | // from const int [N] to int (&)[N] is invalid by my reading of the |
| 180 | // standard, but g++ accepts it even with -ansi -pedantic. |
| 181 | // No more than one level, though, so don't embed this in the unwrap loop |
| 182 | // above. |
| 183 | const ConstantArrayType *SrcTypeArr, *DestTypeArr; |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 184 | if ((SrcTypeArr = Self.Context.getAsConstantArrayType(SrcType)) && |
| 185 | (DestTypeArr = Self.Context.getAsConstantArrayType(DestType))) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 186 | { |
| 187 | if (SrcTypeArr->getSize() != DestTypeArr->getSize()) { |
| 188 | // Different array sizes. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 189 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 190 | << "const_cast" << OrigDestType << OrigSrcType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | SrcType = SrcTypeArr->getElementType().getUnqualifiedType(); |
| 194 | DestType = DestTypeArr->getElementType().getUnqualifiedType(); |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | // Since we're dealing in canonical types, the remainder must be the same. |
| 199 | if (SrcType != DestType) { |
| 200 | // Cast between unrelated types. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 201 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 202 | << "const_cast" << OrigDestType << OrigSrcType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is |
| 208 | /// valid. |
| 209 | /// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code |
| 210 | /// like this: |
| 211 | /// char *bytes = reinterpret_cast\<char*\>(int_ptr); |
| 212 | void |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 213 | CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType, |
| 214 | const SourceRange &OpRange, const SourceRange &DestRange) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 215 | { |
| 216 | QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType(); |
| 217 | |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 218 | DestType = Self.Context.getCanonicalType(DestType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 219 | QualType SrcType = SrcExpr->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 220 | if (const LValueReferenceType *DestTypeTmp = |
| 221 | DestType->getAsLValueReferenceType()) { |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 222 | if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 223 | // Cannot cast non-lvalue to reference type. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 224 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 225 | << "reinterpret_cast" << OrigDestType << SrcExpr->getSourceRange(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | |
| 229 | // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the |
| 230 | // same effect as the conversion *reinterpret_cast<T*>(&x) with the |
| 231 | // built-in & and * operators. |
| 232 | // This code does this transformation for the checked types. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 233 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); |
| 234 | SrcType = Self.Context.getPointerType(SrcType); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 235 | } else if (const RValueReferenceType *DestTypeTmp = |
| 236 | DestType->getAsRValueReferenceType()) { |
| 237 | // Both the reference conversion and the rvalue rules apply. |
| 238 | Self.DefaultFunctionArrayConversion(SrcExpr); |
| 239 | SrcType = SrcExpr->getType(); |
| 240 | |
| 241 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); |
| 242 | SrcType = Self.Context.getPointerType(SrcType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 243 | } else { |
| 244 | // C++ 5.2.10p1: [...] the lvalue-to-rvalue, array-to-pointer, and |
| 245 | // function-to-pointer standard conversions are performed on the |
| 246 | // expression v. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 247 | Self.DefaultFunctionArrayConversion(SrcExpr); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 248 | SrcType = SrcExpr->getType(); |
| 249 | } |
| 250 | |
| 251 | // Canonicalize source for comparison. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 252 | SrcType = Self.Context.getCanonicalType(SrcType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 253 | |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 254 | const MemberPointerType *DestMemPtr = DestType->getAsMemberPointerType(), |
| 255 | *SrcMemPtr = SrcType->getAsMemberPointerType(); |
| 256 | if (DestMemPtr && SrcMemPtr) { |
| 257 | // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1" |
| 258 | // can be explicitly converted to an rvalue of type "pointer to member |
| 259 | // of Y of type T2" if T1 and T2 are both function types or both object |
| 260 | // types. |
| 261 | if (DestMemPtr->getPointeeType()->isFunctionType() != |
| 262 | SrcMemPtr->getPointeeType()->isFunctionType()) { |
| 263 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic) |
| 264 | << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange; |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away |
| 269 | // constness. |
| 270 | if (CastsAwayConstness(Self, SrcType, DestType)) { |
| 271 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away) |
| 272 | << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange; |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | // A valid member pointer cast. |
| 277 | return; |
| 278 | } |
| 279 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 280 | bool destIsPtr = DestType->isPointerType(); |
| 281 | bool srcIsPtr = SrcType->isPointerType(); |
| 282 | if (!destIsPtr && !srcIsPtr) { |
| 283 | // Except for std::nullptr_t->integer, which is not supported yet, and |
| 284 | // lvalue->reference, which is handled above, at least one of the two |
| 285 | // arguments must be a pointer. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 286 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 287 | << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | if (SrcType == DestType) { |
| 292 | // C++ 5.2.10p2 has a note that mentions that, subject to all other |
| 293 | // restrictions, a cast to the same type is allowed. The intent is not |
| 294 | // entirely clear here, since all other paragraphs explicitly forbid casts |
| 295 | // to the same type. However, the behavior of compilers is pretty consistent |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 296 | // on this point: allow same-type conversion if the involved types are |
| 297 | // pointers, disallow otherwise. |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
| 301 | // Note: Clang treats enumeration types as integral types. If this is ever |
| 302 | // changed for C++, the additional check here will be redundant. |
| 303 | if (DestType->isIntegralType() && !DestType->isEnumeralType()) { |
Sebastian Redl | 03a6cf9 | 2008-11-05 22:15:14 +0000 | [diff] [blame] | 304 | assert(srcIsPtr && "One type must be a pointer"); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 305 | // C++ 5.2.10p4: A pointer can be explicitly converted to any integral |
| 306 | // type large enough to hold it. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 307 | if (Self.Context.getTypeSize(SrcType) > |
| 308 | Self.Context.getTypeSize(DestType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 309 | Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_small_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 310 | << OrigDestType << DestRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 311 | } |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | if (SrcType->isIntegralType() || SrcType->isEnumeralType()) { |
Sebastian Redl | 03a6cf9 | 2008-11-05 22:15:14 +0000 | [diff] [blame] | 316 | assert(destIsPtr && "One type must be a pointer"); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 317 | // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly |
| 318 | // converted to a pointer. |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | if (!destIsPtr || !srcIsPtr) { |
| 323 | // With the valid non-pointer conversions out of the way, we can be even |
| 324 | // more stringent. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 325 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 326 | << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | |
| 330 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 331 | if (CastsAwayConstness(Self, SrcType, DestType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 332 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 333 | << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | |
| 337 | // Not casting away constness, so the only remaining check is for compatible |
| 338 | // pointer categories. |
| 339 | |
| 340 | if (SrcType->isFunctionPointerType()) { |
| 341 | if (DestType->isFunctionPointerType()) { |
| 342 | // C++ 5.2.10p6: A pointer to a function can be explicitly converted to |
| 343 | // a pointer to a function of a different type. |
| 344 | return; |
| 345 | } |
| 346 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 347 | // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to |
| 348 | // an object type or vice versa is conditionally-supported. |
| 349 | // Compilers support it in C++03 too, though, because it's necessary for |
| 350 | // casting the return value of dlsym() and GetProcAddress(). |
| 351 | // FIXME: Conditionally-supported behavior should be configurable in the |
| 352 | // TargetInfo or similar. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 353 | if (!Self.getLangOptions().CPlusPlus0x) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 354 | Self.Diag(OpRange.getBegin(), diag::ext_reinterpret_cast_fn_obj) |
| 355 | << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 356 | } |
| 357 | return; |
| 358 | } |
| 359 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 360 | if (DestType->isFunctionPointerType()) { |
| 361 | // See above. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 362 | if (!Self.getLangOptions().CPlusPlus0x) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 363 | Self.Diag(OpRange.getBegin(), diag::ext_reinterpret_cast_fn_obj) |
| 364 | << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 365 | } |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | // C++ 5.2.10p7: A pointer to an object can be explicitly converted to |
| 370 | // a pointer to an object of different type. |
| 371 | // Void pointers are not specified, but supported by every compiler out there. |
| 372 | // So we finish by allowing everything that remains - it's got to be two |
| 373 | // object pointers. |
| 374 | } |
| 375 | |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 376 | /// CastsAwayConstness - Check if the pointer conversion from SrcType to |
| 377 | /// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by |
| 378 | /// the cast checkers. Both arguments must denote pointer (possibly to member) |
| 379 | /// types. |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 380 | bool |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 381 | CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 382 | { |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 383 | // Casting away constness is defined in C++ 5.2.11p8 with reference to |
| 384 | // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since |
| 385 | // the rules are non-trivial. So first we construct Tcv *...cv* as described |
| 386 | // in C++ 5.2.11p8. |
| 387 | assert((SrcType->isPointerType() || SrcType->isMemberPointerType()) && |
| 388 | "Source type is not pointer or pointer to member."); |
| 389 | assert((DestType->isPointerType() || DestType->isMemberPointerType()) && |
| 390 | "Destination type is not pointer or pointer to member."); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 391 | |
| 392 | QualType UnwrappedSrcType = SrcType, UnwrappedDestType = DestType; |
| 393 | llvm::SmallVector<unsigned, 8> cv1, cv2; |
| 394 | |
| 395 | // Find the qualifications. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 396 | while (Self.UnwrapSimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 397 | cv1.push_back(UnwrappedSrcType.getCVRQualifiers()); |
| 398 | cv2.push_back(UnwrappedDestType.getCVRQualifiers()); |
| 399 | } |
| 400 | assert(cv1.size() > 0 && "Must have at least one pointer level."); |
| 401 | |
| 402 | // Construct void pointers with those qualifiers (in reverse order of |
| 403 | // unwrapping, of course). |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 404 | QualType SrcConstruct = Self.Context.VoidTy; |
| 405 | QualType DestConstruct = Self.Context.VoidTy; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 406 | for (llvm::SmallVector<unsigned, 8>::reverse_iterator i1 = cv1.rbegin(), |
| 407 | i2 = cv2.rbegin(); |
| 408 | i1 != cv1.rend(); ++i1, ++i2) |
| 409 | { |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 410 | SrcConstruct = Self.Context.getPointerType( |
| 411 | SrcConstruct.getQualifiedType(*i1)); |
| 412 | DestConstruct = Self.Context.getPointerType( |
| 413 | DestConstruct.getQualifiedType(*i2)); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // Test if they're compatible. |
| 417 | return SrcConstruct != DestConstruct && |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 418 | !Self.IsQualificationConversion(SrcConstruct, DestConstruct); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid. |
| 422 | /// Refer to C++ 5.2.9 for details. Static casts are mostly used for making |
| 423 | /// implicit conversions explicit and getting rid of data loss warnings. |
| 424 | void |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 425 | CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType, |
| 426 | const SourceRange &OpRange) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 427 | { |
| 428 | // The order the tests is not entirely arbitrary. There is one conversion |
| 429 | // that can be handled in two different ways. Given: |
| 430 | // struct A {}; |
| 431 | // struct B : public A { |
| 432 | // B(); B(const A&); |
| 433 | // }; |
| 434 | // const A &a = B(); |
| 435 | // the cast static_cast<const B&>(a) could be seen as either a static |
| 436 | // reference downcast, or an explicit invocation of the user-defined |
| 437 | // conversion using B's conversion constructor. |
| 438 | // DR 427 specifies that the downcast is to be applied here. |
| 439 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 440 | // FIXME: With N2812, casts to rvalue refs will change. |
| 441 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 442 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
| 443 | if (DestType->isVoidType()) { |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | // C++ 5.2.9p5, reference downcast. |
| 448 | // See the function for details. |
| 449 | // DR 427 specifies that this is to be applied before paragraph 2. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 450 | if (TryStaticReferenceDowncast(Self, SrcExpr, DestType, OpRange) |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 451 | > TSC_NotApplicable) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 452 | return; |
| 453 | } |
| 454 | |
| 455 | // C++ 5.2.9p2: An expression e can be explicitly converted to a type T |
| 456 | // [...] if the declaration "T t(e);" is well-formed, [...]. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 457 | if (TryStaticImplicitCast(Self, SrcExpr, DestType, OpRange) > |
| 458 | TSC_NotApplicable) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
| 462 | // C++ 5.2.9p6: May apply the reverse of any standard conversion, except |
| 463 | // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean |
| 464 | // conversions, subject to further restrictions. |
| 465 | // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal |
| 466 | // of qualification conversions impossible. |
| 467 | |
| 468 | // The lvalue-to-rvalue, array-to-pointer and function-to-pointer conversions |
| 469 | // are applied to the expression. |
| 470 | QualType OrigSrcType = SrcExpr->getType(); |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 471 | Self.DefaultFunctionArrayConversion(SrcExpr); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 472 | |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 473 | QualType SrcType = Self.Context.getCanonicalType(SrcExpr->getType()); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 474 | |
| 475 | // Reverse integral promotion/conversion. All such conversions are themselves |
| 476 | // again integral promotions or conversions and are thus already handled by |
| 477 | // p2 (TryDirectInitialization above). |
| 478 | // (Note: any data loss warnings should be suppressed.) |
| 479 | // The exception is the reverse of enum->integer, i.e. integer->enum (and |
| 480 | // enum->enum). See also C++ 5.2.9p7. |
| 481 | // The same goes for reverse floating point promotion/conversion and |
| 482 | // floating-integral conversions. Again, only floating->enum is relevant. |
| 483 | if (DestType->isEnumeralType()) { |
| 484 | if (SrcType->isComplexType() || SrcType->isVectorType()) { |
| 485 | // Fall through - these cannot be converted. |
| 486 | } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) { |
| 487 | return; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast. |
| 492 | // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 493 | if (TryStaticPointerDowncast(Self, SrcType, DestType, OpRange) |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 494 | > TSC_NotApplicable) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 498 | // Reverse member pointer conversion. C++ 4.11 specifies member pointer |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 499 | // conversion. C++ 5.2.9p9 has additional information. |
| 500 | // DR54's access restrictions apply here also. |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 501 | if (TryStaticMemberPointerUpcast(Self, SrcType, DestType, OpRange) |
| 502 | > TSC_NotApplicable) { |
| 503 | return; |
| 504 | } |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 505 | |
| 506 | // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to |
| 507 | // void*. C++ 5.2.9p10 specifies additional restrictions, which really is |
| 508 | // just the usual constness stuff. |
| 509 | if (const PointerType *SrcPointer = SrcType->getAsPointerType()) { |
| 510 | QualType SrcPointee = SrcPointer->getPointeeType(); |
| 511 | if (SrcPointee->isVoidType()) { |
| 512 | if (const PointerType *DestPointer = DestType->getAsPointerType()) { |
| 513 | QualType DestPointee = DestPointer->getPointeeType(); |
| 514 | if (DestPointee->isObjectType()) { |
| 515 | // This is definitely the intended conversion, but it might fail due |
| 516 | // to a const violation. |
| 517 | if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 518 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 519 | << "static_cast" << DestType << OrigSrcType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 520 | } |
| 521 | return; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // We tried everything. Everything! Nothing works! :-( |
| 528 | // FIXME: Error reporting could be a lot better. Should store the reason |
| 529 | // why every substep failed and, at the end, select the most specific and |
| 530 | // report that. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 531 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 532 | << "static_cast" << DestType << OrigSrcType |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 533 | << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /// Tests whether a conversion according to C++ 5.2.9p5 is valid. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 537 | TryStaticCastResult |
| 538 | TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType, |
| 539 | const SourceRange &OpRange) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 540 | { |
| 541 | // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be |
| 542 | // cast to type "reference to cv2 D", where D is a class derived from B, |
| 543 | // if a valid standard conversion from "pointer to D" to "pointer to B" |
| 544 | // exists, cv2 >= cv1, and B is not a virtual base class of D. |
| 545 | // In addition, DR54 clarifies that the base must be accessible in the |
| 546 | // current context. Although the wording of DR54 only applies to the pointer |
| 547 | // variant of this rule, the intent is clearly for it to apply to the this |
| 548 | // conversion as well. |
| 549 | |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 550 | if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) { |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 551 | return TSC_NotApplicable; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | const ReferenceType *DestReference = DestType->getAsReferenceType(); |
| 555 | if (!DestReference) { |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 556 | return TSC_NotApplicable; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 557 | } |
| 558 | QualType DestPointee = DestReference->getPointeeType(); |
| 559 | |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 560 | return TryStaticDowncast(Self, SrcExpr->getType(), DestPointee, OpRange, |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 561 | SrcExpr->getType(), DestType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /// Tests whether a conversion according to C++ 5.2.9p8 is valid. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 565 | TryStaticCastResult |
| 566 | TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType, |
| 567 | const SourceRange &OpRange) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 568 | { |
| 569 | // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class |
| 570 | // type, can be converted to an rvalue of type "pointer to cv2 D", where D |
| 571 | // is a class derived from B, if a valid standard conversion from "pointer |
| 572 | // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base |
| 573 | // class of D. |
| 574 | // In addition, DR54 clarifies that the base must be accessible in the |
| 575 | // current context. |
| 576 | |
| 577 | const PointerType *SrcPointer = SrcType->getAsPointerType(); |
| 578 | if (!SrcPointer) { |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 579 | return TSC_NotApplicable; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | const PointerType *DestPointer = DestType->getAsPointerType(); |
| 583 | if (!DestPointer) { |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 584 | return TSC_NotApplicable; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 587 | return TryStaticDowncast(Self, SrcPointer->getPointeeType(), |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 588 | DestPointer->getPointeeType(), |
| 589 | OpRange, SrcType, DestType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 592 | /// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and |
| 593 | /// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 594 | /// DestType, both of which must be canonical, is possible and allowed. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 595 | TryStaticCastResult |
| 596 | TryStaticDowncast(Sema &Self, QualType SrcType, QualType DestType, |
| 597 | const SourceRange &OpRange, QualType OrigSrcType, |
| 598 | QualType OrigDestType) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 599 | { |
| 600 | // Downcast can only happen in class hierarchies, so we need classes. |
| 601 | if (!DestType->isRecordType() || !SrcType->isRecordType()) { |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 602 | return TSC_NotApplicable; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 606 | /*DetectVirtual=*/true); |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 607 | if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) { |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 608 | return TSC_NotApplicable; |
| 609 | } |
| 610 | |
| 611 | // Target type does derive from source type. Now we're serious. If an error |
| 612 | // appears now, it's not ignored. |
| 613 | // This may not be entirely in line with the standard. Take for example: |
| 614 | // struct A {}; |
| 615 | // struct B : virtual A { |
| 616 | // B(A&); |
| 617 | // }; |
| 618 | // |
| 619 | // void f() |
| 620 | // { |
| 621 | // (void)static_cast<const B&>(*((A*)0)); |
| 622 | // } |
| 623 | // As far as the standard is concerned, p5 does not apply (A is virtual), so |
| 624 | // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid. |
| 625 | // However, both GCC and Comeau reject this example, and accepting it would |
| 626 | // mean more complex code if we're to preserve the nice error message. |
| 627 | // FIXME: Being 100% compliant here would be nice to have. |
| 628 | |
| 629 | // Must preserve cv, as always. |
| 630 | if (!DestType.isAtLeastAsQualifiedAs(SrcType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 631 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 632 | << "static_cast" << OrigDestType << OrigSrcType << OpRange; |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 633 | return TSC_Failed; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) { |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 637 | // This code is analoguous to that in CheckDerivedToBaseConversion, except |
| 638 | // that it builds the paths in reverse order. |
| 639 | // To sum up: record all paths to the base and build a nice string from |
| 640 | // them. Use it to spice up the error message. |
| 641 | Paths.clear(); |
| 642 | Paths.setRecordingPaths(true); |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 643 | Self.IsDerivedFrom(DestType, SrcType, Paths); |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 644 | std::string PathDisplayStr; |
| 645 | std::set<unsigned> DisplayedPaths; |
| 646 | for (BasePaths::paths_iterator Path = Paths.begin(); |
| 647 | Path != Paths.end(); ++Path) { |
| 648 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 649 | // We haven't displayed a path to this particular base |
| 650 | // class subobject yet. |
| 651 | PathDisplayStr += "\n "; |
| 652 | for (BasePath::const_reverse_iterator Element = Path->rbegin(); |
| 653 | Element != Path->rend(); ++Element) |
| 654 | PathDisplayStr += Element->Base->getType().getAsString() + " -> "; |
| 655 | PathDisplayStr += DestType.getAsString(); |
| 656 | } |
| 657 | } |
| 658 | |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 659 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 660 | << SrcType.getUnqualifiedType() << DestType.getUnqualifiedType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 661 | << PathDisplayStr << OpRange; |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 662 | return TSC_Failed; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | if (Paths.getDetectedVirtual() != 0) { |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 666 | QualType VirtualBase(Paths.getDetectedVirtual(), 0); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 667 | Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 668 | << OrigSrcType << OrigDestType << VirtualBase << OpRange; |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 669 | return TSC_Failed; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | // FIXME: Test accessibility. |
| 673 | |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 674 | return TSC_Success; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 677 | /// TryStaticMemberPointerUpcast - Tests whether a conversion according to |
| 678 | /// C++ 5.2.9p9 is valid: |
| 679 | /// |
| 680 | /// An rvalue of type "pointer to member of D of type cv1 T" can be |
| 681 | /// converted to an rvalue of type "pointer to member of B of type cv2 T", |
| 682 | /// where B is a base class of D [...]. |
| 683 | /// |
| 684 | TryStaticCastResult |
| 685 | TryStaticMemberPointerUpcast(Sema &Self, QualType SrcType, QualType DestType, |
| 686 | const SourceRange &OpRange) |
| 687 | { |
| 688 | const MemberPointerType *SrcMemPtr = SrcType->getAsMemberPointerType(); |
| 689 | if (!SrcMemPtr) |
| 690 | return TSC_NotApplicable; |
| 691 | const MemberPointerType *DestMemPtr = DestType->getAsMemberPointerType(); |
| 692 | if (!DestMemPtr) |
| 693 | return TSC_NotApplicable; |
| 694 | |
| 695 | // T == T, modulo cv |
| 696 | if (Self.Context.getCanonicalType( |
| 697 | SrcMemPtr->getPointeeType().getUnqualifiedType()) != |
| 698 | Self.Context.getCanonicalType(DestMemPtr->getPointeeType(). |
| 699 | getUnqualifiedType())) |
| 700 | return TSC_NotApplicable; |
| 701 | |
| 702 | // B base of D |
| 703 | QualType SrcClass(SrcMemPtr->getClass(), 0); |
| 704 | QualType DestClass(DestMemPtr->getClass(), 0); |
| 705 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 706 | /*DetectVirtual=*/true); |
| 707 | if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) { |
| 708 | return TSC_NotApplicable; |
| 709 | } |
| 710 | |
| 711 | // B is a base of D. But is it an allowed base? If not, it's a hard error. |
| 712 | if (Paths.isAmbiguous(DestClass)) { |
| 713 | Paths.clear(); |
| 714 | Paths.setRecordingPaths(true); |
| 715 | bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths); |
| 716 | assert(StillOkay); |
| 717 | StillOkay = StillOkay; |
| 718 | std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths); |
| 719 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv) |
| 720 | << 1 << SrcClass << DestClass << PathDisplayStr << OpRange; |
| 721 | return TSC_Failed; |
| 722 | } |
| 723 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 724 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 725 | Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual) |
| 726 | << SrcClass << DestClass << QualType(VBase, 0) << OpRange; |
| 727 | return TSC_Failed; |
| 728 | } |
| 729 | |
| 730 | // FIXME: Test accessibility. |
| 731 | |
| 732 | return TSC_Success; |
| 733 | } |
| 734 | |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 735 | /// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 |
| 736 | /// is valid: |
| 737 | /// |
| 738 | /// An expression e can be explicitly converted to a type T using a |
| 739 | /// @c static_cast if the declaration "T t(e);" is well-formed [...]. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 740 | TryStaticCastResult |
| 741 | TryStaticImplicitCast(Sema &Self, Expr *SrcExpr, QualType DestType, |
| 742 | const SourceRange &OpRange) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 743 | { |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 744 | if (DestType->isReferenceType()) { |
| 745 | // At this point of CheckStaticCast, if the destination is a reference, |
| 746 | // this has to work. There is no other way that works. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 747 | return Self.CheckReferenceInit(SrcExpr, DestType) ? |
| 748 | TSC_Failed : TSC_Success; |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 749 | } |
| 750 | if (DestType->isRecordType()) { |
| 751 | // FIXME: Use an implementation of C++ [over.match.ctor] for this. |
| 752 | return TSC_NotApplicable; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 755 | // FIXME: To get a proper error from invalid conversions here, we need to |
| 756 | // reimplement more of this. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 757 | ImplicitConversionSequence ICS = Self.TryImplicitConversion( |
| 758 | SrcExpr, DestType); |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 759 | return ICS.ConversionKind == ImplicitConversionSequence::BadConversion ? |
| 760 | TSC_NotApplicable : TSC_Success; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid. |
| 764 | /// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime- |
| 765 | /// checked downcasts in class hierarchies. |
| 766 | void |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 767 | CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType, |
| 768 | const SourceRange &OpRange, |
| 769 | const SourceRange &DestRange) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 770 | { |
| 771 | QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType(); |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 772 | DestType = Self.Context.getCanonicalType(DestType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 773 | |
| 774 | // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type, |
| 775 | // or "pointer to cv void". |
| 776 | |
| 777 | QualType DestPointee; |
| 778 | const PointerType *DestPointer = DestType->getAsPointerType(); |
| 779 | const ReferenceType *DestReference = DestType->getAsReferenceType(); |
| 780 | if (DestPointer) { |
| 781 | DestPointee = DestPointer->getPointeeType(); |
| 782 | } else if (DestReference) { |
| 783 | DestPointee = DestReference->getPointeeType(); |
| 784 | } else { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 785 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 786 | << OrigDestType << DestRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 787 | return; |
| 788 | } |
| 789 | |
| 790 | const RecordType *DestRecord = DestPointee->getAsRecordType(); |
| 791 | if (DestPointee->isVoidType()) { |
| 792 | assert(DestPointer && "Reference to void is not possible"); |
| 793 | } else if (DestRecord) { |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 794 | if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee, |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 795 | diag::err_bad_dynamic_cast_incomplete, |
| 796 | DestRange)) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 797 | return; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 798 | } else { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 799 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 800 | << DestPointee.getUnqualifiedType() << DestRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 801 | return; |
| 802 | } |
| 803 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 804 | // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to |
| 805 | // complete class type, [...]. If T is an lvalue reference type, v shall be |
| 806 | // an lvalue of a complete class type, [...]. If T is an rvalue reference |
| 807 | // type, v shall be an expression having a complete effective class type, |
| 808 | // [...] |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 809 | |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 810 | QualType SrcType = Self.Context.getCanonicalType(OrigSrcType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 811 | QualType SrcPointee; |
| 812 | if (DestPointer) { |
| 813 | if (const PointerType *SrcPointer = SrcType->getAsPointerType()) { |
| 814 | SrcPointee = SrcPointer->getPointeeType(); |
| 815 | } else { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 816 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 817 | << OrigSrcType << SrcExpr->getSourceRange(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 818 | return; |
| 819 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 820 | } else if (DestReference->isLValueReferenceType()) { |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 821 | if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 822 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 823 | << "dynamic_cast" << OrigDestType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 824 | } |
| 825 | SrcPointee = SrcType; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 826 | } else { |
| 827 | SrcPointee = SrcType; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | const RecordType *SrcRecord = SrcPointee->getAsRecordType(); |
| 831 | if (SrcRecord) { |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 832 | if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee, |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 833 | diag::err_bad_dynamic_cast_incomplete, |
| 834 | SrcExpr->getSourceRange())) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 835 | return; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 836 | } else { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 837 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 838 | << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 839 | return; |
| 840 | } |
| 841 | |
| 842 | assert((DestPointer || DestReference) && |
| 843 | "Bad destination non-ptr/ref slipped through."); |
| 844 | assert((DestRecord || DestPointee->isVoidType()) && |
| 845 | "Bad destination pointee slipped through."); |
| 846 | assert(SrcRecord && "Bad source pointee slipped through."); |
| 847 | |
| 848 | // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness. |
| 849 | if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 850 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 851 | << "dynamic_cast" << OrigDestType << OrigSrcType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 852 | return; |
| 853 | } |
| 854 | |
| 855 | // C++ 5.2.7p3: If the type of v is the same as the required result type, |
| 856 | // [except for cv]. |
| 857 | if (DestRecord == SrcRecord) { |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | // C++ 5.2.7p5 |
| 862 | // Upcasts are resolved statically. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 863 | if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) { |
| 864 | Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee, |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 865 | OpRange.getBegin(), OpRange); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 866 | // Diagnostic already emitted on error. |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | // C++ 5.2.7p6: Otherwise, v shall be [polymorphic]. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 871 | const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition(Self.Context); |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 872 | assert(SrcDecl && "Definition missing"); |
| 873 | if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 874 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 875 | << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange(); |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 876 | } |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 877 | |
| 878 | // Done. Everything else is run-time checks. |
| 879 | } |