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