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