blob: 8e2cf861f83775ee09e1c500abf9f901124a88a6 [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
Douglas Gregor9c4782a2009-04-17 00:04:06 +000099void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
100 if (this->Body)
101 C.Deallocate(Body);
102 this->NumStmts = NumStmts;
103
104 Body = new (C) Stmt*[NumStmts];
105 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
106}
Chris Lattner4b009652007-07-25 00:24:17 +0000107
Chris Lattner4b009652007-07-25 00:24:17 +0000108const char *LabelStmt::getName() const {
109 return getID()->getName();
110}
111
Steve Naroffc32a20d2007-08-31 23:49:30 +0000112// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber7340a2f2008-08-05 23:15:29 +0000113SourceRange ReturnStmt::getSourceRange() const {
Steve Naroffc32a20d2007-08-31 23:49:30 +0000114 if (RetExpr)
115 return SourceRange(RetLoc, RetExpr->getLocEnd());
116 else
117 return SourceRange(RetLoc);
118}
119
Ted Kremenekad5682f2007-10-01 16:34:52 +0000120bool Stmt::hasImplicitControlFlow() const {
121 switch (sClass) {
122 default:
123 return false;
Nico Weber7340a2f2008-08-05 23:15:29 +0000124
Ted Kremenekad5682f2007-10-01 16:34:52 +0000125 case CallExprClass:
126 case ConditionalOperatorClass:
127 case ChooseExprClass:
128 case StmtExprClass:
129 case DeclStmtClass:
Nico Weber7340a2f2008-08-05 23:15:29 +0000130 return true;
131
Ted Kremenekad5682f2007-10-01 16:34:52 +0000132 case Stmt::BinaryOperatorClass: {
133 const BinaryOperator* B = cast<BinaryOperator>(this);
134 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
135 return true;
136 else
137 return false;
138 }
139 }
140}
141
Chris Lattnere8625112009-03-10 04:59:06 +0000142Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekb30de272008-10-27 18:40:21 +0000143 return cast<Expr>(Exprs[i]);
144}
Chris Lattnere8625112009-03-10 04:59:06 +0000145
146/// getOutputConstraint - Return the constraint string for the specified
147/// output operand. All output constraints are known to be non-empty (either
148/// '=' or '+').
149std::string AsmStmt::getOutputConstraint(unsigned i) const {
150 return std::string(Constraints[i]->getStrData(),
151 Constraints[i]->getByteLength());
Ted Kremenekb30de272008-10-27 18:40:21 +0000152}
Chris Lattnere8625112009-03-10 04:59:06 +0000153
Chris Lattner5b132432009-03-11 00:23:13 +0000154/// getNumPlusOperands - Return the number of output operands that have a "+"
155/// constraint.
156unsigned AsmStmt::getNumPlusOperands() const {
157 unsigned Res = 0;
158 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
159 if (isOutputPlusConstraint(i))
160 ++Res;
161 return Res;
162}
163
164
Chris Lattnere8625112009-03-10 04:59:06 +0000165
166Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekb30de272008-10-27 18:40:21 +0000167 return cast<Expr>(Exprs[i + NumOutputs]);
168}
Chris Lattnere8625112009-03-10 04:59:06 +0000169
170/// getInputConstraint - Return the specified input constraint. Unlike output
171/// constraints, these can be empty.
172std::string AsmStmt::getInputConstraint(unsigned i) const {
173 return std::string(Constraints[i + NumOutputs]->getStrData(),
174 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekb30de272008-10-27 18:40:21 +0000175}
176
Chris Lattner20ac04c2009-03-10 06:33:24 +0000177
178/// getNamedOperand - Given a symbolic operand reference like %[foo],
179/// translate this into a numeric value needed to reference the same operand.
180/// This returns -1 if the operand name is invalid.
181int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
182 unsigned NumPlusOperands = 0;
183
184 // Check if this is an output operand.
185 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
186 if (getOutputName(i) == SymbolicName)
187 return i;
Chris Lattner20ac04c2009-03-10 06:33:24 +0000188 }
189
190 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
191 if (getInputName(i) == SymbolicName)
192 return getNumOutputs() + NumPlusOperands + i;
193
194 // Not found.
195 return -1;
196}
197
198
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000199/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
200/// it into pieces. If the asm string is erroneous, emit errors and return
201/// true, otherwise return false.
Chris Lattnerc5164732009-03-10 23:41:04 +0000202unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
203 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000204 const char *StrStart = getAsmString()->getStrData();
205 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000206 const char *CurPtr = StrStart;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000207
208 // "Simple" inline asms have no constraints or operands, just convert the asm
209 // string to escape $'s.
210 if (isSimple()) {
211 std::string Result;
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000212 for (; CurPtr != StrEnd; ++CurPtr) {
213 switch (*CurPtr) {
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000214 case '$':
215 Result += "$$";
216 break;
217 default:
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000218 Result += *CurPtr;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000219 break;
220 }
221 }
222 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000223 return 0;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000224 }
225
226 // CurStringPiece - The current string that we are building up as we scan the
227 // asm string.
228 std::string CurStringPiece;
229
230 while (1) {
231 // Done with the string?
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000232 if (CurPtr == StrEnd) {
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000233 if (!CurStringPiece.empty())
234 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000235 return 0;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000236 }
237
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000238 char CurChar = *CurPtr++;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000239 if (CurChar == '$') {
240 CurStringPiece += "$$";
241 continue;
242 } else if (CurChar != '%') {
243 CurStringPiece += CurChar;
244 continue;
245 }
246
247 // Escaped "%" character in asm string.
Chris Lattnera7f22702009-03-11 00:06:36 +0000248 if (CurPtr == StrEnd) {
249 // % at end of string is invalid (no escape).
250 DiagOffs = CurPtr-StrStart-1;
251 return diag::err_asm_invalid_escape;
252 }
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000253
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000254 char EscapedChar = *CurPtr++;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000255 if (EscapedChar == '%') { // %% -> %
256 // Escaped percentage sign.
257 CurStringPiece += '%';
258 continue;
259 }
260
261 if (EscapedChar == '=') { // %= -> Generate an unique ID.
262 CurStringPiece += "${:uid}";
263 continue;
264 }
265
266 // Otherwise, we have an operand. If we have accumulated a string so far,
267 // add it to the Pieces list.
268 if (!CurStringPiece.empty()) {
269 Pieces.push_back(AsmStringPiece(CurStringPiece));
270 CurStringPiece.clear();
271 }
272
273 // Handle %x4 and %x[foo] by capturing x as the modifier character.
274 char Modifier = '\0';
275 if (isalpha(EscapedChar)) {
276 Modifier = EscapedChar;
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000277 EscapedChar = *CurPtr++;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000278 }
279
280 if (isdigit(EscapedChar)) {
281 // %n - Assembler operand n
Chris Lattner3f402202009-03-11 22:52:17 +0000282 unsigned N = 0;
283
284 --CurPtr;
285 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner9e09e962009-03-11 23:09:16 +0000286 N = N*10 + ((*CurPtr++)-'0');
Chris Lattner5b132432009-03-11 00:23:13 +0000287
288 unsigned NumOperands =
289 getNumOutputs() + getNumPlusOperands() + getNumInputs();
290 if (N >= NumOperands) {
291 DiagOffs = CurPtr-StrStart-1;
292 return diag::err_asm_invalid_operand_number;
293 }
294
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000295 Pieces.push_back(AsmStringPiece(N, Modifier));
296 continue;
297 }
298
299 // Handle %[foo], a symbolic operand reference.
300 if (EscapedChar == '[') {
Chris Lattnera7f22702009-03-11 00:06:36 +0000301 DiagOffs = CurPtr-StrStart-1;
302
303 // Find the ']'.
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000304 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnera7f22702009-03-11 00:06:36 +0000305 if (NameEnd == 0)
306 return diag::err_asm_unterminated_symbolic_operand_name;
307 if (NameEnd == CurPtr)
308 return diag::err_asm_empty_symbolic_operand_name;
309
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000310 std::string SymbolicName(CurPtr, NameEnd);
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000311
312 int N = getNamedOperand(SymbolicName);
Chris Lattnera7f22702009-03-11 00:06:36 +0000313 if (N == -1) {
314 // Verify that an operand with that name exists.
315 DiagOffs = CurPtr-StrStart;
316 return diag::err_asm_unknown_symbolic_operand_name;
317 }
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000318 Pieces.push_back(AsmStringPiece(N, Modifier));
Chris Lattnera7f22702009-03-11 00:06:36 +0000319
320 CurPtr = NameEnd+1;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000321 continue;
322 }
323
Chris Lattnerc0da38d2009-03-10 23:57:07 +0000324 DiagOffs = CurPtr-StrStart-1;
Chris Lattnerd4dd42c2009-03-10 23:51:40 +0000325 return diag::err_asm_invalid_escape;
Chris Lattner5c6a6c52009-03-10 23:21:44 +0000326 }
327}
328
Chris Lattner006d1542008-01-30 05:01:46 +0000329//===----------------------------------------------------------------------===//
330// Constructors
331//===----------------------------------------------------------------------===//
332
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000333AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattner006d1542008-01-30 05:01:46 +0000334 unsigned numoutputs, unsigned numinputs,
335 std::string *names, StringLiteral **constraints,
336 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
337 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlsson965d5202007-11-22 01:36:19 +0000338 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000339 , IsSimple(issimple), IsVolatile(isvolatile)
340 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson965d5202007-11-22 01:36:19 +0000341 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
342 Names.push_back(names[i]);
343 Exprs.push_back(exprs[i]);
Nico Weber7340a2f2008-08-05 23:15:29 +0000344 Constraints.push_back(constraints[i]);
Anders Carlsson965d5202007-11-22 01:36:19 +0000345 }
Nico Weber7340a2f2008-08-05 23:15:29 +0000346
Anders Carlsson965d5202007-11-22 01:36:19 +0000347 for (unsigned i = 0; i != numclobbers; i++)
348 Clobbers.push_back(clobbers[i]);
349}
350
Chris Lattner006d1542008-01-30 05:01:46 +0000351ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
352 Stmt *Body, SourceLocation FCL,
Nico Weber7340a2f2008-08-05 23:15:29 +0000353 SourceLocation RPL)
Chris Lattner006d1542008-01-30 05:01:46 +0000354: Stmt(ObjCForCollectionStmtClass) {
355 SubExprs[ELEM] = Elem;
356 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
357 SubExprs[BODY] = Body;
358 ForLoc = FCL;
359 RParenLoc = RPL;
360}
361
362
Nico Weber7340a2f2008-08-05 23:15:29 +0000363ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
364 SourceLocation rparenloc,
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000365 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattner006d1542008-01-30 05:01:46 +0000366 Stmt *atCatchList)
367: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000368 ExceptionDecl = catchVarDecl;
Chris Lattner006d1542008-01-30 05:01:46 +0000369 SubExprs[BODY] = atCatchStmt;
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000370 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbard23d9922009-03-01 04:28:32 +0000371 // FIXME: O(N^2) in number of catch blocks.
Eli Friedmanf3e02ed2008-05-25 04:34:57 +0000372 if (atCatchList) {
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000373 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
374
Nico Weber7340a2f2008-08-05 23:15:29 +0000375 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000376 AtCatchList = NextCatch;
Nico Weber7340a2f2008-08-05 23:15:29 +0000377
Ted Kremenek61aa7f92008-02-01 21:28:59 +0000378 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner006d1542008-01-30 05:01:46 +0000379 }
380 AtCatchLoc = atCatchLoc;
381 RParenLoc = rparenloc;
382}
383
384
Ted Kremenek51be0562007-08-24 21:09:09 +0000385//===----------------------------------------------------------------------===//
386// Child Iterators for iterating over subexpressions/substatements
387//===----------------------------------------------------------------------===//
388
389// DeclStmt
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000390Stmt::child_iterator DeclStmt::child_begin() {
391 return StmtIterator(DG.begin(), DG.end());
Ted Kremenekb59f9cf2008-08-05 20:46:55 +0000392}
393
Ted Kremenek1bc18e62008-10-07 23:09:49 +0000394Stmt::child_iterator DeclStmt::child_end() {
395 return StmtIterator(DG.end(), DG.end());
Ted Kremenek62f23bb2008-10-06 20:54:44 +0000396}
397
Ted Kremenek51be0562007-08-24 21:09:09 +0000398// NullStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000399Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
400Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000401
402// CompoundStmt
403Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek0c97e042009-02-07 01:47:29 +0000404Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000405
Ted Kremeneke07b67e2007-08-30 16:50:46 +0000406// CaseStmt
407Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
408Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
409
410// DefaultStmt
411Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
412Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000413
414// LabelStmt
415Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000416Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000417
418// IfStmt
419Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
420Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
421
422// SwitchStmt
423Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
424Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
425
426// WhileStmt
427Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
428Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
429
430// DoStmt
431Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
432Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
433
434// ForStmt
435Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
436Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
437
Ted Kremenek42730c52008-01-07 19:49:32 +0000438// ObjCForCollectionStmt
Nico Weber7340a2f2008-08-05 23:15:29 +0000439Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
440 return &SubExprs[0];
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000441}
Nico Weber7340a2f2008-08-05 23:15:29 +0000442Stmt::child_iterator ObjCForCollectionStmt::child_end() {
443 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000444}
445
Ted Kremenek51be0562007-08-24 21:09:09 +0000446// GotoStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000447Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
448Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000449
450// IndirectGotoStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000451Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
452const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000453
Ted Kremenek156714e2008-06-17 03:11:08 +0000454Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
455Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek51be0562007-08-24 21:09:09 +0000456
457// ContinueStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000458Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
459Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000460
461// BreakStmt
Ted Kremeneka6478552007-10-18 23:28:49 +0000462Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
463Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek51be0562007-08-24 21:09:09 +0000464
465// ReturnStmt
Ted Kremenek156714e2008-06-17 03:11:08 +0000466const Expr* ReturnStmt::getRetValue() const {
467 return cast_or_null<Expr>(RetExpr);
468}
469Expr* ReturnStmt::getRetValue() {
470 return cast_or_null<Expr>(RetExpr);
Ted Kremenek51be0562007-08-24 21:09:09 +0000471}
472
Ted Kremenek156714e2008-06-17 03:11:08 +0000473Stmt::child_iterator ReturnStmt::child_begin() {
474 return &RetExpr;
475}
476Stmt::child_iterator ReturnStmt::child_end() {
477 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek8a66ed42007-08-27 20:58:16 +0000478}
Ted Kremenek51be0562007-08-24 21:09:09 +0000479
Chris Lattner8a40a832007-10-29 04:04:16 +0000480// AsmStmt
Ted Kremenekb30de272008-10-27 18:40:21 +0000481Stmt::child_iterator AsmStmt::child_begin() {
482 return Exprs.empty() ? 0 : &Exprs[0];
483}
484Stmt::child_iterator AsmStmt::child_end() {
485 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
486}
Chris Lattner8a40a832007-10-29 04:04:16 +0000487
Ted Kremenek42730c52008-01-07 19:49:32 +0000488// ObjCAtCatchStmt
489Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000490Stmt::child_iterator ObjCAtCatchStmt::child_end() {
491 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000492}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000493
Ted Kremenek42730c52008-01-07 19:49:32 +0000494// ObjCAtFinallyStmt
495Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
496Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000497
Ted Kremenek42730c52008-01-07 19:49:32 +0000498// ObjCAtTryStmt
499Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber7340a2f2008-08-05 23:15:29 +0000500Stmt::child_iterator ObjCAtTryStmt::child_end() {
501 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian06798362007-11-01 23:59:59 +0000502}
Fariborz Jahanian70952482007-11-01 21:12:44 +0000503
Ted Kremenek42730c52008-01-07 19:49:32 +0000504// ObjCAtThrowStmt
505Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000506 return &Throw;
507}
508
Ted Kremenek42730c52008-01-07 19:49:32 +0000509Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000510 return &Throw+1;
511}
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000512
513// ObjCAtSynchronizedStmt
514Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000515 return &SubStmts[0];
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000516}
517
518Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian499bf412008-01-29 22:59:37 +0000519 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +0000520}
521
Sebastian Redl743c8162008-12-22 19:15:10 +0000522// CXXCatchStmt
523Stmt::child_iterator CXXCatchStmt::child_begin() {
524 return &HandlerBlock;
525}
526
527Stmt::child_iterator CXXCatchStmt::child_end() {
528 return &HandlerBlock + 1;
529}
530
531QualType CXXCatchStmt::getCaughtType() {
532 if (ExceptionDecl)
533 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
534 return QualType();
535}
536
537void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl237116b2008-12-22 21:35:02 +0000538 if (ExceptionDecl)
539 ExceptionDecl->Destroy(C);
Sebastian Redl743c8162008-12-22 19:15:10 +0000540 Stmt::Destroy(C);
541}
Sebastian Redl237116b2008-12-22 21:35:02 +0000542
543// CXXTryStmt
544Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
545Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
546
547CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
548 Stmt **handlers, unsigned numHandlers)
549 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
550 Stmts.push_back(tryBlock);
551 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
552}