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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 17 | #include "clang/Lex/IdentifierTable.h" |
| 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 | |
Ted Kremenek | 8297777 | 2007-08-24 21:09:09 +0000 | [diff] [blame] | 84 | //===----------------------------------------------------------------------===// |
| 85 | // Child Iterators for iterating over subexpressions/substatements |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
| 88 | // DeclStmt |
| 89 | Stmt::child_iterator DeclStmt::child_begin() { return NULL; } |
| 90 | Stmt::child_iterator DeclStmt::child_end() { return NULL; } |
| 91 | |
| 92 | // NullStmt |
| 93 | Stmt::child_iterator NullStmt::child_begin() { return NULL; } |
| 94 | Stmt::child_iterator NullStmt::child_end() { return NULL; } |
| 95 | |
| 96 | // CompoundStmt |
| 97 | Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; } |
| 98 | Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); } |
| 99 | |
| 100 | // SwitchCase |
| 101 | Stmt::child_iterator SwitchCase::child_begin() { return &SubStmt; } |
| 102 | Stmt::child_iterator SwitchCase::child_end() { return child_begin()+1; } |
| 103 | |
| 104 | // LabelStmt |
| 105 | Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; } |
| 106 | Stmt::child_iterator LabelStmt::child_end() { return child_begin()+1; } |
| 107 | |
| 108 | // IfStmt |
| 109 | Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; } |
| 110 | Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 111 | |
| 112 | // SwitchStmt |
| 113 | Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; } |
| 114 | Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 115 | |
| 116 | // WhileStmt |
| 117 | Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; } |
| 118 | Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 119 | |
| 120 | // DoStmt |
| 121 | Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; } |
| 122 | Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 123 | |
| 124 | // ForStmt |
| 125 | Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; } |
| 126 | Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; } |
| 127 | |
| 128 | // GotoStmt |
| 129 | Stmt::child_iterator GotoStmt::child_begin() { return NULL; } |
| 130 | Stmt::child_iterator GotoStmt::child_end() { return NULL; } |
| 131 | |
| 132 | // IndirectGotoStmt |
| 133 | Stmt::child_iterator IndirectGotoStmt::child_begin() { |
| 134 | return reinterpret_cast<Stmt**>(&Target); |
| 135 | } |
| 136 | |
| 137 | Stmt::child_iterator IndirectGotoStmt::child_end() { return child_begin()+1; } |
| 138 | |
| 139 | // ContinueStmt |
| 140 | Stmt::child_iterator ContinueStmt::child_begin() { return NULL; } |
| 141 | Stmt::child_iterator ContinueStmt::child_end() { return NULL; } |
| 142 | |
| 143 | // BreakStmt |
| 144 | Stmt::child_iterator BreakStmt::child_begin() { return NULL; } |
| 145 | Stmt::child_iterator BreakStmt::child_end() { return NULL; } |
| 146 | |
| 147 | // ReturnStmt |
| 148 | Stmt::child_iterator ReturnStmt::child_begin() { |
| 149 | return reinterpret_cast<Stmt**>(&RetExpr); |
| 150 | } |
| 151 | |
| 152 | Stmt::child_iterator ReturnStmt::child_end() { return child_begin()+1; } |
| 153 | |