blob: cd8fba6b7a16b7107bef2fa7e82d3a50ff736411 [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"
Chris Lattner4a9e9272009-04-26 01:32:48 +000017#include "clang/AST/StmtCXX.h"
18#include "clang/AST/StmtObjC.h"
Sebastian Redl743c8162008-12-22 19:15:10 +000019#include "clang/AST/Type.h"
Ted Kremenek39dc20a2009-02-06 01:42:09 +000020#include "clang/AST/ASTContext.h"
Chris Lattnerd4dd42c2009-03-10 23:51:40 +000021#include "clang/AST/ASTDiagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022using namespace clang;
23
Chris Lattner4b009652007-07-25 00:24:17 +000024static struct StmtClassNameTable {
Chris Lattner603bf122007-08-25 01:42:24 +000025 const char *Name;
26 unsigned Counter;
27 unsigned Size;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000028} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner603bf122007-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 Gregor19330152008-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);
Chris Lattner4b009652007-07-25 00:24:17 +000040#include "clang/AST/StmtNodes.def"
Nico Weber7340a2f2008-08-05 23:15:29 +000041
Chris Lattner603bf122007-08-25 01:42:24 +000042 return StmtClassInfo[E];
43}
44
Chris Lattner4b009652007-07-25 00:24:17 +000045const char *Stmt::getStmtClassName() const {
Douglas Gregora5d06282009-08-08 01:41:12 +000046 return getStmtInfoTableEntry((StmtClass)sClass).Name;
Chris Lattner4b009652007-07-25 00:24:17 +000047}
48
Chris Lattnerdaac6942009-03-04 04:23:07 +000049void Stmt::DestroyChildren(ASTContext &C) {
50 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
Douglas Gregor0e7e7f42009-01-16 06:50:08 +000051 if (Stmt* Child = *I++) Child->Destroy(C);
Ted Kremenekafdf8112008-05-20 00:43:19 +000052}
53
Douglas Gregor53e8e4b2009-08-07 06:08:38 +000054void Stmt::DoDestroy(ASTContext &C) {
Ted Kremenekafdf8112008-05-20 00:43:19 +000055 DestroyChildren(C);
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 Lattner4b009652007-07-25 00:24:17 +000060void Stmt::PrintStats() {
Chris Lattner603bf122007-08-25 01:42:24 +000061 // Ensure the table is primed.
62 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber7340a2f2008-08-05 23:15:29 +000063
Chris Lattner4b009652007-07-25 00:24:17 +000064 unsigned sum = 0;
65 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner9c0da3b2007-08-25 01:55:00 +000066 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000067 if (StmtClassInfo[i].Name == 0) continue;
68 sum += StmtClassInfo[i].Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000069 }
70 fprintf(stderr, " %d stmts/exprs total.\n", sum);
71 sum = 0;
Chris Lattner9c0da3b2007-08-25 01:55:00 +000072 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner603bf122007-08-25 01:42:24 +000073 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregore6609442009-05-26 14:40:08 +000074 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weber7340a2f2008-08-05 23:15:29 +000075 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner603bf122007-08-25 01:42:24 +000076 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
77 StmtClassInfo[i].Size,
78 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
79 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Chris Lattner4b009652007-07-25 00:24:17 +000080 }
81 fprintf(stderr, "Total bytes = %d\n", sum);
82}
83
84void Stmt::addStmtClass(StmtClass s) {
Chris Lattner603bf122007-08-25 01:42:24 +000085 ++getStmtInfoTableEntry(s).Counter;
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
88static bool StatSwitch = false;
89
90bool Stmt::CollectingStats(bool enable) {
91 if (enable) StatSwitch = true;
Chris Lattner603bf122007-08-25 01:42:24 +000092 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000093}
94
Douglas Gregora5d06282009-08-08 01:41:12 +000095void SwitchStmt::DoDestroy(ASTContext &Ctx) {
96 // Destroy the SwitchCase statements in this switch. In the normal
97 // case, this loop will merely decrement the reference counts from
98 // the Retain() calls in addSwitchCase();
99 SwitchCase *SC = FirstCase;
100 while (SC) {
101 SwitchCase *Next = SC->getNextSwitchCase();
102 SC->Destroy(Ctx);
103 SC = Next;
104 }
105
106 Stmt::DoDestroy(Ctx);
107}
108
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000109void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
110 if (this->Body)
111 C.Deallocate(Body);
112 this->NumStmts = NumStmts;
113
114 Body = new (C) Stmt*[NumStmts];
115 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
116}
Chris Lattner4b009652007-07-25 00:24:17 +0000117
Chris Lattner4b009652007-07-25 00:24:17 +0000118const char *LabelStmt::getName() const {
119 return getID()->getName();
120}
121
Steve Naroffc32a20d2007-08-31 23:49:30 +0000122// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber7340a2f2008-08-05 23:15:29 +0000123SourceRange ReturnStmt::getSourceRange() const {
Steve Naroffc32a20d2007-08-31 23:49:30 +0000124 if (RetExpr)
125 return SourceRange(RetLoc, RetExpr->getLocEnd());
126 else
127 return SourceRange(RetLoc);
128}
129
Ted Kremenekad5682f2007-10-01 16:34:52 +0000130bool Stmt::hasImplicitControlFlow() const {
131 switch (sClass) {
132 default:
133 return false;
Nico Weber7340a2f2008-08-05 23:15:29 +0000134
Ted Kremenekad5682f2007-10-01 16:34:52 +0000135 case CallExprClass:
136 case ConditionalOperatorClass:
137 case ChooseExprClass:
138 case StmtExprClass:
139 case DeclStmtClass:
Nico Weber7340a2f2008-08-05 23:15:29 +0000140 return true;
141
Ted Kremenekad5682f2007-10-01 16:34:52 +0000142 case Stmt::BinaryOperatorClass: {
143 const BinaryOperator* B = cast<BinaryOperator>(this);
144 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
145 return true;
146 else
147 return false;
148 }
149 }
150}
151
Chris Lattnere8625112009-03-10 04:59:06 +0000152Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekb30de272008-10-27 18:40:21 +0000153 return cast<Expr>(Exprs[i]);
154}
Chris Lattnere8625112009-03-10 04:59:06 +0000155
156/// getOutputConstraint - Return the constraint string for the specified
157/// output operand. All output constraints are known to be non-empty (either
158/// '=' or '+').
159std::string AsmStmt::getOutputConstraint(unsigned i) const {
160 return std::string(Constraints[i]->getStrData(),
161 Constraints[i]->getByteLength());
Ted Kremenekb30de272008-10-27 18:40:21 +0000162}
Chris Lattnere8625112009-03-10 04:59:06 +0000163
Chris Lattner5b132432009-03-11 00:23:13 +0000164/// getNumPlusOperands - Return the number of output operands that have a "+"
165/// constraint.
166unsigned AsmStmt::getNumPlusOperands() const {
167 unsigned Res = 0;
168 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
169 if (isOutputPlusConstraint(i))
170 ++Res;
171 return Res;
172}
173
174
Chris Lattnere8625112009-03-10 04:59:06 +0000175
176Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekb30de272008-10-27 18:40:21 +0000177 return cast<Expr>(Exprs[i + NumOutputs]);
178}
Chris Lattnere8625112009-03-10 04:59:06 +0000179
180/// getInputConstraint - Return the specified input constraint. Unlike output
181/// constraints, these can be empty.
182std::string AsmStmt::getInputConstraint(unsigned i) const {
183 return std::string(Constraints[i + NumOutputs]->getStrData(),
184 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekb30de272008-10-27 18:40:21 +0000185}
186
Chris Lattner20ac04c2009-03-10 06:33:24 +0000187
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000188void AsmStmt::setOutputsAndInputs(unsigned NumOutputs,
189 unsigned NumInputs,
190 const std::string *Names,
191 StringLiteral **Constraints,
192 Stmt **Exprs) {
193 this->NumOutputs = NumOutputs;
194 this->NumInputs = NumInputs;
195 this->Names.clear();
196 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
197 this->Constraints.clear();
198 this->Constraints.insert(this->Constraints.end(),
199 Constraints, Constraints + NumOutputs + NumInputs);
200 this->Exprs.clear();
201 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
202}
203
Chris Lattner20ac04c2009-03-10 06:33:24 +0000204/// getNamedOperand - Given a symbolic operand reference like %[foo],
205/// translate this into a numeric value needed to reference the same operand.
206/// This returns -1 if the operand name is invalid.
207int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
208 unsigned NumPlusOperands = 0;
209
210 // Check if this is an output operand.
211 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
212 if (getOutputName(i) == SymbolicName)
213 return i;
Chris Lattner20ac04c2009-03-10 06:33:24 +0000214 }
215
216 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
217 if (getInputName(i) == SymbolicName)
218 return getNumOutputs() + NumPlusOperands + i;
219
220 // Not found.
221 return -1;
222}
223
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000224void AsmStmt::setClobbers(StringLiteral **Clobbers, unsigned NumClobbers) {
225 this->Clobbers.clear();
226 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
227}
Chris Lattner20ac04c2009-03-10 06:33:24 +0000228
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000229/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
230/// it into pieces. If the asm string is erroneous, emit errors and return
231/// true, otherwise return false.
Chris Lattnerc5164732009-03-10 23:41:04 +0000232unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
233 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000234 const char *StrStart = getAsmString()->getStrData();
235 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000236 const char *CurPtr = StrStart;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000237
238 // "Simple" inline asms have no constraints or operands, just convert the asm
239 // string to escape $'s.
240 if (isSimple()) {
241 std::string Result;
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000242 for (; CurPtr != StrEnd; ++CurPtr) {
243 switch (*CurPtr) {
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000244 case '$':
245 Result += "$$";
246 break;
247 default:
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000248 Result += *CurPtr;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000249 break;
250 }
251 }
252 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000253 return 0;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000254 }
255
256 // CurStringPiece - The current string that we are building up as we scan the
257 // asm string.
258 std::string CurStringPiece;
259
260 while (1) {
261 // Done with the string?
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000262 if (CurPtr == StrEnd) {
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000263 if (!CurStringPiece.empty())
264 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000265 return 0;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000266 }
267
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000268 char CurChar = *CurPtr++;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000269 if (CurChar == '$') {
270 CurStringPiece += "$$";
271 continue;
272 } else if (CurChar != '%') {
273 CurStringPiece += CurChar;
274 continue;
275 }
276
277 // Escaped "%" character in asm string.
Chris Lattnera7f22702009-03-11 00:06:36 +0000278 if (CurPtr == StrEnd) {
279 // % at end of string is invalid (no escape).
280 DiagOffs = CurPtr-StrStart-1;
281 return diag::err_asm_invalid_escape;
282 }
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000283
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000284 char EscapedChar = *CurPtr++;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000285 if (EscapedChar == '%') { // %% -> %
286 // Escaped percentage sign.
287 CurStringPiece += '%';
288 continue;
289 }
290
291 if (EscapedChar == '=') { // %= -> Generate an unique ID.
292 CurStringPiece += "${:uid}";
293 continue;
294 }
295
296 // Otherwise, we have an operand. If we have accumulated a string so far,
297 // add it to the Pieces list.
298 if (!CurStringPiece.empty()) {
299 Pieces.push_back(AsmStringPiece(CurStringPiece));
300 CurStringPiece.clear();
301 }
302
303 // Handle %x4 and %x[foo] by capturing x as the modifier character.
304 char Modifier = '\0';
305 if (isalpha(EscapedChar)) {
306 Modifier = EscapedChar;
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000307 EscapedChar = *CurPtr++;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000308 }
309
310 if (isdigit(EscapedChar)) {
311 // %n - Assembler operand n
Chris Lattner3f402202009-03-11 22:52:17 +0000312 unsigned N = 0;
313
314 --CurPtr;
315 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner9e09e962009-03-11 23:09:16 +0000316 N = N*10 + ((*CurPtr++)-'0');
Chris Lattner5b132432009-03-11 00:23:13 +0000317
318 unsigned NumOperands =
319 getNumOutputs() + getNumPlusOperands() + getNumInputs();
320 if (N >= NumOperands) {
321 DiagOffs = CurPtr-StrStart-1;
322 return diag::err_asm_invalid_operand_number;
323 }
324
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000325 Pieces.push_back(AsmStringPiece(N, Modifier));
326 continue;
327 }
328
329 // Handle %[foo], a symbolic operand reference.
330 if (EscapedChar == '[') {
Chris Lattnera7f22702009-03-11 00:06:36 +0000331 DiagOffs = CurPtr-StrStart-1;
332
333 // Find the ']'.
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000334 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnera7f22702009-03-11 00:06:36 +0000335 if (NameEnd == 0)
336 return diag::err_asm_unterminated_symbolic_operand_name;
337 if (NameEnd == CurPtr)
338 return diag::err_asm_empty_symbolic_operand_name;
339
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000340 std::string SymbolicName(CurPtr, NameEnd);
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000341
342 int N = getNamedOperand(SymbolicName);
Chris Lattnera7f22702009-03-11 00:06:36 +0000343 if (N == -1) {
344 // Verify that an operand with that name exists.
345 DiagOffs = CurPtr-StrStart;
346 return diag::err_asm_unknown_symbolic_operand_name;
347 }
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000348 Pieces.push_back(AsmStringPiece(N, Modifier));
Chris Lattnera7f22702009-03-11 00:06:36 +0000349
350 CurPtr = NameEnd+1;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000351 continue;
352 }
353
Chris Lattnerc0da38d2009-03-10 23:57:07 +0000354 DiagOffs = CurPtr-StrStart-1;
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000355 return diag::err_asm_invalid_escape;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000356 }
357}
358
Chris Lattner006d1542008-01-30 05:01:46 +0000359//===----------------------------------------------------------------------===//
360// Constructors
361//===----------------------------------------------------------------------===//
362
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000363AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattner006d1542008-01-30 05:01:46 +0000364 unsigned numoutputs, unsigned numinputs,
365 std::string *names, StringLiteral **constraints,
366 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
367 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlsson965d5202007-11-22 01:36:19 +0000368 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000369 , IsSimple(issimple), IsVolatile(isvolatile)
370 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson965d5202007-11-22 01:36:19 +0000371 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
372 Names.push_back(names[i]);
373 Exprs.push_back(exprs[i]);
Nico Weber7340a2f2008-08-05 23:15:29 +0000374 Constraints.push_back(constraints[i]);
Anders Carlsson965d5202007-11-22 01:36:19 +0000375 }
Nico Weber7340a2f2008-08-05 23:15:29 +0000376
Anders Carlsson965d5202007-11-22 01:36:19 +0000377 for (unsigned i = 0; i != numclobbers; i++)
378 Clobbers.push_back(clobbers[i]);
379}
380
Chris Lattner006d1542008-01-30 05:01:46 +0000381ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
382 Stmt *Body, SourceLocation FCL,
Nico Weber7340a2f2008-08-05 23:15:29 +0000383 SourceLocation RPL)
Chris Lattner006d1542008-01-30 05:01:46 +0000384: Stmt(ObjCForCollectionStmtClass) {
385 SubExprs[ELEM] = Elem;
386 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
387 SubExprs[BODY] = Body;
388 ForLoc = FCL;
389 RParenLoc = RPL;
390}
391
392
Nico Weber7340a2f2008-08-05 23:15:29 +0000393ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
394 SourceLocation rparenloc,
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000395 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattner006d1542008-01-30 05:01:46 +0000396 Stmt *atCatchList)
397: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000398 ExceptionDecl = catchVarDecl;
Chris Lattner006d1542008-01-30 05:01:46 +0000399 SubExprs[BODY] = atCatchStmt;
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000400 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbard23d9922009-03-01 04:28:32 +0000401 // FIXME: O(N^2) in number of catch blocks.
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000402 if (atCatchList) {
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000403 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
404
Nico Weber7340a2f2008-08-05 23:15:29 +0000405 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000406 AtCatchList = NextCatch;
Nico Weber7340a2f2008-08-05 23:15:29 +0000407
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000408 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner006d1542008-01-30 05:01:46 +0000409 }
410 AtCatchLoc = atCatchLoc;
411 RParenLoc = rparenloc;
412}
413
414
Ted Kremenek51be0562007-08-24 21:09:09 +0000415//===----------------------------------------------------------------------===//
416// Child Iterators for iterating over subexpressions/substatements
417//===----------------------------------------------------------------------===//
418
419// DeclStmt
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000420Stmt::child_iterator DeclStmt::child_begin() {
421 return StmtIterator(DG.begin(), DG.end());
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000422}
423
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000424Stmt::child_iterator DeclStmt::child_end() {
425 return StmtIterator(DG.end(), DG.end());
Ted Kremenek62f23bb2008-10-06 20:54:44 +0000426}
427
Ted Kremenek51be0562007-08-24 21:09:09 +0000428// NullStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000429Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
430Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000431
432// CompoundStmt
433Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek0c97e042009-02-07 01:47:29 +0000434Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000435
Ted Kremeneke07b67e2007-08-30 16:50:46 +0000436// CaseStmt
437Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
438Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
439
440// DefaultStmt
441Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
442Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000443
444// LabelStmt
445Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000446Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000447
448// IfStmt
449Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
450Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
451
452// SwitchStmt
453Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
454Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
455
456// WhileStmt
457Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
458Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
459
460// DoStmt
461Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
462Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
463
464// ForStmt
465Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
466Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
467
Ted Kremenek42730c52008-01-07 19:49:32 +0000468// ObjCForCollectionStmt
Nico Weber7340a2f2008-08-05 23:15:29 +0000469Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
470 return &SubExprs[0];
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000471}
Nico Weber7340a2f2008-08-05 23:15:29 +0000472Stmt::child_iterator ObjCForCollectionStmt::child_end() {
473 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000474}
475
Ted Kremenek51be0562007-08-24 21:09:09 +0000476// GotoStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000477Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
478Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000479
480// IndirectGotoStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000481Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
482const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000483
Ted Kremenek156714e2008-06-17 03:11:08 +0000484Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
485Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000486
487// ContinueStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000488Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
489Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000490
491// BreakStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000492Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
493Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000494
495// ReturnStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000496const Expr* ReturnStmt::getRetValue() const {
497 return cast_or_null<Expr>(RetExpr);
498}
499Expr* ReturnStmt::getRetValue() {
500 return cast_or_null<Expr>(RetExpr);
Ted Kremenek51be0562007-08-24 21:09:09 +0000501}
502
Ted Kremenek156714e2008-06-17 03:11:08 +0000503Stmt::child_iterator ReturnStmt::child_begin() {
504 return &RetExpr;
505}
506Stmt::child_iterator ReturnStmt::child_end() {
507 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000508}
Ted Kremenek51be0562007-08-24 21:09:09 +0000509
Chris Lattner8a40a832007-10-29 04:04:16 +0000510// AsmStmt
Ted Kremenekb30de272008-10-27 18:40:21 +0000511Stmt::child_iterator AsmStmt::child_begin() {
512 return Exprs.empty() ? 0 : &Exprs[0];
513}
514Stmt::child_iterator AsmStmt::child_end() {
515 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
516}
Chris Lattner8a40a832007-10-29 04:04:16 +0000517
Ted Kremenek42730c52008-01-07 19:49:32 +0000518// ObjCAtCatchStmt
519Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000520Stmt::child_iterator ObjCAtCatchStmt::child_end() {
521 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000522}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000523
Ted Kremenek42730c52008-01-07 19:49:32 +0000524// ObjCAtFinallyStmt
525Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
526Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000527
Ted Kremenek42730c52008-01-07 19:49:32 +0000528// ObjCAtTryStmt
529Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000530Stmt::child_iterator ObjCAtTryStmt::child_end() {
531 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000532}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000533
Ted Kremenek42730c52008-01-07 19:49:32 +0000534// ObjCAtThrowStmt
535Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000536 return &Throw;
537}
538
Ted Kremenek42730c52008-01-07 19:49:32 +0000539Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000540 return &Throw+1;
541}
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000542
543// ObjCAtSynchronizedStmt
544Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000545 return &SubStmts[0];
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000546}
547
548Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000549 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000550}
551
Sebastian Redl743c8162008-12-22 19:15:10 +0000552// CXXCatchStmt
553Stmt::child_iterator CXXCatchStmt::child_begin() {
554 return &HandlerBlock;
555}
556
557Stmt::child_iterator CXXCatchStmt::child_end() {
558 return &HandlerBlock + 1;
559}
560
561QualType CXXCatchStmt::getCaughtType() {
562 if (ExceptionDecl)
Douglas Gregor57420b42009-05-18 20:51:54 +0000563 return ExceptionDecl->getType();
Sebastian Redl743c8162008-12-22 19:15:10 +0000564 return QualType();
565}
566
Douglas Gregor53e8e4b2009-08-07 06:08:38 +0000567void CXXCatchStmt::DoDestroy(ASTContext& C) {
Sebastian Redl237116b2008-12-22 21:35:02 +0000568 if (ExceptionDecl)
569 ExceptionDecl->Destroy(C);
Douglas Gregor53e8e4b2009-08-07 06:08:38 +0000570 Stmt::DoDestroy(C);
Sebastian Redl743c8162008-12-22 19:15:10 +0000571}
Sebastian Redl237116b2008-12-22 21:35:02 +0000572
573// CXXTryStmt
574Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
575Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
576
577CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
578 Stmt **handlers, unsigned numHandlers)
579 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
580 Stmts.push_back(tryBlock);
581 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
582}