blob: 773b4da940b860e98354899466e51e136fcc3640 [file] [log] [blame]
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001//===- Stmt.cpp - Statement AST Node Implementation -----------------------===//
Chris Lattnerf42cce72006-10-25 04:09:21 +00002//
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"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclGroup.h"
Chris Lattner29375652006-12-04 18:06:35 +000018#include "clang/AST/ExprCXX.h"
Steve Naroff021ca182008-05-29 21:12:08 +000019#include "clang/AST/ExprObjC.h"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000020#include "clang/AST/ExprOpenMP.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000021#include "clang/AST/Stmt.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtObjC.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000024#include "clang/AST/StmtOpenMP.h"
Sebastian Redl54c04d42008-12-22 19:15:10 +000025#include "clang/AST/Type.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000026#include "clang/Basic/CharInfo.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000027#include "clang/Basic/LLVM.h"
28#include "clang/Basic/SourceLocation.h"
Chris Lattner1a8f3942010-04-23 16:29:58 +000029#include "clang/Basic/TargetInfo.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000030#include "clang/Lex/Token.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000031#include "llvm/ADT/SmallVector.h"
Chad Rosier14836ba2012-08-24 17:05:45 +000032#include "llvm/ADT/StringExtras.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000033#include "llvm/ADT/StringRef.h"
34#include "llvm/Support/Casting.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/MathExtras.h"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000038#include "llvm/Support/raw_ostream.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000039#include <algorithm>
40#include <cassert>
41#include <cstring>
42#include <string>
43#include <utility>
44
Chris Lattnerf42cce72006-10-25 04:09:21 +000045using namespace clang;
46
Steve Narofff84d11f2007-05-23 21:48:04 +000047static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000048 const char *Name;
49 unsigned Counter;
50 unsigned Size;
Alexis Hunt656bb312010-05-05 15:24:00 +000051} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner4d15a0d2007-08-25 01:42:24 +000052
53static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
54 static bool Initialized = false;
55 if (Initialized)
56 return StmtClassInfo[E];
57
58 // Intialize the table on the first use.
59 Initialized = true;
Alexis Huntabb2ac82010-05-18 06:22:21 +000060#define ABSTRACT_STMT(STMT)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000061#define STMT(CLASS, PARENT) \
62 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
63 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Alexis Hunt656bb312010-05-05 15:24:00 +000064#include "clang/AST/StmtNodes.inc"
Nico Weberde565e32008-08-05 23:15:29 +000065
Chris Lattner4d15a0d2007-08-25 01:42:24 +000066 return StmtClassInfo[E];
67}
68
Craig Topper37932912013-08-18 10:09:15 +000069void *Stmt::operator new(size_t bytes, const ASTContext& C,
Craig Topper5a050012013-08-18 17:45:38 +000070 unsigned alignment) {
Benjamin Kramerea70eb32012-12-01 15:09:41 +000071 return ::operator new(bytes, C, alignment);
72}
73
Steve Narofff1e53692007-03-23 22:27:02 +000074const char *Stmt::getStmtClassName() const {
John McCall925b16622010-10-26 08:39:16 +000075 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000076}
Steve Narofff84d11f2007-05-23 21:48:04 +000077
78void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000079 // Ensure the table is primed.
80 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000081
Steve Narofff84d11f2007-05-23 21:48:04 +000082 unsigned sum = 0;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000083 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Alexis Hunt656bb312010-05-05 15:24:00 +000084 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000085 if (StmtClassInfo[i].Name == nullptr) continue;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000086 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000087 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000088 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000089 sum = 0;
Alexis Hunt656bb312010-05-05 15:24:00 +000090 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000091 if (StmtClassInfo[i].Name == nullptr) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +000092 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000093 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
94 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
95 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
96 << " bytes)\n";
Chris Lattner4d15a0d2007-08-25 01:42:24 +000097 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +000098 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000099
100 llvm::errs() << "Total bytes = " << sum << "\n";
Steve Narofff84d11f2007-05-23 21:48:04 +0000101}
102
103void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +0000104 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +0000105}
106
Daniel Dunbar62905572012-03-05 21:42:49 +0000107bool Stmt::StatisticsEnabled = false;
108void Stmt::EnableStatistics() {
109 StatisticsEnabled = true;
Steve Narofff84d11f2007-05-23 21:48:04 +0000110}
111
John McCall4db5c3c2011-07-07 06:58:02 +0000112Stmt *Stmt::IgnoreImplicit() {
113 Stmt *s = this;
114
Richard Smith520449d2015-02-05 06:15:50 +0000115 if (auto *ewc = dyn_cast<ExprWithCleanups>(s))
John McCall4db5c3c2011-07-07 06:58:02 +0000116 s = ewc->getSubExpr();
117
Richard Smith520449d2015-02-05 06:15:50 +0000118 if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s))
119 s = mte->GetTemporaryExpr();
120
121 if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s))
122 s = bte->getSubExpr();
123
124 while (auto *ice = dyn_cast<ImplicitCastExpr>(s))
John McCall4db5c3c2011-07-07 06:58:02 +0000125 s = ice->getSubExpr();
126
127 return s;
128}
129
Alexander Musmana5f070a2014-10-01 06:03:56 +0000130/// \brief Skip no-op (attributed, compound) container stmts and skip captured
131/// stmt at the top, if \a IgnoreCaptured is true.
132Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
133 Stmt *S = this;
134 if (IgnoreCaptured)
135 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
136 S = CapS->getCapturedStmt();
137 while (true) {
138 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
139 S = AS->getSubStmt();
140 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
141 if (CS->size() != 1)
142 break;
143 S = CS->body_back();
144 } else
145 break;
146 }
147 return S;
148}
149
Chandler Carrutha626d642011-09-10 00:02:34 +0000150/// \brief Strip off all label-like statements.
151///
Richard Smithc202b282012-04-14 00:33:13 +0000152/// This will strip off label statements, case statements, attributed
153/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000154const Stmt *Stmt::stripLabelLikeStatements() const {
155 const Stmt *S = this;
156 while (true) {
157 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
158 S = LS->getSubStmt();
159 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
160 S = SC->getSubStmt();
Richard Smithc202b282012-04-14 00:33:13 +0000161 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
162 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000163 else
164 return S;
165 }
166}
167
John McCallbd066782011-02-09 08:16:59 +0000168namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000169
John McCallbd066782011-02-09 08:16:59 +0000170 struct good {};
171 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000172
173 // These silly little functions have to be static inline to suppress
174 // unused warnings, and they have to be defined to suppress other
175 // warnings.
Nick Lewyckybae992f2011-02-09 08:42:57 +0000176 static inline good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000177
178 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000179 template <class T> good implements_children(children_t T::*) {
180 return good();
181 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000182 LLVM_ATTRIBUTE_UNUSED
Nick Lewyckybae992f2011-02-09 08:42:57 +0000183 static inline bad implements_children(children_t Stmt::*) {
184 return bad();
185 }
John McCallbd066782011-02-09 08:16:59 +0000186
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000187 typedef SourceLocation getLocStart_t() const;
188 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000189 return good();
190 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000191 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000192 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
193 return bad();
194 }
195
196 typedef SourceLocation getLocEnd_t() const;
197 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
198 return good();
199 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000200 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000201 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000202 return bad();
203 }
John McCallbd066782011-02-09 08:16:59 +0000204
205#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000206 (void) is_good(implements_children(&type::children))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000207#define ASSERT_IMPLEMENTS_getLocStart(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000208 (void) is_good(implements_getLocStart(&type::getLocStart))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000209#define ASSERT_IMPLEMENTS_getLocEnd(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000210 (void) is_good(implements_getLocEnd(&type::getLocEnd))
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000211
212} // namespace
John McCallbd066782011-02-09 08:16:59 +0000213
214/// Check whether the various Stmt classes implement their member
215/// functions.
Eli Friedmandc41d792013-09-10 22:57:15 +0000216LLVM_ATTRIBUTE_UNUSED
John McCallbd066782011-02-09 08:16:59 +0000217static inline void check_implementations() {
218#define ABSTRACT_STMT(type)
219#define STMT(type, base) \
220 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000221 ASSERT_IMPLEMENTS_getLocStart(type); \
222 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCallbd066782011-02-09 08:16:59 +0000223#include "clang/AST/StmtNodes.inc"
224}
225
226Stmt::child_range Stmt::children() {
227 switch (getStmtClass()) {
228 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
229#define ABSTRACT_STMT(type)
230#define STMT(type, base) \
231 case Stmt::type##Class: \
232 return static_cast<type*>(this)->children();
233#include "clang/AST/StmtNodes.inc"
234 }
235 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000236}
237
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000238// Amusing macro metaprogramming hack: check whether a class provides
239// a more specific implementation of getSourceRange.
240//
241// See also Expr.cpp:getExprLoc().
242namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000243
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000244 /// This implementation is used when a class provides a custom
245 /// implementation of getSourceRange.
246 template <class S, class T>
247 SourceRange getSourceRangeImpl(const Stmt *stmt,
248 SourceRange (T::*v)() const) {
249 return static_cast<const S*>(stmt)->getSourceRange();
250 }
251
252 /// This implementation is used when a class doesn't provide a custom
253 /// implementation of getSourceRange. Overload resolution should pick it over
254 /// the implementation above because it's more specialized according to
255 /// function template partial ordering.
256 template <class S>
257 SourceRange getSourceRangeImpl(const Stmt *stmt,
258 SourceRange (Stmt::*v)() const) {
259 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
260 static_cast<const S*>(stmt)->getLocEnd());
261 }
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000262
263} // namespace
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000264
John McCallbd066782011-02-09 08:16:59 +0000265SourceRange Stmt::getSourceRange() const {
266 switch (getStmtClass()) {
267 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
268#define ABSTRACT_STMT(type)
269#define STMT(type, base) \
270 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000271 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000272#include "clang/AST/StmtNodes.inc"
273 }
274 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000275}
276
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000277SourceLocation Stmt::getLocStart() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000278// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000279 switch (getStmtClass()) {
280 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
281#define ABSTRACT_STMT(type)
282#define STMT(type, base) \
283 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000284 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000285#include "clang/AST/StmtNodes.inc"
286 }
287 llvm_unreachable("unknown statement kind");
288}
289
290SourceLocation Stmt::getLocEnd() const {
291 switch (getStmtClass()) {
292 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
293#define ABSTRACT_STMT(type)
294#define STMT(type, base) \
295 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000296 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000297#include "clang/AST/StmtNodes.inc"
298 }
299 llvm_unreachable("unknown statement kind");
300}
301
Craig Toppere6960e22013-08-22 05:28:54 +0000302CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000303 SourceLocation LB, SourceLocation RB)
Aaron Ballmance6c67e2014-10-22 21:06:18 +0000304 : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000305 CompoundStmtBits.NumStmts = Stmts.size();
306 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000307 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
308
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000309 if (Stmts.empty()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000310 Body = nullptr;
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000311 return;
312 }
313
Nico Webera2a0eb92012-12-29 20:03:39 +0000314 Body = new (C) Stmt*[Stmts.size()];
315 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000316}
317
Craig Topper9ee84ad2015-12-04 05:01:44 +0000318void CompoundStmt::setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts) {
319 if (Body)
Douglas Gregora9af1d12009-04-17 00:04:06 +0000320 C.Deallocate(Body);
Craig Topper9ee84ad2015-12-04 05:01:44 +0000321 CompoundStmtBits.NumStmts = Stmts.size();
322 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
323 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
Douglas Gregora9af1d12009-04-17 00:04:06 +0000324
Craig Topper9ee84ad2015-12-04 05:01:44 +0000325 Body = new (C) Stmt*[Stmts.size()];
326 std::copy(Stmts.begin(), Stmts.end(), Body);
Douglas Gregora9af1d12009-04-17 00:04:06 +0000327}
Steve Narofff84d11f2007-05-23 21:48:04 +0000328
Chris Lattnereefa10e2007-05-28 06:56:27 +0000329const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000330 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000331}
332
Craig Toppere6960e22013-08-22 05:28:54 +0000333AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000334 ArrayRef<const Attr*> Attrs,
335 Stmt *SubStmt) {
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000336 assert(!Attrs.empty() && "Attrs should not be empty");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000337 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(Attrs.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000338 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000339 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
340}
341
Craig Toppere6960e22013-08-22 05:28:54 +0000342AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
343 unsigned NumAttrs) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000344 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000345 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(NumAttrs),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000346 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000347 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
348}
349
Craig Topperc571c812013-08-22 06:02:26 +0000350std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000351 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
352 return gccAsmStmt->generateAsmString(C);
353 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
354 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000355 llvm_unreachable("unknown asm statement kind!");
356}
357
358StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000359 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
360 return gccAsmStmt->getOutputConstraint(i);
361 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
362 return msAsmStmt->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000363 llvm_unreachable("unknown asm statement kind!");
364}
365
366const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000367 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
368 return gccAsmStmt->getOutputExpr(i);
369 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
370 return msAsmStmt->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000371 llvm_unreachable("unknown asm statement kind!");
372}
373
374StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000375 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
376 return gccAsmStmt->getInputConstraint(i);
377 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
378 return msAsmStmt->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000379 llvm_unreachable("unknown asm statement kind!");
380}
381
382const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000383 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
384 return gccAsmStmt->getInputExpr(i);
385 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
386 return msAsmStmt->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000387 llvm_unreachable("unknown asm statement kind!");
388}
389
390StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000391 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
392 return gccAsmStmt->getClobber(i);
393 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
394 return msAsmStmt->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000395 llvm_unreachable("unknown asm statement kind!");
396}
397
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000398/// getNumPlusOperands - Return the number of output operands that have a "+"
399/// constraint.
400unsigned AsmStmt::getNumPlusOperands() const {
401 unsigned Res = 0;
402 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
403 if (isOutputPlusConstraint(i))
404 ++Res;
405 return Res;
406}
407
Akira Hatanaka987f1862014-08-22 06:05:21 +0000408char GCCAsmStmt::AsmStringPiece::getModifier() const {
409 assert(isOperand() && "Only Operands can have modifiers.");
410 return isLetter(Str[0]) ? Str[0] : '\0';
411}
412
Chad Rosier6100ae12012-08-27 23:47:56 +0000413StringRef GCCAsmStmt::getClobber(unsigned i) const {
414 return getClobberStringLiteral(i)->getString();
415}
416
Chad Rosierde70e0e2012-08-25 00:11:56 +0000417Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000418 return cast<Expr>(Exprs[i]);
419}
Chris Lattner72bbf172009-03-10 04:59:06 +0000420
421/// getOutputConstraint - Return the constraint string for the specified
422/// output operand. All output constraints are known to be non-empty (either
423/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000424StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000425 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000426}
Chris Lattner72bbf172009-03-10 04:59:06 +0000427
Chad Rosierde70e0e2012-08-25 00:11:56 +0000428Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000429 return cast<Expr>(Exprs[i + NumOutputs]);
430}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000431
Chad Rosierde70e0e2012-08-25 00:11:56 +0000432void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000433 Exprs[i + NumOutputs] = E;
434}
435
Chris Lattner72bbf172009-03-10 04:59:06 +0000436/// getInputConstraint - Return the specified input constraint. Unlike output
437/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000438StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000439 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000440}
441
Craig Topperc571c812013-08-22 06:02:26 +0000442void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
443 IdentifierInfo **Names,
444 StringLiteral **Constraints,
445 Stmt **Exprs,
446 unsigned NumOutputs,
447 unsigned NumInputs,
448 StringLiteral **Clobbers,
449 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000450 this->NumOutputs = NumOutputs;
451 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000452 this->NumClobbers = NumClobbers;
453
454 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000455
Anders Carlsson98323d22010-01-30 23:19:41 +0000456 C.Deallocate(this->Names);
457 this->Names = new (C) IdentifierInfo*[NumExprs];
458 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000459
Anders Carlsson98323d22010-01-30 23:19:41 +0000460 C.Deallocate(this->Exprs);
461 this->Exprs = new (C) Stmt*[NumExprs];
462 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000463
Anders Carlsson98323d22010-01-30 23:19:41 +0000464 C.Deallocate(this->Constraints);
465 this->Constraints = new (C) StringLiteral*[NumExprs];
466 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000467
Anders Carlsson98323d22010-01-30 23:19:41 +0000468 C.Deallocate(this->Clobbers);
469 this->Clobbers = new (C) StringLiteral*[NumClobbers];
470 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000471}
472
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000473/// getNamedOperand - Given a symbolic operand reference like %[foo],
474/// translate this into a numeric value needed to reference the same operand.
475/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000476int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000477 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000479 // Check if this is an output operand.
480 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
481 if (getOutputName(i) == SymbolicName)
482 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000485 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
486 if (getInputName(i) == SymbolicName)
487 return getNumOutputs() + NumPlusOperands + i;
488
489 // Not found.
490 return -1;
491}
492
Chris Lattner35b58362009-03-10 23:21:44 +0000493/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
494/// it into pieces. If the asm string is erroneous, emit errors and return
495/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000496unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000497 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000498 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000499 const char *StrStart = Str.begin();
500 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000501 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000502
Chris Lattner35b58362009-03-10 23:21:44 +0000503 // "Simple" inline asms have no constraints or operands, just convert the asm
504 // string to escape $'s.
505 if (isSimple()) {
506 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000507 for (; CurPtr != StrEnd; ++CurPtr) {
508 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000509 case '$':
510 Result += "$$";
511 break;
512 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000513 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000514 break;
515 }
516 }
517 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000518 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000519 }
520
521 // CurStringPiece - The current string that we are building up as we scan the
522 // asm string.
523 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000524
Douglas Gregore8bbc122011-09-02 00:18:52 +0000525 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000526
Richard Smithd18ab802016-05-16 22:52:23 +0000527 unsigned LastAsmStringToken = 0;
528 unsigned LastAsmStringOffset = 0;
529
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000530 while (true) {
Chris Lattner35b58362009-03-10 23:21:44 +0000531 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000532 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000533 if (!CurStringPiece.empty())
534 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000535 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Chris Lattnera41b8472009-03-10 23:51:40 +0000538 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000539 switch (CurChar) {
540 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000541 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
542 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
543 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000544 case '%':
545 break;
546 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000547 CurStringPiece += CurChar;
548 continue;
549 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000550
Chris Lattner35b58362009-03-10 23:21:44 +0000551 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000552 if (CurPtr == StrEnd) {
553 // % at end of string is invalid (no escape).
554 DiagOffs = CurPtr-StrStart-1;
555 return diag::err_asm_invalid_escape;
556 }
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000557 // Handle escaped char and continue looping over the asm string.
Chris Lattnera41b8472009-03-10 23:51:40 +0000558 char EscapedChar = *CurPtr++;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000559 switch (EscapedChar) {
560 default:
561 break;
562 case '%': // %% -> %
563 case '{': // %{ -> {
564 case '}': // %} -> }
565 CurStringPiece += EscapedChar;
Chris Lattner35b58362009-03-10 23:21:44 +0000566 continue;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000567 case '=': // %= -> Generate a unique ID.
Chris Lattner35b58362009-03-10 23:21:44 +0000568 CurStringPiece += "${:uid}";
569 continue;
570 }
Mike Stump11289f42009-09-09 15:08:12 +0000571
Chris Lattner35b58362009-03-10 23:21:44 +0000572 // Otherwise, we have an operand. If we have accumulated a string so far,
573 // add it to the Pieces list.
574 if (!CurStringPiece.empty()) {
575 Pieces.push_back(AsmStringPiece(CurStringPiece));
576 CurStringPiece.clear();
577 }
Mike Stump11289f42009-09-09 15:08:12 +0000578
Akira Hatanaka987f1862014-08-22 06:05:21 +0000579 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
580 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
581
582 const char *Begin = CurPtr - 1; // Points to the character following '%'.
583 const char *Percent = Begin - 1; // Points to '%'.
584
Jordan Rosea7d03842013-02-08 22:30:41 +0000585 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000586 if (CurPtr == StrEnd) { // Premature end.
587 DiagOffs = CurPtr-StrStart-1;
588 return diag::err_asm_invalid_escape;
589 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000590 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Akira Hatanaka987f1862014-08-22 06:05:21 +0000593 const TargetInfo &TI = C.getTargetInfo();
594 const SourceManager &SM = C.getSourceManager();
595 const LangOptions &LO = C.getLangOpts();
596
597 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000598 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000599 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000600 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000601
Chris Lattner99d892b2009-03-11 22:52:17 +0000602 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000603 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000604 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000605
Chris Lattner14311922009-03-11 00:23:13 +0000606 unsigned NumOperands =
607 getNumOutputs() + getNumPlusOperands() + getNumInputs();
608 if (N >= NumOperands) {
609 DiagOffs = CurPtr-StrStart-1;
610 return diag::err_asm_invalid_operand_number;
611 }
612
Akira Hatanaka987f1862014-08-22 06:05:21 +0000613 // Str contains "x4" (Operand without the leading %).
614 std::string Str(Begin, CurPtr - Begin);
615
616 // (BeginLoc, EndLoc) represents the range of the operand we are currently
617 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000618 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
619 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
620 &LastAsmStringOffset);
621 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
622 CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken,
623 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000624
Benjamin Kramer3204b152015-05-29 19:42:19 +0000625 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Chris Lattner35b58362009-03-10 23:21:44 +0000626 continue;
627 }
Mike Stump11289f42009-09-09 15:08:12 +0000628
Akira Hatanaka987f1862014-08-22 06:05:21 +0000629 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000630 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000631 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000632
Chris Lattner3fa25c62009-03-11 00:06:36 +0000633 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000634 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000635 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000636 return diag::err_asm_unterminated_symbolic_operand_name;
637 if (NameEnd == CurPtr)
638 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000639
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000640 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattner35b58362009-03-10 23:21:44 +0000642 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000643 if (N == -1) {
644 // Verify that an operand with that name exists.
645 DiagOffs = CurPtr-StrStart;
646 return diag::err_asm_unknown_symbolic_operand_name;
647 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000648
649 // Str contains "x[foo]" (Operand without the leading %).
650 std::string Str(Begin, NameEnd + 1 - Begin);
651
652 // (BeginLoc, EndLoc) represents the range of the operand we are currently
653 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000654 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
655 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
656 &LastAsmStringOffset);
657 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
658 NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken,
659 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000660
Benjamin Kramer3204b152015-05-29 19:42:19 +0000661 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000662
Chris Lattner3fa25c62009-03-11 00:06:36 +0000663 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000664 continue;
665 }
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000667 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000668 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000669 }
670}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000671
672/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000673std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000674 // Analyze the asm string to decompose it into its pieces. We know that Sema
675 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000676 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000677 unsigned DiagOffs;
678 AnalyzeAsmString(Pieces, C, DiagOffs);
679
680 std::string AsmString;
681 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
682 if (Pieces[i].isString())
683 AsmString += Pieces[i].getString();
684 else if (Pieces[i].getModifier() == '\0')
685 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
686 else
687 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
688 Pieces[i].getModifier() + '}';
689 }
690 return AsmString;
691}
Chris Lattner35b58362009-03-10 23:21:44 +0000692
Chad Rosier3b0c2602012-08-27 20:23:31 +0000693/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000694std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000695 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000696 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000697}
698
Chad Rosierfe31e622012-08-24 00:07:09 +0000699Expr *MSAsmStmt::getOutputExpr(unsigned i) {
700 return cast<Expr>(Exprs[i]);
701}
702
703Expr *MSAsmStmt::getInputExpr(unsigned i) {
704 return cast<Expr>(Exprs[i + NumOutputs]);
705}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000706
Chad Rosierfe31e622012-08-24 00:07:09 +0000707void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
708 Exprs[i + NumOutputs] = E;
709}
710
Chris Lattner86f5e132008-01-30 05:01:46 +0000711//===----------------------------------------------------------------------===//
712// Constructors
713//===----------------------------------------------------------------------===//
714
Craig Toppere6960e22013-08-22 05:28:54 +0000715GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
716 bool issimple, bool isvolatile, unsigned numoutputs,
717 unsigned numinputs, IdentifierInfo **names,
718 StringLiteral **constraints, Expr **exprs,
719 StringLiteral *asmstr, unsigned numclobbers,
720 StringLiteral **clobbers, SourceLocation rparenloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000721 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
722 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Chad Rosier42032fa2012-08-07 23:12:23 +0000723 unsigned NumExprs = NumOutputs + NumInputs;
724
Anders Carlsson98323d22010-01-30 23:19:41 +0000725 Names = new (C) IdentifierInfo*[NumExprs];
726 std::copy(names, names + NumExprs, Names);
727
728 Exprs = new (C) Stmt*[NumExprs];
729 std::copy(exprs, exprs + NumExprs, Exprs);
730
731 Constraints = new (C) StringLiteral*[NumExprs];
732 std::copy(constraints, constraints + NumExprs, Constraints);
733
734 Clobbers = new (C) StringLiteral*[NumClobbers];
735 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000736}
737
Craig Toppere6960e22013-08-22 05:28:54 +0000738MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000739 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000740 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000741 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000742 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
743 StringRef asmstr, ArrayRef<StringRef> clobbers,
744 SourceLocation endloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000745 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
746 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
747 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
John McCallf413f5e2013-05-03 00:10:13 +0000748 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
749}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000750
Craig Toppere6960e22013-08-22 05:28:54 +0000751static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
Benjamin Kramer2ab0d882015-08-04 12:34:23 +0000752 return str.copy(C);
John McCallf413f5e2013-05-03 00:10:13 +0000753}
754
Craig Toppere6960e22013-08-22 05:28:54 +0000755void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000756 ArrayRef<Token> asmtoks,
757 ArrayRef<StringRef> constraints,
758 ArrayRef<Expr*> exprs,
759 ArrayRef<StringRef> clobbers) {
760 assert(NumAsmToks == asmtoks.size());
761 assert(NumClobbers == clobbers.size());
762
Craig Toppercaf138e2015-12-05 07:41:42 +0000763 assert(exprs.size() == NumOutputs + NumInputs);
764 assert(exprs.size() == constraints.size());
John McCallf413f5e2013-05-03 00:10:13 +0000765
766 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000767
Craig Toppercaf138e2015-12-05 07:41:42 +0000768 Exprs = new (C) Stmt*[exprs.size()];
769 std::copy(exprs.begin(), exprs.end(), Exprs);
Chad Rosierfe31e622012-08-24 00:07:09 +0000770
Craig Toppercaf138e2015-12-05 07:41:42 +0000771 AsmToks = new (C) Token[asmtoks.size()];
772 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000773
Craig Toppercaf138e2015-12-05 07:41:42 +0000774 Constraints = new (C) StringRef[exprs.size()];
775 std::transform(constraints.begin(), constraints.end(), Constraints,
776 [&](StringRef Constraint) {
777 return copyIntoContext(C, Constraint);
778 });
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000779
Chad Rosierbaf53f92012-08-10 21:36:25 +0000780 Clobbers = new (C) StringRef[NumClobbers];
Craig Toppercaf138e2015-12-05 07:41:42 +0000781 // FIXME: Avoid the allocation/copy if at all possible.
782 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
783 [&](StringRef Clobber) {
784 return copyIntoContext(C, Clobber);
785 });
Chad Rosier32503022012-06-11 20:47:18 +0000786}
787
Richard Smithb130fe72016-06-23 19:16:49 +0000788IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, bool IsConstexpr,
Richard Smitha547eb22016-07-14 00:11:03 +0000789 Stmt *init, VarDecl *var, Expr *cond, Stmt *then,
790 SourceLocation EL, Stmt *elsev)
Richard Smithb130fe72016-06-23 19:16:49 +0000791 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL) {
792 setConstexpr(IsConstexpr);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000793 setConditionVariable(C, var);
Richard Smitha547eb22016-07-14 00:11:03 +0000794 SubExprs[INIT] = init;
Pavel Labath515f4db2013-09-03 14:41:16 +0000795 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000796 SubExprs[THEN] = then;
Chad Rosier42032fa2012-08-07 23:12:23 +0000797 SubExprs[ELSE] = elsev;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000798}
799
800VarDecl *IfStmt::getConditionVariable() const {
801 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000802 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000803
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000804 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
805 return cast<VarDecl>(DS->getSingleDecl());
806}
807
Craig Toppere6960e22013-08-22 05:28:54 +0000808void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000809 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000810 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000811 return;
812 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000813
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000814 SourceRange VarRange = V->getSourceRange();
815 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
816 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000817}
818
Erik Pilkington5cd57172016-08-16 17:44:11 +0000819bool IfStmt::isObjCAvailabilityCheck() const {
820 return isa<ObjCAvailabilityCheckExpr>(SubExprs[COND]);
821}
822
Craig Toppere6960e22013-08-22 05:28:54 +0000823ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000824 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000825 SourceLocation RP)
Chad Rosier42032fa2012-08-07 23:12:23 +0000826 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000827{
828 SubExprs[INIT] = Init;
829 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000830 SubExprs[COND] = Cond;
831 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000832 SubExprs[BODY] = Body;
833}
834
835VarDecl *ForStmt::getConditionVariable() const {
836 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000837 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000838
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000839 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
840 return cast<VarDecl>(DS->getSingleDecl());
841}
842
Craig Toppere6960e22013-08-22 05:28:54 +0000843void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000844 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000845 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000846 return;
847 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000848
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000849 SourceRange VarRange = V->getSourceRange();
850 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
851 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000852}
853
Richard Smitha547eb22016-07-14 00:11:03 +0000854SwitchStmt::SwitchStmt(const ASTContext &C, Stmt *init, VarDecl *Var,
855 Expr *cond)
Benjamin Kramer475386d2015-04-02 15:29:07 +0000856 : Stmt(SwitchStmtClass), FirstCase(nullptr, false) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000857 setConditionVariable(C, Var);
Richard Smitha547eb22016-07-14 00:11:03 +0000858 SubExprs[INIT] = init;
Pavel Labath515f4db2013-09-03 14:41:16 +0000859 SubExprs[COND] = cond;
Craig Topper36250ad2014-05-12 05:36:57 +0000860 SubExprs[BODY] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000861}
862
863VarDecl *SwitchStmt::getConditionVariable() const {
864 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000865 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000866
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000867 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
868 return cast<VarDecl>(DS->getSingleDecl());
869}
870
Craig Toppere6960e22013-08-22 05:28:54 +0000871void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000872 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000873 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000874 return;
875 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000876
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000877 SourceRange VarRange = V->getSourceRange();
878 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
879 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000880}
881
John McCallbd066782011-02-09 08:16:59 +0000882Stmt *SwitchCase::getSubStmt() {
Chris Lattner80d51eb2011-02-28 00:18:06 +0000883 if (isa<CaseStmt>(this))
884 return cast<CaseStmt>(this)->getSubStmt();
John McCallbd066782011-02-09 08:16:59 +0000885 return cast<DefaultStmt>(this)->getSubStmt();
886}
887
Craig Toppere6960e22013-08-22 05:28:54 +0000888WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000889 SourceLocation WL)
Chris Lattner80d51eb2011-02-28 00:18:06 +0000890 : Stmt(WhileStmtClass) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000891 setConditionVariable(C, Var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000892 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000893 SubExprs[BODY] = body;
894 WhileLoc = WL;
895}
896
897VarDecl *WhileStmt::getConditionVariable() const {
898 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000899 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000900
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000901 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
902 return cast<VarDecl>(DS->getSingleDecl());
903}
904
Craig Toppere6960e22013-08-22 05:28:54 +0000905void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000906 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000907 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000908 return;
909 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000910
911 SourceRange VarRange = V->getSourceRange();
912 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
913 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000914}
915
Ted Kremenek066dd932007-08-24 21:09:09 +0000916// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000917LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall9de91602010-10-28 08:53:48 +0000918 if (AddrLabelExpr *E =
919 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
920 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +0000921 return nullptr;
John McCall9de91602010-10-28 08:53:48 +0000922}
Ted Kremenek066dd932007-08-24 21:09:09 +0000923
Ted Kremenek066dd932007-08-24 21:09:09 +0000924// ReturnStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000925const Expr* ReturnStmt::getRetValue() const {
926 return cast_or_null<Expr>(RetExpr);
927}
928Expr* ReturnStmt::getRetValue() {
929 return cast_or_null<Expr>(RetExpr);
Ted Kremenek066dd932007-08-24 21:09:09 +0000930}
John Wiegley1c0675e2011-04-28 01:08:34 +0000931
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000932SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +0000933 Stmt *Handler)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000934 : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc) {
Warren Huntf6be4cb2014-07-25 20:52:51 +0000935 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000936 Children[HANDLER] = Handler;
937}
938
Warren Huntf6be4cb2014-07-25 20:52:51 +0000939SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +0000940 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +0000941 Stmt *Handler) {
942 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +0000943}
944
945SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
946 return dyn_cast<SEHExceptStmt>(getHandler());
947}
948
949SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
950 return dyn_cast<SEHFinallyStmt>(getHandler());
951}
952
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000953SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block)
954 : Stmt(SEHExceptStmtClass), Loc(Loc) {
Pavel Labath515f4db2013-09-03 14:41:16 +0000955 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +0000956 Children[BLOCK] = Block;
957}
958
Craig Toppere6960e22013-08-22 05:28:54 +0000959SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
960 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000961 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
962}
963
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000964SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, Stmt *Block)
965 : Stmt(SEHFinallyStmtClass), Loc(Loc), Block(Block) {}
John Wiegley1c0675e2011-04-28 01:08:34 +0000966
Craig Toppere6960e22013-08-22 05:28:54 +0000967SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +0000968 Stmt *Block) {
969 return new(C)SEHFinallyStmt(Loc,Block);
970}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000971
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000972CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
973 VarDecl *Var)
974 : VarAndKind(Var, Kind), Loc(Loc) {
975 switch (Kind) {
976 case VCK_This:
977 assert(!Var && "'this' capture cannot have a variable!");
978 break;
979 case VCK_ByRef:
980 assert(Var && "capturing by reference must have a variable!");
981 break;
982 case VCK_ByCopy:
983 assert(Var && "capturing by copy must have a variable!");
984 assert(
985 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
986 Var->getType()
987 ->castAs<ReferenceType>()
988 ->getPointeeType()
989 ->isScalarType())) &&
990 "captures by copy are expected to have a scalar type!");
991 break;
992 case VCK_VLAType:
993 assert(!Var &&
994 "Variable-length array type capture cannot have a variable!");
995 break;
996 }
997}
998
Chandler Carruth21c90602015-12-30 03:24:14 +0000999CapturedStmt::VariableCaptureKind
1000CapturedStmt::Capture::getCaptureKind() const {
1001 return VarAndKind.getInt();
1002}
1003
1004VarDecl *CapturedStmt::Capture::getCapturedVar() const {
1005 assert((capturesVariable() || capturesVariableByCopy()) &&
1006 "No variable available for 'this' or VAT capture");
1007 return VarAndKind.getPointer();
1008}
1009
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001010CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1011 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1012
1013 // Offset of the first Capture object.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001014 unsigned FirstCaptureOffset = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001015
1016 return reinterpret_cast<Capture *>(
1017 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1018 + FirstCaptureOffset);
1019}
1020
Wei Pan17fbf6e2013-05-04 03:59:06 +00001021CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1022 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001023 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001024 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001025 RecordDecl *RD)
1026 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001027 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001028 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001029 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001030 assert(RD && "null record declaration for captured statement");
1031
1032 // Copy initialization expressions.
1033 Stmt **Stored = getStoredStmts();
1034 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1035 *Stored++ = CaptureInits[I];
1036
1037 // Copy the statement being captured.
1038 *Stored = S;
1039
1040 // Copy all Capture objects.
1041 Capture *Buffer = getStoredCaptures();
1042 std::copy(Captures.begin(), Captures.end(), Buffer);
1043}
1044
1045CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1046 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001047 CapDeclAndKind(nullptr, CR_Default) {
Craig Topper36250ad2014-05-12 05:36:57 +00001048 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001049}
1050
Craig Toppere6960e22013-08-22 05:28:54 +00001051CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001052 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001053 ArrayRef<Capture> Captures,
1054 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001055 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001056 RecordDecl *RD) {
1057 // The layout is
1058 //
1059 // -----------------------------------------------------------
1060 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1061 // ----------------^-------------------^----------------------
1062 // getStoredStmts() getStoredCaptures()
1063 //
1064 // where S is the statement being captured.
1065 //
1066 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1067
1068 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1069 if (!Captures.empty()) {
1070 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001071 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001072 Size += sizeof(Capture) * Captures.size();
1073 }
1074
1075 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001076 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001077}
1078
Craig Toppere6960e22013-08-22 05:28:54 +00001079CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001080 unsigned NumCaptures) {
1081 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1082 if (NumCaptures > 0) {
1083 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001084 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001085 Size += sizeof(Capture) * NumCaptures;
1086 }
1087
1088 void *Mem = Context.Allocate(Size);
1089 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1090}
1091
1092Stmt::child_range CapturedStmt::children() {
Simon Pilgrim2c518802017-03-30 14:13:19 +00001093 // Children are captured field initializers.
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001094 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001095}
1096
Chandler Carruth21c90602015-12-30 03:24:14 +00001097CapturedDecl *CapturedStmt::getCapturedDecl() {
1098 return CapDeclAndKind.getPointer();
1099}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001100
Chandler Carruth21c90602015-12-30 03:24:14 +00001101const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1102 return CapDeclAndKind.getPointer();
1103}
1104
1105/// \brief Set the outlined function declaration.
1106void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1107 assert(D && "null CapturedDecl");
1108 CapDeclAndKind.setPointer(D);
1109}
1110
1111/// \brief Retrieve the captured region kind.
1112CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1113 return CapDeclAndKind.getInt();
1114}
1115
1116/// \brief Set the captured region kind.
1117void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1118 CapDeclAndKind.setInt(Kind);
1119}
1120
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001121bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001122 for (const auto &I : captures()) {
Alexey Bataev2c845412017-05-15 16:26:15 +00001123 if (!I.capturesVariable() && !I.capturesVariableByCopy())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001124 continue;
Alexey Bataeve85de8f2017-09-20 20:11:31 +00001125 if (I.getCapturedVar()->getCanonicalDecl() == Var->getCanonicalDecl())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001126 return true;
1127 }
1128
1129 return false;
1130}