blob: 9b405f1e23e6bd5694d509768f0301cc3f03f380 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- 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"
18using namespace clang;
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000021 const char *Name;
22 unsigned Counter;
23 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000024} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000025
26static 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 Spencer5f016e22007-07-11 17:01:13 +000036#include "clang/AST/StmtNodes.def"
Reid Spencer5f016e22007-07-11 17:01:13 +000037
Chris Lattner63381352007-08-25 01:42:24 +000038 return StmtClassInfo[E];
39}
40
Reid Spencer5f016e22007-07-11 17:01:13 +000041const char *Stmt::getStmtClassName() const {
Chris Lattner63381352007-08-25 01:42:24 +000042 return getStmtInfoTableEntry(sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000043}
44
45void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000046 // Ensure the table is primed.
47 getStmtInfoTableEntry(Stmt::NullStmtClass);
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049 unsigned sum = 0;
50 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000051 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000052 if (StmtClassInfo[i].Name == 0) continue;
53 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000054 }
55 fprintf(stderr, " %d stmts/exprs total.\n", sum);
56 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000057 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000058 if (StmtClassInfo[i].Name == 0) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000060 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 Spencer5f016e22007-07-11 17:01:13 +000064 }
65 fprintf(stderr, "Total bytes = %d\n", sum);
66}
67
68void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000069 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000070}
71
72static bool StatSwitch = false;
73
74bool Stmt::CollectingStats(bool enable) {
75 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000076 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000077}
78
79
Reid Spencer5f016e22007-07-11 17:01:13 +000080const char *LabelStmt::getName() const {
81 return getID()->getName();
82}
83
Steve Naroff507f2d52007-08-31 23:49:30 +000084// This is defined here to avoid polluting Stmt.h with importing Expr.h
85SourceRange ReturnStmt::getSourceRange() const {
86 if (RetExpr)
87 return SourceRange(RetLoc, RetExpr->getLocEnd());
88 else
89 return SourceRange(RetLoc);
90}
91
Ted Kremenek82977772007-08-24 21:09:09 +000092//===----------------------------------------------------------------------===//
93// Child Iterators for iterating over subexpressions/substatements
94//===----------------------------------------------------------------------===//
95
96// DeclStmt
97Stmt::child_iterator DeclStmt::child_begin() { return NULL; }
98Stmt::child_iterator DeclStmt::child_end() { return NULL; }
99
100// NullStmt
101Stmt::child_iterator NullStmt::child_begin() { return NULL; }
102Stmt::child_iterator NullStmt::child_end() { return NULL; }
103
104// CompoundStmt
105Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
106Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
107
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000108// CaseStmt
109Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
110Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
111
112// DefaultStmt
113Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
114Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000115
116// LabelStmt
117Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000118Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000119
120// IfStmt
121Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
122Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
123
124// SwitchStmt
125Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
126Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
127
128// WhileStmt
129Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
130Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
131
132// DoStmt
133Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
134Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
135
136// ForStmt
137Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
138Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
139
140// GotoStmt
141Stmt::child_iterator GotoStmt::child_begin() { return NULL; }
142Stmt::child_iterator GotoStmt::child_end() { return NULL; }
143
144// IndirectGotoStmt
145Stmt::child_iterator IndirectGotoStmt::child_begin() {
146 return reinterpret_cast<Stmt**>(&Target);
147}
148
149Stmt::child_iterator IndirectGotoStmt::child_end() { return child_begin()+1; }
150
151// ContinueStmt
152Stmt::child_iterator ContinueStmt::child_begin() { return NULL; }
153Stmt::child_iterator ContinueStmt::child_end() { return NULL; }
154
155// BreakStmt
156Stmt::child_iterator BreakStmt::child_begin() { return NULL; }
157Stmt::child_iterator BreakStmt::child_end() { return NULL; }
158
159// ReturnStmt
Ted Kremenek2298f912007-08-27 20:58:16 +0000160Stmt::child_iterator ReturnStmt::child_begin() {
161 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr);
162 else return NULL;
Ted Kremenek82977772007-08-24 21:09:09 +0000163}
164
Ted Kremenek2298f912007-08-27 20:58:16 +0000165Stmt::child_iterator ReturnStmt::child_end() {
166 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr)+1;
167 else return NULL;
168}
Ted Kremenek82977772007-08-24 21:09:09 +0000169