blob: 104e3361892f062d113be442e0a715ca0ad35e1c [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
50void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000051 // Ensure the table is primed.
52 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000053
Reid Spencer5f016e22007-07-11 17:01:13 +000054 unsigned sum = 0;
55 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000056 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000057 if (StmtClassInfo[i].Name == 0) continue;
58 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 }
60 fprintf(stderr, " %d stmts/exprs total.\n", sum);
61 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000062 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000063 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000064 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000065 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000066 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
67 StmtClassInfo[i].Size,
68 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
69 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
71 fprintf(stderr, "Total bytes = %d\n", sum);
72}
73
74void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000075 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000076}
77
78static bool StatSwitch = false;
79
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000080bool Stmt::CollectingStats(bool Enable) {
81 if (Enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000082 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000083}
84
Douglas Gregor025452f2009-04-17 00:04:06 +000085void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
86 if (this->Body)
87 C.Deallocate(Body);
88 this->NumStmts = NumStmts;
89
90 Body = new (C) Stmt*[NumStmts];
91 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
92}
Reid Spencer5f016e22007-07-11 17:01:13 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094const char *LabelStmt::getName() const {
Daniel Dunbare013d682009-10-18 20:26:12 +000095 return getID()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +000096}
97
Steve Naroff507f2d52007-08-31 23:49:30 +000098// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +000099SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000100 if (RetExpr)
101 return SourceRange(RetLoc, RetExpr->getLocEnd());
102 else
103 return SourceRange(RetLoc);
104}
105
Ted Kremenekd48ade62007-10-01 16:34:52 +0000106bool Stmt::hasImplicitControlFlow() const {
107 switch (sClass) {
108 default:
109 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000110
Ted Kremenekd48ade62007-10-01 16:34:52 +0000111 case CallExprClass:
112 case ConditionalOperatorClass:
113 case ChooseExprClass:
114 case StmtExprClass:
115 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000116 return true;
117
Ted Kremenekd48ade62007-10-01 16:34:52 +0000118 case Stmt::BinaryOperatorClass: {
119 const BinaryOperator* B = cast<BinaryOperator>(this);
120 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
121 return true;
122 else
123 return false;
124 }
125 }
126}
127
Chris Lattnerb3277932009-03-10 04:59:06 +0000128Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000129 return cast<Expr>(Exprs[i]);
130}
Chris Lattnerb3277932009-03-10 04:59:06 +0000131
132/// getOutputConstraint - Return the constraint string for the specified
133/// output operand. All output constraints are known to be non-empty (either
134/// '=' or '+').
135std::string AsmStmt::getOutputConstraint(unsigned i) const {
136 return std::string(Constraints[i]->getStrData(),
137 Constraints[i]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000138}
Chris Lattnerb3277932009-03-10 04:59:06 +0000139
Chris Lattner85759272009-03-11 00:23:13 +0000140/// getNumPlusOperands - Return the number of output operands that have a "+"
141/// constraint.
142unsigned AsmStmt::getNumPlusOperands() const {
143 unsigned Res = 0;
144 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
145 if (isOutputPlusConstraint(i))
146 ++Res;
147 return Res;
148}
149
150
Chris Lattnerb3277932009-03-10 04:59:06 +0000151
152Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000153 return cast<Expr>(Exprs[i + NumOutputs]);
154}
Chris Lattnerb3277932009-03-10 04:59:06 +0000155
156/// getInputConstraint - Return the specified input constraint. Unlike output
157/// constraints, these can be empty.
158std::string AsmStmt::getInputConstraint(unsigned i) const {
159 return std::string(Constraints[i + NumOutputs]->getStrData(),
160 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000161}
162
Chris Lattner10ca96a2009-03-10 06:33:24 +0000163
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000164void AsmStmt::setOutputsAndInputs(unsigned NumOutputs,
Mike Stump1eb44332009-09-09 15:08:12 +0000165 unsigned NumInputs,
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000166 const std::string *Names,
167 StringLiteral **Constraints,
168 Stmt **Exprs) {
169 this->NumOutputs = NumOutputs;
170 this->NumInputs = NumInputs;
171 this->Names.clear();
172 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
173 this->Constraints.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000174 this->Constraints.insert(this->Constraints.end(),
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000175 Constraints, Constraints + NumOutputs + NumInputs);
176 this->Exprs.clear();
177 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
178}
179
Chris Lattner10ca96a2009-03-10 06:33:24 +0000180/// getNamedOperand - Given a symbolic operand reference like %[foo],
181/// translate this into a numeric value needed to reference the same operand.
182/// This returns -1 if the operand name is invalid.
183int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
184 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Chris Lattner10ca96a2009-03-10 06:33:24 +0000186 // Check if this is an output operand.
187 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
188 if (getOutputName(i) == SymbolicName)
189 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Chris Lattner10ca96a2009-03-10 06:33:24 +0000192 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
193 if (getInputName(i) == SymbolicName)
194 return getNumOutputs() + NumPlusOperands + i;
195
196 // Not found.
197 return -1;
198}
199
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000200void AsmStmt::setClobbers(StringLiteral **Clobbers, unsigned NumClobbers) {
201 this->Clobbers.clear();
202 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
203}
Chris Lattner10ca96a2009-03-10 06:33:24 +0000204
Chris Lattner458cd9c2009-03-10 23:21:44 +0000205/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
206/// it into pieces. If the asm string is erroneous, emit errors and return
207/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000208unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
209 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000210 const char *StrStart = getAsmString()->getStrData();
211 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000212 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattner458cd9c2009-03-10 23:21:44 +0000214 // "Simple" inline asms have no constraints or operands, just convert the asm
215 // string to escape $'s.
216 if (isSimple()) {
217 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000218 for (; CurPtr != StrEnd; ++CurPtr) {
219 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000220 case '$':
221 Result += "$$";
222 break;
223 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000224 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000225 break;
226 }
227 }
228 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000229 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000230 }
231
232 // CurStringPiece - The current string that we are building up as we scan the
233 // asm string.
234 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattner458cd9c2009-03-10 23:21:44 +0000236 while (1) {
237 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000238 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000239 if (!CurStringPiece.empty())
240 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000241 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000242 }
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattner3182db12009-03-10 23:51:40 +0000244 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000245 if (CurChar == '$') {
246 CurStringPiece += "$$";
247 continue;
248 } else if (CurChar != '%') {
249 CurStringPiece += CurChar;
250 continue;
251 }
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Chris Lattner458cd9c2009-03-10 23:21:44 +0000253 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000254 if (CurPtr == StrEnd) {
255 // % at end of string is invalid (no escape).
256 DiagOffs = CurPtr-StrStart-1;
257 return diag::err_asm_invalid_escape;
258 }
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Chris Lattner3182db12009-03-10 23:51:40 +0000260 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000261 if (EscapedChar == '%') { // %% -> %
262 // Escaped percentage sign.
263 CurStringPiece += '%';
264 continue;
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Chris Lattner458cd9c2009-03-10 23:21:44 +0000267 if (EscapedChar == '=') { // %= -> Generate an unique ID.
268 CurStringPiece += "${:uid}";
269 continue;
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattner458cd9c2009-03-10 23:21:44 +0000272 // Otherwise, we have an operand. If we have accumulated a string so far,
273 // add it to the Pieces list.
274 if (!CurStringPiece.empty()) {
275 Pieces.push_back(AsmStringPiece(CurStringPiece));
276 CurStringPiece.clear();
277 }
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattner458cd9c2009-03-10 23:21:44 +0000279 // Handle %x4 and %x[foo] by capturing x as the modifier character.
280 char Modifier = '\0';
281 if (isalpha(EscapedChar)) {
282 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000283 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner458cd9c2009-03-10 23:21:44 +0000286 if (isdigit(EscapedChar)) {
287 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000288 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattnercafc2222009-03-11 22:52:17 +0000290 --CurPtr;
291 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000292 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattner85759272009-03-11 00:23:13 +0000294 unsigned NumOperands =
295 getNumOutputs() + getNumPlusOperands() + getNumInputs();
296 if (N >= NumOperands) {
297 DiagOffs = CurPtr-StrStart-1;
298 return diag::err_asm_invalid_operand_number;
299 }
300
Chris Lattner458cd9c2009-03-10 23:21:44 +0000301 Pieces.push_back(AsmStringPiece(N, Modifier));
302 continue;
303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Chris Lattner458cd9c2009-03-10 23:21:44 +0000305 // Handle %[foo], a symbolic operand reference.
306 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000307 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000309 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000310 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000311 if (NameEnd == 0)
312 return diag::err_asm_unterminated_symbolic_operand_name;
313 if (NameEnd == CurPtr)
314 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattner3182db12009-03-10 23:51:40 +0000316 std::string SymbolicName(CurPtr, NameEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Chris Lattner458cd9c2009-03-10 23:21:44 +0000318 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000319 if (N == -1) {
320 // Verify that an operand with that name exists.
321 DiagOffs = CurPtr-StrStart;
322 return diag::err_asm_unknown_symbolic_operand_name;
323 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000324 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000326 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000327 continue;
328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner2ff0f422009-03-10 23:57:07 +0000330 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000331 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000332 }
333}
334
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000335//===----------------------------------------------------------------------===//
336// Constructors
337//===----------------------------------------------------------------------===//
338
Anders Carlssondfab34a2008-02-05 23:03:50 +0000339AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Mike Stump3b11fd32010-01-04 22:37:17 +0000340 bool msasm, unsigned numoutputs, unsigned numinputs,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000341 std::string *names, StringLiteral **constraints,
342 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
343 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000344 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000345 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000346 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000347 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
348 Names.push_back(names[i]);
349 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000350 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000351 }
Nico Weber608b17f2008-08-05 23:15:29 +0000352
Anders Carlssonb235fc22007-11-22 01:36:19 +0000353 for (unsigned i = 0; i != numclobbers; i++)
354 Clobbers.push_back(clobbers[i]);
355}
356
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000357ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
358 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000359 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000360: Stmt(ObjCForCollectionStmtClass) {
361 SubExprs[ELEM] = Elem;
362 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
363 SubExprs[BODY] = Body;
364 ForLoc = FCL;
365 RParenLoc = RPL;
366}
367
368
Nico Weber608b17f2008-08-05 23:15:29 +0000369ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
370 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000371 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000372 Stmt *atCatchList)
373: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000374 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000375 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000376 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000377 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000378 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000379 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
380
Nico Weber608b17f2008-08-05 23:15:29 +0000381 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000382 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000383
Ted Kremenekff981022008-02-01 21:28:59 +0000384 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000385 }
386 AtCatchLoc = atCatchLoc;
387 RParenLoc = rparenloc;
388}
389
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000390//===----------------------------------------------------------------------===//
391// AST Destruction.
392//===----------------------------------------------------------------------===//
393
394void Stmt::DestroyChildren(ASTContext &C) {
395 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
396 if (Stmt* Child = *I++) Child->Destroy(C);
397}
398
399static void BranchDestroy(ASTContext &C, Stmt *S, Stmt **SubExprs,
400 unsigned NumExprs) {
401 // We do not use child_iterator here because that will include
402 // the expressions referenced by the condition variable.
403 for (Stmt **I = SubExprs, **E = SubExprs + NumExprs; I != E; ++I)
404 if (Stmt *Child = *I) Child->Destroy(C);
405
406 S->~Stmt();
407 C.Deallocate((void *) S);
408}
409
410void Stmt::DoDestroy(ASTContext &C) {
411 DestroyChildren(C);
412 this->~Stmt();
413 C.Deallocate((void *)this);
414}
415
416void CXXCatchStmt::DoDestroy(ASTContext& C) {
417 if (ExceptionDecl)
418 ExceptionDecl->Destroy(C);
419 Stmt::DoDestroy(C);
420}
421
422void DeclStmt::DoDestroy(ASTContext &C) {
423 // Don't use StmtIterator to iterate over the Decls, as that can recurse
424 // into VLA size expressions (which are owned by the VLA). Further, Decls
425 // are owned by the DeclContext, and will be destroyed with them.
426 if (DG.isDeclGroup())
427 DG.getDeclGroup().Destroy(C);
428}
429
430void IfStmt::DoDestroy(ASTContext &C) {
431 BranchDestroy(C, this, SubExprs, END_EXPR);
432}
433
434void ForStmt::DoDestroy(ASTContext &C) {
435 BranchDestroy(C, this, SubExprs, END_EXPR);
436}
437
438void SwitchStmt::DoDestroy(ASTContext &C) {
439 // Destroy the SwitchCase statements in this switch. In the normal
440 // case, this loop will merely decrement the reference counts from
441 // the Retain() calls in addSwitchCase();
442 SwitchCase *SC = FirstCase;
443 while (SC) {
444 SwitchCase *Next = SC->getNextSwitchCase();
445 SC->Destroy(C);
446 SC = Next;
447 }
448
449 BranchDestroy(C, this, SubExprs, END_EXPR);
450}
451
452void WhileStmt::DoDestroy(ASTContext &C) {
453 BranchDestroy(C, this, SubExprs, END_EXPR);
454}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000455
Ted Kremenek82977772007-08-24 21:09:09 +0000456//===----------------------------------------------------------------------===//
457// Child Iterators for iterating over subexpressions/substatements
458//===----------------------------------------------------------------------===//
459
460// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000461Stmt::child_iterator DeclStmt::child_begin() {
462 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000463}
464
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000465Stmt::child_iterator DeclStmt::child_end() {
466 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000467}
468
Ted Kremenek82977772007-08-24 21:09:09 +0000469// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000470Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
471Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000472
473// CompoundStmt
474Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000475Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000476
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000477// CaseStmt
478Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
479Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
480
481// DefaultStmt
482Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
483Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000484
485// LabelStmt
486Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000487Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000488
489// IfStmt
Ted Kremenek35628d12009-12-23 23:38:34 +0000490Stmt::child_iterator IfStmt::child_begin() {
491 return child_iterator(Var, &SubExprs[0]);
492}
493Stmt::child_iterator IfStmt::child_end() {
494 return child_iterator(0, &SubExprs[0]+END_EXPR);
495}
Ted Kremenek82977772007-08-24 21:09:09 +0000496
497// SwitchStmt
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000498Stmt::child_iterator SwitchStmt::child_begin() {
499 return child_iterator(Var, &SubExprs[0]);
500}
501Stmt::child_iterator SwitchStmt::child_end() {
502 return child_iterator(0, &SubExprs[0]+END_EXPR);
503}
Ted Kremenek82977772007-08-24 21:09:09 +0000504
505// WhileStmt
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000506Stmt::child_iterator WhileStmt::child_begin() {
507 return child_iterator(Var, &SubExprs[0]);
508}
509Stmt::child_iterator WhileStmt::child_end() {
510 return child_iterator(0, &SubExprs[0]+END_EXPR);
511}
Ted Kremenek82977772007-08-24 21:09:09 +0000512
513// DoStmt
514Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
515Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
516
517// ForStmt
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000518Stmt::child_iterator ForStmt::child_begin() {
519 return child_iterator(CondVar, &SubExprs[0]);
520}
521Stmt::child_iterator ForStmt::child_end() {
Ted Kremenek62812132009-12-24 01:59:46 +0000522 return child_iterator(0, &SubExprs[0]+END_EXPR);
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000523}
Ted Kremenek82977772007-08-24 21:09:09 +0000524
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000525// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000526Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
527 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000528}
Nico Weber608b17f2008-08-05 23:15:29 +0000529Stmt::child_iterator ObjCForCollectionStmt::child_end() {
530 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000531}
532
Ted Kremenek82977772007-08-24 21:09:09 +0000533// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000534Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
535Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000536
537// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000538Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
539const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000540
Ted Kremenek1060aff2008-06-17 03:11:08 +0000541Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
542Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000543
544// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000545Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
546Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000547
548// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000549Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
550Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000551
552// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000553const Expr* ReturnStmt::getRetValue() const {
554 return cast_or_null<Expr>(RetExpr);
555}
556Expr* ReturnStmt::getRetValue() {
557 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000558}
559
Ted Kremenek1060aff2008-06-17 03:11:08 +0000560Stmt::child_iterator ReturnStmt::child_begin() {
561 return &RetExpr;
562}
563Stmt::child_iterator ReturnStmt::child_end() {
564 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000565}
Ted Kremenek82977772007-08-24 21:09:09 +0000566
Chris Lattnerfe795952007-10-29 04:04:16 +0000567// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000568Stmt::child_iterator AsmStmt::child_begin() {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000569 return Exprs.empty() ? 0 : &Exprs[0];
570}
571Stmt::child_iterator AsmStmt::child_end() {
572 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
573}
Chris Lattnerfe795952007-10-29 04:04:16 +0000574
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000575// ObjCAtCatchStmt
576Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000577Stmt::child_iterator ObjCAtCatchStmt::child_end() {
578 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000579}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000580
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000581// ObjCAtFinallyStmt
582Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
583Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000584
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000585// ObjCAtTryStmt
586Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000587Stmt::child_iterator ObjCAtTryStmt::child_end() {
588 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000589}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000590
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000591// ObjCAtThrowStmt
592Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000593 return &Throw;
594}
595
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000596Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000597 return &Throw+1;
598}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000599
600// ObjCAtSynchronizedStmt
601Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000602 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000603}
604
605Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000606 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000607}
608
Sebastian Redl4b07b292008-12-22 19:15:10 +0000609// CXXCatchStmt
610Stmt::child_iterator CXXCatchStmt::child_begin() {
611 return &HandlerBlock;
612}
613
614Stmt::child_iterator CXXCatchStmt::child_end() {
615 return &HandlerBlock + 1;
616}
617
Mike Stump6515afe2009-11-30 20:10:58 +0000618QualType CXXCatchStmt::getCaughtType() const {
Sebastian Redl4b07b292008-12-22 19:15:10 +0000619 if (ExceptionDecl)
Douglas Gregord308e622009-05-18 20:51:54 +0000620 return ExceptionDecl->getType();
Sebastian Redl4b07b292008-12-22 19:15:10 +0000621 return QualType();
622}
623
Sebastian Redl8351da02008-12-22 21:35:02 +0000624// CXXTryStmt
625Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
626Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
627
628CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
629 Stmt **handlers, unsigned numHandlers)
630 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
631 Stmts.push_back(tryBlock);
632 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
633}