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