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