blob: ac679f70096153690c0c6afe2b37aa139093cfa3 [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 McCallf6a16482010-12-04 03:47:34 +0000519 Self.IgnoredValueConversions(SrcExpr);
John McCall2de56d12010-08-25 11:45:40 +0000520 Kind = CK_ToVoid;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000521 return;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000522 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000523
John McCallf89e55a2010-11-18 06:31:45 +0000524 VK = Expr::getValueKindForType(DestType);
525 if (VK == VK_RValue && !DestType->isRecordType())
Douglas Gregora873dfc2010-02-03 00:27:59 +0000526 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000527
528 unsigned msg = diag::err_bad_cxx_cast_generic;
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000529 if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, msg,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000530 Kind, BasePath) != TC_Success && msg != 0)
Douglas Gregor8e960432010-11-08 03:40:48 +0000531 {
John McCalldaa8e4e2010-11-15 09:13:47 +0000532 if (SrcExpr->getType() == Self.Context.OverloadTy)
Douglas Gregor8e960432010-11-08 03:40:48 +0000533 {
534 OverloadExpr* oe = OverloadExpr::find(SrcExpr).Expression;
535 Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
536 << oe->getName() << DestType << OpRange << oe->getQualifierRange();
537 NoteAllOverloadCandidates(SrcExpr, Self);
538 }
539 else
540 Self.Diag(OpRange.getBegin(), msg) << CT_Static
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000541 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregor8e960432010-11-08 03:40:48 +0000542 }
John McCalle2b76882010-11-16 05:46:29 +0000543 else if (Kind == CK_BitCast)
John McCallb7f4ffe2010-08-12 21:44:57 +0000544 Self.CheckCastAlign(SrcExpr, DestType, OpRange);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000545}
546
547/// TryStaticCast - Check if a static cast can be performed, and do so if
548/// possible. If @p CStyle, ignore access restrictions on hierarchy casting
549/// and casting away constness.
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000550static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000551 QualType DestType, bool CStyle,
Anders Carlssoncb3c3082009-09-01 20:52:42 +0000552 const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000553 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000554 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000555 // The order the tests is not entirely arbitrary. There is one conversion
556 // that can be handled in two different ways. Given:
557 // struct A {};
558 // struct B : public A {
559 // B(); B(const A&);
560 // };
561 // const A &a = B();
562 // the cast static_cast<const B&>(a) could be seen as either a static
563 // reference downcast, or an explicit invocation of the user-defined
564 // conversion using B's conversion constructor.
565 // DR 427 specifies that the downcast is to be applied here.
566
567 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
568 // Done outside this function.
569
570 TryCastResult tcr;
571
572 // C++ 5.2.9p5, reference downcast.
573 // See the function for details.
574 // DR 427 specifies that this is to be applied before paragraph 2.
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000575 tcr = TryStaticReferenceDowncast(Self, SrcExpr, DestType, CStyle, OpRange,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000576 msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000577 if (tcr != TC_NotApplicable)
578 return tcr;
579
580 // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
581 // reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
582 tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, msg);
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000583 if (tcr != TC_NotApplicable) {
John McCall2de56d12010-08-25 11:45:40 +0000584 Kind = CK_NoOp;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000585 return tcr;
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000586 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000587
588 // C++ 5.2.9p2: An expression e can be explicitly converted to a type T
589 // [...] if the declaration "T t(e);" is well-formed, [...].
Fariborz Jahaniane9f42082009-08-26 18:55:36 +0000590 tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CStyle, OpRange, msg,
Douglas Gregord6e44a32010-04-16 22:09:46 +0000591 Kind);
Anders Carlsson3c31a392009-09-26 00:12:34 +0000592 if (tcr != TC_NotApplicable)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000593 return tcr;
Anders Carlsson0aebc812009-09-09 21:33:21 +0000594
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000595 // C++ 5.2.9p6: May apply the reverse of any standard conversion, except
596 // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
597 // conversions, subject to further restrictions.
598 // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
599 // of qualification conversions impossible.
600 // In the CStyle case, the earlier attempt to const_cast should have taken
601 // care of reverse qualification conversions.
602
603 QualType OrigSrcType = SrcExpr->getType();
604
605 QualType SrcType = Self.Context.getCanonicalType(SrcExpr->getType());
606
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000607 // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly
608 // converted to an integral type.
609 if (Self.getLangOptions().CPlusPlus0x && SrcType->isEnumeralType()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000610 assert(SrcType->getAs<EnumType>()->getDecl()->isScoped());
611 if (DestType->isBooleanType()) {
612 Kind = CK_IntegralToBoolean;
613 return TC_Success;
614 } else if (DestType->isIntegralType(Self.Context)) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000615 Kind = CK_IntegralCast;
616 return TC_Success;
617 }
618 }
619
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000620 // Reverse integral promotion/conversion. All such conversions are themselves
621 // again integral promotions or conversions and are thus already handled by
622 // p2 (TryDirectInitialization above).
623 // (Note: any data loss warnings should be suppressed.)
624 // The exception is the reverse of enum->integer, i.e. integer->enum (and
625 // enum->enum). See also C++ 5.2.9p7.
626 // The same goes for reverse floating point promotion/conversion and
627 // floating-integral conversions. Again, only floating->enum is relevant.
628 if (DestType->isEnumeralType()) {
629 if (SrcType->isComplexType() || SrcType->isVectorType()) {
630 // Fall through - these cannot be converted.
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000631 } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
John McCall2de56d12010-08-25 11:45:40 +0000632 Kind = CK_IntegralCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000633 return TC_Success;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000634 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000635 }
636
637 // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
638 // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000639 tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000640 Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000641 if (tcr != TC_NotApplicable)
642 return tcr;
643
644 // Reverse member pointer conversion. C++ 4.11 specifies member pointer
645 // conversion. C++ 5.2.9p9 has additional information.
646 // DR54's access restrictions apply here also.
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000647 tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
Anders Carlssoncee22422010-04-24 19:22:20 +0000648 OpRange, msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000649 if (tcr != TC_NotApplicable)
650 return tcr;
651
652 // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
653 // void*. C++ 5.2.9p10 specifies additional restrictions, which really is
654 // just the usual constness stuff.
Ted Kremenek6217b802009-07-29 21:53:49 +0000655 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000656 QualType SrcPointee = SrcPointer->getPointeeType();
657 if (SrcPointee->isVoidType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000658 if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000659 QualType DestPointee = DestPointer->getPointeeType();
660 if (DestPointee->isIncompleteOrObjectType()) {
661 // This is definitely the intended conversion, but it might fail due
662 // to a const violation.
663 if (!CStyle && !DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
664 msg = diag::err_bad_cxx_cast_const_away;
665 return TC_Failed;
666 }
John McCall2de56d12010-08-25 11:45:40 +0000667 Kind = CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000668 return TC_Success;
669 }
670 }
Fariborz Jahanian2f6c5502010-05-10 23:46:53 +0000671 else if (DestType->isObjCObjectPointerType()) {
672 // allow both c-style cast and static_cast of objective-c pointers as
673 // they are pervasive.
John McCall2de56d12010-08-25 11:45:40 +0000674 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +0000675 return TC_Success;
676 }
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000677 else if (CStyle && DestType->isBlockPointerType()) {
678 // allow c-style cast of void * to block pointers.
John McCall2de56d12010-08-25 11:45:40 +0000679 Kind = CK_AnyPointerToBlockPointerCast;
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000680 return TC_Success;
681 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000682 }
683 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000684 // Allow arbitray objective-c pointer conversion with static casts.
685 if (SrcType->isObjCObjectPointerType() &&
John McCalldaa8e4e2010-11-15 09:13:47 +0000686 DestType->isObjCObjectPointerType()) {
687 Kind = CK_BitCast;
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000688 return TC_Success;
John McCalldaa8e4e2010-11-15 09:13:47 +0000689 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000690
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000691 // We tried everything. Everything! Nothing works! :-(
692 return TC_NotApplicable;
693}
694
695/// Tests whether a conversion according to N2844 is valid.
696TryCastResult
697TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
Mike Stump1eb44332009-09-09 15:08:12 +0000698 unsigned &msg) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000699 // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
700 // reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
Ted Kremenek6217b802009-07-29 21:53:49 +0000701 const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000702 if (!R)
703 return TC_NotApplicable;
704
John McCall7eb0a9e2010-11-24 05:12:34 +0000705 if (!SrcExpr->isLValue())
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000706 return TC_NotApplicable;
707
708 // Because we try the reference downcast before this function, from now on
709 // this is the only cast possibility, so we issue an error if we fail now.
710 // FIXME: Should allow casting away constness if CStyle.
711 bool DerivedToBase;
Douglas Gregor569c3162010-08-07 11:51:51 +0000712 bool ObjCConversion;
Douglas Gregor393896f2009-11-05 13:06:35 +0000713 if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
714 SrcExpr->getType(), R->getPointeeType(),
Douglas Gregor569c3162010-08-07 11:51:51 +0000715 DerivedToBase, ObjCConversion) <
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000716 Sema::Ref_Compatible_With_Added_Qualification) {
717 msg = diag::err_bad_lvalue_to_rvalue_cast;
718 return TC_Failed;
719 }
720
Douglas Gregorf0e0b172010-03-25 00:20:38 +0000721 // FIXME: We should probably have an AST node for lvalue-to-rvalue
722 // conversions.
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000723 return TC_Success;
724}
725
726/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
727TryCastResult
728TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
729 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000730 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000731 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000732 // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
733 // cast to type "reference to cv2 D", where D is a class derived from B,
734 // if a valid standard conversion from "pointer to D" to "pointer to B"
735 // exists, cv2 >= cv1, and B is not a virtual base class of D.
736 // In addition, DR54 clarifies that the base must be accessible in the
737 // current context. Although the wording of DR54 only applies to the pointer
738 // variant of this rule, the intent is clearly for it to apply to the this
739 // conversion as well.
740
Ted Kremenek6217b802009-07-29 21:53:49 +0000741 const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000742 if (!DestReference) {
743 return TC_NotApplicable;
744 }
745 bool RValueRef = DestReference->isRValueReferenceType();
John McCall7eb0a9e2010-11-24 05:12:34 +0000746 if (!RValueRef && !SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000747 // We know the left side is an lvalue reference, so we can suggest a reason.
748 msg = diag::err_bad_cxx_cast_rvalue;
749 return TC_NotApplicable;
750 }
751
752 QualType DestPointee = DestReference->getPointeeType();
753
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000754 return TryStaticDowncast(Self,
755 Self.Context.getCanonicalType(SrcExpr->getType()),
756 Self.Context.getCanonicalType(DestPointee), CStyle,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000757 OpRange, SrcExpr->getType(), DestType, msg, Kind,
758 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000759}
760
761/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
762TryCastResult
763TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
Mike Stump1eb44332009-09-09 15:08:12 +0000764 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000765 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000766 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000767 // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
768 // type, can be converted to an rvalue of type "pointer to cv2 D", where D
769 // is a class derived from B, if a valid standard conversion from "pointer
770 // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
771 // class of D.
772 // In addition, DR54 clarifies that the base must be accessible in the
773 // current context.
774
Ted Kremenek6217b802009-07-29 21:53:49 +0000775 const PointerType *DestPointer = DestType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000776 if (!DestPointer) {
777 return TC_NotApplicable;
778 }
779
Ted Kremenek6217b802009-07-29 21:53:49 +0000780 const PointerType *SrcPointer = SrcType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000781 if (!SrcPointer) {
782 msg = diag::err_bad_static_cast_pointer_nonpointer;
783 return TC_NotApplicable;
784 }
785
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000786 return TryStaticDowncast(Self,
787 Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
788 Self.Context.getCanonicalType(DestPointer->getPointeeType()),
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000789 CStyle, OpRange, SrcType, DestType, msg, Kind,
790 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000791}
792
793/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
794/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000795/// DestType is possible and allowed.
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000796TryCastResult
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000797TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000798 bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000799 QualType OrigDestType, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000800 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000801 // We can only work with complete types. But don't complain if it doesn't work
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000802 if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) ||
803 Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0)))
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000804 return TC_NotApplicable;
805
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000806 // Downcast can only happen in class hierarchies, so we need classes.
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000807 if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000808 return TC_NotApplicable;
809 }
810
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000811 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000812 /*DetectVirtual=*/true);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000813 if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
814 return TC_NotApplicable;
815 }
816
817 // Target type does derive from source type. Now we're serious. If an error
818 // appears now, it's not ignored.
819 // This may not be entirely in line with the standard. Take for example:
820 // struct A {};
821 // struct B : virtual A {
822 // B(A&);
823 // };
Mike Stump1eb44332009-09-09 15:08:12 +0000824 //
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000825 // void f()
826 // {
827 // (void)static_cast<const B&>(*((A*)0));
828 // }
829 // As far as the standard is concerned, p5 does not apply (A is virtual), so
830 // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
831 // However, both GCC and Comeau reject this example, and accepting it would
832 // mean more complex code if we're to preserve the nice error message.
833 // FIXME: Being 100% compliant here would be nice to have.
834
835 // Must preserve cv, as always, unless we're in C-style mode.
836 if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
837 msg = diag::err_bad_cxx_cast_const_away;
838 return TC_Failed;
839 }
840
841 if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
842 // This code is analoguous to that in CheckDerivedToBaseConversion, except
843 // that it builds the paths in reverse order.
844 // To sum up: record all paths to the base and build a nice string from
845 // them. Use it to spice up the error message.
846 if (!Paths.isRecordingPaths()) {
847 Paths.clear();
848 Paths.setRecordingPaths(true);
849 Self.IsDerivedFrom(DestType, SrcType, Paths);
850 }
851 std::string PathDisplayStr;
852 std::set<unsigned> DisplayedPaths;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000853 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000854 PI != PE; ++PI) {
855 if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
856 // We haven't displayed a path to this particular base
857 // class subobject yet.
858 PathDisplayStr += "\n ";
Douglas Gregora8f32e02009-10-06 17:59:45 +0000859 for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
860 EE = PI->rend();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000861 EI != EE; ++EI)
862 PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000863 PathDisplayStr += QualType(DestType).getAsString();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000864 }
865 }
866
867 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000868 << QualType(SrcType).getUnqualifiedType()
869 << QualType(DestType).getUnqualifiedType()
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000870 << PathDisplayStr << OpRange;
871 msg = 0;
872 return TC_Failed;
873 }
874
875 if (Paths.getDetectedVirtual() != 0) {
876 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
877 Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
878 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
879 msg = 0;
880 return TC_Failed;
881 }
882
John McCall6b2accb2010-02-10 09:31:12 +0000883 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
John McCall6b2accb2010-02-10 09:31:12 +0000884 SrcType, DestType,
John McCall58e6f342010-03-16 05:22:47 +0000885 Paths.front(),
886 diag::err_downcast_from_inaccessible_base)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000887 msg = 0;
888 return TC_Failed;
889 }
890
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000891 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +0000892 Kind = CK_BaseToDerived;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000893 return TC_Success;
894}
895
896/// TryStaticMemberPointerUpcast - Tests whether a conversion according to
897/// C++ 5.2.9p9 is valid:
898///
899/// An rvalue of type "pointer to member of D of type cv1 T" can be
900/// converted to an rvalue of type "pointer to member of B of type cv2 T",
901/// where B is a base class of D [...].
902///
903TryCastResult
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000904TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr, QualType SrcType,
905 QualType DestType, bool CStyle,
906 const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000907 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000908 CXXCastPath &BasePath) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000909 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000910 if (!DestMemPtr)
911 return TC_NotApplicable;
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000912
913 bool WasOverloadedFunction = false;
John McCall6bb80172010-03-30 21:47:33 +0000914 DeclAccessPair FoundOverload;
Douglas Gregor1a8cf732010-04-14 23:11:21 +0000915 if (SrcExpr->getType() == Self.Context.OverloadTy) {
916 if (FunctionDecl *Fn
917 = Self.ResolveAddressOfOverloadedFunction(SrcExpr, DestType, false,
918 FoundOverload)) {
919 CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
920 SrcType = Self.Context.getMemberPointerType(Fn->getType(),
921 Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
922 WasOverloadedFunction = true;
923 }
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000924 }
Douglas Gregor1a8cf732010-04-14 23:11:21 +0000925
Ted Kremenek6217b802009-07-29 21:53:49 +0000926 const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000927 if (!SrcMemPtr) {
928 msg = diag::err_bad_static_cast_member_pointer_nonmp;
929 return TC_NotApplicable;
930 }
931
932 // T == T, modulo cv
Douglas Gregora4923eb2009-11-16 21:35:15 +0000933 if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
934 DestMemPtr->getPointeeType()))
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000935 return TC_NotApplicable;
936
937 // B base of D
938 QualType SrcClass(SrcMemPtr->getClass(), 0);
939 QualType DestClass(DestMemPtr->getClass(), 0);
Anders Carlssoncee22422010-04-24 19:22:20 +0000940 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000941 /*DetectVirtual=*/true);
942 if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
943 return TC_NotApplicable;
944 }
945
946 // 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 +0000947 if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000948 Paths.clear();
949 Paths.setRecordingPaths(true);
950 bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
951 assert(StillOkay);
952 StillOkay = StillOkay;
953 std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
954 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
955 << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
956 msg = 0;
957 return TC_Failed;
958 }
959
960 if (const RecordType *VBase = Paths.getDetectedVirtual()) {
961 Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
962 << SrcClass << DestClass << QualType(VBase, 0) << OpRange;
963 msg = 0;
964 return TC_Failed;
965 }
966
John McCall6b2accb2010-02-10 09:31:12 +0000967 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
Eli Friedman0fab8cd2010-07-23 19:25:41 +0000968 DestClass, SrcClass,
John McCall58e6f342010-03-16 05:22:47 +0000969 Paths.front(),
970 diag::err_upcast_to_inaccessible_base)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000971 msg = 0;
972 return TC_Failed;
973 }
974
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000975 if (WasOverloadedFunction) {
976 // Resolve the address of the overloaded function again, this time
977 // allowing complaints if something goes wrong.
978 FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr,
979 DestType,
John McCall6bb80172010-03-30 21:47:33 +0000980 true,
981 FoundOverload);
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000982 if (!Fn) {
983 msg = 0;
984 return TC_Failed;
985 }
986
John McCall6bb80172010-03-30 21:47:33 +0000987 SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000988 if (!SrcExpr) {
989 msg = 0;
990 return TC_Failed;
991 }
992 }
993
Anders Carlssoncee22422010-04-24 19:22:20 +0000994 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +0000995 Kind = CK_DerivedToBaseMemberPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000996 return TC_Success;
997}
998
999/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
1000/// is valid:
1001///
1002/// An expression e can be explicitly converted to a type T using a
1003/// @c static_cast if the declaration "T t(e);" is well-formed [...].
1004TryCastResult
Sebastian Redla82e4ae2009-11-14 21:15:49 +00001005TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
Fariborz Jahaniane9f42082009-08-26 18:55:36 +00001006 bool CStyle, const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001007 CastKind &Kind) {
Anders Carlssond851b372009-09-07 18:25:47 +00001008 if (DestType->isRecordType()) {
1009 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
1010 diag::err_bad_dynamic_cast_incomplete)) {
1011 msg = 0;
1012 return TC_Failed;
1013 }
1014 }
Douglas Gregord6e44a32010-04-16 22:09:46 +00001015
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001016 InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
1017 InitializationKind InitKind
Douglas Gregord6e44a32010-04-16 22:09:46 +00001018 = InitializationKind::CreateCast(/*FIXME:*/OpRange,
1019 CStyle);
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001020 InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExpr, 1);
Douglas Gregor8e960432010-11-08 03:40:48 +00001021
1022 // At this point of CheckStaticCast, if the destination is a reference,
1023 // or the expression is an overload expression this has to work.
1024 // There is no other way that works.
1025 // On the other hand, if we're checking a C-style cast, we've still got
1026 // the reinterpret_cast way.
1027
Douglas Gregord6e44a32010-04-16 22:09:46 +00001028 if (InitSeq.getKind() == InitializationSequence::FailedSequence &&
Douglas Gregor8e960432010-11-08 03:40:48 +00001029 (CStyle || !DestType->isReferenceType()))
Anders Carlsson3c31a392009-09-26 00:12:34 +00001030 return TC_NotApplicable;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001031
John McCall60d7b3a2010-08-24 06:29:42 +00001032 ExprResult Result
John McCallf312b1e2010-08-26 23:41:50 +00001033 = InitSeq.Perform(Self, Entity, InitKind, MultiExprArg(Self, &SrcExpr, 1));
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001034 if (Result.isInvalid()) {
1035 msg = 0;
1036 return TC_Failed;
1037 }
1038
Douglas Gregord6e44a32010-04-16 22:09:46 +00001039 if (InitSeq.isConstructorInitialization())
John McCall2de56d12010-08-25 11:45:40 +00001040 Kind = CK_ConstructorConversion;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001041 else
John McCall2de56d12010-08-25 11:45:40 +00001042 Kind = CK_NoOp;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001043
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001044 SrcExpr = Result.takeAs<Expr>();
1045 return TC_Success;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001046}
1047
1048/// TryConstCast - See if a const_cast from source to destination is allowed,
1049/// and perform it if it is.
1050static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
1051 bool CStyle, unsigned &msg) {
1052 DestType = Self.Context.getCanonicalType(DestType);
1053 QualType SrcType = SrcExpr->getType();
1054 if (const LValueReferenceType *DestTypeTmp =
Ted Kremenek6217b802009-07-29 21:53:49 +00001055 DestType->getAs<LValueReferenceType>()) {
John McCall7eb0a9e2010-11-24 05:12:34 +00001056 if (!SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001057 // Cannot const_cast non-lvalue to lvalue reference type. But if this
1058 // is C-style, static_cast might find a way, so we simply suggest a
1059 // message and tell the parent to keep searching.
1060 msg = diag::err_bad_cxx_cast_rvalue;
1061 return TC_NotApplicable;
1062 }
1063
1064 // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
1065 // [...] if a pointer to T1 can be [cast] to the type pointer to T2.
1066 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1067 SrcType = Self.Context.getPointerType(SrcType);
1068 }
1069
1070 // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
1071 // the rules for const_cast are the same as those used for pointers.
1072
John McCalld425d2b2010-05-18 09:35:29 +00001073 if (!DestType->isPointerType() &&
1074 !DestType->isMemberPointerType() &&
1075 !DestType->isObjCObjectPointerType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001076 // Cannot cast to non-pointer, non-reference type. Note that, if DestType
1077 // was a reference type, we converted it to a pointer above.
1078 // The status of rvalue references isn't entirely clear, but it looks like
1079 // conversion to them is simply invalid.
1080 // C++ 5.2.11p3: For two pointer types [...]
1081 if (!CStyle)
1082 msg = diag::err_bad_const_cast_dest;
1083 return TC_NotApplicable;
1084 }
1085 if (DestType->isFunctionPointerType() ||
1086 DestType->isMemberFunctionPointerType()) {
1087 // Cannot cast direct function pointers.
1088 // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
1089 // T is the ultimate pointee of source and target type.
1090 if (!CStyle)
1091 msg = diag::err_bad_const_cast_dest;
1092 return TC_NotApplicable;
1093 }
1094 SrcType = Self.Context.getCanonicalType(SrcType);
1095
1096 // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
1097 // completely equal.
1098 // FIXME: const_cast should probably not be able to convert between pointers
1099 // to different address spaces.
1100 // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
1101 // in multi-level pointers may change, but the level count must be the same,
1102 // as must be the final pointee type.
1103 while (SrcType != DestType &&
Douglas Gregor5a57efd2010-06-09 03:53:18 +00001104 Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) {
Chandler Carruth595e2902009-12-29 08:05:19 +00001105 Qualifiers Quals;
1106 SrcType = Self.Context.getUnqualifiedArrayType(SrcType, Quals);
1107 DestType = Self.Context.getUnqualifiedArrayType(DestType, Quals);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001108 }
1109
1110 // Since we're dealing in canonical types, the remainder must be the same.
1111 if (SrcType != DestType)
1112 return TC_NotApplicable;
1113
1114 return TC_Success;
1115}
1116
Douglas Gregor8e960432010-11-08 03:40:48 +00001117
1118static void NoteAllOverloadCandidates(Expr* const Expr, Sema& sema)
1119{
1120
1121 assert(Expr->getType() == sema.Context.OverloadTy);
1122
1123 OverloadExpr::FindResult Ovl = OverloadExpr::find(Expr);
1124 OverloadExpr *const OvlExpr = Ovl.Expression;
1125
1126 for (UnresolvedSetIterator it = OvlExpr->decls_begin(),
1127 end = OvlExpr->decls_end(); it != end; ++it) {
1128 if ( FunctionTemplateDecl *ftd =
1129 dyn_cast<FunctionTemplateDecl>((*it)->getUnderlyingDecl()) )
1130 {
1131 sema.NoteOverloadCandidate(ftd->getTemplatedDecl());
1132 }
1133 else if ( FunctionDecl *f =
1134 dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl()) )
1135 {
1136 sema.NoteOverloadCandidate(f);
1137 }
1138 }
1139}
1140
1141
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001142static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
1143 QualType DestType, bool CStyle,
1144 const SourceRange &OpRange,
Anders Carlsson3c31a392009-09-26 00:12:34 +00001145 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001146 CastKind &Kind) {
Douglas Gregore39a3892010-07-13 23:17:26 +00001147 bool IsLValueCast = false;
1148
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001149 DestType = Self.Context.getCanonicalType(DestType);
1150 QualType SrcType = SrcExpr->getType();
Douglas Gregor8e960432010-11-08 03:40:48 +00001151
1152 // Is the source an overloaded name? (i.e. &foo)
1153 // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5)
John McCall404cd162010-11-13 01:35:44 +00001154 if (SrcType == Self.Context.OverloadTy)
Douglas Gregor8e960432010-11-08 03:40:48 +00001155 return TC_NotApplicable;
1156
Ted Kremenek6217b802009-07-29 21:53:49 +00001157 if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001158 bool LValue = DestTypeTmp->isLValueReferenceType();
John McCall7eb0a9e2010-11-24 05:12:34 +00001159 if (LValue && !SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001160 // Cannot cast non-lvalue to reference type. See the similar comment in
1161 // const_cast.
1162 msg = diag::err_bad_cxx_cast_rvalue;
1163 return TC_NotApplicable;
1164 }
1165
1166 // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
1167 // same effect as the conversion *reinterpret_cast<T*>(&x) with the
1168 // built-in & and * operators.
1169 // This code does this transformation for the checked types.
1170 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1171 SrcType = Self.Context.getPointerType(SrcType);
Douglas Gregor8e960432010-11-08 03:40:48 +00001172
Douglas Gregore39a3892010-07-13 23:17:26 +00001173 IsLValueCast = true;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001174 }
1175
1176 // Canonicalize source for comparison.
1177 SrcType = Self.Context.getCanonicalType(SrcType);
1178
Ted Kremenek6217b802009-07-29 21:53:49 +00001179 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
1180 *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001181 if (DestMemPtr && SrcMemPtr) {
1182 // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
1183 // can be explicitly converted to an rvalue of type "pointer to member
1184 // of Y of type T2" if T1 and T2 are both function types or both object
1185 // types.
1186 if (DestMemPtr->getPointeeType()->isFunctionType() !=
1187 SrcMemPtr->getPointeeType()->isFunctionType())
1188 return TC_NotApplicable;
1189
1190 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
1191 // constness.
1192 // A reinterpret_cast followed by a const_cast can, though, so in C-style,
1193 // we accept it.
1194 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1195 msg = diag::err_bad_cxx_cast_const_away;
1196 return TC_Failed;
1197 }
1198
Charles Davisf231df32010-08-16 05:30:44 +00001199 // Don't allow casting between member pointers of different sizes.
1200 if (Self.Context.getTypeSize(DestMemPtr) !=
1201 Self.Context.getTypeSize(SrcMemPtr)) {
1202 msg = diag::err_bad_cxx_cast_member_pointer_size;
1203 return TC_Failed;
1204 }
1205
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001206 // A valid member pointer cast.
John McCall2de56d12010-08-25 11:45:40 +00001207 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001208 return TC_Success;
1209 }
1210
1211 // See below for the enumeral issue.
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001212 if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001213 // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
1214 // type large enough to hold it. A value of std::nullptr_t can be
1215 // converted to an integral type; the conversion has the same meaning
1216 // and validity as a conversion of (void*)0 to the integral type.
1217 if (Self.Context.getTypeSize(SrcType) >
1218 Self.Context.getTypeSize(DestType)) {
1219 msg = diag::err_bad_reinterpret_cast_small_int;
1220 return TC_Failed;
1221 }
John McCall2de56d12010-08-25 11:45:40 +00001222 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001223 return TC_Success;
1224 }
1225
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001226 bool destIsVector = DestType->isVectorType();
1227 bool srcIsVector = SrcType->isVectorType();
1228 if (srcIsVector || destIsVector) {
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001229 // FIXME: Should this also apply to floating point types?
1230 bool srcIsScalar = SrcType->isIntegralType(Self.Context);
1231 bool destIsScalar = DestType->isIntegralType(Self.Context);
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001232
1233 // Check if this is a cast between a vector and something else.
1234 if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
1235 !(srcIsVector && destIsVector))
1236 return TC_NotApplicable;
1237
1238 // If both types have the same size, we can successfully cast.
Douglas Gregorf2a55392009-12-22 22:47:22 +00001239 if (Self.Context.getTypeSize(SrcType)
1240 == Self.Context.getTypeSize(DestType)) {
John McCall2de56d12010-08-25 11:45:40 +00001241 Kind = CK_BitCast;
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001242 return TC_Success;
Douglas Gregorf2a55392009-12-22 22:47:22 +00001243 }
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001244
1245 if (destIsScalar)
1246 msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
1247 else if (srcIsScalar)
1248 msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
1249 else
1250 msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
1251
1252 return TC_Failed;
1253 }
1254
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001255 bool destIsPtr = DestType->isAnyPointerType() ||
1256 DestType->isBlockPointerType();
1257 bool srcIsPtr = SrcType->isAnyPointerType() ||
1258 SrcType->isBlockPointerType();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001259 if (!destIsPtr && !srcIsPtr) {
1260 // Except for std::nullptr_t->integer and lvalue->reference, which are
1261 // handled above, at least one of the two arguments must be a pointer.
1262 return TC_NotApplicable;
1263 }
1264
1265 if (SrcType == DestType) {
1266 // C++ 5.2.10p2 has a note that mentions that, subject to all other
1267 // restrictions, a cast to the same type is allowed. The intent is not
1268 // entirely clear here, since all other paragraphs explicitly forbid casts
1269 // to the same type. However, the behavior of compilers is pretty consistent
1270 // on this point: allow same-type conversion if the involved types are
1271 // pointers, disallow otherwise.
John McCall2de56d12010-08-25 11:45:40 +00001272 Kind = CK_NoOp;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001273 return TC_Success;
1274 }
1275
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001276 if (DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001277 assert(srcIsPtr && "One type must be a pointer");
1278 // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
1279 // type large enough to hold it.
1280 if (Self.Context.getTypeSize(SrcType) >
1281 Self.Context.getTypeSize(DestType)) {
1282 msg = diag::err_bad_reinterpret_cast_small_int;
1283 return TC_Failed;
1284 }
John McCall2de56d12010-08-25 11:45:40 +00001285 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001286 return TC_Success;
1287 }
1288
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001289 if (SrcType->isIntegralOrEnumerationType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001290 assert(destIsPtr && "One type must be a pointer");
1291 // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
1292 // converted to a pointer.
John McCall404cd162010-11-13 01:35:44 +00001293 // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not
1294 // necessarily converted to a null pointer value.]
John McCall2de56d12010-08-25 11:45:40 +00001295 Kind = CK_IntegralToPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001296 return TC_Success;
1297 }
1298
1299 if (!destIsPtr || !srcIsPtr) {
1300 // With the valid non-pointer conversions out of the way, we can be even
1301 // more stringent.
1302 return TC_NotApplicable;
1303 }
1304
1305 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
1306 // The C-style cast operator can.
1307 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1308 msg = diag::err_bad_cxx_cast_const_away;
1309 return TC_Failed;
1310 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001311
1312 // Cannot convert between block pointers and Objective-C object pointers.
1313 if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) ||
1314 (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType()))
1315 return TC_NotApplicable;
1316
1317 // Any pointer can be cast to an Objective-C pointer type with a C-style
1318 // cast.
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001319 if (CStyle && DestType->isObjCObjectPointerType()) {
John McCall2de56d12010-08-25 11:45:40 +00001320 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001321 return TC_Success;
1322 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001323
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001324 // Not casting away constness, so the only remaining check is for compatible
1325 // pointer categories.
John McCall2de56d12010-08-25 11:45:40 +00001326 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001327
1328 if (SrcType->isFunctionPointerType()) {
1329 if (DestType->isFunctionPointerType()) {
1330 // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
1331 // a pointer to a function of a different type.
1332 return TC_Success;
1333 }
1334
1335 // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
1336 // an object type or vice versa is conditionally-supported.
1337 // Compilers support it in C++03 too, though, because it's necessary for
1338 // casting the return value of dlsym() and GetProcAddress().
1339 // FIXME: Conditionally-supported behavior should be configurable in the
1340 // TargetInfo or similar.
1341 if (!Self.getLangOptions().CPlusPlus0x)
1342 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1343 return TC_Success;
1344 }
1345
1346 if (DestType->isFunctionPointerType()) {
1347 // See above.
1348 if (!Self.getLangOptions().CPlusPlus0x)
1349 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1350 return TC_Success;
1351 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001352
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001353 // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
1354 // a pointer to an object of different type.
1355 // Void pointers are not specified, but supported by every compiler out there.
1356 // So we finish by allowing everything that remains - it's got to be two
1357 // object pointers.
1358 return TC_Success;
1359}
1360
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001361bool
John McCallf89e55a2010-11-18 06:31:45 +00001362Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, ExprValueKind &VK,
1363 Expr *&CastExpr, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +00001364 CXXCastPath &BasePath,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001365 bool FunctionalStyle) {
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001366 if (CastExpr->isBoundMemberFunction(Context))
1367 return Diag(CastExpr->getLocStart(),
1368 diag::err_invalid_use_of_bound_member_func)
1369 << CastExpr->getSourceRange();
1370
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001371 // This test is outside everything else because it's the only case where
1372 // a non-lvalue-reference target type does not lead to decay.
1373 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Anders Carlssonbb378cb2009-10-18 20:31:03 +00001374 if (CastTy->isVoidType()) {
John McCallf6a16482010-12-04 03:47:34 +00001375 IgnoredValueConversions(CastExpr);
John McCall2de56d12010-08-25 11:45:40 +00001376 Kind = CK_ToVoid;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001377 return false;
Anders Carlssonbb378cb2009-10-18 20:31:03 +00001378 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001379
John McCall9b4b9d62010-11-30 02:05:44 +00001380 // Make sure we determine the value kind before we bail out for
1381 // dependent types.
1382 VK = Expr::getValueKindForType(CastTy);
1383
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001384 // If the type is dependent, we won't do any other semantic analysis now.
John McCalldaa8e4e2010-11-15 09:13:47 +00001385 if (CastTy->isDependentType() || CastExpr->isTypeDependent()) {
1386 Kind = CK_Dependent;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001387 return false;
John McCalldaa8e4e2010-11-15 09:13:47 +00001388 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001389
John McCallf89e55a2010-11-18 06:31:45 +00001390 if (VK == VK_RValue && !CastTy->isRecordType())
Douglas Gregora873dfc2010-02-03 00:27:59 +00001391 DefaultFunctionArrayLvalueConversion(CastExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001392
1393 // C++ [expr.cast]p5: The conversions performed by
1394 // - a const_cast,
1395 // - a static_cast,
1396 // - a static_cast followed by a const_cast,
1397 // - a reinterpret_cast, or
1398 // - a reinterpret_cast followed by a const_cast,
1399 // can be performed using the cast notation of explicit type conversion.
1400 // [...] If a conversion can be interpreted in more than one of the ways
1401 // listed above, the interpretation that appears first in the list is used,
1402 // even if a cast resulting from that interpretation is ill-formed.
1403 // In plain language, this means trying a const_cast ...
1404 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlssoncb3c3082009-09-01 20:52:42 +00001405 TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,
1406 msg);
Anders Carlssonda921fd2009-10-19 18:14:28 +00001407 if (tcr == TC_Success)
John McCall2de56d12010-08-25 11:45:40 +00001408 Kind = CK_NoOp;
Anders Carlssonda921fd2009-10-19 18:14:28 +00001409
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001410 if (tcr == TC_NotApplicable) {
1411 // ... or if that is not possible, a static_cast, ignoring const, ...
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001412 tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg, Kind,
1413 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001414 if (tcr == TC_NotApplicable) {
1415 // ... and finally a reinterpret_cast, ignoring const.
Anders Carlsson3c31a392009-09-26 00:12:34 +00001416 tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg,
1417 Kind);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001418 }
1419 }
1420
Nick Lewycky43328e92010-11-09 00:19:31 +00001421 if (tcr != TC_Success && msg != 0) {
1422 if (CastExpr->getType() == Context.OverloadTy) {
Douglas Gregor8e960432010-11-08 03:40:48 +00001423 DeclAccessPair Found;
Nick Lewycky43328e92010-11-09 00:19:31 +00001424 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(CastExpr,
Douglas Gregor8e960432010-11-08 03:40:48 +00001425 CastTy,
1426 /* Complain */ true,
1427 Found);
1428 assert(!Fn && "cast failed but able to resolve overload expression!!");
Nick Lewycky43328e92010-11-09 00:19:31 +00001429 (void)Fn;
1430 } else {
Douglas Gregor8e960432010-11-08 03:40:48 +00001431 Diag(R.getBegin(), msg) << (FunctionalStyle ? CT_Functional : CT_CStyle)
1432 << CastExpr->getType() << CastTy << R;
1433 }
1434 }
John McCalle2b76882010-11-16 05:46:29 +00001435 else if (Kind == CK_BitCast)
John McCallb7f4ffe2010-08-12 21:44:57 +00001436 CheckCastAlign(CastExpr, CastTy, R);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001437
1438 return tcr != TC_Success;
1439}