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