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" |
| 16 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | static struct StmtClassNameTable { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 21 | const char *Name; |
| 22 | unsigned Counter; |
| 23 | unsigned Size; |
Chris Lattner | 1f683e9 | 2007-08-25 01:55:00 +0000 | [diff] [blame] | 24 | } StmtClassInfo[Stmt::lastExprConstant+1]; |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 25 | |
| 26 | static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) { |
| 27 | static bool Initialized = false; |
| 28 | if (Initialized) |
| 29 | return StmtClassInfo[E]; |
| 30 | |
| 31 | // Intialize the table on the first use. |
| 32 | Initialized = true; |
| 33 | #define STMT(N, CLASS, PARENT) \ |
| 34 | StmtClassInfo[N].Name = #CLASS; \ |
| 35 | StmtClassInfo[N].Size = sizeof(CLASS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | #include "clang/AST/StmtNodes.def" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 38 | return StmtClassInfo[E]; |
| 39 | } |
| 40 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | const char *Stmt::getStmtClassName() const { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 42 | return getStmtInfoTableEntry(sClass).Name; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void Stmt::PrintStats() { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 46 | // Ensure the table is primed. |
| 47 | getStmtInfoTableEntry(Stmt::NullStmtClass); |
| 48 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | unsigned sum = 0; |
| 50 | fprintf(stderr, "*** Stmt/Expr Stats:\n"); |
Chris Lattner | 1f683e9 | 2007-08-25 01:55:00 +0000 | [diff] [blame] | 51 | for (int i = 0; i != Stmt::lastExprConstant+1; i++) { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 52 | if (StmtClassInfo[i].Name == 0) continue; |
| 53 | sum += StmtClassInfo[i].Counter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | } |
| 55 | fprintf(stderr, " %d stmts/exprs total.\n", sum); |
| 56 | sum = 0; |
Chris Lattner | 1f683e9 | 2007-08-25 01:55:00 +0000 | [diff] [blame] | 57 | for (int i = 0; i != Stmt::lastExprConstant+1; i++) { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 58 | if (StmtClassInfo[i].Name == 0) continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | fprintf(stderr, " %d %s, %d each (%d bytes)\n", |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 60 | StmtClassInfo[i].Counter, StmtClassInfo[i].Name, |
| 61 | StmtClassInfo[i].Size, |
| 62 | StmtClassInfo[i].Counter*StmtClassInfo[i].Size); |
| 63 | sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | } |
| 65 | fprintf(stderr, "Total bytes = %d\n", sum); |
| 66 | } |
| 67 | |
| 68 | void Stmt::addStmtClass(StmtClass s) { |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 69 | ++getStmtInfoTableEntry(s).Counter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static bool StatSwitch = false; |
| 73 | |
| 74 | bool Stmt::CollectingStats(bool enable) { |
| 75 | if (enable) StatSwitch = true; |
Chris Lattner | 6338135 | 2007-08-25 01:42:24 +0000 | [diff] [blame] | 76 | return StatSwitch; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | const char *LabelStmt::getName() const { |
| 81 | return getID()->getName(); |
| 82 | } |
| 83 | |
Steve Naroff | 507f2d5 | 2007-08-31 23:49:30 +0000 | [diff] [blame] | 84 | // This is defined here to avoid polluting Stmt.h with importing Expr.h |
| 85 | SourceRange ReturnStmt::getSourceRange() const { |
| 86 | if (RetExpr) |
| 87 | return SourceRange(RetLoc, RetExpr->getLocEnd()); |
| 88 | else |
| 89 | return SourceRange(RetLoc); |
| 90 | } |
| 91 | |
Ted Kremenek | d48ade6 | 2007-10-01 16:34:52 +0000 | [diff] [blame] | 92 | bool Stmt::hasImplicitControlFlow() const { |
| 93 | switch (sClass) { |
| 94 | default: |
| 95 | return false; |
| 96 | |
| 97 | case CallExprClass: |
| 98 | case ConditionalOperatorClass: |
| 99 | case ChooseExprClass: |
| 100 | case StmtExprClass: |
| 101 | case DeclStmtClass: |
| 102 | return true; |
| 103 | |
| 104 | case Stmt::BinaryOperatorClass: { |
| 105 | const BinaryOperator* B = cast<BinaryOperator>(this); |
| 106 | if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma) |
| 107 | return true; |
| 108 | else |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 114 | AsmStmt::AsmStmt(SourceLocation asmloc, |
Anders Carlsson | 39c47b5 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 115 | bool isvolatile, |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 116 | unsigned numoutputs, |
| 117 | unsigned numinputs, |
| 118 | std::string *names, |
| 119 | StringLiteral **constraints, |
| 120 | Expr **exprs, |
| 121 | StringLiteral *asmstr, |
| 122 | unsigned numclobbers, |
| 123 | StringLiteral **clobbers, |
| 124 | SourceLocation rparenloc) |
| 125 | : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr) |
Anders Carlsson | 39c47b5 | 2007-11-23 23:12:25 +0000 | [diff] [blame] | 126 | , IsVolatile(isvolatile), NumOutputs(numoutputs), NumInputs(numinputs) |
Anders Carlsson | b235fc2 | 2007-11-22 01:36:19 +0000 | [diff] [blame] | 127 | { |
| 128 | for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) { |
| 129 | Names.push_back(names[i]); |
| 130 | Exprs.push_back(exprs[i]); |
| 131 | Constraints.push_back(constraints[i]); |
| 132 | } |
| 133 | |
| 134 | for (unsigned i = 0; i != numclobbers; i++) |
| 135 | Clobbers.push_back(clobbers[i]); |
| 136 | } |
| 137 | |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 138 | //===----------------------------------------------------------------------===// |
| 139 | // Child Iterators for iterating over subexpressions/substatements |
| 140 | //===----------------------------------------------------------------------===// |
| 141 | |
| 142 | // DeclStmt |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 143 | Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); } |
| 144 | Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 145 | |
| 146 | // NullStmt |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 147 | Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); } |
| 148 | Stmt::child_iterator NullStmt::child_end() { return child_iterator(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 149 | |
| 150 | // CompoundStmt |
| 151 | Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; } |
| 152 | Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); } |
| 153 | |
Ted Kremenek | d97bb6c | 2007-08-30 16:50:46 +0000 | [diff] [blame] | 154 | // CaseStmt |
| 155 | Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; } |
| 156 | Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; } |
| 157 | |
| 158 | // DefaultStmt |
| 159 | Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; } |
| 160 | Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 161 | |
| 162 | // LabelStmt |
| 163 | Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; } |
Chris Lattner | b393879 | 2007-08-30 00:53:54 +0000 | [diff] [blame] | 164 | Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 165 | |
| 166 | // IfStmt |
| 167 | Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; } |
| 168 | Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 169 | |
| 170 | // SwitchStmt |
| 171 | Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; } |
| 172 | Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 173 | |
| 174 | // WhileStmt |
| 175 | Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; } |
| 176 | Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 177 | |
| 178 | // DoStmt |
| 179 | Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; } |
| 180 | Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 181 | |
| 182 | // ForStmt |
| 183 | Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; } |
| 184 | Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 185 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame^] | 186 | // ObjCForCollectionStmt |
| 187 | Stmt::child_iterator ObjCForCollectionStmt::child_begin() { |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 188 | return &SubExprs[0]; |
| 189 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame^] | 190 | Stmt::child_iterator ObjCForCollectionStmt::child_end() { |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 191 | return &SubExprs[0]+END_EXPR; |
| 192 | } |
| 193 | |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 194 | // GotoStmt |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 195 | Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); } |
| 196 | Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 197 | |
| 198 | // IndirectGotoStmt |
| 199 | Stmt::child_iterator IndirectGotoStmt::child_begin() { |
| 200 | return reinterpret_cast<Stmt**>(&Target); |
| 201 | } |
| 202 | |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 203 | Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 204 | |
| 205 | // ContinueStmt |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 206 | Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); } |
| 207 | Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 208 | |
| 209 | // BreakStmt |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 210 | Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); } |
| 211 | Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 212 | |
| 213 | // ReturnStmt |
Ted Kremenek | 2298f91 | 2007-08-27 20:58:16 +0000 | [diff] [blame] | 214 | Stmt::child_iterator ReturnStmt::child_begin() { |
| 215 | if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 216 | else return child_iterator(); |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Ted Kremenek | 2298f91 | 2007-08-27 20:58:16 +0000 | [diff] [blame] | 219 | Stmt::child_iterator ReturnStmt::child_end() { |
| 220 | if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr)+1; |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 221 | else return child_iterator(); |
Ted Kremenek | 2298f91 | 2007-08-27 20:58:16 +0000 | [diff] [blame] | 222 | } |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 223 | |
Chris Lattner | fe79595 | 2007-10-29 04:04:16 +0000 | [diff] [blame] | 224 | // AsmStmt |
| 225 | Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); } |
| 226 | Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); } |
| 227 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame^] | 228 | // ObjCAtCatchStmt |
| 229 | Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; } |
| 230 | Stmt::child_iterator ObjCAtCatchStmt::child_end() { |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 231 | return &SubExprs[0]+END_EXPR; |
| 232 | } |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame^] | 234 | // ObjCAtFinallyStmt |
| 235 | Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; } |
| 236 | Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; } |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame^] | 238 | // ObjCAtTryStmt |
| 239 | Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; } |
| 240 | Stmt::child_iterator ObjCAtTryStmt::child_end() { |
Fariborz Jahanian | 89079ea | 2007-11-07 17:43:16 +0000 | [diff] [blame] | 241 | return &SubStmts[0]+END_EXPR; |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 242 | } |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 243 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame^] | 244 | // ObjCAtThrowStmt |
| 245 | Stmt::child_iterator ObjCAtThrowStmt::child_begin() { |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 246 | return &Throw; |
| 247 | } |
| 248 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame^] | 249 | Stmt::child_iterator ObjCAtThrowStmt::child_end() { |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 250 | return &Throw+1; |
| 251 | } |