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