blob: a80bd87d216b4aff52bd16cc9c39780b445ac889 [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
Craig Topper05ed1a02013-08-18 10:09:15 +000057void *Stmt::operator new(size_t bytes, const ASTContext* C,
Craig Topper5b3ebb42013-08-18 17:45:38 +000058 unsigned alignment) {
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000059 return ::operator new(bytes, *C, alignment);
60}
61
Reid Spencer5f016e22007-07-11 17:01:13 +000062const char *Stmt::getStmtClassName() const {
John McCall8e6285a2010-10-26 08:39:16 +000063 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000064}
65
66void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000067 // Ensure the table is primed.
68 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000069
Reid Spencer5f016e22007-07-11 17:01:13 +000070 unsigned sum = 0;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000071 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Sean Hunt4bfe1962010-05-05 15:24:00 +000072 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000073 if (StmtClassInfo[i].Name == 0) continue;
74 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000075 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000076 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000077 sum = 0;
Sean Hunt4bfe1962010-05-05 15:24:00 +000078 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000079 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000080 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000081 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
82 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
83 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
84 << " bytes)\n";
Chris Lattner63381352007-08-25 01:42:24 +000085 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000087
88 llvm::errs() << "Total bytes = " << sum << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000089}
90
91void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000092 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000093}
94
Daniel Dunbar02892a62012-03-05 21:42:49 +000095bool Stmt::StatisticsEnabled = false;
96void Stmt::EnableStatistics() {
97 StatisticsEnabled = true;
Reid Spencer5f016e22007-07-11 17:01:13 +000098}
99
John McCall7e5e5f42011-07-07 06:58:02 +0000100Stmt *Stmt::IgnoreImplicit() {
101 Stmt *s = this;
102
103 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
104 s = ewc->getSubExpr();
105
106 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
107 s = ice->getSubExpr();
108
109 return s;
110}
111
Chandler Carrutha1364be2011-09-10 00:02:34 +0000112/// \brief Strip off all label-like statements.
113///
Richard Smith534986f2012-04-14 00:33:13 +0000114/// This will strip off label statements, case statements, attributed
115/// statements and default statements recursively.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000116const Stmt *Stmt::stripLabelLikeStatements() const {
117 const Stmt *S = this;
118 while (true) {
119 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
120 S = LS->getSubStmt();
121 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
122 S = SC->getSubStmt();
Richard Smith534986f2012-04-14 00:33:13 +0000123 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
124 S = AS->getSubStmt();
Chandler Carrutha1364be2011-09-10 00:02:34 +0000125 else
126 return S;
127 }
128}
129
John McCall63c00d72011-02-09 08:16:59 +0000130namespace {
131 struct good {};
132 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +0000133
134 // These silly little functions have to be static inline to suppress
135 // unused warnings, and they have to be defined to suppress other
136 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000137 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +0000138
139 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000140 template <class T> good implements_children(children_t T::*) {
141 return good();
142 }
143 static inline bad implements_children(children_t Stmt::*) {
144 return bad();
145 }
John McCall63c00d72011-02-09 08:16:59 +0000146
Erik Verbruggen65d78312012-12-25 14:51:39 +0000147 typedef SourceLocation getLocStart_t() const;
148 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000149 return good();
150 }
Erik Verbruggen65d78312012-12-25 14:51:39 +0000151 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
152 return bad();
153 }
154
155 typedef SourceLocation getLocEnd_t() const;
156 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
157 return good();
158 }
159 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000160 return bad();
161 }
John McCall63c00d72011-02-09 08:16:59 +0000162
163#define ASSERT_IMPLEMENTS_children(type) \
164 (void) sizeof(is_good(implements_children(&type::children)))
Erik Verbruggen65d78312012-12-25 14:51:39 +0000165#define ASSERT_IMPLEMENTS_getLocStart(type) \
166 (void) sizeof(is_good(implements_getLocStart(&type::getLocStart)))
167#define ASSERT_IMPLEMENTS_getLocEnd(type) \
168 (void) sizeof(is_good(implements_getLocEnd(&type::getLocEnd)))
John McCall63c00d72011-02-09 08:16:59 +0000169}
170
171/// Check whether the various Stmt classes implement their member
172/// functions.
173static inline void check_implementations() {
174#define ABSTRACT_STMT(type)
175#define STMT(type, base) \
176 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000177 ASSERT_IMPLEMENTS_getLocStart(type); \
178 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCall63c00d72011-02-09 08:16:59 +0000179#include "clang/AST/StmtNodes.inc"
180}
181
182Stmt::child_range Stmt::children() {
183 switch (getStmtClass()) {
184 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
185#define ABSTRACT_STMT(type)
186#define STMT(type, base) \
187 case Stmt::type##Class: \
188 return static_cast<type*>(this)->children();
189#include "clang/AST/StmtNodes.inc"
190 }
191 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000192}
193
Erik Verbruggen65d78312012-12-25 14:51:39 +0000194// Amusing macro metaprogramming hack: check whether a class provides
195// a more specific implementation of getSourceRange.
196//
197// See also Expr.cpp:getExprLoc().
198namespace {
199 /// This implementation is used when a class provides a custom
200 /// implementation of getSourceRange.
201 template <class S, class T>
202 SourceRange getSourceRangeImpl(const Stmt *stmt,
203 SourceRange (T::*v)() const) {
204 return static_cast<const S*>(stmt)->getSourceRange();
205 }
206
207 /// This implementation is used when a class doesn't provide a custom
208 /// implementation of getSourceRange. Overload resolution should pick it over
209 /// the implementation above because it's more specialized according to
210 /// function template partial ordering.
211 template <class S>
212 SourceRange getSourceRangeImpl(const Stmt *stmt,
213 SourceRange (Stmt::*v)() const) {
214 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
215 static_cast<const S*>(stmt)->getLocEnd());
216 }
217}
218
John McCall63c00d72011-02-09 08:16:59 +0000219SourceRange Stmt::getSourceRange() const {
220 switch (getStmtClass()) {
221 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
222#define ABSTRACT_STMT(type)
223#define STMT(type, base) \
224 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000225 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCall63c00d72011-02-09 08:16:59 +0000226#include "clang/AST/StmtNodes.inc"
227 }
228 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000229}
230
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000231SourceLocation Stmt::getLocStart() const {
Erik Verbruggen65d78312012-12-25 14:51:39 +0000232// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000233 switch (getStmtClass()) {
234 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
235#define ABSTRACT_STMT(type)
236#define STMT(type, base) \
237 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000238 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000239#include "clang/AST/StmtNodes.inc"
240 }
241 llvm_unreachable("unknown statement kind");
242}
243
244SourceLocation Stmt::getLocEnd() const {
245 switch (getStmtClass()) {
246 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
247#define ABSTRACT_STMT(type)
248#define STMT(type, base) \
249 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000250 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000251#include "clang/AST/StmtNodes.inc"
252 }
253 llvm_unreachable("unknown statement kind");
254}
255
Nico Weberd36aa352012-12-29 20:03:39 +0000256CompoundStmt::CompoundStmt(ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000257 SourceLocation LB, SourceLocation RB)
258 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
Nico Weberd36aa352012-12-29 20:03:39 +0000259 CompoundStmtBits.NumStmts = Stmts.size();
260 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000261 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
262
Nico Weberd36aa352012-12-29 20:03:39 +0000263 if (Stmts.size() == 0) {
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000264 Body = 0;
265 return;
266 }
267
Nico Weberd36aa352012-12-29 20:03:39 +0000268 Body = new (C) Stmt*[Stmts.size()];
269 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000270}
271
Douglas Gregor025452f2009-04-17 00:04:06 +0000272void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
273 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
Alexander Kornienko49908902012-07-09 10:04:07 +0000285AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
286 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
294AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
295 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
296 void *Mem = C.Allocate(sizeof(AttributedStmt) +
297 sizeof(Attr*) * (NumAttrs - 1),
298 llvm::alignOf<AttributedStmt>());
299 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
300}
301
Chad Rosieraba59aa2012-08-28 17:43:23 +0000302std::string AsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000303 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
304 return gccAsmStmt->generateAsmString(C);
305 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
306 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000307 llvm_unreachable("unknown asm statement kind!");
308}
309
310StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000311 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
312 return gccAsmStmt->getOutputConstraint(i);
313 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
314 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000315 llvm_unreachable("unknown asm statement kind!");
316}
317
318const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000319 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
320 return gccAsmStmt->getOutputExpr(i);
321 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
322 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000323 llvm_unreachable("unknown asm statement kind!");
324}
325
326StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000327 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
328 return gccAsmStmt->getInputConstraint(i);
329 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
330 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000331 llvm_unreachable("unknown asm statement kind!");
332}
333
334const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000335 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
336 return gccAsmStmt->getInputExpr(i);
337 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
338 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000339 llvm_unreachable("unknown asm statement kind!");
340}
341
342StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000343 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
344 return gccAsmStmt->getClobber(i);
345 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
346 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000347 llvm_unreachable("unknown asm statement kind!");
348}
349
Chad Rosierc4fb2212012-08-28 00:24:05 +0000350/// getNumPlusOperands - Return the number of output operands that have a "+"
351/// constraint.
352unsigned AsmStmt::getNumPlusOperands() const {
353 unsigned Res = 0;
354 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
355 if (isOutputPlusConstraint(i))
356 ++Res;
357 return Res;
358}
359
Chad Rosier33f05582012-08-27 23:47:56 +0000360StringRef GCCAsmStmt::getClobber(unsigned i) const {
361 return getClobberStringLiteral(i)->getString();
362}
363
Chad Rosierdf5faf52012-08-25 00:11:56 +0000364Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000365 return cast<Expr>(Exprs[i]);
366}
Chris Lattnerb3277932009-03-10 04:59:06 +0000367
368/// getOutputConstraint - Return the constraint string for the specified
369/// output operand. All output constraints are known to be non-empty (either
370/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000371StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000372 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000373}
Chris Lattnerb3277932009-03-10 04:59:06 +0000374
Chad Rosierdf5faf52012-08-25 00:11:56 +0000375Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000376 return cast<Expr>(Exprs[i + NumOutputs]);
377}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000378void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000379 Exprs[i + NumOutputs] = E;
380}
381
Chris Lattnerb3277932009-03-10 04:59:06 +0000382/// getInputConstraint - Return the specified input constraint. Unlike output
383/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000384StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000385 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000386}
387
Chad Rosierdf5faf52012-08-25 00:11:56 +0000388void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000389 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000390 StringLiteral **Constraints,
391 Stmt **Exprs,
392 unsigned NumOutputs,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000393 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000394 StringLiteral **Clobbers,
395 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000396 this->NumOutputs = NumOutputs;
397 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000398 this->NumClobbers = NumClobbers;
399
400 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000401
Anders Carlsson966146e2010-01-30 23:19:41 +0000402 C.Deallocate(this->Names);
403 this->Names = new (C) IdentifierInfo*[NumExprs];
404 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000405
Anders Carlsson966146e2010-01-30 23:19:41 +0000406 C.Deallocate(this->Exprs);
407 this->Exprs = new (C) Stmt*[NumExprs];
408 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000409
Anders Carlsson966146e2010-01-30 23:19:41 +0000410 C.Deallocate(this->Constraints);
411 this->Constraints = new (C) StringLiteral*[NumExprs];
412 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000413
Anders Carlsson966146e2010-01-30 23:19:41 +0000414 C.Deallocate(this->Clobbers);
415 this->Clobbers = new (C) StringLiteral*[NumClobbers];
416 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000417}
418
Chris Lattner10ca96a2009-03-10 06:33:24 +0000419/// getNamedOperand - Given a symbolic operand reference like %[foo],
420/// translate this into a numeric value needed to reference the same operand.
421/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000422int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000423 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner10ca96a2009-03-10 06:33:24 +0000425 // Check if this is an output operand.
426 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
427 if (getOutputName(i) == SymbolicName)
428 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattner10ca96a2009-03-10 06:33:24 +0000431 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
432 if (getInputName(i) == SymbolicName)
433 return getNumOutputs() + NumPlusOperands + i;
434
435 // Not found.
436 return -1;
437}
438
Chris Lattner458cd9c2009-03-10 23:21:44 +0000439/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
440/// it into pieces. If the asm string is erroneous, emit errors and return
441/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000442unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000443 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000444 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000445 const char *StrStart = Str.begin();
446 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000447 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Chris Lattner458cd9c2009-03-10 23:21:44 +0000449 // "Simple" inline asms have no constraints or operands, just convert the asm
450 // string to escape $'s.
451 if (isSimple()) {
452 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000453 for (; CurPtr != StrEnd; ++CurPtr) {
454 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000455 case '$':
456 Result += "$$";
457 break;
458 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000459 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000460 break;
461 }
462 }
463 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000464 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000465 }
466
467 // CurStringPiece - The current string that we are building up as we scan the
468 // asm string.
469 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000471 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000472
Chris Lattner458cd9c2009-03-10 23:21:44 +0000473 while (1) {
474 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000475 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000476 if (!CurStringPiece.empty())
477 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000478 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner3182db12009-03-10 23:51:40 +0000481 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000482 switch (CurChar) {
483 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000484 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
485 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
486 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000487 case '%':
488 break;
489 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000490 CurStringPiece += CurChar;
491 continue;
492 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000493
Chris Lattner458cd9c2009-03-10 23:21:44 +0000494 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000495 if (CurPtr == StrEnd) {
496 // % at end of string is invalid (no escape).
497 DiagOffs = CurPtr-StrStart-1;
498 return diag::err_asm_invalid_escape;
499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Chris Lattner3182db12009-03-10 23:51:40 +0000501 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000502 if (EscapedChar == '%') { // %% -> %
503 // Escaped percentage sign.
504 CurStringPiece += '%';
505 continue;
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Chris Lattner458cd9c2009-03-10 23:21:44 +0000508 if (EscapedChar == '=') { // %= -> Generate an unique ID.
509 CurStringPiece += "${:uid}";
510 continue;
511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattner458cd9c2009-03-10 23:21:44 +0000513 // Otherwise, we have an operand. If we have accumulated a string so far,
514 // add it to the Pieces list.
515 if (!CurStringPiece.empty()) {
516 Pieces.push_back(AsmStringPiece(CurStringPiece));
517 CurStringPiece.clear();
518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chris Lattner458cd9c2009-03-10 23:21:44 +0000520 // Handle %x4 and %x[foo] by capturing x as the modifier character.
521 char Modifier = '\0';
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000522 if (isLetter(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000523 if (CurPtr == StrEnd) { // Premature end.
524 DiagOffs = CurPtr-StrStart-1;
525 return diag::err_asm_invalid_escape;
526 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000527 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000528 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000529 }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000531 if (isDigit(EscapedChar)) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000532 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000533 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattnercafc2222009-03-11 22:52:17 +0000535 --CurPtr;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000536 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000537 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner85759272009-03-11 00:23:13 +0000539 unsigned NumOperands =
540 getNumOutputs() + getNumPlusOperands() + getNumInputs();
541 if (N >= NumOperands) {
542 DiagOffs = CurPtr-StrStart-1;
543 return diag::err_asm_invalid_operand_number;
544 }
545
Chris Lattner458cd9c2009-03-10 23:21:44 +0000546 Pieces.push_back(AsmStringPiece(N, Modifier));
547 continue;
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattner458cd9c2009-03-10 23:21:44 +0000550 // Handle %[foo], a symbolic operand reference.
551 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000552 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000554 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000555 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000556 if (NameEnd == 0)
557 return diag::err_asm_unterminated_symbolic_operand_name;
558 if (NameEnd == CurPtr)
559 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner5f9e2722011-07-23 10:55:15 +0000561 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Chris Lattner458cd9c2009-03-10 23:21:44 +0000563 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000564 if (N == -1) {
565 // Verify that an operand with that name exists.
566 DiagOffs = CurPtr-StrStart;
567 return diag::err_asm_unknown_symbolic_operand_name;
568 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000569 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000571 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000572 continue;
573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattner2ff0f422009-03-10 23:57:07 +0000575 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000576 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000577 }
578}
Chad Rosierda083b22012-08-27 20:23:31 +0000579
580/// Assemble final IR asm string (GCC-style).
581std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000582 // Analyze the asm string to decompose it into its pieces. We know that Sema
583 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000584 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000585 unsigned DiagOffs;
586 AnalyzeAsmString(Pieces, C, DiagOffs);
587
588 std::string AsmString;
589 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
590 if (Pieces[i].isString())
591 AsmString += Pieces[i].getString();
592 else if (Pieces[i].getModifier() == '\0')
593 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
594 else
595 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
596 Pieces[i].getModifier() + '}';
597 }
598 return AsmString;
599}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000600
Chad Rosierda083b22012-08-27 20:23:31 +0000601/// Assemble final IR asm string (MS-style).
602std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
603 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000604 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000605}
606
Chad Rosier633abb02012-08-24 00:07:09 +0000607Expr *MSAsmStmt::getOutputExpr(unsigned i) {
608 return cast<Expr>(Exprs[i]);
609}
610
611Expr *MSAsmStmt::getInputExpr(unsigned i) {
612 return cast<Expr>(Exprs[i + NumOutputs]);
613}
614void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
615 Exprs[i + NumOutputs] = E;
616}
617
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000618QualType CXXCatchStmt::getCaughtType() const {
619 if (ExceptionDecl)
620 return ExceptionDecl->getType();
621 return QualType();
622}
623
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000624//===----------------------------------------------------------------------===//
625// Constructors
626//===----------------------------------------------------------------------===//
627
Chad Rosierdf5faf52012-08-25 00:11:56 +0000628GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
629 bool isvolatile, unsigned numoutputs, unsigned numinputs,
630 IdentifierInfo **names, StringLiteral **constraints,
631 Expr **exprs, StringLiteral *asmstr,
632 unsigned numclobbers, StringLiteral **clobbers,
633 SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000634 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
635 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000636
Chad Rosier0e2a8682012-08-07 23:12:23 +0000637 unsigned NumExprs = NumOutputs + NumInputs;
638
Anders Carlsson966146e2010-01-30 23:19:41 +0000639 Names = new (C) IdentifierInfo*[NumExprs];
640 std::copy(names, names + NumExprs, Names);
641
642 Exprs = new (C) Stmt*[NumExprs];
643 std::copy(exprs, exprs + NumExprs, Exprs);
644
645 Constraints = new (C) StringLiteral*[NumExprs];
646 std::copy(constraints, constraints + NumExprs, Constraints);
647
648 Clobbers = new (C) StringLiteral*[NumClobbers];
649 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000650}
651
Chad Rosier058ab172012-08-16 00:06:53 +0000652MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
653 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosiere54cba12012-10-16 21:55:39 +0000654 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallaeeacf72013-05-03 00:10:13 +0000655 unsigned numinputs,
Chad Rosiere54cba12012-10-16 21:55:39 +0000656 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
657 StringRef asmstr, ArrayRef<StringRef> clobbers,
658 SourceLocation endloc)
659 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
660 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
John McCallaeeacf72013-05-03 00:10:13 +0000661 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
Chad Rosier058ab172012-08-16 00:06:53 +0000662
John McCallaeeacf72013-05-03 00:10:13 +0000663 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
664}
Chad Rosier058ab172012-08-16 00:06:53 +0000665
John McCallaeeacf72013-05-03 00:10:13 +0000666static StringRef copyIntoContext(ASTContext &C, StringRef str) {
667 size_t size = str.size();
668 char *buffer = new (C) char[size];
669 memcpy(buffer, str.data(), size);
670 return StringRef(buffer, size);
671}
672
673void MSAsmStmt::initialize(ASTContext &C,
674 StringRef asmstr,
675 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;
713 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
714 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
Chad Rosier0e2a8682012-08-07 23:12:23 +0000734ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
735 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
Chad Rosier0e2a8682012-08-07 23:12:23 +0000747ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000748 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
Sam Weiniga1a396d2010-02-03 03:56:39 +0000764CXXTryStmt *CXXTryStmt::Create(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
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000773CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
774 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;
797 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
798 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
799 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
Chad Rosier0e2a8682012-08-07 23:12:23 +0000824IfStmt::IfStmt(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);
829 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
830 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
842void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
843 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
Chad Rosier0e2a8682012-08-07 23:12:23 +0000853ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
854 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);
860 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
861 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
862 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
873void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
874 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
Chad Rosier0e2a8682012-08-07 23:12:23 +0000884SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
885 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000886{
887 setConditionVariable(C, Var);
888 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
889 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
900void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
901 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
Chad Rosier0e2a8682012-08-07 23:12:23 +0000917WhileStmt::WhileStmt(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);
921 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
922 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
934void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
935 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
973SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
974 bool IsCXXTry,
975 SourceLocation TryLoc,
976 Stmt *TryBlock,
977 Stmt *Handler) {
978 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
979}
980
981SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
982 return dyn_cast<SEHExceptStmt>(getHandler());
983}
984
985SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
986 return dyn_cast<SEHFinallyStmt>(getHandler());
987}
988
989SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
990 Expr *FilterExpr,
991 Stmt *Block)
992 : Stmt(SEHExceptStmtClass),
993 Loc(Loc)
994{
995 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
996 Children[BLOCK] = Block;
997}
998
999SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
1000 SourceLocation Loc,
1001 Expr *FilterExpr,
1002 Stmt *Block) {
1003 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1004}
1005
1006SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1007 Stmt *Block)
1008 : Stmt(SEHFinallyStmtClass),
1009 Loc(Loc),
1010 Block(Block)
1011{}
1012
1013SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
1014 SourceLocation Loc,
1015 Stmt *Block) {
1016 return new(C)SEHFinallyStmt(Loc,Block);
1017}
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001018
1019CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1020 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1021
1022 // Offset of the first Capture object.
1023 unsigned FirstCaptureOffset =
1024 llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1025
1026 return reinterpret_cast<Capture *>(
1027 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1028 + FirstCaptureOffset);
1029}
1030
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001031CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1032 ArrayRef<Capture> Captures,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001033 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001034 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001035 RecordDecl *RD)
1036 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001037 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001038 assert( S && "null captured statement");
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001039 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001040 assert(RD && "null record declaration for captured statement");
1041
1042 // Copy initialization expressions.
1043 Stmt **Stored = getStoredStmts();
1044 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1045 *Stored++ = CaptureInits[I];
1046
1047 // Copy the statement being captured.
1048 *Stored = S;
1049
1050 // Copy all Capture objects.
1051 Capture *Buffer = getStoredCaptures();
1052 std::copy(Captures.begin(), Captures.end(), Buffer);
1053}
1054
1055CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1056 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001057 CapDeclAndKind(0, CR_Default), TheRecordDecl(0) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001058 getStoredStmts()[NumCaptures] = 0;
1059}
1060
1061CapturedStmt *CapturedStmt::Create(ASTContext &Context, Stmt *S,
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001062 CapturedRegionKind Kind,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001063 ArrayRef<Capture> Captures,
1064 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001065 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001066 RecordDecl *RD) {
1067 // The layout is
1068 //
1069 // -----------------------------------------------------------
1070 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1071 // ----------------^-------------------^----------------------
1072 // getStoredStmts() getStoredCaptures()
1073 //
1074 // where S is the statement being captured.
1075 //
1076 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1077
1078 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1079 if (!Captures.empty()) {
1080 // Realign for the following Capture array.
1081 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1082 Size += sizeof(Capture) * Captures.size();
1083 }
1084
1085 void *Mem = Context.Allocate(Size);
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001086 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001087}
1088
1089CapturedStmt *CapturedStmt::CreateDeserialized(ASTContext &Context,
1090 unsigned NumCaptures) {
1091 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1092 if (NumCaptures > 0) {
1093 // Realign for the following Capture array.
1094 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1095 Size += sizeof(Capture) * NumCaptures;
1096 }
1097
1098 void *Mem = Context.Allocate(Size);
1099 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1100}
1101
1102Stmt::child_range CapturedStmt::children() {
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001103 // Children are captured field initilizers.
1104 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001105}
1106
1107bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Ben Langmuirdc5be4f2013-05-03 19:20:19 +00001108 for (const_capture_iterator I = capture_begin(),
1109 E = capture_end(); I != E; ++I) {
Richard Smith0d8e9642013-05-16 06:20:58 +00001110 if (!I->capturesVariable())
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001111 continue;
1112
1113 // This does not handle variable redeclarations. This should be
1114 // extended to capture variables with redeclarations, for example
1115 // a thread-private variable in OpenMP.
1116 if (I->getCapturedVar() == Var)
1117 return true;
1118 }
1119
1120 return false;
1121}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001122
1123OMPPrivateClause *OMPPrivateClause::Create(ASTContext &C,
1124 SourceLocation StartLoc,
1125 SourceLocation LParenLoc,
1126 SourceLocation EndLoc,
1127 ArrayRef<Expr *> VL) {
1128 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * VL.size(),
1129 llvm::alignOf<OMPPrivateClause>());
1130 OMPPrivateClause *Clause = new (Mem) OMPPrivateClause(StartLoc, LParenLoc,
1131 EndLoc, VL.size());
1132 Clause->setVarRefs(VL);
1133 return Clause;
1134}
1135
1136OMPPrivateClause *OMPPrivateClause::CreateEmpty(ASTContext &C,
1137 unsigned N) {
1138 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * N,
1139 llvm::alignOf<OMPPrivateClause>());
1140 return new (Mem) OMPPrivateClause(N);
1141}
1142
1143void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
1144 assert(Clauses.size() == this->Clauses.size() &&
1145 "Number of clauses is not the same as the preallocated buffer");
1146 std::copy(Clauses.begin(), Clauses.end(), this->Clauses.begin());
1147}
1148
1149OMPParallelDirective *OMPParallelDirective::Create(
1150 ASTContext &C,
1151 SourceLocation StartLoc,
1152 SourceLocation EndLoc,
1153 ArrayRef<OMPClause *> Clauses,
1154 Stmt *AssociatedStmt) {
1155 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1156 sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *),
1157 llvm::alignOf<OMPParallelDirective>());
1158 OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1159 Clauses.size());
1160 Dir->setClauses(Clauses);
1161 Dir->setAssociatedStmt(AssociatedStmt);
1162 return Dir;
1163}
1164
1165OMPParallelDirective *OMPParallelDirective::CreateEmpty(ASTContext &C,
1166 unsigned N,
1167 EmptyShell) {
1168 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1169 sizeof(OMPClause *) * N + sizeof(Stmt *),
1170 llvm::alignOf<OMPParallelDirective>());
1171 return new (Mem) OMPParallelDirective(N);
1172}