Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Stmt.cpp - Statement AST Node Implementation ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Stmt class and statement subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/ASTDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprObjC.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 18 | #include "clang/AST/Stmt.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtCXX.h" |
| 20 | #include "clang/AST/StmtObjC.h" |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
Chris Lattner | 9bffb07 | 2010-04-23 16:29:58 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Token.h" |
Chad Rosier | be3ace8 | 2012-08-24 17:05:45 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | b43c8ec | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | static struct StmtClassNameTable { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 29 | const char *Name; |
| 30 | unsigned Counter; |
| 31 | unsigned Size; |
Sean Hunt | 4bfe196 | 2010-05-05 15:24:00 +0000 | [diff] [blame] | 32 | } StmtClassInfo[Stmt::lastStmtConstant+1]; |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 33 | |
| 34 | static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) { |
| 35 | static bool Initialized = false; |
| 36 | if (Initialized) |
| 37 | return StmtClassInfo[E]; |
| 38 | |
| 39 | // Intialize the table on the first use. |
| 40 | Initialized = true; |
Sean Hunt | 7381d5c | 2010-05-18 06:22:21 +0000 | [diff] [blame] | 41 | #define ABSTRACT_STMT(STMT) |
Douglas Gregor | f2cad86 | 2008-11-14 12:46:07 +0000 | [diff] [blame] | 42 | #define STMT(CLASS, PARENT) \ |
| 43 | StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \ |
| 44 | StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS); |
Sean Hunt | 4bfe196 | 2010-05-05 15:24:00 +0000 | [diff] [blame] | 45 | #include "clang/AST/StmtNodes.inc" |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 47 | return StmtClassInfo[E]; |
| 48 | } |
| 49 | |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 50 | void *Stmt::operator new(size_t bytes, ASTContext& C, |
| 51 | unsigned alignment) throw() { |
| 52 | return ::operator new(bytes, C, alignment); |
| 53 | } |
| 54 | |
| 55 | void *Stmt::operator new(size_t bytes, ASTContext* C, |
| 56 | unsigned alignment) throw() { |
| 57 | return ::operator new(bytes, *C, alignment); |
| 58 | } |
| 59 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | const char *Stmt::getStmtClassName() const { |
John McCall | 8e6285a | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 61 | return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void Stmt::PrintStats() { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 65 | // Ensure the table is primed. |
| 66 | getStmtInfoTableEntry(Stmt::NullStmtClass); |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 67 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | unsigned sum = 0; |
Chandler Carruth | b43c8ec | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 69 | llvm::errs() << "\n*** Stmt/Expr Stats:\n"; |
Sean Hunt | 4bfe196 | 2010-05-05 15:24:00 +0000 | [diff] [blame] | 70 | for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 71 | if (StmtClassInfo[i].Name == 0) continue; |
| 72 | sum += StmtClassInfo[i].Counter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | } |
Chandler Carruth | b43c8ec | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 74 | llvm::errs() << " " << sum << " stmts/exprs total.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | sum = 0; |
Sean Hunt | 4bfe196 | 2010-05-05 15:24:00 +0000 | [diff] [blame] | 76 | for (int i = 0; i != Stmt::lastStmtConstant+1; i++) { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 77 | if (StmtClassInfo[i].Name == 0) continue; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 78 | if (StmtClassInfo[i].Counter == 0) continue; |
Chandler Carruth | b43c8ec | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 79 | llvm::errs() << " " << StmtClassInfo[i].Counter << " " |
| 80 | << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size |
| 81 | << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size |
| 82 | << " bytes)\n"; |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 83 | sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 84 | } |
Chandler Carruth | b43c8ec | 2011-07-04 06:13:27 +0000 | [diff] [blame] | 85 | |
| 86 | llvm::errs() << "Total bytes = " << sum << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void Stmt::addStmtClass(StmtClass s) { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 90 | ++getStmtInfoTableEntry(s).Counter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Daniel Dunbar | 02892a6 | 2012-03-05 21:42:49 +0000 | [diff] [blame] | 93 | bool Stmt::StatisticsEnabled = false; |
| 94 | void Stmt::EnableStatistics() { |
| 95 | StatisticsEnabled = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | } |
| 97 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 98 | Stmt *Stmt::IgnoreImplicit() { |
| 99 | Stmt *s = this; |
| 100 | |
| 101 | if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s)) |
| 102 | s = ewc->getSubExpr(); |
| 103 | |
| 104 | while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s)) |
| 105 | s = ice->getSubExpr(); |
| 106 | |
| 107 | return s; |
| 108 | } |
| 109 | |
Chandler Carruth | a1364be | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 110 | /// \brief Strip off all label-like statements. |
| 111 | /// |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 112 | /// This will strip off label statements, case statements, attributed |
| 113 | /// statements and default statements recursively. |
Chandler Carruth | a1364be | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 114 | const Stmt *Stmt::stripLabelLikeStatements() const { |
| 115 | const Stmt *S = this; |
| 116 | while (true) { |
| 117 | if (const LabelStmt *LS = dyn_cast<LabelStmt>(S)) |
| 118 | S = LS->getSubStmt(); |
| 119 | else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) |
| 120 | S = SC->getSubStmt(); |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 121 | else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S)) |
| 122 | S = AS->getSubStmt(); |
Chandler Carruth | a1364be | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 123 | else |
| 124 | return S; |
| 125 | } |
| 126 | } |
| 127 | |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 128 | namespace { |
| 129 | struct good {}; |
| 130 | struct bad {}; |
John McCall | f8c7fdb | 2011-02-09 08:31:17 +0000 | [diff] [blame] | 131 | |
| 132 | // These silly little functions have to be static inline to suppress |
| 133 | // unused warnings, and they have to be defined to suppress other |
| 134 | // warnings. |
Nick Lewycky | 086eb9f | 2011-02-09 08:42:57 +0000 | [diff] [blame] | 135 | static inline good is_good(good) { return good(); } |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 136 | |
| 137 | typedef Stmt::child_range children_t(); |
Nick Lewycky | 086eb9f | 2011-02-09 08:42:57 +0000 | [diff] [blame] | 138 | template <class T> good implements_children(children_t T::*) { |
| 139 | return good(); |
| 140 | } |
| 141 | static inline bad implements_children(children_t Stmt::*) { |
| 142 | return bad(); |
| 143 | } |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 144 | |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 145 | typedef SourceLocation getLocStart_t() const; |
| 146 | template <class T> good implements_getLocStart(getLocStart_t T::*) { |
Nick Lewycky | 086eb9f | 2011-02-09 08:42:57 +0000 | [diff] [blame] | 147 | return good(); |
| 148 | } |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 149 | static inline bad implements_getLocStart(getLocStart_t Stmt::*) { |
| 150 | return bad(); |
| 151 | } |
| 152 | |
| 153 | typedef SourceLocation getLocEnd_t() const; |
| 154 | template <class T> good implements_getLocEnd(getLocEnd_t T::*) { |
| 155 | return good(); |
| 156 | } |
| 157 | static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) { |
Nick Lewycky | 086eb9f | 2011-02-09 08:42:57 +0000 | [diff] [blame] | 158 | return bad(); |
| 159 | } |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 160 | |
| 161 | #define ASSERT_IMPLEMENTS_children(type) \ |
| 162 | (void) sizeof(is_good(implements_children(&type::children))) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 163 | #define ASSERT_IMPLEMENTS_getLocStart(type) \ |
| 164 | (void) sizeof(is_good(implements_getLocStart(&type::getLocStart))) |
| 165 | #define ASSERT_IMPLEMENTS_getLocEnd(type) \ |
| 166 | (void) sizeof(is_good(implements_getLocEnd(&type::getLocEnd))) |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /// Check whether the various Stmt classes implement their member |
| 170 | /// functions. |
| 171 | static inline void check_implementations() { |
| 172 | #define ABSTRACT_STMT(type) |
| 173 | #define STMT(type, base) \ |
| 174 | ASSERT_IMPLEMENTS_children(type); \ |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 175 | ASSERT_IMPLEMENTS_getLocStart(type); \ |
| 176 | ASSERT_IMPLEMENTS_getLocEnd(type); |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 177 | #include "clang/AST/StmtNodes.inc" |
| 178 | } |
| 179 | |
| 180 | Stmt::child_range Stmt::children() { |
| 181 | switch (getStmtClass()) { |
| 182 | case Stmt::NoStmtClass: llvm_unreachable("statement without class"); |
| 183 | #define ABSTRACT_STMT(type) |
| 184 | #define STMT(type, base) \ |
| 185 | case Stmt::type##Class: \ |
| 186 | return static_cast<type*>(this)->children(); |
| 187 | #include "clang/AST/StmtNodes.inc" |
| 188 | } |
| 189 | llvm_unreachable("unknown statement kind!"); |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 192 | // Amusing macro metaprogramming hack: check whether a class provides |
| 193 | // a more specific implementation of getSourceRange. |
| 194 | // |
| 195 | // See also Expr.cpp:getExprLoc(). |
| 196 | namespace { |
| 197 | /// This implementation is used when a class provides a custom |
| 198 | /// implementation of getSourceRange. |
| 199 | template <class S, class T> |
| 200 | SourceRange getSourceRangeImpl(const Stmt *stmt, |
| 201 | SourceRange (T::*v)() const) { |
| 202 | return static_cast<const S*>(stmt)->getSourceRange(); |
| 203 | } |
| 204 | |
| 205 | /// This implementation is used when a class doesn't provide a custom |
| 206 | /// implementation of getSourceRange. Overload resolution should pick it over |
| 207 | /// the implementation above because it's more specialized according to |
| 208 | /// function template partial ordering. |
| 209 | template <class S> |
| 210 | SourceRange getSourceRangeImpl(const Stmt *stmt, |
| 211 | SourceRange (Stmt::*v)() const) { |
| 212 | return SourceRange(static_cast<const S*>(stmt)->getLocStart(), |
| 213 | static_cast<const S*>(stmt)->getLocEnd()); |
| 214 | } |
| 215 | } |
| 216 | |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 217 | SourceRange Stmt::getSourceRange() const { |
| 218 | switch (getStmtClass()) { |
| 219 | case Stmt::NoStmtClass: llvm_unreachable("statement without class"); |
| 220 | #define ABSTRACT_STMT(type) |
| 221 | #define STMT(type, base) \ |
| 222 | case Stmt::type##Class: \ |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 223 | return getSourceRangeImpl<type>(this, &type::getSourceRange); |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 224 | #include "clang/AST/StmtNodes.inc" |
| 225 | } |
| 226 | llvm_unreachable("unknown statement kind!"); |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Daniel Dunbar | 90e25a8 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 229 | SourceLocation Stmt::getLocStart() const { |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 230 | // llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n"; |
Daniel Dunbar | 90e25a8 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 231 | switch (getStmtClass()) { |
| 232 | case Stmt::NoStmtClass: llvm_unreachable("statement without class"); |
| 233 | #define ABSTRACT_STMT(type) |
| 234 | #define STMT(type, base) \ |
| 235 | case Stmt::type##Class: \ |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 236 | return static_cast<const type*>(this)->getLocStart(); |
Daniel Dunbar | 90e25a8 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 237 | #include "clang/AST/StmtNodes.inc" |
| 238 | } |
| 239 | llvm_unreachable("unknown statement kind"); |
| 240 | } |
| 241 | |
| 242 | SourceLocation Stmt::getLocEnd() const { |
| 243 | switch (getStmtClass()) { |
| 244 | case Stmt::NoStmtClass: llvm_unreachable("statement without class"); |
| 245 | #define ABSTRACT_STMT(type) |
| 246 | #define STMT(type, base) \ |
| 247 | case Stmt::type##Class: \ |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 248 | return static_cast<const type*>(this)->getLocEnd(); |
Daniel Dunbar | 90e25a8 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 249 | #include "clang/AST/StmtNodes.inc" |
| 250 | } |
| 251 | llvm_unreachable("unknown statement kind"); |
| 252 | } |
| 253 | |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 254 | CompoundStmt::CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts, |
| 255 | SourceLocation LB, SourceLocation RB) |
| 256 | : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) { |
| 257 | CompoundStmtBits.NumStmts = NumStmts; |
| 258 | assert(CompoundStmtBits.NumStmts == NumStmts && |
| 259 | "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!"); |
| 260 | |
| 261 | if (NumStmts == 0) { |
| 262 | Body = 0; |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | Body = new (C) Stmt*[NumStmts]; |
| 267 | memcpy(Body, StmtStart, NumStmts * sizeof(*Body)); |
| 268 | } |
| 269 | |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 270 | void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) { |
| 271 | if (this->Body) |
| 272 | C.Deallocate(Body); |
John McCall | 8e6285a | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 273 | this->CompoundStmtBits.NumStmts = NumStmts; |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 274 | |
| 275 | Body = new (C) Stmt*[NumStmts]; |
| 276 | memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts); |
| 277 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | const char *LabelStmt::getName() const { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 280 | return getDecl()->getIdentifier()->getNameStart(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Alexander Kornienko | 4990890 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 283 | AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc, |
| 284 | ArrayRef<const Attr*> Attrs, |
| 285 | Stmt *SubStmt) { |
| 286 | void *Mem = C.Allocate(sizeof(AttributedStmt) + |
| 287 | sizeof(Attr*) * (Attrs.size() - 1), |
| 288 | llvm::alignOf<AttributedStmt>()); |
| 289 | return new (Mem) AttributedStmt(Loc, Attrs, SubStmt); |
| 290 | } |
| 291 | |
| 292 | AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) { |
| 293 | assert(NumAttrs > 0 && "NumAttrs should be greater than zero"); |
| 294 | void *Mem = C.Allocate(sizeof(AttributedStmt) + |
| 295 | sizeof(Attr*) * (NumAttrs - 1), |
| 296 | llvm::alignOf<AttributedStmt>()); |
| 297 | return new (Mem) AttributedStmt(EmptyShell(), NumAttrs); |
| 298 | } |
| 299 | |
Ted Kremenek | d48ade6 | 2007-10-01 16:34:52 +0000 | [diff] [blame] | 300 | bool Stmt::hasImplicitControlFlow() const { |
John McCall | 8e6285a | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 301 | switch (StmtBits.sClass) { |
Ted Kremenek | d48ade6 | 2007-10-01 16:34:52 +0000 | [diff] [blame] | 302 | default: |
| 303 | return false; |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | d48ade6 | 2007-10-01 16:34:52 +0000 | [diff] [blame] | 305 | case CallExprClass: |
| 306 | case ConditionalOperatorClass: |
| 307 | case ChooseExprClass: |
| 308 | case StmtExprClass: |
| 309 | case DeclStmtClass: |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 310 | return true; |
| 311 | |
Ted Kremenek | d48ade6 | 2007-10-01 16:34:52 +0000 | [diff] [blame] | 312 | case Stmt::BinaryOperatorClass: { |
| 313 | const BinaryOperator* B = cast<BinaryOperator>(this); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 314 | if (B->isLogicalOp() || B->getOpcode() == BO_Comma) |
Ted Kremenek | d48ade6 | 2007-10-01 16:34:52 +0000 | [diff] [blame] | 315 | return true; |
| 316 | else |
| 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Chad Rosier | aba59aa | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 322 | std::string AsmStmt::generateAsmString(ASTContext &C) const { |
Chad Rosier | 9252701 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 323 | if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
| 324 | return gccAsmStmt->generateAsmString(C); |
| 325 | if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
| 326 | return msAsmStmt->generateAsmString(C); |
Chad Rosier | aba59aa | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 327 | llvm_unreachable("unknown asm statement kind!"); |
| 328 | } |
| 329 | |
| 330 | StringRef AsmStmt::getOutputConstraint(unsigned i) const { |
Chad Rosier | 9252701 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 331 | if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
| 332 | return gccAsmStmt->getOutputConstraint(i); |
| 333 | if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
| 334 | return msAsmStmt->getOutputConstraint(i); |
Chad Rosier | aba59aa | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 335 | llvm_unreachable("unknown asm statement kind!"); |
| 336 | } |
| 337 | |
| 338 | const Expr *AsmStmt::getOutputExpr(unsigned i) const { |
Chad Rosier | 9252701 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 339 | if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
| 340 | return gccAsmStmt->getOutputExpr(i); |
| 341 | if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
| 342 | return msAsmStmt->getOutputExpr(i); |
Chad Rosier | aba59aa | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 343 | llvm_unreachable("unknown asm statement kind!"); |
| 344 | } |
| 345 | |
| 346 | StringRef AsmStmt::getInputConstraint(unsigned i) const { |
Chad Rosier | 9252701 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 347 | if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
| 348 | return gccAsmStmt->getInputConstraint(i); |
| 349 | if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
| 350 | return msAsmStmt->getInputConstraint(i); |
Chad Rosier | aba59aa | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 351 | llvm_unreachable("unknown asm statement kind!"); |
| 352 | } |
| 353 | |
| 354 | const Expr *AsmStmt::getInputExpr(unsigned i) const { |
Chad Rosier | 9252701 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 355 | if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
| 356 | return gccAsmStmt->getInputExpr(i); |
| 357 | if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
| 358 | return msAsmStmt->getInputExpr(i); |
Chad Rosier | aba59aa | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 359 | llvm_unreachable("unknown asm statement kind!"); |
| 360 | } |
| 361 | |
| 362 | StringRef AsmStmt::getClobber(unsigned i) const { |
Chad Rosier | 9252701 | 2012-08-28 18:21:14 +0000 | [diff] [blame] | 363 | if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this)) |
| 364 | return gccAsmStmt->getClobber(i); |
| 365 | if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this)) |
| 366 | return msAsmStmt->getClobber(i); |
Chad Rosier | aba59aa | 2012-08-28 17:43:23 +0000 | [diff] [blame] | 367 | llvm_unreachable("unknown asm statement kind!"); |
| 368 | } |
| 369 | |
Chad Rosier | c4fb221 | 2012-08-28 00:24:05 +0000 | [diff] [blame] | 370 | /// getNumPlusOperands - Return the number of output operands that have a "+" |
| 371 | /// constraint. |
| 372 | unsigned AsmStmt::getNumPlusOperands() const { |
| 373 | unsigned Res = 0; |
| 374 | for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) |
| 375 | if (isOutputPlusConstraint(i)) |
| 376 | ++Res; |
| 377 | return Res; |
| 378 | } |
| 379 | |
Chad Rosier | 33f0558 | 2012-08-27 23:47:56 +0000 | [diff] [blame] | 380 | StringRef GCCAsmStmt::getClobber(unsigned i) const { |
| 381 | return getClobberStringLiteral(i)->getString(); |
| 382 | } |
| 383 | |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 384 | Expr *GCCAsmStmt::getOutputExpr(unsigned i) { |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 385 | return cast<Expr>(Exprs[i]); |
| 386 | } |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 387 | |
| 388 | /// getOutputConstraint - Return the constraint string for the specified |
| 389 | /// output operand. All output constraints are known to be non-empty (either |
| 390 | /// '=' or '+'). |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 391 | StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const { |
Anders Carlsson | acb6bcb | 2010-01-30 20:38:10 +0000 | [diff] [blame] | 392 | return getOutputConstraintLiteral(i)->getString(); |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 393 | } |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 394 | |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 395 | Expr *GCCAsmStmt::getInputExpr(unsigned i) { |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 396 | return cast<Expr>(Exprs[i + NumOutputs]); |
| 397 | } |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 398 | void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) { |
Chris Lattner | 935f0f0 | 2011-02-21 22:09:29 +0000 | [diff] [blame] | 399 | Exprs[i + NumOutputs] = E; |
| 400 | } |
| 401 | |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 402 | /// getInputConstraint - Return the specified input constraint. Unlike output |
| 403 | /// constraints, these can be empty. |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 404 | StringRef GCCAsmStmt::getInputConstraint(unsigned i) const { |
Anders Carlsson | acb6bcb | 2010-01-30 20:38:10 +0000 | [diff] [blame] | 405 | return getInputConstraintLiteral(i)->getString(); |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 408 | void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C, |
Anders Carlsson | ff93dbd | 2010-01-30 22:25:16 +0000 | [diff] [blame] | 409 | IdentifierInfo **Names, |
Anders Carlsson | fdba9c0 | 2010-01-30 19:34:25 +0000 | [diff] [blame] | 410 | StringLiteral **Constraints, |
| 411 | Stmt **Exprs, |
| 412 | unsigned NumOutputs, |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 413 | unsigned NumInputs, |
Anders Carlsson | fdba9c0 | 2010-01-30 19:34:25 +0000 | [diff] [blame] | 414 | StringLiteral **Clobbers, |
| 415 | unsigned NumClobbers) { |
Douglas Gregor | cd7d5a9 | 2009-04-17 20:57:14 +0000 | [diff] [blame] | 416 | this->NumOutputs = NumOutputs; |
| 417 | this->NumInputs = NumInputs; |
Anders Carlsson | 966146e | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 418 | this->NumClobbers = NumClobbers; |
| 419 | |
| 420 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 421 | |
Anders Carlsson | 966146e | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 422 | C.Deallocate(this->Names); |
| 423 | this->Names = new (C) IdentifierInfo*[NumExprs]; |
| 424 | std::copy(Names, Names + NumExprs, this->Names); |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 425 | |
Anders Carlsson | 966146e | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 426 | C.Deallocate(this->Exprs); |
| 427 | this->Exprs = new (C) Stmt*[NumExprs]; |
| 428 | std::copy(Exprs, Exprs + NumExprs, this->Exprs); |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 429 | |
Anders Carlsson | 966146e | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 430 | C.Deallocate(this->Constraints); |
| 431 | this->Constraints = new (C) StringLiteral*[NumExprs]; |
| 432 | std::copy(Constraints, Constraints + NumExprs, this->Constraints); |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 433 | |
Anders Carlsson | 966146e | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 434 | C.Deallocate(this->Clobbers); |
| 435 | this->Clobbers = new (C) StringLiteral*[NumClobbers]; |
| 436 | std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers); |
Douglas Gregor | cd7d5a9 | 2009-04-17 20:57:14 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | 10ca96a | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 439 | /// getNamedOperand - Given a symbolic operand reference like %[foo], |
| 440 | /// translate this into a numeric value needed to reference the same operand. |
| 441 | /// This returns -1 if the operand name is invalid. |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 442 | int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const { |
Chris Lattner | 10ca96a | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 443 | unsigned NumPlusOperands = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 10ca96a | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 445 | // Check if this is an output operand. |
| 446 | for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) { |
| 447 | if (getOutputName(i) == SymbolicName) |
| 448 | return i; |
Chris Lattner | 10ca96a | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 449 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 10ca96a | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 451 | for (unsigned i = 0, e = getNumInputs(); i != e; ++i) |
| 452 | if (getInputName(i) == SymbolicName) |
| 453 | return getNumOutputs() + NumPlusOperands + i; |
| 454 | |
| 455 | // Not found. |
| 456 | return -1; |
| 457 | } |
| 458 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 459 | /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing |
| 460 | /// it into pieces. If the asm string is erroneous, emit errors and return |
| 461 | /// true, otherwise return false. |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 462 | unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces, |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 463 | ASTContext &C, unsigned &DiagOffs) const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 464 | StringRef Str = getAsmString()->getString(); |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 465 | const char *StrStart = Str.begin(); |
| 466 | const char *StrEnd = Str.end(); |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 467 | const char *CurPtr = StrStart; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 469 | // "Simple" inline asms have no constraints or operands, just convert the asm |
| 470 | // string to escape $'s. |
| 471 | if (isSimple()) { |
| 472 | std::string Result; |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 473 | for (; CurPtr != StrEnd; ++CurPtr) { |
| 474 | switch (*CurPtr) { |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 475 | case '$': |
| 476 | Result += "$$"; |
| 477 | break; |
| 478 | default: |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 479 | Result += *CurPtr; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 480 | break; |
| 481 | } |
| 482 | } |
| 483 | Pieces.push_back(AsmStringPiece(Result)); |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 484 | return 0; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // CurStringPiece - The current string that we are building up as we scan the |
| 488 | // asm string. |
| 489 | std::string CurStringPiece; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 491 | bool HasVariants = !C.getTargetInfo().hasNoAsmVariants(); |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 493 | while (1) { |
| 494 | // Done with the string? |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 495 | if (CurPtr == StrEnd) { |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 496 | if (!CurStringPiece.empty()) |
| 497 | Pieces.push_back(AsmStringPiece(CurStringPiece)); |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 498 | return 0; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 501 | char CurChar = *CurPtr++; |
Chris Lattner | 018b54e | 2010-04-05 18:44:00 +0000 | [diff] [blame] | 502 | switch (CurChar) { |
| 503 | case '$': CurStringPiece += "$$"; continue; |
Chris Lattner | 9bffb07 | 2010-04-23 16:29:58 +0000 | [diff] [blame] | 504 | case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue; |
| 505 | case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue; |
| 506 | case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue; |
Chris Lattner | 018b54e | 2010-04-05 18:44:00 +0000 | [diff] [blame] | 507 | case '%': |
| 508 | break; |
| 509 | default: |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 510 | CurStringPiece += CurChar; |
| 511 | continue; |
| 512 | } |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 514 | // Escaped "%" character in asm string. |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 515 | if (CurPtr == StrEnd) { |
| 516 | // % at end of string is invalid (no escape). |
| 517 | DiagOffs = CurPtr-StrStart-1; |
| 518 | return diag::err_asm_invalid_escape; |
| 519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 521 | char EscapedChar = *CurPtr++; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 522 | if (EscapedChar == '%') { // %% -> % |
| 523 | // Escaped percentage sign. |
| 524 | CurStringPiece += '%'; |
| 525 | continue; |
| 526 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 528 | if (EscapedChar == '=') { // %= -> Generate an unique ID. |
| 529 | CurStringPiece += "${:uid}"; |
| 530 | continue; |
| 531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 533 | // Otherwise, we have an operand. If we have accumulated a string so far, |
| 534 | // add it to the Pieces list. |
| 535 | if (!CurStringPiece.empty()) { |
| 536 | Pieces.push_back(AsmStringPiece(CurStringPiece)); |
| 537 | CurStringPiece.clear(); |
| 538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 540 | // Handle %x4 and %x[foo] by capturing x as the modifier character. |
| 541 | char Modifier = '\0'; |
| 542 | if (isalpha(EscapedChar)) { |
Benjamin Kramer | bc57f3c | 2011-07-05 11:13:37 +0000 | [diff] [blame] | 543 | if (CurPtr == StrEnd) { // Premature end. |
| 544 | DiagOffs = CurPtr-StrStart-1; |
| 545 | return diag::err_asm_invalid_escape; |
| 546 | } |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 547 | Modifier = EscapedChar; |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 548 | EscapedChar = *CurPtr++; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 549 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 551 | if (isdigit(EscapedChar)) { |
| 552 | // %n - Assembler operand n |
Chris Lattner | cafc222 | 2009-03-11 22:52:17 +0000 | [diff] [blame] | 553 | unsigned N = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Chris Lattner | cafc222 | 2009-03-11 22:52:17 +0000 | [diff] [blame] | 555 | --CurPtr; |
| 556 | while (CurPtr != StrEnd && isdigit(*CurPtr)) |
Chris Lattner | 32a47ed | 2009-03-11 23:09:16 +0000 | [diff] [blame] | 557 | N = N*10 + ((*CurPtr++)-'0'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 8575927 | 2009-03-11 00:23:13 +0000 | [diff] [blame] | 559 | unsigned NumOperands = |
| 560 | getNumOutputs() + getNumPlusOperands() + getNumInputs(); |
| 561 | if (N >= NumOperands) { |
| 562 | DiagOffs = CurPtr-StrStart-1; |
| 563 | return diag::err_asm_invalid_operand_number; |
| 564 | } |
| 565 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 566 | Pieces.push_back(AsmStringPiece(N, Modifier)); |
| 567 | continue; |
| 568 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 570 | // Handle %[foo], a symbolic operand reference. |
| 571 | if (EscapedChar == '[') { |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 572 | DiagOffs = CurPtr-StrStart-1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 574 | // Find the ']'. |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 575 | const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr); |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 576 | if (NameEnd == 0) |
| 577 | return diag::err_asm_unterminated_symbolic_operand_name; |
| 578 | if (NameEnd == CurPtr) |
| 579 | return diag::err_asm_empty_symbolic_operand_name; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 581 | StringRef SymbolicName(CurPtr, NameEnd - CurPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 583 | int N = getNamedOperand(SymbolicName); |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 584 | if (N == -1) { |
| 585 | // Verify that an operand with that name exists. |
| 586 | DiagOffs = CurPtr-StrStart; |
| 587 | return diag::err_asm_unknown_symbolic_operand_name; |
| 588 | } |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 589 | Pieces.push_back(AsmStringPiece(N, Modifier)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 591 | CurPtr = NameEnd+1; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 592 | continue; |
| 593 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 2ff0f42 | 2009-03-10 23:57:07 +0000 | [diff] [blame] | 595 | DiagOffs = CurPtr-StrStart-1; |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 596 | return diag::err_asm_invalid_escape; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
Chad Rosier | da083b2 | 2012-08-27 20:23:31 +0000 | [diff] [blame] | 599 | |
| 600 | /// Assemble final IR asm string (GCC-style). |
| 601 | std::string GCCAsmStmt::generateAsmString(ASTContext &C) const { |
Chad Rosier | be3ace8 | 2012-08-24 17:05:45 +0000 | [diff] [blame] | 602 | // Analyze the asm string to decompose it into its pieces. We know that Sema |
| 603 | // has already done this, so it is guaranteed to be successful. |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 604 | SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces; |
Chad Rosier | be3ace8 | 2012-08-24 17:05:45 +0000 | [diff] [blame] | 605 | unsigned DiagOffs; |
| 606 | AnalyzeAsmString(Pieces, C, DiagOffs); |
| 607 | |
| 608 | std::string AsmString; |
| 609 | for (unsigned i = 0, e = Pieces.size(); i != e; ++i) { |
| 610 | if (Pieces[i].isString()) |
| 611 | AsmString += Pieces[i].getString(); |
| 612 | else if (Pieces[i].getModifier() == '\0') |
| 613 | AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo()); |
| 614 | else |
| 615 | AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' + |
| 616 | Pieces[i].getModifier() + '}'; |
| 617 | } |
| 618 | return AsmString; |
| 619 | } |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 620 | |
Chad Rosier | da083b2 | 2012-08-27 20:23:31 +0000 | [diff] [blame] | 621 | /// Assemble final IR asm string (MS-style). |
| 622 | std::string MSAsmStmt::generateAsmString(ASTContext &C) const { |
| 623 | // FIXME: This needs to be translated into the IR string representation. |
Chad Rosier | 00d1637 | 2012-08-28 20:33:49 +0000 | [diff] [blame] | 624 | return AsmStr; |
Chad Rosier | da083b2 | 2012-08-27 20:23:31 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Chad Rosier | 633abb0 | 2012-08-24 00:07:09 +0000 | [diff] [blame] | 627 | Expr *MSAsmStmt::getOutputExpr(unsigned i) { |
| 628 | return cast<Expr>(Exprs[i]); |
| 629 | } |
| 630 | |
| 631 | Expr *MSAsmStmt::getInputExpr(unsigned i) { |
| 632 | return cast<Expr>(Exprs[i + NumOutputs]); |
| 633 | } |
| 634 | void MSAsmStmt::setInputExpr(unsigned i, Expr *E) { |
| 635 | Exprs[i + NumOutputs] = E; |
| 636 | } |
| 637 | |
Sam Weinig | b0e4cb6 | 2010-02-03 02:09:59 +0000 | [diff] [blame] | 638 | QualType CXXCatchStmt::getCaughtType() const { |
| 639 | if (ExceptionDecl) |
| 640 | return ExceptionDecl->getType(); |
| 641 | return QualType(); |
| 642 | } |
| 643 | |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 644 | //===----------------------------------------------------------------------===// |
| 645 | // Constructors |
| 646 | //===----------------------------------------------------------------------===// |
| 647 | |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 648 | GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, |
| 649 | bool isvolatile, unsigned numoutputs, unsigned numinputs, |
| 650 | IdentifierInfo **names, StringLiteral **constraints, |
| 651 | Expr **exprs, StringLiteral *asmstr, |
| 652 | unsigned numclobbers, StringLiteral **clobbers, |
| 653 | SourceLocation rparenloc) |
Chad Rosier | 066ef86 | 2012-08-27 19:38:01 +0000 | [diff] [blame] | 654 | : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs, |
| 655 | numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) { |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 656 | |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 657 | unsigned NumExprs = NumOutputs + NumInputs; |
| 658 | |
Anders Carlsson | 966146e | 2010-01-30 23:19:41 +0000 | [diff] [blame] | 659 | Names = new (C) IdentifierInfo*[NumExprs]; |
| 660 | std::copy(names, names + NumExprs, Names); |
| 661 | |
| 662 | Exprs = new (C) Stmt*[NumExprs]; |
| 663 | std::copy(exprs, exprs + NumExprs, Exprs); |
| 664 | |
| 665 | Constraints = new (C) StringLiteral*[NumExprs]; |
| 666 | std::copy(constraints, constraints + NumExprs, Constraints); |
| 667 | |
| 668 | Clobbers = new (C) StringLiteral*[NumClobbers]; |
| 669 | std::copy(clobbers, clobbers + NumClobbers, Clobbers); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Chad Rosier | 058ab17 | 2012-08-16 00:06:53 +0000 | [diff] [blame] | 672 | MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc, |
| 673 | SourceLocation lbraceloc, bool issimple, bool isvolatile, |
Chad Rosier | e54cba1 | 2012-10-16 21:55:39 +0000 | [diff] [blame] | 674 | ArrayRef<Token> asmtoks, unsigned numoutputs, |
| 675 | unsigned numinputs, ArrayRef<IdentifierInfo*> names, |
| 676 | ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs, |
| 677 | StringRef asmstr, ArrayRef<StringRef> clobbers, |
| 678 | SourceLocation endloc) |
| 679 | : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs, |
| 680 | numinputs, clobbers.size()), LBraceLoc(lbraceloc), |
Chad Rosier | 12603e2 | 2012-09-04 16:36:26 +0000 | [diff] [blame] | 681 | EndLoc(endloc), AsmStr(asmstr.str()), NumAsmToks(asmtoks.size()) { |
Chad Rosier | 058ab17 | 2012-08-16 00:06:53 +0000 | [diff] [blame] | 682 | |
| 683 | unsigned NumExprs = NumOutputs + NumInputs; |
| 684 | |
| 685 | Names = new (C) IdentifierInfo*[NumExprs]; |
Chad Rosier | e54cba1 | 2012-10-16 21:55:39 +0000 | [diff] [blame] | 686 | for (unsigned i = 0, e = NumExprs; i != e; ++i) |
| 687 | Names[i] = names[i]; |
Chad Rosier | 79efe24 | 2012-08-07 00:29:06 +0000 | [diff] [blame] | 688 | |
Chad Rosier | 633abb0 | 2012-08-24 00:07:09 +0000 | [diff] [blame] | 689 | Exprs = new (C) Stmt*[NumExprs]; |
Chad Rosier | e54cba1 | 2012-10-16 21:55:39 +0000 | [diff] [blame] | 690 | for (unsigned i = 0, e = NumExprs; i != e; ++i) |
| 691 | Exprs[i] = exprs[i]; |
Chad Rosier | 633abb0 | 2012-08-24 00:07:09 +0000 | [diff] [blame] | 692 | |
Chad Rosier | 79efe24 | 2012-08-07 00:29:06 +0000 | [diff] [blame] | 693 | AsmToks = new (C) Token[NumAsmToks]; |
| 694 | for (unsigned i = 0, e = NumAsmToks; i != e; ++i) |
| 695 | AsmToks[i] = asmtoks[i]; |
Chad Rosier | 62f22b8 | 2012-08-08 19:48:07 +0000 | [diff] [blame] | 696 | |
Chad Rosier | 89fb6d7 | 2012-08-28 20:28:20 +0000 | [diff] [blame] | 697 | Constraints = new (C) StringRef[NumExprs]; |
| 698 | for (unsigned i = 0, e = NumExprs; i != e; ++i) { |
| 699 | size_t size = constraints[i].size(); |
| 700 | char *dest = new (C) char[size]; |
| 701 | std::strncpy(dest, constraints[i].data(), size); |
| 702 | Constraints[i] = StringRef(dest, size); |
| 703 | } |
| 704 | |
Chad Rosier | 33c72e1 | 2012-08-10 21:36:25 +0000 | [diff] [blame] | 705 | Clobbers = new (C) StringRef[NumClobbers]; |
Chad Rosier | e790bc3 | 2012-08-10 21:06:19 +0000 | [diff] [blame] | 706 | for (unsigned i = 0, e = NumClobbers; i != e; ++i) { |
| 707 | // FIXME: Avoid the allocation/copy if at all possible. |
| 708 | size_t size = clobbers[i].size(); |
| 709 | char *dest = new (C) char[size]; |
| 710 | std::strncpy(dest, clobbers[i].data(), size); |
Chad Rosier | 33c72e1 | 2012-08-10 21:36:25 +0000 | [diff] [blame] | 711 | Clobbers[i] = StringRef(dest, size); |
Chad Rosier | e790bc3 | 2012-08-10 21:06:19 +0000 | [diff] [blame] | 712 | } |
Chad Rosier | 8cd64b4 | 2012-06-11 20:47:18 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 715 | ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, |
| 716 | Stmt *Body, SourceLocation FCL, |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 717 | SourceLocation RPL) |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 718 | : Stmt(ObjCForCollectionStmtClass) { |
| 719 | SubExprs[ELEM] = Elem; |
| 720 | SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect); |
| 721 | SubExprs[BODY] = Body; |
| 722 | ForLoc = FCL; |
| 723 | RParenLoc = RPL; |
| 724 | } |
| 725 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 726 | ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt, |
| 727 | Stmt **CatchStmts, unsigned NumCatchStmts, |
| 728 | Stmt *atFinallyStmt) |
| 729 | : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc), |
| 730 | NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0) |
| 731 | { |
| 732 | Stmt **Stmts = getStmts(); |
| 733 | Stmts[0] = atTryStmt; |
| 734 | for (unsigned I = 0; I != NumCatchStmts; ++I) |
| 735 | Stmts[I + 1] = CatchStmts[I]; |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 736 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 737 | if (HasFinally) |
| 738 | Stmts[NumCatchStmts + 1] = atFinallyStmt; |
| 739 | } |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 740 | |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 741 | ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context, |
| 742 | SourceLocation atTryLoc, |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 743 | Stmt *atTryStmt, |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 744 | Stmt **CatchStmts, |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 745 | unsigned NumCatchStmts, |
| 746 | Stmt *atFinallyStmt) { |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 747 | unsigned Size = sizeof(ObjCAtTryStmt) + |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 748 | (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *); |
Chris Lattner | 3248854 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 749 | void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>()); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 750 | return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts, |
| 751 | atFinallyStmt); |
| 752 | } |
Ted Kremenek | ff98102 | 2008-02-01 21:28:59 +0000 | [diff] [blame] | 753 | |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 754 | ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context, |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 755 | unsigned NumCatchStmts, |
| 756 | bool HasFinally) { |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 757 | unsigned Size = sizeof(ObjCAtTryStmt) + |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 758 | (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *); |
Chris Lattner | 3248854 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 759 | void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>()); |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 760 | return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 761 | } |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 762 | |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 763 | SourceLocation ObjCAtTryStmt::getLocEnd() const { |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 764 | if (HasFinally) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame^] | 765 | return getFinallyStmt()->getLocEnd(); |
| 766 | if (NumCatchStmts) |
| 767 | return getCatchStmt(NumCatchStmts - 1)->getLocEnd(); |
| 768 | return getTryBody()->getLocEnd(); |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Sam Weinig | a1a396d | 2010-02-03 03:56:39 +0000 | [diff] [blame] | 771 | CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc, |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 772 | Stmt *tryBlock, Stmt **handlers, |
Sam Weinig | a1a396d | 2010-02-03 03:56:39 +0000 | [diff] [blame] | 773 | unsigned numHandlers) { |
| 774 | std::size_t Size = sizeof(CXXTryStmt); |
| 775 | Size += ((numHandlers + 1) * sizeof(Stmt)); |
| 776 | |
Chris Lattner | 3248854 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 777 | void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>()); |
Sam Weinig | a1a396d | 2010-02-03 03:56:39 +0000 | [diff] [blame] | 778 | return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers); |
| 779 | } |
| 780 | |
Argyrios Kyrtzidis | 7cb45e3 | 2010-07-22 16:03:56 +0000 | [diff] [blame] | 781 | CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty, |
| 782 | unsigned numHandlers) { |
| 783 | std::size_t Size = sizeof(CXXTryStmt); |
| 784 | Size += ((numHandlers + 1) * sizeof(Stmt)); |
| 785 | |
Chris Lattner | 3248854 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 786 | void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>()); |
Argyrios Kyrtzidis | 7cb45e3 | 2010-07-22 16:03:56 +0000 | [diff] [blame] | 787 | return new (Mem) CXXTryStmt(Empty, numHandlers); |
| 788 | } |
| 789 | |
Sam Weinig | a1a396d | 2010-02-03 03:56:39 +0000 | [diff] [blame] | 790 | CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, |
Sam Weinig | b0e4cb6 | 2010-02-03 02:09:59 +0000 | [diff] [blame] | 791 | Stmt **handlers, unsigned numHandlers) |
| 792 | : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) { |
Sam Weinig | a1a396d | 2010-02-03 03:56:39 +0000 | [diff] [blame] | 793 | Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1); |
Sam Weinig | b0e4cb6 | 2010-02-03 02:09:59 +0000 | [diff] [blame] | 794 | Stmts[0] = tryBlock; |
| 795 | std::copy(handlers, handlers + NumHandlers, Stmts + 1); |
| 796 | } |
| 797 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 798 | CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt, |
| 799 | Expr *Cond, Expr *Inc, DeclStmt *LoopVar, |
| 800 | Stmt *Body, SourceLocation FL, |
| 801 | SourceLocation CL, SourceLocation RPL) |
| 802 | : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) { |
| 803 | SubExprs[RANGE] = Range; |
| 804 | SubExprs[BEGINEND] = BeginEndStmt; |
| 805 | SubExprs[COND] = reinterpret_cast<Stmt*>(Cond); |
| 806 | SubExprs[INC] = reinterpret_cast<Stmt*>(Inc); |
| 807 | SubExprs[LOOPVAR] = LoopVar; |
| 808 | SubExprs[BODY] = Body; |
| 809 | } |
| 810 | |
| 811 | Expr *CXXForRangeStmt::getRangeInit() { |
| 812 | DeclStmt *RangeStmt = getRangeStmt(); |
| 813 | VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl()); |
| 814 | assert(RangeDecl &&& "for-range should have a single var decl"); |
| 815 | return RangeDecl->getInit(); |
| 816 | } |
| 817 | |
| 818 | const Expr *CXXForRangeStmt::getRangeInit() const { |
| 819 | return const_cast<CXXForRangeStmt*>(this)->getRangeInit(); |
| 820 | } |
| 821 | |
| 822 | VarDecl *CXXForRangeStmt::getLoopVariable() { |
| 823 | Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl(); |
| 824 | assert(LV && "No loop variable in CXXForRangeStmt"); |
| 825 | return cast<VarDecl>(LV); |
| 826 | } |
| 827 | |
| 828 | const VarDecl *CXXForRangeStmt::getLoopVariable() const { |
| 829 | return const_cast<CXXForRangeStmt*>(this)->getLoopVariable(); |
| 830 | } |
| 831 | |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 832 | IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond, |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 833 | Stmt *then, SourceLocation EL, Stmt *elsev) |
| 834 | : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL) |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 835 | { |
| 836 | setConditionVariable(C, var); |
| 837 | SubExprs[COND] = reinterpret_cast<Stmt*>(cond); |
| 838 | SubExprs[THEN] = then; |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 839 | SubExprs[ELSE] = elsev; |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | VarDecl *IfStmt::getConditionVariable() const { |
| 843 | if (!SubExprs[VAR]) |
| 844 | return 0; |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 845 | |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 846 | DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]); |
| 847 | return cast<VarDecl>(DS->getSingleDecl()); |
| 848 | } |
| 849 | |
| 850 | void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) { |
| 851 | if (!V) { |
| 852 | SubExprs[VAR] = 0; |
| 853 | return; |
| 854 | } |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 855 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 856 | SourceRange VarRange = V->getSourceRange(); |
| 857 | SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(), |
| 858 | VarRange.getEnd()); |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 861 | ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, |
| 862 | Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP, |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 863 | SourceLocation RP) |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 864 | : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP) |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 865 | { |
| 866 | SubExprs[INIT] = Init; |
| 867 | setConditionVariable(C, condVar); |
| 868 | SubExprs[COND] = reinterpret_cast<Stmt*>(Cond); |
| 869 | SubExprs[INC] = reinterpret_cast<Stmt*>(Inc); |
| 870 | SubExprs[BODY] = Body; |
| 871 | } |
| 872 | |
| 873 | VarDecl *ForStmt::getConditionVariable() const { |
| 874 | if (!SubExprs[CONDVAR]) |
| 875 | return 0; |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 877 | DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]); |
| 878 | return cast<VarDecl>(DS->getSingleDecl()); |
| 879 | } |
| 880 | |
| 881 | void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) { |
| 882 | if (!V) { |
| 883 | SubExprs[CONDVAR] = 0; |
| 884 | return; |
| 885 | } |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 886 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 887 | SourceRange VarRange = V->getSourceRange(); |
| 888 | SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(), |
| 889 | VarRange.getEnd()); |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 892 | SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond) |
| 893 | : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0) |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 894 | { |
| 895 | setConditionVariable(C, Var); |
| 896 | SubExprs[COND] = reinterpret_cast<Stmt*>(cond); |
| 897 | SubExprs[BODY] = NULL; |
| 898 | } |
| 899 | |
| 900 | VarDecl *SwitchStmt::getConditionVariable() const { |
| 901 | if (!SubExprs[VAR]) |
| 902 | return 0; |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 903 | |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 904 | DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]); |
| 905 | return cast<VarDecl>(DS->getSingleDecl()); |
| 906 | } |
| 907 | |
| 908 | void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) { |
| 909 | if (!V) { |
| 910 | SubExprs[VAR] = 0; |
| 911 | return; |
| 912 | } |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 913 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 914 | SourceRange VarRange = V->getSourceRange(); |
| 915 | SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(), |
| 916 | VarRange.getEnd()); |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 917 | } |
| 918 | |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 919 | Stmt *SwitchCase::getSubStmt() { |
Chris Lattner | c4002c7 | 2011-02-28 00:18:06 +0000 | [diff] [blame] | 920 | if (isa<CaseStmt>(this)) |
| 921 | return cast<CaseStmt>(this)->getSubStmt(); |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 922 | return cast<DefaultStmt>(this)->getSubStmt(); |
| 923 | } |
| 924 | |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 925 | WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body, |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 926 | SourceLocation WL) |
Chris Lattner | c4002c7 | 2011-02-28 00:18:06 +0000 | [diff] [blame] | 927 | : Stmt(WhileStmtClass) { |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 928 | setConditionVariable(C, Var); |
| 929 | SubExprs[COND] = reinterpret_cast<Stmt*>(cond); |
| 930 | SubExprs[BODY] = body; |
| 931 | WhileLoc = WL; |
| 932 | } |
| 933 | |
| 934 | VarDecl *WhileStmt::getConditionVariable() const { |
| 935 | if (!SubExprs[VAR]) |
| 936 | return 0; |
Chad Rosier | 0e2a868 | 2012-08-07 23:12:23 +0000 | [diff] [blame] | 937 | |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 938 | DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]); |
| 939 | return cast<VarDecl>(DS->getSingleDecl()); |
| 940 | } |
| 941 | |
| 942 | void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) { |
| 943 | if (!V) { |
| 944 | SubExprs[VAR] = 0; |
| 945 | return; |
| 946 | } |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 947 | |
| 948 | SourceRange VarRange = V->getSourceRange(); |
| 949 | SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(), |
| 950 | VarRange.getEnd()); |
Douglas Gregor | 43dec6b | 2010-06-21 23:44:13 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 953 | // IndirectGotoStmt |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 954 | LabelDecl *IndirectGotoStmt::getConstantTarget() { |
John McCall | 95c225d | 2010-10-28 08:53:48 +0000 | [diff] [blame] | 955 | if (AddrLabelExpr *E = |
| 956 | dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts())) |
| 957 | return E->getLabel(); |
| 958 | return 0; |
| 959 | } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 960 | |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 961 | // ReturnStmt |
Ted Kremenek | 1060aff | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 962 | const Expr* ReturnStmt::getRetValue() const { |
| 963 | return cast_or_null<Expr>(RetExpr); |
| 964 | } |
| 965 | Expr* ReturnStmt::getRetValue() { |
| 966 | return cast_or_null<Expr>(RetExpr); |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 967 | } |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 968 | |
| 969 | SEHTryStmt::SEHTryStmt(bool IsCXXTry, |
| 970 | SourceLocation TryLoc, |
| 971 | Stmt *TryBlock, |
| 972 | Stmt *Handler) |
| 973 | : Stmt(SEHTryStmtClass), |
| 974 | IsCXXTry(IsCXXTry), |
| 975 | TryLoc(TryLoc) |
| 976 | { |
| 977 | Children[TRY] = TryBlock; |
| 978 | Children[HANDLER] = Handler; |
| 979 | } |
| 980 | |
| 981 | SEHTryStmt* SEHTryStmt::Create(ASTContext &C, |
| 982 | bool IsCXXTry, |
| 983 | SourceLocation TryLoc, |
| 984 | Stmt *TryBlock, |
| 985 | Stmt *Handler) { |
| 986 | return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler); |
| 987 | } |
| 988 | |
| 989 | SEHExceptStmt* SEHTryStmt::getExceptHandler() const { |
| 990 | return dyn_cast<SEHExceptStmt>(getHandler()); |
| 991 | } |
| 992 | |
| 993 | SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const { |
| 994 | return dyn_cast<SEHFinallyStmt>(getHandler()); |
| 995 | } |
| 996 | |
| 997 | SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, |
| 998 | Expr *FilterExpr, |
| 999 | Stmt *Block) |
| 1000 | : Stmt(SEHExceptStmtClass), |
| 1001 | Loc(Loc) |
| 1002 | { |
| 1003 | Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr); |
| 1004 | Children[BLOCK] = Block; |
| 1005 | } |
| 1006 | |
| 1007 | SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C, |
| 1008 | SourceLocation Loc, |
| 1009 | Expr *FilterExpr, |
| 1010 | Stmt *Block) { |
| 1011 | return new(C) SEHExceptStmt(Loc,FilterExpr,Block); |
| 1012 | } |
| 1013 | |
| 1014 | SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, |
| 1015 | Stmt *Block) |
| 1016 | : Stmt(SEHFinallyStmtClass), |
| 1017 | Loc(Loc), |
| 1018 | Block(Block) |
| 1019 | {} |
| 1020 | |
| 1021 | SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C, |
| 1022 | SourceLocation Loc, |
| 1023 | Stmt *Block) { |
| 1024 | return new(C)SEHFinallyStmt(Loc,Block); |
| 1025 | } |