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