blob: a0830997dcdd577464e7bc829c413279f83e3c1f [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;
John McCall2adddca2010-02-03 00:55:45 +000038#define ABSTRACT_EXPR(CLASS, PARENT)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000039#define STMT(CLASS, PARENT) \
40 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
41 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Steve Narofff1e53692007-03-23 22:27:02 +000042#include "clang/AST/StmtNodes.def"
Nico Weberde565e32008-08-05 23:15:29 +000043
Chris Lattner4d15a0d2007-08-25 01:42:24 +000044 return StmtClassInfo[E];
45}
46
Steve Narofff1e53692007-03-23 22:27:02 +000047const char *Stmt::getStmtClassName() const {
Douglas Gregor2c742022009-08-08 01:41:12 +000048 return getStmtInfoTableEntry((StmtClass)sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000049}
Steve Narofff84d11f2007-05-23 21:48:04 +000050
51void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000052 // Ensure the table is primed.
53 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000054
Steve Narofff84d11f2007-05-23 21:48:04 +000055 unsigned sum = 0;
56 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattnerd8c9fc52007-08-25 01:55:00 +000057 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000058 if (StmtClassInfo[i].Name == 0) continue;
59 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000060 }
61 fprintf(stderr, " %d stmts/exprs total.\n", sum);
62 sum = 0;
Chris Lattnerd8c9fc52007-08-25 01:55:00 +000063 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000064 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +000065 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weberde565e32008-08-05 23:15:29 +000066 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner4d15a0d2007-08-25 01:42:24 +000067 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
68 StmtClassInfo[i].Size,
69 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
70 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +000071 }
72 fprintf(stderr, "Total bytes = %d\n", sum);
73}
74
75void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000076 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000077}
78
79static bool StatSwitch = false;
80
Kovarththanan Rajaratnam130f7f92009-11-29 14:54:35 +000081bool Stmt::CollectingStats(bool Enable) {
82 if (Enable) StatSwitch = true;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000083 return StatSwitch;
Steve Narofff84d11f2007-05-23 21:48:04 +000084}
85
Douglas Gregora9af1d12009-04-17 00:04:06 +000086void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
87 if (this->Body)
88 C.Deallocate(Body);
89 this->NumStmts = NumStmts;
90
91 Body = new (C) Stmt*[NumStmts];
92 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
93}
Steve Narofff84d11f2007-05-23 21:48:04 +000094
Chris Lattnereefa10e2007-05-28 06:56:27 +000095const char *LabelStmt::getName() const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +000096 return getID()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +000097}
98
Steve Naroffdc9f36e2007-08-31 23:49:30 +000099// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weberde565e32008-08-05 23:15:29 +0000100SourceRange ReturnStmt::getSourceRange() const {
Steve Naroffdc9f36e2007-08-31 23:49:30 +0000101 if (RetExpr)
102 return SourceRange(RetLoc, RetExpr->getLocEnd());
103 else
104 return SourceRange(RetLoc);
105}
106
Ted Kremenek7f74e132007-10-01 16:34:52 +0000107bool Stmt::hasImplicitControlFlow() const {
108 switch (sClass) {
109 default:
110 return false;
Nico Weberde565e32008-08-05 23:15:29 +0000111
Ted Kremenek7f74e132007-10-01 16:34:52 +0000112 case CallExprClass:
113 case ConditionalOperatorClass:
114 case ChooseExprClass:
115 case StmtExprClass:
116 case DeclStmtClass:
Nico Weberde565e32008-08-05 23:15:29 +0000117 return true;
118
Ted Kremenek7f74e132007-10-01 16:34:52 +0000119 case Stmt::BinaryOperatorClass: {
120 const BinaryOperator* B = cast<BinaryOperator>(this);
121 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
122 return true;
123 else
124 return false;
125 }
126 }
127}
128
Chris Lattner72bbf172009-03-10 04:59:06 +0000129Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000130 return cast<Expr>(Exprs[i]);
131}
Chris Lattner72bbf172009-03-10 04:59:06 +0000132
133/// getOutputConstraint - Return the constraint string for the specified
134/// output operand. All output constraints are known to be non-empty (either
135/// '=' or '+').
Anders Carlsson66de0812010-01-30 20:38:10 +0000136llvm::StringRef AsmStmt::getOutputConstraint(unsigned i) const {
137 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000138}
Chris Lattner72bbf172009-03-10 04:59:06 +0000139
Chris Lattner14311922009-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 Lattner72bbf172009-03-10 04:59:06 +0000150Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000151 return cast<Expr>(Exprs[i + NumOutputs]);
152}
Chris Lattner72bbf172009-03-10 04:59:06 +0000153
154/// getInputConstraint - Return the specified input constraint. Unlike output
155/// constraints, these can be empty.
Anders Carlsson66de0812010-01-30 20:38:10 +0000156llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
157 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000158}
159
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000160
Anders Carlsson66de0812010-01-30 20:38:10 +0000161void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlsson9a020f92010-01-30 22:25:16 +0000162 IdentifierInfo **Names,
Anders Carlsson96fe0b52010-01-30 19:34:25 +0000163 StringLiteral **Constraints,
164 Stmt **Exprs,
165 unsigned NumOutputs,
166 unsigned NumInputs,
167 StringLiteral **Clobbers,
168 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000169 this->NumOutputs = NumOutputs;
170 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000171 this->NumClobbers = NumClobbers;
172
173 unsigned NumExprs = NumOutputs + NumInputs;
Anders Carlsson96fe0b52010-01-30 19:34:25 +0000174
Anders Carlsson98323d22010-01-30 23:19:41 +0000175 C.Deallocate(this->Names);
176 this->Names = new (C) IdentifierInfo*[NumExprs];
177 std::copy(Names, Names + NumExprs, this->Names);
178
179 C.Deallocate(this->Exprs);
180 this->Exprs = new (C) Stmt*[NumExprs];
181 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
182
183 C.Deallocate(this->Constraints);
184 this->Constraints = new (C) StringLiteral*[NumExprs];
185 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
186
187 C.Deallocate(this->Clobbers);
188 this->Clobbers = new (C) StringLiteral*[NumClobbers];
189 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000190}
191
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000192/// getNamedOperand - Given a symbolic operand reference like %[foo],
193/// translate this into a numeric value needed to reference the same operand.
194/// This returns -1 if the operand name is invalid.
Anders Carlsson66de0812010-01-30 20:38:10 +0000195int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000196 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000198 // Check if this is an output operand.
199 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
200 if (getOutputName(i) == SymbolicName)
201 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000202 }
Mike Stump11289f42009-09-09 15:08:12 +0000203
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000204 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
205 if (getInputName(i) == SymbolicName)
206 return getNumOutputs() + NumPlusOperands + i;
207
208 // Not found.
209 return -1;
210}
211
Chris Lattner35b58362009-03-10 23:21:44 +0000212/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
213/// it into pieces. If the asm string is erroneous, emit errors and return
214/// true, otherwise return false.
Chris Lattnerd8c7ba22009-03-10 23:41:04 +0000215unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
216 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner35b58362009-03-10 23:21:44 +0000217 const char *StrStart = getAsmString()->getStrData();
218 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattnera41b8472009-03-10 23:51:40 +0000219 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000220
Chris Lattner35b58362009-03-10 23:21:44 +0000221 // "Simple" inline asms have no constraints or operands, just convert the asm
222 // string to escape $'s.
223 if (isSimple()) {
224 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000225 for (; CurPtr != StrEnd; ++CurPtr) {
226 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000227 case '$':
228 Result += "$$";
229 break;
230 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000231 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000232 break;
233 }
234 }
235 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000236 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000237 }
238
239 // CurStringPiece - The current string that we are building up as we scan the
240 // asm string.
241 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000242
Chris Lattner35b58362009-03-10 23:21:44 +0000243 while (1) {
244 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000245 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000246 if (!CurStringPiece.empty())
247 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000248 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000249 }
Mike Stump11289f42009-09-09 15:08:12 +0000250
Chris Lattnera41b8472009-03-10 23:51:40 +0000251 char CurChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000252 if (CurChar == '$') {
253 CurStringPiece += "$$";
254 continue;
255 } else if (CurChar != '%') {
256 CurStringPiece += CurChar;
257 continue;
258 }
Mike Stump11289f42009-09-09 15:08:12 +0000259
Chris Lattner35b58362009-03-10 23:21:44 +0000260 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000261 if (CurPtr == StrEnd) {
262 // % at end of string is invalid (no escape).
263 DiagOffs = CurPtr-StrStart-1;
264 return diag::err_asm_invalid_escape;
265 }
Mike Stump11289f42009-09-09 15:08:12 +0000266
Chris Lattnera41b8472009-03-10 23:51:40 +0000267 char EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000268 if (EscapedChar == '%') { // %% -> %
269 // Escaped percentage sign.
270 CurStringPiece += '%';
271 continue;
272 }
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattner35b58362009-03-10 23:21:44 +0000274 if (EscapedChar == '=') { // %= -> Generate an unique ID.
275 CurStringPiece += "${:uid}";
276 continue;
277 }
Mike Stump11289f42009-09-09 15:08:12 +0000278
Chris Lattner35b58362009-03-10 23:21:44 +0000279 // Otherwise, we have an operand. If we have accumulated a string so far,
280 // add it to the Pieces list.
281 if (!CurStringPiece.empty()) {
282 Pieces.push_back(AsmStringPiece(CurStringPiece));
283 CurStringPiece.clear();
284 }
Mike Stump11289f42009-09-09 15:08:12 +0000285
Chris Lattner35b58362009-03-10 23:21:44 +0000286 // Handle %x4 and %x[foo] by capturing x as the modifier character.
287 char Modifier = '\0';
288 if (isalpha(EscapedChar)) {
289 Modifier = EscapedChar;
Chris Lattnera41b8472009-03-10 23:51:40 +0000290 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000291 }
Mike Stump11289f42009-09-09 15:08:12 +0000292
Chris Lattner35b58362009-03-10 23:21:44 +0000293 if (isdigit(EscapedChar)) {
294 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000295 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000296
Chris Lattner99d892b2009-03-11 22:52:17 +0000297 --CurPtr;
298 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000299 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattner14311922009-03-11 00:23:13 +0000301 unsigned NumOperands =
302 getNumOutputs() + getNumPlusOperands() + getNumInputs();
303 if (N >= NumOperands) {
304 DiagOffs = CurPtr-StrStart-1;
305 return diag::err_asm_invalid_operand_number;
306 }
307
Chris Lattner35b58362009-03-10 23:21:44 +0000308 Pieces.push_back(AsmStringPiece(N, Modifier));
309 continue;
310 }
Mike Stump11289f42009-09-09 15:08:12 +0000311
Chris Lattner35b58362009-03-10 23:21:44 +0000312 // Handle %[foo], a symbolic operand reference.
313 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000314 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000315
Chris Lattner3fa25c62009-03-11 00:06:36 +0000316 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000317 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000318 if (NameEnd == 0)
319 return diag::err_asm_unterminated_symbolic_operand_name;
320 if (NameEnd == CurPtr)
321 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000322
Anders Carlsson0c5d7442010-01-30 20:48:08 +0000323 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000324
Chris Lattner35b58362009-03-10 23:21:44 +0000325 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000326 if (N == -1) {
327 // Verify that an operand with that name exists.
328 DiagOffs = CurPtr-StrStart;
329 return diag::err_asm_unknown_symbolic_operand_name;
330 }
Chris Lattner35b58362009-03-10 23:21:44 +0000331 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump11289f42009-09-09 15:08:12 +0000332
Chris Lattner3fa25c62009-03-11 00:06:36 +0000333 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000334 continue;
335 }
Mike Stump11289f42009-09-09 15:08:12 +0000336
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000337 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000338 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000339 }
340}
341
Chris Lattner86f5e132008-01-30 05:01:46 +0000342//===----------------------------------------------------------------------===//
343// Constructors
344//===----------------------------------------------------------------------===//
345
Anders Carlsson98323d22010-01-30 23:19:41 +0000346AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
347 bool isvolatile, bool msasm,
348 unsigned numoutputs, unsigned numinputs,
Anders Carlsson9a020f92010-01-30 22:25:16 +0000349 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattner86f5e132008-01-30 05:01:46 +0000350 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
351 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000352 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump90be58a2010-01-04 22:37:17 +0000353 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson98323d22010-01-30 23:19:41 +0000354 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weberde565e32008-08-05 23:15:29 +0000355
Anders Carlsson98323d22010-01-30 23:19:41 +0000356 unsigned NumExprs = NumOutputs +NumInputs;
357
358 Names = new (C) IdentifierInfo*[NumExprs];
359 std::copy(names, names + NumExprs, Names);
360
361 Exprs = new (C) Stmt*[NumExprs];
362 std::copy(exprs, exprs + NumExprs, Exprs);
363
364 Constraints = new (C) StringLiteral*[NumExprs];
365 std::copy(constraints, constraints + NumExprs, Constraints);
366
367 Clobbers = new (C) StringLiteral*[NumClobbers];
368 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000369}
370
Chris Lattner86f5e132008-01-30 05:01:46 +0000371ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
372 Stmt *Body, SourceLocation FCL,
Nico Weberde565e32008-08-05 23:15:29 +0000373 SourceLocation RPL)
Chris Lattner86f5e132008-01-30 05:01:46 +0000374: Stmt(ObjCForCollectionStmtClass) {
375 SubExprs[ELEM] = Elem;
376 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
377 SubExprs[BODY] = Body;
378 ForLoc = FCL;
379 RParenLoc = RPL;
380}
381
382
Nico Weberde565e32008-08-05 23:15:29 +0000383ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
384 SourceLocation rparenloc,
Steve Naroff371b8fb2009-03-03 19:52:17 +0000385 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattner86f5e132008-01-30 05:01:46 +0000386 Stmt *atCatchList)
387: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff371b8fb2009-03-03 19:52:17 +0000388 ExceptionDecl = catchVarDecl;
Chris Lattner86f5e132008-01-30 05:01:46 +0000389 SubExprs[BODY] = atCatchStmt;
Eli Friedman1f97e572008-05-25 04:34:57 +0000390 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar947bca22009-03-01 04:28:32 +0000391 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman1f97e572008-05-25 04:34:57 +0000392 if (atCatchList) {
Ted Kremeneka4965842008-02-01 21:28:59 +0000393 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
394
Nico Weberde565e32008-08-05 23:15:29 +0000395 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremeneka4965842008-02-01 21:28:59 +0000396 AtCatchList = NextCatch;
Nico Weberde565e32008-08-05 23:15:29 +0000397
Ted Kremeneka4965842008-02-01 21:28:59 +0000398 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattner86f5e132008-01-30 05:01:46 +0000399 }
400 AtCatchLoc = atCatchLoc;
401 RParenLoc = rparenloc;
402}
403
Ted Kremenek1c3ab072009-12-24 01:48:39 +0000404//===----------------------------------------------------------------------===//
405// AST Destruction.
406//===----------------------------------------------------------------------===//
407
408void Stmt::DestroyChildren(ASTContext &C) {
409 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
410 if (Stmt* Child = *I++) Child->Destroy(C);
411}
412
413static void BranchDestroy(ASTContext &C, Stmt *S, Stmt **SubExprs,
414 unsigned NumExprs) {
415 // We do not use child_iterator here because that will include
416 // the expressions referenced by the condition variable.
417 for (Stmt **I = SubExprs, **E = SubExprs + NumExprs; I != E; ++I)
418 if (Stmt *Child = *I) Child->Destroy(C);
419
420 S->~Stmt();
421 C.Deallocate((void *) S);
422}
423
424void Stmt::DoDestroy(ASTContext &C) {
425 DestroyChildren(C);
426 this->~Stmt();
427 C.Deallocate((void *)this);
428}
429
430void CXXCatchStmt::DoDestroy(ASTContext& C) {
431 if (ExceptionDecl)
432 ExceptionDecl->Destroy(C);
433 Stmt::DoDestroy(C);
434}
435
436void DeclStmt::DoDestroy(ASTContext &C) {
437 // Don't use StmtIterator to iterate over the Decls, as that can recurse
438 // into VLA size expressions (which are owned by the VLA). Further, Decls
439 // are owned by the DeclContext, and will be destroyed with them.
440 if (DG.isDeclGroup())
441 DG.getDeclGroup().Destroy(C);
442}
443
444void IfStmt::DoDestroy(ASTContext &C) {
445 BranchDestroy(C, this, SubExprs, END_EXPR);
446}
447
448void ForStmt::DoDestroy(ASTContext &C) {
449 BranchDestroy(C, this, SubExprs, END_EXPR);
450}
451
452void SwitchStmt::DoDestroy(ASTContext &C) {
453 // Destroy the SwitchCase statements in this switch. In the normal
454 // case, this loop will merely decrement the reference counts from
455 // the Retain() calls in addSwitchCase();
456 SwitchCase *SC = FirstCase;
457 while (SC) {
458 SwitchCase *Next = SC->getNextSwitchCase();
459 SC->Destroy(C);
460 SC = Next;
461 }
462
463 BranchDestroy(C, this, SubExprs, END_EXPR);
464}
465
466void WhileStmt::DoDestroy(ASTContext &C) {
467 BranchDestroy(C, this, SubExprs, END_EXPR);
468}
Chris Lattner86f5e132008-01-30 05:01:46 +0000469
Anders Carlsson98323d22010-01-30 23:19:41 +0000470void AsmStmt::DoDestroy(ASTContext &C) {
471 DestroyChildren(C);
472
473 C.Deallocate(Names);
474 C.Deallocate(Constraints);
475 C.Deallocate(Exprs);
476 C.Deallocate(Clobbers);
477
Benjamin Kramer43a645c2010-01-31 09:01:55 +0000478 this->~AsmStmt();
Anders Carlsson98323d22010-01-30 23:19:41 +0000479 C.Deallocate((void *)this);
480}
481
Ted Kremenek066dd932007-08-24 21:09:09 +0000482//===----------------------------------------------------------------------===//
483// Child Iterators for iterating over subexpressions/substatements
484//===----------------------------------------------------------------------===//
485
486// DeclStmt
Ted Kremenek9bb286f2008-10-07 23:09:49 +0000487Stmt::child_iterator DeclStmt::child_begin() {
488 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek4f8792b2008-08-05 20:46:55 +0000489}
490
Ted Kremenek9bb286f2008-10-07 23:09:49 +0000491Stmt::child_iterator DeclStmt::child_end() {
492 return StmtIterator(DG.end(), DG.end());
Ted Kremenekacf920d2008-10-06 20:54:44 +0000493}
494
Ted Kremenek066dd932007-08-24 21:09:09 +0000495// NullStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000496Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
497Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000498
499// CompoundStmt
500Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek5a201952009-02-07 01:47:29 +0000501Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000502
Ted Kremenek14f0d1a2007-08-30 16:50:46 +0000503// CaseStmt
504Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
505Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
506
507// DefaultStmt
508Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
509Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000510
511// LabelStmt
512Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000513Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000514
515// IfStmt
Ted Kremenekb27a6d22009-12-23 23:38:34 +0000516Stmt::child_iterator IfStmt::child_begin() {
517 return child_iterator(Var, &SubExprs[0]);
518}
519Stmt::child_iterator IfStmt::child_end() {
520 return child_iterator(0, &SubExprs[0]+END_EXPR);
521}
Ted Kremenek066dd932007-08-24 21:09:09 +0000522
523// SwitchStmt
Ted Kremenekee7553d2009-12-24 00:39:05 +0000524Stmt::child_iterator SwitchStmt::child_begin() {
525 return child_iterator(Var, &SubExprs[0]);
526}
527Stmt::child_iterator SwitchStmt::child_end() {
528 return child_iterator(0, &SubExprs[0]+END_EXPR);
529}
Ted Kremenek066dd932007-08-24 21:09:09 +0000530
531// WhileStmt
Ted Kremenekb04c5cb2009-12-24 00:54:19 +0000532Stmt::child_iterator WhileStmt::child_begin() {
533 return child_iterator(Var, &SubExprs[0]);
534}
535Stmt::child_iterator WhileStmt::child_end() {
536 return child_iterator(0, &SubExprs[0]+END_EXPR);
537}
Ted Kremenek066dd932007-08-24 21:09:09 +0000538
539// DoStmt
540Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
541Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
542
543// ForStmt
Ted Kremenek1c3ab072009-12-24 01:48:39 +0000544Stmt::child_iterator ForStmt::child_begin() {
545 return child_iterator(CondVar, &SubExprs[0]);
546}
547Stmt::child_iterator ForStmt::child_end() {
Ted Kremenek17113252009-12-24 01:59:46 +0000548 return child_iterator(0, &SubExprs[0]+END_EXPR);
Ted Kremenek1c3ab072009-12-24 01:48:39 +0000549}
Ted Kremenek066dd932007-08-24 21:09:09 +0000550
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000551// ObjCForCollectionStmt
Nico Weberde565e32008-08-05 23:15:29 +0000552Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
553 return &SubExprs[0];
Fariborz Jahanian83615522008-01-02 22:54:34 +0000554}
Nico Weberde565e32008-08-05 23:15:29 +0000555Stmt::child_iterator ObjCForCollectionStmt::child_end() {
556 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian83615522008-01-02 22:54:34 +0000557}
558
Ted Kremenek066dd932007-08-24 21:09:09 +0000559// GotoStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000560Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
561Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000562
563// IndirectGotoStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000564Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
565const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000566
Ted Kremenekc6501db2008-06-17 03:11:08 +0000567Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
568Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek066dd932007-08-24 21:09:09 +0000569
570// ContinueStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000571Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
572Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000573
574// BreakStmt
Ted Kremenek04746ce2007-10-18 23:28:49 +0000575Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
576Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek066dd932007-08-24 21:09:09 +0000577
578// ReturnStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000579const Expr* ReturnStmt::getRetValue() const {
580 return cast_or_null<Expr>(RetExpr);
581}
582Expr* ReturnStmt::getRetValue() {
583 return cast_or_null<Expr>(RetExpr);
Ted Kremenek066dd932007-08-24 21:09:09 +0000584}
585
Ted Kremenekc6501db2008-06-17 03:11:08 +0000586Stmt::child_iterator ReturnStmt::child_begin() {
587 return &RetExpr;
588}
589Stmt::child_iterator ReturnStmt::child_end() {
590 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek5b3ed282007-08-27 20:58:16 +0000591}
Ted Kremenek066dd932007-08-24 21:09:09 +0000592
Chris Lattner73c56c02007-10-29 04:04:16 +0000593// AsmStmt
Mike Stump11289f42009-09-09 15:08:12 +0000594Stmt::child_iterator AsmStmt::child_begin() {
Anders Carlsson98323d22010-01-30 23:19:41 +0000595 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0];
Ted Kremenek5778acf2008-10-27 18:40:21 +0000596}
597Stmt::child_iterator AsmStmt::child_end() {
Anders Carlsson98323d22010-01-30 23:19:41 +0000598 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0] + NumOutputs + NumInputs;
Ted Kremenek5778acf2008-10-27 18:40:21 +0000599}
Chris Lattner73c56c02007-10-29 04:04:16 +0000600
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000601// ObjCAtCatchStmt
602Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weberde565e32008-08-05 23:15:29 +0000603Stmt::child_iterator ObjCAtCatchStmt::child_end() {
604 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +0000605}
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000606
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000607// ObjCAtFinallyStmt
608Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
609Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000610
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000611// ObjCAtTryStmt
612Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weberde565e32008-08-05 23:15:29 +0000613Stmt::child_iterator ObjCAtTryStmt::child_end() {
614 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +0000615}
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000616
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000617// ObjCAtThrowStmt
618Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000619 return &Throw;
620}
621
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000622Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000623 return &Throw+1;
624}
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000625
626// ObjCAtSynchronizedStmt
627Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahanian284011b2008-01-29 22:59:37 +0000628 return &SubStmts[0];
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000629}
630
631Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahanian284011b2008-01-29 22:59:37 +0000632 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian48085b82008-01-29 19:14:59 +0000633}
634
Sebastian Redl54c04d42008-12-22 19:15:10 +0000635// CXXCatchStmt
636Stmt::child_iterator CXXCatchStmt::child_begin() {
637 return &HandlerBlock;
638}
639
640Stmt::child_iterator CXXCatchStmt::child_end() {
641 return &HandlerBlock + 1;
642}
643
Mike Stump21d68e22009-11-30 20:10:58 +0000644QualType CXXCatchStmt::getCaughtType() const {
Sebastian Redl54c04d42008-12-22 19:15:10 +0000645 if (ExceptionDecl)
Douglas Gregor5e16fbe2009-05-18 20:51:54 +0000646 return ExceptionDecl->getType();
Sebastian Redl54c04d42008-12-22 19:15:10 +0000647 return QualType();
648}
649
Sebastian Redl9b244a82008-12-22 21:35:02 +0000650// CXXTryStmt
651Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
652Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
653
654CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
655 Stmt **handlers, unsigned numHandlers)
656 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
657 Stmts.push_back(tryBlock);
658 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
659}