blob: 6a1e500d330aac2a3fc3c7b94de1a8dac7fd81a2 [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
Craig Topper05ed1a02013-08-18 10:09:15 +0000625void APNumericStorage::setIntValue(const ASTContext &C,
626 const llvm::APInt &Val) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000627 if (hasAllocation())
628 C.Deallocate(pVal);
629
630 BitWidth = Val.getBitWidth();
631 unsigned NumWords = Val.getNumWords();
632 const uint64_t* Words = Val.getRawData();
633 if (NumWords > 1) {
634 pVal = new (C) uint64_t[NumWords];
635 std::copy(Words, Words + NumWords, pVal);
636 } else if (NumWords == 1)
637 VAL = Words[0];
638 else
639 VAL = 0;
640}
641
Craig Topper05ed1a02013-08-18 10:09:15 +0000642IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000643 QualType type, SourceLocation l)
644 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
645 false, false),
646 Loc(l) {
647 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
648 assert(V.getBitWidth() == C.getIntWidth(type) &&
649 "Integer type is not the correct size for constant.");
650 setValue(C, V);
651}
652
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000653IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000654IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000655 QualType type, SourceLocation l) {
656 return new (C) IntegerLiteral(C, V, type, l);
657}
658
659IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000660IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000661 return new (C) IntegerLiteral(Empty);
662}
663
Craig Topper05ed1a02013-08-18 10:09:15 +0000664FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000665 bool isexact, QualType Type, SourceLocation L)
666 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
667 false, false), Loc(L) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000668 setSemantics(V.getSemantics());
Benjamin Kramer478851c2012-07-04 17:04:04 +0000669 FloatingLiteralBits.IsExact = isexact;
670 setValue(C, V);
671}
672
Craig Topper05ed1a02013-08-18 10:09:15 +0000673FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer478851c2012-07-04 17:04:04 +0000674 : Expr(FloatingLiteralClass, Empty) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000675 setRawSemantics(IEEEhalf);
Benjamin Kramer478851c2012-07-04 17:04:04 +0000676 FloatingLiteralBits.IsExact = false;
677}
678
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000679FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000680FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000681 bool isexact, QualType Type, SourceLocation L) {
682 return new (C) FloatingLiteral(C, V, isexact, Type, L);
683}
684
685FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000686FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000687 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000688}
689
Tim Northover9ec55f22013-01-22 09:46:51 +0000690const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
691 switch(FloatingLiteralBits.Semantics) {
692 case IEEEhalf:
693 return llvm::APFloat::IEEEhalf;
694 case IEEEsingle:
695 return llvm::APFloat::IEEEsingle;
696 case IEEEdouble:
697 return llvm::APFloat::IEEEdouble;
698 case x87DoubleExtended:
699 return llvm::APFloat::x87DoubleExtended;
700 case IEEEquad:
701 return llvm::APFloat::IEEEquad;
702 case PPCDoubleDouble:
703 return llvm::APFloat::PPCDoubleDouble;
704 }
705 llvm_unreachable("Unrecognised floating semantics");
706}
707
708void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
709 if (&Sem == &llvm::APFloat::IEEEhalf)
710 FloatingLiteralBits.Semantics = IEEEhalf;
711 else if (&Sem == &llvm::APFloat::IEEEsingle)
712 FloatingLiteralBits.Semantics = IEEEsingle;
713 else if (&Sem == &llvm::APFloat::IEEEdouble)
714 FloatingLiteralBits.Semantics = IEEEdouble;
715 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
716 FloatingLiteralBits.Semantics = x87DoubleExtended;
717 else if (&Sem == &llvm::APFloat::IEEEquad)
718 FloatingLiteralBits.Semantics = IEEEquad;
719 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
720 FloatingLiteralBits.Semantics = PPCDoubleDouble;
721 else
722 llvm_unreachable("Unknown floating semantics");
723}
724
Chris Lattnerda8249e2008-06-07 22:13:43 +0000725/// getValueAsApproximateDouble - This returns the value as an inaccurate
726/// double. Note that this may cause loss of precision, but is useful for
727/// debugging dumps, etc.
728double FloatingLiteral::getValueAsApproximateDouble() const {
729 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000730 bool ignored;
731 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
732 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000733 return V.convertToDouble();
734}
735
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000736int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000737 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000738 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000739 case Ascii:
740 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000741 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000742 break;
743 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000744 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000745 break;
746 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000747 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000748 break;
749 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000750 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000751 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000752 }
753 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
754 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000755 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000756 && "character byte widths supported are 1, 2, and 4 only");
757 return CharByteWidth;
758}
759
Craig Topper05ed1a02013-08-18 10:09:15 +0000760StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000761 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000762 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000763 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000764 // Allocate enough space for the StringLiteral plus an array of locations for
765 // any concatenated string tokens.
766 void *Mem = C.Allocate(sizeof(StringLiteral)+
767 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000768 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000769 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000772 SL->setString(C,Str,Kind,Pascal);
773
Chris Lattner2085fd62009-02-18 06:40:38 +0000774 SL->TokLocs[0] = Loc[0];
775 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000776
Chris Lattner726e1682009-02-18 05:49:11 +0000777 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000778 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
779 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000780}
781
Craig Topper05ed1a02013-08-18 10:09:15 +0000782StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
783 unsigned NumStrs) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000784 void *Mem = C.Allocate(sizeof(StringLiteral)+
785 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000786 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000787 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000788 SL->CharByteWidth = 0;
789 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000790 SL->NumConcatenated = NumStrs;
791 return SL;
792}
793
Alexander Kornienkoae541212013-02-01 12:35:51 +0000794void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieu8ab09da2012-06-13 20:25:24 +0000795 switch (getKind()) {
796 case Ascii: break; // no prefix.
797 case Wide: OS << 'L'; break;
798 case UTF8: OS << "u8"; break;
799 case UTF16: OS << 'u'; break;
800 case UTF32: OS << 'U'; break;
801 }
802 OS << '"';
803 static const char Hex[] = "0123456789ABCDEF";
804
805 unsigned LastSlashX = getLength();
806 for (unsigned I = 0, N = getLength(); I != N; ++I) {
807 switch (uint32_t Char = getCodeUnit(I)) {
808 default:
809 // FIXME: Convert UTF-8 back to codepoints before rendering.
810
811 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
812 // Leave invalid surrogates alone; we'll use \x for those.
813 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
814 Char <= 0xdbff) {
815 uint32_t Trail = getCodeUnit(I + 1);
816 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
817 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
818 ++I;
819 }
820 }
821
822 if (Char > 0xff) {
823 // If this is a wide string, output characters over 0xff using \x
824 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
825 // codepoint: use \x escapes for invalid codepoints.
826 if (getKind() == Wide ||
827 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
828 // FIXME: Is this the best way to print wchar_t?
829 OS << "\\x";
830 int Shift = 28;
831 while ((Char >> Shift) == 0)
832 Shift -= 4;
833 for (/**/; Shift >= 0; Shift -= 4)
834 OS << Hex[(Char >> Shift) & 15];
835 LastSlashX = I;
836 break;
837 }
838
839 if (Char > 0xffff)
840 OS << "\\U00"
841 << Hex[(Char >> 20) & 15]
842 << Hex[(Char >> 16) & 15];
843 else
844 OS << "\\u";
845 OS << Hex[(Char >> 12) & 15]
846 << Hex[(Char >> 8) & 15]
847 << Hex[(Char >> 4) & 15]
848 << Hex[(Char >> 0) & 15];
849 break;
850 }
851
852 // If we used \x... for the previous character, and this character is a
853 // hexadecimal digit, prevent it being slurped as part of the \x.
854 if (LastSlashX + 1 == I) {
855 switch (Char) {
856 case '0': case '1': case '2': case '3': case '4':
857 case '5': case '6': case '7': case '8': case '9':
858 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
859 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
860 OS << "\"\"";
861 }
862 }
863
864 assert(Char <= 0xff &&
865 "Characters above 0xff should already have been handled.");
866
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000867 if (isPrintable(Char))
Richard Trieu8ab09da2012-06-13 20:25:24 +0000868 OS << (char)Char;
869 else // Output anything hard as an octal escape.
870 OS << '\\'
871 << (char)('0' + ((Char >> 6) & 7))
872 << (char)('0' + ((Char >> 3) & 7))
873 << (char)('0' + ((Char >> 0) & 7));
874 break;
875 // Handle some common non-printable cases to make dumps prettier.
876 case '\\': OS << "\\\\"; break;
877 case '"': OS << "\\\""; break;
878 case '\n': OS << "\\n"; break;
879 case '\t': OS << "\\t"; break;
880 case '\a': OS << "\\a"; break;
881 case '\b': OS << "\\b"; break;
882 }
883 }
884 OS << '"';
885}
886
Craig Topper05ed1a02013-08-18 10:09:15 +0000887void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedman64f45a22011-11-01 02:23:42 +0000888 StringKind Kind, bool IsPascal) {
889 //FIXME: we assume that the string data comes from a target that uses the same
890 // code unit size and endianess for the type of string.
891 this->Kind = Kind;
892 this->IsPascal = IsPascal;
893
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000894 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000895 assert((Str.size()%CharByteWidth == 0)
896 && "size of data must be multiple of CharByteWidth");
897 Length = Str.size()/CharByteWidth;
898
899 switch(CharByteWidth) {
900 case 1: {
901 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000902 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000903 StrData.asChar = AStrData;
904 break;
905 }
906 case 2: {
907 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000908 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000909 StrData.asUInt16 = AStrData;
910 break;
911 }
912 case 4: {
913 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000914 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000915 StrData.asUInt32 = AStrData;
916 break;
917 }
918 default:
919 assert(false && "unsupported CharByteWidth");
920 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000921}
922
Chris Lattner08f92e32010-11-17 07:37:15 +0000923/// getLocationOfByte - Return a source location that points to the specified
924/// byte of this string literal.
925///
926/// Strings are amazingly complex. They can be formed from multiple tokens and
927/// can have escape sequences in them in addition to the usual trigraph and
928/// escaped newline business. This routine handles this complexity.
929///
930SourceLocation StringLiteral::
931getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
932 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000933 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
934 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000935
Chris Lattner08f92e32010-11-17 07:37:15 +0000936 // Loop over all of the tokens in this string until we find the one that
937 // contains the byte we're looking for.
938 unsigned TokNo = 0;
939 while (1) {
940 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
941 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
942
943 // Get the spelling of the string so that we can get the data that makes up
944 // the string literal, not the identifier for the macro it is potentially
945 // expanded through.
946 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
947
948 // Re-lex the token to get its length and original spelling.
949 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
950 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000951 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000952 if (Invalid)
953 return StrTokSpellingLoc;
954
955 const char *StrData = Buffer.data()+LocInfo.second;
956
Chris Lattner08f92e32010-11-17 07:37:15 +0000957 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000958 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
959 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000960 Token TheTok;
961 TheLexer.LexFromRawLexer(TheTok);
962
963 // Use the StringLiteralParser to compute the length of the string in bytes.
964 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
965 unsigned TokNumBytes = SLP.GetStringLength();
966
967 // If the byte is in this token, return the location of the byte.
968 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000969 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000970 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
971
972 // Now that we know the offset of the token in the spelling, use the
973 // preprocessor to get the offset in the original source.
974 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
975 }
976
977 // Move to the next string token.
978 ++TokNo;
979 ByteNo -= TokNumBytes;
980 }
981}
982
983
984
Reid Spencer5f016e22007-07-11 17:01:13 +0000985/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
986/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie0bea8632012-10-08 01:11:04 +0000987StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000989 case UO_PostInc: return "++";
990 case UO_PostDec: return "--";
991 case UO_PreInc: return "++";
992 case UO_PreDec: return "--";
993 case UO_AddrOf: return "&";
994 case UO_Deref: return "*";
995 case UO_Plus: return "+";
996 case UO_Minus: return "-";
997 case UO_Not: return "~";
998 case UO_LNot: return "!";
999 case UO_Real: return "__real";
1000 case UO_Imag: return "__imag";
1001 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001003 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +00001004}
1005
John McCall2de56d12010-08-25 11:45:40 +00001006UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001007UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1008 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001009 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +00001010 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1011 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1012 case OO_Amp: return UO_AddrOf;
1013 case OO_Star: return UO_Deref;
1014 case OO_Plus: return UO_Plus;
1015 case OO_Minus: return UO_Minus;
1016 case OO_Tilde: return UO_Not;
1017 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001018 }
1019}
1020
1021OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1022 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +00001023 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1024 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1025 case UO_AddrOf: return OO_Amp;
1026 case UO_Deref: return OO_Star;
1027 case UO_Plus: return OO_Plus;
1028 case UO_Minus: return OO_Minus;
1029 case UO_Not: return OO_Tilde;
1030 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001031 default: return OO_None;
1032 }
1033}
1034
1035
Reid Spencer5f016e22007-07-11 17:01:13 +00001036//===----------------------------------------------------------------------===//
1037// Postfix Operators.
1038//===----------------------------------------------------------------------===//
1039
Craig Topper05ed1a02013-08-18 10:09:15 +00001040CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1041 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1042 ExprValueKind VK, SourceLocation rparenloc)
John McCallf89e55a2010-11-18 06:31:45 +00001043 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001044 fn->isTypeDependent(),
1045 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001046 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001047 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001048 NumArgs(args.size()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001050 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +00001051 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001052 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001053 if (args[i]->isTypeDependent())
1054 ExprBits.TypeDependent = true;
1055 if (args[i]->isValueDependent())
1056 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001057 if (args[i]->isInstantiationDependent())
1058 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001059 if (args[i]->containsUnexpandedParameterPack())
1060 ExprBits.ContainsUnexpandedParameterPack = true;
1061
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001062 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001063 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001064
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001065 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +00001066 RParenLoc = rparenloc;
1067}
Nate Begemane2ce1d92008-01-17 17:46:27 +00001068
Craig Topper05ed1a02013-08-18 10:09:15 +00001069CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCallf89e55a2010-11-18 06:31:45 +00001070 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1071 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001072 fn->isTypeDependent(),
1073 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001074 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001075 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001076 NumArgs(args.size()) {
Ted Kremenek668bf912009-02-09 20:51:47 +00001077
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001078 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001079 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001080 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001081 if (args[i]->isTypeDependent())
1082 ExprBits.TypeDependent = true;
1083 if (args[i]->isValueDependent())
1084 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001085 if (args[i]->isInstantiationDependent())
1086 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001087 if (args[i]->containsUnexpandedParameterPack())
1088 ExprBits.ContainsUnexpandedParameterPack = true;
1089
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001090 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001091 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001092
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001093 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 RParenLoc = rparenloc;
1095}
1096
Craig Topper05ed1a02013-08-18 10:09:15 +00001097CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Mike Stump1eb44332009-09-09 15:08:12 +00001098 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001099 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001100 SubExprs = new (C) Stmt*[PREARGS_START];
1101 CallExprBits.NumPreArgs = 0;
1102}
1103
Craig Topper05ed1a02013-08-18 10:09:15 +00001104CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001105 EmptyShell Empty)
1106 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1107 // FIXME: Why do we allocate this?
1108 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1109 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001110}
1111
Nuno Lopesd20254f2009-12-20 23:11:08 +00001112Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +00001113 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +00001114
1115 while (SubstNonTypeTemplateParmExpr *NTTP
1116 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1117 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1118 }
1119
Sebastian Redl20012152010-09-10 20:55:30 +00001120 // If we're calling a dereference, look at the pointer instead.
1121 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1122 if (BO->isPtrMemOp())
1123 CEE = BO->getRHS()->IgnoreParenCasts();
1124 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1125 if (UO->getOpcode() == UO_Deref)
1126 CEE = UO->getSubExpr()->IgnoreParenCasts();
1127 }
Chris Lattner6346f962009-07-17 15:46:27 +00001128 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001129 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001130 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1131 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001132
1133 return 0;
1134}
1135
Nuno Lopesd20254f2009-12-20 23:11:08 +00001136FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001137 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001138}
1139
Chris Lattnerd18b3292007-12-28 05:25:02 +00001140/// setNumArgs - This changes the number of arguments present in this call.
1141/// Any orphaned expressions are deleted by this, and any new operands are set
1142/// to null.
Craig Topper05ed1a02013-08-18 10:09:15 +00001143void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001144 // No change, just return.
1145 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Chris Lattnerd18b3292007-12-28 05:25:02 +00001147 // If shrinking # arguments, just delete the extras and forgot them.
1148 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001149 this->NumArgs = NumArgs;
1150 return;
1151 }
1152
1153 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001154 unsigned NumPreArgs = getNumPreArgs();
1155 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001156 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001157 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001158 NewSubExprs[i] = SubExprs[i];
1159 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001160 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1161 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001162 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregor88c9a462009-04-17 21:46:47 +00001164 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001165 SubExprs = NewSubExprs;
1166 this->NumArgs = NumArgs;
1167}
1168
Chris Lattnercb888962008-10-06 05:00:53 +00001169/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1170/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001171unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001172 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001173 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001174 // ImplicitCastExpr.
1175 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1176 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001177 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001179 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1180 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001181 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Anders Carlssonbcba2012008-01-31 02:13:57 +00001183 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1184 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001185 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001187 if (!FDecl->getIdentifier())
1188 return 0;
1189
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001190 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001191}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001192
Richard Smithba571832013-01-17 23:46:04 +00001193bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1194 if (unsigned BI = isBuiltinCall())
1195 return Ctx.BuiltinInfo.isUnevaluated(BI);
1196 return false;
1197}
1198
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001199QualType CallExpr::getCallReturnType() const {
1200 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001201 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001202 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001203 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001204 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001205 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1206 // This should never be overloaded and so should never return null.
1207 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001208
John McCall864c0412011-04-26 20:42:42 +00001209 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001210 return FnType->getResultType();
1211}
Chris Lattnercb888962008-10-06 05:00:53 +00001212
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001213SourceLocation CallExpr::getLocStart() const {
1214 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001215 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001216
1217 SourceLocation begin = getCallee()->getLocStart();
1218 if (begin.isInvalid() && getNumArgs() > 0)
1219 begin = getArg(0)->getLocStart();
1220 return begin;
1221}
1222SourceLocation CallExpr::getLocEnd() const {
1223 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001224 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001225
1226 SourceLocation end = getRParenLoc();
1227 if (end.isInvalid() && getNumArgs() > 0)
1228 end = getArg(getNumArgs() - 1)->getLocEnd();
1229 return end;
1230}
John McCall2882eca2011-02-21 06:23:05 +00001231
Craig Topper05ed1a02013-08-18 10:09:15 +00001232OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001233 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001234 TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001235 ArrayRef<OffsetOfNode> comps,
1236 ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001237 SourceLocation RParenLoc) {
1238 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001239 sizeof(OffsetOfNode) * comps.size() +
1240 sizeof(Expr*) * exprs.size());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001241
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001242 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1243 RParenLoc);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001244}
1245
Craig Topper05ed1a02013-08-18 10:09:15 +00001246OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001247 unsigned numComps, unsigned numExprs) {
1248 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1249 sizeof(OffsetOfNode) * numComps +
1250 sizeof(Expr*) * numExprs);
1251 return new (Mem) OffsetOfExpr(numComps, numExprs);
1252}
1253
Craig Topper05ed1a02013-08-18 10:09:15 +00001254OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001255 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001256 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001257 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001258 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1259 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001260 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001261 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001262 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001263 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001264 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001265{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001266 for (unsigned i = 0; i != comps.size(); ++i) {
1267 setComponent(i, comps[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001268 }
Sean Huntc3021132010-05-05 15:23:54 +00001269
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001270 for (unsigned i = 0; i != exprs.size(); ++i) {
1271 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001272 ExprBits.ValueDependent = true;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001273 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001274 ExprBits.ContainsUnexpandedParameterPack = true;
1275
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001276 setIndexExpr(i, exprs[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001277 }
1278}
1279
1280IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1281 assert(getKind() == Field || getKind() == Identifier);
1282 if (getKind() == Field)
1283 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001284
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001285 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1286}
1287
Craig Topper05ed1a02013-08-18 10:09:15 +00001288MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001289 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001290 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001291 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001292 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001293 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001294 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001295 QualType ty,
1296 ExprValueKind vk,
1297 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001298 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001299
Douglas Gregor40d96a62011-02-28 21:54:11 +00001300 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001301 founddecl.getDecl() != memberdecl ||
1302 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001303 if (hasQualOrFound)
1304 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001305
John McCalld5532b62009-11-23 01:53:49 +00001306 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001307 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1308 else if (TemplateKWLoc.isValid())
1309 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Chris Lattner32488542010-10-30 05:14:06 +00001311 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001312 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1313 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001314
1315 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001316 // FIXME: Wrong. We should be looking at the member declaration we found.
1317 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001318 E->setValueDependent(true);
1319 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001320 E->setInstantiationDependent(true);
1321 }
1322 else if (QualifierLoc &&
1323 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1324 E->setInstantiationDependent(true);
1325
John McCall6bb80172010-03-30 21:47:33 +00001326 E->HasQualifierOrFoundDecl = true;
1327
1328 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001329 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001330 NQ->FoundDecl = founddecl;
1331 }
1332
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001333 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1334
John McCall6bb80172010-03-30 21:47:33 +00001335 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001336 bool Dependent = false;
1337 bool InstantiationDependent = false;
1338 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001339 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1340 Dependent,
1341 InstantiationDependent,
1342 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001343 if (InstantiationDependent)
1344 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001345 } else if (TemplateKWLoc.isValid()) {
1346 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001347 }
1348
1349 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001350}
1351
Daniel Dunbar396ec672012-03-09 15:39:15 +00001352SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001353 if (isImplicitAccess()) {
1354 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001355 return getQualifierLoc().getBeginLoc();
1356 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001357 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001358
Daniel Dunbar396ec672012-03-09 15:39:15 +00001359 // FIXME: We don't want this to happen. Rather, we should be able to
1360 // detect all kinds of implicit accesses more cleanly.
1361 SourceLocation BaseStartLoc = getBase()->getLocStart();
1362 if (BaseStartLoc.isValid())
1363 return BaseStartLoc;
1364 return MemberLoc;
1365}
1366SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001367 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbar396ec672012-03-09 15:39:15 +00001368 if (hasExplicitTemplateArgs())
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001369 EndLoc = getRAngleLoc();
1370 else if (EndLoc.isInvalid())
1371 EndLoc = getBase()->getLocEnd();
1372 return EndLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001373}
1374
John McCall1d9b3b22011-09-09 05:25:32 +00001375void CastExpr::CheckCastConsistency() const {
1376 switch (getCastKind()) {
1377 case CK_DerivedToBase:
1378 case CK_UncheckedDerivedToBase:
1379 case CK_DerivedToBaseMemberPointer:
1380 case CK_BaseToDerived:
1381 case CK_BaseToDerivedMemberPointer:
1382 assert(!path_empty() && "Cast kind should have a base path!");
1383 break;
1384
1385 case CK_CPointerToObjCPointerCast:
1386 assert(getType()->isObjCObjectPointerType());
1387 assert(getSubExpr()->getType()->isPointerType());
1388 goto CheckNoBasePath;
1389
1390 case CK_BlockPointerToObjCPointerCast:
1391 assert(getType()->isObjCObjectPointerType());
1392 assert(getSubExpr()->getType()->isBlockPointerType());
1393 goto CheckNoBasePath;
1394
John McCall4d4e5c12012-02-15 01:22:51 +00001395 case CK_ReinterpretMemberPointer:
1396 assert(getType()->isMemberPointerType());
1397 assert(getSubExpr()->getType()->isMemberPointerType());
1398 goto CheckNoBasePath;
1399
John McCall1d9b3b22011-09-09 05:25:32 +00001400 case CK_BitCast:
1401 // Arbitrary casts to C pointer types count as bitcasts.
1402 // Otherwise, we should only have block and ObjC pointer casts
1403 // here if they stay within the type kind.
1404 if (!getType()->isPointerType()) {
1405 assert(getType()->isObjCObjectPointerType() ==
1406 getSubExpr()->getType()->isObjCObjectPointerType());
1407 assert(getType()->isBlockPointerType() ==
1408 getSubExpr()->getType()->isBlockPointerType());
1409 }
1410 goto CheckNoBasePath;
1411
1412 case CK_AnyPointerToBlockPointerCast:
1413 assert(getType()->isBlockPointerType());
1414 assert(getSubExpr()->getType()->isAnyPointerType() &&
1415 !getSubExpr()->getType()->isBlockPointerType());
1416 goto CheckNoBasePath;
1417
Douglas Gregorac1303e2012-02-22 05:02:47 +00001418 case CK_CopyAndAutoreleaseBlockObject:
1419 assert(getType()->isBlockPointerType());
1420 assert(getSubExpr()->getType()->isBlockPointerType());
1421 goto CheckNoBasePath;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001422
1423 case CK_FunctionToPointerDecay:
1424 assert(getType()->isPointerType());
1425 assert(getSubExpr()->getType()->isFunctionType());
1426 goto CheckNoBasePath;
1427
John McCall1d9b3b22011-09-09 05:25:32 +00001428 // These should not have an inheritance path.
1429 case CK_Dynamic:
1430 case CK_ToUnion:
1431 case CK_ArrayToPointerDecay:
John McCall1d9b3b22011-09-09 05:25:32 +00001432 case CK_NullToMemberPointer:
1433 case CK_NullToPointer:
1434 case CK_ConstructorConversion:
1435 case CK_IntegralToPointer:
1436 case CK_PointerToIntegral:
1437 case CK_ToVoid:
1438 case CK_VectorSplat:
1439 case CK_IntegralCast:
1440 case CK_IntegralToFloating:
1441 case CK_FloatingToIntegral:
1442 case CK_FloatingCast:
1443 case CK_ObjCObjectLValueCast:
1444 case CK_FloatingRealToComplex:
1445 case CK_FloatingComplexToReal:
1446 case CK_FloatingComplexCast:
1447 case CK_FloatingComplexToIntegralComplex:
1448 case CK_IntegralRealToComplex:
1449 case CK_IntegralComplexToReal:
1450 case CK_IntegralComplexCast:
1451 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001452 case CK_ARCProduceObject:
1453 case CK_ARCConsumeObject:
1454 case CK_ARCReclaimReturnedObject:
1455 case CK_ARCExtendBlockObject:
Guy Benyeie6b9d802013-01-20 12:31:11 +00001456 case CK_ZeroToOCLEvent:
John McCall1d9b3b22011-09-09 05:25:32 +00001457 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1458 goto CheckNoBasePath;
1459
1460 case CK_Dependent:
1461 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001462 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001463 case CK_AtomicToNonAtomic:
1464 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001465 case CK_PointerToBoolean:
1466 case CK_IntegralToBoolean:
1467 case CK_FloatingToBoolean:
1468 case CK_MemberPointerToBoolean:
1469 case CK_FloatingComplexToBoolean:
1470 case CK_IntegralComplexToBoolean:
1471 case CK_LValueBitCast: // -> bool&
1472 case CK_UserDefinedConversion: // operator bool()
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001473 case CK_BuiltinFnToFnPtr:
John McCall1d9b3b22011-09-09 05:25:32 +00001474 CheckNoBasePath:
1475 assert(path_empty() && "Cast kind should not have a base path!");
1476 break;
1477 }
1478}
1479
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001480const char *CastExpr::getCastKindName() const {
1481 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001482 case CK_Dependent:
1483 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001484 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001485 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001486 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001487 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001488 case CK_LValueToRValue:
1489 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001490 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001491 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001492 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001493 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001494 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001495 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001496 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001497 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001498 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001499 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001500 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001501 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001502 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001503 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001504 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001505 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001506 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001507 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001508 case CK_NullToPointer:
1509 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001510 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001511 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001512 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001513 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001514 case CK_ReinterpretMemberPointer:
1515 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001516 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001517 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001518 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001519 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001520 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001521 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001522 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001523 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001524 case CK_PointerToBoolean:
1525 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001526 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001527 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001528 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001529 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001530 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001531 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001532 case CK_IntegralToBoolean:
1533 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001534 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001535 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001536 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001537 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001538 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001539 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001540 case CK_FloatingToBoolean:
1541 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001542 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001543 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001544 case CK_CPointerToObjCPointerCast:
1545 return "CPointerToObjCPointerCast";
1546 case CK_BlockPointerToObjCPointerCast:
1547 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001548 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001549 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001550 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001551 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001552 case CK_FloatingRealToComplex:
1553 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001554 case CK_FloatingComplexToReal:
1555 return "FloatingComplexToReal";
1556 case CK_FloatingComplexToBoolean:
1557 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001558 case CK_FloatingComplexCast:
1559 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001560 case CK_FloatingComplexToIntegralComplex:
1561 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001562 case CK_IntegralRealToComplex:
1563 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001564 case CK_IntegralComplexToReal:
1565 return "IntegralComplexToReal";
1566 case CK_IntegralComplexToBoolean:
1567 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001568 case CK_IntegralComplexCast:
1569 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001570 case CK_IntegralComplexToFloatingComplex:
1571 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001572 case CK_ARCConsumeObject:
1573 return "ARCConsumeObject";
1574 case CK_ARCProduceObject:
1575 return "ARCProduceObject";
1576 case CK_ARCReclaimReturnedObject:
1577 return "ARCReclaimReturnedObject";
1578 case CK_ARCExtendBlockObject:
1579 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001580 case CK_AtomicToNonAtomic:
1581 return "AtomicToNonAtomic";
1582 case CK_NonAtomicToAtomic:
1583 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001584 case CK_CopyAndAutoreleaseBlockObject:
1585 return "CopyAndAutoreleaseBlockObject";
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001586 case CK_BuiltinFnToFnPtr:
1587 return "BuiltinFnToFnPtr";
Guy Benyeie6b9d802013-01-20 12:31:11 +00001588 case CK_ZeroToOCLEvent:
1589 return "ZeroToOCLEvent";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001590 }
Mike Stump1eb44332009-09-09 15:08:12 +00001591
John McCall2bb5d002010-11-13 09:02:35 +00001592 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001593}
1594
Douglas Gregor6eef5192009-12-14 19:27:10 +00001595Expr *CastExpr::getSubExprAsWritten() {
1596 Expr *SubExpr = 0;
1597 CastExpr *E = this;
1598 do {
1599 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001600
1601 // Skip through reference binding to temporary.
1602 if (MaterializeTemporaryExpr *Materialize
1603 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1604 SubExpr = Materialize->GetTemporaryExpr();
1605
Douglas Gregor6eef5192009-12-14 19:27:10 +00001606 // Skip any temporary bindings; they're implicit.
1607 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1608 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001609
Douglas Gregor6eef5192009-12-14 19:27:10 +00001610 // Conversions by constructor and conversion functions have a
1611 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001612 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001613 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001614 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001615 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001616
Douglas Gregor6eef5192009-12-14 19:27:10 +00001617 // If the subexpression we're left with is an implicit cast, look
1618 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001619 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1620
Douglas Gregor6eef5192009-12-14 19:27:10 +00001621 return SubExpr;
1622}
1623
John McCallf871d0c2010-08-07 06:22:56 +00001624CXXBaseSpecifier **CastExpr::path_buffer() {
1625 switch (getStmtClass()) {
1626#define ABSTRACT_STMT(x)
1627#define CASTEXPR(Type, Base) \
1628 case Stmt::Type##Class: \
1629 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1630#define STMT(Type, Base)
1631#include "clang/AST/StmtNodes.inc"
1632 default:
1633 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001634 }
1635}
1636
1637void CastExpr::setCastPath(const CXXCastPath &Path) {
1638 assert(Path.size() == path_size());
1639 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1640}
1641
Craig Topper05ed1a02013-08-18 10:09:15 +00001642ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallf871d0c2010-08-07 06:22:56 +00001643 CastKind Kind, Expr *Operand,
1644 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001645 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001646 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1647 void *Buffer =
1648 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1649 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001650 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001651 if (PathSize) E->setCastPath(*BasePath);
1652 return E;
1653}
1654
Craig Topper05ed1a02013-08-18 10:09:15 +00001655ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallf871d0c2010-08-07 06:22:56 +00001656 unsigned PathSize) {
1657 void *Buffer =
1658 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1659 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1660}
1661
1662
Craig Topper05ed1a02013-08-18 10:09:15 +00001663CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001664 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001665 const CXXCastPath *BasePath,
1666 TypeSourceInfo *WrittenTy,
1667 SourceLocation L, SourceLocation R) {
1668 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1669 void *Buffer =
1670 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1671 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001672 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001673 if (PathSize) E->setCastPath(*BasePath);
1674 return E;
1675}
1676
Craig Topper05ed1a02013-08-18 10:09:15 +00001677CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1678 unsigned PathSize) {
John McCallf871d0c2010-08-07 06:22:56 +00001679 void *Buffer =
1680 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1681 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1682}
1683
Reid Spencer5f016e22007-07-11 17:01:13 +00001684/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1685/// corresponds to, e.g. "<<=".
David Blaikie0bea8632012-10-08 01:11:04 +00001686StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001688 case BO_PtrMemD: return ".*";
1689 case BO_PtrMemI: return "->*";
1690 case BO_Mul: return "*";
1691 case BO_Div: return "/";
1692 case BO_Rem: return "%";
1693 case BO_Add: return "+";
1694 case BO_Sub: return "-";
1695 case BO_Shl: return "<<";
1696 case BO_Shr: return ">>";
1697 case BO_LT: return "<";
1698 case BO_GT: return ">";
1699 case BO_LE: return "<=";
1700 case BO_GE: return ">=";
1701 case BO_EQ: return "==";
1702 case BO_NE: return "!=";
1703 case BO_And: return "&";
1704 case BO_Xor: return "^";
1705 case BO_Or: return "|";
1706 case BO_LAnd: return "&&";
1707 case BO_LOr: return "||";
1708 case BO_Assign: return "=";
1709 case BO_MulAssign: return "*=";
1710 case BO_DivAssign: return "/=";
1711 case BO_RemAssign: return "%=";
1712 case BO_AddAssign: return "+=";
1713 case BO_SubAssign: return "-=";
1714 case BO_ShlAssign: return "<<=";
1715 case BO_ShrAssign: return ">>=";
1716 case BO_AndAssign: return "&=";
1717 case BO_XorAssign: return "^=";
1718 case BO_OrAssign: return "|=";
1719 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001720 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001721
David Blaikie30263482012-01-20 21:50:17 +00001722 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001723}
1724
John McCall2de56d12010-08-25 11:45:40 +00001725BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001726BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1727 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001728 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001729 case OO_Plus: return BO_Add;
1730 case OO_Minus: return BO_Sub;
1731 case OO_Star: return BO_Mul;
1732 case OO_Slash: return BO_Div;
1733 case OO_Percent: return BO_Rem;
1734 case OO_Caret: return BO_Xor;
1735 case OO_Amp: return BO_And;
1736 case OO_Pipe: return BO_Or;
1737 case OO_Equal: return BO_Assign;
1738 case OO_Less: return BO_LT;
1739 case OO_Greater: return BO_GT;
1740 case OO_PlusEqual: return BO_AddAssign;
1741 case OO_MinusEqual: return BO_SubAssign;
1742 case OO_StarEqual: return BO_MulAssign;
1743 case OO_SlashEqual: return BO_DivAssign;
1744 case OO_PercentEqual: return BO_RemAssign;
1745 case OO_CaretEqual: return BO_XorAssign;
1746 case OO_AmpEqual: return BO_AndAssign;
1747 case OO_PipeEqual: return BO_OrAssign;
1748 case OO_LessLess: return BO_Shl;
1749 case OO_GreaterGreater: return BO_Shr;
1750 case OO_LessLessEqual: return BO_ShlAssign;
1751 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1752 case OO_EqualEqual: return BO_EQ;
1753 case OO_ExclaimEqual: return BO_NE;
1754 case OO_LessEqual: return BO_LE;
1755 case OO_GreaterEqual: return BO_GE;
1756 case OO_AmpAmp: return BO_LAnd;
1757 case OO_PipePipe: return BO_LOr;
1758 case OO_Comma: return BO_Comma;
1759 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001760 }
1761}
1762
1763OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1764 static const OverloadedOperatorKind OverOps[] = {
1765 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1766 OO_Star, OO_Slash, OO_Percent,
1767 OO_Plus, OO_Minus,
1768 OO_LessLess, OO_GreaterGreater,
1769 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1770 OO_EqualEqual, OO_ExclaimEqual,
1771 OO_Amp,
1772 OO_Caret,
1773 OO_Pipe,
1774 OO_AmpAmp,
1775 OO_PipePipe,
1776 OO_Equal, OO_StarEqual,
1777 OO_SlashEqual, OO_PercentEqual,
1778 OO_PlusEqual, OO_MinusEqual,
1779 OO_LessLessEqual, OO_GreaterGreaterEqual,
1780 OO_AmpEqual, OO_CaretEqual,
1781 OO_PipeEqual,
1782 OO_Comma
1783 };
1784 return OverOps[Opc];
1785}
1786
Craig Topper05ed1a02013-08-18 10:09:15 +00001787InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001788 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001789 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001790 false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001791 InitExprs(C, initExprs.size()),
Abramo Bagnara23700f02012-11-08 18:41:43 +00001792 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001793{
1794 sawArrayRangeDesignator(false);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001795 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001796 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001797 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001798 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001799 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001800 if (initExprs[I]->isInstantiationDependent())
1801 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001802 if (initExprs[I]->containsUnexpandedParameterPack())
1803 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001804 }
Sean Huntc3021132010-05-05 15:23:54 +00001805
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001806 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001807}
Reid Spencer5f016e22007-07-11 17:01:13 +00001808
Craig Topper05ed1a02013-08-18 10:09:15 +00001809void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001810 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001811 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001812}
1813
Craig Topper05ed1a02013-08-18 10:09:15 +00001814void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001815 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001816}
1817
Craig Topper05ed1a02013-08-18 10:09:15 +00001818Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001819 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001820 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001821 InitExprs.back() = expr;
1822 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001823 }
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Douglas Gregor4c678342009-01-28 21:54:33 +00001825 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1826 InitExprs[Init] = expr;
1827 return Result;
1828}
1829
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001830void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001831 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001832 ArrayFillerOrUnionFieldInit = filler;
1833 // Fill out any "holes" in the array due to designated initializers.
1834 Expr **inits = getInits();
1835 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1836 if (inits[i] == 0)
1837 inits[i] = filler;
1838}
1839
Richard Smithfe587202012-04-15 02:50:59 +00001840bool InitListExpr::isStringLiteralInit() const {
1841 if (getNumInits() != 1)
1842 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001843 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1844 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smithfe587202012-04-15 02:50:59 +00001845 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001846 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smithfe587202012-04-15 02:50:59 +00001847 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1848}
1849
Erik Verbruggen65d78312012-12-25 14:51:39 +00001850SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara23700f02012-11-08 18:41:43 +00001851 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen65d78312012-12-25 14:51:39 +00001852 return SyntacticForm->getLocStart();
1853 SourceLocation Beg = LBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001854 if (Beg.isInvalid()) {
1855 // Find the first non-null initializer.
1856 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1857 E = InitExprs.end();
1858 I != E; ++I) {
1859 if (Stmt *S = *I) {
1860 Beg = S->getLocStart();
1861 break;
1862 }
1863 }
1864 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001865 return Beg;
1866}
1867
1868SourceLocation InitListExpr::getLocEnd() const {
1869 if (InitListExpr *SyntacticForm = getSyntacticForm())
1870 return SyntacticForm->getLocEnd();
1871 SourceLocation End = RBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001872 if (End.isInvalid()) {
1873 // Find the first non-null initializer from the end.
1874 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen65d78312012-12-25 14:51:39 +00001875 E = InitExprs.rend();
1876 I != E; ++I) {
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001877 if (Stmt *S = *I) {
Erik Verbruggen65d78312012-12-25 14:51:39 +00001878 End = S->getLocEnd();
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001879 break;
Erik Verbruggen65d78312012-12-25 14:51:39 +00001880 }
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001881 }
1882 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001883 return End;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001884}
1885
Steve Naroffbfdcae62008-09-04 15:31:07 +00001886/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001887///
John McCalla345edb2012-02-17 03:32:35 +00001888const FunctionProtoType *BlockExpr::getFunctionType() const {
1889 // The block pointer is never sugared, but the function type might be.
1890 return cast<BlockPointerType>(getType())
1891 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001892}
1893
Mike Stump1eb44332009-09-09 15:08:12 +00001894SourceLocation BlockExpr::getCaretLocation() const {
1895 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001896}
Mike Stump1eb44332009-09-09 15:08:12 +00001897const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001898 return TheBlock->getBody();
1899}
Mike Stump1eb44332009-09-09 15:08:12 +00001900Stmt *BlockExpr::getBody() {
1901 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001902}
Steve Naroff56ee6892008-10-08 17:01:13 +00001903
1904
Reid Spencer5f016e22007-07-11 17:01:13 +00001905//===----------------------------------------------------------------------===//
1906// Generic Expression Routines
1907//===----------------------------------------------------------------------===//
1908
Chris Lattner026dc962009-02-14 07:37:35 +00001909/// isUnusedResultAWarning - Return true if this immediate expression should
1910/// be warned about if the result is unused. If so, fill in Loc and Ranges
1911/// with location to warn on and the source range[s] to report with the
1912/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001913bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1914 SourceRange &R1, SourceRange &R2,
1915 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001916 // Don't warn if the expr is type dependent. The type could end up
1917 // instantiating to void.
1918 if (isTypeDependent())
1919 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 switch (getStmtClass()) {
1922 default:
John McCall0faede62010-03-12 07:11:26 +00001923 if (getType()->isVoidType())
1924 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001925 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001926 Loc = getExprLoc();
1927 R1 = getSourceRange();
1928 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001929 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001930 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001931 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001932 case GenericSelectionExprClass:
1933 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001934 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmana5e66012013-07-20 00:40:58 +00001935 case ChooseExprClass:
1936 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1937 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 case UnaryOperatorClass: {
1939 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Reid Spencer5f016e22007-07-11 17:01:13 +00001941 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001942 case UO_Plus:
1943 case UO_Minus:
1944 case UO_AddrOf:
1945 case UO_Not:
1946 case UO_LNot:
1947 case UO_Deref:
1948 break;
John McCall2de56d12010-08-25 11:45:40 +00001949 case UO_PostInc:
1950 case UO_PostDec:
1951 case UO_PreInc:
1952 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001953 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001954 case UO_Real:
1955 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001956 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001957 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1958 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001959 return false;
1960 break;
John McCall2de56d12010-08-25 11:45:40 +00001961 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001962 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001963 }
Eli Friedmana6115062012-05-24 00:47:05 +00001964 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001965 Loc = UO->getOperatorLoc();
1966 R1 = UO->getSubExpr()->getSourceRange();
1967 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001968 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001969 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001970 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001971 switch (BO->getOpcode()) {
1972 default:
1973 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001974 // Consider the RHS of comma for side effects. LHS was checked by
1975 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001976 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001977 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1978 // lvalue-ness) of an assignment written in a macro.
1979 if (IntegerLiteral *IE =
1980 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1981 if (IE->getValue() == 0)
1982 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001983 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001984 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001985 case BO_LAnd:
1986 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001987 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1988 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001989 return false;
1990 break;
John McCallbf0ee352010-02-16 04:10:53 +00001991 }
Chris Lattner026dc962009-02-14 07:37:35 +00001992 if (BO->isAssignmentOp())
1993 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001994 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001995 Loc = BO->getOperatorLoc();
1996 R1 = BO->getLHS()->getSourceRange();
1997 R2 = BO->getRHS()->getSourceRange();
1998 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001999 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00002000 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00002001 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002002 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002003 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002004
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002005 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002006 // If only one of the LHS or RHS is a warning, the operator might
2007 // be being used for control flow. Only warn if both the LHS and
2008 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002009 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002010 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002011 return false;
2012 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00002013 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00002014 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002015 }
2016
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002018 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002019 Loc = cast<MemberExpr>(this)->getMemberLoc();
2020 R1 = SourceRange(Loc, Loc);
2021 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2022 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Reid Spencer5f016e22007-07-11 17:01:13 +00002024 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002025 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002026 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2027 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2028 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2029 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00002030
Chandler Carruth9b106832011-08-17 09:49:44 +00002031 case CXXOperatorCallExprClass: {
2032 // We warn about operator== and operator!= even when user-defined operator
2033 // overloads as there is no reasonable way to define these such that they
2034 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2035 // warning: these operators are commonly typo'ed, and so warning on them
2036 // provides additional value as well. If this list is updated,
2037 // DiagnoseUnusedComparison should be as well.
2038 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2039 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002040 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00002041 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002042 Loc = Op->getOperatorLoc();
2043 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00002044 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002045 }
Chandler Carruth9b106832011-08-17 09:49:44 +00002046
2047 // Fallthrough for generic call handling.
2048 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002049 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002050 case CXXMemberCallExprClass:
2051 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002052 // If this is a direct call, get the callee.
2053 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00002054 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00002055 // If the callee has attribute pure, const, or warn_unused_result, warn
2056 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002057 //
2058 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2059 // updated to match for QoI.
2060 if (FD->getAttr<WarnUnusedResultAttr>() ||
2061 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002062 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002063 Loc = CE->getCallee()->getLocStart();
2064 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002066 if (unsigned NumArgs = CE->getNumArgs())
2067 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2068 CE->getArg(NumArgs-1)->getLocEnd());
2069 return true;
2070 }
Chris Lattner026dc962009-02-14 07:37:35 +00002071 }
2072 return false;
2073 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002074
Matt Beaumont-Gay84c3b972012-10-23 06:15:26 +00002075 // If we don't know precisely what we're looking at, let's not warn.
2076 case UnresolvedLookupExprClass:
2077 case CXXUnresolvedConstructExprClass:
2078 return false;
2079
Anders Carlsson58beed92009-11-17 17:11:23 +00002080 case CXXTemporaryObjectExprClass:
Lubos Lunak81e45492013-07-21 13:15:58 +00002081 case CXXConstructExprClass: {
2082 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2083 if (Type->hasAttr<WarnUnusedAttr>()) {
2084 WarnE = this;
2085 Loc = getLocStart();
2086 R1 = getSourceRange();
2087 return true;
2088 }
2089 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002090 return false;
Lubos Lunak81e45492013-07-21 13:15:58 +00002091 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002092
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002093 case ObjCMessageExprClass: {
2094 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00002095 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002096 ME->isInstanceMessage() &&
2097 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas4bdb6022013-07-19 20:25:56 +00002098 ME->getMethodFamily() == OMF_init) {
Eli Friedmana6115062012-05-24 00:47:05 +00002099 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00002100 Loc = getExprLoc();
2101 R1 = ME->getSourceRange();
2102 return true;
2103 }
2104
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002105 const ObjCMethodDecl *MD = ME->getMethodDecl();
2106 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002107 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002108 Loc = getExprLoc();
2109 return true;
2110 }
Chris Lattner026dc962009-02-14 07:37:35 +00002111 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002112 }
Mike Stump1eb44332009-09-09 15:08:12 +00002113
John McCall12f78a62010-12-02 01:19:52 +00002114 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002115 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00002116 Loc = getExprLoc();
2117 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00002118 return true;
John McCall12f78a62010-12-02 01:19:52 +00002119
John McCall4b9c2d22011-11-06 09:01:30 +00002120 case PseudoObjectExprClass: {
2121 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2122
2123 // Only complain about things that have the form of a getter.
2124 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2125 isa<BinaryOperator>(PO->getSyntacticForm()))
2126 return false;
2127
Eli Friedmana6115062012-05-24 00:47:05 +00002128 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00002129 Loc = getExprLoc();
2130 R1 = getSourceRange();
2131 return true;
2132 }
2133
Chris Lattner611b2ec2008-07-26 19:51:01 +00002134 case StmtExprClass: {
2135 // Statement exprs don't logically have side effects themselves, but are
2136 // sometimes used in macros in ways that give them a type that is unused.
2137 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2138 // however, if the result of the stmt expr is dead, we don't want to emit a
2139 // warning.
2140 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002141 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002142 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002143 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002144 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2145 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002146 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002147 }
Mike Stump1eb44332009-09-09 15:08:12 +00002148
John McCall0faede62010-03-12 07:11:26 +00002149 if (getType()->isVoidType())
2150 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002151 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002152 Loc = cast<StmtExpr>(this)->getLParenLoc();
2153 R1 = getSourceRange();
2154 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002155 }
Eli Friedman63199172012-09-24 23:02:26 +00002156 case CXXFunctionalCastExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002157 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002158 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002159 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002160 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002161 if (CE->getCastKind() == CK_ToVoid) {
2162 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002163 CE->getSubExpr()->getType().isVolatileQualified()) {
2164 const DeclRefExpr *DRE =
2165 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2166 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2167 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2168 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2169 R1, R2, Ctx);
2170 }
2171 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002172 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002173 }
Eli Friedman4059da82012-05-24 21:05:41 +00002174
Eli Friedmana6115062012-05-24 00:47:05 +00002175 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002176 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002177 if (CE->getCastKind() == CK_ConstructorConversion)
2178 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002179
Eli Friedmana6115062012-05-24 00:47:05 +00002180 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002181 if (const CXXFunctionalCastExpr *CXXCE =
2182 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedmancdd4b782013-08-15 22:02:56 +00002183 Loc = CXXCE->getLocStart();
Eli Friedman4059da82012-05-24 21:05:41 +00002184 R1 = CXXCE->getSubExpr()->getSourceRange();
2185 } else {
2186 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2187 Loc = CStyleCE->getLParenLoc();
2188 R1 = CStyleCE->getSubExpr()->getSourceRange();
2189 }
Chris Lattner026dc962009-02-14 07:37:35 +00002190 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002191 }
Eli Friedmana6115062012-05-24 00:47:05 +00002192 case ImplicitCastExprClass: {
2193 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002194
Eli Friedmana6115062012-05-24 00:47:05 +00002195 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2196 if (ICE->getCastKind() == CK_LValueToRValue &&
2197 ICE->getSubExpr()->getType().isVolatileQualified())
2198 return false;
2199
2200 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2201 }
Chris Lattner04421082008-04-08 04:40:51 +00002202 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002203 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002204 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smithc3bf52c2013-04-20 22:23:05 +00002205 case CXXDefaultInitExprClass:
2206 return (cast<CXXDefaultInitExpr>(this)
2207 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002208
2209 case CXXNewExprClass:
2210 // FIXME: In theory, there might be new expressions that don't have side
2211 // effects (e.g. a placement new with an uninitialized POD).
2212 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002213 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002214 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002215 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002216 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002217 case ExprWithCleanupsClass:
2218 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002219 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002220 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002221}
2222
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002223/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002224/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002225bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002226 const Expr *E = IgnoreParens();
2227 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002228 default:
2229 return false;
2230 case ObjCIvarRefExprClass:
2231 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002232 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002233 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002234 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002235 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002236 case MaterializeTemporaryExprClass:
2237 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2238 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002239 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002240 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002241 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002242 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002243
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002244 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2245 if (VD->hasGlobalStorage())
2246 return true;
2247 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002248 // dereferencing to a pointer is always a gc'able candidate,
2249 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002250 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002251 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002252 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002253 return false;
2254 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002255 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002256 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002257 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002258 }
2259 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002260 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002261 }
2262}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002263
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002264bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2265 if (isTypeDependent())
2266 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002267 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002268}
2269
John McCall864c0412011-04-26 20:42:42 +00002270QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002271 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002272
2273 // Bound member expressions are always one of these possibilities:
2274 // x->m x.m x->*y x.*y
2275 // (possibly parenthesized)
2276
2277 expr = expr->IgnoreParens();
2278 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2279 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2280 return mem->getMemberDecl()->getType();
2281 }
2282
2283 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2284 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2285 ->getPointeeType();
2286 assert(type->isFunctionType());
2287 return type;
2288 }
2289
2290 assert(isa<UnresolvedMemberExpr>(expr));
2291 return QualType();
2292}
2293
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002294Expr* Expr::IgnoreParens() {
2295 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002296 while (true) {
2297 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2298 E = P->getSubExpr();
2299 continue;
2300 }
2301 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2302 if (P->getOpcode() == UO_Extension) {
2303 E = P->getSubExpr();
2304 continue;
2305 }
2306 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002307 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2308 if (!P->isResultDependent()) {
2309 E = P->getResultExpr();
2310 continue;
2311 }
2312 }
Eli Friedmana5e66012013-07-20 00:40:58 +00002313 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2314 if (!P->isConditionDependent()) {
2315 E = P->getChosenSubExpr();
2316 continue;
2317 }
2318 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002319 return E;
2320 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002321}
2322
Chris Lattner56f34942008-02-13 01:02:39 +00002323/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2324/// or CastExprs or ImplicitCastExprs, returning their operand.
2325Expr *Expr::IgnoreParenCasts() {
2326 Expr *E = this;
2327 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002328 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002329 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002330 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002331 continue;
2332 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002333 if (MaterializeTemporaryExpr *Materialize
2334 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2335 E = Materialize->GetTemporaryExpr();
2336 continue;
2337 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002338 if (SubstNonTypeTemplateParmExpr *NTTP
2339 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2340 E = NTTP->getReplacement();
2341 continue;
2342 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002343 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002344 }
2345}
2346
John McCall9c5d70c2010-12-04 08:24:19 +00002347/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2348/// casts. This is intended purely as a temporary workaround for code
2349/// that hasn't yet been rewritten to do the right thing about those
2350/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002351Expr *Expr::IgnoreParenLValueCasts() {
2352 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002353 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002354 E = E->IgnoreParens();
2355 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002356 if (P->getCastKind() == CK_LValueToRValue) {
2357 E = P->getSubExpr();
2358 continue;
2359 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002360 } else if (MaterializeTemporaryExpr *Materialize
2361 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2362 E = Materialize->GetTemporaryExpr();
2363 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002364 } else if (SubstNonTypeTemplateParmExpr *NTTP
2365 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2366 E = NTTP->getReplacement();
2367 continue;
John McCallf6a16482010-12-04 03:47:34 +00002368 }
2369 break;
2370 }
2371 return E;
2372}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002373
2374Expr *Expr::ignoreParenBaseCasts() {
2375 Expr *E = this;
2376 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002377 E = E->IgnoreParens();
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002378 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2379 if (CE->getCastKind() == CK_DerivedToBase ||
2380 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2381 CE->getCastKind() == CK_NoOp) {
2382 E = CE->getSubExpr();
2383 continue;
2384 }
2385 }
2386
2387 return E;
2388 }
2389}
2390
John McCall2fc46bf2010-05-05 22:59:52 +00002391Expr *Expr::IgnoreParenImpCasts() {
2392 Expr *E = this;
2393 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002394 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002395 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002396 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002397 continue;
2398 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002399 if (MaterializeTemporaryExpr *Materialize
2400 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2401 E = Materialize->GetTemporaryExpr();
2402 continue;
2403 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002404 if (SubstNonTypeTemplateParmExpr *NTTP
2405 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2406 E = NTTP->getReplacement();
2407 continue;
2408 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002409 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002410 }
2411}
2412
Hans Wennborg2f072b42011-06-09 17:06:51 +00002413Expr *Expr::IgnoreConversionOperator() {
2414 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002415 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002416 return MCE->getImplicitObjectArgument();
2417 }
2418 return this;
2419}
2420
Chris Lattnerecdd8412009-03-13 17:28:01 +00002421/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2422/// value (including ptr->int casts of the same size). Strip off any
2423/// ParenExpr or CastExprs, returning their operand.
2424Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2425 Expr *E = this;
2426 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002427 E = E->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +00002428
Chris Lattnerecdd8412009-03-13 17:28:01 +00002429 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2430 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002431 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002432 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Chris Lattnerecdd8412009-03-13 17:28:01 +00002434 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2435 E = SE;
2436 continue;
2437 }
Mike Stump1eb44332009-09-09 15:08:12 +00002438
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002439 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002440 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002441 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002442 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002443 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2444 E = SE;
2445 continue;
2446 }
2447 }
Mike Stump1eb44332009-09-09 15:08:12 +00002448
Douglas Gregorc0244c52011-09-08 17:56:33 +00002449 if (SubstNonTypeTemplateParmExpr *NTTP
2450 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2451 E = NTTP->getReplacement();
2452 continue;
2453 }
2454
Chris Lattnerecdd8412009-03-13 17:28:01 +00002455 return E;
2456 }
2457}
2458
Douglas Gregor6eef5192009-12-14 19:27:10 +00002459bool Expr::isDefaultArgument() const {
2460 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002461 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2462 E = M->GetTemporaryExpr();
2463
Douglas Gregor6eef5192009-12-14 19:27:10 +00002464 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2465 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002466
Douglas Gregor6eef5192009-12-14 19:27:10 +00002467 return isa<CXXDefaultArgExpr>(E);
2468}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002469
Douglas Gregor2f599792010-04-02 18:24:57 +00002470/// \brief Skip over any no-op casts and any temporary-binding
2471/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002472static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002473 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2474 E = M->GetTemporaryExpr();
2475
Douglas Gregor2f599792010-04-02 18:24:57 +00002476 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002477 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002478 E = ICE->getSubExpr();
2479 else
2480 break;
2481 }
2482
2483 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2484 E = BE->getSubExpr();
2485
2486 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002487 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002488 E = ICE->getSubExpr();
2489 else
2490 break;
2491 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002492
2493 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002494}
2495
John McCall558d2ab2010-09-15 10:14:12 +00002496/// isTemporaryObject - Determines if this expression produces a
2497/// temporary of the given class type.
2498bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2499 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2500 return false;
2501
Anders Carlssonf8b30152010-11-28 16:40:49 +00002502 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002503
John McCall58277b52010-09-15 20:59:13 +00002504 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002505 if (!E->Classify(C).isPRValue()) {
2506 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002507 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002508 return false;
2509 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002510
John McCall19e60ad2010-09-16 06:57:56 +00002511 // Black-list a few cases which yield pr-values of class type that don't
2512 // refer to temporaries of that type:
2513
2514 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002515 if (isa<ImplicitCastExpr>(E)) {
2516 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2517 case CK_DerivedToBase:
2518 case CK_UncheckedDerivedToBase:
2519 return false;
2520 default:
2521 break;
2522 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002523 }
2524
John McCall19e60ad2010-09-16 06:57:56 +00002525 // - member expressions (all)
2526 if (isa<MemberExpr>(E))
2527 return false;
2528
Eli Friedman32f498a2012-06-15 23:51:06 +00002529 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2530 if (BO->isPtrMemOp())
2531 return false;
2532
John McCall56ca35d2011-02-17 10:25:35 +00002533 // - opaque values (all)
2534 if (isa<OpaqueValueExpr>(E))
2535 return false;
2536
John McCall558d2ab2010-09-15 10:14:12 +00002537 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002538}
2539
Douglas Gregor75e85042011-03-02 21:06:53 +00002540bool Expr::isImplicitCXXThis() const {
2541 const Expr *E = this;
2542
2543 // Strip away parentheses and casts we don't care about.
2544 while (true) {
2545 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2546 E = Paren->getSubExpr();
2547 continue;
2548 }
2549
2550 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2551 if (ICE->getCastKind() == CK_NoOp ||
2552 ICE->getCastKind() == CK_LValueToRValue ||
2553 ICE->getCastKind() == CK_DerivedToBase ||
2554 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2555 E = ICE->getSubExpr();
2556 continue;
2557 }
2558 }
2559
2560 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2561 if (UnOp->getOpcode() == UO_Extension) {
2562 E = UnOp->getSubExpr();
2563 continue;
2564 }
2565 }
2566
Douglas Gregor03e80032011-06-21 17:03:29 +00002567 if (const MaterializeTemporaryExpr *M
2568 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2569 E = M->GetTemporaryExpr();
2570 continue;
2571 }
2572
Douglas Gregor75e85042011-03-02 21:06:53 +00002573 break;
2574 }
2575
2576 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2577 return This->isImplicit();
2578
2579 return false;
2580}
2581
Douglas Gregor898574e2008-12-05 23:32:09 +00002582/// hasAnyTypeDependentArguments - Determines if any of the expressions
2583/// in Exprs is type-dependent.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002584bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00002585 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002586 if (Exprs[I]->isTypeDependent())
2587 return true;
2588
2589 return false;
2590}
2591
John McCall4204f072010-08-02 21:13:48 +00002592bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002593 // This function is attempting whether an expression is an initializer
Eli Friedman21cde052013-07-16 22:40:53 +00002594 // which can be evaluated at compile-time. It very closely parallels
2595 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2596 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2597 // to isEvaluatable most of the time.
2598 //
John McCall4204f072010-08-02 21:13:48 +00002599 // If we ever capture reference-binding directly in the AST, we can
2600 // kill the second parameter.
2601
2602 if (IsForRef) {
2603 EvalResult Result;
2604 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2605 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002606
Anders Carlssone8a32b82008-11-24 05:23:59 +00002607 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002608 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002609 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002610 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002611 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002612 case CXXTemporaryObjectExprClass:
2613 case CXXConstructExprClass: {
2614 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002615
Eli Friedman21cde052013-07-16 22:40:53 +00002616 if (CE->getConstructor()->isTrivial() &&
2617 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2618 // Trivial default constructor
Richard Smith180f4792011-11-10 06:34:14 +00002619 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002620
Eli Friedman21cde052013-07-16 22:40:53 +00002621 // Trivial copy constructor
2622 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2623 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smith180f4792011-11-10 06:34:14 +00002624 }
2625
Richard Smith180f4792011-11-10 06:34:14 +00002626 break;
John McCallb4b9b152010-08-01 21:51:45 +00002627 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002628 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002629 // This handles gcc's extension that allows global initializers like
2630 // "struct x {int x;} x = (struct x) {};".
2631 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002632 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002633 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002634 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002635 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002636 // FIXME: This doesn't deal with fields with reference types correctly.
2637 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2638 // to bitfields.
Eli Friedman21cde052013-07-16 22:40:53 +00002639 const InitListExpr *ILE = cast<InitListExpr>(this);
2640 if (ILE->getType()->isArrayType()) {
2641 unsigned numInits = ILE->getNumInits();
2642 for (unsigned i = 0; i < numInits; i++) {
2643 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2644 return false;
2645 }
2646 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002647 }
Eli Friedman21cde052013-07-16 22:40:53 +00002648
2649 if (ILE->getType()->isRecordType()) {
2650 unsigned ElementNo = 0;
2651 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2652 for (RecordDecl::field_iterator Field = RD->field_begin(),
2653 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2654 // If this is a union, skip all the fields that aren't being initialized.
2655 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2656 continue;
2657
2658 // Don't emit anonymous bitfields, they just affect layout.
2659 if (Field->isUnnamedBitfield())
2660 continue;
2661
2662 if (ElementNo < ILE->getNumInits()) {
2663 const Expr *Elt = ILE->getInit(ElementNo++);
2664 if (Field->isBitField()) {
2665 // Bitfields have to evaluate to an integer.
2666 llvm::APSInt ResultTmp;
2667 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2668 return false;
2669 } else {
2670 bool RefType = Field->getType()->isReferenceType();
2671 if (!Elt->isConstantInitializer(Ctx, RefType))
2672 return false;
2673 }
2674 }
2675 }
2676 return true;
2677 }
2678
2679 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002680 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002681 case ImplicitValueInitExprClass:
2682 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002683 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002684 return cast<ParenExpr>(this)->getSubExpr()
2685 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002686 case GenericSelectionExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002687 return cast<GenericSelectionExpr>(this)->getResultExpr()
2688 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002689 case ChooseExprClass:
Eli Friedmana5e66012013-07-20 00:40:58 +00002690 if (cast<ChooseExpr>(this)->isConditionDependent())
2691 return false;
2692 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002693 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002694 case UnaryOperatorClass: {
2695 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002696 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002697 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002698 break;
2699 }
John McCall4204f072010-08-02 21:13:48 +00002700 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002701 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002702 case ImplicitCastExprClass:
Eli Friedman21cde052013-07-16 22:40:53 +00002703 case CStyleCastExprClass:
2704 case ObjCBridgedCastExprClass:
2705 case CXXDynamicCastExprClass:
2706 case CXXReinterpretCastExprClass:
2707 case CXXConstCastExprClass: {
Richard Smithd62ca372011-12-06 22:44:34 +00002708 const CastExpr *CE = cast<CastExpr>(this);
2709
Eli Friedman6bd97192011-12-21 00:43:02 +00002710 // Handle misc casts we want to ignore.
Eli Friedman6bd97192011-12-21 00:43:02 +00002711 if (CE->getCastKind() == CK_NoOp ||
2712 CE->getCastKind() == CK_LValueToRValue ||
2713 CE->getCastKind() == CK_ToUnion ||
Eli Friedman21cde052013-07-16 22:40:53 +00002714 CE->getCastKind() == CK_ConstructorConversion ||
2715 CE->getCastKind() == CK_NonAtomicToAtomic ||
2716 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smithd62ca372011-12-06 22:44:34 +00002717 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2718
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002719 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002720 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002721 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002722 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002723 ->isConstantInitializer(Ctx, false);
Eli Friedman21cde052013-07-16 22:40:53 +00002724
2725 case SubstNonTypeTemplateParmExprClass:
2726 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2727 ->isConstantInitializer(Ctx, false);
2728 case CXXDefaultArgExprClass:
2729 return cast<CXXDefaultArgExpr>(this)->getExpr()
2730 ->isConstantInitializer(Ctx, false);
2731 case CXXDefaultInitExprClass:
2732 return cast<CXXDefaultInitExpr>(this)->getExpr()
2733 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002734 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002735 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002736}
2737
Richard Smith8ae4ec22012-08-07 04:16:51 +00002738bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2739 if (isInstantiationDependent())
2740 return true;
2741
2742 switch (getStmtClass()) {
2743 case NoStmtClass:
2744 #define ABSTRACT_STMT(Type)
2745 #define STMT(Type, Base) case Type##Class:
2746 #define EXPR(Type, Base)
2747 #include "clang/AST/StmtNodes.inc"
2748 llvm_unreachable("unexpected Expr kind");
2749
2750 case DependentScopeDeclRefExprClass:
2751 case CXXUnresolvedConstructExprClass:
2752 case CXXDependentScopeMemberExprClass:
2753 case UnresolvedLookupExprClass:
2754 case UnresolvedMemberExprClass:
2755 case PackExpansionExprClass:
2756 case SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +00002757 case FunctionParmPackExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002758 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2759
Richard Smith60b70382012-08-07 05:18:29 +00002760 case DeclRefExprClass:
2761 case ObjCIvarRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002762 case PredefinedExprClass:
2763 case IntegerLiteralClass:
2764 case FloatingLiteralClass:
2765 case ImaginaryLiteralClass:
2766 case StringLiteralClass:
2767 case CharacterLiteralClass:
2768 case OffsetOfExprClass:
2769 case ImplicitValueInitExprClass:
2770 case UnaryExprOrTypeTraitExprClass:
2771 case AddrLabelExprClass:
2772 case GNUNullExprClass:
2773 case CXXBoolLiteralExprClass:
2774 case CXXNullPtrLiteralExprClass:
2775 case CXXThisExprClass:
2776 case CXXScalarValueInitExprClass:
2777 case TypeTraitExprClass:
2778 case UnaryTypeTraitExprClass:
2779 case BinaryTypeTraitExprClass:
2780 case ArrayTypeTraitExprClass:
2781 case ExpressionTraitExprClass:
2782 case CXXNoexceptExprClass:
2783 case SizeOfPackExprClass:
2784 case ObjCStringLiteralClass:
2785 case ObjCEncodeExprClass:
2786 case ObjCBoolLiteralExprClass:
2787 case CXXUuidofExprClass:
2788 case OpaqueValueExprClass:
2789 // These never have a side-effect.
2790 return false;
2791
2792 case CallExprClass:
John McCall76da55d2013-04-16 07:28:30 +00002793 case MSPropertyRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002794 case CompoundAssignOperatorClass:
2795 case VAArgExprClass:
2796 case AtomicExprClass:
2797 case StmtExprClass:
2798 case CXXOperatorCallExprClass:
2799 case CXXMemberCallExprClass:
2800 case UserDefinedLiteralClass:
2801 case CXXThrowExprClass:
2802 case CXXNewExprClass:
2803 case CXXDeleteExprClass:
2804 case ExprWithCleanupsClass:
2805 case CXXBindTemporaryExprClass:
2806 case BlockExprClass:
2807 case CUDAKernelCallExprClass:
2808 // These always have a side-effect.
2809 return true;
2810
2811 case ParenExprClass:
2812 case ArraySubscriptExprClass:
2813 case MemberExprClass:
2814 case ConditionalOperatorClass:
2815 case BinaryConditionalOperatorClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002816 case CompoundLiteralExprClass:
2817 case ExtVectorElementExprClass:
2818 case DesignatedInitExprClass:
2819 case ParenListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002820 case CXXPseudoDestructorExprClass:
Richard Smith7c3e6152013-06-12 22:31:48 +00002821 case CXXStdInitializerListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002822 case SubstNonTypeTemplateParmExprClass:
2823 case MaterializeTemporaryExprClass:
2824 case ShuffleVectorExprClass:
2825 case AsTypeExprClass:
2826 // These have a side-effect if any subexpression does.
2827 break;
2828
Richard Smith60b70382012-08-07 05:18:29 +00002829 case UnaryOperatorClass:
2830 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002831 return true;
2832 break;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002833
2834 case BinaryOperatorClass:
2835 if (cast<BinaryOperator>(this)->isAssignmentOp())
2836 return true;
2837 break;
2838
Richard Smith8ae4ec22012-08-07 04:16:51 +00002839 case InitListExprClass:
2840 // FIXME: The children for an InitListExpr doesn't include the array filler.
2841 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2842 if (E->HasSideEffects(Ctx))
2843 return true;
2844 break;
2845
2846 case GenericSelectionExprClass:
2847 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2848 HasSideEffects(Ctx);
2849
2850 case ChooseExprClass:
Eli Friedmana5e66012013-07-20 00:40:58 +00002851 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith8ae4ec22012-08-07 04:16:51 +00002852
2853 case CXXDefaultArgExprClass:
2854 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2855
Richard Smithc3bf52c2013-04-20 22:23:05 +00002856 case CXXDefaultInitExprClass:
2857 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2858 return E->HasSideEffects(Ctx);
2859 // If we've not yet parsed the initializer, assume it has side-effects.
2860 return true;
2861
Richard Smith8ae4ec22012-08-07 04:16:51 +00002862 case CXXDynamicCastExprClass: {
2863 // A dynamic_cast expression has side-effects if it can throw.
2864 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2865 if (DCE->getTypeAsWritten()->isReferenceType() &&
2866 DCE->getCastKind() == CK_Dynamic)
2867 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002868 } // Fall through.
2869 case ImplicitCastExprClass:
2870 case CStyleCastExprClass:
2871 case CXXStaticCastExprClass:
2872 case CXXReinterpretCastExprClass:
2873 case CXXConstCastExprClass:
2874 case CXXFunctionalCastExprClass: {
2875 const CastExpr *CE = cast<CastExpr>(this);
2876 if (CE->getCastKind() == CK_LValueToRValue &&
2877 CE->getSubExpr()->getType().isVolatileQualified())
2878 return true;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002879 break;
2880 }
2881
Richard Smith0d729102012-08-13 20:08:14 +00002882 case CXXTypeidExprClass:
2883 // typeid might throw if its subexpression is potentially-evaluated, so has
2884 // side-effects in that case whether or not its subexpression does.
2885 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith8ae4ec22012-08-07 04:16:51 +00002886
2887 case CXXConstructExprClass:
2888 case CXXTemporaryObjectExprClass: {
2889 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smith60b70382012-08-07 05:18:29 +00002890 if (!CE->getConstructor()->isTrivial())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002891 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002892 // A trivial constructor does not add any side-effects of its own. Just look
2893 // at its arguments.
Richard Smith8ae4ec22012-08-07 04:16:51 +00002894 break;
2895 }
2896
2897 case LambdaExprClass: {
2898 const LambdaExpr *LE = cast<LambdaExpr>(this);
2899 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2900 E = LE->capture_end(); I != E; ++I)
2901 if (I->getCaptureKind() == LCK_ByCopy)
2902 // FIXME: Only has a side-effect if the variable is volatile or if
2903 // the copy would invoke a non-trivial copy constructor.
2904 return true;
2905 return false;
2906 }
2907
2908 case PseudoObjectExprClass: {
2909 // Only look for side-effects in the semantic form, and look past
2910 // OpaqueValueExpr bindings in that form.
2911 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2912 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2913 E = PO->semantics_end();
2914 I != E; ++I) {
2915 const Expr *Subexpr = *I;
2916 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2917 Subexpr = OVE->getSourceExpr();
2918 if (Subexpr->HasSideEffects(Ctx))
2919 return true;
2920 }
2921 return false;
2922 }
2923
2924 case ObjCBoxedExprClass:
2925 case ObjCArrayLiteralClass:
2926 case ObjCDictionaryLiteralClass:
2927 case ObjCMessageExprClass:
2928 case ObjCSelectorExprClass:
2929 case ObjCProtocolExprClass:
2930 case ObjCPropertyRefExprClass:
2931 case ObjCIsaExprClass:
2932 case ObjCIndirectCopyRestoreExprClass:
2933 case ObjCSubscriptRefExprClass:
2934 case ObjCBridgedCastExprClass:
2935 // FIXME: Classify these cases better.
2936 return true;
2937 }
2938
2939 // Recurse to children.
2940 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2941 if (const Stmt *S = *SubStmts)
2942 if (cast<Expr>(S)->HasSideEffects(Ctx))
2943 return true;
2944
2945 return false;
2946}
2947
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002948namespace {
2949 /// \brief Look for a call to a non-trivial function within an expression.
2950 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2951 {
2952 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2953
2954 bool NonTrivial;
2955
2956 public:
2957 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002958 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002959
2960 bool hasNonTrivialCall() const { return NonTrivial; }
2961
2962 void VisitCallExpr(CallExpr *E) {
2963 if (CXXMethodDecl *Method
2964 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2965 if (Method->isTrivial()) {
2966 // Recurse to children of the call.
2967 Inherited::VisitStmt(E);
2968 return;
2969 }
2970 }
2971
2972 NonTrivial = true;
2973 }
2974
2975 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2976 if (E->getConstructor()->isTrivial()) {
2977 // Recurse to children of the call.
2978 Inherited::VisitStmt(E);
2979 return;
2980 }
2981
2982 NonTrivial = true;
2983 }
2984
2985 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2986 if (E->getTemporary()->getDestructor()->isTrivial()) {
2987 Inherited::VisitStmt(E);
2988 return;
2989 }
2990
2991 NonTrivial = true;
2992 }
2993 };
2994}
2995
2996bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2997 NonTrivialCallFinder Finder(Ctx);
2998 Finder.Visit(this);
2999 return Finder.hasNonTrivialCall();
3000}
3001
Chandler Carruth82214a82011-02-18 23:54:50 +00003002/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3003/// pointer constant or not, as well as the specific kind of constant detected.
3004/// Null pointer constants can be integer constant expressions with the
3005/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3006/// (a GNU extension).
3007Expr::NullPointerConstantKind
3008Expr::isNullPointerConstant(ASTContext &Ctx,
3009 NullPointerConstantValueDependence NPC) const {
Richard Smithf050d242013-06-13 02:46:14 +00003010 if (isValueDependent() && !Ctx.getLangOpts().CPlusPlus11) {
Douglas Gregorce940492009-09-25 04:25:58 +00003011 switch (NPC) {
3012 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00003013 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00003014 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003015 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie50800fc2012-08-08 17:33:31 +00003016 return NPCK_ZeroExpression;
Chandler Carruth82214a82011-02-18 23:54:50 +00003017 else
3018 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00003019
Douglas Gregorce940492009-09-25 04:25:58 +00003020 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003021 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00003022 }
3023 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00003024
Sebastian Redl07779722008-10-31 14:43:28 +00003025 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00003026 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003027 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00003028 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00003029 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00003030 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00003031 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00003032 Pointee->isVoidType() && // to void*
3033 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00003034 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00003035 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003036 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003037 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3038 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00003039 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00003040 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3041 // Accept ((void*)0) as a null pointer constant, as many other
3042 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00003043 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00003044 } else if (const GenericSelectionExpr *GE =
3045 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedmana5e66012013-07-20 00:40:58 +00003046 if (GE->isResultDependent())
3047 return NPCK_NotNull;
Peter Collingbournef111d932011-04-15 00:35:48 +00003048 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedmana5e66012013-07-20 00:40:58 +00003049 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3050 if (CE->isConditionDependent())
3051 return NPCK_NotNull;
3052 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00003053 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00003054 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smithc3bf52c2013-04-20 22:23:05 +00003055 // See through default argument expressions.
Douglas Gregorce940492009-09-25 04:25:58 +00003056 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smithc3bf52c2013-04-20 22:23:05 +00003057 } else if (const CXXDefaultInitExpr *DefaultInit
3058 = dyn_cast<CXXDefaultInitExpr>(this)) {
3059 // See through default initializer expressions.
3060 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003061 } else if (isa<GNUNullExpr>(this)) {
3062 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00003063 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00003064 } else if (const MaterializeTemporaryExpr *M
3065 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3066 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00003067 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3068 if (const Expr *Source = OVE->getSourceExpr())
3069 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00003070 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003071
Richard Smith4e24f0f2013-01-02 12:01:23 +00003072 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003073 if (getType()->isNullPtrType())
Richard Smith4e24f0f2013-01-02 12:01:23 +00003074 return NPCK_CXX11_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003075
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003076 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smithf050d242013-06-13 02:46:14 +00003077 if (!Ctx.getLangOpts().CPlusPlus11 &&
3078 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003079 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3080 const Expr *InitExpr = CLE->getInitializer();
3081 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3082 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3083 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003084 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00003085 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003086 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00003087 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00003088
Richard Smith80ad52f2013-01-02 11:42:31 +00003089 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smithf050d242013-06-13 02:46:14 +00003090 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3091 // value zero or a prvalue of type std::nullptr_t.
3092 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
3093 return (Lit && !Lit->getValue()) ? NPCK_ZeroLiteral : NPCK_NotNull;
Richard Smith70488e22012-02-14 21:38:30 +00003094 } else {
Richard Smithf050d242013-06-13 02:46:14 +00003095 // If we have an integer constant expression, we need to *evaluate* it and
3096 // test for the value 0.
Richard Smith70488e22012-02-14 21:38:30 +00003097 if (!isIntegerConstantExpr(Ctx))
3098 return NPCK_NotNull;
3099 }
Chandler Carruth82214a82011-02-18 23:54:50 +00003100
David Blaikie50800fc2012-08-08 17:33:31 +00003101 if (EvaluateKnownConstInt(Ctx) != 0)
3102 return NPCK_NotNull;
3103
3104 if (isa<IntegerLiteral>(this))
3105 return NPCK_ZeroLiteral;
3106 return NPCK_ZeroExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00003107}
Steve Naroff31a45842007-07-28 23:10:27 +00003108
John McCallf6a16482010-12-04 03:47:34 +00003109/// \brief If this expression is an l-value for an Objective C
3110/// property, find the underlying property reference expression.
3111const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3112 const Expr *E = this;
3113 while (true) {
3114 assert((E->getValueKind() == VK_LValue &&
3115 E->getObjectKind() == OK_ObjCProperty) &&
3116 "expression is not a property reference");
3117 E = E->IgnoreParenCasts();
3118 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3119 if (BO->getOpcode() == BO_Comma) {
3120 E = BO->getRHS();
3121 continue;
3122 }
3123 }
3124
3125 break;
3126 }
3127
3128 return cast<ObjCPropertyRefExpr>(E);
3129}
3130
Anna Zaksbbff82f2012-10-01 20:34:04 +00003131bool Expr::isObjCSelfExpr() const {
3132 const Expr *E = IgnoreParenImpCasts();
3133
3134 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3135 if (!DRE)
3136 return false;
3137
3138 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3139 if (!Param)
3140 return false;
3141
3142 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3143 if (!M)
3144 return false;
3145
3146 return M->getSelfDecl() == Param;
3147}
3148
John McCall993f43f2013-05-06 21:39:12 +00003149FieldDecl *Expr::getSourceBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00003150 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003151
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003152 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00003153 if (ICE->getCastKind() == CK_LValueToRValue ||
3154 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003155 E = ICE->getSubExpr()->IgnoreParens();
3156 else
3157 break;
3158 }
3159
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003160 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00003161 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003162 if (Field->isBitField())
3163 return Field;
3164
John McCall993f43f2013-05-06 21:39:12 +00003165 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3166 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3167 if (Ivar->isBitField())
3168 return Ivar;
3169
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00003170 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3171 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3172 if (Field->isBitField())
3173 return Field;
3174
Eli Friedman42068e92011-07-13 02:05:57 +00003175 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003176 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCall993f43f2013-05-06 21:39:12 +00003177 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003178
Eli Friedman42068e92011-07-13 02:05:57 +00003179 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCall993f43f2013-05-06 21:39:12 +00003180 return BinOp->getRHS()->getSourceBitField();
Eli Friedman42068e92011-07-13 02:05:57 +00003181 }
3182
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003183 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003184}
3185
Anders Carlsson09380262010-01-31 17:18:49 +00003186bool Expr::refersToVectorElement() const {
3187 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00003188
Anders Carlsson09380262010-01-31 17:18:49 +00003189 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00003190 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00003191 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00003192 E = ICE->getSubExpr()->IgnoreParens();
3193 else
3194 break;
3195 }
Sean Huntc3021132010-05-05 15:23:54 +00003196
Anders Carlsson09380262010-01-31 17:18:49 +00003197 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3198 return ASE->getBase()->getType()->isVectorType();
3199
3200 if (isa<ExtVectorElementExpr>(E))
3201 return true;
3202
3203 return false;
3204}
3205
Chris Lattner2140e902009-02-16 22:14:05 +00003206/// isArrow - Return true if the base expression is a pointer to vector,
3207/// return false if the base expression is a vector.
3208bool ExtVectorElementExpr::isArrow() const {
3209 return getBase()->getType()->isPointerType();
3210}
3211
Nate Begeman213541a2008-04-18 23:10:10 +00003212unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003213 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003214 return VT->getNumElements();
3215 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003216}
3217
Nate Begeman8a997642008-05-09 06:41:27 +00003218/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003219bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003220 // FIXME: Refactor this code to an accessor on the AST node which returns the
3221 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003222 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003223
3224 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003225 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003226 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003227
Nate Begeman190d6a22009-01-18 02:01:21 +00003228 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003229 if (Comp[0] == 's' || Comp[0] == 'S')
3230 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003231
Daniel Dunbar15027422009-10-17 23:53:04 +00003232 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003233 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003234 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003235
Steve Narofffec0b492007-07-30 03:29:09 +00003236 return false;
3237}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003238
Nate Begeman8a997642008-05-09 06:41:27 +00003239/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003240void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003241 SmallVectorImpl<unsigned> &Elts) const {
3242 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003243 if (Comp[0] == 's' || Comp[0] == 'S')
3244 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003245
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003246 bool isHi = Comp == "hi";
3247 bool isLo = Comp == "lo";
3248 bool isEven = Comp == "even";
3249 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003250
Nate Begeman8a997642008-05-09 06:41:27 +00003251 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3252 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003253
Nate Begeman8a997642008-05-09 06:41:27 +00003254 if (isHi)
3255 Index = e + i;
3256 else if (isLo)
3257 Index = i;
3258 else if (isEven)
3259 Index = 2 * i;
3260 else if (isOdd)
3261 Index = 2 * i + 1;
3262 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003263 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003264
Nate Begeman3b8d1162008-05-13 21:03:02 +00003265 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003266 }
Nate Begeman8a997642008-05-09 06:41:27 +00003267}
3268
Douglas Gregor04badcf2010-04-21 00:45:42 +00003269ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003270 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003271 SourceLocation LBracLoc,
3272 SourceLocation SuperLoc,
3273 bool IsInstanceSuper,
3274 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003275 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003276 ArrayRef<SourceLocation> SelLocs,
3277 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003278 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003279 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003280 SourceLocation RBracLoc,
3281 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003282 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003283 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003284 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003285 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003286 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3287 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003288 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003289 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3290 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003291{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003292 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003293 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003294}
3295
Douglas Gregor04badcf2010-04-21 00:45:42 +00003296ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003297 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003298 SourceLocation LBracLoc,
3299 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003300 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003301 ArrayRef<SourceLocation> SelLocs,
3302 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003303 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003304 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003305 SourceLocation RBracLoc,
3306 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003307 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003308 T->isDependentType(), T->isInstantiationDependentType(),
3309 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003310 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3311 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003312 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003313 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003314 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003315{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003316 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003317 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003318}
3319
Douglas Gregor04badcf2010-04-21 00:45:42 +00003320ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003321 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003322 SourceLocation LBracLoc,
3323 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003324 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003325 ArrayRef<SourceLocation> SelLocs,
3326 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003327 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003328 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003329 SourceLocation RBracLoc,
3330 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003331 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003332 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003333 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003334 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003335 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3336 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003337 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003338 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003339 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003340{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003341 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003342 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003343}
3344
3345void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3346 ArrayRef<SourceLocation> SelLocs,
3347 SelectorLocationsKind SelLocsK) {
3348 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003349 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003350 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003351 if (Args[I]->isTypeDependent())
3352 ExprBits.TypeDependent = true;
3353 if (Args[I]->isValueDependent())
3354 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003355 if (Args[I]->isInstantiationDependent())
3356 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003357 if (Args[I]->containsUnexpandedParameterPack())
3358 ExprBits.ContainsUnexpandedParameterPack = true;
3359
3360 MyArgs[I] = Args[I];
3361 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003362
Benjamin Kramer19562c92012-02-20 00:20:48 +00003363 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003364 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003365 if (SelLocsK == SelLoc_NonStandard)
3366 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3367 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003368}
3369
Douglas Gregor04badcf2010-04-21 00:45:42 +00003370ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003371 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003372 SourceLocation LBracLoc,
3373 SourceLocation SuperLoc,
3374 bool IsInstanceSuper,
3375 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003376 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003377 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003378 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003379 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003380 SourceLocation RBracLoc,
3381 bool isImplicit) {
3382 assert((!SelLocs.empty() || isImplicit) &&
3383 "No selector locs for non-implicit message");
3384 ObjCMessageExpr *Mem;
3385 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3386 if (isImplicit)
3387 Mem = alloc(Context, Args.size(), 0);
3388 else
3389 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003390 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003391 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003392 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003393}
3394
3395ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003396 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003397 SourceLocation LBracLoc,
3398 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003399 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003400 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003401 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003402 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003403 SourceLocation RBracLoc,
3404 bool isImplicit) {
3405 assert((!SelLocs.empty() || isImplicit) &&
3406 "No selector locs for non-implicit message");
3407 ObjCMessageExpr *Mem;
3408 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3409 if (isImplicit)
3410 Mem = alloc(Context, Args.size(), 0);
3411 else
3412 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003413 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003414 SelLocs, SelLocsK, Method, Args, RBracLoc,
3415 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003416}
3417
3418ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003419 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003420 SourceLocation LBracLoc,
3421 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003422 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003423 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003424 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003425 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003426 SourceLocation RBracLoc,
3427 bool isImplicit) {
3428 assert((!SelLocs.empty() || isImplicit) &&
3429 "No selector locs for non-implicit message");
3430 ObjCMessageExpr *Mem;
3431 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3432 if (isImplicit)
3433 Mem = alloc(Context, Args.size(), 0);
3434 else
3435 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003436 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003437 SelLocs, SelLocsK, Method, Args, RBracLoc,
3438 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003439}
3440
Sean Huntc3021132010-05-05 15:23:54 +00003441ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003442 unsigned NumArgs,
3443 unsigned NumStoredSelLocs) {
3444 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003445 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3446}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003447
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003448ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3449 ArrayRef<Expr *> Args,
3450 SourceLocation RBraceLoc,
3451 ArrayRef<SourceLocation> SelLocs,
3452 Selector Sel,
3453 SelectorLocationsKind &SelLocsK) {
3454 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3455 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3456 : 0;
3457 return alloc(C, Args.size(), NumStoredSelLocs);
3458}
3459
3460ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3461 unsigned NumArgs,
3462 unsigned NumStoredSelLocs) {
3463 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3464 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3465 return (ObjCMessageExpr *)C.Allocate(Size,
3466 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3467}
3468
3469void ObjCMessageExpr::getSelectorLocs(
3470 SmallVectorImpl<SourceLocation> &SelLocs) const {
3471 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3472 SelLocs.push_back(getSelectorLoc(i));
3473}
3474
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003475SourceRange ObjCMessageExpr::getReceiverRange() const {
3476 switch (getReceiverKind()) {
3477 case Instance:
3478 return getInstanceReceiver()->getSourceRange();
3479
3480 case Class:
3481 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3482
3483 case SuperInstance:
3484 case SuperClass:
3485 return getSuperLoc();
3486 }
3487
David Blaikie30263482012-01-20 21:50:17 +00003488 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003489}
3490
Douglas Gregor04badcf2010-04-21 00:45:42 +00003491Selector ObjCMessageExpr::getSelector() const {
3492 if (HasMethod)
3493 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3494 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003495 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003496}
3497
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003498QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor04badcf2010-04-21 00:45:42 +00003499 switch (getReceiverKind()) {
3500 case Instance:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003501 return getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003502 case Class:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003503 return getClassReceiver();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003504 case SuperInstance:
Douglas Gregor04badcf2010-04-21 00:45:42 +00003505 case SuperClass:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003506 return getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003507 }
3508
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003509 llvm_unreachable("unexpected receiver kind");
3510}
3511
3512ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3513 QualType T = getReceiverType();
3514
3515 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3516 return Ptr->getInterfaceDecl();
3517
3518 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3519 return Ty->getInterface();
3520
Douglas Gregor04badcf2010-04-21 00:45:42 +00003521 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003522}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003523
Chris Lattner5f9e2722011-07-23 10:55:15 +00003524StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003525 switch (getBridgeKind()) {
3526 case OBC_Bridge:
3527 return "__bridge";
3528 case OBC_BridgeTransfer:
3529 return "__bridge_transfer";
3530 case OBC_BridgeRetained:
3531 return "__bridge_retained";
3532 }
David Blaikie30263482012-01-20 21:50:17 +00003533
3534 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003535}
3536
Craig Topper05ed1a02013-08-18 10:09:15 +00003537ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003538 QualType Type, SourceLocation BLoc,
3539 SourceLocation RP)
3540 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3541 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003542 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003543 Type->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003544 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003545{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003546 SubExprs = new (C) Stmt*[args.size()];
3547 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003548 if (args[i]->isTypeDependent())
3549 ExprBits.TypeDependent = true;
3550 if (args[i]->isValueDependent())
3551 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003552 if (args[i]->isInstantiationDependent())
3553 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003554 if (args[i]->containsUnexpandedParameterPack())
3555 ExprBits.ContainsUnexpandedParameterPack = true;
3556
3557 SubExprs[i] = args[i];
3558 }
3559}
3560
Craig Topper05ed1a02013-08-18 10:09:15 +00003561void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman888376a2009-08-12 02:28:50 +00003562 if (SubExprs) C.Deallocate(SubExprs);
3563
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003564 this->NumExprs = Exprs.size();
Dmitri Gribenko2ad77cd2013-05-10 17:30:13 +00003565 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003566 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00003567}
Nate Begeman888376a2009-08-12 02:28:50 +00003568
Craig Topper05ed1a02013-08-18 10:09:15 +00003569GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003570 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003571 ArrayRef<TypeSourceInfo*> AssocTypes,
3572 ArrayRef<Expr*> AssocExprs,
3573 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003574 SourceLocation RParenLoc,
3575 bool ContainsUnexpandedParameterPack,
3576 unsigned ResultIndex)
3577 : Expr(GenericSelectionExprClass,
3578 AssocExprs[ResultIndex]->getType(),
3579 AssocExprs[ResultIndex]->getValueKind(),
3580 AssocExprs[ResultIndex]->getObjectKind(),
3581 AssocExprs[ResultIndex]->isTypeDependent(),
3582 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003583 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003584 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003585 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3586 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3587 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3588 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003589 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003590 assert(AssocTypes.size() == AssocExprs.size());
3591 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3592 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003593}
3594
Craig Topper05ed1a02013-08-18 10:09:15 +00003595GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003596 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003597 ArrayRef<TypeSourceInfo*> AssocTypes,
3598 ArrayRef<Expr*> AssocExprs,
3599 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003600 SourceLocation RParenLoc,
3601 bool ContainsUnexpandedParameterPack)
3602 : Expr(GenericSelectionExprClass,
3603 Context.DependentTy,
3604 VK_RValue,
3605 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003606 /*isTypeDependent=*/true,
3607 /*isValueDependent=*/true,
3608 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003609 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003610 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3611 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3612 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3613 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003614 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003615 assert(AssocTypes.size() == AssocExprs.size());
3616 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3617 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003618}
3619
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003620//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003621// DesignatedInitExpr
3622//===----------------------------------------------------------------------===//
3623
Chandler Carruthb1138242011-06-16 06:47:06 +00003624IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003625 assert(Kind == FieldDesignator && "Only valid on a field designator");
3626 if (Field.NameOrField & 0x01)
3627 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3628 else
3629 return getField()->getIdentifier();
3630}
3631
Craig Topper05ed1a02013-08-18 10:09:15 +00003632DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003633 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003634 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003635 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003636 bool GNUSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003637 ArrayRef<Expr*> IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003638 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003639 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003640 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003641 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003642 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003643 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003644 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003645 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003646 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003647
3648 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003649 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003650 *Child++ = Init;
3651
3652 // Copy the designators and their subexpressions, computing
3653 // value-dependence along the way.
3654 unsigned IndexIdx = 0;
3655 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003656 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003657
3658 if (this->Designators[I].isArrayDesignator()) {
3659 // Compute type- and value-dependence.
3660 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003661 if (Index->isTypeDependent() || Index->isValueDependent())
3662 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003663 if (Index->isInstantiationDependent())
3664 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003665 // Propagate unexpanded parameter packs.
3666 if (Index->containsUnexpandedParameterPack())
3667 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003668
3669 // Copy the index expressions into permanent storage.
3670 *Child++ = IndexExprs[IndexIdx++];
3671 } else if (this->Designators[I].isArrayRangeDesignator()) {
3672 // Compute type- and value-dependence.
3673 Expr *Start = IndexExprs[IndexIdx];
3674 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003675 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003676 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003677 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003678 ExprBits.InstantiationDependent = true;
3679 } else if (Start->isInstantiationDependent() ||
3680 End->isInstantiationDependent()) {
3681 ExprBits.InstantiationDependent = true;
3682 }
3683
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003684 // Propagate unexpanded parameter packs.
3685 if (Start->containsUnexpandedParameterPack() ||
3686 End->containsUnexpandedParameterPack())
3687 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003688
3689 // Copy the start/end expressions into permanent storage.
3690 *Child++ = IndexExprs[IndexIdx++];
3691 *Child++ = IndexExprs[IndexIdx++];
3692 }
3693 }
3694
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003695 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003696}
3697
Douglas Gregor05c13a32009-01-22 00:58:24 +00003698DesignatedInitExpr *
Craig Topper05ed1a02013-08-18 10:09:15 +00003699DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003700 unsigned NumDesignators,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003701 ArrayRef<Expr*> IndexExprs,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003702 SourceLocation ColonOrEqualLoc,
3703 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003704 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003705 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003706 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003707 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003708 IndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003709}
3710
Craig Topper05ed1a02013-08-18 10:09:15 +00003711DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003712 unsigned NumIndexExprs) {
3713 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3714 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3715 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3716}
3717
Craig Topper05ed1a02013-08-18 10:09:15 +00003718void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003719 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003720 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003721 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003722 NumDesignators = NumDesigs;
3723 for (unsigned I = 0; I != NumDesigs; ++I)
3724 Designators[I] = Desigs[I];
3725}
3726
Abramo Bagnara24f46742011-03-16 15:08:46 +00003727SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3728 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3729 if (size() == 1)
3730 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen65d78312012-12-25 14:51:39 +00003731 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3732 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara24f46742011-03-16 15:08:46 +00003733}
3734
Erik Verbruggen65d78312012-12-25 14:51:39 +00003735SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003736 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003737 Designator &First =
3738 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003739 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003740 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003741 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3742 else
3743 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3744 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003745 StartLoc =
3746 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen65d78312012-12-25 14:51:39 +00003747 return StartLoc;
3748}
3749
3750SourceLocation DesignatedInitExpr::getLocEnd() const {
3751 return getInit()->getLocEnd();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003752}
3753
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003754Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003755 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003756 char *Ptr = static_cast<char *>(
3757 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003758 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003759 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3760 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3761}
3762
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003763Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003764 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003765 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003766 char *Ptr = static_cast<char *>(
3767 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003768 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003769 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3770 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3771}
3772
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003773Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003774 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003775 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003776 char *Ptr = static_cast<char *>(
3777 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003778 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003779 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3780 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3781}
3782
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003783/// \brief Replaces the designator at index @p Idx with the series
3784/// of designators in [First, Last).
Craig Topper05ed1a02013-08-18 10:09:15 +00003785void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003786 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003787 const Designator *Last) {
3788 unsigned NumNewDesignators = Last - First;
3789 if (NumNewDesignators == 0) {
3790 std::copy_backward(Designators + Idx + 1,
3791 Designators + NumDesignators,
3792 Designators + Idx);
3793 --NumNewDesignators;
3794 return;
3795 } else if (NumNewDesignators == 1) {
3796 Designators[Idx] = *First;
3797 return;
3798 }
3799
Mike Stump1eb44332009-09-09 15:08:12 +00003800 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003801 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003802 std::copy(Designators, Designators + Idx, NewDesignators);
3803 std::copy(First, Last, NewDesignators + Idx);
3804 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3805 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003806 Designators = NewDesignators;
3807 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3808}
3809
Craig Topper05ed1a02013-08-18 10:09:15 +00003810ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003811 ArrayRef<Expr*> exprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003812 SourceLocation rparenloc)
3813 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003814 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003815 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3816 Exprs = new (C) Stmt*[exprs.size()];
3817 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003818 if (exprs[i]->isTypeDependent())
3819 ExprBits.TypeDependent = true;
3820 if (exprs[i]->isValueDependent())
3821 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003822 if (exprs[i]->isInstantiationDependent())
3823 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003824 if (exprs[i]->containsUnexpandedParameterPack())
3825 ExprBits.ContainsUnexpandedParameterPack = true;
3826
Nate Begeman2ef13e52009-08-10 23:49:36 +00003827 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003828 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003829}
3830
John McCalle996ffd2011-02-16 08:02:54 +00003831const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3832 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3833 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003834 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3835 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003836 e = cast<CXXConstructExpr>(e)->getArg(0);
3837 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3838 e = ice->getSubExpr();
3839 return cast<OpaqueValueExpr>(e);
3840}
3841
Craig Topper05ed1a02013-08-18 10:09:15 +00003842PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3843 EmptyShell sh,
John McCall4b9c2d22011-11-06 09:01:30 +00003844 unsigned numSemanticExprs) {
3845 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3846 (1 + numSemanticExprs) * sizeof(Expr*),
3847 llvm::alignOf<PseudoObjectExpr>());
3848 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3849}
3850
3851PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3852 : Expr(PseudoObjectExprClass, shell) {
3853 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3854}
3855
Craig Topper05ed1a02013-08-18 10:09:15 +00003856PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCall4b9c2d22011-11-06 09:01:30 +00003857 ArrayRef<Expr*> semantics,
3858 unsigned resultIndex) {
3859 assert(syntax && "no syntactic expression!");
3860 assert(semantics.size() && "no semantic expressions!");
3861
3862 QualType type;
3863 ExprValueKind VK;
3864 if (resultIndex == NoResult) {
3865 type = C.VoidTy;
3866 VK = VK_RValue;
3867 } else {
3868 assert(resultIndex < semantics.size());
3869 type = semantics[resultIndex]->getType();
3870 VK = semantics[resultIndex]->getValueKind();
3871 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3872 }
3873
3874 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3875 (1 + semantics.size()) * sizeof(Expr*),
3876 llvm::alignOf<PseudoObjectExpr>());
3877 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3878 resultIndex);
3879}
3880
3881PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3882 Expr *syntax, ArrayRef<Expr*> semantics,
3883 unsigned resultIndex)
3884 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3885 /*filled in at end of ctor*/ false, false, false, false) {
3886 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3887 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3888
3889 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3890 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3891 getSubExprsBuffer()[i] = E;
3892
3893 if (E->isTypeDependent())
3894 ExprBits.TypeDependent = true;
3895 if (E->isValueDependent())
3896 ExprBits.ValueDependent = true;
3897 if (E->isInstantiationDependent())
3898 ExprBits.InstantiationDependent = true;
3899 if (E->containsUnexpandedParameterPack())
3900 ExprBits.ContainsUnexpandedParameterPack = true;
3901
3902 if (isa<OpaqueValueExpr>(E))
3903 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3904 "opaque-value semantic expressions for pseudo-object "
3905 "operations must have sources");
3906 }
3907}
3908
Douglas Gregor05c13a32009-01-22 00:58:24 +00003909//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003910// ExprIterator.
3911//===----------------------------------------------------------------------===//
3912
3913Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3914Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3915Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3916const Expr* ConstExprIterator::operator[](size_t idx) const {
3917 return cast<Expr>(I[idx]);
3918}
3919const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3920const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3921
3922//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003923// Child Iterators for iterating over subexpressions/substatements
3924//===----------------------------------------------------------------------===//
3925
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003926// UnaryExprOrTypeTraitExpr
3927Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003928 // If this is of a type and the type is a VLA type (and not a typedef), the
3929 // size expression of the VLA needs to be treated as an executable expression.
3930 // Why isn't this weirdness documented better in StmtIterator?
3931 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003932 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003933 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003934 return child_range(child_iterator(T), child_iterator());
3935 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003936 }
John McCall63c00d72011-02-09 08:16:59 +00003937 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003938}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003939
Steve Naroff563477d2007-09-18 23:55:05 +00003940// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003941Stmt::child_range ObjCMessageExpr::children() {
3942 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003943 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003944 begin = reinterpret_cast<Stmt **>(this + 1);
3945 else
3946 begin = reinterpret_cast<Stmt **>(getArgs());
3947 return child_range(begin,
3948 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003949}
3950
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003951ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003952 QualType T, ObjCMethodDecl *Method,
3953 SourceRange SR)
3954 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3955 false, false, false, false),
3956 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3957{
3958 Expr **SaveElements = getElements();
3959 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3960 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3961 ExprBits.ValueDependent = true;
3962 if (Elements[I]->isInstantiationDependent())
3963 ExprBits.InstantiationDependent = true;
3964 if (Elements[I]->containsUnexpandedParameterPack())
3965 ExprBits.ContainsUnexpandedParameterPack = true;
3966
3967 SaveElements[I] = Elements[I];
3968 }
3969}
3970
3971ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003972 ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003973 QualType T, ObjCMethodDecl * Method,
3974 SourceRange SR) {
3975 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3976 + Elements.size() * sizeof(Expr *));
3977 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3978}
3979
3980ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3981 unsigned NumElements) {
3982
3983 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3984 + NumElements * sizeof(Expr *));
3985 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3986}
3987
3988ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3989 ArrayRef<ObjCDictionaryElement> VK,
3990 bool HasPackExpansions,
3991 QualType T, ObjCMethodDecl *method,
3992 SourceRange SR)
3993 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3994 false, false),
3995 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3996 DictWithObjectsMethod(method)
3997{
3998 KeyValuePair *KeyValues = getKeyValues();
3999 ExpansionData *Expansions = getExpansionData();
4000 for (unsigned I = 0; I < NumElements; I++) {
4001 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4002 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4003 ExprBits.ValueDependent = true;
4004 if (VK[I].Key->isInstantiationDependent() ||
4005 VK[I].Value->isInstantiationDependent())
4006 ExprBits.InstantiationDependent = true;
4007 if (VK[I].EllipsisLoc.isInvalid() &&
4008 (VK[I].Key->containsUnexpandedParameterPack() ||
4009 VK[I].Value->containsUnexpandedParameterPack()))
4010 ExprBits.ContainsUnexpandedParameterPack = true;
4011
4012 KeyValues[I].Key = VK[I].Key;
4013 KeyValues[I].Value = VK[I].Value;
4014 if (Expansions) {
4015 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4016 if (VK[I].NumExpansions)
4017 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4018 else
4019 Expansions[I].NumExpansionsPlusOne = 0;
4020 }
4021 }
4022}
4023
4024ObjCDictionaryLiteral *
4025ObjCDictionaryLiteral::Create(ASTContext &C,
4026 ArrayRef<ObjCDictionaryElement> VK,
4027 bool HasPackExpansions,
4028 QualType T, ObjCMethodDecl *method,
4029 SourceRange SR) {
4030 unsigned ExpansionsSize = 0;
4031 if (HasPackExpansions)
4032 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4033
4034 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4035 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4036 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4037}
4038
4039ObjCDictionaryLiteral *
4040ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
4041 bool HasPackExpansions) {
4042 unsigned ExpansionsSize = 0;
4043 if (HasPackExpansions)
4044 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4045 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4046 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4047 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4048 HasPackExpansions);
4049}
4050
4051ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
4052 Expr *base,
4053 Expr *key, QualType T,
4054 ObjCMethodDecl *getMethod,
4055 ObjCMethodDecl *setMethod,
4056 SourceLocation RB) {
4057 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4058 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4059 OK_ObjCSubscript,
4060 getMethod, setMethod, RB);
4061}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004062
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004063AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004064 QualType t, AtomicOp op, SourceLocation RP)
4065 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4066 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004067 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004068{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004069 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4070 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004071 if (args[i]->isTypeDependent())
4072 ExprBits.TypeDependent = true;
4073 if (args[i]->isValueDependent())
4074 ExprBits.ValueDependent = true;
4075 if (args[i]->isInstantiationDependent())
4076 ExprBits.InstantiationDependent = true;
4077 if (args[i]->containsUnexpandedParameterPack())
4078 ExprBits.ContainsUnexpandedParameterPack = true;
4079
4080 SubExprs[i] = args[i];
4081 }
4082}
Richard Smithe1b2abc2012-04-10 22:49:28 +00004083
4084unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4085 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00004086 case AO__c11_atomic_init:
4087 case AO__c11_atomic_load:
4088 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004089 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00004090
4091 case AO__c11_atomic_store:
4092 case AO__c11_atomic_exchange:
4093 case AO__atomic_load:
4094 case AO__atomic_store:
4095 case AO__atomic_store_n:
4096 case AO__atomic_exchange_n:
4097 case AO__c11_atomic_fetch_add:
4098 case AO__c11_atomic_fetch_sub:
4099 case AO__c11_atomic_fetch_and:
4100 case AO__c11_atomic_fetch_or:
4101 case AO__c11_atomic_fetch_xor:
4102 case AO__atomic_fetch_add:
4103 case AO__atomic_fetch_sub:
4104 case AO__atomic_fetch_and:
4105 case AO__atomic_fetch_or:
4106 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00004107 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00004108 case AO__atomic_add_fetch:
4109 case AO__atomic_sub_fetch:
4110 case AO__atomic_and_fetch:
4111 case AO__atomic_or_fetch:
4112 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00004113 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004114 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00004115
4116 case AO__atomic_exchange:
4117 return 4;
4118
4119 case AO__c11_atomic_compare_exchange_strong:
4120 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004121 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00004122
4123 case AO__atomic_compare_exchange:
4124 case AO__atomic_compare_exchange_n:
4125 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00004126 }
4127 llvm_unreachable("unknown atomic op");
4128}