blob: ea91fbec4f85833c886bb9aa8bb8569e1e6d4b4c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnera4d55d82008-10-06 06:40:35 +000014#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000017#include "clang/AST/DeclCXX.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor25d0a0f2012-02-23 07:33:15 +000020#include "clang/AST/EvaluatedExprVisitor.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000026#include "clang/Basic/CharInfo.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000028#include "clang/Basic/TargetInfo.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000029#include "clang/Lex/Lexer.h"
30#include "clang/Lex/LiteralSupport.h"
31#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000032#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000033#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000034#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000035#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000036using namespace clang;
37
Rafael Espindola8d852e32012-06-27 18:18:05 +000038const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindola632fbaa2012-06-28 01:56:38 +000039 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000040
41 QualType DerivedType = E->getType();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000042 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
43 DerivedType = PTy->getPointeeType();
44
Rafael Espindola251c4492012-07-17 20:24:05 +000045 if (DerivedType->isDependentType())
46 return NULL;
47
Rafael Espindola0b4fe502012-06-26 17:45:31 +000048 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000049 Decl *D = Ty->getDecl();
50 return cast<CXXRecordDecl>(D);
51}
52
Richard Smith4e43dec2013-06-03 00:17:11 +000053const Expr *Expr::skipRValueSubobjectAdjustments(
54 SmallVectorImpl<const Expr *> &CommaLHSs,
55 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
Rafael Espindola0a7dd832012-10-27 01:03:43 +000056 const Expr *E = this;
57 while (true) {
58 E = E->IgnoreParens();
59
60 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
61 if ((CE->getCastKind() == CK_DerivedToBase ||
62 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
63 E->getType()->isRecordType()) {
64 E = CE->getSubExpr();
65 CXXRecordDecl *Derived
66 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
67 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
68 continue;
69 }
70
71 if (CE->getCastKind() == CK_NoOp) {
72 E = CE->getSubExpr();
73 continue;
74 }
75 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
Richard Smithd6b69872013-06-15 00:30:29 +000076 if (!ME->isArrow()) {
Rafael Espindola0a7dd832012-10-27 01:03:43 +000077 assert(ME->getBase()->getType()->isRecordType());
78 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
Richard Smithd6b69872013-06-15 00:30:29 +000079 if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
Richard Smithd1b55dc2013-06-03 07:13:35 +000080 E = ME->getBase();
81 Adjustments.push_back(SubobjectAdjustment(Field));
82 continue;
83 }
Rafael Espindola0a7dd832012-10-27 01:03:43 +000084 }
85 }
86 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
87 if (BO->isPtrMemOp()) {
Rafael Espindolaef4b6662012-11-01 14:32:20 +000088 assert(BO->getRHS()->isRValue());
Rafael Espindola0a7dd832012-10-27 01:03:43 +000089 E = BO->getLHS();
90 const MemberPointerType *MPT =
91 BO->getRHS()->getType()->getAs<MemberPointerType>();
92 Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
Richard Smith4e43dec2013-06-03 00:17:11 +000093 continue;
94 } else if (BO->getOpcode() == BO_Comma) {
95 CommaLHSs.push_back(BO->getLHS());
96 E = BO->getRHS();
97 continue;
Rafael Espindola0a7dd832012-10-27 01:03:43 +000098 }
99 }
100
101 // Nothing changed.
102 break;
103 }
104 return E;
105}
106
107const Expr *
108Expr::findMaterializedTemporary(const MaterializeTemporaryExpr *&MTE) const {
109 const Expr *E = this;
Richard Smithc3bf52c2013-04-20 22:23:05 +0000110
111 // This might be a default initializer for a reference member. Walk over the
112 // wrapper node for that.
113 if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
114 E = DAE->getExpr();
115
Rafael Espindola0a7dd832012-10-27 01:03:43 +0000116 // Look through single-element init lists that claim to be lvalues. They're
117 // just syntactic wrappers in this case.
118 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) {
Richard Smithc3bf52c2013-04-20 22:23:05 +0000119 if (ILE->getNumInits() == 1 && ILE->isGLValue()) {
Rafael Espindola0a7dd832012-10-27 01:03:43 +0000120 E = ILE->getInit(0);
Richard Smithc3bf52c2013-04-20 22:23:05 +0000121 if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
122 E = DAE->getExpr();
123 }
Rafael Espindola0a7dd832012-10-27 01:03:43 +0000124 }
125
126 // Look through expressions for materialized temporaries (for now).
127 if (const MaterializeTemporaryExpr *M
128 = dyn_cast<MaterializeTemporaryExpr>(E)) {
129 MTE = M;
130 E = M->GetTemporaryExpr();
131 }
132
133 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
134 E = DAE->getExpr();
135 return E;
136}
137
Chris Lattner2b334bb2010-04-16 23:34:13 +0000138/// isKnownToHaveBooleanValue - Return true if this is an integer expression
139/// that is known to return 0 or 1. This happens for _Bool/bool expressions
140/// but also int expressions which are produced by things like comparisons in
141/// C.
142bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +0000143 const Expr *E = IgnoreParens();
144
Chris Lattner2b334bb2010-04-16 23:34:13 +0000145 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +0000146 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +0000147 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +0000148 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +0000149
Peter Collingbournef111d932011-04-15 00:35:48 +0000150 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000151 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000152 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000153 return UO->getSubExpr()->isKnownToHaveBooleanValue();
154 default:
155 return false;
156 }
157 }
Sean Huntc3021132010-05-05 15:23:54 +0000158
John McCall6907fbe2010-06-12 01:56:02 +0000159 // Only look through implicit casts. If the user writes
160 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +0000161 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000162 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000163
Peter Collingbournef111d932011-04-15 00:35:48 +0000164 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000165 switch (BO->getOpcode()) {
166 default: return false;
John McCall2de56d12010-08-25 11:45:40 +0000167 case BO_LT: // Relational operators.
168 case BO_GT:
169 case BO_LE:
170 case BO_GE:
171 case BO_EQ: // Equality operators.
172 case BO_NE:
173 case BO_LAnd: // AND operator.
174 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +0000175 return true;
Sean Huntc3021132010-05-05 15:23:54 +0000176
John McCall2de56d12010-08-25 11:45:40 +0000177 case BO_And: // Bitwise AND operator.
178 case BO_Xor: // Bitwise XOR operator.
179 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +0000180 // Handle things like (x==2)|(y==12).
181 return BO->getLHS()->isKnownToHaveBooleanValue() &&
182 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000183
John McCall2de56d12010-08-25 11:45:40 +0000184 case BO_Comma:
185 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000186 return BO->getRHS()->isKnownToHaveBooleanValue();
187 }
188 }
Sean Huntc3021132010-05-05 15:23:54 +0000189
Peter Collingbournef111d932011-04-15 00:35:48 +0000190 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000191 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
192 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000193
Chris Lattner2b334bb2010-04-16 23:34:13 +0000194 return false;
195}
196
John McCall63c00d72011-02-09 08:16:59 +0000197// Amusing macro metaprogramming hack: check whether a class provides
198// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000199//
200// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-02-09 08:16:59 +0000201namespace {
202 /// This implementation is used when a class provides a custom
203 /// implementation of getExprLoc.
204 template <class E, class T>
205 SourceLocation getExprLocImpl(const Expr *expr,
206 SourceLocation (T::*v)() const) {
207 return static_cast<const E*>(expr)->getExprLoc();
208 }
209
210 /// This implementation is used when a class doesn't provide
211 /// a custom implementation of getExprLoc. Overload resolution
212 /// should pick it over the implementation above because it's
213 /// more specialized according to function template partial ordering.
214 template <class E>
215 SourceLocation getExprLocImpl(const Expr *expr,
216 SourceLocation (Expr::*v)() const) {
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000217 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-02-09 08:16:59 +0000218 }
219}
220
221SourceLocation Expr::getExprLoc() const {
222 switch (getStmtClass()) {
223 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
224#define ABSTRACT_STMT(type)
225#define STMT(type, base) \
226 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
227#define EXPR(type, base) \
228 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
229#include "clang/AST/StmtNodes.inc"
230 }
231 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000232}
233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234//===----------------------------------------------------------------------===//
235// Primary Expressions.
236//===----------------------------------------------------------------------===//
237
Douglas Gregor561f8122011-07-01 01:22:09 +0000238/// \brief Compute the type-, value-, and instantiation-dependence of a
239/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000240/// based on the declaration being referenced.
Craig Topper9db7a7e2013-08-22 04:58:56 +0000241static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
242 QualType T, bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000243 bool &ValueDependent,
244 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000245 TypeDependent = false;
246 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000248
249 // (TD) C++ [temp.dep.expr]p3:
250 // An id-expression is type-dependent if it contains:
251 //
Sean Huntc3021132010-05-05 15:23:54 +0000252 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000253 //
254 // (VD) C++ [temp.dep.constexpr]p2:
255 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000256
Douglas Gregor0da76df2009-11-23 11:41:28 +0000257 // (TD) - an identifier that was declared with dependent type
258 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000259 if (T->isDependentType()) {
260 TypeDependent = true;
261 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000262 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000263 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000264 } else if (T->isInstantiationDependentType()) {
265 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000266 }
Douglas Gregord967e312011-01-19 21:52:31 +0000267
Douglas Gregor0da76df2009-11-23 11:41:28 +0000268 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000269 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000270 == DeclarationName::CXXConversionFunctionName) {
271 QualType T = D->getDeclName().getCXXNameType();
272 if (T->isDependentType()) {
273 TypeDependent = true;
274 ValueDependent = true;
275 InstantiationDependent = true;
276 return;
277 }
278
279 if (T->isInstantiationDependentType())
280 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000281 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000282
Douglas Gregor0da76df2009-11-23 11:41:28 +0000283 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000284 if (isa<NonTypeTemplateParmDecl>(D)) {
285 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000286 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000287 return;
288 }
289
Douglas Gregor0da76df2009-11-23 11:41:28 +0000290 // (VD) - a constant with integral or enumeration type and is
291 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000292 // (VD) - a constant with literal type and is initialized with an
293 // expression that is value-dependent [C++11].
294 // (VD) - FIXME: Missing from the standard:
295 // - an entity with reference type and is initialized with an
296 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000297 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith80ad52f2013-01-02 11:42:31 +0000298 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smitha10b9782013-04-22 15:31:51 +0000299 Var->getType()->isLiteralType(Ctx) :
Richard Smithdb1822c2011-11-08 01:31:09 +0000300 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikie4ef832f2012-08-10 00:55:35 +0000301 (Var->getType().isConstQualified() ||
Richard Smithdb1822c2011-11-08 01:31:09 +0000302 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000303 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000304 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000305 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000306 InstantiationDependent = true;
307 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000308 }
309
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000310 // (VD) - FIXME: Missing from the standard:
311 // - a member function or a static data member of the current
312 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000313 if (Var->isStaticDataMember() &&
314 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000315 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000316 InstantiationDependent = true;
317 }
Douglas Gregord967e312011-01-19 21:52:31 +0000318
319 return;
320 }
321
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000322 // (VD) - FIXME: Missing from the standard:
323 // - a member function or a static data member of the current
324 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000325 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
326 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000327 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000328 }
Douglas Gregord967e312011-01-19 21:52:31 +0000329}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000330
Craig Topper9db7a7e2013-08-22 04:58:56 +0000331void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000332 bool TypeDependent = false;
333 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000334 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000335 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
336 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000337
338 // (TD) C++ [temp.dep.expr]p3:
339 // An id-expression is type-dependent if it contains:
340 //
341 // and
342 //
343 // (VD) C++ [temp.dep.constexpr]p2:
344 // An identifier is value-dependent if it is:
345 if (!TypeDependent && !ValueDependent &&
346 hasExplicitTemplateArgs() &&
347 TemplateSpecializationType::anyDependentTemplateArguments(
348 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000349 getNumTemplateArgs(),
350 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000351 TypeDependent = true;
352 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000353 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000354 }
355
356 ExprBits.TypeDependent = TypeDependent;
357 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000358 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000359
Douglas Gregor10738d32010-12-23 23:51:58 +0000360 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000361 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000362 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000363}
364
Craig Topper9db7a7e2013-08-22 04:58:56 +0000365DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000366 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000367 SourceLocation TemplateKWLoc,
John McCallf4b88a42012-03-10 09:33:50 +0000368 ValueDecl *D, bool RefersToEnclosingLocal,
369 const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000370 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000371 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000372 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000373 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000374 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
375 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000376 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000377 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000378 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
379 if (FoundD)
380 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000381 DeclRefExprBits.HasTemplateKWAndArgsInfo
382 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCallf4b88a42012-03-10 09:33:50 +0000383 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor561f8122011-07-01 01:22:09 +0000384 if (TemplateArgs) {
385 bool Dependent = false;
386 bool InstantiationDependent = false;
387 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000388 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
389 Dependent,
390 InstantiationDependent,
391 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000392 if (InstantiationDependent)
393 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000394 } else if (TemplateKWLoc.isValid()) {
395 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000396 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000397 DeclRefExprBits.HadMultipleCandidates = 0;
398
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000399 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000400}
401
Craig Topper9db7a7e2013-08-22 04:58:56 +0000402DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000403 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000404 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000405 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000406 bool RefersToEnclosingLocal,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000407 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000408 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000409 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000410 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000411 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000412 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000413 RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000414 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000415 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000416}
417
Craig Topper9db7a7e2013-08-22 04:58:56 +0000418DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000419 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000420 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000421 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000422 bool RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000423 const DeclarationNameInfo &NameInfo,
424 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000425 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000426 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000427 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000428 // Filter out cases where the found Decl is the same as the value refenenced.
429 if (D == FoundD)
430 FoundD = 0;
431
Douglas Gregora2813ce2009-10-23 18:54:35 +0000432 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7247c882013-05-15 07:37:26 +0000433 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000434 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000435 if (FoundD)
436 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000437 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000438 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
439 else if (TemplateKWLoc.isValid())
440 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000441
Chris Lattner32488542010-10-30 05:14:06 +0000442 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000443 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000444 RefersToEnclosingLocal,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000445 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000446}
447
Craig Topper9db7a7e2013-08-22 04:58:56 +0000448DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000449 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000450 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000451 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000452 unsigned NumTemplateArgs) {
453 std::size_t Size = sizeof(DeclRefExpr);
454 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000455 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000456 if (HasFoundDecl)
457 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000458 if (HasTemplateKWAndArgsInfo)
459 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000460
Chris Lattner32488542010-10-30 05:14:06 +0000461 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000462 return new (Mem) DeclRefExpr(EmptyShell());
463}
464
Daniel Dunbar396ec672012-03-09 15:39:15 +0000465SourceLocation DeclRefExpr::getLocStart() const {
466 if (hasQualifier())
467 return getQualifierLoc().getBeginLoc();
468 return getNameInfo().getLocStart();
469}
470SourceLocation DeclRefExpr::getLocEnd() const {
471 if (hasExplicitTemplateArgs())
472 return getRAngleLoc();
473 return getNameInfo().getLocEnd();
474}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000475
Anders Carlsson3a082d82009-09-08 18:24:21 +0000476// FIXME: Maybe this should use DeclPrinter with a special "print predefined
477// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000478std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
479 ASTContext &Context = CurrentDecl->getASTContext();
480
Anders Carlsson3a082d82009-09-08 18:24:21 +0000481 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000482 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000483 return FD->getNameAsString();
484
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000485 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000486 llvm::raw_svector_ostream Out(Name);
487
488 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000489 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000490 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000491 if (MD->isStatic())
492 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000493 }
494
David Blaikie4e4d0842012-03-11 07:00:24 +0000495 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramerb063ef02013-02-23 13:53:57 +0000496 std::string Proto;
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000497 llvm::raw_string_ostream POut(Proto);
Benjamin Kramerb063ef02013-02-23 13:53:57 +0000498 FD->printQualifiedName(POut, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000499
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000500 const FunctionDecl *Decl = FD;
501 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
502 Decl = Pattern;
503 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000504 const FunctionProtoType *FT = 0;
505 if (FD->hasWrittenPrototype())
506 FT = dyn_cast<FunctionProtoType>(AFT);
507
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000508 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000509 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000510 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000511 if (i) POut << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000512 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000513 }
514
515 if (FT->isVariadic()) {
516 if (FD->getNumParams()) POut << ", ";
517 POut << "...";
518 }
519 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000520 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000521
Sam Weinig4eadcc52009-12-27 01:38:20 +0000522 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis4ae711b2012-12-14 19:44:11 +0000523 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikie4ef832f2012-08-10 00:55:35 +0000524 if (FT->isConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000525 POut << " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000526 if (FT->isVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000527 POut << " volatile";
528 RefQualifierKind Ref = MD->getRefQualifier();
529 if (Ref == RQ_LValue)
530 POut << " &";
531 else if (Ref == RQ_RValue)
532 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000533 }
534
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000535 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
536 SpecsTy Specs;
537 const DeclContext *Ctx = FD->getDeclContext();
538 while (Ctx && isa<NamedDecl>(Ctx)) {
539 const ClassTemplateSpecializationDecl *Spec
540 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
541 if (Spec && !Spec->isExplicitSpecialization())
542 Specs.push_back(Spec);
543 Ctx = Ctx->getParent();
544 }
545
546 std::string TemplateParams;
547 llvm::raw_string_ostream TOut(TemplateParams);
548 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
549 I != E; ++I) {
550 const TemplateParameterList *Params
551 = (*I)->getSpecializedTemplate()->getTemplateParameters();
552 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
553 assert(Params->size() == Args.size());
554 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
555 StringRef Param = Params->getParam(i)->getName();
556 if (Param.empty()) continue;
557 TOut << Param << " = ";
558 Args.get(i).print(Policy, TOut);
559 TOut << ", ";
560 }
561 }
562
563 FunctionTemplateSpecializationInfo *FSI
564 = FD->getTemplateSpecializationInfo();
565 if (FSI && !FSI->isExplicitSpecialization()) {
566 const TemplateParameterList* Params
567 = FSI->getTemplate()->getTemplateParameters();
568 const TemplateArgumentList* Args = FSI->TemplateArguments;
569 assert(Params->size() == Args->size());
570 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
571 StringRef Param = Params->getParam(i)->getName();
572 if (Param.empty()) continue;
573 TOut << Param << " = ";
574 Args->get(i).print(Policy, TOut);
575 TOut << ", ";
576 }
577 }
578
579 TOut.flush();
580 if (!TemplateParams.empty()) {
581 // remove the trailing comma and space
582 TemplateParams.resize(TemplateParams.size() - 2);
583 POut << " [" << TemplateParams << "]";
584 }
585
586 POut.flush();
587
Benjamin Kramer28bdbf02013-08-21 11:45:27 +0000588 // Print "auto" for all deduced return types. This includes C++1y return
589 // type deduction and lambdas. For trailing return types resolve the
590 // decltype expression. Otherwise print the real type when this is
591 // not a constructor or destructor.
592 if ((isa<CXXMethodDecl>(FD) &&
593 cast<CXXMethodDecl>(FD)->getParent()->isLambda()) ||
594 (FT && FT->getResultType()->getAs<AutoType>()))
595 Proto = "auto " + Proto;
596 else if (FT && FT->getResultType()->getAs<DecltypeType>())
597 FT->getResultType()->getAs<DecltypeType>()->getUnderlyingType()
598 .getAsStringInternal(Proto, Policy);
599 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000600 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000601
602 Out << Proto;
603
604 Out.flush();
605 return Name.str().str();
606 }
Wei Pan15b26742013-08-26 14:27:34 +0000607 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
608 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
609 // Skip to its enclosing function or method, but not its enclosing
610 // CapturedDecl.
611 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
612 const Decl *D = Decl::castFromDeclContext(DC);
613 return ComputeName(IT, D);
614 }
615 llvm_unreachable("CapturedDecl not inside a function or method");
616 }
Anders Carlsson3a082d82009-09-08 18:24:21 +0000617 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000618 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000619 llvm::raw_svector_ostream Out(Name);
620 Out << (MD->isInstanceMethod() ? '-' : '+');
621 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000622
623 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
624 // a null check to avoid a crash.
625 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000626 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000627
Anders Carlsson3a082d82009-09-08 18:24:21 +0000628 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000629 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000630 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000631
Anders Carlsson3a082d82009-09-08 18:24:21 +0000632 Out << ' ';
633 Out << MD->getSelector().getAsString();
634 Out << ']';
635
636 Out.flush();
637 return Name.str().str();
638 }
639 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
640 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
641 return "top level";
642 }
643 return "";
644}
645
Craig Topper05ed1a02013-08-18 10:09:15 +0000646void APNumericStorage::setIntValue(const ASTContext &C,
647 const llvm::APInt &Val) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000648 if (hasAllocation())
649 C.Deallocate(pVal);
650
651 BitWidth = Val.getBitWidth();
652 unsigned NumWords = Val.getNumWords();
653 const uint64_t* Words = Val.getRawData();
654 if (NumWords > 1) {
655 pVal = new (C) uint64_t[NumWords];
656 std::copy(Words, Words + NumWords, pVal);
657 } else if (NumWords == 1)
658 VAL = Words[0];
659 else
660 VAL = 0;
661}
662
Craig Topper05ed1a02013-08-18 10:09:15 +0000663IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000664 QualType type, SourceLocation l)
665 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
666 false, false),
667 Loc(l) {
668 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
669 assert(V.getBitWidth() == C.getIntWidth(type) &&
670 "Integer type is not the correct size for constant.");
671 setValue(C, V);
672}
673
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000674IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000675IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000676 QualType type, SourceLocation l) {
677 return new (C) IntegerLiteral(C, V, type, l);
678}
679
680IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000681IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000682 return new (C) IntegerLiteral(Empty);
683}
684
Craig Topper05ed1a02013-08-18 10:09:15 +0000685FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000686 bool isexact, QualType Type, SourceLocation L)
687 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
688 false, false), Loc(L) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000689 setSemantics(V.getSemantics());
Benjamin Kramer478851c2012-07-04 17:04:04 +0000690 FloatingLiteralBits.IsExact = isexact;
691 setValue(C, V);
692}
693
Craig Topper05ed1a02013-08-18 10:09:15 +0000694FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer478851c2012-07-04 17:04:04 +0000695 : Expr(FloatingLiteralClass, Empty) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000696 setRawSemantics(IEEEhalf);
Benjamin Kramer478851c2012-07-04 17:04:04 +0000697 FloatingLiteralBits.IsExact = false;
698}
699
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000700FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000701FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000702 bool isexact, QualType Type, SourceLocation L) {
703 return new (C) FloatingLiteral(C, V, isexact, Type, L);
704}
705
706FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000707FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000708 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000709}
710
Tim Northover9ec55f22013-01-22 09:46:51 +0000711const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
712 switch(FloatingLiteralBits.Semantics) {
713 case IEEEhalf:
714 return llvm::APFloat::IEEEhalf;
715 case IEEEsingle:
716 return llvm::APFloat::IEEEsingle;
717 case IEEEdouble:
718 return llvm::APFloat::IEEEdouble;
719 case x87DoubleExtended:
720 return llvm::APFloat::x87DoubleExtended;
721 case IEEEquad:
722 return llvm::APFloat::IEEEquad;
723 case PPCDoubleDouble:
724 return llvm::APFloat::PPCDoubleDouble;
725 }
726 llvm_unreachable("Unrecognised floating semantics");
727}
728
729void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
730 if (&Sem == &llvm::APFloat::IEEEhalf)
731 FloatingLiteralBits.Semantics = IEEEhalf;
732 else if (&Sem == &llvm::APFloat::IEEEsingle)
733 FloatingLiteralBits.Semantics = IEEEsingle;
734 else if (&Sem == &llvm::APFloat::IEEEdouble)
735 FloatingLiteralBits.Semantics = IEEEdouble;
736 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
737 FloatingLiteralBits.Semantics = x87DoubleExtended;
738 else if (&Sem == &llvm::APFloat::IEEEquad)
739 FloatingLiteralBits.Semantics = IEEEquad;
740 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
741 FloatingLiteralBits.Semantics = PPCDoubleDouble;
742 else
743 llvm_unreachable("Unknown floating semantics");
744}
745
Chris Lattnerda8249e2008-06-07 22:13:43 +0000746/// getValueAsApproximateDouble - This returns the value as an inaccurate
747/// double. Note that this may cause loss of precision, but is useful for
748/// debugging dumps, etc.
749double FloatingLiteral::getValueAsApproximateDouble() const {
750 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000751 bool ignored;
752 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
753 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000754 return V.convertToDouble();
755}
756
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000757int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000758 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000759 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000760 case Ascii:
761 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000762 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000763 break;
764 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000765 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000766 break;
767 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000768 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000769 break;
770 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000771 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000772 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000773 }
774 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
775 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000776 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000777 && "character byte widths supported are 1, 2, and 4 only");
778 return CharByteWidth;
779}
780
Craig Topper05ed1a02013-08-18 10:09:15 +0000781StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000782 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000783 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000784 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000785 // Allocate enough space for the StringLiteral plus an array of locations for
786 // any concatenated string tokens.
787 void *Mem = C.Allocate(sizeof(StringLiteral)+
788 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000789 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000790 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000793 SL->setString(C,Str,Kind,Pascal);
794
Chris Lattner2085fd62009-02-18 06:40:38 +0000795 SL->TokLocs[0] = Loc[0];
796 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000797
Chris Lattner726e1682009-02-18 05:49:11 +0000798 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000799 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
800 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000801}
802
Craig Topper05ed1a02013-08-18 10:09:15 +0000803StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
804 unsigned NumStrs) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000805 void *Mem = C.Allocate(sizeof(StringLiteral)+
806 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000807 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000808 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000809 SL->CharByteWidth = 0;
810 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000811 SL->NumConcatenated = NumStrs;
812 return SL;
813}
814
Alexander Kornienkoae541212013-02-01 12:35:51 +0000815void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieu8ab09da2012-06-13 20:25:24 +0000816 switch (getKind()) {
817 case Ascii: break; // no prefix.
818 case Wide: OS << 'L'; break;
819 case UTF8: OS << "u8"; break;
820 case UTF16: OS << 'u'; break;
821 case UTF32: OS << 'U'; break;
822 }
823 OS << '"';
824 static const char Hex[] = "0123456789ABCDEF";
825
826 unsigned LastSlashX = getLength();
827 for (unsigned I = 0, N = getLength(); I != N; ++I) {
828 switch (uint32_t Char = getCodeUnit(I)) {
829 default:
830 // FIXME: Convert UTF-8 back to codepoints before rendering.
831
832 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
833 // Leave invalid surrogates alone; we'll use \x for those.
834 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
835 Char <= 0xdbff) {
836 uint32_t Trail = getCodeUnit(I + 1);
837 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
838 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
839 ++I;
840 }
841 }
842
843 if (Char > 0xff) {
844 // If this is a wide string, output characters over 0xff using \x
845 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
846 // codepoint: use \x escapes for invalid codepoints.
847 if (getKind() == Wide ||
848 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
849 // FIXME: Is this the best way to print wchar_t?
850 OS << "\\x";
851 int Shift = 28;
852 while ((Char >> Shift) == 0)
853 Shift -= 4;
854 for (/**/; Shift >= 0; Shift -= 4)
855 OS << Hex[(Char >> Shift) & 15];
856 LastSlashX = I;
857 break;
858 }
859
860 if (Char > 0xffff)
861 OS << "\\U00"
862 << Hex[(Char >> 20) & 15]
863 << Hex[(Char >> 16) & 15];
864 else
865 OS << "\\u";
866 OS << Hex[(Char >> 12) & 15]
867 << Hex[(Char >> 8) & 15]
868 << Hex[(Char >> 4) & 15]
869 << Hex[(Char >> 0) & 15];
870 break;
871 }
872
873 // If we used \x... for the previous character, and this character is a
874 // hexadecimal digit, prevent it being slurped as part of the \x.
875 if (LastSlashX + 1 == I) {
876 switch (Char) {
877 case '0': case '1': case '2': case '3': case '4':
878 case '5': case '6': case '7': case '8': case '9':
879 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
880 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
881 OS << "\"\"";
882 }
883 }
884
885 assert(Char <= 0xff &&
886 "Characters above 0xff should already have been handled.");
887
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000888 if (isPrintable(Char))
Richard Trieu8ab09da2012-06-13 20:25:24 +0000889 OS << (char)Char;
890 else // Output anything hard as an octal escape.
891 OS << '\\'
892 << (char)('0' + ((Char >> 6) & 7))
893 << (char)('0' + ((Char >> 3) & 7))
894 << (char)('0' + ((Char >> 0) & 7));
895 break;
896 // Handle some common non-printable cases to make dumps prettier.
897 case '\\': OS << "\\\\"; break;
898 case '"': OS << "\\\""; break;
899 case '\n': OS << "\\n"; break;
900 case '\t': OS << "\\t"; break;
901 case '\a': OS << "\\a"; break;
902 case '\b': OS << "\\b"; break;
903 }
904 }
905 OS << '"';
906}
907
Craig Topper05ed1a02013-08-18 10:09:15 +0000908void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedman64f45a22011-11-01 02:23:42 +0000909 StringKind Kind, bool IsPascal) {
910 //FIXME: we assume that the string data comes from a target that uses the same
911 // code unit size and endianess for the type of string.
912 this->Kind = Kind;
913 this->IsPascal = IsPascal;
914
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000915 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000916 assert((Str.size()%CharByteWidth == 0)
917 && "size of data must be multiple of CharByteWidth");
918 Length = Str.size()/CharByteWidth;
919
920 switch(CharByteWidth) {
921 case 1: {
922 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000923 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000924 StrData.asChar = AStrData;
925 break;
926 }
927 case 2: {
928 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000929 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000930 StrData.asUInt16 = AStrData;
931 break;
932 }
933 case 4: {
934 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000935 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000936 StrData.asUInt32 = AStrData;
937 break;
938 }
939 default:
940 assert(false && "unsupported CharByteWidth");
941 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000942}
943
Chris Lattner08f92e32010-11-17 07:37:15 +0000944/// getLocationOfByte - Return a source location that points to the specified
945/// byte of this string literal.
946///
947/// Strings are amazingly complex. They can be formed from multiple tokens and
948/// can have escape sequences in them in addition to the usual trigraph and
949/// escaped newline business. This routine handles this complexity.
950///
951SourceLocation StringLiteral::
952getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
953 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000954 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
955 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000956
Chris Lattner08f92e32010-11-17 07:37:15 +0000957 // Loop over all of the tokens in this string until we find the one that
958 // contains the byte we're looking for.
959 unsigned TokNo = 0;
960 while (1) {
961 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
962 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
963
964 // Get the spelling of the string so that we can get the data that makes up
965 // the string literal, not the identifier for the macro it is potentially
966 // expanded through.
967 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
968
969 // Re-lex the token to get its length and original spelling.
970 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
971 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000972 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000973 if (Invalid)
974 return StrTokSpellingLoc;
975
976 const char *StrData = Buffer.data()+LocInfo.second;
977
Chris Lattner08f92e32010-11-17 07:37:15 +0000978 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000979 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
980 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000981 Token TheTok;
982 TheLexer.LexFromRawLexer(TheTok);
983
984 // Use the StringLiteralParser to compute the length of the string in bytes.
985 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
986 unsigned TokNumBytes = SLP.GetStringLength();
987
988 // If the byte is in this token, return the location of the byte.
989 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000990 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000991 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
992
993 // Now that we know the offset of the token in the spelling, use the
994 // preprocessor to get the offset in the original source.
995 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
996 }
997
998 // Move to the next string token.
999 ++TokNo;
1000 ByteNo -= TokNumBytes;
1001 }
1002}
1003
1004
1005
Reid Spencer5f016e22007-07-11 17:01:13 +00001006/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1007/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie0bea8632012-10-08 01:11:04 +00001008StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001010 case UO_PostInc: return "++";
1011 case UO_PostDec: return "--";
1012 case UO_PreInc: return "++";
1013 case UO_PreDec: return "--";
1014 case UO_AddrOf: return "&";
1015 case UO_Deref: return "*";
1016 case UO_Plus: return "+";
1017 case UO_Minus: return "-";
1018 case UO_Not: return "~";
1019 case UO_LNot: return "!";
1020 case UO_Real: return "__real";
1021 case UO_Imag: return "__imag";
1022 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001024 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +00001025}
1026
John McCall2de56d12010-08-25 11:45:40 +00001027UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001028UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1029 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001030 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +00001031 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1032 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1033 case OO_Amp: return UO_AddrOf;
1034 case OO_Star: return UO_Deref;
1035 case OO_Plus: return UO_Plus;
1036 case OO_Minus: return UO_Minus;
1037 case OO_Tilde: return UO_Not;
1038 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001039 }
1040}
1041
1042OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1043 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +00001044 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1045 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1046 case UO_AddrOf: return OO_Amp;
1047 case UO_Deref: return OO_Star;
1048 case UO_Plus: return OO_Plus;
1049 case UO_Minus: return OO_Minus;
1050 case UO_Not: return OO_Tilde;
1051 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001052 default: return OO_None;
1053 }
1054}
1055
1056
Reid Spencer5f016e22007-07-11 17:01:13 +00001057//===----------------------------------------------------------------------===//
1058// Postfix Operators.
1059//===----------------------------------------------------------------------===//
1060
Craig Topper05ed1a02013-08-18 10:09:15 +00001061CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1062 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1063 ExprValueKind VK, SourceLocation rparenloc)
John McCallf89e55a2010-11-18 06:31:45 +00001064 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001065 fn->isTypeDependent(),
1066 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001067 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001068 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001069 NumArgs(args.size()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001071 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +00001072 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001073 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001074 if (args[i]->isTypeDependent())
1075 ExprBits.TypeDependent = true;
1076 if (args[i]->isValueDependent())
1077 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001078 if (args[i]->isInstantiationDependent())
1079 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001080 if (args[i]->containsUnexpandedParameterPack())
1081 ExprBits.ContainsUnexpandedParameterPack = true;
1082
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001083 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001084 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001085
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001086 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +00001087 RParenLoc = rparenloc;
1088}
Nate Begemane2ce1d92008-01-17 17:46:27 +00001089
Craig Topper05ed1a02013-08-18 10:09:15 +00001090CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCallf89e55a2010-11-18 06:31:45 +00001091 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1092 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001093 fn->isTypeDependent(),
1094 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001095 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001096 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001097 NumArgs(args.size()) {
Ted Kremenek668bf912009-02-09 20:51:47 +00001098
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001099 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001100 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001101 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001102 if (args[i]->isTypeDependent())
1103 ExprBits.TypeDependent = true;
1104 if (args[i]->isValueDependent())
1105 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001106 if (args[i]->isInstantiationDependent())
1107 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001108 if (args[i]->containsUnexpandedParameterPack())
1109 ExprBits.ContainsUnexpandedParameterPack = true;
1110
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001111 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001112 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001113
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001114 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 RParenLoc = rparenloc;
1116}
1117
Craig Topper05ed1a02013-08-18 10:09:15 +00001118CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Mike Stump1eb44332009-09-09 15:08:12 +00001119 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001120 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001121 SubExprs = new (C) Stmt*[PREARGS_START];
1122 CallExprBits.NumPreArgs = 0;
1123}
1124
Craig Topper05ed1a02013-08-18 10:09:15 +00001125CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001126 EmptyShell Empty)
1127 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1128 // FIXME: Why do we allocate this?
1129 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1130 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001131}
1132
Nuno Lopesd20254f2009-12-20 23:11:08 +00001133Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +00001134 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +00001135
1136 while (SubstNonTypeTemplateParmExpr *NTTP
1137 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1138 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1139 }
1140
Sebastian Redl20012152010-09-10 20:55:30 +00001141 // If we're calling a dereference, look at the pointer instead.
1142 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1143 if (BO->isPtrMemOp())
1144 CEE = BO->getRHS()->IgnoreParenCasts();
1145 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1146 if (UO->getOpcode() == UO_Deref)
1147 CEE = UO->getSubExpr()->IgnoreParenCasts();
1148 }
Chris Lattner6346f962009-07-17 15:46:27 +00001149 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001150 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001151 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1152 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001153
1154 return 0;
1155}
1156
Nuno Lopesd20254f2009-12-20 23:11:08 +00001157FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001158 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001159}
1160
Chris Lattnerd18b3292007-12-28 05:25:02 +00001161/// setNumArgs - This changes the number of arguments present in this call.
1162/// Any orphaned expressions are deleted by this, and any new operands are set
1163/// to null.
Craig Topper05ed1a02013-08-18 10:09:15 +00001164void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001165 // No change, just return.
1166 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Chris Lattnerd18b3292007-12-28 05:25:02 +00001168 // If shrinking # arguments, just delete the extras and forgot them.
1169 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001170 this->NumArgs = NumArgs;
1171 return;
1172 }
1173
1174 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001175 unsigned NumPreArgs = getNumPreArgs();
1176 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001177 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001178 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001179 NewSubExprs[i] = SubExprs[i];
1180 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001181 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1182 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001183 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Douglas Gregor88c9a462009-04-17 21:46:47 +00001185 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001186 SubExprs = NewSubExprs;
1187 this->NumArgs = NumArgs;
1188}
1189
Chris Lattnercb888962008-10-06 05:00:53 +00001190/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1191/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001192unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001193 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001194 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001195 // ImplicitCastExpr.
1196 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1197 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001198 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001200 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1201 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001202 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Anders Carlssonbcba2012008-01-31 02:13:57 +00001204 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1205 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001206 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001208 if (!FDecl->getIdentifier())
1209 return 0;
1210
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001211 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001212}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001213
Richard Smithba571832013-01-17 23:46:04 +00001214bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1215 if (unsigned BI = isBuiltinCall())
1216 return Ctx.BuiltinInfo.isUnevaluated(BI);
1217 return false;
1218}
1219
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001220QualType CallExpr::getCallReturnType() const {
1221 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001222 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001223 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001224 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001225 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001226 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1227 // This should never be overloaded and so should never return null.
1228 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001229
John McCall864c0412011-04-26 20:42:42 +00001230 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001231 return FnType->getResultType();
1232}
Chris Lattnercb888962008-10-06 05:00:53 +00001233
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001234SourceLocation CallExpr::getLocStart() const {
1235 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001236 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001237
1238 SourceLocation begin = getCallee()->getLocStart();
1239 if (begin.isInvalid() && getNumArgs() > 0)
1240 begin = getArg(0)->getLocStart();
1241 return begin;
1242}
1243SourceLocation CallExpr::getLocEnd() const {
1244 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001245 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001246
1247 SourceLocation end = getRParenLoc();
1248 if (end.isInvalid() && getNumArgs() > 0)
1249 end = getArg(getNumArgs() - 1)->getLocEnd();
1250 return end;
1251}
John McCall2882eca2011-02-21 06:23:05 +00001252
Craig Topper05ed1a02013-08-18 10:09:15 +00001253OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001254 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001255 TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001256 ArrayRef<OffsetOfNode> comps,
1257 ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001258 SourceLocation RParenLoc) {
1259 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001260 sizeof(OffsetOfNode) * comps.size() +
1261 sizeof(Expr*) * exprs.size());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001262
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001263 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1264 RParenLoc);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001265}
1266
Craig Topper05ed1a02013-08-18 10:09:15 +00001267OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001268 unsigned numComps, unsigned numExprs) {
1269 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1270 sizeof(OffsetOfNode) * numComps +
1271 sizeof(Expr*) * numExprs);
1272 return new (Mem) OffsetOfExpr(numComps, numExprs);
1273}
1274
Craig Topper05ed1a02013-08-18 10:09:15 +00001275OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001276 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001277 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001278 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001279 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1280 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001281 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001282 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001283 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001284 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001285 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001286{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001287 for (unsigned i = 0; i != comps.size(); ++i) {
1288 setComponent(i, comps[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001289 }
Sean Huntc3021132010-05-05 15:23:54 +00001290
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001291 for (unsigned i = 0; i != exprs.size(); ++i) {
1292 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001293 ExprBits.ValueDependent = true;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001294 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001295 ExprBits.ContainsUnexpandedParameterPack = true;
1296
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001297 setIndexExpr(i, exprs[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001298 }
1299}
1300
1301IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1302 assert(getKind() == Field || getKind() == Identifier);
1303 if (getKind() == Field)
1304 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001305
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001306 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1307}
1308
Craig Topper05ed1a02013-08-18 10:09:15 +00001309MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001310 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001311 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001312 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001313 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001314 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001315 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001316 QualType ty,
1317 ExprValueKind vk,
1318 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001319 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001320
Douglas Gregor40d96a62011-02-28 21:54:11 +00001321 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001322 founddecl.getDecl() != memberdecl ||
1323 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001324 if (hasQualOrFound)
1325 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001326
John McCalld5532b62009-11-23 01:53:49 +00001327 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001328 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1329 else if (TemplateKWLoc.isValid())
1330 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Chris Lattner32488542010-10-30 05:14:06 +00001332 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001333 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1334 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001335
1336 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001337 // FIXME: Wrong. We should be looking at the member declaration we found.
1338 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001339 E->setValueDependent(true);
1340 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001341 E->setInstantiationDependent(true);
1342 }
1343 else if (QualifierLoc &&
1344 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1345 E->setInstantiationDependent(true);
1346
John McCall6bb80172010-03-30 21:47:33 +00001347 E->HasQualifierOrFoundDecl = true;
1348
1349 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001350 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001351 NQ->FoundDecl = founddecl;
1352 }
1353
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001354 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1355
John McCall6bb80172010-03-30 21:47:33 +00001356 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001357 bool Dependent = false;
1358 bool InstantiationDependent = false;
1359 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001360 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1361 Dependent,
1362 InstantiationDependent,
1363 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001364 if (InstantiationDependent)
1365 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001366 } else if (TemplateKWLoc.isValid()) {
1367 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001368 }
1369
1370 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001371}
1372
Daniel Dunbar396ec672012-03-09 15:39:15 +00001373SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001374 if (isImplicitAccess()) {
1375 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001376 return getQualifierLoc().getBeginLoc();
1377 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001378 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001379
Daniel Dunbar396ec672012-03-09 15:39:15 +00001380 // FIXME: We don't want this to happen. Rather, we should be able to
1381 // detect all kinds of implicit accesses more cleanly.
1382 SourceLocation BaseStartLoc = getBase()->getLocStart();
1383 if (BaseStartLoc.isValid())
1384 return BaseStartLoc;
1385 return MemberLoc;
1386}
1387SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001388 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbar396ec672012-03-09 15:39:15 +00001389 if (hasExplicitTemplateArgs())
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001390 EndLoc = getRAngleLoc();
1391 else if (EndLoc.isInvalid())
1392 EndLoc = getBase()->getLocEnd();
1393 return EndLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001394}
1395
John McCall1d9b3b22011-09-09 05:25:32 +00001396void CastExpr::CheckCastConsistency() const {
1397 switch (getCastKind()) {
1398 case CK_DerivedToBase:
1399 case CK_UncheckedDerivedToBase:
1400 case CK_DerivedToBaseMemberPointer:
1401 case CK_BaseToDerived:
1402 case CK_BaseToDerivedMemberPointer:
1403 assert(!path_empty() && "Cast kind should have a base path!");
1404 break;
1405
1406 case CK_CPointerToObjCPointerCast:
1407 assert(getType()->isObjCObjectPointerType());
1408 assert(getSubExpr()->getType()->isPointerType());
1409 goto CheckNoBasePath;
1410
1411 case CK_BlockPointerToObjCPointerCast:
1412 assert(getType()->isObjCObjectPointerType());
1413 assert(getSubExpr()->getType()->isBlockPointerType());
1414 goto CheckNoBasePath;
1415
John McCall4d4e5c12012-02-15 01:22:51 +00001416 case CK_ReinterpretMemberPointer:
1417 assert(getType()->isMemberPointerType());
1418 assert(getSubExpr()->getType()->isMemberPointerType());
1419 goto CheckNoBasePath;
1420
John McCall1d9b3b22011-09-09 05:25:32 +00001421 case CK_BitCast:
1422 // Arbitrary casts to C pointer types count as bitcasts.
1423 // Otherwise, we should only have block and ObjC pointer casts
1424 // here if they stay within the type kind.
1425 if (!getType()->isPointerType()) {
1426 assert(getType()->isObjCObjectPointerType() ==
1427 getSubExpr()->getType()->isObjCObjectPointerType());
1428 assert(getType()->isBlockPointerType() ==
1429 getSubExpr()->getType()->isBlockPointerType());
1430 }
1431 goto CheckNoBasePath;
1432
1433 case CK_AnyPointerToBlockPointerCast:
1434 assert(getType()->isBlockPointerType());
1435 assert(getSubExpr()->getType()->isAnyPointerType() &&
1436 !getSubExpr()->getType()->isBlockPointerType());
1437 goto CheckNoBasePath;
1438
Douglas Gregorac1303e2012-02-22 05:02:47 +00001439 case CK_CopyAndAutoreleaseBlockObject:
1440 assert(getType()->isBlockPointerType());
1441 assert(getSubExpr()->getType()->isBlockPointerType());
1442 goto CheckNoBasePath;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001443
1444 case CK_FunctionToPointerDecay:
1445 assert(getType()->isPointerType());
1446 assert(getSubExpr()->getType()->isFunctionType());
1447 goto CheckNoBasePath;
1448
John McCall1d9b3b22011-09-09 05:25:32 +00001449 // These should not have an inheritance path.
1450 case CK_Dynamic:
1451 case CK_ToUnion:
1452 case CK_ArrayToPointerDecay:
John McCall1d9b3b22011-09-09 05:25:32 +00001453 case CK_NullToMemberPointer:
1454 case CK_NullToPointer:
1455 case CK_ConstructorConversion:
1456 case CK_IntegralToPointer:
1457 case CK_PointerToIntegral:
1458 case CK_ToVoid:
1459 case CK_VectorSplat:
1460 case CK_IntegralCast:
1461 case CK_IntegralToFloating:
1462 case CK_FloatingToIntegral:
1463 case CK_FloatingCast:
1464 case CK_ObjCObjectLValueCast:
1465 case CK_FloatingRealToComplex:
1466 case CK_FloatingComplexToReal:
1467 case CK_FloatingComplexCast:
1468 case CK_FloatingComplexToIntegralComplex:
1469 case CK_IntegralRealToComplex:
1470 case CK_IntegralComplexToReal:
1471 case CK_IntegralComplexCast:
1472 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001473 case CK_ARCProduceObject:
1474 case CK_ARCConsumeObject:
1475 case CK_ARCReclaimReturnedObject:
1476 case CK_ARCExtendBlockObject:
Guy Benyeie6b9d802013-01-20 12:31:11 +00001477 case CK_ZeroToOCLEvent:
John McCall1d9b3b22011-09-09 05:25:32 +00001478 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1479 goto CheckNoBasePath;
1480
1481 case CK_Dependent:
1482 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001483 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001484 case CK_AtomicToNonAtomic:
1485 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001486 case CK_PointerToBoolean:
1487 case CK_IntegralToBoolean:
1488 case CK_FloatingToBoolean:
1489 case CK_MemberPointerToBoolean:
1490 case CK_FloatingComplexToBoolean:
1491 case CK_IntegralComplexToBoolean:
1492 case CK_LValueBitCast: // -> bool&
1493 case CK_UserDefinedConversion: // operator bool()
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001494 case CK_BuiltinFnToFnPtr:
John McCall1d9b3b22011-09-09 05:25:32 +00001495 CheckNoBasePath:
1496 assert(path_empty() && "Cast kind should not have a base path!");
1497 break;
1498 }
1499}
1500
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001501const char *CastExpr::getCastKindName() const {
1502 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001503 case CK_Dependent:
1504 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001505 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001506 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001507 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001508 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001509 case CK_LValueToRValue:
1510 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001511 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001512 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001513 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001514 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001515 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001516 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001517 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001518 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001519 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001520 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001521 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001522 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001523 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001524 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001525 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001526 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001527 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001528 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001529 case CK_NullToPointer:
1530 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001531 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001532 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001533 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001534 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001535 case CK_ReinterpretMemberPointer:
1536 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001537 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001538 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001539 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001540 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001541 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001542 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001543 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001544 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001545 case CK_PointerToBoolean:
1546 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001547 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001548 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001549 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001550 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001551 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001552 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001553 case CK_IntegralToBoolean:
1554 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001555 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001556 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001557 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001558 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001559 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001560 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001561 case CK_FloatingToBoolean:
1562 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001563 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001564 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001565 case CK_CPointerToObjCPointerCast:
1566 return "CPointerToObjCPointerCast";
1567 case CK_BlockPointerToObjCPointerCast:
1568 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001569 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001570 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001571 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001572 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001573 case CK_FloatingRealToComplex:
1574 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001575 case CK_FloatingComplexToReal:
1576 return "FloatingComplexToReal";
1577 case CK_FloatingComplexToBoolean:
1578 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001579 case CK_FloatingComplexCast:
1580 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001581 case CK_FloatingComplexToIntegralComplex:
1582 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001583 case CK_IntegralRealToComplex:
1584 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001585 case CK_IntegralComplexToReal:
1586 return "IntegralComplexToReal";
1587 case CK_IntegralComplexToBoolean:
1588 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001589 case CK_IntegralComplexCast:
1590 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001591 case CK_IntegralComplexToFloatingComplex:
1592 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001593 case CK_ARCConsumeObject:
1594 return "ARCConsumeObject";
1595 case CK_ARCProduceObject:
1596 return "ARCProduceObject";
1597 case CK_ARCReclaimReturnedObject:
1598 return "ARCReclaimReturnedObject";
1599 case CK_ARCExtendBlockObject:
1600 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001601 case CK_AtomicToNonAtomic:
1602 return "AtomicToNonAtomic";
1603 case CK_NonAtomicToAtomic:
1604 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001605 case CK_CopyAndAutoreleaseBlockObject:
1606 return "CopyAndAutoreleaseBlockObject";
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001607 case CK_BuiltinFnToFnPtr:
1608 return "BuiltinFnToFnPtr";
Guy Benyeie6b9d802013-01-20 12:31:11 +00001609 case CK_ZeroToOCLEvent:
1610 return "ZeroToOCLEvent";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001611 }
Mike Stump1eb44332009-09-09 15:08:12 +00001612
John McCall2bb5d002010-11-13 09:02:35 +00001613 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001614}
1615
Douglas Gregor6eef5192009-12-14 19:27:10 +00001616Expr *CastExpr::getSubExprAsWritten() {
1617 Expr *SubExpr = 0;
1618 CastExpr *E = this;
1619 do {
1620 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001621
1622 // Skip through reference binding to temporary.
1623 if (MaterializeTemporaryExpr *Materialize
1624 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1625 SubExpr = Materialize->GetTemporaryExpr();
1626
Douglas Gregor6eef5192009-12-14 19:27:10 +00001627 // Skip any temporary bindings; they're implicit.
1628 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1629 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001630
Douglas Gregor6eef5192009-12-14 19:27:10 +00001631 // Conversions by constructor and conversion functions have a
1632 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001633 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001634 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001635 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001636 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001637
Douglas Gregor6eef5192009-12-14 19:27:10 +00001638 // If the subexpression we're left with is an implicit cast, look
1639 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001640 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1641
Douglas Gregor6eef5192009-12-14 19:27:10 +00001642 return SubExpr;
1643}
1644
John McCallf871d0c2010-08-07 06:22:56 +00001645CXXBaseSpecifier **CastExpr::path_buffer() {
1646 switch (getStmtClass()) {
1647#define ABSTRACT_STMT(x)
1648#define CASTEXPR(Type, Base) \
1649 case Stmt::Type##Class: \
1650 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1651#define STMT(Type, Base)
1652#include "clang/AST/StmtNodes.inc"
1653 default:
1654 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001655 }
1656}
1657
1658void CastExpr::setCastPath(const CXXCastPath &Path) {
1659 assert(Path.size() == path_size());
1660 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1661}
1662
Craig Topper05ed1a02013-08-18 10:09:15 +00001663ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallf871d0c2010-08-07 06:22:56 +00001664 CastKind Kind, Expr *Operand,
1665 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001666 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001667 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1668 void *Buffer =
1669 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1670 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001671 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001672 if (PathSize) E->setCastPath(*BasePath);
1673 return E;
1674}
1675
Craig Topper05ed1a02013-08-18 10:09:15 +00001676ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallf871d0c2010-08-07 06:22:56 +00001677 unsigned PathSize) {
1678 void *Buffer =
1679 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1680 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1681}
1682
1683
Craig Topper05ed1a02013-08-18 10:09:15 +00001684CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001685 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001686 const CXXCastPath *BasePath,
1687 TypeSourceInfo *WrittenTy,
1688 SourceLocation L, SourceLocation R) {
1689 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1690 void *Buffer =
1691 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1692 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001693 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001694 if (PathSize) E->setCastPath(*BasePath);
1695 return E;
1696}
1697
Craig Topper05ed1a02013-08-18 10:09:15 +00001698CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1699 unsigned PathSize) {
John McCallf871d0c2010-08-07 06:22:56 +00001700 void *Buffer =
1701 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1702 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1703}
1704
Reid Spencer5f016e22007-07-11 17:01:13 +00001705/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1706/// corresponds to, e.g. "<<=".
David Blaikie0bea8632012-10-08 01:11:04 +00001707StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001709 case BO_PtrMemD: return ".*";
1710 case BO_PtrMemI: return "->*";
1711 case BO_Mul: return "*";
1712 case BO_Div: return "/";
1713 case BO_Rem: return "%";
1714 case BO_Add: return "+";
1715 case BO_Sub: return "-";
1716 case BO_Shl: return "<<";
1717 case BO_Shr: return ">>";
1718 case BO_LT: return "<";
1719 case BO_GT: return ">";
1720 case BO_LE: return "<=";
1721 case BO_GE: return ">=";
1722 case BO_EQ: return "==";
1723 case BO_NE: return "!=";
1724 case BO_And: return "&";
1725 case BO_Xor: return "^";
1726 case BO_Or: return "|";
1727 case BO_LAnd: return "&&";
1728 case BO_LOr: return "||";
1729 case BO_Assign: return "=";
1730 case BO_MulAssign: return "*=";
1731 case BO_DivAssign: return "/=";
1732 case BO_RemAssign: return "%=";
1733 case BO_AddAssign: return "+=";
1734 case BO_SubAssign: return "-=";
1735 case BO_ShlAssign: return "<<=";
1736 case BO_ShrAssign: return ">>=";
1737 case BO_AndAssign: return "&=";
1738 case BO_XorAssign: return "^=";
1739 case BO_OrAssign: return "|=";
1740 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001741 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001742
David Blaikie30263482012-01-20 21:50:17 +00001743 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001744}
1745
John McCall2de56d12010-08-25 11:45:40 +00001746BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001747BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1748 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001749 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001750 case OO_Plus: return BO_Add;
1751 case OO_Minus: return BO_Sub;
1752 case OO_Star: return BO_Mul;
1753 case OO_Slash: return BO_Div;
1754 case OO_Percent: return BO_Rem;
1755 case OO_Caret: return BO_Xor;
1756 case OO_Amp: return BO_And;
1757 case OO_Pipe: return BO_Or;
1758 case OO_Equal: return BO_Assign;
1759 case OO_Less: return BO_LT;
1760 case OO_Greater: return BO_GT;
1761 case OO_PlusEqual: return BO_AddAssign;
1762 case OO_MinusEqual: return BO_SubAssign;
1763 case OO_StarEqual: return BO_MulAssign;
1764 case OO_SlashEqual: return BO_DivAssign;
1765 case OO_PercentEqual: return BO_RemAssign;
1766 case OO_CaretEqual: return BO_XorAssign;
1767 case OO_AmpEqual: return BO_AndAssign;
1768 case OO_PipeEqual: return BO_OrAssign;
1769 case OO_LessLess: return BO_Shl;
1770 case OO_GreaterGreater: return BO_Shr;
1771 case OO_LessLessEqual: return BO_ShlAssign;
1772 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1773 case OO_EqualEqual: return BO_EQ;
1774 case OO_ExclaimEqual: return BO_NE;
1775 case OO_LessEqual: return BO_LE;
1776 case OO_GreaterEqual: return BO_GE;
1777 case OO_AmpAmp: return BO_LAnd;
1778 case OO_PipePipe: return BO_LOr;
1779 case OO_Comma: return BO_Comma;
1780 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001781 }
1782}
1783
1784OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1785 static const OverloadedOperatorKind OverOps[] = {
1786 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1787 OO_Star, OO_Slash, OO_Percent,
1788 OO_Plus, OO_Minus,
1789 OO_LessLess, OO_GreaterGreater,
1790 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1791 OO_EqualEqual, OO_ExclaimEqual,
1792 OO_Amp,
1793 OO_Caret,
1794 OO_Pipe,
1795 OO_AmpAmp,
1796 OO_PipePipe,
1797 OO_Equal, OO_StarEqual,
1798 OO_SlashEqual, OO_PercentEqual,
1799 OO_PlusEqual, OO_MinusEqual,
1800 OO_LessLessEqual, OO_GreaterGreaterEqual,
1801 OO_AmpEqual, OO_CaretEqual,
1802 OO_PipeEqual,
1803 OO_Comma
1804 };
1805 return OverOps[Opc];
1806}
1807
Craig Topper05ed1a02013-08-18 10:09:15 +00001808InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001809 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001810 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001811 false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001812 InitExprs(C, initExprs.size()),
Abramo Bagnara23700f02012-11-08 18:41:43 +00001813 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001814{
1815 sawArrayRangeDesignator(false);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001816 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001817 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001818 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001819 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001820 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001821 if (initExprs[I]->isInstantiationDependent())
1822 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001823 if (initExprs[I]->containsUnexpandedParameterPack())
1824 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001825 }
Sean Huntc3021132010-05-05 15:23:54 +00001826
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001827 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001828}
Reid Spencer5f016e22007-07-11 17:01:13 +00001829
Craig Topper05ed1a02013-08-18 10:09:15 +00001830void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001831 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001832 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001833}
1834
Craig Topper05ed1a02013-08-18 10:09:15 +00001835void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001836 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001837}
1838
Craig Topper05ed1a02013-08-18 10:09:15 +00001839Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001840 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001841 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001842 InitExprs.back() = expr;
1843 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001844 }
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Douglas Gregor4c678342009-01-28 21:54:33 +00001846 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1847 InitExprs[Init] = expr;
1848 return Result;
1849}
1850
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001851void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001852 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001853 ArrayFillerOrUnionFieldInit = filler;
1854 // Fill out any "holes" in the array due to designated initializers.
1855 Expr **inits = getInits();
1856 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1857 if (inits[i] == 0)
1858 inits[i] = filler;
1859}
1860
Richard Smithfe587202012-04-15 02:50:59 +00001861bool InitListExpr::isStringLiteralInit() const {
1862 if (getNumInits() != 1)
1863 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001864 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1865 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smithfe587202012-04-15 02:50:59 +00001866 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001867 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smithfe587202012-04-15 02:50:59 +00001868 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1869}
1870
Erik Verbruggen65d78312012-12-25 14:51:39 +00001871SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara23700f02012-11-08 18:41:43 +00001872 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen65d78312012-12-25 14:51:39 +00001873 return SyntacticForm->getLocStart();
1874 SourceLocation Beg = LBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001875 if (Beg.isInvalid()) {
1876 // Find the first non-null initializer.
1877 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1878 E = InitExprs.end();
1879 I != E; ++I) {
1880 if (Stmt *S = *I) {
1881 Beg = S->getLocStart();
1882 break;
1883 }
1884 }
1885 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001886 return Beg;
1887}
1888
1889SourceLocation InitListExpr::getLocEnd() const {
1890 if (InitListExpr *SyntacticForm = getSyntacticForm())
1891 return SyntacticForm->getLocEnd();
1892 SourceLocation End = RBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001893 if (End.isInvalid()) {
1894 // Find the first non-null initializer from the end.
1895 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen65d78312012-12-25 14:51:39 +00001896 E = InitExprs.rend();
1897 I != E; ++I) {
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001898 if (Stmt *S = *I) {
Erik Verbruggen65d78312012-12-25 14:51:39 +00001899 End = S->getLocEnd();
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001900 break;
Erik Verbruggen65d78312012-12-25 14:51:39 +00001901 }
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001902 }
1903 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001904 return End;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001905}
1906
Steve Naroffbfdcae62008-09-04 15:31:07 +00001907/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001908///
John McCalla345edb2012-02-17 03:32:35 +00001909const FunctionProtoType *BlockExpr::getFunctionType() const {
1910 // The block pointer is never sugared, but the function type might be.
1911 return cast<BlockPointerType>(getType())
1912 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001913}
1914
Mike Stump1eb44332009-09-09 15:08:12 +00001915SourceLocation BlockExpr::getCaretLocation() const {
1916 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001917}
Mike Stump1eb44332009-09-09 15:08:12 +00001918const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001919 return TheBlock->getBody();
1920}
Mike Stump1eb44332009-09-09 15:08:12 +00001921Stmt *BlockExpr::getBody() {
1922 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001923}
Steve Naroff56ee6892008-10-08 17:01:13 +00001924
1925
Reid Spencer5f016e22007-07-11 17:01:13 +00001926//===----------------------------------------------------------------------===//
1927// Generic Expression Routines
1928//===----------------------------------------------------------------------===//
1929
Chris Lattner026dc962009-02-14 07:37:35 +00001930/// isUnusedResultAWarning - Return true if this immediate expression should
1931/// be warned about if the result is unused. If so, fill in Loc and Ranges
1932/// with location to warn on and the source range[s] to report with the
1933/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001934bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1935 SourceRange &R1, SourceRange &R2,
1936 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001937 // Don't warn if the expr is type dependent. The type could end up
1938 // instantiating to void.
1939 if (isTypeDependent())
1940 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Reid Spencer5f016e22007-07-11 17:01:13 +00001942 switch (getStmtClass()) {
1943 default:
John McCall0faede62010-03-12 07:11:26 +00001944 if (getType()->isVoidType())
1945 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001946 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001947 Loc = getExprLoc();
1948 R1 = getSourceRange();
1949 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001951 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001952 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001953 case GenericSelectionExprClass:
1954 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001955 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmana5e66012013-07-20 00:40:58 +00001956 case ChooseExprClass:
1957 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1958 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001959 case UnaryOperatorClass: {
1960 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001963 case UO_Plus:
1964 case UO_Minus:
1965 case UO_AddrOf:
1966 case UO_Not:
1967 case UO_LNot:
1968 case UO_Deref:
1969 break;
John McCall2de56d12010-08-25 11:45:40 +00001970 case UO_PostInc:
1971 case UO_PostDec:
1972 case UO_PreInc:
1973 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001974 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001975 case UO_Real:
1976 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001978 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1979 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001980 return false;
1981 break;
John McCall2de56d12010-08-25 11:45:40 +00001982 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001983 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 }
Eli Friedmana6115062012-05-24 00:47:05 +00001985 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001986 Loc = UO->getOperatorLoc();
1987 R1 = UO->getSubExpr()->getSourceRange();
1988 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001989 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001990 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001991 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001992 switch (BO->getOpcode()) {
1993 default:
1994 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001995 // Consider the RHS of comma for side effects. LHS was checked by
1996 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001997 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001998 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1999 // lvalue-ness) of an assignment written in a macro.
2000 if (IntegerLiteral *IE =
2001 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2002 if (IE->getValue() == 0)
2003 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002004 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00002005 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00002006 case BO_LAnd:
2007 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00002008 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2009 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00002010 return false;
2011 break;
John McCallbf0ee352010-02-16 04:10:53 +00002012 }
Chris Lattner026dc962009-02-14 07:37:35 +00002013 if (BO->isAssignmentOp())
2014 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002015 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002016 Loc = BO->getOperatorLoc();
2017 R1 = BO->getLHS()->getSourceRange();
2018 R2 = BO->getRHS()->getSourceRange();
2019 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00002020 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00002021 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00002022 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002023 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002024 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002025
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002026 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002027 // If only one of the LHS or RHS is a warning, the operator might
2028 // be being used for control flow. Only warn if both the LHS and
2029 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002030 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002031 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002032 return false;
2033 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00002034 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00002035 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002036 }
2037
Reid Spencer5f016e22007-07-11 17:01:13 +00002038 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002039 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002040 Loc = cast<MemberExpr>(this)->getMemberLoc();
2041 R1 = SourceRange(Loc, Loc);
2042 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2043 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Reid Spencer5f016e22007-07-11 17:01:13 +00002045 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002046 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002047 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2048 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2049 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2050 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00002051
Chandler Carruth9b106832011-08-17 09:49:44 +00002052 case CXXOperatorCallExprClass: {
2053 // We warn about operator== and operator!= even when user-defined operator
2054 // overloads as there is no reasonable way to define these such that they
2055 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2056 // warning: these operators are commonly typo'ed, and so warning on them
2057 // provides additional value as well. If this list is updated,
2058 // DiagnoseUnusedComparison should be as well.
2059 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2060 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002061 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00002062 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002063 Loc = Op->getOperatorLoc();
2064 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00002065 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002066 }
Chandler Carruth9b106832011-08-17 09:49:44 +00002067
2068 // Fallthrough for generic call handling.
2069 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002070 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002071 case CXXMemberCallExprClass:
2072 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002073 // If this is a direct call, get the callee.
2074 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00002075 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00002076 // If the callee has attribute pure, const, or warn_unused_result, warn
2077 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002078 //
2079 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2080 // updated to match for QoI.
2081 if (FD->getAttr<WarnUnusedResultAttr>() ||
2082 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002083 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002084 Loc = CE->getCallee()->getLocStart();
2085 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002086
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002087 if (unsigned NumArgs = CE->getNumArgs())
2088 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2089 CE->getArg(NumArgs-1)->getLocEnd());
2090 return true;
2091 }
Chris Lattner026dc962009-02-14 07:37:35 +00002092 }
2093 return false;
2094 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002095
Matt Beaumont-Gay84c3b972012-10-23 06:15:26 +00002096 // If we don't know precisely what we're looking at, let's not warn.
2097 case UnresolvedLookupExprClass:
2098 case CXXUnresolvedConstructExprClass:
2099 return false;
2100
Anders Carlsson58beed92009-11-17 17:11:23 +00002101 case CXXTemporaryObjectExprClass:
Lubos Lunak81e45492013-07-21 13:15:58 +00002102 case CXXConstructExprClass: {
2103 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2104 if (Type->hasAttr<WarnUnusedAttr>()) {
2105 WarnE = this;
2106 Loc = getLocStart();
2107 R1 = getSourceRange();
2108 return true;
2109 }
2110 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002111 return false;
Lubos Lunak81e45492013-07-21 13:15:58 +00002112 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002113
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002114 case ObjCMessageExprClass: {
2115 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00002116 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002117 ME->isInstanceMessage() &&
2118 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas4bdb6022013-07-19 20:25:56 +00002119 ME->getMethodFamily() == OMF_init) {
Eli Friedmana6115062012-05-24 00:47:05 +00002120 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00002121 Loc = getExprLoc();
2122 R1 = ME->getSourceRange();
2123 return true;
2124 }
2125
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002126 const ObjCMethodDecl *MD = ME->getMethodDecl();
2127 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002128 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002129 Loc = getExprLoc();
2130 return true;
2131 }
Chris Lattner026dc962009-02-14 07:37:35 +00002132 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002133 }
Mike Stump1eb44332009-09-09 15:08:12 +00002134
John McCall12f78a62010-12-02 01:19:52 +00002135 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002136 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00002137 Loc = getExprLoc();
2138 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00002139 return true;
John McCall12f78a62010-12-02 01:19:52 +00002140
John McCall4b9c2d22011-11-06 09:01:30 +00002141 case PseudoObjectExprClass: {
2142 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2143
2144 // Only complain about things that have the form of a getter.
2145 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2146 isa<BinaryOperator>(PO->getSyntacticForm()))
2147 return false;
2148
Eli Friedmana6115062012-05-24 00:47:05 +00002149 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00002150 Loc = getExprLoc();
2151 R1 = getSourceRange();
2152 return true;
2153 }
2154
Chris Lattner611b2ec2008-07-26 19:51:01 +00002155 case StmtExprClass: {
2156 // Statement exprs don't logically have side effects themselves, but are
2157 // sometimes used in macros in ways that give them a type that is unused.
2158 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2159 // however, if the result of the stmt expr is dead, we don't want to emit a
2160 // warning.
2161 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002162 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002163 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002164 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002165 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2166 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002167 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002168 }
Mike Stump1eb44332009-09-09 15:08:12 +00002169
John McCall0faede62010-03-12 07:11:26 +00002170 if (getType()->isVoidType())
2171 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002172 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002173 Loc = cast<StmtExpr>(this)->getLParenLoc();
2174 R1 = getSourceRange();
2175 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002176 }
Eli Friedman63199172012-09-24 23:02:26 +00002177 case CXXFunctionalCastExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002178 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002179 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002180 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002181 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002182 if (CE->getCastKind() == CK_ToVoid) {
2183 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002184 CE->getSubExpr()->getType().isVolatileQualified()) {
2185 const DeclRefExpr *DRE =
2186 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2187 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2188 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2189 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2190 R1, R2, Ctx);
2191 }
2192 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002193 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002194 }
Eli Friedman4059da82012-05-24 21:05:41 +00002195
Eli Friedmana6115062012-05-24 00:47:05 +00002196 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002197 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002198 if (CE->getCastKind() == CK_ConstructorConversion)
2199 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002200
Eli Friedmana6115062012-05-24 00:47:05 +00002201 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002202 if (const CXXFunctionalCastExpr *CXXCE =
2203 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedmancdd4b782013-08-15 22:02:56 +00002204 Loc = CXXCE->getLocStart();
Eli Friedman4059da82012-05-24 21:05:41 +00002205 R1 = CXXCE->getSubExpr()->getSourceRange();
2206 } else {
2207 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2208 Loc = CStyleCE->getLParenLoc();
2209 R1 = CStyleCE->getSubExpr()->getSourceRange();
2210 }
Chris Lattner026dc962009-02-14 07:37:35 +00002211 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002212 }
Eli Friedmana6115062012-05-24 00:47:05 +00002213 case ImplicitCastExprClass: {
2214 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002215
Eli Friedmana6115062012-05-24 00:47:05 +00002216 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2217 if (ICE->getCastKind() == CK_LValueToRValue &&
2218 ICE->getSubExpr()->getType().isVolatileQualified())
2219 return false;
2220
2221 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2222 }
Chris Lattner04421082008-04-08 04:40:51 +00002223 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002224 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002225 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smithc3bf52c2013-04-20 22:23:05 +00002226 case CXXDefaultInitExprClass:
2227 return (cast<CXXDefaultInitExpr>(this)
2228 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002229
2230 case CXXNewExprClass:
2231 // FIXME: In theory, there might be new expressions that don't have side
2232 // effects (e.g. a placement new with an uninitialized POD).
2233 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002234 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002235 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002236 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002237 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002238 case ExprWithCleanupsClass:
2239 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002240 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002241 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002242}
2243
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002244/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002245/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002246bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002247 const Expr *E = IgnoreParens();
2248 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002249 default:
2250 return false;
2251 case ObjCIvarRefExprClass:
2252 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002253 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002254 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002255 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002256 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002257 case MaterializeTemporaryExprClass:
2258 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2259 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002260 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002261 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002262 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002263 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002264
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002265 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2266 if (VD->hasGlobalStorage())
2267 return true;
2268 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002269 // dereferencing to a pointer is always a gc'able candidate,
2270 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002271 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002272 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002273 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002274 return false;
2275 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002276 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002277 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002278 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002279 }
2280 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002281 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002282 }
2283}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002284
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002285bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2286 if (isTypeDependent())
2287 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002288 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002289}
2290
John McCall864c0412011-04-26 20:42:42 +00002291QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002292 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002293
2294 // Bound member expressions are always one of these possibilities:
2295 // x->m x.m x->*y x.*y
2296 // (possibly parenthesized)
2297
2298 expr = expr->IgnoreParens();
2299 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2300 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2301 return mem->getMemberDecl()->getType();
2302 }
2303
2304 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2305 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2306 ->getPointeeType();
2307 assert(type->isFunctionType());
2308 return type;
2309 }
2310
2311 assert(isa<UnresolvedMemberExpr>(expr));
2312 return QualType();
2313}
2314
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002315Expr* Expr::IgnoreParens() {
2316 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002317 while (true) {
2318 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2319 E = P->getSubExpr();
2320 continue;
2321 }
2322 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2323 if (P->getOpcode() == UO_Extension) {
2324 E = P->getSubExpr();
2325 continue;
2326 }
2327 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002328 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2329 if (!P->isResultDependent()) {
2330 E = P->getResultExpr();
2331 continue;
2332 }
2333 }
Eli Friedmana5e66012013-07-20 00:40:58 +00002334 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2335 if (!P->isConditionDependent()) {
2336 E = P->getChosenSubExpr();
2337 continue;
2338 }
2339 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002340 return E;
2341 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002342}
2343
Chris Lattner56f34942008-02-13 01:02:39 +00002344/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2345/// or CastExprs or ImplicitCastExprs, returning their operand.
2346Expr *Expr::IgnoreParenCasts() {
2347 Expr *E = this;
2348 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002349 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002350 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002351 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002352 continue;
2353 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002354 if (MaterializeTemporaryExpr *Materialize
2355 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2356 E = Materialize->GetTemporaryExpr();
2357 continue;
2358 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002359 if (SubstNonTypeTemplateParmExpr *NTTP
2360 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2361 E = NTTP->getReplacement();
2362 continue;
2363 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002364 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002365 }
2366}
2367
John McCall9c5d70c2010-12-04 08:24:19 +00002368/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2369/// casts. This is intended purely as a temporary workaround for code
2370/// that hasn't yet been rewritten to do the right thing about those
2371/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002372Expr *Expr::IgnoreParenLValueCasts() {
2373 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002374 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002375 E = E->IgnoreParens();
2376 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002377 if (P->getCastKind() == CK_LValueToRValue) {
2378 E = P->getSubExpr();
2379 continue;
2380 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002381 } else if (MaterializeTemporaryExpr *Materialize
2382 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2383 E = Materialize->GetTemporaryExpr();
2384 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002385 } else if (SubstNonTypeTemplateParmExpr *NTTP
2386 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2387 E = NTTP->getReplacement();
2388 continue;
John McCallf6a16482010-12-04 03:47:34 +00002389 }
2390 break;
2391 }
2392 return E;
2393}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002394
2395Expr *Expr::ignoreParenBaseCasts() {
2396 Expr *E = this;
2397 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002398 E = E->IgnoreParens();
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002399 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2400 if (CE->getCastKind() == CK_DerivedToBase ||
2401 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2402 CE->getCastKind() == CK_NoOp) {
2403 E = CE->getSubExpr();
2404 continue;
2405 }
2406 }
2407
2408 return E;
2409 }
2410}
2411
John McCall2fc46bf2010-05-05 22:59:52 +00002412Expr *Expr::IgnoreParenImpCasts() {
2413 Expr *E = this;
2414 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002415 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002416 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002417 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002418 continue;
2419 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002420 if (MaterializeTemporaryExpr *Materialize
2421 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2422 E = Materialize->GetTemporaryExpr();
2423 continue;
2424 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002425 if (SubstNonTypeTemplateParmExpr *NTTP
2426 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2427 E = NTTP->getReplacement();
2428 continue;
2429 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002430 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002431 }
2432}
2433
Hans Wennborg2f072b42011-06-09 17:06:51 +00002434Expr *Expr::IgnoreConversionOperator() {
2435 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002436 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002437 return MCE->getImplicitObjectArgument();
2438 }
2439 return this;
2440}
2441
Chris Lattnerecdd8412009-03-13 17:28:01 +00002442/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2443/// value (including ptr->int casts of the same size). Strip off any
2444/// ParenExpr or CastExprs, returning their operand.
2445Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2446 Expr *E = this;
2447 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002448 E = E->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +00002449
Chris Lattnerecdd8412009-03-13 17:28:01 +00002450 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2451 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002452 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002453 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002454
Chris Lattnerecdd8412009-03-13 17:28:01 +00002455 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2456 E = SE;
2457 continue;
2458 }
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002460 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002461 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002462 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002463 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002464 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2465 E = SE;
2466 continue;
2467 }
2468 }
Mike Stump1eb44332009-09-09 15:08:12 +00002469
Douglas Gregorc0244c52011-09-08 17:56:33 +00002470 if (SubstNonTypeTemplateParmExpr *NTTP
2471 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2472 E = NTTP->getReplacement();
2473 continue;
2474 }
2475
Chris Lattnerecdd8412009-03-13 17:28:01 +00002476 return E;
2477 }
2478}
2479
Douglas Gregor6eef5192009-12-14 19:27:10 +00002480bool Expr::isDefaultArgument() const {
2481 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002482 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2483 E = M->GetTemporaryExpr();
2484
Douglas Gregor6eef5192009-12-14 19:27:10 +00002485 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2486 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002487
Douglas Gregor6eef5192009-12-14 19:27:10 +00002488 return isa<CXXDefaultArgExpr>(E);
2489}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002490
Douglas Gregor2f599792010-04-02 18:24:57 +00002491/// \brief Skip over any no-op casts and any temporary-binding
2492/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002493static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002494 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2495 E = M->GetTemporaryExpr();
2496
Douglas Gregor2f599792010-04-02 18:24:57 +00002497 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002498 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002499 E = ICE->getSubExpr();
2500 else
2501 break;
2502 }
2503
2504 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2505 E = BE->getSubExpr();
2506
2507 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002508 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002509 E = ICE->getSubExpr();
2510 else
2511 break;
2512 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002513
2514 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002515}
2516
John McCall558d2ab2010-09-15 10:14:12 +00002517/// isTemporaryObject - Determines if this expression produces a
2518/// temporary of the given class type.
2519bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2520 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2521 return false;
2522
Anders Carlssonf8b30152010-11-28 16:40:49 +00002523 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002524
John McCall58277b52010-09-15 20:59:13 +00002525 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002526 if (!E->Classify(C).isPRValue()) {
2527 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002528 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002529 return false;
2530 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002531
John McCall19e60ad2010-09-16 06:57:56 +00002532 // Black-list a few cases which yield pr-values of class type that don't
2533 // refer to temporaries of that type:
2534
2535 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002536 if (isa<ImplicitCastExpr>(E)) {
2537 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2538 case CK_DerivedToBase:
2539 case CK_UncheckedDerivedToBase:
2540 return false;
2541 default:
2542 break;
2543 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002544 }
2545
John McCall19e60ad2010-09-16 06:57:56 +00002546 // - member expressions (all)
2547 if (isa<MemberExpr>(E))
2548 return false;
2549
Eli Friedman32f498a2012-06-15 23:51:06 +00002550 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2551 if (BO->isPtrMemOp())
2552 return false;
2553
John McCall56ca35d2011-02-17 10:25:35 +00002554 // - opaque values (all)
2555 if (isa<OpaqueValueExpr>(E))
2556 return false;
2557
John McCall558d2ab2010-09-15 10:14:12 +00002558 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002559}
2560
Douglas Gregor75e85042011-03-02 21:06:53 +00002561bool Expr::isImplicitCXXThis() const {
2562 const Expr *E = this;
2563
2564 // Strip away parentheses and casts we don't care about.
2565 while (true) {
2566 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2567 E = Paren->getSubExpr();
2568 continue;
2569 }
2570
2571 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2572 if (ICE->getCastKind() == CK_NoOp ||
2573 ICE->getCastKind() == CK_LValueToRValue ||
2574 ICE->getCastKind() == CK_DerivedToBase ||
2575 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2576 E = ICE->getSubExpr();
2577 continue;
2578 }
2579 }
2580
2581 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2582 if (UnOp->getOpcode() == UO_Extension) {
2583 E = UnOp->getSubExpr();
2584 continue;
2585 }
2586 }
2587
Douglas Gregor03e80032011-06-21 17:03:29 +00002588 if (const MaterializeTemporaryExpr *M
2589 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2590 E = M->GetTemporaryExpr();
2591 continue;
2592 }
2593
Douglas Gregor75e85042011-03-02 21:06:53 +00002594 break;
2595 }
2596
2597 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2598 return This->isImplicit();
2599
2600 return false;
2601}
2602
Douglas Gregor898574e2008-12-05 23:32:09 +00002603/// hasAnyTypeDependentArguments - Determines if any of the expressions
2604/// in Exprs is type-dependent.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002605bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00002606 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002607 if (Exprs[I]->isTypeDependent())
2608 return true;
2609
2610 return false;
2611}
2612
John McCall4204f072010-08-02 21:13:48 +00002613bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002614 // This function is attempting whether an expression is an initializer
Eli Friedman21cde052013-07-16 22:40:53 +00002615 // which can be evaluated at compile-time. It very closely parallels
2616 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2617 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2618 // to isEvaluatable most of the time.
2619 //
John McCall4204f072010-08-02 21:13:48 +00002620 // If we ever capture reference-binding directly in the AST, we can
2621 // kill the second parameter.
2622
2623 if (IsForRef) {
2624 EvalResult Result;
2625 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2626 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002627
Anders Carlssone8a32b82008-11-24 05:23:59 +00002628 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002629 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002630 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002631 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002632 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002633 case CXXTemporaryObjectExprClass:
2634 case CXXConstructExprClass: {
2635 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002636
Eli Friedman21cde052013-07-16 22:40:53 +00002637 if (CE->getConstructor()->isTrivial() &&
2638 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2639 // Trivial default constructor
Richard Smith180f4792011-11-10 06:34:14 +00002640 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002641
Eli Friedman21cde052013-07-16 22:40:53 +00002642 // Trivial copy constructor
2643 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2644 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smith180f4792011-11-10 06:34:14 +00002645 }
2646
Richard Smith180f4792011-11-10 06:34:14 +00002647 break;
John McCallb4b9b152010-08-01 21:51:45 +00002648 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002649 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002650 // This handles gcc's extension that allows global initializers like
2651 // "struct x {int x;} x = (struct x) {};".
2652 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002653 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002654 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002655 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002656 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002657 // FIXME: This doesn't deal with fields with reference types correctly.
2658 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2659 // to bitfields.
Eli Friedman21cde052013-07-16 22:40:53 +00002660 const InitListExpr *ILE = cast<InitListExpr>(this);
2661 if (ILE->getType()->isArrayType()) {
2662 unsigned numInits = ILE->getNumInits();
2663 for (unsigned i = 0; i < numInits; i++) {
2664 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2665 return false;
2666 }
2667 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002668 }
Eli Friedman21cde052013-07-16 22:40:53 +00002669
2670 if (ILE->getType()->isRecordType()) {
2671 unsigned ElementNo = 0;
2672 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2673 for (RecordDecl::field_iterator Field = RD->field_begin(),
2674 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2675 // If this is a union, skip all the fields that aren't being initialized.
2676 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2677 continue;
2678
2679 // Don't emit anonymous bitfields, they just affect layout.
2680 if (Field->isUnnamedBitfield())
2681 continue;
2682
2683 if (ElementNo < ILE->getNumInits()) {
2684 const Expr *Elt = ILE->getInit(ElementNo++);
2685 if (Field->isBitField()) {
2686 // Bitfields have to evaluate to an integer.
2687 llvm::APSInt ResultTmp;
2688 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2689 return false;
2690 } else {
2691 bool RefType = Field->getType()->isReferenceType();
2692 if (!Elt->isConstantInitializer(Ctx, RefType))
2693 return false;
2694 }
2695 }
2696 }
2697 return true;
2698 }
2699
2700 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002701 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002702 case ImplicitValueInitExprClass:
2703 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002704 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002705 return cast<ParenExpr>(this)->getSubExpr()
2706 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002707 case GenericSelectionExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002708 return cast<GenericSelectionExpr>(this)->getResultExpr()
2709 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002710 case ChooseExprClass:
Eli Friedmana5e66012013-07-20 00:40:58 +00002711 if (cast<ChooseExpr>(this)->isConditionDependent())
2712 return false;
2713 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002714 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002715 case UnaryOperatorClass: {
2716 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002717 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002718 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002719 break;
2720 }
John McCall4204f072010-08-02 21:13:48 +00002721 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002722 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002723 case ImplicitCastExprClass:
Eli Friedman21cde052013-07-16 22:40:53 +00002724 case CStyleCastExprClass:
2725 case ObjCBridgedCastExprClass:
2726 case CXXDynamicCastExprClass:
2727 case CXXReinterpretCastExprClass:
2728 case CXXConstCastExprClass: {
Richard Smithd62ca372011-12-06 22:44:34 +00002729 const CastExpr *CE = cast<CastExpr>(this);
2730
Eli Friedman6bd97192011-12-21 00:43:02 +00002731 // Handle misc casts we want to ignore.
Eli Friedman6bd97192011-12-21 00:43:02 +00002732 if (CE->getCastKind() == CK_NoOp ||
2733 CE->getCastKind() == CK_LValueToRValue ||
2734 CE->getCastKind() == CK_ToUnion ||
Eli Friedman21cde052013-07-16 22:40:53 +00002735 CE->getCastKind() == CK_ConstructorConversion ||
2736 CE->getCastKind() == CK_NonAtomicToAtomic ||
2737 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smithd62ca372011-12-06 22:44:34 +00002738 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2739
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002740 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002741 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002742 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002743 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002744 ->isConstantInitializer(Ctx, false);
Eli Friedman21cde052013-07-16 22:40:53 +00002745
2746 case SubstNonTypeTemplateParmExprClass:
2747 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2748 ->isConstantInitializer(Ctx, false);
2749 case CXXDefaultArgExprClass:
2750 return cast<CXXDefaultArgExpr>(this)->getExpr()
2751 ->isConstantInitializer(Ctx, false);
2752 case CXXDefaultInitExprClass:
2753 return cast<CXXDefaultInitExpr>(this)->getExpr()
2754 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002755 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002756 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002757}
2758
Richard Smith8ae4ec22012-08-07 04:16:51 +00002759bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2760 if (isInstantiationDependent())
2761 return true;
2762
2763 switch (getStmtClass()) {
2764 case NoStmtClass:
2765 #define ABSTRACT_STMT(Type)
2766 #define STMT(Type, Base) case Type##Class:
2767 #define EXPR(Type, Base)
2768 #include "clang/AST/StmtNodes.inc"
2769 llvm_unreachable("unexpected Expr kind");
2770
2771 case DependentScopeDeclRefExprClass:
2772 case CXXUnresolvedConstructExprClass:
2773 case CXXDependentScopeMemberExprClass:
2774 case UnresolvedLookupExprClass:
2775 case UnresolvedMemberExprClass:
2776 case PackExpansionExprClass:
2777 case SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +00002778 case FunctionParmPackExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002779 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2780
Richard Smith60b70382012-08-07 05:18:29 +00002781 case DeclRefExprClass:
2782 case ObjCIvarRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002783 case PredefinedExprClass:
2784 case IntegerLiteralClass:
2785 case FloatingLiteralClass:
2786 case ImaginaryLiteralClass:
2787 case StringLiteralClass:
2788 case CharacterLiteralClass:
2789 case OffsetOfExprClass:
2790 case ImplicitValueInitExprClass:
2791 case UnaryExprOrTypeTraitExprClass:
2792 case AddrLabelExprClass:
2793 case GNUNullExprClass:
2794 case CXXBoolLiteralExprClass:
2795 case CXXNullPtrLiteralExprClass:
2796 case CXXThisExprClass:
2797 case CXXScalarValueInitExprClass:
2798 case TypeTraitExprClass:
2799 case UnaryTypeTraitExprClass:
2800 case BinaryTypeTraitExprClass:
2801 case ArrayTypeTraitExprClass:
2802 case ExpressionTraitExprClass:
2803 case CXXNoexceptExprClass:
2804 case SizeOfPackExprClass:
2805 case ObjCStringLiteralClass:
2806 case ObjCEncodeExprClass:
2807 case ObjCBoolLiteralExprClass:
2808 case CXXUuidofExprClass:
2809 case OpaqueValueExprClass:
2810 // These never have a side-effect.
2811 return false;
2812
2813 case CallExprClass:
John McCall76da55d2013-04-16 07:28:30 +00002814 case MSPropertyRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002815 case CompoundAssignOperatorClass:
2816 case VAArgExprClass:
2817 case AtomicExprClass:
2818 case StmtExprClass:
2819 case CXXOperatorCallExprClass:
2820 case CXXMemberCallExprClass:
2821 case UserDefinedLiteralClass:
2822 case CXXThrowExprClass:
2823 case CXXNewExprClass:
2824 case CXXDeleteExprClass:
2825 case ExprWithCleanupsClass:
2826 case CXXBindTemporaryExprClass:
2827 case BlockExprClass:
2828 case CUDAKernelCallExprClass:
2829 // These always have a side-effect.
2830 return true;
2831
2832 case ParenExprClass:
2833 case ArraySubscriptExprClass:
2834 case MemberExprClass:
2835 case ConditionalOperatorClass:
2836 case BinaryConditionalOperatorClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002837 case CompoundLiteralExprClass:
2838 case ExtVectorElementExprClass:
2839 case DesignatedInitExprClass:
2840 case ParenListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002841 case CXXPseudoDestructorExprClass:
Richard Smith7c3e6152013-06-12 22:31:48 +00002842 case CXXStdInitializerListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002843 case SubstNonTypeTemplateParmExprClass:
2844 case MaterializeTemporaryExprClass:
2845 case ShuffleVectorExprClass:
2846 case AsTypeExprClass:
2847 // These have a side-effect if any subexpression does.
2848 break;
2849
Richard Smith60b70382012-08-07 05:18:29 +00002850 case UnaryOperatorClass:
2851 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002852 return true;
2853 break;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002854
2855 case BinaryOperatorClass:
2856 if (cast<BinaryOperator>(this)->isAssignmentOp())
2857 return true;
2858 break;
2859
Richard Smith8ae4ec22012-08-07 04:16:51 +00002860 case InitListExprClass:
2861 // FIXME: The children for an InitListExpr doesn't include the array filler.
2862 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2863 if (E->HasSideEffects(Ctx))
2864 return true;
2865 break;
2866
2867 case GenericSelectionExprClass:
2868 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2869 HasSideEffects(Ctx);
2870
2871 case ChooseExprClass:
Eli Friedmana5e66012013-07-20 00:40:58 +00002872 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith8ae4ec22012-08-07 04:16:51 +00002873
2874 case CXXDefaultArgExprClass:
2875 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2876
Richard Smithc3bf52c2013-04-20 22:23:05 +00002877 case CXXDefaultInitExprClass:
2878 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2879 return E->HasSideEffects(Ctx);
2880 // If we've not yet parsed the initializer, assume it has side-effects.
2881 return true;
2882
Richard Smith8ae4ec22012-08-07 04:16:51 +00002883 case CXXDynamicCastExprClass: {
2884 // A dynamic_cast expression has side-effects if it can throw.
2885 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2886 if (DCE->getTypeAsWritten()->isReferenceType() &&
2887 DCE->getCastKind() == CK_Dynamic)
2888 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002889 } // Fall through.
2890 case ImplicitCastExprClass:
2891 case CStyleCastExprClass:
2892 case CXXStaticCastExprClass:
2893 case CXXReinterpretCastExprClass:
2894 case CXXConstCastExprClass:
2895 case CXXFunctionalCastExprClass: {
2896 const CastExpr *CE = cast<CastExpr>(this);
2897 if (CE->getCastKind() == CK_LValueToRValue &&
2898 CE->getSubExpr()->getType().isVolatileQualified())
2899 return true;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002900 break;
2901 }
2902
Richard Smith0d729102012-08-13 20:08:14 +00002903 case CXXTypeidExprClass:
2904 // typeid might throw if its subexpression is potentially-evaluated, so has
2905 // side-effects in that case whether or not its subexpression does.
2906 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith8ae4ec22012-08-07 04:16:51 +00002907
2908 case CXXConstructExprClass:
2909 case CXXTemporaryObjectExprClass: {
2910 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smith60b70382012-08-07 05:18:29 +00002911 if (!CE->getConstructor()->isTrivial())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002912 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002913 // A trivial constructor does not add any side-effects of its own. Just look
2914 // at its arguments.
Richard Smith8ae4ec22012-08-07 04:16:51 +00002915 break;
2916 }
2917
2918 case LambdaExprClass: {
2919 const LambdaExpr *LE = cast<LambdaExpr>(this);
2920 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2921 E = LE->capture_end(); I != E; ++I)
2922 if (I->getCaptureKind() == LCK_ByCopy)
2923 // FIXME: Only has a side-effect if the variable is volatile or if
2924 // the copy would invoke a non-trivial copy constructor.
2925 return true;
2926 return false;
2927 }
2928
2929 case PseudoObjectExprClass: {
2930 // Only look for side-effects in the semantic form, and look past
2931 // OpaqueValueExpr bindings in that form.
2932 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2933 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2934 E = PO->semantics_end();
2935 I != E; ++I) {
2936 const Expr *Subexpr = *I;
2937 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2938 Subexpr = OVE->getSourceExpr();
2939 if (Subexpr->HasSideEffects(Ctx))
2940 return true;
2941 }
2942 return false;
2943 }
2944
2945 case ObjCBoxedExprClass:
2946 case ObjCArrayLiteralClass:
2947 case ObjCDictionaryLiteralClass:
2948 case ObjCMessageExprClass:
2949 case ObjCSelectorExprClass:
2950 case ObjCProtocolExprClass:
2951 case ObjCPropertyRefExprClass:
2952 case ObjCIsaExprClass:
2953 case ObjCIndirectCopyRestoreExprClass:
2954 case ObjCSubscriptRefExprClass:
2955 case ObjCBridgedCastExprClass:
2956 // FIXME: Classify these cases better.
2957 return true;
2958 }
2959
2960 // Recurse to children.
2961 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2962 if (const Stmt *S = *SubStmts)
2963 if (cast<Expr>(S)->HasSideEffects(Ctx))
2964 return true;
2965
2966 return false;
2967}
2968
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002969namespace {
2970 /// \brief Look for a call to a non-trivial function within an expression.
2971 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2972 {
2973 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2974
2975 bool NonTrivial;
2976
2977 public:
2978 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002979 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002980
2981 bool hasNonTrivialCall() const { return NonTrivial; }
2982
2983 void VisitCallExpr(CallExpr *E) {
2984 if (CXXMethodDecl *Method
2985 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2986 if (Method->isTrivial()) {
2987 // Recurse to children of the call.
2988 Inherited::VisitStmt(E);
2989 return;
2990 }
2991 }
2992
2993 NonTrivial = true;
2994 }
2995
2996 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2997 if (E->getConstructor()->isTrivial()) {
2998 // Recurse to children of the call.
2999 Inherited::VisitStmt(E);
3000 return;
3001 }
3002
3003 NonTrivial = true;
3004 }
3005
3006 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3007 if (E->getTemporary()->getDestructor()->isTrivial()) {
3008 Inherited::VisitStmt(E);
3009 return;
3010 }
3011
3012 NonTrivial = true;
3013 }
3014 };
3015}
3016
3017bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3018 NonTrivialCallFinder Finder(Ctx);
3019 Finder.Visit(this);
3020 return Finder.hasNonTrivialCall();
3021}
3022
Chandler Carruth82214a82011-02-18 23:54:50 +00003023/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3024/// pointer constant or not, as well as the specific kind of constant detected.
3025/// Null pointer constants can be integer constant expressions with the
3026/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3027/// (a GNU extension).
3028Expr::NullPointerConstantKind
3029Expr::isNullPointerConstant(ASTContext &Ctx,
3030 NullPointerConstantValueDependence NPC) const {
Richard Smithf050d242013-06-13 02:46:14 +00003031 if (isValueDependent() && !Ctx.getLangOpts().CPlusPlus11) {
Douglas Gregorce940492009-09-25 04:25:58 +00003032 switch (NPC) {
3033 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00003034 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00003035 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003036 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie50800fc2012-08-08 17:33:31 +00003037 return NPCK_ZeroExpression;
Chandler Carruth82214a82011-02-18 23:54:50 +00003038 else
3039 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00003040
Douglas Gregorce940492009-09-25 04:25:58 +00003041 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003042 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00003043 }
3044 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00003045
Sebastian Redl07779722008-10-31 14:43:28 +00003046 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00003047 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003048 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00003049 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00003050 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00003051 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00003052 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00003053 Pointee->isVoidType() && // to void*
3054 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00003055 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00003056 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003057 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003058 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3059 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00003060 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00003061 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3062 // Accept ((void*)0) as a null pointer constant, as many other
3063 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00003064 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00003065 } else if (const GenericSelectionExpr *GE =
3066 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedmana5e66012013-07-20 00:40:58 +00003067 if (GE->isResultDependent())
3068 return NPCK_NotNull;
Peter Collingbournef111d932011-04-15 00:35:48 +00003069 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedmana5e66012013-07-20 00:40:58 +00003070 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3071 if (CE->isConditionDependent())
3072 return NPCK_NotNull;
3073 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00003074 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00003075 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smithc3bf52c2013-04-20 22:23:05 +00003076 // See through default argument expressions.
Douglas Gregorce940492009-09-25 04:25:58 +00003077 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smithc3bf52c2013-04-20 22:23:05 +00003078 } else if (const CXXDefaultInitExpr *DefaultInit
3079 = dyn_cast<CXXDefaultInitExpr>(this)) {
3080 // See through default initializer expressions.
3081 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003082 } else if (isa<GNUNullExpr>(this)) {
3083 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00003084 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00003085 } else if (const MaterializeTemporaryExpr *M
3086 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3087 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00003088 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3089 if (const Expr *Source = OVE->getSourceExpr())
3090 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00003091 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003092
Richard Smith4e24f0f2013-01-02 12:01:23 +00003093 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003094 if (getType()->isNullPtrType())
Richard Smith4e24f0f2013-01-02 12:01:23 +00003095 return NPCK_CXX11_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003096
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003097 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smithf050d242013-06-13 02:46:14 +00003098 if (!Ctx.getLangOpts().CPlusPlus11 &&
3099 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003100 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3101 const Expr *InitExpr = CLE->getInitializer();
3102 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3103 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3104 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003105 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00003106 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003107 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00003108 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00003109
Richard Smith80ad52f2013-01-02 11:42:31 +00003110 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smithf050d242013-06-13 02:46:14 +00003111 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3112 // value zero or a prvalue of type std::nullptr_t.
3113 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
3114 return (Lit && !Lit->getValue()) ? NPCK_ZeroLiteral : NPCK_NotNull;
Richard Smith70488e22012-02-14 21:38:30 +00003115 } else {
Richard Smithf050d242013-06-13 02:46:14 +00003116 // If we have an integer constant expression, we need to *evaluate* it and
3117 // test for the value 0.
Richard Smith70488e22012-02-14 21:38:30 +00003118 if (!isIntegerConstantExpr(Ctx))
3119 return NPCK_NotNull;
3120 }
Chandler Carruth82214a82011-02-18 23:54:50 +00003121
David Blaikie50800fc2012-08-08 17:33:31 +00003122 if (EvaluateKnownConstInt(Ctx) != 0)
3123 return NPCK_NotNull;
3124
3125 if (isa<IntegerLiteral>(this))
3126 return NPCK_ZeroLiteral;
3127 return NPCK_ZeroExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00003128}
Steve Naroff31a45842007-07-28 23:10:27 +00003129
John McCallf6a16482010-12-04 03:47:34 +00003130/// \brief If this expression is an l-value for an Objective C
3131/// property, find the underlying property reference expression.
3132const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3133 const Expr *E = this;
3134 while (true) {
3135 assert((E->getValueKind() == VK_LValue &&
3136 E->getObjectKind() == OK_ObjCProperty) &&
3137 "expression is not a property reference");
3138 E = E->IgnoreParenCasts();
3139 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3140 if (BO->getOpcode() == BO_Comma) {
3141 E = BO->getRHS();
3142 continue;
3143 }
3144 }
3145
3146 break;
3147 }
3148
3149 return cast<ObjCPropertyRefExpr>(E);
3150}
3151
Anna Zaksbbff82f2012-10-01 20:34:04 +00003152bool Expr::isObjCSelfExpr() const {
3153 const Expr *E = IgnoreParenImpCasts();
3154
3155 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3156 if (!DRE)
3157 return false;
3158
3159 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3160 if (!Param)
3161 return false;
3162
3163 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3164 if (!M)
3165 return false;
3166
3167 return M->getSelfDecl() == Param;
3168}
3169
John McCall993f43f2013-05-06 21:39:12 +00003170FieldDecl *Expr::getSourceBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00003171 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003172
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003173 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00003174 if (ICE->getCastKind() == CK_LValueToRValue ||
3175 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003176 E = ICE->getSubExpr()->IgnoreParens();
3177 else
3178 break;
3179 }
3180
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003181 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00003182 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003183 if (Field->isBitField())
3184 return Field;
3185
John McCall993f43f2013-05-06 21:39:12 +00003186 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3187 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3188 if (Ivar->isBitField())
3189 return Ivar;
3190
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00003191 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3192 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3193 if (Field->isBitField())
3194 return Field;
3195
Eli Friedman42068e92011-07-13 02:05:57 +00003196 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003197 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCall993f43f2013-05-06 21:39:12 +00003198 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003199
Eli Friedman42068e92011-07-13 02:05:57 +00003200 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCall993f43f2013-05-06 21:39:12 +00003201 return BinOp->getRHS()->getSourceBitField();
Eli Friedman42068e92011-07-13 02:05:57 +00003202 }
3203
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003204 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003205}
3206
Anders Carlsson09380262010-01-31 17:18:49 +00003207bool Expr::refersToVectorElement() const {
3208 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00003209
Anders Carlsson09380262010-01-31 17:18:49 +00003210 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00003211 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00003212 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00003213 E = ICE->getSubExpr()->IgnoreParens();
3214 else
3215 break;
3216 }
Sean Huntc3021132010-05-05 15:23:54 +00003217
Anders Carlsson09380262010-01-31 17:18:49 +00003218 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3219 return ASE->getBase()->getType()->isVectorType();
3220
3221 if (isa<ExtVectorElementExpr>(E))
3222 return true;
3223
3224 return false;
3225}
3226
Chris Lattner2140e902009-02-16 22:14:05 +00003227/// isArrow - Return true if the base expression is a pointer to vector,
3228/// return false if the base expression is a vector.
3229bool ExtVectorElementExpr::isArrow() const {
3230 return getBase()->getType()->isPointerType();
3231}
3232
Nate Begeman213541a2008-04-18 23:10:10 +00003233unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003234 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003235 return VT->getNumElements();
3236 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003237}
3238
Nate Begeman8a997642008-05-09 06:41:27 +00003239/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003240bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003241 // FIXME: Refactor this code to an accessor on the AST node which returns the
3242 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003243 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003244
3245 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003246 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003247 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Nate Begeman190d6a22009-01-18 02:01:21 +00003249 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003250 if (Comp[0] == 's' || Comp[0] == 'S')
3251 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003252
Daniel Dunbar15027422009-10-17 23:53:04 +00003253 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003254 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003255 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003256
Steve Narofffec0b492007-07-30 03:29:09 +00003257 return false;
3258}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003259
Nate Begeman8a997642008-05-09 06:41:27 +00003260/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003261void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003262 SmallVectorImpl<unsigned> &Elts) const {
3263 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003264 if (Comp[0] == 's' || Comp[0] == 'S')
3265 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003266
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003267 bool isHi = Comp == "hi";
3268 bool isLo = Comp == "lo";
3269 bool isEven = Comp == "even";
3270 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Nate Begeman8a997642008-05-09 06:41:27 +00003272 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3273 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003274
Nate Begeman8a997642008-05-09 06:41:27 +00003275 if (isHi)
3276 Index = e + i;
3277 else if (isLo)
3278 Index = i;
3279 else if (isEven)
3280 Index = 2 * i;
3281 else if (isOdd)
3282 Index = 2 * i + 1;
3283 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003284 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003285
Nate Begeman3b8d1162008-05-13 21:03:02 +00003286 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003287 }
Nate Begeman8a997642008-05-09 06:41:27 +00003288}
3289
Douglas Gregor04badcf2010-04-21 00:45:42 +00003290ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003291 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003292 SourceLocation LBracLoc,
3293 SourceLocation SuperLoc,
3294 bool IsInstanceSuper,
3295 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003296 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003297 ArrayRef<SourceLocation> SelLocs,
3298 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003299 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003300 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003301 SourceLocation RBracLoc,
3302 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003303 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003304 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003305 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003306 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003307 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3308 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003309 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003310 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3311 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003312{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003313 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003314 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003315}
3316
Douglas Gregor04badcf2010-04-21 00:45:42 +00003317ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003318 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003319 SourceLocation LBracLoc,
3320 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003321 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003322 ArrayRef<SourceLocation> SelLocs,
3323 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003324 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003325 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003326 SourceLocation RBracLoc,
3327 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003328 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003329 T->isDependentType(), T->isInstantiationDependentType(),
3330 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003331 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3332 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003333 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003334 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003335 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003336{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003337 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003338 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003339}
3340
Douglas Gregor04badcf2010-04-21 00:45:42 +00003341ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003342 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003343 SourceLocation LBracLoc,
3344 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003345 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003346 ArrayRef<SourceLocation> SelLocs,
3347 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003348 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003349 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003350 SourceLocation RBracLoc,
3351 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003352 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003353 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003354 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003355 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003356 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3357 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003358 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003359 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003360 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003361{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003362 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003363 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003364}
3365
3366void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3367 ArrayRef<SourceLocation> SelLocs,
3368 SelectorLocationsKind SelLocsK) {
3369 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003370 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003371 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003372 if (Args[I]->isTypeDependent())
3373 ExprBits.TypeDependent = true;
3374 if (Args[I]->isValueDependent())
3375 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003376 if (Args[I]->isInstantiationDependent())
3377 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003378 if (Args[I]->containsUnexpandedParameterPack())
3379 ExprBits.ContainsUnexpandedParameterPack = true;
3380
3381 MyArgs[I] = Args[I];
3382 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003383
Benjamin Kramer19562c92012-02-20 00:20:48 +00003384 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003385 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003386 if (SelLocsK == SelLoc_NonStandard)
3387 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3388 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003389}
3390
Craig Topper9db7a7e2013-08-22 04:58:56 +00003391ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003392 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003393 SourceLocation LBracLoc,
3394 SourceLocation SuperLoc,
3395 bool IsInstanceSuper,
3396 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003397 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003398 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003399 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003400 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003401 SourceLocation RBracLoc,
3402 bool isImplicit) {
3403 assert((!SelLocs.empty() || isImplicit) &&
3404 "No selector locs for non-implicit message");
3405 ObjCMessageExpr *Mem;
3406 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3407 if (isImplicit)
3408 Mem = alloc(Context, Args.size(), 0);
3409 else
3410 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003411 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003412 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003413 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003414}
3415
Craig Topper9db7a7e2013-08-22 04:58:56 +00003416ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003417 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003418 SourceLocation LBracLoc,
3419 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003420 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003421 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003422 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003423 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003424 SourceLocation RBracLoc,
3425 bool isImplicit) {
3426 assert((!SelLocs.empty() || isImplicit) &&
3427 "No selector locs for non-implicit message");
3428 ObjCMessageExpr *Mem;
3429 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3430 if (isImplicit)
3431 Mem = alloc(Context, Args.size(), 0);
3432 else
3433 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003434 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003435 SelLocs, SelLocsK, Method, Args, RBracLoc,
3436 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003437}
3438
Craig Topper9db7a7e2013-08-22 04:58:56 +00003439ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003440 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003441 SourceLocation LBracLoc,
3442 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003443 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003444 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003445 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003446 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003447 SourceLocation RBracLoc,
3448 bool isImplicit) {
3449 assert((!SelLocs.empty() || isImplicit) &&
3450 "No selector locs for non-implicit message");
3451 ObjCMessageExpr *Mem;
3452 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3453 if (isImplicit)
3454 Mem = alloc(Context, Args.size(), 0);
3455 else
3456 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003457 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003458 SelLocs, SelLocsK, Method, Args, RBracLoc,
3459 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003460}
3461
Craig Topper9db7a7e2013-08-22 04:58:56 +00003462ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003463 unsigned NumArgs,
3464 unsigned NumStoredSelLocs) {
3465 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003466 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3467}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003468
Craig Topper9db7a7e2013-08-22 04:58:56 +00003469ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003470 ArrayRef<Expr *> Args,
3471 SourceLocation RBraceLoc,
3472 ArrayRef<SourceLocation> SelLocs,
3473 Selector Sel,
3474 SelectorLocationsKind &SelLocsK) {
3475 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3476 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3477 : 0;
3478 return alloc(C, Args.size(), NumStoredSelLocs);
3479}
3480
Craig Topper9db7a7e2013-08-22 04:58:56 +00003481ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003482 unsigned NumArgs,
3483 unsigned NumStoredSelLocs) {
3484 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3485 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3486 return (ObjCMessageExpr *)C.Allocate(Size,
3487 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3488}
3489
3490void ObjCMessageExpr::getSelectorLocs(
3491 SmallVectorImpl<SourceLocation> &SelLocs) const {
3492 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3493 SelLocs.push_back(getSelectorLoc(i));
3494}
3495
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003496SourceRange ObjCMessageExpr::getReceiverRange() const {
3497 switch (getReceiverKind()) {
3498 case Instance:
3499 return getInstanceReceiver()->getSourceRange();
3500
3501 case Class:
3502 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3503
3504 case SuperInstance:
3505 case SuperClass:
3506 return getSuperLoc();
3507 }
3508
David Blaikie30263482012-01-20 21:50:17 +00003509 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003510}
3511
Douglas Gregor04badcf2010-04-21 00:45:42 +00003512Selector ObjCMessageExpr::getSelector() const {
3513 if (HasMethod)
3514 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3515 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003516 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003517}
3518
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003519QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor04badcf2010-04-21 00:45:42 +00003520 switch (getReceiverKind()) {
3521 case Instance:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003522 return getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003523 case Class:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003524 return getClassReceiver();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003525 case SuperInstance:
Douglas Gregor04badcf2010-04-21 00:45:42 +00003526 case SuperClass:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003527 return getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003528 }
3529
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003530 llvm_unreachable("unexpected receiver kind");
3531}
3532
3533ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3534 QualType T = getReceiverType();
3535
3536 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3537 return Ptr->getInterfaceDecl();
3538
3539 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3540 return Ty->getInterface();
3541
Douglas Gregor04badcf2010-04-21 00:45:42 +00003542 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003543}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003544
Chris Lattner5f9e2722011-07-23 10:55:15 +00003545StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003546 switch (getBridgeKind()) {
3547 case OBC_Bridge:
3548 return "__bridge";
3549 case OBC_BridgeTransfer:
3550 return "__bridge_transfer";
3551 case OBC_BridgeRetained:
3552 return "__bridge_retained";
3553 }
David Blaikie30263482012-01-20 21:50:17 +00003554
3555 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003556}
3557
Craig Topper05ed1a02013-08-18 10:09:15 +00003558ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003559 QualType Type, SourceLocation BLoc,
3560 SourceLocation RP)
3561 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3562 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003563 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003564 Type->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003565 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003566{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003567 SubExprs = new (C) Stmt*[args.size()];
3568 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003569 if (args[i]->isTypeDependent())
3570 ExprBits.TypeDependent = true;
3571 if (args[i]->isValueDependent())
3572 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003573 if (args[i]->isInstantiationDependent())
3574 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003575 if (args[i]->containsUnexpandedParameterPack())
3576 ExprBits.ContainsUnexpandedParameterPack = true;
3577
3578 SubExprs[i] = args[i];
3579 }
3580}
3581
Craig Topper05ed1a02013-08-18 10:09:15 +00003582void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman888376a2009-08-12 02:28:50 +00003583 if (SubExprs) C.Deallocate(SubExprs);
3584
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003585 this->NumExprs = Exprs.size();
Dmitri Gribenko2ad77cd2013-05-10 17:30:13 +00003586 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003587 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00003588}
Nate Begeman888376a2009-08-12 02:28:50 +00003589
Craig Topper05ed1a02013-08-18 10:09:15 +00003590GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003591 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003592 ArrayRef<TypeSourceInfo*> AssocTypes,
3593 ArrayRef<Expr*> AssocExprs,
3594 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003595 SourceLocation RParenLoc,
3596 bool ContainsUnexpandedParameterPack,
3597 unsigned ResultIndex)
3598 : Expr(GenericSelectionExprClass,
3599 AssocExprs[ResultIndex]->getType(),
3600 AssocExprs[ResultIndex]->getValueKind(),
3601 AssocExprs[ResultIndex]->getObjectKind(),
3602 AssocExprs[ResultIndex]->isTypeDependent(),
3603 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003604 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003605 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003606 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3607 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3608 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3609 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003610 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003611 assert(AssocTypes.size() == AssocExprs.size());
3612 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3613 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003614}
3615
Craig Topper05ed1a02013-08-18 10:09:15 +00003616GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003617 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003618 ArrayRef<TypeSourceInfo*> AssocTypes,
3619 ArrayRef<Expr*> AssocExprs,
3620 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003621 SourceLocation RParenLoc,
3622 bool ContainsUnexpandedParameterPack)
3623 : Expr(GenericSelectionExprClass,
3624 Context.DependentTy,
3625 VK_RValue,
3626 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003627 /*isTypeDependent=*/true,
3628 /*isValueDependent=*/true,
3629 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003630 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003631 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3632 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3633 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3634 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003635 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003636 assert(AssocTypes.size() == AssocExprs.size());
3637 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3638 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003639}
3640
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003641//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003642// DesignatedInitExpr
3643//===----------------------------------------------------------------------===//
3644
Chandler Carruthb1138242011-06-16 06:47:06 +00003645IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003646 assert(Kind == FieldDesignator && "Only valid on a field designator");
3647 if (Field.NameOrField & 0x01)
3648 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3649 else
3650 return getField()->getIdentifier();
3651}
3652
Craig Topper05ed1a02013-08-18 10:09:15 +00003653DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003654 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003655 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003656 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003657 bool GNUSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003658 ArrayRef<Expr*> IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003659 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003660 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003661 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003662 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003663 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003664 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003665 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003666 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003667 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003668
3669 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003670 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003671 *Child++ = Init;
3672
3673 // Copy the designators and their subexpressions, computing
3674 // value-dependence along the way.
3675 unsigned IndexIdx = 0;
3676 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003677 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003678
3679 if (this->Designators[I].isArrayDesignator()) {
3680 // Compute type- and value-dependence.
3681 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003682 if (Index->isTypeDependent() || Index->isValueDependent())
3683 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003684 if (Index->isInstantiationDependent())
3685 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003686 // Propagate unexpanded parameter packs.
3687 if (Index->containsUnexpandedParameterPack())
3688 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003689
3690 // Copy the index expressions into permanent storage.
3691 *Child++ = IndexExprs[IndexIdx++];
3692 } else if (this->Designators[I].isArrayRangeDesignator()) {
3693 // Compute type- and value-dependence.
3694 Expr *Start = IndexExprs[IndexIdx];
3695 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003696 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003697 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003698 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003699 ExprBits.InstantiationDependent = true;
3700 } else if (Start->isInstantiationDependent() ||
3701 End->isInstantiationDependent()) {
3702 ExprBits.InstantiationDependent = true;
3703 }
3704
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003705 // Propagate unexpanded parameter packs.
3706 if (Start->containsUnexpandedParameterPack() ||
3707 End->containsUnexpandedParameterPack())
3708 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003709
3710 // Copy the start/end expressions into permanent storage.
3711 *Child++ = IndexExprs[IndexIdx++];
3712 *Child++ = IndexExprs[IndexIdx++];
3713 }
3714 }
3715
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003716 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003717}
3718
Douglas Gregor05c13a32009-01-22 00:58:24 +00003719DesignatedInitExpr *
Craig Topper05ed1a02013-08-18 10:09:15 +00003720DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003721 unsigned NumDesignators,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003722 ArrayRef<Expr*> IndexExprs,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003723 SourceLocation ColonOrEqualLoc,
3724 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003725 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003726 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003727 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003728 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003729 IndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003730}
3731
Craig Topper05ed1a02013-08-18 10:09:15 +00003732DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003733 unsigned NumIndexExprs) {
3734 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3735 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3736 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3737}
3738
Craig Topper05ed1a02013-08-18 10:09:15 +00003739void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003740 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003741 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003742 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003743 NumDesignators = NumDesigs;
3744 for (unsigned I = 0; I != NumDesigs; ++I)
3745 Designators[I] = Desigs[I];
3746}
3747
Abramo Bagnara24f46742011-03-16 15:08:46 +00003748SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3749 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3750 if (size() == 1)
3751 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen65d78312012-12-25 14:51:39 +00003752 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3753 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara24f46742011-03-16 15:08:46 +00003754}
3755
Erik Verbruggen65d78312012-12-25 14:51:39 +00003756SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003757 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003758 Designator &First =
3759 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003760 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003761 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003762 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3763 else
3764 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3765 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003766 StartLoc =
3767 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen65d78312012-12-25 14:51:39 +00003768 return StartLoc;
3769}
3770
3771SourceLocation DesignatedInitExpr::getLocEnd() const {
3772 return getInit()->getLocEnd();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003773}
3774
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003775Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003776 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003777 char *Ptr = static_cast<char *>(
3778 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003779 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003780 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3781 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3782}
3783
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003784Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003785 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003786 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003787 char *Ptr = static_cast<char *>(
3788 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003789 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003790 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3791 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3792}
3793
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003794Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003795 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003796 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003797 char *Ptr = static_cast<char *>(
3798 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003799 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003800 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3801 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3802}
3803
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003804/// \brief Replaces the designator at index @p Idx with the series
3805/// of designators in [First, Last).
Craig Topper05ed1a02013-08-18 10:09:15 +00003806void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003807 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003808 const Designator *Last) {
3809 unsigned NumNewDesignators = Last - First;
3810 if (NumNewDesignators == 0) {
3811 std::copy_backward(Designators + Idx + 1,
3812 Designators + NumDesignators,
3813 Designators + Idx);
3814 --NumNewDesignators;
3815 return;
3816 } else if (NumNewDesignators == 1) {
3817 Designators[Idx] = *First;
3818 return;
3819 }
3820
Mike Stump1eb44332009-09-09 15:08:12 +00003821 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003822 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003823 std::copy(Designators, Designators + Idx, NewDesignators);
3824 std::copy(First, Last, NewDesignators + Idx);
3825 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3826 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003827 Designators = NewDesignators;
3828 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3829}
3830
Craig Topper05ed1a02013-08-18 10:09:15 +00003831ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003832 ArrayRef<Expr*> exprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003833 SourceLocation rparenloc)
3834 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003835 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003836 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3837 Exprs = new (C) Stmt*[exprs.size()];
3838 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003839 if (exprs[i]->isTypeDependent())
3840 ExprBits.TypeDependent = true;
3841 if (exprs[i]->isValueDependent())
3842 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003843 if (exprs[i]->isInstantiationDependent())
3844 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003845 if (exprs[i]->containsUnexpandedParameterPack())
3846 ExprBits.ContainsUnexpandedParameterPack = true;
3847
Nate Begeman2ef13e52009-08-10 23:49:36 +00003848 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003849 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003850}
3851
John McCalle996ffd2011-02-16 08:02:54 +00003852const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3853 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3854 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003855 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3856 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003857 e = cast<CXXConstructExpr>(e)->getArg(0);
3858 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3859 e = ice->getSubExpr();
3860 return cast<OpaqueValueExpr>(e);
3861}
3862
Craig Topper05ed1a02013-08-18 10:09:15 +00003863PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3864 EmptyShell sh,
John McCall4b9c2d22011-11-06 09:01:30 +00003865 unsigned numSemanticExprs) {
3866 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3867 (1 + numSemanticExprs) * sizeof(Expr*),
3868 llvm::alignOf<PseudoObjectExpr>());
3869 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3870}
3871
3872PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3873 : Expr(PseudoObjectExprClass, shell) {
3874 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3875}
3876
Craig Topper05ed1a02013-08-18 10:09:15 +00003877PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCall4b9c2d22011-11-06 09:01:30 +00003878 ArrayRef<Expr*> semantics,
3879 unsigned resultIndex) {
3880 assert(syntax && "no syntactic expression!");
3881 assert(semantics.size() && "no semantic expressions!");
3882
3883 QualType type;
3884 ExprValueKind VK;
3885 if (resultIndex == NoResult) {
3886 type = C.VoidTy;
3887 VK = VK_RValue;
3888 } else {
3889 assert(resultIndex < semantics.size());
3890 type = semantics[resultIndex]->getType();
3891 VK = semantics[resultIndex]->getValueKind();
3892 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3893 }
3894
3895 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3896 (1 + semantics.size()) * sizeof(Expr*),
3897 llvm::alignOf<PseudoObjectExpr>());
3898 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3899 resultIndex);
3900}
3901
3902PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3903 Expr *syntax, ArrayRef<Expr*> semantics,
3904 unsigned resultIndex)
3905 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3906 /*filled in at end of ctor*/ false, false, false, false) {
3907 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3908 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3909
3910 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3911 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3912 getSubExprsBuffer()[i] = E;
3913
3914 if (E->isTypeDependent())
3915 ExprBits.TypeDependent = true;
3916 if (E->isValueDependent())
3917 ExprBits.ValueDependent = true;
3918 if (E->isInstantiationDependent())
3919 ExprBits.InstantiationDependent = true;
3920 if (E->containsUnexpandedParameterPack())
3921 ExprBits.ContainsUnexpandedParameterPack = true;
3922
3923 if (isa<OpaqueValueExpr>(E))
3924 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3925 "opaque-value semantic expressions for pseudo-object "
3926 "operations must have sources");
3927 }
3928}
3929
Douglas Gregor05c13a32009-01-22 00:58:24 +00003930//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003931// ExprIterator.
3932//===----------------------------------------------------------------------===//
3933
3934Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3935Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3936Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3937const Expr* ConstExprIterator::operator[](size_t idx) const {
3938 return cast<Expr>(I[idx]);
3939}
3940const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3941const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3942
3943//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003944// Child Iterators for iterating over subexpressions/substatements
3945//===----------------------------------------------------------------------===//
3946
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003947// UnaryExprOrTypeTraitExpr
3948Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003949 // If this is of a type and the type is a VLA type (and not a typedef), the
3950 // size expression of the VLA needs to be treated as an executable expression.
3951 // Why isn't this weirdness documented better in StmtIterator?
3952 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003953 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003954 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003955 return child_range(child_iterator(T), child_iterator());
3956 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003957 }
John McCall63c00d72011-02-09 08:16:59 +00003958 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003959}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003960
Steve Naroff563477d2007-09-18 23:55:05 +00003961// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003962Stmt::child_range ObjCMessageExpr::children() {
3963 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003964 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003965 begin = reinterpret_cast<Stmt **>(this + 1);
3966 else
3967 begin = reinterpret_cast<Stmt **>(getArgs());
3968 return child_range(begin,
3969 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003970}
3971
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003972ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003973 QualType T, ObjCMethodDecl *Method,
3974 SourceRange SR)
3975 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3976 false, false, false, false),
3977 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3978{
3979 Expr **SaveElements = getElements();
3980 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3981 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3982 ExprBits.ValueDependent = true;
3983 if (Elements[I]->isInstantiationDependent())
3984 ExprBits.InstantiationDependent = true;
3985 if (Elements[I]->containsUnexpandedParameterPack())
3986 ExprBits.ContainsUnexpandedParameterPack = true;
3987
3988 SaveElements[I] = Elements[I];
3989 }
3990}
3991
Craig Topper9db7a7e2013-08-22 04:58:56 +00003992ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003993 ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003994 QualType T, ObjCMethodDecl * Method,
3995 SourceRange SR) {
3996 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3997 + Elements.size() * sizeof(Expr *));
3998 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3999}
4000
Craig Topper9db7a7e2013-08-22 04:58:56 +00004001ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004002 unsigned NumElements) {
4003
4004 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4005 + NumElements * sizeof(Expr *));
4006 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4007}
4008
4009ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4010 ArrayRef<ObjCDictionaryElement> VK,
4011 bool HasPackExpansions,
4012 QualType T, ObjCMethodDecl *method,
4013 SourceRange SR)
4014 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4015 false, false),
4016 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4017 DictWithObjectsMethod(method)
4018{
4019 KeyValuePair *KeyValues = getKeyValues();
4020 ExpansionData *Expansions = getExpansionData();
4021 for (unsigned I = 0; I < NumElements; I++) {
4022 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4023 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4024 ExprBits.ValueDependent = true;
4025 if (VK[I].Key->isInstantiationDependent() ||
4026 VK[I].Value->isInstantiationDependent())
4027 ExprBits.InstantiationDependent = true;
4028 if (VK[I].EllipsisLoc.isInvalid() &&
4029 (VK[I].Key->containsUnexpandedParameterPack() ||
4030 VK[I].Value->containsUnexpandedParameterPack()))
4031 ExprBits.ContainsUnexpandedParameterPack = true;
4032
4033 KeyValues[I].Key = VK[I].Key;
4034 KeyValues[I].Value = VK[I].Value;
4035 if (Expansions) {
4036 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4037 if (VK[I].NumExpansions)
4038 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4039 else
4040 Expansions[I].NumExpansionsPlusOne = 0;
4041 }
4042 }
4043}
4044
4045ObjCDictionaryLiteral *
Craig Topper9db7a7e2013-08-22 04:58:56 +00004046ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004047 ArrayRef<ObjCDictionaryElement> VK,
4048 bool HasPackExpansions,
4049 QualType T, ObjCMethodDecl *method,
4050 SourceRange SR) {
4051 unsigned ExpansionsSize = 0;
4052 if (HasPackExpansions)
4053 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4054
4055 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4056 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4057 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4058}
4059
4060ObjCDictionaryLiteral *
Craig Topper9db7a7e2013-08-22 04:58:56 +00004061ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004062 bool HasPackExpansions) {
4063 unsigned ExpansionsSize = 0;
4064 if (HasPackExpansions)
4065 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4066 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4067 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4068 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4069 HasPackExpansions);
4070}
4071
Craig Topper9db7a7e2013-08-22 04:58:56 +00004072ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004073 Expr *base,
4074 Expr *key, QualType T,
4075 ObjCMethodDecl *getMethod,
4076 ObjCMethodDecl *setMethod,
4077 SourceLocation RB) {
4078 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4079 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4080 OK_ObjCSubscript,
4081 getMethod, setMethod, RB);
4082}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004083
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004084AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004085 QualType t, AtomicOp op, SourceLocation RP)
4086 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4087 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004088 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004089{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004090 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4091 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004092 if (args[i]->isTypeDependent())
4093 ExprBits.TypeDependent = true;
4094 if (args[i]->isValueDependent())
4095 ExprBits.ValueDependent = true;
4096 if (args[i]->isInstantiationDependent())
4097 ExprBits.InstantiationDependent = true;
4098 if (args[i]->containsUnexpandedParameterPack())
4099 ExprBits.ContainsUnexpandedParameterPack = true;
4100
4101 SubExprs[i] = args[i];
4102 }
4103}
Richard Smithe1b2abc2012-04-10 22:49:28 +00004104
4105unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4106 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00004107 case AO__c11_atomic_init:
4108 case AO__c11_atomic_load:
4109 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004110 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00004111
4112 case AO__c11_atomic_store:
4113 case AO__c11_atomic_exchange:
4114 case AO__atomic_load:
4115 case AO__atomic_store:
4116 case AO__atomic_store_n:
4117 case AO__atomic_exchange_n:
4118 case AO__c11_atomic_fetch_add:
4119 case AO__c11_atomic_fetch_sub:
4120 case AO__c11_atomic_fetch_and:
4121 case AO__c11_atomic_fetch_or:
4122 case AO__c11_atomic_fetch_xor:
4123 case AO__atomic_fetch_add:
4124 case AO__atomic_fetch_sub:
4125 case AO__atomic_fetch_and:
4126 case AO__atomic_fetch_or:
4127 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00004128 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00004129 case AO__atomic_add_fetch:
4130 case AO__atomic_sub_fetch:
4131 case AO__atomic_and_fetch:
4132 case AO__atomic_or_fetch:
4133 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00004134 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004135 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00004136
4137 case AO__atomic_exchange:
4138 return 4;
4139
4140 case AO__c11_atomic_compare_exchange_strong:
4141 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004142 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00004143
4144 case AO__atomic_compare_exchange:
4145 case AO__atomic_compare_exchange_n:
4146 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00004147 }
4148 llvm_unreachable("unknown atomic op");
4149}