blob: 3a9ad5ff7f6d32a5a3b4f6c8e4712555f2284d64 [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,
Douglas Gregorba278e22011-01-25 16:13:26 +000081 QualType DestType, CastKind &Kind,
82 CXXCastPath &BasePath,
83 unsigned &msg);
Sebastian Redl9f831db2009-07-25 15:41:38 +000084static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr,
Anders Carlsson7d3360f2010-04-24 19:36:51 +000085 QualType DestType, bool CStyle,
86 const SourceRange &OpRange,
87 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +000088 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +000089 CXXCastPath &BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +000090static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType,
91 QualType DestType, bool CStyle,
92 const SourceRange &OpRange,
Anders Carlsson9a1cd872009-11-12 16:53:16 +000093 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +000094 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +000095 CXXCastPath &BasePath);
Douglas Gregor8f3952c2009-11-15 09:20:52 +000096static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType,
97 CanQualType DestType, bool CStyle,
Sebastian Redl9f831db2009-07-25 15:41:38 +000098 const SourceRange &OpRange,
99 QualType OrigSrcType,
Anders Carlsson9a1cd872009-11-12 16:53:16 +0000100 QualType OrigDestType, unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000101 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000102 CXXCastPath &BasePath);
Douglas Gregorc934bc82010-03-07 23:24:59 +0000103static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr,
Anders Carlssonb78feca2010-04-24 19:22:20 +0000104 QualType SrcType,
105 QualType DestType,bool CStyle,
106 const SourceRange &OpRange,
107 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000108 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000109 CXXCastPath &BasePath);
Anders Carlssonb78feca2010-04-24 19:22:20 +0000110
Sebastian Redl7c353682009-11-14 21:15:49 +0000111static TryCastResult TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000112 QualType DestType, bool CStyle,
113 const SourceRange &OpRange,
Fariborz Jahanian1cec0c42009-08-26 18:55:36 +0000114 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000115 CastKind &Kind);
Sebastian Redl7c353682009-11-14 21:15:49 +0000116static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000117 QualType DestType, bool CStyle,
118 const SourceRange &OpRange,
Anders Carlssonf1ae6d42009-09-01 20:52:42 +0000119 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000120 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000121 CXXCastPath &BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000122static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
123 bool CStyle, unsigned &msg);
124static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
125 QualType DestType, bool CStyle,
126 const SourceRange &OpRange,
Anders Carlsson9d1b34b2009-09-26 00:12:34 +0000127 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000128 CastKind &Kind);
Sebastian Redl842ef522008-11-08 13:00:26 +0000129
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000130/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
John McCalldadc5752010-08-24 06:29:42 +0000131ExprResult
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000132Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
John McCallba7bf592010-08-24 05:47:05 +0000133 SourceLocation LAngleBracketLoc, ParsedType Ty,
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000134 SourceLocation RAngleBracketLoc,
John McCallfaf5fb42010-08-26 23:41:50 +0000135 SourceLocation LParenLoc, Expr *E,
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000136 SourceLocation RParenLoc) {
John McCalld377e042010-01-15 19:13:16 +0000137
John McCall97513962010-01-15 18:39:57 +0000138 TypeSourceInfo *DestTInfo;
139 QualType DestType = GetTypeFromParser(Ty, &DestTInfo);
140 if (!DestTInfo)
141 DestTInfo = Context.getTrivialTypeSourceInfo(DestType, SourceLocation());
John McCalld377e042010-01-15 19:13:16 +0000142
143 return BuildCXXNamedCast(OpLoc, Kind, DestTInfo, move(E),
144 SourceRange(LAngleBracketLoc, RAngleBracketLoc),
145 SourceRange(LParenLoc, RParenLoc));
146}
147
John McCalldadc5752010-08-24 06:29:42 +0000148ExprResult
John McCalld377e042010-01-15 19:13:16 +0000149Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
John McCallfaf5fb42010-08-26 23:41:50 +0000150 TypeSourceInfo *DestTInfo, Expr *Ex,
John McCalld377e042010-01-15 19:13:16 +0000151 SourceRange AngleBrackets, SourceRange Parens) {
John McCalld377e042010-01-15 19:13:16 +0000152 QualType DestType = DestTInfo->getType();
153
154 SourceRange OpRange(OpLoc, Parens.getEnd());
155 SourceRange DestRange = AngleBrackets;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000156
Douglas Gregor19b8c4f2008-12-17 22:52:20 +0000157 // If the type is dependent, we won't do the semantic analysis now.
158 // FIXME: should we check this in a more fine-grained manner?
159 bool TypeDependent = DestType->isDependentType() || Ex->isTypeDependent();
160
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +0000161 if (Ex->isBoundMemberFunction(Context))
162 Diag(Ex->getLocStart(), diag::err_invalid_use_of_bound_member_func)
163 << Ex->getSourceRange();
164
John McCall7decc9e2010-11-18 06:31:45 +0000165 ExprValueKind VK = VK_RValue;
John McCall29ac8e22010-11-26 10:57:22 +0000166 if (TypeDependent)
167 VK = Expr::getValueKindForType(DestType);
168
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000169 switch (Kind) {
John McCall8cb679e2010-11-15 09:13:47 +0000170 default: llvm_unreachable("Unknown C++ cast!");
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000171
172 case tok::kw_const_cast:
Douglas Gregor19b8c4f2008-12-17 22:52:20 +0000173 if (!TypeDependent)
John McCall7decc9e2010-11-18 06:31:45 +0000174 CheckConstCast(*this, Ex, DestType, VK, OpRange, DestRange);
John McCallcf142162010-08-07 06:22:56 +0000175 return Owned(CXXConstCastExpr::Create(Context,
Douglas Gregora8a089b2010-07-13 18:40:04 +0000176 DestType.getNonLValueExprType(Context),
Douglas Gregor4478f852011-01-12 22:41:29 +0000177 VK, Ex, DestTInfo, OpLoc,
178 Parens.getEnd()));
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000179
Anders Carlsson4ab4f7f2009-08-02 19:07:59 +0000180 case tok::kw_dynamic_cast: {
John McCall8cb679e2010-11-15 09:13:47 +0000181 CastKind Kind = CK_Dependent;
John McCallcf142162010-08-07 06:22:56 +0000182 CXXCastPath BasePath;
Douglas Gregor19b8c4f2008-12-17 22:52:20 +0000183 if (!TypeDependent)
John McCall7decc9e2010-11-18 06:31:45 +0000184 CheckDynamicCast(*this, Ex, DestType, VK, OpRange, DestRange,
185 Kind, BasePath);
John McCallcf142162010-08-07 06:22:56 +0000186 return Owned(CXXDynamicCastExpr::Create(Context,
Douglas Gregora8a089b2010-07-13 18:40:04 +0000187 DestType.getNonLValueExprType(Context),
John McCall7decc9e2010-11-18 06:31:45 +0000188 VK, Kind, Ex, &BasePath, DestTInfo,
Douglas Gregor4478f852011-01-12 22:41:29 +0000189 OpLoc, Parens.getEnd()));
Anders Carlsson4ab4f7f2009-08-02 19:07:59 +0000190 }
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000191 case tok::kw_reinterpret_cast: {
John McCall8cb679e2010-11-15 09:13:47 +0000192 CastKind Kind = CK_Dependent;
Douglas Gregor19b8c4f2008-12-17 22:52:20 +0000193 if (!TypeDependent)
John McCall7decc9e2010-11-18 06:31:45 +0000194 CheckReinterpretCast(*this, Ex, DestType, VK, OpRange, DestRange, Kind);
John McCallcf142162010-08-07 06:22:56 +0000195 return Owned(CXXReinterpretCastExpr::Create(Context,
Douglas Gregora8a089b2010-07-13 18:40:04 +0000196 DestType.getNonLValueExprType(Context),
John McCall7decc9e2010-11-18 06:31:45 +0000197 VK, Kind, Ex, 0,
Douglas Gregor4478f852011-01-12 22:41:29 +0000198 DestTInfo, OpLoc, Parens.getEnd()));
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000199 }
Anders Carlssonf10e4142009-08-07 22:21:05 +0000200 case tok::kw_static_cast: {
John McCall8cb679e2010-11-15 09:13:47 +0000201 CastKind Kind = CK_Dependent;
John McCallcf142162010-08-07 06:22:56 +0000202 CXXCastPath BasePath;
Douglas Gregorb33eed02010-04-16 22:09:46 +0000203 if (!TypeDependent)
John McCall7decc9e2010-11-18 06:31:45 +0000204 CheckStaticCast(*this, Ex, DestType, VK, OpRange, Kind, BasePath);
Anders Carlssone9766d52009-09-09 21:33:21 +0000205
John McCallcf142162010-08-07 06:22:56 +0000206 return Owned(CXXStaticCastExpr::Create(Context,
Douglas Gregora8a089b2010-07-13 18:40:04 +0000207 DestType.getNonLValueExprType(Context),
John McCall7decc9e2010-11-18 06:31:45 +0000208 VK, Kind, Ex, &BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000209 DestTInfo, OpLoc, Parens.getEnd()));
Anders Carlssonf10e4142009-08-07 22:21:05 +0000210 }
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000211 }
212
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000213 return ExprError();
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000214}
215
Sebastian Redl55db1ec2009-11-18 18:10:53 +0000216/// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes,
217/// this removes one level of indirection from both types, provided that they're
218/// the same kind of pointer (plain or to-member). Unlike the Sema function,
219/// this one doesn't care if the two pointers-to-member don't point into the
220/// same class. This is because CastsAwayConstness doesn't care.
Dan Gohman28ade552010-07-26 21:25:24 +0000221static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) {
Sebastian Redl55db1ec2009-11-18 18:10:53 +0000222 const PointerType *T1PtrType = T1->getAs<PointerType>(),
223 *T2PtrType = T2->getAs<PointerType>();
224 if (T1PtrType && T2PtrType) {
225 T1 = T1PtrType->getPointeeType();
226 T2 = T2PtrType->getPointeeType();
227 return true;
228 }
Fariborz Jahanian8c3f06d2010-02-03 20:32:31 +0000229 const ObjCObjectPointerType *T1ObjCPtrType =
230 T1->getAs<ObjCObjectPointerType>(),
231 *T2ObjCPtrType =
232 T2->getAs<ObjCObjectPointerType>();
233 if (T1ObjCPtrType) {
234 if (T2ObjCPtrType) {
235 T1 = T1ObjCPtrType->getPointeeType();
236 T2 = T2ObjCPtrType->getPointeeType();
237 return true;
238 }
239 else if (T2PtrType) {
240 T1 = T1ObjCPtrType->getPointeeType();
241 T2 = T2PtrType->getPointeeType();
242 return true;
243 }
244 }
245 else if (T2ObjCPtrType) {
246 if (T1PtrType) {
247 T2 = T2ObjCPtrType->getPointeeType();
248 T1 = T1PtrType->getPointeeType();
249 return true;
250 }
251 }
252
Sebastian Redl55db1ec2009-11-18 18:10:53 +0000253 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
254 *T2MPType = T2->getAs<MemberPointerType>();
255 if (T1MPType && T2MPType) {
256 T1 = T1MPType->getPointeeType();
257 T2 = T2MPType->getPointeeType();
258 return true;
259 }
Douglas Gregoreaff2cb2010-07-08 20:27:32 +0000260
261 const BlockPointerType *T1BPType = T1->getAs<BlockPointerType>(),
262 *T2BPType = T2->getAs<BlockPointerType>();
263 if (T1BPType && T2BPType) {
264 T1 = T1BPType->getPointeeType();
265 T2 = T2BPType->getPointeeType();
266 return true;
267 }
268
Sebastian Redl55db1ec2009-11-18 18:10:53 +0000269 return false;
270}
271
Sebastian Redla5a77a62009-01-27 23:18:31 +0000272/// CastsAwayConstness - Check if the pointer conversion from SrcType to
273/// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by
274/// the cast checkers. Both arguments must denote pointer (possibly to member)
275/// types.
Sebastian Redl802f14c2009-10-22 15:07:22 +0000276static bool
Mike Stump11289f42009-09-09 15:08:12 +0000277CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) {
Sebastian Redla5a77a62009-01-27 23:18:31 +0000278 // Casting away constness is defined in C++ 5.2.11p8 with reference to
279 // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since
280 // the rules are non-trivial. So first we construct Tcv *...cv* as described
281 // in C++ 5.2.11p8.
Douglas Gregoreaff2cb2010-07-08 20:27:32 +0000282 assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() ||
283 SrcType->isBlockPointerType()) &&
Sebastian Redla5a77a62009-01-27 23:18:31 +0000284 "Source type is not pointer or pointer to member.");
Douglas Gregoreaff2cb2010-07-08 20:27:32 +0000285 assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() ||
286 DestType->isBlockPointerType()) &&
Sebastian Redla5a77a62009-01-27 23:18:31 +0000287 "Destination type is not pointer or pointer to member.");
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000288
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000289 QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType),
290 UnwrappedDestType = Self.Context.getCanonicalType(DestType);
John McCall8ccfcb52009-09-24 19:53:00 +0000291 llvm::SmallVector<Qualifiers, 8> cv1, cv2;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000292
293 // Find the qualifications.
Sebastian Redl55db1ec2009-11-18 18:10:53 +0000294 while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
Anders Carlsson76f513f2010-06-04 22:47:55 +0000295 Qualifiers SrcQuals;
296 Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals);
297 cv1.push_back(SrcQuals);
298
299 Qualifiers DestQuals;
300 Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals);
301 cv2.push_back(DestQuals);
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000302 }
Douglas Gregoreaff2cb2010-07-08 20:27:32 +0000303 if (cv1.empty())
304 return false;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000305
306 // Construct void pointers with those qualifiers (in reverse order of
307 // unwrapping, of course).
Sebastian Redl842ef522008-11-08 13:00:26 +0000308 QualType SrcConstruct = Self.Context.VoidTy;
309 QualType DestConstruct = Self.Context.VoidTy;
John McCall8ccfcb52009-09-24 19:53:00 +0000310 ASTContext &Context = Self.Context;
311 for (llvm::SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(),
312 i2 = cv2.rbegin();
Mike Stump11289f42009-09-09 15:08:12 +0000313 i1 != cv1.rend(); ++i1, ++i2) {
John McCall8ccfcb52009-09-24 19:53:00 +0000314 SrcConstruct
315 = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1));
316 DestConstruct
317 = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2));
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000318 }
319
320 // Test if they're compatible.
321 return SrcConstruct != DestConstruct &&
Sebastian Redl842ef522008-11-08 13:00:26 +0000322 !Self.IsQualificationConversion(SrcConstruct, DestConstruct);
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000323}
324
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000325/// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid.
326/// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime-
327/// checked downcasts in class hierarchies.
Anders Carlsson4ab4f7f2009-08-02 19:07:59 +0000328static void
Sebastian Redl842ef522008-11-08 13:00:26 +0000329CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCall7decc9e2010-11-18 06:31:45 +0000330 ExprValueKind &VK, const SourceRange &OpRange,
John McCalle3027922010-08-25 11:45:40 +0000331 const SourceRange &DestRange, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000332 CXXCastPath &BasePath) {
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000333 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
Sebastian Redl842ef522008-11-08 13:00:26 +0000334 DestType = Self.Context.getCanonicalType(DestType);
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000335
336 // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type,
337 // or "pointer to cv void".
338
339 QualType DestPointee;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000340 const PointerType *DestPointer = DestType->getAs<PointerType>();
John McCall7decc9e2010-11-18 06:31:45 +0000341 const ReferenceType *DestReference = 0;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000342 if (DestPointer) {
343 DestPointee = DestPointer->getPointeeType();
John McCall7decc9e2010-11-18 06:31:45 +0000344 } else if ((DestReference = DestType->getAs<ReferenceType>())) {
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000345 DestPointee = DestReference->getPointeeType();
Douglas Gregor465184a2011-01-22 00:06:57 +0000346 VK = isa<LValueReferenceType>(DestReference) ? VK_LValue
347 : isa<RValueReferenceType>(DestReference) ? VK_XValue
348 : VK_RValue;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000349 } else {
Chris Lattner377d1f82008-11-18 22:52:51 +0000350 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000351 << OrigDestType << DestRange;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000352 return;
353 }
354
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000355 const RecordType *DestRecord = DestPointee->getAs<RecordType>();
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000356 if (DestPointee->isVoidType()) {
357 assert(DestPointer && "Reference to void is not possible");
358 } else if (DestRecord) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000359 if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
Douglas Gregor89336232010-03-29 23:34:08 +0000360 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssond624e162009-08-26 23:45:07 +0000361 << DestRange))
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000362 return;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000363 } else {
Chris Lattner377d1f82008-11-18 22:52:51 +0000364 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000365 << DestPointee.getUnqualifiedType() << DestRange;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000366 return;
367 }
368
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000369 // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
370 // complete class type, [...]. If T is an lvalue reference type, v shall be
Douglas Gregor465184a2011-01-22 00:06:57 +0000371 // an lvalue of a complete class type, [...]. If T is an rvalue reference
372 // type, v shall be an expression having a complete class type, [...]
Sebastian Redl842ef522008-11-08 13:00:26 +0000373 QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000374 QualType SrcPointee;
375 if (DestPointer) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000376 if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000377 SrcPointee = SrcPointer->getPointeeType();
378 } else {
Chris Lattner377d1f82008-11-18 22:52:51 +0000379 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000380 << OrigSrcType << SrcExpr->getSourceRange();
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000381 return;
382 }
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000383 } else if (DestReference->isLValueReferenceType()) {
John McCall086a4642010-11-24 05:12:34 +0000384 if (!SrcExpr->isLValue()) {
Chris Lattner377d1f82008-11-18 22:52:51 +0000385 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
Sebastian Redl9f831db2009-07-25 15:41:38 +0000386 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000387 }
388 SrcPointee = SrcType;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000389 } else {
390 SrcPointee = SrcType;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000391 }
392
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000393 const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000394 if (SrcRecord) {
Douglas Gregored0cfbd2009-03-09 16:13:40 +0000395 if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
Douglas Gregor89336232010-03-29 23:34:08 +0000396 Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
Anders Carlssond624e162009-08-26 23:45:07 +0000397 << SrcExpr->getSourceRange()))
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000398 return;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000399 } else {
Chris Lattner29e812b2008-11-20 06:06:08 +0000400 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000401 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000402 return;
403 }
404
405 assert((DestPointer || DestReference) &&
406 "Bad destination non-ptr/ref slipped through.");
407 assert((DestRecord || DestPointee->isVoidType()) &&
408 "Bad destination pointee slipped through.");
409 assert(SrcRecord && "Bad source pointee slipped through.");
410
411 // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness.
412 if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
Chris Lattner377d1f82008-11-18 22:52:51 +0000413 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
Sebastian Redl9f831db2009-07-25 15:41:38 +0000414 << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000415 return;
416 }
417
418 // C++ 5.2.7p3: If the type of v is the same as the required result type,
419 // [except for cv].
420 if (DestRecord == SrcRecord) {
John McCalle3027922010-08-25 11:45:40 +0000421 Kind = CK_NoOp;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000422 return;
423 }
424
425 // C++ 5.2.7p5
426 // Upcasts are resolved statically.
Sebastian Redl842ef522008-11-08 13:00:26 +0000427 if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
Anders Carlssona70cff62010-04-24 19:06:50 +0000428 if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
429 OpRange.getBegin(), OpRange,
430 &BasePath))
431 return;
432
John McCalle3027922010-08-25 11:45:40 +0000433 Kind = CK_DerivedToBase;
Douglas Gregor88d292c2010-05-13 16:44:06 +0000434
435 // If we are casting to or through a virtual base class, we need a
436 // vtable.
437 if (Self.BasePathInvolvesVirtualBase(BasePath))
438 Self.MarkVTableUsed(OpRange.getBegin(),
439 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000440 return;
441 }
442
443 // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000444 const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition();
Sebastian Redlb426f6332008-11-06 15:59:35 +0000445 assert(SrcDecl && "Definition missing");
446 if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
Chris Lattner29e812b2008-11-20 06:06:08 +0000447 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000448 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redlb426f6332008-11-06 15:59:35 +0000449 }
Douglas Gregor88d292c2010-05-13 16:44:06 +0000450 Self.MarkVTableUsed(OpRange.getBegin(),
451 cast<CXXRecordDecl>(SrcRecord->getDecl()));
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000452
453 // Done. Everything else is run-time checks.
John McCalle3027922010-08-25 11:45:40 +0000454 Kind = CK_Dynamic;
Sebastian Redl3c5aa4d2008-11-05 21:50:06 +0000455}
Sebastian Redl9f831db2009-07-25 15:41:38 +0000456
457/// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
458/// Refer to C++ 5.2.11 for details. const_cast is typically used in code
459/// like this:
460/// const char *str = "literal";
461/// legacy_function(const_cast\<char*\>(str));
462void
John McCall7decc9e2010-11-18 06:31:45 +0000463CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType, ExprValueKind &VK,
Mike Stump11289f42009-09-09 15:08:12 +0000464 const SourceRange &OpRange, const SourceRange &DestRange) {
John McCall7decc9e2010-11-18 06:31:45 +0000465 VK = Expr::getValueKindForType(DestType);
466 if (VK == VK_RValue)
Douglas Gregorb92a1562010-02-03 00:27:59 +0000467 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000468
469 unsigned msg = diag::err_bad_cxx_cast_generic;
470 if (TryConstCast(Self, SrcExpr, DestType, /*CStyle*/false, msg) != TC_Success
471 && msg != 0)
472 Self.Diag(OpRange.getBegin(), msg) << CT_Const
473 << SrcExpr->getType() << DestType << OpRange;
474}
475
476/// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is
477/// valid.
478/// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code
479/// like this:
480/// char *bytes = reinterpret_cast\<char*\>(int_ptr);
481void
482CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCall7decc9e2010-11-18 06:31:45 +0000483 ExprValueKind &VK, const SourceRange &OpRange,
484 const SourceRange &DestRange, CastKind &Kind) {
485 VK = Expr::getValueKindForType(DestType);
486 if (VK == VK_RValue)
Douglas Gregorb92a1562010-02-03 00:27:59 +0000487 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000488
489 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlsson9d1b34b2009-09-26 00:12:34 +0000490 if (TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange,
491 msg, Kind)
Sebastian Redl9f831db2009-07-25 15:41:38 +0000492 != TC_Success && msg != 0)
Douglas Gregore81f58e2010-11-08 03:40:48 +0000493 {
494 if (SrcExpr->getType() == Self.Context.OverloadTy)
495 {
496 //FIXME: &f<int>; is overloaded and resolvable
497 Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload)
498 << OverloadExpr::find(SrcExpr).Expression->getName()
499 << DestType << OpRange;
500 NoteAllOverloadCandidates(SrcExpr, Self);
501
502 }
503 else
504 Self.Diag(OpRange.getBegin(), msg) << CT_Reinterpret
Sebastian Redl9f831db2009-07-25 15:41:38 +0000505 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregore81f58e2010-11-08 03:40:48 +0000506 }
507
Sebastian Redl9f831db2009-07-25 15:41:38 +0000508}
509
510
511/// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid.
512/// Refer to C++ 5.2.9 for details. Static casts are mostly used for making
513/// implicit conversions explicit and getting rid of data loss warnings.
514void
515CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
John McCall7decc9e2010-11-18 06:31:45 +0000516 ExprValueKind &VK, const SourceRange &OpRange,
517 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000518 // This test is outside everything else because it's the only case where
519 // a non-lvalue-reference target type does not lead to decay.
520 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Eli Friedman03bf60a2009-11-16 05:44:20 +0000521 if (DestType->isVoidType()) {
John McCall34376a62010-12-04 03:47:34 +0000522 Self.IgnoredValueConversions(SrcExpr);
John McCalle3027922010-08-25 11:45:40 +0000523 Kind = CK_ToVoid;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000524 return;
Eli Friedman03bf60a2009-11-16 05:44:20 +0000525 }
Sebastian Redl9f831db2009-07-25 15:41:38 +0000526
John McCall7decc9e2010-11-18 06:31:45 +0000527 VK = Expr::getValueKindForType(DestType);
528 if (VK == VK_RValue && !DestType->isRecordType())
Douglas Gregorb92a1562010-02-03 00:27:59 +0000529 Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000530
531 unsigned msg = diag::err_bad_cxx_cast_generic;
Sebastian Redl7c353682009-11-14 21:15:49 +0000532 if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, msg,
Anders Carlssona70cff62010-04-24 19:06:50 +0000533 Kind, BasePath) != TC_Success && msg != 0)
Douglas Gregore81f58e2010-11-08 03:40:48 +0000534 {
John McCall8cb679e2010-11-15 09:13:47 +0000535 if (SrcExpr->getType() == Self.Context.OverloadTy)
Douglas Gregore81f58e2010-11-08 03:40:48 +0000536 {
537 OverloadExpr* oe = OverloadExpr::find(SrcExpr).Expression;
538 Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
539 << oe->getName() << DestType << OpRange << oe->getQualifierRange();
540 NoteAllOverloadCandidates(SrcExpr, Self);
541 }
542 else
543 Self.Diag(OpRange.getBegin(), msg) << CT_Static
Sebastian Redl9f831db2009-07-25 15:41:38 +0000544 << SrcExpr->getType() << DestType << OpRange;
Douglas Gregore81f58e2010-11-08 03:40:48 +0000545 }
John McCalld50a2712010-11-16 05:46:29 +0000546 else if (Kind == CK_BitCast)
John McCall2b5c1b22010-08-12 21:44:57 +0000547 Self.CheckCastAlign(SrcExpr, DestType, OpRange);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000548}
549
550/// TryStaticCast - Check if a static cast can be performed, and do so if
551/// possible. If @p CStyle, ignore access restrictions on hierarchy casting
552/// and casting away constness.
Sebastian Redl7c353682009-11-14 21:15:49 +0000553static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000554 QualType DestType, bool CStyle,
Anders Carlssonf1ae6d42009-09-01 20:52:42 +0000555 const SourceRange &OpRange, unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000556 CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000557 CXXCastPath &BasePath) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000558 // The order the tests is not entirely arbitrary. There is one conversion
559 // that can be handled in two different ways. Given:
560 // struct A {};
561 // struct B : public A {
562 // B(); B(const A&);
563 // };
564 // const A &a = B();
565 // the cast static_cast<const B&>(a) could be seen as either a static
566 // reference downcast, or an explicit invocation of the user-defined
567 // conversion using B's conversion constructor.
568 // DR 427 specifies that the downcast is to be applied here.
569
570 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
571 // Done outside this function.
572
573 TryCastResult tcr;
574
575 // C++ 5.2.9p5, reference downcast.
576 // See the function for details.
577 // DR 427 specifies that this is to be applied before paragraph 2.
Sebastian Redl7c353682009-11-14 21:15:49 +0000578 tcr = TryStaticReferenceDowncast(Self, SrcExpr, DestType, CStyle, OpRange,
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000579 msg, Kind, BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000580 if (tcr != TC_NotApplicable)
581 return tcr;
582
Douglas Gregor465184a2011-01-22 00:06:57 +0000583 // C++0x [expr.static.cast]p3:
584 // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2
585 // T2" if "cv2 T2" is reference-compatible with "cv1 T1".
Douglas Gregorba278e22011-01-25 16:13:26 +0000586 tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, Kind, BasePath, msg);
587 if (tcr != TC_NotApplicable)
Sebastian Redl9f831db2009-07-25 15:41:38 +0000588 return tcr;
589
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,
Douglas Gregorba278e22011-01-25 16:13:26 +0000698 CastKind &Kind, CXXCastPath &BasePath, 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
Douglas Gregorba278e22011-01-25 16:13:26 +0000722 if (DerivedToBase) {
723 Kind = CK_DerivedToBase;
724 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
725 /*DetectVirtual=*/true);
726 if (!Self.IsDerivedFrom(SrcExpr->getType(), R->getPointeeType(), Paths))
727 return TC_NotApplicable;
728
729 Self.BuildBasePathArray(Paths, BasePath);
730 } else
731 Kind = CK_NoOp;
732
Sebastian Redl9f831db2009-07-25 15:41:38 +0000733 return TC_Success;
734}
735
736/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
737TryCastResult
738TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
739 bool CStyle, const SourceRange &OpRange,
John McCalle3027922010-08-25 11:45:40 +0000740 unsigned &msg, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000741 CXXCastPath &BasePath) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000742 // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
743 // cast to type "reference to cv2 D", where D is a class derived from B,
744 // if a valid standard conversion from "pointer to D" to "pointer to B"
745 // exists, cv2 >= cv1, and B is not a virtual base class of D.
746 // In addition, DR54 clarifies that the base must be accessible in the
747 // current context. Although the wording of DR54 only applies to the pointer
748 // variant of this rule, the intent is clearly for it to apply to the this
749 // conversion as well.
750
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000751 const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000752 if (!DestReference) {
753 return TC_NotApplicable;
754 }
755 bool RValueRef = DestReference->isRValueReferenceType();
John McCall086a4642010-11-24 05:12:34 +0000756 if (!RValueRef && !SrcExpr->isLValue()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000757 // We know the left side is an lvalue reference, so we can suggest a reason.
758 msg = diag::err_bad_cxx_cast_rvalue;
759 return TC_NotApplicable;
760 }
761
762 QualType DestPointee = DestReference->getPointeeType();
763
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000764 return TryStaticDowncast(Self,
765 Self.Context.getCanonicalType(SrcExpr->getType()),
766 Self.Context.getCanonicalType(DestPointee), CStyle,
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000767 OpRange, SrcExpr->getType(), DestType, msg, Kind,
768 BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000769}
770
771/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
772TryCastResult
773TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
Mike Stump11289f42009-09-09 15:08:12 +0000774 bool CStyle, const SourceRange &OpRange,
John McCalle3027922010-08-25 11:45:40 +0000775 unsigned &msg, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000776 CXXCastPath &BasePath) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000777 // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
778 // type, can be converted to an rvalue of type "pointer to cv2 D", where D
779 // is a class derived from B, if a valid standard conversion from "pointer
780 // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
781 // class of D.
782 // In addition, DR54 clarifies that the base must be accessible in the
783 // current context.
784
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000785 const PointerType *DestPointer = DestType->getAs<PointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000786 if (!DestPointer) {
787 return TC_NotApplicable;
788 }
789
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000790 const PointerType *SrcPointer = SrcType->getAs<PointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000791 if (!SrcPointer) {
792 msg = diag::err_bad_static_cast_pointer_nonpointer;
793 return TC_NotApplicable;
794 }
795
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000796 return TryStaticDowncast(Self,
797 Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
798 Self.Context.getCanonicalType(DestPointer->getPointeeType()),
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000799 CStyle, OpRange, SrcType, DestType, msg, Kind,
800 BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000801}
802
803/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
804/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000805/// DestType is possible and allowed.
Sebastian Redl9f831db2009-07-25 15:41:38 +0000806TryCastResult
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000807TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000808 bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
Anders Carlsson9a1cd872009-11-12 16:53:16 +0000809 QualType OrigDestType, unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +0000810 CastKind &Kind, CXXCastPath &BasePath) {
Sebastian Redl802f14c2009-10-22 15:07:22 +0000811 // We can only work with complete types. But don't complain if it doesn't work
Douglas Gregor89336232010-03-29 23:34:08 +0000812 if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) ||
813 Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0)))
Sebastian Redl802f14c2009-10-22 15:07:22 +0000814 return TC_NotApplicable;
815
Sebastian Redl9f831db2009-07-25 15:41:38 +0000816 // Downcast can only happen in class hierarchies, so we need classes.
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000817 if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000818 return TC_NotApplicable;
819 }
820
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000821 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000822 /*DetectVirtual=*/true);
Sebastian Redl9f831db2009-07-25 15:41:38 +0000823 if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
824 return TC_NotApplicable;
825 }
826
827 // Target type does derive from source type. Now we're serious. If an error
828 // appears now, it's not ignored.
829 // This may not be entirely in line with the standard. Take for example:
830 // struct A {};
831 // struct B : virtual A {
832 // B(A&);
833 // };
Mike Stump11289f42009-09-09 15:08:12 +0000834 //
Sebastian Redl9f831db2009-07-25 15:41:38 +0000835 // void f()
836 // {
837 // (void)static_cast<const B&>(*((A*)0));
838 // }
839 // As far as the standard is concerned, p5 does not apply (A is virtual), so
840 // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
841 // However, both GCC and Comeau reject this example, and accepting it would
842 // mean more complex code if we're to preserve the nice error message.
843 // FIXME: Being 100% compliant here would be nice to have.
844
845 // Must preserve cv, as always, unless we're in C-style mode.
846 if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
847 msg = diag::err_bad_cxx_cast_const_away;
848 return TC_Failed;
849 }
850
851 if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
852 // This code is analoguous to that in CheckDerivedToBaseConversion, except
853 // that it builds the paths in reverse order.
854 // To sum up: record all paths to the base and build a nice string from
855 // them. Use it to spice up the error message.
856 if (!Paths.isRecordingPaths()) {
857 Paths.clear();
858 Paths.setRecordingPaths(true);
859 Self.IsDerivedFrom(DestType, SrcType, Paths);
860 }
861 std::string PathDisplayStr;
862 std::set<unsigned> DisplayedPaths;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000863 for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000864 PI != PE; ++PI) {
865 if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
866 // We haven't displayed a path to this particular base
867 // class subobject yet.
868 PathDisplayStr += "\n ";
Douglas Gregor36d1b142009-10-06 17:59:45 +0000869 for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
870 EE = PI->rend();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000871 EI != EE; ++EI)
872 PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000873 PathDisplayStr += QualType(DestType).getAsString();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000874 }
875 }
876
877 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
Douglas Gregor8f3952c2009-11-15 09:20:52 +0000878 << QualType(SrcType).getUnqualifiedType()
879 << QualType(DestType).getUnqualifiedType()
Sebastian Redl9f831db2009-07-25 15:41:38 +0000880 << PathDisplayStr << OpRange;
881 msg = 0;
882 return TC_Failed;
883 }
884
885 if (Paths.getDetectedVirtual() != 0) {
886 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
887 Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
888 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
889 msg = 0;
890 return TC_Failed;
891 }
892
John McCall5b0829a2010-02-10 09:31:12 +0000893 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
John McCall5b0829a2010-02-10 09:31:12 +0000894 SrcType, DestType,
John McCall1064d7e2010-03-16 05:22:47 +0000895 Paths.front(),
896 diag::err_downcast_from_inaccessible_base)) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000897 msg = 0;
898 return TC_Failed;
899 }
900
Anders Carlsson7d3360f2010-04-24 19:36:51 +0000901 Self.BuildBasePathArray(Paths, BasePath);
John McCalle3027922010-08-25 11:45:40 +0000902 Kind = CK_BaseToDerived;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000903 return TC_Success;
904}
905
906/// TryStaticMemberPointerUpcast - Tests whether a conversion according to
907/// C++ 5.2.9p9 is valid:
908///
909/// An rvalue of type "pointer to member of D of type cv1 T" can be
910/// converted to an rvalue of type "pointer to member of B of type cv2 T",
911/// where B is a base class of D [...].
912///
913TryCastResult
Douglas Gregorc934bc82010-03-07 23:24:59 +0000914TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr, QualType SrcType,
915 QualType DestType, bool CStyle,
916 const SourceRange &OpRange,
John McCalle3027922010-08-25 11:45:40 +0000917 unsigned &msg, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +0000918 CXXCastPath &BasePath) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000919 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000920 if (!DestMemPtr)
921 return TC_NotApplicable;
Douglas Gregorc934bc82010-03-07 23:24:59 +0000922
923 bool WasOverloadedFunction = false;
John McCall16df1e52010-03-30 21:47:33 +0000924 DeclAccessPair FoundOverload;
Douglas Gregor064fdb22010-04-14 23:11:21 +0000925 if (SrcExpr->getType() == Self.Context.OverloadTy) {
926 if (FunctionDecl *Fn
927 = Self.ResolveAddressOfOverloadedFunction(SrcExpr, DestType, false,
928 FoundOverload)) {
929 CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
930 SrcType = Self.Context.getMemberPointerType(Fn->getType(),
931 Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
932 WasOverloadedFunction = true;
933 }
Douglas Gregorc934bc82010-03-07 23:24:59 +0000934 }
Douglas Gregor064fdb22010-04-14 23:11:21 +0000935
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000936 const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +0000937 if (!SrcMemPtr) {
938 msg = diag::err_bad_static_cast_member_pointer_nonmp;
939 return TC_NotApplicable;
940 }
941
942 // T == T, modulo cv
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000943 if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
944 DestMemPtr->getPointeeType()))
Sebastian Redl9f831db2009-07-25 15:41:38 +0000945 return TC_NotApplicable;
946
947 // B base of D
948 QualType SrcClass(SrcMemPtr->getClass(), 0);
949 QualType DestClass(DestMemPtr->getClass(), 0);
Anders Carlssonb78feca2010-04-24 19:22:20 +0000950 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Sebastian Redl9f831db2009-07-25 15:41:38 +0000951 /*DetectVirtual=*/true);
952 if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
953 return TC_NotApplicable;
954 }
955
956 // 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 +0000957 if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000958 Paths.clear();
959 Paths.setRecordingPaths(true);
960 bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
961 assert(StillOkay);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000962 (void)StillOkay;
Sebastian Redl9f831db2009-07-25 15:41:38 +0000963 std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
964 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
965 << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
966 msg = 0;
967 return TC_Failed;
968 }
969
970 if (const RecordType *VBase = Paths.getDetectedVirtual()) {
971 Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
972 << SrcClass << DestClass << QualType(VBase, 0) << OpRange;
973 msg = 0;
974 return TC_Failed;
975 }
976
John McCall5b0829a2010-02-10 09:31:12 +0000977 if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
Eli Friedmand4c75cd2010-07-23 19:25:41 +0000978 DestClass, SrcClass,
John McCall1064d7e2010-03-16 05:22:47 +0000979 Paths.front(),
980 diag::err_upcast_to_inaccessible_base)) {
Sebastian Redl9f831db2009-07-25 15:41:38 +0000981 msg = 0;
982 return TC_Failed;
983 }
984
Douglas Gregorc934bc82010-03-07 23:24:59 +0000985 if (WasOverloadedFunction) {
986 // Resolve the address of the overloaded function again, this time
987 // allowing complaints if something goes wrong.
988 FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr,
989 DestType,
John McCall16df1e52010-03-30 21:47:33 +0000990 true,
991 FoundOverload);
Douglas Gregorc934bc82010-03-07 23:24:59 +0000992 if (!Fn) {
993 msg = 0;
994 return TC_Failed;
995 }
996
John McCall16df1e52010-03-30 21:47:33 +0000997 SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
Douglas Gregorc934bc82010-03-07 23:24:59 +0000998 if (!SrcExpr) {
999 msg = 0;
1000 return TC_Failed;
1001 }
1002 }
1003
Anders Carlssonb78feca2010-04-24 19:22:20 +00001004 Self.BuildBasePathArray(Paths, BasePath);
John McCalle3027922010-08-25 11:45:40 +00001005 Kind = CK_DerivedToBaseMemberPointer;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001006 return TC_Success;
1007}
1008
1009/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
1010/// is valid:
1011///
1012/// An expression e can be explicitly converted to a type T using a
1013/// @c static_cast if the declaration "T t(e);" is well-formed [...].
1014TryCastResult
Sebastian Redl7c353682009-11-14 21:15:49 +00001015TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
Fariborz Jahanian1cec0c42009-08-26 18:55:36 +00001016 bool CStyle, const SourceRange &OpRange, unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +00001017 CastKind &Kind) {
Anders Carlsson85ec4ff2009-09-07 18:25:47 +00001018 if (DestType->isRecordType()) {
1019 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
1020 diag::err_bad_dynamic_cast_incomplete)) {
1021 msg = 0;
1022 return TC_Failed;
1023 }
1024 }
Douglas Gregorb33eed02010-04-16 22:09:46 +00001025
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00001026 InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
1027 InitializationKind InitKind
Douglas Gregorb33eed02010-04-16 22:09:46 +00001028 = InitializationKind::CreateCast(/*FIXME:*/OpRange,
1029 CStyle);
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00001030 InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExpr, 1);
Douglas Gregore81f58e2010-11-08 03:40:48 +00001031
1032 // At this point of CheckStaticCast, if the destination is a reference,
1033 // or the expression is an overload expression this has to work.
1034 // There is no other way that works.
1035 // On the other hand, if we're checking a C-style cast, we've still got
1036 // the reinterpret_cast way.
1037
Douglas Gregorb33eed02010-04-16 22:09:46 +00001038 if (InitSeq.getKind() == InitializationSequence::FailedSequence &&
Douglas Gregore81f58e2010-11-08 03:40:48 +00001039 (CStyle || !DestType->isReferenceType()))
Anders Carlsson9d1b34b2009-09-26 00:12:34 +00001040 return TC_NotApplicable;
Douglas Gregorb33eed02010-04-16 22:09:46 +00001041
John McCalldadc5752010-08-24 06:29:42 +00001042 ExprResult Result
John McCallfaf5fb42010-08-26 23:41:50 +00001043 = InitSeq.Perform(Self, Entity, InitKind, MultiExprArg(Self, &SrcExpr, 1));
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00001044 if (Result.isInvalid()) {
1045 msg = 0;
1046 return TC_Failed;
1047 }
1048
Douglas Gregorb33eed02010-04-16 22:09:46 +00001049 if (InitSeq.isConstructorInitialization())
John McCalle3027922010-08-25 11:45:40 +00001050 Kind = CK_ConstructorConversion;
Douglas Gregorb33eed02010-04-16 22:09:46 +00001051 else
John McCalle3027922010-08-25 11:45:40 +00001052 Kind = CK_NoOp;
Douglas Gregorb33eed02010-04-16 22:09:46 +00001053
Douglas Gregor5c8ffab2010-04-16 19:30:02 +00001054 SrcExpr = Result.takeAs<Expr>();
1055 return TC_Success;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001056}
1057
1058/// TryConstCast - See if a const_cast from source to destination is allowed,
1059/// and perform it if it is.
1060static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
1061 bool CStyle, unsigned &msg) {
1062 DestType = Self.Context.getCanonicalType(DestType);
1063 QualType SrcType = SrcExpr->getType();
Douglas Gregorc1ed20c2011-01-22 00:19:52 +00001064 if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) {
1065 if (DestTypeTmp->isLValueReferenceType() && !SrcExpr->isLValue()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001066 // Cannot const_cast non-lvalue to lvalue reference type. But if this
1067 // is C-style, static_cast might find a way, so we simply suggest a
1068 // message and tell the parent to keep searching.
1069 msg = diag::err_bad_cxx_cast_rvalue;
1070 return TC_NotApplicable;
1071 }
1072
1073 // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
1074 // [...] if a pointer to T1 can be [cast] to the type pointer to T2.
1075 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1076 SrcType = Self.Context.getPointerType(SrcType);
1077 }
1078
1079 // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
1080 // the rules for const_cast are the same as those used for pointers.
1081
John McCall0e704f72010-05-18 09:35:29 +00001082 if (!DestType->isPointerType() &&
1083 !DestType->isMemberPointerType() &&
1084 !DestType->isObjCObjectPointerType()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001085 // Cannot cast to non-pointer, non-reference type. Note that, if DestType
1086 // was a reference type, we converted it to a pointer above.
1087 // The status of rvalue references isn't entirely clear, but it looks like
1088 // conversion to them is simply invalid.
1089 // C++ 5.2.11p3: For two pointer types [...]
1090 if (!CStyle)
1091 msg = diag::err_bad_const_cast_dest;
1092 return TC_NotApplicable;
1093 }
1094 if (DestType->isFunctionPointerType() ||
1095 DestType->isMemberFunctionPointerType()) {
1096 // Cannot cast direct function pointers.
1097 // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
1098 // T is the ultimate pointee of source and target type.
1099 if (!CStyle)
1100 msg = diag::err_bad_const_cast_dest;
1101 return TC_NotApplicable;
1102 }
1103 SrcType = Self.Context.getCanonicalType(SrcType);
1104
1105 // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
1106 // completely equal.
1107 // FIXME: const_cast should probably not be able to convert between pointers
1108 // to different address spaces.
1109 // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
1110 // in multi-level pointers may change, but the level count must be the same,
1111 // as must be the final pointee type.
1112 while (SrcType != DestType &&
Douglas Gregor1fc3d662010-06-09 03:53:18 +00001113 Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) {
Chandler Carruth585fb1e2009-12-29 08:05:19 +00001114 Qualifiers Quals;
1115 SrcType = Self.Context.getUnqualifiedArrayType(SrcType, Quals);
1116 DestType = Self.Context.getUnqualifiedArrayType(DestType, Quals);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001117 }
1118
1119 // Since we're dealing in canonical types, the remainder must be the same.
1120 if (SrcType != DestType)
1121 return TC_NotApplicable;
1122
1123 return TC_Success;
1124}
1125
Douglas Gregore81f58e2010-11-08 03:40:48 +00001126
1127static void NoteAllOverloadCandidates(Expr* const Expr, Sema& sema)
1128{
1129
1130 assert(Expr->getType() == sema.Context.OverloadTy);
1131
1132 OverloadExpr::FindResult Ovl = OverloadExpr::find(Expr);
1133 OverloadExpr *const OvlExpr = Ovl.Expression;
1134
1135 for (UnresolvedSetIterator it = OvlExpr->decls_begin(),
1136 end = OvlExpr->decls_end(); it != end; ++it) {
1137 if ( FunctionTemplateDecl *ftd =
1138 dyn_cast<FunctionTemplateDecl>((*it)->getUnderlyingDecl()) )
1139 {
1140 sema.NoteOverloadCandidate(ftd->getTemplatedDecl());
1141 }
1142 else if ( FunctionDecl *f =
1143 dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl()) )
1144 {
1145 sema.NoteOverloadCandidate(f);
1146 }
1147 }
1148}
1149
1150
Sebastian Redl9f831db2009-07-25 15:41:38 +00001151static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
1152 QualType DestType, bool CStyle,
1153 const SourceRange &OpRange,
Anders Carlsson9d1b34b2009-09-26 00:12:34 +00001154 unsigned &msg,
John McCalle3027922010-08-25 11:45:40 +00001155 CastKind &Kind) {
Douglas Gregor51954272010-07-13 23:17:26 +00001156 bool IsLValueCast = false;
1157
Sebastian Redl9f831db2009-07-25 15:41:38 +00001158 DestType = Self.Context.getCanonicalType(DestType);
1159 QualType SrcType = SrcExpr->getType();
Douglas Gregore81f58e2010-11-08 03:40:48 +00001160
1161 // Is the source an overloaded name? (i.e. &foo)
1162 // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5)
John McCalle84af4e2010-11-13 01:35:44 +00001163 if (SrcType == Self.Context.OverloadTy)
Douglas Gregore81f58e2010-11-08 03:40:48 +00001164 return TC_NotApplicable;
1165
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001166 if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001167 bool LValue = DestTypeTmp->isLValueReferenceType();
John McCall086a4642010-11-24 05:12:34 +00001168 if (LValue && !SrcExpr->isLValue()) {
Douglas Gregorc1ed20c2011-01-22 00:19:52 +00001169 // Cannot cast non-lvalue to lvalue reference type. See the similar
1170 // comment in const_cast.
Sebastian Redl9f831db2009-07-25 15:41:38 +00001171 msg = diag::err_bad_cxx_cast_rvalue;
1172 return TC_NotApplicable;
1173 }
1174
1175 // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
1176 // same effect as the conversion *reinterpret_cast<T*>(&x) with the
1177 // built-in & and * operators.
1178 // This code does this transformation for the checked types.
1179 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1180 SrcType = Self.Context.getPointerType(SrcType);
Douglas Gregore81f58e2010-11-08 03:40:48 +00001181
Douglas Gregor51954272010-07-13 23:17:26 +00001182 IsLValueCast = true;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001183 }
1184
1185 // Canonicalize source for comparison.
1186 SrcType = Self.Context.getCanonicalType(SrcType);
1187
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001188 const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
1189 *SrcMemPtr = SrcType->getAs<MemberPointerType>();
Sebastian Redl9f831db2009-07-25 15:41:38 +00001190 if (DestMemPtr && SrcMemPtr) {
1191 // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
1192 // can be explicitly converted to an rvalue of type "pointer to member
1193 // of Y of type T2" if T1 and T2 are both function types or both object
1194 // types.
1195 if (DestMemPtr->getPointeeType()->isFunctionType() !=
1196 SrcMemPtr->getPointeeType()->isFunctionType())
1197 return TC_NotApplicable;
1198
1199 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
1200 // constness.
1201 // A reinterpret_cast followed by a const_cast can, though, so in C-style,
1202 // we accept it.
1203 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1204 msg = diag::err_bad_cxx_cast_const_away;
1205 return TC_Failed;
1206 }
1207
Charles Davisebab1ed2010-08-16 05:30:44 +00001208 // Don't allow casting between member pointers of different sizes.
1209 if (Self.Context.getTypeSize(DestMemPtr) !=
1210 Self.Context.getTypeSize(SrcMemPtr)) {
1211 msg = diag::err_bad_cxx_cast_member_pointer_size;
1212 return TC_Failed;
1213 }
1214
Sebastian Redl9f831db2009-07-25 15:41:38 +00001215 // A valid member pointer cast.
John McCalle3027922010-08-25 11:45:40 +00001216 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001217 return TC_Success;
1218 }
1219
1220 // See below for the enumeral issue.
Douglas Gregor6972a622010-06-16 00:35:25 +00001221 if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001222 // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
1223 // type large enough to hold it. A value of std::nullptr_t can be
1224 // converted to an integral type; the conversion has the same meaning
1225 // and validity as a conversion of (void*)0 to the integral type.
1226 if (Self.Context.getTypeSize(SrcType) >
1227 Self.Context.getTypeSize(DestType)) {
1228 msg = diag::err_bad_reinterpret_cast_small_int;
1229 return TC_Failed;
1230 }
John McCalle3027922010-08-25 11:45:40 +00001231 Kind = CK_PointerToIntegral;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001232 return TC_Success;
1233 }
1234
Anders Carlsson570af5d2009-09-16 19:19:43 +00001235 bool destIsVector = DestType->isVectorType();
1236 bool srcIsVector = SrcType->isVectorType();
1237 if (srcIsVector || destIsVector) {
Douglas Gregor6972a622010-06-16 00:35:25 +00001238 // FIXME: Should this also apply to floating point types?
1239 bool srcIsScalar = SrcType->isIntegralType(Self.Context);
1240 bool destIsScalar = DestType->isIntegralType(Self.Context);
Anders Carlsson570af5d2009-09-16 19:19:43 +00001241
1242 // Check if this is a cast between a vector and something else.
1243 if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
1244 !(srcIsVector && destIsVector))
1245 return TC_NotApplicable;
1246
1247 // If both types have the same size, we can successfully cast.
Douglas Gregor19fc0b72009-12-22 22:47:22 +00001248 if (Self.Context.getTypeSize(SrcType)
1249 == Self.Context.getTypeSize(DestType)) {
John McCalle3027922010-08-25 11:45:40 +00001250 Kind = CK_BitCast;
Anders Carlsson570af5d2009-09-16 19:19:43 +00001251 return TC_Success;
Douglas Gregor19fc0b72009-12-22 22:47:22 +00001252 }
Anders Carlsson570af5d2009-09-16 19:19:43 +00001253
1254 if (destIsScalar)
1255 msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
1256 else if (srcIsScalar)
1257 msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
1258 else
1259 msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
1260
1261 return TC_Failed;
1262 }
1263
Douglas Gregoreaff2cb2010-07-08 20:27:32 +00001264 bool destIsPtr = DestType->isAnyPointerType() ||
1265 DestType->isBlockPointerType();
1266 bool srcIsPtr = SrcType->isAnyPointerType() ||
1267 SrcType->isBlockPointerType();
Sebastian Redl9f831db2009-07-25 15:41:38 +00001268 if (!destIsPtr && !srcIsPtr) {
1269 // Except for std::nullptr_t->integer and lvalue->reference, which are
1270 // handled above, at least one of the two arguments must be a pointer.
1271 return TC_NotApplicable;
1272 }
1273
1274 if (SrcType == DestType) {
1275 // C++ 5.2.10p2 has a note that mentions that, subject to all other
1276 // restrictions, a cast to the same type is allowed. The intent is not
1277 // entirely clear here, since all other paragraphs explicitly forbid casts
1278 // to the same type. However, the behavior of compilers is pretty consistent
1279 // on this point: allow same-type conversion if the involved types are
1280 // pointers, disallow otherwise.
John McCalle3027922010-08-25 11:45:40 +00001281 Kind = CK_NoOp;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001282 return TC_Success;
1283 }
1284
Douglas Gregor6972a622010-06-16 00:35:25 +00001285 if (DestType->isIntegralType(Self.Context)) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001286 assert(srcIsPtr && "One type must be a pointer");
1287 // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
1288 // type large enough to hold it.
1289 if (Self.Context.getTypeSize(SrcType) >
1290 Self.Context.getTypeSize(DestType)) {
1291 msg = diag::err_bad_reinterpret_cast_small_int;
1292 return TC_Failed;
1293 }
John McCalle3027922010-08-25 11:45:40 +00001294 Kind = CK_PointerToIntegral;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001295 return TC_Success;
1296 }
1297
Douglas Gregorb90df602010-06-16 00:17:44 +00001298 if (SrcType->isIntegralOrEnumerationType()) {
Sebastian Redl9f831db2009-07-25 15:41:38 +00001299 assert(destIsPtr && "One type must be a pointer");
1300 // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
1301 // converted to a pointer.
John McCalle84af4e2010-11-13 01:35:44 +00001302 // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not
1303 // necessarily converted to a null pointer value.]
John McCalle3027922010-08-25 11:45:40 +00001304 Kind = CK_IntegralToPointer;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001305 return TC_Success;
1306 }
1307
1308 if (!destIsPtr || !srcIsPtr) {
1309 // With the valid non-pointer conversions out of the way, we can be even
1310 // more stringent.
1311 return TC_NotApplicable;
1312 }
1313
1314 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
1315 // The C-style cast operator can.
1316 if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
1317 msg = diag::err_bad_cxx_cast_const_away;
1318 return TC_Failed;
1319 }
Douglas Gregoreaff2cb2010-07-08 20:27:32 +00001320
1321 // Cannot convert between block pointers and Objective-C object pointers.
1322 if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) ||
1323 (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType()))
1324 return TC_NotApplicable;
1325
1326 // Any pointer can be cast to an Objective-C pointer type with a C-style
1327 // cast.
Fariborz Jahanian859c4152009-12-08 23:09:15 +00001328 if (CStyle && DestType->isObjCObjectPointerType()) {
John McCalle3027922010-08-25 11:45:40 +00001329 Kind = CK_AnyPointerToObjCPointerCast;
Fariborz Jahanian859c4152009-12-08 23:09:15 +00001330 return TC_Success;
1331 }
Douglas Gregoreaff2cb2010-07-08 20:27:32 +00001332
Sebastian Redl9f831db2009-07-25 15:41:38 +00001333 // Not casting away constness, so the only remaining check is for compatible
1334 // pointer categories.
John McCalle3027922010-08-25 11:45:40 +00001335 Kind = IsLValueCast? CK_LValueBitCast : CK_BitCast;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001336
1337 if (SrcType->isFunctionPointerType()) {
1338 if (DestType->isFunctionPointerType()) {
1339 // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
1340 // a pointer to a function of a different type.
1341 return TC_Success;
1342 }
1343
1344 // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
1345 // an object type or vice versa is conditionally-supported.
1346 // Compilers support it in C++03 too, though, because it's necessary for
1347 // casting the return value of dlsym() and GetProcAddress().
1348 // FIXME: Conditionally-supported behavior should be configurable in the
1349 // TargetInfo or similar.
1350 if (!Self.getLangOptions().CPlusPlus0x)
1351 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1352 return TC_Success;
1353 }
1354
1355 if (DestType->isFunctionPointerType()) {
1356 // See above.
1357 if (!Self.getLangOptions().CPlusPlus0x)
1358 Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
1359 return TC_Success;
1360 }
Douglas Gregoreaff2cb2010-07-08 20:27:32 +00001361
Sebastian Redl9f831db2009-07-25 15:41:38 +00001362 // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
1363 // a pointer to an object of different type.
1364 // Void pointers are not specified, but supported by every compiler out there.
1365 // So we finish by allowing everything that remains - it's got to be two
1366 // object pointers.
1367 return TC_Success;
1368}
1369
Anders Carlssona70cff62010-04-24 19:06:50 +00001370bool
John McCall7decc9e2010-11-18 06:31:45 +00001371Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, ExprValueKind &VK,
1372 Expr *&CastExpr, CastKind &Kind,
John McCallcf142162010-08-07 06:22:56 +00001373 CXXCastPath &BasePath,
Anders Carlssona70cff62010-04-24 19:06:50 +00001374 bool FunctionalStyle) {
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001375 if (CastExpr->isBoundMemberFunction(Context))
1376 return Diag(CastExpr->getLocStart(),
1377 diag::err_invalid_use_of_bound_member_func)
1378 << CastExpr->getSourceRange();
1379
Sebastian Redl9f831db2009-07-25 15:41:38 +00001380 // This test is outside everything else because it's the only case where
1381 // a non-lvalue-reference target type does not lead to decay.
1382 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
Anders Carlsson9500ad12009-10-18 20:31:03 +00001383 if (CastTy->isVoidType()) {
John McCall34376a62010-12-04 03:47:34 +00001384 IgnoredValueConversions(CastExpr);
John McCalle3027922010-08-25 11:45:40 +00001385 Kind = CK_ToVoid;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001386 return false;
Anders Carlsson9500ad12009-10-18 20:31:03 +00001387 }
Sebastian Redl9f831db2009-07-25 15:41:38 +00001388
John McCall4cec5f82010-11-30 02:05:44 +00001389 // Make sure we determine the value kind before we bail out for
1390 // dependent types.
1391 VK = Expr::getValueKindForType(CastTy);
1392
Sebastian Redl9f831db2009-07-25 15:41:38 +00001393 // If the type is dependent, we won't do any other semantic analysis now.
John McCall8cb679e2010-11-15 09:13:47 +00001394 if (CastTy->isDependentType() || CastExpr->isTypeDependent()) {
1395 Kind = CK_Dependent;
Sebastian Redl9f831db2009-07-25 15:41:38 +00001396 return false;
John McCall8cb679e2010-11-15 09:13:47 +00001397 }
Sebastian Redl9f831db2009-07-25 15:41:38 +00001398
John McCall7decc9e2010-11-18 06:31:45 +00001399 if (VK == VK_RValue && !CastTy->isRecordType())
Douglas Gregorb92a1562010-02-03 00:27:59 +00001400 DefaultFunctionArrayLvalueConversion(CastExpr);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001401
1402 // C++ [expr.cast]p5: The conversions performed by
1403 // - a const_cast,
1404 // - a static_cast,
1405 // - a static_cast followed by a const_cast,
1406 // - a reinterpret_cast, or
1407 // - a reinterpret_cast followed by a const_cast,
1408 // can be performed using the cast notation of explicit type conversion.
1409 // [...] If a conversion can be interpreted in more than one of the ways
1410 // listed above, the interpretation that appears first in the list is used,
1411 // even if a cast resulting from that interpretation is ill-formed.
1412 // In plain language, this means trying a const_cast ...
1413 unsigned msg = diag::err_bad_cxx_cast_generic;
Anders Carlssonf1ae6d42009-09-01 20:52:42 +00001414 TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,
1415 msg);
Anders Carlsson027732b2009-10-19 18:14:28 +00001416 if (tcr == TC_Success)
John McCalle3027922010-08-25 11:45:40 +00001417 Kind = CK_NoOp;
Anders Carlsson027732b2009-10-19 18:14:28 +00001418
Sebastian Redl9f831db2009-07-25 15:41:38 +00001419 if (tcr == TC_NotApplicable) {
1420 // ... or if that is not possible, a static_cast, ignoring const, ...
Anders Carlssona70cff62010-04-24 19:06:50 +00001421 tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg, Kind,
1422 BasePath);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001423 if (tcr == TC_NotApplicable) {
1424 // ... and finally a reinterpret_cast, ignoring const.
Anders Carlsson9d1b34b2009-09-26 00:12:34 +00001425 tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg,
1426 Kind);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001427 }
1428 }
1429
Nick Lewycky14d88eb2010-11-09 00:19:31 +00001430 if (tcr != TC_Success && msg != 0) {
1431 if (CastExpr->getType() == Context.OverloadTy) {
Douglas Gregore81f58e2010-11-08 03:40:48 +00001432 DeclAccessPair Found;
Nick Lewycky14d88eb2010-11-09 00:19:31 +00001433 FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(CastExpr,
Douglas Gregore81f58e2010-11-08 03:40:48 +00001434 CastTy,
1435 /* Complain */ true,
1436 Found);
1437 assert(!Fn && "cast failed but able to resolve overload expression!!");
Nick Lewycky14d88eb2010-11-09 00:19:31 +00001438 (void)Fn;
1439 } else {
Douglas Gregore81f58e2010-11-08 03:40:48 +00001440 Diag(R.getBegin(), msg) << (FunctionalStyle ? CT_Functional : CT_CStyle)
1441 << CastExpr->getType() << CastTy << R;
1442 }
1443 }
John McCalld50a2712010-11-16 05:46:29 +00001444 else if (Kind == CK_BitCast)
John McCall2b5c1b22010-08-12 21:44:57 +00001445 CheckCastAlign(CastExpr, CastTy, R);
Sebastian Redl9f831db2009-07-25 15:41:38 +00001446
1447 return tcr != TC_Success;
1448}