blob: 032159274404d2cfb7e5623479490ab8c2c7fac9 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Stmt.cpp - Statement 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 Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera93d0f22012-12-01 17:12:56 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ExprCXX.h"
Steve Narofff494b572008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Benjamin Kramera93d0f22012-12-01 17:12:56 +000018#include "clang/AST/Stmt.h"
Chris Lattner16f00492009-04-26 01:32:48 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtObjC.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000021#include "clang/AST/StmtOpenMP.h"
Sebastian Redl4b07b292008-12-22 19:15:10 +000022#include "clang/AST/Type.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
Chris Lattner9bffb072010-04-23 16:29:58 +000024#include "clang/Basic/TargetInfo.h"
Benjamin Kramera93d0f22012-12-01 17:12:56 +000025#include "clang/Lex/Token.h"
Chad Rosierbe3ace82012-08-24 17:05:45 +000026#include "llvm/ADT/StringExtras.h"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000027#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Reid Spencer5f016e22007-07-11 17:01:13 +000030static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000031 const char *Name;
32 unsigned Counter;
33 unsigned Size;
Sean Hunt4bfe1962010-05-05 15:24:00 +000034} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000035
36static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
37 static bool Initialized = false;
38 if (Initialized)
39 return StmtClassInfo[E];
40
41 // Intialize the table on the first use.
42 Initialized = true;
Sean Hunt7381d5c2010-05-18 06:22:21 +000043#define ABSTRACT_STMT(STMT)
Douglas Gregorf2cad862008-11-14 12:46:07 +000044#define STMT(CLASS, PARENT) \
45 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
46 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Sean Hunt4bfe1962010-05-05 15:24:00 +000047#include "clang/AST/StmtNodes.inc"
Nico Weber608b17f2008-08-05 23:15:29 +000048
Chris Lattner63381352007-08-25 01:42:24 +000049 return StmtClassInfo[E];
50}
51
Craig Topper05ed1a02013-08-18 10:09:15 +000052void *Stmt::operator new(size_t bytes, const ASTContext& C,
Craig Topper5b3ebb42013-08-18 17:45:38 +000053 unsigned alignment) {
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000054 return ::operator new(bytes, C, alignment);
55}
56
Reid Spencer5f016e22007-07-11 17:01:13 +000057const char *Stmt::getStmtClassName() const {
John McCall8e6285a2010-10-26 08:39:16 +000058 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000059}
60
61void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000062 // Ensure the table is primed.
63 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065 unsigned sum = 0;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000066 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Sean Hunt4bfe1962010-05-05 15:24:00 +000067 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000068 if (StmtClassInfo[i].Name == 0) continue;
69 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000071 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000072 sum = 0;
Sean Hunt4bfe1962010-05-05 15:24:00 +000073 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000074 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000075 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000076 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
77 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
78 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
79 << " bytes)\n";
Chris Lattner63381352007-08-25 01:42:24 +000080 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000081 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000082
83 llvm::errs() << "Total bytes = " << sum << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000084}
85
86void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000087 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000088}
89
Daniel Dunbar02892a62012-03-05 21:42:49 +000090bool Stmt::StatisticsEnabled = false;
91void Stmt::EnableStatistics() {
92 StatisticsEnabled = true;
Reid Spencer5f016e22007-07-11 17:01:13 +000093}
94
John McCall7e5e5f42011-07-07 06:58:02 +000095Stmt *Stmt::IgnoreImplicit() {
96 Stmt *s = this;
97
98 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
99 s = ewc->getSubExpr();
100
101 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
102 s = ice->getSubExpr();
103
104 return s;
105}
106
Chandler Carrutha1364be2011-09-10 00:02:34 +0000107/// \brief Strip off all label-like statements.
108///
Richard Smith534986f2012-04-14 00:33:13 +0000109/// This will strip off label statements, case statements, attributed
110/// statements and default statements recursively.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000111const Stmt *Stmt::stripLabelLikeStatements() const {
112 const Stmt *S = this;
113 while (true) {
114 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
115 S = LS->getSubStmt();
116 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
117 S = SC->getSubStmt();
Richard Smith534986f2012-04-14 00:33:13 +0000118 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
119 S = AS->getSubStmt();
Chandler Carrutha1364be2011-09-10 00:02:34 +0000120 else
121 return S;
122 }
123}
124
John McCall63c00d72011-02-09 08:16:59 +0000125namespace {
126 struct good {};
127 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +0000128
129 // These silly little functions have to be static inline to suppress
130 // unused warnings, and they have to be defined to suppress other
131 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000132 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +0000133
134 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000135 template <class T> good implements_children(children_t T::*) {
136 return good();
137 }
Eli Friedmanbafe46f2013-09-10 22:57:15 +0000138 LLVM_ATTRIBUTE_UNUSED
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000139 static inline bad implements_children(children_t Stmt::*) {
140 return bad();
141 }
John McCall63c00d72011-02-09 08:16:59 +0000142
Erik Verbruggen65d78312012-12-25 14:51:39 +0000143 typedef SourceLocation getLocStart_t() const;
144 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000145 return good();
146 }
Eli Friedmanbafe46f2013-09-10 22:57:15 +0000147 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen65d78312012-12-25 14:51:39 +0000148 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
149 return bad();
150 }
151
152 typedef SourceLocation getLocEnd_t() const;
153 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
154 return good();
155 }
Eli Friedmanbafe46f2013-09-10 22:57:15 +0000156 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen65d78312012-12-25 14:51:39 +0000157 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000158 return bad();
159 }
John McCall63c00d72011-02-09 08:16:59 +0000160
161#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmanbafe46f2013-09-10 22:57:15 +0000162 (void) is_good(implements_children(&type::children))
Erik Verbruggen65d78312012-12-25 14:51:39 +0000163#define ASSERT_IMPLEMENTS_getLocStart(type) \
Eli Friedmanbafe46f2013-09-10 22:57:15 +0000164 (void) is_good(implements_getLocStart(&type::getLocStart))
Erik Verbruggen65d78312012-12-25 14:51:39 +0000165#define ASSERT_IMPLEMENTS_getLocEnd(type) \
Eli Friedmanbafe46f2013-09-10 22:57:15 +0000166 (void) is_good(implements_getLocEnd(&type::getLocEnd))
John McCall63c00d72011-02-09 08:16:59 +0000167}
168
169/// Check whether the various Stmt classes implement their member
170/// functions.
Eli Friedmanbafe46f2013-09-10 22:57:15 +0000171LLVM_ATTRIBUTE_UNUSED
John McCall63c00d72011-02-09 08:16:59 +0000172static inline void check_implementations() {
173#define ABSTRACT_STMT(type)
174#define STMT(type, base) \
175 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000176 ASSERT_IMPLEMENTS_getLocStart(type); \
177 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCall63c00d72011-02-09 08:16:59 +0000178#include "clang/AST/StmtNodes.inc"
179}
180
181Stmt::child_range Stmt::children() {
182 switch (getStmtClass()) {
183 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
184#define ABSTRACT_STMT(type)
185#define STMT(type, base) \
186 case Stmt::type##Class: \
187 return static_cast<type*>(this)->children();
188#include "clang/AST/StmtNodes.inc"
189 }
190 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000191}
192
Erik Verbruggen65d78312012-12-25 14:51:39 +0000193// Amusing macro metaprogramming hack: check whether a class provides
194// a more specific implementation of getSourceRange.
195//
196// See also Expr.cpp:getExprLoc().
197namespace {
198 /// This implementation is used when a class provides a custom
199 /// implementation of getSourceRange.
200 template <class S, class T>
201 SourceRange getSourceRangeImpl(const Stmt *stmt,
202 SourceRange (T::*v)() const) {
203 return static_cast<const S*>(stmt)->getSourceRange();
204 }
205
206 /// This implementation is used when a class doesn't provide a custom
207 /// implementation of getSourceRange. Overload resolution should pick it over
208 /// the implementation above because it's more specialized according to
209 /// function template partial ordering.
210 template <class S>
211 SourceRange getSourceRangeImpl(const Stmt *stmt,
212 SourceRange (Stmt::*v)() const) {
213 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
214 static_cast<const S*>(stmt)->getLocEnd());
215 }
216}
217
John McCall63c00d72011-02-09 08:16:59 +0000218SourceRange Stmt::getSourceRange() const {
219 switch (getStmtClass()) {
220 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
221#define ABSTRACT_STMT(type)
222#define STMT(type, base) \
223 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000224 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCall63c00d72011-02-09 08:16:59 +0000225#include "clang/AST/StmtNodes.inc"
226 }
227 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000228}
229
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000230SourceLocation Stmt::getLocStart() const {
Erik Verbruggen65d78312012-12-25 14:51:39 +0000231// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000232 switch (getStmtClass()) {
233 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
234#define ABSTRACT_STMT(type)
235#define STMT(type, base) \
236 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000237 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000238#include "clang/AST/StmtNodes.inc"
239 }
240 llvm_unreachable("unknown statement kind");
241}
242
243SourceLocation Stmt::getLocEnd() const {
244 switch (getStmtClass()) {
245 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
246#define ABSTRACT_STMT(type)
247#define STMT(type, base) \
248 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000249 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000250#include "clang/AST/StmtNodes.inc"
251 }
252 llvm_unreachable("unknown statement kind");
253}
254
Craig Topper0b861562013-08-22 05:28:54 +0000255CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000256 SourceLocation LB, SourceLocation RB)
257 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
Nico Weberd36aa352012-12-29 20:03:39 +0000258 CompoundStmtBits.NumStmts = Stmts.size();
259 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000260 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
261
Nico Weberd36aa352012-12-29 20:03:39 +0000262 if (Stmts.size() == 0) {
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000263 Body = 0;
264 return;
265 }
266
Nico Weberd36aa352012-12-29 20:03:39 +0000267 Body = new (C) Stmt*[Stmts.size()];
268 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000269}
270
Craig Topper8b4b98b2013-08-22 06:02:26 +0000271void CompoundStmt::setStmts(const ASTContext &C, Stmt **Stmts,
272 unsigned NumStmts) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000273 if (this->Body)
274 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000275 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000276
277 Body = new (C) Stmt*[NumStmts];
278 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
279}
Reid Spencer5f016e22007-07-11 17:01:13 +0000280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000282 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000283}
284
Craig Topper0b861562013-08-22 05:28:54 +0000285AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko49908902012-07-09 10:04:07 +0000286 ArrayRef<const Attr*> Attrs,
287 Stmt *SubStmt) {
288 void *Mem = C.Allocate(sizeof(AttributedStmt) +
289 sizeof(Attr*) * (Attrs.size() - 1),
290 llvm::alignOf<AttributedStmt>());
291 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
292}
293
Craig Topper0b861562013-08-22 05:28:54 +0000294AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
295 unsigned NumAttrs) {
Alexander Kornienko49908902012-07-09 10:04:07 +0000296 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
297 void *Mem = C.Allocate(sizeof(AttributedStmt) +
298 sizeof(Attr*) * (NumAttrs - 1),
299 llvm::alignOf<AttributedStmt>());
300 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
301}
302
Craig Topper8b4b98b2013-08-22 06:02:26 +0000303std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000304 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
305 return gccAsmStmt->generateAsmString(C);
306 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
307 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000308 llvm_unreachable("unknown asm statement kind!");
309}
310
311StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000312 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
313 return gccAsmStmt->getOutputConstraint(i);
314 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
315 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000316 llvm_unreachable("unknown asm statement kind!");
317}
318
319const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000320 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
321 return gccAsmStmt->getOutputExpr(i);
322 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
323 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000324 llvm_unreachable("unknown asm statement kind!");
325}
326
327StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000328 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
329 return gccAsmStmt->getInputConstraint(i);
330 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
331 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000332 llvm_unreachable("unknown asm statement kind!");
333}
334
335const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000336 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
337 return gccAsmStmt->getInputExpr(i);
338 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
339 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000340 llvm_unreachable("unknown asm statement kind!");
341}
342
343StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000344 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
345 return gccAsmStmt->getClobber(i);
346 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
347 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000348 llvm_unreachable("unknown asm statement kind!");
349}
350
Chad Rosierc4fb2212012-08-28 00:24:05 +0000351/// getNumPlusOperands - Return the number of output operands that have a "+"
352/// constraint.
353unsigned AsmStmt::getNumPlusOperands() const {
354 unsigned Res = 0;
355 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
356 if (isOutputPlusConstraint(i))
357 ++Res;
358 return Res;
359}
360
Chad Rosier33f05582012-08-27 23:47:56 +0000361StringRef GCCAsmStmt::getClobber(unsigned i) const {
362 return getClobberStringLiteral(i)->getString();
363}
364
Chad Rosierdf5faf52012-08-25 00:11:56 +0000365Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000366 return cast<Expr>(Exprs[i]);
367}
Chris Lattnerb3277932009-03-10 04:59:06 +0000368
369/// getOutputConstraint - Return the constraint string for the specified
370/// output operand. All output constraints are known to be non-empty (either
371/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000372StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000373 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000374}
Chris Lattnerb3277932009-03-10 04:59:06 +0000375
Chad Rosierdf5faf52012-08-25 00:11:56 +0000376Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000377 return cast<Expr>(Exprs[i + NumOutputs]);
378}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000379void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000380 Exprs[i + NumOutputs] = E;
381}
382
Chris Lattnerb3277932009-03-10 04:59:06 +0000383/// getInputConstraint - Return the specified input constraint. Unlike output
384/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000385StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000386 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000387}
388
Craig Topper8b4b98b2013-08-22 06:02:26 +0000389void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
390 IdentifierInfo **Names,
391 StringLiteral **Constraints,
392 Stmt **Exprs,
393 unsigned NumOutputs,
394 unsigned NumInputs,
395 StringLiteral **Clobbers,
396 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000397 this->NumOutputs = NumOutputs;
398 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000399 this->NumClobbers = NumClobbers;
400
401 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000402
Anders Carlsson966146e2010-01-30 23:19:41 +0000403 C.Deallocate(this->Names);
404 this->Names = new (C) IdentifierInfo*[NumExprs];
405 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000406
Anders Carlsson966146e2010-01-30 23:19:41 +0000407 C.Deallocate(this->Exprs);
408 this->Exprs = new (C) Stmt*[NumExprs];
409 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000410
Anders Carlsson966146e2010-01-30 23:19:41 +0000411 C.Deallocate(this->Constraints);
412 this->Constraints = new (C) StringLiteral*[NumExprs];
413 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000414
Anders Carlsson966146e2010-01-30 23:19:41 +0000415 C.Deallocate(this->Clobbers);
416 this->Clobbers = new (C) StringLiteral*[NumClobbers];
417 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000418}
419
Chris Lattner10ca96a2009-03-10 06:33:24 +0000420/// getNamedOperand - Given a symbolic operand reference like %[foo],
421/// translate this into a numeric value needed to reference the same operand.
422/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000423int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000424 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner10ca96a2009-03-10 06:33:24 +0000426 // Check if this is an output operand.
427 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
428 if (getOutputName(i) == SymbolicName)
429 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000430 }
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattner10ca96a2009-03-10 06:33:24 +0000432 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
433 if (getInputName(i) == SymbolicName)
434 return getNumOutputs() + NumPlusOperands + i;
435
436 // Not found.
437 return -1;
438}
439
Chris Lattner458cd9c2009-03-10 23:21:44 +0000440/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
441/// it into pieces. If the asm string is erroneous, emit errors and return
442/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000443unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topper8b4b98b2013-08-22 06:02:26 +0000444 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000445 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000446 const char *StrStart = Str.begin();
447 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000448 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner458cd9c2009-03-10 23:21:44 +0000450 // "Simple" inline asms have no constraints or operands, just convert the asm
451 // string to escape $'s.
452 if (isSimple()) {
453 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000454 for (; CurPtr != StrEnd; ++CurPtr) {
455 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000456 case '$':
457 Result += "$$";
458 break;
459 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000460 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000461 break;
462 }
463 }
464 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000465 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000466 }
467
468 // CurStringPiece - The current string that we are building up as we scan the
469 // asm string.
470 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000472 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000473
Chris Lattner458cd9c2009-03-10 23:21:44 +0000474 while (1) {
475 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000476 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000477 if (!CurStringPiece.empty())
478 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000479 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000480 }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattner3182db12009-03-10 23:51:40 +0000482 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000483 switch (CurChar) {
484 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000485 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
486 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
487 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000488 case '%':
489 break;
490 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000491 CurStringPiece += CurChar;
492 continue;
493 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000494
Chris Lattner458cd9c2009-03-10 23:21:44 +0000495 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000496 if (CurPtr == StrEnd) {
497 // % at end of string is invalid (no escape).
498 DiagOffs = CurPtr-StrStart-1;
499 return diag::err_asm_invalid_escape;
500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner3182db12009-03-10 23:51:40 +0000502 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000503 if (EscapedChar == '%') { // %% -> %
504 // Escaped percentage sign.
505 CurStringPiece += '%';
506 continue;
507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattner458cd9c2009-03-10 23:21:44 +0000509 if (EscapedChar == '=') { // %= -> Generate an unique ID.
510 CurStringPiece += "${:uid}";
511 continue;
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattner458cd9c2009-03-10 23:21:44 +0000514 // Otherwise, we have an operand. If we have accumulated a string so far,
515 // add it to the Pieces list.
516 if (!CurStringPiece.empty()) {
517 Pieces.push_back(AsmStringPiece(CurStringPiece));
518 CurStringPiece.clear();
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner458cd9c2009-03-10 23:21:44 +0000521 // Handle %x4 and %x[foo] by capturing x as the modifier character.
522 char Modifier = '\0';
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000523 if (isLetter(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000524 if (CurPtr == StrEnd) { // Premature end.
525 DiagOffs = CurPtr-StrStart-1;
526 return diag::err_asm_invalid_escape;
527 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000528 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000529 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000530 }
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000532 if (isDigit(EscapedChar)) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000533 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000534 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Chris Lattnercafc2222009-03-11 22:52:17 +0000536 --CurPtr;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000537 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000538 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattner85759272009-03-11 00:23:13 +0000540 unsigned NumOperands =
541 getNumOutputs() + getNumPlusOperands() + getNumInputs();
542 if (N >= NumOperands) {
543 DiagOffs = CurPtr-StrStart-1;
544 return diag::err_asm_invalid_operand_number;
545 }
546
Chris Lattner458cd9c2009-03-10 23:21:44 +0000547 Pieces.push_back(AsmStringPiece(N, Modifier));
548 continue;
549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner458cd9c2009-03-10 23:21:44 +0000551 // Handle %[foo], a symbolic operand reference.
552 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000553 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000555 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000556 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000557 if (NameEnd == 0)
558 return diag::err_asm_unterminated_symbolic_operand_name;
559 if (NameEnd == CurPtr)
560 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattner5f9e2722011-07-23 10:55:15 +0000562 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Chris Lattner458cd9c2009-03-10 23:21:44 +0000564 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000565 if (N == -1) {
566 // Verify that an operand with that name exists.
567 DiagOffs = CurPtr-StrStart;
568 return diag::err_asm_unknown_symbolic_operand_name;
569 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000570 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000572 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000573 continue;
574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Chris Lattner2ff0f422009-03-10 23:57:07 +0000576 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000577 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000578 }
579}
Chad Rosierda083b22012-08-27 20:23:31 +0000580
581/// Assemble final IR asm string (GCC-style).
Craig Topper8b4b98b2013-08-22 06:02:26 +0000582std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000583 // Analyze the asm string to decompose it into its pieces. We know that Sema
584 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000585 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000586 unsigned DiagOffs;
587 AnalyzeAsmString(Pieces, C, DiagOffs);
588
589 std::string AsmString;
590 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
591 if (Pieces[i].isString())
592 AsmString += Pieces[i].getString();
593 else if (Pieces[i].getModifier() == '\0')
594 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
595 else
596 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
597 Pieces[i].getModifier() + '}';
598 }
599 return AsmString;
600}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000601
Chad Rosierda083b22012-08-27 20:23:31 +0000602/// Assemble final IR asm string (MS-style).
Craig Topper8b4b98b2013-08-22 06:02:26 +0000603std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosierda083b22012-08-27 20:23:31 +0000604 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000605 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000606}
607
Chad Rosier633abb02012-08-24 00:07:09 +0000608Expr *MSAsmStmt::getOutputExpr(unsigned i) {
609 return cast<Expr>(Exprs[i]);
610}
611
612Expr *MSAsmStmt::getInputExpr(unsigned i) {
613 return cast<Expr>(Exprs[i + NumOutputs]);
614}
615void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
616 Exprs[i + NumOutputs] = E;
617}
618
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000619QualType CXXCatchStmt::getCaughtType() const {
620 if (ExceptionDecl)
621 return ExceptionDecl->getType();
622 return QualType();
623}
624
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000625//===----------------------------------------------------------------------===//
626// Constructors
627//===----------------------------------------------------------------------===//
628
Craig Topper0b861562013-08-22 05:28:54 +0000629GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
630 bool issimple, bool isvolatile, unsigned numoutputs,
631 unsigned numinputs, IdentifierInfo **names,
632 StringLiteral **constraints, Expr **exprs,
633 StringLiteral *asmstr, unsigned numclobbers,
634 StringLiteral **clobbers, SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000635 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
636 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000637
Chad Rosier0e2a8682012-08-07 23:12:23 +0000638 unsigned NumExprs = NumOutputs + NumInputs;
639
Anders Carlsson966146e2010-01-30 23:19:41 +0000640 Names = new (C) IdentifierInfo*[NumExprs];
641 std::copy(names, names + NumExprs, Names);
642
643 Exprs = new (C) Stmt*[NumExprs];
644 std::copy(exprs, exprs + NumExprs, Exprs);
645
646 Constraints = new (C) StringLiteral*[NumExprs];
647 std::copy(constraints, constraints + NumExprs, Constraints);
648
649 Clobbers = new (C) StringLiteral*[NumClobbers];
650 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000651}
652
Craig Topper0b861562013-08-22 05:28:54 +0000653MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier058ab172012-08-16 00:06:53 +0000654 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosiere54cba12012-10-16 21:55:39 +0000655 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallaeeacf72013-05-03 00:10:13 +0000656 unsigned numinputs,
Chad Rosiere54cba12012-10-16 21:55:39 +0000657 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
658 StringRef asmstr, ArrayRef<StringRef> clobbers,
659 SourceLocation endloc)
660 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
661 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
John McCallaeeacf72013-05-03 00:10:13 +0000662 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
Chad Rosier058ab172012-08-16 00:06:53 +0000663
John McCallaeeacf72013-05-03 00:10:13 +0000664 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
665}
Chad Rosier058ab172012-08-16 00:06:53 +0000666
Craig Topper0b861562013-08-22 05:28:54 +0000667static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
John McCallaeeacf72013-05-03 00:10:13 +0000668 size_t size = str.size();
669 char *buffer = new (C) char[size];
670 memcpy(buffer, str.data(), size);
671 return StringRef(buffer, size);
672}
673
Craig Topper0b861562013-08-22 05:28:54 +0000674void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallaeeacf72013-05-03 00:10:13 +0000675 ArrayRef<Token> asmtoks,
676 ArrayRef<StringRef> constraints,
677 ArrayRef<Expr*> exprs,
678 ArrayRef<StringRef> clobbers) {
679 assert(NumAsmToks == asmtoks.size());
680 assert(NumClobbers == clobbers.size());
681
682 unsigned NumExprs = exprs.size();
683 assert(NumExprs == NumOutputs + NumInputs);
684 assert(NumExprs == constraints.size());
685
686 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier79efe242012-08-07 00:29:06 +0000687
Chad Rosier633abb02012-08-24 00:07:09 +0000688 Exprs = new (C) Stmt*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000689 for (unsigned i = 0, e = NumExprs; i != e; ++i)
690 Exprs[i] = exprs[i];
Chad Rosier633abb02012-08-24 00:07:09 +0000691
Chad Rosier79efe242012-08-07 00:29:06 +0000692 AsmToks = new (C) Token[NumAsmToks];
693 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
694 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000695
Chad Rosier89fb6d72012-08-28 20:28:20 +0000696 Constraints = new (C) StringRef[NumExprs];
697 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
John McCallaeeacf72013-05-03 00:10:13 +0000698 Constraints[i] = copyIntoContext(C, constraints[i]);
Chad Rosier89fb6d72012-08-28 20:28:20 +0000699 }
700
Chad Rosier33c72e12012-08-10 21:36:25 +0000701 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000702 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
703 // FIXME: Avoid the allocation/copy if at all possible.
John McCallaeeacf72013-05-03 00:10:13 +0000704 Clobbers[i] = copyIntoContext(C, clobbers[i]);
Chad Rosiere790bc32012-08-10 21:06:19 +0000705 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000706}
707
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000708ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
709 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000710 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000711: Stmt(ObjCForCollectionStmtClass) {
712 SubExprs[ELEM] = Elem;
Pavel Labathb2b169a2013-09-03 14:41:16 +0000713 SubExprs[COLLECTION] = Collect;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000714 SubExprs[BODY] = Body;
715 ForLoc = FCL;
716 RParenLoc = RPL;
717}
718
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000719ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
720 Stmt **CatchStmts, unsigned NumCatchStmts,
721 Stmt *atFinallyStmt)
722 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
723 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
724{
725 Stmt **Stmts = getStmts();
726 Stmts[0] = atTryStmt;
727 for (unsigned I = 0; I != NumCatchStmts; ++I)
728 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000729
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000730 if (HasFinally)
731 Stmts[NumCatchStmts + 1] = atFinallyStmt;
732}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000733
Craig Topper0b861562013-08-22 05:28:54 +0000734ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000735 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000736 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000737 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000738 unsigned NumCatchStmts,
739 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000740 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000741 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000742 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000743 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
744 atFinallyStmt);
745}
Ted Kremenekff981022008-02-01 21:28:59 +0000746
Craig Topper0b861562013-08-22 05:28:54 +0000747ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
748 unsigned NumCatchStmts,
749 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000750 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000751 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000752 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000753 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000754}
Nico Weber608b17f2008-08-05 23:15:29 +0000755
Erik Verbruggen65d78312012-12-25 14:51:39 +0000756SourceLocation ObjCAtTryStmt::getLocEnd() const {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000757 if (HasFinally)
Erik Verbruggen65d78312012-12-25 14:51:39 +0000758 return getFinallyStmt()->getLocEnd();
759 if (NumCatchStmts)
760 return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
761 return getTryBody()->getLocEnd();
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000762}
763
Craig Topper0b861562013-08-22 05:28:54 +0000764CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
Nico Weber07cf58c2012-12-29 20:13:03 +0000765 Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000766 std::size_t Size = sizeof(CXXTryStmt);
Nico Weber07cf58c2012-12-29 20:13:03 +0000767 Size += ((handlers.size() + 1) * sizeof(Stmt));
Sam Weiniga1a396d2010-02-03 03:56:39 +0000768
Chris Lattner32488542010-10-30 05:14:06 +0000769 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Nico Weber07cf58c2012-12-29 20:13:03 +0000770 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
Sam Weiniga1a396d2010-02-03 03:56:39 +0000771}
772
Craig Topper0b861562013-08-22 05:28:54 +0000773CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000774 unsigned numHandlers) {
775 std::size_t Size = sizeof(CXXTryStmt);
776 Size += ((numHandlers + 1) * sizeof(Stmt));
777
Chris Lattner32488542010-10-30 05:14:06 +0000778 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000779 return new (Mem) CXXTryStmt(Empty, numHandlers);
780}
781
Sam Weiniga1a396d2010-02-03 03:56:39 +0000782CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Nico Weber07cf58c2012-12-29 20:13:03 +0000783 ArrayRef<Stmt*> handlers)
784 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000785 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000786 Stmts[0] = tryBlock;
Nico Weber07cf58c2012-12-29 20:13:03 +0000787 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000788}
789
Richard Smithad762fc2011-04-14 22:09:26 +0000790CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
791 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
792 Stmt *Body, SourceLocation FL,
793 SourceLocation CL, SourceLocation RPL)
794 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
795 SubExprs[RANGE] = Range;
796 SubExprs[BEGINEND] = BeginEndStmt;
Pavel Labathb2b169a2013-09-03 14:41:16 +0000797 SubExprs[COND] = Cond;
798 SubExprs[INC] = Inc;
Richard Smithad762fc2011-04-14 22:09:26 +0000799 SubExprs[LOOPVAR] = LoopVar;
800 SubExprs[BODY] = Body;
801}
802
803Expr *CXXForRangeStmt::getRangeInit() {
804 DeclStmt *RangeStmt = getRangeStmt();
805 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
806 assert(RangeDecl &&& "for-range should have a single var decl");
807 return RangeDecl->getInit();
808}
809
810const Expr *CXXForRangeStmt::getRangeInit() const {
811 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
812}
813
814VarDecl *CXXForRangeStmt::getLoopVariable() {
815 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
816 assert(LV && "No loop variable in CXXForRangeStmt");
817 return cast<VarDecl>(LV);
818}
819
820const VarDecl *CXXForRangeStmt::getLoopVariable() const {
821 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
822}
823
Craig Topper0b861562013-08-22 05:28:54 +0000824IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000825 Stmt *then, SourceLocation EL, Stmt *elsev)
826 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000827{
828 setConditionVariable(C, var);
Pavel Labathb2b169a2013-09-03 14:41:16 +0000829 SubExprs[COND] = cond;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000830 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000831 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000832}
833
834VarDecl *IfStmt::getConditionVariable() const {
835 if (!SubExprs[VAR])
836 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000837
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000838 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
839 return cast<VarDecl>(DS->getSingleDecl());
840}
841
Craig Topper0b861562013-08-22 05:28:54 +0000842void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000843 if (!V) {
844 SubExprs[VAR] = 0;
845 return;
846 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000847
Daniel Dunbar96a00142012-03-09 18:35:03 +0000848 SourceRange VarRange = V->getSourceRange();
849 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
850 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000851}
852
Craig Topper0b861562013-08-22 05:28:54 +0000853ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000854 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000855 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000856 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000857{
858 SubExprs[INIT] = Init;
859 setConditionVariable(C, condVar);
Pavel Labathb2b169a2013-09-03 14:41:16 +0000860 SubExprs[COND] = Cond;
861 SubExprs[INC] = Inc;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000862 SubExprs[BODY] = Body;
863}
864
865VarDecl *ForStmt::getConditionVariable() const {
866 if (!SubExprs[CONDVAR])
867 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000868
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000869 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
870 return cast<VarDecl>(DS->getSingleDecl());
871}
872
Craig Topper0b861562013-08-22 05:28:54 +0000873void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000874 if (!V) {
875 SubExprs[CONDVAR] = 0;
876 return;
877 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000878
Daniel Dunbar96a00142012-03-09 18:35:03 +0000879 SourceRange VarRange = V->getSourceRange();
880 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
881 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000882}
883
Craig Topper0b861562013-08-22 05:28:54 +0000884SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000885 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000886{
887 setConditionVariable(C, Var);
Pavel Labathb2b169a2013-09-03 14:41:16 +0000888 SubExprs[COND] = cond;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000889 SubExprs[BODY] = NULL;
890}
891
892VarDecl *SwitchStmt::getConditionVariable() const {
893 if (!SubExprs[VAR])
894 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000895
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000896 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
897 return cast<VarDecl>(DS->getSingleDecl());
898}
899
Craig Topper0b861562013-08-22 05:28:54 +0000900void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000901 if (!V) {
902 SubExprs[VAR] = 0;
903 return;
904 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000905
Daniel Dunbar96a00142012-03-09 18:35:03 +0000906 SourceRange VarRange = V->getSourceRange();
907 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
908 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000909}
910
John McCall63c00d72011-02-09 08:16:59 +0000911Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000912 if (isa<CaseStmt>(this))
913 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000914 return cast<DefaultStmt>(this)->getSubStmt();
915}
916
Craig Topper0b861562013-08-22 05:28:54 +0000917WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000918 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000919 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000920 setConditionVariable(C, Var);
Pavel Labathb2b169a2013-09-03 14:41:16 +0000921 SubExprs[COND] = cond;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000922 SubExprs[BODY] = body;
923 WhileLoc = WL;
924}
925
926VarDecl *WhileStmt::getConditionVariable() const {
927 if (!SubExprs[VAR])
928 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000929
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000930 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
931 return cast<VarDecl>(DS->getSingleDecl());
932}
933
Craig Topper0b861562013-08-22 05:28:54 +0000934void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000935 if (!V) {
936 SubExprs[VAR] = 0;
937 return;
938 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000939
940 SourceRange VarRange = V->getSourceRange();
941 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
942 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000943}
944
Ted Kremenek82977772007-08-24 21:09:09 +0000945// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000946LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000947 if (AddrLabelExpr *E =
948 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
949 return E->getLabel();
950 return 0;
951}
Ted Kremenek82977772007-08-24 21:09:09 +0000952
Ted Kremenek82977772007-08-24 21:09:09 +0000953// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000954const Expr* ReturnStmt::getRetValue() const {
955 return cast_or_null<Expr>(RetExpr);
956}
957Expr* ReturnStmt::getRetValue() {
958 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000959}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000960
961SEHTryStmt::SEHTryStmt(bool IsCXXTry,
962 SourceLocation TryLoc,
963 Stmt *TryBlock,
964 Stmt *Handler)
965 : Stmt(SEHTryStmtClass),
966 IsCXXTry(IsCXXTry),
967 TryLoc(TryLoc)
968{
969 Children[TRY] = TryBlock;
970 Children[HANDLER] = Handler;
971}
972
Craig Topper0b861562013-08-22 05:28:54 +0000973SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
974 SourceLocation TryLoc, Stmt *TryBlock,
John Wiegley28bbe4b2011-04-28 01:08:34 +0000975 Stmt *Handler) {
976 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
977}
978
979SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
980 return dyn_cast<SEHExceptStmt>(getHandler());
981}
982
983SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
984 return dyn_cast<SEHFinallyStmt>(getHandler());
985}
986
987SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
988 Expr *FilterExpr,
989 Stmt *Block)
990 : Stmt(SEHExceptStmtClass),
991 Loc(Loc)
992{
Pavel Labathb2b169a2013-09-03 14:41:16 +0000993 Children[FILTER_EXPR] = FilterExpr;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000994 Children[BLOCK] = Block;
995}
996
Craig Topper0b861562013-08-22 05:28:54 +0000997SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
998 Expr *FilterExpr, Stmt *Block) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000999 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1000}
1001
1002SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1003 Stmt *Block)
1004 : Stmt(SEHFinallyStmtClass),
1005 Loc(Loc),
1006 Block(Block)
1007{}
1008
Craig Topper0b861562013-08-22 05:28:54 +00001009SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley28bbe4b2011-04-28 01:08:34 +00001010 Stmt *Block) {
1011 return new(C)SEHFinallyStmt(Loc,Block);
1012}
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001013
1014CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1015 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1016
1017 // Offset of the first Capture object.
1018 unsigned FirstCaptureOffset =
1019 llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1020
1021 return reinterpret_cast<Capture *>(
1022 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1023 + FirstCaptureOffset);
1024}
1025
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001026CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1027 ArrayRef<Capture> Captures,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001028 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001029 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001030 RecordDecl *RD)
1031 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001032 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001033 assert( S && "null captured statement");
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001034 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001035 assert(RD && "null record declaration for captured statement");
1036
1037 // Copy initialization expressions.
1038 Stmt **Stored = getStoredStmts();
1039 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1040 *Stored++ = CaptureInits[I];
1041
1042 // Copy the statement being captured.
1043 *Stored = S;
1044
1045 // Copy all Capture objects.
1046 Capture *Buffer = getStoredCaptures();
1047 std::copy(Captures.begin(), Captures.end(), Buffer);
1048}
1049
1050CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1051 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001052 CapDeclAndKind(0, CR_Default), TheRecordDecl(0) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001053 getStoredStmts()[NumCaptures] = 0;
1054}
1055
Craig Topper0b861562013-08-22 05:28:54 +00001056CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001057 CapturedRegionKind Kind,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001058 ArrayRef<Capture> Captures,
1059 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001060 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001061 RecordDecl *RD) {
1062 // The layout is
1063 //
1064 // -----------------------------------------------------------
1065 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1066 // ----------------^-------------------^----------------------
1067 // getStoredStmts() getStoredCaptures()
1068 //
1069 // where S is the statement being captured.
1070 //
1071 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1072
1073 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1074 if (!Captures.empty()) {
1075 // Realign for the following Capture array.
1076 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1077 Size += sizeof(Capture) * Captures.size();
1078 }
1079
1080 void *Mem = Context.Allocate(Size);
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001081 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001082}
1083
Craig Topper0b861562013-08-22 05:28:54 +00001084CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001085 unsigned NumCaptures) {
1086 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1087 if (NumCaptures > 0) {
1088 // Realign for the following Capture array.
1089 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1090 Size += sizeof(Capture) * NumCaptures;
1091 }
1092
1093 void *Mem = Context.Allocate(Size);
1094 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1095}
1096
1097Stmt::child_range CapturedStmt::children() {
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001098 // Children are captured field initilizers.
1099 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001100}
1101
1102bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Ben Langmuirdc5be4f2013-05-03 19:20:19 +00001103 for (const_capture_iterator I = capture_begin(),
1104 E = capture_end(); I != E; ++I) {
Richard Smith0d8e9642013-05-16 06:20:58 +00001105 if (!I->capturesVariable())
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001106 continue;
1107
1108 // This does not handle variable redeclarations. This should be
1109 // extended to capture variables with redeclarations, for example
1110 // a thread-private variable in OpenMP.
1111 if (I->getCapturedVar() == Var)
1112 return true;
1113 }
1114
1115 return false;
1116}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001117
Alexey Bataev0c018352013-09-06 18:03:48 +00001118StmtRange OMPClause::children() {
1119 switch(getClauseKind()) {
1120 default : break;
1121#define OPENMP_CLAUSE(Name, Class) \
1122 case OMPC_ ## Name : return static_cast<Class *>(this)->children();
1123#include "clang/Basic/OpenMPKinds.def"
1124 }
1125 llvm_unreachable("unknown OMPClause");
1126}
1127
Craig Topper0b861562013-08-22 05:28:54 +00001128OMPPrivateClause *OMPPrivateClause::Create(const ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001129 SourceLocation StartLoc,
1130 SourceLocation LParenLoc,
1131 SourceLocation EndLoc,
1132 ArrayRef<Expr *> VL) {
1133 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * VL.size(),
1134 llvm::alignOf<OMPPrivateClause>());
1135 OMPPrivateClause *Clause = new (Mem) OMPPrivateClause(StartLoc, LParenLoc,
1136 EndLoc, VL.size());
1137 Clause->setVarRefs(VL);
1138 return Clause;
1139}
1140
Craig Topper0b861562013-08-22 05:28:54 +00001141OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001142 unsigned N) {
1143 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * N,
1144 llvm::alignOf<OMPPrivateClause>());
1145 return new (Mem) OMPPrivateClause(N);
1146}
1147
Alexey Bataev0c018352013-09-06 18:03:48 +00001148OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
1149 SourceLocation StartLoc,
1150 SourceLocation LParenLoc,
1151 SourceLocation EndLoc,
1152 ArrayRef<Expr *> VL) {
1153 void *Mem = C.Allocate(sizeof(OMPSharedClause) + sizeof(Expr *) * VL.size(),
1154 llvm::alignOf<OMPSharedClause>());
1155 OMPSharedClause *Clause = new (Mem) OMPSharedClause(StartLoc, LParenLoc,
1156 EndLoc, VL.size());
1157 Clause->setVarRefs(VL);
1158 return Clause;
1159}
1160
1161OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C,
1162 unsigned N) {
1163 void *Mem = C.Allocate(sizeof(OMPSharedClause) + sizeof(Expr *) * N,
1164 llvm::alignOf<OMPSharedClause>());
1165 return new (Mem) OMPSharedClause(N);
1166}
1167
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001168void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
1169 assert(Clauses.size() == this->Clauses.size() &&
1170 "Number of clauses is not the same as the preallocated buffer");
1171 std::copy(Clauses.begin(), Clauses.end(), this->Clauses.begin());
1172}
1173
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001174OMPParallelDirective *OMPParallelDirective::Create(
Craig Topper0b861562013-08-22 05:28:54 +00001175 const ASTContext &C,
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001176 SourceLocation StartLoc,
1177 SourceLocation EndLoc,
1178 ArrayRef<OMPClause *> Clauses,
1179 Stmt *AssociatedStmt) {
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001180 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1181 sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *),
1182 llvm::alignOf<OMPParallelDirective>());
1183 OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1184 Clauses.size());
1185 Dir->setClauses(Clauses);
1186 Dir->setAssociatedStmt(AssociatedStmt);
1187 return Dir;
1188}
1189
Craig Topper0b861562013-08-22 05:28:54 +00001190OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001191 unsigned N,
1192 EmptyShell) {
1193 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1194 sizeof(OMPClause *) * N + sizeof(Stmt *),
1195 llvm::alignOf<OMPParallelDirective>());
1196 return new (Mem) OMPParallelDirective(N);
1197}