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