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