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