blob: 749b677d910fe6fc1fc654b89342a11730f4c0e9 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +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 Naroff9ed3e772008-05-29 21:12:08 +000016#include "clang/AST/ExprObjC.h"
Sebastian Redl743c8162008-12-22 19:15:10 +000017#include "clang/AST/Type.h"
Ted Kremenek39dc20a2009-02-06 01:42:09 +000018#include "clang/AST/ASTContext.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattner4b009652007-07-25 00:24:17 +000021static struct StmtClassNameTable {
Chris Lattner603bf122007-08-25 01:42:24 +000022 const char *Name;
23 unsigned Counter;
24 unsigned Size;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000025} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner603bf122007-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 Gregor19330152008-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);
Chris Lattner4b009652007-07-25 00:24:17 +000037#include "clang/AST/StmtNodes.def"
Nico Weber7340a2f2008-08-05 23:15:29 +000038
Chris Lattner603bf122007-08-25 01:42:24 +000039 return StmtClassInfo[E];
40}
41
Chris Lattner4b009652007-07-25 00:24:17 +000042const char *Stmt::getStmtClassName() const {
Chris Lattner603bf122007-08-25 01:42:24 +000043 return getStmtInfoTableEntry(sClass).Name;
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
Chris Lattnerdaac6942009-03-04 04:23:07 +000046void Stmt::DestroyChildren(ASTContext &C) {
47 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
Douglas Gregor0e7e7f42009-01-16 06:50:08 +000048 if (Stmt* Child = *I++) Child->Destroy(C);
Ted Kremenekafdf8112008-05-20 00:43:19 +000049}
50
Chris Lattnerdaac6942009-03-04 04:23:07 +000051void Stmt::Destroy(ASTContext &C) {
Ted Kremenekafdf8112008-05-20 00:43:19 +000052 DestroyChildren(C);
Ted Kremenekbde41092008-05-20 04:10:52 +000053 // FIXME: Eventually all Stmts should be allocated with the allocator
54 // in ASTContext, just like with Decls.
Ted Kremenek39dc20a2009-02-06 01:42:09 +000055 this->~Stmt();
56 C.Deallocate((void *)this);
Ted Kremenek1ed5dc62008-05-19 22:02:12 +000057}
58
Chris Lattnerdaac6942009-03-04 04:23:07 +000059void DeclStmt::Destroy(ASTContext &C) {
Ted Kremenek39dc20a2009-02-06 01:42:09 +000060 this->~DeclStmt();
61 C.Deallocate((void *)this);
Ted Kremeneka1e77702008-05-21 15:53:55 +000062}
63
Chris Lattner4b009652007-07-25 00:24:17 +000064void Stmt::PrintStats() {
Chris Lattner603bf122007-08-25 01:42:24 +000065 // Ensure the table is primed.
66 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber7340a2f2008-08-05 23:15:29 +000067
Chris Lattner4b009652007-07-25 00:24:17 +000068 unsigned sum = 0;
69 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner9c0da3b2007-08-25 01:55:00 +000070 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000071 if (StmtClassInfo[i].Name == 0) continue;
72 sum += StmtClassInfo[i].Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000073 }
74 fprintf(stderr, " %d stmts/exprs total.\n", sum);
75 sum = 0;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000076 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000077 if (StmtClassInfo[i].Name == 0) continue;
Nico Weber7340a2f2008-08-05 23:15:29 +000078 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner603bf122007-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;
Chris Lattner4b009652007-07-25 00:24:17 +000083 }
84 fprintf(stderr, "Total bytes = %d\n", sum);
85}
86
87void Stmt::addStmtClass(StmtClass s) {
Chris Lattner603bf122007-08-25 01:42:24 +000088 ++getStmtInfoTableEntry(s).Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000089}
90
91static bool StatSwitch = false;
92
93bool Stmt::CollectingStats(bool enable) {
94 if (enable) StatSwitch = true;
Chris Lattner603bf122007-08-25 01:42:24 +000095 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000096}
97
98
Chris Lattner4b009652007-07-25 00:24:17 +000099const char *LabelStmt::getName() const {
100 return getID()->getName();
101}
102
Steve Naroffc32a20d2007-08-31 23:49:30 +0000103// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber7340a2f2008-08-05 23:15:29 +0000104SourceRange ReturnStmt::getSourceRange() const {
Steve Naroffc32a20d2007-08-31 23:49:30 +0000105 if (RetExpr)
106 return SourceRange(RetLoc, RetExpr->getLocEnd());
107 else
108 return SourceRange(RetLoc);
109}
110
Ted Kremenekad5682f2007-10-01 16:34:52 +0000111bool Stmt::hasImplicitControlFlow() const {
112 switch (sClass) {
113 default:
114 return false;
Nico Weber7340a2f2008-08-05 23:15:29 +0000115
Ted Kremenekad5682f2007-10-01 16:34:52 +0000116 case CallExprClass:
117 case ConditionalOperatorClass:
118 case ChooseExprClass:
119 case StmtExprClass:
120 case DeclStmtClass:
Nico Weber7340a2f2008-08-05 23:15:29 +0000121 return true;
122
Ted Kremenekad5682f2007-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 Kremenekb30de272008-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 Lattner006d1542008-01-30 05:01:46 +0000146//===----------------------------------------------------------------------===//
147// Constructors
148//===----------------------------------------------------------------------===//
149
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000150AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattner006d1542008-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 Carlsson965d5202007-11-22 01:36:19 +0000155 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000156 , IsSimple(issimple), IsVolatile(isvolatile)
157 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson965d5202007-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 Weber7340a2f2008-08-05 23:15:29 +0000161 Constraints.push_back(constraints[i]);
Anders Carlsson965d5202007-11-22 01:36:19 +0000162 }
Nico Weber7340a2f2008-08-05 23:15:29 +0000163
Anders Carlsson965d5202007-11-22 01:36:19 +0000164 for (unsigned i = 0; i != numclobbers; i++)
165 Clobbers.push_back(clobbers[i]);
166}
167
Chris Lattner006d1542008-01-30 05:01:46 +0000168ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
169 Stmt *Body, SourceLocation FCL,
Nico Weber7340a2f2008-08-05 23:15:29 +0000170 SourceLocation RPL)
Chris Lattner006d1542008-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 Weber7340a2f2008-08-05 23:15:29 +0000180ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
181 SourceLocation rparenloc,
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000182 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattner006d1542008-01-30 05:01:46 +0000183 Stmt *atCatchList)
184: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000185 ExceptionDecl = catchVarDecl;
Chris Lattner006d1542008-01-30 05:01:46 +0000186 SubExprs[BODY] = atCatchStmt;
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000187 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbard23d9922009-03-01 04:28:32 +0000188 // FIXME: O(N^2) in number of catch blocks.
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000189 if (atCatchList) {
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000190 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
191
Nico Weber7340a2f2008-08-05 23:15:29 +0000192 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000193 AtCatchList = NextCatch;
Nico Weber7340a2f2008-08-05 23:15:29 +0000194
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000195 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner006d1542008-01-30 05:01:46 +0000196 }
197 AtCatchLoc = atCatchLoc;
198 RParenLoc = rparenloc;
199}
200
201
Ted Kremenek51be0562007-08-24 21:09:09 +0000202//===----------------------------------------------------------------------===//
203// Child Iterators for iterating over subexpressions/substatements
204//===----------------------------------------------------------------------===//
205
206// DeclStmt
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000207Stmt::child_iterator DeclStmt::child_begin() {
208 return StmtIterator(DG.begin(), DG.end());
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000209}
210
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000211Stmt::child_iterator DeclStmt::child_end() {
212 return StmtIterator(DG.end(), DG.end());
Ted Kremenek62f23bb2008-10-06 20:54:44 +0000213}
214
Ted Kremenek51be0562007-08-24 21:09:09 +0000215// NullStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000216Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
217Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000218
219// CompoundStmt
220Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek0c97e042009-02-07 01:47:29 +0000221Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000222
Ted Kremeneke07b67e2007-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 Kremenek51be0562007-08-24 21:09:09 +0000230
231// LabelStmt
232Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000233Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-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 Kremenek42730c52008-01-07 19:49:32 +0000255// ObjCForCollectionStmt
Nico Weber7340a2f2008-08-05 23:15:29 +0000256Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
257 return &SubExprs[0];
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000258}
Nico Weber7340a2f2008-08-05 23:15:29 +0000259Stmt::child_iterator ObjCForCollectionStmt::child_end() {
260 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000261}
262
Ted Kremenek51be0562007-08-24 21:09:09 +0000263// GotoStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000264Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
265Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000266
267// IndirectGotoStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000268Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
269const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000270
Ted Kremenek156714e2008-06-17 03:11:08 +0000271Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
272Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000273
274// ContinueStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000275Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
276Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000277
278// BreakStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000279Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
280Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000281
282// ReturnStmt
Ted Kremenek156714e2008-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 Kremenek51be0562007-08-24 21:09:09 +0000288}
289
Ted Kremenek156714e2008-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 Kremenek8a66ed42007-08-27 20:58:16 +0000295}
Ted Kremenek51be0562007-08-24 21:09:09 +0000296
Chris Lattner8a40a832007-10-29 04:04:16 +0000297// AsmStmt
Ted Kremenekb30de272008-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 Lattner8a40a832007-10-29 04:04:16 +0000304
Ted Kremenek42730c52008-01-07 19:49:32 +0000305// ObjCAtCatchStmt
306Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000307Stmt::child_iterator ObjCAtCatchStmt::child_end() {
308 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000309}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000310
Ted Kremenek42730c52008-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 Jahanian70952482007-11-01 21:12:44 +0000314
Ted Kremenek42730c52008-01-07 19:49:32 +0000315// ObjCAtTryStmt
316Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000317Stmt::child_iterator ObjCAtTryStmt::child_end() {
318 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000319}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000320
Ted Kremenek42730c52008-01-07 19:49:32 +0000321// ObjCAtThrowStmt
322Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000323 return &Throw;
324}
325
Ted Kremenek42730c52008-01-07 19:49:32 +0000326Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000327 return &Throw+1;
328}
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000329
330// ObjCAtSynchronizedStmt
331Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000332 return &SubStmts[0];
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000333}
334
335Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000336 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000337}
338
Sebastian Redl743c8162008-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 Redl237116b2008-12-22 21:35:02 +0000355 if (ExceptionDecl)
356 ExceptionDecl->Destroy(C);
Sebastian Redl743c8162008-12-22 19:15:10 +0000357 Stmt::Destroy(C);
358}
Sebastian Redl237116b2008-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}