blob: 3a838fadafa415bb6880c5b824e3d624ba28b10c [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"
Torok Edwinf42e4a62009-08-24 13:25:12 +000022#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Reid Spencer5f016e22007-07-11 17:01:13 +000025static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000026 const char *Name;
27 unsigned Counter;
28 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000029} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000030
31static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
32 static bool Initialized = false;
33 if (Initialized)
34 return StmtClassInfo[E];
35
36 // Intialize the table on the first use.
37 Initialized = true;
Douglas Gregorf2cad862008-11-14 12:46:07 +000038#define STMT(CLASS, PARENT) \
39 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
40 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Reid Spencer5f016e22007-07-11 17:01:13 +000041#include "clang/AST/StmtNodes.def"
Nico Weber608b17f2008-08-05 23:15:29 +000042
Chris Lattner63381352007-08-25 01:42:24 +000043 return StmtClassInfo[E];
44}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046const char *Stmt::getStmtClassName() const {
Douglas Gregor43d9d922009-08-08 01:41:12 +000047 return getStmtInfoTableEntry((StmtClass)sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
Chris Lattner24e1e702009-03-04 04:23:07 +000050void Stmt::DestroyChildren(ASTContext &C) {
51 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
Douglas Gregor860f6d42009-01-16 06:50:08 +000052 if (Stmt* Child = *I++) Child->Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +000053}
54
Douglas Gregor42602bb2009-08-07 06:08:38 +000055void Stmt::DoDestroy(ASTContext &C) {
Ted Kremenek27f8a282008-05-20 00:43:19 +000056 DestroyChildren(C);
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000057 this->~Stmt();
58 C.Deallocate((void *)this);
Ted Kremenek9c1863e2008-05-19 22:02:12 +000059}
60
Reid Spencer5f016e22007-07-11 17:01:13 +000061void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000062 // Ensure the table is primed.
63 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065 unsigned sum = 0;
66 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000067 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000068 if (StmtClassInfo[i].Name == 0) continue;
69 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
71 fprintf(stderr, " %d stmts/exprs total.\n", sum);
72 sum = 0;
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;
Douglas Gregordbe833d2009-05-26 14:40:08 +000075 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000076 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000077 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
78 StmtClassInfo[i].Size,
79 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
80 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000081 }
82 fprintf(stderr, "Total bytes = %d\n", sum);
83}
84
85void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000086 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
89static bool StatSwitch = false;
90
91bool Stmt::CollectingStats(bool enable) {
92 if (enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000093 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000094}
95
Douglas Gregor43d9d922009-08-08 01:41:12 +000096void SwitchStmt::DoDestroy(ASTContext &Ctx) {
97 // Destroy the SwitchCase statements in this switch. In the normal
Mike Stump1eb44332009-09-09 15:08:12 +000098 // case, this loop will merely decrement the reference counts from
Douglas Gregor43d9d922009-08-08 01:41:12 +000099 // the Retain() calls in addSwitchCase();
100 SwitchCase *SC = FirstCase;
101 while (SC) {
102 SwitchCase *Next = SC->getNextSwitchCase();
103 SC->Destroy(Ctx);
104 SC = Next;
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Douglas Gregor43d9d922009-08-08 01:41:12 +0000107 Stmt::DoDestroy(Ctx);
108}
109
Douglas Gregor025452f2009-04-17 00:04:06 +0000110void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
111 if (this->Body)
112 C.Deallocate(Body);
113 this->NumStmts = NumStmts;
114
115 Body = new (C) Stmt*[NumStmts];
116 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
117}
Reid Spencer5f016e22007-07-11 17:01:13 +0000118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119const char *LabelStmt::getName() const {
120 return getID()->getName();
121}
122
Steve Naroff507f2d52007-08-31 23:49:30 +0000123// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000124SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000125 if (RetExpr)
126 return SourceRange(RetLoc, RetExpr->getLocEnd());
127 else
128 return SourceRange(RetLoc);
129}
130
Ted Kremenekd48ade62007-10-01 16:34:52 +0000131bool Stmt::hasImplicitControlFlow() const {
132 switch (sClass) {
133 default:
134 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000135
Ted Kremenekd48ade62007-10-01 16:34:52 +0000136 case CallExprClass:
137 case ConditionalOperatorClass:
138 case ChooseExprClass:
139 case StmtExprClass:
140 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000141 return true;
142
Ted Kremenekd48ade62007-10-01 16:34:52 +0000143 case Stmt::BinaryOperatorClass: {
144 const BinaryOperator* B = cast<BinaryOperator>(this);
145 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
146 return true;
147 else
148 return false;
149 }
150 }
151}
152
Chris Lattnerb3277932009-03-10 04:59:06 +0000153Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000154 return cast<Expr>(Exprs[i]);
155}
Chris Lattnerb3277932009-03-10 04:59:06 +0000156
157/// getOutputConstraint - Return the constraint string for the specified
158/// output operand. All output constraints are known to be non-empty (either
159/// '=' or '+').
160std::string AsmStmt::getOutputConstraint(unsigned i) const {
161 return std::string(Constraints[i]->getStrData(),
162 Constraints[i]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000163}
Chris Lattnerb3277932009-03-10 04:59:06 +0000164
Chris Lattner85759272009-03-11 00:23:13 +0000165/// getNumPlusOperands - Return the number of output operands that have a "+"
166/// constraint.
167unsigned AsmStmt::getNumPlusOperands() const {
168 unsigned Res = 0;
169 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
170 if (isOutputPlusConstraint(i))
171 ++Res;
172 return Res;
173}
174
175
Chris Lattnerb3277932009-03-10 04:59:06 +0000176
177Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000178 return cast<Expr>(Exprs[i + NumOutputs]);
179}
Chris Lattnerb3277932009-03-10 04:59:06 +0000180
181/// getInputConstraint - Return the specified input constraint. Unlike output
182/// constraints, these can be empty.
183std::string AsmStmt::getInputConstraint(unsigned i) const {
184 return std::string(Constraints[i + NumOutputs]->getStrData(),
185 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000186}
187
Chris Lattner10ca96a2009-03-10 06:33:24 +0000188
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000189void AsmStmt::setOutputsAndInputs(unsigned NumOutputs,
Mike Stump1eb44332009-09-09 15:08:12 +0000190 unsigned NumInputs,
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000191 const std::string *Names,
192 StringLiteral **Constraints,
193 Stmt **Exprs) {
194 this->NumOutputs = NumOutputs;
195 this->NumInputs = NumInputs;
196 this->Names.clear();
197 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
198 this->Constraints.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000199 this->Constraints.insert(this->Constraints.end(),
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000200 Constraints, Constraints + NumOutputs + NumInputs);
201 this->Exprs.clear();
202 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
203}
204
Chris Lattner10ca96a2009-03-10 06:33:24 +0000205/// getNamedOperand - Given a symbolic operand reference like %[foo],
206/// translate this into a numeric value needed to reference the same operand.
207/// This returns -1 if the operand name is invalid.
208int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
209 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattner10ca96a2009-03-10 06:33:24 +0000211 // Check if this is an output operand.
212 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
213 if (getOutputName(i) == SymbolicName)
214 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Chris Lattner10ca96a2009-03-10 06:33:24 +0000217 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
218 if (getInputName(i) == SymbolicName)
219 return getNumOutputs() + NumPlusOperands + i;
220
221 // Not found.
222 return -1;
223}
224
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000225void AsmStmt::setClobbers(StringLiteral **Clobbers, unsigned NumClobbers) {
226 this->Clobbers.clear();
227 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
228}
Chris Lattner10ca96a2009-03-10 06:33:24 +0000229
Chris Lattner458cd9c2009-03-10 23:21:44 +0000230/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
231/// it into pieces. If the asm string is erroneous, emit errors and return
232/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000233unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
234 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000235 const char *StrStart = getAsmString()->getStrData();
236 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000237 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Chris Lattner458cd9c2009-03-10 23:21:44 +0000239 // "Simple" inline asms have no constraints or operands, just convert the asm
240 // string to escape $'s.
241 if (isSimple()) {
242 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000243 for (; CurPtr != StrEnd; ++CurPtr) {
244 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000245 case '$':
246 Result += "$$";
247 break;
248 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000249 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000250 break;
251 }
252 }
253 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000254 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000255 }
256
257 // CurStringPiece - The current string that we are building up as we scan the
258 // asm string.
259 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Chris Lattner458cd9c2009-03-10 23:21:44 +0000261 while (1) {
262 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000263 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000264 if (!CurStringPiece.empty())
265 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000266 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattner3182db12009-03-10 23:51:40 +0000269 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000270 if (CurChar == '$') {
271 CurStringPiece += "$$";
272 continue;
273 } else if (CurChar != '%') {
274 CurStringPiece += CurChar;
275 continue;
276 }
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner458cd9c2009-03-10 23:21:44 +0000278 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000279 if (CurPtr == StrEnd) {
280 // % at end of string is invalid (no escape).
281 DiagOffs = CurPtr-StrStart-1;
282 return diag::err_asm_invalid_escape;
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattner3182db12009-03-10 23:51:40 +0000285 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000286 if (EscapedChar == '%') { // %% -> %
287 // Escaped percentage sign.
288 CurStringPiece += '%';
289 continue;
290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattner458cd9c2009-03-10 23:21:44 +0000292 if (EscapedChar == '=') { // %= -> Generate an unique ID.
293 CurStringPiece += "${:uid}";
294 continue;
295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattner458cd9c2009-03-10 23:21:44 +0000297 // Otherwise, we have an operand. If we have accumulated a string so far,
298 // add it to the Pieces list.
299 if (!CurStringPiece.empty()) {
300 Pieces.push_back(AsmStringPiece(CurStringPiece));
301 CurStringPiece.clear();
302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattner458cd9c2009-03-10 23:21:44 +0000304 // Handle %x4 and %x[foo] by capturing x as the modifier character.
305 char Modifier = '\0';
306 if (isalpha(EscapedChar)) {
307 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000308 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Chris Lattner458cd9c2009-03-10 23:21:44 +0000311 if (isdigit(EscapedChar)) {
312 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000313 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattnercafc2222009-03-11 22:52:17 +0000315 --CurPtr;
316 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000317 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Chris Lattner85759272009-03-11 00:23:13 +0000319 unsigned NumOperands =
320 getNumOutputs() + getNumPlusOperands() + getNumInputs();
321 if (N >= NumOperands) {
322 DiagOffs = CurPtr-StrStart-1;
323 return diag::err_asm_invalid_operand_number;
324 }
325
Chris Lattner458cd9c2009-03-10 23:21:44 +0000326 Pieces.push_back(AsmStringPiece(N, Modifier));
327 continue;
328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner458cd9c2009-03-10 23:21:44 +0000330 // Handle %[foo], a symbolic operand reference.
331 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000332 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000334 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000335 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000336 if (NameEnd == 0)
337 return diag::err_asm_unterminated_symbolic_operand_name;
338 if (NameEnd == CurPtr)
339 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattner3182db12009-03-10 23:51:40 +0000341 std::string SymbolicName(CurPtr, NameEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Chris Lattner458cd9c2009-03-10 23:21:44 +0000343 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000344 if (N == -1) {
345 // Verify that an operand with that name exists.
346 DiagOffs = CurPtr-StrStart;
347 return diag::err_asm_unknown_symbolic_operand_name;
348 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000349 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000351 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000352 continue;
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner2ff0f422009-03-10 23:57:07 +0000355 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000356 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000357 }
358}
359
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000360//===----------------------------------------------------------------------===//
361// Constructors
362//===----------------------------------------------------------------------===//
363
Anders Carlssondfab34a2008-02-05 23:03:50 +0000364AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000365 unsigned numoutputs, unsigned numinputs,
366 std::string *names, StringLiteral **constraints,
367 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
368 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000369 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000370 , IsSimple(issimple), IsVolatile(isvolatile)
371 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000372 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
373 Names.push_back(names[i]);
374 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000375 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000376 }
Nico Weber608b17f2008-08-05 23:15:29 +0000377
Anders Carlssonb235fc22007-11-22 01:36:19 +0000378 for (unsigned i = 0; i != numclobbers; i++)
379 Clobbers.push_back(clobbers[i]);
380}
381
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000382ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
383 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000384 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000385: Stmt(ObjCForCollectionStmtClass) {
386 SubExprs[ELEM] = Elem;
387 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
388 SubExprs[BODY] = Body;
389 ForLoc = FCL;
390 RParenLoc = RPL;
391}
392
393
Nico Weber608b17f2008-08-05 23:15:29 +0000394ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
395 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000396 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000397 Stmt *atCatchList)
398: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000399 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000400 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000401 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000402 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000403 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000404 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
405
Nico Weber608b17f2008-08-05 23:15:29 +0000406 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000407 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000408
Ted Kremenekff981022008-02-01 21:28:59 +0000409 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000410 }
411 AtCatchLoc = atCatchLoc;
412 RParenLoc = rparenloc;
413}
414
415
Ted Kremenek82977772007-08-24 21:09:09 +0000416//===----------------------------------------------------------------------===//
417// Child Iterators for iterating over subexpressions/substatements
418//===----------------------------------------------------------------------===//
419
420// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000421Stmt::child_iterator DeclStmt::child_begin() {
422 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000423}
424
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000425Stmt::child_iterator DeclStmt::child_end() {
426 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000427}
428
Ted Kremenek82977772007-08-24 21:09:09 +0000429// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000430Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
431Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000432
433// CompoundStmt
434Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000435Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000436
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000437// CaseStmt
438Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
439Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
440
441// DefaultStmt
442Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
443Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000444
445// LabelStmt
446Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000447Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000448
449// IfStmt
450Stmt::child_iterator IfStmt::child_begin() { return &SubExprs[0]; }
451Stmt::child_iterator IfStmt::child_end() { return &SubExprs[0]+END_EXPR; }
452
453// SwitchStmt
454Stmt::child_iterator SwitchStmt::child_begin() { return &SubExprs[0]; }
455Stmt::child_iterator SwitchStmt::child_end() { return &SubExprs[0]+END_EXPR; }
456
457// WhileStmt
458Stmt::child_iterator WhileStmt::child_begin() { return &SubExprs[0]; }
459Stmt::child_iterator WhileStmt::child_end() { return &SubExprs[0]+END_EXPR; }
460
461// DoStmt
462Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
463Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
464
465// ForStmt
466Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
467Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
468
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000469// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000470Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
471 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000472}
Nico Weber608b17f2008-08-05 23:15:29 +0000473Stmt::child_iterator ObjCForCollectionStmt::child_end() {
474 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000475}
476
Ted Kremenek82977772007-08-24 21:09:09 +0000477// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000478Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
479Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000480
481// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000482Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
483const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000484
Ted Kremenek1060aff2008-06-17 03:11:08 +0000485Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
486Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000487
488// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000489Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
490Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000491
492// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000493Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
494Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000495
496// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000497const Expr* ReturnStmt::getRetValue() const {
498 return cast_or_null<Expr>(RetExpr);
499}
500Expr* ReturnStmt::getRetValue() {
501 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000502}
503
Ted Kremenek1060aff2008-06-17 03:11:08 +0000504Stmt::child_iterator ReturnStmt::child_begin() {
505 return &RetExpr;
506}
507Stmt::child_iterator ReturnStmt::child_end() {
508 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000509}
Ted Kremenek82977772007-08-24 21:09:09 +0000510
Chris Lattnerfe795952007-10-29 04:04:16 +0000511// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000512Stmt::child_iterator AsmStmt::child_begin() {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000513 return Exprs.empty() ? 0 : &Exprs[0];
514}
515Stmt::child_iterator AsmStmt::child_end() {
516 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
517}
Chris Lattnerfe795952007-10-29 04:04:16 +0000518
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000519// ObjCAtCatchStmt
520Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000521Stmt::child_iterator ObjCAtCatchStmt::child_end() {
522 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000523}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000524
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000525// ObjCAtFinallyStmt
526Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
527Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000528
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000529// ObjCAtTryStmt
530Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000531Stmt::child_iterator ObjCAtTryStmt::child_end() {
532 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000533}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000534
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000535// ObjCAtThrowStmt
536Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000537 return &Throw;
538}
539
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000540Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000541 return &Throw+1;
542}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000543
544// ObjCAtSynchronizedStmt
545Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000546 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000547}
548
549Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000550 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000551}
552
Sebastian Redl4b07b292008-12-22 19:15:10 +0000553// CXXCatchStmt
554Stmt::child_iterator CXXCatchStmt::child_begin() {
555 return &HandlerBlock;
556}
557
558Stmt::child_iterator CXXCatchStmt::child_end() {
559 return &HandlerBlock + 1;
560}
561
562QualType CXXCatchStmt::getCaughtType() {
563 if (ExceptionDecl)
Douglas Gregord308e622009-05-18 20:51:54 +0000564 return ExceptionDecl->getType();
Sebastian Redl4b07b292008-12-22 19:15:10 +0000565 return QualType();
566}
567
Douglas Gregor42602bb2009-08-07 06:08:38 +0000568void CXXCatchStmt::DoDestroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000569 if (ExceptionDecl)
570 ExceptionDecl->Destroy(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +0000571 Stmt::DoDestroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000572}
Sebastian Redl8351da02008-12-22 21:35:02 +0000573
574// CXXTryStmt
575Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
576Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
577
578CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
579 Stmt **handlers, unsigned numHandlers)
580 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
581 Stmts.push_back(tryBlock);
582 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
583}