blob: 0ba117e3e2a02e6d5f5ffd977f76d6e6bfe8de2f [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 }
607 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000608 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000609 llvm::raw_svector_ostream Out(Name);
610 Out << (MD->isInstanceMethod() ? '-' : '+');
611 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000612
613 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
614 // a null check to avoid a crash.
615 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000616 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000617
Anders Carlsson3a082d82009-09-08 18:24:21 +0000618 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000619 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000620 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000621
Anders Carlsson3a082d82009-09-08 18:24:21 +0000622 Out << ' ';
623 Out << MD->getSelector().getAsString();
624 Out << ']';
625
626 Out.flush();
627 return Name.str().str();
628 }
629 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
630 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
631 return "top level";
632 }
633 return "";
634}
635
Craig Topper05ed1a02013-08-18 10:09:15 +0000636void APNumericStorage::setIntValue(const ASTContext &C,
637 const llvm::APInt &Val) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000638 if (hasAllocation())
639 C.Deallocate(pVal);
640
641 BitWidth = Val.getBitWidth();
642 unsigned NumWords = Val.getNumWords();
643 const uint64_t* Words = Val.getRawData();
644 if (NumWords > 1) {
645 pVal = new (C) uint64_t[NumWords];
646 std::copy(Words, Words + NumWords, pVal);
647 } else if (NumWords == 1)
648 VAL = Words[0];
649 else
650 VAL = 0;
651}
652
Craig Topper05ed1a02013-08-18 10:09:15 +0000653IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000654 QualType type, SourceLocation l)
655 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
656 false, false),
657 Loc(l) {
658 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
659 assert(V.getBitWidth() == C.getIntWidth(type) &&
660 "Integer type is not the correct size for constant.");
661 setValue(C, V);
662}
663
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000664IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000665IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000666 QualType type, SourceLocation l) {
667 return new (C) IntegerLiteral(C, V, type, l);
668}
669
670IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000671IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000672 return new (C) IntegerLiteral(Empty);
673}
674
Craig Topper05ed1a02013-08-18 10:09:15 +0000675FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000676 bool isexact, QualType Type, SourceLocation L)
677 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
678 false, false), Loc(L) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000679 setSemantics(V.getSemantics());
Benjamin Kramer478851c2012-07-04 17:04:04 +0000680 FloatingLiteralBits.IsExact = isexact;
681 setValue(C, V);
682}
683
Craig Topper05ed1a02013-08-18 10:09:15 +0000684FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer478851c2012-07-04 17:04:04 +0000685 : Expr(FloatingLiteralClass, Empty) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000686 setRawSemantics(IEEEhalf);
Benjamin Kramer478851c2012-07-04 17:04:04 +0000687 FloatingLiteralBits.IsExact = false;
688}
689
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000690FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000691FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000692 bool isexact, QualType Type, SourceLocation L) {
693 return new (C) FloatingLiteral(C, V, isexact, Type, L);
694}
695
696FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000697FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000698 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000699}
700
Tim Northover9ec55f22013-01-22 09:46:51 +0000701const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
702 switch(FloatingLiteralBits.Semantics) {
703 case IEEEhalf:
704 return llvm::APFloat::IEEEhalf;
705 case IEEEsingle:
706 return llvm::APFloat::IEEEsingle;
707 case IEEEdouble:
708 return llvm::APFloat::IEEEdouble;
709 case x87DoubleExtended:
710 return llvm::APFloat::x87DoubleExtended;
711 case IEEEquad:
712 return llvm::APFloat::IEEEquad;
713 case PPCDoubleDouble:
714 return llvm::APFloat::PPCDoubleDouble;
715 }
716 llvm_unreachable("Unrecognised floating semantics");
717}
718
719void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
720 if (&Sem == &llvm::APFloat::IEEEhalf)
721 FloatingLiteralBits.Semantics = IEEEhalf;
722 else if (&Sem == &llvm::APFloat::IEEEsingle)
723 FloatingLiteralBits.Semantics = IEEEsingle;
724 else if (&Sem == &llvm::APFloat::IEEEdouble)
725 FloatingLiteralBits.Semantics = IEEEdouble;
726 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
727 FloatingLiteralBits.Semantics = x87DoubleExtended;
728 else if (&Sem == &llvm::APFloat::IEEEquad)
729 FloatingLiteralBits.Semantics = IEEEquad;
730 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
731 FloatingLiteralBits.Semantics = PPCDoubleDouble;
732 else
733 llvm_unreachable("Unknown floating semantics");
734}
735
Chris Lattnerda8249e2008-06-07 22:13:43 +0000736/// getValueAsApproximateDouble - This returns the value as an inaccurate
737/// double. Note that this may cause loss of precision, but is useful for
738/// debugging dumps, etc.
739double FloatingLiteral::getValueAsApproximateDouble() const {
740 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000741 bool ignored;
742 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
743 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000744 return V.convertToDouble();
745}
746
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000747int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000748 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000749 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000750 case Ascii:
751 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000752 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000753 break;
754 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000755 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000756 break;
757 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000758 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000759 break;
760 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000761 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000762 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000763 }
764 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
765 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000766 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000767 && "character byte widths supported are 1, 2, and 4 only");
768 return CharByteWidth;
769}
770
Craig Topper05ed1a02013-08-18 10:09:15 +0000771StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000772 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000773 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000774 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000775 // Allocate enough space for the StringLiteral plus an array of locations for
776 // any concatenated string tokens.
777 void *Mem = C.Allocate(sizeof(StringLiteral)+
778 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000779 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000780 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000783 SL->setString(C,Str,Kind,Pascal);
784
Chris Lattner2085fd62009-02-18 06:40:38 +0000785 SL->TokLocs[0] = Loc[0];
786 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000787
Chris Lattner726e1682009-02-18 05:49:11 +0000788 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000789 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
790 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000791}
792
Craig Topper05ed1a02013-08-18 10:09:15 +0000793StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
794 unsigned NumStrs) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000795 void *Mem = C.Allocate(sizeof(StringLiteral)+
796 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000797 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000798 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000799 SL->CharByteWidth = 0;
800 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000801 SL->NumConcatenated = NumStrs;
802 return SL;
803}
804
Alexander Kornienkoae541212013-02-01 12:35:51 +0000805void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieu8ab09da2012-06-13 20:25:24 +0000806 switch (getKind()) {
807 case Ascii: break; // no prefix.
808 case Wide: OS << 'L'; break;
809 case UTF8: OS << "u8"; break;
810 case UTF16: OS << 'u'; break;
811 case UTF32: OS << 'U'; break;
812 }
813 OS << '"';
814 static const char Hex[] = "0123456789ABCDEF";
815
816 unsigned LastSlashX = getLength();
817 for (unsigned I = 0, N = getLength(); I != N; ++I) {
818 switch (uint32_t Char = getCodeUnit(I)) {
819 default:
820 // FIXME: Convert UTF-8 back to codepoints before rendering.
821
822 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
823 // Leave invalid surrogates alone; we'll use \x for those.
824 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
825 Char <= 0xdbff) {
826 uint32_t Trail = getCodeUnit(I + 1);
827 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
828 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
829 ++I;
830 }
831 }
832
833 if (Char > 0xff) {
834 // If this is a wide string, output characters over 0xff using \x
835 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
836 // codepoint: use \x escapes for invalid codepoints.
837 if (getKind() == Wide ||
838 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
839 // FIXME: Is this the best way to print wchar_t?
840 OS << "\\x";
841 int Shift = 28;
842 while ((Char >> Shift) == 0)
843 Shift -= 4;
844 for (/**/; Shift >= 0; Shift -= 4)
845 OS << Hex[(Char >> Shift) & 15];
846 LastSlashX = I;
847 break;
848 }
849
850 if (Char > 0xffff)
851 OS << "\\U00"
852 << Hex[(Char >> 20) & 15]
853 << Hex[(Char >> 16) & 15];
854 else
855 OS << "\\u";
856 OS << Hex[(Char >> 12) & 15]
857 << Hex[(Char >> 8) & 15]
858 << Hex[(Char >> 4) & 15]
859 << Hex[(Char >> 0) & 15];
860 break;
861 }
862
863 // If we used \x... for the previous character, and this character is a
864 // hexadecimal digit, prevent it being slurped as part of the \x.
865 if (LastSlashX + 1 == I) {
866 switch (Char) {
867 case '0': case '1': case '2': case '3': case '4':
868 case '5': case '6': case '7': case '8': case '9':
869 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
870 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
871 OS << "\"\"";
872 }
873 }
874
875 assert(Char <= 0xff &&
876 "Characters above 0xff should already have been handled.");
877
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000878 if (isPrintable(Char))
Richard Trieu8ab09da2012-06-13 20:25:24 +0000879 OS << (char)Char;
880 else // Output anything hard as an octal escape.
881 OS << '\\'
882 << (char)('0' + ((Char >> 6) & 7))
883 << (char)('0' + ((Char >> 3) & 7))
884 << (char)('0' + ((Char >> 0) & 7));
885 break;
886 // Handle some common non-printable cases to make dumps prettier.
887 case '\\': OS << "\\\\"; break;
888 case '"': OS << "\\\""; break;
889 case '\n': OS << "\\n"; break;
890 case '\t': OS << "\\t"; break;
891 case '\a': OS << "\\a"; break;
892 case '\b': OS << "\\b"; break;
893 }
894 }
895 OS << '"';
896}
897
Craig Topper05ed1a02013-08-18 10:09:15 +0000898void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedman64f45a22011-11-01 02:23:42 +0000899 StringKind Kind, bool IsPascal) {
900 //FIXME: we assume that the string data comes from a target that uses the same
901 // code unit size and endianess for the type of string.
902 this->Kind = Kind;
903 this->IsPascal = IsPascal;
904
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000905 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000906 assert((Str.size()%CharByteWidth == 0)
907 && "size of data must be multiple of CharByteWidth");
908 Length = Str.size()/CharByteWidth;
909
910 switch(CharByteWidth) {
911 case 1: {
912 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000913 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000914 StrData.asChar = AStrData;
915 break;
916 }
917 case 2: {
918 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000919 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000920 StrData.asUInt16 = AStrData;
921 break;
922 }
923 case 4: {
924 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000925 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000926 StrData.asUInt32 = AStrData;
927 break;
928 }
929 default:
930 assert(false && "unsupported CharByteWidth");
931 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000932}
933
Chris Lattner08f92e32010-11-17 07:37:15 +0000934/// getLocationOfByte - Return a source location that points to the specified
935/// byte of this string literal.
936///
937/// Strings are amazingly complex. They can be formed from multiple tokens and
938/// can have escape sequences in them in addition to the usual trigraph and
939/// escaped newline business. This routine handles this complexity.
940///
941SourceLocation StringLiteral::
942getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
943 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000944 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
945 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000946
Chris Lattner08f92e32010-11-17 07:37:15 +0000947 // Loop over all of the tokens in this string until we find the one that
948 // contains the byte we're looking for.
949 unsigned TokNo = 0;
950 while (1) {
951 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
952 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
953
954 // Get the spelling of the string so that we can get the data that makes up
955 // the string literal, not the identifier for the macro it is potentially
956 // expanded through.
957 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
958
959 // Re-lex the token to get its length and original spelling.
960 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
961 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000962 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000963 if (Invalid)
964 return StrTokSpellingLoc;
965
966 const char *StrData = Buffer.data()+LocInfo.second;
967
Chris Lattner08f92e32010-11-17 07:37:15 +0000968 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000969 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
970 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000971 Token TheTok;
972 TheLexer.LexFromRawLexer(TheTok);
973
974 // Use the StringLiteralParser to compute the length of the string in bytes.
975 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
976 unsigned TokNumBytes = SLP.GetStringLength();
977
978 // If the byte is in this token, return the location of the byte.
979 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000980 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000981 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
982
983 // Now that we know the offset of the token in the spelling, use the
984 // preprocessor to get the offset in the original source.
985 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
986 }
987
988 // Move to the next string token.
989 ++TokNo;
990 ByteNo -= TokNumBytes;
991 }
992}
993
994
995
Reid Spencer5f016e22007-07-11 17:01:13 +0000996/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
997/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie0bea8632012-10-08 01:11:04 +0000998StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001000 case UO_PostInc: return "++";
1001 case UO_PostDec: return "--";
1002 case UO_PreInc: return "++";
1003 case UO_PreDec: return "--";
1004 case UO_AddrOf: return "&";
1005 case UO_Deref: return "*";
1006 case UO_Plus: return "+";
1007 case UO_Minus: return "-";
1008 case UO_Not: return "~";
1009 case UO_LNot: return "!";
1010 case UO_Real: return "__real";
1011 case UO_Imag: return "__imag";
1012 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001014 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +00001015}
1016
John McCall2de56d12010-08-25 11:45:40 +00001017UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001018UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1019 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001020 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +00001021 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1022 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1023 case OO_Amp: return UO_AddrOf;
1024 case OO_Star: return UO_Deref;
1025 case OO_Plus: return UO_Plus;
1026 case OO_Minus: return UO_Minus;
1027 case OO_Tilde: return UO_Not;
1028 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001029 }
1030}
1031
1032OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1033 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +00001034 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1035 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1036 case UO_AddrOf: return OO_Amp;
1037 case UO_Deref: return OO_Star;
1038 case UO_Plus: return OO_Plus;
1039 case UO_Minus: return OO_Minus;
1040 case UO_Not: return OO_Tilde;
1041 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001042 default: return OO_None;
1043 }
1044}
1045
1046
Reid Spencer5f016e22007-07-11 17:01:13 +00001047//===----------------------------------------------------------------------===//
1048// Postfix Operators.
1049//===----------------------------------------------------------------------===//
1050
Craig Topper05ed1a02013-08-18 10:09:15 +00001051CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1052 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1053 ExprValueKind VK, SourceLocation rparenloc)
John McCallf89e55a2010-11-18 06:31:45 +00001054 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001055 fn->isTypeDependent(),
1056 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001057 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001058 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001059 NumArgs(args.size()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001061 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +00001062 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001063 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001064 if (args[i]->isTypeDependent())
1065 ExprBits.TypeDependent = true;
1066 if (args[i]->isValueDependent())
1067 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001068 if (args[i]->isInstantiationDependent())
1069 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001070 if (args[i]->containsUnexpandedParameterPack())
1071 ExprBits.ContainsUnexpandedParameterPack = true;
1072
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001073 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001074 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001075
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001076 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +00001077 RParenLoc = rparenloc;
1078}
Nate Begemane2ce1d92008-01-17 17:46:27 +00001079
Craig Topper05ed1a02013-08-18 10:09:15 +00001080CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCallf89e55a2010-11-18 06:31:45 +00001081 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1082 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001083 fn->isTypeDependent(),
1084 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001085 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001086 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001087 NumArgs(args.size()) {
Ted Kremenek668bf912009-02-09 20:51:47 +00001088
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001089 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001090 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001091 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001092 if (args[i]->isTypeDependent())
1093 ExprBits.TypeDependent = true;
1094 if (args[i]->isValueDependent())
1095 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001096 if (args[i]->isInstantiationDependent())
1097 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001098 if (args[i]->containsUnexpandedParameterPack())
1099 ExprBits.ContainsUnexpandedParameterPack = true;
1100
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001101 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001102 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001103
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001104 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 RParenLoc = rparenloc;
1106}
1107
Craig Topper05ed1a02013-08-18 10:09:15 +00001108CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Mike Stump1eb44332009-09-09 15:08:12 +00001109 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001110 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001111 SubExprs = new (C) Stmt*[PREARGS_START];
1112 CallExprBits.NumPreArgs = 0;
1113}
1114
Craig Topper05ed1a02013-08-18 10:09:15 +00001115CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001116 EmptyShell Empty)
1117 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1118 // FIXME: Why do we allocate this?
1119 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1120 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001121}
1122
Nuno Lopesd20254f2009-12-20 23:11:08 +00001123Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +00001124 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +00001125
1126 while (SubstNonTypeTemplateParmExpr *NTTP
1127 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1128 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1129 }
1130
Sebastian Redl20012152010-09-10 20:55:30 +00001131 // If we're calling a dereference, look at the pointer instead.
1132 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1133 if (BO->isPtrMemOp())
1134 CEE = BO->getRHS()->IgnoreParenCasts();
1135 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1136 if (UO->getOpcode() == UO_Deref)
1137 CEE = UO->getSubExpr()->IgnoreParenCasts();
1138 }
Chris Lattner6346f962009-07-17 15:46:27 +00001139 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001140 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001141 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1142 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001143
1144 return 0;
1145}
1146
Nuno Lopesd20254f2009-12-20 23:11:08 +00001147FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001148 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001149}
1150
Chris Lattnerd18b3292007-12-28 05:25:02 +00001151/// setNumArgs - This changes the number of arguments present in this call.
1152/// Any orphaned expressions are deleted by this, and any new operands are set
1153/// to null.
Craig Topper05ed1a02013-08-18 10:09:15 +00001154void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001155 // No change, just return.
1156 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattnerd18b3292007-12-28 05:25:02 +00001158 // If shrinking # arguments, just delete the extras and forgot them.
1159 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001160 this->NumArgs = NumArgs;
1161 return;
1162 }
1163
1164 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001165 unsigned NumPreArgs = getNumPreArgs();
1166 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001167 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001168 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001169 NewSubExprs[i] = SubExprs[i];
1170 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001171 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1172 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001173 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregor88c9a462009-04-17 21:46:47 +00001175 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001176 SubExprs = NewSubExprs;
1177 this->NumArgs = NumArgs;
1178}
1179
Chris Lattnercb888962008-10-06 05:00:53 +00001180/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1181/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001182unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001183 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001184 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001185 // ImplicitCastExpr.
1186 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1187 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001188 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001190 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1191 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001192 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Anders Carlssonbcba2012008-01-31 02:13:57 +00001194 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1195 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001196 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001198 if (!FDecl->getIdentifier())
1199 return 0;
1200
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001201 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001202}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001203
Richard Smithba571832013-01-17 23:46:04 +00001204bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1205 if (unsigned BI = isBuiltinCall())
1206 return Ctx.BuiltinInfo.isUnevaluated(BI);
1207 return false;
1208}
1209
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001210QualType CallExpr::getCallReturnType() const {
1211 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001212 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001213 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001214 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001215 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001216 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1217 // This should never be overloaded and so should never return null.
1218 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001219
John McCall864c0412011-04-26 20:42:42 +00001220 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001221 return FnType->getResultType();
1222}
Chris Lattnercb888962008-10-06 05:00:53 +00001223
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001224SourceLocation CallExpr::getLocStart() const {
1225 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001226 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001227
1228 SourceLocation begin = getCallee()->getLocStart();
1229 if (begin.isInvalid() && getNumArgs() > 0)
1230 begin = getArg(0)->getLocStart();
1231 return begin;
1232}
1233SourceLocation CallExpr::getLocEnd() const {
1234 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001235 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001236
1237 SourceLocation end = getRParenLoc();
1238 if (end.isInvalid() && getNumArgs() > 0)
1239 end = getArg(getNumArgs() - 1)->getLocEnd();
1240 return end;
1241}
John McCall2882eca2011-02-21 06:23:05 +00001242
Craig Topper05ed1a02013-08-18 10:09:15 +00001243OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001244 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001245 TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001246 ArrayRef<OffsetOfNode> comps,
1247 ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001248 SourceLocation RParenLoc) {
1249 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001250 sizeof(OffsetOfNode) * comps.size() +
1251 sizeof(Expr*) * exprs.size());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001252
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001253 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1254 RParenLoc);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001255}
1256
Craig Topper05ed1a02013-08-18 10:09:15 +00001257OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001258 unsigned numComps, unsigned numExprs) {
1259 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1260 sizeof(OffsetOfNode) * numComps +
1261 sizeof(Expr*) * numExprs);
1262 return new (Mem) OffsetOfExpr(numComps, numExprs);
1263}
1264
Craig Topper05ed1a02013-08-18 10:09:15 +00001265OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001266 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001267 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001268 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001269 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1270 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001271 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001272 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001273 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001274 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001275 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001276{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001277 for (unsigned i = 0; i != comps.size(); ++i) {
1278 setComponent(i, comps[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001279 }
Sean Huntc3021132010-05-05 15:23:54 +00001280
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001281 for (unsigned i = 0; i != exprs.size(); ++i) {
1282 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001283 ExprBits.ValueDependent = true;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001284 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001285 ExprBits.ContainsUnexpandedParameterPack = true;
1286
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001287 setIndexExpr(i, exprs[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001288 }
1289}
1290
1291IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1292 assert(getKind() == Field || getKind() == Identifier);
1293 if (getKind() == Field)
1294 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001295
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001296 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1297}
1298
Craig Topper05ed1a02013-08-18 10:09:15 +00001299MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001300 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001301 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001302 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001303 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001304 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001305 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001306 QualType ty,
1307 ExprValueKind vk,
1308 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001309 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001310
Douglas Gregor40d96a62011-02-28 21:54:11 +00001311 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001312 founddecl.getDecl() != memberdecl ||
1313 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001314 if (hasQualOrFound)
1315 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001316
John McCalld5532b62009-11-23 01:53:49 +00001317 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001318 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1319 else if (TemplateKWLoc.isValid())
1320 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Chris Lattner32488542010-10-30 05:14:06 +00001322 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001323 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1324 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001325
1326 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001327 // FIXME: Wrong. We should be looking at the member declaration we found.
1328 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001329 E->setValueDependent(true);
1330 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001331 E->setInstantiationDependent(true);
1332 }
1333 else if (QualifierLoc &&
1334 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1335 E->setInstantiationDependent(true);
1336
John McCall6bb80172010-03-30 21:47:33 +00001337 E->HasQualifierOrFoundDecl = true;
1338
1339 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001340 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001341 NQ->FoundDecl = founddecl;
1342 }
1343
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001344 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1345
John McCall6bb80172010-03-30 21:47:33 +00001346 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001347 bool Dependent = false;
1348 bool InstantiationDependent = false;
1349 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001350 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1351 Dependent,
1352 InstantiationDependent,
1353 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001354 if (InstantiationDependent)
1355 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001356 } else if (TemplateKWLoc.isValid()) {
1357 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001358 }
1359
1360 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001361}
1362
Daniel Dunbar396ec672012-03-09 15:39:15 +00001363SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001364 if (isImplicitAccess()) {
1365 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001366 return getQualifierLoc().getBeginLoc();
1367 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001368 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001369
Daniel Dunbar396ec672012-03-09 15:39:15 +00001370 // FIXME: We don't want this to happen. Rather, we should be able to
1371 // detect all kinds of implicit accesses more cleanly.
1372 SourceLocation BaseStartLoc = getBase()->getLocStart();
1373 if (BaseStartLoc.isValid())
1374 return BaseStartLoc;
1375 return MemberLoc;
1376}
1377SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001378 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbar396ec672012-03-09 15:39:15 +00001379 if (hasExplicitTemplateArgs())
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001380 EndLoc = getRAngleLoc();
1381 else if (EndLoc.isInvalid())
1382 EndLoc = getBase()->getLocEnd();
1383 return EndLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001384}
1385
John McCall1d9b3b22011-09-09 05:25:32 +00001386void CastExpr::CheckCastConsistency() const {
1387 switch (getCastKind()) {
1388 case CK_DerivedToBase:
1389 case CK_UncheckedDerivedToBase:
1390 case CK_DerivedToBaseMemberPointer:
1391 case CK_BaseToDerived:
1392 case CK_BaseToDerivedMemberPointer:
1393 assert(!path_empty() && "Cast kind should have a base path!");
1394 break;
1395
1396 case CK_CPointerToObjCPointerCast:
1397 assert(getType()->isObjCObjectPointerType());
1398 assert(getSubExpr()->getType()->isPointerType());
1399 goto CheckNoBasePath;
1400
1401 case CK_BlockPointerToObjCPointerCast:
1402 assert(getType()->isObjCObjectPointerType());
1403 assert(getSubExpr()->getType()->isBlockPointerType());
1404 goto CheckNoBasePath;
1405
John McCall4d4e5c12012-02-15 01:22:51 +00001406 case CK_ReinterpretMemberPointer:
1407 assert(getType()->isMemberPointerType());
1408 assert(getSubExpr()->getType()->isMemberPointerType());
1409 goto CheckNoBasePath;
1410
John McCall1d9b3b22011-09-09 05:25:32 +00001411 case CK_BitCast:
1412 // Arbitrary casts to C pointer types count as bitcasts.
1413 // Otherwise, we should only have block and ObjC pointer casts
1414 // here if they stay within the type kind.
1415 if (!getType()->isPointerType()) {
1416 assert(getType()->isObjCObjectPointerType() ==
1417 getSubExpr()->getType()->isObjCObjectPointerType());
1418 assert(getType()->isBlockPointerType() ==
1419 getSubExpr()->getType()->isBlockPointerType());
1420 }
1421 goto CheckNoBasePath;
1422
1423 case CK_AnyPointerToBlockPointerCast:
1424 assert(getType()->isBlockPointerType());
1425 assert(getSubExpr()->getType()->isAnyPointerType() &&
1426 !getSubExpr()->getType()->isBlockPointerType());
1427 goto CheckNoBasePath;
1428
Douglas Gregorac1303e2012-02-22 05:02:47 +00001429 case CK_CopyAndAutoreleaseBlockObject:
1430 assert(getType()->isBlockPointerType());
1431 assert(getSubExpr()->getType()->isBlockPointerType());
1432 goto CheckNoBasePath;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001433
1434 case CK_FunctionToPointerDecay:
1435 assert(getType()->isPointerType());
1436 assert(getSubExpr()->getType()->isFunctionType());
1437 goto CheckNoBasePath;
1438
John McCall1d9b3b22011-09-09 05:25:32 +00001439 // These should not have an inheritance path.
1440 case CK_Dynamic:
1441 case CK_ToUnion:
1442 case CK_ArrayToPointerDecay:
John McCall1d9b3b22011-09-09 05:25:32 +00001443 case CK_NullToMemberPointer:
1444 case CK_NullToPointer:
1445 case CK_ConstructorConversion:
1446 case CK_IntegralToPointer:
1447 case CK_PointerToIntegral:
1448 case CK_ToVoid:
1449 case CK_VectorSplat:
1450 case CK_IntegralCast:
1451 case CK_IntegralToFloating:
1452 case CK_FloatingToIntegral:
1453 case CK_FloatingCast:
1454 case CK_ObjCObjectLValueCast:
1455 case CK_FloatingRealToComplex:
1456 case CK_FloatingComplexToReal:
1457 case CK_FloatingComplexCast:
1458 case CK_FloatingComplexToIntegralComplex:
1459 case CK_IntegralRealToComplex:
1460 case CK_IntegralComplexToReal:
1461 case CK_IntegralComplexCast:
1462 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001463 case CK_ARCProduceObject:
1464 case CK_ARCConsumeObject:
1465 case CK_ARCReclaimReturnedObject:
1466 case CK_ARCExtendBlockObject:
Guy Benyeie6b9d802013-01-20 12:31:11 +00001467 case CK_ZeroToOCLEvent:
John McCall1d9b3b22011-09-09 05:25:32 +00001468 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1469 goto CheckNoBasePath;
1470
1471 case CK_Dependent:
1472 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001473 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001474 case CK_AtomicToNonAtomic:
1475 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001476 case CK_PointerToBoolean:
1477 case CK_IntegralToBoolean:
1478 case CK_FloatingToBoolean:
1479 case CK_MemberPointerToBoolean:
1480 case CK_FloatingComplexToBoolean:
1481 case CK_IntegralComplexToBoolean:
1482 case CK_LValueBitCast: // -> bool&
1483 case CK_UserDefinedConversion: // operator bool()
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001484 case CK_BuiltinFnToFnPtr:
John McCall1d9b3b22011-09-09 05:25:32 +00001485 CheckNoBasePath:
1486 assert(path_empty() && "Cast kind should not have a base path!");
1487 break;
1488 }
1489}
1490
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001491const char *CastExpr::getCastKindName() const {
1492 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001493 case CK_Dependent:
1494 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001495 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001496 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001497 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001498 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001499 case CK_LValueToRValue:
1500 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001501 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001502 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001503 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001504 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001505 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001506 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001507 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001508 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001509 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001510 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001511 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001512 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001513 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001514 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001515 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001516 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001517 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001518 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001519 case CK_NullToPointer:
1520 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001521 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001522 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001523 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001524 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001525 case CK_ReinterpretMemberPointer:
1526 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001527 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001528 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001529 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001530 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001531 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001532 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001533 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001534 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001535 case CK_PointerToBoolean:
1536 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001537 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001538 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001539 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001540 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001541 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001542 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001543 case CK_IntegralToBoolean:
1544 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001545 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001546 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001547 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001548 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001549 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001550 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001551 case CK_FloatingToBoolean:
1552 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001553 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001554 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001555 case CK_CPointerToObjCPointerCast:
1556 return "CPointerToObjCPointerCast";
1557 case CK_BlockPointerToObjCPointerCast:
1558 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001559 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001560 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001561 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001562 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001563 case CK_FloatingRealToComplex:
1564 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001565 case CK_FloatingComplexToReal:
1566 return "FloatingComplexToReal";
1567 case CK_FloatingComplexToBoolean:
1568 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001569 case CK_FloatingComplexCast:
1570 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001571 case CK_FloatingComplexToIntegralComplex:
1572 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001573 case CK_IntegralRealToComplex:
1574 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001575 case CK_IntegralComplexToReal:
1576 return "IntegralComplexToReal";
1577 case CK_IntegralComplexToBoolean:
1578 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001579 case CK_IntegralComplexCast:
1580 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001581 case CK_IntegralComplexToFloatingComplex:
1582 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001583 case CK_ARCConsumeObject:
1584 return "ARCConsumeObject";
1585 case CK_ARCProduceObject:
1586 return "ARCProduceObject";
1587 case CK_ARCReclaimReturnedObject:
1588 return "ARCReclaimReturnedObject";
1589 case CK_ARCExtendBlockObject:
1590 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001591 case CK_AtomicToNonAtomic:
1592 return "AtomicToNonAtomic";
1593 case CK_NonAtomicToAtomic:
1594 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001595 case CK_CopyAndAutoreleaseBlockObject:
1596 return "CopyAndAutoreleaseBlockObject";
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001597 case CK_BuiltinFnToFnPtr:
1598 return "BuiltinFnToFnPtr";
Guy Benyeie6b9d802013-01-20 12:31:11 +00001599 case CK_ZeroToOCLEvent:
1600 return "ZeroToOCLEvent";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001601 }
Mike Stump1eb44332009-09-09 15:08:12 +00001602
John McCall2bb5d002010-11-13 09:02:35 +00001603 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001604}
1605
Douglas Gregor6eef5192009-12-14 19:27:10 +00001606Expr *CastExpr::getSubExprAsWritten() {
1607 Expr *SubExpr = 0;
1608 CastExpr *E = this;
1609 do {
1610 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001611
1612 // Skip through reference binding to temporary.
1613 if (MaterializeTemporaryExpr *Materialize
1614 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1615 SubExpr = Materialize->GetTemporaryExpr();
1616
Douglas Gregor6eef5192009-12-14 19:27:10 +00001617 // Skip any temporary bindings; they're implicit.
1618 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1619 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001620
Douglas Gregor6eef5192009-12-14 19:27:10 +00001621 // Conversions by constructor and conversion functions have a
1622 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001623 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001624 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001625 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001626 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001627
Douglas Gregor6eef5192009-12-14 19:27:10 +00001628 // If the subexpression we're left with is an implicit cast, look
1629 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001630 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1631
Douglas Gregor6eef5192009-12-14 19:27:10 +00001632 return SubExpr;
1633}
1634
John McCallf871d0c2010-08-07 06:22:56 +00001635CXXBaseSpecifier **CastExpr::path_buffer() {
1636 switch (getStmtClass()) {
1637#define ABSTRACT_STMT(x)
1638#define CASTEXPR(Type, Base) \
1639 case Stmt::Type##Class: \
1640 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1641#define STMT(Type, Base)
1642#include "clang/AST/StmtNodes.inc"
1643 default:
1644 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001645 }
1646}
1647
1648void CastExpr::setCastPath(const CXXCastPath &Path) {
1649 assert(Path.size() == path_size());
1650 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1651}
1652
Craig Topper05ed1a02013-08-18 10:09:15 +00001653ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallf871d0c2010-08-07 06:22:56 +00001654 CastKind Kind, Expr *Operand,
1655 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001656 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001657 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1658 void *Buffer =
1659 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1660 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001661 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001662 if (PathSize) E->setCastPath(*BasePath);
1663 return E;
1664}
1665
Craig Topper05ed1a02013-08-18 10:09:15 +00001666ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallf871d0c2010-08-07 06:22:56 +00001667 unsigned PathSize) {
1668 void *Buffer =
1669 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1670 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1671}
1672
1673
Craig Topper05ed1a02013-08-18 10:09:15 +00001674CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001675 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001676 const CXXCastPath *BasePath,
1677 TypeSourceInfo *WrittenTy,
1678 SourceLocation L, SourceLocation R) {
1679 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1680 void *Buffer =
1681 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1682 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001683 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001684 if (PathSize) E->setCastPath(*BasePath);
1685 return E;
1686}
1687
Craig Topper05ed1a02013-08-18 10:09:15 +00001688CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1689 unsigned PathSize) {
John McCallf871d0c2010-08-07 06:22:56 +00001690 void *Buffer =
1691 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1692 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1693}
1694
Reid Spencer5f016e22007-07-11 17:01:13 +00001695/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1696/// corresponds to, e.g. "<<=".
David Blaikie0bea8632012-10-08 01:11:04 +00001697StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001699 case BO_PtrMemD: return ".*";
1700 case BO_PtrMemI: return "->*";
1701 case BO_Mul: return "*";
1702 case BO_Div: return "/";
1703 case BO_Rem: return "%";
1704 case BO_Add: return "+";
1705 case BO_Sub: return "-";
1706 case BO_Shl: return "<<";
1707 case BO_Shr: return ">>";
1708 case BO_LT: return "<";
1709 case BO_GT: return ">";
1710 case BO_LE: return "<=";
1711 case BO_GE: return ">=";
1712 case BO_EQ: return "==";
1713 case BO_NE: return "!=";
1714 case BO_And: return "&";
1715 case BO_Xor: return "^";
1716 case BO_Or: return "|";
1717 case BO_LAnd: return "&&";
1718 case BO_LOr: return "||";
1719 case BO_Assign: return "=";
1720 case BO_MulAssign: return "*=";
1721 case BO_DivAssign: return "/=";
1722 case BO_RemAssign: return "%=";
1723 case BO_AddAssign: return "+=";
1724 case BO_SubAssign: return "-=";
1725 case BO_ShlAssign: return "<<=";
1726 case BO_ShrAssign: return ">>=";
1727 case BO_AndAssign: return "&=";
1728 case BO_XorAssign: return "^=";
1729 case BO_OrAssign: return "|=";
1730 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001732
David Blaikie30263482012-01-20 21:50:17 +00001733 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001734}
1735
John McCall2de56d12010-08-25 11:45:40 +00001736BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001737BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1738 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001739 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001740 case OO_Plus: return BO_Add;
1741 case OO_Minus: return BO_Sub;
1742 case OO_Star: return BO_Mul;
1743 case OO_Slash: return BO_Div;
1744 case OO_Percent: return BO_Rem;
1745 case OO_Caret: return BO_Xor;
1746 case OO_Amp: return BO_And;
1747 case OO_Pipe: return BO_Or;
1748 case OO_Equal: return BO_Assign;
1749 case OO_Less: return BO_LT;
1750 case OO_Greater: return BO_GT;
1751 case OO_PlusEqual: return BO_AddAssign;
1752 case OO_MinusEqual: return BO_SubAssign;
1753 case OO_StarEqual: return BO_MulAssign;
1754 case OO_SlashEqual: return BO_DivAssign;
1755 case OO_PercentEqual: return BO_RemAssign;
1756 case OO_CaretEqual: return BO_XorAssign;
1757 case OO_AmpEqual: return BO_AndAssign;
1758 case OO_PipeEqual: return BO_OrAssign;
1759 case OO_LessLess: return BO_Shl;
1760 case OO_GreaterGreater: return BO_Shr;
1761 case OO_LessLessEqual: return BO_ShlAssign;
1762 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1763 case OO_EqualEqual: return BO_EQ;
1764 case OO_ExclaimEqual: return BO_NE;
1765 case OO_LessEqual: return BO_LE;
1766 case OO_GreaterEqual: return BO_GE;
1767 case OO_AmpAmp: return BO_LAnd;
1768 case OO_PipePipe: return BO_LOr;
1769 case OO_Comma: return BO_Comma;
1770 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001771 }
1772}
1773
1774OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1775 static const OverloadedOperatorKind OverOps[] = {
1776 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1777 OO_Star, OO_Slash, OO_Percent,
1778 OO_Plus, OO_Minus,
1779 OO_LessLess, OO_GreaterGreater,
1780 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1781 OO_EqualEqual, OO_ExclaimEqual,
1782 OO_Amp,
1783 OO_Caret,
1784 OO_Pipe,
1785 OO_AmpAmp,
1786 OO_PipePipe,
1787 OO_Equal, OO_StarEqual,
1788 OO_SlashEqual, OO_PercentEqual,
1789 OO_PlusEqual, OO_MinusEqual,
1790 OO_LessLessEqual, OO_GreaterGreaterEqual,
1791 OO_AmpEqual, OO_CaretEqual,
1792 OO_PipeEqual,
1793 OO_Comma
1794 };
1795 return OverOps[Opc];
1796}
1797
Craig Topper05ed1a02013-08-18 10:09:15 +00001798InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001799 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001800 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001801 false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001802 InitExprs(C, initExprs.size()),
Abramo Bagnara23700f02012-11-08 18:41:43 +00001803 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001804{
1805 sawArrayRangeDesignator(false);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001806 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001807 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001808 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001809 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001810 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001811 if (initExprs[I]->isInstantiationDependent())
1812 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001813 if (initExprs[I]->containsUnexpandedParameterPack())
1814 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001815 }
Sean Huntc3021132010-05-05 15:23:54 +00001816
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001817 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001818}
Reid Spencer5f016e22007-07-11 17:01:13 +00001819
Craig Topper05ed1a02013-08-18 10:09:15 +00001820void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001821 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001822 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001823}
1824
Craig Topper05ed1a02013-08-18 10:09:15 +00001825void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001826 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001827}
1828
Craig Topper05ed1a02013-08-18 10:09:15 +00001829Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001830 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001831 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001832 InitExprs.back() = expr;
1833 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Douglas Gregor4c678342009-01-28 21:54:33 +00001836 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1837 InitExprs[Init] = expr;
1838 return Result;
1839}
1840
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001841void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001842 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001843 ArrayFillerOrUnionFieldInit = filler;
1844 // Fill out any "holes" in the array due to designated initializers.
1845 Expr **inits = getInits();
1846 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1847 if (inits[i] == 0)
1848 inits[i] = filler;
1849}
1850
Richard Smithfe587202012-04-15 02:50:59 +00001851bool InitListExpr::isStringLiteralInit() const {
1852 if (getNumInits() != 1)
1853 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001854 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1855 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smithfe587202012-04-15 02:50:59 +00001856 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001857 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smithfe587202012-04-15 02:50:59 +00001858 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1859}
1860
Erik Verbruggen65d78312012-12-25 14:51:39 +00001861SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara23700f02012-11-08 18:41:43 +00001862 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen65d78312012-12-25 14:51:39 +00001863 return SyntacticForm->getLocStart();
1864 SourceLocation Beg = LBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001865 if (Beg.isInvalid()) {
1866 // Find the first non-null initializer.
1867 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1868 E = InitExprs.end();
1869 I != E; ++I) {
1870 if (Stmt *S = *I) {
1871 Beg = S->getLocStart();
1872 break;
1873 }
1874 }
1875 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001876 return Beg;
1877}
1878
1879SourceLocation InitListExpr::getLocEnd() const {
1880 if (InitListExpr *SyntacticForm = getSyntacticForm())
1881 return SyntacticForm->getLocEnd();
1882 SourceLocation End = RBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001883 if (End.isInvalid()) {
1884 // Find the first non-null initializer from the end.
1885 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen65d78312012-12-25 14:51:39 +00001886 E = InitExprs.rend();
1887 I != E; ++I) {
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001888 if (Stmt *S = *I) {
Erik Verbruggen65d78312012-12-25 14:51:39 +00001889 End = S->getLocEnd();
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001890 break;
Erik Verbruggen65d78312012-12-25 14:51:39 +00001891 }
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001892 }
1893 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001894 return End;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001895}
1896
Steve Naroffbfdcae62008-09-04 15:31:07 +00001897/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001898///
John McCalla345edb2012-02-17 03:32:35 +00001899const FunctionProtoType *BlockExpr::getFunctionType() const {
1900 // The block pointer is never sugared, but the function type might be.
1901 return cast<BlockPointerType>(getType())
1902 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001903}
1904
Mike Stump1eb44332009-09-09 15:08:12 +00001905SourceLocation BlockExpr::getCaretLocation() const {
1906 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001907}
Mike Stump1eb44332009-09-09 15:08:12 +00001908const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001909 return TheBlock->getBody();
1910}
Mike Stump1eb44332009-09-09 15:08:12 +00001911Stmt *BlockExpr::getBody() {
1912 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001913}
Steve Naroff56ee6892008-10-08 17:01:13 +00001914
1915
Reid Spencer5f016e22007-07-11 17:01:13 +00001916//===----------------------------------------------------------------------===//
1917// Generic Expression Routines
1918//===----------------------------------------------------------------------===//
1919
Chris Lattner026dc962009-02-14 07:37:35 +00001920/// isUnusedResultAWarning - Return true if this immediate expression should
1921/// be warned about if the result is unused. If so, fill in Loc and Ranges
1922/// with location to warn on and the source range[s] to report with the
1923/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001924bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1925 SourceRange &R1, SourceRange &R2,
1926 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001927 // Don't warn if the expr is type dependent. The type could end up
1928 // instantiating to void.
1929 if (isTypeDependent())
1930 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Reid Spencer5f016e22007-07-11 17:01:13 +00001932 switch (getStmtClass()) {
1933 default:
John McCall0faede62010-03-12 07:11:26 +00001934 if (getType()->isVoidType())
1935 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001936 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001937 Loc = getExprLoc();
1938 R1 = getSourceRange();
1939 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001940 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001941 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001942 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001943 case GenericSelectionExprClass:
1944 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001945 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmana5e66012013-07-20 00:40:58 +00001946 case ChooseExprClass:
1947 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1948 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001949 case UnaryOperatorClass: {
1950 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001953 case UO_Plus:
1954 case UO_Minus:
1955 case UO_AddrOf:
1956 case UO_Not:
1957 case UO_LNot:
1958 case UO_Deref:
1959 break;
John McCall2de56d12010-08-25 11:45:40 +00001960 case UO_PostInc:
1961 case UO_PostDec:
1962 case UO_PreInc:
1963 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001964 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001965 case UO_Real:
1966 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001967 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001968 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1969 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001970 return false;
1971 break;
John McCall2de56d12010-08-25 11:45:40 +00001972 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001973 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001974 }
Eli Friedmana6115062012-05-24 00:47:05 +00001975 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001976 Loc = UO->getOperatorLoc();
1977 R1 = UO->getSubExpr()->getSourceRange();
1978 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001979 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001980 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001981 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001982 switch (BO->getOpcode()) {
1983 default:
1984 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001985 // Consider the RHS of comma for side effects. LHS was checked by
1986 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001987 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001988 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1989 // lvalue-ness) of an assignment written in a macro.
1990 if (IntegerLiteral *IE =
1991 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1992 if (IE->getValue() == 0)
1993 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001994 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001995 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001996 case BO_LAnd:
1997 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001998 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1999 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00002000 return false;
2001 break;
John McCallbf0ee352010-02-16 04:10:53 +00002002 }
Chris Lattner026dc962009-02-14 07:37:35 +00002003 if (BO->isAssignmentOp())
2004 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002005 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002006 Loc = BO->getOperatorLoc();
2007 R1 = BO->getLHS()->getSourceRange();
2008 R2 = BO->getRHS()->getSourceRange();
2009 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00002010 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00002011 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00002012 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002013 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002014 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002015
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002016 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002017 // If only one of the LHS or RHS is a warning, the operator might
2018 // be being used for control flow. Only warn if both the LHS and
2019 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002020 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002021 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002022 return false;
2023 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00002024 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00002025 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002026 }
2027
Reid Spencer5f016e22007-07-11 17:01:13 +00002028 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002029 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002030 Loc = cast<MemberExpr>(this)->getMemberLoc();
2031 R1 = SourceRange(Loc, Loc);
2032 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2033 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002034
Reid Spencer5f016e22007-07-11 17:01:13 +00002035 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002036 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002037 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2038 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2039 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2040 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00002041
Chandler Carruth9b106832011-08-17 09:49:44 +00002042 case CXXOperatorCallExprClass: {
2043 // We warn about operator== and operator!= even when user-defined operator
2044 // overloads as there is no reasonable way to define these such that they
2045 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2046 // warning: these operators are commonly typo'ed, and so warning on them
2047 // provides additional value as well. If this list is updated,
2048 // DiagnoseUnusedComparison should be as well.
2049 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2050 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002051 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00002052 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002053 Loc = Op->getOperatorLoc();
2054 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00002055 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002056 }
Chandler Carruth9b106832011-08-17 09:49:44 +00002057
2058 // Fallthrough for generic call handling.
2059 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002060 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002061 case CXXMemberCallExprClass:
2062 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002063 // If this is a direct call, get the callee.
2064 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00002065 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00002066 // If the callee has attribute pure, const, or warn_unused_result, warn
2067 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002068 //
2069 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2070 // updated to match for QoI.
2071 if (FD->getAttr<WarnUnusedResultAttr>() ||
2072 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002073 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002074 Loc = CE->getCallee()->getLocStart();
2075 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002076
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002077 if (unsigned NumArgs = CE->getNumArgs())
2078 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2079 CE->getArg(NumArgs-1)->getLocEnd());
2080 return true;
2081 }
Chris Lattner026dc962009-02-14 07:37:35 +00002082 }
2083 return false;
2084 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002085
Matt Beaumont-Gay84c3b972012-10-23 06:15:26 +00002086 // If we don't know precisely what we're looking at, let's not warn.
2087 case UnresolvedLookupExprClass:
2088 case CXXUnresolvedConstructExprClass:
2089 return false;
2090
Anders Carlsson58beed92009-11-17 17:11:23 +00002091 case CXXTemporaryObjectExprClass:
Lubos Lunak81e45492013-07-21 13:15:58 +00002092 case CXXConstructExprClass: {
2093 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2094 if (Type->hasAttr<WarnUnusedAttr>()) {
2095 WarnE = this;
2096 Loc = getLocStart();
2097 R1 = getSourceRange();
2098 return true;
2099 }
2100 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002101 return false;
Lubos Lunak81e45492013-07-21 13:15:58 +00002102 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002103
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002104 case ObjCMessageExprClass: {
2105 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00002106 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002107 ME->isInstanceMessage() &&
2108 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas4bdb6022013-07-19 20:25:56 +00002109 ME->getMethodFamily() == OMF_init) {
Eli Friedmana6115062012-05-24 00:47:05 +00002110 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00002111 Loc = getExprLoc();
2112 R1 = ME->getSourceRange();
2113 return true;
2114 }
2115
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002116 const ObjCMethodDecl *MD = ME->getMethodDecl();
2117 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002118 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002119 Loc = getExprLoc();
2120 return true;
2121 }
Chris Lattner026dc962009-02-14 07:37:35 +00002122 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002123 }
Mike Stump1eb44332009-09-09 15:08:12 +00002124
John McCall12f78a62010-12-02 01:19:52 +00002125 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002126 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00002127 Loc = getExprLoc();
2128 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00002129 return true;
John McCall12f78a62010-12-02 01:19:52 +00002130
John McCall4b9c2d22011-11-06 09:01:30 +00002131 case PseudoObjectExprClass: {
2132 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2133
2134 // Only complain about things that have the form of a getter.
2135 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2136 isa<BinaryOperator>(PO->getSyntacticForm()))
2137 return false;
2138
Eli Friedmana6115062012-05-24 00:47:05 +00002139 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00002140 Loc = getExprLoc();
2141 R1 = getSourceRange();
2142 return true;
2143 }
2144
Chris Lattner611b2ec2008-07-26 19:51:01 +00002145 case StmtExprClass: {
2146 // Statement exprs don't logically have side effects themselves, but are
2147 // sometimes used in macros in ways that give them a type that is unused.
2148 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2149 // however, if the result of the stmt expr is dead, we don't want to emit a
2150 // warning.
2151 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002152 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002153 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002154 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002155 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2156 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002157 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002158 }
Mike Stump1eb44332009-09-09 15:08:12 +00002159
John McCall0faede62010-03-12 07:11:26 +00002160 if (getType()->isVoidType())
2161 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002162 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002163 Loc = cast<StmtExpr>(this)->getLParenLoc();
2164 R1 = getSourceRange();
2165 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002166 }
Eli Friedman63199172012-09-24 23:02:26 +00002167 case CXXFunctionalCastExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002168 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002169 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002170 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002171 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002172 if (CE->getCastKind() == CK_ToVoid) {
2173 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002174 CE->getSubExpr()->getType().isVolatileQualified()) {
2175 const DeclRefExpr *DRE =
2176 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2177 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2178 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2179 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2180 R1, R2, Ctx);
2181 }
2182 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002183 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002184 }
Eli Friedman4059da82012-05-24 21:05:41 +00002185
Eli Friedmana6115062012-05-24 00:47:05 +00002186 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002187 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002188 if (CE->getCastKind() == CK_ConstructorConversion)
2189 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002190
Eli Friedmana6115062012-05-24 00:47:05 +00002191 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002192 if (const CXXFunctionalCastExpr *CXXCE =
2193 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedmancdd4b782013-08-15 22:02:56 +00002194 Loc = CXXCE->getLocStart();
Eli Friedman4059da82012-05-24 21:05:41 +00002195 R1 = CXXCE->getSubExpr()->getSourceRange();
2196 } else {
2197 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2198 Loc = CStyleCE->getLParenLoc();
2199 R1 = CStyleCE->getSubExpr()->getSourceRange();
2200 }
Chris Lattner026dc962009-02-14 07:37:35 +00002201 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002202 }
Eli Friedmana6115062012-05-24 00:47:05 +00002203 case ImplicitCastExprClass: {
2204 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002205
Eli Friedmana6115062012-05-24 00:47:05 +00002206 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2207 if (ICE->getCastKind() == CK_LValueToRValue &&
2208 ICE->getSubExpr()->getType().isVolatileQualified())
2209 return false;
2210
2211 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2212 }
Chris Lattner04421082008-04-08 04:40:51 +00002213 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002214 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002215 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smithc3bf52c2013-04-20 22:23:05 +00002216 case CXXDefaultInitExprClass:
2217 return (cast<CXXDefaultInitExpr>(this)
2218 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002219
2220 case CXXNewExprClass:
2221 // FIXME: In theory, there might be new expressions that don't have side
2222 // effects (e.g. a placement new with an uninitialized POD).
2223 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002224 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002225 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002226 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002227 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002228 case ExprWithCleanupsClass:
2229 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002230 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002231 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002232}
2233
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002234/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002235/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002236bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002237 const Expr *E = IgnoreParens();
2238 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002239 default:
2240 return false;
2241 case ObjCIvarRefExprClass:
2242 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002243 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002244 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002245 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002246 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002247 case MaterializeTemporaryExprClass:
2248 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2249 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002250 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002251 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002252 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002253 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002254
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002255 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2256 if (VD->hasGlobalStorage())
2257 return true;
2258 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002259 // dereferencing to a pointer is always a gc'able candidate,
2260 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002261 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002262 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002263 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002264 return false;
2265 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002266 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002267 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002268 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002269 }
2270 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002271 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002272 }
2273}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002274
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002275bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2276 if (isTypeDependent())
2277 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002278 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002279}
2280
John McCall864c0412011-04-26 20:42:42 +00002281QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002282 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002283
2284 // Bound member expressions are always one of these possibilities:
2285 // x->m x.m x->*y x.*y
2286 // (possibly parenthesized)
2287
2288 expr = expr->IgnoreParens();
2289 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2290 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2291 return mem->getMemberDecl()->getType();
2292 }
2293
2294 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2295 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2296 ->getPointeeType();
2297 assert(type->isFunctionType());
2298 return type;
2299 }
2300
2301 assert(isa<UnresolvedMemberExpr>(expr));
2302 return QualType();
2303}
2304
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002305Expr* Expr::IgnoreParens() {
2306 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002307 while (true) {
2308 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2309 E = P->getSubExpr();
2310 continue;
2311 }
2312 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2313 if (P->getOpcode() == UO_Extension) {
2314 E = P->getSubExpr();
2315 continue;
2316 }
2317 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002318 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2319 if (!P->isResultDependent()) {
2320 E = P->getResultExpr();
2321 continue;
2322 }
2323 }
Eli Friedmana5e66012013-07-20 00:40:58 +00002324 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2325 if (!P->isConditionDependent()) {
2326 E = P->getChosenSubExpr();
2327 continue;
2328 }
2329 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002330 return E;
2331 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002332}
2333
Chris Lattner56f34942008-02-13 01:02:39 +00002334/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2335/// or CastExprs or ImplicitCastExprs, returning their operand.
2336Expr *Expr::IgnoreParenCasts() {
2337 Expr *E = this;
2338 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002339 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002340 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002341 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002342 continue;
2343 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002344 if (MaterializeTemporaryExpr *Materialize
2345 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2346 E = Materialize->GetTemporaryExpr();
2347 continue;
2348 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002349 if (SubstNonTypeTemplateParmExpr *NTTP
2350 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2351 E = NTTP->getReplacement();
2352 continue;
2353 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002354 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002355 }
2356}
2357
John McCall9c5d70c2010-12-04 08:24:19 +00002358/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2359/// casts. This is intended purely as a temporary workaround for code
2360/// that hasn't yet been rewritten to do the right thing about those
2361/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002362Expr *Expr::IgnoreParenLValueCasts() {
2363 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002364 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002365 E = E->IgnoreParens();
2366 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002367 if (P->getCastKind() == CK_LValueToRValue) {
2368 E = P->getSubExpr();
2369 continue;
2370 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002371 } else if (MaterializeTemporaryExpr *Materialize
2372 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2373 E = Materialize->GetTemporaryExpr();
2374 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002375 } else if (SubstNonTypeTemplateParmExpr *NTTP
2376 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2377 E = NTTP->getReplacement();
2378 continue;
John McCallf6a16482010-12-04 03:47:34 +00002379 }
2380 break;
2381 }
2382 return E;
2383}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002384
2385Expr *Expr::ignoreParenBaseCasts() {
2386 Expr *E = this;
2387 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002388 E = E->IgnoreParens();
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002389 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2390 if (CE->getCastKind() == CK_DerivedToBase ||
2391 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2392 CE->getCastKind() == CK_NoOp) {
2393 E = CE->getSubExpr();
2394 continue;
2395 }
2396 }
2397
2398 return E;
2399 }
2400}
2401
John McCall2fc46bf2010-05-05 22:59:52 +00002402Expr *Expr::IgnoreParenImpCasts() {
2403 Expr *E = this;
2404 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002405 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002406 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002407 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002408 continue;
2409 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002410 if (MaterializeTemporaryExpr *Materialize
2411 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2412 E = Materialize->GetTemporaryExpr();
2413 continue;
2414 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002415 if (SubstNonTypeTemplateParmExpr *NTTP
2416 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2417 E = NTTP->getReplacement();
2418 continue;
2419 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002420 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002421 }
2422}
2423
Hans Wennborg2f072b42011-06-09 17:06:51 +00002424Expr *Expr::IgnoreConversionOperator() {
2425 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002426 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002427 return MCE->getImplicitObjectArgument();
2428 }
2429 return this;
2430}
2431
Chris Lattnerecdd8412009-03-13 17:28:01 +00002432/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2433/// value (including ptr->int casts of the same size). Strip off any
2434/// ParenExpr or CastExprs, returning their operand.
2435Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2436 Expr *E = this;
2437 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002438 E = E->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Chris Lattnerecdd8412009-03-13 17:28:01 +00002440 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2441 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002442 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002443 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002444
Chris Lattnerecdd8412009-03-13 17:28:01 +00002445 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2446 E = SE;
2447 continue;
2448 }
Mike Stump1eb44332009-09-09 15:08:12 +00002449
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002450 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002451 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002452 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002453 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002454 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2455 E = SE;
2456 continue;
2457 }
2458 }
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Douglas Gregorc0244c52011-09-08 17:56:33 +00002460 if (SubstNonTypeTemplateParmExpr *NTTP
2461 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2462 E = NTTP->getReplacement();
2463 continue;
2464 }
2465
Chris Lattnerecdd8412009-03-13 17:28:01 +00002466 return E;
2467 }
2468}
2469
Douglas Gregor6eef5192009-12-14 19:27:10 +00002470bool Expr::isDefaultArgument() const {
2471 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002472 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2473 E = M->GetTemporaryExpr();
2474
Douglas Gregor6eef5192009-12-14 19:27:10 +00002475 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2476 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002477
Douglas Gregor6eef5192009-12-14 19:27:10 +00002478 return isa<CXXDefaultArgExpr>(E);
2479}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002480
Douglas Gregor2f599792010-04-02 18:24:57 +00002481/// \brief Skip over any no-op casts and any temporary-binding
2482/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002483static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002484 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2485 E = M->GetTemporaryExpr();
2486
Douglas Gregor2f599792010-04-02 18:24:57 +00002487 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002488 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002489 E = ICE->getSubExpr();
2490 else
2491 break;
2492 }
2493
2494 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2495 E = BE->getSubExpr();
2496
2497 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 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002503
2504 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002505}
2506
John McCall558d2ab2010-09-15 10:14:12 +00002507/// isTemporaryObject - Determines if this expression produces a
2508/// temporary of the given class type.
2509bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2510 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2511 return false;
2512
Anders Carlssonf8b30152010-11-28 16:40:49 +00002513 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002514
John McCall58277b52010-09-15 20:59:13 +00002515 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002516 if (!E->Classify(C).isPRValue()) {
2517 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002518 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002519 return false;
2520 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002521
John McCall19e60ad2010-09-16 06:57:56 +00002522 // Black-list a few cases which yield pr-values of class type that don't
2523 // refer to temporaries of that type:
2524
2525 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002526 if (isa<ImplicitCastExpr>(E)) {
2527 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2528 case CK_DerivedToBase:
2529 case CK_UncheckedDerivedToBase:
2530 return false;
2531 default:
2532 break;
2533 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002534 }
2535
John McCall19e60ad2010-09-16 06:57:56 +00002536 // - member expressions (all)
2537 if (isa<MemberExpr>(E))
2538 return false;
2539
Eli Friedman32f498a2012-06-15 23:51:06 +00002540 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2541 if (BO->isPtrMemOp())
2542 return false;
2543
John McCall56ca35d2011-02-17 10:25:35 +00002544 // - opaque values (all)
2545 if (isa<OpaqueValueExpr>(E))
2546 return false;
2547
John McCall558d2ab2010-09-15 10:14:12 +00002548 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002549}
2550
Douglas Gregor75e85042011-03-02 21:06:53 +00002551bool Expr::isImplicitCXXThis() const {
2552 const Expr *E = this;
2553
2554 // Strip away parentheses and casts we don't care about.
2555 while (true) {
2556 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2557 E = Paren->getSubExpr();
2558 continue;
2559 }
2560
2561 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2562 if (ICE->getCastKind() == CK_NoOp ||
2563 ICE->getCastKind() == CK_LValueToRValue ||
2564 ICE->getCastKind() == CK_DerivedToBase ||
2565 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2566 E = ICE->getSubExpr();
2567 continue;
2568 }
2569 }
2570
2571 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2572 if (UnOp->getOpcode() == UO_Extension) {
2573 E = UnOp->getSubExpr();
2574 continue;
2575 }
2576 }
2577
Douglas Gregor03e80032011-06-21 17:03:29 +00002578 if (const MaterializeTemporaryExpr *M
2579 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2580 E = M->GetTemporaryExpr();
2581 continue;
2582 }
2583
Douglas Gregor75e85042011-03-02 21:06:53 +00002584 break;
2585 }
2586
2587 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2588 return This->isImplicit();
2589
2590 return false;
2591}
2592
Douglas Gregor898574e2008-12-05 23:32:09 +00002593/// hasAnyTypeDependentArguments - Determines if any of the expressions
2594/// in Exprs is type-dependent.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002595bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00002596 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002597 if (Exprs[I]->isTypeDependent())
2598 return true;
2599
2600 return false;
2601}
2602
John McCall4204f072010-08-02 21:13:48 +00002603bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002604 // This function is attempting whether an expression is an initializer
Eli Friedman21cde052013-07-16 22:40:53 +00002605 // which can be evaluated at compile-time. It very closely parallels
2606 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2607 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2608 // to isEvaluatable most of the time.
2609 //
John McCall4204f072010-08-02 21:13:48 +00002610 // If we ever capture reference-binding directly in the AST, we can
2611 // kill the second parameter.
2612
2613 if (IsForRef) {
2614 EvalResult Result;
2615 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2616 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002617
Anders Carlssone8a32b82008-11-24 05:23:59 +00002618 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002619 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002620 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002621 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002622 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002623 case CXXTemporaryObjectExprClass:
2624 case CXXConstructExprClass: {
2625 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002626
Eli Friedman21cde052013-07-16 22:40:53 +00002627 if (CE->getConstructor()->isTrivial() &&
2628 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2629 // Trivial default constructor
Richard Smith180f4792011-11-10 06:34:14 +00002630 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002631
Eli Friedman21cde052013-07-16 22:40:53 +00002632 // Trivial copy constructor
2633 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2634 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smith180f4792011-11-10 06:34:14 +00002635 }
2636
Richard Smith180f4792011-11-10 06:34:14 +00002637 break;
John McCallb4b9b152010-08-01 21:51:45 +00002638 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002639 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002640 // This handles gcc's extension that allows global initializers like
2641 // "struct x {int x;} x = (struct x) {};".
2642 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002643 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002644 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002645 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002646 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002647 // FIXME: This doesn't deal with fields with reference types correctly.
2648 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2649 // to bitfields.
Eli Friedman21cde052013-07-16 22:40:53 +00002650 const InitListExpr *ILE = cast<InitListExpr>(this);
2651 if (ILE->getType()->isArrayType()) {
2652 unsigned numInits = ILE->getNumInits();
2653 for (unsigned i = 0; i < numInits; i++) {
2654 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2655 return false;
2656 }
2657 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002658 }
Eli Friedman21cde052013-07-16 22:40:53 +00002659
2660 if (ILE->getType()->isRecordType()) {
2661 unsigned ElementNo = 0;
2662 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2663 for (RecordDecl::field_iterator Field = RD->field_begin(),
2664 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2665 // If this is a union, skip all the fields that aren't being initialized.
2666 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2667 continue;
2668
2669 // Don't emit anonymous bitfields, they just affect layout.
2670 if (Field->isUnnamedBitfield())
2671 continue;
2672
2673 if (ElementNo < ILE->getNumInits()) {
2674 const Expr *Elt = ILE->getInit(ElementNo++);
2675 if (Field->isBitField()) {
2676 // Bitfields have to evaluate to an integer.
2677 llvm::APSInt ResultTmp;
2678 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2679 return false;
2680 } else {
2681 bool RefType = Field->getType()->isReferenceType();
2682 if (!Elt->isConstantInitializer(Ctx, RefType))
2683 return false;
2684 }
2685 }
2686 }
2687 return true;
2688 }
2689
2690 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002691 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002692 case ImplicitValueInitExprClass:
2693 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002694 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002695 return cast<ParenExpr>(this)->getSubExpr()
2696 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002697 case GenericSelectionExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002698 return cast<GenericSelectionExpr>(this)->getResultExpr()
2699 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002700 case ChooseExprClass:
Eli Friedmana5e66012013-07-20 00:40:58 +00002701 if (cast<ChooseExpr>(this)->isConditionDependent())
2702 return false;
2703 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002704 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002705 case UnaryOperatorClass: {
2706 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002707 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002708 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002709 break;
2710 }
John McCall4204f072010-08-02 21:13:48 +00002711 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002712 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002713 case ImplicitCastExprClass:
Eli Friedman21cde052013-07-16 22:40:53 +00002714 case CStyleCastExprClass:
2715 case ObjCBridgedCastExprClass:
2716 case CXXDynamicCastExprClass:
2717 case CXXReinterpretCastExprClass:
2718 case CXXConstCastExprClass: {
Richard Smithd62ca372011-12-06 22:44:34 +00002719 const CastExpr *CE = cast<CastExpr>(this);
2720
Eli Friedman6bd97192011-12-21 00:43:02 +00002721 // Handle misc casts we want to ignore.
Eli Friedman6bd97192011-12-21 00:43:02 +00002722 if (CE->getCastKind() == CK_NoOp ||
2723 CE->getCastKind() == CK_LValueToRValue ||
2724 CE->getCastKind() == CK_ToUnion ||
Eli Friedman21cde052013-07-16 22:40:53 +00002725 CE->getCastKind() == CK_ConstructorConversion ||
2726 CE->getCastKind() == CK_NonAtomicToAtomic ||
2727 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smithd62ca372011-12-06 22:44:34 +00002728 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2729
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002730 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002731 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002732 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002733 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002734 ->isConstantInitializer(Ctx, false);
Eli Friedman21cde052013-07-16 22:40:53 +00002735
2736 case SubstNonTypeTemplateParmExprClass:
2737 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2738 ->isConstantInitializer(Ctx, false);
2739 case CXXDefaultArgExprClass:
2740 return cast<CXXDefaultArgExpr>(this)->getExpr()
2741 ->isConstantInitializer(Ctx, false);
2742 case CXXDefaultInitExprClass:
2743 return cast<CXXDefaultInitExpr>(this)->getExpr()
2744 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002745 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002746 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002747}
2748
Richard Smith8ae4ec22012-08-07 04:16:51 +00002749bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2750 if (isInstantiationDependent())
2751 return true;
2752
2753 switch (getStmtClass()) {
2754 case NoStmtClass:
2755 #define ABSTRACT_STMT(Type)
2756 #define STMT(Type, Base) case Type##Class:
2757 #define EXPR(Type, Base)
2758 #include "clang/AST/StmtNodes.inc"
2759 llvm_unreachable("unexpected Expr kind");
2760
2761 case DependentScopeDeclRefExprClass:
2762 case CXXUnresolvedConstructExprClass:
2763 case CXXDependentScopeMemberExprClass:
2764 case UnresolvedLookupExprClass:
2765 case UnresolvedMemberExprClass:
2766 case PackExpansionExprClass:
2767 case SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +00002768 case FunctionParmPackExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002769 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2770
Richard Smith60b70382012-08-07 05:18:29 +00002771 case DeclRefExprClass:
2772 case ObjCIvarRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002773 case PredefinedExprClass:
2774 case IntegerLiteralClass:
2775 case FloatingLiteralClass:
2776 case ImaginaryLiteralClass:
2777 case StringLiteralClass:
2778 case CharacterLiteralClass:
2779 case OffsetOfExprClass:
2780 case ImplicitValueInitExprClass:
2781 case UnaryExprOrTypeTraitExprClass:
2782 case AddrLabelExprClass:
2783 case GNUNullExprClass:
2784 case CXXBoolLiteralExprClass:
2785 case CXXNullPtrLiteralExprClass:
2786 case CXXThisExprClass:
2787 case CXXScalarValueInitExprClass:
2788 case TypeTraitExprClass:
2789 case UnaryTypeTraitExprClass:
2790 case BinaryTypeTraitExprClass:
2791 case ArrayTypeTraitExprClass:
2792 case ExpressionTraitExprClass:
2793 case CXXNoexceptExprClass:
2794 case SizeOfPackExprClass:
2795 case ObjCStringLiteralClass:
2796 case ObjCEncodeExprClass:
2797 case ObjCBoolLiteralExprClass:
2798 case CXXUuidofExprClass:
2799 case OpaqueValueExprClass:
2800 // These never have a side-effect.
2801 return false;
2802
2803 case CallExprClass:
John McCall76da55d2013-04-16 07:28:30 +00002804 case MSPropertyRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002805 case CompoundAssignOperatorClass:
2806 case VAArgExprClass:
2807 case AtomicExprClass:
2808 case StmtExprClass:
2809 case CXXOperatorCallExprClass:
2810 case CXXMemberCallExprClass:
2811 case UserDefinedLiteralClass:
2812 case CXXThrowExprClass:
2813 case CXXNewExprClass:
2814 case CXXDeleteExprClass:
2815 case ExprWithCleanupsClass:
2816 case CXXBindTemporaryExprClass:
2817 case BlockExprClass:
2818 case CUDAKernelCallExprClass:
2819 // These always have a side-effect.
2820 return true;
2821
2822 case ParenExprClass:
2823 case ArraySubscriptExprClass:
2824 case MemberExprClass:
2825 case ConditionalOperatorClass:
2826 case BinaryConditionalOperatorClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002827 case CompoundLiteralExprClass:
2828 case ExtVectorElementExprClass:
2829 case DesignatedInitExprClass:
2830 case ParenListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002831 case CXXPseudoDestructorExprClass:
Richard Smith7c3e6152013-06-12 22:31:48 +00002832 case CXXStdInitializerListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002833 case SubstNonTypeTemplateParmExprClass:
2834 case MaterializeTemporaryExprClass:
2835 case ShuffleVectorExprClass:
2836 case AsTypeExprClass:
2837 // These have a side-effect if any subexpression does.
2838 break;
2839
Richard Smith60b70382012-08-07 05:18:29 +00002840 case UnaryOperatorClass:
2841 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002842 return true;
2843 break;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002844
2845 case BinaryOperatorClass:
2846 if (cast<BinaryOperator>(this)->isAssignmentOp())
2847 return true;
2848 break;
2849
Richard Smith8ae4ec22012-08-07 04:16:51 +00002850 case InitListExprClass:
2851 // FIXME: The children for an InitListExpr doesn't include the array filler.
2852 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2853 if (E->HasSideEffects(Ctx))
2854 return true;
2855 break;
2856
2857 case GenericSelectionExprClass:
2858 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2859 HasSideEffects(Ctx);
2860
2861 case ChooseExprClass:
Eli Friedmana5e66012013-07-20 00:40:58 +00002862 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith8ae4ec22012-08-07 04:16:51 +00002863
2864 case CXXDefaultArgExprClass:
2865 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2866
Richard Smithc3bf52c2013-04-20 22:23:05 +00002867 case CXXDefaultInitExprClass:
2868 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2869 return E->HasSideEffects(Ctx);
2870 // If we've not yet parsed the initializer, assume it has side-effects.
2871 return true;
2872
Richard Smith8ae4ec22012-08-07 04:16:51 +00002873 case CXXDynamicCastExprClass: {
2874 // A dynamic_cast expression has side-effects if it can throw.
2875 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2876 if (DCE->getTypeAsWritten()->isReferenceType() &&
2877 DCE->getCastKind() == CK_Dynamic)
2878 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002879 } // Fall through.
2880 case ImplicitCastExprClass:
2881 case CStyleCastExprClass:
2882 case CXXStaticCastExprClass:
2883 case CXXReinterpretCastExprClass:
2884 case CXXConstCastExprClass:
2885 case CXXFunctionalCastExprClass: {
2886 const CastExpr *CE = cast<CastExpr>(this);
2887 if (CE->getCastKind() == CK_LValueToRValue &&
2888 CE->getSubExpr()->getType().isVolatileQualified())
2889 return true;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002890 break;
2891 }
2892
Richard Smith0d729102012-08-13 20:08:14 +00002893 case CXXTypeidExprClass:
2894 // typeid might throw if its subexpression is potentially-evaluated, so has
2895 // side-effects in that case whether or not its subexpression does.
2896 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith8ae4ec22012-08-07 04:16:51 +00002897
2898 case CXXConstructExprClass:
2899 case CXXTemporaryObjectExprClass: {
2900 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smith60b70382012-08-07 05:18:29 +00002901 if (!CE->getConstructor()->isTrivial())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002902 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002903 // A trivial constructor does not add any side-effects of its own. Just look
2904 // at its arguments.
Richard Smith8ae4ec22012-08-07 04:16:51 +00002905 break;
2906 }
2907
2908 case LambdaExprClass: {
2909 const LambdaExpr *LE = cast<LambdaExpr>(this);
2910 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2911 E = LE->capture_end(); I != E; ++I)
2912 if (I->getCaptureKind() == LCK_ByCopy)
2913 // FIXME: Only has a side-effect if the variable is volatile or if
2914 // the copy would invoke a non-trivial copy constructor.
2915 return true;
2916 return false;
2917 }
2918
2919 case PseudoObjectExprClass: {
2920 // Only look for side-effects in the semantic form, and look past
2921 // OpaqueValueExpr bindings in that form.
2922 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2923 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2924 E = PO->semantics_end();
2925 I != E; ++I) {
2926 const Expr *Subexpr = *I;
2927 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2928 Subexpr = OVE->getSourceExpr();
2929 if (Subexpr->HasSideEffects(Ctx))
2930 return true;
2931 }
2932 return false;
2933 }
2934
2935 case ObjCBoxedExprClass:
2936 case ObjCArrayLiteralClass:
2937 case ObjCDictionaryLiteralClass:
2938 case ObjCMessageExprClass:
2939 case ObjCSelectorExprClass:
2940 case ObjCProtocolExprClass:
2941 case ObjCPropertyRefExprClass:
2942 case ObjCIsaExprClass:
2943 case ObjCIndirectCopyRestoreExprClass:
2944 case ObjCSubscriptRefExprClass:
2945 case ObjCBridgedCastExprClass:
2946 // FIXME: Classify these cases better.
2947 return true;
2948 }
2949
2950 // Recurse to children.
2951 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2952 if (const Stmt *S = *SubStmts)
2953 if (cast<Expr>(S)->HasSideEffects(Ctx))
2954 return true;
2955
2956 return false;
2957}
2958
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002959namespace {
2960 /// \brief Look for a call to a non-trivial function within an expression.
2961 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2962 {
2963 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2964
2965 bool NonTrivial;
2966
2967 public:
2968 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002969 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002970
2971 bool hasNonTrivialCall() const { return NonTrivial; }
2972
2973 void VisitCallExpr(CallExpr *E) {
2974 if (CXXMethodDecl *Method
2975 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2976 if (Method->isTrivial()) {
2977 // Recurse to children of the call.
2978 Inherited::VisitStmt(E);
2979 return;
2980 }
2981 }
2982
2983 NonTrivial = true;
2984 }
2985
2986 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2987 if (E->getConstructor()->isTrivial()) {
2988 // Recurse to children of the call.
2989 Inherited::VisitStmt(E);
2990 return;
2991 }
2992
2993 NonTrivial = true;
2994 }
2995
2996 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2997 if (E->getTemporary()->getDestructor()->isTrivial()) {
2998 Inherited::VisitStmt(E);
2999 return;
3000 }
3001
3002 NonTrivial = true;
3003 }
3004 };
3005}
3006
3007bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3008 NonTrivialCallFinder Finder(Ctx);
3009 Finder.Visit(this);
3010 return Finder.hasNonTrivialCall();
3011}
3012
Chandler Carruth82214a82011-02-18 23:54:50 +00003013/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3014/// pointer constant or not, as well as the specific kind of constant detected.
3015/// Null pointer constants can be integer constant expressions with the
3016/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3017/// (a GNU extension).
3018Expr::NullPointerConstantKind
3019Expr::isNullPointerConstant(ASTContext &Ctx,
3020 NullPointerConstantValueDependence NPC) const {
Richard Smithf050d242013-06-13 02:46:14 +00003021 if (isValueDependent() && !Ctx.getLangOpts().CPlusPlus11) {
Douglas Gregorce940492009-09-25 04:25:58 +00003022 switch (NPC) {
3023 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00003024 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00003025 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003026 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie50800fc2012-08-08 17:33:31 +00003027 return NPCK_ZeroExpression;
Chandler Carruth82214a82011-02-18 23:54:50 +00003028 else
3029 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00003030
Douglas Gregorce940492009-09-25 04:25:58 +00003031 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003032 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00003033 }
3034 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00003035
Sebastian Redl07779722008-10-31 14:43:28 +00003036 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00003037 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003038 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00003039 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00003040 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00003041 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00003042 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00003043 Pointee->isVoidType() && // to void*
3044 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00003045 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00003046 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003047 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003048 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3049 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00003050 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00003051 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3052 // Accept ((void*)0) as a null pointer constant, as many other
3053 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00003054 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00003055 } else if (const GenericSelectionExpr *GE =
3056 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedmana5e66012013-07-20 00:40:58 +00003057 if (GE->isResultDependent())
3058 return NPCK_NotNull;
Peter Collingbournef111d932011-04-15 00:35:48 +00003059 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedmana5e66012013-07-20 00:40:58 +00003060 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3061 if (CE->isConditionDependent())
3062 return NPCK_NotNull;
3063 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00003064 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00003065 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smithc3bf52c2013-04-20 22:23:05 +00003066 // See through default argument expressions.
Douglas Gregorce940492009-09-25 04:25:58 +00003067 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smithc3bf52c2013-04-20 22:23:05 +00003068 } else if (const CXXDefaultInitExpr *DefaultInit
3069 = dyn_cast<CXXDefaultInitExpr>(this)) {
3070 // See through default initializer expressions.
3071 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003072 } else if (isa<GNUNullExpr>(this)) {
3073 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00003074 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00003075 } else if (const MaterializeTemporaryExpr *M
3076 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3077 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00003078 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3079 if (const Expr *Source = OVE->getSourceExpr())
3080 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00003081 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003082
Richard Smith4e24f0f2013-01-02 12:01:23 +00003083 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003084 if (getType()->isNullPtrType())
Richard Smith4e24f0f2013-01-02 12:01:23 +00003085 return NPCK_CXX11_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003086
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003087 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smithf050d242013-06-13 02:46:14 +00003088 if (!Ctx.getLangOpts().CPlusPlus11 &&
3089 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003090 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3091 const Expr *InitExpr = CLE->getInitializer();
3092 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3093 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3094 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003095 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00003096 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003097 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00003098 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00003099
Richard Smith80ad52f2013-01-02 11:42:31 +00003100 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smithf050d242013-06-13 02:46:14 +00003101 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3102 // value zero or a prvalue of type std::nullptr_t.
3103 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
3104 return (Lit && !Lit->getValue()) ? NPCK_ZeroLiteral : NPCK_NotNull;
Richard Smith70488e22012-02-14 21:38:30 +00003105 } else {
Richard Smithf050d242013-06-13 02:46:14 +00003106 // If we have an integer constant expression, we need to *evaluate* it and
3107 // test for the value 0.
Richard Smith70488e22012-02-14 21:38:30 +00003108 if (!isIntegerConstantExpr(Ctx))
3109 return NPCK_NotNull;
3110 }
Chandler Carruth82214a82011-02-18 23:54:50 +00003111
David Blaikie50800fc2012-08-08 17:33:31 +00003112 if (EvaluateKnownConstInt(Ctx) != 0)
3113 return NPCK_NotNull;
3114
3115 if (isa<IntegerLiteral>(this))
3116 return NPCK_ZeroLiteral;
3117 return NPCK_ZeroExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00003118}
Steve Naroff31a45842007-07-28 23:10:27 +00003119
John McCallf6a16482010-12-04 03:47:34 +00003120/// \brief If this expression is an l-value for an Objective C
3121/// property, find the underlying property reference expression.
3122const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3123 const Expr *E = this;
3124 while (true) {
3125 assert((E->getValueKind() == VK_LValue &&
3126 E->getObjectKind() == OK_ObjCProperty) &&
3127 "expression is not a property reference");
3128 E = E->IgnoreParenCasts();
3129 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3130 if (BO->getOpcode() == BO_Comma) {
3131 E = BO->getRHS();
3132 continue;
3133 }
3134 }
3135
3136 break;
3137 }
3138
3139 return cast<ObjCPropertyRefExpr>(E);
3140}
3141
Anna Zaksbbff82f2012-10-01 20:34:04 +00003142bool Expr::isObjCSelfExpr() const {
3143 const Expr *E = IgnoreParenImpCasts();
3144
3145 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3146 if (!DRE)
3147 return false;
3148
3149 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3150 if (!Param)
3151 return false;
3152
3153 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3154 if (!M)
3155 return false;
3156
3157 return M->getSelfDecl() == Param;
3158}
3159
John McCall993f43f2013-05-06 21:39:12 +00003160FieldDecl *Expr::getSourceBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00003161 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003162
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003163 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00003164 if (ICE->getCastKind() == CK_LValueToRValue ||
3165 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003166 E = ICE->getSubExpr()->IgnoreParens();
3167 else
3168 break;
3169 }
3170
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003171 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00003172 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003173 if (Field->isBitField())
3174 return Field;
3175
John McCall993f43f2013-05-06 21:39:12 +00003176 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3177 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3178 if (Ivar->isBitField())
3179 return Ivar;
3180
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00003181 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3182 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3183 if (Field->isBitField())
3184 return Field;
3185
Eli Friedman42068e92011-07-13 02:05:57 +00003186 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003187 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCall993f43f2013-05-06 21:39:12 +00003188 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003189
Eli Friedman42068e92011-07-13 02:05:57 +00003190 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCall993f43f2013-05-06 21:39:12 +00003191 return BinOp->getRHS()->getSourceBitField();
Eli Friedman42068e92011-07-13 02:05:57 +00003192 }
3193
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003194 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003195}
3196
Anders Carlsson09380262010-01-31 17:18:49 +00003197bool Expr::refersToVectorElement() const {
3198 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00003199
Anders Carlsson09380262010-01-31 17:18:49 +00003200 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00003201 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00003202 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00003203 E = ICE->getSubExpr()->IgnoreParens();
3204 else
3205 break;
3206 }
Sean Huntc3021132010-05-05 15:23:54 +00003207
Anders Carlsson09380262010-01-31 17:18:49 +00003208 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3209 return ASE->getBase()->getType()->isVectorType();
3210
3211 if (isa<ExtVectorElementExpr>(E))
3212 return true;
3213
3214 return false;
3215}
3216
Chris Lattner2140e902009-02-16 22:14:05 +00003217/// isArrow - Return true if the base expression is a pointer to vector,
3218/// return false if the base expression is a vector.
3219bool ExtVectorElementExpr::isArrow() const {
3220 return getBase()->getType()->isPointerType();
3221}
3222
Nate Begeman213541a2008-04-18 23:10:10 +00003223unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003224 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003225 return VT->getNumElements();
3226 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003227}
3228
Nate Begeman8a997642008-05-09 06:41:27 +00003229/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003230bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003231 // FIXME: Refactor this code to an accessor on the AST node which returns the
3232 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003233 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003234
3235 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003236 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003237 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003238
Nate Begeman190d6a22009-01-18 02:01:21 +00003239 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003240 if (Comp[0] == 's' || Comp[0] == 'S')
3241 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003242
Daniel Dunbar15027422009-10-17 23:53:04 +00003243 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003244 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003245 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003246
Steve Narofffec0b492007-07-30 03:29:09 +00003247 return false;
3248}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003249
Nate Begeman8a997642008-05-09 06:41:27 +00003250/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003251void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003252 SmallVectorImpl<unsigned> &Elts) const {
3253 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003254 if (Comp[0] == 's' || Comp[0] == 'S')
3255 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003256
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003257 bool isHi = Comp == "hi";
3258 bool isLo = Comp == "lo";
3259 bool isEven = Comp == "even";
3260 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003261
Nate Begeman8a997642008-05-09 06:41:27 +00003262 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3263 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003264
Nate Begeman8a997642008-05-09 06:41:27 +00003265 if (isHi)
3266 Index = e + i;
3267 else if (isLo)
3268 Index = i;
3269 else if (isEven)
3270 Index = 2 * i;
3271 else if (isOdd)
3272 Index = 2 * i + 1;
3273 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003274 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003275
Nate Begeman3b8d1162008-05-13 21:03:02 +00003276 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003277 }
Nate Begeman8a997642008-05-09 06:41:27 +00003278}
3279
Douglas Gregor04badcf2010-04-21 00:45:42 +00003280ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003281 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003282 SourceLocation LBracLoc,
3283 SourceLocation SuperLoc,
3284 bool IsInstanceSuper,
3285 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003286 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003287 ArrayRef<SourceLocation> SelLocs,
3288 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003289 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003290 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003291 SourceLocation RBracLoc,
3292 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003293 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003294 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003295 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003296 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003297 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3298 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003299 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003300 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3301 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003302{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003303 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003304 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003305}
3306
Douglas Gregor04badcf2010-04-21 00:45:42 +00003307ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003308 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003309 SourceLocation LBracLoc,
3310 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003311 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003312 ArrayRef<SourceLocation> SelLocs,
3313 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003314 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003315 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003316 SourceLocation RBracLoc,
3317 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003318 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003319 T->isDependentType(), T->isInstantiationDependentType(),
3320 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003321 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3322 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003323 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003324 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003325 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003326{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003327 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003328 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003329}
3330
Douglas Gregor04badcf2010-04-21 00:45:42 +00003331ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003332 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003333 SourceLocation LBracLoc,
3334 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003335 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003336 ArrayRef<SourceLocation> SelLocs,
3337 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003338 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003339 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003340 SourceLocation RBracLoc,
3341 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003342 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003343 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003344 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003345 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003346 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3347 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003348 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003349 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003350 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003351{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003352 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003353 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003354}
3355
3356void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3357 ArrayRef<SourceLocation> SelLocs,
3358 SelectorLocationsKind SelLocsK) {
3359 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003360 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003361 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003362 if (Args[I]->isTypeDependent())
3363 ExprBits.TypeDependent = true;
3364 if (Args[I]->isValueDependent())
3365 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003366 if (Args[I]->isInstantiationDependent())
3367 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003368 if (Args[I]->containsUnexpandedParameterPack())
3369 ExprBits.ContainsUnexpandedParameterPack = true;
3370
3371 MyArgs[I] = Args[I];
3372 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003373
Benjamin Kramer19562c92012-02-20 00:20:48 +00003374 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003375 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003376 if (SelLocsK == SelLoc_NonStandard)
3377 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3378 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003379}
3380
Craig Topper9db7a7e2013-08-22 04:58:56 +00003381ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003382 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003383 SourceLocation LBracLoc,
3384 SourceLocation SuperLoc,
3385 bool IsInstanceSuper,
3386 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003387 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003388 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003389 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003390 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003391 SourceLocation RBracLoc,
3392 bool isImplicit) {
3393 assert((!SelLocs.empty() || isImplicit) &&
3394 "No selector locs for non-implicit message");
3395 ObjCMessageExpr *Mem;
3396 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3397 if (isImplicit)
3398 Mem = alloc(Context, Args.size(), 0);
3399 else
3400 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003401 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003402 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003403 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003404}
3405
Craig Topper9db7a7e2013-08-22 04:58:56 +00003406ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003407 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003408 SourceLocation LBracLoc,
3409 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003410 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003411 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003412 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003413 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003414 SourceLocation RBracLoc,
3415 bool isImplicit) {
3416 assert((!SelLocs.empty() || isImplicit) &&
3417 "No selector locs for non-implicit message");
3418 ObjCMessageExpr *Mem;
3419 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3420 if (isImplicit)
3421 Mem = alloc(Context, Args.size(), 0);
3422 else
3423 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003424 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003425 SelLocs, SelLocsK, Method, Args, RBracLoc,
3426 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003427}
3428
Craig Topper9db7a7e2013-08-22 04:58:56 +00003429ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003430 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003431 SourceLocation LBracLoc,
3432 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003433 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003434 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003435 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003436 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003437 SourceLocation RBracLoc,
3438 bool isImplicit) {
3439 assert((!SelLocs.empty() || isImplicit) &&
3440 "No selector locs for non-implicit message");
3441 ObjCMessageExpr *Mem;
3442 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3443 if (isImplicit)
3444 Mem = alloc(Context, Args.size(), 0);
3445 else
3446 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003447 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003448 SelLocs, SelLocsK, Method, Args, RBracLoc,
3449 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003450}
3451
Craig Topper9db7a7e2013-08-22 04:58:56 +00003452ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003453 unsigned NumArgs,
3454 unsigned NumStoredSelLocs) {
3455 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003456 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3457}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003458
Craig Topper9db7a7e2013-08-22 04:58:56 +00003459ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003460 ArrayRef<Expr *> Args,
3461 SourceLocation RBraceLoc,
3462 ArrayRef<SourceLocation> SelLocs,
3463 Selector Sel,
3464 SelectorLocationsKind &SelLocsK) {
3465 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3466 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3467 : 0;
3468 return alloc(C, Args.size(), NumStoredSelLocs);
3469}
3470
Craig Topper9db7a7e2013-08-22 04:58:56 +00003471ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003472 unsigned NumArgs,
3473 unsigned NumStoredSelLocs) {
3474 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3475 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3476 return (ObjCMessageExpr *)C.Allocate(Size,
3477 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3478}
3479
3480void ObjCMessageExpr::getSelectorLocs(
3481 SmallVectorImpl<SourceLocation> &SelLocs) const {
3482 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3483 SelLocs.push_back(getSelectorLoc(i));
3484}
3485
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003486SourceRange ObjCMessageExpr::getReceiverRange() const {
3487 switch (getReceiverKind()) {
3488 case Instance:
3489 return getInstanceReceiver()->getSourceRange();
3490
3491 case Class:
3492 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3493
3494 case SuperInstance:
3495 case SuperClass:
3496 return getSuperLoc();
3497 }
3498
David Blaikie30263482012-01-20 21:50:17 +00003499 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003500}
3501
Douglas Gregor04badcf2010-04-21 00:45:42 +00003502Selector ObjCMessageExpr::getSelector() const {
3503 if (HasMethod)
3504 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3505 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003506 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003507}
3508
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003509QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor04badcf2010-04-21 00:45:42 +00003510 switch (getReceiverKind()) {
3511 case Instance:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003512 return getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003513 case Class:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003514 return getClassReceiver();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003515 case SuperInstance:
Douglas Gregor04badcf2010-04-21 00:45:42 +00003516 case SuperClass:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003517 return getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003518 }
3519
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003520 llvm_unreachable("unexpected receiver kind");
3521}
3522
3523ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3524 QualType T = getReceiverType();
3525
3526 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3527 return Ptr->getInterfaceDecl();
3528
3529 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3530 return Ty->getInterface();
3531
Douglas Gregor04badcf2010-04-21 00:45:42 +00003532 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003533}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003534
Chris Lattner5f9e2722011-07-23 10:55:15 +00003535StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003536 switch (getBridgeKind()) {
3537 case OBC_Bridge:
3538 return "__bridge";
3539 case OBC_BridgeTransfer:
3540 return "__bridge_transfer";
3541 case OBC_BridgeRetained:
3542 return "__bridge_retained";
3543 }
David Blaikie30263482012-01-20 21:50:17 +00003544
3545 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003546}
3547
Craig Topper05ed1a02013-08-18 10:09:15 +00003548ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003549 QualType Type, SourceLocation BLoc,
3550 SourceLocation RP)
3551 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3552 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003553 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003554 Type->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003555 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003556{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003557 SubExprs = new (C) Stmt*[args.size()];
3558 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003559 if (args[i]->isTypeDependent())
3560 ExprBits.TypeDependent = true;
3561 if (args[i]->isValueDependent())
3562 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003563 if (args[i]->isInstantiationDependent())
3564 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003565 if (args[i]->containsUnexpandedParameterPack())
3566 ExprBits.ContainsUnexpandedParameterPack = true;
3567
3568 SubExprs[i] = args[i];
3569 }
3570}
3571
Craig Topper05ed1a02013-08-18 10:09:15 +00003572void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman888376a2009-08-12 02:28:50 +00003573 if (SubExprs) C.Deallocate(SubExprs);
3574
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003575 this->NumExprs = Exprs.size();
Dmitri Gribenko2ad77cd2013-05-10 17:30:13 +00003576 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003577 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00003578}
Nate Begeman888376a2009-08-12 02:28:50 +00003579
Craig Topper05ed1a02013-08-18 10:09:15 +00003580GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003581 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003582 ArrayRef<TypeSourceInfo*> AssocTypes,
3583 ArrayRef<Expr*> AssocExprs,
3584 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003585 SourceLocation RParenLoc,
3586 bool ContainsUnexpandedParameterPack,
3587 unsigned ResultIndex)
3588 : Expr(GenericSelectionExprClass,
3589 AssocExprs[ResultIndex]->getType(),
3590 AssocExprs[ResultIndex]->getValueKind(),
3591 AssocExprs[ResultIndex]->getObjectKind(),
3592 AssocExprs[ResultIndex]->isTypeDependent(),
3593 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003594 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003595 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003596 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3597 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3598 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3599 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003600 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003601 assert(AssocTypes.size() == AssocExprs.size());
3602 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3603 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003604}
3605
Craig Topper05ed1a02013-08-18 10:09:15 +00003606GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003607 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003608 ArrayRef<TypeSourceInfo*> AssocTypes,
3609 ArrayRef<Expr*> AssocExprs,
3610 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003611 SourceLocation RParenLoc,
3612 bool ContainsUnexpandedParameterPack)
3613 : Expr(GenericSelectionExprClass,
3614 Context.DependentTy,
3615 VK_RValue,
3616 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003617 /*isTypeDependent=*/true,
3618 /*isValueDependent=*/true,
3619 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003620 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003621 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3622 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3623 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3624 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003625 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003626 assert(AssocTypes.size() == AssocExprs.size());
3627 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3628 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003629}
3630
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003631//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003632// DesignatedInitExpr
3633//===----------------------------------------------------------------------===//
3634
Chandler Carruthb1138242011-06-16 06:47:06 +00003635IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003636 assert(Kind == FieldDesignator && "Only valid on a field designator");
3637 if (Field.NameOrField & 0x01)
3638 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3639 else
3640 return getField()->getIdentifier();
3641}
3642
Craig Topper05ed1a02013-08-18 10:09:15 +00003643DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003644 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003645 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003646 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003647 bool GNUSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003648 ArrayRef<Expr*> IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003649 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003650 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003651 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003652 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003653 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003654 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003655 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003656 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003657 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003658
3659 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003660 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003661 *Child++ = Init;
3662
3663 // Copy the designators and their subexpressions, computing
3664 // value-dependence along the way.
3665 unsigned IndexIdx = 0;
3666 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003667 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003668
3669 if (this->Designators[I].isArrayDesignator()) {
3670 // Compute type- and value-dependence.
3671 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003672 if (Index->isTypeDependent() || Index->isValueDependent())
3673 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003674 if (Index->isInstantiationDependent())
3675 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003676 // Propagate unexpanded parameter packs.
3677 if (Index->containsUnexpandedParameterPack())
3678 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003679
3680 // Copy the index expressions into permanent storage.
3681 *Child++ = IndexExprs[IndexIdx++];
3682 } else if (this->Designators[I].isArrayRangeDesignator()) {
3683 // Compute type- and value-dependence.
3684 Expr *Start = IndexExprs[IndexIdx];
3685 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003686 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003687 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003688 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003689 ExprBits.InstantiationDependent = true;
3690 } else if (Start->isInstantiationDependent() ||
3691 End->isInstantiationDependent()) {
3692 ExprBits.InstantiationDependent = true;
3693 }
3694
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003695 // Propagate unexpanded parameter packs.
3696 if (Start->containsUnexpandedParameterPack() ||
3697 End->containsUnexpandedParameterPack())
3698 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003699
3700 // Copy the start/end expressions into permanent storage.
3701 *Child++ = IndexExprs[IndexIdx++];
3702 *Child++ = IndexExprs[IndexIdx++];
3703 }
3704 }
3705
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003706 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003707}
3708
Douglas Gregor05c13a32009-01-22 00:58:24 +00003709DesignatedInitExpr *
Craig Topper05ed1a02013-08-18 10:09:15 +00003710DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003711 unsigned NumDesignators,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003712 ArrayRef<Expr*> IndexExprs,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003713 SourceLocation ColonOrEqualLoc,
3714 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003715 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003716 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003717 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003718 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003719 IndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003720}
3721
Craig Topper05ed1a02013-08-18 10:09:15 +00003722DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003723 unsigned NumIndexExprs) {
3724 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3725 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3726 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3727}
3728
Craig Topper05ed1a02013-08-18 10:09:15 +00003729void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003730 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003731 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003732 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003733 NumDesignators = NumDesigs;
3734 for (unsigned I = 0; I != NumDesigs; ++I)
3735 Designators[I] = Desigs[I];
3736}
3737
Abramo Bagnara24f46742011-03-16 15:08:46 +00003738SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3739 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3740 if (size() == 1)
3741 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen65d78312012-12-25 14:51:39 +00003742 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3743 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara24f46742011-03-16 15:08:46 +00003744}
3745
Erik Verbruggen65d78312012-12-25 14:51:39 +00003746SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003747 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003748 Designator &First =
3749 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003750 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003751 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003752 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3753 else
3754 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3755 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003756 StartLoc =
3757 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen65d78312012-12-25 14:51:39 +00003758 return StartLoc;
3759}
3760
3761SourceLocation DesignatedInitExpr::getLocEnd() const {
3762 return getInit()->getLocEnd();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003763}
3764
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003765Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003766 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003767 char *Ptr = static_cast<char *>(
3768 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003769 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003770 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3771 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3772}
3773
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003774Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003775 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003776 "Requires array range 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::getArrayRangeEnd(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 + 2));
3792}
3793
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003794/// \brief Replaces the designator at index @p Idx with the series
3795/// of designators in [First, Last).
Craig Topper05ed1a02013-08-18 10:09:15 +00003796void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003797 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003798 const Designator *Last) {
3799 unsigned NumNewDesignators = Last - First;
3800 if (NumNewDesignators == 0) {
3801 std::copy_backward(Designators + Idx + 1,
3802 Designators + NumDesignators,
3803 Designators + Idx);
3804 --NumNewDesignators;
3805 return;
3806 } else if (NumNewDesignators == 1) {
3807 Designators[Idx] = *First;
3808 return;
3809 }
3810
Mike Stump1eb44332009-09-09 15:08:12 +00003811 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003812 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003813 std::copy(Designators, Designators + Idx, NewDesignators);
3814 std::copy(First, Last, NewDesignators + Idx);
3815 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3816 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003817 Designators = NewDesignators;
3818 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3819}
3820
Craig Topper05ed1a02013-08-18 10:09:15 +00003821ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003822 ArrayRef<Expr*> exprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003823 SourceLocation rparenloc)
3824 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003825 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003826 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3827 Exprs = new (C) Stmt*[exprs.size()];
3828 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003829 if (exprs[i]->isTypeDependent())
3830 ExprBits.TypeDependent = true;
3831 if (exprs[i]->isValueDependent())
3832 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003833 if (exprs[i]->isInstantiationDependent())
3834 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003835 if (exprs[i]->containsUnexpandedParameterPack())
3836 ExprBits.ContainsUnexpandedParameterPack = true;
3837
Nate Begeman2ef13e52009-08-10 23:49:36 +00003838 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003839 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003840}
3841
John McCalle996ffd2011-02-16 08:02:54 +00003842const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3843 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3844 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003845 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3846 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003847 e = cast<CXXConstructExpr>(e)->getArg(0);
3848 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3849 e = ice->getSubExpr();
3850 return cast<OpaqueValueExpr>(e);
3851}
3852
Craig Topper05ed1a02013-08-18 10:09:15 +00003853PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3854 EmptyShell sh,
John McCall4b9c2d22011-11-06 09:01:30 +00003855 unsigned numSemanticExprs) {
3856 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3857 (1 + numSemanticExprs) * sizeof(Expr*),
3858 llvm::alignOf<PseudoObjectExpr>());
3859 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3860}
3861
3862PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3863 : Expr(PseudoObjectExprClass, shell) {
3864 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3865}
3866
Craig Topper05ed1a02013-08-18 10:09:15 +00003867PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCall4b9c2d22011-11-06 09:01:30 +00003868 ArrayRef<Expr*> semantics,
3869 unsigned resultIndex) {
3870 assert(syntax && "no syntactic expression!");
3871 assert(semantics.size() && "no semantic expressions!");
3872
3873 QualType type;
3874 ExprValueKind VK;
3875 if (resultIndex == NoResult) {
3876 type = C.VoidTy;
3877 VK = VK_RValue;
3878 } else {
3879 assert(resultIndex < semantics.size());
3880 type = semantics[resultIndex]->getType();
3881 VK = semantics[resultIndex]->getValueKind();
3882 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3883 }
3884
3885 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3886 (1 + semantics.size()) * sizeof(Expr*),
3887 llvm::alignOf<PseudoObjectExpr>());
3888 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3889 resultIndex);
3890}
3891
3892PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3893 Expr *syntax, ArrayRef<Expr*> semantics,
3894 unsigned resultIndex)
3895 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3896 /*filled in at end of ctor*/ false, false, false, false) {
3897 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3898 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3899
3900 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3901 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3902 getSubExprsBuffer()[i] = E;
3903
3904 if (E->isTypeDependent())
3905 ExprBits.TypeDependent = true;
3906 if (E->isValueDependent())
3907 ExprBits.ValueDependent = true;
3908 if (E->isInstantiationDependent())
3909 ExprBits.InstantiationDependent = true;
3910 if (E->containsUnexpandedParameterPack())
3911 ExprBits.ContainsUnexpandedParameterPack = true;
3912
3913 if (isa<OpaqueValueExpr>(E))
3914 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3915 "opaque-value semantic expressions for pseudo-object "
3916 "operations must have sources");
3917 }
3918}
3919
Douglas Gregor05c13a32009-01-22 00:58:24 +00003920//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003921// ExprIterator.
3922//===----------------------------------------------------------------------===//
3923
3924Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3925Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3926Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3927const Expr* ConstExprIterator::operator[](size_t idx) const {
3928 return cast<Expr>(I[idx]);
3929}
3930const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3931const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3932
3933//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003934// Child Iterators for iterating over subexpressions/substatements
3935//===----------------------------------------------------------------------===//
3936
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003937// UnaryExprOrTypeTraitExpr
3938Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003939 // If this is of a type and the type is a VLA type (and not a typedef), the
3940 // size expression of the VLA needs to be treated as an executable expression.
3941 // Why isn't this weirdness documented better in StmtIterator?
3942 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003943 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003944 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003945 return child_range(child_iterator(T), child_iterator());
3946 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003947 }
John McCall63c00d72011-02-09 08:16:59 +00003948 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003949}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003950
Steve Naroff563477d2007-09-18 23:55:05 +00003951// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003952Stmt::child_range ObjCMessageExpr::children() {
3953 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003954 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003955 begin = reinterpret_cast<Stmt **>(this + 1);
3956 else
3957 begin = reinterpret_cast<Stmt **>(getArgs());
3958 return child_range(begin,
3959 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003960}
3961
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003962ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003963 QualType T, ObjCMethodDecl *Method,
3964 SourceRange SR)
3965 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3966 false, false, false, false),
3967 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3968{
3969 Expr **SaveElements = getElements();
3970 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3971 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3972 ExprBits.ValueDependent = true;
3973 if (Elements[I]->isInstantiationDependent())
3974 ExprBits.InstantiationDependent = true;
3975 if (Elements[I]->containsUnexpandedParameterPack())
3976 ExprBits.ContainsUnexpandedParameterPack = true;
3977
3978 SaveElements[I] = Elements[I];
3979 }
3980}
3981
Craig Topper9db7a7e2013-08-22 04:58:56 +00003982ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003983 ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003984 QualType T, ObjCMethodDecl * Method,
3985 SourceRange SR) {
3986 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3987 + Elements.size() * sizeof(Expr *));
3988 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3989}
3990
Craig Topper9db7a7e2013-08-22 04:58:56 +00003991ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003992 unsigned NumElements) {
3993
3994 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3995 + NumElements * sizeof(Expr *));
3996 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3997}
3998
3999ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4000 ArrayRef<ObjCDictionaryElement> VK,
4001 bool HasPackExpansions,
4002 QualType T, ObjCMethodDecl *method,
4003 SourceRange SR)
4004 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4005 false, false),
4006 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4007 DictWithObjectsMethod(method)
4008{
4009 KeyValuePair *KeyValues = getKeyValues();
4010 ExpansionData *Expansions = getExpansionData();
4011 for (unsigned I = 0; I < NumElements; I++) {
4012 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4013 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4014 ExprBits.ValueDependent = true;
4015 if (VK[I].Key->isInstantiationDependent() ||
4016 VK[I].Value->isInstantiationDependent())
4017 ExprBits.InstantiationDependent = true;
4018 if (VK[I].EllipsisLoc.isInvalid() &&
4019 (VK[I].Key->containsUnexpandedParameterPack() ||
4020 VK[I].Value->containsUnexpandedParameterPack()))
4021 ExprBits.ContainsUnexpandedParameterPack = true;
4022
4023 KeyValues[I].Key = VK[I].Key;
4024 KeyValues[I].Value = VK[I].Value;
4025 if (Expansions) {
4026 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4027 if (VK[I].NumExpansions)
4028 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4029 else
4030 Expansions[I].NumExpansionsPlusOne = 0;
4031 }
4032 }
4033}
4034
4035ObjCDictionaryLiteral *
Craig Topper9db7a7e2013-08-22 04:58:56 +00004036ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004037 ArrayRef<ObjCDictionaryElement> VK,
4038 bool HasPackExpansions,
4039 QualType T, ObjCMethodDecl *method,
4040 SourceRange SR) {
4041 unsigned ExpansionsSize = 0;
4042 if (HasPackExpansions)
4043 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4044
4045 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4046 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4047 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4048}
4049
4050ObjCDictionaryLiteral *
Craig Topper9db7a7e2013-08-22 04:58:56 +00004051ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004052 bool HasPackExpansions) {
4053 unsigned ExpansionsSize = 0;
4054 if (HasPackExpansions)
4055 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4056 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4057 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4058 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4059 HasPackExpansions);
4060}
4061
Craig Topper9db7a7e2013-08-22 04:58:56 +00004062ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004063 Expr *base,
4064 Expr *key, QualType T,
4065 ObjCMethodDecl *getMethod,
4066 ObjCMethodDecl *setMethod,
4067 SourceLocation RB) {
4068 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4069 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4070 OK_ObjCSubscript,
4071 getMethod, setMethod, RB);
4072}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004073
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004074AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004075 QualType t, AtomicOp op, SourceLocation RP)
4076 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4077 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004078 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004079{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004080 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4081 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004082 if (args[i]->isTypeDependent())
4083 ExprBits.TypeDependent = true;
4084 if (args[i]->isValueDependent())
4085 ExprBits.ValueDependent = true;
4086 if (args[i]->isInstantiationDependent())
4087 ExprBits.InstantiationDependent = true;
4088 if (args[i]->containsUnexpandedParameterPack())
4089 ExprBits.ContainsUnexpandedParameterPack = true;
4090
4091 SubExprs[i] = args[i];
4092 }
4093}
Richard Smithe1b2abc2012-04-10 22:49:28 +00004094
4095unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4096 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00004097 case AO__c11_atomic_init:
4098 case AO__c11_atomic_load:
4099 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004100 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00004101
4102 case AO__c11_atomic_store:
4103 case AO__c11_atomic_exchange:
4104 case AO__atomic_load:
4105 case AO__atomic_store:
4106 case AO__atomic_store_n:
4107 case AO__atomic_exchange_n:
4108 case AO__c11_atomic_fetch_add:
4109 case AO__c11_atomic_fetch_sub:
4110 case AO__c11_atomic_fetch_and:
4111 case AO__c11_atomic_fetch_or:
4112 case AO__c11_atomic_fetch_xor:
4113 case AO__atomic_fetch_add:
4114 case AO__atomic_fetch_sub:
4115 case AO__atomic_fetch_and:
4116 case AO__atomic_fetch_or:
4117 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00004118 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00004119 case AO__atomic_add_fetch:
4120 case AO__atomic_sub_fetch:
4121 case AO__atomic_and_fetch:
4122 case AO__atomic_or_fetch:
4123 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00004124 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004125 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00004126
4127 case AO__atomic_exchange:
4128 return 4;
4129
4130 case AO__c11_atomic_compare_exchange_strong:
4131 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004132 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00004133
4134 case AO__atomic_compare_exchange:
4135 case AO__atomic_compare_exchange_n:
4136 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00004137 }
4138 llvm_unreachable("unknown atomic op");
4139}