blob: 67910c8ab62cc7994a0fb419fd439eef3e788e9d [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
Douglas Gregor025452f2009-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}
Reid Spencer5f016e22007-07-11 17:01:13 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108const char *LabelStmt::getName() const {
109 return getID()->getName();
110}
111
Steve Naroff507f2d52007-08-31 23:49:30 +0000112// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000113SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000114 if (RetExpr)
115 return SourceRange(RetLoc, RetExpr->getLocEnd());
116 else
117 return SourceRange(RetLoc);
118}
119
Ted Kremenekd48ade62007-10-01 16:34:52 +0000120bool Stmt::hasImplicitControlFlow() const {
121 switch (sClass) {
122 default:
123 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000124
Ted Kremenekd48ade62007-10-01 16:34:52 +0000125 case CallExprClass:
126 case ConditionalOperatorClass:
127 case ChooseExprClass:
128 case StmtExprClass:
129 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000130 return true;
131
Ted Kremenekd48ade62007-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 Lattnerb3277932009-03-10 04:59:06 +0000142Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000143 return cast<Expr>(Exprs[i]);
144}
Chris Lattnerb3277932009-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 Kremenekce2fc3a2008-10-27 18:40:21 +0000152}
Chris Lattnerb3277932009-03-10 04:59:06 +0000153
Chris Lattner85759272009-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 Lattnerb3277932009-03-10 04:59:06 +0000165
166Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000167 return cast<Expr>(Exprs[i + NumOutputs]);
168}
Chris Lattnerb3277932009-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 Kremenekce2fc3a2008-10-27 18:40:21 +0000175}
176
Chris Lattner10ca96a2009-03-10 06:33:24 +0000177
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000178void AsmStmt::setOutputsAndInputs(unsigned NumOutputs,
179 unsigned NumInputs,
180 const std::string *Names,
181 StringLiteral **Constraints,
182 Stmt **Exprs) {
183 this->NumOutputs = NumOutputs;
184 this->NumInputs = NumInputs;
185 this->Names.clear();
186 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
187 this->Constraints.clear();
188 this->Constraints.insert(this->Constraints.end(),
189 Constraints, Constraints + NumOutputs + NumInputs);
190 this->Exprs.clear();
191 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
192}
193
Chris Lattner10ca96a2009-03-10 06:33:24 +0000194/// getNamedOperand - Given a symbolic operand reference like %[foo],
195/// translate this into a numeric value needed to reference the same operand.
196/// This returns -1 if the operand name is invalid.
197int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
198 unsigned NumPlusOperands = 0;
199
200 // Check if this is an output operand.
201 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
202 if (getOutputName(i) == SymbolicName)
203 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000204 }
205
206 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
207 if (getInputName(i) == SymbolicName)
208 return getNumOutputs() + NumPlusOperands + i;
209
210 // Not found.
211 return -1;
212}
213
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000214void AsmStmt::setClobbers(StringLiteral **Clobbers, unsigned NumClobbers) {
215 this->Clobbers.clear();
216 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
217}
Chris Lattner10ca96a2009-03-10 06:33:24 +0000218
Chris Lattner458cd9c2009-03-10 23:21:44 +0000219/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
220/// it into pieces. If the asm string is erroneous, emit errors and return
221/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000222unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
223 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000224 const char *StrStart = getAsmString()->getStrData();
225 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000226 const char *CurPtr = StrStart;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000227
228 // "Simple" inline asms have no constraints or operands, just convert the asm
229 // string to escape $'s.
230 if (isSimple()) {
231 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000232 for (; CurPtr != StrEnd; ++CurPtr) {
233 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000234 case '$':
235 Result += "$$";
236 break;
237 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000238 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000239 break;
240 }
241 }
242 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000243 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000244 }
245
246 // CurStringPiece - The current string that we are building up as we scan the
247 // asm string.
248 std::string CurStringPiece;
249
250 while (1) {
251 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000252 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000253 if (!CurStringPiece.empty())
254 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000255 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000256 }
257
Chris Lattner3182db12009-03-10 23:51:40 +0000258 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000259 if (CurChar == '$') {
260 CurStringPiece += "$$";
261 continue;
262 } else if (CurChar != '%') {
263 CurStringPiece += CurChar;
264 continue;
265 }
266
267 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000268 if (CurPtr == StrEnd) {
269 // % at end of string is invalid (no escape).
270 DiagOffs = CurPtr-StrStart-1;
271 return diag::err_asm_invalid_escape;
272 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000273
Chris Lattner3182db12009-03-10 23:51:40 +0000274 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000275 if (EscapedChar == '%') { // %% -> %
276 // Escaped percentage sign.
277 CurStringPiece += '%';
278 continue;
279 }
280
281 if (EscapedChar == '=') { // %= -> Generate an unique ID.
282 CurStringPiece += "${:uid}";
283 continue;
284 }
285
286 // Otherwise, we have an operand. If we have accumulated a string so far,
287 // add it to the Pieces list.
288 if (!CurStringPiece.empty()) {
289 Pieces.push_back(AsmStringPiece(CurStringPiece));
290 CurStringPiece.clear();
291 }
292
293 // Handle %x4 and %x[foo] by capturing x as the modifier character.
294 char Modifier = '\0';
295 if (isalpha(EscapedChar)) {
296 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000297 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000298 }
299
300 if (isdigit(EscapedChar)) {
301 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000302 unsigned N = 0;
303
304 --CurPtr;
305 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000306 N = N*10 + ((*CurPtr++)-'0');
Chris Lattner85759272009-03-11 00:23:13 +0000307
308 unsigned NumOperands =
309 getNumOutputs() + getNumPlusOperands() + getNumInputs();
310 if (N >= NumOperands) {
311 DiagOffs = CurPtr-StrStart-1;
312 return diag::err_asm_invalid_operand_number;
313 }
314
Chris Lattner458cd9c2009-03-10 23:21:44 +0000315 Pieces.push_back(AsmStringPiece(N, Modifier));
316 continue;
317 }
318
319 // Handle %[foo], a symbolic operand reference.
320 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000321 DiagOffs = CurPtr-StrStart-1;
322
323 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000324 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000325 if (NameEnd == 0)
326 return diag::err_asm_unterminated_symbolic_operand_name;
327 if (NameEnd == CurPtr)
328 return diag::err_asm_empty_symbolic_operand_name;
329
Chris Lattner3182db12009-03-10 23:51:40 +0000330 std::string SymbolicName(CurPtr, NameEnd);
Chris Lattner458cd9c2009-03-10 23:21:44 +0000331
332 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000333 if (N == -1) {
334 // Verify that an operand with that name exists.
335 DiagOffs = CurPtr-StrStart;
336 return diag::err_asm_unknown_symbolic_operand_name;
337 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000338 Pieces.push_back(AsmStringPiece(N, Modifier));
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000339
340 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000341 continue;
342 }
343
Chris Lattner2ff0f422009-03-10 23:57:07 +0000344 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000345 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000346 }
347}
348
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000349//===----------------------------------------------------------------------===//
350// Constructors
351//===----------------------------------------------------------------------===//
352
Anders Carlssondfab34a2008-02-05 23:03:50 +0000353AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000354 unsigned numoutputs, unsigned numinputs,
355 std::string *names, StringLiteral **constraints,
356 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
357 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000358 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000359 , IsSimple(issimple), IsVolatile(isvolatile)
360 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000361 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
362 Names.push_back(names[i]);
363 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000364 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000365 }
Nico Weber608b17f2008-08-05 23:15:29 +0000366
Anders Carlssonb235fc22007-11-22 01:36:19 +0000367 for (unsigned i = 0; i != numclobbers; i++)
368 Clobbers.push_back(clobbers[i]);
369}
370
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000371ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
372 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000373 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000374: Stmt(ObjCForCollectionStmtClass) {
375 SubExprs[ELEM] = Elem;
376 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
377 SubExprs[BODY] = Body;
378 ForLoc = FCL;
379 RParenLoc = RPL;
380}
381
382
Nico Weber608b17f2008-08-05 23:15:29 +0000383ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
384 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000385 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000386 Stmt *atCatchList)
387: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000388 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000389 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000390 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000391 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000392 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000393 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
394
Nico Weber608b17f2008-08-05 23:15:29 +0000395 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000396 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000397
Ted Kremenekff981022008-02-01 21:28:59 +0000398 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000399 }
400 AtCatchLoc = atCatchLoc;
401 RParenLoc = rparenloc;
402}
403
404
Ted Kremenek82977772007-08-24 21:09:09 +0000405//===----------------------------------------------------------------------===//
406// Child Iterators for iterating over subexpressions/substatements
407//===----------------------------------------------------------------------===//
408
409// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000410Stmt::child_iterator DeclStmt::child_begin() {
411 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000412}
413
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000414Stmt::child_iterator DeclStmt::child_end() {
415 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000416}
417
Ted Kremenek82977772007-08-24 21:09:09 +0000418// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000419Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
420Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000421
422// CompoundStmt
423Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000424Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000425
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000426// CaseStmt
427Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
428Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
429
430// DefaultStmt
431Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
432Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000433
434// LabelStmt
435Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000436Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000437
438// IfStmt
439Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
440Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
441
442// SwitchStmt
443Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
444Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
445
446// WhileStmt
447Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
448Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
449
450// DoStmt
451Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
452Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
453
454// ForStmt
455Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
456Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
457
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000458// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000459Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
460 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000461}
Nico Weber608b17f2008-08-05 23:15:29 +0000462Stmt::child_iterator ObjCForCollectionStmt::child_end() {
463 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000464}
465
Ted Kremenek82977772007-08-24 21:09:09 +0000466// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000467Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
468Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000469
470// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000471Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
472const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000473
Ted Kremenek1060aff2008-06-17 03:11:08 +0000474Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
475Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000476
477// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000478Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
479Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000480
481// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000482Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
483Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000484
485// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000486const Expr* ReturnStmt::getRetValue() const {
487 return cast_or_null<Expr>(RetExpr);
488}
489Expr* ReturnStmt::getRetValue() {
490 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000491}
492
Ted Kremenek1060aff2008-06-17 03:11:08 +0000493Stmt::child_iterator ReturnStmt::child_begin() {
494 return &RetExpr;
495}
496Stmt::child_iterator ReturnStmt::child_end() {
497 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000498}
Ted Kremenek82977772007-08-24 21:09:09 +0000499
Chris Lattnerfe795952007-10-29 04:04:16 +0000500// AsmStmt
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000501Stmt::child_iterator AsmStmt::child_begin() {
502 return Exprs.empty() ? 0 : &Exprs[0];
503}
504Stmt::child_iterator AsmStmt::child_end() {
505 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
506}
Chris Lattnerfe795952007-10-29 04:04:16 +0000507
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000508// ObjCAtCatchStmt
509Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000510Stmt::child_iterator ObjCAtCatchStmt::child_end() {
511 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000512}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000513
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000514// ObjCAtFinallyStmt
515Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
516Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000517
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000518// ObjCAtTryStmt
519Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000520Stmt::child_iterator ObjCAtTryStmt::child_end() {
521 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000522}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000523
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000524// ObjCAtThrowStmt
525Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000526 return &Throw;
527}
528
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000529Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000530 return &Throw+1;
531}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000532
533// ObjCAtSynchronizedStmt
534Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000535 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000536}
537
538Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000539 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000540}
541
Sebastian Redl4b07b292008-12-22 19:15:10 +0000542// CXXCatchStmt
543Stmt::child_iterator CXXCatchStmt::child_begin() {
544 return &HandlerBlock;
545}
546
547Stmt::child_iterator CXXCatchStmt::child_end() {
548 return &HandlerBlock + 1;
549}
550
551QualType CXXCatchStmt::getCaughtType() {
552 if (ExceptionDecl)
553 return llvm::cast<VarDecl>(ExceptionDecl)->getType();
554 return QualType();
555}
556
557void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000558 if (ExceptionDecl)
559 ExceptionDecl->Destroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000560 Stmt::Destroy(C);
561}
Sebastian Redl8351da02008-12-22 21:35:02 +0000562
563// CXXTryStmt
564Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
565Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
566
567CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
568 Stmt **handlers, unsigned numHandlers)
569 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
570 Stmts.push_back(tryBlock);
571 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
572}