blob: 2711e9ba979da18413136ebcf5ed2608699bd0e0 [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 Topper8b4b98b2013-08-22 06:02:26 +0000267void CompoundStmt::setStmts(const ASTContext &C, Stmt **Stmts,
268 unsigned NumStmts) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000269 if (this->Body)
270 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000271 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000272
273 Body = new (C) Stmt*[NumStmts];
274 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
275}
Reid Spencer5f016e22007-07-11 17:01:13 +0000276
Reid Spencer5f016e22007-07-11 17:01:13 +0000277const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000278 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000279}
280
Craig Topper0b861562013-08-22 05:28:54 +0000281AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko49908902012-07-09 10:04:07 +0000282 ArrayRef<const Attr*> Attrs,
283 Stmt *SubStmt) {
284 void *Mem = C.Allocate(sizeof(AttributedStmt) +
285 sizeof(Attr*) * (Attrs.size() - 1),
286 llvm::alignOf<AttributedStmt>());
287 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
288}
289
Craig Topper0b861562013-08-22 05:28:54 +0000290AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
291 unsigned NumAttrs) {
Alexander Kornienko49908902012-07-09 10:04:07 +0000292 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
293 void *Mem = C.Allocate(sizeof(AttributedStmt) +
294 sizeof(Attr*) * (NumAttrs - 1),
295 llvm::alignOf<AttributedStmt>());
296 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
297}
298
Craig Topper8b4b98b2013-08-22 06:02:26 +0000299std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000300 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
301 return gccAsmStmt->generateAsmString(C);
302 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
303 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000304 llvm_unreachable("unknown asm statement kind!");
305}
306
307StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000308 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
309 return gccAsmStmt->getOutputConstraint(i);
310 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
311 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000312 llvm_unreachable("unknown asm statement kind!");
313}
314
315const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000316 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
317 return gccAsmStmt->getOutputExpr(i);
318 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
319 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000320 llvm_unreachable("unknown asm statement kind!");
321}
322
323StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000324 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
325 return gccAsmStmt->getInputConstraint(i);
326 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
327 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000328 llvm_unreachable("unknown asm statement kind!");
329}
330
331const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000332 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
333 return gccAsmStmt->getInputExpr(i);
334 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
335 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000336 llvm_unreachable("unknown asm statement kind!");
337}
338
339StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000340 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
341 return gccAsmStmt->getClobber(i);
342 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
343 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000344 llvm_unreachable("unknown asm statement kind!");
345}
346
Chad Rosierc4fb2212012-08-28 00:24:05 +0000347/// getNumPlusOperands - Return the number of output operands that have a "+"
348/// constraint.
349unsigned AsmStmt::getNumPlusOperands() const {
350 unsigned Res = 0;
351 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
352 if (isOutputPlusConstraint(i))
353 ++Res;
354 return Res;
355}
356
Chad Rosier33f05582012-08-27 23:47:56 +0000357StringRef GCCAsmStmt::getClobber(unsigned i) const {
358 return getClobberStringLiteral(i)->getString();
359}
360
Chad Rosierdf5faf52012-08-25 00:11:56 +0000361Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000362 return cast<Expr>(Exprs[i]);
363}
Chris Lattnerb3277932009-03-10 04:59:06 +0000364
365/// getOutputConstraint - Return the constraint string for the specified
366/// output operand. All output constraints are known to be non-empty (either
367/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000368StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000369 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000370}
Chris Lattnerb3277932009-03-10 04:59:06 +0000371
Chad Rosierdf5faf52012-08-25 00:11:56 +0000372Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000373 return cast<Expr>(Exprs[i + NumOutputs]);
374}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000375void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000376 Exprs[i + NumOutputs] = E;
377}
378
Chris Lattnerb3277932009-03-10 04:59:06 +0000379/// getInputConstraint - Return the specified input constraint. Unlike output
380/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000381StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000382 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000383}
384
Craig Topper8b4b98b2013-08-22 06:02:26 +0000385void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
386 IdentifierInfo **Names,
387 StringLiteral **Constraints,
388 Stmt **Exprs,
389 unsigned NumOutputs,
390 unsigned NumInputs,
391 StringLiteral **Clobbers,
392 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000393 this->NumOutputs = NumOutputs;
394 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000395 this->NumClobbers = NumClobbers;
396
397 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000398
Anders Carlsson966146e2010-01-30 23:19:41 +0000399 C.Deallocate(this->Names);
400 this->Names = new (C) IdentifierInfo*[NumExprs];
401 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000402
Anders Carlsson966146e2010-01-30 23:19:41 +0000403 C.Deallocate(this->Exprs);
404 this->Exprs = new (C) Stmt*[NumExprs];
405 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000406
Anders Carlsson966146e2010-01-30 23:19:41 +0000407 C.Deallocate(this->Constraints);
408 this->Constraints = new (C) StringLiteral*[NumExprs];
409 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000410
Anders Carlsson966146e2010-01-30 23:19:41 +0000411 C.Deallocate(this->Clobbers);
412 this->Clobbers = new (C) StringLiteral*[NumClobbers];
413 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000414}
415
Chris Lattner10ca96a2009-03-10 06:33:24 +0000416/// getNamedOperand - Given a symbolic operand reference like %[foo],
417/// translate this into a numeric value needed to reference the same operand.
418/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000419int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000420 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattner10ca96a2009-03-10 06:33:24 +0000422 // Check if this is an output operand.
423 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
424 if (getOutputName(i) == SymbolicName)
425 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000426 }
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattner10ca96a2009-03-10 06:33:24 +0000428 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
429 if (getInputName(i) == SymbolicName)
430 return getNumOutputs() + NumPlusOperands + i;
431
432 // Not found.
433 return -1;
434}
435
Chris Lattner458cd9c2009-03-10 23:21:44 +0000436/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
437/// it into pieces. If the asm string is erroneous, emit errors and return
438/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000439unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topper8b4b98b2013-08-22 06:02:26 +0000440 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000441 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000442 const char *StrStart = Str.begin();
443 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000444 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattner458cd9c2009-03-10 23:21:44 +0000446 // "Simple" inline asms have no constraints or operands, just convert the asm
447 // string to escape $'s.
448 if (isSimple()) {
449 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000450 for (; CurPtr != StrEnd; ++CurPtr) {
451 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000452 case '$':
453 Result += "$$";
454 break;
455 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000456 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000457 break;
458 }
459 }
460 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000461 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000462 }
463
464 // CurStringPiece - The current string that we are building up as we scan the
465 // asm string.
466 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000468 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000469
Chris Lattner458cd9c2009-03-10 23:21:44 +0000470 while (1) {
471 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000472 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000473 if (!CurStringPiece.empty())
474 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000475 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattner3182db12009-03-10 23:51:40 +0000478 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000479 switch (CurChar) {
480 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000481 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
482 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
483 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000484 case '%':
485 break;
486 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000487 CurStringPiece += CurChar;
488 continue;
489 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000490
Chris Lattner458cd9c2009-03-10 23:21:44 +0000491 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000492 if (CurPtr == StrEnd) {
493 // % at end of string is invalid (no escape).
494 DiagOffs = CurPtr-StrStart-1;
495 return diag::err_asm_invalid_escape;
496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattner3182db12009-03-10 23:51:40 +0000498 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000499 if (EscapedChar == '%') { // %% -> %
500 // Escaped percentage sign.
501 CurStringPiece += '%';
502 continue;
503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Chris Lattner458cd9c2009-03-10 23:21:44 +0000505 if (EscapedChar == '=') { // %= -> Generate an unique ID.
506 CurStringPiece += "${:uid}";
507 continue;
508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattner458cd9c2009-03-10 23:21:44 +0000510 // Otherwise, we have an operand. If we have accumulated a string so far,
511 // add it to the Pieces list.
512 if (!CurStringPiece.empty()) {
513 Pieces.push_back(AsmStringPiece(CurStringPiece));
514 CurStringPiece.clear();
515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner458cd9c2009-03-10 23:21:44 +0000517 // Handle %x4 and %x[foo] by capturing x as the modifier character.
518 char Modifier = '\0';
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000519 if (isLetter(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000520 if (CurPtr == StrEnd) { // Premature end.
521 DiagOffs = CurPtr-StrStart-1;
522 return diag::err_asm_invalid_escape;
523 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000524 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000525 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000528 if (isDigit(EscapedChar)) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000529 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000530 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Chris Lattnercafc2222009-03-11 22:52:17 +0000532 --CurPtr;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000533 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000534 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Chris Lattner85759272009-03-11 00:23:13 +0000536 unsigned NumOperands =
537 getNumOutputs() + getNumPlusOperands() + getNumInputs();
538 if (N >= NumOperands) {
539 DiagOffs = CurPtr-StrStart-1;
540 return diag::err_asm_invalid_operand_number;
541 }
542
Chris Lattner458cd9c2009-03-10 23:21:44 +0000543 Pieces.push_back(AsmStringPiece(N, Modifier));
544 continue;
545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattner458cd9c2009-03-10 23:21:44 +0000547 // Handle %[foo], a symbolic operand reference.
548 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000549 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000551 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000552 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000553 if (NameEnd == 0)
554 return diag::err_asm_unterminated_symbolic_operand_name;
555 if (NameEnd == CurPtr)
556 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner5f9e2722011-07-23 10:55:15 +0000558 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattner458cd9c2009-03-10 23:21:44 +0000560 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000561 if (N == -1) {
562 // Verify that an operand with that name exists.
563 DiagOffs = CurPtr-StrStart;
564 return diag::err_asm_unknown_symbolic_operand_name;
565 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000566 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000568 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000569 continue;
570 }
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattner2ff0f422009-03-10 23:57:07 +0000572 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000573 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000574 }
575}
Chad Rosierda083b22012-08-27 20:23:31 +0000576
577/// Assemble final IR asm string (GCC-style).
Craig Topper8b4b98b2013-08-22 06:02:26 +0000578std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000579 // Analyze the asm string to decompose it into its pieces. We know that Sema
580 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000581 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000582 unsigned DiagOffs;
583 AnalyzeAsmString(Pieces, C, DiagOffs);
584
585 std::string AsmString;
586 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
587 if (Pieces[i].isString())
588 AsmString += Pieces[i].getString();
589 else if (Pieces[i].getModifier() == '\0')
590 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
591 else
592 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
593 Pieces[i].getModifier() + '}';
594 }
595 return AsmString;
596}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000597
Chad Rosierda083b22012-08-27 20:23:31 +0000598/// Assemble final IR asm string (MS-style).
Craig Topper8b4b98b2013-08-22 06:02:26 +0000599std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosierda083b22012-08-27 20:23:31 +0000600 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000601 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000602}
603
Chad Rosier633abb02012-08-24 00:07:09 +0000604Expr *MSAsmStmt::getOutputExpr(unsigned i) {
605 return cast<Expr>(Exprs[i]);
606}
607
608Expr *MSAsmStmt::getInputExpr(unsigned i) {
609 return cast<Expr>(Exprs[i + NumOutputs]);
610}
611void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
612 Exprs[i + NumOutputs] = E;
613}
614
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000615QualType CXXCatchStmt::getCaughtType() const {
616 if (ExceptionDecl)
617 return ExceptionDecl->getType();
618 return QualType();
619}
620
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000621//===----------------------------------------------------------------------===//
622// Constructors
623//===----------------------------------------------------------------------===//
624
Craig Topper0b861562013-08-22 05:28:54 +0000625GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
626 bool issimple, bool isvolatile, unsigned numoutputs,
627 unsigned numinputs, IdentifierInfo **names,
628 StringLiteral **constraints, Expr **exprs,
629 StringLiteral *asmstr, unsigned numclobbers,
630 StringLiteral **clobbers, SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000631 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
632 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000633
Chad Rosier0e2a8682012-08-07 23:12:23 +0000634 unsigned NumExprs = NumOutputs + NumInputs;
635
Anders Carlsson966146e2010-01-30 23:19:41 +0000636 Names = new (C) IdentifierInfo*[NumExprs];
637 std::copy(names, names + NumExprs, Names);
638
639 Exprs = new (C) Stmt*[NumExprs];
640 std::copy(exprs, exprs + NumExprs, Exprs);
641
642 Constraints = new (C) StringLiteral*[NumExprs];
643 std::copy(constraints, constraints + NumExprs, Constraints);
644
645 Clobbers = new (C) StringLiteral*[NumClobbers];
646 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000647}
648
Craig Topper0b861562013-08-22 05:28:54 +0000649MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier058ab172012-08-16 00:06:53 +0000650 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosiere54cba12012-10-16 21:55:39 +0000651 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallaeeacf72013-05-03 00:10:13 +0000652 unsigned numinputs,
Chad Rosiere54cba12012-10-16 21:55:39 +0000653 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
654 StringRef asmstr, ArrayRef<StringRef> clobbers,
655 SourceLocation endloc)
656 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
657 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
John McCallaeeacf72013-05-03 00:10:13 +0000658 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
Chad Rosier058ab172012-08-16 00:06:53 +0000659
John McCallaeeacf72013-05-03 00:10:13 +0000660 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
661}
Chad Rosier058ab172012-08-16 00:06:53 +0000662
Craig Topper0b861562013-08-22 05:28:54 +0000663static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
John McCallaeeacf72013-05-03 00:10:13 +0000664 size_t size = str.size();
665 char *buffer = new (C) char[size];
666 memcpy(buffer, str.data(), size);
667 return StringRef(buffer, size);
668}
669
Craig Topper0b861562013-08-22 05:28:54 +0000670void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallaeeacf72013-05-03 00:10:13 +0000671 ArrayRef<Token> asmtoks,
672 ArrayRef<StringRef> constraints,
673 ArrayRef<Expr*> exprs,
674 ArrayRef<StringRef> clobbers) {
675 assert(NumAsmToks == asmtoks.size());
676 assert(NumClobbers == clobbers.size());
677
678 unsigned NumExprs = exprs.size();
679 assert(NumExprs == NumOutputs + NumInputs);
680 assert(NumExprs == constraints.size());
681
682 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier79efe242012-08-07 00:29:06 +0000683
Chad Rosier633abb02012-08-24 00:07:09 +0000684 Exprs = new (C) Stmt*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000685 for (unsigned i = 0, e = NumExprs; i != e; ++i)
686 Exprs[i] = exprs[i];
Chad Rosier633abb02012-08-24 00:07:09 +0000687
Chad Rosier79efe242012-08-07 00:29:06 +0000688 AsmToks = new (C) Token[NumAsmToks];
689 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
690 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000691
Chad Rosier89fb6d72012-08-28 20:28:20 +0000692 Constraints = new (C) StringRef[NumExprs];
693 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
John McCallaeeacf72013-05-03 00:10:13 +0000694 Constraints[i] = copyIntoContext(C, constraints[i]);
Chad Rosier89fb6d72012-08-28 20:28:20 +0000695 }
696
Chad Rosier33c72e12012-08-10 21:36:25 +0000697 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000698 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
699 // FIXME: Avoid the allocation/copy if at all possible.
John McCallaeeacf72013-05-03 00:10:13 +0000700 Clobbers[i] = copyIntoContext(C, clobbers[i]);
Chad Rosiere790bc32012-08-10 21:06:19 +0000701 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000702}
703
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000704ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
705 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000706 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000707: Stmt(ObjCForCollectionStmtClass) {
708 SubExprs[ELEM] = Elem;
709 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
710 SubExprs[BODY] = Body;
711 ForLoc = FCL;
712 RParenLoc = RPL;
713}
714
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000715ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
716 Stmt **CatchStmts, unsigned NumCatchStmts,
717 Stmt *atFinallyStmt)
718 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
719 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
720{
721 Stmt **Stmts = getStmts();
722 Stmts[0] = atTryStmt;
723 for (unsigned I = 0; I != NumCatchStmts; ++I)
724 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000725
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000726 if (HasFinally)
727 Stmts[NumCatchStmts + 1] = atFinallyStmt;
728}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000729
Craig Topper0b861562013-08-22 05:28:54 +0000730ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000731 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000732 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000733 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000734 unsigned NumCatchStmts,
735 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000736 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000737 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000738 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000739 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
740 atFinallyStmt);
741}
Ted Kremenekff981022008-02-01 21:28:59 +0000742
Craig Topper0b861562013-08-22 05:28:54 +0000743ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
744 unsigned NumCatchStmts,
745 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000746 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000747 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000748 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000749 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000750}
Nico Weber608b17f2008-08-05 23:15:29 +0000751
Erik Verbruggen65d78312012-12-25 14:51:39 +0000752SourceLocation ObjCAtTryStmt::getLocEnd() const {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000753 if (HasFinally)
Erik Verbruggen65d78312012-12-25 14:51:39 +0000754 return getFinallyStmt()->getLocEnd();
755 if (NumCatchStmts)
756 return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
757 return getTryBody()->getLocEnd();
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000758}
759
Craig Topper0b861562013-08-22 05:28:54 +0000760CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
Nico Weber07cf58c2012-12-29 20:13:03 +0000761 Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000762 std::size_t Size = sizeof(CXXTryStmt);
Nico Weber07cf58c2012-12-29 20:13:03 +0000763 Size += ((handlers.size() + 1) * sizeof(Stmt));
Sam Weiniga1a396d2010-02-03 03:56:39 +0000764
Chris Lattner32488542010-10-30 05:14:06 +0000765 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Nico Weber07cf58c2012-12-29 20:13:03 +0000766 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
Sam Weiniga1a396d2010-02-03 03:56:39 +0000767}
768
Craig Topper0b861562013-08-22 05:28:54 +0000769CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000770 unsigned numHandlers) {
771 std::size_t Size = sizeof(CXXTryStmt);
772 Size += ((numHandlers + 1) * sizeof(Stmt));
773
Chris Lattner32488542010-10-30 05:14:06 +0000774 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000775 return new (Mem) CXXTryStmt(Empty, numHandlers);
776}
777
Sam Weiniga1a396d2010-02-03 03:56:39 +0000778CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Nico Weber07cf58c2012-12-29 20:13:03 +0000779 ArrayRef<Stmt*> handlers)
780 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000781 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000782 Stmts[0] = tryBlock;
Nico Weber07cf58c2012-12-29 20:13:03 +0000783 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000784}
785
Richard Smithad762fc2011-04-14 22:09:26 +0000786CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
787 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
788 Stmt *Body, SourceLocation FL,
789 SourceLocation CL, SourceLocation RPL)
790 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
791 SubExprs[RANGE] = Range;
792 SubExprs[BEGINEND] = BeginEndStmt;
793 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
794 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
795 SubExprs[LOOPVAR] = LoopVar;
796 SubExprs[BODY] = Body;
797}
798
799Expr *CXXForRangeStmt::getRangeInit() {
800 DeclStmt *RangeStmt = getRangeStmt();
801 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
802 assert(RangeDecl &&& "for-range should have a single var decl");
803 return RangeDecl->getInit();
804}
805
806const Expr *CXXForRangeStmt::getRangeInit() const {
807 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
808}
809
810VarDecl *CXXForRangeStmt::getLoopVariable() {
811 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
812 assert(LV && "No loop variable in CXXForRangeStmt");
813 return cast<VarDecl>(LV);
814}
815
816const VarDecl *CXXForRangeStmt::getLoopVariable() const {
817 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
818}
819
Craig Topper0b861562013-08-22 05:28:54 +0000820IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000821 Stmt *then, SourceLocation EL, Stmt *elsev)
822 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000823{
824 setConditionVariable(C, var);
825 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
826 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000827 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000828}
829
830VarDecl *IfStmt::getConditionVariable() const {
831 if (!SubExprs[VAR])
832 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000833
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000834 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
835 return cast<VarDecl>(DS->getSingleDecl());
836}
837
Craig Topper0b861562013-08-22 05:28:54 +0000838void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000839 if (!V) {
840 SubExprs[VAR] = 0;
841 return;
842 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000843
Daniel Dunbar96a00142012-03-09 18:35:03 +0000844 SourceRange VarRange = V->getSourceRange();
845 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
846 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000847}
848
Craig Topper0b861562013-08-22 05:28:54 +0000849ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000850 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000851 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000852 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000853{
854 SubExprs[INIT] = Init;
855 setConditionVariable(C, condVar);
856 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
857 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
858 SubExprs[BODY] = Body;
859}
860
861VarDecl *ForStmt::getConditionVariable() const {
862 if (!SubExprs[CONDVAR])
863 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000864
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000865 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
866 return cast<VarDecl>(DS->getSingleDecl());
867}
868
Craig Topper0b861562013-08-22 05:28:54 +0000869void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000870 if (!V) {
871 SubExprs[CONDVAR] = 0;
872 return;
873 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000874
Daniel Dunbar96a00142012-03-09 18:35:03 +0000875 SourceRange VarRange = V->getSourceRange();
876 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
877 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000878}
879
Craig Topper0b861562013-08-22 05:28:54 +0000880SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000881 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000882{
883 setConditionVariable(C, Var);
884 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
885 SubExprs[BODY] = NULL;
886}
887
888VarDecl *SwitchStmt::getConditionVariable() const {
889 if (!SubExprs[VAR])
890 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000891
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000892 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
893 return cast<VarDecl>(DS->getSingleDecl());
894}
895
Craig Topper0b861562013-08-22 05:28:54 +0000896void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000897 if (!V) {
898 SubExprs[VAR] = 0;
899 return;
900 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000901
Daniel Dunbar96a00142012-03-09 18:35:03 +0000902 SourceRange VarRange = V->getSourceRange();
903 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
904 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000905}
906
John McCall63c00d72011-02-09 08:16:59 +0000907Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000908 if (isa<CaseStmt>(this))
909 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000910 return cast<DefaultStmt>(this)->getSubStmt();
911}
912
Craig Topper0b861562013-08-22 05:28:54 +0000913WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000914 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000915 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000916 setConditionVariable(C, Var);
917 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
918 SubExprs[BODY] = body;
919 WhileLoc = WL;
920}
921
922VarDecl *WhileStmt::getConditionVariable() const {
923 if (!SubExprs[VAR])
924 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000925
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000926 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
927 return cast<VarDecl>(DS->getSingleDecl());
928}
929
Craig Topper0b861562013-08-22 05:28:54 +0000930void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000931 if (!V) {
932 SubExprs[VAR] = 0;
933 return;
934 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000935
936 SourceRange VarRange = V->getSourceRange();
937 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
938 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000939}
940
Ted Kremenek82977772007-08-24 21:09:09 +0000941// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000942LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000943 if (AddrLabelExpr *E =
944 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
945 return E->getLabel();
946 return 0;
947}
Ted Kremenek82977772007-08-24 21:09:09 +0000948
Ted Kremenek82977772007-08-24 21:09:09 +0000949// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000950const Expr* ReturnStmt::getRetValue() const {
951 return cast_or_null<Expr>(RetExpr);
952}
953Expr* ReturnStmt::getRetValue() {
954 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000955}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000956
957SEHTryStmt::SEHTryStmt(bool IsCXXTry,
958 SourceLocation TryLoc,
959 Stmt *TryBlock,
960 Stmt *Handler)
961 : Stmt(SEHTryStmtClass),
962 IsCXXTry(IsCXXTry),
963 TryLoc(TryLoc)
964{
965 Children[TRY] = TryBlock;
966 Children[HANDLER] = Handler;
967}
968
Craig Topper0b861562013-08-22 05:28:54 +0000969SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
970 SourceLocation TryLoc, Stmt *TryBlock,
John Wiegley28bbe4b2011-04-28 01:08:34 +0000971 Stmt *Handler) {
972 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
973}
974
975SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
976 return dyn_cast<SEHExceptStmt>(getHandler());
977}
978
979SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
980 return dyn_cast<SEHFinallyStmt>(getHandler());
981}
982
983SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
984 Expr *FilterExpr,
985 Stmt *Block)
986 : Stmt(SEHExceptStmtClass),
987 Loc(Loc)
988{
989 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
990 Children[BLOCK] = Block;
991}
992
Craig Topper0b861562013-08-22 05:28:54 +0000993SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
994 Expr *FilterExpr, Stmt *Block) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000995 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
996}
997
998SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
999 Stmt *Block)
1000 : Stmt(SEHFinallyStmtClass),
1001 Loc(Loc),
1002 Block(Block)
1003{}
1004
Craig Topper0b861562013-08-22 05:28:54 +00001005SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley28bbe4b2011-04-28 01:08:34 +00001006 Stmt *Block) {
1007 return new(C)SEHFinallyStmt(Loc,Block);
1008}
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001009
1010CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1011 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1012
1013 // Offset of the first Capture object.
1014 unsigned FirstCaptureOffset =
1015 llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1016
1017 return reinterpret_cast<Capture *>(
1018 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1019 + FirstCaptureOffset);
1020}
1021
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001022CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1023 ArrayRef<Capture> Captures,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001024 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001025 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001026 RecordDecl *RD)
1027 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001028 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001029 assert( S && "null captured statement");
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001030 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001031 assert(RD && "null record declaration for captured statement");
1032
1033 // Copy initialization expressions.
1034 Stmt **Stored = getStoredStmts();
1035 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1036 *Stored++ = CaptureInits[I];
1037
1038 // Copy the statement being captured.
1039 *Stored = S;
1040
1041 // Copy all Capture objects.
1042 Capture *Buffer = getStoredCaptures();
1043 std::copy(Captures.begin(), Captures.end(), Buffer);
1044}
1045
1046CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1047 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001048 CapDeclAndKind(0, CR_Default), TheRecordDecl(0) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001049 getStoredStmts()[NumCaptures] = 0;
1050}
1051
Craig Topper0b861562013-08-22 05:28:54 +00001052CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001053 CapturedRegionKind Kind,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001054 ArrayRef<Capture> Captures,
1055 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001056 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001057 RecordDecl *RD) {
1058 // The layout is
1059 //
1060 // -----------------------------------------------------------
1061 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1062 // ----------------^-------------------^----------------------
1063 // getStoredStmts() getStoredCaptures()
1064 //
1065 // where S is the statement being captured.
1066 //
1067 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1068
1069 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1070 if (!Captures.empty()) {
1071 // Realign for the following Capture array.
1072 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1073 Size += sizeof(Capture) * Captures.size();
1074 }
1075
1076 void *Mem = Context.Allocate(Size);
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001077 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001078}
1079
Craig Topper0b861562013-08-22 05:28:54 +00001080CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001081 unsigned NumCaptures) {
1082 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1083 if (NumCaptures > 0) {
1084 // Realign for the following Capture array.
1085 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1086 Size += sizeof(Capture) * NumCaptures;
1087 }
1088
1089 void *Mem = Context.Allocate(Size);
1090 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1091}
1092
1093Stmt::child_range CapturedStmt::children() {
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001094 // Children are captured field initilizers.
1095 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001096}
1097
1098bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Ben Langmuirdc5be4f2013-05-03 19:20:19 +00001099 for (const_capture_iterator I = capture_begin(),
1100 E = capture_end(); I != E; ++I) {
Richard Smith0d8e9642013-05-16 06:20:58 +00001101 if (!I->capturesVariable())
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001102 continue;
1103
1104 // This does not handle variable redeclarations. This should be
1105 // extended to capture variables with redeclarations, for example
1106 // a thread-private variable in OpenMP.
1107 if (I->getCapturedVar() == Var)
1108 return true;
1109 }
1110
1111 return false;
1112}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001113
Craig Topper0b861562013-08-22 05:28:54 +00001114OMPPrivateClause *OMPPrivateClause::Create(const ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001115 SourceLocation StartLoc,
1116 SourceLocation LParenLoc,
1117 SourceLocation EndLoc,
1118 ArrayRef<Expr *> VL) {
1119 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * VL.size(),
1120 llvm::alignOf<OMPPrivateClause>());
1121 OMPPrivateClause *Clause = new (Mem) OMPPrivateClause(StartLoc, LParenLoc,
1122 EndLoc, VL.size());
1123 Clause->setVarRefs(VL);
1124 return Clause;
1125}
1126
Craig Topper0b861562013-08-22 05:28:54 +00001127OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001128 unsigned N) {
1129 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * N,
1130 llvm::alignOf<OMPPrivateClause>());
1131 return new (Mem) OMPPrivateClause(N);
1132}
1133
1134void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
1135 assert(Clauses.size() == this->Clauses.size() &&
1136 "Number of clauses is not the same as the preallocated buffer");
1137 std::copy(Clauses.begin(), Clauses.end(), this->Clauses.begin());
1138}
1139
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001140OMPParallelDirective *OMPParallelDirective::Create(
Craig Topper0b861562013-08-22 05:28:54 +00001141 const ASTContext &C,
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001142 SourceLocation StartLoc,
1143 SourceLocation EndLoc,
1144 ArrayRef<OMPClause *> Clauses,
1145 Stmt *AssociatedStmt) {
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001146 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1147 sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *),
1148 llvm::alignOf<OMPParallelDirective>());
1149 OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1150 Clauses.size());
1151 Dir->setClauses(Clauses);
1152 Dir->setAssociatedStmt(AssociatedStmt);
1153 return Dir;
1154}
1155
Craig Topper0b861562013-08-22 05:28:54 +00001156OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001157 unsigned N,
1158 EmptyShell) {
1159 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1160 sizeof(OMPClause *) * N + sizeof(Stmt *),
1161 llvm::alignOf<OMPParallelDirective>());
1162 return new (Mem) OMPParallelDirective(N);
1163}