blob: 572280bc05456aadae6d4c471d78410e744bef06 [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"
16#include "clang/AST/StmtVisitor.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000021 const char *Name;
22 unsigned Counter;
23 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000024} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-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;
33#define STMT(N, CLASS, PARENT) \
34 StmtClassInfo[N].Name = #CLASS; \
35 StmtClassInfo[N].Size = sizeof(CLASS);
Reid Spencer5f016e22007-07-11 17:01:13 +000036#include "clang/AST/StmtNodes.def"
Reid Spencer5f016e22007-07-11 17:01:13 +000037
Chris Lattner63381352007-08-25 01:42:24 +000038 return StmtClassInfo[E];
39}
40
Reid Spencer5f016e22007-07-11 17:01:13 +000041const char *Stmt::getStmtClassName() const {
Chris Lattner63381352007-08-25 01:42:24 +000042 return getStmtInfoTableEntry(sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000043}
44
45void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000046 // Ensure the table is primed.
47 getStmtInfoTableEntry(Stmt::NullStmtClass);
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049 unsigned sum = 0;
50 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000051 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000052 if (StmtClassInfo[i].Name == 0) continue;
53 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000054 }
55 fprintf(stderr, " %d stmts/exprs total.\n", sum);
56 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000057 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000058 if (StmtClassInfo[i].Name == 0) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000060 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
61 StmtClassInfo[i].Size,
62 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
63 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000064 }
65 fprintf(stderr, "Total bytes = %d\n", sum);
66}
67
68void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000069 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000070}
71
72static bool StatSwitch = false;
73
74bool Stmt::CollectingStats(bool enable) {
75 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000076 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000077}
78
79
Reid Spencer5f016e22007-07-11 17:01:13 +000080const char *LabelStmt::getName() const {
81 return getID()->getName();
82}
83
Steve Naroff507f2d52007-08-31 23:49:30 +000084// This is defined here to avoid polluting Stmt.h with importing Expr.h
85SourceRange ReturnStmt::getSourceRange() const {
86 if (RetExpr)
87 return SourceRange(RetLoc, RetExpr->getLocEnd());
88 else
89 return SourceRange(RetLoc);
90}
91
Ted Kremenekd48ade62007-10-01 16:34:52 +000092bool Stmt::hasImplicitControlFlow() const {
93 switch (sClass) {
94 default:
95 return false;
96
97 case CallExprClass:
98 case ConditionalOperatorClass:
99 case ChooseExprClass:
100 case StmtExprClass:
101 case DeclStmtClass:
102 return true;
103
104 case Stmt::BinaryOperatorClass: {
105 const BinaryOperator* B = cast<BinaryOperator>(this);
106 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
107 return true;
108 else
109 return false;
110 }
111 }
112}
113
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000114//===----------------------------------------------------------------------===//
115// Constructors
116//===----------------------------------------------------------------------===//
117
Anders Carlssondfab34a2008-02-05 23:03:50 +0000118AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000119 unsigned numoutputs, unsigned numinputs,
120 std::string *names, StringLiteral **constraints,
121 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
122 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000123 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000124 , IsSimple(issimple), IsVolatile(isvolatile)
125 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000126 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
127 Names.push_back(names[i]);
128 Exprs.push_back(exprs[i]);
129 Constraints.push_back(constraints[i]);
130 }
131
132 for (unsigned i = 0; i != numclobbers; i++)
133 Clobbers.push_back(clobbers[i]);
134}
135
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000136ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
137 Stmt *Body, SourceLocation FCL,
138 SourceLocation RPL)
139: Stmt(ObjCForCollectionStmtClass) {
140 SubExprs[ELEM] = Elem;
141 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
142 SubExprs[BODY] = Body;
143 ForLoc = FCL;
144 RParenLoc = RPL;
145}
146
147
148ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
149 SourceLocation rparenloc,
150 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
151 Stmt *atCatchList)
152: Stmt(ObjCAtCatchStmtClass) {
153 SubExprs[SELECTOR] = catchVarStmtDecl;
154 SubExprs[BODY] = atCatchStmt;
155 if (!atCatchList)
Ted Kremenekff981022008-02-01 21:28:59 +0000156 SubExprs[NEXT_CATCH] = NULL;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000157 else {
Ted Kremenekff981022008-02-01 21:28:59 +0000158 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
159
160 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
161 AtCatchList = NextCatch;
162
163 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000164 }
165 AtCatchLoc = atCatchLoc;
166 RParenLoc = rparenloc;
167}
168
169
Ted Kremenek82977772007-08-24 21:09:09 +0000170//===----------------------------------------------------------------------===//
171// Child Iterators for iterating over subexpressions/substatements
172//===----------------------------------------------------------------------===//
173
174// DeclStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000175Stmt::child_iterator DeclStmt::child_begin() { return getDecl(); }
176Stmt::child_iterator DeclStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000177
178// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000179Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
180Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000181
182// CompoundStmt
183Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
184Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
185
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000186// CaseStmt
187Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
188Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
189
190// DefaultStmt
191Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
192Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000193
194// LabelStmt
195Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000196Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000197
198// IfStmt
199Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
200Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
201
202// SwitchStmt
203Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
204Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
205
206// WhileStmt
207Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
208Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
209
210// DoStmt
211Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
212Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
213
214// ForStmt
215Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
216Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
217
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000218// ObjCForCollectionStmt
219Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000220 return &SubExprs[0];
221}
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000222Stmt::child_iterator ObjCForCollectionStmt::child_end() {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000223 return &SubExprs[0]+END_EXPR;
224}
225
Ted Kremenek82977772007-08-24 21:09:09 +0000226// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000227Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
228Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000229
230// IndirectGotoStmt
231Stmt::child_iterator IndirectGotoStmt::child_begin() {
232 return reinterpret_cast<Stmt**>(&Target);
233}
234
Ted Kremenek9caf8b12007-10-18 00:24:38 +0000235Stmt::child_iterator IndirectGotoStmt::child_end() { return ++child_begin(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000236
237// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000238Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
239Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000240
241// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000242Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
243Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000244
245// ReturnStmt
Ted Kremenek2298f912007-08-27 20:58:16 +0000246Stmt::child_iterator ReturnStmt::child_begin() {
247 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr);
Ted Kremenek9ac59282007-10-18 23:28:49 +0000248 else return child_iterator();
Ted Kremenek82977772007-08-24 21:09:09 +0000249}
250
Ted Kremenek2298f912007-08-27 20:58:16 +0000251Stmt::child_iterator ReturnStmt::child_end() {
252 if (RetExpr) return reinterpret_cast<Stmt**>(&RetExpr)+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +0000253 else return child_iterator();
Ted Kremenek2298f912007-08-27 20:58:16 +0000254}
Ted Kremenek82977772007-08-24 21:09:09 +0000255
Chris Lattnerfe795952007-10-29 04:04:16 +0000256// AsmStmt
257Stmt::child_iterator AsmStmt::child_begin() { return child_iterator(); }
258Stmt::child_iterator AsmStmt::child_end() { return child_iterator(); }
259
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000260// ObjCAtCatchStmt
261Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
262Stmt::child_iterator ObjCAtCatchStmt::child_end() {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000263 return &SubExprs[0]+END_EXPR;
264}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000265
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000266// ObjCAtFinallyStmt
267Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
268Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000269
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000270// ObjCAtTryStmt
271Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
272Stmt::child_iterator ObjCAtTryStmt::child_end() {
Fariborz Jahanian89079ea2007-11-07 17:43:16 +0000273 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000274}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000275
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000276// ObjCAtThrowStmt
277Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000278 return &Throw;
279}
280
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000281Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000282 return &Throw+1;
283}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000284
285// ObjCAtSynchronizedStmt
286Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000287 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000288}
289
290Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000291 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000292}
293