blob: 079ed4e0daca4edbbd6d836f0602e5d4e7aefb93 [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) {
Ted Kremenek1ed5dc62008-05-19 22:02:12 +000046 for (child_iterator I = child_begin(), E = child_end(); I !=E; ++I)
Ted Kremenekafdf8112008-05-20 00:43:19 +000047 if (Stmt* Child = *I) Child->Destroy(C);
48}
49
50void Stmt::Destroy(ASTContext& C) {
51 DestroyChildren(C);
Ted Kremenekbde41092008-05-20 04:10:52 +000052 // FIXME: Eventually all Stmts should be allocated with the allocator
53 // in ASTContext, just like with Decls.
54 // this->~Stmt();
55 delete this;
Ted Kremenek1ed5dc62008-05-19 22:02:12 +000056}
57
Ted Kremeneka1e77702008-05-21 15:53:55 +000058void DeclStmt::Destroy(ASTContext& C) {
Ted Kremenek1bc18e62008-10-07 23:09:49 +000059 DG.Destroy(C);
Ted Kremenek9ecf7162008-05-21 16:00:02 +000060 delete this;
Ted Kremeneka1e77702008-05-21 15:53:55 +000061}
62
Chris Lattner4b009652007-07-25 00:24:17 +000063void Stmt::PrintStats() {
Chris Lattner603bf122007-08-25 01:42:24 +000064 // Ensure the table is primed.
65 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber7340a2f2008-08-05 23:15:29 +000066
Chris Lattner4b009652007-07-25 00:24:17 +000067 unsigned sum = 0;
68 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner9c0da3b2007-08-25 01:55:00 +000069 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000070 if (StmtClassInfo[i].Name == 0) continue;
71 sum += StmtClassInfo[i].Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000072 }
73 fprintf(stderr, " %d stmts/exprs total.\n", sum);
74 sum = 0;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000075 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000076 if (StmtClassInfo[i].Name == 0) continue;
Nico Weber7340a2f2008-08-05 23:15:29 +000077 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner603bf122007-08-25 01:42:24 +000078 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
79 StmtClassInfo[i].Size,
80 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
81 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Chris Lattner4b009652007-07-25 00:24:17 +000082 }
83 fprintf(stderr, "Total bytes = %d\n", sum);
84}
85
86void Stmt::addStmtClass(StmtClass s) {
Chris Lattner603bf122007-08-25 01:42:24 +000087 ++getStmtInfoTableEntry(s).Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000088}
89
90static bool StatSwitch = false;
91
92bool Stmt::CollectingStats(bool enable) {
93 if (enable) StatSwitch = true;
Chris Lattner603bf122007-08-25 01:42:24 +000094 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000095}
96
97
Chris Lattner4b009652007-07-25 00:24:17 +000098const char *LabelStmt::getName() const {
99 return getID()->getName();
100}
101
Steve Naroffc32a20d2007-08-31 23:49:30 +0000102// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber7340a2f2008-08-05 23:15:29 +0000103SourceRange ReturnStmt::getSourceRange() const {
Steve Naroffc32a20d2007-08-31 23:49:30 +0000104 if (RetExpr)
105 return SourceRange(RetLoc, RetExpr->getLocEnd());
106 else
107 return SourceRange(RetLoc);
108}
109
Ted Kremenekad5682f2007-10-01 16:34:52 +0000110bool Stmt::hasImplicitControlFlow() const {
111 switch (sClass) {
112 default:
113 return false;
Nico Weber7340a2f2008-08-05 23:15:29 +0000114
Ted Kremenekad5682f2007-10-01 16:34:52 +0000115 case CallExprClass:
116 case ConditionalOperatorClass:
117 case ChooseExprClass:
118 case StmtExprClass:
119 case DeclStmtClass:
Nico Weber7340a2f2008-08-05 23:15:29 +0000120 return true;
121
Ted Kremenekad5682f2007-10-01 16:34:52 +0000122 case Stmt::BinaryOperatorClass: {
123 const BinaryOperator* B = cast<BinaryOperator>(this);
124 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
125 return true;
126 else
127 return false;
128 }
129 }
130}
131
Ted Kremenekb30de272008-10-27 18:40:21 +0000132const Expr* AsmStmt::getOutputExpr(unsigned i) const {
133 return cast<Expr>(Exprs[i]);
134}
135Expr* AsmStmt::getOutputExpr(unsigned i) {
136 return cast<Expr>(Exprs[i]);
137}
138Expr* AsmStmt::getInputExpr(unsigned i) {
139 return cast<Expr>(Exprs[i + NumOutputs]);
140}
141const Expr* AsmStmt::getInputExpr(unsigned i) const {
142 return cast<Expr>(Exprs[i + NumOutputs]);
143}
144
Chris Lattner006d1542008-01-30 05:01:46 +0000145//===----------------------------------------------------------------------===//
146// Constructors
147//===----------------------------------------------------------------------===//
148
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000149AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattner006d1542008-01-30 05:01:46 +0000150 unsigned numoutputs, unsigned numinputs,
151 std::string *names, StringLiteral **constraints,
152 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
153 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlsson965d5202007-11-22 01:36:19 +0000154 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000155 , IsSimple(issimple), IsVolatile(isvolatile)
156 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson965d5202007-11-22 01:36:19 +0000157 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
158 Names.push_back(names[i]);
159 Exprs.push_back(exprs[i]);
Nico Weber7340a2f2008-08-05 23:15:29 +0000160 Constraints.push_back(constraints[i]);
Anders Carlsson965d5202007-11-22 01:36:19 +0000161 }
Nico Weber7340a2f2008-08-05 23:15:29 +0000162
Anders Carlsson965d5202007-11-22 01:36:19 +0000163 for (unsigned i = 0; i != numclobbers; i++)
164 Clobbers.push_back(clobbers[i]);
165}
166
Chris Lattner006d1542008-01-30 05:01:46 +0000167ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
168 Stmt *Body, SourceLocation FCL,
Nico Weber7340a2f2008-08-05 23:15:29 +0000169 SourceLocation RPL)
Chris Lattner006d1542008-01-30 05:01:46 +0000170: Stmt(ObjCForCollectionStmtClass) {
171 SubExprs[ELEM] = Elem;
172 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
173 SubExprs[BODY] = Body;
174 ForLoc = FCL;
175 RParenLoc = RPL;
176}
177
178
Nico Weber7340a2f2008-08-05 23:15:29 +0000179ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
180 SourceLocation rparenloc,
181 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
Chris Lattner006d1542008-01-30 05:01:46 +0000182 Stmt *atCatchList)
183: Stmt(ObjCAtCatchStmtClass) {
184 SubExprs[SELECTOR] = catchVarStmtDecl;
185 SubExprs[BODY] = atCatchStmt;
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000186 SubExprs[NEXT_CATCH] = NULL;
187 if (atCatchList) {
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000188 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
189
Nico Weber7340a2f2008-08-05 23:15:29 +0000190 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000191 AtCatchList = NextCatch;
Nico Weber7340a2f2008-08-05 23:15:29 +0000192
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000193 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner006d1542008-01-30 05:01:46 +0000194 }
195 AtCatchLoc = atCatchLoc;
196 RParenLoc = rparenloc;
197}
198
199
Ted Kremenek51be0562007-08-24 21:09:09 +0000200//===----------------------------------------------------------------------===//
201// Child Iterators for iterating over subexpressions/substatements
202//===----------------------------------------------------------------------===//
203
204// DeclStmt
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000205Stmt::child_iterator DeclStmt::child_begin() {
206 return StmtIterator(DG.begin(), DG.end());
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000207}
208
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000209Stmt::child_iterator DeclStmt::child_end() {
210 return StmtIterator(DG.end(), DG.end());
Ted Kremenek62f23bb2008-10-06 20:54:44 +0000211}
212
Ted Kremenek51be0562007-08-24 21:09:09 +0000213// NullStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000214Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
215Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000216
217// CompoundStmt
218Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
219Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
220
Ted Kremeneke07b67e2007-08-30 16:50:46 +0000221// CaseStmt
222Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
223Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
224
225// DefaultStmt
226Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
227Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000228
229// LabelStmt
230Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000231Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000232
233// IfStmt
234Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
235Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
236
237// SwitchStmt
238Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
239Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
240
241// WhileStmt
242Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
243Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
244
245// DoStmt
246Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
247Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
248
249// ForStmt
250Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
251Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
252
Ted Kremenek42730c52008-01-07 19:49:32 +0000253// ObjCForCollectionStmt
Nico Weber7340a2f2008-08-05 23:15:29 +0000254Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
255 return &SubExprs[0];
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000256}
Nico Weber7340a2f2008-08-05 23:15:29 +0000257Stmt::child_iterator ObjCForCollectionStmt::child_end() {
258 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000259}
260
Ted Kremenek51be0562007-08-24 21:09:09 +0000261// GotoStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000262Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
263Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000264
265// IndirectGotoStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000266Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
267const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000268
Ted Kremenek156714e2008-06-17 03:11:08 +0000269Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
270Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000271
272// ContinueStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000273Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
274Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000275
276// BreakStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000277Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
278Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000279
280// ReturnStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000281const Expr* ReturnStmt::getRetValue() const {
282 return cast_or_null<Expr>(RetExpr);
283}
284Expr* ReturnStmt::getRetValue() {
285 return cast_or_null<Expr>(RetExpr);
Ted Kremenek51be0562007-08-24 21:09:09 +0000286}
287
Ted Kremenek156714e2008-06-17 03:11:08 +0000288Stmt::child_iterator ReturnStmt::child_begin() {
289 return &RetExpr;
290}
291Stmt::child_iterator ReturnStmt::child_end() {
292 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000293}
Ted Kremenek51be0562007-08-24 21:09:09 +0000294
Chris Lattner8a40a832007-10-29 04:04:16 +0000295// AsmStmt
Ted Kremenekb30de272008-10-27 18:40:21 +0000296Stmt::child_iterator AsmStmt::child_begin() {
297 return Exprs.empty() ? 0 : &Exprs[0];
298}
299Stmt::child_iterator AsmStmt::child_end() {
300 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
301}
Chris Lattner8a40a832007-10-29 04:04:16 +0000302
Ted Kremenek42730c52008-01-07 19:49:32 +0000303// ObjCAtCatchStmt
304Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000305Stmt::child_iterator ObjCAtCatchStmt::child_end() {
306 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000307}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000308
Ted Kremenek42730c52008-01-07 19:49:32 +0000309// ObjCAtFinallyStmt
310Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
311Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000312
Ted Kremenek42730c52008-01-07 19:49:32 +0000313// ObjCAtTryStmt
314Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000315Stmt::child_iterator ObjCAtTryStmt::child_end() {
316 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000317}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000318
Ted Kremenek42730c52008-01-07 19:49:32 +0000319// ObjCAtThrowStmt
320Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000321 return &Throw;
322}
323
Ted Kremenek42730c52008-01-07 19:49:32 +0000324Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000325 return &Throw+1;
326}
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000327
328// ObjCAtSynchronizedStmt
329Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000330 return &SubStmts[0];
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000331}
332
333Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000334 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000335}
336
Sebastian Redl743c8162008-12-22 19:15:10 +0000337// CXXCatchStmt
338Stmt::child_iterator CXXCatchStmt::child_begin() {
339 return &HandlerBlock;
340}
341
342Stmt::child_iterator CXXCatchStmt::child_end() {
343 return &HandlerBlock + 1;
344}
345
346QualType CXXCatchStmt::getCaughtType() {
347 if (ExceptionDecl)
348 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
349 return QualType();
350}
351
352void CXXCatchStmt::Destroy(ASTContext& C) {
353 ExceptionDecl->Destroy(C);
354 Stmt::Destroy(C);
355}