blob: 6d8ebcf8649559a45f64fe341b6187f9e182b245 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using 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
Ted Kremenek27f8a282008-05-20 00:43:19 +000045void Stmt::DestroyChildren(ASTContext& C) {
Ted Kremenek9c1863e2008-05-19 22:02:12 +000046 for (child_iterator I = child_begin(), E = child_end(); I !=E; ++I)
Ted Kremenek27f8a282008-05-20 00:43:19 +000047 if (Stmt* Child = *I) Child->Destroy(C);
48}
49
50void Stmt::Destroy(ASTContext& C) {
51 DestroyChildren(C);
Ted Kremenekf809e3b2008-05-20 04:10:52 +000052 // FIXME: Eventually all Stmts should be allocated with the allocator
53 // in ASTContext, just like with Decls.
54 // this->~Stmt();
55 delete this;
Ted Kremenek9c1863e2008-05-19 22:02:12 +000056}
57
Ted Kremenek8e355f22008-05-21 15:53:55 +000058void DeclStmt::Destroy(ASTContext& C) {
59 TheDecl->Destroy(C);
Ted Kremenek936ff132008-05-21 16:00:02 +000060 delete this;
Ted Kremenek8e355f22008-05-21 15:53:55 +000061}
62
Reid Spencer5f016e22007-07-11 17:01:13 +000063void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000064 // Ensure the table is primed.
65 getStmtInfoTableEntry(Stmt::NullStmtClass);
66
Reid Spencer5f016e22007-07-11 17:01:13 +000067 unsigned sum = 0;
68 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000069 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000070 if (StmtClassInfo[i].Name == 0) continue;
71 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
73 fprintf(stderr, " %d stmts/exprs total.\n", sum);
74 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000075 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000076 if (StmtClassInfo[i].Name == 0) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +000077 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000078 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
79 StmtClassInfo[i].Size,
80 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
81 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000082 }
83 fprintf(stderr, "Total bytes = %d\n", sum);
84}
85
86void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000087 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000088}
89
90static bool StatSwitch = false;
91
92bool Stmt::CollectingStats(bool enable) {
93 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000094 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000095}
96
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098const char *LabelStmt::getName() const {
99 return getID()->getName();
100}
101
Steve Naroff507f2d52007-08-31 23:49:30 +0000102// This is defined here to avoid polluting Stmt.h with importing Expr.h
103SourceRange ReturnStmt::getSourceRange() const {
104 if (RetExpr)
105 return SourceRange(RetLoc, RetExpr->getLocEnd());
106 else
107 return SourceRange(RetLoc);
108}
109
Ted Kremenekd48ade62007-10-01 16:34:52 +0000110bool Stmt::hasImplicitControlFlow() const {
111 switch (sClass) {
112 default:
113 return false;
114
115 case CallExprClass:
116 case ConditionalOperatorClass:
117 case ChooseExprClass:
118 case StmtExprClass:
119 case DeclStmtClass:
120 return true;
121
122 case Stmt::BinaryOperatorClass: {
123 const BinaryOperator* B = cast<BinaryOperator>(this);
124 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
125 return true;
126 else
127 return false;
128 }
129 }
130}
131
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000132//===----------------------------------------------------------------------===//
133// Constructors
134//===----------------------------------------------------------------------===//
135
Anders Carlssondfab34a2008-02-05 23:03:50 +0000136AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000137 unsigned numoutputs, unsigned numinputs,
138 std::string *names, StringLiteral **constraints,
139 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
140 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000141 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000142 , IsSimple(issimple), IsVolatile(isvolatile)
143 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000144 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
145 Names.push_back(names[i]);
146 Exprs.push_back(exprs[i]);
147 Constraints.push_back(constraints[i]);
148 }
149
150 for (unsigned i = 0; i != numclobbers; i++)
151 Clobbers.push_back(clobbers[i]);
152}
153
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000154ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
155 Stmt *Body, SourceLocation FCL,
156 SourceLocation RPL)
157: Stmt(ObjCForCollectionStmtClass) {
158 SubExprs[ELEM] = Elem;
159 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
160 SubExprs[BODY] = Body;
161 ForLoc = FCL;
162 RParenLoc = RPL;
163}
164
165
166ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
167 SourceLocation rparenloc,
168 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
169 Stmt *atCatchList)
170: Stmt(ObjCAtCatchStmtClass) {
171 SubExprs[SELECTOR] = catchVarStmtDecl;
172 SubExprs[BODY] = atCatchStmt;
173 if (!atCatchList)
Ted Kremenekff981022008-02-01 21:28:59 +0000174 SubExprs[NEXT_CATCH] = NULL;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000175 else {
Ted Kremenekff981022008-02-01 21:28:59 +0000176 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
177
178 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
179 AtCatchList = NextCatch;
180
181 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000182 }
183 AtCatchLoc = atCatchLoc;
184 RParenLoc = rparenloc;
185}
186
187
Ted Kremenek82977772007-08-24 21:09:09 +0000188//===----------------------------------------------------------------------===//
189// Child Iterators for iterating over subexpressions/substatements
190//===----------------------------------------------------------------------===//
191
192// DeclStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000193Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); }
194Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000195
196// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000197Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
198Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000199
200// CompoundStmt
201Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
202Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
203
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000204// CaseStmt
205Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
206Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
207
208// DefaultStmt
209Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
210Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000211
212// LabelStmt
213Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000214Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000215
216// IfStmt
217Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
218Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
219
220// SwitchStmt
221Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
222Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
223
224// WhileStmt
225Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
226Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
227
228// DoStmt
229Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
230Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
231
232// ForStmt
233Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
234Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
235
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000236// ObjCForCollectionStmt
237Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000238 return &SubExprs[0];
239}
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000240Stmt::child_iterator ObjCForCollectionStmt::child_end() {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000241 return &SubExprs[0]+END_EXPR;
242}
243
Ted Kremenek82977772007-08-24 21:09:09 +0000244// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000245Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
246Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000247
248// IndirectGotoStmt
249Stmt::child_iterator IndirectGotoStmt::child_begin() {
250 return reinterpret_cast<Stmt**>(&Target);
251}
252
Ted Kremenek9caf8b12007-10-18 00:24:38 +0000253Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000254
255// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000256Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
257Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000258
259// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000260Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
261Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000262
263// ReturnStmt
Ted Kremenek2298f912007-08-27 20:58:16 +0000264Stmt::child_iterator ReturnStmt::child_begin() {
265 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr);
Ted Kremenek9ac59282007-10-18 23:28:49 +0000266 else return child_iterator();
Ted Kremenek82977772007-08-24 21:09:09 +0000267}
268
Ted Kremenek2298f912007-08-27 20:58:16 +0000269Stmt::child_iterator ReturnStmt::child_end() {
270 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr)+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +0000271 else return child_iterator();
Ted Kremenek2298f912007-08-27 20:58:16 +0000272}
Ted Kremenek82977772007-08-24 21:09:09 +0000273
Chris Lattnerfe795952007-10-29 04:04:16 +0000274// AsmStmt
275Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
276Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
277
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000278// ObjCAtCatchStmt
279Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
280Stmt::child_iterator ObjCAtCatchStmt::child_end() {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000281 return &SubExprs[0]+END_EXPR;
282}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000283
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000284// ObjCAtFinallyStmt
285Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
286Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000287
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000288// ObjCAtTryStmt
289Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
290Stmt::child_iterator ObjCAtTryStmt::child_end() {
Fariborz Jahanian89079ea2007-11-07 17:43:16 +0000291 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000292}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000293
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000294// ObjCAtThrowStmt
295Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000296 return &Throw;
297}
298
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000299Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000300 return &Throw+1;
301}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000302
303// ObjCAtSynchronizedStmt
304Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000305 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000306}
307
308Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000309 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000310}
311