blob: 9b271c81acdafdf393f512dc0a31d36b1078ade0 [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 Topper9b9a5bf2013-08-21 04:01:01 +0000251CompoundStmt::CompoundStmt(ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000252 SourceLocation LB, SourceLocation RB)
253 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
Nico Weberd36aa352012-12-29 20:03:39 +0000254 CompoundStmtBits.NumStmts = Stmts.size();
255 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000256 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
257
Nico Weberd36aa352012-12-29 20:03:39 +0000258 if (Stmts.size() == 0) {
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000259 Body = 0;
260 return;
261 }
262
Nico Weberd36aa352012-12-29 20:03:39 +0000263 Body = new (C) Stmt*[Stmts.size()];
264 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000265}
266
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000267void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000268 if (this->Body)
269 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000270 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000271
272 Body = new (C) Stmt*[NumStmts];
273 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
274}
Reid Spencer5f016e22007-07-11 17:01:13 +0000275
Reid Spencer5f016e22007-07-11 17:01:13 +0000276const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000277 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000278}
279
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000280AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
Alexander Kornienko49908902012-07-09 10:04:07 +0000281 ArrayRef<const Attr*> Attrs,
282 Stmt *SubStmt) {
283 void *Mem = C.Allocate(sizeof(AttributedStmt) +
284 sizeof(Attr*) * (Attrs.size() - 1),
285 llvm::alignOf<AttributedStmt>());
286 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
287}
288
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000289AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
Alexander Kornienko49908902012-07-09 10:04:07 +0000290 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
291 void *Mem = C.Allocate(sizeof(AttributedStmt) +
292 sizeof(Attr*) * (NumAttrs - 1),
293 llvm::alignOf<AttributedStmt>());
294 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
295}
296
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000297std::string AsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000298 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
299 return gccAsmStmt->generateAsmString(C);
300 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
301 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000302 llvm_unreachable("unknown asm statement kind!");
303}
304
305StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000306 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
307 return gccAsmStmt->getOutputConstraint(i);
308 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
309 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000310 llvm_unreachable("unknown asm statement kind!");
311}
312
313const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000314 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
315 return gccAsmStmt->getOutputExpr(i);
316 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
317 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000318 llvm_unreachable("unknown asm statement kind!");
319}
320
321StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000322 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
323 return gccAsmStmt->getInputConstraint(i);
324 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
325 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000326 llvm_unreachable("unknown asm statement kind!");
327}
328
329const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000330 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
331 return gccAsmStmt->getInputExpr(i);
332 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
333 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000334 llvm_unreachable("unknown asm statement kind!");
335}
336
337StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000338 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
339 return gccAsmStmt->getClobber(i);
340 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
341 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000342 llvm_unreachable("unknown asm statement kind!");
343}
344
Chad Rosierc4fb2212012-08-28 00:24:05 +0000345/// getNumPlusOperands - Return the number of output operands that have a "+"
346/// constraint.
347unsigned AsmStmt::getNumPlusOperands() const {
348 unsigned Res = 0;
349 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
350 if (isOutputPlusConstraint(i))
351 ++Res;
352 return Res;
353}
354
Chad Rosier33f05582012-08-27 23:47:56 +0000355StringRef GCCAsmStmt::getClobber(unsigned i) const {
356 return getClobberStringLiteral(i)->getString();
357}
358
Chad Rosierdf5faf52012-08-25 00:11:56 +0000359Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000360 return cast<Expr>(Exprs[i]);
361}
Chris Lattnerb3277932009-03-10 04:59:06 +0000362
363/// getOutputConstraint - Return the constraint string for the specified
364/// output operand. All output constraints are known to be non-empty (either
365/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000366StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000367 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000368}
Chris Lattnerb3277932009-03-10 04:59:06 +0000369
Chad Rosierdf5faf52012-08-25 00:11:56 +0000370Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000371 return cast<Expr>(Exprs[i + NumOutputs]);
372}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000373void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000374 Exprs[i + NumOutputs] = E;
375}
376
Chris Lattnerb3277932009-03-10 04:59:06 +0000377/// getInputConstraint - Return the specified input constraint. Unlike output
378/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000379StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000380 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000381}
382
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000383void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
384 IdentifierInfo **Names,
385 StringLiteral **Constraints,
386 Stmt **Exprs,
387 unsigned NumOutputs,
388 unsigned NumInputs,
389 StringLiteral **Clobbers,
390 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000391 this->NumOutputs = NumOutputs;
392 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000393 this->NumClobbers = NumClobbers;
394
395 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000396
Anders Carlsson966146e2010-01-30 23:19:41 +0000397 C.Deallocate(this->Names);
398 this->Names = new (C) IdentifierInfo*[NumExprs];
399 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000400
Anders Carlsson966146e2010-01-30 23:19:41 +0000401 C.Deallocate(this->Exprs);
402 this->Exprs = new (C) Stmt*[NumExprs];
403 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000404
Anders Carlsson966146e2010-01-30 23:19:41 +0000405 C.Deallocate(this->Constraints);
406 this->Constraints = new (C) StringLiteral*[NumExprs];
407 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000408
Anders Carlsson966146e2010-01-30 23:19:41 +0000409 C.Deallocate(this->Clobbers);
410 this->Clobbers = new (C) StringLiteral*[NumClobbers];
411 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000412}
413
Chris Lattner10ca96a2009-03-10 06:33:24 +0000414/// getNamedOperand - Given a symbolic operand reference like %[foo],
415/// translate this into a numeric value needed to reference the same operand.
416/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000417int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000418 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner10ca96a2009-03-10 06:33:24 +0000420 // Check if this is an output operand.
421 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
422 if (getOutputName(i) == SymbolicName)
423 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner10ca96a2009-03-10 06:33:24 +0000426 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
427 if (getInputName(i) == SymbolicName)
428 return getNumOutputs() + NumPlusOperands + i;
429
430 // Not found.
431 return -1;
432}
433
Chris Lattner458cd9c2009-03-10 23:21:44 +0000434/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
435/// it into pieces. If the asm string is erroneous, emit errors and return
436/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000437unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000438 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000439 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000440 const char *StrStart = Str.begin();
441 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000442 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Chris Lattner458cd9c2009-03-10 23:21:44 +0000444 // "Simple" inline asms have no constraints or operands, just convert the asm
445 // string to escape $'s.
446 if (isSimple()) {
447 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000448 for (; CurPtr != StrEnd; ++CurPtr) {
449 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000450 case '$':
451 Result += "$$";
452 break;
453 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000454 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000455 break;
456 }
457 }
458 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000459 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000460 }
461
462 // CurStringPiece - The current string that we are building up as we scan the
463 // asm string.
464 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000466 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000467
Chris Lattner458cd9c2009-03-10 23:21:44 +0000468 while (1) {
469 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000470 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000471 if (!CurStringPiece.empty())
472 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000473 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattner3182db12009-03-10 23:51:40 +0000476 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000477 switch (CurChar) {
478 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000479 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
480 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
481 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000482 case '%':
483 break;
484 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000485 CurStringPiece += CurChar;
486 continue;
487 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000488
Chris Lattner458cd9c2009-03-10 23:21:44 +0000489 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000490 if (CurPtr == StrEnd) {
491 // % at end of string is invalid (no escape).
492 DiagOffs = CurPtr-StrStart-1;
493 return diag::err_asm_invalid_escape;
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattner3182db12009-03-10 23:51:40 +0000496 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000497 if (EscapedChar == '%') { // %% -> %
498 // Escaped percentage sign.
499 CurStringPiece += '%';
500 continue;
501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattner458cd9c2009-03-10 23:21:44 +0000503 if (EscapedChar == '=') { // %= -> Generate an unique ID.
504 CurStringPiece += "${:uid}";
505 continue;
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Chris Lattner458cd9c2009-03-10 23:21:44 +0000508 // Otherwise, we have an operand. If we have accumulated a string so far,
509 // add it to the Pieces list.
510 if (!CurStringPiece.empty()) {
511 Pieces.push_back(AsmStringPiece(CurStringPiece));
512 CurStringPiece.clear();
513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner458cd9c2009-03-10 23:21:44 +0000515 // Handle %x4 and %x[foo] by capturing x as the modifier character.
516 char Modifier = '\0';
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000517 if (isLetter(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000518 if (CurPtr == StrEnd) { // Premature end.
519 DiagOffs = CurPtr-StrStart-1;
520 return diag::err_asm_invalid_escape;
521 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000522 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000523 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000524 }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000526 if (isDigit(EscapedChar)) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000527 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000528 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattnercafc2222009-03-11 22:52:17 +0000530 --CurPtr;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000531 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000532 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattner85759272009-03-11 00:23:13 +0000534 unsigned NumOperands =
535 getNumOutputs() + getNumPlusOperands() + getNumInputs();
536 if (N >= NumOperands) {
537 DiagOffs = CurPtr-StrStart-1;
538 return diag::err_asm_invalid_operand_number;
539 }
540
Chris Lattner458cd9c2009-03-10 23:21:44 +0000541 Pieces.push_back(AsmStringPiece(N, Modifier));
542 continue;
543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattner458cd9c2009-03-10 23:21:44 +0000545 // Handle %[foo], a symbolic operand reference.
546 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000547 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000549 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000550 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000551 if (NameEnd == 0)
552 return diag::err_asm_unterminated_symbolic_operand_name;
553 if (NameEnd == CurPtr)
554 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Chris Lattner5f9e2722011-07-23 10:55:15 +0000556 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner458cd9c2009-03-10 23:21:44 +0000558 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000559 if (N == -1) {
560 // Verify that an operand with that name exists.
561 DiagOffs = CurPtr-StrStart;
562 return diag::err_asm_unknown_symbolic_operand_name;
563 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000564 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000566 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000567 continue;
568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner2ff0f422009-03-10 23:57:07 +0000570 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000571 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000572 }
573}
Chad Rosierda083b22012-08-27 20:23:31 +0000574
575/// Assemble final IR asm string (GCC-style).
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000576std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000577 // Analyze the asm string to decompose it into its pieces. We know that Sema
578 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000579 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000580 unsigned DiagOffs;
581 AnalyzeAsmString(Pieces, C, DiagOffs);
582
583 std::string AsmString;
584 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
585 if (Pieces[i].isString())
586 AsmString += Pieces[i].getString();
587 else if (Pieces[i].getModifier() == '\0')
588 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
589 else
590 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
591 Pieces[i].getModifier() + '}';
592 }
593 return AsmString;
594}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000595
Chad Rosierda083b22012-08-27 20:23:31 +0000596/// Assemble final IR asm string (MS-style).
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000597std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierda083b22012-08-27 20:23:31 +0000598 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000599 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000600}
601
Chad Rosier633abb02012-08-24 00:07:09 +0000602Expr *MSAsmStmt::getOutputExpr(unsigned i) {
603 return cast<Expr>(Exprs[i]);
604}
605
606Expr *MSAsmStmt::getInputExpr(unsigned i) {
607 return cast<Expr>(Exprs[i + NumOutputs]);
608}
609void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
610 Exprs[i + NumOutputs] = E;
611}
612
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000613QualType CXXCatchStmt::getCaughtType() const {
614 if (ExceptionDecl)
615 return ExceptionDecl->getType();
616 return QualType();
617}
618
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000619//===----------------------------------------------------------------------===//
620// Constructors
621//===----------------------------------------------------------------------===//
622
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000623GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
624 bool isvolatile, unsigned numoutputs, unsigned numinputs,
625 IdentifierInfo **names, StringLiteral **constraints,
626 Expr **exprs, StringLiteral *asmstr,
627 unsigned numclobbers, StringLiteral **clobbers,
Chad Rosierdf5faf52012-08-25 00:11:56 +0000628 SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000629 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
630 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000631
Chad Rosier0e2a8682012-08-07 23:12:23 +0000632 unsigned NumExprs = NumOutputs + NumInputs;
633
Anders Carlsson966146e2010-01-30 23:19:41 +0000634 Names = new (C) IdentifierInfo*[NumExprs];
635 std::copy(names, names + NumExprs, Names);
636
637 Exprs = new (C) Stmt*[NumExprs];
638 std::copy(exprs, exprs + NumExprs, Exprs);
639
640 Constraints = new (C) StringLiteral*[NumExprs];
641 std::copy(constraints, constraints + NumExprs, Constraints);
642
643 Clobbers = new (C) StringLiteral*[NumClobbers];
644 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000645}
646
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000647MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
Chad Rosier058ab172012-08-16 00:06:53 +0000648 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosiere54cba12012-10-16 21:55:39 +0000649 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallaeeacf72013-05-03 00:10:13 +0000650 unsigned numinputs,
Chad Rosiere54cba12012-10-16 21:55:39 +0000651 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
652 StringRef asmstr, ArrayRef<StringRef> clobbers,
653 SourceLocation endloc)
654 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
655 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
John McCallaeeacf72013-05-03 00:10:13 +0000656 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
Chad Rosier058ab172012-08-16 00:06:53 +0000657
John McCallaeeacf72013-05-03 00:10:13 +0000658 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
659}
Chad Rosier058ab172012-08-16 00:06:53 +0000660
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000661static StringRef copyIntoContext(ASTContext &C, StringRef str) {
John McCallaeeacf72013-05-03 00:10:13 +0000662 size_t size = str.size();
663 char *buffer = new (C) char[size];
664 memcpy(buffer, str.data(), size);
665 return StringRef(buffer, size);
666}
667
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000668void MSAsmStmt::initialize(ASTContext &C,
John McCallaeeacf72013-05-03 00:10:13 +0000669 StringRef asmstr,
670 ArrayRef<Token> asmtoks,
671 ArrayRef<StringRef> constraints,
672 ArrayRef<Expr*> exprs,
673 ArrayRef<StringRef> clobbers) {
674 assert(NumAsmToks == asmtoks.size());
675 assert(NumClobbers == clobbers.size());
676
677 unsigned NumExprs = exprs.size();
678 assert(NumExprs == NumOutputs + NumInputs);
679 assert(NumExprs == constraints.size());
680
681 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier79efe242012-08-07 00:29:06 +0000682
Chad Rosier633abb02012-08-24 00:07:09 +0000683 Exprs = new (C) Stmt*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000684 for (unsigned i = 0, e = NumExprs; i != e; ++i)
685 Exprs[i] = exprs[i];
Chad Rosier633abb02012-08-24 00:07:09 +0000686
Chad Rosier79efe242012-08-07 00:29:06 +0000687 AsmToks = new (C) Token[NumAsmToks];
688 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
689 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000690
Chad Rosier89fb6d72012-08-28 20:28:20 +0000691 Constraints = new (C) StringRef[NumExprs];
692 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
John McCallaeeacf72013-05-03 00:10:13 +0000693 Constraints[i] = copyIntoContext(C, constraints[i]);
Chad Rosier89fb6d72012-08-28 20:28:20 +0000694 }
695
Chad Rosier33c72e12012-08-10 21:36:25 +0000696 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000697 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
698 // FIXME: Avoid the allocation/copy if at all possible.
John McCallaeeacf72013-05-03 00:10:13 +0000699 Clobbers[i] = copyIntoContext(C, clobbers[i]);
Chad Rosiere790bc32012-08-10 21:06:19 +0000700 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000701}
702
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000703ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
704 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000705 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000706: Stmt(ObjCForCollectionStmtClass) {
707 SubExprs[ELEM] = Elem;
708 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
709 SubExprs[BODY] = Body;
710 ForLoc = FCL;
711 RParenLoc = RPL;
712}
713
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000714ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
715 Stmt **CatchStmts, unsigned NumCatchStmts,
716 Stmt *atFinallyStmt)
717 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
718 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
719{
720 Stmt **Stmts = getStmts();
721 Stmts[0] = atTryStmt;
722 for (unsigned I = 0; I != NumCatchStmts; ++I)
723 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000724
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000725 if (HasFinally)
726 Stmts[NumCatchStmts + 1] = atFinallyStmt;
727}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000728
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000729ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000730 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000731 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000732 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000733 unsigned NumCatchStmts,
734 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000735 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000736 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000737 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000738 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
739 atFinallyStmt);
740}
Ted Kremenekff981022008-02-01 21:28:59 +0000741
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000742ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
743 unsigned NumCatchStmts,
744 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000745 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000746 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000747 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000748 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000749}
Nico Weber608b17f2008-08-05 23:15:29 +0000750
Erik Verbruggen65d78312012-12-25 14:51:39 +0000751SourceLocation ObjCAtTryStmt::getLocEnd() const {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000752 if (HasFinally)
Erik Verbruggen65d78312012-12-25 14:51:39 +0000753 return getFinallyStmt()->getLocEnd();
754 if (NumCatchStmts)
755 return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
756 return getTryBody()->getLocEnd();
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000757}
758
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000759CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Nico Weber07cf58c2012-12-29 20:13:03 +0000760 Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000761 std::size_t Size = sizeof(CXXTryStmt);
Nico Weber07cf58c2012-12-29 20:13:03 +0000762 Size += ((handlers.size() + 1) * sizeof(Stmt));
Sam Weiniga1a396d2010-02-03 03:56:39 +0000763
Chris Lattner32488542010-10-30 05:14:06 +0000764 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Nico Weber07cf58c2012-12-29 20:13:03 +0000765 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
Sam Weiniga1a396d2010-02-03 03:56:39 +0000766}
767
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000768CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000769 unsigned numHandlers) {
770 std::size_t Size = sizeof(CXXTryStmt);
771 Size += ((numHandlers + 1) * sizeof(Stmt));
772
Chris Lattner32488542010-10-30 05:14:06 +0000773 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000774 return new (Mem) CXXTryStmt(Empty, numHandlers);
775}
776
Sam Weiniga1a396d2010-02-03 03:56:39 +0000777CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Nico Weber07cf58c2012-12-29 20:13:03 +0000778 ArrayRef<Stmt*> handlers)
779 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000780 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000781 Stmts[0] = tryBlock;
Nico Weber07cf58c2012-12-29 20:13:03 +0000782 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000783}
784
Richard Smithad762fc2011-04-14 22:09:26 +0000785CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
786 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
787 Stmt *Body, SourceLocation FL,
788 SourceLocation CL, SourceLocation RPL)
789 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
790 SubExprs[RANGE] = Range;
791 SubExprs[BEGINEND] = BeginEndStmt;
792 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
793 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
794 SubExprs[LOOPVAR] = LoopVar;
795 SubExprs[BODY] = Body;
796}
797
798Expr *CXXForRangeStmt::getRangeInit() {
799 DeclStmt *RangeStmt = getRangeStmt();
800 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
801 assert(RangeDecl &&& "for-range should have a single var decl");
802 return RangeDecl->getInit();
803}
804
805const Expr *CXXForRangeStmt::getRangeInit() const {
806 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
807}
808
809VarDecl *CXXForRangeStmt::getLoopVariable() {
810 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
811 assert(LV && "No loop variable in CXXForRangeStmt");
812 return cast<VarDecl>(LV);
813}
814
815const VarDecl *CXXForRangeStmt::getLoopVariable() const {
816 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
817}
818
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000819IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000820 Stmt *then, SourceLocation EL, Stmt *elsev)
821 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000822{
823 setConditionVariable(C, var);
824 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
825 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000826 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000827}
828
829VarDecl *IfStmt::getConditionVariable() const {
830 if (!SubExprs[VAR])
831 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000832
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000833 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
834 return cast<VarDecl>(DS->getSingleDecl());
835}
836
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000837void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000838 if (!V) {
839 SubExprs[VAR] = 0;
840 return;
841 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000842
Daniel Dunbar96a00142012-03-09 18:35:03 +0000843 SourceRange VarRange = V->getSourceRange();
844 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
845 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000846}
847
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000848ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000849 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000850 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000851 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000852{
853 SubExprs[INIT] = Init;
854 setConditionVariable(C, condVar);
855 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
856 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
857 SubExprs[BODY] = Body;
858}
859
860VarDecl *ForStmt::getConditionVariable() const {
861 if (!SubExprs[CONDVAR])
862 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000863
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000864 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
865 return cast<VarDecl>(DS->getSingleDecl());
866}
867
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000868void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000869 if (!V) {
870 SubExprs[CONDVAR] = 0;
871 return;
872 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000873
Daniel Dunbar96a00142012-03-09 18:35:03 +0000874 SourceRange VarRange = V->getSourceRange();
875 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
876 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000877}
878
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000879SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000880 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000881{
882 setConditionVariable(C, Var);
883 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
884 SubExprs[BODY] = NULL;
885}
886
887VarDecl *SwitchStmt::getConditionVariable() const {
888 if (!SubExprs[VAR])
889 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000890
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000891 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
892 return cast<VarDecl>(DS->getSingleDecl());
893}
894
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000895void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000896 if (!V) {
897 SubExprs[VAR] = 0;
898 return;
899 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000900
Daniel Dunbar96a00142012-03-09 18:35:03 +0000901 SourceRange VarRange = V->getSourceRange();
902 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
903 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000904}
905
John McCall63c00d72011-02-09 08:16:59 +0000906Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000907 if (isa<CaseStmt>(this))
908 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000909 return cast<DefaultStmt>(this)->getSubStmt();
910}
911
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000912WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000913 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000914 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000915 setConditionVariable(C, Var);
916 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
917 SubExprs[BODY] = body;
918 WhileLoc = WL;
919}
920
921VarDecl *WhileStmt::getConditionVariable() const {
922 if (!SubExprs[VAR])
923 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000924
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000925 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
926 return cast<VarDecl>(DS->getSingleDecl());
927}
928
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000929void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000930 if (!V) {
931 SubExprs[VAR] = 0;
932 return;
933 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000934
935 SourceRange VarRange = V->getSourceRange();
936 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
937 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000938}
939
Ted Kremenek82977772007-08-24 21:09:09 +0000940// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000941LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000942 if (AddrLabelExpr *E =
943 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
944 return E->getLabel();
945 return 0;
946}
Ted Kremenek82977772007-08-24 21:09:09 +0000947
Ted Kremenek82977772007-08-24 21:09:09 +0000948// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000949const Expr* ReturnStmt::getRetValue() const {
950 return cast_or_null<Expr>(RetExpr);
951}
952Expr* ReturnStmt::getRetValue() {
953 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000954}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000955
956SEHTryStmt::SEHTryStmt(bool IsCXXTry,
957 SourceLocation TryLoc,
958 Stmt *TryBlock,
959 Stmt *Handler)
960 : Stmt(SEHTryStmtClass),
961 IsCXXTry(IsCXXTry),
962 TryLoc(TryLoc)
963{
964 Children[TRY] = TryBlock;
965 Children[HANDLER] = Handler;
966}
967
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000968SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
969 bool IsCXXTry,
970 SourceLocation TryLoc,
971 Stmt *TryBlock,
John Wiegley28bbe4b2011-04-28 01:08:34 +0000972 Stmt *Handler) {
973 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
974}
975
976SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
977 return dyn_cast<SEHExceptStmt>(getHandler());
978}
979
980SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
981 return dyn_cast<SEHFinallyStmt>(getHandler());
982}
983
984SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
985 Expr *FilterExpr,
986 Stmt *Block)
987 : Stmt(SEHExceptStmtClass),
988 Loc(Loc)
989{
990 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
991 Children[BLOCK] = Block;
992}
993
Craig Topper9b9a5bf2013-08-21 04:01:01 +0000994SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
995 SourceLocation Loc,
996 Expr *FilterExpr,
997 Stmt *Block) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000998 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
999}
1000
1001SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1002 Stmt *Block)
1003 : Stmt(SEHFinallyStmtClass),
1004 Loc(Loc),
1005 Block(Block)
1006{}
1007
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001008SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
1009 SourceLocation Loc,
John Wiegley28bbe4b2011-04-28 01:08:34 +00001010 Stmt *Block) {
1011 return new(C)SEHFinallyStmt(Loc,Block);
1012}
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001013
1014CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1015 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1016
1017 // Offset of the first Capture object.
1018 unsigned FirstCaptureOffset =
1019 llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1020
1021 return reinterpret_cast<Capture *>(
1022 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1023 + FirstCaptureOffset);
1024}
1025
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001026CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1027 ArrayRef<Capture> Captures,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001028 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001029 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001030 RecordDecl *RD)
1031 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001032 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001033 assert( S && "null captured statement");
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001034 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001035 assert(RD && "null record declaration for captured statement");
1036
1037 // Copy initialization expressions.
1038 Stmt **Stored = getStoredStmts();
1039 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1040 *Stored++ = CaptureInits[I];
1041
1042 // Copy the statement being captured.
1043 *Stored = S;
1044
1045 // Copy all Capture objects.
1046 Capture *Buffer = getStoredCaptures();
1047 std::copy(Captures.begin(), Captures.end(), Buffer);
1048}
1049
1050CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1051 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001052 CapDeclAndKind(0, CR_Default), TheRecordDecl(0) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001053 getStoredStmts()[NumCaptures] = 0;
1054}
1055
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001056CapturedStmt *CapturedStmt::Create(ASTContext &Context, Stmt *S,
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001057 CapturedRegionKind Kind,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001058 ArrayRef<Capture> Captures,
1059 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001060 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001061 RecordDecl *RD) {
1062 // The layout is
1063 //
1064 // -----------------------------------------------------------
1065 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1066 // ----------------^-------------------^----------------------
1067 // getStoredStmts() getStoredCaptures()
1068 //
1069 // where S is the statement being captured.
1070 //
1071 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1072
1073 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1074 if (!Captures.empty()) {
1075 // Realign for the following Capture array.
1076 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1077 Size += sizeof(Capture) * Captures.size();
1078 }
1079
1080 void *Mem = Context.Allocate(Size);
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001081 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001082}
1083
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001084CapturedStmt *CapturedStmt::CreateDeserialized(ASTContext &Context,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001085 unsigned NumCaptures) {
1086 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1087 if (NumCaptures > 0) {
1088 // Realign for the following Capture array.
1089 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1090 Size += sizeof(Capture) * NumCaptures;
1091 }
1092
1093 void *Mem = Context.Allocate(Size);
1094 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1095}
1096
1097Stmt::child_range CapturedStmt::children() {
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001098 // Children are captured field initilizers.
1099 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001100}
1101
1102bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Ben Langmuirdc5be4f2013-05-03 19:20:19 +00001103 for (const_capture_iterator I = capture_begin(),
1104 E = capture_end(); I != E; ++I) {
Richard Smith0d8e9642013-05-16 06:20:58 +00001105 if (!I->capturesVariable())
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001106 continue;
1107
1108 // This does not handle variable redeclarations. This should be
1109 // extended to capture variables with redeclarations, for example
1110 // a thread-private variable in OpenMP.
1111 if (I->getCapturedVar() == Var)
1112 return true;
1113 }
1114
1115 return false;
1116}
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001117
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001118OMPPrivateClause *OMPPrivateClause::Create(ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001119 SourceLocation StartLoc,
1120 SourceLocation LParenLoc,
1121 SourceLocation EndLoc,
1122 ArrayRef<Expr *> VL) {
1123 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * VL.size(),
1124 llvm::alignOf<OMPPrivateClause>());
1125 OMPPrivateClause *Clause = new (Mem) OMPPrivateClause(StartLoc, LParenLoc,
1126 EndLoc, VL.size());
1127 Clause->setVarRefs(VL);
1128 return Clause;
1129}
1130
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001131OMPPrivateClause *OMPPrivateClause::CreateEmpty(ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001132 unsigned N) {
1133 void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * N,
1134 llvm::alignOf<OMPPrivateClause>());
1135 return new (Mem) OMPPrivateClause(N);
1136}
1137
1138void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
1139 assert(Clauses.size() == this->Clauses.size() &&
1140 "Number of clauses is not the same as the preallocated buffer");
1141 std::copy(Clauses.begin(), Clauses.end(), this->Clauses.begin());
1142}
1143
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001144OMPParallelDirective *OMPParallelDirective::Create(
1145 ASTContext &C,
1146 SourceLocation StartLoc,
1147 SourceLocation EndLoc,
1148 ArrayRef<OMPClause *> Clauses,
1149 Stmt *AssociatedStmt) {
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001150 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1151 sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *),
1152 llvm::alignOf<OMPParallelDirective>());
1153 OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1154 Clauses.size());
1155 Dir->setClauses(Clauses);
1156 Dir->setAssociatedStmt(AssociatedStmt);
1157 return Dir;
1158}
1159
Craig Topper9b9a5bf2013-08-21 04:01:01 +00001160OMPParallelDirective *OMPParallelDirective::CreateEmpty(ASTContext &C,
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001161 unsigned N,
1162 EmptyShell) {
1163 void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
1164 sizeof(OMPClause *) * N + sizeof(Stmt *),
1165 llvm::alignOf<OMPParallelDirective>());
1166 return new (Mem) OMPParallelDirective(N);
1167}