blob: 993dda71c98c576329323513ef38b4caa81dd048 [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);
52 this->~Stmt();
Ted Kremenek9c1863e2008-05-19 22:02:12 +000053}
54
Reid Spencer5f016e22007-07-11 17:01:13 +000055void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000056 // Ensure the table is primed.
57 getStmtInfoTableEntry(Stmt::NullStmtClass);
58
Reid Spencer5f016e22007-07-11 17:01:13 +000059 unsigned sum = 0;
60 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000061 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000062 if (StmtClassInfo[i].Name == 0) continue;
63 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000064 }
65 fprintf(stderr, " %d stmts/exprs total.\n", sum);
66 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000067 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000068 if (StmtClassInfo[i].Name == 0) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +000069 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000070 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
71 StmtClassInfo[i].Size,
72 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
73 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
75 fprintf(stderr, "Total bytes = %d\n", sum);
76}
77
78void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000079 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000080}
81
82static bool StatSwitch = false;
83
84bool Stmt::CollectingStats(bool enable) {
85 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000086 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
89
Reid Spencer5f016e22007-07-11 17:01:13 +000090const char *LabelStmt::getName() const {
91 return getID()->getName();
92}
93
Steve Naroff507f2d52007-08-31 23:49:30 +000094// This is defined here to avoid polluting Stmt.h with importing Expr.h
95SourceRange ReturnStmt::getSourceRange() const {
96 if (RetExpr)
97 return SourceRange(RetLoc, RetExpr->getLocEnd());
98 else
99 return SourceRange(RetLoc);
100}
101
Ted Kremenekd48ade62007-10-01 16:34:52 +0000102bool Stmt::hasImplicitControlFlow() const {
103 switch (sClass) {
104 default:
105 return false;
106
107 case CallExprClass:
108 case ConditionalOperatorClass:
109 case ChooseExprClass:
110 case StmtExprClass:
111 case DeclStmtClass:
112 return true;
113
114 case Stmt::BinaryOperatorClass: {
115 const BinaryOperator* B = cast<BinaryOperator>(this);
116 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
117 return true;
118 else
119 return false;
120 }
121 }
122}
123
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000124//===----------------------------------------------------------------------===//
125// Constructors
126//===----------------------------------------------------------------------===//
127
Anders Carlssondfab34a2008-02-05 23:03:50 +0000128AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000129 unsigned numoutputs, unsigned numinputs,
130 std::string *names, StringLiteral **constraints,
131 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
132 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000133 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000134 , IsSimple(issimple), IsVolatile(isvolatile)
135 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000136 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
137 Names.push_back(names[i]);
138 Exprs.push_back(exprs[i]);
139 Constraints.push_back(constraints[i]);
140 }
141
142 for (unsigned i = 0; i != numclobbers; i++)
143 Clobbers.push_back(clobbers[i]);
144}
145
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000146ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
147 Stmt *Body, SourceLocation FCL,
148 SourceLocation RPL)
149: Stmt(ObjCForCollectionStmtClass) {
150 SubExprs[ELEM] = Elem;
151 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
152 SubExprs[BODY] = Body;
153 ForLoc = FCL;
154 RParenLoc = RPL;
155}
156
157
158ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
159 SourceLocation rparenloc,
160 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
161 Stmt *atCatchList)
162: Stmt(ObjCAtCatchStmtClass) {
163 SubExprs[SELECTOR] = catchVarStmtDecl;
164 SubExprs[BODY] = atCatchStmt;
165 if (!atCatchList)
Ted Kremenekff981022008-02-01 21:28:59 +0000166 SubExprs[NEXT_CATCH] = NULL;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000167 else {
Ted Kremenekff981022008-02-01 21:28:59 +0000168 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
169
170 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
171 AtCatchList = NextCatch;
172
173 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000174 }
175 AtCatchLoc = atCatchLoc;
176 RParenLoc = rparenloc;
177}
178
179
Ted Kremenek82977772007-08-24 21:09:09 +0000180//===----------------------------------------------------------------------===//
181// Child Iterators for iterating over subexpressions/substatements
182//===----------------------------------------------------------------------===//
183
184// DeclStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000185Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); }
186Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000187
188// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000189Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
190Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000191
192// CompoundStmt
193Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
194Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
195
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000196// CaseStmt
197Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
198Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
199
200// DefaultStmt
201Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
202Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000203
204// LabelStmt
205Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000206Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000207
208// IfStmt
209Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
210Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
211
212// SwitchStmt
213Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
214Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
215
216// WhileStmt
217Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
218Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
219
220// DoStmt
221Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
222Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
223
224// ForStmt
225Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
226Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
227
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000228// ObjCForCollectionStmt
229Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000230 return &SubExprs[0];
231}
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000232Stmt::child_iterator ObjCForCollectionStmt::child_end() {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000233 return &SubExprs[0]+END_EXPR;
234}
235
Ted Kremenek82977772007-08-24 21:09:09 +0000236// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000237Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
238Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000239
240// IndirectGotoStmt
241Stmt::child_iterator IndirectGotoStmt::child_begin() {
242 return reinterpret_cast<Stmt**>(&Target);
243}
244
Ted Kremenek9caf8b12007-10-18 00:24:38 +0000245Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000246
247// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000248Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
249Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000250
251// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000252Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
253Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000254
255// ReturnStmt
Ted Kremenek2298f912007-08-27 20:58:16 +0000256Stmt::child_iterator ReturnStmt::child_begin() {
257 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr);
Ted Kremenek9ac59282007-10-18 23:28:49 +0000258 else return child_iterator();
Ted Kremenek82977772007-08-24 21:09:09 +0000259}
260
Ted Kremenek2298f912007-08-27 20:58:16 +0000261Stmt::child_iterator ReturnStmt::child_end() {
262 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr)+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +0000263 else return child_iterator();
Ted Kremenek2298f912007-08-27 20:58:16 +0000264}
Ted Kremenek82977772007-08-24 21:09:09 +0000265
Chris Lattnerfe795952007-10-29 04:04:16 +0000266// AsmStmt
267Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
268Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
269
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000270// ObjCAtCatchStmt
271Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
272Stmt::child_iterator ObjCAtCatchStmt::child_end() {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000273 return &SubExprs[0]+END_EXPR;
274}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000275
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000276// ObjCAtFinallyStmt
277Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
278Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000279
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000280// ObjCAtTryStmt
281Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
282Stmt::child_iterator ObjCAtTryStmt::child_end() {
Fariborz Jahanian89079ea2007-11-07 17:43:16 +0000283 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000284}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000285
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000286// ObjCAtThrowStmt
287Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000288 return &Throw;
289}
290
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000291Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000292 return &Throw+1;
293}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000294
295// ObjCAtSynchronizedStmt
296Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000297 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000298}
299
300Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000301 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000302}
303