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