blob: 205a3229ee1423f0aa31b5f67652d198d851a637 [file] [log] [blame]
Chris Lattnerf42cce72006-10-25 04:09:21 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf42cce72006-10-25 04:09:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer444a1302012-12-01 17:12:56 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTDiagnostic.h"
Chris Lattner29375652006-12-04 18:06:35 +000016#include "clang/AST/ExprCXX.h"
Steve Naroff021ca182008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000018#include "clang/AST/Stmt.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtObjC.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtOpenMP.h"
Sebastian Redl54c04d42008-12-22 19:15:10 +000022#include "clang/AST/Type.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
Chris Lattner1a8f3942010-04-23 16:29:58 +000024#include "clang/Basic/TargetInfo.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000025#include "clang/Lex/Token.h"
Chad Rosier14836ba2012-08-24 17:05:45 +000026#include "llvm/ADT/StringExtras.h"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnerf42cce72006-10-25 04:09:21 +000028using namespace clang;
29
Steve Narofff84d11f2007-05-23 21:48:04 +000030static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000031 const char *Name;
32 unsigned Counter;
33 unsigned Size;
Alexis Hunt656bb312010-05-05 15:24:00 +000034} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner4d15a0d2007-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;
Alexis Huntabb2ac82010-05-18 06:22:21 +000043#define ABSTRACT_STMT(STMT)
Douglas Gregorbe35ce92008-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);
Alexis Hunt656bb312010-05-05 15:24:00 +000047#include "clang/AST/StmtNodes.inc"
Nico Weberde565e32008-08-05 23:15:29 +000048
Chris Lattner4d15a0d2007-08-25 01:42:24 +000049 return StmtClassInfo[E];
50}
51
Craig Topper37932912013-08-18 10:09:15 +000052void *Stmt::operator new(size_t bytes, const ASTContext& C,
Craig Topper5a050012013-08-18 17:45:38 +000053 unsigned alignment) {
Benjamin Kramerea70eb32012-12-01 15:09:41 +000054 return ::operator new(bytes, C, alignment);
55}
56
Steve Narofff1e53692007-03-23 22:27:02 +000057const char *Stmt::getStmtClassName() const {
John McCall925b16622010-10-26 08:39:16 +000058 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000059}
Steve Narofff84d11f2007-05-23 21:48:04 +000060
61void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000062 // Ensure the table is primed.
63 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000064
Steve Narofff84d11f2007-05-23 21:48:04 +000065 unsigned sum = 0;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000066 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Alexis Hunt656bb312010-05-05 15:24:00 +000067 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000068 if (StmtClassInfo[i].Name == nullptr) continue;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000069 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000070 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000071 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000072 sum = 0;
Alexis Hunt656bb312010-05-05 15:24:00 +000073 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000074 if (StmtClassInfo[i].Name == nullptr) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +000075 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthbfb154a2011-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 Lattner4d15a0d2007-08-25 01:42:24 +000080 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +000081 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000082
83 llvm::errs() << "Total bytes = " << sum << "\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000084}
85
86void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000087 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000088}
89
Daniel Dunbar62905572012-03-05 21:42:49 +000090bool Stmt::StatisticsEnabled = false;
91void Stmt::EnableStatistics() {
92 StatisticsEnabled = true;
Steve Narofff84d11f2007-05-23 21:48:04 +000093}
94
John McCall4db5c3c2011-07-07 06:58:02 +000095Stmt *Stmt::IgnoreImplicit() {
96 Stmt *s = this;
97
Richard Smith520449d2015-02-05 06:15:50 +000098 if (auto *ewc = dyn_cast<ExprWithCleanups>(s))
John McCall4db5c3c2011-07-07 06:58:02 +000099 s = ewc->getSubExpr();
100
Richard Smith520449d2015-02-05 06:15:50 +0000101 if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s))
102 s = mte->GetTemporaryExpr();
103
104 if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s))
105 s = bte->getSubExpr();
106
107 while (auto *ice = dyn_cast<ImplicitCastExpr>(s))
John McCall4db5c3c2011-07-07 06:58:02 +0000108 s = ice->getSubExpr();
109
110 return s;
111}
112
Alexander Musmana5f070a2014-10-01 06:03:56 +0000113/// \brief Skip no-op (attributed, compound) container stmts and skip captured
114/// stmt at the top, if \a IgnoreCaptured is true.
115Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
116 Stmt *S = this;
117 if (IgnoreCaptured)
118 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
119 S = CapS->getCapturedStmt();
120 while (true) {
121 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
122 S = AS->getSubStmt();
123 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
124 if (CS->size() != 1)
125 break;
126 S = CS->body_back();
127 } else
128 break;
129 }
130 return S;
131}
132
Chandler Carrutha626d642011-09-10 00:02:34 +0000133/// \brief Strip off all label-like statements.
134///
Richard Smithc202b282012-04-14 00:33:13 +0000135/// This will strip off label statements, case statements, attributed
136/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000137const Stmt *Stmt::stripLabelLikeStatements() const {
138 const Stmt *S = this;
139 while (true) {
140 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
141 S = LS->getSubStmt();
142 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
143 S = SC->getSubStmt();
Richard Smithc202b282012-04-14 00:33:13 +0000144 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
145 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000146 else
147 return S;
148 }
149}
150
John McCallbd066782011-02-09 08:16:59 +0000151namespace {
152 struct good {};
153 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000154
155 // These silly little functions have to be static inline to suppress
156 // unused warnings, and they have to be defined to suppress other
157 // warnings.
Nick Lewyckybae992f2011-02-09 08:42:57 +0000158 static inline good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000159
160 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000161 template <class T> good implements_children(children_t T::*) {
162 return good();
163 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000164 LLVM_ATTRIBUTE_UNUSED
Nick Lewyckybae992f2011-02-09 08:42:57 +0000165 static inline bad implements_children(children_t Stmt::*) {
166 return bad();
167 }
John McCallbd066782011-02-09 08:16:59 +0000168
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000169 typedef SourceLocation getLocStart_t() const;
170 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000171 return good();
172 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000173 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000174 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
175 return bad();
176 }
177
178 typedef SourceLocation getLocEnd_t() const;
179 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
180 return good();
181 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000182 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000183 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000184 return bad();
185 }
John McCallbd066782011-02-09 08:16:59 +0000186
187#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000188 (void) is_good(implements_children(&type::children))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000189#define ASSERT_IMPLEMENTS_getLocStart(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000190 (void) is_good(implements_getLocStart(&type::getLocStart))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000191#define ASSERT_IMPLEMENTS_getLocEnd(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000192 (void) is_good(implements_getLocEnd(&type::getLocEnd))
John McCallbd066782011-02-09 08:16:59 +0000193}
194
195/// Check whether the various Stmt classes implement their member
196/// functions.
Eli Friedmandc41d792013-09-10 22:57:15 +0000197LLVM_ATTRIBUTE_UNUSED
John McCallbd066782011-02-09 08:16:59 +0000198static inline void check_implementations() {
199#define ABSTRACT_STMT(type)
200#define STMT(type, base) \
201 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000202 ASSERT_IMPLEMENTS_getLocStart(type); \
203 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCallbd066782011-02-09 08:16:59 +0000204#include "clang/AST/StmtNodes.inc"
205}
206
207Stmt::child_range Stmt::children() {
208 switch (getStmtClass()) {
209 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
210#define ABSTRACT_STMT(type)
211#define STMT(type, base) \
212 case Stmt::type##Class: \
213 return static_cast<type*>(this)->children();
214#include "clang/AST/StmtNodes.inc"
215 }
216 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000217}
218
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000219// Amusing macro metaprogramming hack: check whether a class provides
220// a more specific implementation of getSourceRange.
221//
222// See also Expr.cpp:getExprLoc().
223namespace {
224 /// This implementation is used when a class provides a custom
225 /// implementation of getSourceRange.
226 template <class S, class T>
227 SourceRange getSourceRangeImpl(const Stmt *stmt,
228 SourceRange (T::*v)() const) {
229 return static_cast<const S*>(stmt)->getSourceRange();
230 }
231
232 /// This implementation is used when a class doesn't provide a custom
233 /// implementation of getSourceRange. Overload resolution should pick it over
234 /// the implementation above because it's more specialized according to
235 /// function template partial ordering.
236 template <class S>
237 SourceRange getSourceRangeImpl(const Stmt *stmt,
238 SourceRange (Stmt::*v)() const) {
239 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
240 static_cast<const S*>(stmt)->getLocEnd());
241 }
242}
243
John McCallbd066782011-02-09 08:16:59 +0000244SourceRange Stmt::getSourceRange() const {
245 switch (getStmtClass()) {
246 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
247#define ABSTRACT_STMT(type)
248#define STMT(type, base) \
249 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000250 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000251#include "clang/AST/StmtNodes.inc"
252 }
253 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000254}
255
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000256SourceLocation Stmt::getLocStart() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000257// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000258 switch (getStmtClass()) {
259 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
260#define ABSTRACT_STMT(type)
261#define STMT(type, base) \
262 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000263 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000264#include "clang/AST/StmtNodes.inc"
265 }
266 llvm_unreachable("unknown statement kind");
267}
268
269SourceLocation Stmt::getLocEnd() const {
270 switch (getStmtClass()) {
271 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
272#define ABSTRACT_STMT(type)
273#define STMT(type, base) \
274 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000275 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000276#include "clang/AST/StmtNodes.inc"
277 }
278 llvm_unreachable("unknown statement kind");
279}
280
Craig Toppere6960e22013-08-22 05:28:54 +0000281CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000282 SourceLocation LB, SourceLocation RB)
Aaron Ballmance6c67e2014-10-22 21:06:18 +0000283 : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000284 CompoundStmtBits.NumStmts = Stmts.size();
285 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000286 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
287
Nico Webera2a0eb92012-12-29 20:03:39 +0000288 if (Stmts.size() == 0) {
Craig Topper36250ad2014-05-12 05:36:57 +0000289 Body = nullptr;
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000290 return;
291 }
292
Nico Webera2a0eb92012-12-29 20:03:39 +0000293 Body = new (C) Stmt*[Stmts.size()];
294 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000295}
296
Craig Topperc571c812013-08-22 06:02:26 +0000297void CompoundStmt::setStmts(const ASTContext &C, Stmt **Stmts,
298 unsigned NumStmts) {
Douglas Gregora9af1d12009-04-17 00:04:06 +0000299 if (this->Body)
300 C.Deallocate(Body);
John McCall925b16622010-10-26 08:39:16 +0000301 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregora9af1d12009-04-17 00:04:06 +0000302
303 Body = new (C) Stmt*[NumStmts];
304 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
305}
Steve Narofff84d11f2007-05-23 21:48:04 +0000306
Chris Lattnereefa10e2007-05-28 06:56:27 +0000307const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000308 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000309}
310
Craig Toppere6960e22013-08-22 05:28:54 +0000311AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000312 ArrayRef<const Attr*> Attrs,
313 Stmt *SubStmt) {
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000314 assert(!Attrs.empty() && "Attrs should not be empty");
315 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * Attrs.size(),
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000316 llvm::alignOf<AttributedStmt>());
317 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
318}
319
Craig Toppere6960e22013-08-22 05:28:54 +0000320AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
321 unsigned NumAttrs) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000322 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000323 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * NumAttrs,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000324 llvm::alignOf<AttributedStmt>());
325 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
326}
327
Craig Topperc571c812013-08-22 06:02:26 +0000328std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000329 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
330 return gccAsmStmt->generateAsmString(C);
331 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
332 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000333 llvm_unreachable("unknown asm statement kind!");
334}
335
336StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000337 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
338 return gccAsmStmt->getOutputConstraint(i);
339 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
340 return msAsmStmt->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000341 llvm_unreachable("unknown asm statement kind!");
342}
343
344const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000345 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
346 return gccAsmStmt->getOutputExpr(i);
347 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
348 return msAsmStmt->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000349 llvm_unreachable("unknown asm statement kind!");
350}
351
352StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000353 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
354 return gccAsmStmt->getInputConstraint(i);
355 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
356 return msAsmStmt->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000357 llvm_unreachable("unknown asm statement kind!");
358}
359
360const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000361 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
362 return gccAsmStmt->getInputExpr(i);
363 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
364 return msAsmStmt->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000365 llvm_unreachable("unknown asm statement kind!");
366}
367
368StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000369 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
370 return gccAsmStmt->getClobber(i);
371 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
372 return msAsmStmt->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000373 llvm_unreachable("unknown asm statement kind!");
374}
375
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000376/// getNumPlusOperands - Return the number of output operands that have a "+"
377/// constraint.
378unsigned AsmStmt::getNumPlusOperands() const {
379 unsigned Res = 0;
380 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
381 if (isOutputPlusConstraint(i))
382 ++Res;
383 return Res;
384}
385
Akira Hatanaka987f1862014-08-22 06:05:21 +0000386char GCCAsmStmt::AsmStringPiece::getModifier() const {
387 assert(isOperand() && "Only Operands can have modifiers.");
388 return isLetter(Str[0]) ? Str[0] : '\0';
389}
390
Chad Rosier6100ae12012-08-27 23:47:56 +0000391StringRef GCCAsmStmt::getClobber(unsigned i) const {
392 return getClobberStringLiteral(i)->getString();
393}
394
Chad Rosierde70e0e2012-08-25 00:11:56 +0000395Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000396 return cast<Expr>(Exprs[i]);
397}
Chris Lattner72bbf172009-03-10 04:59:06 +0000398
399/// getOutputConstraint - Return the constraint string for the specified
400/// output operand. All output constraints are known to be non-empty (either
401/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000402StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000403 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000404}
Chris Lattner72bbf172009-03-10 04:59:06 +0000405
Chad Rosierde70e0e2012-08-25 00:11:56 +0000406Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000407 return cast<Expr>(Exprs[i + NumOutputs]);
408}
Chad Rosierde70e0e2012-08-25 00:11:56 +0000409void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000410 Exprs[i + NumOutputs] = E;
411}
412
Chris Lattner72bbf172009-03-10 04:59:06 +0000413/// getInputConstraint - Return the specified input constraint. Unlike output
414/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000415StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000416 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000417}
418
Craig Topperc571c812013-08-22 06:02:26 +0000419void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
420 IdentifierInfo **Names,
421 StringLiteral **Constraints,
422 Stmt **Exprs,
423 unsigned NumOutputs,
424 unsigned NumInputs,
425 StringLiteral **Clobbers,
426 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000427 this->NumOutputs = NumOutputs;
428 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000429 this->NumClobbers = NumClobbers;
430
431 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000432
Anders Carlsson98323d22010-01-30 23:19:41 +0000433 C.Deallocate(this->Names);
434 this->Names = new (C) IdentifierInfo*[NumExprs];
435 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000436
Anders Carlsson98323d22010-01-30 23:19:41 +0000437 C.Deallocate(this->Exprs);
438 this->Exprs = new (C) Stmt*[NumExprs];
439 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000440
Anders Carlsson98323d22010-01-30 23:19:41 +0000441 C.Deallocate(this->Constraints);
442 this->Constraints = new (C) StringLiteral*[NumExprs];
443 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000444
Anders Carlsson98323d22010-01-30 23:19:41 +0000445 C.Deallocate(this->Clobbers);
446 this->Clobbers = new (C) StringLiteral*[NumClobbers];
447 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000448}
449
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000450/// getNamedOperand - Given a symbolic operand reference like %[foo],
451/// translate this into a numeric value needed to reference the same operand.
452/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000453int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000454 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000455
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000456 // Check if this is an output operand.
457 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
458 if (getOutputName(i) == SymbolicName)
459 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000462 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
463 if (getInputName(i) == SymbolicName)
464 return getNumOutputs() + NumPlusOperands + i;
465
466 // Not found.
467 return -1;
468}
469
Chris Lattner35b58362009-03-10 23:21:44 +0000470/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
471/// it into pieces. If the asm string is erroneous, emit errors and return
472/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000473unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000474 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000475 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000476 const char *StrStart = Str.begin();
477 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000478 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000479
Chris Lattner35b58362009-03-10 23:21:44 +0000480 // "Simple" inline asms have no constraints or operands, just convert the asm
481 // string to escape $'s.
482 if (isSimple()) {
483 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000484 for (; CurPtr != StrEnd; ++CurPtr) {
485 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000486 case '$':
487 Result += "$$";
488 break;
489 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000490 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000491 break;
492 }
493 }
494 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000495 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000496 }
497
498 // CurStringPiece - The current string that we are building up as we scan the
499 // asm string.
500 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000501
Douglas Gregore8bbc122011-09-02 00:18:52 +0000502 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000503
Chris Lattner35b58362009-03-10 23:21:44 +0000504 while (1) {
505 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000506 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000507 if (!CurStringPiece.empty())
508 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000509 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000510 }
Mike Stump11289f42009-09-09 15:08:12 +0000511
Chris Lattnera41b8472009-03-10 23:51:40 +0000512 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000513 switch (CurChar) {
514 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000515 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
516 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
517 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000518 case '%':
519 break;
520 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000521 CurStringPiece += CurChar;
522 continue;
523 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000524
Chris Lattner35b58362009-03-10 23:21:44 +0000525 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000526 if (CurPtr == StrEnd) {
527 // % at end of string is invalid (no escape).
528 DiagOffs = CurPtr-StrStart-1;
529 return diag::err_asm_invalid_escape;
530 }
Mike Stump11289f42009-09-09 15:08:12 +0000531
Chris Lattnera41b8472009-03-10 23:51:40 +0000532 char EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000533 if (EscapedChar == '%') { // %% -> %
534 // Escaped percentage sign.
535 CurStringPiece += '%';
536 continue;
537 }
Mike Stump11289f42009-09-09 15:08:12 +0000538
Chris Lattner35b58362009-03-10 23:21:44 +0000539 if (EscapedChar == '=') { // %= -> Generate an unique ID.
540 CurStringPiece += "${:uid}";
541 continue;
542 }
Mike Stump11289f42009-09-09 15:08:12 +0000543
Chris Lattner35b58362009-03-10 23:21:44 +0000544 // Otherwise, we have an operand. If we have accumulated a string so far,
545 // add it to the Pieces list.
546 if (!CurStringPiece.empty()) {
547 Pieces.push_back(AsmStringPiece(CurStringPiece));
548 CurStringPiece.clear();
549 }
Mike Stump11289f42009-09-09 15:08:12 +0000550
Akira Hatanaka987f1862014-08-22 06:05:21 +0000551 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
552 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
553
554 const char *Begin = CurPtr - 1; // Points to the character following '%'.
555 const char *Percent = Begin - 1; // Points to '%'.
556
Jordan Rosea7d03842013-02-08 22:30:41 +0000557 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000558 if (CurPtr == StrEnd) { // Premature end.
559 DiagOffs = CurPtr-StrStart-1;
560 return diag::err_asm_invalid_escape;
561 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000562 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000563 }
Mike Stump11289f42009-09-09 15:08:12 +0000564
Akira Hatanaka987f1862014-08-22 06:05:21 +0000565 const TargetInfo &TI = C.getTargetInfo();
566 const SourceManager &SM = C.getSourceManager();
567 const LangOptions &LO = C.getLangOpts();
568
569 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000570 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000571 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000572 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattner99d892b2009-03-11 22:52:17 +0000574 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000575 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000576 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner14311922009-03-11 00:23:13 +0000578 unsigned NumOperands =
579 getNumOutputs() + getNumPlusOperands() + getNumInputs();
580 if (N >= NumOperands) {
581 DiagOffs = CurPtr-StrStart-1;
582 return diag::err_asm_invalid_operand_number;
583 }
584
Akira Hatanaka987f1862014-08-22 06:05:21 +0000585 // Str contains "x4" (Operand without the leading %).
586 std::string Str(Begin, CurPtr - Begin);
587
588 // (BeginLoc, EndLoc) represents the range of the operand we are currently
589 // processing. Unlike Str, the range includes the leading '%'.
590 SourceLocation BeginLoc =
591 getAsmString()->getLocationOfByte(Percent - StrStart, SM, LO, TI);
592 SourceLocation EndLoc =
593 getAsmString()->getLocationOfByte(CurPtr - StrStart, SM, LO, TI);
594
595 Pieces.push_back(AsmStringPiece(N, Str, BeginLoc, EndLoc));
Chris Lattner35b58362009-03-10 23:21:44 +0000596 continue;
597 }
Mike Stump11289f42009-09-09 15:08:12 +0000598
Akira Hatanaka987f1862014-08-22 06:05:21 +0000599 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000600 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000601 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000602
Chris Lattner3fa25c62009-03-11 00:06:36 +0000603 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000604 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000605 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000606 return diag::err_asm_unterminated_symbolic_operand_name;
607 if (NameEnd == CurPtr)
608 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000609
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000610 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000611
Chris Lattner35b58362009-03-10 23:21:44 +0000612 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000613 if (N == -1) {
614 // Verify that an operand with that name exists.
615 DiagOffs = CurPtr-StrStart;
616 return diag::err_asm_unknown_symbolic_operand_name;
617 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000618
619 // Str contains "x[foo]" (Operand without the leading %).
620 std::string Str(Begin, NameEnd + 1 - Begin);
621
622 // (BeginLoc, EndLoc) represents the range of the operand we are currently
623 // processing. Unlike Str, the range includes the leading '%'.
624 SourceLocation BeginLoc =
625 getAsmString()->getLocationOfByte(Percent - StrStart, SM, LO, TI);
626 SourceLocation EndLoc =
627 getAsmString()->getLocationOfByte(NameEnd + 1 - StrStart, SM, LO, TI);
628
629 Pieces.push_back(AsmStringPiece(N, Str, BeginLoc, EndLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000630
Chris Lattner3fa25c62009-03-11 00:06:36 +0000631 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000632 continue;
633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000635 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000636 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000637 }
638}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000639
640/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000641std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000642 // Analyze the asm string to decompose it into its pieces. We know that Sema
643 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000644 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000645 unsigned DiagOffs;
646 AnalyzeAsmString(Pieces, C, DiagOffs);
647
648 std::string AsmString;
649 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
650 if (Pieces[i].isString())
651 AsmString += Pieces[i].getString();
652 else if (Pieces[i].getModifier() == '\0')
653 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
654 else
655 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
656 Pieces[i].getModifier() + '}';
657 }
658 return AsmString;
659}
Chris Lattner35b58362009-03-10 23:21:44 +0000660
Chad Rosier3b0c2602012-08-27 20:23:31 +0000661/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000662std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000663 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000664 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000665}
666
Chad Rosierfe31e622012-08-24 00:07:09 +0000667Expr *MSAsmStmt::getOutputExpr(unsigned i) {
668 return cast<Expr>(Exprs[i]);
669}
670
671Expr *MSAsmStmt::getInputExpr(unsigned i) {
672 return cast<Expr>(Exprs[i + NumOutputs]);
673}
674void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
675 Exprs[i + NumOutputs] = E;
676}
677
Sam Weinigebcea982010-02-03 02:09:59 +0000678QualType CXXCatchStmt::getCaughtType() const {
679 if (ExceptionDecl)
680 return ExceptionDecl->getType();
681 return QualType();
682}
683
Chris Lattner86f5e132008-01-30 05:01:46 +0000684//===----------------------------------------------------------------------===//
685// Constructors
686//===----------------------------------------------------------------------===//
687
Craig Toppere6960e22013-08-22 05:28:54 +0000688GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
689 bool issimple, bool isvolatile, unsigned numoutputs,
690 unsigned numinputs, IdentifierInfo **names,
691 StringLiteral **constraints, Expr **exprs,
692 StringLiteral *asmstr, unsigned numclobbers,
693 StringLiteral **clobbers, SourceLocation rparenloc)
Chad Rosierfe3352e2012-08-27 19:38:01 +0000694 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
695 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weberde565e32008-08-05 23:15:29 +0000696
Chad Rosier42032fa2012-08-07 23:12:23 +0000697 unsigned NumExprs = NumOutputs + NumInputs;
698
Anders Carlsson98323d22010-01-30 23:19:41 +0000699 Names = new (C) IdentifierInfo*[NumExprs];
700 std::copy(names, names + NumExprs, Names);
701
702 Exprs = new (C) Stmt*[NumExprs];
703 std::copy(exprs, exprs + NumExprs, Exprs);
704
705 Constraints = new (C) StringLiteral*[NumExprs];
706 std::copy(constraints, constraints + NumExprs, Constraints);
707
708 Clobbers = new (C) StringLiteral*[NumClobbers];
709 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000710}
711
Craig Toppere6960e22013-08-22 05:28:54 +0000712MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000713 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000714 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000715 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000716 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
717 StringRef asmstr, ArrayRef<StringRef> clobbers,
718 SourceLocation endloc)
719 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
720 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
John McCallf413f5e2013-05-03 00:10:13 +0000721 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000722
John McCallf413f5e2013-05-03 00:10:13 +0000723 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
724}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000725
Craig Toppere6960e22013-08-22 05:28:54 +0000726static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
John McCallf413f5e2013-05-03 00:10:13 +0000727 size_t size = str.size();
728 char *buffer = new (C) char[size];
729 memcpy(buffer, str.data(), size);
730 return StringRef(buffer, size);
731}
732
Craig Toppere6960e22013-08-22 05:28:54 +0000733void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000734 ArrayRef<Token> asmtoks,
735 ArrayRef<StringRef> constraints,
736 ArrayRef<Expr*> exprs,
737 ArrayRef<StringRef> clobbers) {
738 assert(NumAsmToks == asmtoks.size());
739 assert(NumClobbers == clobbers.size());
740
741 unsigned NumExprs = exprs.size();
742 assert(NumExprs == NumOutputs + NumInputs);
743 assert(NumExprs == constraints.size());
744
745 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000746
Chad Rosierfe31e622012-08-24 00:07:09 +0000747 Exprs = new (C) Stmt*[NumExprs];
Chad Rosierf8037a12012-10-16 21:55:39 +0000748 for (unsigned i = 0, e = NumExprs; i != e; ++i)
749 Exprs[i] = exprs[i];
Chad Rosierfe31e622012-08-24 00:07:09 +0000750
Chad Rosier99fc3812012-08-07 00:29:06 +0000751 AsmToks = new (C) Token[NumAsmToks];
752 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
753 AsmToks[i] = asmtoks[i];
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000754
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000755 Constraints = new (C) StringRef[NumExprs];
756 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
John McCallf413f5e2013-05-03 00:10:13 +0000757 Constraints[i] = copyIntoContext(C, constraints[i]);
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000758 }
759
Chad Rosierbaf53f92012-08-10 21:36:25 +0000760 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosierd6ef7042012-08-10 21:06:19 +0000761 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
762 // FIXME: Avoid the allocation/copy if at all possible.
John McCallf413f5e2013-05-03 00:10:13 +0000763 Clobbers[i] = copyIntoContext(C, clobbers[i]);
Chad Rosierd6ef7042012-08-10 21:06:19 +0000764 }
Chad Rosier32503022012-06-11 20:47:18 +0000765}
766
Chris Lattner86f5e132008-01-30 05:01:46 +0000767ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
768 Stmt *Body, SourceLocation FCL,
Nico Weberde565e32008-08-05 23:15:29 +0000769 SourceLocation RPL)
Chris Lattner86f5e132008-01-30 05:01:46 +0000770: Stmt(ObjCForCollectionStmtClass) {
771 SubExprs[ELEM] = Elem;
Pavel Labath515f4db2013-09-03 14:41:16 +0000772 SubExprs[COLLECTION] = Collect;
Chris Lattner86f5e132008-01-30 05:01:46 +0000773 SubExprs[BODY] = Body;
774 ForLoc = FCL;
775 RParenLoc = RPL;
776}
777
Douglas Gregor96c79492010-04-23 22:50:49 +0000778ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
779 Stmt **CatchStmts, unsigned NumCatchStmts,
780 Stmt *atFinallyStmt)
781 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000782 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
Douglas Gregor96c79492010-04-23 22:50:49 +0000783 Stmt **Stmts = getStmts();
784 Stmts[0] = atTryStmt;
785 for (unsigned I = 0; I != NumCatchStmts; ++I)
786 Stmts[I + 1] = CatchStmts[I];
Chad Rosier42032fa2012-08-07 23:12:23 +0000787
Douglas Gregor96c79492010-04-23 22:50:49 +0000788 if (HasFinally)
789 Stmts[NumCatchStmts + 1] = atFinallyStmt;
790}
Chris Lattner86f5e132008-01-30 05:01:46 +0000791
Craig Toppere6960e22013-08-22 05:28:54 +0000792ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
Chad Rosier42032fa2012-08-07 23:12:23 +0000793 SourceLocation atTryLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +0000794 Stmt *atTryStmt,
Chad Rosier42032fa2012-08-07 23:12:23 +0000795 Stmt **CatchStmts,
Douglas Gregor96c79492010-04-23 22:50:49 +0000796 unsigned NumCatchStmts,
797 Stmt *atFinallyStmt) {
Chad Rosier42032fa2012-08-07 23:12:23 +0000798 unsigned Size = sizeof(ObjCAtTryStmt) +
Craig Topper36250ad2014-05-12 05:36:57 +0000799 (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
Chris Lattner5c0b4052010-10-30 05:14:06 +0000800 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor96c79492010-04-23 22:50:49 +0000801 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
802 atFinallyStmt);
803}
Ted Kremeneka4965842008-02-01 21:28:59 +0000804
Craig Toppere6960e22013-08-22 05:28:54 +0000805ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
806 unsigned NumCatchStmts,
807 bool HasFinally) {
Chad Rosier42032fa2012-08-07 23:12:23 +0000808 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor96c79492010-04-23 22:50:49 +0000809 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner5c0b4052010-10-30 05:14:06 +0000810 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier42032fa2012-08-07 23:12:23 +0000811 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor96c79492010-04-23 22:50:49 +0000812}
Nico Weberde565e32008-08-05 23:15:29 +0000813
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000814SourceLocation ObjCAtTryStmt::getLocEnd() const {
Douglas Gregor96c79492010-04-23 22:50:49 +0000815 if (HasFinally)
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000816 return getFinallyStmt()->getLocEnd();
817 if (NumCatchStmts)
818 return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
819 return getTryBody()->getLocEnd();
Chris Lattner86f5e132008-01-30 05:01:46 +0000820}
821
Craig Toppere6960e22013-08-22 05:28:54 +0000822CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
Nico Weber9dff3782012-12-29 20:13:03 +0000823 Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000824 std::size_t Size = sizeof(CXXTryStmt);
Nico Weber9dff3782012-12-29 20:13:03 +0000825 Size += ((handlers.size() + 1) * sizeof(Stmt));
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000826
Chris Lattner5c0b4052010-10-30 05:14:06 +0000827 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Nico Weber9dff3782012-12-29 20:13:03 +0000828 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000829}
830
Craig Toppere6960e22013-08-22 05:28:54 +0000831CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
Argyrios Kyrtzidis47cd7a92010-07-22 16:03:56 +0000832 unsigned numHandlers) {
833 std::size_t Size = sizeof(CXXTryStmt);
834 Size += ((numHandlers + 1) * sizeof(Stmt));
835
Chris Lattner5c0b4052010-10-30 05:14:06 +0000836 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis47cd7a92010-07-22 16:03:56 +0000837 return new (Mem) CXXTryStmt(Empty, numHandlers);
838}
839
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000840CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Nico Weber9dff3782012-12-29 20:13:03 +0000841 ArrayRef<Stmt*> handlers)
842 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000843 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigebcea982010-02-03 02:09:59 +0000844 Stmts[0] = tryBlock;
Nico Weber9dff3782012-12-29 20:13:03 +0000845 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
Sam Weinigebcea982010-02-03 02:09:59 +0000846}
847
Richard Smith02e85f32011-04-14 22:09:26 +0000848CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
849 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
850 Stmt *Body, SourceLocation FL,
851 SourceLocation CL, SourceLocation RPL)
852 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
853 SubExprs[RANGE] = Range;
854 SubExprs[BEGINEND] = BeginEndStmt;
Pavel Labath515f4db2013-09-03 14:41:16 +0000855 SubExprs[COND] = Cond;
856 SubExprs[INC] = Inc;
Richard Smith02e85f32011-04-14 22:09:26 +0000857 SubExprs[LOOPVAR] = LoopVar;
858 SubExprs[BODY] = Body;
859}
860
861Expr *CXXForRangeStmt::getRangeInit() {
862 DeclStmt *RangeStmt = getRangeStmt();
863 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
Richard Trieu86738692014-02-27 23:59:14 +0000864 assert(RangeDecl && "for-range should have a single var decl");
Richard Smith02e85f32011-04-14 22:09:26 +0000865 return RangeDecl->getInit();
866}
867
868const Expr *CXXForRangeStmt::getRangeInit() const {
869 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
870}
871
872VarDecl *CXXForRangeStmt::getLoopVariable() {
873 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
874 assert(LV && "No loop variable in CXXForRangeStmt");
875 return cast<VarDecl>(LV);
876}
877
878const VarDecl *CXXForRangeStmt::getLoopVariable() const {
879 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
880}
881
Craig Toppere6960e22013-08-22 05:28:54 +0000882IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000883 Stmt *then, SourceLocation EL, Stmt *elsev)
884 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000885{
886 setConditionVariable(C, var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000887 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000888 SubExprs[THEN] = then;
Chad Rosier42032fa2012-08-07 23:12:23 +0000889 SubExprs[ELSE] = elsev;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000890}
891
892VarDecl *IfStmt::getConditionVariable() const {
893 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000894 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000895
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000896 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
897 return cast<VarDecl>(DS->getSingleDecl());
898}
899
Craig Toppere6960e22013-08-22 05:28:54 +0000900void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000901 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000902 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000903 return;
904 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000905
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000906 SourceRange VarRange = V->getSourceRange();
907 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
908 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000909}
910
Craig Toppere6960e22013-08-22 05:28:54 +0000911ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000912 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000913 SourceLocation RP)
Chad Rosier42032fa2012-08-07 23:12:23 +0000914 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000915{
916 SubExprs[INIT] = Init;
917 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000918 SubExprs[COND] = Cond;
919 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000920 SubExprs[BODY] = Body;
921}
922
923VarDecl *ForStmt::getConditionVariable() const {
924 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000925 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000926
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000927 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
928 return cast<VarDecl>(DS->getSingleDecl());
929}
930
Craig Toppere6960e22013-08-22 05:28:54 +0000931void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000932 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000933 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000934 return;
935 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000936
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000937 SourceRange VarRange = V->getSourceRange();
938 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
939 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000940}
941
Craig Toppere6960e22013-08-22 05:28:54 +0000942SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
Benjamin Kramer475386d2015-04-02 15:29:07 +0000943 : Stmt(SwitchStmtClass), FirstCase(nullptr, false) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000944 setConditionVariable(C, Var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000945 SubExprs[COND] = cond;
Craig Topper36250ad2014-05-12 05:36:57 +0000946 SubExprs[BODY] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000947}
948
949VarDecl *SwitchStmt::getConditionVariable() const {
950 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000951 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000952
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000953 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
954 return cast<VarDecl>(DS->getSingleDecl());
955}
956
Craig Toppere6960e22013-08-22 05:28:54 +0000957void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000958 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000959 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000960 return;
961 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000962
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000963 SourceRange VarRange = V->getSourceRange();
964 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
965 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000966}
967
John McCallbd066782011-02-09 08:16:59 +0000968Stmt *SwitchCase::getSubStmt() {
Chris Lattner80d51eb2011-02-28 00:18:06 +0000969 if (isa<CaseStmt>(this))
970 return cast<CaseStmt>(this)->getSubStmt();
John McCallbd066782011-02-09 08:16:59 +0000971 return cast<DefaultStmt>(this)->getSubStmt();
972}
973
Craig Toppere6960e22013-08-22 05:28:54 +0000974WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000975 SourceLocation WL)
Chris Lattner80d51eb2011-02-28 00:18:06 +0000976 : Stmt(WhileStmtClass) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000977 setConditionVariable(C, Var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000978 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000979 SubExprs[BODY] = body;
980 WhileLoc = WL;
981}
982
983VarDecl *WhileStmt::getConditionVariable() const {
984 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000985 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000986
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000987 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
988 return cast<VarDecl>(DS->getSingleDecl());
989}
990
Craig Toppere6960e22013-08-22 05:28:54 +0000991void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000992 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000993 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000994 return;
995 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000996
997 SourceRange VarRange = V->getSourceRange();
998 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
999 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001000}
1001
Ted Kremenek066dd932007-08-24 21:09:09 +00001002// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001003LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall9de91602010-10-28 08:53:48 +00001004 if (AddrLabelExpr *E =
1005 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
1006 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +00001007 return nullptr;
John McCall9de91602010-10-28 08:53:48 +00001008}
Ted Kremenek066dd932007-08-24 21:09:09 +00001009
Ted Kremenek066dd932007-08-24 21:09:09 +00001010// ReturnStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +00001011const Expr* ReturnStmt::getRetValue() const {
1012 return cast_or_null<Expr>(RetExpr);
1013}
1014Expr* ReturnStmt::getRetValue() {
1015 return cast_or_null<Expr>(RetExpr);
Ted Kremenek066dd932007-08-24 21:09:09 +00001016}
John Wiegley1c0675e2011-04-28 01:08:34 +00001017
Warren Huntf6be4cb2014-07-25 20:52:51 +00001018SEHTryStmt::SEHTryStmt(bool IsCXXTry,
1019 SourceLocation TryLoc,
1020 Stmt *TryBlock,
1021 Stmt *Handler)
1022 : Stmt(SEHTryStmtClass),
1023 IsCXXTry(IsCXXTry),
1024 TryLoc(TryLoc)
1025{
1026 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +00001027 Children[HANDLER] = Handler;
1028}
1029
Warren Huntf6be4cb2014-07-25 20:52:51 +00001030SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +00001031 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001032 Stmt *Handler) {
1033 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +00001034}
1035
1036SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1037 return dyn_cast<SEHExceptStmt>(getHandler());
1038}
1039
1040SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1041 return dyn_cast<SEHFinallyStmt>(getHandler());
1042}
1043
1044SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
1045 Expr *FilterExpr,
1046 Stmt *Block)
1047 : Stmt(SEHExceptStmtClass),
1048 Loc(Loc)
1049{
Pavel Labath515f4db2013-09-03 14:41:16 +00001050 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +00001051 Children[BLOCK] = Block;
1052}
1053
Craig Toppere6960e22013-08-22 05:28:54 +00001054SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
1055 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +00001056 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1057}
1058
1059SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1060 Stmt *Block)
1061 : Stmt(SEHFinallyStmtClass),
1062 Loc(Loc),
1063 Block(Block)
1064{}
1065
Craig Toppere6960e22013-08-22 05:28:54 +00001066SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +00001067 Stmt *Block) {
1068 return new(C)SEHFinallyStmt(Loc,Block);
1069}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001070
1071CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1072 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1073
1074 // Offset of the first Capture object.
1075 unsigned FirstCaptureOffset =
1076 llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1077
1078 return reinterpret_cast<Capture *>(
1079 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1080 + FirstCaptureOffset);
1081}
1082
Wei Pan17fbf6e2013-05-04 03:59:06 +00001083CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1084 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001085 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001086 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001087 RecordDecl *RD)
1088 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001089 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001090 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001091 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001092 assert(RD && "null record declaration for captured statement");
1093
1094 // Copy initialization expressions.
1095 Stmt **Stored = getStoredStmts();
1096 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1097 *Stored++ = CaptureInits[I];
1098
1099 // Copy the statement being captured.
1100 *Stored = S;
1101
1102 // Copy all Capture objects.
1103 Capture *Buffer = getStoredCaptures();
1104 std::copy(Captures.begin(), Captures.end(), Buffer);
1105}
1106
1107CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1108 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Craig Topper36250ad2014-05-12 05:36:57 +00001109 CapDeclAndKind(nullptr, CR_Default), TheRecordDecl(nullptr) {
1110 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001111}
1112
Craig Toppere6960e22013-08-22 05:28:54 +00001113CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001114 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001115 ArrayRef<Capture> Captures,
1116 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001117 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001118 RecordDecl *RD) {
1119 // The layout is
1120 //
1121 // -----------------------------------------------------------
1122 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1123 // ----------------^-------------------^----------------------
1124 // getStoredStmts() getStoredCaptures()
1125 //
1126 // where S is the statement being captured.
1127 //
1128 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1129
1130 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1131 if (!Captures.empty()) {
1132 // Realign for the following Capture array.
1133 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1134 Size += sizeof(Capture) * Captures.size();
1135 }
1136
1137 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001138 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001139}
1140
Craig Toppere6960e22013-08-22 05:28:54 +00001141CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001142 unsigned NumCaptures) {
1143 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1144 if (NumCaptures > 0) {
1145 // Realign for the following Capture array.
1146 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1147 Size += sizeof(Capture) * NumCaptures;
1148 }
1149
1150 void *Mem = Context.Allocate(Size);
1151 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1152}
1153
1154Stmt::child_range CapturedStmt::children() {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001155 // Children are captured field initilizers.
1156 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001157}
1158
1159bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001160 for (const auto &I : captures()) {
1161 if (!I.capturesVariable())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001162 continue;
1163
1164 // This does not handle variable redeclarations. This should be
1165 // extended to capture variables with redeclarations, for example
1166 // a thread-private variable in OpenMP.
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001167 if (I.getCapturedVar() == Var)
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001168 return true;
1169 }
1170
1171 return false;
1172}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001173
Alexey Bataev758e55e2013-09-06 18:03:48 +00001174StmtRange OMPClause::children() {
1175 switch(getClauseKind()) {
1176 default : break;
1177#define OPENMP_CLAUSE(Name, Class) \
1178 case OMPC_ ## Name : return static_cast<Class *>(this)->children();
1179#include "clang/Basic/OpenMPKinds.def"
1180 }
1181 llvm_unreachable("unknown OMPClause");
1182}
1183
Alexey Bataev03b340a2014-10-21 03:16:40 +00001184void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1185 assert(VL.size() == varlist_size() &&
1186 "Number of private copies is not the same as the preallocated buffer");
1187 std::copy(VL.begin(), VL.end(), varlist_end());
1188}
1189
1190OMPPrivateClause *
1191OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
1192 SourceLocation LParenLoc, SourceLocation EndLoc,
1193 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
1194 // Allocate space for private variables and initializer expressions.
Alexey Bataev13193812014-02-25 11:25:38 +00001195 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1196 llvm::alignOf<Expr *>()) +
Alexey Bataev03b340a2014-10-21 03:16:40 +00001197 2 * sizeof(Expr *) * VL.size());
1198 OMPPrivateClause *Clause =
1199 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001200 Clause->setVarRefs(VL);
Alexey Bataev03b340a2014-10-21 03:16:40 +00001201 Clause->setPrivateCopies(PrivateVL);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001202 return Clause;
1203}
1204
Craig Toppere6960e22013-08-22 05:28:54 +00001205OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001206 unsigned N) {
Alexey Bataev13193812014-02-25 11:25:38 +00001207 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
1208 llvm::alignOf<Expr *>()) +
Alexey Bataev03b340a2014-10-21 03:16:40 +00001209 2 * sizeof(Expr *) * N);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001210 return new (Mem) OMPPrivateClause(N);
1211}
1212
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001213void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1214 assert(VL.size() == varlist_size() &&
1215 "Number of private copies is not the same as the preallocated buffer");
1216 std::copy(VL.begin(), VL.end(), varlist_end());
1217}
1218
1219void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
1220 assert(VL.size() == varlist_size() &&
1221 "Number of inits is not the same as the preallocated buffer");
1222 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
1223}
1224
1225OMPFirstprivateClause *
1226OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
1227 SourceLocation LParenLoc, SourceLocation EndLoc,
1228 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
1229 ArrayRef<Expr *> InitVL) {
Alexey Bataev13193812014-02-25 11:25:38 +00001230 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1231 llvm::alignOf<Expr *>()) +
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001232 3 * sizeof(Expr *) * VL.size());
1233 OMPFirstprivateClause *Clause =
1234 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001235 Clause->setVarRefs(VL);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001236 Clause->setPrivateCopies(PrivateVL);
1237 Clause->setInits(InitVL);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001238 return Clause;
1239}
1240
1241OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
1242 unsigned N) {
Alexey Bataev13193812014-02-25 11:25:38 +00001243 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
1244 llvm::alignOf<Expr *>()) +
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001245 3 * sizeof(Expr *) * N);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001246 return new (Mem) OMPFirstprivateClause(N);
1247}
1248
Alexey Bataev38e89532015-04-16 04:54:05 +00001249void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
1250 assert(PrivateCopies.size() == varlist_size() &&
1251 "Number of private copies is not the same as the preallocated buffer");
1252 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
1253}
1254
1255void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
1256 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
1257 "not the same as the "
1258 "preallocated buffer");
1259 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
1260}
1261
1262void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
1263 assert(DstExprs.size() == varlist_size() && "Number of destination "
1264 "expressions is not the same as "
1265 "the preallocated buffer");
1266 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
1267}
1268
1269void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
1270 assert(AssignmentOps.size() == varlist_size() &&
1271 "Number of assignment expressions is not the same as the preallocated "
1272 "buffer");
1273 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
1274 getDestinationExprs().end());
1275}
1276
1277OMPLastprivateClause *OMPLastprivateClause::Create(
1278 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1279 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
1280 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00001281 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1282 llvm::alignOf<Expr *>()) +
Alexey Bataev38e89532015-04-16 04:54:05 +00001283 5 * sizeof(Expr *) * VL.size());
Alexander Musman1bb328c2014-06-04 13:06:39 +00001284 OMPLastprivateClause *Clause =
1285 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1286 Clause->setVarRefs(VL);
Alexey Bataev38e89532015-04-16 04:54:05 +00001287 Clause->setSourceExprs(SrcExprs);
1288 Clause->setDestinationExprs(DstExprs);
1289 Clause->setAssignmentOps(AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00001290 return Clause;
1291}
1292
1293OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
1294 unsigned N) {
1295 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLastprivateClause),
1296 llvm::alignOf<Expr *>()) +
Alexey Bataev38e89532015-04-16 04:54:05 +00001297 5 * sizeof(Expr *) * N);
Alexander Musman1bb328c2014-06-04 13:06:39 +00001298 return new (Mem) OMPLastprivateClause(N);
1299}
1300
Alexey Bataev758e55e2013-09-06 18:03:48 +00001301OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
1302 SourceLocation StartLoc,
1303 SourceLocation LParenLoc,
1304 SourceLocation EndLoc,
1305 ArrayRef<Expr *> VL) {
Alexey Bataev13193812014-02-25 11:25:38 +00001306 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1307 llvm::alignOf<Expr *>()) +
1308 sizeof(Expr *) * VL.size());
Alexey Bataev758e55e2013-09-06 18:03:48 +00001309 OMPSharedClause *Clause = new (Mem) OMPSharedClause(StartLoc, LParenLoc,
1310 EndLoc, VL.size());
1311 Clause->setVarRefs(VL);
1312 return Clause;
1313}
1314
1315OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C,
1316 unsigned N) {
Alexey Bataev13193812014-02-25 11:25:38 +00001317 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
1318 llvm::alignOf<Expr *>()) +
1319 sizeof(Expr *) * N);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001320 return new (Mem) OMPSharedClause(N);
1321}
1322
Alexander Musman3276a272015-03-21 10:12:56 +00001323void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
1324 assert(IL.size() == varlist_size() &&
1325 "Number of inits is not the same as the preallocated buffer");
1326 std::copy(IL.begin(), IL.end(), varlist_end());
1327}
1328
1329void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
1330 assert(UL.size() == varlist_size() &&
1331 "Number of updates is not the same as the preallocated buffer");
1332 std::copy(UL.begin(), UL.end(), getInits().end());
1333}
1334
1335void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
1336 assert(FL.size() == varlist_size() &&
1337 "Number of final updates is not the same as the preallocated buffer");
1338 std::copy(FL.begin(), FL.end(), getUpdates().end());
1339}
1340
1341OMPLinearClause *
1342OMPLinearClause::Create(const ASTContext &C, SourceLocation StartLoc,
1343 SourceLocation LParenLoc, SourceLocation ColonLoc,
1344 SourceLocation EndLoc, ArrayRef<Expr *> VL,
1345 ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep) {
1346 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
1347 // (Step and CalcStep).
Alexander Musman8dba6642014-04-22 13:09:42 +00001348 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1349 llvm::alignOf<Expr *>()) +
Alexander Musman3276a272015-03-21 10:12:56 +00001350 (4 * VL.size() + 2) * sizeof(Expr *));
Alexander Musman8dba6642014-04-22 13:09:42 +00001351 OMPLinearClause *Clause = new (Mem)
1352 OMPLinearClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1353 Clause->setVarRefs(VL);
Alexander Musman3276a272015-03-21 10:12:56 +00001354 Clause->setInits(IL);
1355 // Fill update and final expressions with zeroes, they are provided later,
1356 // after the directive construction.
1357 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
1358 nullptr);
1359 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
1360 nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00001361 Clause->setStep(Step);
Alexander Musman3276a272015-03-21 10:12:56 +00001362 Clause->setCalcStep(CalcStep);
Alexander Musman8dba6642014-04-22 13:09:42 +00001363 return Clause;
1364}
1365
1366OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
1367 unsigned NumVars) {
Alexander Musman3276a272015-03-21 10:12:56 +00001368 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
1369 // (Step and CalcStep).
Alexander Musman8dba6642014-04-22 13:09:42 +00001370 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPLinearClause),
1371 llvm::alignOf<Expr *>()) +
Alexander Musman3276a272015-03-21 10:12:56 +00001372 (4 * NumVars + 2) * sizeof(Expr *));
Alexander Musman8dba6642014-04-22 13:09:42 +00001373 return new (Mem) OMPLinearClause(NumVars);
1374}
1375
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001376OMPAlignedClause *
1377OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
1378 SourceLocation LParenLoc, SourceLocation ColonLoc,
1379 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
1380 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1381 llvm::alignOf<Expr *>()) +
1382 sizeof(Expr *) * (VL.size() + 1));
1383 OMPAlignedClause *Clause = new (Mem)
1384 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
1385 Clause->setVarRefs(VL);
1386 Clause->setAlignment(A);
1387 return Clause;
1388}
1389
1390OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
1391 unsigned NumVars) {
1392 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPAlignedClause),
1393 llvm::alignOf<Expr *>()) +
1394 sizeof(Expr *) * (NumVars + 1));
1395 return new (Mem) OMPAlignedClause(NumVars);
1396}
1397
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001398OMPCopyinClause *OMPCopyinClause::Create(const ASTContext &C,
1399 SourceLocation StartLoc,
1400 SourceLocation LParenLoc,
1401 SourceLocation EndLoc,
1402 ArrayRef<Expr *> VL) {
1403 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1404 llvm::alignOf<Expr *>()) +
1405 sizeof(Expr *) * VL.size());
1406 OMPCopyinClause *Clause = new (Mem) OMPCopyinClause(StartLoc, LParenLoc,
1407 EndLoc, VL.size());
1408 Clause->setVarRefs(VL);
1409 return Clause;
1410}
1411
1412OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C,
1413 unsigned N) {
1414 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyinClause),
1415 llvm::alignOf<Expr *>()) +
1416 sizeof(Expr *) * N);
1417 return new (Mem) OMPCopyinClause(N);
1418}
1419
Alexey Bataeva63048e2015-03-23 06:18:07 +00001420void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
1421 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
1422 "not the same as the "
1423 "preallocated buffer");
1424 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
1425}
1426
1427void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
1428 assert(DstExprs.size() == varlist_size() && "Number of destination "
1429 "expressions is not the same as "
1430 "the preallocated buffer");
1431 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
1432}
1433
1434void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
1435 assert(AssignmentOps.size() == varlist_size() &&
1436 "Number of assignment expressions is not the same as the preallocated "
1437 "buffer");
1438 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
1439 getDestinationExprs().end());
1440}
1441
1442OMPCopyprivateClause *OMPCopyprivateClause::Create(
1443 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1444 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
1445 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00001446 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1447 llvm::alignOf<Expr *>()) +
Alexey Bataeva63048e2015-03-23 06:18:07 +00001448 4 * sizeof(Expr *) * VL.size());
Alexey Bataevbae9a792014-06-27 10:37:06 +00001449 OMPCopyprivateClause *Clause =
1450 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
1451 Clause->setVarRefs(VL);
Alexey Bataeva63048e2015-03-23 06:18:07 +00001452 Clause->setSourceExprs(SrcExprs);
1453 Clause->setDestinationExprs(DstExprs);
1454 Clause->setAssignmentOps(AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001455 return Clause;
1456}
1457
1458OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
1459 unsigned N) {
1460 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPCopyprivateClause),
1461 llvm::alignOf<Expr *>()) +
Alexey Bataeva63048e2015-03-23 06:18:07 +00001462 4 * sizeof(Expr *) * N);
Alexey Bataevbae9a792014-06-27 10:37:06 +00001463 return new (Mem) OMPCopyprivateClause(N);
1464}
1465
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001466void OMPExecutableDirective::setClauses(ArrayRef<OMPClause *> Clauses) {
Alexander Musmanfd2b7592014-03-27 15:14:18 +00001467 assert(Clauses.size() == getNumClauses() &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001468 "Number of clauses is not the same as the preallocated buffer");
Alexander Musmanfd2b7592014-03-27 15:14:18 +00001469 std::copy(Clauses.begin(), Clauses.end(), getClauses().begin());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001470}
1471
Alexander Musmana5f070a2014-10-01 06:03:56 +00001472void OMPLoopDirective::setCounters(ArrayRef<Expr *> A) {
1473 assert(A.size() == getCollapsedNumber() &&
1474 "Number of loop counters is not the same as the collapsed number");
1475 std::copy(A.begin(), A.end(), getCounters().begin());
1476}
1477
1478void OMPLoopDirective::setUpdates(ArrayRef<Expr *> A) {
1479 assert(A.size() == getCollapsedNumber() &&
1480 "Number of counter updates is not the same as the collapsed number");
1481 std::copy(A.begin(), A.end(), getUpdates().begin());
1482}
1483
1484void OMPLoopDirective::setFinals(ArrayRef<Expr *> A) {
1485 assert(A.size() == getCollapsedNumber() &&
1486 "Number of counter finals is not the same as the collapsed number");
1487 std::copy(A.begin(), A.end(), getFinals().begin());
1488}
1489
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001490void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
1491 assert(
1492 LHSExprs.size() == varlist_size() &&
1493 "Number of LHS expressions is not the same as the preallocated buffer");
1494 std::copy(LHSExprs.begin(), LHSExprs.end(), varlist_end());
1495}
1496
1497void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
1498 assert(
1499 RHSExprs.size() == varlist_size() &&
1500 "Number of RHS expressions is not the same as the preallocated buffer");
1501 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
1502}
1503
1504void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
1505 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
1506 "expressions is not the same "
1507 "as the preallocated buffer");
1508 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
1509}
1510
Alexey Bataevc5e02582014-06-16 07:08:35 +00001511OMPReductionClause *OMPReductionClause::Create(
1512 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
1513 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001514 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
1515 ArrayRef<Expr *> LHSExprs, ArrayRef<Expr *> RHSExprs,
1516 ArrayRef<Expr *> ReductionOps) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001517 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1518 llvm::alignOf<Expr *>()) +
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001519 4 * sizeof(Expr *) * VL.size());
Alexey Bataevc5e02582014-06-16 07:08:35 +00001520 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
1521 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
1522 Clause->setVarRefs(VL);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001523 Clause->setLHSExprs(LHSExprs);
1524 Clause->setRHSExprs(RHSExprs);
1525 Clause->setReductionOps(ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001526 return Clause;
1527}
1528
1529OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
1530 unsigned N) {
1531 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPReductionClause),
1532 llvm::alignOf<Expr *>()) +
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001533 4 * sizeof(Expr *) * N);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001534 return new (Mem) OMPReductionClause(N);
1535}
1536
Alexey Bataev6125da92014-07-21 11:26:11 +00001537OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
1538 SourceLocation StartLoc,
1539 SourceLocation LParenLoc,
1540 SourceLocation EndLoc,
1541 ArrayRef<Expr *> VL) {
1542 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
1543 llvm::alignOf<Expr *>()) +
1544 sizeof(Expr *) * VL.size());
1545 OMPFlushClause *Clause =
1546 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
1547 Clause->setVarRefs(VL);
1548 return Clause;
1549}
1550
1551OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
1552 void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFlushClause),
1553 llvm::alignOf<Expr *>()) +
1554 sizeof(Expr *) * N);
1555 return new (Mem) OMPFlushClause(N);
1556}
1557
Alexey Bataevd74d0602014-10-13 06:02:40 +00001558const OMPClause *
1559OMPExecutableDirective::getSingleClause(OpenMPClauseKind K) const {
1560 auto ClauseFilter =
1561 [=](const OMPClause *C) -> bool { return C->getClauseKind() == K; };
1562 OMPExecutableDirective::filtered_clause_iterator<decltype(ClauseFilter)> I(
1563 clauses(), ClauseFilter);
1564
1565 if (I) {
1566 auto *Clause = *I;
1567 assert(!++I && "There are at least 2 clauses of the specified kind");
1568 return Clause;
1569 }
1570 return nullptr;
1571}
1572
Craig Toppercee61332013-08-21 04:01:01 +00001573OMPParallelDirective *OMPParallelDirective::Create(
Craig Toppere6960e22013-08-22 05:28:54 +00001574 const ASTContext &C,
Craig Toppercee61332013-08-21 04:01:01 +00001575 SourceLocation StartLoc,
1576 SourceLocation EndLoc,
1577 ArrayRef<OMPClause *> Clauses,
1578 Stmt *AssociatedStmt) {
Alexey Bataev13193812014-02-25 11:25:38 +00001579 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1580 llvm::alignOf<OMPClause *>());
1581 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1582 sizeof(Stmt *));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001583 OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc,
1584 Clauses.size());
1585 Dir->setClauses(Clauses);
1586 Dir->setAssociatedStmt(AssociatedStmt);
1587 return Dir;
1588}
1589
Craig Toppere6960e22013-08-22 05:28:54 +00001590OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001591 unsigned NumClauses,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001592 EmptyShell) {
Alexey Bataev13193812014-02-25 11:25:38 +00001593 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
1594 llvm::alignOf<OMPClause *>());
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001595 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1596 sizeof(Stmt *));
1597 return new (Mem) OMPParallelDirective(NumClauses);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001598}
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001599
Alexey Bataevabfc0692014-06-25 06:52:00 +00001600OMPSimdDirective *
1601OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1602 SourceLocation EndLoc, unsigned CollapsedNum,
Alexander Musmana5f070a2014-10-01 06:03:56 +00001603 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
Alexander Musmanc6388682014-12-15 07:07:06 +00001604 const HelperExprs &Exprs) {
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001605 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1606 llvm::alignOf<OMPClause *>());
Alexander Musmanc6388682014-12-15 07:07:06 +00001607 void *Mem =
1608 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1609 sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_simd));
Alexey Bataevabfc0692014-06-25 06:52:00 +00001610 OMPSimdDirective *Dir = new (Mem)
1611 OMPSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001612 Dir->setClauses(Clauses);
1613 Dir->setAssociatedStmt(AssociatedStmt);
Alexander Musmanc6388682014-12-15 07:07:06 +00001614 Dir->setIterationVariable(Exprs.IterationVarRef);
1615 Dir->setLastIteration(Exprs.LastIteration);
1616 Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1617 Dir->setPreCond(Exprs.PreCond);
1618 Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1619 Dir->setInit(Exprs.Init);
1620 Dir->setInc(Exprs.Inc);
1621 Dir->setCounters(Exprs.Counters);
1622 Dir->setUpdates(Exprs.Updates);
1623 Dir->setFinals(Exprs.Finals);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001624 return Dir;
1625}
1626
1627OMPSimdDirective *OMPSimdDirective::CreateEmpty(const ASTContext &C,
1628 unsigned NumClauses,
1629 unsigned CollapsedNum,
1630 EmptyShell) {
1631 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSimdDirective),
1632 llvm::alignOf<OMPClause *>());
Alexander Musmanc6388682014-12-15 07:07:06 +00001633 void *Mem =
1634 C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1635 sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_simd));
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001636 return new (Mem) OMPSimdDirective(CollapsedNum, NumClauses);
1637}
1638
Alexey Bataevabfc0692014-06-25 06:52:00 +00001639OMPForDirective *
1640OMPForDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1641 SourceLocation EndLoc, unsigned CollapsedNum,
Alexander Musmana5f070a2014-10-01 06:03:56 +00001642 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
Alexander Musmanc6388682014-12-15 07:07:06 +00001643 const HelperExprs &Exprs) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001644 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1645 llvm::alignOf<OMPClause *>());
Alexander Musmanc6388682014-12-15 07:07:06 +00001646 void *Mem =
1647 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1648 sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for));
Alexey Bataevf29276e2014-06-18 04:14:57 +00001649 OMPForDirective *Dir =
Alexey Bataevabfc0692014-06-25 06:52:00 +00001650 new (Mem) OMPForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
Alexey Bataevf29276e2014-06-18 04:14:57 +00001651 Dir->setClauses(Clauses);
1652 Dir->setAssociatedStmt(AssociatedStmt);
Alexander Musmanc6388682014-12-15 07:07:06 +00001653 Dir->setIterationVariable(Exprs.IterationVarRef);
1654 Dir->setLastIteration(Exprs.LastIteration);
1655 Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1656 Dir->setPreCond(Exprs.PreCond);
1657 Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1658 Dir->setInit(Exprs.Init);
1659 Dir->setInc(Exprs.Inc);
1660 Dir->setIsLastIterVariable(Exprs.IL);
1661 Dir->setLowerBoundVariable(Exprs.LB);
1662 Dir->setUpperBoundVariable(Exprs.UB);
1663 Dir->setStrideVariable(Exprs.ST);
1664 Dir->setEnsureUpperBound(Exprs.EUB);
1665 Dir->setNextLowerBound(Exprs.NLB);
1666 Dir->setNextUpperBound(Exprs.NUB);
1667 Dir->setCounters(Exprs.Counters);
1668 Dir->setUpdates(Exprs.Updates);
1669 Dir->setFinals(Exprs.Finals);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001670 return Dir;
1671}
1672
1673OMPForDirective *OMPForDirective::CreateEmpty(const ASTContext &C,
1674 unsigned NumClauses,
1675 unsigned CollapsedNum,
1676 EmptyShell) {
1677 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForDirective),
1678 llvm::alignOf<OMPClause *>());
Alexander Musmanc6388682014-12-15 07:07:06 +00001679 void *Mem =
1680 C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1681 sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for));
Alexey Bataevf29276e2014-06-18 04:14:57 +00001682 return new (Mem) OMPForDirective(CollapsedNum, NumClauses);
1683}
1684
Alexander Musmanc6388682014-12-15 07:07:06 +00001685OMPForSimdDirective *
1686OMPForSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1687 SourceLocation EndLoc, unsigned CollapsedNum,
1688 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1689 const HelperExprs &Exprs) {
Alexander Musmanf82886e2014-09-18 05:12:34 +00001690 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForSimdDirective),
1691 llvm::alignOf<OMPClause *>());
Alexander Musmanc6388682014-12-15 07:07:06 +00001692 void *Mem =
1693 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
1694 sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for_simd));
Alexander Musmanf82886e2014-09-18 05:12:34 +00001695 OMPForSimdDirective *Dir = new (Mem)
1696 OMPForSimdDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1697 Dir->setClauses(Clauses);
1698 Dir->setAssociatedStmt(AssociatedStmt);
Alexander Musmanc6388682014-12-15 07:07:06 +00001699 Dir->setIterationVariable(Exprs.IterationVarRef);
1700 Dir->setLastIteration(Exprs.LastIteration);
1701 Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1702 Dir->setPreCond(Exprs.PreCond);
1703 Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1704 Dir->setInit(Exprs.Init);
1705 Dir->setInc(Exprs.Inc);
1706 Dir->setIsLastIterVariable(Exprs.IL);
1707 Dir->setLowerBoundVariable(Exprs.LB);
1708 Dir->setUpperBoundVariable(Exprs.UB);
1709 Dir->setStrideVariable(Exprs.ST);
1710 Dir->setEnsureUpperBound(Exprs.EUB);
1711 Dir->setNextLowerBound(Exprs.NLB);
1712 Dir->setNextUpperBound(Exprs.NUB);
1713 Dir->setCounters(Exprs.Counters);
1714 Dir->setUpdates(Exprs.Updates);
1715 Dir->setFinals(Exprs.Finals);
Alexander Musmanf82886e2014-09-18 05:12:34 +00001716 return Dir;
1717}
1718
1719OMPForSimdDirective *OMPForSimdDirective::CreateEmpty(const ASTContext &C,
1720 unsigned NumClauses,
1721 unsigned CollapsedNum,
1722 EmptyShell) {
1723 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPForSimdDirective),
1724 llvm::alignOf<OMPClause *>());
Alexander Musmanc6388682014-12-15 07:07:06 +00001725 void *Mem =
1726 C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
1727 sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_for_simd));
Alexander Musmanf82886e2014-09-18 05:12:34 +00001728 return new (Mem) OMPForSimdDirective(CollapsedNum, NumClauses);
1729}
1730
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001731OMPSectionsDirective *OMPSectionsDirective::Create(
1732 const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1733 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1734 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1735 llvm::alignOf<OMPClause *>());
1736 void *Mem =
1737 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1738 OMPSectionsDirective *Dir =
1739 new (Mem) OMPSectionsDirective(StartLoc, EndLoc, Clauses.size());
1740 Dir->setClauses(Clauses);
1741 Dir->setAssociatedStmt(AssociatedStmt);
1742 return Dir;
1743}
1744
1745OMPSectionsDirective *OMPSectionsDirective::CreateEmpty(const ASTContext &C,
1746 unsigned NumClauses,
1747 EmptyShell) {
1748 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1749 llvm::alignOf<OMPClause *>());
1750 void *Mem =
1751 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1752 return new (Mem) OMPSectionsDirective(NumClauses);
1753}
1754
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001755OMPSectionDirective *OMPSectionDirective::Create(const ASTContext &C,
1756 SourceLocation StartLoc,
1757 SourceLocation EndLoc,
1758 Stmt *AssociatedStmt) {
1759 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionsDirective),
1760 llvm::alignOf<Stmt *>());
1761 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1762 OMPSectionDirective *Dir = new (Mem) OMPSectionDirective(StartLoc, EndLoc);
1763 Dir->setAssociatedStmt(AssociatedStmt);
1764 return Dir;
1765}
1766
1767OMPSectionDirective *OMPSectionDirective::CreateEmpty(const ASTContext &C,
1768 EmptyShell) {
1769 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSectionDirective),
1770 llvm::alignOf<Stmt *>());
1771 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1772 return new (Mem) OMPSectionDirective();
1773}
1774
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001775OMPSingleDirective *OMPSingleDirective::Create(const ASTContext &C,
1776 SourceLocation StartLoc,
1777 SourceLocation EndLoc,
1778 ArrayRef<OMPClause *> Clauses,
1779 Stmt *AssociatedStmt) {
1780 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1781 llvm::alignOf<OMPClause *>());
1782 void *Mem =
1783 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1784 OMPSingleDirective *Dir =
1785 new (Mem) OMPSingleDirective(StartLoc, EndLoc, Clauses.size());
1786 Dir->setClauses(Clauses);
1787 Dir->setAssociatedStmt(AssociatedStmt);
1788 return Dir;
1789}
1790
1791OMPSingleDirective *OMPSingleDirective::CreateEmpty(const ASTContext &C,
1792 unsigned NumClauses,
1793 EmptyShell) {
1794 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPSingleDirective),
1795 llvm::alignOf<OMPClause *>());
1796 void *Mem =
1797 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1798 return new (Mem) OMPSingleDirective(NumClauses);
1799}
1800
Alexander Musman80c22892014-07-17 08:54:58 +00001801OMPMasterDirective *OMPMasterDirective::Create(const ASTContext &C,
1802 SourceLocation StartLoc,
1803 SourceLocation EndLoc,
1804 Stmt *AssociatedStmt) {
1805 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
1806 llvm::alignOf<Stmt *>());
1807 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1808 OMPMasterDirective *Dir = new (Mem) OMPMasterDirective(StartLoc, EndLoc);
1809 Dir->setAssociatedStmt(AssociatedStmt);
1810 return Dir;
1811}
1812
1813OMPMasterDirective *OMPMasterDirective::CreateEmpty(const ASTContext &C,
1814 EmptyShell) {
1815 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPMasterDirective),
1816 llvm::alignOf<Stmt *>());
1817 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1818 return new (Mem) OMPMasterDirective();
1819}
1820
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001821OMPCriticalDirective *OMPCriticalDirective::Create(
1822 const ASTContext &C, const DeclarationNameInfo &Name,
1823 SourceLocation StartLoc, SourceLocation EndLoc, Stmt *AssociatedStmt) {
1824 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
1825 llvm::alignOf<Stmt *>());
1826 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1827 OMPCriticalDirective *Dir =
1828 new (Mem) OMPCriticalDirective(Name, StartLoc, EndLoc);
1829 Dir->setAssociatedStmt(AssociatedStmt);
1830 return Dir;
1831}
1832
1833OMPCriticalDirective *OMPCriticalDirective::CreateEmpty(const ASTContext &C,
1834 EmptyShell) {
1835 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPCriticalDirective),
1836 llvm::alignOf<Stmt *>());
1837 void *Mem = C.Allocate(Size + sizeof(Stmt *));
1838 return new (Mem) OMPCriticalDirective();
1839}
1840
Alexander Musmana5f070a2014-10-01 06:03:56 +00001841OMPParallelForDirective *OMPParallelForDirective::Create(
1842 const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1843 unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
Alexander Musmanc6388682014-12-15 07:07:06 +00001844 const HelperExprs &Exprs) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001845 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1846 llvm::alignOf<OMPClause *>());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001847 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
Alexander Musmanc6388682014-12-15 07:07:06 +00001848 sizeof(Stmt *) *
1849 numLoopChildren(CollapsedNum, OMPD_parallel_for));
Alexey Bataev4acb8592014-07-07 13:01:15 +00001850 OMPParallelForDirective *Dir = new (Mem)
1851 OMPParallelForDirective(StartLoc, EndLoc, CollapsedNum, Clauses.size());
1852 Dir->setClauses(Clauses);
1853 Dir->setAssociatedStmt(AssociatedStmt);
Alexander Musmanc6388682014-12-15 07:07:06 +00001854 Dir->setIterationVariable(Exprs.IterationVarRef);
1855 Dir->setLastIteration(Exprs.LastIteration);
1856 Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1857 Dir->setPreCond(Exprs.PreCond);
1858 Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1859 Dir->setInit(Exprs.Init);
1860 Dir->setInc(Exprs.Inc);
1861 Dir->setIsLastIterVariable(Exprs.IL);
1862 Dir->setLowerBoundVariable(Exprs.LB);
1863 Dir->setUpperBoundVariable(Exprs.UB);
1864 Dir->setStrideVariable(Exprs.ST);
1865 Dir->setEnsureUpperBound(Exprs.EUB);
1866 Dir->setNextLowerBound(Exprs.NLB);
1867 Dir->setNextUpperBound(Exprs.NUB);
1868 Dir->setCounters(Exprs.Counters);
1869 Dir->setUpdates(Exprs.Updates);
1870 Dir->setFinals(Exprs.Finals);
Alexey Bataev4acb8592014-07-07 13:01:15 +00001871 return Dir;
1872}
1873
1874OMPParallelForDirective *
1875OMPParallelForDirective::CreateEmpty(const ASTContext &C, unsigned NumClauses,
1876 unsigned CollapsedNum, EmptyShell) {
1877 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForDirective),
1878 llvm::alignOf<OMPClause *>());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001879 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses +
Alexander Musmanc6388682014-12-15 07:07:06 +00001880 sizeof(Stmt *) *
1881 numLoopChildren(CollapsedNum, OMPD_parallel_for));
Alexey Bataev4acb8592014-07-07 13:01:15 +00001882 return new (Mem) OMPParallelForDirective(CollapsedNum, NumClauses);
1883}
1884
Alexander Musmane4e893b2014-09-23 09:33:00 +00001885OMPParallelForSimdDirective *OMPParallelForSimdDirective::Create(
1886 const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
Alexander Musmana5f070a2014-10-01 06:03:56 +00001887 unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
Alexander Musmanc6388682014-12-15 07:07:06 +00001888 const HelperExprs &Exprs) {
Alexander Musmane4e893b2014-09-23 09:33:00 +00001889 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForSimdDirective),
1890 llvm::alignOf<OMPClause *>());
Alexander Musmanc6388682014-12-15 07:07:06 +00001891 void *Mem = C.Allocate(
1892 Size + sizeof(OMPClause *) * Clauses.size() +
1893 sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_parallel_for_simd));
Alexander Musmane4e893b2014-09-23 09:33:00 +00001894 OMPParallelForSimdDirective *Dir = new (Mem) OMPParallelForSimdDirective(
1895 StartLoc, EndLoc, CollapsedNum, Clauses.size());
1896 Dir->setClauses(Clauses);
1897 Dir->setAssociatedStmt(AssociatedStmt);
Alexander Musmanc6388682014-12-15 07:07:06 +00001898 Dir->setIterationVariable(Exprs.IterationVarRef);
1899 Dir->setLastIteration(Exprs.LastIteration);
1900 Dir->setCalcLastIteration(Exprs.CalcLastIteration);
1901 Dir->setPreCond(Exprs.PreCond);
1902 Dir->setCond(Exprs.Cond, Exprs.SeparatedCond);
1903 Dir->setInit(Exprs.Init);
1904 Dir->setInc(Exprs.Inc);
1905 Dir->setIsLastIterVariable(Exprs.IL);
1906 Dir->setLowerBoundVariable(Exprs.LB);
1907 Dir->setUpperBoundVariable(Exprs.UB);
1908 Dir->setStrideVariable(Exprs.ST);
1909 Dir->setEnsureUpperBound(Exprs.EUB);
1910 Dir->setNextLowerBound(Exprs.NLB);
1911 Dir->setNextUpperBound(Exprs.NUB);
1912 Dir->setCounters(Exprs.Counters);
1913 Dir->setUpdates(Exprs.Updates);
1914 Dir->setFinals(Exprs.Finals);
Alexander Musmane4e893b2014-09-23 09:33:00 +00001915 return Dir;
1916}
1917
1918OMPParallelForSimdDirective *
1919OMPParallelForSimdDirective::CreateEmpty(const ASTContext &C,
1920 unsigned NumClauses,
1921 unsigned CollapsedNum, EmptyShell) {
1922 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelForSimdDirective),
1923 llvm::alignOf<OMPClause *>());
Alexander Musmanc6388682014-12-15 07:07:06 +00001924 void *Mem = C.Allocate(
1925 Size + sizeof(OMPClause *) * NumClauses +
1926 sizeof(Stmt *) * numLoopChildren(CollapsedNum, OMPD_parallel_for_simd));
Alexander Musmane4e893b2014-09-23 09:33:00 +00001927 return new (Mem) OMPParallelForSimdDirective(CollapsedNum, NumClauses);
1928}
1929
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001930OMPParallelSectionsDirective *OMPParallelSectionsDirective::Create(
1931 const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1932 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt) {
1933 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
1934 llvm::alignOf<OMPClause *>());
1935 void *Mem =
1936 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1937 OMPParallelSectionsDirective *Dir =
1938 new (Mem) OMPParallelSectionsDirective(StartLoc, EndLoc, Clauses.size());
1939 Dir->setClauses(Clauses);
1940 Dir->setAssociatedStmt(AssociatedStmt);
1941 return Dir;
1942}
1943
1944OMPParallelSectionsDirective *
1945OMPParallelSectionsDirective::CreateEmpty(const ASTContext &C,
1946 unsigned NumClauses, EmptyShell) {
1947 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelSectionsDirective),
1948 llvm::alignOf<OMPClause *>());
1949 void *Mem =
1950 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1951 return new (Mem) OMPParallelSectionsDirective(NumClauses);
1952}
1953
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001954OMPTaskDirective *OMPTaskDirective::Create(const ASTContext &C,
1955 SourceLocation StartLoc,
1956 SourceLocation EndLoc,
1957 ArrayRef<OMPClause *> Clauses,
1958 Stmt *AssociatedStmt) {
1959 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
1960 llvm::alignOf<OMPClause *>());
1961 void *Mem =
1962 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
1963 OMPTaskDirective *Dir =
1964 new (Mem) OMPTaskDirective(StartLoc, EndLoc, Clauses.size());
1965 Dir->setClauses(Clauses);
1966 Dir->setAssociatedStmt(AssociatedStmt);
1967 return Dir;
1968}
1969
1970OMPTaskDirective *OMPTaskDirective::CreateEmpty(const ASTContext &C,
1971 unsigned NumClauses,
1972 EmptyShell) {
1973 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTaskDirective),
1974 llvm::alignOf<OMPClause *>());
1975 void *Mem =
1976 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
1977 return new (Mem) OMPTaskDirective(NumClauses);
1978}
1979
Alexey Bataev68446b72014-07-18 07:47:19 +00001980OMPTaskyieldDirective *OMPTaskyieldDirective::Create(const ASTContext &C,
1981 SourceLocation StartLoc,
1982 SourceLocation EndLoc) {
1983 void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
1984 OMPTaskyieldDirective *Dir =
1985 new (Mem) OMPTaskyieldDirective(StartLoc, EndLoc);
1986 return Dir;
1987}
1988
1989OMPTaskyieldDirective *OMPTaskyieldDirective::CreateEmpty(const ASTContext &C,
1990 EmptyShell) {
1991 void *Mem = C.Allocate(sizeof(OMPTaskyieldDirective));
1992 return new (Mem) OMPTaskyieldDirective();
1993}
1994
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001995OMPBarrierDirective *OMPBarrierDirective::Create(const ASTContext &C,
1996 SourceLocation StartLoc,
1997 SourceLocation EndLoc) {
1998 void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
1999 OMPBarrierDirective *Dir = new (Mem) OMPBarrierDirective(StartLoc, EndLoc);
2000 return Dir;
2001}
2002
2003OMPBarrierDirective *OMPBarrierDirective::CreateEmpty(const ASTContext &C,
2004 EmptyShell) {
2005 void *Mem = C.Allocate(sizeof(OMPBarrierDirective));
2006 return new (Mem) OMPBarrierDirective();
2007}
2008
Alexey Bataev2df347a2014-07-18 10:17:07 +00002009OMPTaskwaitDirective *OMPTaskwaitDirective::Create(const ASTContext &C,
2010 SourceLocation StartLoc,
2011 SourceLocation EndLoc) {
2012 void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
2013 OMPTaskwaitDirective *Dir = new (Mem) OMPTaskwaitDirective(StartLoc, EndLoc);
2014 return Dir;
2015}
2016
2017OMPTaskwaitDirective *OMPTaskwaitDirective::CreateEmpty(const ASTContext &C,
2018 EmptyShell) {
2019 void *Mem = C.Allocate(sizeof(OMPTaskwaitDirective));
2020 return new (Mem) OMPTaskwaitDirective();
2021}
2022
Alexey Bataev6125da92014-07-21 11:26:11 +00002023OMPFlushDirective *OMPFlushDirective::Create(const ASTContext &C,
2024 SourceLocation StartLoc,
2025 SourceLocation EndLoc,
2026 ArrayRef<OMPClause *> Clauses) {
2027 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
2028 llvm::alignOf<OMPClause *>());
2029 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size());
2030 OMPFlushDirective *Dir =
2031 new (Mem) OMPFlushDirective(StartLoc, EndLoc, Clauses.size());
2032 Dir->setClauses(Clauses);
2033 return Dir;
2034}
2035
2036OMPFlushDirective *OMPFlushDirective::CreateEmpty(const ASTContext &C,
2037 unsigned NumClauses,
2038 EmptyShell) {
2039 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPFlushDirective),
2040 llvm::alignOf<OMPClause *>());
2041 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * NumClauses);
2042 return new (Mem) OMPFlushDirective(NumClauses);
2043}
2044
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002045OMPOrderedDirective *OMPOrderedDirective::Create(const ASTContext &C,
2046 SourceLocation StartLoc,
2047 SourceLocation EndLoc,
2048 Stmt *AssociatedStmt) {
2049 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPOrderedDirective),
2050 llvm::alignOf<Stmt *>());
2051 void *Mem = C.Allocate(Size + sizeof(Stmt *));
2052 OMPOrderedDirective *Dir = new (Mem) OMPOrderedDirective(StartLoc, EndLoc);
2053 Dir->setAssociatedStmt(AssociatedStmt);
2054 return Dir;
2055}
2056
2057OMPOrderedDirective *OMPOrderedDirective::CreateEmpty(const ASTContext &C,
2058 EmptyShell) {
2059 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPOrderedDirective),
2060 llvm::alignOf<Stmt *>());
2061 void *Mem = C.Allocate(Size + sizeof(Stmt *));
2062 return new (Mem) OMPOrderedDirective();
2063}
2064
Alexey Bataevb78ca832015-04-01 03:33:17 +00002065OMPAtomicDirective *OMPAtomicDirective::Create(
2066 const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
2067 ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt, Expr *X, Expr *V,
2068 Expr *E, Expr *UE, bool IsXLHSInRHSPart, bool IsPostfixUpdate) {
Alexey Bataev0162e452014-07-22 10:10:35 +00002069 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPAtomicDirective),
2070 llvm::alignOf<OMPClause *>());
Alexey Bataev62cec442014-11-18 10:14:22 +00002071 void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() +
Alexey Bataev1d160b12015-03-13 12:27:31 +00002072 5 * sizeof(Stmt *));
Alexey Bataev0162e452014-07-22 10:10:35 +00002073 OMPAtomicDirective *Dir =
2074 new (Mem) OMPAtomicDirective(StartLoc, EndLoc, Clauses.size());
2075 Dir->setClauses(Clauses);
2076 Dir->setAssociatedStmt(AssociatedStmt);
Alexey Bataev62cec442014-11-18 10:14:22 +00002077 Dir->setX(X);
2078 Dir->setV(V);
2079 Dir->setExpr(E);
Alexey Bataevb4505a72015-03-30 05:20:59 +00002080 Dir->setUpdateExpr(UE);
2081 Dir->IsXLHSInRHSPart = IsXLHSInRHSPart;
Alexey Bataevb78ca832015-04-01 03:33:17 +00002082 Dir->IsPostfixUpdate = IsPostfixUpdate;
Alexey Bataev0162e452014-07-22 10:10:35 +00002083 return Dir;
2084}
2085
2086OMPAtomicDirective *OMPAtomicDirective::CreateEmpty(const ASTContext &C,
2087 unsigned NumClauses,
2088 EmptyShell) {
2089 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPAtomicDirective),
2090 llvm::alignOf<OMPClause *>());
2091 void *Mem =
Alexey Bataev1d160b12015-03-13 12:27:31 +00002092 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + 5 * sizeof(Stmt *));
Alexey Bataev0162e452014-07-22 10:10:35 +00002093 return new (Mem) OMPAtomicDirective(NumClauses);
2094}
2095
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002096OMPTargetDirective *OMPTargetDirective::Create(const ASTContext &C,
2097 SourceLocation StartLoc,
2098 SourceLocation EndLoc,
2099 ArrayRef<OMPClause *> Clauses,
2100 Stmt *AssociatedStmt) {
2101 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTargetDirective),
2102 llvm::alignOf<OMPClause *>());
2103 void *Mem =
2104 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
2105 OMPTargetDirective *Dir =
2106 new (Mem) OMPTargetDirective(StartLoc, EndLoc, Clauses.size());
2107 Dir->setClauses(Clauses);
2108 Dir->setAssociatedStmt(AssociatedStmt);
2109 return Dir;
2110}
2111
2112OMPTargetDirective *OMPTargetDirective::CreateEmpty(const ASTContext &C,
2113 unsigned NumClauses,
2114 EmptyShell) {
2115 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTargetDirective),
2116 llvm::alignOf<OMPClause *>());
2117 void *Mem =
2118 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
2119 return new (Mem) OMPTargetDirective(NumClauses);
2120}
2121
Alexey Bataev13314bf2014-10-09 04:18:56 +00002122OMPTeamsDirective *OMPTeamsDirective::Create(const ASTContext &C,
2123 SourceLocation StartLoc,
2124 SourceLocation EndLoc,
2125 ArrayRef<OMPClause *> Clauses,
2126 Stmt *AssociatedStmt) {
2127 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTeamsDirective),
2128 llvm::alignOf<OMPClause *>());
2129 void *Mem =
2130 C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *));
2131 OMPTeamsDirective *Dir =
2132 new (Mem) OMPTeamsDirective(StartLoc, EndLoc, Clauses.size());
2133 Dir->setClauses(Clauses);
2134 Dir->setAssociatedStmt(AssociatedStmt);
2135 return Dir;
2136}
2137
2138OMPTeamsDirective *OMPTeamsDirective::CreateEmpty(const ASTContext &C,
2139 unsigned NumClauses,
2140 EmptyShell) {
2141 unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPTeamsDirective),
2142 llvm::alignOf<OMPClause *>());
2143 void *Mem =
2144 C.Allocate(Size + sizeof(OMPClause *) * NumClauses + sizeof(Stmt *));
2145 return new (Mem) OMPTeamsDirective(NumClauses);
2146}
2147