blob: 25078e7b00faecb61783327960d1b0f515e6c168 [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"
Saar Raza0f50d72020-01-18 09:11:43 +020019#include "clang/AST/ExprConcepts.h"
Chris Lattner29375652006-12-04 18:06:35 +000020#include "clang/AST/ExprCXX.h"
Steve Naroff021ca182008-05-29 21:12:08 +000021#include "clang/AST/ExprObjC.h"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000022#include "clang/AST/ExprOpenMP.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000023#include "clang/AST/StmtCXX.h"
24#include "clang/AST/StmtObjC.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000025#include "clang/AST/StmtOpenMP.h"
Sebastian Redl54c04d42008-12-22 19:15:10 +000026#include "clang/AST/Type.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000027#include "clang/Basic/CharInfo.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000028#include "clang/Basic/LLVM.h"
29#include "clang/Basic/SourceLocation.h"
Chris Lattner1a8f3942010-04-23 16:29:58 +000030#include "clang/Basic/TargetInfo.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000031#include "clang/Lex/Token.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000032#include "llvm/ADT/SmallVector.h"
Chad Rosier14836ba2012-08-24 17:05:45 +000033#include "llvm/ADT/StringExtras.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000034#include "llvm/ADT/StringRef.h"
35#include "llvm/Support/Casting.h"
36#include "llvm/Support/Compiler.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/MathExtras.h"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000039#include "llvm/Support/raw_ostream.h"
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000040#include <algorithm>
41#include <cassert>
42#include <cstring>
43#include <string>
44#include <utility>
Bruno Riccic397a262019-08-27 11:35:49 +000045#include <type_traits>
Eugene Zelenko1eab6c12017-11-14 23:35:42 +000046
Chris Lattnerf42cce72006-10-25 04:09:21 +000047using namespace clang;
48
Steve Narofff84d11f2007-05-23 21:48:04 +000049static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000050 const char *Name;
51 unsigned Counter;
52 unsigned Size;
Alexis Hunt656bb312010-05-05 15:24:00 +000053} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner4d15a0d2007-08-25 01:42:24 +000054
55static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
56 static bool Initialized = false;
57 if (Initialized)
58 return StmtClassInfo[E];
59
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +000060 // Initialize the table on the first use.
Chris Lattner4d15a0d2007-08-25 01:42:24 +000061 Initialized = true;
Alexis Huntabb2ac82010-05-18 06:22:21 +000062#define ABSTRACT_STMT(STMT)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000063#define STMT(CLASS, PARENT) \
64 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
65 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Alexis Hunt656bb312010-05-05 15:24:00 +000066#include "clang/AST/StmtNodes.inc"
Nico Weberde565e32008-08-05 23:15:29 +000067
Chris Lattner4d15a0d2007-08-25 01:42:24 +000068 return StmtClassInfo[E];
69}
70
Craig Topper37932912013-08-18 10:09:15 +000071void *Stmt::operator new(size_t bytes, const ASTContext& C,
Craig Topper5a050012013-08-18 17:45:38 +000072 unsigned alignment) {
Benjamin Kramerea70eb32012-12-01 15:09:41 +000073 return ::operator new(bytes, C, alignment);
74}
75
Steve Narofff1e53692007-03-23 22:27:02 +000076const char *Stmt::getStmtClassName() const {
John McCall925b16622010-10-26 08:39:16 +000077 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000078}
Steve Narofff84d11f2007-05-23 21:48:04 +000079
Bruno Ricci65034b82018-12-04 16:04:19 +000080// Check that no statement / expression class is polymorphic. LLVM style RTTI
81// should be used instead. If absolutely needed an exception can still be added
82// here by defining the appropriate macro (but please don't do this).
83#define STMT(CLASS, PARENT) \
84 static_assert(!std::is_polymorphic<CLASS>::value, \
85 #CLASS " should not be polymorphic!");
86#include "clang/AST/StmtNodes.inc"
87
Bruno Riccic397a262019-08-27 11:35:49 +000088// Check that no statement / expression class has a non-trival destructor.
89// Statements and expressions are allocated with the BumpPtrAllocator from
90// ASTContext and therefore their destructor is not executed.
91#define STMT(CLASS, PARENT) \
92 static_assert(std::is_trivially_destructible<CLASS>::value, \
93 #CLASS " should be trivially destructible!");
94// FIXME: InitListExpr is not trivially destructible due to its ASTVector.
95#define INITLISTEXPR(CLASS, PARENT)
96#include "clang/AST/StmtNodes.inc"
97
Steve Narofff84d11f2007-05-23 21:48:04 +000098void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000099 // Ensure the table is primed.
100 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +0000101
Steve Narofff84d11f2007-05-23 21:48:04 +0000102 unsigned sum = 0;
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000103 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Alexis Hunt656bb312010-05-05 15:24:00 +0000104 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +0000105 if (StmtClassInfo[i].Name == nullptr) continue;
Chris Lattner4d15a0d2007-08-25 01:42:24 +0000106 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +0000107 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000108 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Steve Narofff84d11f2007-05-23 21:48:04 +0000109 sum = 0;
Alexis Hunt656bb312010-05-05 15:24:00 +0000110 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +0000111 if (StmtClassInfo[i].Name == nullptr) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +0000112 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000113 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
114 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
115 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
116 << " bytes)\n";
Chris Lattner4d15a0d2007-08-25 01:42:24 +0000117 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +0000118 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000119
120 llvm::errs() << "Total bytes = " << sum << "\n";
Steve Narofff84d11f2007-05-23 21:48:04 +0000121}
122
123void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +0000124 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +0000125}
126
Daniel Dunbar62905572012-03-05 21:42:49 +0000127bool Stmt::StatisticsEnabled = false;
128void Stmt::EnableStatistics() {
129 StatisticsEnabled = true;
Steve Narofff84d11f2007-05-23 21:48:04 +0000130}
131
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000132/// Skip no-op (attributed, compound) container stmts and skip captured
Alexander Musmana5f070a2014-10-01 06:03:56 +0000133/// stmt at the top, if \a IgnoreCaptured is true.
134Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
135 Stmt *S = this;
136 if (IgnoreCaptured)
137 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
138 S = CapS->getCapturedStmt();
139 while (true) {
140 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
141 S = AS->getSubStmt();
142 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
143 if (CS->size() != 1)
144 break;
145 S = CS->body_back();
146 } else
147 break;
148 }
149 return S;
150}
151
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000152/// Strip off all label-like statements.
Chandler Carrutha626d642011-09-10 00:02:34 +0000153///
Richard Smithc202b282012-04-14 00:33:13 +0000154/// This will strip off label statements, case statements, attributed
155/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000156const Stmt *Stmt::stripLabelLikeStatements() const {
157 const Stmt *S = this;
158 while (true) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000159 if (const auto *LS = dyn_cast<LabelStmt>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000160 S = LS->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000161 else if (const auto *SC = dyn_cast<SwitchCase>(S))
Chandler Carrutha626d642011-09-10 00:02:34 +0000162 S = SC->getSubStmt();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000163 else if (const auto *AS = dyn_cast<AttributedStmt>(S))
Richard Smithc202b282012-04-14 00:33:13 +0000164 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000165 else
166 return S;
167 }
168}
169
John McCallbd066782011-02-09 08:16:59 +0000170namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000171
John McCallbd066782011-02-09 08:16:59 +0000172 struct good {};
173 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000174
175 // These silly little functions have to be static inline to suppress
176 // unused warnings, and they have to be defined to suppress other
177 // warnings.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000178 static good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000179
180 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000181 template <class T> good implements_children(children_t T::*) {
182 return good();
183 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000184 LLVM_ATTRIBUTE_UNUSED
Eugene Zelenko7855e772018-04-03 00:11:50 +0000185 static bad implements_children(children_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000186 return bad();
187 }
John McCallbd066782011-02-09 08:16:59 +0000188
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000189 typedef SourceLocation getBeginLoc_t() const;
190 template <class T> good implements_getBeginLoc(getBeginLoc_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000191 return good();
192 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000193 LLVM_ATTRIBUTE_UNUSED
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000194 static bad implements_getBeginLoc(getBeginLoc_t Stmt::*) { return bad(); }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000195
196 typedef SourceLocation getLocEnd_t() const;
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000197 template <class T> good implements_getEndLoc(getLocEnd_t T::*) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000198 return good();
199 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000200 LLVM_ATTRIBUTE_UNUSED
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000201 static bad implements_getEndLoc(getLocEnd_t Stmt::*) { return bad(); }
John McCallbd066782011-02-09 08:16:59 +0000202
203#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000204 (void) is_good(implements_children(&type::children))
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000205#define ASSERT_IMPLEMENTS_getBeginLoc(type) \
206 (void)is_good(implements_getBeginLoc(&type::getBeginLoc))
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000207#define ASSERT_IMPLEMENTS_getEndLoc(type) \
208 (void)is_good(implements_getEndLoc(&type::getEndLoc))
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000209
210} // namespace
John McCallbd066782011-02-09 08:16:59 +0000211
212/// Check whether the various Stmt classes implement their member
213/// functions.
Eli Friedmandc41d792013-09-10 22:57:15 +0000214LLVM_ATTRIBUTE_UNUSED
John McCallbd066782011-02-09 08:16:59 +0000215static inline void check_implementations() {
216#define ABSTRACT_STMT(type)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000217#define STMT(type, base) \
218 ASSERT_IMPLEMENTS_children(type); \
219 ASSERT_IMPLEMENTS_getBeginLoc(type); \
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000220 ASSERT_IMPLEMENTS_getEndLoc(type);
John McCallbd066782011-02-09 08:16:59 +0000221#include "clang/AST/StmtNodes.inc"
222}
223
224Stmt::child_range Stmt::children() {
225 switch (getStmtClass()) {
226 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
227#define ABSTRACT_STMT(type)
228#define STMT(type, base) \
229 case Stmt::type##Class: \
230 return static_cast<type*>(this)->children();
231#include "clang/AST/StmtNodes.inc"
232 }
233 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000234}
235
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000236// Amusing macro metaprogramming hack: check whether a class provides
237// a more specific implementation of getSourceRange.
238//
239// See also Expr.cpp:getExprLoc().
240namespace {
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000241
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000242 /// This implementation is used when a class provides a custom
243 /// implementation of getSourceRange.
244 template <class S, class T>
245 SourceRange getSourceRangeImpl(const Stmt *stmt,
246 SourceRange (T::*v)() const) {
247 return static_cast<const S*>(stmt)->getSourceRange();
248 }
249
250 /// This implementation is used when a class doesn't provide a custom
251 /// implementation of getSourceRange. Overload resolution should pick it over
252 /// the implementation above because it's more specialized according to
253 /// function template partial ordering.
254 template <class S>
255 SourceRange getSourceRangeImpl(const Stmt *stmt,
256 SourceRange (Stmt::*v)() const) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000257 return SourceRange(static_cast<const S *>(stmt)->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000258 static_cast<const S *>(stmt)->getEndLoc());
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000259 }
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000260
261} // namespace
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000262
John McCallbd066782011-02-09 08:16:59 +0000263SourceRange Stmt::getSourceRange() const {
264 switch (getStmtClass()) {
265 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
266#define ABSTRACT_STMT(type)
267#define STMT(type, base) \
268 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000269 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000270#include "clang/AST/StmtNodes.inc"
271 }
272 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000273}
274
Stephen Kelly724e9e52018-08-09 20:05:03 +0000275SourceLocation Stmt::getBeginLoc() const {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000276 switch (getStmtClass()) {
277 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
278#define ABSTRACT_STMT(type)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000279#define STMT(type, base) \
280 case Stmt::type##Class: \
281 return static_cast<const type *>(this)->getBeginLoc();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000282#include "clang/AST/StmtNodes.inc"
283 }
284 llvm_unreachable("unknown statement kind");
285}
286
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000287SourceLocation Stmt::getEndLoc() const {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000288 switch (getStmtClass()) {
289 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
290#define ABSTRACT_STMT(type)
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000291#define STMT(type, base) \
292 case Stmt::type##Class: \
293 return static_cast<const type *>(this)->getEndLoc();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000294#include "clang/AST/StmtNodes.inc"
295 }
296 llvm_unreachable("unknown statement kind");
297}
298
George Karpenkov5eb4cc62018-09-15 02:01:47 +0000299int64_t Stmt::getID(const ASTContext &Context) const {
Artem Dergachev057647d2018-12-03 22:19:05 +0000300 return Context.getAllocator().identifyKnownAlignedObject<Stmt>(this);
George Karpenkov5eb4cc62018-09-15 02:01:47 +0000301}
302
Benjamin Kramer07420902017-12-24 16:24:20 +0000303CompoundStmt::CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB,
304 SourceLocation RB)
Bruno Ricci41d11c02018-10-27 18:43:27 +0000305 : Stmt(CompoundStmtClass), RBraceLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000306 CompoundStmtBits.NumStmts = Stmts.size();
Benjamin Kramer07420902017-12-24 16:24:20 +0000307 setStmts(Stmts);
Bruno Ricci41d11c02018-10-27 18:43:27 +0000308 CompoundStmtBits.LBraceLoc = LB;
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000309}
310
Benjamin Kramer07420902017-12-24 16:24:20 +0000311void CompoundStmt::setStmts(ArrayRef<Stmt *> Stmts) {
Craig Topper9ee84ad2015-12-04 05:01:44 +0000312 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
313 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
Douglas Gregora9af1d12009-04-17 00:04:06 +0000314
Benjamin Kramer07420902017-12-24 16:24:20 +0000315 std::copy(Stmts.begin(), Stmts.end(), body_begin());
316}
317
318CompoundStmt *CompoundStmt::Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
319 SourceLocation LB, SourceLocation RB) {
320 void *Mem =
321 C.Allocate(totalSizeToAlloc<Stmt *>(Stmts.size()), alignof(CompoundStmt));
322 return new (Mem) CompoundStmt(Stmts, LB, RB);
323}
324
325CompoundStmt *CompoundStmt::CreateEmpty(const ASTContext &C,
326 unsigned NumStmts) {
327 void *Mem =
328 C.Allocate(totalSizeToAlloc<Stmt *>(NumStmts), alignof(CompoundStmt));
329 CompoundStmt *New = new (Mem) CompoundStmt(EmptyShell());
330 New->CompoundStmtBits.NumStmts = NumStmts;
331 return New;
Douglas Gregora9af1d12009-04-17 00:04:06 +0000332}
Steve Narofff84d11f2007-05-23 21:48:04 +0000333
Richard Smitha6e8d5e2019-02-15 00:27:53 +0000334const Expr *ValueStmt::getExprStmt() const {
335 const Stmt *S = this;
336 do {
337 if (const auto *E = dyn_cast<Expr>(S))
338 return E;
339
340 if (const auto *LS = dyn_cast<LabelStmt>(S))
341 S = LS->getSubStmt();
342 else if (const auto *AS = dyn_cast<AttributedStmt>(S))
343 S = AS->getSubStmt();
344 else
345 llvm_unreachable("unknown kind of ValueStmt");
346 } while (isa<ValueStmt>(S));
347
348 return nullptr;
349}
350
Chris Lattnereefa10e2007-05-28 06:56:27 +0000351const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000352 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000353}
354
Craig Toppere6960e22013-08-22 05:28:54 +0000355AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000356 ArrayRef<const Attr*> Attrs,
357 Stmt *SubStmt) {
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000358 assert(!Attrs.empty() && "Attrs should not be empty");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000359 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(Attrs.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000360 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000361 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
362}
363
Craig Toppere6960e22013-08-22 05:28:54 +0000364AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
365 unsigned NumAttrs) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000366 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
Benjamin Kramer917fdbe2017-12-24 16:24:11 +0000367 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(NumAttrs),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000368 alignof(AttributedStmt));
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000369 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
370}
371
Craig Topperc571c812013-08-22 06:02:26 +0000372std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000373 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000374 return gccAsmStmt->generateAsmString(C);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000375 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000376 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000377 llvm_unreachable("unknown asm statement kind!");
378}
379
380StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000381 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000382 return gccAsmStmt->getOutputConstraint(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000383 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000384 return msAsmStmt->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000385 llvm_unreachable("unknown asm statement kind!");
386}
387
388const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000389 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000390 return gccAsmStmt->getOutputExpr(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000391 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000392 return msAsmStmt->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000393 llvm_unreachable("unknown asm statement kind!");
394}
395
396StringRef AsmStmt::getInputConstraint(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000397 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000398 return gccAsmStmt->getInputConstraint(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000399 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000400 return msAsmStmt->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000401 llvm_unreachable("unknown asm statement kind!");
402}
403
404const Expr *AsmStmt::getInputExpr(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000405 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000406 return gccAsmStmt->getInputExpr(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000407 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000408 return msAsmStmt->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000409 llvm_unreachable("unknown asm statement kind!");
410}
411
412StringRef AsmStmt::getClobber(unsigned i) const {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000413 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000414 return gccAsmStmt->getClobber(i);
Eugene Zelenko7855e772018-04-03 00:11:50 +0000415 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this))
Chad Rosierf70b7e22012-08-28 18:21:14 +0000416 return msAsmStmt->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000417 llvm_unreachable("unknown asm statement kind!");
418}
419
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000420/// getNumPlusOperands - Return the number of output operands that have a "+"
421/// constraint.
422unsigned AsmStmt::getNumPlusOperands() const {
423 unsigned Res = 0;
424 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
425 if (isOutputPlusConstraint(i))
426 ++Res;
427 return Res;
428}
429
Akira Hatanaka987f1862014-08-22 06:05:21 +0000430char GCCAsmStmt::AsmStringPiece::getModifier() const {
431 assert(isOperand() && "Only Operands can have modifiers.");
432 return isLetter(Str[0]) ? Str[0] : '\0';
433}
434
Chad Rosier6100ae12012-08-27 23:47:56 +0000435StringRef GCCAsmStmt::getClobber(unsigned i) const {
436 return getClobberStringLiteral(i)->getString();
437}
438
Chad Rosierde70e0e2012-08-25 00:11:56 +0000439Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000440 return cast<Expr>(Exprs[i]);
441}
Chris Lattner72bbf172009-03-10 04:59:06 +0000442
443/// getOutputConstraint - Return the constraint string for the specified
444/// output operand. All output constraints are known to be non-empty (either
445/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000446StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000447 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000448}
Chris Lattner72bbf172009-03-10 04:59:06 +0000449
Chad Rosierde70e0e2012-08-25 00:11:56 +0000450Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000451 return cast<Expr>(Exprs[i + NumOutputs]);
452}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000453
Chad Rosierde70e0e2012-08-25 00:11:56 +0000454void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000455 Exprs[i + NumOutputs] = E;
456}
457
Jennifer Yub8fee672019-06-03 15:57:25 +0000458AddrLabelExpr *GCCAsmStmt::getLabelExpr(unsigned i) const {
Bill Wendling50cac242020-02-24 18:32:50 -0800459 return cast<AddrLabelExpr>(Exprs[i + NumOutputs + NumInputs]);
Jennifer Yub8fee672019-06-03 15:57:25 +0000460}
461
462StringRef GCCAsmStmt::getLabelName(unsigned i) const {
463 return getLabelExpr(i)->getLabel()->getName();
464}
465
Chris Lattner72bbf172009-03-10 04:59:06 +0000466/// getInputConstraint - Return the specified input constraint. Unlike output
467/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000468StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000469 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000470}
471
Craig Topperc571c812013-08-22 06:02:26 +0000472void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
473 IdentifierInfo **Names,
474 StringLiteral **Constraints,
475 Stmt **Exprs,
476 unsigned NumOutputs,
477 unsigned NumInputs,
Jennifer Yub8fee672019-06-03 15:57:25 +0000478 unsigned NumLabels,
Craig Topperc571c812013-08-22 06:02:26 +0000479 StringLiteral **Clobbers,
480 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000481 this->NumOutputs = NumOutputs;
482 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000483 this->NumClobbers = NumClobbers;
Jennifer Yub8fee672019-06-03 15:57:25 +0000484 this->NumLabels = NumLabels;
485 assert(!(NumOutputs && NumLabels) && "asm goto cannot have outputs");
Anders Carlsson98323d22010-01-30 23:19:41 +0000486
Jennifer Yub8fee672019-06-03 15:57:25 +0000487 unsigned NumExprs = NumOutputs + NumInputs + NumLabels;
Chad Rosier42032fa2012-08-07 23:12:23 +0000488
Anders Carlsson98323d22010-01-30 23:19:41 +0000489 C.Deallocate(this->Names);
490 this->Names = new (C) IdentifierInfo*[NumExprs];
491 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000492
Anders Carlsson98323d22010-01-30 23:19:41 +0000493 C.Deallocate(this->Exprs);
494 this->Exprs = new (C) Stmt*[NumExprs];
495 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000496
Benjamin Kramer851f57e2019-05-30 07:21:08 +0000497 unsigned NumConstraints = NumOutputs + NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000498 C.Deallocate(this->Constraints);
Benjamin Kramer851f57e2019-05-30 07:21:08 +0000499 this->Constraints = new (C) StringLiteral*[NumConstraints];
500 std::copy(Constraints, Constraints + NumConstraints, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000501
Anders Carlsson98323d22010-01-30 23:19:41 +0000502 C.Deallocate(this->Clobbers);
503 this->Clobbers = new (C) StringLiteral*[NumClobbers];
504 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000505}
506
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000507/// getNamedOperand - Given a symbolic operand reference like %[foo],
508/// translate this into a numeric value needed to reference the same operand.
509/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000510int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000511 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000512
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000513 // Check if this is an output operand.
514 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
515 if (getOutputName(i) == SymbolicName)
516 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000519 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
520 if (getInputName(i) == SymbolicName)
521 return getNumOutputs() + NumPlusOperands + i;
522
Jennifer Yub8fee672019-06-03 15:57:25 +0000523 for (unsigned i = 0, e = getNumLabels(); i != e; ++i)
524 if (getLabelName(i) == SymbolicName)
Bill Wendling50cac242020-02-24 18:32:50 -0800525 return i + getNumOutputs() + getNumInputs();
Jennifer Yub8fee672019-06-03 15:57:25 +0000526
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000527 // Not found.
528 return -1;
529}
530
Chris Lattner35b58362009-03-10 23:21:44 +0000531/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
532/// it into pieces. If the asm string is erroneous, emit errors and return
533/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000534unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000535 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000536 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000537 const char *StrStart = Str.begin();
538 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000539 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000540
Chris Lattner35b58362009-03-10 23:21:44 +0000541 // "Simple" inline asms have no constraints or operands, just convert the asm
542 // string to escape $'s.
543 if (isSimple()) {
544 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000545 for (; CurPtr != StrEnd; ++CurPtr) {
546 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000547 case '$':
548 Result += "$$";
549 break;
550 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000551 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000552 break;
553 }
554 }
555 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000556 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000557 }
558
559 // CurStringPiece - The current string that we are building up as we scan the
560 // asm string.
561 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000562
Douglas Gregore8bbc122011-09-02 00:18:52 +0000563 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000564
Richard Smithd18ab802016-05-16 22:52:23 +0000565 unsigned LastAsmStringToken = 0;
566 unsigned LastAsmStringOffset = 0;
567
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000568 while (true) {
Chris Lattner35b58362009-03-10 23:21:44 +0000569 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000570 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000571 if (!CurStringPiece.empty())
572 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000573 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000574 }
Mike Stump11289f42009-09-09 15:08:12 +0000575
Chris Lattnera41b8472009-03-10 23:51:40 +0000576 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000577 switch (CurChar) {
578 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000579 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
580 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
581 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000582 case '%':
583 break;
584 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000585 CurStringPiece += CurChar;
586 continue;
587 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000588
Chris Lattner35b58362009-03-10 23:21:44 +0000589 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000590 if (CurPtr == StrEnd) {
591 // % at end of string is invalid (no escape).
592 DiagOffs = CurPtr-StrStart-1;
593 return diag::err_asm_invalid_escape;
594 }
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000595 // Handle escaped char and continue looping over the asm string.
Chris Lattnera41b8472009-03-10 23:51:40 +0000596 char EscapedChar = *CurPtr++;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000597 switch (EscapedChar) {
598 default:
599 break;
600 case '%': // %% -> %
601 case '{': // %{ -> {
602 case '}': // %} -> }
603 CurStringPiece += EscapedChar;
Chris Lattner35b58362009-03-10 23:21:44 +0000604 continue;
Michael Zuckerman2460bad2016-10-31 15:27:54 +0000605 case '=': // %= -> Generate a unique ID.
Chris Lattner35b58362009-03-10 23:21:44 +0000606 CurStringPiece += "${:uid}";
607 continue;
608 }
Mike Stump11289f42009-09-09 15:08:12 +0000609
Chris Lattner35b58362009-03-10 23:21:44 +0000610 // Otherwise, we have an operand. If we have accumulated a string so far,
611 // add it to the Pieces list.
612 if (!CurStringPiece.empty()) {
613 Pieces.push_back(AsmStringPiece(CurStringPiece));
614 CurStringPiece.clear();
615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Akira Hatanaka987f1862014-08-22 06:05:21 +0000617 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
618 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
619
620 const char *Begin = CurPtr - 1; // Points to the character following '%'.
621 const char *Percent = Begin - 1; // Points to '%'.
622
Jordan Rosea7d03842013-02-08 22:30:41 +0000623 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000624 if (CurPtr == StrEnd) { // Premature end.
625 DiagOffs = CurPtr-StrStart-1;
626 return diag::err_asm_invalid_escape;
627 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000628 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000629 }
Mike Stump11289f42009-09-09 15:08:12 +0000630
Akira Hatanaka987f1862014-08-22 06:05:21 +0000631 const TargetInfo &TI = C.getTargetInfo();
632 const SourceManager &SM = C.getSourceManager();
633 const LangOptions &LO = C.getLangOpts();
634
635 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000636 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000637 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000638 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000639
Chris Lattner99d892b2009-03-11 22:52:17 +0000640 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000641 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000642 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000643
Jennifer Yub8fee672019-06-03 15:57:25 +0000644 unsigned NumOperands = getNumOutputs() + getNumPlusOperands() +
645 getNumInputs() + getNumLabels();
Chris Lattner14311922009-03-11 00:23:13 +0000646 if (N >= NumOperands) {
647 DiagOffs = CurPtr-StrStart-1;
648 return diag::err_asm_invalid_operand_number;
649 }
650
Akira Hatanaka987f1862014-08-22 06:05:21 +0000651 // Str contains "x4" (Operand without the leading %).
652 std::string Str(Begin, CurPtr - Begin);
653
654 // (BeginLoc, EndLoc) represents the range of the operand we are currently
655 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000656 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
657 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
658 &LastAsmStringOffset);
659 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
660 CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken,
661 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000662
Benjamin Kramer3204b152015-05-29 19:42:19 +0000663 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Chris Lattner35b58362009-03-10 23:21:44 +0000664 continue;
665 }
Mike Stump11289f42009-09-09 15:08:12 +0000666
Akira Hatanaka987f1862014-08-22 06:05:21 +0000667 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000668 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000669 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000670
Chris Lattner3fa25c62009-03-11 00:06:36 +0000671 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000672 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000673 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000674 return diag::err_asm_unterminated_symbolic_operand_name;
675 if (NameEnd == CurPtr)
676 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000678 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000679
Chris Lattner35b58362009-03-10 23:21:44 +0000680 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000681 if (N == -1) {
682 // Verify that an operand with that name exists.
683 DiagOffs = CurPtr-StrStart;
684 return diag::err_asm_unknown_symbolic_operand_name;
685 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000686
687 // Str contains "x[foo]" (Operand without the leading %).
688 std::string Str(Begin, NameEnd + 1 - Begin);
689
690 // (BeginLoc, EndLoc) represents the range of the operand we are currently
691 // processing. Unlike Str, the range includes the leading '%'.
Richard Smithd18ab802016-05-16 22:52:23 +0000692 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
693 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
694 &LastAsmStringOffset);
695 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
696 NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken,
697 &LastAsmStringOffset);
Akira Hatanaka987f1862014-08-22 06:05:21 +0000698
Benjamin Kramer3204b152015-05-29 19:42:19 +0000699 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattner3fa25c62009-03-11 00:06:36 +0000701 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000702 continue;
703 }
Mike Stump11289f42009-09-09 15:08:12 +0000704
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000705 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000706 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000707 }
708}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000709
710/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000711std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000712 // Analyze the asm string to decompose it into its pieces. We know that Sema
713 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000714 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000715 unsigned DiagOffs;
716 AnalyzeAsmString(Pieces, C, DiagOffs);
717
718 std::string AsmString;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000719 for (const auto &Piece : Pieces) {
720 if (Piece.isString())
721 AsmString += Piece.getString();
722 else if (Piece.getModifier() == '\0')
723 AsmString += '$' + llvm::utostr(Piece.getOperandNo());
Chad Rosier14836ba2012-08-24 17:05:45 +0000724 else
Eugene Zelenko7855e772018-04-03 00:11:50 +0000725 AsmString += "${" + llvm::utostr(Piece.getOperandNo()) + ':' +
726 Piece.getModifier() + '}';
Chad Rosier14836ba2012-08-24 17:05:45 +0000727 }
728 return AsmString;
729}
Chris Lattner35b58362009-03-10 23:21:44 +0000730
Chad Rosier3b0c2602012-08-27 20:23:31 +0000731/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000732std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000733 // FIXME: This needs to be translated into the IR string representation.
Benjamin Krameradcd0262020-01-28 20:23:46 +0100734 return std::string(AsmStr);
Chad Rosier3b0c2602012-08-27 20:23:31 +0000735}
736
Chad Rosierfe31e622012-08-24 00:07:09 +0000737Expr *MSAsmStmt::getOutputExpr(unsigned i) {
738 return cast<Expr>(Exprs[i]);
739}
740
741Expr *MSAsmStmt::getInputExpr(unsigned i) {
742 return cast<Expr>(Exprs[i + NumOutputs]);
743}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000744
Chad Rosierfe31e622012-08-24 00:07:09 +0000745void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
746 Exprs[i + NumOutputs] = E;
747}
748
Chris Lattner86f5e132008-01-30 05:01:46 +0000749//===----------------------------------------------------------------------===//
750// Constructors
751//===----------------------------------------------------------------------===//
752
Craig Toppere6960e22013-08-22 05:28:54 +0000753GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
754 bool issimple, bool isvolatile, unsigned numoutputs,
755 unsigned numinputs, IdentifierInfo **names,
756 StringLiteral **constraints, Expr **exprs,
757 StringLiteral *asmstr, unsigned numclobbers,
Jennifer Yub8fee672019-06-03 15:57:25 +0000758 StringLiteral **clobbers, unsigned numlabels,
759 SourceLocation rparenloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000760 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
Jennifer Yub8fee672019-06-03 15:57:25 +0000761 numinputs, numclobbers),
762 RParenLoc(rparenloc), AsmStr(asmstr), NumLabels(numlabels) {
763 unsigned NumExprs = NumOutputs + NumInputs + NumLabels;
Chad Rosier42032fa2012-08-07 23:12:23 +0000764
Anders Carlsson98323d22010-01-30 23:19:41 +0000765 Names = new (C) IdentifierInfo*[NumExprs];
766 std::copy(names, names + NumExprs, Names);
767
768 Exprs = new (C) Stmt*[NumExprs];
769 std::copy(exprs, exprs + NumExprs, Exprs);
770
Benjamin Kramer851f57e2019-05-30 07:21:08 +0000771 unsigned NumConstraints = NumOutputs + NumInputs;
772 Constraints = new (C) StringLiteral*[NumConstraints];
773 std::copy(constraints, constraints + NumConstraints, Constraints);
Anders Carlsson98323d22010-01-30 23:19:41 +0000774
775 Clobbers = new (C) StringLiteral*[NumClobbers];
776 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000777}
778
Craig Toppere6960e22013-08-22 05:28:54 +0000779MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000780 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000781 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000782 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000783 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
784 StringRef asmstr, ArrayRef<StringRef> clobbers,
785 SourceLocation endloc)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +0000786 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
787 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
788 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
John McCallf413f5e2013-05-03 00:10:13 +0000789 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
790}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000791
Craig Toppere6960e22013-08-22 05:28:54 +0000792static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
Benjamin Kramer2ab0d882015-08-04 12:34:23 +0000793 return str.copy(C);
John McCallf413f5e2013-05-03 00:10:13 +0000794}
795
Craig Toppere6960e22013-08-22 05:28:54 +0000796void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000797 ArrayRef<Token> asmtoks,
798 ArrayRef<StringRef> constraints,
799 ArrayRef<Expr*> exprs,
800 ArrayRef<StringRef> clobbers) {
801 assert(NumAsmToks == asmtoks.size());
802 assert(NumClobbers == clobbers.size());
803
Craig Toppercaf138e2015-12-05 07:41:42 +0000804 assert(exprs.size() == NumOutputs + NumInputs);
805 assert(exprs.size() == constraints.size());
John McCallf413f5e2013-05-03 00:10:13 +0000806
807 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000808
Craig Toppercaf138e2015-12-05 07:41:42 +0000809 Exprs = new (C) Stmt*[exprs.size()];
810 std::copy(exprs.begin(), exprs.end(), Exprs);
Chad Rosierfe31e622012-08-24 00:07:09 +0000811
Craig Toppercaf138e2015-12-05 07:41:42 +0000812 AsmToks = new (C) Token[asmtoks.size()];
813 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000814
Craig Toppercaf138e2015-12-05 07:41:42 +0000815 Constraints = new (C) StringRef[exprs.size()];
816 std::transform(constraints.begin(), constraints.end(), Constraints,
817 [&](StringRef Constraint) {
818 return copyIntoContext(C, Constraint);
819 });
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000820
Chad Rosierbaf53f92012-08-10 21:36:25 +0000821 Clobbers = new (C) StringRef[NumClobbers];
Craig Toppercaf138e2015-12-05 07:41:42 +0000822 // FIXME: Avoid the allocation/copy if at all possible.
823 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
824 [&](StringRef Clobber) {
825 return copyIntoContext(C, Clobber);
826 });
Chad Rosier32503022012-06-11 20:47:18 +0000827}
828
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000829IfStmt::IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr,
Zequan Wu94c6cea2020-08-10 16:29:33 -0700830 Stmt *Init, VarDecl *Var, Expr *Cond, SourceLocation LPL,
831 SourceLocation RPL, Stmt *Then, SourceLocation EL, Stmt *Else)
832 : Stmt(IfStmtClass), LParenLoc(LPL), RParenLoc(RPL) {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000833 bool HasElse = Else != nullptr;
834 bool HasVar = Var != nullptr;
835 bool HasInit = Init != nullptr;
836 IfStmtBits.HasElse = HasElse;
837 IfStmtBits.HasVar = HasVar;
838 IfStmtBits.HasInit = HasInit;
839
Richard Smithb130fe72016-06-23 19:16:49 +0000840 setConstexpr(IsConstexpr);
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000841
842 setCond(Cond);
843 setThen(Then);
844 if (HasElse)
845 setElse(Else);
846 if (HasVar)
847 setConditionVariable(Ctx, Var);
848 if (HasInit)
849 setInit(Init);
850
851 setIfLoc(IL);
852 if (HasElse)
853 setElseLoc(EL);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000854}
855
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000856IfStmt::IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit)
857 : Stmt(IfStmtClass, Empty) {
858 IfStmtBits.HasElse = HasElse;
859 IfStmtBits.HasVar = HasVar;
860 IfStmtBits.HasInit = HasInit;
861}
Chad Rosier42032fa2012-08-07 23:12:23 +0000862
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000863IfStmt *IfStmt::Create(const ASTContext &Ctx, SourceLocation IL,
864 bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond,
Zequan Wu94c6cea2020-08-10 16:29:33 -0700865 SourceLocation LPL, SourceLocation RPL, Stmt *Then,
866 SourceLocation EL, Stmt *Else) {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000867 bool HasElse = Else != nullptr;
868 bool HasVar = Var != nullptr;
869 bool HasInit = Init != nullptr;
870 void *Mem = Ctx.Allocate(
871 totalSizeToAlloc<Stmt *, SourceLocation>(
872 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
873 alignof(IfStmt));
874 return new (Mem)
Zequan Wu94c6cea2020-08-10 16:29:33 -0700875 IfStmt(Ctx, IL, IsConstexpr, Init, Var, Cond, LPL, RPL, Then, EL, Else);
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000876}
877
878IfStmt *IfStmt::CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
879 bool HasInit) {
880 void *Mem = Ctx.Allocate(
881 totalSizeToAlloc<Stmt *, SourceLocation>(
882 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
883 alignof(IfStmt));
884 return new (Mem) IfStmt(EmptyShell(), HasElse, HasVar, HasInit);
885}
886
887VarDecl *IfStmt::getConditionVariable() {
888 auto *DS = getConditionVariableDeclStmt();
889 if (!DS)
890 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000891 return cast<VarDecl>(DS->getSingleDecl());
892}
893
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000894void IfStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
895 assert(hasVarStorage() &&
896 "This if statement has no storage for a condition variable!");
897
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000898 if (!V) {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000899 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000900 return;
901 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000902
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000903 SourceRange VarRange = V->getSourceRange();
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000904 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
905 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000906}
907
Erik Pilkington5cd57172016-08-16 17:44:11 +0000908bool IfStmt::isObjCAvailabilityCheck() const {
Bruno Riccib1cc94b2018-10-27 21:12:20 +0000909 return isa<ObjCAvailabilityCheckExpr>(getCond());
Erik Pilkington5cd57172016-08-16 17:44:11 +0000910}
911
Richard Smithfbf60b72019-12-13 14:10:13 -0800912Optional<const Stmt*> IfStmt::getNondiscardedCase(const ASTContext &Ctx) const {
913 if (!isConstexpr() || getCond()->isValueDependent())
914 return None;
915 return !getCond()->EvaluateKnownConstInt(Ctx) ? getElse() : getThen();
916}
917
Craig Toppere6960e22013-08-22 05:28:54 +0000918ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000919 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000920 SourceLocation RP)
Bruno Ricci41d11c02018-10-27 18:43:27 +0000921 : Stmt(ForStmtClass), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000922{
923 SubExprs[INIT] = Init;
924 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000925 SubExprs[COND] = Cond;
926 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000927 SubExprs[BODY] = Body;
Bruno Ricci41d11c02018-10-27 18:43:27 +0000928 ForStmtBits.ForLoc = FL;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000929}
930
931VarDecl *ForStmt::getConditionVariable() const {
932 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000933 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000934
Eugene Zelenko7855e772018-04-03 00:11:50 +0000935 auto *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000936 return cast<VarDecl>(DS->getSingleDecl());
937}
938
Craig Toppere6960e22013-08-22 05:28:54 +0000939void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000940 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000941 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000942 return;
943 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000944
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000945 SourceRange VarRange = V->getSourceRange();
946 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
947 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000948}
949
Bruno Riccie2806f82018-10-29 16:12:37 +0000950SwitchStmt::SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
Zequan Wu94c6cea2020-08-10 16:29:33 -0700951 Expr *Cond, SourceLocation LParenLoc,
952 SourceLocation RParenLoc)
953 : Stmt(SwitchStmtClass), FirstCase(nullptr), LParenLoc(LParenLoc),
954 RParenLoc(RParenLoc) {
Bruno Riccie2806f82018-10-29 16:12:37 +0000955 bool HasInit = Init != nullptr;
956 bool HasVar = Var != nullptr;
957 SwitchStmtBits.HasInit = HasInit;
958 SwitchStmtBits.HasVar = HasVar;
959 SwitchStmtBits.AllEnumCasesCovered = false;
960
961 setCond(Cond);
962 setBody(nullptr);
963 if (HasInit)
964 setInit(Init);
965 if (HasVar)
966 setConditionVariable(Ctx, Var);
967
968 setSwitchLoc(SourceLocation{});
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000969}
970
Bruno Riccie2806f82018-10-29 16:12:37 +0000971SwitchStmt::SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar)
972 : Stmt(SwitchStmtClass, Empty) {
973 SwitchStmtBits.HasInit = HasInit;
974 SwitchStmtBits.HasVar = HasVar;
975 SwitchStmtBits.AllEnumCasesCovered = false;
976}
Chad Rosier42032fa2012-08-07 23:12:23 +0000977
Bruno Riccie2806f82018-10-29 16:12:37 +0000978SwitchStmt *SwitchStmt::Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
Zequan Wu94c6cea2020-08-10 16:29:33 -0700979 Expr *Cond, SourceLocation LParenLoc,
980 SourceLocation RParenLoc) {
Bruno Riccie2806f82018-10-29 16:12:37 +0000981 bool HasInit = Init != nullptr;
982 bool HasVar = Var != nullptr;
983 void *Mem = Ctx.Allocate(
984 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
985 alignof(SwitchStmt));
Zequan Wu94c6cea2020-08-10 16:29:33 -0700986 return new (Mem) SwitchStmt(Ctx, Init, Var, Cond, LParenLoc, RParenLoc);
Bruno Riccie2806f82018-10-29 16:12:37 +0000987}
988
989SwitchStmt *SwitchStmt::CreateEmpty(const ASTContext &Ctx, bool HasInit,
990 bool HasVar) {
991 void *Mem = Ctx.Allocate(
992 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
993 alignof(SwitchStmt));
994 return new (Mem) SwitchStmt(EmptyShell(), HasInit, HasVar);
995}
996
997VarDecl *SwitchStmt::getConditionVariable() {
998 auto *DS = getConditionVariableDeclStmt();
999 if (!DS)
1000 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001001 return cast<VarDecl>(DS->getSingleDecl());
1002}
1003
Bruno Riccie2806f82018-10-29 16:12:37 +00001004void SwitchStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
1005 assert(hasVarStorage() &&
1006 "This switch statement has no storage for a condition variable!");
1007
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001008 if (!V) {
Bruno Riccie2806f82018-10-29 16:12:37 +00001009 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001010 return;
1011 }
Chad Rosier42032fa2012-08-07 23:12:23 +00001012
Daniel Dunbar62ee6412012-03-09 18:35:03 +00001013 SourceRange VarRange = V->getSourceRange();
Bruno Riccie2806f82018-10-29 16:12:37 +00001014 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
1015 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001016}
1017
Bruno Riccibacf7512018-10-30 13:42:41 +00001018WhileStmt::WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
Vy Nguyen17ea41e2020-07-09 23:19:06 -04001019 Stmt *Body, SourceLocation WL, SourceLocation LParenLoc,
1020 SourceLocation RParenLoc)
Bruno Riccibacf7512018-10-30 13:42:41 +00001021 : Stmt(WhileStmtClass) {
1022 bool HasVar = Var != nullptr;
1023 WhileStmtBits.HasVar = HasVar;
1024
1025 setCond(Cond);
1026 setBody(Body);
1027 if (HasVar)
1028 setConditionVariable(Ctx, Var);
1029
1030 setWhileLoc(WL);
Vy Nguyen17ea41e2020-07-09 23:19:06 -04001031 setLParenLoc(LParenLoc);
1032 setRParenLoc(RParenLoc);
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001033}
1034
Bruno Riccibacf7512018-10-30 13:42:41 +00001035WhileStmt::WhileStmt(EmptyShell Empty, bool HasVar)
1036 : Stmt(WhileStmtClass, Empty) {
1037 WhileStmtBits.HasVar = HasVar;
1038}
Chad Rosier42032fa2012-08-07 23:12:23 +00001039
Bruno Riccibacf7512018-10-30 13:42:41 +00001040WhileStmt *WhileStmt::Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
Vy Nguyen17ea41e2020-07-09 23:19:06 -04001041 Stmt *Body, SourceLocation WL,
1042 SourceLocation LParenLoc,
1043 SourceLocation RParenLoc) {
Bruno Riccibacf7512018-10-30 13:42:41 +00001044 bool HasVar = Var != nullptr;
1045 void *Mem =
1046 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1047 alignof(WhileStmt));
Vy Nguyen17ea41e2020-07-09 23:19:06 -04001048 return new (Mem) WhileStmt(Ctx, Var, Cond, Body, WL, LParenLoc, RParenLoc);
Bruno Riccibacf7512018-10-30 13:42:41 +00001049}
1050
1051WhileStmt *WhileStmt::CreateEmpty(const ASTContext &Ctx, bool HasVar) {
1052 void *Mem =
1053 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1054 alignof(WhileStmt));
1055 return new (Mem) WhileStmt(EmptyShell(), HasVar);
1056}
1057
1058VarDecl *WhileStmt::getConditionVariable() {
1059 auto *DS = getConditionVariableDeclStmt();
1060 if (!DS)
1061 return nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001062 return cast<VarDecl>(DS->getSingleDecl());
1063}
1064
Bruno Riccibacf7512018-10-30 13:42:41 +00001065void WhileStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
1066 assert(hasVarStorage() &&
1067 "This while statement has no storage for a condition variable!");
1068
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001069 if (!V) {
Bruno Riccibacf7512018-10-30 13:42:41 +00001070 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001071 return;
1072 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +00001073
1074 SourceRange VarRange = V->getSourceRange();
Bruno Riccibacf7512018-10-30 13:42:41 +00001075 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
1076 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +00001077}
1078
Ted Kremenek066dd932007-08-24 21:09:09 +00001079// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001080LabelDecl *IndirectGotoStmt::getConstantTarget() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001081 if (auto *E = dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
John McCall9de91602010-10-28 08:53:48 +00001082 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +00001083 return nullptr;
John McCall9de91602010-10-28 08:53:48 +00001084}
Ted Kremenek066dd932007-08-24 21:09:09 +00001085
Ted Kremenek066dd932007-08-24 21:09:09 +00001086// ReturnStmt
Bruno Ricci023b1d12018-10-30 14:40:49 +00001087ReturnStmt::ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1088 : Stmt(ReturnStmtClass), RetExpr(E) {
1089 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1090 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1091 if (HasNRVOCandidate)
1092 setNRVOCandidate(NRVOCandidate);
1093 setReturnLoc(RL);
Ted Kremenekc6501db2008-06-17 03:11:08 +00001094}
Bruno Ricci023b1d12018-10-30 14:40:49 +00001095
1096ReturnStmt::ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate)
1097 : Stmt(ReturnStmtClass, Empty) {
1098 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1099}
1100
1101ReturnStmt *ReturnStmt::Create(const ASTContext &Ctx, SourceLocation RL,
1102 Expr *E, const VarDecl *NRVOCandidate) {
1103 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1104 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1105 alignof(ReturnStmt));
1106 return new (Mem) ReturnStmt(RL, E, NRVOCandidate);
1107}
1108
1109ReturnStmt *ReturnStmt::CreateEmpty(const ASTContext &Ctx,
1110 bool HasNRVOCandidate) {
1111 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1112 alignof(ReturnStmt));
1113 return new (Mem) ReturnStmt(EmptyShell(), HasNRVOCandidate);
Ted Kremenek066dd932007-08-24 21:09:09 +00001114}
John Wiegley1c0675e2011-04-28 01:08:34 +00001115
Bruno Ricci5b30571752018-10-28 12:30:53 +00001116// CaseStmt
1117CaseStmt *CaseStmt::Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1118 SourceLocation caseLoc, SourceLocation ellipsisLoc,
1119 SourceLocation colonLoc) {
1120 bool CaseStmtIsGNURange = rhs != nullptr;
1121 void *Mem = Ctx.Allocate(
1122 totalSizeToAlloc<Stmt *, SourceLocation>(
1123 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1124 alignof(CaseStmt));
1125 return new (Mem) CaseStmt(lhs, rhs, caseLoc, ellipsisLoc, colonLoc);
1126}
1127
1128CaseStmt *CaseStmt::CreateEmpty(const ASTContext &Ctx,
1129 bool CaseStmtIsGNURange) {
1130 void *Mem = Ctx.Allocate(
1131 totalSizeToAlloc<Stmt *, SourceLocation>(
1132 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1133 alignof(CaseStmt));
1134 return new (Mem) CaseStmt(EmptyShell(), CaseStmtIsGNURange);
1135}
1136
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001137SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001138 Stmt *Handler)
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001139 : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc) {
Warren Huntf6be4cb2014-07-25 20:52:51 +00001140 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +00001141 Children[HANDLER] = Handler;
1142}
1143
Warren Huntf6be4cb2014-07-25 20:52:51 +00001144SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +00001145 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +00001146 Stmt *Handler) {
1147 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +00001148}
1149
1150SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1151 return dyn_cast<SEHExceptStmt>(getHandler());
1152}
1153
1154SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1155 return dyn_cast<SEHFinallyStmt>(getHandler());
1156}
1157
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001158SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block)
1159 : Stmt(SEHExceptStmtClass), Loc(Loc) {
Pavel Labath515f4db2013-09-03 14:41:16 +00001160 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +00001161 Children[BLOCK] = Block;
1162}
1163
Craig Toppere6960e22013-08-22 05:28:54 +00001164SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
1165 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +00001166 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1167}
1168
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001169SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, Stmt *Block)
1170 : Stmt(SEHFinallyStmtClass), Loc(Loc), Block(Block) {}
John Wiegley1c0675e2011-04-28 01:08:34 +00001171
Craig Toppere6960e22013-08-22 05:28:54 +00001172SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +00001173 Stmt *Block) {
1174 return new(C)SEHFinallyStmt(Loc,Block);
1175}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001176
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001177CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
1178 VarDecl *Var)
1179 : VarAndKind(Var, Kind), Loc(Loc) {
1180 switch (Kind) {
1181 case VCK_This:
1182 assert(!Var && "'this' capture cannot have a variable!");
1183 break;
1184 case VCK_ByRef:
1185 assert(Var && "capturing by reference must have a variable!");
1186 break;
1187 case VCK_ByCopy:
1188 assert(Var && "capturing by copy must have a variable!");
1189 assert(
1190 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
1191 Var->getType()
1192 ->castAs<ReferenceType>()
1193 ->getPointeeType()
1194 ->isScalarType())) &&
1195 "captures by copy are expected to have a scalar type!");
1196 break;
1197 case VCK_VLAType:
1198 assert(!Var &&
1199 "Variable-length array type capture cannot have a variable!");
1200 break;
1201 }
1202}
1203
Chandler Carruth21c90602015-12-30 03:24:14 +00001204CapturedStmt::VariableCaptureKind
1205CapturedStmt::Capture::getCaptureKind() const {
1206 return VarAndKind.getInt();
1207}
1208
1209VarDecl *CapturedStmt::Capture::getCapturedVar() const {
1210 assert((capturesVariable() || capturesVariableByCopy()) &&
1211 "No variable available for 'this' or VAT capture");
1212 return VarAndKind.getPointer();
1213}
1214
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001215CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1216 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1217
1218 // Offset of the first Capture object.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001219 unsigned FirstCaptureOffset = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001220
1221 return reinterpret_cast<Capture *>(
1222 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1223 + FirstCaptureOffset);
1224}
1225
Wei Pan17fbf6e2013-05-04 03:59:06 +00001226CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1227 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001228 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001229 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001230 RecordDecl *RD)
1231 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001232 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001233 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001234 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001235 assert(RD && "null record declaration for captured statement");
1236
1237 // Copy initialization expressions.
1238 Stmt **Stored = getStoredStmts();
1239 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1240 *Stored++ = CaptureInits[I];
1241
1242 // Copy the statement being captured.
1243 *Stored = S;
1244
1245 // Copy all Capture objects.
1246 Capture *Buffer = getStoredCaptures();
1247 std::copy(Captures.begin(), Captures.end(), Buffer);
1248}
1249
1250CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1251 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001252 CapDeclAndKind(nullptr, CR_Default) {
Craig Topper36250ad2014-05-12 05:36:57 +00001253 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001254}
1255
Craig Toppere6960e22013-08-22 05:28:54 +00001256CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001257 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001258 ArrayRef<Capture> Captures,
1259 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001260 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001261 RecordDecl *RD) {
1262 // The layout is
1263 //
1264 // -----------------------------------------------------------
1265 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1266 // ----------------^-------------------^----------------------
1267 // getStoredStmts() getStoredCaptures()
1268 //
1269 // where S is the statement being captured.
1270 //
1271 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1272
1273 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1274 if (!Captures.empty()) {
1275 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001276 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001277 Size += sizeof(Capture) * Captures.size();
1278 }
1279
1280 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001281 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001282}
1283
Craig Toppere6960e22013-08-22 05:28:54 +00001284CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001285 unsigned NumCaptures) {
1286 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1287 if (NumCaptures > 0) {
1288 // Realign for the following Capture array.
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001289 Size = llvm::alignTo(Size, alignof(Capture));
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001290 Size += sizeof(Capture) * NumCaptures;
1291 }
1292
1293 void *Mem = Context.Allocate(Size);
1294 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1295}
1296
1297Stmt::child_range CapturedStmt::children() {
Simon Pilgrim2c518802017-03-30 14:13:19 +00001298 // Children are captured field initializers.
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001299 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001300}
1301
Bruno Ricci06186502019-04-12 15:36:02 +00001302Stmt::const_child_range CapturedStmt::children() const {
1303 return const_child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1304}
1305
Chandler Carruth21c90602015-12-30 03:24:14 +00001306CapturedDecl *CapturedStmt::getCapturedDecl() {
1307 return CapDeclAndKind.getPointer();
1308}
Eugene Zelenko1eab6c12017-11-14 23:35:42 +00001309
Chandler Carruth21c90602015-12-30 03:24:14 +00001310const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1311 return CapDeclAndKind.getPointer();
1312}
1313
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001314/// Set the outlined function declaration.
Chandler Carruth21c90602015-12-30 03:24:14 +00001315void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1316 assert(D && "null CapturedDecl");
1317 CapDeclAndKind.setPointer(D);
1318}
1319
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001320/// Retrieve the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001321CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1322 return CapDeclAndKind.getInt();
1323}
1324
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001325/// Set the captured region kind.
Chandler Carruth21c90602015-12-30 03:24:14 +00001326void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1327 CapDeclAndKind.setInt(Kind);
1328}
1329
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001330bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001331 for (const auto &I : captures()) {
Alexey Bataev2c845412017-05-15 16:26:15 +00001332 if (!I.capturesVariable() && !I.capturesVariableByCopy())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001333 continue;
Alexey Bataeve85de8f2017-09-20 20:11:31 +00001334 if (I.getCapturedVar()->getCanonicalDecl() == Var->getCanonicalDecl())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001335 return true;
1336 }
1337
1338 return false;
1339}