blob: 7c69a793645bcbbf1042c608968cef8bc900cd60 [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
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000454CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
455 unsigned numHandlers) {
456 std::size_t Size = sizeof(CXXTryStmt);
457 Size += ((numHandlers + 1) * sizeof(Stmt));
458
459 void *Mem = C.Allocate(Size, llvm::alignof<CXXTryStmt>());
460 return new (Mem) CXXTryStmt(Empty, numHandlers);
461}
462
Sam Weiniga1a396d2010-02-03 03:56:39 +0000463CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000464 Stmt **handlers, unsigned numHandlers)
465 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000466 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000467 Stmts[0] = tryBlock;
468 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
469}
470
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000471IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
472 Stmt *then, SourceLocation EL, Stmt *elsev)
473 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
474{
475 setConditionVariable(C, var);
476 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
477 SubExprs[THEN] = then;
478 SubExprs[ELSE] = elsev;
479}
480
481VarDecl *IfStmt::getConditionVariable() const {
482 if (!SubExprs[VAR])
483 return 0;
484
485 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
486 return cast<VarDecl>(DS->getSingleDecl());
487}
488
489void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
490 if (!V) {
491 SubExprs[VAR] = 0;
492 return;
493 }
494
495 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
496 V->getSourceRange().getBegin(),
497 V->getSourceRange().getEnd());
498}
499
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000500ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
501 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
502 SourceLocation RP)
503 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
504{
505 SubExprs[INIT] = Init;
506 setConditionVariable(C, condVar);
507 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
508 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
509 SubExprs[BODY] = Body;
510}
511
512VarDecl *ForStmt::getConditionVariable() const {
513 if (!SubExprs[CONDVAR])
514 return 0;
515
516 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
517 return cast<VarDecl>(DS->getSingleDecl());
518}
519
520void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
521 if (!V) {
522 SubExprs[CONDVAR] = 0;
523 return;
524 }
525
526 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V),
527 V->getSourceRange().getBegin(),
528 V->getSourceRange().getEnd());
529}
530
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000531SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
532 : Stmt(SwitchStmtClass), FirstCase(0)
533{
534 setConditionVariable(C, Var);
535 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
536 SubExprs[BODY] = NULL;
537}
538
539VarDecl *SwitchStmt::getConditionVariable() const {
540 if (!SubExprs[VAR])
541 return 0;
542
543 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
544 return cast<VarDecl>(DS->getSingleDecl());
545}
546
547void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
548 if (!V) {
549 SubExprs[VAR] = 0;
550 return;
551 }
552
553 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
554 V->getSourceRange().getBegin(),
555 V->getSourceRange().getEnd());
556}
557
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000558WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
559 SourceLocation WL)
560: Stmt(WhileStmtClass)
561{
562 setConditionVariable(C, Var);
563 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
564 SubExprs[BODY] = body;
565 WhileLoc = WL;
566}
567
568VarDecl *WhileStmt::getConditionVariable() const {
569 if (!SubExprs[VAR])
570 return 0;
571
572 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
573 return cast<VarDecl>(DS->getSingleDecl());
574}
575
576void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
577 if (!V) {
578 SubExprs[VAR] = 0;
579 return;
580 }
581
582 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
583 V->getSourceRange().getBegin(),
584 V->getSourceRange().getEnd());
585}
586
Ted Kremenek82977772007-08-24 21:09:09 +0000587//===----------------------------------------------------------------------===//
588// Child Iterators for iterating over subexpressions/substatements
589//===----------------------------------------------------------------------===//
590
591// DeclStmt
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000592Stmt::child_iterator DeclStmt::child_begin() {
593 return StmtIterator(DG.begin(), DG.end());
Ted Kremenek14f8b4f2008-08-05 20:46:55 +0000594}
595
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000596Stmt::child_iterator DeclStmt::child_end() {
597 return StmtIterator(DG.end(), DG.end());
Ted Kremenek65aa3b92008-10-06 20:54:44 +0000598}
599
Ted Kremenek82977772007-08-24 21:09:09 +0000600// NullStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000601Stmt::child_iterator NullStmt::child_begin() { return child_iterator(); }
602Stmt::child_iterator NullStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000603
604// CompoundStmt
605Stmt::child_iterator CompoundStmt::child_begin() { return &Body[0]; }
Ted Kremenek8189cde2009-02-07 01:47:29 +0000606Stmt::child_iterator CompoundStmt::child_end() { return &Body[0]+NumStmts; }
Ted Kremenek82977772007-08-24 21:09:09 +0000607
Ted Kremenekd97bb6c2007-08-30 16:50:46 +0000608// CaseStmt
609Stmt::child_iterator CaseStmt::child_begin() { return &SubExprs[0]; }
610Stmt::child_iterator CaseStmt::child_end() { return &SubExprs[END_EXPR]; }
611
612// DefaultStmt
613Stmt::child_iterator DefaultStmt::child_begin() { return &SubStmt; }
614Stmt::child_iterator DefaultStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000615
616// LabelStmt
617Stmt::child_iterator LabelStmt::child_begin() { return &SubStmt; }
Chris Lattnerb3938792007-08-30 00:53:54 +0000618Stmt::child_iterator LabelStmt::child_end() { return &SubStmt+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000619
620// IfStmt
Ted Kremenek35628d12009-12-23 23:38:34 +0000621Stmt::child_iterator IfStmt::child_begin() {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000622 return &SubExprs[0];
Ted Kremenek35628d12009-12-23 23:38:34 +0000623}
624Stmt::child_iterator IfStmt::child_end() {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000625 return &SubExprs[0]+END_EXPR;
Ted Kremenek35628d12009-12-23 23:38:34 +0000626}
Ted Kremenek82977772007-08-24 21:09:09 +0000627
628// SwitchStmt
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000629Stmt::child_iterator SwitchStmt::child_begin() {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000630 return &SubExprs[0];
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000631}
632Stmt::child_iterator SwitchStmt::child_end() {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000633 return &SubExprs[0]+END_EXPR;
Ted Kremeneka3be0ea2009-12-24 00:39:05 +0000634}
Ted Kremenek82977772007-08-24 21:09:09 +0000635
636// WhileStmt
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000637Stmt::child_iterator WhileStmt::child_begin() {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000638 return &SubExprs[0];
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000639}
640Stmt::child_iterator WhileStmt::child_end() {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000641 return &SubExprs[0]+END_EXPR;
Ted Kremenek7d02b8c2009-12-24 00:54:19 +0000642}
Ted Kremenek82977772007-08-24 21:09:09 +0000643
644// DoStmt
645Stmt::child_iterator DoStmt::child_begin() { return &SubExprs[0]; }
646Stmt::child_iterator DoStmt::child_end() { return &SubExprs[0]+END_EXPR; }
647
648// ForStmt
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000649Stmt::child_iterator ForStmt::child_begin() {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000650 return &SubExprs[0];
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000651}
652Stmt::child_iterator ForStmt::child_end() {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000653 return &SubExprs[0]+END_EXPR;
Ted Kremenekf0d975f2009-12-24 01:48:39 +0000654}
Ted Kremenek82977772007-08-24 21:09:09 +0000655
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000656// ObjCForCollectionStmt
Nico Weber608b17f2008-08-05 23:15:29 +0000657Stmt::child_iterator ObjCForCollectionStmt::child_begin() {
658 return &SubExprs[0];
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000659}
Nico Weber608b17f2008-08-05 23:15:29 +0000660Stmt::child_iterator ObjCForCollectionStmt::child_end() {
661 return &SubExprs[0]+END_EXPR;
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000662}
663
Ted Kremenek82977772007-08-24 21:09:09 +0000664// GotoStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000665Stmt::child_iterator GotoStmt::child_begin() { return child_iterator(); }
666Stmt::child_iterator GotoStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000667
668// IndirectGotoStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000669Expr* IndirectGotoStmt::getTarget() { return cast<Expr>(Target); }
670const Expr* IndirectGotoStmt::getTarget() const { return cast<Expr>(Target); }
Ted Kremenek82977772007-08-24 21:09:09 +0000671
Ted Kremenek1060aff2008-06-17 03:11:08 +0000672Stmt::child_iterator IndirectGotoStmt::child_begin() { return &Target; }
673Stmt::child_iterator IndirectGotoStmt::child_end() { return &Target+1; }
Ted Kremenek82977772007-08-24 21:09:09 +0000674
675// ContinueStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000676Stmt::child_iterator ContinueStmt::child_begin() { return child_iterator(); }
677Stmt::child_iterator ContinueStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000678
679// BreakStmt
Ted Kremenek9ac59282007-10-18 23:28:49 +0000680Stmt::child_iterator BreakStmt::child_begin() { return child_iterator(); }
681Stmt::child_iterator BreakStmt::child_end() { return child_iterator(); }
Ted Kremenek82977772007-08-24 21:09:09 +0000682
683// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000684const Expr* ReturnStmt::getRetValue() const {
685 return cast_or_null<Expr>(RetExpr);
686}
687Expr* ReturnStmt::getRetValue() {
688 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000689}
690
Ted Kremenek1060aff2008-06-17 03:11:08 +0000691Stmt::child_iterator ReturnStmt::child_begin() {
692 return &RetExpr;
693}
694Stmt::child_iterator ReturnStmt::child_end() {
695 return RetExpr ? &RetExpr+1 : &RetExpr;
Ted Kremenek2298f912007-08-27 20:58:16 +0000696}
Ted Kremenek82977772007-08-24 21:09:09 +0000697
Chris Lattnerfe795952007-10-29 04:04:16 +0000698// AsmStmt
Mike Stump1eb44332009-09-09 15:08:12 +0000699Stmt::child_iterator AsmStmt::child_begin() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000700 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0];
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000701}
702Stmt::child_iterator AsmStmt::child_end() {
Anders Carlsson966146e2010-01-30 23:19:41 +0000703 return NumOutputs + NumInputs == 0 ? 0 : &Exprs[0] + NumOutputs + NumInputs;
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000704}
Chris Lattnerfe795952007-10-29 04:04:16 +0000705
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000706// ObjCAtCatchStmt
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000707Stmt::child_iterator ObjCAtCatchStmt::child_begin() { return &Body; }
708Stmt::child_iterator ObjCAtCatchStmt::child_end() { return &Body + 1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000709
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710// ObjCAtFinallyStmt
711Stmt::child_iterator ObjCAtFinallyStmt::child_begin() { return &AtFinallyStmt; }
712Stmt::child_iterator ObjCAtFinallyStmt::child_end() { return &AtFinallyStmt+1; }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000713
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000714// ObjCAtTryStmt
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000715Stmt::child_iterator ObjCAtTryStmt::child_begin() { return getStmts(); }
716
717Stmt::child_iterator ObjCAtTryStmt::child_end() {
718 return getStmts() + 1 + NumCatchStmts + HasFinally;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +0000719}
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000720
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000721// ObjCAtThrowStmt
722Stmt::child_iterator ObjCAtThrowStmt::child_begin() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000723 return &Throw;
724}
725
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000726Stmt::child_iterator ObjCAtThrowStmt::child_end() {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000727 return &Throw+1;
728}
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000729
730// ObjCAtSynchronizedStmt
731Stmt::child_iterator ObjCAtSynchronizedStmt::child_begin() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000732 return &SubStmts[0];
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000733}
734
735Stmt::child_iterator ObjCAtSynchronizedStmt::child_end() {
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000736 return &SubStmts[0]+END_EXPR;
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +0000737}
738
Sebastian Redl4b07b292008-12-22 19:15:10 +0000739// CXXCatchStmt
740Stmt::child_iterator CXXCatchStmt::child_begin() {
741 return &HandlerBlock;
742}
743
744Stmt::child_iterator CXXCatchStmt::child_end() {
745 return &HandlerBlock + 1;
746}
747
Sebastian Redl8351da02008-12-22 21:35:02 +0000748// CXXTryStmt
Sam Weiniga1a396d2010-02-03 03:56:39 +0000749Stmt::child_iterator CXXTryStmt::child_begin() {
750 return reinterpret_cast<Stmt **>(this + 1);
751}
752
753Stmt::child_iterator CXXTryStmt::child_end() {
754 return reinterpret_cast<Stmt **>(this + 1) + NumHandlers + 1;
755}