blob: 80f5695e42ad8495b177b8fd0449d1797852a577 [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"
Chris Lattner9bffb072010-04-23 16:29:58 +000022#include "clang/Basic/TargetInfo.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000023#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Reid Spencer5f016e22007-07-11 17:01:13 +000026static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000027 const char *Name;
28 unsigned Counter;
29 unsigned Size;
Sean Hunt4bfe1962010-05-05 15:24:00 +000030} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000031
32static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
33 static bool Initialized = false;
34 if (Initialized)
35 return StmtClassInfo[E];
36
37 // Intialize the table on the first use.
38 Initialized = true;
Sean Hunt7381d5c2010-05-18 06:22:21 +000039#define ABSTRACT_STMT(STMT)
Douglas Gregorf2cad862008-11-14 12:46:07 +000040#define STMT(CLASS, PARENT) \
41 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
42 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Sean Hunt4bfe1962010-05-05 15:24:00 +000043#include "clang/AST/StmtNodes.inc"
Nico Weber608b17f2008-08-05 23:15:29 +000044
Chris Lattner63381352007-08-25 01:42:24 +000045 return StmtClassInfo[E];
46}
47
Reid Spencer5f016e22007-07-11 17:01:13 +000048const char *Stmt::getStmtClassName() const {
Douglas Gregor43d9d922009-08-08 01:41:12 +000049 return getStmtInfoTableEntry((StmtClass)sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000050}
51
52void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000053 // Ensure the table is primed.
54 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000055
Reid Spencer5f016e22007-07-11 17:01:13 +000056 unsigned sum = 0;
57 fprintf(stderr, "*** Stmt/Expr Stats:\n");
Sean Hunt4bfe1962010-05-05 15:24:00 +000058 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000059 if (StmtClassInfo[i].Name == 0) continue;
60 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 }
62 fprintf(stderr, " %d stmts/exprs total.\n", sum);
63 sum = 0;
Sean Hunt4bfe1962010-05-05 15:24:00 +000064 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000065 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000066 if (StmtClassInfo[i].Counter == 0) continue;
Nico Weber608b17f2008-08-05 23:15:29 +000067 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000068 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
69 StmtClassInfo[i].Size,
70 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
71 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
73 fprintf(stderr, "Total bytes = %d\n", sum);
74}
75
76void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000077 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000078}
79
80static bool StatSwitch = false;
81
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000082bool Stmt::CollectingStats(bool Enable) {
83 if (Enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000084 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000085}
86
Douglas Gregor025452f2009-04-17 00:04:06 +000087void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
88 if (this->Body)
89 C.Deallocate(Body);
90 this->NumStmts = NumStmts;
91
92 Body = new (C) Stmt*[NumStmts];
93 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
94}
Reid Spencer5f016e22007-07-11 17:01:13 +000095
Reid Spencer5f016e22007-07-11 17:01:13 +000096const char *LabelStmt::getName() const {
Daniel Dunbare013d682009-10-18 20:26:12 +000097 return getID()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +000098}
99
Steve Naroff507f2d52007-08-31 23:49:30 +0000100// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000101SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000102 if (RetExpr)
103 return SourceRange(RetLoc, RetExpr->getLocEnd());
104 else
105 return SourceRange(RetLoc);
106}
107
Ted Kremenekd48ade62007-10-01 16:34:52 +0000108bool Stmt::hasImplicitControlFlow() const {
109 switch (sClass) {
110 default:
111 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000112
Ted Kremenekd48ade62007-10-01 16:34:52 +0000113 case CallExprClass:
114 case ConditionalOperatorClass:
115 case ChooseExprClass:
116 case StmtExprClass:
117 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000118 return true;
119
Ted Kremenekd48ade62007-10-01 16:34:52 +0000120 case Stmt::BinaryOperatorClass: {
121 const BinaryOperator* B = cast<BinaryOperator>(this);
122 if (B->isLogicalOp() || B->getOpcode() == BinaryOperator::Comma)
123 return true;
124 else
125 return false;
126 }
127 }
128}
129
Chris Lattnerb3277932009-03-10 04:59:06 +0000130Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000131 return cast<Expr>(Exprs[i]);
132}
Chris Lattnerb3277932009-03-10 04:59:06 +0000133
134/// getOutputConstraint - Return the constraint string for the specified
135/// output operand. All output constraints are known to be non-empty (either
136/// '=' or '+').
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000137llvm::StringRef AsmStmt::getOutputConstraint(unsigned i) const {
138 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000139}
Chris Lattnerb3277932009-03-10 04:59:06 +0000140
Chris Lattner85759272009-03-11 00:23:13 +0000141/// getNumPlusOperands - Return the number of output operands that have a "+"
142/// constraint.
143unsigned AsmStmt::getNumPlusOperands() const {
144 unsigned Res = 0;
145 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
146 if (isOutputPlusConstraint(i))
147 ++Res;
148 return Res;
149}
150
Chris Lattnerb3277932009-03-10 04:59:06 +0000151Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000152 return cast<Expr>(Exprs[i + NumOutputs]);
153}
Chris Lattnerb3277932009-03-10 04:59:06 +0000154
155/// getInputConstraint - Return the specified input constraint. Unlike output
156/// constraints, these can be empty.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000157llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
158 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000159}
160
Chris Lattner10ca96a2009-03-10 06:33:24 +0000161
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000162void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000163 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000164 StringLiteral **Constraints,
165 Stmt **Exprs,
166 unsigned NumOutputs,
Sean Huntc3021132010-05-05 15:23:54 +0000167 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000168 StringLiteral **Clobbers,
169 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000170 this->NumOutputs = NumOutputs;
171 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000172 this->NumClobbers = NumClobbers;
173
174 unsigned NumExprs = NumOutputs + NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000175
Anders Carlsson966146e2010-01-30 23:19:41 +0000176 C.Deallocate(this->Names);
177 this->Names = new (C) IdentifierInfo*[NumExprs];
178 std::copy(Names, Names + NumExprs, this->Names);
Sean Huntc3021132010-05-05 15:23:54 +0000179
Anders Carlsson966146e2010-01-30 23:19:41 +0000180 C.Deallocate(this->Exprs);
181 this->Exprs = new (C) Stmt*[NumExprs];
182 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Sean Huntc3021132010-05-05 15:23:54 +0000183
Anders Carlsson966146e2010-01-30 23:19:41 +0000184 C.Deallocate(this->Constraints);
185 this->Constraints = new (C) StringLiteral*[NumExprs];
186 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Sean Huntc3021132010-05-05 15:23:54 +0000187
Anders Carlsson966146e2010-01-30 23:19:41 +0000188 C.Deallocate(this->Clobbers);
189 this->Clobbers = new (C) StringLiteral*[NumClobbers];
190 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000191}
192
Chris Lattner10ca96a2009-03-10 06:33:24 +0000193/// getNamedOperand - Given a symbolic operand reference like %[foo],
194/// translate this into a numeric value needed to reference the same operand.
195/// This returns -1 if the operand name is invalid.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000196int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000197 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Chris Lattner10ca96a2009-03-10 06:33:24 +0000199 // Check if this is an output operand.
200 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
201 if (getOutputName(i) == SymbolicName)
202 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattner10ca96a2009-03-10 06:33:24 +0000205 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
206 if (getInputName(i) == SymbolicName)
207 return getNumOutputs() + NumPlusOperands + i;
208
209 // Not found.
210 return -1;
211}
212
Chris Lattner458cd9c2009-03-10 23:21:44 +0000213/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
214/// it into pieces. If the asm string is erroneous, emit errors and return
215/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000216unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
217 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000218 const char *StrStart = getAsmString()->getStrData();
219 const char *StrEnd = StrStart + getAsmString()->getByteLength();
Chris Lattner3182db12009-03-10 23:51:40 +0000220 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattner458cd9c2009-03-10 23:21:44 +0000222 // "Simple" inline asms have no constraints or operands, just convert the asm
223 // string to escape $'s.
224 if (isSimple()) {
225 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000226 for (; CurPtr != StrEnd; ++CurPtr) {
227 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000228 case '$':
229 Result += "$$";
230 break;
231 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000232 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000233 break;
234 }
235 }
236 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000237 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000238 }
239
240 // CurStringPiece - The current string that we are building up as we scan the
241 // asm string.
242 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattner9bffb072010-04-23 16:29:58 +0000244 bool HasVariants = !C.Target.hasNoAsmVariants();
Sean Huntc3021132010-05-05 15:23:54 +0000245
Chris Lattner458cd9c2009-03-10 23:21:44 +0000246 while (1) {
247 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000248 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000249 if (!CurStringPiece.empty())
250 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000251 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000252 }
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Chris Lattner3182db12009-03-10 23:51:40 +0000254 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000255 switch (CurChar) {
256 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000257 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
258 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
259 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000260 case '%':
261 break;
262 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000263 CurStringPiece += CurChar;
264 continue;
265 }
Sean Huntc3021132010-05-05 15:23:54 +0000266
Chris Lattner458cd9c2009-03-10 23:21:44 +0000267 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000268 if (CurPtr == StrEnd) {
269 // % at end of string is invalid (no escape).
270 DiagOffs = CurPtr-StrStart-1;
271 return diag::err_asm_invalid_escape;
272 }
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattner3182db12009-03-10 23:51:40 +0000274 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000275 if (EscapedChar == '%') { // %% -> %
276 // Escaped percentage sign.
277 CurStringPiece += '%';
278 continue;
279 }
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattner458cd9c2009-03-10 23:21:44 +0000281 if (EscapedChar == '=') { // %= -> Generate an unique ID.
282 CurStringPiece += "${:uid}";
283 continue;
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner458cd9c2009-03-10 23:21:44 +0000286 // Otherwise, we have an operand. If we have accumulated a string so far,
287 // add it to the Pieces list.
288 if (!CurStringPiece.empty()) {
289 Pieces.push_back(AsmStringPiece(CurStringPiece));
290 CurStringPiece.clear();
291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattner458cd9c2009-03-10 23:21:44 +0000293 // Handle %x4 and %x[foo] by capturing x as the modifier character.
294 char Modifier = '\0';
295 if (isalpha(EscapedChar)) {
296 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000297 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000298 }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Chris Lattner458cd9c2009-03-10 23:21:44 +0000300 if (isdigit(EscapedChar)) {
301 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000302 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattnercafc2222009-03-11 22:52:17 +0000304 --CurPtr;
305 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000306 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattner85759272009-03-11 00:23:13 +0000308 unsigned NumOperands =
309 getNumOutputs() + getNumPlusOperands() + getNumInputs();
310 if (N >= NumOperands) {
311 DiagOffs = CurPtr-StrStart-1;
312 return diag::err_asm_invalid_operand_number;
313 }
314
Chris Lattner458cd9c2009-03-10 23:21:44 +0000315 Pieces.push_back(AsmStringPiece(N, Modifier));
316 continue;
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Chris Lattner458cd9c2009-03-10 23:21:44 +0000319 // Handle %[foo], a symbolic operand reference.
320 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000321 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000323 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000324 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000325 if (NameEnd == 0)
326 return diag::err_asm_unterminated_symbolic_operand_name;
327 if (NameEnd == CurPtr)
328 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Anders Carlsson95c9ce92010-01-30 20:48:08 +0000330 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Chris Lattner458cd9c2009-03-10 23:21:44 +0000332 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000333 if (N == -1) {
334 // Verify that an operand with that name exists.
335 DiagOffs = CurPtr-StrStart;
336 return diag::err_asm_unknown_symbolic_operand_name;
337 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000338 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000340 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000341 continue;
342 }
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Chris Lattner2ff0f422009-03-10 23:57:07 +0000344 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000345 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000346 }
347}
348
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000349QualType CXXCatchStmt::getCaughtType() const {
350 if (ExceptionDecl)
351 return ExceptionDecl->getType();
352 return QualType();
353}
354
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000355//===----------------------------------------------------------------------===//
356// Constructors
357//===----------------------------------------------------------------------===//
358
Sean Huntc3021132010-05-05 15:23:54 +0000359AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
360 bool isvolatile, bool msasm,
Anders Carlsson966146e2010-01-30 23:19:41 +0000361 unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000362 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000363 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
364 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000365 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000366 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson966146e2010-01-30 23:19:41 +0000367 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000368
Anders Carlsson966146e2010-01-30 23:19:41 +0000369 unsigned NumExprs = NumOutputs +NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000370
Anders Carlsson966146e2010-01-30 23:19:41 +0000371 Names = new (C) IdentifierInfo*[NumExprs];
372 std::copy(names, names + NumExprs, Names);
373
374 Exprs = new (C) Stmt*[NumExprs];
375 std::copy(exprs, exprs + NumExprs, Exprs);
376
377 Constraints = new (C) StringLiteral*[NumExprs];
378 std::copy(constraints, constraints + NumExprs, Constraints);
379
380 Clobbers = new (C) StringLiteral*[NumClobbers];
381 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000382}
383
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000384ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
385 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000386 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000387: Stmt(ObjCForCollectionStmtClass) {
388 SubExprs[ELEM] = Elem;
389 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
390 SubExprs[BODY] = Body;
391 ForLoc = FCL;
392 RParenLoc = RPL;
393}
394
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000395ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
396 Stmt **CatchStmts, unsigned NumCatchStmts,
397 Stmt *atFinallyStmt)
398 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
399 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
400{
401 Stmt **Stmts = getStmts();
402 Stmts[0] = atTryStmt;
403 for (unsigned I = 0; I != NumCatchStmts; ++I)
404 Stmts[I + 1] = CatchStmts[I];
Sean Huntc3021132010-05-05 15:23:54 +0000405
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000406 if (HasFinally)
407 Stmts[NumCatchStmts + 1] = atFinallyStmt;
408}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000409
Sean Huntc3021132010-05-05 15:23:54 +0000410ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
411 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000412 Stmt *atTryStmt,
Sean Huntc3021132010-05-05 15:23:54 +0000413 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000414 unsigned NumCatchStmts,
415 Stmt *atFinallyStmt) {
Sean Huntc3021132010-05-05 15:23:54 +0000416 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000417 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
418 void *Mem = Context.Allocate(Size, llvm::alignof<ObjCAtTryStmt>());
419 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
420 atFinallyStmt);
421}
Ted Kremenekff981022008-02-01 21:28:59 +0000422
Sean Huntc3021132010-05-05 15:23:54 +0000423ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000424 unsigned NumCatchStmts,
425 bool HasFinally) {
Sean Huntc3021132010-05-05 15:23:54 +0000426 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000427 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
428 void *Mem = Context.Allocate(Size, llvm::alignof<ObjCAtTryStmt>());
Sean Huntc3021132010-05-05 15:23:54 +0000429 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000430}
Nico Weber608b17f2008-08-05 23:15:29 +0000431
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000432SourceRange ObjCAtTryStmt::getSourceRange() const {
433 SourceLocation EndLoc;
434 if (HasFinally)
435 EndLoc = getFinallyStmt()->getLocEnd();
436 else if (NumCatchStmts)
437 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
438 else
439 EndLoc = getTryBody()->getLocEnd();
Sean Huntc3021132010-05-05 15:23:54 +0000440
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000441 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000442}
443
Sam Weiniga1a396d2010-02-03 03:56:39 +0000444CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000445 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000446 unsigned numHandlers) {
447 std::size_t Size = sizeof(CXXTryStmt);
448 Size += ((numHandlers + 1) * sizeof(Stmt));
449
450 void *Mem = C.Allocate(Size, llvm::alignof<CXXTryStmt>());
451 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
452}
453
454CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000455 Stmt **handlers, unsigned numHandlers)
456 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000457 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000458 Stmts[0] = tryBlock;
459 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
460}
461
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000462//===----------------------------------------------------------------------===//
463// AST Destruction.
464//===----------------------------------------------------------------------===//
465
466void Stmt::DestroyChildren(ASTContext &C) {
467 for (child_iterator I = child_begin(), E = child_end(); I !=E; )
468 if (Stmt* Child = *I++) Child->Destroy(C);
469}
470
471static void BranchDestroy(ASTContext &C, Stmt *S, Stmt **SubExprs,
472 unsigned NumExprs) {
473 // We do not use child_iterator here because that will include
474 // the expressions referenced by the condition variable.
475 for (Stmt **I = SubExprs, **E = SubExprs + NumExprs; I != E; ++I)
476 if (Stmt *Child = *I) Child->Destroy(C);
Sean Huntc3021132010-05-05 15:23:54 +0000477
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000478 S->~Stmt();
479 C.Deallocate((void *) S);
480}
481
482void Stmt::DoDestroy(ASTContext &C) {
483 DestroyChildren(C);
484 this->~Stmt();
485 C.Deallocate((void *)this);
486}
487
488void CXXCatchStmt::DoDestroy(ASTContext& C) {
489 if (ExceptionDecl)
490 ExceptionDecl->Destroy(C);
491 Stmt::DoDestroy(C);
492}
493
494void DeclStmt::DoDestroy(ASTContext &C) {
495 // Don't use StmtIterator to iterate over the Decls, as that can recurse
496 // into VLA size expressions (which are owned by the VLA). Further, Decls
497 // are owned by the DeclContext, and will be destroyed with them.
498 if (DG.isDeclGroup())
499 DG.getDeclGroup().Destroy(C);
500}
501
502void IfStmt::DoDestroy(ASTContext &C) {
503 BranchDestroy(C, this, SubExprs, END_EXPR);
504}
505
506void ForStmt::DoDestroy(ASTContext &C) {
507 BranchDestroy(C, this, SubExprs, END_EXPR);
508}
509
510void SwitchStmt::DoDestroy(ASTContext &C) {
511 // Destroy the SwitchCase statements in this switch. In the normal
512 // case, this loop will merely decrement the reference counts from
513 // the Retain() calls in addSwitchCase();
514 SwitchCase *SC = FirstCase;
515 while (SC) {
516 SwitchCase *Next = SC->getNextSwitchCase();
517 SC->Destroy(C);
518 SC = Next;
519 }
Sean Huntc3021132010-05-05 15:23:54 +0000520
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000521 BranchDestroy(C, this, SubExprs, END_EXPR);
522}
523
524void WhileStmt::DoDestroy(ASTContext &C) {
525 BranchDestroy(C, this, SubExprs, END_EXPR);
526}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000527
Anders Carlsson966146e2010-01-30 23:19:41 +0000528void AsmStmt::DoDestroy(ASTContext &C) {
529 DestroyChildren(C);
Sean Huntc3021132010-05-05 15:23:54 +0000530
Anders Carlsson966146e2010-01-30 23:19:41 +0000531 C.Deallocate(Names);
532 C.Deallocate(Constraints);
533 C.Deallocate(Exprs);
534 C.Deallocate(Clobbers);
Sean Huntc3021132010-05-05 15:23:54 +0000535
Benjamin Kramera0531542010-01-31 09:01:55 +0000536 this->~AsmStmt();
Anders Carlsson966146e2010-01-30 23:19:41 +0000537 C.Deallocate((void *)this);
538}
539
Ted Kremenek82977772007-08-24 21:09:09 +0000540//===----------------------------------------------------------------------===//
541// Child Iterators for iterating over subexpressions/substatements
542//===----------------------------------------------------------------------===//
543
544// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000545Stmt::child_iterator DeclStmt::child_begin() {
546 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000547}
548
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000549Stmt::child_iterator DeclStmt::child_end() {
550 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000551}
552
Ted Kremenek82977772007-08-24 21:09:09 +0000553// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000554Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
555Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000556
557// CompoundStmt
558Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000559Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000560
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000561// CaseStmt
562Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
563Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
564
565// DefaultStmt
566Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
567Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000568
569// LabelStmt
570Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000571Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000572
573// IfStmt
Ted Kremenek35628d12009-12-23 23:38:34 +0000574Stmt::child_iterator IfStmt::child_begin() {
575 return child_iterator(Var, &SubExprs[0]);
576}
577Stmt::child_iterator IfStmt::child_end() {
578 return child_iterator(0, &SubExprs[0]+END_EXPR);
579}
Ted Kremenek82977772007-08-24 21:09:09 +0000580
581// SwitchStmt
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000582Stmt::child_iterator SwitchStmt::child_begin() {
583 return child_iterator(Var, &SubExprs[0]);
584}
585Stmt::child_iterator SwitchStmt::child_end() {
586 return child_iterator(0, &SubExprs[0]+END_EXPR);
587}
Ted Kremenek82977772007-08-24 21:09:09 +0000588
589// WhileStmt
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000590Stmt::child_iterator WhileStmt::child_begin() {
591 return child_iterator(Var, &SubExprs[0]);
592}
593Stmt::child_iterator WhileStmt::child_end() {
594 return child_iterator(0, &SubExprs[0]+END_EXPR);
595}
Ted Kremenek82977772007-08-24 21:09:09 +0000596
597// DoStmt
598Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
599Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
600
601// ForStmt
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000602Stmt::child_iterator ForStmt::child_begin() {
603 return child_iterator(CondVar, &SubExprs[0]);
604}
605Stmt::child_iterator ForStmt::child_end() {
Ted Kremenek62812132009-12-24 01:59:46 +0000606 return child_iterator(0, &SubExprs[0]+END_EXPR);
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000607}
Ted Kremenek82977772007-08-24 21:09:09 +0000608
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000609// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000610Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
611 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000612}
Nico Weber608b17f2008-08-05 23:15:29 +0000613Stmt::child_iterator ObjCForCollectionStmt::child_end() {
614 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000615}
616
Ted Kremenek82977772007-08-24 21:09:09 +0000617// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000618Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
619Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000620
621// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000622Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
623const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000624
Ted Kremenek1060aff2008-06-17 03:11:08 +0000625Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
626Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000627
628// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000629Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
630Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000631
632// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000633Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
634Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000635
636// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000637const Expr* ReturnStmt::getRetValue() const {
638 return cast_or_null<Expr>(RetExpr);
639}
640Expr* ReturnStmt::getRetValue() {
641 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000642}
643
Ted Kremenek1060aff2008-06-17 03:11:08 +0000644Stmt::child_iterator ReturnStmt::child_begin() {
645 return &RetExpr;
646}
647Stmt::child_iterator ReturnStmt::child_end() {
648 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000649}
Ted Kremenek82977772007-08-24 21:09:09 +0000650
Chris Lattnerfe795952007-10-29 04:04:16 +0000651// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000652Stmt::child_iterator AsmStmt::child_begin() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000653 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0];
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000654}
655Stmt::child_iterator AsmStmt::child_end() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000656 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0] + NumOutputs + NumInputs;
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000657}
Chris Lattnerfe795952007-10-29 04:04:16 +0000658
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000659// ObjCAtCatchStmt
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000660Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &Body; }
661Stmt::child_iterator ObjCAtCatchStmt::child_end() { return &Body + 1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000662
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000663// ObjCAtFinallyStmt
664Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
665Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000666
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000667// ObjCAtTryStmt
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000668Stmt::child_iterator ObjCAtTryStmt::child_begin() { return getStmts(); }
669
670Stmt::child_iterator ObjCAtTryStmt::child_end() {
671 return getStmts() + 1 + NumCatchStmts + HasFinally;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000672}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000673
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000674// ObjCAtThrowStmt
675Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000676 return &Throw;
677}
678
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000679Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000680 return &Throw+1;
681}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000682
683// ObjCAtSynchronizedStmt
684Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000685 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000686}
687
688Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000689 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000690}
691
Sebastian Redl4b07b292008-12-22 19:15:10 +0000692// CXXCatchStmt
693Stmt::child_iterator CXXCatchStmt::child_begin() {
694 return &HandlerBlock;
695}
696
697Stmt::child_iterator CXXCatchStmt::child_end() {
698 return &HandlerBlock + 1;
699}
700
Sebastian Redl8351da02008-12-22 21:35:02 +0000701// CXXTryStmt
Sam Weiniga1a396d2010-02-03 03:56:39 +0000702Stmt::child_iterator CXXTryStmt::child_begin() {
703 return reinterpret_cast<Stmt **>(this + 1);
704}
705
706Stmt::child_iterator CXXTryStmt::child_end() {
707 return reinterpret_cast<Stmt **>(this + 1) + NumHandlers + 1;
708}