blob: 116291bfa1efbdc5abc61e1e628c46fcbaf0b801 [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
Bruno Ricci65034b82018-12-04 16:04:19 +000079// Check that no statement / expression class is polymorphic. LLVM style RTTI
80// should be used instead. If absolutely needed an exception can still be added
81// here by defining the appropriate macro (but please don't do this).
82#define STMT(CLASS, PARENT) \
83 static_assert(!std::is_polymorphic<CLASS>::value, \
84 #CLASS " should not be polymorphic!");
85#include "clang/AST/StmtNodes.inc"
86
Steve Narofff84d11f2007-05-23 21:48:04 +000087void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000088 // Ensure the table is primed.
89 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000090
Steve Narofff84d11f2007-05-23 21:48:04 +000091 unsigned sum = 0;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000092 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Alexis Hunt656bb312010-05-05 15:24:00 +000093 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000094 if (StmtClassInfo[i].Name == nullptr) continue;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000095 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000096 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000097 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000098 sum = 0;
Alexis Hunt656bb312010-05-05 15:24:00 +000099 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +0000100 if (StmtClassInfo[i].Name == nullptr) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +0000101 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000102 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
103 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
104 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
105 << " bytes)\n";
Chris Lattner4d15a0d2007-08-25 01:42:24 +0000106 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +0000107 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000108
109 llvm::errs() << "Total bytes = " << sum << "\n";
Steve Narofff84d11f2007-05-23 21:48:04 +0000110}
111
112void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +0000113 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +0000114}
115
Daniel Dunbar62905572012-03-05 21:42:49 +0000116bool Stmt::StatisticsEnabled = false;
117void Stmt::EnableStatistics() {
118 StatisticsEnabled = true;
Steve Narofff84d11f2007-05-23 21:48:04 +0000119}
120
John McCall4db5c3c2011-07-07 06:58:02 +0000121Stmt *Stmt::IgnoreImplicit() {
122 Stmt *s = this;
123
Stephen Kelly0945c8b2018-08-14 21:33:28 +0000124 Stmt *lasts = nullptr;
John McCall4db5c3c2011-07-07 06:58:02 +0000125
Stephen Kelly0945c8b2018-08-14 21:33:28 +0000126 while (s != lasts) {
127 lasts = s;
Richard Smith520449d2015-02-05 06:15:50 +0000128
Bill Wendling7c44da22018-10-31 03:48:47 +0000129 if (auto *fe = dyn_cast<FullExpr>(s))
130 s = fe->getSubExpr();
Richard Smith520449d2015-02-05 06:15:50 +0000131
Stephen Kelly0945c8b2018-08-14 21:33:28 +0000132 if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s))
133 s = mte->GetTemporaryExpr();
134
135 if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s))
136 s = bte->getSubExpr();
137
138 if (auto *ice = dyn_cast<ImplicitCastExpr>(s))
139 s = ice->getSubExpr();
140 }
John McCall4db5c3c2011-07-07 06:58:02 +0000141
142 return s;
143}
144
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000145/// Skip no-op (attributed, compound) container stmts and skip captured
Alexander Musmana5f070a2014-10-01 06:03:56 +0000146/// stmt at the top, if \a IgnoreCaptured is true.
147Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
148 Stmt *S = this;
149 if (IgnoreCaptured)
150 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
151 S = CapS->getCapturedStmt();
152 while (true) {
153 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
154 S = AS->getSubStmt();
155 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
156 if (CS->size() != 1)
157 break;
158 S = CS->body_back();
159 } else
160 break;
161 }
162 return S;
163}
164
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000165/// Strip off all label-like statements.
Chandler Carrutha626d642011-09-10 00:02:34 +0000166///
Richard Smithc202b282012-04-14 00:33:13 +0000167/// This will strip off label statements, case statements, attributed
168/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000169const Stmt *Stmt::stripLabelLikeStatements() const {
170 const Stmt *S = this;
171 while (true) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000172 if (const auto *LS = dyn_cast<LabelStmt>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000173 S = LS->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000174 else if (const auto *SC = dyn_cast<SwitchCase>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000175 S = SC->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000176 else if (const auto *AS = dyn_cast<AttributedStmt>(S))
Richard Smithc202b282012-04-14 00:33:13 +0000177 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000178 else
179 return S;
180 }
181}
182
John McCallbd066782011-02-09 08:16:59 +0000183namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000184
John McCallbd066782011-02-09 08:16:59 +0000185 struct good {};
186 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000187
188 // These silly little functions have to be static inline to suppress
189 // unused warnings, and they have to be defined to suppress other
190 // warnings.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000191 static good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000192
193 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000194 template <class T> good implements_children(children_t T::*) {
195 return good();
196 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000197 LLVM_ATTRIBUTE_UNUSED
Eugene Zelenko7855e772018-04-03 00:11:50 +0000198 static bad implements_children(children_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000199 return bad();
200 }
John McCallbd066782011-02-09 08:16:59 +0000201
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000202 typedef SourceLocation getBeginLoc_t() const;
203 template <class T> good implements_getBeginLoc(getBeginLoc_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000204 return good();
205 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000206 LLVM_ATTRIBUTE_UNUSED
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000207 static bad implements_getBeginLoc(getBeginLoc_t Stmt::*) { return bad(); }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000208
209 typedef SourceLocation getLocEnd_t() const;
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000210 template <class T> good implements_getEndLoc(getLocEnd_t T::*) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000211 return good();
212 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000213 LLVM_ATTRIBUTE_UNUSED
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000214 static bad implements_getEndLoc(getLocEnd_t Stmt::*) { return bad(); }
John McCallbd066782011-02-09 08:16:59 +0000215
216#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000217 (void) is_good(implements_children(&type::children))
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000218#define ASSERT_IMPLEMENTS_getBeginLoc(type) \
219 (void)is_good(implements_getBeginLoc(&type::getBeginLoc))
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000220#define ASSERT_IMPLEMENTS_getEndLoc(type) \
221 (void)is_good(implements_getEndLoc(&type::getEndLoc))
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000222
223} // namespace
John McCallbd066782011-02-09 08:16:59 +0000224
225/// Check whether the various Stmt classes implement their member
226/// functions.
Eli Friedmandc41d792013-09-10 22:57:15 +0000227LLVM_ATTRIBUTE_UNUSED
John McCallbd066782011-02-09 08:16:59 +0000228static inline void check_implementations() {
229#define ABSTRACT_STMT(type)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000230#define STMT(type, base) \
231 ASSERT_IMPLEMENTS_children(type); \
232 ASSERT_IMPLEMENTS_getBeginLoc(type); \
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000233 ASSERT_IMPLEMENTS_getEndLoc(type);
John McCallbd066782011-02-09 08:16:59 +0000234#include "clang/AST/StmtNodes.inc"
235}
236
237Stmt::child_range Stmt::children() {
238 switch (getStmtClass()) {
239 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
240#define ABSTRACT_STMT(type)
241#define STMT(type, base) \
242 case Stmt::type##Class: \
243 return static_cast<type*>(this)->children();
244#include "clang/AST/StmtNodes.inc"
245 }
246 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000247}
248
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000249// Amusing macro metaprogramming hack: check whether a class provides
250// a more specific implementation of getSourceRange.
251//
252// See also Expr.cpp:getExprLoc().
253namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000254
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000255 /// This implementation is used when a class provides a custom
256 /// implementation of getSourceRange.
257 template <class S, class T>
258 SourceRange getSourceRangeImpl(const Stmt *stmt,
259 SourceRange (T::*v)() const) {
260 return static_cast<const S*>(stmt)->getSourceRange();
261 }
262
263 /// This implementation is used when a class doesn't provide a custom
264 /// implementation of getSourceRange. Overload resolution should pick it over
265 /// the implementation above because it's more specialized according to
266 /// function template partial ordering.
267 template <class S>
268 SourceRange getSourceRangeImpl(const Stmt *stmt,
269 SourceRange (Stmt::*v)() const) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000270 return SourceRange(static_cast<const S *>(stmt)->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000271 static_cast<const S *>(stmt)->getEndLoc());
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000272 }
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000273
274} // namespace
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000275
John McCallbd066782011-02-09 08:16:59 +0000276SourceRange Stmt::getSourceRange() const {
277 switch (getStmtClass()) {
278 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
279#define ABSTRACT_STMT(type)
280#define STMT(type, base) \
281 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000282 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000283#include "clang/AST/StmtNodes.inc"
284 }
285 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000286}
287
Stephen Kelly724e9e52018-08-09 20:05:03 +0000288SourceLocation Stmt::getBeginLoc() const {
289 // llvm::errs() << "getBeginLoc() for " << getStmtClassName() << "\n";
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000290 switch (getStmtClass()) {
291 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
292#define ABSTRACT_STMT(type)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000293#define STMT(type, base) \
294 case Stmt::type##Class: \
295 return static_cast<const type *>(this)->getBeginLoc();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000296#include "clang/AST/StmtNodes.inc"
297 }
298 llvm_unreachable("unknown statement kind");
299}
300
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000301SourceLocation Stmt::getEndLoc() const {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000302 switch (getStmtClass()) {
303 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
304#define ABSTRACT_STMT(type)
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000305#define STMT(type, base) \
306 case Stmt::type##Class: \
307 return static_cast<const type *>(this)->getEndLoc();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000308#include "clang/AST/StmtNodes.inc"
309 }
310 llvm_unreachable("unknown statement kind");
311}
312
George Karpenkov5eb4cc62018-09-15 02:01:47 +0000313int64_t Stmt::getID(const ASTContext &Context) const {
Artem Dergachev057647d2018-12-03 22:19:05 +0000314 return Context.getAllocator().identifyKnownAlignedObject<Stmt>(this);
George Karpenkov5eb4cc62018-09-15 02:01:47 +0000315}
316
Benjamin Kramer07420902017-12-24 16:24:20 +0000317CompoundStmt::CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB,
318 SourceLocation RB)
Bruno Ricci41d11c02018-10-27 18:43:27 +0000319 : Stmt(CompoundStmtClass), RBraceLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000320 CompoundStmtBits.NumStmts = Stmts.size();
Benjamin Kramer07420902017-12-24 16:24:20 +0000321 setStmts(Stmts);
Bruno Ricci41d11c02018-10-27 18:43:27 +0000322 CompoundStmtBits.LBraceLoc = LB;
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000323}
324
Benjamin Kramer07420902017-12-24 16:24:20 +0000325void CompoundStmt::setStmts(ArrayRef<Stmt *> Stmts) {
Craig Topper9ee84ad2015-12-04 05:01:44 +0000326 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
327 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
Douglas Gregora9af1d12009-04-17 00:04:06 +0000328
Benjamin Kramer07420902017-12-24 16:24:20 +0000329 std::copy(Stmts.begin(), Stmts.end(), body_begin());
330}
331
332CompoundStmt *CompoundStmt::Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
333 SourceLocation LB, SourceLocation RB) {
334 void *Mem =
335 C.Allocate(totalSizeToAlloc<Stmt *>(Stmts.size()), alignof(CompoundStmt));
336 return new (Mem) CompoundStmt(Stmts, LB, RB);
337}
338
339CompoundStmt *CompoundStmt::CreateEmpty(const ASTContext &C,
340 unsigned NumStmts) {
341 void *Mem =
342 C.Allocate(totalSizeToAlloc<Stmt *>(NumStmts), alignof(CompoundStmt));
343 CompoundStmt *New = new (Mem) CompoundStmt(EmptyShell());
344 New->CompoundStmtBits.NumStmts = NumStmts;
345 return New;
Douglas Gregora9af1d12009-04-17 00:04:06 +0000346}
Steve Narofff84d11f2007-05-23 21:48:04 +0000347
Chris Lattnereefa10e2007-05-28 06:56:27 +0000348const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000349 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000350}
351
Craig Toppere6960e22013-08-22 05:28:54 +0000352AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000353 ArrayRef<const Attr*> Attrs,
354 Stmt *SubStmt) {
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000355 assert(!Attrs.empty() && "Attrs should not be empty");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000356 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(Attrs.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000357 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000358 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
359}
360
Craig Toppere6960e22013-08-22 05:28:54 +0000361AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
362 unsigned NumAttrs) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000363 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000364 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(NumAttrs),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000365 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000366 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
367}
368
Craig Topperc571c812013-08-22 06:02:26 +0000369std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000370 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000371 return gccAsmStmt->generateAsmString(C);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000372 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000373 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000374 llvm_unreachable("unknown asm statement kind!");
375}
376
377StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000378 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000379 return gccAsmStmt->getOutputConstraint(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000380 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000381 return msAsmStmt->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000382 llvm_unreachable("unknown asm statement kind!");
383}
384
385const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000386 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000387 return gccAsmStmt->getOutputExpr(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000388 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000389 return msAsmStmt->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000390 llvm_unreachable("unknown asm statement kind!");
391}
392
393StringRef AsmStmt::getInputConstraint(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000394 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000395 return gccAsmStmt->getInputConstraint(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000396 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000397 return msAsmStmt->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000398 llvm_unreachable("unknown asm statement kind!");
399}
400
401const Expr *AsmStmt::getInputExpr(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000402 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000403 return gccAsmStmt->getInputExpr(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000404 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000405 return msAsmStmt->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000406 llvm_unreachable("unknown asm statement kind!");
407}
408
409StringRef AsmStmt::getClobber(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000410 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000411 return gccAsmStmt->getClobber(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000412 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000413 return msAsmStmt->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000414 llvm_unreachable("unknown asm statement kind!");
415}
416
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000417/// getNumPlusOperands - Return the number of output operands that have a "+"
418/// constraint.
419unsigned AsmStmt::getNumPlusOperands() const {
420 unsigned Res = 0;
421 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
422 if (isOutputPlusConstraint(i))
423 ++Res;
424 return Res;
425}
426
Akira Hatanaka987f1862014-08-22 06:05:21 +0000427char GCCAsmStmt::AsmStringPiece::getModifier() const {
428 assert(isOperand() && "Only Operands can have modifiers.");
429 return isLetter(Str[0]) ? Str[0] : '\0';
430}
431
Chad Rosier6100ae12012-08-27 23:47:56 +0000432StringRef GCCAsmStmt::getClobber(unsigned i) const {
433 return getClobberStringLiteral(i)->getString();
434}
435
Chad Rosierde70e0e2012-08-25 00:11:56 +0000436Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000437 return cast<Expr>(Exprs[i]);
438}
Chris Lattner72bbf172009-03-10 04:59:06 +0000439
440/// getOutputConstraint - Return the constraint string for the specified
441/// output operand. All output constraints are known to be non-empty (either
442/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000443StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000444 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000445}
Chris Lattner72bbf172009-03-10 04:59:06 +0000446
Chad Rosierde70e0e2012-08-25 00:11:56 +0000447Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000448 return cast<Expr>(Exprs[i + NumOutputs]);
449}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000450
Chad Rosierde70e0e2012-08-25 00:11:56 +0000451void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000452 Exprs[i + NumOutputs] = E;
453}
454
Chris Lattner72bbf172009-03-10 04:59:06 +0000455/// getInputConstraint - Return the specified input constraint. Unlike output
456/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000457StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000458 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000459}
460
Craig Topperc571c812013-08-22 06:02:26 +0000461void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
462 IdentifierInfo **Names,
463 StringLiteral **Constraints,
464 Stmt **Exprs,
465 unsigned NumOutputs,
466 unsigned NumInputs,
467 StringLiteral **Clobbers,
468 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000469 this->NumOutputs = NumOutputs;
470 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000471 this->NumClobbers = NumClobbers;
472
473 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000474
Anders Carlsson98323d22010-01-30 23:19:41 +0000475 C.Deallocate(this->Names);
476 this->Names = new (C) IdentifierInfo*[NumExprs];
477 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000478
Anders Carlsson98323d22010-01-30 23:19:41 +0000479 C.Deallocate(this->Exprs);
480 this->Exprs = new (C) Stmt*[NumExprs];
481 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000482
Anders Carlsson98323d22010-01-30 23:19:41 +0000483 C.Deallocate(this->Constraints);
484 this->Constraints = new (C) StringLiteral*[NumExprs];
485 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000486
Anders Carlsson98323d22010-01-30 23:19:41 +0000487 C.Deallocate(this->Clobbers);
488 this->Clobbers = new (C) StringLiteral*[NumClobbers];
489 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000490}
491
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000492/// getNamedOperand - Given a symbolic operand reference like %[foo],
493/// translate this into a numeric value needed to reference the same operand.
494/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000495int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000496 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000497
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000498 // Check if this is an output operand.
499 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
500 if (getOutputName(i) == SymbolicName)
501 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000504 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
505 if (getInputName(i) == SymbolicName)
506 return getNumOutputs() + NumPlusOperands + i;
507
508 // Not found.
509 return -1;
510}
511
Chris Lattner35b58362009-03-10 23:21:44 +0000512/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
513/// it into pieces. If the asm string is erroneous, emit errors and return
514/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000515unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000516 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000517 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000518 const char *StrStart = Str.begin();
519 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000520 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000521
Chris Lattner35b58362009-03-10 23:21:44 +0000522 // "Simple" inline asms have no constraints or operands, just convert the asm
523 // string to escape $'s.
524 if (isSimple()) {
525 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000526 for (; CurPtr != StrEnd; ++CurPtr) {
527 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000528 case '$':
529 Result += "$$";
530 break;
531 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000532 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000533 break;
534 }
535 }
536 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000537 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000538 }
539
540 // CurStringPiece - The current string that we are building up as we scan the
541 // asm string.
542 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000543
Douglas Gregore8bbc122011-09-02 00:18:52 +0000544 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000545
Richard Smithd18ab802016-05-16 22:52:23 +0000546 unsigned LastAsmStringToken = 0;
547 unsigned LastAsmStringOffset = 0;
548
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000549 while (true) {
Chris Lattner35b58362009-03-10 23:21:44 +0000550 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000551 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000552 if (!CurStringPiece.empty())
553 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000554 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000555 }
Mike Stump11289f42009-09-09 15:08:12 +0000556
Chris Lattnera41b8472009-03-10 23:51:40 +0000557 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000558 switch (CurChar) {
559 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000560 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
561 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
562 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000563 case '%':
564 break;
565 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000566 CurStringPiece += CurChar;
567 continue;
568 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000569
Chris Lattner35b58362009-03-10 23:21:44 +0000570 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000571 if (CurPtr == StrEnd) {
572 // % at end of string is invalid (no escape).
573 DiagOffs = CurPtr-StrStart-1;
574 return diag::err_asm_invalid_escape;
575 }
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000576 // Handle escaped char and continue looping over the asm string.
Chris Lattnera41b8472009-03-10 23:51:40 +0000577 char EscapedChar = *CurPtr++;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000578 switch (EscapedChar) {
579 default:
580 break;
581 case '%': // %% -> %
582 case '{': // %{ -> {
583 case '}': // %} -> }
584 CurStringPiece += EscapedChar;
Chris Lattner35b58362009-03-10 23:21:44 +0000585 continue;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000586 case '=': // %= -> Generate a unique ID.
Chris Lattner35b58362009-03-10 23:21:44 +0000587 CurStringPiece += "${:uid}";
588 continue;
589 }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Chris Lattner35b58362009-03-10 23:21:44 +0000591 // Otherwise, we have an operand. If we have accumulated a string so far,
592 // add it to the Pieces list.
593 if (!CurStringPiece.empty()) {
594 Pieces.push_back(AsmStringPiece(CurStringPiece));
595 CurStringPiece.clear();
596 }
Mike Stump11289f42009-09-09 15:08:12 +0000597
Akira Hatanaka987f1862014-08-22 06:05:21 +0000598 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
599 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
600
601 const char *Begin = CurPtr - 1; // Points to the character following '%'.
602 const char *Percent = Begin - 1; // Points to '%'.
603
Jordan Rosea7d03842013-02-08 22:30:41 +0000604 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000605 if (CurPtr == StrEnd) { // Premature end.
606 DiagOffs = CurPtr-StrStart-1;
607 return diag::err_asm_invalid_escape;
608 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000609 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000610 }
Mike Stump11289f42009-09-09 15:08:12 +0000611
Akira Hatanaka987f1862014-08-22 06:05:21 +0000612 const TargetInfo &TI = C.getTargetInfo();
613 const SourceManager &SM = C.getSourceManager();
614 const LangOptions &LO = C.getLangOpts();
615
616 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000617 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000618 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000619 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattner99d892b2009-03-11 22:52:17 +0000621 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000622 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000623 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000624
Chris Lattner14311922009-03-11 00:23:13 +0000625 unsigned NumOperands =
626 getNumOutputs() + getNumPlusOperands() + getNumInputs();
627 if (N >= NumOperands) {
628 DiagOffs = CurPtr-StrStart-1;
629 return diag::err_asm_invalid_operand_number;
630 }
631
Akira Hatanaka987f1862014-08-22 06:05:21 +0000632 // Str contains "x4" (Operand without the leading %).
633 std::string Str(Begin, CurPtr - Begin);
634
635 // (BeginLoc, EndLoc) represents the range of the operand we are currently
636 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000637 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
638 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
639 &LastAsmStringOffset);
640 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
641 CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken,
642 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000643
Benjamin Kramer3204b152015-05-29 19:42:19 +0000644 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Chris Lattner35b58362009-03-10 23:21:44 +0000645 continue;
646 }
Mike Stump11289f42009-09-09 15:08:12 +0000647
Akira Hatanaka987f1862014-08-22 06:05:21 +0000648 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000649 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000650 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000651
Chris Lattner3fa25c62009-03-11 00:06:36 +0000652 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000653 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000654 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000655 return diag::err_asm_unterminated_symbolic_operand_name;
656 if (NameEnd == CurPtr)
657 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000659 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000660
Chris Lattner35b58362009-03-10 23:21:44 +0000661 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000662 if (N == -1) {
663 // Verify that an operand with that name exists.
664 DiagOffs = CurPtr-StrStart;
665 return diag::err_asm_unknown_symbolic_operand_name;
666 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000667
668 // Str contains "x[foo]" (Operand without the leading %).
669 std::string Str(Begin, NameEnd + 1 - Begin);
670
671 // (BeginLoc, EndLoc) represents the range of the operand we are currently
672 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000673 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
674 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
675 &LastAsmStringOffset);
676 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
677 NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken,
678 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000679
Benjamin Kramer3204b152015-05-29 19:42:19 +0000680 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattner3fa25c62009-03-11 00:06:36 +0000682 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000683 continue;
684 }
Mike Stump11289f42009-09-09 15:08:12 +0000685
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000686 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000687 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000688 }
689}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000690
691/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000692std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000693 // Analyze the asm string to decompose it into its pieces. We know that Sema
694 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000695 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000696 unsigned DiagOffs;
697 AnalyzeAsmString(Pieces, C, DiagOffs);
698
699 std::string AsmString;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000700 for (const auto &Piece : Pieces) {
701 if (Piece.isString())
702 AsmString += Piece.getString();
703 else if (Piece.getModifier() == '\0')
704 AsmString += '$' + llvm::utostr(Piece.getOperandNo());
Chad Rosier14836ba2012-08-24 17:05:45 +0000705 else
Eugene Zelenko7855e772018-04-03 00:11:50 +0000706 AsmString += "${" + llvm::utostr(Piece.getOperandNo()) + ':' +
707 Piece.getModifier() + '}';
Chad Rosier14836ba2012-08-24 17:05:45 +0000708 }
709 return AsmString;
710}
Chris Lattner35b58362009-03-10 23:21:44 +0000711
Chad Rosier3b0c2602012-08-27 20:23:31 +0000712/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000713std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000714 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000715 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000716}
717
Chad Rosierfe31e622012-08-24 00:07:09 +0000718Expr *MSAsmStmt::getOutputExpr(unsigned i) {
719 return cast<Expr>(Exprs[i]);
720}
721
722Expr *MSAsmStmt::getInputExpr(unsigned i) {
723 return cast<Expr>(Exprs[i + NumOutputs]);
724}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000725
Chad Rosierfe31e622012-08-24 00:07:09 +0000726void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
727 Exprs[i + NumOutputs] = E;
728}
729
Chris Lattner86f5e132008-01-30 05:01:46 +0000730//===----------------------------------------------------------------------===//
731// Constructors
732//===----------------------------------------------------------------------===//
733
Craig Toppere6960e22013-08-22 05:28:54 +0000734GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
735 bool issimple, bool isvolatile, unsigned numoutputs,
736 unsigned numinputs, IdentifierInfo **names,
737 StringLiteral **constraints, Expr **exprs,
738 StringLiteral *asmstr, unsigned numclobbers,
739 StringLiteral **clobbers, SourceLocation rparenloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000740 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
741 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Chad Rosier42032fa2012-08-07 23:12:23 +0000742 unsigned NumExprs = NumOutputs + NumInputs;
743
Anders Carlsson98323d22010-01-30 23:19:41 +0000744 Names = new (C) IdentifierInfo*[NumExprs];
745 std::copy(names, names + NumExprs, Names);
746
747 Exprs = new (C) Stmt*[NumExprs];
748 std::copy(exprs, exprs + NumExprs, Exprs);
749
750 Constraints = new (C) StringLiteral*[NumExprs];
751 std::copy(constraints, constraints + NumExprs, Constraints);
752
753 Clobbers = new (C) StringLiteral*[NumClobbers];
754 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000755}
756
Craig Toppere6960e22013-08-22 05:28:54 +0000757MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000758 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000759 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000760 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000761 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
762 StringRef asmstr, ArrayRef<StringRef> clobbers,
763 SourceLocation endloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000764 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
765 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
766 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
John McCallf413f5e2013-05-03 00:10:13 +0000767 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
768}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000769
Craig Toppere6960e22013-08-22 05:28:54 +0000770static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
Benjamin Kramer2ab0d882015-08-04 12:34:23 +0000771 return str.copy(C);
John McCallf413f5e2013-05-03 00:10:13 +0000772}
773
Craig Toppere6960e22013-08-22 05:28:54 +0000774void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000775 ArrayRef<Token> asmtoks,
776 ArrayRef<StringRef> constraints,
777 ArrayRef<Expr*> exprs,
778 ArrayRef<StringRef> clobbers) {
779 assert(NumAsmToks == asmtoks.size());
780 assert(NumClobbers == clobbers.size());
781
Craig Toppercaf138e2015-12-05 07:41:42 +0000782 assert(exprs.size() == NumOutputs + NumInputs);
783 assert(exprs.size() == constraints.size());
John McCallf413f5e2013-05-03 00:10:13 +0000784
785 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000786
Craig Toppercaf138e2015-12-05 07:41:42 +0000787 Exprs = new (C) Stmt*[exprs.size()];
788 std::copy(exprs.begin(), exprs.end(), Exprs);
Chad Rosierfe31e622012-08-24 00:07:09 +0000789
Craig Toppercaf138e2015-12-05 07:41:42 +0000790 AsmToks = new (C) Token[asmtoks.size()];
791 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000792
Craig Toppercaf138e2015-12-05 07:41:42 +0000793 Constraints = new (C) StringRef[exprs.size()];
794 std::transform(constraints.begin(), constraints.end(), Constraints,
795 [&](StringRef Constraint) {
796 return copyIntoContext(C, Constraint);
797 });
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000798
Chad Rosierbaf53f92012-08-10 21:36:25 +0000799 Clobbers = new (C) StringRef[NumClobbers];
Craig Toppercaf138e2015-12-05 07:41:42 +0000800 // FIXME: Avoid the allocation/copy if at all possible.
801 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
802 [&](StringRef Clobber) {
803 return copyIntoContext(C, Clobber);
804 });
Chad Rosier32503022012-06-11 20:47:18 +0000805}
806
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000807IfStmt::IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr,
808 Stmt *Init, VarDecl *Var, Expr *Cond, Stmt *Then,
809 SourceLocation EL, Stmt *Else)
810 : Stmt(IfStmtClass) {
811 bool HasElse = Else != nullptr;
812 bool HasVar = Var != nullptr;
813 bool HasInit = Init != nullptr;
814 IfStmtBits.HasElse = HasElse;
815 IfStmtBits.HasVar = HasVar;
816 IfStmtBits.HasInit = HasInit;
817
Richard Smithb130fe72016-06-23 19:16:49 +0000818 setConstexpr(IsConstexpr);
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000819
820 setCond(Cond);
821 setThen(Then);
822 if (HasElse)
823 setElse(Else);
824 if (HasVar)
825 setConditionVariable(Ctx, Var);
826 if (HasInit)
827 setInit(Init);
828
829 setIfLoc(IL);
830 if (HasElse)
831 setElseLoc(EL);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000832}
833
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000834IfStmt::IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit)
835 : Stmt(IfStmtClass, Empty) {
836 IfStmtBits.HasElse = HasElse;
837 IfStmtBits.HasVar = HasVar;
838 IfStmtBits.HasInit = HasInit;
839}
Chad Rosier42032fa2012-08-07 23:12:23 +0000840
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000841IfStmt *IfStmt::Create(const ASTContext &Ctx, SourceLocation IL,
842 bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond,
843 Stmt *Then, SourceLocation EL, Stmt *Else) {
844 bool HasElse = Else != nullptr;
845 bool HasVar = Var != nullptr;
846 bool HasInit = Init != nullptr;
847 void *Mem = Ctx.Allocate(
848 totalSizeToAlloc<Stmt *, SourceLocation>(
849 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
850 alignof(IfStmt));
851 return new (Mem)
852 IfStmt(Ctx, IL, IsConstexpr, Init, Var, Cond, Then, EL, Else);
853}
854
855IfStmt *IfStmt::CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
856 bool HasInit) {
857 void *Mem = Ctx.Allocate(
858 totalSizeToAlloc<Stmt *, SourceLocation>(
859 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
860 alignof(IfStmt));
861 return new (Mem) IfStmt(EmptyShell(), HasElse, HasVar, HasInit);
862}
863
864VarDecl *IfStmt::getConditionVariable() {
865 auto *DS = getConditionVariableDeclStmt();
866 if (!DS)
867 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000868 return cast<VarDecl>(DS->getSingleDecl());
869}
870
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000871void IfStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
872 assert(hasVarStorage() &&
873 "This if statement has no storage for a condition variable!");
874
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000875 if (!V) {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000876 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000877 return;
878 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000879
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000880 SourceRange VarRange = V->getSourceRange();
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000881 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
882 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000883}
884
Erik Pilkington5cd57172016-08-16 17:44:11 +0000885bool IfStmt::isObjCAvailabilityCheck() const {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000886 return isa<ObjCAvailabilityCheckExpr>(getCond());
Erik Pilkington5cd57172016-08-16 17:44:11 +0000887}
888
Craig Toppere6960e22013-08-22 05:28:54 +0000889ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000890 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000891 SourceLocation RP)
Bruno Ricci41d11c02018-10-27 18:43:27 +0000892 : Stmt(ForStmtClass), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000893{
894 SubExprs[INIT] = Init;
895 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000896 SubExprs[COND] = Cond;
897 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000898 SubExprs[BODY] = Body;
Bruno Ricci41d11c02018-10-27 18:43:27 +0000899 ForStmtBits.ForLoc = FL;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000900}
901
902VarDecl *ForStmt::getConditionVariable() const {
903 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000904 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000905
Eugene Zelenko7855e772018-04-03 00:11:50 +0000906 auto *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000907 return cast<VarDecl>(DS->getSingleDecl());
908}
909
Craig Toppere6960e22013-08-22 05:28:54 +0000910void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000911 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000912 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000913 return;
914 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000915
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000916 SourceRange VarRange = V->getSourceRange();
917 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
918 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000919}
920
Bruno Riccie2806f82018-10-29 16:12:37 +0000921SwitchStmt::SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
922 Expr *Cond)
923 : Stmt(SwitchStmtClass), FirstCase(nullptr) {
924 bool HasInit = Init != nullptr;
925 bool HasVar = Var != nullptr;
926 SwitchStmtBits.HasInit = HasInit;
927 SwitchStmtBits.HasVar = HasVar;
928 SwitchStmtBits.AllEnumCasesCovered = false;
929
930 setCond(Cond);
931 setBody(nullptr);
932 if (HasInit)
933 setInit(Init);
934 if (HasVar)
935 setConditionVariable(Ctx, Var);
936
937 setSwitchLoc(SourceLocation{});
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000938}
939
Bruno Riccie2806f82018-10-29 16:12:37 +0000940SwitchStmt::SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar)
941 : Stmt(SwitchStmtClass, Empty) {
942 SwitchStmtBits.HasInit = HasInit;
943 SwitchStmtBits.HasVar = HasVar;
944 SwitchStmtBits.AllEnumCasesCovered = false;
945}
Chad Rosier42032fa2012-08-07 23:12:23 +0000946
Bruno Riccie2806f82018-10-29 16:12:37 +0000947SwitchStmt *SwitchStmt::Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
948 Expr *Cond) {
949 bool HasInit = Init != nullptr;
950 bool HasVar = Var != nullptr;
951 void *Mem = Ctx.Allocate(
952 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
953 alignof(SwitchStmt));
954 return new (Mem) SwitchStmt(Ctx, Init, Var, Cond);
955}
956
957SwitchStmt *SwitchStmt::CreateEmpty(const ASTContext &Ctx, bool HasInit,
958 bool HasVar) {
959 void *Mem = Ctx.Allocate(
960 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
961 alignof(SwitchStmt));
962 return new (Mem) SwitchStmt(EmptyShell(), HasInit, HasVar);
963}
964
965VarDecl *SwitchStmt::getConditionVariable() {
966 auto *DS = getConditionVariableDeclStmt();
967 if (!DS)
968 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000969 return cast<VarDecl>(DS->getSingleDecl());
970}
971
Bruno Riccie2806f82018-10-29 16:12:37 +0000972void SwitchStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
973 assert(hasVarStorage() &&
974 "This switch statement has no storage for a condition variable!");
975
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000976 if (!V) {
Bruno Riccie2806f82018-10-29 16:12:37 +0000977 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000978 return;
979 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000980
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000981 SourceRange VarRange = V->getSourceRange();
Bruno Riccie2806f82018-10-29 16:12:37 +0000982 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
983 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000984}
985
Bruno Riccibacf7512018-10-30 13:42:41 +0000986WhileStmt::WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
987 Stmt *Body, SourceLocation WL)
988 : Stmt(WhileStmtClass) {
989 bool HasVar = Var != nullptr;
990 WhileStmtBits.HasVar = HasVar;
991
992 setCond(Cond);
993 setBody(Body);
994 if (HasVar)
995 setConditionVariable(Ctx, Var);
996
997 setWhileLoc(WL);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000998}
999
Bruno Riccibacf7512018-10-30 13:42:41 +00001000WhileStmt::WhileStmt(EmptyShell Empty, bool HasVar)
1001 : Stmt(WhileStmtClass, Empty) {
1002 WhileStmtBits.HasVar = HasVar;
1003}
Chad Rosier42032fa2012-08-07 23:12:23 +00001004
Bruno Riccibacf7512018-10-30 13:42:41 +00001005WhileStmt *WhileStmt::Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
1006 Stmt *Body, SourceLocation WL) {
1007 bool HasVar = Var != nullptr;
1008 void *Mem =
1009 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1010 alignof(WhileStmt));
1011 return new (Mem) WhileStmt(Ctx, Var, Cond, Body, WL);
1012}
1013
1014WhileStmt *WhileStmt::CreateEmpty(const ASTContext &Ctx, bool HasVar) {
1015 void *Mem =
1016 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1017 alignof(WhileStmt));
1018 return new (Mem) WhileStmt(EmptyShell(), HasVar);
1019}
1020
1021VarDecl *WhileStmt::getConditionVariable() {
1022 auto *DS = getConditionVariableDeclStmt();
1023 if (!DS)
1024 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001025 return cast<VarDecl>(DS->getSingleDecl());
1026}
1027
Bruno Riccibacf7512018-10-30 13:42:41 +00001028void WhileStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
1029 assert(hasVarStorage() &&
1030 "This while statement has no storage for a condition variable!");
1031
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001032 if (!V) {
Bruno Riccibacf7512018-10-30 13:42:41 +00001033 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001034 return;
1035 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +00001036
1037 SourceRange VarRange = V->getSourceRange();
Bruno Riccibacf7512018-10-30 13:42:41 +00001038 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
1039 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001040}
1041
Ted Kremenek066dd932007-08-24 21:09:09 +00001042// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001043LabelDecl *IndirectGotoStmt::getConstantTarget() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001044 if (auto *E = dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
John McCall9de91602010-10-28 08:53:48 +00001045 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +00001046 return nullptr;
John McCall9de91602010-10-28 08:53:48 +00001047}
Ted Kremenek066dd932007-08-24 21:09:09 +00001048
Ted Kremenek066dd932007-08-24 21:09:09 +00001049// ReturnStmt
Bruno Ricci023b1d12018-10-30 14:40:49 +00001050ReturnStmt::ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1051 : Stmt(ReturnStmtClass), RetExpr(E) {
1052 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1053 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1054 if (HasNRVOCandidate)
1055 setNRVOCandidate(NRVOCandidate);
1056 setReturnLoc(RL);
Ted Kremenekc6501db2008-06-17 03:11:08 +00001057}
Bruno Ricci023b1d12018-10-30 14:40:49 +00001058
1059ReturnStmt::ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate)
1060 : Stmt(ReturnStmtClass, Empty) {
1061 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1062}
1063
1064ReturnStmt *ReturnStmt::Create(const ASTContext &Ctx, SourceLocation RL,
1065 Expr *E, const VarDecl *NRVOCandidate) {
1066 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1067 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1068 alignof(ReturnStmt));
1069 return new (Mem) ReturnStmt(RL, E, NRVOCandidate);
1070}
1071
1072ReturnStmt *ReturnStmt::CreateEmpty(const ASTContext &Ctx,
1073 bool HasNRVOCandidate) {
1074 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1075 alignof(ReturnStmt));
1076 return new (Mem) ReturnStmt(EmptyShell(), HasNRVOCandidate);
Ted Kremenek066dd932007-08-24 21:09:09 +00001077}
John Wiegley1c0675e2011-04-28 01:08:34 +00001078
Bruno Ricci5b30571752018-10-28 12:30:53 +00001079// CaseStmt
1080CaseStmt *CaseStmt::Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1081 SourceLocation caseLoc, SourceLocation ellipsisLoc,
1082 SourceLocation colonLoc) {
1083 bool CaseStmtIsGNURange = rhs != nullptr;
1084 void *Mem = Ctx.Allocate(
1085 totalSizeToAlloc<Stmt *, SourceLocation>(
1086 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1087 alignof(CaseStmt));
1088 return new (Mem) CaseStmt(lhs, rhs, caseLoc, ellipsisLoc, colonLoc);
1089}
1090
1091CaseStmt *CaseStmt::CreateEmpty(const ASTContext &Ctx,
1092 bool CaseStmtIsGNURange) {
1093 void *Mem = Ctx.Allocate(
1094 totalSizeToAlloc<Stmt *, SourceLocation>(
1095 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1096 alignof(CaseStmt));
1097 return new (Mem) CaseStmt(EmptyShell(), CaseStmtIsGNURange);
1098}
1099
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001100SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001101 Stmt *Handler)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001102 : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc) {
Warren Huntf6be4cb2014-07-25 20:52:51 +00001103 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +00001104 Children[HANDLER] = Handler;
1105}
1106
Warren Huntf6be4cb2014-07-25 20:52:51 +00001107SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +00001108 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001109 Stmt *Handler) {
1110 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +00001111}
1112
1113SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1114 return dyn_cast<SEHExceptStmt>(getHandler());
1115}
1116
1117SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1118 return dyn_cast<SEHFinallyStmt>(getHandler());
1119}
1120
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001121SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block)
1122 : Stmt(SEHExceptStmtClass), Loc(Loc) {
Pavel Labath515f4db2013-09-03 14:41:16 +00001123 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +00001124 Children[BLOCK] = Block;
1125}
1126
Craig Toppere6960e22013-08-22 05:28:54 +00001127SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
1128 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +00001129 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1130}
1131
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001132SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, Stmt *Block)
1133 : Stmt(SEHFinallyStmtClass), Loc(Loc), Block(Block) {}
John Wiegley1c0675e2011-04-28 01:08:34 +00001134
Craig Toppere6960e22013-08-22 05:28:54 +00001135SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +00001136 Stmt *Block) {
1137 return new(C)SEHFinallyStmt(Loc,Block);
1138}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001139
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001140CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
1141 VarDecl *Var)
1142 : VarAndKind(Var, Kind), Loc(Loc) {
1143 switch (Kind) {
1144 case VCK_This:
1145 assert(!Var && "'this' capture cannot have a variable!");
1146 break;
1147 case VCK_ByRef:
1148 assert(Var && "capturing by reference must have a variable!");
1149 break;
1150 case VCK_ByCopy:
1151 assert(Var && "capturing by copy must have a variable!");
1152 assert(
1153 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
1154 Var->getType()
1155 ->castAs<ReferenceType>()
1156 ->getPointeeType()
1157 ->isScalarType())) &&
1158 "captures by copy are expected to have a scalar type!");
1159 break;
1160 case VCK_VLAType:
1161 assert(!Var &&
1162 "Variable-length array type capture cannot have a variable!");
1163 break;
1164 }
1165}
1166
Chandler Carruth21c90602015-12-30 03:24:14 +00001167CapturedStmt::VariableCaptureKind
1168CapturedStmt::Capture::getCaptureKind() const {
1169 return VarAndKind.getInt();
1170}
1171
1172VarDecl *CapturedStmt::Capture::getCapturedVar() const {
1173 assert((capturesVariable() || capturesVariableByCopy()) &&
1174 "No variable available for 'this' or VAT capture");
1175 return VarAndKind.getPointer();
1176}
1177
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001178CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1179 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1180
1181 // Offset of the first Capture object.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001182 unsigned FirstCaptureOffset = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001183
1184 return reinterpret_cast<Capture *>(
1185 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1186 + FirstCaptureOffset);
1187}
1188
Wei Pan17fbf6e2013-05-04 03:59:06 +00001189CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1190 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001191 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001192 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001193 RecordDecl *RD)
1194 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001195 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001196 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001197 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001198 assert(RD && "null record declaration for captured statement");
1199
1200 // Copy initialization expressions.
1201 Stmt **Stored = getStoredStmts();
1202 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1203 *Stored++ = CaptureInits[I];
1204
1205 // Copy the statement being captured.
1206 *Stored = S;
1207
1208 // Copy all Capture objects.
1209 Capture *Buffer = getStoredCaptures();
1210 std::copy(Captures.begin(), Captures.end(), Buffer);
1211}
1212
1213CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1214 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001215 CapDeclAndKind(nullptr, CR_Default) {
Craig Topper36250ad2014-05-12 05:36:57 +00001216 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001217}
1218
Craig Toppere6960e22013-08-22 05:28:54 +00001219CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001220 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001221 ArrayRef<Capture> Captures,
1222 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001223 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001224 RecordDecl *RD) {
1225 // The layout is
1226 //
1227 // -----------------------------------------------------------
1228 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1229 // ----------------^-------------------^----------------------
1230 // getStoredStmts() getStoredCaptures()
1231 //
1232 // where S is the statement being captured.
1233 //
1234 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1235
1236 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1237 if (!Captures.empty()) {
1238 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001239 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001240 Size += sizeof(Capture) * Captures.size();
1241 }
1242
1243 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001244 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001245}
1246
Craig Toppere6960e22013-08-22 05:28:54 +00001247CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001248 unsigned NumCaptures) {
1249 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1250 if (NumCaptures > 0) {
1251 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001252 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001253 Size += sizeof(Capture) * NumCaptures;
1254 }
1255
1256 void *Mem = Context.Allocate(Size);
1257 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1258}
1259
1260Stmt::child_range CapturedStmt::children() {
Simon Pilgrim2c518802017-03-30 14:13:19 +00001261 // Children are captured field initializers.
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001262 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001263}
1264
Chandler Carruth21c90602015-12-30 03:24:14 +00001265CapturedDecl *CapturedStmt::getCapturedDecl() {
1266 return CapDeclAndKind.getPointer();
1267}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001268
Chandler Carruth21c90602015-12-30 03:24:14 +00001269const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1270 return CapDeclAndKind.getPointer();
1271}
1272
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001273/// Set the outlined function declaration.
Chandler Carruth21c90602015-12-30 03:24:14 +00001274void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1275 assert(D && "null CapturedDecl");
1276 CapDeclAndKind.setPointer(D);
1277}
1278
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001279/// Retrieve the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001280CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1281 return CapDeclAndKind.getInt();
1282}
1283
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001284/// Set the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001285void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1286 CapDeclAndKind.setInt(Kind);
1287}
1288
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001289bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001290 for (const auto &I : captures()) {
Alexey Bataev2c845412017-05-15 16:26:15 +00001291 if (!I.capturesVariable() && !I.capturesVariableByCopy())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001292 continue;
Alexey Bataeve85de8f2017-09-20 20:11:31 +00001293 if (I.getCapturedVar()->getCanonicalDecl() == Var->getCanonicalDecl())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001294 return true;
1295 }
1296
1297 return false;
1298}