John McCall | d8d3ced | 2011-10-11 17:38:55 +0000 | [diff] [blame] | 1 | //===--- SemaCast.cpp - Semantic Analysis for Casts -----------------------===// |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
John McCall | d8d3ced | 2011-10-11 17:38:55 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for cast expressions, including |
| 11 | // 1) C-style casts like '(int) x' |
| 12 | // 2) C++ functional casts like 'int(x)' |
| 13 | // 3) C++ named casts like 'static_cast<int>(x)' |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
| 21 | #include "clang/AST/ExprObjC.h" |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Initialization.h" |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallVector.h" |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 26 | #include <set> |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 30 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 31 | enum TryCastResult { |
| 32 | TC_NotApplicable, ///< The cast method is not applicable. |
| 33 | TC_Success, ///< The cast method is appropriate and successful. |
| 34 | TC_Failed ///< The cast method is appropriate, but failed. A |
| 35 | ///< diagnostic has been emitted. |
| 36 | }; |
| 37 | |
| 38 | enum CastType { |
| 39 | CT_Const, ///< const_cast |
| 40 | CT_Static, ///< static_cast |
| 41 | CT_Reinterpret, ///< reinterpret_cast |
| 42 | CT_Dynamic, ///< dynamic_cast |
| 43 | CT_CStyle, ///< (Type)expr |
| 44 | CT_Functional ///< Type(expr) |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 47 | namespace { |
| 48 | struct CastOperation { |
| 49 | CastOperation(Sema &S, QualType destType, ExprResult src) |
| 50 | : Self(S), SrcExpr(src), DestType(destType), |
| 51 | ResultType(destType.getNonLValueExprType(S.Context)), |
| 52 | ValueKind(Expr::getValueKindForType(destType)), |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 53 | Kind(CK_Dependent), IsARCUnbridgedCast(false) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 54 | |
| 55 | if (const BuiltinType *placeholder = |
| 56 | src.get()->getType()->getAsPlaceholderType()) { |
| 57 | PlaceholderKind = placeholder->getKind(); |
| 58 | } else { |
| 59 | PlaceholderKind = (BuiltinType::Kind) 0; |
| 60 | } |
| 61 | } |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 62 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 63 | Sema &Self; |
| 64 | ExprResult SrcExpr; |
| 65 | QualType DestType; |
| 66 | QualType ResultType; |
| 67 | ExprValueKind ValueKind; |
| 68 | CastKind Kind; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 69 | BuiltinType::Kind PlaceholderKind; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 70 | CXXCastPath BasePath; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 71 | bool IsARCUnbridgedCast; |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 72 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 73 | SourceRange OpRange; |
| 74 | SourceRange DestRange; |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 75 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 76 | // Top-level semantics-checking routines. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 77 | void CheckConstCast(); |
| 78 | void CheckReinterpretCast(); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 79 | void CheckStaticCast(); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 80 | void CheckDynamicCast(); |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 81 | void CheckCXXCStyleCast(bool FunctionalCast, bool ListInitialization); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 82 | void CheckCStyleCast(); |
| 83 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 84 | /// Complete an apparently-successful cast operation that yields |
| 85 | /// the given expression. |
| 86 | ExprResult complete(CastExpr *castExpr) { |
| 87 | // If this is an unbridged cast, wrap the result in an implicit |
| 88 | // cast that yields the unbridged-cast placeholder type. |
| 89 | if (IsARCUnbridgedCast) { |
| 90 | castExpr = ImplicitCastExpr::Create(Self.Context, |
| 91 | Self.Context.ARCUnbridgedCastTy, |
| 92 | CK_Dependent, castExpr, 0, |
| 93 | castExpr->getValueKind()); |
| 94 | } |
| 95 | return Self.Owned(castExpr); |
| 96 | } |
| 97 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 98 | // Internal convenience methods. |
| 99 | |
| 100 | /// Try to handle the given placeholder expression kind. Return |
| 101 | /// true if the source expression has the appropriate placeholder |
| 102 | /// kind. A placeholder can only be claimed once. |
| 103 | bool claimPlaceholder(BuiltinType::Kind K) { |
| 104 | if (PlaceholderKind != K) return false; |
| 105 | |
| 106 | PlaceholderKind = (BuiltinType::Kind) 0; |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | bool isPlaceholder() const { |
| 111 | return PlaceholderKind != 0; |
| 112 | } |
| 113 | bool isPlaceholder(BuiltinType::Kind K) const { |
| 114 | return PlaceholderKind == K; |
| 115 | } |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 116 | |
| 117 | void checkCastAlign() { |
| 118 | Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange); |
| 119 | } |
| 120 | |
| 121 | void checkObjCARCConversion(Sema::CheckedConversionKind CCK) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 122 | assert(Self.getLangOpts().ObjCAutoRefCount); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 123 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 124 | Expr *src = SrcExpr.get(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 125 | if (Self.CheckObjCARCConversion(OpRange, DestType, src, CCK) == |
| 126 | Sema::ACR_unbridged) |
| 127 | IsARCUnbridgedCast = true; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 128 | SrcExpr = src; |
| 129 | } |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 130 | |
| 131 | /// Check for and handle non-overload placeholder expressions. |
| 132 | void checkNonOverloadPlaceholders() { |
| 133 | if (!isPlaceholder() || isPlaceholder(BuiltinType::Overload)) |
| 134 | return; |
| 135 | |
| 136 | SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.take()); |
| 137 | if (SrcExpr.isInvalid()) |
| 138 | return; |
| 139 | PlaceholderKind = (BuiltinType::Kind) 0; |
| 140 | } |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 141 | }; |
| 142 | } |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 143 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 144 | static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, |
| 145 | bool CheckCVR, bool CheckObjCLifetime); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 146 | |
| 147 | // The Try functions attempt a specific way of casting. If they succeed, they |
| 148 | // return TC_Success. If their way of casting is not appropriate for the given |
| 149 | // arguments, they return TC_NotApplicable and *may* set diag to a diagnostic |
| 150 | // to emit if no other way succeeds. If their way of casting is appropriate but |
| 151 | // fails, they return TC_Failed and *must* set diag; they can set it to 0 if |
| 152 | // they emit a specialized diagnostic. |
| 153 | // All diagnostics returned by these functions must expect the same three |
| 154 | // arguments: |
| 155 | // %0: Cast Type (a value from the CastType enumeration) |
| 156 | // %1: Source Type |
| 157 | // %2: Destination Type |
| 158 | static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, |
Douglas Gregor | 8ec14e6 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 159 | QualType DestType, bool CStyle, |
| 160 | CastKind &Kind, |
Douglas Gregor | 88b22a4 | 2011-01-25 16:13:26 +0000 | [diff] [blame] | 161 | CXXCastPath &BasePath, |
| 162 | unsigned &msg); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 163 | static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 164 | QualType DestType, bool CStyle, |
| 165 | const SourceRange &OpRange, |
| 166 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 167 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 168 | CXXCastPath &BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 169 | static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType, |
| 170 | QualType DestType, bool CStyle, |
| 171 | const SourceRange &OpRange, |
Anders Carlsson | 95c5d8a | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 172 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 173 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 174 | CXXCastPath &BasePath); |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 175 | static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType, |
| 176 | CanQualType DestType, bool CStyle, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 177 | const SourceRange &OpRange, |
| 178 | QualType OrigSrcType, |
Anders Carlsson | 95c5d8a | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 179 | QualType OrigDestType, unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 180 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 181 | CXXCastPath &BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 182 | static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 183 | QualType SrcType, |
| 184 | QualType DestType,bool CStyle, |
| 185 | const SourceRange &OpRange, |
| 186 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 187 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 188 | CXXCastPath &BasePath); |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 189 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 190 | static TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 191 | QualType DestType, |
| 192 | Sema::CheckedConversionKind CCK, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 193 | const SourceRange &OpRange, |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 194 | unsigned &msg, CastKind &Kind, |
| 195 | bool ListInitialization); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 196 | static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 197 | QualType DestType, |
| 198 | Sema::CheckedConversionKind CCK, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 199 | const SourceRange &OpRange, |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 200 | unsigned &msg, CastKind &Kind, |
| 201 | CXXCastPath &BasePath, |
| 202 | bool ListInitialization); |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 203 | static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr, |
| 204 | QualType DestType, bool CStyle, |
| 205 | unsigned &msg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 206 | static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 207 | QualType DestType, bool CStyle, |
| 208 | const SourceRange &OpRange, |
Anders Carlsson | 3c31a39 | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 209 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 210 | CastKind &Kind); |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 211 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 212 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 213 | /// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 214 | ExprResult |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 215 | Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 216 | SourceLocation LAngleBracketLoc, Declarator &D, |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 217 | SourceLocation RAngleBracketLoc, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 218 | SourceLocation LParenLoc, Expr *E, |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 219 | SourceLocation RParenLoc) { |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 220 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 221 | assert(!D.isInvalidType()); |
| 222 | |
| 223 | TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, E->getType()); |
| 224 | if (D.isInvalidType()) |
| 225 | return ExprError(); |
| 226 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 227 | if (getLangOpts().CPlusPlus) { |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 228 | // Check that there are no default arguments (C++ only). |
| 229 | CheckExtraCXXDefaultArguments(D); |
| 230 | } |
| 231 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 232 | return BuildCXXNamedCast(OpLoc, Kind, TInfo, E, |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 233 | SourceRange(LAngleBracketLoc, RAngleBracketLoc), |
| 234 | SourceRange(LParenLoc, RParenLoc)); |
| 235 | } |
| 236 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 237 | ExprResult |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 238 | Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 239 | TypeSourceInfo *DestTInfo, Expr *E, |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 240 | SourceRange AngleBrackets, SourceRange Parens) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 241 | ExprResult Ex = Owned(E); |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 242 | QualType DestType = DestTInfo->getType(); |
| 243 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 244 | // If the type is dependent, we won't do the semantic analysis now. |
| 245 | // FIXME: should we check this in a more fine-grained manner? |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 246 | bool TypeDependent = DestType->isDependentType() || Ex.get()->isTypeDependent(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 247 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 248 | CastOperation Op(*this, DestType, E); |
| 249 | Op.OpRange = SourceRange(OpLoc, Parens.getEnd()); |
| 250 | Op.DestRange = AngleBrackets; |
John McCall | a21e06c | 2010-11-26 10:57:22 +0000 | [diff] [blame] | 251 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 252 | switch (Kind) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 253 | default: llvm_unreachable("Unknown C++ cast!"); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 254 | |
| 255 | case tok::kw_const_cast: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 256 | if (!TypeDependent) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 257 | Op.CheckConstCast(); |
| 258 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 259 | return ExprError(); |
| 260 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 261 | return Op.complete(CXXConstCastExpr::Create(Context, Op.ResultType, |
| 262 | Op.ValueKind, Op.SrcExpr.take(), DestTInfo, |
Fariborz Jahanian | f799ae1 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 263 | OpLoc, Parens.getEnd(), |
| 264 | AngleBrackets)); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 265 | |
Anders Carlsson | 714179b | 2009-08-02 19:07:59 +0000 | [diff] [blame] | 266 | case tok::kw_dynamic_cast: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 267 | if (!TypeDependent) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 268 | Op.CheckDynamicCast(); |
| 269 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 270 | return ExprError(); |
| 271 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 272 | return Op.complete(CXXDynamicCastExpr::Create(Context, Op.ResultType, |
| 273 | Op.ValueKind, Op.Kind, Op.SrcExpr.take(), |
| 274 | &Op.BasePath, DestTInfo, |
Fariborz Jahanian | f799ae1 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 275 | OpLoc, Parens.getEnd(), |
| 276 | AngleBrackets)); |
Anders Carlsson | 714179b | 2009-08-02 19:07:59 +0000 | [diff] [blame] | 277 | } |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 278 | case tok::kw_reinterpret_cast: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 279 | if (!TypeDependent) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 280 | Op.CheckReinterpretCast(); |
| 281 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 282 | return ExprError(); |
| 283 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 284 | return Op.complete(CXXReinterpretCastExpr::Create(Context, Op.ResultType, |
| 285 | Op.ValueKind, Op.Kind, Op.SrcExpr.take(), |
| 286 | 0, DestTInfo, OpLoc, |
Fariborz Jahanian | f799ae1 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 287 | Parens.getEnd(), |
| 288 | AngleBrackets)); |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 289 | } |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 290 | case tok::kw_static_cast: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 291 | if (!TypeDependent) { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 292 | Op.CheckStaticCast(); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 293 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 294 | return ExprError(); |
| 295 | } |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 296 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 297 | return Op.complete(CXXStaticCastExpr::Create(Context, Op.ResultType, |
| 298 | Op.ValueKind, Op.Kind, Op.SrcExpr.take(), |
| 299 | &Op.BasePath, DestTInfo, |
Fariborz Jahanian | f799ae1 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 300 | OpLoc, Parens.getEnd(), |
| 301 | AngleBrackets)); |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 302 | } |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 303 | } |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 304 | } |
| 305 | |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 306 | /// Try to diagnose a failed overloaded cast. Returns true if |
| 307 | /// diagnostics were emitted. |
| 308 | static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT, |
| 309 | SourceRange range, Expr *src, |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 310 | QualType destType, |
| 311 | bool listInitialization) { |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 312 | switch (CT) { |
| 313 | // These cast kinds don't consider user-defined conversions. |
| 314 | case CT_Const: |
| 315 | case CT_Reinterpret: |
| 316 | case CT_Dynamic: |
| 317 | return false; |
| 318 | |
| 319 | // These do. |
| 320 | case CT_Static: |
| 321 | case CT_CStyle: |
| 322 | case CT_Functional: |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | QualType srcType = src->getType(); |
| 327 | if (!destType->isRecordType() && !srcType->isRecordType()) |
| 328 | return false; |
| 329 | |
| 330 | InitializedEntity entity = InitializedEntity::InitializeTemporary(destType); |
| 331 | InitializationKind initKind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 332 | = (CT == CT_CStyle)? InitializationKind::CreateCStyleCast(range.getBegin(), |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 333 | range, listInitialization) |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 334 | : (CT == CT_Functional)? InitializationKind::CreateFunctionalCast(range, |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 335 | listInitialization) |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 336 | : InitializationKind::CreateCast(/*type range?*/ range); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 337 | InitializationSequence sequence(S, entity, initKind, src); |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 338 | |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 339 | assert(sequence.Failed() && "initialization succeeded on second try?"); |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 340 | switch (sequence.getFailureKind()) { |
| 341 | default: return false; |
| 342 | |
| 343 | case InitializationSequence::FK_ConstructorOverloadFailed: |
| 344 | case InitializationSequence::FK_UserConversionOverloadFailed: |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | OverloadCandidateSet &candidates = sequence.getFailedCandidateSet(); |
| 349 | |
| 350 | unsigned msg = 0; |
| 351 | OverloadCandidateDisplayKind howManyCandidates = OCD_AllCandidates; |
| 352 | |
| 353 | switch (sequence.getFailedOverloadResult()) { |
| 354 | case OR_Success: llvm_unreachable("successful failed overload"); |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 355 | case OR_No_Viable_Function: |
| 356 | if (candidates.empty()) |
| 357 | msg = diag::err_ovl_no_conversion_in_cast; |
| 358 | else |
| 359 | msg = diag::err_ovl_no_viable_conversion_in_cast; |
| 360 | howManyCandidates = OCD_AllCandidates; |
| 361 | break; |
| 362 | |
| 363 | case OR_Ambiguous: |
| 364 | msg = diag::err_ovl_ambiguous_conversion_in_cast; |
| 365 | howManyCandidates = OCD_ViableCandidates; |
| 366 | break; |
| 367 | |
| 368 | case OR_Deleted: |
| 369 | msg = diag::err_ovl_deleted_conversion_in_cast; |
| 370 | howManyCandidates = OCD_ViableCandidates; |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | S.Diag(range.getBegin(), msg) |
| 375 | << CT << srcType << destType |
| 376 | << range << src->getSourceRange(); |
| 377 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 378 | candidates.NoteCandidates(S, howManyCandidates, src); |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 379 | |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | /// Diagnose a failed cast. |
| 384 | static void diagnoseBadCast(Sema &S, unsigned msg, CastType castType, |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 385 | SourceRange opRange, Expr *src, QualType destType, |
| 386 | bool listInitialization) { |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 387 | if (msg == diag::err_bad_cxx_cast_generic && |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 388 | tryDiagnoseOverloadedCast(S, castType, opRange, src, destType, |
| 389 | listInitialization)) |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 390 | return; |
| 391 | |
| 392 | S.Diag(opRange.getBegin(), msg) << castType |
| 393 | << src->getType() << destType << opRange << src->getSourceRange(); |
| 394 | } |
| 395 | |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 396 | /// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes, |
| 397 | /// this removes one level of indirection from both types, provided that they're |
| 398 | /// the same kind of pointer (plain or to-member). Unlike the Sema function, |
| 399 | /// this one doesn't care if the two pointers-to-member don't point into the |
| 400 | /// same class. This is because CastsAwayConstness doesn't care. |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 401 | static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) { |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 402 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 403 | *T2PtrType = T2->getAs<PointerType>(); |
| 404 | if (T1PtrType && T2PtrType) { |
| 405 | T1 = T1PtrType->getPointeeType(); |
| 406 | T2 = T2PtrType->getPointeeType(); |
| 407 | return true; |
| 408 | } |
Fariborz Jahanian | 72a8659 | 2010-02-03 20:32:31 +0000 | [diff] [blame] | 409 | const ObjCObjectPointerType *T1ObjCPtrType = |
| 410 | T1->getAs<ObjCObjectPointerType>(), |
| 411 | *T2ObjCPtrType = |
| 412 | T2->getAs<ObjCObjectPointerType>(); |
| 413 | if (T1ObjCPtrType) { |
| 414 | if (T2ObjCPtrType) { |
| 415 | T1 = T1ObjCPtrType->getPointeeType(); |
| 416 | T2 = T2ObjCPtrType->getPointeeType(); |
| 417 | return true; |
| 418 | } |
| 419 | else if (T2PtrType) { |
| 420 | T1 = T1ObjCPtrType->getPointeeType(); |
| 421 | T2 = T2PtrType->getPointeeType(); |
| 422 | return true; |
| 423 | } |
| 424 | } |
| 425 | else if (T2ObjCPtrType) { |
| 426 | if (T1PtrType) { |
| 427 | T2 = T2ObjCPtrType->getPointeeType(); |
| 428 | T1 = T1PtrType->getPointeeType(); |
| 429 | return true; |
| 430 | } |
| 431 | } |
| 432 | |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 433 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 434 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 435 | if (T1MPType && T2MPType) { |
| 436 | T1 = T1MPType->getPointeeType(); |
| 437 | T2 = T2MPType->getPointeeType(); |
| 438 | return true; |
| 439 | } |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 440 | |
| 441 | const BlockPointerType *T1BPType = T1->getAs<BlockPointerType>(), |
| 442 | *T2BPType = T2->getAs<BlockPointerType>(); |
| 443 | if (T1BPType && T2BPType) { |
| 444 | T1 = T1BPType->getPointeeType(); |
| 445 | T2 = T2BPType->getPointeeType(); |
| 446 | return true; |
| 447 | } |
| 448 | |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 452 | /// CastsAwayConstness - Check if the pointer conversion from SrcType to |
| 453 | /// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by |
| 454 | /// the cast checkers. Both arguments must denote pointer (possibly to member) |
| 455 | /// types. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 456 | /// |
| 457 | /// \param CheckCVR Whether to check for const/volatile/restrict qualifiers. |
| 458 | /// |
| 459 | /// \param CheckObjCLifetime Whether to check Objective-C lifetime qualifiers. |
Sebastian Redl | 5ed66f7 | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 460 | static bool |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 461 | CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, |
| 462 | bool CheckCVR, bool CheckObjCLifetime) { |
| 463 | // If the only checking we care about is for Objective-C lifetime qualifiers, |
| 464 | // and we're not in ARC mode, there's nothing to check. |
| 465 | if (!CheckCVR && CheckObjCLifetime && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 466 | !Self.Context.getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 467 | return false; |
| 468 | |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 469 | // Casting away constness is defined in C++ 5.2.11p8 with reference to |
| 470 | // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since |
| 471 | // the rules are non-trivial. So first we construct Tcv *...cv* as described |
| 472 | // in C++ 5.2.11p8. |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 473 | assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || |
| 474 | SrcType->isBlockPointerType()) && |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 475 | "Source type is not pointer or pointer to member."); |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 476 | assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() || |
| 477 | DestType->isBlockPointerType()) && |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 478 | "Destination type is not pointer or pointer to member."); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 479 | |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 480 | QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType), |
| 481 | UnwrappedDestType = Self.Context.getCanonicalType(DestType); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 482 | SmallVector<Qualifiers, 8> cv1, cv2; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 483 | |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 484 | // Find the qualifiers. We only care about cvr-qualifiers for the |
| 485 | // purpose of this check, because other qualifiers (address spaces, |
| 486 | // Objective-C GC, etc.) are part of the type's identity. |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 487 | while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 488 | // Determine the relevant qualifiers at this level. |
| 489 | Qualifiers SrcQuals, DestQuals; |
Anders Carlsson | 52647c6 | 2010-06-04 22:47:55 +0000 | [diff] [blame] | 490 | Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals); |
Anders Carlsson | 52647c6 | 2010-06-04 22:47:55 +0000 | [diff] [blame] | 491 | Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 492 | |
| 493 | Qualifiers RetainedSrcQuals, RetainedDestQuals; |
| 494 | if (CheckCVR) { |
| 495 | RetainedSrcQuals.setCVRQualifiers(SrcQuals.getCVRQualifiers()); |
| 496 | RetainedDestQuals.setCVRQualifiers(DestQuals.getCVRQualifiers()); |
| 497 | } |
| 498 | |
| 499 | if (CheckObjCLifetime && |
| 500 | !DestQuals.compatiblyIncludesObjCLifetime(SrcQuals)) |
| 501 | return true; |
| 502 | |
| 503 | cv1.push_back(RetainedSrcQuals); |
| 504 | cv2.push_back(RetainedDestQuals); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 505 | } |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 506 | if (cv1.empty()) |
| 507 | return false; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 508 | |
| 509 | // Construct void pointers with those qualifiers (in reverse order of |
| 510 | // unwrapping, of course). |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 511 | QualType SrcConstruct = Self.Context.VoidTy; |
| 512 | QualType DestConstruct = Self.Context.VoidTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 513 | ASTContext &Context = Self.Context; |
Craig Topper | 163fbf8 | 2013-07-08 03:55:09 +0000 | [diff] [blame] | 514 | for (SmallVectorImpl<Qualifiers>::reverse_iterator i1 = cv1.rbegin(), |
| 515 | i2 = cv2.rbegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | i1 != cv1.rend(); ++i1, ++i2) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 517 | SrcConstruct |
| 518 | = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1)); |
| 519 | DestConstruct |
| 520 | = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2)); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | // Test if they're compatible. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 524 | bool ObjCLifetimeConversion; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 525 | return SrcConstruct != DestConstruct && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 526 | !Self.IsQualificationConversion(SrcConstruct, DestConstruct, false, |
| 527 | ObjCLifetimeConversion); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 530 | /// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid. |
| 531 | /// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime- |
| 532 | /// checked downcasts in class hierarchies. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 533 | void CastOperation::CheckDynamicCast() { |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 534 | if (ValueKind == VK_RValue) |
Eli Friedman | 7a420df | 2011-10-31 20:59:03 +0000 | [diff] [blame] | 535 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 536 | else if (isPlaceholder()) |
| 537 | SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.take()); |
| 538 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 539 | return; |
Eli Friedman | 7a420df | 2011-10-31 20:59:03 +0000 | [diff] [blame] | 540 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 541 | QualType OrigSrcType = SrcExpr.get()->getType(); |
| 542 | QualType DestType = Self.Context.getCanonicalType(this->DestType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 543 | |
| 544 | // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type, |
| 545 | // or "pointer to cv void". |
| 546 | |
| 547 | QualType DestPointee; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 548 | const PointerType *DestPointer = DestType->getAs<PointerType>(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 549 | const ReferenceType *DestReference = 0; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 550 | if (DestPointer) { |
| 551 | DestPointee = DestPointer->getPointeeType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 552 | } else if ((DestReference = DestType->getAs<ReferenceType>())) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 553 | DestPointee = DestReference->getPointeeType(); |
| 554 | } else { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 555 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 556 | << this->DestType << DestRange; |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 557 | SrcExpr = ExprError(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 561 | const RecordType *DestRecord = DestPointee->getAs<RecordType>(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 562 | if (DestPointee->isVoidType()) { |
| 563 | assert(DestPointer && "Reference to void is not possible"); |
| 564 | } else if (DestRecord) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 565 | if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 566 | diag::err_bad_dynamic_cast_incomplete, |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 567 | DestRange)) { |
| 568 | SrcExpr = ExprError(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 569 | return; |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 570 | } |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 571 | } else { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 572 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 573 | << DestPointee.getUnqualifiedType() << DestRange; |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 574 | SrcExpr = ExprError(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 575 | return; |
| 576 | } |
| 577 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 578 | // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to |
| 579 | // complete class type, [...]. If T is an lvalue reference type, v shall be |
Douglas Gregor | dc843f2 | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 580 | // an lvalue of a complete class type, [...]. If T is an rvalue reference |
| 581 | // type, v shall be an expression having a complete class type, [...] |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 582 | QualType SrcType = Self.Context.getCanonicalType(OrigSrcType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 583 | QualType SrcPointee; |
| 584 | if (DestPointer) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 585 | if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 586 | SrcPointee = SrcPointer->getPointeeType(); |
| 587 | } else { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 588 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 589 | << OrigSrcType << SrcExpr.get()->getSourceRange(); |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 590 | SrcExpr = ExprError(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 591 | return; |
| 592 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 593 | } else if (DestReference->isLValueReferenceType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 594 | if (!SrcExpr.get()->isLValue()) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 595 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 596 | << CT_Dynamic << OrigSrcType << this->DestType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 597 | } |
| 598 | SrcPointee = SrcType; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 599 | } else { |
| 600 | SrcPointee = SrcType; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 603 | const RecordType *SrcRecord = SrcPointee->getAs<RecordType>(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 604 | if (SrcRecord) { |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 605 | if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 606 | diag::err_bad_dynamic_cast_incomplete, |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 607 | SrcExpr.get())) { |
| 608 | SrcExpr = ExprError(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 609 | return; |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 610 | } |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 611 | } else { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 612 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 613 | << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 614 | SrcExpr = ExprError(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 615 | return; |
| 616 | } |
| 617 | |
| 618 | assert((DestPointer || DestReference) && |
| 619 | "Bad destination non-ptr/ref slipped through."); |
| 620 | assert((DestRecord || DestPointee->isVoidType()) && |
| 621 | "Bad destination pointee slipped through."); |
| 622 | assert(SrcRecord && "Bad source pointee slipped through."); |
| 623 | |
| 624 | // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness. |
| 625 | if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 626 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 627 | << CT_Dynamic << OrigSrcType << this->DestType << OpRange; |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 628 | SrcExpr = ExprError(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | |
| 632 | // C++ 5.2.7p3: If the type of v is the same as the required result type, |
| 633 | // [except for cv]. |
| 634 | if (DestRecord == SrcRecord) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 635 | Kind = CK_NoOp; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | |
| 639 | // C++ 5.2.7p5 |
| 640 | // Upcasts are resolved statically. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 641 | if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 642 | if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee, |
| 643 | OpRange.getBegin(), OpRange, |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 644 | &BasePath)) { |
| 645 | SrcExpr = ExprError(); |
| 646 | return; |
| 647 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 648 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 649 | Kind = CK_DerivedToBase; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 650 | |
| 651 | // If we are casting to or through a virtual base class, we need a |
| 652 | // vtable. |
| 653 | if (Self.BasePathInvolvesVirtualBase(BasePath)) |
| 654 | Self.MarkVTableUsed(OpRange.getBegin(), |
| 655 | cast<CXXRecordDecl>(SrcRecord->getDecl())); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 656 | return; |
| 657 | } |
| 658 | |
| 659 | // C++ 5.2.7p6: Otherwise, v shall be [polymorphic]. |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 660 | const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition(); |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 661 | assert(SrcDecl && "Definition missing"); |
| 662 | if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 663 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 664 | << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 665 | SrcExpr = ExprError(); |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 666 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 667 | Self.MarkVTableUsed(OpRange.getBegin(), |
| 668 | cast<CXXRecordDecl>(SrcRecord->getDecl())); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 669 | |
Arnaud A. de Grandmaison | 789d82a | 2013-08-01 08:28:32 +0000 | [diff] [blame] | 670 | // dynamic_cast is not available with fno-rtti |
| 671 | if (!Self.getLangOpts().RTTI) { |
| 672 | Self.Diag(OpRange.getBegin(), diag::err_no_dynamic_cast_with_fno_rtti); |
| 673 | SrcExpr = ExprError(); |
| 674 | return; |
| 675 | } |
| 676 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 677 | // Done. Everything else is run-time checks. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 678 | Kind = CK_Dynamic; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 679 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 680 | |
| 681 | /// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid. |
| 682 | /// Refer to C++ 5.2.11 for details. const_cast is typically used in code |
| 683 | /// like this: |
| 684 | /// const char *str = "literal"; |
| 685 | /// legacy_function(const_cast\<char*\>(str)); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 686 | void CastOperation::CheckConstCast() { |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 687 | if (ValueKind == VK_RValue) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 688 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 689 | else if (isPlaceholder()) |
| 690 | SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.take()); |
| 691 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 692 | return; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 693 | |
| 694 | unsigned msg = diag::err_bad_cxx_cast_generic; |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 695 | if (TryConstCast(Self, SrcExpr, DestType, /*CStyle*/false, msg) != TC_Success |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 696 | && msg != 0) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 697 | Self.Diag(OpRange.getBegin(), msg) << CT_Const |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 698 | << SrcExpr.get()->getType() << DestType << OpRange; |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 699 | SrcExpr = ExprError(); |
| 700 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 701 | } |
| 702 | |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 703 | /// Check that a reinterpret_cast\<DestType\>(SrcExpr) is not used as upcast |
| 704 | /// or downcast between respective pointers or references. |
| 705 | static void DiagnoseReinterpretUpDownCast(Sema &Self, const Expr *SrcExpr, |
| 706 | QualType DestType, |
| 707 | SourceRange OpRange) { |
| 708 | QualType SrcType = SrcExpr->getType(); |
| 709 | // When casting from pointer or reference, get pointee type; use original |
| 710 | // type otherwise. |
| 711 | const CXXRecordDecl *SrcPointeeRD = SrcType->getPointeeCXXRecordDecl(); |
| 712 | const CXXRecordDecl *SrcRD = |
| 713 | SrcPointeeRD ? SrcPointeeRD : SrcType->getAsCXXRecordDecl(); |
| 714 | |
John McCall | fdb468f | 2013-03-27 00:03:48 +0000 | [diff] [blame] | 715 | // Examining subobjects for records is only possible if the complete and |
| 716 | // valid definition is available. Also, template instantiation is not |
| 717 | // allowed here. |
| 718 | if (!SrcRD || !SrcRD->isCompleteDefinition() || SrcRD->isInvalidDecl()) |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 719 | return; |
| 720 | |
| 721 | const CXXRecordDecl *DestRD = DestType->getPointeeCXXRecordDecl(); |
| 722 | |
John McCall | fdb468f | 2013-03-27 00:03:48 +0000 | [diff] [blame] | 723 | if (!DestRD || !DestRD->isCompleteDefinition() || DestRD->isInvalidDecl()) |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 724 | return; |
| 725 | |
| 726 | enum { |
| 727 | ReinterpretUpcast, |
| 728 | ReinterpretDowncast |
| 729 | } ReinterpretKind; |
| 730 | |
| 731 | CXXBasePaths BasePaths; |
| 732 | |
| 733 | if (SrcRD->isDerivedFrom(DestRD, BasePaths)) |
| 734 | ReinterpretKind = ReinterpretUpcast; |
| 735 | else if (DestRD->isDerivedFrom(SrcRD, BasePaths)) |
| 736 | ReinterpretKind = ReinterpretDowncast; |
| 737 | else |
| 738 | return; |
| 739 | |
| 740 | bool VirtualBase = true; |
| 741 | bool NonZeroOffset = false; |
John McCall | fdb468f | 2013-03-27 00:03:48 +0000 | [diff] [blame] | 742 | for (CXXBasePaths::const_paths_iterator I = BasePaths.begin(), |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 743 | E = BasePaths.end(); |
| 744 | I != E; ++I) { |
| 745 | const CXXBasePath &Path = *I; |
| 746 | CharUnits Offset = CharUnits::Zero(); |
| 747 | bool IsVirtual = false; |
| 748 | for (CXXBasePath::const_iterator IElem = Path.begin(), EElem = Path.end(); |
| 749 | IElem != EElem; ++IElem) { |
| 750 | IsVirtual = IElem->Base->isVirtual(); |
| 751 | if (IsVirtual) |
| 752 | break; |
| 753 | const CXXRecordDecl *BaseRD = IElem->Base->getType()->getAsCXXRecordDecl(); |
| 754 | assert(BaseRD && "Base type should be a valid unqualified class type"); |
John McCall | fdb468f | 2013-03-27 00:03:48 +0000 | [diff] [blame] | 755 | // Don't check if any base has invalid declaration or has no definition |
| 756 | // since it has no layout info. |
| 757 | const CXXRecordDecl *Class = IElem->Class, |
| 758 | *ClassDefinition = Class->getDefinition(); |
| 759 | if (Class->isInvalidDecl() || !ClassDefinition || |
| 760 | !ClassDefinition->isCompleteDefinition()) |
| 761 | return; |
| 762 | |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 763 | const ASTRecordLayout &DerivedLayout = |
John McCall | fdb468f | 2013-03-27 00:03:48 +0000 | [diff] [blame] | 764 | Self.Context.getASTRecordLayout(Class); |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 765 | Offset += DerivedLayout.getBaseClassOffset(BaseRD); |
| 766 | } |
| 767 | if (!IsVirtual) { |
| 768 | // Don't warn if any path is a non-virtually derived base at offset zero. |
| 769 | if (Offset.isZero()) |
| 770 | return; |
| 771 | // Offset makes sense only for non-virtual bases. |
| 772 | else |
| 773 | NonZeroOffset = true; |
| 774 | } |
| 775 | VirtualBase = VirtualBase && IsVirtual; |
| 776 | } |
| 777 | |
Andy Gibbs | de7afe0 | 2013-06-19 13:33:37 +0000 | [diff] [blame] | 778 | (void) NonZeroOffset; // Silence set but not used warning. |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 779 | assert((VirtualBase || NonZeroOffset) && |
| 780 | "Should have returned if has non-virtual base with zero offset"); |
| 781 | |
| 782 | QualType BaseType = |
| 783 | ReinterpretKind == ReinterpretUpcast? DestType : SrcType; |
| 784 | QualType DerivedType = |
| 785 | ReinterpretKind == ReinterpretUpcast? SrcType : DestType; |
| 786 | |
Jordan Rose | 5fd1fac | 2013-03-28 19:09:40 +0000 | [diff] [blame] | 787 | SourceLocation BeginLoc = OpRange.getBegin(); |
| 788 | Self.Diag(BeginLoc, diag::warn_reinterpret_different_from_static) |
Joerg Sonnenberger | 7348454 | 2013-06-26 21:31:47 +0000 | [diff] [blame] | 789 | << DerivedType << BaseType << !VirtualBase << int(ReinterpretKind) |
Jordan Rose | 5fd1fac | 2013-03-28 19:09:40 +0000 | [diff] [blame] | 790 | << OpRange; |
| 791 | Self.Diag(BeginLoc, diag::note_reinterpret_updowncast_use_static) |
Joerg Sonnenberger | 7348454 | 2013-06-26 21:31:47 +0000 | [diff] [blame] | 792 | << int(ReinterpretKind) |
Jordan Rose | 5fd1fac | 2013-03-28 19:09:40 +0000 | [diff] [blame] | 793 | << FixItHint::CreateReplacement(BeginLoc, "static_cast"); |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 796 | /// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is |
| 797 | /// valid. |
| 798 | /// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code |
| 799 | /// like this: |
| 800 | /// char *bytes = reinterpret_cast\<char*\>(int_ptr); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 801 | void CastOperation::CheckReinterpretCast() { |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 802 | if (ValueKind == VK_RValue && !isPlaceholder(BuiltinType::Overload)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 803 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 804 | else |
| 805 | checkNonOverloadPlaceholders(); |
| 806 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 807 | return; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 808 | |
| 809 | unsigned msg = diag::err_bad_cxx_cast_generic; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 810 | TryCastResult tcr = |
| 811 | TryReinterpretCast(Self, SrcExpr, DestType, |
| 812 | /*CStyle*/false, OpRange, msg, Kind); |
| 813 | if (tcr != TC_Success && msg != 0) |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 814 | { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 815 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 816 | return; |
| 817 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 818 | //FIXME: &f<int>; is overloaded and resolvable |
| 819 | Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 820 | << OverloadExpr::find(SrcExpr.get()).Expression->getName() |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 821 | << DestType << OpRange; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 822 | Self.NoteAllOverloadCandidates(SrcExpr.get()); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 823 | |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 824 | } else { |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 825 | diagnoseBadCast(Self, msg, CT_Reinterpret, OpRange, SrcExpr.get(), |
| 826 | DestType, /*listInitialization=*/false); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 827 | } |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 828 | SrcExpr = ExprError(); |
John McCall | 437da05 | 2013-03-22 02:58:14 +0000 | [diff] [blame] | 829 | } else if (tcr == TC_Success) { |
| 830 | if (Self.getLangOpts().ObjCAutoRefCount) |
| 831 | checkObjCARCConversion(Sema::CCK_OtherCast); |
| 832 | DiagnoseReinterpretUpDownCast(Self, SrcExpr.get(), DestType, OpRange); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 833 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | |
| 837 | /// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid. |
| 838 | /// Refer to C++ 5.2.9 for details. Static casts are mostly used for making |
| 839 | /// implicit conversions explicit and getting rid of data loss warnings. |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 840 | void CastOperation::CheckStaticCast() { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 841 | if (isPlaceholder()) { |
| 842 | checkNonOverloadPlaceholders(); |
| 843 | if (SrcExpr.isInvalid()) |
| 844 | return; |
| 845 | } |
| 846 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 847 | // This test is outside everything else because it's the only case where |
| 848 | // a non-lvalue-reference target type does not lead to decay. |
| 849 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
Eli Friedman | 05d9d7a | 2009-11-16 05:44:20 +0000 | [diff] [blame] | 850 | if (DestType->isVoidType()) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 851 | Kind = CK_ToVoid; |
| 852 | |
| 853 | if (claimPlaceholder(BuiltinType::Overload)) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 854 | Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 855 | false, // Decay Function to ptr |
| 856 | true, // Complain |
| 857 | OpRange, DestType, diag::err_bad_static_cast_overload); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 858 | if (SrcExpr.isInvalid()) |
| 859 | return; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 860 | } |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 861 | |
| 862 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 863 | return; |
Eli Friedman | 05d9d7a | 2009-11-16 05:44:20 +0000 | [diff] [blame] | 864 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 865 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 866 | if (ValueKind == VK_RValue && !DestType->isRecordType() && |
| 867 | !isPlaceholder(BuiltinType::Overload)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 868 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
| 869 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 870 | return; |
| 871 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 872 | |
| 873 | unsigned msg = diag::err_bad_cxx_cast_generic; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 874 | TryCastResult tcr |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 875 | = TryStaticCast(Self, SrcExpr, DestType, Sema::CCK_OtherCast, OpRange, msg, |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 876 | Kind, BasePath, /*ListInitialization=*/false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 877 | if (tcr != TC_Success && msg != 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 878 | if (SrcExpr.isInvalid()) |
| 879 | return; |
| 880 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
| 881 | OverloadExpr* oe = OverloadExpr::find(SrcExpr.get()).Expression; |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 882 | Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 883 | << oe->getName() << DestType << OpRange |
| 884 | << oe->getQualifierLoc().getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 885 | Self.NoteAllOverloadCandidates(SrcExpr.get()); |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 886 | } else { |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 887 | diagnoseBadCast(Self, msg, CT_Static, OpRange, SrcExpr.get(), DestType, |
| 888 | /*listInitialization=*/false); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 889 | } |
Eli Friedman | 2437c86 | 2013-07-26 23:47:47 +0000 | [diff] [blame] | 890 | SrcExpr = ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 891 | } else if (tcr == TC_Success) { |
| 892 | if (Kind == CK_BitCast) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 893 | checkCastAlign(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 894 | if (Self.getLangOpts().ObjCAutoRefCount) |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 895 | checkObjCARCConversion(Sema::CCK_OtherCast); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 896 | } else if (Kind == CK_BitCast) { |
| 897 | checkCastAlign(); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 898 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /// TryStaticCast - Check if a static cast can be performed, and do so if |
| 902 | /// possible. If @p CStyle, ignore access restrictions on hierarchy casting |
| 903 | /// and casting away constness. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 904 | static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 905 | QualType DestType, |
| 906 | Sema::CheckedConversionKind CCK, |
Anders Carlsson | cb3c308 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 907 | const SourceRange &OpRange, unsigned &msg, |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 908 | CastKind &Kind, CXXCastPath &BasePath, |
| 909 | bool ListInitialization) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 910 | // Determine whether we have the semantics of a C-style cast. |
| 911 | bool CStyle |
| 912 | = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast); |
| 913 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 914 | // The order the tests is not entirely arbitrary. There is one conversion |
| 915 | // that can be handled in two different ways. Given: |
| 916 | // struct A {}; |
| 917 | // struct B : public A { |
| 918 | // B(); B(const A&); |
| 919 | // }; |
| 920 | // const A &a = B(); |
| 921 | // the cast static_cast<const B&>(a) could be seen as either a static |
| 922 | // reference downcast, or an explicit invocation of the user-defined |
| 923 | // conversion using B's conversion constructor. |
| 924 | // DR 427 specifies that the downcast is to be applied here. |
| 925 | |
| 926 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
| 927 | // Done outside this function. |
| 928 | |
| 929 | TryCastResult tcr; |
| 930 | |
| 931 | // C++ 5.2.9p5, reference downcast. |
| 932 | // See the function for details. |
| 933 | // DR 427 specifies that this is to be applied before paragraph 2. |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 934 | tcr = TryStaticReferenceDowncast(Self, SrcExpr.get(), DestType, CStyle, |
| 935 | OpRange, msg, Kind, BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 936 | if (tcr != TC_NotApplicable) |
| 937 | return tcr; |
| 938 | |
Douglas Gregor | dc843f2 | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 939 | // C++0x [expr.static.cast]p3: |
| 940 | // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2 |
| 941 | // T2" if "cv2 T2" is reference-compatible with "cv1 T1". |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 942 | tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind, |
| 943 | BasePath, msg); |
Douglas Gregor | 88b22a4 | 2011-01-25 16:13:26 +0000 | [diff] [blame] | 944 | if (tcr != TC_NotApplicable) |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 945 | return tcr; |
| 946 | |
| 947 | // C++ 5.2.9p2: An expression e can be explicitly converted to a type T |
| 948 | // [...] if the declaration "T t(e);" is well-formed, [...]. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 949 | tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CCK, OpRange, msg, |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 950 | Kind, ListInitialization); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 951 | if (SrcExpr.isInvalid()) |
| 952 | return TC_Failed; |
Anders Carlsson | 3c31a39 | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 953 | if (tcr != TC_NotApplicable) |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 954 | return tcr; |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 955 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 956 | // C++ 5.2.9p6: May apply the reverse of any standard conversion, except |
| 957 | // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean |
| 958 | // conversions, subject to further restrictions. |
| 959 | // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal |
| 960 | // of qualification conversions impossible. |
| 961 | // In the CStyle case, the earlier attempt to const_cast should have taken |
| 962 | // care of reverse qualification conversions. |
| 963 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 964 | QualType SrcType = Self.Context.getCanonicalType(SrcExpr.get()->getType()); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 965 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 966 | // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly |
Douglas Gregor | 1e856d9 | 2011-02-18 03:01:41 +0000 | [diff] [blame] | 967 | // converted to an integral type. [...] A value of a scoped enumeration type |
| 968 | // can also be explicitly converted to a floating-point type [...]. |
| 969 | if (const EnumType *Enum = SrcType->getAs<EnumType>()) { |
| 970 | if (Enum->getDecl()->isScoped()) { |
| 971 | if (DestType->isBooleanType()) { |
| 972 | Kind = CK_IntegralToBoolean; |
| 973 | return TC_Success; |
| 974 | } else if (DestType->isIntegralType(Self.Context)) { |
| 975 | Kind = CK_IntegralCast; |
| 976 | return TC_Success; |
| 977 | } else if (DestType->isRealFloatingType()) { |
| 978 | Kind = CK_IntegralToFloating; |
| 979 | return TC_Success; |
| 980 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 981 | } |
| 982 | } |
Douglas Gregor | 1e856d9 | 2011-02-18 03:01:41 +0000 | [diff] [blame] | 983 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 984 | // Reverse integral promotion/conversion. All such conversions are themselves |
| 985 | // again integral promotions or conversions and are thus already handled by |
| 986 | // p2 (TryDirectInitialization above). |
| 987 | // (Note: any data loss warnings should be suppressed.) |
| 988 | // The exception is the reverse of enum->integer, i.e. integer->enum (and |
| 989 | // enum->enum). See also C++ 5.2.9p7. |
| 990 | // The same goes for reverse floating point promotion/conversion and |
| 991 | // floating-integral conversions. Again, only floating->enum is relevant. |
| 992 | if (DestType->isEnumeralType()) { |
Eli Friedman | cc2fca2 | 2011-09-02 17:38:59 +0000 | [diff] [blame] | 993 | if (SrcType->isIntegralOrEnumerationType()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 994 | Kind = CK_IntegralCast; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 995 | return TC_Success; |
Eli Friedman | cc2fca2 | 2011-09-02 17:38:59 +0000 | [diff] [blame] | 996 | } else if (SrcType->isRealFloatingType()) { |
| 997 | Kind = CK_FloatingToIntegral; |
| 998 | return TC_Success; |
Eli Friedman | 05d9d7a | 2009-11-16 05:44:20 +0000 | [diff] [blame] | 999 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast. |
| 1003 | // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance. |
Anders Carlsson | 95c5d8a | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 1004 | tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg, |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1005 | Kind, BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1006 | if (tcr != TC_NotApplicable) |
| 1007 | return tcr; |
| 1008 | |
| 1009 | // Reverse member pointer conversion. C++ 4.11 specifies member pointer |
| 1010 | // conversion. C++ 5.2.9p9 has additional information. |
| 1011 | // DR54's access restrictions apply here also. |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1012 | tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1013 | OpRange, msg, Kind, BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1014 | if (tcr != TC_NotApplicable) |
| 1015 | return tcr; |
| 1016 | |
| 1017 | // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to |
| 1018 | // void*. C++ 5.2.9p10 specifies additional restrictions, which really is |
| 1019 | // just the usual constness stuff. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1020 | if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1021 | QualType SrcPointee = SrcPointer->getPointeeType(); |
| 1022 | if (SrcPointee->isVoidType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1023 | if (const PointerType *DestPointer = DestType->getAs<PointerType>()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1024 | QualType DestPointee = DestPointer->getPointeeType(); |
| 1025 | if (DestPointee->isIncompleteOrObjectType()) { |
| 1026 | // This is definitely the intended conversion, but it might fail due |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1027 | // to a qualifier violation. Note that we permit Objective-C lifetime |
| 1028 | // and GC qualifier mismatches here. |
| 1029 | if (!CStyle) { |
| 1030 | Qualifiers DestPointeeQuals = DestPointee.getQualifiers(); |
| 1031 | Qualifiers SrcPointeeQuals = SrcPointee.getQualifiers(); |
| 1032 | DestPointeeQuals.removeObjCGCAttr(); |
| 1033 | DestPointeeQuals.removeObjCLifetime(); |
| 1034 | SrcPointeeQuals.removeObjCGCAttr(); |
| 1035 | SrcPointeeQuals.removeObjCLifetime(); |
| 1036 | if (DestPointeeQuals != SrcPointeeQuals && |
| 1037 | !DestPointeeQuals.compatiblyIncludes(SrcPointeeQuals)) { |
| 1038 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
| 1039 | return TC_Failed; |
| 1040 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1041 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1042 | Kind = CK_BitCast; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1043 | return TC_Success; |
| 1044 | } |
| 1045 | } |
Fariborz Jahanian | 2f6c550 | 2010-05-10 23:46:53 +0000 | [diff] [blame] | 1046 | else if (DestType->isObjCObjectPointerType()) { |
| 1047 | // allow both c-style cast and static_cast of objective-c pointers as |
| 1048 | // they are pervasive. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1049 | Kind = CK_CPointerToObjCPointerCast; |
Fariborz Jahanian | 92ef5d7 | 2009-12-08 23:09:15 +0000 | [diff] [blame] | 1050 | return TC_Success; |
| 1051 | } |
Fariborz Jahanian | 3b27f1a | 2009-12-11 22:40:48 +0000 | [diff] [blame] | 1052 | else if (CStyle && DestType->isBlockPointerType()) { |
| 1053 | // allow c-style cast of void * to block pointers. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1054 | Kind = CK_AnyPointerToBlockPointerCast; |
Fariborz Jahanian | 3b27f1a | 2009-12-11 22:40:48 +0000 | [diff] [blame] | 1055 | return TC_Success; |
| 1056 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
Fariborz Jahanian | 65267b2 | 2010-05-12 18:16:59 +0000 | [diff] [blame] | 1059 | // Allow arbitray objective-c pointer conversion with static casts. |
| 1060 | if (SrcType->isObjCObjectPointerType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1061 | DestType->isObjCObjectPointerType()) { |
| 1062 | Kind = CK_BitCast; |
Fariborz Jahanian | 65267b2 | 2010-05-12 18:16:59 +0000 | [diff] [blame] | 1063 | return TC_Success; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1064 | } |
Fariborz Jahanian | 65267b2 | 2010-05-12 18:16:59 +0000 | [diff] [blame] | 1065 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1066 | // We tried everything. Everything! Nothing works! :-( |
| 1067 | return TC_NotApplicable; |
| 1068 | } |
| 1069 | |
| 1070 | /// Tests whether a conversion according to N2844 is valid. |
| 1071 | TryCastResult |
| 1072 | TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType, |
Douglas Gregor | 8ec14e6 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 1073 | bool CStyle, CastKind &Kind, CXXCastPath &BasePath, |
| 1074 | unsigned &msg) { |
Douglas Gregor | dc843f2 | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 1075 | // C++0x [expr.static.cast]p3: |
| 1076 | // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to |
| 1077 | // cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1078 | const RValueReferenceType *R = DestType->getAs<RValueReferenceType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1079 | if (!R) |
| 1080 | return TC_NotApplicable; |
| 1081 | |
Douglas Gregor | dc843f2 | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 1082 | if (!SrcExpr->isGLValue()) |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1083 | return TC_NotApplicable; |
| 1084 | |
| 1085 | // Because we try the reference downcast before this function, from now on |
| 1086 | // this is the only cast possibility, so we issue an error if we fail now. |
| 1087 | // FIXME: Should allow casting away constness if CStyle. |
| 1088 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 1089 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1090 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8ec14e6 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 1091 | QualType FromType = SrcExpr->getType(); |
| 1092 | QualType ToType = R->getPointeeType(); |
| 1093 | if (CStyle) { |
| 1094 | FromType = FromType.getUnqualifiedType(); |
| 1095 | ToType = ToType.getUnqualifiedType(); |
| 1096 | } |
| 1097 | |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1098 | if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(), |
Douglas Gregor | 8ec14e6 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 1099 | ToType, FromType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1100 | DerivedToBase, ObjCConversion, |
| 1101 | ObjCLifetimeConversion) |
| 1102 | < Sema::Ref_Compatible_With_Added_Qualification) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1103 | msg = diag::err_bad_lvalue_to_rvalue_cast; |
| 1104 | return TC_Failed; |
| 1105 | } |
| 1106 | |
Douglas Gregor | 88b22a4 | 2011-01-25 16:13:26 +0000 | [diff] [blame] | 1107 | if (DerivedToBase) { |
| 1108 | Kind = CK_DerivedToBase; |
| 1109 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1110 | /*DetectVirtual=*/true); |
| 1111 | if (!Self.IsDerivedFrom(SrcExpr->getType(), R->getPointeeType(), Paths)) |
| 1112 | return TC_NotApplicable; |
| 1113 | |
| 1114 | Self.BuildBasePathArray(Paths, BasePath); |
| 1115 | } else |
| 1116 | Kind = CK_NoOp; |
| 1117 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1118 | return TC_Success; |
| 1119 | } |
| 1120 | |
| 1121 | /// Tests whether a conversion according to C++ 5.2.9p5 is valid. |
| 1122 | TryCastResult |
| 1123 | TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType, |
| 1124 | bool CStyle, const SourceRange &OpRange, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1125 | unsigned &msg, CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1126 | CXXCastPath &BasePath) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1127 | // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be |
| 1128 | // cast to type "reference to cv2 D", where D is a class derived from B, |
| 1129 | // if a valid standard conversion from "pointer to D" to "pointer to B" |
| 1130 | // exists, cv2 >= cv1, and B is not a virtual base class of D. |
| 1131 | // In addition, DR54 clarifies that the base must be accessible in the |
| 1132 | // current context. Although the wording of DR54 only applies to the pointer |
| 1133 | // variant of this rule, the intent is clearly for it to apply to the this |
| 1134 | // conversion as well. |
| 1135 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1136 | const ReferenceType *DestReference = DestType->getAs<ReferenceType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1137 | if (!DestReference) { |
| 1138 | return TC_NotApplicable; |
| 1139 | } |
| 1140 | bool RValueRef = DestReference->isRValueReferenceType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1141 | if (!RValueRef && !SrcExpr->isLValue()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1142 | // We know the left side is an lvalue reference, so we can suggest a reason. |
| 1143 | msg = diag::err_bad_cxx_cast_rvalue; |
| 1144 | return TC_NotApplicable; |
| 1145 | } |
| 1146 | |
| 1147 | QualType DestPointee = DestReference->getPointeeType(); |
| 1148 | |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1149 | return TryStaticDowncast(Self, |
| 1150 | Self.Context.getCanonicalType(SrcExpr->getType()), |
| 1151 | Self.Context.getCanonicalType(DestPointee), CStyle, |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1152 | OpRange, SrcExpr->getType(), DestType, msg, Kind, |
| 1153 | BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /// Tests whether a conversion according to C++ 5.2.9p8 is valid. |
| 1157 | TryCastResult |
| 1158 | TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | bool CStyle, const SourceRange &OpRange, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1160 | unsigned &msg, CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1161 | CXXCastPath &BasePath) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1162 | // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class |
| 1163 | // type, can be converted to an rvalue of type "pointer to cv2 D", where D |
| 1164 | // is a class derived from B, if a valid standard conversion from "pointer |
| 1165 | // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base |
| 1166 | // class of D. |
| 1167 | // In addition, DR54 clarifies that the base must be accessible in the |
| 1168 | // current context. |
| 1169 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1170 | const PointerType *DestPointer = DestType->getAs<PointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1171 | if (!DestPointer) { |
| 1172 | return TC_NotApplicable; |
| 1173 | } |
| 1174 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1175 | const PointerType *SrcPointer = SrcType->getAs<PointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1176 | if (!SrcPointer) { |
| 1177 | msg = diag::err_bad_static_cast_pointer_nonpointer; |
| 1178 | return TC_NotApplicable; |
| 1179 | } |
| 1180 | |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1181 | return TryStaticDowncast(Self, |
| 1182 | Self.Context.getCanonicalType(SrcPointer->getPointeeType()), |
| 1183 | Self.Context.getCanonicalType(DestPointer->getPointeeType()), |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1184 | CStyle, OpRange, SrcType, DestType, msg, Kind, |
| 1185 | BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | /// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and |
| 1189 | /// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1190 | /// DestType is possible and allowed. |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1191 | TryCastResult |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1192 | TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1193 | bool CStyle, const SourceRange &OpRange, QualType OrigSrcType, |
Anders Carlsson | 95c5d8a | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 1194 | QualType OrigDestType, unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1195 | CastKind &Kind, CXXCastPath &BasePath) { |
Sebastian Redl | 5ed66f7 | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 1196 | // We can only work with complete types. But don't complain if it doesn't work |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1197 | if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, 0) || |
| 1198 | Self.RequireCompleteType(OpRange.getBegin(), DestType, 0)) |
Sebastian Redl | 5ed66f7 | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 1199 | return TC_NotApplicable; |
| 1200 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1201 | // Downcast can only happen in class hierarchies, so we need classes. |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1202 | if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1203 | return TC_NotApplicable; |
| 1204 | } |
| 1205 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1206 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1207 | /*DetectVirtual=*/true); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1208 | if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) { |
| 1209 | return TC_NotApplicable; |
| 1210 | } |
| 1211 | |
| 1212 | // Target type does derive from source type. Now we're serious. If an error |
| 1213 | // appears now, it's not ignored. |
| 1214 | // This may not be entirely in line with the standard. Take for example: |
| 1215 | // struct A {}; |
| 1216 | // struct B : virtual A { |
| 1217 | // B(A&); |
| 1218 | // }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | // |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1220 | // void f() |
| 1221 | // { |
| 1222 | // (void)static_cast<const B&>(*((A*)0)); |
| 1223 | // } |
| 1224 | // As far as the standard is concerned, p5 does not apply (A is virtual), so |
| 1225 | // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid. |
| 1226 | // However, both GCC and Comeau reject this example, and accepting it would |
| 1227 | // mean more complex code if we're to preserve the nice error message. |
| 1228 | // FIXME: Being 100% compliant here would be nice to have. |
| 1229 | |
| 1230 | // Must preserve cv, as always, unless we're in C-style mode. |
| 1231 | if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1232 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1233 | return TC_Failed; |
| 1234 | } |
| 1235 | |
| 1236 | if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) { |
| 1237 | // This code is analoguous to that in CheckDerivedToBaseConversion, except |
| 1238 | // that it builds the paths in reverse order. |
| 1239 | // To sum up: record all paths to the base and build a nice string from |
| 1240 | // them. Use it to spice up the error message. |
| 1241 | if (!Paths.isRecordingPaths()) { |
| 1242 | Paths.clear(); |
| 1243 | Paths.setRecordingPaths(true); |
| 1244 | Self.IsDerivedFrom(DestType, SrcType, Paths); |
| 1245 | } |
| 1246 | std::string PathDisplayStr; |
| 1247 | std::set<unsigned> DisplayedPaths; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1248 | for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1249 | PI != PE; ++PI) { |
| 1250 | if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) { |
| 1251 | // We haven't displayed a path to this particular base |
| 1252 | // class subobject yet. |
| 1253 | PathDisplayStr += "\n "; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1254 | for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(), |
| 1255 | EE = PI->rend(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1256 | EI != EE; ++EI) |
| 1257 | PathDisplayStr += EI->Base->getType().getAsString() + " -> "; |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1258 | PathDisplayStr += QualType(DestType).getAsString(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast) |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1263 | << QualType(SrcType).getUnqualifiedType() |
| 1264 | << QualType(DestType).getUnqualifiedType() |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1265 | << PathDisplayStr << OpRange; |
| 1266 | msg = 0; |
| 1267 | return TC_Failed; |
| 1268 | } |
| 1269 | |
| 1270 | if (Paths.getDetectedVirtual() != 0) { |
| 1271 | QualType VirtualBase(Paths.getDetectedVirtual(), 0); |
| 1272 | Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual) |
| 1273 | << OrigSrcType << OrigDestType << VirtualBase << OpRange; |
| 1274 | msg = 0; |
| 1275 | return TC_Failed; |
| 1276 | } |
| 1277 | |
John McCall | 417d39f | 2011-02-14 23:21:33 +0000 | [diff] [blame] | 1278 | if (!CStyle) { |
| 1279 | switch (Self.CheckBaseClassAccess(OpRange.getBegin(), |
| 1280 | SrcType, DestType, |
| 1281 | Paths.front(), |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1282 | diag::err_downcast_from_inaccessible_base)) { |
John McCall | 417d39f | 2011-02-14 23:21:33 +0000 | [diff] [blame] | 1283 | case Sema::AR_accessible: |
| 1284 | case Sema::AR_delayed: // be optimistic |
| 1285 | case Sema::AR_dependent: // be optimistic |
| 1286 | break; |
| 1287 | |
| 1288 | case Sema::AR_inaccessible: |
| 1289 | msg = 0; |
| 1290 | return TC_Failed; |
| 1291 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1294 | Self.BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1295 | Kind = CK_BaseToDerived; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1296 | return TC_Success; |
| 1297 | } |
| 1298 | |
| 1299 | /// TryStaticMemberPointerUpcast - Tests whether a conversion according to |
| 1300 | /// C++ 5.2.9p9 is valid: |
| 1301 | /// |
| 1302 | /// An rvalue of type "pointer to member of D of type cv1 T" can be |
| 1303 | /// converted to an rvalue of type "pointer to member of B of type cv2 T", |
| 1304 | /// where B is a base class of D [...]. |
| 1305 | /// |
| 1306 | TryCastResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1307 | TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, QualType SrcType, |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1308 | QualType DestType, bool CStyle, |
| 1309 | const SourceRange &OpRange, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1310 | unsigned &msg, CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1311 | CXXCastPath &BasePath) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1312 | const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1313 | if (!DestMemPtr) |
| 1314 | return TC_NotApplicable; |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1315 | |
| 1316 | bool WasOverloadedFunction = false; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1317 | DeclAccessPair FoundOverload; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1318 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 1319 | if (FunctionDecl *Fn |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1320 | = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), DestType, false, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 1321 | FoundOverload)) { |
| 1322 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 1323 | SrcType = Self.Context.getMemberPointerType(Fn->getType(), |
| 1324 | Self.Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 1325 | WasOverloadedFunction = true; |
| 1326 | } |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1327 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 1328 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1329 | const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1330 | if (!SrcMemPtr) { |
| 1331 | msg = diag::err_bad_static_cast_member_pointer_nonmp; |
| 1332 | return TC_NotApplicable; |
| 1333 | } |
| 1334 | |
| 1335 | // T == T, modulo cv |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1336 | if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(), |
| 1337 | DestMemPtr->getPointeeType())) |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1338 | return TC_NotApplicable; |
| 1339 | |
| 1340 | // B base of D |
| 1341 | QualType SrcClass(SrcMemPtr->getClass(), 0); |
| 1342 | QualType DestClass(DestMemPtr->getClass(), 0); |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1343 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1344 | /*DetectVirtual=*/true); |
| 1345 | if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) { |
| 1346 | return TC_NotApplicable; |
| 1347 | } |
| 1348 | |
| 1349 | // B is a base of D. But is it an allowed base? If not, it's a hard error. |
Douglas Gregor | e0d5fe2 | 2010-05-21 20:29:55 +0000 | [diff] [blame] | 1350 | if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1351 | Paths.clear(); |
| 1352 | Paths.setRecordingPaths(true); |
| 1353 | bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths); |
| 1354 | assert(StillOkay); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1355 | (void)StillOkay; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1356 | std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths); |
| 1357 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv) |
| 1358 | << 1 << SrcClass << DestClass << PathDisplayStr << OpRange; |
| 1359 | msg = 0; |
| 1360 | return TC_Failed; |
| 1361 | } |
| 1362 | |
| 1363 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
| 1364 | Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual) |
| 1365 | << SrcClass << DestClass << QualType(VBase, 0) << OpRange; |
| 1366 | msg = 0; |
| 1367 | return TC_Failed; |
| 1368 | } |
| 1369 | |
John McCall | 417d39f | 2011-02-14 23:21:33 +0000 | [diff] [blame] | 1370 | if (!CStyle) { |
| 1371 | switch (Self.CheckBaseClassAccess(OpRange.getBegin(), |
| 1372 | DestClass, SrcClass, |
| 1373 | Paths.front(), |
| 1374 | diag::err_upcast_to_inaccessible_base)) { |
| 1375 | case Sema::AR_accessible: |
| 1376 | case Sema::AR_delayed: |
| 1377 | case Sema::AR_dependent: |
| 1378 | // Optimistically assume that the delayed and dependent cases |
| 1379 | // will work out. |
| 1380 | break; |
| 1381 | |
| 1382 | case Sema::AR_inaccessible: |
| 1383 | msg = 0; |
| 1384 | return TC_Failed; |
| 1385 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1388 | if (WasOverloadedFunction) { |
| 1389 | // Resolve the address of the overloaded function again, this time |
| 1390 | // allowing complaints if something goes wrong. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1391 | FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1392 | DestType, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1393 | true, |
| 1394 | FoundOverload); |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1395 | if (!Fn) { |
| 1396 | msg = 0; |
| 1397 | return TC_Failed; |
| 1398 | } |
| 1399 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1400 | SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1401 | if (!SrcExpr.isUsable()) { |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1402 | msg = 0; |
| 1403 | return TC_Failed; |
| 1404 | } |
| 1405 | } |
| 1406 | |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1407 | Self.BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1408 | Kind = CK_DerivedToBaseMemberPointer; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1409 | return TC_Success; |
| 1410 | } |
| 1411 | |
| 1412 | /// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 |
| 1413 | /// is valid: |
| 1414 | /// |
| 1415 | /// An expression e can be explicitly converted to a type T using a |
| 1416 | /// @c static_cast if the declaration "T t(e);" is well-formed [...]. |
| 1417 | TryCastResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1418 | TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1419 | Sema::CheckedConversionKind CCK, |
| 1420 | const SourceRange &OpRange, unsigned &msg, |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1421 | CastKind &Kind, bool ListInitialization) { |
Anders Carlsson | d851b37 | 2009-09-07 18:25:47 +0000 | [diff] [blame] | 1422 | if (DestType->isRecordType()) { |
| 1423 | if (Self.RequireCompleteType(OpRange.getBegin(), DestType, |
Aaron Ballman | 21eb6d4 | 2012-05-07 00:02:00 +0000 | [diff] [blame] | 1424 | diag::err_bad_dynamic_cast_incomplete) || |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1425 | Self.RequireNonAbstractType(OpRange.getBegin(), DestType, |
Aaron Ballman | 21eb6d4 | 2012-05-07 00:02:00 +0000 | [diff] [blame] | 1426 | diag::err_allocation_of_abstract_type)) { |
Anders Carlsson | d851b37 | 2009-09-07 18:25:47 +0000 | [diff] [blame] | 1427 | msg = 0; |
| 1428 | return TC_Failed; |
| 1429 | } |
| 1430 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 1431 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 1432 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType); |
| 1433 | InitializationKind InitKind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1434 | = (CCK == Sema::CCK_CStyleCast) |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 1435 | ? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange, |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1436 | ListInitialization) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1437 | : (CCK == Sema::CCK_FunctionalCast) |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1438 | ? InitializationKind::CreateFunctionalCast(OpRange, ListInitialization) |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 1439 | : InitializationKind::CreateCast(OpRange); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1440 | Expr *SrcExprRaw = SrcExpr.get(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1441 | InitializationSequence InitSeq(Self, Entity, InitKind, SrcExprRaw); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1442 | |
| 1443 | // At this point of CheckStaticCast, if the destination is a reference, |
| 1444 | // or the expression is an overload expression this has to work. |
| 1445 | // There is no other way that works. |
| 1446 | // On the other hand, if we're checking a C-style cast, we've still got |
| 1447 | // the reinterpret_cast way. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1448 | bool CStyle |
| 1449 | = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 1450 | if (InitSeq.Failed() && (CStyle || !DestType->isReferenceType())) |
Anders Carlsson | 3c31a39 | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 1451 | return TC_NotApplicable; |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1452 | |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1453 | ExprResult Result = InitSeq.Perform(Self, Entity, InitKind, SrcExprRaw); |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 1454 | if (Result.isInvalid()) { |
| 1455 | msg = 0; |
| 1456 | return TC_Failed; |
| 1457 | } |
| 1458 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1459 | if (InitSeq.isConstructorInitialization()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1460 | Kind = CK_ConstructorConversion; |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1461 | else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1462 | Kind = CK_NoOp; |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1463 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1464 | SrcExpr = Result; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 1465 | return TC_Success; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | /// TryConstCast - See if a const_cast from source to destination is allowed, |
| 1469 | /// and perform it if it is. |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 1470 | static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr, |
| 1471 | QualType DestType, bool CStyle, |
| 1472 | unsigned &msg) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1473 | DestType = Self.Context.getCanonicalType(DestType); |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 1474 | QualType SrcType = SrcExpr.get()->getType(); |
| 1475 | bool NeedToMaterializeTemporary = false; |
| 1476 | |
Douglas Gregor | 575d2a3 | 2011-01-22 00:19:52 +0000 | [diff] [blame] | 1477 | if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) { |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 1478 | // C++11 5.2.11p4: |
| 1479 | // if a pointer to T1 can be explicitly converted to the type "pointer to |
| 1480 | // T2" using a const_cast, then the following conversions can also be |
| 1481 | // made: |
| 1482 | // -- an lvalue of type T1 can be explicitly converted to an lvalue of |
| 1483 | // type T2 using the cast const_cast<T2&>; |
| 1484 | // -- a glvalue of type T1 can be explicitly converted to an xvalue of |
| 1485 | // type T2 using the cast const_cast<T2&&>; and |
| 1486 | // -- if T1 is a class type, a prvalue of type T1 can be explicitly |
| 1487 | // converted to an xvalue of type T2 using the cast const_cast<T2&&>. |
| 1488 | |
| 1489 | if (isa<LValueReferenceType>(DestTypeTmp) && !SrcExpr.get()->isLValue()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1490 | // Cannot const_cast non-lvalue to lvalue reference type. But if this |
| 1491 | // is C-style, static_cast might find a way, so we simply suggest a |
| 1492 | // message and tell the parent to keep searching. |
| 1493 | msg = diag::err_bad_cxx_cast_rvalue; |
| 1494 | return TC_NotApplicable; |
| 1495 | } |
| 1496 | |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 1497 | if (isa<RValueReferenceType>(DestTypeTmp) && SrcExpr.get()->isRValue()) { |
| 1498 | if (!SrcType->isRecordType()) { |
| 1499 | // Cannot const_cast non-class prvalue to rvalue reference type. But if |
| 1500 | // this is C-style, static_cast can do this. |
| 1501 | msg = diag::err_bad_cxx_cast_rvalue; |
| 1502 | return TC_NotApplicable; |
| 1503 | } |
| 1504 | |
| 1505 | // Materialize the class prvalue so that the const_cast can bind a |
| 1506 | // reference to it. |
| 1507 | NeedToMaterializeTemporary = true; |
| 1508 | } |
| 1509 | |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1510 | // It's not completely clear under the standard whether we can |
| 1511 | // const_cast bit-field gl-values. Doing so would not be |
| 1512 | // intrinsically complicated, but for now, we say no for |
| 1513 | // consistency with other compilers and await the word of the |
| 1514 | // committee. |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 1515 | if (SrcExpr.get()->refersToBitField()) { |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1516 | msg = diag::err_bad_cxx_cast_bitfield; |
| 1517 | return TC_NotApplicable; |
| 1518 | } |
| 1519 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1520 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); |
| 1521 | SrcType = Self.Context.getPointerType(SrcType); |
| 1522 | } |
| 1523 | |
| 1524 | // C++ 5.2.11p5: For a const_cast involving pointers to data members [...] |
| 1525 | // the rules for const_cast are the same as those used for pointers. |
| 1526 | |
John McCall | d425d2b | 2010-05-18 09:35:29 +0000 | [diff] [blame] | 1527 | if (!DestType->isPointerType() && |
| 1528 | !DestType->isMemberPointerType() && |
| 1529 | !DestType->isObjCObjectPointerType()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1530 | // Cannot cast to non-pointer, non-reference type. Note that, if DestType |
| 1531 | // was a reference type, we converted it to a pointer above. |
| 1532 | // The status of rvalue references isn't entirely clear, but it looks like |
| 1533 | // conversion to them is simply invalid. |
| 1534 | // C++ 5.2.11p3: For two pointer types [...] |
| 1535 | if (!CStyle) |
| 1536 | msg = diag::err_bad_const_cast_dest; |
| 1537 | return TC_NotApplicable; |
| 1538 | } |
| 1539 | if (DestType->isFunctionPointerType() || |
| 1540 | DestType->isMemberFunctionPointerType()) { |
| 1541 | // Cannot cast direct function pointers. |
| 1542 | // C++ 5.2.11p2: [...] where T is any object type or the void type [...] |
| 1543 | // T is the ultimate pointee of source and target type. |
| 1544 | if (!CStyle) |
| 1545 | msg = diag::err_bad_const_cast_dest; |
| 1546 | return TC_NotApplicable; |
| 1547 | } |
| 1548 | SrcType = Self.Context.getCanonicalType(SrcType); |
| 1549 | |
| 1550 | // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are |
| 1551 | // completely equal. |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1552 | // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers |
| 1553 | // in multi-level pointers may change, but the level count must be the same, |
| 1554 | // as must be the final pointee type. |
| 1555 | while (SrcType != DestType && |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 1556 | Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1557 | Qualifiers SrcQuals, DestQuals; |
| 1558 | SrcType = Self.Context.getUnqualifiedArrayType(SrcType, SrcQuals); |
| 1559 | DestType = Self.Context.getUnqualifiedArrayType(DestType, DestQuals); |
| 1560 | |
| 1561 | // const_cast is permitted to strip cvr-qualifiers, only. Make sure that |
| 1562 | // the other qualifiers (e.g., address spaces) are identical. |
| 1563 | SrcQuals.removeCVRQualifiers(); |
| 1564 | DestQuals.removeCVRQualifiers(); |
| 1565 | if (SrcQuals != DestQuals) |
| 1566 | return TC_NotApplicable; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | // Since we're dealing in canonical types, the remainder must be the same. |
| 1570 | if (SrcType != DestType) |
| 1571 | return TC_NotApplicable; |
| 1572 | |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 1573 | if (NeedToMaterializeTemporary) |
| 1574 | // This is a const_cast from a class prvalue to an rvalue reference type. |
| 1575 | // Materialize a temporary to store the result of the conversion. |
| 1576 | SrcExpr = new (Self.Context) MaterializeTemporaryExpr( |
| 1577 | SrcType, SrcExpr.take(), /*IsLValueReference*/ false, |
| 1578 | /*ExtendingDecl*/ 0); |
| 1579 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1580 | return TC_Success; |
| 1581 | } |
| 1582 | |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1583 | // Checks for undefined behavior in reinterpret_cast. |
| 1584 | // The cases that is checked for is: |
| 1585 | // *reinterpret_cast<T*>(&a) |
| 1586 | // reinterpret_cast<T&>(a) |
| 1587 | // where accessing 'a' as type 'T' will result in undefined behavior. |
| 1588 | void Sema::CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType, |
| 1589 | bool IsDereference, |
| 1590 | SourceRange Range) { |
| 1591 | unsigned DiagID = IsDereference ? |
| 1592 | diag::warn_pointer_indirection_from_incompatible_type : |
| 1593 | diag::warn_undefined_reinterpret_cast; |
| 1594 | |
| 1595 | if (Diags.getDiagnosticLevel(DiagID, Range.getBegin()) == |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1596 | DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1597 | return; |
| 1598 | } |
| 1599 | |
| 1600 | QualType SrcTy, DestTy; |
| 1601 | if (IsDereference) { |
| 1602 | if (!SrcType->getAs<PointerType>() || !DestType->getAs<PointerType>()) { |
| 1603 | return; |
| 1604 | } |
| 1605 | SrcTy = SrcType->getPointeeType(); |
| 1606 | DestTy = DestType->getPointeeType(); |
| 1607 | } else { |
| 1608 | if (!DestType->getAs<ReferenceType>()) { |
| 1609 | return; |
| 1610 | } |
| 1611 | SrcTy = SrcType; |
| 1612 | DestTy = DestType->getPointeeType(); |
| 1613 | } |
| 1614 | |
| 1615 | // Cast is compatible if the types are the same. |
| 1616 | if (Context.hasSameUnqualifiedType(DestTy, SrcTy)) { |
| 1617 | return; |
| 1618 | } |
| 1619 | // or one of the types is a char or void type |
| 1620 | if (DestTy->isAnyCharacterType() || DestTy->isVoidType() || |
| 1621 | SrcTy->isAnyCharacterType() || SrcTy->isVoidType()) { |
| 1622 | return; |
| 1623 | } |
| 1624 | // or one of the types is a tag type. |
Chandler Carruth | 1f8f2d5 | 2011-05-24 07:43:19 +0000 | [diff] [blame] | 1625 | if (SrcTy->getAs<TagType>() || DestTy->getAs<TagType>()) { |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1626 | return; |
| 1627 | } |
| 1628 | |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1629 | // FIXME: Scoped enums? |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1630 | if ((SrcTy->isUnsignedIntegerType() && DestTy->isSignedIntegerType()) || |
| 1631 | (SrcTy->isSignedIntegerType() && DestTy->isUnsignedIntegerType())) { |
| 1632 | if (Context.getTypeSize(DestTy) == Context.getTypeSize(SrcTy)) { |
| 1633 | return; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range; |
| 1638 | } |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 1639 | |
Fariborz Jahanian | 91dd9df | 2012-08-16 18:33:47 +0000 | [diff] [blame] | 1640 | static void DiagnoseCastOfObjCSEL(Sema &Self, const ExprResult &SrcExpr, |
| 1641 | QualType DestType) { |
| 1642 | QualType SrcType = SrcExpr.get()->getType(); |
Fariborz Jahanian | 0c252fa | 2012-12-13 00:42:06 +0000 | [diff] [blame] | 1643 | if (Self.Context.hasSameType(SrcType, DestType)) |
| 1644 | return; |
Fariborz Jahanian | 91dd9df | 2012-08-16 18:33:47 +0000 | [diff] [blame] | 1645 | if (const PointerType *SrcPtrTy = SrcType->getAs<PointerType>()) |
| 1646 | if (SrcPtrTy->isObjCSelType()) { |
| 1647 | QualType DT = DestType; |
| 1648 | if (isa<PointerType>(DestType)) |
| 1649 | DT = DestType->getPointeeType(); |
| 1650 | if (!DT.getUnqualifiedType()->isVoidType()) |
| 1651 | Self.Diag(SrcExpr.get()->getExprLoc(), |
| 1652 | diag::warn_cast_pointer_from_sel) |
| 1653 | << SrcType << DestType << SrcExpr.get()->getSourceRange(); |
| 1654 | } |
| 1655 | } |
| 1656 | |
David Blaikie | 9b29f4f | 2012-10-16 18:53:14 +0000 | [diff] [blame] | 1657 | static void checkIntToPointerCast(bool CStyle, SourceLocation Loc, |
| 1658 | const Expr *SrcExpr, QualType DestType, |
| 1659 | Sema &Self) { |
| 1660 | QualType SrcType = SrcExpr->getType(); |
| 1661 | |
| 1662 | // Not warning on reinterpret_cast, boolean, constant expressions, etc |
| 1663 | // are not explicit design choices, but consistent with GCC's behavior. |
| 1664 | // Feel free to modify them if you've reason/evidence for an alternative. |
| 1665 | if (CStyle && SrcType->isIntegralType(Self.Context) |
| 1666 | && !SrcType->isBooleanType() |
| 1667 | && !SrcType->isEnumeralType() |
| 1668 | && !SrcExpr->isIntegerConstantExpr(Self.Context) |
Ted Kremenek | 2628b44 | 2013-05-29 21:50:46 +0000 | [diff] [blame] | 1669 | && Self.Context.getTypeSize(DestType) > |
| 1670 | Self.Context.getTypeSize(SrcType)) { |
| 1671 | // Separate between casts to void* and non-void* pointers. |
| 1672 | // Some APIs use (abuse) void* for something like a user context, |
| 1673 | // and often that value is an integer even if it isn't a pointer itself. |
| 1674 | // Having a separate warning flag allows users to control the warning |
| 1675 | // for their workflow. |
| 1676 | unsigned Diag = DestType->isVoidPointerType() ? |
| 1677 | diag::warn_int_to_void_pointer_cast |
| 1678 | : diag::warn_int_to_pointer_cast; |
| 1679 | Self.Diag(Loc, Diag) << SrcType << DestType; |
| 1680 | } |
David Blaikie | 9b29f4f | 2012-10-16 18:53:14 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1683 | static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1684 | QualType DestType, bool CStyle, |
| 1685 | const SourceRange &OpRange, |
Anders Carlsson | 3c31a39 | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 1686 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1687 | CastKind &Kind) { |
Douglas Gregor | e39a389 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1688 | bool IsLValueCast = false; |
| 1689 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1690 | DestType = Self.Context.getCanonicalType(DestType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1691 | QualType SrcType = SrcExpr.get()->getType(); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1692 | |
| 1693 | // Is the source an overloaded name? (i.e. &foo) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1694 | // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5) ... |
| 1695 | if (SrcType == Self.Context.OverloadTy) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1696 | // ... unless foo<int> resolves to an lvalue unambiguously. |
| 1697 | // TODO: what if this fails because of DiagnoseUseOfDecl or something |
| 1698 | // like it? |
| 1699 | ExprResult SingleFunctionExpr = SrcExpr; |
| 1700 | if (Self.ResolveAndFixSingleFunctionTemplateSpecialization( |
| 1701 | SingleFunctionExpr, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1702 | Expr::getValueKindForType(DestType) == VK_RValue // Convert Fun to Ptr |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1703 | ) && SingleFunctionExpr.isUsable()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1704 | SrcExpr = SingleFunctionExpr; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1705 | SrcType = SrcExpr.get()->getType(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1706 | } else { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1707 | return TC_NotApplicable; |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1708 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1709 | } |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1710 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1711 | if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) { |
Richard Smith | 6850faf | 2012-04-29 08:24:44 +0000 | [diff] [blame] | 1712 | if (!SrcExpr.get()->isGLValue()) { |
| 1713 | // Cannot cast non-glvalue to (lvalue or rvalue) reference type. See the |
| 1714 | // similar comment in const_cast. |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1715 | msg = diag::err_bad_cxx_cast_rvalue; |
| 1716 | return TC_NotApplicable; |
| 1717 | } |
| 1718 | |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1719 | if (!CStyle) { |
| 1720 | Self.CheckCompatibleReinterpretCast(SrcType, DestType, |
| 1721 | /*isDereference=*/false, OpRange); |
| 1722 | } |
| 1723 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1724 | // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the |
| 1725 | // same effect as the conversion *reinterpret_cast<T*>(&x) with the |
| 1726 | // built-in & and * operators. |
Argyrios Kyrtzidis | b464a5b | 2011-04-22 22:31:13 +0000 | [diff] [blame] | 1727 | |
Argyrios Kyrtzidis | bb29d1b | 2011-04-22 23:57:57 +0000 | [diff] [blame] | 1728 | const char *inappropriate = 0; |
| 1729 | switch (SrcExpr.get()->getObjectKind()) { |
Argyrios Kyrtzidis | e5e3d31 | 2011-04-23 01:10:24 +0000 | [diff] [blame] | 1730 | case OK_Ordinary: |
| 1731 | break; |
Argyrios Kyrtzidis | bb29d1b | 2011-04-22 23:57:57 +0000 | [diff] [blame] | 1732 | case OK_BitField: inappropriate = "bit-field"; break; |
| 1733 | case OK_VectorComponent: inappropriate = "vector element"; break; |
| 1734 | case OK_ObjCProperty: inappropriate = "property expression"; break; |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1735 | case OK_ObjCSubscript: inappropriate = "container subscripting expression"; |
| 1736 | break; |
Argyrios Kyrtzidis | bb29d1b | 2011-04-22 23:57:57 +0000 | [diff] [blame] | 1737 | } |
| 1738 | if (inappropriate) { |
| 1739 | Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_reference) |
| 1740 | << inappropriate << DestType |
| 1741 | << OpRange << SrcExpr.get()->getSourceRange(); |
| 1742 | msg = 0; SrcExpr = ExprError(); |
Argyrios Kyrtzidis | b464a5b | 2011-04-22 22:31:13 +0000 | [diff] [blame] | 1743 | return TC_NotApplicable; |
| 1744 | } |
| 1745 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1746 | // This code does this transformation for the checked types. |
| 1747 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); |
| 1748 | SrcType = Self.Context.getPointerType(SrcType); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1749 | |
Douglas Gregor | e39a389 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1750 | IsLValueCast = true; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | // Canonicalize source for comparison. |
| 1754 | SrcType = Self.Context.getCanonicalType(SrcType); |
| 1755 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1756 | const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(), |
| 1757 | *SrcMemPtr = SrcType->getAs<MemberPointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1758 | if (DestMemPtr && SrcMemPtr) { |
| 1759 | // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1" |
| 1760 | // can be explicitly converted to an rvalue of type "pointer to member |
| 1761 | // of Y of type T2" if T1 and T2 are both function types or both object |
| 1762 | // types. |
| 1763 | if (DestMemPtr->getPointeeType()->isFunctionType() != |
| 1764 | SrcMemPtr->getPointeeType()->isFunctionType()) |
| 1765 | return TC_NotApplicable; |
| 1766 | |
| 1767 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away |
| 1768 | // constness. |
| 1769 | // A reinterpret_cast followed by a const_cast can, though, so in C-style, |
| 1770 | // we accept it. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1771 | if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, |
| 1772 | /*CheckObjCLifetime=*/CStyle)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1773 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1774 | return TC_Failed; |
| 1775 | } |
| 1776 | |
Charles Davis | f231df3 | 2010-08-16 05:30:44 +0000 | [diff] [blame] | 1777 | // Don't allow casting between member pointers of different sizes. |
| 1778 | if (Self.Context.getTypeSize(DestMemPtr) != |
| 1779 | Self.Context.getTypeSize(SrcMemPtr)) { |
| 1780 | msg = diag::err_bad_cxx_cast_member_pointer_size; |
| 1781 | return TC_Failed; |
| 1782 | } |
| 1783 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1784 | // A valid member pointer cast. |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 1785 | assert(!IsLValueCast); |
| 1786 | Kind = CK_ReinterpretMemberPointer; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1787 | return TC_Success; |
| 1788 | } |
| 1789 | |
| 1790 | // See below for the enumeral issue. |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1791 | if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1792 | // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral |
| 1793 | // type large enough to hold it. A value of std::nullptr_t can be |
| 1794 | // converted to an integral type; the conversion has the same meaning |
| 1795 | // and validity as a conversion of (void*)0 to the integral type. |
| 1796 | if (Self.Context.getTypeSize(SrcType) > |
| 1797 | Self.Context.getTypeSize(DestType)) { |
| 1798 | msg = diag::err_bad_reinterpret_cast_small_int; |
| 1799 | return TC_Failed; |
| 1800 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1801 | Kind = CK_PointerToIntegral; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1802 | return TC_Success; |
| 1803 | } |
| 1804 | |
Anders Carlsson | 0de51bc | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1805 | bool destIsVector = DestType->isVectorType(); |
| 1806 | bool srcIsVector = SrcType->isVectorType(); |
| 1807 | if (srcIsVector || destIsVector) { |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1808 | // FIXME: Should this also apply to floating point types? |
| 1809 | bool srcIsScalar = SrcType->isIntegralType(Self.Context); |
| 1810 | bool destIsScalar = DestType->isIntegralType(Self.Context); |
Anders Carlsson | 0de51bc | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1811 | |
| 1812 | // Check if this is a cast between a vector and something else. |
| 1813 | if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) && |
| 1814 | !(srcIsVector && destIsVector)) |
| 1815 | return TC_NotApplicable; |
| 1816 | |
| 1817 | // If both types have the same size, we can successfully cast. |
Douglas Gregor | f2a5539 | 2009-12-22 22:47:22 +0000 | [diff] [blame] | 1818 | if (Self.Context.getTypeSize(SrcType) |
| 1819 | == Self.Context.getTypeSize(DestType)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1820 | Kind = CK_BitCast; |
Anders Carlsson | 0de51bc | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1821 | return TC_Success; |
Douglas Gregor | f2a5539 | 2009-12-22 22:47:22 +0000 | [diff] [blame] | 1822 | } |
Anders Carlsson | 0de51bc | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1823 | |
| 1824 | if (destIsScalar) |
| 1825 | msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size; |
| 1826 | else if (srcIsScalar) |
| 1827 | msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size; |
| 1828 | else |
| 1829 | msg = diag::err_bad_cxx_cast_vector_to_vector_different_size; |
| 1830 | |
| 1831 | return TC_Failed; |
| 1832 | } |
Chad Rosier | 41f4431 | 2012-02-03 02:54:37 +0000 | [diff] [blame] | 1833 | |
| 1834 | if (SrcType == DestType) { |
| 1835 | // C++ 5.2.10p2 has a note that mentions that, subject to all other |
| 1836 | // restrictions, a cast to the same type is allowed so long as it does not |
| 1837 | // cast away constness. In C++98, the intent was not entirely clear here, |
| 1838 | // since all other paragraphs explicitly forbid casts to the same type. |
| 1839 | // C++11 clarifies this case with p2. |
| 1840 | // |
| 1841 | // The only allowed types are: integral, enumeration, pointer, or |
| 1842 | // pointer-to-member types. We also won't restrict Obj-C pointers either. |
| 1843 | Kind = CK_NoOp; |
| 1844 | TryCastResult Result = TC_NotApplicable; |
| 1845 | if (SrcType->isIntegralOrEnumerationType() || |
| 1846 | SrcType->isAnyPointerType() || |
| 1847 | SrcType->isMemberPointerType() || |
| 1848 | SrcType->isBlockPointerType()) { |
| 1849 | Result = TC_Success; |
| 1850 | } |
| 1851 | return Result; |
| 1852 | } |
| 1853 | |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1854 | bool destIsPtr = DestType->isAnyPointerType() || |
| 1855 | DestType->isBlockPointerType(); |
| 1856 | bool srcIsPtr = SrcType->isAnyPointerType() || |
| 1857 | SrcType->isBlockPointerType(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1858 | if (!destIsPtr && !srcIsPtr) { |
| 1859 | // Except for std::nullptr_t->integer and lvalue->reference, which are |
| 1860 | // handled above, at least one of the two arguments must be a pointer. |
| 1861 | return TC_NotApplicable; |
| 1862 | } |
| 1863 | |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1864 | if (DestType->isIntegralType(Self.Context)) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1865 | assert(srcIsPtr && "One type must be a pointer"); |
| 1866 | // C++ 5.2.10p4: A pointer can be explicitly converted to any integral |
Francois Pichet | 30aff5b | 2011-05-11 22:13:54 +0000 | [diff] [blame] | 1867 | // type large enough to hold it; except in Microsoft mode, where the |
Hans Wennborg | 649c6c5 | 2013-06-06 09:16:36 +0000 | [diff] [blame] | 1868 | // integral type size doesn't matter (except we don't allow bool). |
| 1869 | bool MicrosoftException = Self.getLangOpts().MicrosoftExt && |
| 1870 | !DestType->isBooleanType(); |
Francois Pichet | 30aff5b | 2011-05-11 22:13:54 +0000 | [diff] [blame] | 1871 | if ((Self.Context.getTypeSize(SrcType) > |
| 1872 | Self.Context.getTypeSize(DestType)) && |
Hans Wennborg | 649c6c5 | 2013-06-06 09:16:36 +0000 | [diff] [blame] | 1873 | !MicrosoftException) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1874 | msg = diag::err_bad_reinterpret_cast_small_int; |
| 1875 | return TC_Failed; |
| 1876 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1877 | Kind = CK_PointerToIntegral; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1878 | return TC_Success; |
| 1879 | } |
| 1880 | |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1881 | if (SrcType->isIntegralOrEnumerationType()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1882 | assert(destIsPtr && "One type must be a pointer"); |
David Blaikie | 9b29f4f | 2012-10-16 18:53:14 +0000 | [diff] [blame] | 1883 | checkIntToPointerCast(CStyle, OpRange.getBegin(), SrcExpr.get(), DestType, |
| 1884 | Self); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1885 | // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly |
| 1886 | // converted to a pointer. |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1887 | // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not |
| 1888 | // necessarily converted to a null pointer value.] |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1889 | Kind = CK_IntegralToPointer; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1890 | return TC_Success; |
| 1891 | } |
| 1892 | |
| 1893 | if (!destIsPtr || !srcIsPtr) { |
| 1894 | // With the valid non-pointer conversions out of the way, we can be even |
| 1895 | // more stringent. |
| 1896 | return TC_NotApplicable; |
| 1897 | } |
| 1898 | |
| 1899 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness. |
| 1900 | // The C-style cast operator can. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1901 | if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, |
| 1902 | /*CheckObjCLifetime=*/CStyle)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1903 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1904 | return TC_Failed; |
| 1905 | } |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1906 | |
| 1907 | // Cannot convert between block pointers and Objective-C object pointers. |
| 1908 | if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) || |
| 1909 | (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType())) |
| 1910 | return TC_NotApplicable; |
| 1911 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1912 | if (IsLValueCast) { |
| 1913 | Kind = CK_LValueBitCast; |
| 1914 | } else if (DestType->isObjCObjectPointerType()) { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 1915 | Kind = Self.PrepareCastToObjCObjectPointer(SrcExpr); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1916 | } else if (DestType->isBlockPointerType()) { |
| 1917 | if (!SrcType->isBlockPointerType()) { |
| 1918 | Kind = CK_AnyPointerToBlockPointerCast; |
| 1919 | } else { |
| 1920 | Kind = CK_BitCast; |
| 1921 | } |
| 1922 | } else { |
| 1923 | Kind = CK_BitCast; |
| 1924 | } |
| 1925 | |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1926 | // Any pointer can be cast to an Objective-C pointer type with a C-style |
| 1927 | // cast. |
Fariborz Jahanian | 92ef5d7 | 2009-12-08 23:09:15 +0000 | [diff] [blame] | 1928 | if (CStyle && DestType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 92ef5d7 | 2009-12-08 23:09:15 +0000 | [diff] [blame] | 1929 | return TC_Success; |
| 1930 | } |
Fariborz Jahanian | 91dd9df | 2012-08-16 18:33:47 +0000 | [diff] [blame] | 1931 | if (CStyle) |
| 1932 | DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType); |
| 1933 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1934 | // Not casting away constness, so the only remaining check is for compatible |
| 1935 | // pointer categories. |
| 1936 | |
| 1937 | if (SrcType->isFunctionPointerType()) { |
| 1938 | if (DestType->isFunctionPointerType()) { |
| 1939 | // C++ 5.2.10p6: A pointer to a function can be explicitly converted to |
| 1940 | // a pointer to a function of a different type. |
| 1941 | return TC_Success; |
| 1942 | } |
| 1943 | |
| 1944 | // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to |
| 1945 | // an object type or vice versa is conditionally-supported. |
| 1946 | // Compilers support it in C++03 too, though, because it's necessary for |
| 1947 | // casting the return value of dlsym() and GetProcAddress(). |
| 1948 | // FIXME: Conditionally-supported behavior should be configurable in the |
| 1949 | // TargetInfo or similar. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1950 | Self.Diag(OpRange.getBegin(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1951 | Self.getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1952 | diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj) |
| 1953 | << OpRange; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1954 | return TC_Success; |
| 1955 | } |
| 1956 | |
| 1957 | if (DestType->isFunctionPointerType()) { |
| 1958 | // See above. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1959 | Self.Diag(OpRange.getBegin(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1960 | Self.getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1961 | diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj) |
| 1962 | << OpRange; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1963 | return TC_Success; |
| 1964 | } |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1965 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1966 | // C++ 5.2.10p7: A pointer to an object can be explicitly converted to |
| 1967 | // a pointer to an object of different type. |
| 1968 | // Void pointers are not specified, but supported by every compiler out there. |
| 1969 | // So we finish by allowing everything that remains - it's got to be two |
| 1970 | // object pointers. |
| 1971 | return TC_Success; |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 1972 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1973 | |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1974 | void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle, |
| 1975 | bool ListInitialization) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1976 | // Handle placeholders. |
| 1977 | if (isPlaceholder()) { |
| 1978 | // C-style casts can resolve __unknown_any types. |
| 1979 | if (claimPlaceholder(BuiltinType::UnknownAny)) { |
| 1980 | SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType, |
| 1981 | SrcExpr.get(), Kind, |
| 1982 | ValueKind, BasePath); |
| 1983 | return; |
| 1984 | } |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1985 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1986 | checkNonOverloadPlaceholders(); |
| 1987 | if (SrcExpr.isInvalid()) |
| 1988 | return; |
John McCall | 4919dfd | 2011-10-17 17:42:19 +0000 | [diff] [blame] | 1989 | } |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1990 | |
| 1991 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1992 | // This test is outside everything else because it's the only case where |
| 1993 | // a non-lvalue-reference target type does not lead to decay. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1994 | if (DestType->isVoidType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1995 | Kind = CK_ToVoid; |
| 1996 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1997 | if (claimPlaceholder(BuiltinType::Overload)) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1998 | Self.ResolveAndFixSingleFunctionTemplateSpecialization( |
| 1999 | SrcExpr, /* Decay Function to ptr */ false, |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2000 | /* Complain */ true, DestRange, DestType, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 2001 | diag::err_bad_cstyle_cast_overload); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2002 | if (SrcExpr.isInvalid()) |
| 2003 | return; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2004 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2005 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2006 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2007 | return; |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2010 | // If the type is dependent, we won't do any other semantic analysis now. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2011 | if (DestType->isDependentType() || SrcExpr.get()->isTypeDependent()) { |
| 2012 | assert(Kind == CK_Dependent); |
| 2013 | return; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2014 | } |
Benjamin Kramer | 5b4a40a | 2011-07-08 20:20:17 +0000 | [diff] [blame] | 2015 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 2016 | if (ValueKind == VK_RValue && !DestType->isRecordType() && |
| 2017 | !isPlaceholder(BuiltinType::Overload)) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2018 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
| 2019 | if (SrcExpr.isInvalid()) |
| 2020 | return; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2021 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2022 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2023 | // AltiVec vector initialization with a single literal. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2024 | if (const VectorType *vecTy = DestType->getAs<VectorType>()) |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2025 | if (vecTy->getVectorKind() == VectorType::AltiVecVector |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2026 | && (SrcExpr.get()->getType()->isIntegerType() |
| 2027 | || SrcExpr.get()->getType()->isFloatingType())) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2028 | Kind = CK_VectorSplat; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2029 | return; |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2032 | // C++ [expr.cast]p5: The conversions performed by |
| 2033 | // - a const_cast, |
| 2034 | // - a static_cast, |
| 2035 | // - a static_cast followed by a const_cast, |
| 2036 | // - a reinterpret_cast, or |
| 2037 | // - a reinterpret_cast followed by a const_cast, |
| 2038 | // can be performed using the cast notation of explicit type conversion. |
| 2039 | // [...] If a conversion can be interpreted in more than one of the ways |
| 2040 | // listed above, the interpretation that appears first in the list is used, |
| 2041 | // even if a cast resulting from that interpretation is ill-formed. |
| 2042 | // In plain language, this means trying a const_cast ... |
| 2043 | unsigned msg = diag::err_bad_cxx_cast_generic; |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 2044 | TryCastResult tcr = TryConstCast(Self, SrcExpr, DestType, |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2045 | /*CStyle*/true, msg); |
Richard Smith | 41cb3d9 | 2013-06-14 22:27:52 +0000 | [diff] [blame] | 2046 | if (SrcExpr.isInvalid()) |
| 2047 | return; |
Anders Carlsson | da921fd | 2009-10-19 18:14:28 +0000 | [diff] [blame] | 2048 | if (tcr == TC_Success) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2049 | Kind = CK_NoOp; |
Anders Carlsson | da921fd | 2009-10-19 18:14:28 +0000 | [diff] [blame] | 2050 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2051 | Sema::CheckedConversionKind CCK |
| 2052 | = FunctionalStyle? Sema::CCK_FunctionalCast |
| 2053 | : Sema::CCK_CStyleCast; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2054 | if (tcr == TC_NotApplicable) { |
| 2055 | // ... or if that is not possible, a static_cast, ignoring const, ... |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2056 | tcr = TryStaticCast(Self, SrcExpr, DestType, CCK, OpRange, |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 2057 | msg, Kind, BasePath, ListInitialization); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2058 | if (SrcExpr.isInvalid()) |
| 2059 | return; |
| 2060 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2061 | if (tcr == TC_NotApplicable) { |
| 2062 | // ... and finally a reinterpret_cast, ignoring const. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2063 | tcr = TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/true, |
| 2064 | OpRange, msg, Kind); |
| 2065 | if (SrcExpr.isInvalid()) |
| 2066 | return; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2067 | } |
| 2068 | } |
| 2069 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2070 | if (Self.getLangOpts().ObjCAutoRefCount && tcr == TC_Success) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2071 | checkObjCARCConversion(CCK); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2072 | |
Nick Lewycky | 43328e9 | 2010-11-09 00:19:31 +0000 | [diff] [blame] | 2073 | if (tcr != TC_Success && msg != 0) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2074 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 2075 | DeclAccessPair Found; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2076 | FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), |
| 2077 | DestType, |
| 2078 | /*Complain*/ true, |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 2079 | Found); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2080 | |
Richard Trieu | 32ac00d | 2011-04-16 01:09:30 +0000 | [diff] [blame] | 2081 | assert(!Fn && "cast failed but able to resolve overload expression!!"); |
Nick Lewycky | 43328e9 | 2010-11-09 00:19:31 +0000 | [diff] [blame] | 2082 | (void)Fn; |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 2083 | |
Nick Lewycky | 43328e9 | 2010-11-09 00:19:31 +0000 | [diff] [blame] | 2084 | } else { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2085 | diagnoseBadCast(Self, msg, (FunctionalStyle ? CT_Functional : CT_CStyle), |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 2086 | OpRange, SrcExpr.get(), DestType, ListInitialization); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 2087 | } |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2088 | } else if (Kind == CK_BitCast) { |
| 2089 | checkCastAlign(); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 2090 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2091 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2092 | // Clear out SrcExpr if there was a fatal error. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2093 | if (tcr != TC_Success) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2094 | SrcExpr = ExprError(); |
| 2095 | } |
| 2096 | |
Fariborz Jahanian | bbb8afd | 2012-08-17 17:22:34 +0000 | [diff] [blame] | 2097 | /// DiagnoseBadFunctionCast - Warn whenever a function call is cast to a |
| 2098 | /// non-matching type. Such as enum function call to int, int call to |
| 2099 | /// pointer; etc. Cast to 'void' is an exception. |
| 2100 | static void DiagnoseBadFunctionCast(Sema &Self, const ExprResult &SrcExpr, |
| 2101 | QualType DestType) { |
| 2102 | if (Self.Diags.getDiagnosticLevel(diag::warn_bad_function_cast, |
| 2103 | SrcExpr.get()->getExprLoc()) |
| 2104 | == DiagnosticsEngine::Ignored) |
| 2105 | return; |
| 2106 | |
| 2107 | if (!isa<CallExpr>(SrcExpr.get())) |
| 2108 | return; |
| 2109 | |
| 2110 | QualType SrcType = SrcExpr.get()->getType(); |
| 2111 | if (DestType.getUnqualifiedType()->isVoidType()) |
| 2112 | return; |
| 2113 | if ((SrcType->isAnyPointerType() || SrcType->isBlockPointerType()) |
| 2114 | && (DestType->isAnyPointerType() || DestType->isBlockPointerType())) |
| 2115 | return; |
| 2116 | if (SrcType->isIntegerType() && DestType->isIntegerType() && |
| 2117 | (SrcType->isBooleanType() == DestType->isBooleanType()) && |
| 2118 | (SrcType->isEnumeralType() == DestType->isEnumeralType())) |
| 2119 | return; |
| 2120 | if (SrcType->isRealFloatingType() && DestType->isRealFloatingType()) |
| 2121 | return; |
| 2122 | if (SrcType->isEnumeralType() && DestType->isEnumeralType()) |
| 2123 | return; |
| 2124 | if (SrcType->isComplexType() && DestType->isComplexType()) |
| 2125 | return; |
| 2126 | if (SrcType->isComplexIntegerType() && DestType->isComplexIntegerType()) |
| 2127 | return; |
| 2128 | |
| 2129 | Self.Diag(SrcExpr.get()->getExprLoc(), |
| 2130 | diag::warn_bad_function_cast) |
| 2131 | << SrcType << DestType << SrcExpr.get()->getSourceRange(); |
| 2132 | } |
| 2133 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2134 | /// Check the semantics of a C-style cast operation, in C. |
| 2135 | void CastOperation::CheckCStyleCast() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2136 | assert(!Self.getLangOpts().CPlusPlus); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2137 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2138 | // C-style casts can resolve __unknown_any types. |
| 2139 | if (claimPlaceholder(BuiltinType::UnknownAny)) { |
| 2140 | SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType, |
| 2141 | SrcExpr.get(), Kind, |
| 2142 | ValueKind, BasePath); |
| 2143 | return; |
| 2144 | } |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2145 | |
| 2146 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 2147 | // type needs to be scalar. |
| 2148 | if (DestType->isVoidType()) { |
| 2149 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
| 2150 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); |
| 2151 | if (SrcExpr.isInvalid()) |
| 2152 | return; |
| 2153 | |
| 2154 | // Cast to void allows any expr type. |
| 2155 | Kind = CK_ToVoid; |
| 2156 | return; |
| 2157 | } |
| 2158 | |
| 2159 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
| 2160 | if (SrcExpr.isInvalid()) |
| 2161 | return; |
| 2162 | QualType SrcType = SrcExpr.get()->getType(); |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 2163 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2164 | assert(!SrcType->isPlaceholderType()); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2165 | |
| 2166 | if (Self.RequireCompleteType(OpRange.getBegin(), DestType, |
| 2167 | diag::err_typecheck_cast_to_incomplete)) { |
| 2168 | SrcExpr = ExprError(); |
| 2169 | return; |
| 2170 | } |
| 2171 | |
| 2172 | if (!DestType->isScalarType() && !DestType->isVectorType()) { |
| 2173 | const RecordType *DestRecordTy = DestType->getAs<RecordType>(); |
| 2174 | |
| 2175 | if (DestRecordTy && Self.Context.hasSameUnqualifiedType(DestType, SrcType)){ |
| 2176 | // GCC struct/union extension: allow cast to self. |
| 2177 | Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2178 | << DestType << SrcExpr.get()->getSourceRange(); |
| 2179 | Kind = CK_NoOp; |
| 2180 | return; |
| 2181 | } |
| 2182 | |
| 2183 | // GCC's cast to union extension. |
| 2184 | if (DestRecordTy && DestRecordTy->getDecl()->isUnion()) { |
| 2185 | RecordDecl *RD = DestRecordTy->getDecl(); |
| 2186 | RecordDecl::field_iterator Field, FieldEnd; |
| 2187 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
| 2188 | Field != FieldEnd; ++Field) { |
| 2189 | if (Self.Context.hasSameUnqualifiedType(Field->getType(), SrcType) && |
| 2190 | !Field->isUnnamedBitfield()) { |
| 2191 | Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2192 | << SrcExpr.get()->getSourceRange(); |
| 2193 | break; |
| 2194 | } |
| 2195 | } |
| 2196 | if (Field == FieldEnd) { |
| 2197 | Self.Diag(OpRange.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2198 | << SrcType << SrcExpr.get()->getSourceRange(); |
| 2199 | SrcExpr = ExprError(); |
| 2200 | return; |
| 2201 | } |
| 2202 | Kind = CK_ToUnion; |
| 2203 | return; |
| 2204 | } |
| 2205 | |
| 2206 | // Reject any other conversions to non-scalar types. |
| 2207 | Self.Diag(OpRange.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 2208 | << DestType << SrcExpr.get()->getSourceRange(); |
| 2209 | SrcExpr = ExprError(); |
| 2210 | return; |
| 2211 | } |
| 2212 | |
| 2213 | // The type we're casting to is known to be a scalar or vector. |
| 2214 | |
| 2215 | // Require the operand to be a scalar or vector. |
| 2216 | if (!SrcType->isScalarType() && !SrcType->isVectorType()) { |
| 2217 | Self.Diag(SrcExpr.get()->getExprLoc(), |
| 2218 | diag::err_typecheck_expect_scalar_operand) |
| 2219 | << SrcType << SrcExpr.get()->getSourceRange(); |
| 2220 | SrcExpr = ExprError(); |
| 2221 | return; |
| 2222 | } |
| 2223 | |
| 2224 | if (DestType->isExtVectorType()) { |
| 2225 | SrcExpr = Self.CheckExtVectorCast(OpRange, DestType, SrcExpr.take(), Kind); |
| 2226 | return; |
| 2227 | } |
| 2228 | |
| 2229 | if (const VectorType *DestVecTy = DestType->getAs<VectorType>()) { |
| 2230 | if (DestVecTy->getVectorKind() == VectorType::AltiVecVector && |
| 2231 | (SrcType->isIntegerType() || SrcType->isFloatingType())) { |
| 2232 | Kind = CK_VectorSplat; |
| 2233 | } else if (Self.CheckVectorCast(OpRange, DestType, SrcType, Kind)) { |
| 2234 | SrcExpr = ExprError(); |
| 2235 | } |
| 2236 | return; |
| 2237 | } |
| 2238 | |
| 2239 | if (SrcType->isVectorType()) { |
| 2240 | if (Self.CheckVectorCast(OpRange, SrcType, DestType, Kind)) |
| 2241 | SrcExpr = ExprError(); |
| 2242 | return; |
| 2243 | } |
| 2244 | |
| 2245 | // The source and target types are both scalars, i.e. |
| 2246 | // - arithmetic types (fundamental, enum, and complex) |
| 2247 | // - all kinds of pointers |
| 2248 | // Note that member pointers were filtered out with C++, above. |
| 2249 | |
| 2250 | if (isa<ObjCSelectorExpr>(SrcExpr.get())) { |
| 2251 | Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_selector_expr); |
| 2252 | SrcExpr = ExprError(); |
| 2253 | return; |
| 2254 | } |
| 2255 | |
| 2256 | // If either type is a pointer, the other type has to be either an |
| 2257 | // integer or a pointer. |
| 2258 | if (!DestType->isArithmeticType()) { |
| 2259 | if (!SrcType->isIntegralType(Self.Context) && SrcType->isArithmeticType()) { |
| 2260 | Self.Diag(SrcExpr.get()->getExprLoc(), |
| 2261 | diag::err_cast_pointer_from_non_pointer_int) |
| 2262 | << SrcType << SrcExpr.get()->getSourceRange(); |
| 2263 | SrcExpr = ExprError(); |
| 2264 | return; |
| 2265 | } |
David Blaikie | 9b29f4f | 2012-10-16 18:53:14 +0000 | [diff] [blame] | 2266 | checkIntToPointerCast(/* CStyle */ true, OpRange.getBegin(), SrcExpr.get(), |
| 2267 | DestType, Self); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2268 | } else if (!SrcType->isArithmeticType()) { |
| 2269 | if (!DestType->isIntegralType(Self.Context) && |
| 2270 | DestType->isArithmeticType()) { |
| 2271 | Self.Diag(SrcExpr.get()->getLocStart(), |
| 2272 | diag::err_cast_pointer_to_non_pointer_int) |
Abramo Bagnara | f7ce194 | 2011-11-15 11:25:38 +0000 | [diff] [blame] | 2273 | << DestType << SrcExpr.get()->getSourceRange(); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2274 | SrcExpr = ExprError(); |
| 2275 | return; |
| 2276 | } |
| 2277 | } |
| 2278 | |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 2279 | if (Self.getLangOpts().OpenCL && !Self.getOpenCLOptions().cl_khr_fp16) { |
| 2280 | if (DestType->isHalfType()) { |
| 2281 | Self.Diag(SrcExpr.get()->getLocStart(), diag::err_opencl_cast_to_half) |
| 2282 | << DestType << SrcExpr.get()->getSourceRange(); |
| 2283 | SrcExpr = ExprError(); |
| 2284 | return; |
| 2285 | } |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2288 | // ARC imposes extra restrictions on casts. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2289 | if (Self.getLangOpts().ObjCAutoRefCount) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2290 | checkObjCARCConversion(Sema::CCK_CStyleCast); |
| 2291 | if (SrcExpr.isInvalid()) |
| 2292 | return; |
| 2293 | |
| 2294 | if (const PointerType *CastPtr = DestType->getAs<PointerType>()) { |
| 2295 | if (const PointerType *ExprPtr = SrcType->getAs<PointerType>()) { |
| 2296 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 2297 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 2298 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 2299 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 2300 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
| 2301 | Self.Diag(SrcExpr.get()->getLocStart(), |
| 2302 | diag::err_typecheck_incompatible_ownership) |
| 2303 | << SrcType << DestType << Sema::AA_Casting |
| 2304 | << SrcExpr.get()->getSourceRange(); |
| 2305 | return; |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | else if (!Self.CheckObjCARCUnavailableWeakConversion(DestType, SrcType)) { |
| 2310 | Self.Diag(SrcExpr.get()->getLocStart(), |
| 2311 | diag::err_arc_convesion_of_weak_unavailable) |
| 2312 | << 1 << SrcType << DestType << SrcExpr.get()->getSourceRange(); |
| 2313 | SrcExpr = ExprError(); |
| 2314 | return; |
| 2315 | } |
| 2316 | } |
Fariborz Jahanian | 91dd9df | 2012-08-16 18:33:47 +0000 | [diff] [blame] | 2317 | DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType); |
Fariborz Jahanian | bbb8afd | 2012-08-17 17:22:34 +0000 | [diff] [blame] | 2318 | DiagnoseBadFunctionCast(Self, SrcExpr, DestType); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2319 | Kind = Self.PrepareScalarCast(SrcExpr, DestType); |
| 2320 | if (SrcExpr.isInvalid()) |
| 2321 | return; |
| 2322 | |
| 2323 | if (Kind == CK_BitCast) |
| 2324 | checkCastAlign(); |
| 2325 | } |
| 2326 | |
| 2327 | ExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc, |
| 2328 | TypeSourceInfo *CastTypeInfo, |
| 2329 | SourceLocation RPLoc, |
| 2330 | Expr *CastExpr) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2331 | CastOperation Op(*this, CastTypeInfo->getType(), CastExpr); |
| 2332 | Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange(); |
| 2333 | Op.OpRange = SourceRange(LPLoc, CastExpr->getLocEnd()); |
| 2334 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2335 | if (getLangOpts().CPlusPlus) { |
Sebastian Redl | 6dc00f6 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 2336 | Op.CheckCXXCStyleCast(/*FunctionalStyle=*/ false, |
| 2337 | isa<InitListExpr>(CastExpr)); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2338 | } else { |
| 2339 | Op.CheckCStyleCast(); |
| 2340 | } |
| 2341 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2342 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2343 | return ExprError(); |
| 2344 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2345 | return Op.complete(CStyleCastExpr::Create(Context, Op.ResultType, |
| 2346 | Op.ValueKind, Op.Kind, Op.SrcExpr.take(), |
| 2347 | &Op.BasePath, CastTypeInfo, LPLoc, RPLoc)); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | ExprResult Sema::BuildCXXFunctionalCastExpr(TypeSourceInfo *CastTypeInfo, |
| 2351 | SourceLocation LPLoc, |
| 2352 | Expr *CastExpr, |
| 2353 | SourceLocation RPLoc) { |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 2354 | assert(LPLoc.isValid() && "List-initialization shouldn't get here."); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2355 | CastOperation Op(*this, CastTypeInfo->getType(), CastExpr); |
| 2356 | Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange(); |
| 2357 | Op.OpRange = SourceRange(Op.DestRange.getBegin(), CastExpr->getLocEnd()); |
| 2358 | |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 2359 | Op.CheckCXXCStyleCast(/*FunctionalStyle=*/true, /*ListInit=*/false); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2360 | if (Op.SrcExpr.isInvalid()) |
| 2361 | return ExprError(); |
Daniel Jasper | a770a4d | 2012-07-16 08:05:07 +0000 | [diff] [blame] | 2362 | |
| 2363 | if (CXXConstructExpr *ConstructExpr = dyn_cast<CXXConstructExpr>(Op.SrcExpr.get())) |
| 2364 | ConstructExpr->setParenRange(SourceRange(LPLoc, RPLoc)); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2365 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2366 | return Op.complete(CXXFunctionalCastExpr::Create(Context, Op.ResultType, |
Eli Friedman | cdd4b78 | 2013-08-15 22:02:56 +0000 | [diff] [blame^] | 2367 | Op.ValueKind, CastTypeInfo, Op.Kind, |
| 2368 | Op.SrcExpr.take(), &Op.BasePath, LPLoc, RPLoc)); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2369 | } |