blob: e467883288ba71919f70151d09693ae4e55aa4d4 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner86ee2862008-10-06 06:40:35 +000014#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000017#include "clang/AST/DeclCXX.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor1be329d2012-02-23 07:33:15 +000020#include "clang/AST/EvaluatedExprVisitor.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
David Majnemerbed356a2013-11-06 23:31:56 +000023#include "clang/AST/Mangle.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000025#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000027#include "clang/Basic/CharInfo.h"
Chris Lattnere925d612010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000030#include "clang/Lex/Lexer.h"
31#include "clang/Lex/LiteralSupport.h"
32#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000033#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000034#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000035#include <algorithm>
Eli Friedmanfcec6302011-11-01 02:23:42 +000036#include <cstring>
Chris Lattner1b926492006-08-23 06:42:10 +000037using namespace clang;
38
Rafael Espindolab7f5a9c2012-06-27 18:18:05 +000039const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindolaecbe2e92012-06-28 01:56:38 +000040 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola49e860b2012-06-26 17:45:31 +000041
42 QualType DerivedType = E->getType();
Rafael Espindola49e860b2012-06-26 17:45:31 +000043 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
44 DerivedType = PTy->getPointeeType();
45
Rafael Espindola60a2bba2012-07-17 20:24:05 +000046 if (DerivedType->isDependentType())
47 return NULL;
48
Rafael Espindola49e860b2012-06-26 17:45:31 +000049 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +000050 Decl *D = Ty->getDecl();
51 return cast<CXXRecordDecl>(D);
52}
53
Richard Smithf3fabd22013-06-03 00:17:11 +000054const Expr *Expr::skipRValueSubobjectAdjustments(
55 SmallVectorImpl<const Expr *> &CommaLHSs,
56 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
Rafael Espindola9c006de2012-10-27 01:03:43 +000057 const Expr *E = this;
58 while (true) {
59 E = E->IgnoreParens();
60
61 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
62 if ((CE->getCastKind() == CK_DerivedToBase ||
63 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
64 E->getType()->isRecordType()) {
65 E = CE->getSubExpr();
66 CXXRecordDecl *Derived
67 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
68 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
69 continue;
70 }
71
72 if (CE->getCastKind() == CK_NoOp) {
73 E = CE->getSubExpr();
74 continue;
75 }
76 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
Richard Smith6b6f8aa2013-06-15 00:30:29 +000077 if (!ME->isArrow()) {
Rafael Espindola9c006de2012-10-27 01:03:43 +000078 assert(ME->getBase()->getType()->isRecordType());
79 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
Richard Smith6b6f8aa2013-06-15 00:30:29 +000080 if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
Richard Smith2d187902013-06-03 07:13:35 +000081 E = ME->getBase();
82 Adjustments.push_back(SubobjectAdjustment(Field));
83 continue;
84 }
Rafael Espindola9c006de2012-10-27 01:03:43 +000085 }
86 }
87 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
88 if (BO->isPtrMemOp()) {
Rafael Espindola973aa202012-11-01 14:32:20 +000089 assert(BO->getRHS()->isRValue());
Rafael Espindola9c006de2012-10-27 01:03:43 +000090 E = BO->getLHS();
91 const MemberPointerType *MPT =
92 BO->getRHS()->getType()->getAs<MemberPointerType>();
93 Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
Richard Smithf3fabd22013-06-03 00:17:11 +000094 continue;
95 } else if (BO->getOpcode() == BO_Comma) {
96 CommaLHSs.push_back(BO->getLHS());
97 E = BO->getRHS();
98 continue;
Rafael Espindola9c006de2012-10-27 01:03:43 +000099 }
100 }
101
102 // Nothing changed.
103 break;
104 }
105 return E;
106}
107
108const Expr *
109Expr::findMaterializedTemporary(const MaterializeTemporaryExpr *&MTE) const {
110 const Expr *E = this;
Richard Smith852c9db2013-04-20 22:23:05 +0000111
112 // This might be a default initializer for a reference member. Walk over the
113 // wrapper node for that.
114 if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
115 E = DAE->getExpr();
116
Rafael Espindola9c006de2012-10-27 01:03:43 +0000117 // Look through single-element init lists that claim to be lvalues. They're
118 // just syntactic wrappers in this case.
119 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) {
Richard Smith852c9db2013-04-20 22:23:05 +0000120 if (ILE->getNumInits() == 1 && ILE->isGLValue()) {
Rafael Espindola9c006de2012-10-27 01:03:43 +0000121 E = ILE->getInit(0);
Richard Smith852c9db2013-04-20 22:23:05 +0000122 if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
123 E = DAE->getExpr();
124 }
Rafael Espindola9c006de2012-10-27 01:03:43 +0000125 }
126
127 // Look through expressions for materialized temporaries (for now).
128 if (const MaterializeTemporaryExpr *M
129 = dyn_cast<MaterializeTemporaryExpr>(E)) {
130 MTE = M;
131 E = M->GetTemporaryExpr();
132 }
133
134 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
135 E = DAE->getExpr();
136 return E;
137}
138
Chris Lattner4ebae652010-04-16 23:34:13 +0000139/// isKnownToHaveBooleanValue - Return true if this is an integer expression
140/// that is known to return 0 or 1. This happens for _Bool/bool expressions
141/// but also int expressions which are produced by things like comparisons in
142/// C.
143bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbourne91147592011-04-15 00:35:48 +0000144 const Expr *E = IgnoreParens();
145
Chris Lattner4ebae652010-04-16 23:34:13 +0000146 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +0000147 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000148 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +0000149 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000150
Peter Collingbourne91147592011-04-15 00:35:48 +0000151 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000152 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +0000153 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +0000154 return UO->getSubExpr()->isKnownToHaveBooleanValue();
155 default:
156 return false;
157 }
158 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000159
John McCall45d30c32010-06-12 01:56:02 +0000160 // Only look through implicit casts. If the user writes
161 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +0000162 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000163 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000164
Peter Collingbourne91147592011-04-15 00:35:48 +0000165 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000166 switch (BO->getOpcode()) {
167 default: return false;
John McCalle3027922010-08-25 11:45:40 +0000168 case BO_LT: // Relational operators.
169 case BO_GT:
170 case BO_LE:
171 case BO_GE:
172 case BO_EQ: // Equality operators.
173 case BO_NE:
174 case BO_LAnd: // AND operator.
175 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000176 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000177
John McCalle3027922010-08-25 11:45:40 +0000178 case BO_And: // Bitwise AND operator.
179 case BO_Xor: // Bitwise XOR operator.
180 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000181 // Handle things like (x==2)|(y==12).
182 return BO->getLHS()->isKnownToHaveBooleanValue() &&
183 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000184
John McCalle3027922010-08-25 11:45:40 +0000185 case BO_Comma:
186 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +0000187 return BO->getRHS()->isKnownToHaveBooleanValue();
188 }
189 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000190
Peter Collingbourne91147592011-04-15 00:35:48 +0000191 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000192 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
193 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000194
Chris Lattner4ebae652010-04-16 23:34:13 +0000195 return false;
196}
197
John McCallbd066782011-02-09 08:16:59 +0000198// Amusing macro metaprogramming hack: check whether a class provides
199// a more specific implementation of getExprLoc().
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000200//
201// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCallbd066782011-02-09 08:16:59 +0000202namespace {
203 /// This implementation is used when a class provides a custom
204 /// implementation of getExprLoc.
205 template <class E, class T>
206 SourceLocation getExprLocImpl(const Expr *expr,
207 SourceLocation (T::*v)() const) {
208 return static_cast<const E*>(expr)->getExprLoc();
209 }
210
211 /// This implementation is used when a class doesn't provide
212 /// a custom implementation of getExprLoc. Overload resolution
213 /// should pick it over the implementation above because it's
214 /// more specialized according to function template partial ordering.
215 template <class E>
216 SourceLocation getExprLocImpl(const Expr *expr,
217 SourceLocation (Expr::*v)() const) {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000218 return static_cast<const E*>(expr)->getLocStart();
John McCallbd066782011-02-09 08:16:59 +0000219 }
220}
221
222SourceLocation Expr::getExprLoc() const {
223 switch (getStmtClass()) {
224 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
225#define ABSTRACT_STMT(type)
226#define STMT(type, base) \
227 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
228#define EXPR(type, base) \
229 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
230#include "clang/AST/StmtNodes.inc"
231 }
232 llvm_unreachable("unknown statement kind");
John McCallbd066782011-02-09 08:16:59 +0000233}
234
Chris Lattner0eedafe2006-08-24 04:56:27 +0000235//===----------------------------------------------------------------------===//
236// Primary Expressions.
237//===----------------------------------------------------------------------===//
238
Douglas Gregor678d76c2011-07-01 01:22:09 +0000239/// \brief Compute the type-, value-, and instantiation-dependence of a
240/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000241/// based on the declaration being referenced.
Craig Topperce7167c2013-08-22 04:58:56 +0000242static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
243 QualType T, bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000244 bool &ValueDependent,
245 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000246 TypeDependent = false;
247 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000248 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000249
250 // (TD) C++ [temp.dep.expr]p3:
251 // An id-expression is type-dependent if it contains:
252 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000253 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000254 //
255 // (VD) C++ [temp.dep.constexpr]p2:
256 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000257
Douglas Gregored6c7442009-11-23 11:41:28 +0000258 // (TD) - an identifier that was declared with dependent type
259 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000260 if (T->isDependentType()) {
261 TypeDependent = true;
262 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000263 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000264 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000265 } else if (T->isInstantiationDependentType()) {
266 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000267 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000268
Douglas Gregored6c7442009-11-23 11:41:28 +0000269 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000270 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-07-01 01:22:09 +0000271 == DeclarationName::CXXConversionFunctionName) {
272 QualType T = D->getDeclName().getCXXNameType();
273 if (T->isDependentType()) {
274 TypeDependent = true;
275 ValueDependent = true;
276 InstantiationDependent = true;
277 return;
278 }
279
280 if (T->isInstantiationDependentType())
281 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000282 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000283
Douglas Gregored6c7442009-11-23 11:41:28 +0000284 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000285 if (isa<NonTypeTemplateParmDecl>(D)) {
286 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000287 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000288 return;
289 }
290
Douglas Gregored6c7442009-11-23 11:41:28 +0000291 // (VD) - a constant with integral or enumeration type and is
292 // initialized with an expression that is value-dependent.
Richard Smithec8dcd22011-11-08 01:31:09 +0000293 // (VD) - a constant with literal type and is initialized with an
294 // expression that is value-dependent [C++11].
295 // (VD) - FIXME: Missing from the standard:
296 // - an entity with reference type and is initialized with an
297 // expression that is value-dependent [C++11]
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000298 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000299 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smithd9f663b2013-04-22 15:31:51 +0000300 Var->getType()->isLiteralType(Ctx) :
Richard Smithec8dcd22011-11-08 01:31:09 +0000301 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikief5697e52012-08-10 00:55:35 +0000302 (Var->getType().isConstQualified() ||
Richard Smithec8dcd22011-11-08 01:31:09 +0000303 Var->getType()->isReferenceType())) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000304 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000305 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000306 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000307 InstantiationDependent = true;
308 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000309 }
310
Douglas Gregor0e4de762010-05-11 08:41:30 +0000311 // (VD) - FIXME: Missing from the standard:
312 // - a member function or a static data member of the current
313 // instantiation
Richard Smithec8dcd22011-11-08 01:31:09 +0000314 if (Var->isStaticDataMember() &&
315 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000316 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000317 InstantiationDependent = true;
318 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000319
320 return;
321 }
322
Douglas Gregor0e4de762010-05-11 08:41:30 +0000323 // (VD) - FIXME: Missing from the standard:
324 // - a member function or a static data member of the current
325 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000326 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
327 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000328 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000329 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000330}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000331
Craig Topperce7167c2013-08-22 04:58:56 +0000332void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000333 bool TypeDependent = false;
334 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000335 bool InstantiationDependent = false;
Daniel Dunbar9d355812012-03-09 01:51:51 +0000336 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
337 ValueDependent, InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000338
339 // (TD) C++ [temp.dep.expr]p3:
340 // An id-expression is type-dependent if it contains:
341 //
342 // and
343 //
344 // (VD) C++ [temp.dep.constexpr]p2:
345 // An identifier is value-dependent if it is:
346 if (!TypeDependent && !ValueDependent &&
347 hasExplicitTemplateArgs() &&
348 TemplateSpecializationType::anyDependentTemplateArguments(
349 getTemplateArgs(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000350 getNumTemplateArgs(),
351 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000352 TypeDependent = true;
353 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000354 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000355 }
356
357 ExprBits.TypeDependent = TypeDependent;
358 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000359 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000360
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000361 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000362 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000363 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000364}
365
Craig Topperce7167c2013-08-22 04:58:56 +0000366DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000367 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000368 SourceLocation TemplateKWLoc,
John McCall113bee02012-03-10 09:33:50 +0000369 ValueDecl *D, bool RefersToEnclosingLocal,
370 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000371 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000372 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000373 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000374 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000375 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
376 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000377 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000378 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000379 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
380 if (FoundD)
381 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000382 DeclRefExprBits.HasTemplateKWAndArgsInfo
383 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCall113bee02012-03-10 09:33:50 +0000384 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000385 if (TemplateArgs) {
386 bool Dependent = false;
387 bool InstantiationDependent = false;
388 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000389 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
390 Dependent,
391 InstantiationDependent,
392 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000393 if (InstantiationDependent)
394 setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000395 } else if (TemplateKWLoc.isValid()) {
396 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000397 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000398 DeclRefExprBits.HadMultipleCandidates = 0;
399
Daniel Dunbar9d355812012-03-09 01:51:51 +0000400 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000401}
402
Craig Topperce7167c2013-08-22 04:58:56 +0000403DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000404 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000405 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000406 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000407 bool RefersToEnclosingLocal,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000408 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000409 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000410 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000411 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000412 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000413 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000414 RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000415 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000416 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000417}
418
Craig Topperce7167c2013-08-22 04:58:56 +0000419DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000420 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000421 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000422 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000423 bool RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000424 const DeclarationNameInfo &NameInfo,
425 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000426 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000427 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000428 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000429 // Filter out cases where the found Decl is the same as the value refenenced.
430 if (D == FoundD)
431 FoundD = 0;
432
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000433 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7d170102013-05-15 07:37:26 +0000434 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000435 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000436 if (FoundD)
437 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000438 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000439 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
440 else if (TemplateKWLoc.isValid())
441 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000442
Chris Lattner5c0b4052010-10-30 05:14:06 +0000443 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000444 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000445 RefersToEnclosingLocal,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000446 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000447}
448
Craig Topperce7167c2013-08-22 04:58:56 +0000449DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000450 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000451 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000452 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000453 unsigned NumTemplateArgs) {
454 std::size_t Size = sizeof(DeclRefExpr);
455 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000456 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000457 if (HasFoundDecl)
458 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000459 if (HasTemplateKWAndArgsInfo)
460 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000461
Chris Lattner5c0b4052010-10-30 05:14:06 +0000462 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000463 return new (Mem) DeclRefExpr(EmptyShell());
464}
465
Daniel Dunbarb507f272012-03-09 15:39:15 +0000466SourceLocation DeclRefExpr::getLocStart() const {
467 if (hasQualifier())
468 return getQualifierLoc().getBeginLoc();
469 return getNameInfo().getLocStart();
470}
471SourceLocation DeclRefExpr::getLocEnd() const {
472 if (hasExplicitTemplateArgs())
473 return getRAngleLoc();
474 return getNameInfo().getLocEnd();
475}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000476
Anders Carlsson2fb08242009-09-08 18:24:21 +0000477// FIXME: Maybe this should use DeclPrinter with a special "print predefined
478// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000479std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
480 ASTContext &Context = CurrentDecl->getASTContext();
481
David Majnemerbed356a2013-11-06 23:31:56 +0000482 if (IT == PredefinedExpr::FuncDName) {
483 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
484 OwningPtr<MangleContext> MC;
485 MC.reset(Context.createMangleContext());
486
487 if (MC->shouldMangleDeclName(ND)) {
488 SmallString<256> Buffer;
489 llvm::raw_svector_ostream Out(Buffer);
490 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
491 MC->mangleCXXCtor(CD, Ctor_Base, Out);
492 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
493 MC->mangleCXXDtor(DD, Dtor_Base, Out);
494 else
495 MC->mangleName(ND, Out);
496
497 Out.flush();
498 if (!Buffer.empty() && Buffer.front() == '\01')
499 return Buffer.substr(1);
500 return Buffer.str();
501 } else
502 return ND->getIdentifier()->getName();
503 }
504 return "";
505 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000506 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000507 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000508 return FD->getNameAsString();
509
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000510 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000511 llvm::raw_svector_ostream Out(Name);
512
513 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000514 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000515 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000516 if (MD->isStatic())
517 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000518 }
519
David Blaikiebbafb8a2012-03-11 07:00:24 +0000520 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000521 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000522 llvm::raw_string_ostream POut(Proto);
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000523 FD->printQualifiedName(POut, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000524
Douglas Gregor11a434a2012-04-10 20:14:15 +0000525 const FunctionDecl *Decl = FD;
526 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
527 Decl = Pattern;
528 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000529 const FunctionProtoType *FT = 0;
530 if (FD->hasWrittenPrototype())
531 FT = dyn_cast<FunctionProtoType>(AFT);
532
Douglas Gregor11a434a2012-04-10 20:14:15 +0000533 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000534 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000535 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000536 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000537 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000538 }
539
540 if (FT->isVariadic()) {
541 if (FD->getNumParams()) POut << ", ";
542 POut << "...";
543 }
544 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000545 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000546
Sam Weinig4e83bd22009-12-27 01:38:20 +0000547 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000548 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000549 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000550 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000551 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000552 POut << " volatile";
553 RefQualifierKind Ref = MD->getRefQualifier();
554 if (Ref == RQ_LValue)
555 POut << " &";
556 else if (Ref == RQ_RValue)
557 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000558 }
559
Douglas Gregor11a434a2012-04-10 20:14:15 +0000560 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
561 SpecsTy Specs;
562 const DeclContext *Ctx = FD->getDeclContext();
563 while (Ctx && isa<NamedDecl>(Ctx)) {
564 const ClassTemplateSpecializationDecl *Spec
565 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
566 if (Spec && !Spec->isExplicitSpecialization())
567 Specs.push_back(Spec);
568 Ctx = Ctx->getParent();
569 }
570
571 std::string TemplateParams;
572 llvm::raw_string_ostream TOut(TemplateParams);
573 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
574 I != E; ++I) {
575 const TemplateParameterList *Params
576 = (*I)->getSpecializedTemplate()->getTemplateParameters();
577 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
578 assert(Params->size() == Args.size());
579 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
580 StringRef Param = Params->getParam(i)->getName();
581 if (Param.empty()) continue;
582 TOut << Param << " = ";
583 Args.get(i).print(Policy, TOut);
584 TOut << ", ";
585 }
586 }
587
588 FunctionTemplateSpecializationInfo *FSI
589 = FD->getTemplateSpecializationInfo();
590 if (FSI && !FSI->isExplicitSpecialization()) {
591 const TemplateParameterList* Params
592 = FSI->getTemplate()->getTemplateParameters();
593 const TemplateArgumentList* Args = FSI->TemplateArguments;
594 assert(Params->size() == Args->size());
595 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
596 StringRef Param = Params->getParam(i)->getName();
597 if (Param.empty()) continue;
598 TOut << Param << " = ";
599 Args->get(i).print(Policy, TOut);
600 TOut << ", ";
601 }
602 }
603
604 TOut.flush();
605 if (!TemplateParams.empty()) {
606 // remove the trailing comma and space
607 TemplateParams.resize(TemplateParams.size() - 2);
608 POut << " [" << TemplateParams << "]";
609 }
610
611 POut.flush();
612
Benjamin Kramer90f54222013-08-21 11:45:27 +0000613 // Print "auto" for all deduced return types. This includes C++1y return
614 // type deduction and lambdas. For trailing return types resolve the
615 // decltype expression. Otherwise print the real type when this is
616 // not a constructor or destructor.
617 if ((isa<CXXMethodDecl>(FD) &&
618 cast<CXXMethodDecl>(FD)->getParent()->isLambda()) ||
619 (FT && FT->getResultType()->getAs<AutoType>()))
620 Proto = "auto " + Proto;
621 else if (FT && FT->getResultType()->getAs<DecltypeType>())
622 FT->getResultType()->getAs<DecltypeType>()->getUnderlyingType()
623 .getAsStringInternal(Proto, Policy);
624 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Sam Weinigd060ed42009-12-06 23:55:13 +0000625 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000626
627 Out << Proto;
628
629 Out.flush();
630 return Name.str().str();
631 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000632 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
633 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
634 // Skip to its enclosing function or method, but not its enclosing
635 // CapturedDecl.
636 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
637 const Decl *D = Decl::castFromDeclContext(DC);
638 return ComputeName(IT, D);
639 }
640 llvm_unreachable("CapturedDecl not inside a function or method");
641 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000642 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000643 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000644 llvm::raw_svector_ostream Out(Name);
645 Out << (MD->isInstanceMethod() ? '-' : '+');
646 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000647
648 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
649 // a null check to avoid a crash.
650 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000651 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000652
Anders Carlsson2fb08242009-09-08 18:24:21 +0000653 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000654 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000655 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000656
Anders Carlsson2fb08242009-09-08 18:24:21 +0000657 Out << ' ';
658 Out << MD->getSelector().getAsString();
659 Out << ']';
660
661 Out.flush();
662 return Name.str().str();
663 }
664 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
665 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
666 return "top level";
667 }
668 return "";
669}
670
Craig Topper37932912013-08-18 10:09:15 +0000671void APNumericStorage::setIntValue(const ASTContext &C,
672 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000673 if (hasAllocation())
674 C.Deallocate(pVal);
675
676 BitWidth = Val.getBitWidth();
677 unsigned NumWords = Val.getNumWords();
678 const uint64_t* Words = Val.getRawData();
679 if (NumWords > 1) {
680 pVal = new (C) uint64_t[NumWords];
681 std::copy(Words, Words + NumWords, pVal);
682 } else if (NumWords == 1)
683 VAL = Words[0];
684 else
685 VAL = 0;
686}
687
Craig Topper37932912013-08-18 10:09:15 +0000688IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000689 QualType type, SourceLocation l)
690 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
691 false, false),
692 Loc(l) {
693 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
694 assert(V.getBitWidth() == C.getIntWidth(type) &&
695 "Integer type is not the correct size for constant.");
696 setValue(C, V);
697}
698
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000699IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000700IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000701 QualType type, SourceLocation l) {
702 return new (C) IntegerLiteral(C, V, type, l);
703}
704
705IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000706IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000707 return new (C) IntegerLiteral(Empty);
708}
709
Craig Topper37932912013-08-18 10:09:15 +0000710FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000711 bool isexact, QualType Type, SourceLocation L)
712 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
713 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000714 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000715 FloatingLiteralBits.IsExact = isexact;
716 setValue(C, V);
717}
718
Craig Topper37932912013-08-18 10:09:15 +0000719FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000720 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000721 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000722 FloatingLiteralBits.IsExact = false;
723}
724
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000725FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000726FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000727 bool isexact, QualType Type, SourceLocation L) {
728 return new (C) FloatingLiteral(C, V, isexact, Type, L);
729}
730
731FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000732FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000733 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000734}
735
Tim Northover178723a2013-01-22 09:46:51 +0000736const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
737 switch(FloatingLiteralBits.Semantics) {
738 case IEEEhalf:
739 return llvm::APFloat::IEEEhalf;
740 case IEEEsingle:
741 return llvm::APFloat::IEEEsingle;
742 case IEEEdouble:
743 return llvm::APFloat::IEEEdouble;
744 case x87DoubleExtended:
745 return llvm::APFloat::x87DoubleExtended;
746 case IEEEquad:
747 return llvm::APFloat::IEEEquad;
748 case PPCDoubleDouble:
749 return llvm::APFloat::PPCDoubleDouble;
750 }
751 llvm_unreachable("Unrecognised floating semantics");
752}
753
754void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
755 if (&Sem == &llvm::APFloat::IEEEhalf)
756 FloatingLiteralBits.Semantics = IEEEhalf;
757 else if (&Sem == &llvm::APFloat::IEEEsingle)
758 FloatingLiteralBits.Semantics = IEEEsingle;
759 else if (&Sem == &llvm::APFloat::IEEEdouble)
760 FloatingLiteralBits.Semantics = IEEEdouble;
761 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
762 FloatingLiteralBits.Semantics = x87DoubleExtended;
763 else if (&Sem == &llvm::APFloat::IEEEquad)
764 FloatingLiteralBits.Semantics = IEEEquad;
765 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
766 FloatingLiteralBits.Semantics = PPCDoubleDouble;
767 else
768 llvm_unreachable("Unknown floating semantics");
769}
770
Chris Lattnera0173132008-06-07 22:13:43 +0000771/// getValueAsApproximateDouble - This returns the value as an inaccurate
772/// double. Note that this may cause loss of precision, but is useful for
773/// debugging dumps, etc.
774double FloatingLiteral::getValueAsApproximateDouble() const {
775 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000776 bool ignored;
777 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
778 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000779 return V.convertToDouble();
780}
781
Nick Lewycky4ed84042012-02-24 09:07:53 +0000782int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000783 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000784 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000785 case Ascii:
786 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000787 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000788 break;
789 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000790 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000791 break;
792 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000793 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000794 break;
795 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000796 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000797 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000798 }
799 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
800 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000801 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000802 && "character byte widths supported are 1, 2, and 4 only");
803 return CharByteWidth;
804}
805
Craig Topper37932912013-08-18 10:09:15 +0000806StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000807 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000808 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000809 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000810 // Allocate enough space for the StringLiteral plus an array of locations for
811 // any concatenated string tokens.
812 void *Mem = C.Allocate(sizeof(StringLiteral)+
813 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000814 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000815 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000816
Steve Naroffdf7855b2007-02-21 23:46:25 +0000817 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000818 SL->setString(C,Str,Kind,Pascal);
819
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000820 SL->TokLocs[0] = Loc[0];
821 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000822
Chris Lattner630970d2009-02-18 05:49:11 +0000823 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000824 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
825 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000826}
827
Craig Topper37932912013-08-18 10:09:15 +0000828StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
829 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000830 void *Mem = C.Allocate(sizeof(StringLiteral)+
831 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000832 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000833 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000834 SL->CharByteWidth = 0;
835 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000836 SL->NumConcatenated = NumStrs;
837 return SL;
838}
839
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000840void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000841 switch (getKind()) {
842 case Ascii: break; // no prefix.
843 case Wide: OS << 'L'; break;
844 case UTF8: OS << "u8"; break;
845 case UTF16: OS << 'u'; break;
846 case UTF32: OS << 'U'; break;
847 }
848 OS << '"';
849 static const char Hex[] = "0123456789ABCDEF";
850
851 unsigned LastSlashX = getLength();
852 for (unsigned I = 0, N = getLength(); I != N; ++I) {
853 switch (uint32_t Char = getCodeUnit(I)) {
854 default:
855 // FIXME: Convert UTF-8 back to codepoints before rendering.
856
857 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
858 // Leave invalid surrogates alone; we'll use \x for those.
859 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
860 Char <= 0xdbff) {
861 uint32_t Trail = getCodeUnit(I + 1);
862 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
863 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
864 ++I;
865 }
866 }
867
868 if (Char > 0xff) {
869 // If this is a wide string, output characters over 0xff using \x
870 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
871 // codepoint: use \x escapes for invalid codepoints.
872 if (getKind() == Wide ||
873 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
874 // FIXME: Is this the best way to print wchar_t?
875 OS << "\\x";
876 int Shift = 28;
877 while ((Char >> Shift) == 0)
878 Shift -= 4;
879 for (/**/; Shift >= 0; Shift -= 4)
880 OS << Hex[(Char >> Shift) & 15];
881 LastSlashX = I;
882 break;
883 }
884
885 if (Char > 0xffff)
886 OS << "\\U00"
887 << Hex[(Char >> 20) & 15]
888 << Hex[(Char >> 16) & 15];
889 else
890 OS << "\\u";
891 OS << Hex[(Char >> 12) & 15]
892 << Hex[(Char >> 8) & 15]
893 << Hex[(Char >> 4) & 15]
894 << Hex[(Char >> 0) & 15];
895 break;
896 }
897
898 // If we used \x... for the previous character, and this character is a
899 // hexadecimal digit, prevent it being slurped as part of the \x.
900 if (LastSlashX + 1 == I) {
901 switch (Char) {
902 case '0': case '1': case '2': case '3': case '4':
903 case '5': case '6': case '7': case '8': case '9':
904 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
905 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
906 OS << "\"\"";
907 }
908 }
909
910 assert(Char <= 0xff &&
911 "Characters above 0xff should already have been handled.");
912
Jordan Rosea7d03842013-02-08 22:30:41 +0000913 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000914 OS << (char)Char;
915 else // Output anything hard as an octal escape.
916 OS << '\\'
917 << (char)('0' + ((Char >> 6) & 7))
918 << (char)('0' + ((Char >> 3) & 7))
919 << (char)('0' + ((Char >> 0) & 7));
920 break;
921 // Handle some common non-printable cases to make dumps prettier.
922 case '\\': OS << "\\\\"; break;
923 case '"': OS << "\\\""; break;
924 case '\n': OS << "\\n"; break;
925 case '\t': OS << "\\t"; break;
926 case '\a': OS << "\\a"; break;
927 case '\b': OS << "\\b"; break;
928 }
929 }
930 OS << '"';
931}
932
Craig Topper37932912013-08-18 10:09:15 +0000933void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000934 StringKind Kind, bool IsPascal) {
935 //FIXME: we assume that the string data comes from a target that uses the same
936 // code unit size and endianess for the type of string.
937 this->Kind = Kind;
938 this->IsPascal = IsPascal;
939
Nick Lewycky4ed84042012-02-24 09:07:53 +0000940 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000941 assert((Str.size()%CharByteWidth == 0)
942 && "size of data must be multiple of CharByteWidth");
943 Length = Str.size()/CharByteWidth;
944
945 switch(CharByteWidth) {
946 case 1: {
947 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000948 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000949 StrData.asChar = AStrData;
950 break;
951 }
952 case 2: {
953 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000954 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000955 StrData.asUInt16 = AStrData;
956 break;
957 }
958 case 4: {
959 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000960 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000961 StrData.asUInt32 = AStrData;
962 break;
963 }
964 default:
965 assert(false && "unsupported CharByteWidth");
966 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000967}
968
Chris Lattnere925d612010-11-17 07:37:15 +0000969/// getLocationOfByte - Return a source location that points to the specified
970/// byte of this string literal.
971///
972/// Strings are amazingly complex. They can be formed from multiple tokens and
973/// can have escape sequences in them in addition to the usual trigraph and
974/// escaped newline business. This routine handles this complexity.
975///
976SourceLocation StringLiteral::
977getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
978 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +0000979 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
980 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +0000981
Chris Lattnere925d612010-11-17 07:37:15 +0000982 // Loop over all of the tokens in this string until we find the one that
983 // contains the byte we're looking for.
984 unsigned TokNo = 0;
985 while (1) {
986 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
987 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
988
989 // Get the spelling of the string so that we can get the data that makes up
990 // the string literal, not the identifier for the macro it is potentially
991 // expanded through.
992 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
993
994 // Re-lex the token to get its length and original spelling.
995 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
996 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000997 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +0000998 if (Invalid)
999 return StrTokSpellingLoc;
1000
1001 const char *StrData = Buffer.data()+LocInfo.second;
1002
Chris Lattnere925d612010-11-17 07:37:15 +00001003 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +00001004 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1005 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +00001006 Token TheTok;
1007 TheLexer.LexFromRawLexer(TheTok);
1008
1009 // Use the StringLiteralParser to compute the length of the string in bytes.
1010 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
1011 unsigned TokNumBytes = SLP.GetStringLength();
1012
1013 // If the byte is in this token, return the location of the byte.
1014 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +00001015 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +00001016 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1017
1018 // Now that we know the offset of the token in the spelling, use the
1019 // preprocessor to get the offset in the original source.
1020 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1021 }
1022
1023 // Move to the next string token.
1024 ++TokNo;
1025 ByteNo -= TokNumBytes;
1026 }
1027}
1028
1029
1030
Chris Lattner1b926492006-08-23 06:42:10 +00001031/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1032/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001033StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001034 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001035 case UO_PostInc: return "++";
1036 case UO_PostDec: return "--";
1037 case UO_PreInc: return "++";
1038 case UO_PreDec: return "--";
1039 case UO_AddrOf: return "&";
1040 case UO_Deref: return "*";
1041 case UO_Plus: return "+";
1042 case UO_Minus: return "-";
1043 case UO_Not: return "~";
1044 case UO_LNot: return "!";
1045 case UO_Real: return "__real";
1046 case UO_Imag: return "__imag";
1047 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001048 }
David Blaikief47fa302012-01-17 02:30:50 +00001049 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001050}
1051
John McCalle3027922010-08-25 11:45:40 +00001052UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001053UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1054 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001055 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001056 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1057 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1058 case OO_Amp: return UO_AddrOf;
1059 case OO_Star: return UO_Deref;
1060 case OO_Plus: return UO_Plus;
1061 case OO_Minus: return UO_Minus;
1062 case OO_Tilde: return UO_Not;
1063 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001064 }
1065}
1066
1067OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1068 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001069 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1070 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1071 case UO_AddrOf: return OO_Amp;
1072 case UO_Deref: return OO_Star;
1073 case UO_Plus: return OO_Plus;
1074 case UO_Minus: return OO_Minus;
1075 case UO_Not: return OO_Tilde;
1076 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001077 default: return OO_None;
1078 }
1079}
1080
1081
Chris Lattner0eedafe2006-08-24 04:56:27 +00001082//===----------------------------------------------------------------------===//
1083// Postfix Operators.
1084//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001085
Craig Topper37932912013-08-18 10:09:15 +00001086CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1087 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1088 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001089 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001090 fn->isTypeDependent(),
1091 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001092 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001093 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001094 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001095
Benjamin Kramerc215e762012-08-24 11:54:20 +00001096 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001097 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001098 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001099 if (args[i]->isTypeDependent())
1100 ExprBits.TypeDependent = true;
1101 if (args[i]->isValueDependent())
1102 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001103 if (args[i]->isInstantiationDependent())
1104 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001105 if (args[i]->containsUnexpandedParameterPack())
1106 ExprBits.ContainsUnexpandedParameterPack = true;
1107
Peter Collingbourne3a347252011-02-08 21:18:02 +00001108 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001109 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001110
Peter Collingbourne3a347252011-02-08 21:18:02 +00001111 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001112 RParenLoc = rparenloc;
1113}
Nate Begeman1e36a852008-01-17 17:46:27 +00001114
Craig Topper37932912013-08-18 10:09:15 +00001115CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001116 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1117 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001118 fn->isTypeDependent(),
1119 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001120 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001121 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001122 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001123
Benjamin Kramerc215e762012-08-24 11:54:20 +00001124 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001125 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001126 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001127 if (args[i]->isTypeDependent())
1128 ExprBits.TypeDependent = true;
1129 if (args[i]->isValueDependent())
1130 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001131 if (args[i]->isInstantiationDependent())
1132 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001133 if (args[i]->containsUnexpandedParameterPack())
1134 ExprBits.ContainsUnexpandedParameterPack = true;
1135
Peter Collingbourne3a347252011-02-08 21:18:02 +00001136 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001137 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001138
Peter Collingbourne3a347252011-02-08 21:18:02 +00001139 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001140 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001141}
1142
Craig Topper37932912013-08-18 10:09:15 +00001143CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Mike Stump11289f42009-09-09 15:08:12 +00001144 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001145 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001146 SubExprs = new (C) Stmt*[PREARGS_START];
1147 CallExprBits.NumPreArgs = 0;
1148}
1149
Craig Topper37932912013-08-18 10:09:15 +00001150CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001151 EmptyShell Empty)
1152 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1153 // FIXME: Why do we allocate this?
1154 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1155 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001156}
1157
Nuno Lopes518e3702009-12-20 23:11:08 +00001158Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001159 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001160
1161 while (SubstNonTypeTemplateParmExpr *NTTP
1162 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1163 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1164 }
1165
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001166 // If we're calling a dereference, look at the pointer instead.
1167 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1168 if (BO->isPtrMemOp())
1169 CEE = BO->getRHS()->IgnoreParenCasts();
1170 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1171 if (UO->getOpcode() == UO_Deref)
1172 CEE = UO->getSubExpr()->IgnoreParenCasts();
1173 }
Chris Lattner52301912009-07-17 15:46:27 +00001174 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001175 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001176 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1177 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001178
1179 return 0;
1180}
1181
Nuno Lopes518e3702009-12-20 23:11:08 +00001182FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001183 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001184}
1185
Chris Lattnere4407ed2007-12-28 05:25:02 +00001186/// setNumArgs - This changes the number of arguments present in this call.
1187/// Any orphaned expressions are deleted by this, and any new operands are set
1188/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001189void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001190 // No change, just return.
1191 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001192
Chris Lattnere4407ed2007-12-28 05:25:02 +00001193 // If shrinking # arguments, just delete the extras and forgot them.
1194 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001195 this->NumArgs = NumArgs;
1196 return;
1197 }
1198
1199 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001200 unsigned NumPreArgs = getNumPreArgs();
1201 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001202 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001203 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001204 NewSubExprs[i] = SubExprs[i];
1205 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001206 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1207 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001208 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001209
Douglas Gregorba6e5572009-04-17 21:46:47 +00001210 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001211 SubExprs = NewSubExprs;
1212 this->NumArgs = NumArgs;
1213}
1214
Chris Lattner01ff98a2008-10-06 05:00:53 +00001215/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1216/// not, return 0.
Richard Smithd62306a2011-11-10 06:34:14 +00001217unsigned CallExpr::isBuiltinCall() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001218 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001219 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001220 // ImplicitCastExpr.
1221 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1222 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001223 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001224
Steve Narofff6e3b3292008-01-31 01:07:12 +00001225 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1226 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001227 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001228
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001229 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1230 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001231 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001232
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001233 if (!FDecl->getIdentifier())
1234 return 0;
1235
Douglas Gregor15fc9562009-09-12 00:22:50 +00001236 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001237}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001238
Richard Smith5011a002013-01-17 23:46:04 +00001239bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1240 if (unsigned BI = isBuiltinCall())
1241 return Ctx.BuiltinInfo.isUnevaluated(BI);
1242 return false;
1243}
1244
Anders Carlsson00a27592009-05-26 04:57:27 +00001245QualType CallExpr::getCallReturnType() const {
1246 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001247 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001248 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001249 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001250 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001251 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1252 // This should never be overloaded and so should never return null.
1253 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001254
John McCall0009fcc2011-04-26 20:42:42 +00001255 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +00001256 return FnType->getResultType();
1257}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001258
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001259SourceLocation CallExpr::getLocStart() const {
1260 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001261 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001262
1263 SourceLocation begin = getCallee()->getLocStart();
1264 if (begin.isInvalid() && getNumArgs() > 0)
1265 begin = getArg(0)->getLocStart();
1266 return begin;
1267}
1268SourceLocation CallExpr::getLocEnd() const {
1269 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001270 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001271
1272 SourceLocation end = getRParenLoc();
1273 if (end.isInvalid() && getNumArgs() > 0)
1274 end = getArg(getNumArgs() - 1)->getLocEnd();
1275 return end;
1276}
John McCall701417a2011-02-21 06:23:05 +00001277
Craig Topper37932912013-08-18 10:09:15 +00001278OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001279 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001280 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001281 ArrayRef<OffsetOfNode> comps,
1282 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001283 SourceLocation RParenLoc) {
1284 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001285 sizeof(OffsetOfNode) * comps.size() +
1286 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001287
Benjamin Kramerc215e762012-08-24 11:54:20 +00001288 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1289 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001290}
1291
Craig Topper37932912013-08-18 10:09:15 +00001292OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001293 unsigned numComps, unsigned numExprs) {
1294 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1295 sizeof(OffsetOfNode) * numComps +
1296 sizeof(Expr*) * numExprs);
1297 return new (Mem) OffsetOfExpr(numComps, numExprs);
1298}
1299
Craig Topper37932912013-08-18 10:09:15 +00001300OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001301 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001302 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001303 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001304 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1305 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001306 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001307 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001308 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001309 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001310 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001311{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001312 for (unsigned i = 0; i != comps.size(); ++i) {
1313 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001314 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001315
Benjamin Kramerc215e762012-08-24 11:54:20 +00001316 for (unsigned i = 0; i != exprs.size(); ++i) {
1317 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001318 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001319 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001320 ExprBits.ContainsUnexpandedParameterPack = true;
1321
Benjamin Kramerc215e762012-08-24 11:54:20 +00001322 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001323 }
1324}
1325
1326IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1327 assert(getKind() == Field || getKind() == Identifier);
1328 if (getKind() == Field)
1329 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001330
Douglas Gregor882211c2010-04-28 22:16:22 +00001331 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1332}
1333
Craig Topper37932912013-08-18 10:09:15 +00001334MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001335 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001336 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001337 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001338 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001339 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001340 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001341 QualType ty,
1342 ExprValueKind vk,
1343 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001344 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001345
Douglas Gregorea972d32011-02-28 21:54:11 +00001346 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001347 founddecl.getDecl() != memberdecl ||
1348 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001349 if (hasQualOrFound)
1350 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001351
John McCall6b51f282009-11-23 01:53:49 +00001352 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001353 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1354 else if (TemplateKWLoc.isValid())
1355 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001356
Chris Lattner5c0b4052010-10-30 05:14:06 +00001357 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001358 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1359 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001360
1361 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001362 // FIXME: Wrong. We should be looking at the member declaration we found.
1363 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001364 E->setValueDependent(true);
1365 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001366 E->setInstantiationDependent(true);
1367 }
1368 else if (QualifierLoc &&
1369 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1370 E->setInstantiationDependent(true);
1371
John McCall16df1e52010-03-30 21:47:33 +00001372 E->HasQualifierOrFoundDecl = true;
1373
1374 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001375 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001376 NQ->FoundDecl = founddecl;
1377 }
1378
Abramo Bagnara7945c982012-01-27 09:46:47 +00001379 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1380
John McCall16df1e52010-03-30 21:47:33 +00001381 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001382 bool Dependent = false;
1383 bool InstantiationDependent = false;
1384 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001385 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1386 Dependent,
1387 InstantiationDependent,
1388 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001389 if (InstantiationDependent)
1390 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001391 } else if (TemplateKWLoc.isValid()) {
1392 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001393 }
1394
1395 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001396}
1397
Daniel Dunbarb507f272012-03-09 15:39:15 +00001398SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001399 if (isImplicitAccess()) {
1400 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001401 return getQualifierLoc().getBeginLoc();
1402 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001403 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001404
Daniel Dunbarb507f272012-03-09 15:39:15 +00001405 // FIXME: We don't want this to happen. Rather, we should be able to
1406 // detect all kinds of implicit accesses more cleanly.
1407 SourceLocation BaseStartLoc = getBase()->getLocStart();
1408 if (BaseStartLoc.isValid())
1409 return BaseStartLoc;
1410 return MemberLoc;
1411}
1412SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001413 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001414 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001415 EndLoc = getRAngleLoc();
1416 else if (EndLoc.isInvalid())
1417 EndLoc = getBase()->getLocEnd();
1418 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001419}
1420
John McCall9320b872011-09-09 05:25:32 +00001421void CastExpr::CheckCastConsistency() const {
1422 switch (getCastKind()) {
1423 case CK_DerivedToBase:
1424 case CK_UncheckedDerivedToBase:
1425 case CK_DerivedToBaseMemberPointer:
1426 case CK_BaseToDerived:
1427 case CK_BaseToDerivedMemberPointer:
1428 assert(!path_empty() && "Cast kind should have a base path!");
1429 break;
1430
1431 case CK_CPointerToObjCPointerCast:
1432 assert(getType()->isObjCObjectPointerType());
1433 assert(getSubExpr()->getType()->isPointerType());
1434 goto CheckNoBasePath;
1435
1436 case CK_BlockPointerToObjCPointerCast:
1437 assert(getType()->isObjCObjectPointerType());
1438 assert(getSubExpr()->getType()->isBlockPointerType());
1439 goto CheckNoBasePath;
1440
John McCallc62bb392012-02-15 01:22:51 +00001441 case CK_ReinterpretMemberPointer:
1442 assert(getType()->isMemberPointerType());
1443 assert(getSubExpr()->getType()->isMemberPointerType());
1444 goto CheckNoBasePath;
1445
John McCall9320b872011-09-09 05:25:32 +00001446 case CK_BitCast:
1447 // Arbitrary casts to C pointer types count as bitcasts.
1448 // Otherwise, we should only have block and ObjC pointer casts
1449 // here if they stay within the type kind.
1450 if (!getType()->isPointerType()) {
1451 assert(getType()->isObjCObjectPointerType() ==
1452 getSubExpr()->getType()->isObjCObjectPointerType());
1453 assert(getType()->isBlockPointerType() ==
1454 getSubExpr()->getType()->isBlockPointerType());
1455 }
1456 goto CheckNoBasePath;
1457
1458 case CK_AnyPointerToBlockPointerCast:
1459 assert(getType()->isBlockPointerType());
1460 assert(getSubExpr()->getType()->isAnyPointerType() &&
1461 !getSubExpr()->getType()->isBlockPointerType());
1462 goto CheckNoBasePath;
1463
Douglas Gregored90df32012-02-22 05:02:47 +00001464 case CK_CopyAndAutoreleaseBlockObject:
1465 assert(getType()->isBlockPointerType());
1466 assert(getSubExpr()->getType()->isBlockPointerType());
1467 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001468
1469 case CK_FunctionToPointerDecay:
1470 assert(getType()->isPointerType());
1471 assert(getSubExpr()->getType()->isFunctionType());
1472 goto CheckNoBasePath;
1473
John McCall9320b872011-09-09 05:25:32 +00001474 // These should not have an inheritance path.
1475 case CK_Dynamic:
1476 case CK_ToUnion:
1477 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001478 case CK_NullToMemberPointer:
1479 case CK_NullToPointer:
1480 case CK_ConstructorConversion:
1481 case CK_IntegralToPointer:
1482 case CK_PointerToIntegral:
1483 case CK_ToVoid:
1484 case CK_VectorSplat:
1485 case CK_IntegralCast:
1486 case CK_IntegralToFloating:
1487 case CK_FloatingToIntegral:
1488 case CK_FloatingCast:
1489 case CK_ObjCObjectLValueCast:
1490 case CK_FloatingRealToComplex:
1491 case CK_FloatingComplexToReal:
1492 case CK_FloatingComplexCast:
1493 case CK_FloatingComplexToIntegralComplex:
1494 case CK_IntegralRealToComplex:
1495 case CK_IntegralComplexToReal:
1496 case CK_IntegralComplexCast:
1497 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001498 case CK_ARCProduceObject:
1499 case CK_ARCConsumeObject:
1500 case CK_ARCReclaimReturnedObject:
1501 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001502 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001503 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1504 goto CheckNoBasePath;
1505
1506 case CK_Dependent:
1507 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001508 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001509 case CK_AtomicToNonAtomic:
1510 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001511 case CK_PointerToBoolean:
1512 case CK_IntegralToBoolean:
1513 case CK_FloatingToBoolean:
1514 case CK_MemberPointerToBoolean:
1515 case CK_FloatingComplexToBoolean:
1516 case CK_IntegralComplexToBoolean:
1517 case CK_LValueBitCast: // -> bool&
1518 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001519 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001520 CheckNoBasePath:
1521 assert(path_empty() && "Cast kind should not have a base path!");
1522 break;
1523 }
1524}
1525
Anders Carlsson496335e2009-09-03 00:59:21 +00001526const char *CastExpr::getCastKindName() const {
1527 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001528 case CK_Dependent:
1529 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001530 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001531 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001532 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001533 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001534 case CK_LValueToRValue:
1535 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001536 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001537 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001538 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001539 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001540 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001541 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001542 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001543 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001544 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001545 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001546 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001547 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001548 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001549 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001550 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001551 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001552 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001553 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001554 case CK_NullToPointer:
1555 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001556 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001557 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001558 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001559 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001560 case CK_ReinterpretMemberPointer:
1561 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001562 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001563 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001564 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001565 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001566 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001567 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001568 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001569 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001570 case CK_PointerToBoolean:
1571 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001572 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001573 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001574 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001575 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001576 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001577 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001578 case CK_IntegralToBoolean:
1579 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001580 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001581 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001582 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001583 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001584 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001585 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001586 case CK_FloatingToBoolean:
1587 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001588 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001589 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001590 case CK_CPointerToObjCPointerCast:
1591 return "CPointerToObjCPointerCast";
1592 case CK_BlockPointerToObjCPointerCast:
1593 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001594 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001595 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001596 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001597 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001598 case CK_FloatingRealToComplex:
1599 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001600 case CK_FloatingComplexToReal:
1601 return "FloatingComplexToReal";
1602 case CK_FloatingComplexToBoolean:
1603 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001604 case CK_FloatingComplexCast:
1605 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001606 case CK_FloatingComplexToIntegralComplex:
1607 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001608 case CK_IntegralRealToComplex:
1609 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001610 case CK_IntegralComplexToReal:
1611 return "IntegralComplexToReal";
1612 case CK_IntegralComplexToBoolean:
1613 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001614 case CK_IntegralComplexCast:
1615 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001616 case CK_IntegralComplexToFloatingComplex:
1617 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001618 case CK_ARCConsumeObject:
1619 return "ARCConsumeObject";
1620 case CK_ARCProduceObject:
1621 return "ARCProduceObject";
1622 case CK_ARCReclaimReturnedObject:
1623 return "ARCReclaimReturnedObject";
1624 case CK_ARCExtendBlockObject:
1625 return "ARCCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001626 case CK_AtomicToNonAtomic:
1627 return "AtomicToNonAtomic";
1628 case CK_NonAtomicToAtomic:
1629 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001630 case CK_CopyAndAutoreleaseBlockObject:
1631 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001632 case CK_BuiltinFnToFnPtr:
1633 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001634 case CK_ZeroToOCLEvent:
1635 return "ZeroToOCLEvent";
Anders Carlsson496335e2009-09-03 00:59:21 +00001636 }
Mike Stump11289f42009-09-09 15:08:12 +00001637
John McCallc5e62b42010-11-13 09:02:35 +00001638 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001639}
1640
Douglas Gregord196a582009-12-14 19:27:10 +00001641Expr *CastExpr::getSubExprAsWritten() {
1642 Expr *SubExpr = 0;
1643 CastExpr *E = this;
1644 do {
1645 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001646
1647 // Skip through reference binding to temporary.
1648 if (MaterializeTemporaryExpr *Materialize
1649 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1650 SubExpr = Materialize->GetTemporaryExpr();
1651
Douglas Gregord196a582009-12-14 19:27:10 +00001652 // Skip any temporary bindings; they're implicit.
1653 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1654 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001655
Douglas Gregord196a582009-12-14 19:27:10 +00001656 // Conversions by constructor and conversion functions have a
1657 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001658 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001659 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001660 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001661 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001662
Douglas Gregord196a582009-12-14 19:27:10 +00001663 // If the subexpression we're left with is an implicit cast, look
1664 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001665 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1666
Douglas Gregord196a582009-12-14 19:27:10 +00001667 return SubExpr;
1668}
1669
John McCallcf142162010-08-07 06:22:56 +00001670CXXBaseSpecifier **CastExpr::path_buffer() {
1671 switch (getStmtClass()) {
1672#define ABSTRACT_STMT(x)
1673#define CASTEXPR(Type, Base) \
1674 case Stmt::Type##Class: \
1675 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1676#define STMT(Type, Base)
1677#include "clang/AST/StmtNodes.inc"
1678 default:
1679 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001680 }
1681}
1682
1683void CastExpr::setCastPath(const CXXCastPath &Path) {
1684 assert(Path.size() == path_size());
1685 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1686}
1687
Craig Topper37932912013-08-18 10:09:15 +00001688ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001689 CastKind Kind, Expr *Operand,
1690 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001691 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001692 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1693 void *Buffer =
1694 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1695 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001696 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001697 if (PathSize) E->setCastPath(*BasePath);
1698 return E;
1699}
1700
Craig Topper37932912013-08-18 10:09:15 +00001701ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001702 unsigned PathSize) {
1703 void *Buffer =
1704 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1705 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1706}
1707
1708
Craig Topper37932912013-08-18 10:09:15 +00001709CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001710 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001711 const CXXCastPath *BasePath,
1712 TypeSourceInfo *WrittenTy,
1713 SourceLocation L, SourceLocation R) {
1714 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1715 void *Buffer =
1716 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1717 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001718 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001719 if (PathSize) E->setCastPath(*BasePath);
1720 return E;
1721}
1722
Craig Topper37932912013-08-18 10:09:15 +00001723CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1724 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001725 void *Buffer =
1726 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1727 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1728}
1729
Chris Lattner1b926492006-08-23 06:42:10 +00001730/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1731/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001732StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001733 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001734 case BO_PtrMemD: return ".*";
1735 case BO_PtrMemI: return "->*";
1736 case BO_Mul: return "*";
1737 case BO_Div: return "/";
1738 case BO_Rem: return "%";
1739 case BO_Add: return "+";
1740 case BO_Sub: return "-";
1741 case BO_Shl: return "<<";
1742 case BO_Shr: return ">>";
1743 case BO_LT: return "<";
1744 case BO_GT: return ">";
1745 case BO_LE: return "<=";
1746 case BO_GE: return ">=";
1747 case BO_EQ: return "==";
1748 case BO_NE: return "!=";
1749 case BO_And: return "&";
1750 case BO_Xor: return "^";
1751 case BO_Or: return "|";
1752 case BO_LAnd: return "&&";
1753 case BO_LOr: return "||";
1754 case BO_Assign: return "=";
1755 case BO_MulAssign: return "*=";
1756 case BO_DivAssign: return "/=";
1757 case BO_RemAssign: return "%=";
1758 case BO_AddAssign: return "+=";
1759 case BO_SubAssign: return "-=";
1760 case BO_ShlAssign: return "<<=";
1761 case BO_ShrAssign: return ">>=";
1762 case BO_AndAssign: return "&=";
1763 case BO_XorAssign: return "^=";
1764 case BO_OrAssign: return "|=";
1765 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001766 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001767
David Blaikiee4d798f2012-01-20 21:50:17 +00001768 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001769}
Steve Naroff47500512007-04-19 23:00:49 +00001770
John McCalle3027922010-08-25 11:45:40 +00001771BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001772BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1773 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001774 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001775 case OO_Plus: return BO_Add;
1776 case OO_Minus: return BO_Sub;
1777 case OO_Star: return BO_Mul;
1778 case OO_Slash: return BO_Div;
1779 case OO_Percent: return BO_Rem;
1780 case OO_Caret: return BO_Xor;
1781 case OO_Amp: return BO_And;
1782 case OO_Pipe: return BO_Or;
1783 case OO_Equal: return BO_Assign;
1784 case OO_Less: return BO_LT;
1785 case OO_Greater: return BO_GT;
1786 case OO_PlusEqual: return BO_AddAssign;
1787 case OO_MinusEqual: return BO_SubAssign;
1788 case OO_StarEqual: return BO_MulAssign;
1789 case OO_SlashEqual: return BO_DivAssign;
1790 case OO_PercentEqual: return BO_RemAssign;
1791 case OO_CaretEqual: return BO_XorAssign;
1792 case OO_AmpEqual: return BO_AndAssign;
1793 case OO_PipeEqual: return BO_OrAssign;
1794 case OO_LessLess: return BO_Shl;
1795 case OO_GreaterGreater: return BO_Shr;
1796 case OO_LessLessEqual: return BO_ShlAssign;
1797 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1798 case OO_EqualEqual: return BO_EQ;
1799 case OO_ExclaimEqual: return BO_NE;
1800 case OO_LessEqual: return BO_LE;
1801 case OO_GreaterEqual: return BO_GE;
1802 case OO_AmpAmp: return BO_LAnd;
1803 case OO_PipePipe: return BO_LOr;
1804 case OO_Comma: return BO_Comma;
1805 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001806 }
1807}
1808
1809OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1810 static const OverloadedOperatorKind OverOps[] = {
1811 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1812 OO_Star, OO_Slash, OO_Percent,
1813 OO_Plus, OO_Minus,
1814 OO_LessLess, OO_GreaterGreater,
1815 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1816 OO_EqualEqual, OO_ExclaimEqual,
1817 OO_Amp,
1818 OO_Caret,
1819 OO_Pipe,
1820 OO_AmpAmp,
1821 OO_PipePipe,
1822 OO_Equal, OO_StarEqual,
1823 OO_SlashEqual, OO_PercentEqual,
1824 OO_PlusEqual, OO_MinusEqual,
1825 OO_LessLessEqual, OO_GreaterGreaterEqual,
1826 OO_AmpEqual, OO_CaretEqual,
1827 OO_PipeEqual,
1828 OO_Comma
1829 };
1830 return OverOps[Opc];
1831}
1832
Craig Topper37932912013-08-18 10:09:15 +00001833InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001834 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001835 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001836 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001837 InitExprs(C, initExprs.size()),
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001838 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001839{
1840 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001841 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001842 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001843 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001844 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001845 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001846 if (initExprs[I]->isInstantiationDependent())
1847 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001848 if (initExprs[I]->containsUnexpandedParameterPack())
1849 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001850 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001851
Benjamin Kramerc215e762012-08-24 11:54:20 +00001852 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001853}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001854
Craig Topper37932912013-08-18 10:09:15 +00001855void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001856 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001857 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001858}
1859
Craig Topper37932912013-08-18 10:09:15 +00001860void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001861 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001862}
1863
Craig Topper37932912013-08-18 10:09:15 +00001864Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001865 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001866 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001867 InitExprs.back() = expr;
1868 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001869 }
Mike Stump11289f42009-09-09 15:08:12 +00001870
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001871 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1872 InitExprs[Init] = expr;
1873 return Result;
1874}
1875
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001876void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001877 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001878 ArrayFillerOrUnionFieldInit = filler;
1879 // Fill out any "holes" in the array due to designated initializers.
1880 Expr **inits = getInits();
1881 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1882 if (inits[i] == 0)
1883 inits[i] = filler;
1884}
1885
Richard Smith9ec1e482012-04-15 02:50:59 +00001886bool InitListExpr::isStringLiteralInit() const {
1887 if (getNumInits() != 1)
1888 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001889 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1890 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001891 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001892 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001893 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1894}
1895
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001896SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001897 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001898 return SyntacticForm->getLocStart();
1899 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001900 if (Beg.isInvalid()) {
1901 // Find the first non-null initializer.
1902 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1903 E = InitExprs.end();
1904 I != E; ++I) {
1905 if (Stmt *S = *I) {
1906 Beg = S->getLocStart();
1907 break;
1908 }
1909 }
1910 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001911 return Beg;
1912}
1913
1914SourceLocation InitListExpr::getLocEnd() const {
1915 if (InitListExpr *SyntacticForm = getSyntacticForm())
1916 return SyntacticForm->getLocEnd();
1917 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001918 if (End.isInvalid()) {
1919 // Find the first non-null initializer from the end.
1920 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001921 E = InitExprs.rend();
1922 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001923 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001924 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001925 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001926 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001927 }
1928 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001929 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001930}
1931
Steve Naroff991e99d2008-09-04 15:31:07 +00001932/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001933///
John McCallc833dea2012-02-17 03:32:35 +00001934const FunctionProtoType *BlockExpr::getFunctionType() const {
1935 // The block pointer is never sugared, but the function type might be.
1936 return cast<BlockPointerType>(getType())
1937 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001938}
1939
Mike Stump11289f42009-09-09 15:08:12 +00001940SourceLocation BlockExpr::getCaretLocation() const {
1941 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001942}
Mike Stump11289f42009-09-09 15:08:12 +00001943const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001944 return TheBlock->getBody();
1945}
Mike Stump11289f42009-09-09 15:08:12 +00001946Stmt *BlockExpr::getBody() {
1947 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001948}
Steve Naroff415d3d52008-10-08 17:01:13 +00001949
1950
Chris Lattner1ec5f562007-06-27 05:38:08 +00001951//===----------------------------------------------------------------------===//
1952// Generic Expression Routines
1953//===----------------------------------------------------------------------===//
1954
Chris Lattner237f2752009-02-14 07:37:35 +00001955/// isUnusedResultAWarning - Return true if this immediate expression should
1956/// be warned about if the result is unused. If so, fill in Loc and Ranges
1957/// with location to warn on and the source range[s] to report with the
1958/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00001959bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1960 SourceRange &R1, SourceRange &R2,
1961 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001962 // Don't warn if the expr is type dependent. The type could end up
1963 // instantiating to void.
1964 if (isTypeDependent())
1965 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001966
Chris Lattner1ec5f562007-06-27 05:38:08 +00001967 switch (getStmtClass()) {
1968 default:
John McCallc493a732010-03-12 07:11:26 +00001969 if (getType()->isVoidType())
1970 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001971 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001972 Loc = getExprLoc();
1973 R1 = getSourceRange();
1974 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001975 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001976 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001977 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001978 case GenericSelectionExprClass:
1979 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001980 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00001981 case ChooseExprClass:
1982 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1983 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001984 case UnaryOperatorClass: {
1985 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001986
Chris Lattner1ec5f562007-06-27 05:38:08 +00001987 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001988 case UO_Plus:
1989 case UO_Minus:
1990 case UO_AddrOf:
1991 case UO_Not:
1992 case UO_LNot:
1993 case UO_Deref:
1994 break;
John McCalle3027922010-08-25 11:45:40 +00001995 case UO_PostInc:
1996 case UO_PostDec:
1997 case UO_PreInc:
1998 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001999 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00002000 case UO_Real:
2001 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00002002 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00002003 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2004 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00002005 return false;
2006 break;
John McCalle3027922010-08-25 11:45:40 +00002007 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002008 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002009 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002010 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002011 Loc = UO->getOperatorLoc();
2012 R1 = UO->getSubExpr()->getSourceRange();
2013 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002014 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00002015 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002016 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00002017 switch (BO->getOpcode()) {
2018 default:
2019 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002020 // Consider the RHS of comma for side effects. LHS was checked by
2021 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002022 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002023 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2024 // lvalue-ness) of an assignment written in a macro.
2025 if (IntegerLiteral *IE =
2026 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2027 if (IE->getValue() == 0)
2028 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002029 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002030 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002031 case BO_LAnd:
2032 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002033 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2034 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002035 return false;
2036 break;
John McCall1e3715a2010-02-16 04:10:53 +00002037 }
Chris Lattner237f2752009-02-14 07:37:35 +00002038 if (BO->isAssignmentOp())
2039 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002040 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002041 Loc = BO->getOperatorLoc();
2042 R1 = BO->getLHS()->getSourceRange();
2043 R2 = BO->getRHS()->getSourceRange();
2044 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002045 }
Chris Lattner86928112007-08-25 02:00:02 +00002046 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002047 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002048 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002049 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002050
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002051 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002052 // If only one of the LHS or RHS is a warning, the operator might
2053 // be being used for control flow. Only warn if both the LHS and
2054 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002055 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002056 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002057 return false;
2058 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002059 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002060 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002061 }
2062
Chris Lattnera44d1162007-06-27 05:58:59 +00002063 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002064 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002065 Loc = cast<MemberExpr>(this)->getMemberLoc();
2066 R1 = SourceRange(Loc, Loc);
2067 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2068 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002069
Chris Lattner1ec5f562007-06-27 05:38:08 +00002070 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002071 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002072 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2073 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2074 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2075 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002076
Chandler Carruth46339472011-08-17 09:49:44 +00002077 case CXXOperatorCallExprClass: {
2078 // We warn about operator== and operator!= even when user-defined operator
2079 // overloads as there is no reasonable way to define these such that they
2080 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2081 // warning: these operators are commonly typo'ed, and so warning on them
2082 // provides additional value as well. If this list is updated,
2083 // DiagnoseUnusedComparison should be as well.
2084 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2085 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002086 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002087 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002088 Loc = Op->getOperatorLoc();
2089 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002090 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002091 }
Chandler Carruth46339472011-08-17 09:49:44 +00002092
2093 // Fallthrough for generic call handling.
2094 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002095 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002096 case CXXMemberCallExprClass:
2097 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002098 // If this is a direct call, get the callee.
2099 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002100 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002101 // If the callee has attribute pure, const, or warn_unused_result, warn
2102 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002103 //
2104 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2105 // updated to match for QoI.
2106 if (FD->getAttr<WarnUnusedResultAttr>() ||
2107 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002108 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002109 Loc = CE->getCallee()->getLocStart();
2110 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002111
Chris Lattner1a6babf2009-10-13 04:53:48 +00002112 if (unsigned NumArgs = CE->getNumArgs())
2113 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2114 CE->getArg(NumArgs-1)->getLocEnd());
2115 return true;
2116 }
Chris Lattner237f2752009-02-14 07:37:35 +00002117 }
2118 return false;
2119 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002120
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002121 // If we don't know precisely what we're looking at, let's not warn.
2122 case UnresolvedLookupExprClass:
2123 case CXXUnresolvedConstructExprClass:
2124 return false;
2125
Anders Carlsson6aa50392009-11-17 17:11:23 +00002126 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002127 case CXXConstructExprClass: {
2128 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2129 if (Type->hasAttr<WarnUnusedAttr>()) {
2130 WarnE = this;
2131 Loc = getLocStart();
2132 R1 = getSourceRange();
2133 return true;
2134 }
2135 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002136 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002137 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002138
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002139 case ObjCMessageExprClass: {
2140 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002141 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002142 ME->isInstanceMessage() &&
2143 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002144 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002145 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002146 Loc = getExprLoc();
2147 R1 = ME->getSourceRange();
2148 return true;
2149 }
2150
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002151 const ObjCMethodDecl *MD = ME->getMethodDecl();
2152 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002153 WarnE = this;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002154 Loc = getExprLoc();
2155 return true;
2156 }
Chris Lattner237f2752009-02-14 07:37:35 +00002157 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002158 }
Mike Stump11289f42009-09-09 15:08:12 +00002159
John McCallb7bd14f2010-12-02 01:19:52 +00002160 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002161 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002162 Loc = getExprLoc();
2163 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002164 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002165
John McCallfe96e0b2011-11-06 09:01:30 +00002166 case PseudoObjectExprClass: {
2167 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2168
2169 // Only complain about things that have the form of a getter.
2170 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2171 isa<BinaryOperator>(PO->getSyntacticForm()))
2172 return false;
2173
Eli Friedmanc11535c2012-05-24 00:47:05 +00002174 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002175 Loc = getExprLoc();
2176 R1 = getSourceRange();
2177 return true;
2178 }
2179
Chris Lattner944d3062008-07-26 19:51:01 +00002180 case StmtExprClass: {
2181 // Statement exprs don't logically have side effects themselves, but are
2182 // sometimes used in macros in ways that give them a type that is unused.
2183 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2184 // however, if the result of the stmt expr is dead, we don't want to emit a
2185 // warning.
2186 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002187 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002188 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002189 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002190 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2191 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002192 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002193 }
Mike Stump11289f42009-09-09 15:08:12 +00002194
John McCallc493a732010-03-12 07:11:26 +00002195 if (getType()->isVoidType())
2196 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002197 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002198 Loc = cast<StmtExpr>(this)->getLParenLoc();
2199 R1 = getSourceRange();
2200 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002201 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002202 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002203 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002204 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002205 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002206 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002207 if (CE->getCastKind() == CK_ToVoid) {
2208 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002209 CE->getSubExpr()->getType().isVolatileQualified()) {
2210 const DeclRefExpr *DRE =
2211 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2212 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2213 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2214 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2215 R1, R2, Ctx);
2216 }
2217 }
Chris Lattner2706a552009-07-28 18:25:28 +00002218 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002219 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002220
Eli Friedmanc11535c2012-05-24 00:47:05 +00002221 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002222 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002223 if (CE->getCastKind() == CK_ConstructorConversion)
2224 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002225
Eli Friedmanc11535c2012-05-24 00:47:05 +00002226 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002227 if (const CXXFunctionalCastExpr *CXXCE =
2228 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002229 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002230 R1 = CXXCE->getSubExpr()->getSourceRange();
2231 } else {
2232 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2233 Loc = CStyleCE->getLParenLoc();
2234 R1 = CStyleCE->getSubExpr()->getSourceRange();
2235 }
Chris Lattner237f2752009-02-14 07:37:35 +00002236 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002237 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002238 case ImplicitCastExprClass: {
2239 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002240
Eli Friedmanc11535c2012-05-24 00:47:05 +00002241 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2242 if (ICE->getCastKind() == CK_LValueToRValue &&
2243 ICE->getSubExpr()->getType().isVolatileQualified())
2244 return false;
2245
2246 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2247 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002248 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002249 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002250 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002251 case CXXDefaultInitExprClass:
2252 return (cast<CXXDefaultInitExpr>(this)
2253 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002254
2255 case CXXNewExprClass:
2256 // FIXME: In theory, there might be new expressions that don't have side
2257 // effects (e.g. a placement new with an uninitialized POD).
2258 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002259 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002260 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002261 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002262 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002263 case ExprWithCleanupsClass:
2264 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002265 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002266 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002267}
2268
Fariborz Jahanian07735332009-02-22 18:40:18 +00002269/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002270/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002271bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002272 const Expr *E = IgnoreParens();
2273 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002274 default:
2275 return false;
2276 case ObjCIvarRefExprClass:
2277 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002278 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002279 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002280 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002281 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002282 case MaterializeTemporaryExprClass:
2283 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2284 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002285 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002286 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002287 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002288 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002289
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002290 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2291 if (VD->hasGlobalStorage())
2292 return true;
2293 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002294 // dereferencing to a pointer is always a gc'able candidate,
2295 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002296 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002297 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002298 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002299 return false;
2300 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002301 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002302 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002303 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002304 }
2305 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002306 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002307 }
2308}
Sebastian Redlce354af2010-09-10 20:55:33 +00002309
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002310bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2311 if (isTypeDependent())
2312 return false;
John McCall086a4642010-11-24 05:12:34 +00002313 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002314}
2315
John McCall0009fcc2011-04-26 20:42:42 +00002316QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002317 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002318
2319 // Bound member expressions are always one of these possibilities:
2320 // x->m x.m x->*y x.*y
2321 // (possibly parenthesized)
2322
2323 expr = expr->IgnoreParens();
2324 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2325 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2326 return mem->getMemberDecl()->getType();
2327 }
2328
2329 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2330 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2331 ->getPointeeType();
2332 assert(type->isFunctionType());
2333 return type;
2334 }
2335
2336 assert(isa<UnresolvedMemberExpr>(expr));
2337 return QualType();
2338}
2339
Ted Kremenekfff70962008-01-17 16:57:34 +00002340Expr* Expr::IgnoreParens() {
2341 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002342 while (true) {
2343 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2344 E = P->getSubExpr();
2345 continue;
2346 }
2347 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2348 if (P->getOpcode() == UO_Extension) {
2349 E = P->getSubExpr();
2350 continue;
2351 }
2352 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002353 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2354 if (!P->isResultDependent()) {
2355 E = P->getResultExpr();
2356 continue;
2357 }
2358 }
Eli Friedman75807f22013-07-20 00:40:58 +00002359 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2360 if (!P->isConditionDependent()) {
2361 E = P->getChosenSubExpr();
2362 continue;
2363 }
2364 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002365 return E;
2366 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002367}
2368
Chris Lattnerf2660962008-02-13 01:02:39 +00002369/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2370/// or CastExprs or ImplicitCastExprs, returning their operand.
2371Expr *Expr::IgnoreParenCasts() {
2372 Expr *E = this;
2373 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002374 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002375 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002376 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002377 continue;
2378 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002379 if (MaterializeTemporaryExpr *Materialize
2380 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2381 E = Materialize->GetTemporaryExpr();
2382 continue;
2383 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002384 if (SubstNonTypeTemplateParmExpr *NTTP
2385 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2386 E = NTTP->getReplacement();
2387 continue;
2388 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002389 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002390 }
2391}
2392
John McCall5a4ce8b2010-12-04 08:24:19 +00002393/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2394/// casts. This is intended purely as a temporary workaround for code
2395/// that hasn't yet been rewritten to do the right thing about those
2396/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002397Expr *Expr::IgnoreParenLValueCasts() {
2398 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002399 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002400 E = E->IgnoreParens();
2401 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002402 if (P->getCastKind() == CK_LValueToRValue) {
2403 E = P->getSubExpr();
2404 continue;
2405 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002406 } else if (MaterializeTemporaryExpr *Materialize
2407 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2408 E = Materialize->GetTemporaryExpr();
2409 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002410 } else if (SubstNonTypeTemplateParmExpr *NTTP
2411 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2412 E = NTTP->getReplacement();
2413 continue;
John McCall34376a62010-12-04 03:47:34 +00002414 }
2415 break;
2416 }
2417 return E;
2418}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002419
2420Expr *Expr::ignoreParenBaseCasts() {
2421 Expr *E = this;
2422 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002423 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002424 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2425 if (CE->getCastKind() == CK_DerivedToBase ||
2426 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2427 CE->getCastKind() == CK_NoOp) {
2428 E = CE->getSubExpr();
2429 continue;
2430 }
2431 }
2432
2433 return E;
2434 }
2435}
2436
John McCalleebc8322010-05-05 22:59:52 +00002437Expr *Expr::IgnoreParenImpCasts() {
2438 Expr *E = this;
2439 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002440 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002441 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002442 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002443 continue;
2444 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002445 if (MaterializeTemporaryExpr *Materialize
2446 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2447 E = Materialize->GetTemporaryExpr();
2448 continue;
2449 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002450 if (SubstNonTypeTemplateParmExpr *NTTP
2451 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2452 E = NTTP->getReplacement();
2453 continue;
2454 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002455 return E;
John McCalleebc8322010-05-05 22:59:52 +00002456 }
2457}
2458
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002459Expr *Expr::IgnoreConversionOperator() {
2460 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002461 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002462 return MCE->getImplicitObjectArgument();
2463 }
2464 return this;
2465}
2466
Chris Lattneref26c772009-03-13 17:28:01 +00002467/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2468/// value (including ptr->int casts of the same size). Strip off any
2469/// ParenExpr or CastExprs, returning their operand.
2470Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2471 Expr *E = this;
2472 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002473 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002474
Chris Lattneref26c772009-03-13 17:28:01 +00002475 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2476 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002477 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002478 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002479
Chris Lattneref26c772009-03-13 17:28:01 +00002480 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2481 E = SE;
2482 continue;
2483 }
Mike Stump11289f42009-09-09 15:08:12 +00002484
Abramo Bagnara932e3932010-10-15 07:51:18 +00002485 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002486 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002487 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002488 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002489 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2490 E = SE;
2491 continue;
2492 }
2493 }
Mike Stump11289f42009-09-09 15:08:12 +00002494
Douglas Gregor6a40b082011-09-08 17:56:33 +00002495 if (SubstNonTypeTemplateParmExpr *NTTP
2496 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2497 E = NTTP->getReplacement();
2498 continue;
2499 }
2500
Chris Lattneref26c772009-03-13 17:28:01 +00002501 return E;
2502 }
2503}
2504
Douglas Gregord196a582009-12-14 19:27:10 +00002505bool Expr::isDefaultArgument() const {
2506 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002507 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2508 E = M->GetTemporaryExpr();
2509
Douglas Gregord196a582009-12-14 19:27:10 +00002510 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2511 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002512
Douglas Gregord196a582009-12-14 19:27:10 +00002513 return isa<CXXDefaultArgExpr>(E);
2514}
Chris Lattneref26c772009-03-13 17:28:01 +00002515
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002516/// \brief Skip over any no-op casts and any temporary-binding
2517/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002518static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002519 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2520 E = M->GetTemporaryExpr();
2521
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002522 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002523 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002524 E = ICE->getSubExpr();
2525 else
2526 break;
2527 }
2528
2529 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2530 E = BE->getSubExpr();
2531
2532 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002533 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002534 E = ICE->getSubExpr();
2535 else
2536 break;
2537 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002538
2539 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002540}
2541
John McCall7a626f62010-09-15 10:14:12 +00002542/// isTemporaryObject - Determines if this expression produces a
2543/// temporary of the given class type.
2544bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2545 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2546 return false;
2547
Anders Carlsson66bbf502010-11-28 16:40:49 +00002548 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002549
John McCall02dc8c72010-09-15 20:59:13 +00002550 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002551 if (!E->Classify(C).isPRValue()) {
2552 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002553 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002554 return false;
2555 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002556
John McCallf4ee1dd2010-09-16 06:57:56 +00002557 // Black-list a few cases which yield pr-values of class type that don't
2558 // refer to temporaries of that type:
2559
2560 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002561 if (isa<ImplicitCastExpr>(E)) {
2562 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2563 case CK_DerivedToBase:
2564 case CK_UncheckedDerivedToBase:
2565 return false;
2566 default:
2567 break;
2568 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002569 }
2570
John McCallf4ee1dd2010-09-16 06:57:56 +00002571 // - member expressions (all)
2572 if (isa<MemberExpr>(E))
2573 return false;
2574
Eli Friedman13ffdd82012-06-15 23:51:06 +00002575 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2576 if (BO->isPtrMemOp())
2577 return false;
2578
John McCallc07a0c72011-02-17 10:25:35 +00002579 // - opaque values (all)
2580 if (isa<OpaqueValueExpr>(E))
2581 return false;
2582
John McCall7a626f62010-09-15 10:14:12 +00002583 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002584}
2585
Douglas Gregor25b7e052011-03-02 21:06:53 +00002586bool Expr::isImplicitCXXThis() const {
2587 const Expr *E = this;
2588
2589 // Strip away parentheses and casts we don't care about.
2590 while (true) {
2591 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2592 E = Paren->getSubExpr();
2593 continue;
2594 }
2595
2596 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2597 if (ICE->getCastKind() == CK_NoOp ||
2598 ICE->getCastKind() == CK_LValueToRValue ||
2599 ICE->getCastKind() == CK_DerivedToBase ||
2600 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2601 E = ICE->getSubExpr();
2602 continue;
2603 }
2604 }
2605
2606 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2607 if (UnOp->getOpcode() == UO_Extension) {
2608 E = UnOp->getSubExpr();
2609 continue;
2610 }
2611 }
2612
Douglas Gregorfe314812011-06-21 17:03:29 +00002613 if (const MaterializeTemporaryExpr *M
2614 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2615 E = M->GetTemporaryExpr();
2616 continue;
2617 }
2618
Douglas Gregor25b7e052011-03-02 21:06:53 +00002619 break;
2620 }
2621
2622 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2623 return This->isImplicit();
2624
2625 return false;
2626}
2627
Douglas Gregor4619e432008-12-05 23:32:09 +00002628/// hasAnyTypeDependentArguments - Determines if any of the expressions
2629/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002630bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002631 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002632 if (Exprs[I]->isTypeDependent())
2633 return true;
2634
2635 return false;
2636}
2637
John McCall8b0f4ff2010-08-02 21:13:48 +00002638bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002639 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002640 // which can be evaluated at compile-time. It very closely parallels
2641 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2642 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2643 // to isEvaluatable most of the time.
2644 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002645 // If we ever capture reference-binding directly in the AST, we can
2646 // kill the second parameter.
2647
2648 if (IsForRef) {
2649 EvalResult Result;
2650 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2651 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002652
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002653 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002654 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002655 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002656 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002657 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002658 case CXXTemporaryObjectExprClass:
2659 case CXXConstructExprClass: {
2660 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002661
Eli Friedman4c27ac22013-07-16 22:40:53 +00002662 if (CE->getConstructor()->isTrivial() &&
2663 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2664 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002665 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002666
Eli Friedman4c27ac22013-07-16 22:40:53 +00002667 // Trivial copy constructor
2668 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2669 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smithd62306a2011-11-10 06:34:14 +00002670 }
2671
Richard Smithd62306a2011-11-10 06:34:14 +00002672 break;
John McCall81c9cea2010-08-01 21:51:45 +00002673 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002674 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002675 // This handles gcc's extension that allows global initializers like
2676 // "struct x {int x;} x = (struct x) {};".
2677 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002678 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002679 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002680 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002681 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002682 const InitListExpr *ILE = cast<InitListExpr>(this);
2683 if (ILE->getType()->isArrayType()) {
2684 unsigned numInits = ILE->getNumInits();
2685 for (unsigned i = 0; i < numInits; i++) {
2686 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2687 return false;
2688 }
2689 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002690 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002691
2692 if (ILE->getType()->isRecordType()) {
2693 unsigned ElementNo = 0;
2694 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2695 for (RecordDecl::field_iterator Field = RD->field_begin(),
2696 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2697 // If this is a union, skip all the fields that aren't being initialized.
2698 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2699 continue;
2700
2701 // Don't emit anonymous bitfields, they just affect layout.
2702 if (Field->isUnnamedBitfield())
2703 continue;
2704
2705 if (ElementNo < ILE->getNumInits()) {
2706 const Expr *Elt = ILE->getInit(ElementNo++);
2707 if (Field->isBitField()) {
2708 // Bitfields have to evaluate to an integer.
2709 llvm::APSInt ResultTmp;
2710 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2711 return false;
2712 } else {
2713 bool RefType = Field->getType()->isReferenceType();
2714 if (!Elt->isConstantInitializer(Ctx, RefType))
2715 return false;
2716 }
2717 }
2718 }
2719 return true;
2720 }
2721
2722 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002723 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002724 case ImplicitValueInitExprClass:
2725 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002726 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002727 return cast<ParenExpr>(this)->getSubExpr()
2728 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002729 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002730 return cast<GenericSelectionExpr>(this)->getResultExpr()
2731 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002732 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002733 if (cast<ChooseExpr>(this)->isConditionDependent())
2734 return false;
2735 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002736 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002737 case UnaryOperatorClass: {
2738 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002739 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002740 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002741 break;
2742 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002743 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002744 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002745 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002746 case CStyleCastExprClass:
2747 case ObjCBridgedCastExprClass:
2748 case CXXDynamicCastExprClass:
2749 case CXXReinterpretCastExprClass:
2750 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002751 const CastExpr *CE = cast<CastExpr>(this);
2752
Eli Friedman13ec75b2011-12-21 00:43:02 +00002753 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002754 if (CE->getCastKind() == CK_NoOp ||
2755 CE->getCastKind() == CK_LValueToRValue ||
2756 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002757 CE->getCastKind() == CK_ConstructorConversion ||
2758 CE->getCastKind() == CK_NonAtomicToAtomic ||
2759 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smith161f09a2011-12-06 22:44:34 +00002760 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2761
Eli Friedman384da272009-01-25 03:12:18 +00002762 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002763 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002764 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002765 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002766 ->isConstantInitializer(Ctx, false);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002767
2768 case SubstNonTypeTemplateParmExprClass:
2769 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2770 ->isConstantInitializer(Ctx, false);
2771 case CXXDefaultArgExprClass:
2772 return cast<CXXDefaultArgExpr>(this)->getExpr()
2773 ->isConstantInitializer(Ctx, false);
2774 case CXXDefaultInitExprClass:
2775 return cast<CXXDefaultInitExpr>(this)->getExpr()
2776 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002777 }
Eli Friedman384da272009-01-25 03:12:18 +00002778 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002779}
2780
Richard Smith0421ce72012-08-07 04:16:51 +00002781bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2782 if (isInstantiationDependent())
2783 return true;
2784
2785 switch (getStmtClass()) {
2786 case NoStmtClass:
2787 #define ABSTRACT_STMT(Type)
2788 #define STMT(Type, Base) case Type##Class:
2789 #define EXPR(Type, Base)
2790 #include "clang/AST/StmtNodes.inc"
2791 llvm_unreachable("unexpected Expr kind");
2792
2793 case DependentScopeDeclRefExprClass:
2794 case CXXUnresolvedConstructExprClass:
2795 case CXXDependentScopeMemberExprClass:
2796 case UnresolvedLookupExprClass:
2797 case UnresolvedMemberExprClass:
2798 case PackExpansionExprClass:
2799 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002800 case FunctionParmPackExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002801 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2802
Richard Smitha33e4fe2012-08-07 05:18:29 +00002803 case DeclRefExprClass:
2804 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002805 case PredefinedExprClass:
2806 case IntegerLiteralClass:
2807 case FloatingLiteralClass:
2808 case ImaginaryLiteralClass:
2809 case StringLiteralClass:
2810 case CharacterLiteralClass:
2811 case OffsetOfExprClass:
2812 case ImplicitValueInitExprClass:
2813 case UnaryExprOrTypeTraitExprClass:
2814 case AddrLabelExprClass:
2815 case GNUNullExprClass:
2816 case CXXBoolLiteralExprClass:
2817 case CXXNullPtrLiteralExprClass:
2818 case CXXThisExprClass:
2819 case CXXScalarValueInitExprClass:
2820 case TypeTraitExprClass:
2821 case UnaryTypeTraitExprClass:
2822 case BinaryTypeTraitExprClass:
2823 case ArrayTypeTraitExprClass:
2824 case ExpressionTraitExprClass:
2825 case CXXNoexceptExprClass:
2826 case SizeOfPackExprClass:
2827 case ObjCStringLiteralClass:
2828 case ObjCEncodeExprClass:
2829 case ObjCBoolLiteralExprClass:
2830 case CXXUuidofExprClass:
2831 case OpaqueValueExprClass:
2832 // These never have a side-effect.
2833 return false;
2834
2835 case CallExprClass:
John McCall5e77d762013-04-16 07:28:30 +00002836 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002837 case CompoundAssignOperatorClass:
2838 case VAArgExprClass:
2839 case AtomicExprClass:
2840 case StmtExprClass:
2841 case CXXOperatorCallExprClass:
2842 case CXXMemberCallExprClass:
2843 case UserDefinedLiteralClass:
2844 case CXXThrowExprClass:
2845 case CXXNewExprClass:
2846 case CXXDeleteExprClass:
2847 case ExprWithCleanupsClass:
2848 case CXXBindTemporaryExprClass:
2849 case BlockExprClass:
2850 case CUDAKernelCallExprClass:
2851 // These always have a side-effect.
2852 return true;
2853
2854 case ParenExprClass:
2855 case ArraySubscriptExprClass:
2856 case MemberExprClass:
2857 case ConditionalOperatorClass:
2858 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002859 case CompoundLiteralExprClass:
2860 case ExtVectorElementExprClass:
2861 case DesignatedInitExprClass:
2862 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002863 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00002864 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002865 case SubstNonTypeTemplateParmExprClass:
2866 case MaterializeTemporaryExprClass:
2867 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00002868 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002869 case AsTypeExprClass:
2870 // These have a side-effect if any subexpression does.
2871 break;
2872
Richard Smitha33e4fe2012-08-07 05:18:29 +00002873 case UnaryOperatorClass:
2874 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002875 return true;
2876 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002877
2878 case BinaryOperatorClass:
2879 if (cast<BinaryOperator>(this)->isAssignmentOp())
2880 return true;
2881 break;
2882
Richard Smith0421ce72012-08-07 04:16:51 +00002883 case InitListExprClass:
2884 // FIXME: The children for an InitListExpr doesn't include the array filler.
2885 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2886 if (E->HasSideEffects(Ctx))
2887 return true;
2888 break;
2889
2890 case GenericSelectionExprClass:
2891 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2892 HasSideEffects(Ctx);
2893
2894 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002895 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith0421ce72012-08-07 04:16:51 +00002896
2897 case CXXDefaultArgExprClass:
2898 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2899
Richard Smith852c9db2013-04-20 22:23:05 +00002900 case CXXDefaultInitExprClass:
2901 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2902 return E->HasSideEffects(Ctx);
2903 // If we've not yet parsed the initializer, assume it has side-effects.
2904 return true;
2905
Richard Smith0421ce72012-08-07 04:16:51 +00002906 case CXXDynamicCastExprClass: {
2907 // A dynamic_cast expression has side-effects if it can throw.
2908 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2909 if (DCE->getTypeAsWritten()->isReferenceType() &&
2910 DCE->getCastKind() == CK_Dynamic)
2911 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002912 } // Fall through.
2913 case ImplicitCastExprClass:
2914 case CStyleCastExprClass:
2915 case CXXStaticCastExprClass:
2916 case CXXReinterpretCastExprClass:
2917 case CXXConstCastExprClass:
2918 case CXXFunctionalCastExprClass: {
2919 const CastExpr *CE = cast<CastExpr>(this);
2920 if (CE->getCastKind() == CK_LValueToRValue &&
2921 CE->getSubExpr()->getType().isVolatileQualified())
2922 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00002923 break;
2924 }
2925
Richard Smithef8bf432012-08-13 20:08:14 +00002926 case CXXTypeidExprClass:
2927 // typeid might throw if its subexpression is potentially-evaluated, so has
2928 // side-effects in that case whether or not its subexpression does.
2929 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00002930
2931 case CXXConstructExprClass:
2932 case CXXTemporaryObjectExprClass: {
2933 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smitha33e4fe2012-08-07 05:18:29 +00002934 if (!CE->getConstructor()->isTrivial())
Richard Smith0421ce72012-08-07 04:16:51 +00002935 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002936 // A trivial constructor does not add any side-effects of its own. Just look
2937 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00002938 break;
2939 }
2940
2941 case LambdaExprClass: {
2942 const LambdaExpr *LE = cast<LambdaExpr>(this);
2943 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2944 E = LE->capture_end(); I != E; ++I)
2945 if (I->getCaptureKind() == LCK_ByCopy)
2946 // FIXME: Only has a side-effect if the variable is volatile or if
2947 // the copy would invoke a non-trivial copy constructor.
2948 return true;
2949 return false;
2950 }
2951
2952 case PseudoObjectExprClass: {
2953 // Only look for side-effects in the semantic form, and look past
2954 // OpaqueValueExpr bindings in that form.
2955 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2956 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2957 E = PO->semantics_end();
2958 I != E; ++I) {
2959 const Expr *Subexpr = *I;
2960 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2961 Subexpr = OVE->getSourceExpr();
2962 if (Subexpr->HasSideEffects(Ctx))
2963 return true;
2964 }
2965 return false;
2966 }
2967
2968 case ObjCBoxedExprClass:
2969 case ObjCArrayLiteralClass:
2970 case ObjCDictionaryLiteralClass:
2971 case ObjCMessageExprClass:
2972 case ObjCSelectorExprClass:
2973 case ObjCProtocolExprClass:
2974 case ObjCPropertyRefExprClass:
2975 case ObjCIsaExprClass:
2976 case ObjCIndirectCopyRestoreExprClass:
2977 case ObjCSubscriptRefExprClass:
2978 case ObjCBridgedCastExprClass:
2979 // FIXME: Classify these cases better.
2980 return true;
2981 }
2982
2983 // Recurse to children.
2984 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2985 if (const Stmt *S = *SubStmts)
2986 if (cast<Expr>(S)->HasSideEffects(Ctx))
2987 return true;
2988
2989 return false;
2990}
2991
Douglas Gregor1be329d2012-02-23 07:33:15 +00002992namespace {
2993 /// \brief Look for a call to a non-trivial function within an expression.
2994 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2995 {
2996 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2997
2998 bool NonTrivial;
2999
3000 public:
3001 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00003002 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00003003
3004 bool hasNonTrivialCall() const { return NonTrivial; }
3005
3006 void VisitCallExpr(CallExpr *E) {
3007 if (CXXMethodDecl *Method
3008 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3009 if (Method->isTrivial()) {
3010 // Recurse to children of the call.
3011 Inherited::VisitStmt(E);
3012 return;
3013 }
3014 }
3015
3016 NonTrivial = true;
3017 }
3018
3019 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3020 if (E->getConstructor()->isTrivial()) {
3021 // Recurse to children of the call.
3022 Inherited::VisitStmt(E);
3023 return;
3024 }
3025
3026 NonTrivial = true;
3027 }
3028
3029 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3030 if (E->getTemporary()->getDestructor()->isTrivial()) {
3031 Inherited::VisitStmt(E);
3032 return;
3033 }
3034
3035 NonTrivial = true;
3036 }
3037 };
3038}
3039
3040bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3041 NonTrivialCallFinder Finder(Ctx);
3042 Finder.Visit(this);
3043 return Finder.hasNonTrivialCall();
3044}
3045
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003046/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3047/// pointer constant or not, as well as the specific kind of constant detected.
3048/// Null pointer constants can be integer constant expressions with the
3049/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3050/// (a GNU extension).
3051Expr::NullPointerConstantKind
3052Expr::isNullPointerConstant(ASTContext &Ctx,
3053 NullPointerConstantValueDependence NPC) const {
Richard Smith4055de42013-06-13 02:46:14 +00003054 if (isValueDependent() && !Ctx.getLangOpts().CPlusPlus11) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003055 switch (NPC) {
3056 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003057 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003058 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003059 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003060 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003061 else
3062 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003063
Douglas Gregor56751b52009-09-25 04:25:58 +00003064 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003065 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003066 }
3067 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003068
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003069 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003070 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003071 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003072 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003073 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003074 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003075 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003076 Pointee->isVoidType() && // to void*
3077 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003078 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003079 }
Steve Naroffada7d422007-05-20 17:54:12 +00003080 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003081 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3082 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003083 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003084 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3085 // Accept ((void*)0) as a null pointer constant, as many other
3086 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003087 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003088 } else if (const GenericSelectionExpr *GE =
3089 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003090 if (GE->isResultDependent())
3091 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003092 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003093 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3094 if (CE->isConditionDependent())
3095 return NPCK_NotNull;
3096 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003097 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003098 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003099 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003100 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003101 } else if (const CXXDefaultInitExpr *DefaultInit
3102 = dyn_cast<CXXDefaultInitExpr>(this)) {
3103 // See through default initializer expressions.
3104 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003105 } else if (isa<GNUNullExpr>(this)) {
3106 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003107 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003108 } else if (const MaterializeTemporaryExpr *M
3109 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3110 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003111 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3112 if (const Expr *Source = OVE->getSourceExpr())
3113 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003114 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003115
Richard Smith89645bc2013-01-02 12:01:23 +00003116 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003117 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003118 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003119
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003120 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003121 if (!Ctx.getLangOpts().CPlusPlus11 &&
3122 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003123 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3124 const Expr *InitExpr = CLE->getInitializer();
3125 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3126 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3127 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003128 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003129 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003130 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003131 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003132
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003133 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003134 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3135 // value zero or a prvalue of type std::nullptr_t.
3136 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
3137 return (Lit && !Lit->getValue()) ? NPCK_ZeroLiteral : NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003138 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003139 // If we have an integer constant expression, we need to *evaluate* it and
3140 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003141 if (!isIntegerConstantExpr(Ctx))
3142 return NPCK_NotNull;
3143 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003144
David Blaikie1c7c8f72012-08-08 17:33:31 +00003145 if (EvaluateKnownConstInt(Ctx) != 0)
3146 return NPCK_NotNull;
3147
3148 if (isa<IntegerLiteral>(this))
3149 return NPCK_ZeroLiteral;
3150 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003151}
Steve Narofff7a5da12007-07-28 23:10:27 +00003152
John McCall34376a62010-12-04 03:47:34 +00003153/// \brief If this expression is an l-value for an Objective C
3154/// property, find the underlying property reference expression.
3155const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3156 const Expr *E = this;
3157 while (true) {
3158 assert((E->getValueKind() == VK_LValue &&
3159 E->getObjectKind() == OK_ObjCProperty) &&
3160 "expression is not a property reference");
3161 E = E->IgnoreParenCasts();
3162 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3163 if (BO->getOpcode() == BO_Comma) {
3164 E = BO->getRHS();
3165 continue;
3166 }
3167 }
3168
3169 break;
3170 }
3171
3172 return cast<ObjCPropertyRefExpr>(E);
3173}
3174
Anna Zaks97c7ce32012-10-01 20:34:04 +00003175bool Expr::isObjCSelfExpr() const {
3176 const Expr *E = IgnoreParenImpCasts();
3177
3178 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3179 if (!DRE)
3180 return false;
3181
3182 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3183 if (!Param)
3184 return false;
3185
3186 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3187 if (!M)
3188 return false;
3189
3190 return M->getSelfDecl() == Param;
3191}
3192
John McCalld25db7e2013-05-06 21:39:12 +00003193FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003194 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003195
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003196 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003197 if (ICE->getCastKind() == CK_LValueToRValue ||
3198 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003199 E = ICE->getSubExpr()->IgnoreParens();
3200 else
3201 break;
3202 }
3203
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003204 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003205 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003206 if (Field->isBitField())
3207 return Field;
3208
John McCalld25db7e2013-05-06 21:39:12 +00003209 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3210 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3211 if (Ivar->isBitField())
3212 return Ivar;
3213
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003214 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3215 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3216 if (Field->isBitField())
3217 return Field;
3218
Eli Friedman609ada22011-07-13 02:05:57 +00003219 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003220 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003221 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003222
Eli Friedman609ada22011-07-13 02:05:57 +00003223 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003224 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003225 }
3226
Douglas Gregor71235ec2009-05-02 02:18:30 +00003227 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003228}
3229
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003230bool Expr::refersToVectorElement() const {
3231 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003232
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003233 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003234 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003235 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003236 E = ICE->getSubExpr()->IgnoreParens();
3237 else
3238 break;
3239 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003240
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003241 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3242 return ASE->getBase()->getType()->isVectorType();
3243
3244 if (isa<ExtVectorElementExpr>(E))
3245 return true;
3246
3247 return false;
3248}
3249
Chris Lattnerb8211f62009-02-16 22:14:05 +00003250/// isArrow - Return true if the base expression is a pointer to vector,
3251/// return false if the base expression is a vector.
3252bool ExtVectorElementExpr::isArrow() const {
3253 return getBase()->getType()->isPointerType();
3254}
3255
Nate Begemance4d7fc2008-04-18 23:10:10 +00003256unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003257 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003258 return VT->getNumElements();
3259 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003260}
3261
Nate Begemanf322eab2008-05-09 06:41:27 +00003262/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003263bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003264 // FIXME: Refactor this code to an accessor on the AST node which returns the
3265 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003266 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003267
3268 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003269 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003270 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003271
Nate Begeman7e5185b2009-01-18 02:01:21 +00003272 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003273 if (Comp[0] == 's' || Comp[0] == 'S')
3274 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003275
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003276 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003277 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003278 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003279
Steve Naroff0d595ca2007-07-30 03:29:09 +00003280 return false;
3281}
Chris Lattner885b4952007-08-02 23:36:59 +00003282
Nate Begemanf322eab2008-05-09 06:41:27 +00003283/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003284void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003285 SmallVectorImpl<unsigned> &Elts) const {
3286 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003287 if (Comp[0] == 's' || Comp[0] == 'S')
3288 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003289
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003290 bool isHi = Comp == "hi";
3291 bool isLo = Comp == "lo";
3292 bool isEven = Comp == "even";
3293 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003294
Nate Begemanf322eab2008-05-09 06:41:27 +00003295 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3296 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003297
Nate Begemanf322eab2008-05-09 06:41:27 +00003298 if (isHi)
3299 Index = e + i;
3300 else if (isLo)
3301 Index = i;
3302 else if (isEven)
3303 Index = 2 * i;
3304 else if (isOdd)
3305 Index = 2 * i + 1;
3306 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003307 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003308
Nate Begemand3862152008-05-13 21:03:02 +00003309 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003310 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003311}
3312
Douglas Gregor9a129192010-04-21 00:45:42 +00003313ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003314 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003315 SourceLocation LBracLoc,
3316 SourceLocation SuperLoc,
3317 bool IsInstanceSuper,
3318 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003319 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003320 ArrayRef<SourceLocation> SelLocs,
3321 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003322 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003323 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003324 SourceLocation RBracLoc,
3325 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003326 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003327 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003328 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003329 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003330 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3331 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003332 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003333 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3334 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003335{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003336 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003337 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003338}
3339
Douglas Gregor9a129192010-04-21 00:45:42 +00003340ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003341 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003342 SourceLocation LBracLoc,
3343 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003344 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003345 ArrayRef<SourceLocation> SelLocs,
3346 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003347 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003348 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003349 SourceLocation RBracLoc,
3350 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003351 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003352 T->isDependentType(), T->isInstantiationDependentType(),
3353 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003354 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3355 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003356 Kind(Class),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003357 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003358 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003359{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003360 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003361 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003362}
3363
Douglas Gregor9a129192010-04-21 00:45:42 +00003364ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003365 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003366 SourceLocation LBracLoc,
3367 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003368 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003369 ArrayRef<SourceLocation> SelLocs,
3370 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003371 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003372 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003373 SourceLocation RBracLoc,
3374 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003375 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003376 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003377 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003378 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003379 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3380 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003381 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003382 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003383 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003384{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003385 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003386 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003387}
3388
3389void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3390 ArrayRef<SourceLocation> SelLocs,
3391 SelectorLocationsKind SelLocsK) {
3392 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003393 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003394 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003395 if (Args[I]->isTypeDependent())
3396 ExprBits.TypeDependent = true;
3397 if (Args[I]->isValueDependent())
3398 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003399 if (Args[I]->isInstantiationDependent())
3400 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003401 if (Args[I]->containsUnexpandedParameterPack())
3402 ExprBits.ContainsUnexpandedParameterPack = true;
3403
3404 MyArgs[I] = Args[I];
3405 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003406
Benjamin Kramer2325b242012-02-20 00:20:48 +00003407 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003408 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003409 if (SelLocsK == SelLoc_NonStandard)
3410 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3411 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003412}
3413
Craig Topperce7167c2013-08-22 04:58:56 +00003414ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003415 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003416 SourceLocation LBracLoc,
3417 SourceLocation SuperLoc,
3418 bool IsInstanceSuper,
3419 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003420 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003421 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003422 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003423 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003424 SourceLocation RBracLoc,
3425 bool isImplicit) {
3426 assert((!SelLocs.empty() || isImplicit) &&
3427 "No selector locs for non-implicit message");
3428 ObjCMessageExpr *Mem;
3429 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3430 if (isImplicit)
3431 Mem = alloc(Context, Args.size(), 0);
3432 else
3433 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003434 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003435 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003436 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003437}
3438
Craig Topperce7167c2013-08-22 04:58:56 +00003439ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003440 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003441 SourceLocation LBracLoc,
3442 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003443 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003444 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003445 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003446 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003447 SourceLocation RBracLoc,
3448 bool isImplicit) {
3449 assert((!SelLocs.empty() || isImplicit) &&
3450 "No selector locs for non-implicit message");
3451 ObjCMessageExpr *Mem;
3452 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3453 if (isImplicit)
3454 Mem = alloc(Context, Args.size(), 0);
3455 else
3456 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003457 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003458 SelLocs, SelLocsK, Method, Args, RBracLoc,
3459 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003460}
3461
Craig Topperce7167c2013-08-22 04:58:56 +00003462ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003463 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003464 SourceLocation LBracLoc,
3465 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003466 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003467 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003468 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003469 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003470 SourceLocation RBracLoc,
3471 bool isImplicit) {
3472 assert((!SelLocs.empty() || isImplicit) &&
3473 "No selector locs for non-implicit message");
3474 ObjCMessageExpr *Mem;
3475 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3476 if (isImplicit)
3477 Mem = alloc(Context, Args.size(), 0);
3478 else
3479 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003480 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003481 SelLocs, SelLocsK, Method, Args, RBracLoc,
3482 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003483}
3484
Craig Topperce7167c2013-08-22 04:58:56 +00003485ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003486 unsigned NumArgs,
3487 unsigned NumStoredSelLocs) {
3488 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003489 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3490}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003491
Craig Topperce7167c2013-08-22 04:58:56 +00003492ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003493 ArrayRef<Expr *> Args,
3494 SourceLocation RBraceLoc,
3495 ArrayRef<SourceLocation> SelLocs,
3496 Selector Sel,
3497 SelectorLocationsKind &SelLocsK) {
3498 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3499 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3500 : 0;
3501 return alloc(C, Args.size(), NumStoredSelLocs);
3502}
3503
Craig Topperce7167c2013-08-22 04:58:56 +00003504ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003505 unsigned NumArgs,
3506 unsigned NumStoredSelLocs) {
3507 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3508 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3509 return (ObjCMessageExpr *)C.Allocate(Size,
3510 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3511}
3512
3513void ObjCMessageExpr::getSelectorLocs(
3514 SmallVectorImpl<SourceLocation> &SelLocs) const {
3515 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3516 SelLocs.push_back(getSelectorLoc(i));
3517}
3518
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003519SourceRange ObjCMessageExpr::getReceiverRange() const {
3520 switch (getReceiverKind()) {
3521 case Instance:
3522 return getInstanceReceiver()->getSourceRange();
3523
3524 case Class:
3525 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3526
3527 case SuperInstance:
3528 case SuperClass:
3529 return getSuperLoc();
3530 }
3531
David Blaikiee4d798f2012-01-20 21:50:17 +00003532 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003533}
3534
Douglas Gregor9a129192010-04-21 00:45:42 +00003535Selector ObjCMessageExpr::getSelector() const {
3536 if (HasMethod)
3537 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3538 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003539 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003540}
3541
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003542QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003543 switch (getReceiverKind()) {
3544 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003545 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003546 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003547 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003548 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003549 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003550 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003551 }
3552
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003553 llvm_unreachable("unexpected receiver kind");
3554}
3555
3556ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3557 QualType T = getReceiverType();
3558
3559 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3560 return Ptr->getInterfaceDecl();
3561
3562 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3563 return Ty->getInterface();
3564
Douglas Gregor9a129192010-04-21 00:45:42 +00003565 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003566}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003567
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003568StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003569 switch (getBridgeKind()) {
3570 case OBC_Bridge:
3571 return "__bridge";
3572 case OBC_BridgeTransfer:
3573 return "__bridge_transfer";
3574 case OBC_BridgeRetained:
3575 return "__bridge_retained";
3576 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003577
3578 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003579}
3580
Craig Topper37932912013-08-18 10:09:15 +00003581ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003582 QualType Type, SourceLocation BLoc,
3583 SourceLocation RP)
3584 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3585 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003586 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003587 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003588 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003589{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003590 SubExprs = new (C) Stmt*[args.size()];
3591 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003592 if (args[i]->isTypeDependent())
3593 ExprBits.TypeDependent = true;
3594 if (args[i]->isValueDependent())
3595 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003596 if (args[i]->isInstantiationDependent())
3597 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003598 if (args[i]->containsUnexpandedParameterPack())
3599 ExprBits.ContainsUnexpandedParameterPack = true;
3600
3601 SubExprs[i] = args[i];
3602 }
3603}
3604
Craig Topper37932912013-08-18 10:09:15 +00003605void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003606 if (SubExprs) C.Deallocate(SubExprs);
3607
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003608 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003609 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003610 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003611}
Nate Begeman48745922009-08-12 02:28:50 +00003612
Craig Topper37932912013-08-18 10:09:15 +00003613GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003614 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003615 ArrayRef<TypeSourceInfo*> AssocTypes,
3616 ArrayRef<Expr*> AssocExprs,
3617 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003618 SourceLocation RParenLoc,
3619 bool ContainsUnexpandedParameterPack,
3620 unsigned ResultIndex)
3621 : Expr(GenericSelectionExprClass,
3622 AssocExprs[ResultIndex]->getType(),
3623 AssocExprs[ResultIndex]->getValueKind(),
3624 AssocExprs[ResultIndex]->getObjectKind(),
3625 AssocExprs[ResultIndex]->isTypeDependent(),
3626 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003627 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003628 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003629 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3630 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3631 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3632 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003633 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003634 assert(AssocTypes.size() == AssocExprs.size());
3635 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3636 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003637}
3638
Craig Topper37932912013-08-18 10:09:15 +00003639GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003640 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003641 ArrayRef<TypeSourceInfo*> AssocTypes,
3642 ArrayRef<Expr*> AssocExprs,
3643 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003644 SourceLocation RParenLoc,
3645 bool ContainsUnexpandedParameterPack)
3646 : Expr(GenericSelectionExprClass,
3647 Context.DependentTy,
3648 VK_RValue,
3649 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003650 /*isTypeDependent=*/true,
3651 /*isValueDependent=*/true,
3652 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003653 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003654 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3655 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3656 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3657 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003658 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003659 assert(AssocTypes.size() == AssocExprs.size());
3660 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3661 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003662}
3663
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003664//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003665// DesignatedInitExpr
3666//===----------------------------------------------------------------------===//
3667
Chandler Carruth631abd92011-06-16 06:47:06 +00003668IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003669 assert(Kind == FieldDesignator && "Only valid on a field designator");
3670 if (Field.NameOrField & 0x01)
3671 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3672 else
3673 return getField()->getIdentifier();
3674}
3675
Craig Topper37932912013-08-18 10:09:15 +00003676DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003677 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003678 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003679 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003680 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003681 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003682 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003683 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003684 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003685 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003686 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003687 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003688 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003689 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003690 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003691
3692 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003693 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003694 *Child++ = Init;
3695
3696 // Copy the designators and their subexpressions, computing
3697 // value-dependence along the way.
3698 unsigned IndexIdx = 0;
3699 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003700 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003701
3702 if (this->Designators[I].isArrayDesignator()) {
3703 // Compute type- and value-dependence.
3704 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003705 if (Index->isTypeDependent() || Index->isValueDependent())
3706 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003707 if (Index->isInstantiationDependent())
3708 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003709 // Propagate unexpanded parameter packs.
3710 if (Index->containsUnexpandedParameterPack())
3711 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003712
3713 // Copy the index expressions into permanent storage.
3714 *Child++ = IndexExprs[IndexIdx++];
3715 } else if (this->Designators[I].isArrayRangeDesignator()) {
3716 // Compute type- and value-dependence.
3717 Expr *Start = IndexExprs[IndexIdx];
3718 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003719 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003720 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003721 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003722 ExprBits.InstantiationDependent = true;
3723 } else if (Start->isInstantiationDependent() ||
3724 End->isInstantiationDependent()) {
3725 ExprBits.InstantiationDependent = true;
3726 }
3727
Douglas Gregora6e053e2010-12-15 01:34:56 +00003728 // Propagate unexpanded parameter packs.
3729 if (Start->containsUnexpandedParameterPack() ||
3730 End->containsUnexpandedParameterPack())
3731 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003732
3733 // Copy the start/end expressions into permanent storage.
3734 *Child++ = IndexExprs[IndexIdx++];
3735 *Child++ = IndexExprs[IndexIdx++];
3736 }
3737 }
3738
Benjamin Kramerc215e762012-08-24 11:54:20 +00003739 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003740}
3741
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003742DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003743DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003744 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003745 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003746 SourceLocation ColonOrEqualLoc,
3747 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003748 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003749 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003750 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003751 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003752 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003753}
3754
Craig Topper37932912013-08-18 10:09:15 +00003755DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003756 unsigned NumIndexExprs) {
3757 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3758 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3759 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3760}
3761
Craig Topper37932912013-08-18 10:09:15 +00003762void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003763 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003764 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003765 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003766 NumDesignators = NumDesigs;
3767 for (unsigned I = 0; I != NumDesigs; ++I)
3768 Designators[I] = Desigs[I];
3769}
3770
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003771SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3772 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3773 if (size() == 1)
3774 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003775 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3776 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003777}
3778
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003779SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003780 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003781 Designator &First =
3782 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003783 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003784 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003785 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3786 else
3787 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3788 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003789 StartLoc =
3790 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003791 return StartLoc;
3792}
3793
3794SourceLocation DesignatedInitExpr::getLocEnd() const {
3795 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003796}
3797
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003798Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003799 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003800 char *Ptr = static_cast<char *>(
3801 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003802 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003803 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3804 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3805}
3806
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003807Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003808 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003809 "Requires array range designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003810 char *Ptr = static_cast<char *>(
3811 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003812 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003813 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3814 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3815}
3816
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003817Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003818 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003819 "Requires array range designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003820 char *Ptr = static_cast<char *>(
3821 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003822 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003823 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3824 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3825}
3826
Douglas Gregord5846a12009-04-15 06:41:24 +00003827/// \brief Replaces the designator at index @p Idx with the series
3828/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00003829void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003830 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003831 const Designator *Last) {
3832 unsigned NumNewDesignators = Last - First;
3833 if (NumNewDesignators == 0) {
3834 std::copy_backward(Designators + Idx + 1,
3835 Designators + NumDesignators,
3836 Designators + Idx);
3837 --NumNewDesignators;
3838 return;
3839 } else if (NumNewDesignators == 1) {
3840 Designators[Idx] = *First;
3841 return;
3842 }
3843
Mike Stump11289f42009-09-09 15:08:12 +00003844 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003845 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003846 std::copy(Designators, Designators + Idx, NewDesignators);
3847 std::copy(First, Last, NewDesignators + Idx);
3848 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3849 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003850 Designators = NewDesignators;
3851 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3852}
3853
Craig Topper37932912013-08-18 10:09:15 +00003854ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003855 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003856 SourceLocation rparenloc)
3857 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003858 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003859 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3860 Exprs = new (C) Stmt*[exprs.size()];
3861 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003862 if (exprs[i]->isTypeDependent())
3863 ExprBits.TypeDependent = true;
3864 if (exprs[i]->isValueDependent())
3865 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003866 if (exprs[i]->isInstantiationDependent())
3867 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003868 if (exprs[i]->containsUnexpandedParameterPack())
3869 ExprBits.ContainsUnexpandedParameterPack = true;
3870
Nate Begeman5ec4b312009-08-10 23:49:36 +00003871 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003872 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003873}
3874
John McCall1bf58462011-02-16 08:02:54 +00003875const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3876 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3877 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003878 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3879 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003880 e = cast<CXXConstructExpr>(e)->getArg(0);
3881 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3882 e = ice->getSubExpr();
3883 return cast<OpaqueValueExpr>(e);
3884}
3885
Craig Topper37932912013-08-18 10:09:15 +00003886PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3887 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00003888 unsigned numSemanticExprs) {
3889 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3890 (1 + numSemanticExprs) * sizeof(Expr*),
3891 llvm::alignOf<PseudoObjectExpr>());
3892 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3893}
3894
3895PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3896 : Expr(PseudoObjectExprClass, shell) {
3897 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3898}
3899
Craig Topper37932912013-08-18 10:09:15 +00003900PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00003901 ArrayRef<Expr*> semantics,
3902 unsigned resultIndex) {
3903 assert(syntax && "no syntactic expression!");
3904 assert(semantics.size() && "no semantic expressions!");
3905
3906 QualType type;
3907 ExprValueKind VK;
3908 if (resultIndex == NoResult) {
3909 type = C.VoidTy;
3910 VK = VK_RValue;
3911 } else {
3912 assert(resultIndex < semantics.size());
3913 type = semantics[resultIndex]->getType();
3914 VK = semantics[resultIndex]->getValueKind();
3915 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3916 }
3917
3918 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3919 (1 + semantics.size()) * sizeof(Expr*),
3920 llvm::alignOf<PseudoObjectExpr>());
3921 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3922 resultIndex);
3923}
3924
3925PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3926 Expr *syntax, ArrayRef<Expr*> semantics,
3927 unsigned resultIndex)
3928 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3929 /*filled in at end of ctor*/ false, false, false, false) {
3930 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3931 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3932
3933 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3934 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3935 getSubExprsBuffer()[i] = E;
3936
3937 if (E->isTypeDependent())
3938 ExprBits.TypeDependent = true;
3939 if (E->isValueDependent())
3940 ExprBits.ValueDependent = true;
3941 if (E->isInstantiationDependent())
3942 ExprBits.InstantiationDependent = true;
3943 if (E->containsUnexpandedParameterPack())
3944 ExprBits.ContainsUnexpandedParameterPack = true;
3945
3946 if (isa<OpaqueValueExpr>(E))
3947 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3948 "opaque-value semantic expressions for pseudo-object "
3949 "operations must have sources");
3950 }
3951}
3952
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003953//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003954// ExprIterator.
3955//===----------------------------------------------------------------------===//
3956
3957Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3958Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3959Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3960const Expr* ConstExprIterator::operator[](size_t idx) const {
3961 return cast<Expr>(I[idx]);
3962}
3963const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3964const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3965
3966//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003967// Child Iterators for iterating over subexpressions/substatements
3968//===----------------------------------------------------------------------===//
3969
Peter Collingbournee190dee2011-03-11 19:24:49 +00003970// UnaryExprOrTypeTraitExpr
3971Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003972 // If this is of a type and the type is a VLA type (and not a typedef), the
3973 // size expression of the VLA needs to be treated as an executable expression.
3974 // Why isn't this weirdness documented better in StmtIterator?
3975 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003976 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003977 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003978 return child_range(child_iterator(T), child_iterator());
3979 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003980 }
John McCallbd066782011-02-09 08:16:59 +00003981 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003982}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003983
Steve Naroffd54978b2007-09-18 23:55:05 +00003984// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003985Stmt::child_range ObjCMessageExpr::children() {
3986 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003987 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003988 begin = reinterpret_cast<Stmt **>(this + 1);
3989 else
3990 begin = reinterpret_cast<Stmt **>(getArgs());
3991 return child_range(begin,
3992 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003993}
3994
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003995ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00003996 QualType T, ObjCMethodDecl *Method,
3997 SourceRange SR)
3998 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3999 false, false, false, false),
4000 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
4001{
4002 Expr **SaveElements = getElements();
4003 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4004 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4005 ExprBits.ValueDependent = true;
4006 if (Elements[I]->isInstantiationDependent())
4007 ExprBits.InstantiationDependent = true;
4008 if (Elements[I]->containsUnexpandedParameterPack())
4009 ExprBits.ContainsUnexpandedParameterPack = true;
4010
4011 SaveElements[I] = Elements[I];
4012 }
4013}
4014
Craig Topperce7167c2013-08-22 04:58:56 +00004015ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004016 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004017 QualType T, ObjCMethodDecl * Method,
4018 SourceRange SR) {
4019 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4020 + Elements.size() * sizeof(Expr *));
4021 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4022}
4023
Craig Topperce7167c2013-08-22 04:58:56 +00004024ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004025 unsigned NumElements) {
4026
4027 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4028 + NumElements * sizeof(Expr *));
4029 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4030}
4031
4032ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4033 ArrayRef<ObjCDictionaryElement> VK,
4034 bool HasPackExpansions,
4035 QualType T, ObjCMethodDecl *method,
4036 SourceRange SR)
4037 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4038 false, false),
4039 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4040 DictWithObjectsMethod(method)
4041{
4042 KeyValuePair *KeyValues = getKeyValues();
4043 ExpansionData *Expansions = getExpansionData();
4044 for (unsigned I = 0; I < NumElements; I++) {
4045 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4046 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4047 ExprBits.ValueDependent = true;
4048 if (VK[I].Key->isInstantiationDependent() ||
4049 VK[I].Value->isInstantiationDependent())
4050 ExprBits.InstantiationDependent = true;
4051 if (VK[I].EllipsisLoc.isInvalid() &&
4052 (VK[I].Key->containsUnexpandedParameterPack() ||
4053 VK[I].Value->containsUnexpandedParameterPack()))
4054 ExprBits.ContainsUnexpandedParameterPack = true;
4055
4056 KeyValues[I].Key = VK[I].Key;
4057 KeyValues[I].Value = VK[I].Value;
4058 if (Expansions) {
4059 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4060 if (VK[I].NumExpansions)
4061 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4062 else
4063 Expansions[I].NumExpansionsPlusOne = 0;
4064 }
4065 }
4066}
4067
4068ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004069ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004070 ArrayRef<ObjCDictionaryElement> VK,
4071 bool HasPackExpansions,
4072 QualType T, ObjCMethodDecl *method,
4073 SourceRange SR) {
4074 unsigned ExpansionsSize = 0;
4075 if (HasPackExpansions)
4076 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4077
4078 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4079 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4080 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4081}
4082
4083ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004084ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004085 bool HasPackExpansions) {
4086 unsigned ExpansionsSize = 0;
4087 if (HasPackExpansions)
4088 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4089 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4090 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4091 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4092 HasPackExpansions);
4093}
4094
Craig Topperce7167c2013-08-22 04:58:56 +00004095ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004096 Expr *base,
4097 Expr *key, QualType T,
4098 ObjCMethodDecl *getMethod,
4099 ObjCMethodDecl *setMethod,
4100 SourceLocation RB) {
4101 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4102 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4103 OK_ObjCSubscript,
4104 getMethod, setMethod, RB);
4105}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004106
Benjamin Kramerc215e762012-08-24 11:54:20 +00004107AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004108 QualType t, AtomicOp op, SourceLocation RP)
4109 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4110 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004111 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004112{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004113 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4114 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004115 if (args[i]->isTypeDependent())
4116 ExprBits.TypeDependent = true;
4117 if (args[i]->isValueDependent())
4118 ExprBits.ValueDependent = true;
4119 if (args[i]->isInstantiationDependent())
4120 ExprBits.InstantiationDependent = true;
4121 if (args[i]->containsUnexpandedParameterPack())
4122 ExprBits.ContainsUnexpandedParameterPack = true;
4123
4124 SubExprs[i] = args[i];
4125 }
4126}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004127
4128unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4129 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004130 case AO__c11_atomic_init:
4131 case AO__c11_atomic_load:
4132 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004133 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004134
4135 case AO__c11_atomic_store:
4136 case AO__c11_atomic_exchange:
4137 case AO__atomic_load:
4138 case AO__atomic_store:
4139 case AO__atomic_store_n:
4140 case AO__atomic_exchange_n:
4141 case AO__c11_atomic_fetch_add:
4142 case AO__c11_atomic_fetch_sub:
4143 case AO__c11_atomic_fetch_and:
4144 case AO__c11_atomic_fetch_or:
4145 case AO__c11_atomic_fetch_xor:
4146 case AO__atomic_fetch_add:
4147 case AO__atomic_fetch_sub:
4148 case AO__atomic_fetch_and:
4149 case AO__atomic_fetch_or:
4150 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004151 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004152 case AO__atomic_add_fetch:
4153 case AO__atomic_sub_fetch:
4154 case AO__atomic_and_fetch:
4155 case AO__atomic_or_fetch:
4156 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004157 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004158 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004159
4160 case AO__atomic_exchange:
4161 return 4;
4162
4163 case AO__c11_atomic_compare_exchange_strong:
4164 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004165 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004166
4167 case AO__atomic_compare_exchange:
4168 case AO__atomic_compare_exchange_n:
4169 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004170 }
4171 llvm_unreachable("unknown atomic op");
4172}