blob: ffc6c2e62a8567c5b0bfd9929bdfd8c8ece09a53 [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 Kremenek9c1863e2008-05-19 22:02:12 +000045void Stmt::DestroyChildren() {
46 for (child_iterator I = child_begin(), E = child_end(); I !=E; ++I)
47 delete *I; // Handles the case when *I == NULL.
48}
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000051 // Ensure the table is primed.
52 getStmtInfoTableEntry(Stmt::NullStmtClass);
53
Reid Spencer5f016e22007-07-11 17:01:13 +000054 unsigned sum = 0;
55 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000056 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000057 if (StmtClassInfo[i].Name == 0) continue;
58 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 }
60 fprintf(stderr, " %d stmts/exprs total.\n", sum);
61 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000062 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000063 if (StmtClassInfo[i].Name == 0) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +000064 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000065 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
66 StmtClassInfo[i].Size,
67 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
68 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000069 }
70 fprintf(stderr, "Total bytes = %d\n", sum);
71}
72
73void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000074 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000075}
76
77static bool StatSwitch = false;
78
79bool Stmt::CollectingStats(bool enable) {
80 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000081 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000082}
83
84
Reid Spencer5f016e22007-07-11 17:01:13 +000085const char *LabelStmt::getName() const {
86 return getID()->getName();
87}
88
Steve Naroff507f2d52007-08-31 23:49:30 +000089// This is defined here to avoid polluting Stmt.h with importing Expr.h
90SourceRange ReturnStmt::getSourceRange() const {
91 if (RetExpr)
92 return SourceRange(RetLoc, RetExpr->getLocEnd());
93 else
94 return SourceRange(RetLoc);
95}
96
Ted Kremenekd48ade62007-10-01 16:34:52 +000097bool Stmt::hasImplicitControlFlow() const {
98 switch (sClass) {
99 default:
100 return false;
101
102 case CallExprClass:
103 case ConditionalOperatorClass:
104 case ChooseExprClass:
105 case StmtExprClass:
106 case DeclStmtClass:
107 return true;
108
109 case Stmt::BinaryOperatorClass: {
110 const BinaryOperator* B = cast<BinaryOperator>(this);
111 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
112 return true;
113 else
114 return false;
115 }
116 }
117}
118
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000119//===----------------------------------------------------------------------===//
120// Constructors
121//===----------------------------------------------------------------------===//
122
Anders Carlssondfab34a2008-02-05 23:03:50 +0000123AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000124 unsigned numoutputs, unsigned numinputs,
125 std::string *names, StringLiteral **constraints,
126 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
127 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000128 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000129 , IsSimple(issimple), IsVolatile(isvolatile)
130 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000131 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
132 Names.push_back(names[i]);
133 Exprs.push_back(exprs[i]);
134 Constraints.push_back(constraints[i]);
135 }
136
137 for (unsigned i = 0; i != numclobbers; i++)
138 Clobbers.push_back(clobbers[i]);
139}
140
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000141ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
142 Stmt *Body, SourceLocation FCL,
143 SourceLocation RPL)
144: Stmt(ObjCForCollectionStmtClass) {
145 SubExprs[ELEM] = Elem;
146 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
147 SubExprs[BODY] = Body;
148 ForLoc = FCL;
149 RParenLoc = RPL;
150}
151
152
153ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
154 SourceLocation rparenloc,
155 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
156 Stmt *atCatchList)
157: Stmt(ObjCAtCatchStmtClass) {
158 SubExprs[SELECTOR] = catchVarStmtDecl;
159 SubExprs[BODY] = atCatchStmt;
160 if (!atCatchList)
Ted Kremenekff981022008-02-01 21:28:59 +0000161 SubExprs[NEXT_CATCH] = NULL;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000162 else {
Ted Kremenekff981022008-02-01 21:28:59 +0000163 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
164
165 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
166 AtCatchList = NextCatch;
167
168 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000169 }
170 AtCatchLoc = atCatchLoc;
171 RParenLoc = rparenloc;
172}
173
174
Ted Kremenek82977772007-08-24 21:09:09 +0000175//===----------------------------------------------------------------------===//
176// Child Iterators for iterating over subexpressions/substatements
177//===----------------------------------------------------------------------===//
178
179// DeclStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000180Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); }
181Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000182
183// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000184Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
185Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000186
187// CompoundStmt
188Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
189Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
190
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000191// CaseStmt
192Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
193Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
194
195// DefaultStmt
196Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
197Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000198
199// LabelStmt
200Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000201Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000202
203// IfStmt
204Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
205Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
206
207// SwitchStmt
208Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
209Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
210
211// WhileStmt
212Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
213Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
214
215// DoStmt
216Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
217Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
218
219// ForStmt
220Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
221Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
222
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000223// ObjCForCollectionStmt
224Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000225 return &SubExprs[0];
226}
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000227Stmt::child_iterator ObjCForCollectionStmt::child_end() {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000228 return &SubExprs[0]+END_EXPR;
229}
230
Ted Kremenek82977772007-08-24 21:09:09 +0000231// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000232Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
233Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000234
235// IndirectGotoStmt
236Stmt::child_iterator IndirectGotoStmt::child_begin() {
237 return reinterpret_cast<Stmt**>(&Target);
238}
239
Ted Kremenek9caf8b12007-10-18 00:24:38 +0000240Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000241
242// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000243Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
244Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000245
246// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000247Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
248Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000249
250// ReturnStmt
Ted Kremenek2298f912007-08-27 20:58:16 +0000251Stmt::child_iterator ReturnStmt::child_begin() {
252 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr);
Ted Kremenek9ac59282007-10-18 23:28:49 +0000253 else return child_iterator();
Ted Kremenek82977772007-08-24 21:09:09 +0000254}
255
Ted Kremenek2298f912007-08-27 20:58:16 +0000256Stmt::child_iterator ReturnStmt::child_end() {
257 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr)+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +0000258 else return child_iterator();
Ted Kremenek2298f912007-08-27 20:58:16 +0000259}
Ted Kremenek82977772007-08-24 21:09:09 +0000260
Chris Lattnerfe795952007-10-29 04:04:16 +0000261// AsmStmt
262Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
263Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
264
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000265// ObjCAtCatchStmt
266Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
267Stmt::child_iterator ObjCAtCatchStmt::child_end() {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000268 return &SubExprs[0]+END_EXPR;
269}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000270
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000271// ObjCAtFinallyStmt
272Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
273Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000274
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275// ObjCAtTryStmt
276Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
277Stmt::child_iterator ObjCAtTryStmt::child_end() {
Fariborz Jahanian89079ea2007-11-07 17:43:16 +0000278 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000279}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000280
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000281// ObjCAtThrowStmt
282Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000283 return &Throw;
284}
285
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000286Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000287 return &Throw+1;
288}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000289
290// ObjCAtSynchronizedStmt
291Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000292 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000293}
294
295Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000296 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000297}
298