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