blob: 9a4d4347b608212afdf85a3175be607fcf7ed20e [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018using namespace clang;
19
Chris Lattner4b009652007-07-25 00:24:17 +000020static struct StmtClassNameTable {
Chris Lattner603bf122007-08-25 01:42:24 +000021 const char *Name;
22 unsigned Counter;
23 unsigned Size;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000024} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner603bf122007-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);
Chris Lattner4b009652007-07-25 00:24:17 +000036#include "clang/AST/StmtNodes.def"
Chris Lattner4b009652007-07-25 00:24:17 +000037
Chris Lattner603bf122007-08-25 01:42:24 +000038 return StmtClassInfo[E];
39}
40
Chris Lattner4b009652007-07-25 00:24:17 +000041const char *Stmt::getStmtClassName() const {
Chris Lattner603bf122007-08-25 01:42:24 +000042 return getStmtInfoTableEntry(sClass).Name;
Chris Lattner4b009652007-07-25 00:24:17 +000043}
44
45void Stmt::PrintStats() {
Chris Lattner603bf122007-08-25 01:42:24 +000046 // Ensure the table is primed.
47 getStmtInfoTableEntry(Stmt::NullStmtClass);
48
Chris Lattner4b009652007-07-25 00:24:17 +000049 unsigned sum = 0;
50 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner9c0da3b2007-08-25 01:55:00 +000051 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000052 if (StmtClassInfo[i].Name == 0) continue;
53 sum += StmtClassInfo[i].Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000054 }
55 fprintf(stderr, " %d stmts/exprs total.\n", sum);
56 sum = 0;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000057 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000058 if (StmtClassInfo[i].Name == 0) continue;
Chris Lattner4b009652007-07-25 00:24:17 +000059 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner603bf122007-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;
Chris Lattner4b009652007-07-25 00:24:17 +000064 }
65 fprintf(stderr, "Total bytes = %d\n", sum);
66}
67
68void Stmt::addStmtClass(StmtClass s) {
Chris Lattner603bf122007-08-25 01:42:24 +000069 ++getStmtInfoTableEntry(s).Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000070}
71
72static bool StatSwitch = false;
73
74bool Stmt::CollectingStats(bool enable) {
75 if (enable) StatSwitch = true;
Chris Lattner603bf122007-08-25 01:42:24 +000076 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000077}
78
79
Chris Lattner4b009652007-07-25 00:24:17 +000080const char *LabelStmt::getName() const {
81 return getID()->getName();
82}
83
Steve Naroffc32a20d2007-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 Kremenekad5682f2007-10-01 16:34:52 +000092bool 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 Carlsson965d5202007-11-22 01:36:19 +0000114AsmStmt::AsmStmt(SourceLocation asmloc,
115 unsigned numoutputs,
116 unsigned numinputs,
117 std::string *names,
118 StringLiteral **constraints,
119 Expr **exprs,
120 StringLiteral *asmstr,
121 unsigned numclobbers,
122 StringLiteral **clobbers,
123 SourceLocation rparenloc)
124 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
125 , NumOutputs(numoutputs), NumInputs(numinputs)
126{
127 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
128 Names.push_back(names[i]);
129 Exprs.push_back(exprs[i]);
130 Constraints.push_back(constraints[i]);
131 }
132
133 for (unsigned i = 0; i != numclobbers; i++)
134 Clobbers.push_back(clobbers[i]);
135}
136
Ted Kremenek51be0562007-08-24 21:09:09 +0000137//===----------------------------------------------------------------------===//
138// Child Iterators for iterating over subexpressions/substatements
139//===----------------------------------------------------------------------===//
140
141// DeclStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000142Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); }
143Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000144
145// NullStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000146Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
147Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000148
149// CompoundStmt
150Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
151Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
152
Ted Kremeneke07b67e2007-08-30 16:50:46 +0000153// CaseStmt
154Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
155Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
156
157// DefaultStmt
158Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
159Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000160
161// LabelStmt
162Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000163Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000164
165// IfStmt
166Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
167Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
168
169// SwitchStmt
170Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
171Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
172
173// WhileStmt
174Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
175Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
176
177// DoStmt
178Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
179Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
180
181// ForStmt
182Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
183Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
184
185// GotoStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000186Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
187Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000188
189// IndirectGotoStmt
190Stmt::child_iterator IndirectGotoStmt::child_begin() {
191 return reinterpret_cast<Stmt**>(&Target);
192}
193
Ted Kremenek4f398252007-10-18 00:24:38 +0000194Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000195
196// ContinueStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000197Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
198Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000199
200// BreakStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000201Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
202Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000203
204// ReturnStmt
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000205Stmt::child_iterator ReturnStmt::child_begin() {
206 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr);
Ted Kremeneka6478552007-10-18 23:28:49 +0000207 else return child_iterator();
Ted Kremenek51be0562007-08-24 21:09:09 +0000208}
209
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000210Stmt::child_iterator ReturnStmt::child_end() {
211 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr)+1;
Ted Kremeneka6478552007-10-18 23:28:49 +0000212 else return child_iterator();
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000213}
Ted Kremenek51be0562007-08-24 21:09:09 +0000214
Chris Lattner8a40a832007-10-29 04:04:16 +0000215// AsmStmt
216Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
217Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
218
Fariborz Jahanian70952482007-11-01 21:12:44 +0000219// ObjcAtCatchStmt
Fariborz Jahanian06798362007-11-01 23:59:59 +0000220Stmt::child_iterator ObjcAtCatchStmt::child_begin() { return &SubExprs[0]; }
221Stmt::child_iterator ObjcAtCatchStmt::child_end() {
222 return &SubExprs[0]+END_EXPR;
223}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000224
225// ObjcAtFinallyStmt
226Stmt::child_iterator ObjcAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
227Stmt::child_iterator ObjcAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
228
229// ObjcAtTryStmt
230Stmt::child_iterator ObjcAtTryStmt::child_begin() { return &SubStmts[0]; }
Fariborz Jahanian06798362007-11-01 23:59:59 +0000231Stmt::child_iterator ObjcAtTryStmt::child_end() {
Fariborz Jahaniance14dd12007-11-07 17:43:16 +0000232 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000233}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000234
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000235// ObjcAtThrowStmt
236Stmt::child_iterator ObjcAtThrowStmt::child_begin() {
237 return &Throw;
238}
239
240Stmt::child_iterator ObjcAtThrowStmt::child_end() {
241 return &Throw+1;
242}