blob: 577b0ac5609969c05e822f67ece795a8e9116f72 [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
Chris Lattnerb3277932009-03-10 04:59:06 +0000150Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000151 return cast<Expr>(Exprs[i + NumOutputs]);
152}
Chris Lattnerb3277932009-03-10 04:59:06 +0000153
154/// getInputConstraint - Return the specified input constraint. Unlike output
155/// constraints, these can be empty.
156std::string AsmStmt::getInputConstraint(unsigned i) const {
157 return std::string(Constraints[i + NumOutputs]->getStrData(),
158 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000159}
160
Chris Lattner10ca96a2009-03-10 06:33:24 +0000161
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000162void AsmStmt::setOutputsAndInputsAndClobbers(const std::string *Names,
163 StringLiteral **Constraints,
164 Stmt **Exprs,
165 unsigned NumOutputs,
166 unsigned NumInputs,
167 StringLiteral **Clobbers,
168 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000169 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);
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000178
179 this->Clobbers.clear();
180 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000181}
182
Chris Lattner10ca96a2009-03-10 06:33:24 +0000183/// getNamedOperand - Given a symbolic operand reference like %[foo],
184/// translate this into a numeric value needed to reference the same operand.
185/// This returns -1 if the operand name is invalid.
186int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
187 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattner10ca96a2009-03-10 06:33:24 +0000189 // Check if this is an output operand.
190 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
191 if (getOutputName(i) == SymbolicName)
192 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Chris Lattner10ca96a2009-03-10 06:33:24 +0000195 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
196 if (getInputName(i) == SymbolicName)
197 return getNumOutputs() + NumPlusOperands + i;
198
199 // Not found.
200 return -1;
201}
202
Chris Lattner458cd9c2009-03-10 23:21:44 +0000203/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
204/// it into pieces. If the asm string is erroneous, emit errors and return
205/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000206unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
207 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000208 const char *StrStart = getAsmString()->getStrData();
209 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000210 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattner458cd9c2009-03-10 23:21:44 +0000212 // "Simple" inline asms have no constraints or operands, just convert the asm
213 // string to escape $'s.
214 if (isSimple()) {
215 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000216 for (; CurPtr != StrEnd; ++CurPtr) {
217 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000218 case '$':
219 Result += "$$";
220 break;
221 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000222 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000223 break;
224 }
225 }
226 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000227 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000228 }
229
230 // CurStringPiece - The current string that we are building up as we scan the
231 // asm string.
232 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattner458cd9c2009-03-10 23:21:44 +0000234 while (1) {
235 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000236 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000237 if (!CurStringPiece.empty())
238 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000239 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000240 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner3182db12009-03-10 23:51:40 +0000242 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000243 if (CurChar == '$') {
244 CurStringPiece += "$$";
245 continue;
246 } else if (CurChar != '%') {
247 CurStringPiece += CurChar;
248 continue;
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Chris Lattner458cd9c2009-03-10 23:21:44 +0000251 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000252 if (CurPtr == StrEnd) {
253 // % at end of string is invalid (no escape).
254 DiagOffs = CurPtr-StrStart-1;
255 return diag::err_asm_invalid_escape;
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner3182db12009-03-10 23:51:40 +0000258 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000259 if (EscapedChar == '%') { // %% -> %
260 // Escaped percentage sign.
261 CurStringPiece += '%';
262 continue;
263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Chris Lattner458cd9c2009-03-10 23:21:44 +0000265 if (EscapedChar == '=') { // %= -> Generate an unique ID.
266 CurStringPiece += "${:uid}";
267 continue;
268 }
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattner458cd9c2009-03-10 23:21:44 +0000270 // Otherwise, we have an operand. If we have accumulated a string so far,
271 // add it to the Pieces list.
272 if (!CurStringPiece.empty()) {
273 Pieces.push_back(AsmStringPiece(CurStringPiece));
274 CurStringPiece.clear();
275 }
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner458cd9c2009-03-10 23:21:44 +0000277 // Handle %x4 and %x[foo] by capturing x as the modifier character.
278 char Modifier = '\0';
279 if (isalpha(EscapedChar)) {
280 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000281 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Chris Lattner458cd9c2009-03-10 23:21:44 +0000284 if (isdigit(EscapedChar)) {
285 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000286 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattnercafc2222009-03-11 22:52:17 +0000288 --CurPtr;
289 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000290 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattner85759272009-03-11 00:23:13 +0000292 unsigned NumOperands =
293 getNumOutputs() + getNumPlusOperands() + getNumInputs();
294 if (N >= NumOperands) {
295 DiagOffs = CurPtr-StrStart-1;
296 return diag::err_asm_invalid_operand_number;
297 }
298
Chris Lattner458cd9c2009-03-10 23:21:44 +0000299 Pieces.push_back(AsmStringPiece(N, Modifier));
300 continue;
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattner458cd9c2009-03-10 23:21:44 +0000303 // Handle %[foo], a symbolic operand reference.
304 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000305 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000307 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000308 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000309 if (NameEnd == 0)
310 return diag::err_asm_unterminated_symbolic_operand_name;
311 if (NameEnd == CurPtr)
312 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Chris Lattner3182db12009-03-10 23:51:40 +0000314 std::string SymbolicName(CurPtr, NameEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattner458cd9c2009-03-10 23:21:44 +0000316 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000317 if (N == -1) {
318 // Verify that an operand with that name exists.
319 DiagOffs = CurPtr-StrStart;
320 return diag::err_asm_unknown_symbolic_operand_name;
321 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000322 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000324 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000325 continue;
326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner2ff0f422009-03-10 23:57:07 +0000328 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000329 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000330 }
331}
332
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000333//===----------------------------------------------------------------------===//
334// Constructors
335//===----------------------------------------------------------------------===//
336
Anders Carlssondfab34a2008-02-05 23:03:50 +0000337AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Mike Stump3b11fd32010-01-04 22:37:17 +0000338 bool msasm, unsigned numoutputs, unsigned numinputs,
Anders Carlsson703e3942010-01-24 05:50:09 +0000339 const std::string *names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000340 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
341 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000342 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000343 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000344 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000345 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
346 Names.push_back(names[i]);
347 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000348 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000349 }
Nico Weber608b17f2008-08-05 23:15:29 +0000350
Anders Carlssonb235fc22007-11-22 01:36:19 +0000351 for (unsigned i = 0; i != numclobbers; i++)
352 Clobbers.push_back(clobbers[i]);
353}
354
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000355ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
356 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000357 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000358: Stmt(ObjCForCollectionStmtClass) {
359 SubExprs[ELEM] = Elem;
360 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
361 SubExprs[BODY] = Body;
362 ForLoc = FCL;
363 RParenLoc = RPL;
364}
365
366
Nico Weber608b17f2008-08-05 23:15:29 +0000367ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
368 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000369 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000370 Stmt *atCatchList)
371: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000372 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000373 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000374 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000375 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000376 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000377 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
378
Nico Weber608b17f2008-08-05 23:15:29 +0000379 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000380 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000381
Ted Kremenekff981022008-02-01 21:28:59 +0000382 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000383 }
384 AtCatchLoc = atCatchLoc;
385 RParenLoc = rparenloc;
386}
387
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000388//===----------------------------------------------------------------------===//
389// AST Destruction.
390//===----------------------------------------------------------------------===//
391
392void Stmt::DestroyChildren(ASTContext &C) {
393 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
394 if (Stmt* Child = *I++) Child->Destroy(C);
395}
396
397static void BranchDestroy(ASTContext &C, Stmt *S, Stmt **SubExprs,
398 unsigned NumExprs) {
399 // We do not use child_iterator here because that will include
400 // the expressions referenced by the condition variable.
401 for (Stmt **I = SubExprs, **E = SubExprs + NumExprs; I != E; ++I)
402 if (Stmt *Child = *I) Child->Destroy(C);
403
404 S->~Stmt();
405 C.Deallocate((void *) S);
406}
407
408void Stmt::DoDestroy(ASTContext &C) {
409 DestroyChildren(C);
410 this->~Stmt();
411 C.Deallocate((void *)this);
412}
413
414void CXXCatchStmt::DoDestroy(ASTContext& C) {
415 if (ExceptionDecl)
416 ExceptionDecl->Destroy(C);
417 Stmt::DoDestroy(C);
418}
419
420void DeclStmt::DoDestroy(ASTContext &C) {
421 // Don't use StmtIterator to iterate over the Decls, as that can recurse
422 // into VLA size expressions (which are owned by the VLA). Further, Decls
423 // are owned by the DeclContext, and will be destroyed with them.
424 if (DG.isDeclGroup())
425 DG.getDeclGroup().Destroy(C);
426}
427
428void IfStmt::DoDestroy(ASTContext &C) {
429 BranchDestroy(C, this, SubExprs, END_EXPR);
430}
431
432void ForStmt::DoDestroy(ASTContext &C) {
433 BranchDestroy(C, this, SubExprs, END_EXPR);
434}
435
436void SwitchStmt::DoDestroy(ASTContext &C) {
437 // Destroy the SwitchCase statements in this switch. In the normal
438 // case, this loop will merely decrement the reference counts from
439 // the Retain() calls in addSwitchCase();
440 SwitchCase *SC = FirstCase;
441 while (SC) {
442 SwitchCase *Next = SC->getNextSwitchCase();
443 SC->Destroy(C);
444 SC = Next;
445 }
446
447 BranchDestroy(C, this, SubExprs, END_EXPR);
448}
449
450void WhileStmt::DoDestroy(ASTContext &C) {
451 BranchDestroy(C, this, SubExprs, END_EXPR);
452}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000453
Ted Kremenek82977772007-08-24 21:09:09 +0000454//===----------------------------------------------------------------------===//
455// Child Iterators for iterating over subexpressions/substatements
456//===----------------------------------------------------------------------===//
457
458// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000459Stmt::child_iterator DeclStmt::child_begin() {
460 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000461}
462
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000463Stmt::child_iterator DeclStmt::child_end() {
464 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000465}
466
Ted Kremenek82977772007-08-24 21:09:09 +0000467// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000468Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
469Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000470
471// CompoundStmt
472Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000473Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000474
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000475// CaseStmt
476Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
477Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
478
479// DefaultStmt
480Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
481Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000482
483// LabelStmt
484Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000485Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000486
487// IfStmt
Ted Kremenek35628d12009-12-23 23:38:34 +0000488Stmt::child_iterator IfStmt::child_begin() {
489 return child_iterator(Var, &SubExprs[0]);
490}
491Stmt::child_iterator IfStmt::child_end() {
492 return child_iterator(0, &SubExprs[0]+END_EXPR);
493}
Ted Kremenek82977772007-08-24 21:09:09 +0000494
495// SwitchStmt
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000496Stmt::child_iterator SwitchStmt::child_begin() {
497 return child_iterator(Var, &SubExprs[0]);
498}
499Stmt::child_iterator SwitchStmt::child_end() {
500 return child_iterator(0, &SubExprs[0]+END_EXPR);
501}
Ted Kremenek82977772007-08-24 21:09:09 +0000502
503// WhileStmt
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000504Stmt::child_iterator WhileStmt::child_begin() {
505 return child_iterator(Var, &SubExprs[0]);
506}
507Stmt::child_iterator WhileStmt::child_end() {
508 return child_iterator(0, &SubExprs[0]+END_EXPR);
509}
Ted Kremenek82977772007-08-24 21:09:09 +0000510
511// DoStmt
512Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
513Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
514
515// ForStmt
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000516Stmt::child_iterator ForStmt::child_begin() {
517 return child_iterator(CondVar, &SubExprs[0]);
518}
519Stmt::child_iterator ForStmt::child_end() {
Ted Kremenek62812132009-12-24 01:59:46 +0000520 return child_iterator(0, &SubExprs[0]+END_EXPR);
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000521}
Ted Kremenek82977772007-08-24 21:09:09 +0000522
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000523// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000524Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
525 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000526}
Nico Weber608b17f2008-08-05 23:15:29 +0000527Stmt::child_iterator ObjCForCollectionStmt::child_end() {
528 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000529}
530
Ted Kremenek82977772007-08-24 21:09:09 +0000531// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000532Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
533Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000534
535// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000536Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
537const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000538
Ted Kremenek1060aff2008-06-17 03:11:08 +0000539Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
540Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000541
542// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000543Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
544Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000545
546// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000547Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
548Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000549
550// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000551const Expr* ReturnStmt::getRetValue() const {
552 return cast_or_null<Expr>(RetExpr);
553}
554Expr* ReturnStmt::getRetValue() {
555 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000556}
557
Ted Kremenek1060aff2008-06-17 03:11:08 +0000558Stmt::child_iterator ReturnStmt::child_begin() {
559 return &RetExpr;
560}
561Stmt::child_iterator ReturnStmt::child_end() {
562 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000563}
Ted Kremenek82977772007-08-24 21:09:09 +0000564
Chris Lattnerfe795952007-10-29 04:04:16 +0000565// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000566Stmt::child_iterator AsmStmt::child_begin() {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000567 return Exprs.empty() ? 0 : &Exprs[0];
568}
569Stmt::child_iterator AsmStmt::child_end() {
570 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
571}
Chris Lattnerfe795952007-10-29 04:04:16 +0000572
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000573// ObjCAtCatchStmt
574Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000575Stmt::child_iterator ObjCAtCatchStmt::child_end() {
576 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000577}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000578
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579// ObjCAtFinallyStmt
580Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
581Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000582
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000583// ObjCAtTryStmt
584Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000585Stmt::child_iterator ObjCAtTryStmt::child_end() {
586 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000587}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000588
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589// ObjCAtThrowStmt
590Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000591 return &Throw;
592}
593
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000595 return &Throw+1;
596}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000597
598// ObjCAtSynchronizedStmt
599Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000600 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000601}
602
603Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000604 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000605}
606
Sebastian Redl4b07b292008-12-22 19:15:10 +0000607// CXXCatchStmt
608Stmt::child_iterator CXXCatchStmt::child_begin() {
609 return &HandlerBlock;
610}
611
612Stmt::child_iterator CXXCatchStmt::child_end() {
613 return &HandlerBlock + 1;
614}
615
Mike Stump6515afe2009-11-30 20:10:58 +0000616QualType CXXCatchStmt::getCaughtType() const {
Sebastian Redl4b07b292008-12-22 19:15:10 +0000617 if (ExceptionDecl)
Douglas Gregord308e622009-05-18 20:51:54 +0000618 return ExceptionDecl->getType();
Sebastian Redl4b07b292008-12-22 19:15:10 +0000619 return QualType();
620}
621
Sebastian Redl8351da02008-12-22 21:35:02 +0000622// CXXTryStmt
623Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
624Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
625
626CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
627 Stmt **handlers, unsigned numHandlers)
628 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
629 Stmts.push_back(tryBlock);
630 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
631}