blob: ff9e2721d29545e6db5c194c66ee4d61868d32bf [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 }
138 static inline bad implements_children(children_t Stmt::*) {
139 return bad();
140 }
John McCall63c00d72011-02-09 08:16:59 +0000141
Erik Verbruggen65d78312012-12-25 14:51:39 +0000142 typedef SourceLocation getLocStart_t() const;
143 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000144 return good();
145 }
Erik Verbruggen65d78312012-12-25 14:51:39 +0000146 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
147 return bad();
148 }
149
150 typedef SourceLocation getLocEnd_t() const;
151 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
152 return good();
153 }
154 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000155 return bad();
156 }
John McCall63c00d72011-02-09 08:16:59 +0000157
158#define ASSERT_IMPLEMENTS_children(type) \
159 (void) sizeof(is_good(implements_children(&type::children)))
Erik Verbruggen65d78312012-12-25 14:51:39 +0000160#define ASSERT_IMPLEMENTS_getLocStart(type) \
161 (void) sizeof(is_good(implements_getLocStart(&type::getLocStart)))
162#define ASSERT_IMPLEMENTS_getLocEnd(type) \
163 (void) sizeof(is_good(implements_getLocEnd(&type::getLocEnd)))
John McCall63c00d72011-02-09 08:16:59 +0000164}
165
166/// Check whether the various Stmt classes implement their member
167/// functions.
168static inline void check_implementations() {
169#define ABSTRACT_STMT(type)
170#define STMT(type, base) \
171 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000172 ASSERT_IMPLEMENTS_getLocStart(type); \
173 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCall63c00d72011-02-09 08:16:59 +0000174#include "clang/AST/StmtNodes.inc"
175}
176
177Stmt::child_range Stmt::children() {
178 switch (getStmtClass()) {
179 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
180#define ABSTRACT_STMT(type)
181#define STMT(type, base) \
182 case Stmt::type##Class: \
183 return static_cast<type*>(this)->children();
184#include "clang/AST/StmtNodes.inc"
185 }
186 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000187}
188
Erik Verbruggen65d78312012-12-25 14:51:39 +0000189// Amusing macro metaprogramming hack: check whether a class provides
190// a more specific implementation of getSourceRange.
191//
192// See also Expr.cpp:getExprLoc().
193namespace {
194 /// This implementation is used when a class provides a custom
195 /// implementation of getSourceRange.
196 template <class S, class T>
197 SourceRange getSourceRangeImpl(const Stmt *stmt,
198 SourceRange (T::*v)() const) {
199 return static_cast<const S*>(stmt)->getSourceRange();
200 }
201
202 /// This implementation is used when a class doesn't provide a custom
203 /// implementation of getSourceRange. Overload resolution should pick it over
204 /// the implementation above because it's more specialized according to
205 /// function template partial ordering.
206 template <class S>
207 SourceRange getSourceRangeImpl(const Stmt *stmt,
208 SourceRange (Stmt::*v)() const) {
209 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
210 static_cast<const S*>(stmt)->getLocEnd());
211 }
212}
213
John McCall63c00d72011-02-09 08:16:59 +0000214SourceRange Stmt::getSourceRange() const {
215 switch (getStmtClass()) {
216 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
217#define ABSTRACT_STMT(type)
218#define STMT(type, base) \
219 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000220 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCall63c00d72011-02-09 08:16:59 +0000221#include "clang/AST/StmtNodes.inc"
222 }
223 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000224}
225
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000226SourceLocation Stmt::getLocStart() const {
Erik Verbruggen65d78312012-12-25 14:51:39 +0000227// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000228 switch (getStmtClass()) {
229 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
230#define ABSTRACT_STMT(type)
231#define STMT(type, base) \
232 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000233 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000234#include "clang/AST/StmtNodes.inc"
235 }
236 llvm_unreachable("unknown statement kind");
237}
238
239SourceLocation Stmt::getLocEnd() const {
240 switch (getStmtClass()) {
241 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
242#define ABSTRACT_STMT(type)
243#define STMT(type, base) \
244 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000245 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000246#include "clang/AST/StmtNodes.inc"
247 }
248 llvm_unreachable("unknown statement kind");
249}
250
Craig Topper0b861562013-08-22 05:28:54 +0000251CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000252 SourceLocation LB, SourceLocation RB)
253 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
Nico Weberd36aa352012-12-29 20:03:39 +0000254 CompoundStmtBits.NumStmts = Stmts.size();
255 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000256 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
257
Nico Weberd36aa352012-12-29 20:03:39 +0000258 if (Stmts.size() == 0) {
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000259 Body = 0;
260 return;
261 }
262
Nico Weberd36aa352012-12-29 20:03:39 +0000263 Body = new (C) Stmt*[Stmts.size()];
264 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000265}
266
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000267void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000268 if (this->Body)
269 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000270 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000271
272 Body = new (C) Stmt*[NumStmts];
273 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
274}
Reid Spencer5f016e22007-07-11 17:01:13 +0000275
Reid Spencer5f016e22007-07-11 17:01:13 +0000276const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000277 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000278}
279
Craig Topper0b861562013-08-22 05:28:54 +0000280AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko49908902012-07-09 10:04:07 +0000281 ArrayRef<const Attr*> Attrs,
282 Stmt *SubStmt) {
283 void *Mem = C.Allocate(sizeof(AttributedStmt) +
284 sizeof(Attr*) * (Attrs.size() - 1),
285 llvm::alignOf<AttributedStmt>());
286 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
287}
288
Craig Topper0b861562013-08-22 05:28:54 +0000289AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
290 unsigned NumAttrs) {
Alexander Kornienko49908902012-07-09 10:04:07 +0000291 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
292 void *Mem = C.Allocate(sizeof(AttributedStmt) +
293 sizeof(Attr*) * (NumAttrs - 1),
294 llvm::alignOf<AttributedStmt>());
295 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
296}
297
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000298std::string AsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000299 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
300 return gccAsmStmt->generateAsmString(C);
301 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
302 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000303 llvm_unreachable("unknown asm statement kind!");
304}
305
306StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000307 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
308 return gccAsmStmt->getOutputConstraint(i);
309 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
310 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000311 llvm_unreachable("unknown asm statement kind!");
312}
313
314const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000315 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
316 return gccAsmStmt->getOutputExpr(i);
317 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
318 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000319 llvm_unreachable("unknown asm statement kind!");
320}
321
322StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000323 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
324 return gccAsmStmt->getInputConstraint(i);
325 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
326 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000327 llvm_unreachable("unknown asm statement kind!");
328}
329
330const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000331 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
332 return gccAsmStmt->getInputExpr(i);
333 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
334 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000335 llvm_unreachable("unknown asm statement kind!");
336}
337
338StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000339 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
340 return gccAsmStmt->getClobber(i);
341 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
342 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000343 llvm_unreachable("unknown asm statement kind!");
344}
345
Chad Rosierc4fb2212012-08-28 00:24:05 +0000346/// getNumPlusOperands - Return the number of output operands that have a "+"
347/// constraint.
348unsigned AsmStmt::getNumPlusOperands() const {
349 unsigned Res = 0;
350 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
351 if (isOutputPlusConstraint(i))
352 ++Res;
353 return Res;
354}
355
Chad Rosier33f05582012-08-27 23:47:56 +0000356StringRef GCCAsmStmt::getClobber(unsigned i) const {
357 return getClobberStringLiteral(i)->getString();
358}
359
Chad Rosierdf5faf52012-08-25 00:11:56 +0000360Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000361 return cast<Expr>(Exprs[i]);
362}
Chris Lattnerb3277932009-03-10 04:59:06 +0000363
364/// getOutputConstraint - Return the constraint string for the specified
365/// output operand. All output constraints are known to be non-empty (either
366/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000367StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000368 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000369}
Chris Lattnerb3277932009-03-10 04:59:06 +0000370
Chad Rosierdf5faf52012-08-25 00:11:56 +0000371Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000372 return cast<Expr>(Exprs[i + NumOutputs]);
373}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000374void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000375 Exprs[i + NumOutputs] = E;
376}
377
Chris Lattnerb3277932009-03-10 04:59:06 +0000378/// getInputConstraint - Return the specified input constraint. Unlike output
379/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000380StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000381 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000382}
383
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000384void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
385 IdentifierInfo **Names,
386 StringLiteral **Constraints,
387 Stmt **Exprs,
388 unsigned NumOutputs,
389 unsigned NumInputs,
390 StringLiteral **Clobbers,
391 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000392 this->NumOutputs = NumOutputs;
393 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000394 this->NumClobbers = NumClobbers;
395
396 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000397
Anders Carlsson966146e2010-01-30 23:19:41 +0000398 C.Deallocate(this->Names);
399 this->Names = new (C) IdentifierInfo*[NumExprs];
400 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000401
Anders Carlsson966146e2010-01-30 23:19:41 +0000402 C.Deallocate(this->Exprs);
403 this->Exprs = new (C) Stmt*[NumExprs];
404 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000405
Anders Carlsson966146e2010-01-30 23:19:41 +0000406 C.Deallocate(this->Constraints);
407 this->Constraints = new (C) StringLiteral*[NumExprs];
408 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000409
Anders Carlsson966146e2010-01-30 23:19:41 +0000410 C.Deallocate(this->Clobbers);
411 this->Clobbers = new (C) StringLiteral*[NumClobbers];
412 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000413}
414
Chris Lattner10ca96a2009-03-10 06:33:24 +0000415/// getNamedOperand - Given a symbolic operand reference like %[foo],
416/// translate this into a numeric value needed to reference the same operand.
417/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000418int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000419 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattner10ca96a2009-03-10 06:33:24 +0000421 // Check if this is an output operand.
422 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
423 if (getOutputName(i) == SymbolicName)
424 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattner10ca96a2009-03-10 06:33:24 +0000427 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
428 if (getInputName(i) == SymbolicName)
429 return getNumOutputs() + NumPlusOperands + i;
430
431 // Not found.
432 return -1;
433}
434
Chris Lattner458cd9c2009-03-10 23:21:44 +0000435/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
436/// it into pieces. If the asm string is erroneous, emit errors and return
437/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000438unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000439 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000440 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000441 const char *StrStart = Str.begin();
442 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000443 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner458cd9c2009-03-10 23:21:44 +0000445 // "Simple" inline asms have no constraints or operands, just convert the asm
446 // string to escape $'s.
447 if (isSimple()) {
448 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000449 for (; CurPtr != StrEnd; ++CurPtr) {
450 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000451 case '$':
452 Result += "$$";
453 break;
454 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000455 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000456 break;
457 }
458 }
459 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000460 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000461 }
462
463 // CurStringPiece - The current string that we are building up as we scan the
464 // asm string.
465 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000467 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000468
Chris Lattner458cd9c2009-03-10 23:21:44 +0000469 while (1) {
470 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000471 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000472 if (!CurStringPiece.empty())
473 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000474 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattner3182db12009-03-10 23:51:40 +0000477 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000478 switch (CurChar) {
479 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000480 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
481 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
482 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000483 case '%':
484 break;
485 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000486 CurStringPiece += CurChar;
487 continue;
488 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000489
Chris Lattner458cd9c2009-03-10 23:21:44 +0000490 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000491 if (CurPtr == StrEnd) {
492 // % at end of string is invalid (no escape).
493 DiagOffs = CurPtr-StrStart-1;
494 return diag::err_asm_invalid_escape;
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Chris Lattner3182db12009-03-10 23:51:40 +0000497 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000498 if (EscapedChar == '%') { // %% -> %
499 // Escaped percentage sign.
500 CurStringPiece += '%';
501 continue;
502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattner458cd9c2009-03-10 23:21:44 +0000504 if (EscapedChar == '=') { // %= -> Generate an unique ID.
505 CurStringPiece += "${:uid}";
506 continue;
507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattner458cd9c2009-03-10 23:21:44 +0000509 // Otherwise, we have an operand. If we have accumulated a string so far,
510 // add it to the Pieces list.
511 if (!CurStringPiece.empty()) {
512 Pieces.push_back(AsmStringPiece(CurStringPiece));
513 CurStringPiece.clear();
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattner458cd9c2009-03-10 23:21:44 +0000516 // Handle %x4 and %x[foo] by capturing x as the modifier character.
517 char Modifier = '\0';
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000518 if (isLetter(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000519 if (CurPtr == StrEnd) { // Premature end.
520 DiagOffs = CurPtr-StrStart-1;
521 return diag::err_asm_invalid_escape;
522 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000523 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000524 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000527 if (isDigit(EscapedChar)) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000528 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000529 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Chris Lattnercafc2222009-03-11 22:52:17 +0000531 --CurPtr;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000532 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000533 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattner85759272009-03-11 00:23:13 +0000535 unsigned NumOperands =
536 getNumOutputs() + getNumPlusOperands() + getNumInputs();
537 if (N >= NumOperands) {
538 DiagOffs = CurPtr-StrStart-1;
539 return diag::err_asm_invalid_operand_number;
540 }
541
Chris Lattner458cd9c2009-03-10 23:21:44 +0000542 Pieces.push_back(AsmStringPiece(N, Modifier));
543 continue;
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattner458cd9c2009-03-10 23:21:44 +0000546 // Handle %[foo], a symbolic operand reference.
547 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000548 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000550 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000551 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000552 if (NameEnd == 0)
553 return diag::err_asm_unterminated_symbolic_operand_name;
554 if (NameEnd == CurPtr)
555 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattner5f9e2722011-07-23 10:55:15 +0000557 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner458cd9c2009-03-10 23:21:44 +0000559 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000560 if (N == -1) {
561 // Verify that an operand with that name exists.
562 DiagOffs = CurPtr-StrStart;
563 return diag::err_asm_unknown_symbolic_operand_name;
564 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000565 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000567 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000568 continue;
569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner2ff0f422009-03-10 23:57:07 +0000571 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000572 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000573 }
574}
Chad Rosierda083b22012-08-27 20:23:31 +0000575
576/// Assemble final IR asm string (GCC-style).
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000577std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000578 // Analyze the asm string to decompose it into its pieces. We know that Sema
579 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000580 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000581 unsigned DiagOffs;
582 AnalyzeAsmString(Pieces, C, DiagOffs);
583
584 std::string AsmString;
585 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
586 if (Pieces[i].isString())
587 AsmString += Pieces[i].getString();
588 else if (Pieces[i].getModifier() == '\0')
589 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
590 else
591 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
592 Pieces[i].getModifier() + '}';
593 }
594 return AsmString;
595}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000596
Chad Rosierda083b22012-08-27 20:23:31 +0000597/// Assemble final IR asm string (MS-style).
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000598std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierda083b22012-08-27 20:23:31 +0000599 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000600 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000601}
602
Chad Rosier633abb02012-08-24 00:07:09 +0000603Expr *MSAsmStmt::getOutputExpr(unsigned i) {
604 return cast<Expr>(Exprs[i]);
605}
606
607Expr *MSAsmStmt::getInputExpr(unsigned i) {
608 return cast<Expr>(Exprs[i + NumOutputs]);
609}
610void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
611 Exprs[i + NumOutputs] = E;
612}
613
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000614QualType CXXCatchStmt::getCaughtType() const {
615 if (ExceptionDecl)
616 return ExceptionDecl->getType();
617 return QualType();
618}
619
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000620//===----------------------------------------------------------------------===//
621// Constructors
622//===----------------------------------------------------------------------===//
623
Craig Topper0b861562013-08-22 05:28:54 +0000624GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
625 bool issimple, bool isvolatile, unsigned numoutputs,
626 unsigned numinputs, IdentifierInfo **names,
627 StringLiteral **constraints, Expr **exprs,
628 StringLiteral *asmstr, unsigned numclobbers,
629 StringLiteral **clobbers, SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000630 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
631 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000632
Chad Rosier0e2a8682012-08-07 23:12:23 +0000633 unsigned NumExprs = NumOutputs + NumInputs;
634
Anders Carlsson966146e2010-01-30 23:19:41 +0000635 Names = new (C) IdentifierInfo*[NumExprs];
636 std::copy(names, names + NumExprs, Names);
637
638 Exprs = new (C) Stmt*[NumExprs];
639 std::copy(exprs, exprs + NumExprs, Exprs);
640
641 Constraints = new (C) StringLiteral*[NumExprs];
642 std::copy(constraints, constraints + NumExprs, Constraints);
643
644 Clobbers = new (C) StringLiteral*[NumClobbers];
645 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000646}
647
Craig Topper0b861562013-08-22 05:28:54 +0000648MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier058ab172012-08-16 00:06:53 +0000649 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosiere54cba12012-10-16 21:55:39 +0000650 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallaeeacf72013-05-03 00:10:13 +0000651 unsigned numinputs,
Chad Rosiere54cba12012-10-16 21:55:39 +0000652 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
653 StringRef asmstr, ArrayRef<StringRef> clobbers,
654 SourceLocation endloc)
655 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
656 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
John McCallaeeacf72013-05-03 00:10:13 +0000657 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
Chad Rosier058ab172012-08-16 00:06:53 +0000658
John McCallaeeacf72013-05-03 00:10:13 +0000659 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
660}
Chad Rosier058ab172012-08-16 00:06:53 +0000661
Craig Topper0b861562013-08-22 05:28:54 +0000662static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
John McCallaeeacf72013-05-03 00:10:13 +0000663 size_t size = str.size();
664 char *buffer = new (C) char[size];
665 memcpy(buffer, str.data(), size);
666 return StringRef(buffer, size);
667}
668
Craig Topper0b861562013-08-22 05:28:54 +0000669void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallaeeacf72013-05-03 00:10:13 +0000670 ArrayRef<Token> asmtoks,
671 ArrayRef<StringRef> constraints,
672 ArrayRef<Expr*> exprs,
673 ArrayRef<StringRef> clobbers) {
674 assert(NumAsmToks == asmtoks.size());
675 assert(NumClobbers == clobbers.size());
676
677 unsigned NumExprs = exprs.size();
678 assert(NumExprs == NumOutputs + NumInputs);
679 assert(NumExprs == constraints.size());
680
681 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier79efe242012-08-07 00:29:06 +0000682
Chad Rosier633abb02012-08-24 00:07:09 +0000683 Exprs = new (C) Stmt*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000684 for (unsigned i = 0, e = NumExprs; i != e; ++i)
685 Exprs[i] = exprs[i];
Chad Rosier633abb02012-08-24 00:07:09 +0000686
Chad Rosier79efe242012-08-07 00:29:06 +0000687 AsmToks = new (C) Token[NumAsmToks];
688 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
689 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000690
Chad Rosier89fb6d72012-08-28 20:28:20 +0000691 Constraints = new (C) StringRef[NumExprs];
692 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
John McCallaeeacf72013-05-03 00:10:13 +0000693 Constraints[i] = copyIntoContext(C, constraints[i]);
Chad Rosier89fb6d72012-08-28 20:28:20 +0000694 }
695
Chad Rosier33c72e12012-08-10 21:36:25 +0000696 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000697 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
698 // FIXME: Avoid the allocation/copy if at all possible.
John McCallaeeacf72013-05-03 00:10:13 +0000699 Clobbers[i] = copyIntoContext(C, clobbers[i]);
Chad Rosiere790bc32012-08-10 21:06:19 +0000700 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000701}
702
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000703ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
704 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000705 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000706: Stmt(ObjCForCollectionStmtClass) {
707 SubExprs[ELEM] = Elem;
708 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
709 SubExprs[BODY] = Body;
710 ForLoc = FCL;
711 RParenLoc = RPL;
712}
713
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000714ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
715 Stmt **CatchStmts, unsigned NumCatchStmts,
716 Stmt *atFinallyStmt)
717 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
718 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
719{
720 Stmt **Stmts = getStmts();
721 Stmts[0] = atTryStmt;
722 for (unsigned I = 0; I != NumCatchStmts; ++I)
723 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000724
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000725 if (HasFinally)
726 Stmts[NumCatchStmts + 1] = atFinallyStmt;
727}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000728
Craig Topper0b861562013-08-22 05:28:54 +0000729ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000730 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000731 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000732 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000733 unsigned NumCatchStmts,
734 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000735 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000736 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000737 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000738 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
739 atFinallyStmt);
740}
Ted Kremenekff981022008-02-01 21:28:59 +0000741
Craig Topper0b861562013-08-22 05:28:54 +0000742ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
743 unsigned NumCatchStmts,
744 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000745 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000746 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000747 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000748 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000749}
Nico Weber608b17f2008-08-05 23:15:29 +0000750
Erik Verbruggen65d78312012-12-25 14:51:39 +0000751SourceLocation ObjCAtTryStmt::getLocEnd() const {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000752 if (HasFinally)
Erik Verbruggen65d78312012-12-25 14:51:39 +0000753 return getFinallyStmt()->getLocEnd();
754 if (NumCatchStmts)
755 return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
756 return getTryBody()->getLocEnd();
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000757}
758
Craig Topper0b861562013-08-22 05:28:54 +0000759CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
Nico Weber07cf58c2012-12-29 20:13:03 +0000760 Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000761 std::size_t Size = sizeof(CXXTryStmt);
Nico Weber07cf58c2012-12-29 20:13:03 +0000762 Size += ((handlers.size() + 1) * sizeof(Stmt));
Sam Weiniga1a396d2010-02-03 03:56:39 +0000763
Chris Lattner32488542010-10-30 05:14:06 +0000764 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Nico Weber07cf58c2012-12-29 20:13:03 +0000765 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
Sam Weiniga1a396d2010-02-03 03:56:39 +0000766}
767
Craig Topper0b861562013-08-22 05:28:54 +0000768CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000769 unsigned numHandlers) {
770 std::size_t Size = sizeof(CXXTryStmt);
771 Size += ((numHandlers + 1) * sizeof(Stmt));
772
Chris Lattner32488542010-10-30 05:14:06 +0000773 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000774 return new (Mem) CXXTryStmt(Empty, numHandlers);
775}
776
Sam Weiniga1a396d2010-02-03 03:56:39 +0000777CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Nico Weber07cf58c2012-12-29 20:13:03 +0000778 ArrayRef<Stmt*> handlers)
779 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000780 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000781 Stmts[0] = tryBlock;
Nico Weber07cf58c2012-12-29 20:13:03 +0000782 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000783}
784
Richard Smithad762fc2011-04-14 22:09:26 +0000785CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
786 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
787 Stmt *Body, SourceLocation FL,
788 SourceLocation CL, SourceLocation RPL)
789 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
790 SubExprs[RANGE] = Range;
791 SubExprs[BEGINEND] = BeginEndStmt;
792 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
793 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
794 SubExprs[LOOPVAR] = LoopVar;
795 SubExprs[BODY] = Body;
796}
797
798Expr *CXXForRangeStmt::getRangeInit() {
799 DeclStmt *RangeStmt = getRangeStmt();
800 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
801 assert(RangeDecl &&& "for-range should have a single var decl");
802 return RangeDecl->getInit();
803}
804
805const Expr *CXXForRangeStmt::getRangeInit() const {
806 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
807}
808
809VarDecl *CXXForRangeStmt::getLoopVariable() {
810 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
811 assert(LV && "No loop variable in CXXForRangeStmt");
812 return cast<VarDecl>(LV);
813}
814
815const VarDecl *CXXForRangeStmt::getLoopVariable() const {
816 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
817}
818
Craig Topper0b861562013-08-22 05:28:54 +0000819IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000820 Stmt *then, SourceLocation EL, Stmt *elsev)
821 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000822{
823 setConditionVariable(C, var);
824 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
825 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000826 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000827}
828
829VarDecl *IfStmt::getConditionVariable() const {
830 if (!SubExprs[VAR])
831 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000832
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000833 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
834 return cast<VarDecl>(DS->getSingleDecl());
835}
836
Craig Topper0b861562013-08-22 05:28:54 +0000837void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000838 if (!V) {
839 SubExprs[VAR] = 0;
840 return;
841 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000842
Daniel Dunbar96a00142012-03-09 18:35:03 +0000843 SourceRange VarRange = V->getSourceRange();
844 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
845 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000846}
847
Craig Topper0b861562013-08-22 05:28:54 +0000848ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000849 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000850 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000851 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000852{
853 SubExprs[INIT] = Init;
854 setConditionVariable(C, condVar);
855 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
856 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
857 SubExprs[BODY] = Body;
858}
859
860VarDecl *ForStmt::getConditionVariable() const {
861 if (!SubExprs[CONDVAR])
862 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000863
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000864 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
865 return cast<VarDecl>(DS->getSingleDecl());
866}
867
Craig Topper0b861562013-08-22 05:28:54 +0000868void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000869 if (!V) {
870 SubExprs[CONDVAR] = 0;
871 return;
872 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000873
Daniel Dunbar96a00142012-03-09 18:35:03 +0000874 SourceRange VarRange = V->getSourceRange();
875 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
876 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000877}
878
Craig Topper0b861562013-08-22 05:28:54 +0000879SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000880 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000881{
882 setConditionVariable(C, Var);
883 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
884 SubExprs[BODY] = NULL;
885}
886
887VarDecl *SwitchStmt::getConditionVariable() const {
888 if (!SubExprs[VAR])
889 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000890
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000891 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
892 return cast<VarDecl>(DS->getSingleDecl());
893}
894
Craig Topper0b861562013-08-22 05:28:54 +0000895void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000896 if (!V) {
897 SubExprs[VAR] = 0;
898 return;
899 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000900
Daniel Dunbar96a00142012-03-09 18:35:03 +0000901 SourceRange VarRange = V->getSourceRange();
902 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
903 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000904}
905
John McCall63c00d72011-02-09 08:16:59 +0000906Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000907 if (isa<CaseStmt>(this))
908 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000909 return cast<DefaultStmt>(this)->getSubStmt();
910}
911
Craig Topper0b861562013-08-22 05:28:54 +0000912WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000913 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000914 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000915 setConditionVariable(C, Var);
916 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
917 SubExprs[BODY] = body;
918 WhileLoc = WL;
919}
920
921VarDecl *WhileStmt::getConditionVariable() const {
922 if (!SubExprs[VAR])
923 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000924
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000925 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
926 return cast<VarDecl>(DS->getSingleDecl());
927}
928
Craig Topper0b861562013-08-22 05:28:54 +0000929void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000930 if (!V) {
931 SubExprs[VAR] = 0;
932 return;
933 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000934
935 SourceRange VarRange = V->getSourceRange();
936 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
937 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000938}
939
Ted Kremenek82977772007-08-24 21:09:09 +0000940// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000941LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000942 if (AddrLabelExpr *E =
943 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
944 return E->getLabel();
945 return 0;
946}
Ted Kremenek82977772007-08-24 21:09:09 +0000947
Ted Kremenek82977772007-08-24 21:09:09 +0000948// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000949const Expr* ReturnStmt::getRetValue() const {
950 return cast_or_null<Expr>(RetExpr);
951}
952Expr* ReturnStmt::getRetValue() {
953 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000954}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000955
956SEHTryStmt::SEHTryStmt(bool IsCXXTry,
957 SourceLocation TryLoc,
958 Stmt *TryBlock,
959 Stmt *Handler)
960 : Stmt(SEHTryStmtClass),
961 IsCXXTry(IsCXXTry),
962 TryLoc(TryLoc)
963{
964 Children[TRY] = TryBlock;
965 Children[HANDLER] = Handler;
966}
967
Craig Topper0b861562013-08-22 05:28:54 +0000968SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
969 SourceLocation TryLoc, Stmt *TryBlock,
John Wiegley28bbe4b2011-04-28 01:08:34 +0000970 Stmt *Handler) {
971 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
972}
973
974SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
975 return dyn_cast<SEHExceptStmt>(getHandler());
976}
977
978SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
979 return dyn_cast<SEHFinallyStmt>(getHandler());
980}
981
982SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
983 Expr *FilterExpr,
984 Stmt *Block)
985 : Stmt(SEHExceptStmtClass),
986 Loc(Loc)
987{
988 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
989 Children[BLOCK] = Block;
990}
991
Craig Topper0b861562013-08-22 05:28:54 +0000992SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
993 Expr *FilterExpr, Stmt *Block) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000994 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
995}
996
997SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
998 Stmt *Block)
999 : Stmt(SEHFinallyStmtClass),
1000 Loc(Loc),
1001 Block(Block)
1002{}
1003
Craig Topper0b861562013-08-22 05:28:54 +00001004SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley28bbe4b2011-04-28 01:08:34 +00001005 Stmt *Block) {
1006 return new(C)SEHFinallyStmt(Loc,Block);
1007}
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001008
1009CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1010 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1011
1012 // Offset of the first Capture object.
1013 unsigned FirstCaptureOffset =
1014 llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1015
1016 return reinterpret_cast<Capture *>(
1017 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1018 + FirstCaptureOffset);
1019}
1020
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001021CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1022 ArrayRef<Capture> Captures,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001023 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001024 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001025 RecordDecl *RD)
1026 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001027 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001028 assert( S && "null captured statement");
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001029 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001030 assert(RD && "null record declaration for captured statement");
1031
1032 // Copy initialization expressions.
1033 Stmt **Stored = getStoredStmts();
1034 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1035 *Stored++ = CaptureInits[I];
1036
1037 // Copy the statement being captured.
1038 *Stored = S;
1039
1040 // Copy all Capture objects.
1041 Capture *Buffer = getStoredCaptures();
1042 std::copy(Captures.begin(), Captures.end(), Buffer);
1043}
1044
1045CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1046 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001047 CapDeclAndKind(0, CR_Default), TheRecordDecl(0) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001048 getStoredStmts()[NumCaptures] = 0;
1049}
1050
Craig Topper0b861562013-08-22 05:28:54 +00001051CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001052 CapturedRegionKind Kind,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001053 ArrayRef<Capture> Captures,
1054 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001055 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001056 RecordDecl *RD) {
1057 // The layout is
1058 //
1059 // -----------------------------------------------------------
1060 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1061 // ----------------^-------------------^----------------------
1062 // getStoredStmts() getStoredCaptures()
1063 //
1064 // where S is the statement being captured.
1065 //
1066 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1067
1068 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1069 if (!Captures.empty()) {
1070 // Realign for the following Capture array.
1071 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1072 Size += sizeof(Capture) * Captures.size();
1073 }
1074
1075 void *Mem = Context.Allocate(Size);
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001076 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001077}
1078
Craig Topper0b861562013-08-22 05:28:54 +00001079CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001080 unsigned NumCaptures) {
1081 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1082 if (NumCaptures > 0) {
1083 // Realign for the following Capture array.
1084 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1085 Size += sizeof(Capture) * NumCaptures;
1086 }
1087
1088 void *Mem = Context.Allocate(Size);
1089 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1090}
1091
1092Stmt::child_range CapturedStmt::children() {
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001093 // Children are captured field initilizers.
1094 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001095}
1096
1097bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Ben Langmuirdc5be4f2013-05-03 19:20:19 +00001098 for (const_capture_iterator I = capture_begin(),
1099 E = capture_end(); I != E; ++I) {
Richard Smith0d8e9642013-05-16 06:20:58 +00001100 if (!I->capturesVariable())
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001101 continue;
1102
1103 // This does not handle variable redeclarations. This should be
1104 // extended to capture variables with redeclarations, for example
1105 // a thread-private variable in OpenMP.
1106 if (I->getCapturedVar() == Var)
1107 return true;
1108 }
1109
1110 return false;
1111}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001112
Craig Topper0b861562013-08-22 05:28:54 +00001113OMPPrivateClause *OMPPrivateClause::Create(const ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001114 SourceLocation StartLoc,
1115 SourceLocation LParenLoc,
1116 SourceLocation EndLoc,
1117 ArrayRef<Expr *> VL) {
1118 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * VL.size(),
1119 llvm::alignOf<OMPPrivateClause>());
1120 OMPPrivateClause *Clause = new (Mem) OMPPrivateClause(StartLoc, LParenLoc,
1121 EndLoc, VL.size());
1122 Clause->setVarRefs(VL);
1123 return Clause;
1124}
1125
Craig Topper0b861562013-08-22 05:28:54 +00001126OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001127 unsigned N) {
1128 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * N,
1129 llvm::alignOf<OMPPrivateClause>());
1130 return new (Mem) OMPPrivateClause(N);
1131}
1132
1133void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
1134 assert(Clauses.size() == this->Clauses.size() &&
1135 "Number of clauses is not the same as the preallocated buffer");
1136 std::copy(Clauses.begin(), Clauses.end(), this->Clauses.begin());
1137}
1138
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001139OMPParallelDirective *OMPParallelDirective::Create(
Craig Topper0b861562013-08-22 05:28:54 +00001140 const ASTContext &C,
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001141 SourceLocation StartLoc,
1142 SourceLocation EndLoc,
1143 ArrayRef<OMPClause *> Clauses,
1144 Stmt *AssociatedStmt) {
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001145 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1146 sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *),
1147 llvm::alignOf<OMPParallelDirective>());
1148 OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1149 Clauses.size());
1150 Dir->setClauses(Clauses);
1151 Dir->setAssociatedStmt(AssociatedStmt);
1152 return Dir;
1153}
1154
Craig Topper0b861562013-08-22 05:28:54 +00001155OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001156 unsigned N,
1157 EmptyShell) {
1158 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1159 sizeof(OMPClause *) * N + sizeof(Stmt *),
1160 llvm::alignOf<OMPParallelDirective>());
1161 return new (Mem) OMPParallelDirective(N);
1162}