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