blob: ba1c3b0f407c3dea01c290b597bad761c3d57baf [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),
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000175 VK, Ex, DestTInfo, OpLoc,
176 Parens.getEnd()));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000177
Anders Carlsson714179b2009-08-02 19:07:59 +0000178 case tok::kw_dynamic_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000179 CastKind Kind = CK_Dependent;
John McCallf871d0c2010-08-07 06:22:56 +0000180 CXXCastPath BasePath;
Douglas Gregor9103bb22008-12-17 22:52:20 +0000181 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000182 CheckDynamicCast(*this, Ex, DestType, VK, OpRange, DestRange,
183 Kind, BasePath);
John McCallf871d0c2010-08-07 06:22:56 +0000184 return Owned(CXXDynamicCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000185 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000186 VK, Kind, Ex, &BasePath, DestTInfo,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000187 OpLoc, Parens.getEnd()));
Anders Carlsson714179b2009-08-02 19:07:59 +0000188 }
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000189 case tok::kw_reinterpret_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000190 CastKind Kind = CK_Dependent;
Douglas Gregor9103bb22008-12-17 22:52:20 +0000191 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000192 CheckReinterpretCast(*this, Ex, DestType, VK, OpRange, DestRange, Kind);
John McCallf871d0c2010-08-07 06:22:56 +0000193 return Owned(CXXReinterpretCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000194 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000195 VK, Kind, Ex, 0,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000196 DestTInfo, OpLoc, Parens.getEnd()));
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000197 }
Anders Carlssoncdb61972009-08-07 22:21:05 +0000198 case tok::kw_static_cast: {
John McCalldaa8e4e2010-11-15 09:13:47 +0000199 CastKind Kind = CK_Dependent;
John McCallf871d0c2010-08-07 06:22:56 +0000200 CXXCastPath BasePath;
Douglas Gregord6e44a32010-04-16 22:09:46 +0000201 if (!TypeDependent)
John McCallf89e55a2010-11-18 06:31:45 +0000202 CheckStaticCast(*this, Ex, DestType, VK, OpRange, Kind, BasePath);
Anders Carlsson0aebc812009-09-09 21:33:21 +0000203
John McCallf871d0c2010-08-07 06:22:56 +0000204 return Owned(CXXStaticCastExpr::Create(Context,
Douglas Gregor63982352010-07-13 18:40:04 +0000205 DestType.getNonLValueExprType(Context),
John McCallf89e55a2010-11-18 06:31:45 +0000206 VK, Kind, Ex, &BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000207 DestTInfo, OpLoc, Parens.getEnd()));
Anders Carlssoncdb61972009-08-07 22:21:05 +0000208 }
Sebastian Redl26d85b12008-11-05 21:50:06 +0000209 }
210
Sebastian Redlf53597f2009-03-15 17:47:39 +0000211 return ExprError();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000212}
213
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000214/// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes,
215/// this removes one level of indirection from both types, provided that they're
216/// the same kind of pointer (plain or to-member). Unlike the Sema function,
217/// this one doesn't care if the two pointers-to-member don't point into the
218/// same class. This is because CastsAwayConstness doesn't care.
Dan Gohman3c46e8d2010-07-26 21:25:24 +0000219static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) {
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000220 const PointerType *T1PtrType = T1->getAs<PointerType>(),
221 *T2PtrType = T2->getAs<PointerType>();
222 if (T1PtrType && T2PtrType) {
223 T1 = T1PtrType->getPointeeType();
224 T2 = T2PtrType->getPointeeType();
225 return true;
226 }
Fariborz Jahanian72a86592010-02-03 20:32:31 +0000227 const ObjCObjectPointerType *T1ObjCPtrType =
228 T1->getAs<ObjCObjectPointerType>(),
229 *T2ObjCPtrType =
230 T2->getAs<ObjCObjectPointerType>();
231 if (T1ObjCPtrType) {
232 if (T2ObjCPtrType) {
233 T1 = T1ObjCPtrType->getPointeeType();
234 T2 = T2ObjCPtrType->getPointeeType();
235 return true;
236 }
237 else if (T2PtrType) {
238 T1 = T1ObjCPtrType->getPointeeType();
239 T2 = T2PtrType->getPointeeType();
240 return true;
241 }
242 }
243 else if (T2ObjCPtrType) {
244 if (T1PtrType) {
245 T2 = T2ObjCPtrType->getPointeeType();
246 T1 = T1PtrType->getPointeeType();
247 return true;
248 }
249 }
250
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000251 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
252 *T2MPType = T2->getAs<MemberPointerType>();
253 if (T1MPType && T2MPType) {
254 T1 = T1MPType->getPointeeType();
255 T2 = T2MPType->getPointeeType();
256 return true;
257 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000258
259 const BlockPointerType *T1BPType = T1->getAs<BlockPointerType>(),
260 *T2BPType = T2->getAs<BlockPointerType>();
261 if (T1BPType && T2BPType) {
262 T1 = T1BPType->getPointeeType();
263 T2 = T2BPType->getPointeeType();
264 return true;
265 }
266
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000267 return false;
268}
269
Sebastian Redldb647282009-01-27 23:18:31 +0000270/// CastsAwayConstness - Check if the pointer conversion from SrcType to
271/// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by
272/// the cast checkers. Both arguments must denote pointer (possibly to member)
273/// types.
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000274static bool
Mike Stump1eb44332009-09-09 15:08:12 +0000275CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) {
Sebastian Redldb647282009-01-27 23:18:31 +0000276 // Casting away constness is defined in C++ 5.2.11p8 with reference to
277 // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since
278 // the rules are non-trivial. So first we construct Tcv *...cv* as described
279 // in C++ 5.2.11p8.
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000280 assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() ||
281 SrcType->isBlockPointerType()) &&
Sebastian Redldb647282009-01-27 23:18:31 +0000282 "Source type is not pointer or pointer to member.");
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000283 assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() ||
284 DestType->isBlockPointerType()) &&
Sebastian Redldb647282009-01-27 23:18:31 +0000285 "Destination type is not pointer or pointer to member.");
Sebastian Redl26d85b12008-11-05 21:50:06 +0000286
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000287 QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType),
288 UnwrappedDestType = Self.Context.getCanonicalType(DestType);
John McCall0953e762009-09-24 19:53:00 +0000289 llvm::SmallVector<Qualifiers, 8> cv1, cv2;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000290
291 // Find the qualifications.
Sebastian Redl76d69bb2009-11-18 18:10:53 +0000292 while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
Anders Carlsson52647c62010-06-04 22:47:55 +0000293 Qualifiers SrcQuals;
294 Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals);
295 cv1.push_back(SrcQuals);
296
297 Qualifiers DestQuals;
298 Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals);
299 cv2.push_back(DestQuals);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000300 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +0000301 if (cv1.empty())
302 return false;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000303
304 // Construct void pointers with those qualifiers (in reverse order of
305 // unwrapping, of course).
Sebastian Redl37d6de32008-11-08 13:00:26 +0000306 QualType SrcConstruct = Self.Context.VoidTy;
307 QualType DestConstruct = Self.Context.VoidTy;
John McCall0953e762009-09-24 19:53:00 +0000308 ASTContext &Context = Self.Context;
309 for (llvm::SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(),
310 i2 = cv2.rbegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000311 i1 != cv1.rend(); ++i1, ++i2) {
John McCall0953e762009-09-24 19:53:00 +0000312 SrcConstruct
313 = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1));
314 DestConstruct
315 = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000316 }
317
318 // Test if they're compatible.
319 return SrcConstruct != DestConstruct &&
Sebastian Redl37d6de32008-11-08 13:00:26 +0000320 !Self.IsQualificationConversion(SrcConstruct, DestConstruct);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000321}
322
Sebastian Redl26d85b12008-11-05 21:50:06 +0000323/// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid.
324/// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime-
325/// checked downcasts in class hierarchies.
Anders Carlsson714179b2009-08-02 19:07:59 +0000326static void
Sebastian Redl37d6de32008-11-08 13:00:26 +0000327CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000328 ExprValueKind &VK, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000329 const SourceRange &DestRange, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000330 CXXCastPath &BasePath) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000331 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
Sebastian Redl37d6de32008-11-08 13:00:26 +0000332 DestType = Self.Context.getCanonicalType(DestType);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000333
334 // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type,
335 // or "pointer to cv void".
336
337 QualType DestPointee;
Ted Kremenek6217b802009-07-29 21:53:49 +0000338 const PointerType *DestPointer = DestType->getAs<PointerType>();
John McCallf89e55a2010-11-18 06:31:45 +0000339 const ReferenceType *DestReference = 0;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000340 if (DestPointer) {
341 DestPointee = DestPointer->getPointeeType();
John McCallf89e55a2010-11-18 06:31:45 +0000342 } else if ((DestReference = DestType->getAs<ReferenceType>())) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000343 DestPointee = DestReference->getPointeeType();
John McCallf89e55a2010-11-18 06:31:45 +0000344 VK = isa<LValueReferenceType>(DestReference) ? VK_LValue : VK_RValue;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000345 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000346 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
Chris Lattnerd1625842008-11-24 06:25:27 +0000347 << OrigDestType << DestRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000348 return;
349 }
350
Ted Kremenek6217b802009-07-29 21:53:49 +0000351 const RecordType *DestRecord = DestPointee->getAs<RecordType>();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000352 if (DestPointee->isVoidType()) {
353 assert(DestPointer && "Reference to void is not possible");
354 } else if (DestRecord) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000355 if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000356 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssonb7906612009-08-26 23:45:07 +0000357 << DestRange))
Sebastian Redl26d85b12008-11-05 21:50:06 +0000358 return;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000359 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000360 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattnerd1625842008-11-24 06:25:27 +0000361 << DestPointee.getUnqualifiedType() << DestRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000362 return;
363 }
364
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000365 // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
366 // complete class type, [...]. If T is an lvalue reference type, v shall be
367 // an lvalue of a complete class type, [...]. If T is an rvalue reference
368 // type, v shall be an expression having a complete effective class type,
369 // [...]
Sebastian Redl26d85b12008-11-05 21:50:06 +0000370
Sebastian Redl37d6de32008-11-08 13:00:26 +0000371 QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
Sebastian Redl26d85b12008-11-05 21:50:06 +0000372 QualType SrcPointee;
373 if (DestPointer) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000374 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl26d85b12008-11-05 21:50:06 +0000375 SrcPointee = SrcPointer->getPointeeType();
376 } else {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000377 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
Chris Lattnerd1625842008-11-24 06:25:27 +0000378 << OrigSrcType << SrcExpr->getSourceRange();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000379 return;
380 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000381 } else if (DestReference->isLValueReferenceType()) {
John McCall7eb0a9e2010-11-24 05:12:34 +0000382 if (!SrcExpr->isLValue()) {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000383 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000384 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000385 }
386 SrcPointee = SrcType;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000387 } else {
388 SrcPointee = SrcType;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000389 }
390
Ted Kremenek6217b802009-07-29 21:53:49 +0000391 const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000392 if (SrcRecord) {
Douglas Gregor86447ec2009-03-09 16:13:40 +0000393 if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000394 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssonb7906612009-08-26 23:45:07 +0000395 << SrcExpr->getSourceRange()))
Sebastian Redl26d85b12008-11-05 21:50:06 +0000396 return;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000397 } else {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000398 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattnerd1625842008-11-24 06:25:27 +0000399 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redl26d85b12008-11-05 21:50:06 +0000400 return;
401 }
402
403 assert((DestPointer || DestReference) &&
404 "Bad destination non-ptr/ref slipped through.");
405 assert((DestRecord || DestPointee->isVoidType()) &&
406 "Bad destination pointee slipped through.");
407 assert(SrcRecord && "Bad source pointee slipped through.");
408
409 // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness.
410 if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000411 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000412 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000413 return;
414 }
415
416 // C++ 5.2.7p3: If the type of v is the same as the required result type,
417 // [except for cv].
418 if (DestRecord == SrcRecord) {
John McCall2de56d12010-08-25 11:45:40 +0000419 Kind = CK_NoOp;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000420 return;
421 }
422
423 // C++ 5.2.7p5
424 // Upcasts are resolved statically.
Sebastian Redl37d6de32008-11-08 13:00:26 +0000425 if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000426 if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
427 OpRange.getBegin(), OpRange,
428 &BasePath))
429 return;
430
John McCall2de56d12010-08-25 11:45:40 +0000431 Kind = CK_DerivedToBase;
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000432
433 // If we are casting to or through a virtual base class, we need a
434 // vtable.
435 if (Self.BasePathInvolvesVirtualBase(BasePath))
436 Self.MarkVTableUsed(OpRange.getBegin(),
437 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000438 return;
439 }
440
441 // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
Douglas Gregor952b0172010-02-11 01:04:33 +0000442 const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition();
Sebastian Redld93f0dd2008-11-06 15:59:35 +0000443 assert(SrcDecl && "Definition missing");
444 if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000445 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
Chris Lattnerd1625842008-11-24 06:25:27 +0000446 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redld93f0dd2008-11-06 15:59:35 +0000447 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000448 Self.MarkVTableUsed(OpRange.getBegin(),
449 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl26d85b12008-11-05 21:50:06 +0000450
451 // Done. Everything else is run-time checks.
John McCall2de56d12010-08-25 11:45:40 +0000452 Kind = CK_Dynamic;
Sebastian Redl26d85b12008-11-05 21:50:06 +0000453}
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000454
455/// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
456/// Refer to C++ 5.2.11 for details. const_cast is typically used in code
457/// like this:
458/// const char *str = "literal";
459/// legacy_function(const_cast\<char*\>(str));
460void
John McCallf89e55a2010-11-18 06:31:45 +0000461CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType, ExprValueKind &VK,
Mike Stump1eb44332009-09-09 15:08:12 +0000462 const SourceRange &OpRange, const SourceRange &DestRange) {
John McCallf89e55a2010-11-18 06:31:45 +0000463 VK = Expr::getValueKindForType(DestType);
464 if (VK == VK_RValue)
Douglas Gregora873dfc2010-02-03 00:27:59 +0000465 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000466
467 unsigned msg = diag::err_bad_cxx_cast_generic;
468 if (TryConstCast(Self, SrcExpr, DestType, /*CStyle*/false, msg) != TC_Success
469 && msg != 0)
470 Self.Diag(OpRange.getBegin(), msg) << CT_Const
471 << SrcExpr->getType() << DestType << OpRange;
472}
473
474/// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is
475/// valid.
476/// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code
477/// like this:
478/// char *bytes = reinterpret_cast\<char*\>(int_ptr);
479void
480CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000481 ExprValueKind &VK, const SourceRange &OpRange,
482 const SourceRange &DestRange, CastKind &Kind) {
483 VK = Expr::getValueKindForType(DestType);
484 if (VK == VK_RValue)
Douglas Gregora873dfc2010-02-03 00:27:59 +0000485 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000486
487 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlsson3c31a392009-09-26 00:12:34 +0000488 if (TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange,
489 msg, Kind)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000490 != TC_Success && msg != 0)
Douglas Gregor8e960432010-11-08 03:40:48 +0000491 {
492 if (SrcExpr->getType() == Self.Context.OverloadTy)
493 {
494 //FIXME: &f<int>; is overloaded and resolvable
495 Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload)
496 << OverloadExpr::find(SrcExpr).Expression->getName()
497 << DestType << OpRange;
498 NoteAllOverloadCandidates(SrcExpr, Self);
499
500 }
501 else
502 Self.Diag(OpRange.getBegin(), msg) << CT_Reinterpret
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000503 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregor8e960432010-11-08 03:40:48 +0000504 }
505
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000506}
507
508
509/// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid.
510/// Refer to C++ 5.2.9 for details. Static casts are mostly used for making
511/// implicit conversions explicit and getting rid of data loss warnings.
512void
513CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCallf89e55a2010-11-18 06:31:45 +0000514 ExprValueKind &VK, const SourceRange &OpRange,
515 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000516 // This test is outside everything else because it's the only case where
517 // a non-lvalue-reference target type does not lead to decay.
518 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000519 if (DestType->isVoidType()) {
John McCallf6a16482010-12-04 03:47:34 +0000520 Self.IgnoredValueConversions(SrcExpr);
John McCall2de56d12010-08-25 11:45:40 +0000521 Kind = CK_ToVoid;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000522 return;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000523 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000524
John McCallf89e55a2010-11-18 06:31:45 +0000525 VK = Expr::getValueKindForType(DestType);
526 if (VK == VK_RValue && !DestType->isRecordType())
Douglas Gregora873dfc2010-02-03 00:27:59 +0000527 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000528
529 unsigned msg = diag::err_bad_cxx_cast_generic;
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000530 if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, msg,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000531 Kind, BasePath) != TC_Success && msg != 0)
Douglas Gregor8e960432010-11-08 03:40:48 +0000532 {
John McCalldaa8e4e2010-11-15 09:13:47 +0000533 if (SrcExpr->getType() == Self.Context.OverloadTy)
Douglas Gregor8e960432010-11-08 03:40:48 +0000534 {
535 OverloadExpr* oe = OverloadExpr::find(SrcExpr).Expression;
536 Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
537 << oe->getName() << DestType << OpRange << oe->getQualifierRange();
538 NoteAllOverloadCandidates(SrcExpr, Self);
539 }
540 else
541 Self.Diag(OpRange.getBegin(), msg) << CT_Static
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000542 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregor8e960432010-11-08 03:40:48 +0000543 }
John McCalle2b76882010-11-16 05:46:29 +0000544 else if (Kind == CK_BitCast)
John McCallb7f4ffe2010-08-12 21:44:57 +0000545 Self.CheckCastAlign(SrcExpr, DestType, OpRange);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000546}
547
548/// TryStaticCast - Check if a static cast can be performed, and do so if
549/// possible. If @p CStyle, ignore access restrictions on hierarchy casting
550/// and casting away constness.
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000551static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000552 QualType DestType, bool CStyle,
Anders Carlssoncb3c3082009-09-01 20:52:42 +0000553 const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000554 CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000555 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000556 // The order the tests is not entirely arbitrary. There is one conversion
557 // that can be handled in two different ways. Given:
558 // struct A {};
559 // struct B : public A {
560 // B(); B(const A&);
561 // };
562 // const A &a = B();
563 // the cast static_cast<const B&>(a) could be seen as either a static
564 // reference downcast, or an explicit invocation of the user-defined
565 // conversion using B's conversion constructor.
566 // DR 427 specifies that the downcast is to be applied here.
567
568 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
569 // Done outside this function.
570
571 TryCastResult tcr;
572
573 // C++ 5.2.9p5, reference downcast.
574 // See the function for details.
575 // DR 427 specifies that this is to be applied before paragraph 2.
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000576 tcr = TryStaticReferenceDowncast(Self, SrcExpr, DestType, CStyle, OpRange,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000577 msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000578 if (tcr != TC_NotApplicable)
579 return tcr;
580
581 // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
582 // reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
583 tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, msg);
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000584 if (tcr != TC_NotApplicable) {
John McCall2de56d12010-08-25 11:45:40 +0000585 Kind = CK_NoOp;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000586 return tcr;
Sebastian Redla82e4ae2009-11-14 21:15:49 +0000587 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000588
589 // C++ 5.2.9p2: An expression e can be explicitly converted to a type T
590 // [...] if the declaration "T t(e);" is well-formed, [...].
Fariborz Jahaniane9f42082009-08-26 18:55:36 +0000591 tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CStyle, OpRange, msg,
Douglas Gregord6e44a32010-04-16 22:09:46 +0000592 Kind);
Anders Carlsson3c31a392009-09-26 00:12:34 +0000593 if (tcr != TC_NotApplicable)
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000594 return tcr;
Anders Carlsson0aebc812009-09-09 21:33:21 +0000595
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000596 // C++ 5.2.9p6: May apply the reverse of any standard conversion, except
597 // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
598 // conversions, subject to further restrictions.
599 // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
600 // of qualification conversions impossible.
601 // In the CStyle case, the earlier attempt to const_cast should have taken
602 // care of reverse qualification conversions.
603
604 QualType OrigSrcType = SrcExpr->getType();
605
606 QualType SrcType = Self.Context.getCanonicalType(SrcExpr->getType());
607
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000608 // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly
609 // converted to an integral type.
610 if (Self.getLangOptions().CPlusPlus0x && SrcType->isEnumeralType()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000611 assert(SrcType->getAs<EnumType>()->getDecl()->isScoped());
612 if (DestType->isBooleanType()) {
613 Kind = CK_IntegralToBoolean;
614 return TC_Success;
615 } else if (DestType->isIntegralType(Self.Context)) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000616 Kind = CK_IntegralCast;
617 return TC_Success;
618 }
619 }
620
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000621 // Reverse integral promotion/conversion. All such conversions are themselves
622 // again integral promotions or conversions and are thus already handled by
623 // p2 (TryDirectInitialization above).
624 // (Note: any data loss warnings should be suppressed.)
625 // The exception is the reverse of enum->integer, i.e. integer->enum (and
626 // enum->enum). See also C++ 5.2.9p7.
627 // The same goes for reverse floating point promotion/conversion and
628 // floating-integral conversions. Again, only floating->enum is relevant.
629 if (DestType->isEnumeralType()) {
630 if (SrcType->isComplexType() || SrcType->isVectorType()) {
631 // Fall through - these cannot be converted.
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000632 } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
John McCall2de56d12010-08-25 11:45:40 +0000633 Kind = CK_IntegralCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000634 return TC_Success;
Eli Friedman05d9d7a2009-11-16 05:44:20 +0000635 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000636 }
637
638 // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
639 // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000640 tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000641 Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000642 if (tcr != TC_NotApplicable)
643 return tcr;
644
645 // Reverse member pointer conversion. C++ 4.11 specifies member pointer
646 // conversion. C++ 5.2.9p9 has additional information.
647 // DR54's access restrictions apply here also.
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000648 tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
Anders Carlssoncee22422010-04-24 19:22:20 +0000649 OpRange, msg, Kind, BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000650 if (tcr != TC_NotApplicable)
651 return tcr;
652
653 // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
654 // void*. C++ 5.2.9p10 specifies additional restrictions, which really is
655 // just the usual constness stuff.
Ted Kremenek6217b802009-07-29 21:53:49 +0000656 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000657 QualType SrcPointee = SrcPointer->getPointeeType();
658 if (SrcPointee->isVoidType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000659 if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000660 QualType DestPointee = DestPointer->getPointeeType();
661 if (DestPointee->isIncompleteOrObjectType()) {
662 // This is definitely the intended conversion, but it might fail due
663 // to a const violation.
664 if (!CStyle && !DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
665 msg = diag::err_bad_cxx_cast_const_away;
666 return TC_Failed;
667 }
John McCall2de56d12010-08-25 11:45:40 +0000668 Kind = CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000669 return TC_Success;
670 }
671 }
Fariborz Jahanian2f6c5502010-05-10 23:46:53 +0000672 else if (DestType->isObjCObjectPointerType()) {
673 // allow both c-style cast and static_cast of objective-c pointers as
674 // they are pervasive.
John McCall2de56d12010-08-25 11:45:40 +0000675 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +0000676 return TC_Success;
677 }
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000678 else if (CStyle && DestType->isBlockPointerType()) {
679 // allow c-style cast of void * to block pointers.
John McCall2de56d12010-08-25 11:45:40 +0000680 Kind = CK_AnyPointerToBlockPointerCast;
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000681 return TC_Success;
682 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000683 }
684 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000685 // Allow arbitray objective-c pointer conversion with static casts.
686 if (SrcType->isObjCObjectPointerType() &&
John McCalldaa8e4e2010-11-15 09:13:47 +0000687 DestType->isObjCObjectPointerType()) {
688 Kind = CK_BitCast;
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000689 return TC_Success;
John McCalldaa8e4e2010-11-15 09:13:47 +0000690 }
Fariborz Jahanian65267b22010-05-12 18:16:59 +0000691
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000692 // We tried everything. Everything! Nothing works! :-(
693 return TC_NotApplicable;
694}
695
696/// Tests whether a conversion according to N2844 is valid.
697TryCastResult
698TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
Mike Stump1eb44332009-09-09 15:08:12 +0000699 unsigned &msg) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000700 // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
701 // reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
Ted Kremenek6217b802009-07-29 21:53:49 +0000702 const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000703 if (!R)
704 return TC_NotApplicable;
705
John McCall7eb0a9e2010-11-24 05:12:34 +0000706 if (!SrcExpr->isLValue())
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000707 return TC_NotApplicable;
708
709 // Because we try the reference downcast before this function, from now on
710 // this is the only cast possibility, so we issue an error if we fail now.
711 // FIXME: Should allow casting away constness if CStyle.
712 bool DerivedToBase;
Douglas Gregor569c3162010-08-07 11:51:51 +0000713 bool ObjCConversion;
Douglas Gregor393896f2009-11-05 13:06:35 +0000714 if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
715 SrcExpr->getType(), R->getPointeeType(),
Douglas Gregor569c3162010-08-07 11:51:51 +0000716 DerivedToBase, ObjCConversion) <
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000717 Sema::Ref_Compatible_With_Added_Qualification) {
718 msg = diag::err_bad_lvalue_to_rvalue_cast;
719 return TC_Failed;
720 }
721
Douglas Gregorf0e0b172010-03-25 00:20:38 +0000722 // FIXME: We should probably have an AST node for lvalue-to-rvalue
723 // conversions.
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000724 return TC_Success;
725}
726
727/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
728TryCastResult
729TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
730 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000731 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000732 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000733 // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
734 // cast to type "reference to cv2 D", where D is a class derived from B,
735 // if a valid standard conversion from "pointer to D" to "pointer to B"
736 // exists, cv2 >= cv1, and B is not a virtual base class of D.
737 // In addition, DR54 clarifies that the base must be accessible in the
738 // current context. Although the wording of DR54 only applies to the pointer
739 // variant of this rule, the intent is clearly for it to apply to the this
740 // conversion as well.
741
Ted Kremenek6217b802009-07-29 21:53:49 +0000742 const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000743 if (!DestReference) {
744 return TC_NotApplicable;
745 }
746 bool RValueRef = DestReference->isRValueReferenceType();
John McCall7eb0a9e2010-11-24 05:12:34 +0000747 if (!RValueRef && !SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000748 // We know the left side is an lvalue reference, so we can suggest a reason.
749 msg = diag::err_bad_cxx_cast_rvalue;
750 return TC_NotApplicable;
751 }
752
753 QualType DestPointee = DestReference->getPointeeType();
754
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000755 return TryStaticDowncast(Self,
756 Self.Context.getCanonicalType(SrcExpr->getType()),
757 Self.Context.getCanonicalType(DestPointee), CStyle,
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000758 OpRange, SrcExpr->getType(), DestType, msg, Kind,
759 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000760}
761
762/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
763TryCastResult
764TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
Mike Stump1eb44332009-09-09 15:08:12 +0000765 bool CStyle, const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000766 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000767 CXXCastPath &BasePath) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000768 // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
769 // type, can be converted to an rvalue of type "pointer to cv2 D", where D
770 // is a class derived from B, if a valid standard conversion from "pointer
771 // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
772 // class of D.
773 // In addition, DR54 clarifies that the base must be accessible in the
774 // current context.
775
Ted Kremenek6217b802009-07-29 21:53:49 +0000776 const PointerType *DestPointer = DestType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000777 if (!DestPointer) {
778 return TC_NotApplicable;
779 }
780
Ted Kremenek6217b802009-07-29 21:53:49 +0000781 const PointerType *SrcPointer = SrcType->getAs<PointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000782 if (!SrcPointer) {
783 msg = diag::err_bad_static_cast_pointer_nonpointer;
784 return TC_NotApplicable;
785 }
786
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000787 return TryStaticDowncast(Self,
788 Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
789 Self.Context.getCanonicalType(DestPointer->getPointeeType()),
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000790 CStyle, OpRange, SrcType, DestType, msg, Kind,
791 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000792}
793
794/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
795/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000796/// DestType is possible and allowed.
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000797TryCastResult
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000798TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000799 bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
Anders Carlsson95c5d8a2009-11-12 16:53:16 +0000800 QualType OrigDestType, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +0000801 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000802 // We can only work with complete types. But don't complain if it doesn't work
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000803 if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) ||
804 Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0)))
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000805 return TC_NotApplicable;
806
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000807 // Downcast can only happen in class hierarchies, so we need classes.
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000808 if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000809 return TC_NotApplicable;
810 }
811
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000812 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000813 /*DetectVirtual=*/true);
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000814 if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
815 return TC_NotApplicable;
816 }
817
818 // Target type does derive from source type. Now we're serious. If an error
819 // appears now, it's not ignored.
820 // This may not be entirely in line with the standard. Take for example:
821 // struct A {};
822 // struct B : virtual A {
823 // B(A&);
824 // };
Mike Stump1eb44332009-09-09 15:08:12 +0000825 //
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000826 // void f()
827 // {
828 // (void)static_cast<const B&>(*((A*)0));
829 // }
830 // As far as the standard is concerned, p5 does not apply (A is virtual), so
831 // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
832 // However, both GCC and Comeau reject this example, and accepting it would
833 // mean more complex code if we're to preserve the nice error message.
834 // FIXME: Being 100% compliant here would be nice to have.
835
836 // Must preserve cv, as always, unless we're in C-style mode.
837 if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
838 msg = diag::err_bad_cxx_cast_const_away;
839 return TC_Failed;
840 }
841
842 if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
843 // This code is analoguous to that in CheckDerivedToBaseConversion, except
844 // that it builds the paths in reverse order.
845 // To sum up: record all paths to the base and build a nice string from
846 // them. Use it to spice up the error message.
847 if (!Paths.isRecordingPaths()) {
848 Paths.clear();
849 Paths.setRecordingPaths(true);
850 Self.IsDerivedFrom(DestType, SrcType, Paths);
851 }
852 std::string PathDisplayStr;
853 std::set<unsigned> DisplayedPaths;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000854 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000855 PI != PE; ++PI) {
856 if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
857 // We haven't displayed a path to this particular base
858 // class subobject yet.
859 PathDisplayStr += "\n ";
Douglas Gregora8f32e02009-10-06 17:59:45 +0000860 for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
861 EE = PI->rend();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000862 EI != EE; ++EI)
863 PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000864 PathDisplayStr += QualType(DestType).getAsString();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000865 }
866 }
867
868 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000869 << QualType(SrcType).getUnqualifiedType()
870 << QualType(DestType).getUnqualifiedType()
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000871 << PathDisplayStr << OpRange;
872 msg = 0;
873 return TC_Failed;
874 }
875
876 if (Paths.getDetectedVirtual() != 0) {
877 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
878 Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
879 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
880 msg = 0;
881 return TC_Failed;
882 }
883
John McCall6b2accb2010-02-10 09:31:12 +0000884 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
John McCall6b2accb2010-02-10 09:31:12 +0000885 SrcType, DestType,
John McCall58e6f342010-03-16 05:22:47 +0000886 Paths.front(),
887 diag::err_downcast_from_inaccessible_base)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000888 msg = 0;
889 return TC_Failed;
890 }
891
Anders Carlssonf9d68e12010-04-24 19:36:51 +0000892 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +0000893 Kind = CK_BaseToDerived;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000894 return TC_Success;
895}
896
897/// TryStaticMemberPointerUpcast - Tests whether a conversion according to
898/// C++ 5.2.9p9 is valid:
899///
900/// An rvalue of type "pointer to member of D of type cv1 T" can be
901/// converted to an rvalue of type "pointer to member of B of type cv2 T",
902/// where B is a base class of D [...].
903///
904TryCastResult
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000905TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr, QualType SrcType,
906 QualType DestType, bool CStyle,
907 const SourceRange &OpRange,
John McCall2de56d12010-08-25 11:45:40 +0000908 unsigned &msg, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +0000909 CXXCastPath &BasePath) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000910 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000911 if (!DestMemPtr)
912 return TC_NotApplicable;
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000913
914 bool WasOverloadedFunction = false;
John McCall6bb80172010-03-30 21:47:33 +0000915 DeclAccessPair FoundOverload;
Douglas Gregor1a8cf732010-04-14 23:11:21 +0000916 if (SrcExpr->getType() == Self.Context.OverloadTy) {
917 if (FunctionDecl *Fn
918 = Self.ResolveAddressOfOverloadedFunction(SrcExpr, DestType, false,
919 FoundOverload)) {
920 CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
921 SrcType = Self.Context.getMemberPointerType(Fn->getType(),
922 Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
923 WasOverloadedFunction = true;
924 }
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000925 }
Douglas Gregor1a8cf732010-04-14 23:11:21 +0000926
Ted Kremenek6217b802009-07-29 21:53:49 +0000927 const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000928 if (!SrcMemPtr) {
929 msg = diag::err_bad_static_cast_member_pointer_nonmp;
930 return TC_NotApplicable;
931 }
932
933 // T == T, modulo cv
Douglas Gregora4923eb2009-11-16 21:35:15 +0000934 if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
935 DestMemPtr->getPointeeType()))
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000936 return TC_NotApplicable;
937
938 // B base of D
939 QualType SrcClass(SrcMemPtr->getClass(), 0);
940 QualType DestClass(DestMemPtr->getClass(), 0);
Anders Carlssoncee22422010-04-24 19:22:20 +0000941 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000942 /*DetectVirtual=*/true);
943 if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
944 return TC_NotApplicable;
945 }
946
947 // 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 +0000948 if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000949 Paths.clear();
950 Paths.setRecordingPaths(true);
951 bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
952 assert(StillOkay);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +0000953 (void)StillOkay;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000954 std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
955 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
956 << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
957 msg = 0;
958 return TC_Failed;
959 }
960
961 if (const RecordType *VBase = Paths.getDetectedVirtual()) {
962 Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
963 << SrcClass << DestClass << QualType(VBase, 0) << OpRange;
964 msg = 0;
965 return TC_Failed;
966 }
967
John McCall6b2accb2010-02-10 09:31:12 +0000968 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
Eli Friedman0fab8cd2010-07-23 19:25:41 +0000969 DestClass, SrcClass,
John McCall58e6f342010-03-16 05:22:47 +0000970 Paths.front(),
971 diag::err_upcast_to_inaccessible_base)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000972 msg = 0;
973 return TC_Failed;
974 }
975
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000976 if (WasOverloadedFunction) {
977 // Resolve the address of the overloaded function again, this time
978 // allowing complaints if something goes wrong.
979 FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr,
980 DestType,
John McCall6bb80172010-03-30 21:47:33 +0000981 true,
982 FoundOverload);
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000983 if (!Fn) {
984 msg = 0;
985 return TC_Failed;
986 }
987
John McCall6bb80172010-03-30 21:47:33 +0000988 SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000989 if (!SrcExpr) {
990 msg = 0;
991 return TC_Failed;
992 }
993 }
994
Anders Carlssoncee22422010-04-24 19:22:20 +0000995 Self.BuildBasePathArray(Paths, BasePath);
John McCall2de56d12010-08-25 11:45:40 +0000996 Kind = CK_DerivedToBaseMemberPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000997 return TC_Success;
998}
999
1000/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
1001/// is valid:
1002///
1003/// An expression e can be explicitly converted to a type T using a
1004/// @c static_cast if the declaration "T t(e);" is well-formed [...].
1005TryCastResult
Sebastian Redla82e4ae2009-11-14 21:15:49 +00001006TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
Fariborz Jahaniane9f42082009-08-26 18:55:36 +00001007 bool CStyle, const SourceRange &OpRange, unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001008 CastKind &Kind) {
Anders Carlssond851b372009-09-07 18:25:47 +00001009 if (DestType->isRecordType()) {
1010 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
1011 diag::err_bad_dynamic_cast_incomplete)) {
1012 msg = 0;
1013 return TC_Failed;
1014 }
1015 }
Douglas Gregord6e44a32010-04-16 22:09:46 +00001016
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001017 InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
1018 InitializationKind InitKind
Douglas Gregord6e44a32010-04-16 22:09:46 +00001019 = InitializationKind::CreateCast(/*FIXME:*/OpRange,
1020 CStyle);
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001021 InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExpr, 1);
Douglas Gregor8e960432010-11-08 03:40:48 +00001022
1023 // At this point of CheckStaticCast, if the destination is a reference,
1024 // or the expression is an overload expression this has to work.
1025 // There is no other way that works.
1026 // On the other hand, if we're checking a C-style cast, we've still got
1027 // the reinterpret_cast way.
1028
Douglas Gregord6e44a32010-04-16 22:09:46 +00001029 if (InitSeq.getKind() == InitializationSequence::FailedSequence &&
Douglas Gregor8e960432010-11-08 03:40:48 +00001030 (CStyle || !DestType->isReferenceType()))
Anders Carlsson3c31a392009-09-26 00:12:34 +00001031 return TC_NotApplicable;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001032
John McCall60d7b3a2010-08-24 06:29:42 +00001033 ExprResult Result
John McCallf312b1e2010-08-26 23:41:50 +00001034 = InitSeq.Perform(Self, Entity, InitKind, MultiExprArg(Self, &SrcExpr, 1));
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001035 if (Result.isInvalid()) {
1036 msg = 0;
1037 return TC_Failed;
1038 }
1039
Douglas Gregord6e44a32010-04-16 22:09:46 +00001040 if (InitSeq.isConstructorInitialization())
John McCall2de56d12010-08-25 11:45:40 +00001041 Kind = CK_ConstructorConversion;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001042 else
John McCall2de56d12010-08-25 11:45:40 +00001043 Kind = CK_NoOp;
Douglas Gregord6e44a32010-04-16 22:09:46 +00001044
Douglas Gregorf0e43e52010-04-16 19:30:02 +00001045 SrcExpr = Result.takeAs<Expr>();
1046 return TC_Success;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001047}
1048
1049/// TryConstCast - See if a const_cast from source to destination is allowed,
1050/// and perform it if it is.
1051static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
1052 bool CStyle, unsigned &msg) {
1053 DestType = Self.Context.getCanonicalType(DestType);
1054 QualType SrcType = SrcExpr->getType();
1055 if (const LValueReferenceType *DestTypeTmp =
Ted Kremenek6217b802009-07-29 21:53:49 +00001056 DestType->getAs<LValueReferenceType>()) {
John McCall7eb0a9e2010-11-24 05:12:34 +00001057 if (!SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001058 // Cannot const_cast non-lvalue to lvalue reference type. But if this
1059 // is C-style, static_cast might find a way, so we simply suggest a
1060 // message and tell the parent to keep searching.
1061 msg = diag::err_bad_cxx_cast_rvalue;
1062 return TC_NotApplicable;
1063 }
1064
1065 // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
1066 // [...] if a pointer to T1 can be [cast] to the type pointer to T2.
1067 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1068 SrcType = Self.Context.getPointerType(SrcType);
1069 }
1070
1071 // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
1072 // the rules for const_cast are the same as those used for pointers.
1073
John McCalld425d2b2010-05-18 09:35:29 +00001074 if (!DestType->isPointerType() &&
1075 !DestType->isMemberPointerType() &&
1076 !DestType->isObjCObjectPointerType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001077 // Cannot cast to non-pointer, non-reference type. Note that, if DestType
1078 // was a reference type, we converted it to a pointer above.
1079 // The status of rvalue references isn't entirely clear, but it looks like
1080 // conversion to them is simply invalid.
1081 // C++ 5.2.11p3: For two pointer types [...]
1082 if (!CStyle)
1083 msg = diag::err_bad_const_cast_dest;
1084 return TC_NotApplicable;
1085 }
1086 if (DestType->isFunctionPointerType() ||
1087 DestType->isMemberFunctionPointerType()) {
1088 // Cannot cast direct function pointers.
1089 // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
1090 // T is the ultimate pointee of source and target type.
1091 if (!CStyle)
1092 msg = diag::err_bad_const_cast_dest;
1093 return TC_NotApplicable;
1094 }
1095 SrcType = Self.Context.getCanonicalType(SrcType);
1096
1097 // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
1098 // completely equal.
1099 // FIXME: const_cast should probably not be able to convert between pointers
1100 // to different address spaces.
1101 // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
1102 // in multi-level pointers may change, but the level count must be the same,
1103 // as must be the final pointee type.
1104 while (SrcType != DestType &&
Douglas Gregor5a57efd2010-06-09 03:53:18 +00001105 Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) {
Chandler Carruth595e2902009-12-29 08:05:19 +00001106 Qualifiers Quals;
1107 SrcType = Self.Context.getUnqualifiedArrayType(SrcType, Quals);
1108 DestType = Self.Context.getUnqualifiedArrayType(DestType, Quals);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001109 }
1110
1111 // Since we're dealing in canonical types, the remainder must be the same.
1112 if (SrcType != DestType)
1113 return TC_NotApplicable;
1114
1115 return TC_Success;
1116}
1117
Douglas Gregor8e960432010-11-08 03:40:48 +00001118
1119static void NoteAllOverloadCandidates(Expr* const Expr, Sema& sema)
1120{
1121
1122 assert(Expr->getType() == sema.Context.OverloadTy);
1123
1124 OverloadExpr::FindResult Ovl = OverloadExpr::find(Expr);
1125 OverloadExpr *const OvlExpr = Ovl.Expression;
1126
1127 for (UnresolvedSetIterator it = OvlExpr->decls_begin(),
1128 end = OvlExpr->decls_end(); it != end; ++it) {
1129 if ( FunctionTemplateDecl *ftd =
1130 dyn_cast<FunctionTemplateDecl>((*it)->getUnderlyingDecl()) )
1131 {
1132 sema.NoteOverloadCandidate(ftd->getTemplatedDecl());
1133 }
1134 else if ( FunctionDecl *f =
1135 dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl()) )
1136 {
1137 sema.NoteOverloadCandidate(f);
1138 }
1139 }
1140}
1141
1142
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001143static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
1144 QualType DestType, bool CStyle,
1145 const SourceRange &OpRange,
Anders Carlsson3c31a392009-09-26 00:12:34 +00001146 unsigned &msg,
John McCall2de56d12010-08-25 11:45:40 +00001147 CastKind &Kind) {
Douglas Gregore39a3892010-07-13 23:17:26 +00001148 bool IsLValueCast = false;
1149
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001150 DestType = Self.Context.getCanonicalType(DestType);
1151 QualType SrcType = SrcExpr->getType();
Douglas Gregor8e960432010-11-08 03:40:48 +00001152
1153 // Is the source an overloaded name? (i.e. &foo)
1154 // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5)
John McCall404cd162010-11-13 01:35:44 +00001155 if (SrcType == Self.Context.OverloadTy)
Douglas Gregor8e960432010-11-08 03:40:48 +00001156 return TC_NotApplicable;
1157
Ted Kremenek6217b802009-07-29 21:53:49 +00001158 if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001159 bool LValue = DestTypeTmp->isLValueReferenceType();
John McCall7eb0a9e2010-11-24 05:12:34 +00001160 if (LValue && !SrcExpr->isLValue()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001161 // Cannot cast non-lvalue to reference type. See the similar comment in
1162 // const_cast.
1163 msg = diag::err_bad_cxx_cast_rvalue;
1164 return TC_NotApplicable;
1165 }
1166
1167 // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
1168 // same effect as the conversion *reinterpret_cast<T*>(&x) with the
1169 // built-in & and * operators.
1170 // This code does this transformation for the checked types.
1171 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1172 SrcType = Self.Context.getPointerType(SrcType);
Douglas Gregor8e960432010-11-08 03:40:48 +00001173
Douglas Gregore39a3892010-07-13 23:17:26 +00001174 IsLValueCast = true;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001175 }
1176
1177 // Canonicalize source for comparison.
1178 SrcType = Self.Context.getCanonicalType(SrcType);
1179
Ted Kremenek6217b802009-07-29 21:53:49 +00001180 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
1181 *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001182 if (DestMemPtr && SrcMemPtr) {
1183 // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
1184 // can be explicitly converted to an rvalue of type "pointer to member
1185 // of Y of type T2" if T1 and T2 are both function types or both object
1186 // types.
1187 if (DestMemPtr->getPointeeType()->isFunctionType() !=
1188 SrcMemPtr->getPointeeType()->isFunctionType())
1189 return TC_NotApplicable;
1190
1191 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
1192 // constness.
1193 // A reinterpret_cast followed by a const_cast can, though, so in C-style,
1194 // we accept it.
1195 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1196 msg = diag::err_bad_cxx_cast_const_away;
1197 return TC_Failed;
1198 }
1199
Charles Davisf231df32010-08-16 05:30:44 +00001200 // Don't allow casting between member pointers of different sizes.
1201 if (Self.Context.getTypeSize(DestMemPtr) !=
1202 Self.Context.getTypeSize(SrcMemPtr)) {
1203 msg = diag::err_bad_cxx_cast_member_pointer_size;
1204 return TC_Failed;
1205 }
1206
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001207 // A valid member pointer cast.
John McCall2de56d12010-08-25 11:45:40 +00001208 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001209 return TC_Success;
1210 }
1211
1212 // See below for the enumeral issue.
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001213 if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001214 // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
1215 // type large enough to hold it. A value of std::nullptr_t can be
1216 // converted to an integral type; the conversion has the same meaning
1217 // and validity as a conversion of (void*)0 to the integral type.
1218 if (Self.Context.getTypeSize(SrcType) >
1219 Self.Context.getTypeSize(DestType)) {
1220 msg = diag::err_bad_reinterpret_cast_small_int;
1221 return TC_Failed;
1222 }
John McCall2de56d12010-08-25 11:45:40 +00001223 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001224 return TC_Success;
1225 }
1226
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001227 bool destIsVector = DestType->isVectorType();
1228 bool srcIsVector = SrcType->isVectorType();
1229 if (srcIsVector || destIsVector) {
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001230 // FIXME: Should this also apply to floating point types?
1231 bool srcIsScalar = SrcType->isIntegralType(Self.Context);
1232 bool destIsScalar = DestType->isIntegralType(Self.Context);
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001233
1234 // Check if this is a cast between a vector and something else.
1235 if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
1236 !(srcIsVector && destIsVector))
1237 return TC_NotApplicable;
1238
1239 // If both types have the same size, we can successfully cast.
Douglas Gregorf2a55392009-12-22 22:47:22 +00001240 if (Self.Context.getTypeSize(SrcType)
1241 == Self.Context.getTypeSize(DestType)) {
John McCall2de56d12010-08-25 11:45:40 +00001242 Kind = CK_BitCast;
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001243 return TC_Success;
Douglas Gregorf2a55392009-12-22 22:47:22 +00001244 }
Anders Carlsson0de51bc2009-09-16 19:19:43 +00001245
1246 if (destIsScalar)
1247 msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
1248 else if (srcIsScalar)
1249 msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
1250 else
1251 msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
1252
1253 return TC_Failed;
1254 }
1255
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001256 bool destIsPtr = DestType->isAnyPointerType() ||
1257 DestType->isBlockPointerType();
1258 bool srcIsPtr = SrcType->isAnyPointerType() ||
1259 SrcType->isBlockPointerType();
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001260 if (!destIsPtr && !srcIsPtr) {
1261 // Except for std::nullptr_t->integer and lvalue->reference, which are
1262 // handled above, at least one of the two arguments must be a pointer.
1263 return TC_NotApplicable;
1264 }
1265
1266 if (SrcType == DestType) {
1267 // C++ 5.2.10p2 has a note that mentions that, subject to all other
1268 // restrictions, a cast to the same type is allowed. The intent is not
1269 // entirely clear here, since all other paragraphs explicitly forbid casts
1270 // to the same type. However, the behavior of compilers is pretty consistent
1271 // on this point: allow same-type conversion if the involved types are
1272 // pointers, disallow otherwise.
John McCall2de56d12010-08-25 11:45:40 +00001273 Kind = CK_NoOp;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001274 return TC_Success;
1275 }
1276
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001277 if (DestType->isIntegralType(Self.Context)) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001278 assert(srcIsPtr && "One type must be a pointer");
1279 // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
1280 // type large enough to hold it.
1281 if (Self.Context.getTypeSize(SrcType) >
1282 Self.Context.getTypeSize(DestType)) {
1283 msg = diag::err_bad_reinterpret_cast_small_int;
1284 return TC_Failed;
1285 }
John McCall2de56d12010-08-25 11:45:40 +00001286 Kind = CK_PointerToIntegral;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001287 return TC_Success;
1288 }
1289
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001290 if (SrcType->isIntegralOrEnumerationType()) {
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001291 assert(destIsPtr && "One type must be a pointer");
1292 // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
1293 // converted to a pointer.
John McCall404cd162010-11-13 01:35:44 +00001294 // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not
1295 // necessarily converted to a null pointer value.]
John McCall2de56d12010-08-25 11:45:40 +00001296 Kind = CK_IntegralToPointer;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001297 return TC_Success;
1298 }
1299
1300 if (!destIsPtr || !srcIsPtr) {
1301 // With the valid non-pointer conversions out of the way, we can be even
1302 // more stringent.
1303 return TC_NotApplicable;
1304 }
1305
1306 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
1307 // The C-style cast operator can.
1308 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1309 msg = diag::err_bad_cxx_cast_const_away;
1310 return TC_Failed;
1311 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001312
1313 // Cannot convert between block pointers and Objective-C object pointers.
1314 if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) ||
1315 (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType()))
1316 return TC_NotApplicable;
1317
1318 // Any pointer can be cast to an Objective-C pointer type with a C-style
1319 // cast.
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001320 if (CStyle && DestType->isObjCObjectPointerType()) {
John McCall2de56d12010-08-25 11:45:40 +00001321 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian92ef5d72009-12-08 23:09:15 +00001322 return TC_Success;
1323 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001324
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001325 // Not casting away constness, so the only remaining check is for compatible
1326 // pointer categories.
John McCall2de56d12010-08-25 11:45:40 +00001327 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001328
1329 if (SrcType->isFunctionPointerType()) {
1330 if (DestType->isFunctionPointerType()) {
1331 // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
1332 // a pointer to a function of a different type.
1333 return TC_Success;
1334 }
1335
1336 // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
1337 // an object type or vice versa is conditionally-supported.
1338 // Compilers support it in C++03 too, though, because it's necessary for
1339 // casting the return value of dlsym() and GetProcAddress().
1340 // FIXME: Conditionally-supported behavior should be configurable in the
1341 // TargetInfo or similar.
1342 if (!Self.getLangOptions().CPlusPlus0x)
1343 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1344 return TC_Success;
1345 }
1346
1347 if (DestType->isFunctionPointerType()) {
1348 // See above.
1349 if (!Self.getLangOptions().CPlusPlus0x)
1350 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1351 return TC_Success;
1352 }
Douglas Gregorbf9fb882010-07-08 20:27:32 +00001353
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001354 // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
1355 // a pointer to an object of different type.
1356 // Void pointers are not specified, but supported by every compiler out there.
1357 // So we finish by allowing everything that remains - it's got to be two
1358 // object pointers.
1359 return TC_Success;
1360}
1361
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001362bool
John McCallf89e55a2010-11-18 06:31:45 +00001363Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, ExprValueKind &VK,
1364 Expr *&CastExpr, CastKind &Kind,
John McCallf871d0c2010-08-07 06:22:56 +00001365 CXXCastPath &BasePath,
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001366 bool FunctionalStyle) {
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001367 if (CastExpr->isBoundMemberFunction(Context))
1368 return Diag(CastExpr->getLocStart(),
1369 diag::err_invalid_use_of_bound_member_func)
1370 << CastExpr->getSourceRange();
1371
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001372 // This test is outside everything else because it's the only case where
1373 // a non-lvalue-reference target type does not lead to decay.
1374 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Anders Carlssonbb378cb2009-10-18 20:31:03 +00001375 if (CastTy->isVoidType()) {
John McCallf6a16482010-12-04 03:47:34 +00001376 IgnoredValueConversions(CastExpr);
John McCall2de56d12010-08-25 11:45:40 +00001377 Kind = CK_ToVoid;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001378 return false;
Anders Carlssonbb378cb2009-10-18 20:31:03 +00001379 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001380
John McCall9b4b9d62010-11-30 02:05:44 +00001381 // Make sure we determine the value kind before we bail out for
1382 // dependent types.
1383 VK = Expr::getValueKindForType(CastTy);
1384
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001385 // If the type is dependent, we won't do any other semantic analysis now.
John McCalldaa8e4e2010-11-15 09:13:47 +00001386 if (CastTy->isDependentType() || CastExpr->isTypeDependent()) {
1387 Kind = CK_Dependent;
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001388 return false;
John McCalldaa8e4e2010-11-15 09:13:47 +00001389 }
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001390
John McCallf89e55a2010-11-18 06:31:45 +00001391 if (VK == VK_RValue && !CastTy->isRecordType())
Douglas Gregora873dfc2010-02-03 00:27:59 +00001392 DefaultFunctionArrayLvalueConversion(CastExpr);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001393
1394 // C++ [expr.cast]p5: The conversions performed by
1395 // - a const_cast,
1396 // - a static_cast,
1397 // - a static_cast followed by a const_cast,
1398 // - a reinterpret_cast, or
1399 // - a reinterpret_cast followed by a const_cast,
1400 // can be performed using the cast notation of explicit type conversion.
1401 // [...] If a conversion can be interpreted in more than one of the ways
1402 // listed above, the interpretation that appears first in the list is used,
1403 // even if a cast resulting from that interpretation is ill-formed.
1404 // In plain language, this means trying a const_cast ...
1405 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlssoncb3c3082009-09-01 20:52:42 +00001406 TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,
1407 msg);
Anders Carlssonda921fd2009-10-19 18:14:28 +00001408 if (tcr == TC_Success)
John McCall2de56d12010-08-25 11:45:40 +00001409 Kind = CK_NoOp;
Anders Carlssonda921fd2009-10-19 18:14:28 +00001410
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001411 if (tcr == TC_NotApplicable) {
1412 // ... or if that is not possible, a static_cast, ignoring const, ...
Anders Carlsson5cf86ba2010-04-24 19:06:50 +00001413 tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg, Kind,
1414 BasePath);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001415 if (tcr == TC_NotApplicable) {
1416 // ... and finally a reinterpret_cast, ignoring const.
Anders Carlsson3c31a392009-09-26 00:12:34 +00001417 tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg,
1418 Kind);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001419 }
1420 }
1421
Nick Lewycky43328e92010-11-09 00:19:31 +00001422 if (tcr != TC_Success && msg != 0) {
1423 if (CastExpr->getType() == Context.OverloadTy) {
Douglas Gregor8e960432010-11-08 03:40:48 +00001424 DeclAccessPair Found;
Nick Lewycky43328e92010-11-09 00:19:31 +00001425 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(CastExpr,
Douglas Gregor8e960432010-11-08 03:40:48 +00001426 CastTy,
1427 /* Complain */ true,
1428 Found);
1429 assert(!Fn && "cast failed but able to resolve overload expression!!");
Nick Lewycky43328e92010-11-09 00:19:31 +00001430 (void)Fn;
1431 } else {
Douglas Gregor8e960432010-11-08 03:40:48 +00001432 Diag(R.getBegin(), msg) << (FunctionalStyle ? CT_Functional : CT_CStyle)
1433 << CastExpr->getType() << CastTy << R;
1434 }
1435 }
John McCalle2b76882010-11-16 05:46:29 +00001436 else if (Kind == CK_BitCast)
John McCallb7f4ffe2010-08-12 21:44:57 +00001437 CheckCastAlign(CastExpr, CastTy, R);
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001438
1439 return tcr != TC_Success;
1440}