blob: 37f7d267d97c41d6e380dac8460d03bcf9d7ef29 [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.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000241static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregord967e312011-01-19 21:52:31 +0000242 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
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000331void DeclRefExpr::computeDependence(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
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000365DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
366 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
Douglas Gregora2813ce2009-10-23 18:54:35 +0000402DeclRefExpr *DeclRefExpr::Create(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
418DeclRefExpr *DeclRefExpr::Create(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
Chandler Carruth3aa81402011-05-01 23:48:14 +0000448DeclRefExpr *DeclRefExpr::CreateEmpty(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
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000588 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
589 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000590
591 Out << Proto;
592
593 Out.flush();
594 return Name.str().str();
595 }
596 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000597 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000598 llvm::raw_svector_ostream Out(Name);
599 Out << (MD->isInstanceMethod() ? '-' : '+');
600 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000601
602 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
603 // a null check to avoid a crash.
604 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000605 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000606
Anders Carlsson3a082d82009-09-08 18:24:21 +0000607 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000608 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000609 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000610
Anders Carlsson3a082d82009-09-08 18:24:21 +0000611 Out << ' ';
612 Out << MD->getSelector().getAsString();
613 Out << ']';
614
615 Out.flush();
616 return Name.str().str();
617 }
618 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
619 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
620 return "top level";
621 }
622 return "";
623}
624
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000625void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
626 if (hasAllocation())
627 C.Deallocate(pVal);
628
629 BitWidth = Val.getBitWidth();
630 unsigned NumWords = Val.getNumWords();
631 const uint64_t* Words = Val.getRawData();
632 if (NumWords > 1) {
633 pVal = new (C) uint64_t[NumWords];
634 std::copy(Words, Words + NumWords, pVal);
635 } else if (NumWords == 1)
636 VAL = Words[0];
637 else
638 VAL = 0;
639}
640
Benjamin Kramer478851c2012-07-04 17:04:04 +0000641IntegerLiteral::IntegerLiteral(ASTContext &C, const llvm::APInt &V,
642 QualType type, SourceLocation l)
643 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
644 false, false),
645 Loc(l) {
646 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
647 assert(V.getBitWidth() == C.getIntWidth(type) &&
648 "Integer type is not the correct size for constant.");
649 setValue(C, V);
650}
651
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000652IntegerLiteral *
653IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
654 QualType type, SourceLocation l) {
655 return new (C) IntegerLiteral(C, V, type, l);
656}
657
658IntegerLiteral *
659IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
660 return new (C) IntegerLiteral(Empty);
661}
662
Benjamin Kramer478851c2012-07-04 17:04:04 +0000663FloatingLiteral::FloatingLiteral(ASTContext &C, const llvm::APFloat &V,
664 bool isexact, QualType Type, SourceLocation L)
665 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
666 false, false), Loc(L) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000667 setSemantics(V.getSemantics());
Benjamin Kramer478851c2012-07-04 17:04:04 +0000668 FloatingLiteralBits.IsExact = isexact;
669 setValue(C, V);
670}
671
672FloatingLiteral::FloatingLiteral(ASTContext &C, EmptyShell Empty)
673 : Expr(FloatingLiteralClass, Empty) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000674 setRawSemantics(IEEEhalf);
Benjamin Kramer478851c2012-07-04 17:04:04 +0000675 FloatingLiteralBits.IsExact = false;
676}
677
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000678FloatingLiteral *
679FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
680 bool isexact, QualType Type, SourceLocation L) {
681 return new (C) FloatingLiteral(C, V, isexact, Type, L);
682}
683
684FloatingLiteral *
685FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000686 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000687}
688
Tim Northover9ec55f22013-01-22 09:46:51 +0000689const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
690 switch(FloatingLiteralBits.Semantics) {
691 case IEEEhalf:
692 return llvm::APFloat::IEEEhalf;
693 case IEEEsingle:
694 return llvm::APFloat::IEEEsingle;
695 case IEEEdouble:
696 return llvm::APFloat::IEEEdouble;
697 case x87DoubleExtended:
698 return llvm::APFloat::x87DoubleExtended;
699 case IEEEquad:
700 return llvm::APFloat::IEEEquad;
701 case PPCDoubleDouble:
702 return llvm::APFloat::PPCDoubleDouble;
703 }
704 llvm_unreachable("Unrecognised floating semantics");
705}
706
707void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
708 if (&Sem == &llvm::APFloat::IEEEhalf)
709 FloatingLiteralBits.Semantics = IEEEhalf;
710 else if (&Sem == &llvm::APFloat::IEEEsingle)
711 FloatingLiteralBits.Semantics = IEEEsingle;
712 else if (&Sem == &llvm::APFloat::IEEEdouble)
713 FloatingLiteralBits.Semantics = IEEEdouble;
714 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
715 FloatingLiteralBits.Semantics = x87DoubleExtended;
716 else if (&Sem == &llvm::APFloat::IEEEquad)
717 FloatingLiteralBits.Semantics = IEEEquad;
718 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
719 FloatingLiteralBits.Semantics = PPCDoubleDouble;
720 else
721 llvm_unreachable("Unknown floating semantics");
722}
723
Chris Lattnerda8249e2008-06-07 22:13:43 +0000724/// getValueAsApproximateDouble - This returns the value as an inaccurate
725/// double. Note that this may cause loss of precision, but is useful for
726/// debugging dumps, etc.
727double FloatingLiteral::getValueAsApproximateDouble() const {
728 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000729 bool ignored;
730 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
731 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000732 return V.convertToDouble();
733}
734
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000735int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000736 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000737 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000738 case Ascii:
739 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000740 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000741 break;
742 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000743 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000744 break;
745 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000746 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000747 break;
748 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000749 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000750 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000751 }
752 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
753 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000754 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000755 && "character byte widths supported are 1, 2, and 4 only");
756 return CharByteWidth;
757}
758
Chris Lattner5f9e2722011-07-23 10:55:15 +0000759StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000760 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000761 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000762 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000763 // Allocate enough space for the StringLiteral plus an array of locations for
764 // any concatenated string tokens.
765 void *Mem = C.Allocate(sizeof(StringLiteral)+
766 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000767 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000768 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000771 SL->setString(C,Str,Kind,Pascal);
772
Chris Lattner2085fd62009-02-18 06:40:38 +0000773 SL->TokLocs[0] = Loc[0];
774 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000775
Chris Lattner726e1682009-02-18 05:49:11 +0000776 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000777 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
778 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000779}
780
Douglas Gregor673ecd62009-04-15 16:35:07 +0000781StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
782 void *Mem = C.Allocate(sizeof(StringLiteral)+
783 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000784 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000785 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000786 SL->CharByteWidth = 0;
787 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000788 SL->NumConcatenated = NumStrs;
789 return SL;
790}
791
Alexander Kornienkoae541212013-02-01 12:35:51 +0000792void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieu8ab09da2012-06-13 20:25:24 +0000793 switch (getKind()) {
794 case Ascii: break; // no prefix.
795 case Wide: OS << 'L'; break;
796 case UTF8: OS << "u8"; break;
797 case UTF16: OS << 'u'; break;
798 case UTF32: OS << 'U'; break;
799 }
800 OS << '"';
801 static const char Hex[] = "0123456789ABCDEF";
802
803 unsigned LastSlashX = getLength();
804 for (unsigned I = 0, N = getLength(); I != N; ++I) {
805 switch (uint32_t Char = getCodeUnit(I)) {
806 default:
807 // FIXME: Convert UTF-8 back to codepoints before rendering.
808
809 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
810 // Leave invalid surrogates alone; we'll use \x for those.
811 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
812 Char <= 0xdbff) {
813 uint32_t Trail = getCodeUnit(I + 1);
814 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
815 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
816 ++I;
817 }
818 }
819
820 if (Char > 0xff) {
821 // If this is a wide string, output characters over 0xff using \x
822 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
823 // codepoint: use \x escapes for invalid codepoints.
824 if (getKind() == Wide ||
825 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
826 // FIXME: Is this the best way to print wchar_t?
827 OS << "\\x";
828 int Shift = 28;
829 while ((Char >> Shift) == 0)
830 Shift -= 4;
831 for (/**/; Shift >= 0; Shift -= 4)
832 OS << Hex[(Char >> Shift) & 15];
833 LastSlashX = I;
834 break;
835 }
836
837 if (Char > 0xffff)
838 OS << "\\U00"
839 << Hex[(Char >> 20) & 15]
840 << Hex[(Char >> 16) & 15];
841 else
842 OS << "\\u";
843 OS << Hex[(Char >> 12) & 15]
844 << Hex[(Char >> 8) & 15]
845 << Hex[(Char >> 4) & 15]
846 << Hex[(Char >> 0) & 15];
847 break;
848 }
849
850 // If we used \x... for the previous character, and this character is a
851 // hexadecimal digit, prevent it being slurped as part of the \x.
852 if (LastSlashX + 1 == I) {
853 switch (Char) {
854 case '0': case '1': case '2': case '3': case '4':
855 case '5': case '6': case '7': case '8': case '9':
856 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
857 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
858 OS << "\"\"";
859 }
860 }
861
862 assert(Char <= 0xff &&
863 "Characters above 0xff should already have been handled.");
864
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000865 if (isPrintable(Char))
Richard Trieu8ab09da2012-06-13 20:25:24 +0000866 OS << (char)Char;
867 else // Output anything hard as an octal escape.
868 OS << '\\'
869 << (char)('0' + ((Char >> 6) & 7))
870 << (char)('0' + ((Char >> 3) & 7))
871 << (char)('0' + ((Char >> 0) & 7));
872 break;
873 // Handle some common non-printable cases to make dumps prettier.
874 case '\\': OS << "\\\\"; break;
875 case '"': OS << "\\\""; break;
876 case '\n': OS << "\\n"; break;
877 case '\t': OS << "\\t"; break;
878 case '\a': OS << "\\a"; break;
879 case '\b': OS << "\\b"; break;
880 }
881 }
882 OS << '"';
883}
884
Eli Friedman64f45a22011-11-01 02:23:42 +0000885void StringLiteral::setString(ASTContext &C, StringRef Str,
886 StringKind Kind, bool IsPascal) {
887 //FIXME: we assume that the string data comes from a target that uses the same
888 // code unit size and endianess for the type of string.
889 this->Kind = Kind;
890 this->IsPascal = IsPascal;
891
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000892 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000893 assert((Str.size()%CharByteWidth == 0)
894 && "size of data must be multiple of CharByteWidth");
895 Length = Str.size()/CharByteWidth;
896
897 switch(CharByteWidth) {
898 case 1: {
899 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000900 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000901 StrData.asChar = AStrData;
902 break;
903 }
904 case 2: {
905 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000906 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000907 StrData.asUInt16 = AStrData;
908 break;
909 }
910 case 4: {
911 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000912 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000913 StrData.asUInt32 = AStrData;
914 break;
915 }
916 default:
917 assert(false && "unsupported CharByteWidth");
918 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000919}
920
Chris Lattner08f92e32010-11-17 07:37:15 +0000921/// getLocationOfByte - Return a source location that points to the specified
922/// byte of this string literal.
923///
924/// Strings are amazingly complex. They can be formed from multiple tokens and
925/// can have escape sequences in them in addition to the usual trigraph and
926/// escaped newline business. This routine handles this complexity.
927///
928SourceLocation StringLiteral::
929getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
930 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000931 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
932 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000933
Chris Lattner08f92e32010-11-17 07:37:15 +0000934 // Loop over all of the tokens in this string until we find the one that
935 // contains the byte we're looking for.
936 unsigned TokNo = 0;
937 while (1) {
938 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
939 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
940
941 // Get the spelling of the string so that we can get the data that makes up
942 // the string literal, not the identifier for the macro it is potentially
943 // expanded through.
944 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
945
946 // Re-lex the token to get its length and original spelling.
947 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
948 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000949 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000950 if (Invalid)
951 return StrTokSpellingLoc;
952
953 const char *StrData = Buffer.data()+LocInfo.second;
954
Chris Lattner08f92e32010-11-17 07:37:15 +0000955 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000956 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
957 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000958 Token TheTok;
959 TheLexer.LexFromRawLexer(TheTok);
960
961 // Use the StringLiteralParser to compute the length of the string in bytes.
962 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
963 unsigned TokNumBytes = SLP.GetStringLength();
964
965 // If the byte is in this token, return the location of the byte.
966 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000967 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000968 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
969
970 // Now that we know the offset of the token in the spelling, use the
971 // preprocessor to get the offset in the original source.
972 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
973 }
974
975 // Move to the next string token.
976 ++TokNo;
977 ByteNo -= TokNumBytes;
978 }
979}
980
981
982
Reid Spencer5f016e22007-07-11 17:01:13 +0000983/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
984/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie0bea8632012-10-08 01:11:04 +0000985StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000987 case UO_PostInc: return "++";
988 case UO_PostDec: return "--";
989 case UO_PreInc: return "++";
990 case UO_PreDec: return "--";
991 case UO_AddrOf: return "&";
992 case UO_Deref: return "*";
993 case UO_Plus: return "+";
994 case UO_Minus: return "-";
995 case UO_Not: return "~";
996 case UO_LNot: return "!";
997 case UO_Real: return "__real";
998 case UO_Imag: return "__imag";
999 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001001 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +00001002}
1003
John McCall2de56d12010-08-25 11:45:40 +00001004UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001005UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1006 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001007 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +00001008 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1009 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1010 case OO_Amp: return UO_AddrOf;
1011 case OO_Star: return UO_Deref;
1012 case OO_Plus: return UO_Plus;
1013 case OO_Minus: return UO_Minus;
1014 case OO_Tilde: return UO_Not;
1015 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001016 }
1017}
1018
1019OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1020 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +00001021 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1022 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1023 case UO_AddrOf: return OO_Amp;
1024 case UO_Deref: return OO_Star;
1025 case UO_Plus: return OO_Plus;
1026 case UO_Minus: return OO_Minus;
1027 case UO_Not: return OO_Tilde;
1028 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001029 default: return OO_None;
1030 }
1031}
1032
1033
Reid Spencer5f016e22007-07-11 17:01:13 +00001034//===----------------------------------------------------------------------===//
1035// Postfix Operators.
1036//===----------------------------------------------------------------------===//
1037
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001038CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001039 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +00001040 SourceLocation rparenloc)
1041 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001042 fn->isTypeDependent(),
1043 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001044 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001045 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001046 NumArgs(args.size()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001048 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +00001049 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001050 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001051 if (args[i]->isTypeDependent())
1052 ExprBits.TypeDependent = true;
1053 if (args[i]->isValueDependent())
1054 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001055 if (args[i]->isInstantiationDependent())
1056 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001057 if (args[i]->containsUnexpandedParameterPack())
1058 ExprBits.ContainsUnexpandedParameterPack = true;
1059
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001060 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001061 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001062
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001063 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +00001064 RParenLoc = rparenloc;
1065}
Nate Begemane2ce1d92008-01-17 17:46:27 +00001066
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001067CallExpr::CallExpr(ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCallf89e55a2010-11-18 06:31:45 +00001068 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1069 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001070 fn->isTypeDependent(),
1071 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001072 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001073 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001074 NumArgs(args.size()) {
Ted Kremenek668bf912009-02-09 20:51:47 +00001075
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001076 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001077 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001078 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001079 if (args[i]->isTypeDependent())
1080 ExprBits.TypeDependent = true;
1081 if (args[i]->isValueDependent())
1082 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001083 if (args[i]->isInstantiationDependent())
1084 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001085 if (args[i]->containsUnexpandedParameterPack())
1086 ExprBits.ContainsUnexpandedParameterPack = true;
1087
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001088 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001089 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001090
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001091 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 RParenLoc = rparenloc;
1093}
1094
Mike Stump1eb44332009-09-09 15:08:12 +00001095CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
1096 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001097 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001098 SubExprs = new (C) Stmt*[PREARGS_START];
1099 CallExprBits.NumPreArgs = 0;
1100}
1101
1102CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
1103 EmptyShell Empty)
1104 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1105 // FIXME: Why do we allocate this?
1106 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1107 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001108}
1109
Nuno Lopesd20254f2009-12-20 23:11:08 +00001110Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +00001111 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +00001112
1113 while (SubstNonTypeTemplateParmExpr *NTTP
1114 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1115 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1116 }
1117
Sebastian Redl20012152010-09-10 20:55:30 +00001118 // If we're calling a dereference, look at the pointer instead.
1119 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1120 if (BO->isPtrMemOp())
1121 CEE = BO->getRHS()->IgnoreParenCasts();
1122 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1123 if (UO->getOpcode() == UO_Deref)
1124 CEE = UO->getSubExpr()->IgnoreParenCasts();
1125 }
Chris Lattner6346f962009-07-17 15:46:27 +00001126 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001127 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001128 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1129 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001130
1131 return 0;
1132}
1133
Nuno Lopesd20254f2009-12-20 23:11:08 +00001134FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001135 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001136}
1137
Chris Lattnerd18b3292007-12-28 05:25:02 +00001138/// setNumArgs - This changes the number of arguments present in this call.
1139/// Any orphaned expressions are deleted by this, and any new operands are set
1140/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001141void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001142 // No change, just return.
1143 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Chris Lattnerd18b3292007-12-28 05:25:02 +00001145 // If shrinking # arguments, just delete the extras and forgot them.
1146 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001147 this->NumArgs = NumArgs;
1148 return;
1149 }
1150
1151 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001152 unsigned NumPreArgs = getNumPreArgs();
1153 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001154 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001155 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001156 NewSubExprs[i] = SubExprs[i];
1157 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001158 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1159 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001160 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Douglas Gregor88c9a462009-04-17 21:46:47 +00001162 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001163 SubExprs = NewSubExprs;
1164 this->NumArgs = NumArgs;
1165}
1166
Chris Lattnercb888962008-10-06 05:00:53 +00001167/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1168/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001169unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001170 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001171 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001172 // ImplicitCastExpr.
1173 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1174 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001175 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001177 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1178 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001179 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Anders Carlssonbcba2012008-01-31 02:13:57 +00001181 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1182 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001183 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001185 if (!FDecl->getIdentifier())
1186 return 0;
1187
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001188 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001189}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001190
Richard Smithba571832013-01-17 23:46:04 +00001191bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1192 if (unsigned BI = isBuiltinCall())
1193 return Ctx.BuiltinInfo.isUnevaluated(BI);
1194 return false;
1195}
1196
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001197QualType CallExpr::getCallReturnType() const {
1198 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001199 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001200 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001201 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001202 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001203 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1204 // This should never be overloaded and so should never return null.
1205 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001206
John McCall864c0412011-04-26 20:42:42 +00001207 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001208 return FnType->getResultType();
1209}
Chris Lattnercb888962008-10-06 05:00:53 +00001210
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001211SourceLocation CallExpr::getLocStart() const {
1212 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001213 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001214
1215 SourceLocation begin = getCallee()->getLocStart();
1216 if (begin.isInvalid() && getNumArgs() > 0)
1217 begin = getArg(0)->getLocStart();
1218 return begin;
1219}
1220SourceLocation CallExpr::getLocEnd() const {
1221 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001222 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001223
1224 SourceLocation end = getRParenLoc();
1225 if (end.isInvalid() && getNumArgs() > 0)
1226 end = getArg(getNumArgs() - 1)->getLocEnd();
1227 return end;
1228}
John McCall2882eca2011-02-21 06:23:05 +00001229
Sean Huntc3021132010-05-05 15:23:54 +00001230OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001231 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001232 TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001233 ArrayRef<OffsetOfNode> comps,
1234 ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001235 SourceLocation RParenLoc) {
1236 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001237 sizeof(OffsetOfNode) * comps.size() +
1238 sizeof(Expr*) * exprs.size());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001239
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001240 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1241 RParenLoc);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001242}
1243
1244OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1245 unsigned numComps, unsigned numExprs) {
1246 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1247 sizeof(OffsetOfNode) * numComps +
1248 sizeof(Expr*) * numExprs);
1249 return new (Mem) OffsetOfExpr(numComps, numExprs);
1250}
1251
Sean Huntc3021132010-05-05 15:23:54 +00001252OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001253 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001254 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001255 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001256 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1257 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001258 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001259 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001260 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001261 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001262 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001263{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001264 for (unsigned i = 0; i != comps.size(); ++i) {
1265 setComponent(i, comps[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001266 }
Sean Huntc3021132010-05-05 15:23:54 +00001267
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001268 for (unsigned i = 0; i != exprs.size(); ++i) {
1269 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001270 ExprBits.ValueDependent = true;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001271 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001272 ExprBits.ContainsUnexpandedParameterPack = true;
1273
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001274 setIndexExpr(i, exprs[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001275 }
1276}
1277
1278IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1279 assert(getKind() == Field || getKind() == Identifier);
1280 if (getKind() == Field)
1281 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001282
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001283 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1284}
1285
Mike Stump1eb44332009-09-09 15:08:12 +00001286MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001287 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001288 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001289 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001290 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001291 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001292 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001293 QualType ty,
1294 ExprValueKind vk,
1295 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001296 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001297
Douglas Gregor40d96a62011-02-28 21:54:11 +00001298 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001299 founddecl.getDecl() != memberdecl ||
1300 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001301 if (hasQualOrFound)
1302 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001303
John McCalld5532b62009-11-23 01:53:49 +00001304 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001305 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1306 else if (TemplateKWLoc.isValid())
1307 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Chris Lattner32488542010-10-30 05:14:06 +00001309 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001310 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1311 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001312
1313 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001314 // FIXME: Wrong. We should be looking at the member declaration we found.
1315 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001316 E->setValueDependent(true);
1317 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001318 E->setInstantiationDependent(true);
1319 }
1320 else if (QualifierLoc &&
1321 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1322 E->setInstantiationDependent(true);
1323
John McCall6bb80172010-03-30 21:47:33 +00001324 E->HasQualifierOrFoundDecl = true;
1325
1326 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001327 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001328 NQ->FoundDecl = founddecl;
1329 }
1330
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001331 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1332
John McCall6bb80172010-03-30 21:47:33 +00001333 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001334 bool Dependent = false;
1335 bool InstantiationDependent = false;
1336 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001337 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1338 Dependent,
1339 InstantiationDependent,
1340 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001341 if (InstantiationDependent)
1342 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001343 } else if (TemplateKWLoc.isValid()) {
1344 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001345 }
1346
1347 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001348}
1349
Daniel Dunbar396ec672012-03-09 15:39:15 +00001350SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001351 if (isImplicitAccess()) {
1352 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001353 return getQualifierLoc().getBeginLoc();
1354 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001355 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001356
Daniel Dunbar396ec672012-03-09 15:39:15 +00001357 // FIXME: We don't want this to happen. Rather, we should be able to
1358 // detect all kinds of implicit accesses more cleanly.
1359 SourceLocation BaseStartLoc = getBase()->getLocStart();
1360 if (BaseStartLoc.isValid())
1361 return BaseStartLoc;
1362 return MemberLoc;
1363}
1364SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001365 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbar396ec672012-03-09 15:39:15 +00001366 if (hasExplicitTemplateArgs())
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001367 EndLoc = getRAngleLoc();
1368 else if (EndLoc.isInvalid())
1369 EndLoc = getBase()->getLocEnd();
1370 return EndLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001371}
1372
John McCall1d9b3b22011-09-09 05:25:32 +00001373void CastExpr::CheckCastConsistency() const {
1374 switch (getCastKind()) {
1375 case CK_DerivedToBase:
1376 case CK_UncheckedDerivedToBase:
1377 case CK_DerivedToBaseMemberPointer:
1378 case CK_BaseToDerived:
1379 case CK_BaseToDerivedMemberPointer:
1380 assert(!path_empty() && "Cast kind should have a base path!");
1381 break;
1382
1383 case CK_CPointerToObjCPointerCast:
1384 assert(getType()->isObjCObjectPointerType());
1385 assert(getSubExpr()->getType()->isPointerType());
1386 goto CheckNoBasePath;
1387
1388 case CK_BlockPointerToObjCPointerCast:
1389 assert(getType()->isObjCObjectPointerType());
1390 assert(getSubExpr()->getType()->isBlockPointerType());
1391 goto CheckNoBasePath;
1392
John McCall4d4e5c12012-02-15 01:22:51 +00001393 case CK_ReinterpretMemberPointer:
1394 assert(getType()->isMemberPointerType());
1395 assert(getSubExpr()->getType()->isMemberPointerType());
1396 goto CheckNoBasePath;
1397
John McCall1d9b3b22011-09-09 05:25:32 +00001398 case CK_BitCast:
1399 // Arbitrary casts to C pointer types count as bitcasts.
1400 // Otherwise, we should only have block and ObjC pointer casts
1401 // here if they stay within the type kind.
1402 if (!getType()->isPointerType()) {
1403 assert(getType()->isObjCObjectPointerType() ==
1404 getSubExpr()->getType()->isObjCObjectPointerType());
1405 assert(getType()->isBlockPointerType() ==
1406 getSubExpr()->getType()->isBlockPointerType());
1407 }
1408 goto CheckNoBasePath;
1409
1410 case CK_AnyPointerToBlockPointerCast:
1411 assert(getType()->isBlockPointerType());
1412 assert(getSubExpr()->getType()->isAnyPointerType() &&
1413 !getSubExpr()->getType()->isBlockPointerType());
1414 goto CheckNoBasePath;
1415
Douglas Gregorac1303e2012-02-22 05:02:47 +00001416 case CK_CopyAndAutoreleaseBlockObject:
1417 assert(getType()->isBlockPointerType());
1418 assert(getSubExpr()->getType()->isBlockPointerType());
1419 goto CheckNoBasePath;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001420
1421 case CK_FunctionToPointerDecay:
1422 assert(getType()->isPointerType());
1423 assert(getSubExpr()->getType()->isFunctionType());
1424 goto CheckNoBasePath;
1425
John McCall1d9b3b22011-09-09 05:25:32 +00001426 // These should not have an inheritance path.
1427 case CK_Dynamic:
1428 case CK_ToUnion:
1429 case CK_ArrayToPointerDecay:
John McCall1d9b3b22011-09-09 05:25:32 +00001430 case CK_NullToMemberPointer:
1431 case CK_NullToPointer:
1432 case CK_ConstructorConversion:
1433 case CK_IntegralToPointer:
1434 case CK_PointerToIntegral:
1435 case CK_ToVoid:
1436 case CK_VectorSplat:
1437 case CK_IntegralCast:
1438 case CK_IntegralToFloating:
1439 case CK_FloatingToIntegral:
1440 case CK_FloatingCast:
1441 case CK_ObjCObjectLValueCast:
1442 case CK_FloatingRealToComplex:
1443 case CK_FloatingComplexToReal:
1444 case CK_FloatingComplexCast:
1445 case CK_FloatingComplexToIntegralComplex:
1446 case CK_IntegralRealToComplex:
1447 case CK_IntegralComplexToReal:
1448 case CK_IntegralComplexCast:
1449 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001450 case CK_ARCProduceObject:
1451 case CK_ARCConsumeObject:
1452 case CK_ARCReclaimReturnedObject:
1453 case CK_ARCExtendBlockObject:
Guy Benyeie6b9d802013-01-20 12:31:11 +00001454 case CK_ZeroToOCLEvent:
John McCall1d9b3b22011-09-09 05:25:32 +00001455 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1456 goto CheckNoBasePath;
1457
1458 case CK_Dependent:
1459 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001460 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001461 case CK_AtomicToNonAtomic:
1462 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001463 case CK_PointerToBoolean:
1464 case CK_IntegralToBoolean:
1465 case CK_FloatingToBoolean:
1466 case CK_MemberPointerToBoolean:
1467 case CK_FloatingComplexToBoolean:
1468 case CK_IntegralComplexToBoolean:
1469 case CK_LValueBitCast: // -> bool&
1470 case CK_UserDefinedConversion: // operator bool()
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001471 case CK_BuiltinFnToFnPtr:
John McCall1d9b3b22011-09-09 05:25:32 +00001472 CheckNoBasePath:
1473 assert(path_empty() && "Cast kind should not have a base path!");
1474 break;
1475 }
1476}
1477
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001478const char *CastExpr::getCastKindName() const {
1479 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001480 case CK_Dependent:
1481 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001482 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001483 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001484 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001485 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001486 case CK_LValueToRValue:
1487 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001488 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001489 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001490 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001491 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001492 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001493 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001494 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001495 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001496 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001497 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001498 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001499 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001500 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001501 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001502 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001503 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001504 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001505 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001506 case CK_NullToPointer:
1507 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001508 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001509 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001510 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001511 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001512 case CK_ReinterpretMemberPointer:
1513 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001514 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001515 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001516 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001517 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001518 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001519 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001520 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001521 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001522 case CK_PointerToBoolean:
1523 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001524 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001525 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001526 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001527 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001528 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001529 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001530 case CK_IntegralToBoolean:
1531 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001532 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001533 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001534 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001535 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001536 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001537 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001538 case CK_FloatingToBoolean:
1539 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001540 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001541 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001542 case CK_CPointerToObjCPointerCast:
1543 return "CPointerToObjCPointerCast";
1544 case CK_BlockPointerToObjCPointerCast:
1545 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001546 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001547 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001548 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001549 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001550 case CK_FloatingRealToComplex:
1551 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001552 case CK_FloatingComplexToReal:
1553 return "FloatingComplexToReal";
1554 case CK_FloatingComplexToBoolean:
1555 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001556 case CK_FloatingComplexCast:
1557 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001558 case CK_FloatingComplexToIntegralComplex:
1559 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001560 case CK_IntegralRealToComplex:
1561 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001562 case CK_IntegralComplexToReal:
1563 return "IntegralComplexToReal";
1564 case CK_IntegralComplexToBoolean:
1565 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001566 case CK_IntegralComplexCast:
1567 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001568 case CK_IntegralComplexToFloatingComplex:
1569 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001570 case CK_ARCConsumeObject:
1571 return "ARCConsumeObject";
1572 case CK_ARCProduceObject:
1573 return "ARCProduceObject";
1574 case CK_ARCReclaimReturnedObject:
1575 return "ARCReclaimReturnedObject";
1576 case CK_ARCExtendBlockObject:
1577 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001578 case CK_AtomicToNonAtomic:
1579 return "AtomicToNonAtomic";
1580 case CK_NonAtomicToAtomic:
1581 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001582 case CK_CopyAndAutoreleaseBlockObject:
1583 return "CopyAndAutoreleaseBlockObject";
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001584 case CK_BuiltinFnToFnPtr:
1585 return "BuiltinFnToFnPtr";
Guy Benyeie6b9d802013-01-20 12:31:11 +00001586 case CK_ZeroToOCLEvent:
1587 return "ZeroToOCLEvent";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001588 }
Mike Stump1eb44332009-09-09 15:08:12 +00001589
John McCall2bb5d002010-11-13 09:02:35 +00001590 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001591}
1592
Douglas Gregor6eef5192009-12-14 19:27:10 +00001593Expr *CastExpr::getSubExprAsWritten() {
1594 Expr *SubExpr = 0;
1595 CastExpr *E = this;
1596 do {
1597 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001598
1599 // Skip through reference binding to temporary.
1600 if (MaterializeTemporaryExpr *Materialize
1601 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1602 SubExpr = Materialize->GetTemporaryExpr();
1603
Douglas Gregor6eef5192009-12-14 19:27:10 +00001604 // Skip any temporary bindings; they're implicit.
1605 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1606 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001607
Douglas Gregor6eef5192009-12-14 19:27:10 +00001608 // Conversions by constructor and conversion functions have a
1609 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001610 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001611 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001612 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001613 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001614
Douglas Gregor6eef5192009-12-14 19:27:10 +00001615 // If the subexpression we're left with is an implicit cast, look
1616 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001617 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1618
Douglas Gregor6eef5192009-12-14 19:27:10 +00001619 return SubExpr;
1620}
1621
John McCallf871d0c2010-08-07 06:22:56 +00001622CXXBaseSpecifier **CastExpr::path_buffer() {
1623 switch (getStmtClass()) {
1624#define ABSTRACT_STMT(x)
1625#define CASTEXPR(Type, Base) \
1626 case Stmt::Type##Class: \
1627 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1628#define STMT(Type, Base)
1629#include "clang/AST/StmtNodes.inc"
1630 default:
1631 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001632 }
1633}
1634
1635void CastExpr::setCastPath(const CXXCastPath &Path) {
1636 assert(Path.size() == path_size());
1637 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1638}
1639
1640ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1641 CastKind Kind, Expr *Operand,
1642 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001643 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001644 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1645 void *Buffer =
1646 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1647 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001648 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001649 if (PathSize) E->setCastPath(*BasePath);
1650 return E;
1651}
1652
1653ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1654 unsigned PathSize) {
1655 void *Buffer =
1656 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1657 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1658}
1659
1660
1661CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001662 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001663 const CXXCastPath *BasePath,
1664 TypeSourceInfo *WrittenTy,
1665 SourceLocation L, SourceLocation R) {
1666 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1667 void *Buffer =
1668 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1669 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001670 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001671 if (PathSize) E->setCastPath(*BasePath);
1672 return E;
1673}
1674
1675CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1676 void *Buffer =
1677 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1678 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1679}
1680
Reid Spencer5f016e22007-07-11 17:01:13 +00001681/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1682/// corresponds to, e.g. "<<=".
David Blaikie0bea8632012-10-08 01:11:04 +00001683StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001685 case BO_PtrMemD: return ".*";
1686 case BO_PtrMemI: return "->*";
1687 case BO_Mul: return "*";
1688 case BO_Div: return "/";
1689 case BO_Rem: return "%";
1690 case BO_Add: return "+";
1691 case BO_Sub: return "-";
1692 case BO_Shl: return "<<";
1693 case BO_Shr: return ">>";
1694 case BO_LT: return "<";
1695 case BO_GT: return ">";
1696 case BO_LE: return "<=";
1697 case BO_GE: return ">=";
1698 case BO_EQ: return "==";
1699 case BO_NE: return "!=";
1700 case BO_And: return "&";
1701 case BO_Xor: return "^";
1702 case BO_Or: return "|";
1703 case BO_LAnd: return "&&";
1704 case BO_LOr: return "||";
1705 case BO_Assign: return "=";
1706 case BO_MulAssign: return "*=";
1707 case BO_DivAssign: return "/=";
1708 case BO_RemAssign: return "%=";
1709 case BO_AddAssign: return "+=";
1710 case BO_SubAssign: return "-=";
1711 case BO_ShlAssign: return "<<=";
1712 case BO_ShrAssign: return ">>=";
1713 case BO_AndAssign: return "&=";
1714 case BO_XorAssign: return "^=";
1715 case BO_OrAssign: return "|=";
1716 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001717 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001718
David Blaikie30263482012-01-20 21:50:17 +00001719 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001720}
1721
John McCall2de56d12010-08-25 11:45:40 +00001722BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001723BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1724 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001725 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001726 case OO_Plus: return BO_Add;
1727 case OO_Minus: return BO_Sub;
1728 case OO_Star: return BO_Mul;
1729 case OO_Slash: return BO_Div;
1730 case OO_Percent: return BO_Rem;
1731 case OO_Caret: return BO_Xor;
1732 case OO_Amp: return BO_And;
1733 case OO_Pipe: return BO_Or;
1734 case OO_Equal: return BO_Assign;
1735 case OO_Less: return BO_LT;
1736 case OO_Greater: return BO_GT;
1737 case OO_PlusEqual: return BO_AddAssign;
1738 case OO_MinusEqual: return BO_SubAssign;
1739 case OO_StarEqual: return BO_MulAssign;
1740 case OO_SlashEqual: return BO_DivAssign;
1741 case OO_PercentEqual: return BO_RemAssign;
1742 case OO_CaretEqual: return BO_XorAssign;
1743 case OO_AmpEqual: return BO_AndAssign;
1744 case OO_PipeEqual: return BO_OrAssign;
1745 case OO_LessLess: return BO_Shl;
1746 case OO_GreaterGreater: return BO_Shr;
1747 case OO_LessLessEqual: return BO_ShlAssign;
1748 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1749 case OO_EqualEqual: return BO_EQ;
1750 case OO_ExclaimEqual: return BO_NE;
1751 case OO_LessEqual: return BO_LE;
1752 case OO_GreaterEqual: return BO_GE;
1753 case OO_AmpAmp: return BO_LAnd;
1754 case OO_PipePipe: return BO_LOr;
1755 case OO_Comma: return BO_Comma;
1756 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001757 }
1758}
1759
1760OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1761 static const OverloadedOperatorKind OverOps[] = {
1762 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1763 OO_Star, OO_Slash, OO_Percent,
1764 OO_Plus, OO_Minus,
1765 OO_LessLess, OO_GreaterGreater,
1766 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1767 OO_EqualEqual, OO_ExclaimEqual,
1768 OO_Amp,
1769 OO_Caret,
1770 OO_Pipe,
1771 OO_AmpAmp,
1772 OO_PipePipe,
1773 OO_Equal, OO_StarEqual,
1774 OO_SlashEqual, OO_PercentEqual,
1775 OO_PlusEqual, OO_MinusEqual,
1776 OO_LessLessEqual, OO_GreaterGreaterEqual,
1777 OO_AmpEqual, OO_CaretEqual,
1778 OO_PipeEqual,
1779 OO_Comma
1780 };
1781 return OverOps[Opc];
1782}
1783
Ted Kremenek709210f2010-04-13 23:39:13 +00001784InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001785 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001786 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001787 false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001788 InitExprs(C, initExprs.size()),
Abramo Bagnara23700f02012-11-08 18:41:43 +00001789 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001790{
1791 sawArrayRangeDesignator(false);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001792 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001793 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001794 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001795 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001796 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001797 if (initExprs[I]->isInstantiationDependent())
1798 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001799 if (initExprs[I]->containsUnexpandedParameterPack())
1800 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001801 }
Sean Huntc3021132010-05-05 15:23:54 +00001802
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001803 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001804}
Reid Spencer5f016e22007-07-11 17:01:13 +00001805
Ted Kremenek709210f2010-04-13 23:39:13 +00001806void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001807 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001808 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001809}
1810
Ted Kremenek709210f2010-04-13 23:39:13 +00001811void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001812 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001813}
1814
Ted Kremenek709210f2010-04-13 23:39:13 +00001815Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001816 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001817 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001818 InitExprs.back() = expr;
1819 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001820 }
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Douglas Gregor4c678342009-01-28 21:54:33 +00001822 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1823 InitExprs[Init] = expr;
1824 return Result;
1825}
1826
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001827void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001828 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001829 ArrayFillerOrUnionFieldInit = filler;
1830 // Fill out any "holes" in the array due to designated initializers.
1831 Expr **inits = getInits();
1832 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1833 if (inits[i] == 0)
1834 inits[i] = filler;
1835}
1836
Richard Smithfe587202012-04-15 02:50:59 +00001837bool InitListExpr::isStringLiteralInit() const {
1838 if (getNumInits() != 1)
1839 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001840 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1841 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smithfe587202012-04-15 02:50:59 +00001842 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001843 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smithfe587202012-04-15 02:50:59 +00001844 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1845}
1846
Erik Verbruggen65d78312012-12-25 14:51:39 +00001847SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara23700f02012-11-08 18:41:43 +00001848 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen65d78312012-12-25 14:51:39 +00001849 return SyntacticForm->getLocStart();
1850 SourceLocation Beg = LBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001851 if (Beg.isInvalid()) {
1852 // Find the first non-null initializer.
1853 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1854 E = InitExprs.end();
1855 I != E; ++I) {
1856 if (Stmt *S = *I) {
1857 Beg = S->getLocStart();
1858 break;
1859 }
1860 }
1861 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001862 return Beg;
1863}
1864
1865SourceLocation InitListExpr::getLocEnd() const {
1866 if (InitListExpr *SyntacticForm = getSyntacticForm())
1867 return SyntacticForm->getLocEnd();
1868 SourceLocation End = RBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001869 if (End.isInvalid()) {
1870 // Find the first non-null initializer from the end.
1871 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen65d78312012-12-25 14:51:39 +00001872 E = InitExprs.rend();
1873 I != E; ++I) {
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001874 if (Stmt *S = *I) {
Erik Verbruggen65d78312012-12-25 14:51:39 +00001875 End = S->getLocEnd();
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001876 break;
Erik Verbruggen65d78312012-12-25 14:51:39 +00001877 }
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001878 }
1879 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001880 return End;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001881}
1882
Steve Naroffbfdcae62008-09-04 15:31:07 +00001883/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001884///
John McCalla345edb2012-02-17 03:32:35 +00001885const FunctionProtoType *BlockExpr::getFunctionType() const {
1886 // The block pointer is never sugared, but the function type might be.
1887 return cast<BlockPointerType>(getType())
1888 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001889}
1890
Mike Stump1eb44332009-09-09 15:08:12 +00001891SourceLocation BlockExpr::getCaretLocation() const {
1892 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001893}
Mike Stump1eb44332009-09-09 15:08:12 +00001894const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001895 return TheBlock->getBody();
1896}
Mike Stump1eb44332009-09-09 15:08:12 +00001897Stmt *BlockExpr::getBody() {
1898 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001899}
Steve Naroff56ee6892008-10-08 17:01:13 +00001900
1901
Reid Spencer5f016e22007-07-11 17:01:13 +00001902//===----------------------------------------------------------------------===//
1903// Generic Expression Routines
1904//===----------------------------------------------------------------------===//
1905
Chris Lattner026dc962009-02-14 07:37:35 +00001906/// isUnusedResultAWarning - Return true if this immediate expression should
1907/// be warned about if the result is unused. If so, fill in Loc and Ranges
1908/// with location to warn on and the source range[s] to report with the
1909/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001910bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1911 SourceRange &R1, SourceRange &R2,
1912 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001913 // Don't warn if the expr is type dependent. The type could end up
1914 // instantiating to void.
1915 if (isTypeDependent())
1916 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 switch (getStmtClass()) {
1919 default:
John McCall0faede62010-03-12 07:11:26 +00001920 if (getType()->isVoidType())
1921 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001922 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001923 Loc = getExprLoc();
1924 R1 = getSourceRange();
1925 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001927 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001928 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001929 case GenericSelectionExprClass:
1930 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001931 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001932 case UnaryOperatorClass: {
1933 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001934
Reid Spencer5f016e22007-07-11 17:01:13 +00001935 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001936 case UO_Plus:
1937 case UO_Minus:
1938 case UO_AddrOf:
1939 case UO_Not:
1940 case UO_LNot:
1941 case UO_Deref:
1942 break;
John McCall2de56d12010-08-25 11:45:40 +00001943 case UO_PostInc:
1944 case UO_PostDec:
1945 case UO_PreInc:
1946 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001947 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001948 case UO_Real:
1949 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001951 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1952 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001953 return false;
1954 break;
John McCall2de56d12010-08-25 11:45:40 +00001955 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001956 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001957 }
Eli Friedmana6115062012-05-24 00:47:05 +00001958 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001959 Loc = UO->getOperatorLoc();
1960 R1 = UO->getSubExpr()->getSourceRange();
1961 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001963 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001964 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001965 switch (BO->getOpcode()) {
1966 default:
1967 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001968 // Consider the RHS of comma for side effects. LHS was checked by
1969 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001970 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001971 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1972 // lvalue-ness) of an assignment written in a macro.
1973 if (IntegerLiteral *IE =
1974 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1975 if (IE->getValue() == 0)
1976 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001977 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001978 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001979 case BO_LAnd:
1980 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001981 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1982 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001983 return false;
1984 break;
John McCallbf0ee352010-02-16 04:10:53 +00001985 }
Chris Lattner026dc962009-02-14 07:37:35 +00001986 if (BO->isAssignmentOp())
1987 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001988 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001989 Loc = BO->getOperatorLoc();
1990 R1 = BO->getLHS()->getSourceRange();
1991 R2 = BO->getRHS()->getSourceRange();
1992 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001993 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001994 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001995 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001996 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001997 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001998
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001999 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002000 // If only one of the LHS or RHS is a warning, the operator might
2001 // be being used for control flow. Only warn if both the LHS and
2002 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002003 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002004 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002005 return false;
2006 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00002007 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00002008 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002009 }
2010
Reid Spencer5f016e22007-07-11 17:01:13 +00002011 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002012 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002013 Loc = cast<MemberExpr>(this)->getMemberLoc();
2014 R1 = SourceRange(Loc, Loc);
2015 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2016 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Reid Spencer5f016e22007-07-11 17:01:13 +00002018 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002019 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002020 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2021 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2022 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2023 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00002024
Chandler Carruth9b106832011-08-17 09:49:44 +00002025 case CXXOperatorCallExprClass: {
2026 // We warn about operator== and operator!= even when user-defined operator
2027 // overloads as there is no reasonable way to define these such that they
2028 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2029 // warning: these operators are commonly typo'ed, and so warning on them
2030 // provides additional value as well. If this list is updated,
2031 // DiagnoseUnusedComparison should be as well.
2032 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2033 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002034 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00002035 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002036 Loc = Op->getOperatorLoc();
2037 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00002038 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002039 }
Chandler Carruth9b106832011-08-17 09:49:44 +00002040
2041 // Fallthrough for generic call handling.
2042 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002043 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002044 case CXXMemberCallExprClass:
2045 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002046 // If this is a direct call, get the callee.
2047 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00002048 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00002049 // If the callee has attribute pure, const, or warn_unused_result, warn
2050 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002051 //
2052 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2053 // updated to match for QoI.
2054 if (FD->getAttr<WarnUnusedResultAttr>() ||
2055 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002056 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002057 Loc = CE->getCallee()->getLocStart();
2058 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002059
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002060 if (unsigned NumArgs = CE->getNumArgs())
2061 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2062 CE->getArg(NumArgs-1)->getLocEnd());
2063 return true;
2064 }
Chris Lattner026dc962009-02-14 07:37:35 +00002065 }
2066 return false;
2067 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002068
Matt Beaumont-Gay84c3b972012-10-23 06:15:26 +00002069 // If we don't know precisely what we're looking at, let's not warn.
2070 case UnresolvedLookupExprClass:
2071 case CXXUnresolvedConstructExprClass:
2072 return false;
2073
Anders Carlsson58beed92009-11-17 17:11:23 +00002074 case CXXTemporaryObjectExprClass:
2075 case CXXConstructExprClass:
2076 return false;
2077
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002078 case ObjCMessageExprClass: {
2079 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00002080 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002081 ME->isInstanceMessage() &&
2082 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas4bdb6022013-07-19 20:25:56 +00002083 ME->getMethodFamily() == OMF_init) {
Eli Friedmana6115062012-05-24 00:47:05 +00002084 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00002085 Loc = getExprLoc();
2086 R1 = ME->getSourceRange();
2087 return true;
2088 }
2089
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002090 const ObjCMethodDecl *MD = ME->getMethodDecl();
2091 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002092 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002093 Loc = getExprLoc();
2094 return true;
2095 }
Chris Lattner026dc962009-02-14 07:37:35 +00002096 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002097 }
Mike Stump1eb44332009-09-09 15:08:12 +00002098
John McCall12f78a62010-12-02 01:19:52 +00002099 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002100 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00002101 Loc = getExprLoc();
2102 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00002103 return true;
John McCall12f78a62010-12-02 01:19:52 +00002104
John McCall4b9c2d22011-11-06 09:01:30 +00002105 case PseudoObjectExprClass: {
2106 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2107
2108 // Only complain about things that have the form of a getter.
2109 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2110 isa<BinaryOperator>(PO->getSyntacticForm()))
2111 return false;
2112
Eli Friedmana6115062012-05-24 00:47:05 +00002113 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00002114 Loc = getExprLoc();
2115 R1 = getSourceRange();
2116 return true;
2117 }
2118
Chris Lattner611b2ec2008-07-26 19:51:01 +00002119 case StmtExprClass: {
2120 // Statement exprs don't logically have side effects themselves, but are
2121 // sometimes used in macros in ways that give them a type that is unused.
2122 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2123 // however, if the result of the stmt expr is dead, we don't want to emit a
2124 // warning.
2125 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002126 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002127 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002128 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002129 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2130 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002131 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002132 }
Mike Stump1eb44332009-09-09 15:08:12 +00002133
John McCall0faede62010-03-12 07:11:26 +00002134 if (getType()->isVoidType())
2135 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002136 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002137 Loc = cast<StmtExpr>(this)->getLParenLoc();
2138 R1 = getSourceRange();
2139 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002140 }
Eli Friedman63199172012-09-24 23:02:26 +00002141 case CXXFunctionalCastExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002142 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002143 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002144 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002145 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002146 if (CE->getCastKind() == CK_ToVoid) {
2147 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002148 CE->getSubExpr()->getType().isVolatileQualified()) {
2149 const DeclRefExpr *DRE =
2150 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2151 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2152 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2153 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2154 R1, R2, Ctx);
2155 }
2156 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002157 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002158 }
Eli Friedman4059da82012-05-24 21:05:41 +00002159
Eli Friedmana6115062012-05-24 00:47:05 +00002160 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002161 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002162 if (CE->getCastKind() == CK_ConstructorConversion)
2163 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002164
Eli Friedmana6115062012-05-24 00:47:05 +00002165 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002166 if (const CXXFunctionalCastExpr *CXXCE =
2167 dyn_cast<CXXFunctionalCastExpr>(this)) {
2168 Loc = CXXCE->getTypeBeginLoc();
2169 R1 = CXXCE->getSubExpr()->getSourceRange();
2170 } else {
2171 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2172 Loc = CStyleCE->getLParenLoc();
2173 R1 = CStyleCE->getSubExpr()->getSourceRange();
2174 }
Chris Lattner026dc962009-02-14 07:37:35 +00002175 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002176 }
Eli Friedmana6115062012-05-24 00:47:05 +00002177 case ImplicitCastExprClass: {
2178 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002179
Eli Friedmana6115062012-05-24 00:47:05 +00002180 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2181 if (ICE->getCastKind() == CK_LValueToRValue &&
2182 ICE->getSubExpr()->getType().isVolatileQualified())
2183 return false;
2184
2185 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2186 }
Chris Lattner04421082008-04-08 04:40:51 +00002187 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002188 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002189 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smithc3bf52c2013-04-20 22:23:05 +00002190 case CXXDefaultInitExprClass:
2191 return (cast<CXXDefaultInitExpr>(this)
2192 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002193
2194 case CXXNewExprClass:
2195 // FIXME: In theory, there might be new expressions that don't have side
2196 // effects (e.g. a placement new with an uninitialized POD).
2197 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002198 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002199 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002200 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002201 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002202 case ExprWithCleanupsClass:
2203 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002204 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002205 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002206}
2207
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002208/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002209/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002210bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002211 const Expr *E = IgnoreParens();
2212 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002213 default:
2214 return false;
2215 case ObjCIvarRefExprClass:
2216 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002217 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002218 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002219 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002220 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002221 case MaterializeTemporaryExprClass:
2222 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2223 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002224 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002225 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002226 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002227 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002228
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002229 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2230 if (VD->hasGlobalStorage())
2231 return true;
2232 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002233 // dereferencing to a pointer is always a gc'able candidate,
2234 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002235 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002236 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002237 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002238 return false;
2239 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002240 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002241 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002242 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002243 }
2244 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002245 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002246 }
2247}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002248
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002249bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2250 if (isTypeDependent())
2251 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002252 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002253}
2254
John McCall864c0412011-04-26 20:42:42 +00002255QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002256 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002257
2258 // Bound member expressions are always one of these possibilities:
2259 // x->m x.m x->*y x.*y
2260 // (possibly parenthesized)
2261
2262 expr = expr->IgnoreParens();
2263 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2264 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2265 return mem->getMemberDecl()->getType();
2266 }
2267
2268 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2269 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2270 ->getPointeeType();
2271 assert(type->isFunctionType());
2272 return type;
2273 }
2274
2275 assert(isa<UnresolvedMemberExpr>(expr));
2276 return QualType();
2277}
2278
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002279Expr* Expr::IgnoreParens() {
2280 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002281 while (true) {
2282 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2283 E = P->getSubExpr();
2284 continue;
2285 }
2286 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2287 if (P->getOpcode() == UO_Extension) {
2288 E = P->getSubExpr();
2289 continue;
2290 }
2291 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002292 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2293 if (!P->isResultDependent()) {
2294 E = P->getResultExpr();
2295 continue;
2296 }
2297 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002298 return E;
2299 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002300}
2301
Chris Lattner56f34942008-02-13 01:02:39 +00002302/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2303/// or CastExprs or ImplicitCastExprs, returning their operand.
2304Expr *Expr::IgnoreParenCasts() {
2305 Expr *E = this;
2306 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002307 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002308 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002309 continue;
2310 }
2311 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002312 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002313 continue;
2314 }
2315 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2316 if (P->getOpcode() == UO_Extension) {
2317 E = P->getSubExpr();
2318 continue;
2319 }
2320 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002321 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2322 if (!P->isResultDependent()) {
2323 E = P->getResultExpr();
2324 continue;
2325 }
2326 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002327 if (MaterializeTemporaryExpr *Materialize
2328 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2329 E = Materialize->GetTemporaryExpr();
2330 continue;
2331 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002332 if (SubstNonTypeTemplateParmExpr *NTTP
2333 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2334 E = NTTP->getReplacement();
2335 continue;
2336 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002337 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002338 }
2339}
2340
John McCall9c5d70c2010-12-04 08:24:19 +00002341/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2342/// casts. This is intended purely as a temporary workaround for code
2343/// that hasn't yet been rewritten to do the right thing about those
2344/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002345Expr *Expr::IgnoreParenLValueCasts() {
2346 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002347 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002348 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2349 E = P->getSubExpr();
2350 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002351 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002352 if (P->getCastKind() == CK_LValueToRValue) {
2353 E = P->getSubExpr();
2354 continue;
2355 }
John McCall9c5d70c2010-12-04 08:24:19 +00002356 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2357 if (P->getOpcode() == UO_Extension) {
2358 E = P->getSubExpr();
2359 continue;
2360 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002361 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2362 if (!P->isResultDependent()) {
2363 E = P->getResultExpr();
2364 continue;
2365 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002366 } else if (MaterializeTemporaryExpr *Materialize
2367 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2368 E = Materialize->GetTemporaryExpr();
2369 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002370 } else if (SubstNonTypeTemplateParmExpr *NTTP
2371 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2372 E = NTTP->getReplacement();
2373 continue;
John McCallf6a16482010-12-04 03:47:34 +00002374 }
2375 break;
2376 }
2377 return E;
2378}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002379
2380Expr *Expr::ignoreParenBaseCasts() {
2381 Expr *E = this;
2382 while (true) {
2383 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2384 E = P->getSubExpr();
2385 continue;
2386 }
2387 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2388 if (CE->getCastKind() == CK_DerivedToBase ||
2389 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2390 CE->getCastKind() == CK_NoOp) {
2391 E = CE->getSubExpr();
2392 continue;
2393 }
2394 }
2395
2396 return E;
2397 }
2398}
2399
John McCall2fc46bf2010-05-05 22:59:52 +00002400Expr *Expr::IgnoreParenImpCasts() {
2401 Expr *E = this;
2402 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002403 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002404 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002405 continue;
2406 }
2407 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002408 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002409 continue;
2410 }
2411 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2412 if (P->getOpcode() == UO_Extension) {
2413 E = P->getSubExpr();
2414 continue;
2415 }
2416 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002417 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2418 if (!P->isResultDependent()) {
2419 E = P->getResultExpr();
2420 continue;
2421 }
2422 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002423 if (MaterializeTemporaryExpr *Materialize
2424 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2425 E = Materialize->GetTemporaryExpr();
2426 continue;
2427 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002428 if (SubstNonTypeTemplateParmExpr *NTTP
2429 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2430 E = NTTP->getReplacement();
2431 continue;
2432 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002433 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002434 }
2435}
2436
Hans Wennborg2f072b42011-06-09 17:06:51 +00002437Expr *Expr::IgnoreConversionOperator() {
2438 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002439 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002440 return MCE->getImplicitObjectArgument();
2441 }
2442 return this;
2443}
2444
Chris Lattnerecdd8412009-03-13 17:28:01 +00002445/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2446/// value (including ptr->int casts of the same size). Strip off any
2447/// ParenExpr or CastExprs, returning their operand.
2448Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2449 Expr *E = this;
2450 while (true) {
2451 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2452 E = P->getSubExpr();
2453 continue;
2454 }
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Chris Lattnerecdd8412009-03-13 17:28:01 +00002456 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2457 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002458 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002459 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002460
Chris Lattnerecdd8412009-03-13 17:28:01 +00002461 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2462 E = SE;
2463 continue;
2464 }
Mike Stump1eb44332009-09-09 15:08:12 +00002465
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002466 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002467 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002468 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002469 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002470 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2471 E = SE;
2472 continue;
2473 }
2474 }
Mike Stump1eb44332009-09-09 15:08:12 +00002475
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002476 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2477 if (P->getOpcode() == UO_Extension) {
2478 E = P->getSubExpr();
2479 continue;
2480 }
2481 }
2482
Peter Collingbournef111d932011-04-15 00:35:48 +00002483 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2484 if (!P->isResultDependent()) {
2485 E = P->getResultExpr();
2486 continue;
2487 }
2488 }
2489
Douglas Gregorc0244c52011-09-08 17:56:33 +00002490 if (SubstNonTypeTemplateParmExpr *NTTP
2491 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2492 E = NTTP->getReplacement();
2493 continue;
2494 }
2495
Chris Lattnerecdd8412009-03-13 17:28:01 +00002496 return E;
2497 }
2498}
2499
Douglas Gregor6eef5192009-12-14 19:27:10 +00002500bool Expr::isDefaultArgument() const {
2501 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002502 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2503 E = M->GetTemporaryExpr();
2504
Douglas Gregor6eef5192009-12-14 19:27:10 +00002505 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2506 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002507
Douglas Gregor6eef5192009-12-14 19:27:10 +00002508 return isa<CXXDefaultArgExpr>(E);
2509}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002510
Douglas Gregor2f599792010-04-02 18:24:57 +00002511/// \brief Skip over any no-op casts and any temporary-binding
2512/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002513static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002514 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2515 E = M->GetTemporaryExpr();
2516
Douglas Gregor2f599792010-04-02 18:24:57 +00002517 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002518 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002519 E = ICE->getSubExpr();
2520 else
2521 break;
2522 }
2523
2524 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2525 E = BE->getSubExpr();
2526
2527 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002528 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002529 E = ICE->getSubExpr();
2530 else
2531 break;
2532 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002533
2534 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002535}
2536
John McCall558d2ab2010-09-15 10:14:12 +00002537/// isTemporaryObject - Determines if this expression produces a
2538/// temporary of the given class type.
2539bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2540 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2541 return false;
2542
Anders Carlssonf8b30152010-11-28 16:40:49 +00002543 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002544
John McCall58277b52010-09-15 20:59:13 +00002545 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002546 if (!E->Classify(C).isPRValue()) {
2547 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002548 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002549 return false;
2550 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002551
John McCall19e60ad2010-09-16 06:57:56 +00002552 // Black-list a few cases which yield pr-values of class type that don't
2553 // refer to temporaries of that type:
2554
2555 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002556 if (isa<ImplicitCastExpr>(E)) {
2557 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2558 case CK_DerivedToBase:
2559 case CK_UncheckedDerivedToBase:
2560 return false;
2561 default:
2562 break;
2563 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002564 }
2565
John McCall19e60ad2010-09-16 06:57:56 +00002566 // - member expressions (all)
2567 if (isa<MemberExpr>(E))
2568 return false;
2569
Eli Friedman32f498a2012-06-15 23:51:06 +00002570 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2571 if (BO->isPtrMemOp())
2572 return false;
2573
John McCall56ca35d2011-02-17 10:25:35 +00002574 // - opaque values (all)
2575 if (isa<OpaqueValueExpr>(E))
2576 return false;
2577
John McCall558d2ab2010-09-15 10:14:12 +00002578 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002579}
2580
Douglas Gregor75e85042011-03-02 21:06:53 +00002581bool Expr::isImplicitCXXThis() const {
2582 const Expr *E = this;
2583
2584 // Strip away parentheses and casts we don't care about.
2585 while (true) {
2586 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2587 E = Paren->getSubExpr();
2588 continue;
2589 }
2590
2591 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2592 if (ICE->getCastKind() == CK_NoOp ||
2593 ICE->getCastKind() == CK_LValueToRValue ||
2594 ICE->getCastKind() == CK_DerivedToBase ||
2595 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2596 E = ICE->getSubExpr();
2597 continue;
2598 }
2599 }
2600
2601 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2602 if (UnOp->getOpcode() == UO_Extension) {
2603 E = UnOp->getSubExpr();
2604 continue;
2605 }
2606 }
2607
Douglas Gregor03e80032011-06-21 17:03:29 +00002608 if (const MaterializeTemporaryExpr *M
2609 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2610 E = M->GetTemporaryExpr();
2611 continue;
2612 }
2613
Douglas Gregor75e85042011-03-02 21:06:53 +00002614 break;
2615 }
2616
2617 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2618 return This->isImplicit();
2619
2620 return false;
2621}
2622
Douglas Gregor898574e2008-12-05 23:32:09 +00002623/// hasAnyTypeDependentArguments - Determines if any of the expressions
2624/// in Exprs is type-dependent.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002625bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00002626 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002627 if (Exprs[I]->isTypeDependent())
2628 return true;
2629
2630 return false;
2631}
2632
John McCall4204f072010-08-02 21:13:48 +00002633bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002634 // This function is attempting whether an expression is an initializer
Eli Friedman21cde052013-07-16 22:40:53 +00002635 // which can be evaluated at compile-time. It very closely parallels
2636 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2637 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2638 // to isEvaluatable most of the time.
2639 //
John McCall4204f072010-08-02 21:13:48 +00002640 // If we ever capture reference-binding directly in the AST, we can
2641 // kill the second parameter.
2642
2643 if (IsForRef) {
2644 EvalResult Result;
2645 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2646 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002647
Anders Carlssone8a32b82008-11-24 05:23:59 +00002648 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002649 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002650 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002651 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002652 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002653 case CXXTemporaryObjectExprClass:
2654 case CXXConstructExprClass: {
2655 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002656
Eli Friedman21cde052013-07-16 22:40:53 +00002657 if (CE->getConstructor()->isTrivial() &&
2658 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2659 // Trivial default constructor
Richard Smith180f4792011-11-10 06:34:14 +00002660 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002661
Eli Friedman21cde052013-07-16 22:40:53 +00002662 // Trivial copy constructor
2663 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2664 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smith180f4792011-11-10 06:34:14 +00002665 }
2666
Richard Smith180f4792011-11-10 06:34:14 +00002667 break;
John McCallb4b9b152010-08-01 21:51:45 +00002668 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002669 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002670 // This handles gcc's extension that allows global initializers like
2671 // "struct x {int x;} x = (struct x) {};".
2672 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002673 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002674 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002675 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002676 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002677 // FIXME: This doesn't deal with fields with reference types correctly.
2678 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2679 // to bitfields.
Eli Friedman21cde052013-07-16 22:40:53 +00002680 const InitListExpr *ILE = cast<InitListExpr>(this);
2681 if (ILE->getType()->isArrayType()) {
2682 unsigned numInits = ILE->getNumInits();
2683 for (unsigned i = 0; i < numInits; i++) {
2684 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2685 return false;
2686 }
2687 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002688 }
Eli Friedman21cde052013-07-16 22:40:53 +00002689
2690 if (ILE->getType()->isRecordType()) {
2691 unsigned ElementNo = 0;
2692 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2693 for (RecordDecl::field_iterator Field = RD->field_begin(),
2694 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2695 // If this is a union, skip all the fields that aren't being initialized.
2696 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2697 continue;
2698
2699 // Don't emit anonymous bitfields, they just affect layout.
2700 if (Field->isUnnamedBitfield())
2701 continue;
2702
2703 if (ElementNo < ILE->getNumInits()) {
2704 const Expr *Elt = ILE->getInit(ElementNo++);
2705 if (Field->isBitField()) {
2706 // Bitfields have to evaluate to an integer.
2707 llvm::APSInt ResultTmp;
2708 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2709 return false;
2710 } else {
2711 bool RefType = Field->getType()->isReferenceType();
2712 if (!Elt->isConstantInitializer(Ctx, RefType))
2713 return false;
2714 }
2715 }
2716 }
2717 return true;
2718 }
2719
2720 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002721 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002722 case ImplicitValueInitExprClass:
2723 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002724 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002725 return cast<ParenExpr>(this)->getSubExpr()
2726 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002727 case GenericSelectionExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002728 return cast<GenericSelectionExpr>(this)->getResultExpr()
2729 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002730 case ChooseExprClass:
2731 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2732 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002733 case UnaryOperatorClass: {
2734 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002735 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002736 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002737 break;
2738 }
John McCall4204f072010-08-02 21:13:48 +00002739 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002740 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002741 case ImplicitCastExprClass:
Eli Friedman21cde052013-07-16 22:40:53 +00002742 case CStyleCastExprClass:
2743 case ObjCBridgedCastExprClass:
2744 case CXXDynamicCastExprClass:
2745 case CXXReinterpretCastExprClass:
2746 case CXXConstCastExprClass: {
Richard Smithd62ca372011-12-06 22:44:34 +00002747 const CastExpr *CE = cast<CastExpr>(this);
2748
Eli Friedman6bd97192011-12-21 00:43:02 +00002749 // Handle misc casts we want to ignore.
Eli Friedman6bd97192011-12-21 00:43:02 +00002750 if (CE->getCastKind() == CK_NoOp ||
2751 CE->getCastKind() == CK_LValueToRValue ||
2752 CE->getCastKind() == CK_ToUnion ||
Eli Friedman21cde052013-07-16 22:40:53 +00002753 CE->getCastKind() == CK_ConstructorConversion ||
2754 CE->getCastKind() == CK_NonAtomicToAtomic ||
2755 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smithd62ca372011-12-06 22:44:34 +00002756 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2757
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002758 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002759 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002760 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002761 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002762 ->isConstantInitializer(Ctx, false);
Eli Friedman21cde052013-07-16 22:40:53 +00002763
2764 case SubstNonTypeTemplateParmExprClass:
2765 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2766 ->isConstantInitializer(Ctx, false);
2767 case CXXDefaultArgExprClass:
2768 return cast<CXXDefaultArgExpr>(this)->getExpr()
2769 ->isConstantInitializer(Ctx, false);
2770 case CXXDefaultInitExprClass:
2771 return cast<CXXDefaultInitExpr>(this)->getExpr()
2772 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002773 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002774 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002775}
2776
Richard Smith8ae4ec22012-08-07 04:16:51 +00002777bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2778 if (isInstantiationDependent())
2779 return true;
2780
2781 switch (getStmtClass()) {
2782 case NoStmtClass:
2783 #define ABSTRACT_STMT(Type)
2784 #define STMT(Type, Base) case Type##Class:
2785 #define EXPR(Type, Base)
2786 #include "clang/AST/StmtNodes.inc"
2787 llvm_unreachable("unexpected Expr kind");
2788
2789 case DependentScopeDeclRefExprClass:
2790 case CXXUnresolvedConstructExprClass:
2791 case CXXDependentScopeMemberExprClass:
2792 case UnresolvedLookupExprClass:
2793 case UnresolvedMemberExprClass:
2794 case PackExpansionExprClass:
2795 case SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +00002796 case FunctionParmPackExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002797 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2798
Richard Smith60b70382012-08-07 05:18:29 +00002799 case DeclRefExprClass:
2800 case ObjCIvarRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002801 case PredefinedExprClass:
2802 case IntegerLiteralClass:
2803 case FloatingLiteralClass:
2804 case ImaginaryLiteralClass:
2805 case StringLiteralClass:
2806 case CharacterLiteralClass:
2807 case OffsetOfExprClass:
2808 case ImplicitValueInitExprClass:
2809 case UnaryExprOrTypeTraitExprClass:
2810 case AddrLabelExprClass:
2811 case GNUNullExprClass:
2812 case CXXBoolLiteralExprClass:
2813 case CXXNullPtrLiteralExprClass:
2814 case CXXThisExprClass:
2815 case CXXScalarValueInitExprClass:
2816 case TypeTraitExprClass:
2817 case UnaryTypeTraitExprClass:
2818 case BinaryTypeTraitExprClass:
2819 case ArrayTypeTraitExprClass:
2820 case ExpressionTraitExprClass:
2821 case CXXNoexceptExprClass:
2822 case SizeOfPackExprClass:
2823 case ObjCStringLiteralClass:
2824 case ObjCEncodeExprClass:
2825 case ObjCBoolLiteralExprClass:
2826 case CXXUuidofExprClass:
2827 case OpaqueValueExprClass:
2828 // These never have a side-effect.
2829 return false;
2830
2831 case CallExprClass:
John McCall76da55d2013-04-16 07:28:30 +00002832 case MSPropertyRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002833 case CompoundAssignOperatorClass:
2834 case VAArgExprClass:
2835 case AtomicExprClass:
2836 case StmtExprClass:
2837 case CXXOperatorCallExprClass:
2838 case CXXMemberCallExprClass:
2839 case UserDefinedLiteralClass:
2840 case CXXThrowExprClass:
2841 case CXXNewExprClass:
2842 case CXXDeleteExprClass:
2843 case ExprWithCleanupsClass:
2844 case CXXBindTemporaryExprClass:
2845 case BlockExprClass:
2846 case CUDAKernelCallExprClass:
2847 // These always have a side-effect.
2848 return true;
2849
2850 case ParenExprClass:
2851 case ArraySubscriptExprClass:
2852 case MemberExprClass:
2853 case ConditionalOperatorClass:
2854 case BinaryConditionalOperatorClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002855 case CompoundLiteralExprClass:
2856 case ExtVectorElementExprClass:
2857 case DesignatedInitExprClass:
2858 case ParenListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002859 case CXXPseudoDestructorExprClass:
Richard Smith7c3e6152013-06-12 22:31:48 +00002860 case CXXStdInitializerListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002861 case SubstNonTypeTemplateParmExprClass:
2862 case MaterializeTemporaryExprClass:
2863 case ShuffleVectorExprClass:
2864 case AsTypeExprClass:
2865 // These have a side-effect if any subexpression does.
2866 break;
2867
Richard Smith60b70382012-08-07 05:18:29 +00002868 case UnaryOperatorClass:
2869 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002870 return true;
2871 break;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002872
2873 case BinaryOperatorClass:
2874 if (cast<BinaryOperator>(this)->isAssignmentOp())
2875 return true;
2876 break;
2877
Richard Smith8ae4ec22012-08-07 04:16:51 +00002878 case InitListExprClass:
2879 // FIXME: The children for an InitListExpr doesn't include the array filler.
2880 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2881 if (E->HasSideEffects(Ctx))
2882 return true;
2883 break;
2884
2885 case GenericSelectionExprClass:
2886 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2887 HasSideEffects(Ctx);
2888
2889 case ChooseExprClass:
2890 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->HasSideEffects(Ctx);
2891
2892 case CXXDefaultArgExprClass:
2893 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2894
Richard Smithc3bf52c2013-04-20 22:23:05 +00002895 case CXXDefaultInitExprClass:
2896 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2897 return E->HasSideEffects(Ctx);
2898 // If we've not yet parsed the initializer, assume it has side-effects.
2899 return true;
2900
Richard Smith8ae4ec22012-08-07 04:16:51 +00002901 case CXXDynamicCastExprClass: {
2902 // A dynamic_cast expression has side-effects if it can throw.
2903 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2904 if (DCE->getTypeAsWritten()->isReferenceType() &&
2905 DCE->getCastKind() == CK_Dynamic)
2906 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002907 } // Fall through.
2908 case ImplicitCastExprClass:
2909 case CStyleCastExprClass:
2910 case CXXStaticCastExprClass:
2911 case CXXReinterpretCastExprClass:
2912 case CXXConstCastExprClass:
2913 case CXXFunctionalCastExprClass: {
2914 const CastExpr *CE = cast<CastExpr>(this);
2915 if (CE->getCastKind() == CK_LValueToRValue &&
2916 CE->getSubExpr()->getType().isVolatileQualified())
2917 return true;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002918 break;
2919 }
2920
Richard Smith0d729102012-08-13 20:08:14 +00002921 case CXXTypeidExprClass:
2922 // typeid might throw if its subexpression is potentially-evaluated, so has
2923 // side-effects in that case whether or not its subexpression does.
2924 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith8ae4ec22012-08-07 04:16:51 +00002925
2926 case CXXConstructExprClass:
2927 case CXXTemporaryObjectExprClass: {
2928 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smith60b70382012-08-07 05:18:29 +00002929 if (!CE->getConstructor()->isTrivial())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002930 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002931 // A trivial constructor does not add any side-effects of its own. Just look
2932 // at its arguments.
Richard Smith8ae4ec22012-08-07 04:16:51 +00002933 break;
2934 }
2935
2936 case LambdaExprClass: {
2937 const LambdaExpr *LE = cast<LambdaExpr>(this);
2938 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2939 E = LE->capture_end(); I != E; ++I)
2940 if (I->getCaptureKind() == LCK_ByCopy)
2941 // FIXME: Only has a side-effect if the variable is volatile or if
2942 // the copy would invoke a non-trivial copy constructor.
2943 return true;
2944 return false;
2945 }
2946
2947 case PseudoObjectExprClass: {
2948 // Only look for side-effects in the semantic form, and look past
2949 // OpaqueValueExpr bindings in that form.
2950 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2951 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2952 E = PO->semantics_end();
2953 I != E; ++I) {
2954 const Expr *Subexpr = *I;
2955 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2956 Subexpr = OVE->getSourceExpr();
2957 if (Subexpr->HasSideEffects(Ctx))
2958 return true;
2959 }
2960 return false;
2961 }
2962
2963 case ObjCBoxedExprClass:
2964 case ObjCArrayLiteralClass:
2965 case ObjCDictionaryLiteralClass:
2966 case ObjCMessageExprClass:
2967 case ObjCSelectorExprClass:
2968 case ObjCProtocolExprClass:
2969 case ObjCPropertyRefExprClass:
2970 case ObjCIsaExprClass:
2971 case ObjCIndirectCopyRestoreExprClass:
2972 case ObjCSubscriptRefExprClass:
2973 case ObjCBridgedCastExprClass:
2974 // FIXME: Classify these cases better.
2975 return true;
2976 }
2977
2978 // Recurse to children.
2979 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2980 if (const Stmt *S = *SubStmts)
2981 if (cast<Expr>(S)->HasSideEffects(Ctx))
2982 return true;
2983
2984 return false;
2985}
2986
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002987namespace {
2988 /// \brief Look for a call to a non-trivial function within an expression.
2989 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2990 {
2991 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2992
2993 bool NonTrivial;
2994
2995 public:
2996 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002997 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002998
2999 bool hasNonTrivialCall() const { return NonTrivial; }
3000
3001 void VisitCallExpr(CallExpr *E) {
3002 if (CXXMethodDecl *Method
3003 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3004 if (Method->isTrivial()) {
3005 // Recurse to children of the call.
3006 Inherited::VisitStmt(E);
3007 return;
3008 }
3009 }
3010
3011 NonTrivial = true;
3012 }
3013
3014 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3015 if (E->getConstructor()->isTrivial()) {
3016 // Recurse to children of the call.
3017 Inherited::VisitStmt(E);
3018 return;
3019 }
3020
3021 NonTrivial = true;
3022 }
3023
3024 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3025 if (E->getTemporary()->getDestructor()->isTrivial()) {
3026 Inherited::VisitStmt(E);
3027 return;
3028 }
3029
3030 NonTrivial = true;
3031 }
3032 };
3033}
3034
3035bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3036 NonTrivialCallFinder Finder(Ctx);
3037 Finder.Visit(this);
3038 return Finder.hasNonTrivialCall();
3039}
3040
Chandler Carruth82214a82011-02-18 23:54:50 +00003041/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3042/// pointer constant or not, as well as the specific kind of constant detected.
3043/// Null pointer constants can be integer constant expressions with the
3044/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3045/// (a GNU extension).
3046Expr::NullPointerConstantKind
3047Expr::isNullPointerConstant(ASTContext &Ctx,
3048 NullPointerConstantValueDependence NPC) const {
Richard Smithf050d242013-06-13 02:46:14 +00003049 if (isValueDependent() && !Ctx.getLangOpts().CPlusPlus11) {
Douglas Gregorce940492009-09-25 04:25:58 +00003050 switch (NPC) {
3051 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00003052 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00003053 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003054 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie50800fc2012-08-08 17:33:31 +00003055 return NPCK_ZeroExpression;
Chandler Carruth82214a82011-02-18 23:54:50 +00003056 else
3057 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00003058
Douglas Gregorce940492009-09-25 04:25:58 +00003059 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003060 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00003061 }
3062 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00003063
Sebastian Redl07779722008-10-31 14:43:28 +00003064 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00003065 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003066 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00003067 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00003068 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00003069 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00003070 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00003071 Pointee->isVoidType() && // to void*
3072 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00003073 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00003074 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003075 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003076 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3077 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00003078 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00003079 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3080 // Accept ((void*)0) as a null pointer constant, as many other
3081 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00003082 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00003083 } else if (const GenericSelectionExpr *GE =
3084 dyn_cast<GenericSelectionExpr>(this)) {
3085 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00003086 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00003087 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smithc3bf52c2013-04-20 22:23:05 +00003088 // See through default argument expressions.
Douglas Gregorce940492009-09-25 04:25:58 +00003089 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smithc3bf52c2013-04-20 22:23:05 +00003090 } else if (const CXXDefaultInitExpr *DefaultInit
3091 = dyn_cast<CXXDefaultInitExpr>(this)) {
3092 // See through default initializer expressions.
3093 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003094 } else if (isa<GNUNullExpr>(this)) {
3095 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00003096 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00003097 } else if (const MaterializeTemporaryExpr *M
3098 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3099 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00003100 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3101 if (const Expr *Source = OVE->getSourceExpr())
3102 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00003103 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003104
Richard Smith4e24f0f2013-01-02 12:01:23 +00003105 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003106 if (getType()->isNullPtrType())
Richard Smith4e24f0f2013-01-02 12:01:23 +00003107 return NPCK_CXX11_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003108
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003109 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smithf050d242013-06-13 02:46:14 +00003110 if (!Ctx.getLangOpts().CPlusPlus11 &&
3111 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003112 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3113 const Expr *InitExpr = CLE->getInitializer();
3114 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3115 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3116 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003117 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00003118 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003119 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00003120 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Richard Smith80ad52f2013-01-02 11:42:31 +00003122 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smithf050d242013-06-13 02:46:14 +00003123 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3124 // value zero or a prvalue of type std::nullptr_t.
3125 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
3126 return (Lit && !Lit->getValue()) ? NPCK_ZeroLiteral : NPCK_NotNull;
Richard Smith70488e22012-02-14 21:38:30 +00003127 } else {
Richard Smithf050d242013-06-13 02:46:14 +00003128 // If we have an integer constant expression, we need to *evaluate* it and
3129 // test for the value 0.
Richard Smith70488e22012-02-14 21:38:30 +00003130 if (!isIntegerConstantExpr(Ctx))
3131 return NPCK_NotNull;
3132 }
Chandler Carruth82214a82011-02-18 23:54:50 +00003133
David Blaikie50800fc2012-08-08 17:33:31 +00003134 if (EvaluateKnownConstInt(Ctx) != 0)
3135 return NPCK_NotNull;
3136
3137 if (isa<IntegerLiteral>(this))
3138 return NPCK_ZeroLiteral;
3139 return NPCK_ZeroExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00003140}
Steve Naroff31a45842007-07-28 23:10:27 +00003141
John McCallf6a16482010-12-04 03:47:34 +00003142/// \brief If this expression is an l-value for an Objective C
3143/// property, find the underlying property reference expression.
3144const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3145 const Expr *E = this;
3146 while (true) {
3147 assert((E->getValueKind() == VK_LValue &&
3148 E->getObjectKind() == OK_ObjCProperty) &&
3149 "expression is not a property reference");
3150 E = E->IgnoreParenCasts();
3151 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3152 if (BO->getOpcode() == BO_Comma) {
3153 E = BO->getRHS();
3154 continue;
3155 }
3156 }
3157
3158 break;
3159 }
3160
3161 return cast<ObjCPropertyRefExpr>(E);
3162}
3163
Anna Zaksbbff82f2012-10-01 20:34:04 +00003164bool Expr::isObjCSelfExpr() const {
3165 const Expr *E = IgnoreParenImpCasts();
3166
3167 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3168 if (!DRE)
3169 return false;
3170
3171 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3172 if (!Param)
3173 return false;
3174
3175 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3176 if (!M)
3177 return false;
3178
3179 return M->getSelfDecl() == Param;
3180}
3181
John McCall993f43f2013-05-06 21:39:12 +00003182FieldDecl *Expr::getSourceBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00003183 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003184
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003185 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00003186 if (ICE->getCastKind() == CK_LValueToRValue ||
3187 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003188 E = ICE->getSubExpr()->IgnoreParens();
3189 else
3190 break;
3191 }
3192
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003193 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00003194 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003195 if (Field->isBitField())
3196 return Field;
3197
John McCall993f43f2013-05-06 21:39:12 +00003198 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3199 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3200 if (Ivar->isBitField())
3201 return Ivar;
3202
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00003203 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3204 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3205 if (Field->isBitField())
3206 return Field;
3207
Eli Friedman42068e92011-07-13 02:05:57 +00003208 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003209 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCall993f43f2013-05-06 21:39:12 +00003210 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003211
Eli Friedman42068e92011-07-13 02:05:57 +00003212 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCall993f43f2013-05-06 21:39:12 +00003213 return BinOp->getRHS()->getSourceBitField();
Eli Friedman42068e92011-07-13 02:05:57 +00003214 }
3215
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003216 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003217}
3218
Anders Carlsson09380262010-01-31 17:18:49 +00003219bool Expr::refersToVectorElement() const {
3220 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00003221
Anders Carlsson09380262010-01-31 17:18:49 +00003222 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00003223 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00003224 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00003225 E = ICE->getSubExpr()->IgnoreParens();
3226 else
3227 break;
3228 }
Sean Huntc3021132010-05-05 15:23:54 +00003229
Anders Carlsson09380262010-01-31 17:18:49 +00003230 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3231 return ASE->getBase()->getType()->isVectorType();
3232
3233 if (isa<ExtVectorElementExpr>(E))
3234 return true;
3235
3236 return false;
3237}
3238
Chris Lattner2140e902009-02-16 22:14:05 +00003239/// isArrow - Return true if the base expression is a pointer to vector,
3240/// return false if the base expression is a vector.
3241bool ExtVectorElementExpr::isArrow() const {
3242 return getBase()->getType()->isPointerType();
3243}
3244
Nate Begeman213541a2008-04-18 23:10:10 +00003245unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003246 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003247 return VT->getNumElements();
3248 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003249}
3250
Nate Begeman8a997642008-05-09 06:41:27 +00003251/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003252bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003253 // FIXME: Refactor this code to an accessor on the AST node which returns the
3254 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003255 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003256
3257 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003258 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003259 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003260
Nate Begeman190d6a22009-01-18 02:01:21 +00003261 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003262 if (Comp[0] == 's' || Comp[0] == 'S')
3263 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003264
Daniel Dunbar15027422009-10-17 23:53:04 +00003265 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003266 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003267 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003268
Steve Narofffec0b492007-07-30 03:29:09 +00003269 return false;
3270}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003271
Nate Begeman8a997642008-05-09 06:41:27 +00003272/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003273void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003274 SmallVectorImpl<unsigned> &Elts) const {
3275 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003276 if (Comp[0] == 's' || Comp[0] == 'S')
3277 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003278
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003279 bool isHi = Comp == "hi";
3280 bool isLo = Comp == "lo";
3281 bool isEven = Comp == "even";
3282 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003283
Nate Begeman8a997642008-05-09 06:41:27 +00003284 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3285 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003286
Nate Begeman8a997642008-05-09 06:41:27 +00003287 if (isHi)
3288 Index = e + i;
3289 else if (isLo)
3290 Index = i;
3291 else if (isEven)
3292 Index = 2 * i;
3293 else if (isOdd)
3294 Index = 2 * i + 1;
3295 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003296 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003297
Nate Begeman3b8d1162008-05-13 21:03:02 +00003298 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003299 }
Nate Begeman8a997642008-05-09 06:41:27 +00003300}
3301
Douglas Gregor04badcf2010-04-21 00:45:42 +00003302ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003303 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003304 SourceLocation LBracLoc,
3305 SourceLocation SuperLoc,
3306 bool IsInstanceSuper,
3307 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003308 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003309 ArrayRef<SourceLocation> SelLocs,
3310 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003311 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003312 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003313 SourceLocation RBracLoc,
3314 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003315 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003316 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003317 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003318 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003319 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3320 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003321 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003322 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3323 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003324{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003325 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003326 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003327}
3328
Douglas Gregor04badcf2010-04-21 00:45:42 +00003329ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003330 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003331 SourceLocation LBracLoc,
3332 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003333 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003334 ArrayRef<SourceLocation> SelLocs,
3335 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003336 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003337 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003338 SourceLocation RBracLoc,
3339 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003340 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003341 T->isDependentType(), T->isInstantiationDependentType(),
3342 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003343 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3344 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003345 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003346 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003347 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003348{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003349 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003350 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003351}
3352
Douglas Gregor04badcf2010-04-21 00:45:42 +00003353ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003354 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003355 SourceLocation LBracLoc,
3356 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003357 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003358 ArrayRef<SourceLocation> SelLocs,
3359 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003360 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003361 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003362 SourceLocation RBracLoc,
3363 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003364 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003365 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003366 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003367 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003368 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3369 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003370 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003371 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003372 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003373{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003374 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003375 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003376}
3377
3378void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3379 ArrayRef<SourceLocation> SelLocs,
3380 SelectorLocationsKind SelLocsK) {
3381 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003382 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003383 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003384 if (Args[I]->isTypeDependent())
3385 ExprBits.TypeDependent = true;
3386 if (Args[I]->isValueDependent())
3387 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003388 if (Args[I]->isInstantiationDependent())
3389 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003390 if (Args[I]->containsUnexpandedParameterPack())
3391 ExprBits.ContainsUnexpandedParameterPack = true;
3392
3393 MyArgs[I] = Args[I];
3394 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003395
Benjamin Kramer19562c92012-02-20 00:20:48 +00003396 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003397 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003398 if (SelLocsK == SelLoc_NonStandard)
3399 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3400 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003401}
3402
Douglas Gregor04badcf2010-04-21 00:45:42 +00003403ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003404 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003405 SourceLocation LBracLoc,
3406 SourceLocation SuperLoc,
3407 bool IsInstanceSuper,
3408 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003409 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003410 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003411 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003412 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003413 SourceLocation RBracLoc,
3414 bool isImplicit) {
3415 assert((!SelLocs.empty() || isImplicit) &&
3416 "No selector locs for non-implicit message");
3417 ObjCMessageExpr *Mem;
3418 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3419 if (isImplicit)
3420 Mem = alloc(Context, Args.size(), 0);
3421 else
3422 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003423 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003424 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003425 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003426}
3427
3428ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003429 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003430 SourceLocation LBracLoc,
3431 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003432 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003433 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003434 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003435 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003436 SourceLocation RBracLoc,
3437 bool isImplicit) {
3438 assert((!SelLocs.empty() || isImplicit) &&
3439 "No selector locs for non-implicit message");
3440 ObjCMessageExpr *Mem;
3441 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3442 if (isImplicit)
3443 Mem = alloc(Context, Args.size(), 0);
3444 else
3445 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003446 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003447 SelLocs, SelLocsK, Method, Args, RBracLoc,
3448 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003449}
3450
3451ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003452 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003453 SourceLocation LBracLoc,
3454 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003455 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003456 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003457 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003458 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003459 SourceLocation RBracLoc,
3460 bool isImplicit) {
3461 assert((!SelLocs.empty() || isImplicit) &&
3462 "No selector locs for non-implicit message");
3463 ObjCMessageExpr *Mem;
3464 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3465 if (isImplicit)
3466 Mem = alloc(Context, Args.size(), 0);
3467 else
3468 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003469 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003470 SelLocs, SelLocsK, Method, Args, RBracLoc,
3471 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003472}
3473
Sean Huntc3021132010-05-05 15:23:54 +00003474ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003475 unsigned NumArgs,
3476 unsigned NumStoredSelLocs) {
3477 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003478 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3479}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003480
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003481ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3482 ArrayRef<Expr *> Args,
3483 SourceLocation RBraceLoc,
3484 ArrayRef<SourceLocation> SelLocs,
3485 Selector Sel,
3486 SelectorLocationsKind &SelLocsK) {
3487 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3488 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3489 : 0;
3490 return alloc(C, Args.size(), NumStoredSelLocs);
3491}
3492
3493ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3494 unsigned NumArgs,
3495 unsigned NumStoredSelLocs) {
3496 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3497 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3498 return (ObjCMessageExpr *)C.Allocate(Size,
3499 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3500}
3501
3502void ObjCMessageExpr::getSelectorLocs(
3503 SmallVectorImpl<SourceLocation> &SelLocs) const {
3504 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3505 SelLocs.push_back(getSelectorLoc(i));
3506}
3507
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003508SourceRange ObjCMessageExpr::getReceiverRange() const {
3509 switch (getReceiverKind()) {
3510 case Instance:
3511 return getInstanceReceiver()->getSourceRange();
3512
3513 case Class:
3514 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3515
3516 case SuperInstance:
3517 case SuperClass:
3518 return getSuperLoc();
3519 }
3520
David Blaikie30263482012-01-20 21:50:17 +00003521 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003522}
3523
Douglas Gregor04badcf2010-04-21 00:45:42 +00003524Selector ObjCMessageExpr::getSelector() const {
3525 if (HasMethod)
3526 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3527 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003528 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003529}
3530
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003531QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor04badcf2010-04-21 00:45:42 +00003532 switch (getReceiverKind()) {
3533 case Instance:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003534 return getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003535 case Class:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003536 return getClassReceiver();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003537 case SuperInstance:
Douglas Gregor04badcf2010-04-21 00:45:42 +00003538 case SuperClass:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003539 return getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003540 }
3541
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003542 llvm_unreachable("unexpected receiver kind");
3543}
3544
3545ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3546 QualType T = getReceiverType();
3547
3548 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3549 return Ptr->getInterfaceDecl();
3550
3551 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3552 return Ty->getInterface();
3553
Douglas Gregor04badcf2010-04-21 00:45:42 +00003554 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003555}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003556
Chris Lattner5f9e2722011-07-23 10:55:15 +00003557StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003558 switch (getBridgeKind()) {
3559 case OBC_Bridge:
3560 return "__bridge";
3561 case OBC_BridgeTransfer:
3562 return "__bridge_transfer";
3563 case OBC_BridgeRetained:
3564 return "__bridge_retained";
3565 }
David Blaikie30263482012-01-20 21:50:17 +00003566
3567 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003568}
3569
Jay Foad4ba2a172011-01-12 09:06:06 +00003570bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003571 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003572}
3573
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003574ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003575 QualType Type, SourceLocation BLoc,
3576 SourceLocation RP)
3577 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3578 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003579 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003580 Type->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003581 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003582{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003583 SubExprs = new (C) Stmt*[args.size()];
3584 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003585 if (args[i]->isTypeDependent())
3586 ExprBits.TypeDependent = true;
3587 if (args[i]->isValueDependent())
3588 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003589 if (args[i]->isInstantiationDependent())
3590 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003591 if (args[i]->containsUnexpandedParameterPack())
3592 ExprBits.ContainsUnexpandedParameterPack = true;
3593
3594 SubExprs[i] = args[i];
3595 }
3596}
3597
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003598void ShuffleVectorExpr::setExprs(ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman888376a2009-08-12 02:28:50 +00003599 if (SubExprs) C.Deallocate(SubExprs);
3600
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003601 this->NumExprs = Exprs.size();
Dmitri Gribenko2ad77cd2013-05-10 17:30:13 +00003602 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003603 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00003604}
Nate Begeman888376a2009-08-12 02:28:50 +00003605
Peter Collingbournef111d932011-04-15 00:35:48 +00003606GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3607 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 unsigned ResultIndex)
3614 : Expr(GenericSelectionExprClass,
3615 AssocExprs[ResultIndex]->getType(),
3616 AssocExprs[ResultIndex]->getValueKind(),
3617 AssocExprs[ResultIndex]->getObjectKind(),
3618 AssocExprs[ResultIndex]->isTypeDependent(),
3619 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003620 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003621 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003622 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3623 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3624 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3625 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003626 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003627 assert(AssocTypes.size() == AssocExprs.size());
3628 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3629 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003630}
3631
3632GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3633 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003634 ArrayRef<TypeSourceInfo*> AssocTypes,
3635 ArrayRef<Expr*> AssocExprs,
3636 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003637 SourceLocation RParenLoc,
3638 bool ContainsUnexpandedParameterPack)
3639 : Expr(GenericSelectionExprClass,
3640 Context.DependentTy,
3641 VK_RValue,
3642 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003643 /*isTypeDependent=*/true,
3644 /*isValueDependent=*/true,
3645 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003646 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003647 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3648 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3649 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3650 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003651 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003652 assert(AssocTypes.size() == AssocExprs.size());
3653 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3654 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003655}
3656
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003657//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003658// DesignatedInitExpr
3659//===----------------------------------------------------------------------===//
3660
Chandler Carruthb1138242011-06-16 06:47:06 +00003661IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003662 assert(Kind == FieldDesignator && "Only valid on a field designator");
3663 if (Field.NameOrField & 0x01)
3664 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3665 else
3666 return getField()->getIdentifier();
3667}
3668
Sean Huntc3021132010-05-05 15:23:54 +00003669DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003670 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003671 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003672 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003673 bool GNUSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003674 ArrayRef<Expr*> IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003675 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003676 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003677 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003678 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003679 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003680 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003681 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003682 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003683 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003684
3685 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003686 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003687 *Child++ = Init;
3688
3689 // Copy the designators and their subexpressions, computing
3690 // value-dependence along the way.
3691 unsigned IndexIdx = 0;
3692 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003693 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003694
3695 if (this->Designators[I].isArrayDesignator()) {
3696 // Compute type- and value-dependence.
3697 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003698 if (Index->isTypeDependent() || Index->isValueDependent())
3699 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003700 if (Index->isInstantiationDependent())
3701 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003702 // Propagate unexpanded parameter packs.
3703 if (Index->containsUnexpandedParameterPack())
3704 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003705
3706 // Copy the index expressions into permanent storage.
3707 *Child++ = IndexExprs[IndexIdx++];
3708 } else if (this->Designators[I].isArrayRangeDesignator()) {
3709 // Compute type- and value-dependence.
3710 Expr *Start = IndexExprs[IndexIdx];
3711 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003712 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003713 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003714 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003715 ExprBits.InstantiationDependent = true;
3716 } else if (Start->isInstantiationDependent() ||
3717 End->isInstantiationDependent()) {
3718 ExprBits.InstantiationDependent = true;
3719 }
3720
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003721 // Propagate unexpanded parameter packs.
3722 if (Start->containsUnexpandedParameterPack() ||
3723 End->containsUnexpandedParameterPack())
3724 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003725
3726 // Copy the start/end expressions into permanent storage.
3727 *Child++ = IndexExprs[IndexIdx++];
3728 *Child++ = IndexExprs[IndexIdx++];
3729 }
3730 }
3731
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003732 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003733}
3734
Douglas Gregor05c13a32009-01-22 00:58:24 +00003735DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003736DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003737 unsigned NumDesignators,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003738 ArrayRef<Expr*> IndexExprs,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003739 SourceLocation ColonOrEqualLoc,
3740 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003741 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003742 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003743 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003744 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003745 IndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003746}
3747
Mike Stump1eb44332009-09-09 15:08:12 +00003748DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003749 unsigned NumIndexExprs) {
3750 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3751 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3752 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3753}
3754
Douglas Gregor319d57f2010-01-06 23:17:19 +00003755void DesignatedInitExpr::setDesignators(ASTContext &C,
3756 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003757 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003758 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003759 NumDesignators = NumDesigs;
3760 for (unsigned I = 0; I != NumDesigs; ++I)
3761 Designators[I] = Desigs[I];
3762}
3763
Abramo Bagnara24f46742011-03-16 15:08:46 +00003764SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3765 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3766 if (size() == 1)
3767 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen65d78312012-12-25 14:51:39 +00003768 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3769 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara24f46742011-03-16 15:08:46 +00003770}
3771
Erik Verbruggen65d78312012-12-25 14:51:39 +00003772SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003773 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003774 Designator &First =
3775 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003776 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003777 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003778 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3779 else
3780 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3781 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003782 StartLoc =
3783 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen65d78312012-12-25 14:51:39 +00003784 return StartLoc;
3785}
3786
3787SourceLocation DesignatedInitExpr::getLocEnd() const {
3788 return getInit()->getLocEnd();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003789}
3790
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003791Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003792 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003793 char *Ptr = static_cast<char *>(
3794 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003795 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003796 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3797 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3798}
3799
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003800Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003801 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003802 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003803 char *Ptr = static_cast<char *>(
3804 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003805 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003806 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3807 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3808}
3809
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003810Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003811 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003812 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003813 char *Ptr = static_cast<char *>(
3814 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003815 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003816 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3817 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3818}
3819
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003820/// \brief Replaces the designator at index @p Idx with the series
3821/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003822void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003823 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003824 const Designator *Last) {
3825 unsigned NumNewDesignators = Last - First;
3826 if (NumNewDesignators == 0) {
3827 std::copy_backward(Designators + Idx + 1,
3828 Designators + NumDesignators,
3829 Designators + Idx);
3830 --NumNewDesignators;
3831 return;
3832 } else if (NumNewDesignators == 1) {
3833 Designators[Idx] = *First;
3834 return;
3835 }
3836
Mike Stump1eb44332009-09-09 15:08:12 +00003837 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003838 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003839 std::copy(Designators, Designators + Idx, NewDesignators);
3840 std::copy(First, Last, NewDesignators + Idx);
3841 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3842 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003843 Designators = NewDesignators;
3844 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3845}
3846
Mike Stump1eb44332009-09-09 15:08:12 +00003847ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003848 ArrayRef<Expr*> exprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003849 SourceLocation rparenloc)
3850 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003851 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003852 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3853 Exprs = new (C) Stmt*[exprs.size()];
3854 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003855 if (exprs[i]->isTypeDependent())
3856 ExprBits.TypeDependent = true;
3857 if (exprs[i]->isValueDependent())
3858 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003859 if (exprs[i]->isInstantiationDependent())
3860 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003861 if (exprs[i]->containsUnexpandedParameterPack())
3862 ExprBits.ContainsUnexpandedParameterPack = true;
3863
Nate Begeman2ef13e52009-08-10 23:49:36 +00003864 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003865 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003866}
3867
John McCalle996ffd2011-02-16 08:02:54 +00003868const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3869 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3870 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003871 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3872 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003873 e = cast<CXXConstructExpr>(e)->getArg(0);
3874 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3875 e = ice->getSubExpr();
3876 return cast<OpaqueValueExpr>(e);
3877}
3878
John McCall4b9c2d22011-11-06 09:01:30 +00003879PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3880 unsigned numSemanticExprs) {
3881 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3882 (1 + numSemanticExprs) * sizeof(Expr*),
3883 llvm::alignOf<PseudoObjectExpr>());
3884 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3885}
3886
3887PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3888 : Expr(PseudoObjectExprClass, shell) {
3889 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3890}
3891
3892PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3893 ArrayRef<Expr*> semantics,
3894 unsigned resultIndex) {
3895 assert(syntax && "no syntactic expression!");
3896 assert(semantics.size() && "no semantic expressions!");
3897
3898 QualType type;
3899 ExprValueKind VK;
3900 if (resultIndex == NoResult) {
3901 type = C.VoidTy;
3902 VK = VK_RValue;
3903 } else {
3904 assert(resultIndex < semantics.size());
3905 type = semantics[resultIndex]->getType();
3906 VK = semantics[resultIndex]->getValueKind();
3907 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3908 }
3909
3910 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3911 (1 + semantics.size()) * sizeof(Expr*),
3912 llvm::alignOf<PseudoObjectExpr>());
3913 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3914 resultIndex);
3915}
3916
3917PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3918 Expr *syntax, ArrayRef<Expr*> semantics,
3919 unsigned resultIndex)
3920 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3921 /*filled in at end of ctor*/ false, false, false, false) {
3922 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3923 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3924
3925 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3926 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3927 getSubExprsBuffer()[i] = E;
3928
3929 if (E->isTypeDependent())
3930 ExprBits.TypeDependent = true;
3931 if (E->isValueDependent())
3932 ExprBits.ValueDependent = true;
3933 if (E->isInstantiationDependent())
3934 ExprBits.InstantiationDependent = true;
3935 if (E->containsUnexpandedParameterPack())
3936 ExprBits.ContainsUnexpandedParameterPack = true;
3937
3938 if (isa<OpaqueValueExpr>(E))
3939 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3940 "opaque-value semantic expressions for pseudo-object "
3941 "operations must have sources");
3942 }
3943}
3944
Douglas Gregor05c13a32009-01-22 00:58:24 +00003945//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003946// ExprIterator.
3947//===----------------------------------------------------------------------===//
3948
3949Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3950Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3951Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3952const Expr* ConstExprIterator::operator[](size_t idx) const {
3953 return cast<Expr>(I[idx]);
3954}
3955const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3956const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3957
3958//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003959// Child Iterators for iterating over subexpressions/substatements
3960//===----------------------------------------------------------------------===//
3961
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003962// UnaryExprOrTypeTraitExpr
3963Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003964 // If this is of a type and the type is a VLA type (and not a typedef), the
3965 // size expression of the VLA needs to be treated as an executable expression.
3966 // Why isn't this weirdness documented better in StmtIterator?
3967 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003968 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003969 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003970 return child_range(child_iterator(T), child_iterator());
3971 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003972 }
John McCall63c00d72011-02-09 08:16:59 +00003973 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003974}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003975
Steve Naroff563477d2007-09-18 23:55:05 +00003976// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003977Stmt::child_range ObjCMessageExpr::children() {
3978 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003979 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003980 begin = reinterpret_cast<Stmt **>(this + 1);
3981 else
3982 begin = reinterpret_cast<Stmt **>(getArgs());
3983 return child_range(begin,
3984 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003985}
3986
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003987ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003988 QualType T, ObjCMethodDecl *Method,
3989 SourceRange SR)
3990 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3991 false, false, false, false),
3992 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3993{
3994 Expr **SaveElements = getElements();
3995 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3996 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3997 ExprBits.ValueDependent = true;
3998 if (Elements[I]->isInstantiationDependent())
3999 ExprBits.InstantiationDependent = true;
4000 if (Elements[I]->containsUnexpandedParameterPack())
4001 ExprBits.ContainsUnexpandedParameterPack = true;
4002
4003 SaveElements[I] = Elements[I];
4004 }
4005}
4006
4007ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004008 ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004009 QualType T, ObjCMethodDecl * Method,
4010 SourceRange SR) {
4011 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4012 + Elements.size() * sizeof(Expr *));
4013 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4014}
4015
4016ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
4017 unsigned NumElements) {
4018
4019 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4020 + NumElements * sizeof(Expr *));
4021 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4022}
4023
4024ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4025 ArrayRef<ObjCDictionaryElement> VK,
4026 bool HasPackExpansions,
4027 QualType T, ObjCMethodDecl *method,
4028 SourceRange SR)
4029 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4030 false, false),
4031 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4032 DictWithObjectsMethod(method)
4033{
4034 KeyValuePair *KeyValues = getKeyValues();
4035 ExpansionData *Expansions = getExpansionData();
4036 for (unsigned I = 0; I < NumElements; I++) {
4037 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4038 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4039 ExprBits.ValueDependent = true;
4040 if (VK[I].Key->isInstantiationDependent() ||
4041 VK[I].Value->isInstantiationDependent())
4042 ExprBits.InstantiationDependent = true;
4043 if (VK[I].EllipsisLoc.isInvalid() &&
4044 (VK[I].Key->containsUnexpandedParameterPack() ||
4045 VK[I].Value->containsUnexpandedParameterPack()))
4046 ExprBits.ContainsUnexpandedParameterPack = true;
4047
4048 KeyValues[I].Key = VK[I].Key;
4049 KeyValues[I].Value = VK[I].Value;
4050 if (Expansions) {
4051 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4052 if (VK[I].NumExpansions)
4053 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4054 else
4055 Expansions[I].NumExpansionsPlusOne = 0;
4056 }
4057 }
4058}
4059
4060ObjCDictionaryLiteral *
4061ObjCDictionaryLiteral::Create(ASTContext &C,
4062 ArrayRef<ObjCDictionaryElement> VK,
4063 bool HasPackExpansions,
4064 QualType T, ObjCMethodDecl *method,
4065 SourceRange SR) {
4066 unsigned ExpansionsSize = 0;
4067 if (HasPackExpansions)
4068 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4069
4070 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4071 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4072 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4073}
4074
4075ObjCDictionaryLiteral *
4076ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
4077 bool HasPackExpansions) {
4078 unsigned ExpansionsSize = 0;
4079 if (HasPackExpansions)
4080 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4081 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4082 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4083 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4084 HasPackExpansions);
4085}
4086
4087ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
4088 Expr *base,
4089 Expr *key, QualType T,
4090 ObjCMethodDecl *getMethod,
4091 ObjCMethodDecl *setMethod,
4092 SourceLocation RB) {
4093 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4094 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4095 OK_ObjCSubscript,
4096 getMethod, setMethod, RB);
4097}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004098
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004099AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004100 QualType t, AtomicOp op, SourceLocation RP)
4101 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4102 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004103 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004104{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004105 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4106 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004107 if (args[i]->isTypeDependent())
4108 ExprBits.TypeDependent = true;
4109 if (args[i]->isValueDependent())
4110 ExprBits.ValueDependent = true;
4111 if (args[i]->isInstantiationDependent())
4112 ExprBits.InstantiationDependent = true;
4113 if (args[i]->containsUnexpandedParameterPack())
4114 ExprBits.ContainsUnexpandedParameterPack = true;
4115
4116 SubExprs[i] = args[i];
4117 }
4118}
Richard Smithe1b2abc2012-04-10 22:49:28 +00004119
4120unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4121 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00004122 case AO__c11_atomic_init:
4123 case AO__c11_atomic_load:
4124 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004125 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00004126
4127 case AO__c11_atomic_store:
4128 case AO__c11_atomic_exchange:
4129 case AO__atomic_load:
4130 case AO__atomic_store:
4131 case AO__atomic_store_n:
4132 case AO__atomic_exchange_n:
4133 case AO__c11_atomic_fetch_add:
4134 case AO__c11_atomic_fetch_sub:
4135 case AO__c11_atomic_fetch_and:
4136 case AO__c11_atomic_fetch_or:
4137 case AO__c11_atomic_fetch_xor:
4138 case AO__atomic_fetch_add:
4139 case AO__atomic_fetch_sub:
4140 case AO__atomic_fetch_and:
4141 case AO__atomic_fetch_or:
4142 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00004143 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00004144 case AO__atomic_add_fetch:
4145 case AO__atomic_sub_fetch:
4146 case AO__atomic_and_fetch:
4147 case AO__atomic_or_fetch:
4148 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00004149 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004150 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00004151
4152 case AO__atomic_exchange:
4153 return 4;
4154
4155 case AO__c11_atomic_compare_exchange_strong:
4156 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004157 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00004158
4159 case AO__atomic_compare_exchange:
4160 case AO__atomic_compare_exchange_n:
4161 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00004162 }
4163 llvm_unreachable("unknown atomic op");
4164}