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