blob: 0a4d403106bd4517df755741151fba7ed90baf28 [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
Jennifer Yub8fee672019-06-03 15:57:25 +0000447AddrLabelExpr *GCCAsmStmt::getLabelExpr(unsigned i) const {
448 return cast<AddrLabelExpr>(Exprs[i + NumInputs]);
449}
450
451StringRef GCCAsmStmt::getLabelName(unsigned i) const {
452 return getLabelExpr(i)->getLabel()->getName();
453}
454
Chris Lattner72bbf172009-03-10 04:59:06 +0000455/// getInputConstraint - Return the specified input constraint. Unlike output
456/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000457StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000458 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000459}
460
Craig Topperc571c812013-08-22 06:02:26 +0000461void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
462 IdentifierInfo **Names,
463 StringLiteral **Constraints,
464 Stmt **Exprs,
465 unsigned NumOutputs,
466 unsigned NumInputs,
Jennifer Yub8fee672019-06-03 15:57:25 +0000467 unsigned NumLabels,
Craig Topperc571c812013-08-22 06:02:26 +0000468 StringLiteral **Clobbers,
469 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000470 this->NumOutputs = NumOutputs;
471 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000472 this->NumClobbers = NumClobbers;
Jennifer Yub8fee672019-06-03 15:57:25 +0000473 this->NumLabels = NumLabels;
474 assert(!(NumOutputs && NumLabels) && "asm goto cannot have outputs");
Anders Carlsson98323d22010-01-30 23:19:41 +0000475
Jennifer Yub8fee672019-06-03 15:57:25 +0000476 unsigned NumExprs = NumOutputs + NumInputs + NumLabels;
Chad Rosier42032fa2012-08-07 23:12:23 +0000477
Anders Carlsson98323d22010-01-30 23:19:41 +0000478 C.Deallocate(this->Names);
479 this->Names = new (C) IdentifierInfo*[NumExprs];
480 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000481
Anders Carlsson98323d22010-01-30 23:19:41 +0000482 C.Deallocate(this->Exprs);
483 this->Exprs = new (C) Stmt*[NumExprs];
484 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000485
Benjamin Kramer851f57e2019-05-30 07:21:08 +0000486 unsigned NumConstraints = NumOutputs + NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000487 C.Deallocate(this->Constraints);
Benjamin Kramer851f57e2019-05-30 07:21:08 +0000488 this->Constraints = new (C) StringLiteral*[NumConstraints];
489 std::copy(Constraints, Constraints + NumConstraints, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000490
Anders Carlsson98323d22010-01-30 23:19:41 +0000491 C.Deallocate(this->Clobbers);
492 this->Clobbers = new (C) StringLiteral*[NumClobbers];
493 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000494}
495
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000496/// getNamedOperand - Given a symbolic operand reference like %[foo],
497/// translate this into a numeric value needed to reference the same operand.
498/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000499int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000500 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000502 // Check if this is an output operand.
503 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
504 if (getOutputName(i) == SymbolicName)
505 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000508 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
509 if (getInputName(i) == SymbolicName)
510 return getNumOutputs() + NumPlusOperands + i;
511
Jennifer Yub8fee672019-06-03 15:57:25 +0000512 for (unsigned i = 0, e = getNumLabels(); i != e; ++i)
513 if (getLabelName(i) == SymbolicName)
514 return i + getNumInputs();
515
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000516 // Not found.
517 return -1;
518}
519
Chris Lattner35b58362009-03-10 23:21:44 +0000520/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
521/// it into pieces. If the asm string is erroneous, emit errors and return
522/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000523unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000524 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000525 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000526 const char *StrStart = Str.begin();
527 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000528 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000529
Chris Lattner35b58362009-03-10 23:21:44 +0000530 // "Simple" inline asms have no constraints or operands, just convert the asm
531 // string to escape $'s.
532 if (isSimple()) {
533 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000534 for (; CurPtr != StrEnd; ++CurPtr) {
535 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000536 case '$':
537 Result += "$$";
538 break;
539 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000540 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000541 break;
542 }
543 }
544 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000545 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000546 }
547
548 // CurStringPiece - The current string that we are building up as we scan the
549 // asm string.
550 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000551
Douglas Gregore8bbc122011-09-02 00:18:52 +0000552 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000553
Richard Smithd18ab802016-05-16 22:52:23 +0000554 unsigned LastAsmStringToken = 0;
555 unsigned LastAsmStringOffset = 0;
556
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000557 while (true) {
Chris Lattner35b58362009-03-10 23:21:44 +0000558 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000559 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000560 if (!CurStringPiece.empty())
561 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000562 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000563 }
Mike Stump11289f42009-09-09 15:08:12 +0000564
Chris Lattnera41b8472009-03-10 23:51:40 +0000565 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000566 switch (CurChar) {
567 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000568 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
569 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
570 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000571 case '%':
572 break;
573 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000574 CurStringPiece += CurChar;
575 continue;
576 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000577
Chris Lattner35b58362009-03-10 23:21:44 +0000578 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000579 if (CurPtr == StrEnd) {
580 // % at end of string is invalid (no escape).
581 DiagOffs = CurPtr-StrStart-1;
582 return diag::err_asm_invalid_escape;
583 }
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000584 // Handle escaped char and continue looping over the asm string.
Chris Lattnera41b8472009-03-10 23:51:40 +0000585 char EscapedChar = *CurPtr++;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000586 switch (EscapedChar) {
587 default:
588 break;
589 case '%': // %% -> %
590 case '{': // %{ -> {
591 case '}': // %} -> }
592 CurStringPiece += EscapedChar;
Chris Lattner35b58362009-03-10 23:21:44 +0000593 continue;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000594 case '=': // %= -> Generate a unique ID.
Chris Lattner35b58362009-03-10 23:21:44 +0000595 CurStringPiece += "${:uid}";
596 continue;
597 }
Mike Stump11289f42009-09-09 15:08:12 +0000598
Chris Lattner35b58362009-03-10 23:21:44 +0000599 // Otherwise, we have an operand. If we have accumulated a string so far,
600 // add it to the Pieces list.
601 if (!CurStringPiece.empty()) {
602 Pieces.push_back(AsmStringPiece(CurStringPiece));
603 CurStringPiece.clear();
604 }
Mike Stump11289f42009-09-09 15:08:12 +0000605
Akira Hatanaka987f1862014-08-22 06:05:21 +0000606 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
607 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
608
609 const char *Begin = CurPtr - 1; // Points to the character following '%'.
610 const char *Percent = Begin - 1; // Points to '%'.
611
Jordan Rosea7d03842013-02-08 22:30:41 +0000612 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000613 if (CurPtr == StrEnd) { // Premature end.
614 DiagOffs = CurPtr-StrStart-1;
615 return diag::err_asm_invalid_escape;
616 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000617 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000618 }
Mike Stump11289f42009-09-09 15:08:12 +0000619
Akira Hatanaka987f1862014-08-22 06:05:21 +0000620 const TargetInfo &TI = C.getTargetInfo();
621 const SourceManager &SM = C.getSourceManager();
622 const LangOptions &LO = C.getLangOpts();
623
624 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000625 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000626 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000627 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000628
Chris Lattner99d892b2009-03-11 22:52:17 +0000629 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000630 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000631 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000632
Jennifer Yub8fee672019-06-03 15:57:25 +0000633 unsigned NumOperands = getNumOutputs() + getNumPlusOperands() +
634 getNumInputs() + getNumLabels();
Chris Lattner14311922009-03-11 00:23:13 +0000635 if (N >= NumOperands) {
636 DiagOffs = CurPtr-StrStart-1;
637 return diag::err_asm_invalid_operand_number;
638 }
639
Akira Hatanaka987f1862014-08-22 06:05:21 +0000640 // Str contains "x4" (Operand without the leading %).
641 std::string Str(Begin, CurPtr - Begin);
642
643 // (BeginLoc, EndLoc) represents the range of the operand we are currently
644 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000645 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
646 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
647 &LastAsmStringOffset);
648 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
649 CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken,
650 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000651
Benjamin Kramer3204b152015-05-29 19:42:19 +0000652 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Chris Lattner35b58362009-03-10 23:21:44 +0000653 continue;
654 }
Mike Stump11289f42009-09-09 15:08:12 +0000655
Akira Hatanaka987f1862014-08-22 06:05:21 +0000656 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000657 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000658 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000659
Chris Lattner3fa25c62009-03-11 00:06:36 +0000660 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000661 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000662 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000663 return diag::err_asm_unterminated_symbolic_operand_name;
664 if (NameEnd == CurPtr)
665 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000667 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000668
Chris Lattner35b58362009-03-10 23:21:44 +0000669 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000670 if (N == -1) {
671 // Verify that an operand with that name exists.
672 DiagOffs = CurPtr-StrStart;
673 return diag::err_asm_unknown_symbolic_operand_name;
674 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000675
676 // Str contains "x[foo]" (Operand without the leading %).
677 std::string Str(Begin, NameEnd + 1 - Begin);
678
679 // (BeginLoc, EndLoc) represents the range of the operand we are currently
680 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000681 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
682 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
683 &LastAsmStringOffset);
684 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
685 NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken,
686 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000687
Benjamin Kramer3204b152015-05-29 19:42:19 +0000688 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattner3fa25c62009-03-11 00:06:36 +0000690 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000691 continue;
692 }
Mike Stump11289f42009-09-09 15:08:12 +0000693
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000694 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000695 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000696 }
697}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000698
699/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000700std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000701 // Analyze the asm string to decompose it into its pieces. We know that Sema
702 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000703 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000704 unsigned DiagOffs;
705 AnalyzeAsmString(Pieces, C, DiagOffs);
706
707 std::string AsmString;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000708 for (const auto &Piece : Pieces) {
709 if (Piece.isString())
710 AsmString += Piece.getString();
711 else if (Piece.getModifier() == '\0')
712 AsmString += '$' + llvm::utostr(Piece.getOperandNo());
Chad Rosier14836ba2012-08-24 17:05:45 +0000713 else
Eugene Zelenko7855e772018-04-03 00:11:50 +0000714 AsmString += "${" + llvm::utostr(Piece.getOperandNo()) + ':' +
715 Piece.getModifier() + '}';
Chad Rosier14836ba2012-08-24 17:05:45 +0000716 }
717 return AsmString;
718}
Chris Lattner35b58362009-03-10 23:21:44 +0000719
Chad Rosier3b0c2602012-08-27 20:23:31 +0000720/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000721std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000722 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000723 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000724}
725
Chad Rosierfe31e622012-08-24 00:07:09 +0000726Expr *MSAsmStmt::getOutputExpr(unsigned i) {
727 return cast<Expr>(Exprs[i]);
728}
729
730Expr *MSAsmStmt::getInputExpr(unsigned i) {
731 return cast<Expr>(Exprs[i + NumOutputs]);
732}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000733
Chad Rosierfe31e622012-08-24 00:07:09 +0000734void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
735 Exprs[i + NumOutputs] = E;
736}
737
Chris Lattner86f5e132008-01-30 05:01:46 +0000738//===----------------------------------------------------------------------===//
739// Constructors
740//===----------------------------------------------------------------------===//
741
Craig Toppere6960e22013-08-22 05:28:54 +0000742GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
743 bool issimple, bool isvolatile, unsigned numoutputs,
744 unsigned numinputs, IdentifierInfo **names,
745 StringLiteral **constraints, Expr **exprs,
746 StringLiteral *asmstr, unsigned numclobbers,
Jennifer Yub8fee672019-06-03 15:57:25 +0000747 StringLiteral **clobbers, unsigned numlabels,
748 SourceLocation rparenloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000749 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
Jennifer Yub8fee672019-06-03 15:57:25 +0000750 numinputs, numclobbers),
751 RParenLoc(rparenloc), AsmStr(asmstr), NumLabels(numlabels) {
752 unsigned NumExprs = NumOutputs + NumInputs + NumLabels;
Chad Rosier42032fa2012-08-07 23:12:23 +0000753
Anders Carlsson98323d22010-01-30 23:19:41 +0000754 Names = new (C) IdentifierInfo*[NumExprs];
755 std::copy(names, names + NumExprs, Names);
756
757 Exprs = new (C) Stmt*[NumExprs];
758 std::copy(exprs, exprs + NumExprs, Exprs);
759
Benjamin Kramer851f57e2019-05-30 07:21:08 +0000760 unsigned NumConstraints = NumOutputs + NumInputs;
761 Constraints = new (C) StringLiteral*[NumConstraints];
762 std::copy(constraints, constraints + NumConstraints, Constraints);
Anders Carlsson98323d22010-01-30 23:19:41 +0000763
764 Clobbers = new (C) StringLiteral*[NumClobbers];
765 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000766}
767
Craig Toppere6960e22013-08-22 05:28:54 +0000768MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000769 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000770 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000771 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000772 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
773 StringRef asmstr, ArrayRef<StringRef> clobbers,
774 SourceLocation endloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000775 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
776 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
777 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
John McCallf413f5e2013-05-03 00:10:13 +0000778 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
779}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000780
Craig Toppere6960e22013-08-22 05:28:54 +0000781static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
Benjamin Kramer2ab0d882015-08-04 12:34:23 +0000782 return str.copy(C);
John McCallf413f5e2013-05-03 00:10:13 +0000783}
784
Craig Toppere6960e22013-08-22 05:28:54 +0000785void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000786 ArrayRef<Token> asmtoks,
787 ArrayRef<StringRef> constraints,
788 ArrayRef<Expr*> exprs,
789 ArrayRef<StringRef> clobbers) {
790 assert(NumAsmToks == asmtoks.size());
791 assert(NumClobbers == clobbers.size());
792
Craig Toppercaf138e2015-12-05 07:41:42 +0000793 assert(exprs.size() == NumOutputs + NumInputs);
794 assert(exprs.size() == constraints.size());
John McCallf413f5e2013-05-03 00:10:13 +0000795
796 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000797
Craig Toppercaf138e2015-12-05 07:41:42 +0000798 Exprs = new (C) Stmt*[exprs.size()];
799 std::copy(exprs.begin(), exprs.end(), Exprs);
Chad Rosierfe31e622012-08-24 00:07:09 +0000800
Craig Toppercaf138e2015-12-05 07:41:42 +0000801 AsmToks = new (C) Token[asmtoks.size()];
802 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000803
Craig Toppercaf138e2015-12-05 07:41:42 +0000804 Constraints = new (C) StringRef[exprs.size()];
805 std::transform(constraints.begin(), constraints.end(), Constraints,
806 [&](StringRef Constraint) {
807 return copyIntoContext(C, Constraint);
808 });
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000809
Chad Rosierbaf53f92012-08-10 21:36:25 +0000810 Clobbers = new (C) StringRef[NumClobbers];
Craig Toppercaf138e2015-12-05 07:41:42 +0000811 // FIXME: Avoid the allocation/copy if at all possible.
812 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
813 [&](StringRef Clobber) {
814 return copyIntoContext(C, Clobber);
815 });
Chad Rosier32503022012-06-11 20:47:18 +0000816}
817
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000818IfStmt::IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr,
819 Stmt *Init, VarDecl *Var, Expr *Cond, Stmt *Then,
820 SourceLocation EL, Stmt *Else)
821 : Stmt(IfStmtClass) {
822 bool HasElse = Else != nullptr;
823 bool HasVar = Var != nullptr;
824 bool HasInit = Init != nullptr;
825 IfStmtBits.HasElse = HasElse;
826 IfStmtBits.HasVar = HasVar;
827 IfStmtBits.HasInit = HasInit;
828
Richard Smithb130fe72016-06-23 19:16:49 +0000829 setConstexpr(IsConstexpr);
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000830
831 setCond(Cond);
832 setThen(Then);
833 if (HasElse)
834 setElse(Else);
835 if (HasVar)
836 setConditionVariable(Ctx, Var);
837 if (HasInit)
838 setInit(Init);
839
840 setIfLoc(IL);
841 if (HasElse)
842 setElseLoc(EL);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000843}
844
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000845IfStmt::IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit)
846 : Stmt(IfStmtClass, Empty) {
847 IfStmtBits.HasElse = HasElse;
848 IfStmtBits.HasVar = HasVar;
849 IfStmtBits.HasInit = HasInit;
850}
Chad Rosier42032fa2012-08-07 23:12:23 +0000851
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000852IfStmt *IfStmt::Create(const ASTContext &Ctx, SourceLocation IL,
853 bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond,
854 Stmt *Then, SourceLocation EL, Stmt *Else) {
855 bool HasElse = Else != nullptr;
856 bool HasVar = Var != nullptr;
857 bool HasInit = Init != nullptr;
858 void *Mem = Ctx.Allocate(
859 totalSizeToAlloc<Stmt *, SourceLocation>(
860 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
861 alignof(IfStmt));
862 return new (Mem)
863 IfStmt(Ctx, IL, IsConstexpr, Init, Var, Cond, Then, EL, Else);
864}
865
866IfStmt *IfStmt::CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
867 bool HasInit) {
868 void *Mem = Ctx.Allocate(
869 totalSizeToAlloc<Stmt *, SourceLocation>(
870 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
871 alignof(IfStmt));
872 return new (Mem) IfStmt(EmptyShell(), HasElse, HasVar, HasInit);
873}
874
875VarDecl *IfStmt::getConditionVariable() {
876 auto *DS = getConditionVariableDeclStmt();
877 if (!DS)
878 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000879 return cast<VarDecl>(DS->getSingleDecl());
880}
881
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000882void IfStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
883 assert(hasVarStorage() &&
884 "This if statement has no storage for a condition variable!");
885
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000886 if (!V) {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000887 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000888 return;
889 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000890
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000891 SourceRange VarRange = V->getSourceRange();
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000892 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
893 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000894}
895
Erik Pilkington5cd57172016-08-16 17:44:11 +0000896bool IfStmt::isObjCAvailabilityCheck() const {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000897 return isa<ObjCAvailabilityCheckExpr>(getCond());
Erik Pilkington5cd57172016-08-16 17:44:11 +0000898}
899
Craig Toppere6960e22013-08-22 05:28:54 +0000900ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000901 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000902 SourceLocation RP)
Bruno Ricci41d11c02018-10-27 18:43:27 +0000903 : Stmt(ForStmtClass), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000904{
905 SubExprs[INIT] = Init;
906 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000907 SubExprs[COND] = Cond;
908 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000909 SubExprs[BODY] = Body;
Bruno Ricci41d11c02018-10-27 18:43:27 +0000910 ForStmtBits.ForLoc = FL;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000911}
912
913VarDecl *ForStmt::getConditionVariable() const {
914 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000915 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000916
Eugene Zelenko7855e772018-04-03 00:11:50 +0000917 auto *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000918 return cast<VarDecl>(DS->getSingleDecl());
919}
920
Craig Toppere6960e22013-08-22 05:28:54 +0000921void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000922 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000923 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000924 return;
925 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000926
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000927 SourceRange VarRange = V->getSourceRange();
928 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
929 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000930}
931
Bruno Riccie2806f82018-10-29 16:12:37 +0000932SwitchStmt::SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
933 Expr *Cond)
934 : Stmt(SwitchStmtClass), FirstCase(nullptr) {
935 bool HasInit = Init != nullptr;
936 bool HasVar = Var != nullptr;
937 SwitchStmtBits.HasInit = HasInit;
938 SwitchStmtBits.HasVar = HasVar;
939 SwitchStmtBits.AllEnumCasesCovered = false;
940
941 setCond(Cond);
942 setBody(nullptr);
943 if (HasInit)
944 setInit(Init);
945 if (HasVar)
946 setConditionVariable(Ctx, Var);
947
948 setSwitchLoc(SourceLocation{});
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000949}
950
Bruno Riccie2806f82018-10-29 16:12:37 +0000951SwitchStmt::SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar)
952 : Stmt(SwitchStmtClass, Empty) {
953 SwitchStmtBits.HasInit = HasInit;
954 SwitchStmtBits.HasVar = HasVar;
955 SwitchStmtBits.AllEnumCasesCovered = false;
956}
Chad Rosier42032fa2012-08-07 23:12:23 +0000957
Bruno Riccie2806f82018-10-29 16:12:37 +0000958SwitchStmt *SwitchStmt::Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
959 Expr *Cond) {
960 bool HasInit = Init != nullptr;
961 bool HasVar = Var != nullptr;
962 void *Mem = Ctx.Allocate(
963 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
964 alignof(SwitchStmt));
965 return new (Mem) SwitchStmt(Ctx, Init, Var, Cond);
966}
967
968SwitchStmt *SwitchStmt::CreateEmpty(const ASTContext &Ctx, bool HasInit,
969 bool HasVar) {
970 void *Mem = Ctx.Allocate(
971 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
972 alignof(SwitchStmt));
973 return new (Mem) SwitchStmt(EmptyShell(), HasInit, HasVar);
974}
975
976VarDecl *SwitchStmt::getConditionVariable() {
977 auto *DS = getConditionVariableDeclStmt();
978 if (!DS)
979 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000980 return cast<VarDecl>(DS->getSingleDecl());
981}
982
Bruno Riccie2806f82018-10-29 16:12:37 +0000983void SwitchStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
984 assert(hasVarStorage() &&
985 "This switch statement has no storage for a condition variable!");
986
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000987 if (!V) {
Bruno Riccie2806f82018-10-29 16:12:37 +0000988 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000989 return;
990 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000991
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000992 SourceRange VarRange = V->getSourceRange();
Bruno Riccie2806f82018-10-29 16:12:37 +0000993 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
994 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000995}
996
Bruno Riccibacf7512018-10-30 13:42:41 +0000997WhileStmt::WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
998 Stmt *Body, SourceLocation WL)
999 : Stmt(WhileStmtClass) {
1000 bool HasVar = Var != nullptr;
1001 WhileStmtBits.HasVar = HasVar;
1002
1003 setCond(Cond);
1004 setBody(Body);
1005 if (HasVar)
1006 setConditionVariable(Ctx, Var);
1007
1008 setWhileLoc(WL);
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001009}
1010
Bruno Riccibacf7512018-10-30 13:42:41 +00001011WhileStmt::WhileStmt(EmptyShell Empty, bool HasVar)
1012 : Stmt(WhileStmtClass, Empty) {
1013 WhileStmtBits.HasVar = HasVar;
1014}
Chad Rosier42032fa2012-08-07 23:12:23 +00001015
Bruno Riccibacf7512018-10-30 13:42:41 +00001016WhileStmt *WhileStmt::Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
1017 Stmt *Body, SourceLocation WL) {
1018 bool HasVar = Var != nullptr;
1019 void *Mem =
1020 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1021 alignof(WhileStmt));
1022 return new (Mem) WhileStmt(Ctx, Var, Cond, Body, WL);
1023}
1024
1025WhileStmt *WhileStmt::CreateEmpty(const ASTContext &Ctx, bool HasVar) {
1026 void *Mem =
1027 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1028 alignof(WhileStmt));
1029 return new (Mem) WhileStmt(EmptyShell(), HasVar);
1030}
1031
1032VarDecl *WhileStmt::getConditionVariable() {
1033 auto *DS = getConditionVariableDeclStmt();
1034 if (!DS)
1035 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001036 return cast<VarDecl>(DS->getSingleDecl());
1037}
1038
Bruno Riccibacf7512018-10-30 13:42:41 +00001039void WhileStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
1040 assert(hasVarStorage() &&
1041 "This while statement has no storage for a condition variable!");
1042
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001043 if (!V) {
Bruno Riccibacf7512018-10-30 13:42:41 +00001044 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001045 return;
1046 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +00001047
1048 SourceRange VarRange = V->getSourceRange();
Bruno Riccibacf7512018-10-30 13:42:41 +00001049 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
1050 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001051}
1052
Ted Kremenek066dd932007-08-24 21:09:09 +00001053// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001054LabelDecl *IndirectGotoStmt::getConstantTarget() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001055 if (auto *E = dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
John McCall9de91602010-10-28 08:53:48 +00001056 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +00001057 return nullptr;
John McCall9de91602010-10-28 08:53:48 +00001058}
Ted Kremenek066dd932007-08-24 21:09:09 +00001059
Ted Kremenek066dd932007-08-24 21:09:09 +00001060// ReturnStmt
Bruno Ricci023b1d12018-10-30 14:40:49 +00001061ReturnStmt::ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1062 : Stmt(ReturnStmtClass), RetExpr(E) {
1063 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1064 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1065 if (HasNRVOCandidate)
1066 setNRVOCandidate(NRVOCandidate);
1067 setReturnLoc(RL);
Ted Kremenekc6501db2008-06-17 03:11:08 +00001068}
Bruno Ricci023b1d12018-10-30 14:40:49 +00001069
1070ReturnStmt::ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate)
1071 : Stmt(ReturnStmtClass, Empty) {
1072 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1073}
1074
1075ReturnStmt *ReturnStmt::Create(const ASTContext &Ctx, SourceLocation RL,
1076 Expr *E, const VarDecl *NRVOCandidate) {
1077 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1078 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1079 alignof(ReturnStmt));
1080 return new (Mem) ReturnStmt(RL, E, NRVOCandidate);
1081}
1082
1083ReturnStmt *ReturnStmt::CreateEmpty(const ASTContext &Ctx,
1084 bool HasNRVOCandidate) {
1085 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1086 alignof(ReturnStmt));
1087 return new (Mem) ReturnStmt(EmptyShell(), HasNRVOCandidate);
Ted Kremenek066dd932007-08-24 21:09:09 +00001088}
John Wiegley1c0675e2011-04-28 01:08:34 +00001089
Bruno Ricci5b30571752018-10-28 12:30:53 +00001090// CaseStmt
1091CaseStmt *CaseStmt::Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1092 SourceLocation caseLoc, SourceLocation ellipsisLoc,
1093 SourceLocation colonLoc) {
1094 bool CaseStmtIsGNURange = rhs != nullptr;
1095 void *Mem = Ctx.Allocate(
1096 totalSizeToAlloc<Stmt *, SourceLocation>(
1097 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1098 alignof(CaseStmt));
1099 return new (Mem) CaseStmt(lhs, rhs, caseLoc, ellipsisLoc, colonLoc);
1100}
1101
1102CaseStmt *CaseStmt::CreateEmpty(const ASTContext &Ctx,
1103 bool CaseStmtIsGNURange) {
1104 void *Mem = Ctx.Allocate(
1105 totalSizeToAlloc<Stmt *, SourceLocation>(
1106 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1107 alignof(CaseStmt));
1108 return new (Mem) CaseStmt(EmptyShell(), CaseStmtIsGNURange);
1109}
1110
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001111SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001112 Stmt *Handler)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001113 : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc) {
Warren Huntf6be4cb2014-07-25 20:52:51 +00001114 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +00001115 Children[HANDLER] = Handler;
1116}
1117
Warren Huntf6be4cb2014-07-25 20:52:51 +00001118SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +00001119 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001120 Stmt *Handler) {
1121 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +00001122}
1123
1124SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1125 return dyn_cast<SEHExceptStmt>(getHandler());
1126}
1127
1128SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1129 return dyn_cast<SEHFinallyStmt>(getHandler());
1130}
1131
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001132SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block)
1133 : Stmt(SEHExceptStmtClass), Loc(Loc) {
Pavel Labath515f4db2013-09-03 14:41:16 +00001134 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +00001135 Children[BLOCK] = Block;
1136}
1137
Craig Toppere6960e22013-08-22 05:28:54 +00001138SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
1139 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +00001140 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1141}
1142
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001143SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, Stmt *Block)
1144 : Stmt(SEHFinallyStmtClass), Loc(Loc), Block(Block) {}
John Wiegley1c0675e2011-04-28 01:08:34 +00001145
Craig Toppere6960e22013-08-22 05:28:54 +00001146SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +00001147 Stmt *Block) {
1148 return new(C)SEHFinallyStmt(Loc,Block);
1149}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001150
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001151CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
1152 VarDecl *Var)
1153 : VarAndKind(Var, Kind), Loc(Loc) {
1154 switch (Kind) {
1155 case VCK_This:
1156 assert(!Var && "'this' capture cannot have a variable!");
1157 break;
1158 case VCK_ByRef:
1159 assert(Var && "capturing by reference must have a variable!");
1160 break;
1161 case VCK_ByCopy:
1162 assert(Var && "capturing by copy must have a variable!");
1163 assert(
1164 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
1165 Var->getType()
1166 ->castAs<ReferenceType>()
1167 ->getPointeeType()
1168 ->isScalarType())) &&
1169 "captures by copy are expected to have a scalar type!");
1170 break;
1171 case VCK_VLAType:
1172 assert(!Var &&
1173 "Variable-length array type capture cannot have a variable!");
1174 break;
1175 }
1176}
1177
Chandler Carruth21c90602015-12-30 03:24:14 +00001178CapturedStmt::VariableCaptureKind
1179CapturedStmt::Capture::getCaptureKind() const {
1180 return VarAndKind.getInt();
1181}
1182
1183VarDecl *CapturedStmt::Capture::getCapturedVar() const {
1184 assert((capturesVariable() || capturesVariableByCopy()) &&
1185 "No variable available for 'this' or VAT capture");
1186 return VarAndKind.getPointer();
1187}
1188
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001189CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1190 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1191
1192 // Offset of the first Capture object.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001193 unsigned FirstCaptureOffset = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001194
1195 return reinterpret_cast<Capture *>(
1196 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1197 + FirstCaptureOffset);
1198}
1199
Wei Pan17fbf6e2013-05-04 03:59:06 +00001200CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1201 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001202 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001203 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001204 RecordDecl *RD)
1205 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001206 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001207 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001208 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001209 assert(RD && "null record declaration for captured statement");
1210
1211 // Copy initialization expressions.
1212 Stmt **Stored = getStoredStmts();
1213 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1214 *Stored++ = CaptureInits[I];
1215
1216 // Copy the statement being captured.
1217 *Stored = S;
1218
1219 // Copy all Capture objects.
1220 Capture *Buffer = getStoredCaptures();
1221 std::copy(Captures.begin(), Captures.end(), Buffer);
1222}
1223
1224CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1225 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001226 CapDeclAndKind(nullptr, CR_Default) {
Craig Topper36250ad2014-05-12 05:36:57 +00001227 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001228}
1229
Craig Toppere6960e22013-08-22 05:28:54 +00001230CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001231 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001232 ArrayRef<Capture> Captures,
1233 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001234 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001235 RecordDecl *RD) {
1236 // The layout is
1237 //
1238 // -----------------------------------------------------------
1239 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1240 // ----------------^-------------------^----------------------
1241 // getStoredStmts() getStoredCaptures()
1242 //
1243 // where S is the statement being captured.
1244 //
1245 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1246
1247 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1248 if (!Captures.empty()) {
1249 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001250 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001251 Size += sizeof(Capture) * Captures.size();
1252 }
1253
1254 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001255 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001256}
1257
Craig Toppere6960e22013-08-22 05:28:54 +00001258CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001259 unsigned NumCaptures) {
1260 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1261 if (NumCaptures > 0) {
1262 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001263 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001264 Size += sizeof(Capture) * NumCaptures;
1265 }
1266
1267 void *Mem = Context.Allocate(Size);
1268 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1269}
1270
1271Stmt::child_range CapturedStmt::children() {
Simon Pilgrim2c518802017-03-30 14:13:19 +00001272 // Children are captured field initializers.
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001273 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001274}
1275
Bruno Ricci06186502019-04-12 15:36:02 +00001276Stmt::const_child_range CapturedStmt::children() const {
1277 return const_child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1278}
1279
Chandler Carruth21c90602015-12-30 03:24:14 +00001280CapturedDecl *CapturedStmt::getCapturedDecl() {
1281 return CapDeclAndKind.getPointer();
1282}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001283
Chandler Carruth21c90602015-12-30 03:24:14 +00001284const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1285 return CapDeclAndKind.getPointer();
1286}
1287
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001288/// Set the outlined function declaration.
Chandler Carruth21c90602015-12-30 03:24:14 +00001289void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1290 assert(D && "null CapturedDecl");
1291 CapDeclAndKind.setPointer(D);
1292}
1293
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001294/// Retrieve the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001295CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1296 return CapDeclAndKind.getInt();
1297}
1298
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001299/// Set the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001300void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1301 CapDeclAndKind.setInt(Kind);
1302}
1303
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001304bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001305 for (const auto &I : captures()) {
Alexey Bataev2c845412017-05-15 16:26:15 +00001306 if (!I.capturesVariable() && !I.capturesVariableByCopy())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001307 continue;
Alexey Bataeve85de8f2017-09-20 20:11:31 +00001308 if (I.getCapturedVar()->getCanonicalDecl() == Var->getCanonicalDecl())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001309 return true;
1310 }
1311
1312 return false;
1313}