1f4a2713aSLionel Sambuc //===--- SemaCast.cpp - Semantic Analysis for Casts -----------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements semantic analysis for cast expressions, including
11f4a2713aSLionel Sambuc // 1) C-style casts like '(int) x'
12f4a2713aSLionel Sambuc // 2) C++ functional casts like 'int(x)'
13f4a2713aSLionel Sambuc // 3) C++ named casts like 'static_cast<int>(x)'
14f4a2713aSLionel Sambuc //
15f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
16f4a2713aSLionel Sambuc
17f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
19f4a2713aSLionel Sambuc #include "clang/AST/CXXInheritance.h"
20f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
21f4a2713aSLionel Sambuc #include "clang/AST/ExprObjC.h"
22f4a2713aSLionel Sambuc #include "clang/AST/RecordLayout.h"
23f4a2713aSLionel Sambuc #include "clang/Basic/PartialDiagnostic.h"
24*0a6a1f1dSLionel Sambuc #include "clang/Basic/TargetInfo.h"
25f4a2713aSLionel Sambuc #include "clang/Sema/Initialization.h"
26f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
27f4a2713aSLionel Sambuc #include <set>
28f4a2713aSLionel Sambuc using namespace clang;
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc enum TryCastResult {
33f4a2713aSLionel Sambuc TC_NotApplicable, ///< The cast method is not applicable.
34f4a2713aSLionel Sambuc TC_Success, ///< The cast method is appropriate and successful.
35f4a2713aSLionel Sambuc TC_Failed ///< The cast method is appropriate, but failed. A
36f4a2713aSLionel Sambuc ///< diagnostic has been emitted.
37f4a2713aSLionel Sambuc };
38f4a2713aSLionel Sambuc
39f4a2713aSLionel Sambuc enum CastType {
40f4a2713aSLionel Sambuc CT_Const, ///< const_cast
41f4a2713aSLionel Sambuc CT_Static, ///< static_cast
42f4a2713aSLionel Sambuc CT_Reinterpret, ///< reinterpret_cast
43f4a2713aSLionel Sambuc CT_Dynamic, ///< dynamic_cast
44f4a2713aSLionel Sambuc CT_CStyle, ///< (Type)expr
45f4a2713aSLionel Sambuc CT_Functional ///< Type(expr)
46f4a2713aSLionel Sambuc };
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc namespace {
49f4a2713aSLionel Sambuc struct CastOperation {
CastOperation__anon130919f30111::CastOperation50f4a2713aSLionel Sambuc CastOperation(Sema &S, QualType destType, ExprResult src)
51f4a2713aSLionel Sambuc : Self(S), SrcExpr(src), DestType(destType),
52f4a2713aSLionel Sambuc ResultType(destType.getNonLValueExprType(S.Context)),
53f4a2713aSLionel Sambuc ValueKind(Expr::getValueKindForType(destType)),
54f4a2713aSLionel Sambuc Kind(CK_Dependent), IsARCUnbridgedCast(false) {
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc if (const BuiltinType *placeholder =
57f4a2713aSLionel Sambuc src.get()->getType()->getAsPlaceholderType()) {
58f4a2713aSLionel Sambuc PlaceholderKind = placeholder->getKind();
59f4a2713aSLionel Sambuc } else {
60f4a2713aSLionel Sambuc PlaceholderKind = (BuiltinType::Kind) 0;
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc }
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc Sema &Self;
65f4a2713aSLionel Sambuc ExprResult SrcExpr;
66f4a2713aSLionel Sambuc QualType DestType;
67f4a2713aSLionel Sambuc QualType ResultType;
68f4a2713aSLionel Sambuc ExprValueKind ValueKind;
69f4a2713aSLionel Sambuc CastKind Kind;
70f4a2713aSLionel Sambuc BuiltinType::Kind PlaceholderKind;
71f4a2713aSLionel Sambuc CXXCastPath BasePath;
72f4a2713aSLionel Sambuc bool IsARCUnbridgedCast;
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc SourceRange OpRange;
75f4a2713aSLionel Sambuc SourceRange DestRange;
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc // Top-level semantics-checking routines.
78f4a2713aSLionel Sambuc void CheckConstCast();
79f4a2713aSLionel Sambuc void CheckReinterpretCast();
80f4a2713aSLionel Sambuc void CheckStaticCast();
81f4a2713aSLionel Sambuc void CheckDynamicCast();
82f4a2713aSLionel Sambuc void CheckCXXCStyleCast(bool FunctionalCast, bool ListInitialization);
83f4a2713aSLionel Sambuc void CheckCStyleCast();
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc /// Complete an apparently-successful cast operation that yields
86f4a2713aSLionel Sambuc /// the given expression.
complete__anon130919f30111::CastOperation87f4a2713aSLionel Sambuc ExprResult complete(CastExpr *castExpr) {
88f4a2713aSLionel Sambuc // If this is an unbridged cast, wrap the result in an implicit
89f4a2713aSLionel Sambuc // cast that yields the unbridged-cast placeholder type.
90f4a2713aSLionel Sambuc if (IsARCUnbridgedCast) {
91f4a2713aSLionel Sambuc castExpr = ImplicitCastExpr::Create(Self.Context,
92f4a2713aSLionel Sambuc Self.Context.ARCUnbridgedCastTy,
93*0a6a1f1dSLionel Sambuc CK_Dependent, castExpr, nullptr,
94f4a2713aSLionel Sambuc castExpr->getValueKind());
95f4a2713aSLionel Sambuc }
96*0a6a1f1dSLionel Sambuc return castExpr;
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc
99f4a2713aSLionel Sambuc // Internal convenience methods.
100f4a2713aSLionel Sambuc
101f4a2713aSLionel Sambuc /// Try to handle the given placeholder expression kind. Return
102f4a2713aSLionel Sambuc /// true if the source expression has the appropriate placeholder
103f4a2713aSLionel Sambuc /// kind. A placeholder can only be claimed once.
claimPlaceholder__anon130919f30111::CastOperation104f4a2713aSLionel Sambuc bool claimPlaceholder(BuiltinType::Kind K) {
105f4a2713aSLionel Sambuc if (PlaceholderKind != K) return false;
106f4a2713aSLionel Sambuc
107f4a2713aSLionel Sambuc PlaceholderKind = (BuiltinType::Kind) 0;
108f4a2713aSLionel Sambuc return true;
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc
isPlaceholder__anon130919f30111::CastOperation111f4a2713aSLionel Sambuc bool isPlaceholder() const {
112f4a2713aSLionel Sambuc return PlaceholderKind != 0;
113f4a2713aSLionel Sambuc }
isPlaceholder__anon130919f30111::CastOperation114f4a2713aSLionel Sambuc bool isPlaceholder(BuiltinType::Kind K) const {
115f4a2713aSLionel Sambuc return PlaceholderKind == K;
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc
checkCastAlign__anon130919f30111::CastOperation118f4a2713aSLionel Sambuc void checkCastAlign() {
119f4a2713aSLionel Sambuc Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange);
120f4a2713aSLionel Sambuc }
121f4a2713aSLionel Sambuc
checkObjCARCConversion__anon130919f30111::CastOperation122f4a2713aSLionel Sambuc void checkObjCARCConversion(Sema::CheckedConversionKind CCK) {
123f4a2713aSLionel Sambuc assert(Self.getLangOpts().ObjCAutoRefCount);
124f4a2713aSLionel Sambuc
125f4a2713aSLionel Sambuc Expr *src = SrcExpr.get();
126f4a2713aSLionel Sambuc if (Self.CheckObjCARCConversion(OpRange, DestType, src, CCK) ==
127f4a2713aSLionel Sambuc Sema::ACR_unbridged)
128f4a2713aSLionel Sambuc IsARCUnbridgedCast = true;
129f4a2713aSLionel Sambuc SrcExpr = src;
130f4a2713aSLionel Sambuc }
131f4a2713aSLionel Sambuc
132f4a2713aSLionel Sambuc /// Check for and handle non-overload placeholder expressions.
checkNonOverloadPlaceholders__anon130919f30111::CastOperation133f4a2713aSLionel Sambuc void checkNonOverloadPlaceholders() {
134f4a2713aSLionel Sambuc if (!isPlaceholder() || isPlaceholder(BuiltinType::Overload))
135f4a2713aSLionel Sambuc return;
136f4a2713aSLionel Sambuc
137*0a6a1f1dSLionel Sambuc SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
138f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
139f4a2713aSLionel Sambuc return;
140f4a2713aSLionel Sambuc PlaceholderKind = (BuiltinType::Kind) 0;
141f4a2713aSLionel Sambuc }
142f4a2713aSLionel Sambuc };
143f4a2713aSLionel Sambuc }
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc // The Try functions attempt a specific way of casting. If they succeed, they
146f4a2713aSLionel Sambuc // return TC_Success. If their way of casting is not appropriate for the given
147f4a2713aSLionel Sambuc // arguments, they return TC_NotApplicable and *may* set diag to a diagnostic
148f4a2713aSLionel Sambuc // to emit if no other way succeeds. If their way of casting is appropriate but
149f4a2713aSLionel Sambuc // fails, they return TC_Failed and *must* set diag; they can set it to 0 if
150f4a2713aSLionel Sambuc // they emit a specialized diagnostic.
151f4a2713aSLionel Sambuc // All diagnostics returned by these functions must expect the same three
152f4a2713aSLionel Sambuc // arguments:
153f4a2713aSLionel Sambuc // %0: Cast Type (a value from the CastType enumeration)
154f4a2713aSLionel Sambuc // %1: Source Type
155f4a2713aSLionel Sambuc // %2: Destination Type
156f4a2713aSLionel Sambuc static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr,
157f4a2713aSLionel Sambuc QualType DestType, bool CStyle,
158f4a2713aSLionel Sambuc CastKind &Kind,
159f4a2713aSLionel Sambuc CXXCastPath &BasePath,
160f4a2713aSLionel Sambuc unsigned &msg);
161f4a2713aSLionel Sambuc static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr,
162f4a2713aSLionel Sambuc QualType DestType, bool CStyle,
163f4a2713aSLionel Sambuc const SourceRange &OpRange,
164f4a2713aSLionel Sambuc unsigned &msg,
165f4a2713aSLionel Sambuc CastKind &Kind,
166f4a2713aSLionel Sambuc CXXCastPath &BasePath);
167f4a2713aSLionel Sambuc static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType,
168f4a2713aSLionel Sambuc QualType DestType, bool CStyle,
169f4a2713aSLionel Sambuc const SourceRange &OpRange,
170f4a2713aSLionel Sambuc unsigned &msg,
171f4a2713aSLionel Sambuc CastKind &Kind,
172f4a2713aSLionel Sambuc CXXCastPath &BasePath);
173f4a2713aSLionel Sambuc static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType,
174f4a2713aSLionel Sambuc CanQualType DestType, bool CStyle,
175f4a2713aSLionel Sambuc const SourceRange &OpRange,
176f4a2713aSLionel Sambuc QualType OrigSrcType,
177f4a2713aSLionel Sambuc QualType OrigDestType, unsigned &msg,
178f4a2713aSLionel Sambuc CastKind &Kind,
179f4a2713aSLionel Sambuc CXXCastPath &BasePath);
180f4a2713aSLionel Sambuc static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr,
181f4a2713aSLionel Sambuc QualType SrcType,
182f4a2713aSLionel Sambuc QualType DestType,bool CStyle,
183f4a2713aSLionel Sambuc const SourceRange &OpRange,
184f4a2713aSLionel Sambuc unsigned &msg,
185f4a2713aSLionel Sambuc CastKind &Kind,
186f4a2713aSLionel Sambuc CXXCastPath &BasePath);
187f4a2713aSLionel Sambuc
188f4a2713aSLionel Sambuc static TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr,
189f4a2713aSLionel Sambuc QualType DestType,
190f4a2713aSLionel Sambuc Sema::CheckedConversionKind CCK,
191f4a2713aSLionel Sambuc const SourceRange &OpRange,
192f4a2713aSLionel Sambuc unsigned &msg, CastKind &Kind,
193f4a2713aSLionel Sambuc bool ListInitialization);
194f4a2713aSLionel Sambuc static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr,
195f4a2713aSLionel Sambuc QualType DestType,
196f4a2713aSLionel Sambuc Sema::CheckedConversionKind CCK,
197f4a2713aSLionel Sambuc const SourceRange &OpRange,
198f4a2713aSLionel Sambuc unsigned &msg, CastKind &Kind,
199f4a2713aSLionel Sambuc CXXCastPath &BasePath,
200f4a2713aSLionel Sambuc bool ListInitialization);
201f4a2713aSLionel Sambuc static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr,
202f4a2713aSLionel Sambuc QualType DestType, bool CStyle,
203f4a2713aSLionel Sambuc unsigned &msg);
204f4a2713aSLionel Sambuc static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr,
205f4a2713aSLionel Sambuc QualType DestType, bool CStyle,
206f4a2713aSLionel Sambuc const SourceRange &OpRange,
207f4a2713aSLionel Sambuc unsigned &msg,
208f4a2713aSLionel Sambuc CastKind &Kind);
209f4a2713aSLionel Sambuc
210f4a2713aSLionel Sambuc
211f4a2713aSLionel Sambuc /// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
212f4a2713aSLionel Sambuc ExprResult
ActOnCXXNamedCast(SourceLocation OpLoc,tok::TokenKind Kind,SourceLocation LAngleBracketLoc,Declarator & D,SourceLocation RAngleBracketLoc,SourceLocation LParenLoc,Expr * E,SourceLocation RParenLoc)213f4a2713aSLionel Sambuc Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
214f4a2713aSLionel Sambuc SourceLocation LAngleBracketLoc, Declarator &D,
215f4a2713aSLionel Sambuc SourceLocation RAngleBracketLoc,
216f4a2713aSLionel Sambuc SourceLocation LParenLoc, Expr *E,
217f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
218f4a2713aSLionel Sambuc
219f4a2713aSLionel Sambuc assert(!D.isInvalidType());
220f4a2713aSLionel Sambuc
221f4a2713aSLionel Sambuc TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, E->getType());
222f4a2713aSLionel Sambuc if (D.isInvalidType())
223f4a2713aSLionel Sambuc return ExprError();
224f4a2713aSLionel Sambuc
225f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus) {
226f4a2713aSLionel Sambuc // Check that there are no default arguments (C++ only).
227f4a2713aSLionel Sambuc CheckExtraCXXDefaultArguments(D);
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc
230f4a2713aSLionel Sambuc return BuildCXXNamedCast(OpLoc, Kind, TInfo, E,
231f4a2713aSLionel Sambuc SourceRange(LAngleBracketLoc, RAngleBracketLoc),
232f4a2713aSLionel Sambuc SourceRange(LParenLoc, RParenLoc));
233f4a2713aSLionel Sambuc }
234f4a2713aSLionel Sambuc
235f4a2713aSLionel Sambuc ExprResult
BuildCXXNamedCast(SourceLocation OpLoc,tok::TokenKind Kind,TypeSourceInfo * DestTInfo,Expr * E,SourceRange AngleBrackets,SourceRange Parens)236f4a2713aSLionel Sambuc Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
237f4a2713aSLionel Sambuc TypeSourceInfo *DestTInfo, Expr *E,
238f4a2713aSLionel Sambuc SourceRange AngleBrackets, SourceRange Parens) {
239*0a6a1f1dSLionel Sambuc ExprResult Ex = E;
240f4a2713aSLionel Sambuc QualType DestType = DestTInfo->getType();
241f4a2713aSLionel Sambuc
242f4a2713aSLionel Sambuc // If the type is dependent, we won't do the semantic analysis now.
243*0a6a1f1dSLionel Sambuc bool TypeDependent =
244*0a6a1f1dSLionel Sambuc DestType->isDependentType() || Ex.get()->isTypeDependent();
245f4a2713aSLionel Sambuc
246f4a2713aSLionel Sambuc CastOperation Op(*this, DestType, E);
247f4a2713aSLionel Sambuc Op.OpRange = SourceRange(OpLoc, Parens.getEnd());
248f4a2713aSLionel Sambuc Op.DestRange = AngleBrackets;
249f4a2713aSLionel Sambuc
250f4a2713aSLionel Sambuc switch (Kind) {
251f4a2713aSLionel Sambuc default: llvm_unreachable("Unknown C++ cast!");
252f4a2713aSLionel Sambuc
253f4a2713aSLionel Sambuc case tok::kw_const_cast:
254f4a2713aSLionel Sambuc if (!TypeDependent) {
255f4a2713aSLionel Sambuc Op.CheckConstCast();
256f4a2713aSLionel Sambuc if (Op.SrcExpr.isInvalid())
257f4a2713aSLionel Sambuc return ExprError();
258f4a2713aSLionel Sambuc }
259f4a2713aSLionel Sambuc return Op.complete(CXXConstCastExpr::Create(Context, Op.ResultType,
260*0a6a1f1dSLionel Sambuc Op.ValueKind, Op.SrcExpr.get(), DestTInfo,
261f4a2713aSLionel Sambuc OpLoc, Parens.getEnd(),
262f4a2713aSLionel Sambuc AngleBrackets));
263f4a2713aSLionel Sambuc
264f4a2713aSLionel Sambuc case tok::kw_dynamic_cast: {
265f4a2713aSLionel Sambuc if (!TypeDependent) {
266f4a2713aSLionel Sambuc Op.CheckDynamicCast();
267f4a2713aSLionel Sambuc if (Op.SrcExpr.isInvalid())
268f4a2713aSLionel Sambuc return ExprError();
269f4a2713aSLionel Sambuc }
270f4a2713aSLionel Sambuc return Op.complete(CXXDynamicCastExpr::Create(Context, Op.ResultType,
271*0a6a1f1dSLionel Sambuc Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
272f4a2713aSLionel Sambuc &Op.BasePath, DestTInfo,
273f4a2713aSLionel Sambuc OpLoc, Parens.getEnd(),
274f4a2713aSLionel Sambuc AngleBrackets));
275f4a2713aSLionel Sambuc }
276f4a2713aSLionel Sambuc case tok::kw_reinterpret_cast: {
277f4a2713aSLionel Sambuc if (!TypeDependent) {
278f4a2713aSLionel Sambuc Op.CheckReinterpretCast();
279f4a2713aSLionel Sambuc if (Op.SrcExpr.isInvalid())
280f4a2713aSLionel Sambuc return ExprError();
281f4a2713aSLionel Sambuc }
282f4a2713aSLionel Sambuc return Op.complete(CXXReinterpretCastExpr::Create(Context, Op.ResultType,
283*0a6a1f1dSLionel Sambuc Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
284*0a6a1f1dSLionel Sambuc nullptr, DestTInfo, OpLoc,
285f4a2713aSLionel Sambuc Parens.getEnd(),
286f4a2713aSLionel Sambuc AngleBrackets));
287f4a2713aSLionel Sambuc }
288f4a2713aSLionel Sambuc case tok::kw_static_cast: {
289f4a2713aSLionel Sambuc if (!TypeDependent) {
290f4a2713aSLionel Sambuc Op.CheckStaticCast();
291f4a2713aSLionel Sambuc if (Op.SrcExpr.isInvalid())
292f4a2713aSLionel Sambuc return ExprError();
293f4a2713aSLionel Sambuc }
294f4a2713aSLionel Sambuc
295f4a2713aSLionel Sambuc return Op.complete(CXXStaticCastExpr::Create(Context, Op.ResultType,
296*0a6a1f1dSLionel Sambuc Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
297f4a2713aSLionel Sambuc &Op.BasePath, DestTInfo,
298f4a2713aSLionel Sambuc OpLoc, Parens.getEnd(),
299f4a2713aSLionel Sambuc AngleBrackets));
300f4a2713aSLionel Sambuc }
301f4a2713aSLionel Sambuc }
302f4a2713aSLionel Sambuc }
303f4a2713aSLionel Sambuc
304f4a2713aSLionel Sambuc /// Try to diagnose a failed overloaded cast. Returns true if
305f4a2713aSLionel Sambuc /// diagnostics were emitted.
tryDiagnoseOverloadedCast(Sema & S,CastType CT,SourceRange range,Expr * src,QualType destType,bool listInitialization)306f4a2713aSLionel Sambuc static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT,
307f4a2713aSLionel Sambuc SourceRange range, Expr *src,
308f4a2713aSLionel Sambuc QualType destType,
309f4a2713aSLionel Sambuc bool listInitialization) {
310f4a2713aSLionel Sambuc switch (CT) {
311f4a2713aSLionel Sambuc // These cast kinds don't consider user-defined conversions.
312f4a2713aSLionel Sambuc case CT_Const:
313f4a2713aSLionel Sambuc case CT_Reinterpret:
314f4a2713aSLionel Sambuc case CT_Dynamic:
315f4a2713aSLionel Sambuc return false;
316f4a2713aSLionel Sambuc
317f4a2713aSLionel Sambuc // These do.
318f4a2713aSLionel Sambuc case CT_Static:
319f4a2713aSLionel Sambuc case CT_CStyle:
320f4a2713aSLionel Sambuc case CT_Functional:
321f4a2713aSLionel Sambuc break;
322f4a2713aSLionel Sambuc }
323f4a2713aSLionel Sambuc
324f4a2713aSLionel Sambuc QualType srcType = src->getType();
325f4a2713aSLionel Sambuc if (!destType->isRecordType() && !srcType->isRecordType())
326f4a2713aSLionel Sambuc return false;
327f4a2713aSLionel Sambuc
328f4a2713aSLionel Sambuc InitializedEntity entity = InitializedEntity::InitializeTemporary(destType);
329f4a2713aSLionel Sambuc InitializationKind initKind
330f4a2713aSLionel Sambuc = (CT == CT_CStyle)? InitializationKind::CreateCStyleCast(range.getBegin(),
331f4a2713aSLionel Sambuc range, listInitialization)
332f4a2713aSLionel Sambuc : (CT == CT_Functional)? InitializationKind::CreateFunctionalCast(range,
333f4a2713aSLionel Sambuc listInitialization)
334f4a2713aSLionel Sambuc : InitializationKind::CreateCast(/*type range?*/ range);
335f4a2713aSLionel Sambuc InitializationSequence sequence(S, entity, initKind, src);
336f4a2713aSLionel Sambuc
337f4a2713aSLionel Sambuc assert(sequence.Failed() && "initialization succeeded on second try?");
338f4a2713aSLionel Sambuc switch (sequence.getFailureKind()) {
339f4a2713aSLionel Sambuc default: return false;
340f4a2713aSLionel Sambuc
341f4a2713aSLionel Sambuc case InitializationSequence::FK_ConstructorOverloadFailed:
342f4a2713aSLionel Sambuc case InitializationSequence::FK_UserConversionOverloadFailed:
343f4a2713aSLionel Sambuc break;
344f4a2713aSLionel Sambuc }
345f4a2713aSLionel Sambuc
346f4a2713aSLionel Sambuc OverloadCandidateSet &candidates = sequence.getFailedCandidateSet();
347f4a2713aSLionel Sambuc
348f4a2713aSLionel Sambuc unsigned msg = 0;
349f4a2713aSLionel Sambuc OverloadCandidateDisplayKind howManyCandidates = OCD_AllCandidates;
350f4a2713aSLionel Sambuc
351f4a2713aSLionel Sambuc switch (sequence.getFailedOverloadResult()) {
352f4a2713aSLionel Sambuc case OR_Success: llvm_unreachable("successful failed overload");
353f4a2713aSLionel Sambuc case OR_No_Viable_Function:
354f4a2713aSLionel Sambuc if (candidates.empty())
355f4a2713aSLionel Sambuc msg = diag::err_ovl_no_conversion_in_cast;
356f4a2713aSLionel Sambuc else
357f4a2713aSLionel Sambuc msg = diag::err_ovl_no_viable_conversion_in_cast;
358f4a2713aSLionel Sambuc howManyCandidates = OCD_AllCandidates;
359f4a2713aSLionel Sambuc break;
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc case OR_Ambiguous:
362f4a2713aSLionel Sambuc msg = diag::err_ovl_ambiguous_conversion_in_cast;
363f4a2713aSLionel Sambuc howManyCandidates = OCD_ViableCandidates;
364f4a2713aSLionel Sambuc break;
365f4a2713aSLionel Sambuc
366f4a2713aSLionel Sambuc case OR_Deleted:
367f4a2713aSLionel Sambuc msg = diag::err_ovl_deleted_conversion_in_cast;
368f4a2713aSLionel Sambuc howManyCandidates = OCD_ViableCandidates;
369f4a2713aSLionel Sambuc break;
370f4a2713aSLionel Sambuc }
371f4a2713aSLionel Sambuc
372f4a2713aSLionel Sambuc S.Diag(range.getBegin(), msg)
373f4a2713aSLionel Sambuc << CT << srcType << destType
374f4a2713aSLionel Sambuc << range << src->getSourceRange();
375f4a2713aSLionel Sambuc
376f4a2713aSLionel Sambuc candidates.NoteCandidates(S, howManyCandidates, src);
377f4a2713aSLionel Sambuc
378f4a2713aSLionel Sambuc return true;
379f4a2713aSLionel Sambuc }
380f4a2713aSLionel Sambuc
381f4a2713aSLionel Sambuc /// Diagnose a failed cast.
diagnoseBadCast(Sema & S,unsigned msg,CastType castType,SourceRange opRange,Expr * src,QualType destType,bool listInitialization)382f4a2713aSLionel Sambuc static void diagnoseBadCast(Sema &S, unsigned msg, CastType castType,
383f4a2713aSLionel Sambuc SourceRange opRange, Expr *src, QualType destType,
384f4a2713aSLionel Sambuc bool listInitialization) {
385f4a2713aSLionel Sambuc if (msg == diag::err_bad_cxx_cast_generic &&
386f4a2713aSLionel Sambuc tryDiagnoseOverloadedCast(S, castType, opRange, src, destType,
387f4a2713aSLionel Sambuc listInitialization))
388f4a2713aSLionel Sambuc return;
389f4a2713aSLionel Sambuc
390f4a2713aSLionel Sambuc S.Diag(opRange.getBegin(), msg) << castType
391f4a2713aSLionel Sambuc << src->getType() << destType << opRange << src->getSourceRange();
392f4a2713aSLionel Sambuc }
393f4a2713aSLionel Sambuc
394f4a2713aSLionel Sambuc /// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes,
395f4a2713aSLionel Sambuc /// this removes one level of indirection from both types, provided that they're
396f4a2713aSLionel Sambuc /// the same kind of pointer (plain or to-member). Unlike the Sema function,
397f4a2713aSLionel Sambuc /// this one doesn't care if the two pointers-to-member don't point into the
398f4a2713aSLionel Sambuc /// same class. This is because CastsAwayConstness doesn't care.
UnwrapDissimilarPointerTypes(QualType & T1,QualType & T2)399f4a2713aSLionel Sambuc static bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) {
400f4a2713aSLionel Sambuc const PointerType *T1PtrType = T1->getAs<PointerType>(),
401f4a2713aSLionel Sambuc *T2PtrType = T2->getAs<PointerType>();
402f4a2713aSLionel Sambuc if (T1PtrType && T2PtrType) {
403f4a2713aSLionel Sambuc T1 = T1PtrType->getPointeeType();
404f4a2713aSLionel Sambuc T2 = T2PtrType->getPointeeType();
405f4a2713aSLionel Sambuc return true;
406f4a2713aSLionel Sambuc }
407f4a2713aSLionel Sambuc const ObjCObjectPointerType *T1ObjCPtrType =
408f4a2713aSLionel Sambuc T1->getAs<ObjCObjectPointerType>(),
409f4a2713aSLionel Sambuc *T2ObjCPtrType =
410f4a2713aSLionel Sambuc T2->getAs<ObjCObjectPointerType>();
411f4a2713aSLionel Sambuc if (T1ObjCPtrType) {
412f4a2713aSLionel Sambuc if (T2ObjCPtrType) {
413f4a2713aSLionel Sambuc T1 = T1ObjCPtrType->getPointeeType();
414f4a2713aSLionel Sambuc T2 = T2ObjCPtrType->getPointeeType();
415f4a2713aSLionel Sambuc return true;
416f4a2713aSLionel Sambuc }
417f4a2713aSLionel Sambuc else if (T2PtrType) {
418f4a2713aSLionel Sambuc T1 = T1ObjCPtrType->getPointeeType();
419f4a2713aSLionel Sambuc T2 = T2PtrType->getPointeeType();
420f4a2713aSLionel Sambuc return true;
421f4a2713aSLionel Sambuc }
422f4a2713aSLionel Sambuc }
423f4a2713aSLionel Sambuc else if (T2ObjCPtrType) {
424f4a2713aSLionel Sambuc if (T1PtrType) {
425f4a2713aSLionel Sambuc T2 = T2ObjCPtrType->getPointeeType();
426f4a2713aSLionel Sambuc T1 = T1PtrType->getPointeeType();
427f4a2713aSLionel Sambuc return true;
428f4a2713aSLionel Sambuc }
429f4a2713aSLionel Sambuc }
430f4a2713aSLionel Sambuc
431f4a2713aSLionel Sambuc const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
432f4a2713aSLionel Sambuc *T2MPType = T2->getAs<MemberPointerType>();
433f4a2713aSLionel Sambuc if (T1MPType && T2MPType) {
434f4a2713aSLionel Sambuc T1 = T1MPType->getPointeeType();
435f4a2713aSLionel Sambuc T2 = T2MPType->getPointeeType();
436f4a2713aSLionel Sambuc return true;
437f4a2713aSLionel Sambuc }
438f4a2713aSLionel Sambuc
439f4a2713aSLionel Sambuc const BlockPointerType *T1BPType = T1->getAs<BlockPointerType>(),
440f4a2713aSLionel Sambuc *T2BPType = T2->getAs<BlockPointerType>();
441f4a2713aSLionel Sambuc if (T1BPType && T2BPType) {
442f4a2713aSLionel Sambuc T1 = T1BPType->getPointeeType();
443f4a2713aSLionel Sambuc T2 = T2BPType->getPointeeType();
444f4a2713aSLionel Sambuc return true;
445f4a2713aSLionel Sambuc }
446f4a2713aSLionel Sambuc
447f4a2713aSLionel Sambuc return false;
448f4a2713aSLionel Sambuc }
449f4a2713aSLionel Sambuc
450f4a2713aSLionel Sambuc /// CastsAwayConstness - Check if the pointer conversion from SrcType to
451f4a2713aSLionel Sambuc /// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by
452f4a2713aSLionel Sambuc /// the cast checkers. Both arguments must denote pointer (possibly to member)
453f4a2713aSLionel Sambuc /// types.
454f4a2713aSLionel Sambuc ///
455f4a2713aSLionel Sambuc /// \param CheckCVR Whether to check for const/volatile/restrict qualifiers.
456f4a2713aSLionel Sambuc ///
457f4a2713aSLionel Sambuc /// \param CheckObjCLifetime Whether to check Objective-C lifetime qualifiers.
458f4a2713aSLionel Sambuc static bool
CastsAwayConstness(Sema & Self,QualType SrcType,QualType DestType,bool CheckCVR,bool CheckObjCLifetime,QualType * TheOffendingSrcType=nullptr,QualType * TheOffendingDestType=nullptr,Qualifiers * CastAwayQualifiers=nullptr)459f4a2713aSLionel Sambuc CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType,
460*0a6a1f1dSLionel Sambuc bool CheckCVR, bool CheckObjCLifetime,
461*0a6a1f1dSLionel Sambuc QualType *TheOffendingSrcType = nullptr,
462*0a6a1f1dSLionel Sambuc QualType *TheOffendingDestType = nullptr,
463*0a6a1f1dSLionel Sambuc Qualifiers *CastAwayQualifiers = nullptr) {
464f4a2713aSLionel Sambuc // If the only checking we care about is for Objective-C lifetime qualifiers,
465f4a2713aSLionel Sambuc // and we're not in ARC mode, there's nothing to check.
466f4a2713aSLionel Sambuc if (!CheckCVR && CheckObjCLifetime &&
467f4a2713aSLionel Sambuc !Self.Context.getLangOpts().ObjCAutoRefCount)
468f4a2713aSLionel Sambuc return false;
469f4a2713aSLionel Sambuc
470f4a2713aSLionel Sambuc // Casting away constness is defined in C++ 5.2.11p8 with reference to
471f4a2713aSLionel Sambuc // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since
472f4a2713aSLionel Sambuc // the rules are non-trivial. So first we construct Tcv *...cv* as described
473f4a2713aSLionel Sambuc // in C++ 5.2.11p8.
474f4a2713aSLionel Sambuc assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() ||
475f4a2713aSLionel Sambuc SrcType->isBlockPointerType()) &&
476f4a2713aSLionel Sambuc "Source type is not pointer or pointer to member.");
477f4a2713aSLionel Sambuc assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() ||
478f4a2713aSLionel Sambuc DestType->isBlockPointerType()) &&
479f4a2713aSLionel Sambuc "Destination type is not pointer or pointer to member.");
480f4a2713aSLionel Sambuc
481f4a2713aSLionel Sambuc QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType),
482f4a2713aSLionel Sambuc UnwrappedDestType = Self.Context.getCanonicalType(DestType);
483f4a2713aSLionel Sambuc SmallVector<Qualifiers, 8> cv1, cv2;
484f4a2713aSLionel Sambuc
485f4a2713aSLionel Sambuc // Find the qualifiers. We only care about cvr-qualifiers for the
486f4a2713aSLionel Sambuc // purpose of this check, because other qualifiers (address spaces,
487f4a2713aSLionel Sambuc // Objective-C GC, etc.) are part of the type's identity.
488*0a6a1f1dSLionel Sambuc QualType PrevUnwrappedSrcType = UnwrappedSrcType;
489*0a6a1f1dSLionel Sambuc QualType PrevUnwrappedDestType = UnwrappedDestType;
490f4a2713aSLionel Sambuc while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
491f4a2713aSLionel Sambuc // Determine the relevant qualifiers at this level.
492f4a2713aSLionel Sambuc Qualifiers SrcQuals, DestQuals;
493f4a2713aSLionel Sambuc Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals);
494f4a2713aSLionel Sambuc Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals);
495f4a2713aSLionel Sambuc
496f4a2713aSLionel Sambuc Qualifiers RetainedSrcQuals, RetainedDestQuals;
497f4a2713aSLionel Sambuc if (CheckCVR) {
498f4a2713aSLionel Sambuc RetainedSrcQuals.setCVRQualifiers(SrcQuals.getCVRQualifiers());
499f4a2713aSLionel Sambuc RetainedDestQuals.setCVRQualifiers(DestQuals.getCVRQualifiers());
500*0a6a1f1dSLionel Sambuc
501*0a6a1f1dSLionel Sambuc if (RetainedSrcQuals != RetainedDestQuals && TheOffendingSrcType &&
502*0a6a1f1dSLionel Sambuc TheOffendingDestType && CastAwayQualifiers) {
503*0a6a1f1dSLionel Sambuc *TheOffendingSrcType = PrevUnwrappedSrcType;
504*0a6a1f1dSLionel Sambuc *TheOffendingDestType = PrevUnwrappedDestType;
505*0a6a1f1dSLionel Sambuc *CastAwayQualifiers = RetainedSrcQuals - RetainedDestQuals;
506*0a6a1f1dSLionel Sambuc }
507f4a2713aSLionel Sambuc }
508f4a2713aSLionel Sambuc
509f4a2713aSLionel Sambuc if (CheckObjCLifetime &&
510f4a2713aSLionel Sambuc !DestQuals.compatiblyIncludesObjCLifetime(SrcQuals))
511f4a2713aSLionel Sambuc return true;
512f4a2713aSLionel Sambuc
513f4a2713aSLionel Sambuc cv1.push_back(RetainedSrcQuals);
514f4a2713aSLionel Sambuc cv2.push_back(RetainedDestQuals);
515*0a6a1f1dSLionel Sambuc
516*0a6a1f1dSLionel Sambuc PrevUnwrappedSrcType = UnwrappedSrcType;
517*0a6a1f1dSLionel Sambuc PrevUnwrappedDestType = UnwrappedDestType;
518f4a2713aSLionel Sambuc }
519f4a2713aSLionel Sambuc if (cv1.empty())
520f4a2713aSLionel Sambuc return false;
521f4a2713aSLionel Sambuc
522f4a2713aSLionel Sambuc // Construct void pointers with those qualifiers (in reverse order of
523f4a2713aSLionel Sambuc // unwrapping, of course).
524f4a2713aSLionel Sambuc QualType SrcConstruct = Self.Context.VoidTy;
525f4a2713aSLionel Sambuc QualType DestConstruct = Self.Context.VoidTy;
526f4a2713aSLionel Sambuc ASTContext &Context = Self.Context;
527f4a2713aSLionel Sambuc for (SmallVectorImpl<Qualifiers>::reverse_iterator i1 = cv1.rbegin(),
528f4a2713aSLionel Sambuc i2 = cv2.rbegin();
529f4a2713aSLionel Sambuc i1 != cv1.rend(); ++i1, ++i2) {
530f4a2713aSLionel Sambuc SrcConstruct
531f4a2713aSLionel Sambuc = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1));
532f4a2713aSLionel Sambuc DestConstruct
533f4a2713aSLionel Sambuc = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2));
534f4a2713aSLionel Sambuc }
535f4a2713aSLionel Sambuc
536f4a2713aSLionel Sambuc // Test if they're compatible.
537f4a2713aSLionel Sambuc bool ObjCLifetimeConversion;
538f4a2713aSLionel Sambuc return SrcConstruct != DestConstruct &&
539f4a2713aSLionel Sambuc !Self.IsQualificationConversion(SrcConstruct, DestConstruct, false,
540f4a2713aSLionel Sambuc ObjCLifetimeConversion);
541f4a2713aSLionel Sambuc }
542f4a2713aSLionel Sambuc
543f4a2713aSLionel Sambuc /// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid.
544f4a2713aSLionel Sambuc /// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime-
545f4a2713aSLionel Sambuc /// checked downcasts in class hierarchies.
CheckDynamicCast()546f4a2713aSLionel Sambuc void CastOperation::CheckDynamicCast() {
547f4a2713aSLionel Sambuc if (ValueKind == VK_RValue)
548*0a6a1f1dSLionel Sambuc SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
549f4a2713aSLionel Sambuc else if (isPlaceholder())
550*0a6a1f1dSLionel Sambuc SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
551f4a2713aSLionel Sambuc if (SrcExpr.isInvalid()) // if conversion failed, don't report another error
552f4a2713aSLionel Sambuc return;
553f4a2713aSLionel Sambuc
554f4a2713aSLionel Sambuc QualType OrigSrcType = SrcExpr.get()->getType();
555f4a2713aSLionel Sambuc QualType DestType = Self.Context.getCanonicalType(this->DestType);
556f4a2713aSLionel Sambuc
557f4a2713aSLionel Sambuc // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type,
558f4a2713aSLionel Sambuc // or "pointer to cv void".
559f4a2713aSLionel Sambuc
560f4a2713aSLionel Sambuc QualType DestPointee;
561f4a2713aSLionel Sambuc const PointerType *DestPointer = DestType->getAs<PointerType>();
562*0a6a1f1dSLionel Sambuc const ReferenceType *DestReference = nullptr;
563f4a2713aSLionel Sambuc if (DestPointer) {
564f4a2713aSLionel Sambuc DestPointee = DestPointer->getPointeeType();
565f4a2713aSLionel Sambuc } else if ((DestReference = DestType->getAs<ReferenceType>())) {
566f4a2713aSLionel Sambuc DestPointee = DestReference->getPointeeType();
567f4a2713aSLionel Sambuc } else {
568f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
569f4a2713aSLionel Sambuc << this->DestType << DestRange;
570f4a2713aSLionel Sambuc SrcExpr = ExprError();
571f4a2713aSLionel Sambuc return;
572f4a2713aSLionel Sambuc }
573f4a2713aSLionel Sambuc
574f4a2713aSLionel Sambuc const RecordType *DestRecord = DestPointee->getAs<RecordType>();
575f4a2713aSLionel Sambuc if (DestPointee->isVoidType()) {
576f4a2713aSLionel Sambuc assert(DestPointer && "Reference to void is not possible");
577f4a2713aSLionel Sambuc } else if (DestRecord) {
578f4a2713aSLionel Sambuc if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
579f4a2713aSLionel Sambuc diag::err_bad_dynamic_cast_incomplete,
580f4a2713aSLionel Sambuc DestRange)) {
581f4a2713aSLionel Sambuc SrcExpr = ExprError();
582f4a2713aSLionel Sambuc return;
583f4a2713aSLionel Sambuc }
584f4a2713aSLionel Sambuc } else {
585f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
586f4a2713aSLionel Sambuc << DestPointee.getUnqualifiedType() << DestRange;
587f4a2713aSLionel Sambuc SrcExpr = ExprError();
588f4a2713aSLionel Sambuc return;
589f4a2713aSLionel Sambuc }
590f4a2713aSLionel Sambuc
591f4a2713aSLionel Sambuc // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
592f4a2713aSLionel Sambuc // complete class type, [...]. If T is an lvalue reference type, v shall be
593f4a2713aSLionel Sambuc // an lvalue of a complete class type, [...]. If T is an rvalue reference
594f4a2713aSLionel Sambuc // type, v shall be an expression having a complete class type, [...]
595f4a2713aSLionel Sambuc QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
596f4a2713aSLionel Sambuc QualType SrcPointee;
597f4a2713aSLionel Sambuc if (DestPointer) {
598f4a2713aSLionel Sambuc if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
599f4a2713aSLionel Sambuc SrcPointee = SrcPointer->getPointeeType();
600f4a2713aSLionel Sambuc } else {
601f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
602f4a2713aSLionel Sambuc << OrigSrcType << SrcExpr.get()->getSourceRange();
603f4a2713aSLionel Sambuc SrcExpr = ExprError();
604f4a2713aSLionel Sambuc return;
605f4a2713aSLionel Sambuc }
606f4a2713aSLionel Sambuc } else if (DestReference->isLValueReferenceType()) {
607f4a2713aSLionel Sambuc if (!SrcExpr.get()->isLValue()) {
608f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
609f4a2713aSLionel Sambuc << CT_Dynamic << OrigSrcType << this->DestType << OpRange;
610f4a2713aSLionel Sambuc }
611f4a2713aSLionel Sambuc SrcPointee = SrcType;
612f4a2713aSLionel Sambuc } else {
613*0a6a1f1dSLionel Sambuc // If we're dynamic_casting from a prvalue to an rvalue reference, we need
614*0a6a1f1dSLionel Sambuc // to materialize the prvalue before we bind the reference to it.
615*0a6a1f1dSLionel Sambuc if (SrcExpr.get()->isRValue())
616*0a6a1f1dSLionel Sambuc SrcExpr = new (Self.Context) MaterializeTemporaryExpr(
617*0a6a1f1dSLionel Sambuc SrcType, SrcExpr.get(), /*IsLValueReference*/false);
618f4a2713aSLionel Sambuc SrcPointee = SrcType;
619f4a2713aSLionel Sambuc }
620f4a2713aSLionel Sambuc
621f4a2713aSLionel Sambuc const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
622f4a2713aSLionel Sambuc if (SrcRecord) {
623f4a2713aSLionel Sambuc if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
624f4a2713aSLionel Sambuc diag::err_bad_dynamic_cast_incomplete,
625f4a2713aSLionel Sambuc SrcExpr.get())) {
626f4a2713aSLionel Sambuc SrcExpr = ExprError();
627f4a2713aSLionel Sambuc return;
628f4a2713aSLionel Sambuc }
629f4a2713aSLionel Sambuc } else {
630f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
631f4a2713aSLionel Sambuc << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange();
632f4a2713aSLionel Sambuc SrcExpr = ExprError();
633f4a2713aSLionel Sambuc return;
634f4a2713aSLionel Sambuc }
635f4a2713aSLionel Sambuc
636f4a2713aSLionel Sambuc assert((DestPointer || DestReference) &&
637f4a2713aSLionel Sambuc "Bad destination non-ptr/ref slipped through.");
638f4a2713aSLionel Sambuc assert((DestRecord || DestPointee->isVoidType()) &&
639f4a2713aSLionel Sambuc "Bad destination pointee slipped through.");
640f4a2713aSLionel Sambuc assert(SrcRecord && "Bad source pointee slipped through.");
641f4a2713aSLionel Sambuc
642f4a2713aSLionel Sambuc // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness.
643f4a2713aSLionel Sambuc if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
644f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away)
645f4a2713aSLionel Sambuc << CT_Dynamic << OrigSrcType << this->DestType << OpRange;
646f4a2713aSLionel Sambuc SrcExpr = ExprError();
647f4a2713aSLionel Sambuc return;
648f4a2713aSLionel Sambuc }
649f4a2713aSLionel Sambuc
650f4a2713aSLionel Sambuc // C++ 5.2.7p3: If the type of v is the same as the required result type,
651f4a2713aSLionel Sambuc // [except for cv].
652f4a2713aSLionel Sambuc if (DestRecord == SrcRecord) {
653f4a2713aSLionel Sambuc Kind = CK_NoOp;
654f4a2713aSLionel Sambuc return;
655f4a2713aSLionel Sambuc }
656f4a2713aSLionel Sambuc
657f4a2713aSLionel Sambuc // C++ 5.2.7p5
658f4a2713aSLionel Sambuc // Upcasts are resolved statically.
659f4a2713aSLionel Sambuc if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
660f4a2713aSLionel Sambuc if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
661f4a2713aSLionel Sambuc OpRange.getBegin(), OpRange,
662f4a2713aSLionel Sambuc &BasePath)) {
663f4a2713aSLionel Sambuc SrcExpr = ExprError();
664f4a2713aSLionel Sambuc return;
665f4a2713aSLionel Sambuc }
666f4a2713aSLionel Sambuc
667f4a2713aSLionel Sambuc Kind = CK_DerivedToBase;
668f4a2713aSLionel Sambuc
669f4a2713aSLionel Sambuc // If we are casting to or through a virtual base class, we need a
670f4a2713aSLionel Sambuc // vtable.
671f4a2713aSLionel Sambuc if (Self.BasePathInvolvesVirtualBase(BasePath))
672f4a2713aSLionel Sambuc Self.MarkVTableUsed(OpRange.getBegin(),
673f4a2713aSLionel Sambuc cast<CXXRecordDecl>(SrcRecord->getDecl()));
674f4a2713aSLionel Sambuc return;
675f4a2713aSLionel Sambuc }
676f4a2713aSLionel Sambuc
677f4a2713aSLionel Sambuc // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
678f4a2713aSLionel Sambuc const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition();
679f4a2713aSLionel Sambuc assert(SrcDecl && "Definition missing");
680f4a2713aSLionel Sambuc if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
681f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
682f4a2713aSLionel Sambuc << SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange();
683f4a2713aSLionel Sambuc SrcExpr = ExprError();
684f4a2713aSLionel Sambuc }
685f4a2713aSLionel Sambuc Self.MarkVTableUsed(OpRange.getBegin(),
686f4a2713aSLionel Sambuc cast<CXXRecordDecl>(SrcRecord->getDecl()));
687f4a2713aSLionel Sambuc
688f4a2713aSLionel Sambuc // dynamic_cast is not available with -fno-rtti.
689f4a2713aSLionel Sambuc // As an exception, dynamic_cast to void* is available because it doesn't
690f4a2713aSLionel Sambuc // use RTTI.
691f4a2713aSLionel Sambuc if (!Self.getLangOpts().RTTI && !DestPointee->isVoidType()) {
692f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_no_dynamic_cast_with_fno_rtti);
693f4a2713aSLionel Sambuc SrcExpr = ExprError();
694f4a2713aSLionel Sambuc return;
695f4a2713aSLionel Sambuc }
696f4a2713aSLionel Sambuc
697f4a2713aSLionel Sambuc // Done. Everything else is run-time checks.
698f4a2713aSLionel Sambuc Kind = CK_Dynamic;
699f4a2713aSLionel Sambuc }
700f4a2713aSLionel Sambuc
701f4a2713aSLionel Sambuc /// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
702f4a2713aSLionel Sambuc /// Refer to C++ 5.2.11 for details. const_cast is typically used in code
703f4a2713aSLionel Sambuc /// like this:
704f4a2713aSLionel Sambuc /// const char *str = "literal";
705f4a2713aSLionel Sambuc /// legacy_function(const_cast\<char*\>(str));
CheckConstCast()706f4a2713aSLionel Sambuc void CastOperation::CheckConstCast() {
707f4a2713aSLionel Sambuc if (ValueKind == VK_RValue)
708*0a6a1f1dSLionel Sambuc SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
709f4a2713aSLionel Sambuc else if (isPlaceholder())
710*0a6a1f1dSLionel Sambuc SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
711f4a2713aSLionel Sambuc if (SrcExpr.isInvalid()) // if conversion failed, don't report another error
712f4a2713aSLionel Sambuc return;
713f4a2713aSLionel Sambuc
714f4a2713aSLionel Sambuc unsigned msg = diag::err_bad_cxx_cast_generic;
715f4a2713aSLionel Sambuc if (TryConstCast(Self, SrcExpr, DestType, /*CStyle*/false, msg) != TC_Success
716f4a2713aSLionel Sambuc && msg != 0) {
717f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), msg) << CT_Const
718f4a2713aSLionel Sambuc << SrcExpr.get()->getType() << DestType << OpRange;
719f4a2713aSLionel Sambuc SrcExpr = ExprError();
720f4a2713aSLionel Sambuc }
721f4a2713aSLionel Sambuc }
722f4a2713aSLionel Sambuc
723f4a2713aSLionel Sambuc /// Check that a reinterpret_cast\<DestType\>(SrcExpr) is not used as upcast
724f4a2713aSLionel Sambuc /// or downcast between respective pointers or references.
DiagnoseReinterpretUpDownCast(Sema & Self,const Expr * SrcExpr,QualType DestType,SourceRange OpRange)725f4a2713aSLionel Sambuc static void DiagnoseReinterpretUpDownCast(Sema &Self, const Expr *SrcExpr,
726f4a2713aSLionel Sambuc QualType DestType,
727f4a2713aSLionel Sambuc SourceRange OpRange) {
728f4a2713aSLionel Sambuc QualType SrcType = SrcExpr->getType();
729f4a2713aSLionel Sambuc // When casting from pointer or reference, get pointee type; use original
730f4a2713aSLionel Sambuc // type otherwise.
731f4a2713aSLionel Sambuc const CXXRecordDecl *SrcPointeeRD = SrcType->getPointeeCXXRecordDecl();
732f4a2713aSLionel Sambuc const CXXRecordDecl *SrcRD =
733f4a2713aSLionel Sambuc SrcPointeeRD ? SrcPointeeRD : SrcType->getAsCXXRecordDecl();
734f4a2713aSLionel Sambuc
735f4a2713aSLionel Sambuc // Examining subobjects for records is only possible if the complete and
736f4a2713aSLionel Sambuc // valid definition is available. Also, template instantiation is not
737f4a2713aSLionel Sambuc // allowed here.
738f4a2713aSLionel Sambuc if (!SrcRD || !SrcRD->isCompleteDefinition() || SrcRD->isInvalidDecl())
739f4a2713aSLionel Sambuc return;
740f4a2713aSLionel Sambuc
741f4a2713aSLionel Sambuc const CXXRecordDecl *DestRD = DestType->getPointeeCXXRecordDecl();
742f4a2713aSLionel Sambuc
743f4a2713aSLionel Sambuc if (!DestRD || !DestRD->isCompleteDefinition() || DestRD->isInvalidDecl())
744f4a2713aSLionel Sambuc return;
745f4a2713aSLionel Sambuc
746f4a2713aSLionel Sambuc enum {
747f4a2713aSLionel Sambuc ReinterpretUpcast,
748f4a2713aSLionel Sambuc ReinterpretDowncast
749f4a2713aSLionel Sambuc } ReinterpretKind;
750f4a2713aSLionel Sambuc
751f4a2713aSLionel Sambuc CXXBasePaths BasePaths;
752f4a2713aSLionel Sambuc
753f4a2713aSLionel Sambuc if (SrcRD->isDerivedFrom(DestRD, BasePaths))
754f4a2713aSLionel Sambuc ReinterpretKind = ReinterpretUpcast;
755f4a2713aSLionel Sambuc else if (DestRD->isDerivedFrom(SrcRD, BasePaths))
756f4a2713aSLionel Sambuc ReinterpretKind = ReinterpretDowncast;
757f4a2713aSLionel Sambuc else
758f4a2713aSLionel Sambuc return;
759f4a2713aSLionel Sambuc
760f4a2713aSLionel Sambuc bool VirtualBase = true;
761f4a2713aSLionel Sambuc bool NonZeroOffset = false;
762f4a2713aSLionel Sambuc for (CXXBasePaths::const_paths_iterator I = BasePaths.begin(),
763f4a2713aSLionel Sambuc E = BasePaths.end();
764f4a2713aSLionel Sambuc I != E; ++I) {
765f4a2713aSLionel Sambuc const CXXBasePath &Path = *I;
766f4a2713aSLionel Sambuc CharUnits Offset = CharUnits::Zero();
767f4a2713aSLionel Sambuc bool IsVirtual = false;
768f4a2713aSLionel Sambuc for (CXXBasePath::const_iterator IElem = Path.begin(), EElem = Path.end();
769f4a2713aSLionel Sambuc IElem != EElem; ++IElem) {
770f4a2713aSLionel Sambuc IsVirtual = IElem->Base->isVirtual();
771f4a2713aSLionel Sambuc if (IsVirtual)
772f4a2713aSLionel Sambuc break;
773f4a2713aSLionel Sambuc const CXXRecordDecl *BaseRD = IElem->Base->getType()->getAsCXXRecordDecl();
774f4a2713aSLionel Sambuc assert(BaseRD && "Base type should be a valid unqualified class type");
775f4a2713aSLionel Sambuc // Don't check if any base has invalid declaration or has no definition
776f4a2713aSLionel Sambuc // since it has no layout info.
777f4a2713aSLionel Sambuc const CXXRecordDecl *Class = IElem->Class,
778f4a2713aSLionel Sambuc *ClassDefinition = Class->getDefinition();
779f4a2713aSLionel Sambuc if (Class->isInvalidDecl() || !ClassDefinition ||
780f4a2713aSLionel Sambuc !ClassDefinition->isCompleteDefinition())
781f4a2713aSLionel Sambuc return;
782f4a2713aSLionel Sambuc
783f4a2713aSLionel Sambuc const ASTRecordLayout &DerivedLayout =
784f4a2713aSLionel Sambuc Self.Context.getASTRecordLayout(Class);
785f4a2713aSLionel Sambuc Offset += DerivedLayout.getBaseClassOffset(BaseRD);
786f4a2713aSLionel Sambuc }
787f4a2713aSLionel Sambuc if (!IsVirtual) {
788f4a2713aSLionel Sambuc // Don't warn if any path is a non-virtually derived base at offset zero.
789f4a2713aSLionel Sambuc if (Offset.isZero())
790f4a2713aSLionel Sambuc return;
791f4a2713aSLionel Sambuc // Offset makes sense only for non-virtual bases.
792f4a2713aSLionel Sambuc else
793f4a2713aSLionel Sambuc NonZeroOffset = true;
794f4a2713aSLionel Sambuc }
795f4a2713aSLionel Sambuc VirtualBase = VirtualBase && IsVirtual;
796f4a2713aSLionel Sambuc }
797f4a2713aSLionel Sambuc
798f4a2713aSLionel Sambuc (void) NonZeroOffset; // Silence set but not used warning.
799f4a2713aSLionel Sambuc assert((VirtualBase || NonZeroOffset) &&
800f4a2713aSLionel Sambuc "Should have returned if has non-virtual base with zero offset");
801f4a2713aSLionel Sambuc
802f4a2713aSLionel Sambuc QualType BaseType =
803f4a2713aSLionel Sambuc ReinterpretKind == ReinterpretUpcast? DestType : SrcType;
804f4a2713aSLionel Sambuc QualType DerivedType =
805f4a2713aSLionel Sambuc ReinterpretKind == ReinterpretUpcast? SrcType : DestType;
806f4a2713aSLionel Sambuc
807f4a2713aSLionel Sambuc SourceLocation BeginLoc = OpRange.getBegin();
808f4a2713aSLionel Sambuc Self.Diag(BeginLoc, diag::warn_reinterpret_different_from_static)
809f4a2713aSLionel Sambuc << DerivedType << BaseType << !VirtualBase << int(ReinterpretKind)
810f4a2713aSLionel Sambuc << OpRange;
811f4a2713aSLionel Sambuc Self.Diag(BeginLoc, diag::note_reinterpret_updowncast_use_static)
812f4a2713aSLionel Sambuc << int(ReinterpretKind)
813f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(BeginLoc, "static_cast");
814f4a2713aSLionel Sambuc }
815f4a2713aSLionel Sambuc
816f4a2713aSLionel Sambuc /// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is
817f4a2713aSLionel Sambuc /// valid.
818f4a2713aSLionel Sambuc /// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code
819f4a2713aSLionel Sambuc /// like this:
820f4a2713aSLionel Sambuc /// char *bytes = reinterpret_cast\<char*\>(int_ptr);
CheckReinterpretCast()821f4a2713aSLionel Sambuc void CastOperation::CheckReinterpretCast() {
822f4a2713aSLionel Sambuc if (ValueKind == VK_RValue && !isPlaceholder(BuiltinType::Overload))
823*0a6a1f1dSLionel Sambuc SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
824f4a2713aSLionel Sambuc else
825f4a2713aSLionel Sambuc checkNonOverloadPlaceholders();
826f4a2713aSLionel Sambuc if (SrcExpr.isInvalid()) // if conversion failed, don't report another error
827f4a2713aSLionel Sambuc return;
828f4a2713aSLionel Sambuc
829f4a2713aSLionel Sambuc unsigned msg = diag::err_bad_cxx_cast_generic;
830f4a2713aSLionel Sambuc TryCastResult tcr =
831f4a2713aSLionel Sambuc TryReinterpretCast(Self, SrcExpr, DestType,
832f4a2713aSLionel Sambuc /*CStyle*/false, OpRange, msg, Kind);
833f4a2713aSLionel Sambuc if (tcr != TC_Success && msg != 0)
834f4a2713aSLionel Sambuc {
835f4a2713aSLionel Sambuc if (SrcExpr.isInvalid()) // if conversion failed, don't report another error
836f4a2713aSLionel Sambuc return;
837f4a2713aSLionel Sambuc if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
838f4a2713aSLionel Sambuc //FIXME: &f<int>; is overloaded and resolvable
839f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload)
840f4a2713aSLionel Sambuc << OverloadExpr::find(SrcExpr.get()).Expression->getName()
841f4a2713aSLionel Sambuc << DestType << OpRange;
842f4a2713aSLionel Sambuc Self.NoteAllOverloadCandidates(SrcExpr.get());
843f4a2713aSLionel Sambuc
844f4a2713aSLionel Sambuc } else {
845f4a2713aSLionel Sambuc diagnoseBadCast(Self, msg, CT_Reinterpret, OpRange, SrcExpr.get(),
846f4a2713aSLionel Sambuc DestType, /*listInitialization=*/false);
847f4a2713aSLionel Sambuc }
848f4a2713aSLionel Sambuc SrcExpr = ExprError();
849f4a2713aSLionel Sambuc } else if (tcr == TC_Success) {
850f4a2713aSLionel Sambuc if (Self.getLangOpts().ObjCAutoRefCount)
851f4a2713aSLionel Sambuc checkObjCARCConversion(Sema::CCK_OtherCast);
852f4a2713aSLionel Sambuc DiagnoseReinterpretUpDownCast(Self, SrcExpr.get(), DestType, OpRange);
853f4a2713aSLionel Sambuc }
854f4a2713aSLionel Sambuc }
855f4a2713aSLionel Sambuc
856f4a2713aSLionel Sambuc
857f4a2713aSLionel Sambuc /// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid.
858f4a2713aSLionel Sambuc /// Refer to C++ 5.2.9 for details. Static casts are mostly used for making
859f4a2713aSLionel Sambuc /// implicit conversions explicit and getting rid of data loss warnings.
CheckStaticCast()860f4a2713aSLionel Sambuc void CastOperation::CheckStaticCast() {
861f4a2713aSLionel Sambuc if (isPlaceholder()) {
862f4a2713aSLionel Sambuc checkNonOverloadPlaceholders();
863f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
864f4a2713aSLionel Sambuc return;
865f4a2713aSLionel Sambuc }
866f4a2713aSLionel Sambuc
867f4a2713aSLionel Sambuc // This test is outside everything else because it's the only case where
868f4a2713aSLionel Sambuc // a non-lvalue-reference target type does not lead to decay.
869f4a2713aSLionel Sambuc // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
870f4a2713aSLionel Sambuc if (DestType->isVoidType()) {
871f4a2713aSLionel Sambuc Kind = CK_ToVoid;
872f4a2713aSLionel Sambuc
873f4a2713aSLionel Sambuc if (claimPlaceholder(BuiltinType::Overload)) {
874f4a2713aSLionel Sambuc Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr,
875f4a2713aSLionel Sambuc false, // Decay Function to ptr
876f4a2713aSLionel Sambuc true, // Complain
877f4a2713aSLionel Sambuc OpRange, DestType, diag::err_bad_static_cast_overload);
878f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
879f4a2713aSLionel Sambuc return;
880f4a2713aSLionel Sambuc }
881f4a2713aSLionel Sambuc
882*0a6a1f1dSLionel Sambuc SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
883f4a2713aSLionel Sambuc return;
884f4a2713aSLionel Sambuc }
885f4a2713aSLionel Sambuc
886f4a2713aSLionel Sambuc if (ValueKind == VK_RValue && !DestType->isRecordType() &&
887f4a2713aSLionel Sambuc !isPlaceholder(BuiltinType::Overload)) {
888*0a6a1f1dSLionel Sambuc SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
889f4a2713aSLionel Sambuc if (SrcExpr.isInvalid()) // if conversion failed, don't report another error
890f4a2713aSLionel Sambuc return;
891f4a2713aSLionel Sambuc }
892f4a2713aSLionel Sambuc
893f4a2713aSLionel Sambuc unsigned msg = diag::err_bad_cxx_cast_generic;
894f4a2713aSLionel Sambuc TryCastResult tcr
895f4a2713aSLionel Sambuc = TryStaticCast(Self, SrcExpr, DestType, Sema::CCK_OtherCast, OpRange, msg,
896f4a2713aSLionel Sambuc Kind, BasePath, /*ListInitialization=*/false);
897f4a2713aSLionel Sambuc if (tcr != TC_Success && msg != 0) {
898f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
899f4a2713aSLionel Sambuc return;
900f4a2713aSLionel Sambuc if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
901f4a2713aSLionel Sambuc OverloadExpr* oe = OverloadExpr::find(SrcExpr.get()).Expression;
902f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
903f4a2713aSLionel Sambuc << oe->getName() << DestType << OpRange
904f4a2713aSLionel Sambuc << oe->getQualifierLoc().getSourceRange();
905f4a2713aSLionel Sambuc Self.NoteAllOverloadCandidates(SrcExpr.get());
906f4a2713aSLionel Sambuc } else {
907f4a2713aSLionel Sambuc diagnoseBadCast(Self, msg, CT_Static, OpRange, SrcExpr.get(), DestType,
908f4a2713aSLionel Sambuc /*listInitialization=*/false);
909f4a2713aSLionel Sambuc }
910f4a2713aSLionel Sambuc SrcExpr = ExprError();
911f4a2713aSLionel Sambuc } else if (tcr == TC_Success) {
912f4a2713aSLionel Sambuc if (Kind == CK_BitCast)
913f4a2713aSLionel Sambuc checkCastAlign();
914f4a2713aSLionel Sambuc if (Self.getLangOpts().ObjCAutoRefCount)
915f4a2713aSLionel Sambuc checkObjCARCConversion(Sema::CCK_OtherCast);
916f4a2713aSLionel Sambuc } else if (Kind == CK_BitCast) {
917f4a2713aSLionel Sambuc checkCastAlign();
918f4a2713aSLionel Sambuc }
919f4a2713aSLionel Sambuc }
920f4a2713aSLionel Sambuc
921f4a2713aSLionel Sambuc /// TryStaticCast - Check if a static cast can be performed, and do so if
922f4a2713aSLionel Sambuc /// possible. If @p CStyle, ignore access restrictions on hierarchy casting
923f4a2713aSLionel Sambuc /// and casting away constness.
TryStaticCast(Sema & Self,ExprResult & SrcExpr,QualType DestType,Sema::CheckedConversionKind CCK,const SourceRange & OpRange,unsigned & msg,CastKind & Kind,CXXCastPath & BasePath,bool ListInitialization)924f4a2713aSLionel Sambuc static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr,
925f4a2713aSLionel Sambuc QualType DestType,
926f4a2713aSLionel Sambuc Sema::CheckedConversionKind CCK,
927f4a2713aSLionel Sambuc const SourceRange &OpRange, unsigned &msg,
928f4a2713aSLionel Sambuc CastKind &Kind, CXXCastPath &BasePath,
929f4a2713aSLionel Sambuc bool ListInitialization) {
930f4a2713aSLionel Sambuc // Determine whether we have the semantics of a C-style cast.
931f4a2713aSLionel Sambuc bool CStyle
932f4a2713aSLionel Sambuc = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast);
933f4a2713aSLionel Sambuc
934f4a2713aSLionel Sambuc // The order the tests is not entirely arbitrary. There is one conversion
935f4a2713aSLionel Sambuc // that can be handled in two different ways. Given:
936f4a2713aSLionel Sambuc // struct A {};
937f4a2713aSLionel Sambuc // struct B : public A {
938f4a2713aSLionel Sambuc // B(); B(const A&);
939f4a2713aSLionel Sambuc // };
940f4a2713aSLionel Sambuc // const A &a = B();
941f4a2713aSLionel Sambuc // the cast static_cast<const B&>(a) could be seen as either a static
942f4a2713aSLionel Sambuc // reference downcast, or an explicit invocation of the user-defined
943f4a2713aSLionel Sambuc // conversion using B's conversion constructor.
944f4a2713aSLionel Sambuc // DR 427 specifies that the downcast is to be applied here.
945f4a2713aSLionel Sambuc
946f4a2713aSLionel Sambuc // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
947f4a2713aSLionel Sambuc // Done outside this function.
948f4a2713aSLionel Sambuc
949f4a2713aSLionel Sambuc TryCastResult tcr;
950f4a2713aSLionel Sambuc
951f4a2713aSLionel Sambuc // C++ 5.2.9p5, reference downcast.
952f4a2713aSLionel Sambuc // See the function for details.
953f4a2713aSLionel Sambuc // DR 427 specifies that this is to be applied before paragraph 2.
954f4a2713aSLionel Sambuc tcr = TryStaticReferenceDowncast(Self, SrcExpr.get(), DestType, CStyle,
955f4a2713aSLionel Sambuc OpRange, msg, Kind, BasePath);
956f4a2713aSLionel Sambuc if (tcr != TC_NotApplicable)
957f4a2713aSLionel Sambuc return tcr;
958f4a2713aSLionel Sambuc
959f4a2713aSLionel Sambuc // C++0x [expr.static.cast]p3:
960f4a2713aSLionel Sambuc // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2
961f4a2713aSLionel Sambuc // T2" if "cv2 T2" is reference-compatible with "cv1 T1".
962f4a2713aSLionel Sambuc tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind,
963f4a2713aSLionel Sambuc BasePath, msg);
964f4a2713aSLionel Sambuc if (tcr != TC_NotApplicable)
965f4a2713aSLionel Sambuc return tcr;
966f4a2713aSLionel Sambuc
967f4a2713aSLionel Sambuc // C++ 5.2.9p2: An expression e can be explicitly converted to a type T
968f4a2713aSLionel Sambuc // [...] if the declaration "T t(e);" is well-formed, [...].
969f4a2713aSLionel Sambuc tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CCK, OpRange, msg,
970f4a2713aSLionel Sambuc Kind, ListInitialization);
971f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
972f4a2713aSLionel Sambuc return TC_Failed;
973f4a2713aSLionel Sambuc if (tcr != TC_NotApplicable)
974f4a2713aSLionel Sambuc return tcr;
975f4a2713aSLionel Sambuc
976f4a2713aSLionel Sambuc // C++ 5.2.9p6: May apply the reverse of any standard conversion, except
977f4a2713aSLionel Sambuc // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
978f4a2713aSLionel Sambuc // conversions, subject to further restrictions.
979f4a2713aSLionel Sambuc // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
980f4a2713aSLionel Sambuc // of qualification conversions impossible.
981f4a2713aSLionel Sambuc // In the CStyle case, the earlier attempt to const_cast should have taken
982f4a2713aSLionel Sambuc // care of reverse qualification conversions.
983f4a2713aSLionel Sambuc
984f4a2713aSLionel Sambuc QualType SrcType = Self.Context.getCanonicalType(SrcExpr.get()->getType());
985f4a2713aSLionel Sambuc
986f4a2713aSLionel Sambuc // C++0x 5.2.9p9: A value of a scoped enumeration type can be explicitly
987f4a2713aSLionel Sambuc // converted to an integral type. [...] A value of a scoped enumeration type
988f4a2713aSLionel Sambuc // can also be explicitly converted to a floating-point type [...].
989f4a2713aSLionel Sambuc if (const EnumType *Enum = SrcType->getAs<EnumType>()) {
990f4a2713aSLionel Sambuc if (Enum->getDecl()->isScoped()) {
991f4a2713aSLionel Sambuc if (DestType->isBooleanType()) {
992f4a2713aSLionel Sambuc Kind = CK_IntegralToBoolean;
993f4a2713aSLionel Sambuc return TC_Success;
994f4a2713aSLionel Sambuc } else if (DestType->isIntegralType(Self.Context)) {
995f4a2713aSLionel Sambuc Kind = CK_IntegralCast;
996f4a2713aSLionel Sambuc return TC_Success;
997f4a2713aSLionel Sambuc } else if (DestType->isRealFloatingType()) {
998f4a2713aSLionel Sambuc Kind = CK_IntegralToFloating;
999f4a2713aSLionel Sambuc return TC_Success;
1000f4a2713aSLionel Sambuc }
1001f4a2713aSLionel Sambuc }
1002f4a2713aSLionel Sambuc }
1003f4a2713aSLionel Sambuc
1004f4a2713aSLionel Sambuc // Reverse integral promotion/conversion. All such conversions are themselves
1005f4a2713aSLionel Sambuc // again integral promotions or conversions and are thus already handled by
1006f4a2713aSLionel Sambuc // p2 (TryDirectInitialization above).
1007f4a2713aSLionel Sambuc // (Note: any data loss warnings should be suppressed.)
1008f4a2713aSLionel Sambuc // The exception is the reverse of enum->integer, i.e. integer->enum (and
1009f4a2713aSLionel Sambuc // enum->enum). See also C++ 5.2.9p7.
1010f4a2713aSLionel Sambuc // The same goes for reverse floating point promotion/conversion and
1011f4a2713aSLionel Sambuc // floating-integral conversions. Again, only floating->enum is relevant.
1012f4a2713aSLionel Sambuc if (DestType->isEnumeralType()) {
1013f4a2713aSLionel Sambuc if (SrcType->isIntegralOrEnumerationType()) {
1014f4a2713aSLionel Sambuc Kind = CK_IntegralCast;
1015f4a2713aSLionel Sambuc return TC_Success;
1016f4a2713aSLionel Sambuc } else if (SrcType->isRealFloatingType()) {
1017f4a2713aSLionel Sambuc Kind = CK_FloatingToIntegral;
1018f4a2713aSLionel Sambuc return TC_Success;
1019f4a2713aSLionel Sambuc }
1020f4a2713aSLionel Sambuc }
1021f4a2713aSLionel Sambuc
1022f4a2713aSLionel Sambuc // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
1023f4a2713aSLionel Sambuc // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
1024f4a2713aSLionel Sambuc tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
1025f4a2713aSLionel Sambuc Kind, BasePath);
1026f4a2713aSLionel Sambuc if (tcr != TC_NotApplicable)
1027f4a2713aSLionel Sambuc return tcr;
1028f4a2713aSLionel Sambuc
1029f4a2713aSLionel Sambuc // Reverse member pointer conversion. C++ 4.11 specifies member pointer
1030f4a2713aSLionel Sambuc // conversion. C++ 5.2.9p9 has additional information.
1031f4a2713aSLionel Sambuc // DR54's access restrictions apply here also.
1032f4a2713aSLionel Sambuc tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
1033f4a2713aSLionel Sambuc OpRange, msg, Kind, BasePath);
1034f4a2713aSLionel Sambuc if (tcr != TC_NotApplicable)
1035f4a2713aSLionel Sambuc return tcr;
1036f4a2713aSLionel Sambuc
1037f4a2713aSLionel Sambuc // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
1038f4a2713aSLionel Sambuc // void*. C++ 5.2.9p10 specifies additional restrictions, which really is
1039f4a2713aSLionel Sambuc // just the usual constness stuff.
1040f4a2713aSLionel Sambuc if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
1041f4a2713aSLionel Sambuc QualType SrcPointee = SrcPointer->getPointeeType();
1042f4a2713aSLionel Sambuc if (SrcPointee->isVoidType()) {
1043f4a2713aSLionel Sambuc if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
1044f4a2713aSLionel Sambuc QualType DestPointee = DestPointer->getPointeeType();
1045f4a2713aSLionel Sambuc if (DestPointee->isIncompleteOrObjectType()) {
1046f4a2713aSLionel Sambuc // This is definitely the intended conversion, but it might fail due
1047f4a2713aSLionel Sambuc // to a qualifier violation. Note that we permit Objective-C lifetime
1048f4a2713aSLionel Sambuc // and GC qualifier mismatches here.
1049f4a2713aSLionel Sambuc if (!CStyle) {
1050f4a2713aSLionel Sambuc Qualifiers DestPointeeQuals = DestPointee.getQualifiers();
1051f4a2713aSLionel Sambuc Qualifiers SrcPointeeQuals = SrcPointee.getQualifiers();
1052f4a2713aSLionel Sambuc DestPointeeQuals.removeObjCGCAttr();
1053f4a2713aSLionel Sambuc DestPointeeQuals.removeObjCLifetime();
1054f4a2713aSLionel Sambuc SrcPointeeQuals.removeObjCGCAttr();
1055f4a2713aSLionel Sambuc SrcPointeeQuals.removeObjCLifetime();
1056f4a2713aSLionel Sambuc if (DestPointeeQuals != SrcPointeeQuals &&
1057f4a2713aSLionel Sambuc !DestPointeeQuals.compatiblyIncludes(SrcPointeeQuals)) {
1058f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_qualifiers_away;
1059f4a2713aSLionel Sambuc return TC_Failed;
1060f4a2713aSLionel Sambuc }
1061f4a2713aSLionel Sambuc }
1062f4a2713aSLionel Sambuc Kind = CK_BitCast;
1063f4a2713aSLionel Sambuc return TC_Success;
1064f4a2713aSLionel Sambuc }
1065f4a2713aSLionel Sambuc }
1066f4a2713aSLionel Sambuc else if (DestType->isObjCObjectPointerType()) {
1067f4a2713aSLionel Sambuc // allow both c-style cast and static_cast of objective-c pointers as
1068f4a2713aSLionel Sambuc // they are pervasive.
1069f4a2713aSLionel Sambuc Kind = CK_CPointerToObjCPointerCast;
1070f4a2713aSLionel Sambuc return TC_Success;
1071f4a2713aSLionel Sambuc }
1072f4a2713aSLionel Sambuc else if (CStyle && DestType->isBlockPointerType()) {
1073f4a2713aSLionel Sambuc // allow c-style cast of void * to block pointers.
1074f4a2713aSLionel Sambuc Kind = CK_AnyPointerToBlockPointerCast;
1075f4a2713aSLionel Sambuc return TC_Success;
1076f4a2713aSLionel Sambuc }
1077f4a2713aSLionel Sambuc }
1078f4a2713aSLionel Sambuc }
1079f4a2713aSLionel Sambuc // Allow arbitray objective-c pointer conversion with static casts.
1080f4a2713aSLionel Sambuc if (SrcType->isObjCObjectPointerType() &&
1081f4a2713aSLionel Sambuc DestType->isObjCObjectPointerType()) {
1082f4a2713aSLionel Sambuc Kind = CK_BitCast;
1083f4a2713aSLionel Sambuc return TC_Success;
1084f4a2713aSLionel Sambuc }
1085*0a6a1f1dSLionel Sambuc // Allow ns-pointer to cf-pointer conversion in either direction
1086*0a6a1f1dSLionel Sambuc // with static casts.
1087*0a6a1f1dSLionel Sambuc if (!CStyle &&
1088*0a6a1f1dSLionel Sambuc Self.CheckTollFreeBridgeStaticCast(DestType, SrcExpr.get(), Kind))
1089*0a6a1f1dSLionel Sambuc return TC_Success;
1090f4a2713aSLionel Sambuc
1091f4a2713aSLionel Sambuc // We tried everything. Everything! Nothing works! :-(
1092f4a2713aSLionel Sambuc return TC_NotApplicable;
1093f4a2713aSLionel Sambuc }
1094f4a2713aSLionel Sambuc
1095f4a2713aSLionel Sambuc /// Tests whether a conversion according to N2844 is valid.
1096f4a2713aSLionel Sambuc TryCastResult
TryLValueToRValueCast(Sema & Self,Expr * SrcExpr,QualType DestType,bool CStyle,CastKind & Kind,CXXCastPath & BasePath,unsigned & msg)1097f4a2713aSLionel Sambuc TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
1098f4a2713aSLionel Sambuc bool CStyle, CastKind &Kind, CXXCastPath &BasePath,
1099f4a2713aSLionel Sambuc unsigned &msg) {
1100f4a2713aSLionel Sambuc // C++0x [expr.static.cast]p3:
1101f4a2713aSLionel Sambuc // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to
1102f4a2713aSLionel Sambuc // cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
1103f4a2713aSLionel Sambuc const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
1104f4a2713aSLionel Sambuc if (!R)
1105f4a2713aSLionel Sambuc return TC_NotApplicable;
1106f4a2713aSLionel Sambuc
1107f4a2713aSLionel Sambuc if (!SrcExpr->isGLValue())
1108f4a2713aSLionel Sambuc return TC_NotApplicable;
1109f4a2713aSLionel Sambuc
1110f4a2713aSLionel Sambuc // Because we try the reference downcast before this function, from now on
1111f4a2713aSLionel Sambuc // this is the only cast possibility, so we issue an error if we fail now.
1112f4a2713aSLionel Sambuc // FIXME: Should allow casting away constness if CStyle.
1113f4a2713aSLionel Sambuc bool DerivedToBase;
1114f4a2713aSLionel Sambuc bool ObjCConversion;
1115f4a2713aSLionel Sambuc bool ObjCLifetimeConversion;
1116f4a2713aSLionel Sambuc QualType FromType = SrcExpr->getType();
1117f4a2713aSLionel Sambuc QualType ToType = R->getPointeeType();
1118f4a2713aSLionel Sambuc if (CStyle) {
1119f4a2713aSLionel Sambuc FromType = FromType.getUnqualifiedType();
1120f4a2713aSLionel Sambuc ToType = ToType.getUnqualifiedType();
1121f4a2713aSLionel Sambuc }
1122f4a2713aSLionel Sambuc
1123f4a2713aSLionel Sambuc if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
1124f4a2713aSLionel Sambuc ToType, FromType,
1125f4a2713aSLionel Sambuc DerivedToBase, ObjCConversion,
1126f4a2713aSLionel Sambuc ObjCLifetimeConversion)
1127f4a2713aSLionel Sambuc < Sema::Ref_Compatible_With_Added_Qualification) {
1128f4a2713aSLionel Sambuc msg = diag::err_bad_lvalue_to_rvalue_cast;
1129f4a2713aSLionel Sambuc return TC_Failed;
1130f4a2713aSLionel Sambuc }
1131f4a2713aSLionel Sambuc
1132f4a2713aSLionel Sambuc if (DerivedToBase) {
1133f4a2713aSLionel Sambuc Kind = CK_DerivedToBase;
1134f4a2713aSLionel Sambuc CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1135f4a2713aSLionel Sambuc /*DetectVirtual=*/true);
1136f4a2713aSLionel Sambuc if (!Self.IsDerivedFrom(SrcExpr->getType(), R->getPointeeType(), Paths))
1137f4a2713aSLionel Sambuc return TC_NotApplicable;
1138f4a2713aSLionel Sambuc
1139f4a2713aSLionel Sambuc Self.BuildBasePathArray(Paths, BasePath);
1140f4a2713aSLionel Sambuc } else
1141f4a2713aSLionel Sambuc Kind = CK_NoOp;
1142f4a2713aSLionel Sambuc
1143f4a2713aSLionel Sambuc return TC_Success;
1144f4a2713aSLionel Sambuc }
1145f4a2713aSLionel Sambuc
1146f4a2713aSLionel Sambuc /// Tests whether a conversion according to C++ 5.2.9p5 is valid.
1147f4a2713aSLionel Sambuc TryCastResult
TryStaticReferenceDowncast(Sema & Self,Expr * SrcExpr,QualType DestType,bool CStyle,const SourceRange & OpRange,unsigned & msg,CastKind & Kind,CXXCastPath & BasePath)1148f4a2713aSLionel Sambuc TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
1149f4a2713aSLionel Sambuc bool CStyle, const SourceRange &OpRange,
1150f4a2713aSLionel Sambuc unsigned &msg, CastKind &Kind,
1151f4a2713aSLionel Sambuc CXXCastPath &BasePath) {
1152f4a2713aSLionel Sambuc // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
1153f4a2713aSLionel Sambuc // cast to type "reference to cv2 D", where D is a class derived from B,
1154f4a2713aSLionel Sambuc // if a valid standard conversion from "pointer to D" to "pointer to B"
1155f4a2713aSLionel Sambuc // exists, cv2 >= cv1, and B is not a virtual base class of D.
1156f4a2713aSLionel Sambuc // In addition, DR54 clarifies that the base must be accessible in the
1157f4a2713aSLionel Sambuc // current context. Although the wording of DR54 only applies to the pointer
1158f4a2713aSLionel Sambuc // variant of this rule, the intent is clearly for it to apply to the this
1159f4a2713aSLionel Sambuc // conversion as well.
1160f4a2713aSLionel Sambuc
1161f4a2713aSLionel Sambuc const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
1162f4a2713aSLionel Sambuc if (!DestReference) {
1163f4a2713aSLionel Sambuc return TC_NotApplicable;
1164f4a2713aSLionel Sambuc }
1165f4a2713aSLionel Sambuc bool RValueRef = DestReference->isRValueReferenceType();
1166f4a2713aSLionel Sambuc if (!RValueRef && !SrcExpr->isLValue()) {
1167f4a2713aSLionel Sambuc // We know the left side is an lvalue reference, so we can suggest a reason.
1168f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_rvalue;
1169f4a2713aSLionel Sambuc return TC_NotApplicable;
1170f4a2713aSLionel Sambuc }
1171f4a2713aSLionel Sambuc
1172f4a2713aSLionel Sambuc QualType DestPointee = DestReference->getPointeeType();
1173f4a2713aSLionel Sambuc
1174*0a6a1f1dSLionel Sambuc // FIXME: If the source is a prvalue, we should issue a warning (because the
1175*0a6a1f1dSLionel Sambuc // cast always has undefined behavior), and for AST consistency, we should
1176*0a6a1f1dSLionel Sambuc // materialize a temporary.
1177f4a2713aSLionel Sambuc return TryStaticDowncast(Self,
1178f4a2713aSLionel Sambuc Self.Context.getCanonicalType(SrcExpr->getType()),
1179f4a2713aSLionel Sambuc Self.Context.getCanonicalType(DestPointee), CStyle,
1180f4a2713aSLionel Sambuc OpRange, SrcExpr->getType(), DestType, msg, Kind,
1181f4a2713aSLionel Sambuc BasePath);
1182f4a2713aSLionel Sambuc }
1183f4a2713aSLionel Sambuc
1184f4a2713aSLionel Sambuc /// Tests whether a conversion according to C++ 5.2.9p8 is valid.
1185f4a2713aSLionel Sambuc TryCastResult
TryStaticPointerDowncast(Sema & Self,QualType SrcType,QualType DestType,bool CStyle,const SourceRange & OpRange,unsigned & msg,CastKind & Kind,CXXCastPath & BasePath)1186f4a2713aSLionel Sambuc TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
1187f4a2713aSLionel Sambuc bool CStyle, const SourceRange &OpRange,
1188f4a2713aSLionel Sambuc unsigned &msg, CastKind &Kind,
1189f4a2713aSLionel Sambuc CXXCastPath &BasePath) {
1190f4a2713aSLionel Sambuc // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
1191f4a2713aSLionel Sambuc // type, can be converted to an rvalue of type "pointer to cv2 D", where D
1192f4a2713aSLionel Sambuc // is a class derived from B, if a valid standard conversion from "pointer
1193f4a2713aSLionel Sambuc // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
1194f4a2713aSLionel Sambuc // class of D.
1195f4a2713aSLionel Sambuc // In addition, DR54 clarifies that the base must be accessible in the
1196f4a2713aSLionel Sambuc // current context.
1197f4a2713aSLionel Sambuc
1198f4a2713aSLionel Sambuc const PointerType *DestPointer = DestType->getAs<PointerType>();
1199f4a2713aSLionel Sambuc if (!DestPointer) {
1200f4a2713aSLionel Sambuc return TC_NotApplicable;
1201f4a2713aSLionel Sambuc }
1202f4a2713aSLionel Sambuc
1203f4a2713aSLionel Sambuc const PointerType *SrcPointer = SrcType->getAs<PointerType>();
1204f4a2713aSLionel Sambuc if (!SrcPointer) {
1205f4a2713aSLionel Sambuc msg = diag::err_bad_static_cast_pointer_nonpointer;
1206f4a2713aSLionel Sambuc return TC_NotApplicable;
1207f4a2713aSLionel Sambuc }
1208f4a2713aSLionel Sambuc
1209f4a2713aSLionel Sambuc return TryStaticDowncast(Self,
1210f4a2713aSLionel Sambuc Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
1211f4a2713aSLionel Sambuc Self.Context.getCanonicalType(DestPointer->getPointeeType()),
1212f4a2713aSLionel Sambuc CStyle, OpRange, SrcType, DestType, msg, Kind,
1213f4a2713aSLionel Sambuc BasePath);
1214f4a2713aSLionel Sambuc }
1215f4a2713aSLionel Sambuc
1216f4a2713aSLionel Sambuc /// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
1217f4a2713aSLionel Sambuc /// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
1218f4a2713aSLionel Sambuc /// DestType is possible and allowed.
1219f4a2713aSLionel Sambuc TryCastResult
TryStaticDowncast(Sema & Self,CanQualType SrcType,CanQualType DestType,bool CStyle,const SourceRange & OpRange,QualType OrigSrcType,QualType OrigDestType,unsigned & msg,CastKind & Kind,CXXCastPath & BasePath)1220f4a2713aSLionel Sambuc TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
1221f4a2713aSLionel Sambuc bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
1222f4a2713aSLionel Sambuc QualType OrigDestType, unsigned &msg,
1223f4a2713aSLionel Sambuc CastKind &Kind, CXXCastPath &BasePath) {
1224f4a2713aSLionel Sambuc // We can only work with complete types. But don't complain if it doesn't work
1225f4a2713aSLionel Sambuc if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, 0) ||
1226f4a2713aSLionel Sambuc Self.RequireCompleteType(OpRange.getBegin(), DestType, 0))
1227f4a2713aSLionel Sambuc return TC_NotApplicable;
1228f4a2713aSLionel Sambuc
1229f4a2713aSLionel Sambuc // Downcast can only happen in class hierarchies, so we need classes.
1230f4a2713aSLionel Sambuc if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
1231f4a2713aSLionel Sambuc return TC_NotApplicable;
1232f4a2713aSLionel Sambuc }
1233f4a2713aSLionel Sambuc
1234f4a2713aSLionel Sambuc CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1235f4a2713aSLionel Sambuc /*DetectVirtual=*/true);
1236f4a2713aSLionel Sambuc if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
1237f4a2713aSLionel Sambuc return TC_NotApplicable;
1238f4a2713aSLionel Sambuc }
1239f4a2713aSLionel Sambuc
1240f4a2713aSLionel Sambuc // Target type does derive from source type. Now we're serious. If an error
1241f4a2713aSLionel Sambuc // appears now, it's not ignored.
1242f4a2713aSLionel Sambuc // This may not be entirely in line with the standard. Take for example:
1243f4a2713aSLionel Sambuc // struct A {};
1244f4a2713aSLionel Sambuc // struct B : virtual A {
1245f4a2713aSLionel Sambuc // B(A&);
1246f4a2713aSLionel Sambuc // };
1247f4a2713aSLionel Sambuc //
1248f4a2713aSLionel Sambuc // void f()
1249f4a2713aSLionel Sambuc // {
1250f4a2713aSLionel Sambuc // (void)static_cast<const B&>(*((A*)0));
1251f4a2713aSLionel Sambuc // }
1252f4a2713aSLionel Sambuc // As far as the standard is concerned, p5 does not apply (A is virtual), so
1253f4a2713aSLionel Sambuc // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
1254f4a2713aSLionel Sambuc // However, both GCC and Comeau reject this example, and accepting it would
1255f4a2713aSLionel Sambuc // mean more complex code if we're to preserve the nice error message.
1256f4a2713aSLionel Sambuc // FIXME: Being 100% compliant here would be nice to have.
1257f4a2713aSLionel Sambuc
1258f4a2713aSLionel Sambuc // Must preserve cv, as always, unless we're in C-style mode.
1259f4a2713aSLionel Sambuc if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
1260f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_qualifiers_away;
1261f4a2713aSLionel Sambuc return TC_Failed;
1262f4a2713aSLionel Sambuc }
1263f4a2713aSLionel Sambuc
1264f4a2713aSLionel Sambuc if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
1265f4a2713aSLionel Sambuc // This code is analoguous to that in CheckDerivedToBaseConversion, except
1266f4a2713aSLionel Sambuc // that it builds the paths in reverse order.
1267f4a2713aSLionel Sambuc // To sum up: record all paths to the base and build a nice string from
1268f4a2713aSLionel Sambuc // them. Use it to spice up the error message.
1269f4a2713aSLionel Sambuc if (!Paths.isRecordingPaths()) {
1270f4a2713aSLionel Sambuc Paths.clear();
1271f4a2713aSLionel Sambuc Paths.setRecordingPaths(true);
1272f4a2713aSLionel Sambuc Self.IsDerivedFrom(DestType, SrcType, Paths);
1273f4a2713aSLionel Sambuc }
1274f4a2713aSLionel Sambuc std::string PathDisplayStr;
1275f4a2713aSLionel Sambuc std::set<unsigned> DisplayedPaths;
1276f4a2713aSLionel Sambuc for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
1277f4a2713aSLionel Sambuc PI != PE; ++PI) {
1278f4a2713aSLionel Sambuc if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
1279f4a2713aSLionel Sambuc // We haven't displayed a path to this particular base
1280f4a2713aSLionel Sambuc // class subobject yet.
1281f4a2713aSLionel Sambuc PathDisplayStr += "\n ";
1282f4a2713aSLionel Sambuc for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
1283f4a2713aSLionel Sambuc EE = PI->rend();
1284f4a2713aSLionel Sambuc EI != EE; ++EI)
1285f4a2713aSLionel Sambuc PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
1286f4a2713aSLionel Sambuc PathDisplayStr += QualType(DestType).getAsString();
1287f4a2713aSLionel Sambuc }
1288f4a2713aSLionel Sambuc }
1289f4a2713aSLionel Sambuc
1290f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
1291f4a2713aSLionel Sambuc << QualType(SrcType).getUnqualifiedType()
1292f4a2713aSLionel Sambuc << QualType(DestType).getUnqualifiedType()
1293f4a2713aSLionel Sambuc << PathDisplayStr << OpRange;
1294f4a2713aSLionel Sambuc msg = 0;
1295f4a2713aSLionel Sambuc return TC_Failed;
1296f4a2713aSLionel Sambuc }
1297f4a2713aSLionel Sambuc
1298*0a6a1f1dSLionel Sambuc if (Paths.getDetectedVirtual() != nullptr) {
1299f4a2713aSLionel Sambuc QualType VirtualBase(Paths.getDetectedVirtual(), 0);
1300f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
1301f4a2713aSLionel Sambuc << OrigSrcType << OrigDestType << VirtualBase << OpRange;
1302f4a2713aSLionel Sambuc msg = 0;
1303f4a2713aSLionel Sambuc return TC_Failed;
1304f4a2713aSLionel Sambuc }
1305f4a2713aSLionel Sambuc
1306f4a2713aSLionel Sambuc if (!CStyle) {
1307f4a2713aSLionel Sambuc switch (Self.CheckBaseClassAccess(OpRange.getBegin(),
1308f4a2713aSLionel Sambuc SrcType, DestType,
1309f4a2713aSLionel Sambuc Paths.front(),
1310f4a2713aSLionel Sambuc diag::err_downcast_from_inaccessible_base)) {
1311f4a2713aSLionel Sambuc case Sema::AR_accessible:
1312f4a2713aSLionel Sambuc case Sema::AR_delayed: // be optimistic
1313f4a2713aSLionel Sambuc case Sema::AR_dependent: // be optimistic
1314f4a2713aSLionel Sambuc break;
1315f4a2713aSLionel Sambuc
1316f4a2713aSLionel Sambuc case Sema::AR_inaccessible:
1317f4a2713aSLionel Sambuc msg = 0;
1318f4a2713aSLionel Sambuc return TC_Failed;
1319f4a2713aSLionel Sambuc }
1320f4a2713aSLionel Sambuc }
1321f4a2713aSLionel Sambuc
1322f4a2713aSLionel Sambuc Self.BuildBasePathArray(Paths, BasePath);
1323f4a2713aSLionel Sambuc Kind = CK_BaseToDerived;
1324f4a2713aSLionel Sambuc return TC_Success;
1325f4a2713aSLionel Sambuc }
1326f4a2713aSLionel Sambuc
1327f4a2713aSLionel Sambuc /// TryStaticMemberPointerUpcast - Tests whether a conversion according to
1328f4a2713aSLionel Sambuc /// C++ 5.2.9p9 is valid:
1329f4a2713aSLionel Sambuc ///
1330f4a2713aSLionel Sambuc /// An rvalue of type "pointer to member of D of type cv1 T" can be
1331f4a2713aSLionel Sambuc /// converted to an rvalue of type "pointer to member of B of type cv2 T",
1332f4a2713aSLionel Sambuc /// where B is a base class of D [...].
1333f4a2713aSLionel Sambuc ///
1334f4a2713aSLionel Sambuc TryCastResult
TryStaticMemberPointerUpcast(Sema & Self,ExprResult & SrcExpr,QualType SrcType,QualType DestType,bool CStyle,const SourceRange & OpRange,unsigned & msg,CastKind & Kind,CXXCastPath & BasePath)1335f4a2713aSLionel Sambuc TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, QualType SrcType,
1336f4a2713aSLionel Sambuc QualType DestType, bool CStyle,
1337f4a2713aSLionel Sambuc const SourceRange &OpRange,
1338f4a2713aSLionel Sambuc unsigned &msg, CastKind &Kind,
1339f4a2713aSLionel Sambuc CXXCastPath &BasePath) {
1340f4a2713aSLionel Sambuc const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
1341f4a2713aSLionel Sambuc if (!DestMemPtr)
1342f4a2713aSLionel Sambuc return TC_NotApplicable;
1343f4a2713aSLionel Sambuc
1344f4a2713aSLionel Sambuc bool WasOverloadedFunction = false;
1345f4a2713aSLionel Sambuc DeclAccessPair FoundOverload;
1346f4a2713aSLionel Sambuc if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
1347f4a2713aSLionel Sambuc if (FunctionDecl *Fn
1348f4a2713aSLionel Sambuc = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), DestType, false,
1349f4a2713aSLionel Sambuc FoundOverload)) {
1350f4a2713aSLionel Sambuc CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
1351f4a2713aSLionel Sambuc SrcType = Self.Context.getMemberPointerType(Fn->getType(),
1352f4a2713aSLionel Sambuc Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
1353f4a2713aSLionel Sambuc WasOverloadedFunction = true;
1354f4a2713aSLionel Sambuc }
1355f4a2713aSLionel Sambuc }
1356f4a2713aSLionel Sambuc
1357f4a2713aSLionel Sambuc const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
1358f4a2713aSLionel Sambuc if (!SrcMemPtr) {
1359f4a2713aSLionel Sambuc msg = diag::err_bad_static_cast_member_pointer_nonmp;
1360f4a2713aSLionel Sambuc return TC_NotApplicable;
1361f4a2713aSLionel Sambuc }
1362f4a2713aSLionel Sambuc
1363f4a2713aSLionel Sambuc // T == T, modulo cv
1364f4a2713aSLionel Sambuc if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
1365f4a2713aSLionel Sambuc DestMemPtr->getPointeeType()))
1366f4a2713aSLionel Sambuc return TC_NotApplicable;
1367f4a2713aSLionel Sambuc
1368f4a2713aSLionel Sambuc // B base of D
1369f4a2713aSLionel Sambuc QualType SrcClass(SrcMemPtr->getClass(), 0);
1370f4a2713aSLionel Sambuc QualType DestClass(DestMemPtr->getClass(), 0);
1371f4a2713aSLionel Sambuc CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1372f4a2713aSLionel Sambuc /*DetectVirtual=*/true);
1373*0a6a1f1dSLionel Sambuc if (Self.RequireCompleteType(OpRange.getBegin(), SrcClass, 0) ||
1374*0a6a1f1dSLionel Sambuc !Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
1375f4a2713aSLionel Sambuc return TC_NotApplicable;
1376f4a2713aSLionel Sambuc }
1377f4a2713aSLionel Sambuc
1378f4a2713aSLionel Sambuc // B is a base of D. But is it an allowed base? If not, it's a hard error.
1379f4a2713aSLionel Sambuc if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) {
1380f4a2713aSLionel Sambuc Paths.clear();
1381f4a2713aSLionel Sambuc Paths.setRecordingPaths(true);
1382f4a2713aSLionel Sambuc bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
1383f4a2713aSLionel Sambuc assert(StillOkay);
1384f4a2713aSLionel Sambuc (void)StillOkay;
1385f4a2713aSLionel Sambuc std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
1386f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
1387f4a2713aSLionel Sambuc << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
1388f4a2713aSLionel Sambuc msg = 0;
1389f4a2713aSLionel Sambuc return TC_Failed;
1390f4a2713aSLionel Sambuc }
1391f4a2713aSLionel Sambuc
1392f4a2713aSLionel Sambuc if (const RecordType *VBase = Paths.getDetectedVirtual()) {
1393f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
1394f4a2713aSLionel Sambuc << SrcClass << DestClass << QualType(VBase, 0) << OpRange;
1395f4a2713aSLionel Sambuc msg = 0;
1396f4a2713aSLionel Sambuc return TC_Failed;
1397f4a2713aSLionel Sambuc }
1398f4a2713aSLionel Sambuc
1399f4a2713aSLionel Sambuc if (!CStyle) {
1400f4a2713aSLionel Sambuc switch (Self.CheckBaseClassAccess(OpRange.getBegin(),
1401f4a2713aSLionel Sambuc DestClass, SrcClass,
1402f4a2713aSLionel Sambuc Paths.front(),
1403f4a2713aSLionel Sambuc diag::err_upcast_to_inaccessible_base)) {
1404f4a2713aSLionel Sambuc case Sema::AR_accessible:
1405f4a2713aSLionel Sambuc case Sema::AR_delayed:
1406f4a2713aSLionel Sambuc case Sema::AR_dependent:
1407f4a2713aSLionel Sambuc // Optimistically assume that the delayed and dependent cases
1408f4a2713aSLionel Sambuc // will work out.
1409f4a2713aSLionel Sambuc break;
1410f4a2713aSLionel Sambuc
1411f4a2713aSLionel Sambuc case Sema::AR_inaccessible:
1412f4a2713aSLionel Sambuc msg = 0;
1413f4a2713aSLionel Sambuc return TC_Failed;
1414f4a2713aSLionel Sambuc }
1415f4a2713aSLionel Sambuc }
1416f4a2713aSLionel Sambuc
1417f4a2713aSLionel Sambuc if (WasOverloadedFunction) {
1418f4a2713aSLionel Sambuc // Resolve the address of the overloaded function again, this time
1419f4a2713aSLionel Sambuc // allowing complaints if something goes wrong.
1420f4a2713aSLionel Sambuc FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(),
1421f4a2713aSLionel Sambuc DestType,
1422f4a2713aSLionel Sambuc true,
1423f4a2713aSLionel Sambuc FoundOverload);
1424f4a2713aSLionel Sambuc if (!Fn) {
1425f4a2713aSLionel Sambuc msg = 0;
1426f4a2713aSLionel Sambuc return TC_Failed;
1427f4a2713aSLionel Sambuc }
1428f4a2713aSLionel Sambuc
1429f4a2713aSLionel Sambuc SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
1430f4a2713aSLionel Sambuc if (!SrcExpr.isUsable()) {
1431f4a2713aSLionel Sambuc msg = 0;
1432f4a2713aSLionel Sambuc return TC_Failed;
1433f4a2713aSLionel Sambuc }
1434f4a2713aSLionel Sambuc }
1435f4a2713aSLionel Sambuc
1436f4a2713aSLionel Sambuc Self.BuildBasePathArray(Paths, BasePath);
1437f4a2713aSLionel Sambuc Kind = CK_DerivedToBaseMemberPointer;
1438f4a2713aSLionel Sambuc return TC_Success;
1439f4a2713aSLionel Sambuc }
1440f4a2713aSLionel Sambuc
1441f4a2713aSLionel Sambuc /// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
1442f4a2713aSLionel Sambuc /// is valid:
1443f4a2713aSLionel Sambuc ///
1444f4a2713aSLionel Sambuc /// An expression e can be explicitly converted to a type T using a
1445f4a2713aSLionel Sambuc /// @c static_cast if the declaration "T t(e);" is well-formed [...].
1446f4a2713aSLionel Sambuc TryCastResult
TryStaticImplicitCast(Sema & Self,ExprResult & SrcExpr,QualType DestType,Sema::CheckedConversionKind CCK,const SourceRange & OpRange,unsigned & msg,CastKind & Kind,bool ListInitialization)1447f4a2713aSLionel Sambuc TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
1448f4a2713aSLionel Sambuc Sema::CheckedConversionKind CCK,
1449f4a2713aSLionel Sambuc const SourceRange &OpRange, unsigned &msg,
1450f4a2713aSLionel Sambuc CastKind &Kind, bool ListInitialization) {
1451f4a2713aSLionel Sambuc if (DestType->isRecordType()) {
1452f4a2713aSLionel Sambuc if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
1453f4a2713aSLionel Sambuc diag::err_bad_dynamic_cast_incomplete) ||
1454f4a2713aSLionel Sambuc Self.RequireNonAbstractType(OpRange.getBegin(), DestType,
1455f4a2713aSLionel Sambuc diag::err_allocation_of_abstract_type)) {
1456f4a2713aSLionel Sambuc msg = 0;
1457f4a2713aSLionel Sambuc return TC_Failed;
1458f4a2713aSLionel Sambuc }
1459*0a6a1f1dSLionel Sambuc } else if (DestType->isMemberPointerType()) {
1460*0a6a1f1dSLionel Sambuc if (Self.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
1461*0a6a1f1dSLionel Sambuc Self.RequireCompleteType(OpRange.getBegin(), DestType, 0);
1462*0a6a1f1dSLionel Sambuc }
1463f4a2713aSLionel Sambuc }
1464f4a2713aSLionel Sambuc
1465f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
1466f4a2713aSLionel Sambuc InitializationKind InitKind
1467f4a2713aSLionel Sambuc = (CCK == Sema::CCK_CStyleCast)
1468f4a2713aSLionel Sambuc ? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange,
1469f4a2713aSLionel Sambuc ListInitialization)
1470f4a2713aSLionel Sambuc : (CCK == Sema::CCK_FunctionalCast)
1471f4a2713aSLionel Sambuc ? InitializationKind::CreateFunctionalCast(OpRange, ListInitialization)
1472f4a2713aSLionel Sambuc : InitializationKind::CreateCast(OpRange);
1473f4a2713aSLionel Sambuc Expr *SrcExprRaw = SrcExpr.get();
1474f4a2713aSLionel Sambuc InitializationSequence InitSeq(Self, Entity, InitKind, SrcExprRaw);
1475f4a2713aSLionel Sambuc
1476f4a2713aSLionel Sambuc // At this point of CheckStaticCast, if the destination is a reference,
1477f4a2713aSLionel Sambuc // or the expression is an overload expression this has to work.
1478f4a2713aSLionel Sambuc // There is no other way that works.
1479f4a2713aSLionel Sambuc // On the other hand, if we're checking a C-style cast, we've still got
1480f4a2713aSLionel Sambuc // the reinterpret_cast way.
1481f4a2713aSLionel Sambuc bool CStyle
1482f4a2713aSLionel Sambuc = (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast);
1483f4a2713aSLionel Sambuc if (InitSeq.Failed() && (CStyle || !DestType->isReferenceType()))
1484f4a2713aSLionel Sambuc return TC_NotApplicable;
1485f4a2713aSLionel Sambuc
1486f4a2713aSLionel Sambuc ExprResult Result = InitSeq.Perform(Self, Entity, InitKind, SrcExprRaw);
1487f4a2713aSLionel Sambuc if (Result.isInvalid()) {
1488f4a2713aSLionel Sambuc msg = 0;
1489f4a2713aSLionel Sambuc return TC_Failed;
1490f4a2713aSLionel Sambuc }
1491f4a2713aSLionel Sambuc
1492f4a2713aSLionel Sambuc if (InitSeq.isConstructorInitialization())
1493f4a2713aSLionel Sambuc Kind = CK_ConstructorConversion;
1494f4a2713aSLionel Sambuc else
1495f4a2713aSLionel Sambuc Kind = CK_NoOp;
1496f4a2713aSLionel Sambuc
1497f4a2713aSLionel Sambuc SrcExpr = Result;
1498f4a2713aSLionel Sambuc return TC_Success;
1499f4a2713aSLionel Sambuc }
1500f4a2713aSLionel Sambuc
1501f4a2713aSLionel Sambuc /// TryConstCast - See if a const_cast from source to destination is allowed,
1502f4a2713aSLionel Sambuc /// and perform it if it is.
TryConstCast(Sema & Self,ExprResult & SrcExpr,QualType DestType,bool CStyle,unsigned & msg)1503f4a2713aSLionel Sambuc static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr,
1504f4a2713aSLionel Sambuc QualType DestType, bool CStyle,
1505f4a2713aSLionel Sambuc unsigned &msg) {
1506f4a2713aSLionel Sambuc DestType = Self.Context.getCanonicalType(DestType);
1507f4a2713aSLionel Sambuc QualType SrcType = SrcExpr.get()->getType();
1508f4a2713aSLionel Sambuc bool NeedToMaterializeTemporary = false;
1509f4a2713aSLionel Sambuc
1510f4a2713aSLionel Sambuc if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) {
1511f4a2713aSLionel Sambuc // C++11 5.2.11p4:
1512f4a2713aSLionel Sambuc // if a pointer to T1 can be explicitly converted to the type "pointer to
1513f4a2713aSLionel Sambuc // T2" using a const_cast, then the following conversions can also be
1514f4a2713aSLionel Sambuc // made:
1515f4a2713aSLionel Sambuc // -- an lvalue of type T1 can be explicitly converted to an lvalue of
1516f4a2713aSLionel Sambuc // type T2 using the cast const_cast<T2&>;
1517f4a2713aSLionel Sambuc // -- a glvalue of type T1 can be explicitly converted to an xvalue of
1518f4a2713aSLionel Sambuc // type T2 using the cast const_cast<T2&&>; and
1519f4a2713aSLionel Sambuc // -- if T1 is a class type, a prvalue of type T1 can be explicitly
1520f4a2713aSLionel Sambuc // converted to an xvalue of type T2 using the cast const_cast<T2&&>.
1521f4a2713aSLionel Sambuc
1522f4a2713aSLionel Sambuc if (isa<LValueReferenceType>(DestTypeTmp) && !SrcExpr.get()->isLValue()) {
1523f4a2713aSLionel Sambuc // Cannot const_cast non-lvalue to lvalue reference type. But if this
1524f4a2713aSLionel Sambuc // is C-style, static_cast might find a way, so we simply suggest a
1525f4a2713aSLionel Sambuc // message and tell the parent to keep searching.
1526f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_rvalue;
1527f4a2713aSLionel Sambuc return TC_NotApplicable;
1528f4a2713aSLionel Sambuc }
1529f4a2713aSLionel Sambuc
1530f4a2713aSLionel Sambuc if (isa<RValueReferenceType>(DestTypeTmp) && SrcExpr.get()->isRValue()) {
1531f4a2713aSLionel Sambuc if (!SrcType->isRecordType()) {
1532f4a2713aSLionel Sambuc // Cannot const_cast non-class prvalue to rvalue reference type. But if
1533f4a2713aSLionel Sambuc // this is C-style, static_cast can do this.
1534f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_rvalue;
1535f4a2713aSLionel Sambuc return TC_NotApplicable;
1536f4a2713aSLionel Sambuc }
1537f4a2713aSLionel Sambuc
1538f4a2713aSLionel Sambuc // Materialize the class prvalue so that the const_cast can bind a
1539f4a2713aSLionel Sambuc // reference to it.
1540f4a2713aSLionel Sambuc NeedToMaterializeTemporary = true;
1541f4a2713aSLionel Sambuc }
1542f4a2713aSLionel Sambuc
1543f4a2713aSLionel Sambuc // It's not completely clear under the standard whether we can
1544f4a2713aSLionel Sambuc // const_cast bit-field gl-values. Doing so would not be
1545f4a2713aSLionel Sambuc // intrinsically complicated, but for now, we say no for
1546f4a2713aSLionel Sambuc // consistency with other compilers and await the word of the
1547f4a2713aSLionel Sambuc // committee.
1548f4a2713aSLionel Sambuc if (SrcExpr.get()->refersToBitField()) {
1549f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_bitfield;
1550f4a2713aSLionel Sambuc return TC_NotApplicable;
1551f4a2713aSLionel Sambuc }
1552f4a2713aSLionel Sambuc
1553f4a2713aSLionel Sambuc DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1554f4a2713aSLionel Sambuc SrcType = Self.Context.getPointerType(SrcType);
1555f4a2713aSLionel Sambuc }
1556f4a2713aSLionel Sambuc
1557f4a2713aSLionel Sambuc // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
1558f4a2713aSLionel Sambuc // the rules for const_cast are the same as those used for pointers.
1559f4a2713aSLionel Sambuc
1560f4a2713aSLionel Sambuc if (!DestType->isPointerType() &&
1561f4a2713aSLionel Sambuc !DestType->isMemberPointerType() &&
1562f4a2713aSLionel Sambuc !DestType->isObjCObjectPointerType()) {
1563f4a2713aSLionel Sambuc // Cannot cast to non-pointer, non-reference type. Note that, if DestType
1564f4a2713aSLionel Sambuc // was a reference type, we converted it to a pointer above.
1565f4a2713aSLionel Sambuc // The status of rvalue references isn't entirely clear, but it looks like
1566f4a2713aSLionel Sambuc // conversion to them is simply invalid.
1567f4a2713aSLionel Sambuc // C++ 5.2.11p3: For two pointer types [...]
1568f4a2713aSLionel Sambuc if (!CStyle)
1569f4a2713aSLionel Sambuc msg = diag::err_bad_const_cast_dest;
1570f4a2713aSLionel Sambuc return TC_NotApplicable;
1571f4a2713aSLionel Sambuc }
1572f4a2713aSLionel Sambuc if (DestType->isFunctionPointerType() ||
1573f4a2713aSLionel Sambuc DestType->isMemberFunctionPointerType()) {
1574f4a2713aSLionel Sambuc // Cannot cast direct function pointers.
1575f4a2713aSLionel Sambuc // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
1576f4a2713aSLionel Sambuc // T is the ultimate pointee of source and target type.
1577f4a2713aSLionel Sambuc if (!CStyle)
1578f4a2713aSLionel Sambuc msg = diag::err_bad_const_cast_dest;
1579f4a2713aSLionel Sambuc return TC_NotApplicable;
1580f4a2713aSLionel Sambuc }
1581f4a2713aSLionel Sambuc SrcType = Self.Context.getCanonicalType(SrcType);
1582f4a2713aSLionel Sambuc
1583f4a2713aSLionel Sambuc // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
1584f4a2713aSLionel Sambuc // completely equal.
1585f4a2713aSLionel Sambuc // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
1586f4a2713aSLionel Sambuc // in multi-level pointers may change, but the level count must be the same,
1587f4a2713aSLionel Sambuc // as must be the final pointee type.
1588f4a2713aSLionel Sambuc while (SrcType != DestType &&
1589f4a2713aSLionel Sambuc Self.Context.UnwrapSimilarPointerTypes(SrcType, DestType)) {
1590f4a2713aSLionel Sambuc Qualifiers SrcQuals, DestQuals;
1591f4a2713aSLionel Sambuc SrcType = Self.Context.getUnqualifiedArrayType(SrcType, SrcQuals);
1592f4a2713aSLionel Sambuc DestType = Self.Context.getUnqualifiedArrayType(DestType, DestQuals);
1593f4a2713aSLionel Sambuc
1594f4a2713aSLionel Sambuc // const_cast is permitted to strip cvr-qualifiers, only. Make sure that
1595f4a2713aSLionel Sambuc // the other qualifiers (e.g., address spaces) are identical.
1596f4a2713aSLionel Sambuc SrcQuals.removeCVRQualifiers();
1597f4a2713aSLionel Sambuc DestQuals.removeCVRQualifiers();
1598f4a2713aSLionel Sambuc if (SrcQuals != DestQuals)
1599f4a2713aSLionel Sambuc return TC_NotApplicable;
1600f4a2713aSLionel Sambuc }
1601f4a2713aSLionel Sambuc
1602f4a2713aSLionel Sambuc // Since we're dealing in canonical types, the remainder must be the same.
1603f4a2713aSLionel Sambuc if (SrcType != DestType)
1604f4a2713aSLionel Sambuc return TC_NotApplicable;
1605f4a2713aSLionel Sambuc
1606f4a2713aSLionel Sambuc if (NeedToMaterializeTemporary)
1607f4a2713aSLionel Sambuc // This is a const_cast from a class prvalue to an rvalue reference type.
1608f4a2713aSLionel Sambuc // Materialize a temporary to store the result of the conversion.
1609f4a2713aSLionel Sambuc SrcExpr = new (Self.Context) MaterializeTemporaryExpr(
1610*0a6a1f1dSLionel Sambuc SrcType, SrcExpr.get(), /*IsLValueReference*/ false);
1611f4a2713aSLionel Sambuc
1612f4a2713aSLionel Sambuc return TC_Success;
1613f4a2713aSLionel Sambuc }
1614f4a2713aSLionel Sambuc
1615f4a2713aSLionel Sambuc // Checks for undefined behavior in reinterpret_cast.
1616f4a2713aSLionel Sambuc // The cases that is checked for is:
1617f4a2713aSLionel Sambuc // *reinterpret_cast<T*>(&a)
1618f4a2713aSLionel Sambuc // reinterpret_cast<T&>(a)
1619f4a2713aSLionel Sambuc // where accessing 'a' as type 'T' will result in undefined behavior.
CheckCompatibleReinterpretCast(QualType SrcType,QualType DestType,bool IsDereference,SourceRange Range)1620f4a2713aSLionel Sambuc void Sema::CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType,
1621f4a2713aSLionel Sambuc bool IsDereference,
1622f4a2713aSLionel Sambuc SourceRange Range) {
1623f4a2713aSLionel Sambuc unsigned DiagID = IsDereference ?
1624f4a2713aSLionel Sambuc diag::warn_pointer_indirection_from_incompatible_type :
1625f4a2713aSLionel Sambuc diag::warn_undefined_reinterpret_cast;
1626f4a2713aSLionel Sambuc
1627*0a6a1f1dSLionel Sambuc if (Diags.isIgnored(DiagID, Range.getBegin()))
1628f4a2713aSLionel Sambuc return;
1629f4a2713aSLionel Sambuc
1630f4a2713aSLionel Sambuc QualType SrcTy, DestTy;
1631f4a2713aSLionel Sambuc if (IsDereference) {
1632f4a2713aSLionel Sambuc if (!SrcType->getAs<PointerType>() || !DestType->getAs<PointerType>()) {
1633f4a2713aSLionel Sambuc return;
1634f4a2713aSLionel Sambuc }
1635f4a2713aSLionel Sambuc SrcTy = SrcType->getPointeeType();
1636f4a2713aSLionel Sambuc DestTy = DestType->getPointeeType();
1637f4a2713aSLionel Sambuc } else {
1638f4a2713aSLionel Sambuc if (!DestType->getAs<ReferenceType>()) {
1639f4a2713aSLionel Sambuc return;
1640f4a2713aSLionel Sambuc }
1641f4a2713aSLionel Sambuc SrcTy = SrcType;
1642f4a2713aSLionel Sambuc DestTy = DestType->getPointeeType();
1643f4a2713aSLionel Sambuc }
1644f4a2713aSLionel Sambuc
1645f4a2713aSLionel Sambuc // Cast is compatible if the types are the same.
1646f4a2713aSLionel Sambuc if (Context.hasSameUnqualifiedType(DestTy, SrcTy)) {
1647f4a2713aSLionel Sambuc return;
1648f4a2713aSLionel Sambuc }
1649f4a2713aSLionel Sambuc // or one of the types is a char or void type
1650f4a2713aSLionel Sambuc if (DestTy->isAnyCharacterType() || DestTy->isVoidType() ||
1651f4a2713aSLionel Sambuc SrcTy->isAnyCharacterType() || SrcTy->isVoidType()) {
1652f4a2713aSLionel Sambuc return;
1653f4a2713aSLionel Sambuc }
1654f4a2713aSLionel Sambuc // or one of the types is a tag type.
1655f4a2713aSLionel Sambuc if (SrcTy->getAs<TagType>() || DestTy->getAs<TagType>()) {
1656f4a2713aSLionel Sambuc return;
1657f4a2713aSLionel Sambuc }
1658f4a2713aSLionel Sambuc
1659f4a2713aSLionel Sambuc // FIXME: Scoped enums?
1660f4a2713aSLionel Sambuc if ((SrcTy->isUnsignedIntegerType() && DestTy->isSignedIntegerType()) ||
1661f4a2713aSLionel Sambuc (SrcTy->isSignedIntegerType() && DestTy->isUnsignedIntegerType())) {
1662f4a2713aSLionel Sambuc if (Context.getTypeSize(DestTy) == Context.getTypeSize(SrcTy)) {
1663f4a2713aSLionel Sambuc return;
1664f4a2713aSLionel Sambuc }
1665f4a2713aSLionel Sambuc }
1666f4a2713aSLionel Sambuc
1667f4a2713aSLionel Sambuc Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range;
1668f4a2713aSLionel Sambuc }
1669f4a2713aSLionel Sambuc
DiagnoseCastOfObjCSEL(Sema & Self,const ExprResult & SrcExpr,QualType DestType)1670f4a2713aSLionel Sambuc static void DiagnoseCastOfObjCSEL(Sema &Self, const ExprResult &SrcExpr,
1671f4a2713aSLionel Sambuc QualType DestType) {
1672f4a2713aSLionel Sambuc QualType SrcType = SrcExpr.get()->getType();
1673f4a2713aSLionel Sambuc if (Self.Context.hasSameType(SrcType, DestType))
1674f4a2713aSLionel Sambuc return;
1675f4a2713aSLionel Sambuc if (const PointerType *SrcPtrTy = SrcType->getAs<PointerType>())
1676f4a2713aSLionel Sambuc if (SrcPtrTy->isObjCSelType()) {
1677f4a2713aSLionel Sambuc QualType DT = DestType;
1678f4a2713aSLionel Sambuc if (isa<PointerType>(DestType))
1679f4a2713aSLionel Sambuc DT = DestType->getPointeeType();
1680f4a2713aSLionel Sambuc if (!DT.getUnqualifiedType()->isVoidType())
1681f4a2713aSLionel Sambuc Self.Diag(SrcExpr.get()->getExprLoc(),
1682f4a2713aSLionel Sambuc diag::warn_cast_pointer_from_sel)
1683f4a2713aSLionel Sambuc << SrcType << DestType << SrcExpr.get()->getSourceRange();
1684f4a2713aSLionel Sambuc }
1685f4a2713aSLionel Sambuc }
1686f4a2713aSLionel Sambuc
checkIntToPointerCast(bool CStyle,SourceLocation Loc,const Expr * SrcExpr,QualType DestType,Sema & Self)1687f4a2713aSLionel Sambuc static void checkIntToPointerCast(bool CStyle, SourceLocation Loc,
1688f4a2713aSLionel Sambuc const Expr *SrcExpr, QualType DestType,
1689f4a2713aSLionel Sambuc Sema &Self) {
1690f4a2713aSLionel Sambuc QualType SrcType = SrcExpr->getType();
1691f4a2713aSLionel Sambuc
1692f4a2713aSLionel Sambuc // Not warning on reinterpret_cast, boolean, constant expressions, etc
1693f4a2713aSLionel Sambuc // are not explicit design choices, but consistent with GCC's behavior.
1694f4a2713aSLionel Sambuc // Feel free to modify them if you've reason/evidence for an alternative.
1695f4a2713aSLionel Sambuc if (CStyle && SrcType->isIntegralType(Self.Context)
1696f4a2713aSLionel Sambuc && !SrcType->isBooleanType()
1697f4a2713aSLionel Sambuc && !SrcType->isEnumeralType()
1698f4a2713aSLionel Sambuc && !SrcExpr->isIntegerConstantExpr(Self.Context)
1699f4a2713aSLionel Sambuc && Self.Context.getTypeSize(DestType) >
1700f4a2713aSLionel Sambuc Self.Context.getTypeSize(SrcType)) {
1701f4a2713aSLionel Sambuc // Separate between casts to void* and non-void* pointers.
1702f4a2713aSLionel Sambuc // Some APIs use (abuse) void* for something like a user context,
1703f4a2713aSLionel Sambuc // and often that value is an integer even if it isn't a pointer itself.
1704f4a2713aSLionel Sambuc // Having a separate warning flag allows users to control the warning
1705f4a2713aSLionel Sambuc // for their workflow.
1706f4a2713aSLionel Sambuc unsigned Diag = DestType->isVoidPointerType() ?
1707f4a2713aSLionel Sambuc diag::warn_int_to_void_pointer_cast
1708f4a2713aSLionel Sambuc : diag::warn_int_to_pointer_cast;
1709f4a2713aSLionel Sambuc Self.Diag(Loc, Diag) << SrcType << DestType;
1710f4a2713aSLionel Sambuc }
1711f4a2713aSLionel Sambuc }
1712f4a2713aSLionel Sambuc
TryReinterpretCast(Sema & Self,ExprResult & SrcExpr,QualType DestType,bool CStyle,const SourceRange & OpRange,unsigned & msg,CastKind & Kind)1713f4a2713aSLionel Sambuc static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr,
1714f4a2713aSLionel Sambuc QualType DestType, bool CStyle,
1715f4a2713aSLionel Sambuc const SourceRange &OpRange,
1716f4a2713aSLionel Sambuc unsigned &msg,
1717f4a2713aSLionel Sambuc CastKind &Kind) {
1718f4a2713aSLionel Sambuc bool IsLValueCast = false;
1719f4a2713aSLionel Sambuc
1720f4a2713aSLionel Sambuc DestType = Self.Context.getCanonicalType(DestType);
1721f4a2713aSLionel Sambuc QualType SrcType = SrcExpr.get()->getType();
1722f4a2713aSLionel Sambuc
1723f4a2713aSLionel Sambuc // Is the source an overloaded name? (i.e. &foo)
1724f4a2713aSLionel Sambuc // If so, reinterpret_cast can not help us here (13.4, p1, bullet 5) ...
1725f4a2713aSLionel Sambuc if (SrcType == Self.Context.OverloadTy) {
1726f4a2713aSLionel Sambuc // ... unless foo<int> resolves to an lvalue unambiguously.
1727f4a2713aSLionel Sambuc // TODO: what if this fails because of DiagnoseUseOfDecl or something
1728f4a2713aSLionel Sambuc // like it?
1729f4a2713aSLionel Sambuc ExprResult SingleFunctionExpr = SrcExpr;
1730f4a2713aSLionel Sambuc if (Self.ResolveAndFixSingleFunctionTemplateSpecialization(
1731f4a2713aSLionel Sambuc SingleFunctionExpr,
1732f4a2713aSLionel Sambuc Expr::getValueKindForType(DestType) == VK_RValue // Convert Fun to Ptr
1733f4a2713aSLionel Sambuc ) && SingleFunctionExpr.isUsable()) {
1734f4a2713aSLionel Sambuc SrcExpr = SingleFunctionExpr;
1735f4a2713aSLionel Sambuc SrcType = SrcExpr.get()->getType();
1736f4a2713aSLionel Sambuc } else {
1737f4a2713aSLionel Sambuc return TC_NotApplicable;
1738f4a2713aSLionel Sambuc }
1739f4a2713aSLionel Sambuc }
1740f4a2713aSLionel Sambuc
1741f4a2713aSLionel Sambuc if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
1742f4a2713aSLionel Sambuc if (!SrcExpr.get()->isGLValue()) {
1743f4a2713aSLionel Sambuc // Cannot cast non-glvalue to (lvalue or rvalue) reference type. See the
1744f4a2713aSLionel Sambuc // similar comment in const_cast.
1745f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_rvalue;
1746f4a2713aSLionel Sambuc return TC_NotApplicable;
1747f4a2713aSLionel Sambuc }
1748f4a2713aSLionel Sambuc
1749f4a2713aSLionel Sambuc if (!CStyle) {
1750f4a2713aSLionel Sambuc Self.CheckCompatibleReinterpretCast(SrcType, DestType,
1751f4a2713aSLionel Sambuc /*isDereference=*/false, OpRange);
1752f4a2713aSLionel Sambuc }
1753f4a2713aSLionel Sambuc
1754f4a2713aSLionel Sambuc // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
1755f4a2713aSLionel Sambuc // same effect as the conversion *reinterpret_cast<T*>(&x) with the
1756f4a2713aSLionel Sambuc // built-in & and * operators.
1757f4a2713aSLionel Sambuc
1758*0a6a1f1dSLionel Sambuc const char *inappropriate = nullptr;
1759f4a2713aSLionel Sambuc switch (SrcExpr.get()->getObjectKind()) {
1760f4a2713aSLionel Sambuc case OK_Ordinary:
1761f4a2713aSLionel Sambuc break;
1762f4a2713aSLionel Sambuc case OK_BitField: inappropriate = "bit-field"; break;
1763f4a2713aSLionel Sambuc case OK_VectorComponent: inappropriate = "vector element"; break;
1764f4a2713aSLionel Sambuc case OK_ObjCProperty: inappropriate = "property expression"; break;
1765f4a2713aSLionel Sambuc case OK_ObjCSubscript: inappropriate = "container subscripting expression";
1766f4a2713aSLionel Sambuc break;
1767f4a2713aSLionel Sambuc }
1768f4a2713aSLionel Sambuc if (inappropriate) {
1769f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_reference)
1770f4a2713aSLionel Sambuc << inappropriate << DestType
1771f4a2713aSLionel Sambuc << OpRange << SrcExpr.get()->getSourceRange();
1772f4a2713aSLionel Sambuc msg = 0; SrcExpr = ExprError();
1773f4a2713aSLionel Sambuc return TC_NotApplicable;
1774f4a2713aSLionel Sambuc }
1775f4a2713aSLionel Sambuc
1776f4a2713aSLionel Sambuc // This code does this transformation for the checked types.
1777f4a2713aSLionel Sambuc DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
1778f4a2713aSLionel Sambuc SrcType = Self.Context.getPointerType(SrcType);
1779f4a2713aSLionel Sambuc
1780f4a2713aSLionel Sambuc IsLValueCast = true;
1781f4a2713aSLionel Sambuc }
1782f4a2713aSLionel Sambuc
1783f4a2713aSLionel Sambuc // Canonicalize source for comparison.
1784f4a2713aSLionel Sambuc SrcType = Self.Context.getCanonicalType(SrcType);
1785f4a2713aSLionel Sambuc
1786f4a2713aSLionel Sambuc const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
1787f4a2713aSLionel Sambuc *SrcMemPtr = SrcType->getAs<MemberPointerType>();
1788f4a2713aSLionel Sambuc if (DestMemPtr && SrcMemPtr) {
1789f4a2713aSLionel Sambuc // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
1790f4a2713aSLionel Sambuc // can be explicitly converted to an rvalue of type "pointer to member
1791f4a2713aSLionel Sambuc // of Y of type T2" if T1 and T2 are both function types or both object
1792f4a2713aSLionel Sambuc // types.
1793f4a2713aSLionel Sambuc if (DestMemPtr->getPointeeType()->isFunctionType() !=
1794f4a2713aSLionel Sambuc SrcMemPtr->getPointeeType()->isFunctionType())
1795f4a2713aSLionel Sambuc return TC_NotApplicable;
1796f4a2713aSLionel Sambuc
1797f4a2713aSLionel Sambuc // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
1798f4a2713aSLionel Sambuc // constness.
1799f4a2713aSLionel Sambuc // A reinterpret_cast followed by a const_cast can, though, so in C-style,
1800f4a2713aSLionel Sambuc // we accept it.
1801f4a2713aSLionel Sambuc if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle,
1802f4a2713aSLionel Sambuc /*CheckObjCLifetime=*/CStyle)) {
1803f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_qualifiers_away;
1804f4a2713aSLionel Sambuc return TC_Failed;
1805f4a2713aSLionel Sambuc }
1806f4a2713aSLionel Sambuc
1807*0a6a1f1dSLionel Sambuc if (Self.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
1808*0a6a1f1dSLionel Sambuc // We need to determine the inheritance model that the class will use if
1809*0a6a1f1dSLionel Sambuc // haven't yet.
1810*0a6a1f1dSLionel Sambuc Self.RequireCompleteType(OpRange.getBegin(), SrcType, 0);
1811*0a6a1f1dSLionel Sambuc Self.RequireCompleteType(OpRange.getBegin(), DestType, 0);
1812*0a6a1f1dSLionel Sambuc }
1813*0a6a1f1dSLionel Sambuc
1814f4a2713aSLionel Sambuc // Don't allow casting between member pointers of different sizes.
1815f4a2713aSLionel Sambuc if (Self.Context.getTypeSize(DestMemPtr) !=
1816f4a2713aSLionel Sambuc Self.Context.getTypeSize(SrcMemPtr)) {
1817f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_member_pointer_size;
1818f4a2713aSLionel Sambuc return TC_Failed;
1819f4a2713aSLionel Sambuc }
1820f4a2713aSLionel Sambuc
1821f4a2713aSLionel Sambuc // A valid member pointer cast.
1822f4a2713aSLionel Sambuc assert(!IsLValueCast);
1823f4a2713aSLionel Sambuc Kind = CK_ReinterpretMemberPointer;
1824f4a2713aSLionel Sambuc return TC_Success;
1825f4a2713aSLionel Sambuc }
1826f4a2713aSLionel Sambuc
1827f4a2713aSLionel Sambuc // See below for the enumeral issue.
1828f4a2713aSLionel Sambuc if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) {
1829f4a2713aSLionel Sambuc // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
1830f4a2713aSLionel Sambuc // type large enough to hold it. A value of std::nullptr_t can be
1831f4a2713aSLionel Sambuc // converted to an integral type; the conversion has the same meaning
1832f4a2713aSLionel Sambuc // and validity as a conversion of (void*)0 to the integral type.
1833f4a2713aSLionel Sambuc if (Self.Context.getTypeSize(SrcType) >
1834f4a2713aSLionel Sambuc Self.Context.getTypeSize(DestType)) {
1835f4a2713aSLionel Sambuc msg = diag::err_bad_reinterpret_cast_small_int;
1836f4a2713aSLionel Sambuc return TC_Failed;
1837f4a2713aSLionel Sambuc }
1838f4a2713aSLionel Sambuc Kind = CK_PointerToIntegral;
1839f4a2713aSLionel Sambuc return TC_Success;
1840f4a2713aSLionel Sambuc }
1841f4a2713aSLionel Sambuc
1842f4a2713aSLionel Sambuc bool destIsVector = DestType->isVectorType();
1843f4a2713aSLionel Sambuc bool srcIsVector = SrcType->isVectorType();
1844f4a2713aSLionel Sambuc if (srcIsVector || destIsVector) {
1845f4a2713aSLionel Sambuc // FIXME: Should this also apply to floating point types?
1846f4a2713aSLionel Sambuc bool srcIsScalar = SrcType->isIntegralType(Self.Context);
1847f4a2713aSLionel Sambuc bool destIsScalar = DestType->isIntegralType(Self.Context);
1848f4a2713aSLionel Sambuc
1849f4a2713aSLionel Sambuc // Check if this is a cast between a vector and something else.
1850f4a2713aSLionel Sambuc if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
1851f4a2713aSLionel Sambuc !(srcIsVector && destIsVector))
1852f4a2713aSLionel Sambuc return TC_NotApplicable;
1853f4a2713aSLionel Sambuc
1854f4a2713aSLionel Sambuc // If both types have the same size, we can successfully cast.
1855f4a2713aSLionel Sambuc if (Self.Context.getTypeSize(SrcType)
1856f4a2713aSLionel Sambuc == Self.Context.getTypeSize(DestType)) {
1857f4a2713aSLionel Sambuc Kind = CK_BitCast;
1858f4a2713aSLionel Sambuc return TC_Success;
1859f4a2713aSLionel Sambuc }
1860f4a2713aSLionel Sambuc
1861f4a2713aSLionel Sambuc if (destIsScalar)
1862f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
1863f4a2713aSLionel Sambuc else if (srcIsScalar)
1864f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
1865f4a2713aSLionel Sambuc else
1866f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
1867f4a2713aSLionel Sambuc
1868f4a2713aSLionel Sambuc return TC_Failed;
1869f4a2713aSLionel Sambuc }
1870f4a2713aSLionel Sambuc
1871f4a2713aSLionel Sambuc if (SrcType == DestType) {
1872f4a2713aSLionel Sambuc // C++ 5.2.10p2 has a note that mentions that, subject to all other
1873f4a2713aSLionel Sambuc // restrictions, a cast to the same type is allowed so long as it does not
1874f4a2713aSLionel Sambuc // cast away constness. In C++98, the intent was not entirely clear here,
1875f4a2713aSLionel Sambuc // since all other paragraphs explicitly forbid casts to the same type.
1876f4a2713aSLionel Sambuc // C++11 clarifies this case with p2.
1877f4a2713aSLionel Sambuc //
1878f4a2713aSLionel Sambuc // The only allowed types are: integral, enumeration, pointer, or
1879f4a2713aSLionel Sambuc // pointer-to-member types. We also won't restrict Obj-C pointers either.
1880f4a2713aSLionel Sambuc Kind = CK_NoOp;
1881f4a2713aSLionel Sambuc TryCastResult Result = TC_NotApplicable;
1882f4a2713aSLionel Sambuc if (SrcType->isIntegralOrEnumerationType() ||
1883f4a2713aSLionel Sambuc SrcType->isAnyPointerType() ||
1884f4a2713aSLionel Sambuc SrcType->isMemberPointerType() ||
1885f4a2713aSLionel Sambuc SrcType->isBlockPointerType()) {
1886f4a2713aSLionel Sambuc Result = TC_Success;
1887f4a2713aSLionel Sambuc }
1888f4a2713aSLionel Sambuc return Result;
1889f4a2713aSLionel Sambuc }
1890f4a2713aSLionel Sambuc
1891f4a2713aSLionel Sambuc bool destIsPtr = DestType->isAnyPointerType() ||
1892f4a2713aSLionel Sambuc DestType->isBlockPointerType();
1893f4a2713aSLionel Sambuc bool srcIsPtr = SrcType->isAnyPointerType() ||
1894f4a2713aSLionel Sambuc SrcType->isBlockPointerType();
1895f4a2713aSLionel Sambuc if (!destIsPtr && !srcIsPtr) {
1896f4a2713aSLionel Sambuc // Except for std::nullptr_t->integer and lvalue->reference, which are
1897f4a2713aSLionel Sambuc // handled above, at least one of the two arguments must be a pointer.
1898f4a2713aSLionel Sambuc return TC_NotApplicable;
1899f4a2713aSLionel Sambuc }
1900f4a2713aSLionel Sambuc
1901f4a2713aSLionel Sambuc if (DestType->isIntegralType(Self.Context)) {
1902f4a2713aSLionel Sambuc assert(srcIsPtr && "One type must be a pointer");
1903f4a2713aSLionel Sambuc // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
1904f4a2713aSLionel Sambuc // type large enough to hold it; except in Microsoft mode, where the
1905f4a2713aSLionel Sambuc // integral type size doesn't matter (except we don't allow bool).
1906f4a2713aSLionel Sambuc bool MicrosoftException = Self.getLangOpts().MicrosoftExt &&
1907f4a2713aSLionel Sambuc !DestType->isBooleanType();
1908f4a2713aSLionel Sambuc if ((Self.Context.getTypeSize(SrcType) >
1909f4a2713aSLionel Sambuc Self.Context.getTypeSize(DestType)) &&
1910f4a2713aSLionel Sambuc !MicrosoftException) {
1911f4a2713aSLionel Sambuc msg = diag::err_bad_reinterpret_cast_small_int;
1912f4a2713aSLionel Sambuc return TC_Failed;
1913f4a2713aSLionel Sambuc }
1914f4a2713aSLionel Sambuc Kind = CK_PointerToIntegral;
1915f4a2713aSLionel Sambuc return TC_Success;
1916f4a2713aSLionel Sambuc }
1917f4a2713aSLionel Sambuc
1918f4a2713aSLionel Sambuc if (SrcType->isIntegralOrEnumerationType()) {
1919f4a2713aSLionel Sambuc assert(destIsPtr && "One type must be a pointer");
1920f4a2713aSLionel Sambuc checkIntToPointerCast(CStyle, OpRange.getBegin(), SrcExpr.get(), DestType,
1921f4a2713aSLionel Sambuc Self);
1922f4a2713aSLionel Sambuc // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
1923f4a2713aSLionel Sambuc // converted to a pointer.
1924f4a2713aSLionel Sambuc // C++ 5.2.10p9: [Note: ...a null pointer constant of integral type is not
1925f4a2713aSLionel Sambuc // necessarily converted to a null pointer value.]
1926f4a2713aSLionel Sambuc Kind = CK_IntegralToPointer;
1927f4a2713aSLionel Sambuc return TC_Success;
1928f4a2713aSLionel Sambuc }
1929f4a2713aSLionel Sambuc
1930f4a2713aSLionel Sambuc if (!destIsPtr || !srcIsPtr) {
1931f4a2713aSLionel Sambuc // With the valid non-pointer conversions out of the way, we can be even
1932f4a2713aSLionel Sambuc // more stringent.
1933f4a2713aSLionel Sambuc return TC_NotApplicable;
1934f4a2713aSLionel Sambuc }
1935f4a2713aSLionel Sambuc
1936f4a2713aSLionel Sambuc // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
1937f4a2713aSLionel Sambuc // The C-style cast operator can.
1938f4a2713aSLionel Sambuc if (CastsAwayConstness(Self, SrcType, DestType, /*CheckCVR=*/!CStyle,
1939f4a2713aSLionel Sambuc /*CheckObjCLifetime=*/CStyle)) {
1940f4a2713aSLionel Sambuc msg = diag::err_bad_cxx_cast_qualifiers_away;
1941f4a2713aSLionel Sambuc return TC_Failed;
1942f4a2713aSLionel Sambuc }
1943f4a2713aSLionel Sambuc
1944f4a2713aSLionel Sambuc // Cannot convert between block pointers and Objective-C object pointers.
1945f4a2713aSLionel Sambuc if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) ||
1946f4a2713aSLionel Sambuc (DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType()))
1947f4a2713aSLionel Sambuc return TC_NotApplicable;
1948f4a2713aSLionel Sambuc
1949f4a2713aSLionel Sambuc if (IsLValueCast) {
1950f4a2713aSLionel Sambuc Kind = CK_LValueBitCast;
1951f4a2713aSLionel Sambuc } else if (DestType->isObjCObjectPointerType()) {
1952f4a2713aSLionel Sambuc Kind = Self.PrepareCastToObjCObjectPointer(SrcExpr);
1953f4a2713aSLionel Sambuc } else if (DestType->isBlockPointerType()) {
1954f4a2713aSLionel Sambuc if (!SrcType->isBlockPointerType()) {
1955f4a2713aSLionel Sambuc Kind = CK_AnyPointerToBlockPointerCast;
1956f4a2713aSLionel Sambuc } else {
1957f4a2713aSLionel Sambuc Kind = CK_BitCast;
1958f4a2713aSLionel Sambuc }
1959f4a2713aSLionel Sambuc } else {
1960f4a2713aSLionel Sambuc Kind = CK_BitCast;
1961f4a2713aSLionel Sambuc }
1962f4a2713aSLionel Sambuc
1963f4a2713aSLionel Sambuc // Any pointer can be cast to an Objective-C pointer type with a C-style
1964f4a2713aSLionel Sambuc // cast.
1965f4a2713aSLionel Sambuc if (CStyle && DestType->isObjCObjectPointerType()) {
1966f4a2713aSLionel Sambuc return TC_Success;
1967f4a2713aSLionel Sambuc }
1968f4a2713aSLionel Sambuc if (CStyle)
1969f4a2713aSLionel Sambuc DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType);
1970f4a2713aSLionel Sambuc
1971f4a2713aSLionel Sambuc // Not casting away constness, so the only remaining check is for compatible
1972f4a2713aSLionel Sambuc // pointer categories.
1973f4a2713aSLionel Sambuc
1974f4a2713aSLionel Sambuc if (SrcType->isFunctionPointerType()) {
1975f4a2713aSLionel Sambuc if (DestType->isFunctionPointerType()) {
1976f4a2713aSLionel Sambuc // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
1977f4a2713aSLionel Sambuc // a pointer to a function of a different type.
1978f4a2713aSLionel Sambuc return TC_Success;
1979f4a2713aSLionel Sambuc }
1980f4a2713aSLionel Sambuc
1981f4a2713aSLionel Sambuc // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
1982f4a2713aSLionel Sambuc // an object type or vice versa is conditionally-supported.
1983f4a2713aSLionel Sambuc // Compilers support it in C++03 too, though, because it's necessary for
1984f4a2713aSLionel Sambuc // casting the return value of dlsym() and GetProcAddress().
1985f4a2713aSLionel Sambuc // FIXME: Conditionally-supported behavior should be configurable in the
1986f4a2713aSLionel Sambuc // TargetInfo or similar.
1987f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(),
1988f4a2713aSLionel Sambuc Self.getLangOpts().CPlusPlus11 ?
1989f4a2713aSLionel Sambuc diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj)
1990f4a2713aSLionel Sambuc << OpRange;
1991f4a2713aSLionel Sambuc return TC_Success;
1992f4a2713aSLionel Sambuc }
1993f4a2713aSLionel Sambuc
1994f4a2713aSLionel Sambuc if (DestType->isFunctionPointerType()) {
1995f4a2713aSLionel Sambuc // See above.
1996f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(),
1997f4a2713aSLionel Sambuc Self.getLangOpts().CPlusPlus11 ?
1998f4a2713aSLionel Sambuc diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj)
1999f4a2713aSLionel Sambuc << OpRange;
2000f4a2713aSLionel Sambuc return TC_Success;
2001f4a2713aSLionel Sambuc }
2002f4a2713aSLionel Sambuc
2003f4a2713aSLionel Sambuc // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
2004f4a2713aSLionel Sambuc // a pointer to an object of different type.
2005f4a2713aSLionel Sambuc // Void pointers are not specified, but supported by every compiler out there.
2006f4a2713aSLionel Sambuc // So we finish by allowing everything that remains - it's got to be two
2007f4a2713aSLionel Sambuc // object pointers.
2008f4a2713aSLionel Sambuc return TC_Success;
2009f4a2713aSLionel Sambuc }
2010f4a2713aSLionel Sambuc
CheckCXXCStyleCast(bool FunctionalStyle,bool ListInitialization)2011f4a2713aSLionel Sambuc void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
2012f4a2713aSLionel Sambuc bool ListInitialization) {
2013f4a2713aSLionel Sambuc // Handle placeholders.
2014f4a2713aSLionel Sambuc if (isPlaceholder()) {
2015f4a2713aSLionel Sambuc // C-style casts can resolve __unknown_any types.
2016f4a2713aSLionel Sambuc if (claimPlaceholder(BuiltinType::UnknownAny)) {
2017f4a2713aSLionel Sambuc SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType,
2018f4a2713aSLionel Sambuc SrcExpr.get(), Kind,
2019f4a2713aSLionel Sambuc ValueKind, BasePath);
2020f4a2713aSLionel Sambuc return;
2021f4a2713aSLionel Sambuc }
2022f4a2713aSLionel Sambuc
2023f4a2713aSLionel Sambuc checkNonOverloadPlaceholders();
2024f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2025f4a2713aSLionel Sambuc return;
2026f4a2713aSLionel Sambuc }
2027f4a2713aSLionel Sambuc
2028f4a2713aSLionel Sambuc // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
2029f4a2713aSLionel Sambuc // This test is outside everything else because it's the only case where
2030f4a2713aSLionel Sambuc // a non-lvalue-reference target type does not lead to decay.
2031f4a2713aSLionel Sambuc if (DestType->isVoidType()) {
2032f4a2713aSLionel Sambuc Kind = CK_ToVoid;
2033f4a2713aSLionel Sambuc
2034f4a2713aSLionel Sambuc if (claimPlaceholder(BuiltinType::Overload)) {
2035f4a2713aSLionel Sambuc Self.ResolveAndFixSingleFunctionTemplateSpecialization(
2036f4a2713aSLionel Sambuc SrcExpr, /* Decay Function to ptr */ false,
2037f4a2713aSLionel Sambuc /* Complain */ true, DestRange, DestType,
2038f4a2713aSLionel Sambuc diag::err_bad_cstyle_cast_overload);
2039f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2040f4a2713aSLionel Sambuc return;
2041f4a2713aSLionel Sambuc }
2042f4a2713aSLionel Sambuc
2043*0a6a1f1dSLionel Sambuc SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
2044f4a2713aSLionel Sambuc return;
2045f4a2713aSLionel Sambuc }
2046f4a2713aSLionel Sambuc
2047f4a2713aSLionel Sambuc // If the type is dependent, we won't do any other semantic analysis now.
2048f4a2713aSLionel Sambuc if (DestType->isDependentType() || SrcExpr.get()->isTypeDependent() ||
2049f4a2713aSLionel Sambuc SrcExpr.get()->isValueDependent()) {
2050f4a2713aSLionel Sambuc assert(Kind == CK_Dependent);
2051f4a2713aSLionel Sambuc return;
2052f4a2713aSLionel Sambuc }
2053f4a2713aSLionel Sambuc
2054f4a2713aSLionel Sambuc if (ValueKind == VK_RValue && !DestType->isRecordType() &&
2055f4a2713aSLionel Sambuc !isPlaceholder(BuiltinType::Overload)) {
2056*0a6a1f1dSLionel Sambuc SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
2057f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2058f4a2713aSLionel Sambuc return;
2059f4a2713aSLionel Sambuc }
2060f4a2713aSLionel Sambuc
2061f4a2713aSLionel Sambuc // AltiVec vector initialization with a single literal.
2062f4a2713aSLionel Sambuc if (const VectorType *vecTy = DestType->getAs<VectorType>())
2063f4a2713aSLionel Sambuc if (vecTy->getVectorKind() == VectorType::AltiVecVector
2064f4a2713aSLionel Sambuc && (SrcExpr.get()->getType()->isIntegerType()
2065f4a2713aSLionel Sambuc || SrcExpr.get()->getType()->isFloatingType())) {
2066f4a2713aSLionel Sambuc Kind = CK_VectorSplat;
2067f4a2713aSLionel Sambuc return;
2068f4a2713aSLionel Sambuc }
2069f4a2713aSLionel Sambuc
2070f4a2713aSLionel Sambuc // C++ [expr.cast]p5: The conversions performed by
2071f4a2713aSLionel Sambuc // - a const_cast,
2072f4a2713aSLionel Sambuc // - a static_cast,
2073f4a2713aSLionel Sambuc // - a static_cast followed by a const_cast,
2074f4a2713aSLionel Sambuc // - a reinterpret_cast, or
2075f4a2713aSLionel Sambuc // - a reinterpret_cast followed by a const_cast,
2076f4a2713aSLionel Sambuc // can be performed using the cast notation of explicit type conversion.
2077f4a2713aSLionel Sambuc // [...] If a conversion can be interpreted in more than one of the ways
2078f4a2713aSLionel Sambuc // listed above, the interpretation that appears first in the list is used,
2079f4a2713aSLionel Sambuc // even if a cast resulting from that interpretation is ill-formed.
2080f4a2713aSLionel Sambuc // In plain language, this means trying a const_cast ...
2081f4a2713aSLionel Sambuc unsigned msg = diag::err_bad_cxx_cast_generic;
2082f4a2713aSLionel Sambuc TryCastResult tcr = TryConstCast(Self, SrcExpr, DestType,
2083f4a2713aSLionel Sambuc /*CStyle*/true, msg);
2084f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2085f4a2713aSLionel Sambuc return;
2086f4a2713aSLionel Sambuc if (tcr == TC_Success)
2087f4a2713aSLionel Sambuc Kind = CK_NoOp;
2088f4a2713aSLionel Sambuc
2089f4a2713aSLionel Sambuc Sema::CheckedConversionKind CCK
2090f4a2713aSLionel Sambuc = FunctionalStyle? Sema::CCK_FunctionalCast
2091f4a2713aSLionel Sambuc : Sema::CCK_CStyleCast;
2092f4a2713aSLionel Sambuc if (tcr == TC_NotApplicable) {
2093f4a2713aSLionel Sambuc // ... or if that is not possible, a static_cast, ignoring const, ...
2094f4a2713aSLionel Sambuc tcr = TryStaticCast(Self, SrcExpr, DestType, CCK, OpRange,
2095f4a2713aSLionel Sambuc msg, Kind, BasePath, ListInitialization);
2096f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2097f4a2713aSLionel Sambuc return;
2098f4a2713aSLionel Sambuc
2099f4a2713aSLionel Sambuc if (tcr == TC_NotApplicable) {
2100f4a2713aSLionel Sambuc // ... and finally a reinterpret_cast, ignoring const.
2101f4a2713aSLionel Sambuc tcr = TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/true,
2102f4a2713aSLionel Sambuc OpRange, msg, Kind);
2103f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2104f4a2713aSLionel Sambuc return;
2105f4a2713aSLionel Sambuc }
2106f4a2713aSLionel Sambuc }
2107f4a2713aSLionel Sambuc
2108f4a2713aSLionel Sambuc if (Self.getLangOpts().ObjCAutoRefCount && tcr == TC_Success)
2109f4a2713aSLionel Sambuc checkObjCARCConversion(CCK);
2110f4a2713aSLionel Sambuc
2111f4a2713aSLionel Sambuc if (tcr != TC_Success && msg != 0) {
2112f4a2713aSLionel Sambuc if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
2113f4a2713aSLionel Sambuc DeclAccessPair Found;
2114f4a2713aSLionel Sambuc FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(),
2115f4a2713aSLionel Sambuc DestType,
2116f4a2713aSLionel Sambuc /*Complain*/ true,
2117f4a2713aSLionel Sambuc Found);
2118*0a6a1f1dSLionel Sambuc if (Fn) {
2119*0a6a1f1dSLionel Sambuc // If DestType is a function type (not to be confused with the function
2120*0a6a1f1dSLionel Sambuc // pointer type), it will be possible to resolve the function address,
2121*0a6a1f1dSLionel Sambuc // but the type cast should be considered as failure.
2122*0a6a1f1dSLionel Sambuc OverloadExpr *OE = OverloadExpr::find(SrcExpr.get()).Expression;
2123*0a6a1f1dSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_bad_cstyle_cast_overload)
2124*0a6a1f1dSLionel Sambuc << OE->getName() << DestType << OpRange
2125*0a6a1f1dSLionel Sambuc << OE->getQualifierLoc().getSourceRange();
2126*0a6a1f1dSLionel Sambuc Self.NoteAllOverloadCandidates(SrcExpr.get());
2127*0a6a1f1dSLionel Sambuc }
2128f4a2713aSLionel Sambuc } else {
2129f4a2713aSLionel Sambuc diagnoseBadCast(Self, msg, (FunctionalStyle ? CT_Functional : CT_CStyle),
2130f4a2713aSLionel Sambuc OpRange, SrcExpr.get(), DestType, ListInitialization);
2131f4a2713aSLionel Sambuc }
2132f4a2713aSLionel Sambuc } else if (Kind == CK_BitCast) {
2133f4a2713aSLionel Sambuc checkCastAlign();
2134f4a2713aSLionel Sambuc }
2135f4a2713aSLionel Sambuc
2136f4a2713aSLionel Sambuc // Clear out SrcExpr if there was a fatal error.
2137f4a2713aSLionel Sambuc if (tcr != TC_Success)
2138f4a2713aSLionel Sambuc SrcExpr = ExprError();
2139f4a2713aSLionel Sambuc }
2140f4a2713aSLionel Sambuc
2141f4a2713aSLionel Sambuc /// DiagnoseBadFunctionCast - Warn whenever a function call is cast to a
2142f4a2713aSLionel Sambuc /// non-matching type. Such as enum function call to int, int call to
2143f4a2713aSLionel Sambuc /// pointer; etc. Cast to 'void' is an exception.
DiagnoseBadFunctionCast(Sema & Self,const ExprResult & SrcExpr,QualType DestType)2144f4a2713aSLionel Sambuc static void DiagnoseBadFunctionCast(Sema &Self, const ExprResult &SrcExpr,
2145f4a2713aSLionel Sambuc QualType DestType) {
2146*0a6a1f1dSLionel Sambuc if (Self.Diags.isIgnored(diag::warn_bad_function_cast,
2147*0a6a1f1dSLionel Sambuc SrcExpr.get()->getExprLoc()))
2148f4a2713aSLionel Sambuc return;
2149f4a2713aSLionel Sambuc
2150f4a2713aSLionel Sambuc if (!isa<CallExpr>(SrcExpr.get()))
2151f4a2713aSLionel Sambuc return;
2152f4a2713aSLionel Sambuc
2153f4a2713aSLionel Sambuc QualType SrcType = SrcExpr.get()->getType();
2154f4a2713aSLionel Sambuc if (DestType.getUnqualifiedType()->isVoidType())
2155f4a2713aSLionel Sambuc return;
2156f4a2713aSLionel Sambuc if ((SrcType->isAnyPointerType() || SrcType->isBlockPointerType())
2157f4a2713aSLionel Sambuc && (DestType->isAnyPointerType() || DestType->isBlockPointerType()))
2158f4a2713aSLionel Sambuc return;
2159f4a2713aSLionel Sambuc if (SrcType->isIntegerType() && DestType->isIntegerType() &&
2160f4a2713aSLionel Sambuc (SrcType->isBooleanType() == DestType->isBooleanType()) &&
2161f4a2713aSLionel Sambuc (SrcType->isEnumeralType() == DestType->isEnumeralType()))
2162f4a2713aSLionel Sambuc return;
2163f4a2713aSLionel Sambuc if (SrcType->isRealFloatingType() && DestType->isRealFloatingType())
2164f4a2713aSLionel Sambuc return;
2165f4a2713aSLionel Sambuc if (SrcType->isEnumeralType() && DestType->isEnumeralType())
2166f4a2713aSLionel Sambuc return;
2167f4a2713aSLionel Sambuc if (SrcType->isComplexType() && DestType->isComplexType())
2168f4a2713aSLionel Sambuc return;
2169f4a2713aSLionel Sambuc if (SrcType->isComplexIntegerType() && DestType->isComplexIntegerType())
2170f4a2713aSLionel Sambuc return;
2171f4a2713aSLionel Sambuc
2172f4a2713aSLionel Sambuc Self.Diag(SrcExpr.get()->getExprLoc(),
2173f4a2713aSLionel Sambuc diag::warn_bad_function_cast)
2174f4a2713aSLionel Sambuc << SrcType << DestType << SrcExpr.get()->getSourceRange();
2175f4a2713aSLionel Sambuc }
2176f4a2713aSLionel Sambuc
2177f4a2713aSLionel Sambuc /// Check the semantics of a C-style cast operation, in C.
CheckCStyleCast()2178f4a2713aSLionel Sambuc void CastOperation::CheckCStyleCast() {
2179f4a2713aSLionel Sambuc assert(!Self.getLangOpts().CPlusPlus);
2180f4a2713aSLionel Sambuc
2181f4a2713aSLionel Sambuc // C-style casts can resolve __unknown_any types.
2182f4a2713aSLionel Sambuc if (claimPlaceholder(BuiltinType::UnknownAny)) {
2183f4a2713aSLionel Sambuc SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType,
2184f4a2713aSLionel Sambuc SrcExpr.get(), Kind,
2185f4a2713aSLionel Sambuc ValueKind, BasePath);
2186f4a2713aSLionel Sambuc return;
2187f4a2713aSLionel Sambuc }
2188f4a2713aSLionel Sambuc
2189f4a2713aSLionel Sambuc // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
2190f4a2713aSLionel Sambuc // type needs to be scalar.
2191f4a2713aSLionel Sambuc if (DestType->isVoidType()) {
2192f4a2713aSLionel Sambuc // We don't necessarily do lvalue-to-rvalue conversions on this.
2193*0a6a1f1dSLionel Sambuc SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
2194f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2195f4a2713aSLionel Sambuc return;
2196f4a2713aSLionel Sambuc
2197f4a2713aSLionel Sambuc // Cast to void allows any expr type.
2198f4a2713aSLionel Sambuc Kind = CK_ToVoid;
2199f4a2713aSLionel Sambuc return;
2200f4a2713aSLionel Sambuc }
2201f4a2713aSLionel Sambuc
2202*0a6a1f1dSLionel Sambuc SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
2203f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2204f4a2713aSLionel Sambuc return;
2205f4a2713aSLionel Sambuc QualType SrcType = SrcExpr.get()->getType();
2206f4a2713aSLionel Sambuc
2207f4a2713aSLionel Sambuc assert(!SrcType->isPlaceholderType());
2208f4a2713aSLionel Sambuc
2209*0a6a1f1dSLionel Sambuc // OpenCL v1 s6.5: Casting a pointer to address space A to a pointer to
2210*0a6a1f1dSLionel Sambuc // address space B is illegal.
2211*0a6a1f1dSLionel Sambuc if (Self.getLangOpts().OpenCL && DestType->isPointerType() &&
2212*0a6a1f1dSLionel Sambuc SrcType->isPointerType()) {
2213*0a6a1f1dSLionel Sambuc const PointerType *DestPtr = DestType->getAs<PointerType>();
2214*0a6a1f1dSLionel Sambuc if (!DestPtr->isAddressSpaceOverlapping(*SrcType->getAs<PointerType>())) {
2215*0a6a1f1dSLionel Sambuc Self.Diag(OpRange.getBegin(),
2216*0a6a1f1dSLionel Sambuc diag::err_typecheck_incompatible_address_space)
2217*0a6a1f1dSLionel Sambuc << SrcType << DestType << Sema::AA_Casting
2218*0a6a1f1dSLionel Sambuc << SrcExpr.get()->getSourceRange();
2219*0a6a1f1dSLionel Sambuc SrcExpr = ExprError();
2220*0a6a1f1dSLionel Sambuc return;
2221*0a6a1f1dSLionel Sambuc }
2222*0a6a1f1dSLionel Sambuc }
2223*0a6a1f1dSLionel Sambuc
2224f4a2713aSLionel Sambuc if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
2225f4a2713aSLionel Sambuc diag::err_typecheck_cast_to_incomplete)) {
2226f4a2713aSLionel Sambuc SrcExpr = ExprError();
2227f4a2713aSLionel Sambuc return;
2228f4a2713aSLionel Sambuc }
2229f4a2713aSLionel Sambuc
2230f4a2713aSLionel Sambuc if (!DestType->isScalarType() && !DestType->isVectorType()) {
2231f4a2713aSLionel Sambuc const RecordType *DestRecordTy = DestType->getAs<RecordType>();
2232f4a2713aSLionel Sambuc
2233f4a2713aSLionel Sambuc if (DestRecordTy && Self.Context.hasSameUnqualifiedType(DestType, SrcType)){
2234f4a2713aSLionel Sambuc // GCC struct/union extension: allow cast to self.
2235f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_nonscalar)
2236f4a2713aSLionel Sambuc << DestType << SrcExpr.get()->getSourceRange();
2237f4a2713aSLionel Sambuc Kind = CK_NoOp;
2238f4a2713aSLionel Sambuc return;
2239f4a2713aSLionel Sambuc }
2240f4a2713aSLionel Sambuc
2241f4a2713aSLionel Sambuc // GCC's cast to union extension.
2242f4a2713aSLionel Sambuc if (DestRecordTy && DestRecordTy->getDecl()->isUnion()) {
2243f4a2713aSLionel Sambuc RecordDecl *RD = DestRecordTy->getDecl();
2244f4a2713aSLionel Sambuc RecordDecl::field_iterator Field, FieldEnd;
2245f4a2713aSLionel Sambuc for (Field = RD->field_begin(), FieldEnd = RD->field_end();
2246f4a2713aSLionel Sambuc Field != FieldEnd; ++Field) {
2247f4a2713aSLionel Sambuc if (Self.Context.hasSameUnqualifiedType(Field->getType(), SrcType) &&
2248f4a2713aSLionel Sambuc !Field->isUnnamedBitfield()) {
2249f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_to_union)
2250f4a2713aSLionel Sambuc << SrcExpr.get()->getSourceRange();
2251f4a2713aSLionel Sambuc break;
2252f4a2713aSLionel Sambuc }
2253f4a2713aSLionel Sambuc }
2254f4a2713aSLionel Sambuc if (Field == FieldEnd) {
2255f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_typecheck_cast_to_union_no_type)
2256f4a2713aSLionel Sambuc << SrcType << SrcExpr.get()->getSourceRange();
2257f4a2713aSLionel Sambuc SrcExpr = ExprError();
2258f4a2713aSLionel Sambuc return;
2259f4a2713aSLionel Sambuc }
2260f4a2713aSLionel Sambuc Kind = CK_ToUnion;
2261f4a2713aSLionel Sambuc return;
2262f4a2713aSLionel Sambuc }
2263f4a2713aSLionel Sambuc
2264f4a2713aSLionel Sambuc // Reject any other conversions to non-scalar types.
2265f4a2713aSLionel Sambuc Self.Diag(OpRange.getBegin(), diag::err_typecheck_cond_expect_scalar)
2266f4a2713aSLionel Sambuc << DestType << SrcExpr.get()->getSourceRange();
2267f4a2713aSLionel Sambuc SrcExpr = ExprError();
2268f4a2713aSLionel Sambuc return;
2269f4a2713aSLionel Sambuc }
2270f4a2713aSLionel Sambuc
2271f4a2713aSLionel Sambuc // The type we're casting to is known to be a scalar or vector.
2272f4a2713aSLionel Sambuc
2273f4a2713aSLionel Sambuc // Require the operand to be a scalar or vector.
2274f4a2713aSLionel Sambuc if (!SrcType->isScalarType() && !SrcType->isVectorType()) {
2275f4a2713aSLionel Sambuc Self.Diag(SrcExpr.get()->getExprLoc(),
2276f4a2713aSLionel Sambuc diag::err_typecheck_expect_scalar_operand)
2277f4a2713aSLionel Sambuc << SrcType << SrcExpr.get()->getSourceRange();
2278f4a2713aSLionel Sambuc SrcExpr = ExprError();
2279f4a2713aSLionel Sambuc return;
2280f4a2713aSLionel Sambuc }
2281f4a2713aSLionel Sambuc
2282f4a2713aSLionel Sambuc if (DestType->isExtVectorType()) {
2283*0a6a1f1dSLionel Sambuc SrcExpr = Self.CheckExtVectorCast(OpRange, DestType, SrcExpr.get(), Kind);
2284f4a2713aSLionel Sambuc return;
2285f4a2713aSLionel Sambuc }
2286f4a2713aSLionel Sambuc
2287f4a2713aSLionel Sambuc if (const VectorType *DestVecTy = DestType->getAs<VectorType>()) {
2288f4a2713aSLionel Sambuc if (DestVecTy->getVectorKind() == VectorType::AltiVecVector &&
2289f4a2713aSLionel Sambuc (SrcType->isIntegerType() || SrcType->isFloatingType())) {
2290f4a2713aSLionel Sambuc Kind = CK_VectorSplat;
2291f4a2713aSLionel Sambuc } else if (Self.CheckVectorCast(OpRange, DestType, SrcType, Kind)) {
2292f4a2713aSLionel Sambuc SrcExpr = ExprError();
2293f4a2713aSLionel Sambuc }
2294f4a2713aSLionel Sambuc return;
2295f4a2713aSLionel Sambuc }
2296f4a2713aSLionel Sambuc
2297f4a2713aSLionel Sambuc if (SrcType->isVectorType()) {
2298f4a2713aSLionel Sambuc if (Self.CheckVectorCast(OpRange, SrcType, DestType, Kind))
2299f4a2713aSLionel Sambuc SrcExpr = ExprError();
2300f4a2713aSLionel Sambuc return;
2301f4a2713aSLionel Sambuc }
2302f4a2713aSLionel Sambuc
2303f4a2713aSLionel Sambuc // The source and target types are both scalars, i.e.
2304f4a2713aSLionel Sambuc // - arithmetic types (fundamental, enum, and complex)
2305f4a2713aSLionel Sambuc // - all kinds of pointers
2306f4a2713aSLionel Sambuc // Note that member pointers were filtered out with C++, above.
2307f4a2713aSLionel Sambuc
2308f4a2713aSLionel Sambuc if (isa<ObjCSelectorExpr>(SrcExpr.get())) {
2309f4a2713aSLionel Sambuc Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_selector_expr);
2310f4a2713aSLionel Sambuc SrcExpr = ExprError();
2311f4a2713aSLionel Sambuc return;
2312f4a2713aSLionel Sambuc }
2313f4a2713aSLionel Sambuc
2314f4a2713aSLionel Sambuc // If either type is a pointer, the other type has to be either an
2315f4a2713aSLionel Sambuc // integer or a pointer.
2316f4a2713aSLionel Sambuc if (!DestType->isArithmeticType()) {
2317f4a2713aSLionel Sambuc if (!SrcType->isIntegralType(Self.Context) && SrcType->isArithmeticType()) {
2318f4a2713aSLionel Sambuc Self.Diag(SrcExpr.get()->getExprLoc(),
2319f4a2713aSLionel Sambuc diag::err_cast_pointer_from_non_pointer_int)
2320f4a2713aSLionel Sambuc << SrcType << SrcExpr.get()->getSourceRange();
2321f4a2713aSLionel Sambuc SrcExpr = ExprError();
2322f4a2713aSLionel Sambuc return;
2323f4a2713aSLionel Sambuc }
2324f4a2713aSLionel Sambuc checkIntToPointerCast(/* CStyle */ true, OpRange.getBegin(), SrcExpr.get(),
2325f4a2713aSLionel Sambuc DestType, Self);
2326f4a2713aSLionel Sambuc } else if (!SrcType->isArithmeticType()) {
2327f4a2713aSLionel Sambuc if (!DestType->isIntegralType(Self.Context) &&
2328f4a2713aSLionel Sambuc DestType->isArithmeticType()) {
2329f4a2713aSLionel Sambuc Self.Diag(SrcExpr.get()->getLocStart(),
2330f4a2713aSLionel Sambuc diag::err_cast_pointer_to_non_pointer_int)
2331f4a2713aSLionel Sambuc << DestType << SrcExpr.get()->getSourceRange();
2332f4a2713aSLionel Sambuc SrcExpr = ExprError();
2333f4a2713aSLionel Sambuc return;
2334f4a2713aSLionel Sambuc }
2335f4a2713aSLionel Sambuc }
2336f4a2713aSLionel Sambuc
2337f4a2713aSLionel Sambuc if (Self.getLangOpts().OpenCL && !Self.getOpenCLOptions().cl_khr_fp16) {
2338f4a2713aSLionel Sambuc if (DestType->isHalfType()) {
2339f4a2713aSLionel Sambuc Self.Diag(SrcExpr.get()->getLocStart(), diag::err_opencl_cast_to_half)
2340f4a2713aSLionel Sambuc << DestType << SrcExpr.get()->getSourceRange();
2341f4a2713aSLionel Sambuc SrcExpr = ExprError();
2342f4a2713aSLionel Sambuc return;
2343f4a2713aSLionel Sambuc }
2344f4a2713aSLionel Sambuc }
2345f4a2713aSLionel Sambuc
2346f4a2713aSLionel Sambuc // ARC imposes extra restrictions on casts.
2347f4a2713aSLionel Sambuc if (Self.getLangOpts().ObjCAutoRefCount) {
2348f4a2713aSLionel Sambuc checkObjCARCConversion(Sema::CCK_CStyleCast);
2349f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2350f4a2713aSLionel Sambuc return;
2351f4a2713aSLionel Sambuc
2352f4a2713aSLionel Sambuc if (const PointerType *CastPtr = DestType->getAs<PointerType>()) {
2353f4a2713aSLionel Sambuc if (const PointerType *ExprPtr = SrcType->getAs<PointerType>()) {
2354f4a2713aSLionel Sambuc Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers();
2355f4a2713aSLionel Sambuc Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers();
2356f4a2713aSLionel Sambuc if (CastPtr->getPointeeType()->isObjCLifetimeType() &&
2357f4a2713aSLionel Sambuc ExprPtr->getPointeeType()->isObjCLifetimeType() &&
2358f4a2713aSLionel Sambuc !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) {
2359f4a2713aSLionel Sambuc Self.Diag(SrcExpr.get()->getLocStart(),
2360f4a2713aSLionel Sambuc diag::err_typecheck_incompatible_ownership)
2361f4a2713aSLionel Sambuc << SrcType << DestType << Sema::AA_Casting
2362f4a2713aSLionel Sambuc << SrcExpr.get()->getSourceRange();
2363f4a2713aSLionel Sambuc return;
2364f4a2713aSLionel Sambuc }
2365f4a2713aSLionel Sambuc }
2366f4a2713aSLionel Sambuc }
2367f4a2713aSLionel Sambuc else if (!Self.CheckObjCARCUnavailableWeakConversion(DestType, SrcType)) {
2368f4a2713aSLionel Sambuc Self.Diag(SrcExpr.get()->getLocStart(),
2369f4a2713aSLionel Sambuc diag::err_arc_convesion_of_weak_unavailable)
2370f4a2713aSLionel Sambuc << 1 << SrcType << DestType << SrcExpr.get()->getSourceRange();
2371f4a2713aSLionel Sambuc SrcExpr = ExprError();
2372f4a2713aSLionel Sambuc return;
2373f4a2713aSLionel Sambuc }
2374f4a2713aSLionel Sambuc }
2375*0a6a1f1dSLionel Sambuc
2376f4a2713aSLionel Sambuc DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType);
2377f4a2713aSLionel Sambuc DiagnoseBadFunctionCast(Self, SrcExpr, DestType);
2378f4a2713aSLionel Sambuc Kind = Self.PrepareScalarCast(SrcExpr, DestType);
2379f4a2713aSLionel Sambuc if (SrcExpr.isInvalid())
2380f4a2713aSLionel Sambuc return;
2381f4a2713aSLionel Sambuc
2382f4a2713aSLionel Sambuc if (Kind == CK_BitCast)
2383f4a2713aSLionel Sambuc checkCastAlign();
2384*0a6a1f1dSLionel Sambuc
2385*0a6a1f1dSLionel Sambuc // -Wcast-qual
2386*0a6a1f1dSLionel Sambuc QualType TheOffendingSrcType, TheOffendingDestType;
2387*0a6a1f1dSLionel Sambuc Qualifiers CastAwayQualifiers;
2388*0a6a1f1dSLionel Sambuc if (SrcType->isAnyPointerType() && DestType->isAnyPointerType() &&
2389*0a6a1f1dSLionel Sambuc CastsAwayConstness(Self, SrcType, DestType, true, false,
2390*0a6a1f1dSLionel Sambuc &TheOffendingSrcType, &TheOffendingDestType,
2391*0a6a1f1dSLionel Sambuc &CastAwayQualifiers)) {
2392*0a6a1f1dSLionel Sambuc int qualifiers = -1;
2393*0a6a1f1dSLionel Sambuc if (CastAwayQualifiers.hasConst() && CastAwayQualifiers.hasVolatile()) {
2394*0a6a1f1dSLionel Sambuc qualifiers = 0;
2395*0a6a1f1dSLionel Sambuc } else if (CastAwayQualifiers.hasConst()) {
2396*0a6a1f1dSLionel Sambuc qualifiers = 1;
2397*0a6a1f1dSLionel Sambuc } else if (CastAwayQualifiers.hasVolatile()) {
2398*0a6a1f1dSLionel Sambuc qualifiers = 2;
2399*0a6a1f1dSLionel Sambuc }
2400*0a6a1f1dSLionel Sambuc // This is a variant of int **x; const int **y = (const int **)x;
2401*0a6a1f1dSLionel Sambuc if (qualifiers == -1)
2402*0a6a1f1dSLionel Sambuc Self.Diag(SrcExpr.get()->getLocStart(), diag::warn_cast_qual2) <<
2403*0a6a1f1dSLionel Sambuc SrcType << DestType;
2404*0a6a1f1dSLionel Sambuc else
2405*0a6a1f1dSLionel Sambuc Self.Diag(SrcExpr.get()->getLocStart(), diag::warn_cast_qual) <<
2406*0a6a1f1dSLionel Sambuc TheOffendingSrcType << TheOffendingDestType << qualifiers;
2407*0a6a1f1dSLionel Sambuc }
2408f4a2713aSLionel Sambuc }
2409f4a2713aSLionel Sambuc
BuildCStyleCastExpr(SourceLocation LPLoc,TypeSourceInfo * CastTypeInfo,SourceLocation RPLoc,Expr * CastExpr)2410f4a2713aSLionel Sambuc ExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc,
2411f4a2713aSLionel Sambuc TypeSourceInfo *CastTypeInfo,
2412f4a2713aSLionel Sambuc SourceLocation RPLoc,
2413f4a2713aSLionel Sambuc Expr *CastExpr) {
2414f4a2713aSLionel Sambuc CastOperation Op(*this, CastTypeInfo->getType(), CastExpr);
2415f4a2713aSLionel Sambuc Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange();
2416f4a2713aSLionel Sambuc Op.OpRange = SourceRange(LPLoc, CastExpr->getLocEnd());
2417f4a2713aSLionel Sambuc
2418f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus) {
2419f4a2713aSLionel Sambuc Op.CheckCXXCStyleCast(/*FunctionalStyle=*/ false,
2420f4a2713aSLionel Sambuc isa<InitListExpr>(CastExpr));
2421f4a2713aSLionel Sambuc } else {
2422f4a2713aSLionel Sambuc Op.CheckCStyleCast();
2423f4a2713aSLionel Sambuc }
2424f4a2713aSLionel Sambuc
2425f4a2713aSLionel Sambuc if (Op.SrcExpr.isInvalid())
2426f4a2713aSLionel Sambuc return ExprError();
2427f4a2713aSLionel Sambuc
2428f4a2713aSLionel Sambuc return Op.complete(CStyleCastExpr::Create(Context, Op.ResultType,
2429*0a6a1f1dSLionel Sambuc Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
2430f4a2713aSLionel Sambuc &Op.BasePath, CastTypeInfo, LPLoc, RPLoc));
2431f4a2713aSLionel Sambuc }
2432f4a2713aSLionel Sambuc
BuildCXXFunctionalCastExpr(TypeSourceInfo * CastTypeInfo,SourceLocation LPLoc,Expr * CastExpr,SourceLocation RPLoc)2433f4a2713aSLionel Sambuc ExprResult Sema::BuildCXXFunctionalCastExpr(TypeSourceInfo *CastTypeInfo,
2434f4a2713aSLionel Sambuc SourceLocation LPLoc,
2435f4a2713aSLionel Sambuc Expr *CastExpr,
2436f4a2713aSLionel Sambuc SourceLocation RPLoc) {
2437f4a2713aSLionel Sambuc assert(LPLoc.isValid() && "List-initialization shouldn't get here.");
2438f4a2713aSLionel Sambuc CastOperation Op(*this, CastTypeInfo->getType(), CastExpr);
2439f4a2713aSLionel Sambuc Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange();
2440f4a2713aSLionel Sambuc Op.OpRange = SourceRange(Op.DestRange.getBegin(), CastExpr->getLocEnd());
2441f4a2713aSLionel Sambuc
2442f4a2713aSLionel Sambuc Op.CheckCXXCStyleCast(/*FunctionalStyle=*/true, /*ListInit=*/false);
2443f4a2713aSLionel Sambuc if (Op.SrcExpr.isInvalid())
2444f4a2713aSLionel Sambuc return ExprError();
2445f4a2713aSLionel Sambuc
2446f4a2713aSLionel Sambuc if (CXXConstructExpr *ConstructExpr = dyn_cast<CXXConstructExpr>(Op.SrcExpr.get()))
2447f4a2713aSLionel Sambuc ConstructExpr->setParenOrBraceRange(SourceRange(LPLoc, RPLoc));
2448f4a2713aSLionel Sambuc
2449f4a2713aSLionel Sambuc return Op.complete(CXXFunctionalCastExpr::Create(Context, Op.ResultType,
2450f4a2713aSLionel Sambuc Op.ValueKind, CastTypeInfo, Op.Kind,
2451*0a6a1f1dSLionel Sambuc Op.SrcExpr.get(), &Op.BasePath, LPLoc, RPLoc));
2452f4a2713aSLionel Sambuc }
2453