blob: 74ed6e92c6697f6b587b271c5759247976ba997f [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"
Chris Lattner4b009652007-07-25 00:24:17 +000018using namespace clang;
19
Chris Lattner4b009652007-07-25 00:24:17 +000020static struct StmtClassNameTable {
Chris Lattner603bf122007-08-25 01:42:24 +000021 const char *Name;
22 unsigned Counter;
23 unsigned Size;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000024} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner603bf122007-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;
Douglas Gregor19330152008-11-14 12:46:07 +000033#define STMT(CLASS, PARENT) \
34 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
35 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Chris Lattner4b009652007-07-25 00:24:17 +000036#include "clang/AST/StmtNodes.def"
Nico Weber7340a2f2008-08-05 23:15:29 +000037
Chris Lattner603bf122007-08-25 01:42:24 +000038 return StmtClassInfo[E];
39}
40
Chris Lattner4b009652007-07-25 00:24:17 +000041const char *Stmt::getStmtClassName() const {
Chris Lattner603bf122007-08-25 01:42:24 +000042 return getStmtInfoTableEntry(sClass).Name;
Chris Lattner4b009652007-07-25 00:24:17 +000043}
44
Ted Kremenekafdf8112008-05-20 00:43:19 +000045void Stmt::DestroyChildren(ASTContext& C) {
Douglas Gregor0e7e7f42009-01-16 06:50:08 +000046 for (child_iterator I = child_begin(), E = child_end(); I !=E; ) {
47 if (Stmt* Child = *I++) Child->Destroy(C);
48 }
Ted Kremenekafdf8112008-05-20 00:43:19 +000049}
50
51void Stmt::Destroy(ASTContext& C) {
52 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.
55 // this->~Stmt();
56 delete this;
Ted Kremenek1ed5dc62008-05-19 22:02:12 +000057}
58
Ted Kremeneka1e77702008-05-21 15:53:55 +000059void DeclStmt::Destroy(ASTContext& C) {
Ted Kremenek1bc18e62008-10-07 23:09:49 +000060 DG.Destroy(C);
Ted Kremenek9ecf7162008-05-21 16:00:02 +000061 delete 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,
182 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
Chris Lattner006d1542008-01-30 05:01:46 +0000183 Stmt *atCatchList)
184: Stmt(ObjCAtCatchStmtClass) {
185 SubExprs[SELECTOR] = catchVarStmtDecl;
186 SubExprs[BODY] = atCatchStmt;
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000187 SubExprs[NEXT_CATCH] = NULL;
188 if (atCatchList) {
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000189 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
190
Nico Weber7340a2f2008-08-05 23:15:29 +0000191 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000192 AtCatchList = NextCatch;
Nico Weber7340a2f2008-08-05 23:15:29 +0000193
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000194 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner006d1542008-01-30 05:01:46 +0000195 }
196 AtCatchLoc = atCatchLoc;
197 RParenLoc = rparenloc;
198}
199
200
Ted Kremenek51be0562007-08-24 21:09:09 +0000201//===----------------------------------------------------------------------===//
202// Child Iterators for iterating over subexpressions/substatements
203//===----------------------------------------------------------------------===//
204
205// DeclStmt
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000206Stmt::child_iterator DeclStmt::child_begin() {
207 return StmtIterator(DG.begin(), DG.end());
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000208}
209
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000210Stmt::child_iterator DeclStmt::child_end() {
211 return StmtIterator(DG.end(), DG.end());
Ted Kremenek62f23bb2008-10-06 20:54:44 +0000212}
213
Ted Kremenek51be0562007-08-24 21:09:09 +0000214// NullStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000215Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
216Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000217
218// CompoundStmt
219Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
220Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
221
Ted Kremeneke07b67e2007-08-30 16:50:46 +0000222// CaseStmt
223Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
224Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
225
226// DefaultStmt
227Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
228Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000229
230// LabelStmt
231Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000232Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000233
234// IfStmt
235Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
236Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
237
238// SwitchStmt
239Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
240Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
241
242// WhileStmt
243Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
244Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
245
246// DoStmt
247Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
248Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
249
250// ForStmt
251Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
252Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
253
Ted Kremenek42730c52008-01-07 19:49:32 +0000254// ObjCForCollectionStmt
Nico Weber7340a2f2008-08-05 23:15:29 +0000255Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
256 return &SubExprs[0];
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000257}
Nico Weber7340a2f2008-08-05 23:15:29 +0000258Stmt::child_iterator ObjCForCollectionStmt::child_end() {
259 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000260}
261
Ted Kremenek51be0562007-08-24 21:09:09 +0000262// GotoStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000263Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
264Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000265
266// IndirectGotoStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000267Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
268const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000269
Ted Kremenek156714e2008-06-17 03:11:08 +0000270Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
271Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000272
273// ContinueStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000274Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
275Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000276
277// BreakStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000278Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
279Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000280
281// ReturnStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000282const Expr* ReturnStmt::getRetValue() const {
283 return cast_or_null<Expr>(RetExpr);
284}
285Expr* ReturnStmt::getRetValue() {
286 return cast_or_null<Expr>(RetExpr);
Ted Kremenek51be0562007-08-24 21:09:09 +0000287}
288
Ted Kremenek156714e2008-06-17 03:11:08 +0000289Stmt::child_iterator ReturnStmt::child_begin() {
290 return &RetExpr;
291}
292Stmt::child_iterator ReturnStmt::child_end() {
293 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000294}
Ted Kremenek51be0562007-08-24 21:09:09 +0000295
Chris Lattner8a40a832007-10-29 04:04:16 +0000296// AsmStmt
Ted Kremenekb30de272008-10-27 18:40:21 +0000297Stmt::child_iterator AsmStmt::child_begin() {
298 return Exprs.empty() ? 0 : &Exprs[0];
299}
300Stmt::child_iterator AsmStmt::child_end() {
301 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
302}
Chris Lattner8a40a832007-10-29 04:04:16 +0000303
Ted Kremenek42730c52008-01-07 19:49:32 +0000304// ObjCAtCatchStmt
305Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000306Stmt::child_iterator ObjCAtCatchStmt::child_end() {
307 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000308}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000309
Ted Kremenek42730c52008-01-07 19:49:32 +0000310// ObjCAtFinallyStmt
311Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
312Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000313
Ted Kremenek42730c52008-01-07 19:49:32 +0000314// ObjCAtTryStmt
315Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000316Stmt::child_iterator ObjCAtTryStmt::child_end() {
317 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000318}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000319
Ted Kremenek42730c52008-01-07 19:49:32 +0000320// ObjCAtThrowStmt
321Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000322 return &Throw;
323}
324
Ted Kremenek42730c52008-01-07 19:49:32 +0000325Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000326 return &Throw+1;
327}
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000328
329// ObjCAtSynchronizedStmt
330Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000331 return &SubStmts[0];
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000332}
333
334Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000335 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000336}
337
Sebastian Redl743c8162008-12-22 19:15:10 +0000338// CXXCatchStmt
339Stmt::child_iterator CXXCatchStmt::child_begin() {
340 return &HandlerBlock;
341}
342
343Stmt::child_iterator CXXCatchStmt::child_end() {
344 return &HandlerBlock + 1;
345}
346
347QualType CXXCatchStmt::getCaughtType() {
348 if (ExceptionDecl)
349 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
350 return QualType();
351}
352
353void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl237116b2008-12-22 21:35:02 +0000354 if (ExceptionDecl)
355 ExceptionDecl->Destroy(C);
Sebastian Redl743c8162008-12-22 19:15:10 +0000356 Stmt::Destroy(C);
357}
Sebastian Redl237116b2008-12-22 21:35:02 +0000358
359// CXXTryStmt
360Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
361Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
362
363CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
364 Stmt **handlers, unsigned numHandlers)
365 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
366 Stmts.push_back(tryBlock);
367 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
368}