blob: 689fe852d8fe47c00db05f9492c670ab277b8b0e [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 +000017using namespace clang;
18
Reid Spencer5f016e22007-07-11 17:01:13 +000019static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000020 const char *Name;
21 unsigned Counter;
22 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000023} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000024
25static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
26 static bool Initialized = false;
27 if (Initialized)
28 return StmtClassInfo[E];
29
30 // Intialize the table on the first use.
31 Initialized = true;
32#define STMT(N, CLASS, PARENT) \
33 StmtClassInfo[N].Name = #CLASS; \
34 StmtClassInfo[N].Size = sizeof(CLASS);
Reid Spencer5f016e22007-07-11 17:01:13 +000035#include "clang/AST/StmtNodes.def"
Nico Weber608b17f2008-08-05 23:15:29 +000036
Chris Lattner63381352007-08-25 01:42:24 +000037 return StmtClassInfo[E];
38}
39
Reid Spencer5f016e22007-07-11 17:01:13 +000040const char *Stmt::getStmtClassName() const {
Chris Lattner63381352007-08-25 01:42:24 +000041 return getStmtInfoTableEntry(sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000042}
43
Ted Kremenek27f8a282008-05-20 00:43:19 +000044void Stmt::DestroyChildren(ASTContext& C) {
Ted Kremenek9c1863e2008-05-19 22:02:12 +000045 for (child_iterator I = child_begin(), E = child_end(); I !=E; ++I)
Ted Kremenek27f8a282008-05-20 00:43:19 +000046 if (Stmt* Child = *I) Child->Destroy(C);
47}
48
49void Stmt::Destroy(ASTContext& C) {
50 DestroyChildren(C);
Ted Kremenekf809e3b2008-05-20 04:10:52 +000051 // FIXME: Eventually all Stmts should be allocated with the allocator
52 // in ASTContext, just like with Decls.
53 // this->~Stmt();
54 delete this;
Ted Kremenek9c1863e2008-05-19 22:02:12 +000055}
56
Ted Kremenek8e355f22008-05-21 15:53:55 +000057void DeclStmt::Destroy(ASTContext& C) {
58 TheDecl->Destroy(C);
Ted Kremenek936ff132008-05-21 16:00:02 +000059 delete this;
Ted Kremenek8e355f22008-05-21 15:53:55 +000060}
61
Reid Spencer5f016e22007-07-11 17:01:13 +000062void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000063 // Ensure the table is primed.
64 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000065
Reid Spencer5f016e22007-07-11 17:01:13 +000066 unsigned sum = 0;
67 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000068 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000069 if (StmtClassInfo[i].Name == 0) continue;
70 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000071 }
72 fprintf(stderr, " %d stmts/exprs total.\n", sum);
73 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000074 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000075 if (StmtClassInfo[i].Name == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000076 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000077 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
78 StmtClassInfo[i].Size,
79 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
80 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000081 }
82 fprintf(stderr, "Total bytes = %d\n", sum);
83}
84
85void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000086 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
89static bool StatSwitch = false;
90
91bool Stmt::CollectingStats(bool enable) {
92 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000093 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000094}
95
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097const char *LabelStmt::getName() const {
98 return getID()->getName();
99}
100
Steve Naroff507f2d52007-08-31 23:49:30 +0000101// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000102SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000103 if (RetExpr)
104 return SourceRange(RetLoc, RetExpr->getLocEnd());
105 else
106 return SourceRange(RetLoc);
107}
108
Ted Kremenekd48ade62007-10-01 16:34:52 +0000109bool Stmt::hasImplicitControlFlow() const {
110 switch (sClass) {
111 default:
112 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000113
Ted Kremenekd48ade62007-10-01 16:34:52 +0000114 case CallExprClass:
115 case ConditionalOperatorClass:
116 case ChooseExprClass:
117 case StmtExprClass:
118 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000119 return true;
120
Ted Kremenekd48ade62007-10-01 16:34:52 +0000121 case Stmt::BinaryOperatorClass: {
122 const BinaryOperator* B = cast<BinaryOperator>(this);
123 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
124 return true;
125 else
126 return false;
127 }
128 }
129}
130
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000131//===----------------------------------------------------------------------===//
132// Constructors
133//===----------------------------------------------------------------------===//
134
Anders Carlssondfab34a2008-02-05 23:03:50 +0000135AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000136 unsigned numoutputs, unsigned numinputs,
137 std::string *names, StringLiteral **constraints,
138 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
139 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000140 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000141 , IsSimple(issimple), IsVolatile(isvolatile)
142 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000143 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
144 Names.push_back(names[i]);
145 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000146 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000147 }
Nico Weber608b17f2008-08-05 23:15:29 +0000148
Anders Carlssonb235fc22007-11-22 01:36:19 +0000149 for (unsigned i = 0; i != numclobbers; i++)
150 Clobbers.push_back(clobbers[i]);
151}
152
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000153ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
154 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000155 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000156: Stmt(ObjCForCollectionStmtClass) {
157 SubExprs[ELEM] = Elem;
158 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
159 SubExprs[BODY] = Body;
160 ForLoc = FCL;
161 RParenLoc = RPL;
162}
163
164
Nico Weber608b17f2008-08-05 23:15:29 +0000165ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
166 SourceLocation rparenloc,
167 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000168 Stmt *atCatchList)
169: Stmt(ObjCAtCatchStmtClass) {
170 SubExprs[SELECTOR] = catchVarStmtDecl;
171 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000172 SubExprs[NEXT_CATCH] = NULL;
173 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000174 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
175
Nico Weber608b17f2008-08-05 23:15:29 +0000176 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000177 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000178
Ted Kremenekff981022008-02-01 21:28:59 +0000179 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000180 }
181 AtCatchLoc = atCatchLoc;
182 RParenLoc = rparenloc;
183}
184
185
Ted Kremenek82977772007-08-24 21:09:09 +0000186//===----------------------------------------------------------------------===//
187// Child Iterators for iterating over subexpressions/substatements
188//===----------------------------------------------------------------------===//
189
190// DeclStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000191Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); }
192Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000193
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000194DeclStmt::decl_iterator& DeclStmt::decl_iterator::operator++() {
195 D = D->getNextDeclarator();
196 return *this;
197}
198
Ted Kremenek82977772007-08-24 21:09:09 +0000199// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000200Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
201Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000202
203// CompoundStmt
204Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
205Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
206
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000207// CaseStmt
208Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
209Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
210
211// DefaultStmt
212Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
213Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000214
215// LabelStmt
216Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000217Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000218
219// IfStmt
220Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
221Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
222
223// SwitchStmt
224Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
225Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
226
227// WhileStmt
228Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
229Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
230
231// DoStmt
232Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
233Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
234
235// ForStmt
236Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
237Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
238
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000239// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000240Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
241 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000242}
Nico Weber608b17f2008-08-05 23:15:29 +0000243Stmt::child_iterator ObjCForCollectionStmt::child_end() {
244 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000245}
246
Ted Kremenek82977772007-08-24 21:09:09 +0000247// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000248Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
249Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000250
251// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000252Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
253const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000254
Ted Kremenek1060aff2008-06-17 03:11:08 +0000255Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
256Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000257
258// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000259Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
260Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000261
262// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000263Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
264Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000265
266// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000267const Expr* ReturnStmt::getRetValue() const {
268 return cast_or_null<Expr>(RetExpr);
269}
270Expr* ReturnStmt::getRetValue() {
271 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000272}
273
Ted Kremenek1060aff2008-06-17 03:11:08 +0000274Stmt::child_iterator ReturnStmt::child_begin() {
275 return &RetExpr;
276}
277Stmt::child_iterator ReturnStmt::child_end() {
278 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000279}
Ted Kremenek82977772007-08-24 21:09:09 +0000280
Chris Lattnerfe795952007-10-29 04:04:16 +0000281// AsmStmt
282Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
283Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
284
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000285// ObjCAtCatchStmt
286Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000287Stmt::child_iterator ObjCAtCatchStmt::child_end() {
288 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000289}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000290
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000291// ObjCAtFinallyStmt
292Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
293Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000294
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000295// ObjCAtTryStmt
296Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000297Stmt::child_iterator ObjCAtTryStmt::child_end() {
298 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000299}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000300
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000301// ObjCAtThrowStmt
302Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000303 return &Throw;
304}
305
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000306Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000307 return &Throw+1;
308}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000309
310// ObjCAtSynchronizedStmt
311Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000312 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000313}
314
315Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000316 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000317}
318