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