blob: 749b677d910fe6fc1fc654b89342a11730f4c0e9 [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"
Sebastian Redl54c04d42008-12-22 19:15:10 +000017#include "clang/AST/Type.h"
Ted Kremenekfe7a9602009-02-06 01:42:09 +000018#include "clang/AST/ASTContext.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;
Douglas Gregorbe35ce92008-11-14 12:46:07 +000034#define STMT(CLASS, PARENT) \
35 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
36 StmtClassInfo[(unsigned)Stmt::CLASS##Class].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
Chris Lattner34a22092009-03-04 04:23:07 +000046void Stmt::DestroyChildren(ASTContext &C) {
47 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
Douglas Gregor2b5d4302009-01-16 06:50:08 +000048 if (Stmt* Child = *I++) Child->Destroy(C);
Ted Kremenekce20e8f2008-05-20 00:43:19 +000049}
50
Chris Lattner34a22092009-03-04 04:23:07 +000051void Stmt::Destroy(ASTContext &C) {
Ted Kremenekce20e8f2008-05-20 00:43:19 +000052 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.
Ted Kremenekfe7a9602009-02-06 01:42:09 +000055 this->~Stmt();
56 C.Deallocate((void *)this);
Ted Kremenekee579422008-05-19 22:02:12 +000057}
58
Chris Lattner34a22092009-03-04 04:23:07 +000059void DeclStmt::Destroy(ASTContext &C) {
Ted Kremenekfe7a9602009-02-06 01:42:09 +000060 this->~DeclStmt();
61 C.Deallocate((void *)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
Ted Kremenek5778acf2008-10-27 18:40:21 +0000133const Expr* AsmStmt::getOutputExpr(unsigned i) const {
134 return cast<Expr>(Exprs[i]);
135}
136Expr* AsmStmt::getOutputExpr(unsigned i) {
137 return cast<Expr>(Exprs[i]);
138}
139Expr* AsmStmt::getInputExpr(unsigned i) {
140 return cast<Expr>(Exprs[i + NumOutputs]);
141}
142const Expr* AsmStmt::getInputExpr(unsigned i) const {
143 return cast<Expr>(Exprs[i + NumOutputs]);
144}
145
Chris Lattner86f5e132008-01-30 05:01:46 +0000146//===----------------------------------------------------------------------===//
147// Constructors
148//===----------------------------------------------------------------------===//
149
Anders Carlsson19fe1162008-02-05 23:03:50 +0000150AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattner86f5e132008-01-30 05:01:46 +0000151 unsigned numoutputs, unsigned numinputs,
152 std::string *names, StringLiteral **constraints,
153 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
154 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000155 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlsson19fe1162008-02-05 23:03:50 +0000156 , IsSimple(issimple), IsVolatile(isvolatile)
157 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000158 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
159 Names.push_back(names[i]);
160 Exprs.push_back(exprs[i]);
Nico Weberde565e32008-08-05 23:15:29 +0000161 Constraints.push_back(constraints[i]);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000162 }
Nico Weberde565e32008-08-05 23:15:29 +0000163
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000164 for (unsigned i = 0; i != numclobbers; i++)
165 Clobbers.push_back(clobbers[i]);
166}
167
Chris Lattner86f5e132008-01-30 05:01:46 +0000168ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
169 Stmt *Body, SourceLocation FCL,
Nico Weberde565e32008-08-05 23:15:29 +0000170 SourceLocation RPL)
Chris Lattner86f5e132008-01-30 05:01:46 +0000171: Stmt(ObjCForCollectionStmtClass) {
172 SubExprs[ELEM] = Elem;
173 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
174 SubExprs[BODY] = Body;
175 ForLoc = FCL;
176 RParenLoc = RPL;
177}
178
179
Nico Weberde565e32008-08-05 23:15:29 +0000180ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
181 SourceLocation rparenloc,
Steve Naroff371b8fb2009-03-03 19:52:17 +0000182 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattner86f5e132008-01-30 05:01:46 +0000183 Stmt *atCatchList)
184: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff371b8fb2009-03-03 19:52:17 +0000185 ExceptionDecl = catchVarDecl;
Chris Lattner86f5e132008-01-30 05:01:46 +0000186 SubExprs[BODY] = atCatchStmt;
Eli Friedman1f97e572008-05-25 04:34:57 +0000187 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar947bca22009-03-01 04:28:32 +0000188 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman1f97e572008-05-25 04:34:57 +0000189 if (atCatchList) {
Ted Kremeneka4965842008-02-01 21:28:59 +0000190 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
191
Nico Weberde565e32008-08-05 23:15:29 +0000192 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremeneka4965842008-02-01 21:28:59 +0000193 AtCatchList = NextCatch;
Nico Weberde565e32008-08-05 23:15:29 +0000194
Ted Kremeneka4965842008-02-01 21:28:59 +0000195 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner86f5e132008-01-30 05:01:46 +0000196 }
197 AtCatchLoc = atCatchLoc;
198 RParenLoc = rparenloc;
199}
200
201
Ted Kremenek066dd932007-08-24 21:09:09 +0000202//===----------------------------------------------------------------------===//
203// Child Iterators for iterating over subexpressions/substatements
204//===----------------------------------------------------------------------===//
205
206// DeclStmt
Ted Kremenek9bb286f2008-10-07 23:09:49 +0000207Stmt::child_iterator DeclStmt::child_begin() {
208 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000209}
210
Ted Kremenek9bb286f2008-10-07 23:09:49 +0000211Stmt::child_iterator DeclStmt::child_end() {
212 return StmtIterator(DG.end(), DG.end());
Ted Kremenekacf920d2008-10-06 20:54:44 +0000213}
214
Ted Kremenek066dd932007-08-24 21:09:09 +0000215// NullStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000216Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
217Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000218
219// CompoundStmt
220Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek5a201952009-02-07 01:47:29 +0000221Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000222
Ted Kremenek14f0d1a2007-08-30 16:50:46 +0000223// CaseStmt
224Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
225Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
226
227// DefaultStmt
228Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
229Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000230
231// LabelStmt
232Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000233Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000234
235// IfStmt
236Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
237Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
238
239// SwitchStmt
240Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
241Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
242
243// WhileStmt
244Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
245Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
246
247// DoStmt
248Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
249Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
250
251// ForStmt
252Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
253Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
254
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000255// ObjCForCollectionStmt
Nico Weberde565e32008-08-05 23:15:29 +0000256Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
257 return &SubExprs[0];
Fariborz Jahanian83615522008-01-02 22:54:34 +0000258}
Nico Weberde565e32008-08-05 23:15:29 +0000259Stmt::child_iterator ObjCForCollectionStmt::child_end() {
260 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian83615522008-01-02 22:54:34 +0000261}
262
Ted Kremenek066dd932007-08-24 21:09:09 +0000263// GotoStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000264Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
265Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000266
267// IndirectGotoStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000268Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
269const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000270
Ted Kremenekc6501db2008-06-17 03:11:08 +0000271Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
272Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000273
274// ContinueStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000275Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
276Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000277
278// BreakStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000279Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
280Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000281
282// ReturnStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000283const Expr* ReturnStmt::getRetValue() const {
284 return cast_or_null<Expr>(RetExpr);
285}
286Expr* ReturnStmt::getRetValue() {
287 return cast_or_null<Expr>(RetExpr);
Ted Kremenek066dd932007-08-24 21:09:09 +0000288}
289
Ted Kremenekc6501db2008-06-17 03:11:08 +0000290Stmt::child_iterator ReturnStmt::child_begin() {
291 return &RetExpr;
292}
293Stmt::child_iterator ReturnStmt::child_end() {
294 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek5b3ed282007-08-27 20:58:16 +0000295}
Ted Kremenek066dd932007-08-24 21:09:09 +0000296
Chris Lattner73c56c02007-10-29 04:04:16 +0000297// AsmStmt
Ted Kremenek5778acf2008-10-27 18:40:21 +0000298Stmt::child_iterator AsmStmt::child_begin() {
299 return Exprs.empty() ? 0 : &Exprs[0];
300}
301Stmt::child_iterator AsmStmt::child_end() {
302 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
303}
Chris Lattner73c56c02007-10-29 04:04:16 +0000304
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000305// ObjCAtCatchStmt
306Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weberde565e32008-08-05 23:15:29 +0000307Stmt::child_iterator ObjCAtCatchStmt::child_end() {
308 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +0000309}
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000310
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000311// ObjCAtFinallyStmt
312Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
313Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000314
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000315// ObjCAtTryStmt
316Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weberde565e32008-08-05 23:15:29 +0000317Stmt::child_iterator ObjCAtTryStmt::child_end() {
318 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +0000319}
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000320
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000321// ObjCAtThrowStmt
322Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000323 return &Throw;
324}
325
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000326Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000327 return &Throw+1;
328}
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000329
330// ObjCAtSynchronizedStmt
331Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian284011b2008-01-29 22:59:37 +0000332 return &SubStmts[0];
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000333}
334
335Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian284011b2008-01-29 22:59:37 +0000336 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000337}
338
Sebastian Redl54c04d42008-12-22 19:15:10 +0000339// CXXCatchStmt
340Stmt::child_iterator CXXCatchStmt::child_begin() {
341 return &HandlerBlock;
342}
343
344Stmt::child_iterator CXXCatchStmt::child_end() {
345 return &HandlerBlock + 1;
346}
347
348QualType CXXCatchStmt::getCaughtType() {
349 if (ExceptionDecl)
350 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
351 return QualType();
352}
353
354void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000355 if (ExceptionDecl)
356 ExceptionDecl->Destroy(C);
Sebastian Redl54c04d42008-12-22 19:15:10 +0000357 Stmt::Destroy(C);
358}
Sebastian Redl9b244a82008-12-22 21:35:02 +0000359
360// CXXTryStmt
361Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
362Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
363
364CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
365 Stmt **handlers, unsigned numHandlers)
366 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
367 Stmts.push_back(tryBlock);
368 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
369}