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