blob: 0cf36f98a83d6d94b5dee0237c55392e2684e7af [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Stmt.h"
15#include "clang/AST/ExprCXX.h"
Steve Naroff9ed3e772008-05-29 21:12:08 +000016#include "clang/AST/ExprObjC.h"
Sebastian Redl743c8162008-12-22 19:15:10 +000017#include "clang/AST/Type.h"
Ted Kremenek39dc20a2009-02-06 01:42:09 +000018#include "clang/AST/ASTContext.h"
Chris Lattnerd4dd42c2009-03-10 23:51:40 +000019#include "clang/AST/ASTDiagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattner4b009652007-07-25 00:24:17 +000022static struct StmtClassNameTable {
Chris Lattner603bf122007-08-25 01:42:24 +000023 const char *Name;
24 unsigned Counter;
25 unsigned Size;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000026} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner603bf122007-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 Gregor19330152008-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);
Chris Lattner4b009652007-07-25 00:24:17 +000038#include "clang/AST/StmtNodes.def"
Nico Weber7340a2f2008-08-05 23:15:29 +000039
Chris Lattner603bf122007-08-25 01:42:24 +000040 return StmtClassInfo[E];
41}
42
Chris Lattner4b009652007-07-25 00:24:17 +000043const char *Stmt::getStmtClassName() const {
Chris Lattner603bf122007-08-25 01:42:24 +000044 return getStmtInfoTableEntry(sClass).Name;
Chris Lattner4b009652007-07-25 00:24:17 +000045}
46
Chris Lattnerdaac6942009-03-04 04:23:07 +000047void Stmt::DestroyChildren(ASTContext &C) {
48 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
Douglas Gregor0e7e7f42009-01-16 06:50:08 +000049 if (Stmt* Child = *I++) Child->Destroy(C);
Ted Kremenekafdf8112008-05-20 00:43:19 +000050}
51
Chris Lattnerdaac6942009-03-04 04:23:07 +000052void Stmt::Destroy(ASTContext &C) {
Ted Kremenekafdf8112008-05-20 00:43:19 +000053 DestroyChildren(C);
Ted Kremenekbde41092008-05-20 04:10:52 +000054 // FIXME: Eventually all Stmts should be allocated with the allocator
55 // in ASTContext, just like with Decls.
Ted Kremenek39dc20a2009-02-06 01:42:09 +000056 this->~Stmt();
57 C.Deallocate((void *)this);
Ted Kremenek1ed5dc62008-05-19 22:02:12 +000058}
59
Chris Lattnerdaac6942009-03-04 04:23:07 +000060void DeclStmt::Destroy(ASTContext &C) {
Ted Kremenek39dc20a2009-02-06 01:42:09 +000061 this->~DeclStmt();
62 C.Deallocate((void *)this);
Ted Kremeneka1e77702008-05-21 15:53:55 +000063}
64
Chris Lattner4b009652007-07-25 00:24:17 +000065void Stmt::PrintStats() {
Chris Lattner603bf122007-08-25 01:42:24 +000066 // Ensure the table is primed.
67 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber7340a2f2008-08-05 23:15:29 +000068
Chris Lattner4b009652007-07-25 00:24:17 +000069 unsigned sum = 0;
70 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner9c0da3b2007-08-25 01:55:00 +000071 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000072 if (StmtClassInfo[i].Name == 0) continue;
73 sum += StmtClassInfo[i].Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000074 }
75 fprintf(stderr, " %d stmts/exprs total.\n", sum);
76 sum = 0;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000077 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000078 if (StmtClassInfo[i].Name == 0) continue;
Nico Weber7340a2f2008-08-05 23:15:29 +000079 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner603bf122007-08-25 01:42:24 +000080 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
81 StmtClassInfo[i].Size,
82 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
83 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Chris Lattner4b009652007-07-25 00:24:17 +000084 }
85 fprintf(stderr, "Total bytes = %d\n", sum);
86}
87
88void Stmt::addStmtClass(StmtClass s) {
Chris Lattner603bf122007-08-25 01:42:24 +000089 ++getStmtInfoTableEntry(s).Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000090}
91
92static bool StatSwitch = false;
93
94bool Stmt::CollectingStats(bool enable) {
95 if (enable) StatSwitch = true;
Chris Lattner603bf122007-08-25 01:42:24 +000096 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000097}
98
99
Chris Lattner4b009652007-07-25 00:24:17 +0000100const char *LabelStmt::getName() const {
101 return getID()->getName();
102}
103
Steve Naroffc32a20d2007-08-31 23:49:30 +0000104// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber7340a2f2008-08-05 23:15:29 +0000105SourceRange ReturnStmt::getSourceRange() const {
Steve Naroffc32a20d2007-08-31 23:49:30 +0000106 if (RetExpr)
107 return SourceRange(RetLoc, RetExpr->getLocEnd());
108 else
109 return SourceRange(RetLoc);
110}
111
Ted Kremenekad5682f2007-10-01 16:34:52 +0000112bool Stmt::hasImplicitControlFlow() const {
113 switch (sClass) {
114 default:
115 return false;
Nico Weber7340a2f2008-08-05 23:15:29 +0000116
Ted Kremenekad5682f2007-10-01 16:34:52 +0000117 case CallExprClass:
118 case ConditionalOperatorClass:
119 case ChooseExprClass:
120 case StmtExprClass:
121 case DeclStmtClass:
Nico Weber7340a2f2008-08-05 23:15:29 +0000122 return true;
123
Ted Kremenekad5682f2007-10-01 16:34:52 +0000124 case Stmt::BinaryOperatorClass: {
125 const BinaryOperator* B = cast<BinaryOperator>(this);
126 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
127 return true;
128 else
129 return false;
130 }
131 }
132}
133
Chris Lattnere8625112009-03-10 04:59:06 +0000134Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekb30de272008-10-27 18:40:21 +0000135 return cast<Expr>(Exprs[i]);
136}
Chris Lattnere8625112009-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 Kremenekb30de272008-10-27 18:40:21 +0000144}
Chris Lattnere8625112009-03-10 04:59:06 +0000145
Chris Lattner5b132432009-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 Lattnere8625112009-03-10 04:59:06 +0000157
158Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekb30de272008-10-27 18:40:21 +0000159 return cast<Expr>(Exprs[i + NumOutputs]);
160}
Chris Lattnere8625112009-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 Kremenekb30de272008-10-27 18:40:21 +0000167}
168
Chris Lattner20ac04c2009-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;
180
181 // Keep track of the number of '+' operands.
182 if (isOutputPlusConstraint(i)) ++NumPlusOperands;
183 }
184
185 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
186 if (getInputName(i) == SymbolicName)
187 return getNumOutputs() + NumPlusOperands + i;
188
189 // Not found.
190 return -1;
191}
192
193
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000194/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
195/// it into pieces. If the asm string is erroneous, emit errors and return
196/// true, otherwise return false.
Chris Lattnerc5164732009-03-10 23:41:04 +0000197unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
198 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000199 const char *StrStart = getAsmString()->getStrData();
200 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000201 const char *CurPtr = StrStart;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000202
203 // "Simple" inline asms have no constraints or operands, just convert the asm
204 // string to escape $'s.
205 if (isSimple()) {
206 std::string Result;
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000207 for (; CurPtr != StrEnd; ++CurPtr) {
208 switch (*CurPtr) {
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000209 case '$':
210 Result += "$$";
211 break;
212 default:
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000213 Result += *CurPtr;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000214 break;
215 }
216 }
217 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000218 return 0;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000219 }
220
221 // CurStringPiece - The current string that we are building up as we scan the
222 // asm string.
223 std::string CurStringPiece;
224
225 while (1) {
226 // Done with the string?
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000227 if (CurPtr == StrEnd) {
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000228 if (!CurStringPiece.empty())
229 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000230 return 0;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000231 }
232
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000233 char CurChar = *CurPtr++;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000234 if (CurChar == '$') {
235 CurStringPiece += "$$";
236 continue;
237 } else if (CurChar != '%') {
238 CurStringPiece += CurChar;
239 continue;
240 }
241
242 // Escaped "%" character in asm string.
Chris Lattnera7f22702009-03-11 00:06:36 +0000243 if (CurPtr == StrEnd) {
244 // % at end of string is invalid (no escape).
245 DiagOffs = CurPtr-StrStart-1;
246 return diag::err_asm_invalid_escape;
247 }
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000248
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000249 char EscapedChar = *CurPtr++;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000250 if (EscapedChar == '%') { // %% -> %
251 // Escaped percentage sign.
252 CurStringPiece += '%';
253 continue;
254 }
255
256 if (EscapedChar == '=') { // %= -> Generate an unique ID.
257 CurStringPiece += "${:uid}";
258 continue;
259 }
260
261 // Otherwise, we have an operand. If we have accumulated a string so far,
262 // add it to the Pieces list.
263 if (!CurStringPiece.empty()) {
264 Pieces.push_back(AsmStringPiece(CurStringPiece));
265 CurStringPiece.clear();
266 }
267
268 // Handle %x4 and %x[foo] by capturing x as the modifier character.
269 char Modifier = '\0';
270 if (isalpha(EscapedChar)) {
271 Modifier = EscapedChar;
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000272 EscapedChar = *CurPtr++;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000273 }
274
275 if (isdigit(EscapedChar)) {
276 // %n - Assembler operand n
Chris Lattner3f402202009-03-11 22:52:17 +0000277 unsigned N = 0;
278
279 --CurPtr;
280 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner9e09e962009-03-11 23:09:16 +0000281 N = N*10 + ((*CurPtr++)-'0');
Chris Lattner5b132432009-03-11 00:23:13 +0000282
283 unsigned NumOperands =
284 getNumOutputs() + getNumPlusOperands() + getNumInputs();
285 if (N >= NumOperands) {
286 DiagOffs = CurPtr-StrStart-1;
287 return diag::err_asm_invalid_operand_number;
288 }
289
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000290 Pieces.push_back(AsmStringPiece(N, Modifier));
291 continue;
292 }
293
294 // Handle %[foo], a symbolic operand reference.
295 if (EscapedChar == '[') {
Chris Lattnera7f22702009-03-11 00:06:36 +0000296 DiagOffs = CurPtr-StrStart-1;
297
298 // Find the ']'.
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000299 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnera7f22702009-03-11 00:06:36 +0000300 if (NameEnd == 0)
301 return diag::err_asm_unterminated_symbolic_operand_name;
302 if (NameEnd == CurPtr)
303 return diag::err_asm_empty_symbolic_operand_name;
304
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000305 std::string SymbolicName(CurPtr, NameEnd);
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000306
307 int N = getNamedOperand(SymbolicName);
Chris Lattnera7f22702009-03-11 00:06:36 +0000308 if (N == -1) {
309 // Verify that an operand with that name exists.
310 DiagOffs = CurPtr-StrStart;
311 return diag::err_asm_unknown_symbolic_operand_name;
312 }
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000313 Pieces.push_back(AsmStringPiece(N, Modifier));
Chris Lattnera7f22702009-03-11 00:06:36 +0000314
315 CurPtr = NameEnd+1;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000316 continue;
317 }
318
Chris Lattnerc0da38d2009-03-10 23:57:07 +0000319 DiagOffs = CurPtr-StrStart-1;
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000320 return diag::err_asm_invalid_escape;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000321 }
322}
323
Chris Lattner006d1542008-01-30 05:01:46 +0000324//===----------------------------------------------------------------------===//
325// Constructors
326//===----------------------------------------------------------------------===//
327
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000328AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattner006d1542008-01-30 05:01:46 +0000329 unsigned numoutputs, unsigned numinputs,
330 std::string *names, StringLiteral **constraints,
331 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
332 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlsson965d5202007-11-22 01:36:19 +0000333 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000334 , IsSimple(issimple), IsVolatile(isvolatile)
335 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson965d5202007-11-22 01:36:19 +0000336 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
337 Names.push_back(names[i]);
338 Exprs.push_back(exprs[i]);
Nico Weber7340a2f2008-08-05 23:15:29 +0000339 Constraints.push_back(constraints[i]);
Anders Carlsson965d5202007-11-22 01:36:19 +0000340 }
Nico Weber7340a2f2008-08-05 23:15:29 +0000341
Anders Carlsson965d5202007-11-22 01:36:19 +0000342 for (unsigned i = 0; i != numclobbers; i++)
343 Clobbers.push_back(clobbers[i]);
344}
345
Chris Lattner006d1542008-01-30 05:01:46 +0000346ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
347 Stmt *Body, SourceLocation FCL,
Nico Weber7340a2f2008-08-05 23:15:29 +0000348 SourceLocation RPL)
Chris Lattner006d1542008-01-30 05:01:46 +0000349: Stmt(ObjCForCollectionStmtClass) {
350 SubExprs[ELEM] = Elem;
351 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
352 SubExprs[BODY] = Body;
353 ForLoc = FCL;
354 RParenLoc = RPL;
355}
356
357
Nico Weber7340a2f2008-08-05 23:15:29 +0000358ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
359 SourceLocation rparenloc,
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000360 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattner006d1542008-01-30 05:01:46 +0000361 Stmt *atCatchList)
362: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000363 ExceptionDecl = catchVarDecl;
Chris Lattner006d1542008-01-30 05:01:46 +0000364 SubExprs[BODY] = atCatchStmt;
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000365 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbard23d9922009-03-01 04:28:32 +0000366 // FIXME: O(N^2) in number of catch blocks.
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000367 if (atCatchList) {
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000368 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
369
Nico Weber7340a2f2008-08-05 23:15:29 +0000370 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000371 AtCatchList = NextCatch;
Nico Weber7340a2f2008-08-05 23:15:29 +0000372
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000373 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner006d1542008-01-30 05:01:46 +0000374 }
375 AtCatchLoc = atCatchLoc;
376 RParenLoc = rparenloc;
377}
378
379
Ted Kremenek51be0562007-08-24 21:09:09 +0000380//===----------------------------------------------------------------------===//
381// Child Iterators for iterating over subexpressions/substatements
382//===----------------------------------------------------------------------===//
383
384// DeclStmt
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000385Stmt::child_iterator DeclStmt::child_begin() {
386 return StmtIterator(DG.begin(), DG.end());
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000387}
388
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000389Stmt::child_iterator DeclStmt::child_end() {
390 return StmtIterator(DG.end(), DG.end());
Ted Kremenek62f23bb2008-10-06 20:54:44 +0000391}
392
Ted Kremenek51be0562007-08-24 21:09:09 +0000393// NullStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000394Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
395Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000396
397// CompoundStmt
398Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek0c97e042009-02-07 01:47:29 +0000399Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000400
Ted Kremeneke07b67e2007-08-30 16:50:46 +0000401// CaseStmt
402Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
403Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
404
405// DefaultStmt
406Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
407Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000408
409// LabelStmt
410Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000411Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000412
413// IfStmt
414Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
415Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
416
417// SwitchStmt
418Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
419Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
420
421// WhileStmt
422Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
423Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
424
425// DoStmt
426Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
427Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
428
429// ForStmt
430Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
431Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
432
Ted Kremenek42730c52008-01-07 19:49:32 +0000433// ObjCForCollectionStmt
Nico Weber7340a2f2008-08-05 23:15:29 +0000434Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
435 return &SubExprs[0];
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000436}
Nico Weber7340a2f2008-08-05 23:15:29 +0000437Stmt::child_iterator ObjCForCollectionStmt::child_end() {
438 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000439}
440
Ted Kremenek51be0562007-08-24 21:09:09 +0000441// GotoStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000442Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
443Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000444
445// IndirectGotoStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000446Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
447const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000448
Ted Kremenek156714e2008-06-17 03:11:08 +0000449Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
450Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000451
452// ContinueStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000453Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
454Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000455
456// BreakStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000457Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
458Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000459
460// ReturnStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000461const Expr* ReturnStmt::getRetValue() const {
462 return cast_or_null<Expr>(RetExpr);
463}
464Expr* ReturnStmt::getRetValue() {
465 return cast_or_null<Expr>(RetExpr);
Ted Kremenek51be0562007-08-24 21:09:09 +0000466}
467
Ted Kremenek156714e2008-06-17 03:11:08 +0000468Stmt::child_iterator ReturnStmt::child_begin() {
469 return &RetExpr;
470}
471Stmt::child_iterator ReturnStmt::child_end() {
472 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000473}
Ted Kremenek51be0562007-08-24 21:09:09 +0000474
Chris Lattner8a40a832007-10-29 04:04:16 +0000475// AsmStmt
Ted Kremenekb30de272008-10-27 18:40:21 +0000476Stmt::child_iterator AsmStmt::child_begin() {
477 return Exprs.empty() ? 0 : &Exprs[0];
478}
479Stmt::child_iterator AsmStmt::child_end() {
480 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
481}
Chris Lattner8a40a832007-10-29 04:04:16 +0000482
Ted Kremenek42730c52008-01-07 19:49:32 +0000483// ObjCAtCatchStmt
484Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000485Stmt::child_iterator ObjCAtCatchStmt::child_end() {
486 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000487}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000488
Ted Kremenek42730c52008-01-07 19:49:32 +0000489// ObjCAtFinallyStmt
490Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
491Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000492
Ted Kremenek42730c52008-01-07 19:49:32 +0000493// ObjCAtTryStmt
494Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000495Stmt::child_iterator ObjCAtTryStmt::child_end() {
496 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000497}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000498
Ted Kremenek42730c52008-01-07 19:49:32 +0000499// ObjCAtThrowStmt
500Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000501 return &Throw;
502}
503
Ted Kremenek42730c52008-01-07 19:49:32 +0000504Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000505 return &Throw+1;
506}
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000507
508// ObjCAtSynchronizedStmt
509Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000510 return &SubStmts[0];
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000511}
512
513Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000514 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000515}
516
Sebastian Redl743c8162008-12-22 19:15:10 +0000517// CXXCatchStmt
518Stmt::child_iterator CXXCatchStmt::child_begin() {
519 return &HandlerBlock;
520}
521
522Stmt::child_iterator CXXCatchStmt::child_end() {
523 return &HandlerBlock + 1;
524}
525
526QualType CXXCatchStmt::getCaughtType() {
527 if (ExceptionDecl)
528 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
529 return QualType();
530}
531
532void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl237116b2008-12-22 21:35:02 +0000533 if (ExceptionDecl)
534 ExceptionDecl->Destroy(C);
Sebastian Redl743c8162008-12-22 19:15:10 +0000535 Stmt::Destroy(C);
536}
Sebastian Redl237116b2008-12-22 21:35:02 +0000537
538// CXXTryStmt
539Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
540Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
541
542CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
543 Stmt **handlers, unsigned numHandlers)
544 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
545 Stmts.push_back(tryBlock);
546 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
547}