blob: f8f6e067e1a64e574c11f53045bea3d651b95e3c [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"
Chris Lattner16f00492009-04-26 01:32:48 +000017#include "clang/AST/StmtCXX.h"
18#include "clang/AST/StmtObjC.h"
Sebastian Redl4b07b292008-12-22 19:15:10 +000019#include "clang/AST/Type.h"
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000020#include "clang/AST/ASTContext.h"
Chris Lattner3182db12009-03-10 23:51:40 +000021#include "clang/AST/ASTDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000025 const char *Name;
26 unsigned Counter;
27 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000028} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000029
30static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
31 static bool Initialized = false;
32 if (Initialized)
33 return StmtClassInfo[E];
34
35 // Intialize the table on the first use.
36 Initialized = true;
Douglas Gregorf2cad862008-11-14 12:46:07 +000037#define STMT(CLASS, PARENT) \
38 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
39 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Reid Spencer5f016e22007-07-11 17:01:13 +000040#include "clang/AST/StmtNodes.def"
Nico Weber608b17f2008-08-05 23:15:29 +000041
Chris Lattner63381352007-08-25 01:42:24 +000042 return StmtClassInfo[E];
43}
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045const char *Stmt::getStmtClassName() const {
Chris Lattner63381352007-08-25 01:42:24 +000046 return getStmtInfoTableEntry(sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000047}
48
Chris Lattner24e1e702009-03-04 04:23:07 +000049void Stmt::DestroyChildren(ASTContext &C) {
50 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
Douglas Gregor860f6d42009-01-16 06:50:08 +000051 if (Stmt* Child = *I++) Child->Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +000052}
53
Chris Lattner24e1e702009-03-04 04:23:07 +000054void Stmt::Destroy(ASTContext &C) {
Ted Kremenek27f8a282008-05-20 00:43:19 +000055 DestroyChildren(C);
Ted Kremenekf809e3b2008-05-20 04:10:52 +000056 // FIXME: Eventually all Stmts should be allocated with the allocator
57 // in ASTContext, just like with Decls.
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000058 this->~Stmt();
59 C.Deallocate((void *)this);
Ted Kremenek9c1863e2008-05-19 22:02:12 +000060}
61
Chris Lattner24e1e702009-03-04 04:23:07 +000062void DeclStmt::Destroy(ASTContext &C) {
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000063 this->~DeclStmt();
64 C.Deallocate((void *)this);
Ted Kremenek8e355f22008-05-21 15:53:55 +000065}
66
Reid Spencer5f016e22007-07-11 17:01:13 +000067void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000068 // Ensure the table is primed.
69 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000070
Reid Spencer5f016e22007-07-11 17:01:13 +000071 unsigned sum = 0;
72 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000073 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000074 if (StmtClassInfo[i].Name == 0) continue;
75 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000076 }
77 fprintf(stderr, " %d stmts/exprs total.\n", sum);
78 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000079 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000080 if (StmtClassInfo[i].Name == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000081 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000082 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
83 StmtClassInfo[i].Size,
84 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
85 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 }
87 fprintf(stderr, "Total bytes = %d\n", sum);
88}
89
90void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000091 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000092}
93
94static bool StatSwitch = false;
95
96bool Stmt::CollectingStats(bool enable) {
97 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000098 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000099}
100
Anders Carlssond19cd902009-05-15 00:21:21 +0000101NullStmt* NullStmt::Clone(ASTContext &C) const {
102 return new (C) NullStmt(SemiLoc);
103}
104
Douglas Gregor025452f2009-04-17 00:04:06 +0000105void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
106 if (this->Body)
107 C.Deallocate(Body);
108 this->NumStmts = NumStmts;
109
110 Body = new (C) Stmt*[NumStmts];
111 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
112}
Reid Spencer5f016e22007-07-11 17:01:13 +0000113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114const char *LabelStmt::getName() const {
115 return getID()->getName();
116}
117
Steve Naroff507f2d52007-08-31 23:49:30 +0000118// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000119SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000120 if (RetExpr)
121 return SourceRange(RetLoc, RetExpr->getLocEnd());
122 else
123 return SourceRange(RetLoc);
124}
125
Ted Kremenekd48ade62007-10-01 16:34:52 +0000126bool Stmt::hasImplicitControlFlow() const {
127 switch (sClass) {
128 default:
129 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000130
Ted Kremenekd48ade62007-10-01 16:34:52 +0000131 case CallExprClass:
132 case ConditionalOperatorClass:
133 case ChooseExprClass:
134 case StmtExprClass:
135 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000136 return true;
137
Ted Kremenekd48ade62007-10-01 16:34:52 +0000138 case Stmt::BinaryOperatorClass: {
139 const BinaryOperator* B = cast<BinaryOperator>(this);
140 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
141 return true;
142 else
143 return false;
144 }
145 }
146}
147
Chris Lattnerb3277932009-03-10 04:59:06 +0000148Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000149 return cast<Expr>(Exprs[i]);
150}
Chris Lattnerb3277932009-03-10 04:59:06 +0000151
152/// getOutputConstraint - Return the constraint string for the specified
153/// output operand. All output constraints are known to be non-empty (either
154/// '=' or '+').
155std::string AsmStmt::getOutputConstraint(unsigned i) const {
156 return std::string(Constraints[i]->getStrData(),
157 Constraints[i]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000158}
Chris Lattnerb3277932009-03-10 04:59:06 +0000159
Chris Lattner85759272009-03-11 00:23:13 +0000160/// getNumPlusOperands - Return the number of output operands that have a "+"
161/// constraint.
162unsigned AsmStmt::getNumPlusOperands() const {
163 unsigned Res = 0;
164 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
165 if (isOutputPlusConstraint(i))
166 ++Res;
167 return Res;
168}
169
170
Chris Lattnerb3277932009-03-10 04:59:06 +0000171
172Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000173 return cast<Expr>(Exprs[i + NumOutputs]);
174}
Chris Lattnerb3277932009-03-10 04:59:06 +0000175
176/// getInputConstraint - Return the specified input constraint. Unlike output
177/// constraints, these can be empty.
178std::string AsmStmt::getInputConstraint(unsigned i) const {
179 return std::string(Constraints[i + NumOutputs]->getStrData(),
180 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000181}
182
Chris Lattner10ca96a2009-03-10 06:33:24 +0000183
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000184void AsmStmt::setOutputsAndInputs(unsigned NumOutputs,
185 unsigned NumInputs,
186 const std::string *Names,
187 StringLiteral **Constraints,
188 Stmt **Exprs) {
189 this->NumOutputs = NumOutputs;
190 this->NumInputs = NumInputs;
191 this->Names.clear();
192 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
193 this->Constraints.clear();
194 this->Constraints.insert(this->Constraints.end(),
195 Constraints, Constraints + NumOutputs + NumInputs);
196 this->Exprs.clear();
197 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
198}
199
Chris Lattner10ca96a2009-03-10 06:33:24 +0000200/// getNamedOperand - Given a symbolic operand reference like %[foo],
201/// translate this into a numeric value needed to reference the same operand.
202/// This returns -1 if the operand name is invalid.
203int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
204 unsigned NumPlusOperands = 0;
205
206 // Check if this is an output operand.
207 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
208 if (getOutputName(i) == SymbolicName)
209 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000210 }
211
212 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
213 if (getInputName(i) == SymbolicName)
214 return getNumOutputs() + NumPlusOperands + i;
215
216 // Not found.
217 return -1;
218}
219
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000220void AsmStmt::setClobbers(StringLiteral **Clobbers, unsigned NumClobbers) {
221 this->Clobbers.clear();
222 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
223}
Chris Lattner10ca96a2009-03-10 06:33:24 +0000224
Chris Lattner458cd9c2009-03-10 23:21:44 +0000225/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
226/// it into pieces. If the asm string is erroneous, emit errors and return
227/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000228unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
229 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000230 const char *StrStart = getAsmString()->getStrData();
231 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000232 const char *CurPtr = StrStart;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000233
234 // "Simple" inline asms have no constraints or operands, just convert the asm
235 // string to escape $'s.
236 if (isSimple()) {
237 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000238 for (; CurPtr != StrEnd; ++CurPtr) {
239 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000240 case '$':
241 Result += "$$";
242 break;
243 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000244 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000245 break;
246 }
247 }
248 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000249 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000250 }
251
252 // CurStringPiece - The current string that we are building up as we scan the
253 // asm string.
254 std::string CurStringPiece;
255
256 while (1) {
257 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000258 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000259 if (!CurStringPiece.empty())
260 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000261 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000262 }
263
Chris Lattner3182db12009-03-10 23:51:40 +0000264 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000265 if (CurChar == '$') {
266 CurStringPiece += "$$";
267 continue;
268 } else if (CurChar != '%') {
269 CurStringPiece += CurChar;
270 continue;
271 }
272
273 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000274 if (CurPtr == StrEnd) {
275 // % at end of string is invalid (no escape).
276 DiagOffs = CurPtr-StrStart-1;
277 return diag::err_asm_invalid_escape;
278 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000279
Chris Lattner3182db12009-03-10 23:51:40 +0000280 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000281 if (EscapedChar == '%') { // %% -> %
282 // Escaped percentage sign.
283 CurStringPiece += '%';
284 continue;
285 }
286
287 if (EscapedChar == '=') { // %= -> Generate an unique ID.
288 CurStringPiece += "${:uid}";
289 continue;
290 }
291
292 // Otherwise, we have an operand. If we have accumulated a string so far,
293 // add it to the Pieces list.
294 if (!CurStringPiece.empty()) {
295 Pieces.push_back(AsmStringPiece(CurStringPiece));
296 CurStringPiece.clear();
297 }
298
299 // Handle %x4 and %x[foo] by capturing x as the modifier character.
300 char Modifier = '\0';
301 if (isalpha(EscapedChar)) {
302 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000303 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000304 }
305
306 if (isdigit(EscapedChar)) {
307 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000308 unsigned N = 0;
309
310 --CurPtr;
311 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000312 N = N*10 + ((*CurPtr++)-'0');
Chris Lattner85759272009-03-11 00:23:13 +0000313
314 unsigned NumOperands =
315 getNumOutputs() + getNumPlusOperands() + getNumInputs();
316 if (N >= NumOperands) {
317 DiagOffs = CurPtr-StrStart-1;
318 return diag::err_asm_invalid_operand_number;
319 }
320
Chris Lattner458cd9c2009-03-10 23:21:44 +0000321 Pieces.push_back(AsmStringPiece(N, Modifier));
322 continue;
323 }
324
325 // Handle %[foo], a symbolic operand reference.
326 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000327 DiagOffs = CurPtr-StrStart-1;
328
329 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000330 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000331 if (NameEnd == 0)
332 return diag::err_asm_unterminated_symbolic_operand_name;
333 if (NameEnd == CurPtr)
334 return diag::err_asm_empty_symbolic_operand_name;
335
Chris Lattner3182db12009-03-10 23:51:40 +0000336 std::string SymbolicName(CurPtr, NameEnd);
Chris Lattner458cd9c2009-03-10 23:21:44 +0000337
338 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000339 if (N == -1) {
340 // Verify that an operand with that name exists.
341 DiagOffs = CurPtr-StrStart;
342 return diag::err_asm_unknown_symbolic_operand_name;
343 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000344 Pieces.push_back(AsmStringPiece(N, Modifier));
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000345
346 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000347 continue;
348 }
349
Chris Lattner2ff0f422009-03-10 23:57:07 +0000350 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000351 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000352 }
353}
354
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000355//===----------------------------------------------------------------------===//
356// Constructors
357//===----------------------------------------------------------------------===//
358
Anders Carlssondfab34a2008-02-05 23:03:50 +0000359AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000360 unsigned numoutputs, unsigned numinputs,
361 std::string *names, StringLiteral **constraints,
362 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
363 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000364 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000365 , IsSimple(issimple), IsVolatile(isvolatile)
366 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000367 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
368 Names.push_back(names[i]);
369 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000370 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000371 }
Nico Weber608b17f2008-08-05 23:15:29 +0000372
Anders Carlssonb235fc22007-11-22 01:36:19 +0000373 for (unsigned i = 0; i != numclobbers; i++)
374 Clobbers.push_back(clobbers[i]);
375}
376
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000377ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
378 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000379 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000380: Stmt(ObjCForCollectionStmtClass) {
381 SubExprs[ELEM] = Elem;
382 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
383 SubExprs[BODY] = Body;
384 ForLoc = FCL;
385 RParenLoc = RPL;
386}
387
388
Nico Weber608b17f2008-08-05 23:15:29 +0000389ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
390 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000391 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000392 Stmt *atCatchList)
393: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000394 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000395 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000396 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000397 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000398 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000399 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
400
Nico Weber608b17f2008-08-05 23:15:29 +0000401 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000402 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000403
Ted Kremenekff981022008-02-01 21:28:59 +0000404 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000405 }
406 AtCatchLoc = atCatchLoc;
407 RParenLoc = rparenloc;
408}
409
410
Ted Kremenek82977772007-08-24 21:09:09 +0000411//===----------------------------------------------------------------------===//
412// Child Iterators for iterating over subexpressions/substatements
413//===----------------------------------------------------------------------===//
414
415// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000416Stmt::child_iterator DeclStmt::child_begin() {
417 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000418}
419
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000420Stmt::child_iterator DeclStmt::child_end() {
421 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000422}
423
Ted Kremenek82977772007-08-24 21:09:09 +0000424// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000425Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
426Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000427
428// CompoundStmt
429Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000430Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000431
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000432// CaseStmt
433Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
434Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
435
436// DefaultStmt
437Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
438Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000439
440// LabelStmt
441Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000442Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000443
444// IfStmt
445Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
446Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
447
448// SwitchStmt
449Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
450Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
451
452// WhileStmt
453Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
454Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
455
456// DoStmt
457Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
458Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
459
460// ForStmt
461Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
462Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
463
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000464// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000465Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
466 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000467}
Nico Weber608b17f2008-08-05 23:15:29 +0000468Stmt::child_iterator ObjCForCollectionStmt::child_end() {
469 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000470}
471
Ted Kremenek82977772007-08-24 21:09:09 +0000472// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000473Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
474Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000475
476// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000477Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
478const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000479
Ted Kremenek1060aff2008-06-17 03:11:08 +0000480Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
481Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000482
483// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000484Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
485Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000486
487// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000488Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
489Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000490
491// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000492const Expr* ReturnStmt::getRetValue() const {
493 return cast_or_null<Expr>(RetExpr);
494}
495Expr* ReturnStmt::getRetValue() {
496 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000497}
498
Ted Kremenek1060aff2008-06-17 03:11:08 +0000499Stmt::child_iterator ReturnStmt::child_begin() {
500 return &RetExpr;
501}
502Stmt::child_iterator ReturnStmt::child_end() {
503 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000504}
Ted Kremenek82977772007-08-24 21:09:09 +0000505
Chris Lattnerfe795952007-10-29 04:04:16 +0000506// AsmStmt
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000507Stmt::child_iterator AsmStmt::child_begin() {
508 return Exprs.empty() ? 0 : &Exprs[0];
509}
510Stmt::child_iterator AsmStmt::child_end() {
511 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
512}
Chris Lattnerfe795952007-10-29 04:04:16 +0000513
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000514// ObjCAtCatchStmt
515Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000516Stmt::child_iterator ObjCAtCatchStmt::child_end() {
517 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000518}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000519
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520// ObjCAtFinallyStmt
521Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
522Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000523
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000524// ObjCAtTryStmt
525Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000526Stmt::child_iterator ObjCAtTryStmt::child_end() {
527 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000528}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000529
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000530// ObjCAtThrowStmt
531Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000532 return &Throw;
533}
534
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000535Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000536 return &Throw+1;
537}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000538
539// ObjCAtSynchronizedStmt
540Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000541 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000542}
543
544Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000545 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000546}
547
Sebastian Redl4b07b292008-12-22 19:15:10 +0000548// CXXCatchStmt
549Stmt::child_iterator CXXCatchStmt::child_begin() {
550 return &HandlerBlock;
551}
552
553Stmt::child_iterator CXXCatchStmt::child_end() {
554 return &HandlerBlock + 1;
555}
556
557QualType CXXCatchStmt::getCaughtType() {
558 if (ExceptionDecl)
559 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
560 return QualType();
561}
562
563void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000564 if (ExceptionDecl)
565 ExceptionDecl->Destroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000566 Stmt::Destroy(C);
567}
Sebastian Redl8351da02008-12-22 21:35:02 +0000568
569// CXXTryStmt
570Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
571Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
572
573CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
574 Stmt **handlers, unsigned numHandlers)
575 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
576 Stmts.push_back(tryBlock);
577 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
578}