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