blob: 41a4a64f33f1ffdd8cb027ef66d2b74f3fb244d1 [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
Stephen Kelly0945c8b2018-08-14 21:33:28 +0000116 Stmt *lasts = nullptr;
John McCall4db5c3c2011-07-07 06:58:02 +0000117
Stephen Kelly0945c8b2018-08-14 21:33:28 +0000118 while (s != lasts) {
119 lasts = s;
Richard Smith520449d2015-02-05 06:15:50 +0000120
Stephen Kelly0945c8b2018-08-14 21:33:28 +0000121 if (auto *ewc = dyn_cast<ExprWithCleanups>(s))
122 s = ewc->getSubExpr();
Richard Smith520449d2015-02-05 06:15:50 +0000123
Stephen Kelly0945c8b2018-08-14 21:33:28 +0000124 if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s))
125 s = mte->GetTemporaryExpr();
126
127 if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s))
128 s = bte->getSubExpr();
129
130 if (auto *ice = dyn_cast<ImplicitCastExpr>(s))
131 s = ice->getSubExpr();
132 }
John McCall4db5c3c2011-07-07 06:58:02 +0000133
134 return s;
135}
136
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000137/// Skip no-op (attributed, compound) container stmts and skip captured
Alexander Musmana5f070a2014-10-01 06:03:56 +0000138/// stmt at the top, if \a IgnoreCaptured is true.
139Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
140 Stmt *S = this;
141 if (IgnoreCaptured)
142 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
143 S = CapS->getCapturedStmt();
144 while (true) {
145 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
146 S = AS->getSubStmt();
147 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
148 if (CS->size() != 1)
149 break;
150 S = CS->body_back();
151 } else
152 break;
153 }
154 return S;
155}
156
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000157/// Strip off all label-like statements.
Chandler Carrutha626d642011-09-10 00:02:34 +0000158///
Richard Smithc202b282012-04-14 00:33:13 +0000159/// This will strip off label statements, case statements, attributed
160/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000161const Stmt *Stmt::stripLabelLikeStatements() const {
162 const Stmt *S = this;
163 while (true) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000164 if (const auto *LS = dyn_cast<LabelStmt>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000165 S = LS->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000166 else if (const auto *SC = dyn_cast<SwitchCase>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000167 S = SC->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000168 else if (const auto *AS = dyn_cast<AttributedStmt>(S))
Richard Smithc202b282012-04-14 00:33:13 +0000169 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000170 else
171 return S;
172 }
173}
174
John McCallbd066782011-02-09 08:16:59 +0000175namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000176
John McCallbd066782011-02-09 08:16:59 +0000177 struct good {};
178 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000179
180 // These silly little functions have to be static inline to suppress
181 // unused warnings, and they have to be defined to suppress other
182 // warnings.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000183 static good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000184
185 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000186 template <class T> good implements_children(children_t T::*) {
187 return good();
188 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000189 LLVM_ATTRIBUTE_UNUSED
Eugene Zelenko7855e772018-04-03 00:11:50 +0000190 static bad implements_children(children_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000191 return bad();
192 }
John McCallbd066782011-02-09 08:16:59 +0000193
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000194 typedef SourceLocation getBeginLoc_t() const;
195 template <class T> good implements_getBeginLoc(getBeginLoc_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000196 return good();
197 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000198 LLVM_ATTRIBUTE_UNUSED
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000199 static bad implements_getBeginLoc(getBeginLoc_t Stmt::*) { return bad(); }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000200
201 typedef SourceLocation getLocEnd_t() const;
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000202 template <class T> good implements_getEndLoc(getLocEnd_t T::*) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000203 return good();
204 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000205 LLVM_ATTRIBUTE_UNUSED
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000206 static bad implements_getEndLoc(getLocEnd_t Stmt::*) { return bad(); }
John McCallbd066782011-02-09 08:16:59 +0000207
208#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000209 (void) is_good(implements_children(&type::children))
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000210#define ASSERT_IMPLEMENTS_getBeginLoc(type) \
211 (void)is_good(implements_getBeginLoc(&type::getBeginLoc))
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000212#define ASSERT_IMPLEMENTS_getEndLoc(type) \
213 (void)is_good(implements_getEndLoc(&type::getEndLoc))
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000214
215} // namespace
John McCallbd066782011-02-09 08:16:59 +0000216
217/// Check whether the various Stmt classes implement their member
218/// functions.
Eli Friedmandc41d792013-09-10 22:57:15 +0000219LLVM_ATTRIBUTE_UNUSED
John McCallbd066782011-02-09 08:16:59 +0000220static inline void check_implementations() {
221#define ABSTRACT_STMT(type)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000222#define STMT(type, base) \
223 ASSERT_IMPLEMENTS_children(type); \
224 ASSERT_IMPLEMENTS_getBeginLoc(type); \
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000225 ASSERT_IMPLEMENTS_getEndLoc(type);
John McCallbd066782011-02-09 08:16:59 +0000226#include "clang/AST/StmtNodes.inc"
227}
228
229Stmt::child_range Stmt::children() {
230 switch (getStmtClass()) {
231 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
232#define ABSTRACT_STMT(type)
233#define STMT(type, base) \
234 case Stmt::type##Class: \
235 return static_cast<type*>(this)->children();
236#include "clang/AST/StmtNodes.inc"
237 }
238 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000239}
240
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000241// Amusing macro metaprogramming hack: check whether a class provides
242// a more specific implementation of getSourceRange.
243//
244// See also Expr.cpp:getExprLoc().
245namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000246
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000247 /// This implementation is used when a class provides a custom
248 /// implementation of getSourceRange.
249 template <class S, class T>
250 SourceRange getSourceRangeImpl(const Stmt *stmt,
251 SourceRange (T::*v)() const) {
252 return static_cast<const S*>(stmt)->getSourceRange();
253 }
254
255 /// This implementation is used when a class doesn't provide a custom
256 /// implementation of getSourceRange. Overload resolution should pick it over
257 /// the implementation above because it's more specialized according to
258 /// function template partial ordering.
259 template <class S>
260 SourceRange getSourceRangeImpl(const Stmt *stmt,
261 SourceRange (Stmt::*v)() const) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000262 return SourceRange(static_cast<const S *>(stmt)->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000263 static_cast<const S *>(stmt)->getEndLoc());
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000264 }
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000265
266} // namespace
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000267
John McCallbd066782011-02-09 08:16:59 +0000268SourceRange Stmt::getSourceRange() const {
269 switch (getStmtClass()) {
270 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
271#define ABSTRACT_STMT(type)
272#define STMT(type, base) \
273 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000274 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000275#include "clang/AST/StmtNodes.inc"
276 }
277 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000278}
279
Stephen Kelly724e9e52018-08-09 20:05:03 +0000280SourceLocation Stmt::getBeginLoc() const {
281 // llvm::errs() << "getBeginLoc() for " << getStmtClassName() << "\n";
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000282 switch (getStmtClass()) {
283 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
284#define ABSTRACT_STMT(type)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000285#define STMT(type, base) \
286 case Stmt::type##Class: \
287 return static_cast<const type *>(this)->getBeginLoc();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000288#include "clang/AST/StmtNodes.inc"
289 }
290 llvm_unreachable("unknown statement kind");
291}
292
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000293SourceLocation Stmt::getEndLoc() const {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000294 switch (getStmtClass()) {
295 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
296#define ABSTRACT_STMT(type)
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000297#define STMT(type, base) \
298 case Stmt::type##Class: \
299 return static_cast<const type *>(this)->getEndLoc();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000300#include "clang/AST/StmtNodes.inc"
301 }
302 llvm_unreachable("unknown statement kind");
303}
304
George Karpenkov5eb4cc62018-09-15 02:01:47 +0000305int64_t Stmt::getID(const ASTContext &Context) const {
306 Optional<int64_t> Out = Context.getAllocator().identifyObject(this);
307 assert(Out && "Wrong allocator used");
308 assert(*Out % alignof(Stmt) == 0 && "Wrong alignment information");
309 return *Out / alignof(Stmt);
George Karpenkov5eb4cc62018-09-15 02:01:47 +0000310}
311
Benjamin Kramer07420902017-12-24 16:24:20 +0000312CompoundStmt::CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB,
313 SourceLocation RB)
Bruno Ricci41d11c02018-10-27 18:43:27 +0000314 : Stmt(CompoundStmtClass), RBraceLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000315 CompoundStmtBits.NumStmts = Stmts.size();
Benjamin Kramer07420902017-12-24 16:24:20 +0000316 setStmts(Stmts);
Bruno Ricci41d11c02018-10-27 18:43:27 +0000317 CompoundStmtBits.LBraceLoc = LB;
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000318}
319
Benjamin Kramer07420902017-12-24 16:24:20 +0000320void CompoundStmt::setStmts(ArrayRef<Stmt *> Stmts) {
Craig Topper9ee84ad2015-12-04 05:01:44 +0000321 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
322 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
Douglas Gregora9af1d12009-04-17 00:04:06 +0000323
Benjamin Kramer07420902017-12-24 16:24:20 +0000324 std::copy(Stmts.begin(), Stmts.end(), body_begin());
325}
326
327CompoundStmt *CompoundStmt::Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
328 SourceLocation LB, SourceLocation RB) {
329 void *Mem =
330 C.Allocate(totalSizeToAlloc<Stmt *>(Stmts.size()), alignof(CompoundStmt));
331 return new (Mem) CompoundStmt(Stmts, LB, RB);
332}
333
334CompoundStmt *CompoundStmt::CreateEmpty(const ASTContext &C,
335 unsigned NumStmts) {
336 void *Mem =
337 C.Allocate(totalSizeToAlloc<Stmt *>(NumStmts), alignof(CompoundStmt));
338 CompoundStmt *New = new (Mem) CompoundStmt(EmptyShell());
339 New->CompoundStmtBits.NumStmts = NumStmts;
340 return New;
Douglas Gregora9af1d12009-04-17 00:04:06 +0000341}
Steve Narofff84d11f2007-05-23 21:48:04 +0000342
Chris Lattnereefa10e2007-05-28 06:56:27 +0000343const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000344 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000345}
346
Craig Toppere6960e22013-08-22 05:28:54 +0000347AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000348 ArrayRef<const Attr*> Attrs,
349 Stmt *SubStmt) {
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000350 assert(!Attrs.empty() && "Attrs should not be empty");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000351 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(Attrs.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000352 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000353 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
354}
355
Craig Toppere6960e22013-08-22 05:28:54 +0000356AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
357 unsigned NumAttrs) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000358 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000359 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(NumAttrs),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000360 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000361 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
362}
363
Craig Topperc571c812013-08-22 06:02:26 +0000364std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000365 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000366 return gccAsmStmt->generateAsmString(C);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000367 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000368 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000369 llvm_unreachable("unknown asm statement kind!");
370}
371
372StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000373 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000374 return gccAsmStmt->getOutputConstraint(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000375 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000376 return msAsmStmt->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000377 llvm_unreachable("unknown asm statement kind!");
378}
379
380const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000381 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000382 return gccAsmStmt->getOutputExpr(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000383 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000384 return msAsmStmt->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000385 llvm_unreachable("unknown asm statement kind!");
386}
387
388StringRef AsmStmt::getInputConstraint(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000389 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000390 return gccAsmStmt->getInputConstraint(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000391 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000392 return msAsmStmt->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000393 llvm_unreachable("unknown asm statement kind!");
394}
395
396const Expr *AsmStmt::getInputExpr(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000397 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000398 return gccAsmStmt->getInputExpr(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000399 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000400 return msAsmStmt->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000401 llvm_unreachable("unknown asm statement kind!");
402}
403
404StringRef AsmStmt::getClobber(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000405 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000406 return gccAsmStmt->getClobber(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000407 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000408 return msAsmStmt->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000409 llvm_unreachable("unknown asm statement kind!");
410}
411
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000412/// getNumPlusOperands - Return the number of output operands that have a "+"
413/// constraint.
414unsigned AsmStmt::getNumPlusOperands() const {
415 unsigned Res = 0;
416 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
417 if (isOutputPlusConstraint(i))
418 ++Res;
419 return Res;
420}
421
Akira Hatanaka987f1862014-08-22 06:05:21 +0000422char GCCAsmStmt::AsmStringPiece::getModifier() const {
423 assert(isOperand() && "Only Operands can have modifiers.");
424 return isLetter(Str[0]) ? Str[0] : '\0';
425}
426
Chad Rosier6100ae12012-08-27 23:47:56 +0000427StringRef GCCAsmStmt::getClobber(unsigned i) const {
428 return getClobberStringLiteral(i)->getString();
429}
430
Chad Rosierde70e0e2012-08-25 00:11:56 +0000431Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000432 return cast<Expr>(Exprs[i]);
433}
Chris Lattner72bbf172009-03-10 04:59:06 +0000434
435/// getOutputConstraint - Return the constraint string for the specified
436/// output operand. All output constraints are known to be non-empty (either
437/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000438StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000439 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000440}
Chris Lattner72bbf172009-03-10 04:59:06 +0000441
Chad Rosierde70e0e2012-08-25 00:11:56 +0000442Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000443 return cast<Expr>(Exprs[i + NumOutputs]);
444}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000445
Chad Rosierde70e0e2012-08-25 00:11:56 +0000446void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000447 Exprs[i + NumOutputs] = E;
448}
449
Chris Lattner72bbf172009-03-10 04:59:06 +0000450/// getInputConstraint - Return the specified input constraint. Unlike output
451/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000452StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000453 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000454}
455
Craig Topperc571c812013-08-22 06:02:26 +0000456void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
457 IdentifierInfo **Names,
458 StringLiteral **Constraints,
459 Stmt **Exprs,
460 unsigned NumOutputs,
461 unsigned NumInputs,
462 StringLiteral **Clobbers,
463 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000464 this->NumOutputs = NumOutputs;
465 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000466 this->NumClobbers = NumClobbers;
467
468 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000469
Anders Carlsson98323d22010-01-30 23:19:41 +0000470 C.Deallocate(this->Names);
471 this->Names = new (C) IdentifierInfo*[NumExprs];
472 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000473
Anders Carlsson98323d22010-01-30 23:19:41 +0000474 C.Deallocate(this->Exprs);
475 this->Exprs = new (C) Stmt*[NumExprs];
476 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000477
Anders Carlsson98323d22010-01-30 23:19:41 +0000478 C.Deallocate(this->Constraints);
479 this->Constraints = new (C) StringLiteral*[NumExprs];
480 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000481
Anders Carlsson98323d22010-01-30 23:19:41 +0000482 C.Deallocate(this->Clobbers);
483 this->Clobbers = new (C) StringLiteral*[NumClobbers];
484 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000485}
486
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000487/// getNamedOperand - Given a symbolic operand reference like %[foo],
488/// translate this into a numeric value needed to reference the same operand.
489/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000490int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000491 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000492
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000493 // Check if this is an output operand.
494 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
495 if (getOutputName(i) == SymbolicName)
496 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000497 }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000499 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
500 if (getInputName(i) == SymbolicName)
501 return getNumOutputs() + NumPlusOperands + i;
502
503 // Not found.
504 return -1;
505}
506
Chris Lattner35b58362009-03-10 23:21:44 +0000507/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
508/// it into pieces. If the asm string is erroneous, emit errors and return
509/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000510unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000511 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000512 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000513 const char *StrStart = Str.begin();
514 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000515 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000516
Chris Lattner35b58362009-03-10 23:21:44 +0000517 // "Simple" inline asms have no constraints or operands, just convert the asm
518 // string to escape $'s.
519 if (isSimple()) {
520 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000521 for (; CurPtr != StrEnd; ++CurPtr) {
522 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000523 case '$':
524 Result += "$$";
525 break;
526 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000527 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000528 break;
529 }
530 }
531 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000532 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000533 }
534
535 // CurStringPiece - The current string that we are building up as we scan the
536 // asm string.
537 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000538
Douglas Gregore8bbc122011-09-02 00:18:52 +0000539 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000540
Richard Smithd18ab802016-05-16 22:52:23 +0000541 unsigned LastAsmStringToken = 0;
542 unsigned LastAsmStringOffset = 0;
543
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000544 while (true) {
Chris Lattner35b58362009-03-10 23:21:44 +0000545 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000546 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000547 if (!CurStringPiece.empty())
548 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000549 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Chris Lattnera41b8472009-03-10 23:51:40 +0000552 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000553 switch (CurChar) {
554 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000555 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
556 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
557 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000558 case '%':
559 break;
560 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000561 CurStringPiece += CurChar;
562 continue;
563 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000564
Chris Lattner35b58362009-03-10 23:21:44 +0000565 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000566 if (CurPtr == StrEnd) {
567 // % at end of string is invalid (no escape).
568 DiagOffs = CurPtr-StrStart-1;
569 return diag::err_asm_invalid_escape;
570 }
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000571 // Handle escaped char and continue looping over the asm string.
Chris Lattnera41b8472009-03-10 23:51:40 +0000572 char EscapedChar = *CurPtr++;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000573 switch (EscapedChar) {
574 default:
575 break;
576 case '%': // %% -> %
577 case '{': // %{ -> {
578 case '}': // %} -> }
579 CurStringPiece += EscapedChar;
Chris Lattner35b58362009-03-10 23:21:44 +0000580 continue;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000581 case '=': // %= -> Generate a unique ID.
Chris Lattner35b58362009-03-10 23:21:44 +0000582 CurStringPiece += "${:uid}";
583 continue;
584 }
Mike Stump11289f42009-09-09 15:08:12 +0000585
Chris Lattner35b58362009-03-10 23:21:44 +0000586 // Otherwise, we have an operand. If we have accumulated a string so far,
587 // add it to the Pieces list.
588 if (!CurStringPiece.empty()) {
589 Pieces.push_back(AsmStringPiece(CurStringPiece));
590 CurStringPiece.clear();
591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Akira Hatanaka987f1862014-08-22 06:05:21 +0000593 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
594 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
595
596 const char *Begin = CurPtr - 1; // Points to the character following '%'.
597 const char *Percent = Begin - 1; // Points to '%'.
598
Jordan Rosea7d03842013-02-08 22:30:41 +0000599 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000600 if (CurPtr == StrEnd) { // Premature end.
601 DiagOffs = CurPtr-StrStart-1;
602 return diag::err_asm_invalid_escape;
603 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000604 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000605 }
Mike Stump11289f42009-09-09 15:08:12 +0000606
Akira Hatanaka987f1862014-08-22 06:05:21 +0000607 const TargetInfo &TI = C.getTargetInfo();
608 const SourceManager &SM = C.getSourceManager();
609 const LangOptions &LO = C.getLangOpts();
610
611 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000612 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000613 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000614 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000615
Chris Lattner99d892b2009-03-11 22:52:17 +0000616 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000617 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000618 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000619
Chris Lattner14311922009-03-11 00:23:13 +0000620 unsigned NumOperands =
621 getNumOutputs() + getNumPlusOperands() + getNumInputs();
622 if (N >= NumOperands) {
623 DiagOffs = CurPtr-StrStart-1;
624 return diag::err_asm_invalid_operand_number;
625 }
626
Akira Hatanaka987f1862014-08-22 06:05:21 +0000627 // Str contains "x4" (Operand without the leading %).
628 std::string Str(Begin, CurPtr - Begin);
629
630 // (BeginLoc, EndLoc) represents the range of the operand we are currently
631 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000632 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
633 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
634 &LastAsmStringOffset);
635 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
636 CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken,
637 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000638
Benjamin Kramer3204b152015-05-29 19:42:19 +0000639 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Chris Lattner35b58362009-03-10 23:21:44 +0000640 continue;
641 }
Mike Stump11289f42009-09-09 15:08:12 +0000642
Akira Hatanaka987f1862014-08-22 06:05:21 +0000643 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000644 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000645 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000646
Chris Lattner3fa25c62009-03-11 00:06:36 +0000647 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000648 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000649 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000650 return diag::err_asm_unterminated_symbolic_operand_name;
651 if (NameEnd == CurPtr)
652 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000653
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000654 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000655
Chris Lattner35b58362009-03-10 23:21:44 +0000656 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000657 if (N == -1) {
658 // Verify that an operand with that name exists.
659 DiagOffs = CurPtr-StrStart;
660 return diag::err_asm_unknown_symbolic_operand_name;
661 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000662
663 // Str contains "x[foo]" (Operand without the leading %).
664 std::string Str(Begin, NameEnd + 1 - Begin);
665
666 // (BeginLoc, EndLoc) represents the range of the operand we are currently
667 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000668 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
669 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
670 &LastAsmStringOffset);
671 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
672 NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken,
673 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000674
Benjamin Kramer3204b152015-05-29 19:42:19 +0000675 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000676
Chris Lattner3fa25c62009-03-11 00:06:36 +0000677 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000678 continue;
679 }
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000681 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000682 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000683 }
684}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000685
686/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000687std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000688 // Analyze the asm string to decompose it into its pieces. We know that Sema
689 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000690 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000691 unsigned DiagOffs;
692 AnalyzeAsmString(Pieces, C, DiagOffs);
693
694 std::string AsmString;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000695 for (const auto &Piece : Pieces) {
696 if (Piece.isString())
697 AsmString += Piece.getString();
698 else if (Piece.getModifier() == '\0')
699 AsmString += '$' + llvm::utostr(Piece.getOperandNo());
Chad Rosier14836ba2012-08-24 17:05:45 +0000700 else
Eugene Zelenko7855e772018-04-03 00:11:50 +0000701 AsmString += "${" + llvm::utostr(Piece.getOperandNo()) + ':' +
702 Piece.getModifier() + '}';
Chad Rosier14836ba2012-08-24 17:05:45 +0000703 }
704 return AsmString;
705}
Chris Lattner35b58362009-03-10 23:21:44 +0000706
Chad Rosier3b0c2602012-08-27 20:23:31 +0000707/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000708std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000709 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000710 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000711}
712
Chad Rosierfe31e622012-08-24 00:07:09 +0000713Expr *MSAsmStmt::getOutputExpr(unsigned i) {
714 return cast<Expr>(Exprs[i]);
715}
716
717Expr *MSAsmStmt::getInputExpr(unsigned i) {
718 return cast<Expr>(Exprs[i + NumOutputs]);
719}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000720
Chad Rosierfe31e622012-08-24 00:07:09 +0000721void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
722 Exprs[i + NumOutputs] = E;
723}
724
Chris Lattner86f5e132008-01-30 05:01:46 +0000725//===----------------------------------------------------------------------===//
726// Constructors
727//===----------------------------------------------------------------------===//
728
Craig Toppere6960e22013-08-22 05:28:54 +0000729GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
730 bool issimple, bool isvolatile, unsigned numoutputs,
731 unsigned numinputs, IdentifierInfo **names,
732 StringLiteral **constraints, Expr **exprs,
733 StringLiteral *asmstr, unsigned numclobbers,
734 StringLiteral **clobbers, SourceLocation rparenloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000735 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
736 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Chad Rosier42032fa2012-08-07 23:12:23 +0000737 unsigned NumExprs = NumOutputs + NumInputs;
738
Anders Carlsson98323d22010-01-30 23:19:41 +0000739 Names = new (C) IdentifierInfo*[NumExprs];
740 std::copy(names, names + NumExprs, Names);
741
742 Exprs = new (C) Stmt*[NumExprs];
743 std::copy(exprs, exprs + NumExprs, Exprs);
744
745 Constraints = new (C) StringLiteral*[NumExprs];
746 std::copy(constraints, constraints + NumExprs, Constraints);
747
748 Clobbers = new (C) StringLiteral*[NumClobbers];
749 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000750}
751
Craig Toppere6960e22013-08-22 05:28:54 +0000752MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000753 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000754 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000755 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000756 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
757 StringRef asmstr, ArrayRef<StringRef> clobbers,
758 SourceLocation endloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000759 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
760 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
761 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
John McCallf413f5e2013-05-03 00:10:13 +0000762 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
763}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000764
Craig Toppere6960e22013-08-22 05:28:54 +0000765static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
Benjamin Kramer2ab0d882015-08-04 12:34:23 +0000766 return str.copy(C);
John McCallf413f5e2013-05-03 00:10:13 +0000767}
768
Craig Toppere6960e22013-08-22 05:28:54 +0000769void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000770 ArrayRef<Token> asmtoks,
771 ArrayRef<StringRef> constraints,
772 ArrayRef<Expr*> exprs,
773 ArrayRef<StringRef> clobbers) {
774 assert(NumAsmToks == asmtoks.size());
775 assert(NumClobbers == clobbers.size());
776
Craig Toppercaf138e2015-12-05 07:41:42 +0000777 assert(exprs.size() == NumOutputs + NumInputs);
778 assert(exprs.size() == constraints.size());
John McCallf413f5e2013-05-03 00:10:13 +0000779
780 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000781
Craig Toppercaf138e2015-12-05 07:41:42 +0000782 Exprs = new (C) Stmt*[exprs.size()];
783 std::copy(exprs.begin(), exprs.end(), Exprs);
Chad Rosierfe31e622012-08-24 00:07:09 +0000784
Craig Toppercaf138e2015-12-05 07:41:42 +0000785 AsmToks = new (C) Token[asmtoks.size()];
786 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000787
Craig Toppercaf138e2015-12-05 07:41:42 +0000788 Constraints = new (C) StringRef[exprs.size()];
789 std::transform(constraints.begin(), constraints.end(), Constraints,
790 [&](StringRef Constraint) {
791 return copyIntoContext(C, Constraint);
792 });
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000793
Chad Rosierbaf53f92012-08-10 21:36:25 +0000794 Clobbers = new (C) StringRef[NumClobbers];
Craig Toppercaf138e2015-12-05 07:41:42 +0000795 // FIXME: Avoid the allocation/copy if at all possible.
796 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
797 [&](StringRef Clobber) {
798 return copyIntoContext(C, Clobber);
799 });
Chad Rosier32503022012-06-11 20:47:18 +0000800}
801
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000802IfStmt::IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr,
803 Stmt *Init, VarDecl *Var, Expr *Cond, Stmt *Then,
804 SourceLocation EL, Stmt *Else)
805 : Stmt(IfStmtClass) {
806 bool HasElse = Else != nullptr;
807 bool HasVar = Var != nullptr;
808 bool HasInit = Init != nullptr;
809 IfStmtBits.HasElse = HasElse;
810 IfStmtBits.HasVar = HasVar;
811 IfStmtBits.HasInit = HasInit;
812
Richard Smithb130fe72016-06-23 19:16:49 +0000813 setConstexpr(IsConstexpr);
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000814
815 setCond(Cond);
816 setThen(Then);
817 if (HasElse)
818 setElse(Else);
819 if (HasVar)
820 setConditionVariable(Ctx, Var);
821 if (HasInit)
822 setInit(Init);
823
824 setIfLoc(IL);
825 if (HasElse)
826 setElseLoc(EL);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000827}
828
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000829IfStmt::IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit)
830 : Stmt(IfStmtClass, Empty) {
831 IfStmtBits.HasElse = HasElse;
832 IfStmtBits.HasVar = HasVar;
833 IfStmtBits.HasInit = HasInit;
834}
Chad Rosier42032fa2012-08-07 23:12:23 +0000835
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000836IfStmt *IfStmt::Create(const ASTContext &Ctx, SourceLocation IL,
837 bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond,
838 Stmt *Then, SourceLocation EL, Stmt *Else) {
839 bool HasElse = Else != nullptr;
840 bool HasVar = Var != nullptr;
841 bool HasInit = Init != nullptr;
842 void *Mem = Ctx.Allocate(
843 totalSizeToAlloc<Stmt *, SourceLocation>(
844 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
845 alignof(IfStmt));
846 return new (Mem)
847 IfStmt(Ctx, IL, IsConstexpr, Init, Var, Cond, Then, EL, Else);
848}
849
850IfStmt *IfStmt::CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
851 bool HasInit) {
852 void *Mem = Ctx.Allocate(
853 totalSizeToAlloc<Stmt *, SourceLocation>(
854 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
855 alignof(IfStmt));
856 return new (Mem) IfStmt(EmptyShell(), HasElse, HasVar, HasInit);
857}
858
859VarDecl *IfStmt::getConditionVariable() {
860 auto *DS = getConditionVariableDeclStmt();
861 if (!DS)
862 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000863 return cast<VarDecl>(DS->getSingleDecl());
864}
865
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000866void IfStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
867 assert(hasVarStorage() &&
868 "This if statement has no storage for a condition variable!");
869
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000870 if (!V) {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000871 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000872 return;
873 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000874
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000875 SourceRange VarRange = V->getSourceRange();
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000876 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
877 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000878}
879
Erik Pilkington5cd57172016-08-16 17:44:11 +0000880bool IfStmt::isObjCAvailabilityCheck() const {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000881 return isa<ObjCAvailabilityCheckExpr>(getCond());
Erik Pilkington5cd57172016-08-16 17:44:11 +0000882}
883
Craig Toppere6960e22013-08-22 05:28:54 +0000884ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000885 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000886 SourceLocation RP)
Bruno Ricci41d11c02018-10-27 18:43:27 +0000887 : Stmt(ForStmtClass), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000888{
889 SubExprs[INIT] = Init;
890 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000891 SubExprs[COND] = Cond;
892 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000893 SubExprs[BODY] = Body;
Bruno Ricci41d11c02018-10-27 18:43:27 +0000894 ForStmtBits.ForLoc = FL;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000895}
896
897VarDecl *ForStmt::getConditionVariable() const {
898 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000899 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000900
Eugene Zelenko7855e772018-04-03 00:11:50 +0000901 auto *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000902 return cast<VarDecl>(DS->getSingleDecl());
903}
904
Craig Toppere6960e22013-08-22 05:28:54 +0000905void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000906 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000907 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000908 return;
909 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000910
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000911 SourceRange VarRange = V->getSourceRange();
912 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
913 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000914}
915
Bruno Riccie2806f82018-10-29 16:12:37 +0000916SwitchStmt::SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
917 Expr *Cond)
918 : Stmt(SwitchStmtClass), FirstCase(nullptr) {
919 bool HasInit = Init != nullptr;
920 bool HasVar = Var != nullptr;
921 SwitchStmtBits.HasInit = HasInit;
922 SwitchStmtBits.HasVar = HasVar;
923 SwitchStmtBits.AllEnumCasesCovered = false;
924
925 setCond(Cond);
926 setBody(nullptr);
927 if (HasInit)
928 setInit(Init);
929 if (HasVar)
930 setConditionVariable(Ctx, Var);
931
932 setSwitchLoc(SourceLocation{});
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000933}
934
Bruno Riccie2806f82018-10-29 16:12:37 +0000935SwitchStmt::SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar)
936 : Stmt(SwitchStmtClass, Empty) {
937 SwitchStmtBits.HasInit = HasInit;
938 SwitchStmtBits.HasVar = HasVar;
939 SwitchStmtBits.AllEnumCasesCovered = false;
940}
Chad Rosier42032fa2012-08-07 23:12:23 +0000941
Bruno Riccie2806f82018-10-29 16:12:37 +0000942SwitchStmt *SwitchStmt::Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
943 Expr *Cond) {
944 bool HasInit = Init != nullptr;
945 bool HasVar = Var != nullptr;
946 void *Mem = Ctx.Allocate(
947 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
948 alignof(SwitchStmt));
949 return new (Mem) SwitchStmt(Ctx, Init, Var, Cond);
950}
951
952SwitchStmt *SwitchStmt::CreateEmpty(const ASTContext &Ctx, bool HasInit,
953 bool HasVar) {
954 void *Mem = Ctx.Allocate(
955 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
956 alignof(SwitchStmt));
957 return new (Mem) SwitchStmt(EmptyShell(), HasInit, HasVar);
958}
959
960VarDecl *SwitchStmt::getConditionVariable() {
961 auto *DS = getConditionVariableDeclStmt();
962 if (!DS)
963 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000964 return cast<VarDecl>(DS->getSingleDecl());
965}
966
Bruno Riccie2806f82018-10-29 16:12:37 +0000967void SwitchStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
968 assert(hasVarStorage() &&
969 "This switch statement has no storage for a condition variable!");
970
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000971 if (!V) {
Bruno Riccie2806f82018-10-29 16:12:37 +0000972 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000973 return;
974 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000975
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000976 SourceRange VarRange = V->getSourceRange();
Bruno Riccie2806f82018-10-29 16:12:37 +0000977 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
978 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000979}
980
Craig Toppere6960e22013-08-22 05:28:54 +0000981WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000982 SourceLocation WL)
Chris Lattner80d51eb2011-02-28 00:18:06 +0000983 : Stmt(WhileStmtClass) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000984 setConditionVariable(C, Var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000985 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000986 SubExprs[BODY] = body;
Bruno Ricci41d11c02018-10-27 18:43:27 +0000987 WhileStmtBits.WhileLoc = WL;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000988}
989
990VarDecl *WhileStmt::getConditionVariable() const {
991 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000992 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000993
Eugene Zelenko7855e772018-04-03 00:11:50 +0000994 auto *DS = cast<DeclStmt>(SubExprs[VAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000995 return cast<VarDecl>(DS->getSingleDecl());
996}
997
Craig Toppere6960e22013-08-22 05:28:54 +0000998void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000999 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +00001000 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001001 return;
1002 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +00001003
1004 SourceRange VarRange = V->getSourceRange();
1005 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
1006 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001007}
1008
Ted Kremenek066dd932007-08-24 21:09:09 +00001009// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001010LabelDecl *IndirectGotoStmt::getConstantTarget() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001011 if (auto *E = dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
John McCall9de91602010-10-28 08:53:48 +00001012 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +00001013 return nullptr;
John McCall9de91602010-10-28 08:53:48 +00001014}
Ted Kremenek066dd932007-08-24 21:09:09 +00001015
Ted Kremenek066dd932007-08-24 21:09:09 +00001016// ReturnStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +00001017const Expr* ReturnStmt::getRetValue() const {
1018 return cast_or_null<Expr>(RetExpr);
1019}
1020Expr* ReturnStmt::getRetValue() {
1021 return cast_or_null<Expr>(RetExpr);
Ted Kremenek066dd932007-08-24 21:09:09 +00001022}
John Wiegley1c0675e2011-04-28 01:08:34 +00001023
Bruno Ricci5b30571752018-10-28 12:30:53 +00001024// CaseStmt
1025CaseStmt *CaseStmt::Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1026 SourceLocation caseLoc, SourceLocation ellipsisLoc,
1027 SourceLocation colonLoc) {
1028 bool CaseStmtIsGNURange = rhs != nullptr;
1029 void *Mem = Ctx.Allocate(
1030 totalSizeToAlloc<Stmt *, SourceLocation>(
1031 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1032 alignof(CaseStmt));
1033 return new (Mem) CaseStmt(lhs, rhs, caseLoc, ellipsisLoc, colonLoc);
1034}
1035
1036CaseStmt *CaseStmt::CreateEmpty(const ASTContext &Ctx,
1037 bool CaseStmtIsGNURange) {
1038 void *Mem = Ctx.Allocate(
1039 totalSizeToAlloc<Stmt *, SourceLocation>(
1040 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1041 alignof(CaseStmt));
1042 return new (Mem) CaseStmt(EmptyShell(), CaseStmtIsGNURange);
1043}
1044
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001045SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001046 Stmt *Handler)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001047 : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc) {
Warren Huntf6be4cb2014-07-25 20:52:51 +00001048 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +00001049 Children[HANDLER] = Handler;
1050}
1051
Warren Huntf6be4cb2014-07-25 20:52:51 +00001052SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +00001053 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001054 Stmt *Handler) {
1055 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +00001056}
1057
1058SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1059 return dyn_cast<SEHExceptStmt>(getHandler());
1060}
1061
1062SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1063 return dyn_cast<SEHFinallyStmt>(getHandler());
1064}
1065
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001066SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block)
1067 : Stmt(SEHExceptStmtClass), Loc(Loc) {
Pavel Labath515f4db2013-09-03 14:41:16 +00001068 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +00001069 Children[BLOCK] = Block;
1070}
1071
Craig Toppere6960e22013-08-22 05:28:54 +00001072SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
1073 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +00001074 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1075}
1076
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001077SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, Stmt *Block)
1078 : Stmt(SEHFinallyStmtClass), Loc(Loc), Block(Block) {}
John Wiegley1c0675e2011-04-28 01:08:34 +00001079
Craig Toppere6960e22013-08-22 05:28:54 +00001080SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +00001081 Stmt *Block) {
1082 return new(C)SEHFinallyStmt(Loc,Block);
1083}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001084
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001085CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
1086 VarDecl *Var)
1087 : VarAndKind(Var, Kind), Loc(Loc) {
1088 switch (Kind) {
1089 case VCK_This:
1090 assert(!Var && "'this' capture cannot have a variable!");
1091 break;
1092 case VCK_ByRef:
1093 assert(Var && "capturing by reference must have a variable!");
1094 break;
1095 case VCK_ByCopy:
1096 assert(Var && "capturing by copy must have a variable!");
1097 assert(
1098 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
1099 Var->getType()
1100 ->castAs<ReferenceType>()
1101 ->getPointeeType()
1102 ->isScalarType())) &&
1103 "captures by copy are expected to have a scalar type!");
1104 break;
1105 case VCK_VLAType:
1106 assert(!Var &&
1107 "Variable-length array type capture cannot have a variable!");
1108 break;
1109 }
1110}
1111
Chandler Carruth21c90602015-12-30 03:24:14 +00001112CapturedStmt::VariableCaptureKind
1113CapturedStmt::Capture::getCaptureKind() const {
1114 return VarAndKind.getInt();
1115}
1116
1117VarDecl *CapturedStmt::Capture::getCapturedVar() const {
1118 assert((capturesVariable() || capturesVariableByCopy()) &&
1119 "No variable available for 'this' or VAT capture");
1120 return VarAndKind.getPointer();
1121}
1122
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001123CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1124 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1125
1126 // Offset of the first Capture object.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001127 unsigned FirstCaptureOffset = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001128
1129 return reinterpret_cast<Capture *>(
1130 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1131 + FirstCaptureOffset);
1132}
1133
Wei Pan17fbf6e2013-05-04 03:59:06 +00001134CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1135 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001136 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001137 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001138 RecordDecl *RD)
1139 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001140 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001141 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001142 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001143 assert(RD && "null record declaration for captured statement");
1144
1145 // Copy initialization expressions.
1146 Stmt **Stored = getStoredStmts();
1147 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1148 *Stored++ = CaptureInits[I];
1149
1150 // Copy the statement being captured.
1151 *Stored = S;
1152
1153 // Copy all Capture objects.
1154 Capture *Buffer = getStoredCaptures();
1155 std::copy(Captures.begin(), Captures.end(), Buffer);
1156}
1157
1158CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1159 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001160 CapDeclAndKind(nullptr, CR_Default) {
Craig Topper36250ad2014-05-12 05:36:57 +00001161 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001162}
1163
Craig Toppere6960e22013-08-22 05:28:54 +00001164CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001165 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001166 ArrayRef<Capture> Captures,
1167 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001168 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001169 RecordDecl *RD) {
1170 // The layout is
1171 //
1172 // -----------------------------------------------------------
1173 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1174 // ----------------^-------------------^----------------------
1175 // getStoredStmts() getStoredCaptures()
1176 //
1177 // where S is the statement being captured.
1178 //
1179 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1180
1181 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1182 if (!Captures.empty()) {
1183 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001184 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001185 Size += sizeof(Capture) * Captures.size();
1186 }
1187
1188 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001189 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001190}
1191
Craig Toppere6960e22013-08-22 05:28:54 +00001192CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001193 unsigned NumCaptures) {
1194 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1195 if (NumCaptures > 0) {
1196 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001197 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001198 Size += sizeof(Capture) * NumCaptures;
1199 }
1200
1201 void *Mem = Context.Allocate(Size);
1202 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1203}
1204
1205Stmt::child_range CapturedStmt::children() {
Simon Pilgrim2c518802017-03-30 14:13:19 +00001206 // Children are captured field initializers.
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001207 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001208}
1209
Chandler Carruth21c90602015-12-30 03:24:14 +00001210CapturedDecl *CapturedStmt::getCapturedDecl() {
1211 return CapDeclAndKind.getPointer();
1212}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001213
Chandler Carruth21c90602015-12-30 03:24:14 +00001214const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1215 return CapDeclAndKind.getPointer();
1216}
1217
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001218/// Set the outlined function declaration.
Chandler Carruth21c90602015-12-30 03:24:14 +00001219void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1220 assert(D && "null CapturedDecl");
1221 CapDeclAndKind.setPointer(D);
1222}
1223
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001224/// Retrieve the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001225CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1226 return CapDeclAndKind.getInt();
1227}
1228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001229/// Set the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001230void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1231 CapDeclAndKind.setInt(Kind);
1232}
1233
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001234bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001235 for (const auto &I : captures()) {
Alexey Bataev2c845412017-05-15 16:26:15 +00001236 if (!I.capturesVariable() && !I.capturesVariableByCopy())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001237 continue;
Alexey Bataeve85de8f2017-09-20 20:11:31 +00001238 if (I.getCapturedVar()->getCanonicalDecl() == Var->getCanonicalDecl())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001239 return true;
1240 }
1241
1242 return false;
1243}