blob: ce55fae87e9ab5f77b8a40aeebe134f63194545e [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"
Chris Lattner3182db12009-03-10 23:51:40 +000019#include "clang/AST/ASTDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Reid Spencer5f016e22007-07-11 17:01:13 +000022static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000023 const char *Name;
24 unsigned Counter;
25 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000026} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000027
28static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
29 static bool Initialized = false;
30 if (Initialized)
31 return StmtClassInfo[E];
32
33 // Intialize the table on the first use.
34 Initialized = true;
Douglas Gregorf2cad862008-11-14 12:46:07 +000035#define STMT(CLASS, PARENT) \
36 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
37 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Reid Spencer5f016e22007-07-11 17:01:13 +000038#include "clang/AST/StmtNodes.def"
Nico Weber608b17f2008-08-05 23:15:29 +000039
Chris Lattner63381352007-08-25 01:42:24 +000040 return StmtClassInfo[E];
41}
42
Reid Spencer5f016e22007-07-11 17:01:13 +000043const char *Stmt::getStmtClassName() const {
Chris Lattner63381352007-08-25 01:42:24 +000044 return getStmtInfoTableEntry(sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000045}
46
Chris Lattner24e1e702009-03-04 04:23:07 +000047void Stmt::DestroyChildren(ASTContext &C) {
48 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
Douglas Gregor860f6d42009-01-16 06:50:08 +000049 if (Stmt* Child = *I++) Child->Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +000050}
51
Chris Lattner24e1e702009-03-04 04:23:07 +000052void Stmt::Destroy(ASTContext &C) {
Ted Kremenek27f8a282008-05-20 00:43:19 +000053 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
Chris Lattner24e1e702009-03-04 04:23:07 +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
Chris Lattnerb3277932009-03-10 04:59:06 +0000134Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000135 return cast<Expr>(Exprs[i]);
136}
Chris Lattnerb3277932009-03-10 04:59:06 +0000137
138/// getOutputConstraint - Return the constraint string for the specified
139/// output operand. All output constraints are known to be non-empty (either
140/// '=' or '+').
141std::string AsmStmt::getOutputConstraint(unsigned i) const {
142 return std::string(Constraints[i]->getStrData(),
143 Constraints[i]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000144}
Chris Lattnerb3277932009-03-10 04:59:06 +0000145
Chris Lattner85759272009-03-11 00:23:13 +0000146/// getNumPlusOperands - Return the number of output operands that have a "+"
147/// constraint.
148unsigned AsmStmt::getNumPlusOperands() const {
149 unsigned Res = 0;
150 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
151 if (isOutputPlusConstraint(i))
152 ++Res;
153 return Res;
154}
155
156
Chris Lattnerb3277932009-03-10 04:59:06 +0000157
158Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000159 return cast<Expr>(Exprs[i + NumOutputs]);
160}
Chris Lattnerb3277932009-03-10 04:59:06 +0000161
162/// getInputConstraint - Return the specified input constraint. Unlike output
163/// constraints, these can be empty.
164std::string AsmStmt::getInputConstraint(unsigned i) const {
165 return std::string(Constraints[i + NumOutputs]->getStrData(),
166 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000167}
168
Chris Lattner10ca96a2009-03-10 06:33:24 +0000169
170/// getNamedOperand - Given a symbolic operand reference like %[foo],
171/// translate this into a numeric value needed to reference the same operand.
172/// This returns -1 if the operand name is invalid.
173int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
174 unsigned NumPlusOperands = 0;
175
176 // Check if this is an output operand.
177 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
178 if (getOutputName(i) == SymbolicName)
179 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000180 }
181
182 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
183 if (getInputName(i) == SymbolicName)
184 return getNumOutputs() + NumPlusOperands + i;
185
186 // Not found.
187 return -1;
188}
189
190
Chris Lattner458cd9c2009-03-10 23:21:44 +0000191/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
192/// it into pieces. If the asm string is erroneous, emit errors and return
193/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000194unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
195 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000196 const char *StrStart = getAsmString()->getStrData();
197 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000198 const char *CurPtr = StrStart;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000199
200 // "Simple" inline asms have no constraints or operands, just convert the asm
201 // string to escape $'s.
202 if (isSimple()) {
203 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000204 for (; CurPtr != StrEnd; ++CurPtr) {
205 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000206 case '$':
207 Result += "$$";
208 break;
209 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000210 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000211 break;
212 }
213 }
214 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000215 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000216 }
217
218 // CurStringPiece - The current string that we are building up as we scan the
219 // asm string.
220 std::string CurStringPiece;
221
222 while (1) {
223 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000224 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000225 if (!CurStringPiece.empty())
226 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000227 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000228 }
229
Chris Lattner3182db12009-03-10 23:51:40 +0000230 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000231 if (CurChar == '$') {
232 CurStringPiece += "$$";
233 continue;
234 } else if (CurChar != '%') {
235 CurStringPiece += CurChar;
236 continue;
237 }
238
239 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000240 if (CurPtr == StrEnd) {
241 // % at end of string is invalid (no escape).
242 DiagOffs = CurPtr-StrStart-1;
243 return diag::err_asm_invalid_escape;
244 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000245
Chris Lattner3182db12009-03-10 23:51:40 +0000246 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000247 if (EscapedChar == '%') { // %% -> %
248 // Escaped percentage sign.
249 CurStringPiece += '%';
250 continue;
251 }
252
253 if (EscapedChar == '=') { // %= -> Generate an unique ID.
254 CurStringPiece += "${:uid}";
255 continue;
256 }
257
258 // Otherwise, we have an operand. If we have accumulated a string so far,
259 // add it to the Pieces list.
260 if (!CurStringPiece.empty()) {
261 Pieces.push_back(AsmStringPiece(CurStringPiece));
262 CurStringPiece.clear();
263 }
264
265 // Handle %x4 and %x[foo] by capturing x as the modifier character.
266 char Modifier = '\0';
267 if (isalpha(EscapedChar)) {
268 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000269 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000270 }
271
272 if (isdigit(EscapedChar)) {
273 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000274 unsigned N = 0;
275
276 --CurPtr;
277 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000278 N = N*10 + ((*CurPtr++)-'0');
Chris Lattner85759272009-03-11 00:23:13 +0000279
280 unsigned NumOperands =
281 getNumOutputs() + getNumPlusOperands() + getNumInputs();
282 if (N >= NumOperands) {
283 DiagOffs = CurPtr-StrStart-1;
284 return diag::err_asm_invalid_operand_number;
285 }
286
Chris Lattner458cd9c2009-03-10 23:21:44 +0000287 Pieces.push_back(AsmStringPiece(N, Modifier));
288 continue;
289 }
290
291 // Handle %[foo], a symbolic operand reference.
292 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000293 DiagOffs = CurPtr-StrStart-1;
294
295 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000296 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000297 if (NameEnd == 0)
298 return diag::err_asm_unterminated_symbolic_operand_name;
299 if (NameEnd == CurPtr)
300 return diag::err_asm_empty_symbolic_operand_name;
301
Chris Lattner3182db12009-03-10 23:51:40 +0000302 std::string SymbolicName(CurPtr, NameEnd);
Chris Lattner458cd9c2009-03-10 23:21:44 +0000303
304 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000305 if (N == -1) {
306 // Verify that an operand with that name exists.
307 DiagOffs = CurPtr-StrStart;
308 return diag::err_asm_unknown_symbolic_operand_name;
309 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000310 Pieces.push_back(AsmStringPiece(N, Modifier));
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000311
312 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000313 continue;
314 }
315
Chris Lattner2ff0f422009-03-10 23:57:07 +0000316 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000317 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000318 }
319}
320
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000321//===----------------------------------------------------------------------===//
322// Constructors
323//===----------------------------------------------------------------------===//
324
Anders Carlssondfab34a2008-02-05 23:03:50 +0000325AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000326 unsigned numoutputs, unsigned numinputs,
327 std::string *names, StringLiteral **constraints,
328 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
329 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000330 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000331 , IsSimple(issimple), IsVolatile(isvolatile)
332 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000333 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
334 Names.push_back(names[i]);
335 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000336 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000337 }
Nico Weber608b17f2008-08-05 23:15:29 +0000338
Anders Carlssonb235fc22007-11-22 01:36:19 +0000339 for (unsigned i = 0; i != numclobbers; i++)
340 Clobbers.push_back(clobbers[i]);
341}
342
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000343ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
344 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000345 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000346: Stmt(ObjCForCollectionStmtClass) {
347 SubExprs[ELEM] = Elem;
348 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
349 SubExprs[BODY] = Body;
350 ForLoc = FCL;
351 RParenLoc = RPL;
352}
353
354
Nico Weber608b17f2008-08-05 23:15:29 +0000355ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
356 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000357 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000358 Stmt *atCatchList)
359: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000360 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000361 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000362 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000363 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000364 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000365 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
366
Nico Weber608b17f2008-08-05 23:15:29 +0000367 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000368 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000369
Ted Kremenekff981022008-02-01 21:28:59 +0000370 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000371 }
372 AtCatchLoc = atCatchLoc;
373 RParenLoc = rparenloc;
374}
375
376
Ted Kremenek82977772007-08-24 21:09:09 +0000377//===----------------------------------------------------------------------===//
378// Child Iterators for iterating over subexpressions/substatements
379//===----------------------------------------------------------------------===//
380
381// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000382Stmt::child_iterator DeclStmt::child_begin() {
383 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000384}
385
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000386Stmt::child_iterator DeclStmt::child_end() {
387 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000388}
389
Ted Kremenek82977772007-08-24 21:09:09 +0000390// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000391Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
392Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000393
394// CompoundStmt
395Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000396Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000397
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000398// CaseStmt
399Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
400Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
401
402// DefaultStmt
403Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
404Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000405
406// LabelStmt
407Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000408Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000409
410// IfStmt
411Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
412Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
413
414// SwitchStmt
415Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
416Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
417
418// WhileStmt
419Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
420Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
421
422// DoStmt
423Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
424Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
425
426// ForStmt
427Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
428Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
429
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000430// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000431Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
432 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000433}
Nico Weber608b17f2008-08-05 23:15:29 +0000434Stmt::child_iterator ObjCForCollectionStmt::child_end() {
435 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000436}
437
Ted Kremenek82977772007-08-24 21:09:09 +0000438// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000439Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
440Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000441
442// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000443Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
444const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000445
Ted Kremenek1060aff2008-06-17 03:11:08 +0000446Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
447Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000448
449// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000450Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
451Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000452
453// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000454Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
455Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000456
457// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000458const Expr* ReturnStmt::getRetValue() const {
459 return cast_or_null<Expr>(RetExpr);
460}
461Expr* ReturnStmt::getRetValue() {
462 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000463}
464
Ted Kremenek1060aff2008-06-17 03:11:08 +0000465Stmt::child_iterator ReturnStmt::child_begin() {
466 return &RetExpr;
467}
468Stmt::child_iterator ReturnStmt::child_end() {
469 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000470}
Ted Kremenek82977772007-08-24 21:09:09 +0000471
Chris Lattnerfe795952007-10-29 04:04:16 +0000472// AsmStmt
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000473Stmt::child_iterator AsmStmt::child_begin() {
474 return Exprs.empty() ? 0 : &Exprs[0];
475}
476Stmt::child_iterator AsmStmt::child_end() {
477 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
478}
Chris Lattnerfe795952007-10-29 04:04:16 +0000479
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000480// ObjCAtCatchStmt
481Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000482Stmt::child_iterator ObjCAtCatchStmt::child_end() {
483 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000484}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000485
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000486// ObjCAtFinallyStmt
487Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
488Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000489
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000490// ObjCAtTryStmt
491Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000492Stmt::child_iterator ObjCAtTryStmt::child_end() {
493 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000494}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000495
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000496// ObjCAtThrowStmt
497Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000498 return &Throw;
499}
500
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000501Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000502 return &Throw+1;
503}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000504
505// ObjCAtSynchronizedStmt
506Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000507 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000508}
509
510Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000511 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000512}
513
Sebastian Redl4b07b292008-12-22 19:15:10 +0000514// CXXCatchStmt
515Stmt::child_iterator CXXCatchStmt::child_begin() {
516 return &HandlerBlock;
517}
518
519Stmt::child_iterator CXXCatchStmt::child_end() {
520 return &HandlerBlock + 1;
521}
522
523QualType CXXCatchStmt::getCaughtType() {
524 if (ExceptionDecl)
525 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
526 return QualType();
527}
528
529void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000530 if (ExceptionDecl)
531 ExceptionDecl->Destroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000532 Stmt::Destroy(C);
533}
Sebastian Redl8351da02008-12-22 21:35:02 +0000534
535// CXXTryStmt
536Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
537Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
538
539CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
540 Stmt **handlers, unsigned numHandlers)
541 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
542 Stmts.push_back(tryBlock);
543 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
544}