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