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