blob: d3f3ff0e111bf4db405c2ebd1575c3c9f157d85a [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"
Steve Narofff494b572008-05-29 21:12:08 +000016#include "clang/AST/ExprObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000018#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Reid Spencer5f016e22007-07-11 17:01:13 +000021static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000022 const char *Name;
23 unsigned Counter;
24 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000025} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000026
27static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
28 static bool Initialized = false;
29 if (Initialized)
30 return StmtClassInfo[E];
31
32 // Intialize the table on the first use.
33 Initialized = true;
34#define STMT(N, CLASS, PARENT) \
35 StmtClassInfo[N].Name = #CLASS; \
36 StmtClassInfo[N].Size = sizeof(CLASS);
Reid Spencer5f016e22007-07-11 17:01:13 +000037#include "clang/AST/StmtNodes.def"
Reid Spencer5f016e22007-07-11 17:01:13 +000038
Chris Lattner63381352007-08-25 01:42:24 +000039 return StmtClassInfo[E];
40}
41
Reid Spencer5f016e22007-07-11 17:01:13 +000042const char *Stmt::getStmtClassName() const {
Chris Lattner63381352007-08-25 01:42:24 +000043 return getStmtInfoTableEntry(sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000044}
45
Ted Kremenek27f8a282008-05-20 00:43:19 +000046void Stmt::DestroyChildren(ASTContext& C) {
Ted Kremenek9c1863e2008-05-19 22:02:12 +000047 for (child_iterator I = child_begin(), E = child_end(); I !=E; ++I)
Ted Kremenek27f8a282008-05-20 00:43:19 +000048 if (Stmt* Child = *I) Child->Destroy(C);
49}
50
51void Stmt::Destroy(ASTContext& C) {
52 DestroyChildren(C);
Ted Kremenekf809e3b2008-05-20 04:10:52 +000053 // FIXME: Eventually all Stmts should be allocated with the allocator
54 // in ASTContext, just like with Decls.
55 // this->~Stmt();
56 delete this;
Ted Kremenek9c1863e2008-05-19 22:02:12 +000057}
58
Ted Kremenek8e355f22008-05-21 15:53:55 +000059void DeclStmt::Destroy(ASTContext& C) {
60 TheDecl->Destroy(C);
Ted Kremenek936ff132008-05-21 16:00:02 +000061 delete this;
Ted Kremenek8e355f22008-05-21 15:53:55 +000062}
63
Reid Spencer5f016e22007-07-11 17:01:13 +000064void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000065 // Ensure the table is primed.
66 getStmtInfoTableEntry(Stmt::NullStmtClass);
67
Reid Spencer5f016e22007-07-11 17:01:13 +000068 unsigned sum = 0;
69 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000070 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000071 if (StmtClassInfo[i].Name == 0) continue;
72 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000073 }
74 fprintf(stderr, " %d stmts/exprs total.\n", sum);
75 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000076 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000077 if (StmtClassInfo[i].Name == 0) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +000078 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000079 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
80 StmtClassInfo[i].Size,
81 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
82 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000083 }
84 fprintf(stderr, "Total bytes = %d\n", sum);
85}
86
87void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000088 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000089}
90
91static bool StatSwitch = false;
92
93bool Stmt::CollectingStats(bool enable) {
94 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000095 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000096}
97
98
Reid Spencer5f016e22007-07-11 17:01:13 +000099const char *LabelStmt::getName() const {
100 return getID()->getName();
101}
102
Steve Naroff507f2d52007-08-31 23:49:30 +0000103// This is defined here to avoid polluting Stmt.h with importing Expr.h
104SourceRange ReturnStmt::getSourceRange() const {
105 if (RetExpr)
106 return SourceRange(RetLoc, RetExpr->getLocEnd());
107 else
108 return SourceRange(RetLoc);
109}
110
Ted Kremenekd48ade62007-10-01 16:34:52 +0000111bool Stmt::hasImplicitControlFlow() const {
112 switch (sClass) {
113 default:
114 return false;
115
116 case CallExprClass:
117 case ConditionalOperatorClass:
118 case ChooseExprClass:
119 case StmtExprClass:
120 case DeclStmtClass:
121 return true;
122
123 case Stmt::BinaryOperatorClass: {
124 const BinaryOperator* B = cast<BinaryOperator>(this);
125 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
126 return true;
127 else
128 return false;
129 }
130 }
131}
132
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000133//===----------------------------------------------------------------------===//
134// Constructors
135//===----------------------------------------------------------------------===//
136
Anders Carlssondfab34a2008-02-05 23:03:50 +0000137AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000138 unsigned numoutputs, unsigned numinputs,
139 std::string *names, StringLiteral **constraints,
140 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
141 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000142 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000143 , IsSimple(issimple), IsVolatile(isvolatile)
144 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000145 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
146 Names.push_back(names[i]);
147 Exprs.push_back(exprs[i]);
148 Constraints.push_back(constraints[i]);
149 }
150
151 for (unsigned i = 0; i != numclobbers; i++)
152 Clobbers.push_back(clobbers[i]);
153}
154
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000155ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
156 Stmt *Body, SourceLocation FCL,
157 SourceLocation RPL)
158: Stmt(ObjCForCollectionStmtClass) {
159 SubExprs[ELEM] = Elem;
160 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
161 SubExprs[BODY] = Body;
162 ForLoc = FCL;
163 RParenLoc = RPL;
164}
165
166
167ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
168 SourceLocation rparenloc,
169 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
170 Stmt *atCatchList)
171: Stmt(ObjCAtCatchStmtClass) {
172 SubExprs[SELECTOR] = catchVarStmtDecl;
173 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000174 SubExprs[NEXT_CATCH] = NULL;
175 if (atCatchList) {
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
Ted Kremenek1060aff2008-06-17 03:11:08 +0000249Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
250const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000251
Ted Kremenek1060aff2008-06-17 03:11:08 +0000252Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
253Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
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 Kremenek1060aff2008-06-17 03:11:08 +0000264const Expr* ReturnStmt::getRetValue() const {
265 return cast_or_null<Expr>(RetExpr);
266}
267Expr* ReturnStmt::getRetValue() {
268 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000269}
270
Ted Kremenek1060aff2008-06-17 03:11:08 +0000271Stmt::child_iterator ReturnStmt::child_begin() {
272 return &RetExpr;
273}
274Stmt::child_iterator ReturnStmt::child_end() {
275 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000276}
Ted Kremenek82977772007-08-24 21:09:09 +0000277
Chris Lattnerfe795952007-10-29 04:04:16 +0000278// AsmStmt
279Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
280Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
281
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000282// ObjCAtCatchStmt
283Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
284Stmt::child_iterator ObjCAtCatchStmt::child_end() {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000285 return &SubExprs[0]+END_EXPR;
286}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000287
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000288// ObjCAtFinallyStmt
289Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
290Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000291
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000292// ObjCAtTryStmt
293Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
294Stmt::child_iterator ObjCAtTryStmt::child_end() {
Fariborz Jahanian89079ea2007-11-07 17:43:16 +0000295 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000296}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000297
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000298// ObjCAtThrowStmt
299Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000300 return &Throw;
301}
302
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000303Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000304 return &Throw+1;
305}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000306
307// ObjCAtSynchronizedStmt
308Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000309 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000310}
311
312Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000313 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000314}
315