xref: /minix3/external/bsd/llvm/dist/clang/lib/AST/Expr.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the Expr class and subclasses.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/AST/APValue.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16f4a2713aSLionel Sambuc #include "clang/AST/Attr.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
19f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
20f4a2713aSLionel Sambuc #include "clang/AST/EvaluatedExprVisitor.h"
21f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
22f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
23f4a2713aSLionel Sambuc #include "clang/AST/Mangle.h"
24f4a2713aSLionel Sambuc #include "clang/AST/RecordLayout.h"
25f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
26f4a2713aSLionel Sambuc #include "clang/Basic/Builtins.h"
27f4a2713aSLionel Sambuc #include "clang/Basic/CharInfo.h"
28f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
29f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
30f4a2713aSLionel Sambuc #include "clang/Lex/Lexer.h"
31f4a2713aSLionel Sambuc #include "clang/Lex/LiteralSupport.h"
32f4a2713aSLionel Sambuc #include "clang/Sema/SemaDiagnostic.h"
33f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
34f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
35f4a2713aSLionel Sambuc #include <algorithm>
36f4a2713aSLionel Sambuc #include <cstring>
37f4a2713aSLionel Sambuc using namespace clang;
38f4a2713aSLionel Sambuc 
getBestDynamicClassType() const39f4a2713aSLionel Sambuc const CXXRecordDecl *Expr::getBestDynamicClassType() const {
40f4a2713aSLionel Sambuc   const Expr *E = ignoreParenBaseCasts();
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc   QualType DerivedType = E->getType();
43f4a2713aSLionel Sambuc   if (const PointerType *PTy = DerivedType->getAs<PointerType>())
44f4a2713aSLionel Sambuc     DerivedType = PTy->getPointeeType();
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc   if (DerivedType->isDependentType())
47*0a6a1f1dSLionel Sambuc     return nullptr;
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc   const RecordType *Ty = DerivedType->castAs<RecordType>();
50f4a2713aSLionel Sambuc   Decl *D = Ty->getDecl();
51f4a2713aSLionel Sambuc   return cast<CXXRecordDecl>(D);
52f4a2713aSLionel Sambuc }
53f4a2713aSLionel Sambuc 
skipRValueSubobjectAdjustments(SmallVectorImpl<const Expr * > & CommaLHSs,SmallVectorImpl<SubobjectAdjustment> & Adjustments) const54f4a2713aSLionel Sambuc const Expr *Expr::skipRValueSubobjectAdjustments(
55f4a2713aSLionel Sambuc     SmallVectorImpl<const Expr *> &CommaLHSs,
56f4a2713aSLionel Sambuc     SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
57f4a2713aSLionel Sambuc   const Expr *E = this;
58f4a2713aSLionel Sambuc   while (true) {
59f4a2713aSLionel Sambuc     E = E->IgnoreParens();
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc     if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
62f4a2713aSLionel Sambuc       if ((CE->getCastKind() == CK_DerivedToBase ||
63f4a2713aSLionel Sambuc            CE->getCastKind() == CK_UncheckedDerivedToBase) &&
64f4a2713aSLionel Sambuc           E->getType()->isRecordType()) {
65f4a2713aSLionel Sambuc         E = CE->getSubExpr();
66f4a2713aSLionel Sambuc         CXXRecordDecl *Derived
67f4a2713aSLionel Sambuc           = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
68f4a2713aSLionel Sambuc         Adjustments.push_back(SubobjectAdjustment(CE, Derived));
69f4a2713aSLionel Sambuc         continue;
70f4a2713aSLionel Sambuc       }
71f4a2713aSLionel Sambuc 
72f4a2713aSLionel Sambuc       if (CE->getCastKind() == CK_NoOp) {
73f4a2713aSLionel Sambuc         E = CE->getSubExpr();
74f4a2713aSLionel Sambuc         continue;
75f4a2713aSLionel Sambuc       }
76f4a2713aSLionel Sambuc     } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
77f4a2713aSLionel Sambuc       if (!ME->isArrow()) {
78f4a2713aSLionel Sambuc         assert(ME->getBase()->getType()->isRecordType());
79f4a2713aSLionel Sambuc         if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
80f4a2713aSLionel Sambuc           if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
81f4a2713aSLionel Sambuc             E = ME->getBase();
82f4a2713aSLionel Sambuc             Adjustments.push_back(SubobjectAdjustment(Field));
83f4a2713aSLionel Sambuc             continue;
84f4a2713aSLionel Sambuc           }
85f4a2713aSLionel Sambuc         }
86f4a2713aSLionel Sambuc       }
87f4a2713aSLionel Sambuc     } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
88f4a2713aSLionel Sambuc       if (BO->isPtrMemOp()) {
89f4a2713aSLionel Sambuc         assert(BO->getRHS()->isRValue());
90f4a2713aSLionel Sambuc         E = BO->getLHS();
91f4a2713aSLionel Sambuc         const MemberPointerType *MPT =
92f4a2713aSLionel Sambuc           BO->getRHS()->getType()->getAs<MemberPointerType>();
93f4a2713aSLionel Sambuc         Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
94f4a2713aSLionel Sambuc         continue;
95f4a2713aSLionel Sambuc       } else if (BO->getOpcode() == BO_Comma) {
96f4a2713aSLionel Sambuc         CommaLHSs.push_back(BO->getLHS());
97f4a2713aSLionel Sambuc         E = BO->getRHS();
98f4a2713aSLionel Sambuc         continue;
99f4a2713aSLionel Sambuc       }
100f4a2713aSLionel Sambuc     }
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc     // Nothing changed.
103f4a2713aSLionel Sambuc     break;
104f4a2713aSLionel Sambuc   }
105f4a2713aSLionel Sambuc   return E;
106f4a2713aSLionel Sambuc }
107f4a2713aSLionel Sambuc 
108f4a2713aSLionel Sambuc /// isKnownToHaveBooleanValue - Return true if this is an integer expression
109f4a2713aSLionel Sambuc /// that is known to return 0 or 1.  This happens for _Bool/bool expressions
110f4a2713aSLionel Sambuc /// but also int expressions which are produced by things like comparisons in
111f4a2713aSLionel Sambuc /// C.
isKnownToHaveBooleanValue() const112f4a2713aSLionel Sambuc bool Expr::isKnownToHaveBooleanValue() const {
113f4a2713aSLionel Sambuc   const Expr *E = IgnoreParens();
114f4a2713aSLionel Sambuc 
115f4a2713aSLionel Sambuc   // If this value has _Bool type, it is obvious 0/1.
116f4a2713aSLionel Sambuc   if (E->getType()->isBooleanType()) return true;
117f4a2713aSLionel Sambuc   // If this is a non-scalar-integer type, we don't care enough to try.
118f4a2713aSLionel Sambuc   if (!E->getType()->isIntegralOrEnumerationType()) return false;
119f4a2713aSLionel Sambuc 
120f4a2713aSLionel Sambuc   if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
121f4a2713aSLionel Sambuc     switch (UO->getOpcode()) {
122f4a2713aSLionel Sambuc     case UO_Plus:
123f4a2713aSLionel Sambuc       return UO->getSubExpr()->isKnownToHaveBooleanValue();
124*0a6a1f1dSLionel Sambuc     case UO_LNot:
125*0a6a1f1dSLionel Sambuc       return true;
126f4a2713aSLionel Sambuc     default:
127f4a2713aSLionel Sambuc       return false;
128f4a2713aSLionel Sambuc     }
129f4a2713aSLionel Sambuc   }
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc   // Only look through implicit casts.  If the user writes
132f4a2713aSLionel Sambuc   // '(int) (a && b)' treat it as an arbitrary int.
133f4a2713aSLionel Sambuc   if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
134f4a2713aSLionel Sambuc     return CE->getSubExpr()->isKnownToHaveBooleanValue();
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc   if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
137f4a2713aSLionel Sambuc     switch (BO->getOpcode()) {
138f4a2713aSLionel Sambuc     default: return false;
139f4a2713aSLionel Sambuc     case BO_LT:   // Relational operators.
140f4a2713aSLionel Sambuc     case BO_GT:
141f4a2713aSLionel Sambuc     case BO_LE:
142f4a2713aSLionel Sambuc     case BO_GE:
143f4a2713aSLionel Sambuc     case BO_EQ:   // Equality operators.
144f4a2713aSLionel Sambuc     case BO_NE:
145f4a2713aSLionel Sambuc     case BO_LAnd: // AND operator.
146f4a2713aSLionel Sambuc     case BO_LOr:  // Logical OR operator.
147f4a2713aSLionel Sambuc       return true;
148f4a2713aSLionel Sambuc 
149f4a2713aSLionel Sambuc     case BO_And:  // Bitwise AND operator.
150f4a2713aSLionel Sambuc     case BO_Xor:  // Bitwise XOR operator.
151f4a2713aSLionel Sambuc     case BO_Or:   // Bitwise OR operator.
152f4a2713aSLionel Sambuc       // Handle things like (x==2)|(y==12).
153f4a2713aSLionel Sambuc       return BO->getLHS()->isKnownToHaveBooleanValue() &&
154f4a2713aSLionel Sambuc              BO->getRHS()->isKnownToHaveBooleanValue();
155f4a2713aSLionel Sambuc 
156f4a2713aSLionel Sambuc     case BO_Comma:
157f4a2713aSLionel Sambuc     case BO_Assign:
158f4a2713aSLionel Sambuc       return BO->getRHS()->isKnownToHaveBooleanValue();
159f4a2713aSLionel Sambuc     }
160f4a2713aSLionel Sambuc   }
161f4a2713aSLionel Sambuc 
162f4a2713aSLionel Sambuc   if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
163f4a2713aSLionel Sambuc     return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
164f4a2713aSLionel Sambuc            CO->getFalseExpr()->isKnownToHaveBooleanValue();
165f4a2713aSLionel Sambuc 
166f4a2713aSLionel Sambuc   return false;
167f4a2713aSLionel Sambuc }
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc // Amusing macro metaprogramming hack: check whether a class provides
170f4a2713aSLionel Sambuc // a more specific implementation of getExprLoc().
171f4a2713aSLionel Sambuc //
172f4a2713aSLionel Sambuc // See also Stmt.cpp:{getLocStart(),getLocEnd()}.
173f4a2713aSLionel Sambuc namespace {
174f4a2713aSLionel Sambuc   /// This implementation is used when a class provides a custom
175f4a2713aSLionel Sambuc   /// implementation of getExprLoc.
176f4a2713aSLionel Sambuc   template <class E, class T>
getExprLocImpl(const Expr * expr,SourceLocation (T::* v)()const)177f4a2713aSLionel Sambuc   SourceLocation getExprLocImpl(const Expr *expr,
178f4a2713aSLionel Sambuc                                 SourceLocation (T::*v)() const) {
179f4a2713aSLionel Sambuc     return static_cast<const E*>(expr)->getExprLoc();
180f4a2713aSLionel Sambuc   }
181f4a2713aSLionel Sambuc 
182f4a2713aSLionel Sambuc   /// This implementation is used when a class doesn't provide
183f4a2713aSLionel Sambuc   /// a custom implementation of getExprLoc.  Overload resolution
184f4a2713aSLionel Sambuc   /// should pick it over the implementation above because it's
185f4a2713aSLionel Sambuc   /// more specialized according to function template partial ordering.
186f4a2713aSLionel Sambuc   template <class E>
getExprLocImpl(const Expr * expr,SourceLocation (Expr::* v)()const)187f4a2713aSLionel Sambuc   SourceLocation getExprLocImpl(const Expr *expr,
188f4a2713aSLionel Sambuc                                 SourceLocation (Expr::*v)() const) {
189f4a2713aSLionel Sambuc     return static_cast<const E*>(expr)->getLocStart();
190f4a2713aSLionel Sambuc   }
191f4a2713aSLionel Sambuc }
192f4a2713aSLionel Sambuc 
getExprLoc() const193f4a2713aSLionel Sambuc SourceLocation Expr::getExprLoc() const {
194f4a2713aSLionel Sambuc   switch (getStmtClass()) {
195f4a2713aSLionel Sambuc   case Stmt::NoStmtClass: llvm_unreachable("statement without class");
196f4a2713aSLionel Sambuc #define ABSTRACT_STMT(type)
197f4a2713aSLionel Sambuc #define STMT(type, base) \
198*0a6a1f1dSLionel Sambuc   case Stmt::type##Class: break;
199f4a2713aSLionel Sambuc #define EXPR(type, base) \
200f4a2713aSLionel Sambuc   case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
201f4a2713aSLionel Sambuc #include "clang/AST/StmtNodes.inc"
202f4a2713aSLionel Sambuc   }
203*0a6a1f1dSLionel Sambuc   llvm_unreachable("unknown expression kind");
204f4a2713aSLionel Sambuc }
205f4a2713aSLionel Sambuc 
206f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
207f4a2713aSLionel Sambuc // Primary Expressions.
208f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
209f4a2713aSLionel Sambuc 
210f4a2713aSLionel Sambuc /// \brief Compute the type-, value-, and instantiation-dependence of a
211f4a2713aSLionel Sambuc /// declaration reference
212f4a2713aSLionel Sambuc /// based on the declaration being referenced.
computeDeclRefDependence(const ASTContext & Ctx,NamedDecl * D,QualType T,bool & TypeDependent,bool & ValueDependent,bool & InstantiationDependent)213f4a2713aSLionel Sambuc static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
214f4a2713aSLionel Sambuc                                      QualType T, bool &TypeDependent,
215f4a2713aSLionel Sambuc                                      bool &ValueDependent,
216f4a2713aSLionel Sambuc                                      bool &InstantiationDependent) {
217f4a2713aSLionel Sambuc   TypeDependent = false;
218f4a2713aSLionel Sambuc   ValueDependent = false;
219f4a2713aSLionel Sambuc   InstantiationDependent = false;
220f4a2713aSLionel Sambuc 
221f4a2713aSLionel Sambuc   // (TD) C++ [temp.dep.expr]p3:
222f4a2713aSLionel Sambuc   //   An id-expression is type-dependent if it contains:
223f4a2713aSLionel Sambuc   //
224f4a2713aSLionel Sambuc   // and
225f4a2713aSLionel Sambuc   //
226f4a2713aSLionel Sambuc   // (VD) C++ [temp.dep.constexpr]p2:
227f4a2713aSLionel Sambuc   //  An identifier is value-dependent if it is:
228f4a2713aSLionel Sambuc 
229f4a2713aSLionel Sambuc   //  (TD)  - an identifier that was declared with dependent type
230f4a2713aSLionel Sambuc   //  (VD)  - a name declared with a dependent type,
231f4a2713aSLionel Sambuc   if (T->isDependentType()) {
232f4a2713aSLionel Sambuc     TypeDependent = true;
233f4a2713aSLionel Sambuc     ValueDependent = true;
234f4a2713aSLionel Sambuc     InstantiationDependent = true;
235f4a2713aSLionel Sambuc     return;
236f4a2713aSLionel Sambuc   } else if (T->isInstantiationDependentType()) {
237f4a2713aSLionel Sambuc     InstantiationDependent = true;
238f4a2713aSLionel Sambuc   }
239f4a2713aSLionel Sambuc 
240f4a2713aSLionel Sambuc   //  (TD)  - a conversion-function-id that specifies a dependent type
241f4a2713aSLionel Sambuc   if (D->getDeclName().getNameKind()
242f4a2713aSLionel Sambuc                                 == DeclarationName::CXXConversionFunctionName) {
243f4a2713aSLionel Sambuc     QualType T = D->getDeclName().getCXXNameType();
244f4a2713aSLionel Sambuc     if (T->isDependentType()) {
245f4a2713aSLionel Sambuc       TypeDependent = true;
246f4a2713aSLionel Sambuc       ValueDependent = true;
247f4a2713aSLionel Sambuc       InstantiationDependent = true;
248f4a2713aSLionel Sambuc       return;
249f4a2713aSLionel Sambuc     }
250f4a2713aSLionel Sambuc 
251f4a2713aSLionel Sambuc     if (T->isInstantiationDependentType())
252f4a2713aSLionel Sambuc       InstantiationDependent = true;
253f4a2713aSLionel Sambuc   }
254f4a2713aSLionel Sambuc 
255f4a2713aSLionel Sambuc   //  (VD)  - the name of a non-type template parameter,
256f4a2713aSLionel Sambuc   if (isa<NonTypeTemplateParmDecl>(D)) {
257f4a2713aSLionel Sambuc     ValueDependent = true;
258f4a2713aSLionel Sambuc     InstantiationDependent = true;
259f4a2713aSLionel Sambuc     return;
260f4a2713aSLionel Sambuc   }
261f4a2713aSLionel Sambuc 
262f4a2713aSLionel Sambuc   //  (VD) - a constant with integral or enumeration type and is
263f4a2713aSLionel Sambuc   //         initialized with an expression that is value-dependent.
264f4a2713aSLionel Sambuc   //  (VD) - a constant with literal type and is initialized with an
265f4a2713aSLionel Sambuc   //         expression that is value-dependent [C++11].
266f4a2713aSLionel Sambuc   //  (VD) - FIXME: Missing from the standard:
267f4a2713aSLionel Sambuc   //       -  an entity with reference type and is initialized with an
268f4a2713aSLionel Sambuc   //          expression that is value-dependent [C++11]
269f4a2713aSLionel Sambuc   if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
270f4a2713aSLionel Sambuc     if ((Ctx.getLangOpts().CPlusPlus11 ?
271f4a2713aSLionel Sambuc            Var->getType()->isLiteralType(Ctx) :
272f4a2713aSLionel Sambuc            Var->getType()->isIntegralOrEnumerationType()) &&
273f4a2713aSLionel Sambuc         (Var->getType().isConstQualified() ||
274f4a2713aSLionel Sambuc          Var->getType()->isReferenceType())) {
275f4a2713aSLionel Sambuc       if (const Expr *Init = Var->getAnyInitializer())
276f4a2713aSLionel Sambuc         if (Init->isValueDependent()) {
277f4a2713aSLionel Sambuc           ValueDependent = true;
278f4a2713aSLionel Sambuc           InstantiationDependent = true;
279f4a2713aSLionel Sambuc         }
280f4a2713aSLionel Sambuc     }
281f4a2713aSLionel Sambuc 
282f4a2713aSLionel Sambuc     // (VD) - FIXME: Missing from the standard:
283f4a2713aSLionel Sambuc     //      -  a member function or a static data member of the current
284f4a2713aSLionel Sambuc     //         instantiation
285f4a2713aSLionel Sambuc     if (Var->isStaticDataMember() &&
286f4a2713aSLionel Sambuc         Var->getDeclContext()->isDependentContext()) {
287f4a2713aSLionel Sambuc       ValueDependent = true;
288f4a2713aSLionel Sambuc       InstantiationDependent = true;
289f4a2713aSLionel Sambuc       TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
290f4a2713aSLionel Sambuc       if (TInfo->getType()->isIncompleteArrayType())
291f4a2713aSLionel Sambuc         TypeDependent = true;
292f4a2713aSLionel Sambuc     }
293f4a2713aSLionel Sambuc 
294f4a2713aSLionel Sambuc     return;
295f4a2713aSLionel Sambuc   }
296f4a2713aSLionel Sambuc 
297f4a2713aSLionel Sambuc   // (VD) - FIXME: Missing from the standard:
298f4a2713aSLionel Sambuc   //      -  a member function or a static data member of the current
299f4a2713aSLionel Sambuc   //         instantiation
300f4a2713aSLionel Sambuc   if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
301f4a2713aSLionel Sambuc     ValueDependent = true;
302f4a2713aSLionel Sambuc     InstantiationDependent = true;
303f4a2713aSLionel Sambuc   }
304f4a2713aSLionel Sambuc }
305f4a2713aSLionel Sambuc 
computeDependence(const ASTContext & Ctx)306f4a2713aSLionel Sambuc void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
307f4a2713aSLionel Sambuc   bool TypeDependent = false;
308f4a2713aSLionel Sambuc   bool ValueDependent = false;
309f4a2713aSLionel Sambuc   bool InstantiationDependent = false;
310f4a2713aSLionel Sambuc   computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
311f4a2713aSLionel Sambuc                            ValueDependent, InstantiationDependent);
312f4a2713aSLionel Sambuc 
313*0a6a1f1dSLionel Sambuc   ExprBits.TypeDependent |= TypeDependent;
314*0a6a1f1dSLionel Sambuc   ExprBits.ValueDependent |= ValueDependent;
315*0a6a1f1dSLionel Sambuc   ExprBits.InstantiationDependent |= InstantiationDependent;
316f4a2713aSLionel Sambuc 
317f4a2713aSLionel Sambuc   // Is the declaration a parameter pack?
318f4a2713aSLionel Sambuc   if (getDecl()->isParameterPack())
319f4a2713aSLionel Sambuc     ExprBits.ContainsUnexpandedParameterPack = true;
320f4a2713aSLionel Sambuc }
321f4a2713aSLionel Sambuc 
DeclRefExpr(const ASTContext & Ctx,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,ValueDecl * D,bool RefersToEnclosingVariableOrCapture,const DeclarationNameInfo & NameInfo,NamedDecl * FoundD,const TemplateArgumentListInfo * TemplateArgs,QualType T,ExprValueKind VK)322f4a2713aSLionel Sambuc DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
323f4a2713aSLionel Sambuc                          NestedNameSpecifierLoc QualifierLoc,
324f4a2713aSLionel Sambuc                          SourceLocation TemplateKWLoc,
325*0a6a1f1dSLionel Sambuc                          ValueDecl *D, bool RefersToEnclosingVariableOrCapture,
326f4a2713aSLionel Sambuc                          const DeclarationNameInfo &NameInfo,
327f4a2713aSLionel Sambuc                          NamedDecl *FoundD,
328f4a2713aSLionel Sambuc                          const TemplateArgumentListInfo *TemplateArgs,
329f4a2713aSLionel Sambuc                          QualType T, ExprValueKind VK)
330f4a2713aSLionel Sambuc   : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
331f4a2713aSLionel Sambuc     D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
332f4a2713aSLionel Sambuc   DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
333*0a6a1f1dSLionel Sambuc   if (QualifierLoc) {
334f4a2713aSLionel Sambuc     getInternalQualifierLoc() = QualifierLoc;
335*0a6a1f1dSLionel Sambuc     auto *NNS = QualifierLoc.getNestedNameSpecifier();
336*0a6a1f1dSLionel Sambuc     if (NNS->isInstantiationDependent())
337*0a6a1f1dSLionel Sambuc       ExprBits.InstantiationDependent = true;
338*0a6a1f1dSLionel Sambuc     if (NNS->containsUnexpandedParameterPack())
339*0a6a1f1dSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
340*0a6a1f1dSLionel Sambuc   }
341f4a2713aSLionel Sambuc   DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
342f4a2713aSLionel Sambuc   if (FoundD)
343f4a2713aSLionel Sambuc     getInternalFoundDecl() = FoundD;
344f4a2713aSLionel Sambuc   DeclRefExprBits.HasTemplateKWAndArgsInfo
345f4a2713aSLionel Sambuc     = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
346*0a6a1f1dSLionel Sambuc   DeclRefExprBits.RefersToEnclosingVariableOrCapture =
347*0a6a1f1dSLionel Sambuc       RefersToEnclosingVariableOrCapture;
348f4a2713aSLionel Sambuc   if (TemplateArgs) {
349f4a2713aSLionel Sambuc     bool Dependent = false;
350f4a2713aSLionel Sambuc     bool InstantiationDependent = false;
351f4a2713aSLionel Sambuc     bool ContainsUnexpandedParameterPack = false;
352f4a2713aSLionel Sambuc     getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
353f4a2713aSLionel Sambuc                                                Dependent,
354f4a2713aSLionel Sambuc                                                InstantiationDependent,
355f4a2713aSLionel Sambuc                                                ContainsUnexpandedParameterPack);
356*0a6a1f1dSLionel Sambuc     assert(!Dependent && "built a DeclRefExpr with dependent template args");
357*0a6a1f1dSLionel Sambuc     ExprBits.InstantiationDependent |= InstantiationDependent;
358*0a6a1f1dSLionel Sambuc     ExprBits.ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
359f4a2713aSLionel Sambuc   } else if (TemplateKWLoc.isValid()) {
360f4a2713aSLionel Sambuc     getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
361f4a2713aSLionel Sambuc   }
362f4a2713aSLionel Sambuc   DeclRefExprBits.HadMultipleCandidates = 0;
363f4a2713aSLionel Sambuc 
364f4a2713aSLionel Sambuc   computeDependence(Ctx);
365f4a2713aSLionel Sambuc }
366f4a2713aSLionel Sambuc 
Create(const ASTContext & Context,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,ValueDecl * D,bool RefersToEnclosingVariableOrCapture,SourceLocation NameLoc,QualType T,ExprValueKind VK,NamedDecl * FoundD,const TemplateArgumentListInfo * TemplateArgs)367f4a2713aSLionel Sambuc DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
368f4a2713aSLionel Sambuc                                  NestedNameSpecifierLoc QualifierLoc,
369f4a2713aSLionel Sambuc                                  SourceLocation TemplateKWLoc,
370f4a2713aSLionel Sambuc                                  ValueDecl *D,
371*0a6a1f1dSLionel Sambuc                                  bool RefersToEnclosingVariableOrCapture,
372f4a2713aSLionel Sambuc                                  SourceLocation NameLoc,
373f4a2713aSLionel Sambuc                                  QualType T,
374f4a2713aSLionel Sambuc                                  ExprValueKind VK,
375f4a2713aSLionel Sambuc                                  NamedDecl *FoundD,
376f4a2713aSLionel Sambuc                                  const TemplateArgumentListInfo *TemplateArgs) {
377f4a2713aSLionel Sambuc   return Create(Context, QualifierLoc, TemplateKWLoc, D,
378*0a6a1f1dSLionel Sambuc                 RefersToEnclosingVariableOrCapture,
379f4a2713aSLionel Sambuc                 DeclarationNameInfo(D->getDeclName(), NameLoc),
380f4a2713aSLionel Sambuc                 T, VK, FoundD, TemplateArgs);
381f4a2713aSLionel Sambuc }
382f4a2713aSLionel Sambuc 
Create(const ASTContext & Context,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,ValueDecl * D,bool RefersToEnclosingVariableOrCapture,const DeclarationNameInfo & NameInfo,QualType T,ExprValueKind VK,NamedDecl * FoundD,const TemplateArgumentListInfo * TemplateArgs)383f4a2713aSLionel Sambuc DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
384f4a2713aSLionel Sambuc                                  NestedNameSpecifierLoc QualifierLoc,
385f4a2713aSLionel Sambuc                                  SourceLocation TemplateKWLoc,
386f4a2713aSLionel Sambuc                                  ValueDecl *D,
387*0a6a1f1dSLionel Sambuc                                  bool RefersToEnclosingVariableOrCapture,
388f4a2713aSLionel Sambuc                                  const DeclarationNameInfo &NameInfo,
389f4a2713aSLionel Sambuc                                  QualType T,
390f4a2713aSLionel Sambuc                                  ExprValueKind VK,
391f4a2713aSLionel Sambuc                                  NamedDecl *FoundD,
392f4a2713aSLionel Sambuc                                  const TemplateArgumentListInfo *TemplateArgs) {
393f4a2713aSLionel Sambuc   // Filter out cases where the found Decl is the same as the value refenenced.
394f4a2713aSLionel Sambuc   if (D == FoundD)
395*0a6a1f1dSLionel Sambuc     FoundD = nullptr;
396f4a2713aSLionel Sambuc 
397f4a2713aSLionel Sambuc   std::size_t Size = sizeof(DeclRefExpr);
398f4a2713aSLionel Sambuc   if (QualifierLoc)
399f4a2713aSLionel Sambuc     Size += sizeof(NestedNameSpecifierLoc);
400f4a2713aSLionel Sambuc   if (FoundD)
401f4a2713aSLionel Sambuc     Size += sizeof(NamedDecl *);
402f4a2713aSLionel Sambuc   if (TemplateArgs)
403f4a2713aSLionel Sambuc     Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
404f4a2713aSLionel Sambuc   else if (TemplateKWLoc.isValid())
405f4a2713aSLionel Sambuc     Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
406f4a2713aSLionel Sambuc 
407f4a2713aSLionel Sambuc   void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
408f4a2713aSLionel Sambuc   return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
409*0a6a1f1dSLionel Sambuc                                RefersToEnclosingVariableOrCapture,
410f4a2713aSLionel Sambuc                                NameInfo, FoundD, TemplateArgs, T, VK);
411f4a2713aSLionel Sambuc }
412f4a2713aSLionel Sambuc 
CreateEmpty(const ASTContext & Context,bool HasQualifier,bool HasFoundDecl,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)413f4a2713aSLionel Sambuc DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
414f4a2713aSLionel Sambuc                                       bool HasQualifier,
415f4a2713aSLionel Sambuc                                       bool HasFoundDecl,
416f4a2713aSLionel Sambuc                                       bool HasTemplateKWAndArgsInfo,
417f4a2713aSLionel Sambuc                                       unsigned NumTemplateArgs) {
418f4a2713aSLionel Sambuc   std::size_t Size = sizeof(DeclRefExpr);
419f4a2713aSLionel Sambuc   if (HasQualifier)
420f4a2713aSLionel Sambuc     Size += sizeof(NestedNameSpecifierLoc);
421f4a2713aSLionel Sambuc   if (HasFoundDecl)
422f4a2713aSLionel Sambuc     Size += sizeof(NamedDecl *);
423f4a2713aSLionel Sambuc   if (HasTemplateKWAndArgsInfo)
424f4a2713aSLionel Sambuc     Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
425f4a2713aSLionel Sambuc 
426f4a2713aSLionel Sambuc   void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
427f4a2713aSLionel Sambuc   return new (Mem) DeclRefExpr(EmptyShell());
428f4a2713aSLionel Sambuc }
429f4a2713aSLionel Sambuc 
getLocStart() const430f4a2713aSLionel Sambuc SourceLocation DeclRefExpr::getLocStart() const {
431f4a2713aSLionel Sambuc   if (hasQualifier())
432f4a2713aSLionel Sambuc     return getQualifierLoc().getBeginLoc();
433f4a2713aSLionel Sambuc   return getNameInfo().getLocStart();
434f4a2713aSLionel Sambuc }
getLocEnd() const435f4a2713aSLionel Sambuc SourceLocation DeclRefExpr::getLocEnd() const {
436f4a2713aSLionel Sambuc   if (hasExplicitTemplateArgs())
437f4a2713aSLionel Sambuc     return getRAngleLoc();
438f4a2713aSLionel Sambuc   return getNameInfo().getLocEnd();
439f4a2713aSLionel Sambuc }
440f4a2713aSLionel Sambuc 
PredefinedExpr(SourceLocation L,QualType FNTy,IdentType IT,StringLiteral * SL)441*0a6a1f1dSLionel Sambuc PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, IdentType IT,
442*0a6a1f1dSLionel Sambuc                                StringLiteral *SL)
443*0a6a1f1dSLionel Sambuc     : Expr(PredefinedExprClass, FNTy, VK_LValue, OK_Ordinary,
444*0a6a1f1dSLionel Sambuc            FNTy->isDependentType(), FNTy->isDependentType(),
445*0a6a1f1dSLionel Sambuc            FNTy->isInstantiationDependentType(),
446*0a6a1f1dSLionel Sambuc            /*ContainsUnexpandedParameterPack=*/false),
447*0a6a1f1dSLionel Sambuc       Loc(L), Type(IT), FnName(SL) {}
448*0a6a1f1dSLionel Sambuc 
getFunctionName()449*0a6a1f1dSLionel Sambuc StringLiteral *PredefinedExpr::getFunctionName() {
450*0a6a1f1dSLionel Sambuc   return cast_or_null<StringLiteral>(FnName);
451*0a6a1f1dSLionel Sambuc }
452*0a6a1f1dSLionel Sambuc 
getIdentTypeName(PredefinedExpr::IdentType IT)453*0a6a1f1dSLionel Sambuc StringRef PredefinedExpr::getIdentTypeName(PredefinedExpr::IdentType IT) {
454*0a6a1f1dSLionel Sambuc   switch (IT) {
455*0a6a1f1dSLionel Sambuc   case Func:
456*0a6a1f1dSLionel Sambuc     return "__func__";
457*0a6a1f1dSLionel Sambuc   case Function:
458*0a6a1f1dSLionel Sambuc     return "__FUNCTION__";
459*0a6a1f1dSLionel Sambuc   case FuncDName:
460*0a6a1f1dSLionel Sambuc     return "__FUNCDNAME__";
461*0a6a1f1dSLionel Sambuc   case LFunction:
462*0a6a1f1dSLionel Sambuc     return "L__FUNCTION__";
463*0a6a1f1dSLionel Sambuc   case PrettyFunction:
464*0a6a1f1dSLionel Sambuc     return "__PRETTY_FUNCTION__";
465*0a6a1f1dSLionel Sambuc   case FuncSig:
466*0a6a1f1dSLionel Sambuc     return "__FUNCSIG__";
467*0a6a1f1dSLionel Sambuc   case PrettyFunctionNoVirtual:
468*0a6a1f1dSLionel Sambuc     break;
469*0a6a1f1dSLionel Sambuc   }
470*0a6a1f1dSLionel Sambuc   llvm_unreachable("Unknown ident type for PredefinedExpr");
471*0a6a1f1dSLionel Sambuc }
472*0a6a1f1dSLionel Sambuc 
473f4a2713aSLionel Sambuc // FIXME: Maybe this should use DeclPrinter with a special "print predefined
474f4a2713aSLionel Sambuc // expr" policy instead.
ComputeName(IdentType IT,const Decl * CurrentDecl)475f4a2713aSLionel Sambuc std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
476f4a2713aSLionel Sambuc   ASTContext &Context = CurrentDecl->getASTContext();
477f4a2713aSLionel Sambuc 
478f4a2713aSLionel Sambuc   if (IT == PredefinedExpr::FuncDName) {
479f4a2713aSLionel Sambuc     if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
480*0a6a1f1dSLionel Sambuc       std::unique_ptr<MangleContext> MC;
481f4a2713aSLionel Sambuc       MC.reset(Context.createMangleContext());
482f4a2713aSLionel Sambuc 
483f4a2713aSLionel Sambuc       if (MC->shouldMangleDeclName(ND)) {
484f4a2713aSLionel Sambuc         SmallString<256> Buffer;
485f4a2713aSLionel Sambuc         llvm::raw_svector_ostream Out(Buffer);
486f4a2713aSLionel Sambuc         if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
487f4a2713aSLionel Sambuc           MC->mangleCXXCtor(CD, Ctor_Base, Out);
488f4a2713aSLionel Sambuc         else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
489f4a2713aSLionel Sambuc           MC->mangleCXXDtor(DD, Dtor_Base, Out);
490f4a2713aSLionel Sambuc         else
491f4a2713aSLionel Sambuc           MC->mangleName(ND, Out);
492f4a2713aSLionel Sambuc 
493f4a2713aSLionel Sambuc         Out.flush();
494f4a2713aSLionel Sambuc         if (!Buffer.empty() && Buffer.front() == '\01')
495f4a2713aSLionel Sambuc           return Buffer.substr(1);
496f4a2713aSLionel Sambuc         return Buffer.str();
497f4a2713aSLionel Sambuc       } else
498f4a2713aSLionel Sambuc         return ND->getIdentifier()->getName();
499f4a2713aSLionel Sambuc     }
500f4a2713aSLionel Sambuc     return "";
501f4a2713aSLionel Sambuc   }
502*0a6a1f1dSLionel Sambuc   if (auto *BD = dyn_cast<BlockDecl>(CurrentDecl)) {
503*0a6a1f1dSLionel Sambuc     std::unique_ptr<MangleContext> MC;
504*0a6a1f1dSLionel Sambuc     MC.reset(Context.createMangleContext());
505*0a6a1f1dSLionel Sambuc     SmallString<256> Buffer;
506*0a6a1f1dSLionel Sambuc     llvm::raw_svector_ostream Out(Buffer);
507*0a6a1f1dSLionel Sambuc     auto DC = CurrentDecl->getDeclContext();
508*0a6a1f1dSLionel Sambuc     if (DC->isFileContext())
509*0a6a1f1dSLionel Sambuc       MC->mangleGlobalBlock(BD, /*ID*/ nullptr, Out);
510*0a6a1f1dSLionel Sambuc     else if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
511*0a6a1f1dSLionel Sambuc       MC->mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
512*0a6a1f1dSLionel Sambuc     else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
513*0a6a1f1dSLionel Sambuc       MC->mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
514*0a6a1f1dSLionel Sambuc     else
515*0a6a1f1dSLionel Sambuc       MC->mangleBlock(DC, BD, Out);
516*0a6a1f1dSLionel Sambuc     return Out.str();
517*0a6a1f1dSLionel Sambuc   }
518f4a2713aSLionel Sambuc   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
519*0a6a1f1dSLionel Sambuc     if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual && IT != FuncSig)
520f4a2713aSLionel Sambuc       return FD->getNameAsString();
521f4a2713aSLionel Sambuc 
522f4a2713aSLionel Sambuc     SmallString<256> Name;
523f4a2713aSLionel Sambuc     llvm::raw_svector_ostream Out(Name);
524f4a2713aSLionel Sambuc 
525f4a2713aSLionel Sambuc     if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
526f4a2713aSLionel Sambuc       if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
527f4a2713aSLionel Sambuc         Out << "virtual ";
528f4a2713aSLionel Sambuc       if (MD->isStatic())
529f4a2713aSLionel Sambuc         Out << "static ";
530f4a2713aSLionel Sambuc     }
531f4a2713aSLionel Sambuc 
532f4a2713aSLionel Sambuc     PrintingPolicy Policy(Context.getLangOpts());
533f4a2713aSLionel Sambuc     std::string Proto;
534f4a2713aSLionel Sambuc     llvm::raw_string_ostream POut(Proto);
535f4a2713aSLionel Sambuc 
536f4a2713aSLionel Sambuc     const FunctionDecl *Decl = FD;
537f4a2713aSLionel Sambuc     if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
538f4a2713aSLionel Sambuc       Decl = Pattern;
539f4a2713aSLionel Sambuc     const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
540*0a6a1f1dSLionel Sambuc     const FunctionProtoType *FT = nullptr;
541f4a2713aSLionel Sambuc     if (FD->hasWrittenPrototype())
542f4a2713aSLionel Sambuc       FT = dyn_cast<FunctionProtoType>(AFT);
543f4a2713aSLionel Sambuc 
544*0a6a1f1dSLionel Sambuc     if (IT == FuncSig) {
545*0a6a1f1dSLionel Sambuc       switch (FT->getCallConv()) {
546*0a6a1f1dSLionel Sambuc       case CC_C: POut << "__cdecl "; break;
547*0a6a1f1dSLionel Sambuc       case CC_X86StdCall: POut << "__stdcall "; break;
548*0a6a1f1dSLionel Sambuc       case CC_X86FastCall: POut << "__fastcall "; break;
549*0a6a1f1dSLionel Sambuc       case CC_X86ThisCall: POut << "__thiscall "; break;
550*0a6a1f1dSLionel Sambuc       case CC_X86VectorCall: POut << "__vectorcall "; break;
551*0a6a1f1dSLionel Sambuc       // Only bother printing the conventions that MSVC knows about.
552*0a6a1f1dSLionel Sambuc       default: break;
553*0a6a1f1dSLionel Sambuc       }
554*0a6a1f1dSLionel Sambuc     }
555*0a6a1f1dSLionel Sambuc 
556*0a6a1f1dSLionel Sambuc     FD->printQualifiedName(POut, Policy);
557*0a6a1f1dSLionel Sambuc 
558f4a2713aSLionel Sambuc     POut << "(";
559f4a2713aSLionel Sambuc     if (FT) {
560f4a2713aSLionel Sambuc       for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
561f4a2713aSLionel Sambuc         if (i) POut << ", ";
562f4a2713aSLionel Sambuc         POut << Decl->getParamDecl(i)->getType().stream(Policy);
563f4a2713aSLionel Sambuc       }
564f4a2713aSLionel Sambuc 
565f4a2713aSLionel Sambuc       if (FT->isVariadic()) {
566f4a2713aSLionel Sambuc         if (FD->getNumParams()) POut << ", ";
567f4a2713aSLionel Sambuc         POut << "...";
568f4a2713aSLionel Sambuc       }
569f4a2713aSLionel Sambuc     }
570f4a2713aSLionel Sambuc     POut << ")";
571f4a2713aSLionel Sambuc 
572f4a2713aSLionel Sambuc     if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
573f4a2713aSLionel Sambuc       const FunctionType *FT = MD->getType()->castAs<FunctionType>();
574f4a2713aSLionel Sambuc       if (FT->isConst())
575f4a2713aSLionel Sambuc         POut << " const";
576f4a2713aSLionel Sambuc       if (FT->isVolatile())
577f4a2713aSLionel Sambuc         POut << " volatile";
578f4a2713aSLionel Sambuc       RefQualifierKind Ref = MD->getRefQualifier();
579f4a2713aSLionel Sambuc       if (Ref == RQ_LValue)
580f4a2713aSLionel Sambuc         POut << " &";
581f4a2713aSLionel Sambuc       else if (Ref == RQ_RValue)
582f4a2713aSLionel Sambuc         POut << " &&";
583f4a2713aSLionel Sambuc     }
584f4a2713aSLionel Sambuc 
585f4a2713aSLionel Sambuc     typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
586f4a2713aSLionel Sambuc     SpecsTy Specs;
587f4a2713aSLionel Sambuc     const DeclContext *Ctx = FD->getDeclContext();
588f4a2713aSLionel Sambuc     while (Ctx && isa<NamedDecl>(Ctx)) {
589f4a2713aSLionel Sambuc       const ClassTemplateSpecializationDecl *Spec
590f4a2713aSLionel Sambuc                                = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
591f4a2713aSLionel Sambuc       if (Spec && !Spec->isExplicitSpecialization())
592f4a2713aSLionel Sambuc         Specs.push_back(Spec);
593f4a2713aSLionel Sambuc       Ctx = Ctx->getParent();
594f4a2713aSLionel Sambuc     }
595f4a2713aSLionel Sambuc 
596f4a2713aSLionel Sambuc     std::string TemplateParams;
597f4a2713aSLionel Sambuc     llvm::raw_string_ostream TOut(TemplateParams);
598f4a2713aSLionel Sambuc     for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
599f4a2713aSLionel Sambuc          I != E; ++I) {
600f4a2713aSLionel Sambuc       const TemplateParameterList *Params
601f4a2713aSLionel Sambuc                   = (*I)->getSpecializedTemplate()->getTemplateParameters();
602f4a2713aSLionel Sambuc       const TemplateArgumentList &Args = (*I)->getTemplateArgs();
603f4a2713aSLionel Sambuc       assert(Params->size() == Args.size());
604f4a2713aSLionel Sambuc       for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
605f4a2713aSLionel Sambuc         StringRef Param = Params->getParam(i)->getName();
606f4a2713aSLionel Sambuc         if (Param.empty()) continue;
607f4a2713aSLionel Sambuc         TOut << Param << " = ";
608f4a2713aSLionel Sambuc         Args.get(i).print(Policy, TOut);
609f4a2713aSLionel Sambuc         TOut << ", ";
610f4a2713aSLionel Sambuc       }
611f4a2713aSLionel Sambuc     }
612f4a2713aSLionel Sambuc 
613f4a2713aSLionel Sambuc     FunctionTemplateSpecializationInfo *FSI
614f4a2713aSLionel Sambuc                                           = FD->getTemplateSpecializationInfo();
615f4a2713aSLionel Sambuc     if (FSI && !FSI->isExplicitSpecialization()) {
616f4a2713aSLionel Sambuc       const TemplateParameterList* Params
617f4a2713aSLionel Sambuc                                   = FSI->getTemplate()->getTemplateParameters();
618f4a2713aSLionel Sambuc       const TemplateArgumentList* Args = FSI->TemplateArguments;
619f4a2713aSLionel Sambuc       assert(Params->size() == Args->size());
620f4a2713aSLionel Sambuc       for (unsigned i = 0, e = Params->size(); i != e; ++i) {
621f4a2713aSLionel Sambuc         StringRef Param = Params->getParam(i)->getName();
622f4a2713aSLionel Sambuc         if (Param.empty()) continue;
623f4a2713aSLionel Sambuc         TOut << Param << " = ";
624f4a2713aSLionel Sambuc         Args->get(i).print(Policy, TOut);
625f4a2713aSLionel Sambuc         TOut << ", ";
626f4a2713aSLionel Sambuc       }
627f4a2713aSLionel Sambuc     }
628f4a2713aSLionel Sambuc 
629f4a2713aSLionel Sambuc     TOut.flush();
630f4a2713aSLionel Sambuc     if (!TemplateParams.empty()) {
631f4a2713aSLionel Sambuc       // remove the trailing comma and space
632f4a2713aSLionel Sambuc       TemplateParams.resize(TemplateParams.size() - 2);
633f4a2713aSLionel Sambuc       POut << " [" << TemplateParams << "]";
634f4a2713aSLionel Sambuc     }
635f4a2713aSLionel Sambuc 
636f4a2713aSLionel Sambuc     POut.flush();
637f4a2713aSLionel Sambuc 
638f4a2713aSLionel Sambuc     // Print "auto" for all deduced return types. This includes C++1y return
639f4a2713aSLionel Sambuc     // type deduction and lambdas. For trailing return types resolve the
640f4a2713aSLionel Sambuc     // decltype expression. Otherwise print the real type when this is
641f4a2713aSLionel Sambuc     // not a constructor or destructor.
642*0a6a1f1dSLionel Sambuc     if (isa<CXXMethodDecl>(FD) &&
643*0a6a1f1dSLionel Sambuc          cast<CXXMethodDecl>(FD)->getParent()->isLambda())
644f4a2713aSLionel Sambuc       Proto = "auto " + Proto;
645*0a6a1f1dSLionel Sambuc     else if (FT && FT->getReturnType()->getAs<DecltypeType>())
646*0a6a1f1dSLionel Sambuc       FT->getReturnType()
647*0a6a1f1dSLionel Sambuc           ->getAs<DecltypeType>()
648*0a6a1f1dSLionel Sambuc           ->getUnderlyingType()
649f4a2713aSLionel Sambuc           .getAsStringInternal(Proto, Policy);
650f4a2713aSLionel Sambuc     else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
651*0a6a1f1dSLionel Sambuc       AFT->getReturnType().getAsStringInternal(Proto, Policy);
652f4a2713aSLionel Sambuc 
653f4a2713aSLionel Sambuc     Out << Proto;
654f4a2713aSLionel Sambuc 
655f4a2713aSLionel Sambuc     Out.flush();
656f4a2713aSLionel Sambuc     return Name.str().str();
657f4a2713aSLionel Sambuc   }
658f4a2713aSLionel Sambuc   if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
659f4a2713aSLionel Sambuc     for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
660f4a2713aSLionel Sambuc       // Skip to its enclosing function or method, but not its enclosing
661f4a2713aSLionel Sambuc       // CapturedDecl.
662f4a2713aSLionel Sambuc       if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
663f4a2713aSLionel Sambuc         const Decl *D = Decl::castFromDeclContext(DC);
664f4a2713aSLionel Sambuc         return ComputeName(IT, D);
665f4a2713aSLionel Sambuc       }
666f4a2713aSLionel Sambuc     llvm_unreachable("CapturedDecl not inside a function or method");
667f4a2713aSLionel Sambuc   }
668f4a2713aSLionel Sambuc   if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
669f4a2713aSLionel Sambuc     SmallString<256> Name;
670f4a2713aSLionel Sambuc     llvm::raw_svector_ostream Out(Name);
671f4a2713aSLionel Sambuc     Out << (MD->isInstanceMethod() ? '-' : '+');
672f4a2713aSLionel Sambuc     Out << '[';
673f4a2713aSLionel Sambuc 
674f4a2713aSLionel Sambuc     // For incorrect code, there might not be an ObjCInterfaceDecl.  Do
675f4a2713aSLionel Sambuc     // a null check to avoid a crash.
676f4a2713aSLionel Sambuc     if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
677f4a2713aSLionel Sambuc       Out << *ID;
678f4a2713aSLionel Sambuc 
679f4a2713aSLionel Sambuc     if (const ObjCCategoryImplDecl *CID =
680f4a2713aSLionel Sambuc         dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
681f4a2713aSLionel Sambuc       Out << '(' << *CID << ')';
682f4a2713aSLionel Sambuc 
683f4a2713aSLionel Sambuc     Out <<  ' ';
684*0a6a1f1dSLionel Sambuc     MD->getSelector().print(Out);
685f4a2713aSLionel Sambuc     Out <<  ']';
686f4a2713aSLionel Sambuc 
687f4a2713aSLionel Sambuc     Out.flush();
688f4a2713aSLionel Sambuc     return Name.str().str();
689f4a2713aSLionel Sambuc   }
690f4a2713aSLionel Sambuc   if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
691f4a2713aSLionel Sambuc     // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
692f4a2713aSLionel Sambuc     return "top level";
693f4a2713aSLionel Sambuc   }
694f4a2713aSLionel Sambuc   return "";
695f4a2713aSLionel Sambuc }
696f4a2713aSLionel Sambuc 
setIntValue(const ASTContext & C,const llvm::APInt & Val)697f4a2713aSLionel Sambuc void APNumericStorage::setIntValue(const ASTContext &C,
698f4a2713aSLionel Sambuc                                    const llvm::APInt &Val) {
699f4a2713aSLionel Sambuc   if (hasAllocation())
700f4a2713aSLionel Sambuc     C.Deallocate(pVal);
701f4a2713aSLionel Sambuc 
702f4a2713aSLionel Sambuc   BitWidth = Val.getBitWidth();
703f4a2713aSLionel Sambuc   unsigned NumWords = Val.getNumWords();
704f4a2713aSLionel Sambuc   const uint64_t* Words = Val.getRawData();
705f4a2713aSLionel Sambuc   if (NumWords > 1) {
706f4a2713aSLionel Sambuc     pVal = new (C) uint64_t[NumWords];
707f4a2713aSLionel Sambuc     std::copy(Words, Words + NumWords, pVal);
708f4a2713aSLionel Sambuc   } else if (NumWords == 1)
709f4a2713aSLionel Sambuc     VAL = Words[0];
710f4a2713aSLionel Sambuc   else
711f4a2713aSLionel Sambuc     VAL = 0;
712f4a2713aSLionel Sambuc }
713f4a2713aSLionel Sambuc 
IntegerLiteral(const ASTContext & C,const llvm::APInt & V,QualType type,SourceLocation l)714f4a2713aSLionel Sambuc IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
715f4a2713aSLionel Sambuc                                QualType type, SourceLocation l)
716f4a2713aSLionel Sambuc   : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
717f4a2713aSLionel Sambuc          false, false),
718f4a2713aSLionel Sambuc     Loc(l) {
719f4a2713aSLionel Sambuc   assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
720f4a2713aSLionel Sambuc   assert(V.getBitWidth() == C.getIntWidth(type) &&
721f4a2713aSLionel Sambuc          "Integer type is not the correct size for constant.");
722f4a2713aSLionel Sambuc   setValue(C, V);
723f4a2713aSLionel Sambuc }
724f4a2713aSLionel Sambuc 
725f4a2713aSLionel Sambuc IntegerLiteral *
Create(const ASTContext & C,const llvm::APInt & V,QualType type,SourceLocation l)726f4a2713aSLionel Sambuc IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
727f4a2713aSLionel Sambuc                        QualType type, SourceLocation l) {
728f4a2713aSLionel Sambuc   return new (C) IntegerLiteral(C, V, type, l);
729f4a2713aSLionel Sambuc }
730f4a2713aSLionel Sambuc 
731f4a2713aSLionel Sambuc IntegerLiteral *
Create(const ASTContext & C,EmptyShell Empty)732f4a2713aSLionel Sambuc IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
733f4a2713aSLionel Sambuc   return new (C) IntegerLiteral(Empty);
734f4a2713aSLionel Sambuc }
735f4a2713aSLionel Sambuc 
FloatingLiteral(const ASTContext & C,const llvm::APFloat & V,bool isexact,QualType Type,SourceLocation L)736f4a2713aSLionel Sambuc FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
737f4a2713aSLionel Sambuc                                  bool isexact, QualType Type, SourceLocation L)
738f4a2713aSLionel Sambuc   : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
739f4a2713aSLionel Sambuc          false, false), Loc(L) {
740f4a2713aSLionel Sambuc   setSemantics(V.getSemantics());
741f4a2713aSLionel Sambuc   FloatingLiteralBits.IsExact = isexact;
742f4a2713aSLionel Sambuc   setValue(C, V);
743f4a2713aSLionel Sambuc }
744f4a2713aSLionel Sambuc 
FloatingLiteral(const ASTContext & C,EmptyShell Empty)745f4a2713aSLionel Sambuc FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
746f4a2713aSLionel Sambuc   : Expr(FloatingLiteralClass, Empty) {
747f4a2713aSLionel Sambuc   setRawSemantics(IEEEhalf);
748f4a2713aSLionel Sambuc   FloatingLiteralBits.IsExact = false;
749f4a2713aSLionel Sambuc }
750f4a2713aSLionel Sambuc 
751f4a2713aSLionel Sambuc FloatingLiteral *
Create(const ASTContext & C,const llvm::APFloat & V,bool isexact,QualType Type,SourceLocation L)752f4a2713aSLionel Sambuc FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
753f4a2713aSLionel Sambuc                         bool isexact, QualType Type, SourceLocation L) {
754f4a2713aSLionel Sambuc   return new (C) FloatingLiteral(C, V, isexact, Type, L);
755f4a2713aSLionel Sambuc }
756f4a2713aSLionel Sambuc 
757f4a2713aSLionel Sambuc FloatingLiteral *
Create(const ASTContext & C,EmptyShell Empty)758f4a2713aSLionel Sambuc FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
759f4a2713aSLionel Sambuc   return new (C) FloatingLiteral(C, Empty);
760f4a2713aSLionel Sambuc }
761f4a2713aSLionel Sambuc 
getSemantics() const762f4a2713aSLionel Sambuc const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
763f4a2713aSLionel Sambuc   switch(FloatingLiteralBits.Semantics) {
764f4a2713aSLionel Sambuc   case IEEEhalf:
765f4a2713aSLionel Sambuc     return llvm::APFloat::IEEEhalf;
766f4a2713aSLionel Sambuc   case IEEEsingle:
767f4a2713aSLionel Sambuc     return llvm::APFloat::IEEEsingle;
768f4a2713aSLionel Sambuc   case IEEEdouble:
769f4a2713aSLionel Sambuc     return llvm::APFloat::IEEEdouble;
770f4a2713aSLionel Sambuc   case x87DoubleExtended:
771f4a2713aSLionel Sambuc     return llvm::APFloat::x87DoubleExtended;
772f4a2713aSLionel Sambuc   case IEEEquad:
773f4a2713aSLionel Sambuc     return llvm::APFloat::IEEEquad;
774f4a2713aSLionel Sambuc   case PPCDoubleDouble:
775f4a2713aSLionel Sambuc     return llvm::APFloat::PPCDoubleDouble;
776f4a2713aSLionel Sambuc   }
777f4a2713aSLionel Sambuc   llvm_unreachable("Unrecognised floating semantics");
778f4a2713aSLionel Sambuc }
779f4a2713aSLionel Sambuc 
setSemantics(const llvm::fltSemantics & Sem)780f4a2713aSLionel Sambuc void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
781f4a2713aSLionel Sambuc   if (&Sem == &llvm::APFloat::IEEEhalf)
782f4a2713aSLionel Sambuc     FloatingLiteralBits.Semantics = IEEEhalf;
783f4a2713aSLionel Sambuc   else if (&Sem == &llvm::APFloat::IEEEsingle)
784f4a2713aSLionel Sambuc     FloatingLiteralBits.Semantics = IEEEsingle;
785f4a2713aSLionel Sambuc   else if (&Sem == &llvm::APFloat::IEEEdouble)
786f4a2713aSLionel Sambuc     FloatingLiteralBits.Semantics = IEEEdouble;
787f4a2713aSLionel Sambuc   else if (&Sem == &llvm::APFloat::x87DoubleExtended)
788f4a2713aSLionel Sambuc     FloatingLiteralBits.Semantics = x87DoubleExtended;
789f4a2713aSLionel Sambuc   else if (&Sem == &llvm::APFloat::IEEEquad)
790f4a2713aSLionel Sambuc     FloatingLiteralBits.Semantics = IEEEquad;
791f4a2713aSLionel Sambuc   else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
792f4a2713aSLionel Sambuc     FloatingLiteralBits.Semantics = PPCDoubleDouble;
793f4a2713aSLionel Sambuc   else
794f4a2713aSLionel Sambuc     llvm_unreachable("Unknown floating semantics");
795f4a2713aSLionel Sambuc }
796f4a2713aSLionel Sambuc 
797f4a2713aSLionel Sambuc /// getValueAsApproximateDouble - This returns the value as an inaccurate
798f4a2713aSLionel Sambuc /// double.  Note that this may cause loss of precision, but is useful for
799f4a2713aSLionel Sambuc /// debugging dumps, etc.
getValueAsApproximateDouble() const800f4a2713aSLionel Sambuc double FloatingLiteral::getValueAsApproximateDouble() const {
801f4a2713aSLionel Sambuc   llvm::APFloat V = getValue();
802f4a2713aSLionel Sambuc   bool ignored;
803f4a2713aSLionel Sambuc   V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
804f4a2713aSLionel Sambuc             &ignored);
805f4a2713aSLionel Sambuc   return V.convertToDouble();
806f4a2713aSLionel Sambuc }
807f4a2713aSLionel Sambuc 
mapCharByteWidth(TargetInfo const & target,StringKind k)808f4a2713aSLionel Sambuc int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
809f4a2713aSLionel Sambuc   int CharByteWidth = 0;
810f4a2713aSLionel Sambuc   switch(k) {
811f4a2713aSLionel Sambuc     case Ascii:
812f4a2713aSLionel Sambuc     case UTF8:
813f4a2713aSLionel Sambuc       CharByteWidth = target.getCharWidth();
814f4a2713aSLionel Sambuc       break;
815f4a2713aSLionel Sambuc     case Wide:
816f4a2713aSLionel Sambuc       CharByteWidth = target.getWCharWidth();
817f4a2713aSLionel Sambuc       break;
818f4a2713aSLionel Sambuc     case UTF16:
819f4a2713aSLionel Sambuc       CharByteWidth = target.getChar16Width();
820f4a2713aSLionel Sambuc       break;
821f4a2713aSLionel Sambuc     case UTF32:
822f4a2713aSLionel Sambuc       CharByteWidth = target.getChar32Width();
823f4a2713aSLionel Sambuc       break;
824f4a2713aSLionel Sambuc   }
825f4a2713aSLionel Sambuc   assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
826f4a2713aSLionel Sambuc   CharByteWidth /= 8;
827f4a2713aSLionel Sambuc   assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
828f4a2713aSLionel Sambuc          && "character byte widths supported are 1, 2, and 4 only");
829f4a2713aSLionel Sambuc   return CharByteWidth;
830f4a2713aSLionel Sambuc }
831f4a2713aSLionel Sambuc 
Create(const ASTContext & C,StringRef Str,StringKind Kind,bool Pascal,QualType Ty,const SourceLocation * Loc,unsigned NumStrs)832f4a2713aSLionel Sambuc StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
833f4a2713aSLionel Sambuc                                      StringKind Kind, bool Pascal, QualType Ty,
834f4a2713aSLionel Sambuc                                      const SourceLocation *Loc,
835f4a2713aSLionel Sambuc                                      unsigned NumStrs) {
836*0a6a1f1dSLionel Sambuc   assert(C.getAsConstantArrayType(Ty) &&
837*0a6a1f1dSLionel Sambuc          "StringLiteral must be of constant array type!");
838*0a6a1f1dSLionel Sambuc 
839f4a2713aSLionel Sambuc   // Allocate enough space for the StringLiteral plus an array of locations for
840f4a2713aSLionel Sambuc   // any concatenated string tokens.
841f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(StringLiteral)+
842f4a2713aSLionel Sambuc                          sizeof(SourceLocation)*(NumStrs-1),
843f4a2713aSLionel Sambuc                          llvm::alignOf<StringLiteral>());
844f4a2713aSLionel Sambuc   StringLiteral *SL = new (Mem) StringLiteral(Ty);
845f4a2713aSLionel Sambuc 
846f4a2713aSLionel Sambuc   // OPTIMIZE: could allocate this appended to the StringLiteral.
847f4a2713aSLionel Sambuc   SL->setString(C,Str,Kind,Pascal);
848f4a2713aSLionel Sambuc 
849f4a2713aSLionel Sambuc   SL->TokLocs[0] = Loc[0];
850f4a2713aSLionel Sambuc   SL->NumConcatenated = NumStrs;
851f4a2713aSLionel Sambuc 
852f4a2713aSLionel Sambuc   if (NumStrs != 1)
853f4a2713aSLionel Sambuc     memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
854f4a2713aSLionel Sambuc   return SL;
855f4a2713aSLionel Sambuc }
856f4a2713aSLionel Sambuc 
CreateEmpty(const ASTContext & C,unsigned NumStrs)857f4a2713aSLionel Sambuc StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
858f4a2713aSLionel Sambuc                                           unsigned NumStrs) {
859f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(StringLiteral)+
860f4a2713aSLionel Sambuc                          sizeof(SourceLocation)*(NumStrs-1),
861f4a2713aSLionel Sambuc                          llvm::alignOf<StringLiteral>());
862f4a2713aSLionel Sambuc   StringLiteral *SL = new (Mem) StringLiteral(QualType());
863f4a2713aSLionel Sambuc   SL->CharByteWidth = 0;
864f4a2713aSLionel Sambuc   SL->Length = 0;
865f4a2713aSLionel Sambuc   SL->NumConcatenated = NumStrs;
866f4a2713aSLionel Sambuc   return SL;
867f4a2713aSLionel Sambuc }
868f4a2713aSLionel Sambuc 
outputString(raw_ostream & OS) const869f4a2713aSLionel Sambuc void StringLiteral::outputString(raw_ostream &OS) const {
870f4a2713aSLionel Sambuc   switch (getKind()) {
871f4a2713aSLionel Sambuc   case Ascii: break; // no prefix.
872f4a2713aSLionel Sambuc   case Wide:  OS << 'L'; break;
873f4a2713aSLionel Sambuc   case UTF8:  OS << "u8"; break;
874f4a2713aSLionel Sambuc   case UTF16: OS << 'u'; break;
875f4a2713aSLionel Sambuc   case UTF32: OS << 'U'; break;
876f4a2713aSLionel Sambuc   }
877f4a2713aSLionel Sambuc   OS << '"';
878f4a2713aSLionel Sambuc   static const char Hex[] = "0123456789ABCDEF";
879f4a2713aSLionel Sambuc 
880f4a2713aSLionel Sambuc   unsigned LastSlashX = getLength();
881f4a2713aSLionel Sambuc   for (unsigned I = 0, N = getLength(); I != N; ++I) {
882f4a2713aSLionel Sambuc     switch (uint32_t Char = getCodeUnit(I)) {
883f4a2713aSLionel Sambuc     default:
884f4a2713aSLionel Sambuc       // FIXME: Convert UTF-8 back to codepoints before rendering.
885f4a2713aSLionel Sambuc 
886f4a2713aSLionel Sambuc       // Convert UTF-16 surrogate pairs back to codepoints before rendering.
887f4a2713aSLionel Sambuc       // Leave invalid surrogates alone; we'll use \x for those.
888f4a2713aSLionel Sambuc       if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
889f4a2713aSLionel Sambuc           Char <= 0xdbff) {
890f4a2713aSLionel Sambuc         uint32_t Trail = getCodeUnit(I + 1);
891f4a2713aSLionel Sambuc         if (Trail >= 0xdc00 && Trail <= 0xdfff) {
892f4a2713aSLionel Sambuc           Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
893f4a2713aSLionel Sambuc           ++I;
894f4a2713aSLionel Sambuc         }
895f4a2713aSLionel Sambuc       }
896f4a2713aSLionel Sambuc 
897f4a2713aSLionel Sambuc       if (Char > 0xff) {
898f4a2713aSLionel Sambuc         // If this is a wide string, output characters over 0xff using \x
899f4a2713aSLionel Sambuc         // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
900f4a2713aSLionel Sambuc         // codepoint: use \x escapes for invalid codepoints.
901f4a2713aSLionel Sambuc         if (getKind() == Wide ||
902f4a2713aSLionel Sambuc             (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
903f4a2713aSLionel Sambuc           // FIXME: Is this the best way to print wchar_t?
904f4a2713aSLionel Sambuc           OS << "\\x";
905f4a2713aSLionel Sambuc           int Shift = 28;
906f4a2713aSLionel Sambuc           while ((Char >> Shift) == 0)
907f4a2713aSLionel Sambuc             Shift -= 4;
908f4a2713aSLionel Sambuc           for (/**/; Shift >= 0; Shift -= 4)
909f4a2713aSLionel Sambuc             OS << Hex[(Char >> Shift) & 15];
910f4a2713aSLionel Sambuc           LastSlashX = I;
911f4a2713aSLionel Sambuc           break;
912f4a2713aSLionel Sambuc         }
913f4a2713aSLionel Sambuc 
914f4a2713aSLionel Sambuc         if (Char > 0xffff)
915f4a2713aSLionel Sambuc           OS << "\\U00"
916f4a2713aSLionel Sambuc              << Hex[(Char >> 20) & 15]
917f4a2713aSLionel Sambuc              << Hex[(Char >> 16) & 15];
918f4a2713aSLionel Sambuc         else
919f4a2713aSLionel Sambuc           OS << "\\u";
920f4a2713aSLionel Sambuc         OS << Hex[(Char >> 12) & 15]
921f4a2713aSLionel Sambuc            << Hex[(Char >>  8) & 15]
922f4a2713aSLionel Sambuc            << Hex[(Char >>  4) & 15]
923f4a2713aSLionel Sambuc            << Hex[(Char >>  0) & 15];
924f4a2713aSLionel Sambuc         break;
925f4a2713aSLionel Sambuc       }
926f4a2713aSLionel Sambuc 
927f4a2713aSLionel Sambuc       // If we used \x... for the previous character, and this character is a
928f4a2713aSLionel Sambuc       // hexadecimal digit, prevent it being slurped as part of the \x.
929f4a2713aSLionel Sambuc       if (LastSlashX + 1 == I) {
930f4a2713aSLionel Sambuc         switch (Char) {
931f4a2713aSLionel Sambuc           case '0': case '1': case '2': case '3': case '4':
932f4a2713aSLionel Sambuc           case '5': case '6': case '7': case '8': case '9':
933f4a2713aSLionel Sambuc           case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
934f4a2713aSLionel Sambuc           case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
935f4a2713aSLionel Sambuc             OS << "\"\"";
936f4a2713aSLionel Sambuc         }
937f4a2713aSLionel Sambuc       }
938f4a2713aSLionel Sambuc 
939f4a2713aSLionel Sambuc       assert(Char <= 0xff &&
940f4a2713aSLionel Sambuc              "Characters above 0xff should already have been handled.");
941f4a2713aSLionel Sambuc 
942f4a2713aSLionel Sambuc       if (isPrintable(Char))
943f4a2713aSLionel Sambuc         OS << (char)Char;
944f4a2713aSLionel Sambuc       else  // Output anything hard as an octal escape.
945f4a2713aSLionel Sambuc         OS << '\\'
946f4a2713aSLionel Sambuc            << (char)('0' + ((Char >> 6) & 7))
947f4a2713aSLionel Sambuc            << (char)('0' + ((Char >> 3) & 7))
948f4a2713aSLionel Sambuc            << (char)('0' + ((Char >> 0) & 7));
949f4a2713aSLionel Sambuc       break;
950f4a2713aSLionel Sambuc     // Handle some common non-printable cases to make dumps prettier.
951f4a2713aSLionel Sambuc     case '\\': OS << "\\\\"; break;
952f4a2713aSLionel Sambuc     case '"': OS << "\\\""; break;
953f4a2713aSLionel Sambuc     case '\n': OS << "\\n"; break;
954f4a2713aSLionel Sambuc     case '\t': OS << "\\t"; break;
955f4a2713aSLionel Sambuc     case '\a': OS << "\\a"; break;
956f4a2713aSLionel Sambuc     case '\b': OS << "\\b"; break;
957f4a2713aSLionel Sambuc     }
958f4a2713aSLionel Sambuc   }
959f4a2713aSLionel Sambuc   OS << '"';
960f4a2713aSLionel Sambuc }
961f4a2713aSLionel Sambuc 
setString(const ASTContext & C,StringRef Str,StringKind Kind,bool IsPascal)962f4a2713aSLionel Sambuc void StringLiteral::setString(const ASTContext &C, StringRef Str,
963f4a2713aSLionel Sambuc                               StringKind Kind, bool IsPascal) {
964f4a2713aSLionel Sambuc   //FIXME: we assume that the string data comes from a target that uses the same
965f4a2713aSLionel Sambuc   // code unit size and endianess for the type of string.
966f4a2713aSLionel Sambuc   this->Kind = Kind;
967f4a2713aSLionel Sambuc   this->IsPascal = IsPascal;
968f4a2713aSLionel Sambuc 
969f4a2713aSLionel Sambuc   CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
970f4a2713aSLionel Sambuc   assert((Str.size()%CharByteWidth == 0)
971f4a2713aSLionel Sambuc          && "size of data must be multiple of CharByteWidth");
972f4a2713aSLionel Sambuc   Length = Str.size()/CharByteWidth;
973f4a2713aSLionel Sambuc 
974f4a2713aSLionel Sambuc   switch(CharByteWidth) {
975f4a2713aSLionel Sambuc     case 1: {
976f4a2713aSLionel Sambuc       char *AStrData = new (C) char[Length];
977f4a2713aSLionel Sambuc       std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
978f4a2713aSLionel Sambuc       StrData.asChar = AStrData;
979f4a2713aSLionel Sambuc       break;
980f4a2713aSLionel Sambuc     }
981f4a2713aSLionel Sambuc     case 2: {
982f4a2713aSLionel Sambuc       uint16_t *AStrData = new (C) uint16_t[Length];
983f4a2713aSLionel Sambuc       std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
984f4a2713aSLionel Sambuc       StrData.asUInt16 = AStrData;
985f4a2713aSLionel Sambuc       break;
986f4a2713aSLionel Sambuc     }
987f4a2713aSLionel Sambuc     case 4: {
988f4a2713aSLionel Sambuc       uint32_t *AStrData = new (C) uint32_t[Length];
989f4a2713aSLionel Sambuc       std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
990f4a2713aSLionel Sambuc       StrData.asUInt32 = AStrData;
991f4a2713aSLionel Sambuc       break;
992f4a2713aSLionel Sambuc     }
993f4a2713aSLionel Sambuc     default:
994f4a2713aSLionel Sambuc       assert(false && "unsupported CharByteWidth");
995f4a2713aSLionel Sambuc   }
996f4a2713aSLionel Sambuc }
997f4a2713aSLionel Sambuc 
998f4a2713aSLionel Sambuc /// getLocationOfByte - Return a source location that points to the specified
999f4a2713aSLionel Sambuc /// byte of this string literal.
1000f4a2713aSLionel Sambuc ///
1001f4a2713aSLionel Sambuc /// Strings are amazingly complex.  They can be formed from multiple tokens and
1002f4a2713aSLionel Sambuc /// can have escape sequences in them in addition to the usual trigraph and
1003f4a2713aSLionel Sambuc /// escaped newline business.  This routine handles this complexity.
1004f4a2713aSLionel Sambuc ///
1005f4a2713aSLionel Sambuc SourceLocation StringLiteral::
getLocationOfByte(unsigned ByteNo,const SourceManager & SM,const LangOptions & Features,const TargetInfo & Target) const1006f4a2713aSLionel Sambuc getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
1007f4a2713aSLionel Sambuc                   const LangOptions &Features, const TargetInfo &Target) const {
1008f4a2713aSLionel Sambuc   assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
1009f4a2713aSLionel Sambuc          "Only narrow string literals are currently supported");
1010f4a2713aSLionel Sambuc 
1011f4a2713aSLionel Sambuc   // Loop over all of the tokens in this string until we find the one that
1012f4a2713aSLionel Sambuc   // contains the byte we're looking for.
1013f4a2713aSLionel Sambuc   unsigned TokNo = 0;
1014f4a2713aSLionel Sambuc   while (1) {
1015f4a2713aSLionel Sambuc     assert(TokNo < getNumConcatenated() && "Invalid byte number!");
1016f4a2713aSLionel Sambuc     SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
1017f4a2713aSLionel Sambuc 
1018f4a2713aSLionel Sambuc     // Get the spelling of the string so that we can get the data that makes up
1019f4a2713aSLionel Sambuc     // the string literal, not the identifier for the macro it is potentially
1020f4a2713aSLionel Sambuc     // expanded through.
1021f4a2713aSLionel Sambuc     SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
1022f4a2713aSLionel Sambuc 
1023f4a2713aSLionel Sambuc     // Re-lex the token to get its length and original spelling.
1024f4a2713aSLionel Sambuc     std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
1025f4a2713aSLionel Sambuc     bool Invalid = false;
1026f4a2713aSLionel Sambuc     StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
1027f4a2713aSLionel Sambuc     if (Invalid)
1028f4a2713aSLionel Sambuc       return StrTokSpellingLoc;
1029f4a2713aSLionel Sambuc 
1030f4a2713aSLionel Sambuc     const char *StrData = Buffer.data()+LocInfo.second;
1031f4a2713aSLionel Sambuc 
1032f4a2713aSLionel Sambuc     // Create a lexer starting at the beginning of this token.
1033f4a2713aSLionel Sambuc     Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1034f4a2713aSLionel Sambuc                    Buffer.begin(), StrData, Buffer.end());
1035f4a2713aSLionel Sambuc     Token TheTok;
1036f4a2713aSLionel Sambuc     TheLexer.LexFromRawLexer(TheTok);
1037f4a2713aSLionel Sambuc 
1038f4a2713aSLionel Sambuc     // Use the StringLiteralParser to compute the length of the string in bytes.
1039*0a6a1f1dSLionel Sambuc     StringLiteralParser SLP(TheTok, SM, Features, Target);
1040f4a2713aSLionel Sambuc     unsigned TokNumBytes = SLP.GetStringLength();
1041f4a2713aSLionel Sambuc 
1042f4a2713aSLionel Sambuc     // If the byte is in this token, return the location of the byte.
1043f4a2713aSLionel Sambuc     if (ByteNo < TokNumBytes ||
1044f4a2713aSLionel Sambuc         (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
1045f4a2713aSLionel Sambuc       unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1046f4a2713aSLionel Sambuc 
1047f4a2713aSLionel Sambuc       // Now that we know the offset of the token in the spelling, use the
1048f4a2713aSLionel Sambuc       // preprocessor to get the offset in the original source.
1049f4a2713aSLionel Sambuc       return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1050f4a2713aSLionel Sambuc     }
1051f4a2713aSLionel Sambuc 
1052f4a2713aSLionel Sambuc     // Move to the next string token.
1053f4a2713aSLionel Sambuc     ++TokNo;
1054f4a2713aSLionel Sambuc     ByteNo -= TokNumBytes;
1055f4a2713aSLionel Sambuc   }
1056f4a2713aSLionel Sambuc }
1057f4a2713aSLionel Sambuc 
1058f4a2713aSLionel Sambuc 
1059f4a2713aSLionel Sambuc 
1060f4a2713aSLionel Sambuc /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1061f4a2713aSLionel Sambuc /// corresponds to, e.g. "sizeof" or "[pre]++".
getOpcodeStr(Opcode Op)1062f4a2713aSLionel Sambuc StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
1063f4a2713aSLionel Sambuc   switch (Op) {
1064f4a2713aSLionel Sambuc   case UO_PostInc: return "++";
1065f4a2713aSLionel Sambuc   case UO_PostDec: return "--";
1066f4a2713aSLionel Sambuc   case UO_PreInc:  return "++";
1067f4a2713aSLionel Sambuc   case UO_PreDec:  return "--";
1068f4a2713aSLionel Sambuc   case UO_AddrOf:  return "&";
1069f4a2713aSLionel Sambuc   case UO_Deref:   return "*";
1070f4a2713aSLionel Sambuc   case UO_Plus:    return "+";
1071f4a2713aSLionel Sambuc   case UO_Minus:   return "-";
1072f4a2713aSLionel Sambuc   case UO_Not:     return "~";
1073f4a2713aSLionel Sambuc   case UO_LNot:    return "!";
1074f4a2713aSLionel Sambuc   case UO_Real:    return "__real";
1075f4a2713aSLionel Sambuc   case UO_Imag:    return "__imag";
1076f4a2713aSLionel Sambuc   case UO_Extension: return "__extension__";
1077f4a2713aSLionel Sambuc   }
1078f4a2713aSLionel Sambuc   llvm_unreachable("Unknown unary operator");
1079f4a2713aSLionel Sambuc }
1080f4a2713aSLionel Sambuc 
1081f4a2713aSLionel Sambuc UnaryOperatorKind
getOverloadedOpcode(OverloadedOperatorKind OO,bool Postfix)1082f4a2713aSLionel Sambuc UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1083f4a2713aSLionel Sambuc   switch (OO) {
1084f4a2713aSLionel Sambuc   default: llvm_unreachable("No unary operator for overloaded function");
1085f4a2713aSLionel Sambuc   case OO_PlusPlus:   return Postfix ? UO_PostInc : UO_PreInc;
1086f4a2713aSLionel Sambuc   case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1087f4a2713aSLionel Sambuc   case OO_Amp:        return UO_AddrOf;
1088f4a2713aSLionel Sambuc   case OO_Star:       return UO_Deref;
1089f4a2713aSLionel Sambuc   case OO_Plus:       return UO_Plus;
1090f4a2713aSLionel Sambuc   case OO_Minus:      return UO_Minus;
1091f4a2713aSLionel Sambuc   case OO_Tilde:      return UO_Not;
1092f4a2713aSLionel Sambuc   case OO_Exclaim:    return UO_LNot;
1093f4a2713aSLionel Sambuc   }
1094f4a2713aSLionel Sambuc }
1095f4a2713aSLionel Sambuc 
getOverloadedOperator(Opcode Opc)1096f4a2713aSLionel Sambuc OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1097f4a2713aSLionel Sambuc   switch (Opc) {
1098f4a2713aSLionel Sambuc   case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1099f4a2713aSLionel Sambuc   case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1100f4a2713aSLionel Sambuc   case UO_AddrOf: return OO_Amp;
1101f4a2713aSLionel Sambuc   case UO_Deref: return OO_Star;
1102f4a2713aSLionel Sambuc   case UO_Plus: return OO_Plus;
1103f4a2713aSLionel Sambuc   case UO_Minus: return OO_Minus;
1104f4a2713aSLionel Sambuc   case UO_Not: return OO_Tilde;
1105f4a2713aSLionel Sambuc   case UO_LNot: return OO_Exclaim;
1106f4a2713aSLionel Sambuc   default: return OO_None;
1107f4a2713aSLionel Sambuc   }
1108f4a2713aSLionel Sambuc }
1109f4a2713aSLionel Sambuc 
1110f4a2713aSLionel Sambuc 
1111f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1112f4a2713aSLionel Sambuc // Postfix Operators.
1113f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1114f4a2713aSLionel Sambuc 
CallExpr(const ASTContext & C,StmtClass SC,Expr * fn,unsigned NumPreArgs,ArrayRef<Expr * > args,QualType t,ExprValueKind VK,SourceLocation rparenloc)1115f4a2713aSLionel Sambuc CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1116f4a2713aSLionel Sambuc                    unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1117f4a2713aSLionel Sambuc                    ExprValueKind VK, SourceLocation rparenloc)
1118f4a2713aSLionel Sambuc   : Expr(SC, t, VK, OK_Ordinary,
1119f4a2713aSLionel Sambuc          fn->isTypeDependent(),
1120f4a2713aSLionel Sambuc          fn->isValueDependent(),
1121f4a2713aSLionel Sambuc          fn->isInstantiationDependent(),
1122f4a2713aSLionel Sambuc          fn->containsUnexpandedParameterPack()),
1123f4a2713aSLionel Sambuc     NumArgs(args.size()) {
1124f4a2713aSLionel Sambuc 
1125f4a2713aSLionel Sambuc   SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
1126f4a2713aSLionel Sambuc   SubExprs[FN] = fn;
1127f4a2713aSLionel Sambuc   for (unsigned i = 0; i != args.size(); ++i) {
1128f4a2713aSLionel Sambuc     if (args[i]->isTypeDependent())
1129f4a2713aSLionel Sambuc       ExprBits.TypeDependent = true;
1130f4a2713aSLionel Sambuc     if (args[i]->isValueDependent())
1131f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
1132f4a2713aSLionel Sambuc     if (args[i]->isInstantiationDependent())
1133f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
1134f4a2713aSLionel Sambuc     if (args[i]->containsUnexpandedParameterPack())
1135f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
1136f4a2713aSLionel Sambuc 
1137f4a2713aSLionel Sambuc     SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
1138f4a2713aSLionel Sambuc   }
1139f4a2713aSLionel Sambuc 
1140f4a2713aSLionel Sambuc   CallExprBits.NumPreArgs = NumPreArgs;
1141f4a2713aSLionel Sambuc   RParenLoc = rparenloc;
1142f4a2713aSLionel Sambuc }
1143f4a2713aSLionel Sambuc 
CallExpr(const ASTContext & C,Expr * fn,ArrayRef<Expr * > args,QualType t,ExprValueKind VK,SourceLocation rparenloc)1144f4a2713aSLionel Sambuc CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
1145f4a2713aSLionel Sambuc                    QualType t, ExprValueKind VK, SourceLocation rparenloc)
1146f4a2713aSLionel Sambuc   : Expr(CallExprClass, t, VK, OK_Ordinary,
1147f4a2713aSLionel Sambuc          fn->isTypeDependent(),
1148f4a2713aSLionel Sambuc          fn->isValueDependent(),
1149f4a2713aSLionel Sambuc          fn->isInstantiationDependent(),
1150f4a2713aSLionel Sambuc          fn->containsUnexpandedParameterPack()),
1151f4a2713aSLionel Sambuc     NumArgs(args.size()) {
1152f4a2713aSLionel Sambuc 
1153f4a2713aSLionel Sambuc   SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
1154f4a2713aSLionel Sambuc   SubExprs[FN] = fn;
1155f4a2713aSLionel Sambuc   for (unsigned i = 0; i != args.size(); ++i) {
1156f4a2713aSLionel Sambuc     if (args[i]->isTypeDependent())
1157f4a2713aSLionel Sambuc       ExprBits.TypeDependent = true;
1158f4a2713aSLionel Sambuc     if (args[i]->isValueDependent())
1159f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
1160f4a2713aSLionel Sambuc     if (args[i]->isInstantiationDependent())
1161f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
1162f4a2713aSLionel Sambuc     if (args[i]->containsUnexpandedParameterPack())
1163f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
1164f4a2713aSLionel Sambuc 
1165f4a2713aSLionel Sambuc     SubExprs[i+PREARGS_START] = args[i];
1166f4a2713aSLionel Sambuc   }
1167f4a2713aSLionel Sambuc 
1168f4a2713aSLionel Sambuc   CallExprBits.NumPreArgs = 0;
1169f4a2713aSLionel Sambuc   RParenLoc = rparenloc;
1170f4a2713aSLionel Sambuc }
1171f4a2713aSLionel Sambuc 
CallExpr(const ASTContext & C,StmtClass SC,EmptyShell Empty)1172f4a2713aSLionel Sambuc CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
1173*0a6a1f1dSLionel Sambuc   : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
1174f4a2713aSLionel Sambuc   // FIXME: Why do we allocate this?
1175f4a2713aSLionel Sambuc   SubExprs = new (C) Stmt*[PREARGS_START];
1176f4a2713aSLionel Sambuc   CallExprBits.NumPreArgs = 0;
1177f4a2713aSLionel Sambuc }
1178f4a2713aSLionel Sambuc 
CallExpr(const ASTContext & C,StmtClass SC,unsigned NumPreArgs,EmptyShell Empty)1179f4a2713aSLionel Sambuc CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
1180f4a2713aSLionel Sambuc                    EmptyShell Empty)
1181*0a6a1f1dSLionel Sambuc   : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
1182f4a2713aSLionel Sambuc   // FIXME: Why do we allocate this?
1183f4a2713aSLionel Sambuc   SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1184f4a2713aSLionel Sambuc   CallExprBits.NumPreArgs = NumPreArgs;
1185f4a2713aSLionel Sambuc }
1186f4a2713aSLionel Sambuc 
getCalleeDecl()1187f4a2713aSLionel Sambuc Decl *CallExpr::getCalleeDecl() {
1188f4a2713aSLionel Sambuc   Expr *CEE = getCallee()->IgnoreParenImpCasts();
1189f4a2713aSLionel Sambuc 
1190f4a2713aSLionel Sambuc   while (SubstNonTypeTemplateParmExpr *NTTP
1191f4a2713aSLionel Sambuc                                 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1192f4a2713aSLionel Sambuc     CEE = NTTP->getReplacement()->IgnoreParenCasts();
1193f4a2713aSLionel Sambuc   }
1194f4a2713aSLionel Sambuc 
1195f4a2713aSLionel Sambuc   // If we're calling a dereference, look at the pointer instead.
1196f4a2713aSLionel Sambuc   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1197f4a2713aSLionel Sambuc     if (BO->isPtrMemOp())
1198f4a2713aSLionel Sambuc       CEE = BO->getRHS()->IgnoreParenCasts();
1199f4a2713aSLionel Sambuc   } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1200f4a2713aSLionel Sambuc     if (UO->getOpcode() == UO_Deref)
1201f4a2713aSLionel Sambuc       CEE = UO->getSubExpr()->IgnoreParenCasts();
1202f4a2713aSLionel Sambuc   }
1203f4a2713aSLionel Sambuc   if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
1204f4a2713aSLionel Sambuc     return DRE->getDecl();
1205f4a2713aSLionel Sambuc   if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1206f4a2713aSLionel Sambuc     return ME->getMemberDecl();
1207f4a2713aSLionel Sambuc 
1208*0a6a1f1dSLionel Sambuc   return nullptr;
1209f4a2713aSLionel Sambuc }
1210f4a2713aSLionel Sambuc 
getDirectCallee()1211f4a2713aSLionel Sambuc FunctionDecl *CallExpr::getDirectCallee() {
1212f4a2713aSLionel Sambuc   return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
1213f4a2713aSLionel Sambuc }
1214f4a2713aSLionel Sambuc 
1215f4a2713aSLionel Sambuc /// setNumArgs - This changes the number of arguments present in this call.
1216f4a2713aSLionel Sambuc /// Any orphaned expressions are deleted by this, and any new operands are set
1217f4a2713aSLionel Sambuc /// to null.
setNumArgs(const ASTContext & C,unsigned NumArgs)1218f4a2713aSLionel Sambuc void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
1219f4a2713aSLionel Sambuc   // No change, just return.
1220f4a2713aSLionel Sambuc   if (NumArgs == getNumArgs()) return;
1221f4a2713aSLionel Sambuc 
1222f4a2713aSLionel Sambuc   // If shrinking # arguments, just delete the extras and forgot them.
1223f4a2713aSLionel Sambuc   if (NumArgs < getNumArgs()) {
1224f4a2713aSLionel Sambuc     this->NumArgs = NumArgs;
1225f4a2713aSLionel Sambuc     return;
1226f4a2713aSLionel Sambuc   }
1227f4a2713aSLionel Sambuc 
1228f4a2713aSLionel Sambuc   // Otherwise, we are growing the # arguments.  New an bigger argument array.
1229f4a2713aSLionel Sambuc   unsigned NumPreArgs = getNumPreArgs();
1230f4a2713aSLionel Sambuc   Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
1231f4a2713aSLionel Sambuc   // Copy over args.
1232f4a2713aSLionel Sambuc   for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
1233f4a2713aSLionel Sambuc     NewSubExprs[i] = SubExprs[i];
1234f4a2713aSLionel Sambuc   // Null out new args.
1235f4a2713aSLionel Sambuc   for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1236f4a2713aSLionel Sambuc        i != NumArgs+PREARGS_START+NumPreArgs; ++i)
1237*0a6a1f1dSLionel Sambuc     NewSubExprs[i] = nullptr;
1238f4a2713aSLionel Sambuc 
1239f4a2713aSLionel Sambuc   if (SubExprs) C.Deallocate(SubExprs);
1240f4a2713aSLionel Sambuc   SubExprs = NewSubExprs;
1241f4a2713aSLionel Sambuc   this->NumArgs = NumArgs;
1242f4a2713aSLionel Sambuc }
1243f4a2713aSLionel Sambuc 
1244*0a6a1f1dSLionel Sambuc /// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
1245f4a2713aSLionel Sambuc /// not, return 0.
getBuiltinCallee() const1246*0a6a1f1dSLionel Sambuc unsigned CallExpr::getBuiltinCallee() const {
1247f4a2713aSLionel Sambuc   // All simple function calls (e.g. func()) are implicitly cast to pointer to
1248f4a2713aSLionel Sambuc   // function. As a result, we try and obtain the DeclRefExpr from the
1249f4a2713aSLionel Sambuc   // ImplicitCastExpr.
1250f4a2713aSLionel Sambuc   const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1251f4a2713aSLionel Sambuc   if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
1252f4a2713aSLionel Sambuc     return 0;
1253f4a2713aSLionel Sambuc 
1254f4a2713aSLionel Sambuc   const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1255f4a2713aSLionel Sambuc   if (!DRE)
1256f4a2713aSLionel Sambuc     return 0;
1257f4a2713aSLionel Sambuc 
1258f4a2713aSLionel Sambuc   const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1259f4a2713aSLionel Sambuc   if (!FDecl)
1260f4a2713aSLionel Sambuc     return 0;
1261f4a2713aSLionel Sambuc 
1262f4a2713aSLionel Sambuc   if (!FDecl->getIdentifier())
1263f4a2713aSLionel Sambuc     return 0;
1264f4a2713aSLionel Sambuc 
1265f4a2713aSLionel Sambuc   return FDecl->getBuiltinID();
1266f4a2713aSLionel Sambuc }
1267f4a2713aSLionel Sambuc 
isUnevaluatedBuiltinCall(ASTContext & Ctx) const1268f4a2713aSLionel Sambuc bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1269*0a6a1f1dSLionel Sambuc   if (unsigned BI = getBuiltinCallee())
1270f4a2713aSLionel Sambuc     return Ctx.BuiltinInfo.isUnevaluated(BI);
1271f4a2713aSLionel Sambuc   return false;
1272f4a2713aSLionel Sambuc }
1273f4a2713aSLionel Sambuc 
getCallReturnType() const1274f4a2713aSLionel Sambuc QualType CallExpr::getCallReturnType() const {
1275f4a2713aSLionel Sambuc   QualType CalleeType = getCallee()->getType();
1276f4a2713aSLionel Sambuc   if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
1277f4a2713aSLionel Sambuc     CalleeType = FnTypePtr->getPointeeType();
1278f4a2713aSLionel Sambuc   else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
1279f4a2713aSLionel Sambuc     CalleeType = BPT->getPointeeType();
1280f4a2713aSLionel Sambuc   else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1281f4a2713aSLionel Sambuc     // This should never be overloaded and so should never return null.
1282f4a2713aSLionel Sambuc     CalleeType = Expr::findBoundMemberType(getCallee());
1283f4a2713aSLionel Sambuc 
1284f4a2713aSLionel Sambuc   const FunctionType *FnType = CalleeType->castAs<FunctionType>();
1285*0a6a1f1dSLionel Sambuc   return FnType->getReturnType();
1286f4a2713aSLionel Sambuc }
1287f4a2713aSLionel Sambuc 
getLocStart() const1288f4a2713aSLionel Sambuc SourceLocation CallExpr::getLocStart() const {
1289f4a2713aSLionel Sambuc   if (isa<CXXOperatorCallExpr>(this))
1290f4a2713aSLionel Sambuc     return cast<CXXOperatorCallExpr>(this)->getLocStart();
1291f4a2713aSLionel Sambuc 
1292f4a2713aSLionel Sambuc   SourceLocation begin = getCallee()->getLocStart();
1293*0a6a1f1dSLionel Sambuc   if (begin.isInvalid() && getNumArgs() > 0 && getArg(0))
1294f4a2713aSLionel Sambuc     begin = getArg(0)->getLocStart();
1295f4a2713aSLionel Sambuc   return begin;
1296f4a2713aSLionel Sambuc }
getLocEnd() const1297f4a2713aSLionel Sambuc SourceLocation CallExpr::getLocEnd() const {
1298f4a2713aSLionel Sambuc   if (isa<CXXOperatorCallExpr>(this))
1299f4a2713aSLionel Sambuc     return cast<CXXOperatorCallExpr>(this)->getLocEnd();
1300f4a2713aSLionel Sambuc 
1301f4a2713aSLionel Sambuc   SourceLocation end = getRParenLoc();
1302*0a6a1f1dSLionel Sambuc   if (end.isInvalid() && getNumArgs() > 0 && getArg(getNumArgs() - 1))
1303f4a2713aSLionel Sambuc     end = getArg(getNumArgs() - 1)->getLocEnd();
1304f4a2713aSLionel Sambuc   return end;
1305f4a2713aSLionel Sambuc }
1306f4a2713aSLionel Sambuc 
Create(const ASTContext & C,QualType type,SourceLocation OperatorLoc,TypeSourceInfo * tsi,ArrayRef<OffsetOfNode> comps,ArrayRef<Expr * > exprs,SourceLocation RParenLoc)1307f4a2713aSLionel Sambuc OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
1308f4a2713aSLionel Sambuc                                    SourceLocation OperatorLoc,
1309f4a2713aSLionel Sambuc                                    TypeSourceInfo *tsi,
1310f4a2713aSLionel Sambuc                                    ArrayRef<OffsetOfNode> comps,
1311f4a2713aSLionel Sambuc                                    ArrayRef<Expr*> exprs,
1312f4a2713aSLionel Sambuc                                    SourceLocation RParenLoc) {
1313f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1314f4a2713aSLionel Sambuc                          sizeof(OffsetOfNode) * comps.size() +
1315f4a2713aSLionel Sambuc                          sizeof(Expr*) * exprs.size());
1316f4a2713aSLionel Sambuc 
1317f4a2713aSLionel Sambuc   return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1318f4a2713aSLionel Sambuc                                 RParenLoc);
1319f4a2713aSLionel Sambuc }
1320f4a2713aSLionel Sambuc 
CreateEmpty(const ASTContext & C,unsigned numComps,unsigned numExprs)1321f4a2713aSLionel Sambuc OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
1322f4a2713aSLionel Sambuc                                         unsigned numComps, unsigned numExprs) {
1323f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1324f4a2713aSLionel Sambuc                          sizeof(OffsetOfNode) * numComps +
1325f4a2713aSLionel Sambuc                          sizeof(Expr*) * numExprs);
1326f4a2713aSLionel Sambuc   return new (Mem) OffsetOfExpr(numComps, numExprs);
1327f4a2713aSLionel Sambuc }
1328f4a2713aSLionel Sambuc 
OffsetOfExpr(const ASTContext & C,QualType type,SourceLocation OperatorLoc,TypeSourceInfo * tsi,ArrayRef<OffsetOfNode> comps,ArrayRef<Expr * > exprs,SourceLocation RParenLoc)1329f4a2713aSLionel Sambuc OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
1330f4a2713aSLionel Sambuc                            SourceLocation OperatorLoc, TypeSourceInfo *tsi,
1331f4a2713aSLionel Sambuc                            ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
1332f4a2713aSLionel Sambuc                            SourceLocation RParenLoc)
1333f4a2713aSLionel Sambuc   : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1334f4a2713aSLionel Sambuc          /*TypeDependent=*/false,
1335f4a2713aSLionel Sambuc          /*ValueDependent=*/tsi->getType()->isDependentType(),
1336f4a2713aSLionel Sambuc          tsi->getType()->isInstantiationDependentType(),
1337f4a2713aSLionel Sambuc          tsi->getType()->containsUnexpandedParameterPack()),
1338f4a2713aSLionel Sambuc     OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
1339f4a2713aSLionel Sambuc     NumComps(comps.size()), NumExprs(exprs.size())
1340f4a2713aSLionel Sambuc {
1341f4a2713aSLionel Sambuc   for (unsigned i = 0; i != comps.size(); ++i) {
1342f4a2713aSLionel Sambuc     setComponent(i, comps[i]);
1343f4a2713aSLionel Sambuc   }
1344f4a2713aSLionel Sambuc 
1345f4a2713aSLionel Sambuc   for (unsigned i = 0; i != exprs.size(); ++i) {
1346f4a2713aSLionel Sambuc     if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
1347f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
1348f4a2713aSLionel Sambuc     if (exprs[i]->containsUnexpandedParameterPack())
1349f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
1350f4a2713aSLionel Sambuc 
1351f4a2713aSLionel Sambuc     setIndexExpr(i, exprs[i]);
1352f4a2713aSLionel Sambuc   }
1353f4a2713aSLionel Sambuc }
1354f4a2713aSLionel Sambuc 
getFieldName() const1355f4a2713aSLionel Sambuc IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1356f4a2713aSLionel Sambuc   assert(getKind() == Field || getKind() == Identifier);
1357f4a2713aSLionel Sambuc   if (getKind() == Field)
1358f4a2713aSLionel Sambuc     return getField()->getIdentifier();
1359f4a2713aSLionel Sambuc 
1360f4a2713aSLionel Sambuc   return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1361f4a2713aSLionel Sambuc }
1362f4a2713aSLionel Sambuc 
Create(const ASTContext & C,Expr * base,bool isarrow,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,ValueDecl * memberdecl,DeclAccessPair founddecl,DeclarationNameInfo nameinfo,const TemplateArgumentListInfo * targs,QualType ty,ExprValueKind vk,ExprObjectKind ok)1363f4a2713aSLionel Sambuc MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
1364f4a2713aSLionel Sambuc                                NestedNameSpecifierLoc QualifierLoc,
1365f4a2713aSLionel Sambuc                                SourceLocation TemplateKWLoc,
1366f4a2713aSLionel Sambuc                                ValueDecl *memberdecl,
1367f4a2713aSLionel Sambuc                                DeclAccessPair founddecl,
1368f4a2713aSLionel Sambuc                                DeclarationNameInfo nameinfo,
1369f4a2713aSLionel Sambuc                                const TemplateArgumentListInfo *targs,
1370f4a2713aSLionel Sambuc                                QualType ty,
1371f4a2713aSLionel Sambuc                                ExprValueKind vk,
1372f4a2713aSLionel Sambuc                                ExprObjectKind ok) {
1373f4a2713aSLionel Sambuc   std::size_t Size = sizeof(MemberExpr);
1374f4a2713aSLionel Sambuc 
1375f4a2713aSLionel Sambuc   bool hasQualOrFound = (QualifierLoc ||
1376f4a2713aSLionel Sambuc                          founddecl.getDecl() != memberdecl ||
1377f4a2713aSLionel Sambuc                          founddecl.getAccess() != memberdecl->getAccess());
1378f4a2713aSLionel Sambuc   if (hasQualOrFound)
1379f4a2713aSLionel Sambuc     Size += sizeof(MemberNameQualifier);
1380f4a2713aSLionel Sambuc 
1381f4a2713aSLionel Sambuc   if (targs)
1382f4a2713aSLionel Sambuc     Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1383f4a2713aSLionel Sambuc   else if (TemplateKWLoc.isValid())
1384f4a2713aSLionel Sambuc     Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
1385f4a2713aSLionel Sambuc 
1386f4a2713aSLionel Sambuc   void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
1387f4a2713aSLionel Sambuc   MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1388f4a2713aSLionel Sambuc                                        ty, vk, ok);
1389f4a2713aSLionel Sambuc 
1390f4a2713aSLionel Sambuc   if (hasQualOrFound) {
1391f4a2713aSLionel Sambuc     // FIXME: Wrong. We should be looking at the member declaration we found.
1392f4a2713aSLionel Sambuc     if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
1393f4a2713aSLionel Sambuc       E->setValueDependent(true);
1394f4a2713aSLionel Sambuc       E->setTypeDependent(true);
1395f4a2713aSLionel Sambuc       E->setInstantiationDependent(true);
1396f4a2713aSLionel Sambuc     }
1397f4a2713aSLionel Sambuc     else if (QualifierLoc &&
1398f4a2713aSLionel Sambuc              QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1399f4a2713aSLionel Sambuc       E->setInstantiationDependent(true);
1400f4a2713aSLionel Sambuc 
1401f4a2713aSLionel Sambuc     E->HasQualifierOrFoundDecl = true;
1402f4a2713aSLionel Sambuc 
1403f4a2713aSLionel Sambuc     MemberNameQualifier *NQ = E->getMemberQualifier();
1404f4a2713aSLionel Sambuc     NQ->QualifierLoc = QualifierLoc;
1405f4a2713aSLionel Sambuc     NQ->FoundDecl = founddecl;
1406f4a2713aSLionel Sambuc   }
1407f4a2713aSLionel Sambuc 
1408f4a2713aSLionel Sambuc   E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1409f4a2713aSLionel Sambuc 
1410f4a2713aSLionel Sambuc   if (targs) {
1411f4a2713aSLionel Sambuc     bool Dependent = false;
1412f4a2713aSLionel Sambuc     bool InstantiationDependent = false;
1413f4a2713aSLionel Sambuc     bool ContainsUnexpandedParameterPack = false;
1414f4a2713aSLionel Sambuc     E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1415f4a2713aSLionel Sambuc                                                   Dependent,
1416f4a2713aSLionel Sambuc                                                   InstantiationDependent,
1417f4a2713aSLionel Sambuc                                              ContainsUnexpandedParameterPack);
1418f4a2713aSLionel Sambuc     if (InstantiationDependent)
1419f4a2713aSLionel Sambuc       E->setInstantiationDependent(true);
1420f4a2713aSLionel Sambuc   } else if (TemplateKWLoc.isValid()) {
1421f4a2713aSLionel Sambuc     E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
1422f4a2713aSLionel Sambuc   }
1423f4a2713aSLionel Sambuc 
1424f4a2713aSLionel Sambuc   return E;
1425f4a2713aSLionel Sambuc }
1426f4a2713aSLionel Sambuc 
getLocStart() const1427f4a2713aSLionel Sambuc SourceLocation MemberExpr::getLocStart() const {
1428f4a2713aSLionel Sambuc   if (isImplicitAccess()) {
1429f4a2713aSLionel Sambuc     if (hasQualifier())
1430f4a2713aSLionel Sambuc       return getQualifierLoc().getBeginLoc();
1431f4a2713aSLionel Sambuc     return MemberLoc;
1432f4a2713aSLionel Sambuc   }
1433f4a2713aSLionel Sambuc 
1434f4a2713aSLionel Sambuc   // FIXME: We don't want this to happen. Rather, we should be able to
1435f4a2713aSLionel Sambuc   // detect all kinds of implicit accesses more cleanly.
1436f4a2713aSLionel Sambuc   SourceLocation BaseStartLoc = getBase()->getLocStart();
1437f4a2713aSLionel Sambuc   if (BaseStartLoc.isValid())
1438f4a2713aSLionel Sambuc     return BaseStartLoc;
1439f4a2713aSLionel Sambuc   return MemberLoc;
1440f4a2713aSLionel Sambuc }
getLocEnd() const1441f4a2713aSLionel Sambuc SourceLocation MemberExpr::getLocEnd() const {
1442f4a2713aSLionel Sambuc   SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
1443f4a2713aSLionel Sambuc   if (hasExplicitTemplateArgs())
1444f4a2713aSLionel Sambuc     EndLoc = getRAngleLoc();
1445f4a2713aSLionel Sambuc   else if (EndLoc.isInvalid())
1446f4a2713aSLionel Sambuc     EndLoc = getBase()->getLocEnd();
1447f4a2713aSLionel Sambuc   return EndLoc;
1448f4a2713aSLionel Sambuc }
1449f4a2713aSLionel Sambuc 
CastConsistency() const1450*0a6a1f1dSLionel Sambuc bool CastExpr::CastConsistency() const {
1451f4a2713aSLionel Sambuc   switch (getCastKind()) {
1452f4a2713aSLionel Sambuc   case CK_DerivedToBase:
1453f4a2713aSLionel Sambuc   case CK_UncheckedDerivedToBase:
1454f4a2713aSLionel Sambuc   case CK_DerivedToBaseMemberPointer:
1455f4a2713aSLionel Sambuc   case CK_BaseToDerived:
1456f4a2713aSLionel Sambuc   case CK_BaseToDerivedMemberPointer:
1457f4a2713aSLionel Sambuc     assert(!path_empty() && "Cast kind should have a base path!");
1458f4a2713aSLionel Sambuc     break;
1459f4a2713aSLionel Sambuc 
1460f4a2713aSLionel Sambuc   case CK_CPointerToObjCPointerCast:
1461f4a2713aSLionel Sambuc     assert(getType()->isObjCObjectPointerType());
1462f4a2713aSLionel Sambuc     assert(getSubExpr()->getType()->isPointerType());
1463f4a2713aSLionel Sambuc     goto CheckNoBasePath;
1464f4a2713aSLionel Sambuc 
1465f4a2713aSLionel Sambuc   case CK_BlockPointerToObjCPointerCast:
1466f4a2713aSLionel Sambuc     assert(getType()->isObjCObjectPointerType());
1467f4a2713aSLionel Sambuc     assert(getSubExpr()->getType()->isBlockPointerType());
1468f4a2713aSLionel Sambuc     goto CheckNoBasePath;
1469f4a2713aSLionel Sambuc 
1470f4a2713aSLionel Sambuc   case CK_ReinterpretMemberPointer:
1471f4a2713aSLionel Sambuc     assert(getType()->isMemberPointerType());
1472f4a2713aSLionel Sambuc     assert(getSubExpr()->getType()->isMemberPointerType());
1473f4a2713aSLionel Sambuc     goto CheckNoBasePath;
1474f4a2713aSLionel Sambuc 
1475f4a2713aSLionel Sambuc   case CK_BitCast:
1476f4a2713aSLionel Sambuc     // Arbitrary casts to C pointer types count as bitcasts.
1477f4a2713aSLionel Sambuc     // Otherwise, we should only have block and ObjC pointer casts
1478f4a2713aSLionel Sambuc     // here if they stay within the type kind.
1479f4a2713aSLionel Sambuc     if (!getType()->isPointerType()) {
1480f4a2713aSLionel Sambuc       assert(getType()->isObjCObjectPointerType() ==
1481f4a2713aSLionel Sambuc              getSubExpr()->getType()->isObjCObjectPointerType());
1482f4a2713aSLionel Sambuc       assert(getType()->isBlockPointerType() ==
1483f4a2713aSLionel Sambuc              getSubExpr()->getType()->isBlockPointerType());
1484f4a2713aSLionel Sambuc     }
1485f4a2713aSLionel Sambuc     goto CheckNoBasePath;
1486f4a2713aSLionel Sambuc 
1487f4a2713aSLionel Sambuc   case CK_AnyPointerToBlockPointerCast:
1488f4a2713aSLionel Sambuc     assert(getType()->isBlockPointerType());
1489f4a2713aSLionel Sambuc     assert(getSubExpr()->getType()->isAnyPointerType() &&
1490f4a2713aSLionel Sambuc            !getSubExpr()->getType()->isBlockPointerType());
1491f4a2713aSLionel Sambuc     goto CheckNoBasePath;
1492f4a2713aSLionel Sambuc 
1493f4a2713aSLionel Sambuc   case CK_CopyAndAutoreleaseBlockObject:
1494f4a2713aSLionel Sambuc     assert(getType()->isBlockPointerType());
1495f4a2713aSLionel Sambuc     assert(getSubExpr()->getType()->isBlockPointerType());
1496f4a2713aSLionel Sambuc     goto CheckNoBasePath;
1497f4a2713aSLionel Sambuc 
1498f4a2713aSLionel Sambuc   case CK_FunctionToPointerDecay:
1499f4a2713aSLionel Sambuc     assert(getType()->isPointerType());
1500f4a2713aSLionel Sambuc     assert(getSubExpr()->getType()->isFunctionType());
1501f4a2713aSLionel Sambuc     goto CheckNoBasePath;
1502f4a2713aSLionel Sambuc 
1503*0a6a1f1dSLionel Sambuc   case CK_AddressSpaceConversion:
1504*0a6a1f1dSLionel Sambuc     assert(getType()->isPointerType());
1505*0a6a1f1dSLionel Sambuc     assert(getSubExpr()->getType()->isPointerType());
1506*0a6a1f1dSLionel Sambuc     assert(getType()->getPointeeType().getAddressSpace() !=
1507*0a6a1f1dSLionel Sambuc            getSubExpr()->getType()->getPointeeType().getAddressSpace());
1508f4a2713aSLionel Sambuc   // These should not have an inheritance path.
1509f4a2713aSLionel Sambuc   case CK_Dynamic:
1510f4a2713aSLionel Sambuc   case CK_ToUnion:
1511f4a2713aSLionel Sambuc   case CK_ArrayToPointerDecay:
1512f4a2713aSLionel Sambuc   case CK_NullToMemberPointer:
1513f4a2713aSLionel Sambuc   case CK_NullToPointer:
1514f4a2713aSLionel Sambuc   case CK_ConstructorConversion:
1515f4a2713aSLionel Sambuc   case CK_IntegralToPointer:
1516f4a2713aSLionel Sambuc   case CK_PointerToIntegral:
1517f4a2713aSLionel Sambuc   case CK_ToVoid:
1518f4a2713aSLionel Sambuc   case CK_VectorSplat:
1519f4a2713aSLionel Sambuc   case CK_IntegralCast:
1520f4a2713aSLionel Sambuc   case CK_IntegralToFloating:
1521f4a2713aSLionel Sambuc   case CK_FloatingToIntegral:
1522f4a2713aSLionel Sambuc   case CK_FloatingCast:
1523f4a2713aSLionel Sambuc   case CK_ObjCObjectLValueCast:
1524f4a2713aSLionel Sambuc   case CK_FloatingRealToComplex:
1525f4a2713aSLionel Sambuc   case CK_FloatingComplexToReal:
1526f4a2713aSLionel Sambuc   case CK_FloatingComplexCast:
1527f4a2713aSLionel Sambuc   case CK_FloatingComplexToIntegralComplex:
1528f4a2713aSLionel Sambuc   case CK_IntegralRealToComplex:
1529f4a2713aSLionel Sambuc   case CK_IntegralComplexToReal:
1530f4a2713aSLionel Sambuc   case CK_IntegralComplexCast:
1531f4a2713aSLionel Sambuc   case CK_IntegralComplexToFloatingComplex:
1532f4a2713aSLionel Sambuc   case CK_ARCProduceObject:
1533f4a2713aSLionel Sambuc   case CK_ARCConsumeObject:
1534f4a2713aSLionel Sambuc   case CK_ARCReclaimReturnedObject:
1535f4a2713aSLionel Sambuc   case CK_ARCExtendBlockObject:
1536f4a2713aSLionel Sambuc   case CK_ZeroToOCLEvent:
1537f4a2713aSLionel Sambuc     assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1538f4a2713aSLionel Sambuc     goto CheckNoBasePath;
1539f4a2713aSLionel Sambuc 
1540f4a2713aSLionel Sambuc   case CK_Dependent:
1541f4a2713aSLionel Sambuc   case CK_LValueToRValue:
1542f4a2713aSLionel Sambuc   case CK_NoOp:
1543f4a2713aSLionel Sambuc   case CK_AtomicToNonAtomic:
1544f4a2713aSLionel Sambuc   case CK_NonAtomicToAtomic:
1545f4a2713aSLionel Sambuc   case CK_PointerToBoolean:
1546f4a2713aSLionel Sambuc   case CK_IntegralToBoolean:
1547f4a2713aSLionel Sambuc   case CK_FloatingToBoolean:
1548f4a2713aSLionel Sambuc   case CK_MemberPointerToBoolean:
1549f4a2713aSLionel Sambuc   case CK_FloatingComplexToBoolean:
1550f4a2713aSLionel Sambuc   case CK_IntegralComplexToBoolean:
1551f4a2713aSLionel Sambuc   case CK_LValueBitCast:            // -> bool&
1552f4a2713aSLionel Sambuc   case CK_UserDefinedConversion:    // operator bool()
1553f4a2713aSLionel Sambuc   case CK_BuiltinFnToFnPtr:
1554f4a2713aSLionel Sambuc   CheckNoBasePath:
1555f4a2713aSLionel Sambuc     assert(path_empty() && "Cast kind should not have a base path!");
1556f4a2713aSLionel Sambuc     break;
1557f4a2713aSLionel Sambuc   }
1558*0a6a1f1dSLionel Sambuc   return true;
1559f4a2713aSLionel Sambuc }
1560f4a2713aSLionel Sambuc 
getCastKindName() const1561f4a2713aSLionel Sambuc const char *CastExpr::getCastKindName() const {
1562f4a2713aSLionel Sambuc   switch (getCastKind()) {
1563f4a2713aSLionel Sambuc   case CK_Dependent:
1564f4a2713aSLionel Sambuc     return "Dependent";
1565f4a2713aSLionel Sambuc   case CK_BitCast:
1566f4a2713aSLionel Sambuc     return "BitCast";
1567f4a2713aSLionel Sambuc   case CK_LValueBitCast:
1568f4a2713aSLionel Sambuc     return "LValueBitCast";
1569f4a2713aSLionel Sambuc   case CK_LValueToRValue:
1570f4a2713aSLionel Sambuc     return "LValueToRValue";
1571f4a2713aSLionel Sambuc   case CK_NoOp:
1572f4a2713aSLionel Sambuc     return "NoOp";
1573f4a2713aSLionel Sambuc   case CK_BaseToDerived:
1574f4a2713aSLionel Sambuc     return "BaseToDerived";
1575f4a2713aSLionel Sambuc   case CK_DerivedToBase:
1576f4a2713aSLionel Sambuc     return "DerivedToBase";
1577f4a2713aSLionel Sambuc   case CK_UncheckedDerivedToBase:
1578f4a2713aSLionel Sambuc     return "UncheckedDerivedToBase";
1579f4a2713aSLionel Sambuc   case CK_Dynamic:
1580f4a2713aSLionel Sambuc     return "Dynamic";
1581f4a2713aSLionel Sambuc   case CK_ToUnion:
1582f4a2713aSLionel Sambuc     return "ToUnion";
1583f4a2713aSLionel Sambuc   case CK_ArrayToPointerDecay:
1584f4a2713aSLionel Sambuc     return "ArrayToPointerDecay";
1585f4a2713aSLionel Sambuc   case CK_FunctionToPointerDecay:
1586f4a2713aSLionel Sambuc     return "FunctionToPointerDecay";
1587f4a2713aSLionel Sambuc   case CK_NullToMemberPointer:
1588f4a2713aSLionel Sambuc     return "NullToMemberPointer";
1589f4a2713aSLionel Sambuc   case CK_NullToPointer:
1590f4a2713aSLionel Sambuc     return "NullToPointer";
1591f4a2713aSLionel Sambuc   case CK_BaseToDerivedMemberPointer:
1592f4a2713aSLionel Sambuc     return "BaseToDerivedMemberPointer";
1593f4a2713aSLionel Sambuc   case CK_DerivedToBaseMemberPointer:
1594f4a2713aSLionel Sambuc     return "DerivedToBaseMemberPointer";
1595f4a2713aSLionel Sambuc   case CK_ReinterpretMemberPointer:
1596f4a2713aSLionel Sambuc     return "ReinterpretMemberPointer";
1597f4a2713aSLionel Sambuc   case CK_UserDefinedConversion:
1598f4a2713aSLionel Sambuc     return "UserDefinedConversion";
1599f4a2713aSLionel Sambuc   case CK_ConstructorConversion:
1600f4a2713aSLionel Sambuc     return "ConstructorConversion";
1601f4a2713aSLionel Sambuc   case CK_IntegralToPointer:
1602f4a2713aSLionel Sambuc     return "IntegralToPointer";
1603f4a2713aSLionel Sambuc   case CK_PointerToIntegral:
1604f4a2713aSLionel Sambuc     return "PointerToIntegral";
1605f4a2713aSLionel Sambuc   case CK_PointerToBoolean:
1606f4a2713aSLionel Sambuc     return "PointerToBoolean";
1607f4a2713aSLionel Sambuc   case CK_ToVoid:
1608f4a2713aSLionel Sambuc     return "ToVoid";
1609f4a2713aSLionel Sambuc   case CK_VectorSplat:
1610f4a2713aSLionel Sambuc     return "VectorSplat";
1611f4a2713aSLionel Sambuc   case CK_IntegralCast:
1612f4a2713aSLionel Sambuc     return "IntegralCast";
1613f4a2713aSLionel Sambuc   case CK_IntegralToBoolean:
1614f4a2713aSLionel Sambuc     return "IntegralToBoolean";
1615f4a2713aSLionel Sambuc   case CK_IntegralToFloating:
1616f4a2713aSLionel Sambuc     return "IntegralToFloating";
1617f4a2713aSLionel Sambuc   case CK_FloatingToIntegral:
1618f4a2713aSLionel Sambuc     return "FloatingToIntegral";
1619f4a2713aSLionel Sambuc   case CK_FloatingCast:
1620f4a2713aSLionel Sambuc     return "FloatingCast";
1621f4a2713aSLionel Sambuc   case CK_FloatingToBoolean:
1622f4a2713aSLionel Sambuc     return "FloatingToBoolean";
1623f4a2713aSLionel Sambuc   case CK_MemberPointerToBoolean:
1624f4a2713aSLionel Sambuc     return "MemberPointerToBoolean";
1625f4a2713aSLionel Sambuc   case CK_CPointerToObjCPointerCast:
1626f4a2713aSLionel Sambuc     return "CPointerToObjCPointerCast";
1627f4a2713aSLionel Sambuc   case CK_BlockPointerToObjCPointerCast:
1628f4a2713aSLionel Sambuc     return "BlockPointerToObjCPointerCast";
1629f4a2713aSLionel Sambuc   case CK_AnyPointerToBlockPointerCast:
1630f4a2713aSLionel Sambuc     return "AnyPointerToBlockPointerCast";
1631f4a2713aSLionel Sambuc   case CK_ObjCObjectLValueCast:
1632f4a2713aSLionel Sambuc     return "ObjCObjectLValueCast";
1633f4a2713aSLionel Sambuc   case CK_FloatingRealToComplex:
1634f4a2713aSLionel Sambuc     return "FloatingRealToComplex";
1635f4a2713aSLionel Sambuc   case CK_FloatingComplexToReal:
1636f4a2713aSLionel Sambuc     return "FloatingComplexToReal";
1637f4a2713aSLionel Sambuc   case CK_FloatingComplexToBoolean:
1638f4a2713aSLionel Sambuc     return "FloatingComplexToBoolean";
1639f4a2713aSLionel Sambuc   case CK_FloatingComplexCast:
1640f4a2713aSLionel Sambuc     return "FloatingComplexCast";
1641f4a2713aSLionel Sambuc   case CK_FloatingComplexToIntegralComplex:
1642f4a2713aSLionel Sambuc     return "FloatingComplexToIntegralComplex";
1643f4a2713aSLionel Sambuc   case CK_IntegralRealToComplex:
1644f4a2713aSLionel Sambuc     return "IntegralRealToComplex";
1645f4a2713aSLionel Sambuc   case CK_IntegralComplexToReal:
1646f4a2713aSLionel Sambuc     return "IntegralComplexToReal";
1647f4a2713aSLionel Sambuc   case CK_IntegralComplexToBoolean:
1648f4a2713aSLionel Sambuc     return "IntegralComplexToBoolean";
1649f4a2713aSLionel Sambuc   case CK_IntegralComplexCast:
1650f4a2713aSLionel Sambuc     return "IntegralComplexCast";
1651f4a2713aSLionel Sambuc   case CK_IntegralComplexToFloatingComplex:
1652f4a2713aSLionel Sambuc     return "IntegralComplexToFloatingComplex";
1653f4a2713aSLionel Sambuc   case CK_ARCConsumeObject:
1654f4a2713aSLionel Sambuc     return "ARCConsumeObject";
1655f4a2713aSLionel Sambuc   case CK_ARCProduceObject:
1656f4a2713aSLionel Sambuc     return "ARCProduceObject";
1657f4a2713aSLionel Sambuc   case CK_ARCReclaimReturnedObject:
1658f4a2713aSLionel Sambuc     return "ARCReclaimReturnedObject";
1659f4a2713aSLionel Sambuc   case CK_ARCExtendBlockObject:
1660*0a6a1f1dSLionel Sambuc     return "ARCExtendBlockObject";
1661f4a2713aSLionel Sambuc   case CK_AtomicToNonAtomic:
1662f4a2713aSLionel Sambuc     return "AtomicToNonAtomic";
1663f4a2713aSLionel Sambuc   case CK_NonAtomicToAtomic:
1664f4a2713aSLionel Sambuc     return "NonAtomicToAtomic";
1665f4a2713aSLionel Sambuc   case CK_CopyAndAutoreleaseBlockObject:
1666f4a2713aSLionel Sambuc     return "CopyAndAutoreleaseBlockObject";
1667f4a2713aSLionel Sambuc   case CK_BuiltinFnToFnPtr:
1668f4a2713aSLionel Sambuc     return "BuiltinFnToFnPtr";
1669f4a2713aSLionel Sambuc   case CK_ZeroToOCLEvent:
1670f4a2713aSLionel Sambuc     return "ZeroToOCLEvent";
1671*0a6a1f1dSLionel Sambuc   case CK_AddressSpaceConversion:
1672*0a6a1f1dSLionel Sambuc     return "AddressSpaceConversion";
1673f4a2713aSLionel Sambuc   }
1674f4a2713aSLionel Sambuc 
1675f4a2713aSLionel Sambuc   llvm_unreachable("Unhandled cast kind!");
1676f4a2713aSLionel Sambuc }
1677f4a2713aSLionel Sambuc 
getSubExprAsWritten()1678f4a2713aSLionel Sambuc Expr *CastExpr::getSubExprAsWritten() {
1679*0a6a1f1dSLionel Sambuc   Expr *SubExpr = nullptr;
1680f4a2713aSLionel Sambuc   CastExpr *E = this;
1681f4a2713aSLionel Sambuc   do {
1682f4a2713aSLionel Sambuc     SubExpr = E->getSubExpr();
1683f4a2713aSLionel Sambuc 
1684f4a2713aSLionel Sambuc     // Skip through reference binding to temporary.
1685f4a2713aSLionel Sambuc     if (MaterializeTemporaryExpr *Materialize
1686f4a2713aSLionel Sambuc                                   = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1687f4a2713aSLionel Sambuc       SubExpr = Materialize->GetTemporaryExpr();
1688f4a2713aSLionel Sambuc 
1689f4a2713aSLionel Sambuc     // Skip any temporary bindings; they're implicit.
1690f4a2713aSLionel Sambuc     if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1691f4a2713aSLionel Sambuc       SubExpr = Binder->getSubExpr();
1692f4a2713aSLionel Sambuc 
1693f4a2713aSLionel Sambuc     // Conversions by constructor and conversion functions have a
1694f4a2713aSLionel Sambuc     // subexpression describing the call; strip it off.
1695f4a2713aSLionel Sambuc     if (E->getCastKind() == CK_ConstructorConversion)
1696f4a2713aSLionel Sambuc       SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
1697f4a2713aSLionel Sambuc     else if (E->getCastKind() == CK_UserDefinedConversion)
1698f4a2713aSLionel Sambuc       SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
1699f4a2713aSLionel Sambuc 
1700f4a2713aSLionel Sambuc     // If the subexpression we're left with is an implicit cast, look
1701f4a2713aSLionel Sambuc     // through that, too.
1702f4a2713aSLionel Sambuc   } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1703f4a2713aSLionel Sambuc 
1704f4a2713aSLionel Sambuc   return SubExpr;
1705f4a2713aSLionel Sambuc }
1706f4a2713aSLionel Sambuc 
path_buffer()1707f4a2713aSLionel Sambuc CXXBaseSpecifier **CastExpr::path_buffer() {
1708f4a2713aSLionel Sambuc   switch (getStmtClass()) {
1709f4a2713aSLionel Sambuc #define ABSTRACT_STMT(x)
1710f4a2713aSLionel Sambuc #define CASTEXPR(Type, Base) \
1711f4a2713aSLionel Sambuc   case Stmt::Type##Class: \
1712f4a2713aSLionel Sambuc     return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1713f4a2713aSLionel Sambuc #define STMT(Type, Base)
1714f4a2713aSLionel Sambuc #include "clang/AST/StmtNodes.inc"
1715f4a2713aSLionel Sambuc   default:
1716f4a2713aSLionel Sambuc     llvm_unreachable("non-cast expressions not possible here");
1717f4a2713aSLionel Sambuc   }
1718f4a2713aSLionel Sambuc }
1719f4a2713aSLionel Sambuc 
setCastPath(const CXXCastPath & Path)1720f4a2713aSLionel Sambuc void CastExpr::setCastPath(const CXXCastPath &Path) {
1721f4a2713aSLionel Sambuc   assert(Path.size() == path_size());
1722f4a2713aSLionel Sambuc   memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1723f4a2713aSLionel Sambuc }
1724f4a2713aSLionel Sambuc 
Create(const ASTContext & C,QualType T,CastKind Kind,Expr * Operand,const CXXCastPath * BasePath,ExprValueKind VK)1725f4a2713aSLionel Sambuc ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
1726f4a2713aSLionel Sambuc                                            CastKind Kind, Expr *Operand,
1727f4a2713aSLionel Sambuc                                            const CXXCastPath *BasePath,
1728f4a2713aSLionel Sambuc                                            ExprValueKind VK) {
1729f4a2713aSLionel Sambuc   unsigned PathSize = (BasePath ? BasePath->size() : 0);
1730f4a2713aSLionel Sambuc   void *Buffer =
1731f4a2713aSLionel Sambuc     C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1732f4a2713aSLionel Sambuc   ImplicitCastExpr *E =
1733f4a2713aSLionel Sambuc     new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
1734f4a2713aSLionel Sambuc   if (PathSize) E->setCastPath(*BasePath);
1735f4a2713aSLionel Sambuc   return E;
1736f4a2713aSLionel Sambuc }
1737f4a2713aSLionel Sambuc 
CreateEmpty(const ASTContext & C,unsigned PathSize)1738f4a2713aSLionel Sambuc ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
1739f4a2713aSLionel Sambuc                                                 unsigned PathSize) {
1740f4a2713aSLionel Sambuc   void *Buffer =
1741f4a2713aSLionel Sambuc     C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1742f4a2713aSLionel Sambuc   return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1743f4a2713aSLionel Sambuc }
1744f4a2713aSLionel Sambuc 
1745f4a2713aSLionel Sambuc 
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation R)1746f4a2713aSLionel Sambuc CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
1747f4a2713aSLionel Sambuc                                        ExprValueKind VK, CastKind K, Expr *Op,
1748f4a2713aSLionel Sambuc                                        const CXXCastPath *BasePath,
1749f4a2713aSLionel Sambuc                                        TypeSourceInfo *WrittenTy,
1750f4a2713aSLionel Sambuc                                        SourceLocation L, SourceLocation R) {
1751f4a2713aSLionel Sambuc   unsigned PathSize = (BasePath ? BasePath->size() : 0);
1752f4a2713aSLionel Sambuc   void *Buffer =
1753f4a2713aSLionel Sambuc     C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1754f4a2713aSLionel Sambuc   CStyleCastExpr *E =
1755f4a2713aSLionel Sambuc     new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
1756f4a2713aSLionel Sambuc   if (PathSize) E->setCastPath(*BasePath);
1757f4a2713aSLionel Sambuc   return E;
1758f4a2713aSLionel Sambuc }
1759f4a2713aSLionel Sambuc 
CreateEmpty(const ASTContext & C,unsigned PathSize)1760f4a2713aSLionel Sambuc CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1761f4a2713aSLionel Sambuc                                             unsigned PathSize) {
1762f4a2713aSLionel Sambuc   void *Buffer =
1763f4a2713aSLionel Sambuc     C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1764f4a2713aSLionel Sambuc   return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1765f4a2713aSLionel Sambuc }
1766f4a2713aSLionel Sambuc 
1767f4a2713aSLionel Sambuc /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1768f4a2713aSLionel Sambuc /// corresponds to, e.g. "<<=".
getOpcodeStr(Opcode Op)1769f4a2713aSLionel Sambuc StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
1770f4a2713aSLionel Sambuc   switch (Op) {
1771f4a2713aSLionel Sambuc   case BO_PtrMemD:   return ".*";
1772f4a2713aSLionel Sambuc   case BO_PtrMemI:   return "->*";
1773f4a2713aSLionel Sambuc   case BO_Mul:       return "*";
1774f4a2713aSLionel Sambuc   case BO_Div:       return "/";
1775f4a2713aSLionel Sambuc   case BO_Rem:       return "%";
1776f4a2713aSLionel Sambuc   case BO_Add:       return "+";
1777f4a2713aSLionel Sambuc   case BO_Sub:       return "-";
1778f4a2713aSLionel Sambuc   case BO_Shl:       return "<<";
1779f4a2713aSLionel Sambuc   case BO_Shr:       return ">>";
1780f4a2713aSLionel Sambuc   case BO_LT:        return "<";
1781f4a2713aSLionel Sambuc   case BO_GT:        return ">";
1782f4a2713aSLionel Sambuc   case BO_LE:        return "<=";
1783f4a2713aSLionel Sambuc   case BO_GE:        return ">=";
1784f4a2713aSLionel Sambuc   case BO_EQ:        return "==";
1785f4a2713aSLionel Sambuc   case BO_NE:        return "!=";
1786f4a2713aSLionel Sambuc   case BO_And:       return "&";
1787f4a2713aSLionel Sambuc   case BO_Xor:       return "^";
1788f4a2713aSLionel Sambuc   case BO_Or:        return "|";
1789f4a2713aSLionel Sambuc   case BO_LAnd:      return "&&";
1790f4a2713aSLionel Sambuc   case BO_LOr:       return "||";
1791f4a2713aSLionel Sambuc   case BO_Assign:    return "=";
1792f4a2713aSLionel Sambuc   case BO_MulAssign: return "*=";
1793f4a2713aSLionel Sambuc   case BO_DivAssign: return "/=";
1794f4a2713aSLionel Sambuc   case BO_RemAssign: return "%=";
1795f4a2713aSLionel Sambuc   case BO_AddAssign: return "+=";
1796f4a2713aSLionel Sambuc   case BO_SubAssign: return "-=";
1797f4a2713aSLionel Sambuc   case BO_ShlAssign: return "<<=";
1798f4a2713aSLionel Sambuc   case BO_ShrAssign: return ">>=";
1799f4a2713aSLionel Sambuc   case BO_AndAssign: return "&=";
1800f4a2713aSLionel Sambuc   case BO_XorAssign: return "^=";
1801f4a2713aSLionel Sambuc   case BO_OrAssign:  return "|=";
1802f4a2713aSLionel Sambuc   case BO_Comma:     return ",";
1803f4a2713aSLionel Sambuc   }
1804f4a2713aSLionel Sambuc 
1805f4a2713aSLionel Sambuc   llvm_unreachable("Invalid OpCode!");
1806f4a2713aSLionel Sambuc }
1807f4a2713aSLionel Sambuc 
1808f4a2713aSLionel Sambuc BinaryOperatorKind
getOverloadedOpcode(OverloadedOperatorKind OO)1809f4a2713aSLionel Sambuc BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1810f4a2713aSLionel Sambuc   switch (OO) {
1811f4a2713aSLionel Sambuc   default: llvm_unreachable("Not an overloadable binary operator");
1812f4a2713aSLionel Sambuc   case OO_Plus: return BO_Add;
1813f4a2713aSLionel Sambuc   case OO_Minus: return BO_Sub;
1814f4a2713aSLionel Sambuc   case OO_Star: return BO_Mul;
1815f4a2713aSLionel Sambuc   case OO_Slash: return BO_Div;
1816f4a2713aSLionel Sambuc   case OO_Percent: return BO_Rem;
1817f4a2713aSLionel Sambuc   case OO_Caret: return BO_Xor;
1818f4a2713aSLionel Sambuc   case OO_Amp: return BO_And;
1819f4a2713aSLionel Sambuc   case OO_Pipe: return BO_Or;
1820f4a2713aSLionel Sambuc   case OO_Equal: return BO_Assign;
1821f4a2713aSLionel Sambuc   case OO_Less: return BO_LT;
1822f4a2713aSLionel Sambuc   case OO_Greater: return BO_GT;
1823f4a2713aSLionel Sambuc   case OO_PlusEqual: return BO_AddAssign;
1824f4a2713aSLionel Sambuc   case OO_MinusEqual: return BO_SubAssign;
1825f4a2713aSLionel Sambuc   case OO_StarEqual: return BO_MulAssign;
1826f4a2713aSLionel Sambuc   case OO_SlashEqual: return BO_DivAssign;
1827f4a2713aSLionel Sambuc   case OO_PercentEqual: return BO_RemAssign;
1828f4a2713aSLionel Sambuc   case OO_CaretEqual: return BO_XorAssign;
1829f4a2713aSLionel Sambuc   case OO_AmpEqual: return BO_AndAssign;
1830f4a2713aSLionel Sambuc   case OO_PipeEqual: return BO_OrAssign;
1831f4a2713aSLionel Sambuc   case OO_LessLess: return BO_Shl;
1832f4a2713aSLionel Sambuc   case OO_GreaterGreater: return BO_Shr;
1833f4a2713aSLionel Sambuc   case OO_LessLessEqual: return BO_ShlAssign;
1834f4a2713aSLionel Sambuc   case OO_GreaterGreaterEqual: return BO_ShrAssign;
1835f4a2713aSLionel Sambuc   case OO_EqualEqual: return BO_EQ;
1836f4a2713aSLionel Sambuc   case OO_ExclaimEqual: return BO_NE;
1837f4a2713aSLionel Sambuc   case OO_LessEqual: return BO_LE;
1838f4a2713aSLionel Sambuc   case OO_GreaterEqual: return BO_GE;
1839f4a2713aSLionel Sambuc   case OO_AmpAmp: return BO_LAnd;
1840f4a2713aSLionel Sambuc   case OO_PipePipe: return BO_LOr;
1841f4a2713aSLionel Sambuc   case OO_Comma: return BO_Comma;
1842f4a2713aSLionel Sambuc   case OO_ArrowStar: return BO_PtrMemI;
1843f4a2713aSLionel Sambuc   }
1844f4a2713aSLionel Sambuc }
1845f4a2713aSLionel Sambuc 
getOverloadedOperator(Opcode Opc)1846f4a2713aSLionel Sambuc OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1847f4a2713aSLionel Sambuc   static const OverloadedOperatorKind OverOps[] = {
1848f4a2713aSLionel Sambuc     /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1849f4a2713aSLionel Sambuc     OO_Star, OO_Slash, OO_Percent,
1850f4a2713aSLionel Sambuc     OO_Plus, OO_Minus,
1851f4a2713aSLionel Sambuc     OO_LessLess, OO_GreaterGreater,
1852f4a2713aSLionel Sambuc     OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1853f4a2713aSLionel Sambuc     OO_EqualEqual, OO_ExclaimEqual,
1854f4a2713aSLionel Sambuc     OO_Amp,
1855f4a2713aSLionel Sambuc     OO_Caret,
1856f4a2713aSLionel Sambuc     OO_Pipe,
1857f4a2713aSLionel Sambuc     OO_AmpAmp,
1858f4a2713aSLionel Sambuc     OO_PipePipe,
1859f4a2713aSLionel Sambuc     OO_Equal, OO_StarEqual,
1860f4a2713aSLionel Sambuc     OO_SlashEqual, OO_PercentEqual,
1861f4a2713aSLionel Sambuc     OO_PlusEqual, OO_MinusEqual,
1862f4a2713aSLionel Sambuc     OO_LessLessEqual, OO_GreaterGreaterEqual,
1863f4a2713aSLionel Sambuc     OO_AmpEqual, OO_CaretEqual,
1864f4a2713aSLionel Sambuc     OO_PipeEqual,
1865f4a2713aSLionel Sambuc     OO_Comma
1866f4a2713aSLionel Sambuc   };
1867f4a2713aSLionel Sambuc   return OverOps[Opc];
1868f4a2713aSLionel Sambuc }
1869f4a2713aSLionel Sambuc 
InitListExpr(const ASTContext & C,SourceLocation lbraceloc,ArrayRef<Expr * > initExprs,SourceLocation rbraceloc)1870f4a2713aSLionel Sambuc InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
1871f4a2713aSLionel Sambuc                            ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
1872f4a2713aSLionel Sambuc   : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1873f4a2713aSLionel Sambuc          false, false),
1874f4a2713aSLionel Sambuc     InitExprs(C, initExprs.size()),
1875*0a6a1f1dSLionel Sambuc     LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(nullptr, true)
1876f4a2713aSLionel Sambuc {
1877f4a2713aSLionel Sambuc   sawArrayRangeDesignator(false);
1878f4a2713aSLionel Sambuc   for (unsigned I = 0; I != initExprs.size(); ++I) {
1879f4a2713aSLionel Sambuc     if (initExprs[I]->isTypeDependent())
1880f4a2713aSLionel Sambuc       ExprBits.TypeDependent = true;
1881f4a2713aSLionel Sambuc     if (initExprs[I]->isValueDependent())
1882f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
1883f4a2713aSLionel Sambuc     if (initExprs[I]->isInstantiationDependent())
1884f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
1885f4a2713aSLionel Sambuc     if (initExprs[I]->containsUnexpandedParameterPack())
1886f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
1887f4a2713aSLionel Sambuc   }
1888f4a2713aSLionel Sambuc 
1889f4a2713aSLionel Sambuc   InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
1890f4a2713aSLionel Sambuc }
1891f4a2713aSLionel Sambuc 
reserveInits(const ASTContext & C,unsigned NumInits)1892f4a2713aSLionel Sambuc void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
1893f4a2713aSLionel Sambuc   if (NumInits > InitExprs.size())
1894f4a2713aSLionel Sambuc     InitExprs.reserve(C, NumInits);
1895f4a2713aSLionel Sambuc }
1896f4a2713aSLionel Sambuc 
resizeInits(const ASTContext & C,unsigned NumInits)1897f4a2713aSLionel Sambuc void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
1898*0a6a1f1dSLionel Sambuc   InitExprs.resize(C, NumInits, nullptr);
1899f4a2713aSLionel Sambuc }
1900f4a2713aSLionel Sambuc 
updateInit(const ASTContext & C,unsigned Init,Expr * expr)1901f4a2713aSLionel Sambuc Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
1902f4a2713aSLionel Sambuc   if (Init >= InitExprs.size()) {
1903*0a6a1f1dSLionel Sambuc     InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, nullptr);
1904*0a6a1f1dSLionel Sambuc     setInit(Init, expr);
1905*0a6a1f1dSLionel Sambuc     return nullptr;
1906f4a2713aSLionel Sambuc   }
1907f4a2713aSLionel Sambuc 
1908f4a2713aSLionel Sambuc   Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1909*0a6a1f1dSLionel Sambuc   setInit(Init, expr);
1910f4a2713aSLionel Sambuc   return Result;
1911f4a2713aSLionel Sambuc }
1912f4a2713aSLionel Sambuc 
setArrayFiller(Expr * filler)1913f4a2713aSLionel Sambuc void InitListExpr::setArrayFiller(Expr *filler) {
1914f4a2713aSLionel Sambuc   assert(!hasArrayFiller() && "Filler already set!");
1915f4a2713aSLionel Sambuc   ArrayFillerOrUnionFieldInit = filler;
1916f4a2713aSLionel Sambuc   // Fill out any "holes" in the array due to designated initializers.
1917f4a2713aSLionel Sambuc   Expr **inits = getInits();
1918f4a2713aSLionel Sambuc   for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1919*0a6a1f1dSLionel Sambuc     if (inits[i] == nullptr)
1920f4a2713aSLionel Sambuc       inits[i] = filler;
1921f4a2713aSLionel Sambuc }
1922f4a2713aSLionel Sambuc 
isStringLiteralInit() const1923f4a2713aSLionel Sambuc bool InitListExpr::isStringLiteralInit() const {
1924f4a2713aSLionel Sambuc   if (getNumInits() != 1)
1925f4a2713aSLionel Sambuc     return false;
1926f4a2713aSLionel Sambuc   const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1927f4a2713aSLionel Sambuc   if (!AT || !AT->getElementType()->isIntegerType())
1928f4a2713aSLionel Sambuc     return false;
1929*0a6a1f1dSLionel Sambuc   // It is possible for getInit() to return null.
1930*0a6a1f1dSLionel Sambuc   const Expr *Init = getInit(0);
1931*0a6a1f1dSLionel Sambuc   if (!Init)
1932*0a6a1f1dSLionel Sambuc     return false;
1933*0a6a1f1dSLionel Sambuc   Init = Init->IgnoreParens();
1934f4a2713aSLionel Sambuc   return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1935f4a2713aSLionel Sambuc }
1936f4a2713aSLionel Sambuc 
getLocStart() const1937f4a2713aSLionel Sambuc SourceLocation InitListExpr::getLocStart() const {
1938f4a2713aSLionel Sambuc   if (InitListExpr *SyntacticForm = getSyntacticForm())
1939f4a2713aSLionel Sambuc     return SyntacticForm->getLocStart();
1940f4a2713aSLionel Sambuc   SourceLocation Beg = LBraceLoc;
1941f4a2713aSLionel Sambuc   if (Beg.isInvalid()) {
1942f4a2713aSLionel Sambuc     // Find the first non-null initializer.
1943f4a2713aSLionel Sambuc     for (InitExprsTy::const_iterator I = InitExprs.begin(),
1944f4a2713aSLionel Sambuc                                      E = InitExprs.end();
1945f4a2713aSLionel Sambuc       I != E; ++I) {
1946f4a2713aSLionel Sambuc       if (Stmt *S = *I) {
1947f4a2713aSLionel Sambuc         Beg = S->getLocStart();
1948f4a2713aSLionel Sambuc         break;
1949f4a2713aSLionel Sambuc       }
1950f4a2713aSLionel Sambuc     }
1951f4a2713aSLionel Sambuc   }
1952f4a2713aSLionel Sambuc   return Beg;
1953f4a2713aSLionel Sambuc }
1954f4a2713aSLionel Sambuc 
getLocEnd() const1955f4a2713aSLionel Sambuc SourceLocation InitListExpr::getLocEnd() const {
1956f4a2713aSLionel Sambuc   if (InitListExpr *SyntacticForm = getSyntacticForm())
1957f4a2713aSLionel Sambuc     return SyntacticForm->getLocEnd();
1958f4a2713aSLionel Sambuc   SourceLocation End = RBraceLoc;
1959f4a2713aSLionel Sambuc   if (End.isInvalid()) {
1960f4a2713aSLionel Sambuc     // Find the first non-null initializer from the end.
1961f4a2713aSLionel Sambuc     for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1962f4a2713aSLionel Sambuc          E = InitExprs.rend();
1963f4a2713aSLionel Sambuc          I != E; ++I) {
1964f4a2713aSLionel Sambuc       if (Stmt *S = *I) {
1965f4a2713aSLionel Sambuc         End = S->getLocEnd();
1966f4a2713aSLionel Sambuc         break;
1967f4a2713aSLionel Sambuc       }
1968f4a2713aSLionel Sambuc     }
1969f4a2713aSLionel Sambuc   }
1970f4a2713aSLionel Sambuc   return End;
1971f4a2713aSLionel Sambuc }
1972f4a2713aSLionel Sambuc 
1973f4a2713aSLionel Sambuc /// getFunctionType - Return the underlying function type for this block.
1974f4a2713aSLionel Sambuc ///
getFunctionType() const1975f4a2713aSLionel Sambuc const FunctionProtoType *BlockExpr::getFunctionType() const {
1976f4a2713aSLionel Sambuc   // The block pointer is never sugared, but the function type might be.
1977f4a2713aSLionel Sambuc   return cast<BlockPointerType>(getType())
1978f4a2713aSLionel Sambuc            ->getPointeeType()->castAs<FunctionProtoType>();
1979f4a2713aSLionel Sambuc }
1980f4a2713aSLionel Sambuc 
getCaretLocation() const1981f4a2713aSLionel Sambuc SourceLocation BlockExpr::getCaretLocation() const {
1982f4a2713aSLionel Sambuc   return TheBlock->getCaretLocation();
1983f4a2713aSLionel Sambuc }
getBody() const1984f4a2713aSLionel Sambuc const Stmt *BlockExpr::getBody() const {
1985f4a2713aSLionel Sambuc   return TheBlock->getBody();
1986f4a2713aSLionel Sambuc }
getBody()1987f4a2713aSLionel Sambuc Stmt *BlockExpr::getBody() {
1988f4a2713aSLionel Sambuc   return TheBlock->getBody();
1989f4a2713aSLionel Sambuc }
1990f4a2713aSLionel Sambuc 
1991f4a2713aSLionel Sambuc 
1992f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1993f4a2713aSLionel Sambuc // Generic Expression Routines
1994f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1995f4a2713aSLionel Sambuc 
1996f4a2713aSLionel Sambuc /// isUnusedResultAWarning - Return true if this immediate expression should
1997f4a2713aSLionel Sambuc /// be warned about if the result is unused.  If so, fill in Loc and Ranges
1998f4a2713aSLionel Sambuc /// with location to warn on and the source range[s] to report with the
1999f4a2713aSLionel Sambuc /// warning.
isUnusedResultAWarning(const Expr * & WarnE,SourceLocation & Loc,SourceRange & R1,SourceRange & R2,ASTContext & Ctx) const2000f4a2713aSLionel Sambuc bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
2001f4a2713aSLionel Sambuc                                   SourceRange &R1, SourceRange &R2,
2002f4a2713aSLionel Sambuc                                   ASTContext &Ctx) const {
2003f4a2713aSLionel Sambuc   // Don't warn if the expr is type dependent. The type could end up
2004f4a2713aSLionel Sambuc   // instantiating to void.
2005f4a2713aSLionel Sambuc   if (isTypeDependent())
2006f4a2713aSLionel Sambuc     return false;
2007f4a2713aSLionel Sambuc 
2008f4a2713aSLionel Sambuc   switch (getStmtClass()) {
2009f4a2713aSLionel Sambuc   default:
2010f4a2713aSLionel Sambuc     if (getType()->isVoidType())
2011f4a2713aSLionel Sambuc       return false;
2012f4a2713aSLionel Sambuc     WarnE = this;
2013f4a2713aSLionel Sambuc     Loc = getExprLoc();
2014f4a2713aSLionel Sambuc     R1 = getSourceRange();
2015f4a2713aSLionel Sambuc     return true;
2016f4a2713aSLionel Sambuc   case ParenExprClass:
2017f4a2713aSLionel Sambuc     return cast<ParenExpr>(this)->getSubExpr()->
2018f4a2713aSLionel Sambuc       isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2019f4a2713aSLionel Sambuc   case GenericSelectionExprClass:
2020f4a2713aSLionel Sambuc     return cast<GenericSelectionExpr>(this)->getResultExpr()->
2021f4a2713aSLionel Sambuc       isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2022f4a2713aSLionel Sambuc   case ChooseExprClass:
2023f4a2713aSLionel Sambuc     return cast<ChooseExpr>(this)->getChosenSubExpr()->
2024f4a2713aSLionel Sambuc       isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2025f4a2713aSLionel Sambuc   case UnaryOperatorClass: {
2026f4a2713aSLionel Sambuc     const UnaryOperator *UO = cast<UnaryOperator>(this);
2027f4a2713aSLionel Sambuc 
2028f4a2713aSLionel Sambuc     switch (UO->getOpcode()) {
2029f4a2713aSLionel Sambuc     case UO_Plus:
2030f4a2713aSLionel Sambuc     case UO_Minus:
2031f4a2713aSLionel Sambuc     case UO_AddrOf:
2032f4a2713aSLionel Sambuc     case UO_Not:
2033f4a2713aSLionel Sambuc     case UO_LNot:
2034f4a2713aSLionel Sambuc     case UO_Deref:
2035f4a2713aSLionel Sambuc       break;
2036f4a2713aSLionel Sambuc     case UO_PostInc:
2037f4a2713aSLionel Sambuc     case UO_PostDec:
2038f4a2713aSLionel Sambuc     case UO_PreInc:
2039f4a2713aSLionel Sambuc     case UO_PreDec:                 // ++/--
2040f4a2713aSLionel Sambuc       return false;  // Not a warning.
2041f4a2713aSLionel Sambuc     case UO_Real:
2042f4a2713aSLionel Sambuc     case UO_Imag:
2043f4a2713aSLionel Sambuc       // accessing a piece of a volatile complex is a side-effect.
2044f4a2713aSLionel Sambuc       if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2045f4a2713aSLionel Sambuc           .isVolatileQualified())
2046f4a2713aSLionel Sambuc         return false;
2047f4a2713aSLionel Sambuc       break;
2048f4a2713aSLionel Sambuc     case UO_Extension:
2049f4a2713aSLionel Sambuc       return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2050f4a2713aSLionel Sambuc     }
2051f4a2713aSLionel Sambuc     WarnE = this;
2052f4a2713aSLionel Sambuc     Loc = UO->getOperatorLoc();
2053f4a2713aSLionel Sambuc     R1 = UO->getSubExpr()->getSourceRange();
2054f4a2713aSLionel Sambuc     return true;
2055f4a2713aSLionel Sambuc   }
2056f4a2713aSLionel Sambuc   case BinaryOperatorClass: {
2057f4a2713aSLionel Sambuc     const BinaryOperator *BO = cast<BinaryOperator>(this);
2058f4a2713aSLionel Sambuc     switch (BO->getOpcode()) {
2059f4a2713aSLionel Sambuc       default:
2060f4a2713aSLionel Sambuc         break;
2061f4a2713aSLionel Sambuc       // Consider the RHS of comma for side effects. LHS was checked by
2062f4a2713aSLionel Sambuc       // Sema::CheckCommaOperands.
2063f4a2713aSLionel Sambuc       case BO_Comma:
2064f4a2713aSLionel Sambuc         // ((foo = <blah>), 0) is an idiom for hiding the result (and
2065f4a2713aSLionel Sambuc         // lvalue-ness) of an assignment written in a macro.
2066f4a2713aSLionel Sambuc         if (IntegerLiteral *IE =
2067f4a2713aSLionel Sambuc               dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2068f4a2713aSLionel Sambuc           if (IE->getValue() == 0)
2069f4a2713aSLionel Sambuc             return false;
2070f4a2713aSLionel Sambuc         return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2071f4a2713aSLionel Sambuc       // Consider '||', '&&' to have side effects if the LHS or RHS does.
2072f4a2713aSLionel Sambuc       case BO_LAnd:
2073f4a2713aSLionel Sambuc       case BO_LOr:
2074f4a2713aSLionel Sambuc         if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2075f4a2713aSLionel Sambuc             !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
2076f4a2713aSLionel Sambuc           return false;
2077f4a2713aSLionel Sambuc         break;
2078f4a2713aSLionel Sambuc     }
2079f4a2713aSLionel Sambuc     if (BO->isAssignmentOp())
2080f4a2713aSLionel Sambuc       return false;
2081f4a2713aSLionel Sambuc     WarnE = this;
2082f4a2713aSLionel Sambuc     Loc = BO->getOperatorLoc();
2083f4a2713aSLionel Sambuc     R1 = BO->getLHS()->getSourceRange();
2084f4a2713aSLionel Sambuc     R2 = BO->getRHS()->getSourceRange();
2085f4a2713aSLionel Sambuc     return true;
2086f4a2713aSLionel Sambuc   }
2087f4a2713aSLionel Sambuc   case CompoundAssignOperatorClass:
2088f4a2713aSLionel Sambuc   case VAArgExprClass:
2089f4a2713aSLionel Sambuc   case AtomicExprClass:
2090f4a2713aSLionel Sambuc     return false;
2091f4a2713aSLionel Sambuc 
2092f4a2713aSLionel Sambuc   case ConditionalOperatorClass: {
2093f4a2713aSLionel Sambuc     // If only one of the LHS or RHS is a warning, the operator might
2094f4a2713aSLionel Sambuc     // be being used for control flow. Only warn if both the LHS and
2095f4a2713aSLionel Sambuc     // RHS are warnings.
2096f4a2713aSLionel Sambuc     const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
2097f4a2713aSLionel Sambuc     if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
2098f4a2713aSLionel Sambuc       return false;
2099f4a2713aSLionel Sambuc     if (!Exp->getLHS())
2100f4a2713aSLionel Sambuc       return true;
2101f4a2713aSLionel Sambuc     return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2102f4a2713aSLionel Sambuc   }
2103f4a2713aSLionel Sambuc 
2104f4a2713aSLionel Sambuc   case MemberExprClass:
2105f4a2713aSLionel Sambuc     WarnE = this;
2106f4a2713aSLionel Sambuc     Loc = cast<MemberExpr>(this)->getMemberLoc();
2107f4a2713aSLionel Sambuc     R1 = SourceRange(Loc, Loc);
2108f4a2713aSLionel Sambuc     R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2109f4a2713aSLionel Sambuc     return true;
2110f4a2713aSLionel Sambuc 
2111f4a2713aSLionel Sambuc   case ArraySubscriptExprClass:
2112f4a2713aSLionel Sambuc     WarnE = this;
2113f4a2713aSLionel Sambuc     Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2114f4a2713aSLionel Sambuc     R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2115f4a2713aSLionel Sambuc     R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2116f4a2713aSLionel Sambuc     return true;
2117f4a2713aSLionel Sambuc 
2118f4a2713aSLionel Sambuc   case CXXOperatorCallExprClass: {
2119*0a6a1f1dSLionel Sambuc     // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
2120f4a2713aSLionel Sambuc     // overloads as there is no reasonable way to define these such that they
2121f4a2713aSLionel Sambuc     // have non-trivial, desirable side-effects. See the -Wunused-comparison
2122*0a6a1f1dSLionel Sambuc     // warning: operators == and != are commonly typo'ed, and so warning on them
2123f4a2713aSLionel Sambuc     // provides additional value as well. If this list is updated,
2124f4a2713aSLionel Sambuc     // DiagnoseUnusedComparison should be as well.
2125f4a2713aSLionel Sambuc     const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2126*0a6a1f1dSLionel Sambuc     switch (Op->getOperator()) {
2127*0a6a1f1dSLionel Sambuc     default:
2128*0a6a1f1dSLionel Sambuc       break;
2129*0a6a1f1dSLionel Sambuc     case OO_EqualEqual:
2130*0a6a1f1dSLionel Sambuc     case OO_ExclaimEqual:
2131*0a6a1f1dSLionel Sambuc     case OO_Less:
2132*0a6a1f1dSLionel Sambuc     case OO_Greater:
2133*0a6a1f1dSLionel Sambuc     case OO_GreaterEqual:
2134*0a6a1f1dSLionel Sambuc     case OO_LessEqual:
2135*0a6a1f1dSLionel Sambuc       if (Op->getCallReturnType()->isReferenceType() ||
2136*0a6a1f1dSLionel Sambuc           Op->getCallReturnType()->isVoidType())
2137*0a6a1f1dSLionel Sambuc         break;
2138f4a2713aSLionel Sambuc       WarnE = this;
2139f4a2713aSLionel Sambuc       Loc = Op->getOperatorLoc();
2140f4a2713aSLionel Sambuc       R1 = Op->getSourceRange();
2141f4a2713aSLionel Sambuc       return true;
2142f4a2713aSLionel Sambuc     }
2143f4a2713aSLionel Sambuc 
2144f4a2713aSLionel Sambuc     // Fallthrough for generic call handling.
2145f4a2713aSLionel Sambuc   }
2146f4a2713aSLionel Sambuc   case CallExprClass:
2147f4a2713aSLionel Sambuc   case CXXMemberCallExprClass:
2148f4a2713aSLionel Sambuc   case UserDefinedLiteralClass: {
2149f4a2713aSLionel Sambuc     // If this is a direct call, get the callee.
2150f4a2713aSLionel Sambuc     const CallExpr *CE = cast<CallExpr>(this);
2151f4a2713aSLionel Sambuc     if (const Decl *FD = CE->getCalleeDecl()) {
2152f4a2713aSLionel Sambuc       // If the callee has attribute pure, const, or warn_unused_result, warn
2153f4a2713aSLionel Sambuc       // about it. void foo() { strlen("bar"); } should warn.
2154f4a2713aSLionel Sambuc       //
2155f4a2713aSLionel Sambuc       // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2156f4a2713aSLionel Sambuc       // updated to match for QoI.
2157*0a6a1f1dSLionel Sambuc       if (FD->hasAttr<WarnUnusedResultAttr>() ||
2158*0a6a1f1dSLionel Sambuc           FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
2159f4a2713aSLionel Sambuc         WarnE = this;
2160f4a2713aSLionel Sambuc         Loc = CE->getCallee()->getLocStart();
2161f4a2713aSLionel Sambuc         R1 = CE->getCallee()->getSourceRange();
2162f4a2713aSLionel Sambuc 
2163f4a2713aSLionel Sambuc         if (unsigned NumArgs = CE->getNumArgs())
2164f4a2713aSLionel Sambuc           R2 = SourceRange(CE->getArg(0)->getLocStart(),
2165f4a2713aSLionel Sambuc                            CE->getArg(NumArgs-1)->getLocEnd());
2166f4a2713aSLionel Sambuc         return true;
2167f4a2713aSLionel Sambuc       }
2168f4a2713aSLionel Sambuc     }
2169f4a2713aSLionel Sambuc     return false;
2170f4a2713aSLionel Sambuc   }
2171f4a2713aSLionel Sambuc 
2172f4a2713aSLionel Sambuc   // If we don't know precisely what we're looking at, let's not warn.
2173f4a2713aSLionel Sambuc   case UnresolvedLookupExprClass:
2174f4a2713aSLionel Sambuc   case CXXUnresolvedConstructExprClass:
2175f4a2713aSLionel Sambuc     return false;
2176f4a2713aSLionel Sambuc 
2177f4a2713aSLionel Sambuc   case CXXTemporaryObjectExprClass:
2178f4a2713aSLionel Sambuc   case CXXConstructExprClass: {
2179f4a2713aSLionel Sambuc     if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2180f4a2713aSLionel Sambuc       if (Type->hasAttr<WarnUnusedAttr>()) {
2181f4a2713aSLionel Sambuc         WarnE = this;
2182f4a2713aSLionel Sambuc         Loc = getLocStart();
2183f4a2713aSLionel Sambuc         R1 = getSourceRange();
2184f4a2713aSLionel Sambuc         return true;
2185f4a2713aSLionel Sambuc       }
2186f4a2713aSLionel Sambuc     }
2187f4a2713aSLionel Sambuc     return false;
2188f4a2713aSLionel Sambuc   }
2189f4a2713aSLionel Sambuc 
2190f4a2713aSLionel Sambuc   case ObjCMessageExprClass: {
2191f4a2713aSLionel Sambuc     const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
2192f4a2713aSLionel Sambuc     if (Ctx.getLangOpts().ObjCAutoRefCount &&
2193f4a2713aSLionel Sambuc         ME->isInstanceMessage() &&
2194f4a2713aSLionel Sambuc         !ME->getType()->isVoidType() &&
2195f4a2713aSLionel Sambuc         ME->getMethodFamily() == OMF_init) {
2196f4a2713aSLionel Sambuc       WarnE = this;
2197f4a2713aSLionel Sambuc       Loc = getExprLoc();
2198f4a2713aSLionel Sambuc       R1 = ME->getSourceRange();
2199f4a2713aSLionel Sambuc       return true;
2200f4a2713aSLionel Sambuc     }
2201f4a2713aSLionel Sambuc 
2202*0a6a1f1dSLionel Sambuc     if (const ObjCMethodDecl *MD = ME->getMethodDecl())
2203*0a6a1f1dSLionel Sambuc       if (MD->hasAttr<WarnUnusedResultAttr>() ||
2204*0a6a1f1dSLionel Sambuc           (MD->isPropertyAccessor() && !MD->getReturnType()->isVoidType() &&
2205*0a6a1f1dSLionel Sambuc            !ME->getReceiverType()->isObjCIdType())) {
2206f4a2713aSLionel Sambuc         WarnE = this;
2207f4a2713aSLionel Sambuc         Loc = getExprLoc();
2208f4a2713aSLionel Sambuc         return true;
2209f4a2713aSLionel Sambuc       }
2210*0a6a1f1dSLionel Sambuc 
2211f4a2713aSLionel Sambuc     return false;
2212f4a2713aSLionel Sambuc   }
2213f4a2713aSLionel Sambuc 
2214f4a2713aSLionel Sambuc   case ObjCPropertyRefExprClass:
2215f4a2713aSLionel Sambuc     WarnE = this;
2216f4a2713aSLionel Sambuc     Loc = getExprLoc();
2217f4a2713aSLionel Sambuc     R1 = getSourceRange();
2218f4a2713aSLionel Sambuc     return true;
2219f4a2713aSLionel Sambuc 
2220f4a2713aSLionel Sambuc   case PseudoObjectExprClass: {
2221f4a2713aSLionel Sambuc     const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2222f4a2713aSLionel Sambuc 
2223f4a2713aSLionel Sambuc     // Only complain about things that have the form of a getter.
2224f4a2713aSLionel Sambuc     if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2225f4a2713aSLionel Sambuc         isa<BinaryOperator>(PO->getSyntacticForm()))
2226f4a2713aSLionel Sambuc       return false;
2227f4a2713aSLionel Sambuc 
2228f4a2713aSLionel Sambuc     WarnE = this;
2229f4a2713aSLionel Sambuc     Loc = getExprLoc();
2230f4a2713aSLionel Sambuc     R1 = getSourceRange();
2231f4a2713aSLionel Sambuc     return true;
2232f4a2713aSLionel Sambuc   }
2233f4a2713aSLionel Sambuc 
2234f4a2713aSLionel Sambuc   case StmtExprClass: {
2235f4a2713aSLionel Sambuc     // Statement exprs don't logically have side effects themselves, but are
2236f4a2713aSLionel Sambuc     // sometimes used in macros in ways that give them a type that is unused.
2237f4a2713aSLionel Sambuc     // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2238f4a2713aSLionel Sambuc     // however, if the result of the stmt expr is dead, we don't want to emit a
2239f4a2713aSLionel Sambuc     // warning.
2240f4a2713aSLionel Sambuc     const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
2241f4a2713aSLionel Sambuc     if (!CS->body_empty()) {
2242f4a2713aSLionel Sambuc       if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
2243f4a2713aSLionel Sambuc         return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2244f4a2713aSLionel Sambuc       if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2245f4a2713aSLionel Sambuc         if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
2246f4a2713aSLionel Sambuc           return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2247f4a2713aSLionel Sambuc     }
2248f4a2713aSLionel Sambuc 
2249f4a2713aSLionel Sambuc     if (getType()->isVoidType())
2250f4a2713aSLionel Sambuc       return false;
2251f4a2713aSLionel Sambuc     WarnE = this;
2252f4a2713aSLionel Sambuc     Loc = cast<StmtExpr>(this)->getLParenLoc();
2253f4a2713aSLionel Sambuc     R1 = getSourceRange();
2254f4a2713aSLionel Sambuc     return true;
2255f4a2713aSLionel Sambuc   }
2256f4a2713aSLionel Sambuc   case CXXFunctionalCastExprClass:
2257f4a2713aSLionel Sambuc   case CStyleCastExprClass: {
2258f4a2713aSLionel Sambuc     // Ignore an explicit cast to void unless the operand is a non-trivial
2259f4a2713aSLionel Sambuc     // volatile lvalue.
2260f4a2713aSLionel Sambuc     const CastExpr *CE = cast<CastExpr>(this);
2261f4a2713aSLionel Sambuc     if (CE->getCastKind() == CK_ToVoid) {
2262f4a2713aSLionel Sambuc       if (CE->getSubExpr()->isGLValue() &&
2263f4a2713aSLionel Sambuc           CE->getSubExpr()->getType().isVolatileQualified()) {
2264f4a2713aSLionel Sambuc         const DeclRefExpr *DRE =
2265f4a2713aSLionel Sambuc             dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2266f4a2713aSLionel Sambuc         if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2267f4a2713aSLionel Sambuc               cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2268f4a2713aSLionel Sambuc           return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2269f4a2713aSLionel Sambuc                                                           R1, R2, Ctx);
2270f4a2713aSLionel Sambuc         }
2271f4a2713aSLionel Sambuc       }
2272f4a2713aSLionel Sambuc       return false;
2273f4a2713aSLionel Sambuc     }
2274f4a2713aSLionel Sambuc 
2275f4a2713aSLionel Sambuc     // If this is a cast to a constructor conversion, check the operand.
2276f4a2713aSLionel Sambuc     // Otherwise, the result of the cast is unused.
2277f4a2713aSLionel Sambuc     if (CE->getCastKind() == CK_ConstructorConversion)
2278f4a2713aSLionel Sambuc       return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2279f4a2713aSLionel Sambuc 
2280f4a2713aSLionel Sambuc     WarnE = this;
2281f4a2713aSLionel Sambuc     if (const CXXFunctionalCastExpr *CXXCE =
2282f4a2713aSLionel Sambuc             dyn_cast<CXXFunctionalCastExpr>(this)) {
2283f4a2713aSLionel Sambuc       Loc = CXXCE->getLocStart();
2284f4a2713aSLionel Sambuc       R1 = CXXCE->getSubExpr()->getSourceRange();
2285f4a2713aSLionel Sambuc     } else {
2286f4a2713aSLionel Sambuc       const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2287f4a2713aSLionel Sambuc       Loc = CStyleCE->getLParenLoc();
2288f4a2713aSLionel Sambuc       R1 = CStyleCE->getSubExpr()->getSourceRange();
2289f4a2713aSLionel Sambuc     }
2290f4a2713aSLionel Sambuc     return true;
2291f4a2713aSLionel Sambuc   }
2292f4a2713aSLionel Sambuc   case ImplicitCastExprClass: {
2293f4a2713aSLionel Sambuc     const CastExpr *ICE = cast<ImplicitCastExpr>(this);
2294f4a2713aSLionel Sambuc 
2295f4a2713aSLionel Sambuc     // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2296f4a2713aSLionel Sambuc     if (ICE->getCastKind() == CK_LValueToRValue &&
2297f4a2713aSLionel Sambuc         ICE->getSubExpr()->getType().isVolatileQualified())
2298f4a2713aSLionel Sambuc       return false;
2299f4a2713aSLionel Sambuc 
2300f4a2713aSLionel Sambuc     return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2301f4a2713aSLionel Sambuc   }
2302f4a2713aSLionel Sambuc   case CXXDefaultArgExprClass:
2303f4a2713aSLionel Sambuc     return (cast<CXXDefaultArgExpr>(this)
2304f4a2713aSLionel Sambuc             ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
2305f4a2713aSLionel Sambuc   case CXXDefaultInitExprClass:
2306f4a2713aSLionel Sambuc     return (cast<CXXDefaultInitExpr>(this)
2307f4a2713aSLionel Sambuc             ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
2308f4a2713aSLionel Sambuc 
2309f4a2713aSLionel Sambuc   case CXXNewExprClass:
2310f4a2713aSLionel Sambuc     // FIXME: In theory, there might be new expressions that don't have side
2311f4a2713aSLionel Sambuc     // effects (e.g. a placement new with an uninitialized POD).
2312f4a2713aSLionel Sambuc   case CXXDeleteExprClass:
2313f4a2713aSLionel Sambuc     return false;
2314f4a2713aSLionel Sambuc   case CXXBindTemporaryExprClass:
2315f4a2713aSLionel Sambuc     return (cast<CXXBindTemporaryExpr>(this)
2316f4a2713aSLionel Sambuc             ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
2317f4a2713aSLionel Sambuc   case ExprWithCleanupsClass:
2318f4a2713aSLionel Sambuc     return (cast<ExprWithCleanups>(this)
2319f4a2713aSLionel Sambuc             ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
2320f4a2713aSLionel Sambuc   }
2321f4a2713aSLionel Sambuc }
2322f4a2713aSLionel Sambuc 
2323f4a2713aSLionel Sambuc /// isOBJCGCCandidate - Check if an expression is objc gc'able.
2324f4a2713aSLionel Sambuc /// returns true, if it is; false otherwise.
isOBJCGCCandidate(ASTContext & Ctx) const2325f4a2713aSLionel Sambuc bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
2326f4a2713aSLionel Sambuc   const Expr *E = IgnoreParens();
2327f4a2713aSLionel Sambuc   switch (E->getStmtClass()) {
2328f4a2713aSLionel Sambuc   default:
2329f4a2713aSLionel Sambuc     return false;
2330f4a2713aSLionel Sambuc   case ObjCIvarRefExprClass:
2331f4a2713aSLionel Sambuc     return true;
2332f4a2713aSLionel Sambuc   case Expr::UnaryOperatorClass:
2333f4a2713aSLionel Sambuc     return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
2334f4a2713aSLionel Sambuc   case ImplicitCastExprClass:
2335f4a2713aSLionel Sambuc     return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
2336f4a2713aSLionel Sambuc   case MaterializeTemporaryExprClass:
2337f4a2713aSLionel Sambuc     return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2338f4a2713aSLionel Sambuc                                                       ->isOBJCGCCandidate(Ctx);
2339f4a2713aSLionel Sambuc   case CStyleCastExprClass:
2340f4a2713aSLionel Sambuc     return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
2341f4a2713aSLionel Sambuc   case DeclRefExprClass: {
2342f4a2713aSLionel Sambuc     const Decl *D = cast<DeclRefExpr>(E)->getDecl();
2343f4a2713aSLionel Sambuc 
2344f4a2713aSLionel Sambuc     if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2345f4a2713aSLionel Sambuc       if (VD->hasGlobalStorage())
2346f4a2713aSLionel Sambuc         return true;
2347f4a2713aSLionel Sambuc       QualType T = VD->getType();
2348f4a2713aSLionel Sambuc       // dereferencing to a  pointer is always a gc'able candidate,
2349f4a2713aSLionel Sambuc       // unless it is __weak.
2350f4a2713aSLionel Sambuc       return T->isPointerType() &&
2351f4a2713aSLionel Sambuc              (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
2352f4a2713aSLionel Sambuc     }
2353f4a2713aSLionel Sambuc     return false;
2354f4a2713aSLionel Sambuc   }
2355f4a2713aSLionel Sambuc   case MemberExprClass: {
2356f4a2713aSLionel Sambuc     const MemberExpr *M = cast<MemberExpr>(E);
2357f4a2713aSLionel Sambuc     return M->getBase()->isOBJCGCCandidate(Ctx);
2358f4a2713aSLionel Sambuc   }
2359f4a2713aSLionel Sambuc   case ArraySubscriptExprClass:
2360f4a2713aSLionel Sambuc     return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
2361f4a2713aSLionel Sambuc   }
2362f4a2713aSLionel Sambuc }
2363f4a2713aSLionel Sambuc 
isBoundMemberFunction(ASTContext & Ctx) const2364f4a2713aSLionel Sambuc bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2365f4a2713aSLionel Sambuc   if (isTypeDependent())
2366f4a2713aSLionel Sambuc     return false;
2367f4a2713aSLionel Sambuc   return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
2368f4a2713aSLionel Sambuc }
2369f4a2713aSLionel Sambuc 
findBoundMemberType(const Expr * expr)2370f4a2713aSLionel Sambuc QualType Expr::findBoundMemberType(const Expr *expr) {
2371f4a2713aSLionel Sambuc   assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
2372f4a2713aSLionel Sambuc 
2373f4a2713aSLionel Sambuc   // Bound member expressions are always one of these possibilities:
2374f4a2713aSLionel Sambuc   //   x->m      x.m      x->*y      x.*y
2375f4a2713aSLionel Sambuc   // (possibly parenthesized)
2376f4a2713aSLionel Sambuc 
2377f4a2713aSLionel Sambuc   expr = expr->IgnoreParens();
2378f4a2713aSLionel Sambuc   if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2379f4a2713aSLionel Sambuc     assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2380f4a2713aSLionel Sambuc     return mem->getMemberDecl()->getType();
2381f4a2713aSLionel Sambuc   }
2382f4a2713aSLionel Sambuc 
2383f4a2713aSLionel Sambuc   if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2384f4a2713aSLionel Sambuc     QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2385f4a2713aSLionel Sambuc                       ->getPointeeType();
2386f4a2713aSLionel Sambuc     assert(type->isFunctionType());
2387f4a2713aSLionel Sambuc     return type;
2388f4a2713aSLionel Sambuc   }
2389f4a2713aSLionel Sambuc 
2390f4a2713aSLionel Sambuc   assert(isa<UnresolvedMemberExpr>(expr));
2391f4a2713aSLionel Sambuc   return QualType();
2392f4a2713aSLionel Sambuc }
2393f4a2713aSLionel Sambuc 
IgnoreParens()2394f4a2713aSLionel Sambuc Expr* Expr::IgnoreParens() {
2395f4a2713aSLionel Sambuc   Expr* E = this;
2396f4a2713aSLionel Sambuc   while (true) {
2397f4a2713aSLionel Sambuc     if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2398f4a2713aSLionel Sambuc       E = P->getSubExpr();
2399f4a2713aSLionel Sambuc       continue;
2400f4a2713aSLionel Sambuc     }
2401f4a2713aSLionel Sambuc     if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2402f4a2713aSLionel Sambuc       if (P->getOpcode() == UO_Extension) {
2403f4a2713aSLionel Sambuc         E = P->getSubExpr();
2404f4a2713aSLionel Sambuc         continue;
2405f4a2713aSLionel Sambuc       }
2406f4a2713aSLionel Sambuc     }
2407f4a2713aSLionel Sambuc     if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2408f4a2713aSLionel Sambuc       if (!P->isResultDependent()) {
2409f4a2713aSLionel Sambuc         E = P->getResultExpr();
2410f4a2713aSLionel Sambuc         continue;
2411f4a2713aSLionel Sambuc       }
2412f4a2713aSLionel Sambuc     }
2413f4a2713aSLionel Sambuc     if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2414f4a2713aSLionel Sambuc       if (!P->isConditionDependent()) {
2415f4a2713aSLionel Sambuc         E = P->getChosenSubExpr();
2416f4a2713aSLionel Sambuc         continue;
2417f4a2713aSLionel Sambuc       }
2418f4a2713aSLionel Sambuc     }
2419f4a2713aSLionel Sambuc     return E;
2420f4a2713aSLionel Sambuc   }
2421f4a2713aSLionel Sambuc }
2422f4a2713aSLionel Sambuc 
2423f4a2713aSLionel Sambuc /// IgnoreParenCasts - Ignore parentheses and casts.  Strip off any ParenExpr
2424f4a2713aSLionel Sambuc /// or CastExprs or ImplicitCastExprs, returning their operand.
IgnoreParenCasts()2425f4a2713aSLionel Sambuc Expr *Expr::IgnoreParenCasts() {
2426f4a2713aSLionel Sambuc   Expr *E = this;
2427f4a2713aSLionel Sambuc   while (true) {
2428f4a2713aSLionel Sambuc     E = E->IgnoreParens();
2429f4a2713aSLionel Sambuc     if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2430f4a2713aSLionel Sambuc       E = P->getSubExpr();
2431f4a2713aSLionel Sambuc       continue;
2432f4a2713aSLionel Sambuc     }
2433f4a2713aSLionel Sambuc     if (MaterializeTemporaryExpr *Materialize
2434f4a2713aSLionel Sambuc                                       = dyn_cast<MaterializeTemporaryExpr>(E)) {
2435f4a2713aSLionel Sambuc       E = Materialize->GetTemporaryExpr();
2436f4a2713aSLionel Sambuc       continue;
2437f4a2713aSLionel Sambuc     }
2438f4a2713aSLionel Sambuc     if (SubstNonTypeTemplateParmExpr *NTTP
2439f4a2713aSLionel Sambuc                                   = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2440f4a2713aSLionel Sambuc       E = NTTP->getReplacement();
2441f4a2713aSLionel Sambuc       continue;
2442f4a2713aSLionel Sambuc     }
2443f4a2713aSLionel Sambuc     return E;
2444f4a2713aSLionel Sambuc   }
2445f4a2713aSLionel Sambuc }
2446f4a2713aSLionel Sambuc 
IgnoreCasts()2447*0a6a1f1dSLionel Sambuc Expr *Expr::IgnoreCasts() {
2448*0a6a1f1dSLionel Sambuc   Expr *E = this;
2449*0a6a1f1dSLionel Sambuc   while (true) {
2450*0a6a1f1dSLionel Sambuc     if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2451*0a6a1f1dSLionel Sambuc       E = P->getSubExpr();
2452*0a6a1f1dSLionel Sambuc       continue;
2453*0a6a1f1dSLionel Sambuc     }
2454*0a6a1f1dSLionel Sambuc     if (MaterializeTemporaryExpr *Materialize
2455*0a6a1f1dSLionel Sambuc         = dyn_cast<MaterializeTemporaryExpr>(E)) {
2456*0a6a1f1dSLionel Sambuc       E = Materialize->GetTemporaryExpr();
2457*0a6a1f1dSLionel Sambuc       continue;
2458*0a6a1f1dSLionel Sambuc     }
2459*0a6a1f1dSLionel Sambuc     if (SubstNonTypeTemplateParmExpr *NTTP
2460*0a6a1f1dSLionel Sambuc         = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2461*0a6a1f1dSLionel Sambuc       E = NTTP->getReplacement();
2462*0a6a1f1dSLionel Sambuc       continue;
2463*0a6a1f1dSLionel Sambuc     }
2464*0a6a1f1dSLionel Sambuc     return E;
2465*0a6a1f1dSLionel Sambuc   }
2466*0a6a1f1dSLionel Sambuc }
2467*0a6a1f1dSLionel Sambuc 
2468f4a2713aSLionel Sambuc /// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2469f4a2713aSLionel Sambuc /// casts.  This is intended purely as a temporary workaround for code
2470f4a2713aSLionel Sambuc /// that hasn't yet been rewritten to do the right thing about those
2471f4a2713aSLionel Sambuc /// casts, and may disappear along with the last internal use.
IgnoreParenLValueCasts()2472f4a2713aSLionel Sambuc Expr *Expr::IgnoreParenLValueCasts() {
2473f4a2713aSLionel Sambuc   Expr *E = this;
2474f4a2713aSLionel Sambuc   while (true) {
2475f4a2713aSLionel Sambuc     E = E->IgnoreParens();
2476f4a2713aSLionel Sambuc     if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2477f4a2713aSLionel Sambuc       if (P->getCastKind() == CK_LValueToRValue) {
2478f4a2713aSLionel Sambuc         E = P->getSubExpr();
2479f4a2713aSLionel Sambuc         continue;
2480f4a2713aSLionel Sambuc       }
2481f4a2713aSLionel Sambuc     } else if (MaterializeTemporaryExpr *Materialize
2482f4a2713aSLionel Sambuc                                       = dyn_cast<MaterializeTemporaryExpr>(E)) {
2483f4a2713aSLionel Sambuc       E = Materialize->GetTemporaryExpr();
2484f4a2713aSLionel Sambuc       continue;
2485f4a2713aSLionel Sambuc     } else if (SubstNonTypeTemplateParmExpr *NTTP
2486f4a2713aSLionel Sambuc                                   = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2487f4a2713aSLionel Sambuc       E = NTTP->getReplacement();
2488f4a2713aSLionel Sambuc       continue;
2489f4a2713aSLionel Sambuc     }
2490f4a2713aSLionel Sambuc     break;
2491f4a2713aSLionel Sambuc   }
2492f4a2713aSLionel Sambuc   return E;
2493f4a2713aSLionel Sambuc }
2494f4a2713aSLionel Sambuc 
ignoreParenBaseCasts()2495f4a2713aSLionel Sambuc Expr *Expr::ignoreParenBaseCasts() {
2496f4a2713aSLionel Sambuc   Expr *E = this;
2497f4a2713aSLionel Sambuc   while (true) {
2498f4a2713aSLionel Sambuc     E = E->IgnoreParens();
2499f4a2713aSLionel Sambuc     if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2500f4a2713aSLionel Sambuc       if (CE->getCastKind() == CK_DerivedToBase ||
2501f4a2713aSLionel Sambuc           CE->getCastKind() == CK_UncheckedDerivedToBase ||
2502f4a2713aSLionel Sambuc           CE->getCastKind() == CK_NoOp) {
2503f4a2713aSLionel Sambuc         E = CE->getSubExpr();
2504f4a2713aSLionel Sambuc         continue;
2505f4a2713aSLionel Sambuc       }
2506f4a2713aSLionel Sambuc     }
2507f4a2713aSLionel Sambuc 
2508f4a2713aSLionel Sambuc     return E;
2509f4a2713aSLionel Sambuc   }
2510f4a2713aSLionel Sambuc }
2511f4a2713aSLionel Sambuc 
IgnoreParenImpCasts()2512f4a2713aSLionel Sambuc Expr *Expr::IgnoreParenImpCasts() {
2513f4a2713aSLionel Sambuc   Expr *E = this;
2514f4a2713aSLionel Sambuc   while (true) {
2515f4a2713aSLionel Sambuc     E = E->IgnoreParens();
2516f4a2713aSLionel Sambuc     if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
2517f4a2713aSLionel Sambuc       E = P->getSubExpr();
2518f4a2713aSLionel Sambuc       continue;
2519f4a2713aSLionel Sambuc     }
2520f4a2713aSLionel Sambuc     if (MaterializeTemporaryExpr *Materialize
2521f4a2713aSLionel Sambuc                                       = dyn_cast<MaterializeTemporaryExpr>(E)) {
2522f4a2713aSLionel Sambuc       E = Materialize->GetTemporaryExpr();
2523f4a2713aSLionel Sambuc       continue;
2524f4a2713aSLionel Sambuc     }
2525f4a2713aSLionel Sambuc     if (SubstNonTypeTemplateParmExpr *NTTP
2526f4a2713aSLionel Sambuc                                   = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2527f4a2713aSLionel Sambuc       E = NTTP->getReplacement();
2528f4a2713aSLionel Sambuc       continue;
2529f4a2713aSLionel Sambuc     }
2530f4a2713aSLionel Sambuc     return E;
2531f4a2713aSLionel Sambuc   }
2532f4a2713aSLionel Sambuc }
2533f4a2713aSLionel Sambuc 
IgnoreConversionOperator()2534f4a2713aSLionel Sambuc Expr *Expr::IgnoreConversionOperator() {
2535f4a2713aSLionel Sambuc   if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
2536f4a2713aSLionel Sambuc     if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
2537f4a2713aSLionel Sambuc       return MCE->getImplicitObjectArgument();
2538f4a2713aSLionel Sambuc   }
2539f4a2713aSLionel Sambuc   return this;
2540f4a2713aSLionel Sambuc }
2541f4a2713aSLionel Sambuc 
2542f4a2713aSLionel Sambuc /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2543f4a2713aSLionel Sambuc /// value (including ptr->int casts of the same size).  Strip off any
2544f4a2713aSLionel Sambuc /// ParenExpr or CastExprs, returning their operand.
IgnoreParenNoopCasts(ASTContext & Ctx)2545f4a2713aSLionel Sambuc Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2546f4a2713aSLionel Sambuc   Expr *E = this;
2547f4a2713aSLionel Sambuc   while (true) {
2548f4a2713aSLionel Sambuc     E = E->IgnoreParens();
2549f4a2713aSLionel Sambuc 
2550f4a2713aSLionel Sambuc     if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2551f4a2713aSLionel Sambuc       // We ignore integer <-> casts that are of the same width, ptr<->ptr and
2552f4a2713aSLionel Sambuc       // ptr<->int casts of the same width.  We also ignore all identity casts.
2553f4a2713aSLionel Sambuc       Expr *SE = P->getSubExpr();
2554f4a2713aSLionel Sambuc 
2555f4a2713aSLionel Sambuc       if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2556f4a2713aSLionel Sambuc         E = SE;
2557f4a2713aSLionel Sambuc         continue;
2558f4a2713aSLionel Sambuc       }
2559f4a2713aSLionel Sambuc 
2560f4a2713aSLionel Sambuc       if ((E->getType()->isPointerType() ||
2561f4a2713aSLionel Sambuc            E->getType()->isIntegralType(Ctx)) &&
2562f4a2713aSLionel Sambuc           (SE->getType()->isPointerType() ||
2563f4a2713aSLionel Sambuc            SE->getType()->isIntegralType(Ctx)) &&
2564f4a2713aSLionel Sambuc           Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2565f4a2713aSLionel Sambuc         E = SE;
2566f4a2713aSLionel Sambuc         continue;
2567f4a2713aSLionel Sambuc       }
2568f4a2713aSLionel Sambuc     }
2569f4a2713aSLionel Sambuc 
2570f4a2713aSLionel Sambuc     if (SubstNonTypeTemplateParmExpr *NTTP
2571f4a2713aSLionel Sambuc                                   = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2572f4a2713aSLionel Sambuc       E = NTTP->getReplacement();
2573f4a2713aSLionel Sambuc       continue;
2574f4a2713aSLionel Sambuc     }
2575f4a2713aSLionel Sambuc 
2576f4a2713aSLionel Sambuc     return E;
2577f4a2713aSLionel Sambuc   }
2578f4a2713aSLionel Sambuc }
2579f4a2713aSLionel Sambuc 
isDefaultArgument() const2580f4a2713aSLionel Sambuc bool Expr::isDefaultArgument() const {
2581f4a2713aSLionel Sambuc   const Expr *E = this;
2582f4a2713aSLionel Sambuc   if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2583f4a2713aSLionel Sambuc     E = M->GetTemporaryExpr();
2584f4a2713aSLionel Sambuc 
2585f4a2713aSLionel Sambuc   while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2586f4a2713aSLionel Sambuc     E = ICE->getSubExprAsWritten();
2587f4a2713aSLionel Sambuc 
2588f4a2713aSLionel Sambuc   return isa<CXXDefaultArgExpr>(E);
2589f4a2713aSLionel Sambuc }
2590f4a2713aSLionel Sambuc 
2591f4a2713aSLionel Sambuc /// \brief Skip over any no-op casts and any temporary-binding
2592f4a2713aSLionel Sambuc /// expressions.
skipTemporaryBindingsNoOpCastsAndParens(const Expr * E)2593f4a2713aSLionel Sambuc static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
2594f4a2713aSLionel Sambuc   if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2595f4a2713aSLionel Sambuc     E = M->GetTemporaryExpr();
2596f4a2713aSLionel Sambuc 
2597f4a2713aSLionel Sambuc   while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2598f4a2713aSLionel Sambuc     if (ICE->getCastKind() == CK_NoOp)
2599f4a2713aSLionel Sambuc       E = ICE->getSubExpr();
2600f4a2713aSLionel Sambuc     else
2601f4a2713aSLionel Sambuc       break;
2602f4a2713aSLionel Sambuc   }
2603f4a2713aSLionel Sambuc 
2604f4a2713aSLionel Sambuc   while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2605f4a2713aSLionel Sambuc     E = BE->getSubExpr();
2606f4a2713aSLionel Sambuc 
2607f4a2713aSLionel Sambuc   while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2608f4a2713aSLionel Sambuc     if (ICE->getCastKind() == CK_NoOp)
2609f4a2713aSLionel Sambuc       E = ICE->getSubExpr();
2610f4a2713aSLionel Sambuc     else
2611f4a2713aSLionel Sambuc       break;
2612f4a2713aSLionel Sambuc   }
2613f4a2713aSLionel Sambuc 
2614f4a2713aSLionel Sambuc   return E->IgnoreParens();
2615f4a2713aSLionel Sambuc }
2616f4a2713aSLionel Sambuc 
2617f4a2713aSLionel Sambuc /// isTemporaryObject - Determines if this expression produces a
2618f4a2713aSLionel Sambuc /// temporary of the given class type.
isTemporaryObject(ASTContext & C,const CXXRecordDecl * TempTy) const2619f4a2713aSLionel Sambuc bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2620f4a2713aSLionel Sambuc   if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2621f4a2713aSLionel Sambuc     return false;
2622f4a2713aSLionel Sambuc 
2623f4a2713aSLionel Sambuc   const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
2624f4a2713aSLionel Sambuc 
2625f4a2713aSLionel Sambuc   // Temporaries are by definition pr-values of class type.
2626f4a2713aSLionel Sambuc   if (!E->Classify(C).isPRValue()) {
2627f4a2713aSLionel Sambuc     // In this context, property reference is a message call and is pr-value.
2628f4a2713aSLionel Sambuc     if (!isa<ObjCPropertyRefExpr>(E))
2629f4a2713aSLionel Sambuc       return false;
2630f4a2713aSLionel Sambuc   }
2631f4a2713aSLionel Sambuc 
2632f4a2713aSLionel Sambuc   // Black-list a few cases which yield pr-values of class type that don't
2633f4a2713aSLionel Sambuc   // refer to temporaries of that type:
2634f4a2713aSLionel Sambuc 
2635f4a2713aSLionel Sambuc   // - implicit derived-to-base conversions
2636f4a2713aSLionel Sambuc   if (isa<ImplicitCastExpr>(E)) {
2637f4a2713aSLionel Sambuc     switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2638f4a2713aSLionel Sambuc     case CK_DerivedToBase:
2639f4a2713aSLionel Sambuc     case CK_UncheckedDerivedToBase:
2640f4a2713aSLionel Sambuc       return false;
2641f4a2713aSLionel Sambuc     default:
2642f4a2713aSLionel Sambuc       break;
2643f4a2713aSLionel Sambuc     }
2644f4a2713aSLionel Sambuc   }
2645f4a2713aSLionel Sambuc 
2646f4a2713aSLionel Sambuc   // - member expressions (all)
2647f4a2713aSLionel Sambuc   if (isa<MemberExpr>(E))
2648f4a2713aSLionel Sambuc     return false;
2649f4a2713aSLionel Sambuc 
2650f4a2713aSLionel Sambuc   if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2651f4a2713aSLionel Sambuc     if (BO->isPtrMemOp())
2652f4a2713aSLionel Sambuc       return false;
2653f4a2713aSLionel Sambuc 
2654f4a2713aSLionel Sambuc   // - opaque values (all)
2655f4a2713aSLionel Sambuc   if (isa<OpaqueValueExpr>(E))
2656f4a2713aSLionel Sambuc     return false;
2657f4a2713aSLionel Sambuc 
2658f4a2713aSLionel Sambuc   return true;
2659f4a2713aSLionel Sambuc }
2660f4a2713aSLionel Sambuc 
isImplicitCXXThis() const2661f4a2713aSLionel Sambuc bool Expr::isImplicitCXXThis() const {
2662f4a2713aSLionel Sambuc   const Expr *E = this;
2663f4a2713aSLionel Sambuc 
2664f4a2713aSLionel Sambuc   // Strip away parentheses and casts we don't care about.
2665f4a2713aSLionel Sambuc   while (true) {
2666f4a2713aSLionel Sambuc     if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2667f4a2713aSLionel Sambuc       E = Paren->getSubExpr();
2668f4a2713aSLionel Sambuc       continue;
2669f4a2713aSLionel Sambuc     }
2670f4a2713aSLionel Sambuc 
2671f4a2713aSLionel Sambuc     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2672f4a2713aSLionel Sambuc       if (ICE->getCastKind() == CK_NoOp ||
2673f4a2713aSLionel Sambuc           ICE->getCastKind() == CK_LValueToRValue ||
2674f4a2713aSLionel Sambuc           ICE->getCastKind() == CK_DerivedToBase ||
2675f4a2713aSLionel Sambuc           ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2676f4a2713aSLionel Sambuc         E = ICE->getSubExpr();
2677f4a2713aSLionel Sambuc         continue;
2678f4a2713aSLionel Sambuc       }
2679f4a2713aSLionel Sambuc     }
2680f4a2713aSLionel Sambuc 
2681f4a2713aSLionel Sambuc     if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2682f4a2713aSLionel Sambuc       if (UnOp->getOpcode() == UO_Extension) {
2683f4a2713aSLionel Sambuc         E = UnOp->getSubExpr();
2684f4a2713aSLionel Sambuc         continue;
2685f4a2713aSLionel Sambuc       }
2686f4a2713aSLionel Sambuc     }
2687f4a2713aSLionel Sambuc 
2688f4a2713aSLionel Sambuc     if (const MaterializeTemporaryExpr *M
2689f4a2713aSLionel Sambuc                                       = dyn_cast<MaterializeTemporaryExpr>(E)) {
2690f4a2713aSLionel Sambuc       E = M->GetTemporaryExpr();
2691f4a2713aSLionel Sambuc       continue;
2692f4a2713aSLionel Sambuc     }
2693f4a2713aSLionel Sambuc 
2694f4a2713aSLionel Sambuc     break;
2695f4a2713aSLionel Sambuc   }
2696f4a2713aSLionel Sambuc 
2697f4a2713aSLionel Sambuc   if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2698f4a2713aSLionel Sambuc     return This->isImplicit();
2699f4a2713aSLionel Sambuc 
2700f4a2713aSLionel Sambuc   return false;
2701f4a2713aSLionel Sambuc }
2702f4a2713aSLionel Sambuc 
2703f4a2713aSLionel Sambuc /// hasAnyTypeDependentArguments - Determines if any of the expressions
2704f4a2713aSLionel Sambuc /// in Exprs is type-dependent.
hasAnyTypeDependentArguments(ArrayRef<Expr * > Exprs)2705f4a2713aSLionel Sambuc bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
2706f4a2713aSLionel Sambuc   for (unsigned I = 0; I < Exprs.size(); ++I)
2707f4a2713aSLionel Sambuc     if (Exprs[I]->isTypeDependent())
2708f4a2713aSLionel Sambuc       return true;
2709f4a2713aSLionel Sambuc 
2710f4a2713aSLionel Sambuc   return false;
2711f4a2713aSLionel Sambuc }
2712f4a2713aSLionel Sambuc 
isConstantInitializer(ASTContext & Ctx,bool IsForRef,const Expr ** Culprit) const2713*0a6a1f1dSLionel Sambuc bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef,
2714*0a6a1f1dSLionel Sambuc                                  const Expr **Culprit) const {
2715f4a2713aSLionel Sambuc   // This function is attempting whether an expression is an initializer
2716f4a2713aSLionel Sambuc   // which can be evaluated at compile-time. It very closely parallels
2717f4a2713aSLionel Sambuc   // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2718f4a2713aSLionel Sambuc   // will lead to unexpected results.  Like ConstExprEmitter, it falls back
2719f4a2713aSLionel Sambuc   // to isEvaluatable most of the time.
2720f4a2713aSLionel Sambuc   //
2721f4a2713aSLionel Sambuc   // If we ever capture reference-binding directly in the AST, we can
2722f4a2713aSLionel Sambuc   // kill the second parameter.
2723f4a2713aSLionel Sambuc 
2724f4a2713aSLionel Sambuc   if (IsForRef) {
2725f4a2713aSLionel Sambuc     EvalResult Result;
2726*0a6a1f1dSLionel Sambuc     if (EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects)
2727*0a6a1f1dSLionel Sambuc       return true;
2728*0a6a1f1dSLionel Sambuc     if (Culprit)
2729*0a6a1f1dSLionel Sambuc       *Culprit = this;
2730*0a6a1f1dSLionel Sambuc     return false;
2731f4a2713aSLionel Sambuc   }
2732f4a2713aSLionel Sambuc 
2733f4a2713aSLionel Sambuc   switch (getStmtClass()) {
2734f4a2713aSLionel Sambuc   default: break;
2735f4a2713aSLionel Sambuc   case StringLiteralClass:
2736f4a2713aSLionel Sambuc   case ObjCEncodeExprClass:
2737f4a2713aSLionel Sambuc     return true;
2738f4a2713aSLionel Sambuc   case CXXTemporaryObjectExprClass:
2739f4a2713aSLionel Sambuc   case CXXConstructExprClass: {
2740f4a2713aSLionel Sambuc     const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
2741f4a2713aSLionel Sambuc 
2742f4a2713aSLionel Sambuc     if (CE->getConstructor()->isTrivial() &&
2743f4a2713aSLionel Sambuc         CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2744f4a2713aSLionel Sambuc       // Trivial default constructor
2745f4a2713aSLionel Sambuc       if (!CE->getNumArgs()) return true;
2746f4a2713aSLionel Sambuc 
2747f4a2713aSLionel Sambuc       // Trivial copy constructor
2748f4a2713aSLionel Sambuc       assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2749*0a6a1f1dSLionel Sambuc       return CE->getArg(0)->isConstantInitializer(Ctx, false, Culprit);
2750f4a2713aSLionel Sambuc     }
2751f4a2713aSLionel Sambuc 
2752f4a2713aSLionel Sambuc     break;
2753f4a2713aSLionel Sambuc   }
2754f4a2713aSLionel Sambuc   case CompoundLiteralExprClass: {
2755f4a2713aSLionel Sambuc     // This handles gcc's extension that allows global initializers like
2756f4a2713aSLionel Sambuc     // "struct x {int x;} x = (struct x) {};".
2757f4a2713aSLionel Sambuc     // FIXME: This accepts other cases it shouldn't!
2758f4a2713aSLionel Sambuc     const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
2759*0a6a1f1dSLionel Sambuc     return Exp->isConstantInitializer(Ctx, false, Culprit);
2760f4a2713aSLionel Sambuc   }
2761f4a2713aSLionel Sambuc   case InitListExprClass: {
2762f4a2713aSLionel Sambuc     const InitListExpr *ILE = cast<InitListExpr>(this);
2763f4a2713aSLionel Sambuc     if (ILE->getType()->isArrayType()) {
2764f4a2713aSLionel Sambuc       unsigned numInits = ILE->getNumInits();
2765f4a2713aSLionel Sambuc       for (unsigned i = 0; i < numInits; i++) {
2766*0a6a1f1dSLionel Sambuc         if (!ILE->getInit(i)->isConstantInitializer(Ctx, false, Culprit))
2767f4a2713aSLionel Sambuc           return false;
2768f4a2713aSLionel Sambuc       }
2769f4a2713aSLionel Sambuc       return true;
2770f4a2713aSLionel Sambuc     }
2771f4a2713aSLionel Sambuc 
2772f4a2713aSLionel Sambuc     if (ILE->getType()->isRecordType()) {
2773f4a2713aSLionel Sambuc       unsigned ElementNo = 0;
2774f4a2713aSLionel Sambuc       RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2775*0a6a1f1dSLionel Sambuc       for (const auto *Field : RD->fields()) {
2776f4a2713aSLionel Sambuc         // If this is a union, skip all the fields that aren't being initialized.
2777*0a6a1f1dSLionel Sambuc         if (RD->isUnion() && ILE->getInitializedFieldInUnion() != Field)
2778f4a2713aSLionel Sambuc           continue;
2779f4a2713aSLionel Sambuc 
2780f4a2713aSLionel Sambuc         // Don't emit anonymous bitfields, they just affect layout.
2781f4a2713aSLionel Sambuc         if (Field->isUnnamedBitfield())
2782f4a2713aSLionel Sambuc           continue;
2783f4a2713aSLionel Sambuc 
2784f4a2713aSLionel Sambuc         if (ElementNo < ILE->getNumInits()) {
2785f4a2713aSLionel Sambuc           const Expr *Elt = ILE->getInit(ElementNo++);
2786f4a2713aSLionel Sambuc           if (Field->isBitField()) {
2787f4a2713aSLionel Sambuc             // Bitfields have to evaluate to an integer.
2788f4a2713aSLionel Sambuc             llvm::APSInt ResultTmp;
2789*0a6a1f1dSLionel Sambuc             if (!Elt->EvaluateAsInt(ResultTmp, Ctx)) {
2790*0a6a1f1dSLionel Sambuc               if (Culprit)
2791*0a6a1f1dSLionel Sambuc                 *Culprit = Elt;
2792f4a2713aSLionel Sambuc               return false;
2793*0a6a1f1dSLionel Sambuc             }
2794f4a2713aSLionel Sambuc           } else {
2795f4a2713aSLionel Sambuc             bool RefType = Field->getType()->isReferenceType();
2796*0a6a1f1dSLionel Sambuc             if (!Elt->isConstantInitializer(Ctx, RefType, Culprit))
2797f4a2713aSLionel Sambuc               return false;
2798f4a2713aSLionel Sambuc           }
2799f4a2713aSLionel Sambuc         }
2800f4a2713aSLionel Sambuc       }
2801f4a2713aSLionel Sambuc       return true;
2802f4a2713aSLionel Sambuc     }
2803f4a2713aSLionel Sambuc 
2804f4a2713aSLionel Sambuc     break;
2805f4a2713aSLionel Sambuc   }
2806f4a2713aSLionel Sambuc   case ImplicitValueInitExprClass:
2807f4a2713aSLionel Sambuc     return true;
2808f4a2713aSLionel Sambuc   case ParenExprClass:
2809f4a2713aSLionel Sambuc     return cast<ParenExpr>(this)->getSubExpr()
2810*0a6a1f1dSLionel Sambuc       ->isConstantInitializer(Ctx, IsForRef, Culprit);
2811f4a2713aSLionel Sambuc   case GenericSelectionExprClass:
2812f4a2713aSLionel Sambuc     return cast<GenericSelectionExpr>(this)->getResultExpr()
2813*0a6a1f1dSLionel Sambuc       ->isConstantInitializer(Ctx, IsForRef, Culprit);
2814f4a2713aSLionel Sambuc   case ChooseExprClass:
2815*0a6a1f1dSLionel Sambuc     if (cast<ChooseExpr>(this)->isConditionDependent()) {
2816*0a6a1f1dSLionel Sambuc       if (Culprit)
2817*0a6a1f1dSLionel Sambuc         *Culprit = this;
2818f4a2713aSLionel Sambuc       return false;
2819*0a6a1f1dSLionel Sambuc     }
2820f4a2713aSLionel Sambuc     return cast<ChooseExpr>(this)->getChosenSubExpr()
2821*0a6a1f1dSLionel Sambuc       ->isConstantInitializer(Ctx, IsForRef, Culprit);
2822f4a2713aSLionel Sambuc   case UnaryOperatorClass: {
2823f4a2713aSLionel Sambuc     const UnaryOperator* Exp = cast<UnaryOperator>(this);
2824f4a2713aSLionel Sambuc     if (Exp->getOpcode() == UO_Extension)
2825*0a6a1f1dSLionel Sambuc       return Exp->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
2826f4a2713aSLionel Sambuc     break;
2827f4a2713aSLionel Sambuc   }
2828f4a2713aSLionel Sambuc   case CXXFunctionalCastExprClass:
2829f4a2713aSLionel Sambuc   case CXXStaticCastExprClass:
2830f4a2713aSLionel Sambuc   case ImplicitCastExprClass:
2831f4a2713aSLionel Sambuc   case CStyleCastExprClass:
2832f4a2713aSLionel Sambuc   case ObjCBridgedCastExprClass:
2833f4a2713aSLionel Sambuc   case CXXDynamicCastExprClass:
2834f4a2713aSLionel Sambuc   case CXXReinterpretCastExprClass:
2835f4a2713aSLionel Sambuc   case CXXConstCastExprClass: {
2836f4a2713aSLionel Sambuc     const CastExpr *CE = cast<CastExpr>(this);
2837f4a2713aSLionel Sambuc 
2838f4a2713aSLionel Sambuc     // Handle misc casts we want to ignore.
2839f4a2713aSLionel Sambuc     if (CE->getCastKind() == CK_NoOp ||
2840f4a2713aSLionel Sambuc         CE->getCastKind() == CK_LValueToRValue ||
2841f4a2713aSLionel Sambuc         CE->getCastKind() == CK_ToUnion ||
2842f4a2713aSLionel Sambuc         CE->getCastKind() == CK_ConstructorConversion ||
2843f4a2713aSLionel Sambuc         CE->getCastKind() == CK_NonAtomicToAtomic ||
2844f4a2713aSLionel Sambuc         CE->getCastKind() == CK_AtomicToNonAtomic)
2845*0a6a1f1dSLionel Sambuc       return CE->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
2846f4a2713aSLionel Sambuc 
2847f4a2713aSLionel Sambuc     break;
2848f4a2713aSLionel Sambuc   }
2849f4a2713aSLionel Sambuc   case MaterializeTemporaryExprClass:
2850f4a2713aSLionel Sambuc     return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
2851*0a6a1f1dSLionel Sambuc       ->isConstantInitializer(Ctx, false, Culprit);
2852f4a2713aSLionel Sambuc 
2853f4a2713aSLionel Sambuc   case SubstNonTypeTemplateParmExprClass:
2854f4a2713aSLionel Sambuc     return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2855*0a6a1f1dSLionel Sambuc       ->isConstantInitializer(Ctx, false, Culprit);
2856f4a2713aSLionel Sambuc   case CXXDefaultArgExprClass:
2857f4a2713aSLionel Sambuc     return cast<CXXDefaultArgExpr>(this)->getExpr()
2858*0a6a1f1dSLionel Sambuc       ->isConstantInitializer(Ctx, false, Culprit);
2859f4a2713aSLionel Sambuc   case CXXDefaultInitExprClass:
2860f4a2713aSLionel Sambuc     return cast<CXXDefaultInitExpr>(this)->getExpr()
2861*0a6a1f1dSLionel Sambuc       ->isConstantInitializer(Ctx, false, Culprit);
2862f4a2713aSLionel Sambuc   }
2863*0a6a1f1dSLionel Sambuc   if (isEvaluatable(Ctx))
2864*0a6a1f1dSLionel Sambuc     return true;
2865*0a6a1f1dSLionel Sambuc   if (Culprit)
2866*0a6a1f1dSLionel Sambuc     *Culprit = this;
2867*0a6a1f1dSLionel Sambuc   return false;
2868f4a2713aSLionel Sambuc }
2869f4a2713aSLionel Sambuc 
HasSideEffects(const ASTContext & Ctx,bool IncludePossibleEffects) const2870*0a6a1f1dSLionel Sambuc bool Expr::HasSideEffects(const ASTContext &Ctx,
2871*0a6a1f1dSLionel Sambuc                           bool IncludePossibleEffects) const {
2872*0a6a1f1dSLionel Sambuc   // In circumstances where we care about definite side effects instead of
2873*0a6a1f1dSLionel Sambuc   // potential side effects, we want to ignore expressions that are part of a
2874*0a6a1f1dSLionel Sambuc   // macro expansion as a potential side effect.
2875*0a6a1f1dSLionel Sambuc   if (!IncludePossibleEffects && getExprLoc().isMacroID())
2876*0a6a1f1dSLionel Sambuc     return false;
2877*0a6a1f1dSLionel Sambuc 
2878f4a2713aSLionel Sambuc   if (isInstantiationDependent())
2879*0a6a1f1dSLionel Sambuc     return IncludePossibleEffects;
2880f4a2713aSLionel Sambuc 
2881f4a2713aSLionel Sambuc   switch (getStmtClass()) {
2882f4a2713aSLionel Sambuc   case NoStmtClass:
2883f4a2713aSLionel Sambuc   #define ABSTRACT_STMT(Type)
2884f4a2713aSLionel Sambuc   #define STMT(Type, Base) case Type##Class:
2885f4a2713aSLionel Sambuc   #define EXPR(Type, Base)
2886f4a2713aSLionel Sambuc   #include "clang/AST/StmtNodes.inc"
2887f4a2713aSLionel Sambuc     llvm_unreachable("unexpected Expr kind");
2888f4a2713aSLionel Sambuc 
2889f4a2713aSLionel Sambuc   case DependentScopeDeclRefExprClass:
2890f4a2713aSLionel Sambuc   case CXXUnresolvedConstructExprClass:
2891f4a2713aSLionel Sambuc   case CXXDependentScopeMemberExprClass:
2892f4a2713aSLionel Sambuc   case UnresolvedLookupExprClass:
2893f4a2713aSLionel Sambuc   case UnresolvedMemberExprClass:
2894f4a2713aSLionel Sambuc   case PackExpansionExprClass:
2895f4a2713aSLionel Sambuc   case SubstNonTypeTemplateParmPackExprClass:
2896f4a2713aSLionel Sambuc   case FunctionParmPackExprClass:
2897*0a6a1f1dSLionel Sambuc   case TypoExprClass:
2898*0a6a1f1dSLionel Sambuc   case CXXFoldExprClass:
2899f4a2713aSLionel Sambuc     llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2900f4a2713aSLionel Sambuc 
2901f4a2713aSLionel Sambuc   case DeclRefExprClass:
2902f4a2713aSLionel Sambuc   case ObjCIvarRefExprClass:
2903f4a2713aSLionel Sambuc   case PredefinedExprClass:
2904f4a2713aSLionel Sambuc   case IntegerLiteralClass:
2905f4a2713aSLionel Sambuc   case FloatingLiteralClass:
2906f4a2713aSLionel Sambuc   case ImaginaryLiteralClass:
2907f4a2713aSLionel Sambuc   case StringLiteralClass:
2908f4a2713aSLionel Sambuc   case CharacterLiteralClass:
2909f4a2713aSLionel Sambuc   case OffsetOfExprClass:
2910f4a2713aSLionel Sambuc   case ImplicitValueInitExprClass:
2911f4a2713aSLionel Sambuc   case UnaryExprOrTypeTraitExprClass:
2912f4a2713aSLionel Sambuc   case AddrLabelExprClass:
2913f4a2713aSLionel Sambuc   case GNUNullExprClass:
2914f4a2713aSLionel Sambuc   case CXXBoolLiteralExprClass:
2915f4a2713aSLionel Sambuc   case CXXNullPtrLiteralExprClass:
2916f4a2713aSLionel Sambuc   case CXXThisExprClass:
2917f4a2713aSLionel Sambuc   case CXXScalarValueInitExprClass:
2918f4a2713aSLionel Sambuc   case TypeTraitExprClass:
2919f4a2713aSLionel Sambuc   case ArrayTypeTraitExprClass:
2920f4a2713aSLionel Sambuc   case ExpressionTraitExprClass:
2921f4a2713aSLionel Sambuc   case CXXNoexceptExprClass:
2922f4a2713aSLionel Sambuc   case SizeOfPackExprClass:
2923f4a2713aSLionel Sambuc   case ObjCStringLiteralClass:
2924f4a2713aSLionel Sambuc   case ObjCEncodeExprClass:
2925f4a2713aSLionel Sambuc   case ObjCBoolLiteralExprClass:
2926f4a2713aSLionel Sambuc   case CXXUuidofExprClass:
2927f4a2713aSLionel Sambuc   case OpaqueValueExprClass:
2928f4a2713aSLionel Sambuc     // These never have a side-effect.
2929f4a2713aSLionel Sambuc     return false;
2930f4a2713aSLionel Sambuc 
2931f4a2713aSLionel Sambuc   case CallExprClass:
2932*0a6a1f1dSLionel Sambuc   case CXXOperatorCallExprClass:
2933*0a6a1f1dSLionel Sambuc   case CXXMemberCallExprClass:
2934*0a6a1f1dSLionel Sambuc   case CUDAKernelCallExprClass:
2935*0a6a1f1dSLionel Sambuc   case BlockExprClass:
2936*0a6a1f1dSLionel Sambuc   case CXXBindTemporaryExprClass:
2937*0a6a1f1dSLionel Sambuc   case UserDefinedLiteralClass:
2938*0a6a1f1dSLionel Sambuc     // We don't know a call definitely has side effects, but we can check the
2939*0a6a1f1dSLionel Sambuc     // call's operands.
2940*0a6a1f1dSLionel Sambuc     if (!IncludePossibleEffects)
2941*0a6a1f1dSLionel Sambuc       break;
2942*0a6a1f1dSLionel Sambuc     return true;
2943*0a6a1f1dSLionel Sambuc 
2944f4a2713aSLionel Sambuc   case MSPropertyRefExprClass:
2945f4a2713aSLionel Sambuc   case CompoundAssignOperatorClass:
2946f4a2713aSLionel Sambuc   case VAArgExprClass:
2947f4a2713aSLionel Sambuc   case AtomicExprClass:
2948f4a2713aSLionel Sambuc   case StmtExprClass:
2949f4a2713aSLionel Sambuc   case CXXThrowExprClass:
2950f4a2713aSLionel Sambuc   case CXXNewExprClass:
2951f4a2713aSLionel Sambuc   case CXXDeleteExprClass:
2952f4a2713aSLionel Sambuc   case ExprWithCleanupsClass:
2953f4a2713aSLionel Sambuc     // These always have a side-effect.
2954f4a2713aSLionel Sambuc     return true;
2955f4a2713aSLionel Sambuc 
2956f4a2713aSLionel Sambuc   case ParenExprClass:
2957f4a2713aSLionel Sambuc   case ArraySubscriptExprClass:
2958f4a2713aSLionel Sambuc   case MemberExprClass:
2959f4a2713aSLionel Sambuc   case ConditionalOperatorClass:
2960f4a2713aSLionel Sambuc   case BinaryConditionalOperatorClass:
2961f4a2713aSLionel Sambuc   case CompoundLiteralExprClass:
2962f4a2713aSLionel Sambuc   case ExtVectorElementExprClass:
2963f4a2713aSLionel Sambuc   case DesignatedInitExprClass:
2964f4a2713aSLionel Sambuc   case ParenListExprClass:
2965f4a2713aSLionel Sambuc   case CXXPseudoDestructorExprClass:
2966f4a2713aSLionel Sambuc   case CXXStdInitializerListExprClass:
2967f4a2713aSLionel Sambuc   case SubstNonTypeTemplateParmExprClass:
2968f4a2713aSLionel Sambuc   case MaterializeTemporaryExprClass:
2969f4a2713aSLionel Sambuc   case ShuffleVectorExprClass:
2970f4a2713aSLionel Sambuc   case ConvertVectorExprClass:
2971f4a2713aSLionel Sambuc   case AsTypeExprClass:
2972f4a2713aSLionel Sambuc     // These have a side-effect if any subexpression does.
2973f4a2713aSLionel Sambuc     break;
2974f4a2713aSLionel Sambuc 
2975f4a2713aSLionel Sambuc   case UnaryOperatorClass:
2976f4a2713aSLionel Sambuc     if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
2977f4a2713aSLionel Sambuc       return true;
2978f4a2713aSLionel Sambuc     break;
2979f4a2713aSLionel Sambuc 
2980f4a2713aSLionel Sambuc   case BinaryOperatorClass:
2981f4a2713aSLionel Sambuc     if (cast<BinaryOperator>(this)->isAssignmentOp())
2982f4a2713aSLionel Sambuc       return true;
2983f4a2713aSLionel Sambuc     break;
2984f4a2713aSLionel Sambuc 
2985f4a2713aSLionel Sambuc   case InitListExprClass:
2986f4a2713aSLionel Sambuc     // FIXME: The children for an InitListExpr doesn't include the array filler.
2987f4a2713aSLionel Sambuc     if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2988*0a6a1f1dSLionel Sambuc       if (E->HasSideEffects(Ctx, IncludePossibleEffects))
2989f4a2713aSLionel Sambuc         return true;
2990f4a2713aSLionel Sambuc     break;
2991f4a2713aSLionel Sambuc 
2992f4a2713aSLionel Sambuc   case GenericSelectionExprClass:
2993f4a2713aSLionel Sambuc     return cast<GenericSelectionExpr>(this)->getResultExpr()->
2994*0a6a1f1dSLionel Sambuc         HasSideEffects(Ctx, IncludePossibleEffects);
2995f4a2713aSLionel Sambuc 
2996f4a2713aSLionel Sambuc   case ChooseExprClass:
2997*0a6a1f1dSLionel Sambuc     return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(
2998*0a6a1f1dSLionel Sambuc         Ctx, IncludePossibleEffects);
2999f4a2713aSLionel Sambuc 
3000f4a2713aSLionel Sambuc   case CXXDefaultArgExprClass:
3001*0a6a1f1dSLionel Sambuc     return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(
3002*0a6a1f1dSLionel Sambuc         Ctx, IncludePossibleEffects);
3003f4a2713aSLionel Sambuc 
3004*0a6a1f1dSLionel Sambuc   case CXXDefaultInitExprClass: {
3005*0a6a1f1dSLionel Sambuc     const FieldDecl *FD = cast<CXXDefaultInitExpr>(this)->getField();
3006*0a6a1f1dSLionel Sambuc     if (const Expr *E = FD->getInClassInitializer())
3007*0a6a1f1dSLionel Sambuc       return E->HasSideEffects(Ctx, IncludePossibleEffects);
3008f4a2713aSLionel Sambuc     // If we've not yet parsed the initializer, assume it has side-effects.
3009f4a2713aSLionel Sambuc     return true;
3010*0a6a1f1dSLionel Sambuc   }
3011f4a2713aSLionel Sambuc 
3012f4a2713aSLionel Sambuc   case CXXDynamicCastExprClass: {
3013f4a2713aSLionel Sambuc     // A dynamic_cast expression has side-effects if it can throw.
3014f4a2713aSLionel Sambuc     const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
3015f4a2713aSLionel Sambuc     if (DCE->getTypeAsWritten()->isReferenceType() &&
3016f4a2713aSLionel Sambuc         DCE->getCastKind() == CK_Dynamic)
3017f4a2713aSLionel Sambuc       return true;
3018f4a2713aSLionel Sambuc   } // Fall through.
3019f4a2713aSLionel Sambuc   case ImplicitCastExprClass:
3020f4a2713aSLionel Sambuc   case CStyleCastExprClass:
3021f4a2713aSLionel Sambuc   case CXXStaticCastExprClass:
3022f4a2713aSLionel Sambuc   case CXXReinterpretCastExprClass:
3023f4a2713aSLionel Sambuc   case CXXConstCastExprClass:
3024f4a2713aSLionel Sambuc   case CXXFunctionalCastExprClass: {
3025*0a6a1f1dSLionel Sambuc     // While volatile reads are side-effecting in both C and C++, we treat them
3026*0a6a1f1dSLionel Sambuc     // as having possible (not definite) side-effects. This allows idiomatic
3027*0a6a1f1dSLionel Sambuc     // code to behave without warning, such as sizeof(*v) for a volatile-
3028*0a6a1f1dSLionel Sambuc     // qualified pointer.
3029*0a6a1f1dSLionel Sambuc     if (!IncludePossibleEffects)
3030*0a6a1f1dSLionel Sambuc       break;
3031*0a6a1f1dSLionel Sambuc 
3032f4a2713aSLionel Sambuc     const CastExpr *CE = cast<CastExpr>(this);
3033f4a2713aSLionel Sambuc     if (CE->getCastKind() == CK_LValueToRValue &&
3034f4a2713aSLionel Sambuc         CE->getSubExpr()->getType().isVolatileQualified())
3035f4a2713aSLionel Sambuc       return true;
3036f4a2713aSLionel Sambuc     break;
3037f4a2713aSLionel Sambuc   }
3038f4a2713aSLionel Sambuc 
3039f4a2713aSLionel Sambuc   case CXXTypeidExprClass:
3040f4a2713aSLionel Sambuc     // typeid might throw if its subexpression is potentially-evaluated, so has
3041f4a2713aSLionel Sambuc     // side-effects in that case whether or not its subexpression does.
3042f4a2713aSLionel Sambuc     return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
3043f4a2713aSLionel Sambuc 
3044f4a2713aSLionel Sambuc   case CXXConstructExprClass:
3045f4a2713aSLionel Sambuc   case CXXTemporaryObjectExprClass: {
3046f4a2713aSLionel Sambuc     const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
3047*0a6a1f1dSLionel Sambuc     if (!CE->getConstructor()->isTrivial() && IncludePossibleEffects)
3048f4a2713aSLionel Sambuc       return true;
3049f4a2713aSLionel Sambuc     // A trivial constructor does not add any side-effects of its own. Just look
3050f4a2713aSLionel Sambuc     // at its arguments.
3051f4a2713aSLionel Sambuc     break;
3052f4a2713aSLionel Sambuc   }
3053f4a2713aSLionel Sambuc 
3054f4a2713aSLionel Sambuc   case LambdaExprClass: {
3055f4a2713aSLionel Sambuc     const LambdaExpr *LE = cast<LambdaExpr>(this);
3056f4a2713aSLionel Sambuc     for (LambdaExpr::capture_iterator I = LE->capture_begin(),
3057f4a2713aSLionel Sambuc                                       E = LE->capture_end(); I != E; ++I)
3058f4a2713aSLionel Sambuc       if (I->getCaptureKind() == LCK_ByCopy)
3059f4a2713aSLionel Sambuc         // FIXME: Only has a side-effect if the variable is volatile or if
3060f4a2713aSLionel Sambuc         // the copy would invoke a non-trivial copy constructor.
3061f4a2713aSLionel Sambuc         return true;
3062f4a2713aSLionel Sambuc     return false;
3063f4a2713aSLionel Sambuc   }
3064f4a2713aSLionel Sambuc 
3065f4a2713aSLionel Sambuc   case PseudoObjectExprClass: {
3066f4a2713aSLionel Sambuc     // Only look for side-effects in the semantic form, and look past
3067f4a2713aSLionel Sambuc     // OpaqueValueExpr bindings in that form.
3068f4a2713aSLionel Sambuc     const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
3069f4a2713aSLionel Sambuc     for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
3070f4a2713aSLionel Sambuc                                                     E = PO->semantics_end();
3071f4a2713aSLionel Sambuc          I != E; ++I) {
3072f4a2713aSLionel Sambuc       const Expr *Subexpr = *I;
3073f4a2713aSLionel Sambuc       if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
3074f4a2713aSLionel Sambuc         Subexpr = OVE->getSourceExpr();
3075*0a6a1f1dSLionel Sambuc       if (Subexpr->HasSideEffects(Ctx, IncludePossibleEffects))
3076f4a2713aSLionel Sambuc         return true;
3077f4a2713aSLionel Sambuc     }
3078f4a2713aSLionel Sambuc     return false;
3079f4a2713aSLionel Sambuc   }
3080f4a2713aSLionel Sambuc 
3081f4a2713aSLionel Sambuc   case ObjCBoxedExprClass:
3082f4a2713aSLionel Sambuc   case ObjCArrayLiteralClass:
3083f4a2713aSLionel Sambuc   case ObjCDictionaryLiteralClass:
3084f4a2713aSLionel Sambuc   case ObjCSelectorExprClass:
3085f4a2713aSLionel Sambuc   case ObjCProtocolExprClass:
3086f4a2713aSLionel Sambuc   case ObjCIsaExprClass:
3087f4a2713aSLionel Sambuc   case ObjCIndirectCopyRestoreExprClass:
3088f4a2713aSLionel Sambuc   case ObjCSubscriptRefExprClass:
3089f4a2713aSLionel Sambuc   case ObjCBridgedCastExprClass:
3090*0a6a1f1dSLionel Sambuc   case ObjCMessageExprClass:
3091*0a6a1f1dSLionel Sambuc   case ObjCPropertyRefExprClass:
3092f4a2713aSLionel Sambuc   // FIXME: Classify these cases better.
3093*0a6a1f1dSLionel Sambuc     if (IncludePossibleEffects)
3094f4a2713aSLionel Sambuc       return true;
3095*0a6a1f1dSLionel Sambuc     break;
3096f4a2713aSLionel Sambuc   }
3097f4a2713aSLionel Sambuc 
3098f4a2713aSLionel Sambuc   // Recurse to children.
3099f4a2713aSLionel Sambuc   for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
3100f4a2713aSLionel Sambuc     if (const Stmt *S = *SubStmts)
3101*0a6a1f1dSLionel Sambuc       if (cast<Expr>(S)->HasSideEffects(Ctx, IncludePossibleEffects))
3102f4a2713aSLionel Sambuc         return true;
3103f4a2713aSLionel Sambuc 
3104f4a2713aSLionel Sambuc   return false;
3105f4a2713aSLionel Sambuc }
3106f4a2713aSLionel Sambuc 
3107f4a2713aSLionel Sambuc namespace {
3108f4a2713aSLionel Sambuc   /// \brief Look for a call to a non-trivial function within an expression.
3109f4a2713aSLionel Sambuc   class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
3110f4a2713aSLionel Sambuc   {
3111f4a2713aSLionel Sambuc     typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3112f4a2713aSLionel Sambuc 
3113f4a2713aSLionel Sambuc     bool NonTrivial;
3114f4a2713aSLionel Sambuc 
3115f4a2713aSLionel Sambuc   public:
NonTrivialCallFinder(ASTContext & Context)3116f4a2713aSLionel Sambuc     explicit NonTrivialCallFinder(ASTContext &Context)
3117f4a2713aSLionel Sambuc       : Inherited(Context), NonTrivial(false) { }
3118f4a2713aSLionel Sambuc 
hasNonTrivialCall() const3119f4a2713aSLionel Sambuc     bool hasNonTrivialCall() const { return NonTrivial; }
3120f4a2713aSLionel Sambuc 
VisitCallExpr(CallExpr * E)3121f4a2713aSLionel Sambuc     void VisitCallExpr(CallExpr *E) {
3122f4a2713aSLionel Sambuc       if (CXXMethodDecl *Method
3123f4a2713aSLionel Sambuc           = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3124f4a2713aSLionel Sambuc         if (Method->isTrivial()) {
3125f4a2713aSLionel Sambuc           // Recurse to children of the call.
3126f4a2713aSLionel Sambuc           Inherited::VisitStmt(E);
3127f4a2713aSLionel Sambuc           return;
3128f4a2713aSLionel Sambuc         }
3129f4a2713aSLionel Sambuc       }
3130f4a2713aSLionel Sambuc 
3131f4a2713aSLionel Sambuc       NonTrivial = true;
3132f4a2713aSLionel Sambuc     }
3133f4a2713aSLionel Sambuc 
VisitCXXConstructExpr(CXXConstructExpr * E)3134f4a2713aSLionel Sambuc     void VisitCXXConstructExpr(CXXConstructExpr *E) {
3135f4a2713aSLionel Sambuc       if (E->getConstructor()->isTrivial()) {
3136f4a2713aSLionel Sambuc         // Recurse to children of the call.
3137f4a2713aSLionel Sambuc         Inherited::VisitStmt(E);
3138f4a2713aSLionel Sambuc         return;
3139f4a2713aSLionel Sambuc       }
3140f4a2713aSLionel Sambuc 
3141f4a2713aSLionel Sambuc       NonTrivial = true;
3142f4a2713aSLionel Sambuc     }
3143f4a2713aSLionel Sambuc 
VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr * E)3144f4a2713aSLionel Sambuc     void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3145f4a2713aSLionel Sambuc       if (E->getTemporary()->getDestructor()->isTrivial()) {
3146f4a2713aSLionel Sambuc         Inherited::VisitStmt(E);
3147f4a2713aSLionel Sambuc         return;
3148f4a2713aSLionel Sambuc       }
3149f4a2713aSLionel Sambuc 
3150f4a2713aSLionel Sambuc       NonTrivial = true;
3151f4a2713aSLionel Sambuc     }
3152f4a2713aSLionel Sambuc   };
3153f4a2713aSLionel Sambuc }
3154f4a2713aSLionel Sambuc 
hasNonTrivialCall(ASTContext & Ctx)3155f4a2713aSLionel Sambuc bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3156f4a2713aSLionel Sambuc   NonTrivialCallFinder Finder(Ctx);
3157f4a2713aSLionel Sambuc   Finder.Visit(this);
3158f4a2713aSLionel Sambuc   return Finder.hasNonTrivialCall();
3159f4a2713aSLionel Sambuc }
3160f4a2713aSLionel Sambuc 
3161f4a2713aSLionel Sambuc /// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3162f4a2713aSLionel Sambuc /// pointer constant or not, as well as the specific kind of constant detected.
3163f4a2713aSLionel Sambuc /// Null pointer constants can be integer constant expressions with the
3164f4a2713aSLionel Sambuc /// value zero, casts of zero to void*, nullptr (C++0X), or __null
3165f4a2713aSLionel Sambuc /// (a GNU extension).
3166f4a2713aSLionel Sambuc Expr::NullPointerConstantKind
isNullPointerConstant(ASTContext & Ctx,NullPointerConstantValueDependence NPC) const3167f4a2713aSLionel Sambuc Expr::isNullPointerConstant(ASTContext &Ctx,
3168f4a2713aSLionel Sambuc                             NullPointerConstantValueDependence NPC) const {
3169f4a2713aSLionel Sambuc   if (isValueDependent() &&
3170*0a6a1f1dSLionel Sambuc       (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
3171f4a2713aSLionel Sambuc     switch (NPC) {
3172f4a2713aSLionel Sambuc     case NPC_NeverValueDependent:
3173f4a2713aSLionel Sambuc       llvm_unreachable("Unexpected value dependent expression!");
3174f4a2713aSLionel Sambuc     case NPC_ValueDependentIsNull:
3175f4a2713aSLionel Sambuc       if (isTypeDependent() || getType()->isIntegralType(Ctx))
3176f4a2713aSLionel Sambuc         return NPCK_ZeroExpression;
3177f4a2713aSLionel Sambuc       else
3178f4a2713aSLionel Sambuc         return NPCK_NotNull;
3179f4a2713aSLionel Sambuc 
3180f4a2713aSLionel Sambuc     case NPC_ValueDependentIsNotNull:
3181f4a2713aSLionel Sambuc       return NPCK_NotNull;
3182f4a2713aSLionel Sambuc     }
3183f4a2713aSLionel Sambuc   }
3184f4a2713aSLionel Sambuc 
3185f4a2713aSLionel Sambuc   // Strip off a cast to void*, if it exists. Except in C++.
3186f4a2713aSLionel Sambuc   if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
3187f4a2713aSLionel Sambuc     if (!Ctx.getLangOpts().CPlusPlus) {
3188f4a2713aSLionel Sambuc       // Check that it is a cast to void*.
3189f4a2713aSLionel Sambuc       if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
3190f4a2713aSLionel Sambuc         QualType Pointee = PT->getPointeeType();
3191f4a2713aSLionel Sambuc         if (!Pointee.hasQualifiers() &&
3192f4a2713aSLionel Sambuc             Pointee->isVoidType() &&                              // to void*
3193f4a2713aSLionel Sambuc             CE->getSubExpr()->getType()->isIntegerType())         // from int.
3194f4a2713aSLionel Sambuc           return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
3195f4a2713aSLionel Sambuc       }
3196f4a2713aSLionel Sambuc     }
3197f4a2713aSLionel Sambuc   } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3198f4a2713aSLionel Sambuc     // Ignore the ImplicitCastExpr type entirely.
3199f4a2713aSLionel Sambuc     return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
3200f4a2713aSLionel Sambuc   } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3201f4a2713aSLionel Sambuc     // Accept ((void*)0) as a null pointer constant, as many other
3202f4a2713aSLionel Sambuc     // implementations do.
3203f4a2713aSLionel Sambuc     return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
3204f4a2713aSLionel Sambuc   } else if (const GenericSelectionExpr *GE =
3205f4a2713aSLionel Sambuc                dyn_cast<GenericSelectionExpr>(this)) {
3206f4a2713aSLionel Sambuc     if (GE->isResultDependent())
3207f4a2713aSLionel Sambuc       return NPCK_NotNull;
3208f4a2713aSLionel Sambuc     return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
3209f4a2713aSLionel Sambuc   } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3210f4a2713aSLionel Sambuc     if (CE->isConditionDependent())
3211f4a2713aSLionel Sambuc       return NPCK_NotNull;
3212f4a2713aSLionel Sambuc     return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
3213f4a2713aSLionel Sambuc   } else if (const CXXDefaultArgExpr *DefaultArg
3214f4a2713aSLionel Sambuc                = dyn_cast<CXXDefaultArgExpr>(this)) {
3215f4a2713aSLionel Sambuc     // See through default argument expressions.
3216f4a2713aSLionel Sambuc     return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
3217f4a2713aSLionel Sambuc   } else if (const CXXDefaultInitExpr *DefaultInit
3218f4a2713aSLionel Sambuc                = dyn_cast<CXXDefaultInitExpr>(this)) {
3219f4a2713aSLionel Sambuc     // See through default initializer expressions.
3220f4a2713aSLionel Sambuc     return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
3221f4a2713aSLionel Sambuc   } else if (isa<GNUNullExpr>(this)) {
3222f4a2713aSLionel Sambuc     // The GNU __null extension is always a null pointer constant.
3223f4a2713aSLionel Sambuc     return NPCK_GNUNull;
3224f4a2713aSLionel Sambuc   } else if (const MaterializeTemporaryExpr *M
3225f4a2713aSLionel Sambuc                                    = dyn_cast<MaterializeTemporaryExpr>(this)) {
3226f4a2713aSLionel Sambuc     return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
3227f4a2713aSLionel Sambuc   } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3228f4a2713aSLionel Sambuc     if (const Expr *Source = OVE->getSourceExpr())
3229f4a2713aSLionel Sambuc       return Source->isNullPointerConstant(Ctx, NPC);
3230f4a2713aSLionel Sambuc   }
3231f4a2713aSLionel Sambuc 
3232f4a2713aSLionel Sambuc   // C++11 nullptr_t is always a null pointer constant.
3233f4a2713aSLionel Sambuc   if (getType()->isNullPtrType())
3234f4a2713aSLionel Sambuc     return NPCK_CXX11_nullptr;
3235f4a2713aSLionel Sambuc 
3236f4a2713aSLionel Sambuc   if (const RecordType *UT = getType()->getAsUnionType())
3237f4a2713aSLionel Sambuc     if (!Ctx.getLangOpts().CPlusPlus11 &&
3238f4a2713aSLionel Sambuc         UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
3239f4a2713aSLionel Sambuc       if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3240f4a2713aSLionel Sambuc         const Expr *InitExpr = CLE->getInitializer();
3241f4a2713aSLionel Sambuc         if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3242f4a2713aSLionel Sambuc           return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3243f4a2713aSLionel Sambuc       }
3244f4a2713aSLionel Sambuc   // This expression must be an integer type.
3245f4a2713aSLionel Sambuc   if (!getType()->isIntegerType() ||
3246f4a2713aSLionel Sambuc       (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
3247f4a2713aSLionel Sambuc     return NPCK_NotNull;
3248f4a2713aSLionel Sambuc 
3249f4a2713aSLionel Sambuc   if (Ctx.getLangOpts().CPlusPlus11) {
3250f4a2713aSLionel Sambuc     // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3251f4a2713aSLionel Sambuc     // value zero or a prvalue of type std::nullptr_t.
3252f4a2713aSLionel Sambuc     // Microsoft mode permits C++98 rules reflecting MSVC behavior.
3253f4a2713aSLionel Sambuc     const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
3254f4a2713aSLionel Sambuc     if (Lit && !Lit->getValue())
3255f4a2713aSLionel Sambuc       return NPCK_ZeroLiteral;
3256*0a6a1f1dSLionel Sambuc     else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
3257f4a2713aSLionel Sambuc       return NPCK_NotNull;
3258f4a2713aSLionel Sambuc   } else {
3259f4a2713aSLionel Sambuc     // If we have an integer constant expression, we need to *evaluate* it and
3260f4a2713aSLionel Sambuc     // test for the value 0.
3261f4a2713aSLionel Sambuc     if (!isIntegerConstantExpr(Ctx))
3262f4a2713aSLionel Sambuc       return NPCK_NotNull;
3263f4a2713aSLionel Sambuc   }
3264f4a2713aSLionel Sambuc 
3265f4a2713aSLionel Sambuc   if (EvaluateKnownConstInt(Ctx) != 0)
3266f4a2713aSLionel Sambuc     return NPCK_NotNull;
3267f4a2713aSLionel Sambuc 
3268f4a2713aSLionel Sambuc   if (isa<IntegerLiteral>(this))
3269f4a2713aSLionel Sambuc     return NPCK_ZeroLiteral;
3270f4a2713aSLionel Sambuc   return NPCK_ZeroExpression;
3271f4a2713aSLionel Sambuc }
3272f4a2713aSLionel Sambuc 
3273f4a2713aSLionel Sambuc /// \brief If this expression is an l-value for an Objective C
3274f4a2713aSLionel Sambuc /// property, find the underlying property reference expression.
getObjCProperty() const3275f4a2713aSLionel Sambuc const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3276f4a2713aSLionel Sambuc   const Expr *E = this;
3277f4a2713aSLionel Sambuc   while (true) {
3278f4a2713aSLionel Sambuc     assert((E->getValueKind() == VK_LValue &&
3279f4a2713aSLionel Sambuc             E->getObjectKind() == OK_ObjCProperty) &&
3280f4a2713aSLionel Sambuc            "expression is not a property reference");
3281f4a2713aSLionel Sambuc     E = E->IgnoreParenCasts();
3282f4a2713aSLionel Sambuc     if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3283f4a2713aSLionel Sambuc       if (BO->getOpcode() == BO_Comma) {
3284f4a2713aSLionel Sambuc         E = BO->getRHS();
3285f4a2713aSLionel Sambuc         continue;
3286f4a2713aSLionel Sambuc       }
3287f4a2713aSLionel Sambuc     }
3288f4a2713aSLionel Sambuc 
3289f4a2713aSLionel Sambuc     break;
3290f4a2713aSLionel Sambuc   }
3291f4a2713aSLionel Sambuc 
3292f4a2713aSLionel Sambuc   return cast<ObjCPropertyRefExpr>(E);
3293f4a2713aSLionel Sambuc }
3294f4a2713aSLionel Sambuc 
isObjCSelfExpr() const3295f4a2713aSLionel Sambuc bool Expr::isObjCSelfExpr() const {
3296f4a2713aSLionel Sambuc   const Expr *E = IgnoreParenImpCasts();
3297f4a2713aSLionel Sambuc 
3298f4a2713aSLionel Sambuc   const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3299f4a2713aSLionel Sambuc   if (!DRE)
3300f4a2713aSLionel Sambuc     return false;
3301f4a2713aSLionel Sambuc 
3302f4a2713aSLionel Sambuc   const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3303f4a2713aSLionel Sambuc   if (!Param)
3304f4a2713aSLionel Sambuc     return false;
3305f4a2713aSLionel Sambuc 
3306f4a2713aSLionel Sambuc   const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3307f4a2713aSLionel Sambuc   if (!M)
3308f4a2713aSLionel Sambuc     return false;
3309f4a2713aSLionel Sambuc 
3310f4a2713aSLionel Sambuc   return M->getSelfDecl() == Param;
3311f4a2713aSLionel Sambuc }
3312f4a2713aSLionel Sambuc 
getSourceBitField()3313f4a2713aSLionel Sambuc FieldDecl *Expr::getSourceBitField() {
3314f4a2713aSLionel Sambuc   Expr *E = this->IgnoreParens();
3315f4a2713aSLionel Sambuc 
3316f4a2713aSLionel Sambuc   while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
3317f4a2713aSLionel Sambuc     if (ICE->getCastKind() == CK_LValueToRValue ||
3318f4a2713aSLionel Sambuc         (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
3319f4a2713aSLionel Sambuc       E = ICE->getSubExpr()->IgnoreParens();
3320f4a2713aSLionel Sambuc     else
3321f4a2713aSLionel Sambuc       break;
3322f4a2713aSLionel Sambuc   }
3323f4a2713aSLionel Sambuc 
3324f4a2713aSLionel Sambuc   if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
3325f4a2713aSLionel Sambuc     if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
3326f4a2713aSLionel Sambuc       if (Field->isBitField())
3327f4a2713aSLionel Sambuc         return Field;
3328f4a2713aSLionel Sambuc 
3329f4a2713aSLionel Sambuc   if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3330f4a2713aSLionel Sambuc     if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3331f4a2713aSLionel Sambuc       if (Ivar->isBitField())
3332f4a2713aSLionel Sambuc         return Ivar;
3333f4a2713aSLionel Sambuc 
3334f4a2713aSLionel Sambuc   if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3335f4a2713aSLionel Sambuc     if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3336f4a2713aSLionel Sambuc       if (Field->isBitField())
3337f4a2713aSLionel Sambuc         return Field;
3338f4a2713aSLionel Sambuc 
3339f4a2713aSLionel Sambuc   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
3340f4a2713aSLionel Sambuc     if (BinOp->isAssignmentOp() && BinOp->getLHS())
3341f4a2713aSLionel Sambuc       return BinOp->getLHS()->getSourceBitField();
3342f4a2713aSLionel Sambuc 
3343f4a2713aSLionel Sambuc     if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
3344f4a2713aSLionel Sambuc       return BinOp->getRHS()->getSourceBitField();
3345f4a2713aSLionel Sambuc   }
3346f4a2713aSLionel Sambuc 
3347*0a6a1f1dSLionel Sambuc   if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E))
3348*0a6a1f1dSLionel Sambuc     if (UnOp->isPrefix() && UnOp->isIncrementDecrementOp())
3349*0a6a1f1dSLionel Sambuc       return UnOp->getSubExpr()->getSourceBitField();
3350*0a6a1f1dSLionel Sambuc 
3351*0a6a1f1dSLionel Sambuc   return nullptr;
3352f4a2713aSLionel Sambuc }
3353f4a2713aSLionel Sambuc 
refersToVectorElement() const3354f4a2713aSLionel Sambuc bool Expr::refersToVectorElement() const {
3355f4a2713aSLionel Sambuc   const Expr *E = this->IgnoreParens();
3356f4a2713aSLionel Sambuc 
3357f4a2713aSLionel Sambuc   while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
3358f4a2713aSLionel Sambuc     if (ICE->getValueKind() != VK_RValue &&
3359f4a2713aSLionel Sambuc         ICE->getCastKind() == CK_NoOp)
3360f4a2713aSLionel Sambuc       E = ICE->getSubExpr()->IgnoreParens();
3361f4a2713aSLionel Sambuc     else
3362f4a2713aSLionel Sambuc       break;
3363f4a2713aSLionel Sambuc   }
3364f4a2713aSLionel Sambuc 
3365f4a2713aSLionel Sambuc   if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3366f4a2713aSLionel Sambuc     return ASE->getBase()->getType()->isVectorType();
3367f4a2713aSLionel Sambuc 
3368f4a2713aSLionel Sambuc   if (isa<ExtVectorElementExpr>(E))
3369f4a2713aSLionel Sambuc     return true;
3370f4a2713aSLionel Sambuc 
3371f4a2713aSLionel Sambuc   return false;
3372f4a2713aSLionel Sambuc }
3373f4a2713aSLionel Sambuc 
3374f4a2713aSLionel Sambuc /// isArrow - Return true if the base expression is a pointer to vector,
3375f4a2713aSLionel Sambuc /// return false if the base expression is a vector.
isArrow() const3376f4a2713aSLionel Sambuc bool ExtVectorElementExpr::isArrow() const {
3377f4a2713aSLionel Sambuc   return getBase()->getType()->isPointerType();
3378f4a2713aSLionel Sambuc }
3379f4a2713aSLionel Sambuc 
getNumElements() const3380f4a2713aSLionel Sambuc unsigned ExtVectorElementExpr::getNumElements() const {
3381f4a2713aSLionel Sambuc   if (const VectorType *VT = getType()->getAs<VectorType>())
3382f4a2713aSLionel Sambuc     return VT->getNumElements();
3383f4a2713aSLionel Sambuc   return 1;
3384f4a2713aSLionel Sambuc }
3385f4a2713aSLionel Sambuc 
3386f4a2713aSLionel Sambuc /// containsDuplicateElements - Return true if any element access is repeated.
containsDuplicateElements() const3387f4a2713aSLionel Sambuc bool ExtVectorElementExpr::containsDuplicateElements() const {
3388f4a2713aSLionel Sambuc   // FIXME: Refactor this code to an accessor on the AST node which returns the
3389f4a2713aSLionel Sambuc   // "type" of component access, and share with code below and in Sema.
3390f4a2713aSLionel Sambuc   StringRef Comp = Accessor->getName();
3391f4a2713aSLionel Sambuc 
3392f4a2713aSLionel Sambuc   // Halving swizzles do not contain duplicate elements.
3393f4a2713aSLionel Sambuc   if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
3394f4a2713aSLionel Sambuc     return false;
3395f4a2713aSLionel Sambuc 
3396f4a2713aSLionel Sambuc   // Advance past s-char prefix on hex swizzles.
3397f4a2713aSLionel Sambuc   if (Comp[0] == 's' || Comp[0] == 'S')
3398f4a2713aSLionel Sambuc     Comp = Comp.substr(1);
3399f4a2713aSLionel Sambuc 
3400f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Comp.size(); i != e; ++i)
3401f4a2713aSLionel Sambuc     if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
3402f4a2713aSLionel Sambuc         return true;
3403f4a2713aSLionel Sambuc 
3404f4a2713aSLionel Sambuc   return false;
3405f4a2713aSLionel Sambuc }
3406f4a2713aSLionel Sambuc 
3407f4a2713aSLionel Sambuc /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
getEncodedElementAccess(SmallVectorImpl<unsigned> & Elts) const3408f4a2713aSLionel Sambuc void ExtVectorElementExpr::getEncodedElementAccess(
3409f4a2713aSLionel Sambuc                                   SmallVectorImpl<unsigned> &Elts) const {
3410f4a2713aSLionel Sambuc   StringRef Comp = Accessor->getName();
3411f4a2713aSLionel Sambuc   if (Comp[0] == 's' || Comp[0] == 'S')
3412f4a2713aSLionel Sambuc     Comp = Comp.substr(1);
3413f4a2713aSLionel Sambuc 
3414f4a2713aSLionel Sambuc   bool isHi =   Comp == "hi";
3415f4a2713aSLionel Sambuc   bool isLo =   Comp == "lo";
3416f4a2713aSLionel Sambuc   bool isEven = Comp == "even";
3417f4a2713aSLionel Sambuc   bool isOdd  = Comp == "odd";
3418f4a2713aSLionel Sambuc 
3419f4a2713aSLionel Sambuc   for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3420f4a2713aSLionel Sambuc     uint64_t Index;
3421f4a2713aSLionel Sambuc 
3422f4a2713aSLionel Sambuc     if (isHi)
3423f4a2713aSLionel Sambuc       Index = e + i;
3424f4a2713aSLionel Sambuc     else if (isLo)
3425f4a2713aSLionel Sambuc       Index = i;
3426f4a2713aSLionel Sambuc     else if (isEven)
3427f4a2713aSLionel Sambuc       Index = 2 * i;
3428f4a2713aSLionel Sambuc     else if (isOdd)
3429f4a2713aSLionel Sambuc       Index = 2 * i + 1;
3430f4a2713aSLionel Sambuc     else
3431f4a2713aSLionel Sambuc       Index = ExtVectorType::getAccessorIdx(Comp[i]);
3432f4a2713aSLionel Sambuc 
3433f4a2713aSLionel Sambuc     Elts.push_back(Index);
3434f4a2713aSLionel Sambuc   }
3435f4a2713aSLionel Sambuc }
3436f4a2713aSLionel Sambuc 
ObjCMessageExpr(QualType T,ExprValueKind VK,SourceLocation LBracLoc,SourceLocation SuperLoc,bool IsInstanceSuper,QualType SuperType,Selector Sel,ArrayRef<SourceLocation> SelLocs,SelectorLocationsKind SelLocsK,ObjCMethodDecl * Method,ArrayRef<Expr * > Args,SourceLocation RBracLoc,bool isImplicit)3437f4a2713aSLionel Sambuc ObjCMessageExpr::ObjCMessageExpr(QualType T,
3438f4a2713aSLionel Sambuc                                  ExprValueKind VK,
3439f4a2713aSLionel Sambuc                                  SourceLocation LBracLoc,
3440f4a2713aSLionel Sambuc                                  SourceLocation SuperLoc,
3441f4a2713aSLionel Sambuc                                  bool IsInstanceSuper,
3442f4a2713aSLionel Sambuc                                  QualType SuperType,
3443f4a2713aSLionel Sambuc                                  Selector Sel,
3444f4a2713aSLionel Sambuc                                  ArrayRef<SourceLocation> SelLocs,
3445f4a2713aSLionel Sambuc                                  SelectorLocationsKind SelLocsK,
3446f4a2713aSLionel Sambuc                                  ObjCMethodDecl *Method,
3447f4a2713aSLionel Sambuc                                  ArrayRef<Expr *> Args,
3448f4a2713aSLionel Sambuc                                  SourceLocation RBracLoc,
3449f4a2713aSLionel Sambuc                                  bool isImplicit)
3450f4a2713aSLionel Sambuc   : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
3451f4a2713aSLionel Sambuc          /*TypeDependent=*/false, /*ValueDependent=*/false,
3452f4a2713aSLionel Sambuc          /*InstantiationDependent=*/false,
3453f4a2713aSLionel Sambuc          /*ContainsUnexpandedParameterPack=*/false),
3454f4a2713aSLionel Sambuc     SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3455f4a2713aSLionel Sambuc                                                        : Sel.getAsOpaquePtr())),
3456f4a2713aSLionel Sambuc     Kind(IsInstanceSuper? SuperInstance : SuperClass),
3457*0a6a1f1dSLionel Sambuc     HasMethod(Method != nullptr), IsDelegateInitCall(false),
3458*0a6a1f1dSLionel Sambuc     IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
3459*0a6a1f1dSLionel Sambuc     RBracLoc(RBracLoc)
3460f4a2713aSLionel Sambuc {
3461f4a2713aSLionel Sambuc   initArgsAndSelLocs(Args, SelLocs, SelLocsK);
3462f4a2713aSLionel Sambuc   setReceiverPointer(SuperType.getAsOpaquePtr());
3463f4a2713aSLionel Sambuc }
3464f4a2713aSLionel Sambuc 
ObjCMessageExpr(QualType T,ExprValueKind VK,SourceLocation LBracLoc,TypeSourceInfo * Receiver,Selector Sel,ArrayRef<SourceLocation> SelLocs,SelectorLocationsKind SelLocsK,ObjCMethodDecl * Method,ArrayRef<Expr * > Args,SourceLocation RBracLoc,bool isImplicit)3465f4a2713aSLionel Sambuc ObjCMessageExpr::ObjCMessageExpr(QualType T,
3466f4a2713aSLionel Sambuc                                  ExprValueKind VK,
3467f4a2713aSLionel Sambuc                                  SourceLocation LBracLoc,
3468f4a2713aSLionel Sambuc                                  TypeSourceInfo *Receiver,
3469f4a2713aSLionel Sambuc                                  Selector Sel,
3470f4a2713aSLionel Sambuc                                  ArrayRef<SourceLocation> SelLocs,
3471f4a2713aSLionel Sambuc                                  SelectorLocationsKind SelLocsK,
3472f4a2713aSLionel Sambuc                                  ObjCMethodDecl *Method,
3473f4a2713aSLionel Sambuc                                  ArrayRef<Expr *> Args,
3474f4a2713aSLionel Sambuc                                  SourceLocation RBracLoc,
3475f4a2713aSLionel Sambuc                                  bool isImplicit)
3476f4a2713aSLionel Sambuc   : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
3477f4a2713aSLionel Sambuc          T->isDependentType(), T->isInstantiationDependentType(),
3478f4a2713aSLionel Sambuc          T->containsUnexpandedParameterPack()),
3479f4a2713aSLionel Sambuc     SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3480f4a2713aSLionel Sambuc                                                        : Sel.getAsOpaquePtr())),
3481f4a2713aSLionel Sambuc     Kind(Class),
3482*0a6a1f1dSLionel Sambuc     HasMethod(Method != nullptr), IsDelegateInitCall(false),
3483*0a6a1f1dSLionel Sambuc     IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
3484f4a2713aSLionel Sambuc {
3485f4a2713aSLionel Sambuc   initArgsAndSelLocs(Args, SelLocs, SelLocsK);
3486f4a2713aSLionel Sambuc   setReceiverPointer(Receiver);
3487f4a2713aSLionel Sambuc }
3488f4a2713aSLionel Sambuc 
ObjCMessageExpr(QualType T,ExprValueKind VK,SourceLocation LBracLoc,Expr * Receiver,Selector Sel,ArrayRef<SourceLocation> SelLocs,SelectorLocationsKind SelLocsK,ObjCMethodDecl * Method,ArrayRef<Expr * > Args,SourceLocation RBracLoc,bool isImplicit)3489f4a2713aSLionel Sambuc ObjCMessageExpr::ObjCMessageExpr(QualType T,
3490f4a2713aSLionel Sambuc                                  ExprValueKind VK,
3491f4a2713aSLionel Sambuc                                  SourceLocation LBracLoc,
3492f4a2713aSLionel Sambuc                                  Expr *Receiver,
3493f4a2713aSLionel Sambuc                                  Selector Sel,
3494f4a2713aSLionel Sambuc                                  ArrayRef<SourceLocation> SelLocs,
3495f4a2713aSLionel Sambuc                                  SelectorLocationsKind SelLocsK,
3496f4a2713aSLionel Sambuc                                  ObjCMethodDecl *Method,
3497f4a2713aSLionel Sambuc                                  ArrayRef<Expr *> Args,
3498f4a2713aSLionel Sambuc                                  SourceLocation RBracLoc,
3499f4a2713aSLionel Sambuc                                  bool isImplicit)
3500f4a2713aSLionel Sambuc   : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
3501f4a2713aSLionel Sambuc          Receiver->isTypeDependent(),
3502f4a2713aSLionel Sambuc          Receiver->isInstantiationDependent(),
3503f4a2713aSLionel Sambuc          Receiver->containsUnexpandedParameterPack()),
3504f4a2713aSLionel Sambuc     SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3505f4a2713aSLionel Sambuc                                                        : Sel.getAsOpaquePtr())),
3506f4a2713aSLionel Sambuc     Kind(Instance),
3507*0a6a1f1dSLionel Sambuc     HasMethod(Method != nullptr), IsDelegateInitCall(false),
3508*0a6a1f1dSLionel Sambuc     IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
3509f4a2713aSLionel Sambuc {
3510f4a2713aSLionel Sambuc   initArgsAndSelLocs(Args, SelLocs, SelLocsK);
3511f4a2713aSLionel Sambuc   setReceiverPointer(Receiver);
3512f4a2713aSLionel Sambuc }
3513f4a2713aSLionel Sambuc 
initArgsAndSelLocs(ArrayRef<Expr * > Args,ArrayRef<SourceLocation> SelLocs,SelectorLocationsKind SelLocsK)3514f4a2713aSLionel Sambuc void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3515f4a2713aSLionel Sambuc                                          ArrayRef<SourceLocation> SelLocs,
3516f4a2713aSLionel Sambuc                                          SelectorLocationsKind SelLocsK) {
3517f4a2713aSLionel Sambuc   setNumArgs(Args.size());
3518f4a2713aSLionel Sambuc   Expr **MyArgs = getArgs();
3519f4a2713aSLionel Sambuc   for (unsigned I = 0; I != Args.size(); ++I) {
3520f4a2713aSLionel Sambuc     if (Args[I]->isTypeDependent())
3521f4a2713aSLionel Sambuc       ExprBits.TypeDependent = true;
3522f4a2713aSLionel Sambuc     if (Args[I]->isValueDependent())
3523f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
3524f4a2713aSLionel Sambuc     if (Args[I]->isInstantiationDependent())
3525f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
3526f4a2713aSLionel Sambuc     if (Args[I]->containsUnexpandedParameterPack())
3527f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
3528f4a2713aSLionel Sambuc 
3529f4a2713aSLionel Sambuc     MyArgs[I] = Args[I];
3530f4a2713aSLionel Sambuc   }
3531f4a2713aSLionel Sambuc 
3532f4a2713aSLionel Sambuc   SelLocsKind = SelLocsK;
3533f4a2713aSLionel Sambuc   if (!isImplicit()) {
3534f4a2713aSLionel Sambuc     if (SelLocsK == SelLoc_NonStandard)
3535f4a2713aSLionel Sambuc       std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3536f4a2713aSLionel Sambuc   }
3537f4a2713aSLionel Sambuc }
3538f4a2713aSLionel Sambuc 
Create(const ASTContext & Context,QualType T,ExprValueKind VK,SourceLocation LBracLoc,SourceLocation SuperLoc,bool IsInstanceSuper,QualType SuperType,Selector Sel,ArrayRef<SourceLocation> SelLocs,ObjCMethodDecl * Method,ArrayRef<Expr * > Args,SourceLocation RBracLoc,bool isImplicit)3539f4a2713aSLionel Sambuc ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
3540f4a2713aSLionel Sambuc                                          ExprValueKind VK,
3541f4a2713aSLionel Sambuc                                          SourceLocation LBracLoc,
3542f4a2713aSLionel Sambuc                                          SourceLocation SuperLoc,
3543f4a2713aSLionel Sambuc                                          bool IsInstanceSuper,
3544f4a2713aSLionel Sambuc                                          QualType SuperType,
3545f4a2713aSLionel Sambuc                                          Selector Sel,
3546f4a2713aSLionel Sambuc                                          ArrayRef<SourceLocation> SelLocs,
3547f4a2713aSLionel Sambuc                                          ObjCMethodDecl *Method,
3548f4a2713aSLionel Sambuc                                          ArrayRef<Expr *> Args,
3549f4a2713aSLionel Sambuc                                          SourceLocation RBracLoc,
3550f4a2713aSLionel Sambuc                                          bool isImplicit) {
3551f4a2713aSLionel Sambuc   assert((!SelLocs.empty() || isImplicit) &&
3552f4a2713aSLionel Sambuc          "No selector locs for non-implicit message");
3553f4a2713aSLionel Sambuc   ObjCMessageExpr *Mem;
3554f4a2713aSLionel Sambuc   SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3555f4a2713aSLionel Sambuc   if (isImplicit)
3556f4a2713aSLionel Sambuc     Mem = alloc(Context, Args.size(), 0);
3557f4a2713aSLionel Sambuc   else
3558f4a2713aSLionel Sambuc     Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
3559f4a2713aSLionel Sambuc   return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
3560f4a2713aSLionel Sambuc                                    SuperType, Sel, SelLocs, SelLocsK,
3561f4a2713aSLionel Sambuc                                    Method, Args, RBracLoc, isImplicit);
3562f4a2713aSLionel Sambuc }
3563f4a2713aSLionel Sambuc 
Create(const ASTContext & Context,QualType T,ExprValueKind VK,SourceLocation LBracLoc,TypeSourceInfo * Receiver,Selector Sel,ArrayRef<SourceLocation> SelLocs,ObjCMethodDecl * Method,ArrayRef<Expr * > Args,SourceLocation RBracLoc,bool isImplicit)3564f4a2713aSLionel Sambuc ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
3565f4a2713aSLionel Sambuc                                          ExprValueKind VK,
3566f4a2713aSLionel Sambuc                                          SourceLocation LBracLoc,
3567f4a2713aSLionel Sambuc                                          TypeSourceInfo *Receiver,
3568f4a2713aSLionel Sambuc                                          Selector Sel,
3569f4a2713aSLionel Sambuc                                          ArrayRef<SourceLocation> SelLocs,
3570f4a2713aSLionel Sambuc                                          ObjCMethodDecl *Method,
3571f4a2713aSLionel Sambuc                                          ArrayRef<Expr *> Args,
3572f4a2713aSLionel Sambuc                                          SourceLocation RBracLoc,
3573f4a2713aSLionel Sambuc                                          bool isImplicit) {
3574f4a2713aSLionel Sambuc   assert((!SelLocs.empty() || isImplicit) &&
3575f4a2713aSLionel Sambuc          "No selector locs for non-implicit message");
3576f4a2713aSLionel Sambuc   ObjCMessageExpr *Mem;
3577f4a2713aSLionel Sambuc   SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3578f4a2713aSLionel Sambuc   if (isImplicit)
3579f4a2713aSLionel Sambuc     Mem = alloc(Context, Args.size(), 0);
3580f4a2713aSLionel Sambuc   else
3581f4a2713aSLionel Sambuc     Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
3582f4a2713aSLionel Sambuc   return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
3583f4a2713aSLionel Sambuc                                    SelLocs, SelLocsK, Method, Args, RBracLoc,
3584f4a2713aSLionel Sambuc                                    isImplicit);
3585f4a2713aSLionel Sambuc }
3586f4a2713aSLionel Sambuc 
Create(const ASTContext & Context,QualType T,ExprValueKind VK,SourceLocation LBracLoc,Expr * Receiver,Selector Sel,ArrayRef<SourceLocation> SelLocs,ObjCMethodDecl * Method,ArrayRef<Expr * > Args,SourceLocation RBracLoc,bool isImplicit)3587f4a2713aSLionel Sambuc ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
3588f4a2713aSLionel Sambuc                                          ExprValueKind VK,
3589f4a2713aSLionel Sambuc                                          SourceLocation LBracLoc,
3590f4a2713aSLionel Sambuc                                          Expr *Receiver,
3591f4a2713aSLionel Sambuc                                          Selector Sel,
3592f4a2713aSLionel Sambuc                                          ArrayRef<SourceLocation> SelLocs,
3593f4a2713aSLionel Sambuc                                          ObjCMethodDecl *Method,
3594f4a2713aSLionel Sambuc                                          ArrayRef<Expr *> Args,
3595f4a2713aSLionel Sambuc                                          SourceLocation RBracLoc,
3596f4a2713aSLionel Sambuc                                          bool isImplicit) {
3597f4a2713aSLionel Sambuc   assert((!SelLocs.empty() || isImplicit) &&
3598f4a2713aSLionel Sambuc          "No selector locs for non-implicit message");
3599f4a2713aSLionel Sambuc   ObjCMessageExpr *Mem;
3600f4a2713aSLionel Sambuc   SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3601f4a2713aSLionel Sambuc   if (isImplicit)
3602f4a2713aSLionel Sambuc     Mem = alloc(Context, Args.size(), 0);
3603f4a2713aSLionel Sambuc   else
3604f4a2713aSLionel Sambuc     Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
3605f4a2713aSLionel Sambuc   return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
3606f4a2713aSLionel Sambuc                                    SelLocs, SelLocsK, Method, Args, RBracLoc,
3607f4a2713aSLionel Sambuc                                    isImplicit);
3608f4a2713aSLionel Sambuc }
3609f4a2713aSLionel Sambuc 
CreateEmpty(const ASTContext & Context,unsigned NumArgs,unsigned NumStoredSelLocs)3610f4a2713aSLionel Sambuc ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
3611f4a2713aSLionel Sambuc                                               unsigned NumArgs,
3612f4a2713aSLionel Sambuc                                               unsigned NumStoredSelLocs) {
3613f4a2713aSLionel Sambuc   ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
3614f4a2713aSLionel Sambuc   return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3615f4a2713aSLionel Sambuc }
3616f4a2713aSLionel Sambuc 
alloc(const ASTContext & C,ArrayRef<Expr * > Args,SourceLocation RBraceLoc,ArrayRef<SourceLocation> SelLocs,Selector Sel,SelectorLocationsKind & SelLocsK)3617f4a2713aSLionel Sambuc ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
3618f4a2713aSLionel Sambuc                                         ArrayRef<Expr *> Args,
3619f4a2713aSLionel Sambuc                                         SourceLocation RBraceLoc,
3620f4a2713aSLionel Sambuc                                         ArrayRef<SourceLocation> SelLocs,
3621f4a2713aSLionel Sambuc                                         Selector Sel,
3622f4a2713aSLionel Sambuc                                         SelectorLocationsKind &SelLocsK) {
3623f4a2713aSLionel Sambuc   SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3624f4a2713aSLionel Sambuc   unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3625f4a2713aSLionel Sambuc                                                                : 0;
3626f4a2713aSLionel Sambuc   return alloc(C, Args.size(), NumStoredSelLocs);
3627f4a2713aSLionel Sambuc }
3628f4a2713aSLionel Sambuc 
alloc(const ASTContext & C,unsigned NumArgs,unsigned NumStoredSelLocs)3629f4a2713aSLionel Sambuc ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
3630f4a2713aSLionel Sambuc                                         unsigned NumArgs,
3631f4a2713aSLionel Sambuc                                         unsigned NumStoredSelLocs) {
3632f4a2713aSLionel Sambuc   unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3633f4a2713aSLionel Sambuc     NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3634f4a2713aSLionel Sambuc   return (ObjCMessageExpr *)C.Allocate(Size,
3635f4a2713aSLionel Sambuc                                      llvm::AlignOf<ObjCMessageExpr>::Alignment);
3636f4a2713aSLionel Sambuc }
3637f4a2713aSLionel Sambuc 
getSelectorLocs(SmallVectorImpl<SourceLocation> & SelLocs) const3638f4a2713aSLionel Sambuc void ObjCMessageExpr::getSelectorLocs(
3639f4a2713aSLionel Sambuc                                SmallVectorImpl<SourceLocation> &SelLocs) const {
3640f4a2713aSLionel Sambuc   for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3641f4a2713aSLionel Sambuc     SelLocs.push_back(getSelectorLoc(i));
3642f4a2713aSLionel Sambuc }
3643f4a2713aSLionel Sambuc 
getReceiverRange() const3644f4a2713aSLionel Sambuc SourceRange ObjCMessageExpr::getReceiverRange() const {
3645f4a2713aSLionel Sambuc   switch (getReceiverKind()) {
3646f4a2713aSLionel Sambuc   case Instance:
3647f4a2713aSLionel Sambuc     return getInstanceReceiver()->getSourceRange();
3648f4a2713aSLionel Sambuc 
3649f4a2713aSLionel Sambuc   case Class:
3650f4a2713aSLionel Sambuc     return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3651f4a2713aSLionel Sambuc 
3652f4a2713aSLionel Sambuc   case SuperInstance:
3653f4a2713aSLionel Sambuc   case SuperClass:
3654f4a2713aSLionel Sambuc     return getSuperLoc();
3655f4a2713aSLionel Sambuc   }
3656f4a2713aSLionel Sambuc 
3657f4a2713aSLionel Sambuc   llvm_unreachable("Invalid ReceiverKind!");
3658f4a2713aSLionel Sambuc }
3659f4a2713aSLionel Sambuc 
getSelector() const3660f4a2713aSLionel Sambuc Selector ObjCMessageExpr::getSelector() const {
3661f4a2713aSLionel Sambuc   if (HasMethod)
3662f4a2713aSLionel Sambuc     return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3663f4a2713aSLionel Sambuc                                                                ->getSelector();
3664f4a2713aSLionel Sambuc   return Selector(SelectorOrMethod);
3665f4a2713aSLionel Sambuc }
3666f4a2713aSLionel Sambuc 
getReceiverType() const3667f4a2713aSLionel Sambuc QualType ObjCMessageExpr::getReceiverType() const {
3668f4a2713aSLionel Sambuc   switch (getReceiverKind()) {
3669f4a2713aSLionel Sambuc   case Instance:
3670f4a2713aSLionel Sambuc     return getInstanceReceiver()->getType();
3671f4a2713aSLionel Sambuc   case Class:
3672f4a2713aSLionel Sambuc     return getClassReceiver();
3673f4a2713aSLionel Sambuc   case SuperInstance:
3674f4a2713aSLionel Sambuc   case SuperClass:
3675f4a2713aSLionel Sambuc     return getSuperType();
3676f4a2713aSLionel Sambuc   }
3677f4a2713aSLionel Sambuc 
3678f4a2713aSLionel Sambuc   llvm_unreachable("unexpected receiver kind");
3679f4a2713aSLionel Sambuc }
3680f4a2713aSLionel Sambuc 
getReceiverInterface() const3681f4a2713aSLionel Sambuc ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3682f4a2713aSLionel Sambuc   QualType T = getReceiverType();
3683f4a2713aSLionel Sambuc 
3684f4a2713aSLionel Sambuc   if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3685f4a2713aSLionel Sambuc     return Ptr->getInterfaceDecl();
3686f4a2713aSLionel Sambuc 
3687f4a2713aSLionel Sambuc   if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3688f4a2713aSLionel Sambuc     return Ty->getInterface();
3689f4a2713aSLionel Sambuc 
3690*0a6a1f1dSLionel Sambuc   return nullptr;
3691f4a2713aSLionel Sambuc }
3692f4a2713aSLionel Sambuc 
getBridgeKindName() const3693f4a2713aSLionel Sambuc StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
3694f4a2713aSLionel Sambuc   switch (getBridgeKind()) {
3695f4a2713aSLionel Sambuc   case OBC_Bridge:
3696f4a2713aSLionel Sambuc     return "__bridge";
3697f4a2713aSLionel Sambuc   case OBC_BridgeTransfer:
3698f4a2713aSLionel Sambuc     return "__bridge_transfer";
3699f4a2713aSLionel Sambuc   case OBC_BridgeRetained:
3700f4a2713aSLionel Sambuc     return "__bridge_retained";
3701f4a2713aSLionel Sambuc   }
3702f4a2713aSLionel Sambuc 
3703f4a2713aSLionel Sambuc   llvm_unreachable("Invalid BridgeKind!");
3704f4a2713aSLionel Sambuc }
3705f4a2713aSLionel Sambuc 
ShuffleVectorExpr(const ASTContext & C,ArrayRef<Expr * > args,QualType Type,SourceLocation BLoc,SourceLocation RP)3706f4a2713aSLionel Sambuc ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
3707f4a2713aSLionel Sambuc                                      QualType Type, SourceLocation BLoc,
3708f4a2713aSLionel Sambuc                                      SourceLocation RP)
3709f4a2713aSLionel Sambuc    : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3710f4a2713aSLionel Sambuc           Type->isDependentType(), Type->isDependentType(),
3711f4a2713aSLionel Sambuc           Type->isInstantiationDependentType(),
3712f4a2713aSLionel Sambuc           Type->containsUnexpandedParameterPack()),
3713f4a2713aSLionel Sambuc      BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
3714f4a2713aSLionel Sambuc {
3715f4a2713aSLionel Sambuc   SubExprs = new (C) Stmt*[args.size()];
3716f4a2713aSLionel Sambuc   for (unsigned i = 0; i != args.size(); i++) {
3717f4a2713aSLionel Sambuc     if (args[i]->isTypeDependent())
3718f4a2713aSLionel Sambuc       ExprBits.TypeDependent = true;
3719f4a2713aSLionel Sambuc     if (args[i]->isValueDependent())
3720f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
3721f4a2713aSLionel Sambuc     if (args[i]->isInstantiationDependent())
3722f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
3723f4a2713aSLionel Sambuc     if (args[i]->containsUnexpandedParameterPack())
3724f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
3725f4a2713aSLionel Sambuc 
3726f4a2713aSLionel Sambuc     SubExprs[i] = args[i];
3727f4a2713aSLionel Sambuc   }
3728f4a2713aSLionel Sambuc }
3729f4a2713aSLionel Sambuc 
setExprs(const ASTContext & C,ArrayRef<Expr * > Exprs)3730f4a2713aSLionel Sambuc void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
3731f4a2713aSLionel Sambuc   if (SubExprs) C.Deallocate(SubExprs);
3732f4a2713aSLionel Sambuc 
3733f4a2713aSLionel Sambuc   this->NumExprs = Exprs.size();
3734f4a2713aSLionel Sambuc   SubExprs = new (C) Stmt*[NumExprs];
3735f4a2713aSLionel Sambuc   memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
3736f4a2713aSLionel Sambuc }
3737f4a2713aSLionel Sambuc 
GenericSelectionExpr(const ASTContext & Context,SourceLocation GenericLoc,Expr * ControllingExpr,ArrayRef<TypeSourceInfo * > AssocTypes,ArrayRef<Expr * > AssocExprs,SourceLocation DefaultLoc,SourceLocation RParenLoc,bool ContainsUnexpandedParameterPack,unsigned ResultIndex)3738f4a2713aSLionel Sambuc GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
3739f4a2713aSLionel Sambuc                                SourceLocation GenericLoc, Expr *ControllingExpr,
3740f4a2713aSLionel Sambuc                                ArrayRef<TypeSourceInfo*> AssocTypes,
3741f4a2713aSLionel Sambuc                                ArrayRef<Expr*> AssocExprs,
3742f4a2713aSLionel Sambuc                                SourceLocation DefaultLoc,
3743f4a2713aSLionel Sambuc                                SourceLocation RParenLoc,
3744f4a2713aSLionel Sambuc                                bool ContainsUnexpandedParameterPack,
3745f4a2713aSLionel Sambuc                                unsigned ResultIndex)
3746f4a2713aSLionel Sambuc   : Expr(GenericSelectionExprClass,
3747f4a2713aSLionel Sambuc          AssocExprs[ResultIndex]->getType(),
3748f4a2713aSLionel Sambuc          AssocExprs[ResultIndex]->getValueKind(),
3749f4a2713aSLionel Sambuc          AssocExprs[ResultIndex]->getObjectKind(),
3750f4a2713aSLionel Sambuc          AssocExprs[ResultIndex]->isTypeDependent(),
3751f4a2713aSLionel Sambuc          AssocExprs[ResultIndex]->isValueDependent(),
3752f4a2713aSLionel Sambuc          AssocExprs[ResultIndex]->isInstantiationDependent(),
3753f4a2713aSLionel Sambuc          ContainsUnexpandedParameterPack),
3754f4a2713aSLionel Sambuc     AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3755f4a2713aSLionel Sambuc     SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3756f4a2713aSLionel Sambuc     NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3757f4a2713aSLionel Sambuc     GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
3758f4a2713aSLionel Sambuc   SubExprs[CONTROLLING] = ControllingExpr;
3759f4a2713aSLionel Sambuc   assert(AssocTypes.size() == AssocExprs.size());
3760f4a2713aSLionel Sambuc   std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3761f4a2713aSLionel Sambuc   std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
3762f4a2713aSLionel Sambuc }
3763f4a2713aSLionel Sambuc 
GenericSelectionExpr(const ASTContext & Context,SourceLocation GenericLoc,Expr * ControllingExpr,ArrayRef<TypeSourceInfo * > AssocTypes,ArrayRef<Expr * > AssocExprs,SourceLocation DefaultLoc,SourceLocation RParenLoc,bool ContainsUnexpandedParameterPack)3764f4a2713aSLionel Sambuc GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
3765f4a2713aSLionel Sambuc                                SourceLocation GenericLoc, Expr *ControllingExpr,
3766f4a2713aSLionel Sambuc                                ArrayRef<TypeSourceInfo*> AssocTypes,
3767f4a2713aSLionel Sambuc                                ArrayRef<Expr*> AssocExprs,
3768f4a2713aSLionel Sambuc                                SourceLocation DefaultLoc,
3769f4a2713aSLionel Sambuc                                SourceLocation RParenLoc,
3770f4a2713aSLionel Sambuc                                bool ContainsUnexpandedParameterPack)
3771f4a2713aSLionel Sambuc   : Expr(GenericSelectionExprClass,
3772f4a2713aSLionel Sambuc          Context.DependentTy,
3773f4a2713aSLionel Sambuc          VK_RValue,
3774f4a2713aSLionel Sambuc          OK_Ordinary,
3775f4a2713aSLionel Sambuc          /*isTypeDependent=*/true,
3776f4a2713aSLionel Sambuc          /*isValueDependent=*/true,
3777f4a2713aSLionel Sambuc          /*isInstantiationDependent=*/true,
3778f4a2713aSLionel Sambuc          ContainsUnexpandedParameterPack),
3779f4a2713aSLionel Sambuc     AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3780f4a2713aSLionel Sambuc     SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3781f4a2713aSLionel Sambuc     NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3782f4a2713aSLionel Sambuc     DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
3783f4a2713aSLionel Sambuc   SubExprs[CONTROLLING] = ControllingExpr;
3784f4a2713aSLionel Sambuc   assert(AssocTypes.size() == AssocExprs.size());
3785f4a2713aSLionel Sambuc   std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3786f4a2713aSLionel Sambuc   std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
3787f4a2713aSLionel Sambuc }
3788f4a2713aSLionel Sambuc 
3789f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3790f4a2713aSLionel Sambuc //  DesignatedInitExpr
3791f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3792f4a2713aSLionel Sambuc 
getFieldName() const3793f4a2713aSLionel Sambuc IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
3794f4a2713aSLionel Sambuc   assert(Kind == FieldDesignator && "Only valid on a field designator");
3795f4a2713aSLionel Sambuc   if (Field.NameOrField & 0x01)
3796f4a2713aSLionel Sambuc     return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3797f4a2713aSLionel Sambuc   else
3798f4a2713aSLionel Sambuc     return getField()->getIdentifier();
3799f4a2713aSLionel Sambuc }
3800f4a2713aSLionel Sambuc 
DesignatedInitExpr(const ASTContext & C,QualType Ty,unsigned NumDesignators,const Designator * Designators,SourceLocation EqualOrColonLoc,bool GNUSyntax,ArrayRef<Expr * > IndexExprs,Expr * Init)3801f4a2713aSLionel Sambuc DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
3802f4a2713aSLionel Sambuc                                        unsigned NumDesignators,
3803f4a2713aSLionel Sambuc                                        const Designator *Designators,
3804f4a2713aSLionel Sambuc                                        SourceLocation EqualOrColonLoc,
3805f4a2713aSLionel Sambuc                                        bool GNUSyntax,
3806f4a2713aSLionel Sambuc                                        ArrayRef<Expr*> IndexExprs,
3807f4a2713aSLionel Sambuc                                        Expr *Init)
3808f4a2713aSLionel Sambuc   : Expr(DesignatedInitExprClass, Ty,
3809f4a2713aSLionel Sambuc          Init->getValueKind(), Init->getObjectKind(),
3810f4a2713aSLionel Sambuc          Init->isTypeDependent(), Init->isValueDependent(),
3811f4a2713aSLionel Sambuc          Init->isInstantiationDependent(),
3812f4a2713aSLionel Sambuc          Init->containsUnexpandedParameterPack()),
3813f4a2713aSLionel Sambuc     EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3814f4a2713aSLionel Sambuc     NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
3815f4a2713aSLionel Sambuc   this->Designators = new (C) Designator[NumDesignators];
3816f4a2713aSLionel Sambuc 
3817f4a2713aSLionel Sambuc   // Record the initializer itself.
3818f4a2713aSLionel Sambuc   child_range Child = children();
3819f4a2713aSLionel Sambuc   *Child++ = Init;
3820f4a2713aSLionel Sambuc 
3821f4a2713aSLionel Sambuc   // Copy the designators and their subexpressions, computing
3822f4a2713aSLionel Sambuc   // value-dependence along the way.
3823f4a2713aSLionel Sambuc   unsigned IndexIdx = 0;
3824f4a2713aSLionel Sambuc   for (unsigned I = 0; I != NumDesignators; ++I) {
3825f4a2713aSLionel Sambuc     this->Designators[I] = Designators[I];
3826f4a2713aSLionel Sambuc 
3827f4a2713aSLionel Sambuc     if (this->Designators[I].isArrayDesignator()) {
3828f4a2713aSLionel Sambuc       // Compute type- and value-dependence.
3829f4a2713aSLionel Sambuc       Expr *Index = IndexExprs[IndexIdx];
3830f4a2713aSLionel Sambuc       if (Index->isTypeDependent() || Index->isValueDependent())
3831*0a6a1f1dSLionel Sambuc         ExprBits.TypeDependent = ExprBits.ValueDependent = true;
3832f4a2713aSLionel Sambuc       if (Index->isInstantiationDependent())
3833f4a2713aSLionel Sambuc         ExprBits.InstantiationDependent = true;
3834f4a2713aSLionel Sambuc       // Propagate unexpanded parameter packs.
3835f4a2713aSLionel Sambuc       if (Index->containsUnexpandedParameterPack())
3836f4a2713aSLionel Sambuc         ExprBits.ContainsUnexpandedParameterPack = true;
3837f4a2713aSLionel Sambuc 
3838f4a2713aSLionel Sambuc       // Copy the index expressions into permanent storage.
3839f4a2713aSLionel Sambuc       *Child++ = IndexExprs[IndexIdx++];
3840f4a2713aSLionel Sambuc     } else if (this->Designators[I].isArrayRangeDesignator()) {
3841f4a2713aSLionel Sambuc       // Compute type- and value-dependence.
3842f4a2713aSLionel Sambuc       Expr *Start = IndexExprs[IndexIdx];
3843f4a2713aSLionel Sambuc       Expr *End = IndexExprs[IndexIdx + 1];
3844f4a2713aSLionel Sambuc       if (Start->isTypeDependent() || Start->isValueDependent() ||
3845f4a2713aSLionel Sambuc           End->isTypeDependent() || End->isValueDependent()) {
3846*0a6a1f1dSLionel Sambuc         ExprBits.TypeDependent = ExprBits.ValueDependent = true;
3847f4a2713aSLionel Sambuc         ExprBits.InstantiationDependent = true;
3848f4a2713aSLionel Sambuc       } else if (Start->isInstantiationDependent() ||
3849f4a2713aSLionel Sambuc                  End->isInstantiationDependent()) {
3850f4a2713aSLionel Sambuc         ExprBits.InstantiationDependent = true;
3851f4a2713aSLionel Sambuc       }
3852f4a2713aSLionel Sambuc 
3853f4a2713aSLionel Sambuc       // Propagate unexpanded parameter packs.
3854f4a2713aSLionel Sambuc       if (Start->containsUnexpandedParameterPack() ||
3855f4a2713aSLionel Sambuc           End->containsUnexpandedParameterPack())
3856f4a2713aSLionel Sambuc         ExprBits.ContainsUnexpandedParameterPack = true;
3857f4a2713aSLionel Sambuc 
3858f4a2713aSLionel Sambuc       // Copy the start/end expressions into permanent storage.
3859f4a2713aSLionel Sambuc       *Child++ = IndexExprs[IndexIdx++];
3860f4a2713aSLionel Sambuc       *Child++ = IndexExprs[IndexIdx++];
3861f4a2713aSLionel Sambuc     }
3862f4a2713aSLionel Sambuc   }
3863f4a2713aSLionel Sambuc 
3864f4a2713aSLionel Sambuc   assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
3865f4a2713aSLionel Sambuc }
3866f4a2713aSLionel Sambuc 
3867f4a2713aSLionel Sambuc DesignatedInitExpr *
Create(const ASTContext & C,Designator * Designators,unsigned NumDesignators,ArrayRef<Expr * > IndexExprs,SourceLocation ColonOrEqualLoc,bool UsesColonSyntax,Expr * Init)3868f4a2713aSLionel Sambuc DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
3869f4a2713aSLionel Sambuc                            unsigned NumDesignators,
3870f4a2713aSLionel Sambuc                            ArrayRef<Expr*> IndexExprs,
3871f4a2713aSLionel Sambuc                            SourceLocation ColonOrEqualLoc,
3872f4a2713aSLionel Sambuc                            bool UsesColonSyntax, Expr *Init) {
3873f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3874f4a2713aSLionel Sambuc                          sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
3875f4a2713aSLionel Sambuc   return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
3876f4a2713aSLionel Sambuc                                       ColonOrEqualLoc, UsesColonSyntax,
3877f4a2713aSLionel Sambuc                                       IndexExprs, Init);
3878f4a2713aSLionel Sambuc }
3879f4a2713aSLionel Sambuc 
CreateEmpty(const ASTContext & C,unsigned NumIndexExprs)3880f4a2713aSLionel Sambuc DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
3881f4a2713aSLionel Sambuc                                                     unsigned NumIndexExprs) {
3882f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3883f4a2713aSLionel Sambuc                          sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3884f4a2713aSLionel Sambuc   return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3885f4a2713aSLionel Sambuc }
3886f4a2713aSLionel Sambuc 
setDesignators(const ASTContext & C,const Designator * Desigs,unsigned NumDesigs)3887f4a2713aSLionel Sambuc void DesignatedInitExpr::setDesignators(const ASTContext &C,
3888f4a2713aSLionel Sambuc                                         const Designator *Desigs,
3889f4a2713aSLionel Sambuc                                         unsigned NumDesigs) {
3890f4a2713aSLionel Sambuc   Designators = new (C) Designator[NumDesigs];
3891f4a2713aSLionel Sambuc   NumDesignators = NumDesigs;
3892f4a2713aSLionel Sambuc   for (unsigned I = 0; I != NumDesigs; ++I)
3893f4a2713aSLionel Sambuc     Designators[I] = Desigs[I];
3894f4a2713aSLionel Sambuc }
3895f4a2713aSLionel Sambuc 
getDesignatorsSourceRange() const3896f4a2713aSLionel Sambuc SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3897f4a2713aSLionel Sambuc   DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3898f4a2713aSLionel Sambuc   if (size() == 1)
3899f4a2713aSLionel Sambuc     return DIE->getDesignator(0)->getSourceRange();
3900f4a2713aSLionel Sambuc   return SourceRange(DIE->getDesignator(0)->getLocStart(),
3901f4a2713aSLionel Sambuc                      DIE->getDesignator(size()-1)->getLocEnd());
3902f4a2713aSLionel Sambuc }
3903f4a2713aSLionel Sambuc 
getLocStart() const3904f4a2713aSLionel Sambuc SourceLocation DesignatedInitExpr::getLocStart() const {
3905f4a2713aSLionel Sambuc   SourceLocation StartLoc;
3906f4a2713aSLionel Sambuc   Designator &First =
3907f4a2713aSLionel Sambuc     *const_cast<DesignatedInitExpr*>(this)->designators_begin();
3908f4a2713aSLionel Sambuc   if (First.isFieldDesignator()) {
3909f4a2713aSLionel Sambuc     if (GNUSyntax)
3910f4a2713aSLionel Sambuc       StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3911f4a2713aSLionel Sambuc     else
3912f4a2713aSLionel Sambuc       StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3913f4a2713aSLionel Sambuc   } else
3914f4a2713aSLionel Sambuc     StartLoc =
3915f4a2713aSLionel Sambuc       SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
3916f4a2713aSLionel Sambuc   return StartLoc;
3917f4a2713aSLionel Sambuc }
3918f4a2713aSLionel Sambuc 
getLocEnd() const3919f4a2713aSLionel Sambuc SourceLocation DesignatedInitExpr::getLocEnd() const {
3920f4a2713aSLionel Sambuc   return getInit()->getLocEnd();
3921f4a2713aSLionel Sambuc }
3922f4a2713aSLionel Sambuc 
getArrayIndex(const Designator & D) const3923f4a2713aSLionel Sambuc Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
3924f4a2713aSLionel Sambuc   assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3925*0a6a1f1dSLionel Sambuc   Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
3926f4a2713aSLionel Sambuc   return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3927f4a2713aSLionel Sambuc }
3928f4a2713aSLionel Sambuc 
getArrayRangeStart(const Designator & D) const3929f4a2713aSLionel Sambuc Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
3930f4a2713aSLionel Sambuc   assert(D.Kind == Designator::ArrayRangeDesignator &&
3931f4a2713aSLionel Sambuc          "Requires array range designator");
3932*0a6a1f1dSLionel Sambuc   Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
3933f4a2713aSLionel Sambuc   return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3934f4a2713aSLionel Sambuc }
3935f4a2713aSLionel Sambuc 
getArrayRangeEnd(const Designator & D) const3936f4a2713aSLionel Sambuc Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
3937f4a2713aSLionel Sambuc   assert(D.Kind == Designator::ArrayRangeDesignator &&
3938f4a2713aSLionel Sambuc          "Requires array range designator");
3939*0a6a1f1dSLionel Sambuc   Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
3940f4a2713aSLionel Sambuc   return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3941f4a2713aSLionel Sambuc }
3942f4a2713aSLionel Sambuc 
3943f4a2713aSLionel Sambuc /// \brief Replaces the designator at index @p Idx with the series
3944f4a2713aSLionel Sambuc /// of designators in [First, Last).
ExpandDesignator(const ASTContext & C,unsigned Idx,const Designator * First,const Designator * Last)3945f4a2713aSLionel Sambuc void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
3946f4a2713aSLionel Sambuc                                           const Designator *First,
3947f4a2713aSLionel Sambuc                                           const Designator *Last) {
3948f4a2713aSLionel Sambuc   unsigned NumNewDesignators = Last - First;
3949f4a2713aSLionel Sambuc   if (NumNewDesignators == 0) {
3950f4a2713aSLionel Sambuc     std::copy_backward(Designators + Idx + 1,
3951f4a2713aSLionel Sambuc                        Designators + NumDesignators,
3952f4a2713aSLionel Sambuc                        Designators + Idx);
3953f4a2713aSLionel Sambuc     --NumNewDesignators;
3954f4a2713aSLionel Sambuc     return;
3955f4a2713aSLionel Sambuc   } else if (NumNewDesignators == 1) {
3956f4a2713aSLionel Sambuc     Designators[Idx] = *First;
3957f4a2713aSLionel Sambuc     return;
3958f4a2713aSLionel Sambuc   }
3959f4a2713aSLionel Sambuc 
3960f4a2713aSLionel Sambuc   Designator *NewDesignators
3961f4a2713aSLionel Sambuc     = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
3962f4a2713aSLionel Sambuc   std::copy(Designators, Designators + Idx, NewDesignators);
3963f4a2713aSLionel Sambuc   std::copy(First, Last, NewDesignators + Idx);
3964f4a2713aSLionel Sambuc   std::copy(Designators + Idx + 1, Designators + NumDesignators,
3965f4a2713aSLionel Sambuc             NewDesignators + Idx + NumNewDesignators);
3966f4a2713aSLionel Sambuc   Designators = NewDesignators;
3967f4a2713aSLionel Sambuc   NumDesignators = NumDesignators - 1 + NumNewDesignators;
3968f4a2713aSLionel Sambuc }
3969f4a2713aSLionel Sambuc 
ParenListExpr(const ASTContext & C,SourceLocation lparenloc,ArrayRef<Expr * > exprs,SourceLocation rparenloc)3970f4a2713aSLionel Sambuc ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
3971f4a2713aSLionel Sambuc                              ArrayRef<Expr*> exprs,
3972f4a2713aSLionel Sambuc                              SourceLocation rparenloc)
3973f4a2713aSLionel Sambuc   : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
3974f4a2713aSLionel Sambuc          false, false, false, false),
3975f4a2713aSLionel Sambuc     NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3976f4a2713aSLionel Sambuc   Exprs = new (C) Stmt*[exprs.size()];
3977f4a2713aSLionel Sambuc   for (unsigned i = 0; i != exprs.size(); ++i) {
3978f4a2713aSLionel Sambuc     if (exprs[i]->isTypeDependent())
3979f4a2713aSLionel Sambuc       ExprBits.TypeDependent = true;
3980f4a2713aSLionel Sambuc     if (exprs[i]->isValueDependent())
3981f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
3982f4a2713aSLionel Sambuc     if (exprs[i]->isInstantiationDependent())
3983f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
3984f4a2713aSLionel Sambuc     if (exprs[i]->containsUnexpandedParameterPack())
3985f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
3986f4a2713aSLionel Sambuc 
3987f4a2713aSLionel Sambuc     Exprs[i] = exprs[i];
3988f4a2713aSLionel Sambuc   }
3989f4a2713aSLionel Sambuc }
3990f4a2713aSLionel Sambuc 
findInCopyConstruct(const Expr * e)3991f4a2713aSLionel Sambuc const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3992f4a2713aSLionel Sambuc   if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3993f4a2713aSLionel Sambuc     e = ewc->getSubExpr();
3994f4a2713aSLionel Sambuc   if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3995f4a2713aSLionel Sambuc     e = m->GetTemporaryExpr();
3996f4a2713aSLionel Sambuc   e = cast<CXXConstructExpr>(e)->getArg(0);
3997f4a2713aSLionel Sambuc   while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3998f4a2713aSLionel Sambuc     e = ice->getSubExpr();
3999f4a2713aSLionel Sambuc   return cast<OpaqueValueExpr>(e);
4000f4a2713aSLionel Sambuc }
4001f4a2713aSLionel Sambuc 
Create(const ASTContext & Context,EmptyShell sh,unsigned numSemanticExprs)4002f4a2713aSLionel Sambuc PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
4003f4a2713aSLionel Sambuc                                            EmptyShell sh,
4004f4a2713aSLionel Sambuc                                            unsigned numSemanticExprs) {
4005f4a2713aSLionel Sambuc   void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
4006f4a2713aSLionel Sambuc                                     (1 + numSemanticExprs) * sizeof(Expr*),
4007f4a2713aSLionel Sambuc                                   llvm::alignOf<PseudoObjectExpr>());
4008f4a2713aSLionel Sambuc   return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
4009f4a2713aSLionel Sambuc }
4010f4a2713aSLionel Sambuc 
PseudoObjectExpr(EmptyShell shell,unsigned numSemanticExprs)4011f4a2713aSLionel Sambuc PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
4012f4a2713aSLionel Sambuc   : Expr(PseudoObjectExprClass, shell) {
4013f4a2713aSLionel Sambuc   PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
4014f4a2713aSLionel Sambuc }
4015f4a2713aSLionel Sambuc 
Create(const ASTContext & C,Expr * syntax,ArrayRef<Expr * > semantics,unsigned resultIndex)4016f4a2713aSLionel Sambuc PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
4017f4a2713aSLionel Sambuc                                            ArrayRef<Expr*> semantics,
4018f4a2713aSLionel Sambuc                                            unsigned resultIndex) {
4019f4a2713aSLionel Sambuc   assert(syntax && "no syntactic expression!");
4020f4a2713aSLionel Sambuc   assert(semantics.size() && "no semantic expressions!");
4021f4a2713aSLionel Sambuc 
4022f4a2713aSLionel Sambuc   QualType type;
4023f4a2713aSLionel Sambuc   ExprValueKind VK;
4024f4a2713aSLionel Sambuc   if (resultIndex == NoResult) {
4025f4a2713aSLionel Sambuc     type = C.VoidTy;
4026f4a2713aSLionel Sambuc     VK = VK_RValue;
4027f4a2713aSLionel Sambuc   } else {
4028f4a2713aSLionel Sambuc     assert(resultIndex < semantics.size());
4029f4a2713aSLionel Sambuc     type = semantics[resultIndex]->getType();
4030f4a2713aSLionel Sambuc     VK = semantics[resultIndex]->getValueKind();
4031f4a2713aSLionel Sambuc     assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
4032f4a2713aSLionel Sambuc   }
4033f4a2713aSLionel Sambuc 
4034f4a2713aSLionel Sambuc   void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
4035f4a2713aSLionel Sambuc                               (1 + semantics.size()) * sizeof(Expr*),
4036f4a2713aSLionel Sambuc                             llvm::alignOf<PseudoObjectExpr>());
4037f4a2713aSLionel Sambuc   return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
4038f4a2713aSLionel Sambuc                                       resultIndex);
4039f4a2713aSLionel Sambuc }
4040f4a2713aSLionel Sambuc 
PseudoObjectExpr(QualType type,ExprValueKind VK,Expr * syntax,ArrayRef<Expr * > semantics,unsigned resultIndex)4041f4a2713aSLionel Sambuc PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
4042f4a2713aSLionel Sambuc                                    Expr *syntax, ArrayRef<Expr*> semantics,
4043f4a2713aSLionel Sambuc                                    unsigned resultIndex)
4044f4a2713aSLionel Sambuc   : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
4045f4a2713aSLionel Sambuc          /*filled in at end of ctor*/ false, false, false, false) {
4046f4a2713aSLionel Sambuc   PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
4047f4a2713aSLionel Sambuc   PseudoObjectExprBits.ResultIndex = resultIndex + 1;
4048f4a2713aSLionel Sambuc 
4049f4a2713aSLionel Sambuc   for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
4050f4a2713aSLionel Sambuc     Expr *E = (i == 0 ? syntax : semantics[i-1]);
4051f4a2713aSLionel Sambuc     getSubExprsBuffer()[i] = E;
4052f4a2713aSLionel Sambuc 
4053f4a2713aSLionel Sambuc     if (E->isTypeDependent())
4054f4a2713aSLionel Sambuc       ExprBits.TypeDependent = true;
4055f4a2713aSLionel Sambuc     if (E->isValueDependent())
4056f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
4057f4a2713aSLionel Sambuc     if (E->isInstantiationDependent())
4058f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
4059f4a2713aSLionel Sambuc     if (E->containsUnexpandedParameterPack())
4060f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
4061f4a2713aSLionel Sambuc 
4062f4a2713aSLionel Sambuc     if (isa<OpaqueValueExpr>(E))
4063*0a6a1f1dSLionel Sambuc       assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr &&
4064f4a2713aSLionel Sambuc              "opaque-value semantic expressions for pseudo-object "
4065f4a2713aSLionel Sambuc              "operations must have sources");
4066f4a2713aSLionel Sambuc   }
4067f4a2713aSLionel Sambuc }
4068f4a2713aSLionel Sambuc 
4069f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
4070f4a2713aSLionel Sambuc //  ExprIterator.
4071f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
4072f4a2713aSLionel Sambuc 
operator [](size_t idx)4073f4a2713aSLionel Sambuc Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
operator *() const4074f4a2713aSLionel Sambuc Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
operator ->() const4075f4a2713aSLionel Sambuc Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
operator [](size_t idx) const4076f4a2713aSLionel Sambuc const Expr* ConstExprIterator::operator[](size_t idx) const {
4077f4a2713aSLionel Sambuc   return cast<Expr>(I[idx]);
4078f4a2713aSLionel Sambuc }
operator *() const4079f4a2713aSLionel Sambuc const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
operator ->() const4080f4a2713aSLionel Sambuc const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
4081f4a2713aSLionel Sambuc 
4082f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
4083f4a2713aSLionel Sambuc //  Child Iterators for iterating over subexpressions/substatements
4084f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
4085f4a2713aSLionel Sambuc 
4086f4a2713aSLionel Sambuc // UnaryExprOrTypeTraitExpr
children()4087f4a2713aSLionel Sambuc Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
4088f4a2713aSLionel Sambuc   // If this is of a type and the type is a VLA type (and not a typedef), the
4089f4a2713aSLionel Sambuc   // size expression of the VLA needs to be treated as an executable expression.
4090f4a2713aSLionel Sambuc   // Why isn't this weirdness documented better in StmtIterator?
4091f4a2713aSLionel Sambuc   if (isArgumentType()) {
4092f4a2713aSLionel Sambuc     if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
4093f4a2713aSLionel Sambuc                                    getArgumentType().getTypePtr()))
4094f4a2713aSLionel Sambuc       return child_range(child_iterator(T), child_iterator());
4095f4a2713aSLionel Sambuc     return child_range();
4096f4a2713aSLionel Sambuc   }
4097f4a2713aSLionel Sambuc   return child_range(&Argument.Ex, &Argument.Ex + 1);
4098f4a2713aSLionel Sambuc }
4099f4a2713aSLionel Sambuc 
4100f4a2713aSLionel Sambuc // ObjCMessageExpr
children()4101f4a2713aSLionel Sambuc Stmt::child_range ObjCMessageExpr::children() {
4102f4a2713aSLionel Sambuc   Stmt **begin;
4103f4a2713aSLionel Sambuc   if (getReceiverKind() == Instance)
4104f4a2713aSLionel Sambuc     begin = reinterpret_cast<Stmt **>(this + 1);
4105f4a2713aSLionel Sambuc   else
4106f4a2713aSLionel Sambuc     begin = reinterpret_cast<Stmt **>(getArgs());
4107f4a2713aSLionel Sambuc   return child_range(begin,
4108f4a2713aSLionel Sambuc                      reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
4109f4a2713aSLionel Sambuc }
4110f4a2713aSLionel Sambuc 
ObjCArrayLiteral(ArrayRef<Expr * > Elements,QualType T,ObjCMethodDecl * Method,SourceRange SR)4111f4a2713aSLionel Sambuc ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
4112f4a2713aSLionel Sambuc                                    QualType T, ObjCMethodDecl *Method,
4113f4a2713aSLionel Sambuc                                    SourceRange SR)
4114f4a2713aSLionel Sambuc   : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
4115f4a2713aSLionel Sambuc          false, false, false, false),
4116f4a2713aSLionel Sambuc     NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
4117f4a2713aSLionel Sambuc {
4118f4a2713aSLionel Sambuc   Expr **SaveElements = getElements();
4119f4a2713aSLionel Sambuc   for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4120f4a2713aSLionel Sambuc     if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4121f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
4122f4a2713aSLionel Sambuc     if (Elements[I]->isInstantiationDependent())
4123f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
4124f4a2713aSLionel Sambuc     if (Elements[I]->containsUnexpandedParameterPack())
4125f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
4126f4a2713aSLionel Sambuc 
4127f4a2713aSLionel Sambuc     SaveElements[I] = Elements[I];
4128f4a2713aSLionel Sambuc   }
4129f4a2713aSLionel Sambuc }
4130f4a2713aSLionel Sambuc 
Create(const ASTContext & C,ArrayRef<Expr * > Elements,QualType T,ObjCMethodDecl * Method,SourceRange SR)4131f4a2713aSLionel Sambuc ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
4132f4a2713aSLionel Sambuc                                            ArrayRef<Expr *> Elements,
4133f4a2713aSLionel Sambuc                                            QualType T, ObjCMethodDecl * Method,
4134f4a2713aSLionel Sambuc                                            SourceRange SR) {
4135f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4136f4a2713aSLionel Sambuc                          + Elements.size() * sizeof(Expr *));
4137f4a2713aSLionel Sambuc   return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4138f4a2713aSLionel Sambuc }
4139f4a2713aSLionel Sambuc 
CreateEmpty(const ASTContext & C,unsigned NumElements)4140f4a2713aSLionel Sambuc ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
4141f4a2713aSLionel Sambuc                                                 unsigned NumElements) {
4142f4a2713aSLionel Sambuc 
4143f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4144f4a2713aSLionel Sambuc                          + NumElements * sizeof(Expr *));
4145f4a2713aSLionel Sambuc   return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4146f4a2713aSLionel Sambuc }
4147f4a2713aSLionel Sambuc 
ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,bool HasPackExpansions,QualType T,ObjCMethodDecl * method,SourceRange SR)4148f4a2713aSLionel Sambuc ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4149f4a2713aSLionel Sambuc                                              ArrayRef<ObjCDictionaryElement> VK,
4150f4a2713aSLionel Sambuc                                              bool HasPackExpansions,
4151f4a2713aSLionel Sambuc                                              QualType T, ObjCMethodDecl *method,
4152f4a2713aSLionel Sambuc                                              SourceRange SR)
4153f4a2713aSLionel Sambuc   : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4154f4a2713aSLionel Sambuc          false, false),
4155f4a2713aSLionel Sambuc     NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4156f4a2713aSLionel Sambuc     DictWithObjectsMethod(method)
4157f4a2713aSLionel Sambuc {
4158f4a2713aSLionel Sambuc   KeyValuePair *KeyValues = getKeyValues();
4159f4a2713aSLionel Sambuc   ExpansionData *Expansions = getExpansionData();
4160f4a2713aSLionel Sambuc   for (unsigned I = 0; I < NumElements; I++) {
4161f4a2713aSLionel Sambuc     if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4162f4a2713aSLionel Sambuc         VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4163f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
4164f4a2713aSLionel Sambuc     if (VK[I].Key->isInstantiationDependent() ||
4165f4a2713aSLionel Sambuc         VK[I].Value->isInstantiationDependent())
4166f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
4167f4a2713aSLionel Sambuc     if (VK[I].EllipsisLoc.isInvalid() &&
4168f4a2713aSLionel Sambuc         (VK[I].Key->containsUnexpandedParameterPack() ||
4169f4a2713aSLionel Sambuc          VK[I].Value->containsUnexpandedParameterPack()))
4170f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
4171f4a2713aSLionel Sambuc 
4172f4a2713aSLionel Sambuc     KeyValues[I].Key = VK[I].Key;
4173f4a2713aSLionel Sambuc     KeyValues[I].Value = VK[I].Value;
4174f4a2713aSLionel Sambuc     if (Expansions) {
4175f4a2713aSLionel Sambuc       Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4176f4a2713aSLionel Sambuc       if (VK[I].NumExpansions)
4177f4a2713aSLionel Sambuc         Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4178f4a2713aSLionel Sambuc       else
4179f4a2713aSLionel Sambuc         Expansions[I].NumExpansionsPlusOne = 0;
4180f4a2713aSLionel Sambuc     }
4181f4a2713aSLionel Sambuc   }
4182f4a2713aSLionel Sambuc }
4183f4a2713aSLionel Sambuc 
4184f4a2713aSLionel Sambuc ObjCDictionaryLiteral *
Create(const ASTContext & C,ArrayRef<ObjCDictionaryElement> VK,bool HasPackExpansions,QualType T,ObjCMethodDecl * method,SourceRange SR)4185f4a2713aSLionel Sambuc ObjCDictionaryLiteral::Create(const ASTContext &C,
4186f4a2713aSLionel Sambuc                               ArrayRef<ObjCDictionaryElement> VK,
4187f4a2713aSLionel Sambuc                               bool HasPackExpansions,
4188f4a2713aSLionel Sambuc                               QualType T, ObjCMethodDecl *method,
4189f4a2713aSLionel Sambuc                               SourceRange SR) {
4190f4a2713aSLionel Sambuc   unsigned ExpansionsSize = 0;
4191f4a2713aSLionel Sambuc   if (HasPackExpansions)
4192f4a2713aSLionel Sambuc     ExpansionsSize = sizeof(ExpansionData) * VK.size();
4193f4a2713aSLionel Sambuc 
4194f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4195f4a2713aSLionel Sambuc                          sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4196f4a2713aSLionel Sambuc   return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4197f4a2713aSLionel Sambuc }
4198f4a2713aSLionel Sambuc 
4199f4a2713aSLionel Sambuc ObjCDictionaryLiteral *
CreateEmpty(const ASTContext & C,unsigned NumElements,bool HasPackExpansions)4200f4a2713aSLionel Sambuc ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
4201f4a2713aSLionel Sambuc                                    bool HasPackExpansions) {
4202f4a2713aSLionel Sambuc   unsigned ExpansionsSize = 0;
4203f4a2713aSLionel Sambuc   if (HasPackExpansions)
4204f4a2713aSLionel Sambuc     ExpansionsSize = sizeof(ExpansionData) * NumElements;
4205f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4206f4a2713aSLionel Sambuc                          sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4207f4a2713aSLionel Sambuc   return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4208f4a2713aSLionel Sambuc                                          HasPackExpansions);
4209f4a2713aSLionel Sambuc }
4210f4a2713aSLionel Sambuc 
Create(const ASTContext & C,Expr * base,Expr * key,QualType T,ObjCMethodDecl * getMethod,ObjCMethodDecl * setMethod,SourceLocation RB)4211f4a2713aSLionel Sambuc ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
4212f4a2713aSLionel Sambuc                                                    Expr *base,
4213f4a2713aSLionel Sambuc                                                    Expr *key, QualType T,
4214f4a2713aSLionel Sambuc                                                    ObjCMethodDecl *getMethod,
4215f4a2713aSLionel Sambuc                                                    ObjCMethodDecl *setMethod,
4216f4a2713aSLionel Sambuc                                                    SourceLocation RB) {
4217f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4218f4a2713aSLionel Sambuc   return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4219f4a2713aSLionel Sambuc                                         OK_ObjCSubscript,
4220f4a2713aSLionel Sambuc                                         getMethod, setMethod, RB);
4221f4a2713aSLionel Sambuc }
4222f4a2713aSLionel Sambuc 
AtomicExpr(SourceLocation BLoc,ArrayRef<Expr * > args,QualType t,AtomicOp op,SourceLocation RP)4223f4a2713aSLionel Sambuc AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
4224f4a2713aSLionel Sambuc                        QualType t, AtomicOp op, SourceLocation RP)
4225f4a2713aSLionel Sambuc   : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4226f4a2713aSLionel Sambuc          false, false, false, false),
4227f4a2713aSLionel Sambuc     NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
4228f4a2713aSLionel Sambuc {
4229f4a2713aSLionel Sambuc   assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4230f4a2713aSLionel Sambuc   for (unsigned i = 0; i != args.size(); i++) {
4231f4a2713aSLionel Sambuc     if (args[i]->isTypeDependent())
4232f4a2713aSLionel Sambuc       ExprBits.TypeDependent = true;
4233f4a2713aSLionel Sambuc     if (args[i]->isValueDependent())
4234f4a2713aSLionel Sambuc       ExprBits.ValueDependent = true;
4235f4a2713aSLionel Sambuc     if (args[i]->isInstantiationDependent())
4236f4a2713aSLionel Sambuc       ExprBits.InstantiationDependent = true;
4237f4a2713aSLionel Sambuc     if (args[i]->containsUnexpandedParameterPack())
4238f4a2713aSLionel Sambuc       ExprBits.ContainsUnexpandedParameterPack = true;
4239f4a2713aSLionel Sambuc 
4240f4a2713aSLionel Sambuc     SubExprs[i] = args[i];
4241f4a2713aSLionel Sambuc   }
4242f4a2713aSLionel Sambuc }
4243f4a2713aSLionel Sambuc 
getNumSubExprs(AtomicOp Op)4244f4a2713aSLionel Sambuc unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4245f4a2713aSLionel Sambuc   switch (Op) {
4246f4a2713aSLionel Sambuc   case AO__c11_atomic_init:
4247f4a2713aSLionel Sambuc   case AO__c11_atomic_load:
4248f4a2713aSLionel Sambuc   case AO__atomic_load_n:
4249f4a2713aSLionel Sambuc     return 2;
4250f4a2713aSLionel Sambuc 
4251f4a2713aSLionel Sambuc   case AO__c11_atomic_store:
4252f4a2713aSLionel Sambuc   case AO__c11_atomic_exchange:
4253f4a2713aSLionel Sambuc   case AO__atomic_load:
4254f4a2713aSLionel Sambuc   case AO__atomic_store:
4255f4a2713aSLionel Sambuc   case AO__atomic_store_n:
4256f4a2713aSLionel Sambuc   case AO__atomic_exchange_n:
4257f4a2713aSLionel Sambuc   case AO__c11_atomic_fetch_add:
4258f4a2713aSLionel Sambuc   case AO__c11_atomic_fetch_sub:
4259f4a2713aSLionel Sambuc   case AO__c11_atomic_fetch_and:
4260f4a2713aSLionel Sambuc   case AO__c11_atomic_fetch_or:
4261f4a2713aSLionel Sambuc   case AO__c11_atomic_fetch_xor:
4262f4a2713aSLionel Sambuc   case AO__atomic_fetch_add:
4263f4a2713aSLionel Sambuc   case AO__atomic_fetch_sub:
4264f4a2713aSLionel Sambuc   case AO__atomic_fetch_and:
4265f4a2713aSLionel Sambuc   case AO__atomic_fetch_or:
4266f4a2713aSLionel Sambuc   case AO__atomic_fetch_xor:
4267f4a2713aSLionel Sambuc   case AO__atomic_fetch_nand:
4268f4a2713aSLionel Sambuc   case AO__atomic_add_fetch:
4269f4a2713aSLionel Sambuc   case AO__atomic_sub_fetch:
4270f4a2713aSLionel Sambuc   case AO__atomic_and_fetch:
4271f4a2713aSLionel Sambuc   case AO__atomic_or_fetch:
4272f4a2713aSLionel Sambuc   case AO__atomic_xor_fetch:
4273f4a2713aSLionel Sambuc   case AO__atomic_nand_fetch:
4274f4a2713aSLionel Sambuc     return 3;
4275f4a2713aSLionel Sambuc 
4276f4a2713aSLionel Sambuc   case AO__atomic_exchange:
4277f4a2713aSLionel Sambuc     return 4;
4278f4a2713aSLionel Sambuc 
4279f4a2713aSLionel Sambuc   case AO__c11_atomic_compare_exchange_strong:
4280f4a2713aSLionel Sambuc   case AO__c11_atomic_compare_exchange_weak:
4281f4a2713aSLionel Sambuc     return 5;
4282f4a2713aSLionel Sambuc 
4283f4a2713aSLionel Sambuc   case AO__atomic_compare_exchange:
4284f4a2713aSLionel Sambuc   case AO__atomic_compare_exchange_n:
4285f4a2713aSLionel Sambuc     return 6;
4286f4a2713aSLionel Sambuc   }
4287f4a2713aSLionel Sambuc   llvm_unreachable("unknown atomic op");
4288f4a2713aSLionel Sambuc }
4289