blob: 74ef46ff750539c5971d3f6e3929fc9d7ac7e674 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnera4d55d82008-10-06 06:40:35 +000014#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000017#include "clang/AST/DeclCXX.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor25d0a0f2012-02-23 07:33:15 +000020#include "clang/AST/EvaluatedExprVisitor.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000026#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000028#include "clang/Lex/Lexer.h"
29#include "clang/Lex/LiteralSupport.h"
30#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000031#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000032#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000033#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000034#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000035using namespace clang;
36
Rafael Espindola8d852e32012-06-27 18:18:05 +000037const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindola632fbaa2012-06-28 01:56:38 +000038 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000039
40 QualType DerivedType = E->getType();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000041 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
42 DerivedType = PTy->getPointeeType();
43
Rafael Espindola251c4492012-07-17 20:24:05 +000044 if (DerivedType->isDependentType())
45 return NULL;
46
Rafael Espindola0b4fe502012-06-26 17:45:31 +000047 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000048 Decl *D = Ty->getDecl();
49 return cast<CXXRecordDecl>(D);
50}
51
Rafael Espindola0a7dd832012-10-27 01:03:43 +000052const Expr *
53Expr::skipRValueSubobjectAdjustments(
54 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
55 const Expr *E = this;
56 while (true) {
57 E = E->IgnoreParens();
58
59 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
60 if ((CE->getCastKind() == CK_DerivedToBase ||
61 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
62 E->getType()->isRecordType()) {
63 E = CE->getSubExpr();
64 CXXRecordDecl *Derived
65 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
66 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
67 continue;
68 }
69
70 if (CE->getCastKind() == CK_NoOp) {
71 E = CE->getSubExpr();
72 continue;
73 }
74 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
75 if (!ME->isArrow() && ME->getBase()->isRValue()) {
76 assert(ME->getBase()->getType()->isRecordType());
77 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
78 E = ME->getBase();
79 Adjustments.push_back(SubobjectAdjustment(Field));
80 continue;
81 }
82 }
83 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
84 if (BO->isPtrMemOp()) {
Rafael Espindolaef4b6662012-11-01 14:32:20 +000085 assert(BO->getRHS()->isRValue());
Rafael Espindola0a7dd832012-10-27 01:03:43 +000086 E = BO->getLHS();
87 const MemberPointerType *MPT =
88 BO->getRHS()->getType()->getAs<MemberPointerType>();
89 Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
90 }
91 }
92
93 // Nothing changed.
94 break;
95 }
96 return E;
97}
98
99const Expr *
100Expr::findMaterializedTemporary(const MaterializeTemporaryExpr *&MTE) const {
101 const Expr *E = this;
102 // Look through single-element init lists that claim to be lvalues. They're
103 // just syntactic wrappers in this case.
104 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) {
105 if (ILE->getNumInits() == 1 && ILE->isGLValue())
106 E = ILE->getInit(0);
107 }
108
109 // Look through expressions for materialized temporaries (for now).
110 if (const MaterializeTemporaryExpr *M
111 = dyn_cast<MaterializeTemporaryExpr>(E)) {
112 MTE = M;
113 E = M->GetTemporaryExpr();
114 }
115
116 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
117 E = DAE->getExpr();
118 return E;
119}
120
Chris Lattner2b334bb2010-04-16 23:34:13 +0000121/// isKnownToHaveBooleanValue - Return true if this is an integer expression
122/// that is known to return 0 or 1. This happens for _Bool/bool expressions
123/// but also int expressions which are produced by things like comparisons in
124/// C.
125bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +0000126 const Expr *E = IgnoreParens();
127
Chris Lattner2b334bb2010-04-16 23:34:13 +0000128 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +0000129 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +0000130 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +0000131 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +0000132
Peter Collingbournef111d932011-04-15 00:35:48 +0000133 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000134 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000135 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000136 return UO->getSubExpr()->isKnownToHaveBooleanValue();
137 default:
138 return false;
139 }
140 }
Sean Huntc3021132010-05-05 15:23:54 +0000141
John McCall6907fbe2010-06-12 01:56:02 +0000142 // Only look through implicit casts. If the user writes
143 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +0000144 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000145 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000146
Peter Collingbournef111d932011-04-15 00:35:48 +0000147 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000148 switch (BO->getOpcode()) {
149 default: return false;
John McCall2de56d12010-08-25 11:45:40 +0000150 case BO_LT: // Relational operators.
151 case BO_GT:
152 case BO_LE:
153 case BO_GE:
154 case BO_EQ: // Equality operators.
155 case BO_NE:
156 case BO_LAnd: // AND operator.
157 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +0000158 return true;
Sean Huntc3021132010-05-05 15:23:54 +0000159
John McCall2de56d12010-08-25 11:45:40 +0000160 case BO_And: // Bitwise AND operator.
161 case BO_Xor: // Bitwise XOR operator.
162 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +0000163 // Handle things like (x==2)|(y==12).
164 return BO->getLHS()->isKnownToHaveBooleanValue() &&
165 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000166
John McCall2de56d12010-08-25 11:45:40 +0000167 case BO_Comma:
168 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000169 return BO->getRHS()->isKnownToHaveBooleanValue();
170 }
171 }
Sean Huntc3021132010-05-05 15:23:54 +0000172
Peter Collingbournef111d932011-04-15 00:35:48 +0000173 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000174 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
175 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000176
Chris Lattner2b334bb2010-04-16 23:34:13 +0000177 return false;
178}
179
John McCall63c00d72011-02-09 08:16:59 +0000180// Amusing macro metaprogramming hack: check whether a class provides
181// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000182//
183// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-02-09 08:16:59 +0000184namespace {
185 /// This implementation is used when a class provides a custom
186 /// implementation of getExprLoc.
187 template <class E, class T>
188 SourceLocation getExprLocImpl(const Expr *expr,
189 SourceLocation (T::*v)() const) {
190 return static_cast<const E*>(expr)->getExprLoc();
191 }
192
193 /// This implementation is used when a class doesn't provide
194 /// a custom implementation of getExprLoc. Overload resolution
195 /// should pick it over the implementation above because it's
196 /// more specialized according to function template partial ordering.
197 template <class E>
198 SourceLocation getExprLocImpl(const Expr *expr,
199 SourceLocation (Expr::*v)() const) {
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000200 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-02-09 08:16:59 +0000201 }
202}
203
204SourceLocation Expr::getExprLoc() const {
205 switch (getStmtClass()) {
206 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
207#define ABSTRACT_STMT(type)
208#define STMT(type, base) \
209 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
210#define EXPR(type, base) \
211 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
212#include "clang/AST/StmtNodes.inc"
213 }
214 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000215}
216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217//===----------------------------------------------------------------------===//
218// Primary Expressions.
219//===----------------------------------------------------------------------===//
220
Douglas Gregor561f8122011-07-01 01:22:09 +0000221/// \brief Compute the type-, value-, and instantiation-dependence of a
222/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000223/// based on the declaration being referenced.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000224static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregord967e312011-01-19 21:52:31 +0000225 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000226 bool &ValueDependent,
227 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000228 TypeDependent = false;
229 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000230 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000231
232 // (TD) C++ [temp.dep.expr]p3:
233 // An id-expression is type-dependent if it contains:
234 //
Sean Huntc3021132010-05-05 15:23:54 +0000235 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000236 //
237 // (VD) C++ [temp.dep.constexpr]p2:
238 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000239
Douglas Gregor0da76df2009-11-23 11:41:28 +0000240 // (TD) - an identifier that was declared with dependent type
241 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000242 if (T->isDependentType()) {
243 TypeDependent = true;
244 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000245 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000246 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 } else if (T->isInstantiationDependentType()) {
248 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000249 }
Douglas Gregord967e312011-01-19 21:52:31 +0000250
Douglas Gregor0da76df2009-11-23 11:41:28 +0000251 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000252 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000253 == DeclarationName::CXXConversionFunctionName) {
254 QualType T = D->getDeclName().getCXXNameType();
255 if (T->isDependentType()) {
256 TypeDependent = true;
257 ValueDependent = true;
258 InstantiationDependent = true;
259 return;
260 }
261
262 if (T->isInstantiationDependentType())
263 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000264 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000265
Douglas Gregor0da76df2009-11-23 11:41:28 +0000266 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000267 if (isa<NonTypeTemplateParmDecl>(D)) {
268 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000269 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000270 return;
271 }
272
Douglas Gregor0da76df2009-11-23 11:41:28 +0000273 // (VD) - a constant with integral or enumeration type and is
274 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000275 // (VD) - a constant with literal type and is initialized with an
276 // expression that is value-dependent [C++11].
277 // (VD) - FIXME: Missing from the standard:
278 // - an entity with reference type and is initialized with an
279 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000280 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith80ad52f2013-01-02 11:42:31 +0000281 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smithdb1822c2011-11-08 01:31:09 +0000282 Var->getType()->isLiteralType() :
283 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikie4ef832f2012-08-10 00:55:35 +0000284 (Var->getType().isConstQualified() ||
Richard Smithdb1822c2011-11-08 01:31:09 +0000285 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000286 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000287 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000288 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000289 InstantiationDependent = true;
290 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000291 }
292
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000293 // (VD) - FIXME: Missing from the standard:
294 // - a member function or a static data member of the current
295 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000296 if (Var->isStaticDataMember() &&
297 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000298 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000299 InstantiationDependent = true;
300 }
Douglas Gregord967e312011-01-19 21:52:31 +0000301
302 return;
303 }
304
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000305 // (VD) - FIXME: Missing from the standard:
306 // - a member function or a static data member of the current
307 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000308 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
309 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000310 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000311 }
Douglas Gregord967e312011-01-19 21:52:31 +0000312}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000313
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000314void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000315 bool TypeDependent = false;
316 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000317 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000318 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
319 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000320
321 // (TD) C++ [temp.dep.expr]p3:
322 // An id-expression is type-dependent if it contains:
323 //
324 // and
325 //
326 // (VD) C++ [temp.dep.constexpr]p2:
327 // An identifier is value-dependent if it is:
328 if (!TypeDependent && !ValueDependent &&
329 hasExplicitTemplateArgs() &&
330 TemplateSpecializationType::anyDependentTemplateArguments(
331 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000332 getNumTemplateArgs(),
333 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000334 TypeDependent = true;
335 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000336 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000337 }
338
339 ExprBits.TypeDependent = TypeDependent;
340 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000341 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000342
Douglas Gregor10738d32010-12-23 23:51:58 +0000343 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000344 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000345 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000346}
347
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000348DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
349 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000350 SourceLocation TemplateKWLoc,
John McCallf4b88a42012-03-10 09:33:50 +0000351 ValueDecl *D, bool RefersToEnclosingLocal,
352 const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000353 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000354 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000355 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000356 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000357 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
358 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000359 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000360 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000361 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
362 if (FoundD)
363 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000364 DeclRefExprBits.HasTemplateKWAndArgsInfo
365 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCallf4b88a42012-03-10 09:33:50 +0000366 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor561f8122011-07-01 01:22:09 +0000367 if (TemplateArgs) {
368 bool Dependent = false;
369 bool InstantiationDependent = false;
370 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000371 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
372 Dependent,
373 InstantiationDependent,
374 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000375 if (InstantiationDependent)
376 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000377 } else if (TemplateKWLoc.isValid()) {
378 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000379 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000380 DeclRefExprBits.HadMultipleCandidates = 0;
381
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000382 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000383}
384
Douglas Gregora2813ce2009-10-23 18:54:35 +0000385DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000386 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000387 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000388 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000389 bool RefersToEnclosingLocal,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000390 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000391 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000392 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000393 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000394 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000395 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000396 RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000397 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000398 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000399}
400
401DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000402 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000403 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000404 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000405 bool RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000406 const DeclarationNameInfo &NameInfo,
407 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000408 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000409 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000410 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000411 // Filter out cases where the found Decl is the same as the value refenenced.
412 if (D == FoundD)
413 FoundD = 0;
414
Douglas Gregora2813ce2009-10-23 18:54:35 +0000415 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000416 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000417 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000418 if (FoundD)
419 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000420 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000421 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
422 else if (TemplateKWLoc.isValid())
423 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000424
Chris Lattner32488542010-10-30 05:14:06 +0000425 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000426 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000427 RefersToEnclosingLocal,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000428 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000429}
430
Chandler Carruth3aa81402011-05-01 23:48:14 +0000431DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000432 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000433 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000434 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000435 unsigned NumTemplateArgs) {
436 std::size_t Size = sizeof(DeclRefExpr);
437 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000438 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000439 if (HasFoundDecl)
440 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000441 if (HasTemplateKWAndArgsInfo)
442 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000443
Chris Lattner32488542010-10-30 05:14:06 +0000444 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000445 return new (Mem) DeclRefExpr(EmptyShell());
446}
447
Daniel Dunbar396ec672012-03-09 15:39:15 +0000448SourceLocation DeclRefExpr::getLocStart() const {
449 if (hasQualifier())
450 return getQualifierLoc().getBeginLoc();
451 return getNameInfo().getLocStart();
452}
453SourceLocation DeclRefExpr::getLocEnd() const {
454 if (hasExplicitTemplateArgs())
455 return getRAngleLoc();
456 return getNameInfo().getLocEnd();
457}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000458
Anders Carlsson3a082d82009-09-08 18:24:21 +0000459// FIXME: Maybe this should use DeclPrinter with a special "print predefined
460// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000461std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
462 ASTContext &Context = CurrentDecl->getASTContext();
463
Anders Carlsson3a082d82009-09-08 18:24:21 +0000464 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000465 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000466 return FD->getNameAsString();
467
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000468 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000469 llvm::raw_svector_ostream Out(Name);
470
471 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000472 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000473 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000474 if (MD->isStatic())
475 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000476 }
477
David Blaikie4e4d0842012-03-11 07:00:24 +0000478 PrintingPolicy Policy(Context.getLangOpts());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000479 std::string Proto = FD->getQualifiedNameAsString(Policy);
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000480 llvm::raw_string_ostream POut(Proto);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000481
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000482 const FunctionDecl *Decl = FD;
483 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
484 Decl = Pattern;
485 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000486 const FunctionProtoType *FT = 0;
487 if (FD->hasWrittenPrototype())
488 FT = dyn_cast<FunctionProtoType>(AFT);
489
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000490 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000491 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000492 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000493 if (i) POut << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000494 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000495 }
496
497 if (FT->isVariadic()) {
498 if (FD->getNumParams()) POut << ", ";
499 POut << "...";
500 }
501 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000502 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000503
Sam Weinig4eadcc52009-12-27 01:38:20 +0000504 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis4ae711b2012-12-14 19:44:11 +0000505 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikie4ef832f2012-08-10 00:55:35 +0000506 if (FT->isConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000507 POut << " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000508 if (FT->isVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000509 POut << " volatile";
510 RefQualifierKind Ref = MD->getRefQualifier();
511 if (Ref == RQ_LValue)
512 POut << " &";
513 else if (Ref == RQ_RValue)
514 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000515 }
516
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000517 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
518 SpecsTy Specs;
519 const DeclContext *Ctx = FD->getDeclContext();
520 while (Ctx && isa<NamedDecl>(Ctx)) {
521 const ClassTemplateSpecializationDecl *Spec
522 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
523 if (Spec && !Spec->isExplicitSpecialization())
524 Specs.push_back(Spec);
525 Ctx = Ctx->getParent();
526 }
527
528 std::string TemplateParams;
529 llvm::raw_string_ostream TOut(TemplateParams);
530 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
531 I != E; ++I) {
532 const TemplateParameterList *Params
533 = (*I)->getSpecializedTemplate()->getTemplateParameters();
534 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
535 assert(Params->size() == Args.size());
536 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
537 StringRef Param = Params->getParam(i)->getName();
538 if (Param.empty()) continue;
539 TOut << Param << " = ";
540 Args.get(i).print(Policy, TOut);
541 TOut << ", ";
542 }
543 }
544
545 FunctionTemplateSpecializationInfo *FSI
546 = FD->getTemplateSpecializationInfo();
547 if (FSI && !FSI->isExplicitSpecialization()) {
548 const TemplateParameterList* Params
549 = FSI->getTemplate()->getTemplateParameters();
550 const TemplateArgumentList* Args = FSI->TemplateArguments;
551 assert(Params->size() == Args->size());
552 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
553 StringRef Param = Params->getParam(i)->getName();
554 if (Param.empty()) continue;
555 TOut << Param << " = ";
556 Args->get(i).print(Policy, TOut);
557 TOut << ", ";
558 }
559 }
560
561 TOut.flush();
562 if (!TemplateParams.empty()) {
563 // remove the trailing comma and space
564 TemplateParams.resize(TemplateParams.size() - 2);
565 POut << " [" << TemplateParams << "]";
566 }
567
568 POut.flush();
569
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000570 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
571 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000572
573 Out << Proto;
574
575 Out.flush();
576 return Name.str().str();
577 }
578 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000579 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000580 llvm::raw_svector_ostream Out(Name);
581 Out << (MD->isInstanceMethod() ? '-' : '+');
582 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000583
584 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
585 // a null check to avoid a crash.
586 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000587 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000588
Anders Carlsson3a082d82009-09-08 18:24:21 +0000589 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000590 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000591 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000592
Anders Carlsson3a082d82009-09-08 18:24:21 +0000593 Out << ' ';
594 Out << MD->getSelector().getAsString();
595 Out << ']';
596
597 Out.flush();
598 return Name.str().str();
599 }
600 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
601 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
602 return "top level";
603 }
604 return "";
605}
606
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000607void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
608 if (hasAllocation())
609 C.Deallocate(pVal);
610
611 BitWidth = Val.getBitWidth();
612 unsigned NumWords = Val.getNumWords();
613 const uint64_t* Words = Val.getRawData();
614 if (NumWords > 1) {
615 pVal = new (C) uint64_t[NumWords];
616 std::copy(Words, Words + NumWords, pVal);
617 } else if (NumWords == 1)
618 VAL = Words[0];
619 else
620 VAL = 0;
621}
622
Benjamin Kramer478851c2012-07-04 17:04:04 +0000623IntegerLiteral::IntegerLiteral(ASTContext &C, const llvm::APInt &V,
624 QualType type, SourceLocation l)
625 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
626 false, false),
627 Loc(l) {
628 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
629 assert(V.getBitWidth() == C.getIntWidth(type) &&
630 "Integer type is not the correct size for constant.");
631 setValue(C, V);
632}
633
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000634IntegerLiteral *
635IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
636 QualType type, SourceLocation l) {
637 return new (C) IntegerLiteral(C, V, type, l);
638}
639
640IntegerLiteral *
641IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
642 return new (C) IntegerLiteral(Empty);
643}
644
Benjamin Kramer478851c2012-07-04 17:04:04 +0000645FloatingLiteral::FloatingLiteral(ASTContext &C, const llvm::APFloat &V,
646 bool isexact, QualType Type, SourceLocation L)
647 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
648 false, false), Loc(L) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000649 setSemantics(V.getSemantics());
Benjamin Kramer478851c2012-07-04 17:04:04 +0000650 FloatingLiteralBits.IsExact = isexact;
651 setValue(C, V);
652}
653
654FloatingLiteral::FloatingLiteral(ASTContext &C, EmptyShell Empty)
655 : Expr(FloatingLiteralClass, Empty) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000656 setRawSemantics(IEEEhalf);
Benjamin Kramer478851c2012-07-04 17:04:04 +0000657 FloatingLiteralBits.IsExact = false;
658}
659
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000660FloatingLiteral *
661FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
662 bool isexact, QualType Type, SourceLocation L) {
663 return new (C) FloatingLiteral(C, V, isexact, Type, L);
664}
665
666FloatingLiteral *
667FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000668 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000669}
670
Tim Northover9ec55f22013-01-22 09:46:51 +0000671const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
672 switch(FloatingLiteralBits.Semantics) {
673 case IEEEhalf:
674 return llvm::APFloat::IEEEhalf;
675 case IEEEsingle:
676 return llvm::APFloat::IEEEsingle;
677 case IEEEdouble:
678 return llvm::APFloat::IEEEdouble;
679 case x87DoubleExtended:
680 return llvm::APFloat::x87DoubleExtended;
681 case IEEEquad:
682 return llvm::APFloat::IEEEquad;
683 case PPCDoubleDouble:
684 return llvm::APFloat::PPCDoubleDouble;
685 }
686 llvm_unreachable("Unrecognised floating semantics");
687}
688
689void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
690 if (&Sem == &llvm::APFloat::IEEEhalf)
691 FloatingLiteralBits.Semantics = IEEEhalf;
692 else if (&Sem == &llvm::APFloat::IEEEsingle)
693 FloatingLiteralBits.Semantics = IEEEsingle;
694 else if (&Sem == &llvm::APFloat::IEEEdouble)
695 FloatingLiteralBits.Semantics = IEEEdouble;
696 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
697 FloatingLiteralBits.Semantics = x87DoubleExtended;
698 else if (&Sem == &llvm::APFloat::IEEEquad)
699 FloatingLiteralBits.Semantics = IEEEquad;
700 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
701 FloatingLiteralBits.Semantics = PPCDoubleDouble;
702 else
703 llvm_unreachable("Unknown floating semantics");
704}
705
Chris Lattnerda8249e2008-06-07 22:13:43 +0000706/// getValueAsApproximateDouble - This returns the value as an inaccurate
707/// double. Note that this may cause loss of precision, but is useful for
708/// debugging dumps, etc.
709double FloatingLiteral::getValueAsApproximateDouble() const {
710 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000711 bool ignored;
712 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
713 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000714 return V.convertToDouble();
715}
716
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000717int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000718 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000719 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000720 case Ascii:
721 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000722 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000723 break;
724 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000725 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000726 break;
727 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000728 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000729 break;
730 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000731 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000732 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000733 }
734 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
735 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000736 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000737 && "character byte widths supported are 1, 2, and 4 only");
738 return CharByteWidth;
739}
740
Chris Lattner5f9e2722011-07-23 10:55:15 +0000741StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000742 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000743 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000744 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000745 // Allocate enough space for the StringLiteral plus an array of locations for
746 // any concatenated string tokens.
747 void *Mem = C.Allocate(sizeof(StringLiteral)+
748 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000749 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000750 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000753 SL->setString(C,Str,Kind,Pascal);
754
Chris Lattner2085fd62009-02-18 06:40:38 +0000755 SL->TokLocs[0] = Loc[0];
756 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000757
Chris Lattner726e1682009-02-18 05:49:11 +0000758 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000759 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
760 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000761}
762
Douglas Gregor673ecd62009-04-15 16:35:07 +0000763StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
764 void *Mem = C.Allocate(sizeof(StringLiteral)+
765 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000766 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000767 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000768 SL->CharByteWidth = 0;
769 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000770 SL->NumConcatenated = NumStrs;
771 return SL;
772}
773
Richard Trieu8ab09da2012-06-13 20:25:24 +0000774void StringLiteral::outputString(raw_ostream &OS) {
775 switch (getKind()) {
776 case Ascii: break; // no prefix.
777 case Wide: OS << 'L'; break;
778 case UTF8: OS << "u8"; break;
779 case UTF16: OS << 'u'; break;
780 case UTF32: OS << 'U'; break;
781 }
782 OS << '"';
783 static const char Hex[] = "0123456789ABCDEF";
784
785 unsigned LastSlashX = getLength();
786 for (unsigned I = 0, N = getLength(); I != N; ++I) {
787 switch (uint32_t Char = getCodeUnit(I)) {
788 default:
789 // FIXME: Convert UTF-8 back to codepoints before rendering.
790
791 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
792 // Leave invalid surrogates alone; we'll use \x for those.
793 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
794 Char <= 0xdbff) {
795 uint32_t Trail = getCodeUnit(I + 1);
796 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
797 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
798 ++I;
799 }
800 }
801
802 if (Char > 0xff) {
803 // If this is a wide string, output characters over 0xff using \x
804 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
805 // codepoint: use \x escapes for invalid codepoints.
806 if (getKind() == Wide ||
807 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
808 // FIXME: Is this the best way to print wchar_t?
809 OS << "\\x";
810 int Shift = 28;
811 while ((Char >> Shift) == 0)
812 Shift -= 4;
813 for (/**/; Shift >= 0; Shift -= 4)
814 OS << Hex[(Char >> Shift) & 15];
815 LastSlashX = I;
816 break;
817 }
818
819 if (Char > 0xffff)
820 OS << "\\U00"
821 << Hex[(Char >> 20) & 15]
822 << Hex[(Char >> 16) & 15];
823 else
824 OS << "\\u";
825 OS << Hex[(Char >> 12) & 15]
826 << Hex[(Char >> 8) & 15]
827 << Hex[(Char >> 4) & 15]
828 << Hex[(Char >> 0) & 15];
829 break;
830 }
831
832 // If we used \x... for the previous character, and this character is a
833 // hexadecimal digit, prevent it being slurped as part of the \x.
834 if (LastSlashX + 1 == I) {
835 switch (Char) {
836 case '0': case '1': case '2': case '3': case '4':
837 case '5': case '6': case '7': case '8': case '9':
838 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
839 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
840 OS << "\"\"";
841 }
842 }
843
844 assert(Char <= 0xff &&
845 "Characters above 0xff should already have been handled.");
846
847 if (isprint(Char))
848 OS << (char)Char;
849 else // Output anything hard as an octal escape.
850 OS << '\\'
851 << (char)('0' + ((Char >> 6) & 7))
852 << (char)('0' + ((Char >> 3) & 7))
853 << (char)('0' + ((Char >> 0) & 7));
854 break;
855 // Handle some common non-printable cases to make dumps prettier.
856 case '\\': OS << "\\\\"; break;
857 case '"': OS << "\\\""; break;
858 case '\n': OS << "\\n"; break;
859 case '\t': OS << "\\t"; break;
860 case '\a': OS << "\\a"; break;
861 case '\b': OS << "\\b"; break;
862 }
863 }
864 OS << '"';
865}
866
Eli Friedman64f45a22011-11-01 02:23:42 +0000867void StringLiteral::setString(ASTContext &C, StringRef Str,
868 StringKind Kind, bool IsPascal) {
869 //FIXME: we assume that the string data comes from a target that uses the same
870 // code unit size and endianess for the type of string.
871 this->Kind = Kind;
872 this->IsPascal = IsPascal;
873
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000874 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000875 assert((Str.size()%CharByteWidth == 0)
876 && "size of data must be multiple of CharByteWidth");
877 Length = Str.size()/CharByteWidth;
878
879 switch(CharByteWidth) {
880 case 1: {
881 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000882 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000883 StrData.asChar = AStrData;
884 break;
885 }
886 case 2: {
887 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000888 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000889 StrData.asUInt16 = AStrData;
890 break;
891 }
892 case 4: {
893 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000894 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000895 StrData.asUInt32 = AStrData;
896 break;
897 }
898 default:
899 assert(false && "unsupported CharByteWidth");
900 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000901}
902
Chris Lattner08f92e32010-11-17 07:37:15 +0000903/// getLocationOfByte - Return a source location that points to the specified
904/// byte of this string literal.
905///
906/// Strings are amazingly complex. They can be formed from multiple tokens and
907/// can have escape sequences in them in addition to the usual trigraph and
908/// escaped newline business. This routine handles this complexity.
909///
910SourceLocation StringLiteral::
911getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
912 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000913 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
914 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000915
Chris Lattner08f92e32010-11-17 07:37:15 +0000916 // Loop over all of the tokens in this string until we find the one that
917 // contains the byte we're looking for.
918 unsigned TokNo = 0;
919 while (1) {
920 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
921 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
922
923 // Get the spelling of the string so that we can get the data that makes up
924 // the string literal, not the identifier for the macro it is potentially
925 // expanded through.
926 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
927
928 // Re-lex the token to get its length and original spelling.
929 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
930 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000931 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000932 if (Invalid)
933 return StrTokSpellingLoc;
934
935 const char *StrData = Buffer.data()+LocInfo.second;
936
Chris Lattner08f92e32010-11-17 07:37:15 +0000937 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000938 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
939 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000940 Token TheTok;
941 TheLexer.LexFromRawLexer(TheTok);
942
943 // Use the StringLiteralParser to compute the length of the string in bytes.
944 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
945 unsigned TokNumBytes = SLP.GetStringLength();
946
947 // If the byte is in this token, return the location of the byte.
948 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000949 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000950 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
951
952 // Now that we know the offset of the token in the spelling, use the
953 // preprocessor to get the offset in the original source.
954 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
955 }
956
957 // Move to the next string token.
958 ++TokNo;
959 ByteNo -= TokNumBytes;
960 }
961}
962
963
964
Reid Spencer5f016e22007-07-11 17:01:13 +0000965/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
966/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie0bea8632012-10-08 01:11:04 +0000967StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000969 case UO_PostInc: return "++";
970 case UO_PostDec: return "--";
971 case UO_PreInc: return "++";
972 case UO_PreDec: return "--";
973 case UO_AddrOf: return "&";
974 case UO_Deref: return "*";
975 case UO_Plus: return "+";
976 case UO_Minus: return "-";
977 case UO_Not: return "~";
978 case UO_LNot: return "!";
979 case UO_Real: return "__real";
980 case UO_Imag: return "__imag";
981 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000983 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000984}
985
John McCall2de56d12010-08-25 11:45:40 +0000986UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000987UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
988 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000989 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000990 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
991 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
992 case OO_Amp: return UO_AddrOf;
993 case OO_Star: return UO_Deref;
994 case OO_Plus: return UO_Plus;
995 case OO_Minus: return UO_Minus;
996 case OO_Tilde: return UO_Not;
997 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000998 }
999}
1000
1001OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1002 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +00001003 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1004 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1005 case UO_AddrOf: return OO_Amp;
1006 case UO_Deref: return OO_Star;
1007 case UO_Plus: return OO_Plus;
1008 case UO_Minus: return OO_Minus;
1009 case UO_Not: return OO_Tilde;
1010 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001011 default: return OO_None;
1012 }
1013}
1014
1015
Reid Spencer5f016e22007-07-11 17:01:13 +00001016//===----------------------------------------------------------------------===//
1017// Postfix Operators.
1018//===----------------------------------------------------------------------===//
1019
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001020CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001021 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +00001022 SourceLocation rparenloc)
1023 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001024 fn->isTypeDependent(),
1025 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001026 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001027 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001028 NumArgs(args.size()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001030 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +00001031 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001032 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001033 if (args[i]->isTypeDependent())
1034 ExprBits.TypeDependent = true;
1035 if (args[i]->isValueDependent())
1036 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001037 if (args[i]->isInstantiationDependent())
1038 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001039 if (args[i]->containsUnexpandedParameterPack())
1040 ExprBits.ContainsUnexpandedParameterPack = true;
1041
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001042 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001043 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001044
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001045 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +00001046 RParenLoc = rparenloc;
1047}
Nate Begemane2ce1d92008-01-17 17:46:27 +00001048
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001049CallExpr::CallExpr(ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCallf89e55a2010-11-18 06:31:45 +00001050 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1051 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001052 fn->isTypeDependent(),
1053 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001054 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001055 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001056 NumArgs(args.size()) {
Ted Kremenek668bf912009-02-09 20:51:47 +00001057
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001058 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001059 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001060 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001061 if (args[i]->isTypeDependent())
1062 ExprBits.TypeDependent = true;
1063 if (args[i]->isValueDependent())
1064 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001065 if (args[i]->isInstantiationDependent())
1066 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001067 if (args[i]->containsUnexpandedParameterPack())
1068 ExprBits.ContainsUnexpandedParameterPack = true;
1069
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001070 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001071 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001072
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001073 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 RParenLoc = rparenloc;
1075}
1076
Mike Stump1eb44332009-09-09 15:08:12 +00001077CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
1078 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001079 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001080 SubExprs = new (C) Stmt*[PREARGS_START];
1081 CallExprBits.NumPreArgs = 0;
1082}
1083
1084CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
1085 EmptyShell Empty)
1086 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1087 // FIXME: Why do we allocate this?
1088 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1089 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001090}
1091
Nuno Lopesd20254f2009-12-20 23:11:08 +00001092Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +00001093 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +00001094
1095 while (SubstNonTypeTemplateParmExpr *NTTP
1096 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1097 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1098 }
1099
Sebastian Redl20012152010-09-10 20:55:30 +00001100 // If we're calling a dereference, look at the pointer instead.
1101 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1102 if (BO->isPtrMemOp())
1103 CEE = BO->getRHS()->IgnoreParenCasts();
1104 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1105 if (UO->getOpcode() == UO_Deref)
1106 CEE = UO->getSubExpr()->IgnoreParenCasts();
1107 }
Chris Lattner6346f962009-07-17 15:46:27 +00001108 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001109 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001110 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1111 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001112
1113 return 0;
1114}
1115
Nuno Lopesd20254f2009-12-20 23:11:08 +00001116FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001117 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001118}
1119
Chris Lattnerd18b3292007-12-28 05:25:02 +00001120/// setNumArgs - This changes the number of arguments present in this call.
1121/// Any orphaned expressions are deleted by this, and any new operands are set
1122/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001123void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001124 // No change, just return.
1125 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Chris Lattnerd18b3292007-12-28 05:25:02 +00001127 // If shrinking # arguments, just delete the extras and forgot them.
1128 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001129 this->NumArgs = NumArgs;
1130 return;
1131 }
1132
1133 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001134 unsigned NumPreArgs = getNumPreArgs();
1135 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001136 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001137 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001138 NewSubExprs[i] = SubExprs[i];
1139 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001140 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1141 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001142 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Douglas Gregor88c9a462009-04-17 21:46:47 +00001144 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001145 SubExprs = NewSubExprs;
1146 this->NumArgs = NumArgs;
1147}
1148
Chris Lattnercb888962008-10-06 05:00:53 +00001149/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1150/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001151unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001152 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001153 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001154 // ImplicitCastExpr.
1155 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1156 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001157 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001159 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1160 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001161 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Anders Carlssonbcba2012008-01-31 02:13:57 +00001163 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1164 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001165 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001167 if (!FDecl->getIdentifier())
1168 return 0;
1169
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001170 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001171}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001172
Richard Smithba571832013-01-17 23:46:04 +00001173bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1174 if (unsigned BI = isBuiltinCall())
1175 return Ctx.BuiltinInfo.isUnevaluated(BI);
1176 return false;
1177}
1178
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001179QualType CallExpr::getCallReturnType() const {
1180 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001181 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001182 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001183 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001184 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001185 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1186 // This should never be overloaded and so should never return null.
1187 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001188
John McCall864c0412011-04-26 20:42:42 +00001189 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001190 return FnType->getResultType();
1191}
Chris Lattnercb888962008-10-06 05:00:53 +00001192
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001193SourceLocation CallExpr::getLocStart() const {
1194 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001195 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001196
1197 SourceLocation begin = getCallee()->getLocStart();
1198 if (begin.isInvalid() && getNumArgs() > 0)
1199 begin = getArg(0)->getLocStart();
1200 return begin;
1201}
1202SourceLocation CallExpr::getLocEnd() const {
1203 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001204 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001205
1206 SourceLocation end = getRParenLoc();
1207 if (end.isInvalid() && getNumArgs() > 0)
1208 end = getArg(getNumArgs() - 1)->getLocEnd();
1209 return end;
1210}
John McCall2882eca2011-02-21 06:23:05 +00001211
Sean Huntc3021132010-05-05 15:23:54 +00001212OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001213 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001214 TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001215 ArrayRef<OffsetOfNode> comps,
1216 ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001217 SourceLocation RParenLoc) {
1218 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001219 sizeof(OffsetOfNode) * comps.size() +
1220 sizeof(Expr*) * exprs.size());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001221
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001222 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1223 RParenLoc);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001224}
1225
1226OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1227 unsigned numComps, unsigned numExprs) {
1228 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1229 sizeof(OffsetOfNode) * numComps +
1230 sizeof(Expr*) * numExprs);
1231 return new (Mem) OffsetOfExpr(numComps, numExprs);
1232}
1233
Sean Huntc3021132010-05-05 15:23:54 +00001234OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001235 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001236 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001237 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001238 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1239 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001240 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001241 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001242 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001243 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001244 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001245{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001246 for (unsigned i = 0; i != comps.size(); ++i) {
1247 setComponent(i, comps[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001248 }
Sean Huntc3021132010-05-05 15:23:54 +00001249
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001250 for (unsigned i = 0; i != exprs.size(); ++i) {
1251 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001252 ExprBits.ValueDependent = true;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001253 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001254 ExprBits.ContainsUnexpandedParameterPack = true;
1255
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001256 setIndexExpr(i, exprs[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001257 }
1258}
1259
1260IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1261 assert(getKind() == Field || getKind() == Identifier);
1262 if (getKind() == Field)
1263 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001264
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001265 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1266}
1267
Mike Stump1eb44332009-09-09 15:08:12 +00001268MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001269 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001270 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001271 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001272 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001273 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001274 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001275 QualType ty,
1276 ExprValueKind vk,
1277 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001278 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001279
Douglas Gregor40d96a62011-02-28 21:54:11 +00001280 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001281 founddecl.getDecl() != memberdecl ||
1282 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001283 if (hasQualOrFound)
1284 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001285
John McCalld5532b62009-11-23 01:53:49 +00001286 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001287 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1288 else if (TemplateKWLoc.isValid())
1289 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Chris Lattner32488542010-10-30 05:14:06 +00001291 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001292 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1293 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001294
1295 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001296 // FIXME: Wrong. We should be looking at the member declaration we found.
1297 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001298 E->setValueDependent(true);
1299 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001300 E->setInstantiationDependent(true);
1301 }
1302 else if (QualifierLoc &&
1303 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1304 E->setInstantiationDependent(true);
1305
John McCall6bb80172010-03-30 21:47:33 +00001306 E->HasQualifierOrFoundDecl = true;
1307
1308 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001309 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001310 NQ->FoundDecl = founddecl;
1311 }
1312
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001313 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1314
John McCall6bb80172010-03-30 21:47:33 +00001315 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001316 bool Dependent = false;
1317 bool InstantiationDependent = false;
1318 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001319 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1320 Dependent,
1321 InstantiationDependent,
1322 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001323 if (InstantiationDependent)
1324 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001325 } else if (TemplateKWLoc.isValid()) {
1326 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001327 }
1328
1329 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001330}
1331
Daniel Dunbar396ec672012-03-09 15:39:15 +00001332SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001333 if (isImplicitAccess()) {
1334 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001335 return getQualifierLoc().getBeginLoc();
1336 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001337 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001338
Daniel Dunbar396ec672012-03-09 15:39:15 +00001339 // FIXME: We don't want this to happen. Rather, we should be able to
1340 // detect all kinds of implicit accesses more cleanly.
1341 SourceLocation BaseStartLoc = getBase()->getLocStart();
1342 if (BaseStartLoc.isValid())
1343 return BaseStartLoc;
1344 return MemberLoc;
1345}
1346SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001347 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbar396ec672012-03-09 15:39:15 +00001348 if (hasExplicitTemplateArgs())
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001349 EndLoc = getRAngleLoc();
1350 else if (EndLoc.isInvalid())
1351 EndLoc = getBase()->getLocEnd();
1352 return EndLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001353}
1354
John McCall1d9b3b22011-09-09 05:25:32 +00001355void CastExpr::CheckCastConsistency() const {
1356 switch (getCastKind()) {
1357 case CK_DerivedToBase:
1358 case CK_UncheckedDerivedToBase:
1359 case CK_DerivedToBaseMemberPointer:
1360 case CK_BaseToDerived:
1361 case CK_BaseToDerivedMemberPointer:
1362 assert(!path_empty() && "Cast kind should have a base path!");
1363 break;
1364
1365 case CK_CPointerToObjCPointerCast:
1366 assert(getType()->isObjCObjectPointerType());
1367 assert(getSubExpr()->getType()->isPointerType());
1368 goto CheckNoBasePath;
1369
1370 case CK_BlockPointerToObjCPointerCast:
1371 assert(getType()->isObjCObjectPointerType());
1372 assert(getSubExpr()->getType()->isBlockPointerType());
1373 goto CheckNoBasePath;
1374
John McCall4d4e5c12012-02-15 01:22:51 +00001375 case CK_ReinterpretMemberPointer:
1376 assert(getType()->isMemberPointerType());
1377 assert(getSubExpr()->getType()->isMemberPointerType());
1378 goto CheckNoBasePath;
1379
John McCall1d9b3b22011-09-09 05:25:32 +00001380 case CK_BitCast:
1381 // Arbitrary casts to C pointer types count as bitcasts.
1382 // Otherwise, we should only have block and ObjC pointer casts
1383 // here if they stay within the type kind.
1384 if (!getType()->isPointerType()) {
1385 assert(getType()->isObjCObjectPointerType() ==
1386 getSubExpr()->getType()->isObjCObjectPointerType());
1387 assert(getType()->isBlockPointerType() ==
1388 getSubExpr()->getType()->isBlockPointerType());
1389 }
1390 goto CheckNoBasePath;
1391
1392 case CK_AnyPointerToBlockPointerCast:
1393 assert(getType()->isBlockPointerType());
1394 assert(getSubExpr()->getType()->isAnyPointerType() &&
1395 !getSubExpr()->getType()->isBlockPointerType());
1396 goto CheckNoBasePath;
1397
Douglas Gregorac1303e2012-02-22 05:02:47 +00001398 case CK_CopyAndAutoreleaseBlockObject:
1399 assert(getType()->isBlockPointerType());
1400 assert(getSubExpr()->getType()->isBlockPointerType());
1401 goto CheckNoBasePath;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001402
1403 case CK_FunctionToPointerDecay:
1404 assert(getType()->isPointerType());
1405 assert(getSubExpr()->getType()->isFunctionType());
1406 goto CheckNoBasePath;
1407
John McCall1d9b3b22011-09-09 05:25:32 +00001408 // These should not have an inheritance path.
1409 case CK_Dynamic:
1410 case CK_ToUnion:
1411 case CK_ArrayToPointerDecay:
John McCall1d9b3b22011-09-09 05:25:32 +00001412 case CK_NullToMemberPointer:
1413 case CK_NullToPointer:
1414 case CK_ConstructorConversion:
1415 case CK_IntegralToPointer:
1416 case CK_PointerToIntegral:
1417 case CK_ToVoid:
1418 case CK_VectorSplat:
1419 case CK_IntegralCast:
1420 case CK_IntegralToFloating:
1421 case CK_FloatingToIntegral:
1422 case CK_FloatingCast:
1423 case CK_ObjCObjectLValueCast:
1424 case CK_FloatingRealToComplex:
1425 case CK_FloatingComplexToReal:
1426 case CK_FloatingComplexCast:
1427 case CK_FloatingComplexToIntegralComplex:
1428 case CK_IntegralRealToComplex:
1429 case CK_IntegralComplexToReal:
1430 case CK_IntegralComplexCast:
1431 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001432 case CK_ARCProduceObject:
1433 case CK_ARCConsumeObject:
1434 case CK_ARCReclaimReturnedObject:
1435 case CK_ARCExtendBlockObject:
Guy Benyeie6b9d802013-01-20 12:31:11 +00001436 case CK_ZeroToOCLEvent:
John McCall1d9b3b22011-09-09 05:25:32 +00001437 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1438 goto CheckNoBasePath;
1439
1440 case CK_Dependent:
1441 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001442 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001443 case CK_AtomicToNonAtomic:
1444 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001445 case CK_PointerToBoolean:
1446 case CK_IntegralToBoolean:
1447 case CK_FloatingToBoolean:
1448 case CK_MemberPointerToBoolean:
1449 case CK_FloatingComplexToBoolean:
1450 case CK_IntegralComplexToBoolean:
1451 case CK_LValueBitCast: // -> bool&
1452 case CK_UserDefinedConversion: // operator bool()
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001453 case CK_BuiltinFnToFnPtr:
John McCall1d9b3b22011-09-09 05:25:32 +00001454 CheckNoBasePath:
1455 assert(path_empty() && "Cast kind should not have a base path!");
1456 break;
1457 }
1458}
1459
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001460const char *CastExpr::getCastKindName() const {
1461 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001462 case CK_Dependent:
1463 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001464 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001465 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001466 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001467 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001468 case CK_LValueToRValue:
1469 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001470 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001471 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001472 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001473 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001474 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001475 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001476 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001477 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001478 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001479 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001480 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001481 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001482 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001483 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001484 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001485 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001486 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001487 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001488 case CK_NullToPointer:
1489 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001490 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001491 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001492 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001493 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001494 case CK_ReinterpretMemberPointer:
1495 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001496 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001497 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001498 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001499 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001500 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001501 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001502 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001503 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001504 case CK_PointerToBoolean:
1505 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001506 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001507 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001508 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001509 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001510 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001511 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001512 case CK_IntegralToBoolean:
1513 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001514 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001515 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001516 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001517 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001518 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001519 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001520 case CK_FloatingToBoolean:
1521 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001522 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001523 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001524 case CK_CPointerToObjCPointerCast:
1525 return "CPointerToObjCPointerCast";
1526 case CK_BlockPointerToObjCPointerCast:
1527 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001528 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001529 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001530 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001531 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001532 case CK_FloatingRealToComplex:
1533 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001534 case CK_FloatingComplexToReal:
1535 return "FloatingComplexToReal";
1536 case CK_FloatingComplexToBoolean:
1537 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001538 case CK_FloatingComplexCast:
1539 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001540 case CK_FloatingComplexToIntegralComplex:
1541 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001542 case CK_IntegralRealToComplex:
1543 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001544 case CK_IntegralComplexToReal:
1545 return "IntegralComplexToReal";
1546 case CK_IntegralComplexToBoolean:
1547 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001548 case CK_IntegralComplexCast:
1549 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001550 case CK_IntegralComplexToFloatingComplex:
1551 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001552 case CK_ARCConsumeObject:
1553 return "ARCConsumeObject";
1554 case CK_ARCProduceObject:
1555 return "ARCProduceObject";
1556 case CK_ARCReclaimReturnedObject:
1557 return "ARCReclaimReturnedObject";
1558 case CK_ARCExtendBlockObject:
1559 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001560 case CK_AtomicToNonAtomic:
1561 return "AtomicToNonAtomic";
1562 case CK_NonAtomicToAtomic:
1563 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001564 case CK_CopyAndAutoreleaseBlockObject:
1565 return "CopyAndAutoreleaseBlockObject";
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001566 case CK_BuiltinFnToFnPtr:
1567 return "BuiltinFnToFnPtr";
Guy Benyeie6b9d802013-01-20 12:31:11 +00001568 case CK_ZeroToOCLEvent:
1569 return "ZeroToOCLEvent";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001570 }
Mike Stump1eb44332009-09-09 15:08:12 +00001571
John McCall2bb5d002010-11-13 09:02:35 +00001572 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001573}
1574
Douglas Gregor6eef5192009-12-14 19:27:10 +00001575Expr *CastExpr::getSubExprAsWritten() {
1576 Expr *SubExpr = 0;
1577 CastExpr *E = this;
1578 do {
1579 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001580
1581 // Skip through reference binding to temporary.
1582 if (MaterializeTemporaryExpr *Materialize
1583 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1584 SubExpr = Materialize->GetTemporaryExpr();
1585
Douglas Gregor6eef5192009-12-14 19:27:10 +00001586 // Skip any temporary bindings; they're implicit.
1587 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1588 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001589
Douglas Gregor6eef5192009-12-14 19:27:10 +00001590 // Conversions by constructor and conversion functions have a
1591 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001592 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001593 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001594 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001595 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001596
Douglas Gregor6eef5192009-12-14 19:27:10 +00001597 // If the subexpression we're left with is an implicit cast, look
1598 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001599 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1600
Douglas Gregor6eef5192009-12-14 19:27:10 +00001601 return SubExpr;
1602}
1603
John McCallf871d0c2010-08-07 06:22:56 +00001604CXXBaseSpecifier **CastExpr::path_buffer() {
1605 switch (getStmtClass()) {
1606#define ABSTRACT_STMT(x)
1607#define CASTEXPR(Type, Base) \
1608 case Stmt::Type##Class: \
1609 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1610#define STMT(Type, Base)
1611#include "clang/AST/StmtNodes.inc"
1612 default:
1613 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001614 }
1615}
1616
1617void CastExpr::setCastPath(const CXXCastPath &Path) {
1618 assert(Path.size() == path_size());
1619 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1620}
1621
1622ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1623 CastKind Kind, Expr *Operand,
1624 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001625 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001626 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1627 void *Buffer =
1628 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1629 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001630 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001631 if (PathSize) E->setCastPath(*BasePath);
1632 return E;
1633}
1634
1635ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1636 unsigned PathSize) {
1637 void *Buffer =
1638 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1639 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1640}
1641
1642
1643CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001644 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001645 const CXXCastPath *BasePath,
1646 TypeSourceInfo *WrittenTy,
1647 SourceLocation L, SourceLocation R) {
1648 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1649 void *Buffer =
1650 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1651 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001652 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001653 if (PathSize) E->setCastPath(*BasePath);
1654 return E;
1655}
1656
1657CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1658 void *Buffer =
1659 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1660 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1661}
1662
Reid Spencer5f016e22007-07-11 17:01:13 +00001663/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1664/// corresponds to, e.g. "<<=".
David Blaikie0bea8632012-10-08 01:11:04 +00001665StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001667 case BO_PtrMemD: return ".*";
1668 case BO_PtrMemI: return "->*";
1669 case BO_Mul: return "*";
1670 case BO_Div: return "/";
1671 case BO_Rem: return "%";
1672 case BO_Add: return "+";
1673 case BO_Sub: return "-";
1674 case BO_Shl: return "<<";
1675 case BO_Shr: return ">>";
1676 case BO_LT: return "<";
1677 case BO_GT: return ">";
1678 case BO_LE: return "<=";
1679 case BO_GE: return ">=";
1680 case BO_EQ: return "==";
1681 case BO_NE: return "!=";
1682 case BO_And: return "&";
1683 case BO_Xor: return "^";
1684 case BO_Or: return "|";
1685 case BO_LAnd: return "&&";
1686 case BO_LOr: return "||";
1687 case BO_Assign: return "=";
1688 case BO_MulAssign: return "*=";
1689 case BO_DivAssign: return "/=";
1690 case BO_RemAssign: return "%=";
1691 case BO_AddAssign: return "+=";
1692 case BO_SubAssign: return "-=";
1693 case BO_ShlAssign: return "<<=";
1694 case BO_ShrAssign: return ">>=";
1695 case BO_AndAssign: return "&=";
1696 case BO_XorAssign: return "^=";
1697 case BO_OrAssign: return "|=";
1698 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001700
David Blaikie30263482012-01-20 21:50:17 +00001701 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001702}
1703
John McCall2de56d12010-08-25 11:45:40 +00001704BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001705BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1706 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001707 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001708 case OO_Plus: return BO_Add;
1709 case OO_Minus: return BO_Sub;
1710 case OO_Star: return BO_Mul;
1711 case OO_Slash: return BO_Div;
1712 case OO_Percent: return BO_Rem;
1713 case OO_Caret: return BO_Xor;
1714 case OO_Amp: return BO_And;
1715 case OO_Pipe: return BO_Or;
1716 case OO_Equal: return BO_Assign;
1717 case OO_Less: return BO_LT;
1718 case OO_Greater: return BO_GT;
1719 case OO_PlusEqual: return BO_AddAssign;
1720 case OO_MinusEqual: return BO_SubAssign;
1721 case OO_StarEqual: return BO_MulAssign;
1722 case OO_SlashEqual: return BO_DivAssign;
1723 case OO_PercentEqual: return BO_RemAssign;
1724 case OO_CaretEqual: return BO_XorAssign;
1725 case OO_AmpEqual: return BO_AndAssign;
1726 case OO_PipeEqual: return BO_OrAssign;
1727 case OO_LessLess: return BO_Shl;
1728 case OO_GreaterGreater: return BO_Shr;
1729 case OO_LessLessEqual: return BO_ShlAssign;
1730 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1731 case OO_EqualEqual: return BO_EQ;
1732 case OO_ExclaimEqual: return BO_NE;
1733 case OO_LessEqual: return BO_LE;
1734 case OO_GreaterEqual: return BO_GE;
1735 case OO_AmpAmp: return BO_LAnd;
1736 case OO_PipePipe: return BO_LOr;
1737 case OO_Comma: return BO_Comma;
1738 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001739 }
1740}
1741
1742OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1743 static const OverloadedOperatorKind OverOps[] = {
1744 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1745 OO_Star, OO_Slash, OO_Percent,
1746 OO_Plus, OO_Minus,
1747 OO_LessLess, OO_GreaterGreater,
1748 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1749 OO_EqualEqual, OO_ExclaimEqual,
1750 OO_Amp,
1751 OO_Caret,
1752 OO_Pipe,
1753 OO_AmpAmp,
1754 OO_PipePipe,
1755 OO_Equal, OO_StarEqual,
1756 OO_SlashEqual, OO_PercentEqual,
1757 OO_PlusEqual, OO_MinusEqual,
1758 OO_LessLessEqual, OO_GreaterGreaterEqual,
1759 OO_AmpEqual, OO_CaretEqual,
1760 OO_PipeEqual,
1761 OO_Comma
1762 };
1763 return OverOps[Opc];
1764}
1765
Ted Kremenek709210f2010-04-13 23:39:13 +00001766InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001767 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001768 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001769 false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001770 InitExprs(C, initExprs.size()),
Abramo Bagnara23700f02012-11-08 18:41:43 +00001771 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001772{
1773 sawArrayRangeDesignator(false);
1774 setInitializesStdInitializerList(false);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001775 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001776 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001777 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001778 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001779 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001780 if (initExprs[I]->isInstantiationDependent())
1781 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001782 if (initExprs[I]->containsUnexpandedParameterPack())
1783 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001784 }
Sean Huntc3021132010-05-05 15:23:54 +00001785
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001786 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001787}
Reid Spencer5f016e22007-07-11 17:01:13 +00001788
Ted Kremenek709210f2010-04-13 23:39:13 +00001789void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001790 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001791 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001792}
1793
Ted Kremenek709210f2010-04-13 23:39:13 +00001794void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001795 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001796}
1797
Ted Kremenek709210f2010-04-13 23:39:13 +00001798Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001799 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001800 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001801 InitExprs.back() = expr;
1802 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001803 }
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Douglas Gregor4c678342009-01-28 21:54:33 +00001805 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1806 InitExprs[Init] = expr;
1807 return Result;
1808}
1809
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001810void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001811 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001812 ArrayFillerOrUnionFieldInit = filler;
1813 // Fill out any "holes" in the array due to designated initializers.
1814 Expr **inits = getInits();
1815 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1816 if (inits[i] == 0)
1817 inits[i] = filler;
1818}
1819
Richard Smithfe587202012-04-15 02:50:59 +00001820bool InitListExpr::isStringLiteralInit() const {
1821 if (getNumInits() != 1)
1822 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001823 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1824 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smithfe587202012-04-15 02:50:59 +00001825 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001826 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smithfe587202012-04-15 02:50:59 +00001827 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1828}
1829
Erik Verbruggen65d78312012-12-25 14:51:39 +00001830SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara23700f02012-11-08 18:41:43 +00001831 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen65d78312012-12-25 14:51:39 +00001832 return SyntacticForm->getLocStart();
1833 SourceLocation Beg = LBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001834 if (Beg.isInvalid()) {
1835 // Find the first non-null initializer.
1836 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1837 E = InitExprs.end();
1838 I != E; ++I) {
1839 if (Stmt *S = *I) {
1840 Beg = S->getLocStart();
1841 break;
1842 }
1843 }
1844 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001845 return Beg;
1846}
1847
1848SourceLocation InitListExpr::getLocEnd() const {
1849 if (InitListExpr *SyntacticForm = getSyntacticForm())
1850 return SyntacticForm->getLocEnd();
1851 SourceLocation End = RBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001852 if (End.isInvalid()) {
1853 // Find the first non-null initializer from the end.
1854 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen65d78312012-12-25 14:51:39 +00001855 E = InitExprs.rend();
1856 I != E; ++I) {
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001857 if (Stmt *S = *I) {
Erik Verbruggen65d78312012-12-25 14:51:39 +00001858 End = S->getLocEnd();
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001859 break;
Erik Verbruggen65d78312012-12-25 14:51:39 +00001860 }
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001861 }
1862 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001863 return End;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001864}
1865
Steve Naroffbfdcae62008-09-04 15:31:07 +00001866/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001867///
John McCalla345edb2012-02-17 03:32:35 +00001868const FunctionProtoType *BlockExpr::getFunctionType() const {
1869 // The block pointer is never sugared, but the function type might be.
1870 return cast<BlockPointerType>(getType())
1871 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001872}
1873
Mike Stump1eb44332009-09-09 15:08:12 +00001874SourceLocation BlockExpr::getCaretLocation() const {
1875 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001876}
Mike Stump1eb44332009-09-09 15:08:12 +00001877const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001878 return TheBlock->getBody();
1879}
Mike Stump1eb44332009-09-09 15:08:12 +00001880Stmt *BlockExpr::getBody() {
1881 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001882}
Steve Naroff56ee6892008-10-08 17:01:13 +00001883
1884
Reid Spencer5f016e22007-07-11 17:01:13 +00001885//===----------------------------------------------------------------------===//
1886// Generic Expression Routines
1887//===----------------------------------------------------------------------===//
1888
Chris Lattner026dc962009-02-14 07:37:35 +00001889/// isUnusedResultAWarning - Return true if this immediate expression should
1890/// be warned about if the result is unused. If so, fill in Loc and Ranges
1891/// with location to warn on and the source range[s] to report with the
1892/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001893bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1894 SourceRange &R1, SourceRange &R2,
1895 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001896 // Don't warn if the expr is type dependent. The type could end up
1897 // instantiating to void.
1898 if (isTypeDependent())
1899 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Reid Spencer5f016e22007-07-11 17:01:13 +00001901 switch (getStmtClass()) {
1902 default:
John McCall0faede62010-03-12 07:11:26 +00001903 if (getType()->isVoidType())
1904 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001905 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001906 Loc = getExprLoc();
1907 R1 = getSourceRange();
1908 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001909 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001910 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001911 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001912 case GenericSelectionExprClass:
1913 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001914 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001915 case UnaryOperatorClass: {
1916 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001919 case UO_Plus:
1920 case UO_Minus:
1921 case UO_AddrOf:
1922 case UO_Not:
1923 case UO_LNot:
1924 case UO_Deref:
1925 break;
John McCall2de56d12010-08-25 11:45:40 +00001926 case UO_PostInc:
1927 case UO_PostDec:
1928 case UO_PreInc:
1929 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001930 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001931 case UO_Real:
1932 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001934 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1935 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001936 return false;
1937 break;
John McCall2de56d12010-08-25 11:45:40 +00001938 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001939 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001940 }
Eli Friedmana6115062012-05-24 00:47:05 +00001941 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001942 Loc = UO->getOperatorLoc();
1943 R1 = UO->getSubExpr()->getSourceRange();
1944 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001945 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001946 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001947 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001948 switch (BO->getOpcode()) {
1949 default:
1950 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001951 // Consider the RHS of comma for side effects. LHS was checked by
1952 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001953 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001954 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1955 // lvalue-ness) of an assignment written in a macro.
1956 if (IntegerLiteral *IE =
1957 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1958 if (IE->getValue() == 0)
1959 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001960 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001961 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001962 case BO_LAnd:
1963 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001964 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1965 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001966 return false;
1967 break;
John McCallbf0ee352010-02-16 04:10:53 +00001968 }
Chris Lattner026dc962009-02-14 07:37:35 +00001969 if (BO->isAssignmentOp())
1970 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001971 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001972 Loc = BO->getOperatorLoc();
1973 R1 = BO->getLHS()->getSourceRange();
1974 R2 = BO->getRHS()->getSourceRange();
1975 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001976 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001977 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001978 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001979 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001980 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001981
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001982 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001983 // If only one of the LHS or RHS is a warning, the operator might
1984 // be being used for control flow. Only warn if both the LHS and
1985 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001986 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001987 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001988 return false;
1989 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001990 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00001991 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001992 }
1993
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001995 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001996 Loc = cast<MemberExpr>(this)->getMemberLoc();
1997 R1 = SourceRange(Loc, Loc);
1998 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1999 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Reid Spencer5f016e22007-07-11 17:01:13 +00002001 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002002 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002003 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2004 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2005 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2006 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00002007
Chandler Carruth9b106832011-08-17 09:49:44 +00002008 case CXXOperatorCallExprClass: {
2009 // We warn about operator== and operator!= even when user-defined operator
2010 // overloads as there is no reasonable way to define these such that they
2011 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2012 // warning: these operators are commonly typo'ed, and so warning on them
2013 // provides additional value as well. If this list is updated,
2014 // DiagnoseUnusedComparison should be as well.
2015 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2016 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002017 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00002018 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002019 Loc = Op->getOperatorLoc();
2020 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00002021 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002022 }
Chandler Carruth9b106832011-08-17 09:49:44 +00002023
2024 // Fallthrough for generic call handling.
2025 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002026 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002027 case CXXMemberCallExprClass:
2028 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002029 // If this is a direct call, get the callee.
2030 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00002031 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00002032 // If the callee has attribute pure, const, or warn_unused_result, warn
2033 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002034 //
2035 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2036 // updated to match for QoI.
2037 if (FD->getAttr<WarnUnusedResultAttr>() ||
2038 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002039 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002040 Loc = CE->getCallee()->getLocStart();
2041 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002043 if (unsigned NumArgs = CE->getNumArgs())
2044 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2045 CE->getArg(NumArgs-1)->getLocEnd());
2046 return true;
2047 }
Chris Lattner026dc962009-02-14 07:37:35 +00002048 }
2049 return false;
2050 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002051
Matt Beaumont-Gay84c3b972012-10-23 06:15:26 +00002052 // If we don't know precisely what we're looking at, let's not warn.
2053 case UnresolvedLookupExprClass:
2054 case CXXUnresolvedConstructExprClass:
2055 return false;
2056
Anders Carlsson58beed92009-11-17 17:11:23 +00002057 case CXXTemporaryObjectExprClass:
2058 case CXXConstructExprClass:
2059 return false;
2060
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002061 case ObjCMessageExprClass: {
2062 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00002063 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002064 ME->isInstanceMessage() &&
2065 !ME->getType()->isVoidType() &&
2066 ME->getSelector().getIdentifierInfoForSlot(0) &&
2067 ME->getSelector().getIdentifierInfoForSlot(0)
2068 ->getName().startswith("init")) {
Eli Friedmana6115062012-05-24 00:47:05 +00002069 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00002070 Loc = getExprLoc();
2071 R1 = ME->getSourceRange();
2072 return true;
2073 }
2074
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002075 const ObjCMethodDecl *MD = ME->getMethodDecl();
2076 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002077 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002078 Loc = getExprLoc();
2079 return true;
2080 }
Chris Lattner026dc962009-02-14 07:37:35 +00002081 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002082 }
Mike Stump1eb44332009-09-09 15:08:12 +00002083
John McCall12f78a62010-12-02 01:19:52 +00002084 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002085 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00002086 Loc = getExprLoc();
2087 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00002088 return true;
John McCall12f78a62010-12-02 01:19:52 +00002089
John McCall4b9c2d22011-11-06 09:01:30 +00002090 case PseudoObjectExprClass: {
2091 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2092
2093 // Only complain about things that have the form of a getter.
2094 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2095 isa<BinaryOperator>(PO->getSyntacticForm()))
2096 return false;
2097
Eli Friedmana6115062012-05-24 00:47:05 +00002098 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00002099 Loc = getExprLoc();
2100 R1 = getSourceRange();
2101 return true;
2102 }
2103
Chris Lattner611b2ec2008-07-26 19:51:01 +00002104 case StmtExprClass: {
2105 // Statement exprs don't logically have side effects themselves, but are
2106 // sometimes used in macros in ways that give them a type that is unused.
2107 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2108 // however, if the result of the stmt expr is dead, we don't want to emit a
2109 // warning.
2110 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002111 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002112 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002113 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002114 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2115 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002116 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002117 }
Mike Stump1eb44332009-09-09 15:08:12 +00002118
John McCall0faede62010-03-12 07:11:26 +00002119 if (getType()->isVoidType())
2120 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002121 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002122 Loc = cast<StmtExpr>(this)->getLParenLoc();
2123 R1 = getSourceRange();
2124 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002125 }
Eli Friedman63199172012-09-24 23:02:26 +00002126 case CXXFunctionalCastExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002127 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002128 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002129 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002130 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002131 if (CE->getCastKind() == CK_ToVoid) {
2132 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002133 CE->getSubExpr()->getType().isVolatileQualified()) {
2134 const DeclRefExpr *DRE =
2135 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2136 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2137 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2138 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2139 R1, R2, Ctx);
2140 }
2141 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002142 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002143 }
Eli Friedman4059da82012-05-24 21:05:41 +00002144
Eli Friedmana6115062012-05-24 00:47:05 +00002145 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002146 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002147 if (CE->getCastKind() == CK_ConstructorConversion)
2148 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002149
Eli Friedmana6115062012-05-24 00:47:05 +00002150 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002151 if (const CXXFunctionalCastExpr *CXXCE =
2152 dyn_cast<CXXFunctionalCastExpr>(this)) {
2153 Loc = CXXCE->getTypeBeginLoc();
2154 R1 = CXXCE->getSubExpr()->getSourceRange();
2155 } else {
2156 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2157 Loc = CStyleCE->getLParenLoc();
2158 R1 = CStyleCE->getSubExpr()->getSourceRange();
2159 }
Chris Lattner026dc962009-02-14 07:37:35 +00002160 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002161 }
Eli Friedmana6115062012-05-24 00:47:05 +00002162 case ImplicitCastExprClass: {
2163 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002164
Eli Friedmana6115062012-05-24 00:47:05 +00002165 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2166 if (ICE->getCastKind() == CK_LValueToRValue &&
2167 ICE->getSubExpr()->getType().isVolatileQualified())
2168 return false;
2169
2170 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2171 }
Chris Lattner04421082008-04-08 04:40:51 +00002172 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002173 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002174 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002175
2176 case CXXNewExprClass:
2177 // FIXME: In theory, there might be new expressions that don't have side
2178 // effects (e.g. a placement new with an uninitialized POD).
2179 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002180 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002181 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002182 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002183 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002184 case ExprWithCleanupsClass:
2185 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002186 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002187 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002188}
2189
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002190/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002191/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002192bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002193 const Expr *E = IgnoreParens();
2194 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002195 default:
2196 return false;
2197 case ObjCIvarRefExprClass:
2198 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002199 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002200 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002201 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002202 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002203 case MaterializeTemporaryExprClass:
2204 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2205 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002206 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002207 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002208 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002209 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002210
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002211 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2212 if (VD->hasGlobalStorage())
2213 return true;
2214 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002215 // dereferencing to a pointer is always a gc'able candidate,
2216 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002217 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002218 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002219 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002220 return false;
2221 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002222 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002223 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002224 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002225 }
2226 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002227 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002228 }
2229}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002230
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002231bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2232 if (isTypeDependent())
2233 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002234 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002235}
2236
John McCall864c0412011-04-26 20:42:42 +00002237QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002238 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002239
2240 // Bound member expressions are always one of these possibilities:
2241 // x->m x.m x->*y x.*y
2242 // (possibly parenthesized)
2243
2244 expr = expr->IgnoreParens();
2245 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2246 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2247 return mem->getMemberDecl()->getType();
2248 }
2249
2250 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2251 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2252 ->getPointeeType();
2253 assert(type->isFunctionType());
2254 return type;
2255 }
2256
2257 assert(isa<UnresolvedMemberExpr>(expr));
2258 return QualType();
2259}
2260
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002261Expr* Expr::IgnoreParens() {
2262 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002263 while (true) {
2264 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2265 E = P->getSubExpr();
2266 continue;
2267 }
2268 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2269 if (P->getOpcode() == UO_Extension) {
2270 E = P->getSubExpr();
2271 continue;
2272 }
2273 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002274 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2275 if (!P->isResultDependent()) {
2276 E = P->getResultExpr();
2277 continue;
2278 }
2279 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002280 return E;
2281 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002282}
2283
Chris Lattner56f34942008-02-13 01:02:39 +00002284/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2285/// or CastExprs or ImplicitCastExprs, returning their operand.
2286Expr *Expr::IgnoreParenCasts() {
2287 Expr *E = this;
2288 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002289 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002290 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002291 continue;
2292 }
2293 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002294 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002295 continue;
2296 }
2297 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2298 if (P->getOpcode() == UO_Extension) {
2299 E = P->getSubExpr();
2300 continue;
2301 }
2302 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002303 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2304 if (!P->isResultDependent()) {
2305 E = P->getResultExpr();
2306 continue;
2307 }
2308 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002309 if (MaterializeTemporaryExpr *Materialize
2310 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2311 E = Materialize->GetTemporaryExpr();
2312 continue;
2313 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002314 if (SubstNonTypeTemplateParmExpr *NTTP
2315 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2316 E = NTTP->getReplacement();
2317 continue;
2318 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002319 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002320 }
2321}
2322
John McCall9c5d70c2010-12-04 08:24:19 +00002323/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2324/// casts. This is intended purely as a temporary workaround for code
2325/// that hasn't yet been rewritten to do the right thing about those
2326/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002327Expr *Expr::IgnoreParenLValueCasts() {
2328 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002329 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002330 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2331 E = P->getSubExpr();
2332 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002333 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002334 if (P->getCastKind() == CK_LValueToRValue) {
2335 E = P->getSubExpr();
2336 continue;
2337 }
John McCall9c5d70c2010-12-04 08:24:19 +00002338 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2339 if (P->getOpcode() == UO_Extension) {
2340 E = P->getSubExpr();
2341 continue;
2342 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002343 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2344 if (!P->isResultDependent()) {
2345 E = P->getResultExpr();
2346 continue;
2347 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002348 } else if (MaterializeTemporaryExpr *Materialize
2349 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2350 E = Materialize->GetTemporaryExpr();
2351 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002352 } else if (SubstNonTypeTemplateParmExpr *NTTP
2353 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2354 E = NTTP->getReplacement();
2355 continue;
John McCallf6a16482010-12-04 03:47:34 +00002356 }
2357 break;
2358 }
2359 return E;
2360}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002361
2362Expr *Expr::ignoreParenBaseCasts() {
2363 Expr *E = this;
2364 while (true) {
2365 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2366 E = P->getSubExpr();
2367 continue;
2368 }
2369 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2370 if (CE->getCastKind() == CK_DerivedToBase ||
2371 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2372 CE->getCastKind() == CK_NoOp) {
2373 E = CE->getSubExpr();
2374 continue;
2375 }
2376 }
2377
2378 return E;
2379 }
2380}
2381
John McCall2fc46bf2010-05-05 22:59:52 +00002382Expr *Expr::IgnoreParenImpCasts() {
2383 Expr *E = this;
2384 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002385 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002386 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002387 continue;
2388 }
2389 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002390 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002391 continue;
2392 }
2393 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2394 if (P->getOpcode() == UO_Extension) {
2395 E = P->getSubExpr();
2396 continue;
2397 }
2398 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002399 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2400 if (!P->isResultDependent()) {
2401 E = P->getResultExpr();
2402 continue;
2403 }
2404 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002405 if (MaterializeTemporaryExpr *Materialize
2406 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2407 E = Materialize->GetTemporaryExpr();
2408 continue;
2409 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002410 if (SubstNonTypeTemplateParmExpr *NTTP
2411 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2412 E = NTTP->getReplacement();
2413 continue;
2414 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002415 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002416 }
2417}
2418
Hans Wennborg2f072b42011-06-09 17:06:51 +00002419Expr *Expr::IgnoreConversionOperator() {
2420 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002421 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002422 return MCE->getImplicitObjectArgument();
2423 }
2424 return this;
2425}
2426
Chris Lattnerecdd8412009-03-13 17:28:01 +00002427/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2428/// value (including ptr->int casts of the same size). Strip off any
2429/// ParenExpr or CastExprs, returning their operand.
2430Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2431 Expr *E = this;
2432 while (true) {
2433 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2434 E = P->getSubExpr();
2435 continue;
2436 }
Mike Stump1eb44332009-09-09 15:08:12 +00002437
Chris Lattnerecdd8412009-03-13 17:28:01 +00002438 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2439 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002440 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002441 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002442
Chris Lattnerecdd8412009-03-13 17:28:01 +00002443 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2444 E = SE;
2445 continue;
2446 }
Mike Stump1eb44332009-09-09 15:08:12 +00002447
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002448 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002449 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002450 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002451 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002452 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2453 E = SE;
2454 continue;
2455 }
2456 }
Mike Stump1eb44332009-09-09 15:08:12 +00002457
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002458 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2459 if (P->getOpcode() == UO_Extension) {
2460 E = P->getSubExpr();
2461 continue;
2462 }
2463 }
2464
Peter Collingbournef111d932011-04-15 00:35:48 +00002465 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2466 if (!P->isResultDependent()) {
2467 E = P->getResultExpr();
2468 continue;
2469 }
2470 }
2471
Douglas Gregorc0244c52011-09-08 17:56:33 +00002472 if (SubstNonTypeTemplateParmExpr *NTTP
2473 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2474 E = NTTP->getReplacement();
2475 continue;
2476 }
2477
Chris Lattnerecdd8412009-03-13 17:28:01 +00002478 return E;
2479 }
2480}
2481
Douglas Gregor6eef5192009-12-14 19:27:10 +00002482bool Expr::isDefaultArgument() const {
2483 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002484 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2485 E = M->GetTemporaryExpr();
2486
Douglas Gregor6eef5192009-12-14 19:27:10 +00002487 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2488 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002489
Douglas Gregor6eef5192009-12-14 19:27:10 +00002490 return isa<CXXDefaultArgExpr>(E);
2491}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002492
Douglas Gregor2f599792010-04-02 18:24:57 +00002493/// \brief Skip over any no-op casts and any temporary-binding
2494/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002495static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002496 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2497 E = M->GetTemporaryExpr();
2498
Douglas Gregor2f599792010-04-02 18:24:57 +00002499 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002500 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002501 E = ICE->getSubExpr();
2502 else
2503 break;
2504 }
2505
2506 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2507 E = BE->getSubExpr();
2508
2509 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002510 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002511 E = ICE->getSubExpr();
2512 else
2513 break;
2514 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002515
2516 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002517}
2518
John McCall558d2ab2010-09-15 10:14:12 +00002519/// isTemporaryObject - Determines if this expression produces a
2520/// temporary of the given class type.
2521bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2522 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2523 return false;
2524
Anders Carlssonf8b30152010-11-28 16:40:49 +00002525 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002526
John McCall58277b52010-09-15 20:59:13 +00002527 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002528 if (!E->Classify(C).isPRValue()) {
2529 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002530 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002531 return false;
2532 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002533
John McCall19e60ad2010-09-16 06:57:56 +00002534 // Black-list a few cases which yield pr-values of class type that don't
2535 // refer to temporaries of that type:
2536
2537 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002538 if (isa<ImplicitCastExpr>(E)) {
2539 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2540 case CK_DerivedToBase:
2541 case CK_UncheckedDerivedToBase:
2542 return false;
2543 default:
2544 break;
2545 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002546 }
2547
John McCall19e60ad2010-09-16 06:57:56 +00002548 // - member expressions (all)
2549 if (isa<MemberExpr>(E))
2550 return false;
2551
Eli Friedman32f498a2012-06-15 23:51:06 +00002552 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2553 if (BO->isPtrMemOp())
2554 return false;
2555
John McCall56ca35d2011-02-17 10:25:35 +00002556 // - opaque values (all)
2557 if (isa<OpaqueValueExpr>(E))
2558 return false;
2559
John McCall558d2ab2010-09-15 10:14:12 +00002560 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002561}
2562
Douglas Gregor75e85042011-03-02 21:06:53 +00002563bool Expr::isImplicitCXXThis() const {
2564 const Expr *E = this;
2565
2566 // Strip away parentheses and casts we don't care about.
2567 while (true) {
2568 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2569 E = Paren->getSubExpr();
2570 continue;
2571 }
2572
2573 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2574 if (ICE->getCastKind() == CK_NoOp ||
2575 ICE->getCastKind() == CK_LValueToRValue ||
2576 ICE->getCastKind() == CK_DerivedToBase ||
2577 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2578 E = ICE->getSubExpr();
2579 continue;
2580 }
2581 }
2582
2583 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2584 if (UnOp->getOpcode() == UO_Extension) {
2585 E = UnOp->getSubExpr();
2586 continue;
2587 }
2588 }
2589
Douglas Gregor03e80032011-06-21 17:03:29 +00002590 if (const MaterializeTemporaryExpr *M
2591 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2592 E = M->GetTemporaryExpr();
2593 continue;
2594 }
2595
Douglas Gregor75e85042011-03-02 21:06:53 +00002596 break;
2597 }
2598
2599 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2600 return This->isImplicit();
2601
2602 return false;
2603}
2604
Douglas Gregor898574e2008-12-05 23:32:09 +00002605/// hasAnyTypeDependentArguments - Determines if any of the expressions
2606/// in Exprs is type-dependent.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002607bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00002608 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002609 if (Exprs[I]->isTypeDependent())
2610 return true;
2611
2612 return false;
2613}
2614
John McCall4204f072010-08-02 21:13:48 +00002615bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002616 // This function is attempting whether an expression is an initializer
2617 // which can be evaluated at compile-time. isEvaluatable handles most
2618 // of the cases, but it can't deal with some initializer-specific
2619 // expressions, and it can't deal with aggregates; we deal with those here,
2620 // and fall back to isEvaluatable for the other cases.
2621
John McCall4204f072010-08-02 21:13:48 +00002622 // If we ever capture reference-binding directly in the AST, we can
2623 // kill the second parameter.
2624
2625 if (IsForRef) {
2626 EvalResult Result;
2627 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2628 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002629
Anders Carlssone8a32b82008-11-24 05:23:59 +00002630 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002631 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002632 case IntegerLiteralClass:
2633 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002634 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002635 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002636 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002637 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002638 case CXXTemporaryObjectExprClass:
2639 case CXXConstructExprClass: {
2640 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002641
2642 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002643 if (CE->getConstructor()->isTrivial()) {
2644 // 1) an application of the trivial default constructor or
2645 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002646
Richard Smith180f4792011-11-10 06:34:14 +00002647 // 2) an elidable trivial copy construction of an operand which is
2648 // itself a constant initializer. Note that we consider the
2649 // operand on its own, *not* as a reference binding.
2650 if (CE->isElidable() &&
2651 CE->getArg(0)->isConstantInitializer(Ctx, false))
2652 return true;
2653 }
2654
2655 // 3) a foldable constexpr constructor.
2656 break;
John McCallb4b9b152010-08-01 21:51:45 +00002657 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002658 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002659 // This handles gcc's extension that allows global initializers like
2660 // "struct x {int x;} x = (struct x) {};".
2661 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002662 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002663 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002664 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002665 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002666 // FIXME: This doesn't deal with fields with reference types correctly.
2667 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2668 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002669 const InitListExpr *Exp = cast<InitListExpr>(this);
2670 unsigned numInits = Exp->getNumInits();
2671 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002672 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002673 return false;
2674 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002675 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002676 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002677 case ImplicitValueInitExprClass:
2678 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002679 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002680 return cast<ParenExpr>(this)->getSubExpr()
2681 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002682 case GenericSelectionExprClass:
2683 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2684 return false;
2685 return cast<GenericSelectionExpr>(this)->getResultExpr()
2686 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002687 case ChooseExprClass:
2688 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2689 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002690 case UnaryOperatorClass: {
2691 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002692 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002693 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002694 break;
2695 }
John McCall4204f072010-08-02 21:13:48 +00002696 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002697 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002698 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002699 case CStyleCastExprClass: {
2700 const CastExpr *CE = cast<CastExpr>(this);
2701
David Chisnall7a7ee302012-01-16 17:27:18 +00002702 // If we're promoting an integer to an _Atomic type then this is constant
2703 // if the integer is constant. We also need to check the converse in case
2704 // someone does something like:
2705 //
2706 // int a = (_Atomic(int))42;
2707 //
2708 // I doubt anyone would write code like this directly, but it's quite
2709 // possible as the result of macro expansions.
2710 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2711 CE->getCastKind() == CK_AtomicToNonAtomic)
2712 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2713
Richard Smithd62ca372011-12-06 22:44:34 +00002714 // Handle bitcasts of vector constants.
2715 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2716 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2717
Eli Friedman6bd97192011-12-21 00:43:02 +00002718 // Handle misc casts we want to ignore.
2719 // FIXME: Is it really safe to ignore all these?
2720 if (CE->getCastKind() == CK_NoOp ||
2721 CE->getCastKind() == CK_LValueToRValue ||
2722 CE->getCastKind() == CK_ToUnion ||
2723 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002724 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2725
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002726 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002727 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002728 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002729 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002730 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002731 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002732 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002733}
2734
Richard Smith8ae4ec22012-08-07 04:16:51 +00002735bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2736 if (isInstantiationDependent())
2737 return true;
2738
2739 switch (getStmtClass()) {
2740 case NoStmtClass:
2741 #define ABSTRACT_STMT(Type)
2742 #define STMT(Type, Base) case Type##Class:
2743 #define EXPR(Type, Base)
2744 #include "clang/AST/StmtNodes.inc"
2745 llvm_unreachable("unexpected Expr kind");
2746
2747 case DependentScopeDeclRefExprClass:
2748 case CXXUnresolvedConstructExprClass:
2749 case CXXDependentScopeMemberExprClass:
2750 case UnresolvedLookupExprClass:
2751 case UnresolvedMemberExprClass:
2752 case PackExpansionExprClass:
2753 case SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +00002754 case FunctionParmPackExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002755 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2756
Richard Smith60b70382012-08-07 05:18:29 +00002757 case DeclRefExprClass:
2758 case ObjCIvarRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002759 case PredefinedExprClass:
2760 case IntegerLiteralClass:
2761 case FloatingLiteralClass:
2762 case ImaginaryLiteralClass:
2763 case StringLiteralClass:
2764 case CharacterLiteralClass:
2765 case OffsetOfExprClass:
2766 case ImplicitValueInitExprClass:
2767 case UnaryExprOrTypeTraitExprClass:
2768 case AddrLabelExprClass:
2769 case GNUNullExprClass:
2770 case CXXBoolLiteralExprClass:
2771 case CXXNullPtrLiteralExprClass:
2772 case CXXThisExprClass:
2773 case CXXScalarValueInitExprClass:
2774 case TypeTraitExprClass:
2775 case UnaryTypeTraitExprClass:
2776 case BinaryTypeTraitExprClass:
2777 case ArrayTypeTraitExprClass:
2778 case ExpressionTraitExprClass:
2779 case CXXNoexceptExprClass:
2780 case SizeOfPackExprClass:
2781 case ObjCStringLiteralClass:
2782 case ObjCEncodeExprClass:
2783 case ObjCBoolLiteralExprClass:
2784 case CXXUuidofExprClass:
2785 case OpaqueValueExprClass:
2786 // These never have a side-effect.
2787 return false;
2788
2789 case CallExprClass:
2790 case CompoundAssignOperatorClass:
2791 case VAArgExprClass:
2792 case AtomicExprClass:
2793 case StmtExprClass:
2794 case CXXOperatorCallExprClass:
2795 case CXXMemberCallExprClass:
2796 case UserDefinedLiteralClass:
2797 case CXXThrowExprClass:
2798 case CXXNewExprClass:
2799 case CXXDeleteExprClass:
2800 case ExprWithCleanupsClass:
2801 case CXXBindTemporaryExprClass:
2802 case BlockExprClass:
2803 case CUDAKernelCallExprClass:
2804 // These always have a side-effect.
2805 return true;
2806
2807 case ParenExprClass:
2808 case ArraySubscriptExprClass:
2809 case MemberExprClass:
2810 case ConditionalOperatorClass:
2811 case BinaryConditionalOperatorClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002812 case CompoundLiteralExprClass:
2813 case ExtVectorElementExprClass:
2814 case DesignatedInitExprClass:
2815 case ParenListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002816 case CXXPseudoDestructorExprClass:
2817 case SubstNonTypeTemplateParmExprClass:
2818 case MaterializeTemporaryExprClass:
2819 case ShuffleVectorExprClass:
2820 case AsTypeExprClass:
2821 // These have a side-effect if any subexpression does.
2822 break;
2823
Richard Smith60b70382012-08-07 05:18:29 +00002824 case UnaryOperatorClass:
2825 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002826 return true;
2827 break;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002828
2829 case BinaryOperatorClass:
2830 if (cast<BinaryOperator>(this)->isAssignmentOp())
2831 return true;
2832 break;
2833
Richard Smith8ae4ec22012-08-07 04:16:51 +00002834 case InitListExprClass:
2835 // FIXME: The children for an InitListExpr doesn't include the array filler.
2836 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2837 if (E->HasSideEffects(Ctx))
2838 return true;
2839 break;
2840
2841 case GenericSelectionExprClass:
2842 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2843 HasSideEffects(Ctx);
2844
2845 case ChooseExprClass:
2846 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->HasSideEffects(Ctx);
2847
2848 case CXXDefaultArgExprClass:
2849 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2850
2851 case CXXDynamicCastExprClass: {
2852 // A dynamic_cast expression has side-effects if it can throw.
2853 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2854 if (DCE->getTypeAsWritten()->isReferenceType() &&
2855 DCE->getCastKind() == CK_Dynamic)
2856 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002857 } // Fall through.
2858 case ImplicitCastExprClass:
2859 case CStyleCastExprClass:
2860 case CXXStaticCastExprClass:
2861 case CXXReinterpretCastExprClass:
2862 case CXXConstCastExprClass:
2863 case CXXFunctionalCastExprClass: {
2864 const CastExpr *CE = cast<CastExpr>(this);
2865 if (CE->getCastKind() == CK_LValueToRValue &&
2866 CE->getSubExpr()->getType().isVolatileQualified())
2867 return true;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002868 break;
2869 }
2870
Richard Smith0d729102012-08-13 20:08:14 +00002871 case CXXTypeidExprClass:
2872 // typeid might throw if its subexpression is potentially-evaluated, so has
2873 // side-effects in that case whether or not its subexpression does.
2874 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith8ae4ec22012-08-07 04:16:51 +00002875
2876 case CXXConstructExprClass:
2877 case CXXTemporaryObjectExprClass: {
2878 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smith60b70382012-08-07 05:18:29 +00002879 if (!CE->getConstructor()->isTrivial())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002880 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002881 // A trivial constructor does not add any side-effects of its own. Just look
2882 // at its arguments.
Richard Smith8ae4ec22012-08-07 04:16:51 +00002883 break;
2884 }
2885
2886 case LambdaExprClass: {
2887 const LambdaExpr *LE = cast<LambdaExpr>(this);
2888 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2889 E = LE->capture_end(); I != E; ++I)
2890 if (I->getCaptureKind() == LCK_ByCopy)
2891 // FIXME: Only has a side-effect if the variable is volatile or if
2892 // the copy would invoke a non-trivial copy constructor.
2893 return true;
2894 return false;
2895 }
2896
2897 case PseudoObjectExprClass: {
2898 // Only look for side-effects in the semantic form, and look past
2899 // OpaqueValueExpr bindings in that form.
2900 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2901 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2902 E = PO->semantics_end();
2903 I != E; ++I) {
2904 const Expr *Subexpr = *I;
2905 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2906 Subexpr = OVE->getSourceExpr();
2907 if (Subexpr->HasSideEffects(Ctx))
2908 return true;
2909 }
2910 return false;
2911 }
2912
2913 case ObjCBoxedExprClass:
2914 case ObjCArrayLiteralClass:
2915 case ObjCDictionaryLiteralClass:
2916 case ObjCMessageExprClass:
2917 case ObjCSelectorExprClass:
2918 case ObjCProtocolExprClass:
2919 case ObjCPropertyRefExprClass:
2920 case ObjCIsaExprClass:
2921 case ObjCIndirectCopyRestoreExprClass:
2922 case ObjCSubscriptRefExprClass:
2923 case ObjCBridgedCastExprClass:
2924 // FIXME: Classify these cases better.
2925 return true;
2926 }
2927
2928 // Recurse to children.
2929 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2930 if (const Stmt *S = *SubStmts)
2931 if (cast<Expr>(S)->HasSideEffects(Ctx))
2932 return true;
2933
2934 return false;
2935}
2936
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002937namespace {
2938 /// \brief Look for a call to a non-trivial function within an expression.
2939 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2940 {
2941 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2942
2943 bool NonTrivial;
2944
2945 public:
2946 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002947 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002948
2949 bool hasNonTrivialCall() const { return NonTrivial; }
2950
2951 void VisitCallExpr(CallExpr *E) {
2952 if (CXXMethodDecl *Method
2953 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2954 if (Method->isTrivial()) {
2955 // Recurse to children of the call.
2956 Inherited::VisitStmt(E);
2957 return;
2958 }
2959 }
2960
2961 NonTrivial = true;
2962 }
2963
2964 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2965 if (E->getConstructor()->isTrivial()) {
2966 // Recurse to children of the call.
2967 Inherited::VisitStmt(E);
2968 return;
2969 }
2970
2971 NonTrivial = true;
2972 }
2973
2974 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2975 if (E->getTemporary()->getDestructor()->isTrivial()) {
2976 Inherited::VisitStmt(E);
2977 return;
2978 }
2979
2980 NonTrivial = true;
2981 }
2982 };
2983}
2984
2985bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2986 NonTrivialCallFinder Finder(Ctx);
2987 Finder.Visit(this);
2988 return Finder.hasNonTrivialCall();
2989}
2990
Chandler Carruth82214a82011-02-18 23:54:50 +00002991/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2992/// pointer constant or not, as well as the specific kind of constant detected.
2993/// Null pointer constants can be integer constant expressions with the
2994/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2995/// (a GNU extension).
2996Expr::NullPointerConstantKind
2997Expr::isNullPointerConstant(ASTContext &Ctx,
2998 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002999 if (isValueDependent()) {
3000 switch (NPC) {
3001 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00003002 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00003003 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003004 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie50800fc2012-08-08 17:33:31 +00003005 return NPCK_ZeroExpression;
Chandler Carruth82214a82011-02-18 23:54:50 +00003006 else
3007 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00003008
Douglas Gregorce940492009-09-25 04:25:58 +00003009 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003010 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00003011 }
3012 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00003013
Sebastian Redl07779722008-10-31 14:43:28 +00003014 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00003015 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003016 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00003017 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00003018 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00003019 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00003020 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00003021 Pointee->isVoidType() && // to void*
3022 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00003023 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00003024 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003025 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003026 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3027 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00003028 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00003029 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3030 // Accept ((void*)0) as a null pointer constant, as many other
3031 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00003032 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00003033 } else if (const GenericSelectionExpr *GE =
3034 dyn_cast<GenericSelectionExpr>(this)) {
3035 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00003036 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00003037 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00003038 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00003039 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003040 } else if (isa<GNUNullExpr>(this)) {
3041 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00003042 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00003043 } else if (const MaterializeTemporaryExpr *M
3044 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3045 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00003046 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3047 if (const Expr *Source = OVE->getSourceExpr())
3048 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00003049 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003050
Richard Smith4e24f0f2013-01-02 12:01:23 +00003051 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003052 if (getType()->isNullPtrType())
Richard Smith4e24f0f2013-01-02 12:01:23 +00003053 return NPCK_CXX11_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003054
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003055 if (const RecordType *UT = getType()->getAsUnionType())
3056 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
3057 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3058 const Expr *InitExpr = CLE->getInitializer();
3059 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3060 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3061 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003062 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00003063 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003064 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00003065 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00003066
Reid Spencer5f016e22007-07-11 17:01:13 +00003067 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00003068 // test for the value 0. Don't use the C++11 constant expression semantics
3069 // for this, for now; once the dust settles on core issue 903, we might only
3070 // allow a literal 0 here in C++11 mode.
Richard Smith80ad52f2013-01-02 11:42:31 +00003071 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith70488e22012-02-14 21:38:30 +00003072 if (!isCXX98IntegralConstantExpr(Ctx))
3073 return NPCK_NotNull;
3074 } else {
3075 if (!isIntegerConstantExpr(Ctx))
3076 return NPCK_NotNull;
3077 }
Chandler Carruth82214a82011-02-18 23:54:50 +00003078
David Blaikie50800fc2012-08-08 17:33:31 +00003079 if (EvaluateKnownConstInt(Ctx) != 0)
3080 return NPCK_NotNull;
3081
3082 if (isa<IntegerLiteral>(this))
3083 return NPCK_ZeroLiteral;
3084 return NPCK_ZeroExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00003085}
Steve Naroff31a45842007-07-28 23:10:27 +00003086
John McCallf6a16482010-12-04 03:47:34 +00003087/// \brief If this expression is an l-value for an Objective C
3088/// property, find the underlying property reference expression.
3089const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3090 const Expr *E = this;
3091 while (true) {
3092 assert((E->getValueKind() == VK_LValue &&
3093 E->getObjectKind() == OK_ObjCProperty) &&
3094 "expression is not a property reference");
3095 E = E->IgnoreParenCasts();
3096 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3097 if (BO->getOpcode() == BO_Comma) {
3098 E = BO->getRHS();
3099 continue;
3100 }
3101 }
3102
3103 break;
3104 }
3105
3106 return cast<ObjCPropertyRefExpr>(E);
3107}
3108
Anna Zaksbbff82f2012-10-01 20:34:04 +00003109bool Expr::isObjCSelfExpr() const {
3110 const Expr *E = IgnoreParenImpCasts();
3111
3112 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3113 if (!DRE)
3114 return false;
3115
3116 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3117 if (!Param)
3118 return false;
3119
3120 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3121 if (!M)
3122 return false;
3123
3124 return M->getSelfDecl() == Param;
3125}
3126
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003127FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00003128 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003129
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003130 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00003131 if (ICE->getCastKind() == CK_LValueToRValue ||
3132 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003133 E = ICE->getSubExpr()->IgnoreParens();
3134 else
3135 break;
3136 }
3137
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003138 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00003139 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003140 if (Field->isBitField())
3141 return Field;
3142
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00003143 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3144 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3145 if (Field->isBitField())
3146 return Field;
3147
Eli Friedman42068e92011-07-13 02:05:57 +00003148 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003149 if (BinOp->isAssignmentOp() && BinOp->getLHS())
3150 return BinOp->getLHS()->getBitField();
3151
Eli Friedman42068e92011-07-13 02:05:57 +00003152 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
3153 return BinOp->getRHS()->getBitField();
3154 }
3155
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003156 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003157}
3158
Anders Carlsson09380262010-01-31 17:18:49 +00003159bool Expr::refersToVectorElement() const {
3160 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00003161
Anders Carlsson09380262010-01-31 17:18:49 +00003162 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00003163 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00003164 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00003165 E = ICE->getSubExpr()->IgnoreParens();
3166 else
3167 break;
3168 }
Sean Huntc3021132010-05-05 15:23:54 +00003169
Anders Carlsson09380262010-01-31 17:18:49 +00003170 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3171 return ASE->getBase()->getType()->isVectorType();
3172
3173 if (isa<ExtVectorElementExpr>(E))
3174 return true;
3175
3176 return false;
3177}
3178
Chris Lattner2140e902009-02-16 22:14:05 +00003179/// isArrow - Return true if the base expression is a pointer to vector,
3180/// return false if the base expression is a vector.
3181bool ExtVectorElementExpr::isArrow() const {
3182 return getBase()->getType()->isPointerType();
3183}
3184
Nate Begeman213541a2008-04-18 23:10:10 +00003185unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003186 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003187 return VT->getNumElements();
3188 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003189}
3190
Nate Begeman8a997642008-05-09 06:41:27 +00003191/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003192bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003193 // FIXME: Refactor this code to an accessor on the AST node which returns the
3194 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003195 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003196
3197 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003198 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003199 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Nate Begeman190d6a22009-01-18 02:01:21 +00003201 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003202 if (Comp[0] == 's' || Comp[0] == 'S')
3203 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003204
Daniel Dunbar15027422009-10-17 23:53:04 +00003205 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003206 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003207 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003208
Steve Narofffec0b492007-07-30 03:29:09 +00003209 return false;
3210}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003211
Nate Begeman8a997642008-05-09 06:41:27 +00003212/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003213void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003214 SmallVectorImpl<unsigned> &Elts) const {
3215 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003216 if (Comp[0] == 's' || Comp[0] == 'S')
3217 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003218
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003219 bool isHi = Comp == "hi";
3220 bool isLo = Comp == "lo";
3221 bool isEven = Comp == "even";
3222 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003223
Nate Begeman8a997642008-05-09 06:41:27 +00003224 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3225 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003226
Nate Begeman8a997642008-05-09 06:41:27 +00003227 if (isHi)
3228 Index = e + i;
3229 else if (isLo)
3230 Index = i;
3231 else if (isEven)
3232 Index = 2 * i;
3233 else if (isOdd)
3234 Index = 2 * i + 1;
3235 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003236 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003237
Nate Begeman3b8d1162008-05-13 21:03:02 +00003238 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003239 }
Nate Begeman8a997642008-05-09 06:41:27 +00003240}
3241
Douglas Gregor04badcf2010-04-21 00:45:42 +00003242ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003243 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003244 SourceLocation LBracLoc,
3245 SourceLocation SuperLoc,
3246 bool IsInstanceSuper,
3247 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003248 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003249 ArrayRef<SourceLocation> SelLocs,
3250 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003251 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003252 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003253 SourceLocation RBracLoc,
3254 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003255 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003256 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003257 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003258 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003259 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3260 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003261 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003262 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3263 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003264{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003265 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003266 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003267}
3268
Douglas Gregor04badcf2010-04-21 00:45:42 +00003269ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003270 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003271 SourceLocation LBracLoc,
3272 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003273 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003274 ArrayRef<SourceLocation> SelLocs,
3275 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003276 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003277 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003278 SourceLocation RBracLoc,
3279 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003280 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003281 T->isDependentType(), T->isInstantiationDependentType(),
3282 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003283 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3284 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003285 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003286 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003287 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003288{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003289 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003290 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003291}
3292
Douglas Gregor04badcf2010-04-21 00:45:42 +00003293ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003294 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003295 SourceLocation LBracLoc,
3296 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003297 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003298 ArrayRef<SourceLocation> SelLocs,
3299 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003300 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003301 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003302 SourceLocation RBracLoc,
3303 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003304 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003305 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003306 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003307 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003308 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3309 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003310 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003311 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003312 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003313{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003314 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003315 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003316}
3317
3318void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3319 ArrayRef<SourceLocation> SelLocs,
3320 SelectorLocationsKind SelLocsK) {
3321 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003322 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003323 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003324 if (Args[I]->isTypeDependent())
3325 ExprBits.TypeDependent = true;
3326 if (Args[I]->isValueDependent())
3327 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003328 if (Args[I]->isInstantiationDependent())
3329 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003330 if (Args[I]->containsUnexpandedParameterPack())
3331 ExprBits.ContainsUnexpandedParameterPack = true;
3332
3333 MyArgs[I] = Args[I];
3334 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003335
Benjamin Kramer19562c92012-02-20 00:20:48 +00003336 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003337 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003338 if (SelLocsK == SelLoc_NonStandard)
3339 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3340 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003341}
3342
Douglas Gregor04badcf2010-04-21 00:45:42 +00003343ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003344 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003345 SourceLocation LBracLoc,
3346 SourceLocation SuperLoc,
3347 bool IsInstanceSuper,
3348 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003349 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003350 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003351 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003352 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003353 SourceLocation RBracLoc,
3354 bool isImplicit) {
3355 assert((!SelLocs.empty() || isImplicit) &&
3356 "No selector locs for non-implicit message");
3357 ObjCMessageExpr *Mem;
3358 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3359 if (isImplicit)
3360 Mem = alloc(Context, Args.size(), 0);
3361 else
3362 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003363 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003364 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003365 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003366}
3367
3368ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003369 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003370 SourceLocation LBracLoc,
3371 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003372 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003373 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003374 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003375 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003376 SourceLocation RBracLoc,
3377 bool isImplicit) {
3378 assert((!SelLocs.empty() || isImplicit) &&
3379 "No selector locs for non-implicit message");
3380 ObjCMessageExpr *Mem;
3381 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3382 if (isImplicit)
3383 Mem = alloc(Context, Args.size(), 0);
3384 else
3385 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003386 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003387 SelLocs, SelLocsK, Method, Args, RBracLoc,
3388 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003389}
3390
3391ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003392 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003393 SourceLocation LBracLoc,
3394 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003395 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003396 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003397 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003398 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003399 SourceLocation RBracLoc,
3400 bool isImplicit) {
3401 assert((!SelLocs.empty() || isImplicit) &&
3402 "No selector locs for non-implicit message");
3403 ObjCMessageExpr *Mem;
3404 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3405 if (isImplicit)
3406 Mem = alloc(Context, Args.size(), 0);
3407 else
3408 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003409 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003410 SelLocs, SelLocsK, Method, Args, RBracLoc,
3411 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003412}
3413
Sean Huntc3021132010-05-05 15:23:54 +00003414ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003415 unsigned NumArgs,
3416 unsigned NumStoredSelLocs) {
3417 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003418 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3419}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003420
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003421ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3422 ArrayRef<Expr *> Args,
3423 SourceLocation RBraceLoc,
3424 ArrayRef<SourceLocation> SelLocs,
3425 Selector Sel,
3426 SelectorLocationsKind &SelLocsK) {
3427 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3428 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3429 : 0;
3430 return alloc(C, Args.size(), NumStoredSelLocs);
3431}
3432
3433ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3434 unsigned NumArgs,
3435 unsigned NumStoredSelLocs) {
3436 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3437 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3438 return (ObjCMessageExpr *)C.Allocate(Size,
3439 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3440}
3441
3442void ObjCMessageExpr::getSelectorLocs(
3443 SmallVectorImpl<SourceLocation> &SelLocs) const {
3444 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3445 SelLocs.push_back(getSelectorLoc(i));
3446}
3447
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003448SourceRange ObjCMessageExpr::getReceiverRange() const {
3449 switch (getReceiverKind()) {
3450 case Instance:
3451 return getInstanceReceiver()->getSourceRange();
3452
3453 case Class:
3454 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3455
3456 case SuperInstance:
3457 case SuperClass:
3458 return getSuperLoc();
3459 }
3460
David Blaikie30263482012-01-20 21:50:17 +00003461 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003462}
3463
Douglas Gregor04badcf2010-04-21 00:45:42 +00003464Selector ObjCMessageExpr::getSelector() const {
3465 if (HasMethod)
3466 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3467 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003468 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003469}
3470
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003471QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor04badcf2010-04-21 00:45:42 +00003472 switch (getReceiverKind()) {
3473 case Instance:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003474 return getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003475 case Class:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003476 return getClassReceiver();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003477 case SuperInstance:
Douglas Gregor04badcf2010-04-21 00:45:42 +00003478 case SuperClass:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003479 return getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003480 }
3481
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003482 llvm_unreachable("unexpected receiver kind");
3483}
3484
3485ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3486 QualType T = getReceiverType();
3487
3488 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3489 return Ptr->getInterfaceDecl();
3490
3491 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3492 return Ty->getInterface();
3493
Douglas Gregor04badcf2010-04-21 00:45:42 +00003494 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003495}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003496
Chris Lattner5f9e2722011-07-23 10:55:15 +00003497StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003498 switch (getBridgeKind()) {
3499 case OBC_Bridge:
3500 return "__bridge";
3501 case OBC_BridgeTransfer:
3502 return "__bridge_transfer";
3503 case OBC_BridgeRetained:
3504 return "__bridge_retained";
3505 }
David Blaikie30263482012-01-20 21:50:17 +00003506
3507 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003508}
3509
Jay Foad4ba2a172011-01-12 09:06:06 +00003510bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003511 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003512}
3513
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003514ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003515 QualType Type, SourceLocation BLoc,
3516 SourceLocation RP)
3517 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3518 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003519 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003520 Type->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003521 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003522{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003523 SubExprs = new (C) Stmt*[args.size()];
3524 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003525 if (args[i]->isTypeDependent())
3526 ExprBits.TypeDependent = true;
3527 if (args[i]->isValueDependent())
3528 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003529 if (args[i]->isInstantiationDependent())
3530 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003531 if (args[i]->containsUnexpandedParameterPack())
3532 ExprBits.ContainsUnexpandedParameterPack = true;
3533
3534 SubExprs[i] = args[i];
3535 }
3536}
3537
Nate Begeman888376a2009-08-12 02:28:50 +00003538void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3539 unsigned NumExprs) {
3540 if (SubExprs) C.Deallocate(SubExprs);
3541
3542 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003543 this->NumExprs = NumExprs;
3544 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003545}
Nate Begeman888376a2009-08-12 02:28:50 +00003546
Peter Collingbournef111d932011-04-15 00:35:48 +00003547GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3548 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003549 ArrayRef<TypeSourceInfo*> AssocTypes,
3550 ArrayRef<Expr*> AssocExprs,
3551 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003552 SourceLocation RParenLoc,
3553 bool ContainsUnexpandedParameterPack,
3554 unsigned ResultIndex)
3555 : Expr(GenericSelectionExprClass,
3556 AssocExprs[ResultIndex]->getType(),
3557 AssocExprs[ResultIndex]->getValueKind(),
3558 AssocExprs[ResultIndex]->getObjectKind(),
3559 AssocExprs[ResultIndex]->isTypeDependent(),
3560 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003561 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003562 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003563 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3564 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3565 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3566 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003567 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003568 assert(AssocTypes.size() == AssocExprs.size());
3569 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3570 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003571}
3572
3573GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3574 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003575 ArrayRef<TypeSourceInfo*> AssocTypes,
3576 ArrayRef<Expr*> AssocExprs,
3577 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003578 SourceLocation RParenLoc,
3579 bool ContainsUnexpandedParameterPack)
3580 : Expr(GenericSelectionExprClass,
3581 Context.DependentTy,
3582 VK_RValue,
3583 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003584 /*isTypeDependent=*/true,
3585 /*isValueDependent=*/true,
3586 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003587 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003588 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3589 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3590 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3591 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003592 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003593 assert(AssocTypes.size() == AssocExprs.size());
3594 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3595 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003596}
3597
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003598//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003599// DesignatedInitExpr
3600//===----------------------------------------------------------------------===//
3601
Chandler Carruthb1138242011-06-16 06:47:06 +00003602IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003603 assert(Kind == FieldDesignator && "Only valid on a field designator");
3604 if (Field.NameOrField & 0x01)
3605 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3606 else
3607 return getField()->getIdentifier();
3608}
3609
Sean Huntc3021132010-05-05 15:23:54 +00003610DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003611 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003612 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003613 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003614 bool GNUSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003615 ArrayRef<Expr*> IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003616 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003617 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003618 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003619 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003620 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003621 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003622 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003623 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003624 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003625
3626 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003627 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003628 *Child++ = Init;
3629
3630 // Copy the designators and their subexpressions, computing
3631 // value-dependence along the way.
3632 unsigned IndexIdx = 0;
3633 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003634 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003635
3636 if (this->Designators[I].isArrayDesignator()) {
3637 // Compute type- and value-dependence.
3638 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003639 if (Index->isTypeDependent() || Index->isValueDependent())
3640 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003641 if (Index->isInstantiationDependent())
3642 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003643 // Propagate unexpanded parameter packs.
3644 if (Index->containsUnexpandedParameterPack())
3645 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003646
3647 // Copy the index expressions into permanent storage.
3648 *Child++ = IndexExprs[IndexIdx++];
3649 } else if (this->Designators[I].isArrayRangeDesignator()) {
3650 // Compute type- and value-dependence.
3651 Expr *Start = IndexExprs[IndexIdx];
3652 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003653 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003654 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003655 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003656 ExprBits.InstantiationDependent = true;
3657 } else if (Start->isInstantiationDependent() ||
3658 End->isInstantiationDependent()) {
3659 ExprBits.InstantiationDependent = true;
3660 }
3661
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003662 // Propagate unexpanded parameter packs.
3663 if (Start->containsUnexpandedParameterPack() ||
3664 End->containsUnexpandedParameterPack())
3665 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003666
3667 // Copy the start/end expressions into permanent storage.
3668 *Child++ = IndexExprs[IndexIdx++];
3669 *Child++ = IndexExprs[IndexIdx++];
3670 }
3671 }
3672
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003673 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003674}
3675
Douglas Gregor05c13a32009-01-22 00:58:24 +00003676DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003677DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003678 unsigned NumDesignators,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003679 ArrayRef<Expr*> IndexExprs,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003680 SourceLocation ColonOrEqualLoc,
3681 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003682 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003683 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003684 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003685 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003686 IndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003687}
3688
Mike Stump1eb44332009-09-09 15:08:12 +00003689DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003690 unsigned NumIndexExprs) {
3691 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3692 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3693 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3694}
3695
Douglas Gregor319d57f2010-01-06 23:17:19 +00003696void DesignatedInitExpr::setDesignators(ASTContext &C,
3697 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003698 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003699 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003700 NumDesignators = NumDesigs;
3701 for (unsigned I = 0; I != NumDesigs; ++I)
3702 Designators[I] = Desigs[I];
3703}
3704
Abramo Bagnara24f46742011-03-16 15:08:46 +00003705SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3706 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3707 if (size() == 1)
3708 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen65d78312012-12-25 14:51:39 +00003709 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3710 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara24f46742011-03-16 15:08:46 +00003711}
3712
Erik Verbruggen65d78312012-12-25 14:51:39 +00003713SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003714 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003715 Designator &First =
3716 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003717 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003718 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003719 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3720 else
3721 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3722 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003723 StartLoc =
3724 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen65d78312012-12-25 14:51:39 +00003725 return StartLoc;
3726}
3727
3728SourceLocation DesignatedInitExpr::getLocEnd() const {
3729 return getInit()->getLocEnd();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003730}
3731
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003732Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003733 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003734 char *Ptr = static_cast<char *>(
3735 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003736 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003737 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3738 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3739}
3740
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003741Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003742 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003743 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003744 char *Ptr = static_cast<char *>(
3745 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003746 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003747 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3748 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3749}
3750
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003751Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003752 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003753 "Requires array range designator");
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003754 char *Ptr = static_cast<char *>(
3755 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregor05c13a32009-01-22 00:58:24 +00003756 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003757 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3758 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3759}
3760
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003761/// \brief Replaces the designator at index @p Idx with the series
3762/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003763void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003764 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003765 const Designator *Last) {
3766 unsigned NumNewDesignators = Last - First;
3767 if (NumNewDesignators == 0) {
3768 std::copy_backward(Designators + Idx + 1,
3769 Designators + NumDesignators,
3770 Designators + Idx);
3771 --NumNewDesignators;
3772 return;
3773 } else if (NumNewDesignators == 1) {
3774 Designators[Idx] = *First;
3775 return;
3776 }
3777
Mike Stump1eb44332009-09-09 15:08:12 +00003778 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003779 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003780 std::copy(Designators, Designators + Idx, NewDesignators);
3781 std::copy(First, Last, NewDesignators + Idx);
3782 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3783 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003784 Designators = NewDesignators;
3785 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3786}
3787
Mike Stump1eb44332009-09-09 15:08:12 +00003788ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003789 ArrayRef<Expr*> exprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003790 SourceLocation rparenloc)
3791 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003792 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003793 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3794 Exprs = new (C) Stmt*[exprs.size()];
3795 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003796 if (exprs[i]->isTypeDependent())
3797 ExprBits.TypeDependent = true;
3798 if (exprs[i]->isValueDependent())
3799 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003800 if (exprs[i]->isInstantiationDependent())
3801 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003802 if (exprs[i]->containsUnexpandedParameterPack())
3803 ExprBits.ContainsUnexpandedParameterPack = true;
3804
Nate Begeman2ef13e52009-08-10 23:49:36 +00003805 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003806 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003807}
3808
John McCalle996ffd2011-02-16 08:02:54 +00003809const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3810 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3811 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003812 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3813 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003814 e = cast<CXXConstructExpr>(e)->getArg(0);
3815 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3816 e = ice->getSubExpr();
3817 return cast<OpaqueValueExpr>(e);
3818}
3819
John McCall4b9c2d22011-11-06 09:01:30 +00003820PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3821 unsigned numSemanticExprs) {
3822 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3823 (1 + numSemanticExprs) * sizeof(Expr*),
3824 llvm::alignOf<PseudoObjectExpr>());
3825 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3826}
3827
3828PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3829 : Expr(PseudoObjectExprClass, shell) {
3830 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3831}
3832
3833PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3834 ArrayRef<Expr*> semantics,
3835 unsigned resultIndex) {
3836 assert(syntax && "no syntactic expression!");
3837 assert(semantics.size() && "no semantic expressions!");
3838
3839 QualType type;
3840 ExprValueKind VK;
3841 if (resultIndex == NoResult) {
3842 type = C.VoidTy;
3843 VK = VK_RValue;
3844 } else {
3845 assert(resultIndex < semantics.size());
3846 type = semantics[resultIndex]->getType();
3847 VK = semantics[resultIndex]->getValueKind();
3848 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3849 }
3850
3851 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3852 (1 + semantics.size()) * sizeof(Expr*),
3853 llvm::alignOf<PseudoObjectExpr>());
3854 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3855 resultIndex);
3856}
3857
3858PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3859 Expr *syntax, ArrayRef<Expr*> semantics,
3860 unsigned resultIndex)
3861 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3862 /*filled in at end of ctor*/ false, false, false, false) {
3863 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3864 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3865
3866 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3867 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3868 getSubExprsBuffer()[i] = E;
3869
3870 if (E->isTypeDependent())
3871 ExprBits.TypeDependent = true;
3872 if (E->isValueDependent())
3873 ExprBits.ValueDependent = true;
3874 if (E->isInstantiationDependent())
3875 ExprBits.InstantiationDependent = true;
3876 if (E->containsUnexpandedParameterPack())
3877 ExprBits.ContainsUnexpandedParameterPack = true;
3878
3879 if (isa<OpaqueValueExpr>(E))
3880 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3881 "opaque-value semantic expressions for pseudo-object "
3882 "operations must have sources");
3883 }
3884}
3885
Douglas Gregor05c13a32009-01-22 00:58:24 +00003886//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003887// ExprIterator.
3888//===----------------------------------------------------------------------===//
3889
3890Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3891Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3892Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3893const Expr* ConstExprIterator::operator[](size_t idx) const {
3894 return cast<Expr>(I[idx]);
3895}
3896const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3897const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3898
3899//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003900// Child Iterators for iterating over subexpressions/substatements
3901//===----------------------------------------------------------------------===//
3902
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003903// UnaryExprOrTypeTraitExpr
3904Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003905 // If this is of a type and the type is a VLA type (and not a typedef), the
3906 // size expression of the VLA needs to be treated as an executable expression.
3907 // Why isn't this weirdness documented better in StmtIterator?
3908 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003909 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003910 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003911 return child_range(child_iterator(T), child_iterator());
3912 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003913 }
John McCall63c00d72011-02-09 08:16:59 +00003914 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003915}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003916
Steve Naroff563477d2007-09-18 23:55:05 +00003917// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003918Stmt::child_range ObjCMessageExpr::children() {
3919 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003920 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003921 begin = reinterpret_cast<Stmt **>(this + 1);
3922 else
3923 begin = reinterpret_cast<Stmt **>(getArgs());
3924 return child_range(begin,
3925 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003926}
3927
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003928ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003929 QualType T, ObjCMethodDecl *Method,
3930 SourceRange SR)
3931 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3932 false, false, false, false),
3933 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3934{
3935 Expr **SaveElements = getElements();
3936 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3937 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3938 ExprBits.ValueDependent = true;
3939 if (Elements[I]->isInstantiationDependent())
3940 ExprBits.InstantiationDependent = true;
3941 if (Elements[I]->containsUnexpandedParameterPack())
3942 ExprBits.ContainsUnexpandedParameterPack = true;
3943
3944 SaveElements[I] = Elements[I];
3945 }
3946}
3947
3948ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003949 ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003950 QualType T, ObjCMethodDecl * Method,
3951 SourceRange SR) {
3952 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3953 + Elements.size() * sizeof(Expr *));
3954 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3955}
3956
3957ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3958 unsigned NumElements) {
3959
3960 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3961 + NumElements * sizeof(Expr *));
3962 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3963}
3964
3965ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3966 ArrayRef<ObjCDictionaryElement> VK,
3967 bool HasPackExpansions,
3968 QualType T, ObjCMethodDecl *method,
3969 SourceRange SR)
3970 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3971 false, false),
3972 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3973 DictWithObjectsMethod(method)
3974{
3975 KeyValuePair *KeyValues = getKeyValues();
3976 ExpansionData *Expansions = getExpansionData();
3977 for (unsigned I = 0; I < NumElements; I++) {
3978 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3979 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3980 ExprBits.ValueDependent = true;
3981 if (VK[I].Key->isInstantiationDependent() ||
3982 VK[I].Value->isInstantiationDependent())
3983 ExprBits.InstantiationDependent = true;
3984 if (VK[I].EllipsisLoc.isInvalid() &&
3985 (VK[I].Key->containsUnexpandedParameterPack() ||
3986 VK[I].Value->containsUnexpandedParameterPack()))
3987 ExprBits.ContainsUnexpandedParameterPack = true;
3988
3989 KeyValues[I].Key = VK[I].Key;
3990 KeyValues[I].Value = VK[I].Value;
3991 if (Expansions) {
3992 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3993 if (VK[I].NumExpansions)
3994 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3995 else
3996 Expansions[I].NumExpansionsPlusOne = 0;
3997 }
3998 }
3999}
4000
4001ObjCDictionaryLiteral *
4002ObjCDictionaryLiteral::Create(ASTContext &C,
4003 ArrayRef<ObjCDictionaryElement> VK,
4004 bool HasPackExpansions,
4005 QualType T, ObjCMethodDecl *method,
4006 SourceRange SR) {
4007 unsigned ExpansionsSize = 0;
4008 if (HasPackExpansions)
4009 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4010
4011 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4012 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4013 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4014}
4015
4016ObjCDictionaryLiteral *
4017ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
4018 bool HasPackExpansions) {
4019 unsigned ExpansionsSize = 0;
4020 if (HasPackExpansions)
4021 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4022 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4023 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4024 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4025 HasPackExpansions);
4026}
4027
4028ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
4029 Expr *base,
4030 Expr *key, QualType T,
4031 ObjCMethodDecl *getMethod,
4032 ObjCMethodDecl *setMethod,
4033 SourceLocation RB) {
4034 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4035 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4036 OK_ObjCSubscript,
4037 getMethod, setMethod, RB);
4038}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004039
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004040AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004041 QualType t, AtomicOp op, SourceLocation RP)
4042 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4043 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004044 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004045{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004046 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4047 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004048 if (args[i]->isTypeDependent())
4049 ExprBits.TypeDependent = true;
4050 if (args[i]->isValueDependent())
4051 ExprBits.ValueDependent = true;
4052 if (args[i]->isInstantiationDependent())
4053 ExprBits.InstantiationDependent = true;
4054 if (args[i]->containsUnexpandedParameterPack())
4055 ExprBits.ContainsUnexpandedParameterPack = true;
4056
4057 SubExprs[i] = args[i];
4058 }
4059}
Richard Smithe1b2abc2012-04-10 22:49:28 +00004060
4061unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4062 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00004063 case AO__c11_atomic_init:
4064 case AO__c11_atomic_load:
4065 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004066 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00004067
4068 case AO__c11_atomic_store:
4069 case AO__c11_atomic_exchange:
4070 case AO__atomic_load:
4071 case AO__atomic_store:
4072 case AO__atomic_store_n:
4073 case AO__atomic_exchange_n:
4074 case AO__c11_atomic_fetch_add:
4075 case AO__c11_atomic_fetch_sub:
4076 case AO__c11_atomic_fetch_and:
4077 case AO__c11_atomic_fetch_or:
4078 case AO__c11_atomic_fetch_xor:
4079 case AO__atomic_fetch_add:
4080 case AO__atomic_fetch_sub:
4081 case AO__atomic_fetch_and:
4082 case AO__atomic_fetch_or:
4083 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00004084 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00004085 case AO__atomic_add_fetch:
4086 case AO__atomic_sub_fetch:
4087 case AO__atomic_and_fetch:
4088 case AO__atomic_or_fetch:
4089 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00004090 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004091 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00004092
4093 case AO__atomic_exchange:
4094 return 4;
4095
4096 case AO__c11_atomic_compare_exchange_strong:
4097 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004098 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00004099
4100 case AO__atomic_compare_exchange:
4101 case AO__atomic_compare_exchange_n:
4102 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00004103 }
4104 llvm_unreachable("unknown atomic op");
4105}