blob: 214d1f6175585ddff376aff1aa0608c8d1df71c3 [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;
John McCalla21e06c2010-11-26 10:57:22 +0000164 if (TypeDependent)
165 VK = Expr::getValueKindForType(DestType);
166
Sebastian Redl26d85b12008-11-05 21:50:06 +0000167 switch (Kind) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000168 default: llvm_unreachable("Unknown C++ cast!");
Sebastian Redl26d85b12008-11-05 21:50:06 +0000169
170 case tok::kw_const_cast:
Douglas Gregor9103bb22008-12-17 22:52:20 +0000171 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000172 CheckConstCast(*this, Ex, DestType, VK, OpRange, DestRange);
John McCallf871d0c2010-08-07 06:22:56 +0000173 return Owned(CXXConstCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000174 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000175 VK, Ex, DestTInfo, OpLoc));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000176
Anders Carlsson714179b2009-08-02 19:07:59 +0000177 case tok::kw_dynamic_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000178 CastKind Kind = CK_Dependent;
John McCallf871d0c2010-08-07 06:22:56 +0000179 CXXCastPath BasePath;
Douglas Gregor9103bb22008-12-17 22:52:20 +0000180 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000181 CheckDynamicCast(*this, Ex, DestType, VK, OpRange, DestRange,
182 Kind, BasePath);
John McCallf871d0c2010-08-07 06:22:56 +0000183 return Owned(CXXDynamicCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000184 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000185 VK, Kind, Ex, &BasePath, DestTInfo,
John McCallf871d0c2010-08-07 06:22:56 +0000186 OpLoc));
Anders Carlsson714179b2009-08-02 19:07:59 +0000187 }
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000188 case tok::kw_reinterpret_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000189 CastKind Kind = CK_Dependent;
Douglas Gregor9103bb22008-12-17 22:52:20 +0000190 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000191 CheckReinterpretCast(*this, Ex, DestType, VK, OpRange, DestRange, Kind);
John McCallf871d0c2010-08-07 06:22:56 +0000192 return Owned(CXXReinterpretCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000193 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000194 VK, Kind, Ex, 0,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000195 DestTInfo, OpLoc));
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000196 }
Anders Carlssoncdb61972009-08-07 22:21:05 +0000197 case tok::kw_static_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000198 CastKind Kind = CK_Dependent;
John McCallf871d0c2010-08-07 06:22:56 +0000199 CXXCastPath BasePath;
Douglas Gregord6e44a32010-04-16 22:09:46 +0000200 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000201 CheckStaticCast(*this, Ex, DestType, VK, OpRange, Kind, BasePath);
Anders Carlsson0aebc812009-09-09 21:33:21 +0000202
John McCallf871d0c2010-08-07 06:22:56 +0000203 return Owned(CXXStaticCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000204 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000205 VK, Kind, Ex, &BasePath,
John McCallf871d0c2010-08-07 06:22:56 +0000206 DestTInfo, OpLoc));
Anders Carlssoncdb61972009-08-07 22:21:05 +0000207 }
Sebastian Redl26d85b12008-11-05 21:50:06 +0000208 }
209
Sebastian Redlf53597f2009-03-15 17:47:39 +0000210 return ExprError();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000211}
212
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000213/// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes,
214/// this removes one level of indirection from both types, provided that they're
215/// the same kind of pointer (plain or to-member). Unlike the Sema function,
216/// this one doesn't care if the two pointers-to-member don't point into the
217/// same class. This is because CastsAwayConstness doesn't care.
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000218static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) {
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000219 const PointerType *T1PtrType = T1->getAs<PointerType>(),
220 *T2PtrType = T2->getAs<PointerType>();
221 if (T1PtrType && T2PtrType) {
222 T1 = T1PtrType->getPointeeType();
223 T2 = T2PtrType->getPointeeType();
224 return true;
225 }
Fariborz Jahanian72a86592010-02-03 20:32:31 +0000226 const ObjCObjectPointerType *T1ObjCPtrType =
227 T1->getAs<ObjCObjectPointerType>(),
228 *T2ObjCPtrType =
229 T2->getAs<ObjCObjectPointerType>();
230 if (T1ObjCPtrType) {
231 if (T2ObjCPtrType) {
232 T1 = T1ObjCPtrType->getPointeeType();
233 T2 = T2ObjCPtrType->getPointeeType();
234 return true;
235 }
236 else if (T2PtrType) {
237 T1 = T1ObjCPtrType->getPointeeType();
238 T2 = T2PtrType->getPointeeType();
239 return true;
240 }
241 }
242 else if (T2ObjCPtrType) {
243 if (T1PtrType) {
244 T2 = T2ObjCPtrType->getPointeeType();
245 T1 = T1PtrType->getPointeeType();
246 return true;
247 }
248 }
249
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000250 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
251 *T2MPType = T2->getAs<MemberPointerType>();
252 if (T1MPType && T2MPType) {
253 T1 = T1MPType->getPointeeType();
254 T2 = T2MPType->getPointeeType();
255 return true;
256 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000257
258 const BlockPointerType *T1BPType = T1->getAs<BlockPointerType>(),
259 *T2BPType = T2->getAs<BlockPointerType>();
260 if (T1BPType && T2BPType) {
261 T1 = T1BPType->getPointeeType();
262 T2 = T2BPType->getPointeeType();
263 return true;
264 }
265
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000266 return false;
267}
268
Sebastian Redldb647282009-01-27 23:18:31 +0000269/// CastsAwayConstness - Check if the pointer conversion from SrcType to
270/// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by
271/// the cast checkers. Both arguments must denote pointer (possibly to member)
272/// types.
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000273static bool
Mike Stump1eb44332009-09-09 15:08:12 +0000274CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) {
Sebastian Redldb647282009-01-27 23:18:31 +0000275 // Casting away constness is defined in C++ 5.2.11p8 with reference to
276 // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since
277 // the rules are non-trivial. So first we construct Tcv *...cv* as described
278 // in C++ 5.2.11p8.
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000279 assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() ||
280 SrcType->isBlockPointerType()) &&
Sebastian Redldb647282009-01-27 23:18:31 +0000281 "Source type is not pointer or pointer to member.");
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000282 assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() ||
283 DestType->isBlockPointerType()) &&
Sebastian Redldb647282009-01-27 23:18:31 +0000284 "Destination type is not pointer or pointer to member.");
Sebastian Redl26d85b12008-11-05 21:50:06 +0000285
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000286 QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType),
287 UnwrappedDestType = Self.Context.getCanonicalType(DestType);
John McCall0953e762009-09-24 19:53:00 +0000288 llvm::SmallVector<Qualifiers, 8> cv1, cv2;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000289
290 // Find the qualifications.
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000291 while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
Anders Carlsson52647c62010-06-04 22:47:55 +0000292 Qualifiers SrcQuals;
293 Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals);
294 cv1.push_back(SrcQuals);
295
296 Qualifiers DestQuals;
297 Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals);
298 cv2.push_back(DestQuals);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000299 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000300 if (cv1.empty())
301 return false;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000302
303 // Construct void pointers with those qualifiers (in reverse order of
304 // unwrapping, of course).
Sebastian Redl37d6de32008-11-08 13:00:26 +0000305 QualType SrcConstruct = Self.Context.VoidTy;
306 QualType DestConstruct = Self.Context.VoidTy;
John McCall0953e762009-09-24 19:53:00 +0000307 ASTContext &Context = Self.Context;
308 for (llvm::SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(),
309 i2 = cv2.rbegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000310 i1 != cv1.rend(); ++i1, ++i2) {
John McCall0953e762009-09-24 19:53:00 +0000311 SrcConstruct
312 = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1));
313 DestConstruct
314 = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000315 }
316
317 // Test if they're compatible.
318 return SrcConstruct != DestConstruct &&
Sebastian Redl37d6de32008-11-08 13:00:26 +0000319 !Self.IsQualificationConversion(SrcConstruct, DestConstruct);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000320}
321
Sebastian Redl26d85b12008-11-05 21:50:06 +0000322/// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid.
323/// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime-
324/// checked downcasts in class hierarchies.
Anders Carlsson714179b2009-08-02 19:07:59 +0000325static void
Sebastian Redl37d6de32008-11-08 13:00:26 +0000326CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000327 ExprValueKind &VK, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000328 const SourceRange &DestRange, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000329 CXXCastPath &BasePath) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000330 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
Sebastian Redl37d6de32008-11-08 13:00:26 +0000331 DestType = Self.Context.getCanonicalType(DestType);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000332
333 // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type,
334 // or "pointer to cv void".
335
336 QualType DestPointee;
Ted Kremenek6217b802009-07-29 21:53:49 +0000337 const PointerType *DestPointer = DestType->getAs<PointerType>();
John McCallf89e55a2010-11-18 06:31:45 +0000338 const ReferenceType *DestReference = 0;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000339 if (DestPointer) {
340 DestPointee = DestPointer->getPointeeType();
John McCallf89e55a2010-11-18 06:31:45 +0000341 } else if ((DestReference = DestType->getAs<ReferenceType>())) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000342 DestPointee = DestReference->getPointeeType();
John McCallf89e55a2010-11-18 06:31:45 +0000343 VK = isa<LValueReferenceType>(DestReference) ? VK_LValue : VK_RValue;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000344 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000345 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
Chris Lattnerd1625842008-11-24 06:25:27 +0000346 << OrigDestType << DestRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000347 return;
348 }
349
Ted Kremenek6217b802009-07-29 21:53:49 +0000350 const RecordType *DestRecord = DestPointee->getAs<RecordType>();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000351 if (DestPointee->isVoidType()) {
352 assert(DestPointer && "Reference to void is not possible");
353 } else if (DestRecord) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000354 if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000355 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssonb7906612009-08-26 23:45:07 +0000356 << DestRange))
Sebastian Redl26d85b12008-11-05 21:50:06 +0000357 return;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000358 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000359 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattnerd1625842008-11-24 06:25:27 +0000360 << DestPointee.getUnqualifiedType() << DestRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000361 return;
362 }
363
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000364 // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
365 // complete class type, [...]. If T is an lvalue reference type, v shall be
366 // an lvalue of a complete class type, [...]. If T is an rvalue reference
367 // type, v shall be an expression having a complete effective class type,
368 // [...]
Sebastian Redl26d85b12008-11-05 21:50:06 +0000369
Sebastian Redl37d6de32008-11-08 13:00:26 +0000370 QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000371 QualType SrcPointee;
372 if (DestPointer) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000373 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000374 SrcPointee = SrcPointer->getPointeeType();
375 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000376 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
Chris Lattnerd1625842008-11-24 06:25:27 +0000377 << OrigSrcType << SrcExpr->getSourceRange();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000378 return;
379 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000380 } else if (DestReference->isLValueReferenceType()) {
John McCall7eb0a9e2010-11-24 05:12:34 +0000381 if (!SrcExpr->isLValue()) {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000382 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000383 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000384 }
385 SrcPointee = SrcType;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000386 } else {
387 SrcPointee = SrcType;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000388 }
389
Ted Kremenek6217b802009-07-29 21:53:49 +0000390 const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000391 if (SrcRecord) {
Douglas Gregor86447ec2009-03-09 16:13:40 +0000392 if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000393 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssonb7906612009-08-26 23:45:07 +0000394 << SrcExpr->getSourceRange()))
Sebastian Redl26d85b12008-11-05 21:50:06 +0000395 return;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000396 } else {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000397 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattnerd1625842008-11-24 06:25:27 +0000398 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000399 return;
400 }
401
402 assert((DestPointer || DestReference) &&
403 "Bad destination non-ptr/ref slipped through.");
404 assert((DestRecord || DestPointee->isVoidType()) &&
405 "Bad destination pointee slipped through.");
406 assert(SrcRecord && "Bad source pointee slipped through.");
407
408 // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness.
409 if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000410 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000411 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000412 return;
413 }
414
415 // C++ 5.2.7p3: If the type of v is the same as the required result type,
416 // [except for cv].
417 if (DestRecord == SrcRecord) {
John McCall2de56d12010-08-25 11:45:40 +0000418 Kind = CK_NoOp;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000419 return;
420 }
421
422 // C++ 5.2.7p5
423 // Upcasts are resolved statically.
Sebastian Redl37d6de32008-11-08 13:00:26 +0000424 if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000425 if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
426 OpRange.getBegin(), OpRange,
427 &BasePath))
428 return;
429
John McCall2de56d12010-08-25 11:45:40 +0000430 Kind = CK_DerivedToBase;
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000431
432 // If we are casting to or through a virtual base class, we need a
433 // vtable.
434 if (Self.BasePathInvolvesVirtualBase(BasePath))
435 Self.MarkVTableUsed(OpRange.getBegin(),
436 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000437 return;
438 }
439
440 // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
Douglas Gregor952b0172010-02-11 01:04:33 +0000441 const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition();
Sebastian Redld93f0dd2008-11-06 15:59:35 +0000442 assert(SrcDecl && "Definition missing");
443 if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000444 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
Chris Lattnerd1625842008-11-24 06:25:27 +0000445 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redld93f0dd2008-11-06 15:59:35 +0000446 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000447 Self.MarkVTableUsed(OpRange.getBegin(),
448 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000449
450 // Done. Everything else is run-time checks.
John McCall2de56d12010-08-25 11:45:40 +0000451 Kind = CK_Dynamic;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000452}
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000453
454/// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
455/// Refer to C++ 5.2.11 for details. const_cast is typically used in code
456/// like this:
457/// const char *str = "literal";
458/// legacy_function(const_cast\<char*\>(str));
459void
John McCallf89e55a2010-11-18 06:31:45 +0000460CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType, ExprValueKind &VK,
Mike Stump1eb44332009-09-09 15:08:12 +0000461 const SourceRange &OpRange, const SourceRange &DestRange) {
John McCallf89e55a2010-11-18 06:31:45 +0000462 VK = Expr::getValueKindForType(DestType);
463 if (VK == VK_RValue)
Douglas Gregora873dfc2010-02-03 00:27:59 +0000464 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000465
466 unsigned msg = diag::err_bad_cxx_cast_generic;
467 if (TryConstCast(Self, SrcExpr, DestType, /*CStyle*/false, msg) != TC_Success
468 && msg != 0)
469 Self.Diag(OpRange.getBegin(), msg) << CT_Const
470 << SrcExpr->getType() << DestType << OpRange;
471}
472
473/// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is
474/// valid.
475/// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code
476/// like this:
477/// char *bytes = reinterpret_cast\<char*\>(int_ptr);
478void
479CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000480 ExprValueKind &VK, const SourceRange &OpRange,
481 const SourceRange &DestRange, CastKind &Kind) {
482 VK = Expr::getValueKindForType(DestType);
483 if (VK == VK_RValue)
Douglas Gregora873dfc2010-02-03 00:27:59 +0000484 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000485
486 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlsson3c31a392009-09-26 00:12:34 +0000487 if (TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange,
488 msg, Kind)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000489 != TC_Success && msg != 0)
Douglas Gregor8e960432010-11-08 03:40:48 +0000490 {
491 if (SrcExpr->getType() == Self.Context.OverloadTy)
492 {
493 //FIXME: &f<int>; is overloaded and resolvable
494 Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload)
495 << OverloadExpr::find(SrcExpr).Expression->getName()
496 << DestType << OpRange;
497 NoteAllOverloadCandidates(SrcExpr, Self);
498
499 }
500 else
501 Self.Diag(OpRange.getBegin(), msg) << CT_Reinterpret
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000502 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregor8e960432010-11-08 03:40:48 +0000503 }
504
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000505}
506
507
508/// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid.
509/// Refer to C++ 5.2.9 for details. Static casts are mostly used for making
510/// implicit conversions explicit and getting rid of data loss warnings.
511void
512CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000513 ExprValueKind &VK, const SourceRange &OpRange,
514 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000515 // This test is outside everything else because it's the only case where
516 // a non-lvalue-reference target type does not lead to decay.
517 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000518 if (DestType->isVoidType()) {
John McCall2de56d12010-08-25 11:45:40 +0000519 Kind = CK_ToVoid;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000520 return;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000521 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000522
John McCallf89e55a2010-11-18 06:31:45 +0000523 VK = Expr::getValueKindForType(DestType);
524 if (VK == VK_RValue && !DestType->isRecordType())
Douglas Gregora873dfc2010-02-03 00:27:59 +0000525 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000526
527 unsigned msg = diag::err_bad_cxx_cast_generic;
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000528 if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, msg,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000529 Kind, BasePath) != TC_Success && msg != 0)
Douglas Gregor8e960432010-11-08 03:40:48 +0000530 {
John McCalldaa8e4e2010-11-15 09:13:47 +0000531 if (SrcExpr->getType() == Self.Context.OverloadTy)
Douglas Gregor8e960432010-11-08 03:40:48 +0000532 {
533 OverloadExpr* oe = OverloadExpr::find(SrcExpr).Expression;
534 Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
535 << oe->getName() << DestType << OpRange << oe->getQualifierRange();
536 NoteAllOverloadCandidates(SrcExpr, Self);
537 }
538 else
539 Self.Diag(OpRange.getBegin(), msg) << CT_Static
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000540 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregor8e960432010-11-08 03:40:48 +0000541 }
John McCalle2b76882010-11-16 05:46:29 +0000542 else if (Kind == CK_BitCast)
John McCallb7f4ffe2010-08-12 21:44:57 +0000543 Self.CheckCastAlign(SrcExpr, DestType, OpRange);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000544}
545
546/// TryStaticCast - Check if a static cast can be performed, and do so if
547/// possible. If @p CStyle, ignore access restrictions on hierarchy casting
548/// and casting away constness.
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000549static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000550 QualType DestType, bool CStyle,
Anders Carlssoncb3c3082009-09-01 20:52:42 +0000551 const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000552 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000553 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000554 // The order the tests is not entirely arbitrary. There is one conversion
555 // that can be handled in two different ways. Given:
556 // struct A {};
557 // struct B : public A {
558 // B(); B(const A&);
559 // };
560 // const A &a = B();
561 // the cast static_cast<const B&>(a) could be seen as either a static
562 // reference downcast, or an explicit invocation of the user-defined
563 // conversion using B's conversion constructor.
564 // DR 427 specifies that the downcast is to be applied here.
565
566 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
567 // Done outside this function.
568
569 TryCastResult tcr;
570
571 // C++ 5.2.9p5, reference downcast.
572 // See the function for details.
573 // DR 427 specifies that this is to be applied before paragraph 2.
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000574 tcr = TryStaticReferenceDowncast(Self, SrcExpr, DestType, CStyle, OpRange,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000575 msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000576 if (tcr != TC_NotApplicable)
577 return tcr;
578
579 // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
580 // reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
581 tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, msg);
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000582 if (tcr != TC_NotApplicable) {
John McCall2de56d12010-08-25 11:45:40 +0000583 Kind = CK_NoOp;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000584 return tcr;
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000585 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000586
587 // C++ 5.2.9p2: An expression e can be explicitly converted to a type T
588 // [...] if the declaration "T t(e);" is well-formed, [...].
Fariborz Jahaniane9f42082009-08-26 18:55:36 +0000589 tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CStyle, OpRange, msg,
Douglas Gregord6e44a32010-04-16 22:09:46 +0000590 Kind);
Anders Carlsson3c31a392009-09-26 00:12:34 +0000591 if (tcr != TC_NotApplicable)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000592 return tcr;
Anders Carlsson0aebc812009-09-09 21:33:21 +0000593
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000594 // C++ 5.2.9p6: May apply the reverse of any standard conversion, except
595 // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
596 // conversions, subject to further restrictions.
597 // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
598 // of qualification conversions impossible.
599 // In the CStyle case, the earlier attempt to const_cast should have taken
600 // care of reverse qualification conversions.
601
602 QualType OrigSrcType = SrcExpr->getType();
603
604 QualType SrcType = Self.Context.getCanonicalType(SrcExpr->getType());
605
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000606 // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly
607 // converted to an integral type.
608 if (Self.getLangOptions().CPlusPlus0x && SrcType->isEnumeralType()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000609 assert(SrcType->getAs<EnumType>()->getDecl()->isScoped());
610 if (DestType->isBooleanType()) {
611 Kind = CK_IntegralToBoolean;
612 return TC_Success;
613 } else if (DestType->isIntegralType(Self.Context)) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000614 Kind = CK_IntegralCast;
615 return TC_Success;
616 }
617 }
618
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000619 // Reverse integral promotion/conversion. All such conversions are themselves
620 // again integral promotions or conversions and are thus already handled by
621 // p2 (TryDirectInitialization above).
622 // (Note: any data loss warnings should be suppressed.)
623 // The exception is the reverse of enum->integer, i.e. integer->enum (and
624 // enum->enum). See also C++ 5.2.9p7.
625 // The same goes for reverse floating point promotion/conversion and
626 // floating-integral conversions. Again, only floating->enum is relevant.
627 if (DestType->isEnumeralType()) {
628 if (SrcType->isComplexType() || SrcType->isVectorType()) {
629 // Fall through - these cannot be converted.
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000630 } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
John McCall2de56d12010-08-25 11:45:40 +0000631 Kind = CK_IntegralCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000632 return TC_Success;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000633 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000634 }
635
636 // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
637 // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000638 tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000639 Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000640 if (tcr != TC_NotApplicable)
641 return tcr;
642
643 // Reverse member pointer conversion. C++ 4.11 specifies member pointer
644 // conversion. C++ 5.2.9p9 has additional information.
645 // DR54's access restrictions apply here also.
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000646 tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
Anders Carlssoncee22422010-04-24 19:22:20 +0000647 OpRange, msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000648 if (tcr != TC_NotApplicable)
649 return tcr;
650
651 // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
652 // void*. C++ 5.2.9p10 specifies additional restrictions, which really is
653 // just the usual constness stuff.
Ted Kremenek6217b802009-07-29 21:53:49 +0000654 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000655 QualType SrcPointee = SrcPointer->getPointeeType();
656 if (SrcPointee->isVoidType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000657 if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000658 QualType DestPointee = DestPointer->getPointeeType();
659 if (DestPointee->isIncompleteOrObjectType()) {
660 // This is definitely the intended conversion, but it might fail due
661 // to a const violation.
662 if (!CStyle && !DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
663 msg = diag::err_bad_cxx_cast_const_away;
664 return TC_Failed;
665 }
John McCall2de56d12010-08-25 11:45:40 +0000666 Kind = CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000667 return TC_Success;
668 }
669 }
Fariborz Jahanian2f6c5502010-05-10 23:46:53 +0000670 else if (DestType->isObjCObjectPointerType()) {
671 // allow both c-style cast and static_cast of objective-c pointers as
672 // they are pervasive.
John McCall2de56d12010-08-25 11:45:40 +0000673 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +0000674 return TC_Success;
675 }
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000676 else if (CStyle && DestType->isBlockPointerType()) {
677 // allow c-style cast of void * to block pointers.
John McCall2de56d12010-08-25 11:45:40 +0000678 Kind = CK_AnyPointerToBlockPointerCast;
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000679 return TC_Success;
680 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000681 }
682 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000683 // Allow arbitray objective-c pointer conversion with static casts.
684 if (SrcType->isObjCObjectPointerType() &&
John McCalldaa8e4e2010-11-15 09:13:47 +0000685 DestType->isObjCObjectPointerType()) {
686 Kind = CK_BitCast;
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000687 return TC_Success;
John McCalldaa8e4e2010-11-15 09:13:47 +0000688 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000689
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000690 // We tried everything. Everything! Nothing works! :-(
691 return TC_NotApplicable;
692}
693
694/// Tests whether a conversion according to N2844 is valid.
695TryCastResult
696TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
Mike Stump1eb44332009-09-09 15:08:12 +0000697 unsigned &msg) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000698 // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
699 // reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
Ted Kremenek6217b802009-07-29 21:53:49 +0000700 const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000701 if (!R)
702 return TC_NotApplicable;
703
John McCall7eb0a9e2010-11-24 05:12:34 +0000704 if (!SrcExpr->isLValue())
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000705 return TC_NotApplicable;
706
707 // Because we try the reference downcast before this function, from now on
708 // this is the only cast possibility, so we issue an error if we fail now.
709 // FIXME: Should allow casting away constness if CStyle.
710 bool DerivedToBase;
Douglas Gregor569c3162010-08-07 11:51:51 +0000711 bool ObjCConversion;
Douglas Gregor393896f2009-11-05 13:06:35 +0000712 if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
713 SrcExpr->getType(), R->getPointeeType(),
Douglas Gregor569c3162010-08-07 11:51:51 +0000714 DerivedToBase, ObjCConversion) <
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000715 Sema::Ref_Compatible_With_Added_Qualification) {
716 msg = diag::err_bad_lvalue_to_rvalue_cast;
717 return TC_Failed;
718 }
719
Douglas Gregorf0e0b172010-03-25 00:20:38 +0000720 // FIXME: We should probably have an AST node for lvalue-to-rvalue
721 // conversions.
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000722 return TC_Success;
723}
724
725/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
726TryCastResult
727TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
728 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000729 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000730 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000731 // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
732 // cast to type "reference to cv2 D", where D is a class derived from B,
733 // if a valid standard conversion from "pointer to D" to "pointer to B"
734 // exists, cv2 >= cv1, and B is not a virtual base class of D.
735 // In addition, DR54 clarifies that the base must be accessible in the
736 // current context. Although the wording of DR54 only applies to the pointer
737 // variant of this rule, the intent is clearly for it to apply to the this
738 // conversion as well.
739
Ted Kremenek6217b802009-07-29 21:53:49 +0000740 const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000741 if (!DestReference) {
742 return TC_NotApplicable;
743 }
744 bool RValueRef = DestReference->isRValueReferenceType();
John McCall7eb0a9e2010-11-24 05:12:34 +0000745 if (!RValueRef && !SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000746 // We know the left side is an lvalue reference, so we can suggest a reason.
747 msg = diag::err_bad_cxx_cast_rvalue;
748 return TC_NotApplicable;
749 }
750
751 QualType DestPointee = DestReference->getPointeeType();
752
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000753 return TryStaticDowncast(Self,
754 Self.Context.getCanonicalType(SrcExpr->getType()),
755 Self.Context.getCanonicalType(DestPointee), CStyle,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000756 OpRange, SrcExpr->getType(), DestType, msg, Kind,
757 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000758}
759
760/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
761TryCastResult
762TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
Mike Stump1eb44332009-09-09 15:08:12 +0000763 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000764 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000765 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000766 // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
767 // type, can be converted to an rvalue of type "pointer to cv2 D", where D
768 // is a class derived from B, if a valid standard conversion from "pointer
769 // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
770 // class of D.
771 // In addition, DR54 clarifies that the base must be accessible in the
772 // current context.
773
Ted Kremenek6217b802009-07-29 21:53:49 +0000774 const PointerType *DestPointer = DestType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000775 if (!DestPointer) {
776 return TC_NotApplicable;
777 }
778
Ted Kremenek6217b802009-07-29 21:53:49 +0000779 const PointerType *SrcPointer = SrcType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000780 if (!SrcPointer) {
781 msg = diag::err_bad_static_cast_pointer_nonpointer;
782 return TC_NotApplicable;
783 }
784
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000785 return TryStaticDowncast(Self,
786 Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
787 Self.Context.getCanonicalType(DestPointer->getPointeeType()),
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000788 CStyle, OpRange, SrcType, DestType, msg, Kind,
789 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000790}
791
792/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
793/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000794/// DestType is possible and allowed.
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000795TryCastResult
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000796TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000797 bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000798 QualType OrigDestType, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000799 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000800 // We can only work with complete types. But don't complain if it doesn't work
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000801 if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) ||
802 Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0)))
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000803 return TC_NotApplicable;
804
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000805 // Downcast can only happen in class hierarchies, so we need classes.
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000806 if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000807 return TC_NotApplicable;
808 }
809
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000810 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000811 /*DetectVirtual=*/true);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000812 if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
813 return TC_NotApplicable;
814 }
815
816 // Target type does derive from source type. Now we're serious. If an error
817 // appears now, it's not ignored.
818 // This may not be entirely in line with the standard. Take for example:
819 // struct A {};
820 // struct B : virtual A {
821 // B(A&);
822 // };
Mike Stump1eb44332009-09-09 15:08:12 +0000823 //
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000824 // void f()
825 // {
826 // (void)static_cast<const B&>(*((A*)0));
827 // }
828 // As far as the standard is concerned, p5 does not apply (A is virtual), so
829 // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
830 // However, both GCC and Comeau reject this example, and accepting it would
831 // mean more complex code if we're to preserve the nice error message.
832 // FIXME: Being 100% compliant here would be nice to have.
833
834 // Must preserve cv, as always, unless we're in C-style mode.
835 if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
836 msg = diag::err_bad_cxx_cast_const_away;
837 return TC_Failed;
838 }
839
840 if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
841 // This code is analoguous to that in CheckDerivedToBaseConversion, except
842 // that it builds the paths in reverse order.
843 // To sum up: record all paths to the base and build a nice string from
844 // them. Use it to spice up the error message.
845 if (!Paths.isRecordingPaths()) {
846 Paths.clear();
847 Paths.setRecordingPaths(true);
848 Self.IsDerivedFrom(DestType, SrcType, Paths);
849 }
850 std::string PathDisplayStr;
851 std::set<unsigned> DisplayedPaths;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000852 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000853 PI != PE; ++PI) {
854 if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
855 // We haven't displayed a path to this particular base
856 // class subobject yet.
857 PathDisplayStr += "\n ";
Douglas Gregora8f32e02009-10-06 17:59:45 +0000858 for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
859 EE = PI->rend();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000860 EI != EE; ++EI)
861 PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000862 PathDisplayStr += QualType(DestType).getAsString();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000863 }
864 }
865
866 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000867 << QualType(SrcType).getUnqualifiedType()
868 << QualType(DestType).getUnqualifiedType()
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000869 << PathDisplayStr << OpRange;
870 msg = 0;
871 return TC_Failed;
872 }
873
874 if (Paths.getDetectedVirtual() != 0) {
875 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
876 Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
877 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
878 msg = 0;
879 return TC_Failed;
880 }
881
John McCall6b2accb2010-02-10 09:31:12 +0000882 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
John McCall6b2accb2010-02-10 09:31:12 +0000883 SrcType, DestType,
John McCall58e6f342010-03-16 05:22:47 +0000884 Paths.front(),
885 diag::err_downcast_from_inaccessible_base)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000886 msg = 0;
887 return TC_Failed;
888 }
889
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000890 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +0000891 Kind = CK_BaseToDerived;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000892 return TC_Success;
893}
894
895/// TryStaticMemberPointerUpcast - Tests whether a conversion according to
896/// C++ 5.2.9p9 is valid:
897///
898/// An rvalue of type "pointer to member of D of type cv1 T" can be
899/// converted to an rvalue of type "pointer to member of B of type cv2 T",
900/// where B is a base class of D [...].
901///
902TryCastResult
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000903TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr, QualType SrcType,
904 QualType DestType, bool CStyle,
905 const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000906 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000907 CXXCastPath &BasePath) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000908 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000909 if (!DestMemPtr)
910 return TC_NotApplicable;
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000911
912 bool WasOverloadedFunction = false;
John McCall6bb80172010-03-30 21:47:33 +0000913 DeclAccessPair FoundOverload;
Douglas Gregor1a8cf732010-04-14 23:11:21 +0000914 if (SrcExpr->getType() == Self.Context.OverloadTy) {
915 if (FunctionDecl *Fn
916 = Self.ResolveAddressOfOverloadedFunction(SrcExpr, DestType, false,
917 FoundOverload)) {
918 CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
919 SrcType = Self.Context.getMemberPointerType(Fn->getType(),
920 Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
921 WasOverloadedFunction = true;
922 }
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000923 }
Douglas Gregor1a8cf732010-04-14 23:11:21 +0000924
Ted Kremenek6217b802009-07-29 21:53:49 +0000925 const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000926 if (!SrcMemPtr) {
927 msg = diag::err_bad_static_cast_member_pointer_nonmp;
928 return TC_NotApplicable;
929 }
930
931 // T == T, modulo cv
Douglas Gregora4923eb2009-11-16 21:35:15 +0000932 if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
933 DestMemPtr->getPointeeType()))
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000934 return TC_NotApplicable;
935
936 // B base of D
937 QualType SrcClass(SrcMemPtr->getClass(), 0);
938 QualType DestClass(DestMemPtr->getClass(), 0);
Anders Carlssoncee22422010-04-24 19:22:20 +0000939 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000940 /*DetectVirtual=*/true);
941 if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
942 return TC_NotApplicable;
943 }
944
945 // 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 +0000946 if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000947 Paths.clear();
948 Paths.setRecordingPaths(true);
949 bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
950 assert(StillOkay);
951 StillOkay = StillOkay;
952 std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
953 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
954 << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
955 msg = 0;
956 return TC_Failed;
957 }
958
959 if (const RecordType *VBase = Paths.getDetectedVirtual()) {
960 Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
961 << SrcClass << DestClass << QualType(VBase, 0) << OpRange;
962 msg = 0;
963 return TC_Failed;
964 }
965
John McCall6b2accb2010-02-10 09:31:12 +0000966 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
Eli Friedman0fab8cd2010-07-23 19:25:41 +0000967 DestClass, SrcClass,
John McCall58e6f342010-03-16 05:22:47 +0000968 Paths.front(),
969 diag::err_upcast_to_inaccessible_base)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000970 msg = 0;
971 return TC_Failed;
972 }
973
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000974 if (WasOverloadedFunction) {
975 // Resolve the address of the overloaded function again, this time
976 // allowing complaints if something goes wrong.
977 FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr,
978 DestType,
John McCall6bb80172010-03-30 21:47:33 +0000979 true,
980 FoundOverload);
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000981 if (!Fn) {
982 msg = 0;
983 return TC_Failed;
984 }
985
John McCall6bb80172010-03-30 21:47:33 +0000986 SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000987 if (!SrcExpr) {
988 msg = 0;
989 return TC_Failed;
990 }
991 }
992
Anders Carlssoncee22422010-04-24 19:22:20 +0000993 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +0000994 Kind = CK_DerivedToBaseMemberPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000995 return TC_Success;
996}
997
998/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
999/// is valid:
1000///
1001/// An expression e can be explicitly converted to a type T using a
1002/// @c static_cast if the declaration "T t(e);" is well-formed [...].
1003TryCastResult
Sebastian Redla82e4ae2009-11-14 21:15:49 +00001004TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
Fariborz Jahaniane9f42082009-08-26 18:55:36 +00001005 bool CStyle, const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001006 CastKind &Kind) {
Anders Carlssond851b372009-09-07 18:25:47 +00001007 if (DestType->isRecordType()) {
1008 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
1009 diag::err_bad_dynamic_cast_incomplete)) {
1010 msg = 0;
1011 return TC_Failed;
1012 }
1013 }
Douglas Gregord6e44a32010-04-16 22:09:46 +00001014
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001015 InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
1016 InitializationKind InitKind
Douglas Gregord6e44a32010-04-16 22:09:46 +00001017 = InitializationKind::CreateCast(/*FIXME:*/OpRange,
1018 CStyle);
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001019 InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExpr, 1);
Douglas Gregor8e960432010-11-08 03:40:48 +00001020
1021 // At this point of CheckStaticCast, if the destination is a reference,
1022 // or the expression is an overload expression this has to work.
1023 // There is no other way that works.
1024 // On the other hand, if we're checking a C-style cast, we've still got
1025 // the reinterpret_cast way.
1026
Douglas Gregord6e44a32010-04-16 22:09:46 +00001027 if (InitSeq.getKind() == InitializationSequence::FailedSequence &&
Douglas Gregor8e960432010-11-08 03:40:48 +00001028 (CStyle || !DestType->isReferenceType()))
Anders Carlsson3c31a392009-09-26 00:12:34 +00001029 return TC_NotApplicable;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001030
John McCall60d7b3a2010-08-24 06:29:42 +00001031 ExprResult Result
John McCallf312b1e2010-08-26 23:41:50 +00001032 = InitSeq.Perform(Self, Entity, InitKind, MultiExprArg(Self, &SrcExpr, 1));
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001033 if (Result.isInvalid()) {
1034 msg = 0;
1035 return TC_Failed;
1036 }
1037
Douglas Gregord6e44a32010-04-16 22:09:46 +00001038 if (InitSeq.isConstructorInitialization())
John McCall2de56d12010-08-25 11:45:40 +00001039 Kind = CK_ConstructorConversion;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001040 else
John McCall2de56d12010-08-25 11:45:40 +00001041 Kind = CK_NoOp;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001042
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001043 SrcExpr = Result.takeAs<Expr>();
1044 return TC_Success;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001045}
1046
1047/// TryConstCast - See if a const_cast from source to destination is allowed,
1048/// and perform it if it is.
1049static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
1050 bool CStyle, unsigned &msg) {
1051 DestType = Self.Context.getCanonicalType(DestType);
1052 QualType SrcType = SrcExpr->getType();
1053 if (const LValueReferenceType *DestTypeTmp =
Ted Kremenek6217b802009-07-29 21:53:49 +00001054 DestType->getAs<LValueReferenceType>()) {
John McCall7eb0a9e2010-11-24 05:12:34 +00001055 if (!SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001056 // Cannot const_cast non-lvalue to lvalue reference type. But if this
1057 // is C-style, static_cast might find a way, so we simply suggest a
1058 // message and tell the parent to keep searching.
1059 msg = diag::err_bad_cxx_cast_rvalue;
1060 return TC_NotApplicable;
1061 }
1062
1063 // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
1064 // [...] if a pointer to T1 can be [cast] to the type pointer to T2.
1065 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1066 SrcType = Self.Context.getPointerType(SrcType);
1067 }
1068
1069 // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
1070 // the rules for const_cast are the same as those used for pointers.
1071
John McCalld425d2b2010-05-18 09:35:29 +00001072 if (!DestType->isPointerType() &&
1073 !DestType->isMemberPointerType() &&
1074 !DestType->isObjCObjectPointerType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001075 // Cannot cast to non-pointer, non-reference type. Note that, if DestType
1076 // was a reference type, we converted it to a pointer above.
1077 // The status of rvalue references isn't entirely clear, but it looks like
1078 // conversion to them is simply invalid.
1079 // C++ 5.2.11p3: For two pointer types [...]
1080 if (!CStyle)
1081 msg = diag::err_bad_const_cast_dest;
1082 return TC_NotApplicable;
1083 }
1084 if (DestType->isFunctionPointerType() ||
1085 DestType->isMemberFunctionPointerType()) {
1086 // Cannot cast direct function pointers.
1087 // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
1088 // T is the ultimate pointee of source and target type.
1089 if (!CStyle)
1090 msg = diag::err_bad_const_cast_dest;
1091 return TC_NotApplicable;
1092 }
1093 SrcType = Self.Context.getCanonicalType(SrcType);
1094
1095 // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
1096 // completely equal.
1097 // FIXME: const_cast should probably not be able to convert between pointers
1098 // to different address spaces.
1099 // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
1100 // in multi-level pointers may change, but the level count must be the same,
1101 // as must be the final pointee type.
1102 while (SrcType != DestType &&
Douglas Gregor5a57efd2010-06-09 03:53:18 +00001103 Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) {
Chandler Carruth595e2902009-12-29 08:05:19 +00001104 Qualifiers Quals;
1105 SrcType = Self.Context.getUnqualifiedArrayType(SrcType, Quals);
1106 DestType = Self.Context.getUnqualifiedArrayType(DestType, Quals);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001107 }
1108
1109 // Since we're dealing in canonical types, the remainder must be the same.
1110 if (SrcType != DestType)
1111 return TC_NotApplicable;
1112
1113 return TC_Success;
1114}
1115
Douglas Gregor8e960432010-11-08 03:40:48 +00001116
1117static void NoteAllOverloadCandidates(Expr* const Expr, Sema& sema)
1118{
1119
1120 assert(Expr->getType() == sema.Context.OverloadTy);
1121
1122 OverloadExpr::FindResult Ovl = OverloadExpr::find(Expr);
1123 OverloadExpr *const OvlExpr = Ovl.Expression;
1124
1125 for (UnresolvedSetIterator it = OvlExpr->decls_begin(),
1126 end = OvlExpr->decls_end(); it != end; ++it) {
1127 if ( FunctionTemplateDecl *ftd =
1128 dyn_cast<FunctionTemplateDecl>((*it)->getUnderlyingDecl()) )
1129 {
1130 sema.NoteOverloadCandidate(ftd->getTemplatedDecl());
1131 }
1132 else if ( FunctionDecl *f =
1133 dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl()) )
1134 {
1135 sema.NoteOverloadCandidate(f);
1136 }
1137 }
1138}
1139
1140
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001141static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
1142 QualType DestType, bool CStyle,
1143 const SourceRange &OpRange,
Anders Carlsson3c31a392009-09-26 00:12:34 +00001144 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001145 CastKind &Kind) {
Douglas Gregore39a3892010-07-13 23:17:26 +00001146 bool IsLValueCast = false;
1147
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001148 DestType = Self.Context.getCanonicalType(DestType);
1149 QualType SrcType = SrcExpr->getType();
Douglas Gregor8e960432010-11-08 03:40:48 +00001150
1151 // Is the source an overloaded name? (i.e. &foo)
1152 // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5)
John McCall404cd162010-11-13 01:35:44 +00001153 if (SrcType == Self.Context.OverloadTy)
Douglas Gregor8e960432010-11-08 03:40:48 +00001154 return TC_NotApplicable;
1155
Ted Kremenek6217b802009-07-29 21:53:49 +00001156 if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001157 bool LValue = DestTypeTmp->isLValueReferenceType();
John McCall7eb0a9e2010-11-24 05:12:34 +00001158 if (LValue && !SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001159 // Cannot cast non-lvalue to reference type. See the similar comment in
1160 // const_cast.
1161 msg = diag::err_bad_cxx_cast_rvalue;
1162 return TC_NotApplicable;
1163 }
1164
1165 // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
1166 // same effect as the conversion *reinterpret_cast<T*>(&x) with the
1167 // built-in & and * operators.
1168 // This code does this transformation for the checked types.
1169 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1170 SrcType = Self.Context.getPointerType(SrcType);
Douglas Gregor8e960432010-11-08 03:40:48 +00001171
Douglas Gregore39a3892010-07-13 23:17:26 +00001172 IsLValueCast = true;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001173 }
1174
1175 // Canonicalize source for comparison.
1176 SrcType = Self.Context.getCanonicalType(SrcType);
1177
Ted Kremenek6217b802009-07-29 21:53:49 +00001178 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
1179 *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001180 if (DestMemPtr && SrcMemPtr) {
1181 // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
1182 // can be explicitly converted to an rvalue of type "pointer to member
1183 // of Y of type T2" if T1 and T2 are both function types or both object
1184 // types.
1185 if (DestMemPtr->getPointeeType()->isFunctionType() !=
1186 SrcMemPtr->getPointeeType()->isFunctionType())
1187 return TC_NotApplicable;
1188
1189 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
1190 // constness.
1191 // A reinterpret_cast followed by a const_cast can, though, so in C-style,
1192 // we accept it.
1193 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1194 msg = diag::err_bad_cxx_cast_const_away;
1195 return TC_Failed;
1196 }
1197
Charles Davisf231df32010-08-16 05:30:44 +00001198 // Don't allow casting between member pointers of different sizes.
1199 if (Self.Context.getTypeSize(DestMemPtr) !=
1200 Self.Context.getTypeSize(SrcMemPtr)) {
1201 msg = diag::err_bad_cxx_cast_member_pointer_size;
1202 return TC_Failed;
1203 }
1204
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001205 // A valid member pointer cast.
John McCall2de56d12010-08-25 11:45:40 +00001206 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001207 return TC_Success;
1208 }
1209
1210 // See below for the enumeral issue.
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001211 if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001212 // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
1213 // type large enough to hold it. A value of std::nullptr_t can be
1214 // converted to an integral type; the conversion has the same meaning
1215 // and validity as a conversion of (void*)0 to the integral type.
1216 if (Self.Context.getTypeSize(SrcType) >
1217 Self.Context.getTypeSize(DestType)) {
1218 msg = diag::err_bad_reinterpret_cast_small_int;
1219 return TC_Failed;
1220 }
John McCall2de56d12010-08-25 11:45:40 +00001221 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001222 return TC_Success;
1223 }
1224
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001225 bool destIsVector = DestType->isVectorType();
1226 bool srcIsVector = SrcType->isVectorType();
1227 if (srcIsVector || destIsVector) {
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001228 // FIXME: Should this also apply to floating point types?
1229 bool srcIsScalar = SrcType->isIntegralType(Self.Context);
1230 bool destIsScalar = DestType->isIntegralType(Self.Context);
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001231
1232 // Check if this is a cast between a vector and something else.
1233 if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
1234 !(srcIsVector && destIsVector))
1235 return TC_NotApplicable;
1236
1237 // If both types have the same size, we can successfully cast.
Douglas Gregorf2a55392009-12-22 22:47:22 +00001238 if (Self.Context.getTypeSize(SrcType)
1239 == Self.Context.getTypeSize(DestType)) {
John McCall2de56d12010-08-25 11:45:40 +00001240 Kind = CK_BitCast;
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001241 return TC_Success;
Douglas Gregorf2a55392009-12-22 22:47:22 +00001242 }
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001243
1244 if (destIsScalar)
1245 msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
1246 else if (srcIsScalar)
1247 msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
1248 else
1249 msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
1250
1251 return TC_Failed;
1252 }
1253
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001254 bool destIsPtr = DestType->isAnyPointerType() ||
1255 DestType->isBlockPointerType();
1256 bool srcIsPtr = SrcType->isAnyPointerType() ||
1257 SrcType->isBlockPointerType();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001258 if (!destIsPtr && !srcIsPtr) {
1259 // Except for std::nullptr_t->integer and lvalue->reference, which are
1260 // handled above, at least one of the two arguments must be a pointer.
1261 return TC_NotApplicable;
1262 }
1263
1264 if (SrcType == DestType) {
1265 // C++ 5.2.10p2 has a note that mentions that, subject to all other
1266 // restrictions, a cast to the same type is allowed. The intent is not
1267 // entirely clear here, since all other paragraphs explicitly forbid casts
1268 // to the same type. However, the behavior of compilers is pretty consistent
1269 // on this point: allow same-type conversion if the involved types are
1270 // pointers, disallow otherwise.
John McCall2de56d12010-08-25 11:45:40 +00001271 Kind = CK_NoOp;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001272 return TC_Success;
1273 }
1274
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001275 if (DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001276 assert(srcIsPtr && "One type must be a pointer");
1277 // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
1278 // type large enough to hold it.
1279 if (Self.Context.getTypeSize(SrcType) >
1280 Self.Context.getTypeSize(DestType)) {
1281 msg = diag::err_bad_reinterpret_cast_small_int;
1282 return TC_Failed;
1283 }
John McCall2de56d12010-08-25 11:45:40 +00001284 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001285 return TC_Success;
1286 }
1287
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001288 if (SrcType->isIntegralOrEnumerationType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001289 assert(destIsPtr && "One type must be a pointer");
1290 // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
1291 // converted to a pointer.
John McCall404cd162010-11-13 01:35:44 +00001292 // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not
1293 // necessarily converted to a null pointer value.]
John McCall2de56d12010-08-25 11:45:40 +00001294 Kind = CK_IntegralToPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001295 return TC_Success;
1296 }
1297
1298 if (!destIsPtr || !srcIsPtr) {
1299 // With the valid non-pointer conversions out of the way, we can be even
1300 // more stringent.
1301 return TC_NotApplicable;
1302 }
1303
1304 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
1305 // The C-style cast operator can.
1306 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1307 msg = diag::err_bad_cxx_cast_const_away;
1308 return TC_Failed;
1309 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001310
1311 // Cannot convert between block pointers and Objective-C object pointers.
1312 if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) ||
1313 (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType()))
1314 return TC_NotApplicable;
1315
1316 // Any pointer can be cast to an Objective-C pointer type with a C-style
1317 // cast.
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001318 if (CStyle && DestType->isObjCObjectPointerType()) {
John McCall2de56d12010-08-25 11:45:40 +00001319 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001320 return TC_Success;
1321 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001322
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001323 // Not casting away constness, so the only remaining check is for compatible
1324 // pointer categories.
John McCall2de56d12010-08-25 11:45:40 +00001325 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001326
1327 if (SrcType->isFunctionPointerType()) {
1328 if (DestType->isFunctionPointerType()) {
1329 // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
1330 // a pointer to a function of a different type.
1331 return TC_Success;
1332 }
1333
1334 // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
1335 // an object type or vice versa is conditionally-supported.
1336 // Compilers support it in C++03 too, though, because it's necessary for
1337 // casting the return value of dlsym() and GetProcAddress().
1338 // FIXME: Conditionally-supported behavior should be configurable in the
1339 // TargetInfo or similar.
1340 if (!Self.getLangOptions().CPlusPlus0x)
1341 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1342 return TC_Success;
1343 }
1344
1345 if (DestType->isFunctionPointerType()) {
1346 // See above.
1347 if (!Self.getLangOptions().CPlusPlus0x)
1348 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1349 return TC_Success;
1350 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001351
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001352 // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
1353 // a pointer to an object of different type.
1354 // Void pointers are not specified, but supported by every compiler out there.
1355 // So we finish by allowing everything that remains - it's got to be two
1356 // object pointers.
1357 return TC_Success;
1358}
1359
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001360bool
John McCallf89e55a2010-11-18 06:31:45 +00001361Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, ExprValueKind &VK,
1362 Expr *&CastExpr, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +00001363 CXXCastPath &BasePath,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001364 bool FunctionalStyle) {
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001365 if (CastExpr->isBoundMemberFunction(Context))
1366 return Diag(CastExpr->getLocStart(),
1367 diag::err_invalid_use_of_bound_member_func)
1368 << CastExpr->getSourceRange();
1369
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001370 // This test is outside everything else because it's the only case where
1371 // a non-lvalue-reference target type does not lead to decay.
1372 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Anders Carlssonbb378cb2009-10-18 20:31:03 +00001373 if (CastTy->isVoidType()) {
John McCall2de56d12010-08-25 11:45:40 +00001374 Kind = CK_ToVoid;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001375 return false;
Anders Carlssonbb378cb2009-10-18 20:31:03 +00001376 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001377
John McCall9b4b9d62010-11-30 02:05:44 +00001378 // Make sure we determine the value kind before we bail out for
1379 // dependent types.
1380 VK = Expr::getValueKindForType(CastTy);
1381
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001382 // If the type is dependent, we won't do any other semantic analysis now.
John McCalldaa8e4e2010-11-15 09:13:47 +00001383 if (CastTy->isDependentType() || CastExpr->isTypeDependent()) {
1384 Kind = CK_Dependent;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001385 return false;
John McCalldaa8e4e2010-11-15 09:13:47 +00001386 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001387
John McCallf89e55a2010-11-18 06:31:45 +00001388 if (VK == VK_RValue && !CastTy->isRecordType())
Douglas Gregora873dfc2010-02-03 00:27:59 +00001389 DefaultFunctionArrayLvalueConversion(CastExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001390
1391 // C++ [expr.cast]p5: The conversions performed by
1392 // - a const_cast,
1393 // - a static_cast,
1394 // - a static_cast followed by a const_cast,
1395 // - a reinterpret_cast, or
1396 // - a reinterpret_cast followed by a const_cast,
1397 // can be performed using the cast notation of explicit type conversion.
1398 // [...] If a conversion can be interpreted in more than one of the ways
1399 // listed above, the interpretation that appears first in the list is used,
1400 // even if a cast resulting from that interpretation is ill-formed.
1401 // In plain language, this means trying a const_cast ...
1402 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlssoncb3c3082009-09-01 20:52:42 +00001403 TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,
1404 msg);
Anders Carlssonda921fd2009-10-19 18:14:28 +00001405 if (tcr == TC_Success)
John McCall2de56d12010-08-25 11:45:40 +00001406 Kind = CK_NoOp;
Anders Carlssonda921fd2009-10-19 18:14:28 +00001407
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001408 if (tcr == TC_NotApplicable) {
1409 // ... or if that is not possible, a static_cast, ignoring const, ...
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001410 tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg, Kind,
1411 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001412 if (tcr == TC_NotApplicable) {
1413 // ... and finally a reinterpret_cast, ignoring const.
Anders Carlsson3c31a392009-09-26 00:12:34 +00001414 tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg,
1415 Kind);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001416 }
1417 }
1418
Nick Lewycky43328e92010-11-09 00:19:31 +00001419 if (tcr != TC_Success && msg != 0) {
1420 if (CastExpr->getType() == Context.OverloadTy) {
Douglas Gregor8e960432010-11-08 03:40:48 +00001421 DeclAccessPair Found;
Nick Lewycky43328e92010-11-09 00:19:31 +00001422 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(CastExpr,
Douglas Gregor8e960432010-11-08 03:40:48 +00001423 CastTy,
1424 /* Complain */ true,
1425 Found);
1426 assert(!Fn && "cast failed but able to resolve overload expression!!");
Nick Lewycky43328e92010-11-09 00:19:31 +00001427 (void)Fn;
1428 } else {
Douglas Gregor8e960432010-11-08 03:40:48 +00001429 Diag(R.getBegin(), msg) << (FunctionalStyle ? CT_Functional : CT_CStyle)
1430 << CastExpr->getType() << CastTy << R;
1431 }
1432 }
John McCalle2b76882010-11-16 05:46:29 +00001433 else if (Kind == CK_BitCast)
John McCallb7f4ffe2010-08-12 21:44:57 +00001434 CheckCastAlign(CastExpr, CastTy, R);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001435
1436 return tcr != TC_Success;
1437}