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