blob: 697cdc3fb36081eecd2ebd527fe7fe47e03718f3 [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"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000018#include "clang/AST/ExprOpenMP.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000019#include "clang/AST/Stmt.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000020#include "clang/AST/StmtCXX.h"
21#include "clang/AST/StmtObjC.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtOpenMP.h"
Sebastian Redl54c04d42008-12-22 19:15:10 +000023#include "clang/AST/Type.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000024#include "clang/Basic/CharInfo.h"
Chris Lattner1a8f3942010-04-23 16:29:58 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000026#include "clang/Lex/Token.h"
Chad Rosier14836ba2012-08-24 17:05:45 +000027#include "llvm/ADT/StringExtras.h"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnerf42cce72006-10-25 04:09:21 +000029using namespace clang;
30
Steve Narofff84d11f2007-05-23 21:48:04 +000031static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000032 const char *Name;
33 unsigned Counter;
34 unsigned Size;
Alexis Hunt656bb312010-05-05 15:24:00 +000035} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner4d15a0d2007-08-25 01:42:24 +000036
37static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
38 static bool Initialized = false;
39 if (Initialized)
40 return StmtClassInfo[E];
41
42 // Intialize the table on the first use.
43 Initialized = true;
Alexis Huntabb2ac82010-05-18 06:22:21 +000044#define ABSTRACT_STMT(STMT)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000045#define STMT(CLASS, PARENT) \
46 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
47 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Alexis Hunt656bb312010-05-05 15:24:00 +000048#include "clang/AST/StmtNodes.inc"
Nico Weberde565e32008-08-05 23:15:29 +000049
Chris Lattner4d15a0d2007-08-25 01:42:24 +000050 return StmtClassInfo[E];
51}
52
Craig Topper37932912013-08-18 10:09:15 +000053void *Stmt::operator new(size_t bytes, const ASTContext& C,
Craig Topper5a050012013-08-18 17:45:38 +000054 unsigned alignment) {
Benjamin Kramerea70eb32012-12-01 15:09:41 +000055 return ::operator new(bytes, C, alignment);
56}
57
Steve Narofff1e53692007-03-23 22:27:02 +000058const char *Stmt::getStmtClassName() const {
John McCall925b16622010-10-26 08:39:16 +000059 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000060}
Steve Narofff84d11f2007-05-23 21:48:04 +000061
62void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000063 // Ensure the table is primed.
64 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000065
Steve Narofff84d11f2007-05-23 21:48:04 +000066 unsigned sum = 0;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000067 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Alexis Hunt656bb312010-05-05 15:24:00 +000068 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000069 if (StmtClassInfo[i].Name == nullptr) continue;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000070 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000071 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000072 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000073 sum = 0;
Alexis Hunt656bb312010-05-05 15:24:00 +000074 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000075 if (StmtClassInfo[i].Name == nullptr) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +000076 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000077 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
78 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
79 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
80 << " bytes)\n";
Chris Lattner4d15a0d2007-08-25 01:42:24 +000081 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +000082 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000083
84 llvm::errs() << "Total bytes = " << sum << "\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000085}
86
87void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000088 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000089}
90
Daniel Dunbar62905572012-03-05 21:42:49 +000091bool Stmt::StatisticsEnabled = false;
92void Stmt::EnableStatistics() {
93 StatisticsEnabled = true;
Steve Narofff84d11f2007-05-23 21:48:04 +000094}
95
John McCall4db5c3c2011-07-07 06:58:02 +000096Stmt *Stmt::IgnoreImplicit() {
97 Stmt *s = this;
98
Richard Smith520449d2015-02-05 06:15:50 +000099 if (auto *ewc = dyn_cast<ExprWithCleanups>(s))
John McCall4db5c3c2011-07-07 06:58:02 +0000100 s = ewc->getSubExpr();
101
Richard Smith520449d2015-02-05 06:15:50 +0000102 if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s))
103 s = mte->GetTemporaryExpr();
104
105 if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s))
106 s = bte->getSubExpr();
107
108 while (auto *ice = dyn_cast<ImplicitCastExpr>(s))
John McCall4db5c3c2011-07-07 06:58:02 +0000109 s = ice->getSubExpr();
110
111 return s;
112}
113
Alexander Musmana5f070a2014-10-01 06:03:56 +0000114/// \brief Skip no-op (attributed, compound) container stmts and skip captured
115/// stmt at the top, if \a IgnoreCaptured is true.
116Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
117 Stmt *S = this;
118 if (IgnoreCaptured)
119 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
120 S = CapS->getCapturedStmt();
121 while (true) {
122 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
123 S = AS->getSubStmt();
124 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
125 if (CS->size() != 1)
126 break;
127 S = CS->body_back();
128 } else
129 break;
130 }
131 return S;
132}
133
Chandler Carrutha626d642011-09-10 00:02:34 +0000134/// \brief Strip off all label-like statements.
135///
Richard Smithc202b282012-04-14 00:33:13 +0000136/// This will strip off label statements, case statements, attributed
137/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000138const Stmt *Stmt::stripLabelLikeStatements() const {
139 const Stmt *S = this;
140 while (true) {
141 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
142 S = LS->getSubStmt();
143 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
144 S = SC->getSubStmt();
Richard Smithc202b282012-04-14 00:33:13 +0000145 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
146 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000147 else
148 return S;
149 }
150}
151
John McCallbd066782011-02-09 08:16:59 +0000152namespace {
153 struct good {};
154 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000155
156 // These silly little functions have to be static inline to suppress
157 // unused warnings, and they have to be defined to suppress other
158 // warnings.
Nick Lewyckybae992f2011-02-09 08:42:57 +0000159 static inline good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000160
161 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000162 template <class T> good implements_children(children_t T::*) {
163 return good();
164 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000165 LLVM_ATTRIBUTE_UNUSED
Nick Lewyckybae992f2011-02-09 08:42:57 +0000166 static inline bad implements_children(children_t Stmt::*) {
167 return bad();
168 }
John McCallbd066782011-02-09 08:16:59 +0000169
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000170 typedef SourceLocation getLocStart_t() const;
171 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000172 return good();
173 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000174 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000175 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
176 return bad();
177 }
178
179 typedef SourceLocation getLocEnd_t() const;
180 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
181 return good();
182 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000183 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000184 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000185 return bad();
186 }
John McCallbd066782011-02-09 08:16:59 +0000187
188#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000189 (void) is_good(implements_children(&type::children))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000190#define ASSERT_IMPLEMENTS_getLocStart(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000191 (void) is_good(implements_getLocStart(&type::getLocStart))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000192#define ASSERT_IMPLEMENTS_getLocEnd(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000193 (void) is_good(implements_getLocEnd(&type::getLocEnd))
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000194}
John McCallbd066782011-02-09 08:16:59 +0000195
196/// Check whether the various Stmt classes implement their member
197/// functions.
Eli Friedmandc41d792013-09-10 22:57:15 +0000198LLVM_ATTRIBUTE_UNUSED
John McCallbd066782011-02-09 08:16:59 +0000199static inline void check_implementations() {
200#define ABSTRACT_STMT(type)
201#define STMT(type, base) \
202 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000203 ASSERT_IMPLEMENTS_getLocStart(type); \
204 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCallbd066782011-02-09 08:16:59 +0000205#include "clang/AST/StmtNodes.inc"
206}
207
208Stmt::child_range Stmt::children() {
209 switch (getStmtClass()) {
210 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
211#define ABSTRACT_STMT(type)
212#define STMT(type, base) \
213 case Stmt::type##Class: \
214 return static_cast<type*>(this)->children();
215#include "clang/AST/StmtNodes.inc"
216 }
217 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000218}
219
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000220// Amusing macro metaprogramming hack: check whether a class provides
221// a more specific implementation of getSourceRange.
222//
223// See also Expr.cpp:getExprLoc().
224namespace {
225 /// This implementation is used when a class provides a custom
226 /// implementation of getSourceRange.
227 template <class S, class T>
228 SourceRange getSourceRangeImpl(const Stmt *stmt,
229 SourceRange (T::*v)() const) {
230 return static_cast<const S*>(stmt)->getSourceRange();
231 }
232
233 /// This implementation is used when a class doesn't provide a custom
234 /// implementation of getSourceRange. Overload resolution should pick it over
235 /// the implementation above because it's more specialized according to
236 /// function template partial ordering.
237 template <class S>
238 SourceRange getSourceRangeImpl(const Stmt *stmt,
239 SourceRange (Stmt::*v)() const) {
240 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
241 static_cast<const S*>(stmt)->getLocEnd());
242 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000243}
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000244
John McCallbd066782011-02-09 08:16:59 +0000245SourceRange Stmt::getSourceRange() const {
246 switch (getStmtClass()) {
247 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
248#define ABSTRACT_STMT(type)
249#define STMT(type, base) \
250 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000251 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000252#include "clang/AST/StmtNodes.inc"
253 }
254 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000255}
256
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000257SourceLocation Stmt::getLocStart() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000258// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000259 switch (getStmtClass()) {
260 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
261#define ABSTRACT_STMT(type)
262#define STMT(type, base) \
263 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000264 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000265#include "clang/AST/StmtNodes.inc"
266 }
267 llvm_unreachable("unknown statement kind");
268}
269
270SourceLocation Stmt::getLocEnd() const {
271 switch (getStmtClass()) {
272 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
273#define ABSTRACT_STMT(type)
274#define STMT(type, base) \
275 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000276 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000277#include "clang/AST/StmtNodes.inc"
278 }
279 llvm_unreachable("unknown statement kind");
280}
281
Craig Toppere6960e22013-08-22 05:28:54 +0000282CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000283 SourceLocation LB, SourceLocation RB)
Aaron Ballmance6c67e2014-10-22 21:06:18 +0000284 : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000285 CompoundStmtBits.NumStmts = Stmts.size();
286 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000287 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
288
Nico Webera2a0eb92012-12-29 20:03:39 +0000289 if (Stmts.size() == 0) {
Craig Topper36250ad2014-05-12 05:36:57 +0000290 Body = nullptr;
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000291 return;
292 }
293
Nico Webera2a0eb92012-12-29 20:03:39 +0000294 Body = new (C) Stmt*[Stmts.size()];
295 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000296}
297
Craig Topper9ee84ad2015-12-04 05:01:44 +0000298void CompoundStmt::setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts) {
299 if (Body)
Douglas Gregora9af1d12009-04-17 00:04:06 +0000300 C.Deallocate(Body);
Craig Topper9ee84ad2015-12-04 05:01:44 +0000301 CompoundStmtBits.NumStmts = Stmts.size();
302 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
303 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
Douglas Gregora9af1d12009-04-17 00:04:06 +0000304
Craig Topper9ee84ad2015-12-04 05:01:44 +0000305 Body = new (C) Stmt*[Stmts.size()];
306 std::copy(Stmts.begin(), Stmts.end(), Body);
Douglas Gregora9af1d12009-04-17 00:04:06 +0000307}
Steve Narofff84d11f2007-05-23 21:48:04 +0000308
Chris Lattnereefa10e2007-05-28 06:56:27 +0000309const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000310 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000311}
312
Craig Toppere6960e22013-08-22 05:28:54 +0000313AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000314 ArrayRef<const Attr*> Attrs,
315 Stmt *SubStmt) {
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000316 assert(!Attrs.empty() && "Attrs should not be empty");
317 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * Attrs.size(),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000318 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000319 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
320}
321
Craig Toppere6960e22013-08-22 05:28:54 +0000322AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
323 unsigned NumAttrs) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000324 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000325 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * NumAttrs,
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000326 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000327 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
328}
329
Craig Topperc571c812013-08-22 06:02:26 +0000330std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000331 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
332 return gccAsmStmt->generateAsmString(C);
333 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
334 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000335 llvm_unreachable("unknown asm statement kind!");
336}
337
338StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000339 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
340 return gccAsmStmt->getOutputConstraint(i);
341 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
342 return msAsmStmt->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000343 llvm_unreachable("unknown asm statement kind!");
344}
345
346const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000347 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
348 return gccAsmStmt->getOutputExpr(i);
349 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
350 return msAsmStmt->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000351 llvm_unreachable("unknown asm statement kind!");
352}
353
354StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000355 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
356 return gccAsmStmt->getInputConstraint(i);
357 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
358 return msAsmStmt->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000359 llvm_unreachable("unknown asm statement kind!");
360}
361
362const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000363 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
364 return gccAsmStmt->getInputExpr(i);
365 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
366 return msAsmStmt->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000367 llvm_unreachable("unknown asm statement kind!");
368}
369
370StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000371 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
372 return gccAsmStmt->getClobber(i);
373 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
374 return msAsmStmt->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000375 llvm_unreachable("unknown asm statement kind!");
376}
377
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000378/// getNumPlusOperands - Return the number of output operands that have a "+"
379/// constraint.
380unsigned AsmStmt::getNumPlusOperands() const {
381 unsigned Res = 0;
382 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
383 if (isOutputPlusConstraint(i))
384 ++Res;
385 return Res;
386}
387
Akira Hatanaka987f1862014-08-22 06:05:21 +0000388char GCCAsmStmt::AsmStringPiece::getModifier() const {
389 assert(isOperand() && "Only Operands can have modifiers.");
390 return isLetter(Str[0]) ? Str[0] : '\0';
391}
392
Chad Rosier6100ae12012-08-27 23:47:56 +0000393StringRef GCCAsmStmt::getClobber(unsigned i) const {
394 return getClobberStringLiteral(i)->getString();
395}
396
Chad Rosierde70e0e2012-08-25 00:11:56 +0000397Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000398 return cast<Expr>(Exprs[i]);
399}
Chris Lattner72bbf172009-03-10 04:59:06 +0000400
401/// getOutputConstraint - Return the constraint string for the specified
402/// output operand. All output constraints are known to be non-empty (either
403/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000404StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000405 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000406}
Chris Lattner72bbf172009-03-10 04:59:06 +0000407
Chad Rosierde70e0e2012-08-25 00:11:56 +0000408Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000409 return cast<Expr>(Exprs[i + NumOutputs]);
410}
Chad Rosierde70e0e2012-08-25 00:11:56 +0000411void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000412 Exprs[i + NumOutputs] = E;
413}
414
Chris Lattner72bbf172009-03-10 04:59:06 +0000415/// getInputConstraint - Return the specified input constraint. Unlike output
416/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000417StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000418 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000419}
420
Craig Topperc571c812013-08-22 06:02:26 +0000421void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
422 IdentifierInfo **Names,
423 StringLiteral **Constraints,
424 Stmt **Exprs,
425 unsigned NumOutputs,
426 unsigned NumInputs,
427 StringLiteral **Clobbers,
428 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000429 this->NumOutputs = NumOutputs;
430 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000431 this->NumClobbers = NumClobbers;
432
433 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000434
Anders Carlsson98323d22010-01-30 23:19:41 +0000435 C.Deallocate(this->Names);
436 this->Names = new (C) IdentifierInfo*[NumExprs];
437 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000438
Anders Carlsson98323d22010-01-30 23:19:41 +0000439 C.Deallocate(this->Exprs);
440 this->Exprs = new (C) Stmt*[NumExprs];
441 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000442
Anders Carlsson98323d22010-01-30 23:19:41 +0000443 C.Deallocate(this->Constraints);
444 this->Constraints = new (C) StringLiteral*[NumExprs];
445 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000446
Anders Carlsson98323d22010-01-30 23:19:41 +0000447 C.Deallocate(this->Clobbers);
448 this->Clobbers = new (C) StringLiteral*[NumClobbers];
449 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000450}
451
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000452/// getNamedOperand - Given a symbolic operand reference like %[foo],
453/// translate this into a numeric value needed to reference the same operand.
454/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000455int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000456 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000458 // Check if this is an output operand.
459 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
460 if (getOutputName(i) == SymbolicName)
461 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000464 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
465 if (getInputName(i) == SymbolicName)
466 return getNumOutputs() + NumPlusOperands + i;
467
468 // Not found.
469 return -1;
470}
471
Chris Lattner35b58362009-03-10 23:21:44 +0000472/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
473/// it into pieces. If the asm string is erroneous, emit errors and return
474/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000475unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000476 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000477 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000478 const char *StrStart = Str.begin();
479 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000480 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattner35b58362009-03-10 23:21:44 +0000482 // "Simple" inline asms have no constraints or operands, just convert the asm
483 // string to escape $'s.
484 if (isSimple()) {
485 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000486 for (; CurPtr != StrEnd; ++CurPtr) {
487 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000488 case '$':
489 Result += "$$";
490 break;
491 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000492 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000493 break;
494 }
495 }
496 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000497 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000498 }
499
500 // CurStringPiece - The current string that we are building up as we scan the
501 // asm string.
502 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregore8bbc122011-09-02 00:18:52 +0000504 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000505
Richard Smithd18ab802016-05-16 22:52:23 +0000506 unsigned LastAsmStringToken = 0;
507 unsigned LastAsmStringOffset = 0;
508
Chris Lattner35b58362009-03-10 23:21:44 +0000509 while (1) {
510 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000511 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000512 if (!CurStringPiece.empty())
513 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000514 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000515 }
Mike Stump11289f42009-09-09 15:08:12 +0000516
Chris Lattnera41b8472009-03-10 23:51:40 +0000517 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000518 switch (CurChar) {
519 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000520 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
521 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
522 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000523 case '%':
524 break;
525 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000526 CurStringPiece += CurChar;
527 continue;
528 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000529
Chris Lattner35b58362009-03-10 23:21:44 +0000530 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000531 if (CurPtr == StrEnd) {
532 // % at end of string is invalid (no escape).
533 DiagOffs = CurPtr-StrStart-1;
534 return diag::err_asm_invalid_escape;
535 }
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000536 // Handle escaped char and continue looping over the asm string.
Chris Lattnera41b8472009-03-10 23:51:40 +0000537 char EscapedChar = *CurPtr++;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000538 switch (EscapedChar) {
539 default:
540 break;
541 case '%': // %% -> %
542 case '{': // %{ -> {
543 case '}': // %} -> }
544 CurStringPiece += EscapedChar;
Chris Lattner35b58362009-03-10 23:21:44 +0000545 continue;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000546 case '=': // %= -> Generate a unique ID.
Chris Lattner35b58362009-03-10 23:21:44 +0000547 CurStringPiece += "${:uid}";
548 continue;
549 }
Mike Stump11289f42009-09-09 15:08:12 +0000550
Chris Lattner35b58362009-03-10 23:21:44 +0000551 // Otherwise, we have an operand. If we have accumulated a string so far,
552 // add it to the Pieces list.
553 if (!CurStringPiece.empty()) {
554 Pieces.push_back(AsmStringPiece(CurStringPiece));
555 CurStringPiece.clear();
556 }
Mike Stump11289f42009-09-09 15:08:12 +0000557
Akira Hatanaka987f1862014-08-22 06:05:21 +0000558 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
559 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
560
561 const char *Begin = CurPtr - 1; // Points to the character following '%'.
562 const char *Percent = Begin - 1; // Points to '%'.
563
Jordan Rosea7d03842013-02-08 22:30:41 +0000564 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000565 if (CurPtr == StrEnd) { // Premature end.
566 DiagOffs = CurPtr-StrStart-1;
567 return diag::err_asm_invalid_escape;
568 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000569 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000570 }
Mike Stump11289f42009-09-09 15:08:12 +0000571
Akira Hatanaka987f1862014-08-22 06:05:21 +0000572 const TargetInfo &TI = C.getTargetInfo();
573 const SourceManager &SM = C.getSourceManager();
574 const LangOptions &LO = C.getLangOpts();
575
576 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000577 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000578 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000579 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattner99d892b2009-03-11 22:52:17 +0000581 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000582 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000583 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000584
Chris Lattner14311922009-03-11 00:23:13 +0000585 unsigned NumOperands =
586 getNumOutputs() + getNumPlusOperands() + getNumInputs();
587 if (N >= NumOperands) {
588 DiagOffs = CurPtr-StrStart-1;
589 return diag::err_asm_invalid_operand_number;
590 }
591
Akira Hatanaka987f1862014-08-22 06:05:21 +0000592 // Str contains "x4" (Operand without the leading %).
593 std::string Str(Begin, CurPtr - Begin);
594
595 // (BeginLoc, EndLoc) represents the range of the operand we are currently
596 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000597 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
598 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
599 &LastAsmStringOffset);
600 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
601 CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken,
602 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000603
Benjamin Kramer3204b152015-05-29 19:42:19 +0000604 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Chris Lattner35b58362009-03-10 23:21:44 +0000605 continue;
606 }
Mike Stump11289f42009-09-09 15:08:12 +0000607
Akira Hatanaka987f1862014-08-22 06:05:21 +0000608 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000609 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000610 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000611
Chris Lattner3fa25c62009-03-11 00:06:36 +0000612 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000613 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000614 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000615 return diag::err_asm_unterminated_symbolic_operand_name;
616 if (NameEnd == CurPtr)
617 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000619 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattner35b58362009-03-10 23:21:44 +0000621 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000622 if (N == -1) {
623 // Verify that an operand with that name exists.
624 DiagOffs = CurPtr-StrStart;
625 return diag::err_asm_unknown_symbolic_operand_name;
626 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000627
628 // Str contains "x[foo]" (Operand without the leading %).
629 std::string Str(Begin, NameEnd + 1 - Begin);
630
631 // (BeginLoc, EndLoc) represents the range of the operand we are currently
632 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000633 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
634 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
635 &LastAsmStringOffset);
636 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
637 NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken,
638 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000639
Benjamin Kramer3204b152015-05-29 19:42:19 +0000640 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattner3fa25c62009-03-11 00:06:36 +0000642 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000643 continue;
644 }
Mike Stump11289f42009-09-09 15:08:12 +0000645
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000646 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000647 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000648 }
649}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000650
651/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000652std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000653 // Analyze the asm string to decompose it into its pieces. We know that Sema
654 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000655 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000656 unsigned DiagOffs;
657 AnalyzeAsmString(Pieces, C, DiagOffs);
658
659 std::string AsmString;
660 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
661 if (Pieces[i].isString())
662 AsmString += Pieces[i].getString();
663 else if (Pieces[i].getModifier() == '\0')
664 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
665 else
666 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
667 Pieces[i].getModifier() + '}';
668 }
669 return AsmString;
670}
Chris Lattner35b58362009-03-10 23:21:44 +0000671
Chad Rosier3b0c2602012-08-27 20:23:31 +0000672/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000673std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000674 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000675 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000676}
677
Chad Rosierfe31e622012-08-24 00:07:09 +0000678Expr *MSAsmStmt::getOutputExpr(unsigned i) {
679 return cast<Expr>(Exprs[i]);
680}
681
682Expr *MSAsmStmt::getInputExpr(unsigned i) {
683 return cast<Expr>(Exprs[i + NumOutputs]);
684}
685void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
686 Exprs[i + NumOutputs] = E;
687}
688
Chris Lattner86f5e132008-01-30 05:01:46 +0000689//===----------------------------------------------------------------------===//
690// Constructors
691//===----------------------------------------------------------------------===//
692
Craig Toppere6960e22013-08-22 05:28:54 +0000693GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
694 bool issimple, bool isvolatile, unsigned numoutputs,
695 unsigned numinputs, IdentifierInfo **names,
696 StringLiteral **constraints, Expr **exprs,
697 StringLiteral *asmstr, unsigned numclobbers,
698 StringLiteral **clobbers, SourceLocation rparenloc)
Chad Rosierfe3352e2012-08-27 19:38:01 +0000699 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
700 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weberde565e32008-08-05 23:15:29 +0000701
Chad Rosier42032fa2012-08-07 23:12:23 +0000702 unsigned NumExprs = NumOutputs + NumInputs;
703
Anders Carlsson98323d22010-01-30 23:19:41 +0000704 Names = new (C) IdentifierInfo*[NumExprs];
705 std::copy(names, names + NumExprs, Names);
706
707 Exprs = new (C) Stmt*[NumExprs];
708 std::copy(exprs, exprs + NumExprs, Exprs);
709
710 Constraints = new (C) StringLiteral*[NumExprs];
711 std::copy(constraints, constraints + NumExprs, Constraints);
712
713 Clobbers = new (C) StringLiteral*[NumClobbers];
714 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000715}
716
Craig Toppere6960e22013-08-22 05:28:54 +0000717MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000718 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000719 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000720 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000721 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
722 StringRef asmstr, ArrayRef<StringRef> clobbers,
723 SourceLocation endloc)
724 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
725 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
John McCallf413f5e2013-05-03 00:10:13 +0000726 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000727
John McCallf413f5e2013-05-03 00:10:13 +0000728 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
729}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000730
Craig Toppere6960e22013-08-22 05:28:54 +0000731static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
Benjamin Kramer2ab0d882015-08-04 12:34:23 +0000732 return str.copy(C);
John McCallf413f5e2013-05-03 00:10:13 +0000733}
734
Craig Toppere6960e22013-08-22 05:28:54 +0000735void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000736 ArrayRef<Token> asmtoks,
737 ArrayRef<StringRef> constraints,
738 ArrayRef<Expr*> exprs,
739 ArrayRef<StringRef> clobbers) {
740 assert(NumAsmToks == asmtoks.size());
741 assert(NumClobbers == clobbers.size());
742
Craig Toppercaf138e2015-12-05 07:41:42 +0000743 assert(exprs.size() == NumOutputs + NumInputs);
744 assert(exprs.size() == constraints.size());
John McCallf413f5e2013-05-03 00:10:13 +0000745
746 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000747
Craig Toppercaf138e2015-12-05 07:41:42 +0000748 Exprs = new (C) Stmt*[exprs.size()];
749 std::copy(exprs.begin(), exprs.end(), Exprs);
Chad Rosierfe31e622012-08-24 00:07:09 +0000750
Craig Toppercaf138e2015-12-05 07:41:42 +0000751 AsmToks = new (C) Token[asmtoks.size()];
752 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000753
Craig Toppercaf138e2015-12-05 07:41:42 +0000754 Constraints = new (C) StringRef[exprs.size()];
755 std::transform(constraints.begin(), constraints.end(), Constraints,
756 [&](StringRef Constraint) {
757 return copyIntoContext(C, Constraint);
758 });
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000759
Chad Rosierbaf53f92012-08-10 21:36:25 +0000760 Clobbers = new (C) StringRef[NumClobbers];
Craig Toppercaf138e2015-12-05 07:41:42 +0000761 // FIXME: Avoid the allocation/copy if at all possible.
762 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
763 [&](StringRef Clobber) {
764 return copyIntoContext(C, Clobber);
765 });
Chad Rosier32503022012-06-11 20:47:18 +0000766}
767
Richard Smithb130fe72016-06-23 19:16:49 +0000768IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, bool IsConstexpr,
Richard Smitha547eb22016-07-14 00:11:03 +0000769 Stmt *init, VarDecl *var, Expr *cond, Stmt *then,
770 SourceLocation EL, Stmt *elsev)
Richard Smithb130fe72016-06-23 19:16:49 +0000771 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL) {
772 setConstexpr(IsConstexpr);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000773 setConditionVariable(C, var);
Richard Smitha547eb22016-07-14 00:11:03 +0000774 SubExprs[INIT] = init;
Pavel Labath515f4db2013-09-03 14:41:16 +0000775 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000776 SubExprs[THEN] = then;
Chad Rosier42032fa2012-08-07 23:12:23 +0000777 SubExprs[ELSE] = elsev;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000778}
779
780VarDecl *IfStmt::getConditionVariable() const {
781 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000782 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000783
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000784 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
785 return cast<VarDecl>(DS->getSingleDecl());
786}
787
Craig Toppere6960e22013-08-22 05:28:54 +0000788void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000789 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000790 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000791 return;
792 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000793
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000794 SourceRange VarRange = V->getSourceRange();
795 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
796 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000797}
798
Erik Pilkington5cd57172016-08-16 17:44:11 +0000799bool IfStmt::isObjCAvailabilityCheck() const {
800 return isa<ObjCAvailabilityCheckExpr>(SubExprs[COND]);
801}
802
Craig Toppere6960e22013-08-22 05:28:54 +0000803ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000804 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000805 SourceLocation RP)
Chad Rosier42032fa2012-08-07 23:12:23 +0000806 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000807{
808 SubExprs[INIT] = Init;
809 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000810 SubExprs[COND] = Cond;
811 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000812 SubExprs[BODY] = Body;
813}
814
815VarDecl *ForStmt::getConditionVariable() const {
816 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000817 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000818
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000819 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
820 return cast<VarDecl>(DS->getSingleDecl());
821}
822
Craig Toppere6960e22013-08-22 05:28:54 +0000823void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000824 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000825 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000826 return;
827 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000828
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000829 SourceRange VarRange = V->getSourceRange();
830 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
831 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000832}
833
Richard Smitha547eb22016-07-14 00:11:03 +0000834SwitchStmt::SwitchStmt(const ASTContext &C, Stmt *init, VarDecl *Var,
835 Expr *cond)
Benjamin Kramer475386d2015-04-02 15:29:07 +0000836 : Stmt(SwitchStmtClass), FirstCase(nullptr, false) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000837 setConditionVariable(C, Var);
Richard Smitha547eb22016-07-14 00:11:03 +0000838 SubExprs[INIT] = init;
Pavel Labath515f4db2013-09-03 14:41:16 +0000839 SubExprs[COND] = cond;
Craig Topper36250ad2014-05-12 05:36:57 +0000840 SubExprs[BODY] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000841}
842
843VarDecl *SwitchStmt::getConditionVariable() const {
844 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000845 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000846
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000847 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
848 return cast<VarDecl>(DS->getSingleDecl());
849}
850
Craig Toppere6960e22013-08-22 05:28:54 +0000851void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000852 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000853 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000854 return;
855 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000856
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000857 SourceRange VarRange = V->getSourceRange();
858 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
859 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000860}
861
John McCallbd066782011-02-09 08:16:59 +0000862Stmt *SwitchCase::getSubStmt() {
Chris Lattner80d51eb2011-02-28 00:18:06 +0000863 if (isa<CaseStmt>(this))
864 return cast<CaseStmt>(this)->getSubStmt();
John McCallbd066782011-02-09 08:16:59 +0000865 return cast<DefaultStmt>(this)->getSubStmt();
866}
867
Craig Toppere6960e22013-08-22 05:28:54 +0000868WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000869 SourceLocation WL)
Chris Lattner80d51eb2011-02-28 00:18:06 +0000870 : Stmt(WhileStmtClass) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000871 setConditionVariable(C, Var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000872 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000873 SubExprs[BODY] = body;
874 WhileLoc = WL;
875}
876
877VarDecl *WhileStmt::getConditionVariable() const {
878 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000879 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000880
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000881 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
882 return cast<VarDecl>(DS->getSingleDecl());
883}
884
Craig Toppere6960e22013-08-22 05:28:54 +0000885void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000886 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000887 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000888 return;
889 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000890
891 SourceRange VarRange = V->getSourceRange();
892 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
893 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000894}
895
Ted Kremenek066dd932007-08-24 21:09:09 +0000896// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000897LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall9de91602010-10-28 08:53:48 +0000898 if (AddrLabelExpr *E =
899 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
900 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +0000901 return nullptr;
John McCall9de91602010-10-28 08:53:48 +0000902}
Ted Kremenek066dd932007-08-24 21:09:09 +0000903
Ted Kremenek066dd932007-08-24 21:09:09 +0000904// ReturnStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000905const Expr* ReturnStmt::getRetValue() const {
906 return cast_or_null<Expr>(RetExpr);
907}
908Expr* ReturnStmt::getRetValue() {
909 return cast_or_null<Expr>(RetExpr);
Ted Kremenek066dd932007-08-24 21:09:09 +0000910}
John Wiegley1c0675e2011-04-28 01:08:34 +0000911
Warren Huntf6be4cb2014-07-25 20:52:51 +0000912SEHTryStmt::SEHTryStmt(bool IsCXXTry,
913 SourceLocation TryLoc,
914 Stmt *TryBlock,
915 Stmt *Handler)
916 : Stmt(SEHTryStmtClass),
917 IsCXXTry(IsCXXTry),
918 TryLoc(TryLoc)
919{
920 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000921 Children[HANDLER] = Handler;
922}
923
Warren Huntf6be4cb2014-07-25 20:52:51 +0000924SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +0000925 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +0000926 Stmt *Handler) {
927 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +0000928}
929
930SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
931 return dyn_cast<SEHExceptStmt>(getHandler());
932}
933
934SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
935 return dyn_cast<SEHFinallyStmt>(getHandler());
936}
937
938SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
939 Expr *FilterExpr,
940 Stmt *Block)
941 : Stmt(SEHExceptStmtClass),
942 Loc(Loc)
943{
Pavel Labath515f4db2013-09-03 14:41:16 +0000944 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +0000945 Children[BLOCK] = Block;
946}
947
Craig Toppere6960e22013-08-22 05:28:54 +0000948SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
949 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000950 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
951}
952
953SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
954 Stmt *Block)
955 : Stmt(SEHFinallyStmtClass),
956 Loc(Loc),
957 Block(Block)
958{}
959
Craig Toppere6960e22013-08-22 05:28:54 +0000960SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +0000961 Stmt *Block) {
962 return new(C)SEHFinallyStmt(Loc,Block);
963}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000964
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000965CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
966 VarDecl *Var)
967 : VarAndKind(Var, Kind), Loc(Loc) {
968 switch (Kind) {
969 case VCK_This:
970 assert(!Var && "'this' capture cannot have a variable!");
971 break;
972 case VCK_ByRef:
973 assert(Var && "capturing by reference must have a variable!");
974 break;
975 case VCK_ByCopy:
976 assert(Var && "capturing by copy must have a variable!");
977 assert(
978 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
979 Var->getType()
980 ->castAs<ReferenceType>()
981 ->getPointeeType()
982 ->isScalarType())) &&
983 "captures by copy are expected to have a scalar type!");
984 break;
985 case VCK_VLAType:
986 assert(!Var &&
987 "Variable-length array type capture cannot have a variable!");
988 break;
989 }
990}
991
Chandler Carruth21c90602015-12-30 03:24:14 +0000992CapturedStmt::VariableCaptureKind
993CapturedStmt::Capture::getCaptureKind() const {
994 return VarAndKind.getInt();
995}
996
997VarDecl *CapturedStmt::Capture::getCapturedVar() const {
998 assert((capturesVariable() || capturesVariableByCopy()) &&
999 "No variable available for 'this' or VAT capture");
1000 return VarAndKind.getPointer();
1001}
1002
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001003CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1004 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1005
1006 // Offset of the first Capture object.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001007 unsigned FirstCaptureOffset = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001008
1009 return reinterpret_cast<Capture *>(
1010 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1011 + FirstCaptureOffset);
1012}
1013
Wei Pan17fbf6e2013-05-04 03:59:06 +00001014CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1015 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001016 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001017 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001018 RecordDecl *RD)
1019 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001020 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001021 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001022 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001023 assert(RD && "null record declaration for captured statement");
1024
1025 // Copy initialization expressions.
1026 Stmt **Stored = getStoredStmts();
1027 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1028 *Stored++ = CaptureInits[I];
1029
1030 // Copy the statement being captured.
1031 *Stored = S;
1032
1033 // Copy all Capture objects.
1034 Capture *Buffer = getStoredCaptures();
1035 std::copy(Captures.begin(), Captures.end(), Buffer);
1036}
1037
1038CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1039 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Craig Topper36250ad2014-05-12 05:36:57 +00001040 CapDeclAndKind(nullptr, CR_Default), TheRecordDecl(nullptr) {
1041 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001042}
1043
Craig Toppere6960e22013-08-22 05:28:54 +00001044CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001045 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001046 ArrayRef<Capture> Captures,
1047 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001048 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001049 RecordDecl *RD) {
1050 // The layout is
1051 //
1052 // -----------------------------------------------------------
1053 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1054 // ----------------^-------------------^----------------------
1055 // getStoredStmts() getStoredCaptures()
1056 //
1057 // where S is the statement being captured.
1058 //
1059 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1060
1061 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1062 if (!Captures.empty()) {
1063 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001064 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001065 Size += sizeof(Capture) * Captures.size();
1066 }
1067
1068 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001069 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001070}
1071
Craig Toppere6960e22013-08-22 05:28:54 +00001072CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001073 unsigned NumCaptures) {
1074 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1075 if (NumCaptures > 0) {
1076 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001077 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001078 Size += sizeof(Capture) * NumCaptures;
1079 }
1080
1081 void *Mem = Context.Allocate(Size);
1082 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1083}
1084
1085Stmt::child_range CapturedStmt::children() {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001086 // Children are captured field initilizers.
1087 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001088}
1089
Chandler Carruth21c90602015-12-30 03:24:14 +00001090CapturedDecl *CapturedStmt::getCapturedDecl() {
1091 return CapDeclAndKind.getPointer();
1092}
1093const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1094 return CapDeclAndKind.getPointer();
1095}
1096
1097/// \brief Set the outlined function declaration.
1098void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1099 assert(D && "null CapturedDecl");
1100 CapDeclAndKind.setPointer(D);
1101}
1102
1103/// \brief Retrieve the captured region kind.
1104CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1105 return CapDeclAndKind.getInt();
1106}
1107
1108/// \brief Set the captured region kind.
1109void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1110 CapDeclAndKind.setInt(Kind);
1111}
1112
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001113bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001114 for (const auto &I : captures()) {
1115 if (!I.capturesVariable())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001116 continue;
1117
1118 // This does not handle variable redeclarations. This should be
1119 // extended to capture variables with redeclarations, for example
1120 // a thread-private variable in OpenMP.
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001121 if (I.getCapturedVar() == Var)
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001122 return true;
1123 }
1124
1125 return false;
1126}