blob: 8f713c0e2efb397dc8001f98708f4e01ea79acec [file] [log] [blame]
Chris Lattnerf42cce72006-10-25 04:09:21 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerf42cce72006-10-25 04:09:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Stmt.h"
Chris Lattner29375652006-12-04 18:06:35 +000015#include "clang/AST/ExprCXX.h"
Steve Naroff021ca182008-05-29 21:12:08 +000016#include "clang/AST/ExprObjC.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000018#include "clang/Basic/IdentifierTable.h"
Chris Lattnerf42cce72006-10-25 04:09:21 +000019using namespace clang;
20
Steve Narofff84d11f2007-05-23 21:48:04 +000021static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000022 const char *Name;
23 unsigned Counter;
24 unsigned Size;
Chris Lattnerd8c9fc52007-08-25 01:55:00 +000025} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner4d15a0d2007-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);
Steve Narofff1e53692007-03-23 22:27:02 +000037#include "clang/AST/StmtNodes.def"
Nico Weberde565e32008-08-05 23:15:29 +000038
Chris Lattner4d15a0d2007-08-25 01:42:24 +000039 return StmtClassInfo[E];
40}
41
Steve Narofff1e53692007-03-23 22:27:02 +000042const char *Stmt::getStmtClassName() const {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000043 return getStmtInfoTableEntry(sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000044}
Steve Narofff84d11f2007-05-23 21:48:04 +000045
Ted Kremenekce20e8f2008-05-20 00:43:19 +000046void Stmt::DestroyChildren(ASTContext& C) {
Ted Kremenekee579422008-05-19 22:02:12 +000047 for (child_iterator I = child_begin(), E = child_end(); I !=E; ++I)
Ted Kremenekce20e8f2008-05-20 00:43:19 +000048 if (Stmt* Child = *I) Child->Destroy(C);
49}
50
51void Stmt::Destroy(ASTContext& C) {
52 DestroyChildren(C);
Ted Kremenek40e489d2008-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 Kremenekee579422008-05-19 22:02:12 +000057}
58
Ted Kremenek512d9412008-05-21 15:53:55 +000059void DeclStmt::Destroy(ASTContext& C) {
60 TheDecl->Destroy(C);
Ted Kremenekf025a8b2008-05-21 16:00:02 +000061 delete this;
Ted Kremenek512d9412008-05-21 15:53:55 +000062}
63
Steve Narofff84d11f2007-05-23 21:48:04 +000064void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000065 // Ensure the table is primed.
66 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000067
Steve Narofff84d11f2007-05-23 21:48:04 +000068 unsigned sum = 0;
69 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattnerd8c9fc52007-08-25 01:55:00 +000070 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000071 if (StmtClassInfo[i].Name == 0) continue;
72 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000073 }
74 fprintf(stderr, " %d stmts/exprs total.\n", sum);
75 sum = 0;
Chris Lattnerd8c9fc52007-08-25 01:55:00 +000076 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000077 if (StmtClassInfo[i].Name == 0) continue;
Nico Weberde565e32008-08-05 23:15:29 +000078 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner4d15a0d2007-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;
Steve Narofff84d11f2007-05-23 21:48:04 +000083 }
84 fprintf(stderr, "Total bytes = %d\n", sum);
85}
86
87void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000088 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000089}
90
91static bool StatSwitch = false;
92
93bool Stmt::CollectingStats(bool enable) {
94 if (enable) StatSwitch = true;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000095 return StatSwitch;
Steve Narofff84d11f2007-05-23 21:48:04 +000096}
97
98
Chris Lattnereefa10e2007-05-28 06:56:27 +000099const char *LabelStmt::getName() const {
100 return getID()->getName();
101}
102
Steve Naroffdc9f36e2007-08-31 23:49:30 +0000103// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weberde565e32008-08-05 23:15:29 +0000104SourceRange ReturnStmt::getSourceRange() const {
Steve Naroffdc9f36e2007-08-31 23:49:30 +0000105 if (RetExpr)
106 return SourceRange(RetLoc, RetExpr->getLocEnd());
107 else
108 return SourceRange(RetLoc);
109}
110
Ted Kremenek7f74e132007-10-01 16:34:52 +0000111bool Stmt::hasImplicitControlFlow() const {
112 switch (sClass) {
113 default:
114 return false;
Nico Weberde565e32008-08-05 23:15:29 +0000115
Ted Kremenek7f74e132007-10-01 16:34:52 +0000116 case CallExprClass:
117 case ConditionalOperatorClass:
118 case ChooseExprClass:
119 case StmtExprClass:
120 case DeclStmtClass:
Nico Weberde565e32008-08-05 23:15:29 +0000121 return true;
122
Ted Kremenek7f74e132007-10-01 16:34:52 +0000123 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 Lattner86f5e132008-01-30 05:01:46 +0000133//===----------------------------------------------------------------------===//
134// Constructors
135//===----------------------------------------------------------------------===//
136
Anders Carlsson19fe1162008-02-05 23:03:50 +0000137AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattner86f5e132008-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 Carlsson94ea8aa2007-11-22 01:36:19 +0000142 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlsson19fe1162008-02-05 23:03:50 +0000143 , IsSimple(issimple), IsVolatile(isvolatile)
144 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson94ea8aa2007-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]);
Nico Weberde565e32008-08-05 23:15:29 +0000148 Constraints.push_back(constraints[i]);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000149 }
Nico Weberde565e32008-08-05 23:15:29 +0000150
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000151 for (unsigned i = 0; i != numclobbers; i++)
152 Clobbers.push_back(clobbers[i]);
153}
154
Chris Lattner86f5e132008-01-30 05:01:46 +0000155ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
156 Stmt *Body, SourceLocation FCL,
Nico Weberde565e32008-08-05 23:15:29 +0000157 SourceLocation RPL)
Chris Lattner86f5e132008-01-30 05:01:46 +0000158: 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
Nico Weberde565e32008-08-05 23:15:29 +0000167ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
168 SourceLocation rparenloc,
169 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
Chris Lattner86f5e132008-01-30 05:01:46 +0000170 Stmt *atCatchList)
171: Stmt(ObjCAtCatchStmtClass) {
172 SubExprs[SELECTOR] = catchVarStmtDecl;
173 SubExprs[BODY] = atCatchStmt;
Eli Friedman1f97e572008-05-25 04:34:57 +0000174 SubExprs[NEXT_CATCH] = NULL;
175 if (atCatchList) {
Ted Kremeneka4965842008-02-01 21:28:59 +0000176 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
177
Nico Weberde565e32008-08-05 23:15:29 +0000178 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremeneka4965842008-02-01 21:28:59 +0000179 AtCatchList = NextCatch;
Nico Weberde565e32008-08-05 23:15:29 +0000180
Ted Kremeneka4965842008-02-01 21:28:59 +0000181 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner86f5e132008-01-30 05:01:46 +0000182 }
183 AtCatchLoc = atCatchLoc;
184 RParenLoc = rparenloc;
185}
186
187
Ted Kremenek066dd932007-08-24 21:09:09 +0000188//===----------------------------------------------------------------------===//
189// Child Iterators for iterating over subexpressions/substatements
190//===----------------------------------------------------------------------===//
191
192// DeclStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000193Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); }
194Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000195
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000196DeclStmt::decl_iterator& DeclStmt::decl_iterator::operator++() {
197 D = D->getNextDeclarator();
198 return *this;
199}
200
Ted Kremenek066dd932007-08-24 21:09:09 +0000201// NullStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000202Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
203Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000204
205// CompoundStmt
206Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
207Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
208
Ted Kremenek14f0d1a2007-08-30 16:50:46 +0000209// CaseStmt
210Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
211Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
212
213// DefaultStmt
214Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
215Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000216
217// LabelStmt
218Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000219Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000220
221// IfStmt
222Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
223Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
224
225// SwitchStmt
226Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
227Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
228
229// WhileStmt
230Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
231Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
232
233// DoStmt
234Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
235Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
236
237// ForStmt
238Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
239Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
240
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000241// ObjCForCollectionStmt
Nico Weberde565e32008-08-05 23:15:29 +0000242Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
243 return &SubExprs[0];
Fariborz Jahanian83615522008-01-02 22:54:34 +0000244}
Nico Weberde565e32008-08-05 23:15:29 +0000245Stmt::child_iterator ObjCForCollectionStmt::child_end() {
246 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian83615522008-01-02 22:54:34 +0000247}
248
Ted Kremenek066dd932007-08-24 21:09:09 +0000249// GotoStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000250Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
251Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000252
253// IndirectGotoStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000254Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
255const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000256
Ted Kremenekc6501db2008-06-17 03:11:08 +0000257Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
258Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000259
260// ContinueStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000261Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
262Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000263
264// BreakStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000265Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
266Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000267
268// ReturnStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000269const Expr* ReturnStmt::getRetValue() const {
270 return cast_or_null<Expr>(RetExpr);
271}
272Expr* ReturnStmt::getRetValue() {
273 return cast_or_null<Expr>(RetExpr);
Ted Kremenek066dd932007-08-24 21:09:09 +0000274}
275
Ted Kremenekc6501db2008-06-17 03:11:08 +0000276Stmt::child_iterator ReturnStmt::child_begin() {
277 return &RetExpr;
278}
279Stmt::child_iterator ReturnStmt::child_end() {
280 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek5b3ed282007-08-27 20:58:16 +0000281}
Ted Kremenek066dd932007-08-24 21:09:09 +0000282
Chris Lattner73c56c02007-10-29 04:04:16 +0000283// AsmStmt
284Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
285Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
286
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000287// ObjCAtCatchStmt
288Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weberde565e32008-08-05 23:15:29 +0000289Stmt::child_iterator ObjCAtCatchStmt::child_end() {
290 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +0000291}
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000292
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000293// ObjCAtFinallyStmt
294Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
295Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000296
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000297// ObjCAtTryStmt
298Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weberde565e32008-08-05 23:15:29 +0000299Stmt::child_iterator ObjCAtTryStmt::child_end() {
300 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +0000301}
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000302
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000303// ObjCAtThrowStmt
304Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000305 return &Throw;
306}
307
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000308Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000309 return &Throw+1;
310}
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000311
312// ObjCAtSynchronizedStmt
313Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian284011b2008-01-29 22:59:37 +0000314 return &SubStmts[0];
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000315}
316
317Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian284011b2008-01-29 22:59:37 +0000318 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000319}
320