blob: 06c2758a9bd7276ce3efa3aca85295a452afdf8d [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
25static void NoteAllOverloadCandidates(Expr* const Expr, Sema& sema);
26
Sebastian Redl9cc11e72009-07-25 15:41:38 +000027enum TryCastResult {
28 TC_NotApplicable, ///< The cast method is not applicable.
29 TC_Success, ///< The cast method is appropriate and successful.
30 TC_Failed ///< The cast method is appropriate, but failed. A
31 ///< diagnostic has been emitted.
32};
33
34enum CastType {
35 CT_Const, ///< const_cast
36 CT_Static, ///< static_cast
37 CT_Reinterpret, ///< reinterpret_cast
38 CT_Dynamic, ///< dynamic_cast
39 CT_CStyle, ///< (Type)expr
40 CT_Functional ///< Type(expr)
Sebastian Redl37d6de32008-11-08 13:00:26 +000041};
42
Douglas Gregor8e960432010-11-08 03:40:48 +000043
44
45
Sebastian Redl37d6de32008-11-08 13:00:26 +000046static void CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +000047 ExprValueKind &VK,
Sebastian Redl37d6de32008-11-08 13:00:26 +000048 const SourceRange &OpRange,
49 const SourceRange &DestRange);
50static void CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +000051 ExprValueKind &VK,
Sebastian Redl37d6de32008-11-08 13:00:26 +000052 const SourceRange &OpRange,
Anders Carlsson7f9e6462009-09-15 04:48:33 +000053 const SourceRange &DestRange,
John McCall2de56d12010-08-25 11:45:40 +000054 CastKind &Kind);
Sebastian Redl37d6de32008-11-08 13:00:26 +000055static void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +000056 ExprValueKind &VK,
Anders Carlssoncdb61972009-08-07 22:21:05 +000057 const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +000058 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +000059 CXXCastPath &BasePath);
Sebastian Redl37d6de32008-11-08 13:00:26 +000060static void CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +000061 ExprValueKind &VK,
Sebastian Redl37d6de32008-11-08 13:00:26 +000062 const SourceRange &OpRange,
Mike Stump1eb44332009-09-09 15:08:12 +000063 const SourceRange &DestRange,
John McCall2de56d12010-08-25 11:45:40 +000064 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +000065 CXXCastPath &BasePath);
Sebastian Redl37d6de32008-11-08 13:00:26 +000066
67static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType);
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,
81 QualType DestType, unsigned &msg);
82static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr,
Anders Carlssonf9d68e12010-04-24 19:36:51 +000083 QualType DestType, bool CStyle,
84 const SourceRange &OpRange,
85 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +000086 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +000087 CXXCastPath &BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +000088static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType,
89 QualType DestType, bool CStyle,
90 const SourceRange &OpRange,
Anders Carlsson95c5d8a2009-11-12 16:53:16 +000091 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +000092 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +000093 CXXCastPath &BasePath);
Douglas Gregorab15d0e2009-11-15 09:20:52 +000094static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType,
95 CanQualType DestType, bool CStyle,
Sebastian Redl9cc11e72009-07-25 15:41:38 +000096 const SourceRange &OpRange,
97 QualType OrigSrcType,
Anders Carlsson95c5d8a2009-11-12 16:53:16 +000098 QualType OrigDestType, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +000099 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000100 CXXCastPath &BasePath);
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000101static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr,
Anders Carlssoncee22422010-04-24 19:22:20 +0000102 QualType SrcType,
103 QualType DestType,bool CStyle,
104 const SourceRange &OpRange,
105 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000106 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000107 CXXCastPath &BasePath);
Anders Carlssoncee22422010-04-24 19:22:20 +0000108
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000109static TryCastResult TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000110 QualType DestType, bool CStyle,
111 const SourceRange &OpRange,
Fariborz Jahaniane9f42082009-08-26 18:55:36 +0000112 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000113 CastKind &Kind);
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000114static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000115 QualType DestType, bool CStyle,
116 const SourceRange &OpRange,
Anders Carlssoncb3c3082009-09-01 20:52:42 +0000117 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000118 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000119 CXXCastPath &BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000120static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
121 bool CStyle, unsigned &msg);
122static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
123 QualType DestType, bool CStyle,
124 const SourceRange &OpRange,
Anders Carlsson3c31a392009-09-26 00:12:34 +0000125 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000126 CastKind &Kind);
Sebastian Redl37d6de32008-11-08 13:00:26 +0000127
Sebastian Redl26d85b12008-11-05 21:50:06 +0000128/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
John McCall60d7b3a2010-08-24 06:29:42 +0000129ExprResult
Sebastian Redl26d85b12008-11-05 21:50:06 +0000130Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
John McCallb3d87482010-08-24 05:47:05 +0000131 SourceLocation LAngleBracketLoc, ParsedType Ty,
Sebastian Redl26d85b12008-11-05 21:50:06 +0000132 SourceLocation RAngleBracketLoc,
John McCallf312b1e2010-08-26 23:41:50 +0000133 SourceLocation LParenLoc, Expr *E,
Sebastian Redl26d85b12008-11-05 21:50:06 +0000134 SourceLocation RParenLoc) {
John McCallc89724c2010-01-15 19:13:16 +0000135
John McCall9d125032010-01-15 18:39:57 +0000136 TypeSourceInfo *DestTInfo;
137 QualType DestType = GetTypeFromParser(Ty, &DestTInfo);
138 if (!DestTInfo)
139 DestTInfo = Context.getTrivialTypeSourceInfo(DestType, SourceLocation());
John McCallc89724c2010-01-15 19:13:16 +0000140
141 return BuildCXXNamedCast(OpLoc, Kind, DestTInfo, move(E),
142 SourceRange(LAngleBracketLoc, RAngleBracketLoc),
143 SourceRange(LParenLoc, RParenLoc));
144}
145
John McCall60d7b3a2010-08-24 06:29:42 +0000146ExprResult
John McCallc89724c2010-01-15 19:13:16 +0000147Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
John McCallf312b1e2010-08-26 23:41:50 +0000148 TypeSourceInfo *DestTInfo, Expr *Ex,
John McCallc89724c2010-01-15 19:13:16 +0000149 SourceRange AngleBrackets, SourceRange Parens) {
John McCallc89724c2010-01-15 19:13:16 +0000150 QualType DestType = DestTInfo->getType();
151
152 SourceRange OpRange(OpLoc, Parens.getEnd());
153 SourceRange DestRange = AngleBrackets;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000154
Douglas Gregor9103bb22008-12-17 22:52:20 +0000155 // If the type is dependent, we won't do the semantic analysis now.
156 // FIXME: should we check this in a more fine-grained manner?
157 bool TypeDependent = DestType->isDependentType() || Ex->isTypeDependent();
158
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +0000159 if (Ex->isBoundMemberFunction(Context))
160 Diag(Ex->getLocStart(), diag::err_invalid_use_of_bound_member_func)
161 << Ex->getSourceRange();
162
John McCallf89e55a2010-11-18 06:31:45 +0000163 ExprValueKind VK = VK_RValue;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000164 switch (Kind) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000165 default: llvm_unreachable("Unknown C++ cast!");
Sebastian Redl26d85b12008-11-05 21:50:06 +0000166
167 case tok::kw_const_cast:
Douglas Gregor9103bb22008-12-17 22:52:20 +0000168 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000169 CheckConstCast(*this, Ex, DestType, VK, OpRange, DestRange);
John McCallf871d0c2010-08-07 06:22:56 +0000170 return Owned(CXXConstCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000171 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000172 VK, Ex, DestTInfo, OpLoc));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000173
Anders Carlsson714179b2009-08-02 19:07:59 +0000174 case tok::kw_dynamic_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000175 CastKind Kind = CK_Dependent;
John McCallf871d0c2010-08-07 06:22:56 +0000176 CXXCastPath BasePath;
Douglas Gregor9103bb22008-12-17 22:52:20 +0000177 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000178 CheckDynamicCast(*this, Ex, DestType, VK, OpRange, DestRange,
179 Kind, BasePath);
John McCallf871d0c2010-08-07 06:22:56 +0000180 return Owned(CXXDynamicCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000181 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000182 VK, Kind, Ex, &BasePath, DestTInfo,
John McCallf871d0c2010-08-07 06:22:56 +0000183 OpLoc));
Anders Carlsson714179b2009-08-02 19:07:59 +0000184 }
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000185 case tok::kw_reinterpret_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000186 CastKind Kind = CK_Dependent;
Douglas Gregor9103bb22008-12-17 22:52:20 +0000187 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000188 CheckReinterpretCast(*this, Ex, DestType, VK, OpRange, DestRange, Kind);
John McCallf871d0c2010-08-07 06:22:56 +0000189 return Owned(CXXReinterpretCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000190 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000191 VK, Kind, Ex, 0,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000192 DestTInfo, OpLoc));
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000193 }
Anders Carlssoncdb61972009-08-07 22:21:05 +0000194 case tok::kw_static_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000195 CastKind Kind = CK_Dependent;
John McCallf871d0c2010-08-07 06:22:56 +0000196 CXXCastPath BasePath;
Douglas Gregord6e44a32010-04-16 22:09:46 +0000197 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000198 CheckStaticCast(*this, Ex, DestType, VK, OpRange, Kind, BasePath);
Anders Carlsson0aebc812009-09-09 21:33:21 +0000199
John McCallf871d0c2010-08-07 06:22:56 +0000200 return Owned(CXXStaticCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000201 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000202 VK, Kind, Ex, &BasePath,
John McCallf871d0c2010-08-07 06:22:56 +0000203 DestTInfo, OpLoc));
Anders Carlssoncdb61972009-08-07 22:21:05 +0000204 }
Sebastian Redl26d85b12008-11-05 21:50:06 +0000205 }
206
Sebastian Redlf53597f2009-03-15 17:47:39 +0000207 return ExprError();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000208}
209
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000210/// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes,
211/// this removes one level of indirection from both types, provided that they're
212/// the same kind of pointer (plain or to-member). Unlike the Sema function,
213/// this one doesn't care if the two pointers-to-member don't point into the
214/// same class. This is because CastsAwayConstness doesn't care.
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000215static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) {
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000216 const PointerType *T1PtrType = T1->getAs<PointerType>(),
217 *T2PtrType = T2->getAs<PointerType>();
218 if (T1PtrType && T2PtrType) {
219 T1 = T1PtrType->getPointeeType();
220 T2 = T2PtrType->getPointeeType();
221 return true;
222 }
Fariborz Jahanian72a86592010-02-03 20:32:31 +0000223 const ObjCObjectPointerType *T1ObjCPtrType =
224 T1->getAs<ObjCObjectPointerType>(),
225 *T2ObjCPtrType =
226 T2->getAs<ObjCObjectPointerType>();
227 if (T1ObjCPtrType) {
228 if (T2ObjCPtrType) {
229 T1 = T1ObjCPtrType->getPointeeType();
230 T2 = T2ObjCPtrType->getPointeeType();
231 return true;
232 }
233 else if (T2PtrType) {
234 T1 = T1ObjCPtrType->getPointeeType();
235 T2 = T2PtrType->getPointeeType();
236 return true;
237 }
238 }
239 else if (T2ObjCPtrType) {
240 if (T1PtrType) {
241 T2 = T2ObjCPtrType->getPointeeType();
242 T1 = T1PtrType->getPointeeType();
243 return true;
244 }
245 }
246
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000247 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
248 *T2MPType = T2->getAs<MemberPointerType>();
249 if (T1MPType && T2MPType) {
250 T1 = T1MPType->getPointeeType();
251 T2 = T2MPType->getPointeeType();
252 return true;
253 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000254
255 const BlockPointerType *T1BPType = T1->getAs<BlockPointerType>(),
256 *T2BPType = T2->getAs<BlockPointerType>();
257 if (T1BPType && T2BPType) {
258 T1 = T1BPType->getPointeeType();
259 T2 = T2BPType->getPointeeType();
260 return true;
261 }
262
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000263 return false;
264}
265
Sebastian Redldb647282009-01-27 23:18:31 +0000266/// CastsAwayConstness - Check if the pointer conversion from SrcType to
267/// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by
268/// the cast checkers. Both arguments must denote pointer (possibly to member)
269/// types.
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000270static bool
Mike Stump1eb44332009-09-09 15:08:12 +0000271CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) {
Sebastian Redldb647282009-01-27 23:18:31 +0000272 // Casting away constness is defined in C++ 5.2.11p8 with reference to
273 // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since
274 // the rules are non-trivial. So first we construct Tcv *...cv* as described
275 // in C++ 5.2.11p8.
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000276 assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() ||
277 SrcType->isBlockPointerType()) &&
Sebastian Redldb647282009-01-27 23:18:31 +0000278 "Source type is not pointer or pointer to member.");
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000279 assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() ||
280 DestType->isBlockPointerType()) &&
Sebastian Redldb647282009-01-27 23:18:31 +0000281 "Destination type is not pointer or pointer to member.");
Sebastian Redl26d85b12008-11-05 21:50:06 +0000282
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000283 QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType),
284 UnwrappedDestType = Self.Context.getCanonicalType(DestType);
John McCall0953e762009-09-24 19:53:00 +0000285 llvm::SmallVector<Qualifiers, 8> cv1, cv2;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000286
287 // Find the qualifications.
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000288 while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
Anders Carlsson52647c62010-06-04 22:47:55 +0000289 Qualifiers SrcQuals;
290 Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals);
291 cv1.push_back(SrcQuals);
292
293 Qualifiers DestQuals;
294 Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals);
295 cv2.push_back(DestQuals);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000296 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000297 if (cv1.empty())
298 return false;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000299
300 // Construct void pointers with those qualifiers (in reverse order of
301 // unwrapping, of course).
Sebastian Redl37d6de32008-11-08 13:00:26 +0000302 QualType SrcConstruct = Self.Context.VoidTy;
303 QualType DestConstruct = Self.Context.VoidTy;
John McCall0953e762009-09-24 19:53:00 +0000304 ASTContext &Context = Self.Context;
305 for (llvm::SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(),
306 i2 = cv2.rbegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000307 i1 != cv1.rend(); ++i1, ++i2) {
John McCall0953e762009-09-24 19:53:00 +0000308 SrcConstruct
309 = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1));
310 DestConstruct
311 = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000312 }
313
314 // Test if they're compatible.
315 return SrcConstruct != DestConstruct &&
Sebastian Redl37d6de32008-11-08 13:00:26 +0000316 !Self.IsQualificationConversion(SrcConstruct, DestConstruct);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000317}
318
Sebastian Redl26d85b12008-11-05 21:50:06 +0000319/// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid.
320/// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime-
321/// checked downcasts in class hierarchies.
Anders Carlsson714179b2009-08-02 19:07:59 +0000322static void
Sebastian Redl37d6de32008-11-08 13:00:26 +0000323CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000324 ExprValueKind &VK, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000325 const SourceRange &DestRange, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000326 CXXCastPath &BasePath) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000327 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
Sebastian Redl37d6de32008-11-08 13:00:26 +0000328 DestType = Self.Context.getCanonicalType(DestType);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000329
330 // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type,
331 // or "pointer to cv void".
332
333 QualType DestPointee;
Ted Kremenek6217b802009-07-29 21:53:49 +0000334 const PointerType *DestPointer = DestType->getAs<PointerType>();
John McCallf89e55a2010-11-18 06:31:45 +0000335 const ReferenceType *DestReference = 0;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000336 if (DestPointer) {
337 DestPointee = DestPointer->getPointeeType();
John McCallf89e55a2010-11-18 06:31:45 +0000338 } else if ((DestReference = DestType->getAs<ReferenceType>())) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000339 DestPointee = DestReference->getPointeeType();
John McCallf89e55a2010-11-18 06:31:45 +0000340 VK = isa<LValueReferenceType>(DestReference) ? VK_LValue : VK_RValue;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000341 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000342 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
Chris Lattnerd1625842008-11-24 06:25:27 +0000343 << OrigDestType << DestRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000344 return;
345 }
346
Ted Kremenek6217b802009-07-29 21:53:49 +0000347 const RecordType *DestRecord = DestPointee->getAs<RecordType>();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000348 if (DestPointee->isVoidType()) {
349 assert(DestPointer && "Reference to void is not possible");
350 } else if (DestRecord) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000351 if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000352 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssonb7906612009-08-26 23:45:07 +0000353 << DestRange))
Sebastian Redl26d85b12008-11-05 21:50:06 +0000354 return;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000355 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000356 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattnerd1625842008-11-24 06:25:27 +0000357 << DestPointee.getUnqualifiedType() << DestRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000358 return;
359 }
360
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000361 // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
362 // complete class type, [...]. If T is an lvalue reference type, v shall be
363 // an lvalue of a complete class type, [...]. If T is an rvalue reference
364 // type, v shall be an expression having a complete effective class type,
365 // [...]
Sebastian Redl26d85b12008-11-05 21:50:06 +0000366
Sebastian Redl37d6de32008-11-08 13:00:26 +0000367 QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000368 QualType SrcPointee;
369 if (DestPointer) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000370 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000371 SrcPointee = SrcPointer->getPointeeType();
372 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000373 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
Chris Lattnerd1625842008-11-24 06:25:27 +0000374 << OrigSrcType << SrcExpr->getSourceRange();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000375 return;
376 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000377 } else if (DestReference->isLValueReferenceType()) {
Sebastian Redl37d6de32008-11-08 13:00:26 +0000378 if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000379 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000380 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000381 }
382 SrcPointee = SrcType;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000383 } else {
384 SrcPointee = SrcType;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000385 }
386
Ted Kremenek6217b802009-07-29 21:53:49 +0000387 const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000388 if (SrcRecord) {
Douglas Gregor86447ec2009-03-09 16:13:40 +0000389 if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000390 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssonb7906612009-08-26 23:45:07 +0000391 << SrcExpr->getSourceRange()))
Sebastian Redl26d85b12008-11-05 21:50:06 +0000392 return;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000393 } else {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000394 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattnerd1625842008-11-24 06:25:27 +0000395 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000396 return;
397 }
398
399 assert((DestPointer || DestReference) &&
400 "Bad destination non-ptr/ref slipped through.");
401 assert((DestRecord || DestPointee->isVoidType()) &&
402 "Bad destination pointee slipped through.");
403 assert(SrcRecord && "Bad source pointee slipped through.");
404
405 // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness.
406 if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000407 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000408 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000409 return;
410 }
411
412 // C++ 5.2.7p3: If the type of v is the same as the required result type,
413 // [except for cv].
414 if (DestRecord == SrcRecord) {
John McCall2de56d12010-08-25 11:45:40 +0000415 Kind = CK_NoOp;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000416 return;
417 }
418
419 // C++ 5.2.7p5
420 // Upcasts are resolved statically.
Sebastian Redl37d6de32008-11-08 13:00:26 +0000421 if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000422 if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
423 OpRange.getBegin(), OpRange,
424 &BasePath))
425 return;
426
John McCall2de56d12010-08-25 11:45:40 +0000427 Kind = CK_DerivedToBase;
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000428
429 // If we are casting to or through a virtual base class, we need a
430 // vtable.
431 if (Self.BasePathInvolvesVirtualBase(BasePath))
432 Self.MarkVTableUsed(OpRange.getBegin(),
433 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000434 return;
435 }
436
437 // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
Douglas Gregor952b0172010-02-11 01:04:33 +0000438 const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition();
Sebastian Redld93f0dd2008-11-06 15:59:35 +0000439 assert(SrcDecl && "Definition missing");
440 if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000441 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
Chris Lattnerd1625842008-11-24 06:25:27 +0000442 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redld93f0dd2008-11-06 15:59:35 +0000443 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000444 Self.MarkVTableUsed(OpRange.getBegin(),
445 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000446
447 // Done. Everything else is run-time checks.
John McCall2de56d12010-08-25 11:45:40 +0000448 Kind = CK_Dynamic;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000449}
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000450
451/// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
452/// Refer to C++ 5.2.11 for details. const_cast is typically used in code
453/// like this:
454/// const char *str = "literal";
455/// legacy_function(const_cast\<char*\>(str));
456void
John McCallf89e55a2010-11-18 06:31:45 +0000457CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType, ExprValueKind &VK,
Mike Stump1eb44332009-09-09 15:08:12 +0000458 const SourceRange &OpRange, const SourceRange &DestRange) {
John McCallf89e55a2010-11-18 06:31:45 +0000459 VK = Expr::getValueKindForType(DestType);
460 if (VK == VK_RValue)
Douglas Gregora873dfc2010-02-03 00:27:59 +0000461 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000462
463 unsigned msg = diag::err_bad_cxx_cast_generic;
464 if (TryConstCast(Self, SrcExpr, DestType, /*CStyle*/false, msg) != TC_Success
465 && msg != 0)
466 Self.Diag(OpRange.getBegin(), msg) << CT_Const
467 << SrcExpr->getType() << DestType << OpRange;
468}
469
470/// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is
471/// valid.
472/// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code
473/// like this:
474/// char *bytes = reinterpret_cast\<char*\>(int_ptr);
475void
476CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000477 ExprValueKind &VK, const SourceRange &OpRange,
478 const SourceRange &DestRange, CastKind &Kind) {
479 VK = Expr::getValueKindForType(DestType);
480 if (VK == VK_RValue)
Douglas Gregora873dfc2010-02-03 00:27:59 +0000481 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000482
483 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlsson3c31a392009-09-26 00:12:34 +0000484 if (TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange,
485 msg, Kind)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000486 != TC_Success && msg != 0)
Douglas Gregor8e960432010-11-08 03:40:48 +0000487 {
488 if (SrcExpr->getType() == Self.Context.OverloadTy)
489 {
490 //FIXME: &f<int>; is overloaded and resolvable
491 Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload)
492 << OverloadExpr::find(SrcExpr).Expression->getName()
493 << DestType << OpRange;
494 NoteAllOverloadCandidates(SrcExpr, Self);
495
496 }
497 else
498 Self.Diag(OpRange.getBegin(), msg) << CT_Reinterpret
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000499 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregor8e960432010-11-08 03:40:48 +0000500 }
501
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000502}
503
504
505/// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid.
506/// Refer to C++ 5.2.9 for details. Static casts are mostly used for making
507/// implicit conversions explicit and getting rid of data loss warnings.
508void
509CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000510 ExprValueKind &VK, const SourceRange &OpRange,
511 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000512 // This test is outside everything else because it's the only case where
513 // a non-lvalue-reference target type does not lead to decay.
514 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000515 if (DestType->isVoidType()) {
John McCall2de56d12010-08-25 11:45:40 +0000516 Kind = CK_ToVoid;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000517 return;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000518 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000519
John McCallf89e55a2010-11-18 06:31:45 +0000520 VK = Expr::getValueKindForType(DestType);
521 if (VK == VK_RValue && !DestType->isRecordType())
Douglas Gregora873dfc2010-02-03 00:27:59 +0000522 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000523
524 unsigned msg = diag::err_bad_cxx_cast_generic;
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000525 if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, msg,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000526 Kind, BasePath) != TC_Success && msg != 0)
Douglas Gregor8e960432010-11-08 03:40:48 +0000527 {
John McCalldaa8e4e2010-11-15 09:13:47 +0000528 if (SrcExpr->getType() == Self.Context.OverloadTy)
Douglas Gregor8e960432010-11-08 03:40:48 +0000529 {
530 OverloadExpr* oe = OverloadExpr::find(SrcExpr).Expression;
531 Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
532 << oe->getName() << DestType << OpRange << oe->getQualifierRange();
533 NoteAllOverloadCandidates(SrcExpr, Self);
534 }
535 else
536 Self.Diag(OpRange.getBegin(), msg) << CT_Static
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000537 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregor8e960432010-11-08 03:40:48 +0000538 }
John McCalle2b76882010-11-16 05:46:29 +0000539 else if (Kind == CK_BitCast)
John McCallb7f4ffe2010-08-12 21:44:57 +0000540 Self.CheckCastAlign(SrcExpr, DestType, OpRange);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000541}
542
543/// TryStaticCast - Check if a static cast can be performed, and do so if
544/// possible. If @p CStyle, ignore access restrictions on hierarchy casting
545/// and casting away constness.
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000546static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000547 QualType DestType, bool CStyle,
Anders Carlssoncb3c3082009-09-01 20:52:42 +0000548 const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000549 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000550 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000551 // The order the tests is not entirely arbitrary. There is one conversion
552 // that can be handled in two different ways. Given:
553 // struct A {};
554 // struct B : public A {
555 // B(); B(const A&);
556 // };
557 // const A &a = B();
558 // the cast static_cast<const B&>(a) could be seen as either a static
559 // reference downcast, or an explicit invocation of the user-defined
560 // conversion using B's conversion constructor.
561 // DR 427 specifies that the downcast is to be applied here.
562
563 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
564 // Done outside this function.
565
566 TryCastResult tcr;
567
568 // C++ 5.2.9p5, reference downcast.
569 // See the function for details.
570 // DR 427 specifies that this is to be applied before paragraph 2.
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000571 tcr = TryStaticReferenceDowncast(Self, SrcExpr, DestType, CStyle, OpRange,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000572 msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000573 if (tcr != TC_NotApplicable)
574 return tcr;
575
576 // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
577 // reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
578 tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, msg);
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000579 if (tcr != TC_NotApplicable) {
John McCall2de56d12010-08-25 11:45:40 +0000580 Kind = CK_NoOp;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000581 return tcr;
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000582 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000583
584 // C++ 5.2.9p2: An expression e can be explicitly converted to a type T
585 // [...] if the declaration "T t(e);" is well-formed, [...].
Fariborz Jahaniane9f42082009-08-26 18:55:36 +0000586 tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CStyle, OpRange, msg,
Douglas Gregord6e44a32010-04-16 22:09:46 +0000587 Kind);
Anders Carlsson3c31a392009-09-26 00:12:34 +0000588 if (tcr != TC_NotApplicable)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000589 return tcr;
Anders Carlsson0aebc812009-09-09 21:33:21 +0000590
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000591 // C++ 5.2.9p6: May apply the reverse of any standard conversion, except
592 // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
593 // conversions, subject to further restrictions.
594 // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
595 // of qualification conversions impossible.
596 // In the CStyle case, the earlier attempt to const_cast should have taken
597 // care of reverse qualification conversions.
598
599 QualType OrigSrcType = SrcExpr->getType();
600
601 QualType SrcType = Self.Context.getCanonicalType(SrcExpr->getType());
602
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000603 // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly
604 // converted to an integral type.
605 if (Self.getLangOptions().CPlusPlus0x && SrcType->isEnumeralType()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000606 assert(SrcType->getAs<EnumType>()->getDecl()->isScoped());
607 if (DestType->isBooleanType()) {
608 Kind = CK_IntegralToBoolean;
609 return TC_Success;
610 } else if (DestType->isIntegralType(Self.Context)) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000611 Kind = CK_IntegralCast;
612 return TC_Success;
613 }
614 }
615
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000616 // Reverse integral promotion/conversion. All such conversions are themselves
617 // again integral promotions or conversions and are thus already handled by
618 // p2 (TryDirectInitialization above).
619 // (Note: any data loss warnings should be suppressed.)
620 // The exception is the reverse of enum->integer, i.e. integer->enum (and
621 // enum->enum). See also C++ 5.2.9p7.
622 // The same goes for reverse floating point promotion/conversion and
623 // floating-integral conversions. Again, only floating->enum is relevant.
624 if (DestType->isEnumeralType()) {
625 if (SrcType->isComplexType() || SrcType->isVectorType()) {
626 // Fall through - these cannot be converted.
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000627 } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
John McCall2de56d12010-08-25 11:45:40 +0000628 Kind = CK_IntegralCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000629 return TC_Success;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000630 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000631 }
632
633 // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
634 // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000635 tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000636 Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000637 if (tcr != TC_NotApplicable)
638 return tcr;
639
640 // Reverse member pointer conversion. C++ 4.11 specifies member pointer
641 // conversion. C++ 5.2.9p9 has additional information.
642 // DR54's access restrictions apply here also.
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000643 tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
Anders Carlssoncee22422010-04-24 19:22:20 +0000644 OpRange, msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000645 if (tcr != TC_NotApplicable)
646 return tcr;
647
648 // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
649 // void*. C++ 5.2.9p10 specifies additional restrictions, which really is
650 // just the usual constness stuff.
Ted Kremenek6217b802009-07-29 21:53:49 +0000651 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000652 QualType SrcPointee = SrcPointer->getPointeeType();
653 if (SrcPointee->isVoidType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000654 if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000655 QualType DestPointee = DestPointer->getPointeeType();
656 if (DestPointee->isIncompleteOrObjectType()) {
657 // This is definitely the intended conversion, but it might fail due
658 // to a const violation.
659 if (!CStyle && !DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
660 msg = diag::err_bad_cxx_cast_const_away;
661 return TC_Failed;
662 }
John McCall2de56d12010-08-25 11:45:40 +0000663 Kind = CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000664 return TC_Success;
665 }
666 }
Fariborz Jahanian2f6c5502010-05-10 23:46:53 +0000667 else if (DestType->isObjCObjectPointerType()) {
668 // allow both c-style cast and static_cast of objective-c pointers as
669 // they are pervasive.
John McCall2de56d12010-08-25 11:45:40 +0000670 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +0000671 return TC_Success;
672 }
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000673 else if (CStyle && DestType->isBlockPointerType()) {
674 // allow c-style cast of void * to block pointers.
John McCall2de56d12010-08-25 11:45:40 +0000675 Kind = CK_AnyPointerToBlockPointerCast;
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000676 return TC_Success;
677 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000678 }
679 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000680 // Allow arbitray objective-c pointer conversion with static casts.
681 if (SrcType->isObjCObjectPointerType() &&
John McCalldaa8e4e2010-11-15 09:13:47 +0000682 DestType->isObjCObjectPointerType()) {
683 Kind = CK_BitCast;
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000684 return TC_Success;
John McCalldaa8e4e2010-11-15 09:13:47 +0000685 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000686
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000687 // We tried everything. Everything! Nothing works! :-(
688 return TC_NotApplicable;
689}
690
691/// Tests whether a conversion according to N2844 is valid.
692TryCastResult
693TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
Mike Stump1eb44332009-09-09 15:08:12 +0000694 unsigned &msg) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000695 // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
696 // reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
Ted Kremenek6217b802009-07-29 21:53:49 +0000697 const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000698 if (!R)
699 return TC_NotApplicable;
700
701 if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid)
702 return TC_NotApplicable;
703
704 // Because we try the reference downcast before this function, from now on
705 // this is the only cast possibility, so we issue an error if we fail now.
706 // FIXME: Should allow casting away constness if CStyle.
707 bool DerivedToBase;
Douglas Gregor569c3162010-08-07 11:51:51 +0000708 bool ObjCConversion;
Douglas Gregor393896f2009-11-05 13:06:35 +0000709 if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
710 SrcExpr->getType(), R->getPointeeType(),
Douglas Gregor569c3162010-08-07 11:51:51 +0000711 DerivedToBase, ObjCConversion) <
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000712 Sema::Ref_Compatible_With_Added_Qualification) {
713 msg = diag::err_bad_lvalue_to_rvalue_cast;
714 return TC_Failed;
715 }
716
Douglas Gregorf0e0b172010-03-25 00:20:38 +0000717 // FIXME: We should probably have an AST node for lvalue-to-rvalue
718 // conversions.
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000719 return TC_Success;
720}
721
722/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
723TryCastResult
724TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
725 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000726 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000727 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000728 // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
729 // cast to type "reference to cv2 D", where D is a class derived from B,
730 // if a valid standard conversion from "pointer to D" to "pointer to B"
731 // exists, cv2 >= cv1, and B is not a virtual base class of D.
732 // In addition, DR54 clarifies that the base must be accessible in the
733 // current context. Although the wording of DR54 only applies to the pointer
734 // variant of this rule, the intent is clearly for it to apply to the this
735 // conversion as well.
736
Ted Kremenek6217b802009-07-29 21:53:49 +0000737 const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000738 if (!DestReference) {
739 return TC_NotApplicable;
740 }
741 bool RValueRef = DestReference->isRValueReferenceType();
742 if (!RValueRef && SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
743 // We know the left side is an lvalue reference, so we can suggest a reason.
744 msg = diag::err_bad_cxx_cast_rvalue;
745 return TC_NotApplicable;
746 }
747
748 QualType DestPointee = DestReference->getPointeeType();
749
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000750 return TryStaticDowncast(Self,
751 Self.Context.getCanonicalType(SrcExpr->getType()),
752 Self.Context.getCanonicalType(DestPointee), CStyle,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000753 OpRange, SrcExpr->getType(), DestType, msg, Kind,
754 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000755}
756
757/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
758TryCastResult
759TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
Mike Stump1eb44332009-09-09 15:08:12 +0000760 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000761 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000762 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000763 // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
764 // type, can be converted to an rvalue of type "pointer to cv2 D", where D
765 // is a class derived from B, if a valid standard conversion from "pointer
766 // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
767 // class of D.
768 // In addition, DR54 clarifies that the base must be accessible in the
769 // current context.
770
Ted Kremenek6217b802009-07-29 21:53:49 +0000771 const PointerType *DestPointer = DestType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000772 if (!DestPointer) {
773 return TC_NotApplicable;
774 }
775
Ted Kremenek6217b802009-07-29 21:53:49 +0000776 const PointerType *SrcPointer = SrcType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000777 if (!SrcPointer) {
778 msg = diag::err_bad_static_cast_pointer_nonpointer;
779 return TC_NotApplicable;
780 }
781
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000782 return TryStaticDowncast(Self,
783 Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
784 Self.Context.getCanonicalType(DestPointer->getPointeeType()),
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000785 CStyle, OpRange, SrcType, DestType, msg, Kind,
786 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000787}
788
789/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
790/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000791/// DestType is possible and allowed.
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000792TryCastResult
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000793TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000794 bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000795 QualType OrigDestType, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000796 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000797 // We can only work with complete types. But don't complain if it doesn't work
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000798 if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) ||
799 Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0)))
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000800 return TC_NotApplicable;
801
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000802 // Downcast can only happen in class hierarchies, so we need classes.
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000803 if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000804 return TC_NotApplicable;
805 }
806
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000807 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000808 /*DetectVirtual=*/true);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000809 if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
810 return TC_NotApplicable;
811 }
812
813 // Target type does derive from source type. Now we're serious. If an error
814 // appears now, it's not ignored.
815 // This may not be entirely in line with the standard. Take for example:
816 // struct A {};
817 // struct B : virtual A {
818 // B(A&);
819 // };
Mike Stump1eb44332009-09-09 15:08:12 +0000820 //
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000821 // void f()
822 // {
823 // (void)static_cast<const B&>(*((A*)0));
824 // }
825 // As far as the standard is concerned, p5 does not apply (A is virtual), so
826 // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
827 // However, both GCC and Comeau reject this example, and accepting it would
828 // mean more complex code if we're to preserve the nice error message.
829 // FIXME: Being 100% compliant here would be nice to have.
830
831 // Must preserve cv, as always, unless we're in C-style mode.
832 if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
833 msg = diag::err_bad_cxx_cast_const_away;
834 return TC_Failed;
835 }
836
837 if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
838 // This code is analoguous to that in CheckDerivedToBaseConversion, except
839 // that it builds the paths in reverse order.
840 // To sum up: record all paths to the base and build a nice string from
841 // them. Use it to spice up the error message.
842 if (!Paths.isRecordingPaths()) {
843 Paths.clear();
844 Paths.setRecordingPaths(true);
845 Self.IsDerivedFrom(DestType, SrcType, Paths);
846 }
847 std::string PathDisplayStr;
848 std::set<unsigned> DisplayedPaths;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000849 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000850 PI != PE; ++PI) {
851 if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
852 // We haven't displayed a path to this particular base
853 // class subobject yet.
854 PathDisplayStr += "\n ";
Douglas Gregora8f32e02009-10-06 17:59:45 +0000855 for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
856 EE = PI->rend();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000857 EI != EE; ++EI)
858 PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000859 PathDisplayStr += QualType(DestType).getAsString();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000860 }
861 }
862
863 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000864 << QualType(SrcType).getUnqualifiedType()
865 << QualType(DestType).getUnqualifiedType()
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000866 << PathDisplayStr << OpRange;
867 msg = 0;
868 return TC_Failed;
869 }
870
871 if (Paths.getDetectedVirtual() != 0) {
872 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
873 Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
874 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
875 msg = 0;
876 return TC_Failed;
877 }
878
John McCall6b2accb2010-02-10 09:31:12 +0000879 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
John McCall6b2accb2010-02-10 09:31:12 +0000880 SrcType, DestType,
John McCall58e6f342010-03-16 05:22:47 +0000881 Paths.front(),
882 diag::err_downcast_from_inaccessible_base)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000883 msg = 0;
884 return TC_Failed;
885 }
886
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000887 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +0000888 Kind = CK_BaseToDerived;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000889 return TC_Success;
890}
891
892/// TryStaticMemberPointerUpcast - Tests whether a conversion according to
893/// C++ 5.2.9p9 is valid:
894///
895/// An rvalue of type "pointer to member of D of type cv1 T" can be
896/// converted to an rvalue of type "pointer to member of B of type cv2 T",
897/// where B is a base class of D [...].
898///
899TryCastResult
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000900TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr, QualType SrcType,
901 QualType DestType, bool CStyle,
902 const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000903 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000904 CXXCastPath &BasePath) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000905 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000906 if (!DestMemPtr)
907 return TC_NotApplicable;
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000908
909 bool WasOverloadedFunction = false;
John McCall6bb80172010-03-30 21:47:33 +0000910 DeclAccessPair FoundOverload;
Douglas Gregor1a8cf732010-04-14 23:11:21 +0000911 if (SrcExpr->getType() == Self.Context.OverloadTy) {
912 if (FunctionDecl *Fn
913 = Self.ResolveAddressOfOverloadedFunction(SrcExpr, DestType, false,
914 FoundOverload)) {
915 CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
916 SrcType = Self.Context.getMemberPointerType(Fn->getType(),
917 Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
918 WasOverloadedFunction = true;
919 }
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000920 }
Douglas Gregor1a8cf732010-04-14 23:11:21 +0000921
Ted Kremenek6217b802009-07-29 21:53:49 +0000922 const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000923 if (!SrcMemPtr) {
924 msg = diag::err_bad_static_cast_member_pointer_nonmp;
925 return TC_NotApplicable;
926 }
927
928 // T == T, modulo cv
Douglas Gregora4923eb2009-11-16 21:35:15 +0000929 if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
930 DestMemPtr->getPointeeType()))
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000931 return TC_NotApplicable;
932
933 // B base of D
934 QualType SrcClass(SrcMemPtr->getClass(), 0);
935 QualType DestClass(DestMemPtr->getClass(), 0);
Anders Carlssoncee22422010-04-24 19:22:20 +0000936 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000937 /*DetectVirtual=*/true);
938 if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
939 return TC_NotApplicable;
940 }
941
942 // 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 +0000943 if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000944 Paths.clear();
945 Paths.setRecordingPaths(true);
946 bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
947 assert(StillOkay);
948 StillOkay = StillOkay;
949 std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
950 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
951 << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
952 msg = 0;
953 return TC_Failed;
954 }
955
956 if (const RecordType *VBase = Paths.getDetectedVirtual()) {
957 Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
958 << SrcClass << DestClass << QualType(VBase, 0) << OpRange;
959 msg = 0;
960 return TC_Failed;
961 }
962
John McCall6b2accb2010-02-10 09:31:12 +0000963 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
Eli Friedman0fab8cd2010-07-23 19:25:41 +0000964 DestClass, SrcClass,
John McCall58e6f342010-03-16 05:22:47 +0000965 Paths.front(),
966 diag::err_upcast_to_inaccessible_base)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000967 msg = 0;
968 return TC_Failed;
969 }
970
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000971 if (WasOverloadedFunction) {
972 // Resolve the address of the overloaded function again, this time
973 // allowing complaints if something goes wrong.
974 FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr,
975 DestType,
John McCall6bb80172010-03-30 21:47:33 +0000976 true,
977 FoundOverload);
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000978 if (!Fn) {
979 msg = 0;
980 return TC_Failed;
981 }
982
John McCall6bb80172010-03-30 21:47:33 +0000983 SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000984 if (!SrcExpr) {
985 msg = 0;
986 return TC_Failed;
987 }
988 }
989
Anders Carlssoncee22422010-04-24 19:22:20 +0000990 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +0000991 Kind = CK_DerivedToBaseMemberPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000992 return TC_Success;
993}
994
995/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
996/// is valid:
997///
998/// An expression e can be explicitly converted to a type T using a
999/// @c static_cast if the declaration "T t(e);" is well-formed [...].
1000TryCastResult
Sebastian Redla82e4ae2009-11-14 21:15:49 +00001001TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
Fariborz Jahaniane9f42082009-08-26 18:55:36 +00001002 bool CStyle, const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001003 CastKind &Kind) {
Anders Carlssond851b372009-09-07 18:25:47 +00001004 if (DestType->isRecordType()) {
1005 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
1006 diag::err_bad_dynamic_cast_incomplete)) {
1007 msg = 0;
1008 return TC_Failed;
1009 }
1010 }
Douglas Gregord6e44a32010-04-16 22:09:46 +00001011
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001012 InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
1013 InitializationKind InitKind
Douglas Gregord6e44a32010-04-16 22:09:46 +00001014 = InitializationKind::CreateCast(/*FIXME:*/OpRange,
1015 CStyle);
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001016 InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExpr, 1);
Douglas Gregor8e960432010-11-08 03:40:48 +00001017
1018 // At this point of CheckStaticCast, if the destination is a reference,
1019 // or the expression is an overload expression this has to work.
1020 // There is no other way that works.
1021 // On the other hand, if we're checking a C-style cast, we've still got
1022 // the reinterpret_cast way.
1023
Douglas Gregord6e44a32010-04-16 22:09:46 +00001024 if (InitSeq.getKind() == InitializationSequence::FailedSequence &&
Douglas Gregor8e960432010-11-08 03:40:48 +00001025 (CStyle || !DestType->isReferenceType()))
Anders Carlsson3c31a392009-09-26 00:12:34 +00001026 return TC_NotApplicable;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001027
John McCall60d7b3a2010-08-24 06:29:42 +00001028 ExprResult Result
John McCallf312b1e2010-08-26 23:41:50 +00001029 = InitSeq.Perform(Self, Entity, InitKind, MultiExprArg(Self, &SrcExpr, 1));
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001030 if (Result.isInvalid()) {
1031 msg = 0;
1032 return TC_Failed;
1033 }
1034
Douglas Gregord6e44a32010-04-16 22:09:46 +00001035 if (InitSeq.isConstructorInitialization())
John McCall2de56d12010-08-25 11:45:40 +00001036 Kind = CK_ConstructorConversion;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001037 else
John McCall2de56d12010-08-25 11:45:40 +00001038 Kind = CK_NoOp;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001039
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001040 SrcExpr = Result.takeAs<Expr>();
1041 return TC_Success;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001042}
1043
1044/// TryConstCast - See if a const_cast from source to destination is allowed,
1045/// and perform it if it is.
1046static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
1047 bool CStyle, unsigned &msg) {
1048 DestType = Self.Context.getCanonicalType(DestType);
1049 QualType SrcType = SrcExpr->getType();
1050 if (const LValueReferenceType *DestTypeTmp =
Ted Kremenek6217b802009-07-29 21:53:49 +00001051 DestType->getAs<LValueReferenceType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001052 if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
1053 // Cannot const_cast non-lvalue to lvalue reference type. But if this
1054 // is C-style, static_cast might find a way, so we simply suggest a
1055 // message and tell the parent to keep searching.
1056 msg = diag::err_bad_cxx_cast_rvalue;
1057 return TC_NotApplicable;
1058 }
1059
1060 // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
1061 // [...] if a pointer to T1 can be [cast] to the type pointer to T2.
1062 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1063 SrcType = Self.Context.getPointerType(SrcType);
1064 }
1065
1066 // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
1067 // the rules for const_cast are the same as those used for pointers.
1068
John McCalld425d2b2010-05-18 09:35:29 +00001069 if (!DestType->isPointerType() &&
1070 !DestType->isMemberPointerType() &&
1071 !DestType->isObjCObjectPointerType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001072 // Cannot cast to non-pointer, non-reference type. Note that, if DestType
1073 // was a reference type, we converted it to a pointer above.
1074 // The status of rvalue references isn't entirely clear, but it looks like
1075 // conversion to them is simply invalid.
1076 // C++ 5.2.11p3: For two pointer types [...]
1077 if (!CStyle)
1078 msg = diag::err_bad_const_cast_dest;
1079 return TC_NotApplicable;
1080 }
1081 if (DestType->isFunctionPointerType() ||
1082 DestType->isMemberFunctionPointerType()) {
1083 // Cannot cast direct function pointers.
1084 // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
1085 // T is the ultimate pointee of source and target type.
1086 if (!CStyle)
1087 msg = diag::err_bad_const_cast_dest;
1088 return TC_NotApplicable;
1089 }
1090 SrcType = Self.Context.getCanonicalType(SrcType);
1091
1092 // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
1093 // completely equal.
1094 // FIXME: const_cast should probably not be able to convert between pointers
1095 // to different address spaces.
1096 // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
1097 // in multi-level pointers may change, but the level count must be the same,
1098 // as must be the final pointee type.
1099 while (SrcType != DestType &&
Douglas Gregor5a57efd2010-06-09 03:53:18 +00001100 Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) {
Chandler Carruth595e2902009-12-29 08:05:19 +00001101 Qualifiers Quals;
1102 SrcType = Self.Context.getUnqualifiedArrayType(SrcType, Quals);
1103 DestType = Self.Context.getUnqualifiedArrayType(DestType, Quals);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001104 }
1105
1106 // Since we're dealing in canonical types, the remainder must be the same.
1107 if (SrcType != DestType)
1108 return TC_NotApplicable;
1109
1110 return TC_Success;
1111}
1112
Douglas Gregor8e960432010-11-08 03:40:48 +00001113
1114static void NoteAllOverloadCandidates(Expr* const Expr, Sema& sema)
1115{
1116
1117 assert(Expr->getType() == sema.Context.OverloadTy);
1118
1119 OverloadExpr::FindResult Ovl = OverloadExpr::find(Expr);
1120 OverloadExpr *const OvlExpr = Ovl.Expression;
1121
1122 for (UnresolvedSetIterator it = OvlExpr->decls_begin(),
1123 end = OvlExpr->decls_end(); it != end; ++it) {
1124 if ( FunctionTemplateDecl *ftd =
1125 dyn_cast<FunctionTemplateDecl>((*it)->getUnderlyingDecl()) )
1126 {
1127 sema.NoteOverloadCandidate(ftd->getTemplatedDecl());
1128 }
1129 else if ( FunctionDecl *f =
1130 dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl()) )
1131 {
1132 sema.NoteOverloadCandidate(f);
1133 }
1134 }
1135}
1136
1137
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001138static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
1139 QualType DestType, bool CStyle,
1140 const SourceRange &OpRange,
Anders Carlsson3c31a392009-09-26 00:12:34 +00001141 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001142 CastKind &Kind) {
Douglas Gregore39a3892010-07-13 23:17:26 +00001143 bool IsLValueCast = false;
1144
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001145 DestType = Self.Context.getCanonicalType(DestType);
1146 QualType SrcType = SrcExpr->getType();
Douglas Gregor8e960432010-11-08 03:40:48 +00001147
1148 // Is the source an overloaded name? (i.e. &foo)
1149 // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5)
John McCall404cd162010-11-13 01:35:44 +00001150 if (SrcType == Self.Context.OverloadTy)
Douglas Gregor8e960432010-11-08 03:40:48 +00001151 return TC_NotApplicable;
1152
Ted Kremenek6217b802009-07-29 21:53:49 +00001153 if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001154 bool LValue = DestTypeTmp->isLValueReferenceType();
1155 if (LValue && SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
1156 // Cannot cast non-lvalue to reference type. See the similar comment in
1157 // const_cast.
1158 msg = diag::err_bad_cxx_cast_rvalue;
1159 return TC_NotApplicable;
1160 }
1161
1162 // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
1163 // same effect as the conversion *reinterpret_cast<T*>(&x) with the
1164 // built-in & and * operators.
1165 // This code does this transformation for the checked types.
1166 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1167 SrcType = Self.Context.getPointerType(SrcType);
Douglas Gregor8e960432010-11-08 03:40:48 +00001168
Douglas Gregore39a3892010-07-13 23:17:26 +00001169 IsLValueCast = true;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001170 }
1171
1172 // Canonicalize source for comparison.
1173 SrcType = Self.Context.getCanonicalType(SrcType);
1174
Ted Kremenek6217b802009-07-29 21:53:49 +00001175 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
1176 *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001177 if (DestMemPtr && SrcMemPtr) {
1178 // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
1179 // can be explicitly converted to an rvalue of type "pointer to member
1180 // of Y of type T2" if T1 and T2 are both function types or both object
1181 // types.
1182 if (DestMemPtr->getPointeeType()->isFunctionType() !=
1183 SrcMemPtr->getPointeeType()->isFunctionType())
1184 return TC_NotApplicable;
1185
1186 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
1187 // constness.
1188 // A reinterpret_cast followed by a const_cast can, though, so in C-style,
1189 // we accept it.
1190 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1191 msg = diag::err_bad_cxx_cast_const_away;
1192 return TC_Failed;
1193 }
1194
Charles Davisf231df32010-08-16 05:30:44 +00001195 // Don't allow casting between member pointers of different sizes.
1196 if (Self.Context.getTypeSize(DestMemPtr) !=
1197 Self.Context.getTypeSize(SrcMemPtr)) {
1198 msg = diag::err_bad_cxx_cast_member_pointer_size;
1199 return TC_Failed;
1200 }
1201
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001202 // A valid member pointer cast.
John McCall2de56d12010-08-25 11:45:40 +00001203 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001204 return TC_Success;
1205 }
1206
1207 // See below for the enumeral issue.
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001208 if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001209 // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
1210 // type large enough to hold it. A value of std::nullptr_t can be
1211 // converted to an integral type; the conversion has the same meaning
1212 // and validity as a conversion of (void*)0 to the integral type.
1213 if (Self.Context.getTypeSize(SrcType) >
1214 Self.Context.getTypeSize(DestType)) {
1215 msg = diag::err_bad_reinterpret_cast_small_int;
1216 return TC_Failed;
1217 }
John McCall2de56d12010-08-25 11:45:40 +00001218 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001219 return TC_Success;
1220 }
1221
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001222 bool destIsVector = DestType->isVectorType();
1223 bool srcIsVector = SrcType->isVectorType();
1224 if (srcIsVector || destIsVector) {
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001225 // FIXME: Should this also apply to floating point types?
1226 bool srcIsScalar = SrcType->isIntegralType(Self.Context);
1227 bool destIsScalar = DestType->isIntegralType(Self.Context);
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001228
1229 // Check if this is a cast between a vector and something else.
1230 if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
1231 !(srcIsVector && destIsVector))
1232 return TC_NotApplicable;
1233
1234 // If both types have the same size, we can successfully cast.
Douglas Gregorf2a55392009-12-22 22:47:22 +00001235 if (Self.Context.getTypeSize(SrcType)
1236 == Self.Context.getTypeSize(DestType)) {
John McCall2de56d12010-08-25 11:45:40 +00001237 Kind = CK_BitCast;
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001238 return TC_Success;
Douglas Gregorf2a55392009-12-22 22:47:22 +00001239 }
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001240
1241 if (destIsScalar)
1242 msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
1243 else if (srcIsScalar)
1244 msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
1245 else
1246 msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
1247
1248 return TC_Failed;
1249 }
1250
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001251 bool destIsPtr = DestType->isAnyPointerType() ||
1252 DestType->isBlockPointerType();
1253 bool srcIsPtr = SrcType->isAnyPointerType() ||
1254 SrcType->isBlockPointerType();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001255 if (!destIsPtr && !srcIsPtr) {
1256 // Except for std::nullptr_t->integer and lvalue->reference, which are
1257 // handled above, at least one of the two arguments must be a pointer.
1258 return TC_NotApplicable;
1259 }
1260
1261 if (SrcType == DestType) {
1262 // C++ 5.2.10p2 has a note that mentions that, subject to all other
1263 // restrictions, a cast to the same type is allowed. The intent is not
1264 // entirely clear here, since all other paragraphs explicitly forbid casts
1265 // to the same type. However, the behavior of compilers is pretty consistent
1266 // on this point: allow same-type conversion if the involved types are
1267 // pointers, disallow otherwise.
John McCall2de56d12010-08-25 11:45:40 +00001268 Kind = CK_NoOp;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001269 return TC_Success;
1270 }
1271
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001272 if (DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001273 assert(srcIsPtr && "One type must be a pointer");
1274 // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
1275 // type large enough to hold it.
1276 if (Self.Context.getTypeSize(SrcType) >
1277 Self.Context.getTypeSize(DestType)) {
1278 msg = diag::err_bad_reinterpret_cast_small_int;
1279 return TC_Failed;
1280 }
John McCall2de56d12010-08-25 11:45:40 +00001281 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001282 return TC_Success;
1283 }
1284
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001285 if (SrcType->isIntegralOrEnumerationType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001286 assert(destIsPtr && "One type must be a pointer");
1287 // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
1288 // converted to a pointer.
John McCall404cd162010-11-13 01:35:44 +00001289 // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not
1290 // necessarily converted to a null pointer value.]
John McCall2de56d12010-08-25 11:45:40 +00001291 Kind = CK_IntegralToPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001292 return TC_Success;
1293 }
1294
1295 if (!destIsPtr || !srcIsPtr) {
1296 // With the valid non-pointer conversions out of the way, we can be even
1297 // more stringent.
1298 return TC_NotApplicable;
1299 }
1300
1301 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
1302 // The C-style cast operator can.
1303 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1304 msg = diag::err_bad_cxx_cast_const_away;
1305 return TC_Failed;
1306 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001307
1308 // Cannot convert between block pointers and Objective-C object pointers.
1309 if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) ||
1310 (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType()))
1311 return TC_NotApplicable;
1312
1313 // Any pointer can be cast to an Objective-C pointer type with a C-style
1314 // cast.
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001315 if (CStyle && DestType->isObjCObjectPointerType()) {
John McCall2de56d12010-08-25 11:45:40 +00001316 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001317 return TC_Success;
1318 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001319
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001320 // Not casting away constness, so the only remaining check is for compatible
1321 // pointer categories.
John McCall2de56d12010-08-25 11:45:40 +00001322 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001323
1324 if (SrcType->isFunctionPointerType()) {
1325 if (DestType->isFunctionPointerType()) {
1326 // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
1327 // a pointer to a function of a different type.
1328 return TC_Success;
1329 }
1330
1331 // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
1332 // an object type or vice versa is conditionally-supported.
1333 // Compilers support it in C++03 too, though, because it's necessary for
1334 // casting the return value of dlsym() and GetProcAddress().
1335 // FIXME: Conditionally-supported behavior should be configurable in the
1336 // TargetInfo or similar.
1337 if (!Self.getLangOptions().CPlusPlus0x)
1338 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1339 return TC_Success;
1340 }
1341
1342 if (DestType->isFunctionPointerType()) {
1343 // See above.
1344 if (!Self.getLangOptions().CPlusPlus0x)
1345 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1346 return TC_Success;
1347 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001348
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001349 // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
1350 // a pointer to an object of different type.
1351 // Void pointers are not specified, but supported by every compiler out there.
1352 // So we finish by allowing everything that remains - it's got to be two
1353 // object pointers.
1354 return TC_Success;
1355}
1356
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001357bool
John McCallf89e55a2010-11-18 06:31:45 +00001358Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, ExprValueKind &VK,
1359 Expr *&CastExpr, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +00001360 CXXCastPath &BasePath,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001361 bool FunctionalStyle) {
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001362 if (CastExpr->isBoundMemberFunction(Context))
1363 return Diag(CastExpr->getLocStart(),
1364 diag::err_invalid_use_of_bound_member_func)
1365 << CastExpr->getSourceRange();
1366
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001367 // This test is outside everything else because it's the only case where
1368 // a non-lvalue-reference target type does not lead to decay.
1369 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Anders Carlssonbb378cb2009-10-18 20:31:03 +00001370 if (CastTy->isVoidType()) {
John McCall2de56d12010-08-25 11:45:40 +00001371 Kind = CK_ToVoid;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001372 return false;
Anders Carlssonbb378cb2009-10-18 20:31:03 +00001373 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001374
1375 // If the type is dependent, we won't do any other semantic analysis now.
John McCalldaa8e4e2010-11-15 09:13:47 +00001376 if (CastTy->isDependentType() || CastExpr->isTypeDependent()) {
1377 Kind = CK_Dependent;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001378 return false;
John McCalldaa8e4e2010-11-15 09:13:47 +00001379 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001380
John McCallf89e55a2010-11-18 06:31:45 +00001381 VK = Expr::getValueKindForType(CastTy);
1382 if (VK == VK_RValue && !CastTy->isRecordType())
Douglas Gregora873dfc2010-02-03 00:27:59 +00001383 DefaultFunctionArrayLvalueConversion(CastExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001384
1385 // C++ [expr.cast]p5: The conversions performed by
1386 // - a const_cast,
1387 // - a static_cast,
1388 // - a static_cast followed by a const_cast,
1389 // - a reinterpret_cast, or
1390 // - a reinterpret_cast followed by a const_cast,
1391 // can be performed using the cast notation of explicit type conversion.
1392 // [...] If a conversion can be interpreted in more than one of the ways
1393 // listed above, the interpretation that appears first in the list is used,
1394 // even if a cast resulting from that interpretation is ill-formed.
1395 // In plain language, this means trying a const_cast ...
1396 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlssoncb3c3082009-09-01 20:52:42 +00001397 TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,
1398 msg);
Anders Carlssonda921fd2009-10-19 18:14:28 +00001399 if (tcr == TC_Success)
John McCall2de56d12010-08-25 11:45:40 +00001400 Kind = CK_NoOp;
Anders Carlssonda921fd2009-10-19 18:14:28 +00001401
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001402 if (tcr == TC_NotApplicable) {
1403 // ... or if that is not possible, a static_cast, ignoring const, ...
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001404 tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg, Kind,
1405 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001406 if (tcr == TC_NotApplicable) {
1407 // ... and finally a reinterpret_cast, ignoring const.
Anders Carlsson3c31a392009-09-26 00:12:34 +00001408 tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg,
1409 Kind);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001410 }
1411 }
1412
Nick Lewycky43328e92010-11-09 00:19:31 +00001413 if (tcr != TC_Success && msg != 0) {
1414 if (CastExpr->getType() == Context.OverloadTy) {
Douglas Gregor8e960432010-11-08 03:40:48 +00001415 DeclAccessPair Found;
Nick Lewycky43328e92010-11-09 00:19:31 +00001416 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(CastExpr,
Douglas Gregor8e960432010-11-08 03:40:48 +00001417 CastTy,
1418 /* Complain */ true,
1419 Found);
1420 assert(!Fn && "cast failed but able to resolve overload expression!!");
Nick Lewycky43328e92010-11-09 00:19:31 +00001421 (void)Fn;
1422 } else {
Douglas Gregor8e960432010-11-08 03:40:48 +00001423 Diag(R.getBegin(), msg) << (FunctionalStyle ? CT_Functional : CT_CStyle)
1424 << CastExpr->getType() << CastTy << R;
1425 }
1426 }
John McCalle2b76882010-11-16 05:46:29 +00001427 else if (Kind == CK_BitCast)
John McCallb7f4ffe2010-08-12 21:44:57 +00001428 CheckCastAlign(CastExpr, CastTy, R);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001429
1430 return tcr != TC_Success;
1431}