blob: dfdb2ce453d5707bade39597b91d7db1973ba6cd [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
Chris Lattner24e1e702009-03-04 04:23:07 +000050void Stmt::DestroyChildren(ASTContext &C) {
51 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
Douglas Gregor860f6d42009-01-16 06:50:08 +000052 if (Stmt* Child = *I++) Child->Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +000053}
54
Douglas Gregor42602bb2009-08-07 06:08:38 +000055void Stmt::DoDestroy(ASTContext &C) {
Ted Kremenek27f8a282008-05-20 00:43:19 +000056 DestroyChildren(C);
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000057 this->~Stmt();
58 C.Deallocate((void *)this);
Ted Kremenek9c1863e2008-05-19 22:02:12 +000059}
60
Reid Spencer5f016e22007-07-11 17:01:13 +000061void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000062 // Ensure the table is primed.
63 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065 unsigned sum = 0;
66 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000067 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000068 if (StmtClassInfo[i].Name == 0) continue;
69 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
71 fprintf(stderr, " %d stmts/exprs total.\n", sum);
72 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000073 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000074 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000075 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000076 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000077 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
78 StmtClassInfo[i].Size,
79 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
80 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000081 }
82 fprintf(stderr, "Total bytes = %d\n", sum);
83}
84
85void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000086 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
89static bool StatSwitch = false;
90
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000091bool Stmt::CollectingStats(bool Enable) {
92 if (Enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000093 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000094}
95
Douglas Gregor43d9d922009-08-08 01:41:12 +000096void SwitchStmt::DoDestroy(ASTContext &Ctx) {
97 // Destroy the SwitchCase statements in this switch. In the normal
Mike Stump1eb44332009-09-09 15:08:12 +000098 // case, this loop will merely decrement the reference counts from
Douglas Gregor43d9d922009-08-08 01:41:12 +000099 // the Retain() calls in addSwitchCase();
100 SwitchCase *SC = FirstCase;
101 while (SC) {
102 SwitchCase *Next = SC->getNextSwitchCase();
103 SC->Destroy(Ctx);
104 SC = Next;
105 }
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000106
107 // We do not use child_iterator here because that will include
108 // the expressions referenced by the condition variable.
109 for (Stmt **I = &SubExprs[0], **E = &SubExprs[END_EXPR]; I != E; ++I)
110 if (Stmt *Child = *I) Child->Destroy(Ctx);
111
112 this->~Stmt();
113 Ctx.Deallocate((void *)this);
Douglas Gregor43d9d922009-08-08 01:41:12 +0000114}
115
Douglas Gregor025452f2009-04-17 00:04:06 +0000116void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
117 if (this->Body)
118 C.Deallocate(Body);
119 this->NumStmts = NumStmts;
120
121 Body = new (C) Stmt*[NumStmts];
122 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
123}
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125const char *LabelStmt::getName() const {
Daniel Dunbare013d682009-10-18 20:26:12 +0000126 return getID()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000127}
128
Steve Naroff507f2d52007-08-31 23:49:30 +0000129// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000130SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000131 if (RetExpr)
132 return SourceRange(RetLoc, RetExpr->getLocEnd());
133 else
134 return SourceRange(RetLoc);
135}
136
Ted Kremenekd48ade62007-10-01 16:34:52 +0000137bool Stmt::hasImplicitControlFlow() const {
138 switch (sClass) {
139 default:
140 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000141
Ted Kremenekd48ade62007-10-01 16:34:52 +0000142 case CallExprClass:
143 case ConditionalOperatorClass:
144 case ChooseExprClass:
145 case StmtExprClass:
146 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000147 return true;
148
Ted Kremenekd48ade62007-10-01 16:34:52 +0000149 case Stmt::BinaryOperatorClass: {
150 const BinaryOperator* B = cast<BinaryOperator>(this);
151 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
152 return true;
153 else
154 return false;
155 }
156 }
157}
158
Chris Lattnerb3277932009-03-10 04:59:06 +0000159Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000160 return cast<Expr>(Exprs[i]);
161}
Chris Lattnerb3277932009-03-10 04:59:06 +0000162
163/// getOutputConstraint - Return the constraint string for the specified
164/// output operand. All output constraints are known to be non-empty (either
165/// '=' or '+').
166std::string AsmStmt::getOutputConstraint(unsigned i) const {
167 return std::string(Constraints[i]->getStrData(),
168 Constraints[i]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000169}
Chris Lattnerb3277932009-03-10 04:59:06 +0000170
Chris Lattner85759272009-03-11 00:23:13 +0000171/// getNumPlusOperands - Return the number of output operands that have a "+"
172/// constraint.
173unsigned AsmStmt::getNumPlusOperands() const {
174 unsigned Res = 0;
175 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
176 if (isOutputPlusConstraint(i))
177 ++Res;
178 return Res;
179}
180
181
Chris Lattnerb3277932009-03-10 04:59:06 +0000182
183Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000184 return cast<Expr>(Exprs[i + NumOutputs]);
185}
Chris Lattnerb3277932009-03-10 04:59:06 +0000186
187/// getInputConstraint - Return the specified input constraint. Unlike output
188/// constraints, these can be empty.
189std::string AsmStmt::getInputConstraint(unsigned i) const {
190 return std::string(Constraints[i + NumOutputs]->getStrData(),
191 Constraints[i + NumOutputs]->getByteLength());
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000192}
193
Chris Lattner10ca96a2009-03-10 06:33:24 +0000194
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000195void AsmStmt::setOutputsAndInputs(unsigned NumOutputs,
Mike Stump1eb44332009-09-09 15:08:12 +0000196 unsigned NumInputs,
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000197 const std::string *Names,
198 StringLiteral **Constraints,
199 Stmt **Exprs) {
200 this->NumOutputs = NumOutputs;
201 this->NumInputs = NumInputs;
202 this->Names.clear();
203 this->Names.insert(this->Names.end(), Names, Names + NumOutputs + NumInputs);
204 this->Constraints.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000205 this->Constraints.insert(this->Constraints.end(),
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000206 Constraints, Constraints + NumOutputs + NumInputs);
207 this->Exprs.clear();
208 this->Exprs.insert(this->Exprs.end(), Exprs, Exprs + NumOutputs + NumInputs);
209}
210
Chris Lattner10ca96a2009-03-10 06:33:24 +0000211/// getNamedOperand - Given a symbolic operand reference like %[foo],
212/// translate this into a numeric value needed to reference the same operand.
213/// This returns -1 if the operand name is invalid.
214int AsmStmt::getNamedOperand(const std::string &SymbolicName) const {
215 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Chris Lattner10ca96a2009-03-10 06:33:24 +0000217 // Check if this is an output operand.
218 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
219 if (getOutputName(i) == SymbolicName)
220 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000221 }
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Chris Lattner10ca96a2009-03-10 06:33:24 +0000223 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
224 if (getInputName(i) == SymbolicName)
225 return getNumOutputs() + NumPlusOperands + i;
226
227 // Not found.
228 return -1;
229}
230
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000231void AsmStmt::setClobbers(StringLiteral **Clobbers, unsigned NumClobbers) {
232 this->Clobbers.clear();
233 this->Clobbers.insert(this->Clobbers.end(), Clobbers, Clobbers + NumClobbers);
234}
Chris Lattner10ca96a2009-03-10 06:33:24 +0000235
Chris Lattner458cd9c2009-03-10 23:21:44 +0000236/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
237/// it into pieces. If the asm string is erroneous, emit errors and return
238/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000239unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
240 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000241 const char *StrStart = getAsmString()->getStrData();
242 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000243 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Chris Lattner458cd9c2009-03-10 23:21:44 +0000245 // "Simple" inline asms have no constraints or operands, just convert the asm
246 // string to escape $'s.
247 if (isSimple()) {
248 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000249 for (; CurPtr != StrEnd; ++CurPtr) {
250 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000251 case '$':
252 Result += "$$";
253 break;
254 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000255 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000256 break;
257 }
258 }
259 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000260 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000261 }
262
263 // CurStringPiece - The current string that we are building up as we scan the
264 // asm string.
265 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Chris Lattner458cd9c2009-03-10 23:21:44 +0000267 while (1) {
268 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000269 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000270 if (!CurStringPiece.empty())
271 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000272 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattner3182db12009-03-10 23:51:40 +0000275 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000276 if (CurChar == '$') {
277 CurStringPiece += "$$";
278 continue;
279 } else if (CurChar != '%') {
280 CurStringPiece += CurChar;
281 continue;
282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Chris Lattner458cd9c2009-03-10 23:21:44 +0000284 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000285 if (CurPtr == StrEnd) {
286 // % at end of string is invalid (no escape).
287 DiagOffs = CurPtr-StrStart-1;
288 return diag::err_asm_invalid_escape;
289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattner3182db12009-03-10 23:51:40 +0000291 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000292 if (EscapedChar == '%') { // %% -> %
293 // Escaped percentage sign.
294 CurStringPiece += '%';
295 continue;
296 }
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattner458cd9c2009-03-10 23:21:44 +0000298 if (EscapedChar == '=') { // %= -> Generate an unique ID.
299 CurStringPiece += "${:uid}";
300 continue;
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattner458cd9c2009-03-10 23:21:44 +0000303 // Otherwise, we have an operand. If we have accumulated a string so far,
304 // add it to the Pieces list.
305 if (!CurStringPiece.empty()) {
306 Pieces.push_back(AsmStringPiece(CurStringPiece));
307 CurStringPiece.clear();
308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattner458cd9c2009-03-10 23:21:44 +0000310 // Handle %x4 and %x[foo] by capturing x as the modifier character.
311 char Modifier = '\0';
312 if (isalpha(EscapedChar)) {
313 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000314 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Chris Lattner458cd9c2009-03-10 23:21:44 +0000317 if (isdigit(EscapedChar)) {
318 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000319 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Chris Lattnercafc2222009-03-11 22:52:17 +0000321 --CurPtr;
322 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000323 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Chris Lattner85759272009-03-11 00:23:13 +0000325 unsigned NumOperands =
326 getNumOutputs() + getNumPlusOperands() + getNumInputs();
327 if (N >= NumOperands) {
328 DiagOffs = CurPtr-StrStart-1;
329 return diag::err_asm_invalid_operand_number;
330 }
331
Chris Lattner458cd9c2009-03-10 23:21:44 +0000332 Pieces.push_back(AsmStringPiece(N, Modifier));
333 continue;
334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattner458cd9c2009-03-10 23:21:44 +0000336 // Handle %[foo], a symbolic operand reference.
337 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000338 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000340 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000341 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000342 if (NameEnd == 0)
343 return diag::err_asm_unterminated_symbolic_operand_name;
344 if (NameEnd == CurPtr)
345 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner3182db12009-03-10 23:51:40 +0000347 std::string SymbolicName(CurPtr, NameEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Chris Lattner458cd9c2009-03-10 23:21:44 +0000349 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000350 if (N == -1) {
351 // Verify that an operand with that name exists.
352 DiagOffs = CurPtr-StrStart;
353 return diag::err_asm_unknown_symbolic_operand_name;
354 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000355 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000357 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000358 continue;
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner2ff0f422009-03-10 23:57:07 +0000361 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000362 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000363 }
364}
365
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000366//===----------------------------------------------------------------------===//
367// Constructors
368//===----------------------------------------------------------------------===//
369
Anders Carlssondfab34a2008-02-05 23:03:50 +0000370AsmStmt::AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000371 unsigned numoutputs, unsigned numinputs,
372 std::string *names, StringLiteral **constraints,
373 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
374 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000375 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Anders Carlssondfab34a2008-02-05 23:03:50 +0000376 , IsSimple(issimple), IsVolatile(isvolatile)
377 , NumOutputs(numoutputs), NumInputs(numinputs) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000378 for (unsigned i = 0, e = numinputs + numoutputs; i != e; i++) {
379 Names.push_back(names[i]);
380 Exprs.push_back(exprs[i]);
Nico Weber608b17f2008-08-05 23:15:29 +0000381 Constraints.push_back(constraints[i]);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000382 }
Nico Weber608b17f2008-08-05 23:15:29 +0000383
Anders Carlssonb235fc22007-11-22 01:36:19 +0000384 for (unsigned i = 0; i != numclobbers; i++)
385 Clobbers.push_back(clobbers[i]);
386}
387
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000388ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
389 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000390 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000391: Stmt(ObjCForCollectionStmtClass) {
392 SubExprs[ELEM] = Elem;
393 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
394 SubExprs[BODY] = Body;
395 ForLoc = FCL;
396 RParenLoc = RPL;
397}
398
399
Nico Weber608b17f2008-08-05 23:15:29 +0000400ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
401 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000402 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000403 Stmt *atCatchList)
404: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000405 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000406 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000407 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000408 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000409 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000410 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
411
Nico Weber608b17f2008-08-05 23:15:29 +0000412 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000413 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000414
Ted Kremenekff981022008-02-01 21:28:59 +0000415 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000416 }
417 AtCatchLoc = atCatchLoc;
418 RParenLoc = rparenloc;
419}
420
421
Ted Kremenek82977772007-08-24 21:09:09 +0000422//===----------------------------------------------------------------------===//
423// Child Iterators for iterating over subexpressions/substatements
424//===----------------------------------------------------------------------===//
425
426// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000427Stmt::child_iterator DeclStmt::child_begin() {
428 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000429}
430
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000431Stmt::child_iterator DeclStmt::child_end() {
432 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000433}
434
Ted Kremeneke7809d42009-12-23 08:56:00 +0000435void 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
Ted Kremenek82977772007-08-24 21:09:09 +0000443// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000444Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
445Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000446
447// CompoundStmt
448Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000449Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000450
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000451// CaseStmt
452Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
453Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
454
455// DefaultStmt
456Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
457Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000458
459// LabelStmt
460Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000461Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000462
463// IfStmt
Ted Kremenek35628d12009-12-23 23:38:34 +0000464Stmt::child_iterator IfStmt::child_begin() {
465 return child_iterator(Var, &SubExprs[0]);
466}
467Stmt::child_iterator IfStmt::child_end() {
468 return child_iterator(0, &SubExprs[0]+END_EXPR);
469}
470void IfStmt::DoDestroy(ASTContext &C) {
471 // We do not use child_iterator here because that will include
472 // the expressions referenced by the condition variable.
473 for (Stmt **I = &SubExprs[0], **E = &SubExprs[END_EXPR]; I != E; ++I)
474 if (Stmt *Child = *I) Child->Destroy(C);
475
476 this->~Stmt();
477 C.Deallocate((void *)this);
478}
Ted Kremenek82977772007-08-24 21:09:09 +0000479
480// SwitchStmt
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000481Stmt::child_iterator SwitchStmt::child_begin() {
482 return child_iterator(Var, &SubExprs[0]);
483}
484Stmt::child_iterator SwitchStmt::child_end() {
485 return child_iterator(0, &SubExprs[0]+END_EXPR);
486}
Ted Kremenek82977772007-08-24 21:09:09 +0000487
488// WhileStmt
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000489Stmt::child_iterator WhileStmt::child_begin() {
490 return child_iterator(Var, &SubExprs[0]);
491}
492Stmt::child_iterator WhileStmt::child_end() {
493 return child_iterator(0, &SubExprs[0]+END_EXPR);
494}
495void WhileStmt::DoDestroy(ASTContext &C) {
496 // We do not use child_iterator here because that will include
497 // the expressions referenced by the condition variable.
498 for (Stmt **I = &SubExprs[0], **E = &SubExprs[END_EXPR]; I != E; ++I)
499 if (Stmt *Child = *I) Child->Destroy(C);
500
501 this->~Stmt();
502 C.Deallocate((void *)this);
503}
504
Ted Kremenek82977772007-08-24 21:09:09 +0000505
506// DoStmt
507Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
508Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
509
510// ForStmt
511Stmt::child_iterator ForStmt::child_begin() { return &SubExprs[0]; }
512Stmt::child_iterator ForStmt::child_end() { return &SubExprs[0]+END_EXPR; }
513
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000514// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000515Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
516 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000517}
Nico Weber608b17f2008-08-05 23:15:29 +0000518Stmt::child_iterator ObjCForCollectionStmt::child_end() {
519 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000520}
521
Ted Kremenek82977772007-08-24 21:09:09 +0000522// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000523Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
524Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000525
526// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000527Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
528const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000529
Ted Kremenek1060aff2008-06-17 03:11:08 +0000530Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
531Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000532
533// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000534Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
535Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000536
537// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000538Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
539Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000540
541// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000542const Expr* ReturnStmt::getRetValue() const {
543 return cast_or_null<Expr>(RetExpr);
544}
545Expr* ReturnStmt::getRetValue() {
546 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000547}
548
Ted Kremenek1060aff2008-06-17 03:11:08 +0000549Stmt::child_iterator ReturnStmt::child_begin() {
550 return &RetExpr;
551}
552Stmt::child_iterator ReturnStmt::child_end() {
553 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000554}
Ted Kremenek82977772007-08-24 21:09:09 +0000555
Chris Lattnerfe795952007-10-29 04:04:16 +0000556// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000557Stmt::child_iterator AsmStmt::child_begin() {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000558 return Exprs.empty() ? 0 : &Exprs[0];
559}
560Stmt::child_iterator AsmStmt::child_end() {
561 return Exprs.empty() ? 0 : &Exprs[0] + Exprs.size();
562}
Chris Lattnerfe795952007-10-29 04:04:16 +0000563
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564// ObjCAtCatchStmt
565Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000566Stmt::child_iterator ObjCAtCatchStmt::child_end() {
567 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000568}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000569
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000570// ObjCAtFinallyStmt
571Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
572Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000573
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000574// ObjCAtTryStmt
575Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000576Stmt::child_iterator ObjCAtTryStmt::child_end() {
577 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000578}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000579
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000580// ObjCAtThrowStmt
581Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000582 return &Throw;
583}
584
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000585Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000586 return &Throw+1;
587}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000588
589// ObjCAtSynchronizedStmt
590Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000591 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000592}
593
594Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000595 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000596}
597
Sebastian Redl4b07b292008-12-22 19:15:10 +0000598// CXXCatchStmt
599Stmt::child_iterator CXXCatchStmt::child_begin() {
600 return &HandlerBlock;
601}
602
603Stmt::child_iterator CXXCatchStmt::child_end() {
604 return &HandlerBlock + 1;
605}
606
Mike Stump6515afe2009-11-30 20:10:58 +0000607QualType CXXCatchStmt::getCaughtType() const {
Sebastian Redl4b07b292008-12-22 19:15:10 +0000608 if (ExceptionDecl)
Douglas Gregord308e622009-05-18 20:51:54 +0000609 return ExceptionDecl->getType();
Sebastian Redl4b07b292008-12-22 19:15:10 +0000610 return QualType();
611}
612
Douglas Gregor42602bb2009-08-07 06:08:38 +0000613void CXXCatchStmt::DoDestroy(ASTContext& C) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000614 if (ExceptionDecl)
615 ExceptionDecl->Destroy(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +0000616 Stmt::DoDestroy(C);
Sebastian Redl4b07b292008-12-22 19:15:10 +0000617}
Sebastian Redl8351da02008-12-22 21:35:02 +0000618
619// CXXTryStmt
620Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
621Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+Stmts.size(); }
622
623CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
624 Stmt **handlers, unsigned numHandlers)
625 : Stmt(CXXTryStmtClass), TryLoc(tryLoc) {
626 Stmts.push_back(tryBlock);
627 Stmts.insert(Stmts.end(), handlers, handlers + numHandlers);
628}