blob: b2fadb1834f20f24b1a64adf966a9b6aa1c79224 [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"
Chris Lattner16f00492009-04-26 01:32:48 +000017#include "clang/AST/StmtCXX.h"
18#include "clang/AST/StmtObjC.h"
Sebastian Redl4b07b292008-12-22 19:15:10 +000019#include "clang/AST/Type.h"
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000020#include "clang/AST/ASTContext.h"
Chris Lattner3182db12009-03-10 23:51:40 +000021#include "clang/AST/ASTDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000025 const char *Name;
26 unsigned Counter;
27 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000028} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-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 Gregorf2cad862008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +000040#include "clang/AST/StmtNodes.def"
Nico Weber608b17f2008-08-05 23:15:29 +000041
Chris Lattner63381352007-08-25 01:42:24 +000042 return StmtClassInfo[E];
43}
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045const char *Stmt::getStmtClassName() const {
Chris Lattner63381352007-08-25 01:42:24 +000046 return getStmtInfoTableEntry(sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000047}
48
Chris Lattner24e1e702009-03-04 04:23:07 +000049void Stmt::DestroyChildren(ASTContext &C) {
50 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
Douglas Gregor860f6d42009-01-16 06:50:08 +000051 if (Stmt* Child = *I++) Child->Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +000052}
53
Chris Lattner24e1e702009-03-04 04:23:07 +000054void Stmt::Destroy(ASTContext &C) {
Ted Kremenek27f8a282008-05-20 00:43:19 +000055 DestroyChildren(C);
Ted Kremenekf809e3b2008-05-20 04:10:52 +000056 // FIXME: Eventually all Stmts should be allocated with the allocator
57 // in ASTContext, just like with Decls.
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000058 this->~Stmt();
59 C.Deallocate((void *)this);
Ted Kremenek9c1863e2008-05-19 22:02:12 +000060}
61
Chris Lattner24e1e702009-03-04 04:23:07 +000062void DeclStmt::Destroy(ASTContext &C) {
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000063 this->~DeclStmt();
64 C.Deallocate((void *)this);
Ted Kremenek8e355f22008-05-21 15:53:55 +000065}
66
Reid Spencer5f016e22007-07-11 17:01:13 +000067void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000068 // Ensure the table is primed.
69 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000070
Reid Spencer5f016e22007-07-11 17:01:13 +000071 unsigned sum = 0;
72 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000073 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000074 if (StmtClassInfo[i].Name == 0) continue;
75 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000076 }
77 fprintf(stderr, " %d stmts/exprs total.\n", sum);
78 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000079 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000080 if (StmtClassInfo[i].Name == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000081 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000082 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
83 StmtClassInfo[i].Size,
84 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
85 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 }
87 fprintf(stderr, "Total bytes = %d\n", sum);
88}
89
90void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000091 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000092}
93
94static bool StatSwitch = false;
95
96bool Stmt::CollectingStats(bool enable) {
97 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000098 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000099}
100
Anders Carlssond19cd902009-05-15 00:21:21 +0000101NullStmt* NullStmt::Clone(ASTContext &C) const {
102 return new (C) NullStmt(SemiLoc);
103}
104
Douglas Gregor861ce312009-05-15 22:32:39 +0000105ContinueStmt* ContinueStmt::Clone(ASTContext &C) const {
106 return new (C) ContinueStmt(ContinueLoc);
107}
108
109BreakStmt* BreakStmt::Clone(ASTContext &C) const {
110 return new (C) BreakStmt(BreakLoc);
111}
112
Douglas Gregor025452f2009-04-17 00:04:06 +0000113void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
114 if (this->Body)
115 C.Deallocate(Body);
116 this->NumStmts = NumStmts;
117
118 Body = new (C) Stmt*[NumStmts];
119 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
120}
Reid Spencer5f016e22007-07-11 17:01:13 +0000121
Reid Spencer5f016e22007-07-11 17:01:13 +0000122const char *LabelStmt::getName() const {
123 return getID()->getName();
124}
125
Steve Naroff507f2d52007-08-31 23:49:30 +0000126// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000127SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000128 if (RetExpr)
129 return SourceRange(RetLoc, RetExpr->getLocEnd());
130 else
131 return SourceRange(RetLoc);
132}
133
Ted Kremenekd48ade62007-10-01 16:34:52 +0000134bool Stmt::hasImplicitControlFlow() const {
135 switch (sClass) {
136 default:
137 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000138
Ted Kremenekd48ade62007-10-01 16:34:52 +0000139 case CallExprClass:
140 case ConditionalOperatorClass:
141 case ChooseExprClass:
142 case StmtExprClass:
143 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000144 return true;
145
Ted Kremenekd48ade62007-10-01 16:34:52 +0000146 case Stmt::BinaryOperatorClass: {
147 const BinaryOperator* B = cast<BinaryOperator>(this);
148 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
149 return true;
150 else
151 return false;
152 }
153 }
154}
155
Chris Lattnerb3277932009-03-10 04:59:06 +0000156Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000157 return cast<Expr>(Exprs[i]);
158}
Chris Lattnerb3277932009-03-10 04:59:06 +0000159
160/// getOutputConstraint - Return the constraint string for the specified
161/// output operand. All output constraints are known to be non-empty (either
162/// '=' or '+').
163std::string AsmStmt::getOutputConstraint(unsigned i) const {
164 return std::string(Constraints[i]->getStrData(),
165 Constraints[i]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000166}
Chris Lattnerb3277932009-03-10 04:59:06 +0000167
Chris Lattner85759272009-03-11 00:23:13 +0000168/// getNumPlusOperands - Return the number of output operands that have a "+"
169/// constraint.
170unsigned AsmStmt::getNumPlusOperands() const {
171 unsigned Res = 0;
172 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
173 if (isOutputPlusConstraint(i))
174 ++Res;
175 return Res;
176}
177
178
Chris Lattnerb3277932009-03-10 04:59:06 +0000179
180Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000181 return cast<Expr>(Exprs[i + NumOutputs]);
182}
Chris Lattnerb3277932009-03-10 04:59:06 +0000183
184/// getInputConstraint - Return the specified input constraint. Unlike output
185/// constraints, these can be empty.
186std::string AsmStmt::getInputConstraint(unsigned i) const {
187 return std::string(Constraints[i + NumOutputs]->getStrData(),
188 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000189}
190
Chris Lattner10ca96a2009-03-10 06:33:24 +0000191
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000192void AsmStmt::setOutputsAndInputs(unsigned NumOutputs,
193 unsigned NumInputs,
194 const std::string *Names,
195 StringLiteral **Constraints,
196 Stmt **Exprs) {
197 this->NumOutputs = NumOutputs;
198 this->NumInputs = NumInputs;
199 this->Names.clear();
200 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
201 this->Constraints.clear();
202 this->Constraints.insert(this->Constraints.end(),
203 Constraints, Constraints + NumOutputs + NumInputs);
204 this->Exprs.clear();
205 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
206}
207
Chris Lattner10ca96a2009-03-10 06:33:24 +0000208/// getNamedOperand - Given a symbolic operand reference like %[foo],
209/// translate this into a numeric value needed to reference the same operand.
210/// This returns -1 if the operand name is invalid.
211int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
212 unsigned NumPlusOperands = 0;
213
214 // Check if this is an output operand.
215 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
216 if (getOutputName(i) == SymbolicName)
217 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000218 }
219
220 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
221 if (getInputName(i) == SymbolicName)
222 return getNumOutputs() + NumPlusOperands + i;
223
224 // Not found.
225 return -1;
226}
227
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000228void AsmStmt::setClobbers(StringLiteral **Clobbers, unsigned NumClobbers) {
229 this->Clobbers.clear();
230 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
231}
Chris Lattner10ca96a2009-03-10 06:33:24 +0000232
Chris Lattner458cd9c2009-03-10 23:21:44 +0000233/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
234/// it into pieces. If the asm string is erroneous, emit errors and return
235/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000236unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
237 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000238 const char *StrStart = getAsmString()->getStrData();
239 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000240 const char *CurPtr = StrStart;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000241
242 // "Simple" inline asms have no constraints or operands, just convert the asm
243 // string to escape $'s.
244 if (isSimple()) {
245 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000246 for (; CurPtr != StrEnd; ++CurPtr) {
247 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000248 case '$':
249 Result += "$$";
250 break;
251 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000252 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000253 break;
254 }
255 }
256 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000257 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000258 }
259
260 // CurStringPiece - The current string that we are building up as we scan the
261 // asm string.
262 std::string CurStringPiece;
263
264 while (1) {
265 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000266 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000267 if (!CurStringPiece.empty())
268 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000269 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000270 }
271
Chris Lattner3182db12009-03-10 23:51:40 +0000272 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000273 if (CurChar == '$') {
274 CurStringPiece += "$$";
275 continue;
276 } else if (CurChar != '%') {
277 CurStringPiece += CurChar;
278 continue;
279 }
280
281 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000282 if (CurPtr == StrEnd) {
283 // % at end of string is invalid (no escape).
284 DiagOffs = CurPtr-StrStart-1;
285 return diag::err_asm_invalid_escape;
286 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000287
Chris Lattner3182db12009-03-10 23:51:40 +0000288 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000289 if (EscapedChar == '%') { // %% -> %
290 // Escaped percentage sign.
291 CurStringPiece += '%';
292 continue;
293 }
294
295 if (EscapedChar == '=') { // %= -> Generate an unique ID.
296 CurStringPiece += "${:uid}";
297 continue;
298 }
299
300 // Otherwise, we have an operand. If we have accumulated a string so far,
301 // add it to the Pieces list.
302 if (!CurStringPiece.empty()) {
303 Pieces.push_back(AsmStringPiece(CurStringPiece));
304 CurStringPiece.clear();
305 }
306
307 // Handle %x4 and %x[foo] by capturing x as the modifier character.
308 char Modifier = '\0';
309 if (isalpha(EscapedChar)) {
310 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000311 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000312 }
313
314 if (isdigit(EscapedChar)) {
315 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000316 unsigned N = 0;
317
318 --CurPtr;
319 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000320 N = N*10 + ((*CurPtr++)-'0');
Chris Lattner85759272009-03-11 00:23:13 +0000321
322 unsigned NumOperands =
323 getNumOutputs() + getNumPlusOperands() + getNumInputs();
324 if (N >= NumOperands) {
325 DiagOffs = CurPtr-StrStart-1;
326 return diag::err_asm_invalid_operand_number;
327 }
328
Chris Lattner458cd9c2009-03-10 23:21:44 +0000329 Pieces.push_back(AsmStringPiece(N, Modifier));
330 continue;
331 }
332
333 // Handle %[foo], a symbolic operand reference.
334 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000335 DiagOffs = CurPtr-StrStart-1;
336
337 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000338 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000339 if (NameEnd == 0)
340 return diag::err_asm_unterminated_symbolic_operand_name;
341 if (NameEnd == CurPtr)
342 return diag::err_asm_empty_symbolic_operand_name;
343
Chris Lattner3182db12009-03-10 23:51:40 +0000344 std::string SymbolicName(CurPtr, NameEnd);
Chris Lattner458cd9c2009-03-10 23:21:44 +0000345
346 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000347 if (N == -1) {
348 // Verify that an operand with that name exists.
349 DiagOffs = CurPtr-StrStart;
350 return diag::err_asm_unknown_symbolic_operand_name;
351 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000352 Pieces.push_back(AsmStringPiece(N, Modifier));
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000353
354 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000355 continue;
356 }
357
Chris Lattner2ff0f422009-03-10 23:57:07 +0000358 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000359 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000360 }
361}
362
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000363//===----------------------------------------------------------------------===//
364// Constructors
365//===----------------------------------------------------------------------===//
366
Anders Carlssondfab34a2008-02-05 23:03:50 +0000367AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000368 unsigned numoutputs, unsigned numinputs,
369 std::string *names, StringLiteral **constraints,
370 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
371 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000372 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000373 , IsSimple(issimple), IsVolatile(isvolatile)
374 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000375 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
376 Names.push_back(names[i]);
377 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000378 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000379 }
Nico Weber608b17f2008-08-05 23:15:29 +0000380
Anders Carlssonb235fc22007-11-22 01:36:19 +0000381 for (unsigned i = 0; i != numclobbers; i++)
382 Clobbers.push_back(clobbers[i]);
383}
384
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000385ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
386 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000387 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000388: Stmt(ObjCForCollectionStmtClass) {
389 SubExprs[ELEM] = Elem;
390 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
391 SubExprs[BODY] = Body;
392 ForLoc = FCL;
393 RParenLoc = RPL;
394}
395
396
Nico Weber608b17f2008-08-05 23:15:29 +0000397ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
398 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000399 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000400 Stmt *atCatchList)
401: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000402 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000403 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000404 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000405 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000406 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000407 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
408
Nico Weber608b17f2008-08-05 23:15:29 +0000409 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000410 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000411
Ted Kremenekff981022008-02-01 21:28:59 +0000412 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000413 }
414 AtCatchLoc = atCatchLoc;
415 RParenLoc = rparenloc;
416}
417
418
Ted Kremenek82977772007-08-24 21:09:09 +0000419//===----------------------------------------------------------------------===//
420// Child Iterators for iterating over subexpressions/substatements
421//===----------------------------------------------------------------------===//
422
423// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000424Stmt::child_iterator DeclStmt::child_begin() {
425 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000426}
427
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000428Stmt::child_iterator DeclStmt::child_end() {
429 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000430}
431
Ted Kremenek82977772007-08-24 21:09:09 +0000432// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000433Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
434Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000435
436// CompoundStmt
437Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000438Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000439
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000440// CaseStmt
441Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
442Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
443
444// DefaultStmt
445Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
446Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000447
448// LabelStmt
449Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000450Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000451
452// IfStmt
453Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
454Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
455
456// SwitchStmt
457Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
458Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
459
460// WhileStmt
461Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
462Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
463
464// DoStmt
465Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
466Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
467
468// ForStmt
469Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
470Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
471
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000472// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000473Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
474 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000475}
Nico Weber608b17f2008-08-05 23:15:29 +0000476Stmt::child_iterator ObjCForCollectionStmt::child_end() {
477 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000478}
479
Ted Kremenek82977772007-08-24 21:09:09 +0000480// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000481Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
482Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000483
484// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000485Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
486const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000487
Ted Kremenek1060aff2008-06-17 03:11:08 +0000488Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
489Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000490
491// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000492Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
493Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000494
495// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000496Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
497Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000498
499// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000500const Expr* ReturnStmt::getRetValue() const {
501 return cast_or_null<Expr>(RetExpr);
502}
503Expr* ReturnStmt::getRetValue() {
504 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000505}
506
Ted Kremenek1060aff2008-06-17 03:11:08 +0000507Stmt::child_iterator ReturnStmt::child_begin() {
508 return &RetExpr;
509}
510Stmt::child_iterator ReturnStmt::child_end() {
511 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000512}
Ted Kremenek82977772007-08-24 21:09:09 +0000513
Chris Lattnerfe795952007-10-29 04:04:16 +0000514// AsmStmt
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000515Stmt::child_iterator AsmStmt::child_begin() {
516 return Exprs.empty() ? 0 : &Exprs[0];
517}
518Stmt::child_iterator AsmStmt::child_end() {
519 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
520}
Chris Lattnerfe795952007-10-29 04:04:16 +0000521
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522// ObjCAtCatchStmt
523Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000524Stmt::child_iterator ObjCAtCatchStmt::child_end() {
525 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000526}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000527
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000528// ObjCAtFinallyStmt
529Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
530Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000531
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000532// ObjCAtTryStmt
533Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000534Stmt::child_iterator ObjCAtTryStmt::child_end() {
535 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000536}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000537
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000538// ObjCAtThrowStmt
539Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000540 return &Throw;
541}
542
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000543Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000544 return &Throw+1;
545}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000546
547// ObjCAtSynchronizedStmt
548Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000549 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000550}
551
552Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000553 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000554}
555
Sebastian Redl4b07b292008-12-22 19:15:10 +0000556// CXXCatchStmt
557Stmt::child_iterator CXXCatchStmt::child_begin() {
558 return &HandlerBlock;
559}
560
561Stmt::child_iterator CXXCatchStmt::child_end() {
562 return &HandlerBlock + 1;
563}
564
565QualType CXXCatchStmt::getCaughtType() {
566 if (ExceptionDecl)
Douglas Gregord308e622009-05-18 20:51:54 +0000567 return ExceptionDecl->getType();
Sebastian Redl4b07b292008-12-22 19:15:10 +0000568 return QualType();
569}
570
571void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000572 if (ExceptionDecl)
573 ExceptionDecl->Destroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000574 Stmt::Destroy(C);
575}
Sebastian Redl8351da02008-12-22 21:35:02 +0000576
577// CXXTryStmt
578Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
579Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
580
581CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
582 Stmt **handlers, unsigned numHandlers)
583 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
584 Stmts.push_back(tryBlock);
585 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
586}