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