Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 1 | //===--- SemaNamedCast.cpp - Semantic Analysis for Named Casts ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ named casts. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 19 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Sebastian Redl | 015085f | 2008-11-07 23:29:29 +0000 | [diff] [blame] | 21 | #include <set> |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 24 | |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 25 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 26 | enum TryCastResult { |
| 27 | TC_NotApplicable, ///< The cast method is not applicable. |
| 28 | TC_Success, ///< The cast method is appropriate and successful. |
| 29 | TC_Failed ///< The cast method is appropriate, but failed. A |
| 30 | ///< diagnostic has been emitted. |
| 31 | }; |
| 32 | |
| 33 | enum CastType { |
| 34 | CT_Const, ///< const_cast |
| 35 | CT_Static, ///< static_cast |
| 36 | CT_Reinterpret, ///< reinterpret_cast |
| 37 | CT_Dynamic, ///< dynamic_cast |
| 38 | CT_CStyle, ///< (Type)expr |
| 39 | CT_Functional ///< Type(expr) |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 45 | static void CheckConstCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 46 | ExprValueKind &VK, |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 47 | const SourceRange &OpRange, |
| 48 | const SourceRange &DestRange); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 49 | static void CheckReinterpretCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 50 | ExprValueKind &VK, |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 51 | const SourceRange &OpRange, |
Anders Carlsson | 7cd39e0 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 52 | const SourceRange &DestRange, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 53 | CastKind &Kind); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 54 | static void CheckStaticCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 55 | ExprValueKind &VK, |
Anders Carlsson | f10e414 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 56 | const SourceRange &OpRange, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 57 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 58 | CXXCastPath &BasePath); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 59 | static void CheckDynamicCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 60 | ExprValueKind &VK, |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 61 | const SourceRange &OpRange, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | const SourceRange &DestRange, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 63 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 64 | CXXCastPath &BasePath); |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 65 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 66 | static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, |
| 67 | bool CheckCVR, bool CheckObjCLifetime); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 68 | |
| 69 | // The Try functions attempt a specific way of casting. If they succeed, they |
| 70 | // return TC_Success. If their way of casting is not appropriate for the given |
| 71 | // arguments, they return TC_NotApplicable and *may* set diag to a diagnostic |
| 72 | // to emit if no other way succeeds. If their way of casting is appropriate but |
| 73 | // fails, they return TC_Failed and *must* set diag; they can set it to 0 if |
| 74 | // they emit a specialized diagnostic. |
| 75 | // All diagnostics returned by these functions must expect the same three |
| 76 | // arguments: |
| 77 | // %0: Cast Type (a value from the CastType enumeration) |
| 78 | // %1: Source Type |
| 79 | // %2: Destination Type |
| 80 | static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, |
Douglas Gregor | ce95084 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 81 | QualType DestType, bool CStyle, |
| 82 | CastKind &Kind, |
Douglas Gregor | ba278e2 | 2011-01-25 16:13:26 +0000 | [diff] [blame] | 83 | CXXCastPath &BasePath, |
| 84 | unsigned &msg); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 85 | static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 86 | QualType DestType, bool CStyle, |
| 87 | const SourceRange &OpRange, |
| 88 | unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 89 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 90 | CXXCastPath &BasePath); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 91 | static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType, |
| 92 | QualType DestType, bool CStyle, |
| 93 | const SourceRange &OpRange, |
Anders Carlsson | 9a1cd87 | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 94 | unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 95 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 96 | CXXCastPath &BasePath); |
Douglas Gregor | 8f3952c | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 97 | static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType, |
| 98 | CanQualType DestType, bool CStyle, |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 99 | const SourceRange &OpRange, |
| 100 | QualType OrigSrcType, |
Anders Carlsson | 9a1cd87 | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 101 | QualType OrigDestType, unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 102 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 103 | CXXCastPath &BasePath); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 104 | static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 105 | QualType SrcType, |
| 106 | QualType DestType,bool CStyle, |
| 107 | const SourceRange &OpRange, |
| 108 | unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 109 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 110 | CXXCastPath &BasePath); |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 111 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 112 | static TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 113 | QualType DestType, |
| 114 | Sema::CheckedConversionKind CCK, |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 115 | const SourceRange &OpRange, |
Fariborz Jahanian | 1cec0c4 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 116 | unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 117 | CastKind &Kind); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 118 | static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 119 | QualType DestType, |
| 120 | Sema::CheckedConversionKind CCK, |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 121 | const SourceRange &OpRange, |
Anders Carlsson | f1ae6d4 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 122 | unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 123 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 124 | CXXCastPath &BasePath); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 125 | static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType, |
| 126 | bool CStyle, unsigned &msg); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 127 | static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 128 | QualType DestType, bool CStyle, |
| 129 | const SourceRange &OpRange, |
Anders Carlsson | 9d1b34b | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 130 | unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 131 | CastKind &Kind); |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 133 | |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 134 | /// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 135 | ExprResult |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 136 | Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 137 | SourceLocation LAngleBracketLoc, Declarator &D, |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 138 | SourceLocation RAngleBracketLoc, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 139 | SourceLocation LParenLoc, Expr *E, |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 140 | SourceLocation RParenLoc) { |
John McCall | d377e04 | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 141 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 142 | assert(!D.isInvalidType()); |
| 143 | |
| 144 | TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, E->getType()); |
| 145 | if (D.isInvalidType()) |
| 146 | return ExprError(); |
| 147 | |
| 148 | if (getLangOptions().CPlusPlus) { |
| 149 | // Check that there are no default arguments (C++ only). |
| 150 | CheckExtraCXXDefaultArguments(D); |
| 151 | } |
| 152 | |
| 153 | return BuildCXXNamedCast(OpLoc, Kind, TInfo, move(E), |
John McCall | d377e04 | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 154 | SourceRange(LAngleBracketLoc, RAngleBracketLoc), |
| 155 | SourceRange(LParenLoc, RParenLoc)); |
| 156 | } |
| 157 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 158 | ExprResult |
John McCall | d377e04 | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 159 | Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 160 | TypeSourceInfo *DestTInfo, Expr *E, |
John McCall | d377e04 | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 161 | SourceRange AngleBrackets, SourceRange Parens) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 162 | ExprResult Ex = Owned(E); |
John McCall | d377e04 | 2010-01-15 19:13:16 +0000 | [diff] [blame] | 163 | QualType DestType = DestTInfo->getType(); |
| 164 | |
| 165 | SourceRange OpRange(OpLoc, Parens.getEnd()); |
| 166 | SourceRange DestRange = AngleBrackets; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 167 | |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 168 | // If the type is dependent, we won't do the semantic analysis now. |
| 169 | // FIXME: should we check this in a more fine-grained manner? |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 170 | bool TypeDependent = DestType->isDependentType() || Ex.get()->isTypeDependent(); |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 171 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 172 | ExprValueKind VK = VK_RValue; |
John McCall | 29ac8e2 | 2010-11-26 10:57:22 +0000 | [diff] [blame] | 173 | if (TypeDependent) |
| 174 | VK = Expr::getValueKindForType(DestType); |
| 175 | |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 176 | switch (Kind) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 177 | default: llvm_unreachable("Unknown C++ cast!"); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 178 | |
| 179 | case tok::kw_const_cast: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 180 | if (!TypeDependent) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 181 | CheckConstCast(*this, Ex, DestType, VK, OpRange, DestRange); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 182 | if (Ex.isInvalid()) |
| 183 | return ExprError(); |
| 184 | } |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 185 | return Owned(CXXConstCastExpr::Create(Context, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 186 | DestType.getNonLValueExprType(Context), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 187 | VK, Ex.take(), DestTInfo, OpLoc, |
Douglas Gregor | 4478f85 | 2011-01-12 22:41:29 +0000 | [diff] [blame] | 188 | Parens.getEnd())); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 189 | |
Anders Carlsson | 4ab4f7f | 2009-08-02 19:07:59 +0000 | [diff] [blame] | 190 | case tok::kw_dynamic_cast: { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 191 | CastKind Kind = CK_Dependent; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 192 | CXXCastPath BasePath; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 193 | if (!TypeDependent) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 194 | CheckDynamicCast(*this, Ex, DestType, VK, OpRange, DestRange, |
| 195 | Kind, BasePath); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 196 | if (Ex.isInvalid()) |
| 197 | return ExprError(); |
| 198 | } |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 199 | return Owned(CXXDynamicCastExpr::Create(Context, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 200 | DestType.getNonLValueExprType(Context), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 201 | VK, Kind, Ex.take(), &BasePath, DestTInfo, |
Douglas Gregor | 4478f85 | 2011-01-12 22:41:29 +0000 | [diff] [blame] | 202 | OpLoc, Parens.getEnd())); |
Anders Carlsson | 4ab4f7f | 2009-08-02 19:07:59 +0000 | [diff] [blame] | 203 | } |
Anders Carlsson | 7cd39e0 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 204 | case tok::kw_reinterpret_cast: { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 205 | CastKind Kind = CK_Dependent; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 206 | if (!TypeDependent) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 207 | CheckReinterpretCast(*this, Ex, DestType, VK, OpRange, DestRange, Kind); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 208 | if (Ex.isInvalid()) |
| 209 | return ExprError(); |
| 210 | } |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 211 | return Owned(CXXReinterpretCastExpr::Create(Context, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 212 | DestType.getNonLValueExprType(Context), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 213 | VK, Kind, Ex.take(), 0, |
Douglas Gregor | 4478f85 | 2011-01-12 22:41:29 +0000 | [diff] [blame] | 214 | DestTInfo, OpLoc, Parens.getEnd())); |
Anders Carlsson | 7cd39e0 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 215 | } |
Anders Carlsson | f10e414 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 216 | case tok::kw_static_cast: { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 217 | CastKind Kind = CK_Dependent; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 218 | CXXCastPath BasePath; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 219 | if (!TypeDependent) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 220 | CheckStaticCast(*this, Ex, DestType, VK, OpRange, Kind, BasePath); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 221 | if (Ex.isInvalid()) |
| 222 | return ExprError(); |
| 223 | } |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 224 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 225 | return Owned(CXXStaticCastExpr::Create(Context, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 226 | DestType.getNonLValueExprType(Context), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 227 | VK, Kind, Ex.take(), &BasePath, |
Douglas Gregor | 4478f85 | 2011-01-12 22:41:29 +0000 | [diff] [blame] | 228 | DestTInfo, OpLoc, Parens.getEnd())); |
Anders Carlsson | f10e414 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 229 | } |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 232 | return ExprError(); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 233 | } |
| 234 | |
John McCall | 909acf8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 235 | /// Try to diagnose a failed overloaded cast. Returns true if |
| 236 | /// diagnostics were emitted. |
| 237 | static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT, |
| 238 | SourceRange range, Expr *src, |
| 239 | QualType destType) { |
| 240 | switch (CT) { |
| 241 | // These cast kinds don't consider user-defined conversions. |
| 242 | case CT_Const: |
| 243 | case CT_Reinterpret: |
| 244 | case CT_Dynamic: |
| 245 | return false; |
| 246 | |
| 247 | // These do. |
| 248 | case CT_Static: |
| 249 | case CT_CStyle: |
| 250 | case CT_Functional: |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | QualType srcType = src->getType(); |
| 255 | if (!destType->isRecordType() && !srcType->isRecordType()) |
| 256 | return false; |
| 257 | |
| 258 | InitializedEntity entity = InitializedEntity::InitializeTemporary(destType); |
| 259 | InitializationKind initKind |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 260 | = (CT == CT_CStyle)? InitializationKind::CreateCStyleCast(range.getBegin(), |
| 261 | range) |
| 262 | : (CT == CT_Functional)? InitializationKind::CreateFunctionalCast(range) |
| 263 | : InitializationKind::CreateCast(/*type range?*/ range); |
John McCall | 909acf8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 264 | InitializationSequence sequence(S, entity, initKind, &src, 1); |
| 265 | |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 266 | assert(sequence.Failed() && "initialization succeeded on second try?"); |
John McCall | 909acf8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 267 | switch (sequence.getFailureKind()) { |
| 268 | default: return false; |
| 269 | |
| 270 | case InitializationSequence::FK_ConstructorOverloadFailed: |
| 271 | case InitializationSequence::FK_UserConversionOverloadFailed: |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | OverloadCandidateSet &candidates = sequence.getFailedCandidateSet(); |
| 276 | |
| 277 | unsigned msg = 0; |
| 278 | OverloadCandidateDisplayKind howManyCandidates = OCD_AllCandidates; |
| 279 | |
| 280 | switch (sequence.getFailedOverloadResult()) { |
| 281 | case OR_Success: llvm_unreachable("successful failed overload"); |
| 282 | return false; |
| 283 | case OR_No_Viable_Function: |
| 284 | if (candidates.empty()) |
| 285 | msg = diag::err_ovl_no_conversion_in_cast; |
| 286 | else |
| 287 | msg = diag::err_ovl_no_viable_conversion_in_cast; |
| 288 | howManyCandidates = OCD_AllCandidates; |
| 289 | break; |
| 290 | |
| 291 | case OR_Ambiguous: |
| 292 | msg = diag::err_ovl_ambiguous_conversion_in_cast; |
| 293 | howManyCandidates = OCD_ViableCandidates; |
| 294 | break; |
| 295 | |
| 296 | case OR_Deleted: |
| 297 | msg = diag::err_ovl_deleted_conversion_in_cast; |
| 298 | howManyCandidates = OCD_ViableCandidates; |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | S.Diag(range.getBegin(), msg) |
| 303 | << CT << srcType << destType |
| 304 | << range << src->getSourceRange(); |
| 305 | |
| 306 | candidates.NoteCandidates(S, howManyCandidates, &src, 1); |
| 307 | |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | /// Diagnose a failed cast. |
| 312 | static void diagnoseBadCast(Sema &S, unsigned msg, CastType castType, |
| 313 | SourceRange opRange, Expr *src, QualType destType) { |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 314 | if (src->getType() == S.Context.BoundMemberTy) { |
| 315 | (void) S.CheckPlaceholderExpr(src); // will always fail |
| 316 | return; |
| 317 | } |
| 318 | |
John McCall | 909acf8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 319 | if (msg == diag::err_bad_cxx_cast_generic && |
| 320 | tryDiagnoseOverloadedCast(S, castType, opRange, src, destType)) |
| 321 | return; |
| 322 | |
| 323 | S.Diag(opRange.getBegin(), msg) << castType |
| 324 | << src->getType() << destType << opRange << src->getSourceRange(); |
| 325 | } |
| 326 | |
Sebastian Redl | 55db1ec | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 327 | /// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes, |
| 328 | /// this removes one level of indirection from both types, provided that they're |
| 329 | /// the same kind of pointer (plain or to-member). Unlike the Sema function, |
| 330 | /// this one doesn't care if the two pointers-to-member don't point into the |
| 331 | /// same class. This is because CastsAwayConstness doesn't care. |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 332 | static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) { |
Sebastian Redl | 55db1ec | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 333 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 334 | *T2PtrType = T2->getAs<PointerType>(); |
| 335 | if (T1PtrType && T2PtrType) { |
| 336 | T1 = T1PtrType->getPointeeType(); |
| 337 | T2 = T2PtrType->getPointeeType(); |
| 338 | return true; |
| 339 | } |
Fariborz Jahanian | 8c3f06d | 2010-02-03 20:32:31 +0000 | [diff] [blame] | 340 | const ObjCObjectPointerType *T1ObjCPtrType = |
| 341 | T1->getAs<ObjCObjectPointerType>(), |
| 342 | *T2ObjCPtrType = |
| 343 | T2->getAs<ObjCObjectPointerType>(); |
| 344 | if (T1ObjCPtrType) { |
| 345 | if (T2ObjCPtrType) { |
| 346 | T1 = T1ObjCPtrType->getPointeeType(); |
| 347 | T2 = T2ObjCPtrType->getPointeeType(); |
| 348 | return true; |
| 349 | } |
| 350 | else if (T2PtrType) { |
| 351 | T1 = T1ObjCPtrType->getPointeeType(); |
| 352 | T2 = T2PtrType->getPointeeType(); |
| 353 | return true; |
| 354 | } |
| 355 | } |
| 356 | else if (T2ObjCPtrType) { |
| 357 | if (T1PtrType) { |
| 358 | T2 = T2ObjCPtrType->getPointeeType(); |
| 359 | T1 = T1PtrType->getPointeeType(); |
| 360 | return true; |
| 361 | } |
| 362 | } |
| 363 | |
Sebastian Redl | 55db1ec | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 364 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 365 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 366 | if (T1MPType && T2MPType) { |
| 367 | T1 = T1MPType->getPointeeType(); |
| 368 | T2 = T2MPType->getPointeeType(); |
| 369 | return true; |
| 370 | } |
Douglas Gregor | eaff2cb | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 371 | |
| 372 | const BlockPointerType *T1BPType = T1->getAs<BlockPointerType>(), |
| 373 | *T2BPType = T2->getAs<BlockPointerType>(); |
| 374 | if (T1BPType && T2BPType) { |
| 375 | T1 = T1BPType->getPointeeType(); |
| 376 | T2 = T2BPType->getPointeeType(); |
| 377 | return true; |
| 378 | } |
| 379 | |
Sebastian Redl | 55db1ec | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 380 | return false; |
| 381 | } |
| 382 | |
Sebastian Redl | a5a77a6 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 383 | /// CastsAwayConstness - Check if the pointer conversion from SrcType to |
| 384 | /// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by |
| 385 | /// the cast checkers. Both arguments must denote pointer (possibly to member) |
| 386 | /// types. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 387 | /// |
| 388 | /// \param CheckCVR Whether to check for const/volatile/restrict qualifiers. |
| 389 | /// |
| 390 | /// \param CheckObjCLifetime Whether to check Objective-C lifetime qualifiers. |
Sebastian Redl | 802f14c | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 391 | static bool |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 392 | CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType, |
| 393 | bool CheckCVR, bool CheckObjCLifetime) { |
| 394 | // If the only checking we care about is for Objective-C lifetime qualifiers, |
| 395 | // and we're not in ARC mode, there's nothing to check. |
| 396 | if (!CheckCVR && CheckObjCLifetime && |
| 397 | !Self.Context.getLangOptions().ObjCAutoRefCount) |
| 398 | return false; |
| 399 | |
Sebastian Redl | a5a77a6 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 400 | // Casting away constness is defined in C++ 5.2.11p8 with reference to |
| 401 | // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since |
| 402 | // the rules are non-trivial. So first we construct Tcv *...cv* as described |
| 403 | // in C++ 5.2.11p8. |
Douglas Gregor | eaff2cb | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 404 | assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() || |
| 405 | SrcType->isBlockPointerType()) && |
Sebastian Redl | a5a77a6 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 406 | "Source type is not pointer or pointer to member."); |
Douglas Gregor | eaff2cb | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 407 | assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() || |
| 408 | DestType->isBlockPointerType()) && |
Sebastian Redl | a5a77a6 | 2009-01-27 23:18:31 +0000 | [diff] [blame] | 409 | "Destination type is not pointer or pointer to member."); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 410 | |
Douglas Gregor | 8f3952c | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 411 | QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType), |
| 412 | UnwrappedDestType = Self.Context.getCanonicalType(DestType); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 413 | SmallVector<Qualifiers, 8> cv1, cv2; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 414 | |
Douglas Gregor | b472e93 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 415 | // Find the qualifiers. We only care about cvr-qualifiers for the |
| 416 | // purpose of this check, because other qualifiers (address spaces, |
| 417 | // Objective-C GC, etc.) are part of the type's identity. |
Sebastian Redl | 55db1ec | 2009-11-18 18:10:53 +0000 | [diff] [blame] | 418 | while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 419 | // Determine the relevant qualifiers at this level. |
| 420 | Qualifiers SrcQuals, DestQuals; |
Anders Carlsson | 76f513f | 2010-06-04 22:47:55 +0000 | [diff] [blame] | 421 | Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals); |
Anders Carlsson | 76f513f | 2010-06-04 22:47:55 +0000 | [diff] [blame] | 422 | Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 423 | |
| 424 | Qualifiers RetainedSrcQuals, RetainedDestQuals; |
| 425 | if (CheckCVR) { |
| 426 | RetainedSrcQuals.setCVRQualifiers(SrcQuals.getCVRQualifiers()); |
| 427 | RetainedDestQuals.setCVRQualifiers(DestQuals.getCVRQualifiers()); |
| 428 | } |
| 429 | |
| 430 | if (CheckObjCLifetime && |
| 431 | !DestQuals.compatiblyIncludesObjCLifetime(SrcQuals)) |
| 432 | return true; |
| 433 | |
| 434 | cv1.push_back(RetainedSrcQuals); |
| 435 | cv2.push_back(RetainedDestQuals); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 436 | } |
Douglas Gregor | eaff2cb | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 437 | if (cv1.empty()) |
| 438 | return false; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 439 | |
| 440 | // Construct void pointers with those qualifiers (in reverse order of |
| 441 | // unwrapping, of course). |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 442 | QualType SrcConstruct = Self.Context.VoidTy; |
| 443 | QualType DestConstruct = Self.Context.VoidTy; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 444 | ASTContext &Context = Self.Context; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 445 | for (SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 446 | i2 = cv2.rbegin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | i1 != cv1.rend(); ++i1, ++i2) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 448 | SrcConstruct |
| 449 | = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1)); |
| 450 | DestConstruct |
| 451 | = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2)); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // Test if they're compatible. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 455 | bool ObjCLifetimeConversion; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 456 | return SrcConstruct != DestConstruct && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 457 | !Self.IsQualificationConversion(SrcConstruct, DestConstruct, false, |
| 458 | ObjCLifetimeConversion); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 461 | /// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid. |
| 462 | /// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime- |
| 463 | /// checked downcasts in class hierarchies. |
Anders Carlsson | 4ab4f7f | 2009-08-02 19:07:59 +0000 | [diff] [blame] | 464 | static void |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 465 | CheckDynamicCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 466 | ExprValueKind &VK, const SourceRange &OpRange, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 467 | const SourceRange &DestRange, CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 468 | CXXCastPath &BasePath) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 469 | QualType OrigDestType = DestType, OrigSrcType = SrcExpr.get()->getType(); |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 470 | DestType = Self.Context.getCanonicalType(DestType); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 471 | |
| 472 | // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type, |
| 473 | // or "pointer to cv void". |
| 474 | |
| 475 | QualType DestPointee; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 476 | const PointerType *DestPointer = DestType->getAs<PointerType>(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 477 | const ReferenceType *DestReference = 0; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 478 | if (DestPointer) { |
| 479 | DestPointee = DestPointer->getPointeeType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 480 | } else if ((DestReference = DestType->getAs<ReferenceType>())) { |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 481 | DestPointee = DestReference->getPointeeType(); |
Douglas Gregor | 465184a | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 482 | VK = isa<LValueReferenceType>(DestReference) ? VK_LValue |
| 483 | : isa<RValueReferenceType>(DestReference) ? VK_XValue |
| 484 | : VK_RValue; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 485 | } else { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 486 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 487 | << OrigDestType << DestRange; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 491 | const RecordType *DestRecord = DestPointee->getAs<RecordType>(); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 492 | if (DestPointee->isVoidType()) { |
| 493 | assert(DestPointer && "Reference to void is not possible"); |
| 494 | } else if (DestRecord) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 495 | if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 496 | Self.PDiag(diag::err_bad_dynamic_cast_incomplete) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 497 | << DestRange)) |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 498 | return; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 499 | } else { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 500 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 501 | << DestPointee.getUnqualifiedType() << DestRange; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 502 | return; |
| 503 | } |
| 504 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 505 | // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to |
| 506 | // complete class type, [...]. If T is an lvalue reference type, v shall be |
Douglas Gregor | 465184a | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 507 | // an lvalue of a complete class type, [...]. If T is an rvalue reference |
| 508 | // type, v shall be an expression having a complete class type, [...] |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 509 | QualType SrcType = Self.Context.getCanonicalType(OrigSrcType); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 510 | QualType SrcPointee; |
| 511 | if (DestPointer) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 512 | if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 513 | SrcPointee = SrcPointer->getPointeeType(); |
| 514 | } else { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 515 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 516 | << OrigSrcType << SrcExpr.get()->getSourceRange(); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 517 | return; |
| 518 | } |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 519 | } else if (DestReference->isLValueReferenceType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 520 | if (!SrcExpr.get()->isLValue()) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 521 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue) |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 522 | << CT_Dynamic << OrigSrcType << OrigDestType << OpRange; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 523 | } |
| 524 | SrcPointee = SrcType; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 525 | } else { |
| 526 | SrcPointee = SrcType; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 529 | const RecordType *SrcRecord = SrcPointee->getAs<RecordType>(); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 530 | if (SrcRecord) { |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 531 | if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 532 | Self.PDiag(diag::err_bad_dynamic_cast_incomplete) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 533 | << SrcExpr.get()->getSourceRange())) |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 534 | return; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 535 | } else { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 536 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 537 | << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 538 | return; |
| 539 | } |
| 540 | |
| 541 | assert((DestPointer || DestReference) && |
| 542 | "Bad destination non-ptr/ref slipped through."); |
| 543 | assert((DestRecord || DestPointee->isVoidType()) && |
| 544 | "Bad destination pointee slipped through."); |
| 545 | assert(SrcRecord && "Bad source pointee slipped through."); |
| 546 | |
| 547 | // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness. |
| 548 | if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) { |
Douglas Gregor | b472e93 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 549 | Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away) |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 550 | << CT_Dynamic << OrigSrcType << OrigDestType << OpRange; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 551 | return; |
| 552 | } |
| 553 | |
| 554 | // C++ 5.2.7p3: If the type of v is the same as the required result type, |
| 555 | // [except for cv]. |
| 556 | if (DestRecord == SrcRecord) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 557 | Kind = CK_NoOp; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | |
| 561 | // C++ 5.2.7p5 |
| 562 | // Upcasts are resolved statically. |
Sebastian Redl | 842ef52 | 2008-11-08 13:00:26 +0000 | [diff] [blame] | 563 | if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) { |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 564 | if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee, |
| 565 | OpRange.getBegin(), OpRange, |
| 566 | &BasePath)) |
| 567 | return; |
| 568 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 569 | Kind = CK_DerivedToBase; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 570 | |
| 571 | // If we are casting to or through a virtual base class, we need a |
| 572 | // vtable. |
| 573 | if (Self.BasePathInvolvesVirtualBase(BasePath)) |
| 574 | Self.MarkVTableUsed(OpRange.getBegin(), |
| 575 | cast<CXXRecordDecl>(SrcRecord->getDecl())); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 576 | return; |
| 577 | } |
| 578 | |
| 579 | // C++ 5.2.7p6: Otherwise, v shall be [polymorphic]. |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 580 | const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition(); |
Sebastian Redl | b426f633 | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 581 | assert(SrcDecl && "Definition missing"); |
| 582 | if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 583 | Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 584 | << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange(); |
Sebastian Redl | b426f633 | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 585 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 586 | Self.MarkVTableUsed(OpRange.getBegin(), |
| 587 | cast<CXXRecordDecl>(SrcRecord->getDecl())); |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 588 | |
| 589 | // Done. Everything else is run-time checks. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 590 | Kind = CK_Dynamic; |
Sebastian Redl | 3c5aa4d | 2008-11-05 21:50:06 +0000 | [diff] [blame] | 591 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 592 | |
| 593 | /// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid. |
| 594 | /// Refer to C++ 5.2.11 for details. const_cast is typically used in code |
| 595 | /// like this: |
| 596 | /// const char *str = "literal"; |
| 597 | /// legacy_function(const_cast\<char*\>(str)); |
| 598 | void |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 599 | CheckConstCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, ExprValueKind &VK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | const SourceRange &OpRange, const SourceRange &DestRange) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 601 | VK = Expr::getValueKindForType(DestType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 602 | if (VK == VK_RValue) { |
| 603 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
| 604 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 605 | return; |
| 606 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 607 | |
| 608 | unsigned msg = diag::err_bad_cxx_cast_generic; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 609 | if (TryConstCast(Self, SrcExpr.get(), DestType, /*CStyle*/false, msg) != TC_Success |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 610 | && msg != 0) |
| 611 | Self.Diag(OpRange.getBegin(), msg) << CT_Const |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 612 | << SrcExpr.get()->getType() << DestType << OpRange; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is |
| 616 | /// valid. |
| 617 | /// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code |
| 618 | /// like this: |
| 619 | /// char *bytes = reinterpret_cast\<char*\>(int_ptr); |
| 620 | void |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 621 | CheckReinterpretCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 622 | ExprValueKind &VK, const SourceRange &OpRange, |
| 623 | const SourceRange &DestRange, CastKind &Kind) { |
| 624 | VK = Expr::getValueKindForType(DestType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 625 | if (VK == VK_RValue) { |
| 626 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
| 627 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 628 | return; |
| 629 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 630 | |
| 631 | unsigned msg = diag::err_bad_cxx_cast_generic; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 632 | TryCastResult tcr = |
| 633 | TryReinterpretCast(Self, SrcExpr, DestType, |
| 634 | /*CStyle*/false, OpRange, msg, Kind); |
| 635 | if (tcr != TC_Success && msg != 0) |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 636 | { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 637 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 638 | return; |
| 639 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 640 | //FIXME: &f<int>; is overloaded and resolvable |
| 641 | Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 642 | << OverloadExpr::find(SrcExpr.get()).Expression->getName() |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 643 | << DestType << OpRange; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 644 | Self.NoteAllOverloadCandidates(SrcExpr.get()); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 645 | |
John McCall | 909acf8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 646 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 647 | diagnoseBadCast(Self, msg, CT_Reinterpret, OpRange, SrcExpr.get(), DestType); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 648 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 649 | } else if (tcr == TC_Success && Self.getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | 7887637 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 650 | Expr *Exp = SrcExpr.get(); |
Fariborz Jahanian | 2e9e89a | 2011-06-21 18:21:32 +0000 | [diff] [blame] | 651 | // Note that Exp does not change with CCK_OtherCast cast type |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 652 | Self.CheckObjCARCConversion(OpRange, DestType, |
Fariborz Jahanian | 7887637 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 653 | Exp, Sema::CCK_OtherCast); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 654 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | |
| 658 | /// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid. |
| 659 | /// Refer to C++ 5.2.9 for details. Static casts are mostly used for making |
| 660 | /// implicit conversions explicit and getting rid of data loss warnings. |
| 661 | void |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 662 | CheckStaticCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 663 | ExprValueKind &VK, const SourceRange &OpRange, |
| 664 | CastKind &Kind, CXXCastPath &BasePath) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 665 | // This test is outside everything else because it's the only case where |
| 666 | // a non-lvalue-reference target type does not lead to decay. |
| 667 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
Eli Friedman | 03bf60a | 2009-11-16 05:44:20 +0000 | [diff] [blame] | 668 | if (DestType->isVoidType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 669 | SrcExpr = Self.IgnoredValueConversions(SrcExpr.take()); |
| 670 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 671 | return; |
| 672 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 673 | ExprResult SingleFunctionExpression = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 674 | Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr.get(), |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 675 | false, // Decay Function to ptr |
| 676 | true, // Complain |
| 677 | OpRange, DestType, diag::err_bad_static_cast_overload); |
| 678 | if (SingleFunctionExpression.isUsable()) |
| 679 | { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 680 | SrcExpr = SingleFunctionExpression; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 681 | Kind = CK_ToVoid; |
| 682 | } |
| 683 | } |
| 684 | else |
| 685 | Kind = CK_ToVoid; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 686 | return; |
Eli Friedman | 03bf60a | 2009-11-16 05:44:20 +0000 | [diff] [blame] | 687 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 688 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 689 | VK = Expr::getValueKindForType(DestType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 690 | if (VK == VK_RValue && !DestType->isRecordType()) { |
| 691 | SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.take()); |
| 692 | if (SrcExpr.isInvalid()) // if conversion failed, don't report another error |
| 693 | return; |
| 694 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 695 | |
| 696 | unsigned msg = diag::err_bad_cxx_cast_generic; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 697 | TryCastResult tcr |
| 698 | = TryStaticCast(Self, SrcExpr, DestType, Sema::CCK_OtherCast, OpRange, msg, |
| 699 | Kind, BasePath); |
| 700 | if (tcr != TC_Success && msg != 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 701 | if (SrcExpr.isInvalid()) |
| 702 | return; |
| 703 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
| 704 | OverloadExpr* oe = OverloadExpr::find(SrcExpr.get()).Expression; |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 705 | Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload) |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 706 | << oe->getName() << DestType << OpRange |
| 707 | << oe->getQualifierLoc().getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 708 | Self.NoteAllOverloadCandidates(SrcExpr.get()); |
John McCall | 909acf8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 709 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 710 | diagnoseBadCast(Self, msg, CT_Static, OpRange, SrcExpr.get(), DestType); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 711 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 712 | } else if (tcr == TC_Success) { |
| 713 | if (Kind == CK_BitCast) |
| 714 | Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange); |
Fariborz Jahanian | 7887637 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 715 | if (Self.getLangOptions().ObjCAutoRefCount) { |
| 716 | Expr *Exp = SrcExpr.get(); |
Fariborz Jahanian | 2e9e89a | 2011-06-21 18:21:32 +0000 | [diff] [blame] | 717 | // Note that Exp does not change with CCK_OtherCast cast type |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 718 | Self.CheckObjCARCConversion(OpRange, DestType, |
Fariborz Jahanian | 7887637 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 719 | Exp, Sema::CCK_OtherCast); |
| 720 | } |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 721 | } |
John McCall | d50a271 | 2010-11-16 05:46:29 +0000 | [diff] [blame] | 722 | else if (Kind == CK_BitCast) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 723 | Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /// TryStaticCast - Check if a static cast can be performed, and do so if |
| 727 | /// possible. If @p CStyle, ignore access restrictions on hierarchy casting |
| 728 | /// and casting away constness. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 729 | static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 730 | QualType DestType, |
| 731 | Sema::CheckedConversionKind CCK, |
Anders Carlsson | f1ae6d4 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 732 | const SourceRange &OpRange, unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 733 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 734 | CXXCastPath &BasePath) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 735 | // Determine whether we have the semantics of a C-style cast. |
| 736 | bool CStyle |
| 737 | = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast); |
| 738 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 739 | // The order the tests is not entirely arbitrary. There is one conversion |
| 740 | // that can be handled in two different ways. Given: |
| 741 | // struct A {}; |
| 742 | // struct B : public A { |
| 743 | // B(); B(const A&); |
| 744 | // }; |
| 745 | // const A &a = B(); |
| 746 | // the cast static_cast<const B&>(a) could be seen as either a static |
| 747 | // reference downcast, or an explicit invocation of the user-defined |
| 748 | // conversion using B's conversion constructor. |
| 749 | // DR 427 specifies that the downcast is to be applied here. |
| 750 | |
| 751 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
| 752 | // Done outside this function. |
| 753 | |
| 754 | TryCastResult tcr; |
| 755 | |
| 756 | // C++ 5.2.9p5, reference downcast. |
| 757 | // See the function for details. |
| 758 | // DR 427 specifies that this is to be applied before paragraph 2. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 759 | tcr = TryStaticReferenceDowncast(Self, SrcExpr.get(), DestType, CStyle, OpRange, |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 760 | msg, Kind, BasePath); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 761 | if (tcr != TC_NotApplicable) |
| 762 | return tcr; |
| 763 | |
Douglas Gregor | 465184a | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 764 | // C++0x [expr.static.cast]p3: |
| 765 | // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2 |
| 766 | // T2" if "cv2 T2" is reference-compatible with "cv1 T1". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 767 | tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind, BasePath, |
Douglas Gregor | ce95084 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 768 | msg); |
Douglas Gregor | ba278e2 | 2011-01-25 16:13:26 +0000 | [diff] [blame] | 769 | if (tcr != TC_NotApplicable) |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 770 | return tcr; |
| 771 | |
| 772 | // C++ 5.2.9p2: An expression e can be explicitly converted to a type T |
| 773 | // [...] if the declaration "T t(e);" is well-formed, [...]. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 774 | tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CCK, OpRange, msg, |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 775 | Kind); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 776 | if (SrcExpr.isInvalid()) |
| 777 | return TC_Failed; |
Anders Carlsson | 9d1b34b | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 778 | if (tcr != TC_NotApplicable) |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 779 | return tcr; |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 780 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 781 | // C++ 5.2.9p6: May apply the reverse of any standard conversion, except |
| 782 | // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean |
| 783 | // conversions, subject to further restrictions. |
| 784 | // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal |
| 785 | // of qualification conversions impossible. |
| 786 | // In the CStyle case, the earlier attempt to const_cast should have taken |
| 787 | // care of reverse qualification conversions. |
| 788 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 789 | QualType SrcType = Self.Context.getCanonicalType(SrcExpr.get()->getType()); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 791 | // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly |
Douglas Gregor | b327eac | 2011-02-18 03:01:41 +0000 | [diff] [blame] | 792 | // converted to an integral type. [...] A value of a scoped enumeration type |
| 793 | // can also be explicitly converted to a floating-point type [...]. |
| 794 | if (const EnumType *Enum = SrcType->getAs<EnumType>()) { |
| 795 | if (Enum->getDecl()->isScoped()) { |
| 796 | if (DestType->isBooleanType()) { |
| 797 | Kind = CK_IntegralToBoolean; |
| 798 | return TC_Success; |
| 799 | } else if (DestType->isIntegralType(Self.Context)) { |
| 800 | Kind = CK_IntegralCast; |
| 801 | return TC_Success; |
| 802 | } else if (DestType->isRealFloatingType()) { |
| 803 | Kind = CK_IntegralToFloating; |
| 804 | return TC_Success; |
| 805 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
Douglas Gregor | b327eac | 2011-02-18 03:01:41 +0000 | [diff] [blame] | 808 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 809 | // Reverse integral promotion/conversion. All such conversions are themselves |
| 810 | // again integral promotions or conversions and are thus already handled by |
| 811 | // p2 (TryDirectInitialization above). |
| 812 | // (Note: any data loss warnings should be suppressed.) |
| 813 | // The exception is the reverse of enum->integer, i.e. integer->enum (and |
| 814 | // enum->enum). See also C++ 5.2.9p7. |
| 815 | // The same goes for reverse floating point promotion/conversion and |
| 816 | // floating-integral conversions. Again, only floating->enum is relevant. |
| 817 | if (DestType->isEnumeralType()) { |
Eli Friedman | 2953889 | 2011-09-02 17:38:59 +0000 | [diff] [blame] | 818 | if (SrcType->isIntegralOrEnumerationType()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 819 | Kind = CK_IntegralCast; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 820 | return TC_Success; |
Eli Friedman | 2953889 | 2011-09-02 17:38:59 +0000 | [diff] [blame] | 821 | } else if (SrcType->isRealFloatingType()) { |
| 822 | Kind = CK_FloatingToIntegral; |
| 823 | return TC_Success; |
Eli Friedman | 03bf60a | 2009-11-16 05:44:20 +0000 | [diff] [blame] | 824 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast. |
| 828 | // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance. |
Anders Carlsson | 9a1cd87 | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 829 | tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg, |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 830 | Kind, BasePath); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 831 | if (tcr != TC_NotApplicable) |
| 832 | return tcr; |
| 833 | |
| 834 | // Reverse member pointer conversion. C++ 4.11 specifies member pointer |
| 835 | // conversion. C++ 5.2.9p9 has additional information. |
| 836 | // DR54's access restrictions apply here also. |
Douglas Gregor | c934bc8 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 837 | tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 838 | OpRange, msg, Kind, BasePath); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 839 | if (tcr != TC_NotApplicable) |
| 840 | return tcr; |
| 841 | |
| 842 | // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to |
| 843 | // void*. C++ 5.2.9p10 specifies additional restrictions, which really is |
| 844 | // just the usual constness stuff. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 845 | if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 846 | QualType SrcPointee = SrcPointer->getPointeeType(); |
| 847 | if (SrcPointee->isVoidType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 848 | if (const PointerType *DestPointer = DestType->getAs<PointerType>()) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 849 | QualType DestPointee = DestPointer->getPointeeType(); |
| 850 | if (DestPointee->isIncompleteOrObjectType()) { |
| 851 | // This is definitely the intended conversion, but it might fail due |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 852 | // to a qualifier violation. Note that we permit Objective-C lifetime |
| 853 | // and GC qualifier mismatches here. |
| 854 | if (!CStyle) { |
| 855 | Qualifiers DestPointeeQuals = DestPointee.getQualifiers(); |
| 856 | Qualifiers SrcPointeeQuals = SrcPointee.getQualifiers(); |
| 857 | DestPointeeQuals.removeObjCGCAttr(); |
| 858 | DestPointeeQuals.removeObjCLifetime(); |
| 859 | SrcPointeeQuals.removeObjCGCAttr(); |
| 860 | SrcPointeeQuals.removeObjCLifetime(); |
| 861 | if (DestPointeeQuals != SrcPointeeQuals && |
| 862 | !DestPointeeQuals.compatiblyIncludes(SrcPointeeQuals)) { |
| 863 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
| 864 | return TC_Failed; |
| 865 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 866 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 867 | Kind = CK_BitCast; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 868 | return TC_Success; |
| 869 | } |
| 870 | } |
Fariborz Jahanian | eee1669 | 2010-05-10 23:46:53 +0000 | [diff] [blame] | 871 | else if (DestType->isObjCObjectPointerType()) { |
| 872 | // allow both c-style cast and static_cast of objective-c pointers as |
| 873 | // they are pervasive. |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 874 | Kind = CK_CPointerToObjCPointerCast; |
Fariborz Jahanian | 859c415 | 2009-12-08 23:09:15 +0000 | [diff] [blame] | 875 | return TC_Success; |
| 876 | } |
Fariborz Jahanian | ffe912c | 2009-12-11 22:40:48 +0000 | [diff] [blame] | 877 | else if (CStyle && DestType->isBlockPointerType()) { |
| 878 | // allow c-style cast of void * to block pointers. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 879 | Kind = CK_AnyPointerToBlockPointerCast; |
Fariborz Jahanian | ffe912c | 2009-12-11 22:40:48 +0000 | [diff] [blame] | 880 | return TC_Success; |
| 881 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
Fariborz Jahanian | b0901b7 | 2010-05-12 18:16:59 +0000 | [diff] [blame] | 884 | // Allow arbitray objective-c pointer conversion with static casts. |
| 885 | if (SrcType->isObjCObjectPointerType() && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 886 | DestType->isObjCObjectPointerType()) { |
| 887 | Kind = CK_BitCast; |
Fariborz Jahanian | b0901b7 | 2010-05-12 18:16:59 +0000 | [diff] [blame] | 888 | return TC_Success; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 889 | } |
Fariborz Jahanian | b0901b7 | 2010-05-12 18:16:59 +0000 | [diff] [blame] | 890 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 891 | // We tried everything. Everything! Nothing works! :-( |
| 892 | return TC_NotApplicable; |
| 893 | } |
| 894 | |
| 895 | /// Tests whether a conversion according to N2844 is valid. |
| 896 | TryCastResult |
| 897 | TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType, |
Douglas Gregor | ce95084 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 898 | bool CStyle, CastKind &Kind, CXXCastPath &BasePath, |
| 899 | unsigned &msg) { |
Douglas Gregor | 465184a | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 900 | // C++0x [expr.static.cast]p3: |
| 901 | // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to |
| 902 | // cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1". |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 903 | const RValueReferenceType *R = DestType->getAs<RValueReferenceType>(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 904 | if (!R) |
| 905 | return TC_NotApplicable; |
| 906 | |
Douglas Gregor | 465184a | 2011-01-22 00:06:57 +0000 | [diff] [blame] | 907 | if (!SrcExpr->isGLValue()) |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 908 | return TC_NotApplicable; |
| 909 | |
| 910 | // Because we try the reference downcast before this function, from now on |
| 911 | // this is the only cast possibility, so we issue an error if we fail now. |
| 912 | // FIXME: Should allow casting away constness if CStyle. |
| 913 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 914 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 915 | bool ObjCLifetimeConversion; |
Douglas Gregor | ce95084 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 916 | QualType FromType = SrcExpr->getType(); |
| 917 | QualType ToType = R->getPointeeType(); |
| 918 | if (CStyle) { |
| 919 | FromType = FromType.getUnqualifiedType(); |
| 920 | ToType = ToType.getUnqualifiedType(); |
| 921 | } |
| 922 | |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 923 | if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(), |
Douglas Gregor | ce95084 | 2011-01-26 21:04:06 +0000 | [diff] [blame] | 924 | ToType, FromType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 925 | DerivedToBase, ObjCConversion, |
| 926 | ObjCLifetimeConversion) |
| 927 | < Sema::Ref_Compatible_With_Added_Qualification) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 928 | msg = diag::err_bad_lvalue_to_rvalue_cast; |
| 929 | return TC_Failed; |
| 930 | } |
| 931 | |
Douglas Gregor | ba278e2 | 2011-01-25 16:13:26 +0000 | [diff] [blame] | 932 | if (DerivedToBase) { |
| 933 | Kind = CK_DerivedToBase; |
| 934 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 935 | /*DetectVirtual=*/true); |
| 936 | if (!Self.IsDerivedFrom(SrcExpr->getType(), R->getPointeeType(), Paths)) |
| 937 | return TC_NotApplicable; |
| 938 | |
| 939 | Self.BuildBasePathArray(Paths, BasePath); |
| 940 | } else |
| 941 | Kind = CK_NoOp; |
| 942 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 943 | return TC_Success; |
| 944 | } |
| 945 | |
| 946 | /// Tests whether a conversion according to C++ 5.2.9p5 is valid. |
| 947 | TryCastResult |
| 948 | TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType, |
| 949 | bool CStyle, const SourceRange &OpRange, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 950 | unsigned &msg, CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 951 | CXXCastPath &BasePath) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 952 | // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be |
| 953 | // cast to type "reference to cv2 D", where D is a class derived from B, |
| 954 | // if a valid standard conversion from "pointer to D" to "pointer to B" |
| 955 | // exists, cv2 >= cv1, and B is not a virtual base class of D. |
| 956 | // In addition, DR54 clarifies that the base must be accessible in the |
| 957 | // current context. Although the wording of DR54 only applies to the pointer |
| 958 | // variant of this rule, the intent is clearly for it to apply to the this |
| 959 | // conversion as well. |
| 960 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 961 | const ReferenceType *DestReference = DestType->getAs<ReferenceType>(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 962 | if (!DestReference) { |
| 963 | return TC_NotApplicable; |
| 964 | } |
| 965 | bool RValueRef = DestReference->isRValueReferenceType(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 966 | if (!RValueRef && !SrcExpr->isLValue()) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 967 | // We know the left side is an lvalue reference, so we can suggest a reason. |
| 968 | msg = diag::err_bad_cxx_cast_rvalue; |
| 969 | return TC_NotApplicable; |
| 970 | } |
| 971 | |
| 972 | QualType DestPointee = DestReference->getPointeeType(); |
| 973 | |
Douglas Gregor | 8f3952c | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 974 | return TryStaticDowncast(Self, |
| 975 | Self.Context.getCanonicalType(SrcExpr->getType()), |
| 976 | Self.Context.getCanonicalType(DestPointee), CStyle, |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 977 | OpRange, SrcExpr->getType(), DestType, msg, Kind, |
| 978 | BasePath); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /// Tests whether a conversion according to C++ 5.2.9p8 is valid. |
| 982 | TryCastResult |
| 983 | TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | bool CStyle, const SourceRange &OpRange, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 985 | unsigned &msg, CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 986 | CXXCastPath &BasePath) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 987 | // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class |
| 988 | // type, can be converted to an rvalue of type "pointer to cv2 D", where D |
| 989 | // is a class derived from B, if a valid standard conversion from "pointer |
| 990 | // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base |
| 991 | // class of D. |
| 992 | // In addition, DR54 clarifies that the base must be accessible in the |
| 993 | // current context. |
| 994 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 995 | const PointerType *DestPointer = DestType->getAs<PointerType>(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 996 | if (!DestPointer) { |
| 997 | return TC_NotApplicable; |
| 998 | } |
| 999 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1000 | const PointerType *SrcPointer = SrcType->getAs<PointerType>(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1001 | if (!SrcPointer) { |
| 1002 | msg = diag::err_bad_static_cast_pointer_nonpointer; |
| 1003 | return TC_NotApplicable; |
| 1004 | } |
| 1005 | |
Douglas Gregor | 8f3952c | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1006 | return TryStaticDowncast(Self, |
| 1007 | Self.Context.getCanonicalType(SrcPointer->getPointeeType()), |
| 1008 | Self.Context.getCanonicalType(DestPointer->getPointeeType()), |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1009 | CStyle, OpRange, SrcType, DestType, msg, Kind, |
| 1010 | BasePath); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | /// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and |
| 1014 | /// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to |
Douglas Gregor | 8f3952c | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1015 | /// DestType is possible and allowed. |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1016 | TryCastResult |
Douglas Gregor | 8f3952c | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1017 | TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType, |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1018 | bool CStyle, const SourceRange &OpRange, QualType OrigSrcType, |
Anders Carlsson | 9a1cd87 | 2009-11-12 16:53:16 +0000 | [diff] [blame] | 1019 | QualType OrigDestType, unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1020 | CastKind &Kind, CXXCastPath &BasePath) { |
Sebastian Redl | 802f14c | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 1021 | // We can only work with complete types. But don't complain if it doesn't work |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 1022 | if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) || |
| 1023 | Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0))) |
Sebastian Redl | 802f14c | 2009-10-22 15:07:22 +0000 | [diff] [blame] | 1024 | return TC_NotApplicable; |
| 1025 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1026 | // Downcast can only happen in class hierarchies, so we need classes. |
Douglas Gregor | 8f3952c | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1027 | if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1028 | return TC_NotApplicable; |
| 1029 | } |
| 1030 | |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1031 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1032 | /*DetectVirtual=*/true); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1033 | if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) { |
| 1034 | return TC_NotApplicable; |
| 1035 | } |
| 1036 | |
| 1037 | // Target type does derive from source type. Now we're serious. If an error |
| 1038 | // appears now, it's not ignored. |
| 1039 | // This may not be entirely in line with the standard. Take for example: |
| 1040 | // struct A {}; |
| 1041 | // struct B : virtual A { |
| 1042 | // B(A&); |
| 1043 | // }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | // |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1045 | // void f() |
| 1046 | // { |
| 1047 | // (void)static_cast<const B&>(*((A*)0)); |
| 1048 | // } |
| 1049 | // As far as the standard is concerned, p5 does not apply (A is virtual), so |
| 1050 | // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid. |
| 1051 | // However, both GCC and Comeau reject this example, and accepting it would |
| 1052 | // mean more complex code if we're to preserve the nice error message. |
| 1053 | // FIXME: Being 100% compliant here would be nice to have. |
| 1054 | |
| 1055 | // Must preserve cv, as always, unless we're in C-style mode. |
| 1056 | if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) { |
Douglas Gregor | b472e93 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1057 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1058 | return TC_Failed; |
| 1059 | } |
| 1060 | |
| 1061 | if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) { |
| 1062 | // This code is analoguous to that in CheckDerivedToBaseConversion, except |
| 1063 | // that it builds the paths in reverse order. |
| 1064 | // To sum up: record all paths to the base and build a nice string from |
| 1065 | // them. Use it to spice up the error message. |
| 1066 | if (!Paths.isRecordingPaths()) { |
| 1067 | Paths.clear(); |
| 1068 | Paths.setRecordingPaths(true); |
| 1069 | Self.IsDerivedFrom(DestType, SrcType, Paths); |
| 1070 | } |
| 1071 | std::string PathDisplayStr; |
| 1072 | std::set<unsigned> DisplayedPaths; |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1073 | for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1074 | PI != PE; ++PI) { |
| 1075 | if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) { |
| 1076 | // We haven't displayed a path to this particular base |
| 1077 | // class subobject yet. |
| 1078 | PathDisplayStr += "\n "; |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1079 | for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(), |
| 1080 | EE = PI->rend(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1081 | EI != EE; ++EI) |
| 1082 | PathDisplayStr += EI->Base->getType().getAsString() + " -> "; |
Douglas Gregor | 8f3952c | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1083 | PathDisplayStr += QualType(DestType).getAsString(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast) |
Douglas Gregor | 8f3952c | 2009-11-15 09:20:52 +0000 | [diff] [blame] | 1088 | << QualType(SrcType).getUnqualifiedType() |
| 1089 | << QualType(DestType).getUnqualifiedType() |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1090 | << PathDisplayStr << OpRange; |
| 1091 | msg = 0; |
| 1092 | return TC_Failed; |
| 1093 | } |
| 1094 | |
| 1095 | if (Paths.getDetectedVirtual() != 0) { |
| 1096 | QualType VirtualBase(Paths.getDetectedVirtual(), 0); |
| 1097 | Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual) |
| 1098 | << OrigSrcType << OrigDestType << VirtualBase << OpRange; |
| 1099 | msg = 0; |
| 1100 | return TC_Failed; |
| 1101 | } |
| 1102 | |
John McCall | fe9cf0a | 2011-02-14 23:21:33 +0000 | [diff] [blame] | 1103 | if (!CStyle) { |
| 1104 | switch (Self.CheckBaseClassAccess(OpRange.getBegin(), |
| 1105 | SrcType, DestType, |
| 1106 | Paths.front(), |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1107 | diag::err_downcast_from_inaccessible_base)) { |
John McCall | fe9cf0a | 2011-02-14 23:21:33 +0000 | [diff] [blame] | 1108 | case Sema::AR_accessible: |
| 1109 | case Sema::AR_delayed: // be optimistic |
| 1110 | case Sema::AR_dependent: // be optimistic |
| 1111 | break; |
| 1112 | |
| 1113 | case Sema::AR_inaccessible: |
| 1114 | msg = 0; |
| 1115 | return TC_Failed; |
| 1116 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1119 | Self.BuildBasePathArray(Paths, BasePath); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1120 | Kind = CK_BaseToDerived; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1121 | return TC_Success; |
| 1122 | } |
| 1123 | |
| 1124 | /// TryStaticMemberPointerUpcast - Tests whether a conversion according to |
| 1125 | /// C++ 5.2.9p9 is valid: |
| 1126 | /// |
| 1127 | /// An rvalue of type "pointer to member of D of type cv1 T" can be |
| 1128 | /// converted to an rvalue of type "pointer to member of B of type cv2 T", |
| 1129 | /// where B is a base class of D [...]. |
| 1130 | /// |
| 1131 | TryCastResult |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1132 | TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, QualType SrcType, |
Douglas Gregor | c934bc8 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1133 | QualType DestType, bool CStyle, |
| 1134 | const SourceRange &OpRange, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1135 | unsigned &msg, CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1136 | CXXCastPath &BasePath) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1137 | const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1138 | if (!DestMemPtr) |
| 1139 | return TC_NotApplicable; |
Douglas Gregor | c934bc8 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1140 | |
| 1141 | bool WasOverloadedFunction = false; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1142 | DeclAccessPair FoundOverload; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1143 | if (SrcExpr.get()->getType() == Self.Context.OverloadTy) { |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 1144 | if (FunctionDecl *Fn |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1145 | = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), DestType, false, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 1146 | FoundOverload)) { |
| 1147 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 1148 | SrcType = Self.Context.getMemberPointerType(Fn->getType(), |
| 1149 | Self.Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 1150 | WasOverloadedFunction = true; |
| 1151 | } |
Douglas Gregor | c934bc8 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1152 | } |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 1153 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1154 | const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1155 | if (!SrcMemPtr) { |
| 1156 | msg = diag::err_bad_static_cast_member_pointer_nonmp; |
| 1157 | return TC_NotApplicable; |
| 1158 | } |
| 1159 | |
| 1160 | // T == T, modulo cv |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1161 | if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(), |
| 1162 | DestMemPtr->getPointeeType())) |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1163 | return TC_NotApplicable; |
| 1164 | |
| 1165 | // B base of D |
| 1166 | QualType SrcClass(SrcMemPtr->getClass(), 0); |
| 1167 | QualType DestClass(DestMemPtr->getClass(), 0); |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1168 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1169 | /*DetectVirtual=*/true); |
| 1170 | if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) { |
| 1171 | return TC_NotApplicable; |
| 1172 | } |
| 1173 | |
| 1174 | // B is a base of D. But is it an allowed base? If not, it's a hard error. |
Douglas Gregor | 27ac429 | 2010-05-21 20:29:55 +0000 | [diff] [blame] | 1175 | if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1176 | Paths.clear(); |
| 1177 | Paths.setRecordingPaths(true); |
| 1178 | bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths); |
| 1179 | assert(StillOkay); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1180 | (void)StillOkay; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1181 | std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths); |
| 1182 | Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv) |
| 1183 | << 1 << SrcClass << DestClass << PathDisplayStr << OpRange; |
| 1184 | msg = 0; |
| 1185 | return TC_Failed; |
| 1186 | } |
| 1187 | |
| 1188 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
| 1189 | Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual) |
| 1190 | << SrcClass << DestClass << QualType(VBase, 0) << OpRange; |
| 1191 | msg = 0; |
| 1192 | return TC_Failed; |
| 1193 | } |
| 1194 | |
John McCall | fe9cf0a | 2011-02-14 23:21:33 +0000 | [diff] [blame] | 1195 | if (!CStyle) { |
| 1196 | switch (Self.CheckBaseClassAccess(OpRange.getBegin(), |
| 1197 | DestClass, SrcClass, |
| 1198 | Paths.front(), |
| 1199 | diag::err_upcast_to_inaccessible_base)) { |
| 1200 | case Sema::AR_accessible: |
| 1201 | case Sema::AR_delayed: |
| 1202 | case Sema::AR_dependent: |
| 1203 | // Optimistically assume that the delayed and dependent cases |
| 1204 | // will work out. |
| 1205 | break; |
| 1206 | |
| 1207 | case Sema::AR_inaccessible: |
| 1208 | msg = 0; |
| 1209 | return TC_Failed; |
| 1210 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Douglas Gregor | c934bc8 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1213 | if (WasOverloadedFunction) { |
| 1214 | // Resolve the address of the overloaded function again, this time |
| 1215 | // allowing complaints if something goes wrong. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1216 | FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), |
Douglas Gregor | c934bc8 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1217 | DestType, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1218 | true, |
| 1219 | FoundOverload); |
Douglas Gregor | c934bc8 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1220 | if (!Fn) { |
| 1221 | msg = 0; |
| 1222 | return TC_Failed; |
| 1223 | } |
| 1224 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1225 | SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1226 | if (!SrcExpr.isUsable()) { |
Douglas Gregor | c934bc8 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 1227 | msg = 0; |
| 1228 | return TC_Failed; |
| 1229 | } |
| 1230 | } |
| 1231 | |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1232 | Self.BuildBasePathArray(Paths, BasePath); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1233 | Kind = CK_DerivedToBaseMemberPointer; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1234 | return TC_Success; |
| 1235 | } |
| 1236 | |
| 1237 | /// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 |
| 1238 | /// is valid: |
| 1239 | /// |
| 1240 | /// An expression e can be explicitly converted to a type T using a |
| 1241 | /// @c static_cast if the declaration "T t(e);" is well-formed [...]. |
| 1242 | TryCastResult |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1243 | TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1244 | Sema::CheckedConversionKind CCK, |
| 1245 | const SourceRange &OpRange, unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1246 | CastKind &Kind) { |
Anders Carlsson | 85ec4ff | 2009-09-07 18:25:47 +0000 | [diff] [blame] | 1247 | if (DestType->isRecordType()) { |
| 1248 | if (Self.RequireCompleteType(OpRange.getBegin(), DestType, |
| 1249 | diag::err_bad_dynamic_cast_incomplete)) { |
| 1250 | msg = 0; |
| 1251 | return TC_Failed; |
| 1252 | } |
| 1253 | } |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1254 | |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 1255 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType); |
| 1256 | InitializationKind InitKind |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1257 | = (CCK == Sema::CCK_CStyleCast) |
| 1258 | ? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange) |
| 1259 | : (CCK == Sema::CCK_FunctionalCast) |
| 1260 | ? InitializationKind::CreateFunctionalCast(OpRange) |
| 1261 | : InitializationKind::CreateCast(OpRange); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1262 | Expr *SrcExprRaw = SrcExpr.get(); |
| 1263 | InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExprRaw, 1); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1264 | |
| 1265 | // At this point of CheckStaticCast, if the destination is a reference, |
| 1266 | // or the expression is an overload expression this has to work. |
| 1267 | // There is no other way that works. |
| 1268 | // On the other hand, if we're checking a C-style cast, we've still got |
| 1269 | // the reinterpret_cast way. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1270 | bool CStyle |
| 1271 | = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 1272 | if (InitSeq.Failed() && (CStyle || !DestType->isReferenceType())) |
Anders Carlsson | 9d1b34b | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 1273 | return TC_NotApplicable; |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1274 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1275 | ExprResult Result |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1276 | = InitSeq.Perform(Self, Entity, InitKind, MultiExprArg(Self, &SrcExprRaw, 1)); |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 1277 | if (Result.isInvalid()) { |
| 1278 | msg = 0; |
| 1279 | return TC_Failed; |
| 1280 | } |
| 1281 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1282 | if (InitSeq.isConstructorInitialization()) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1283 | Kind = CK_ConstructorConversion; |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1284 | else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1285 | Kind = CK_NoOp; |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 1286 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1287 | SrcExpr = move(Result); |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 1288 | return TC_Success; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | /// TryConstCast - See if a const_cast from source to destination is allowed, |
| 1292 | /// and perform it if it is. |
| 1293 | static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType, |
| 1294 | bool CStyle, unsigned &msg) { |
| 1295 | DestType = Self.Context.getCanonicalType(DestType); |
| 1296 | QualType SrcType = SrcExpr->getType(); |
Douglas Gregor | c1ed20c | 2011-01-22 00:19:52 +0000 | [diff] [blame] | 1297 | if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) { |
| 1298 | if (DestTypeTmp->isLValueReferenceType() && !SrcExpr->isLValue()) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1299 | // Cannot const_cast non-lvalue to lvalue reference type. But if this |
| 1300 | // is C-style, static_cast might find a way, so we simply suggest a |
| 1301 | // message and tell the parent to keep searching. |
| 1302 | msg = diag::err_bad_cxx_cast_rvalue; |
| 1303 | return TC_NotApplicable; |
| 1304 | } |
| 1305 | |
| 1306 | // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2 |
| 1307 | // [...] if a pointer to T1 can be [cast] to the type pointer to T2. |
| 1308 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); |
| 1309 | SrcType = Self.Context.getPointerType(SrcType); |
| 1310 | } |
| 1311 | |
| 1312 | // C++ 5.2.11p5: For a const_cast involving pointers to data members [...] |
| 1313 | // the rules for const_cast are the same as those used for pointers. |
| 1314 | |
John McCall | 0e704f7 | 2010-05-18 09:35:29 +0000 | [diff] [blame] | 1315 | if (!DestType->isPointerType() && |
| 1316 | !DestType->isMemberPointerType() && |
| 1317 | !DestType->isObjCObjectPointerType()) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1318 | // Cannot cast to non-pointer, non-reference type. Note that, if DestType |
| 1319 | // was a reference type, we converted it to a pointer above. |
| 1320 | // The status of rvalue references isn't entirely clear, but it looks like |
| 1321 | // conversion to them is simply invalid. |
| 1322 | // C++ 5.2.11p3: For two pointer types [...] |
| 1323 | if (!CStyle) |
| 1324 | msg = diag::err_bad_const_cast_dest; |
| 1325 | return TC_NotApplicable; |
| 1326 | } |
| 1327 | if (DestType->isFunctionPointerType() || |
| 1328 | DestType->isMemberFunctionPointerType()) { |
| 1329 | // Cannot cast direct function pointers. |
| 1330 | // C++ 5.2.11p2: [...] where T is any object type or the void type [...] |
| 1331 | // T is the ultimate pointee of source and target type. |
| 1332 | if (!CStyle) |
| 1333 | msg = diag::err_bad_const_cast_dest; |
| 1334 | return TC_NotApplicable; |
| 1335 | } |
| 1336 | SrcType = Self.Context.getCanonicalType(SrcType); |
| 1337 | |
| 1338 | // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are |
| 1339 | // completely equal. |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1340 | // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers |
| 1341 | // in multi-level pointers may change, but the level count must be the same, |
| 1342 | // as must be the final pointee type. |
| 1343 | while (SrcType != DestType && |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 1344 | Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) { |
Douglas Gregor | b472e93 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1345 | Qualifiers SrcQuals, DestQuals; |
| 1346 | SrcType = Self.Context.getUnqualifiedArrayType(SrcType, SrcQuals); |
| 1347 | DestType = Self.Context.getUnqualifiedArrayType(DestType, DestQuals); |
| 1348 | |
| 1349 | // const_cast is permitted to strip cvr-qualifiers, only. Make sure that |
| 1350 | // the other qualifiers (e.g., address spaces) are identical. |
| 1351 | SrcQuals.removeCVRQualifiers(); |
| 1352 | DestQuals.removeCVRQualifiers(); |
| 1353 | if (SrcQuals != DestQuals) |
| 1354 | return TC_NotApplicable; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | // Since we're dealing in canonical types, the remainder must be the same. |
| 1358 | if (SrcType != DestType) |
| 1359 | return TC_NotApplicable; |
| 1360 | |
| 1361 | return TC_Success; |
| 1362 | } |
| 1363 | |
Argyrios Kyrtzidis | 69a2c92 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1364 | // Checks for undefined behavior in reinterpret_cast. |
| 1365 | // The cases that is checked for is: |
| 1366 | // *reinterpret_cast<T*>(&a) |
| 1367 | // reinterpret_cast<T&>(a) |
| 1368 | // where accessing 'a' as type 'T' will result in undefined behavior. |
| 1369 | void Sema::CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType, |
| 1370 | bool IsDereference, |
| 1371 | SourceRange Range) { |
| 1372 | unsigned DiagID = IsDereference ? |
| 1373 | diag::warn_pointer_indirection_from_incompatible_type : |
| 1374 | diag::warn_undefined_reinterpret_cast; |
| 1375 | |
| 1376 | if (Diags.getDiagnosticLevel(DiagID, Range.getBegin()) == |
| 1377 | Diagnostic::Ignored) { |
| 1378 | return; |
| 1379 | } |
| 1380 | |
| 1381 | QualType SrcTy, DestTy; |
| 1382 | if (IsDereference) { |
| 1383 | if (!SrcType->getAs<PointerType>() || !DestType->getAs<PointerType>()) { |
| 1384 | return; |
| 1385 | } |
| 1386 | SrcTy = SrcType->getPointeeType(); |
| 1387 | DestTy = DestType->getPointeeType(); |
| 1388 | } else { |
| 1389 | if (!DestType->getAs<ReferenceType>()) { |
| 1390 | return; |
| 1391 | } |
| 1392 | SrcTy = SrcType; |
| 1393 | DestTy = DestType->getPointeeType(); |
| 1394 | } |
| 1395 | |
| 1396 | // Cast is compatible if the types are the same. |
| 1397 | if (Context.hasSameUnqualifiedType(DestTy, SrcTy)) { |
| 1398 | return; |
| 1399 | } |
| 1400 | // or one of the types is a char or void type |
| 1401 | if (DestTy->isAnyCharacterType() || DestTy->isVoidType() || |
| 1402 | SrcTy->isAnyCharacterType() || SrcTy->isVoidType()) { |
| 1403 | return; |
| 1404 | } |
| 1405 | // or one of the types is a tag type. |
Chandler Carruth | 0dc3f8d | 2011-05-24 07:43:19 +0000 | [diff] [blame] | 1406 | if (SrcTy->getAs<TagType>() || DestTy->getAs<TagType>()) { |
Argyrios Kyrtzidis | 69a2c92 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1407 | return; |
| 1408 | } |
| 1409 | |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1410 | // FIXME: Scoped enums? |
Argyrios Kyrtzidis | 69a2c92 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1411 | if ((SrcTy->isUnsignedIntegerType() && DestTy->isSignedIntegerType()) || |
| 1412 | (SrcTy->isSignedIntegerType() && DestTy->isUnsignedIntegerType())) { |
| 1413 | if (Context.getTypeSize(DestTy) == Context.getTypeSize(SrcTy)) { |
| 1414 | return; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range; |
| 1419 | } |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 1420 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1421 | static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1422 | QualType DestType, bool CStyle, |
| 1423 | const SourceRange &OpRange, |
Anders Carlsson | 9d1b34b | 2009-09-26 00:12:34 +0000 | [diff] [blame] | 1424 | unsigned &msg, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1425 | CastKind &Kind) { |
Douglas Gregor | 5195427 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1426 | bool IsLValueCast = false; |
| 1427 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1428 | DestType = Self.Context.getCanonicalType(DestType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1429 | QualType SrcType = SrcExpr.get()->getType(); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1430 | |
| 1431 | // Is the source an overloaded name? (i.e. &foo) |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1432 | // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5) ... |
| 1433 | if (SrcType == Self.Context.OverloadTy) { |
| 1434 | // ... unless foo<int> resolves to an lvalue unambiguously |
| 1435 | ExprResult SingleFunctionExpr = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1436 | Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr.get(), |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1437 | Expr::getValueKindForType(DestType) == VK_RValue // Convert Fun to Ptr |
| 1438 | ); |
| 1439 | if (SingleFunctionExpr.isUsable()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1440 | SrcExpr = move(SingleFunctionExpr); |
| 1441 | SrcType = SrcExpr.get()->getType(); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1442 | } |
| 1443 | else |
| 1444 | return TC_NotApplicable; |
| 1445 | } |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1446 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1447 | if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1448 | bool LValue = DestTypeTmp->isLValueReferenceType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1449 | if (LValue && !SrcExpr.get()->isLValue()) { |
Douglas Gregor | c1ed20c | 2011-01-22 00:19:52 +0000 | [diff] [blame] | 1450 | // Cannot cast non-lvalue to lvalue reference type. See the similar |
| 1451 | // comment in const_cast. |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1452 | msg = diag::err_bad_cxx_cast_rvalue; |
| 1453 | return TC_NotApplicable; |
| 1454 | } |
| 1455 | |
Argyrios Kyrtzidis | 69a2c92 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 1456 | if (!CStyle) { |
| 1457 | Self.CheckCompatibleReinterpretCast(SrcType, DestType, |
| 1458 | /*isDereference=*/false, OpRange); |
| 1459 | } |
| 1460 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1461 | // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the |
| 1462 | // same effect as the conversion *reinterpret_cast<T*>(&x) with the |
| 1463 | // built-in & and * operators. |
Argyrios Kyrtzidis | 47a1285 | 2011-04-22 22:31:13 +0000 | [diff] [blame] | 1464 | |
Argyrios Kyrtzidis | bf04231 | 2011-04-22 23:57:57 +0000 | [diff] [blame] | 1465 | const char *inappropriate = 0; |
| 1466 | switch (SrcExpr.get()->getObjectKind()) { |
Argyrios Kyrtzidis | 3d2185b | 2011-04-23 01:10:24 +0000 | [diff] [blame] | 1467 | case OK_Ordinary: |
| 1468 | break; |
Argyrios Kyrtzidis | bf04231 | 2011-04-22 23:57:57 +0000 | [diff] [blame] | 1469 | case OK_BitField: inappropriate = "bit-field"; break; |
| 1470 | case OK_VectorComponent: inappropriate = "vector element"; break; |
| 1471 | case OK_ObjCProperty: inappropriate = "property expression"; break; |
| 1472 | } |
| 1473 | if (inappropriate) { |
| 1474 | Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_reference) |
| 1475 | << inappropriate << DestType |
| 1476 | << OpRange << SrcExpr.get()->getSourceRange(); |
| 1477 | msg = 0; SrcExpr = ExprError(); |
Argyrios Kyrtzidis | 47a1285 | 2011-04-22 22:31:13 +0000 | [diff] [blame] | 1478 | return TC_NotApplicable; |
| 1479 | } |
| 1480 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1481 | // This code does this transformation for the checked types. |
| 1482 | DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); |
| 1483 | SrcType = Self.Context.getPointerType(SrcType); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1484 | |
Douglas Gregor | 5195427 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1485 | IsLValueCast = true; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | // Canonicalize source for comparison. |
| 1489 | SrcType = Self.Context.getCanonicalType(SrcType); |
| 1490 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1491 | const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(), |
| 1492 | *SrcMemPtr = SrcType->getAs<MemberPointerType>(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1493 | if (DestMemPtr && SrcMemPtr) { |
| 1494 | // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1" |
| 1495 | // can be explicitly converted to an rvalue of type "pointer to member |
| 1496 | // of Y of type T2" if T1 and T2 are both function types or both object |
| 1497 | // types. |
| 1498 | if (DestMemPtr->getPointeeType()->isFunctionType() != |
| 1499 | SrcMemPtr->getPointeeType()->isFunctionType()) |
| 1500 | return TC_NotApplicable; |
| 1501 | |
| 1502 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away |
| 1503 | // constness. |
| 1504 | // A reinterpret_cast followed by a const_cast can, though, so in C-style, |
| 1505 | // we accept it. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1506 | if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, |
| 1507 | /*CheckObjCLifetime=*/CStyle)) { |
Douglas Gregor | b472e93 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1508 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1509 | return TC_Failed; |
| 1510 | } |
| 1511 | |
Charles Davis | ebab1ed | 2010-08-16 05:30:44 +0000 | [diff] [blame] | 1512 | // Don't allow casting between member pointers of different sizes. |
| 1513 | if (Self.Context.getTypeSize(DestMemPtr) != |
| 1514 | Self.Context.getTypeSize(SrcMemPtr)) { |
| 1515 | msg = diag::err_bad_cxx_cast_member_pointer_size; |
| 1516 | return TC_Failed; |
| 1517 | } |
| 1518 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1519 | // A valid member pointer cast. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1520 | Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1521 | return TC_Success; |
| 1522 | } |
| 1523 | |
| 1524 | // See below for the enumeral issue. |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1525 | if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1526 | // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral |
| 1527 | // type large enough to hold it. A value of std::nullptr_t can be |
| 1528 | // converted to an integral type; the conversion has the same meaning |
| 1529 | // and validity as a conversion of (void*)0 to the integral type. |
| 1530 | if (Self.Context.getTypeSize(SrcType) > |
| 1531 | Self.Context.getTypeSize(DestType)) { |
| 1532 | msg = diag::err_bad_reinterpret_cast_small_int; |
| 1533 | return TC_Failed; |
| 1534 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1535 | Kind = CK_PointerToIntegral; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1536 | return TC_Success; |
| 1537 | } |
| 1538 | |
Anders Carlsson | 570af5d | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1539 | bool destIsVector = DestType->isVectorType(); |
| 1540 | bool srcIsVector = SrcType->isVectorType(); |
| 1541 | if (srcIsVector || destIsVector) { |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1542 | // FIXME: Should this also apply to floating point types? |
| 1543 | bool srcIsScalar = SrcType->isIntegralType(Self.Context); |
| 1544 | bool destIsScalar = DestType->isIntegralType(Self.Context); |
Anders Carlsson | 570af5d | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1545 | |
| 1546 | // Check if this is a cast between a vector and something else. |
| 1547 | if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) && |
| 1548 | !(srcIsVector && destIsVector)) |
| 1549 | return TC_NotApplicable; |
| 1550 | |
| 1551 | // If both types have the same size, we can successfully cast. |
Douglas Gregor | 19fc0b7 | 2009-12-22 22:47:22 +0000 | [diff] [blame] | 1552 | if (Self.Context.getTypeSize(SrcType) |
| 1553 | == Self.Context.getTypeSize(DestType)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1554 | Kind = CK_BitCast; |
Anders Carlsson | 570af5d | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1555 | return TC_Success; |
Douglas Gregor | 19fc0b7 | 2009-12-22 22:47:22 +0000 | [diff] [blame] | 1556 | } |
Anders Carlsson | 570af5d | 2009-09-16 19:19:43 +0000 | [diff] [blame] | 1557 | |
| 1558 | if (destIsScalar) |
| 1559 | msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size; |
| 1560 | else if (srcIsScalar) |
| 1561 | msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size; |
| 1562 | else |
| 1563 | msg = diag::err_bad_cxx_cast_vector_to_vector_different_size; |
| 1564 | |
| 1565 | return TC_Failed; |
| 1566 | } |
| 1567 | |
Douglas Gregor | eaff2cb | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1568 | bool destIsPtr = DestType->isAnyPointerType() || |
| 1569 | DestType->isBlockPointerType(); |
| 1570 | bool srcIsPtr = SrcType->isAnyPointerType() || |
| 1571 | SrcType->isBlockPointerType(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1572 | if (!destIsPtr && !srcIsPtr) { |
| 1573 | // Except for std::nullptr_t->integer and lvalue->reference, which are |
| 1574 | // handled above, at least one of the two arguments must be a pointer. |
| 1575 | return TC_NotApplicable; |
| 1576 | } |
| 1577 | |
| 1578 | if (SrcType == DestType) { |
| 1579 | // C++ 5.2.10p2 has a note that mentions that, subject to all other |
| 1580 | // restrictions, a cast to the same type is allowed. The intent is not |
| 1581 | // entirely clear here, since all other paragraphs explicitly forbid casts |
| 1582 | // to the same type. However, the behavior of compilers is pretty consistent |
| 1583 | // on this point: allow same-type conversion if the involved types are |
| 1584 | // pointers, disallow otherwise. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1585 | Kind = CK_NoOp; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1586 | return TC_Success; |
| 1587 | } |
| 1588 | |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1589 | if (DestType->isIntegralType(Self.Context)) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1590 | assert(srcIsPtr && "One type must be a pointer"); |
| 1591 | // C++ 5.2.10p4: A pointer can be explicitly converted to any integral |
Francois Pichet | b796b63 | 2011-05-11 22:13:54 +0000 | [diff] [blame] | 1592 | // type large enough to hold it; except in Microsoft mode, where the |
| 1593 | // integral type size doesn't matter. |
| 1594 | if ((Self.Context.getTypeSize(SrcType) > |
| 1595 | Self.Context.getTypeSize(DestType)) && |
| 1596 | !Self.getLangOptions().Microsoft) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1597 | msg = diag::err_bad_reinterpret_cast_small_int; |
| 1598 | return TC_Failed; |
| 1599 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1600 | Kind = CK_PointerToIntegral; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1601 | return TC_Success; |
| 1602 | } |
| 1603 | |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1604 | if (SrcType->isIntegralOrEnumerationType()) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1605 | assert(destIsPtr && "One type must be a pointer"); |
| 1606 | // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly |
| 1607 | // converted to a pointer. |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1608 | // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not |
| 1609 | // necessarily converted to a null pointer value.] |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1610 | Kind = CK_IntegralToPointer; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1611 | return TC_Success; |
| 1612 | } |
| 1613 | |
| 1614 | if (!destIsPtr || !srcIsPtr) { |
| 1615 | // With the valid non-pointer conversions out of the way, we can be even |
| 1616 | // more stringent. |
| 1617 | return TC_NotApplicable; |
| 1618 | } |
| 1619 | |
| 1620 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness. |
| 1621 | // The C-style cast operator can. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1622 | if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle, |
| 1623 | /*CheckObjCLifetime=*/CStyle)) { |
Douglas Gregor | b472e93 | 2011-04-15 17:59:54 +0000 | [diff] [blame] | 1624 | msg = diag::err_bad_cxx_cast_qualifiers_away; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1625 | return TC_Failed; |
| 1626 | } |
Douglas Gregor | eaff2cb | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1627 | |
| 1628 | // Cannot convert between block pointers and Objective-C object pointers. |
| 1629 | if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) || |
| 1630 | (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType())) |
| 1631 | return TC_NotApplicable; |
| 1632 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1633 | if (IsLValueCast) { |
| 1634 | Kind = CK_LValueBitCast; |
| 1635 | } else if (DestType->isObjCObjectPointerType()) { |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame^] | 1636 | Kind = Self.PrepareCastToObjCObjectPointer(SrcExpr); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1637 | } else if (DestType->isBlockPointerType()) { |
| 1638 | if (!SrcType->isBlockPointerType()) { |
| 1639 | Kind = CK_AnyPointerToBlockPointerCast; |
| 1640 | } else { |
| 1641 | Kind = CK_BitCast; |
| 1642 | } |
| 1643 | } else { |
| 1644 | Kind = CK_BitCast; |
| 1645 | } |
| 1646 | |
Douglas Gregor | eaff2cb | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1647 | // Any pointer can be cast to an Objective-C pointer type with a C-style |
| 1648 | // cast. |
Fariborz Jahanian | 859c415 | 2009-12-08 23:09:15 +0000 | [diff] [blame] | 1649 | if (CStyle && DestType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 859c415 | 2009-12-08 23:09:15 +0000 | [diff] [blame] | 1650 | return TC_Success; |
| 1651 | } |
Douglas Gregor | eaff2cb | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1652 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1653 | // Not casting away constness, so the only remaining check is for compatible |
| 1654 | // pointer categories. |
| 1655 | |
| 1656 | if (SrcType->isFunctionPointerType()) { |
| 1657 | if (DestType->isFunctionPointerType()) { |
| 1658 | // C++ 5.2.10p6: A pointer to a function can be explicitly converted to |
| 1659 | // a pointer to a function of a different type. |
| 1660 | return TC_Success; |
| 1661 | } |
| 1662 | |
| 1663 | // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to |
| 1664 | // an object type or vice versa is conditionally-supported. |
| 1665 | // Compilers support it in C++03 too, though, because it's necessary for |
| 1666 | // casting the return value of dlsym() and GetProcAddress(). |
| 1667 | // FIXME: Conditionally-supported behavior should be configurable in the |
| 1668 | // TargetInfo or similar. |
| 1669 | if (!Self.getLangOptions().CPlusPlus0x) |
| 1670 | Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange; |
| 1671 | return TC_Success; |
| 1672 | } |
| 1673 | |
| 1674 | if (DestType->isFunctionPointerType()) { |
| 1675 | // See above. |
| 1676 | if (!Self.getLangOptions().CPlusPlus0x) |
| 1677 | Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange; |
| 1678 | return TC_Success; |
| 1679 | } |
Douglas Gregor | eaff2cb | 2010-07-08 20:27:32 +0000 | [diff] [blame] | 1680 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1681 | // C++ 5.2.10p7: A pointer to an object can be explicitly converted to |
| 1682 | // a pointer to an object of different type. |
| 1683 | // Void pointers are not specified, but supported by every compiler out there. |
| 1684 | // So we finish by allowing everything that remains - it's got to be two |
| 1685 | // object pointers. |
| 1686 | return TC_Success; |
John McCall | 909acf8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 1687 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1688 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1689 | ExprResult |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1690 | Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, ExprValueKind &VK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1691 | Expr *CastExpr, CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1692 | CXXCastPath &BasePath, |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1693 | bool FunctionalStyle) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1694 | // This test is outside everything else because it's the only case where |
| 1695 | // a non-lvalue-reference target type does not lead to decay. |
| 1696 | // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". |
Anders Carlsson | 9500ad1 | 2009-10-18 20:31:03 +0000 | [diff] [blame] | 1697 | if (CastTy->isVoidType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1698 | Kind = CK_ToVoid; |
| 1699 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1700 | ExprResult CastExprRes = IgnoredValueConversions(CastExpr); |
| 1701 | if (CastExprRes.isInvalid()) |
| 1702 | return ExprError(); |
| 1703 | CastExpr = CastExprRes.take(); |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1704 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1705 | if (CastExpr->getType() == Context.BoundMemberTy) |
| 1706 | return CheckPlaceholderExpr(CastExpr); // will always fail |
| 1707 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1708 | if (CastExpr->getType() == Context.OverloadTy) { |
| 1709 | ExprResult SingleFunctionExpr = |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 1710 | ResolveAndFixSingleFunctionTemplateSpecialization( |
| 1711 | CastExpr, /* Decay Function to ptr */ false, |
| 1712 | /* Complain */ true, R, CastTy, |
| 1713 | diag::err_bad_cstyle_cast_overload); |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1714 | if (SingleFunctionExpr.isInvalid()) |
| 1715 | return ExprError(); |
| 1716 | CastExpr = SingleFunctionExpr.take(); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1717 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1718 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1719 | assert(!CastExpr->getType()->isPlaceholderType()); |
| 1720 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1721 | return Owned(CastExpr); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
John McCall | 4cec5f8 | 2010-11-30 02:05:44 +0000 | [diff] [blame] | 1724 | // Make sure we determine the value kind before we bail out for |
| 1725 | // dependent types. |
| 1726 | VK = Expr::getValueKindForType(CastTy); |
| 1727 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1728 | // If the type is dependent, we won't do any other semantic analysis now. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1729 | if (CastTy->isDependentType() || CastExpr->isTypeDependent()) { |
| 1730 | Kind = CK_Dependent; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1731 | return Owned(CastExpr); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1732 | } |
Benjamin Kramer | 65cc107 | 2011-07-08 20:20:17 +0000 | [diff] [blame] | 1733 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1734 | if (VK == VK_RValue && !CastTy->isRecordType()) { |
| 1735 | ExprResult CastExprRes = DefaultFunctionArrayLvalueConversion(CastExpr); |
| 1736 | if (CastExprRes.isInvalid()) |
| 1737 | return ExprError(); |
| 1738 | CastExpr = CastExprRes.take(); |
| 1739 | } |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1740 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 1741 | // AltiVec vector initialization with a single literal. |
| 1742 | if (const VectorType *vecTy = CastTy->getAs<VectorType>()) |
| 1743 | if (vecTy->getVectorKind() == VectorType::AltiVecVector |
| 1744 | && (CastExpr->getType()->isIntegerType() |
| 1745 | || CastExpr->getType()->isFloatingType())) { |
| 1746 | Kind = CK_VectorSplat; |
| 1747 | return Owned(CastExpr); |
| 1748 | } |
| 1749 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1750 | // C++ [expr.cast]p5: The conversions performed by |
| 1751 | // - a const_cast, |
| 1752 | // - a static_cast, |
| 1753 | // - a static_cast followed by a const_cast, |
| 1754 | // - a reinterpret_cast, or |
| 1755 | // - a reinterpret_cast followed by a const_cast, |
| 1756 | // can be performed using the cast notation of explicit type conversion. |
| 1757 | // [...] If a conversion can be interpreted in more than one of the ways |
| 1758 | // listed above, the interpretation that appears first in the list is used, |
| 1759 | // even if a cast resulting from that interpretation is ill-formed. |
| 1760 | // In plain language, this means trying a const_cast ... |
| 1761 | unsigned msg = diag::err_bad_cxx_cast_generic; |
Anders Carlsson | f1ae6d4 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 1762 | TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true, |
| 1763 | msg); |
Anders Carlsson | 027732b | 2009-10-19 18:14:28 +0000 | [diff] [blame] | 1764 | if (tcr == TC_Success) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1765 | Kind = CK_NoOp; |
Anders Carlsson | 027732b | 2009-10-19 18:14:28 +0000 | [diff] [blame] | 1766 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1767 | Sema::CheckedConversionKind CCK |
| 1768 | = FunctionalStyle? Sema::CCK_FunctionalCast |
| 1769 | : Sema::CCK_CStyleCast; |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1770 | if (tcr == TC_NotApplicable) { |
| 1771 | // ... or if that is not possible, a static_cast, ignoring const, ... |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1772 | ExprResult CastExprRes = Owned(CastExpr); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1773 | tcr = TryStaticCast(*this, CastExprRes, CastTy, CCK, R, msg, Kind, |
| 1774 | BasePath); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1775 | if (CastExprRes.isInvalid()) |
| 1776 | return ExprError(); |
| 1777 | CastExpr = CastExprRes.take(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1778 | if (tcr == TC_NotApplicable) { |
| 1779 | // ... and finally a reinterpret_cast, ignoring const. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1780 | CastExprRes = Owned(CastExpr); |
Richard Trieu | 70d14f5 | 2011-04-16 01:09:30 +0000 | [diff] [blame] | 1781 | tcr = TryReinterpretCast(*this, CastExprRes, CastTy, /*CStyle*/true, R, |
| 1782 | msg, Kind); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1783 | if (CastExprRes.isInvalid()) |
| 1784 | return ExprError(); |
| 1785 | CastExpr = CastExprRes.take(); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | |
Fariborz Jahanian | f291340 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 1789 | if (getLangOptions().ObjCAutoRefCount && tcr == TC_Success) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1790 | CheckObjCARCConversion(R, CastTy, CastExpr, CCK); |
| 1791 | |
Nick Lewycky | 14d88eb | 2010-11-09 00:19:31 +0000 | [diff] [blame] | 1792 | if (tcr != TC_Success && msg != 0) { |
| 1793 | if (CastExpr->getType() == Context.OverloadTy) { |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1794 | DeclAccessPair Found; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1795 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(CastExpr, |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1796 | CastTy, |
| 1797 | /* Complain */ true, |
| 1798 | Found); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1799 | |
Richard Trieu | 70d14f5 | 2011-04-16 01:09:30 +0000 | [diff] [blame] | 1800 | assert(!Fn && "cast failed but able to resolve overload expression!!"); |
Nick Lewycky | 14d88eb | 2010-11-09 00:19:31 +0000 | [diff] [blame] | 1801 | (void)Fn; |
John McCall | 909acf8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 1802 | |
Nick Lewycky | 14d88eb | 2010-11-09 00:19:31 +0000 | [diff] [blame] | 1803 | } else { |
John McCall | 909acf8 | 2011-02-14 18:34:10 +0000 | [diff] [blame] | 1804 | diagnoseBadCast(*this, msg, (FunctionalStyle ? CT_Functional : CT_CStyle), |
| 1805 | R, CastExpr, CastTy); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 1806 | } |
| 1807 | } |
John McCall | d50a271 | 2010-11-16 05:46:29 +0000 | [diff] [blame] | 1808 | else if (Kind == CK_BitCast) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 1809 | CheckCastAlign(CastExpr, CastTy, R); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1810 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1811 | if (tcr != TC_Success) |
| 1812 | return ExprError(); |
| 1813 | |
| 1814 | return Owned(CastExpr); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1815 | } |