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" |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 17 | #include "clang/AST/Type.h" |
Ted Kremenek | 11e5a7f | 2009-02-06 01:42:09 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | static struct StmtClassNameTable { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 23 | const char *Name; |
| 24 | unsigned Counter; |
| 25 | unsigned Size; |
Chris Lattner | 1f683e9 | 2007-08-25 01:55:00 +0000 | [diff] [blame] | 26 | } StmtClassInfo[Stmt::lastExprConstant+1]; |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 27 | |
| 28 | static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) { |
| 29 | static bool Initialized = false; |
| 30 | if (Initialized) |
| 31 | return StmtClassInfo[E]; |
| 32 | |
| 33 | // Intialize the table on the first use. |
| 34 | Initialized = true; |
Douglas Gregor | f2cad86 | 2008-11-14 12:46:07 +0000 | [diff] [blame] | 35 | #define STMT(CLASS, PARENT) \ |
| 36 | StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \ |
| 37 | StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | #include "clang/AST/StmtNodes.def" |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 40 | return StmtClassInfo[E]; |
| 41 | } |
| 42 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | const char *Stmt::getStmtClassName() const { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 44 | return getStmtInfoTableEntry(sClass).Name; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 47 | void Stmt::DestroyChildren(ASTContext &C) { |
| 48 | for (child_iterator I = child_begin(), E = child_end(); I !=E; ) |
Douglas Gregor | 860f6d4 | 2009-01-16 06:50:08 +0000 | [diff] [blame] | 49 | if (Stmt* Child = *I++) Child->Destroy(C); |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 52 | void Stmt::Destroy(ASTContext &C) { |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 53 | DestroyChildren(C); |
Ted Kremenek | f809e3b | 2008-05-20 04:10:52 +0000 | [diff] [blame] | 54 | // FIXME: Eventually all Stmts should be allocated with the allocator |
| 55 | // in ASTContext, just like with Decls. |
Ted Kremenek | 11e5a7f | 2009-02-06 01:42:09 +0000 | [diff] [blame] | 56 | this->~Stmt(); |
| 57 | C.Deallocate((void *)this); |
Ted Kremenek | 9c1863e | 2008-05-19 22:02:12 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 24e1e70 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 60 | void DeclStmt::Destroy(ASTContext &C) { |
Ted Kremenek | 11e5a7f | 2009-02-06 01:42:09 +0000 | [diff] [blame] | 61 | this->~DeclStmt(); |
| 62 | C.Deallocate((void *)this); |
Ted Kremenek | 8e355f2 | 2008-05-21 15:53:55 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | void Stmt::PrintStats() { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 66 | // Ensure the table is primed. |
| 67 | getStmtInfoTableEntry(Stmt::NullStmtClass); |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 68 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | unsigned sum = 0; |
| 70 | fprintf(stderr, "*** Stmt/Expr Stats:\n"); |
Chris Lattner | 1f683e9 | 2007-08-25 01:55:00 +0000 | [diff] [blame] | 71 | for (int i = 0; i != Stmt::lastExprConstant+1; i++) { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 72 | if (StmtClassInfo[i].Name == 0) continue; |
| 73 | sum += StmtClassInfo[i].Counter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | } |
| 75 | fprintf(stderr, " %d stmts/exprs total.\n", sum); |
| 76 | sum = 0; |
Chris Lattner | 1f683e9 | 2007-08-25 01:55:00 +0000 | [diff] [blame] | 77 | for (int i = 0; i != Stmt::lastExprConstant+1; i++) { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 78 | if (StmtClassInfo[i].Name == 0) continue; |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 79 | fprintf(stderr, " %d %s, %d each (%d bytes)\n", |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 80 | StmtClassInfo[i].Counter, StmtClassInfo[i].Name, |
| 81 | StmtClassInfo[i].Size, |
| 82 | StmtClassInfo[i].Counter*StmtClassInfo[i].Size); |
| 83 | sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 84 | } |
| 85 | fprintf(stderr, "Total bytes = %d\n", sum); |
| 86 | } |
| 87 | |
| 88 | void Stmt::addStmtClass(StmtClass s) { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 89 | ++getStmtInfoTableEntry(s).Counter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static bool StatSwitch = false; |
| 93 | |
| 94 | bool Stmt::CollectingStats(bool enable) { |
| 95 | if (enable) StatSwitch = true; |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 96 | return StatSwitch; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | const char *LabelStmt::getName() const { |
| 101 | return getID()->getName(); |
| 102 | } |
| 103 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 104 | // 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] | 105 | SourceRange ReturnStmt::getSourceRange() const { |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 106 | if (RetExpr) |
| 107 | return SourceRange(RetLoc, RetExpr->getLocEnd()); |
| 108 | else |
| 109 | return SourceRange(RetLoc); |
| 110 | } |
| 111 | |
Ted Kremenek | d48ade6 | 2007-10-01 16:34:52 +0000 | [diff] [blame] | 112 | bool Stmt::hasImplicitControlFlow() const { |
| 113 | switch (sClass) { |
| 114 | default: |
| 115 | return false; |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | d48ade6 | 2007-10-01 16:34:52 +0000 | [diff] [blame] | 117 | case CallExprClass: |
| 118 | case ConditionalOperatorClass: |
| 119 | case ChooseExprClass: |
| 120 | case StmtExprClass: |
| 121 | case DeclStmtClass: |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 122 | return true; |
| 123 | |
Ted Kremenek | d48ade6 | 2007-10-01 16:34:52 +0000 | [diff] [blame] | 124 | case Stmt::BinaryOperatorClass: { |
| 125 | const BinaryOperator* B = cast<BinaryOperator>(this); |
| 126 | if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma) |
| 127 | return true; |
| 128 | else |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 134 | Expr *AsmStmt::getOutputExpr(unsigned i) { |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 135 | return cast<Expr>(Exprs[i]); |
| 136 | } |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 137 | |
| 138 | /// getOutputConstraint - Return the constraint string for the specified |
| 139 | /// output operand. All output constraints are known to be non-empty (either |
| 140 | /// '=' or '+'). |
| 141 | std::string AsmStmt::getOutputConstraint(unsigned i) const { |
| 142 | return std::string(Constraints[i]->getStrData(), |
| 143 | Constraints[i]->getByteLength()); |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 8575927 | 2009-03-11 00:23:13 +0000 | [diff] [blame] | 146 | /// getNumPlusOperands - Return the number of output operands that have a "+" |
| 147 | /// constraint. |
| 148 | unsigned AsmStmt::getNumPlusOperands() const { |
| 149 | unsigned Res = 0; |
| 150 | for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) |
| 151 | if (isOutputPlusConstraint(i)) |
| 152 | ++Res; |
| 153 | return Res; |
| 154 | } |
| 155 | |
| 156 | |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 157 | |
| 158 | Expr *AsmStmt::getInputExpr(unsigned i) { |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 159 | return cast<Expr>(Exprs[i + NumOutputs]); |
| 160 | } |
Chris Lattner | b327793 | 2009-03-10 04:59:06 +0000 | [diff] [blame] | 161 | |
| 162 | /// getInputConstraint - Return the specified input constraint. Unlike output |
| 163 | /// constraints, these can be empty. |
| 164 | std::string AsmStmt::getInputConstraint(unsigned i) const { |
| 165 | return std::string(Constraints[i + NumOutputs]->getStrData(), |
| 166 | Constraints[i + NumOutputs]->getByteLength()); |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 10ca96a | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 169 | |
| 170 | /// getNamedOperand - Given a symbolic operand reference like %[foo], |
| 171 | /// translate this into a numeric value needed to reference the same operand. |
| 172 | /// This returns -1 if the operand name is invalid. |
| 173 | int AsmStmt::getNamedOperand(const std::string &SymbolicName) const { |
| 174 | unsigned NumPlusOperands = 0; |
| 175 | |
| 176 | // Check if this is an output operand. |
| 177 | for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) { |
| 178 | if (getOutputName(i) == SymbolicName) |
| 179 | return i; |
Chris Lattner | 10ca96a | 2009-03-10 06:33:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | for (unsigned i = 0, e = getNumInputs(); i != e; ++i) |
| 183 | if (getInputName(i) == SymbolicName) |
| 184 | return getNumOutputs() + NumPlusOperands + i; |
| 185 | |
| 186 | // Not found. |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 191 | /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing |
| 192 | /// it into pieces. If the asm string is erroneous, emit errors and return |
| 193 | /// true, otherwise return false. |
Chris Lattner | fb5058e | 2009-03-10 23:41:04 +0000 | [diff] [blame] | 194 | unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces, |
| 195 | ASTContext &C, unsigned &DiagOffs) const { |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 196 | const char *StrStart = getAsmString()->getStrData(); |
| 197 | const char *StrEnd = StrStart + getAsmString()->getByteLength(); |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 198 | const char *CurPtr = StrStart; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 199 | |
| 200 | // "Simple" inline asms have no constraints or operands, just convert the asm |
| 201 | // string to escape $'s. |
| 202 | if (isSimple()) { |
| 203 | std::string Result; |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 204 | for (; CurPtr != StrEnd; ++CurPtr) { |
| 205 | switch (*CurPtr) { |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 206 | case '$': |
| 207 | Result += "$$"; |
| 208 | break; |
| 209 | default: |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 210 | Result += *CurPtr; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | } |
| 214 | Pieces.push_back(AsmStringPiece(Result)); |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 215 | return 0; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // CurStringPiece - The current string that we are building up as we scan the |
| 219 | // asm string. |
| 220 | std::string CurStringPiece; |
| 221 | |
| 222 | while (1) { |
| 223 | // Done with the string? |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 224 | if (CurPtr == StrEnd) { |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 225 | if (!CurStringPiece.empty()) |
| 226 | Pieces.push_back(AsmStringPiece(CurStringPiece)); |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 227 | return 0; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 230 | char CurChar = *CurPtr++; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 231 | if (CurChar == '$') { |
| 232 | CurStringPiece += "$$"; |
| 233 | continue; |
| 234 | } else if (CurChar != '%') { |
| 235 | CurStringPiece += CurChar; |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | // Escaped "%" character in asm string. |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 240 | if (CurPtr == StrEnd) { |
| 241 | // % at end of string is invalid (no escape). |
| 242 | DiagOffs = CurPtr-StrStart-1; |
| 243 | return diag::err_asm_invalid_escape; |
| 244 | } |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 246 | char EscapedChar = *CurPtr++; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 247 | if (EscapedChar == '%') { // %% -> % |
| 248 | // Escaped percentage sign. |
| 249 | CurStringPiece += '%'; |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | if (EscapedChar == '=') { // %= -> Generate an unique ID. |
| 254 | CurStringPiece += "${:uid}"; |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | // Otherwise, we have an operand. If we have accumulated a string so far, |
| 259 | // add it to the Pieces list. |
| 260 | if (!CurStringPiece.empty()) { |
| 261 | Pieces.push_back(AsmStringPiece(CurStringPiece)); |
| 262 | CurStringPiece.clear(); |
| 263 | } |
| 264 | |
| 265 | // Handle %x4 and %x[foo] by capturing x as the modifier character. |
| 266 | char Modifier = '\0'; |
| 267 | if (isalpha(EscapedChar)) { |
| 268 | Modifier = EscapedChar; |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 269 | EscapedChar = *CurPtr++; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (isdigit(EscapedChar)) { |
| 273 | // %n - Assembler operand n |
Chris Lattner | cafc222 | 2009-03-11 22:52:17 +0000 | [diff] [blame] | 274 | unsigned N = 0; |
| 275 | |
| 276 | --CurPtr; |
| 277 | while (CurPtr != StrEnd && isdigit(*CurPtr)) |
Chris Lattner | 32a47ed | 2009-03-11 23:09:16 +0000 | [diff] [blame] | 278 | N = N*10 + ((*CurPtr++)-'0'); |
Chris Lattner | 8575927 | 2009-03-11 00:23:13 +0000 | [diff] [blame] | 279 | |
| 280 | unsigned NumOperands = |
| 281 | getNumOutputs() + getNumPlusOperands() + getNumInputs(); |
| 282 | if (N >= NumOperands) { |
| 283 | DiagOffs = CurPtr-StrStart-1; |
| 284 | return diag::err_asm_invalid_operand_number; |
| 285 | } |
| 286 | |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 287 | Pieces.push_back(AsmStringPiece(N, Modifier)); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | // Handle %[foo], a symbolic operand reference. |
| 292 | if (EscapedChar == '[') { |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 293 | DiagOffs = CurPtr-StrStart-1; |
| 294 | |
| 295 | // Find the ']'. |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 296 | const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr); |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 297 | if (NameEnd == 0) |
| 298 | return diag::err_asm_unterminated_symbolic_operand_name; |
| 299 | if (NameEnd == CurPtr) |
| 300 | return diag::err_asm_empty_symbolic_operand_name; |
| 301 | |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 302 | std::string SymbolicName(CurPtr, NameEnd); |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 303 | |
| 304 | int N = getNamedOperand(SymbolicName); |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 305 | if (N == -1) { |
| 306 | // Verify that an operand with that name exists. |
| 307 | DiagOffs = CurPtr-StrStart; |
| 308 | return diag::err_asm_unknown_symbolic_operand_name; |
| 309 | } |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 310 | Pieces.push_back(AsmStringPiece(N, Modifier)); |
Chris Lattner | eab8cfb | 2009-03-11 00:06:36 +0000 | [diff] [blame] | 311 | |
| 312 | CurPtr = NameEnd+1; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 313 | continue; |
| 314 | } |
| 315 | |
Chris Lattner | 2ff0f42 | 2009-03-10 23:57:07 +0000 | [diff] [blame] | 316 | DiagOffs = CurPtr-StrStart-1; |
Chris Lattner | 3182db1 | 2009-03-10 23:51:40 +0000 | [diff] [blame] | 317 | return diag::err_asm_invalid_escape; |
Chris Lattner | 458cd9c | 2009-03-10 23:21:44 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 321 | //===----------------------------------------------------------------------===// |
| 322 | // Constructors |
| 323 | //===----------------------------------------------------------------------===// |
| 324 | |
Anders Carlsson | dfab34a | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 325 | AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile, |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 326 | unsigned numoutputs, unsigned numinputs, |
| 327 | std::string *names, StringLiteral **constraints, |
| 328 | Expr **exprs, StringLiteral *asmstr, unsigned numclobbers, |
| 329 | StringLiteral **clobbers, SourceLocation rparenloc) |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 330 | : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr) |
Anders Carlsson | dfab34a | 2008-02-05 23:03:50 +0000 | [diff] [blame] | 331 | , IsSimple(issimple), IsVolatile(isvolatile) |
| 332 | , NumOutputs(numoutputs), NumInputs(numinputs) { |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 333 | for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) { |
| 334 | Names.push_back(names[i]); |
| 335 | Exprs.push_back(exprs[i]); |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 336 | Constraints.push_back(constraints[i]); |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 337 | } |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 338 | |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 339 | for (unsigned i = 0; i != numclobbers; i++) |
| 340 | Clobbers.push_back(clobbers[i]); |
| 341 | } |
| 342 | |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 343 | ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, |
| 344 | Stmt *Body, SourceLocation FCL, |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 345 | SourceLocation RPL) |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 346 | : Stmt(ObjCForCollectionStmtClass) { |
| 347 | SubExprs[ELEM] = Elem; |
| 348 | SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect); |
| 349 | SubExprs[BODY] = Body; |
| 350 | ForLoc = FCL; |
| 351 | RParenLoc = RPL; |
| 352 | } |
| 353 | |
| 354 | |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 355 | ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc, |
| 356 | SourceLocation rparenloc, |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 357 | ParmVarDecl *catchVarDecl, Stmt *atCatchStmt, |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 358 | Stmt *atCatchList) |
| 359 | : Stmt(ObjCAtCatchStmtClass) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 360 | ExceptionDecl = catchVarDecl; |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 361 | SubExprs[BODY] = atCatchStmt; |
Eli Friedman | 0613c37 | 2008-05-25 04:34:57 +0000 | [diff] [blame] | 362 | SubExprs[NEXT_CATCH] = NULL; |
Daniel Dunbar | 93b2bdb | 2009-03-01 04:28:32 +0000 | [diff] [blame] | 363 | // FIXME: O(N^2) in number of catch blocks. |
Eli Friedman | 0613c37 | 2008-05-25 04:34:57 +0000 | [diff] [blame] | 364 | if (atCatchList) { |
Ted Kremenek | ff98102 | 2008-02-01 21:28:59 +0000 | [diff] [blame] | 365 | ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList); |
| 366 | |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 367 | while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt()) |
Ted Kremenek | ff98102 | 2008-02-01 21:28:59 +0000 | [diff] [blame] | 368 | AtCatchList = NextCatch; |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | ff98102 | 2008-02-01 21:28:59 +0000 | [diff] [blame] | 370 | AtCatchList->SubExprs[NEXT_CATCH] = this; |
Chris Lattner | db6ed17 | 2008-01-30 05:01:46 +0000 | [diff] [blame] | 371 | } |
| 372 | AtCatchLoc = atCatchLoc; |
| 373 | RParenLoc = rparenloc; |
| 374 | } |
| 375 | |
| 376 | |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 377 | //===----------------------------------------------------------------------===// |
| 378 | // Child Iterators for iterating over subexpressions/substatements |
| 379 | //===----------------------------------------------------------------------===// |
| 380 | |
| 381 | // DeclStmt |
Ted Kremenek | 8ffb159 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 382 | Stmt::child_iterator DeclStmt::child_begin() { |
| 383 | return StmtIterator(DG.begin(), DG.end()); |
Ted Kremenek | 14f8b4f | 2008-08-05 20:46:55 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Ted Kremenek | 8ffb159 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 386 | Stmt::child_iterator DeclStmt::child_end() { |
| 387 | return StmtIterator(DG.end(), DG.end()); |
Ted Kremenek | 65aa3b9 | 2008-10-06 20:54:44 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 390 | // NullStmt |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 391 | Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); } |
| 392 | Stmt::child_iterator NullStmt::child_end() { return child_iterator(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 393 | |
| 394 | // CompoundStmt |
| 395 | Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 396 | Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 397 | |
Ted Kremenek | d97bb6c | 2007-08-30 16:50:46 +0000 | [diff] [blame] | 398 | // CaseStmt |
| 399 | Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; } |
| 400 | Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; } |
| 401 | |
| 402 | // DefaultStmt |
| 403 | Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; } |
| 404 | Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 405 | |
| 406 | // LabelStmt |
| 407 | Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; } |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 408 | Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 409 | |
| 410 | // IfStmt |
| 411 | Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; } |
| 412 | Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 413 | |
| 414 | // SwitchStmt |
| 415 | Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; } |
| 416 | Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 417 | |
| 418 | // WhileStmt |
| 419 | Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; } |
| 420 | Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 421 | |
| 422 | // DoStmt |
| 423 | Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; } |
| 424 | Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 425 | |
| 426 | // ForStmt |
| 427 | Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; } |
| 428 | Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 429 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 430 | // ObjCForCollectionStmt |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 431 | Stmt::child_iterator ObjCForCollectionStmt::child_begin() { |
| 432 | return &SubExprs[0]; |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 433 | } |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 434 | Stmt::child_iterator ObjCForCollectionStmt::child_end() { |
| 435 | return &SubExprs[0]+END_EXPR; |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 438 | // GotoStmt |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 439 | Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); } |
| 440 | Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 441 | |
| 442 | // IndirectGotoStmt |
Ted Kremenek | 1060aff | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 443 | Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); } |
| 444 | const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 445 | |
Ted Kremenek | 1060aff | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 446 | Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; } |
| 447 | Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 448 | |
| 449 | // ContinueStmt |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 450 | Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); } |
| 451 | Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 452 | |
| 453 | // BreakStmt |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 454 | Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); } |
| 455 | Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 456 | |
| 457 | // ReturnStmt |
Ted Kremenek | 1060aff | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 458 | const Expr* ReturnStmt::getRetValue() const { |
| 459 | return cast_or_null<Expr>(RetExpr); |
| 460 | } |
| 461 | Expr* ReturnStmt::getRetValue() { |
| 462 | return cast_or_null<Expr>(RetExpr); |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Ted Kremenek | 1060aff | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 465 | Stmt::child_iterator ReturnStmt::child_begin() { |
| 466 | return &RetExpr; |
| 467 | } |
| 468 | Stmt::child_iterator ReturnStmt::child_end() { |
| 469 | return RetExpr ? &RetExpr+1 : &RetExpr; |
Ted Kremenek | 2298f91 | 2007-08-27 20:58:16 +0000 | [diff] [blame] | 470 | } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 471 | |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 472 | // AsmStmt |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 473 | Stmt::child_iterator AsmStmt::child_begin() { |
| 474 | return Exprs.empty() ? 0 : &Exprs[0]; |
| 475 | } |
| 476 | Stmt::child_iterator AsmStmt::child_end() { |
| 477 | return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size(); |
| 478 | } |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 479 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 480 | // ObjCAtCatchStmt |
| 481 | Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; } |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 482 | Stmt::child_iterator ObjCAtCatchStmt::child_end() { |
| 483 | return &SubExprs[0]+END_EXPR; |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 484 | } |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 486 | // ObjCAtFinallyStmt |
| 487 | Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; } |
| 488 | Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; } |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 489 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 490 | // ObjCAtTryStmt |
| 491 | Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; } |
Nico Weber | 608b17f | 2008-08-05 23:15:29 +0000 | [diff] [blame] | 492 | Stmt::child_iterator ObjCAtTryStmt::child_end() { |
| 493 | return &SubStmts[0]+END_EXPR; |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 494 | } |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 495 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 496 | // ObjCAtThrowStmt |
| 497 | Stmt::child_iterator ObjCAtThrowStmt::child_begin() { |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 498 | return &Throw; |
| 499 | } |
| 500 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 501 | Stmt::child_iterator ObjCAtThrowStmt::child_end() { |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 502 | return &Throw+1; |
| 503 | } |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 504 | |
| 505 | // ObjCAtSynchronizedStmt |
| 506 | Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 507 | return &SubStmts[0]; |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 511 | return &SubStmts[0]+END_EXPR; |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 514 | // CXXCatchStmt |
| 515 | Stmt::child_iterator CXXCatchStmt::child_begin() { |
| 516 | return &HandlerBlock; |
| 517 | } |
| 518 | |
| 519 | Stmt::child_iterator CXXCatchStmt::child_end() { |
| 520 | return &HandlerBlock + 1; |
| 521 | } |
| 522 | |
| 523 | QualType CXXCatchStmt::getCaughtType() { |
| 524 | if (ExceptionDecl) |
| 525 | return llvm::cast<VarDecl>(ExceptionDecl)->getType(); |
| 526 | return QualType(); |
| 527 | } |
| 528 | |
| 529 | void CXXCatchStmt::Destroy(ASTContext& C) { |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 530 | if (ExceptionDecl) |
| 531 | ExceptionDecl->Destroy(C); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 532 | Stmt::Destroy(C); |
| 533 | } |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 534 | |
| 535 | // CXXTryStmt |
| 536 | Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; } |
| 537 | Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); } |
| 538 | |
| 539 | CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, |
| 540 | Stmt **handlers, unsigned numHandlers) |
| 541 | : Stmt(CXXTryStmtClass), TryLoc(tryLoc) { |
| 542 | Stmts.push_back(tryBlock); |
| 543 | Stmts.insert(Stmts.end(), handlers, handlers + numHandlers); |
| 544 | } |