blob: 2c6b8e748bc57188a12939be8fb53b8ad1d1bedb [file] [log] [blame]
Sebastian Redl26d85b12008-11-05 21:50:06 +00001//===--- 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 McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Initialization.h"
Sebastian Redl26d85b12008-11-05 21:50:06 +000016#include "clang/AST/ExprCXX.h"
17#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssonb7906612009-08-26 23:45:07 +000019#include "clang/Basic/PartialDiagnostic.h"
Sebastian Redl26d85b12008-11-05 21:50:06 +000020#include "llvm/ADT/SmallVector.h"
Sebastian Redle3dc28a2008-11-07 23:29:29 +000021#include <set>
Sebastian Redl26d85b12008-11-05 21:50:06 +000022using namespace clang;
23
Douglas Gregor8e960432010-11-08 03:40:48 +000024
Douglas Gregor8e960432010-11-08 03:40:48 +000025
Sebastian Redl9cc11e72009-07-25 15:41:38 +000026enum 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
33enum 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 Redl37d6de32008-11-08 13:00:26 +000040};
41
Douglas Gregor8e960432010-11-08 03:40:48 +000042
43
44
John Wiegley429bb272011-04-08 18:41:53 +000045static void CheckConstCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +000046 ExprValueKind &VK,
Sebastian Redl37d6de32008-11-08 13:00:26 +000047 const SourceRange &OpRange,
48 const SourceRange &DestRange);
John Wiegley429bb272011-04-08 18:41:53 +000049static void CheckReinterpretCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +000050 ExprValueKind &VK,
Sebastian Redl37d6de32008-11-08 13:00:26 +000051 const SourceRange &OpRange,
Anders Carlsson7f9e6462009-09-15 04:48:33 +000052 const SourceRange &DestRange,
John McCall2de56d12010-08-25 11:45:40 +000053 CastKind &Kind);
John Wiegley429bb272011-04-08 18:41:53 +000054static void CheckStaticCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +000055 ExprValueKind &VK,
Anders Carlssoncdb61972009-08-07 22:21:05 +000056 const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +000057 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +000058 CXXCastPath &BasePath);
John Wiegley429bb272011-04-08 18:41:53 +000059static void CheckDynamicCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +000060 ExprValueKind &VK,
Sebastian Redl37d6de32008-11-08 13:00:26 +000061 const SourceRange &OpRange,
Mike Stump1eb44332009-09-09 15:08:12 +000062 const SourceRange &DestRange,
John McCall2de56d12010-08-25 11:45:40 +000063 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +000064 CXXCastPath &BasePath);
Sebastian Redl37d6de32008-11-08 13:00:26 +000065
John McCallf85e1932011-06-15 23:02:42 +000066static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType,
67 bool CheckCVR, bool CheckObjCLifetime);
Sebastian Redl9cc11e72009-07-25 15:41:38 +000068
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
80static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr,
Douglas Gregor8ec14e62011-01-26 21:04:06 +000081 QualType DestType, bool CStyle,
82 CastKind &Kind,
Douglas Gregor88b22a42011-01-25 16:13:26 +000083 CXXCastPath &BasePath,
84 unsigned &msg);
Sebastian Redl9cc11e72009-07-25 15:41:38 +000085static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr,
Anders Carlssonf9d68e12010-04-24 19:36:51 +000086 QualType DestType, bool CStyle,
87 const SourceRange &OpRange,
88 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +000089 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +000090 CXXCastPath &BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +000091static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType,
92 QualType DestType, bool CStyle,
93 const SourceRange &OpRange,
Anders Carlsson95c5d8a2009-11-12 16:53:16 +000094 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +000095 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +000096 CXXCastPath &BasePath);
Douglas Gregorab15d0e2009-11-15 09:20:52 +000097static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType,
98 CanQualType DestType, bool CStyle,
Sebastian Redl9cc11e72009-07-25 15:41:38 +000099 const SourceRange &OpRange,
100 QualType OrigSrcType,
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000101 QualType OrigDestType, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000102 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000103 CXXCastPath &BasePath);
John Wiegley429bb272011-04-08 18:41:53 +0000104static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr,
Anders Carlssoncee22422010-04-24 19:22:20 +0000105 QualType SrcType,
106 QualType DestType,bool CStyle,
107 const SourceRange &OpRange,
108 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000109 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000110 CXXCastPath &BasePath);
Anders Carlssoncee22422010-04-24 19:22:20 +0000111
John Wiegley429bb272011-04-08 18:41:53 +0000112static TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr,
John McCallf85e1932011-06-15 23:02:42 +0000113 QualType DestType,
114 Sema::CheckedConversionKind CCK,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000115 const SourceRange &OpRange,
Fariborz Jahaniane9f42082009-08-26 18:55:36 +0000116 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000117 CastKind &Kind);
John Wiegley429bb272011-04-08 18:41:53 +0000118static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr,
John McCallf85e1932011-06-15 23:02:42 +0000119 QualType DestType,
120 Sema::CheckedConversionKind CCK,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000121 const SourceRange &OpRange,
Anders Carlssoncb3c3082009-09-01 20:52:42 +0000122 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000123 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000124 CXXCastPath &BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000125static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
126 bool CStyle, unsigned &msg);
John Wiegley429bb272011-04-08 18:41:53 +0000127static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000128 QualType DestType, bool CStyle,
129 const SourceRange &OpRange,
Anders Carlsson3c31a392009-09-26 00:12:34 +0000130 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000131 CastKind &Kind);
Sebastian Redl37d6de32008-11-08 13:00:26 +0000132
Douglas Gregor1be8eec2011-02-19 21:32:49 +0000133
Sebastian Redl26d85b12008-11-05 21:50:06 +0000134/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
John McCall60d7b3a2010-08-24 06:29:42 +0000135ExprResult
Sebastian Redl26d85b12008-11-05 21:50:06 +0000136Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000137 SourceLocation LAngleBracketLoc, Declarator &D,
Sebastian Redl26d85b12008-11-05 21:50:06 +0000138 SourceLocation RAngleBracketLoc,
John McCallf312b1e2010-08-26 23:41:50 +0000139 SourceLocation LParenLoc, Expr *E,
Sebastian Redl26d85b12008-11-05 21:50:06 +0000140 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +0000141
Argyrios Kyrtzidis31862ba2011-07-01 22:22:50 +0000142 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 McCallc89724c2010-01-15 19:13:16 +0000154 SourceRange(LAngleBracketLoc, RAngleBracketLoc),
155 SourceRange(LParenLoc, RParenLoc));
156}
157
John McCall60d7b3a2010-08-24 06:29:42 +0000158ExprResult
John McCallc89724c2010-01-15 19:13:16 +0000159Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
John Wiegley429bb272011-04-08 18:41:53 +0000160 TypeSourceInfo *DestTInfo, Expr *E,
John McCallc89724c2010-01-15 19:13:16 +0000161 SourceRange AngleBrackets, SourceRange Parens) {
John Wiegley429bb272011-04-08 18:41:53 +0000162 ExprResult Ex = Owned(E);
John McCallc89724c2010-01-15 19:13:16 +0000163 QualType DestType = DestTInfo->getType();
164
165 SourceRange OpRange(OpLoc, Parens.getEnd());
166 SourceRange DestRange = AngleBrackets;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000167
Douglas Gregor9103bb22008-12-17 22:52:20 +0000168 // 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 Wiegley429bb272011-04-08 18:41:53 +0000170 bool TypeDependent = DestType->isDependentType() || Ex.get()->isTypeDependent();
Douglas Gregor9103bb22008-12-17 22:52:20 +0000171
John McCallf89e55a2010-11-18 06:31:45 +0000172 ExprValueKind VK = VK_RValue;
John McCalla21e06c2010-11-26 10:57:22 +0000173 if (TypeDependent)
174 VK = Expr::getValueKindForType(DestType);
175
Sebastian Redl26d85b12008-11-05 21:50:06 +0000176 switch (Kind) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000177 default: llvm_unreachable("Unknown C++ cast!");
Sebastian Redl26d85b12008-11-05 21:50:06 +0000178
179 case tok::kw_const_cast:
John Wiegley429bb272011-04-08 18:41:53 +0000180 if (!TypeDependent) {
John McCallf89e55a2010-11-18 06:31:45 +0000181 CheckConstCast(*this, Ex, DestType, VK, OpRange, DestRange);
John Wiegley429bb272011-04-08 18:41:53 +0000182 if (Ex.isInvalid())
183 return ExprError();
184 }
John McCallf871d0c2010-08-07 06:22:56 +0000185 return Owned(CXXConstCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000186 DestType.getNonLValueExprType(Context),
John Wiegley429bb272011-04-08 18:41:53 +0000187 VK, Ex.take(), DestTInfo, OpLoc,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000188 Parens.getEnd()));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000189
Anders Carlsson714179b2009-08-02 19:07:59 +0000190 case tok::kw_dynamic_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000191 CastKind Kind = CK_Dependent;
John McCallf871d0c2010-08-07 06:22:56 +0000192 CXXCastPath BasePath;
John Wiegley429bb272011-04-08 18:41:53 +0000193 if (!TypeDependent) {
John McCallf89e55a2010-11-18 06:31:45 +0000194 CheckDynamicCast(*this, Ex, DestType, VK, OpRange, DestRange,
195 Kind, BasePath);
John Wiegley429bb272011-04-08 18:41:53 +0000196 if (Ex.isInvalid())
197 return ExprError();
198 }
John McCallf871d0c2010-08-07 06:22:56 +0000199 return Owned(CXXDynamicCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000200 DestType.getNonLValueExprType(Context),
John Wiegley429bb272011-04-08 18:41:53 +0000201 VK, Kind, Ex.take(), &BasePath, DestTInfo,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000202 OpLoc, Parens.getEnd()));
Anders Carlsson714179b2009-08-02 19:07:59 +0000203 }
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000204 case tok::kw_reinterpret_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000205 CastKind Kind = CK_Dependent;
John Wiegley429bb272011-04-08 18:41:53 +0000206 if (!TypeDependent) {
John McCallf89e55a2010-11-18 06:31:45 +0000207 CheckReinterpretCast(*this, Ex, DestType, VK, OpRange, DestRange, Kind);
John Wiegley429bb272011-04-08 18:41:53 +0000208 if (Ex.isInvalid())
209 return ExprError();
210 }
John McCallf871d0c2010-08-07 06:22:56 +0000211 return Owned(CXXReinterpretCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000212 DestType.getNonLValueExprType(Context),
John Wiegley429bb272011-04-08 18:41:53 +0000213 VK, Kind, Ex.take(), 0,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000214 DestTInfo, OpLoc, Parens.getEnd()));
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000215 }
Anders Carlssoncdb61972009-08-07 22:21:05 +0000216 case tok::kw_static_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000217 CastKind Kind = CK_Dependent;
John McCallf871d0c2010-08-07 06:22:56 +0000218 CXXCastPath BasePath;
John Wiegley429bb272011-04-08 18:41:53 +0000219 if (!TypeDependent) {
John McCallf89e55a2010-11-18 06:31:45 +0000220 CheckStaticCast(*this, Ex, DestType, VK, OpRange, Kind, BasePath);
John Wiegley429bb272011-04-08 18:41:53 +0000221 if (Ex.isInvalid())
222 return ExprError();
223 }
Anders Carlsson0aebc812009-09-09 21:33:21 +0000224
John McCallf871d0c2010-08-07 06:22:56 +0000225 return Owned(CXXStaticCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000226 DestType.getNonLValueExprType(Context),
John Wiegley429bb272011-04-08 18:41:53 +0000227 VK, Kind, Ex.take(), &BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000228 DestTInfo, OpLoc, Parens.getEnd()));
Anders Carlssoncdb61972009-08-07 22:21:05 +0000229 }
Sebastian Redl26d85b12008-11-05 21:50:06 +0000230 }
231
Sebastian Redlf53597f2009-03-15 17:47:39 +0000232 return ExprError();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000233}
234
John McCall79ab2c82011-02-14 18:34:10 +0000235/// Try to diagnose a failed overloaded cast. Returns true if
236/// diagnostics were emitted.
237static 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 McCallf85e1932011-06-15 23:02:42 +0000260 = (CT == CT_CStyle)? InitializationKind::CreateCStyleCast(range.getBegin(),
261 range)
262 : (CT == CT_Functional)? InitializationKind::CreateFunctionalCast(range)
263 : InitializationKind::CreateCast(/*type range?*/ range);
John McCall79ab2c82011-02-14 18:34:10 +0000264 InitializationSequence sequence(S, entity, initKind, &src, 1);
265
Sebastian Redl383616c2011-06-05 12:23:28 +0000266 assert(sequence.Failed() && "initialization succeeded on second try?");
John McCall79ab2c82011-02-14 18:34:10 +0000267 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.
312static void diagnoseBadCast(Sema &S, unsigned msg, CastType castType,
313 SourceRange opRange, Expr *src, QualType destType) {
John McCall864c0412011-04-26 20:42:42 +0000314 if (src->getType() == S.Context.BoundMemberTy) {
315 (void) S.CheckPlaceholderExpr(src); // will always fail
316 return;
317 }
318
John McCall79ab2c82011-02-14 18:34:10 +0000319 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 Redl76d69bb2009-11-18 18:10:53 +0000327/// 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 Gohman3c46e8d2010-07-26 21:25:24 +0000332static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) {
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000333 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 Jahanian72a86592010-02-03 20:32:31 +0000340 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 Redl76d69bb2009-11-18 18:10:53 +0000364 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 Gregorbf9fb882010-07-08 20:27:32 +0000371
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 Redl76d69bb2009-11-18 18:10:53 +0000380 return false;
381}
382
Sebastian Redldb647282009-01-27 23:18:31 +0000383/// 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 McCallf85e1932011-06-15 23:02:42 +0000387///
388/// \param CheckCVR Whether to check for const/volatile/restrict qualifiers.
389///
390/// \param CheckObjCLifetime Whether to check Objective-C lifetime qualifiers.
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000391static bool
John McCallf85e1932011-06-15 23:02:42 +0000392CastsAwayConstness(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 Redldb647282009-01-27 23:18:31 +0000400 // 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 Gregorbf9fb882010-07-08 20:27:32 +0000404 assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() ||
405 SrcType->isBlockPointerType()) &&
Sebastian Redldb647282009-01-27 23:18:31 +0000406 "Source type is not pointer or pointer to member.");
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000407 assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() ||
408 DestType->isBlockPointerType()) &&
Sebastian Redldb647282009-01-27 23:18:31 +0000409 "Destination type is not pointer or pointer to member.");
Sebastian Redl26d85b12008-11-05 21:50:06 +0000410
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000411 QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType),
412 UnwrappedDestType = Self.Context.getCanonicalType(DestType);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000413 SmallVector<Qualifiers, 8> cv1, cv2;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000414
Douglas Gregord4c5f842011-04-15 17:59:54 +0000415 // 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 Redl76d69bb2009-11-18 18:10:53 +0000418 while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
John McCallf85e1932011-06-15 23:02:42 +0000419 // Determine the relevant qualifiers at this level.
420 Qualifiers SrcQuals, DestQuals;
Anders Carlsson52647c62010-06-04 22:47:55 +0000421 Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals);
Anders Carlsson52647c62010-06-04 22:47:55 +0000422 Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals);
John McCallf85e1932011-06-15 23:02:42 +0000423
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 Redl26d85b12008-11-05 21:50:06 +0000436 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000437 if (cv1.empty())
438 return false;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000439
440 // Construct void pointers with those qualifiers (in reverse order of
441 // unwrapping, of course).
Sebastian Redl37d6de32008-11-08 13:00:26 +0000442 QualType SrcConstruct = Self.Context.VoidTy;
443 QualType DestConstruct = Self.Context.VoidTy;
John McCall0953e762009-09-24 19:53:00 +0000444 ASTContext &Context = Self.Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000445 for (SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(),
John McCall0953e762009-09-24 19:53:00 +0000446 i2 = cv2.rbegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000447 i1 != cv1.rend(); ++i1, ++i2) {
John McCall0953e762009-09-24 19:53:00 +0000448 SrcConstruct
449 = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1));
450 DestConstruct
451 = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000452 }
453
454 // Test if they're compatible.
John McCallf85e1932011-06-15 23:02:42 +0000455 bool ObjCLifetimeConversion;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000456 return SrcConstruct != DestConstruct &&
John McCallf85e1932011-06-15 23:02:42 +0000457 !Self.IsQualificationConversion(SrcConstruct, DestConstruct, false,
458 ObjCLifetimeConversion);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000459}
460
Sebastian Redl26d85b12008-11-05 21:50:06 +0000461/// 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 Carlsson714179b2009-08-02 19:07:59 +0000464static void
John Wiegley429bb272011-04-08 18:41:53 +0000465CheckDynamicCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000466 ExprValueKind &VK, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000467 const SourceRange &DestRange, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000468 CXXCastPath &BasePath) {
John Wiegley429bb272011-04-08 18:41:53 +0000469 QualType OrigDestType = DestType, OrigSrcType = SrcExpr.get()->getType();
Sebastian Redl37d6de32008-11-08 13:00:26 +0000470 DestType = Self.Context.getCanonicalType(DestType);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000471
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 Kremenek6217b802009-07-29 21:53:49 +0000476 const PointerType *DestPointer = DestType->getAs<PointerType>();
John McCallf89e55a2010-11-18 06:31:45 +0000477 const ReferenceType *DestReference = 0;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000478 if (DestPointer) {
479 DestPointee = DestPointer->getPointeeType();
John McCallf89e55a2010-11-18 06:31:45 +0000480 } else if ((DestReference = DestType->getAs<ReferenceType>())) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000481 DestPointee = DestReference->getPointeeType();
Douglas Gregordc843f22011-01-22 00:06:57 +0000482 VK = isa<LValueReferenceType>(DestReference) ? VK_LValue
483 : isa<RValueReferenceType>(DestReference) ? VK_XValue
484 : VK_RValue;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000485 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000486 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
Chris Lattnerd1625842008-11-24 06:25:27 +0000487 << OrigDestType << DestRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000488 return;
489 }
490
Ted Kremenek6217b802009-07-29 21:53:49 +0000491 const RecordType *DestRecord = DestPointee->getAs<RecordType>();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000492 if (DestPointee->isVoidType()) {
493 assert(DestPointer && "Reference to void is not possible");
494 } else if (DestRecord) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000495 if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000496 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssonb7906612009-08-26 23:45:07 +0000497 << DestRange))
Sebastian Redl26d85b12008-11-05 21:50:06 +0000498 return;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000499 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000500 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattnerd1625842008-11-24 06:25:27 +0000501 << DestPointee.getUnqualifiedType() << DestRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000502 return;
503 }
504
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000505 // 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 Gregordc843f22011-01-22 00:06:57 +0000507 // 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 Redl37d6de32008-11-08 13:00:26 +0000509 QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000510 QualType SrcPointee;
511 if (DestPointer) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000512 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000513 SrcPointee = SrcPointer->getPointeeType();
514 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000515 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
John Wiegley429bb272011-04-08 18:41:53 +0000516 << OrigSrcType << SrcExpr.get()->getSourceRange();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000517 return;
518 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000519 } else if (DestReference->isLValueReferenceType()) {
John Wiegley429bb272011-04-08 18:41:53 +0000520 if (!SrcExpr.get()->isLValue()) {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000521 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000522 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000523 }
524 SrcPointee = SrcType;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000525 } else {
526 SrcPointee = SrcType;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000527 }
528
Ted Kremenek6217b802009-07-29 21:53:49 +0000529 const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000530 if (SrcRecord) {
Douglas Gregor86447ec2009-03-09 16:13:40 +0000531 if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000532 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
John Wiegley429bb272011-04-08 18:41:53 +0000533 << SrcExpr.get()->getSourceRange()))
Sebastian Redl26d85b12008-11-05 21:50:06 +0000534 return;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000535 } else {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000536 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
John Wiegley429bb272011-04-08 18:41:53 +0000537 << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000538 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 Gregord4c5f842011-04-15 17:59:54 +0000549 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000550 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000551 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 McCall2de56d12010-08-25 11:45:40 +0000557 Kind = CK_NoOp;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000558 return;
559 }
560
561 // C++ 5.2.7p5
562 // Upcasts are resolved statically.
Sebastian Redl37d6de32008-11-08 13:00:26 +0000563 if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000564 if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
565 OpRange.getBegin(), OpRange,
566 &BasePath))
567 return;
568
John McCall2de56d12010-08-25 11:45:40 +0000569 Kind = CK_DerivedToBase;
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000570
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 Redl26d85b12008-11-05 21:50:06 +0000576 return;
577 }
578
579 // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
Douglas Gregor952b0172010-02-11 01:04:33 +0000580 const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition();
Sebastian Redld93f0dd2008-11-06 15:59:35 +0000581 assert(SrcDecl && "Definition missing");
582 if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000583 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
John Wiegley429bb272011-04-08 18:41:53 +0000584 << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange();
Sebastian Redld93f0dd2008-11-06 15:59:35 +0000585 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000586 Self.MarkVTableUsed(OpRange.getBegin(),
587 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000588
589 // Done. Everything else is run-time checks.
John McCall2de56d12010-08-25 11:45:40 +0000590 Kind = CK_Dynamic;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000591}
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000592
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));
598void
John Wiegley429bb272011-04-08 18:41:53 +0000599CheckConstCast(Sema &Self, ExprResult &SrcExpr, QualType DestType, ExprValueKind &VK,
Mike Stump1eb44332009-09-09 15:08:12 +0000600 const SourceRange &OpRange, const SourceRange &DestRange) {
John McCallf89e55a2010-11-18 06:31:45 +0000601 VK = Expr::getValueKindForType(DestType);
John Wiegley429bb272011-04-08 18:41:53 +0000602 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 Redl9cc11e72009-07-25 15:41:38 +0000607
608 unsigned msg = diag::err_bad_cxx_cast_generic;
John Wiegley429bb272011-04-08 18:41:53 +0000609 if (TryConstCast(Self, SrcExpr.get(), DestType, /*CStyle*/false, msg) != TC_Success
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000610 && msg != 0)
611 Self.Diag(OpRange.getBegin(), msg) << CT_Const
John Wiegley429bb272011-04-08 18:41:53 +0000612 << SrcExpr.get()->getType() << DestType << OpRange;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000613}
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);
620void
John Wiegley429bb272011-04-08 18:41:53 +0000621CheckReinterpretCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000622 ExprValueKind &VK, const SourceRange &OpRange,
623 const SourceRange &DestRange, CastKind &Kind) {
624 VK = Expr::getValueKindForType(DestType);
John Wiegley429bb272011-04-08 18:41:53 +0000625 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 Redl9cc11e72009-07-25 15:41:38 +0000630
631 unsigned msg = diag::err_bad_cxx_cast_generic;
John McCallf85e1932011-06-15 23:02:42 +0000632 TryCastResult tcr =
633 TryReinterpretCast(Self, SrcExpr, DestType,
634 /*CStyle*/false, OpRange, msg, Kind);
635 if (tcr != TC_Success && msg != 0)
Douglas Gregor8e960432010-11-08 03:40:48 +0000636 {
John Wiegley429bb272011-04-08 18:41:53 +0000637 if (SrcExpr.isInvalid()) // if conversion failed, don't report another error
638 return;
639 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
Douglas Gregor8e960432010-11-08 03:40:48 +0000640 //FIXME: &f<int>; is overloaded and resolvable
641 Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload)
John Wiegley429bb272011-04-08 18:41:53 +0000642 << OverloadExpr::find(SrcExpr.get()).Expression->getName()
Douglas Gregor8e960432010-11-08 03:40:48 +0000643 << DestType << OpRange;
John Wiegley429bb272011-04-08 18:41:53 +0000644 Self.NoteAllOverloadCandidates(SrcExpr.get());
Douglas Gregor8e960432010-11-08 03:40:48 +0000645
John McCall79ab2c82011-02-14 18:34:10 +0000646 } else {
John Wiegley429bb272011-04-08 18:41:53 +0000647 diagnoseBadCast(Self, msg, CT_Reinterpret, OpRange, SrcExpr.get(), DestType);
Douglas Gregor8e960432010-11-08 03:40:48 +0000648 }
John McCallf85e1932011-06-15 23:02:42 +0000649 } else if (tcr == TC_Success && Self.getLangOptions().ObjCAutoRefCount) {
Fariborz Jahanianaf975172011-06-21 17:38:29 +0000650 Expr *Exp = SrcExpr.get();
Fariborz Jahanian06269422011-06-21 18:21:32 +0000651 // Note that Exp does not change with CCK_OtherCast cast type
John McCallf85e1932011-06-15 23:02:42 +0000652 Self.CheckObjCARCConversion(OpRange, DestType,
Fariborz Jahanianaf975172011-06-21 17:38:29 +0000653 Exp, Sema::CCK_OtherCast);
John McCallf85e1932011-06-15 23:02:42 +0000654 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000655}
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.
661void
John Wiegley429bb272011-04-08 18:41:53 +0000662CheckStaticCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000663 ExprValueKind &VK, const SourceRange &OpRange,
664 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000665 // 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 Friedman05d9d7a2009-11-16 05:44:20 +0000668 if (DestType->isVoidType()) {
John Wiegley429bb272011-04-08 18:41:53 +0000669 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 Gregor1be8eec2011-02-19 21:32:49 +0000673 ExprResult SingleFunctionExpression =
John Wiegley429bb272011-04-08 18:41:53 +0000674 Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr.get(),
Douglas Gregor1be8eec2011-02-19 21:32:49 +0000675 false, // Decay Function to ptr
676 true, // Complain
677 OpRange, DestType, diag::err_bad_static_cast_overload);
678 if (SingleFunctionExpression.isUsable())
679 {
John Wiegley429bb272011-04-08 18:41:53 +0000680 SrcExpr = SingleFunctionExpression;
Douglas Gregor1be8eec2011-02-19 21:32:49 +0000681 Kind = CK_ToVoid;
682 }
683 }
684 else
685 Kind = CK_ToVoid;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000686 return;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000687 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000688
John McCallf89e55a2010-11-18 06:31:45 +0000689 VK = Expr::getValueKindForType(DestType);
John Wiegley429bb272011-04-08 18:41:53 +0000690 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 Redl9cc11e72009-07-25 15:41:38 +0000695
696 unsigned msg = diag::err_bad_cxx_cast_generic;
John McCallf85e1932011-06-15 23:02:42 +0000697 TryCastResult tcr
698 = TryStaticCast(Self, SrcExpr, DestType, Sema::CCK_OtherCast, OpRange, msg,
699 Kind, BasePath);
700 if (tcr != TC_Success && msg != 0) {
John Wiegley429bb272011-04-08 18:41:53 +0000701 if (SrcExpr.isInvalid())
702 return;
703 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
704 OverloadExpr* oe = OverloadExpr::find(SrcExpr.get()).Expression;
Douglas Gregor8e960432010-11-08 03:40:48 +0000705 Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
Douglas Gregor4c9be892011-02-28 20:01:57 +0000706 << oe->getName() << DestType << OpRange
707 << oe->getQualifierLoc().getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +0000708 Self.NoteAllOverloadCandidates(SrcExpr.get());
John McCall79ab2c82011-02-14 18:34:10 +0000709 } else {
John Wiegley429bb272011-04-08 18:41:53 +0000710 diagnoseBadCast(Self, msg, CT_Static, OpRange, SrcExpr.get(), DestType);
Douglas Gregor8e960432010-11-08 03:40:48 +0000711 }
John McCallf85e1932011-06-15 23:02:42 +0000712 } else if (tcr == TC_Success) {
713 if (Kind == CK_BitCast)
714 Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange);
Fariborz Jahanianaf975172011-06-21 17:38:29 +0000715 if (Self.getLangOptions().ObjCAutoRefCount) {
716 Expr *Exp = SrcExpr.get();
Fariborz Jahanian06269422011-06-21 18:21:32 +0000717 // Note that Exp does not change with CCK_OtherCast cast type
John McCallf85e1932011-06-15 23:02:42 +0000718 Self.CheckObjCARCConversion(OpRange, DestType,
Fariborz Jahanianaf975172011-06-21 17:38:29 +0000719 Exp, Sema::CCK_OtherCast);
720 }
Douglas Gregor8e960432010-11-08 03:40:48 +0000721 }
John McCalle2b76882010-11-16 05:46:29 +0000722 else if (Kind == CK_BitCast)
John Wiegley429bb272011-04-08 18:41:53 +0000723 Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000724}
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 Wiegley429bb272011-04-08 18:41:53 +0000729static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr,
John McCallf85e1932011-06-15 23:02:42 +0000730 QualType DestType,
731 Sema::CheckedConversionKind CCK,
Anders Carlssoncb3c3082009-09-01 20:52:42 +0000732 const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000733 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000734 CXXCastPath &BasePath) {
John McCallf85e1932011-06-15 23:02:42 +0000735 // 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 Redl9cc11e72009-07-25 15:41:38 +0000739 // 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 Wiegley429bb272011-04-08 18:41:53 +0000759 tcr = TryStaticReferenceDowncast(Self, SrcExpr.get(), DestType, CStyle, OpRange,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000760 msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000761 if (tcr != TC_NotApplicable)
762 return tcr;
763
Douglas Gregordc843f22011-01-22 00:06:57 +0000764 // 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 Wiegley429bb272011-04-08 18:41:53 +0000767 tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind, BasePath,
Douglas Gregor8ec14e62011-01-26 21:04:06 +0000768 msg);
Douglas Gregor88b22a42011-01-25 16:13:26 +0000769 if (tcr != TC_NotApplicable)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000770 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 McCallf85e1932011-06-15 23:02:42 +0000774 tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CCK, OpRange, msg,
Douglas Gregord6e44a32010-04-16 22:09:46 +0000775 Kind);
John Wiegley429bb272011-04-08 18:41:53 +0000776 if (SrcExpr.isInvalid())
777 return TC_Failed;
Anders Carlsson3c31a392009-09-26 00:12:34 +0000778 if (tcr != TC_NotApplicable)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000779 return tcr;
Anders Carlsson0aebc812009-09-09 21:33:21 +0000780
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000781 // 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 Wiegley429bb272011-04-08 18:41:53 +0000789 QualType SrcType = Self.Context.getCanonicalType(SrcExpr.get()->getType());
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000790
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000791 // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly
Douglas Gregor1e856d92011-02-18 03:01:41 +0000792 // 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 Gregor1274ccd2010-10-08 23:50:27 +0000806 }
807 }
Douglas Gregor1e856d92011-02-18 03:01:41 +0000808
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000809 // 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()) {
818 if (SrcType->isComplexType() || SrcType->isVectorType()) {
819 // Fall through - these cannot be converted.
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000820 } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
John McCall2de56d12010-08-25 11:45:40 +0000821 Kind = CK_IntegralCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000822 return TC_Success;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000823 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000824 }
825
826 // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
827 // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000828 tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000829 Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000830 if (tcr != TC_NotApplicable)
831 return tcr;
832
833 // Reverse member pointer conversion. C++ 4.11 specifies member pointer
834 // conversion. C++ 5.2.9p9 has additional information.
835 // DR54's access restrictions apply here also.
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000836 tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
Anders Carlssoncee22422010-04-24 19:22:20 +0000837 OpRange, msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000838 if (tcr != TC_NotApplicable)
839 return tcr;
840
841 // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
842 // void*. C++ 5.2.9p10 specifies additional restrictions, which really is
843 // just the usual constness stuff.
Ted Kremenek6217b802009-07-29 21:53:49 +0000844 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000845 QualType SrcPointee = SrcPointer->getPointeeType();
846 if (SrcPointee->isVoidType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000847 if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000848 QualType DestPointee = DestPointer->getPointeeType();
849 if (DestPointee->isIncompleteOrObjectType()) {
850 // This is definitely the intended conversion, but it might fail due
John McCallf85e1932011-06-15 23:02:42 +0000851 // to a qualifier violation. Note that we permit Objective-C lifetime
852 // and GC qualifier mismatches here.
853 if (!CStyle) {
854 Qualifiers DestPointeeQuals = DestPointee.getQualifiers();
855 Qualifiers SrcPointeeQuals = SrcPointee.getQualifiers();
856 DestPointeeQuals.removeObjCGCAttr();
857 DestPointeeQuals.removeObjCLifetime();
858 SrcPointeeQuals.removeObjCGCAttr();
859 SrcPointeeQuals.removeObjCLifetime();
860 if (DestPointeeQuals != SrcPointeeQuals &&
861 !DestPointeeQuals.compatiblyIncludes(SrcPointeeQuals)) {
862 msg = diag::err_bad_cxx_cast_qualifiers_away;
863 return TC_Failed;
864 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000865 }
John McCall2de56d12010-08-25 11:45:40 +0000866 Kind = CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000867 return TC_Success;
868 }
869 }
Fariborz Jahanian2f6c5502010-05-10 23:46:53 +0000870 else if (DestType->isObjCObjectPointerType()) {
871 // allow both c-style cast and static_cast of objective-c pointers as
872 // they are pervasive.
John McCall2de56d12010-08-25 11:45:40 +0000873 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +0000874 return TC_Success;
875 }
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000876 else if (CStyle && DestType->isBlockPointerType()) {
877 // allow c-style cast of void * to block pointers.
John McCall2de56d12010-08-25 11:45:40 +0000878 Kind = CK_AnyPointerToBlockPointerCast;
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000879 return TC_Success;
880 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000881 }
882 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000883 // Allow arbitray objective-c pointer conversion with static casts.
884 if (SrcType->isObjCObjectPointerType() &&
John McCalldaa8e4e2010-11-15 09:13:47 +0000885 DestType->isObjCObjectPointerType()) {
886 Kind = CK_BitCast;
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000887 return TC_Success;
John McCalldaa8e4e2010-11-15 09:13:47 +0000888 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000889
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000890 // We tried everything. Everything! Nothing works! :-(
891 return TC_NotApplicable;
892}
893
894/// Tests whether a conversion according to N2844 is valid.
895TryCastResult
896TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
Douglas Gregor8ec14e62011-01-26 21:04:06 +0000897 bool CStyle, CastKind &Kind, CXXCastPath &BasePath,
898 unsigned &msg) {
Douglas Gregordc843f22011-01-22 00:06:57 +0000899 // C++0x [expr.static.cast]p3:
900 // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to
901 // cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
Ted Kremenek6217b802009-07-29 21:53:49 +0000902 const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000903 if (!R)
904 return TC_NotApplicable;
905
Douglas Gregordc843f22011-01-22 00:06:57 +0000906 if (!SrcExpr->isGLValue())
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000907 return TC_NotApplicable;
908
909 // Because we try the reference downcast before this function, from now on
910 // this is the only cast possibility, so we issue an error if we fail now.
911 // FIXME: Should allow casting away constness if CStyle.
912 bool DerivedToBase;
Douglas Gregor569c3162010-08-07 11:51:51 +0000913 bool ObjCConversion;
John McCallf85e1932011-06-15 23:02:42 +0000914 bool ObjCLifetimeConversion;
Douglas Gregor8ec14e62011-01-26 21:04:06 +0000915 QualType FromType = SrcExpr->getType();
916 QualType ToType = R->getPointeeType();
917 if (CStyle) {
918 FromType = FromType.getUnqualifiedType();
919 ToType = ToType.getUnqualifiedType();
920 }
921
Douglas Gregor393896f2009-11-05 13:06:35 +0000922 if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
Douglas Gregor8ec14e62011-01-26 21:04:06 +0000923 ToType, FromType,
John McCallf85e1932011-06-15 23:02:42 +0000924 DerivedToBase, ObjCConversion,
925 ObjCLifetimeConversion)
926 < Sema::Ref_Compatible_With_Added_Qualification) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000927 msg = diag::err_bad_lvalue_to_rvalue_cast;
928 return TC_Failed;
929 }
930
Douglas Gregor88b22a42011-01-25 16:13:26 +0000931 if (DerivedToBase) {
932 Kind = CK_DerivedToBase;
933 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
934 /*DetectVirtual=*/true);
935 if (!Self.IsDerivedFrom(SrcExpr->getType(), R->getPointeeType(), Paths))
936 return TC_NotApplicable;
937
938 Self.BuildBasePathArray(Paths, BasePath);
939 } else
940 Kind = CK_NoOp;
941
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000942 return TC_Success;
943}
944
945/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
946TryCastResult
947TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
948 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000949 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000950 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000951 // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
952 // cast to type "reference to cv2 D", where D is a class derived from B,
953 // if a valid standard conversion from "pointer to D" to "pointer to B"
954 // exists, cv2 >= cv1, and B is not a virtual base class of D.
955 // In addition, DR54 clarifies that the base must be accessible in the
956 // current context. Although the wording of DR54 only applies to the pointer
957 // variant of this rule, the intent is clearly for it to apply to the this
958 // conversion as well.
959
Ted Kremenek6217b802009-07-29 21:53:49 +0000960 const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000961 if (!DestReference) {
962 return TC_NotApplicable;
963 }
964 bool RValueRef = DestReference->isRValueReferenceType();
John McCall7eb0a9e2010-11-24 05:12:34 +0000965 if (!RValueRef && !SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000966 // We know the left side is an lvalue reference, so we can suggest a reason.
967 msg = diag::err_bad_cxx_cast_rvalue;
968 return TC_NotApplicable;
969 }
970
971 QualType DestPointee = DestReference->getPointeeType();
972
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000973 return TryStaticDowncast(Self,
974 Self.Context.getCanonicalType(SrcExpr->getType()),
975 Self.Context.getCanonicalType(DestPointee), CStyle,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000976 OpRange, SrcExpr->getType(), DestType, msg, Kind,
977 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000978}
979
980/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
981TryCastResult
982TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
Mike Stump1eb44332009-09-09 15:08:12 +0000983 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000984 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000985 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000986 // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
987 // type, can be converted to an rvalue of type "pointer to cv2 D", where D
988 // is a class derived from B, if a valid standard conversion from "pointer
989 // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
990 // class of D.
991 // In addition, DR54 clarifies that the base must be accessible in the
992 // current context.
993
Ted Kremenek6217b802009-07-29 21:53:49 +0000994 const PointerType *DestPointer = DestType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000995 if (!DestPointer) {
996 return TC_NotApplicable;
997 }
998
Ted Kremenek6217b802009-07-29 21:53:49 +0000999 const PointerType *SrcPointer = SrcType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001000 if (!SrcPointer) {
1001 msg = diag::err_bad_static_cast_pointer_nonpointer;
1002 return TC_NotApplicable;
1003 }
1004
Douglas Gregorab15d0e2009-11-15 09:20:52 +00001005 return TryStaticDowncast(Self,
1006 Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
1007 Self.Context.getCanonicalType(DestPointer->getPointeeType()),
Anders Carlssonf9d68e12010-04-24 19:36:51 +00001008 CStyle, OpRange, SrcType, DestType, msg, Kind,
1009 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001010}
1011
1012/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
1013/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
Douglas Gregorab15d0e2009-11-15 09:20:52 +00001014/// DestType is possible and allowed.
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001015TryCastResult
Douglas Gregorab15d0e2009-11-15 09:20:52 +00001016TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001017 bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
Anders Carlsson95c5d8a2009-11-12 16:53:16 +00001018 QualType OrigDestType, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001019 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl5ed66f72009-10-22 15:07:22 +00001020 // We can only work with complete types. But don't complain if it doesn't work
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001021 if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) ||
1022 Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0)))
Sebastian Redl5ed66f72009-10-22 15:07:22 +00001023 return TC_NotApplicable;
1024
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001025 // Downcast can only happen in class hierarchies, so we need classes.
Douglas Gregorab15d0e2009-11-15 09:20:52 +00001026 if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001027 return TC_NotApplicable;
1028 }
1029
Anders Carlssonf9d68e12010-04-24 19:36:51 +00001030 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Douglas Gregora8f32e02009-10-06 17:59:45 +00001031 /*DetectVirtual=*/true);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001032 if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
1033 return TC_NotApplicable;
1034 }
1035
1036 // Target type does derive from source type. Now we're serious. If an error
1037 // appears now, it's not ignored.
1038 // This may not be entirely in line with the standard. Take for example:
1039 // struct A {};
1040 // struct B : virtual A {
1041 // B(A&);
1042 // };
Mike Stump1eb44332009-09-09 15:08:12 +00001043 //
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001044 // void f()
1045 // {
1046 // (void)static_cast<const B&>(*((A*)0));
1047 // }
1048 // As far as the standard is concerned, p5 does not apply (A is virtual), so
1049 // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
1050 // However, both GCC and Comeau reject this example, and accepting it would
1051 // mean more complex code if we're to preserve the nice error message.
1052 // FIXME: Being 100% compliant here would be nice to have.
1053
1054 // Must preserve cv, as always, unless we're in C-style mode.
1055 if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
Douglas Gregord4c5f842011-04-15 17:59:54 +00001056 msg = diag::err_bad_cxx_cast_qualifiers_away;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001057 return TC_Failed;
1058 }
1059
1060 if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
1061 // This code is analoguous to that in CheckDerivedToBaseConversion, except
1062 // that it builds the paths in reverse order.
1063 // To sum up: record all paths to the base and build a nice string from
1064 // them. Use it to spice up the error message.
1065 if (!Paths.isRecordingPaths()) {
1066 Paths.clear();
1067 Paths.setRecordingPaths(true);
1068 Self.IsDerivedFrom(DestType, SrcType, Paths);
1069 }
1070 std::string PathDisplayStr;
1071 std::set<unsigned> DisplayedPaths;
Douglas Gregora8f32e02009-10-06 17:59:45 +00001072 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001073 PI != PE; ++PI) {
1074 if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
1075 // We haven't displayed a path to this particular base
1076 // class subobject yet.
1077 PathDisplayStr += "\n ";
Douglas Gregora8f32e02009-10-06 17:59:45 +00001078 for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
1079 EE = PI->rend();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001080 EI != EE; ++EI)
1081 PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
Douglas Gregorab15d0e2009-11-15 09:20:52 +00001082 PathDisplayStr += QualType(DestType).getAsString();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001083 }
1084 }
1085
1086 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
Douglas Gregorab15d0e2009-11-15 09:20:52 +00001087 << QualType(SrcType).getUnqualifiedType()
1088 << QualType(DestType).getUnqualifiedType()
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001089 << PathDisplayStr << OpRange;
1090 msg = 0;
1091 return TC_Failed;
1092 }
1093
1094 if (Paths.getDetectedVirtual() != 0) {
1095 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
1096 Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
1097 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
1098 msg = 0;
1099 return TC_Failed;
1100 }
1101
John McCall417d39f2011-02-14 23:21:33 +00001102 if (!CStyle) {
1103 switch (Self.CheckBaseClassAccess(OpRange.getBegin(),
1104 SrcType, DestType,
1105 Paths.front(),
John McCall58e6f342010-03-16 05:22:47 +00001106 diag::err_downcast_from_inaccessible_base)) {
John McCall417d39f2011-02-14 23:21:33 +00001107 case Sema::AR_accessible:
1108 case Sema::AR_delayed: // be optimistic
1109 case Sema::AR_dependent: // be optimistic
1110 break;
1111
1112 case Sema::AR_inaccessible:
1113 msg = 0;
1114 return TC_Failed;
1115 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001116 }
1117
Anders Carlssonf9d68e12010-04-24 19:36:51 +00001118 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +00001119 Kind = CK_BaseToDerived;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001120 return TC_Success;
1121}
1122
1123/// TryStaticMemberPointerUpcast - Tests whether a conversion according to
1124/// C++ 5.2.9p9 is valid:
1125///
1126/// An rvalue of type "pointer to member of D of type cv1 T" can be
1127/// converted to an rvalue of type "pointer to member of B of type cv2 T",
1128/// where B is a base class of D [...].
1129///
1130TryCastResult
John Wiegley429bb272011-04-08 18:41:53 +00001131TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, QualType SrcType,
Douglas Gregor4ce46c22010-03-07 23:24:59 +00001132 QualType DestType, bool CStyle,
1133 const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +00001134 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +00001135 CXXCastPath &BasePath) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001136 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001137 if (!DestMemPtr)
1138 return TC_NotApplicable;
Douglas Gregor4ce46c22010-03-07 23:24:59 +00001139
1140 bool WasOverloadedFunction = false;
John McCall6bb80172010-03-30 21:47:33 +00001141 DeclAccessPair FoundOverload;
John Wiegley429bb272011-04-08 18:41:53 +00001142 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00001143 if (FunctionDecl *Fn
John Wiegley429bb272011-04-08 18:41:53 +00001144 = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), DestType, false,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00001145 FoundOverload)) {
1146 CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
1147 SrcType = Self.Context.getMemberPointerType(Fn->getType(),
1148 Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
1149 WasOverloadedFunction = true;
1150 }
Douglas Gregor4ce46c22010-03-07 23:24:59 +00001151 }
Douglas Gregor1a8cf732010-04-14 23:11:21 +00001152
Ted Kremenek6217b802009-07-29 21:53:49 +00001153 const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001154 if (!SrcMemPtr) {
1155 msg = diag::err_bad_static_cast_member_pointer_nonmp;
1156 return TC_NotApplicable;
1157 }
1158
1159 // T == T, modulo cv
Douglas Gregora4923eb2009-11-16 21:35:15 +00001160 if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
1161 DestMemPtr->getPointeeType()))
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001162 return TC_NotApplicable;
1163
1164 // B base of D
1165 QualType SrcClass(SrcMemPtr->getClass(), 0);
1166 QualType DestClass(DestMemPtr->getClass(), 0);
Anders Carlssoncee22422010-04-24 19:22:20 +00001167 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001168 /*DetectVirtual=*/true);
1169 if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
1170 return TC_NotApplicable;
1171 }
1172
1173 // B is a base of D. But is it an allowed base? If not, it's a hard error.
Douglas Gregore0d5fe22010-05-21 20:29:55 +00001174 if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001175 Paths.clear();
1176 Paths.setRecordingPaths(true);
1177 bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
1178 assert(StillOkay);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001179 (void)StillOkay;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001180 std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
1181 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
1182 << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
1183 msg = 0;
1184 return TC_Failed;
1185 }
1186
1187 if (const RecordType *VBase = Paths.getDetectedVirtual()) {
1188 Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
1189 << SrcClass << DestClass << QualType(VBase, 0) << OpRange;
1190 msg = 0;
1191 return TC_Failed;
1192 }
1193
John McCall417d39f2011-02-14 23:21:33 +00001194 if (!CStyle) {
1195 switch (Self.CheckBaseClassAccess(OpRange.getBegin(),
1196 DestClass, SrcClass,
1197 Paths.front(),
1198 diag::err_upcast_to_inaccessible_base)) {
1199 case Sema::AR_accessible:
1200 case Sema::AR_delayed:
1201 case Sema::AR_dependent:
1202 // Optimistically assume that the delayed and dependent cases
1203 // will work out.
1204 break;
1205
1206 case Sema::AR_inaccessible:
1207 msg = 0;
1208 return TC_Failed;
1209 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001210 }
1211
Douglas Gregor4ce46c22010-03-07 23:24:59 +00001212 if (WasOverloadedFunction) {
1213 // Resolve the address of the overloaded function again, this time
1214 // allowing complaints if something goes wrong.
John Wiegley429bb272011-04-08 18:41:53 +00001215 FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(),
Douglas Gregor4ce46c22010-03-07 23:24:59 +00001216 DestType,
John McCall6bb80172010-03-30 21:47:33 +00001217 true,
1218 FoundOverload);
Douglas Gregor4ce46c22010-03-07 23:24:59 +00001219 if (!Fn) {
1220 msg = 0;
1221 return TC_Failed;
1222 }
1223
John McCall6bb80172010-03-30 21:47:33 +00001224 SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
John Wiegley429bb272011-04-08 18:41:53 +00001225 if (!SrcExpr.isUsable()) {
Douglas Gregor4ce46c22010-03-07 23:24:59 +00001226 msg = 0;
1227 return TC_Failed;
1228 }
1229 }
1230
Anders Carlssoncee22422010-04-24 19:22:20 +00001231 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +00001232 Kind = CK_DerivedToBaseMemberPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001233 return TC_Success;
1234}
1235
1236/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
1237/// is valid:
1238///
1239/// An expression e can be explicitly converted to a type T using a
1240/// @c static_cast if the declaration "T t(e);" is well-formed [...].
1241TryCastResult
John Wiegley429bb272011-04-08 18:41:53 +00001242TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
John McCallf85e1932011-06-15 23:02:42 +00001243 Sema::CheckedConversionKind CCK,
1244 const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001245 CastKind &Kind) {
Anders Carlssond851b372009-09-07 18:25:47 +00001246 if (DestType->isRecordType()) {
1247 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
1248 diag::err_bad_dynamic_cast_incomplete)) {
1249 msg = 0;
1250 return TC_Failed;
1251 }
1252 }
Douglas Gregord6e44a32010-04-16 22:09:46 +00001253
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001254 InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
1255 InitializationKind InitKind
John McCallf85e1932011-06-15 23:02:42 +00001256 = (CCK == Sema::CCK_CStyleCast)
1257 ? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange)
1258 : (CCK == Sema::CCK_FunctionalCast)
1259 ? InitializationKind::CreateFunctionalCast(OpRange)
1260 : InitializationKind::CreateCast(OpRange);
John Wiegley429bb272011-04-08 18:41:53 +00001261 Expr *SrcExprRaw = SrcExpr.get();
1262 InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExprRaw, 1);
Douglas Gregor8e960432010-11-08 03:40:48 +00001263
1264 // At this point of CheckStaticCast, if the destination is a reference,
1265 // or the expression is an overload expression this has to work.
1266 // There is no other way that works.
1267 // On the other hand, if we're checking a C-style cast, we've still got
1268 // the reinterpret_cast way.
John McCallf85e1932011-06-15 23:02:42 +00001269 bool CStyle
1270 = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast);
Sebastian Redl383616c2011-06-05 12:23:28 +00001271 if (InitSeq.Failed() && (CStyle || !DestType->isReferenceType()))
Anders Carlsson3c31a392009-09-26 00:12:34 +00001272 return TC_NotApplicable;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001273
John McCall60d7b3a2010-08-24 06:29:42 +00001274 ExprResult Result
John Wiegley429bb272011-04-08 18:41:53 +00001275 = InitSeq.Perform(Self, Entity, InitKind, MultiExprArg(Self, &SrcExprRaw, 1));
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001276 if (Result.isInvalid()) {
1277 msg = 0;
1278 return TC_Failed;
1279 }
1280
Douglas Gregord6e44a32010-04-16 22:09:46 +00001281 if (InitSeq.isConstructorInitialization())
John McCall2de56d12010-08-25 11:45:40 +00001282 Kind = CK_ConstructorConversion;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001283 else
John McCall2de56d12010-08-25 11:45:40 +00001284 Kind = CK_NoOp;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001285
John Wiegley429bb272011-04-08 18:41:53 +00001286 SrcExpr = move(Result);
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001287 return TC_Success;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001288}
1289
1290/// TryConstCast - See if a const_cast from source to destination is allowed,
1291/// and perform it if it is.
1292static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
1293 bool CStyle, unsigned &msg) {
1294 DestType = Self.Context.getCanonicalType(DestType);
1295 QualType SrcType = SrcExpr->getType();
Douglas Gregor575d2a32011-01-22 00:19:52 +00001296 if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) {
1297 if (DestTypeTmp->isLValueReferenceType() && !SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001298 // Cannot const_cast non-lvalue to lvalue reference type. But if this
1299 // is C-style, static_cast might find a way, so we simply suggest a
1300 // message and tell the parent to keep searching.
1301 msg = diag::err_bad_cxx_cast_rvalue;
1302 return TC_NotApplicable;
1303 }
1304
1305 // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
1306 // [...] if a pointer to T1 can be [cast] to the type pointer to T2.
1307 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1308 SrcType = Self.Context.getPointerType(SrcType);
1309 }
1310
1311 // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
1312 // the rules for const_cast are the same as those used for pointers.
1313
John McCalld425d2b2010-05-18 09:35:29 +00001314 if (!DestType->isPointerType() &&
1315 !DestType->isMemberPointerType() &&
1316 !DestType->isObjCObjectPointerType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001317 // Cannot cast to non-pointer, non-reference type. Note that, if DestType
1318 // was a reference type, we converted it to a pointer above.
1319 // The status of rvalue references isn't entirely clear, but it looks like
1320 // conversion to them is simply invalid.
1321 // C++ 5.2.11p3: For two pointer types [...]
1322 if (!CStyle)
1323 msg = diag::err_bad_const_cast_dest;
1324 return TC_NotApplicable;
1325 }
1326 if (DestType->isFunctionPointerType() ||
1327 DestType->isMemberFunctionPointerType()) {
1328 // Cannot cast direct function pointers.
1329 // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
1330 // T is the ultimate pointee of source and target type.
1331 if (!CStyle)
1332 msg = diag::err_bad_const_cast_dest;
1333 return TC_NotApplicable;
1334 }
1335 SrcType = Self.Context.getCanonicalType(SrcType);
1336
1337 // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
1338 // completely equal.
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001339 // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
1340 // in multi-level pointers may change, but the level count must be the same,
1341 // as must be the final pointee type.
1342 while (SrcType != DestType &&
Douglas Gregor5a57efd2010-06-09 03:53:18 +00001343 Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) {
Douglas Gregord4c5f842011-04-15 17:59:54 +00001344 Qualifiers SrcQuals, DestQuals;
1345 SrcType = Self.Context.getUnqualifiedArrayType(SrcType, SrcQuals);
1346 DestType = Self.Context.getUnqualifiedArrayType(DestType, DestQuals);
1347
1348 // const_cast is permitted to strip cvr-qualifiers, only. Make sure that
1349 // the other qualifiers (e.g., address spaces) are identical.
1350 SrcQuals.removeCVRQualifiers();
1351 DestQuals.removeCVRQualifiers();
1352 if (SrcQuals != DestQuals)
1353 return TC_NotApplicable;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001354 }
1355
1356 // Since we're dealing in canonical types, the remainder must be the same.
1357 if (SrcType != DestType)
1358 return TC_NotApplicable;
1359
1360 return TC_Success;
1361}
1362
Argyrios Kyrtzidisf4bbbf02011-05-02 18:21:19 +00001363// Checks for undefined behavior in reinterpret_cast.
1364// The cases that is checked for is:
1365// *reinterpret_cast<T*>(&a)
1366// reinterpret_cast<T&>(a)
1367// where accessing 'a' as type 'T' will result in undefined behavior.
1368void Sema::CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType,
1369 bool IsDereference,
1370 SourceRange Range) {
1371 unsigned DiagID = IsDereference ?
1372 diag::warn_pointer_indirection_from_incompatible_type :
1373 diag::warn_undefined_reinterpret_cast;
1374
1375 if (Diags.getDiagnosticLevel(DiagID, Range.getBegin()) ==
1376 Diagnostic::Ignored) {
1377 return;
1378 }
1379
1380 QualType SrcTy, DestTy;
1381 if (IsDereference) {
1382 if (!SrcType->getAs<PointerType>() || !DestType->getAs<PointerType>()) {
1383 return;
1384 }
1385 SrcTy = SrcType->getPointeeType();
1386 DestTy = DestType->getPointeeType();
1387 } else {
1388 if (!DestType->getAs<ReferenceType>()) {
1389 return;
1390 }
1391 SrcTy = SrcType;
1392 DestTy = DestType->getPointeeType();
1393 }
1394
1395 // Cast is compatible if the types are the same.
1396 if (Context.hasSameUnqualifiedType(DestTy, SrcTy)) {
1397 return;
1398 }
1399 // or one of the types is a char or void type
1400 if (DestTy->isAnyCharacterType() || DestTy->isVoidType() ||
1401 SrcTy->isAnyCharacterType() || SrcTy->isVoidType()) {
1402 return;
1403 }
1404 // or one of the types is a tag type.
Chandler Carruth1f8f2d52011-05-24 07:43:19 +00001405 if (SrcTy->getAs<TagType>() || DestTy->getAs<TagType>()) {
Argyrios Kyrtzidisf4bbbf02011-05-02 18:21:19 +00001406 return;
1407 }
1408
Douglas Gregor575a1c92011-05-20 16:38:50 +00001409 // FIXME: Scoped enums?
Argyrios Kyrtzidisf4bbbf02011-05-02 18:21:19 +00001410 if ((SrcTy->isUnsignedIntegerType() && DestTy->isSignedIntegerType()) ||
1411 (SrcTy->isSignedIntegerType() && DestTy->isUnsignedIntegerType())) {
1412 if (Context.getTypeSize(DestTy) == Context.getTypeSize(SrcTy)) {
1413 return;
1414 }
1415 }
1416
1417 Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range;
1418}
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001419
John Wiegley429bb272011-04-08 18:41:53 +00001420static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr,
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001421 QualType DestType, bool CStyle,
1422 const SourceRange &OpRange,
Anders Carlsson3c31a392009-09-26 00:12:34 +00001423 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001424 CastKind &Kind) {
Douglas Gregore39a3892010-07-13 23:17:26 +00001425 bool IsLValueCast = false;
1426
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001427 DestType = Self.Context.getCanonicalType(DestType);
John Wiegley429bb272011-04-08 18:41:53 +00001428 QualType SrcType = SrcExpr.get()->getType();
Douglas Gregor8e960432010-11-08 03:40:48 +00001429
1430 // Is the source an overloaded name? (i.e. &foo)
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001431 // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5) ...
1432 if (SrcType == Self.Context.OverloadTy) {
1433 // ... unless foo<int> resolves to an lvalue unambiguously
1434 ExprResult SingleFunctionExpr =
John Wiegley429bb272011-04-08 18:41:53 +00001435 Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr.get(),
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001436 Expr::getValueKindForType(DestType) == VK_RValue // Convert Fun to Ptr
1437 );
1438 if (SingleFunctionExpr.isUsable()) {
John Wiegley429bb272011-04-08 18:41:53 +00001439 SrcExpr = move(SingleFunctionExpr);
1440 SrcType = SrcExpr.get()->getType();
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001441 }
1442 else
1443 return TC_NotApplicable;
1444 }
Douglas Gregor8e960432010-11-08 03:40:48 +00001445
Ted Kremenek6217b802009-07-29 21:53:49 +00001446 if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001447 bool LValue = DestTypeTmp->isLValueReferenceType();
John Wiegley429bb272011-04-08 18:41:53 +00001448 if (LValue && !SrcExpr.get()->isLValue()) {
Douglas Gregor575d2a32011-01-22 00:19:52 +00001449 // Cannot cast non-lvalue to lvalue reference type. See the similar
1450 // comment in const_cast.
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001451 msg = diag::err_bad_cxx_cast_rvalue;
1452 return TC_NotApplicable;
1453 }
1454
Argyrios Kyrtzidisf4bbbf02011-05-02 18:21:19 +00001455 if (!CStyle) {
1456 Self.CheckCompatibleReinterpretCast(SrcType, DestType,
1457 /*isDereference=*/false, OpRange);
1458 }
1459
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001460 // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
1461 // same effect as the conversion *reinterpret_cast<T*>(&x) with the
1462 // built-in & and * operators.
Argyrios Kyrtzidisb464a5b2011-04-22 22:31:13 +00001463
Argyrios Kyrtzidisbb29d1b2011-04-22 23:57:57 +00001464 const char *inappropriate = 0;
1465 switch (SrcExpr.get()->getObjectKind()) {
Argyrios Kyrtzidise5e3d312011-04-23 01:10:24 +00001466 case OK_Ordinary:
1467 break;
Argyrios Kyrtzidisbb29d1b2011-04-22 23:57:57 +00001468 case OK_BitField: inappropriate = "bit-field"; break;
1469 case OK_VectorComponent: inappropriate = "vector element"; break;
1470 case OK_ObjCProperty: inappropriate = "property expression"; break;
1471 }
1472 if (inappropriate) {
1473 Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_reference)
1474 << inappropriate << DestType
1475 << OpRange << SrcExpr.get()->getSourceRange();
1476 msg = 0; SrcExpr = ExprError();
Argyrios Kyrtzidisb464a5b2011-04-22 22:31:13 +00001477 return TC_NotApplicable;
1478 }
1479
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001480 // This code does this transformation for the checked types.
1481 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1482 SrcType = Self.Context.getPointerType(SrcType);
Douglas Gregor8e960432010-11-08 03:40:48 +00001483
Douglas Gregore39a3892010-07-13 23:17:26 +00001484 IsLValueCast = true;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001485 }
1486
1487 // Canonicalize source for comparison.
1488 SrcType = Self.Context.getCanonicalType(SrcType);
1489
Ted Kremenek6217b802009-07-29 21:53:49 +00001490 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
1491 *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001492 if (DestMemPtr && SrcMemPtr) {
1493 // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
1494 // can be explicitly converted to an rvalue of type "pointer to member
1495 // of Y of type T2" if T1 and T2 are both function types or both object
1496 // types.
1497 if (DestMemPtr->getPointeeType()->isFunctionType() !=
1498 SrcMemPtr->getPointeeType()->isFunctionType())
1499 return TC_NotApplicable;
1500
1501 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
1502 // constness.
1503 // A reinterpret_cast followed by a const_cast can, though, so in C-style,
1504 // we accept it.
John McCallf85e1932011-06-15 23:02:42 +00001505 if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle,
1506 /*CheckObjCLifetime=*/CStyle)) {
Douglas Gregord4c5f842011-04-15 17:59:54 +00001507 msg = diag::err_bad_cxx_cast_qualifiers_away;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001508 return TC_Failed;
1509 }
1510
Charles Davisf231df32010-08-16 05:30:44 +00001511 // Don't allow casting between member pointers of different sizes.
1512 if (Self.Context.getTypeSize(DestMemPtr) !=
1513 Self.Context.getTypeSize(SrcMemPtr)) {
1514 msg = diag::err_bad_cxx_cast_member_pointer_size;
1515 return TC_Failed;
1516 }
1517
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001518 // A valid member pointer cast.
John McCall2de56d12010-08-25 11:45:40 +00001519 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001520 return TC_Success;
1521 }
1522
1523 // See below for the enumeral issue.
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001524 if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001525 // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
1526 // type large enough to hold it. A value of std::nullptr_t can be
1527 // converted to an integral type; the conversion has the same meaning
1528 // and validity as a conversion of (void*)0 to the integral type.
1529 if (Self.Context.getTypeSize(SrcType) >
1530 Self.Context.getTypeSize(DestType)) {
1531 msg = diag::err_bad_reinterpret_cast_small_int;
1532 return TC_Failed;
1533 }
John McCall2de56d12010-08-25 11:45:40 +00001534 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001535 return TC_Success;
1536 }
1537
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001538 bool destIsVector = DestType->isVectorType();
1539 bool srcIsVector = SrcType->isVectorType();
1540 if (srcIsVector || destIsVector) {
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001541 // FIXME: Should this also apply to floating point types?
1542 bool srcIsScalar = SrcType->isIntegralType(Self.Context);
1543 bool destIsScalar = DestType->isIntegralType(Self.Context);
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001544
1545 // Check if this is a cast between a vector and something else.
1546 if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
1547 !(srcIsVector && destIsVector))
1548 return TC_NotApplicable;
1549
1550 // If both types have the same size, we can successfully cast.
Douglas Gregorf2a55392009-12-22 22:47:22 +00001551 if (Self.Context.getTypeSize(SrcType)
1552 == Self.Context.getTypeSize(DestType)) {
John McCall2de56d12010-08-25 11:45:40 +00001553 Kind = CK_BitCast;
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001554 return TC_Success;
Douglas Gregorf2a55392009-12-22 22:47:22 +00001555 }
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001556
1557 if (destIsScalar)
1558 msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
1559 else if (srcIsScalar)
1560 msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
1561 else
1562 msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
1563
1564 return TC_Failed;
1565 }
1566
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001567 bool destIsPtr = DestType->isAnyPointerType() ||
1568 DestType->isBlockPointerType();
1569 bool srcIsPtr = SrcType->isAnyPointerType() ||
1570 SrcType->isBlockPointerType();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001571 if (!destIsPtr && !srcIsPtr) {
1572 // Except for std::nullptr_t->integer and lvalue->reference, which are
1573 // handled above, at least one of the two arguments must be a pointer.
1574 return TC_NotApplicable;
1575 }
1576
1577 if (SrcType == DestType) {
1578 // C++ 5.2.10p2 has a note that mentions that, subject to all other
1579 // restrictions, a cast to the same type is allowed. The intent is not
1580 // entirely clear here, since all other paragraphs explicitly forbid casts
1581 // to the same type. However, the behavior of compilers is pretty consistent
1582 // on this point: allow same-type conversion if the involved types are
1583 // pointers, disallow otherwise.
John McCall2de56d12010-08-25 11:45:40 +00001584 Kind = CK_NoOp;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001585 return TC_Success;
1586 }
1587
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001588 if (DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001589 assert(srcIsPtr && "One type must be a pointer");
1590 // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
Francois Pichet30aff5b2011-05-11 22:13:54 +00001591 // type large enough to hold it; except in Microsoft mode, where the
1592 // integral type size doesn't matter.
1593 if ((Self.Context.getTypeSize(SrcType) >
1594 Self.Context.getTypeSize(DestType)) &&
1595 !Self.getLangOptions().Microsoft) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001596 msg = diag::err_bad_reinterpret_cast_small_int;
1597 return TC_Failed;
1598 }
John McCall2de56d12010-08-25 11:45:40 +00001599 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001600 return TC_Success;
1601 }
1602
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001603 if (SrcType->isIntegralOrEnumerationType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001604 assert(destIsPtr && "One type must be a pointer");
1605 // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
1606 // converted to a pointer.
John McCall404cd162010-11-13 01:35:44 +00001607 // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not
1608 // necessarily converted to a null pointer value.]
John McCall2de56d12010-08-25 11:45:40 +00001609 Kind = CK_IntegralToPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001610 return TC_Success;
1611 }
1612
1613 if (!destIsPtr || !srcIsPtr) {
1614 // With the valid non-pointer conversions out of the way, we can be even
1615 // more stringent.
1616 return TC_NotApplicable;
1617 }
1618
1619 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
1620 // The C-style cast operator can.
John McCallf85e1932011-06-15 23:02:42 +00001621 if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle,
1622 /*CheckObjCLifetime=*/CStyle)) {
Douglas Gregord4c5f842011-04-15 17:59:54 +00001623 msg = diag::err_bad_cxx_cast_qualifiers_away;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001624 return TC_Failed;
1625 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001626
1627 // Cannot convert between block pointers and Objective-C object pointers.
1628 if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) ||
1629 (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType()))
1630 return TC_NotApplicable;
1631
1632 // Any pointer can be cast to an Objective-C pointer type with a C-style
1633 // cast.
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001634 if (CStyle && DestType->isObjCObjectPointerType()) {
John McCall2de56d12010-08-25 11:45:40 +00001635 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001636 return TC_Success;
1637 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001638
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001639 // Not casting away constness, so the only remaining check is for compatible
1640 // pointer categories.
John McCall2de56d12010-08-25 11:45:40 +00001641 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001642
1643 if (SrcType->isFunctionPointerType()) {
1644 if (DestType->isFunctionPointerType()) {
1645 // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
1646 // a pointer to a function of a different type.
1647 return TC_Success;
1648 }
1649
1650 // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
1651 // an object type or vice versa is conditionally-supported.
1652 // Compilers support it in C++03 too, though, because it's necessary for
1653 // casting the return value of dlsym() and GetProcAddress().
1654 // FIXME: Conditionally-supported behavior should be configurable in the
1655 // TargetInfo or similar.
1656 if (!Self.getLangOptions().CPlusPlus0x)
1657 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1658 return TC_Success;
1659 }
1660
1661 if (DestType->isFunctionPointerType()) {
1662 // See above.
1663 if (!Self.getLangOptions().CPlusPlus0x)
1664 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1665 return TC_Success;
1666 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001667
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001668 // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
1669 // a pointer to an object of different type.
1670 // Void pointers are not specified, but supported by every compiler out there.
1671 // So we finish by allowing everything that remains - it's got to be two
1672 // object pointers.
1673 return TC_Success;
John McCall79ab2c82011-02-14 18:34:10 +00001674}
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001675
John Wiegley429bb272011-04-08 18:41:53 +00001676ExprResult
John McCallf89e55a2010-11-18 06:31:45 +00001677Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, ExprValueKind &VK,
John Wiegley429bb272011-04-08 18:41:53 +00001678 Expr *CastExpr, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +00001679 CXXCastPath &BasePath,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001680 bool FunctionalStyle) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001681 // This test is outside everything else because it's the only case where
1682 // a non-lvalue-reference target type does not lead to decay.
1683 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Anders Carlssonbb378cb2009-10-18 20:31:03 +00001684 if (CastTy->isVoidType()) {
John McCallfb8721c2011-04-10 19:13:55 +00001685 Kind = CK_ToVoid;
1686
John Wiegley429bb272011-04-08 18:41:53 +00001687 ExprResult CastExprRes = IgnoredValueConversions(CastExpr);
1688 if (CastExprRes.isInvalid())
1689 return ExprError();
1690 CastExpr = CastExprRes.take();
John McCallfb8721c2011-04-10 19:13:55 +00001691
John McCall864c0412011-04-26 20:42:42 +00001692 if (CastExpr->getType() == Context.BoundMemberTy)
1693 return CheckPlaceholderExpr(CastExpr); // will always fail
1694
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001695 if (CastExpr->getType() == Context.OverloadTy) {
1696 ExprResult SingleFunctionExpr =
Douglas Gregorfadb53b2011-03-12 01:48:56 +00001697 ResolveAndFixSingleFunctionTemplateSpecialization(
1698 CastExpr, /* Decay Function to ptr */ false,
1699 /* Complain */ true, R, CastTy,
1700 diag::err_bad_cstyle_cast_overload);
John McCallfb8721c2011-04-10 19:13:55 +00001701 if (SingleFunctionExpr.isInvalid())
1702 return ExprError();
1703 CastExpr = SingleFunctionExpr.take();
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001704 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001705
John McCallfb8721c2011-04-10 19:13:55 +00001706 assert(!CastExpr->getType()->isPlaceholderType());
1707
John Wiegley429bb272011-04-08 18:41:53 +00001708 return Owned(CastExpr);
Anton Yartsevd06fea82011-03-27 09:32:40 +00001709 }
1710
John McCall9b4b9d62010-11-30 02:05:44 +00001711 // Make sure we determine the value kind before we bail out for
1712 // dependent types.
1713 VK = Expr::getValueKindForType(CastTy);
1714
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001715 // If the type is dependent, we won't do any other semantic analysis now.
John McCalldaa8e4e2010-11-15 09:13:47 +00001716 if (CastTy->isDependentType() || CastExpr->isTypeDependent()) {
1717 Kind = CK_Dependent;
John Wiegley429bb272011-04-08 18:41:53 +00001718 return Owned(CastExpr);
John McCalldaa8e4e2010-11-15 09:13:47 +00001719 }
Benjamin Kramer5b4a40a2011-07-08 20:20:17 +00001720
John Wiegley429bb272011-04-08 18:41:53 +00001721 if (VK == VK_RValue && !CastTy->isRecordType()) {
1722 ExprResult CastExprRes = DefaultFunctionArrayLvalueConversion(CastExpr);
1723 if (CastExprRes.isInvalid())
1724 return ExprError();
1725 CastExpr = CastExprRes.take();
1726 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001727
John McCallfb8721c2011-04-10 19:13:55 +00001728 // AltiVec vector initialization with a single literal.
1729 if (const VectorType *vecTy = CastTy->getAs<VectorType>())
1730 if (vecTy->getVectorKind() == VectorType::AltiVecVector
1731 && (CastExpr->getType()->isIntegerType()
1732 || CastExpr->getType()->isFloatingType())) {
1733 Kind = CK_VectorSplat;
1734 return Owned(CastExpr);
1735 }
1736
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001737 // C++ [expr.cast]p5: The conversions performed by
1738 // - a const_cast,
1739 // - a static_cast,
1740 // - a static_cast followed by a const_cast,
1741 // - a reinterpret_cast, or
1742 // - a reinterpret_cast followed by a const_cast,
1743 // can be performed using the cast notation of explicit type conversion.
1744 // [...] If a conversion can be interpreted in more than one of the ways
1745 // listed above, the interpretation that appears first in the list is used,
1746 // even if a cast resulting from that interpretation is ill-formed.
1747 // In plain language, this means trying a const_cast ...
1748 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlssoncb3c3082009-09-01 20:52:42 +00001749 TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,
1750 msg);
Anders Carlssonda921fd2009-10-19 18:14:28 +00001751 if (tcr == TC_Success)
John McCall2de56d12010-08-25 11:45:40 +00001752 Kind = CK_NoOp;
Anders Carlssonda921fd2009-10-19 18:14:28 +00001753
John McCallf85e1932011-06-15 23:02:42 +00001754 Sema::CheckedConversionKind CCK
1755 = FunctionalStyle? Sema::CCK_FunctionalCast
1756 : Sema::CCK_CStyleCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001757 if (tcr == TC_NotApplicable) {
1758 // ... or if that is not possible, a static_cast, ignoring const, ...
John Wiegley429bb272011-04-08 18:41:53 +00001759 ExprResult CastExprRes = Owned(CastExpr);
John McCallf85e1932011-06-15 23:02:42 +00001760 tcr = TryStaticCast(*this, CastExprRes, CastTy, CCK, R, msg, Kind,
1761 BasePath);
John Wiegley429bb272011-04-08 18:41:53 +00001762 if (CastExprRes.isInvalid())
1763 return ExprError();
1764 CastExpr = CastExprRes.take();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001765 if (tcr == TC_NotApplicable) {
1766 // ... and finally a reinterpret_cast, ignoring const.
John Wiegley429bb272011-04-08 18:41:53 +00001767 CastExprRes = Owned(CastExpr);
Richard Trieu32ac00d2011-04-16 01:09:30 +00001768 tcr = TryReinterpretCast(*this, CastExprRes, CastTy, /*CStyle*/true, R,
1769 msg, Kind);
John Wiegley429bb272011-04-08 18:41:53 +00001770 if (CastExprRes.isInvalid())
1771 return ExprError();
1772 CastExpr = CastExprRes.take();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001773 }
1774 }
1775
Fariborz Jahanian82007c32011-07-08 17:41:42 +00001776 if (getLangOptions().ObjCAutoRefCount && tcr == TC_Success)
John McCallf85e1932011-06-15 23:02:42 +00001777 CheckObjCARCConversion(R, CastTy, CastExpr, CCK);
1778
Nick Lewycky43328e92010-11-09 00:19:31 +00001779 if (tcr != TC_Success && msg != 0) {
1780 if (CastExpr->getType() == Context.OverloadTy) {
Douglas Gregor8e960432010-11-08 03:40:48 +00001781 DeclAccessPair Found;
John Wiegley429bb272011-04-08 18:41:53 +00001782 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(CastExpr,
Douglas Gregor8e960432010-11-08 03:40:48 +00001783 CastTy,
1784 /* Complain */ true,
1785 Found);
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001786
Richard Trieu32ac00d2011-04-16 01:09:30 +00001787 assert(!Fn && "cast failed but able to resolve overload expression!!");
Nick Lewycky43328e92010-11-09 00:19:31 +00001788 (void)Fn;
John McCall79ab2c82011-02-14 18:34:10 +00001789
Nick Lewycky43328e92010-11-09 00:19:31 +00001790 } else {
John McCall79ab2c82011-02-14 18:34:10 +00001791 diagnoseBadCast(*this, msg, (FunctionalStyle ? CT_Functional : CT_CStyle),
1792 R, CastExpr, CastTy);
Douglas Gregor8e960432010-11-08 03:40:48 +00001793 }
1794 }
John McCalle2b76882010-11-16 05:46:29 +00001795 else if (Kind == CK_BitCast)
John McCallb7f4ffe2010-08-12 21:44:57 +00001796 CheckCastAlign(CastExpr, CastTy, R);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001797
John Wiegley429bb272011-04-08 18:41:53 +00001798 if (tcr != TC_Success)
1799 return ExprError();
1800
1801 return Owned(CastExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001802}