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