blob: 9055ddac35e3577144e091db3ad7c56ab9c69be7 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnera4d55d82008-10-06 06:40:35 +000014#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000017#include "clang/AST/DeclCXX.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor25d0a0f2012-02-23 07:33:15 +000020#include "clang/AST/EvaluatedExprVisitor.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
David Majnemerbafa74f2013-11-06 23:31:56 +000023#include "clang/AST/Mangle.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000027#include "clang/Basic/CharInfo.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000030#include "clang/Lex/Lexer.h"
31#include "clang/Lex/LiteralSupport.h"
32#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000033#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000034#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000035#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000036#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000037using namespace clang;
38
Rafael Espindola8d852e32012-06-27 18:18:05 +000039const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindola632fbaa2012-06-28 01:56:38 +000040 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000041
42 QualType DerivedType = E->getType();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000043 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
44 DerivedType = PTy->getPointeeType();
45
Rafael Espindola251c4492012-07-17 20:24:05 +000046 if (DerivedType->isDependentType())
47 return NULL;
48
Rafael Espindola0b4fe502012-06-26 17:45:31 +000049 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000050 Decl *D = Ty->getDecl();
51 return cast<CXXRecordDecl>(D);
52}
53
Richard Smith4e43dec2013-06-03 00:17:11 +000054const Expr *Expr::skipRValueSubobjectAdjustments(
55 SmallVectorImpl<const Expr *> &CommaLHSs,
56 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
Rafael Espindola0a7dd832012-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 Smithd6b69872013-06-15 00:30:29 +000077 if (!ME->isArrow()) {
Rafael Espindola0a7dd832012-10-27 01:03:43 +000078 assert(ME->getBase()->getType()->isRecordType());
79 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
Richard Smithd6b69872013-06-15 00:30:29 +000080 if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
Richard Smithd1b55dc2013-06-03 07:13:35 +000081 E = ME->getBase();
82 Adjustments.push_back(SubobjectAdjustment(Field));
83 continue;
84 }
Rafael Espindola0a7dd832012-10-27 01:03:43 +000085 }
86 }
87 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
88 if (BO->isPtrMemOp()) {
Rafael Espindolaef4b6662012-11-01 14:32:20 +000089 assert(BO->getRHS()->isRValue());
Rafael Espindola0a7dd832012-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 Smith4e43dec2013-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 Espindola0a7dd832012-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 Smithc3bf52c2013-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 Espindola0a7dd832012-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 Smithc3bf52c2013-04-20 22:23:05 +0000120 if (ILE->getNumInits() == 1 && ILE->isGLValue()) {
Rafael Espindola0a7dd832012-10-27 01:03:43 +0000121 E = ILE->getInit(0);
Richard Smithc3bf52c2013-04-20 22:23:05 +0000122 if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
123 E = DAE->getExpr();
124 }
Rafael Espindola0a7dd832012-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 Lattner2b334bb2010-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 Collingbournef111d932011-04-15 00:35:48 +0000144 const Expr *E = IgnoreParens();
145
Chris Lattner2b334bb2010-04-16 23:34:13 +0000146 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +0000147 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +0000148 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +0000149 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +0000150
Peter Collingbournef111d932011-04-15 00:35:48 +0000151 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000152 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000153 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000154 return UO->getSubExpr()->isKnownToHaveBooleanValue();
155 default:
156 return false;
157 }
158 }
Sean Huntc3021132010-05-05 15:23:54 +0000159
John McCall6907fbe2010-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 Collingbournef111d932011-04-15 00:35:48 +0000162 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000163 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000164
Peter Collingbournef111d932011-04-15 00:35:48 +0000165 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000166 switch (BO->getOpcode()) {
167 default: return false;
John McCall2de56d12010-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 Lattner2b334bb2010-04-16 23:34:13 +0000176 return true;
Sean Huntc3021132010-05-05 15:23:54 +0000177
John McCall2de56d12010-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 Lattner2b334bb2010-04-16 23:34:13 +0000181 // Handle things like (x==2)|(y==12).
182 return BO->getLHS()->isKnownToHaveBooleanValue() &&
183 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000184
John McCall2de56d12010-08-25 11:45:40 +0000185 case BO_Comma:
186 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000187 return BO->getRHS()->isKnownToHaveBooleanValue();
188 }
189 }
Sean Huntc3021132010-05-05 15:23:54 +0000190
Peter Collingbournef111d932011-04-15 00:35:48 +0000191 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000192 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
193 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000194
Chris Lattner2b334bb2010-04-16 23:34:13 +0000195 return false;
196}
197
John McCall63c00d72011-02-09 08:16:59 +0000198// Amusing macro metaprogramming hack: check whether a class provides
199// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000200//
201// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-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 Dunbar90e25a82012-03-09 15:39:19 +0000218 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-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 McCall63c00d72011-02-09 08:16:59 +0000233}
234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235//===----------------------------------------------------------------------===//
236// Primary Expressions.
237//===----------------------------------------------------------------------===//
238
Douglas Gregor561f8122011-07-01 01:22:09 +0000239/// \brief Compute the type-, value-, and instantiation-dependence of a
240/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000241/// based on the declaration being referenced.
Craig Topper9db7a7e2013-08-22 04:58:56 +0000242static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
243 QualType T, bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000244 bool &ValueDependent,
245 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000246 TypeDependent = false;
247 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000248 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000249
250 // (TD) C++ [temp.dep.expr]p3:
251 // An id-expression is type-dependent if it contains:
252 //
Sean Huntc3021132010-05-05 15:23:54 +0000253 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000254 //
255 // (VD) C++ [temp.dep.constexpr]p2:
256 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000257
Douglas Gregor0da76df2009-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 Gregord967e312011-01-19 21:52:31 +0000260 if (T->isDependentType()) {
261 TypeDependent = true;
262 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000263 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000264 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000265 } else if (T->isInstantiationDependentType()) {
266 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000267 }
Douglas Gregord967e312011-01-19 21:52:31 +0000268
Douglas Gregor0da76df2009-11-23 11:41:28 +0000269 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000270 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-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 Gregor0da76df2009-11-23 11:41:28 +0000282 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000283
Douglas Gregor0da76df2009-11-23 11:41:28 +0000284 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000285 if (isa<NonTypeTemplateParmDecl>(D)) {
286 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000287 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000288 return;
289 }
290
Douglas Gregor0da76df2009-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 Smithdb1822c2011-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 Gregord967e312011-01-19 21:52:31 +0000298 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith80ad52f2013-01-02 11:42:31 +0000299 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smitha10b9782013-04-22 15:31:51 +0000300 Var->getType()->isLiteralType(Ctx) :
Richard Smithdb1822c2011-11-08 01:31:09 +0000301 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikie4ef832f2012-08-10 00:55:35 +0000302 (Var->getType().isConstQualified() ||
Richard Smithdb1822c2011-11-08 01:31:09 +0000303 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000304 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000305 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000306 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000307 InstantiationDependent = true;
308 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000309 }
310
Douglas Gregorbb6e73f2010-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 Smithdb1822c2011-11-08 01:31:09 +0000314 if (Var->isStaticDataMember() &&
315 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000316 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000317 InstantiationDependent = true;
Richard Smith418220b2013-11-14 22:40:45 +0000318 TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
319 if (TInfo->getType()->isIncompleteArrayType())
320 TypeDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000321 }
Douglas Gregord967e312011-01-19 21:52:31 +0000322
323 return;
324 }
325
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000326 // (VD) - FIXME: Missing from the standard:
327 // - a member function or a static data member of the current
328 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000329 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
330 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000331 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000332 }
Douglas Gregord967e312011-01-19 21:52:31 +0000333}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000334
Craig Topper9db7a7e2013-08-22 04:58:56 +0000335void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000336 bool TypeDependent = false;
337 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000338 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000339 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
340 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000341
342 // (TD) C++ [temp.dep.expr]p3:
343 // An id-expression is type-dependent if it contains:
344 //
345 // and
346 //
347 // (VD) C++ [temp.dep.constexpr]p2:
348 // An identifier is value-dependent if it is:
349 if (!TypeDependent && !ValueDependent &&
350 hasExplicitTemplateArgs() &&
351 TemplateSpecializationType::anyDependentTemplateArguments(
352 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000353 getNumTemplateArgs(),
354 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000355 TypeDependent = true;
356 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000357 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000358 }
359
360 ExprBits.TypeDependent = TypeDependent;
361 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000362 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000363
Douglas Gregor10738d32010-12-23 23:51:58 +0000364 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000365 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000366 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000367}
368
Craig Topper9db7a7e2013-08-22 04:58:56 +0000369DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000370 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000371 SourceLocation TemplateKWLoc,
John McCallf4b88a42012-03-10 09:33:50 +0000372 ValueDecl *D, bool RefersToEnclosingLocal,
373 const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000374 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000375 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000376 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000377 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000378 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
379 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000380 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000381 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000382 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
383 if (FoundD)
384 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000385 DeclRefExprBits.HasTemplateKWAndArgsInfo
386 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCallf4b88a42012-03-10 09:33:50 +0000387 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor561f8122011-07-01 01:22:09 +0000388 if (TemplateArgs) {
389 bool Dependent = false;
390 bool InstantiationDependent = false;
391 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000392 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
393 Dependent,
394 InstantiationDependent,
395 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000396 if (InstantiationDependent)
397 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000398 } else if (TemplateKWLoc.isValid()) {
399 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000400 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000401 DeclRefExprBits.HadMultipleCandidates = 0;
402
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000403 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000404}
405
Craig Topper9db7a7e2013-08-22 04:58:56 +0000406DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000407 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000408 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000409 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000410 bool RefersToEnclosingLocal,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000411 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000412 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000413 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000414 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000415 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000416 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000417 RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000418 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000419 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000420}
421
Craig Topper9db7a7e2013-08-22 04:58:56 +0000422DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000423 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000424 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000425 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000426 bool RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000427 const DeclarationNameInfo &NameInfo,
428 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000429 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000430 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000431 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000432 // Filter out cases where the found Decl is the same as the value refenenced.
433 if (D == FoundD)
434 FoundD = 0;
435
Douglas Gregora2813ce2009-10-23 18:54:35 +0000436 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7247c882013-05-15 07:37:26 +0000437 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000438 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000439 if (FoundD)
440 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000441 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000442 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
443 else if (TemplateKWLoc.isValid())
444 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000445
Chris Lattner32488542010-10-30 05:14:06 +0000446 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000447 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000448 RefersToEnclosingLocal,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000449 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000450}
451
Craig Topper9db7a7e2013-08-22 04:58:56 +0000452DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000453 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000454 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000455 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000456 unsigned NumTemplateArgs) {
457 std::size_t Size = sizeof(DeclRefExpr);
458 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000459 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000460 if (HasFoundDecl)
461 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000462 if (HasTemplateKWAndArgsInfo)
463 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000464
Chris Lattner32488542010-10-30 05:14:06 +0000465 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000466 return new (Mem) DeclRefExpr(EmptyShell());
467}
468
Daniel Dunbar396ec672012-03-09 15:39:15 +0000469SourceLocation DeclRefExpr::getLocStart() const {
470 if (hasQualifier())
471 return getQualifierLoc().getBeginLoc();
472 return getNameInfo().getLocStart();
473}
474SourceLocation DeclRefExpr::getLocEnd() const {
475 if (hasExplicitTemplateArgs())
476 return getRAngleLoc();
477 return getNameInfo().getLocEnd();
478}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000479
Anders Carlsson3a082d82009-09-08 18:24:21 +0000480// FIXME: Maybe this should use DeclPrinter with a special "print predefined
481// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000482std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
483 ASTContext &Context = CurrentDecl->getASTContext();
484
David Majnemerbafa74f2013-11-06 23:31:56 +0000485 if (IT == PredefinedExpr::FuncDName) {
486 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
487 OwningPtr<MangleContext> MC;
488 MC.reset(Context.createMangleContext());
489
490 if (MC->shouldMangleDeclName(ND)) {
491 SmallString<256> Buffer;
492 llvm::raw_svector_ostream Out(Buffer);
493 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
494 MC->mangleCXXCtor(CD, Ctor_Base, Out);
495 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
496 MC->mangleCXXDtor(DD, Dtor_Base, Out);
497 else
498 MC->mangleName(ND, Out);
499
500 Out.flush();
501 if (!Buffer.empty() && Buffer.front() == '\01')
502 return Buffer.substr(1);
503 return Buffer.str();
504 } else
505 return ND->getIdentifier()->getName();
506 }
507 return "";
508 }
Anders Carlsson3a082d82009-09-08 18:24:21 +0000509 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000510 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000511 return FD->getNameAsString();
512
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000513 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000514 llvm::raw_svector_ostream Out(Name);
515
516 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000517 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000518 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000519 if (MD->isStatic())
520 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000521 }
522
David Blaikie4e4d0842012-03-11 07:00:24 +0000523 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramerb063ef02013-02-23 13:53:57 +0000524 std::string Proto;
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000525 llvm::raw_string_ostream POut(Proto);
Benjamin Kramerb063ef02013-02-23 13:53:57 +0000526 FD->printQualifiedName(POut, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000527
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000528 const FunctionDecl *Decl = FD;
529 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
530 Decl = Pattern;
531 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000532 const FunctionProtoType *FT = 0;
533 if (FD->hasWrittenPrototype())
534 FT = dyn_cast<FunctionProtoType>(AFT);
535
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000536 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000537 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000538 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000539 if (i) POut << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000540 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000541 }
542
543 if (FT->isVariadic()) {
544 if (FD->getNumParams()) POut << ", ";
545 POut << "...";
546 }
547 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000548 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000549
Sam Weinig4eadcc52009-12-27 01:38:20 +0000550 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis4ae711b2012-12-14 19:44:11 +0000551 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikie4ef832f2012-08-10 00:55:35 +0000552 if (FT->isConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000553 POut << " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000554 if (FT->isVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000555 POut << " volatile";
556 RefQualifierKind Ref = MD->getRefQualifier();
557 if (Ref == RQ_LValue)
558 POut << " &";
559 else if (Ref == RQ_RValue)
560 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000561 }
562
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000563 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
564 SpecsTy Specs;
565 const DeclContext *Ctx = FD->getDeclContext();
566 while (Ctx && isa<NamedDecl>(Ctx)) {
567 const ClassTemplateSpecializationDecl *Spec
568 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
569 if (Spec && !Spec->isExplicitSpecialization())
570 Specs.push_back(Spec);
571 Ctx = Ctx->getParent();
572 }
573
574 std::string TemplateParams;
575 llvm::raw_string_ostream TOut(TemplateParams);
576 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
577 I != E; ++I) {
578 const TemplateParameterList *Params
579 = (*I)->getSpecializedTemplate()->getTemplateParameters();
580 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
581 assert(Params->size() == Args.size());
582 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
583 StringRef Param = Params->getParam(i)->getName();
584 if (Param.empty()) continue;
585 TOut << Param << " = ";
586 Args.get(i).print(Policy, TOut);
587 TOut << ", ";
588 }
589 }
590
591 FunctionTemplateSpecializationInfo *FSI
592 = FD->getTemplateSpecializationInfo();
593 if (FSI && !FSI->isExplicitSpecialization()) {
594 const TemplateParameterList* Params
595 = FSI->getTemplate()->getTemplateParameters();
596 const TemplateArgumentList* Args = FSI->TemplateArguments;
597 assert(Params->size() == Args->size());
598 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
599 StringRef Param = Params->getParam(i)->getName();
600 if (Param.empty()) continue;
601 TOut << Param << " = ";
602 Args->get(i).print(Policy, TOut);
603 TOut << ", ";
604 }
605 }
606
607 TOut.flush();
608 if (!TemplateParams.empty()) {
609 // remove the trailing comma and space
610 TemplateParams.resize(TemplateParams.size() - 2);
611 POut << " [" << TemplateParams << "]";
612 }
613
614 POut.flush();
615
Benjamin Kramer28bdbf02013-08-21 11:45:27 +0000616 // Print "auto" for all deduced return types. This includes C++1y return
617 // type deduction and lambdas. For trailing return types resolve the
618 // decltype expression. Otherwise print the real type when this is
619 // not a constructor or destructor.
620 if ((isa<CXXMethodDecl>(FD) &&
621 cast<CXXMethodDecl>(FD)->getParent()->isLambda()) ||
622 (FT && FT->getResultType()->getAs<AutoType>()))
623 Proto = "auto " + Proto;
624 else if (FT && FT->getResultType()->getAs<DecltypeType>())
625 FT->getResultType()->getAs<DecltypeType>()->getUnderlyingType()
626 .getAsStringInternal(Proto, Policy);
627 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000628 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000629
630 Out << Proto;
631
632 Out.flush();
633 return Name.str().str();
634 }
Wei Pan15b26742013-08-26 14:27:34 +0000635 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
636 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
637 // Skip to its enclosing function or method, but not its enclosing
638 // CapturedDecl.
639 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
640 const Decl *D = Decl::castFromDeclContext(DC);
641 return ComputeName(IT, D);
642 }
643 llvm_unreachable("CapturedDecl not inside a function or method");
644 }
Anders Carlsson3a082d82009-09-08 18:24:21 +0000645 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000646 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000647 llvm::raw_svector_ostream Out(Name);
648 Out << (MD->isInstanceMethod() ? '-' : '+');
649 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000650
651 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
652 // a null check to avoid a crash.
653 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000654 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000655
Anders Carlsson3a082d82009-09-08 18:24:21 +0000656 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000657 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000658 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000659
Anders Carlsson3a082d82009-09-08 18:24:21 +0000660 Out << ' ';
661 Out << MD->getSelector().getAsString();
662 Out << ']';
663
664 Out.flush();
665 return Name.str().str();
666 }
667 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
668 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
669 return "top level";
670 }
671 return "";
672}
673
Craig Topper05ed1a02013-08-18 10:09:15 +0000674void APNumericStorage::setIntValue(const ASTContext &C,
675 const llvm::APInt &Val) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000676 if (hasAllocation())
677 C.Deallocate(pVal);
678
679 BitWidth = Val.getBitWidth();
680 unsigned NumWords = Val.getNumWords();
681 const uint64_t* Words = Val.getRawData();
682 if (NumWords > 1) {
683 pVal = new (C) uint64_t[NumWords];
684 std::copy(Words, Words + NumWords, pVal);
685 } else if (NumWords == 1)
686 VAL = Words[0];
687 else
688 VAL = 0;
689}
690
Craig Topper05ed1a02013-08-18 10:09:15 +0000691IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000692 QualType type, SourceLocation l)
693 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
694 false, false),
695 Loc(l) {
696 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
697 assert(V.getBitWidth() == C.getIntWidth(type) &&
698 "Integer type is not the correct size for constant.");
699 setValue(C, V);
700}
701
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000702IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000703IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000704 QualType type, SourceLocation l) {
705 return new (C) IntegerLiteral(C, V, type, l);
706}
707
708IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000709IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000710 return new (C) IntegerLiteral(Empty);
711}
712
Craig Topper05ed1a02013-08-18 10:09:15 +0000713FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000714 bool isexact, QualType Type, SourceLocation L)
715 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
716 false, false), Loc(L) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000717 setSemantics(V.getSemantics());
Benjamin Kramer478851c2012-07-04 17:04:04 +0000718 FloatingLiteralBits.IsExact = isexact;
719 setValue(C, V);
720}
721
Craig Topper05ed1a02013-08-18 10:09:15 +0000722FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer478851c2012-07-04 17:04:04 +0000723 : Expr(FloatingLiteralClass, Empty) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000724 setRawSemantics(IEEEhalf);
Benjamin Kramer478851c2012-07-04 17:04:04 +0000725 FloatingLiteralBits.IsExact = false;
726}
727
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000728FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000729FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000730 bool isexact, QualType Type, SourceLocation L) {
731 return new (C) FloatingLiteral(C, V, isexact, Type, L);
732}
733
734FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000735FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000736 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000737}
738
Tim Northover9ec55f22013-01-22 09:46:51 +0000739const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
740 switch(FloatingLiteralBits.Semantics) {
741 case IEEEhalf:
742 return llvm::APFloat::IEEEhalf;
743 case IEEEsingle:
744 return llvm::APFloat::IEEEsingle;
745 case IEEEdouble:
746 return llvm::APFloat::IEEEdouble;
747 case x87DoubleExtended:
748 return llvm::APFloat::x87DoubleExtended;
749 case IEEEquad:
750 return llvm::APFloat::IEEEquad;
751 case PPCDoubleDouble:
752 return llvm::APFloat::PPCDoubleDouble;
753 }
754 llvm_unreachable("Unrecognised floating semantics");
755}
756
757void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
758 if (&Sem == &llvm::APFloat::IEEEhalf)
759 FloatingLiteralBits.Semantics = IEEEhalf;
760 else if (&Sem == &llvm::APFloat::IEEEsingle)
761 FloatingLiteralBits.Semantics = IEEEsingle;
762 else if (&Sem == &llvm::APFloat::IEEEdouble)
763 FloatingLiteralBits.Semantics = IEEEdouble;
764 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
765 FloatingLiteralBits.Semantics = x87DoubleExtended;
766 else if (&Sem == &llvm::APFloat::IEEEquad)
767 FloatingLiteralBits.Semantics = IEEEquad;
768 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
769 FloatingLiteralBits.Semantics = PPCDoubleDouble;
770 else
771 llvm_unreachable("Unknown floating semantics");
772}
773
Chris Lattnerda8249e2008-06-07 22:13:43 +0000774/// getValueAsApproximateDouble - This returns the value as an inaccurate
775/// double. Note that this may cause loss of precision, but is useful for
776/// debugging dumps, etc.
777double FloatingLiteral::getValueAsApproximateDouble() const {
778 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000779 bool ignored;
780 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
781 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000782 return V.convertToDouble();
783}
784
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000785int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000786 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000787 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000788 case Ascii:
789 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000790 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000791 break;
792 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000793 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000794 break;
795 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000796 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000797 break;
798 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000799 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000800 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000801 }
802 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
803 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000804 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000805 && "character byte widths supported are 1, 2, and 4 only");
806 return CharByteWidth;
807}
808
Craig Topper05ed1a02013-08-18 10:09:15 +0000809StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000810 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000811 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000812 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000813 // Allocate enough space for the StringLiteral plus an array of locations for
814 // any concatenated string tokens.
815 void *Mem = C.Allocate(sizeof(StringLiteral)+
816 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000817 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000818 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000821 SL->setString(C,Str,Kind,Pascal);
822
Chris Lattner2085fd62009-02-18 06:40:38 +0000823 SL->TokLocs[0] = Loc[0];
824 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000825
Chris Lattner726e1682009-02-18 05:49:11 +0000826 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000827 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
828 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000829}
830
Craig Topper05ed1a02013-08-18 10:09:15 +0000831StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
832 unsigned NumStrs) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000833 void *Mem = C.Allocate(sizeof(StringLiteral)+
834 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000835 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000836 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000837 SL->CharByteWidth = 0;
838 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000839 SL->NumConcatenated = NumStrs;
840 return SL;
841}
842
Alexander Kornienkoae541212013-02-01 12:35:51 +0000843void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieu8ab09da2012-06-13 20:25:24 +0000844 switch (getKind()) {
845 case Ascii: break; // no prefix.
846 case Wide: OS << 'L'; break;
847 case UTF8: OS << "u8"; break;
848 case UTF16: OS << 'u'; break;
849 case UTF32: OS << 'U'; break;
850 }
851 OS << '"';
852 static const char Hex[] = "0123456789ABCDEF";
853
854 unsigned LastSlashX = getLength();
855 for (unsigned I = 0, N = getLength(); I != N; ++I) {
856 switch (uint32_t Char = getCodeUnit(I)) {
857 default:
858 // FIXME: Convert UTF-8 back to codepoints before rendering.
859
860 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
861 // Leave invalid surrogates alone; we'll use \x for those.
862 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
863 Char <= 0xdbff) {
864 uint32_t Trail = getCodeUnit(I + 1);
865 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
866 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
867 ++I;
868 }
869 }
870
871 if (Char > 0xff) {
872 // If this is a wide string, output characters over 0xff using \x
873 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
874 // codepoint: use \x escapes for invalid codepoints.
875 if (getKind() == Wide ||
876 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
877 // FIXME: Is this the best way to print wchar_t?
878 OS << "\\x";
879 int Shift = 28;
880 while ((Char >> Shift) == 0)
881 Shift -= 4;
882 for (/**/; Shift >= 0; Shift -= 4)
883 OS << Hex[(Char >> Shift) & 15];
884 LastSlashX = I;
885 break;
886 }
887
888 if (Char > 0xffff)
889 OS << "\\U00"
890 << Hex[(Char >> 20) & 15]
891 << Hex[(Char >> 16) & 15];
892 else
893 OS << "\\u";
894 OS << Hex[(Char >> 12) & 15]
895 << Hex[(Char >> 8) & 15]
896 << Hex[(Char >> 4) & 15]
897 << Hex[(Char >> 0) & 15];
898 break;
899 }
900
901 // If we used \x... for the previous character, and this character is a
902 // hexadecimal digit, prevent it being slurped as part of the \x.
903 if (LastSlashX + 1 == I) {
904 switch (Char) {
905 case '0': case '1': case '2': case '3': case '4':
906 case '5': case '6': case '7': case '8': case '9':
907 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
908 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
909 OS << "\"\"";
910 }
911 }
912
913 assert(Char <= 0xff &&
914 "Characters above 0xff should already have been handled.");
915
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000916 if (isPrintable(Char))
Richard Trieu8ab09da2012-06-13 20:25:24 +0000917 OS << (char)Char;
918 else // Output anything hard as an octal escape.
919 OS << '\\'
920 << (char)('0' + ((Char >> 6) & 7))
921 << (char)('0' + ((Char >> 3) & 7))
922 << (char)('0' + ((Char >> 0) & 7));
923 break;
924 // Handle some common non-printable cases to make dumps prettier.
925 case '\\': OS << "\\\\"; break;
926 case '"': OS << "\\\""; break;
927 case '\n': OS << "\\n"; break;
928 case '\t': OS << "\\t"; break;
929 case '\a': OS << "\\a"; break;
930 case '\b': OS << "\\b"; break;
931 }
932 }
933 OS << '"';
934}
935
Craig Topper05ed1a02013-08-18 10:09:15 +0000936void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedman64f45a22011-11-01 02:23:42 +0000937 StringKind Kind, bool IsPascal) {
938 //FIXME: we assume that the string data comes from a target that uses the same
939 // code unit size and endianess for the type of string.
940 this->Kind = Kind;
941 this->IsPascal = IsPascal;
942
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000943 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000944 assert((Str.size()%CharByteWidth == 0)
945 && "size of data must be multiple of CharByteWidth");
946 Length = Str.size()/CharByteWidth;
947
948 switch(CharByteWidth) {
949 case 1: {
950 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000951 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000952 StrData.asChar = AStrData;
953 break;
954 }
955 case 2: {
956 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000957 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000958 StrData.asUInt16 = AStrData;
959 break;
960 }
961 case 4: {
962 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000963 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000964 StrData.asUInt32 = AStrData;
965 break;
966 }
967 default:
968 assert(false && "unsupported CharByteWidth");
969 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000970}
971
Chris Lattner08f92e32010-11-17 07:37:15 +0000972/// getLocationOfByte - Return a source location that points to the specified
973/// byte of this string literal.
974///
975/// Strings are amazingly complex. They can be formed from multiple tokens and
976/// can have escape sequences in them in addition to the usual trigraph and
977/// escaped newline business. This routine handles this complexity.
978///
979SourceLocation StringLiteral::
980getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
981 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000982 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
983 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000984
Chris Lattner08f92e32010-11-17 07:37:15 +0000985 // Loop over all of the tokens in this string until we find the one that
986 // contains the byte we're looking for.
987 unsigned TokNo = 0;
988 while (1) {
989 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
990 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
991
992 // Get the spelling of the string so that we can get the data that makes up
993 // the string literal, not the identifier for the macro it is potentially
994 // expanded through.
995 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
996
997 // Re-lex the token to get its length and original spelling.
998 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
999 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001000 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +00001001 if (Invalid)
1002 return StrTokSpellingLoc;
1003
1004 const char *StrData = Buffer.data()+LocInfo.second;
1005
Chris Lattner08f92e32010-11-17 07:37:15 +00001006 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +00001007 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1008 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +00001009 Token TheTok;
1010 TheLexer.LexFromRawLexer(TheTok);
1011
1012 // Use the StringLiteralParser to compute the length of the string in bytes.
1013 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
1014 unsigned TokNumBytes = SLP.GetStringLength();
1015
1016 // If the byte is in this token, return the location of the byte.
1017 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +00001018 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +00001019 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1020
1021 // Now that we know the offset of the token in the spelling, use the
1022 // preprocessor to get the offset in the original source.
1023 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1024 }
1025
1026 // Move to the next string token.
1027 ++TokNo;
1028 ByteNo -= TokNumBytes;
1029 }
1030}
1031
1032
1033
Reid Spencer5f016e22007-07-11 17:01:13 +00001034/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1035/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie0bea8632012-10-08 01:11:04 +00001036StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001038 case UO_PostInc: return "++";
1039 case UO_PostDec: return "--";
1040 case UO_PreInc: return "++";
1041 case UO_PreDec: return "--";
1042 case UO_AddrOf: return "&";
1043 case UO_Deref: return "*";
1044 case UO_Plus: return "+";
1045 case UO_Minus: return "-";
1046 case UO_Not: return "~";
1047 case UO_LNot: return "!";
1048 case UO_Real: return "__real";
1049 case UO_Imag: return "__imag";
1050 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001052 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +00001053}
1054
John McCall2de56d12010-08-25 11:45:40 +00001055UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001056UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1057 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001058 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +00001059 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1060 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1061 case OO_Amp: return UO_AddrOf;
1062 case OO_Star: return UO_Deref;
1063 case OO_Plus: return UO_Plus;
1064 case OO_Minus: return UO_Minus;
1065 case OO_Tilde: return UO_Not;
1066 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001067 }
1068}
1069
1070OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1071 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +00001072 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1073 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1074 case UO_AddrOf: return OO_Amp;
1075 case UO_Deref: return OO_Star;
1076 case UO_Plus: return OO_Plus;
1077 case UO_Minus: return OO_Minus;
1078 case UO_Not: return OO_Tilde;
1079 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001080 default: return OO_None;
1081 }
1082}
1083
1084
Reid Spencer5f016e22007-07-11 17:01:13 +00001085//===----------------------------------------------------------------------===//
1086// Postfix Operators.
1087//===----------------------------------------------------------------------===//
1088
Craig Topper05ed1a02013-08-18 10:09:15 +00001089CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1090 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1091 ExprValueKind VK, SourceLocation rparenloc)
John McCallf89e55a2010-11-18 06:31:45 +00001092 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001093 fn->isTypeDependent(),
1094 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001095 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001096 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001097 NumArgs(args.size()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001099 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +00001100 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001101 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001102 if (args[i]->isTypeDependent())
1103 ExprBits.TypeDependent = true;
1104 if (args[i]->isValueDependent())
1105 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001106 if (args[i]->isInstantiationDependent())
1107 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001108 if (args[i]->containsUnexpandedParameterPack())
1109 ExprBits.ContainsUnexpandedParameterPack = true;
1110
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001111 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001112 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001113
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001114 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +00001115 RParenLoc = rparenloc;
1116}
Nate Begemane2ce1d92008-01-17 17:46:27 +00001117
Craig Topper05ed1a02013-08-18 10:09:15 +00001118CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCallf89e55a2010-11-18 06:31:45 +00001119 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1120 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001121 fn->isTypeDependent(),
1122 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001123 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001124 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001125 NumArgs(args.size()) {
Ted Kremenek668bf912009-02-09 20:51:47 +00001126
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001127 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001128 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001129 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001130 if (args[i]->isTypeDependent())
1131 ExprBits.TypeDependent = true;
1132 if (args[i]->isValueDependent())
1133 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001134 if (args[i]->isInstantiationDependent())
1135 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001136 if (args[i]->containsUnexpandedParameterPack())
1137 ExprBits.ContainsUnexpandedParameterPack = true;
1138
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001139 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001140 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001141
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001142 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 RParenLoc = rparenloc;
1144}
1145
Craig Topper05ed1a02013-08-18 10:09:15 +00001146CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Mike Stump1eb44332009-09-09 15:08:12 +00001147 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001148 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001149 SubExprs = new (C) Stmt*[PREARGS_START];
1150 CallExprBits.NumPreArgs = 0;
1151}
1152
Craig Topper05ed1a02013-08-18 10:09:15 +00001153CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001154 EmptyShell Empty)
1155 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1156 // FIXME: Why do we allocate this?
1157 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1158 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001159}
1160
Nuno Lopesd20254f2009-12-20 23:11:08 +00001161Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +00001162 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +00001163
1164 while (SubstNonTypeTemplateParmExpr *NTTP
1165 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1166 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1167 }
1168
Sebastian Redl20012152010-09-10 20:55:30 +00001169 // If we're calling a dereference, look at the pointer instead.
1170 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1171 if (BO->isPtrMemOp())
1172 CEE = BO->getRHS()->IgnoreParenCasts();
1173 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1174 if (UO->getOpcode() == UO_Deref)
1175 CEE = UO->getSubExpr()->IgnoreParenCasts();
1176 }
Chris Lattner6346f962009-07-17 15:46:27 +00001177 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001178 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001179 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1180 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001181
1182 return 0;
1183}
1184
Nuno Lopesd20254f2009-12-20 23:11:08 +00001185FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001186 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001187}
1188
Chris Lattnerd18b3292007-12-28 05:25:02 +00001189/// setNumArgs - This changes the number of arguments present in this call.
1190/// Any orphaned expressions are deleted by this, and any new operands are set
1191/// to null.
Craig Topper05ed1a02013-08-18 10:09:15 +00001192void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001193 // No change, just return.
1194 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Chris Lattnerd18b3292007-12-28 05:25:02 +00001196 // If shrinking # arguments, just delete the extras and forgot them.
1197 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001198 this->NumArgs = NumArgs;
1199 return;
1200 }
1201
1202 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001203 unsigned NumPreArgs = getNumPreArgs();
1204 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001205 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001206 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001207 NewSubExprs[i] = SubExprs[i];
1208 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001209 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1210 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001211 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Douglas Gregor88c9a462009-04-17 21:46:47 +00001213 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001214 SubExprs = NewSubExprs;
1215 this->NumArgs = NumArgs;
1216}
1217
Chris Lattnercb888962008-10-06 05:00:53 +00001218/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1219/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001220unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001221 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001222 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001223 // ImplicitCastExpr.
1224 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1225 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001226 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001228 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1229 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001230 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Anders Carlssonbcba2012008-01-31 02:13:57 +00001232 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1233 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001234 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001236 if (!FDecl->getIdentifier())
1237 return 0;
1238
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001239 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001240}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001241
Richard Smithba571832013-01-17 23:46:04 +00001242bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1243 if (unsigned BI = isBuiltinCall())
1244 return Ctx.BuiltinInfo.isUnevaluated(BI);
1245 return false;
1246}
1247
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001248QualType CallExpr::getCallReturnType() const {
1249 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001250 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001251 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001252 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001253 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001254 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1255 // This should never be overloaded and so should never return null.
1256 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001257
John McCall864c0412011-04-26 20:42:42 +00001258 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001259 return FnType->getResultType();
1260}
Chris Lattnercb888962008-10-06 05:00:53 +00001261
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001262SourceLocation CallExpr::getLocStart() const {
1263 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001264 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001265
1266 SourceLocation begin = getCallee()->getLocStart();
1267 if (begin.isInvalid() && getNumArgs() > 0)
1268 begin = getArg(0)->getLocStart();
1269 return begin;
1270}
1271SourceLocation CallExpr::getLocEnd() const {
1272 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001273 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001274
1275 SourceLocation end = getRParenLoc();
1276 if (end.isInvalid() && getNumArgs() > 0)
1277 end = getArg(getNumArgs() - 1)->getLocEnd();
1278 return end;
1279}
John McCall2882eca2011-02-21 06:23:05 +00001280
Craig Topper05ed1a02013-08-18 10:09:15 +00001281OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001282 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001283 TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001284 ArrayRef<OffsetOfNode> comps,
1285 ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001286 SourceLocation RParenLoc) {
1287 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001288 sizeof(OffsetOfNode) * comps.size() +
1289 sizeof(Expr*) * exprs.size());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001290
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001291 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1292 RParenLoc);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001293}
1294
Craig Topper05ed1a02013-08-18 10:09:15 +00001295OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001296 unsigned numComps, unsigned numExprs) {
1297 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1298 sizeof(OffsetOfNode) * numComps +
1299 sizeof(Expr*) * numExprs);
1300 return new (Mem) OffsetOfExpr(numComps, numExprs);
1301}
1302
Craig Topper05ed1a02013-08-18 10:09:15 +00001303OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001304 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001305 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001306 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001307 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1308 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001309 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001310 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001311 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001312 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001313 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001314{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001315 for (unsigned i = 0; i != comps.size(); ++i) {
1316 setComponent(i, comps[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001317 }
Sean Huntc3021132010-05-05 15:23:54 +00001318
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001319 for (unsigned i = 0; i != exprs.size(); ++i) {
1320 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001321 ExprBits.ValueDependent = true;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001322 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001323 ExprBits.ContainsUnexpandedParameterPack = true;
1324
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001325 setIndexExpr(i, exprs[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001326 }
1327}
1328
1329IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1330 assert(getKind() == Field || getKind() == Identifier);
1331 if (getKind() == Field)
1332 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001333
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001334 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1335}
1336
Craig Topper05ed1a02013-08-18 10:09:15 +00001337MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001338 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001339 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001340 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001341 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001342 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001343 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001344 QualType ty,
1345 ExprValueKind vk,
1346 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001347 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001348
Douglas Gregor40d96a62011-02-28 21:54:11 +00001349 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001350 founddecl.getDecl() != memberdecl ||
1351 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001352 if (hasQualOrFound)
1353 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001354
John McCalld5532b62009-11-23 01:53:49 +00001355 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001356 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1357 else if (TemplateKWLoc.isValid())
1358 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Chris Lattner32488542010-10-30 05:14:06 +00001360 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001361 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1362 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001363
1364 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001365 // FIXME: Wrong. We should be looking at the member declaration we found.
1366 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001367 E->setValueDependent(true);
1368 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001369 E->setInstantiationDependent(true);
1370 }
1371 else if (QualifierLoc &&
1372 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1373 E->setInstantiationDependent(true);
1374
John McCall6bb80172010-03-30 21:47:33 +00001375 E->HasQualifierOrFoundDecl = true;
1376
1377 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001378 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001379 NQ->FoundDecl = founddecl;
1380 }
1381
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001382 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1383
John McCall6bb80172010-03-30 21:47:33 +00001384 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001385 bool Dependent = false;
1386 bool InstantiationDependent = false;
1387 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001388 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1389 Dependent,
1390 InstantiationDependent,
1391 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001392 if (InstantiationDependent)
1393 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001394 } else if (TemplateKWLoc.isValid()) {
1395 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001396 }
1397
1398 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001399}
1400
Daniel Dunbar396ec672012-03-09 15:39:15 +00001401SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001402 if (isImplicitAccess()) {
1403 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001404 return getQualifierLoc().getBeginLoc();
1405 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001406 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001407
Daniel Dunbar396ec672012-03-09 15:39:15 +00001408 // FIXME: We don't want this to happen. Rather, we should be able to
1409 // detect all kinds of implicit accesses more cleanly.
1410 SourceLocation BaseStartLoc = getBase()->getLocStart();
1411 if (BaseStartLoc.isValid())
1412 return BaseStartLoc;
1413 return MemberLoc;
1414}
1415SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001416 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbar396ec672012-03-09 15:39:15 +00001417 if (hasExplicitTemplateArgs())
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001418 EndLoc = getRAngleLoc();
1419 else if (EndLoc.isInvalid())
1420 EndLoc = getBase()->getLocEnd();
1421 return EndLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001422}
1423
John McCall1d9b3b22011-09-09 05:25:32 +00001424void CastExpr::CheckCastConsistency() const {
1425 switch (getCastKind()) {
1426 case CK_DerivedToBase:
1427 case CK_UncheckedDerivedToBase:
1428 case CK_DerivedToBaseMemberPointer:
1429 case CK_BaseToDerived:
1430 case CK_BaseToDerivedMemberPointer:
1431 assert(!path_empty() && "Cast kind should have a base path!");
1432 break;
1433
1434 case CK_CPointerToObjCPointerCast:
1435 assert(getType()->isObjCObjectPointerType());
1436 assert(getSubExpr()->getType()->isPointerType());
1437 goto CheckNoBasePath;
1438
1439 case CK_BlockPointerToObjCPointerCast:
1440 assert(getType()->isObjCObjectPointerType());
1441 assert(getSubExpr()->getType()->isBlockPointerType());
1442 goto CheckNoBasePath;
1443
John McCall4d4e5c12012-02-15 01:22:51 +00001444 case CK_ReinterpretMemberPointer:
1445 assert(getType()->isMemberPointerType());
1446 assert(getSubExpr()->getType()->isMemberPointerType());
1447 goto CheckNoBasePath;
1448
John McCall1d9b3b22011-09-09 05:25:32 +00001449 case CK_BitCast:
1450 // Arbitrary casts to C pointer types count as bitcasts.
1451 // Otherwise, we should only have block and ObjC pointer casts
1452 // here if they stay within the type kind.
1453 if (!getType()->isPointerType()) {
1454 assert(getType()->isObjCObjectPointerType() ==
1455 getSubExpr()->getType()->isObjCObjectPointerType());
1456 assert(getType()->isBlockPointerType() ==
1457 getSubExpr()->getType()->isBlockPointerType());
1458 }
1459 goto CheckNoBasePath;
1460
1461 case CK_AnyPointerToBlockPointerCast:
1462 assert(getType()->isBlockPointerType());
1463 assert(getSubExpr()->getType()->isAnyPointerType() &&
1464 !getSubExpr()->getType()->isBlockPointerType());
1465 goto CheckNoBasePath;
1466
Douglas Gregorac1303e2012-02-22 05:02:47 +00001467 case CK_CopyAndAutoreleaseBlockObject:
1468 assert(getType()->isBlockPointerType());
1469 assert(getSubExpr()->getType()->isBlockPointerType());
1470 goto CheckNoBasePath;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001471
1472 case CK_FunctionToPointerDecay:
1473 assert(getType()->isPointerType());
1474 assert(getSubExpr()->getType()->isFunctionType());
1475 goto CheckNoBasePath;
1476
John McCall1d9b3b22011-09-09 05:25:32 +00001477 // These should not have an inheritance path.
1478 case CK_Dynamic:
1479 case CK_ToUnion:
1480 case CK_ArrayToPointerDecay:
John McCall1d9b3b22011-09-09 05:25:32 +00001481 case CK_NullToMemberPointer:
1482 case CK_NullToPointer:
1483 case CK_ConstructorConversion:
1484 case CK_IntegralToPointer:
1485 case CK_PointerToIntegral:
1486 case CK_ToVoid:
1487 case CK_VectorSplat:
1488 case CK_IntegralCast:
1489 case CK_IntegralToFloating:
1490 case CK_FloatingToIntegral:
1491 case CK_FloatingCast:
1492 case CK_ObjCObjectLValueCast:
1493 case CK_FloatingRealToComplex:
1494 case CK_FloatingComplexToReal:
1495 case CK_FloatingComplexCast:
1496 case CK_FloatingComplexToIntegralComplex:
1497 case CK_IntegralRealToComplex:
1498 case CK_IntegralComplexToReal:
1499 case CK_IntegralComplexCast:
1500 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001501 case CK_ARCProduceObject:
1502 case CK_ARCConsumeObject:
1503 case CK_ARCReclaimReturnedObject:
1504 case CK_ARCExtendBlockObject:
Guy Benyeie6b9d802013-01-20 12:31:11 +00001505 case CK_ZeroToOCLEvent:
John McCall1d9b3b22011-09-09 05:25:32 +00001506 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1507 goto CheckNoBasePath;
1508
1509 case CK_Dependent:
1510 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001511 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001512 case CK_AtomicToNonAtomic:
1513 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001514 case CK_PointerToBoolean:
1515 case CK_IntegralToBoolean:
1516 case CK_FloatingToBoolean:
1517 case CK_MemberPointerToBoolean:
1518 case CK_FloatingComplexToBoolean:
1519 case CK_IntegralComplexToBoolean:
1520 case CK_LValueBitCast: // -> bool&
1521 case CK_UserDefinedConversion: // operator bool()
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001522 case CK_BuiltinFnToFnPtr:
John McCall1d9b3b22011-09-09 05:25:32 +00001523 CheckNoBasePath:
1524 assert(path_empty() && "Cast kind should not have a base path!");
1525 break;
1526 }
1527}
1528
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001529const char *CastExpr::getCastKindName() const {
1530 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001531 case CK_Dependent:
1532 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001533 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001534 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001535 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001536 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001537 case CK_LValueToRValue:
1538 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001539 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001540 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001541 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001542 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001543 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001544 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001545 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001546 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001547 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001548 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001549 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001550 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001551 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001552 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001553 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001554 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001555 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001556 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001557 case CK_NullToPointer:
1558 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001559 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001560 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001561 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001562 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001563 case CK_ReinterpretMemberPointer:
1564 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001565 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001566 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001567 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001568 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001569 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001570 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001571 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001572 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001573 case CK_PointerToBoolean:
1574 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001575 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001576 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001577 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001578 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001579 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001580 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001581 case CK_IntegralToBoolean:
1582 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001583 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001584 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001585 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001586 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001587 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001588 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001589 case CK_FloatingToBoolean:
1590 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001591 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001592 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001593 case CK_CPointerToObjCPointerCast:
1594 return "CPointerToObjCPointerCast";
1595 case CK_BlockPointerToObjCPointerCast:
1596 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001597 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001598 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001599 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001600 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001601 case CK_FloatingRealToComplex:
1602 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001603 case CK_FloatingComplexToReal:
1604 return "FloatingComplexToReal";
1605 case CK_FloatingComplexToBoolean:
1606 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001607 case CK_FloatingComplexCast:
1608 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001609 case CK_FloatingComplexToIntegralComplex:
1610 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001611 case CK_IntegralRealToComplex:
1612 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001613 case CK_IntegralComplexToReal:
1614 return "IntegralComplexToReal";
1615 case CK_IntegralComplexToBoolean:
1616 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001617 case CK_IntegralComplexCast:
1618 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001619 case CK_IntegralComplexToFloatingComplex:
1620 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001621 case CK_ARCConsumeObject:
1622 return "ARCConsumeObject";
1623 case CK_ARCProduceObject:
1624 return "ARCProduceObject";
1625 case CK_ARCReclaimReturnedObject:
1626 return "ARCReclaimReturnedObject";
1627 case CK_ARCExtendBlockObject:
1628 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001629 case CK_AtomicToNonAtomic:
1630 return "AtomicToNonAtomic";
1631 case CK_NonAtomicToAtomic:
1632 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001633 case CK_CopyAndAutoreleaseBlockObject:
1634 return "CopyAndAutoreleaseBlockObject";
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001635 case CK_BuiltinFnToFnPtr:
1636 return "BuiltinFnToFnPtr";
Guy Benyeie6b9d802013-01-20 12:31:11 +00001637 case CK_ZeroToOCLEvent:
1638 return "ZeroToOCLEvent";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001639 }
Mike Stump1eb44332009-09-09 15:08:12 +00001640
John McCall2bb5d002010-11-13 09:02:35 +00001641 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001642}
1643
Douglas Gregor6eef5192009-12-14 19:27:10 +00001644Expr *CastExpr::getSubExprAsWritten() {
1645 Expr *SubExpr = 0;
1646 CastExpr *E = this;
1647 do {
1648 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001649
1650 // Skip through reference binding to temporary.
1651 if (MaterializeTemporaryExpr *Materialize
1652 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1653 SubExpr = Materialize->GetTemporaryExpr();
1654
Douglas Gregor6eef5192009-12-14 19:27:10 +00001655 // Skip any temporary bindings; they're implicit.
1656 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1657 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001658
Douglas Gregor6eef5192009-12-14 19:27:10 +00001659 // Conversions by constructor and conversion functions have a
1660 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001661 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001662 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001663 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001664 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001665
Douglas Gregor6eef5192009-12-14 19:27:10 +00001666 // If the subexpression we're left with is an implicit cast, look
1667 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001668 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1669
Douglas Gregor6eef5192009-12-14 19:27:10 +00001670 return SubExpr;
1671}
1672
John McCallf871d0c2010-08-07 06:22:56 +00001673CXXBaseSpecifier **CastExpr::path_buffer() {
1674 switch (getStmtClass()) {
1675#define ABSTRACT_STMT(x)
1676#define CASTEXPR(Type, Base) \
1677 case Stmt::Type##Class: \
1678 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1679#define STMT(Type, Base)
1680#include "clang/AST/StmtNodes.inc"
1681 default:
1682 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001683 }
1684}
1685
1686void CastExpr::setCastPath(const CXXCastPath &Path) {
1687 assert(Path.size() == path_size());
1688 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1689}
1690
Craig Topper05ed1a02013-08-18 10:09:15 +00001691ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallf871d0c2010-08-07 06:22:56 +00001692 CastKind Kind, Expr *Operand,
1693 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001694 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001695 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1696 void *Buffer =
1697 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1698 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001699 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001700 if (PathSize) E->setCastPath(*BasePath);
1701 return E;
1702}
1703
Craig Topper05ed1a02013-08-18 10:09:15 +00001704ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallf871d0c2010-08-07 06:22:56 +00001705 unsigned PathSize) {
1706 void *Buffer =
1707 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1708 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1709}
1710
1711
Craig Topper05ed1a02013-08-18 10:09:15 +00001712CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001713 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001714 const CXXCastPath *BasePath,
1715 TypeSourceInfo *WrittenTy,
1716 SourceLocation L, SourceLocation R) {
1717 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1718 void *Buffer =
1719 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1720 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001721 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001722 if (PathSize) E->setCastPath(*BasePath);
1723 return E;
1724}
1725
Craig Topper05ed1a02013-08-18 10:09:15 +00001726CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1727 unsigned PathSize) {
John McCallf871d0c2010-08-07 06:22:56 +00001728 void *Buffer =
1729 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1730 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1731}
1732
Reid Spencer5f016e22007-07-11 17:01:13 +00001733/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1734/// corresponds to, e.g. "<<=".
David Blaikie0bea8632012-10-08 01:11:04 +00001735StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001736 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001737 case BO_PtrMemD: return ".*";
1738 case BO_PtrMemI: return "->*";
1739 case BO_Mul: return "*";
1740 case BO_Div: return "/";
1741 case BO_Rem: return "%";
1742 case BO_Add: return "+";
1743 case BO_Sub: return "-";
1744 case BO_Shl: return "<<";
1745 case BO_Shr: return ">>";
1746 case BO_LT: return "<";
1747 case BO_GT: return ">";
1748 case BO_LE: return "<=";
1749 case BO_GE: return ">=";
1750 case BO_EQ: return "==";
1751 case BO_NE: return "!=";
1752 case BO_And: return "&";
1753 case BO_Xor: return "^";
1754 case BO_Or: return "|";
1755 case BO_LAnd: return "&&";
1756 case BO_LOr: return "||";
1757 case BO_Assign: return "=";
1758 case BO_MulAssign: return "*=";
1759 case BO_DivAssign: return "/=";
1760 case BO_RemAssign: return "%=";
1761 case BO_AddAssign: return "+=";
1762 case BO_SubAssign: return "-=";
1763 case BO_ShlAssign: return "<<=";
1764 case BO_ShrAssign: return ">>=";
1765 case BO_AndAssign: return "&=";
1766 case BO_XorAssign: return "^=";
1767 case BO_OrAssign: return "|=";
1768 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001770
David Blaikie30263482012-01-20 21:50:17 +00001771 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001772}
1773
John McCall2de56d12010-08-25 11:45:40 +00001774BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001775BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1776 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001777 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001778 case OO_Plus: return BO_Add;
1779 case OO_Minus: return BO_Sub;
1780 case OO_Star: return BO_Mul;
1781 case OO_Slash: return BO_Div;
1782 case OO_Percent: return BO_Rem;
1783 case OO_Caret: return BO_Xor;
1784 case OO_Amp: return BO_And;
1785 case OO_Pipe: return BO_Or;
1786 case OO_Equal: return BO_Assign;
1787 case OO_Less: return BO_LT;
1788 case OO_Greater: return BO_GT;
1789 case OO_PlusEqual: return BO_AddAssign;
1790 case OO_MinusEqual: return BO_SubAssign;
1791 case OO_StarEqual: return BO_MulAssign;
1792 case OO_SlashEqual: return BO_DivAssign;
1793 case OO_PercentEqual: return BO_RemAssign;
1794 case OO_CaretEqual: return BO_XorAssign;
1795 case OO_AmpEqual: return BO_AndAssign;
1796 case OO_PipeEqual: return BO_OrAssign;
1797 case OO_LessLess: return BO_Shl;
1798 case OO_GreaterGreater: return BO_Shr;
1799 case OO_LessLessEqual: return BO_ShlAssign;
1800 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1801 case OO_EqualEqual: return BO_EQ;
1802 case OO_ExclaimEqual: return BO_NE;
1803 case OO_LessEqual: return BO_LE;
1804 case OO_GreaterEqual: return BO_GE;
1805 case OO_AmpAmp: return BO_LAnd;
1806 case OO_PipePipe: return BO_LOr;
1807 case OO_Comma: return BO_Comma;
1808 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001809 }
1810}
1811
1812OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1813 static const OverloadedOperatorKind OverOps[] = {
1814 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1815 OO_Star, OO_Slash, OO_Percent,
1816 OO_Plus, OO_Minus,
1817 OO_LessLess, OO_GreaterGreater,
1818 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1819 OO_EqualEqual, OO_ExclaimEqual,
1820 OO_Amp,
1821 OO_Caret,
1822 OO_Pipe,
1823 OO_AmpAmp,
1824 OO_PipePipe,
1825 OO_Equal, OO_StarEqual,
1826 OO_SlashEqual, OO_PercentEqual,
1827 OO_PlusEqual, OO_MinusEqual,
1828 OO_LessLessEqual, OO_GreaterGreaterEqual,
1829 OO_AmpEqual, OO_CaretEqual,
1830 OO_PipeEqual,
1831 OO_Comma
1832 };
1833 return OverOps[Opc];
1834}
1835
Craig Topper05ed1a02013-08-18 10:09:15 +00001836InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001837 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001838 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001839 false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001840 InitExprs(C, initExprs.size()),
Abramo Bagnara23700f02012-11-08 18:41:43 +00001841 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001842{
1843 sawArrayRangeDesignator(false);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001844 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001845 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001846 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001847 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001848 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001849 if (initExprs[I]->isInstantiationDependent())
1850 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001851 if (initExprs[I]->containsUnexpandedParameterPack())
1852 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001853 }
Sean Huntc3021132010-05-05 15:23:54 +00001854
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001855 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001856}
Reid Spencer5f016e22007-07-11 17:01:13 +00001857
Craig Topper05ed1a02013-08-18 10:09:15 +00001858void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001859 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001860 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001861}
1862
Craig Topper05ed1a02013-08-18 10:09:15 +00001863void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001864 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001865}
1866
Craig Topper05ed1a02013-08-18 10:09:15 +00001867Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001868 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001869 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001870 InitExprs.back() = expr;
1871 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001872 }
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Douglas Gregor4c678342009-01-28 21:54:33 +00001874 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1875 InitExprs[Init] = expr;
1876 return Result;
1877}
1878
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001879void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001880 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001881 ArrayFillerOrUnionFieldInit = filler;
1882 // Fill out any "holes" in the array due to designated initializers.
1883 Expr **inits = getInits();
1884 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1885 if (inits[i] == 0)
1886 inits[i] = filler;
1887}
1888
Richard Smithfe587202012-04-15 02:50:59 +00001889bool InitListExpr::isStringLiteralInit() const {
1890 if (getNumInits() != 1)
1891 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001892 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1893 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smithfe587202012-04-15 02:50:59 +00001894 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001895 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smithfe587202012-04-15 02:50:59 +00001896 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1897}
1898
Erik Verbruggen65d78312012-12-25 14:51:39 +00001899SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara23700f02012-11-08 18:41:43 +00001900 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen65d78312012-12-25 14:51:39 +00001901 return SyntacticForm->getLocStart();
1902 SourceLocation Beg = LBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001903 if (Beg.isInvalid()) {
1904 // Find the first non-null initializer.
1905 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1906 E = InitExprs.end();
1907 I != E; ++I) {
1908 if (Stmt *S = *I) {
1909 Beg = S->getLocStart();
1910 break;
1911 }
1912 }
1913 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001914 return Beg;
1915}
1916
1917SourceLocation InitListExpr::getLocEnd() const {
1918 if (InitListExpr *SyntacticForm = getSyntacticForm())
1919 return SyntacticForm->getLocEnd();
1920 SourceLocation End = RBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001921 if (End.isInvalid()) {
1922 // Find the first non-null initializer from the end.
1923 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen65d78312012-12-25 14:51:39 +00001924 E = InitExprs.rend();
1925 I != E; ++I) {
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001926 if (Stmt *S = *I) {
Erik Verbruggen65d78312012-12-25 14:51:39 +00001927 End = S->getLocEnd();
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001928 break;
Erik Verbruggen65d78312012-12-25 14:51:39 +00001929 }
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001930 }
1931 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001932 return End;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001933}
1934
Steve Naroffbfdcae62008-09-04 15:31:07 +00001935/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001936///
John McCalla345edb2012-02-17 03:32:35 +00001937const FunctionProtoType *BlockExpr::getFunctionType() const {
1938 // The block pointer is never sugared, but the function type might be.
1939 return cast<BlockPointerType>(getType())
1940 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001941}
1942
Mike Stump1eb44332009-09-09 15:08:12 +00001943SourceLocation BlockExpr::getCaretLocation() const {
1944 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001945}
Mike Stump1eb44332009-09-09 15:08:12 +00001946const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001947 return TheBlock->getBody();
1948}
Mike Stump1eb44332009-09-09 15:08:12 +00001949Stmt *BlockExpr::getBody() {
1950 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001951}
Steve Naroff56ee6892008-10-08 17:01:13 +00001952
1953
Reid Spencer5f016e22007-07-11 17:01:13 +00001954//===----------------------------------------------------------------------===//
1955// Generic Expression Routines
1956//===----------------------------------------------------------------------===//
1957
Chris Lattner026dc962009-02-14 07:37:35 +00001958/// isUnusedResultAWarning - Return true if this immediate expression should
1959/// be warned about if the result is unused. If so, fill in Loc and Ranges
1960/// with location to warn on and the source range[s] to report with the
1961/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001962bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1963 SourceRange &R1, SourceRange &R2,
1964 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001965 // Don't warn if the expr is type dependent. The type could end up
1966 // instantiating to void.
1967 if (isTypeDependent())
1968 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Reid Spencer5f016e22007-07-11 17:01:13 +00001970 switch (getStmtClass()) {
1971 default:
John McCall0faede62010-03-12 07:11:26 +00001972 if (getType()->isVoidType())
1973 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001974 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001975 Loc = getExprLoc();
1976 R1 = getSourceRange();
1977 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001979 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001980 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001981 case GenericSelectionExprClass:
1982 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001983 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmana5e66012013-07-20 00:40:58 +00001984 case ChooseExprClass:
1985 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1986 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001987 case UnaryOperatorClass: {
1988 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001989
Reid Spencer5f016e22007-07-11 17:01:13 +00001990 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001991 case UO_Plus:
1992 case UO_Minus:
1993 case UO_AddrOf:
1994 case UO_Not:
1995 case UO_LNot:
1996 case UO_Deref:
1997 break;
John McCall2de56d12010-08-25 11:45:40 +00001998 case UO_PostInc:
1999 case UO_PostDec:
2000 case UO_PreInc:
2001 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00002002 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00002003 case UO_Real:
2004 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00002005 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00002006 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2007 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00002008 return false;
2009 break;
John McCall2de56d12010-08-25 11:45:40 +00002010 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00002011 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00002012 }
Eli Friedmana6115062012-05-24 00:47:05 +00002013 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002014 Loc = UO->getOperatorLoc();
2015 R1 = UO->getSubExpr()->getSourceRange();
2016 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 }
Chris Lattnere7716e62007-12-01 06:07:34 +00002018 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002019 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00002020 switch (BO->getOpcode()) {
2021 default:
2022 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00002023 // Consider the RHS of comma for side effects. LHS was checked by
2024 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00002025 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00002026 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2027 // lvalue-ness) of an assignment written in a macro.
2028 if (IntegerLiteral *IE =
2029 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2030 if (IE->getValue() == 0)
2031 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002032 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00002033 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00002034 case BO_LAnd:
2035 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00002036 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2037 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00002038 return false;
2039 break;
John McCallbf0ee352010-02-16 04:10:53 +00002040 }
Chris Lattner026dc962009-02-14 07:37:35 +00002041 if (BO->isAssignmentOp())
2042 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002043 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002044 Loc = BO->getOperatorLoc();
2045 R1 = BO->getLHS()->getSourceRange();
2046 R2 = BO->getRHS()->getSourceRange();
2047 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00002048 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00002049 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00002050 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002051 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002052 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002053
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002054 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002055 // If only one of the LHS or RHS is a warning, the operator might
2056 // be being used for control flow. Only warn if both the LHS and
2057 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002058 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002059 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002060 return false;
2061 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00002062 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00002063 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002064 }
2065
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002067 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002068 Loc = cast<MemberExpr>(this)->getMemberLoc();
2069 R1 = SourceRange(Loc, Loc);
2070 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2071 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002074 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002075 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2076 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2077 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2078 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00002079
Chandler Carruth9b106832011-08-17 09:49:44 +00002080 case CXXOperatorCallExprClass: {
2081 // We warn about operator== and operator!= even when user-defined operator
2082 // overloads as there is no reasonable way to define these such that they
2083 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2084 // warning: these operators are commonly typo'ed, and so warning on them
2085 // provides additional value as well. If this list is updated,
2086 // DiagnoseUnusedComparison should be as well.
2087 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2088 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002089 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00002090 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002091 Loc = Op->getOperatorLoc();
2092 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00002093 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002094 }
Chandler Carruth9b106832011-08-17 09:49:44 +00002095
2096 // Fallthrough for generic call handling.
2097 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002098 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002099 case CXXMemberCallExprClass:
2100 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002101 // If this is a direct call, get the callee.
2102 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00002103 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00002104 // If the callee has attribute pure, const, or warn_unused_result, warn
2105 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002106 //
2107 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2108 // updated to match for QoI.
2109 if (FD->getAttr<WarnUnusedResultAttr>() ||
2110 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002111 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002112 Loc = CE->getCallee()->getLocStart();
2113 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002114
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002115 if (unsigned NumArgs = CE->getNumArgs())
2116 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2117 CE->getArg(NumArgs-1)->getLocEnd());
2118 return true;
2119 }
Chris Lattner026dc962009-02-14 07:37:35 +00002120 }
2121 return false;
2122 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002123
Matt Beaumont-Gay84c3b972012-10-23 06:15:26 +00002124 // If we don't know precisely what we're looking at, let's not warn.
2125 case UnresolvedLookupExprClass:
2126 case CXXUnresolvedConstructExprClass:
2127 return false;
2128
Anders Carlsson58beed92009-11-17 17:11:23 +00002129 case CXXTemporaryObjectExprClass:
Lubos Lunak81e45492013-07-21 13:15:58 +00002130 case CXXConstructExprClass: {
2131 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2132 if (Type->hasAttr<WarnUnusedAttr>()) {
2133 WarnE = this;
2134 Loc = getLocStart();
2135 R1 = getSourceRange();
2136 return true;
2137 }
2138 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002139 return false;
Lubos Lunak81e45492013-07-21 13:15:58 +00002140 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002141
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002142 case ObjCMessageExprClass: {
2143 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00002144 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002145 ME->isInstanceMessage() &&
2146 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas4bdb6022013-07-19 20:25:56 +00002147 ME->getMethodFamily() == OMF_init) {
Eli Friedmana6115062012-05-24 00:47:05 +00002148 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00002149 Loc = getExprLoc();
2150 R1 = ME->getSourceRange();
2151 return true;
2152 }
2153
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002154 const ObjCMethodDecl *MD = ME->getMethodDecl();
2155 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002156 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002157 Loc = getExprLoc();
2158 return true;
2159 }
Chris Lattner026dc962009-02-14 07:37:35 +00002160 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002161 }
Mike Stump1eb44332009-09-09 15:08:12 +00002162
John McCall12f78a62010-12-02 01:19:52 +00002163 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002164 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00002165 Loc = getExprLoc();
2166 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00002167 return true;
John McCall12f78a62010-12-02 01:19:52 +00002168
John McCall4b9c2d22011-11-06 09:01:30 +00002169 case PseudoObjectExprClass: {
2170 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2171
2172 // Only complain about things that have the form of a getter.
2173 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2174 isa<BinaryOperator>(PO->getSyntacticForm()))
2175 return false;
2176
Eli Friedmana6115062012-05-24 00:47:05 +00002177 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00002178 Loc = getExprLoc();
2179 R1 = getSourceRange();
2180 return true;
2181 }
2182
Chris Lattner611b2ec2008-07-26 19:51:01 +00002183 case StmtExprClass: {
2184 // Statement exprs don't logically have side effects themselves, but are
2185 // sometimes used in macros in ways that give them a type that is unused.
2186 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2187 // however, if the result of the stmt expr is dead, we don't want to emit a
2188 // warning.
2189 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002190 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002191 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002192 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002193 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2194 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002195 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002196 }
Mike Stump1eb44332009-09-09 15:08:12 +00002197
John McCall0faede62010-03-12 07:11:26 +00002198 if (getType()->isVoidType())
2199 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002200 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002201 Loc = cast<StmtExpr>(this)->getLParenLoc();
2202 R1 = getSourceRange();
2203 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002204 }
Eli Friedman63199172012-09-24 23:02:26 +00002205 case CXXFunctionalCastExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002206 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002207 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002208 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002209 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002210 if (CE->getCastKind() == CK_ToVoid) {
2211 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002212 CE->getSubExpr()->getType().isVolatileQualified()) {
2213 const DeclRefExpr *DRE =
2214 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2215 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2216 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2217 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2218 R1, R2, Ctx);
2219 }
2220 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002221 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002222 }
Eli Friedman4059da82012-05-24 21:05:41 +00002223
Eli Friedmana6115062012-05-24 00:47:05 +00002224 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002225 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002226 if (CE->getCastKind() == CK_ConstructorConversion)
2227 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002228
Eli Friedmana6115062012-05-24 00:47:05 +00002229 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002230 if (const CXXFunctionalCastExpr *CXXCE =
2231 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedmancdd4b782013-08-15 22:02:56 +00002232 Loc = CXXCE->getLocStart();
Eli Friedman4059da82012-05-24 21:05:41 +00002233 R1 = CXXCE->getSubExpr()->getSourceRange();
2234 } else {
2235 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2236 Loc = CStyleCE->getLParenLoc();
2237 R1 = CStyleCE->getSubExpr()->getSourceRange();
2238 }
Chris Lattner026dc962009-02-14 07:37:35 +00002239 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002240 }
Eli Friedmana6115062012-05-24 00:47:05 +00002241 case ImplicitCastExprClass: {
2242 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002243
Eli Friedmana6115062012-05-24 00:47:05 +00002244 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2245 if (ICE->getCastKind() == CK_LValueToRValue &&
2246 ICE->getSubExpr()->getType().isVolatileQualified())
2247 return false;
2248
2249 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2250 }
Chris Lattner04421082008-04-08 04:40:51 +00002251 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002252 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002253 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smithc3bf52c2013-04-20 22:23:05 +00002254 case CXXDefaultInitExprClass:
2255 return (cast<CXXDefaultInitExpr>(this)
2256 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002257
2258 case CXXNewExprClass:
2259 // FIXME: In theory, there might be new expressions that don't have side
2260 // effects (e.g. a placement new with an uninitialized POD).
2261 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002262 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002263 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002264 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002265 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002266 case ExprWithCleanupsClass:
2267 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002268 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002269 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002270}
2271
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002272/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002273/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002274bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002275 const Expr *E = IgnoreParens();
2276 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002277 default:
2278 return false;
2279 case ObjCIvarRefExprClass:
2280 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002281 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002282 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002283 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002284 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002285 case MaterializeTemporaryExprClass:
2286 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2287 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002288 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002289 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002290 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002291 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002292
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002293 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2294 if (VD->hasGlobalStorage())
2295 return true;
2296 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002297 // dereferencing to a pointer is always a gc'able candidate,
2298 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002299 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002300 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002301 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002302 return false;
2303 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002304 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002305 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002306 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002307 }
2308 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002309 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002310 }
2311}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002312
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002313bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2314 if (isTypeDependent())
2315 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002316 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002317}
2318
John McCall864c0412011-04-26 20:42:42 +00002319QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002320 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002321
2322 // Bound member expressions are always one of these possibilities:
2323 // x->m x.m x->*y x.*y
2324 // (possibly parenthesized)
2325
2326 expr = expr->IgnoreParens();
2327 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2328 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2329 return mem->getMemberDecl()->getType();
2330 }
2331
2332 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2333 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2334 ->getPointeeType();
2335 assert(type->isFunctionType());
2336 return type;
2337 }
2338
2339 assert(isa<UnresolvedMemberExpr>(expr));
2340 return QualType();
2341}
2342
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002343Expr* Expr::IgnoreParens() {
2344 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002345 while (true) {
2346 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2347 E = P->getSubExpr();
2348 continue;
2349 }
2350 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2351 if (P->getOpcode() == UO_Extension) {
2352 E = P->getSubExpr();
2353 continue;
2354 }
2355 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002356 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2357 if (!P->isResultDependent()) {
2358 E = P->getResultExpr();
2359 continue;
2360 }
2361 }
Eli Friedmana5e66012013-07-20 00:40:58 +00002362 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2363 if (!P->isConditionDependent()) {
2364 E = P->getChosenSubExpr();
2365 continue;
2366 }
2367 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002368 return E;
2369 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002370}
2371
Chris Lattner56f34942008-02-13 01:02:39 +00002372/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2373/// or CastExprs or ImplicitCastExprs, returning their operand.
2374Expr *Expr::IgnoreParenCasts() {
2375 Expr *E = this;
2376 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002377 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002378 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002379 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002380 continue;
2381 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002382 if (MaterializeTemporaryExpr *Materialize
2383 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2384 E = Materialize->GetTemporaryExpr();
2385 continue;
2386 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002387 if (SubstNonTypeTemplateParmExpr *NTTP
2388 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2389 E = NTTP->getReplacement();
2390 continue;
2391 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002392 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002393 }
2394}
2395
John McCall9c5d70c2010-12-04 08:24:19 +00002396/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2397/// casts. This is intended purely as a temporary workaround for code
2398/// that hasn't yet been rewritten to do the right thing about those
2399/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002400Expr *Expr::IgnoreParenLValueCasts() {
2401 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002402 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002403 E = E->IgnoreParens();
2404 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002405 if (P->getCastKind() == CK_LValueToRValue) {
2406 E = P->getSubExpr();
2407 continue;
2408 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002409 } else if (MaterializeTemporaryExpr *Materialize
2410 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2411 E = Materialize->GetTemporaryExpr();
2412 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002413 } else if (SubstNonTypeTemplateParmExpr *NTTP
2414 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2415 E = NTTP->getReplacement();
2416 continue;
John McCallf6a16482010-12-04 03:47:34 +00002417 }
2418 break;
2419 }
2420 return E;
2421}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002422
2423Expr *Expr::ignoreParenBaseCasts() {
2424 Expr *E = this;
2425 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002426 E = E->IgnoreParens();
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002427 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2428 if (CE->getCastKind() == CK_DerivedToBase ||
2429 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2430 CE->getCastKind() == CK_NoOp) {
2431 E = CE->getSubExpr();
2432 continue;
2433 }
2434 }
2435
2436 return E;
2437 }
2438}
2439
John McCall2fc46bf2010-05-05 22:59:52 +00002440Expr *Expr::IgnoreParenImpCasts() {
2441 Expr *E = this;
2442 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002443 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002444 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002445 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002446 continue;
2447 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002448 if (MaterializeTemporaryExpr *Materialize
2449 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2450 E = Materialize->GetTemporaryExpr();
2451 continue;
2452 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002453 if (SubstNonTypeTemplateParmExpr *NTTP
2454 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2455 E = NTTP->getReplacement();
2456 continue;
2457 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002458 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002459 }
2460}
2461
Hans Wennborg2f072b42011-06-09 17:06:51 +00002462Expr *Expr::IgnoreConversionOperator() {
2463 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002464 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002465 return MCE->getImplicitObjectArgument();
2466 }
2467 return this;
2468}
2469
Chris Lattnerecdd8412009-03-13 17:28:01 +00002470/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2471/// value (including ptr->int casts of the same size). Strip off any
2472/// ParenExpr or CastExprs, returning their operand.
2473Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2474 Expr *E = this;
2475 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002476 E = E->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +00002477
Chris Lattnerecdd8412009-03-13 17:28:01 +00002478 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2479 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002480 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002481 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002482
Chris Lattnerecdd8412009-03-13 17:28:01 +00002483 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2484 E = SE;
2485 continue;
2486 }
Mike Stump1eb44332009-09-09 15:08:12 +00002487
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002488 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002489 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002490 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002491 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002492 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2493 E = SE;
2494 continue;
2495 }
2496 }
Mike Stump1eb44332009-09-09 15:08:12 +00002497
Douglas Gregorc0244c52011-09-08 17:56:33 +00002498 if (SubstNonTypeTemplateParmExpr *NTTP
2499 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2500 E = NTTP->getReplacement();
2501 continue;
2502 }
2503
Chris Lattnerecdd8412009-03-13 17:28:01 +00002504 return E;
2505 }
2506}
2507
Douglas Gregor6eef5192009-12-14 19:27:10 +00002508bool Expr::isDefaultArgument() const {
2509 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002510 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2511 E = M->GetTemporaryExpr();
2512
Douglas Gregor6eef5192009-12-14 19:27:10 +00002513 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2514 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002515
Douglas Gregor6eef5192009-12-14 19:27:10 +00002516 return isa<CXXDefaultArgExpr>(E);
2517}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002518
Douglas Gregor2f599792010-04-02 18:24:57 +00002519/// \brief Skip over any no-op casts and any temporary-binding
2520/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002521static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002522 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2523 E = M->GetTemporaryExpr();
2524
Douglas Gregor2f599792010-04-02 18:24:57 +00002525 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002526 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002527 E = ICE->getSubExpr();
2528 else
2529 break;
2530 }
2531
2532 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2533 E = BE->getSubExpr();
2534
2535 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002536 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002537 E = ICE->getSubExpr();
2538 else
2539 break;
2540 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002541
2542 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002543}
2544
John McCall558d2ab2010-09-15 10:14:12 +00002545/// isTemporaryObject - Determines if this expression produces a
2546/// temporary of the given class type.
2547bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2548 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2549 return false;
2550
Anders Carlssonf8b30152010-11-28 16:40:49 +00002551 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002552
John McCall58277b52010-09-15 20:59:13 +00002553 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002554 if (!E->Classify(C).isPRValue()) {
2555 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002556 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002557 return false;
2558 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002559
John McCall19e60ad2010-09-16 06:57:56 +00002560 // Black-list a few cases which yield pr-values of class type that don't
2561 // refer to temporaries of that type:
2562
2563 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002564 if (isa<ImplicitCastExpr>(E)) {
2565 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2566 case CK_DerivedToBase:
2567 case CK_UncheckedDerivedToBase:
2568 return false;
2569 default:
2570 break;
2571 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002572 }
2573
John McCall19e60ad2010-09-16 06:57:56 +00002574 // - member expressions (all)
2575 if (isa<MemberExpr>(E))
2576 return false;
2577
Eli Friedman32f498a2012-06-15 23:51:06 +00002578 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2579 if (BO->isPtrMemOp())
2580 return false;
2581
John McCall56ca35d2011-02-17 10:25:35 +00002582 // - opaque values (all)
2583 if (isa<OpaqueValueExpr>(E))
2584 return false;
2585
John McCall558d2ab2010-09-15 10:14:12 +00002586 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002587}
2588
Douglas Gregor75e85042011-03-02 21:06:53 +00002589bool Expr::isImplicitCXXThis() const {
2590 const Expr *E = this;
2591
2592 // Strip away parentheses and casts we don't care about.
2593 while (true) {
2594 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2595 E = Paren->getSubExpr();
2596 continue;
2597 }
2598
2599 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2600 if (ICE->getCastKind() == CK_NoOp ||
2601 ICE->getCastKind() == CK_LValueToRValue ||
2602 ICE->getCastKind() == CK_DerivedToBase ||
2603 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2604 E = ICE->getSubExpr();
2605 continue;
2606 }
2607 }
2608
2609 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2610 if (UnOp->getOpcode() == UO_Extension) {
2611 E = UnOp->getSubExpr();
2612 continue;
2613 }
2614 }
2615
Douglas Gregor03e80032011-06-21 17:03:29 +00002616 if (const MaterializeTemporaryExpr *M
2617 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2618 E = M->GetTemporaryExpr();
2619 continue;
2620 }
2621
Douglas Gregor75e85042011-03-02 21:06:53 +00002622 break;
2623 }
2624
2625 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2626 return This->isImplicit();
2627
2628 return false;
2629}
2630
Douglas Gregor898574e2008-12-05 23:32:09 +00002631/// hasAnyTypeDependentArguments - Determines if any of the expressions
2632/// in Exprs is type-dependent.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002633bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00002634 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002635 if (Exprs[I]->isTypeDependent())
2636 return true;
2637
2638 return false;
2639}
2640
John McCall4204f072010-08-02 21:13:48 +00002641bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002642 // This function is attempting whether an expression is an initializer
Eli Friedman21cde052013-07-16 22:40:53 +00002643 // which can be evaluated at compile-time. It very closely parallels
2644 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2645 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2646 // to isEvaluatable most of the time.
2647 //
John McCall4204f072010-08-02 21:13:48 +00002648 // If we ever capture reference-binding directly in the AST, we can
2649 // kill the second parameter.
2650
2651 if (IsForRef) {
2652 EvalResult Result;
2653 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2654 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002655
Anders Carlssone8a32b82008-11-24 05:23:59 +00002656 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002657 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002658 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002659 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002660 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002661 case CXXTemporaryObjectExprClass:
2662 case CXXConstructExprClass: {
2663 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002664
Eli Friedman21cde052013-07-16 22:40:53 +00002665 if (CE->getConstructor()->isTrivial() &&
2666 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2667 // Trivial default constructor
Richard Smith180f4792011-11-10 06:34:14 +00002668 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002669
Eli Friedman21cde052013-07-16 22:40:53 +00002670 // Trivial copy constructor
2671 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2672 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smith180f4792011-11-10 06:34:14 +00002673 }
2674
Richard Smith180f4792011-11-10 06:34:14 +00002675 break;
John McCallb4b9b152010-08-01 21:51:45 +00002676 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002677 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002678 // This handles gcc's extension that allows global initializers like
2679 // "struct x {int x;} x = (struct x) {};".
2680 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002681 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002682 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002683 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002684 case InitListExprClass: {
Eli Friedman21cde052013-07-16 22:40:53 +00002685 const InitListExpr *ILE = cast<InitListExpr>(this);
2686 if (ILE->getType()->isArrayType()) {
2687 unsigned numInits = ILE->getNumInits();
2688 for (unsigned i = 0; i < numInits; i++) {
2689 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2690 return false;
2691 }
2692 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002693 }
Eli Friedman21cde052013-07-16 22:40:53 +00002694
2695 if (ILE->getType()->isRecordType()) {
2696 unsigned ElementNo = 0;
2697 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2698 for (RecordDecl::field_iterator Field = RD->field_begin(),
2699 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2700 // If this is a union, skip all the fields that aren't being initialized.
2701 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2702 continue;
2703
2704 // Don't emit anonymous bitfields, they just affect layout.
2705 if (Field->isUnnamedBitfield())
2706 continue;
2707
2708 if (ElementNo < ILE->getNumInits()) {
2709 const Expr *Elt = ILE->getInit(ElementNo++);
2710 if (Field->isBitField()) {
2711 // Bitfields have to evaluate to an integer.
2712 llvm::APSInt ResultTmp;
2713 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2714 return false;
2715 } else {
2716 bool RefType = Field->getType()->isReferenceType();
2717 if (!Elt->isConstantInitializer(Ctx, RefType))
2718 return false;
2719 }
2720 }
2721 }
2722 return true;
2723 }
2724
2725 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002726 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002727 case ImplicitValueInitExprClass:
2728 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002729 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002730 return cast<ParenExpr>(this)->getSubExpr()
2731 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002732 case GenericSelectionExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002733 return cast<GenericSelectionExpr>(this)->getResultExpr()
2734 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002735 case ChooseExprClass:
Eli Friedmana5e66012013-07-20 00:40:58 +00002736 if (cast<ChooseExpr>(this)->isConditionDependent())
2737 return false;
2738 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002739 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002740 case UnaryOperatorClass: {
2741 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002742 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002743 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002744 break;
2745 }
John McCall4204f072010-08-02 21:13:48 +00002746 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002747 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002748 case ImplicitCastExprClass:
Eli Friedman21cde052013-07-16 22:40:53 +00002749 case CStyleCastExprClass:
2750 case ObjCBridgedCastExprClass:
2751 case CXXDynamicCastExprClass:
2752 case CXXReinterpretCastExprClass:
2753 case CXXConstCastExprClass: {
Richard Smithd62ca372011-12-06 22:44:34 +00002754 const CastExpr *CE = cast<CastExpr>(this);
2755
Eli Friedman6bd97192011-12-21 00:43:02 +00002756 // Handle misc casts we want to ignore.
Eli Friedman6bd97192011-12-21 00:43:02 +00002757 if (CE->getCastKind() == CK_NoOp ||
2758 CE->getCastKind() == CK_LValueToRValue ||
2759 CE->getCastKind() == CK_ToUnion ||
Eli Friedman21cde052013-07-16 22:40:53 +00002760 CE->getCastKind() == CK_ConstructorConversion ||
2761 CE->getCastKind() == CK_NonAtomicToAtomic ||
2762 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smithd62ca372011-12-06 22:44:34 +00002763 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2764
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002765 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002766 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002767 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002768 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002769 ->isConstantInitializer(Ctx, false);
Eli Friedman21cde052013-07-16 22:40:53 +00002770
2771 case SubstNonTypeTemplateParmExprClass:
2772 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2773 ->isConstantInitializer(Ctx, false);
2774 case CXXDefaultArgExprClass:
2775 return cast<CXXDefaultArgExpr>(this)->getExpr()
2776 ->isConstantInitializer(Ctx, false);
2777 case CXXDefaultInitExprClass:
2778 return cast<CXXDefaultInitExpr>(this)->getExpr()
2779 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002780 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002781 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002782}
2783
Richard Smith8ae4ec22012-08-07 04:16:51 +00002784bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2785 if (isInstantiationDependent())
2786 return true;
2787
2788 switch (getStmtClass()) {
2789 case NoStmtClass:
2790 #define ABSTRACT_STMT(Type)
2791 #define STMT(Type, Base) case Type##Class:
2792 #define EXPR(Type, Base)
2793 #include "clang/AST/StmtNodes.inc"
2794 llvm_unreachable("unexpected Expr kind");
2795
2796 case DependentScopeDeclRefExprClass:
2797 case CXXUnresolvedConstructExprClass:
2798 case CXXDependentScopeMemberExprClass:
2799 case UnresolvedLookupExprClass:
2800 case UnresolvedMemberExprClass:
2801 case PackExpansionExprClass:
2802 case SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +00002803 case FunctionParmPackExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002804 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2805
Richard Smith60b70382012-08-07 05:18:29 +00002806 case DeclRefExprClass:
2807 case ObjCIvarRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002808 case PredefinedExprClass:
2809 case IntegerLiteralClass:
2810 case FloatingLiteralClass:
2811 case ImaginaryLiteralClass:
2812 case StringLiteralClass:
2813 case CharacterLiteralClass:
2814 case OffsetOfExprClass:
2815 case ImplicitValueInitExprClass:
2816 case UnaryExprOrTypeTraitExprClass:
2817 case AddrLabelExprClass:
2818 case GNUNullExprClass:
2819 case CXXBoolLiteralExprClass:
2820 case CXXNullPtrLiteralExprClass:
2821 case CXXThisExprClass:
2822 case CXXScalarValueInitExprClass:
2823 case TypeTraitExprClass:
2824 case UnaryTypeTraitExprClass:
2825 case BinaryTypeTraitExprClass:
2826 case ArrayTypeTraitExprClass:
2827 case ExpressionTraitExprClass:
2828 case CXXNoexceptExprClass:
2829 case SizeOfPackExprClass:
2830 case ObjCStringLiteralClass:
2831 case ObjCEncodeExprClass:
2832 case ObjCBoolLiteralExprClass:
2833 case CXXUuidofExprClass:
2834 case OpaqueValueExprClass:
2835 // These never have a side-effect.
2836 return false;
2837
2838 case CallExprClass:
John McCall76da55d2013-04-16 07:28:30 +00002839 case MSPropertyRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002840 case CompoundAssignOperatorClass:
2841 case VAArgExprClass:
2842 case AtomicExprClass:
2843 case StmtExprClass:
2844 case CXXOperatorCallExprClass:
2845 case CXXMemberCallExprClass:
2846 case UserDefinedLiteralClass:
2847 case CXXThrowExprClass:
2848 case CXXNewExprClass:
2849 case CXXDeleteExprClass:
2850 case ExprWithCleanupsClass:
2851 case CXXBindTemporaryExprClass:
2852 case BlockExprClass:
2853 case CUDAKernelCallExprClass:
2854 // These always have a side-effect.
2855 return true;
2856
2857 case ParenExprClass:
2858 case ArraySubscriptExprClass:
2859 case MemberExprClass:
2860 case ConditionalOperatorClass:
2861 case BinaryConditionalOperatorClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002862 case CompoundLiteralExprClass:
2863 case ExtVectorElementExprClass:
2864 case DesignatedInitExprClass:
2865 case ParenListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002866 case CXXPseudoDestructorExprClass:
Richard Smith7c3e6152013-06-12 22:31:48 +00002867 case CXXStdInitializerListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002868 case SubstNonTypeTemplateParmExprClass:
2869 case MaterializeTemporaryExprClass:
2870 case ShuffleVectorExprClass:
Hal Finkel414a1bd2013-09-18 03:29:45 +00002871 case ConvertVectorExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002872 case AsTypeExprClass:
2873 // These have a side-effect if any subexpression does.
2874 break;
2875
Richard Smith60b70382012-08-07 05:18:29 +00002876 case UnaryOperatorClass:
2877 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002878 return true;
2879 break;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002880
2881 case BinaryOperatorClass:
2882 if (cast<BinaryOperator>(this)->isAssignmentOp())
2883 return true;
2884 break;
2885
Richard Smith8ae4ec22012-08-07 04:16:51 +00002886 case InitListExprClass:
2887 // FIXME: The children for an InitListExpr doesn't include the array filler.
2888 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2889 if (E->HasSideEffects(Ctx))
2890 return true;
2891 break;
2892
2893 case GenericSelectionExprClass:
2894 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2895 HasSideEffects(Ctx);
2896
2897 case ChooseExprClass:
Eli Friedmana5e66012013-07-20 00:40:58 +00002898 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith8ae4ec22012-08-07 04:16:51 +00002899
2900 case CXXDefaultArgExprClass:
2901 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2902
Richard Smithc3bf52c2013-04-20 22:23:05 +00002903 case CXXDefaultInitExprClass:
2904 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2905 return E->HasSideEffects(Ctx);
2906 // If we've not yet parsed the initializer, assume it has side-effects.
2907 return true;
2908
Richard Smith8ae4ec22012-08-07 04:16:51 +00002909 case CXXDynamicCastExprClass: {
2910 // A dynamic_cast expression has side-effects if it can throw.
2911 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2912 if (DCE->getTypeAsWritten()->isReferenceType() &&
2913 DCE->getCastKind() == CK_Dynamic)
2914 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002915 } // Fall through.
2916 case ImplicitCastExprClass:
2917 case CStyleCastExprClass:
2918 case CXXStaticCastExprClass:
2919 case CXXReinterpretCastExprClass:
2920 case CXXConstCastExprClass:
2921 case CXXFunctionalCastExprClass: {
2922 const CastExpr *CE = cast<CastExpr>(this);
2923 if (CE->getCastKind() == CK_LValueToRValue &&
2924 CE->getSubExpr()->getType().isVolatileQualified())
2925 return true;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002926 break;
2927 }
2928
Richard Smith0d729102012-08-13 20:08:14 +00002929 case CXXTypeidExprClass:
2930 // typeid might throw if its subexpression is potentially-evaluated, so has
2931 // side-effects in that case whether or not its subexpression does.
2932 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith8ae4ec22012-08-07 04:16:51 +00002933
2934 case CXXConstructExprClass:
2935 case CXXTemporaryObjectExprClass: {
2936 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smith60b70382012-08-07 05:18:29 +00002937 if (!CE->getConstructor()->isTrivial())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002938 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002939 // A trivial constructor does not add any side-effects of its own. Just look
2940 // at its arguments.
Richard Smith8ae4ec22012-08-07 04:16:51 +00002941 break;
2942 }
2943
2944 case LambdaExprClass: {
2945 const LambdaExpr *LE = cast<LambdaExpr>(this);
2946 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2947 E = LE->capture_end(); I != E; ++I)
2948 if (I->getCaptureKind() == LCK_ByCopy)
2949 // FIXME: Only has a side-effect if the variable is volatile or if
2950 // the copy would invoke a non-trivial copy constructor.
2951 return true;
2952 return false;
2953 }
2954
2955 case PseudoObjectExprClass: {
2956 // Only look for side-effects in the semantic form, and look past
2957 // OpaqueValueExpr bindings in that form.
2958 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2959 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2960 E = PO->semantics_end();
2961 I != E; ++I) {
2962 const Expr *Subexpr = *I;
2963 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2964 Subexpr = OVE->getSourceExpr();
2965 if (Subexpr->HasSideEffects(Ctx))
2966 return true;
2967 }
2968 return false;
2969 }
2970
2971 case ObjCBoxedExprClass:
2972 case ObjCArrayLiteralClass:
2973 case ObjCDictionaryLiteralClass:
2974 case ObjCMessageExprClass:
2975 case ObjCSelectorExprClass:
2976 case ObjCProtocolExprClass:
2977 case ObjCPropertyRefExprClass:
2978 case ObjCIsaExprClass:
2979 case ObjCIndirectCopyRestoreExprClass:
2980 case ObjCSubscriptRefExprClass:
2981 case ObjCBridgedCastExprClass:
2982 // FIXME: Classify these cases better.
2983 return true;
2984 }
2985
2986 // Recurse to children.
2987 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2988 if (const Stmt *S = *SubStmts)
2989 if (cast<Expr>(S)->HasSideEffects(Ctx))
2990 return true;
2991
2992 return false;
2993}
2994
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002995namespace {
2996 /// \brief Look for a call to a non-trivial function within an expression.
2997 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2998 {
2999 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3000
3001 bool NonTrivial;
3002
3003 public:
3004 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00003005 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00003006
3007 bool hasNonTrivialCall() const { return NonTrivial; }
3008
3009 void VisitCallExpr(CallExpr *E) {
3010 if (CXXMethodDecl *Method
3011 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3012 if (Method->isTrivial()) {
3013 // Recurse to children of the call.
3014 Inherited::VisitStmt(E);
3015 return;
3016 }
3017 }
3018
3019 NonTrivial = true;
3020 }
3021
3022 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3023 if (E->getConstructor()->isTrivial()) {
3024 // Recurse to children of the call.
3025 Inherited::VisitStmt(E);
3026 return;
3027 }
3028
3029 NonTrivial = true;
3030 }
3031
3032 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3033 if (E->getTemporary()->getDestructor()->isTrivial()) {
3034 Inherited::VisitStmt(E);
3035 return;
3036 }
3037
3038 NonTrivial = true;
3039 }
3040 };
3041}
3042
3043bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3044 NonTrivialCallFinder Finder(Ctx);
3045 Finder.Visit(this);
3046 return Finder.hasNonTrivialCall();
3047}
3048
Chandler Carruth82214a82011-02-18 23:54:50 +00003049/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3050/// pointer constant or not, as well as the specific kind of constant detected.
3051/// Null pointer constants can be integer constant expressions with the
3052/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3053/// (a GNU extension).
3054Expr::NullPointerConstantKind
3055Expr::isNullPointerConstant(ASTContext &Ctx,
3056 NullPointerConstantValueDependence NPC) const {
Reid Klecknere0ac9bf2013-11-12 02:22:34 +00003057 if (isValueDependent() &&
3058 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MicrosoftMode)) {
Douglas Gregorce940492009-09-25 04:25:58 +00003059 switch (NPC) {
3060 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00003061 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00003062 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003063 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie50800fc2012-08-08 17:33:31 +00003064 return NPCK_ZeroExpression;
Chandler Carruth82214a82011-02-18 23:54:50 +00003065 else
3066 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00003067
Douglas Gregorce940492009-09-25 04:25:58 +00003068 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003069 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00003070 }
3071 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00003072
Sebastian Redl07779722008-10-31 14:43:28 +00003073 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00003074 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003075 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00003076 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00003077 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00003078 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00003079 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00003080 Pointee->isVoidType() && // to void*
3081 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00003082 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00003083 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003084 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003085 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3086 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00003087 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00003088 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3089 // Accept ((void*)0) as a null pointer constant, as many other
3090 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00003091 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00003092 } else if (const GenericSelectionExpr *GE =
3093 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedmana5e66012013-07-20 00:40:58 +00003094 if (GE->isResultDependent())
3095 return NPCK_NotNull;
Peter Collingbournef111d932011-04-15 00:35:48 +00003096 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedmana5e66012013-07-20 00:40:58 +00003097 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3098 if (CE->isConditionDependent())
3099 return NPCK_NotNull;
3100 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00003101 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00003102 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smithc3bf52c2013-04-20 22:23:05 +00003103 // See through default argument expressions.
Douglas Gregorce940492009-09-25 04:25:58 +00003104 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smithc3bf52c2013-04-20 22:23:05 +00003105 } else if (const CXXDefaultInitExpr *DefaultInit
3106 = dyn_cast<CXXDefaultInitExpr>(this)) {
3107 // See through default initializer expressions.
3108 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003109 } else if (isa<GNUNullExpr>(this)) {
3110 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00003111 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00003112 } else if (const MaterializeTemporaryExpr *M
3113 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3114 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00003115 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3116 if (const Expr *Source = OVE->getSourceExpr())
3117 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00003118 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003119
Richard Smith4e24f0f2013-01-02 12:01:23 +00003120 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003121 if (getType()->isNullPtrType())
Richard Smith4e24f0f2013-01-02 12:01:23 +00003122 return NPCK_CXX11_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003123
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003124 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smithf050d242013-06-13 02:46:14 +00003125 if (!Ctx.getLangOpts().CPlusPlus11 &&
3126 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003127 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3128 const Expr *InitExpr = CLE->getInitializer();
3129 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3130 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3131 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003132 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00003133 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003134 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00003135 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00003136
Richard Smith80ad52f2013-01-02 11:42:31 +00003137 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smithf050d242013-06-13 02:46:14 +00003138 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3139 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknere0ac9bf2013-11-12 02:22:34 +00003140 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smithf050d242013-06-13 02:46:14 +00003141 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknere0ac9bf2013-11-12 02:22:34 +00003142 if (Lit && !Lit->getValue())
3143 return NPCK_ZeroLiteral;
3144 else if (!Ctx.getLangOpts().MicrosoftMode ||
3145 !isCXX98IntegralConstantExpr(Ctx))
3146 return NPCK_NotNull;
Richard Smith70488e22012-02-14 21:38:30 +00003147 } else {
Richard Smithf050d242013-06-13 02:46:14 +00003148 // If we have an integer constant expression, we need to *evaluate* it and
3149 // test for the value 0.
Richard Smith70488e22012-02-14 21:38:30 +00003150 if (!isIntegerConstantExpr(Ctx))
3151 return NPCK_NotNull;
3152 }
Chandler Carruth82214a82011-02-18 23:54:50 +00003153
David Blaikie50800fc2012-08-08 17:33:31 +00003154 if (EvaluateKnownConstInt(Ctx) != 0)
3155 return NPCK_NotNull;
3156
3157 if (isa<IntegerLiteral>(this))
3158 return NPCK_ZeroLiteral;
3159 return NPCK_ZeroExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00003160}
Steve Naroff31a45842007-07-28 23:10:27 +00003161
John McCallf6a16482010-12-04 03:47:34 +00003162/// \brief If this expression is an l-value for an Objective C
3163/// property, find the underlying property reference expression.
3164const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3165 const Expr *E = this;
3166 while (true) {
3167 assert((E->getValueKind() == VK_LValue &&
3168 E->getObjectKind() == OK_ObjCProperty) &&
3169 "expression is not a property reference");
3170 E = E->IgnoreParenCasts();
3171 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3172 if (BO->getOpcode() == BO_Comma) {
3173 E = BO->getRHS();
3174 continue;
3175 }
3176 }
3177
3178 break;
3179 }
3180
3181 return cast<ObjCPropertyRefExpr>(E);
3182}
3183
Anna Zaksbbff82f2012-10-01 20:34:04 +00003184bool Expr::isObjCSelfExpr() const {
3185 const Expr *E = IgnoreParenImpCasts();
3186
3187 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3188 if (!DRE)
3189 return false;
3190
3191 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3192 if (!Param)
3193 return false;
3194
3195 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3196 if (!M)
3197 return false;
3198
3199 return M->getSelfDecl() == Param;
3200}
3201
John McCall993f43f2013-05-06 21:39:12 +00003202FieldDecl *Expr::getSourceBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00003203 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003204
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003205 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00003206 if (ICE->getCastKind() == CK_LValueToRValue ||
3207 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003208 E = ICE->getSubExpr()->IgnoreParens();
3209 else
3210 break;
3211 }
3212
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003213 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00003214 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003215 if (Field->isBitField())
3216 return Field;
3217
John McCall993f43f2013-05-06 21:39:12 +00003218 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3219 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3220 if (Ivar->isBitField())
3221 return Ivar;
3222
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00003223 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3224 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3225 if (Field->isBitField())
3226 return Field;
3227
Eli Friedman42068e92011-07-13 02:05:57 +00003228 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003229 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCall993f43f2013-05-06 21:39:12 +00003230 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003231
Eli Friedman42068e92011-07-13 02:05:57 +00003232 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCall993f43f2013-05-06 21:39:12 +00003233 return BinOp->getRHS()->getSourceBitField();
Eli Friedman42068e92011-07-13 02:05:57 +00003234 }
3235
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003236 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003237}
3238
Anders Carlsson09380262010-01-31 17:18:49 +00003239bool Expr::refersToVectorElement() const {
3240 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00003241
Anders Carlsson09380262010-01-31 17:18:49 +00003242 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00003243 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00003244 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00003245 E = ICE->getSubExpr()->IgnoreParens();
3246 else
3247 break;
3248 }
Sean Huntc3021132010-05-05 15:23:54 +00003249
Anders Carlsson09380262010-01-31 17:18:49 +00003250 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3251 return ASE->getBase()->getType()->isVectorType();
3252
3253 if (isa<ExtVectorElementExpr>(E))
3254 return true;
3255
3256 return false;
3257}
3258
Chris Lattner2140e902009-02-16 22:14:05 +00003259/// isArrow - Return true if the base expression is a pointer to vector,
3260/// return false if the base expression is a vector.
3261bool ExtVectorElementExpr::isArrow() const {
3262 return getBase()->getType()->isPointerType();
3263}
3264
Nate Begeman213541a2008-04-18 23:10:10 +00003265unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003266 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003267 return VT->getNumElements();
3268 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003269}
3270
Nate Begeman8a997642008-05-09 06:41:27 +00003271/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003272bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003273 // FIXME: Refactor this code to an accessor on the AST node which returns the
3274 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003275 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003276
3277 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003278 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003279 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003280
Nate Begeman190d6a22009-01-18 02:01:21 +00003281 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003282 if (Comp[0] == 's' || Comp[0] == 'S')
3283 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003284
Daniel Dunbar15027422009-10-17 23:53:04 +00003285 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003286 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003287 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003288
Steve Narofffec0b492007-07-30 03:29:09 +00003289 return false;
3290}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003291
Nate Begeman8a997642008-05-09 06:41:27 +00003292/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003293void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003294 SmallVectorImpl<unsigned> &Elts) const {
3295 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003296 if (Comp[0] == 's' || Comp[0] == 'S')
3297 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003299 bool isHi = Comp == "hi";
3300 bool isLo = Comp == "lo";
3301 bool isEven = Comp == "even";
3302 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Nate Begeman8a997642008-05-09 06:41:27 +00003304 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3305 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003306
Nate Begeman8a997642008-05-09 06:41:27 +00003307 if (isHi)
3308 Index = e + i;
3309 else if (isLo)
3310 Index = i;
3311 else if (isEven)
3312 Index = 2 * i;
3313 else if (isOdd)
3314 Index = 2 * i + 1;
3315 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003316 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003317
Nate Begeman3b8d1162008-05-13 21:03:02 +00003318 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003319 }
Nate Begeman8a997642008-05-09 06:41:27 +00003320}
3321
Douglas Gregor04badcf2010-04-21 00:45:42 +00003322ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003323 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003324 SourceLocation LBracLoc,
3325 SourceLocation SuperLoc,
3326 bool IsInstanceSuper,
3327 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003328 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003329 ArrayRef<SourceLocation> SelLocs,
3330 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003331 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003332 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003333 SourceLocation RBracLoc,
3334 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003335 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003336 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003337 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003338 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003339 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3340 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003341 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003342 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3343 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003344{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003345 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003346 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003347}
3348
Douglas Gregor04badcf2010-04-21 00:45:42 +00003349ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003350 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003351 SourceLocation LBracLoc,
3352 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003353 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003354 ArrayRef<SourceLocation> SelLocs,
3355 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003356 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003357 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003358 SourceLocation RBracLoc,
3359 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003360 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003361 T->isDependentType(), T->isInstantiationDependentType(),
3362 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003363 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3364 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003365 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003366 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003367 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003368{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003369 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003370 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003371}
3372
Douglas Gregor04badcf2010-04-21 00:45:42 +00003373ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003374 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003375 SourceLocation LBracLoc,
3376 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003377 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003378 ArrayRef<SourceLocation> SelLocs,
3379 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003380 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003381 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003382 SourceLocation RBracLoc,
3383 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003384 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003385 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003386 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003387 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003388 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3389 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003390 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003391 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003392 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003393{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003394 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003395 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003396}
3397
3398void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3399 ArrayRef<SourceLocation> SelLocs,
3400 SelectorLocationsKind SelLocsK) {
3401 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003402 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003403 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003404 if (Args[I]->isTypeDependent())
3405 ExprBits.TypeDependent = true;
3406 if (Args[I]->isValueDependent())
3407 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003408 if (Args[I]->isInstantiationDependent())
3409 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003410 if (Args[I]->containsUnexpandedParameterPack())
3411 ExprBits.ContainsUnexpandedParameterPack = true;
3412
3413 MyArgs[I] = Args[I];
3414 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003415
Benjamin Kramer19562c92012-02-20 00:20:48 +00003416 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003417 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003418 if (SelLocsK == SelLoc_NonStandard)
3419 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3420 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003421}
3422
Craig Topper9db7a7e2013-08-22 04:58:56 +00003423ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003424 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003425 SourceLocation LBracLoc,
3426 SourceLocation SuperLoc,
3427 bool IsInstanceSuper,
3428 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003429 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003430 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003431 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003432 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003433 SourceLocation RBracLoc,
3434 bool isImplicit) {
3435 assert((!SelLocs.empty() || isImplicit) &&
3436 "No selector locs for non-implicit message");
3437 ObjCMessageExpr *Mem;
3438 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3439 if (isImplicit)
3440 Mem = alloc(Context, Args.size(), 0);
3441 else
3442 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003443 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003444 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003445 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003446}
3447
Craig Topper9db7a7e2013-08-22 04:58:56 +00003448ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003449 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003450 SourceLocation LBracLoc,
3451 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003452 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003453 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003454 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003455 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003456 SourceLocation RBracLoc,
3457 bool isImplicit) {
3458 assert((!SelLocs.empty() || isImplicit) &&
3459 "No selector locs for non-implicit message");
3460 ObjCMessageExpr *Mem;
3461 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3462 if (isImplicit)
3463 Mem = alloc(Context, Args.size(), 0);
3464 else
3465 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003466 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003467 SelLocs, SelLocsK, Method, Args, RBracLoc,
3468 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003469}
3470
Craig Topper9db7a7e2013-08-22 04:58:56 +00003471ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003472 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003473 SourceLocation LBracLoc,
3474 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003475 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003476 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003477 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003478 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003479 SourceLocation RBracLoc,
3480 bool isImplicit) {
3481 assert((!SelLocs.empty() || isImplicit) &&
3482 "No selector locs for non-implicit message");
3483 ObjCMessageExpr *Mem;
3484 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3485 if (isImplicit)
3486 Mem = alloc(Context, Args.size(), 0);
3487 else
3488 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003489 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003490 SelLocs, SelLocsK, Method, Args, RBracLoc,
3491 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003492}
3493
Craig Topper9db7a7e2013-08-22 04:58:56 +00003494ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003495 unsigned NumArgs,
3496 unsigned NumStoredSelLocs) {
3497 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003498 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3499}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003500
Craig Topper9db7a7e2013-08-22 04:58:56 +00003501ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003502 ArrayRef<Expr *> Args,
3503 SourceLocation RBraceLoc,
3504 ArrayRef<SourceLocation> SelLocs,
3505 Selector Sel,
3506 SelectorLocationsKind &SelLocsK) {
3507 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3508 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3509 : 0;
3510 return alloc(C, Args.size(), NumStoredSelLocs);
3511}
3512
Craig Topper9db7a7e2013-08-22 04:58:56 +00003513ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003514 unsigned NumArgs,
3515 unsigned NumStoredSelLocs) {
3516 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3517 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3518 return (ObjCMessageExpr *)C.Allocate(Size,
3519 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3520}
3521
3522void ObjCMessageExpr::getSelectorLocs(
3523 SmallVectorImpl<SourceLocation> &SelLocs) const {
3524 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3525 SelLocs.push_back(getSelectorLoc(i));
3526}
3527
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003528SourceRange ObjCMessageExpr::getReceiverRange() const {
3529 switch (getReceiverKind()) {
3530 case Instance:
3531 return getInstanceReceiver()->getSourceRange();
3532
3533 case Class:
3534 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3535
3536 case SuperInstance:
3537 case SuperClass:
3538 return getSuperLoc();
3539 }
3540
David Blaikie30263482012-01-20 21:50:17 +00003541 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003542}
3543
Douglas Gregor04badcf2010-04-21 00:45:42 +00003544Selector ObjCMessageExpr::getSelector() const {
3545 if (HasMethod)
3546 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3547 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003548 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003549}
3550
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003551QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor04badcf2010-04-21 00:45:42 +00003552 switch (getReceiverKind()) {
3553 case Instance:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003554 return getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003555 case Class:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003556 return getClassReceiver();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003557 case SuperInstance:
Douglas Gregor04badcf2010-04-21 00:45:42 +00003558 case SuperClass:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003559 return getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003560 }
3561
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003562 llvm_unreachable("unexpected receiver kind");
3563}
3564
3565ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3566 QualType T = getReceiverType();
3567
3568 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3569 return Ptr->getInterfaceDecl();
3570
3571 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3572 return Ty->getInterface();
3573
Douglas Gregor04badcf2010-04-21 00:45:42 +00003574 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003575}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003576
Chris Lattner5f9e2722011-07-23 10:55:15 +00003577StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003578 switch (getBridgeKind()) {
3579 case OBC_Bridge:
3580 return "__bridge";
3581 case OBC_BridgeTransfer:
3582 return "__bridge_transfer";
3583 case OBC_BridgeRetained:
3584 return "__bridge_retained";
3585 }
David Blaikie30263482012-01-20 21:50:17 +00003586
3587 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003588}
3589
Craig Topper05ed1a02013-08-18 10:09:15 +00003590ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003591 QualType Type, SourceLocation BLoc,
3592 SourceLocation RP)
3593 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3594 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003595 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003596 Type->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003597 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003598{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003599 SubExprs = new (C) Stmt*[args.size()];
3600 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003601 if (args[i]->isTypeDependent())
3602 ExprBits.TypeDependent = true;
3603 if (args[i]->isValueDependent())
3604 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003605 if (args[i]->isInstantiationDependent())
3606 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003607 if (args[i]->containsUnexpandedParameterPack())
3608 ExprBits.ContainsUnexpandedParameterPack = true;
3609
3610 SubExprs[i] = args[i];
3611 }
3612}
3613
Craig Topper05ed1a02013-08-18 10:09:15 +00003614void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman888376a2009-08-12 02:28:50 +00003615 if (SubExprs) C.Deallocate(SubExprs);
3616
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003617 this->NumExprs = Exprs.size();
Dmitri Gribenko2ad77cd2013-05-10 17:30:13 +00003618 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003619 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00003620}
Nate Begeman888376a2009-08-12 02:28:50 +00003621
Craig Topper05ed1a02013-08-18 10:09:15 +00003622GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003623 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003624 ArrayRef<TypeSourceInfo*> AssocTypes,
3625 ArrayRef<Expr*> AssocExprs,
3626 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003627 SourceLocation RParenLoc,
3628 bool ContainsUnexpandedParameterPack,
3629 unsigned ResultIndex)
3630 : Expr(GenericSelectionExprClass,
3631 AssocExprs[ResultIndex]->getType(),
3632 AssocExprs[ResultIndex]->getValueKind(),
3633 AssocExprs[ResultIndex]->getObjectKind(),
3634 AssocExprs[ResultIndex]->isTypeDependent(),
3635 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003636 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003637 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003638 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3639 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3640 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3641 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003642 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003643 assert(AssocTypes.size() == AssocExprs.size());
3644 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3645 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003646}
3647
Craig Topper05ed1a02013-08-18 10:09:15 +00003648GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003649 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003650 ArrayRef<TypeSourceInfo*> AssocTypes,
3651 ArrayRef<Expr*> AssocExprs,
3652 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003653 SourceLocation RParenLoc,
3654 bool ContainsUnexpandedParameterPack)
3655 : Expr(GenericSelectionExprClass,
3656 Context.DependentTy,
3657 VK_RValue,
3658 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003659 /*isTypeDependent=*/true,
3660 /*isValueDependent=*/true,
3661 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003662 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003663 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3664 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3665 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3666 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003667 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003668 assert(AssocTypes.size() == AssocExprs.size());
3669 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3670 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003671}
3672
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003673//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003674// DesignatedInitExpr
3675//===----------------------------------------------------------------------===//
3676
Chandler Carruthb1138242011-06-16 06:47:06 +00003677IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003678 assert(Kind == FieldDesignator && "Only valid on a field designator");
3679 if (Field.NameOrField & 0x01)
3680 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3681 else
3682 return getField()->getIdentifier();
3683}
3684
Craig Topper05ed1a02013-08-18 10:09:15 +00003685DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003686 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003687 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003688 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003689 bool GNUSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003690 ArrayRef<Expr*> IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003691 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003692 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003693 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003694 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003695 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003696 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003697 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003698 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003699 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003700
3701 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003702 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003703 *Child++ = Init;
3704
3705 // Copy the designators and their subexpressions, computing
3706 // value-dependence along the way.
3707 unsigned IndexIdx = 0;
3708 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003709 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003710
3711 if (this->Designators[I].isArrayDesignator()) {
3712 // Compute type- and value-dependence.
3713 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003714 if (Index->isTypeDependent() || Index->isValueDependent())
3715 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003716 if (Index->isInstantiationDependent())
3717 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003718 // Propagate unexpanded parameter packs.
3719 if (Index->containsUnexpandedParameterPack())
3720 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003721
3722 // Copy the index expressions into permanent storage.
3723 *Child++ = IndexExprs[IndexIdx++];
3724 } else if (this->Designators[I].isArrayRangeDesignator()) {
3725 // Compute type- and value-dependence.
3726 Expr *Start = IndexExprs[IndexIdx];
3727 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003728 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003729 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003730 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003731 ExprBits.InstantiationDependent = true;
3732 } else if (Start->isInstantiationDependent() ||
3733 End->isInstantiationDependent()) {
3734 ExprBits.InstantiationDependent = true;
3735 }
3736
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003737 // Propagate unexpanded parameter packs.
3738 if (Start->containsUnexpandedParameterPack() ||
3739 End->containsUnexpandedParameterPack())
3740 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003741
3742 // Copy the start/end expressions into permanent storage.
3743 *Child++ = IndexExprs[IndexIdx++];
3744 *Child++ = IndexExprs[IndexIdx++];
3745 }
3746 }
3747
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003748 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003749}
3750
Douglas Gregor05c13a32009-01-22 00:58:24 +00003751DesignatedInitExpr *
Craig Topper05ed1a02013-08-18 10:09:15 +00003752DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003753 unsigned NumDesignators,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003754 ArrayRef<Expr*> IndexExprs,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003755 SourceLocation ColonOrEqualLoc,
3756 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003757 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003758 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003759 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003760 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003761 IndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003762}
3763
Craig Topper05ed1a02013-08-18 10:09:15 +00003764DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003765 unsigned NumIndexExprs) {
3766 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3767 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3768 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3769}
3770
Craig Topper05ed1a02013-08-18 10:09:15 +00003771void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003772 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003773 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003774 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003775 NumDesignators = NumDesigs;
3776 for (unsigned I = 0; I != NumDesigs; ++I)
3777 Designators[I] = Desigs[I];
3778}
3779
Abramo Bagnara24f46742011-03-16 15:08:46 +00003780SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3781 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3782 if (size() == 1)
3783 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen65d78312012-12-25 14:51:39 +00003784 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3785 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara24f46742011-03-16 15:08:46 +00003786}
3787
Erik Verbruggen65d78312012-12-25 14:51:39 +00003788SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003789 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003790 Designator &First =
3791 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003792 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003793 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003794 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3795 else
3796 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3797 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003798 StartLoc =
3799 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen65d78312012-12-25 14:51:39 +00003800 return StartLoc;
3801}
3802
3803SourceLocation DesignatedInitExpr::getLocEnd() const {
3804 return getInit()->getLocEnd();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003805}
3806
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003807Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003808 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003809 char *Ptr = static_cast<char *>(
3810 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003811 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003812 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3813 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3814}
3815
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003816Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003817 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003818 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003819 char *Ptr = static_cast<char *>(
3820 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003821 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003822 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3823 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3824}
3825
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003826Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003827 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003828 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003829 char *Ptr = static_cast<char *>(
3830 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003831 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003832 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3833 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3834}
3835
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003836/// \brief Replaces the designator at index @p Idx with the series
3837/// of designators in [First, Last).
Craig Topper05ed1a02013-08-18 10:09:15 +00003838void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003839 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003840 const Designator *Last) {
3841 unsigned NumNewDesignators = Last - First;
3842 if (NumNewDesignators == 0) {
3843 std::copy_backward(Designators + Idx + 1,
3844 Designators + NumDesignators,
3845 Designators + Idx);
3846 --NumNewDesignators;
3847 return;
3848 } else if (NumNewDesignators == 1) {
3849 Designators[Idx] = *First;
3850 return;
3851 }
3852
Mike Stump1eb44332009-09-09 15:08:12 +00003853 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003854 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003855 std::copy(Designators, Designators + Idx, NewDesignators);
3856 std::copy(First, Last, NewDesignators + Idx);
3857 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3858 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003859 Designators = NewDesignators;
3860 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3861}
3862
Craig Topper05ed1a02013-08-18 10:09:15 +00003863ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003864 ArrayRef<Expr*> exprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003865 SourceLocation rparenloc)
3866 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003867 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003868 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3869 Exprs = new (C) Stmt*[exprs.size()];
3870 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003871 if (exprs[i]->isTypeDependent())
3872 ExprBits.TypeDependent = true;
3873 if (exprs[i]->isValueDependent())
3874 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003875 if (exprs[i]->isInstantiationDependent())
3876 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003877 if (exprs[i]->containsUnexpandedParameterPack())
3878 ExprBits.ContainsUnexpandedParameterPack = true;
3879
Nate Begeman2ef13e52009-08-10 23:49:36 +00003880 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003881 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003882}
3883
John McCalle996ffd2011-02-16 08:02:54 +00003884const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3885 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3886 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003887 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3888 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003889 e = cast<CXXConstructExpr>(e)->getArg(0);
3890 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3891 e = ice->getSubExpr();
3892 return cast<OpaqueValueExpr>(e);
3893}
3894
Craig Topper05ed1a02013-08-18 10:09:15 +00003895PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3896 EmptyShell sh,
John McCall4b9c2d22011-11-06 09:01:30 +00003897 unsigned numSemanticExprs) {
3898 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3899 (1 + numSemanticExprs) * sizeof(Expr*),
3900 llvm::alignOf<PseudoObjectExpr>());
3901 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3902}
3903
3904PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3905 : Expr(PseudoObjectExprClass, shell) {
3906 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3907}
3908
Craig Topper05ed1a02013-08-18 10:09:15 +00003909PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCall4b9c2d22011-11-06 09:01:30 +00003910 ArrayRef<Expr*> semantics,
3911 unsigned resultIndex) {
3912 assert(syntax && "no syntactic expression!");
3913 assert(semantics.size() && "no semantic expressions!");
3914
3915 QualType type;
3916 ExprValueKind VK;
3917 if (resultIndex == NoResult) {
3918 type = C.VoidTy;
3919 VK = VK_RValue;
3920 } else {
3921 assert(resultIndex < semantics.size());
3922 type = semantics[resultIndex]->getType();
3923 VK = semantics[resultIndex]->getValueKind();
3924 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3925 }
3926
3927 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3928 (1 + semantics.size()) * sizeof(Expr*),
3929 llvm::alignOf<PseudoObjectExpr>());
3930 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3931 resultIndex);
3932}
3933
3934PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3935 Expr *syntax, ArrayRef<Expr*> semantics,
3936 unsigned resultIndex)
3937 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3938 /*filled in at end of ctor*/ false, false, false, false) {
3939 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3940 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3941
3942 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3943 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3944 getSubExprsBuffer()[i] = E;
3945
3946 if (E->isTypeDependent())
3947 ExprBits.TypeDependent = true;
3948 if (E->isValueDependent())
3949 ExprBits.ValueDependent = true;
3950 if (E->isInstantiationDependent())
3951 ExprBits.InstantiationDependent = true;
3952 if (E->containsUnexpandedParameterPack())
3953 ExprBits.ContainsUnexpandedParameterPack = true;
3954
3955 if (isa<OpaqueValueExpr>(E))
3956 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3957 "opaque-value semantic expressions for pseudo-object "
3958 "operations must have sources");
3959 }
3960}
3961
Douglas Gregor05c13a32009-01-22 00:58:24 +00003962//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003963// ExprIterator.
3964//===----------------------------------------------------------------------===//
3965
3966Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3967Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3968Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3969const Expr* ConstExprIterator::operator[](size_t idx) const {
3970 return cast<Expr>(I[idx]);
3971}
3972const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3973const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3974
3975//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003976// Child Iterators for iterating over subexpressions/substatements
3977//===----------------------------------------------------------------------===//
3978
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003979// UnaryExprOrTypeTraitExpr
3980Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003981 // If this is of a type and the type is a VLA type (and not a typedef), the
3982 // size expression of the VLA needs to be treated as an executable expression.
3983 // Why isn't this weirdness documented better in StmtIterator?
3984 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003985 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003986 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003987 return child_range(child_iterator(T), child_iterator());
3988 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003989 }
John McCall63c00d72011-02-09 08:16:59 +00003990 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003991}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003992
Steve Naroff563477d2007-09-18 23:55:05 +00003993// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003994Stmt::child_range ObjCMessageExpr::children() {
3995 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003996 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003997 begin = reinterpret_cast<Stmt **>(this + 1);
3998 else
3999 begin = reinterpret_cast<Stmt **>(getArgs());
4000 return child_range(begin,
4001 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00004002}
4003
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004004ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004005 QualType T, ObjCMethodDecl *Method,
4006 SourceRange SR)
4007 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
4008 false, false, false, false),
4009 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
4010{
4011 Expr **SaveElements = getElements();
4012 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4013 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4014 ExprBits.ValueDependent = true;
4015 if (Elements[I]->isInstantiationDependent())
4016 ExprBits.InstantiationDependent = true;
4017 if (Elements[I]->containsUnexpandedParameterPack())
4018 ExprBits.ContainsUnexpandedParameterPack = true;
4019
4020 SaveElements[I] = Elements[I];
4021 }
4022}
4023
Craig Topper9db7a7e2013-08-22 04:58:56 +00004024ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004025 ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004026 QualType T, ObjCMethodDecl * Method,
4027 SourceRange SR) {
4028 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4029 + Elements.size() * sizeof(Expr *));
4030 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4031}
4032
Craig Topper9db7a7e2013-08-22 04:58:56 +00004033ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004034 unsigned NumElements) {
4035
4036 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4037 + NumElements * sizeof(Expr *));
4038 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4039}
4040
4041ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4042 ArrayRef<ObjCDictionaryElement> VK,
4043 bool HasPackExpansions,
4044 QualType T, ObjCMethodDecl *method,
4045 SourceRange SR)
4046 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4047 false, false),
4048 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4049 DictWithObjectsMethod(method)
4050{
4051 KeyValuePair *KeyValues = getKeyValues();
4052 ExpansionData *Expansions = getExpansionData();
4053 for (unsigned I = 0; I < NumElements; I++) {
4054 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4055 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4056 ExprBits.ValueDependent = true;
4057 if (VK[I].Key->isInstantiationDependent() ||
4058 VK[I].Value->isInstantiationDependent())
4059 ExprBits.InstantiationDependent = true;
4060 if (VK[I].EllipsisLoc.isInvalid() &&
4061 (VK[I].Key->containsUnexpandedParameterPack() ||
4062 VK[I].Value->containsUnexpandedParameterPack()))
4063 ExprBits.ContainsUnexpandedParameterPack = true;
4064
4065 KeyValues[I].Key = VK[I].Key;
4066 KeyValues[I].Value = VK[I].Value;
4067 if (Expansions) {
4068 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4069 if (VK[I].NumExpansions)
4070 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4071 else
4072 Expansions[I].NumExpansionsPlusOne = 0;
4073 }
4074 }
4075}
4076
4077ObjCDictionaryLiteral *
Craig Topper9db7a7e2013-08-22 04:58:56 +00004078ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004079 ArrayRef<ObjCDictionaryElement> VK,
4080 bool HasPackExpansions,
4081 QualType T, ObjCMethodDecl *method,
4082 SourceRange SR) {
4083 unsigned ExpansionsSize = 0;
4084 if (HasPackExpansions)
4085 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4086
4087 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4088 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4089 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4090}
4091
4092ObjCDictionaryLiteral *
Craig Topper9db7a7e2013-08-22 04:58:56 +00004093ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004094 bool HasPackExpansions) {
4095 unsigned ExpansionsSize = 0;
4096 if (HasPackExpansions)
4097 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4098 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4099 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4100 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4101 HasPackExpansions);
4102}
4103
Craig Topper9db7a7e2013-08-22 04:58:56 +00004104ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004105 Expr *base,
4106 Expr *key, QualType T,
4107 ObjCMethodDecl *getMethod,
4108 ObjCMethodDecl *setMethod,
4109 SourceLocation RB) {
4110 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4111 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4112 OK_ObjCSubscript,
4113 getMethod, setMethod, RB);
4114}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004115
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004116AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004117 QualType t, AtomicOp op, SourceLocation RP)
4118 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4119 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004120 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004121{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004122 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4123 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004124 if (args[i]->isTypeDependent())
4125 ExprBits.TypeDependent = true;
4126 if (args[i]->isValueDependent())
4127 ExprBits.ValueDependent = true;
4128 if (args[i]->isInstantiationDependent())
4129 ExprBits.InstantiationDependent = true;
4130 if (args[i]->containsUnexpandedParameterPack())
4131 ExprBits.ContainsUnexpandedParameterPack = true;
4132
4133 SubExprs[i] = args[i];
4134 }
4135}
Richard Smithe1b2abc2012-04-10 22:49:28 +00004136
4137unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4138 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00004139 case AO__c11_atomic_init:
4140 case AO__c11_atomic_load:
4141 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004142 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00004143
4144 case AO__c11_atomic_store:
4145 case AO__c11_atomic_exchange:
4146 case AO__atomic_load:
4147 case AO__atomic_store:
4148 case AO__atomic_store_n:
4149 case AO__atomic_exchange_n:
4150 case AO__c11_atomic_fetch_add:
4151 case AO__c11_atomic_fetch_sub:
4152 case AO__c11_atomic_fetch_and:
4153 case AO__c11_atomic_fetch_or:
4154 case AO__c11_atomic_fetch_xor:
4155 case AO__atomic_fetch_add:
4156 case AO__atomic_fetch_sub:
4157 case AO__atomic_fetch_and:
4158 case AO__atomic_fetch_or:
4159 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00004160 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00004161 case AO__atomic_add_fetch:
4162 case AO__atomic_sub_fetch:
4163 case AO__atomic_and_fetch:
4164 case AO__atomic_or_fetch:
4165 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00004166 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004167 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00004168
4169 case AO__atomic_exchange:
4170 return 4;
4171
4172 case AO__c11_atomic_compare_exchange_strong:
4173 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004174 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00004175
4176 case AO__atomic_compare_exchange:
4177 case AO__atomic_compare_exchange_n:
4178 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00004179 }
4180 llvm_unreachable("unknown atomic op");
4181}