blob: e90eceec1b34a4965dacf1daf13aaa652a632c09 [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;
John McCall09cc1412010-02-03 00:55:45 +000038#define ABSTRACT_EXPR(CLASS, PARENT)
Douglas Gregorf2cad862008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +000042#include "clang/AST/StmtNodes.def"
Nico Weber608b17f2008-08-05 23:15:29 +000043
Chris Lattner63381352007-08-25 01:42:24 +000044 return StmtClassInfo[E];
45}
46
Reid Spencer5f016e22007-07-11 17:01:13 +000047const char *Stmt::getStmtClassName() const {
Douglas Gregor43d9d922009-08-08 01:41:12 +000048 return getStmtInfoTableEntry((StmtClass)sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
51void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000052 // Ensure the table is primed.
53 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000054
Reid Spencer5f016e22007-07-11 17:01:13 +000055 unsigned sum = 0;
56 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Chris Lattner1f683e92007-08-25 01:55:00 +000057 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000058 if (StmtClassInfo[i].Name == 0) continue;
59 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000060 }
61 fprintf(stderr, " %d stmts/exprs total.\n", sum);
62 sum = 0;
Chris Lattner1f683e92007-08-25 01:55:00 +000063 for (int i = 0; i != Stmt::lastExprConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000064 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000065 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000066 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-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;
Reid Spencer5f016e22007-07-11 17:01:13 +000071 }
72 fprintf(stderr, "Total bytes = %d\n", sum);
73}
74
75void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000076 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000077}
78
79static bool StatSwitch = false;
80
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000081bool Stmt::CollectingStats(bool Enable) {
82 if (Enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000083 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000084}
85
Douglas Gregor025452f2009-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}
Reid Spencer5f016e22007-07-11 17:01:13 +000094
Reid Spencer5f016e22007-07-11 17:01:13 +000095const char *LabelStmt::getName() const {
Daniel Dunbare013d682009-10-18 20:26:12 +000096 return getID()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
Steve Naroff507f2d52007-08-31 23:49:30 +000099// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000100SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000101 if (RetExpr)
102 return SourceRange(RetLoc, RetExpr->getLocEnd());
103 else
104 return SourceRange(RetLoc);
105}
106
Ted Kremenekd48ade62007-10-01 16:34:52 +0000107bool Stmt::hasImplicitControlFlow() const {
108 switch (sClass) {
109 default:
110 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000111
Ted Kremenekd48ade62007-10-01 16:34:52 +0000112 case CallExprClass:
113 case ConditionalOperatorClass:
114 case ChooseExprClass:
115 case StmtExprClass:
116 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000117 return true;
118
Ted Kremenekd48ade62007-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 Lattnerb3277932009-03-10 04:59:06 +0000129Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000130 return cast<Expr>(Exprs[i]);
131}
Chris Lattnerb3277932009-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 Carlssonacb6bcb2010-01-30 20:38:10 +0000136llvm::StringRef AsmStmt::getOutputConstraint(unsigned i) const {
137 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000138}
Chris Lattnerb3277932009-03-10 04:59:06 +0000139
Chris Lattner85759272009-03-11 00:23:13 +0000140/// getNumPlusOperands - Return the number of output operands that have a "+"
141/// constraint.
142unsigned AsmStmt::getNumPlusOperands() const {
143 unsigned Res = 0;
144 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
145 if (isOutputPlusConstraint(i))
146 ++Res;
147 return Res;
148}
149
Chris Lattnerb3277932009-03-10 04:59:06 +0000150Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000151 return cast<Expr>(Exprs[i + NumOutputs]);
152}
Chris Lattnerb3277932009-03-10 04:59:06 +0000153
154/// getInputConstraint - Return the specified input constraint. Unlike output
155/// constraints, these can be empty.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000156llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
157 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000158}
159
Chris Lattner10ca96a2009-03-10 06:33:24 +0000160
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000161void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000162 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000163 StringLiteral **Constraints,
164 Stmt **Exprs,
165 unsigned NumOutputs,
166 unsigned NumInputs,
167 StringLiteral **Clobbers,
168 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000169 this->NumOutputs = NumOutputs;
170 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000171 this->NumClobbers = NumClobbers;
172
173 unsigned NumExprs = NumOutputs + NumInputs;
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000174
Anders Carlsson966146e2010-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 Gregorcd7d5a92009-04-17 20:57:14 +0000190}
191
Chris Lattner10ca96a2009-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 Carlssonacb6bcb2010-01-30 20:38:10 +0000195int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000196 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Chris Lattner10ca96a2009-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 Lattner10ca96a2009-03-10 06:33:24 +0000202 }
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattner10ca96a2009-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 Lattner458cd9c2009-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 Lattnerfb5058e2009-03-10 23:41:04 +0000215unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
216 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000217 const char *StrStart = getAsmString()->getStrData();
218 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000219 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Chris Lattner458cd9c2009-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 Lattner3182db12009-03-10 23:51:40 +0000225 for (; CurPtr != StrEnd; ++CurPtr) {
226 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000227 case '$':
228 Result += "$$";
229 break;
230 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000231 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000232 break;
233 }
234 }
235 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000236 return 0;
Chris Lattner458cd9c2009-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 Stump1eb44332009-09-09 15:08:12 +0000242
Chris Lattner458cd9c2009-03-10 23:21:44 +0000243 while (1) {
244 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000245 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000246 if (!CurStringPiece.empty())
247 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000248 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Chris Lattner3182db12009-03-10 23:51:40 +0000251 char CurChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000252 if (CurChar == '$') {
253 CurStringPiece += "$$";
254 continue;
255 } else if (CurChar != '%') {
256 CurStringPiece += CurChar;
257 continue;
258 }
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Chris Lattner458cd9c2009-03-10 23:21:44 +0000260 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-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 Stump1eb44332009-09-09 15:08:12 +0000266
Chris Lattner3182db12009-03-10 23:51:40 +0000267 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000268 if (EscapedChar == '%') { // %% -> %
269 // Escaped percentage sign.
270 CurStringPiece += '%';
271 continue;
272 }
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattner458cd9c2009-03-10 23:21:44 +0000274 if (EscapedChar == '=') { // %= -> Generate an unique ID.
275 CurStringPiece += "${:uid}";
276 continue;
277 }
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattner458cd9c2009-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 Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner458cd9c2009-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 Lattner3182db12009-03-10 23:51:40 +0000290 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattner458cd9c2009-03-10 23:21:44 +0000293 if (isdigit(EscapedChar)) {
294 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000295 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattnercafc2222009-03-11 22:52:17 +0000297 --CurPtr;
298 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000299 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattner85759272009-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 Lattner458cd9c2009-03-10 23:21:44 +0000308 Pieces.push_back(AsmStringPiece(N, Modifier));
309 continue;
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Chris Lattner458cd9c2009-03-10 23:21:44 +0000312 // Handle %[foo], a symbolic operand reference.
313 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000314 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000316 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000317 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-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 Stump1eb44332009-09-09 15:08:12 +0000322
Anders Carlsson95c9ce92010-01-30 20:48:08 +0000323 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Chris Lattner458cd9c2009-03-10 23:21:44 +0000325 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-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 Lattner458cd9c2009-03-10 23:21:44 +0000331 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000333 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000334 continue;
335 }
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattner2ff0f422009-03-10 23:57:07 +0000337 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000338 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000339 }
340}
341
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000342QualType CXXCatchStmt::getCaughtType() const {
343 if (ExceptionDecl)
344 return ExceptionDecl->getType();
345 return QualType();
346}
347
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000348//===----------------------------------------------------------------------===//
349// Constructors
350//===----------------------------------------------------------------------===//
351
Anders Carlsson966146e2010-01-30 23:19:41 +0000352AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
353 bool isvolatile, bool msasm,
354 unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000355 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000356 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
357 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000358 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000359 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson966146e2010-01-30 23:19:41 +0000360 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000361
Anders Carlsson966146e2010-01-30 23:19:41 +0000362 unsigned NumExprs = NumOutputs +NumInputs;
363
364 Names = new (C) IdentifierInfo*[NumExprs];
365 std::copy(names, names + NumExprs, Names);
366
367 Exprs = new (C) Stmt*[NumExprs];
368 std::copy(exprs, exprs + NumExprs, Exprs);
369
370 Constraints = new (C) StringLiteral*[NumExprs];
371 std::copy(constraints, constraints + NumExprs, Constraints);
372
373 Clobbers = new (C) StringLiteral*[NumClobbers];
374 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000375}
376
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000377ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
378 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000379 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000380: Stmt(ObjCForCollectionStmtClass) {
381 SubExprs[ELEM] = Elem;
382 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
383 SubExprs[BODY] = Body;
384 ForLoc = FCL;
385 RParenLoc = RPL;
386}
387
388
Nico Weber608b17f2008-08-05 23:15:29 +0000389ObjCAtCatchStmt::ObjCAtCatchStmt(SourceLocation atCatchLoc,
390 SourceLocation rparenloc,
Steve Naroff7ba138a2009-03-03 19:52:17 +0000391 ParmVarDecl *catchVarDecl, Stmt *atCatchStmt,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000392 Stmt *atCatchList)
393: Stmt(ObjCAtCatchStmtClass) {
Steve Naroff7ba138a2009-03-03 19:52:17 +0000394 ExceptionDecl = catchVarDecl;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000395 SubExprs[BODY] = atCatchStmt;
Eli Friedman0613c372008-05-25 04:34:57 +0000396 SubExprs[NEXT_CATCH] = NULL;
Daniel Dunbar93b2bdb2009-03-01 04:28:32 +0000397 // FIXME: O(N^2) in number of catch blocks.
Eli Friedman0613c372008-05-25 04:34:57 +0000398 if (atCatchList) {
Ted Kremenekff981022008-02-01 21:28:59 +0000399 ObjCAtCatchStmt *AtCatchList = static_cast<ObjCAtCatchStmt*>(atCatchList);
400
Nico Weber608b17f2008-08-05 23:15:29 +0000401 while (ObjCAtCatchStmt* NextCatch = AtCatchList->getNextCatchStmt())
Ted Kremenekff981022008-02-01 21:28:59 +0000402 AtCatchList = NextCatch;
Nico Weber608b17f2008-08-05 23:15:29 +0000403
Ted Kremenekff981022008-02-01 21:28:59 +0000404 AtCatchList->SubExprs[NEXT_CATCH] = this;
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000405 }
406 AtCatchLoc = atCatchLoc;
407 RParenLoc = rparenloc;
408}
409
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000410CXXTryStmt::CXXTryStmt(ASTContext &C, SourceLocation tryLoc, Stmt *tryBlock,
411 Stmt **handlers, unsigned numHandlers)
412 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
413 Stmts = new (C) Stmt*[NumHandlers + 1];
414 Stmts[0] = tryBlock;
415 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
416}
417
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000418//===----------------------------------------------------------------------===//
419// AST Destruction.
420//===----------------------------------------------------------------------===//
421
422void Stmt::DestroyChildren(ASTContext &C) {
423 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
424 if (Stmt* Child = *I++) Child->Destroy(C);
425}
426
427static void BranchDestroy(ASTContext &C, Stmt *S, Stmt **SubExprs,
428 unsigned NumExprs) {
429 // We do not use child_iterator here because that will include
430 // the expressions referenced by the condition variable.
431 for (Stmt **I = SubExprs, **E = SubExprs + NumExprs; I != E; ++I)
432 if (Stmt *Child = *I) Child->Destroy(C);
433
434 S->~Stmt();
435 C.Deallocate((void *) S);
436}
437
438void Stmt::DoDestroy(ASTContext &C) {
439 DestroyChildren(C);
440 this->~Stmt();
441 C.Deallocate((void *)this);
442}
443
444void CXXCatchStmt::DoDestroy(ASTContext& C) {
445 if (ExceptionDecl)
446 ExceptionDecl->Destroy(C);
447 Stmt::DoDestroy(C);
448}
449
450void DeclStmt::DoDestroy(ASTContext &C) {
451 // Don't use StmtIterator to iterate over the Decls, as that can recurse
452 // into VLA size expressions (which are owned by the VLA). Further, Decls
453 // are owned by the DeclContext, and will be destroyed with them.
454 if (DG.isDeclGroup())
455 DG.getDeclGroup().Destroy(C);
456}
457
458void IfStmt::DoDestroy(ASTContext &C) {
459 BranchDestroy(C, this, SubExprs, END_EXPR);
460}
461
462void ForStmt::DoDestroy(ASTContext &C) {
463 BranchDestroy(C, this, SubExprs, END_EXPR);
464}
465
466void SwitchStmt::DoDestroy(ASTContext &C) {
467 // Destroy the SwitchCase statements in this switch. In the normal
468 // case, this loop will merely decrement the reference counts from
469 // the Retain() calls in addSwitchCase();
470 SwitchCase *SC = FirstCase;
471 while (SC) {
472 SwitchCase *Next = SC->getNextSwitchCase();
473 SC->Destroy(C);
474 SC = Next;
475 }
476
477 BranchDestroy(C, this, SubExprs, END_EXPR);
478}
479
480void WhileStmt::DoDestroy(ASTContext &C) {
481 BranchDestroy(C, this, SubExprs, END_EXPR);
482}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000483
Anders Carlsson966146e2010-01-30 23:19:41 +0000484void AsmStmt::DoDestroy(ASTContext &C) {
485 DestroyChildren(C);
486
487 C.Deallocate(Names);
488 C.Deallocate(Constraints);
489 C.Deallocate(Exprs);
490 C.Deallocate(Clobbers);
491
Benjamin Kramera0531542010-01-31 09:01:55 +0000492 this->~AsmStmt();
Anders Carlsson966146e2010-01-30 23:19:41 +0000493 C.Deallocate((void *)this);
494}
495
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000496void CXXTryStmt::DoDestroy(ASTContext& C) {
497 DestroyChildren(C);
498 C.Deallocate(Stmts);
499
500 this->~CXXTryStmt();
501 C.Deallocate((void *)this);
502}
503
Ted Kremenek82977772007-08-24 21:09:09 +0000504//===----------------------------------------------------------------------===//
505// Child Iterators for iterating over subexpressions/substatements
506//===----------------------------------------------------------------------===//
507
508// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000509Stmt::child_iterator DeclStmt::child_begin() {
510 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000511}
512
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000513Stmt::child_iterator DeclStmt::child_end() {
514 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000515}
516
Ted Kremenek82977772007-08-24 21:09:09 +0000517// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000518Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
519Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000520
521// CompoundStmt
522Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000523Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000524
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000525// CaseStmt
526Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
527Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
528
529// DefaultStmt
530Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
531Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000532
533// LabelStmt
534Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000535Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000536
537// IfStmt
Ted Kremenek35628d12009-12-23 23:38:34 +0000538Stmt::child_iterator IfStmt::child_begin() {
539 return child_iterator(Var, &SubExprs[0]);
540}
541Stmt::child_iterator IfStmt::child_end() {
542 return child_iterator(0, &SubExprs[0]+END_EXPR);
543}
Ted Kremenek82977772007-08-24 21:09:09 +0000544
545// SwitchStmt
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000546Stmt::child_iterator SwitchStmt::child_begin() {
547 return child_iterator(Var, &SubExprs[0]);
548}
549Stmt::child_iterator SwitchStmt::child_end() {
550 return child_iterator(0, &SubExprs[0]+END_EXPR);
551}
Ted Kremenek82977772007-08-24 21:09:09 +0000552
553// WhileStmt
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000554Stmt::child_iterator WhileStmt::child_begin() {
555 return child_iterator(Var, &SubExprs[0]);
556}
557Stmt::child_iterator WhileStmt::child_end() {
558 return child_iterator(0, &SubExprs[0]+END_EXPR);
559}
Ted Kremenek82977772007-08-24 21:09:09 +0000560
561// DoStmt
562Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
563Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
564
565// ForStmt
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000566Stmt::child_iterator ForStmt::child_begin() {
567 return child_iterator(CondVar, &SubExprs[0]);
568}
569Stmt::child_iterator ForStmt::child_end() {
Ted Kremenek62812132009-12-24 01:59:46 +0000570 return child_iterator(0, &SubExprs[0]+END_EXPR);
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000571}
Ted Kremenek82977772007-08-24 21:09:09 +0000572
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000573// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000574Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
575 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000576}
Nico Weber608b17f2008-08-05 23:15:29 +0000577Stmt::child_iterator ObjCForCollectionStmt::child_end() {
578 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000579}
580
Ted Kremenek82977772007-08-24 21:09:09 +0000581// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000582Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
583Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000584
585// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000586Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
587const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000588
Ted Kremenek1060aff2008-06-17 03:11:08 +0000589Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
590Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000591
592// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000593Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
594Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000595
596// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000597Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
598Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000599
600// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000601const Expr* ReturnStmt::getRetValue() const {
602 return cast_or_null<Expr>(RetExpr);
603}
604Expr* ReturnStmt::getRetValue() {
605 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000606}
607
Ted Kremenek1060aff2008-06-17 03:11:08 +0000608Stmt::child_iterator ReturnStmt::child_begin() {
609 return &RetExpr;
610}
611Stmt::child_iterator ReturnStmt::child_end() {
612 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000613}
Ted Kremenek82977772007-08-24 21:09:09 +0000614
Chris Lattnerfe795952007-10-29 04:04:16 +0000615// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000616Stmt::child_iterator AsmStmt::child_begin() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000617 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0];
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000618}
619Stmt::child_iterator AsmStmt::child_end() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000620 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0] + NumOutputs + NumInputs;
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000621}
Chris Lattnerfe795952007-10-29 04:04:16 +0000622
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000623// ObjCAtCatchStmt
624Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000625Stmt::child_iterator ObjCAtCatchStmt::child_end() {
626 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000627}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000628
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000629// ObjCAtFinallyStmt
630Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
631Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000632
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000633// ObjCAtTryStmt
634Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000635Stmt::child_iterator ObjCAtTryStmt::child_end() {
636 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000637}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000638
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000639// ObjCAtThrowStmt
640Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000641 return &Throw;
642}
643
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000644Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000645 return &Throw+1;
646}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000647
648// ObjCAtSynchronizedStmt
649Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000650 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000651}
652
653Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000654 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000655}
656
Sebastian Redl4b07b292008-12-22 19:15:10 +0000657// CXXCatchStmt
658Stmt::child_iterator CXXCatchStmt::child_begin() {
659 return &HandlerBlock;
660}
661
662Stmt::child_iterator CXXCatchStmt::child_end() {
663 return &HandlerBlock + 1;
664}
665
Sebastian Redl8351da02008-12-22 21:35:02 +0000666// CXXTryStmt
667Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000668Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+NumHandlers+1; }