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