John McCall | d8d3ced | 2011-10-11 17:38:55 +0000 | [diff] [blame] | 1 | //===--- SemaCast.cpp - Semantic Analysis for Casts -----------------------===// |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
John McCall | d8d3ced | 2011-10-11 17:38:55 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for cast expressions, including |
| 11 | // 1) C-style casts like '(int) x' |
| 12 | // 2) C++ functional casts like 'int(x)' |
| 13 | // 3) C++ named casts like 'static_cast<int>(x)' |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallVector.h" |
Sebastian Redl | e3dc28a | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 25 | #include <set> |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 29 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 30 | enum TryCastResult { |
| 31 | TC_NotApplicable, ///< The cast method is not applicable. |
| 32 | TC_Success, ///< The cast method is appropriate and successful. |
| 33 | TC_Failed ///< The cast method is appropriate, but failed. A |
| 34 | ///< diagnostic has been emitted. |
| 35 | }; |
| 36 | |
| 37 | enum CastType { |
| 38 | CT_Const, ///< const_cast |
| 39 | CT_Static, ///< static_cast |
| 40 | CT_Reinterpret, ///< reinterpret_cast |
| 41 | CT_Dynamic, ///< dynamic_cast |
| 42 | CT_CStyle, ///< (Type)expr |
| 43 | CT_Functional ///< Type(expr) |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 46 | namespace { |
| 47 | struct CastOperation { |
| 48 | CastOperation(Sema &S, QualType destType, ExprResult src) |
| 49 | : Self(S), SrcExpr(src), DestType(destType), |
| 50 | ResultType(destType.getNonLValueExprType(S.Context)), |
| 51 | ValueKind(Expr::getValueKindForType(destType)), |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 52 | Kind(CK_Dependent), IsARCUnbridgedCast(false) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 53 | |
| 54 | if (const BuiltinType *placeholder = |
| 55 | src.get()->getType()->getAsPlaceholderType()) { |
| 56 | PlaceholderKind = placeholder->getKind(); |
| 57 | } else { |
| 58 | PlaceholderKind = (BuiltinType::Kind) 0; |
| 59 | } |
| 60 | } |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 61 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 62 | Sema &Self; |
| 63 | ExprResult SrcExpr; |
| 64 | QualType DestType; |
| 65 | QualType ResultType; |
| 66 | ExprValueKind ValueKind; |
| 67 | CastKind Kind; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 68 | BuiltinType::Kind PlaceholderKind; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 69 | CXXCastPath BasePath; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 70 | bool IsARCUnbridgedCast; |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 71 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 72 | SourceRange OpRange; |
| 73 | SourceRange DestRange; |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 74 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 75 | // Top-level semantics-checking routines. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 76 | void CheckConstCast(); |
| 77 | void CheckReinterpretCast(); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 78 | void CheckStaticCast(); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 79 | void CheckDynamicCast(); |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 80 | void CheckCXXCStyleCast(bool FunctionalCast); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 81 | void CheckCStyleCast(); |
| 82 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 83 | /// Complete an apparently-successful cast operation that yields |
| 84 | /// the given expression. |
| 85 | ExprResult complete(CastExpr *castExpr) { |
| 86 | // If this is an unbridged cast, wrap the result in an implicit |
| 87 | // cast that yields the unbridged-cast placeholder type. |
| 88 | if (IsARCUnbridgedCast) { |
| 89 | castExpr = ImplicitCastExpr::Create(Self.Context, |
| 90 | Self.Context.ARCUnbridgedCastTy, |
| 91 | CK_Dependent, castExpr, 0, |
| 92 | castExpr->getValueKind()); |
| 93 | } |
| 94 | return Self.Owned(castExpr); |
| 95 | } |
| 96 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 97 | // Internal convenience methods. |
| 98 | |
| 99 | /// Try to handle the given placeholder expression kind. Return |
| 100 | /// true if the source expression has the appropriate placeholder |
| 101 | /// kind. A placeholder can only be claimed once. |
| 102 | bool claimPlaceholder(BuiltinType::Kind K) { |
| 103 | if (PlaceholderKind != K) return false; |
| 104 | |
| 105 | PlaceholderKind = (BuiltinType::Kind) 0; |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | bool isPlaceholder() const { |
| 110 | return PlaceholderKind != 0; |
| 111 | } |
| 112 | bool isPlaceholder(BuiltinType::Kind K) const { |
| 113 | return PlaceholderKind == K; |
| 114 | } |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 115 | |
| 116 | void checkCastAlign() { |
| 117 | Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange); |
| 118 | } |
| 119 | |
| 120 | void checkObjCARCConversion(Sema::CheckedConversionKind CCK) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 121 | assert(Self.getLangOptions().ObjCAutoRefCount); |
| 122 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 123 | Expr *src = SrcExpr.get(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 124 | if (Self.CheckObjCARCConversion(OpRange, DestType, src, CCK) == |
| 125 | Sema::ACR_unbridged) |
| 126 | IsARCUnbridgedCast = true; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 127 | SrcExpr = src; |
| 128 | } |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 129 | |
| 130 | /// Check for and handle non-overload placeholder expressions. |
| 131 | void checkNonOverloadPlaceholders() { |
| 132 | if (!isPlaceholder() || isPlaceholder(BuiltinType::Overload)) |
| 133 | return; |
| 134 | |
| 135 | SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.take()); |
| 136 | if (SrcExpr.isInvalid()) |
| 137 | return; |
| 138 | PlaceholderKind = (BuiltinType::Kind) 0; |
| 139 | } |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 140 | }; |
| 141 | } |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 142 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 143 | static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, |
| 144 | bool CheckCVR, bool CheckObjCLifetime); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 145 | |
| 146 | // The Try functions attempt a specific way of casting. If they succeed, they |
| 147 | // return TC_Success. If their way of casting is not appropriate for the given |
| 148 | // arguments, they return TC_NotApplicable and *may* set diag to a diagnostic |
| 149 | // to emit if no other way succeeds. If their way of casting is appropriate but |
| 150 | // fails, they return TC_Failed and *must* set diag; they can set it to 0 if |
| 151 | // they emit a specialized diagnostic. |
| 152 | // All diagnostics returned by these functions must expect the same three |
| 153 | // arguments: |
| 154 | // %0: Cast Type (a value from the CastType enumeration) |
| 155 | // %1: Source Type |
| 156 | // %2: Destination Type |
| 157 | static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, |
Douglas Gregor | 8ec14e6 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 158 | QualType DestType, bool CStyle, |
| 159 | CastKind &Kind, |
Douglas Gregor | 88b22a4 | 2011-01-25 16:13:26 +0000 | [diff] [blame] | 160 | CXXCastPath &BasePath, |
| 161 | unsigned &msg); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 162 | static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 163 | QualType DestType, bool CStyle, |
| 164 | const SourceRange &OpRange, |
| 165 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 166 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 167 | CXXCastPath &BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 168 | static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType, |
| 169 | QualType DestType, bool CStyle, |
| 170 | const SourceRange &OpRange, |
Anders Carlsson | 95c5d8a | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 171 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 172 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 173 | CXXCastPath &BasePath); |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 174 | static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType, |
| 175 | CanQualType DestType, bool CStyle, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 176 | const SourceRange &OpRange, |
| 177 | QualType OrigSrcType, |
Anders Carlsson | 95c5d8a | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 178 | QualType OrigDestType, unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 179 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 180 | CXXCastPath &BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 181 | static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 182 | QualType SrcType, |
| 183 | QualType DestType,bool CStyle, |
| 184 | const SourceRange &OpRange, |
| 185 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 186 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 187 | CXXCastPath &BasePath); |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 188 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 189 | static TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 190 | QualType DestType, |
| 191 | Sema::CheckedConversionKind CCK, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 192 | const SourceRange &OpRange, |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 193 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 194 | CastKind &Kind); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 195 | static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 196 | QualType DestType, |
| 197 | Sema::CheckedConversionKind CCK, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 198 | const SourceRange &OpRange, |
Anders Carlsson | cb3c308 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 199 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 200 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 201 | CXXCastPath &BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 202 | static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType, |
| 203 | bool CStyle, unsigned &msg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 204 | static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 205 | QualType DestType, bool CStyle, |
| 206 | const SourceRange &OpRange, |
Anders Carlsson | 3c31a39 | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 207 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 208 | CastKind &Kind); |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 209 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 210 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 211 | /// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 212 | ExprResult |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 213 | Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 214 | SourceLocation LAngleBracketLoc, Declarator &D, |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 215 | SourceLocation RAngleBracketLoc, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 216 | SourceLocation LParenLoc, Expr *E, |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 217 | SourceLocation RParenLoc) { |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 218 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 219 | assert(!D.isInvalidType()); |
| 220 | |
| 221 | TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, E->getType()); |
| 222 | if (D.isInvalidType()) |
| 223 | return ExprError(); |
| 224 | |
| 225 | if (getLangOptions().CPlusPlus) { |
| 226 | // Check that there are no default arguments (C++ only). |
| 227 | CheckExtraCXXDefaultArguments(D); |
| 228 | } |
| 229 | |
| 230 | return BuildCXXNamedCast(OpLoc, Kind, TInfo, move(E), |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 231 | SourceRange(LAngleBracketLoc, RAngleBracketLoc), |
| 232 | SourceRange(LParenLoc, RParenLoc)); |
| 233 | } |
| 234 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 235 | ExprResult |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 236 | Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 237 | TypeSourceInfo *DestTInfo, Expr *E, |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 238 | SourceRange AngleBrackets, SourceRange Parens) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 239 | ExprResult Ex = Owned(E); |
John McCall | c89724c | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 240 | QualType DestType = DestTInfo->getType(); |
| 241 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 242 | // If the type is dependent, we won't do the semantic analysis now. |
| 243 | // FIXME: should we check this in a more fine-grained manner? |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 244 | bool TypeDependent = DestType->isDependentType() || Ex.get()->isTypeDependent(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 245 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 246 | CastOperation Op(*this, DestType, E); |
| 247 | Op.OpRange = SourceRange(OpLoc, Parens.getEnd()); |
| 248 | Op.DestRange = AngleBrackets; |
John McCall | a21e06c | 2010-11-26 10:57:22 +0000 | [diff] [blame] | 249 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 250 | switch (Kind) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 251 | default: llvm_unreachable("Unknown C++ cast!"); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 252 | |
| 253 | case tok::kw_const_cast: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 254 | if (!TypeDependent) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 255 | Op.CheckConstCast(); |
| 256 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 257 | return ExprError(); |
| 258 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 259 | return Op.complete(CXXConstCastExpr::Create(Context, Op.ResultType, |
| 260 | Op.ValueKind, Op.SrcExpr.take(), DestTInfo, |
| 261 | OpLoc, Parens.getEnd())); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 262 | |
Anders Carlsson | 714179b | 2009-08-02 19:07:59 +0000 | [diff] [blame] | 263 | case tok::kw_dynamic_cast: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 264 | if (!TypeDependent) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 265 | Op.CheckDynamicCast(); |
| 266 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 267 | return ExprError(); |
| 268 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 269 | return Op.complete(CXXDynamicCastExpr::Create(Context, Op.ResultType, |
| 270 | Op.ValueKind, Op.Kind, Op.SrcExpr.take(), |
| 271 | &Op.BasePath, DestTInfo, |
| 272 | OpLoc, Parens.getEnd())); |
Anders Carlsson | 714179b | 2009-08-02 19:07:59 +0000 | [diff] [blame] | 273 | } |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 274 | case tok::kw_reinterpret_cast: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 275 | if (!TypeDependent) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 276 | Op.CheckReinterpretCast(); |
| 277 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 278 | return ExprError(); |
| 279 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 280 | return Op.complete(CXXReinterpretCastExpr::Create(Context, Op.ResultType, |
| 281 | Op.ValueKind, Op.Kind, Op.SrcExpr.take(), |
| 282 | 0, DestTInfo, OpLoc, |
| 283 | Parens.getEnd())); |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 284 | } |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 285 | case tok::kw_static_cast: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 286 | if (!TypeDependent) { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 287 | Op.CheckStaticCast(); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 288 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 289 | return ExprError(); |
| 290 | } |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 291 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 292 | return Op.complete(CXXStaticCastExpr::Create(Context, Op.ResultType, |
| 293 | Op.ValueKind, Op.Kind, Op.SrcExpr.take(), |
| 294 | &Op.BasePath, DestTInfo, |
| 295 | OpLoc, Parens.getEnd())); |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 296 | } |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 299 | return ExprError(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 300 | } |
| 301 | |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 302 | /// Try to diagnose a failed overloaded cast. Returns true if |
| 303 | /// diagnostics were emitted. |
| 304 | static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT, |
| 305 | SourceRange range, Expr *src, |
| 306 | QualType destType) { |
| 307 | switch (CT) { |
| 308 | // These cast kinds don't consider user-defined conversions. |
| 309 | case CT_Const: |
| 310 | case CT_Reinterpret: |
| 311 | case CT_Dynamic: |
| 312 | return false; |
| 313 | |
| 314 | // These do. |
| 315 | case CT_Static: |
| 316 | case CT_CStyle: |
| 317 | case CT_Functional: |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | QualType srcType = src->getType(); |
| 322 | if (!destType->isRecordType() && !srcType->isRecordType()) |
| 323 | return false; |
| 324 | |
| 325 | InitializedEntity entity = InitializedEntity::InitializeTemporary(destType); |
| 326 | InitializationKind initKind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 327 | = (CT == CT_CStyle)? InitializationKind::CreateCStyleCast(range.getBegin(), |
| 328 | range) |
| 329 | : (CT == CT_Functional)? InitializationKind::CreateFunctionalCast(range) |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 330 | : InitializationKind::CreateCast(/*type range?*/ range); |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 331 | InitializationSequence sequence(S, entity, initKind, &src, 1); |
| 332 | |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 333 | assert(sequence.Failed() && "initialization succeeded on second try?"); |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 334 | switch (sequence.getFailureKind()) { |
| 335 | default: return false; |
| 336 | |
| 337 | case InitializationSequence::FK_ConstructorOverloadFailed: |
| 338 | case InitializationSequence::FK_UserConversionOverloadFailed: |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | OverloadCandidateSet &candidates = sequence.getFailedCandidateSet(); |
| 343 | |
| 344 | unsigned msg = 0; |
| 345 | OverloadCandidateDisplayKind howManyCandidates = OCD_AllCandidates; |
| 346 | |
| 347 | switch (sequence.getFailedOverloadResult()) { |
| 348 | case OR_Success: llvm_unreachable("successful failed overload"); |
| 349 | return false; |
| 350 | case OR_No_Viable_Function: |
| 351 | if (candidates.empty()) |
| 352 | msg = diag::err_ovl_no_conversion_in_cast; |
| 353 | else |
| 354 | msg = diag::err_ovl_no_viable_conversion_in_cast; |
| 355 | howManyCandidates = OCD_AllCandidates; |
| 356 | break; |
| 357 | |
| 358 | case OR_Ambiguous: |
| 359 | msg = diag::err_ovl_ambiguous_conversion_in_cast; |
| 360 | howManyCandidates = OCD_ViableCandidates; |
| 361 | break; |
| 362 | |
| 363 | case OR_Deleted: |
| 364 | msg = diag::err_ovl_deleted_conversion_in_cast; |
| 365 | howManyCandidates = OCD_ViableCandidates; |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | S.Diag(range.getBegin(), msg) |
| 370 | << CT << srcType << destType |
| 371 | << range << src->getSourceRange(); |
| 372 | |
| 373 | candidates.NoteCandidates(S, howManyCandidates, &src, 1); |
| 374 | |
| 375 | return true; |
| 376 | } |
| 377 | |
| 378 | /// Diagnose a failed cast. |
| 379 | static void diagnoseBadCast(Sema &S, unsigned msg, CastType castType, |
| 380 | SourceRange opRange, Expr *src, QualType destType) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 381 | if (src->getType() == S.Context.BoundMemberTy) { |
| 382 | (void) S.CheckPlaceholderExpr(src); // will always fail |
| 383 | return; |
| 384 | } |
| 385 | |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 386 | if (msg == diag::err_bad_cxx_cast_generic && |
| 387 | tryDiagnoseOverloadedCast(S, castType, opRange, src, destType)) |
| 388 | return; |
| 389 | |
| 390 | S.Diag(opRange.getBegin(), msg) << castType |
| 391 | << src->getType() << destType << opRange << src->getSourceRange(); |
| 392 | } |
| 393 | |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 394 | /// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes, |
| 395 | /// this removes one level of indirection from both types, provided that they're |
| 396 | /// the same kind of pointer (plain or to-member). Unlike the Sema function, |
| 397 | /// this one doesn't care if the two pointers-to-member don't point into the |
| 398 | /// same class. This is because CastsAwayConstness doesn't care. |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 399 | static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) { |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 400 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 401 | *T2PtrType = T2->getAs<PointerType>(); |
| 402 | if (T1PtrType && T2PtrType) { |
| 403 | T1 = T1PtrType->getPointeeType(); |
| 404 | T2 = T2PtrType->getPointeeType(); |
| 405 | return true; |
| 406 | } |
Fariborz Jahanian | 72a8659 | 2010-02-03 20:32:31 +0000 | [diff] [blame] | 407 | const ObjCObjectPointerType *T1ObjCPtrType = |
| 408 | T1->getAs<ObjCObjectPointerType>(), |
| 409 | *T2ObjCPtrType = |
| 410 | T2->getAs<ObjCObjectPointerType>(); |
| 411 | if (T1ObjCPtrType) { |
| 412 | if (T2ObjCPtrType) { |
| 413 | T1 = T1ObjCPtrType->getPointeeType(); |
| 414 | T2 = T2ObjCPtrType->getPointeeType(); |
| 415 | return true; |
| 416 | } |
| 417 | else if (T2PtrType) { |
| 418 | T1 = T1ObjCPtrType->getPointeeType(); |
| 419 | T2 = T2PtrType->getPointeeType(); |
| 420 | return true; |
| 421 | } |
| 422 | } |
| 423 | else if (T2ObjCPtrType) { |
| 424 | if (T1PtrType) { |
| 425 | T2 = T2ObjCPtrType->getPointeeType(); |
| 426 | T1 = T1PtrType->getPointeeType(); |
| 427 | return true; |
| 428 | } |
| 429 | } |
| 430 | |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 431 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 432 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 433 | if (T1MPType && T2MPType) { |
| 434 | T1 = T1MPType->getPointeeType(); |
| 435 | T2 = T2MPType->getPointeeType(); |
| 436 | return true; |
| 437 | } |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 438 | |
| 439 | const BlockPointerType *T1BPType = T1->getAs<BlockPointerType>(), |
| 440 | *T2BPType = T2->getAs<BlockPointerType>(); |
| 441 | if (T1BPType && T2BPType) { |
| 442 | T1 = T1BPType->getPointeeType(); |
| 443 | T2 = T2BPType->getPointeeType(); |
| 444 | return true; |
| 445 | } |
| 446 | |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 447 | return false; |
| 448 | } |
| 449 | |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 450 | /// CastsAwayConstness - Check if the pointer conversion from SrcType to |
| 451 | /// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by |
| 452 | /// the cast checkers. Both arguments must denote pointer (possibly to member) |
| 453 | /// types. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 454 | /// |
| 455 | /// \param CheckCVR Whether to check for const/volatile/restrict qualifiers. |
| 456 | /// |
| 457 | /// \param CheckObjCLifetime Whether to check Objective-C lifetime qualifiers. |
Sebastian Redl | 5ed66f7 | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 458 | static bool |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 459 | CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, |
| 460 | bool CheckCVR, bool CheckObjCLifetime) { |
| 461 | // If the only checking we care about is for Objective-C lifetime qualifiers, |
| 462 | // and we're not in ARC mode, there's nothing to check. |
| 463 | if (!CheckCVR && CheckObjCLifetime && |
| 464 | !Self.Context.getLangOptions().ObjCAutoRefCount) |
| 465 | return false; |
| 466 | |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 467 | // Casting away constness is defined in C++ 5.2.11p8 with reference to |
| 468 | // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since |
| 469 | // the rules are non-trivial. So first we construct Tcv *...cv* as described |
| 470 | // in C++ 5.2.11p8. |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 471 | assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || |
| 472 | SrcType->isBlockPointerType()) && |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 473 | "Source type is not pointer or pointer to member."); |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 474 | assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() || |
| 475 | DestType->isBlockPointerType()) && |
Sebastian Redl | db64728 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 476 | "Destination type is not pointer or pointer to member."); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 477 | |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 478 | QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType), |
| 479 | UnwrappedDestType = Self.Context.getCanonicalType(DestType); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 480 | SmallVector<Qualifiers, 8> cv1, cv2; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 482 | // Find the qualifiers. We only care about cvr-qualifiers for the |
| 483 | // purpose of this check, because other qualifiers (address spaces, |
| 484 | // Objective-C GC, etc.) are part of the type's identity. |
Sebastian Redl | 76d69bb | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 485 | while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 486 | // Determine the relevant qualifiers at this level. |
| 487 | Qualifiers SrcQuals, DestQuals; |
Anders Carlsson | 52647c6 | 2010-06-04 22:47:55 +0000 | [diff] [blame] | 488 | Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals); |
Anders Carlsson | 52647c6 | 2010-06-04 22:47:55 +0000 | [diff] [blame] | 489 | Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 490 | |
| 491 | Qualifiers RetainedSrcQuals, RetainedDestQuals; |
| 492 | if (CheckCVR) { |
| 493 | RetainedSrcQuals.setCVRQualifiers(SrcQuals.getCVRQualifiers()); |
| 494 | RetainedDestQuals.setCVRQualifiers(DestQuals.getCVRQualifiers()); |
| 495 | } |
| 496 | |
| 497 | if (CheckObjCLifetime && |
| 498 | !DestQuals.compatiblyIncludesObjCLifetime(SrcQuals)) |
| 499 | return true; |
| 500 | |
| 501 | cv1.push_back(RetainedSrcQuals); |
| 502 | cv2.push_back(RetainedDestQuals); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 503 | } |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 504 | if (cv1.empty()) |
| 505 | return false; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 506 | |
| 507 | // Construct void pointers with those qualifiers (in reverse order of |
| 508 | // unwrapping, of course). |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 509 | QualType SrcConstruct = Self.Context.VoidTy; |
| 510 | QualType DestConstruct = Self.Context.VoidTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 511 | ASTContext &Context = Self.Context; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 512 | for (SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 513 | i2 = cv2.rbegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | i1 != cv1.rend(); ++i1, ++i2) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 515 | SrcConstruct |
| 516 | = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1)); |
| 517 | DestConstruct |
| 518 | = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2)); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // Test if they're compatible. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 522 | bool ObjCLifetimeConversion; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 523 | return SrcConstruct != DestConstruct && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 524 | !Self.IsQualificationConversion(SrcConstruct, DestConstruct, false, |
| 525 | ObjCLifetimeConversion); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 528 | /// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid. |
| 529 | /// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime- |
| 530 | /// checked downcasts in class hierarchies. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 531 | void CastOperation::CheckDynamicCast() { |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 532 | if (ValueKind == VK_RValue) |
Eli Friedman | 7a420df | 2011-10-31 20:59:03 +0000 | [diff] [blame] | 533 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 534 | else if (isPlaceholder()) |
| 535 | SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.take()); |
| 536 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 537 | return; |
Eli Friedman | 7a420df | 2011-10-31 20:59:03 +0000 | [diff] [blame] | 538 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 539 | QualType OrigSrcType = SrcExpr.get()->getType(); |
| 540 | QualType DestType = Self.Context.getCanonicalType(this->DestType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 541 | |
| 542 | // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type, |
| 543 | // or "pointer to cv void". |
| 544 | |
| 545 | QualType DestPointee; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 546 | const PointerType *DestPointer = DestType->getAs<PointerType>(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 547 | const ReferenceType *DestReference = 0; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 548 | if (DestPointer) { |
| 549 | DestPointee = DestPointer->getPointeeType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 550 | } else if ((DestReference = DestType->getAs<ReferenceType>())) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 551 | DestPointee = DestReference->getPointeeType(); |
| 552 | } else { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 553 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 554 | << this->DestType << DestRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 558 | const RecordType *DestRecord = DestPointee->getAs<RecordType>(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 559 | if (DestPointee->isVoidType()) { |
| 560 | assert(DestPointer && "Reference to void is not possible"); |
| 561 | } else if (DestRecord) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 562 | if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 563 | Self.PDiag(diag::err_bad_dynamic_cast_incomplete) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 564 | << DestRange)) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 565 | return; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 566 | } else { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 567 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 568 | << DestPointee.getUnqualifiedType() << DestRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 572 | // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to |
| 573 | // complete class type, [...]. If T is an lvalue reference type, v shall be |
Douglas Gregor | dc843f2 | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 574 | // an lvalue of a complete class type, [...]. If T is an rvalue reference |
| 575 | // type, v shall be an expression having a complete class type, [...] |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 576 | QualType SrcType = Self.Context.getCanonicalType(OrigSrcType); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 577 | QualType SrcPointee; |
| 578 | if (DestPointer) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 579 | if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 580 | SrcPointee = SrcPointer->getPointeeType(); |
| 581 | } else { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 582 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 583 | << OrigSrcType << SrcExpr.get()->getSourceRange(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 584 | return; |
| 585 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 586 | } else if (DestReference->isLValueReferenceType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 587 | if (!SrcExpr.get()->isLValue()) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 588 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 589 | << CT_Dynamic << OrigSrcType << this->DestType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 590 | } |
| 591 | SrcPointee = SrcType; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 592 | } else { |
| 593 | SrcPointee = SrcType; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 596 | const RecordType *SrcRecord = SrcPointee->getAs<RecordType>(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 597 | if (SrcRecord) { |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 598 | if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 599 | Self.PDiag(diag::err_bad_dynamic_cast_incomplete) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 600 | << SrcExpr.get()->getSourceRange())) |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 601 | return; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 602 | } else { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 603 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 604 | << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 605 | return; |
| 606 | } |
| 607 | |
| 608 | assert((DestPointer || DestReference) && |
| 609 | "Bad destination non-ptr/ref slipped through."); |
| 610 | assert((DestRecord || DestPointee->isVoidType()) && |
| 611 | "Bad destination pointee slipped through."); |
| 612 | assert(SrcRecord && "Bad source pointee slipped through."); |
| 613 | |
| 614 | // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness. |
| 615 | if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 616 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 617 | << CT_Dynamic << OrigSrcType << this->DestType << OpRange; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 618 | return; |
| 619 | } |
| 620 | |
| 621 | // C++ 5.2.7p3: If the type of v is the same as the required result type, |
| 622 | // [except for cv]. |
| 623 | if (DestRecord == SrcRecord) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 624 | Kind = CK_NoOp; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 625 | return; |
| 626 | } |
| 627 | |
| 628 | // C++ 5.2.7p5 |
| 629 | // Upcasts are resolved statically. |
Sebastian Redl | 37d6de3 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 630 | if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 631 | if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee, |
| 632 | OpRange.getBegin(), OpRange, |
| 633 | &BasePath)) |
| 634 | return; |
| 635 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 636 | Kind = CK_DerivedToBase; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 637 | |
| 638 | // If we are casting to or through a virtual base class, we need a |
| 639 | // vtable. |
| 640 | if (Self.BasePathInvolvesVirtualBase(BasePath)) |
| 641 | Self.MarkVTableUsed(OpRange.getBegin(), |
| 642 | cast<CXXRecordDecl>(SrcRecord->getDecl())); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 643 | return; |
| 644 | } |
| 645 | |
| 646 | // C++ 5.2.7p6: Otherwise, v shall be [polymorphic]. |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 647 | const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition(); |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 648 | assert(SrcDecl && "Definition missing"); |
| 649 | if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 650 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 651 | << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 652 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 653 | Self.MarkVTableUsed(OpRange.getBegin(), |
| 654 | cast<CXXRecordDecl>(SrcRecord->getDecl())); |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 655 | |
| 656 | // Done. Everything else is run-time checks. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 657 | Kind = CK_Dynamic; |
Sebastian Redl | 26d85b1 | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 658 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 659 | |
| 660 | /// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid. |
| 661 | /// Refer to C++ 5.2.11 for details. const_cast is typically used in code |
| 662 | /// like this: |
| 663 | /// const char *str = "literal"; |
| 664 | /// legacy_function(const_cast\<char*\>(str)); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 665 | void CastOperation::CheckConstCast() { |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 666 | if (ValueKind == VK_RValue) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 667 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 668 | else if (isPlaceholder()) |
| 669 | SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.take()); |
| 670 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 671 | return; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 672 | |
| 673 | unsigned msg = diag::err_bad_cxx_cast_generic; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 674 | if (TryConstCast(Self, SrcExpr.get(), DestType, /*CStyle*/false, msg) != TC_Success |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 675 | && msg != 0) |
| 676 | Self.Diag(OpRange.getBegin(), msg) << CT_Const |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 677 | << SrcExpr.get()->getType() << DestType << OpRange; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is |
| 681 | /// valid. |
| 682 | /// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code |
| 683 | /// like this: |
| 684 | /// char *bytes = reinterpret_cast\<char*\>(int_ptr); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 685 | void CastOperation::CheckReinterpretCast() { |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 686 | if (ValueKind == VK_RValue && !isPlaceholder(BuiltinType::Overload)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 687 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
Eli Friedman | ed0b31f | 2012-01-12 00:44:34 +0000 | [diff] [blame] | 688 | else |
| 689 | checkNonOverloadPlaceholders(); |
| 690 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 691 | return; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 692 | |
| 693 | unsigned msg = diag::err_bad_cxx_cast_generic; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 694 | TryCastResult tcr = |
| 695 | TryReinterpretCast(Self, SrcExpr, DestType, |
| 696 | /*CStyle*/false, OpRange, msg, Kind); |
| 697 | if (tcr != TC_Success && msg != 0) |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 698 | { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 699 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 700 | return; |
| 701 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 702 | //FIXME: &f<int>; is overloaded and resolvable |
| 703 | Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 704 | << OverloadExpr::find(SrcExpr.get()).Expression->getName() |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 705 | << DestType << OpRange; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 706 | Self.NoteAllOverloadCandidates(SrcExpr.get()); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 707 | |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 708 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 709 | diagnoseBadCast(Self, msg, CT_Reinterpret, OpRange, SrcExpr.get(), DestType); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 710 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 711 | } else if (tcr == TC_Success && Self.getLangOptions().ObjCAutoRefCount) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 712 | checkObjCARCConversion(Sema::CCK_OtherCast); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 713 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | |
| 717 | /// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid. |
| 718 | /// Refer to C++ 5.2.9 for details. Static casts are mostly used for making |
| 719 | /// implicit conversions explicit and getting rid of data loss warnings. |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 720 | void CastOperation::CheckStaticCast() { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 721 | if (isPlaceholder()) { |
| 722 | checkNonOverloadPlaceholders(); |
| 723 | if (SrcExpr.isInvalid()) |
| 724 | return; |
| 725 | } |
| 726 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 727 | // This test is outside everything else because it's the only case where |
| 728 | // a non-lvalue-reference target type does not lead to decay. |
| 729 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
Eli Friedman | 05d9d7a | 2009-11-16 05:44:20 +0000 | [diff] [blame] | 730 | if (DestType->isVoidType()) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 731 | Kind = CK_ToVoid; |
| 732 | |
| 733 | if (claimPlaceholder(BuiltinType::Overload)) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 734 | Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 735 | false, // Decay Function to ptr |
| 736 | true, // Complain |
| 737 | OpRange, DestType, diag::err_bad_static_cast_overload); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 738 | if (SrcExpr.isInvalid()) |
| 739 | return; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 740 | } |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 741 | |
| 742 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 743 | return; |
Eli Friedman | 05d9d7a | 2009-11-16 05:44:20 +0000 | [diff] [blame] | 744 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 745 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 746 | if (ValueKind == VK_RValue && !DestType->isRecordType() && |
| 747 | !isPlaceholder(BuiltinType::Overload)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 748 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
| 749 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 750 | return; |
| 751 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 752 | |
| 753 | unsigned msg = diag::err_bad_cxx_cast_generic; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 754 | TryCastResult tcr |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 755 | = TryStaticCast(Self, SrcExpr, DestType, Sema::CCK_OtherCast, OpRange, msg, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 756 | Kind, BasePath); |
| 757 | if (tcr != TC_Success && msg != 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 758 | if (SrcExpr.isInvalid()) |
| 759 | return; |
| 760 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
| 761 | OverloadExpr* oe = OverloadExpr::find(SrcExpr.get()).Expression; |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 762 | Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 763 | << oe->getName() << DestType << OpRange |
| 764 | << oe->getQualifierLoc().getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 765 | Self.NoteAllOverloadCandidates(SrcExpr.get()); |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 766 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 767 | diagnoseBadCast(Self, msg, CT_Static, OpRange, SrcExpr.get(), DestType); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 768 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 769 | } else if (tcr == TC_Success) { |
| 770 | if (Kind == CK_BitCast) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 771 | checkCastAlign(); |
| 772 | if (Self.getLangOptions().ObjCAutoRefCount) |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 773 | checkObjCARCConversion(Sema::CCK_OtherCast); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 774 | } else if (Kind == CK_BitCast) { |
| 775 | checkCastAlign(); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 776 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /// TryStaticCast - Check if a static cast can be performed, and do so if |
| 780 | /// possible. If @p CStyle, ignore access restrictions on hierarchy casting |
| 781 | /// and casting away constness. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 782 | static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 783 | QualType DestType, |
| 784 | Sema::CheckedConversionKind CCK, |
Anders Carlsson | cb3c308 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 785 | const SourceRange &OpRange, unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 786 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 787 | CXXCastPath &BasePath) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 788 | // Determine whether we have the semantics of a C-style cast. |
| 789 | bool CStyle |
| 790 | = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast); |
| 791 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 792 | // The order the tests is not entirely arbitrary. There is one conversion |
| 793 | // that can be handled in two different ways. Given: |
| 794 | // struct A {}; |
| 795 | // struct B : public A { |
| 796 | // B(); B(const A&); |
| 797 | // }; |
| 798 | // const A &a = B(); |
| 799 | // the cast static_cast<const B&>(a) could be seen as either a static |
| 800 | // reference downcast, or an explicit invocation of the user-defined |
| 801 | // conversion using B's conversion constructor. |
| 802 | // DR 427 specifies that the downcast is to be applied here. |
| 803 | |
| 804 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
| 805 | // Done outside this function. |
| 806 | |
| 807 | TryCastResult tcr; |
| 808 | |
| 809 | // C++ 5.2.9p5, reference downcast. |
| 810 | // See the function for details. |
| 811 | // DR 427 specifies that this is to be applied before paragraph 2. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 812 | tcr = TryStaticReferenceDowncast(Self, SrcExpr.get(), DestType, CStyle, OpRange, |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 813 | msg, Kind, BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 814 | if (tcr != TC_NotApplicable) |
| 815 | return tcr; |
| 816 | |
Douglas Gregor | dc843f2 | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 817 | // C++0x [expr.static.cast]p3: |
| 818 | // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2 |
| 819 | // T2" if "cv2 T2" is reference-compatible with "cv1 T1". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 820 | tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind, BasePath, |
Douglas Gregor | 8ec14e6 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 821 | msg); |
Douglas Gregor | 88b22a4 | 2011-01-25 16:13:26 +0000 | [diff] [blame] | 822 | if (tcr != TC_NotApplicable) |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 823 | return tcr; |
| 824 | |
| 825 | // C++ 5.2.9p2: An expression e can be explicitly converted to a type T |
| 826 | // [...] if the declaration "T t(e);" is well-formed, [...]. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 827 | tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CCK, OpRange, msg, |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 828 | Kind); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 829 | if (SrcExpr.isInvalid()) |
| 830 | return TC_Failed; |
Anders Carlsson | 3c31a39 | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 831 | if (tcr != TC_NotApplicable) |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 832 | return tcr; |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 833 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 834 | // C++ 5.2.9p6: May apply the reverse of any standard conversion, except |
| 835 | // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean |
| 836 | // conversions, subject to further restrictions. |
| 837 | // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal |
| 838 | // of qualification conversions impossible. |
| 839 | // In the CStyle case, the earlier attempt to const_cast should have taken |
| 840 | // care of reverse qualification conversions. |
| 841 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 842 | QualType SrcType = Self.Context.getCanonicalType(SrcExpr.get()->getType()); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 843 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 844 | // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly |
Douglas Gregor | 1e856d9 | 2011-02-18 03:01:41 +0000 | [diff] [blame] | 845 | // converted to an integral type. [...] A value of a scoped enumeration type |
| 846 | // can also be explicitly converted to a floating-point type [...]. |
| 847 | if (const EnumType *Enum = SrcType->getAs<EnumType>()) { |
| 848 | if (Enum->getDecl()->isScoped()) { |
| 849 | if (DestType->isBooleanType()) { |
| 850 | Kind = CK_IntegralToBoolean; |
| 851 | return TC_Success; |
| 852 | } else if (DestType->isIntegralType(Self.Context)) { |
| 853 | Kind = CK_IntegralCast; |
| 854 | return TC_Success; |
| 855 | } else if (DestType->isRealFloatingType()) { |
| 856 | Kind = CK_IntegralToFloating; |
| 857 | return TC_Success; |
| 858 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
Douglas Gregor | 1e856d9 | 2011-02-18 03:01:41 +0000 | [diff] [blame] | 861 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 862 | // Reverse integral promotion/conversion. All such conversions are themselves |
| 863 | // again integral promotions or conversions and are thus already handled by |
| 864 | // p2 (TryDirectInitialization above). |
| 865 | // (Note: any data loss warnings should be suppressed.) |
| 866 | // The exception is the reverse of enum->integer, i.e. integer->enum (and |
| 867 | // enum->enum). See also C++ 5.2.9p7. |
| 868 | // The same goes for reverse floating point promotion/conversion and |
| 869 | // floating-integral conversions. Again, only floating->enum is relevant. |
| 870 | if (DestType->isEnumeralType()) { |
Eli Friedman | cc2fca2 | 2011-09-02 17:38:59 +0000 | [diff] [blame] | 871 | if (SrcType->isIntegralOrEnumerationType()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 872 | Kind = CK_IntegralCast; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 873 | return TC_Success; |
Eli Friedman | cc2fca2 | 2011-09-02 17:38:59 +0000 | [diff] [blame] | 874 | } else if (SrcType->isRealFloatingType()) { |
| 875 | Kind = CK_FloatingToIntegral; |
| 876 | return TC_Success; |
Eli Friedman | 05d9d7a | 2009-11-16 05:44:20 +0000 | [diff] [blame] | 877 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast. |
| 881 | // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance. |
Anders Carlsson | 95c5d8a | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 882 | tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg, |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 883 | Kind, BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 884 | if (tcr != TC_NotApplicable) |
| 885 | return tcr; |
| 886 | |
| 887 | // Reverse member pointer conversion. C++ 4.11 specifies member pointer |
| 888 | // conversion. C++ 5.2.9p9 has additional information. |
| 889 | // DR54's access restrictions apply here also. |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 890 | tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 891 | OpRange, msg, Kind, BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 892 | if (tcr != TC_NotApplicable) |
| 893 | return tcr; |
| 894 | |
| 895 | // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to |
| 896 | // void*. C++ 5.2.9p10 specifies additional restrictions, which really is |
| 897 | // just the usual constness stuff. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 898 | if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 899 | QualType SrcPointee = SrcPointer->getPointeeType(); |
| 900 | if (SrcPointee->isVoidType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 901 | if (const PointerType *DestPointer = DestType->getAs<PointerType>()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 902 | QualType DestPointee = DestPointer->getPointeeType(); |
| 903 | if (DestPointee->isIncompleteOrObjectType()) { |
| 904 | // This is definitely the intended conversion, but it might fail due |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 905 | // to a qualifier violation. Note that we permit Objective-C lifetime |
| 906 | // and GC qualifier mismatches here. |
| 907 | if (!CStyle) { |
| 908 | Qualifiers DestPointeeQuals = DestPointee.getQualifiers(); |
| 909 | Qualifiers SrcPointeeQuals = SrcPointee.getQualifiers(); |
| 910 | DestPointeeQuals.removeObjCGCAttr(); |
| 911 | DestPointeeQuals.removeObjCLifetime(); |
| 912 | SrcPointeeQuals.removeObjCGCAttr(); |
| 913 | SrcPointeeQuals.removeObjCLifetime(); |
| 914 | if (DestPointeeQuals != SrcPointeeQuals && |
| 915 | !DestPointeeQuals.compatiblyIncludes(SrcPointeeQuals)) { |
| 916 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
| 917 | return TC_Failed; |
| 918 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 919 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 920 | Kind = CK_BitCast; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 921 | return TC_Success; |
| 922 | } |
| 923 | } |
Fariborz Jahanian | 2f6c550 | 2010-05-10 23:46:53 +0000 | [diff] [blame] | 924 | else if (DestType->isObjCObjectPointerType()) { |
| 925 | // allow both c-style cast and static_cast of objective-c pointers as |
| 926 | // they are pervasive. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 927 | Kind = CK_CPointerToObjCPointerCast; |
Fariborz Jahanian | 92ef5d7 | 2009-12-08 23:09:15 +0000 | [diff] [blame] | 928 | return TC_Success; |
| 929 | } |
Fariborz Jahanian | 3b27f1a | 2009-12-11 22:40:48 +0000 | [diff] [blame] | 930 | else if (CStyle && DestType->isBlockPointerType()) { |
| 931 | // allow c-style cast of void * to block pointers. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 932 | Kind = CK_AnyPointerToBlockPointerCast; |
Fariborz Jahanian | 3b27f1a | 2009-12-11 22:40:48 +0000 | [diff] [blame] | 933 | return TC_Success; |
| 934 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
Fariborz Jahanian | 65267b2 | 2010-05-12 18:16:59 +0000 | [diff] [blame] | 937 | // Allow arbitray objective-c pointer conversion with static casts. |
| 938 | if (SrcType->isObjCObjectPointerType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 939 | DestType->isObjCObjectPointerType()) { |
| 940 | Kind = CK_BitCast; |
Fariborz Jahanian | 65267b2 | 2010-05-12 18:16:59 +0000 | [diff] [blame] | 941 | return TC_Success; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 942 | } |
Fariborz Jahanian | 65267b2 | 2010-05-12 18:16:59 +0000 | [diff] [blame] | 943 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 944 | // We tried everything. Everything! Nothing works! :-( |
| 945 | return TC_NotApplicable; |
| 946 | } |
| 947 | |
| 948 | /// Tests whether a conversion according to N2844 is valid. |
| 949 | TryCastResult |
| 950 | TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType, |
Douglas Gregor | 8ec14e6 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 951 | bool CStyle, CastKind &Kind, CXXCastPath &BasePath, |
| 952 | unsigned &msg) { |
Douglas Gregor | dc843f2 | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 953 | // C++0x [expr.static.cast]p3: |
| 954 | // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to |
| 955 | // cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 956 | const RValueReferenceType *R = DestType->getAs<RValueReferenceType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 957 | if (!R) |
| 958 | return TC_NotApplicable; |
| 959 | |
Douglas Gregor | dc843f2 | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 960 | if (!SrcExpr->isGLValue()) |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 961 | return TC_NotApplicable; |
| 962 | |
| 963 | // Because we try the reference downcast before this function, from now on |
| 964 | // this is the only cast possibility, so we issue an error if we fail now. |
| 965 | // FIXME: Should allow casting away constness if CStyle. |
| 966 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 967 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 968 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8ec14e6 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 969 | QualType FromType = SrcExpr->getType(); |
| 970 | QualType ToType = R->getPointeeType(); |
| 971 | if (CStyle) { |
| 972 | FromType = FromType.getUnqualifiedType(); |
| 973 | ToType = ToType.getUnqualifiedType(); |
| 974 | } |
| 975 | |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 976 | if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(), |
Douglas Gregor | 8ec14e6 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 977 | ToType, FromType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 978 | DerivedToBase, ObjCConversion, |
| 979 | ObjCLifetimeConversion) |
| 980 | < Sema::Ref_Compatible_With_Added_Qualification) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 981 | msg = diag::err_bad_lvalue_to_rvalue_cast; |
| 982 | return TC_Failed; |
| 983 | } |
| 984 | |
Douglas Gregor | 88b22a4 | 2011-01-25 16:13:26 +0000 | [diff] [blame] | 985 | if (DerivedToBase) { |
| 986 | Kind = CK_DerivedToBase; |
| 987 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 988 | /*DetectVirtual=*/true); |
| 989 | if (!Self.IsDerivedFrom(SrcExpr->getType(), R->getPointeeType(), Paths)) |
| 990 | return TC_NotApplicable; |
| 991 | |
| 992 | Self.BuildBasePathArray(Paths, BasePath); |
| 993 | } else |
| 994 | Kind = CK_NoOp; |
| 995 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 996 | return TC_Success; |
| 997 | } |
| 998 | |
| 999 | /// Tests whether a conversion according to C++ 5.2.9p5 is valid. |
| 1000 | TryCastResult |
| 1001 | TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType, |
| 1002 | bool CStyle, const SourceRange &OpRange, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1003 | unsigned &msg, CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1004 | CXXCastPath &BasePath) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1005 | // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be |
| 1006 | // cast to type "reference to cv2 D", where D is a class derived from B, |
| 1007 | // if a valid standard conversion from "pointer to D" to "pointer to B" |
| 1008 | // exists, cv2 >= cv1, and B is not a virtual base class of D. |
| 1009 | // In addition, DR54 clarifies that the base must be accessible in the |
| 1010 | // current context. Although the wording of DR54 only applies to the pointer |
| 1011 | // variant of this rule, the intent is clearly for it to apply to the this |
| 1012 | // conversion as well. |
| 1013 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1014 | const ReferenceType *DestReference = DestType->getAs<ReferenceType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1015 | if (!DestReference) { |
| 1016 | return TC_NotApplicable; |
| 1017 | } |
| 1018 | bool RValueRef = DestReference->isRValueReferenceType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1019 | if (!RValueRef && !SrcExpr->isLValue()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1020 | // We know the left side is an lvalue reference, so we can suggest a reason. |
| 1021 | msg = diag::err_bad_cxx_cast_rvalue; |
| 1022 | return TC_NotApplicable; |
| 1023 | } |
| 1024 | |
| 1025 | QualType DestPointee = DestReference->getPointeeType(); |
| 1026 | |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1027 | return TryStaticDowncast(Self, |
| 1028 | Self.Context.getCanonicalType(SrcExpr->getType()), |
| 1029 | Self.Context.getCanonicalType(DestPointee), CStyle, |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1030 | OpRange, SrcExpr->getType(), DestType, msg, Kind, |
| 1031 | BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /// Tests whether a conversion according to C++ 5.2.9p8 is valid. |
| 1035 | TryCastResult |
| 1036 | TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | bool CStyle, const SourceRange &OpRange, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1038 | unsigned &msg, CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1039 | CXXCastPath &BasePath) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1040 | // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class |
| 1041 | // type, can be converted to an rvalue of type "pointer to cv2 D", where D |
| 1042 | // is a class derived from B, if a valid standard conversion from "pointer |
| 1043 | // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base |
| 1044 | // class of D. |
| 1045 | // In addition, DR54 clarifies that the base must be accessible in the |
| 1046 | // current context. |
| 1047 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1048 | const PointerType *DestPointer = DestType->getAs<PointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1049 | if (!DestPointer) { |
| 1050 | return TC_NotApplicable; |
| 1051 | } |
| 1052 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1053 | const PointerType *SrcPointer = SrcType->getAs<PointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1054 | if (!SrcPointer) { |
| 1055 | msg = diag::err_bad_static_cast_pointer_nonpointer; |
| 1056 | return TC_NotApplicable; |
| 1057 | } |
| 1058 | |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1059 | return TryStaticDowncast(Self, |
| 1060 | Self.Context.getCanonicalType(SrcPointer->getPointeeType()), |
| 1061 | Self.Context.getCanonicalType(DestPointer->getPointeeType()), |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1062 | CStyle, OpRange, SrcType, DestType, msg, Kind, |
| 1063 | BasePath); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and |
| 1067 | /// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1068 | /// DestType is possible and allowed. |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1069 | TryCastResult |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1070 | TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1071 | bool CStyle, const SourceRange &OpRange, QualType OrigSrcType, |
Anders Carlsson | 95c5d8a | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 1072 | QualType OrigDestType, unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1073 | CastKind &Kind, CXXCastPath &BasePath) { |
Sebastian Redl | 5ed66f7 | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 1074 | // We can only work with complete types. But don't complain if it doesn't work |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 1075 | if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) || |
| 1076 | Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0))) |
Sebastian Redl | 5ed66f7 | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 1077 | return TC_NotApplicable; |
| 1078 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1079 | // Downcast can only happen in class hierarchies, so we need classes. |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1080 | if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1081 | return TC_NotApplicable; |
| 1082 | } |
| 1083 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1084 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1085 | /*DetectVirtual=*/true); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1086 | if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) { |
| 1087 | return TC_NotApplicable; |
| 1088 | } |
| 1089 | |
| 1090 | // Target type does derive from source type. Now we're serious. If an error |
| 1091 | // appears now, it's not ignored. |
| 1092 | // This may not be entirely in line with the standard. Take for example: |
| 1093 | // struct A {}; |
| 1094 | // struct B : virtual A { |
| 1095 | // B(A&); |
| 1096 | // }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | // |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1098 | // void f() |
| 1099 | // { |
| 1100 | // (void)static_cast<const B&>(*((A*)0)); |
| 1101 | // } |
| 1102 | // As far as the standard is concerned, p5 does not apply (A is virtual), so |
| 1103 | // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid. |
| 1104 | // However, both GCC and Comeau reject this example, and accepting it would |
| 1105 | // mean more complex code if we're to preserve the nice error message. |
| 1106 | // FIXME: Being 100% compliant here would be nice to have. |
| 1107 | |
| 1108 | // Must preserve cv, as always, unless we're in C-style mode. |
| 1109 | if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1110 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1111 | return TC_Failed; |
| 1112 | } |
| 1113 | |
| 1114 | if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) { |
| 1115 | // This code is analoguous to that in CheckDerivedToBaseConversion, except |
| 1116 | // that it builds the paths in reverse order. |
| 1117 | // To sum up: record all paths to the base and build a nice string from |
| 1118 | // them. Use it to spice up the error message. |
| 1119 | if (!Paths.isRecordingPaths()) { |
| 1120 | Paths.clear(); |
| 1121 | Paths.setRecordingPaths(true); |
| 1122 | Self.IsDerivedFrom(DestType, SrcType, Paths); |
| 1123 | } |
| 1124 | std::string PathDisplayStr; |
| 1125 | std::set<unsigned> DisplayedPaths; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1126 | for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1127 | PI != PE; ++PI) { |
| 1128 | if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) { |
| 1129 | // We haven't displayed a path to this particular base |
| 1130 | // class subobject yet. |
| 1131 | PathDisplayStr += "\n "; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1132 | for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(), |
| 1133 | EE = PI->rend(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1134 | EI != EE; ++EI) |
| 1135 | PathDisplayStr += EI->Base->getType().getAsString() + " -> "; |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1136 | PathDisplayStr += QualType(DestType).getAsString(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast) |
Douglas Gregor | ab15d0e | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1141 | << QualType(SrcType).getUnqualifiedType() |
| 1142 | << QualType(DestType).getUnqualifiedType() |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1143 | << PathDisplayStr << OpRange; |
| 1144 | msg = 0; |
| 1145 | return TC_Failed; |
| 1146 | } |
| 1147 | |
| 1148 | if (Paths.getDetectedVirtual() != 0) { |
| 1149 | QualType VirtualBase(Paths.getDetectedVirtual(), 0); |
| 1150 | Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual) |
| 1151 | << OrigSrcType << OrigDestType << VirtualBase << OpRange; |
| 1152 | msg = 0; |
| 1153 | return TC_Failed; |
| 1154 | } |
| 1155 | |
John McCall | 417d39f | 2011-02-14 23:21:33 +0000 | [diff] [blame] | 1156 | if (!CStyle) { |
| 1157 | switch (Self.CheckBaseClassAccess(OpRange.getBegin(), |
| 1158 | SrcType, DestType, |
| 1159 | Paths.front(), |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1160 | diag::err_downcast_from_inaccessible_base)) { |
John McCall | 417d39f | 2011-02-14 23:21:33 +0000 | [diff] [blame] | 1161 | case Sema::AR_accessible: |
| 1162 | case Sema::AR_delayed: // be optimistic |
| 1163 | case Sema::AR_dependent: // be optimistic |
| 1164 | break; |
| 1165 | |
| 1166 | case Sema::AR_inaccessible: |
| 1167 | msg = 0; |
| 1168 | return TC_Failed; |
| 1169 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1172 | Self.BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1173 | Kind = CK_BaseToDerived; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1174 | return TC_Success; |
| 1175 | } |
| 1176 | |
| 1177 | /// TryStaticMemberPointerUpcast - Tests whether a conversion according to |
| 1178 | /// C++ 5.2.9p9 is valid: |
| 1179 | /// |
| 1180 | /// An rvalue of type "pointer to member of D of type cv1 T" can be |
| 1181 | /// converted to an rvalue of type "pointer to member of B of type cv2 T", |
| 1182 | /// where B is a base class of D [...]. |
| 1183 | /// |
| 1184 | TryCastResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1185 | TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, QualType SrcType, |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1186 | QualType DestType, bool CStyle, |
| 1187 | const SourceRange &OpRange, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1188 | unsigned &msg, CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1189 | CXXCastPath &BasePath) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1190 | const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1191 | if (!DestMemPtr) |
| 1192 | return TC_NotApplicable; |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1193 | |
| 1194 | bool WasOverloadedFunction = false; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1195 | DeclAccessPair FoundOverload; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1196 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 1197 | if (FunctionDecl *Fn |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1198 | = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), DestType, false, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 1199 | FoundOverload)) { |
| 1200 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 1201 | SrcType = Self.Context.getMemberPointerType(Fn->getType(), |
| 1202 | Self.Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 1203 | WasOverloadedFunction = true; |
| 1204 | } |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1205 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 1206 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1207 | const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1208 | if (!SrcMemPtr) { |
| 1209 | msg = diag::err_bad_static_cast_member_pointer_nonmp; |
| 1210 | return TC_NotApplicable; |
| 1211 | } |
| 1212 | |
| 1213 | // T == T, modulo cv |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1214 | if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(), |
| 1215 | DestMemPtr->getPointeeType())) |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1216 | return TC_NotApplicable; |
| 1217 | |
| 1218 | // B base of D |
| 1219 | QualType SrcClass(SrcMemPtr->getClass(), 0); |
| 1220 | QualType DestClass(DestMemPtr->getClass(), 0); |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1221 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1222 | /*DetectVirtual=*/true); |
| 1223 | if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) { |
| 1224 | return TC_NotApplicable; |
| 1225 | } |
| 1226 | |
| 1227 | // B is a base of D. But is it an allowed base? If not, it's a hard error. |
Douglas Gregor | e0d5fe2 | 2010-05-21 20:29:55 +0000 | [diff] [blame] | 1228 | if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1229 | Paths.clear(); |
| 1230 | Paths.setRecordingPaths(true); |
| 1231 | bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths); |
| 1232 | assert(StillOkay); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1233 | (void)StillOkay; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1234 | std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths); |
| 1235 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv) |
| 1236 | << 1 << SrcClass << DestClass << PathDisplayStr << OpRange; |
| 1237 | msg = 0; |
| 1238 | return TC_Failed; |
| 1239 | } |
| 1240 | |
| 1241 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
| 1242 | Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual) |
| 1243 | << SrcClass << DestClass << QualType(VBase, 0) << OpRange; |
| 1244 | msg = 0; |
| 1245 | return TC_Failed; |
| 1246 | } |
| 1247 | |
John McCall | 417d39f | 2011-02-14 23:21:33 +0000 | [diff] [blame] | 1248 | if (!CStyle) { |
| 1249 | switch (Self.CheckBaseClassAccess(OpRange.getBegin(), |
| 1250 | DestClass, SrcClass, |
| 1251 | Paths.front(), |
| 1252 | diag::err_upcast_to_inaccessible_base)) { |
| 1253 | case Sema::AR_accessible: |
| 1254 | case Sema::AR_delayed: |
| 1255 | case Sema::AR_dependent: |
| 1256 | // Optimistically assume that the delayed and dependent cases |
| 1257 | // will work out. |
| 1258 | break; |
| 1259 | |
| 1260 | case Sema::AR_inaccessible: |
| 1261 | msg = 0; |
| 1262 | return TC_Failed; |
| 1263 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1266 | if (WasOverloadedFunction) { |
| 1267 | // Resolve the address of the overloaded function again, this time |
| 1268 | // allowing complaints if something goes wrong. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1269 | FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1270 | DestType, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1271 | true, |
| 1272 | FoundOverload); |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1273 | if (!Fn) { |
| 1274 | msg = 0; |
| 1275 | return TC_Failed; |
| 1276 | } |
| 1277 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1278 | SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1279 | if (!SrcExpr.isUsable()) { |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1280 | msg = 0; |
| 1281 | return TC_Failed; |
| 1282 | } |
| 1283 | } |
| 1284 | |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1285 | Self.BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1286 | Kind = CK_DerivedToBaseMemberPointer; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1287 | return TC_Success; |
| 1288 | } |
| 1289 | |
| 1290 | /// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 |
| 1291 | /// is valid: |
| 1292 | /// |
| 1293 | /// An expression e can be explicitly converted to a type T using a |
| 1294 | /// @c static_cast if the declaration "T t(e);" is well-formed [...]. |
| 1295 | TryCastResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1296 | TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1297 | Sema::CheckedConversionKind CCK, |
| 1298 | const SourceRange &OpRange, unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1299 | CastKind &Kind) { |
Anders Carlsson | d851b37 | 2009-09-07 18:25:47 +0000 | [diff] [blame] | 1300 | if (DestType->isRecordType()) { |
| 1301 | if (Self.RequireCompleteType(OpRange.getBegin(), DestType, |
| 1302 | diag::err_bad_dynamic_cast_incomplete)) { |
| 1303 | msg = 0; |
| 1304 | return TC_Failed; |
| 1305 | } |
| 1306 | } |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1307 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 1308 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType); |
| 1309 | InitializationKind InitKind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1310 | = (CCK == Sema::CCK_CStyleCast) |
| 1311 | ? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange) |
| 1312 | : (CCK == Sema::CCK_FunctionalCast) |
| 1313 | ? InitializationKind::CreateFunctionalCast(OpRange) |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 1314 | : InitializationKind::CreateCast(OpRange); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1315 | Expr *SrcExprRaw = SrcExpr.get(); |
| 1316 | InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExprRaw, 1); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1317 | |
| 1318 | // At this point of CheckStaticCast, if the destination is a reference, |
| 1319 | // or the expression is an overload expression this has to work. |
| 1320 | // There is no other way that works. |
| 1321 | // On the other hand, if we're checking a C-style cast, we've still got |
| 1322 | // the reinterpret_cast way. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1323 | bool CStyle |
| 1324 | = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 1325 | if (InitSeq.Failed() && (CStyle || !DestType->isReferenceType())) |
Anders Carlsson | 3c31a39 | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 1326 | return TC_NotApplicable; |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1327 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1328 | ExprResult Result |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1329 | = InitSeq.Perform(Self, Entity, InitKind, MultiExprArg(Self, &SrcExprRaw, 1)); |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 1330 | if (Result.isInvalid()) { |
| 1331 | msg = 0; |
| 1332 | return TC_Failed; |
| 1333 | } |
| 1334 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1335 | if (InitSeq.isConstructorInitialization()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1336 | Kind = CK_ConstructorConversion; |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1337 | else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1338 | Kind = CK_NoOp; |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1339 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1340 | SrcExpr = move(Result); |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 1341 | return TC_Success; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | /// TryConstCast - See if a const_cast from source to destination is allowed, |
| 1345 | /// and perform it if it is. |
| 1346 | static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType, |
| 1347 | bool CStyle, unsigned &msg) { |
| 1348 | DestType = Self.Context.getCanonicalType(DestType); |
| 1349 | QualType SrcType = SrcExpr->getType(); |
Douglas Gregor | 575d2a3 | 2011-01-22 00:19:52 +0000 | [diff] [blame] | 1350 | if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) { |
| 1351 | if (DestTypeTmp->isLValueReferenceType() && !SrcExpr->isLValue()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1352 | // Cannot const_cast non-lvalue to lvalue reference type. But if this |
| 1353 | // is C-style, static_cast might find a way, so we simply suggest a |
| 1354 | // message and tell the parent to keep searching. |
| 1355 | msg = diag::err_bad_cxx_cast_rvalue; |
| 1356 | return TC_NotApplicable; |
| 1357 | } |
| 1358 | |
| 1359 | // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2 |
| 1360 | // [...] if a pointer to T1 can be [cast] to the type pointer to T2. |
| 1361 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); |
| 1362 | SrcType = Self.Context.getPointerType(SrcType); |
| 1363 | } |
| 1364 | |
| 1365 | // C++ 5.2.11p5: For a const_cast involving pointers to data members [...] |
| 1366 | // the rules for const_cast are the same as those used for pointers. |
| 1367 | |
John McCall | d425d2b | 2010-05-18 09:35:29 +0000 | [diff] [blame] | 1368 | if (!DestType->isPointerType() && |
| 1369 | !DestType->isMemberPointerType() && |
| 1370 | !DestType->isObjCObjectPointerType()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1371 | // Cannot cast to non-pointer, non-reference type. Note that, if DestType |
| 1372 | // was a reference type, we converted it to a pointer above. |
| 1373 | // The status of rvalue references isn't entirely clear, but it looks like |
| 1374 | // conversion to them is simply invalid. |
| 1375 | // C++ 5.2.11p3: For two pointer types [...] |
| 1376 | if (!CStyle) |
| 1377 | msg = diag::err_bad_const_cast_dest; |
| 1378 | return TC_NotApplicable; |
| 1379 | } |
| 1380 | if (DestType->isFunctionPointerType() || |
| 1381 | DestType->isMemberFunctionPointerType()) { |
| 1382 | // Cannot cast direct function pointers. |
| 1383 | // C++ 5.2.11p2: [...] where T is any object type or the void type [...] |
| 1384 | // T is the ultimate pointee of source and target type. |
| 1385 | if (!CStyle) |
| 1386 | msg = diag::err_bad_const_cast_dest; |
| 1387 | return TC_NotApplicable; |
| 1388 | } |
| 1389 | SrcType = Self.Context.getCanonicalType(SrcType); |
| 1390 | |
| 1391 | // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are |
| 1392 | // completely equal. |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1393 | // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers |
| 1394 | // in multi-level pointers may change, but the level count must be the same, |
| 1395 | // as must be the final pointee type. |
| 1396 | while (SrcType != DestType && |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 1397 | Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1398 | Qualifiers SrcQuals, DestQuals; |
| 1399 | SrcType = Self.Context.getUnqualifiedArrayType(SrcType, SrcQuals); |
| 1400 | DestType = Self.Context.getUnqualifiedArrayType(DestType, DestQuals); |
| 1401 | |
| 1402 | // const_cast is permitted to strip cvr-qualifiers, only. Make sure that |
| 1403 | // the other qualifiers (e.g., address spaces) are identical. |
| 1404 | SrcQuals.removeCVRQualifiers(); |
| 1405 | DestQuals.removeCVRQualifiers(); |
| 1406 | if (SrcQuals != DestQuals) |
| 1407 | return TC_NotApplicable; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | // Since we're dealing in canonical types, the remainder must be the same. |
| 1411 | if (SrcType != DestType) |
| 1412 | return TC_NotApplicable; |
| 1413 | |
| 1414 | return TC_Success; |
| 1415 | } |
| 1416 | |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1417 | // Checks for undefined behavior in reinterpret_cast. |
| 1418 | // The cases that is checked for is: |
| 1419 | // *reinterpret_cast<T*>(&a) |
| 1420 | // reinterpret_cast<T&>(a) |
| 1421 | // where accessing 'a' as type 'T' will result in undefined behavior. |
| 1422 | void Sema::CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType, |
| 1423 | bool IsDereference, |
| 1424 | SourceRange Range) { |
| 1425 | unsigned DiagID = IsDereference ? |
| 1426 | diag::warn_pointer_indirection_from_incompatible_type : |
| 1427 | diag::warn_undefined_reinterpret_cast; |
| 1428 | |
| 1429 | if (Diags.getDiagnosticLevel(DiagID, Range.getBegin()) == |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1430 | DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | QualType SrcTy, DestTy; |
| 1435 | if (IsDereference) { |
| 1436 | if (!SrcType->getAs<PointerType>() || !DestType->getAs<PointerType>()) { |
| 1437 | return; |
| 1438 | } |
| 1439 | SrcTy = SrcType->getPointeeType(); |
| 1440 | DestTy = DestType->getPointeeType(); |
| 1441 | } else { |
| 1442 | if (!DestType->getAs<ReferenceType>()) { |
| 1443 | return; |
| 1444 | } |
| 1445 | SrcTy = SrcType; |
| 1446 | DestTy = DestType->getPointeeType(); |
| 1447 | } |
| 1448 | |
| 1449 | // Cast is compatible if the types are the same. |
| 1450 | if (Context.hasSameUnqualifiedType(DestTy, SrcTy)) { |
| 1451 | return; |
| 1452 | } |
| 1453 | // or one of the types is a char or void type |
| 1454 | if (DestTy->isAnyCharacterType() || DestTy->isVoidType() || |
| 1455 | SrcTy->isAnyCharacterType() || SrcTy->isVoidType()) { |
| 1456 | return; |
| 1457 | } |
| 1458 | // or one of the types is a tag type. |
Chandler Carruth | 1f8f2d5 | 2011-05-24 07:43:19 +0000 | [diff] [blame] | 1459 | if (SrcTy->getAs<TagType>() || DestTy->getAs<TagType>()) { |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1463 | // FIXME: Scoped enums? |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1464 | if ((SrcTy->isUnsignedIntegerType() && DestTy->isSignedIntegerType()) || |
| 1465 | (SrcTy->isSignedIntegerType() && DestTy->isUnsignedIntegerType())) { |
| 1466 | if (Context.getTypeSize(DestTy) == Context.getTypeSize(SrcTy)) { |
| 1467 | return; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range; |
| 1472 | } |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 1473 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1474 | static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1475 | QualType DestType, bool CStyle, |
| 1476 | const SourceRange &OpRange, |
Anders Carlsson | 3c31a39 | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 1477 | unsigned &msg, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1478 | CastKind &Kind) { |
Douglas Gregor | e39a389 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1479 | bool IsLValueCast = false; |
| 1480 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1481 | DestType = Self.Context.getCanonicalType(DestType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1482 | QualType SrcType = SrcExpr.get()->getType(); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1483 | |
| 1484 | // Is the source an overloaded name? (i.e. &foo) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1485 | // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5) ... |
| 1486 | if (SrcType == Self.Context.OverloadTy) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1487 | // ... unless foo<int> resolves to an lvalue unambiguously. |
| 1488 | // TODO: what if this fails because of DiagnoseUseOfDecl or something |
| 1489 | // like it? |
| 1490 | ExprResult SingleFunctionExpr = SrcExpr; |
| 1491 | if (Self.ResolveAndFixSingleFunctionTemplateSpecialization( |
| 1492 | SingleFunctionExpr, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1493 | Expr::getValueKindForType(DestType) == VK_RValue // Convert Fun to Ptr |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1494 | ) && SingleFunctionExpr.isUsable()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1495 | SrcExpr = move(SingleFunctionExpr); |
| 1496 | SrcType = SrcExpr.get()->getType(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1497 | } else { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1498 | return TC_NotApplicable; |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1499 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1500 | } |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1501 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1502 | if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1503 | bool LValue = DestTypeTmp->isLValueReferenceType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1504 | if (LValue && !SrcExpr.get()->isLValue()) { |
Douglas Gregor | 575d2a3 | 2011-01-22 00:19:52 +0000 | [diff] [blame] | 1505 | // Cannot cast non-lvalue to lvalue reference type. See the similar |
| 1506 | // comment in const_cast. |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1507 | msg = diag::err_bad_cxx_cast_rvalue; |
| 1508 | return TC_NotApplicable; |
| 1509 | } |
| 1510 | |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1511 | if (!CStyle) { |
| 1512 | Self.CheckCompatibleReinterpretCast(SrcType, DestType, |
| 1513 | /*isDereference=*/false, OpRange); |
| 1514 | } |
| 1515 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1516 | // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the |
| 1517 | // same effect as the conversion *reinterpret_cast<T*>(&x) with the |
| 1518 | // built-in & and * operators. |
Argyrios Kyrtzidis | b464a5b | 2011-04-22 22:31:13 +0000 | [diff] [blame] | 1519 | |
Argyrios Kyrtzidis | bb29d1b | 2011-04-22 23:57:57 +0000 | [diff] [blame] | 1520 | const char *inappropriate = 0; |
| 1521 | switch (SrcExpr.get()->getObjectKind()) { |
Argyrios Kyrtzidis | e5e3d31 | 2011-04-23 01:10:24 +0000 | [diff] [blame] | 1522 | case OK_Ordinary: |
| 1523 | break; |
Argyrios Kyrtzidis | bb29d1b | 2011-04-22 23:57:57 +0000 | [diff] [blame] | 1524 | case OK_BitField: inappropriate = "bit-field"; break; |
| 1525 | case OK_VectorComponent: inappropriate = "vector element"; break; |
| 1526 | case OK_ObjCProperty: inappropriate = "property expression"; break; |
| 1527 | } |
| 1528 | if (inappropriate) { |
| 1529 | Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_reference) |
| 1530 | << inappropriate << DestType |
| 1531 | << OpRange << SrcExpr.get()->getSourceRange(); |
| 1532 | msg = 0; SrcExpr = ExprError(); |
Argyrios Kyrtzidis | b464a5b | 2011-04-22 22:31:13 +0000 | [diff] [blame] | 1533 | return TC_NotApplicable; |
| 1534 | } |
| 1535 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1536 | // This code does this transformation for the checked types. |
| 1537 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); |
| 1538 | SrcType = Self.Context.getPointerType(SrcType); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1539 | |
Douglas Gregor | e39a389 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1540 | IsLValueCast = true; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | // Canonicalize source for comparison. |
| 1544 | SrcType = Self.Context.getCanonicalType(SrcType); |
| 1545 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1546 | const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(), |
| 1547 | *SrcMemPtr = SrcType->getAs<MemberPointerType>(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1548 | if (DestMemPtr && SrcMemPtr) { |
| 1549 | // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1" |
| 1550 | // can be explicitly converted to an rvalue of type "pointer to member |
| 1551 | // of Y of type T2" if T1 and T2 are both function types or both object |
| 1552 | // types. |
| 1553 | if (DestMemPtr->getPointeeType()->isFunctionType() != |
| 1554 | SrcMemPtr->getPointeeType()->isFunctionType()) |
| 1555 | return TC_NotApplicable; |
| 1556 | |
| 1557 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away |
| 1558 | // constness. |
| 1559 | // A reinterpret_cast followed by a const_cast can, though, so in C-style, |
| 1560 | // we accept it. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1561 | if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, |
| 1562 | /*CheckObjCLifetime=*/CStyle)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1563 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1564 | return TC_Failed; |
| 1565 | } |
| 1566 | |
Charles Davis | f231df3 | 2010-08-16 05:30:44 +0000 | [diff] [blame] | 1567 | // Don't allow casting between member pointers of different sizes. |
| 1568 | if (Self.Context.getTypeSize(DestMemPtr) != |
| 1569 | Self.Context.getTypeSize(SrcMemPtr)) { |
| 1570 | msg = diag::err_bad_cxx_cast_member_pointer_size; |
| 1571 | return TC_Failed; |
| 1572 | } |
| 1573 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1574 | // A valid member pointer cast. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1575 | Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1576 | return TC_Success; |
| 1577 | } |
| 1578 | |
| 1579 | // See below for the enumeral issue. |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1580 | if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1581 | // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral |
| 1582 | // type large enough to hold it. A value of std::nullptr_t can be |
| 1583 | // converted to an integral type; the conversion has the same meaning |
| 1584 | // and validity as a conversion of (void*)0 to the integral type. |
| 1585 | if (Self.Context.getTypeSize(SrcType) > |
| 1586 | Self.Context.getTypeSize(DestType)) { |
| 1587 | msg = diag::err_bad_reinterpret_cast_small_int; |
| 1588 | return TC_Failed; |
| 1589 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1590 | Kind = CK_PointerToIntegral; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1591 | return TC_Success; |
| 1592 | } |
| 1593 | |
Anders Carlsson | 0de51bc | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1594 | bool destIsVector = DestType->isVectorType(); |
| 1595 | bool srcIsVector = SrcType->isVectorType(); |
| 1596 | if (srcIsVector || destIsVector) { |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1597 | // FIXME: Should this also apply to floating point types? |
| 1598 | bool srcIsScalar = SrcType->isIntegralType(Self.Context); |
| 1599 | bool destIsScalar = DestType->isIntegralType(Self.Context); |
Anders Carlsson | 0de51bc | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1600 | |
| 1601 | // Check if this is a cast between a vector and something else. |
| 1602 | if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) && |
| 1603 | !(srcIsVector && destIsVector)) |
| 1604 | return TC_NotApplicable; |
| 1605 | |
| 1606 | // If both types have the same size, we can successfully cast. |
Douglas Gregor | f2a5539 | 2009-12-22 22:47:22 +0000 | [diff] [blame] | 1607 | if (Self.Context.getTypeSize(SrcType) |
| 1608 | == Self.Context.getTypeSize(DestType)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1609 | Kind = CK_BitCast; |
Anders Carlsson | 0de51bc | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1610 | return TC_Success; |
Douglas Gregor | f2a5539 | 2009-12-22 22:47:22 +0000 | [diff] [blame] | 1611 | } |
Anders Carlsson | 0de51bc | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1612 | |
| 1613 | if (destIsScalar) |
| 1614 | msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size; |
| 1615 | else if (srcIsScalar) |
| 1616 | msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size; |
| 1617 | else |
| 1618 | msg = diag::err_bad_cxx_cast_vector_to_vector_different_size; |
| 1619 | |
| 1620 | return TC_Failed; |
| 1621 | } |
| 1622 | |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1623 | bool destIsPtr = DestType->isAnyPointerType() || |
| 1624 | DestType->isBlockPointerType(); |
| 1625 | bool srcIsPtr = SrcType->isAnyPointerType() || |
| 1626 | SrcType->isBlockPointerType(); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1627 | if (!destIsPtr && !srcIsPtr) { |
| 1628 | // Except for std::nullptr_t->integer and lvalue->reference, which are |
| 1629 | // handled above, at least one of the two arguments must be a pointer. |
| 1630 | return TC_NotApplicable; |
| 1631 | } |
| 1632 | |
| 1633 | if (SrcType == DestType) { |
| 1634 | // C++ 5.2.10p2 has a note that mentions that, subject to all other |
| 1635 | // restrictions, a cast to the same type is allowed. The intent is not |
| 1636 | // entirely clear here, since all other paragraphs explicitly forbid casts |
| 1637 | // to the same type. However, the behavior of compilers is pretty consistent |
| 1638 | // on this point: allow same-type conversion if the involved types are |
| 1639 | // pointers, disallow otherwise. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1640 | Kind = CK_NoOp; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1641 | return TC_Success; |
| 1642 | } |
| 1643 | |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1644 | if (DestType->isIntegralType(Self.Context)) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1645 | assert(srcIsPtr && "One type must be a pointer"); |
| 1646 | // C++ 5.2.10p4: A pointer can be explicitly converted to any integral |
Francois Pichet | 30aff5b | 2011-05-11 22:13:54 +0000 | [diff] [blame] | 1647 | // type large enough to hold it; except in Microsoft mode, where the |
| 1648 | // integral type size doesn't matter. |
| 1649 | if ((Self.Context.getTypeSize(SrcType) > |
| 1650 | Self.Context.getTypeSize(DestType)) && |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1651 | !Self.getLangOptions().MicrosoftExt) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1652 | msg = diag::err_bad_reinterpret_cast_small_int; |
| 1653 | return TC_Failed; |
| 1654 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1655 | Kind = CK_PointerToIntegral; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1656 | return TC_Success; |
| 1657 | } |
| 1658 | |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1659 | if (SrcType->isIntegralOrEnumerationType()) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1660 | assert(destIsPtr && "One type must be a pointer"); |
| 1661 | // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly |
| 1662 | // converted to a pointer. |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1663 | // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not |
| 1664 | // necessarily converted to a null pointer value.] |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1665 | Kind = CK_IntegralToPointer; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1666 | return TC_Success; |
| 1667 | } |
| 1668 | |
| 1669 | if (!destIsPtr || !srcIsPtr) { |
| 1670 | // With the valid non-pointer conversions out of the way, we can be even |
| 1671 | // more stringent. |
| 1672 | return TC_NotApplicable; |
| 1673 | } |
| 1674 | |
| 1675 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness. |
| 1676 | // The C-style cast operator can. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1677 | if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, |
| 1678 | /*CheckObjCLifetime=*/CStyle)) { |
Douglas Gregor | d4c5f84 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1679 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1680 | return TC_Failed; |
| 1681 | } |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1682 | |
| 1683 | // Cannot convert between block pointers and Objective-C object pointers. |
| 1684 | if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) || |
| 1685 | (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType())) |
| 1686 | return TC_NotApplicable; |
| 1687 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1688 | if (IsLValueCast) { |
| 1689 | Kind = CK_LValueBitCast; |
| 1690 | } else if (DestType->isObjCObjectPointerType()) { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 1691 | Kind = Self.PrepareCastToObjCObjectPointer(SrcExpr); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1692 | } else if (DestType->isBlockPointerType()) { |
| 1693 | if (!SrcType->isBlockPointerType()) { |
| 1694 | Kind = CK_AnyPointerToBlockPointerCast; |
| 1695 | } else { |
| 1696 | Kind = CK_BitCast; |
| 1697 | } |
| 1698 | } else { |
| 1699 | Kind = CK_BitCast; |
| 1700 | } |
| 1701 | |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1702 | // Any pointer can be cast to an Objective-C pointer type with a C-style |
| 1703 | // cast. |
Fariborz Jahanian | 92ef5d7 | 2009-12-08 23:09:15 +0000 | [diff] [blame] | 1704 | if (CStyle && DestType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 92ef5d7 | 2009-12-08 23:09:15 +0000 | [diff] [blame] | 1705 | return TC_Success; |
| 1706 | } |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1707 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1708 | // Not casting away constness, so the only remaining check is for compatible |
| 1709 | // pointer categories. |
| 1710 | |
| 1711 | if (SrcType->isFunctionPointerType()) { |
| 1712 | if (DestType->isFunctionPointerType()) { |
| 1713 | // C++ 5.2.10p6: A pointer to a function can be explicitly converted to |
| 1714 | // a pointer to a function of a different type. |
| 1715 | return TC_Success; |
| 1716 | } |
| 1717 | |
| 1718 | // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to |
| 1719 | // an object type or vice versa is conditionally-supported. |
| 1720 | // Compilers support it in C++03 too, though, because it's necessary for |
| 1721 | // casting the return value of dlsym() and GetProcAddress(). |
| 1722 | // FIXME: Conditionally-supported behavior should be configurable in the |
| 1723 | // TargetInfo or similar. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1724 | Self.Diag(OpRange.getBegin(), |
| 1725 | Self.getLangOptions().CPlusPlus0x ? |
| 1726 | diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj) |
| 1727 | << OpRange; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1728 | return TC_Success; |
| 1729 | } |
| 1730 | |
| 1731 | if (DestType->isFunctionPointerType()) { |
| 1732 | // See above. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1733 | Self.Diag(OpRange.getBegin(), |
| 1734 | Self.getLangOptions().CPlusPlus0x ? |
| 1735 | diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj) |
| 1736 | << OpRange; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1737 | return TC_Success; |
| 1738 | } |
Douglas Gregor | bf9fb88 | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1739 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1740 | // C++ 5.2.10p7: A pointer to an object can be explicitly converted to |
| 1741 | // a pointer to an object of different type. |
| 1742 | // Void pointers are not specified, but supported by every compiler out there. |
| 1743 | // So we finish by allowing everything that remains - it's got to be two |
| 1744 | // object pointers. |
| 1745 | return TC_Success; |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 1746 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1747 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 1748 | void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1749 | // Handle placeholders. |
| 1750 | if (isPlaceholder()) { |
| 1751 | // C-style casts can resolve __unknown_any types. |
| 1752 | if (claimPlaceholder(BuiltinType::UnknownAny)) { |
| 1753 | SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType, |
| 1754 | SrcExpr.get(), Kind, |
| 1755 | ValueKind, BasePath); |
| 1756 | return; |
| 1757 | } |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1758 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1759 | checkNonOverloadPlaceholders(); |
| 1760 | if (SrcExpr.isInvalid()) |
| 1761 | return; |
John McCall | 4919dfd | 2011-10-17 17:42:19 +0000 | [diff] [blame] | 1762 | } |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1763 | |
| 1764 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1765 | // This test is outside everything else because it's the only case where |
| 1766 | // a non-lvalue-reference target type does not lead to decay. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1767 | if (DestType->isVoidType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1768 | Kind = CK_ToVoid; |
| 1769 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1770 | if (claimPlaceholder(BuiltinType::Overload)) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1771 | Self.ResolveAndFixSingleFunctionTemplateSpecialization( |
| 1772 | SrcExpr, /* Decay Function to ptr */ false, |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1773 | /* Complain */ true, DestRange, DestType, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 1774 | diag::err_bad_cstyle_cast_overload); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1775 | if (SrcExpr.isInvalid()) |
| 1776 | return; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1777 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1778 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1779 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); |
| 1780 | if (SrcExpr.isInvalid()) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1781 | return; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1782 | |
| 1783 | return; |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1786 | // If the type is dependent, we won't do any other semantic analysis now. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1787 | if (DestType->isDependentType() || SrcExpr.get()->isTypeDependent()) { |
| 1788 | assert(Kind == CK_Dependent); |
| 1789 | return; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1790 | } |
Benjamin Kramer | 5b4a40a | 2011-07-08 20:20:17 +0000 | [diff] [blame] | 1791 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 1792 | if (ValueKind == VK_RValue && !DestType->isRecordType() && |
| 1793 | !isPlaceholder(BuiltinType::Overload)) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1794 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
| 1795 | if (SrcExpr.isInvalid()) |
| 1796 | return; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1797 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1798 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1799 | // AltiVec vector initialization with a single literal. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1800 | if (const VectorType *vecTy = DestType->getAs<VectorType>()) |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1801 | if (vecTy->getVectorKind() == VectorType::AltiVecVector |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1802 | && (SrcExpr.get()->getType()->isIntegerType() |
| 1803 | || SrcExpr.get()->getType()->isFloatingType())) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1804 | Kind = CK_VectorSplat; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1805 | return; |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1808 | // C++ [expr.cast]p5: The conversions performed by |
| 1809 | // - a const_cast, |
| 1810 | // - a static_cast, |
| 1811 | // - a static_cast followed by a const_cast, |
| 1812 | // - a reinterpret_cast, or |
| 1813 | // - a reinterpret_cast followed by a const_cast, |
| 1814 | // can be performed using the cast notation of explicit type conversion. |
| 1815 | // [...] If a conversion can be interpreted in more than one of the ways |
| 1816 | // listed above, the interpretation that appears first in the list is used, |
| 1817 | // even if a cast resulting from that interpretation is ill-formed. |
| 1818 | // In plain language, this means trying a const_cast ... |
| 1819 | unsigned msg = diag::err_bad_cxx_cast_generic; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1820 | TryCastResult tcr = TryConstCast(Self, SrcExpr.get(), DestType, |
| 1821 | /*CStyle*/true, msg); |
Anders Carlsson | da921fd | 2009-10-19 18:14:28 +0000 | [diff] [blame] | 1822 | if (tcr == TC_Success) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1823 | Kind = CK_NoOp; |
Anders Carlsson | da921fd | 2009-10-19 18:14:28 +0000 | [diff] [blame] | 1824 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1825 | Sema::CheckedConversionKind CCK |
| 1826 | = FunctionalStyle? Sema::CCK_FunctionalCast |
| 1827 | : Sema::CCK_CStyleCast; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1828 | if (tcr == TC_NotApplicable) { |
| 1829 | // ... or if that is not possible, a static_cast, ignoring const, ... |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1830 | tcr = TryStaticCast(Self, SrcExpr, DestType, CCK, OpRange, |
| 1831 | msg, Kind, BasePath); |
| 1832 | if (SrcExpr.isInvalid()) |
| 1833 | return; |
| 1834 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1835 | if (tcr == TC_NotApplicable) { |
| 1836 | // ... and finally a reinterpret_cast, ignoring const. |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1837 | tcr = TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/true, |
| 1838 | OpRange, msg, Kind); |
| 1839 | if (SrcExpr.isInvalid()) |
| 1840 | return; |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1841 | } |
| 1842 | } |
| 1843 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1844 | if (Self.getLangOptions().ObjCAutoRefCount && tcr == TC_Success) |
| 1845 | checkObjCARCConversion(CCK); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1846 | |
Nick Lewycky | 43328e9 | 2010-11-09 00:19:31 +0000 | [diff] [blame] | 1847 | if (tcr != TC_Success && msg != 0) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1848 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1849 | DeclAccessPair Found; |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1850 | FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), |
| 1851 | DestType, |
| 1852 | /*Complain*/ true, |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1853 | Found); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1854 | |
Richard Trieu | 32ac00d | 2011-04-16 01:09:30 +0000 | [diff] [blame] | 1855 | assert(!Fn && "cast failed but able to resolve overload expression!!"); |
Nick Lewycky | 43328e9 | 2010-11-09 00:19:31 +0000 | [diff] [blame] | 1856 | (void)Fn; |
John McCall | 79ab2c8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 1857 | |
Nick Lewycky | 43328e9 | 2010-11-09 00:19:31 +0000 | [diff] [blame] | 1858 | } else { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1859 | diagnoseBadCast(Self, msg, (FunctionalStyle ? CT_Functional : CT_CStyle), |
| 1860 | OpRange, SrcExpr.get(), DestType); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1861 | } |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1862 | } else if (Kind == CK_BitCast) { |
| 1863 | checkCastAlign(); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1864 | } |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1865 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1866 | // Clear out SrcExpr if there was a fatal error. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1867 | if (tcr != TC_Success) |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1868 | SrcExpr = ExprError(); |
| 1869 | } |
| 1870 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1871 | /// Check the semantics of a C-style cast operation, in C. |
| 1872 | void CastOperation::CheckCStyleCast() { |
| 1873 | assert(!Self.getLangOptions().CPlusPlus); |
| 1874 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1875 | // C-style casts can resolve __unknown_any types. |
| 1876 | if (claimPlaceholder(BuiltinType::UnknownAny)) { |
| 1877 | SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType, |
| 1878 | SrcExpr.get(), Kind, |
| 1879 | ValueKind, BasePath); |
| 1880 | return; |
| 1881 | } |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1882 | |
| 1883 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 1884 | // type needs to be scalar. |
| 1885 | if (DestType->isVoidType()) { |
| 1886 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
| 1887 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); |
| 1888 | if (SrcExpr.isInvalid()) |
| 1889 | return; |
| 1890 | |
| 1891 | // Cast to void allows any expr type. |
| 1892 | Kind = CK_ToVoid; |
| 1893 | return; |
| 1894 | } |
| 1895 | |
| 1896 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
| 1897 | if (SrcExpr.isInvalid()) |
| 1898 | return; |
| 1899 | QualType SrcType = SrcExpr.get()->getType(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1900 | assert(!SrcType->isPlaceholderType()); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 1901 | |
| 1902 | if (Self.RequireCompleteType(OpRange.getBegin(), DestType, |
| 1903 | diag::err_typecheck_cast_to_incomplete)) { |
| 1904 | SrcExpr = ExprError(); |
| 1905 | return; |
| 1906 | } |
| 1907 | |
| 1908 | if (!DestType->isScalarType() && !DestType->isVectorType()) { |
| 1909 | const RecordType *DestRecordTy = DestType->getAs<RecordType>(); |
| 1910 | |
| 1911 | if (DestRecordTy && Self.Context.hasSameUnqualifiedType(DestType, SrcType)){ |
| 1912 | // GCC struct/union extension: allow cast to self. |
| 1913 | Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 1914 | << DestType << SrcExpr.get()->getSourceRange(); |
| 1915 | Kind = CK_NoOp; |
| 1916 | return; |
| 1917 | } |
| 1918 | |
| 1919 | // GCC's cast to union extension. |
| 1920 | if (DestRecordTy && DestRecordTy->getDecl()->isUnion()) { |
| 1921 | RecordDecl *RD = DestRecordTy->getDecl(); |
| 1922 | RecordDecl::field_iterator Field, FieldEnd; |
| 1923 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
| 1924 | Field != FieldEnd; ++Field) { |
| 1925 | if (Self.Context.hasSameUnqualifiedType(Field->getType(), SrcType) && |
| 1926 | !Field->isUnnamedBitfield()) { |
| 1927 | Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_to_union) |
| 1928 | << SrcExpr.get()->getSourceRange(); |
| 1929 | break; |
| 1930 | } |
| 1931 | } |
| 1932 | if (Field == FieldEnd) { |
| 1933 | Self.Diag(OpRange.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 1934 | << SrcType << SrcExpr.get()->getSourceRange(); |
| 1935 | SrcExpr = ExprError(); |
| 1936 | return; |
| 1937 | } |
| 1938 | Kind = CK_ToUnion; |
| 1939 | return; |
| 1940 | } |
| 1941 | |
| 1942 | // Reject any other conversions to non-scalar types. |
| 1943 | Self.Diag(OpRange.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 1944 | << DestType << SrcExpr.get()->getSourceRange(); |
| 1945 | SrcExpr = ExprError(); |
| 1946 | return; |
| 1947 | } |
| 1948 | |
| 1949 | // The type we're casting to is known to be a scalar or vector. |
| 1950 | |
| 1951 | // Require the operand to be a scalar or vector. |
| 1952 | if (!SrcType->isScalarType() && !SrcType->isVectorType()) { |
| 1953 | Self.Diag(SrcExpr.get()->getExprLoc(), |
| 1954 | diag::err_typecheck_expect_scalar_operand) |
| 1955 | << SrcType << SrcExpr.get()->getSourceRange(); |
| 1956 | SrcExpr = ExprError(); |
| 1957 | return; |
| 1958 | } |
| 1959 | |
| 1960 | if (DestType->isExtVectorType()) { |
| 1961 | SrcExpr = Self.CheckExtVectorCast(OpRange, DestType, SrcExpr.take(), Kind); |
| 1962 | return; |
| 1963 | } |
| 1964 | |
| 1965 | if (const VectorType *DestVecTy = DestType->getAs<VectorType>()) { |
| 1966 | if (DestVecTy->getVectorKind() == VectorType::AltiVecVector && |
| 1967 | (SrcType->isIntegerType() || SrcType->isFloatingType())) { |
| 1968 | Kind = CK_VectorSplat; |
| 1969 | } else if (Self.CheckVectorCast(OpRange, DestType, SrcType, Kind)) { |
| 1970 | SrcExpr = ExprError(); |
| 1971 | } |
| 1972 | return; |
| 1973 | } |
| 1974 | |
| 1975 | if (SrcType->isVectorType()) { |
| 1976 | if (Self.CheckVectorCast(OpRange, SrcType, DestType, Kind)) |
| 1977 | SrcExpr = ExprError(); |
| 1978 | return; |
| 1979 | } |
| 1980 | |
| 1981 | // The source and target types are both scalars, i.e. |
| 1982 | // - arithmetic types (fundamental, enum, and complex) |
| 1983 | // - all kinds of pointers |
| 1984 | // Note that member pointers were filtered out with C++, above. |
| 1985 | |
| 1986 | if (isa<ObjCSelectorExpr>(SrcExpr.get())) { |
| 1987 | Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_selector_expr); |
| 1988 | SrcExpr = ExprError(); |
| 1989 | return; |
| 1990 | } |
| 1991 | |
| 1992 | // If either type is a pointer, the other type has to be either an |
| 1993 | // integer or a pointer. |
| 1994 | if (!DestType->isArithmeticType()) { |
| 1995 | if (!SrcType->isIntegralType(Self.Context) && SrcType->isArithmeticType()) { |
| 1996 | Self.Diag(SrcExpr.get()->getExprLoc(), |
| 1997 | diag::err_cast_pointer_from_non_pointer_int) |
| 1998 | << SrcType << SrcExpr.get()->getSourceRange(); |
| 1999 | SrcExpr = ExprError(); |
| 2000 | return; |
| 2001 | } |
| 2002 | } else if (!SrcType->isArithmeticType()) { |
| 2003 | if (!DestType->isIntegralType(Self.Context) && |
| 2004 | DestType->isArithmeticType()) { |
| 2005 | Self.Diag(SrcExpr.get()->getLocStart(), |
| 2006 | diag::err_cast_pointer_to_non_pointer_int) |
Abramo Bagnara | f7ce194 | 2011-11-15 11:25:38 +0000 | [diff] [blame] | 2007 | << DestType << SrcExpr.get()->getSourceRange(); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2008 | SrcExpr = ExprError(); |
| 2009 | return; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | // ARC imposes extra restrictions on casts. |
| 2014 | if (Self.getLangOptions().ObjCAutoRefCount) { |
| 2015 | checkObjCARCConversion(Sema::CCK_CStyleCast); |
| 2016 | if (SrcExpr.isInvalid()) |
| 2017 | return; |
| 2018 | |
| 2019 | if (const PointerType *CastPtr = DestType->getAs<PointerType>()) { |
| 2020 | if (const PointerType *ExprPtr = SrcType->getAs<PointerType>()) { |
| 2021 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 2022 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 2023 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 2024 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 2025 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
| 2026 | Self.Diag(SrcExpr.get()->getLocStart(), |
| 2027 | diag::err_typecheck_incompatible_ownership) |
| 2028 | << SrcType << DestType << Sema::AA_Casting |
| 2029 | << SrcExpr.get()->getSourceRange(); |
| 2030 | return; |
| 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | else if (!Self.CheckObjCARCUnavailableWeakConversion(DestType, SrcType)) { |
| 2035 | Self.Diag(SrcExpr.get()->getLocStart(), |
| 2036 | diag::err_arc_convesion_of_weak_unavailable) |
| 2037 | << 1 << SrcType << DestType << SrcExpr.get()->getSourceRange(); |
| 2038 | SrcExpr = ExprError(); |
| 2039 | return; |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | Kind = Self.PrepareScalarCast(SrcExpr, DestType); |
| 2044 | if (SrcExpr.isInvalid()) |
| 2045 | return; |
| 2046 | |
| 2047 | if (Kind == CK_BitCast) |
| 2048 | checkCastAlign(); |
| 2049 | } |
| 2050 | |
| 2051 | ExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc, |
| 2052 | TypeSourceInfo *CastTypeInfo, |
| 2053 | SourceLocation RPLoc, |
| 2054 | Expr *CastExpr) { |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2055 | CastOperation Op(*this, CastTypeInfo->getType(), CastExpr); |
| 2056 | Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange(); |
| 2057 | Op.OpRange = SourceRange(LPLoc, CastExpr->getLocEnd()); |
| 2058 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2059 | if (getLangOptions().CPlusPlus) { |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2060 | Op.CheckCXXCStyleCast(/*FunctionalStyle=*/ false); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 2061 | } else { |
| 2062 | Op.CheckCStyleCast(); |
| 2063 | } |
| 2064 | |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2065 | if (Op.SrcExpr.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2066 | return ExprError(); |
| 2067 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2068 | return Op.complete(CStyleCastExpr::Create(Context, Op.ResultType, |
| 2069 | Op.ValueKind, Op.Kind, Op.SrcExpr.take(), |
| 2070 | &Op.BasePath, CastTypeInfo, LPLoc, RPLoc)); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | ExprResult Sema::BuildCXXFunctionalCastExpr(TypeSourceInfo *CastTypeInfo, |
| 2074 | SourceLocation LPLoc, |
| 2075 | Expr *CastExpr, |
| 2076 | SourceLocation RPLoc) { |
| 2077 | CastOperation Op(*this, CastTypeInfo->getType(), CastExpr); |
| 2078 | Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange(); |
| 2079 | Op.OpRange = SourceRange(Op.DestRange.getBegin(), CastExpr->getLocEnd()); |
| 2080 | |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 2081 | Op.CheckCXXCStyleCast(/*FunctionalStyle=*/ true); |
John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2082 | if (Op.SrcExpr.isInvalid()) |
| 2083 | return ExprError(); |
| 2084 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2085 | return Op.complete(CXXFunctionalCastExpr::Create(Context, Op.ResultType, |
| 2086 | Op.ValueKind, CastTypeInfo, Op.DestRange.getBegin(), |
| 2087 | Op.Kind, Op.SrcExpr.take(), &Op.BasePath, RPLoc)); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2088 | } |