blob: c2c2f0a40961464642af8250f10f3a864b87f1b5 [file] [log] [blame]
Chris Lattnerf42cce72006-10-25 04:09:21 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf42cce72006-10-25 04:09:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Stmt.h"
Chris Lattner29375652006-12-04 18:06:35 +000015#include "clang/AST/ExprCXX.h"
Steve Naroff021ca182008-05-29 21:12:08 +000016#include "clang/AST/ExprObjC.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000017#include "clang/AST/StmtCXX.h"
18#include "clang/AST/StmtObjC.h"
Sebastian Redl54c04d42008-12-22 19:15:10 +000019#include "clang/AST/Type.h"
Ted Kremenekfe7a9602009-02-06 01:42:09 +000020#include "clang/AST/ASTContext.h"
Chris Lattnera41b8472009-03-10 23:51:40 +000021#include "clang/AST/ASTDiagnostic.h"
Torok Edwindb714922009-08-24 13:25:12 +000022#include <cstdio>
Chris Lattnerf42cce72006-10-25 04:09:21 +000023using namespace clang;
24
Steve Narofff84d11f2007-05-23 21:48:04 +000025static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000026 const char *Name;
27 unsigned Counter;
28 unsigned Size;
Chris Lattnerd8c9fc52007-08-25 01:55:00 +000029} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner4d15a0d2007-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 Gregorbe35ce92008-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);
Steve Narofff1e53692007-03-23 22:27:02 +000041#include "clang/AST/StmtNodes.def"
Nico Weberde565e32008-08-05 23:15:29 +000042
Chris Lattner4d15a0d2007-08-25 01:42:24 +000043 return StmtClassInfo[E];
44}
45
Steve Narofff1e53692007-03-23 22:27:02 +000046const char *Stmt::getStmtClassName() const {
Douglas Gregor2c742022009-08-08 01:41:12 +000047 return getStmtInfoTableEntry((StmtClass)sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000048}
Steve Narofff84d11f2007-05-23 21:48:04 +000049
50void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000051 // Ensure the table is primed.
52 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000053
Steve Narofff84d11f2007-05-23 21:48:04 +000054 unsigned sum = 0;
55 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattnerd8c9fc52007-08-25 01:55:00 +000056 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000057 if (StmtClassInfo[i].Name == 0) continue;
58 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000059 }
60 fprintf(stderr, " %d stmts/exprs total.\n", sum);
61 sum = 0;
Chris Lattnerd8c9fc52007-08-25 01:55:00 +000062 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000063 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +000064 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weberde565e32008-08-05 23:15:29 +000065 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner4d15a0d2007-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;
Steve Narofff84d11f2007-05-23 21:48:04 +000070 }
71 fprintf(stderr, "Total bytes = %d\n", sum);
72}
73
74void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000075 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000076}
77
78static bool StatSwitch = false;
79
Kovarththanan Rajaratnam130f7f92009-11-29 14:54:35 +000080bool Stmt::CollectingStats(bool Enable) {
81 if (Enable) StatSwitch = true;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000082 return StatSwitch;
Steve Narofff84d11f2007-05-23 21:48:04 +000083}
84
Douglas Gregora9af1d12009-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}
Steve Narofff84d11f2007-05-23 21:48:04 +000093
Chris Lattnereefa10e2007-05-28 06:56:27 +000094const char *LabelStmt::getName() const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +000095 return getID()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +000096}
97
Steve Naroffdc9f36e2007-08-31 23:49:30 +000098// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weberde565e32008-08-05 23:15:29 +000099SourceRange ReturnStmt::getSourceRange() const {
Steve Naroffdc9f36e2007-08-31 23:49:30 +0000100 if (RetExpr)
101 return SourceRange(RetLoc, RetExpr->getLocEnd());
102 else
103 return SourceRange(RetLoc);
104}
105
Ted Kremenek7f74e132007-10-01 16:34:52 +0000106bool Stmt::hasImplicitControlFlow() const {
107 switch (sClass) {
108 default:
109 return false;
Nico Weberde565e32008-08-05 23:15:29 +0000110
Ted Kremenek7f74e132007-10-01 16:34:52 +0000111 case CallExprClass:
112 case ConditionalOperatorClass:
113 case ChooseExprClass:
114 case StmtExprClass:
115 case DeclStmtClass:
Nico Weberde565e32008-08-05 23:15:29 +0000116 return true;
117
Ted Kremenek7f74e132007-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 Lattner72bbf172009-03-10 04:59:06 +0000128Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000129 return cast<Expr>(Exprs[i]);
130}
Chris Lattner72bbf172009-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 Carlsson66de0812010-01-30 20:38:10 +0000135llvm::StringRef AsmStmt::getOutputConstraint(unsigned i) const {
136 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000137}
Chris Lattner72bbf172009-03-10 04:59:06 +0000138
Chris Lattner14311922009-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 Lattner72bbf172009-03-10 04:59:06 +0000149Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000150 return cast<Expr>(Exprs[i + NumOutputs]);
151}
Chris Lattner72bbf172009-03-10 04:59:06 +0000152
153/// getInputConstraint - Return the specified input constraint. Unlike output
154/// constraints, these can be empty.
Anders Carlsson66de0812010-01-30 20:38:10 +0000155llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
156 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000157}
158
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000159
Anders Carlsson66de0812010-01-30 20:38:10 +0000160void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlsson9a020f92010-01-30 22:25:16 +0000161 IdentifierInfo **Names,
Anders Carlsson96fe0b52010-01-30 19:34:25 +0000162 StringLiteral **Constraints,
163 Stmt **Exprs,
164 unsigned NumOutputs,
165 unsigned NumInputs,
166 StringLiteral **Clobbers,
167 unsigned NumClobbers) {
Douglas Gregorf994f062009-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 Stump11289f42009-09-09 15:08:12 +0000173 this->Constraints.insert(this->Constraints.end(),
Douglas Gregorf994f062009-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 Carlsson96fe0b52010-01-30 19:34:25 +0000177
178 this->Clobbers.clear();
179 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000180}
181
Chris Lattnerd7d5fdf2009-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 Carlsson66de0812010-01-30 20:38:10 +0000185int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000186 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattnerd7d5fdf2009-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 Lattnerd7d5fdf2009-03-10 06:33:24 +0000192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Chris Lattnerd7d5fdf2009-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 Lattner35b58362009-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 Lattnerd8c7ba22009-03-10 23:41:04 +0000205unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
206 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner35b58362009-03-10 23:21:44 +0000207 const char *StrStart = getAsmString()->getStrData();
208 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattnera41b8472009-03-10 23:51:40 +0000209 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000210
Chris Lattner35b58362009-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 Lattnera41b8472009-03-10 23:51:40 +0000215 for (; CurPtr != StrEnd; ++CurPtr) {
216 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000217 case '$':
218 Result += "$$";
219 break;
220 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000221 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000222 break;
223 }
224 }
225 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000226 return 0;
Chris Lattner35b58362009-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 Stump11289f42009-09-09 15:08:12 +0000232
Chris Lattner35b58362009-03-10 23:21:44 +0000233 while (1) {
234 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000235 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000236 if (!CurStringPiece.empty())
237 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000238 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000239 }
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattnera41b8472009-03-10 23:51:40 +0000241 char CurChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000242 if (CurChar == '$') {
243 CurStringPiece += "$$";
244 continue;
245 } else if (CurChar != '%') {
246 CurStringPiece += CurChar;
247 continue;
248 }
Mike Stump11289f42009-09-09 15:08:12 +0000249
Chris Lattner35b58362009-03-10 23:21:44 +0000250 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-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 Stump11289f42009-09-09 15:08:12 +0000256
Chris Lattnera41b8472009-03-10 23:51:40 +0000257 char EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000258 if (EscapedChar == '%') { // %% -> %
259 // Escaped percentage sign.
260 CurStringPiece += '%';
261 continue;
262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
Chris Lattner35b58362009-03-10 23:21:44 +0000264 if (EscapedChar == '=') { // %= -> Generate an unique ID.
265 CurStringPiece += "${:uid}";
266 continue;
267 }
Mike Stump11289f42009-09-09 15:08:12 +0000268
Chris Lattner35b58362009-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 Stump11289f42009-09-09 15:08:12 +0000275
Chris Lattner35b58362009-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 Lattnera41b8472009-03-10 23:51:40 +0000280 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000281 }
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner35b58362009-03-10 23:21:44 +0000283 if (isdigit(EscapedChar)) {
284 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000285 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000286
Chris Lattner99d892b2009-03-11 22:52:17 +0000287 --CurPtr;
288 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000289 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000290
Chris Lattner14311922009-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 Lattner35b58362009-03-10 23:21:44 +0000298 Pieces.push_back(AsmStringPiece(N, Modifier));
299 continue;
300 }
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattner35b58362009-03-10 23:21:44 +0000302 // Handle %[foo], a symbolic operand reference.
303 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000304 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000305
Chris Lattner3fa25c62009-03-11 00:06:36 +0000306 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000307 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattner3fa25c62009-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 Stump11289f42009-09-09 15:08:12 +0000312
Anders Carlsson0c5d7442010-01-30 20:48:08 +0000313 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000314
Chris Lattner35b58362009-03-10 23:21:44 +0000315 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-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 Lattner35b58362009-03-10 23:21:44 +0000321 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump11289f42009-09-09 15:08:12 +0000322
Chris Lattner3fa25c62009-03-11 00:06:36 +0000323 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000324 continue;
325 }
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000327 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000328 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000329 }
330}
331
Chris Lattner86f5e132008-01-30 05:01:46 +0000332//===----------------------------------------------------------------------===//
333// Constructors
334//===----------------------------------------------------------------------===//
335
Anders Carlsson19fe1162008-02-05 23:03:50 +0000336AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Mike Stump90be58a2010-01-04 22:37:17 +0000337 bool msasm, unsigned numoutputs, unsigned numinputs,
Anders Carlsson9a020f92010-01-30 22:25:16 +0000338 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattner86f5e132008-01-30 05:01:46 +0000339 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
340 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000341 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump90be58a2010-01-04 22:37:17 +0000342 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson19fe1162008-02-05 23:03:50 +0000343 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlsson94ea8aa2007-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 Weberde565e32008-08-05 23:15:29 +0000347 Constraints.push_back(constraints[i]);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000348 }
Nico Weberde565e32008-08-05 23:15:29 +0000349
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000350 for (unsigned i = 0; i != numclobbers; i++)
351 Clobbers.push_back(clobbers[i]);
352}
353
Chris Lattner86f5e132008-01-30 05:01:46 +0000354ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
355 Stmt *Body, SourceLocation FCL,
Nico Weberde565e32008-08-05 23:15:29 +0000356 SourceLocation RPL)
Chris Lattner86f5e132008-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 Weberde565e32008-08-05 23:15:29 +0000366ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
367 SourceLocation rparenloc,
Steve Naroff371b8fb2009-03-03 19:52:17 +0000368 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattner86f5e132008-01-30 05:01:46 +0000369 Stmt *atCatchList)
370: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff371b8fb2009-03-03 19:52:17 +0000371 ExceptionDecl = catchVarDecl;
Chris Lattner86f5e132008-01-30 05:01:46 +0000372 SubExprs[BODY] = atCatchStmt;
Eli Friedman1f97e572008-05-25 04:34:57 +0000373 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar947bca22009-03-01 04:28:32 +0000374 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman1f97e572008-05-25 04:34:57 +0000375 if (atCatchList) {
Ted Kremeneka4965842008-02-01 21:28:59 +0000376 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
377
Nico Weberde565e32008-08-05 23:15:29 +0000378 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremeneka4965842008-02-01 21:28:59 +0000379 AtCatchList = NextCatch;
Nico Weberde565e32008-08-05 23:15:29 +0000380
Ted Kremeneka4965842008-02-01 21:28:59 +0000381 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner86f5e132008-01-30 05:01:46 +0000382 }
383 AtCatchLoc = atCatchLoc;
384 RParenLoc = rparenloc;
385}
386
Ted Kremenek1c3ab072009-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 Lattner86f5e132008-01-30 05:01:46 +0000452
Ted Kremenek066dd932007-08-24 21:09:09 +0000453//===----------------------------------------------------------------------===//
454// Child Iterators for iterating over subexpressions/substatements
455//===----------------------------------------------------------------------===//
456
457// DeclStmt
Ted Kremenek9bb286f2008-10-07 23:09:49 +0000458Stmt::child_iterator DeclStmt::child_begin() {
459 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000460}
461
Ted Kremenek9bb286f2008-10-07 23:09:49 +0000462Stmt::child_iterator DeclStmt::child_end() {
463 return StmtIterator(DG.end(), DG.end());
Ted Kremenekacf920d2008-10-06 20:54:44 +0000464}
465
Ted Kremenek066dd932007-08-24 21:09:09 +0000466// NullStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000467Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
468Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000469
470// CompoundStmt
471Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek5a201952009-02-07 01:47:29 +0000472Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000473
Ted Kremenek14f0d1a2007-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 Kremenek066dd932007-08-24 21:09:09 +0000481
482// LabelStmt
483Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000484Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000485
486// IfStmt
Ted Kremenekb27a6d22009-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 Kremenek066dd932007-08-24 21:09:09 +0000493
494// SwitchStmt
Ted Kremenekee7553d2009-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 Kremenek066dd932007-08-24 21:09:09 +0000501
502// WhileStmt
Ted Kremenekb04c5cb2009-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 Kremenek066dd932007-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 Kremenek1c3ab072009-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 Kremenek17113252009-12-24 01:59:46 +0000519 return child_iterator(0, &SubExprs[0]+END_EXPR);
Ted Kremenek1c3ab072009-12-24 01:48:39 +0000520}
Ted Kremenek066dd932007-08-24 21:09:09 +0000521
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000522// ObjCForCollectionStmt
Nico Weberde565e32008-08-05 23:15:29 +0000523Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
524 return &SubExprs[0];
Fariborz Jahanian83615522008-01-02 22:54:34 +0000525}
Nico Weberde565e32008-08-05 23:15:29 +0000526Stmt::child_iterator ObjCForCollectionStmt::child_end() {
527 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian83615522008-01-02 22:54:34 +0000528}
529
Ted Kremenek066dd932007-08-24 21:09:09 +0000530// GotoStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000531Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
532Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000533
534// IndirectGotoStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000535Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
536const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000537
Ted Kremenekc6501db2008-06-17 03:11:08 +0000538Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
539Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000540
541// ContinueStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000542Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
543Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000544
545// BreakStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000546Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
547Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000548
549// ReturnStmt
Ted Kremenekc6501db2008-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 Kremenek066dd932007-08-24 21:09:09 +0000555}
556
Ted Kremenekc6501db2008-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 Kremenek5b3ed282007-08-27 20:58:16 +0000562}
Ted Kremenek066dd932007-08-24 21:09:09 +0000563
Chris Lattner73c56c02007-10-29 04:04:16 +0000564// AsmStmt
Mike Stump11289f42009-09-09 15:08:12 +0000565Stmt::child_iterator AsmStmt::child_begin() {
Ted Kremenek5778acf2008-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 Lattner73c56c02007-10-29 04:04:16 +0000571
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000572// ObjCAtCatchStmt
573Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weberde565e32008-08-05 23:15:29 +0000574Stmt::child_iterator ObjCAtCatchStmt::child_end() {
575 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +0000576}
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000577
Ted Kremenek1b0ea822008-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 Jahanian65590b22007-11-01 21:12:44 +0000581
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000582// ObjCAtTryStmt
583Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weberde565e32008-08-05 23:15:29 +0000584Stmt::child_iterator ObjCAtTryStmt::child_end() {
585 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +0000586}
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000587
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000588// ObjCAtThrowStmt
589Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000590 return &Throw;
591}
592
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000593Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000594 return &Throw+1;
595}
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000596
597// ObjCAtSynchronizedStmt
598Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian284011b2008-01-29 22:59:37 +0000599 return &SubStmts[0];
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000600}
601
602Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian284011b2008-01-29 22:59:37 +0000603 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000604}
605
Sebastian Redl54c04d42008-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 Stump21d68e22009-11-30 20:10:58 +0000615QualType CXXCatchStmt::getCaughtType() const {
Sebastian Redl54c04d42008-12-22 19:15:10 +0000616 if (ExceptionDecl)
Douglas Gregor5e16fbe2009-05-18 20:51:54 +0000617 return ExceptionDecl->getType();
Sebastian Redl54c04d42008-12-22 19:15:10 +0000618 return QualType();
619}
620
Sebastian Redl9b244a82008-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}