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