blob: 68a5a2d6ab0edd0dafd4718a4d091e7fca9efab5 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerf42cce72006-10-25 04:09:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Stmt class and statement subclasses.
10//
11//===----------------------------------------------------------------------===//
12
Eugene Zelenko7855e772018-04-03 00:11:50 +000013#include "clang/AST/Stmt.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTDiagnostic.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclGroup.h"
Eugene Zelenko7855e772018-04-03 00:11:50 +000018#include "clang/AST/Expr.h"
Chris Lattner29375652006-12-04 18:06:35 +000019#include "clang/AST/ExprCXX.h"
Steve Naroff021ca182008-05-29 21:12:08 +000020#include "clang/AST/ExprObjC.h"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000021#include "clang/AST/ExprOpenMP.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtObjC.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000024#include "clang/AST/StmtOpenMP.h"
Sebastian Redl54c04d42008-12-22 19:15:10 +000025#include "clang/AST/Type.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000026#include "clang/Basic/CharInfo.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000027#include "clang/Basic/LLVM.h"
28#include "clang/Basic/SourceLocation.h"
Chris Lattner1a8f3942010-04-23 16:29:58 +000029#include "clang/Basic/TargetInfo.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000030#include "clang/Lex/Token.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000031#include "llvm/ADT/SmallVector.h"
Chad Rosier14836ba2012-08-24 17:05:45 +000032#include "llvm/ADT/StringExtras.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000033#include "llvm/ADT/StringRef.h"
34#include "llvm/Support/Casting.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/MathExtras.h"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000038#include "llvm/Support/raw_ostream.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000039#include <algorithm>
40#include <cassert>
41#include <cstring>
42#include <string>
43#include <utility>
44
Chris Lattnerf42cce72006-10-25 04:09:21 +000045using namespace clang;
46
Steve Narofff84d11f2007-05-23 21:48:04 +000047static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000048 const char *Name;
49 unsigned Counter;
50 unsigned Size;
Alexis Hunt656bb312010-05-05 15:24:00 +000051} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner4d15a0d2007-08-25 01:42:24 +000052
53static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
54 static bool Initialized = false;
55 if (Initialized)
56 return StmtClassInfo[E];
57
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +000058 // Initialize the table on the first use.
Chris Lattner4d15a0d2007-08-25 01:42:24 +000059 Initialized = true;
Alexis Huntabb2ac82010-05-18 06:22:21 +000060#define ABSTRACT_STMT(STMT)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000061#define STMT(CLASS, PARENT) \
62 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
63 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Alexis Hunt656bb312010-05-05 15:24:00 +000064#include "clang/AST/StmtNodes.inc"
Nico Weberde565e32008-08-05 23:15:29 +000065
Chris Lattner4d15a0d2007-08-25 01:42:24 +000066 return StmtClassInfo[E];
67}
68
Craig Topper37932912013-08-18 10:09:15 +000069void *Stmt::operator new(size_t bytes, const ASTContext& C,
Craig Topper5a050012013-08-18 17:45:38 +000070 unsigned alignment) {
Benjamin Kramerea70eb32012-12-01 15:09:41 +000071 return ::operator new(bytes, C, alignment);
72}
73
Steve Narofff1e53692007-03-23 22:27:02 +000074const char *Stmt::getStmtClassName() const {
John McCall925b16622010-10-26 08:39:16 +000075 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000076}
Steve Narofff84d11f2007-05-23 21:48:04 +000077
Bruno Ricci65034b82018-12-04 16:04:19 +000078// Check that no statement / expression class is polymorphic. LLVM style RTTI
79// should be used instead. If absolutely needed an exception can still be added
80// here by defining the appropriate macro (but please don't do this).
81#define STMT(CLASS, PARENT) \
82 static_assert(!std::is_polymorphic<CLASS>::value, \
83 #CLASS " should not be polymorphic!");
84#include "clang/AST/StmtNodes.inc"
85
Steve Narofff84d11f2007-05-23 21:48:04 +000086void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000087 // Ensure the table is primed.
88 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000089
Steve Narofff84d11f2007-05-23 21:48:04 +000090 unsigned sum = 0;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000091 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Alexis Hunt656bb312010-05-05 15:24:00 +000092 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000093 if (StmtClassInfo[i].Name == nullptr) continue;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000094 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000095 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000096 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000097 sum = 0;
Alexis Hunt656bb312010-05-05 15:24:00 +000098 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000099 if (StmtClassInfo[i].Name == nullptr) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +0000100 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000101 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
102 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
103 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
104 << " bytes)\n";
Chris Lattner4d15a0d2007-08-25 01:42:24 +0000105 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +0000106 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000107
108 llvm::errs() << "Total bytes = " << sum << "\n";
Steve Narofff84d11f2007-05-23 21:48:04 +0000109}
110
111void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +0000112 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +0000113}
114
Daniel Dunbar62905572012-03-05 21:42:49 +0000115bool Stmt::StatisticsEnabled = false;
116void Stmt::EnableStatistics() {
117 StatisticsEnabled = true;
Steve Narofff84d11f2007-05-23 21:48:04 +0000118}
119
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000120/// Skip no-op (attributed, compound) container stmts and skip captured
Alexander Musmana5f070a2014-10-01 06:03:56 +0000121/// stmt at the top, if \a IgnoreCaptured is true.
122Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
123 Stmt *S = this;
124 if (IgnoreCaptured)
125 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
126 S = CapS->getCapturedStmt();
127 while (true) {
128 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
129 S = AS->getSubStmt();
130 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
131 if (CS->size() != 1)
132 break;
133 S = CS->body_back();
134 } else
135 break;
136 }
137 return S;
138}
139
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000140/// Strip off all label-like statements.
Chandler Carrutha626d642011-09-10 00:02:34 +0000141///
Richard Smithc202b282012-04-14 00:33:13 +0000142/// This will strip off label statements, case statements, attributed
143/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000144const Stmt *Stmt::stripLabelLikeStatements() const {
145 const Stmt *S = this;
146 while (true) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000147 if (const auto *LS = dyn_cast<LabelStmt>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000148 S = LS->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000149 else if (const auto *SC = dyn_cast<SwitchCase>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000150 S = SC->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000151 else if (const auto *AS = dyn_cast<AttributedStmt>(S))
Richard Smithc202b282012-04-14 00:33:13 +0000152 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000153 else
154 return S;
155 }
156}
157
John McCallbd066782011-02-09 08:16:59 +0000158namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000159
John McCallbd066782011-02-09 08:16:59 +0000160 struct good {};
161 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000162
163 // These silly little functions have to be static inline to suppress
164 // unused warnings, and they have to be defined to suppress other
165 // warnings.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000166 static good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000167
168 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000169 template <class T> good implements_children(children_t T::*) {
170 return good();
171 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000172 LLVM_ATTRIBUTE_UNUSED
Eugene Zelenko7855e772018-04-03 00:11:50 +0000173 static bad implements_children(children_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000174 return bad();
175 }
John McCallbd066782011-02-09 08:16:59 +0000176
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000177 typedef SourceLocation getBeginLoc_t() const;
178 template <class T> good implements_getBeginLoc(getBeginLoc_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000179 return good();
180 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000181 LLVM_ATTRIBUTE_UNUSED
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000182 static bad implements_getBeginLoc(getBeginLoc_t Stmt::*) { return bad(); }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000183
184 typedef SourceLocation getLocEnd_t() const;
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000185 template <class T> good implements_getEndLoc(getLocEnd_t T::*) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000186 return good();
187 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000188 LLVM_ATTRIBUTE_UNUSED
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000189 static bad implements_getEndLoc(getLocEnd_t Stmt::*) { return bad(); }
John McCallbd066782011-02-09 08:16:59 +0000190
191#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000192 (void) is_good(implements_children(&type::children))
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000193#define ASSERT_IMPLEMENTS_getBeginLoc(type) \
194 (void)is_good(implements_getBeginLoc(&type::getBeginLoc))
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000195#define ASSERT_IMPLEMENTS_getEndLoc(type) \
196 (void)is_good(implements_getEndLoc(&type::getEndLoc))
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000197
198} // namespace
John McCallbd066782011-02-09 08:16:59 +0000199
200/// Check whether the various Stmt classes implement their member
201/// functions.
Eli Friedmandc41d792013-09-10 22:57:15 +0000202LLVM_ATTRIBUTE_UNUSED
John McCallbd066782011-02-09 08:16:59 +0000203static inline void check_implementations() {
204#define ABSTRACT_STMT(type)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000205#define STMT(type, base) \
206 ASSERT_IMPLEMENTS_children(type); \
207 ASSERT_IMPLEMENTS_getBeginLoc(type); \
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000208 ASSERT_IMPLEMENTS_getEndLoc(type);
John McCallbd066782011-02-09 08:16:59 +0000209#include "clang/AST/StmtNodes.inc"
210}
211
212Stmt::child_range Stmt::children() {
213 switch (getStmtClass()) {
214 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
215#define ABSTRACT_STMT(type)
216#define STMT(type, base) \
217 case Stmt::type##Class: \
218 return static_cast<type*>(this)->children();
219#include "clang/AST/StmtNodes.inc"
220 }
221 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000222}
223
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000224// Amusing macro metaprogramming hack: check whether a class provides
225// a more specific implementation of getSourceRange.
226//
227// See also Expr.cpp:getExprLoc().
228namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000229
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000230 /// This implementation is used when a class provides a custom
231 /// implementation of getSourceRange.
232 template <class S, class T>
233 SourceRange getSourceRangeImpl(const Stmt *stmt,
234 SourceRange (T::*v)() const) {
235 return static_cast<const S*>(stmt)->getSourceRange();
236 }
237
238 /// This implementation is used when a class doesn't provide a custom
239 /// implementation of getSourceRange. Overload resolution should pick it over
240 /// the implementation above because it's more specialized according to
241 /// function template partial ordering.
242 template <class S>
243 SourceRange getSourceRangeImpl(const Stmt *stmt,
244 SourceRange (Stmt::*v)() const) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000245 return SourceRange(static_cast<const S *>(stmt)->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000246 static_cast<const S *>(stmt)->getEndLoc());
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000247 }
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000248
249} // namespace
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000250
John McCallbd066782011-02-09 08:16:59 +0000251SourceRange Stmt::getSourceRange() const {
252 switch (getStmtClass()) {
253 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
254#define ABSTRACT_STMT(type)
255#define STMT(type, base) \
256 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000257 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000258#include "clang/AST/StmtNodes.inc"
259 }
260 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000261}
262
Stephen Kelly724e9e52018-08-09 20:05:03 +0000263SourceLocation Stmt::getBeginLoc() const {
264 // llvm::errs() << "getBeginLoc() for " << getStmtClassName() << "\n";
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000265 switch (getStmtClass()) {
266 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
267#define ABSTRACT_STMT(type)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000268#define STMT(type, base) \
269 case Stmt::type##Class: \
270 return static_cast<const type *>(this)->getBeginLoc();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000271#include "clang/AST/StmtNodes.inc"
272 }
273 llvm_unreachable("unknown statement kind");
274}
275
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000276SourceLocation Stmt::getEndLoc() const {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000277 switch (getStmtClass()) {
278 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
279#define ABSTRACT_STMT(type)
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000280#define STMT(type, base) \
281 case Stmt::type##Class: \
282 return static_cast<const type *>(this)->getEndLoc();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000283#include "clang/AST/StmtNodes.inc"
284 }
285 llvm_unreachable("unknown statement kind");
286}
287
George Karpenkov5eb4cc62018-09-15 02:01:47 +0000288int64_t Stmt::getID(const ASTContext &Context) const {
Artem Dergachev057647d2018-12-03 22:19:05 +0000289 return Context.getAllocator().identifyKnownAlignedObject<Stmt>(this);
George Karpenkov5eb4cc62018-09-15 02:01:47 +0000290}
291
Benjamin Kramer07420902017-12-24 16:24:20 +0000292CompoundStmt::CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB,
293 SourceLocation RB)
Bruno Ricci41d11c02018-10-27 18:43:27 +0000294 : Stmt(CompoundStmtClass), RBraceLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000295 CompoundStmtBits.NumStmts = Stmts.size();
Benjamin Kramer07420902017-12-24 16:24:20 +0000296 setStmts(Stmts);
Bruno Ricci41d11c02018-10-27 18:43:27 +0000297 CompoundStmtBits.LBraceLoc = LB;
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000298}
299
Benjamin Kramer07420902017-12-24 16:24:20 +0000300void CompoundStmt::setStmts(ArrayRef<Stmt *> Stmts) {
Craig Topper9ee84ad2015-12-04 05:01:44 +0000301 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
302 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
Douglas Gregora9af1d12009-04-17 00:04:06 +0000303
Benjamin Kramer07420902017-12-24 16:24:20 +0000304 std::copy(Stmts.begin(), Stmts.end(), body_begin());
305}
306
307CompoundStmt *CompoundStmt::Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
308 SourceLocation LB, SourceLocation RB) {
309 void *Mem =
310 C.Allocate(totalSizeToAlloc<Stmt *>(Stmts.size()), alignof(CompoundStmt));
311 return new (Mem) CompoundStmt(Stmts, LB, RB);
312}
313
314CompoundStmt *CompoundStmt::CreateEmpty(const ASTContext &C,
315 unsigned NumStmts) {
316 void *Mem =
317 C.Allocate(totalSizeToAlloc<Stmt *>(NumStmts), alignof(CompoundStmt));
318 CompoundStmt *New = new (Mem) CompoundStmt(EmptyShell());
319 New->CompoundStmtBits.NumStmts = NumStmts;
320 return New;
Douglas Gregora9af1d12009-04-17 00:04:06 +0000321}
Steve Narofff84d11f2007-05-23 21:48:04 +0000322
Richard Smitha6e8d5e2019-02-15 00:27:53 +0000323const Expr *ValueStmt::getExprStmt() const {
324 const Stmt *S = this;
325 do {
326 if (const auto *E = dyn_cast<Expr>(S))
327 return E;
328
329 if (const auto *LS = dyn_cast<LabelStmt>(S))
330 S = LS->getSubStmt();
331 else if (const auto *AS = dyn_cast<AttributedStmt>(S))
332 S = AS->getSubStmt();
333 else
334 llvm_unreachable("unknown kind of ValueStmt");
335 } while (isa<ValueStmt>(S));
336
337 return nullptr;
338}
339
Chris Lattnereefa10e2007-05-28 06:56:27 +0000340const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000341 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000342}
343
Craig Toppere6960e22013-08-22 05:28:54 +0000344AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000345 ArrayRef<const Attr*> Attrs,
346 Stmt *SubStmt) {
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000347 assert(!Attrs.empty() && "Attrs should not be empty");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000348 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(Attrs.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000349 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000350 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
351}
352
Craig Toppere6960e22013-08-22 05:28:54 +0000353AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
354 unsigned NumAttrs) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000355 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000356 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(NumAttrs),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000357 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000358 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
359}
360
Craig Topperc571c812013-08-22 06:02:26 +0000361std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000362 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000363 return gccAsmStmt->generateAsmString(C);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000364 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000365 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000366 llvm_unreachable("unknown asm statement kind!");
367}
368
369StringRef AsmStmt::getOutputConstraint(unsigned i) 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->getOutputConstraint(i);
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->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000374 llvm_unreachable("unknown asm statement kind!");
375}
376
377const Expr *AsmStmt::getOutputExpr(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->getOutputExpr(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->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000382 llvm_unreachable("unknown asm statement kind!");
383}
384
385StringRef AsmStmt::getInputConstraint(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->getInputConstraint(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->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000390 llvm_unreachable("unknown asm statement kind!");
391}
392
393const Expr *AsmStmt::getInputExpr(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->getInputExpr(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->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000398 llvm_unreachable("unknown asm statement kind!");
399}
400
401StringRef AsmStmt::getClobber(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->getClobber(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->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000406 llvm_unreachable("unknown asm statement kind!");
407}
408
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000409/// getNumPlusOperands - Return the number of output operands that have a "+"
410/// constraint.
411unsigned AsmStmt::getNumPlusOperands() const {
412 unsigned Res = 0;
413 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
414 if (isOutputPlusConstraint(i))
415 ++Res;
416 return Res;
417}
418
Akira Hatanaka987f1862014-08-22 06:05:21 +0000419char GCCAsmStmt::AsmStringPiece::getModifier() const {
420 assert(isOperand() && "Only Operands can have modifiers.");
421 return isLetter(Str[0]) ? Str[0] : '\0';
422}
423
Chad Rosier6100ae12012-08-27 23:47:56 +0000424StringRef GCCAsmStmt::getClobber(unsigned i) const {
425 return getClobberStringLiteral(i)->getString();
426}
427
Chad Rosierde70e0e2012-08-25 00:11:56 +0000428Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000429 return cast<Expr>(Exprs[i]);
430}
Chris Lattner72bbf172009-03-10 04:59:06 +0000431
432/// getOutputConstraint - Return the constraint string for the specified
433/// output operand. All output constraints are known to be non-empty (either
434/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000435StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000436 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000437}
Chris Lattner72bbf172009-03-10 04:59:06 +0000438
Chad Rosierde70e0e2012-08-25 00:11:56 +0000439Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000440 return cast<Expr>(Exprs[i + NumOutputs]);
441}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000442
Chad Rosierde70e0e2012-08-25 00:11:56 +0000443void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000444 Exprs[i + NumOutputs] = E;
445}
446
Chris Lattner72bbf172009-03-10 04:59:06 +0000447/// getInputConstraint - Return the specified input constraint. Unlike output
448/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000449StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000450 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000451}
452
Craig Topperc571c812013-08-22 06:02:26 +0000453void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
454 IdentifierInfo **Names,
455 StringLiteral **Constraints,
456 Stmt **Exprs,
457 unsigned NumOutputs,
458 unsigned NumInputs,
459 StringLiteral **Clobbers,
460 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000461 this->NumOutputs = NumOutputs;
462 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000463 this->NumClobbers = NumClobbers;
464
Erich Keaned0f34fd2019-05-30 15:38:02 +0000465 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000466
Anders Carlsson98323d22010-01-30 23:19:41 +0000467 C.Deallocate(this->Names);
468 this->Names = new (C) IdentifierInfo*[NumExprs];
469 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000470
Anders Carlsson98323d22010-01-30 23:19:41 +0000471 C.Deallocate(this->Exprs);
472 this->Exprs = new (C) Stmt*[NumExprs];
473 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000474
Benjamin Kramer851f57e2019-05-30 07:21:08 +0000475 unsigned NumConstraints = NumOutputs + NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000476 C.Deallocate(this->Constraints);
Benjamin Kramer851f57e2019-05-30 07:21:08 +0000477 this->Constraints = new (C) StringLiteral*[NumConstraints];
478 std::copy(Constraints, Constraints + NumConstraints, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000479
Anders Carlsson98323d22010-01-30 23:19:41 +0000480 C.Deallocate(this->Clobbers);
481 this->Clobbers = new (C) StringLiteral*[NumClobbers];
482 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000483}
484
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000485/// getNamedOperand - Given a symbolic operand reference like %[foo],
486/// translate this into a numeric value needed to reference the same operand.
487/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000488int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000489 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000490
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000491 // Check if this is an output operand.
492 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
493 if (getOutputName(i) == SymbolicName)
494 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000497 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
498 if (getInputName(i) == SymbolicName)
499 return getNumOutputs() + NumPlusOperands + i;
500
501 // Not found.
502 return -1;
503}
504
Chris Lattner35b58362009-03-10 23:21:44 +0000505/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
506/// it into pieces. If the asm string is erroneous, emit errors and return
507/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000508unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000509 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000510 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000511 const char *StrStart = Str.begin();
512 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000513 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000514
Chris Lattner35b58362009-03-10 23:21:44 +0000515 // "Simple" inline asms have no constraints or operands, just convert the asm
516 // string to escape $'s.
517 if (isSimple()) {
518 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000519 for (; CurPtr != StrEnd; ++CurPtr) {
520 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000521 case '$':
522 Result += "$$";
523 break;
524 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000525 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000526 break;
527 }
528 }
529 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000530 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000531 }
532
533 // CurStringPiece - The current string that we are building up as we scan the
534 // asm string.
535 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Douglas Gregore8bbc122011-09-02 00:18:52 +0000537 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000538
Richard Smithd18ab802016-05-16 22:52:23 +0000539 unsigned LastAsmStringToken = 0;
540 unsigned LastAsmStringOffset = 0;
541
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000542 while (true) {
Chris Lattner35b58362009-03-10 23:21:44 +0000543 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000544 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000545 if (!CurStringPiece.empty())
546 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000547 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Chris Lattnera41b8472009-03-10 23:51:40 +0000550 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000551 switch (CurChar) {
552 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000553 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
554 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
555 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000556 case '%':
557 break;
558 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000559 CurStringPiece += CurChar;
560 continue;
561 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000562
Chris Lattner35b58362009-03-10 23:21:44 +0000563 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000564 if (CurPtr == StrEnd) {
565 // % at end of string is invalid (no escape).
566 DiagOffs = CurPtr-StrStart-1;
567 return diag::err_asm_invalid_escape;
568 }
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000569 // Handle escaped char and continue looping over the asm string.
Chris Lattnera41b8472009-03-10 23:51:40 +0000570 char EscapedChar = *CurPtr++;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000571 switch (EscapedChar) {
572 default:
573 break;
574 case '%': // %% -> %
575 case '{': // %{ -> {
576 case '}': // %} -> }
577 CurStringPiece += EscapedChar;
Chris Lattner35b58362009-03-10 23:21:44 +0000578 continue;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000579 case '=': // %= -> Generate a unique ID.
Chris Lattner35b58362009-03-10 23:21:44 +0000580 CurStringPiece += "${:uid}";
581 continue;
582 }
Mike Stump11289f42009-09-09 15:08:12 +0000583
Chris Lattner35b58362009-03-10 23:21:44 +0000584 // Otherwise, we have an operand. If we have accumulated a string so far,
585 // add it to the Pieces list.
586 if (!CurStringPiece.empty()) {
587 Pieces.push_back(AsmStringPiece(CurStringPiece));
588 CurStringPiece.clear();
589 }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Akira Hatanaka987f1862014-08-22 06:05:21 +0000591 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
592 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
593
594 const char *Begin = CurPtr - 1; // Points to the character following '%'.
595 const char *Percent = Begin - 1; // Points to '%'.
596
Jordan Rosea7d03842013-02-08 22:30:41 +0000597 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000598 if (CurPtr == StrEnd) { // Premature end.
599 DiagOffs = CurPtr-StrStart-1;
600 return diag::err_asm_invalid_escape;
601 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000602 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000603 }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Akira Hatanaka987f1862014-08-22 06:05:21 +0000605 const TargetInfo &TI = C.getTargetInfo();
606 const SourceManager &SM = C.getSourceManager();
607 const LangOptions &LO = C.getLangOpts();
608
609 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000610 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000611 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000612 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000613
Chris Lattner99d892b2009-03-11 22:52:17 +0000614 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000615 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000616 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000617
Erich Keaned0f34fd2019-05-30 15:38:02 +0000618 unsigned NumOperands =
619 getNumOutputs() + getNumPlusOperands() + getNumInputs();
Chris Lattner14311922009-03-11 00:23:13 +0000620 if (N >= NumOperands) {
621 DiagOffs = CurPtr-StrStart-1;
622 return diag::err_asm_invalid_operand_number;
623 }
624
Akira Hatanaka987f1862014-08-22 06:05:21 +0000625 // Str contains "x4" (Operand without the leading %).
626 std::string Str(Begin, CurPtr - Begin);
627
628 // (BeginLoc, EndLoc) represents the range of the operand we are currently
629 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000630 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
631 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
632 &LastAsmStringOffset);
633 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
634 CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken,
635 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000636
Benjamin Kramer3204b152015-05-29 19:42:19 +0000637 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Chris Lattner35b58362009-03-10 23:21:44 +0000638 continue;
639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Akira Hatanaka987f1862014-08-22 06:05:21 +0000641 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000642 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000643 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000644
Chris Lattner3fa25c62009-03-11 00:06:36 +0000645 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000646 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000647 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000648 return diag::err_asm_unterminated_symbolic_operand_name;
649 if (NameEnd == CurPtr)
650 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000651
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000652 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000653
Chris Lattner35b58362009-03-10 23:21:44 +0000654 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000655 if (N == -1) {
656 // Verify that an operand with that name exists.
657 DiagOffs = CurPtr-StrStart;
658 return diag::err_asm_unknown_symbolic_operand_name;
659 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000660
661 // Str contains "x[foo]" (Operand without the leading %).
662 std::string Str(Begin, NameEnd + 1 - Begin);
663
664 // (BeginLoc, EndLoc) represents the range of the operand we are currently
665 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000666 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
667 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
668 &LastAsmStringOffset);
669 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
670 NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken,
671 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000672
Benjamin Kramer3204b152015-05-29 19:42:19 +0000673 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner3fa25c62009-03-11 00:06:36 +0000675 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000676 continue;
677 }
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000679 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000680 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000681 }
682}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000683
684/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000685std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000686 // Analyze the asm string to decompose it into its pieces. We know that Sema
687 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000688 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000689 unsigned DiagOffs;
690 AnalyzeAsmString(Pieces, C, DiagOffs);
691
692 std::string AsmString;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000693 for (const auto &Piece : Pieces) {
694 if (Piece.isString())
695 AsmString += Piece.getString();
696 else if (Piece.getModifier() == '\0')
697 AsmString += '$' + llvm::utostr(Piece.getOperandNo());
Chad Rosier14836ba2012-08-24 17:05:45 +0000698 else
Eugene Zelenko7855e772018-04-03 00:11:50 +0000699 AsmString += "${" + llvm::utostr(Piece.getOperandNo()) + ':' +
700 Piece.getModifier() + '}';
Chad Rosier14836ba2012-08-24 17:05:45 +0000701 }
702 return AsmString;
703}
Chris Lattner35b58362009-03-10 23:21:44 +0000704
Chad Rosier3b0c2602012-08-27 20:23:31 +0000705/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000706std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000707 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000708 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000709}
710
Chad Rosierfe31e622012-08-24 00:07:09 +0000711Expr *MSAsmStmt::getOutputExpr(unsigned i) {
712 return cast<Expr>(Exprs[i]);
713}
714
715Expr *MSAsmStmt::getInputExpr(unsigned i) {
716 return cast<Expr>(Exprs[i + NumOutputs]);
717}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000718
Chad Rosierfe31e622012-08-24 00:07:09 +0000719void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
720 Exprs[i + NumOutputs] = E;
721}
722
Chris Lattner86f5e132008-01-30 05:01:46 +0000723//===----------------------------------------------------------------------===//
724// Constructors
725//===----------------------------------------------------------------------===//
726
Craig Toppere6960e22013-08-22 05:28:54 +0000727GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
728 bool issimple, bool isvolatile, unsigned numoutputs,
729 unsigned numinputs, IdentifierInfo **names,
730 StringLiteral **constraints, Expr **exprs,
731 StringLiteral *asmstr, unsigned numclobbers,
Erich Keaned0f34fd2019-05-30 15:38:02 +0000732 StringLiteral **clobbers, SourceLocation rparenloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000733 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
Erich Keaned0f34fd2019-05-30 15:38:02 +0000734 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
735 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000736
Anders Carlsson98323d22010-01-30 23:19:41 +0000737 Names = new (C) IdentifierInfo*[NumExprs];
738 std::copy(names, names + NumExprs, Names);
739
740 Exprs = new (C) Stmt*[NumExprs];
741 std::copy(exprs, exprs + NumExprs, Exprs);
742
Benjamin Kramer851f57e2019-05-30 07:21:08 +0000743 unsigned NumConstraints = NumOutputs + NumInputs;
744 Constraints = new (C) StringLiteral*[NumConstraints];
745 std::copy(constraints, constraints + NumConstraints, Constraints);
Anders Carlsson98323d22010-01-30 23:19:41 +0000746
747 Clobbers = new (C) StringLiteral*[NumClobbers];
748 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000749}
750
Craig Toppere6960e22013-08-22 05:28:54 +0000751MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000752 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000753 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000754 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000755 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
756 StringRef asmstr, ArrayRef<StringRef> clobbers,
757 SourceLocation endloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000758 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
759 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
760 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
John McCallf413f5e2013-05-03 00:10:13 +0000761 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
762}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000763
Craig Toppere6960e22013-08-22 05:28:54 +0000764static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
Benjamin Kramer2ab0d882015-08-04 12:34:23 +0000765 return str.copy(C);
John McCallf413f5e2013-05-03 00:10:13 +0000766}
767
Craig Toppere6960e22013-08-22 05:28:54 +0000768void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000769 ArrayRef<Token> asmtoks,
770 ArrayRef<StringRef> constraints,
771 ArrayRef<Expr*> exprs,
772 ArrayRef<StringRef> clobbers) {
773 assert(NumAsmToks == asmtoks.size());
774 assert(NumClobbers == clobbers.size());
775
Craig Toppercaf138e2015-12-05 07:41:42 +0000776 assert(exprs.size() == NumOutputs + NumInputs);
777 assert(exprs.size() == constraints.size());
John McCallf413f5e2013-05-03 00:10:13 +0000778
779 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000780
Craig Toppercaf138e2015-12-05 07:41:42 +0000781 Exprs = new (C) Stmt*[exprs.size()];
782 std::copy(exprs.begin(), exprs.end(), Exprs);
Chad Rosierfe31e622012-08-24 00:07:09 +0000783
Craig Toppercaf138e2015-12-05 07:41:42 +0000784 AsmToks = new (C) Token[asmtoks.size()];
785 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000786
Craig Toppercaf138e2015-12-05 07:41:42 +0000787 Constraints = new (C) StringRef[exprs.size()];
788 std::transform(constraints.begin(), constraints.end(), Constraints,
789 [&](StringRef Constraint) {
790 return copyIntoContext(C, Constraint);
791 });
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000792
Chad Rosierbaf53f92012-08-10 21:36:25 +0000793 Clobbers = new (C) StringRef[NumClobbers];
Craig Toppercaf138e2015-12-05 07:41:42 +0000794 // FIXME: Avoid the allocation/copy if at all possible.
795 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
796 [&](StringRef Clobber) {
797 return copyIntoContext(C, Clobber);
798 });
Chad Rosier32503022012-06-11 20:47:18 +0000799}
800
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000801IfStmt::IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr,
802 Stmt *Init, VarDecl *Var, Expr *Cond, Stmt *Then,
803 SourceLocation EL, Stmt *Else)
804 : Stmt(IfStmtClass) {
805 bool HasElse = Else != nullptr;
806 bool HasVar = Var != nullptr;
807 bool HasInit = Init != nullptr;
808 IfStmtBits.HasElse = HasElse;
809 IfStmtBits.HasVar = HasVar;
810 IfStmtBits.HasInit = HasInit;
811
Richard Smithb130fe72016-06-23 19:16:49 +0000812 setConstexpr(IsConstexpr);
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000813
814 setCond(Cond);
815 setThen(Then);
816 if (HasElse)
817 setElse(Else);
818 if (HasVar)
819 setConditionVariable(Ctx, Var);
820 if (HasInit)
821 setInit(Init);
822
823 setIfLoc(IL);
824 if (HasElse)
825 setElseLoc(EL);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000826}
827
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000828IfStmt::IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit)
829 : Stmt(IfStmtClass, Empty) {
830 IfStmtBits.HasElse = HasElse;
831 IfStmtBits.HasVar = HasVar;
832 IfStmtBits.HasInit = HasInit;
833}
Chad Rosier42032fa2012-08-07 23:12:23 +0000834
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000835IfStmt *IfStmt::Create(const ASTContext &Ctx, SourceLocation IL,
836 bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond,
837 Stmt *Then, SourceLocation EL, Stmt *Else) {
838 bool HasElse = Else != nullptr;
839 bool HasVar = Var != nullptr;
840 bool HasInit = Init != nullptr;
841 void *Mem = Ctx.Allocate(
842 totalSizeToAlloc<Stmt *, SourceLocation>(
843 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
844 alignof(IfStmt));
845 return new (Mem)
846 IfStmt(Ctx, IL, IsConstexpr, Init, Var, Cond, Then, EL, Else);
847}
848
849IfStmt *IfStmt::CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
850 bool HasInit) {
851 void *Mem = Ctx.Allocate(
852 totalSizeToAlloc<Stmt *, SourceLocation>(
853 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
854 alignof(IfStmt));
855 return new (Mem) IfStmt(EmptyShell(), HasElse, HasVar, HasInit);
856}
857
858VarDecl *IfStmt::getConditionVariable() {
859 auto *DS = getConditionVariableDeclStmt();
860 if (!DS)
861 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000862 return cast<VarDecl>(DS->getSingleDecl());
863}
864
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000865void IfStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
866 assert(hasVarStorage() &&
867 "This if statement has no storage for a condition variable!");
868
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000869 if (!V) {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000870 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000871 return;
872 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000873
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000874 SourceRange VarRange = V->getSourceRange();
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000875 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
876 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000877}
878
Erik Pilkington5cd57172016-08-16 17:44:11 +0000879bool IfStmt::isObjCAvailabilityCheck() const {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000880 return isa<ObjCAvailabilityCheckExpr>(getCond());
Erik Pilkington5cd57172016-08-16 17:44:11 +0000881}
882
Craig Toppere6960e22013-08-22 05:28:54 +0000883ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000884 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000885 SourceLocation RP)
Bruno Ricci41d11c02018-10-27 18:43:27 +0000886 : Stmt(ForStmtClass), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000887{
888 SubExprs[INIT] = Init;
889 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000890 SubExprs[COND] = Cond;
891 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000892 SubExprs[BODY] = Body;
Bruno Ricci41d11c02018-10-27 18:43:27 +0000893 ForStmtBits.ForLoc = FL;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000894}
895
896VarDecl *ForStmt::getConditionVariable() const {
897 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000898 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000899
Eugene Zelenko7855e772018-04-03 00:11:50 +0000900 auto *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000901 return cast<VarDecl>(DS->getSingleDecl());
902}
903
Craig Toppere6960e22013-08-22 05:28:54 +0000904void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000905 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000906 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000907 return;
908 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000909
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000910 SourceRange VarRange = V->getSourceRange();
911 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
912 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000913}
914
Bruno Riccie2806f82018-10-29 16:12:37 +0000915SwitchStmt::SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
916 Expr *Cond)
917 : Stmt(SwitchStmtClass), FirstCase(nullptr) {
918 bool HasInit = Init != nullptr;
919 bool HasVar = Var != nullptr;
920 SwitchStmtBits.HasInit = HasInit;
921 SwitchStmtBits.HasVar = HasVar;
922 SwitchStmtBits.AllEnumCasesCovered = false;
923
924 setCond(Cond);
925 setBody(nullptr);
926 if (HasInit)
927 setInit(Init);
928 if (HasVar)
929 setConditionVariable(Ctx, Var);
930
931 setSwitchLoc(SourceLocation{});
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000932}
933
Bruno Riccie2806f82018-10-29 16:12:37 +0000934SwitchStmt::SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar)
935 : Stmt(SwitchStmtClass, Empty) {
936 SwitchStmtBits.HasInit = HasInit;
937 SwitchStmtBits.HasVar = HasVar;
938 SwitchStmtBits.AllEnumCasesCovered = false;
939}
Chad Rosier42032fa2012-08-07 23:12:23 +0000940
Bruno Riccie2806f82018-10-29 16:12:37 +0000941SwitchStmt *SwitchStmt::Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
942 Expr *Cond) {
943 bool HasInit = Init != nullptr;
944 bool HasVar = Var != nullptr;
945 void *Mem = Ctx.Allocate(
946 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
947 alignof(SwitchStmt));
948 return new (Mem) SwitchStmt(Ctx, Init, Var, Cond);
949}
950
951SwitchStmt *SwitchStmt::CreateEmpty(const ASTContext &Ctx, bool HasInit,
952 bool HasVar) {
953 void *Mem = Ctx.Allocate(
954 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
955 alignof(SwitchStmt));
956 return new (Mem) SwitchStmt(EmptyShell(), HasInit, HasVar);
957}
958
959VarDecl *SwitchStmt::getConditionVariable() {
960 auto *DS = getConditionVariableDeclStmt();
961 if (!DS)
962 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000963 return cast<VarDecl>(DS->getSingleDecl());
964}
965
Bruno Riccie2806f82018-10-29 16:12:37 +0000966void SwitchStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
967 assert(hasVarStorage() &&
968 "This switch statement has no storage for a condition variable!");
969
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000970 if (!V) {
Bruno Riccie2806f82018-10-29 16:12:37 +0000971 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000972 return;
973 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000974
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000975 SourceRange VarRange = V->getSourceRange();
Bruno Riccie2806f82018-10-29 16:12:37 +0000976 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
977 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000978}
979
Bruno Riccibacf7512018-10-30 13:42:41 +0000980WhileStmt::WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
981 Stmt *Body, SourceLocation WL)
982 : Stmt(WhileStmtClass) {
983 bool HasVar = Var != nullptr;
984 WhileStmtBits.HasVar = HasVar;
985
986 setCond(Cond);
987 setBody(Body);
988 if (HasVar)
989 setConditionVariable(Ctx, Var);
990
991 setWhileLoc(WL);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000992}
993
Bruno Riccibacf7512018-10-30 13:42:41 +0000994WhileStmt::WhileStmt(EmptyShell Empty, bool HasVar)
995 : Stmt(WhileStmtClass, Empty) {
996 WhileStmtBits.HasVar = HasVar;
997}
Chad Rosier42032fa2012-08-07 23:12:23 +0000998
Bruno Riccibacf7512018-10-30 13:42:41 +0000999WhileStmt *WhileStmt::Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
1000 Stmt *Body, SourceLocation WL) {
1001 bool HasVar = Var != nullptr;
1002 void *Mem =
1003 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1004 alignof(WhileStmt));
1005 return new (Mem) WhileStmt(Ctx, Var, Cond, Body, WL);
1006}
1007
1008WhileStmt *WhileStmt::CreateEmpty(const ASTContext &Ctx, bool HasVar) {
1009 void *Mem =
1010 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1011 alignof(WhileStmt));
1012 return new (Mem) WhileStmt(EmptyShell(), HasVar);
1013}
1014
1015VarDecl *WhileStmt::getConditionVariable() {
1016 auto *DS = getConditionVariableDeclStmt();
1017 if (!DS)
1018 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001019 return cast<VarDecl>(DS->getSingleDecl());
1020}
1021
Bruno Riccibacf7512018-10-30 13:42:41 +00001022void WhileStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
1023 assert(hasVarStorage() &&
1024 "This while statement has no storage for a condition variable!");
1025
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001026 if (!V) {
Bruno Riccibacf7512018-10-30 13:42:41 +00001027 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001028 return;
1029 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +00001030
1031 SourceRange VarRange = V->getSourceRange();
Bruno Riccibacf7512018-10-30 13:42:41 +00001032 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
1033 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001034}
1035
Ted Kremenek066dd932007-08-24 21:09:09 +00001036// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001037LabelDecl *IndirectGotoStmt::getConstantTarget() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001038 if (auto *E = dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
John McCall9de91602010-10-28 08:53:48 +00001039 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +00001040 return nullptr;
John McCall9de91602010-10-28 08:53:48 +00001041}
Ted Kremenek066dd932007-08-24 21:09:09 +00001042
Ted Kremenek066dd932007-08-24 21:09:09 +00001043// ReturnStmt
Bruno Ricci023b1d12018-10-30 14:40:49 +00001044ReturnStmt::ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1045 : Stmt(ReturnStmtClass), RetExpr(E) {
1046 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1047 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1048 if (HasNRVOCandidate)
1049 setNRVOCandidate(NRVOCandidate);
1050 setReturnLoc(RL);
Ted Kremenekc6501db2008-06-17 03:11:08 +00001051}
Bruno Ricci023b1d12018-10-30 14:40:49 +00001052
1053ReturnStmt::ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate)
1054 : Stmt(ReturnStmtClass, Empty) {
1055 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1056}
1057
1058ReturnStmt *ReturnStmt::Create(const ASTContext &Ctx, SourceLocation RL,
1059 Expr *E, const VarDecl *NRVOCandidate) {
1060 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1061 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1062 alignof(ReturnStmt));
1063 return new (Mem) ReturnStmt(RL, E, NRVOCandidate);
1064}
1065
1066ReturnStmt *ReturnStmt::CreateEmpty(const ASTContext &Ctx,
1067 bool HasNRVOCandidate) {
1068 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1069 alignof(ReturnStmt));
1070 return new (Mem) ReturnStmt(EmptyShell(), HasNRVOCandidate);
Ted Kremenek066dd932007-08-24 21:09:09 +00001071}
John Wiegley1c0675e2011-04-28 01:08:34 +00001072
Bruno Ricci5b30571752018-10-28 12:30:53 +00001073// CaseStmt
1074CaseStmt *CaseStmt::Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1075 SourceLocation caseLoc, SourceLocation ellipsisLoc,
1076 SourceLocation colonLoc) {
1077 bool CaseStmtIsGNURange = rhs != nullptr;
1078 void *Mem = Ctx.Allocate(
1079 totalSizeToAlloc<Stmt *, SourceLocation>(
1080 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1081 alignof(CaseStmt));
1082 return new (Mem) CaseStmt(lhs, rhs, caseLoc, ellipsisLoc, colonLoc);
1083}
1084
1085CaseStmt *CaseStmt::CreateEmpty(const ASTContext &Ctx,
1086 bool CaseStmtIsGNURange) {
1087 void *Mem = Ctx.Allocate(
1088 totalSizeToAlloc<Stmt *, SourceLocation>(
1089 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1090 alignof(CaseStmt));
1091 return new (Mem) CaseStmt(EmptyShell(), CaseStmtIsGNURange);
1092}
1093
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001094SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001095 Stmt *Handler)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001096 : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc) {
Warren Huntf6be4cb2014-07-25 20:52:51 +00001097 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +00001098 Children[HANDLER] = Handler;
1099}
1100
Warren Huntf6be4cb2014-07-25 20:52:51 +00001101SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +00001102 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001103 Stmt *Handler) {
1104 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +00001105}
1106
1107SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1108 return dyn_cast<SEHExceptStmt>(getHandler());
1109}
1110
1111SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1112 return dyn_cast<SEHFinallyStmt>(getHandler());
1113}
1114
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001115SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block)
1116 : Stmt(SEHExceptStmtClass), Loc(Loc) {
Pavel Labath515f4db2013-09-03 14:41:16 +00001117 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +00001118 Children[BLOCK] = Block;
1119}
1120
Craig Toppere6960e22013-08-22 05:28:54 +00001121SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
1122 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +00001123 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1124}
1125
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001126SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, Stmt *Block)
1127 : Stmt(SEHFinallyStmtClass), Loc(Loc), Block(Block) {}
John Wiegley1c0675e2011-04-28 01:08:34 +00001128
Craig Toppere6960e22013-08-22 05:28:54 +00001129SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +00001130 Stmt *Block) {
1131 return new(C)SEHFinallyStmt(Loc,Block);
1132}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001133
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001134CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
1135 VarDecl *Var)
1136 : VarAndKind(Var, Kind), Loc(Loc) {
1137 switch (Kind) {
1138 case VCK_This:
1139 assert(!Var && "'this' capture cannot have a variable!");
1140 break;
1141 case VCK_ByRef:
1142 assert(Var && "capturing by reference must have a variable!");
1143 break;
1144 case VCK_ByCopy:
1145 assert(Var && "capturing by copy must have a variable!");
1146 assert(
1147 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
1148 Var->getType()
1149 ->castAs<ReferenceType>()
1150 ->getPointeeType()
1151 ->isScalarType())) &&
1152 "captures by copy are expected to have a scalar type!");
1153 break;
1154 case VCK_VLAType:
1155 assert(!Var &&
1156 "Variable-length array type capture cannot have a variable!");
1157 break;
1158 }
1159}
1160
Chandler Carruth21c90602015-12-30 03:24:14 +00001161CapturedStmt::VariableCaptureKind
1162CapturedStmt::Capture::getCaptureKind() const {
1163 return VarAndKind.getInt();
1164}
1165
1166VarDecl *CapturedStmt::Capture::getCapturedVar() const {
1167 assert((capturesVariable() || capturesVariableByCopy()) &&
1168 "No variable available for 'this' or VAT capture");
1169 return VarAndKind.getPointer();
1170}
1171
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001172CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1173 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1174
1175 // Offset of the first Capture object.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001176 unsigned FirstCaptureOffset = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001177
1178 return reinterpret_cast<Capture *>(
1179 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1180 + FirstCaptureOffset);
1181}
1182
Wei Pan17fbf6e2013-05-04 03:59:06 +00001183CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1184 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001185 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001186 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001187 RecordDecl *RD)
1188 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001189 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001190 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001191 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001192 assert(RD && "null record declaration for captured statement");
1193
1194 // Copy initialization expressions.
1195 Stmt **Stored = getStoredStmts();
1196 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1197 *Stored++ = CaptureInits[I];
1198
1199 // Copy the statement being captured.
1200 *Stored = S;
1201
1202 // Copy all Capture objects.
1203 Capture *Buffer = getStoredCaptures();
1204 std::copy(Captures.begin(), Captures.end(), Buffer);
1205}
1206
1207CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1208 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001209 CapDeclAndKind(nullptr, CR_Default) {
Craig Topper36250ad2014-05-12 05:36:57 +00001210 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001211}
1212
Craig Toppere6960e22013-08-22 05:28:54 +00001213CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001214 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001215 ArrayRef<Capture> Captures,
1216 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001217 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001218 RecordDecl *RD) {
1219 // The layout is
1220 //
1221 // -----------------------------------------------------------
1222 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1223 // ----------------^-------------------^----------------------
1224 // getStoredStmts() getStoredCaptures()
1225 //
1226 // where S is the statement being captured.
1227 //
1228 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1229
1230 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1231 if (!Captures.empty()) {
1232 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001233 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001234 Size += sizeof(Capture) * Captures.size();
1235 }
1236
1237 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001238 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001239}
1240
Craig Toppere6960e22013-08-22 05:28:54 +00001241CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001242 unsigned NumCaptures) {
1243 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1244 if (NumCaptures > 0) {
1245 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001246 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001247 Size += sizeof(Capture) * NumCaptures;
1248 }
1249
1250 void *Mem = Context.Allocate(Size);
1251 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1252}
1253
1254Stmt::child_range CapturedStmt::children() {
Simon Pilgrim2c518802017-03-30 14:13:19 +00001255 // Children are captured field initializers.
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001256 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001257}
1258
Bruno Ricci06186502019-04-12 15:36:02 +00001259Stmt::const_child_range CapturedStmt::children() const {
1260 return const_child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1261}
1262
Chandler Carruth21c90602015-12-30 03:24:14 +00001263CapturedDecl *CapturedStmt::getCapturedDecl() {
1264 return CapDeclAndKind.getPointer();
1265}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001266
Chandler Carruth21c90602015-12-30 03:24:14 +00001267const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1268 return CapDeclAndKind.getPointer();
1269}
1270
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001271/// Set the outlined function declaration.
Chandler Carruth21c90602015-12-30 03:24:14 +00001272void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1273 assert(D && "null CapturedDecl");
1274 CapDeclAndKind.setPointer(D);
1275}
1276
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001277/// Retrieve the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001278CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1279 return CapDeclAndKind.getInt();
1280}
1281
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001282/// Set the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001283void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1284 CapDeclAndKind.setInt(Kind);
1285}
1286
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001287bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001288 for (const auto &I : captures()) {
Alexey Bataev2c845412017-05-15 16:26:15 +00001289 if (!I.capturesVariable() && !I.capturesVariableByCopy())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001290 continue;
Alexey Bataeve85de8f2017-09-20 20:11:31 +00001291 if (I.getCapturedVar()->getCanonicalDecl() == Var->getCanonicalDecl())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001292 return true;
1293 }
1294
1295 return false;
1296}