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