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