blob: 17577910d2a36da81ffcf4018648c22f75fe259d [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;
Douglas Gregordbe833d2009-05-26 14:40:08 +000081 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000082 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000083 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
84 StmtClassInfo[i].Size,
85 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
86 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000087 }
88 fprintf(stderr, "Total bytes = %d\n", sum);
89}
90
91void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000092 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000093}
94
95static bool StatSwitch = false;
96
97bool Stmt::CollectingStats(bool enable) {
98 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000099 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100}
101
Anders Carlssond19cd902009-05-15 00:21:21 +0000102NullStmt* NullStmt::Clone(ASTContext &C) const {
103 return new (C) NullStmt(SemiLoc);
104}
105
Douglas Gregor861ce312009-05-15 22:32:39 +0000106ContinueStmt* ContinueStmt::Clone(ASTContext &C) const {
107 return new (C) ContinueStmt(ContinueLoc);
108}
109
110BreakStmt* BreakStmt::Clone(ASTContext &C) const {
111 return new (C) BreakStmt(BreakLoc);
112}
113
Douglas Gregor025452f2009-04-17 00:04:06 +0000114void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
115 if (this->Body)
116 C.Deallocate(Body);
117 this->NumStmts = NumStmts;
118
119 Body = new (C) Stmt*[NumStmts];
120 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
121}
Reid Spencer5f016e22007-07-11 17:01:13 +0000122
Reid Spencer5f016e22007-07-11 17:01:13 +0000123const char *LabelStmt::getName() const {
124 return getID()->getName();
125}
126
Steve Naroff507f2d52007-08-31 23:49:30 +0000127// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000128SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000129 if (RetExpr)
130 return SourceRange(RetLoc, RetExpr->getLocEnd());
131 else
132 return SourceRange(RetLoc);
133}
134
Ted Kremenekd48ade62007-10-01 16:34:52 +0000135bool Stmt::hasImplicitControlFlow() const {
136 switch (sClass) {
137 default:
138 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000139
Ted Kremenekd48ade62007-10-01 16:34:52 +0000140 case CallExprClass:
141 case ConditionalOperatorClass:
142 case ChooseExprClass:
143 case StmtExprClass:
144 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000145 return true;
146
Ted Kremenekd48ade62007-10-01 16:34:52 +0000147 case Stmt::BinaryOperatorClass: {
148 const BinaryOperator* B = cast<BinaryOperator>(this);
149 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
150 return true;
151 else
152 return false;
153 }
154 }
155}
156
Chris Lattnerb3277932009-03-10 04:59:06 +0000157Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000158 return cast<Expr>(Exprs[i]);
159}
Chris Lattnerb3277932009-03-10 04:59:06 +0000160
161/// getOutputConstraint - Return the constraint string for the specified
162/// output operand. All output constraints are known to be non-empty (either
163/// '=' or '+').
164std::string AsmStmt::getOutputConstraint(unsigned i) const {
165 return std::string(Constraints[i]->getStrData(),
166 Constraints[i]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000167}
Chris Lattnerb3277932009-03-10 04:59:06 +0000168
Chris Lattner85759272009-03-11 00:23:13 +0000169/// getNumPlusOperands - Return the number of output operands that have a "+"
170/// constraint.
171unsigned AsmStmt::getNumPlusOperands() const {
172 unsigned Res = 0;
173 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
174 if (isOutputPlusConstraint(i))
175 ++Res;
176 return Res;
177}
178
179
Chris Lattnerb3277932009-03-10 04:59:06 +0000180
181Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000182 return cast<Expr>(Exprs[i + NumOutputs]);
183}
Chris Lattnerb3277932009-03-10 04:59:06 +0000184
185/// getInputConstraint - Return the specified input constraint. Unlike output
186/// constraints, these can be empty.
187std::string AsmStmt::getInputConstraint(unsigned i) const {
188 return std::string(Constraints[i + NumOutputs]->getStrData(),
189 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000190}
191
Chris Lattner10ca96a2009-03-10 06:33:24 +0000192
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000193void AsmStmt::setOutputsAndInputs(unsigned NumOutputs,
194 unsigned NumInputs,
195 const std::string *Names,
196 StringLiteral **Constraints,
197 Stmt **Exprs) {
198 this->NumOutputs = NumOutputs;
199 this->NumInputs = NumInputs;
200 this->Names.clear();
201 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
202 this->Constraints.clear();
203 this->Constraints.insert(this->Constraints.end(),
204 Constraints, Constraints + NumOutputs + NumInputs);
205 this->Exprs.clear();
206 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
207}
208
Chris Lattner10ca96a2009-03-10 06:33:24 +0000209/// getNamedOperand - Given a symbolic operand reference like %[foo],
210/// translate this into a numeric value needed to reference the same operand.
211/// This returns -1 if the operand name is invalid.
212int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
213 unsigned NumPlusOperands = 0;
214
215 // Check if this is an output operand.
216 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
217 if (getOutputName(i) == SymbolicName)
218 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000219 }
220
221 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
222 if (getInputName(i) == SymbolicName)
223 return getNumOutputs() + NumPlusOperands + i;
224
225 // Not found.
226 return -1;
227}
228
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000229void AsmStmt::setClobbers(StringLiteral **Clobbers, unsigned NumClobbers) {
230 this->Clobbers.clear();
231 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
232}
Chris Lattner10ca96a2009-03-10 06:33:24 +0000233
Chris Lattner458cd9c2009-03-10 23:21:44 +0000234/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
235/// it into pieces. If the asm string is erroneous, emit errors and return
236/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000237unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
238 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000239 const char *StrStart = getAsmString()->getStrData();
240 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000241 const char *CurPtr = StrStart;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000242
243 // "Simple" inline asms have no constraints or operands, just convert the asm
244 // string to escape $'s.
245 if (isSimple()) {
246 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000247 for (; CurPtr != StrEnd; ++CurPtr) {
248 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000249 case '$':
250 Result += "$$";
251 break;
252 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000253 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000254 break;
255 }
256 }
257 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000258 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000259 }
260
261 // CurStringPiece - The current string that we are building up as we scan the
262 // asm string.
263 std::string CurStringPiece;
264
265 while (1) {
266 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000267 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000268 if (!CurStringPiece.empty())
269 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000270 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000271 }
272
Chris Lattner3182db12009-03-10 23:51:40 +0000273 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000274 if (CurChar == '$') {
275 CurStringPiece += "$$";
276 continue;
277 } else if (CurChar != '%') {
278 CurStringPiece += CurChar;
279 continue;
280 }
281
282 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000283 if (CurPtr == StrEnd) {
284 // % at end of string is invalid (no escape).
285 DiagOffs = CurPtr-StrStart-1;
286 return diag::err_asm_invalid_escape;
287 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000288
Chris Lattner3182db12009-03-10 23:51:40 +0000289 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000290 if (EscapedChar == '%') { // %% -> %
291 // Escaped percentage sign.
292 CurStringPiece += '%';
293 continue;
294 }
295
296 if (EscapedChar == '=') { // %= -> Generate an unique ID.
297 CurStringPiece += "${:uid}";
298 continue;
299 }
300
301 // Otherwise, we have an operand. If we have accumulated a string so far,
302 // add it to the Pieces list.
303 if (!CurStringPiece.empty()) {
304 Pieces.push_back(AsmStringPiece(CurStringPiece));
305 CurStringPiece.clear();
306 }
307
308 // Handle %x4 and %x[foo] by capturing x as the modifier character.
309 char Modifier = '\0';
310 if (isalpha(EscapedChar)) {
311 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000312 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000313 }
314
315 if (isdigit(EscapedChar)) {
316 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000317 unsigned N = 0;
318
319 --CurPtr;
320 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000321 N = N*10 + ((*CurPtr++)-'0');
Chris Lattner85759272009-03-11 00:23:13 +0000322
323 unsigned NumOperands =
324 getNumOutputs() + getNumPlusOperands() + getNumInputs();
325 if (N >= NumOperands) {
326 DiagOffs = CurPtr-StrStart-1;
327 return diag::err_asm_invalid_operand_number;
328 }
329
Chris Lattner458cd9c2009-03-10 23:21:44 +0000330 Pieces.push_back(AsmStringPiece(N, Modifier));
331 continue;
332 }
333
334 // Handle %[foo], a symbolic operand reference.
335 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000336 DiagOffs = CurPtr-StrStart-1;
337
338 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000339 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000340 if (NameEnd == 0)
341 return diag::err_asm_unterminated_symbolic_operand_name;
342 if (NameEnd == CurPtr)
343 return diag::err_asm_empty_symbolic_operand_name;
344
Chris Lattner3182db12009-03-10 23:51:40 +0000345 std::string SymbolicName(CurPtr, NameEnd);
Chris Lattner458cd9c2009-03-10 23:21:44 +0000346
347 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000348 if (N == -1) {
349 // Verify that an operand with that name exists.
350 DiagOffs = CurPtr-StrStart;
351 return diag::err_asm_unknown_symbolic_operand_name;
352 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000353 Pieces.push_back(AsmStringPiece(N, Modifier));
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000354
355 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000356 continue;
357 }
358
Chris Lattner2ff0f422009-03-10 23:57:07 +0000359 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000360 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000361 }
362}
363
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000364//===----------------------------------------------------------------------===//
365// Constructors
366//===----------------------------------------------------------------------===//
367
Anders Carlssondfab34a2008-02-05 23:03:50 +0000368AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000369 unsigned numoutputs, unsigned numinputs,
370 std::string *names, StringLiteral **constraints,
371 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
372 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000373 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000374 , IsSimple(issimple), IsVolatile(isvolatile)
375 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000376 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
377 Names.push_back(names[i]);
378 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000379 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000380 }
Nico Weber608b17f2008-08-05 23:15:29 +0000381
Anders Carlssonb235fc22007-11-22 01:36:19 +0000382 for (unsigned i = 0; i != numclobbers; i++)
383 Clobbers.push_back(clobbers[i]);
384}
385
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000386ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
387 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000388 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000389: Stmt(ObjCForCollectionStmtClass) {
390 SubExprs[ELEM] = Elem;
391 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
392 SubExprs[BODY] = Body;
393 ForLoc = FCL;
394 RParenLoc = RPL;
395}
396
397
Nico Weber608b17f2008-08-05 23:15:29 +0000398ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
399 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000400 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000401 Stmt *atCatchList)
402: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000403 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000404 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000405 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000406 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000407 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000408 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
409
Nico Weber608b17f2008-08-05 23:15:29 +0000410 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000411 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000412
Ted Kremenekff981022008-02-01 21:28:59 +0000413 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000414 }
415 AtCatchLoc = atCatchLoc;
416 RParenLoc = rparenloc;
417}
418
419
Ted Kremenek82977772007-08-24 21:09:09 +0000420//===----------------------------------------------------------------------===//
421// Child Iterators for iterating over subexpressions/substatements
422//===----------------------------------------------------------------------===//
423
424// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000425Stmt::child_iterator DeclStmt::child_begin() {
426 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000427}
428
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000429Stmt::child_iterator DeclStmt::child_end() {
430 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000431}
432
Ted Kremenek82977772007-08-24 21:09:09 +0000433// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000434Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
435Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000436
437// CompoundStmt
438Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000439Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000440
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000441// CaseStmt
442Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
443Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
444
445// DefaultStmt
446Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
447Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000448
449// LabelStmt
450Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000451Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000452
453// IfStmt
454Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
455Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
456
457// SwitchStmt
458Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
459Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
460
461// WhileStmt
462Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
463Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
464
465// DoStmt
466Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
467Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
468
469// ForStmt
470Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
471Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
472
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000473// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000474Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
475 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000476}
Nico Weber608b17f2008-08-05 23:15:29 +0000477Stmt::child_iterator ObjCForCollectionStmt::child_end() {
478 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000479}
480
Ted Kremenek82977772007-08-24 21:09:09 +0000481// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000482Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
483Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000484
485// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000486Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
487const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000488
Ted Kremenek1060aff2008-06-17 03:11:08 +0000489Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
490Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000491
492// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000493Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
494Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000495
496// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000497Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
498Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000499
500// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000501const Expr* ReturnStmt::getRetValue() const {
502 return cast_or_null<Expr>(RetExpr);
503}
504Expr* ReturnStmt::getRetValue() {
505 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000506}
507
Ted Kremenek1060aff2008-06-17 03:11:08 +0000508Stmt::child_iterator ReturnStmt::child_begin() {
509 return &RetExpr;
510}
511Stmt::child_iterator ReturnStmt::child_end() {
512 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000513}
Ted Kremenek82977772007-08-24 21:09:09 +0000514
Chris Lattnerfe795952007-10-29 04:04:16 +0000515// AsmStmt
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000516Stmt::child_iterator AsmStmt::child_begin() {
517 return Exprs.empty() ? 0 : &Exprs[0];
518}
519Stmt::child_iterator AsmStmt::child_end() {
520 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
521}
Chris Lattnerfe795952007-10-29 04:04:16 +0000522
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000523// ObjCAtCatchStmt
524Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000525Stmt::child_iterator ObjCAtCatchStmt::child_end() {
526 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000527}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000528
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000529// ObjCAtFinallyStmt
530Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
531Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000532
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000533// ObjCAtTryStmt
534Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000535Stmt::child_iterator ObjCAtTryStmt::child_end() {
536 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000537}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000538
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000539// ObjCAtThrowStmt
540Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000541 return &Throw;
542}
543
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000544Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000545 return &Throw+1;
546}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000547
548// ObjCAtSynchronizedStmt
549Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000550 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000551}
552
553Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000554 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000555}
556
Sebastian Redl4b07b292008-12-22 19:15:10 +0000557// CXXCatchStmt
558Stmt::child_iterator CXXCatchStmt::child_begin() {
559 return &HandlerBlock;
560}
561
562Stmt::child_iterator CXXCatchStmt::child_end() {
563 return &HandlerBlock + 1;
564}
565
566QualType CXXCatchStmt::getCaughtType() {
567 if (ExceptionDecl)
Douglas Gregord308e622009-05-18 20:51:54 +0000568 return ExceptionDecl->getType();
Sebastian Redl4b07b292008-12-22 19:15:10 +0000569 return QualType();
570}
571
572void CXXCatchStmt::Destroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000573 if (ExceptionDecl)
574 ExceptionDecl->Destroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000575 Stmt::Destroy(C);
576}
Sebastian Redl8351da02008-12-22 21:35:02 +0000577
578// CXXTryStmt
579Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
580Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
581
582CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
583 Stmt **handlers, unsigned numHandlers)
584 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
585 Stmts.push_back(tryBlock);
586 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
587}