blob: 8347249a466bd595f3d14e0b23fe75e24d75ba7b [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 Weiniga1a396d2010-02-03 03:56:39 +0000410CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
411 Stmt *tryBlock, Stmt **handlers,
412 unsigned numHandlers) {
413 std::size_t Size = sizeof(CXXTryStmt);
414 Size += ((numHandlers + 1) * sizeof(Stmt));
415
416 void *Mem = C.Allocate(Size, llvm::alignof<CXXTryStmt>());
417 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
418}
419
420CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000421 Stmt **handlers, unsigned numHandlers)
422 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000423 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000424 Stmts[0] = tryBlock;
425 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
426}
427
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000428//===----------------------------------------------------------------------===//
429// AST Destruction.
430//===----------------------------------------------------------------------===//
431
432void Stmt::DestroyChildren(ASTContext &C) {
433 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
434 if (Stmt* Child = *I++) Child->Destroy(C);
435}
436
437static void BranchDestroy(ASTContext &C, Stmt *S, Stmt **SubExprs,
438 unsigned NumExprs) {
439 // We do not use child_iterator here because that will include
440 // the expressions referenced by the condition variable.
441 for (Stmt **I = SubExprs, **E = SubExprs + NumExprs; I != E; ++I)
442 if (Stmt *Child = *I) Child->Destroy(C);
443
444 S->~Stmt();
445 C.Deallocate((void *) S);
446}
447
448void Stmt::DoDestroy(ASTContext &C) {
449 DestroyChildren(C);
450 this->~Stmt();
451 C.Deallocate((void *)this);
452}
453
454void CXXCatchStmt::DoDestroy(ASTContext& C) {
455 if (ExceptionDecl)
456 ExceptionDecl->Destroy(C);
457 Stmt::DoDestroy(C);
458}
459
460void DeclStmt::DoDestroy(ASTContext &C) {
461 // Don't use StmtIterator to iterate over the Decls, as that can recurse
462 // into VLA size expressions (which are owned by the VLA). Further, Decls
463 // are owned by the DeclContext, and will be destroyed with them.
464 if (DG.isDeclGroup())
465 DG.getDeclGroup().Destroy(C);
466}
467
468void IfStmt::DoDestroy(ASTContext &C) {
469 BranchDestroy(C, this, SubExprs, END_EXPR);
470}
471
472void ForStmt::DoDestroy(ASTContext &C) {
473 BranchDestroy(C, this, SubExprs, END_EXPR);
474}
475
476void SwitchStmt::DoDestroy(ASTContext &C) {
477 // Destroy the SwitchCase statements in this switch. In the normal
478 // case, this loop will merely decrement the reference counts from
479 // the Retain() calls in addSwitchCase();
480 SwitchCase *SC = FirstCase;
481 while (SC) {
482 SwitchCase *Next = SC->getNextSwitchCase();
483 SC->Destroy(C);
484 SC = Next;
485 }
486
487 BranchDestroy(C, this, SubExprs, END_EXPR);
488}
489
490void WhileStmt::DoDestroy(ASTContext &C) {
491 BranchDestroy(C, this, SubExprs, END_EXPR);
492}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000493
Anders Carlsson966146e2010-01-30 23:19:41 +0000494void AsmStmt::DoDestroy(ASTContext &C) {
495 DestroyChildren(C);
496
497 C.Deallocate(Names);
498 C.Deallocate(Constraints);
499 C.Deallocate(Exprs);
500 C.Deallocate(Clobbers);
501
Benjamin Kramera0531542010-01-31 09:01:55 +0000502 this->~AsmStmt();
Anders Carlsson966146e2010-01-30 23:19:41 +0000503 C.Deallocate((void *)this);
504}
505
Ted Kremenek82977772007-08-24 21:09:09 +0000506//===----------------------------------------------------------------------===//
507// Child Iterators for iterating over subexpressions/substatements
508//===----------------------------------------------------------------------===//
509
510// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000511Stmt::child_iterator DeclStmt::child_begin() {
512 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000513}
514
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000515Stmt::child_iterator DeclStmt::child_end() {
516 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000517}
518
Ted Kremenek82977772007-08-24 21:09:09 +0000519// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000520Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
521Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000522
523// CompoundStmt
524Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000525Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000526
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000527// CaseStmt
528Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
529Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
530
531// DefaultStmt
532Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
533Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000534
535// LabelStmt
536Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000537Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000538
539// IfStmt
Ted Kremenek35628d12009-12-23 23:38:34 +0000540Stmt::child_iterator IfStmt::child_begin() {
541 return child_iterator(Var, &SubExprs[0]);
542}
543Stmt::child_iterator IfStmt::child_end() {
544 return child_iterator(0, &SubExprs[0]+END_EXPR);
545}
Ted Kremenek82977772007-08-24 21:09:09 +0000546
547// SwitchStmt
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000548Stmt::child_iterator SwitchStmt::child_begin() {
549 return child_iterator(Var, &SubExprs[0]);
550}
551Stmt::child_iterator SwitchStmt::child_end() {
552 return child_iterator(0, &SubExprs[0]+END_EXPR);
553}
Ted Kremenek82977772007-08-24 21:09:09 +0000554
555// WhileStmt
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000556Stmt::child_iterator WhileStmt::child_begin() {
557 return child_iterator(Var, &SubExprs[0]);
558}
559Stmt::child_iterator WhileStmt::child_end() {
560 return child_iterator(0, &SubExprs[0]+END_EXPR);
561}
Ted Kremenek82977772007-08-24 21:09:09 +0000562
563// DoStmt
564Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
565Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
566
567// ForStmt
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000568Stmt::child_iterator ForStmt::child_begin() {
569 return child_iterator(CondVar, &SubExprs[0]);
570}
571Stmt::child_iterator ForStmt::child_end() {
Ted Kremenek62812132009-12-24 01:59:46 +0000572 return child_iterator(0, &SubExprs[0]+END_EXPR);
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000573}
Ted Kremenek82977772007-08-24 21:09:09 +0000574
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000575// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000576Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
577 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000578}
Nico Weber608b17f2008-08-05 23:15:29 +0000579Stmt::child_iterator ObjCForCollectionStmt::child_end() {
580 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000581}
582
Ted Kremenek82977772007-08-24 21:09:09 +0000583// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000584Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
585Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000586
587// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000588Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
589const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000590
Ted Kremenek1060aff2008-06-17 03:11:08 +0000591Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
592Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000593
594// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000595Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
596Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000597
598// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000599Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
600Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000601
602// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000603const Expr* ReturnStmt::getRetValue() const {
604 return cast_or_null<Expr>(RetExpr);
605}
606Expr* ReturnStmt::getRetValue() {
607 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000608}
609
Ted Kremenek1060aff2008-06-17 03:11:08 +0000610Stmt::child_iterator ReturnStmt::child_begin() {
611 return &RetExpr;
612}
613Stmt::child_iterator ReturnStmt::child_end() {
614 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000615}
Ted Kremenek82977772007-08-24 21:09:09 +0000616
Chris Lattnerfe795952007-10-29 04:04:16 +0000617// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000618Stmt::child_iterator AsmStmt::child_begin() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000619 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0];
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000620}
621Stmt::child_iterator AsmStmt::child_end() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000622 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0] + NumOutputs + NumInputs;
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000623}
Chris Lattnerfe795952007-10-29 04:04:16 +0000624
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625// ObjCAtCatchStmt
626Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &SubExprs[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000627Stmt::child_iterator ObjCAtCatchStmt::child_end() {
628 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000629}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000630
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000631// ObjCAtFinallyStmt
632Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
633Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000634
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635// ObjCAtTryStmt
636Stmt::child_iterator ObjCAtTryStmt::child_begin() { return &SubStmts[0]; }
Nico Weber608b17f2008-08-05 23:15:29 +0000637Stmt::child_iterator ObjCAtTryStmt::child_end() {
638 return &SubStmts[0]+END_EXPR;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000639}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000640
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000641// ObjCAtThrowStmt
642Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000643 return &Throw;
644}
645
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000646Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000647 return &Throw+1;
648}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000649
650// ObjCAtSynchronizedStmt
651Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000652 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000653}
654
655Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000656 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000657}
658
Sebastian Redl4b07b292008-12-22 19:15:10 +0000659// CXXCatchStmt
660Stmt::child_iterator CXXCatchStmt::child_begin() {
661 return &HandlerBlock;
662}
663
664Stmt::child_iterator CXXCatchStmt::child_end() {
665 return &HandlerBlock + 1;
666}
667
Sebastian Redl8351da02008-12-22 21:35:02 +0000668// CXXTryStmt
Sam Weiniga1a396d2010-02-03 03:56:39 +0000669Stmt::child_iterator CXXTryStmt::child_begin() {
670 return reinterpret_cast<Stmt **>(this + 1);
671}
672
673Stmt::child_iterator CXXTryStmt::child_end() {
674 return reinterpret_cast<Stmt **>(this + 1) + NumHandlers + 1;
675}