blob: fc66202c4aa7a366ff66da4450da06401c06ffc0 [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"
Chad Rosierbe3ace82012-08-24 17:05:45 +000023#include "llvm/ADT/StringExtras.h"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000024#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Reid Spencer5f016e22007-07-11 17:01:13 +000027static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000028 const char *Name;
29 unsigned Counter;
30 unsigned Size;
Sean Hunt4bfe1962010-05-05 15:24:00 +000031} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000032
33static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
34 static bool Initialized = false;
35 if (Initialized)
36 return StmtClassInfo[E];
37
38 // Intialize the table on the first use.
39 Initialized = true;
Sean Hunt7381d5c2010-05-18 06:22:21 +000040#define ABSTRACT_STMT(STMT)
Douglas Gregorf2cad862008-11-14 12:46:07 +000041#define STMT(CLASS, PARENT) \
42 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
43 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Sean Hunt4bfe1962010-05-05 15:24:00 +000044#include "clang/AST/StmtNodes.inc"
Nico Weber608b17f2008-08-05 23:15:29 +000045
Chris Lattner63381352007-08-25 01:42:24 +000046 return StmtClassInfo[E];
47}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049const char *Stmt::getStmtClassName() const {
John McCall8e6285a2010-10-26 08:39:16 +000050 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000051}
52
53void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000054 // Ensure the table is primed.
55 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000056
Reid Spencer5f016e22007-07-11 17:01:13 +000057 unsigned sum = 0;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000058 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Sean Hunt4bfe1962010-05-05 15:24:00 +000059 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000060 if (StmtClassInfo[i].Name == 0) continue;
61 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000062 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000063 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000064 sum = 0;
Sean Hunt4bfe1962010-05-05 15:24:00 +000065 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000066 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000067 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000068 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
69 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
70 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
71 << " bytes)\n";
Chris Lattner63381352007-08-25 01:42:24 +000072 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000073 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000074
75 llvm::errs() << "Total bytes = " << sum << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000076}
77
78void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000079 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000080}
81
Daniel Dunbar02892a62012-03-05 21:42:49 +000082bool Stmt::StatisticsEnabled = false;
83void Stmt::EnableStatistics() {
84 StatisticsEnabled = true;
Reid Spencer5f016e22007-07-11 17:01:13 +000085}
86
John McCall7e5e5f42011-07-07 06:58:02 +000087Stmt *Stmt::IgnoreImplicit() {
88 Stmt *s = this;
89
90 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
91 s = ewc->getSubExpr();
92
93 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
94 s = ice->getSubExpr();
95
96 return s;
97}
98
Chandler Carrutha1364be2011-09-10 00:02:34 +000099/// \brief Strip off all label-like statements.
100///
Richard Smith534986f2012-04-14 00:33:13 +0000101/// This will strip off label statements, case statements, attributed
102/// statements and default statements recursively.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000103const Stmt *Stmt::stripLabelLikeStatements() const {
104 const Stmt *S = this;
105 while (true) {
106 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
107 S = LS->getSubStmt();
108 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
109 S = SC->getSubStmt();
Richard Smith534986f2012-04-14 00:33:13 +0000110 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
111 S = AS->getSubStmt();
Chandler Carrutha1364be2011-09-10 00:02:34 +0000112 else
113 return S;
114 }
115}
116
John McCall63c00d72011-02-09 08:16:59 +0000117namespace {
118 struct good {};
119 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +0000120
121 // These silly little functions have to be static inline to suppress
122 // unused warnings, and they have to be defined to suppress other
123 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000124 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +0000125
126 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000127 template <class T> good implements_children(children_t T::*) {
128 return good();
129 }
130 static inline bad implements_children(children_t Stmt::*) {
131 return bad();
132 }
John McCall63c00d72011-02-09 08:16:59 +0000133
134 typedef SourceRange getSourceRange_t() const;
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000135 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
136 return good();
137 }
138 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
139 return bad();
140 }
John McCall63c00d72011-02-09 08:16:59 +0000141
142#define ASSERT_IMPLEMENTS_children(type) \
143 (void) sizeof(is_good(implements_children(&type::children)))
144#define ASSERT_IMPLEMENTS_getSourceRange(type) \
145 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
146}
147
148/// Check whether the various Stmt classes implement their member
149/// functions.
150static inline void check_implementations() {
151#define ABSTRACT_STMT(type)
152#define STMT(type, base) \
153 ASSERT_IMPLEMENTS_children(type); \
154 ASSERT_IMPLEMENTS_getSourceRange(type);
155#include "clang/AST/StmtNodes.inc"
156}
157
158Stmt::child_range Stmt::children() {
159 switch (getStmtClass()) {
160 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
161#define ABSTRACT_STMT(type)
162#define STMT(type, base) \
163 case Stmt::type##Class: \
164 return static_cast<type*>(this)->children();
165#include "clang/AST/StmtNodes.inc"
166 }
167 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000168}
169
170SourceRange Stmt::getSourceRange() const {
171 switch (getStmtClass()) {
172 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
173#define ABSTRACT_STMT(type)
174#define STMT(type, base) \
175 case Stmt::type##Class: \
176 return static_cast<const type*>(this)->getSourceRange();
177#include "clang/AST/StmtNodes.inc"
178 }
179 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000180}
181
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000182// Amusing macro metaprogramming hack: check whether a class provides
183// a more specific implementation of getLocStart() and getLocEnd().
184//
185// See also Expr.cpp:getExprLoc().
186namespace {
187 /// This implementation is used when a class provides a custom
188 /// implementation of getLocStart.
189 template <class S, class T>
190 SourceLocation getLocStartImpl(const Stmt *stmt,
191 SourceLocation (T::*v)() const) {
192 return static_cast<const S*>(stmt)->getLocStart();
193 }
194
195 /// This implementation is used when a class doesn't provide a custom
196 /// implementation of getLocStart. Overload resolution should pick it over
197 /// the implementation above because it's more specialized according to
198 /// function template partial ordering.
199 template <class S>
200 SourceLocation getLocStartImpl(const Stmt *stmt,
201 SourceLocation (Stmt::*v)() const) {
202 return static_cast<const S*>(stmt)->getSourceRange().getBegin();
203 }
204
205 /// This implementation is used when a class provides a custom
206 /// implementation of getLocEnd.
207 template <class S, class T>
208 SourceLocation getLocEndImpl(const Stmt *stmt,
209 SourceLocation (T::*v)() const) {
210 return static_cast<const S*>(stmt)->getLocEnd();
211 }
212
213 /// This implementation is used when a class doesn't provide a custom
214 /// implementation of getLocEnd. Overload resolution should pick it over
215 /// the implementation above because it's more specialized according to
216 /// function template partial ordering.
217 template <class S>
218 SourceLocation getLocEndImpl(const Stmt *stmt,
219 SourceLocation (Stmt::*v)() const) {
220 return static_cast<const S*>(stmt)->getSourceRange().getEnd();
221 }
222}
223
224SourceLocation Stmt::getLocStart() const {
225 switch (getStmtClass()) {
226 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
227#define ABSTRACT_STMT(type)
228#define STMT(type, base) \
229 case Stmt::type##Class: \
230 return getLocStartImpl<type>(this, &type::getLocStart);
231#include "clang/AST/StmtNodes.inc"
232 }
233 llvm_unreachable("unknown statement kind");
234}
235
236SourceLocation Stmt::getLocEnd() const {
237 switch (getStmtClass()) {
238 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
239#define ABSTRACT_STMT(type)
240#define STMT(type, base) \
241 case Stmt::type##Class: \
242 return getLocEndImpl<type>(this, &type::getLocEnd);
243#include "clang/AST/StmtNodes.inc"
244 }
245 llvm_unreachable("unknown statement kind");
246}
247
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000248CompoundStmt::CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts,
249 SourceLocation LB, SourceLocation RB)
250 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
251 CompoundStmtBits.NumStmts = NumStmts;
252 assert(CompoundStmtBits.NumStmts == NumStmts &&
253 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
254
255 if (NumStmts == 0) {
256 Body = 0;
257 return;
258 }
259
260 Body = new (C) Stmt*[NumStmts];
261 memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
262}
263
Douglas Gregor025452f2009-04-17 00:04:06 +0000264void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
265 if (this->Body)
266 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000267 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000268
269 Body = new (C) Stmt*[NumStmts];
270 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
271}
Reid Spencer5f016e22007-07-11 17:01:13 +0000272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000274 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000275}
276
Alexander Kornienko49908902012-07-09 10:04:07 +0000277AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
278 ArrayRef<const Attr*> Attrs,
279 Stmt *SubStmt) {
280 void *Mem = C.Allocate(sizeof(AttributedStmt) +
281 sizeof(Attr*) * (Attrs.size() - 1),
282 llvm::alignOf<AttributedStmt>());
283 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
284}
285
286AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
287 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
288 void *Mem = C.Allocate(sizeof(AttributedStmt) +
289 sizeof(Attr*) * (NumAttrs - 1),
290 llvm::alignOf<AttributedStmt>());
291 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
292}
293
Steve Naroff507f2d52007-08-31 23:49:30 +0000294// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000295SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000296 if (RetExpr)
297 return SourceRange(RetLoc, RetExpr->getLocEnd());
298 else
299 return SourceRange(RetLoc);
300}
301
Ted Kremenekd48ade62007-10-01 16:34:52 +0000302bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000303 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000304 default:
305 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000306
Ted Kremenekd48ade62007-10-01 16:34:52 +0000307 case CallExprClass:
308 case ConditionalOperatorClass:
309 case ChooseExprClass:
310 case StmtExprClass:
311 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000312 return true;
313
Ted Kremenekd48ade62007-10-01 16:34:52 +0000314 case Stmt::BinaryOperatorClass: {
315 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000316 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000317 return true;
318 else
319 return false;
320 }
321 }
322}
323
Chad Rosier33f05582012-08-27 23:47:56 +0000324StringRef GCCAsmStmt::getClobber(unsigned i) const {
325 return getClobberStringLiteral(i)->getString();
326}
327
Chad Rosierdf5faf52012-08-25 00:11:56 +0000328Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000329 return cast<Expr>(Exprs[i]);
330}
Chris Lattnerb3277932009-03-10 04:59:06 +0000331
332/// getOutputConstraint - Return the constraint string for the specified
333/// output operand. All output constraints are known to be non-empty (either
334/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000335StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000336 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000337}
Chris Lattnerb3277932009-03-10 04:59:06 +0000338
Chris Lattner85759272009-03-11 00:23:13 +0000339/// getNumPlusOperands - Return the number of output operands that have a "+"
340/// constraint.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000341unsigned GCCAsmStmt::getNumPlusOperands() const {
Chris Lattner85759272009-03-11 00:23:13 +0000342 unsigned Res = 0;
343 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
344 if (isOutputPlusConstraint(i))
345 ++Res;
346 return Res;
347}
348
Chad Rosierdf5faf52012-08-25 00:11:56 +0000349Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000350 return cast<Expr>(Exprs[i + NumOutputs]);
351}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000352void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000353 Exprs[i + NumOutputs] = E;
354}
355
Chris Lattnerb3277932009-03-10 04:59:06 +0000356
357/// getInputConstraint - Return the specified input constraint. Unlike output
358/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000359StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000360 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000361}
362
Chris Lattner10ca96a2009-03-10 06:33:24 +0000363
Chad Rosierdf5faf52012-08-25 00:11:56 +0000364void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000365 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000366 StringLiteral **Constraints,
367 Stmt **Exprs,
368 unsigned NumOutputs,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000369 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000370 StringLiteral **Clobbers,
371 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000372 this->NumOutputs = NumOutputs;
373 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000374 this->NumClobbers = NumClobbers;
375
376 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000377
Anders Carlsson966146e2010-01-30 23:19:41 +0000378 C.Deallocate(this->Names);
379 this->Names = new (C) IdentifierInfo*[NumExprs];
380 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000381
Anders Carlsson966146e2010-01-30 23:19:41 +0000382 C.Deallocate(this->Exprs);
383 this->Exprs = new (C) Stmt*[NumExprs];
384 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000385
Anders Carlsson966146e2010-01-30 23:19:41 +0000386 C.Deallocate(this->Constraints);
387 this->Constraints = new (C) StringLiteral*[NumExprs];
388 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000389
Anders Carlsson966146e2010-01-30 23:19:41 +0000390 C.Deallocate(this->Clobbers);
391 this->Clobbers = new (C) StringLiteral*[NumClobbers];
392 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000393}
394
Chris Lattner10ca96a2009-03-10 06:33:24 +0000395/// getNamedOperand - Given a symbolic operand reference like %[foo],
396/// translate this into a numeric value needed to reference the same operand.
397/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000398int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000399 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattner10ca96a2009-03-10 06:33:24 +0000401 // Check if this is an output operand.
402 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
403 if (getOutputName(i) == SymbolicName)
404 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattner10ca96a2009-03-10 06:33:24 +0000407 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
408 if (getInputName(i) == SymbolicName)
409 return getNumOutputs() + NumPlusOperands + i;
410
411 // Not found.
412 return -1;
413}
414
Chris Lattner458cd9c2009-03-10 23:21:44 +0000415/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
416/// it into pieces. If the asm string is erroneous, emit errors and return
417/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000418unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000419 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000420 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000421 const char *StrStart = Str.begin();
422 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000423 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner458cd9c2009-03-10 23:21:44 +0000425 // "Simple" inline asms have no constraints or operands, just convert the asm
426 // string to escape $'s.
427 if (isSimple()) {
428 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000429 for (; CurPtr != StrEnd; ++CurPtr) {
430 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000431 case '$':
432 Result += "$$";
433 break;
434 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000435 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000436 break;
437 }
438 }
439 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000440 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000441 }
442
443 // CurStringPiece - The current string that we are building up as we scan the
444 // asm string.
445 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000447 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000448
Chris Lattner458cd9c2009-03-10 23:21:44 +0000449 while (1) {
450 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000451 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000452 if (!CurStringPiece.empty())
453 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000454 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner3182db12009-03-10 23:51:40 +0000457 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000458 switch (CurChar) {
459 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000460 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
461 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
462 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000463 case '%':
464 break;
465 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000466 CurStringPiece += CurChar;
467 continue;
468 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000469
Chris Lattner458cd9c2009-03-10 23:21:44 +0000470 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000471 if (CurPtr == StrEnd) {
472 // % at end of string is invalid (no escape).
473 DiagOffs = CurPtr-StrStart-1;
474 return diag::err_asm_invalid_escape;
475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattner3182db12009-03-10 23:51:40 +0000477 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000478 if (EscapedChar == '%') { // %% -> %
479 // Escaped percentage sign.
480 CurStringPiece += '%';
481 continue;
482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner458cd9c2009-03-10 23:21:44 +0000484 if (EscapedChar == '=') { // %= -> Generate an unique ID.
485 CurStringPiece += "${:uid}";
486 continue;
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattner458cd9c2009-03-10 23:21:44 +0000489 // Otherwise, we have an operand. If we have accumulated a string so far,
490 // add it to the Pieces list.
491 if (!CurStringPiece.empty()) {
492 Pieces.push_back(AsmStringPiece(CurStringPiece));
493 CurStringPiece.clear();
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattner458cd9c2009-03-10 23:21:44 +0000496 // Handle %x4 and %x[foo] by capturing x as the modifier character.
497 char Modifier = '\0';
498 if (isalpha(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000499 if (CurPtr == StrEnd) { // Premature end.
500 DiagOffs = CurPtr-StrStart-1;
501 return diag::err_asm_invalid_escape;
502 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000503 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000504 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Chris Lattner458cd9c2009-03-10 23:21:44 +0000507 if (isdigit(EscapedChar)) {
508 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000509 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Chris Lattnercafc2222009-03-11 22:52:17 +0000511 --CurPtr;
512 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000513 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner85759272009-03-11 00:23:13 +0000515 unsigned NumOperands =
516 getNumOutputs() + getNumPlusOperands() + getNumInputs();
517 if (N >= NumOperands) {
518 DiagOffs = CurPtr-StrStart-1;
519 return diag::err_asm_invalid_operand_number;
520 }
521
Chris Lattner458cd9c2009-03-10 23:21:44 +0000522 Pieces.push_back(AsmStringPiece(N, Modifier));
523 continue;
524 }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Chris Lattner458cd9c2009-03-10 23:21:44 +0000526 // Handle %[foo], a symbolic operand reference.
527 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000528 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000530 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000531 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000532 if (NameEnd == 0)
533 return diag::err_asm_unterminated_symbolic_operand_name;
534 if (NameEnd == CurPtr)
535 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattner5f9e2722011-07-23 10:55:15 +0000537 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner458cd9c2009-03-10 23:21:44 +0000539 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000540 if (N == -1) {
541 // Verify that an operand with that name exists.
542 DiagOffs = CurPtr-StrStart;
543 return diag::err_asm_unknown_symbolic_operand_name;
544 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000545 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000547 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000548 continue;
549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner2ff0f422009-03-10 23:57:07 +0000551 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000552 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000553 }
554}
Chad Rosierda083b22012-08-27 20:23:31 +0000555
556/// Assemble final IR asm string (GCC-style).
557std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000558 // Analyze the asm string to decompose it into its pieces. We know that Sema
559 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000560 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000561 unsigned DiagOffs;
562 AnalyzeAsmString(Pieces, C, DiagOffs);
563
564 std::string AsmString;
565 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
566 if (Pieces[i].isString())
567 AsmString += Pieces[i].getString();
568 else if (Pieces[i].getModifier() == '\0')
569 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
570 else
571 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
572 Pieces[i].getModifier() + '}';
573 }
574 return AsmString;
575}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000576
Chad Rosierda083b22012-08-27 20:23:31 +0000577/// Assemble final IR asm string (MS-style).
578std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
579 // FIXME: This needs to be translated into the IR string representation.
580 return std::string();
581}
582
Chad Rosier633abb02012-08-24 00:07:09 +0000583Expr *MSAsmStmt::getOutputExpr(unsigned i) {
584 return cast<Expr>(Exprs[i]);
585}
586
587Expr *MSAsmStmt::getInputExpr(unsigned i) {
588 return cast<Expr>(Exprs[i + NumOutputs]);
589}
590void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
591 Exprs[i + NumOutputs] = E;
592}
593
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000594QualType CXXCatchStmt::getCaughtType() const {
595 if (ExceptionDecl)
596 return ExceptionDecl->getType();
597 return QualType();
598}
599
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000600//===----------------------------------------------------------------------===//
601// Constructors
602//===----------------------------------------------------------------------===//
603
Chad Rosierdf5faf52012-08-25 00:11:56 +0000604GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
605 bool isvolatile, unsigned numoutputs, unsigned numinputs,
606 IdentifierInfo **names, StringLiteral **constraints,
607 Expr **exprs, StringLiteral *asmstr,
608 unsigned numclobbers, StringLiteral **clobbers,
609 SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000610 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
611 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000612
Chad Rosier0e2a8682012-08-07 23:12:23 +0000613 unsigned NumExprs = NumOutputs + NumInputs;
614
Anders Carlsson966146e2010-01-30 23:19:41 +0000615 Names = new (C) IdentifierInfo*[NumExprs];
616 std::copy(names, names + NumExprs, Names);
617
618 Exprs = new (C) Stmt*[NumExprs];
619 std::copy(exprs, exprs + NumExprs, Exprs);
620
621 Constraints = new (C) StringLiteral*[NumExprs];
622 std::copy(constraints, constraints + NumExprs, Constraints);
623
624 Clobbers = new (C) StringLiteral*[NumClobbers];
625 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000626}
627
Chad Rosier058ab172012-08-16 00:06:53 +0000628MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
629 SourceLocation lbraceloc, bool issimple, bool isvolatile,
630 ArrayRef<Token> asmtoks, ArrayRef<IdentifierInfo*> inputs,
Chad Rosier633abb02012-08-24 00:07:09 +0000631 ArrayRef<IdentifierInfo*> outputs,
632 ArrayRef<Expr*> inputexprs, ArrayRef<Expr*> outputexprs,
633 StringRef asmstr, ArrayRef<StringRef> clobbers,
634 SourceLocation endloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000635 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, outputs.size(), inputs.size(),
636 clobbers.size()), LBraceLoc(lbraceloc), EndLoc(endloc),
637 AsmStr(asmstr.str()), NumAsmToks(asmtoks.size()) {
Chad Rosier633abb02012-08-24 00:07:09 +0000638 assert (inputs.size() == inputexprs.size() && "Input expr size mismatch!");
639 assert (outputs.size() == outputexprs.size() && "Input expr size mismatch!");
Chad Rosier058ab172012-08-16 00:06:53 +0000640
641 unsigned NumExprs = NumOutputs + NumInputs;
642
643 Names = new (C) IdentifierInfo*[NumExprs];
644 for (unsigned i = 0, e = NumOutputs; i != e; ++i)
645 Names[i] = outputs[i];
646 for (unsigned i = NumOutputs, e = NumExprs; i != e; ++i)
647 Names[i] = inputs[i];
Chad Rosier79efe242012-08-07 00:29:06 +0000648
Chad Rosier633abb02012-08-24 00:07:09 +0000649 Exprs = new (C) Stmt*[NumExprs];
650 for (unsigned i = 0, e = NumOutputs; i != e; ++i)
651 Exprs[i] = outputexprs[i];
652 for (unsigned i = NumOutputs, e = NumExprs; i != e; ++i)
653 Exprs[i] = inputexprs[i];
654
Chad Rosier79efe242012-08-07 00:29:06 +0000655 AsmToks = new (C) Token[NumAsmToks];
656 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
657 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000658
Chad Rosier33c72e12012-08-10 21:36:25 +0000659 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000660 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
661 // FIXME: Avoid the allocation/copy if at all possible.
662 size_t size = clobbers[i].size();
663 char *dest = new (C) char[size];
664 std::strncpy(dest, clobbers[i].data(), size);
Chad Rosier33c72e12012-08-10 21:36:25 +0000665 Clobbers[i] = StringRef(dest, size);
Chad Rosiere790bc32012-08-10 21:06:19 +0000666 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000667}
668
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000669ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
670 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000671 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000672: Stmt(ObjCForCollectionStmtClass) {
673 SubExprs[ELEM] = Elem;
674 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
675 SubExprs[BODY] = Body;
676 ForLoc = FCL;
677 RParenLoc = RPL;
678}
679
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000680ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
681 Stmt **CatchStmts, unsigned NumCatchStmts,
682 Stmt *atFinallyStmt)
683 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
684 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
685{
686 Stmt **Stmts = getStmts();
687 Stmts[0] = atTryStmt;
688 for (unsigned I = 0; I != NumCatchStmts; ++I)
689 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000690
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000691 if (HasFinally)
692 Stmts[NumCatchStmts + 1] = atFinallyStmt;
693}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000694
Chad Rosier0e2a8682012-08-07 23:12:23 +0000695ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
696 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000697 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000698 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000699 unsigned NumCatchStmts,
700 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000701 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000702 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000703 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000704 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
705 atFinallyStmt);
706}
Ted Kremenekff981022008-02-01 21:28:59 +0000707
Chad Rosier0e2a8682012-08-07 23:12:23 +0000708ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000709 unsigned NumCatchStmts,
710 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000711 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000712 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000713 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000714 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000715}
Nico Weber608b17f2008-08-05 23:15:29 +0000716
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000717SourceRange ObjCAtTryStmt::getSourceRange() const {
718 SourceLocation EndLoc;
719 if (HasFinally)
720 EndLoc = getFinallyStmt()->getLocEnd();
721 else if (NumCatchStmts)
722 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
723 else
724 EndLoc = getTryBody()->getLocEnd();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000725
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000726 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000727}
728
Sam Weiniga1a396d2010-02-03 03:56:39 +0000729CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000730 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000731 unsigned numHandlers) {
732 std::size_t Size = sizeof(CXXTryStmt);
733 Size += ((numHandlers + 1) * sizeof(Stmt));
734
Chris Lattner32488542010-10-30 05:14:06 +0000735 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000736 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
737}
738
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000739CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
740 unsigned numHandlers) {
741 std::size_t Size = sizeof(CXXTryStmt);
742 Size += ((numHandlers + 1) * sizeof(Stmt));
743
Chris Lattner32488542010-10-30 05:14:06 +0000744 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000745 return new (Mem) CXXTryStmt(Empty, numHandlers);
746}
747
Sam Weiniga1a396d2010-02-03 03:56:39 +0000748CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000749 Stmt **handlers, unsigned numHandlers)
750 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000751 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000752 Stmts[0] = tryBlock;
753 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
754}
755
Richard Smithad762fc2011-04-14 22:09:26 +0000756CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
757 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
758 Stmt *Body, SourceLocation FL,
759 SourceLocation CL, SourceLocation RPL)
760 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
761 SubExprs[RANGE] = Range;
762 SubExprs[BEGINEND] = BeginEndStmt;
763 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
764 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
765 SubExprs[LOOPVAR] = LoopVar;
766 SubExprs[BODY] = Body;
767}
768
769Expr *CXXForRangeStmt::getRangeInit() {
770 DeclStmt *RangeStmt = getRangeStmt();
771 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
772 assert(RangeDecl &&& "for-range should have a single var decl");
773 return RangeDecl->getInit();
774}
775
776const Expr *CXXForRangeStmt::getRangeInit() const {
777 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
778}
779
780VarDecl *CXXForRangeStmt::getLoopVariable() {
781 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
782 assert(LV && "No loop variable in CXXForRangeStmt");
783 return cast<VarDecl>(LV);
784}
785
786const VarDecl *CXXForRangeStmt::getLoopVariable() const {
787 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
788}
789
Chad Rosier0e2a8682012-08-07 23:12:23 +0000790IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000791 Stmt *then, SourceLocation EL, Stmt *elsev)
792 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000793{
794 setConditionVariable(C, var);
795 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
796 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000797 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000798}
799
800VarDecl *IfStmt::getConditionVariable() const {
801 if (!SubExprs[VAR])
802 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000803
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000804 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
805 return cast<VarDecl>(DS->getSingleDecl());
806}
807
808void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
809 if (!V) {
810 SubExprs[VAR] = 0;
811 return;
812 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000813
Daniel Dunbar96a00142012-03-09 18:35:03 +0000814 SourceRange VarRange = V->getSourceRange();
815 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
816 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000817}
818
Chad Rosier0e2a8682012-08-07 23:12:23 +0000819ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
820 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000821 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000822 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000823{
824 SubExprs[INIT] = Init;
825 setConditionVariable(C, condVar);
826 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
827 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
828 SubExprs[BODY] = Body;
829}
830
831VarDecl *ForStmt::getConditionVariable() const {
832 if (!SubExprs[CONDVAR])
833 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000834
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000835 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
836 return cast<VarDecl>(DS->getSingleDecl());
837}
838
839void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
840 if (!V) {
841 SubExprs[CONDVAR] = 0;
842 return;
843 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000844
Daniel Dunbar96a00142012-03-09 18:35:03 +0000845 SourceRange VarRange = V->getSourceRange();
846 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
847 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000848}
849
Chad Rosier0e2a8682012-08-07 23:12:23 +0000850SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
851 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000852{
853 setConditionVariable(C, Var);
854 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
855 SubExprs[BODY] = NULL;
856}
857
858VarDecl *SwitchStmt::getConditionVariable() const {
859 if (!SubExprs[VAR])
860 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000861
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000862 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
863 return cast<VarDecl>(DS->getSingleDecl());
864}
865
866void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
867 if (!V) {
868 SubExprs[VAR] = 0;
869 return;
870 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000871
Daniel Dunbar96a00142012-03-09 18:35:03 +0000872 SourceRange VarRange = V->getSourceRange();
873 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
874 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000875}
876
John McCall63c00d72011-02-09 08:16:59 +0000877Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000878 if (isa<CaseStmt>(this))
879 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000880 return cast<DefaultStmt>(this)->getSubStmt();
881}
882
Chad Rosier0e2a8682012-08-07 23:12:23 +0000883WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000884 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000885 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000886 setConditionVariable(C, Var);
887 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
888 SubExprs[BODY] = body;
889 WhileLoc = WL;
890}
891
892VarDecl *WhileStmt::getConditionVariable() const {
893 if (!SubExprs[VAR])
894 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000895
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000896 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
897 return cast<VarDecl>(DS->getSingleDecl());
898}
899
900void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
901 if (!V) {
902 SubExprs[VAR] = 0;
903 return;
904 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000905
906 SourceRange VarRange = V->getSourceRange();
907 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
908 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000909}
910
Ted Kremenek82977772007-08-24 21:09:09 +0000911// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000912LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000913 if (AddrLabelExpr *E =
914 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
915 return E->getLabel();
916 return 0;
917}
Ted Kremenek82977772007-08-24 21:09:09 +0000918
Ted Kremenek82977772007-08-24 21:09:09 +0000919// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000920const Expr* ReturnStmt::getRetValue() const {
921 return cast_or_null<Expr>(RetExpr);
922}
923Expr* ReturnStmt::getRetValue() {
924 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000925}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000926
927SEHTryStmt::SEHTryStmt(bool IsCXXTry,
928 SourceLocation TryLoc,
929 Stmt *TryBlock,
930 Stmt *Handler)
931 : Stmt(SEHTryStmtClass),
932 IsCXXTry(IsCXXTry),
933 TryLoc(TryLoc)
934{
935 Children[TRY] = TryBlock;
936 Children[HANDLER] = Handler;
937}
938
939SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
940 bool IsCXXTry,
941 SourceLocation TryLoc,
942 Stmt *TryBlock,
943 Stmt *Handler) {
944 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
945}
946
947SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
948 return dyn_cast<SEHExceptStmt>(getHandler());
949}
950
951SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
952 return dyn_cast<SEHFinallyStmt>(getHandler());
953}
954
955SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
956 Expr *FilterExpr,
957 Stmt *Block)
958 : Stmt(SEHExceptStmtClass),
959 Loc(Loc)
960{
961 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
962 Children[BLOCK] = Block;
963}
964
965SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
966 SourceLocation Loc,
967 Expr *FilterExpr,
968 Stmt *Block) {
969 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
970}
971
972SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
973 Stmt *Block)
974 : Stmt(SEHFinallyStmtClass),
975 Loc(Loc),
976 Block(Block)
977{}
978
979SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
980 SourceLocation Loc,
981 Stmt *Block) {
982 return new(C)SEHFinallyStmt(Loc,Block);
983}