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