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