blob: fd72481fee842beee5b557f0f1293a74cc399af4 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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
Chris Lattner006d1542008-01-30 05:01:46 +0000114//===----------------------------------------------------------------------===//
115// Constructors
116//===----------------------------------------------------------------------===//
117
118AsmStmt::AsmStmt(SourceLocation asmloc, bool isvolatile,
119 unsigned numoutputs, unsigned numinputs,
120 std::string *names, StringLiteral **constraints,
121 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
122 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlsson965d5202007-11-22 01:36:19 +0000123 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Chris Lattner006d1542008-01-30 05:01:46 +0000124 , IsVolatile(isvolatile), NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson965d5202007-11-22 01:36:19 +0000125 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
126 Names.push_back(names[i]);
127 Exprs.push_back(exprs[i]);
128 Constraints.push_back(constraints[i]);
129 }
130
131 for (unsigned i = 0; i != numclobbers; i++)
132 Clobbers.push_back(clobbers[i]);
133}
134
Chris Lattner006d1542008-01-30 05:01:46 +0000135ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
136 Stmt *Body, SourceLocation FCL,
137 SourceLocation RPL)
138: Stmt(ObjCForCollectionStmtClass) {
139 SubExprs[ELEM] = Elem;
140 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
141 SubExprs[BODY] = Body;
142 ForLoc = FCL;
143 RParenLoc = RPL;
144}
145
146
147ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
148 SourceLocation rparenloc,
149 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
150 Stmt *atCatchList)
151: Stmt(ObjCAtCatchStmtClass) {
152 SubExprs[SELECTOR] = catchVarStmtDecl;
153 SubExprs[BODY] = atCatchStmt;
154 if (!atCatchList)
155 NextAtCatchStmt = NULL;
156 else {
157 ObjCAtCatchStmt *AtCatchList =
158 static_cast<ObjCAtCatchStmt*>(atCatchList);
159 while (AtCatchList->NextAtCatchStmt)
160 AtCatchList = AtCatchList->NextAtCatchStmt;
161 AtCatchList->NextAtCatchStmt = this;
162 }
163 AtCatchLoc = atCatchLoc;
164 RParenLoc = rparenloc;
165}
166
167
Ted Kremenek51be0562007-08-24 21:09:09 +0000168//===----------------------------------------------------------------------===//
169// Child Iterators for iterating over subexpressions/substatements
170//===----------------------------------------------------------------------===//
171
172// DeclStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000173Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); }
174Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000175
176// NullStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000177Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
178Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000179
180// CompoundStmt
181Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
182Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
183
Ted Kremeneke07b67e2007-08-30 16:50:46 +0000184// CaseStmt
185Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
186Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
187
188// DefaultStmt
189Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
190Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000191
192// LabelStmt
193Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000194Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000195
196// IfStmt
197Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
198Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
199
200// SwitchStmt
201Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
202Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
203
204// WhileStmt
205Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
206Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
207
208// DoStmt
209Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
210Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
211
212// ForStmt
213Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
214Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
215
Ted Kremenek42730c52008-01-07 19:49:32 +0000216// ObjCForCollectionStmt
217Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000218 return &SubExprs[0];
219}
Ted Kremenek42730c52008-01-07 19:49:32 +0000220Stmt::child_iterator ObjCForCollectionStmt::child_end() {
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000221 return &SubExprs[0]+END_EXPR;
222}
223
Ted Kremenek51be0562007-08-24 21:09:09 +0000224// GotoStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000225Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
226Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000227
228// IndirectGotoStmt
229Stmt::child_iterator IndirectGotoStmt::child_begin() {
230 return reinterpret_cast<Stmt**>(&Target);
231}
232
Ted Kremenek4f398252007-10-18 00:24:38 +0000233Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000234
235// ContinueStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000236Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
237Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000238
239// BreakStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000240Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
241Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000242
243// ReturnStmt
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000244Stmt::child_iterator ReturnStmt::child_begin() {
245 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr);
Ted Kremeneka6478552007-10-18 23:28:49 +0000246 else return child_iterator();
Ted Kremenek51be0562007-08-24 21:09:09 +0000247}
248
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000249Stmt::child_iterator ReturnStmt::child_end() {
250 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr)+1;
Ted Kremeneka6478552007-10-18 23:28:49 +0000251 else return child_iterator();
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000252}
Ted Kremenek51be0562007-08-24 21:09:09 +0000253
Chris Lattner8a40a832007-10-29 04:04:16 +0000254// AsmStmt
255Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
256Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
257
Ted Kremenek42730c52008-01-07 19:49:32 +0000258// ObjCAtCatchStmt
259Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
260Stmt::child_iterator ObjCAtCatchStmt::child_end() {
Fariborz Jahanian06798362007-11-01 23:59:59 +0000261 return &SubExprs[0]+END_EXPR;
262}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000263
Ted Kremenek42730c52008-01-07 19:49:32 +0000264// ObjCAtFinallyStmt
265Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
266Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000267
Ted Kremenek42730c52008-01-07 19:49:32 +0000268// ObjCAtTryStmt
269Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
270Stmt::child_iterator ObjCAtTryStmt::child_end() {
Fariborz Jahaniance14dd12007-11-07 17:43:16 +0000271 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000272}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000273
Ted Kremenek42730c52008-01-07 19:49:32 +0000274// ObjCAtThrowStmt
275Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000276 return &Throw;
277}
278
Ted Kremenek42730c52008-01-07 19:49:32 +0000279Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000280 return &Throw+1;
281}
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000282
283// ObjCAtSynchronizedStmt
284Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000285 return &SubStmts[0];
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000286}
287
288Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000289 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000290}
291