blob: 9c06807b3b9e08217bff9c224095fc88a70ab8ef [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) {
Ted Kremenek8ffb1592008-10-07 23:09:49 +000058 DG.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
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000131const Expr* AsmStmt::getOutputExpr(unsigned i) const {
132 return cast<Expr>(Exprs[i]);
133}
134Expr* AsmStmt::getOutputExpr(unsigned i) {
135 return cast<Expr>(Exprs[i]);
136}
137Expr* AsmStmt::getInputExpr(unsigned i) {
138 return cast<Expr>(Exprs[i + NumOutputs]);
139}
140const Expr* AsmStmt::getInputExpr(unsigned i) const {
141 return cast<Expr>(Exprs[i + NumOutputs]);
142}
143
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000144//===----------------------------------------------------------------------===//
145// Constructors
146//===----------------------------------------------------------------------===//
147
Anders Carlssondfab34a2008-02-05 23:03:50 +0000148AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000149 unsigned numoutputs, unsigned numinputs,
150 std::string *names, StringLiteral **constraints,
151 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
152 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000153 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000154 , IsSimple(issimple), IsVolatile(isvolatile)
155 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000156 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
157 Names.push_back(names[i]);
158 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000159 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000160 }
Nico Weber608b17f2008-08-05 23:15:29 +0000161
Anders Carlssonb235fc22007-11-22 01:36:19 +0000162 for (unsigned i = 0; i != numclobbers; i++)
163 Clobbers.push_back(clobbers[i]);
164}
165
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000166ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
167 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000168 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000169: Stmt(ObjCForCollectionStmtClass) {
170 SubExprs[ELEM] = Elem;
171 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
172 SubExprs[BODY] = Body;
173 ForLoc = FCL;
174 RParenLoc = RPL;
175}
176
177
Nico Weber608b17f2008-08-05 23:15:29 +0000178ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
179 SourceLocation rparenloc,
180 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000181 Stmt *atCatchList)
182: Stmt(ObjCAtCatchStmtClass) {
183 SubExprs[SELECTOR] = catchVarStmtDecl;
184 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000185 SubExprs[NEXT_CATCH] = NULL;
186 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000187 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
188
Nico Weber608b17f2008-08-05 23:15:29 +0000189 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000190 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000191
Ted Kremenekff981022008-02-01 21:28:59 +0000192 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000193 }
194 AtCatchLoc = atCatchLoc;
195 RParenLoc = rparenloc;
196}
197
198
Ted Kremenek82977772007-08-24 21:09:09 +0000199//===----------------------------------------------------------------------===//
200// Child Iterators for iterating over subexpressions/substatements
201//===----------------------------------------------------------------------===//
202
203// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000204Stmt::child_iterator DeclStmt::child_begin() {
205 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000206}
207
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000208Stmt::child_iterator DeclStmt::child_end() {
209 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000210}
211
Ted Kremenek82977772007-08-24 21:09:09 +0000212// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000213Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
214Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000215
216// CompoundStmt
217Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
218Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
219
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000220// CaseStmt
221Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
222Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
223
224// DefaultStmt
225Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
226Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000227
228// LabelStmt
229Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000230Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000231
232// IfStmt
233Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
234Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
235
236// SwitchStmt
237Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
238Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
239
240// WhileStmt
241Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
242Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
243
244// DoStmt
245Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
246Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
247
248// ForStmt
249Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
250Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
251
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000252// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000253Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
254 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000255}
Nico Weber608b17f2008-08-05 23:15:29 +0000256Stmt::child_iterator ObjCForCollectionStmt::child_end() {
257 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000258}
259
Ted Kremenek82977772007-08-24 21:09:09 +0000260// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000261Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
262Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000263
264// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000265Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
266const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000267
Ted Kremenek1060aff2008-06-17 03:11:08 +0000268Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
269Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000270
271// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000272Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
273Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000274
275// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000276Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
277Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000278
279// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000280const Expr* ReturnStmt::getRetValue() const {
281 return cast_or_null<Expr>(RetExpr);
282}
283Expr* ReturnStmt::getRetValue() {
284 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000285}
286
Ted Kremenek1060aff2008-06-17 03:11:08 +0000287Stmt::child_iterator ReturnStmt::child_begin() {
288 return &RetExpr;
289}
290Stmt::child_iterator ReturnStmt::child_end() {
291 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000292}
Ted Kremenek82977772007-08-24 21:09:09 +0000293
Chris Lattnerfe795952007-10-29 04:04:16 +0000294// AsmStmt
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000295Stmt::child_iterator AsmStmt::child_begin() {
296 return Exprs.empty() ? 0 : &Exprs[0];
297}
298Stmt::child_iterator AsmStmt::child_end() {
299 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
300}
Chris Lattnerfe795952007-10-29 04:04:16 +0000301
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000302// ObjCAtCatchStmt
303Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000304Stmt::child_iterator ObjCAtCatchStmt::child_end() {
305 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000306}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000307
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000308// ObjCAtFinallyStmt
309Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
310Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000311
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000312// ObjCAtTryStmt
313Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000314Stmt::child_iterator ObjCAtTryStmt::child_end() {
315 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000316}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000317
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000318// ObjCAtThrowStmt
319Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000320 return &Throw;
321}
322
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000323Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000324 return &Throw+1;
325}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000326
327// ObjCAtSynchronizedStmt
328Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000329 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000330}
331
332Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000333 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000334}
335