blob: b85a67e689f8b7f22da4f3f195e54f9413c5a344 [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 Kremenek11e5a7f2009-02-06 01:42:09 +000061 this->~DeclStmt();
62 C.Deallocate((void *)this);
Ted Kremenek8e355f22008-05-21 15:53:55 +000063}
64
Reid Spencer5f016e22007-07-11 17:01:13 +000065void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000066 // Ensure the table is primed.
67 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000068
Reid Spencer5f016e22007-07-11 17:01:13 +000069 unsigned sum = 0;
70 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000071 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000072 if (StmtClassInfo[i].Name == 0) continue;
73 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
75 fprintf(stderr, " %d stmts/exprs total.\n", sum);
76 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000077 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000078 if (StmtClassInfo[i].Name == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000079 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-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;
Reid Spencer5f016e22007-07-11 17:01:13 +000084 }
85 fprintf(stderr, "Total bytes = %d\n", sum);
86}
87
88void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000089 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000090}
91
92static bool StatSwitch = false;
93
94bool Stmt::CollectingStats(bool enable) {
95 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000096 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
99
Reid Spencer5f016e22007-07-11 17:01:13 +0000100const char *LabelStmt::getName() const {
101 return getID()->getName();
102}
103
Steve Naroff507f2d52007-08-31 23:49:30 +0000104// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000105SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000106 if (RetExpr)
107 return SourceRange(RetLoc, RetExpr->getLocEnd());
108 else
109 return SourceRange(RetLoc);
110}
111
Ted Kremenekd48ade62007-10-01 16:34:52 +0000112bool Stmt::hasImplicitControlFlow() const {
113 switch (sClass) {
114 default:
115 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000116
Ted Kremenekd48ade62007-10-01 16:34:52 +0000117 case CallExprClass:
118 case ConditionalOperatorClass:
119 case ChooseExprClass:
120 case StmtExprClass:
121 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000122 return true;
123
Ted Kremenekd48ade62007-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 Kremenekce2fc3a2008-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 Lattnerdb6ed172008-01-30 05:01:46 +0000147//===----------------------------------------------------------------------===//
148// Constructors
149//===----------------------------------------------------------------------===//
150
Anders Carlssondfab34a2008-02-05 23:03:50 +0000151AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-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 Carlssonb235fc22007-11-22 01:36:19 +0000156 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000157 , IsSimple(issimple), IsVolatile(isvolatile)
158 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-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 Weber608b17f2008-08-05 23:15:29 +0000162 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000163 }
Nico Weber608b17f2008-08-05 23:15:29 +0000164
Anders Carlssonb235fc22007-11-22 01:36:19 +0000165 for (unsigned i = 0; i != numclobbers; i++)
166 Clobbers.push_back(clobbers[i]);
167}
168
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000169ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
170 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000171 SourceLocation RPL)
Chris Lattnerdb6ed172008-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 Weber608b17f2008-08-05 23:15:29 +0000181ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
182 SourceLocation rparenloc,
183 Stmt *catchVarStmtDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000184 Stmt *atCatchList)
185: Stmt(ObjCAtCatchStmtClass) {
186 SubExprs[SELECTOR] = catchVarStmtDecl;
187 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000188 SubExprs[NEXT_CATCH] = NULL;
189 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000190 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
191
Nico Weber608b17f2008-08-05 23:15:29 +0000192 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000193 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000194
Ted Kremenekff981022008-02-01 21:28:59 +0000195 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000196 }
197 AtCatchLoc = atCatchLoc;
198 RParenLoc = rparenloc;
199}
200
201
Ted Kremenek82977772007-08-24 21:09:09 +0000202//===----------------------------------------------------------------------===//
203// Child Iterators for iterating over subexpressions/substatements
204//===----------------------------------------------------------------------===//
205
206// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000207Stmt::child_iterator DeclStmt::child_begin() {
208 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000209}
210
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000211Stmt::child_iterator DeclStmt::child_end() {
212 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000213}
214
Ted Kremenek82977772007-08-24 21:09:09 +0000215// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000216Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
217Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000218
219// CompoundStmt
220Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000221Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000222
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000223// CaseStmt
224Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
225Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
226
227// DefaultStmt
228Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
229Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000230
231// LabelStmt
232Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000233Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000234
235// IfStmt
236Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
237Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
238
239// SwitchStmt
240Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
241Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
242
243// WhileStmt
244Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
245Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
246
247// DoStmt
248Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
249Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
250
251// ForStmt
252Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
253Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
254
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000255// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000256Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
257 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000258}
Nico Weber608b17f2008-08-05 23:15:29 +0000259Stmt::child_iterator ObjCForCollectionStmt::child_end() {
260 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000261}
262
Ted Kremenek82977772007-08-24 21:09:09 +0000263// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000264Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
265Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000266
267// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000268Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
269const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000270
Ted Kremenek1060aff2008-06-17 03:11:08 +0000271Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
272Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000273
274// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000275Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
276Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000277
278// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000279Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
280Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000281
282// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000283const Expr* ReturnStmt::getRetValue() const {
284 return cast_or_null<Expr>(RetExpr);
285}
286Expr* ReturnStmt::getRetValue() {
287 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000288}
289
Ted Kremenek1060aff2008-06-17 03:11:08 +0000290Stmt::child_iterator ReturnStmt::child_begin() {
291 return &RetExpr;
292}
293Stmt::child_iterator ReturnStmt::child_end() {
294 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000295}
Ted Kremenek82977772007-08-24 21:09:09 +0000296
Chris Lattnerfe795952007-10-29 04:04:16 +0000297// AsmStmt
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000298Stmt::child_iterator AsmStmt::child_begin() {
299 return Exprs.empty() ? 0 : &Exprs[0];
300}
301Stmt::child_iterator AsmStmt::child_end() {
302 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
303}
Chris Lattnerfe795952007-10-29 04:04:16 +0000304
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000305// ObjCAtCatchStmt
306Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000307Stmt::child_iterator ObjCAtCatchStmt::child_end() {
308 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000309}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000310
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000311// ObjCAtFinallyStmt
312Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
313Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000314
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000315// ObjCAtTryStmt
316Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000317Stmt::child_iterator ObjCAtTryStmt::child_end() {
318 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000319}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000320
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000321// ObjCAtThrowStmt
322Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000323 return &Throw;
324}
325
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000326Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000327 return &Throw+1;
328}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000329
330// ObjCAtSynchronizedStmt
331Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000332 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000333}
334
335Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000336 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000337}
338
Sebastian Redl4b07b292008-12-22 19:15:10 +0000339// CXXCatchStmt
340Stmt::child_iterator CXXCatchStmt::child_begin() {
341 return &HandlerBlock;
342}
343
344Stmt::child_iterator CXXCatchStmt::child_end() {
345 return &HandlerBlock + 1;
346}
347
348QualType CXXCatchStmt::getCaughtType() {
349 if (ExceptionDecl)
350 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
351 return QualType();
352}
353
354void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000355 if (ExceptionDecl)
356 ExceptionDecl->Destroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000357 Stmt::Destroy(C);
358}
Sebastian Redl8351da02008-12-22 21:35:02 +0000359
360// CXXTryStmt
361Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
362Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
363
364CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
365 Stmt **handlers, unsigned numHandlers)
366 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
367 Stmts.push_back(tryBlock);
368 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
369}