blob: 075deda4dd9438a28ff82a44cabd19bca551a3b1 [file] [log] [blame]
Sebastian Redl3c5aa4d2008-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 McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000015#include "clang/Sema/Initialization.h"
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +000016#include "clang/AST/ExprCXX.h"
17#include "clang/AST/ASTContext.h"
Douglas Gregor36d1b142009-10-06 17:59:45 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssond624e162009-08-26 23:45:07 +000019#include "clang/Basic/PartialDiagnostic.h"
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +000020#include "llvm/ADT/SmallVector.h"
Sebastian Redl015085f2008-11-07 23:29:29 +000021#include <set>
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +000022using namespace clang;
23
Douglas Gregore81f58e2010-11-08 03:40:48 +000024
25static void NoteAllOverloadCandidates(Expr* const Expr, Sema& sema);
26
Sebastian Redl9f831db2009-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 Redl842ef522008-11-08 13:00:26 +000041};
42
Douglas Gregore81f58e2010-11-08 03:40:48 +000043
44
45
Sebastian Redl842ef522008-11-08 13:00:26 +000046static void CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCall7decc9e2010-11-18 06:31:45 +000047 ExprValueKind &VK,
Sebastian Redl842ef522008-11-08 13:00:26 +000048 const SourceRange &OpRange,
49 const SourceRange &DestRange);
50static void CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCall7decc9e2010-11-18 06:31:45 +000051 ExprValueKind &VK,
Sebastian Redl842ef522008-11-08 13:00:26 +000052 const SourceRange &OpRange,
Anders Carlsson7cd39e02009-09-15 04:48:33 +000053 const SourceRange &DestRange,
John McCalle3027922010-08-25 11:45:40 +000054 CastKind &Kind);
Sebastian Redl842ef522008-11-08 13:00:26 +000055static void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCall7decc9e2010-11-18 06:31:45 +000056 ExprValueKind &VK,
Anders Carlssonf10e4142009-08-07 22:21:05 +000057 const SourceRange &OpRange,
John McCalle3027922010-08-25 11:45:40 +000058 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +000059 CXXCastPath &BasePath);
Sebastian Redl842ef522008-11-08 13:00:26 +000060static void CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCall7decc9e2010-11-18 06:31:45 +000061 ExprValueKind &VK,
Sebastian Redl842ef522008-11-08 13:00:26 +000062 const SourceRange &OpRange,
Mike Stump11289f42009-09-09 15:08:12 +000063 const SourceRange &DestRange,
John McCalle3027922010-08-25 11:45:40 +000064 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +000065 CXXCastPath &BasePath);
Sebastian Redl842ef522008-11-08 13:00:26 +000066
67static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType);
Sebastian Redl9f831db2009-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 Carlsson7d3360f2010-04-24 19:36:51 +000083 QualType DestType, bool CStyle,
84 const SourceRange &OpRange,
85 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +000086 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +000087 CXXCastPath &BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +000088static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType,
89 QualType DestType, bool CStyle,
90 const SourceRange &OpRange,
Anders Carlsson9a1cd872009-11-12 16:53:16 +000091 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +000092 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +000093 CXXCastPath &BasePath);
Douglas Gregor8f3952c2009-11-15 09:20:52 +000094static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType,
95 CanQualType DestType, bool CStyle,
Sebastian Redl9f831db2009-07-25 15:41:38 +000096 const SourceRange &OpRange,
97 QualType OrigSrcType,
Anders Carlsson9a1cd872009-11-12 16:53:16 +000098 QualType OrigDestType, unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +000099 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000100 CXXCastPath &BasePath);
Douglas Gregorc934bc82010-03-07 23:24:59 +0000101static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr,
Anders Carlssonb78feca2010-04-24 19:22:20 +0000102 QualType SrcType,
103 QualType DestType,bool CStyle,
104 const SourceRange &OpRange,
105 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000106 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000107 CXXCastPath &BasePath);
Anders Carlssonb78feca2010-04-24 19:22:20 +0000108
Sebastian Redl7c353682009-11-14 21:15:49 +0000109static TryCastResult TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000110 QualType DestType, bool CStyle,
111 const SourceRange &OpRange,
Fariborz Jahanian1cec0c42009-08-26 18:55:36 +0000112 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000113 CastKind &Kind);
Sebastian Redl7c353682009-11-14 21:15:49 +0000114static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000115 QualType DestType, bool CStyle,
116 const SourceRange &OpRange,
Anders Carlssonf1ae6d42009-09-01 20:52:42 +0000117 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000118 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000119 CXXCastPath &BasePath);
Sebastian Redl9f831db2009-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 Carlsson9d1b34b2009-09-26 00:12:34 +0000125 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000126 CastKind &Kind);
Sebastian Redl842ef522008-11-08 13:00:26 +0000127
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000128/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
John McCalldadc5752010-08-24 06:29:42 +0000129ExprResult
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000130Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
John McCallba7bf592010-08-24 05:47:05 +0000131 SourceLocation LAngleBracketLoc, ParsedType Ty,
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000132 SourceLocation RAngleBracketLoc,
John McCallfaf5fb42010-08-26 23:41:50 +0000133 SourceLocation LParenLoc, Expr *E,
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000134 SourceLocation RParenLoc) {
John McCalld377e042010-01-15 19:13:16 +0000135
John McCall97513962010-01-15 18:39:57 +0000136 TypeSourceInfo *DestTInfo;
137 QualType DestType = GetTypeFromParser(Ty, &DestTInfo);
138 if (!DestTInfo)
139 DestTInfo = Context.getTrivialTypeSourceInfo(DestType, SourceLocation());
John McCalld377e042010-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 McCalldadc5752010-08-24 06:29:42 +0000146ExprResult
John McCalld377e042010-01-15 19:13:16 +0000147Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
John McCallfaf5fb42010-08-26 23:41:50 +0000148 TypeSourceInfo *DestTInfo, Expr *Ex,
John McCalld377e042010-01-15 19:13:16 +0000149 SourceRange AngleBrackets, SourceRange Parens) {
John McCalld377e042010-01-15 19:13:16 +0000150 QualType DestType = DestTInfo->getType();
151
152 SourceRange OpRange(OpLoc, Parens.getEnd());
153 SourceRange DestRange = AngleBrackets;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000154
Douglas Gregor19b8c4f2008-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 Kyrtzidisca766292010-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 McCall7decc9e2010-11-18 06:31:45 +0000163 ExprValueKind VK = VK_RValue;
John McCall29ac8e22010-11-26 10:57:22 +0000164 if (TypeDependent)
165 VK = Expr::getValueKindForType(DestType);
166
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000167 switch (Kind) {
John McCall8cb679e2010-11-15 09:13:47 +0000168 default: llvm_unreachable("Unknown C++ cast!");
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000169
170 case tok::kw_const_cast:
Douglas Gregor19b8c4f2008-12-17 22:52:20 +0000171 if (!TypeDependent)
John McCall7decc9e2010-11-18 06:31:45 +0000172 CheckConstCast(*this, Ex, DestType, VK, OpRange, DestRange);
John McCallcf142162010-08-07 06:22:56 +0000173 return Owned(CXXConstCastExpr::Create(Context,
Douglas Gregora8a089b2010-07-13 18:40:04 +0000174 DestType.getNonLValueExprType(Context),
Douglas Gregor4478f852011-01-12 22:41:29 +0000175 VK, Ex, DestTInfo, OpLoc,
176 Parens.getEnd()));
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000177
Anders Carlsson4ab4f7f2009-08-02 19:07:59 +0000178 case tok::kw_dynamic_cast: {
John McCall8cb679e2010-11-15 09:13:47 +0000179 CastKind Kind = CK_Dependent;
John McCallcf142162010-08-07 06:22:56 +0000180 CXXCastPath BasePath;
Douglas Gregor19b8c4f2008-12-17 22:52:20 +0000181 if (!TypeDependent)
John McCall7decc9e2010-11-18 06:31:45 +0000182 CheckDynamicCast(*this, Ex, DestType, VK, OpRange, DestRange,
183 Kind, BasePath);
John McCallcf142162010-08-07 06:22:56 +0000184 return Owned(CXXDynamicCastExpr::Create(Context,
Douglas Gregora8a089b2010-07-13 18:40:04 +0000185 DestType.getNonLValueExprType(Context),
John McCall7decc9e2010-11-18 06:31:45 +0000186 VK, Kind, Ex, &BasePath, DestTInfo,
Douglas Gregor4478f852011-01-12 22:41:29 +0000187 OpLoc, Parens.getEnd()));
Anders Carlsson4ab4f7f2009-08-02 19:07:59 +0000188 }
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000189 case tok::kw_reinterpret_cast: {
John McCall8cb679e2010-11-15 09:13:47 +0000190 CastKind Kind = CK_Dependent;
Douglas Gregor19b8c4f2008-12-17 22:52:20 +0000191 if (!TypeDependent)
John McCall7decc9e2010-11-18 06:31:45 +0000192 CheckReinterpretCast(*this, Ex, DestType, VK, OpRange, DestRange, Kind);
John McCallcf142162010-08-07 06:22:56 +0000193 return Owned(CXXReinterpretCastExpr::Create(Context,
Douglas Gregora8a089b2010-07-13 18:40:04 +0000194 DestType.getNonLValueExprType(Context),
John McCall7decc9e2010-11-18 06:31:45 +0000195 VK, Kind, Ex, 0,
Douglas Gregor4478f852011-01-12 22:41:29 +0000196 DestTInfo, OpLoc, Parens.getEnd()));
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000197 }
Anders Carlssonf10e4142009-08-07 22:21:05 +0000198 case tok::kw_static_cast: {
John McCall8cb679e2010-11-15 09:13:47 +0000199 CastKind Kind = CK_Dependent;
John McCallcf142162010-08-07 06:22:56 +0000200 CXXCastPath BasePath;
Douglas Gregorb33eed02010-04-16 22:09:46 +0000201 if (!TypeDependent)
John McCall7decc9e2010-11-18 06:31:45 +0000202 CheckStaticCast(*this, Ex, DestType, VK, OpRange, Kind, BasePath);
Anders Carlssone9766d52009-09-09 21:33:21 +0000203
John McCallcf142162010-08-07 06:22:56 +0000204 return Owned(CXXStaticCastExpr::Create(Context,
Douglas Gregora8a089b2010-07-13 18:40:04 +0000205 DestType.getNonLValueExprType(Context),
John McCall7decc9e2010-11-18 06:31:45 +0000206 VK, Kind, Ex, &BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000207 DestTInfo, OpLoc, Parens.getEnd()));
Anders Carlssonf10e4142009-08-07 22:21:05 +0000208 }
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000209 }
210
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000211 return ExprError();
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000212}
213
Sebastian Redl55db1ec2009-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 Gohman28ade552010-07-26 21:25:24 +0000219static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) {
Sebastian Redl55db1ec2009-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 Jahanian8c3f06d2010-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 Redl55db1ec2009-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 Gregoreaff2cb2010-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 Redl55db1ec2009-11-18 18:10:53 +0000267 return false;
268}
269
Sebastian Redla5a77a62009-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 Redl802f14c2009-10-22 15:07:22 +0000274static bool
Mike Stump11289f42009-09-09 15:08:12 +0000275CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) {
Sebastian Redla5a77a62009-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 Gregoreaff2cb2010-07-08 20:27:32 +0000280 assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() ||
281 SrcType->isBlockPointerType()) &&
Sebastian Redla5a77a62009-01-27 23:18:31 +0000282 "Source type is not pointer or pointer to member.");
Douglas Gregoreaff2cb2010-07-08 20:27:32 +0000283 assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() ||
284 DestType->isBlockPointerType()) &&
Sebastian Redla5a77a62009-01-27 23:18:31 +0000285 "Destination type is not pointer or pointer to member.");
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000286
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000287 QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType),
288 UnwrappedDestType = Self.Context.getCanonicalType(DestType);
John McCall8ccfcb52009-09-24 19:53:00 +0000289 llvm::SmallVector<Qualifiers, 8> cv1, cv2;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000290
291 // Find the qualifications.
Sebastian Redl55db1ec2009-11-18 18:10:53 +0000292 while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
Anders Carlsson76f513f2010-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 Redl3c5aa4d2008-11-05 21:50:06 +0000300 }
Douglas Gregoreaff2cb2010-07-08 20:27:32 +0000301 if (cv1.empty())
302 return false;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000303
304 // Construct void pointers with those qualifiers (in reverse order of
305 // unwrapping, of course).
Sebastian Redl842ef522008-11-08 13:00:26 +0000306 QualType SrcConstruct = Self.Context.VoidTy;
307 QualType DestConstruct = Self.Context.VoidTy;
John McCall8ccfcb52009-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 Stump11289f42009-09-09 15:08:12 +0000311 i1 != cv1.rend(); ++i1, ++i2) {
John McCall8ccfcb52009-09-24 19:53:00 +0000312 SrcConstruct
313 = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1));
314 DestConstruct
315 = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2));
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000316 }
317
318 // Test if they're compatible.
319 return SrcConstruct != DestConstruct &&
Sebastian Redl842ef522008-11-08 13:00:26 +0000320 !Self.IsQualificationConversion(SrcConstruct, DestConstruct);
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000321}
322
Sebastian Redl3c5aa4d2008-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 Carlsson4ab4f7f2009-08-02 19:07:59 +0000326static void
Sebastian Redl842ef522008-11-08 13:00:26 +0000327CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCall7decc9e2010-11-18 06:31:45 +0000328 ExprValueKind &VK, const SourceRange &OpRange,
John McCalle3027922010-08-25 11:45:40 +0000329 const SourceRange &DestRange, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000330 CXXCastPath &BasePath) {
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000331 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
Sebastian Redl842ef522008-11-08 13:00:26 +0000332 DestType = Self.Context.getCanonicalType(DestType);
Sebastian Redl3c5aa4d2008-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 Kremenekc23c7e62009-07-29 21:53:49 +0000338 const PointerType *DestPointer = DestType->getAs<PointerType>();
John McCall7decc9e2010-11-18 06:31:45 +0000339 const ReferenceType *DestReference = 0;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000340 if (DestPointer) {
341 DestPointee = DestPointer->getPointeeType();
John McCall7decc9e2010-11-18 06:31:45 +0000342 } else if ((DestReference = DestType->getAs<ReferenceType>())) {
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000343 DestPointee = DestReference->getPointeeType();
Douglas Gregor465184a2011-01-22 00:06:57 +0000344 VK = isa<LValueReferenceType>(DestReference) ? VK_LValue
345 : isa<RValueReferenceType>(DestReference) ? VK_XValue
346 : VK_RValue;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000347 } else {
Chris Lattner377d1f82008-11-18 22:52:51 +0000348 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000349 << OrigDestType << DestRange;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000350 return;
351 }
352
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000353 const RecordType *DestRecord = DestPointee->getAs<RecordType>();
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000354 if (DestPointee->isVoidType()) {
355 assert(DestPointer && "Reference to void is not possible");
356 } else if (DestRecord) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000357 if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
Douglas Gregor89336232010-03-29 23:34:08 +0000358 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssond624e162009-08-26 23:45:07 +0000359 << DestRange))
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000360 return;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000361 } else {
Chris Lattner377d1f82008-11-18 22:52:51 +0000362 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000363 << DestPointee.getUnqualifiedType() << DestRange;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000364 return;
365 }
366
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000367 // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
368 // complete class type, [...]. If T is an lvalue reference type, v shall be
Douglas Gregor465184a2011-01-22 00:06:57 +0000369 // an lvalue of a complete class type, [...]. If T is an rvalue reference
370 // type, v shall be an expression having a complete class type, [...]
Sebastian Redl842ef522008-11-08 13:00:26 +0000371 QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000372 QualType SrcPointee;
373 if (DestPointer) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000374 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000375 SrcPointee = SrcPointer->getPointeeType();
376 } else {
Chris Lattner377d1f82008-11-18 22:52:51 +0000377 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000378 << OrigSrcType << SrcExpr->getSourceRange();
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000379 return;
380 }
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000381 } else if (DestReference->isLValueReferenceType()) {
John McCall086a4642010-11-24 05:12:34 +0000382 if (!SrcExpr->isLValue()) {
Chris Lattner377d1f82008-11-18 22:52:51 +0000383 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
Sebastian Redl9f831db2009-07-25 15:41:38 +0000384 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000385 }
386 SrcPointee = SrcType;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000387 } else {
388 SrcPointee = SrcType;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000389 }
390
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000391 const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000392 if (SrcRecord) {
Douglas Gregored0cfbd2009-03-09 16:13:40 +0000393 if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
Douglas Gregor89336232010-03-29 23:34:08 +0000394 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssond624e162009-08-26 23:45:07 +0000395 << SrcExpr->getSourceRange()))
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000396 return;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000397 } else {
Chris Lattner29e812b2008-11-20 06:06:08 +0000398 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000399 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redl3c5aa4d2008-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 Lattner377d1f82008-11-18 22:52:51 +0000411 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
Sebastian Redl9f831db2009-07-25 15:41:38 +0000412 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl3c5aa4d2008-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 McCalle3027922010-08-25 11:45:40 +0000419 Kind = CK_NoOp;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000420 return;
421 }
422
423 // C++ 5.2.7p5
424 // Upcasts are resolved statically.
Sebastian Redl842ef522008-11-08 13:00:26 +0000425 if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
Anders Carlssona70cff62010-04-24 19:06:50 +0000426 if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
427 OpRange.getBegin(), OpRange,
428 &BasePath))
429 return;
430
John McCalle3027922010-08-25 11:45:40 +0000431 Kind = CK_DerivedToBase;
Douglas Gregor88d292c2010-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 Redl3c5aa4d2008-11-05 21:50:06 +0000438 return;
439 }
440
441 // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000442 const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition();
Sebastian Redlb426f6332008-11-06 15:59:35 +0000443 assert(SrcDecl && "Definition missing");
444 if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
Chris Lattner29e812b2008-11-20 06:06:08 +0000445 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000446 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redlb426f6332008-11-06 15:59:35 +0000447 }
Douglas Gregor88d292c2010-05-13 16:44:06 +0000448 Self.MarkVTableUsed(OpRange.getBegin(),
449 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000450
451 // Done. Everything else is run-time checks.
John McCalle3027922010-08-25 11:45:40 +0000452 Kind = CK_Dynamic;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000453}
Sebastian Redl9f831db2009-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 McCall7decc9e2010-11-18 06:31:45 +0000461CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType, ExprValueKind &VK,
Mike Stump11289f42009-09-09 15:08:12 +0000462 const SourceRange &OpRange, const SourceRange &DestRange) {
John McCall7decc9e2010-11-18 06:31:45 +0000463 VK = Expr::getValueKindForType(DestType);
464 if (VK == VK_RValue)
Douglas Gregorb92a1562010-02-03 00:27:59 +0000465 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9f831db2009-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 McCall7decc9e2010-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 Gregorb92a1562010-02-03 00:27:59 +0000485 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000486
487 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlsson9d1b34b2009-09-26 00:12:34 +0000488 if (TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange,
489 msg, Kind)
Sebastian Redl9f831db2009-07-25 15:41:38 +0000490 != TC_Success && msg != 0)
Douglas Gregore81f58e2010-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 Redl9f831db2009-07-25 15:41:38 +0000503 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregore81f58e2010-11-08 03:40:48 +0000504 }
505
Sebastian Redl9f831db2009-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 McCall7decc9e2010-11-18 06:31:45 +0000514 ExprValueKind &VK, const SourceRange &OpRange,
515 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl9f831db2009-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 Friedman03bf60a2009-11-16 05:44:20 +0000519 if (DestType->isVoidType()) {
John McCall34376a62010-12-04 03:47:34 +0000520 Self.IgnoredValueConversions(SrcExpr);
John McCalle3027922010-08-25 11:45:40 +0000521 Kind = CK_ToVoid;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000522 return;
Eli Friedman03bf60a2009-11-16 05:44:20 +0000523 }
Sebastian Redl9f831db2009-07-25 15:41:38 +0000524
John McCall7decc9e2010-11-18 06:31:45 +0000525 VK = Expr::getValueKindForType(DestType);
526 if (VK == VK_RValue && !DestType->isRecordType())
Douglas Gregorb92a1562010-02-03 00:27:59 +0000527 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000528
529 unsigned msg = diag::err_bad_cxx_cast_generic;
Sebastian Redl7c353682009-11-14 21:15:49 +0000530 if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, msg,
Anders Carlssona70cff62010-04-24 19:06:50 +0000531 Kind, BasePath) != TC_Success && msg != 0)
Douglas Gregore81f58e2010-11-08 03:40:48 +0000532 {
John McCall8cb679e2010-11-15 09:13:47 +0000533 if (SrcExpr->getType() == Self.Context.OverloadTy)
Douglas Gregore81f58e2010-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 Redl9f831db2009-07-25 15:41:38 +0000542 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregore81f58e2010-11-08 03:40:48 +0000543 }
John McCalld50a2712010-11-16 05:46:29 +0000544 else if (Kind == CK_BitCast)
John McCall2b5c1b22010-08-12 21:44:57 +0000545 Self.CheckCastAlign(SrcExpr, DestType, OpRange);
Sebastian Redl9f831db2009-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 Redl7c353682009-11-14 21:15:49 +0000551static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000552 QualType DestType, bool CStyle,
Anders Carlssonf1ae6d42009-09-01 20:52:42 +0000553 const SourceRange &OpRange, unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000554 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000555 CXXCastPath &BasePath) {
Sebastian Redl9f831db2009-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 Redl7c353682009-11-14 21:15:49 +0000576 tcr = TryStaticReferenceDowncast(Self, SrcExpr, DestType, CStyle, OpRange,
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000577 msg, Kind, BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000578 if (tcr != TC_NotApplicable)
579 return tcr;
580
Douglas Gregor465184a2011-01-22 00:06:57 +0000581 // C++0x [expr.static.cast]p3:
582 // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2
583 // T2" if "cv2 T2" is reference-compatible with "cv1 T1".
Sebastian Redl9f831db2009-07-25 15:41:38 +0000584 tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, msg);
Sebastian Redl7c353682009-11-14 21:15:49 +0000585 if (tcr != TC_NotApplicable) {
John McCalle3027922010-08-25 11:45:40 +0000586 Kind = CK_NoOp;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000587 return tcr;
Sebastian Redl7c353682009-11-14 21:15:49 +0000588 }
Sebastian Redl9f831db2009-07-25 15:41:38 +0000589
590 // C++ 5.2.9p2: An expression e can be explicitly converted to a type T
591 // [...] if the declaration "T t(e);" is well-formed, [...].
Fariborz Jahanian1cec0c42009-08-26 18:55:36 +0000592 tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CStyle, OpRange, msg,
Douglas Gregorb33eed02010-04-16 22:09:46 +0000593 Kind);
Anders Carlsson9d1b34b2009-09-26 00:12:34 +0000594 if (tcr != TC_NotApplicable)
Sebastian Redl9f831db2009-07-25 15:41:38 +0000595 return tcr;
Anders Carlssone9766d52009-09-09 21:33:21 +0000596
Sebastian Redl9f831db2009-07-25 15:41:38 +0000597 // C++ 5.2.9p6: May apply the reverse of any standard conversion, except
598 // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
599 // conversions, subject to further restrictions.
600 // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
601 // of qualification conversions impossible.
602 // In the CStyle case, the earlier attempt to const_cast should have taken
603 // care of reverse qualification conversions.
604
Sebastian Redl9f831db2009-07-25 15:41:38 +0000605 QualType SrcType = Self.Context.getCanonicalType(SrcExpr->getType());
606
Douglas Gregor0bf31402010-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 McCall8cb679e2010-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 Gregor0bf31402010-10-08 23:50:27 +0000615 Kind = CK_IntegralCast;
616 return TC_Success;
617 }
618 }
619
Sebastian Redl9f831db2009-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 Friedman03bf60a2009-11-16 05:44:20 +0000631 } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
John McCalle3027922010-08-25 11:45:40 +0000632 Kind = CK_IntegralCast;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000633 return TC_Success;
Eli Friedman03bf60a2009-11-16 05:44:20 +0000634 }
Sebastian Redl9f831db2009-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 Carlsson9a1cd872009-11-12 16:53:16 +0000639 tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000640 Kind, BasePath);
Sebastian Redl9f831db2009-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 Gregorc934bc82010-03-07 23:24:59 +0000647 tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
Anders Carlssonb78feca2010-04-24 19:22:20 +0000648 OpRange, msg, Kind, BasePath);
Sebastian Redl9f831db2009-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 Kremenekc23c7e62009-07-29 21:53:49 +0000655 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000656 QualType SrcPointee = SrcPointer->getPointeeType();
657 if (SrcPointee->isVoidType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000658 if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
Sebastian Redl9f831db2009-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 McCalle3027922010-08-25 11:45:40 +0000667 Kind = CK_BitCast;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000668 return TC_Success;
669 }
670 }
Fariborz Jahanianeee16692010-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 McCalle3027922010-08-25 11:45:40 +0000674 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian859c4152009-12-08 23:09:15 +0000675 return TC_Success;
676 }
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000677 else if (CStyle && DestType->isBlockPointerType()) {
678 // allow c-style cast of void * to block pointers.
John McCalle3027922010-08-25 11:45:40 +0000679 Kind = CK_AnyPointerToBlockPointerCast;
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000680 return TC_Success;
681 }
Sebastian Redl9f831db2009-07-25 15:41:38 +0000682 }
683 }
Fariborz Jahanianb0901b72010-05-12 18:16:59 +0000684 // Allow arbitray objective-c pointer conversion with static casts.
685 if (SrcType->isObjCObjectPointerType() &&
John McCall8cb679e2010-11-15 09:13:47 +0000686 DestType->isObjCObjectPointerType()) {
687 Kind = CK_BitCast;
Fariborz Jahanianb0901b72010-05-12 18:16:59 +0000688 return TC_Success;
John McCall8cb679e2010-11-15 09:13:47 +0000689 }
Fariborz Jahanianb0901b72010-05-12 18:16:59 +0000690
Sebastian Redl9f831db2009-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 Stump11289f42009-09-09 15:08:12 +0000698 unsigned &msg) {
Douglas Gregor465184a2011-01-22 00:06:57 +0000699 // C++0x [expr.static.cast]p3:
700 // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to
701 // cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000702 const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000703 if (!R)
704 return TC_NotApplicable;
705
Douglas Gregor465184a2011-01-22 00:06:57 +0000706 if (!SrcExpr->isGLValue())
Sebastian Redl9f831db2009-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 Gregor8b2d2fe2010-08-07 11:51:51 +0000713 bool ObjCConversion;
Douglas Gregor3ec1bf22009-11-05 13:06:35 +0000714 if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
Douglas Gregor465184a2011-01-22 00:06:57 +0000715 R->getPointeeType(), SrcExpr->getType(),
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +0000716 DerivedToBase, ObjCConversion) <
Sebastian Redl9f831db2009-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
Sebastian Redl9f831db2009-07-25 15:41:38 +0000722 return TC_Success;
723}
724
725/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
726TryCastResult
727TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
728 bool CStyle, const SourceRange &OpRange,
John McCalle3027922010-08-25 11:45:40 +0000729 unsigned &msg, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000730 CXXCastPath &BasePath) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000731 // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
732 // cast to type "reference to cv2 D", where D is a class derived from B,
733 // if a valid standard conversion from "pointer to D" to "pointer to B"
734 // exists, cv2 >= cv1, and B is not a virtual base class of D.
735 // In addition, DR54 clarifies that the base must be accessible in the
736 // current context. Although the wording of DR54 only applies to the pointer
737 // variant of this rule, the intent is clearly for it to apply to the this
738 // conversion as well.
739
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000740 const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000741 if (!DestReference) {
742 return TC_NotApplicable;
743 }
744 bool RValueRef = DestReference->isRValueReferenceType();
John McCall086a4642010-11-24 05:12:34 +0000745 if (!RValueRef && !SrcExpr->isLValue()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000746 // We know the left side is an lvalue reference, so we can suggest a reason.
747 msg = diag::err_bad_cxx_cast_rvalue;
748 return TC_NotApplicable;
749 }
750
751 QualType DestPointee = DestReference->getPointeeType();
752
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000753 return TryStaticDowncast(Self,
754 Self.Context.getCanonicalType(SrcExpr->getType()),
755 Self.Context.getCanonicalType(DestPointee), CStyle,
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000756 OpRange, SrcExpr->getType(), DestType, msg, Kind,
757 BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000758}
759
760/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
761TryCastResult
762TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
Mike Stump11289f42009-09-09 15:08:12 +0000763 bool CStyle, const SourceRange &OpRange,
John McCalle3027922010-08-25 11:45:40 +0000764 unsigned &msg, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000765 CXXCastPath &BasePath) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000766 // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
767 // type, can be converted to an rvalue of type "pointer to cv2 D", where D
768 // is a class derived from B, if a valid standard conversion from "pointer
769 // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
770 // class of D.
771 // In addition, DR54 clarifies that the base must be accessible in the
772 // current context.
773
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000774 const PointerType *DestPointer = DestType->getAs<PointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000775 if (!DestPointer) {
776 return TC_NotApplicable;
777 }
778
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000779 const PointerType *SrcPointer = SrcType->getAs<PointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000780 if (!SrcPointer) {
781 msg = diag::err_bad_static_cast_pointer_nonpointer;
782 return TC_NotApplicable;
783 }
784
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000785 return TryStaticDowncast(Self,
786 Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
787 Self.Context.getCanonicalType(DestPointer->getPointeeType()),
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000788 CStyle, OpRange, SrcType, DestType, msg, Kind,
789 BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000790}
791
792/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
793/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000794/// DestType is possible and allowed.
Sebastian Redl9f831db2009-07-25 15:41:38 +0000795TryCastResult
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000796TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000797 bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
Anders Carlsson9a1cd872009-11-12 16:53:16 +0000798 QualType OrigDestType, unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000799 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl802f14c2009-10-22 15:07:22 +0000800 // We can only work with complete types. But don't complain if it doesn't work
Douglas Gregor89336232010-03-29 23:34:08 +0000801 if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) ||
802 Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0)))
Sebastian Redl802f14c2009-10-22 15:07:22 +0000803 return TC_NotApplicable;
804
Sebastian Redl9f831db2009-07-25 15:41:38 +0000805 // Downcast can only happen in class hierarchies, so we need classes.
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000806 if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000807 return TC_NotApplicable;
808 }
809
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000810 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000811 /*DetectVirtual=*/true);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000812 if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
813 return TC_NotApplicable;
814 }
815
816 // Target type does derive from source type. Now we're serious. If an error
817 // appears now, it's not ignored.
818 // This may not be entirely in line with the standard. Take for example:
819 // struct A {};
820 // struct B : virtual A {
821 // B(A&);
822 // };
Mike Stump11289f42009-09-09 15:08:12 +0000823 //
Sebastian Redl9f831db2009-07-25 15:41:38 +0000824 // void f()
825 // {
826 // (void)static_cast<const B&>(*((A*)0));
827 // }
828 // As far as the standard is concerned, p5 does not apply (A is virtual), so
829 // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
830 // However, both GCC and Comeau reject this example, and accepting it would
831 // mean more complex code if we're to preserve the nice error message.
832 // FIXME: Being 100% compliant here would be nice to have.
833
834 // Must preserve cv, as always, unless we're in C-style mode.
835 if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
836 msg = diag::err_bad_cxx_cast_const_away;
837 return TC_Failed;
838 }
839
840 if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
841 // This code is analoguous to that in CheckDerivedToBaseConversion, except
842 // that it builds the paths in reverse order.
843 // To sum up: record all paths to the base and build a nice string from
844 // them. Use it to spice up the error message.
845 if (!Paths.isRecordingPaths()) {
846 Paths.clear();
847 Paths.setRecordingPaths(true);
848 Self.IsDerivedFrom(DestType, SrcType, Paths);
849 }
850 std::string PathDisplayStr;
851 std::set<unsigned> DisplayedPaths;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000852 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000853 PI != PE; ++PI) {
854 if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
855 // We haven't displayed a path to this particular base
856 // class subobject yet.
857 PathDisplayStr += "\n ";
Douglas Gregor36d1b142009-10-06 17:59:45 +0000858 for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
859 EE = PI->rend();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000860 EI != EE; ++EI)
861 PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000862 PathDisplayStr += QualType(DestType).getAsString();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000863 }
864 }
865
866 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000867 << QualType(SrcType).getUnqualifiedType()
868 << QualType(DestType).getUnqualifiedType()
Sebastian Redl9f831db2009-07-25 15:41:38 +0000869 << PathDisplayStr << OpRange;
870 msg = 0;
871 return TC_Failed;
872 }
873
874 if (Paths.getDetectedVirtual() != 0) {
875 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
876 Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
877 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
878 msg = 0;
879 return TC_Failed;
880 }
881
John McCall5b0829a2010-02-10 09:31:12 +0000882 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
John McCall5b0829a2010-02-10 09:31:12 +0000883 SrcType, DestType,
John McCall1064d7e2010-03-16 05:22:47 +0000884 Paths.front(),
885 diag::err_downcast_from_inaccessible_base)) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000886 msg = 0;
887 return TC_Failed;
888 }
889
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000890 Self.BuildBasePathArray(Paths, BasePath);
John McCalle3027922010-08-25 11:45:40 +0000891 Kind = CK_BaseToDerived;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000892 return TC_Success;
893}
894
895/// TryStaticMemberPointerUpcast - Tests whether a conversion according to
896/// C++ 5.2.9p9 is valid:
897///
898/// An rvalue of type "pointer to member of D of type cv1 T" can be
899/// converted to an rvalue of type "pointer to member of B of type cv2 T",
900/// where B is a base class of D [...].
901///
902TryCastResult
Douglas Gregorc934bc82010-03-07 23:24:59 +0000903TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr, QualType SrcType,
904 QualType DestType, bool CStyle,
905 const SourceRange &OpRange,
John McCalle3027922010-08-25 11:45:40 +0000906 unsigned &msg, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000907 CXXCastPath &BasePath) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000908 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000909 if (!DestMemPtr)
910 return TC_NotApplicable;
Douglas Gregorc934bc82010-03-07 23:24:59 +0000911
912 bool WasOverloadedFunction = false;
John McCall16df1e52010-03-30 21:47:33 +0000913 DeclAccessPair FoundOverload;
Douglas Gregor064fdb22010-04-14 23:11:21 +0000914 if (SrcExpr->getType() == Self.Context.OverloadTy) {
915 if (FunctionDecl *Fn
916 = Self.ResolveAddressOfOverloadedFunction(SrcExpr, DestType, false,
917 FoundOverload)) {
918 CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
919 SrcType = Self.Context.getMemberPointerType(Fn->getType(),
920 Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
921 WasOverloadedFunction = true;
922 }
Douglas Gregorc934bc82010-03-07 23:24:59 +0000923 }
Douglas Gregor064fdb22010-04-14 23:11:21 +0000924
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000925 const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000926 if (!SrcMemPtr) {
927 msg = diag::err_bad_static_cast_member_pointer_nonmp;
928 return TC_NotApplicable;
929 }
930
931 // T == T, modulo cv
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000932 if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
933 DestMemPtr->getPointeeType()))
Sebastian Redl9f831db2009-07-25 15:41:38 +0000934 return TC_NotApplicable;
935
936 // B base of D
937 QualType SrcClass(SrcMemPtr->getClass(), 0);
938 QualType DestClass(DestMemPtr->getClass(), 0);
Anders Carlssonb78feca2010-04-24 19:22:20 +0000939 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000940 /*DetectVirtual=*/true);
941 if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
942 return TC_NotApplicable;
943 }
944
945 // B is a base of D. But is it an allowed base? If not, it's a hard error.
Douglas Gregor27ac4292010-05-21 20:29:55 +0000946 if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000947 Paths.clear();
948 Paths.setRecordingPaths(true);
949 bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
950 assert(StillOkay);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000951 (void)StillOkay;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000952 std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
953 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
954 << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
955 msg = 0;
956 return TC_Failed;
957 }
958
959 if (const RecordType *VBase = Paths.getDetectedVirtual()) {
960 Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
961 << SrcClass << DestClass << QualType(VBase, 0) << OpRange;
962 msg = 0;
963 return TC_Failed;
964 }
965
John McCall5b0829a2010-02-10 09:31:12 +0000966 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
Eli Friedmand4c75cd2010-07-23 19:25:41 +0000967 DestClass, SrcClass,
John McCall1064d7e2010-03-16 05:22:47 +0000968 Paths.front(),
969 diag::err_upcast_to_inaccessible_base)) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000970 msg = 0;
971 return TC_Failed;
972 }
973
Douglas Gregorc934bc82010-03-07 23:24:59 +0000974 if (WasOverloadedFunction) {
975 // Resolve the address of the overloaded function again, this time
976 // allowing complaints if something goes wrong.
977 FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr,
978 DestType,
John McCall16df1e52010-03-30 21:47:33 +0000979 true,
980 FoundOverload);
Douglas Gregorc934bc82010-03-07 23:24:59 +0000981 if (!Fn) {
982 msg = 0;
983 return TC_Failed;
984 }
985
John McCall16df1e52010-03-30 21:47:33 +0000986 SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
Douglas Gregorc934bc82010-03-07 23:24:59 +0000987 if (!SrcExpr) {
988 msg = 0;
989 return TC_Failed;
990 }
991 }
992
Anders Carlssonb78feca2010-04-24 19:22:20 +0000993 Self.BuildBasePathArray(Paths, BasePath);
John McCalle3027922010-08-25 11:45:40 +0000994 Kind = CK_DerivedToBaseMemberPointer;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000995 return TC_Success;
996}
997
998/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
999/// is valid:
1000///
1001/// An expression e can be explicitly converted to a type T using a
1002/// @c static_cast if the declaration "T t(e);" is well-formed [...].
1003TryCastResult
Sebastian Redl7c353682009-11-14 21:15:49 +00001004TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
Fariborz Jahanian1cec0c42009-08-26 18:55:36 +00001005 bool CStyle, const SourceRange &OpRange, unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +00001006 CastKind &Kind) {
Anders Carlsson85ec4ff2009-09-07 18:25:47 +00001007 if (DestType->isRecordType()) {
1008 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
1009 diag::err_bad_dynamic_cast_incomplete)) {
1010 msg = 0;
1011 return TC_Failed;
1012 }
1013 }
Douglas Gregorb33eed02010-04-16 22:09:46 +00001014
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00001015 InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
1016 InitializationKind InitKind
Douglas Gregorb33eed02010-04-16 22:09:46 +00001017 = InitializationKind::CreateCast(/*FIXME:*/OpRange,
1018 CStyle);
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00001019 InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExpr, 1);
Douglas Gregore81f58e2010-11-08 03:40:48 +00001020
1021 // At this point of CheckStaticCast, if the destination is a reference,
1022 // or the expression is an overload expression this has to work.
1023 // There is no other way that works.
1024 // On the other hand, if we're checking a C-style cast, we've still got
1025 // the reinterpret_cast way.
1026
Douglas Gregorb33eed02010-04-16 22:09:46 +00001027 if (InitSeq.getKind() == InitializationSequence::FailedSequence &&
Douglas Gregore81f58e2010-11-08 03:40:48 +00001028 (CStyle || !DestType->isReferenceType()))
Anders Carlsson9d1b34b2009-09-26 00:12:34 +00001029 return TC_NotApplicable;
Douglas Gregorb33eed02010-04-16 22:09:46 +00001030
John McCalldadc5752010-08-24 06:29:42 +00001031 ExprResult Result
John McCallfaf5fb42010-08-26 23:41:50 +00001032 = InitSeq.Perform(Self, Entity, InitKind, MultiExprArg(Self, &SrcExpr, 1));
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00001033 if (Result.isInvalid()) {
1034 msg = 0;
1035 return TC_Failed;
1036 }
1037
Douglas Gregorb33eed02010-04-16 22:09:46 +00001038 if (InitSeq.isConstructorInitialization())
John McCalle3027922010-08-25 11:45:40 +00001039 Kind = CK_ConstructorConversion;
Douglas Gregorb33eed02010-04-16 22:09:46 +00001040 else
John McCalle3027922010-08-25 11:45:40 +00001041 Kind = CK_NoOp;
Douglas Gregorb33eed02010-04-16 22:09:46 +00001042
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00001043 SrcExpr = Result.takeAs<Expr>();
1044 return TC_Success;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001045}
1046
1047/// TryConstCast - See if a const_cast from source to destination is allowed,
1048/// and perform it if it is.
1049static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
1050 bool CStyle, unsigned &msg) {
1051 DestType = Self.Context.getCanonicalType(DestType);
1052 QualType SrcType = SrcExpr->getType();
Douglas Gregorc1ed20c2011-01-22 00:19:52 +00001053 if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) {
1054 if (DestTypeTmp->isLValueReferenceType() && !SrcExpr->isLValue()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001055 // Cannot const_cast non-lvalue to lvalue reference type. But if this
1056 // is C-style, static_cast might find a way, so we simply suggest a
1057 // message and tell the parent to keep searching.
1058 msg = diag::err_bad_cxx_cast_rvalue;
1059 return TC_NotApplicable;
1060 }
1061
1062 // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
1063 // [...] if a pointer to T1 can be [cast] to the type pointer to T2.
1064 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1065 SrcType = Self.Context.getPointerType(SrcType);
1066 }
1067
1068 // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
1069 // the rules for const_cast are the same as those used for pointers.
1070
John McCall0e704f72010-05-18 09:35:29 +00001071 if (!DestType->isPointerType() &&
1072 !DestType->isMemberPointerType() &&
1073 !DestType->isObjCObjectPointerType()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001074 // Cannot cast to non-pointer, non-reference type. Note that, if DestType
1075 // was a reference type, we converted it to a pointer above.
1076 // The status of rvalue references isn't entirely clear, but it looks like
1077 // conversion to them is simply invalid.
1078 // C++ 5.2.11p3: For two pointer types [...]
1079 if (!CStyle)
1080 msg = diag::err_bad_const_cast_dest;
1081 return TC_NotApplicable;
1082 }
1083 if (DestType->isFunctionPointerType() ||
1084 DestType->isMemberFunctionPointerType()) {
1085 // Cannot cast direct function pointers.
1086 // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
1087 // T is the ultimate pointee of source and target type.
1088 if (!CStyle)
1089 msg = diag::err_bad_const_cast_dest;
1090 return TC_NotApplicable;
1091 }
1092 SrcType = Self.Context.getCanonicalType(SrcType);
1093
1094 // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
1095 // completely equal.
1096 // FIXME: const_cast should probably not be able to convert between pointers
1097 // to different address spaces.
1098 // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
1099 // in multi-level pointers may change, but the level count must be the same,
1100 // as must be the final pointee type.
1101 while (SrcType != DestType &&
Douglas Gregor1fc3d662010-06-09 03:53:18 +00001102 Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) {
Chandler Carruth585fb1e2009-12-29 08:05:19 +00001103 Qualifiers Quals;
1104 SrcType = Self.Context.getUnqualifiedArrayType(SrcType, Quals);
1105 DestType = Self.Context.getUnqualifiedArrayType(DestType, Quals);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001106 }
1107
1108 // Since we're dealing in canonical types, the remainder must be the same.
1109 if (SrcType != DestType)
1110 return TC_NotApplicable;
1111
1112 return TC_Success;
1113}
1114
Douglas Gregore81f58e2010-11-08 03:40:48 +00001115
1116static void NoteAllOverloadCandidates(Expr* const Expr, Sema& sema)
1117{
1118
1119 assert(Expr->getType() == sema.Context.OverloadTy);
1120
1121 OverloadExpr::FindResult Ovl = OverloadExpr::find(Expr);
1122 OverloadExpr *const OvlExpr = Ovl.Expression;
1123
1124 for (UnresolvedSetIterator it = OvlExpr->decls_begin(),
1125 end = OvlExpr->decls_end(); it != end; ++it) {
1126 if ( FunctionTemplateDecl *ftd =
1127 dyn_cast<FunctionTemplateDecl>((*it)->getUnderlyingDecl()) )
1128 {
1129 sema.NoteOverloadCandidate(ftd->getTemplatedDecl());
1130 }
1131 else if ( FunctionDecl *f =
1132 dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl()) )
1133 {
1134 sema.NoteOverloadCandidate(f);
1135 }
1136 }
1137}
1138
1139
Sebastian Redl9f831db2009-07-25 15:41:38 +00001140static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
1141 QualType DestType, bool CStyle,
1142 const SourceRange &OpRange,
Anders Carlsson9d1b34b2009-09-26 00:12:34 +00001143 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +00001144 CastKind &Kind) {
Douglas Gregor51954272010-07-13 23:17:26 +00001145 bool IsLValueCast = false;
1146
Sebastian Redl9f831db2009-07-25 15:41:38 +00001147 DestType = Self.Context.getCanonicalType(DestType);
1148 QualType SrcType = SrcExpr->getType();
Douglas Gregore81f58e2010-11-08 03:40:48 +00001149
1150 // Is the source an overloaded name? (i.e. &foo)
1151 // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5)
John McCalle84af4e2010-11-13 01:35:44 +00001152 if (SrcType == Self.Context.OverloadTy)
Douglas Gregore81f58e2010-11-08 03:40:48 +00001153 return TC_NotApplicable;
1154
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001155 if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001156 bool LValue = DestTypeTmp->isLValueReferenceType();
John McCall086a4642010-11-24 05:12:34 +00001157 if (LValue && !SrcExpr->isLValue()) {
Douglas Gregorc1ed20c2011-01-22 00:19:52 +00001158 // Cannot cast non-lvalue to lvalue reference type. See the similar
1159 // comment in const_cast.
Sebastian Redl9f831db2009-07-25 15:41:38 +00001160 msg = diag::err_bad_cxx_cast_rvalue;
1161 return TC_NotApplicable;
1162 }
1163
1164 // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
1165 // same effect as the conversion *reinterpret_cast<T*>(&x) with the
1166 // built-in & and * operators.
1167 // This code does this transformation for the checked types.
1168 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1169 SrcType = Self.Context.getPointerType(SrcType);
Douglas Gregore81f58e2010-11-08 03:40:48 +00001170
Douglas Gregor51954272010-07-13 23:17:26 +00001171 IsLValueCast = true;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001172 }
1173
1174 // Canonicalize source for comparison.
1175 SrcType = Self.Context.getCanonicalType(SrcType);
1176
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001177 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
1178 *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +00001179 if (DestMemPtr && SrcMemPtr) {
1180 // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
1181 // can be explicitly converted to an rvalue of type "pointer to member
1182 // of Y of type T2" if T1 and T2 are both function types or both object
1183 // types.
1184 if (DestMemPtr->getPointeeType()->isFunctionType() !=
1185 SrcMemPtr->getPointeeType()->isFunctionType())
1186 return TC_NotApplicable;
1187
1188 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
1189 // constness.
1190 // A reinterpret_cast followed by a const_cast can, though, so in C-style,
1191 // we accept it.
1192 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1193 msg = diag::err_bad_cxx_cast_const_away;
1194 return TC_Failed;
1195 }
1196
Charles Davisebab1ed2010-08-16 05:30:44 +00001197 // Don't allow casting between member pointers of different sizes.
1198 if (Self.Context.getTypeSize(DestMemPtr) !=
1199 Self.Context.getTypeSize(SrcMemPtr)) {
1200 msg = diag::err_bad_cxx_cast_member_pointer_size;
1201 return TC_Failed;
1202 }
1203
Sebastian Redl9f831db2009-07-25 15:41:38 +00001204 // A valid member pointer cast.
John McCalle3027922010-08-25 11:45:40 +00001205 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001206 return TC_Success;
1207 }
1208
1209 // See below for the enumeral issue.
Douglas Gregor6972a622010-06-16 00:35:25 +00001210 if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001211 // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
1212 // type large enough to hold it. A value of std::nullptr_t can be
1213 // converted to an integral type; the conversion has the same meaning
1214 // and validity as a conversion of (void*)0 to the integral type.
1215 if (Self.Context.getTypeSize(SrcType) >
1216 Self.Context.getTypeSize(DestType)) {
1217 msg = diag::err_bad_reinterpret_cast_small_int;
1218 return TC_Failed;
1219 }
John McCalle3027922010-08-25 11:45:40 +00001220 Kind = CK_PointerToIntegral;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001221 return TC_Success;
1222 }
1223
Anders Carlsson570af5d2009-09-16 19:19:43 +00001224 bool destIsVector = DestType->isVectorType();
1225 bool srcIsVector = SrcType->isVectorType();
1226 if (srcIsVector || destIsVector) {
Douglas Gregor6972a622010-06-16 00:35:25 +00001227 // FIXME: Should this also apply to floating point types?
1228 bool srcIsScalar = SrcType->isIntegralType(Self.Context);
1229 bool destIsScalar = DestType->isIntegralType(Self.Context);
Anders Carlsson570af5d2009-09-16 19:19:43 +00001230
1231 // Check if this is a cast between a vector and something else.
1232 if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
1233 !(srcIsVector && destIsVector))
1234 return TC_NotApplicable;
1235
1236 // If both types have the same size, we can successfully cast.
Douglas Gregor19fc0b72009-12-22 22:47:22 +00001237 if (Self.Context.getTypeSize(SrcType)
1238 == Self.Context.getTypeSize(DestType)) {
John McCalle3027922010-08-25 11:45:40 +00001239 Kind = CK_BitCast;
Anders Carlsson570af5d2009-09-16 19:19:43 +00001240 return TC_Success;
Douglas Gregor19fc0b72009-12-22 22:47:22 +00001241 }
Anders Carlsson570af5d2009-09-16 19:19:43 +00001242
1243 if (destIsScalar)
1244 msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
1245 else if (srcIsScalar)
1246 msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
1247 else
1248 msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
1249
1250 return TC_Failed;
1251 }
1252
Douglas Gregoreaff2cb2010-07-08 20:27:32 +00001253 bool destIsPtr = DestType->isAnyPointerType() ||
1254 DestType->isBlockPointerType();
1255 bool srcIsPtr = SrcType->isAnyPointerType() ||
1256 SrcType->isBlockPointerType();
Sebastian Redl9f831db2009-07-25 15:41:38 +00001257 if (!destIsPtr && !srcIsPtr) {
1258 // Except for std::nullptr_t->integer and lvalue->reference, which are
1259 // handled above, at least one of the two arguments must be a pointer.
1260 return TC_NotApplicable;
1261 }
1262
1263 if (SrcType == DestType) {
1264 // C++ 5.2.10p2 has a note that mentions that, subject to all other
1265 // restrictions, a cast to the same type is allowed. The intent is not
1266 // entirely clear here, since all other paragraphs explicitly forbid casts
1267 // to the same type. However, the behavior of compilers is pretty consistent
1268 // on this point: allow same-type conversion if the involved types are
1269 // pointers, disallow otherwise.
John McCalle3027922010-08-25 11:45:40 +00001270 Kind = CK_NoOp;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001271 return TC_Success;
1272 }
1273
Douglas Gregor6972a622010-06-16 00:35:25 +00001274 if (DestType->isIntegralType(Self.Context)) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001275 assert(srcIsPtr && "One type must be a pointer");
1276 // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
1277 // type large enough to hold it.
1278 if (Self.Context.getTypeSize(SrcType) >
1279 Self.Context.getTypeSize(DestType)) {
1280 msg = diag::err_bad_reinterpret_cast_small_int;
1281 return TC_Failed;
1282 }
John McCalle3027922010-08-25 11:45:40 +00001283 Kind = CK_PointerToIntegral;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001284 return TC_Success;
1285 }
1286
Douglas Gregorb90df602010-06-16 00:17:44 +00001287 if (SrcType->isIntegralOrEnumerationType()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001288 assert(destIsPtr && "One type must be a pointer");
1289 // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
1290 // converted to a pointer.
John McCalle84af4e2010-11-13 01:35:44 +00001291 // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not
1292 // necessarily converted to a null pointer value.]
John McCalle3027922010-08-25 11:45:40 +00001293 Kind = CK_IntegralToPointer;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001294 return TC_Success;
1295 }
1296
1297 if (!destIsPtr || !srcIsPtr) {
1298 // With the valid non-pointer conversions out of the way, we can be even
1299 // more stringent.
1300 return TC_NotApplicable;
1301 }
1302
1303 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
1304 // The C-style cast operator can.
1305 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1306 msg = diag::err_bad_cxx_cast_const_away;
1307 return TC_Failed;
1308 }
Douglas Gregoreaff2cb2010-07-08 20:27:32 +00001309
1310 // Cannot convert between block pointers and Objective-C object pointers.
1311 if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) ||
1312 (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType()))
1313 return TC_NotApplicable;
1314
1315 // Any pointer can be cast to an Objective-C pointer type with a C-style
1316 // cast.
Fariborz Jahanian859c4152009-12-08 23:09:15 +00001317 if (CStyle && DestType->isObjCObjectPointerType()) {
John McCalle3027922010-08-25 11:45:40 +00001318 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian859c4152009-12-08 23:09:15 +00001319 return TC_Success;
1320 }
Douglas Gregoreaff2cb2010-07-08 20:27:32 +00001321
Sebastian Redl9f831db2009-07-25 15:41:38 +00001322 // Not casting away constness, so the only remaining check is for compatible
1323 // pointer categories.
John McCalle3027922010-08-25 11:45:40 +00001324 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001325
1326 if (SrcType->isFunctionPointerType()) {
1327 if (DestType->isFunctionPointerType()) {
1328 // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
1329 // a pointer to a function of a different type.
1330 return TC_Success;
1331 }
1332
1333 // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
1334 // an object type or vice versa is conditionally-supported.
1335 // Compilers support it in C++03 too, though, because it's necessary for
1336 // casting the return value of dlsym() and GetProcAddress().
1337 // FIXME: Conditionally-supported behavior should be configurable in the
1338 // TargetInfo or similar.
1339 if (!Self.getLangOptions().CPlusPlus0x)
1340 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1341 return TC_Success;
1342 }
1343
1344 if (DestType->isFunctionPointerType()) {
1345 // See above.
1346 if (!Self.getLangOptions().CPlusPlus0x)
1347 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1348 return TC_Success;
1349 }
Douglas Gregoreaff2cb2010-07-08 20:27:32 +00001350
Sebastian Redl9f831db2009-07-25 15:41:38 +00001351 // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
1352 // a pointer to an object of different type.
1353 // Void pointers are not specified, but supported by every compiler out there.
1354 // So we finish by allowing everything that remains - it's got to be two
1355 // object pointers.
1356 return TC_Success;
1357}
1358
Anders Carlssona70cff62010-04-24 19:06:50 +00001359bool
John McCall7decc9e2010-11-18 06:31:45 +00001360Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, ExprValueKind &VK,
1361 Expr *&CastExpr, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +00001362 CXXCastPath &BasePath,
Anders Carlssona70cff62010-04-24 19:06:50 +00001363 bool FunctionalStyle) {
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001364 if (CastExpr->isBoundMemberFunction(Context))
1365 return Diag(CastExpr->getLocStart(),
1366 diag::err_invalid_use_of_bound_member_func)
1367 << CastExpr->getSourceRange();
1368
Sebastian Redl9f831db2009-07-25 15:41:38 +00001369 // This test is outside everything else because it's the only case where
1370 // a non-lvalue-reference target type does not lead to decay.
1371 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Anders Carlsson9500ad12009-10-18 20:31:03 +00001372 if (CastTy->isVoidType()) {
John McCall34376a62010-12-04 03:47:34 +00001373 IgnoredValueConversions(CastExpr);
John McCalle3027922010-08-25 11:45:40 +00001374 Kind = CK_ToVoid;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001375 return false;
Anders Carlsson9500ad12009-10-18 20:31:03 +00001376 }
Sebastian Redl9f831db2009-07-25 15:41:38 +00001377
John McCall4cec5f82010-11-30 02:05:44 +00001378 // Make sure we determine the value kind before we bail out for
1379 // dependent types.
1380 VK = Expr::getValueKindForType(CastTy);
1381
Sebastian Redl9f831db2009-07-25 15:41:38 +00001382 // If the type is dependent, we won't do any other semantic analysis now.
John McCall8cb679e2010-11-15 09:13:47 +00001383 if (CastTy->isDependentType() || CastExpr->isTypeDependent()) {
1384 Kind = CK_Dependent;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001385 return false;
John McCall8cb679e2010-11-15 09:13:47 +00001386 }
Sebastian Redl9f831db2009-07-25 15:41:38 +00001387
John McCall7decc9e2010-11-18 06:31:45 +00001388 if (VK == VK_RValue && !CastTy->isRecordType())
Douglas Gregorb92a1562010-02-03 00:27:59 +00001389 DefaultFunctionArrayLvalueConversion(CastExpr);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001390
1391 // C++ [expr.cast]p5: The conversions performed by
1392 // - a const_cast,
1393 // - a static_cast,
1394 // - a static_cast followed by a const_cast,
1395 // - a reinterpret_cast, or
1396 // - a reinterpret_cast followed by a const_cast,
1397 // can be performed using the cast notation of explicit type conversion.
1398 // [...] If a conversion can be interpreted in more than one of the ways
1399 // listed above, the interpretation that appears first in the list is used,
1400 // even if a cast resulting from that interpretation is ill-formed.
1401 // In plain language, this means trying a const_cast ...
1402 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlssonf1ae6d42009-09-01 20:52:42 +00001403 TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,
1404 msg);
Anders Carlsson027732b2009-10-19 18:14:28 +00001405 if (tcr == TC_Success)
John McCalle3027922010-08-25 11:45:40 +00001406 Kind = CK_NoOp;
Anders Carlsson027732b2009-10-19 18:14:28 +00001407
Sebastian Redl9f831db2009-07-25 15:41:38 +00001408 if (tcr == TC_NotApplicable) {
1409 // ... or if that is not possible, a static_cast, ignoring const, ...
Anders Carlssona70cff62010-04-24 19:06:50 +00001410 tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg, Kind,
1411 BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001412 if (tcr == TC_NotApplicable) {
1413 // ... and finally a reinterpret_cast, ignoring const.
Anders Carlsson9d1b34b2009-09-26 00:12:34 +00001414 tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg,
1415 Kind);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001416 }
1417 }
1418
Nick Lewycky14d88eb2010-11-09 00:19:31 +00001419 if (tcr != TC_Success && msg != 0) {
1420 if (CastExpr->getType() == Context.OverloadTy) {
Douglas Gregore81f58e2010-11-08 03:40:48 +00001421 DeclAccessPair Found;
Nick Lewycky14d88eb2010-11-09 00:19:31 +00001422 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(CastExpr,
Douglas Gregore81f58e2010-11-08 03:40:48 +00001423 CastTy,
1424 /* Complain */ true,
1425 Found);
1426 assert(!Fn && "cast failed but able to resolve overload expression!!");
Nick Lewycky14d88eb2010-11-09 00:19:31 +00001427 (void)Fn;
1428 } else {
Douglas Gregore81f58e2010-11-08 03:40:48 +00001429 Diag(R.getBegin(), msg) << (FunctionalStyle ? CT_Functional : CT_CStyle)
1430 << CastExpr->getType() << CastTy << R;
1431 }
1432 }
John McCalld50a2712010-11-16 05:46:29 +00001433 else if (Kind == CK_BitCast)
John McCall2b5c1b22010-08-12 21:44:57 +00001434 CheckCastAlign(CastExpr, CastTy, R);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001435
1436 return tcr != TC_Success;
1437}