Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 1 | //===- Stmt.cpp - Statement AST Node Implementation -----------------------===// |
Chris Lattner | f42cce7 | 2006-10-25 04:09:21 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Lattner | f42cce7 | 2006-10-25 04:09:21 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Stmt class and statement subclasses. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 13 | #include "clang/AST/Stmt.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/ASTDiagnostic.h" |
Mark de Wever | 08196e0 | 2020-09-09 19:12:32 +0200 | [diff] [blame^] | 16 | #include "clang/AST/Attr.h" |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/DeclGroup.h" |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Mark de Wever | 08196e0 | 2020-09-09 19:12:32 +0200 | [diff] [blame^] | 21 | #include "clang/AST/ExprConcepts.h" |
Steve Naroff | 021ca18 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprOpenMP.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
| 25 | #include "clang/AST/StmtObjC.h" |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 26 | #include "clang/AST/StmtOpenMP.h" |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 27 | #include "clang/AST/Type.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 28 | #include "clang/Basic/CharInfo.h" |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 29 | #include "clang/Basic/LLVM.h" |
| 30 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | 1a8f394 | 2010-04-23 16:29:58 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 32 | #include "clang/Lex/Token.h" |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallVector.h" |
Chad Rosier | 14836ba | 2012-08-24 17:05:45 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/StringRef.h" |
| 36 | #include "llvm/Support/Casting.h" |
| 37 | #include "llvm/Support/Compiler.h" |
| 38 | #include "llvm/Support/ErrorHandling.h" |
| 39 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | bfb154a | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 41 | #include <algorithm> |
| 42 | #include <cassert> |
| 43 | #include <cstring> |
| 44 | #include <string> |
Bruno Ricci | c397a26 | 2019-08-27 11:35:49 +0000 | [diff] [blame] | 45 | #include <type_traits> |
Mark de Wever | 08196e0 | 2020-09-09 19:12:32 +0200 | [diff] [blame^] | 46 | #include <utility> |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 47 | |
Chris Lattner | f42cce7 | 2006-10-25 04:09:21 +0000 | [diff] [blame] | 48 | using namespace clang; |
| 49 | |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 50 | static struct StmtClassNameTable { |
Chris Lattner | 4d15a0d | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 51 | const char *Name; |
| 52 | unsigned Counter; |
| 53 | unsigned Size; |
Alexis Hunt | 656bb31 | 2010-05-05 15:24:00 +0000 | [diff] [blame] | 54 | } StmtClassInfo[Stmt::lastStmtConstant+1]; |
Chris Lattner | 4d15a0d | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 55 | |
| 56 | static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) { |
| 57 | static bool Initialized = false; |
| 58 | if (Initialized) |
| 59 | return StmtClassInfo[E]; |
| 60 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 61 | // Initialize the table on the first use. |
Chris Lattner | 4d15a0d | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 62 | Initialized = true; |
Alexis Hunt | abb2ac8 | 2010-05-18 06:22:21 +0000 | [diff] [blame] | 63 | #define ABSTRACT_STMT(STMT) |
Douglas Gregor | be35ce9 | 2008-11-14 12:46:07 +0000 | [diff] [blame] | 64 | #define STMT(CLASS, PARENT) \ |
| 65 | StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \ |
| 66 | StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS); |
Alexis Hunt | 656bb31 | 2010-05-05 15:24:00 +0000 | [diff] [blame] | 67 | #include "clang/AST/StmtNodes.inc" |
Nico Weber | de565e3 | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 4d15a0d | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 69 | return StmtClassInfo[E]; |
| 70 | } |
| 71 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 72 | void *Stmt::operator new(size_t bytes, const ASTContext& C, |
Craig Topper | 5a05001 | 2013-08-18 17:45:38 +0000 | [diff] [blame] | 73 | unsigned alignment) { |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 74 | return ::operator new(bytes, C, alignment); |
| 75 | } |
| 76 | |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 77 | const char *Stmt::getStmtClassName() const { |
John McCall | 925b1662 | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 78 | return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 79 | } |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 80 | |
Bruno Ricci | 65034b8 | 2018-12-04 16:04:19 +0000 | [diff] [blame] | 81 | // Check that no statement / expression class is polymorphic. LLVM style RTTI |
| 82 | // should be used instead. If absolutely needed an exception can still be added |
| 83 | // here by defining the appropriate macro (but please don't do this). |
| 84 | #define STMT(CLASS, PARENT) \ |
| 85 | static_assert(!std::is_polymorphic<CLASS>::value, \ |
| 86 | #CLASS " should not be polymorphic!"); |
| 87 | #include "clang/AST/StmtNodes.inc" |
| 88 | |
Bruno Ricci | c397a26 | 2019-08-27 11:35:49 +0000 | [diff] [blame] | 89 | // Check that no statement / expression class has a non-trival destructor. |
| 90 | // Statements and expressions are allocated with the BumpPtrAllocator from |
| 91 | // ASTContext and therefore their destructor is not executed. |
| 92 | #define STMT(CLASS, PARENT) \ |
| 93 | static_assert(std::is_trivially_destructible<CLASS>::value, \ |
| 94 | #CLASS " should be trivially destructible!"); |
| 95 | // FIXME: InitListExpr is not trivially destructible due to its ASTVector. |
| 96 | #define INITLISTEXPR(CLASS, PARENT) |
| 97 | #include "clang/AST/StmtNodes.inc" |
| 98 | |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 99 | void Stmt::PrintStats() { |
Chris Lattner | 4d15a0d | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 100 | // Ensure the table is primed. |
| 101 | getStmtInfoTableEntry(Stmt::NullStmtClass); |
Nico Weber | de565e3 | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 102 | |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 103 | unsigned sum = 0; |
Chandler Carruth | bfb154a | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 104 | llvm::errs() << "\n*** Stmt/Expr Stats:\n"; |
Alexis Hunt | 656bb31 | 2010-05-05 15:24:00 +0000 | [diff] [blame] | 105 | for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 106 | if (StmtClassInfo[i].Name == nullptr) continue; |
Chris Lattner | 4d15a0d | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 107 | sum += StmtClassInfo[i].Counter; |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 108 | } |
Chandler Carruth | bfb154a | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 109 | llvm::errs() << " " << sum << " stmts/exprs total.\n"; |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 110 | sum = 0; |
Alexis Hunt | 656bb31 | 2010-05-05 15:24:00 +0000 | [diff] [blame] | 111 | for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 112 | if (StmtClassInfo[i].Name == nullptr) continue; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 113 | if (StmtClassInfo[i].Counter == 0) continue; |
Chandler Carruth | bfb154a | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 114 | llvm::errs() << " " << StmtClassInfo[i].Counter << " " |
| 115 | << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size |
| 116 | << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size |
| 117 | << " bytes)\n"; |
Chris Lattner | 4d15a0d | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 118 | sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size; |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 119 | } |
Chandler Carruth | bfb154a | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 120 | |
| 121 | llvm::errs() << "Total bytes = " << sum << "\n"; |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void Stmt::addStmtClass(StmtClass s) { |
Chris Lattner | 4d15a0d | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 125 | ++getStmtInfoTableEntry(s).Counter; |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Daniel Dunbar | 6290557 | 2012-03-05 21:42:49 +0000 | [diff] [blame] | 128 | bool Stmt::StatisticsEnabled = false; |
| 129 | void Stmt::EnableStatistics() { |
| 130 | StatisticsEnabled = true; |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Mark de Wever | 08196e0 | 2020-09-09 19:12:32 +0200 | [diff] [blame^] | 133 | static std::pair<Stmt::Likelihood, const Attr *> getLikelihood(const Stmt *S) { |
| 134 | if (const auto *AS = dyn_cast_or_null<AttributedStmt>(S)) |
| 135 | for (const auto *A : AS->getAttrs()) { |
| 136 | if (isa<LikelyAttr>(A)) |
| 137 | return std::make_pair(Stmt::LH_Likely, A); |
| 138 | |
| 139 | if (isa<UnlikelyAttr>(A)) |
| 140 | return std::make_pair(Stmt::LH_Unlikely, A); |
| 141 | } |
| 142 | |
| 143 | return std::make_pair(Stmt::LH_None, nullptr); |
| 144 | } |
| 145 | |
| 146 | Stmt::Likelihood Stmt::getLikelihood(const Stmt *S) { |
| 147 | return ::getLikelihood(S).first; |
| 148 | } |
| 149 | |
| 150 | Stmt::Likelihood Stmt::getLikelihood(const Stmt *Then, const Stmt *Else) { |
| 151 | Likelihood LHT = ::getLikelihood(Then).first; |
| 152 | Likelihood LHE = ::getLikelihood(Else).first; |
| 153 | if (LHE == LH_None) |
| 154 | return LHT; |
| 155 | |
| 156 | // If the same attribute is used on both branches there's a conflict. |
| 157 | if (LHT == LHE) |
| 158 | return LH_None; |
| 159 | |
| 160 | if (LHT != LH_None) |
| 161 | return LHT; |
| 162 | |
| 163 | // Invert the value of Else to get the value for Then. |
| 164 | return LHE == LH_Likely ? LH_Unlikely : LH_Likely; |
| 165 | } |
| 166 | |
| 167 | std::tuple<bool, const Attr *, const Attr *> |
| 168 | Stmt::determineLikelihoodConflict(const Stmt *Then, const Stmt *Else) { |
| 169 | std::pair<Likelihood, const Attr *> LHT = ::getLikelihood(Then); |
| 170 | std::pair<Likelihood, const Attr *> LHE = ::getLikelihood(Else); |
| 171 | // If the same attribute is used on both branches there's a conflict. |
| 172 | if (LHT.first != LH_None && LHT.first == LHE.first) |
| 173 | return std::make_tuple(true, LHT.second, LHE.second); |
| 174 | |
| 175 | return std::make_tuple(false, nullptr, nullptr); |
| 176 | } |
| 177 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 178 | /// Skip no-op (attributed, compound) container stmts and skip captured |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 179 | /// stmt at the top, if \a IgnoreCaptured is true. |
| 180 | Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) { |
| 181 | Stmt *S = this; |
| 182 | if (IgnoreCaptured) |
| 183 | if (auto CapS = dyn_cast_or_null<CapturedStmt>(S)) |
| 184 | S = CapS->getCapturedStmt(); |
| 185 | while (true) { |
| 186 | if (auto AS = dyn_cast_or_null<AttributedStmt>(S)) |
| 187 | S = AS->getSubStmt(); |
| 188 | else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) { |
| 189 | if (CS->size() != 1) |
| 190 | break; |
| 191 | S = CS->body_back(); |
| 192 | } else |
| 193 | break; |
| 194 | } |
| 195 | return S; |
| 196 | } |
| 197 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 198 | /// Strip off all label-like statements. |
Chandler Carruth | a626d64 | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 199 | /// |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 200 | /// This will strip off label statements, case statements, attributed |
| 201 | /// statements and default statements recursively. |
Chandler Carruth | a626d64 | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 202 | const Stmt *Stmt::stripLabelLikeStatements() const { |
| 203 | const Stmt *S = this; |
| 204 | while (true) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 205 | if (const auto *LS = dyn_cast<LabelStmt>(S)) |
Chandler Carruth | a626d64 | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 206 | S = LS->getSubStmt(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 207 | else if (const auto *SC = dyn_cast<SwitchCase>(S)) |
Chandler Carruth | a626d64 | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 208 | S = SC->getSubStmt(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 209 | else if (const auto *AS = dyn_cast<AttributedStmt>(S)) |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 210 | S = AS->getSubStmt(); |
Chandler Carruth | a626d64 | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 211 | else |
| 212 | return S; |
| 213 | } |
| 214 | } |
| 215 | |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 216 | namespace { |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 217 | |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 218 | struct good {}; |
| 219 | struct bad {}; |
John McCall | f75152f | 2011-02-09 08:31:17 +0000 | [diff] [blame] | 220 | |
| 221 | // These silly little functions have to be static inline to suppress |
| 222 | // unused warnings, and they have to be defined to suppress other |
| 223 | // warnings. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 224 | static good is_good(good) { return good(); } |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 225 | |
| 226 | typedef Stmt::child_range children_t(); |
Nick Lewycky | bae992f | 2011-02-09 08:42:57 +0000 | [diff] [blame] | 227 | template <class T> good implements_children(children_t T::*) { |
| 228 | return good(); |
| 229 | } |
Eli Friedman | dc41d79 | 2013-09-10 22:57:15 +0000 | [diff] [blame] | 230 | LLVM_ATTRIBUTE_UNUSED |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 231 | static bad implements_children(children_t Stmt::*) { |
Nick Lewycky | bae992f | 2011-02-09 08:42:57 +0000 | [diff] [blame] | 232 | return bad(); |
| 233 | } |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 234 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 235 | typedef SourceLocation getBeginLoc_t() const; |
| 236 | template <class T> good implements_getBeginLoc(getBeginLoc_t T::*) { |
Nick Lewycky | bae992f | 2011-02-09 08:42:57 +0000 | [diff] [blame] | 237 | return good(); |
| 238 | } |
Eli Friedman | dc41d79 | 2013-09-10 22:57:15 +0000 | [diff] [blame] | 239 | LLVM_ATTRIBUTE_UNUSED |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 240 | static bad implements_getBeginLoc(getBeginLoc_t Stmt::*) { return bad(); } |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 241 | |
| 242 | typedef SourceLocation getLocEnd_t() const; |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 243 | template <class T> good implements_getEndLoc(getLocEnd_t T::*) { |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 244 | return good(); |
| 245 | } |
Eli Friedman | dc41d79 | 2013-09-10 22:57:15 +0000 | [diff] [blame] | 246 | LLVM_ATTRIBUTE_UNUSED |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 247 | static bad implements_getEndLoc(getLocEnd_t Stmt::*) { return bad(); } |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 248 | |
| 249 | #define ASSERT_IMPLEMENTS_children(type) \ |
Eli Friedman | dc41d79 | 2013-09-10 22:57:15 +0000 | [diff] [blame] | 250 | (void) is_good(implements_children(&type::children)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 251 | #define ASSERT_IMPLEMENTS_getBeginLoc(type) \ |
| 252 | (void)is_good(implements_getBeginLoc(&type::getBeginLoc)) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 253 | #define ASSERT_IMPLEMENTS_getEndLoc(type) \ |
| 254 | (void)is_good(implements_getEndLoc(&type::getEndLoc)) |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 255 | |
| 256 | } // namespace |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 257 | |
| 258 | /// Check whether the various Stmt classes implement their member |
| 259 | /// functions. |
Eli Friedman | dc41d79 | 2013-09-10 22:57:15 +0000 | [diff] [blame] | 260 | LLVM_ATTRIBUTE_UNUSED |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 261 | static inline void check_implementations() { |
| 262 | #define ABSTRACT_STMT(type) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 263 | #define STMT(type, base) \ |
| 264 | ASSERT_IMPLEMENTS_children(type); \ |
| 265 | ASSERT_IMPLEMENTS_getBeginLoc(type); \ |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 266 | ASSERT_IMPLEMENTS_getEndLoc(type); |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 267 | #include "clang/AST/StmtNodes.inc" |
| 268 | } |
| 269 | |
| 270 | Stmt::child_range Stmt::children() { |
| 271 | switch (getStmtClass()) { |
| 272 | case Stmt::NoStmtClass: llvm_unreachable("statement without class"); |
| 273 | #define ABSTRACT_STMT(type) |
| 274 | #define STMT(type, base) \ |
| 275 | case Stmt::type##Class: \ |
| 276 | return static_cast<type*>(this)->children(); |
| 277 | #include "clang/AST/StmtNodes.inc" |
| 278 | } |
| 279 | llvm_unreachable("unknown statement kind!"); |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 282 | // Amusing macro metaprogramming hack: check whether a class provides |
| 283 | // a more specific implementation of getSourceRange. |
| 284 | // |
| 285 | // See also Expr.cpp:getExprLoc(). |
| 286 | namespace { |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 287 | |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 288 | /// This implementation is used when a class provides a custom |
| 289 | /// implementation of getSourceRange. |
| 290 | template <class S, class T> |
| 291 | SourceRange getSourceRangeImpl(const Stmt *stmt, |
| 292 | SourceRange (T::*v)() const) { |
| 293 | return static_cast<const S*>(stmt)->getSourceRange(); |
| 294 | } |
| 295 | |
| 296 | /// This implementation is used when a class doesn't provide a custom |
| 297 | /// implementation of getSourceRange. Overload resolution should pick it over |
| 298 | /// the implementation above because it's more specialized according to |
| 299 | /// function template partial ordering. |
| 300 | template <class S> |
| 301 | SourceRange getSourceRangeImpl(const Stmt *stmt, |
| 302 | SourceRange (Stmt::*v)() const) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 303 | return SourceRange(static_cast<const S *>(stmt)->getBeginLoc(), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 304 | static_cast<const S *>(stmt)->getEndLoc()); |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 305 | } |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 306 | |
| 307 | } // namespace |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 308 | |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 309 | SourceRange Stmt::getSourceRange() const { |
| 310 | switch (getStmtClass()) { |
| 311 | case Stmt::NoStmtClass: llvm_unreachable("statement without class"); |
| 312 | #define ABSTRACT_STMT(type) |
| 313 | #define STMT(type, base) \ |
| 314 | case Stmt::type##Class: \ |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 315 | return getSourceRangeImpl<type>(this, &type::getSourceRange); |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 316 | #include "clang/AST/StmtNodes.inc" |
| 317 | } |
| 318 | llvm_unreachable("unknown statement kind!"); |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Stephen Kelly | 724e9e5 | 2018-08-09 20:05:03 +0000 | [diff] [blame] | 321 | SourceLocation Stmt::getBeginLoc() const { |
Daniel Dunbar | b0ab5e9 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 322 | switch (getStmtClass()) { |
| 323 | case Stmt::NoStmtClass: llvm_unreachable("statement without class"); |
| 324 | #define ABSTRACT_STMT(type) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 325 | #define STMT(type, base) \ |
| 326 | case Stmt::type##Class: \ |
| 327 | return static_cast<const type *>(this)->getBeginLoc(); |
Daniel Dunbar | b0ab5e9 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 328 | #include "clang/AST/StmtNodes.inc" |
| 329 | } |
| 330 | llvm_unreachable("unknown statement kind"); |
| 331 | } |
| 332 | |
Stephen Kelly | 02a67ba | 2018-08-09 20:05:47 +0000 | [diff] [blame] | 333 | SourceLocation Stmt::getEndLoc() const { |
Daniel Dunbar | b0ab5e9 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 334 | switch (getStmtClass()) { |
| 335 | case Stmt::NoStmtClass: llvm_unreachable("statement without class"); |
| 336 | #define ABSTRACT_STMT(type) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 337 | #define STMT(type, base) \ |
| 338 | case Stmt::type##Class: \ |
| 339 | return static_cast<const type *>(this)->getEndLoc(); |
Daniel Dunbar | b0ab5e9 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 340 | #include "clang/AST/StmtNodes.inc" |
| 341 | } |
| 342 | llvm_unreachable("unknown statement kind"); |
| 343 | } |
| 344 | |
George Karpenkov | 5eb4cc6 | 2018-09-15 02:01:47 +0000 | [diff] [blame] | 345 | int64_t Stmt::getID(const ASTContext &Context) const { |
Artem Dergachev | 057647d | 2018-12-03 22:19:05 +0000 | [diff] [blame] | 346 | return Context.getAllocator().identifyKnownAlignedObject<Stmt>(this); |
George Karpenkov | 5eb4cc6 | 2018-09-15 02:01:47 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Benjamin Kramer | 0742090 | 2017-12-24 16:24:20 +0000 | [diff] [blame] | 349 | CompoundStmt::CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, |
| 350 | SourceLocation RB) |
Bruno Ricci | 41d11c0 | 2018-10-27 18:43:27 +0000 | [diff] [blame] | 351 | : Stmt(CompoundStmtClass), RBraceLoc(RB) { |
Nico Weber | a2a0eb9 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 352 | CompoundStmtBits.NumStmts = Stmts.size(); |
Benjamin Kramer | 0742090 | 2017-12-24 16:24:20 +0000 | [diff] [blame] | 353 | setStmts(Stmts); |
Bruno Ricci | 41d11c0 | 2018-10-27 18:43:27 +0000 | [diff] [blame] | 354 | CompoundStmtBits.LBraceLoc = LB; |
Benjamin Kramer | e2a929d | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Benjamin Kramer | 0742090 | 2017-12-24 16:24:20 +0000 | [diff] [blame] | 357 | void CompoundStmt::setStmts(ArrayRef<Stmt *> Stmts) { |
Craig Topper | 9ee84ad | 2015-12-04 05:01:44 +0000 | [diff] [blame] | 358 | assert(CompoundStmtBits.NumStmts == Stmts.size() && |
| 359 | "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!"); |
Douglas Gregor | a9af1d1 | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 360 | |
Benjamin Kramer | 0742090 | 2017-12-24 16:24:20 +0000 | [diff] [blame] | 361 | std::copy(Stmts.begin(), Stmts.end(), body_begin()); |
| 362 | } |
| 363 | |
| 364 | CompoundStmt *CompoundStmt::Create(const ASTContext &C, ArrayRef<Stmt *> Stmts, |
| 365 | SourceLocation LB, SourceLocation RB) { |
| 366 | void *Mem = |
| 367 | C.Allocate(totalSizeToAlloc<Stmt *>(Stmts.size()), alignof(CompoundStmt)); |
| 368 | return new (Mem) CompoundStmt(Stmts, LB, RB); |
| 369 | } |
| 370 | |
| 371 | CompoundStmt *CompoundStmt::CreateEmpty(const ASTContext &C, |
| 372 | unsigned NumStmts) { |
| 373 | void *Mem = |
| 374 | C.Allocate(totalSizeToAlloc<Stmt *>(NumStmts), alignof(CompoundStmt)); |
| 375 | CompoundStmt *New = new (Mem) CompoundStmt(EmptyShell()); |
| 376 | New->CompoundStmtBits.NumStmts = NumStmts; |
| 377 | return New; |
Douglas Gregor | a9af1d1 | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 378 | } |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 379 | |
Richard Smith | a6e8d5e | 2019-02-15 00:27:53 +0000 | [diff] [blame] | 380 | const Expr *ValueStmt::getExprStmt() const { |
| 381 | const Stmt *S = this; |
| 382 | do { |
| 383 | if (const auto *E = dyn_cast<Expr>(S)) |
| 384 | return E; |
| 385 | |
| 386 | if (const auto *LS = dyn_cast<LabelStmt>(S)) |
| 387 | S = LS->getSubStmt(); |
| 388 | else if (const auto *AS = dyn_cast<AttributedStmt>(S)) |
| 389 | S = AS->getSubStmt(); |
| 390 | else |
| 391 | llvm_unreachable("unknown kind of ValueStmt"); |
| 392 | } while (isa<ValueStmt>(S)); |
| 393 | |
| 394 | return nullptr; |
| 395 | } |
| 396 | |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 397 | const char *LabelStmt::getName() const { |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 398 | return getDecl()->getIdentifier()->getNameStart(); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 401 | AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc, |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 402 | ArrayRef<const Attr*> Attrs, |
| 403 | Stmt *SubStmt) { |
Aaron Ballman | f3d9b09 | 2014-05-13 14:55:01 +0000 | [diff] [blame] | 404 | assert(!Attrs.empty() && "Attrs should not be empty"); |
Benjamin Kramer | 917fdbe | 2017-12-24 16:24:11 +0000 | [diff] [blame] | 405 | void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(Attrs.size()), |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 406 | alignof(AttributedStmt)); |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 407 | return new (Mem) AttributedStmt(Loc, Attrs, SubStmt); |
| 408 | } |
| 409 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 410 | AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C, |
| 411 | unsigned NumAttrs) { |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 412 | assert(NumAttrs > 0 && "NumAttrs should be greater than zero"); |
Benjamin Kramer | 917fdbe | 2017-12-24 16:24:11 +0000 | [diff] [blame] | 413 | void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(NumAttrs), |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 414 | alignof(AttributedStmt)); |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 415 | return new (Mem) AttributedStmt(EmptyShell(), NumAttrs); |
| 416 | } |
| 417 | |
Craig Topper | c571c81 | 2013-08-22 06:02:26 +0000 | [diff] [blame] | 418 | std::string AsmStmt::generateAsmString(const ASTContext &C) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 419 | if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 420 | return gccAsmStmt->generateAsmString(C); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 421 | if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 422 | return msAsmStmt->generateAsmString(C); |
Chad Rosier | bbbe9ab | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 423 | llvm_unreachable("unknown asm statement kind!"); |
| 424 | } |
| 425 | |
| 426 | StringRef AsmStmt::getOutputConstraint(unsigned i) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 427 | if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 428 | return gccAsmStmt->getOutputConstraint(i); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 429 | if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 430 | return msAsmStmt->getOutputConstraint(i); |
Chad Rosier | bbbe9ab | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 431 | llvm_unreachable("unknown asm statement kind!"); |
| 432 | } |
| 433 | |
| 434 | const Expr *AsmStmt::getOutputExpr(unsigned i) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 435 | if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 436 | return gccAsmStmt->getOutputExpr(i); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 437 | if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 438 | return msAsmStmt->getOutputExpr(i); |
Chad Rosier | bbbe9ab | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 439 | llvm_unreachable("unknown asm statement kind!"); |
| 440 | } |
| 441 | |
| 442 | StringRef AsmStmt::getInputConstraint(unsigned i) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 443 | if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 444 | return gccAsmStmt->getInputConstraint(i); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 445 | if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 446 | return msAsmStmt->getInputConstraint(i); |
Chad Rosier | bbbe9ab | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 447 | llvm_unreachable("unknown asm statement kind!"); |
| 448 | } |
| 449 | |
| 450 | const Expr *AsmStmt::getInputExpr(unsigned i) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 451 | if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 452 | return gccAsmStmt->getInputExpr(i); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 453 | if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 454 | return msAsmStmt->getInputExpr(i); |
Chad Rosier | bbbe9ab | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 455 | llvm_unreachable("unknown asm statement kind!"); |
| 456 | } |
| 457 | |
| 458 | StringRef AsmStmt::getClobber(unsigned i) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 459 | if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 460 | return gccAsmStmt->getClobber(i); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 461 | if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
Chad Rosier | f70b7e2 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 462 | return msAsmStmt->getClobber(i); |
Chad Rosier | bbbe9ab | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 463 | llvm_unreachable("unknown asm statement kind!"); |
| 464 | } |
| 465 | |
Chad Rosier | a1b5c8c | 2012-08-28 00:24:05 +0000 | [diff] [blame] | 466 | /// getNumPlusOperands - Return the number of output operands that have a "+" |
| 467 | /// constraint. |
| 468 | unsigned AsmStmt::getNumPlusOperands() const { |
| 469 | unsigned Res = 0; |
| 470 | for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) |
| 471 | if (isOutputPlusConstraint(i)) |
| 472 | ++Res; |
| 473 | return Res; |
| 474 | } |
| 475 | |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 476 | char GCCAsmStmt::AsmStringPiece::getModifier() const { |
| 477 | assert(isOperand() && "Only Operands can have modifiers."); |
| 478 | return isLetter(Str[0]) ? Str[0] : '\0'; |
| 479 | } |
| 480 | |
Chad Rosier | 6100ae1 | 2012-08-27 23:47:56 +0000 | [diff] [blame] | 481 | StringRef GCCAsmStmt::getClobber(unsigned i) const { |
| 482 | return getClobberStringLiteral(i)->getString(); |
| 483 | } |
| 484 | |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 485 | Expr *GCCAsmStmt::getOutputExpr(unsigned i) { |
Ted Kremenek | 5778acf | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 486 | return cast<Expr>(Exprs[i]); |
| 487 | } |
Chris Lattner | 72bbf17 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 488 | |
| 489 | /// getOutputConstraint - Return the constraint string for the specified |
| 490 | /// output operand. All output constraints are known to be non-empty (either |
| 491 | /// '=' or '+'). |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 492 | StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const { |
Anders Carlsson | 66de081 | 2010-01-30 20:38:10 +0000 | [diff] [blame] | 493 | return getOutputConstraintLiteral(i)->getString(); |
Ted Kremenek | 5778acf | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 494 | } |
Chris Lattner | 72bbf17 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 495 | |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 496 | Expr *GCCAsmStmt::getInputExpr(unsigned i) { |
Ted Kremenek | 5778acf | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 497 | return cast<Expr>(Exprs[i + NumOutputs]); |
| 498 | } |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 499 | |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 500 | void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) { |
Chris Lattner | 93ede02 | 2011-02-21 22:09:29 +0000 | [diff] [blame] | 501 | Exprs[i + NumOutputs] = E; |
| 502 | } |
| 503 | |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 504 | AddrLabelExpr *GCCAsmStmt::getLabelExpr(unsigned i) const { |
Bill Wendling | 50cac24 | 2020-02-24 18:32:50 -0800 | [diff] [blame] | 505 | return cast<AddrLabelExpr>(Exprs[i + NumOutputs + NumInputs]); |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | StringRef GCCAsmStmt::getLabelName(unsigned i) const { |
| 509 | return getLabelExpr(i)->getLabel()->getName(); |
| 510 | } |
| 511 | |
Chris Lattner | 72bbf17 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 512 | /// getInputConstraint - Return the specified input constraint. Unlike output |
| 513 | /// constraints, these can be empty. |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 514 | StringRef GCCAsmStmt::getInputConstraint(unsigned i) const { |
Anders Carlsson | 66de081 | 2010-01-30 20:38:10 +0000 | [diff] [blame] | 515 | return getInputConstraintLiteral(i)->getString(); |
Ted Kremenek | 5778acf | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Craig Topper | c571c81 | 2013-08-22 06:02:26 +0000 | [diff] [blame] | 518 | void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C, |
| 519 | IdentifierInfo **Names, |
| 520 | StringLiteral **Constraints, |
| 521 | Stmt **Exprs, |
| 522 | unsigned NumOutputs, |
| 523 | unsigned NumInputs, |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 524 | unsigned NumLabels, |
Craig Topper | c571c81 | 2013-08-22 06:02:26 +0000 | [diff] [blame] | 525 | StringLiteral **Clobbers, |
| 526 | unsigned NumClobbers) { |
Douglas Gregor | f994f06 | 2009-04-17 20:57:14 +0000 | [diff] [blame] | 527 | this->NumOutputs = NumOutputs; |
| 528 | this->NumInputs = NumInputs; |
Anders Carlsson | 98323d2 | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 529 | this->NumClobbers = NumClobbers; |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 530 | this->NumLabels = NumLabels; |
| 531 | assert(!(NumOutputs && NumLabels) && "asm goto cannot have outputs"); |
Anders Carlsson | 98323d2 | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 532 | |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 533 | unsigned NumExprs = NumOutputs + NumInputs + NumLabels; |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 534 | |
Anders Carlsson | 98323d2 | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 535 | C.Deallocate(this->Names); |
| 536 | this->Names = new (C) IdentifierInfo*[NumExprs]; |
| 537 | std::copy(Names, Names + NumExprs, this->Names); |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 538 | |
Anders Carlsson | 98323d2 | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 539 | C.Deallocate(this->Exprs); |
| 540 | this->Exprs = new (C) Stmt*[NumExprs]; |
| 541 | std::copy(Exprs, Exprs + NumExprs, this->Exprs); |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 542 | |
Benjamin Kramer | 851f57e | 2019-05-30 07:21:08 +0000 | [diff] [blame] | 543 | unsigned NumConstraints = NumOutputs + NumInputs; |
Anders Carlsson | 98323d2 | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 544 | C.Deallocate(this->Constraints); |
Benjamin Kramer | 851f57e | 2019-05-30 07:21:08 +0000 | [diff] [blame] | 545 | this->Constraints = new (C) StringLiteral*[NumConstraints]; |
| 546 | std::copy(Constraints, Constraints + NumConstraints, this->Constraints); |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 547 | |
Anders Carlsson | 98323d2 | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 548 | C.Deallocate(this->Clobbers); |
| 549 | this->Clobbers = new (C) StringLiteral*[NumClobbers]; |
| 550 | std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers); |
Douglas Gregor | f994f06 | 2009-04-17 20:57:14 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Chris Lattner | d7d5fdf | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 553 | /// getNamedOperand - Given a symbolic operand reference like %[foo], |
| 554 | /// translate this into a numeric value needed to reference the same operand. |
| 555 | /// This returns -1 if the operand name is invalid. |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 556 | int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const { |
Chris Lattner | d7d5fdf | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 557 | unsigned NumPlusOperands = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Chris Lattner | d7d5fdf | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 559 | // Check if this is an output operand. |
| 560 | for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) { |
| 561 | if (getOutputName(i) == SymbolicName) |
| 562 | return i; |
Chris Lattner | d7d5fdf | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 563 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | |
Chris Lattner | d7d5fdf | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 565 | for (unsigned i = 0, e = getNumInputs(); i != e; ++i) |
| 566 | if (getInputName(i) == SymbolicName) |
| 567 | return getNumOutputs() + NumPlusOperands + i; |
| 568 | |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 569 | for (unsigned i = 0, e = getNumLabels(); i != e; ++i) |
| 570 | if (getLabelName(i) == SymbolicName) |
Bill Wendling | 50cac24 | 2020-02-24 18:32:50 -0800 | [diff] [blame] | 571 | return i + getNumOutputs() + getNumInputs(); |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 572 | |
Chris Lattner | d7d5fdf | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 573 | // Not found. |
| 574 | return -1; |
| 575 | } |
| 576 | |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 577 | /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing |
| 578 | /// it into pieces. If the asm string is erroneous, emit errors and return |
| 579 | /// true, otherwise return false. |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 580 | unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces, |
Craig Topper | c571c81 | 2013-08-22 06:02:26 +0000 | [diff] [blame] | 581 | const ASTContext &C, unsigned &DiagOffs) const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 582 | StringRef Str = getAsmString()->getString(); |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 583 | const char *StrStart = Str.begin(); |
| 584 | const char *StrEnd = Str.end(); |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 585 | const char *CurPtr = StrStart; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 587 | // "Simple" inline asms have no constraints or operands, just convert the asm |
| 588 | // string to escape $'s. |
| 589 | if (isSimple()) { |
| 590 | std::string Result; |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 591 | for (; CurPtr != StrEnd; ++CurPtr) { |
| 592 | switch (*CurPtr) { |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 593 | case '$': |
| 594 | Result += "$$"; |
| 595 | break; |
| 596 | default: |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 597 | Result += *CurPtr; |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 598 | break; |
| 599 | } |
| 600 | } |
| 601 | Pieces.push_back(AsmStringPiece(Result)); |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 602 | return 0; |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // CurStringPiece - The current string that we are building up as we scan the |
| 606 | // asm string. |
| 607 | std::string CurStringPiece; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 609 | bool HasVariants = !C.getTargetInfo().hasNoAsmVariants(); |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 610 | |
Richard Smith | d18ab80 | 2016-05-16 22:52:23 +0000 | [diff] [blame] | 611 | unsigned LastAsmStringToken = 0; |
| 612 | unsigned LastAsmStringOffset = 0; |
| 613 | |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 614 | while (true) { |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 615 | // Done with the string? |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 616 | if (CurPtr == StrEnd) { |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 617 | if (!CurStringPiece.empty()) |
| 618 | Pieces.push_back(AsmStringPiece(CurStringPiece)); |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 619 | return 0; |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 620 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 622 | char CurChar = *CurPtr++; |
Chris Lattner | da081a8 | 2010-04-05 18:44:00 +0000 | [diff] [blame] | 623 | switch (CurChar) { |
| 624 | case '$': CurStringPiece += "$$"; continue; |
Chris Lattner | 1a8f394 | 2010-04-23 16:29:58 +0000 | [diff] [blame] | 625 | case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue; |
| 626 | case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue; |
| 627 | case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue; |
Chris Lattner | da081a8 | 2010-04-05 18:44:00 +0000 | [diff] [blame] | 628 | case '%': |
| 629 | break; |
| 630 | default: |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 631 | CurStringPiece += CurChar; |
| 632 | continue; |
| 633 | } |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 634 | |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 635 | // Escaped "%" character in asm string. |
Chris Lattner | 3fa25c6 | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 636 | if (CurPtr == StrEnd) { |
| 637 | // % at end of string is invalid (no escape). |
| 638 | DiagOffs = CurPtr-StrStart-1; |
| 639 | return diag::err_asm_invalid_escape; |
| 640 | } |
Michael Zuckerman | 2460bad | 2016-10-31 15:27:54 +0000 | [diff] [blame] | 641 | // Handle escaped char and continue looping over the asm string. |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 642 | char EscapedChar = *CurPtr++; |
Michael Zuckerman | 2460bad | 2016-10-31 15:27:54 +0000 | [diff] [blame] | 643 | switch (EscapedChar) { |
| 644 | default: |
| 645 | break; |
| 646 | case '%': // %% -> % |
| 647 | case '{': // %{ -> { |
| 648 | case '}': // %} -> } |
| 649 | CurStringPiece += EscapedChar; |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 650 | continue; |
Michael Zuckerman | 2460bad | 2016-10-31 15:27:54 +0000 | [diff] [blame] | 651 | case '=': // %= -> Generate a unique ID. |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 652 | CurStringPiece += "${:uid}"; |
| 653 | continue; |
| 654 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 656 | // Otherwise, we have an operand. If we have accumulated a string so far, |
| 657 | // add it to the Pieces list. |
| 658 | if (!CurStringPiece.empty()) { |
| 659 | Pieces.push_back(AsmStringPiece(CurStringPiece)); |
| 660 | CurStringPiece.clear(); |
| 661 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 663 | // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that |
| 664 | // don't (e.g., %x4). 'x' following the '%' is the constraint modifier. |
| 665 | |
| 666 | const char *Begin = CurPtr - 1; // Points to the character following '%'. |
| 667 | const char *Percent = Begin - 1; // Points to '%'. |
| 668 | |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 669 | if (isLetter(EscapedChar)) { |
Benjamin Kramer | e87c38b | 2011-07-05 11:13:37 +0000 | [diff] [blame] | 670 | if (CurPtr == StrEnd) { // Premature end. |
| 671 | DiagOffs = CurPtr-StrStart-1; |
| 672 | return diag::err_asm_invalid_escape; |
| 673 | } |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 674 | EscapedChar = *CurPtr++; |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 675 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 677 | const TargetInfo &TI = C.getTargetInfo(); |
| 678 | const SourceManager &SM = C.getSourceManager(); |
| 679 | const LangOptions &LO = C.getLangOpts(); |
| 680 | |
| 681 | // Handle operands that don't have asmSymbolicName (e.g., %x4). |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 682 | if (isDigit(EscapedChar)) { |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 683 | // %n - Assembler operand n |
Chris Lattner | 99d892b | 2009-03-11 22:52:17 +0000 | [diff] [blame] | 684 | unsigned N = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 99d892b | 2009-03-11 22:52:17 +0000 | [diff] [blame] | 686 | --CurPtr; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 687 | while (CurPtr != StrEnd && isDigit(*CurPtr)) |
Chris Lattner | 84f3afa | 2009-03-11 23:09:16 +0000 | [diff] [blame] | 688 | N = N*10 + ((*CurPtr++)-'0'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 690 | unsigned NumOperands = getNumOutputs() + getNumPlusOperands() + |
| 691 | getNumInputs() + getNumLabels(); |
Chris Lattner | 1431192 | 2009-03-11 00:23:13 +0000 | [diff] [blame] | 692 | if (N >= NumOperands) { |
| 693 | DiagOffs = CurPtr-StrStart-1; |
| 694 | return diag::err_asm_invalid_operand_number; |
| 695 | } |
| 696 | |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 697 | // Str contains "x4" (Operand without the leading %). |
| 698 | std::string Str(Begin, CurPtr - Begin); |
| 699 | |
| 700 | // (BeginLoc, EndLoc) represents the range of the operand we are currently |
| 701 | // processing. Unlike Str, the range includes the leading '%'. |
Richard Smith | d18ab80 | 2016-05-16 22:52:23 +0000 | [diff] [blame] | 702 | SourceLocation BeginLoc = getAsmString()->getLocationOfByte( |
| 703 | Percent - StrStart, SM, LO, TI, &LastAsmStringToken, |
| 704 | &LastAsmStringOffset); |
| 705 | SourceLocation EndLoc = getAsmString()->getLocationOfByte( |
| 706 | CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken, |
| 707 | &LastAsmStringOffset); |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 708 | |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 709 | Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc); |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 710 | continue; |
| 711 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 713 | // Handle operands that have asmSymbolicName (e.g., %x[foo]). |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 714 | if (EscapedChar == '[') { |
Chris Lattner | 3fa25c6 | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 715 | DiagOffs = CurPtr-StrStart-1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 3fa25c6 | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 717 | // Find the ']'. |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 718 | const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 719 | if (NameEnd == nullptr) |
Chris Lattner | 3fa25c6 | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 720 | return diag::err_asm_unterminated_symbolic_operand_name; |
| 721 | if (NameEnd == CurPtr) |
| 722 | return diag::err_asm_empty_symbolic_operand_name; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 723 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 724 | StringRef SymbolicName(CurPtr, NameEnd - CurPtr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 726 | int N = getNamedOperand(SymbolicName); |
Chris Lattner | 3fa25c6 | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 727 | if (N == -1) { |
| 728 | // Verify that an operand with that name exists. |
| 729 | DiagOffs = CurPtr-StrStart; |
| 730 | return diag::err_asm_unknown_symbolic_operand_name; |
| 731 | } |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 732 | |
| 733 | // Str contains "x[foo]" (Operand without the leading %). |
| 734 | std::string Str(Begin, NameEnd + 1 - Begin); |
| 735 | |
| 736 | // (BeginLoc, EndLoc) represents the range of the operand we are currently |
| 737 | // processing. Unlike Str, the range includes the leading '%'. |
Richard Smith | d18ab80 | 2016-05-16 22:52:23 +0000 | [diff] [blame] | 738 | SourceLocation BeginLoc = getAsmString()->getLocationOfByte( |
| 739 | Percent - StrStart, SM, LO, TI, &LastAsmStringToken, |
| 740 | &LastAsmStringOffset); |
| 741 | SourceLocation EndLoc = getAsmString()->getLocationOfByte( |
| 742 | NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken, |
| 743 | &LastAsmStringOffset); |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 744 | |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 745 | Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 3fa25c6 | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 747 | CurPtr = NameEnd+1; |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 748 | continue; |
| 749 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
Chris Lattner | 0cdaa2e | 2009-03-10 23:57:07 +0000 | [diff] [blame] | 751 | DiagOffs = CurPtr-StrStart-1; |
Chris Lattner | a41b847 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 752 | return diag::err_asm_invalid_escape; |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
Chad Rosier | 3b0c260 | 2012-08-27 20:23:31 +0000 | [diff] [blame] | 755 | |
| 756 | /// Assemble final IR asm string (GCC-style). |
Craig Topper | c571c81 | 2013-08-22 06:02:26 +0000 | [diff] [blame] | 757 | std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const { |
Chad Rosier | 14836ba | 2012-08-24 17:05:45 +0000 | [diff] [blame] | 758 | // Analyze the asm string to decompose it into its pieces. We know that Sema |
| 759 | // has already done this, so it is guaranteed to be successful. |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 760 | SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces; |
Chad Rosier | 14836ba | 2012-08-24 17:05:45 +0000 | [diff] [blame] | 761 | unsigned DiagOffs; |
| 762 | AnalyzeAsmString(Pieces, C, DiagOffs); |
| 763 | |
| 764 | std::string AsmString; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 765 | for (const auto &Piece : Pieces) { |
| 766 | if (Piece.isString()) |
| 767 | AsmString += Piece.getString(); |
| 768 | else if (Piece.getModifier() == '\0') |
| 769 | AsmString += '$' + llvm::utostr(Piece.getOperandNo()); |
Chad Rosier | 14836ba | 2012-08-24 17:05:45 +0000 | [diff] [blame] | 770 | else |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 771 | AsmString += "${" + llvm::utostr(Piece.getOperandNo()) + ':' + |
| 772 | Piece.getModifier() + '}'; |
Chad Rosier | 14836ba | 2012-08-24 17:05:45 +0000 | [diff] [blame] | 773 | } |
| 774 | return AsmString; |
| 775 | } |
Chris Lattner | 35b5836 | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 776 | |
Chad Rosier | 3b0c260 | 2012-08-27 20:23:31 +0000 | [diff] [blame] | 777 | /// Assemble final IR asm string (MS-style). |
Craig Topper | c571c81 | 2013-08-22 06:02:26 +0000 | [diff] [blame] | 778 | std::string MSAsmStmt::generateAsmString(const ASTContext &C) const { |
Chad Rosier | 3b0c260 | 2012-08-27 20:23:31 +0000 | [diff] [blame] | 779 | // FIXME: This needs to be translated into the IR string representation. |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 780 | return std::string(AsmStr); |
Chad Rosier | 3b0c260 | 2012-08-27 20:23:31 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Chad Rosier | fe31e62 | 2012-08-24 00:07:09 +0000 | [diff] [blame] | 783 | Expr *MSAsmStmt::getOutputExpr(unsigned i) { |
| 784 | return cast<Expr>(Exprs[i]); |
| 785 | } |
| 786 | |
| 787 | Expr *MSAsmStmt::getInputExpr(unsigned i) { |
| 788 | return cast<Expr>(Exprs[i + NumOutputs]); |
| 789 | } |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 790 | |
Chad Rosier | fe31e62 | 2012-08-24 00:07:09 +0000 | [diff] [blame] | 791 | void MSAsmStmt::setInputExpr(unsigned i, Expr *E) { |
| 792 | Exprs[i + NumOutputs] = E; |
| 793 | } |
| 794 | |
Chris Lattner | 86f5e13 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 795 | //===----------------------------------------------------------------------===// |
| 796 | // Constructors |
| 797 | //===----------------------------------------------------------------------===// |
| 798 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 799 | GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, |
| 800 | bool issimple, bool isvolatile, unsigned numoutputs, |
| 801 | unsigned numinputs, IdentifierInfo **names, |
| 802 | StringLiteral **constraints, Expr **exprs, |
| 803 | StringLiteral *asmstr, unsigned numclobbers, |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 804 | StringLiteral **clobbers, unsigned numlabels, |
| 805 | SourceLocation rparenloc) |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 806 | : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs, |
Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 807 | numinputs, numclobbers), |
| 808 | RParenLoc(rparenloc), AsmStr(asmstr), NumLabels(numlabels) { |
| 809 | unsigned NumExprs = NumOutputs + NumInputs + NumLabels; |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 810 | |
Anders Carlsson | 98323d2 | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 811 | Names = new (C) IdentifierInfo*[NumExprs]; |
| 812 | std::copy(names, names + NumExprs, Names); |
| 813 | |
| 814 | Exprs = new (C) Stmt*[NumExprs]; |
| 815 | std::copy(exprs, exprs + NumExprs, Exprs); |
| 816 | |
Benjamin Kramer | 851f57e | 2019-05-30 07:21:08 +0000 | [diff] [blame] | 817 | unsigned NumConstraints = NumOutputs + NumInputs; |
| 818 | Constraints = new (C) StringLiteral*[NumConstraints]; |
| 819 | std::copy(constraints, constraints + NumConstraints, Constraints); |
Anders Carlsson | 98323d2 | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 820 | |
| 821 | Clobbers = new (C) StringLiteral*[NumClobbers]; |
| 822 | std::copy(clobbers, clobbers + NumClobbers, Clobbers); |
Anders Carlsson | 94ea8aa | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 825 | MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc, |
Chad Rosier | 7dbef3e | 2012-08-16 00:06:53 +0000 | [diff] [blame] | 826 | SourceLocation lbraceloc, bool issimple, bool isvolatile, |
Chad Rosier | f8037a1 | 2012-10-16 21:55:39 +0000 | [diff] [blame] | 827 | ArrayRef<Token> asmtoks, unsigned numoutputs, |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 828 | unsigned numinputs, |
Chad Rosier | f8037a1 | 2012-10-16 21:55:39 +0000 | [diff] [blame] | 829 | ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs, |
| 830 | StringRef asmstr, ArrayRef<StringRef> clobbers, |
| 831 | SourceLocation endloc) |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 832 | : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs, |
| 833 | numinputs, clobbers.size()), LBraceLoc(lbraceloc), |
| 834 | EndLoc(endloc), NumAsmToks(asmtoks.size()) { |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 835 | initialize(C, asmstr, asmtoks, constraints, exprs, clobbers); |
| 836 | } |
Chad Rosier | 7dbef3e | 2012-08-16 00:06:53 +0000 | [diff] [blame] | 837 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 838 | static StringRef copyIntoContext(const ASTContext &C, StringRef str) { |
Benjamin Kramer | 2ab0d88 | 2015-08-04 12:34:23 +0000 | [diff] [blame] | 839 | return str.copy(C); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 842 | void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr, |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 843 | ArrayRef<Token> asmtoks, |
| 844 | ArrayRef<StringRef> constraints, |
| 845 | ArrayRef<Expr*> exprs, |
| 846 | ArrayRef<StringRef> clobbers) { |
| 847 | assert(NumAsmToks == asmtoks.size()); |
| 848 | assert(NumClobbers == clobbers.size()); |
| 849 | |
Craig Topper | caf138e | 2015-12-05 07:41:42 +0000 | [diff] [blame] | 850 | assert(exprs.size() == NumOutputs + NumInputs); |
| 851 | assert(exprs.size() == constraints.size()); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 852 | |
| 853 | AsmStr = copyIntoContext(C, asmstr); |
Chad Rosier | 99fc381 | 2012-08-07 00:29:06 +0000 | [diff] [blame] | 854 | |
Craig Topper | caf138e | 2015-12-05 07:41:42 +0000 | [diff] [blame] | 855 | Exprs = new (C) Stmt*[exprs.size()]; |
| 856 | std::copy(exprs.begin(), exprs.end(), Exprs); |
Chad Rosier | fe31e62 | 2012-08-24 00:07:09 +0000 | [diff] [blame] | 857 | |
Craig Topper | caf138e | 2015-12-05 07:41:42 +0000 | [diff] [blame] | 858 | AsmToks = new (C) Token[asmtoks.size()]; |
| 859 | std::copy(asmtoks.begin(), asmtoks.end(), AsmToks); |
Chad Rosier | 3ed0bd9 | 2012-08-08 19:48:07 +0000 | [diff] [blame] | 860 | |
Craig Topper | caf138e | 2015-12-05 07:41:42 +0000 | [diff] [blame] | 861 | Constraints = new (C) StringRef[exprs.size()]; |
| 862 | std::transform(constraints.begin(), constraints.end(), Constraints, |
| 863 | [&](StringRef Constraint) { |
| 864 | return copyIntoContext(C, Constraint); |
| 865 | }); |
Chad Rosier | 3dd7bf2 | 2012-08-28 20:28:20 +0000 | [diff] [blame] | 866 | |
Chad Rosier | baf53f9 | 2012-08-10 21:36:25 +0000 | [diff] [blame] | 867 | Clobbers = new (C) StringRef[NumClobbers]; |
Craig Topper | caf138e | 2015-12-05 07:41:42 +0000 | [diff] [blame] | 868 | // FIXME: Avoid the allocation/copy if at all possible. |
| 869 | std::transform(clobbers.begin(), clobbers.end(), Clobbers, |
| 870 | [&](StringRef Clobber) { |
| 871 | return copyIntoContext(C, Clobber); |
| 872 | }); |
Chad Rosier | 3250302 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 875 | IfStmt::IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr, |
Zequan Wu | 94c6cea | 2020-08-10 16:29:33 -0700 | [diff] [blame] | 876 | Stmt *Init, VarDecl *Var, Expr *Cond, SourceLocation LPL, |
| 877 | SourceLocation RPL, Stmt *Then, SourceLocation EL, Stmt *Else) |
| 878 | : Stmt(IfStmtClass), LParenLoc(LPL), RParenLoc(RPL) { |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 879 | bool HasElse = Else != nullptr; |
| 880 | bool HasVar = Var != nullptr; |
| 881 | bool HasInit = Init != nullptr; |
| 882 | IfStmtBits.HasElse = HasElse; |
| 883 | IfStmtBits.HasVar = HasVar; |
| 884 | IfStmtBits.HasInit = HasInit; |
| 885 | |
Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 886 | setConstexpr(IsConstexpr); |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 887 | |
| 888 | setCond(Cond); |
| 889 | setThen(Then); |
| 890 | if (HasElse) |
| 891 | setElse(Else); |
| 892 | if (HasVar) |
| 893 | setConditionVariable(Ctx, Var); |
| 894 | if (HasInit) |
| 895 | setInit(Init); |
| 896 | |
| 897 | setIfLoc(IL); |
| 898 | if (HasElse) |
| 899 | setElseLoc(EL); |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 902 | IfStmt::IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit) |
| 903 | : Stmt(IfStmtClass, Empty) { |
| 904 | IfStmtBits.HasElse = HasElse; |
| 905 | IfStmtBits.HasVar = HasVar; |
| 906 | IfStmtBits.HasInit = HasInit; |
| 907 | } |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 908 | |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 909 | IfStmt *IfStmt::Create(const ASTContext &Ctx, SourceLocation IL, |
| 910 | bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond, |
Zequan Wu | 94c6cea | 2020-08-10 16:29:33 -0700 | [diff] [blame] | 911 | SourceLocation LPL, SourceLocation RPL, Stmt *Then, |
| 912 | SourceLocation EL, Stmt *Else) { |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 913 | bool HasElse = Else != nullptr; |
| 914 | bool HasVar = Var != nullptr; |
| 915 | bool HasInit = Init != nullptr; |
| 916 | void *Mem = Ctx.Allocate( |
| 917 | totalSizeToAlloc<Stmt *, SourceLocation>( |
| 918 | NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse), |
| 919 | alignof(IfStmt)); |
| 920 | return new (Mem) |
Zequan Wu | 94c6cea | 2020-08-10 16:29:33 -0700 | [diff] [blame] | 921 | IfStmt(Ctx, IL, IsConstexpr, Init, Var, Cond, LPL, RPL, Then, EL, Else); |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | IfStmt *IfStmt::CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar, |
| 925 | bool HasInit) { |
| 926 | void *Mem = Ctx.Allocate( |
| 927 | totalSizeToAlloc<Stmt *, SourceLocation>( |
| 928 | NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse), |
| 929 | alignof(IfStmt)); |
| 930 | return new (Mem) IfStmt(EmptyShell(), HasElse, HasVar, HasInit); |
| 931 | } |
| 932 | |
| 933 | VarDecl *IfStmt::getConditionVariable() { |
| 934 | auto *DS = getConditionVariableDeclStmt(); |
| 935 | if (!DS) |
| 936 | return nullptr; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 937 | return cast<VarDecl>(DS->getSingleDecl()); |
| 938 | } |
| 939 | |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 940 | void IfStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) { |
| 941 | assert(hasVarStorage() && |
| 942 | "This if statement has no storage for a condition variable!"); |
| 943 | |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 944 | if (!V) { |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 945 | getTrailingObjects<Stmt *>()[varOffset()] = nullptr; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 946 | return; |
| 947 | } |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 948 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 949 | SourceRange VarRange = V->getSourceRange(); |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 950 | getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx) |
| 951 | DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd()); |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 954 | bool IfStmt::isObjCAvailabilityCheck() const { |
Bruno Ricci | b1cc94b | 2018-10-27 21:12:20 +0000 | [diff] [blame] | 955 | return isa<ObjCAvailabilityCheckExpr>(getCond()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 958 | Optional<const Stmt*> IfStmt::getNondiscardedCase(const ASTContext &Ctx) const { |
| 959 | if (!isConstexpr() || getCond()->isValueDependent()) |
| 960 | return None; |
| 961 | return !getCond()->EvaluateKnownConstInt(Ctx) ? getElse() : getThen(); |
| 962 | } |
| 963 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 964 | ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 965 | Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP, |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 966 | SourceLocation RP) |
Bruno Ricci | 41d11c0 | 2018-10-27 18:43:27 +0000 | [diff] [blame] | 967 | : Stmt(ForStmtClass), LParenLoc(LP), RParenLoc(RP) |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 968 | { |
| 969 | SubExprs[INIT] = Init; |
| 970 | setConditionVariable(C, condVar); |
Pavel Labath | 515f4db | 2013-09-03 14:41:16 +0000 | [diff] [blame] | 971 | SubExprs[COND] = Cond; |
| 972 | SubExprs[INC] = Inc; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 973 | SubExprs[BODY] = Body; |
Bruno Ricci | 41d11c0 | 2018-10-27 18:43:27 +0000 | [diff] [blame] | 974 | ForStmtBits.ForLoc = FL; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | VarDecl *ForStmt::getConditionVariable() const { |
| 978 | if (!SubExprs[CONDVAR]) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 979 | return nullptr; |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 980 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 981 | auto *DS = cast<DeclStmt>(SubExprs[CONDVAR]); |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 982 | return cast<VarDecl>(DS->getSingleDecl()); |
| 983 | } |
| 984 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 985 | void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) { |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 986 | if (!V) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 987 | SubExprs[CONDVAR] = nullptr; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 988 | return; |
| 989 | } |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 990 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 991 | SourceRange VarRange = V->getSourceRange(); |
| 992 | SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(), |
| 993 | VarRange.getEnd()); |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Bruno Ricci | e2806f8 | 2018-10-29 16:12:37 +0000 | [diff] [blame] | 996 | SwitchStmt::SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var, |
Zequan Wu | 94c6cea | 2020-08-10 16:29:33 -0700 | [diff] [blame] | 997 | Expr *Cond, SourceLocation LParenLoc, |
| 998 | SourceLocation RParenLoc) |
| 999 | : Stmt(SwitchStmtClass), FirstCase(nullptr), LParenLoc(LParenLoc), |
| 1000 | RParenLoc(RParenLoc) { |
Bruno Ricci | e2806f8 | 2018-10-29 16:12:37 +0000 | [diff] [blame] | 1001 | bool HasInit = Init != nullptr; |
| 1002 | bool HasVar = Var != nullptr; |
| 1003 | SwitchStmtBits.HasInit = HasInit; |
| 1004 | SwitchStmtBits.HasVar = HasVar; |
| 1005 | SwitchStmtBits.AllEnumCasesCovered = false; |
| 1006 | |
| 1007 | setCond(Cond); |
| 1008 | setBody(nullptr); |
| 1009 | if (HasInit) |
| 1010 | setInit(Init); |
| 1011 | if (HasVar) |
| 1012 | setConditionVariable(Ctx, Var); |
| 1013 | |
| 1014 | setSwitchLoc(SourceLocation{}); |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Bruno Ricci | e2806f8 | 2018-10-29 16:12:37 +0000 | [diff] [blame] | 1017 | SwitchStmt::SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar) |
| 1018 | : Stmt(SwitchStmtClass, Empty) { |
| 1019 | SwitchStmtBits.HasInit = HasInit; |
| 1020 | SwitchStmtBits.HasVar = HasVar; |
| 1021 | SwitchStmtBits.AllEnumCasesCovered = false; |
| 1022 | } |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 1023 | |
Bruno Ricci | e2806f8 | 2018-10-29 16:12:37 +0000 | [diff] [blame] | 1024 | SwitchStmt *SwitchStmt::Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var, |
Zequan Wu | 94c6cea | 2020-08-10 16:29:33 -0700 | [diff] [blame] | 1025 | Expr *Cond, SourceLocation LParenLoc, |
| 1026 | SourceLocation RParenLoc) { |
Bruno Ricci | e2806f8 | 2018-10-29 16:12:37 +0000 | [diff] [blame] | 1027 | bool HasInit = Init != nullptr; |
| 1028 | bool HasVar = Var != nullptr; |
| 1029 | void *Mem = Ctx.Allocate( |
| 1030 | totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar), |
| 1031 | alignof(SwitchStmt)); |
Zequan Wu | 94c6cea | 2020-08-10 16:29:33 -0700 | [diff] [blame] | 1032 | return new (Mem) SwitchStmt(Ctx, Init, Var, Cond, LParenLoc, RParenLoc); |
Bruno Ricci | e2806f8 | 2018-10-29 16:12:37 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | SwitchStmt *SwitchStmt::CreateEmpty(const ASTContext &Ctx, bool HasInit, |
| 1036 | bool HasVar) { |
| 1037 | void *Mem = Ctx.Allocate( |
| 1038 | totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar), |
| 1039 | alignof(SwitchStmt)); |
| 1040 | return new (Mem) SwitchStmt(EmptyShell(), HasInit, HasVar); |
| 1041 | } |
| 1042 | |
| 1043 | VarDecl *SwitchStmt::getConditionVariable() { |
| 1044 | auto *DS = getConditionVariableDeclStmt(); |
| 1045 | if (!DS) |
| 1046 | return nullptr; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1047 | return cast<VarDecl>(DS->getSingleDecl()); |
| 1048 | } |
| 1049 | |
Bruno Ricci | e2806f8 | 2018-10-29 16:12:37 +0000 | [diff] [blame] | 1050 | void SwitchStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) { |
| 1051 | assert(hasVarStorage() && |
| 1052 | "This switch statement has no storage for a condition variable!"); |
| 1053 | |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1054 | if (!V) { |
Bruno Ricci | e2806f8 | 2018-10-29 16:12:37 +0000 | [diff] [blame] | 1055 | getTrailingObjects<Stmt *>()[varOffset()] = nullptr; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1056 | return; |
| 1057 | } |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 1058 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1059 | SourceRange VarRange = V->getSourceRange(); |
Bruno Ricci | e2806f8 | 2018-10-29 16:12:37 +0000 | [diff] [blame] | 1060 | getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx) |
| 1061 | DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd()); |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Bruno Ricci | bacf751 | 2018-10-30 13:42:41 +0000 | [diff] [blame] | 1064 | WhileStmt::WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond, |
Vy Nguyen | 17ea41e | 2020-07-09 23:19:06 -0400 | [diff] [blame] | 1065 | Stmt *Body, SourceLocation WL, SourceLocation LParenLoc, |
| 1066 | SourceLocation RParenLoc) |
Bruno Ricci | bacf751 | 2018-10-30 13:42:41 +0000 | [diff] [blame] | 1067 | : Stmt(WhileStmtClass) { |
| 1068 | bool HasVar = Var != nullptr; |
| 1069 | WhileStmtBits.HasVar = HasVar; |
| 1070 | |
| 1071 | setCond(Cond); |
| 1072 | setBody(Body); |
| 1073 | if (HasVar) |
| 1074 | setConditionVariable(Ctx, Var); |
| 1075 | |
| 1076 | setWhileLoc(WL); |
Vy Nguyen | 17ea41e | 2020-07-09 23:19:06 -0400 | [diff] [blame] | 1077 | setLParenLoc(LParenLoc); |
| 1078 | setRParenLoc(RParenLoc); |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Bruno Ricci | bacf751 | 2018-10-30 13:42:41 +0000 | [diff] [blame] | 1081 | WhileStmt::WhileStmt(EmptyShell Empty, bool HasVar) |
| 1082 | : Stmt(WhileStmtClass, Empty) { |
| 1083 | WhileStmtBits.HasVar = HasVar; |
| 1084 | } |
Chad Rosier | 42032fa | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 1085 | |
Bruno Ricci | bacf751 | 2018-10-30 13:42:41 +0000 | [diff] [blame] | 1086 | WhileStmt *WhileStmt::Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond, |
Vy Nguyen | 17ea41e | 2020-07-09 23:19:06 -0400 | [diff] [blame] | 1087 | Stmt *Body, SourceLocation WL, |
| 1088 | SourceLocation LParenLoc, |
| 1089 | SourceLocation RParenLoc) { |
Bruno Ricci | bacf751 | 2018-10-30 13:42:41 +0000 | [diff] [blame] | 1090 | bool HasVar = Var != nullptr; |
| 1091 | void *Mem = |
| 1092 | Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar), |
| 1093 | alignof(WhileStmt)); |
Vy Nguyen | 17ea41e | 2020-07-09 23:19:06 -0400 | [diff] [blame] | 1094 | return new (Mem) WhileStmt(Ctx, Var, Cond, Body, WL, LParenLoc, RParenLoc); |
Bruno Ricci | bacf751 | 2018-10-30 13:42:41 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | WhileStmt *WhileStmt::CreateEmpty(const ASTContext &Ctx, bool HasVar) { |
| 1098 | void *Mem = |
| 1099 | Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar), |
| 1100 | alignof(WhileStmt)); |
| 1101 | return new (Mem) WhileStmt(EmptyShell(), HasVar); |
| 1102 | } |
| 1103 | |
| 1104 | VarDecl *WhileStmt::getConditionVariable() { |
| 1105 | auto *DS = getConditionVariableDeclStmt(); |
| 1106 | if (!DS) |
| 1107 | return nullptr; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1108 | return cast<VarDecl>(DS->getSingleDecl()); |
| 1109 | } |
| 1110 | |
Bruno Ricci | bacf751 | 2018-10-30 13:42:41 +0000 | [diff] [blame] | 1111 | void WhileStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) { |
| 1112 | assert(hasVarStorage() && |
| 1113 | "This while statement has no storage for a condition variable!"); |
| 1114 | |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1115 | if (!V) { |
Bruno Ricci | bacf751 | 2018-10-30 13:42:41 +0000 | [diff] [blame] | 1116 | getTrailingObjects<Stmt *>()[varOffset()] = nullptr; |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1117 | return; |
| 1118 | } |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1119 | |
| 1120 | SourceRange VarRange = V->getSourceRange(); |
Bruno Ricci | bacf751 | 2018-10-30 13:42:41 +0000 | [diff] [blame] | 1121 | getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx) |
| 1122 | DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd()); |
Douglas Gregor | 27b98ea | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Ted Kremenek | 066dd93 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 1125 | // IndirectGotoStmt |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1126 | LabelDecl *IndirectGotoStmt::getConstantTarget() { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1127 | if (auto *E = dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts())) |
John McCall | 9de9160 | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 1128 | return E->getLabel(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1129 | return nullptr; |
John McCall | 9de9160 | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 1130 | } |
Ted Kremenek | 066dd93 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 1131 | |
Ted Kremenek | 066dd93 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 1132 | // ReturnStmt |
Bruno Ricci | 023b1d1 | 2018-10-30 14:40:49 +0000 | [diff] [blame] | 1133 | ReturnStmt::ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate) |
| 1134 | : Stmt(ReturnStmtClass), RetExpr(E) { |
| 1135 | bool HasNRVOCandidate = NRVOCandidate != nullptr; |
| 1136 | ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate; |
| 1137 | if (HasNRVOCandidate) |
| 1138 | setNRVOCandidate(NRVOCandidate); |
| 1139 | setReturnLoc(RL); |
Ted Kremenek | c6501db | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 1140 | } |
Bruno Ricci | 023b1d1 | 2018-10-30 14:40:49 +0000 | [diff] [blame] | 1141 | |
| 1142 | ReturnStmt::ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate) |
| 1143 | : Stmt(ReturnStmtClass, Empty) { |
| 1144 | ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate; |
| 1145 | } |
| 1146 | |
| 1147 | ReturnStmt *ReturnStmt::Create(const ASTContext &Ctx, SourceLocation RL, |
| 1148 | Expr *E, const VarDecl *NRVOCandidate) { |
| 1149 | bool HasNRVOCandidate = NRVOCandidate != nullptr; |
| 1150 | void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate), |
| 1151 | alignof(ReturnStmt)); |
| 1152 | return new (Mem) ReturnStmt(RL, E, NRVOCandidate); |
| 1153 | } |
| 1154 | |
| 1155 | ReturnStmt *ReturnStmt::CreateEmpty(const ASTContext &Ctx, |
| 1156 | bool HasNRVOCandidate) { |
| 1157 | void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate), |
| 1158 | alignof(ReturnStmt)); |
| 1159 | return new (Mem) ReturnStmt(EmptyShell(), HasNRVOCandidate); |
Ted Kremenek | 066dd93 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 1160 | } |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 1161 | |
Bruno Ricci | 5b3057175 | 2018-10-28 12:30:53 +0000 | [diff] [blame] | 1162 | // CaseStmt |
| 1163 | CaseStmt *CaseStmt::Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs, |
| 1164 | SourceLocation caseLoc, SourceLocation ellipsisLoc, |
| 1165 | SourceLocation colonLoc) { |
| 1166 | bool CaseStmtIsGNURange = rhs != nullptr; |
| 1167 | void *Mem = Ctx.Allocate( |
| 1168 | totalSizeToAlloc<Stmt *, SourceLocation>( |
| 1169 | NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange), |
| 1170 | alignof(CaseStmt)); |
| 1171 | return new (Mem) CaseStmt(lhs, rhs, caseLoc, ellipsisLoc, colonLoc); |
| 1172 | } |
| 1173 | |
| 1174 | CaseStmt *CaseStmt::CreateEmpty(const ASTContext &Ctx, |
| 1175 | bool CaseStmtIsGNURange) { |
| 1176 | void *Mem = Ctx.Allocate( |
| 1177 | totalSizeToAlloc<Stmt *, SourceLocation>( |
| 1178 | NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange), |
| 1179 | alignof(CaseStmt)); |
| 1180 | return new (Mem) CaseStmt(EmptyShell(), CaseStmtIsGNURange); |
| 1181 | } |
| 1182 | |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 1183 | SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock, |
Warren Hunt | f6be4cb | 2014-07-25 20:52:51 +0000 | [diff] [blame] | 1184 | Stmt *Handler) |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 1185 | : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc) { |
Warren Hunt | f6be4cb | 2014-07-25 20:52:51 +0000 | [diff] [blame] | 1186 | Children[TRY] = TryBlock; |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 1187 | Children[HANDLER] = Handler; |
| 1188 | } |
| 1189 | |
Warren Hunt | f6be4cb | 2014-07-25 20:52:51 +0000 | [diff] [blame] | 1190 | SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry, |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 1191 | SourceLocation TryLoc, Stmt *TryBlock, |
Warren Hunt | f6be4cb | 2014-07-25 20:52:51 +0000 | [diff] [blame] | 1192 | Stmt *Handler) { |
| 1193 | return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | SEHExceptStmt* SEHTryStmt::getExceptHandler() const { |
| 1197 | return dyn_cast<SEHExceptStmt>(getHandler()); |
| 1198 | } |
| 1199 | |
| 1200 | SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const { |
| 1201 | return dyn_cast<SEHFinallyStmt>(getHandler()); |
| 1202 | } |
| 1203 | |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 1204 | SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block) |
| 1205 | : Stmt(SEHExceptStmtClass), Loc(Loc) { |
Pavel Labath | 515f4db | 2013-09-03 14:41:16 +0000 | [diff] [blame] | 1206 | Children[FILTER_EXPR] = FilterExpr; |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 1207 | Children[BLOCK] = Block; |
| 1208 | } |
| 1209 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 1210 | SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc, |
| 1211 | Expr *FilterExpr, Stmt *Block) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 1212 | return new(C) SEHExceptStmt(Loc,FilterExpr,Block); |
| 1213 | } |
| 1214 | |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 1215 | SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, Stmt *Block) |
| 1216 | : Stmt(SEHFinallyStmtClass), Loc(Loc), Block(Block) {} |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 1217 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 1218 | SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc, |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 1219 | Stmt *Block) { |
| 1220 | return new(C)SEHFinallyStmt(Loc,Block); |
| 1221 | } |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1222 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 1223 | CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind, |
| 1224 | VarDecl *Var) |
| 1225 | : VarAndKind(Var, Kind), Loc(Loc) { |
| 1226 | switch (Kind) { |
| 1227 | case VCK_This: |
| 1228 | assert(!Var && "'this' capture cannot have a variable!"); |
| 1229 | break; |
| 1230 | case VCK_ByRef: |
| 1231 | assert(Var && "capturing by reference must have a variable!"); |
| 1232 | break; |
| 1233 | case VCK_ByCopy: |
| 1234 | assert(Var && "capturing by copy must have a variable!"); |
| 1235 | assert( |
| 1236 | (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() && |
| 1237 | Var->getType() |
| 1238 | ->castAs<ReferenceType>() |
| 1239 | ->getPointeeType() |
| 1240 | ->isScalarType())) && |
| 1241 | "captures by copy are expected to have a scalar type!"); |
| 1242 | break; |
| 1243 | case VCK_VLAType: |
| 1244 | assert(!Var && |
| 1245 | "Variable-length array type capture cannot have a variable!"); |
| 1246 | break; |
| 1247 | } |
| 1248 | } |
| 1249 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 1250 | CapturedStmt::VariableCaptureKind |
| 1251 | CapturedStmt::Capture::getCaptureKind() const { |
| 1252 | return VarAndKind.getInt(); |
| 1253 | } |
| 1254 | |
| 1255 | VarDecl *CapturedStmt::Capture::getCapturedVar() const { |
| 1256 | assert((capturesVariable() || capturesVariableByCopy()) && |
| 1257 | "No variable available for 'this' or VAT capture"); |
| 1258 | return VarAndKind.getPointer(); |
| 1259 | } |
| 1260 | |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1261 | CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const { |
| 1262 | unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1); |
| 1263 | |
| 1264 | // Offset of the first Capture object. |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 1265 | unsigned FirstCaptureOffset = llvm::alignTo(Size, alignof(Capture)); |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1266 | |
| 1267 | return reinterpret_cast<Capture *>( |
| 1268 | reinterpret_cast<char *>(const_cast<CapturedStmt *>(this)) |
| 1269 | + FirstCaptureOffset); |
| 1270 | } |
| 1271 | |
Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 1272 | CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind, |
| 1273 | ArrayRef<Capture> Captures, |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1274 | ArrayRef<Expr *> CaptureInits, |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1275 | CapturedDecl *CD, |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1276 | RecordDecl *RD) |
| 1277 | : Stmt(CapturedStmtClass), NumCaptures(Captures.size()), |
Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 1278 | CapDeclAndKind(CD, Kind), TheRecordDecl(RD) { |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1279 | assert( S && "null captured statement"); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1280 | assert(CD && "null captured declaration for captured statement"); |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1281 | assert(RD && "null record declaration for captured statement"); |
| 1282 | |
| 1283 | // Copy initialization expressions. |
| 1284 | Stmt **Stored = getStoredStmts(); |
| 1285 | for (unsigned I = 0, N = NumCaptures; I != N; ++I) |
| 1286 | *Stored++ = CaptureInits[I]; |
| 1287 | |
| 1288 | // Copy the statement being captured. |
| 1289 | *Stored = S; |
| 1290 | |
| 1291 | // Copy all Capture objects. |
| 1292 | Capture *Buffer = getStoredCaptures(); |
| 1293 | std::copy(Captures.begin(), Captures.end(), Buffer); |
| 1294 | } |
| 1295 | |
| 1296 | CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures) |
| 1297 | : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures), |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 1298 | CapDeclAndKind(nullptr, CR_Default) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1299 | getStoredStmts()[NumCaptures] = nullptr; |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 1302 | CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S, |
Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 1303 | CapturedRegionKind Kind, |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1304 | ArrayRef<Capture> Captures, |
| 1305 | ArrayRef<Expr *> CaptureInits, |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1306 | CapturedDecl *CD, |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1307 | RecordDecl *RD) { |
| 1308 | // The layout is |
| 1309 | // |
| 1310 | // ----------------------------------------------------------- |
| 1311 | // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture | |
| 1312 | // ----------------^-------------------^---------------------- |
| 1313 | // getStoredStmts() getStoredCaptures() |
| 1314 | // |
| 1315 | // where S is the statement being captured. |
| 1316 | // |
| 1317 | assert(CaptureInits.size() == Captures.size() && "wrong number of arguments"); |
| 1318 | |
| 1319 | unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1); |
| 1320 | if (!Captures.empty()) { |
| 1321 | // Realign for the following Capture array. |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 1322 | Size = llvm::alignTo(Size, alignof(Capture)); |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1323 | Size += sizeof(Capture) * Captures.size(); |
| 1324 | } |
| 1325 | |
| 1326 | void *Mem = Context.Allocate(Size); |
Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 1327 | return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD); |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Craig Topper | e6960e2 | 2013-08-22 05:28:54 +0000 | [diff] [blame] | 1330 | CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context, |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1331 | unsigned NumCaptures) { |
| 1332 | unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1); |
| 1333 | if (NumCaptures > 0) { |
| 1334 | // Realign for the following Capture array. |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 1335 | Size = llvm::alignTo(Size, alignof(Capture)); |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1336 | Size += sizeof(Capture) * NumCaptures; |
| 1337 | } |
| 1338 | |
| 1339 | void *Mem = Context.Allocate(Size); |
| 1340 | return new (Mem) CapturedStmt(EmptyShell(), NumCaptures); |
| 1341 | } |
| 1342 | |
| 1343 | Stmt::child_range CapturedStmt::children() { |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 1344 | // Children are captured field initializers. |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1345 | return child_range(getStoredStmts(), getStoredStmts() + NumCaptures); |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Bruno Ricci | 0618650 | 2019-04-12 15:36:02 +0000 | [diff] [blame] | 1348 | Stmt::const_child_range CapturedStmt::children() const { |
| 1349 | return const_child_range(getStoredStmts(), getStoredStmts() + NumCaptures); |
| 1350 | } |
| 1351 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 1352 | CapturedDecl *CapturedStmt::getCapturedDecl() { |
| 1353 | return CapDeclAndKind.getPointer(); |
| 1354 | } |
Eugene Zelenko | 1eab6c1 | 2017-11-14 23:35:42 +0000 | [diff] [blame] | 1355 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 1356 | const CapturedDecl *CapturedStmt::getCapturedDecl() const { |
| 1357 | return CapDeclAndKind.getPointer(); |
| 1358 | } |
| 1359 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1360 | /// Set the outlined function declaration. |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 1361 | void CapturedStmt::setCapturedDecl(CapturedDecl *D) { |
| 1362 | assert(D && "null CapturedDecl"); |
| 1363 | CapDeclAndKind.setPointer(D); |
| 1364 | } |
| 1365 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1366 | /// Retrieve the captured region kind. |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 1367 | CapturedRegionKind CapturedStmt::getCapturedRegionKind() const { |
| 1368 | return CapDeclAndKind.getInt(); |
| 1369 | } |
| 1370 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1371 | /// Set the captured region kind. |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 1372 | void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) { |
| 1373 | CapDeclAndKind.setInt(Kind); |
| 1374 | } |
| 1375 | |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1376 | bool CapturedStmt::capturesVariable(const VarDecl *Var) const { |
Aaron Ballman | c656303a | 2014-03-14 18:08:33 +0000 | [diff] [blame] | 1377 | for (const auto &I : captures()) { |
Alexey Bataev | 2c84541 | 2017-05-15 16:26:15 +0000 | [diff] [blame] | 1378 | if (!I.capturesVariable() && !I.capturesVariableByCopy()) |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1379 | continue; |
Alexey Bataev | e85de8f | 2017-09-20 20:11:31 +0000 | [diff] [blame] | 1380 | if (I.getCapturedVar()->getCanonicalDecl() == Var->getCanonicalDecl()) |
Tareq A. Siraj | 24110cc | 2013-04-16 18:53:08 +0000 | [diff] [blame] | 1381 | return true; |
| 1382 | } |
| 1383 | |
| 1384 | return false; |
| 1385 | } |