blob: a041006c905e41110c64f1fecbad8b180b69733b [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
Eugene Zelenko7855e772018-04-03 00:11:50 +000014#include "clang/AST/Stmt.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/ASTDiagnostic.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000017#include "clang/AST/Decl.h"
18#include "clang/AST/DeclGroup.h"
Eugene Zelenko7855e772018-04-03 00:11:50 +000019#include "clang/AST/Expr.h"
Chris Lattner29375652006-12-04 18:06:35 +000020#include "clang/AST/ExprCXX.h"
Steve Naroff021ca182008-05-29 21:12:08 +000021#include "clang/AST/ExprObjC.h"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000022#include "clang/AST/ExprOpenMP.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000023#include "clang/AST/StmtCXX.h"
24#include "clang/AST/StmtObjC.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000025#include "clang/AST/StmtOpenMP.h"
Sebastian Redl54c04d42008-12-22 19:15:10 +000026#include "clang/AST/Type.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000027#include "clang/Basic/CharInfo.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000028#include "clang/Basic/LLVM.h"
29#include "clang/Basic/SourceLocation.h"
Chris Lattner1a8f3942010-04-23 16:29:58 +000030#include "clang/Basic/TargetInfo.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000031#include "clang/Lex/Token.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000032#include "llvm/ADT/SmallVector.h"
Chad Rosier14836ba2012-08-24 17:05:45 +000033#include "llvm/ADT/StringExtras.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000034#include "llvm/ADT/StringRef.h"
35#include "llvm/Support/Casting.h"
36#include "llvm/Support/Compiler.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/MathExtras.h"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000039#include "llvm/Support/raw_ostream.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000040#include <algorithm>
41#include <cassert>
42#include <cstring>
43#include <string>
44#include <utility>
45
Chris Lattnerf42cce72006-10-25 04:09:21 +000046using namespace clang;
47
Steve Narofff84d11f2007-05-23 21:48:04 +000048static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000049 const char *Name;
50 unsigned Counter;
51 unsigned Size;
Alexis Hunt656bb312010-05-05 15:24:00 +000052} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner4d15a0d2007-08-25 01:42:24 +000053
54static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
55 static bool Initialized = false;
56 if (Initialized)
57 return StmtClassInfo[E];
58
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +000059 // Initialize the table on the first use.
Chris Lattner4d15a0d2007-08-25 01:42:24 +000060 Initialized = true;
Alexis Huntabb2ac82010-05-18 06:22:21 +000061#define ABSTRACT_STMT(STMT)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000062#define STMT(CLASS, PARENT) \
63 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
64 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Alexis Hunt656bb312010-05-05 15:24:00 +000065#include "clang/AST/StmtNodes.inc"
Nico Weberde565e32008-08-05 23:15:29 +000066
Chris Lattner4d15a0d2007-08-25 01:42:24 +000067 return StmtClassInfo[E];
68}
69
Craig Topper37932912013-08-18 10:09:15 +000070void *Stmt::operator new(size_t bytes, const ASTContext& C,
Craig Topper5a050012013-08-18 17:45:38 +000071 unsigned alignment) {
Benjamin Kramerea70eb32012-12-01 15:09:41 +000072 return ::operator new(bytes, C, alignment);
73}
74
Steve Narofff1e53692007-03-23 22:27:02 +000075const char *Stmt::getStmtClassName() const {
John McCall925b16622010-10-26 08:39:16 +000076 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000077}
Steve Narofff84d11f2007-05-23 21:48:04 +000078
79void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000080 // Ensure the table is primed.
81 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000082
Steve Narofff84d11f2007-05-23 21:48:04 +000083 unsigned sum = 0;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000084 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Alexis Hunt656bb312010-05-05 15:24:00 +000085 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000086 if (StmtClassInfo[i].Name == nullptr) continue;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000087 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000088 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000089 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000090 sum = 0;
Alexis Hunt656bb312010-05-05 15:24:00 +000091 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000092 if (StmtClassInfo[i].Name == nullptr) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +000093 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000094 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
95 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
96 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
97 << " bytes)\n";
Chris Lattner4d15a0d2007-08-25 01:42:24 +000098 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +000099 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000100
101 llvm::errs() << "Total bytes = " << sum << "\n";
Steve Narofff84d11f2007-05-23 21:48:04 +0000102}
103
104void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +0000105 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +0000106}
107
Daniel Dunbar62905572012-03-05 21:42:49 +0000108bool Stmt::StatisticsEnabled = false;
109void Stmt::EnableStatistics() {
110 StatisticsEnabled = true;
Steve Narofff84d11f2007-05-23 21:48:04 +0000111}
112
John McCall4db5c3c2011-07-07 06:58:02 +0000113Stmt *Stmt::IgnoreImplicit() {
114 Stmt *s = this;
115
Richard Smith520449d2015-02-05 06:15:50 +0000116 if (auto *ewc = dyn_cast<ExprWithCleanups>(s))
John McCall4db5c3c2011-07-07 06:58:02 +0000117 s = ewc->getSubExpr();
118
Richard Smith520449d2015-02-05 06:15:50 +0000119 if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s))
120 s = mte->GetTemporaryExpr();
121
122 if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s))
123 s = bte->getSubExpr();
124
125 while (auto *ice = dyn_cast<ImplicitCastExpr>(s))
John McCall4db5c3c2011-07-07 06:58:02 +0000126 s = ice->getSubExpr();
127
128 return s;
129}
130
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000131/// Skip no-op (attributed, compound) container stmts and skip captured
Alexander Musmana5f070a2014-10-01 06:03:56 +0000132/// stmt at the top, if \a IgnoreCaptured is true.
133Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
134 Stmt *S = this;
135 if (IgnoreCaptured)
136 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
137 S = CapS->getCapturedStmt();
138 while (true) {
139 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
140 S = AS->getSubStmt();
141 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
142 if (CS->size() != 1)
143 break;
144 S = CS->body_back();
145 } else
146 break;
147 }
148 return S;
149}
150
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000151/// Strip off all label-like statements.
Chandler Carrutha626d642011-09-10 00:02:34 +0000152///
Richard Smithc202b282012-04-14 00:33:13 +0000153/// This will strip off label statements, case statements, attributed
154/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000155const Stmt *Stmt::stripLabelLikeStatements() const {
156 const Stmt *S = this;
157 while (true) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000158 if (const auto *LS = dyn_cast<LabelStmt>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000159 S = LS->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000160 else if (const auto *SC = dyn_cast<SwitchCase>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000161 S = SC->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000162 else if (const auto *AS = dyn_cast<AttributedStmt>(S))
Richard Smithc202b282012-04-14 00:33:13 +0000163 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000164 else
165 return S;
166 }
167}
168
John McCallbd066782011-02-09 08:16:59 +0000169namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000170
John McCallbd066782011-02-09 08:16:59 +0000171 struct good {};
172 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000173
174 // These silly little functions have to be static inline to suppress
175 // unused warnings, and they have to be defined to suppress other
176 // warnings.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000177 static good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000178
179 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000180 template <class T> good implements_children(children_t T::*) {
181 return good();
182 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000183 LLVM_ATTRIBUTE_UNUSED
Eugene Zelenko7855e772018-04-03 00:11:50 +0000184 static bad implements_children(children_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000185 return bad();
186 }
John McCallbd066782011-02-09 08:16:59 +0000187
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000188 typedef SourceLocation getLocStart_t() const;
189 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000190 return good();
191 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000192 LLVM_ATTRIBUTE_UNUSED
Eugene Zelenko7855e772018-04-03 00:11:50 +0000193 static bad implements_getLocStart(getLocStart_t Stmt::*) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000194 return bad();
195 }
196
197 typedef SourceLocation getLocEnd_t() const;
198 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
199 return good();
200 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000201 LLVM_ATTRIBUTE_UNUSED
Eugene Zelenko7855e772018-04-03 00:11:50 +0000202 static bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000203 return bad();
204 }
John McCallbd066782011-02-09 08:16:59 +0000205
206#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000207 (void) is_good(implements_children(&type::children))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000208#define ASSERT_IMPLEMENTS_getLocStart(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000209 (void) is_good(implements_getLocStart(&type::getLocStart))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000210#define ASSERT_IMPLEMENTS_getLocEnd(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000211 (void) is_good(implements_getLocEnd(&type::getLocEnd))
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000212
213} // namespace
John McCallbd066782011-02-09 08:16:59 +0000214
215/// Check whether the various Stmt classes implement their member
216/// functions.
Eli Friedmandc41d792013-09-10 22:57:15 +0000217LLVM_ATTRIBUTE_UNUSED
John McCallbd066782011-02-09 08:16:59 +0000218static inline void check_implementations() {
219#define ABSTRACT_STMT(type)
220#define STMT(type, base) \
221 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000222 ASSERT_IMPLEMENTS_getLocStart(type); \
223 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCallbd066782011-02-09 08:16:59 +0000224#include "clang/AST/StmtNodes.inc"
225}
226
227Stmt::child_range Stmt::children() {
228 switch (getStmtClass()) {
229 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
230#define ABSTRACT_STMT(type)
231#define STMT(type, base) \
232 case Stmt::type##Class: \
233 return static_cast<type*>(this)->children();
234#include "clang/AST/StmtNodes.inc"
235 }
236 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000237}
238
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000239// Amusing macro metaprogramming hack: check whether a class provides
240// a more specific implementation of getSourceRange.
241//
242// See also Expr.cpp:getExprLoc().
243namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000244
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000245 /// This implementation is used when a class provides a custom
246 /// implementation of getSourceRange.
247 template <class S, class T>
248 SourceRange getSourceRangeImpl(const Stmt *stmt,
249 SourceRange (T::*v)() const) {
250 return static_cast<const S*>(stmt)->getSourceRange();
251 }
252
253 /// This implementation is used when a class doesn't provide a custom
254 /// implementation of getSourceRange. Overload resolution should pick it over
255 /// the implementation above because it's more specialized according to
256 /// function template partial ordering.
257 template <class S>
258 SourceRange getSourceRangeImpl(const Stmt *stmt,
259 SourceRange (Stmt::*v)() const) {
260 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
261 static_cast<const S*>(stmt)->getLocEnd());
262 }
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000263
264} // namespace
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000265
John McCallbd066782011-02-09 08:16:59 +0000266SourceRange Stmt::getSourceRange() const {
267 switch (getStmtClass()) {
268 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
269#define ABSTRACT_STMT(type)
270#define STMT(type, base) \
271 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000272 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000273#include "clang/AST/StmtNodes.inc"
274 }
275 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000276}
277
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000278SourceLocation Stmt::getLocStart() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000279// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000280 switch (getStmtClass()) {
281 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
282#define ABSTRACT_STMT(type)
283#define STMT(type, base) \
284 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000285 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000286#include "clang/AST/StmtNodes.inc"
287 }
288 llvm_unreachable("unknown statement kind");
289}
290
291SourceLocation Stmt::getLocEnd() const {
292 switch (getStmtClass()) {
293 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
294#define ABSTRACT_STMT(type)
295#define STMT(type, base) \
296 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000297 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000298#include "clang/AST/StmtNodes.inc"
299 }
300 llvm_unreachable("unknown statement kind");
301}
302
Benjamin Kramer07420902017-12-24 16:24:20 +0000303CompoundStmt::CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB,
304 SourceLocation RB)
305 : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000306 CompoundStmtBits.NumStmts = Stmts.size();
Benjamin Kramer07420902017-12-24 16:24:20 +0000307 setStmts(Stmts);
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000308}
309
Benjamin Kramer07420902017-12-24 16:24:20 +0000310void CompoundStmt::setStmts(ArrayRef<Stmt *> Stmts) {
Craig Topper9ee84ad2015-12-04 05:01:44 +0000311 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
312 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
Douglas Gregora9af1d12009-04-17 00:04:06 +0000313
Benjamin Kramer07420902017-12-24 16:24:20 +0000314 std::copy(Stmts.begin(), Stmts.end(), body_begin());
315}
316
317CompoundStmt *CompoundStmt::Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
318 SourceLocation LB, SourceLocation RB) {
319 void *Mem =
320 C.Allocate(totalSizeToAlloc<Stmt *>(Stmts.size()), alignof(CompoundStmt));
321 return new (Mem) CompoundStmt(Stmts, LB, RB);
322}
323
324CompoundStmt *CompoundStmt::CreateEmpty(const ASTContext &C,
325 unsigned NumStmts) {
326 void *Mem =
327 C.Allocate(totalSizeToAlloc<Stmt *>(NumStmts), alignof(CompoundStmt));
328 CompoundStmt *New = new (Mem) CompoundStmt(EmptyShell());
329 New->CompoundStmtBits.NumStmts = NumStmts;
330 return New;
Douglas Gregora9af1d12009-04-17 00:04:06 +0000331}
Steve Narofff84d11f2007-05-23 21:48:04 +0000332
Chris Lattnereefa10e2007-05-28 06:56:27 +0000333const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000334 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000335}
336
Craig Toppere6960e22013-08-22 05:28:54 +0000337AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000338 ArrayRef<const Attr*> Attrs,
339 Stmt *SubStmt) {
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000340 assert(!Attrs.empty() && "Attrs should not be empty");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000341 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(Attrs.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000342 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000343 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
344}
345
Craig Toppere6960e22013-08-22 05:28:54 +0000346AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
347 unsigned NumAttrs) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000348 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000349 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(NumAttrs),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000350 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000351 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
352}
353
Craig Topperc571c812013-08-22 06:02:26 +0000354std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000355 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000356 return gccAsmStmt->generateAsmString(C);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000357 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000358 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000359 llvm_unreachable("unknown asm statement kind!");
360}
361
362StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000363 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000364 return gccAsmStmt->getOutputConstraint(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000365 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000366 return msAsmStmt->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000367 llvm_unreachable("unknown asm statement kind!");
368}
369
370const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000371 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000372 return gccAsmStmt->getOutputExpr(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000373 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000374 return msAsmStmt->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000375 llvm_unreachable("unknown asm statement kind!");
376}
377
378StringRef AsmStmt::getInputConstraint(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000379 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000380 return gccAsmStmt->getInputConstraint(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000381 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000382 return msAsmStmt->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000383 llvm_unreachable("unknown asm statement kind!");
384}
385
386const Expr *AsmStmt::getInputExpr(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000387 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000388 return gccAsmStmt->getInputExpr(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000389 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000390 return msAsmStmt->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000391 llvm_unreachable("unknown asm statement kind!");
392}
393
394StringRef AsmStmt::getClobber(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000395 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000396 return gccAsmStmt->getClobber(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000397 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000398 return msAsmStmt->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000399 llvm_unreachable("unknown asm statement kind!");
400}
401
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000402/// getNumPlusOperands - Return the number of output operands that have a "+"
403/// constraint.
404unsigned AsmStmt::getNumPlusOperands() const {
405 unsigned Res = 0;
406 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
407 if (isOutputPlusConstraint(i))
408 ++Res;
409 return Res;
410}
411
Akira Hatanaka987f1862014-08-22 06:05:21 +0000412char GCCAsmStmt::AsmStringPiece::getModifier() const {
413 assert(isOperand() && "Only Operands can have modifiers.");
414 return isLetter(Str[0]) ? Str[0] : '\0';
415}
416
Chad Rosier6100ae12012-08-27 23:47:56 +0000417StringRef GCCAsmStmt::getClobber(unsigned i) const {
418 return getClobberStringLiteral(i)->getString();
419}
420
Chad Rosierde70e0e2012-08-25 00:11:56 +0000421Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000422 return cast<Expr>(Exprs[i]);
423}
Chris Lattner72bbf172009-03-10 04:59:06 +0000424
425/// getOutputConstraint - Return the constraint string for the specified
426/// output operand. All output constraints are known to be non-empty (either
427/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000428StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000429 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000430}
Chris Lattner72bbf172009-03-10 04:59:06 +0000431
Chad Rosierde70e0e2012-08-25 00:11:56 +0000432Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000433 return cast<Expr>(Exprs[i + NumOutputs]);
434}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000435
Chad Rosierde70e0e2012-08-25 00:11:56 +0000436void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000437 Exprs[i + NumOutputs] = E;
438}
439
Chris Lattner72bbf172009-03-10 04:59:06 +0000440/// getInputConstraint - Return the specified input constraint. Unlike output
441/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000442StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000443 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000444}
445
Craig Topperc571c812013-08-22 06:02:26 +0000446void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
447 IdentifierInfo **Names,
448 StringLiteral **Constraints,
449 Stmt **Exprs,
450 unsigned NumOutputs,
451 unsigned NumInputs,
452 StringLiteral **Clobbers,
453 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000454 this->NumOutputs = NumOutputs;
455 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000456 this->NumClobbers = NumClobbers;
457
458 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000459
Anders Carlsson98323d22010-01-30 23:19:41 +0000460 C.Deallocate(this->Names);
461 this->Names = new (C) IdentifierInfo*[NumExprs];
462 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000463
Anders Carlsson98323d22010-01-30 23:19:41 +0000464 C.Deallocate(this->Exprs);
465 this->Exprs = new (C) Stmt*[NumExprs];
466 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000467
Anders Carlsson98323d22010-01-30 23:19:41 +0000468 C.Deallocate(this->Constraints);
469 this->Constraints = new (C) StringLiteral*[NumExprs];
470 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000471
Anders Carlsson98323d22010-01-30 23:19:41 +0000472 C.Deallocate(this->Clobbers);
473 this->Clobbers = new (C) StringLiteral*[NumClobbers];
474 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000475}
476
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000477/// getNamedOperand - Given a symbolic operand reference like %[foo],
478/// translate this into a numeric value needed to reference the same operand.
479/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000480int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000481 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000482
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000483 // Check if this is an output operand.
484 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
485 if (getOutputName(i) == SymbolicName)
486 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000487 }
Mike Stump11289f42009-09-09 15:08:12 +0000488
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000489 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
490 if (getInputName(i) == SymbolicName)
491 return getNumOutputs() + NumPlusOperands + i;
492
493 // Not found.
494 return -1;
495}
496
Chris Lattner35b58362009-03-10 23:21:44 +0000497/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
498/// it into pieces. If the asm string is erroneous, emit errors and return
499/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000500unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000501 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000502 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000503 const char *StrStart = Str.begin();
504 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000505 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattner35b58362009-03-10 23:21:44 +0000507 // "Simple" inline asms have no constraints or operands, just convert the asm
508 // string to escape $'s.
509 if (isSimple()) {
510 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000511 for (; CurPtr != StrEnd; ++CurPtr) {
512 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000513 case '$':
514 Result += "$$";
515 break;
516 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000517 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000518 break;
519 }
520 }
521 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000522 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000523 }
524
525 // CurStringPiece - The current string that we are building up as we scan the
526 // asm string.
527 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000528
Douglas Gregore8bbc122011-09-02 00:18:52 +0000529 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000530
Richard Smithd18ab802016-05-16 22:52:23 +0000531 unsigned LastAsmStringToken = 0;
532 unsigned LastAsmStringOffset = 0;
533
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000534 while (true) {
Chris Lattner35b58362009-03-10 23:21:44 +0000535 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000536 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000537 if (!CurStringPiece.empty())
538 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000539 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000540 }
Mike Stump11289f42009-09-09 15:08:12 +0000541
Chris Lattnera41b8472009-03-10 23:51:40 +0000542 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000543 switch (CurChar) {
544 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000545 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
546 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
547 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000548 case '%':
549 break;
550 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000551 CurStringPiece += CurChar;
552 continue;
553 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000554
Chris Lattner35b58362009-03-10 23:21:44 +0000555 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000556 if (CurPtr == StrEnd) {
557 // % at end of string is invalid (no escape).
558 DiagOffs = CurPtr-StrStart-1;
559 return diag::err_asm_invalid_escape;
560 }
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000561 // Handle escaped char and continue looping over the asm string.
Chris Lattnera41b8472009-03-10 23:51:40 +0000562 char EscapedChar = *CurPtr++;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000563 switch (EscapedChar) {
564 default:
565 break;
566 case '%': // %% -> %
567 case '{': // %{ -> {
568 case '}': // %} -> }
569 CurStringPiece += EscapedChar;
Chris Lattner35b58362009-03-10 23:21:44 +0000570 continue;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000571 case '=': // %= -> Generate a unique ID.
Chris Lattner35b58362009-03-10 23:21:44 +0000572 CurStringPiece += "${:uid}";
573 continue;
574 }
Mike Stump11289f42009-09-09 15:08:12 +0000575
Chris Lattner35b58362009-03-10 23:21:44 +0000576 // Otherwise, we have an operand. If we have accumulated a string so far,
577 // add it to the Pieces list.
578 if (!CurStringPiece.empty()) {
579 Pieces.push_back(AsmStringPiece(CurStringPiece));
580 CurStringPiece.clear();
581 }
Mike Stump11289f42009-09-09 15:08:12 +0000582
Akira Hatanaka987f1862014-08-22 06:05:21 +0000583 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
584 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
585
586 const char *Begin = CurPtr - 1; // Points to the character following '%'.
587 const char *Percent = Begin - 1; // Points to '%'.
588
Jordan Rosea7d03842013-02-08 22:30:41 +0000589 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000590 if (CurPtr == StrEnd) { // Premature end.
591 DiagOffs = CurPtr-StrStart-1;
592 return diag::err_asm_invalid_escape;
593 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000594 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Akira Hatanaka987f1862014-08-22 06:05:21 +0000597 const TargetInfo &TI = C.getTargetInfo();
598 const SourceManager &SM = C.getSourceManager();
599 const LangOptions &LO = C.getLangOpts();
600
601 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000602 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000603 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000604 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000605
Chris Lattner99d892b2009-03-11 22:52:17 +0000606 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000607 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000608 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000609
Chris Lattner14311922009-03-11 00:23:13 +0000610 unsigned NumOperands =
611 getNumOutputs() + getNumPlusOperands() + getNumInputs();
612 if (N >= NumOperands) {
613 DiagOffs = CurPtr-StrStart-1;
614 return diag::err_asm_invalid_operand_number;
615 }
616
Akira Hatanaka987f1862014-08-22 06:05:21 +0000617 // Str contains "x4" (Operand without the leading %).
618 std::string Str(Begin, CurPtr - Begin);
619
620 // (BeginLoc, EndLoc) represents the range of the operand we are currently
621 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000622 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
623 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
624 &LastAsmStringOffset);
625 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
626 CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken,
627 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000628
Benjamin Kramer3204b152015-05-29 19:42:19 +0000629 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Chris Lattner35b58362009-03-10 23:21:44 +0000630 continue;
631 }
Mike Stump11289f42009-09-09 15:08:12 +0000632
Akira Hatanaka987f1862014-08-22 06:05:21 +0000633 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000634 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000635 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000636
Chris Lattner3fa25c62009-03-11 00:06:36 +0000637 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000638 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000639 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000640 return diag::err_asm_unterminated_symbolic_operand_name;
641 if (NameEnd == CurPtr)
642 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000643
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000644 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000645
Chris Lattner35b58362009-03-10 23:21:44 +0000646 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000647 if (N == -1) {
648 // Verify that an operand with that name exists.
649 DiagOffs = CurPtr-StrStart;
650 return diag::err_asm_unknown_symbolic_operand_name;
651 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000652
653 // Str contains "x[foo]" (Operand without the leading %).
654 std::string Str(Begin, NameEnd + 1 - Begin);
655
656 // (BeginLoc, EndLoc) represents the range of the operand we are currently
657 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000658 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
659 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
660 &LastAsmStringOffset);
661 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
662 NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken,
663 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000664
Benjamin Kramer3204b152015-05-29 19:42:19 +0000665 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattner3fa25c62009-03-11 00:06:36 +0000667 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000668 continue;
669 }
Mike Stump11289f42009-09-09 15:08:12 +0000670
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000671 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000672 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000673 }
674}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000675
676/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000677std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000678 // Analyze the asm string to decompose it into its pieces. We know that Sema
679 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000680 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000681 unsigned DiagOffs;
682 AnalyzeAsmString(Pieces, C, DiagOffs);
683
684 std::string AsmString;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000685 for (const auto &Piece : Pieces) {
686 if (Piece.isString())
687 AsmString += Piece.getString();
688 else if (Piece.getModifier() == '\0')
689 AsmString += '$' + llvm::utostr(Piece.getOperandNo());
Chad Rosier14836ba2012-08-24 17:05:45 +0000690 else
Eugene Zelenko7855e772018-04-03 00:11:50 +0000691 AsmString += "${" + llvm::utostr(Piece.getOperandNo()) + ':' +
692 Piece.getModifier() + '}';
Chad Rosier14836ba2012-08-24 17:05:45 +0000693 }
694 return AsmString;
695}
Chris Lattner35b58362009-03-10 23:21:44 +0000696
Chad Rosier3b0c2602012-08-27 20:23:31 +0000697/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000698std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000699 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000700 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000701}
702
Chad Rosierfe31e622012-08-24 00:07:09 +0000703Expr *MSAsmStmt::getOutputExpr(unsigned i) {
704 return cast<Expr>(Exprs[i]);
705}
706
707Expr *MSAsmStmt::getInputExpr(unsigned i) {
708 return cast<Expr>(Exprs[i + NumOutputs]);
709}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000710
Chad Rosierfe31e622012-08-24 00:07:09 +0000711void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
712 Exprs[i + NumOutputs] = E;
713}
714
Chris Lattner86f5e132008-01-30 05:01:46 +0000715//===----------------------------------------------------------------------===//
716// Constructors
717//===----------------------------------------------------------------------===//
718
Craig Toppere6960e22013-08-22 05:28:54 +0000719GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
720 bool issimple, bool isvolatile, unsigned numoutputs,
721 unsigned numinputs, IdentifierInfo **names,
722 StringLiteral **constraints, Expr **exprs,
723 StringLiteral *asmstr, unsigned numclobbers,
724 StringLiteral **clobbers, SourceLocation rparenloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000725 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
726 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Chad Rosier42032fa2012-08-07 23:12:23 +0000727 unsigned NumExprs = NumOutputs + NumInputs;
728
Anders Carlsson98323d22010-01-30 23:19:41 +0000729 Names = new (C) IdentifierInfo*[NumExprs];
730 std::copy(names, names + NumExprs, Names);
731
732 Exprs = new (C) Stmt*[NumExprs];
733 std::copy(exprs, exprs + NumExprs, Exprs);
734
735 Constraints = new (C) StringLiteral*[NumExprs];
736 std::copy(constraints, constraints + NumExprs, Constraints);
737
738 Clobbers = new (C) StringLiteral*[NumClobbers];
739 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000740}
741
Craig Toppere6960e22013-08-22 05:28:54 +0000742MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000743 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000744 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000745 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000746 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
747 StringRef asmstr, ArrayRef<StringRef> clobbers,
748 SourceLocation endloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000749 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
750 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
751 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
John McCallf413f5e2013-05-03 00:10:13 +0000752 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
753}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000754
Craig Toppere6960e22013-08-22 05:28:54 +0000755static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
Benjamin Kramer2ab0d882015-08-04 12:34:23 +0000756 return str.copy(C);
John McCallf413f5e2013-05-03 00:10:13 +0000757}
758
Craig Toppere6960e22013-08-22 05:28:54 +0000759void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000760 ArrayRef<Token> asmtoks,
761 ArrayRef<StringRef> constraints,
762 ArrayRef<Expr*> exprs,
763 ArrayRef<StringRef> clobbers) {
764 assert(NumAsmToks == asmtoks.size());
765 assert(NumClobbers == clobbers.size());
766
Craig Toppercaf138e2015-12-05 07:41:42 +0000767 assert(exprs.size() == NumOutputs + NumInputs);
768 assert(exprs.size() == constraints.size());
John McCallf413f5e2013-05-03 00:10:13 +0000769
770 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000771
Craig Toppercaf138e2015-12-05 07:41:42 +0000772 Exprs = new (C) Stmt*[exprs.size()];
773 std::copy(exprs.begin(), exprs.end(), Exprs);
Chad Rosierfe31e622012-08-24 00:07:09 +0000774
Craig Toppercaf138e2015-12-05 07:41:42 +0000775 AsmToks = new (C) Token[asmtoks.size()];
776 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000777
Craig Toppercaf138e2015-12-05 07:41:42 +0000778 Constraints = new (C) StringRef[exprs.size()];
779 std::transform(constraints.begin(), constraints.end(), Constraints,
780 [&](StringRef Constraint) {
781 return copyIntoContext(C, Constraint);
782 });
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000783
Chad Rosierbaf53f92012-08-10 21:36:25 +0000784 Clobbers = new (C) StringRef[NumClobbers];
Craig Toppercaf138e2015-12-05 07:41:42 +0000785 // FIXME: Avoid the allocation/copy if at all possible.
786 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
787 [&](StringRef Clobber) {
788 return copyIntoContext(C, Clobber);
789 });
Chad Rosier32503022012-06-11 20:47:18 +0000790}
791
Richard Smithb130fe72016-06-23 19:16:49 +0000792IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, bool IsConstexpr,
Richard Smitha547eb22016-07-14 00:11:03 +0000793 Stmt *init, VarDecl *var, Expr *cond, Stmt *then,
794 SourceLocation EL, Stmt *elsev)
Richard Smithb130fe72016-06-23 19:16:49 +0000795 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL) {
796 setConstexpr(IsConstexpr);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000797 setConditionVariable(C, var);
Richard Smitha547eb22016-07-14 00:11:03 +0000798 SubExprs[INIT] = init;
Pavel Labath515f4db2013-09-03 14:41:16 +0000799 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000800 SubExprs[THEN] = then;
Chad Rosier42032fa2012-08-07 23:12:23 +0000801 SubExprs[ELSE] = elsev;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000802}
803
804VarDecl *IfStmt::getConditionVariable() const {
805 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000806 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000807
Eugene Zelenko7855e772018-04-03 00:11:50 +0000808 auto *DS = cast<DeclStmt>(SubExprs[VAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000809 return cast<VarDecl>(DS->getSingleDecl());
810}
811
Craig Toppere6960e22013-08-22 05:28:54 +0000812void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000813 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000814 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000815 return;
816 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000817
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000818 SourceRange VarRange = V->getSourceRange();
819 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
820 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000821}
822
Erik Pilkington5cd57172016-08-16 17:44:11 +0000823bool IfStmt::isObjCAvailabilityCheck() const {
824 return isa<ObjCAvailabilityCheckExpr>(SubExprs[COND]);
825}
826
Craig Toppere6960e22013-08-22 05:28:54 +0000827ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000828 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000829 SourceLocation RP)
Chad Rosier42032fa2012-08-07 23:12:23 +0000830 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000831{
832 SubExprs[INIT] = Init;
833 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000834 SubExprs[COND] = Cond;
835 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000836 SubExprs[BODY] = Body;
837}
838
839VarDecl *ForStmt::getConditionVariable() const {
840 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000841 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000842
Eugene Zelenko7855e772018-04-03 00:11:50 +0000843 auto *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000844 return cast<VarDecl>(DS->getSingleDecl());
845}
846
Craig Toppere6960e22013-08-22 05:28:54 +0000847void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000848 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000849 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000850 return;
851 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000852
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000853 SourceRange VarRange = V->getSourceRange();
854 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
855 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000856}
857
Richard Smitha547eb22016-07-14 00:11:03 +0000858SwitchStmt::SwitchStmt(const ASTContext &C, Stmt *init, VarDecl *Var,
859 Expr *cond)
Benjamin Kramer475386d2015-04-02 15:29:07 +0000860 : Stmt(SwitchStmtClass), FirstCase(nullptr, false) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000861 setConditionVariable(C, Var);
Richard Smitha547eb22016-07-14 00:11:03 +0000862 SubExprs[INIT] = init;
Pavel Labath515f4db2013-09-03 14:41:16 +0000863 SubExprs[COND] = cond;
Craig Topper36250ad2014-05-12 05:36:57 +0000864 SubExprs[BODY] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000865}
866
867VarDecl *SwitchStmt::getConditionVariable() const {
868 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000869 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000870
Eugene Zelenko7855e772018-04-03 00:11:50 +0000871 auto *DS = cast<DeclStmt>(SubExprs[VAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000872 return cast<VarDecl>(DS->getSingleDecl());
873}
874
Craig Toppere6960e22013-08-22 05:28:54 +0000875void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000876 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000877 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000878 return;
879 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000880
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000881 SourceRange VarRange = V->getSourceRange();
882 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
883 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000884}
885
John McCallbd066782011-02-09 08:16:59 +0000886Stmt *SwitchCase::getSubStmt() {
Chris Lattner80d51eb2011-02-28 00:18:06 +0000887 if (isa<CaseStmt>(this))
888 return cast<CaseStmt>(this)->getSubStmt();
John McCallbd066782011-02-09 08:16:59 +0000889 return cast<DefaultStmt>(this)->getSubStmt();
890}
891
Craig Toppere6960e22013-08-22 05:28:54 +0000892WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000893 SourceLocation WL)
Chris Lattner80d51eb2011-02-28 00:18:06 +0000894 : Stmt(WhileStmtClass) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000895 setConditionVariable(C, Var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000896 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000897 SubExprs[BODY] = body;
898 WhileLoc = WL;
899}
900
901VarDecl *WhileStmt::getConditionVariable() const {
902 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000903 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000904
Eugene Zelenko7855e772018-04-03 00:11:50 +0000905 auto *DS = cast<DeclStmt>(SubExprs[VAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000906 return cast<VarDecl>(DS->getSingleDecl());
907}
908
Craig Toppere6960e22013-08-22 05:28:54 +0000909void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000910 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000911 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000912 return;
913 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000914
915 SourceRange VarRange = V->getSourceRange();
916 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
917 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000918}
919
Ted Kremenek066dd932007-08-24 21:09:09 +0000920// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000921LabelDecl *IndirectGotoStmt::getConstantTarget() {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000922 if (auto *E = dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
John McCall9de91602010-10-28 08:53:48 +0000923 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +0000924 return nullptr;
John McCall9de91602010-10-28 08:53:48 +0000925}
Ted Kremenek066dd932007-08-24 21:09:09 +0000926
Ted Kremenek066dd932007-08-24 21:09:09 +0000927// ReturnStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000928const Expr* ReturnStmt::getRetValue() const {
929 return cast_or_null<Expr>(RetExpr);
930}
931Expr* ReturnStmt::getRetValue() {
932 return cast_or_null<Expr>(RetExpr);
Ted Kremenek066dd932007-08-24 21:09:09 +0000933}
John Wiegley1c0675e2011-04-28 01:08:34 +0000934
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000935SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +0000936 Stmt *Handler)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000937 : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc) {
Warren Huntf6be4cb2014-07-25 20:52:51 +0000938 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000939 Children[HANDLER] = Handler;
940}
941
Warren Huntf6be4cb2014-07-25 20:52:51 +0000942SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +0000943 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +0000944 Stmt *Handler) {
945 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +0000946}
947
948SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
949 return dyn_cast<SEHExceptStmt>(getHandler());
950}
951
952SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
953 return dyn_cast<SEHFinallyStmt>(getHandler());
954}
955
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000956SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block)
957 : Stmt(SEHExceptStmtClass), Loc(Loc) {
Pavel Labath515f4db2013-09-03 14:41:16 +0000958 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +0000959 Children[BLOCK] = Block;
960}
961
Craig Toppere6960e22013-08-22 05:28:54 +0000962SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
963 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000964 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
965}
966
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000967SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, Stmt *Block)
968 : Stmt(SEHFinallyStmtClass), Loc(Loc), Block(Block) {}
John Wiegley1c0675e2011-04-28 01:08:34 +0000969
Craig Toppere6960e22013-08-22 05:28:54 +0000970SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +0000971 Stmt *Block) {
972 return new(C)SEHFinallyStmt(Loc,Block);
973}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000974
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000975CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
976 VarDecl *Var)
977 : VarAndKind(Var, Kind), Loc(Loc) {
978 switch (Kind) {
979 case VCK_This:
980 assert(!Var && "'this' capture cannot have a variable!");
981 break;
982 case VCK_ByRef:
983 assert(Var && "capturing by reference must have a variable!");
984 break;
985 case VCK_ByCopy:
986 assert(Var && "capturing by copy must have a variable!");
987 assert(
988 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
989 Var->getType()
990 ->castAs<ReferenceType>()
991 ->getPointeeType()
992 ->isScalarType())) &&
993 "captures by copy are expected to have a scalar type!");
994 break;
995 case VCK_VLAType:
996 assert(!Var &&
997 "Variable-length array type capture cannot have a variable!");
998 break;
999 }
1000}
1001
Chandler Carruth21c90602015-12-30 03:24:14 +00001002CapturedStmt::VariableCaptureKind
1003CapturedStmt::Capture::getCaptureKind() const {
1004 return VarAndKind.getInt();
1005}
1006
1007VarDecl *CapturedStmt::Capture::getCapturedVar() const {
1008 assert((capturesVariable() || capturesVariableByCopy()) &&
1009 "No variable available for 'this' or VAT capture");
1010 return VarAndKind.getPointer();
1011}
1012
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001013CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1014 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1015
1016 // Offset of the first Capture object.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001017 unsigned FirstCaptureOffset = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001018
1019 return reinterpret_cast<Capture *>(
1020 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1021 + FirstCaptureOffset);
1022}
1023
Wei Pan17fbf6e2013-05-04 03:59:06 +00001024CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1025 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001026 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001027 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001028 RecordDecl *RD)
1029 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001030 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001031 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001032 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001033 assert(RD && "null record declaration for captured statement");
1034
1035 // Copy initialization expressions.
1036 Stmt **Stored = getStoredStmts();
1037 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1038 *Stored++ = CaptureInits[I];
1039
1040 // Copy the statement being captured.
1041 *Stored = S;
1042
1043 // Copy all Capture objects.
1044 Capture *Buffer = getStoredCaptures();
1045 std::copy(Captures.begin(), Captures.end(), Buffer);
1046}
1047
1048CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1049 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001050 CapDeclAndKind(nullptr, CR_Default) {
Craig Topper36250ad2014-05-12 05:36:57 +00001051 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001052}
1053
Craig Toppere6960e22013-08-22 05:28:54 +00001054CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001055 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001056 ArrayRef<Capture> Captures,
1057 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001058 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001059 RecordDecl *RD) {
1060 // The layout is
1061 //
1062 // -----------------------------------------------------------
1063 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1064 // ----------------^-------------------^----------------------
1065 // getStoredStmts() getStoredCaptures()
1066 //
1067 // where S is the statement being captured.
1068 //
1069 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1070
1071 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1072 if (!Captures.empty()) {
1073 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001074 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001075 Size += sizeof(Capture) * Captures.size();
1076 }
1077
1078 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001079 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001080}
1081
Craig Toppere6960e22013-08-22 05:28:54 +00001082CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001083 unsigned NumCaptures) {
1084 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1085 if (NumCaptures > 0) {
1086 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001087 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001088 Size += sizeof(Capture) * NumCaptures;
1089 }
1090
1091 void *Mem = Context.Allocate(Size);
1092 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1093}
1094
1095Stmt::child_range CapturedStmt::children() {
Simon Pilgrim2c518802017-03-30 14:13:19 +00001096 // Children are captured field initializers.
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001097 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001098}
1099
Chandler Carruth21c90602015-12-30 03:24:14 +00001100CapturedDecl *CapturedStmt::getCapturedDecl() {
1101 return CapDeclAndKind.getPointer();
1102}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001103
Chandler Carruth21c90602015-12-30 03:24:14 +00001104const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1105 return CapDeclAndKind.getPointer();
1106}
1107
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001108/// Set the outlined function declaration.
Chandler Carruth21c90602015-12-30 03:24:14 +00001109void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1110 assert(D && "null CapturedDecl");
1111 CapDeclAndKind.setPointer(D);
1112}
1113
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001114/// Retrieve the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001115CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1116 return CapDeclAndKind.getInt();
1117}
1118
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001119/// Set the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001120void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1121 CapDeclAndKind.setInt(Kind);
1122}
1123
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001124bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001125 for (const auto &I : captures()) {
Alexey Bataev2c845412017-05-15 16:26:15 +00001126 if (!I.capturesVariable() && !I.capturesVariableByCopy())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001127 continue;
Alexey Bataeve85de8f2017-09-20 20:11:31 +00001128 if (I.getCapturedVar()->getCanonicalDecl() == Var->getCanonicalDecl())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001129 return true;
1130 }
1131
1132 return false;
1133}