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