blob: ce97387ee36f42a75716885d58b767577b8d4c66 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Stmt.h"
15#include "clang/AST/ExprCXX.h"
Steve Narofff494b572008-05-29 21:12:08 +000016#include "clang/AST/ExprObjC.h"
Chris Lattner16f00492009-04-26 01:32:48 +000017#include "clang/AST/StmtCXX.h"
18#include "clang/AST/StmtObjC.h"
Sebastian Redl4b07b292008-12-22 19:15:10 +000019#include "clang/AST/Type.h"
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000020#include "clang/AST/ASTContext.h"
Chris Lattner3182db12009-03-10 23:51:40 +000021#include "clang/AST/ASTDiagnostic.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000022#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Reid Spencer5f016e22007-07-11 17:01:13 +000025static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000026 const char *Name;
27 unsigned Counter;
28 unsigned Size;
Chris Lattner1f683e92007-08-25 01:55:00 +000029} StmtClassInfo[Stmt::lastExprConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000030
31static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
32 static bool Initialized = false;
33 if (Initialized)
34 return StmtClassInfo[E];
35
36 // Intialize the table on the first use.
37 Initialized = true;
Douglas Gregorf2cad862008-11-14 12:46:07 +000038#define STMT(CLASS, PARENT) \
39 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
40 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Reid Spencer5f016e22007-07-11 17:01:13 +000041#include "clang/AST/StmtNodes.def"
Nico Weber608b17f2008-08-05 23:15:29 +000042
Chris Lattner63381352007-08-25 01:42:24 +000043 return StmtClassInfo[E];
44}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046const char *Stmt::getStmtClassName() const {
Douglas Gregor43d9d922009-08-08 01:41:12 +000047 return getStmtInfoTableEntry((StmtClass)sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
50void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000051 // Ensure the table is primed.
52 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000053
Reid Spencer5f016e22007-07-11 17:01:13 +000054 unsigned sum = 0;
55 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000056 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000057 if (StmtClassInfo[i].Name == 0) continue;
58 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 }
60 fprintf(stderr, " %d stmts/exprs total.\n", sum);
61 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000062 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000063 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000064 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000065 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000066 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
67 StmtClassInfo[i].Size,
68 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
69 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
71 fprintf(stderr, "Total bytes = %d\n", sum);
72}
73
74void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000075 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000076}
77
78static bool StatSwitch = false;
79
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000080bool Stmt::CollectingStats(bool Enable) {
81 if (Enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000082 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000083}
84
Douglas Gregor025452f2009-04-17 00:04:06 +000085void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
86 if (this->Body)
87 C.Deallocate(Body);
88 this->NumStmts = NumStmts;
89
90 Body = new (C) Stmt*[NumStmts];
91 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
92}
Reid Spencer5f016e22007-07-11 17:01:13 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094const char *LabelStmt::getName() const {
Daniel Dunbare013d682009-10-18 20:26:12 +000095 return getID()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +000096}
97
Steve Naroff507f2d52007-08-31 23:49:30 +000098// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +000099SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000100 if (RetExpr)
101 return SourceRange(RetLoc, RetExpr->getLocEnd());
102 else
103 return SourceRange(RetLoc);
104}
105
Ted Kremenekd48ade62007-10-01 16:34:52 +0000106bool Stmt::hasImplicitControlFlow() const {
107 switch (sClass) {
108 default:
109 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000110
Ted Kremenekd48ade62007-10-01 16:34:52 +0000111 case CallExprClass:
112 case ConditionalOperatorClass:
113 case ChooseExprClass:
114 case StmtExprClass:
115 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000116 return true;
117
Ted Kremenekd48ade62007-10-01 16:34:52 +0000118 case Stmt::BinaryOperatorClass: {
119 const BinaryOperator* B = cast<BinaryOperator>(this);
120 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
121 return true;
122 else
123 return false;
124 }
125 }
126}
127
Chris Lattnerb3277932009-03-10 04:59:06 +0000128Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000129 return cast<Expr>(Exprs[i]);
130}
Chris Lattnerb3277932009-03-10 04:59:06 +0000131
132/// getOutputConstraint - Return the constraint string for the specified
133/// output operand. All output constraints are known to be non-empty (either
134/// '=' or '+').
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000135llvm::StringRef AsmStmt::getOutputConstraint(unsigned i) const {
136 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000137}
Chris Lattnerb3277932009-03-10 04:59:06 +0000138
Chris Lattner85759272009-03-11 00:23:13 +0000139/// getNumPlusOperands - Return the number of output operands that have a "+"
140/// constraint.
141unsigned AsmStmt::getNumPlusOperands() const {
142 unsigned Res = 0;
143 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
144 if (isOutputPlusConstraint(i))
145 ++Res;
146 return Res;
147}
148
Chris Lattnerb3277932009-03-10 04:59:06 +0000149Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000150 return cast<Expr>(Exprs[i + NumOutputs]);
151}
Chris Lattnerb3277932009-03-10 04:59:06 +0000152
153/// getInputConstraint - Return the specified input constraint. Unlike output
154/// constraints, these can be empty.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000155llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
156 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000157}
158
Chris Lattner10ca96a2009-03-10 06:33:24 +0000159
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000160void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000161 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000162 StringLiteral **Constraints,
163 Stmt **Exprs,
164 unsigned NumOutputs,
165 unsigned NumInputs,
166 StringLiteral **Clobbers,
167 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000168 this->NumOutputs = NumOutputs;
169 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000170 this->NumClobbers = NumClobbers;
171
172 unsigned NumExprs = NumOutputs + NumInputs;
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000173
Anders Carlsson966146e2010-01-30 23:19:41 +0000174 C.Deallocate(this->Names);
175 this->Names = new (C) IdentifierInfo*[NumExprs];
176 std::copy(Names, Names + NumExprs, this->Names);
177
178 C.Deallocate(this->Exprs);
179 this->Exprs = new (C) Stmt*[NumExprs];
180 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
181
182 C.Deallocate(this->Constraints);
183 this->Constraints = new (C) StringLiteral*[NumExprs];
184 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
185
186 C.Deallocate(this->Clobbers);
187 this->Clobbers = new (C) StringLiteral*[NumClobbers];
188 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000189}
190
Chris Lattner10ca96a2009-03-10 06:33:24 +0000191/// getNamedOperand - Given a symbolic operand reference like %[foo],
192/// translate this into a numeric value needed to reference the same operand.
193/// This returns -1 if the operand name is invalid.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000194int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000195 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Chris Lattner10ca96a2009-03-10 06:33:24 +0000197 // Check if this is an output operand.
198 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
199 if (getOutputName(i) == SymbolicName)
200 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000201 }
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Chris Lattner10ca96a2009-03-10 06:33:24 +0000203 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
204 if (getInputName(i) == SymbolicName)
205 return getNumOutputs() + NumPlusOperands + i;
206
207 // Not found.
208 return -1;
209}
210
Chris Lattner458cd9c2009-03-10 23:21:44 +0000211/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
212/// it into pieces. If the asm string is erroneous, emit errors and return
213/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000214unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
215 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000216 const char *StrStart = getAsmString()->getStrData();
217 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000218 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Chris Lattner458cd9c2009-03-10 23:21:44 +0000220 // "Simple" inline asms have no constraints or operands, just convert the asm
221 // string to escape $'s.
222 if (isSimple()) {
223 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000224 for (; CurPtr != StrEnd; ++CurPtr) {
225 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000226 case '$':
227 Result += "$$";
228 break;
229 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000230 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000231 break;
232 }
233 }
234 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000235 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000236 }
237
238 // CurStringPiece - The current string that we are building up as we scan the
239 // asm string.
240 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner458cd9c2009-03-10 23:21:44 +0000242 while (1) {
243 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000244 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000245 if (!CurStringPiece.empty())
246 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000247 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattner3182db12009-03-10 23:51:40 +0000250 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000251 if (CurChar == '$') {
252 CurStringPiece += "$$";
253 continue;
254 } else if (CurChar != '%') {
255 CurStringPiece += CurChar;
256 continue;
257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattner458cd9c2009-03-10 23:21:44 +0000259 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000260 if (CurPtr == StrEnd) {
261 // % at end of string is invalid (no escape).
262 DiagOffs = CurPtr-StrStart-1;
263 return diag::err_asm_invalid_escape;
264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattner3182db12009-03-10 23:51:40 +0000266 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000267 if (EscapedChar == '%') { // %% -> %
268 // Escaped percentage sign.
269 CurStringPiece += '%';
270 continue;
271 }
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner458cd9c2009-03-10 23:21:44 +0000273 if (EscapedChar == '=') { // %= -> Generate an unique ID.
274 CurStringPiece += "${:uid}";
275 continue;
276 }
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner458cd9c2009-03-10 23:21:44 +0000278 // Otherwise, we have an operand. If we have accumulated a string so far,
279 // add it to the Pieces list.
280 if (!CurStringPiece.empty()) {
281 Pieces.push_back(AsmStringPiece(CurStringPiece));
282 CurStringPiece.clear();
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattner458cd9c2009-03-10 23:21:44 +0000285 // Handle %x4 and %x[foo] by capturing x as the modifier character.
286 char Modifier = '\0';
287 if (isalpha(EscapedChar)) {
288 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000289 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattner458cd9c2009-03-10 23:21:44 +0000292 if (isdigit(EscapedChar)) {
293 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000294 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Chris Lattnercafc2222009-03-11 22:52:17 +0000296 --CurPtr;
297 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000298 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Chris Lattner85759272009-03-11 00:23:13 +0000300 unsigned NumOperands =
301 getNumOutputs() + getNumPlusOperands() + getNumInputs();
302 if (N >= NumOperands) {
303 DiagOffs = CurPtr-StrStart-1;
304 return diag::err_asm_invalid_operand_number;
305 }
306
Chris Lattner458cd9c2009-03-10 23:21:44 +0000307 Pieces.push_back(AsmStringPiece(N, Modifier));
308 continue;
309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Chris Lattner458cd9c2009-03-10 23:21:44 +0000311 // Handle %[foo], a symbolic operand reference.
312 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000313 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000315 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000316 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000317 if (NameEnd == 0)
318 return diag::err_asm_unterminated_symbolic_operand_name;
319 if (NameEnd == CurPtr)
320 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Anders Carlsson95c9ce92010-01-30 20:48:08 +0000322 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner458cd9c2009-03-10 23:21:44 +0000324 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000325 if (N == -1) {
326 // Verify that an operand with that name exists.
327 DiagOffs = CurPtr-StrStart;
328 return diag::err_asm_unknown_symbolic_operand_name;
329 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000330 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000332 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000333 continue;
334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattner2ff0f422009-03-10 23:57:07 +0000336 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000337 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000338 }
339}
340
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000341//===----------------------------------------------------------------------===//
342// Constructors
343//===----------------------------------------------------------------------===//
344
Anders Carlsson966146e2010-01-30 23:19:41 +0000345AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
346 bool isvolatile, bool msasm,
347 unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000348 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000349 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
350 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000351 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000352 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson966146e2010-01-30 23:19:41 +0000353 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000354
Anders Carlsson966146e2010-01-30 23:19:41 +0000355 unsigned NumExprs = NumOutputs +NumInputs;
356
357 Names = new (C) IdentifierInfo*[NumExprs];
358 std::copy(names, names + NumExprs, Names);
359
360 Exprs = new (C) Stmt*[NumExprs];
361 std::copy(exprs, exprs + NumExprs, Exprs);
362
363 Constraints = new (C) StringLiteral*[NumExprs];
364 std::copy(constraints, constraints + NumExprs, Constraints);
365
366 Clobbers = new (C) StringLiteral*[NumClobbers];
367 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000368}
369
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000370ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
371 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000372 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000373: Stmt(ObjCForCollectionStmtClass) {
374 SubExprs[ELEM] = Elem;
375 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
376 SubExprs[BODY] = Body;
377 ForLoc = FCL;
378 RParenLoc = RPL;
379}
380
381
Nico Weber608b17f2008-08-05 23:15:29 +0000382ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
383 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000384 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000385 Stmt *atCatchList)
386: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000387 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000388 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000389 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000390 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000391 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000392 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
393
Nico Weber608b17f2008-08-05 23:15:29 +0000394 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000395 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000396
Ted Kremenekff981022008-02-01 21:28:59 +0000397 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000398 }
399 AtCatchLoc = atCatchLoc;
400 RParenLoc = rparenloc;
401}
402
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000403//===----------------------------------------------------------------------===//
404// AST Destruction.
405//===----------------------------------------------------------------------===//
406
407void Stmt::DestroyChildren(ASTContext &C) {
408 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
409 if (Stmt* Child = *I++) Child->Destroy(C);
410}
411
412static void BranchDestroy(ASTContext &C, Stmt *S, Stmt **SubExprs,
413 unsigned NumExprs) {
414 // We do not use child_iterator here because that will include
415 // the expressions referenced by the condition variable.
416 for (Stmt **I = SubExprs, **E = SubExprs + NumExprs; I != E; ++I)
417 if (Stmt *Child = *I) Child->Destroy(C);
418
419 S->~Stmt();
420 C.Deallocate((void *) S);
421}
422
423void Stmt::DoDestroy(ASTContext &C) {
424 DestroyChildren(C);
425 this->~Stmt();
426 C.Deallocate((void *)this);
427}
428
429void CXXCatchStmt::DoDestroy(ASTContext& C) {
430 if (ExceptionDecl)
431 ExceptionDecl->Destroy(C);
432 Stmt::DoDestroy(C);
433}
434
435void DeclStmt::DoDestroy(ASTContext &C) {
436 // Don't use StmtIterator to iterate over the Decls, as that can recurse
437 // into VLA size expressions (which are owned by the VLA). Further, Decls
438 // are owned by the DeclContext, and will be destroyed with them.
439 if (DG.isDeclGroup())
440 DG.getDeclGroup().Destroy(C);
441}
442
443void IfStmt::DoDestroy(ASTContext &C) {
444 BranchDestroy(C, this, SubExprs, END_EXPR);
445}
446
447void ForStmt::DoDestroy(ASTContext &C) {
448 BranchDestroy(C, this, SubExprs, END_EXPR);
449}
450
451void SwitchStmt::DoDestroy(ASTContext &C) {
452 // Destroy the SwitchCase statements in this switch. In the normal
453 // case, this loop will merely decrement the reference counts from
454 // the Retain() calls in addSwitchCase();
455 SwitchCase *SC = FirstCase;
456 while (SC) {
457 SwitchCase *Next = SC->getNextSwitchCase();
458 SC->Destroy(C);
459 SC = Next;
460 }
461
462 BranchDestroy(C, this, SubExprs, END_EXPR);
463}
464
465void WhileStmt::DoDestroy(ASTContext &C) {
466 BranchDestroy(C, this, SubExprs, END_EXPR);
467}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000468
Anders Carlsson966146e2010-01-30 23:19:41 +0000469void AsmStmt::DoDestroy(ASTContext &C) {
470 DestroyChildren(C);
471
472 C.Deallocate(Names);
473 C.Deallocate(Constraints);
474 C.Deallocate(Exprs);
475 C.Deallocate(Clobbers);
476
Benjamin Kramera0531542010-01-31 09:01:55 +0000477 this->~AsmStmt();
Anders Carlsson966146e2010-01-30 23:19:41 +0000478 C.Deallocate((void *)this);
479}
480
Ted Kremenek82977772007-08-24 21:09:09 +0000481//===----------------------------------------------------------------------===//
482// Child Iterators for iterating over subexpressions/substatements
483//===----------------------------------------------------------------------===//
484
485// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000486Stmt::child_iterator DeclStmt::child_begin() {
487 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000488}
489
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000490Stmt::child_iterator DeclStmt::child_end() {
491 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000492}
493
Ted Kremenek82977772007-08-24 21:09:09 +0000494// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000495Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
496Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000497
498// CompoundStmt
499Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000500Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000501
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000502// CaseStmt
503Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
504Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
505
506// DefaultStmt
507Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
508Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000509
510// LabelStmt
511Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000512Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000513
514// IfStmt
Ted Kremenek35628d12009-12-23 23:38:34 +0000515Stmt::child_iterator IfStmt::child_begin() {
516 return child_iterator(Var, &SubExprs[0]);
517}
518Stmt::child_iterator IfStmt::child_end() {
519 return child_iterator(0, &SubExprs[0]+END_EXPR);
520}
Ted Kremenek82977772007-08-24 21:09:09 +0000521
522// SwitchStmt
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000523Stmt::child_iterator SwitchStmt::child_begin() {
524 return child_iterator(Var, &SubExprs[0]);
525}
526Stmt::child_iterator SwitchStmt::child_end() {
527 return child_iterator(0, &SubExprs[0]+END_EXPR);
528}
Ted Kremenek82977772007-08-24 21:09:09 +0000529
530// WhileStmt
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000531Stmt::child_iterator WhileStmt::child_begin() {
532 return child_iterator(Var, &SubExprs[0]);
533}
534Stmt::child_iterator WhileStmt::child_end() {
535 return child_iterator(0, &SubExprs[0]+END_EXPR);
536}
Ted Kremenek82977772007-08-24 21:09:09 +0000537
538// DoStmt
539Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
540Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
541
542// ForStmt
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000543Stmt::child_iterator ForStmt::child_begin() {
544 return child_iterator(CondVar, &SubExprs[0]);
545}
546Stmt::child_iterator ForStmt::child_end() {
Ted Kremenek62812132009-12-24 01:59:46 +0000547 return child_iterator(0, &SubExprs[0]+END_EXPR);
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000548}
Ted Kremenek82977772007-08-24 21:09:09 +0000549
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000550// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000551Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
552 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000553}
Nico Weber608b17f2008-08-05 23:15:29 +0000554Stmt::child_iterator ObjCForCollectionStmt::child_end() {
555 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000556}
557
Ted Kremenek82977772007-08-24 21:09:09 +0000558// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000559Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
560Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000561
562// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000563Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
564const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000565
Ted Kremenek1060aff2008-06-17 03:11:08 +0000566Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
567Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000568
569// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000570Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
571Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000572
573// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000574Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
575Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000576
577// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000578const Expr* ReturnStmt::getRetValue() const {
579 return cast_or_null<Expr>(RetExpr);
580}
581Expr* ReturnStmt::getRetValue() {
582 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000583}
584
Ted Kremenek1060aff2008-06-17 03:11:08 +0000585Stmt::child_iterator ReturnStmt::child_begin() {
586 return &RetExpr;
587}
588Stmt::child_iterator ReturnStmt::child_end() {
589 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000590}
Ted Kremenek82977772007-08-24 21:09:09 +0000591
Chris Lattnerfe795952007-10-29 04:04:16 +0000592// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000593Stmt::child_iterator AsmStmt::child_begin() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000594 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0];
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000595}
596Stmt::child_iterator AsmStmt::child_end() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000597 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0] + NumOutputs + NumInputs;
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000598}
Chris Lattnerfe795952007-10-29 04:04:16 +0000599
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000600// ObjCAtCatchStmt
601Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000602Stmt::child_iterator ObjCAtCatchStmt::child_end() {
603 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000604}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000605
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000606// ObjCAtFinallyStmt
607Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
608Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000609
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000610// ObjCAtTryStmt
611Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000612Stmt::child_iterator ObjCAtTryStmt::child_end() {
613 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000614}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000615
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000616// ObjCAtThrowStmt
617Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000618 return &Throw;
619}
620
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000621Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000622 return &Throw+1;
623}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000624
625// ObjCAtSynchronizedStmt
626Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000627 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000628}
629
630Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000631 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000632}
633
Sebastian Redl4b07b292008-12-22 19:15:10 +0000634// CXXCatchStmt
635Stmt::child_iterator CXXCatchStmt::child_begin() {
636 return &HandlerBlock;
637}
638
639Stmt::child_iterator CXXCatchStmt::child_end() {
640 return &HandlerBlock + 1;
641}
642
Mike Stump6515afe2009-11-30 20:10:58 +0000643QualType CXXCatchStmt::getCaughtType() const {
Sebastian Redl4b07b292008-12-22 19:15:10 +0000644 if (ExceptionDecl)
Douglas Gregord308e622009-05-18 20:51:54 +0000645 return ExceptionDecl->getType();
Sebastian Redl4b07b292008-12-22 19:15:10 +0000646 return QualType();
647}
648
Sebastian Redl8351da02008-12-22 21:35:02 +0000649// CXXTryStmt
650Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
651Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
652
653CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
654 Stmt **handlers, unsigned numHandlers)
655 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
656 Stmts.push_back(tryBlock);
657 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
658}