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