blob: 28f7371b09db67bad6fd40b4946e629ae6e835ca [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 '+').
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000135llvm::StringRef AsmStmt::getOutputConstraint(unsigned i) const {
136 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000137}
Chris Lattnerb3277932009-03-10 04:59:06 +0000138
Chris Lattner85759272009-03-11 00:23:13 +0000139/// getNumPlusOperands - Return the number of output operands that have a "+"
140/// constraint.
141unsigned AsmStmt::getNumPlusOperands() const {
142 unsigned Res = 0;
143 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
144 if (isOutputPlusConstraint(i))
145 ++Res;
146 return Res;
147}
148
Chris Lattnerb3277932009-03-10 04:59:06 +0000149Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000150 return cast<Expr>(Exprs[i + NumOutputs]);
151}
Chris Lattnerb3277932009-03-10 04:59:06 +0000152
153/// getInputConstraint - Return the specified input constraint. Unlike output
154/// constraints, these can be empty.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000155llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
156 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000157}
158
Chris Lattner10ca96a2009-03-10 06:33:24 +0000159
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000160void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
161 const std::string *Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000162 StringLiteral **Constraints,
163 Stmt **Exprs,
164 unsigned NumOutputs,
165 unsigned NumInputs,
166 StringLiteral **Clobbers,
167 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000168 this->NumOutputs = NumOutputs;
169 this->NumInputs = NumInputs;
170 this->Names.clear();
171 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
172 this->Constraints.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000173 this->Constraints.insert(this->Constraints.end(),
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000174 Constraints, Constraints + NumOutputs + NumInputs);
175 this->Exprs.clear();
176 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000177
178 this->Clobbers.clear();
179 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000180}
181
Chris Lattner10ca96a2009-03-10 06:33:24 +0000182/// getNamedOperand - Given a symbolic operand reference like %[foo],
183/// translate this into a numeric value needed to reference the same operand.
184/// This returns -1 if the operand name is invalid.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000185int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000186 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattner10ca96a2009-03-10 06:33:24 +0000188 // Check if this is an output operand.
189 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
190 if (getOutputName(i) == SymbolicName)
191 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000192 }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Chris Lattner10ca96a2009-03-10 06:33:24 +0000194 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
195 if (getInputName(i) == SymbolicName)
196 return getNumOutputs() + NumPlusOperands + i;
197
198 // Not found.
199 return -1;
200}
201
Chris Lattner458cd9c2009-03-10 23:21:44 +0000202/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
203/// it into pieces. If the asm string is erroneous, emit errors and return
204/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000205unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
206 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000207 const char *StrStart = getAsmString()->getStrData();
208 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000209 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattner458cd9c2009-03-10 23:21:44 +0000211 // "Simple" inline asms have no constraints or operands, just convert the asm
212 // string to escape $'s.
213 if (isSimple()) {
214 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000215 for (; CurPtr != StrEnd; ++CurPtr) {
216 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000217 case '$':
218 Result += "$$";
219 break;
220 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000221 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000222 break;
223 }
224 }
225 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000226 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000227 }
228
229 // CurStringPiece - The current string that we are building up as we scan the
230 // asm string.
231 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattner458cd9c2009-03-10 23:21:44 +0000233 while (1) {
234 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000235 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000236 if (!CurStringPiece.empty())
237 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000238 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000239 }
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattner3182db12009-03-10 23:51:40 +0000241 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000242 if (CurChar == '$') {
243 CurStringPiece += "$$";
244 continue;
245 } else if (CurChar != '%') {
246 CurStringPiece += CurChar;
247 continue;
248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattner458cd9c2009-03-10 23:21:44 +0000250 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000251 if (CurPtr == StrEnd) {
252 // % at end of string is invalid (no escape).
253 DiagOffs = CurPtr-StrStart-1;
254 return diag::err_asm_invalid_escape;
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattner3182db12009-03-10 23:51:40 +0000257 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000258 if (EscapedChar == '%') { // %% -> %
259 // Escaped percentage sign.
260 CurStringPiece += '%';
261 continue;
262 }
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattner458cd9c2009-03-10 23:21:44 +0000264 if (EscapedChar == '=') { // %= -> Generate an unique ID.
265 CurStringPiece += "${:uid}";
266 continue;
267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattner458cd9c2009-03-10 23:21:44 +0000269 // Otherwise, we have an operand. If we have accumulated a string so far,
270 // add it to the Pieces list.
271 if (!CurStringPiece.empty()) {
272 Pieces.push_back(AsmStringPiece(CurStringPiece));
273 CurStringPiece.clear();
274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner458cd9c2009-03-10 23:21:44 +0000276 // Handle %x4 and %x[foo] by capturing x as the modifier character.
277 char Modifier = '\0';
278 if (isalpha(EscapedChar)) {
279 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000280 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000281 }
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattner458cd9c2009-03-10 23:21:44 +0000283 if (isdigit(EscapedChar)) {
284 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000285 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Chris Lattnercafc2222009-03-11 22:52:17 +0000287 --CurPtr;
288 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000289 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattner85759272009-03-11 00:23:13 +0000291 unsigned NumOperands =
292 getNumOutputs() + getNumPlusOperands() + getNumInputs();
293 if (N >= NumOperands) {
294 DiagOffs = CurPtr-StrStart-1;
295 return diag::err_asm_invalid_operand_number;
296 }
297
Chris Lattner458cd9c2009-03-10 23:21:44 +0000298 Pieces.push_back(AsmStringPiece(N, Modifier));
299 continue;
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattner458cd9c2009-03-10 23:21:44 +0000302 // Handle %[foo], a symbolic operand reference.
303 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000304 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000306 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000307 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000308 if (NameEnd == 0)
309 return diag::err_asm_unterminated_symbolic_operand_name;
310 if (NameEnd == CurPtr)
311 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Anders Carlsson95c9ce92010-01-30 20:48:08 +0000313 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattner458cd9c2009-03-10 23:21:44 +0000315 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000316 if (N == -1) {
317 // Verify that an operand with that name exists.
318 DiagOffs = CurPtr-StrStart;
319 return diag::err_asm_unknown_symbolic_operand_name;
320 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000321 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000323 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000324 continue;
325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Chris Lattner2ff0f422009-03-10 23:57:07 +0000327 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000328 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000329 }
330}
331
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000332//===----------------------------------------------------------------------===//
333// Constructors
334//===----------------------------------------------------------------------===//
335
Anders Carlssondfab34a2008-02-05 23:03:50 +0000336AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Mike Stump3b11fd32010-01-04 22:37:17 +0000337 bool msasm, unsigned numoutputs, unsigned numinputs,
Anders Carlsson703e3942010-01-24 05:50:09 +0000338 const std::string *names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000339 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
340 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000341 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000342 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000343 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000344 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
345 Names.push_back(names[i]);
346 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000347 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000348 }
Nico Weber608b17f2008-08-05 23:15:29 +0000349
Anders Carlssonb235fc22007-11-22 01:36:19 +0000350 for (unsigned i = 0; i != numclobbers; i++)
351 Clobbers.push_back(clobbers[i]);
352}
353
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000354ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
355 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000356 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000357: Stmt(ObjCForCollectionStmtClass) {
358 SubExprs[ELEM] = Elem;
359 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
360 SubExprs[BODY] = Body;
361 ForLoc = FCL;
362 RParenLoc = RPL;
363}
364
365
Nico Weber608b17f2008-08-05 23:15:29 +0000366ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
367 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000368 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000369 Stmt *atCatchList)
370: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000371 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000372 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000373 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000374 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000375 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000376 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
377
Nico Weber608b17f2008-08-05 23:15:29 +0000378 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000379 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000380
Ted Kremenekff981022008-02-01 21:28:59 +0000381 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000382 }
383 AtCatchLoc = atCatchLoc;
384 RParenLoc = rparenloc;
385}
386
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000387//===----------------------------------------------------------------------===//
388// AST Destruction.
389//===----------------------------------------------------------------------===//
390
391void Stmt::DestroyChildren(ASTContext &C) {
392 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
393 if (Stmt* Child = *I++) Child->Destroy(C);
394}
395
396static void BranchDestroy(ASTContext &C, Stmt *S, Stmt **SubExprs,
397 unsigned NumExprs) {
398 // We do not use child_iterator here because that will include
399 // the expressions referenced by the condition variable.
400 for (Stmt **I = SubExprs, **E = SubExprs + NumExprs; I != E; ++I)
401 if (Stmt *Child = *I) Child->Destroy(C);
402
403 S->~Stmt();
404 C.Deallocate((void *) S);
405}
406
407void Stmt::DoDestroy(ASTContext &C) {
408 DestroyChildren(C);
409 this->~Stmt();
410 C.Deallocate((void *)this);
411}
412
413void CXXCatchStmt::DoDestroy(ASTContext& C) {
414 if (ExceptionDecl)
415 ExceptionDecl->Destroy(C);
416 Stmt::DoDestroy(C);
417}
418
419void DeclStmt::DoDestroy(ASTContext &C) {
420 // Don't use StmtIterator to iterate over the Decls, as that can recurse
421 // into VLA size expressions (which are owned by the VLA). Further, Decls
422 // are owned by the DeclContext, and will be destroyed with them.
423 if (DG.isDeclGroup())
424 DG.getDeclGroup().Destroy(C);
425}
426
427void IfStmt::DoDestroy(ASTContext &C) {
428 BranchDestroy(C, this, SubExprs, END_EXPR);
429}
430
431void ForStmt::DoDestroy(ASTContext &C) {
432 BranchDestroy(C, this, SubExprs, END_EXPR);
433}
434
435void SwitchStmt::DoDestroy(ASTContext &C) {
436 // Destroy the SwitchCase statements in this switch. In the normal
437 // case, this loop will merely decrement the reference counts from
438 // the Retain() calls in addSwitchCase();
439 SwitchCase *SC = FirstCase;
440 while (SC) {
441 SwitchCase *Next = SC->getNextSwitchCase();
442 SC->Destroy(C);
443 SC = Next;
444 }
445
446 BranchDestroy(C, this, SubExprs, END_EXPR);
447}
448
449void WhileStmt::DoDestroy(ASTContext &C) {
450 BranchDestroy(C, this, SubExprs, END_EXPR);
451}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000452
Ted Kremenek82977772007-08-24 21:09:09 +0000453//===----------------------------------------------------------------------===//
454// Child Iterators for iterating over subexpressions/substatements
455//===----------------------------------------------------------------------===//
456
457// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000458Stmt::child_iterator DeclStmt::child_begin() {
459 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000460}
461
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000462Stmt::child_iterator DeclStmt::child_end() {
463 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000464}
465
Ted Kremenek82977772007-08-24 21:09:09 +0000466// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000467Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
468Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000469
470// CompoundStmt
471Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000472Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000473
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000474// CaseStmt
475Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
476Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
477
478// DefaultStmt
479Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
480Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000481
482// LabelStmt
483Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000484Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000485
486// IfStmt
Ted Kremenek35628d12009-12-23 23:38:34 +0000487Stmt::child_iterator IfStmt::child_begin() {
488 return child_iterator(Var, &SubExprs[0]);
489}
490Stmt::child_iterator IfStmt::child_end() {
491 return child_iterator(0, &SubExprs[0]+END_EXPR);
492}
Ted Kremenek82977772007-08-24 21:09:09 +0000493
494// SwitchStmt
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000495Stmt::child_iterator SwitchStmt::child_begin() {
496 return child_iterator(Var, &SubExprs[0]);
497}
498Stmt::child_iterator SwitchStmt::child_end() {
499 return child_iterator(0, &SubExprs[0]+END_EXPR);
500}
Ted Kremenek82977772007-08-24 21:09:09 +0000501
502// WhileStmt
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000503Stmt::child_iterator WhileStmt::child_begin() {
504 return child_iterator(Var, &SubExprs[0]);
505}
506Stmt::child_iterator WhileStmt::child_end() {
507 return child_iterator(0, &SubExprs[0]+END_EXPR);
508}
Ted Kremenek82977772007-08-24 21:09:09 +0000509
510// DoStmt
511Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
512Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
513
514// ForStmt
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000515Stmt::child_iterator ForStmt::child_begin() {
516 return child_iterator(CondVar, &SubExprs[0]);
517}
518Stmt::child_iterator ForStmt::child_end() {
Ted Kremenek62812132009-12-24 01:59:46 +0000519 return child_iterator(0, &SubExprs[0]+END_EXPR);
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000520}
Ted Kremenek82977772007-08-24 21:09:09 +0000521
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000523Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
524 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000525}
Nico Weber608b17f2008-08-05 23:15:29 +0000526Stmt::child_iterator ObjCForCollectionStmt::child_end() {
527 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000528}
529
Ted Kremenek82977772007-08-24 21:09:09 +0000530// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000531Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
532Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000533
534// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000535Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
536const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000537
Ted Kremenek1060aff2008-06-17 03:11:08 +0000538Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
539Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000540
541// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000542Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
543Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000544
545// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000546Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
547Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000548
549// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000550const Expr* ReturnStmt::getRetValue() const {
551 return cast_or_null<Expr>(RetExpr);
552}
553Expr* ReturnStmt::getRetValue() {
554 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000555}
556
Ted Kremenek1060aff2008-06-17 03:11:08 +0000557Stmt::child_iterator ReturnStmt::child_begin() {
558 return &RetExpr;
559}
560Stmt::child_iterator ReturnStmt::child_end() {
561 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000562}
Ted Kremenek82977772007-08-24 21:09:09 +0000563
Chris Lattnerfe795952007-10-29 04:04:16 +0000564// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000565Stmt::child_iterator AsmStmt::child_begin() {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000566 return Exprs.empty() ? 0 : &Exprs[0];
567}
568Stmt::child_iterator AsmStmt::child_end() {
569 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
570}
Chris Lattnerfe795952007-10-29 04:04:16 +0000571
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000572// ObjCAtCatchStmt
573Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000574Stmt::child_iterator ObjCAtCatchStmt::child_end() {
575 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000576}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000577
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000578// ObjCAtFinallyStmt
579Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
580Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000581
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582// ObjCAtTryStmt
583Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000584Stmt::child_iterator ObjCAtTryStmt::child_end() {
585 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000586}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000587
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000588// ObjCAtThrowStmt
589Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000590 return &Throw;
591}
592
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000593Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000594 return &Throw+1;
595}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000596
597// ObjCAtSynchronizedStmt
598Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000599 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000600}
601
602Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000603 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000604}
605
Sebastian Redl4b07b292008-12-22 19:15:10 +0000606// CXXCatchStmt
607Stmt::child_iterator CXXCatchStmt::child_begin() {
608 return &HandlerBlock;
609}
610
611Stmt::child_iterator CXXCatchStmt::child_end() {
612 return &HandlerBlock + 1;
613}
614
Mike Stump6515afe2009-11-30 20:10:58 +0000615QualType CXXCatchStmt::getCaughtType() const {
Sebastian Redl4b07b292008-12-22 19:15:10 +0000616 if (ExceptionDecl)
Douglas Gregord308e622009-05-18 20:51:54 +0000617 return ExceptionDecl->getType();
Sebastian Redl4b07b292008-12-22 19:15:10 +0000618 return QualType();
619}
620
Sebastian Redl8351da02008-12-22 21:35:02 +0000621// CXXTryStmt
622Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
623Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
624
625CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
626 Stmt **handlers, unsigned numHandlers)
627 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
628 Stmts.push_back(tryBlock);
629 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
630}