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