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