blob: 02f5eee8287829796aa100563bf380b6a9404dc5 [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
Douglas Gregor025452f2009-04-17 00:04:06 +0000101void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
102 if (this->Body)
103 C.Deallocate(Body);
104 this->NumStmts = NumStmts;
105
106 Body = new (C) Stmt*[NumStmts];
107 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
108}
Reid Spencer5f016e22007-07-11 17:01:13 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110const char *LabelStmt::getName() const {
111 return getID()->getName();
112}
113
Steve Naroff507f2d52007-08-31 23:49:30 +0000114// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000115SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000116 if (RetExpr)
117 return SourceRange(RetLoc, RetExpr->getLocEnd());
118 else
119 return SourceRange(RetLoc);
120}
121
Ted Kremenekd48ade62007-10-01 16:34:52 +0000122bool Stmt::hasImplicitControlFlow() const {
123 switch (sClass) {
124 default:
125 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000126
Ted Kremenekd48ade62007-10-01 16:34:52 +0000127 case CallExprClass:
128 case ConditionalOperatorClass:
129 case ChooseExprClass:
130 case StmtExprClass:
131 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000132 return true;
133
Ted Kremenekd48ade62007-10-01 16:34:52 +0000134 case Stmt::BinaryOperatorClass: {
135 const BinaryOperator* B = cast<BinaryOperator>(this);
136 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
137 return true;
138 else
139 return false;
140 }
141 }
142}
143
Chris Lattnerb3277932009-03-10 04:59:06 +0000144Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000145 return cast<Expr>(Exprs[i]);
146}
Chris Lattnerb3277932009-03-10 04:59:06 +0000147
148/// getOutputConstraint - Return the constraint string for the specified
149/// output operand. All output constraints are known to be non-empty (either
150/// '=' or '+').
151std::string AsmStmt::getOutputConstraint(unsigned i) const {
152 return std::string(Constraints[i]->getStrData(),
153 Constraints[i]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000154}
Chris Lattnerb3277932009-03-10 04:59:06 +0000155
Chris Lattner85759272009-03-11 00:23:13 +0000156/// getNumPlusOperands - Return the number of output operands that have a "+"
157/// constraint.
158unsigned AsmStmt::getNumPlusOperands() const {
159 unsigned Res = 0;
160 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
161 if (isOutputPlusConstraint(i))
162 ++Res;
163 return Res;
164}
165
166
Chris Lattnerb3277932009-03-10 04:59:06 +0000167
168Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000169 return cast<Expr>(Exprs[i + NumOutputs]);
170}
Chris Lattnerb3277932009-03-10 04:59:06 +0000171
172/// getInputConstraint - Return the specified input constraint. Unlike output
173/// constraints, these can be empty.
174std::string AsmStmt::getInputConstraint(unsigned i) const {
175 return std::string(Constraints[i + NumOutputs]->getStrData(),
176 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000177}
178
Chris Lattner10ca96a2009-03-10 06:33:24 +0000179
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000180void AsmStmt::setOutputsAndInputs(unsigned NumOutputs,
181 unsigned NumInputs,
182 const std::string *Names,
183 StringLiteral **Constraints,
184 Stmt **Exprs) {
185 this->NumOutputs = NumOutputs;
186 this->NumInputs = NumInputs;
187 this->Names.clear();
188 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
189 this->Constraints.clear();
190 this->Constraints.insert(this->Constraints.end(),
191 Constraints, Constraints + NumOutputs + NumInputs);
192 this->Exprs.clear();
193 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
194}
195
Chris Lattner10ca96a2009-03-10 06:33:24 +0000196/// getNamedOperand - Given a symbolic operand reference like %[foo],
197/// translate this into a numeric value needed to reference the same operand.
198/// This returns -1 if the operand name is invalid.
199int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
200 unsigned NumPlusOperands = 0;
201
202 // Check if this is an output operand.
203 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
204 if (getOutputName(i) == SymbolicName)
205 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000206 }
207
208 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
209 if (getInputName(i) == SymbolicName)
210 return getNumOutputs() + NumPlusOperands + i;
211
212 // Not found.
213 return -1;
214}
215
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000216void AsmStmt::setClobbers(StringLiteral **Clobbers, unsigned NumClobbers) {
217 this->Clobbers.clear();
218 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
219}
Chris Lattner10ca96a2009-03-10 06:33:24 +0000220
Chris Lattner458cd9c2009-03-10 23:21:44 +0000221/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
222/// it into pieces. If the asm string is erroneous, emit errors and return
223/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000224unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
225 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000226 const char *StrStart = getAsmString()->getStrData();
227 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000228 const char *CurPtr = StrStart;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000229
230 // "Simple" inline asms have no constraints or operands, just convert the asm
231 // string to escape $'s.
232 if (isSimple()) {
233 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000234 for (; CurPtr != StrEnd; ++CurPtr) {
235 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000236 case '$':
237 Result += "$$";
238 break;
239 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000240 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000241 break;
242 }
243 }
244 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000245 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000246 }
247
248 // CurStringPiece - The current string that we are building up as we scan the
249 // asm string.
250 std::string CurStringPiece;
251
252 while (1) {
253 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000254 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000255 if (!CurStringPiece.empty())
256 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000257 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000258 }
259
Chris Lattner3182db12009-03-10 23:51:40 +0000260 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000261 if (CurChar == '$') {
262 CurStringPiece += "$$";
263 continue;
264 } else if (CurChar != '%') {
265 CurStringPiece += CurChar;
266 continue;
267 }
268
269 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000270 if (CurPtr == StrEnd) {
271 // % at end of string is invalid (no escape).
272 DiagOffs = CurPtr-StrStart-1;
273 return diag::err_asm_invalid_escape;
274 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000275
Chris Lattner3182db12009-03-10 23:51:40 +0000276 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000277 if (EscapedChar == '%') { // %% -> %
278 // Escaped percentage sign.
279 CurStringPiece += '%';
280 continue;
281 }
282
283 if (EscapedChar == '=') { // %= -> Generate an unique ID.
284 CurStringPiece += "${:uid}";
285 continue;
286 }
287
288 // Otherwise, we have an operand. If we have accumulated a string so far,
289 // add it to the Pieces list.
290 if (!CurStringPiece.empty()) {
291 Pieces.push_back(AsmStringPiece(CurStringPiece));
292 CurStringPiece.clear();
293 }
294
295 // Handle %x4 and %x[foo] by capturing x as the modifier character.
296 char Modifier = '\0';
297 if (isalpha(EscapedChar)) {
298 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000299 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000300 }
301
302 if (isdigit(EscapedChar)) {
303 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000304 unsigned N = 0;
305
306 --CurPtr;
307 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000308 N = N*10 + ((*CurPtr++)-'0');
Chris Lattner85759272009-03-11 00:23:13 +0000309
310 unsigned NumOperands =
311 getNumOutputs() + getNumPlusOperands() + getNumInputs();
312 if (N >= NumOperands) {
313 DiagOffs = CurPtr-StrStart-1;
314 return diag::err_asm_invalid_operand_number;
315 }
316
Chris Lattner458cd9c2009-03-10 23:21:44 +0000317 Pieces.push_back(AsmStringPiece(N, Modifier));
318 continue;
319 }
320
321 // Handle %[foo], a symbolic operand reference.
322 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000323 DiagOffs = CurPtr-StrStart-1;
324
325 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000326 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000327 if (NameEnd == 0)
328 return diag::err_asm_unterminated_symbolic_operand_name;
329 if (NameEnd == CurPtr)
330 return diag::err_asm_empty_symbolic_operand_name;
331
Chris Lattner3182db12009-03-10 23:51:40 +0000332 std::string SymbolicName(CurPtr, NameEnd);
Chris Lattner458cd9c2009-03-10 23:21:44 +0000333
334 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000335 if (N == -1) {
336 // Verify that an operand with that name exists.
337 DiagOffs = CurPtr-StrStart;
338 return diag::err_asm_unknown_symbolic_operand_name;
339 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000340 Pieces.push_back(AsmStringPiece(N, Modifier));
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000341
342 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000343 continue;
344 }
345
Chris Lattner2ff0f422009-03-10 23:57:07 +0000346 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000347 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000348 }
349}
350
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000351//===----------------------------------------------------------------------===//
352// Constructors
353//===----------------------------------------------------------------------===//
354
Anders Carlssondfab34a2008-02-05 23:03:50 +0000355AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000356 unsigned numoutputs, unsigned numinputs,
357 std::string *names, StringLiteral **constraints,
358 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
359 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000360 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000361 , IsSimple(issimple), IsVolatile(isvolatile)
362 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000363 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
364 Names.push_back(names[i]);
365 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000366 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000367 }
Nico Weber608b17f2008-08-05 23:15:29 +0000368
Anders Carlssonb235fc22007-11-22 01:36:19 +0000369 for (unsigned i = 0; i != numclobbers; i++)
370 Clobbers.push_back(clobbers[i]);
371}
372
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000373ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
374 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000375 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000376: Stmt(ObjCForCollectionStmtClass) {
377 SubExprs[ELEM] = Elem;
378 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
379 SubExprs[BODY] = Body;
380 ForLoc = FCL;
381 RParenLoc = RPL;
382}
383
384
Nico Weber608b17f2008-08-05 23:15:29 +0000385ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
386 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000387 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000388 Stmt *atCatchList)
389: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000390 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000391 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000392 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000393 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000394 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000395 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
396
Nico Weber608b17f2008-08-05 23:15:29 +0000397 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000398 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000399
Ted Kremenekff981022008-02-01 21:28:59 +0000400 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000401 }
402 AtCatchLoc = atCatchLoc;
403 RParenLoc = rparenloc;
404}
405
406
Ted Kremenek82977772007-08-24 21:09:09 +0000407//===----------------------------------------------------------------------===//
408// Child Iterators for iterating over subexpressions/substatements
409//===----------------------------------------------------------------------===//
410
411// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000412Stmt::child_iterator DeclStmt::child_begin() {
413 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000414}
415
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000416Stmt::child_iterator DeclStmt::child_end() {
417 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000418}
419
Ted Kremenek82977772007-08-24 21:09:09 +0000420// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000421Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
422Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000423
424// CompoundStmt
425Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000426Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000427
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000428// CaseStmt
429Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
430Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
431
432// DefaultStmt
433Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
434Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000435
436// LabelStmt
437Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000438Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000439
440// IfStmt
441Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
442Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
443
444// SwitchStmt
445Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
446Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
447
448// WhileStmt
449Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
450Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
451
452// DoStmt
453Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
454Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
455
456// ForStmt
457Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
458Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
459
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000460// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000461Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
462 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000463}
Nico Weber608b17f2008-08-05 23:15:29 +0000464Stmt::child_iterator ObjCForCollectionStmt::child_end() {
465 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000466}
467
Ted Kremenek82977772007-08-24 21:09:09 +0000468// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000469Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
470Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000471
472// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000473Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
474const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000475
Ted Kremenek1060aff2008-06-17 03:11:08 +0000476Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
477Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000478
479// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000480Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
481Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000482
483// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000484Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
485Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000486
487// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000488const Expr* ReturnStmt::getRetValue() const {
489 return cast_or_null<Expr>(RetExpr);
490}
491Expr* ReturnStmt::getRetValue() {
492 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000493}
494
Ted Kremenek1060aff2008-06-17 03:11:08 +0000495Stmt::child_iterator ReturnStmt::child_begin() {
496 return &RetExpr;
497}
498Stmt::child_iterator ReturnStmt::child_end() {
499 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000500}
Ted Kremenek82977772007-08-24 21:09:09 +0000501
Chris Lattnerfe795952007-10-29 04:04:16 +0000502// AsmStmt
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000503Stmt::child_iterator AsmStmt::child_begin() {
504 return Exprs.empty() ? 0 : &Exprs[0];
505}
506Stmt::child_iterator AsmStmt::child_end() {
507 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
508}
Chris Lattnerfe795952007-10-29 04:04:16 +0000509
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000510// ObjCAtCatchStmt
511Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000512Stmt::child_iterator ObjCAtCatchStmt::child_end() {
513 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000514}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000515
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000516// ObjCAtFinallyStmt
517Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
518Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000519
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520// ObjCAtTryStmt
521Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000522Stmt::child_iterator ObjCAtTryStmt::child_end() {
523 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000524}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000525
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000526// ObjCAtThrowStmt
527Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000528 return &Throw;
529}
530
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000531Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000532 return &Throw+1;
533}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000534
535// ObjCAtSynchronizedStmt
536Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000537 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000538}
539
540Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000541 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000542}
543
Sebastian Redl4b07b292008-12-22 19:15:10 +0000544// CXXCatchStmt
545Stmt::child_iterator CXXCatchStmt::child_begin() {
546 return &HandlerBlock;
547}
548
549Stmt::child_iterator CXXCatchStmt::child_end() {
550 return &HandlerBlock + 1;
551}
552
553QualType CXXCatchStmt::getCaughtType() {
554 if (ExceptionDecl)
555 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
556 return QualType();
557}
558
559void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000560 if (ExceptionDecl)
561 ExceptionDecl->Destroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000562 Stmt::Destroy(C);
563}
Sebastian Redl8351da02008-12-22 21:35:02 +0000564
565// CXXTryStmt
566Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
567Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
568
569CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
570 Stmt **handlers, unsigned numHandlers)
571 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
572 Stmts.push_back(tryBlock);
573 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
574}