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