1d8d3ced6f5d7fa55272194b7165a2321a3be31dcJohn McCall//===--- SemaCast.cpp - Semantic Analysis for Casts -----------------------===// 226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl// 326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl// The LLVM Compiler Infrastructure 426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl// 526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl// This file is distributed under the University of Illinois Open Source 626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl// License. See LICENSE.TXT for details. 726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl// 826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl//===----------------------------------------------------------------------===// 926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl// 10d8d3ced6f5d7fa55272194b7165a2321a3be31dcJohn McCall// This file implements semantic analysis for cast expressions, including 11d8d3ced6f5d7fa55272194b7165a2321a3be31dcJohn McCall// 1) C-style casts like '(int) x' 12d8d3ced6f5d7fa55272194b7165a2321a3be31dcJohn McCall// 2) C++ functional casts like 'int(x)' 13d8d3ced6f5d7fa55272194b7165a2321a3be31dcJohn McCall// 3) C++ named casts like 'static_cast<int>(x)' 1426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl// 1526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl//===----------------------------------------------------------------------===// 1626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 172d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h" 18e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h" 1926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl#include "clang/AST/ExprCXX.h" 20a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall#include "clang/AST/ExprObjC.h" 2126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl#include "clang/AST/ASTContext.h" 22a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor#include "clang/AST/CXXInheritance.h" 23b790661a15d93941d2c33a0ea328254277b3d7e3Anders Carlsson#include "clang/Basic/PartialDiagnostic.h" 2426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl#include "llvm/ADT/SmallVector.h" 25e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl#include <set> 2626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redlusing namespace clang; 2726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 288e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 298e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 309cc11e70031365972424b43f439021d88096b146Sebastian Redlenum TryCastResult { 319cc11e70031365972424b43f439021d88096b146Sebastian Redl TC_NotApplicable, ///< The cast method is not applicable. 329cc11e70031365972424b43f439021d88096b146Sebastian Redl TC_Success, ///< The cast method is appropriate and successful. 339cc11e70031365972424b43f439021d88096b146Sebastian Redl TC_Failed ///< The cast method is appropriate, but failed. A 349cc11e70031365972424b43f439021d88096b146Sebastian Redl ///< diagnostic has been emitted. 359cc11e70031365972424b43f439021d88096b146Sebastian Redl}; 369cc11e70031365972424b43f439021d88096b146Sebastian Redl 379cc11e70031365972424b43f439021d88096b146Sebastian Redlenum CastType { 389cc11e70031365972424b43f439021d88096b146Sebastian Redl CT_Const, ///< const_cast 399cc11e70031365972424b43f439021d88096b146Sebastian Redl CT_Static, ///< static_cast 409cc11e70031365972424b43f439021d88096b146Sebastian Redl CT_Reinterpret, ///< reinterpret_cast 419cc11e70031365972424b43f439021d88096b146Sebastian Redl CT_Dynamic, ///< dynamic_cast 429cc11e70031365972424b43f439021d88096b146Sebastian Redl CT_CStyle, ///< (Type)expr 439cc11e70031365972424b43f439021d88096b146Sebastian Redl CT_Functional ///< Type(expr) 4437d6de37498017ba3b24f8b31fff392e35b7ca72Sebastian Redl}; 4537d6de37498017ba3b24f8b31fff392e35b7ca72Sebastian Redl 46b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCallnamespace { 47b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall struct CastOperation { 48b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall CastOperation(Sema &S, QualType destType, ExprResult src) 49b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall : Self(S), SrcExpr(src), DestType(destType), 50b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall ResultType(destType.getNonLValueExprType(S.Context)), 51b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall ValueKind(Expr::getValueKindForType(destType)), 525acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Kind(CK_Dependent), IsARCUnbridgedCast(false) { 53a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 54a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (const BuiltinType *placeholder = 55a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall src.get()->getType()->getAsPlaceholderType()) { 56a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall PlaceholderKind = placeholder->getKind(); 57a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } else { 58a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall PlaceholderKind = (BuiltinType::Kind) 0; 59a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 60a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 61b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 62b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Sema &Self; 63b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall ExprResult SrcExpr; 64b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall QualType DestType; 65b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall QualType ResultType; 66b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall ExprValueKind ValueKind; 67b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall CastKind Kind; 68a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall BuiltinType::Kind PlaceholderKind; 69b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall CXXCastPath BasePath; 705acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall bool IsARCUnbridgedCast; 718e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 72b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall SourceRange OpRange; 73b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall SourceRange DestRange; 748e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 75a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // Top-level semantics-checking routines. 76b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall void CheckConstCast(); 77b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall void CheckReinterpretCast(); 78c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith void CheckStaticCast(); 79b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall void CheckDynamicCast(); 806dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl void CheckCXXCStyleCast(bool FunctionalCast, bool ListInitialization); 81a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall void CheckCStyleCast(); 82a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 835acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall /// Complete an apparently-successful cast operation that yields 845acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall /// the given expression. 855acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall ExprResult complete(CastExpr *castExpr) { 865acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall // If this is an unbridged cast, wrap the result in an implicit 875acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall // cast that yields the unbridged-cast placeholder type. 885acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall if (IsARCUnbridgedCast) { 895acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall castExpr = ImplicitCastExpr::Create(Self.Context, 905acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Self.Context.ARCUnbridgedCastTy, 915acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall CK_Dependent, castExpr, 0, 925acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall castExpr->getValueKind()); 935acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall } 945acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall return Self.Owned(castExpr); 955acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall } 965acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall 97a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // Internal convenience methods. 98a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 99a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall /// Try to handle the given placeholder expression kind. Return 100a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall /// true if the source expression has the appropriate placeholder 101a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall /// kind. A placeholder can only be claimed once. 102a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall bool claimPlaceholder(BuiltinType::Kind K) { 103a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (PlaceholderKind != K) return false; 104a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 105a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall PlaceholderKind = (BuiltinType::Kind) 0; 106a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return true; 107a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 108a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 109a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall bool isPlaceholder() const { 110a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return PlaceholderKind != 0; 111a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 112a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall bool isPlaceholder(BuiltinType::Kind K) const { 113a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return PlaceholderKind == K; 114a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 1158e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 116b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall void checkCastAlign() { 117b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange); 118b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall } 119b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 120b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall void checkObjCARCConversion(Sema::CheckedConversionKind CCK) { 1214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(Self.getLangOpts().ObjCAutoRefCount); 1225acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall 123b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Expr *src = SrcExpr.get(); 1245acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall if (Self.CheckObjCARCConversion(OpRange, DestType, src, CCK) == 1255acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Sema::ACR_unbridged) 1265acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall IsARCUnbridgedCast = true; 127b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall SrcExpr = src; 128b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall } 129a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 130a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall /// Check for and handle non-overload placeholder expressions. 131a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall void checkNonOverloadPlaceholders() { 132a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (!isPlaceholder() || isPlaceholder(BuiltinType::Overload)) 133a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 134a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 135a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.take()); 136a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (SrcExpr.isInvalid()) 137a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 138a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall PlaceholderKind = (BuiltinType::Kind) 0; 139a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 140b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall }; 141b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall} 14237d6de37498017ba3b24f8b31fff392e35b7ca72Sebastian Redl 143f85e193739c953358c865005855253af4f68a497John McCallstatic bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, 144f85e193739c953358c865005855253af4f68a497John McCall bool CheckCVR, bool CheckObjCLifetime); 1459cc11e70031365972424b43f439021d88096b146Sebastian Redl 1469cc11e70031365972424b43f439021d88096b146Sebastian Redl// The Try functions attempt a specific way of casting. If they succeed, they 1479cc11e70031365972424b43f439021d88096b146Sebastian Redl// return TC_Success. If their way of casting is not appropriate for the given 1489cc11e70031365972424b43f439021d88096b146Sebastian Redl// arguments, they return TC_NotApplicable and *may* set diag to a diagnostic 1499cc11e70031365972424b43f439021d88096b146Sebastian Redl// to emit if no other way succeeds. If their way of casting is appropriate but 1509cc11e70031365972424b43f439021d88096b146Sebastian Redl// fails, they return TC_Failed and *must* set diag; they can set it to 0 if 1519cc11e70031365972424b43f439021d88096b146Sebastian Redl// they emit a specialized diagnostic. 1529cc11e70031365972424b43f439021d88096b146Sebastian Redl// All diagnostics returned by these functions must expect the same three 1539cc11e70031365972424b43f439021d88096b146Sebastian Redl// arguments: 1549cc11e70031365972424b43f439021d88096b146Sebastian Redl// %0: Cast Type (a value from the CastType enumeration) 1559cc11e70031365972424b43f439021d88096b146Sebastian Redl// %1: Source Type 1569cc11e70031365972424b43f439021d88096b146Sebastian Redl// %2: Destination Type 1579cc11e70031365972424b43f439021d88096b146Sebastian Redlstatic TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, 1588ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor QualType DestType, bool CStyle, 1598ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor CastKind &Kind, 16088b22a432549e315662f96abe148923c921970cbDouglas Gregor CXXCastPath &BasePath, 16188b22a432549e315662f96abe148923c921970cbDouglas Gregor unsigned &msg); 1629cc11e70031365972424b43f439021d88096b146Sebastian Redlstatic TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, 163f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson QualType DestType, bool CStyle, 164f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson const SourceRange &OpRange, 165f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson unsigned &msg, 1662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall CastKind &Kind, 167f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall CXXCastPath &BasePath); 1689cc11e70031365972424b43f439021d88096b146Sebastian Redlstatic TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType, 1699cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType DestType, bool CStyle, 1709cc11e70031365972424b43f439021d88096b146Sebastian Redl const SourceRange &OpRange, 17195c5d8ac29ba3423e735a0732713907e484b800dAnders Carlsson unsigned &msg, 1722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall CastKind &Kind, 173f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall CXXCastPath &BasePath); 174ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregorstatic TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType, 175ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor CanQualType DestType, bool CStyle, 1769cc11e70031365972424b43f439021d88096b146Sebastian Redl const SourceRange &OpRange, 1779cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType OrigSrcType, 17895c5d8ac29ba3423e735a0732713907e484b800dAnders Carlsson QualType OrigDestType, unsigned &msg, 1792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall CastKind &Kind, 180f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall CXXCastPath &BasePath); 181429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegleystatic TryCastResult TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, 182cee22421929c91b481f4d1bb85cd48c0f6b7510bAnders Carlsson QualType SrcType, 183cee22421929c91b481f4d1bb85cd48c0f6b7510bAnders Carlsson QualType DestType,bool CStyle, 184cee22421929c91b481f4d1bb85cd48c0f6b7510bAnders Carlsson const SourceRange &OpRange, 185cee22421929c91b481f4d1bb85cd48c0f6b7510bAnders Carlsson unsigned &msg, 1862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall CastKind &Kind, 187f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall CXXCastPath &BasePath); 188cee22421929c91b481f4d1bb85cd48c0f6b7510bAnders Carlsson 189429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegleystatic TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, 190f85e193739c953358c865005855253af4f68a497John McCall QualType DestType, 191f85e193739c953358c865005855253af4f68a497John McCall Sema::CheckedConversionKind CCK, 1929cc11e70031365972424b43f439021d88096b146Sebastian Redl const SourceRange &OpRange, 1936dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl unsigned &msg, CastKind &Kind, 1946dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl bool ListInitialization); 195429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegleystatic TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, 196f85e193739c953358c865005855253af4f68a497John McCall QualType DestType, 197f85e193739c953358c865005855253af4f68a497John McCall Sema::CheckedConversionKind CCK, 1989cc11e70031365972424b43f439021d88096b146Sebastian Redl const SourceRange &OpRange, 1996dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl unsigned &msg, CastKind &Kind, 2006dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl CXXCastPath &BasePath, 2016dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl bool ListInitialization); 2029cc11e70031365972424b43f439021d88096b146Sebastian Redlstatic TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType, 2039cc11e70031365972424b43f439021d88096b146Sebastian Redl bool CStyle, unsigned &msg); 204429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegleystatic TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, 2059cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType DestType, bool CStyle, 2069cc11e70031365972424b43f439021d88096b146Sebastian Redl const SourceRange &OpRange, 2073c31a39af925fca4a5bb48f03594dba4190e93e6Anders Carlsson unsigned &msg, 2082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall CastKind &Kind); 20937d6de37498017ba3b24f8b31fff392e35b7ca72Sebastian Redl 2101be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor 21126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's. 21260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult 21326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian RedlSema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, 21431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis SourceLocation LAngleBracketLoc, Declarator &D, 21526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl SourceLocation RAngleBracketLoc, 216f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation LParenLoc, Expr *E, 21726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl SourceLocation RParenLoc) { 218c89724cc6dcb178ec79c76d7e629d9a7b5d83418John McCall 21931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis assert(!D.isInvalidType()); 22031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis 22131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, E->getType()); 22231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis if (D.isInvalidType()) 22331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis return ExprError(); 22431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis 2254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) { 22631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis // Check that there are no default arguments (C++ only). 22731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis CheckExtraCXXDefaultArguments(D); 22831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis } 22931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis 2303fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer return BuildCXXNamedCast(OpLoc, Kind, TInfo, E, 231c89724cc6dcb178ec79c76d7e629d9a7b5d83418John McCall SourceRange(LAngleBracketLoc, RAngleBracketLoc), 232c89724cc6dcb178ec79c76d7e629d9a7b5d83418John McCall SourceRange(LParenLoc, RParenLoc)); 233c89724cc6dcb178ec79c76d7e629d9a7b5d83418John McCall} 234c89724cc6dcb178ec79c76d7e629d9a7b5d83418John McCall 23560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult 236c89724cc6dcb178ec79c76d7e629d9a7b5d83418John McCallSema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, 237429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley TypeSourceInfo *DestTInfo, Expr *E, 238c89724cc6dcb178ec79c76d7e629d9a7b5d83418John McCall SourceRange AngleBrackets, SourceRange Parens) { 239429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Ex = Owned(E); 240c89724cc6dcb178ec79c76d7e629d9a7b5d83418John McCall QualType DestType = DestTInfo->getType(); 241c89724cc6dcb178ec79c76d7e629d9a7b5d83418John McCall 2429103bb27f4eefa0e0d7935387750e3aca24abc49Douglas Gregor // If the type is dependent, we won't do the semantic analysis now. 2439103bb27f4eefa0e0d7935387750e3aca24abc49Douglas Gregor // FIXME: should we check this in a more fine-grained manner? 244429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley bool TypeDependent = DestType->isDependentType() || Ex.get()->isTypeDependent(); 2459103bb27f4eefa0e0d7935387750e3aca24abc49Douglas Gregor 246b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall CastOperation Op(*this, DestType, E); 247b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Op.OpRange = SourceRange(OpLoc, Parens.getEnd()); 248b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Op.DestRange = AngleBrackets; 249a21e06cb62dbd806073f646e756e89d8e23fc1c3John McCall 25026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl switch (Kind) { 251daa8e4e888758d55a7a759dd4a91b83921cef222John McCall default: llvm_unreachable("Unknown C++ cast!"); 25226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 25326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl case tok::kw_const_cast: 254429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (!TypeDependent) { 255b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Op.CheckConstCast(); 256b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (Op.SrcExpr.isInvalid()) 257429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return ExprError(); 258429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley } 2595acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall return Op.complete(CXXConstCastExpr::Create(Context, Op.ResultType, 2605acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Op.ValueKind, Op.SrcExpr.take(), DestTInfo, 2615acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall OpLoc, Parens.getEnd())); 26226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 263714179b179a31a0c3769a968ee18ba87b901b13fAnders Carlsson case tok::kw_dynamic_cast: { 264429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (!TypeDependent) { 265b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Op.CheckDynamicCast(); 266b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (Op.SrcExpr.isInvalid()) 267429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return ExprError(); 268429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley } 2695acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall return Op.complete(CXXDynamicCastExpr::Create(Context, Op.ResultType, 2705acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Op.ValueKind, Op.Kind, Op.SrcExpr.take(), 2715acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall &Op.BasePath, DestTInfo, 2725acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall OpLoc, Parens.getEnd())); 273714179b179a31a0c3769a968ee18ba87b901b13fAnders Carlsson } 2747f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson case tok::kw_reinterpret_cast: { 275429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (!TypeDependent) { 276b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Op.CheckReinterpretCast(); 277b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (Op.SrcExpr.isInvalid()) 278429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return ExprError(); 279429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley } 2805acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall return Op.complete(CXXReinterpretCastExpr::Create(Context, Op.ResultType, 2815acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Op.ValueKind, Op.Kind, Op.SrcExpr.take(), 2825acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall 0, DestTInfo, OpLoc, 2835acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Parens.getEnd())); 2847f9e646b7ed47bc8e9a60031ad0c2b55031e2077Anders Carlsson } 285cdb61979755c1c0699c1ee25eede9a50bf94d29bAnders Carlsson case tok::kw_static_cast: { 286429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (!TypeDependent) { 287c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith Op.CheckStaticCast(); 288b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (Op.SrcExpr.isInvalid()) 289429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return ExprError(); 290429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley } 2910aebc81e02397a5987aaa8e8c7acbdb01a31d7c3Anders Carlsson 2925acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall return Op.complete(CXXStaticCastExpr::Create(Context, Op.ResultType, 2935acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Op.ValueKind, Op.Kind, Op.SrcExpr.take(), 2945acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall &Op.BasePath, DestTInfo, 2955acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall OpLoc, Parens.getEnd())); 296cdb61979755c1c0699c1ee25eede9a50bf94d29bAnders Carlsson } 29726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 29826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 29926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 30079ab2c8104ef5df233d271560ccc734836738e56John McCall/// Try to diagnose a failed overloaded cast. Returns true if 30179ab2c8104ef5df233d271560ccc734836738e56John McCall/// diagnostics were emitted. 30279ab2c8104ef5df233d271560ccc734836738e56John McCallstatic bool tryDiagnoseOverloadedCast(Sema &S, CastType CT, 30379ab2c8104ef5df233d271560ccc734836738e56John McCall SourceRange range, Expr *src, 30420ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl QualType destType, 30520ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl bool listInitialization) { 30679ab2c8104ef5df233d271560ccc734836738e56John McCall switch (CT) { 30779ab2c8104ef5df233d271560ccc734836738e56John McCall // These cast kinds don't consider user-defined conversions. 30879ab2c8104ef5df233d271560ccc734836738e56John McCall case CT_Const: 30979ab2c8104ef5df233d271560ccc734836738e56John McCall case CT_Reinterpret: 31079ab2c8104ef5df233d271560ccc734836738e56John McCall case CT_Dynamic: 31179ab2c8104ef5df233d271560ccc734836738e56John McCall return false; 31279ab2c8104ef5df233d271560ccc734836738e56John McCall 31379ab2c8104ef5df233d271560ccc734836738e56John McCall // These do. 31479ab2c8104ef5df233d271560ccc734836738e56John McCall case CT_Static: 31579ab2c8104ef5df233d271560ccc734836738e56John McCall case CT_CStyle: 31679ab2c8104ef5df233d271560ccc734836738e56John McCall case CT_Functional: 31779ab2c8104ef5df233d271560ccc734836738e56John McCall break; 31879ab2c8104ef5df233d271560ccc734836738e56John McCall } 31979ab2c8104ef5df233d271560ccc734836738e56John McCall 32079ab2c8104ef5df233d271560ccc734836738e56John McCall QualType srcType = src->getType(); 32179ab2c8104ef5df233d271560ccc734836738e56John McCall if (!destType->isRecordType() && !srcType->isRecordType()) 32279ab2c8104ef5df233d271560ccc734836738e56John McCall return false; 32379ab2c8104ef5df233d271560ccc734836738e56John McCall 32479ab2c8104ef5df233d271560ccc734836738e56John McCall InitializedEntity entity = InitializedEntity::InitializeTemporary(destType); 32579ab2c8104ef5df233d271560ccc734836738e56John McCall InitializationKind initKind 326f85e193739c953358c865005855253af4f68a497John McCall = (CT == CT_CStyle)? InitializationKind::CreateCStyleCast(range.getBegin(), 32720ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl range, listInitialization) 3283a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl : (CT == CT_Functional)? InitializationKind::CreateFunctionalCast(range, 32920ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl listInitialization) 330c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith : InitializationKind::CreateCast(/*type range?*/ range); 33179ab2c8104ef5df233d271560ccc734836738e56John McCall InitializationSequence sequence(S, entity, initKind, &src, 1); 33279ab2c8104ef5df233d271560ccc734836738e56John McCall 333383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl assert(sequence.Failed() && "initialization succeeded on second try?"); 33479ab2c8104ef5df233d271560ccc734836738e56John McCall switch (sequence.getFailureKind()) { 33579ab2c8104ef5df233d271560ccc734836738e56John McCall default: return false; 33679ab2c8104ef5df233d271560ccc734836738e56John McCall 33779ab2c8104ef5df233d271560ccc734836738e56John McCall case InitializationSequence::FK_ConstructorOverloadFailed: 33879ab2c8104ef5df233d271560ccc734836738e56John McCall case InitializationSequence::FK_UserConversionOverloadFailed: 33979ab2c8104ef5df233d271560ccc734836738e56John McCall break; 34079ab2c8104ef5df233d271560ccc734836738e56John McCall } 34179ab2c8104ef5df233d271560ccc734836738e56John McCall 34279ab2c8104ef5df233d271560ccc734836738e56John McCall OverloadCandidateSet &candidates = sequence.getFailedCandidateSet(); 34379ab2c8104ef5df233d271560ccc734836738e56John McCall 34479ab2c8104ef5df233d271560ccc734836738e56John McCall unsigned msg = 0; 34579ab2c8104ef5df233d271560ccc734836738e56John McCall OverloadCandidateDisplayKind howManyCandidates = OCD_AllCandidates; 34679ab2c8104ef5df233d271560ccc734836738e56John McCall 34779ab2c8104ef5df233d271560ccc734836738e56John McCall switch (sequence.getFailedOverloadResult()) { 34879ab2c8104ef5df233d271560ccc734836738e56John McCall case OR_Success: llvm_unreachable("successful failed overload"); 34979ab2c8104ef5df233d271560ccc734836738e56John McCall case OR_No_Viable_Function: 35079ab2c8104ef5df233d271560ccc734836738e56John McCall if (candidates.empty()) 35179ab2c8104ef5df233d271560ccc734836738e56John McCall msg = diag::err_ovl_no_conversion_in_cast; 35279ab2c8104ef5df233d271560ccc734836738e56John McCall else 35379ab2c8104ef5df233d271560ccc734836738e56John McCall msg = diag::err_ovl_no_viable_conversion_in_cast; 35479ab2c8104ef5df233d271560ccc734836738e56John McCall howManyCandidates = OCD_AllCandidates; 35579ab2c8104ef5df233d271560ccc734836738e56John McCall break; 35679ab2c8104ef5df233d271560ccc734836738e56John McCall 35779ab2c8104ef5df233d271560ccc734836738e56John McCall case OR_Ambiguous: 35879ab2c8104ef5df233d271560ccc734836738e56John McCall msg = diag::err_ovl_ambiguous_conversion_in_cast; 35979ab2c8104ef5df233d271560ccc734836738e56John McCall howManyCandidates = OCD_ViableCandidates; 36079ab2c8104ef5df233d271560ccc734836738e56John McCall break; 36179ab2c8104ef5df233d271560ccc734836738e56John McCall 36279ab2c8104ef5df233d271560ccc734836738e56John McCall case OR_Deleted: 36379ab2c8104ef5df233d271560ccc734836738e56John McCall msg = diag::err_ovl_deleted_conversion_in_cast; 36479ab2c8104ef5df233d271560ccc734836738e56John McCall howManyCandidates = OCD_ViableCandidates; 36579ab2c8104ef5df233d271560ccc734836738e56John McCall break; 36679ab2c8104ef5df233d271560ccc734836738e56John McCall } 36779ab2c8104ef5df233d271560ccc734836738e56John McCall 36879ab2c8104ef5df233d271560ccc734836738e56John McCall S.Diag(range.getBegin(), msg) 36979ab2c8104ef5df233d271560ccc734836738e56John McCall << CT << srcType << destType 37079ab2c8104ef5df233d271560ccc734836738e56John McCall << range << src->getSourceRange(); 37179ab2c8104ef5df233d271560ccc734836738e56John McCall 37213a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles candidates.NoteCandidates(S, howManyCandidates, src); 37379ab2c8104ef5df233d271560ccc734836738e56John McCall 37479ab2c8104ef5df233d271560ccc734836738e56John McCall return true; 37579ab2c8104ef5df233d271560ccc734836738e56John McCall} 37679ab2c8104ef5df233d271560ccc734836738e56John McCall 37779ab2c8104ef5df233d271560ccc734836738e56John McCall/// Diagnose a failed cast. 37879ab2c8104ef5df233d271560ccc734836738e56John McCallstatic void diagnoseBadCast(Sema &S, unsigned msg, CastType castType, 37920ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl SourceRange opRange, Expr *src, QualType destType, 38020ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl bool listInitialization) { 381864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall if (src->getType() == S.Context.BoundMemberTy) { 382864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall (void) S.CheckPlaceholderExpr(src); // will always fail 383864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall return; 384864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall } 385864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall 38679ab2c8104ef5df233d271560ccc734836738e56John McCall if (msg == diag::err_bad_cxx_cast_generic && 38720ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl tryDiagnoseOverloadedCast(S, castType, opRange, src, destType, 38820ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl listInitialization)) 38979ab2c8104ef5df233d271560ccc734836738e56John McCall return; 39079ab2c8104ef5df233d271560ccc734836738e56John McCall 39179ab2c8104ef5df233d271560ccc734836738e56John McCall S.Diag(opRange.getBegin(), msg) << castType 39279ab2c8104ef5df233d271560ccc734836738e56John McCall << src->getType() << destType << opRange << src->getSourceRange(); 39379ab2c8104ef5df233d271560ccc734836738e56John McCall} 39479ab2c8104ef5df233d271560ccc734836738e56John McCall 39576d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl/// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes, 39676d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl/// this removes one level of indirection from both types, provided that they're 39776d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl/// the same kind of pointer (plain or to-member). Unlike the Sema function, 39876d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl/// this one doesn't care if the two pointers-to-member don't point into the 39976d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl/// same class. This is because CastsAwayConstness doesn't care. 4003c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmanstatic bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) { 40176d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl const PointerType *T1PtrType = T1->getAs<PointerType>(), 40276d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl *T2PtrType = T2->getAs<PointerType>(); 40376d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl if (T1PtrType && T2PtrType) { 40476d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl T1 = T1PtrType->getPointeeType(); 40576d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl T2 = T2PtrType->getPointeeType(); 40676d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl return true; 40776d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl } 40872a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian const ObjCObjectPointerType *T1ObjCPtrType = 40972a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian T1->getAs<ObjCObjectPointerType>(), 41072a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian *T2ObjCPtrType = 41172a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian T2->getAs<ObjCObjectPointerType>(); 41272a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian if (T1ObjCPtrType) { 41372a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian if (T2ObjCPtrType) { 41472a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian T1 = T1ObjCPtrType->getPointeeType(); 41572a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian T2 = T2ObjCPtrType->getPointeeType(); 41672a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian return true; 41772a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian } 41872a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian else if (T2PtrType) { 41972a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian T1 = T1ObjCPtrType->getPointeeType(); 42072a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian T2 = T2PtrType->getPointeeType(); 42172a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian return true; 42272a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian } 42372a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian } 42472a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian else if (T2ObjCPtrType) { 42572a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian if (T1PtrType) { 42672a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian T2 = T2ObjCPtrType->getPointeeType(); 42772a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian T1 = T1PtrType->getPointeeType(); 42872a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian return true; 42972a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian } 43072a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian } 43172a8659e9c4bdf28459cf1463e97896468f99cb9Fariborz Jahanian 43276d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), 43376d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl *T2MPType = T2->getAs<MemberPointerType>(); 43476d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl if (T1MPType && T2MPType) { 43576d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl T1 = T1MPType->getPointeeType(); 43676d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl T2 = T2MPType->getPointeeType(); 43776d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl return true; 43876d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl } 439bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor 440bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor const BlockPointerType *T1BPType = T1->getAs<BlockPointerType>(), 441bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor *T2BPType = T2->getAs<BlockPointerType>(); 442bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor if (T1BPType && T2BPType) { 443bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor T1 = T1BPType->getPointeeType(); 444bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor T2 = T2BPType->getPointeeType(); 445bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor return true; 446bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor } 447bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor 44876d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl return false; 44976d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl} 45076d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl 4519cc11e70031365972424b43f439021d88096b146Sebastian Redl/// CastsAwayConstness - Check if the pointer conversion from SrcType to 4529cc11e70031365972424b43f439021d88096b146Sebastian Redl/// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by 4539cc11e70031365972424b43f439021d88096b146Sebastian Redl/// the cast checkers. Both arguments must denote pointer (possibly to member) 4549cc11e70031365972424b43f439021d88096b146Sebastian Redl/// types. 455f85e193739c953358c865005855253af4f68a497John McCall/// 456f85e193739c953358c865005855253af4f68a497John McCall/// \param CheckCVR Whether to check for const/volatile/restrict qualifiers. 457f85e193739c953358c865005855253af4f68a497John McCall/// 458f85e193739c953358c865005855253af4f68a497John McCall/// \param CheckObjCLifetime Whether to check Objective-C lifetime qualifiers. 4595ed66f709126b60e88631bf86d7e2d59e774686fSebastian Redlstatic bool 460f85e193739c953358c865005855253af4f68a497John McCallCastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, 461f85e193739c953358c865005855253af4f68a497John McCall bool CheckCVR, bool CheckObjCLifetime) { 462f85e193739c953358c865005855253af4f68a497John McCall // If the only checking we care about is for Objective-C lifetime qualifiers, 463f85e193739c953358c865005855253af4f68a497John McCall // and we're not in ARC mode, there's nothing to check. 464f85e193739c953358c865005855253af4f68a497John McCall if (!CheckCVR && CheckObjCLifetime && 4654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !Self.Context.getLangOpts().ObjCAutoRefCount) 466f85e193739c953358c865005855253af4f68a497John McCall return false; 467f85e193739c953358c865005855253af4f68a497John McCall 4689cc11e70031365972424b43f439021d88096b146Sebastian Redl // Casting away constness is defined in C++ 5.2.11p8 with reference to 4699cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since 4709cc11e70031365972424b43f439021d88096b146Sebastian Redl // the rules are non-trivial. So first we construct Tcv *...cv* as described 4719cc11e70031365972424b43f439021d88096b146Sebastian Redl // in C++ 5.2.11p8. 472bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || 473bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor SrcType->isBlockPointerType()) && 4749cc11e70031365972424b43f439021d88096b146Sebastian Redl "Source type is not pointer or pointer to member."); 475bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() || 476bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor DestType->isBlockPointerType()) && 4779cc11e70031365972424b43f439021d88096b146Sebastian Redl "Destination type is not pointer or pointer to member."); 478f20269b42843d10c930886ee661ee1dd37a4248bSebastian Redl 479ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType), 480ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor UnwrappedDestType = Self.Context.getCanonicalType(DestType); 4815f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<Qualifiers, 8> cv1, cv2; 48226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 483d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor // Find the qualifiers. We only care about cvr-qualifiers for the 484d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor // purpose of this check, because other qualifiers (address spaces, 485d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor // Objective-C GC, etc.) are part of the type's identity. 48676d69bb7f984c390f90504a06dfc7485387ffdd7Sebastian Redl while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) { 487f85e193739c953358c865005855253af4f68a497John McCall // Determine the relevant qualifiers at this level. 488f85e193739c953358c865005855253af4f68a497John McCall Qualifiers SrcQuals, DestQuals; 48952647c63c3cbdf0c87fe8db3ef6f475bfd49725dAnders Carlsson Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals); 49052647c63c3cbdf0c87fe8db3ef6f475bfd49725dAnders Carlsson Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals); 491f85e193739c953358c865005855253af4f68a497John McCall 492f85e193739c953358c865005855253af4f68a497John McCall Qualifiers RetainedSrcQuals, RetainedDestQuals; 493f85e193739c953358c865005855253af4f68a497John McCall if (CheckCVR) { 494f85e193739c953358c865005855253af4f68a497John McCall RetainedSrcQuals.setCVRQualifiers(SrcQuals.getCVRQualifiers()); 495f85e193739c953358c865005855253af4f68a497John McCall RetainedDestQuals.setCVRQualifiers(DestQuals.getCVRQualifiers()); 496f85e193739c953358c865005855253af4f68a497John McCall } 497f85e193739c953358c865005855253af4f68a497John McCall 498f85e193739c953358c865005855253af4f68a497John McCall if (CheckObjCLifetime && 499f85e193739c953358c865005855253af4f68a497John McCall !DestQuals.compatiblyIncludesObjCLifetime(SrcQuals)) 500f85e193739c953358c865005855253af4f68a497John McCall return true; 501f85e193739c953358c865005855253af4f68a497John McCall 502f85e193739c953358c865005855253af4f68a497John McCall cv1.push_back(RetainedSrcQuals); 503f85e193739c953358c865005855253af4f68a497John McCall cv2.push_back(RetainedDestQuals); 50426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 505bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor if (cv1.empty()) 506bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor return false; 50726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 5089cc11e70031365972424b43f439021d88096b146Sebastian Redl // Construct void pointers with those qualifiers (in reverse order of 5099cc11e70031365972424b43f439021d88096b146Sebastian Redl // unwrapping, of course). 5109cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType SrcConstruct = Self.Context.VoidTy; 5119cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType DestConstruct = Self.Context.VoidTy; 5120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall ASTContext &Context = Self.Context; 5135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner for (SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(), 5140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall i2 = cv2.rbegin(); 5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump i1 != cv1.rend(); ++i1, ++i2) { 5160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall SrcConstruct 5170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1)); 5180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall DestConstruct 5190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2)); 52026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 52126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 5229cc11e70031365972424b43f439021d88096b146Sebastian Redl // Test if they're compatible. 523f85e193739c953358c865005855253af4f68a497John McCall bool ObjCLifetimeConversion; 5249cc11e70031365972424b43f439021d88096b146Sebastian Redl return SrcConstruct != DestConstruct && 525f85e193739c953358c865005855253af4f68a497John McCall !Self.IsQualificationConversion(SrcConstruct, DestConstruct, false, 526f85e193739c953358c865005855253af4f68a497John McCall ObjCLifetimeConversion); 52726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 52826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 5299cc11e70031365972424b43f439021d88096b146Sebastian Redl/// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid. 5309cc11e70031365972424b43f439021d88096b146Sebastian Redl/// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime- 5319cc11e70031365972424b43f439021d88096b146Sebastian Redl/// checked downcasts in class hierarchies. 532b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCallvoid CastOperation::CheckDynamicCast() { 533ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman if (ValueKind == VK_RValue) 5347a420df78dd207d505b0c05d5f4b12a627b8b994Eli Friedman SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); 535ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman else if (isPlaceholder()) 536ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.take()); 537ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman if (SrcExpr.isInvalid()) // if conversion failed, don't report another error 538ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman return; 5397a420df78dd207d505b0c05d5f4b12a627b8b994Eli Friedman 540b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall QualType OrigSrcType = SrcExpr.get()->getType(); 541b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall QualType DestType = Self.Context.getCanonicalType(this->DestType); 54226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 5439cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type, 5449cc11e70031365972424b43f439021d88096b146Sebastian Redl // or "pointer to cv void". 5457c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 5469cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType DestPointee; 5476217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const PointerType *DestPointer = DestType->getAs<PointerType>(); 548f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall const ReferenceType *DestReference = 0; 5499cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestPointer) { 5509cc11e70031365972424b43f439021d88096b146Sebastian Redl DestPointee = DestPointer->getPointeeType(); 551f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall } else if ((DestReference = DestType->getAs<ReferenceType>())) { 5529cc11e70031365972424b43f439021d88096b146Sebastian Redl DestPointee = DestReference->getPointeeType(); 55326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } else { 5549cc11e70031365972424b43f439021d88096b146Sebastian Redl Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr) 555b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall << this->DestType << DestRange; 5566e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl return; 5576e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl } 5586e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl 5596217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RecordType *DestRecord = DestPointee->getAs<RecordType>(); 5609cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestPointee->isVoidType()) { 5619cc11e70031365972424b43f439021d88096b146Sebastian Redl assert(DestPointer && "Reference to void is not possible"); 5629cc11e70031365972424b43f439021d88096b146Sebastian Redl } else if (DestRecord) { 5639cc11e70031365972424b43f439021d88096b146Sebastian Redl if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee, 564d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor diag::err_bad_dynamic_cast_incomplete, 565d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor DestRange)) 5669cc11e70031365972424b43f439021d88096b146Sebastian Redl return; 5679cc11e70031365972424b43f439021d88096b146Sebastian Redl } else { 5689cc11e70031365972424b43f439021d88096b146Sebastian Redl Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) 5699cc11e70031365972424b43f439021d88096b146Sebastian Redl << DestPointee.getUnqualifiedType() << DestRange; 57026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl return; 57126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 57226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 5739cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to 5749cc11e70031365972424b43f439021d88096b146Sebastian Redl // complete class type, [...]. If T is an lvalue reference type, v shall be 575dc843f221c95ed404e681b4d782bc81cba14295bDouglas Gregor // an lvalue of a complete class type, [...]. If T is an rvalue reference 576dc843f221c95ed404e681b4d782bc81cba14295bDouglas Gregor // type, v shall be an expression having a complete class type, [...] 5779cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType SrcType = Self.Context.getCanonicalType(OrigSrcType); 5789cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType SrcPointee; 5799cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestPointer) { 5806217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { 5819cc11e70031365972424b43f439021d88096b146Sebastian Redl SrcPointee = SrcPointer->getPointeeType(); 5829cc11e70031365972424b43f439021d88096b146Sebastian Redl } else { 5839cc11e70031365972424b43f439021d88096b146Sebastian Redl Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr) 584429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley << OrigSrcType << SrcExpr.get()->getSourceRange(); 5859cc11e70031365972424b43f439021d88096b146Sebastian Redl return; 58626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 5879cc11e70031365972424b43f439021d88096b146Sebastian Redl } else if (DestReference->isLValueReferenceType()) { 588429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (!SrcExpr.get()->isLValue()) { 5899cc11e70031365972424b43f439021d88096b146Sebastian Redl Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue) 590b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall << CT_Dynamic << OrigSrcType << this->DestType << OpRange; 5919cc11e70031365972424b43f439021d88096b146Sebastian Redl } 5929cc11e70031365972424b43f439021d88096b146Sebastian Redl SrcPointee = SrcType; 5939cc11e70031365972424b43f439021d88096b146Sebastian Redl } else { 5949cc11e70031365972424b43f439021d88096b146Sebastian Redl SrcPointee = SrcType; 59526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 59626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 5976217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RecordType *SrcRecord = SrcPointee->getAs<RecordType>(); 5989cc11e70031365972424b43f439021d88096b146Sebastian Redl if (SrcRecord) { 5999cc11e70031365972424b43f439021d88096b146Sebastian Redl if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee, 600d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor diag::err_bad_dynamic_cast_incomplete, 601d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor SrcExpr.get())) 6029cc11e70031365972424b43f439021d88096b146Sebastian Redl return; 6039cc11e70031365972424b43f439021d88096b146Sebastian Redl } else { 6049cc11e70031365972424b43f439021d88096b146Sebastian Redl Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) 605429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); 60626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl return; 60726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 60826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 6099cc11e70031365972424b43f439021d88096b146Sebastian Redl assert((DestPointer || DestReference) && 6109cc11e70031365972424b43f439021d88096b146Sebastian Redl "Bad destination non-ptr/ref slipped through."); 6119cc11e70031365972424b43f439021d88096b146Sebastian Redl assert((DestRecord || DestPointee->isVoidType()) && 6129cc11e70031365972424b43f439021d88096b146Sebastian Redl "Bad destination pointee slipped through."); 6139cc11e70031365972424b43f439021d88096b146Sebastian Redl assert(SrcRecord && "Bad source pointee slipped through."); 61426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 6159cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness. 6169cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) { 617d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away) 618b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall << CT_Dynamic << OrigSrcType << this->DestType << OpRange; 61926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl return; 62026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 62126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 6229cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.7p3: If the type of v is the same as the required result type, 6239cc11e70031365972424b43f439021d88096b146Sebastian Redl // [except for cv]. 6249cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestRecord == SrcRecord) { 6252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_NoOp; 62626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl return; 62726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 62826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 6299cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.7p5 6309cc11e70031365972424b43f439021d88096b146Sebastian Redl // Upcasts are resolved statically. 6319cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) { 6325cf86ba6b5a724bf91cb52feade1158f1fbeb605Anders Carlsson if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee, 6335cf86ba6b5a724bf91cb52feade1158f1fbeb605Anders Carlsson OpRange.getBegin(), OpRange, 6345cf86ba6b5a724bf91cb52feade1158f1fbeb605Anders Carlsson &BasePath)) 6355cf86ba6b5a724bf91cb52feade1158f1fbeb605Anders Carlsson return; 6365cf86ba6b5a724bf91cb52feade1158f1fbeb605Anders Carlsson 6372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_DerivedToBase; 6386fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor 6396fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor // If we are casting to or through a virtual base class, we need a 6406fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor // vtable. 6416fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor if (Self.BasePathInvolvesVirtualBase(BasePath)) 6426fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor Self.MarkVTableUsed(OpRange.getBegin(), 6436fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor cast<CXXRecordDecl>(SrcRecord->getDecl())); 64426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl return; 64526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 64626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 6479cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.7p6: Otherwise, v shall be [polymorphic]. 648952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition(); 6499cc11e70031365972424b43f439021d88096b146Sebastian Redl assert(SrcDecl && "Definition missing"); 6509cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) { 6519cc11e70031365972424b43f439021d88096b146Sebastian Redl Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic) 652429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); 6539cc11e70031365972424b43f439021d88096b146Sebastian Redl } 6546fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor Self.MarkVTableUsed(OpRange.getBegin(), 6556fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor cast<CXXRecordDecl>(SrcRecord->getDecl())); 6569cc11e70031365972424b43f439021d88096b146Sebastian Redl 6579cc11e70031365972424b43f439021d88096b146Sebastian Redl // Done. Everything else is run-time checks. 6582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_Dynamic; 65926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 66026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 6619cc11e70031365972424b43f439021d88096b146Sebastian Redl/// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid. 6629cc11e70031365972424b43f439021d88096b146Sebastian Redl/// Refer to C++ 5.2.11 for details. const_cast is typically used in code 6639cc11e70031365972424b43f439021d88096b146Sebastian Redl/// like this: 6649cc11e70031365972424b43f439021d88096b146Sebastian Redl/// const char *str = "literal"; 6659cc11e70031365972424b43f439021d88096b146Sebastian Redl/// legacy_function(const_cast\<char*\>(str)); 666b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCallvoid CastOperation::CheckConstCast() { 667ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman if (ValueKind == VK_RValue) 668429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); 669ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman else if (isPlaceholder()) 670ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.take()); 671ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman if (SrcExpr.isInvalid()) // if conversion failed, don't report another error 672ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman return; 67326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 6749cc11e70031365972424b43f439021d88096b146Sebastian Redl unsigned msg = diag::err_bad_cxx_cast_generic; 675429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (TryConstCast(Self, SrcExpr.get(), DestType, /*CStyle*/false, msg) != TC_Success 6769cc11e70031365972424b43f439021d88096b146Sebastian Redl && msg != 0) 6779cc11e70031365972424b43f439021d88096b146Sebastian Redl Self.Diag(OpRange.getBegin(), msg) << CT_Const 678429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley << SrcExpr.get()->getType() << DestType << OpRange; 6799cc11e70031365972424b43f439021d88096b146Sebastian Redl} 68026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 6819cc11e70031365972424b43f439021d88096b146Sebastian Redl/// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is 6829cc11e70031365972424b43f439021d88096b146Sebastian Redl/// valid. 6839cc11e70031365972424b43f439021d88096b146Sebastian Redl/// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code 6849cc11e70031365972424b43f439021d88096b146Sebastian Redl/// like this: 6859cc11e70031365972424b43f439021d88096b146Sebastian Redl/// char *bytes = reinterpret_cast\<char*\>(int_ptr); 686b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCallvoid CastOperation::CheckReinterpretCast() { 687ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman if (ValueKind == VK_RValue && !isPlaceholder(BuiltinType::Overload)) 688429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); 689ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman else 690ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman checkNonOverloadPlaceholders(); 691ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman if (SrcExpr.isInvalid()) // if conversion failed, don't report another error 692ed0b31f8474b60661de6cbb75fb04aa0de9bd1efEli Friedman return; 69326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 6949cc11e70031365972424b43f439021d88096b146Sebastian Redl unsigned msg = diag::err_bad_cxx_cast_generic; 695f85e193739c953358c865005855253af4f68a497John McCall TryCastResult tcr = 696f85e193739c953358c865005855253af4f68a497John McCall TryReinterpretCast(Self, SrcExpr, DestType, 697f85e193739c953358c865005855253af4f68a497John McCall /*CStyle*/false, OpRange, msg, Kind); 698f85e193739c953358c865005855253af4f68a497John McCall if (tcr != TC_Success && msg != 0) 6998e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor { 700429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (SrcExpr.isInvalid()) // if conversion failed, don't report another error 701429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return; 702429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { 7038e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor //FIXME: &f<int>; is overloaded and resolvable 7048e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload) 705429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley << OverloadExpr::find(SrcExpr.get()).Expression->getName() 7068e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor << DestType << OpRange; 707429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Self.NoteAllOverloadCandidates(SrcExpr.get()); 7088e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 70979ab2c8104ef5df233d271560ccc734836738e56John McCall } else { 71020ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl diagnoseBadCast(Self, msg, CT_Reinterpret, OpRange, SrcExpr.get(), 71120ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl DestType, /*listInitialization=*/false); 7128e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor } 7134e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (tcr == TC_Success && Self.getLangOpts().ObjCAutoRefCount) { 714b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall checkObjCARCConversion(Sema::CCK_OtherCast); 715f85e193739c953358c865005855253af4f68a497John McCall } 71626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 71726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 7189cc11e70031365972424b43f439021d88096b146Sebastian Redl 71926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl/// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid. 72026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl/// Refer to C++ 5.2.9 for details. Static casts are mostly used for making 72126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl/// implicit conversions explicit and getting rid of data loss warnings. 722c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smithvoid CastOperation::CheckStaticCast() { 723a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (isPlaceholder()) { 724a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall checkNonOverloadPlaceholders(); 725a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (SrcExpr.isInvalid()) 726a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 727a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 728a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 7299cc11e70031365972424b43f439021d88096b146Sebastian Redl // This test is outside everything else because it's the only case where 7309cc11e70031365972424b43f439021d88096b146Sebastian Redl // a non-lvalue-reference target type does not lead to decay. 7319cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". 73205d9d7aa2d5dcb92ff434143803cdaf70a21c9f9Eli Friedman if (DestType->isVoidType()) { 733a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Kind = CK_ToVoid; 734a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 735a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (claimPlaceholder(BuiltinType::Overload)) { 7366dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr, 7371be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor false, // Decay Function to ptr 7381be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor true, // Complain 7391be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor OpRange, DestType, diag::err_bad_static_cast_overload); 7406dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall if (SrcExpr.isInvalid()) 7416dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall return; 7421be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor } 743a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 744a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); 7459cc11e70031365972424b43f439021d88096b146Sebastian Redl return; 74605d9d7aa2d5dcb92ff434143803cdaf70a21c9f9Eli Friedman } 7479cc11e70031365972424b43f439021d88096b146Sebastian Redl 7486dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall if (ValueKind == VK_RValue && !DestType->isRecordType() && 7496dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall !isPlaceholder(BuiltinType::Overload)) { 750429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); 751429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (SrcExpr.isInvalid()) // if conversion failed, don't report another error 752429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return; 753429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley } 7549cc11e70031365972424b43f439021d88096b146Sebastian Redl 7559cc11e70031365972424b43f439021d88096b146Sebastian Redl unsigned msg = diag::err_bad_cxx_cast_generic; 756f85e193739c953358c865005855253af4f68a497John McCall TryCastResult tcr 757c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith = TryStaticCast(Self, SrcExpr, DestType, Sema::CCK_OtherCast, OpRange, msg, 7586dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl Kind, BasePath, /*ListInitialization=*/false); 759f85e193739c953358c865005855253af4f68a497John McCall if (tcr != TC_Success && msg != 0) { 760429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (SrcExpr.isInvalid()) 761429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return; 762429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { 763429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley OverloadExpr* oe = OverloadExpr::find(SrcExpr.get()).Expression; 7648e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload) 7654c9be89bb615ec07eb3ed507c8fa9d0baa8a5ad7Douglas Gregor << oe->getName() << DestType << OpRange 7664c9be89bb615ec07eb3ed507c8fa9d0baa8a5ad7Douglas Gregor << oe->getQualifierLoc().getSourceRange(); 767429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Self.NoteAllOverloadCandidates(SrcExpr.get()); 76879ab2c8104ef5df233d271560ccc734836738e56John McCall } else { 76920ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl diagnoseBadCast(Self, msg, CT_Static, OpRange, SrcExpr.get(), DestType, 77020ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl /*listInitialization=*/false); 7718e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor } 772f85e193739c953358c865005855253af4f68a497John McCall } else if (tcr == TC_Success) { 773f85e193739c953358c865005855253af4f68a497John McCall if (Kind == CK_BitCast) 774b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall checkCastAlign(); 7754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Self.getLangOpts().ObjCAutoRefCount) 776c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith checkObjCARCConversion(Sema::CCK_OtherCast); 777b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall } else if (Kind == CK_BitCast) { 778b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall checkCastAlign(); 7798e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor } 7809cc11e70031365972424b43f439021d88096b146Sebastian Redl} 7819cc11e70031365972424b43f439021d88096b146Sebastian Redl 7829cc11e70031365972424b43f439021d88096b146Sebastian Redl/// TryStaticCast - Check if a static cast can be performed, and do so if 7839cc11e70031365972424b43f439021d88096b146Sebastian Redl/// possible. If @p CStyle, ignore access restrictions on hierarchy casting 7849cc11e70031365972424b43f439021d88096b146Sebastian Redl/// and casting away constness. 785429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegleystatic TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, 786f85e193739c953358c865005855253af4f68a497John McCall QualType DestType, 787f85e193739c953358c865005855253af4f68a497John McCall Sema::CheckedConversionKind CCK, 788cb3c308ef0e63b2902911b985517309c26f975dcAnders Carlsson const SourceRange &OpRange, unsigned &msg, 7896dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl CastKind &Kind, CXXCastPath &BasePath, 7906dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl bool ListInitialization) { 791f85e193739c953358c865005855253af4f68a497John McCall // Determine whether we have the semantics of a C-style cast. 792f85e193739c953358c865005855253af4f68a497John McCall bool CStyle 793f85e193739c953358c865005855253af4f68a497John McCall = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast); 794f85e193739c953358c865005855253af4f68a497John McCall 79526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // The order the tests is not entirely arbitrary. There is one conversion 79626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // that can be handled in two different ways. Given: 79726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // struct A {}; 79826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // struct B : public A { 79926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // B(); B(const A&); 80026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // }; 80126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // const A &a = B(); 80226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // the cast static_cast<const B&>(a) could be seen as either a static 80326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // reference downcast, or an explicit invocation of the user-defined 80426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // conversion using B's conversion constructor. 80526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // DR 427 specifies that the downcast is to be applied here. 80626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 80726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". 8089cc11e70031365972424b43f439021d88096b146Sebastian Redl // Done outside this function. 8099cc11e70031365972424b43f439021d88096b146Sebastian Redl 8109cc11e70031365972424b43f439021d88096b146Sebastian Redl TryCastResult tcr; 81126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 81226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // C++ 5.2.9p5, reference downcast. 81326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // See the function for details. 81426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // DR 427 specifies that this is to be applied before paragraph 2. 8156dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl tcr = TryStaticReferenceDowncast(Self, SrcExpr.get(), DestType, CStyle, 8166dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl OpRange, msg, Kind, BasePath); 8179cc11e70031365972424b43f439021d88096b146Sebastian Redl if (tcr != TC_NotApplicable) 8189cc11e70031365972424b43f439021d88096b146Sebastian Redl return tcr; 81926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 820dc843f221c95ed404e681b4d782bc81cba14295bDouglas Gregor // C++0x [expr.static.cast]p3: 821dc843f221c95ed404e681b4d782bc81cba14295bDouglas Gregor // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2 822dc843f221c95ed404e681b4d782bc81cba14295bDouglas Gregor // T2" if "cv2 T2" is reference-compatible with "cv1 T1". 8236dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind, 8246dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl BasePath, msg); 82588b22a432549e315662f96abe148923c921970cbDouglas Gregor if (tcr != TC_NotApplicable) 8269cc11e70031365972424b43f439021d88096b146Sebastian Redl return tcr; 827157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl 82826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // C++ 5.2.9p2: An expression e can be explicitly converted to a type T 82926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // [...] if the declaration "T t(e);" is well-formed, [...]. 830f85e193739c953358c865005855253af4f68a497John McCall tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CCK, OpRange, msg, 8316dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl Kind, ListInitialization); 832429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (SrcExpr.isInvalid()) 833429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return TC_Failed; 8343c31a39af925fca4a5bb48f03594dba4190e93e6Anders Carlsson if (tcr != TC_NotApplicable) 8359cc11e70031365972424b43f439021d88096b146Sebastian Redl return tcr; 8360aebc81e02397a5987aaa8e8c7acbdb01a31d7c3Anders Carlsson 83726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // C++ 5.2.9p6: May apply the reverse of any standard conversion, except 83826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean 83926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // conversions, subject to further restrictions. 84026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal 84126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // of qualification conversions impossible. 8429cc11e70031365972424b43f439021d88096b146Sebastian Redl // In the CStyle case, the earlier attempt to const_cast should have taken 8439cc11e70031365972424b43f439021d88096b146Sebastian Redl // care of reverse qualification conversions. 84426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 845429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley QualType SrcType = Self.Context.getCanonicalType(SrcExpr.get()->getType()); 84626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 8471274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly 8481e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor // converted to an integral type. [...] A value of a scoped enumeration type 8491e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor // can also be explicitly converted to a floating-point type [...]. 8501e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor if (const EnumType *Enum = SrcType->getAs<EnumType>()) { 8511e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor if (Enum->getDecl()->isScoped()) { 8521e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor if (DestType->isBooleanType()) { 8531e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor Kind = CK_IntegralToBoolean; 8541e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor return TC_Success; 8551e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor } else if (DestType->isIntegralType(Self.Context)) { 8561e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor Kind = CK_IntegralCast; 8571e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor return TC_Success; 8581e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor } else if (DestType->isRealFloatingType()) { 8591e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor Kind = CK_IntegralToFloating; 8601e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor return TC_Success; 8611e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor } 8621274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor } 8631274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor } 8641e856d99c52d9e93eab48084c3aca4a59204b94bDouglas Gregor 86526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // Reverse integral promotion/conversion. All such conversions are themselves 86626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // again integral promotions or conversions and are thus already handled by 86726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // p2 (TryDirectInitialization above). 86826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // (Note: any data loss warnings should be suppressed.) 86926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // The exception is the reverse of enum->integer, i.e. integer->enum (and 87026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // enum->enum). See also C++ 5.2.9p7. 87126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // The same goes for reverse floating point promotion/conversion and 87226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // floating-integral conversions. Again, only floating->enum is relevant. 87326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl if (DestType->isEnumeralType()) { 874cc2fca2ba2716293204901b8d2393428b965f12aEli Friedman if (SrcType->isIntegralOrEnumerationType()) { 8752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_IntegralCast; 8769cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 877cc2fca2ba2716293204901b8d2393428b965f12aEli Friedman } else if (SrcType->isRealFloatingType()) { 878cc2fca2ba2716293204901b8d2393428b965f12aEli Friedman Kind = CK_FloatingToIntegral; 879cc2fca2ba2716293204901b8d2393428b965f12aEli Friedman return TC_Success; 88005d9d7aa2d5dcb92ff434143803cdaf70a21c9f9Eli Friedman } 88126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 88226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 88326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast. 88426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance. 88595c5d8ac29ba3423e735a0732713907e484b800dAnders Carlsson tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg, 886f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson Kind, BasePath); 8879cc11e70031365972424b43f439021d88096b146Sebastian Redl if (tcr != TC_NotApplicable) 8889cc11e70031365972424b43f439021d88096b146Sebastian Redl return tcr; 88926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 89021593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl // Reverse member pointer conversion. C++ 4.11 specifies member pointer 89126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // conversion. C++ 5.2.9p9 has additional information. 89226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // DR54's access restrictions apply here also. 8934ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle, 894cee22421929c91b481f4d1bb85cd48c0f6b7510bAnders Carlsson OpRange, msg, Kind, BasePath); 8959cc11e70031365972424b43f439021d88096b146Sebastian Redl if (tcr != TC_NotApplicable) 8969cc11e70031365972424b43f439021d88096b146Sebastian Redl return tcr; 89726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 89826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to 89926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // void*. C++ 5.2.9p10 specifies additional restrictions, which really is 90026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // just the usual constness stuff. 9016217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { 90226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl QualType SrcPointee = SrcPointer->getPointeeType(); 90326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl if (SrcPointee->isVoidType()) { 9046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const PointerType *DestPointer = DestType->getAs<PointerType>()) { 90526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl QualType DestPointee = DestPointer->getPointeeType(); 9068dcb29db8483d4dcaeeecc0e653b642b0a41cd2cDouglas Gregor if (DestPointee->isIncompleteOrObjectType()) { 90726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // This is definitely the intended conversion, but it might fail due 908f85e193739c953358c865005855253af4f68a497John McCall // to a qualifier violation. Note that we permit Objective-C lifetime 909f85e193739c953358c865005855253af4f68a497John McCall // and GC qualifier mismatches here. 910f85e193739c953358c865005855253af4f68a497John McCall if (!CStyle) { 911f85e193739c953358c865005855253af4f68a497John McCall Qualifiers DestPointeeQuals = DestPointee.getQualifiers(); 912f85e193739c953358c865005855253af4f68a497John McCall Qualifiers SrcPointeeQuals = SrcPointee.getQualifiers(); 913f85e193739c953358c865005855253af4f68a497John McCall DestPointeeQuals.removeObjCGCAttr(); 914f85e193739c953358c865005855253af4f68a497John McCall DestPointeeQuals.removeObjCLifetime(); 915f85e193739c953358c865005855253af4f68a497John McCall SrcPointeeQuals.removeObjCGCAttr(); 916f85e193739c953358c865005855253af4f68a497John McCall SrcPointeeQuals.removeObjCLifetime(); 917f85e193739c953358c865005855253af4f68a497John McCall if (DestPointeeQuals != SrcPointeeQuals && 918f85e193739c953358c865005855253af4f68a497John McCall !DestPointeeQuals.compatiblyIncludes(SrcPointeeQuals)) { 919f85e193739c953358c865005855253af4f68a497John McCall msg = diag::err_bad_cxx_cast_qualifiers_away; 920f85e193739c953358c865005855253af4f68a497John McCall return TC_Failed; 921f85e193739c953358c865005855253af4f68a497John McCall } 92226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 9232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_BitCast; 9249cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 92526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 92626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 9272f6c5506958dc3b84bef0671be6a2d979ab4c284Fariborz Jahanian else if (DestType->isObjCObjectPointerType()) { 9282f6c5506958dc3b84bef0671be6a2d979ab4c284Fariborz Jahanian // allow both c-style cast and static_cast of objective-c pointers as 9292f6c5506958dc3b84bef0671be6a2d979ab4c284Fariborz Jahanian // they are pervasive. 9301d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall Kind = CK_CPointerToObjCPointerCast; 93192ef5d75720c032808181133e4d7c5f82230237eFariborz Jahanian return TC_Success; 93292ef5d75720c032808181133e4d7c5f82230237eFariborz Jahanian } 9333b27f1a80e4e433b503efd344c909eeafaa9033cFariborz Jahanian else if (CStyle && DestType->isBlockPointerType()) { 9343b27f1a80e4e433b503efd344c909eeafaa9033cFariborz Jahanian // allow c-style cast of void * to block pointers. 9352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_AnyPointerToBlockPointerCast; 9363b27f1a80e4e433b503efd344c909eeafaa9033cFariborz Jahanian return TC_Success; 9373b27f1a80e4e433b503efd344c909eeafaa9033cFariborz Jahanian } 93826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 93926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 94065267b233b167c9276075376a97147f586d37a69Fariborz Jahanian // Allow arbitray objective-c pointer conversion with static casts. 94165267b233b167c9276075376a97147f586d37a69Fariborz Jahanian if (SrcType->isObjCObjectPointerType() && 942daa8e4e888758d55a7a759dd4a91b83921cef222John McCall DestType->isObjCObjectPointerType()) { 943daa8e4e888758d55a7a759dd4a91b83921cef222John McCall Kind = CK_BitCast; 94465267b233b167c9276075376a97147f586d37a69Fariborz Jahanian return TC_Success; 945daa8e4e888758d55a7a759dd4a91b83921cef222John McCall } 94665267b233b167c9276075376a97147f586d37a69Fariborz Jahanian 94726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // We tried everything. Everything! Nothing works! :-( 9489cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 94926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 95026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 951157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl/// Tests whether a conversion according to N2844 is valid. 9529cc11e70031365972424b43f439021d88096b146Sebastian RedlTryCastResult 953157be839ade8312389b20d02a3d470c0487fd756Sebastian RedlTryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType, 9548ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor bool CStyle, CastKind &Kind, CXXCastPath &BasePath, 9558ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor unsigned &msg) { 956dc843f221c95ed404e681b4d782bc81cba14295bDouglas Gregor // C++0x [expr.static.cast]p3: 957dc843f221c95ed404e681b4d782bc81cba14295bDouglas Gregor // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to 958dc843f221c95ed404e681b4d782bc81cba14295bDouglas Gregor // cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1". 9596217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RValueReferenceType *R = DestType->getAs<RValueReferenceType>(); 960157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl if (!R) 9619cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 962157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl 963dc843f221c95ed404e681b4d782bc81cba14295bDouglas Gregor if (!SrcExpr->isGLValue()) 9649cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 965157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl 966157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl // Because we try the reference downcast before this function, from now on 967157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl // this is the only cast possibility, so we issue an error if we fail now. 9689cc11e70031365972424b43f439021d88096b146Sebastian Redl // FIXME: Should allow casting away constness if CStyle. 969157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl bool DerivedToBase; 970569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor bool ObjCConversion; 971f85e193739c953358c865005855253af4f68a497John McCall bool ObjCLifetimeConversion; 9728ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor QualType FromType = SrcExpr->getType(); 9738ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor QualType ToType = R->getPointeeType(); 9748ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor if (CStyle) { 9758ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor FromType = FromType.getUnqualifiedType(); 9768ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor ToType = ToType.getUnqualifiedType(); 9778ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor } 9788ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor 979393896f49d5248435cf203cf1de60a86dc507c44Douglas Gregor if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(), 9808ec14e605725a87991f622d63f547f877ba59fefDouglas Gregor ToType, FromType, 981f85e193739c953358c865005855253af4f68a497John McCall DerivedToBase, ObjCConversion, 982f85e193739c953358c865005855253af4f68a497John McCall ObjCLifetimeConversion) 983f85e193739c953358c865005855253af4f68a497John McCall < Sema::Ref_Compatible_With_Added_Qualification) { 9849cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_lvalue_to_rvalue_cast; 9859cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 986157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl } 987157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl 98888b22a432549e315662f96abe148923c921970cbDouglas Gregor if (DerivedToBase) { 98988b22a432549e315662f96abe148923c921970cbDouglas Gregor Kind = CK_DerivedToBase; 99088b22a432549e315662f96abe148923c921970cbDouglas Gregor CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 99188b22a432549e315662f96abe148923c921970cbDouglas Gregor /*DetectVirtual=*/true); 99288b22a432549e315662f96abe148923c921970cbDouglas Gregor if (!Self.IsDerivedFrom(SrcExpr->getType(), R->getPointeeType(), Paths)) 99388b22a432549e315662f96abe148923c921970cbDouglas Gregor return TC_NotApplicable; 99488b22a432549e315662f96abe148923c921970cbDouglas Gregor 99588b22a432549e315662f96abe148923c921970cbDouglas Gregor Self.BuildBasePathArray(Paths, BasePath); 99688b22a432549e315662f96abe148923c921970cbDouglas Gregor } else 99788b22a432549e315662f96abe148923c921970cbDouglas Gregor Kind = CK_NoOp; 99888b22a432549e315662f96abe148923c921970cbDouglas Gregor 9999cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 1000157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl} 1001157be839ade8312389b20d02a3d470c0487fd756Sebastian Redl 100226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl/// Tests whether a conversion according to C++ 5.2.9p5 is valid. 10039cc11e70031365972424b43f439021d88096b146Sebastian RedlTryCastResult 100437d6de37498017ba3b24f8b31fff392e35b7ca72Sebastian RedlTryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType, 10059cc11e70031365972424b43f439021d88096b146Sebastian Redl bool CStyle, const SourceRange &OpRange, 10062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall unsigned &msg, CastKind &Kind, 1007f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall CXXCastPath &BasePath) { 100826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be 100926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // cast to type "reference to cv2 D", where D is a class derived from B, 101026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // if a valid standard conversion from "pointer to D" to "pointer to B" 101126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // exists, cv2 >= cv1, and B is not a virtual base class of D. 101226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // In addition, DR54 clarifies that the base must be accessible in the 101326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // current context. Although the wording of DR54 only applies to the pointer 101426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // variant of this rule, the intent is clearly for it to apply to the this 10159cc11e70031365972424b43f439021d88096b146Sebastian Redl // conversion as well. 101626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 10176217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const ReferenceType *DestReference = DestType->getAs<ReferenceType>(); 101826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl if (!DestReference) { 10199cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 10209cc11e70031365972424b43f439021d88096b146Sebastian Redl } 10219cc11e70031365972424b43f439021d88096b146Sebastian Redl bool RValueRef = DestReference->isRValueReferenceType(); 10227eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (!RValueRef && !SrcExpr->isLValue()) { 10239cc11e70031365972424b43f439021d88096b146Sebastian Redl // We know the left side is an lvalue reference, so we can suggest a reason. 10249cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_cxx_cast_rvalue; 10259cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 102626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 10279cc11e70031365972424b43f439021d88096b146Sebastian Redl 102826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl QualType DestPointee = DestReference->getPointeeType(); 102926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 1030ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor return TryStaticDowncast(Self, 1031ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor Self.Context.getCanonicalType(SrcExpr->getType()), 1032ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor Self.Context.getCanonicalType(DestPointee), CStyle, 1033f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson OpRange, SrcExpr->getType(), DestType, msg, Kind, 1034f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson BasePath); 103526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 103626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 103726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl/// Tests whether a conversion according to C++ 5.2.9p8 is valid. 10389cc11e70031365972424b43f439021d88096b146Sebastian RedlTryCastResult 103937d6de37498017ba3b24f8b31fff392e35b7ca72Sebastian RedlTryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType, 10401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump bool CStyle, const SourceRange &OpRange, 10412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall unsigned &msg, CastKind &Kind, 1042f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall CXXCastPath &BasePath) { 104326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class 104426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // type, can be converted to an rvalue of type "pointer to cv2 D", where D 104526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // is a class derived from B, if a valid standard conversion from "pointer 104626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base 104726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // class of D. 104826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // In addition, DR54 clarifies that the base must be accessible in the 104926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // current context. 105026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 10516217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const PointerType *DestPointer = DestType->getAs<PointerType>(); 105226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl if (!DestPointer) { 10539cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 10549cc11e70031365972424b43f439021d88096b146Sebastian Redl } 10559cc11e70031365972424b43f439021d88096b146Sebastian Redl 10566217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const PointerType *SrcPointer = SrcType->getAs<PointerType>(); 10579cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!SrcPointer) { 10589cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_static_cast_pointer_nonpointer; 10599cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 106026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 106126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 1062ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor return TryStaticDowncast(Self, 1063ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor Self.Context.getCanonicalType(SrcPointer->getPointeeType()), 1064ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor Self.Context.getCanonicalType(DestPointer->getPointeeType()), 1065f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson CStyle, OpRange, SrcType, DestType, msg, Kind, 1066f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson BasePath); 106726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 106826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 1069e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and 1070e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to 1071ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor/// DestType is possible and allowed. 10729cc11e70031365972424b43f439021d88096b146Sebastian RedlTryCastResult 1073ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas GregorTryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType, 10749cc11e70031365972424b43f439021d88096b146Sebastian Redl bool CStyle, const SourceRange &OpRange, QualType OrigSrcType, 107595c5d8ac29ba3423e735a0732713907e484b800dAnders Carlsson QualType OrigDestType, unsigned &msg, 10762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall CastKind &Kind, CXXCastPath &BasePath) { 10775ed66f709126b60e88631bf86d7e2d59e774686fSebastian Redl // We can only work with complete types. But don't complain if it doesn't work 1078d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, 0) || 1079d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor Self.RequireCompleteType(OpRange.getBegin(), DestType, 0)) 10805ed66f709126b60e88631bf86d7e2d59e774686fSebastian Redl return TC_NotApplicable; 10815ed66f709126b60e88631bf86d7e2d59e774686fSebastian Redl 108226d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl // Downcast can only happen in class hierarchies, so we need classes. 1083ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) { 10849cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 108526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 108626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 1087f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 1088a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor /*DetectVirtual=*/true); 108937d6de37498017ba3b24f8b31fff392e35b7ca72Sebastian Redl if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) { 10909cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 1091e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl } 1092e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl 1093e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // Target type does derive from source type. Now we're serious. If an error 1094e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // appears now, it's not ignored. 1095e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // This may not be entirely in line with the standard. Take for example: 1096e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // struct A {}; 1097e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // struct B : virtual A { 1098e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // B(A&); 1099e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // }; 11001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // 1101e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // void f() 1102e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // { 1103e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // (void)static_cast<const B&>(*((A*)0)); 1104e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // } 1105e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // As far as the standard is concerned, p5 does not apply (A is virtual), so 1106e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid. 1107e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // However, both GCC and Comeau reject this example, and accepting it would 1108e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // mean more complex code if we're to preserve the nice error message. 1109e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // FIXME: Being 100% compliant here would be nice to have. 1110e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl 11119cc11e70031365972424b43f439021d88096b146Sebastian Redl // Must preserve cv, as always, unless we're in C-style mode. 11129cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) { 1113d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor msg = diag::err_bad_cxx_cast_qualifiers_away; 11149cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 111526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 111626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 111726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) { 1118e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // This code is analoguous to that in CheckDerivedToBaseConversion, except 1119e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // that it builds the paths in reverse order. 1120e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // To sum up: record all paths to the base and build a nice string from 1121e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // them. Use it to spice up the error message. 11229cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!Paths.isRecordingPaths()) { 11239cc11e70031365972424b43f439021d88096b146Sebastian Redl Paths.clear(); 11249cc11e70031365972424b43f439021d88096b146Sebastian Redl Paths.setRecordingPaths(true); 11259cc11e70031365972424b43f439021d88096b146Sebastian Redl Self.IsDerivedFrom(DestType, SrcType, Paths); 11269cc11e70031365972424b43f439021d88096b146Sebastian Redl } 1127e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl std::string PathDisplayStr; 1128e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl std::set<unsigned> DisplayedPaths; 1129a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end(); 11309cc11e70031365972424b43f439021d88096b146Sebastian Redl PI != PE; ++PI) { 11319cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) { 1132e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // We haven't displayed a path to this particular base 1133e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl // class subobject yet. 1134e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl PathDisplayStr += "\n "; 1135a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(), 1136a8f32e0965ee19ecc53cd796e34268377a20357cDouglas Gregor EE = PI->rend(); 11379cc11e70031365972424b43f439021d88096b146Sebastian Redl EI != EE; ++EI) 11389cc11e70031365972424b43f439021d88096b146Sebastian Redl PathDisplayStr += EI->Base->getType().getAsString() + " -> "; 1139ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor PathDisplayStr += QualType(DestType).getAsString(); 1140e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl } 1141e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl } 1142e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl 1143c9c7c4e06bba5dce053162ea1ead5743d7bba35bChris Lattner Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast) 1144ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor << QualType(SrcType).getUnqualifiedType() 1145ab15d0e5af616c11b6cbb478c81d0a8eaa9d450aDouglas Gregor << QualType(DestType).getUnqualifiedType() 1146c9c7c4e06bba5dce053162ea1ead5743d7bba35bChris Lattner << PathDisplayStr << OpRange; 11479cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = 0; 11489cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 114926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 115026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 115126d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl if (Paths.getDetectedVirtual() != 0) { 1152e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl QualType VirtualBase(Paths.getDetectedVirtual(), 0); 1153c9c7c4e06bba5dce053162ea1ead5743d7bba35bChris Lattner Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual) 1154d162584991885ab004a02573a73ce06422b921fcChris Lattner << OrigSrcType << OrigDestType << VirtualBase << OpRange; 11559cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = 0; 11569cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 115726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 115826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 1159417d39f300da40067600863c2733fdeb513fb4d2John McCall if (!CStyle) { 1160417d39f300da40067600863c2733fdeb513fb4d2John McCall switch (Self.CheckBaseClassAccess(OpRange.getBegin(), 1161417d39f300da40067600863c2733fdeb513fb4d2John McCall SrcType, DestType, 1162417d39f300da40067600863c2733fdeb513fb4d2John McCall Paths.front(), 116358e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall diag::err_downcast_from_inaccessible_base)) { 1164417d39f300da40067600863c2733fdeb513fb4d2John McCall case Sema::AR_accessible: 1165417d39f300da40067600863c2733fdeb513fb4d2John McCall case Sema::AR_delayed: // be optimistic 1166417d39f300da40067600863c2733fdeb513fb4d2John McCall case Sema::AR_dependent: // be optimistic 1167417d39f300da40067600863c2733fdeb513fb4d2John McCall break; 1168417d39f300da40067600863c2733fdeb513fb4d2John McCall 1169417d39f300da40067600863c2733fdeb513fb4d2John McCall case Sema::AR_inaccessible: 1170417d39f300da40067600863c2733fdeb513fb4d2John McCall msg = 0; 1171417d39f300da40067600863c2733fdeb513fb4d2John McCall return TC_Failed; 1172417d39f300da40067600863c2733fdeb513fb4d2John McCall } 11739cc11e70031365972424b43f439021d88096b146Sebastian Redl } 117426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 1175f9d68e1dd015972318b2448f75115ff4fc3d5008Anders Carlsson Self.BuildBasePathArray(Paths, BasePath); 11762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_BaseToDerived; 11779cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 117826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 117926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 118021593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl/// TryStaticMemberPointerUpcast - Tests whether a conversion according to 118121593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl/// C++ 5.2.9p9 is valid: 118221593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl/// 118321593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl/// An rvalue of type "pointer to member of D of type cv1 T" can be 118421593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl/// converted to an rvalue of type "pointer to member of B of type cv2 T", 118521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl/// where B is a base class of D [...]. 118621593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl/// 11879cc11e70031365972424b43f439021d88096b146Sebastian RedlTryCastResult 1188429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleyTryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, QualType SrcType, 11894ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor QualType DestType, bool CStyle, 11904ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor const SourceRange &OpRange, 11912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall unsigned &msg, CastKind &Kind, 1192f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall CXXCastPath &BasePath) { 11936217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(); 119421593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl if (!DestMemPtr) 11959cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 11964ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor 11974ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor bool WasOverloadedFunction = false; 11986bb8017bb9e828d118e15e59d71c66bba323c364John McCall DeclAccessPair FoundOverload; 1199429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { 12001a8cf73a825ef35917eede448817237b5fd47b05Douglas Gregor if (FunctionDecl *Fn 1201429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), DestType, false, 12021a8cf73a825ef35917eede448817237b5fd47b05Douglas Gregor FoundOverload)) { 12031a8cf73a825ef35917eede448817237b5fd47b05Douglas Gregor CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); 12041a8cf73a825ef35917eede448817237b5fd47b05Douglas Gregor SrcType = Self.Context.getMemberPointerType(Fn->getType(), 12051a8cf73a825ef35917eede448817237b5fd47b05Douglas Gregor Self.Context.getTypeDeclType(M->getParent()).getTypePtr()); 12061a8cf73a825ef35917eede448817237b5fd47b05Douglas Gregor WasOverloadedFunction = true; 12071a8cf73a825ef35917eede448817237b5fd47b05Douglas Gregor } 12084ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor } 12091a8cf73a825ef35917eede448817237b5fd47b05Douglas Gregor 12106217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>(); 12119cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!SrcMemPtr) { 12129cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_static_cast_member_pointer_nonmp; 12139cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 12149cc11e70031365972424b43f439021d88096b146Sebastian Redl } 121521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl 121621593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl // T == T, modulo cv 1217a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(), 1218a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor DestMemPtr->getPointeeType())) 12199cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 122021593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl 122121593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl // B base of D 122221593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl QualType SrcClass(SrcMemPtr->getClass(), 0); 122321593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl QualType DestClass(DestMemPtr->getClass(), 0); 1224cee22421929c91b481f4d1bb85cd48c0f6b7510bAnders Carlsson CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 122521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl /*DetectVirtual=*/true); 122621593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) { 12279cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 122821593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl } 122921593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl 123021593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl // B is a base of D. But is it an allowed base? If not, it's a hard error. 1231e0d5fe2a417b84ac8b51927ebeb8f1c9ae492760Douglas Gregor if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) { 123221593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl Paths.clear(); 123321593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl Paths.setRecordingPaths(true); 123421593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths); 123521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl assert(StillOkay); 1236c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin (void)StillOkay; 123721593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths); 123821593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv) 123921593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl << 1 << SrcClass << DestClass << PathDisplayStr << OpRange; 12409cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = 0; 12419cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 124221593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl } 124321593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl 1244c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor if (const RecordType *VBase = Paths.getDetectedVirtual()) { 124521593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual) 124621593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl << SrcClass << DestClass << QualType(VBase, 0) << OpRange; 12479cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = 0; 12489cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 124921593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl } 125021593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl 1251417d39f300da40067600863c2733fdeb513fb4d2John McCall if (!CStyle) { 1252417d39f300da40067600863c2733fdeb513fb4d2John McCall switch (Self.CheckBaseClassAccess(OpRange.getBegin(), 1253417d39f300da40067600863c2733fdeb513fb4d2John McCall DestClass, SrcClass, 1254417d39f300da40067600863c2733fdeb513fb4d2John McCall Paths.front(), 1255417d39f300da40067600863c2733fdeb513fb4d2John McCall diag::err_upcast_to_inaccessible_base)) { 1256417d39f300da40067600863c2733fdeb513fb4d2John McCall case Sema::AR_accessible: 1257417d39f300da40067600863c2733fdeb513fb4d2John McCall case Sema::AR_delayed: 1258417d39f300da40067600863c2733fdeb513fb4d2John McCall case Sema::AR_dependent: 1259417d39f300da40067600863c2733fdeb513fb4d2John McCall // Optimistically assume that the delayed and dependent cases 1260417d39f300da40067600863c2733fdeb513fb4d2John McCall // will work out. 1261417d39f300da40067600863c2733fdeb513fb4d2John McCall break; 1262417d39f300da40067600863c2733fdeb513fb4d2John McCall 1263417d39f300da40067600863c2733fdeb513fb4d2John McCall case Sema::AR_inaccessible: 1264417d39f300da40067600863c2733fdeb513fb4d2John McCall msg = 0; 1265417d39f300da40067600863c2733fdeb513fb4d2John McCall return TC_Failed; 1266417d39f300da40067600863c2733fdeb513fb4d2John McCall } 12679cc11e70031365972424b43f439021d88096b146Sebastian Redl } 126821593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl 12694ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor if (WasOverloadedFunction) { 12704ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor // Resolve the address of the overloaded function again, this time 12714ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor // allowing complaints if something goes wrong. 1272429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), 12734ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor DestType, 12746bb8017bb9e828d118e15e59d71c66bba323c364John McCall true, 12756bb8017bb9e828d118e15e59d71c66bba323c364John McCall FoundOverload); 12764ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor if (!Fn) { 12774ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor msg = 0; 12784ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor return TC_Failed; 12794ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor } 12804ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor 12816bb8017bb9e828d118e15e59d71c66bba323c364John McCall SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn); 1282429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (!SrcExpr.isUsable()) { 12834ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor msg = 0; 12844ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor return TC_Failed; 12854ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor } 12864ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor } 12874ce46c2db2b17ef52b34dbeeec01e448025c8edcDouglas Gregor 1288cee22421929c91b481f4d1bb85cd48c0f6b7510bAnders Carlsson Self.BuildBasePathArray(Paths, BasePath); 12892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_DerivedToBaseMemberPointer; 12909cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 129121593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl} 129221593acb933324b439bc68b68e7cc7d1c3e3484dSebastian Redl 1293e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 1294e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl/// is valid: 1295e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl/// 1296e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl/// An expression e can be explicitly converted to a type T using a 1297e3dc28a32a61960bdef59f79d0da161c72a5c88aSebastian Redl/// @c static_cast if the declaration "T t(e);" is well-formed [...]. 12989cc11e70031365972424b43f439021d88096b146Sebastian RedlTryCastResult 1299429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn WiegleyTryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, 1300f85e193739c953358c865005855253af4f68a497John McCall Sema::CheckedConversionKind CCK, 1301f85e193739c953358c865005855253af4f68a497John McCall const SourceRange &OpRange, unsigned &msg, 13026dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl CastKind &Kind, bool ListInitialization) { 1303d851b37a331a7bc0047922ea9244aff85470987eAnders Carlsson if (DestType->isRecordType()) { 1304d851b37a331a7bc0047922ea9244aff85470987eAnders Carlsson if (Self.RequireCompleteType(OpRange.getBegin(), DestType, 130521eb6d4961abf710de3864a537655e9c57903803Aaron Ballman diag::err_bad_dynamic_cast_incomplete) || 1306860a319e62b0e256817a458396d191aa91c0787aEli Friedman Self.RequireNonAbstractType(OpRange.getBegin(), DestType, 130721eb6d4961abf710de3864a537655e9c57903803Aaron Ballman diag::err_allocation_of_abstract_type)) { 1308d851b37a331a7bc0047922ea9244aff85470987eAnders Carlsson msg = 0; 1309d851b37a331a7bc0047922ea9244aff85470987eAnders Carlsson return TC_Failed; 1310d851b37a331a7bc0047922ea9244aff85470987eAnders Carlsson } 1311d851b37a331a7bc0047922ea9244aff85470987eAnders Carlsson } 13123a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl 1313f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType); 1314f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor InitializationKind InitKind 1315f85e193739c953358c865005855253af4f68a497John McCall = (CCK == Sema::CCK_CStyleCast) 13163a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl ? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange, 13176dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl ListInitialization) 1318f85e193739c953358c865005855253af4f68a497John McCall : (CCK == Sema::CCK_FunctionalCast) 13196dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl ? InitializationKind::CreateFunctionalCast(OpRange, ListInitialization) 1320c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith : InitializationKind::CreateCast(OpRange); 1321429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Expr *SrcExprRaw = SrcExpr.get(); 1322429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExprRaw, 1); 13238e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 13248e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor // At this point of CheckStaticCast, if the destination is a reference, 13258e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor // or the expression is an overload expression this has to work. 13268e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor // There is no other way that works. 13278e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor // On the other hand, if we're checking a C-style cast, we've still got 13288e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor // the reinterpret_cast way. 1329f85e193739c953358c865005855253af4f68a497John McCall bool CStyle 1330f85e193739c953358c865005855253af4f68a497John McCall = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast); 1331383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl if (InitSeq.Failed() && (CStyle || !DestType->isReferenceType())) 13323c31a39af925fca4a5bb48f03594dba4190e93e6Anders Carlsson return TC_NotApplicable; 1333d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor 13345354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer ExprResult Result = InitSeq.Perform(Self, Entity, InitKind, SrcExprRaw); 1335f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor if (Result.isInvalid()) { 1336f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor msg = 0; 1337f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor return TC_Failed; 1338f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor } 1339f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor 1340d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor if (InitSeq.isConstructorInitialization()) 13412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_ConstructorConversion; 1342d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor else 13432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_NoOp; 1344d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor 13453fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer SrcExpr = Result; 1346f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor return TC_Success; 134726d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 134826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 13499cc11e70031365972424b43f439021d88096b146Sebastian Redl/// TryConstCast - See if a const_cast from source to destination is allowed, 13509cc11e70031365972424b43f439021d88096b146Sebastian Redl/// and perform it if it is. 13519cc11e70031365972424b43f439021d88096b146Sebastian Redlstatic TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType, 13529cc11e70031365972424b43f439021d88096b146Sebastian Redl bool CStyle, unsigned &msg) { 135337d6de37498017ba3b24f8b31fff392e35b7ca72Sebastian Redl DestType = Self.Context.getCanonicalType(DestType); 13549cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType SrcType = SrcExpr->getType(); 1355575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) { 1356575d2a30f288ddab2f24a77dfcc71f6f7f808394Douglas Gregor if (DestTypeTmp->isLValueReferenceType() && !SrcExpr->isLValue()) { 13579cc11e70031365972424b43f439021d88096b146Sebastian Redl // Cannot const_cast non-lvalue to lvalue reference type. But if this 13589cc11e70031365972424b43f439021d88096b146Sebastian Redl // is C-style, static_cast might find a way, so we simply suggest a 13599cc11e70031365972424b43f439021d88096b146Sebastian Redl // message and tell the parent to keep searching. 13609cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_cxx_cast_rvalue; 13619cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 13629cc11e70031365972424b43f439021d88096b146Sebastian Redl } 136326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 13649cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2 13659cc11e70031365972424b43f439021d88096b146Sebastian Redl // [...] if a pointer to T1 can be [cast] to the type pointer to T2. 13669cc11e70031365972424b43f439021d88096b146Sebastian Redl DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); 13679cc11e70031365972424b43f439021d88096b146Sebastian Redl SrcType = Self.Context.getPointerType(SrcType); 13689cc11e70031365972424b43f439021d88096b146Sebastian Redl } 136926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 13709cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.11p5: For a const_cast involving pointers to data members [...] 13719cc11e70031365972424b43f439021d88096b146Sebastian Redl // the rules for const_cast are the same as those used for pointers. 13729cc11e70031365972424b43f439021d88096b146Sebastian Redl 1373d425d2b78665f42dddda56da31d6a3f576493474John McCall if (!DestType->isPointerType() && 1374d425d2b78665f42dddda56da31d6a3f576493474John McCall !DestType->isMemberPointerType() && 1375d425d2b78665f42dddda56da31d6a3f576493474John McCall !DestType->isObjCObjectPointerType()) { 13769cc11e70031365972424b43f439021d88096b146Sebastian Redl // Cannot cast to non-pointer, non-reference type. Note that, if DestType 13779cc11e70031365972424b43f439021d88096b146Sebastian Redl // was a reference type, we converted it to a pointer above. 13789cc11e70031365972424b43f439021d88096b146Sebastian Redl // The status of rvalue references isn't entirely clear, but it looks like 13799cc11e70031365972424b43f439021d88096b146Sebastian Redl // conversion to them is simply invalid. 13809cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.11p3: For two pointer types [...] 13819cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!CStyle) 13829cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_const_cast_dest; 13839cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 13849cc11e70031365972424b43f439021d88096b146Sebastian Redl } 13859cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestType->isFunctionPointerType() || 13869cc11e70031365972424b43f439021d88096b146Sebastian Redl DestType->isMemberFunctionPointerType()) { 13879cc11e70031365972424b43f439021d88096b146Sebastian Redl // Cannot cast direct function pointers. 13889cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.11p2: [...] where T is any object type or the void type [...] 13899cc11e70031365972424b43f439021d88096b146Sebastian Redl // T is the ultimate pointee of source and target type. 13909cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!CStyle) 13919cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_const_cast_dest; 13929cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 139326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 13949cc11e70031365972424b43f439021d88096b146Sebastian Redl SrcType = Self.Context.getCanonicalType(SrcType); 139526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 13969cc11e70031365972424b43f439021d88096b146Sebastian Redl // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are 13979cc11e70031365972424b43f439021d88096b146Sebastian Redl // completely equal. 13989cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers 13999cc11e70031365972424b43f439021d88096b146Sebastian Redl // in multi-level pointers may change, but the level count must be the same, 14009cc11e70031365972424b43f439021d88096b146Sebastian Redl // as must be the final pointee type. 14019cc11e70031365972424b43f439021d88096b146Sebastian Redl while (SrcType != DestType && 14025a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) { 1403d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor Qualifiers SrcQuals, DestQuals; 1404d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor SrcType = Self.Context.getUnqualifiedArrayType(SrcType, SrcQuals); 1405d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor DestType = Self.Context.getUnqualifiedArrayType(DestType, DestQuals); 1406d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor 1407d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor // const_cast is permitted to strip cvr-qualifiers, only. Make sure that 1408d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor // the other qualifiers (e.g., address spaces) are identical. 1409d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor SrcQuals.removeCVRQualifiers(); 1410d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor DestQuals.removeCVRQualifiers(); 1411d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor if (SrcQuals != DestQuals) 1412d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor return TC_NotApplicable; 141326d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 141426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 14159cc11e70031365972424b43f439021d88096b146Sebastian Redl // Since we're dealing in canonical types, the remainder must be the same. 14169cc11e70031365972424b43f439021d88096b146Sebastian Redl if (SrcType != DestType) 14179cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 141826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 14199cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 14209cc11e70031365972424b43f439021d88096b146Sebastian Redl} 14219cc11e70031365972424b43f439021d88096b146Sebastian Redl 1422f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis// Checks for undefined behavior in reinterpret_cast. 1423f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis// The cases that is checked for is: 1424f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis// *reinterpret_cast<T*>(&a) 1425f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis// reinterpret_cast<T&>(a) 1426f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis// where accessing 'a' as type 'T' will result in undefined behavior. 1427f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidisvoid Sema::CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType, 1428f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis bool IsDereference, 1429f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis SourceRange Range) { 1430f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis unsigned DiagID = IsDereference ? 1431f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis diag::warn_pointer_indirection_from_incompatible_type : 1432f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis diag::warn_undefined_reinterpret_cast; 1433f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis 1434f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis if (Diags.getDiagnosticLevel(DiagID, Range.getBegin()) == 1435d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie DiagnosticsEngine::Ignored) { 1436f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis return; 1437f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1438f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis 1439f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis QualType SrcTy, DestTy; 1440f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis if (IsDereference) { 1441f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis if (!SrcType->getAs<PointerType>() || !DestType->getAs<PointerType>()) { 1442f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis return; 1443f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1444f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis SrcTy = SrcType->getPointeeType(); 1445f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis DestTy = DestType->getPointeeType(); 1446f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } else { 1447f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis if (!DestType->getAs<ReferenceType>()) { 1448f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis return; 1449f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1450f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis SrcTy = SrcType; 1451f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis DestTy = DestType->getPointeeType(); 1452f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1453f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis 1454f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis // Cast is compatible if the types are the same. 1455f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis if (Context.hasSameUnqualifiedType(DestTy, SrcTy)) { 1456f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis return; 1457f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1458f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis // or one of the types is a char or void type 1459f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis if (DestTy->isAnyCharacterType() || DestTy->isVoidType() || 1460f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis SrcTy->isAnyCharacterType() || SrcTy->isVoidType()) { 1461f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis return; 1462f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1463f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis // or one of the types is a tag type. 14641f8f2d52ff3712770a49f318a687b0c8b0ada9d0Chandler Carruth if (SrcTy->getAs<TagType>() || DestTy->getAs<TagType>()) { 1465f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis return; 1466f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1467f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis 1468575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor // FIXME: Scoped enums? 1469f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis if ((SrcTy->isUnsignedIntegerType() && DestTy->isSignedIntegerType()) || 1470f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis (SrcTy->isSignedIntegerType() && DestTy->isUnsignedIntegerType())) { 1471f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis if (Context.getTypeSize(DestTy) == Context.getTypeSize(SrcTy)) { 1472f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis return; 1473f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1474f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1475f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis 1476f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range; 1477f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis} 1478fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor 147991dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanianstatic void DiagnoseCastOfObjCSEL(Sema &Self, const ExprResult &SrcExpr, 148091dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian QualType DestType) { 148191dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian QualType SrcType = SrcExpr.get()->getType(); 148291dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian if (const PointerType *SrcPtrTy = SrcType->getAs<PointerType>()) 148391dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian if (SrcPtrTy->isObjCSelType()) { 148491dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian QualType DT = DestType; 148591dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian if (isa<PointerType>(DestType)) 148691dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian DT = DestType->getPointeeType(); 148791dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian if (!DT.getUnqualifiedType()->isVoidType()) 148891dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian Self.Diag(SrcExpr.get()->getExprLoc(), 148991dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian diag::warn_cast_pointer_from_sel) 149091dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian << SrcType << DestType << SrcExpr.get()->getSourceRange(); 149191dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian } 149291dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian} 149391dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian 1494429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegleystatic TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, 14959cc11e70031365972424b43f439021d88096b146Sebastian Redl QualType DestType, bool CStyle, 14969cc11e70031365972424b43f439021d88096b146Sebastian Redl const SourceRange &OpRange, 14973c31a39af925fca4a5bb48f03594dba4190e93e6Anders Carlsson unsigned &msg, 14982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall CastKind &Kind) { 1499e39a3894513349908cdb3beba2614e53cb288e6cDouglas Gregor bool IsLValueCast = false; 1500e39a3894513349908cdb3beba2614e53cb288e6cDouglas Gregor 15019cc11e70031365972424b43f439021d88096b146Sebastian Redl DestType = Self.Context.getCanonicalType(DestType); 1502429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley QualType SrcType = SrcExpr.get()->getType(); 15038e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 15048e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor // Is the source an overloaded name? (i.e. &foo) 15051be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5) ... 15061be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor if (SrcType == Self.Context.OverloadTy) { 15076dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall // ... unless foo<int> resolves to an lvalue unambiguously. 15086dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall // TODO: what if this fails because of DiagnoseUseOfDecl or something 15096dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall // like it? 15106dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall ExprResult SingleFunctionExpr = SrcExpr; 15116dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall if (Self.ResolveAndFixSingleFunctionTemplateSpecialization( 15126dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall SingleFunctionExpr, 15131be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor Expr::getValueKindForType(DestType) == VK_RValue // Convert Fun to Ptr 15146dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall ) && SingleFunctionExpr.isUsable()) { 15153fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer SrcExpr = SingleFunctionExpr; 1516429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley SrcType = SrcExpr.get()->getType(); 15176dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall } else { 15181be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor return TC_NotApplicable; 15196dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall } 15201be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor } 15218e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 15226217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) { 15236850fafd27ba804d4d4ca8af404beed5574e3749Richard Smith if (!SrcExpr.get()->isGLValue()) { 15246850fafd27ba804d4d4ca8af404beed5574e3749Richard Smith // Cannot cast non-glvalue to (lvalue or rvalue) reference type. See the 15256850fafd27ba804d4d4ca8af404beed5574e3749Richard Smith // similar comment in const_cast. 15269cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_cxx_cast_rvalue; 15279cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 152826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 15299cc11e70031365972424b43f439021d88096b146Sebastian Redl 1530f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis if (!CStyle) { 1531f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis Self.CheckCompatibleReinterpretCast(SrcType, DestType, 1532f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis /*isDereference=*/false, OpRange); 1533f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis } 1534f4bbbf0aaf741cc7d014e2cf059670a6756f8cbdArgyrios Kyrtzidis 15359cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the 15369cc11e70031365972424b43f439021d88096b146Sebastian Redl // same effect as the conversion *reinterpret_cast<T*>(&x) with the 15379cc11e70031365972424b43f439021d88096b146Sebastian Redl // built-in & and * operators. 1538b464a5b18916b467ed884d07f9e34295d39cec0aArgyrios Kyrtzidis 1539bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis const char *inappropriate = 0; 1540bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis switch (SrcExpr.get()->getObjectKind()) { 1541e5e3d31dab0de2dcbf8da94177da74fe95509d2aArgyrios Kyrtzidis case OK_Ordinary: 1542e5e3d31dab0de2dcbf8da94177da74fe95509d2aArgyrios Kyrtzidis break; 1543bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis case OK_BitField: inappropriate = "bit-field"; break; 1544bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis case OK_VectorComponent: inappropriate = "vector element"; break; 1545bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis case OK_ObjCProperty: inappropriate = "property expression"; break; 1546ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case OK_ObjCSubscript: inappropriate = "container subscripting expression"; 1547ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek break; 1548bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis } 1549bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis if (inappropriate) { 1550bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_reference) 1551bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis << inappropriate << DestType 1552bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis << OpRange << SrcExpr.get()->getSourceRange(); 1553bb29d1ba8b0895e3923c724f49845636f35b4bdeArgyrios Kyrtzidis msg = 0; SrcExpr = ExprError(); 1554b464a5b18916b467ed884d07f9e34295d39cec0aArgyrios Kyrtzidis return TC_NotApplicable; 1555b464a5b18916b467ed884d07f9e34295d39cec0aArgyrios Kyrtzidis } 1556b464a5b18916b467ed884d07f9e34295d39cec0aArgyrios Kyrtzidis 15579cc11e70031365972424b43f439021d88096b146Sebastian Redl // This code does this transformation for the checked types. 15589cc11e70031365972424b43f439021d88096b146Sebastian Redl DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); 15599cc11e70031365972424b43f439021d88096b146Sebastian Redl SrcType = Self.Context.getPointerType(SrcType); 15608e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor 1561e39a3894513349908cdb3beba2614e53cb288e6cDouglas Gregor IsLValueCast = true; 15629cc11e70031365972424b43f439021d88096b146Sebastian Redl } 15639cc11e70031365972424b43f439021d88096b146Sebastian Redl 15649cc11e70031365972424b43f439021d88096b146Sebastian Redl // Canonicalize source for comparison. 15659cc11e70031365972424b43f439021d88096b146Sebastian Redl SrcType = Self.Context.getCanonicalType(SrcType); 15669cc11e70031365972424b43f439021d88096b146Sebastian Redl 15676217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(), 15686217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek *SrcMemPtr = SrcType->getAs<MemberPointerType>(); 15699cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestMemPtr && SrcMemPtr) { 15709cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1" 15719cc11e70031365972424b43f439021d88096b146Sebastian Redl // can be explicitly converted to an rvalue of type "pointer to member 15729cc11e70031365972424b43f439021d88096b146Sebastian Redl // of Y of type T2" if T1 and T2 are both function types or both object 15739cc11e70031365972424b43f439021d88096b146Sebastian Redl // types. 15749cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestMemPtr->getPointeeType()->isFunctionType() != 15759cc11e70031365972424b43f439021d88096b146Sebastian Redl SrcMemPtr->getPointeeType()->isFunctionType()) 15769cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 15779cc11e70031365972424b43f439021d88096b146Sebastian Redl 15789cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away 15799cc11e70031365972424b43f439021d88096b146Sebastian Redl // constness. 15809cc11e70031365972424b43f439021d88096b146Sebastian Redl // A reinterpret_cast followed by a const_cast can, though, so in C-style, 15819cc11e70031365972424b43f439021d88096b146Sebastian Redl // we accept it. 1582f85e193739c953358c865005855253af4f68a497John McCall if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, 1583f85e193739c953358c865005855253af4f68a497John McCall /*CheckObjCLifetime=*/CStyle)) { 1584d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor msg = diag::err_bad_cxx_cast_qualifiers_away; 15859cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 158626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 15879cc11e70031365972424b43f439021d88096b146Sebastian Redl 1588f231df318d8d937adb24f3db23c23c31ab378a82Charles Davis // Don't allow casting between member pointers of different sizes. 1589f231df318d8d937adb24f3db23c23c31ab378a82Charles Davis if (Self.Context.getTypeSize(DestMemPtr) != 1590f231df318d8d937adb24f3db23c23c31ab378a82Charles Davis Self.Context.getTypeSize(SrcMemPtr)) { 1591f231df318d8d937adb24f3db23c23c31ab378a82Charles Davis msg = diag::err_bad_cxx_cast_member_pointer_size; 1592f231df318d8d937adb24f3db23c23c31ab378a82Charles Davis return TC_Failed; 1593f231df318d8d937adb24f3db23c23c31ab378a82Charles Davis } 1594f231df318d8d937adb24f3db23c23c31ab378a82Charles Davis 15959cc11e70031365972424b43f439021d88096b146Sebastian Redl // A valid member pointer cast. 15964d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall assert(!IsLValueCast); 15974d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall Kind = CK_ReinterpretMemberPointer; 15989cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 159926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 160026d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 16019cc11e70031365972424b43f439021d88096b146Sebastian Redl // See below for the enumeral issue. 16029d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) { 16039cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral 16049cc11e70031365972424b43f439021d88096b146Sebastian Redl // type large enough to hold it. A value of std::nullptr_t can be 16059cc11e70031365972424b43f439021d88096b146Sebastian Redl // converted to an integral type; the conversion has the same meaning 16069cc11e70031365972424b43f439021d88096b146Sebastian Redl // and validity as a conversion of (void*)0 to the integral type. 16079cc11e70031365972424b43f439021d88096b146Sebastian Redl if (Self.Context.getTypeSize(SrcType) > 16089cc11e70031365972424b43f439021d88096b146Sebastian Redl Self.Context.getTypeSize(DestType)) { 16099cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_reinterpret_cast_small_int; 16109cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 16119cc11e70031365972424b43f439021d88096b146Sebastian Redl } 16122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_PointerToIntegral; 16139cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 161426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 161526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 16160de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson bool destIsVector = DestType->isVectorType(); 16170de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson bool srcIsVector = SrcType->isVectorType(); 16180de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson if (srcIsVector || destIsVector) { 16199d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor // FIXME: Should this also apply to floating point types? 16209d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor bool srcIsScalar = SrcType->isIntegralType(Self.Context); 16219d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor bool destIsScalar = DestType->isIntegralType(Self.Context); 16220de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson 16230de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson // Check if this is a cast between a vector and something else. 16240de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) && 16250de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson !(srcIsVector && destIsVector)) 16260de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson return TC_NotApplicable; 16270de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson 16280de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson // If both types have the same size, we can successfully cast. 1629f2a5539ea74bc4c4637851395f6bf6f78689f4e6Douglas Gregor if (Self.Context.getTypeSize(SrcType) 1630f2a5539ea74bc4c4637851395f6bf6f78689f4e6Douglas Gregor == Self.Context.getTypeSize(DestType)) { 16312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_BitCast; 16320de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson return TC_Success; 1633f2a5539ea74bc4c4637851395f6bf6f78689f4e6Douglas Gregor } 16340de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson 16350de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson if (destIsScalar) 16360de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size; 16370de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson else if (srcIsScalar) 16380de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size; 16390de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson else 16400de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson msg = diag::err_bad_cxx_cast_vector_to_vector_different_size; 16410de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson 16420de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson return TC_Failed; 16430de51bc6122f6cee0e8081d65dfcc7d500da1c6fAnders Carlsson } 164441f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier 164541f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier if (SrcType == DestType) { 164641f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier // C++ 5.2.10p2 has a note that mentions that, subject to all other 164741f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier // restrictions, a cast to the same type is allowed so long as it does not 164841f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier // cast away constness. In C++98, the intent was not entirely clear here, 164941f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier // since all other paragraphs explicitly forbid casts to the same type. 165041f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier // C++11 clarifies this case with p2. 165141f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier // 165241f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier // The only allowed types are: integral, enumeration, pointer, or 165341f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier // pointer-to-member types. We also won't restrict Obj-C pointers either. 165441f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier Kind = CK_NoOp; 165541f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier TryCastResult Result = TC_NotApplicable; 165641f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier if (SrcType->isIntegralOrEnumerationType() || 165741f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier SrcType->isAnyPointerType() || 165841f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier SrcType->isMemberPointerType() || 165941f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier SrcType->isBlockPointerType()) { 166041f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier Result = TC_Success; 166141f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier } 166241f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier return Result; 166341f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier } 166441f4431f3989ff23029eaf2ad947f07e39fb268cChad Rosier 1665bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor bool destIsPtr = DestType->isAnyPointerType() || 1666bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor DestType->isBlockPointerType(); 1667bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor bool srcIsPtr = SrcType->isAnyPointerType() || 1668bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor SrcType->isBlockPointerType(); 16699cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!destIsPtr && !srcIsPtr) { 16709cc11e70031365972424b43f439021d88096b146Sebastian Redl // Except for std::nullptr_t->integer and lvalue->reference, which are 16719cc11e70031365972424b43f439021d88096b146Sebastian Redl // handled above, at least one of the two arguments must be a pointer. 16729cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 16739cc11e70031365972424b43f439021d88096b146Sebastian Redl } 167426d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 16759d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor if (DestType->isIntegralType(Self.Context)) { 16769cc11e70031365972424b43f439021d88096b146Sebastian Redl assert(srcIsPtr && "One type must be a pointer"); 16779cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.10p4: A pointer can be explicitly converted to any integral 167830aff5b794599fac45cd8716412caf32f29ccb7eFrancois Pichet // type large enough to hold it; except in Microsoft mode, where the 167930aff5b794599fac45cd8716412caf32f29ccb7eFrancois Pichet // integral type size doesn't matter. 168030aff5b794599fac45cd8716412caf32f29ccb7eFrancois Pichet if ((Self.Context.getTypeSize(SrcType) > 168130aff5b794599fac45cd8716412caf32f29ccb7eFrancois Pichet Self.Context.getTypeSize(DestType)) && 16824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !Self.getLangOpts().MicrosoftExt) { 16839cc11e70031365972424b43f439021d88096b146Sebastian Redl msg = diag::err_bad_reinterpret_cast_small_int; 16849cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 16859cc11e70031365972424b43f439021d88096b146Sebastian Redl } 16862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_PointerToIntegral; 16879cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 168826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 168926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 16902ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor if (SrcType->isIntegralOrEnumerationType()) { 16919cc11e70031365972424b43f439021d88096b146Sebastian Redl assert(destIsPtr && "One type must be a pointer"); 16929cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly 16939cc11e70031365972424b43f439021d88096b146Sebastian Redl // converted to a pointer. 1694404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not 1695404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall // necessarily converted to a null pointer value.] 16962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_IntegralToPointer; 16979cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 169826d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl } 169926d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 17009cc11e70031365972424b43f439021d88096b146Sebastian Redl if (!destIsPtr || !srcIsPtr) { 17019cc11e70031365972424b43f439021d88096b146Sebastian Redl // With the valid non-pointer conversions out of the way, we can be even 17029cc11e70031365972424b43f439021d88096b146Sebastian Redl // more stringent. 17039cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_NotApplicable; 1704d93f0ddba0965ded252e228134b30ce30e863fb0Sebastian Redl } 170526d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl 17069cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness. 17079cc11e70031365972424b43f439021d88096b146Sebastian Redl // The C-style cast operator can. 1708f85e193739c953358c865005855253af4f68a497John McCall if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, 1709f85e193739c953358c865005855253af4f68a497John McCall /*CheckObjCLifetime=*/CStyle)) { 1710d4c5f84bbed2ecb5ddd0f0e8316c553b2084772aDouglas Gregor msg = diag::err_bad_cxx_cast_qualifiers_away; 17119cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Failed; 17129cc11e70031365972424b43f439021d88096b146Sebastian Redl } 1713bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor 1714bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor // Cannot convert between block pointers and Objective-C object pointers. 1715bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) || 1716bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType())) 1717bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor return TC_NotApplicable; 1718bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor 17191d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall if (IsLValueCast) { 17201d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall Kind = CK_LValueBitCast; 17211d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall } else if (DestType->isObjCObjectPointerType()) { 1722dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall Kind = Self.PrepareCastToObjCObjectPointer(SrcExpr); 17231d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall } else if (DestType->isBlockPointerType()) { 17241d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall if (!SrcType->isBlockPointerType()) { 17251d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall Kind = CK_AnyPointerToBlockPointerCast; 17261d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall } else { 17271d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall Kind = CK_BitCast; 17281d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall } 17291d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall } else { 17301d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall Kind = CK_BitCast; 17311d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall } 17321d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall 1733bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor // Any pointer can be cast to an Objective-C pointer type with a C-style 1734bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor // cast. 173592ef5d75720c032808181133e4d7c5f82230237eFariborz Jahanian if (CStyle && DestType->isObjCObjectPointerType()) { 173692ef5d75720c032808181133e4d7c5f82230237eFariborz Jahanian return TC_Success; 173792ef5d75720c032808181133e4d7c5f82230237eFariborz Jahanian } 173891dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian if (CStyle) 173991dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType); 174091dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian 17419cc11e70031365972424b43f439021d88096b146Sebastian Redl // Not casting away constness, so the only remaining check is for compatible 17429cc11e70031365972424b43f439021d88096b146Sebastian Redl // pointer categories. 17439cc11e70031365972424b43f439021d88096b146Sebastian Redl 17449cc11e70031365972424b43f439021d88096b146Sebastian Redl if (SrcType->isFunctionPointerType()) { 17459cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestType->isFunctionPointerType()) { 17469cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.10p6: A pointer to a function can be explicitly converted to 17479cc11e70031365972424b43f439021d88096b146Sebastian Redl // a pointer to a function of a different type. 17489cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 17499cc11e70031365972424b43f439021d88096b146Sebastian Redl } 17509cc11e70031365972424b43f439021d88096b146Sebastian Redl 17519cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to 17529cc11e70031365972424b43f439021d88096b146Sebastian Redl // an object type or vice versa is conditionally-supported. 17539cc11e70031365972424b43f439021d88096b146Sebastian Redl // Compilers support it in C++03 too, though, because it's necessary for 17549cc11e70031365972424b43f439021d88096b146Sebastian Redl // casting the return value of dlsym() and GetProcAddress(). 17559cc11e70031365972424b43f439021d88096b146Sebastian Redl // FIXME: Conditionally-supported behavior should be configurable in the 17569cc11e70031365972424b43f439021d88096b146Sebastian Redl // TargetInfo or similar. 1757ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith Self.Diag(OpRange.getBegin(), 17584e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie Self.getLangOpts().CPlusPlus0x ? 1759ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj) 1760ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith << OpRange; 17619cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 17629cc11e70031365972424b43f439021d88096b146Sebastian Redl } 17639cc11e70031365972424b43f439021d88096b146Sebastian Redl 17649cc11e70031365972424b43f439021d88096b146Sebastian Redl if (DestType->isFunctionPointerType()) { 17659cc11e70031365972424b43f439021d88096b146Sebastian Redl // See above. 1766ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith Self.Diag(OpRange.getBegin(), 17674e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie Self.getLangOpts().CPlusPlus0x ? 1768ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj) 1769ebaf0e6ab743394dda086a01b457838cb6e589a8Richard Smith << OpRange; 17709cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 17719cc11e70031365972424b43f439021d88096b146Sebastian Redl } 1772bf9fb88e29e565061c1e91d790af6b43c25915a7Douglas Gregor 17739cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ 5.2.10p7: A pointer to an object can be explicitly converted to 17749cc11e70031365972424b43f439021d88096b146Sebastian Redl // a pointer to an object of different type. 17759cc11e70031365972424b43f439021d88096b146Sebastian Redl // Void pointers are not specified, but supported by every compiler out there. 17769cc11e70031365972424b43f439021d88096b146Sebastian Redl // So we finish by allowing everything that remains - it's got to be two 17779cc11e70031365972424b43f439021d88096b146Sebastian Redl // object pointers. 17789cc11e70031365972424b43f439021d88096b146Sebastian Redl return TC_Success; 177979ab2c8104ef5df233d271560ccc734836738e56John McCall} 17809cc11e70031365972424b43f439021d88096b146Sebastian Redl 17816dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redlvoid CastOperation::CheckCXXCStyleCast(bool FunctionalStyle, 17826dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl bool ListInitialization) { 1783a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // Handle placeholders. 1784a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (isPlaceholder()) { 1785a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // C-style casts can resolve __unknown_any types. 1786a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (claimPlaceholder(BuiltinType::UnknownAny)) { 1787a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType, 1788a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr.get(), Kind, 1789a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall ValueKind, BasePath); 1790a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 1791a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 1792b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 1793a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall checkNonOverloadPlaceholders(); 1794a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (SrcExpr.isInvalid()) 1795a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 17964919dfd54e2296ca997e3d1c9dab85976bba8e95John McCall } 1797a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 1798a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". 17999cc11e70031365972424b43f439021d88096b146Sebastian Redl // This test is outside everything else because it's the only case where 18009cc11e70031365972424b43f439021d88096b146Sebastian Redl // a non-lvalue-reference target type does not lead to decay. 1801b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (DestType->isVoidType()) { 1802fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall Kind = CK_ToVoid; 1803fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall 1804a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (claimPlaceholder(BuiltinType::Overload)) { 18056dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall Self.ResolveAndFixSingleFunctionTemplateSpecialization( 18066dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall SrcExpr, /* Decay Function to ptr */ false, 1807b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall /* Complain */ true, DestRange, DestType, 1808fadb53b351977ca7f99a9a613596cba6531979a3Douglas Gregor diag::err_bad_cstyle_cast_overload); 1809b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (SrcExpr.isInvalid()) 1810b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall return; 18111be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor } 18129cc11e70031365972424b43f439021d88096b146Sebastian Redl 1813a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); 1814a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (SrcExpr.isInvalid()) 1815b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall return; 1816d06fea8580658470f92fb5d0d3d7ab5b475728dcAnton Yartsev 1817b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall return; 1818b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall } 18199b4b9d6d221ab804f5b65fec6c8661be4d5c2b84John McCall 18209cc11e70031365972424b43f439021d88096b146Sebastian Redl // If the type is dependent, we won't do any other semantic analysis now. 1821b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (DestType->isDependentType() || SrcExpr.get()->isTypeDependent()) { 1822b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall assert(Kind == CK_Dependent); 1823b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall return; 1824daa8e4e888758d55a7a759dd4a91b83921cef222John McCall } 18255b4a40a9d740d9025c16fa2f34c80a96906051c3Benjamin Kramer 18266dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall if (ValueKind == VK_RValue && !DestType->isRecordType() && 18276dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall !isPlaceholder(BuiltinType::Overload)) { 1828b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); 1829b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (SrcExpr.isInvalid()) 1830b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall return; 1831429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley } 18329cc11e70031365972424b43f439021d88096b146Sebastian Redl 1833fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall // AltiVec vector initialization with a single literal. 1834b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (const VectorType *vecTy = DestType->getAs<VectorType>()) 1835fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall if (vecTy->getVectorKind() == VectorType::AltiVecVector 1836b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall && (SrcExpr.get()->getType()->isIntegerType() 1837b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall || SrcExpr.get()->getType()->isFloatingType())) { 1838fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall Kind = CK_VectorSplat; 1839b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall return; 1840fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall } 1841fb8721ce4c6fef3739b1cbd1e38e3f1949462033John McCall 18429cc11e70031365972424b43f439021d88096b146Sebastian Redl // C++ [expr.cast]p5: The conversions performed by 18439cc11e70031365972424b43f439021d88096b146Sebastian Redl // - a const_cast, 18449cc11e70031365972424b43f439021d88096b146Sebastian Redl // - a static_cast, 18459cc11e70031365972424b43f439021d88096b146Sebastian Redl // - a static_cast followed by a const_cast, 18469cc11e70031365972424b43f439021d88096b146Sebastian Redl // - a reinterpret_cast, or 18479cc11e70031365972424b43f439021d88096b146Sebastian Redl // - a reinterpret_cast followed by a const_cast, 18489cc11e70031365972424b43f439021d88096b146Sebastian Redl // can be performed using the cast notation of explicit type conversion. 18499cc11e70031365972424b43f439021d88096b146Sebastian Redl // [...] If a conversion can be interpreted in more than one of the ways 18509cc11e70031365972424b43f439021d88096b146Sebastian Redl // listed above, the interpretation that appears first in the list is used, 18519cc11e70031365972424b43f439021d88096b146Sebastian Redl // even if a cast resulting from that interpretation is ill-formed. 18529cc11e70031365972424b43f439021d88096b146Sebastian Redl // In plain language, this means trying a const_cast ... 18539cc11e70031365972424b43f439021d88096b146Sebastian Redl unsigned msg = diag::err_bad_cxx_cast_generic; 1854b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall TryCastResult tcr = TryConstCast(Self, SrcExpr.get(), DestType, 1855b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall /*CStyle*/true, msg); 1856da921fd19fd496494365f9f2325c338e66216709Anders Carlsson if (tcr == TC_Success) 18572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Kind = CK_NoOp; 1858da921fd19fd496494365f9f2325c338e66216709Anders Carlsson 1859f85e193739c953358c865005855253af4f68a497John McCall Sema::CheckedConversionKind CCK 1860f85e193739c953358c865005855253af4f68a497John McCall = FunctionalStyle? Sema::CCK_FunctionalCast 1861f85e193739c953358c865005855253af4f68a497John McCall : Sema::CCK_CStyleCast; 18629cc11e70031365972424b43f439021d88096b146Sebastian Redl if (tcr == TC_NotApplicable) { 18639cc11e70031365972424b43f439021d88096b146Sebastian Redl // ... or if that is not possible, a static_cast, ignoring const, ... 1864b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall tcr = TryStaticCast(Self, SrcExpr, DestType, CCK, OpRange, 18656dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl msg, Kind, BasePath, ListInitialization); 1866b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (SrcExpr.isInvalid()) 1867b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall return; 1868b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 18699cc11e70031365972424b43f439021d88096b146Sebastian Redl if (tcr == TC_NotApplicable) { 18709cc11e70031365972424b43f439021d88096b146Sebastian Redl // ... and finally a reinterpret_cast, ignoring const. 1871b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall tcr = TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/true, 1872b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall OpRange, msg, Kind); 1873b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (SrcExpr.isInvalid()) 1874b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall return; 18759cc11e70031365972424b43f439021d88096b146Sebastian Redl } 18769cc11e70031365972424b43f439021d88096b146Sebastian Redl } 18779cc11e70031365972424b43f439021d88096b146Sebastian Redl 18784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Self.getLangOpts().ObjCAutoRefCount && tcr == TC_Success) 1879b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall checkObjCARCConversion(CCK); 1880f85e193739c953358c865005855253af4f68a497John McCall 188143328e91627382870d51b5d84e08873cc0c970ebNick Lewycky if (tcr != TC_Success && msg != 0) { 1882b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { 18838e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor DeclAccessPair Found; 1884b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), 1885b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall DestType, 1886b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall /*Complain*/ true, 18878e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor Found); 18881be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor 188932ac00d05b3e12bb155f95dfcede7e66ab26c234Richard Trieu assert(!Fn && "cast failed but able to resolve overload expression!!"); 189043328e91627382870d51b5d84e08873cc0c970ebNick Lewycky (void)Fn; 189179ab2c8104ef5df233d271560ccc734836738e56John McCall 189243328e91627382870d51b5d84e08873cc0c970ebNick Lewycky } else { 1893b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall diagnoseBadCast(Self, msg, (FunctionalStyle ? CT_Functional : CT_CStyle), 189420ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl OpRange, SrcExpr.get(), DestType, ListInitialization); 18958e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor } 1896b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall } else if (Kind == CK_BitCast) { 1897b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall checkCastAlign(); 18988e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor } 18999cc11e70031365972424b43f439021d88096b146Sebastian Redl 1900b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall // Clear out SrcExpr if there was a fatal error. 1901429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (tcr != TC_Success) 1902b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall SrcExpr = ExprError(); 1903b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall} 1904b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 1905bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian/// DiagnoseBadFunctionCast - Warn whenever a function call is cast to a 1906bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian/// non-matching type. Such as enum function call to int, int call to 1907bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian/// pointer; etc. Cast to 'void' is an exception. 1908bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanianstatic void DiagnoseBadFunctionCast(Sema &Self, const ExprResult &SrcExpr, 1909bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian QualType DestType) { 1910bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian if (Self.Diags.getDiagnosticLevel(diag::warn_bad_function_cast, 1911bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian SrcExpr.get()->getExprLoc()) 1912bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian == DiagnosticsEngine::Ignored) 1913bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian return; 1914bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian 1915bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian if (!isa<CallExpr>(SrcExpr.get())) 1916bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian return; 1917bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian 1918bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian QualType SrcType = SrcExpr.get()->getType(); 1919bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian if (DestType.getUnqualifiedType()->isVoidType()) 1920bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian return; 1921bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian if ((SrcType->isAnyPointerType() || SrcType->isBlockPointerType()) 1922bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian && (DestType->isAnyPointerType() || DestType->isBlockPointerType())) 1923bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian return; 1924bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian if (SrcType->isIntegerType() && DestType->isIntegerType() && 1925bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian (SrcType->isBooleanType() == DestType->isBooleanType()) && 1926bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian (SrcType->isEnumeralType() == DestType->isEnumeralType())) 1927bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian return; 1928bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian if (SrcType->isRealFloatingType() && DestType->isRealFloatingType()) 1929bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian return; 1930bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian if (SrcType->isEnumeralType() && DestType->isEnumeralType()) 1931bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian return; 1932bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian if (SrcType->isComplexType() && DestType->isComplexType()) 1933bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian return; 1934bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian if (SrcType->isComplexIntegerType() && DestType->isComplexIntegerType()) 1935bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian return; 1936bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian 1937bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian Self.Diag(SrcExpr.get()->getExprLoc(), 1938bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian diag::warn_bad_function_cast) 1939bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian << SrcType << DestType << SrcExpr.get()->getSourceRange(); 1940bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian} 1941bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian 1942a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall/// Check the semantics of a C-style cast operation, in C. 1943a180f04c091bf3ede4fe292ba6a29d61da09e936John McCallvoid CastOperation::CheckCStyleCast() { 19444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(!Self.getLangOpts().CPlusPlus); 1945a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 19465acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall // C-style casts can resolve __unknown_any types. 19475acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall if (claimPlaceholder(BuiltinType::UnknownAny)) { 19485acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType, 19495acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall SrcExpr.get(), Kind, 19505acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall ValueKind, BasePath); 19515acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall return; 19525acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall } 1953a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 1954a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // C99 6.5.4p2: the cast type needs to be void or scalar and the expression 1955a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // type needs to be scalar. 1956a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (DestType->isVoidType()) { 1957a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // We don't necessarily do lvalue-to-rvalue conversions on this. 1958a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); 1959a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (SrcExpr.isInvalid()) 1960a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 1961a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 1962a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // Cast to void allows any expr type. 1963a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Kind = CK_ToVoid; 1964a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 1965a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 1966a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 1967a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); 1968a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (SrcExpr.isInvalid()) 1969a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 1970a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall QualType SrcType = SrcExpr.get()->getType(); 19717a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall 19725acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall assert(!SrcType->isPlaceholderType()); 1973a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 1974a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (Self.RequireCompleteType(OpRange.getBegin(), DestType, 1975a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall diag::err_typecheck_cast_to_incomplete)) { 1976a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 1977a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 1978a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 1979a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 1980a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (!DestType->isScalarType() && !DestType->isVectorType()) { 1981a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall const RecordType *DestRecordTy = DestType->getAs<RecordType>(); 1982a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 1983a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (DestRecordTy && Self.Context.hasSameUnqualifiedType(DestType, SrcType)){ 1984a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // GCC struct/union extension: allow cast to self. 1985a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_nonscalar) 1986a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall << DestType << SrcExpr.get()->getSourceRange(); 1987a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Kind = CK_NoOp; 1988a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 1989a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 1990a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 1991a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // GCC's cast to union extension. 1992a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (DestRecordTy && DestRecordTy->getDecl()->isUnion()) { 1993a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall RecordDecl *RD = DestRecordTy->getDecl(); 1994a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall RecordDecl::field_iterator Field, FieldEnd; 1995a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall for (Field = RD->field_begin(), FieldEnd = RD->field_end(); 1996a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Field != FieldEnd; ++Field) { 1997a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (Self.Context.hasSameUnqualifiedType(Field->getType(), SrcType) && 1998a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall !Field->isUnnamedBitfield()) { 1999a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_to_union) 2000a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall << SrcExpr.get()->getSourceRange(); 2001a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall break; 2002a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2003a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2004a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (Field == FieldEnd) { 2005a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(OpRange.getBegin(), diag::err_typecheck_cast_to_union_no_type) 2006a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall << SrcType << SrcExpr.get()->getSourceRange(); 2007a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 2008a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2009a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2010a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Kind = CK_ToUnion; 2011a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2012a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2013a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2014a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // Reject any other conversions to non-scalar types. 2015a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(OpRange.getBegin(), diag::err_typecheck_cond_expect_scalar) 2016a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall << DestType << SrcExpr.get()->getSourceRange(); 2017a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 2018a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2019a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2020a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2021a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // The type we're casting to is known to be a scalar or vector. 2022a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2023a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // Require the operand to be a scalar or vector. 2024a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (!SrcType->isScalarType() && !SrcType->isVectorType()) { 2025a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(SrcExpr.get()->getExprLoc(), 2026a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall diag::err_typecheck_expect_scalar_operand) 2027a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall << SrcType << SrcExpr.get()->getSourceRange(); 2028a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 2029a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2030a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2031a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2032a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (DestType->isExtVectorType()) { 2033a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = Self.CheckExtVectorCast(OpRange, DestType, SrcExpr.take(), Kind); 2034a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2035a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2036a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2037a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (const VectorType *DestVecTy = DestType->getAs<VectorType>()) { 2038a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (DestVecTy->getVectorKind() == VectorType::AltiVecVector && 2039a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall (SrcType->isIntegerType() || SrcType->isFloatingType())) { 2040a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Kind = CK_VectorSplat; 2041a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } else if (Self.CheckVectorCast(OpRange, DestType, SrcType, Kind)) { 2042a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 2043a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2044a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2045a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2046a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2047a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (SrcType->isVectorType()) { 2048a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (Self.CheckVectorCast(OpRange, SrcType, DestType, Kind)) 2049a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 2050a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2051a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2052a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2053a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // The source and target types are both scalars, i.e. 2054a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // - arithmetic types (fundamental, enum, and complex) 2055a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // - all kinds of pointers 2056a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // Note that member pointers were filtered out with C++, above. 2057a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2058a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (isa<ObjCSelectorExpr>(SrcExpr.get())) { 2059a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_selector_expr); 2060a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 2061a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2062a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2063a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2064a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // If either type is a pointer, the other type has to be either an 2065a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // integer or a pointer. 2066a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (!DestType->isArithmeticType()) { 2067a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (!SrcType->isIntegralType(Self.Context) && SrcType->isArithmeticType()) { 2068a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(SrcExpr.get()->getExprLoc(), 2069a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall diag::err_cast_pointer_from_non_pointer_int) 2070a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall << SrcType << SrcExpr.get()->getSourceRange(); 2071a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 2072a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2073a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2074a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } else if (!SrcType->isArithmeticType()) { 2075a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (!DestType->isIntegralType(Self.Context) && 2076a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall DestType->isArithmeticType()) { 2077a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(SrcExpr.get()->getLocStart(), 2078a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall diag::err_cast_pointer_to_non_pointer_int) 2079f7ce19400ba4d7e7f17f804490b76035de6fdf5eAbramo Bagnara << DestType << SrcExpr.get()->getSourceRange(); 2080a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 2081a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2082a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2083a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2084a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2085a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall // ARC imposes extra restrictions on casts. 20864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Self.getLangOpts().ObjCAutoRefCount) { 2087a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall checkObjCARCConversion(Sema::CCK_CStyleCast); 2088a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (SrcExpr.isInvalid()) 2089a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2090a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2091a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (const PointerType *CastPtr = DestType->getAs<PointerType>()) { 2092a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (const PointerType *ExprPtr = SrcType->getAs<PointerType>()) { 2093a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); 2094a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); 2095a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (CastPtr->getPointeeType()->isObjCLifetimeType() && 2096a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall ExprPtr->getPointeeType()->isObjCLifetimeType() && 2097a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { 2098a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(SrcExpr.get()->getLocStart(), 2099a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall diag::err_typecheck_incompatible_ownership) 2100a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall << SrcType << DestType << Sema::AA_Casting 2101a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall << SrcExpr.get()->getSourceRange(); 2102a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2103a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2104a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2105a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2106a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall else if (!Self.CheckObjCARCUnavailableWeakConversion(DestType, SrcType)) { 2107a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Self.Diag(SrcExpr.get()->getLocStart(), 2108a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall diag::err_arc_convesion_of_weak_unavailable) 2109a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall << 1 << SrcType << DestType << SrcExpr.get()->getSourceRange(); 2110a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SrcExpr = ExprError(); 2111a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2112a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2113a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 211491dd9df4435f8b14e599d15f14c4bad77bc4e71fFariborz Jahanian DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType); 2115bbb8afd7f0bdac635583e07c72a9fcf905aa7c4cFariborz Jahanian DiagnoseBadFunctionCast(Self, SrcExpr, DestType); 2116a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Kind = Self.PrepareScalarCast(SrcExpr, DestType); 2117a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (SrcExpr.isInvalid()) 2118a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall return; 2119a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2120a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall if (Kind == CK_BitCast) 2121a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall checkCastAlign(); 2122a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall} 2123a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2124a180f04c091bf3ede4fe292ba6a29d61da09e936John McCallExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc, 2125a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall TypeSourceInfo *CastTypeInfo, 2126a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall SourceLocation RPLoc, 2127a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Expr *CastExpr) { 2128b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall CastOperation Op(*this, CastTypeInfo->getType(), CastExpr); 2129b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange(); 2130b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Op.OpRange = SourceRange(LPLoc, CastExpr->getLocEnd()); 2131b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 21324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) { 21336dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl Op.CheckCXXCStyleCast(/*FunctionalStyle=*/ false, 21346dc00f6e98a00bd1c332927c3e04918d7e8b0d4fSebastian Redl isa<InitListExpr>(CastExpr)); 2135a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } else { 2136a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall Op.CheckCStyleCast(); 2137a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall } 2138a180f04c091bf3ede4fe292ba6a29d61da09e936John McCall 2139b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (Op.SrcExpr.isInvalid()) 2140b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall return ExprError(); 2141b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 21425acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall return Op.complete(CStyleCastExpr::Create(Context, Op.ResultType, 21435acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Op.ValueKind, Op.Kind, Op.SrcExpr.take(), 21445acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall &Op.BasePath, CastTypeInfo, LPLoc, RPLoc)); 2145b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall} 2146b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 2147b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCallExprResult Sema::BuildCXXFunctionalCastExpr(TypeSourceInfo *CastTypeInfo, 2148b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall SourceLocation LPLoc, 2149b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Expr *CastExpr, 2150b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall SourceLocation RPLoc) { 215120ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl assert(LPLoc.isValid() && "List-initialization shouldn't get here."); 2152b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall CastOperation Op(*this, CastTypeInfo->getType(), CastExpr); 2153b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange(); 2154b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Op.OpRange = SourceRange(Op.DestRange.getBegin(), CastExpr->getLocEnd()); 2155b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 215620ff0e2d74c188cb3fd4ec3dead41a80a37c0202Sebastian Redl Op.CheckCXXCStyleCast(/*FunctionalStyle=*/true, /*ListInit=*/false); 2157b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall if (Op.SrcExpr.isInvalid()) 2158429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return ExprError(); 2159a770a4daf77037b5526dba1c447d61acf0aba01aDaniel Jasper 2160a770a4daf77037b5526dba1c447d61acf0aba01aDaniel Jasper if (CXXConstructExpr *ConstructExpr = dyn_cast<CXXConstructExpr>(Op.SrcExpr.get())) 2161a770a4daf77037b5526dba1c447d61acf0aba01aDaniel Jasper ConstructExpr->setParenRange(SourceRange(LPLoc, RPLoc)); 2162429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley 21635acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall return Op.complete(CXXFunctionalCastExpr::Create(Context, Op.ResultType, 21645acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Op.ValueKind, CastTypeInfo, Op.DestRange.getBegin(), 21655acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall Op.Kind, Op.SrcExpr.take(), &Op.BasePath, RPLoc)); 216626d85b197257bfa15cd8b10dfef9e741e19c4fc5Sebastian Redl} 2167