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