blob: e987d84c23182c54b581dbcd4f84662648857cf6 [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"
Ted Kremenek39dc20a2009-02-06 01:42:09 +000018#include "clang/AST/ASTContext.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattner4b009652007-07-25 00:24:17 +000021static struct StmtClassNameTable {
Chris Lattner603bf122007-08-25 01:42:24 +000022 const char *Name;
23 unsigned Counter;
24 unsigned Size;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000025} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner603bf122007-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 Gregor19330152008-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);
Chris Lattner4b009652007-07-25 00:24:17 +000037#include "clang/AST/StmtNodes.def"
Nico Weber7340a2f2008-08-05 23:15:29 +000038
Chris Lattner603bf122007-08-25 01:42:24 +000039 return StmtClassInfo[E];
40}
41
Chris Lattner4b009652007-07-25 00:24:17 +000042const char *Stmt::getStmtClassName() const {
Chris Lattner603bf122007-08-25 01:42:24 +000043 return getStmtInfoTableEntry(sClass).Name;
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
Ted Kremenekafdf8112008-05-20 00:43:19 +000046void Stmt::DestroyChildren(ASTContext& C) {
Douglas Gregor0e7e7f42009-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 Kremenekafdf8112008-05-20 00:43:19 +000050}
51
52void Stmt::Destroy(ASTContext& C) {
53 DestroyChildren(C);
Ted Kremenekbde41092008-05-20 04:10:52 +000054 // FIXME: Eventually all Stmts should be allocated with the allocator
55 // in ASTContext, just like with Decls.
Ted Kremenek39dc20a2009-02-06 01:42:09 +000056 this->~Stmt();
57 C.Deallocate((void *)this);
Ted Kremenek1ed5dc62008-05-19 22:02:12 +000058}
59
Ted Kremeneka1e77702008-05-21 15:53:55 +000060void DeclStmt::Destroy(ASTContext& C) {
Ted Kremenek39dc20a2009-02-06 01:42:09 +000061 this->~DeclStmt();
62 C.Deallocate((void *)this);
Ted Kremeneka1e77702008-05-21 15:53:55 +000063}
64
Chris Lattner4b009652007-07-25 00:24:17 +000065void Stmt::PrintStats() {
Chris Lattner603bf122007-08-25 01:42:24 +000066 // Ensure the table is primed.
67 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber7340a2f2008-08-05 23:15:29 +000068
Chris Lattner4b009652007-07-25 00:24:17 +000069 unsigned sum = 0;
70 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner9c0da3b2007-08-25 01:55:00 +000071 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000072 if (StmtClassInfo[i].Name == 0) continue;
73 sum += StmtClassInfo[i].Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000074 }
75 fprintf(stderr, " %d stmts/exprs total.\n", sum);
76 sum = 0;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000077 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000078 if (StmtClassInfo[i].Name == 0) continue;
Nico Weber7340a2f2008-08-05 23:15:29 +000079 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner603bf122007-08-25 01:42:24 +000080 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
81 StmtClassInfo[i].Size,
82 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
83 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Chris Lattner4b009652007-07-25 00:24:17 +000084 }
85 fprintf(stderr, "Total bytes = %d\n", sum);
86}
87
88void Stmt::addStmtClass(StmtClass s) {
Chris Lattner603bf122007-08-25 01:42:24 +000089 ++getStmtInfoTableEntry(s).Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000090}
91
92static bool StatSwitch = false;
93
94bool Stmt::CollectingStats(bool enable) {
95 if (enable) StatSwitch = true;
Chris Lattner603bf122007-08-25 01:42:24 +000096 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000097}
98
99
Chris Lattner4b009652007-07-25 00:24:17 +0000100const char *LabelStmt::getName() const {
101 return getID()->getName();
102}
103
Steve Naroffc32a20d2007-08-31 23:49:30 +0000104// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber7340a2f2008-08-05 23:15:29 +0000105SourceRange ReturnStmt::getSourceRange() const {
Steve Naroffc32a20d2007-08-31 23:49:30 +0000106 if (RetExpr)
107 return SourceRange(RetLoc, RetExpr->getLocEnd());
108 else
109 return SourceRange(RetLoc);
110}
111
Ted Kremenekad5682f2007-10-01 16:34:52 +0000112bool Stmt::hasImplicitControlFlow() const {
113 switch (sClass) {
114 default:
115 return false;
Nico Weber7340a2f2008-08-05 23:15:29 +0000116
Ted Kremenekad5682f2007-10-01 16:34:52 +0000117 case CallExprClass:
118 case ConditionalOperatorClass:
119 case ChooseExprClass:
120 case StmtExprClass:
121 case DeclStmtClass:
Nico Weber7340a2f2008-08-05 23:15:29 +0000122 return true;
123
Ted Kremenekad5682f2007-10-01 16:34:52 +0000124 case Stmt::BinaryOperatorClass: {
125 const BinaryOperator* B = cast<BinaryOperator>(this);
126 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
127 return true;
128 else
129 return false;
130 }
131 }
132}
133
Ted Kremenekb30de272008-10-27 18:40:21 +0000134const Expr* AsmStmt::getOutputExpr(unsigned i) const {
135 return cast<Expr>(Exprs[i]);
136}
137Expr* AsmStmt::getOutputExpr(unsigned i) {
138 return cast<Expr>(Exprs[i]);
139}
140Expr* AsmStmt::getInputExpr(unsigned i) {
141 return cast<Expr>(Exprs[i + NumOutputs]);
142}
143const Expr* AsmStmt::getInputExpr(unsigned i) const {
144 return cast<Expr>(Exprs[i + NumOutputs]);
145}
146
Chris Lattner006d1542008-01-30 05:01:46 +0000147//===----------------------------------------------------------------------===//
148// Constructors
149//===----------------------------------------------------------------------===//
150
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000151AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattner006d1542008-01-30 05:01:46 +0000152 unsigned numoutputs, unsigned numinputs,
153 std::string *names, StringLiteral **constraints,
154 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
155 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlsson965d5202007-11-22 01:36:19 +0000156 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000157 , IsSimple(issimple), IsVolatile(isvolatile)
158 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson965d5202007-11-22 01:36:19 +0000159 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
160 Names.push_back(names[i]);
161 Exprs.push_back(exprs[i]);
Nico Weber7340a2f2008-08-05 23:15:29 +0000162 Constraints.push_back(constraints[i]);
Anders Carlsson965d5202007-11-22 01:36:19 +0000163 }
Nico Weber7340a2f2008-08-05 23:15:29 +0000164
Anders Carlsson965d5202007-11-22 01:36:19 +0000165 for (unsigned i = 0; i != numclobbers; i++)
166 Clobbers.push_back(clobbers[i]);
167}
168
Chris Lattner006d1542008-01-30 05:01:46 +0000169ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
170 Stmt *Body, SourceLocation FCL,
Nico Weber7340a2f2008-08-05 23:15:29 +0000171 SourceLocation RPL)
Chris Lattner006d1542008-01-30 05:01:46 +0000172: Stmt(ObjCForCollectionStmtClass) {
173 SubExprs[ELEM] = Elem;
174 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
175 SubExprs[BODY] = Body;
176 ForLoc = FCL;
177 RParenLoc = RPL;
178}
179
180
Nico Weber7340a2f2008-08-05 23:15:29 +0000181ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
182 SourceLocation rparenloc,
Daniel Dunbard23d9922009-03-01 04:28:32 +0000183 DeclStmt *catchVarStmtDecl, Stmt *atCatchStmt,
Chris Lattner006d1542008-01-30 05:01:46 +0000184 Stmt *atCatchList)
185: Stmt(ObjCAtCatchStmtClass) {
186 SubExprs[SELECTOR] = catchVarStmtDecl;
187 SubExprs[BODY] = atCatchStmt;
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000188 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbard23d9922009-03-01 04:28:32 +0000189 // FIXME: O(N^2) in number of catch blocks.
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000190 if (atCatchList) {
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000191 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
192
Nico Weber7340a2f2008-08-05 23:15:29 +0000193 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000194 AtCatchList = NextCatch;
Nico Weber7340a2f2008-08-05 23:15:29 +0000195
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000196 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner006d1542008-01-30 05:01:46 +0000197 }
198 AtCatchLoc = atCatchLoc;
199 RParenLoc = rparenloc;
200}
201
202
Ted Kremenek51be0562007-08-24 21:09:09 +0000203//===----------------------------------------------------------------------===//
204// Child Iterators for iterating over subexpressions/substatements
205//===----------------------------------------------------------------------===//
206
207// DeclStmt
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000208Stmt::child_iterator DeclStmt::child_begin() {
209 return StmtIterator(DG.begin(), DG.end());
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000210}
211
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000212Stmt::child_iterator DeclStmt::child_end() {
213 return StmtIterator(DG.end(), DG.end());
Ted Kremenek62f23bb2008-10-06 20:54:44 +0000214}
215
Ted Kremenek51be0562007-08-24 21:09:09 +0000216// NullStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000217Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
218Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000219
220// CompoundStmt
221Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek0c97e042009-02-07 01:47:29 +0000222Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000223
Ted Kremeneke07b67e2007-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 Kremenek51be0562007-08-24 21:09:09 +0000231
232// LabelStmt
233Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000234Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-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 Kremenek42730c52008-01-07 19:49:32 +0000256// ObjCForCollectionStmt
Nico Weber7340a2f2008-08-05 23:15:29 +0000257Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
258 return &SubExprs[0];
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000259}
Nico Weber7340a2f2008-08-05 23:15:29 +0000260Stmt::child_iterator ObjCForCollectionStmt::child_end() {
261 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000262}
263
Ted Kremenek51be0562007-08-24 21:09:09 +0000264// GotoStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000265Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
266Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000267
268// IndirectGotoStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000269Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
270const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000271
Ted Kremenek156714e2008-06-17 03:11:08 +0000272Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
273Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000274
275// ContinueStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000276Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
277Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000278
279// BreakStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000280Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
281Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000282
283// ReturnStmt
Ted Kremenek156714e2008-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 Kremenek51be0562007-08-24 21:09:09 +0000289}
290
Ted Kremenek156714e2008-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 Kremenek8a66ed42007-08-27 20:58:16 +0000296}
Ted Kremenek51be0562007-08-24 21:09:09 +0000297
Chris Lattner8a40a832007-10-29 04:04:16 +0000298// AsmStmt
Ted Kremenekb30de272008-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 Lattner8a40a832007-10-29 04:04:16 +0000305
Ted Kremenek42730c52008-01-07 19:49:32 +0000306// ObjCAtCatchStmt
307Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000308Stmt::child_iterator ObjCAtCatchStmt::child_end() {
309 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000310}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000311
Ted Kremenek42730c52008-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 Jahanian70952482007-11-01 21:12:44 +0000315
Ted Kremenek42730c52008-01-07 19:49:32 +0000316// ObjCAtTryStmt
317Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000318Stmt::child_iterator ObjCAtTryStmt::child_end() {
319 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000320}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000321
Ted Kremenek42730c52008-01-07 19:49:32 +0000322// ObjCAtThrowStmt
323Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000324 return &Throw;
325}
326
Ted Kremenek42730c52008-01-07 19:49:32 +0000327Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000328 return &Throw+1;
329}
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000330
331// ObjCAtSynchronizedStmt
332Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000333 return &SubStmts[0];
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000334}
335
336Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000337 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000338}
339
Sebastian Redl743c8162008-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 Redl237116b2008-12-22 21:35:02 +0000356 if (ExceptionDecl)
357 ExceptionDecl->Destroy(C);
Sebastian Redl743c8162008-12-22 19:15:10 +0000358 Stmt::Destroy(C);
359}
Sebastian Redl237116b2008-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}