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