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