blob: 541bb0401c1e1a2af6f8ed09526c7aaa4b827574 [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"
Sebastian Redl4b07b292008-12-22 19:15:10 +000017#include "clang/AST/Type.h"
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000018#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Reid Spencer5f016e22007-07-11 17:01:13 +000021static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000022 const char *Name;
23 unsigned Counter;
24 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000025} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-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 Gregorf2cad862008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +000037#include "clang/AST/StmtNodes.def"
Nico Weber608b17f2008-08-05 23:15:29 +000038
Chris Lattner63381352007-08-25 01:42:24 +000039 return StmtClassInfo[E];
40}
41
Reid Spencer5f016e22007-07-11 17:01:13 +000042const char *Stmt::getStmtClassName() const {
Chris Lattner63381352007-08-25 01:42:24 +000043 return getStmtInfoTableEntry(sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000044}
45
Ted Kremenek27f8a282008-05-20 00:43:19 +000046void Stmt::DestroyChildren(ASTContext& C) {
Douglas Gregor860f6d42009-01-16 06:50:08 +000047 for (child_iterator I = child_begin(), E = child_end(); I !=E; ) {
48 if (Stmt* Child = *I++) Child->Destroy(C);
49 }
Ted Kremenek27f8a282008-05-20 00:43:19 +000050}
51
52void Stmt::Destroy(ASTContext& C) {
53 DestroyChildren(C);
Ted Kremenekf809e3b2008-05-20 04:10:52 +000054 // FIXME: Eventually all Stmts should be allocated with the allocator
55 // in ASTContext, just like with Decls.
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000056 this->~Stmt();
57 C.Deallocate((void *)this);
Ted Kremenek9c1863e2008-05-19 22:02:12 +000058}
59
Ted Kremenek8e355f22008-05-21 15:53:55 +000060void DeclStmt::Destroy(ASTContext& C) {
Ted Kremenek8ffb1592008-10-07 23:09:49 +000061 DG.Destroy(C);
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000062 this->~DeclStmt();
63 C.Deallocate((void *)this);
Ted Kremenek8e355f22008-05-21 15:53:55 +000064}
65
Reid Spencer5f016e22007-07-11 17:01:13 +000066void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000067 // Ensure the table is primed.
68 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000069
Reid Spencer5f016e22007-07-11 17:01:13 +000070 unsigned sum = 0;
71 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000072 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000073 if (StmtClassInfo[i].Name == 0) continue;
74 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000075 }
76 fprintf(stderr, " %d stmts/exprs total.\n", sum);
77 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000078 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000079 if (StmtClassInfo[i].Name == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000080 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000081 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
82 StmtClassInfo[i].Size,
83 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
84 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000085 }
86 fprintf(stderr, "Total bytes = %d\n", sum);
87}
88
89void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000090 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000091}
92
93static bool StatSwitch = false;
94
95bool Stmt::CollectingStats(bool enable) {
96 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000097 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000098}
99
100
Reid Spencer5f016e22007-07-11 17:01:13 +0000101const char *LabelStmt::getName() const {
102 return getID()->getName();
103}
104
Steve Naroff507f2d52007-08-31 23:49:30 +0000105// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000106SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000107 if (RetExpr)
108 return SourceRange(RetLoc, RetExpr->getLocEnd());
109 else
110 return SourceRange(RetLoc);
111}
112
Ted Kremenekd48ade62007-10-01 16:34:52 +0000113bool Stmt::hasImplicitControlFlow() const {
114 switch (sClass) {
115 default:
116 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000117
Ted Kremenekd48ade62007-10-01 16:34:52 +0000118 case CallExprClass:
119 case ConditionalOperatorClass:
120 case ChooseExprClass:
121 case StmtExprClass:
122 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000123 return true;
124
Ted Kremenekd48ade62007-10-01 16:34:52 +0000125 case Stmt::BinaryOperatorClass: {
126 const BinaryOperator* B = cast<BinaryOperator>(this);
127 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
128 return true;
129 else
130 return false;
131 }
132 }
133}
134
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000135const Expr* AsmStmt::getOutputExpr(unsigned i) const {
136 return cast<Expr>(Exprs[i]);
137}
138Expr* AsmStmt::getOutputExpr(unsigned i) {
139 return cast<Expr>(Exprs[i]);
140}
141Expr* AsmStmt::getInputExpr(unsigned i) {
142 return cast<Expr>(Exprs[i + NumOutputs]);
143}
144const Expr* AsmStmt::getInputExpr(unsigned i) const {
145 return cast<Expr>(Exprs[i + NumOutputs]);
146}
147
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000148//===----------------------------------------------------------------------===//
149// Constructors
150//===----------------------------------------------------------------------===//
151
Anders Carlssondfab34a2008-02-05 23:03:50 +0000152AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000153 unsigned numoutputs, unsigned numinputs,
154 std::string *names, StringLiteral **constraints,
155 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
156 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000157 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000158 , IsSimple(issimple), IsVolatile(isvolatile)
159 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000160 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
161 Names.push_back(names[i]);
162 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000163 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000164 }
Nico Weber608b17f2008-08-05 23:15:29 +0000165
Anders Carlssonb235fc22007-11-22 01:36:19 +0000166 for (unsigned i = 0; i != numclobbers; i++)
167 Clobbers.push_back(clobbers[i]);
168}
169
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000170ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
171 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000172 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000173: Stmt(ObjCForCollectionStmtClass) {
174 SubExprs[ELEM] = Elem;
175 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
176 SubExprs[BODY] = Body;
177 ForLoc = FCL;
178 RParenLoc = RPL;
179}
180
181
Nico Weber608b17f2008-08-05 23:15:29 +0000182ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
183 SourceLocation rparenloc,
184 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000185 Stmt *atCatchList)
186: Stmt(ObjCAtCatchStmtClass) {
187 SubExprs[SELECTOR] = catchVarStmtDecl;
188 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000189 SubExprs[NEXT_CATCH] = NULL;
190 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000191 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
192
Nico Weber608b17f2008-08-05 23:15:29 +0000193 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000194 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000195
Ted Kremenekff981022008-02-01 21:28:59 +0000196 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000197 }
198 AtCatchLoc = atCatchLoc;
199 RParenLoc = rparenloc;
200}
201
202
Ted Kremenek82977772007-08-24 21:09:09 +0000203//===----------------------------------------------------------------------===//
204// Child Iterators for iterating over subexpressions/substatements
205//===----------------------------------------------------------------------===//
206
207// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000208Stmt::child_iterator DeclStmt::child_begin() {
209 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000210}
211
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000212Stmt::child_iterator DeclStmt::child_end() {
213 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000214}
215
Ted Kremenek82977772007-08-24 21:09:09 +0000216// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000217Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
218Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000219
220// CompoundStmt
221Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
222Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+Body.size(); }
223
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000224// CaseStmt
225Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
226Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
227
228// DefaultStmt
229Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
230Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000231
232// LabelStmt
233Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000234Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000235
236// IfStmt
237Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
238Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
239
240// SwitchStmt
241Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
242Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
243
244// WhileStmt
245Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
246Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
247
248// DoStmt
249Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
250Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
251
252// ForStmt
253Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
254Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
255
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000256// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000257Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
258 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000259}
Nico Weber608b17f2008-08-05 23:15:29 +0000260Stmt::child_iterator ObjCForCollectionStmt::child_end() {
261 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000262}
263
Ted Kremenek82977772007-08-24 21:09:09 +0000264// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000265Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
266Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000267
268// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000269Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
270const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000271
Ted Kremenek1060aff2008-06-17 03:11:08 +0000272Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
273Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000274
275// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000276Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
277Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000278
279// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000280Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
281Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000282
283// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000284const Expr* ReturnStmt::getRetValue() const {
285 return cast_or_null<Expr>(RetExpr);
286}
287Expr* ReturnStmt::getRetValue() {
288 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000289}
290
Ted Kremenek1060aff2008-06-17 03:11:08 +0000291Stmt::child_iterator ReturnStmt::child_begin() {
292 return &RetExpr;
293}
294Stmt::child_iterator ReturnStmt::child_end() {
295 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000296}
Ted Kremenek82977772007-08-24 21:09:09 +0000297
Chris Lattnerfe795952007-10-29 04:04:16 +0000298// AsmStmt
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000299Stmt::child_iterator AsmStmt::child_begin() {
300 return Exprs.empty() ? 0 : &Exprs[0];
301}
302Stmt::child_iterator AsmStmt::child_end() {
303 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
304}
Chris Lattnerfe795952007-10-29 04:04:16 +0000305
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000306// ObjCAtCatchStmt
307Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000308Stmt::child_iterator ObjCAtCatchStmt::child_end() {
309 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000310}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000311
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000312// ObjCAtFinallyStmt
313Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
314Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000315
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000316// ObjCAtTryStmt
317Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000318Stmt::child_iterator ObjCAtTryStmt::child_end() {
319 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000320}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000321
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000322// ObjCAtThrowStmt
323Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000324 return &Throw;
325}
326
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000327Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000328 return &Throw+1;
329}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000330
331// ObjCAtSynchronizedStmt
332Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000333 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000334}
335
336Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000337 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000338}
339
Sebastian Redl4b07b292008-12-22 19:15:10 +0000340// CXXCatchStmt
341Stmt::child_iterator CXXCatchStmt::child_begin() {
342 return &HandlerBlock;
343}
344
345Stmt::child_iterator CXXCatchStmt::child_end() {
346 return &HandlerBlock + 1;
347}
348
349QualType CXXCatchStmt::getCaughtType() {
350 if (ExceptionDecl)
351 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
352 return QualType();
353}
354
355void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000356 if (ExceptionDecl)
357 ExceptionDecl->Destroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000358 Stmt::Destroy(C);
359}
Sebastian Redl8351da02008-12-22 21:35:02 +0000360
361// CXXTryStmt
362Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
363Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
364
365CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
366 Stmt **handlers, unsigned numHandlers)
367 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
368 Stmts.push_back(tryBlock);
369 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
370}