blob: 6b14ae5fd2fac9821864cf9f537f2df7eccb0e35 [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"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000023#include "llvm/Support/raw_ostream.h"
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 {
John McCall8e6285a2010-10-26 08:39:16 +000049 return getStmtInfoTableEntry((StmtClass) StmtBits.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;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000057 llvm::errs() << "\n*** 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 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000062 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000063 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;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000067 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
68 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
69 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
70 << " bytes)\n";
Chris Lattner63381352007-08-25 01:42:24 +000071 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000073
74 llvm::errs() << "Total bytes = " << sum << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000075}
76
77void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000078 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000079}
80
Daniel Dunbar02892a62012-03-05 21:42:49 +000081bool Stmt::StatisticsEnabled = false;
82void Stmt::EnableStatistics() {
83 StatisticsEnabled = true;
Reid Spencer5f016e22007-07-11 17:01:13 +000084}
85
John McCall7e5e5f42011-07-07 06:58:02 +000086Stmt *Stmt::IgnoreImplicit() {
87 Stmt *s = this;
88
89 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
90 s = ewc->getSubExpr();
91
92 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
93 s = ice->getSubExpr();
94
95 return s;
96}
97
Chandler Carrutha1364be2011-09-10 00:02:34 +000098/// \brief Strip off all label-like statements.
99///
Richard Smith534986f2012-04-14 00:33:13 +0000100/// This will strip off label statements, case statements, attributed
101/// statements and default statements recursively.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000102const Stmt *Stmt::stripLabelLikeStatements() const {
103 const Stmt *S = this;
104 while (true) {
105 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
106 S = LS->getSubStmt();
107 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
108 S = SC->getSubStmt();
Richard Smith534986f2012-04-14 00:33:13 +0000109 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
110 S = AS->getSubStmt();
Chandler Carrutha1364be2011-09-10 00:02:34 +0000111 else
112 return S;
113 }
114}
115
John McCall63c00d72011-02-09 08:16:59 +0000116namespace {
117 struct good {};
118 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +0000119
120 // These silly little functions have to be static inline to suppress
121 // unused warnings, and they have to be defined to suppress other
122 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000123 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +0000124
125 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000126 template <class T> good implements_children(children_t T::*) {
127 return good();
128 }
129 static inline bad implements_children(children_t Stmt::*) {
130 return bad();
131 }
John McCall63c00d72011-02-09 08:16:59 +0000132
133 typedef SourceRange getSourceRange_t() const;
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000134 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
135 return good();
136 }
137 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
138 return bad();
139 }
John McCall63c00d72011-02-09 08:16:59 +0000140
141#define ASSERT_IMPLEMENTS_children(type) \
142 (void) sizeof(is_good(implements_children(&type::children)))
143#define ASSERT_IMPLEMENTS_getSourceRange(type) \
144 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
145}
146
147/// Check whether the various Stmt classes implement their member
148/// functions.
149static inline void check_implementations() {
150#define ABSTRACT_STMT(type)
151#define STMT(type, base) \
152 ASSERT_IMPLEMENTS_children(type); \
153 ASSERT_IMPLEMENTS_getSourceRange(type);
154#include "clang/AST/StmtNodes.inc"
155}
156
157Stmt::child_range Stmt::children() {
158 switch (getStmtClass()) {
159 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
160#define ABSTRACT_STMT(type)
161#define STMT(type, base) \
162 case Stmt::type##Class: \
163 return static_cast<type*>(this)->children();
164#include "clang/AST/StmtNodes.inc"
165 }
166 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000167}
168
169SourceRange Stmt::getSourceRange() const {
170 switch (getStmtClass()) {
171 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
172#define ABSTRACT_STMT(type)
173#define STMT(type, base) \
174 case Stmt::type##Class: \
175 return static_cast<const type*>(this)->getSourceRange();
176#include "clang/AST/StmtNodes.inc"
177 }
178 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000179}
180
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000181// Amusing macro metaprogramming hack: check whether a class provides
182// a more specific implementation of getLocStart() and getLocEnd().
183//
184// See also Expr.cpp:getExprLoc().
185namespace {
186 /// This implementation is used when a class provides a custom
187 /// implementation of getLocStart.
188 template <class S, class T>
189 SourceLocation getLocStartImpl(const Stmt *stmt,
190 SourceLocation (T::*v)() const) {
191 return static_cast<const S*>(stmt)->getLocStart();
192 }
193
194 /// This implementation is used when a class doesn't provide a custom
195 /// implementation of getLocStart. Overload resolution should pick it over
196 /// the implementation above because it's more specialized according to
197 /// function template partial ordering.
198 template <class S>
199 SourceLocation getLocStartImpl(const Stmt *stmt,
200 SourceLocation (Stmt::*v)() const) {
201 return static_cast<const S*>(stmt)->getSourceRange().getBegin();
202 }
203
204 /// This implementation is used when a class provides a custom
205 /// implementation of getLocEnd.
206 template <class S, class T>
207 SourceLocation getLocEndImpl(const Stmt *stmt,
208 SourceLocation (T::*v)() const) {
209 return static_cast<const S*>(stmt)->getLocEnd();
210 }
211
212 /// This implementation is used when a class doesn't provide a custom
213 /// implementation of getLocEnd. Overload resolution should pick it over
214 /// the implementation above because it's more specialized according to
215 /// function template partial ordering.
216 template <class S>
217 SourceLocation getLocEndImpl(const Stmt *stmt,
218 SourceLocation (Stmt::*v)() const) {
219 return static_cast<const S*>(stmt)->getSourceRange().getEnd();
220 }
221}
222
223SourceLocation Stmt::getLocStart() const {
224 switch (getStmtClass()) {
225 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
226#define ABSTRACT_STMT(type)
227#define STMT(type, base) \
228 case Stmt::type##Class: \
229 return getLocStartImpl<type>(this, &type::getLocStart);
230#include "clang/AST/StmtNodes.inc"
231 }
232 llvm_unreachable("unknown statement kind");
233}
234
235SourceLocation Stmt::getLocEnd() const {
236 switch (getStmtClass()) {
237 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
238#define ABSTRACT_STMT(type)
239#define STMT(type, base) \
240 case Stmt::type##Class: \
241 return getLocEndImpl<type>(this, &type::getLocEnd);
242#include "clang/AST/StmtNodes.inc"
243 }
244 llvm_unreachable("unknown statement kind");
245}
246
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000247CompoundStmt::CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts,
248 SourceLocation LB, SourceLocation RB)
249 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
250 CompoundStmtBits.NumStmts = NumStmts;
251 assert(CompoundStmtBits.NumStmts == NumStmts &&
252 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
253
254 if (NumStmts == 0) {
255 Body = 0;
256 return;
257 }
258
259 Body = new (C) Stmt*[NumStmts];
260 memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
261}
262
Douglas Gregor025452f2009-04-17 00:04:06 +0000263void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
264 if (this->Body)
265 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000266 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000267
268 Body = new (C) Stmt*[NumStmts];
269 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
270}
Reid Spencer5f016e22007-07-11 17:01:13 +0000271
Reid Spencer5f016e22007-07-11 17:01:13 +0000272const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000273 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000274}
275
Alexander Kornienko49908902012-07-09 10:04:07 +0000276AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
277 ArrayRef<const Attr*> Attrs,
278 Stmt *SubStmt) {
279 void *Mem = C.Allocate(sizeof(AttributedStmt) +
280 sizeof(Attr*) * (Attrs.size() - 1),
281 llvm::alignOf<AttributedStmt>());
282 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
283}
284
285AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
286 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
287 void *Mem = C.Allocate(sizeof(AttributedStmt) +
288 sizeof(Attr*) * (NumAttrs - 1),
289 llvm::alignOf<AttributedStmt>());
290 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
291}
292
Steve Naroff507f2d52007-08-31 23:49:30 +0000293// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000294SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000295 if (RetExpr)
296 return SourceRange(RetLoc, RetExpr->getLocEnd());
297 else
298 return SourceRange(RetLoc);
299}
300
Ted Kremenekd48ade62007-10-01 16:34:52 +0000301bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000302 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000303 default:
304 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000305
Ted Kremenekd48ade62007-10-01 16:34:52 +0000306 case CallExprClass:
307 case ConditionalOperatorClass:
308 case ChooseExprClass:
309 case StmtExprClass:
310 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000311 return true;
312
Ted Kremenekd48ade62007-10-01 16:34:52 +0000313 case Stmt::BinaryOperatorClass: {
314 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000315 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000316 return true;
317 else
318 return false;
319 }
320 }
321}
322
Chris Lattnerb3277932009-03-10 04:59:06 +0000323Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000324 return cast<Expr>(Exprs[i]);
325}
Chris Lattnerb3277932009-03-10 04:59:06 +0000326
327/// getOutputConstraint - Return the constraint string for the specified
328/// output operand. All output constraints are known to be non-empty (either
329/// '=' or '+').
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000331 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000332}
Chris Lattnerb3277932009-03-10 04:59:06 +0000333
Chris Lattner85759272009-03-11 00:23:13 +0000334/// getNumPlusOperands - Return the number of output operands that have a "+"
335/// constraint.
336unsigned AsmStmt::getNumPlusOperands() const {
337 unsigned Res = 0;
338 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
339 if (isOutputPlusConstraint(i))
340 ++Res;
341 return Res;
342}
343
Chris Lattnerb3277932009-03-10 04:59:06 +0000344Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000345 return cast<Expr>(Exprs[i + NumOutputs]);
346}
Chris Lattner935f0f02011-02-21 22:09:29 +0000347void AsmStmt::setInputExpr(unsigned i, Expr *E) {
348 Exprs[i + NumOutputs] = E;
349}
350
Chris Lattnerb3277932009-03-10 04:59:06 +0000351
352/// getInputConstraint - Return the specified input constraint. Unlike output
353/// constraints, these can be empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000354StringRef AsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000355 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000356}
357
Chris Lattner10ca96a2009-03-10 06:33:24 +0000358
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000359void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000360 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000361 StringLiteral **Constraints,
362 Stmt **Exprs,
363 unsigned NumOutputs,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000364 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000365 StringLiteral **Clobbers,
366 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000367 this->NumOutputs = NumOutputs;
368 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000369 this->NumClobbers = NumClobbers;
370
371 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000372
Anders Carlsson966146e2010-01-30 23:19:41 +0000373 C.Deallocate(this->Names);
374 this->Names = new (C) IdentifierInfo*[NumExprs];
375 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000376
Anders Carlsson966146e2010-01-30 23:19:41 +0000377 C.Deallocate(this->Exprs);
378 this->Exprs = new (C) Stmt*[NumExprs];
379 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000380
Anders Carlsson966146e2010-01-30 23:19:41 +0000381 C.Deallocate(this->Constraints);
382 this->Constraints = new (C) StringLiteral*[NumExprs];
383 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000384
Anders Carlsson966146e2010-01-30 23:19:41 +0000385 C.Deallocate(this->Clobbers);
386 this->Clobbers = new (C) StringLiteral*[NumClobbers];
387 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000388}
389
Chris Lattner10ca96a2009-03-10 06:33:24 +0000390/// getNamedOperand - Given a symbolic operand reference like %[foo],
391/// translate this into a numeric value needed to reference the same operand.
392/// This returns -1 if the operand name is invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000393int AsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000394 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattner10ca96a2009-03-10 06:33:24 +0000396 // Check if this is an output operand.
397 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
398 if (getOutputName(i) == SymbolicName)
399 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattner10ca96a2009-03-10 06:33:24 +0000402 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
403 if (getInputName(i) == SymbolicName)
404 return getNumOutputs() + NumPlusOperands + i;
405
406 // Not found.
407 return -1;
408}
409
Chris Lattner458cd9c2009-03-10 23:21:44 +0000410/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
411/// it into pieces. If the asm string is erroneous, emit errors and return
412/// true, otherwise return false.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000413unsigned AsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000414 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000415 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000416 const char *StrStart = Str.begin();
417 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000418 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner458cd9c2009-03-10 23:21:44 +0000420 // "Simple" inline asms have no constraints or operands, just convert the asm
421 // string to escape $'s.
422 if (isSimple()) {
423 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000424 for (; CurPtr != StrEnd; ++CurPtr) {
425 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000426 case '$':
427 Result += "$$";
428 break;
429 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000430 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000431 break;
432 }
433 }
434 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000435 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000436 }
437
438 // CurStringPiece - The current string that we are building up as we scan the
439 // asm string.
440 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000442 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000443
Chris Lattner458cd9c2009-03-10 23:21:44 +0000444 while (1) {
445 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000446 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000447 if (!CurStringPiece.empty())
448 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000449 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Chris Lattner3182db12009-03-10 23:51:40 +0000452 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000453 switch (CurChar) {
454 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000455 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
456 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
457 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000458 case '%':
459 break;
460 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000461 CurStringPiece += CurChar;
462 continue;
463 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000464
Chris Lattner458cd9c2009-03-10 23:21:44 +0000465 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000466 if (CurPtr == StrEnd) {
467 // % at end of string is invalid (no escape).
468 DiagOffs = CurPtr-StrStart-1;
469 return diag::err_asm_invalid_escape;
470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattner3182db12009-03-10 23:51:40 +0000472 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000473 if (EscapedChar == '%') { // %% -> %
474 // Escaped percentage sign.
475 CurStringPiece += '%';
476 continue;
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattner458cd9c2009-03-10 23:21:44 +0000479 if (EscapedChar == '=') { // %= -> Generate an unique ID.
480 CurStringPiece += "${:uid}";
481 continue;
482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner458cd9c2009-03-10 23:21:44 +0000484 // Otherwise, we have an operand. If we have accumulated a string so far,
485 // add it to the Pieces list.
486 if (!CurStringPiece.empty()) {
487 Pieces.push_back(AsmStringPiece(CurStringPiece));
488 CurStringPiece.clear();
489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner458cd9c2009-03-10 23:21:44 +0000491 // Handle %x4 and %x[foo] by capturing x as the modifier character.
492 char Modifier = '\0';
493 if (isalpha(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000494 if (CurPtr == StrEnd) { // Premature end.
495 DiagOffs = CurPtr-StrStart-1;
496 return diag::err_asm_invalid_escape;
497 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000498 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000499 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner458cd9c2009-03-10 23:21:44 +0000502 if (isdigit(EscapedChar)) {
503 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000504 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Chris Lattnercafc2222009-03-11 22:52:17 +0000506 --CurPtr;
507 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000508 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattner85759272009-03-11 00:23:13 +0000510 unsigned NumOperands =
511 getNumOutputs() + getNumPlusOperands() + getNumInputs();
512 if (N >= NumOperands) {
513 DiagOffs = CurPtr-StrStart-1;
514 return diag::err_asm_invalid_operand_number;
515 }
516
Chris Lattner458cd9c2009-03-10 23:21:44 +0000517 Pieces.push_back(AsmStringPiece(N, Modifier));
518 continue;
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner458cd9c2009-03-10 23:21:44 +0000521 // Handle %[foo], a symbolic operand reference.
522 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000523 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000525 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000526 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000527 if (NameEnd == 0)
528 return diag::err_asm_unterminated_symbolic_operand_name;
529 if (NameEnd == CurPtr)
530 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Chris Lattner5f9e2722011-07-23 10:55:15 +0000532 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattner458cd9c2009-03-10 23:21:44 +0000534 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000535 if (N == -1) {
536 // Verify that an operand with that name exists.
537 DiagOffs = CurPtr-StrStart;
538 return diag::err_asm_unknown_symbolic_operand_name;
539 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000540 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000542 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000543 continue;
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattner2ff0f422009-03-10 23:57:07 +0000546 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000547 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000548 }
549}
550
Chad Rosier633abb02012-08-24 00:07:09 +0000551Expr *MSAsmStmt::getOutputExpr(unsigned i) {
552 return cast<Expr>(Exprs[i]);
553}
554
555Expr *MSAsmStmt::getInputExpr(unsigned i) {
556 return cast<Expr>(Exprs[i + NumOutputs]);
557}
558void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
559 Exprs[i + NumOutputs] = E;
560}
561
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000562QualType CXXCatchStmt::getCaughtType() const {
563 if (ExceptionDecl)
564 return ExceptionDecl->getType();
565 return QualType();
566}
567
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000568//===----------------------------------------------------------------------===//
569// Constructors
570//===----------------------------------------------------------------------===//
571
Chad Rosier0e2a8682012-08-07 23:12:23 +0000572AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
Chad Rosierdf4ee102012-08-20 17:11:53 +0000573 bool isvolatile, unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000574 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000575 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
576 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000577 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Chad Rosierdf4ee102012-08-20 17:11:53 +0000578 , IsSimple(issimple), IsVolatile(isvolatile), NumOutputs(numoutputs)
579 , NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000580
Chad Rosier0e2a8682012-08-07 23:12:23 +0000581 unsigned NumExprs = NumOutputs + NumInputs;
582
Anders Carlsson966146e2010-01-30 23:19:41 +0000583 Names = new (C) IdentifierInfo*[NumExprs];
584 std::copy(names, names + NumExprs, Names);
585
586 Exprs = new (C) Stmt*[NumExprs];
587 std::copy(exprs, exprs + NumExprs, Exprs);
588
589 Constraints = new (C) StringLiteral*[NumExprs];
590 std::copy(constraints, constraints + NumExprs, Constraints);
591
592 Clobbers = new (C) StringLiteral*[NumClobbers];
593 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000594}
595
Chad Rosier058ab172012-08-16 00:06:53 +0000596MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
597 SourceLocation lbraceloc, bool issimple, bool isvolatile,
598 ArrayRef<Token> asmtoks, ArrayRef<IdentifierInfo*> inputs,
Chad Rosier633abb02012-08-24 00:07:09 +0000599 ArrayRef<IdentifierInfo*> outputs,
600 ArrayRef<Expr*> inputexprs, ArrayRef<Expr*> outputexprs,
601 StringRef asmstr, ArrayRef<StringRef> clobbers,
602 SourceLocation endloc)
Chad Rosier7bd092b2012-08-15 16:53:30 +0000603 : Stmt(MSAsmStmtClass), AsmLoc(asmloc), LBraceLoc(lbraceloc), EndLoc(endloc),
Chad Rosiercace2102012-08-09 21:06:32 +0000604 AsmStr(asmstr.str()), IsSimple(issimple), IsVolatile(isvolatile),
Chad Rosier058ab172012-08-16 00:06:53 +0000605 NumAsmToks(asmtoks.size()), NumInputs(inputs.size()),
606 NumOutputs(outputs.size()), NumClobbers(clobbers.size()) {
Chad Rosier633abb02012-08-24 00:07:09 +0000607 assert (inputs.size() == inputexprs.size() && "Input expr size mismatch!");
608 assert (outputs.size() == outputexprs.size() && "Input expr size mismatch!");
Chad Rosier058ab172012-08-16 00:06:53 +0000609
610 unsigned NumExprs = NumOutputs + NumInputs;
611
612 Names = new (C) IdentifierInfo*[NumExprs];
613 for (unsigned i = 0, e = NumOutputs; i != e; ++i)
614 Names[i] = outputs[i];
615 for (unsigned i = NumOutputs, e = NumExprs; i != e; ++i)
616 Names[i] = inputs[i];
Chad Rosier79efe242012-08-07 00:29:06 +0000617
Chad Rosier633abb02012-08-24 00:07:09 +0000618 Exprs = new (C) Stmt*[NumExprs];
619 for (unsigned i = 0, e = NumOutputs; i != e; ++i)
620 Exprs[i] = outputexprs[i];
621 for (unsigned i = NumOutputs, e = NumExprs; i != e; ++i)
622 Exprs[i] = inputexprs[i];
623
Chad Rosier79efe242012-08-07 00:29:06 +0000624 AsmToks = new (C) Token[NumAsmToks];
625 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
626 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000627
Chad Rosier33c72e12012-08-10 21:36:25 +0000628 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000629 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
630 // FIXME: Avoid the allocation/copy if at all possible.
631 size_t size = clobbers[i].size();
632 char *dest = new (C) char[size];
633 std::strncpy(dest, clobbers[i].data(), size);
Chad Rosier33c72e12012-08-10 21:36:25 +0000634 Clobbers[i] = StringRef(dest, size);
Chad Rosiere790bc32012-08-10 21:06:19 +0000635 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000636}
637
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000638ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
639 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000640 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000641: Stmt(ObjCForCollectionStmtClass) {
642 SubExprs[ELEM] = Elem;
643 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
644 SubExprs[BODY] = Body;
645 ForLoc = FCL;
646 RParenLoc = RPL;
647}
648
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000649ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
650 Stmt **CatchStmts, unsigned NumCatchStmts,
651 Stmt *atFinallyStmt)
652 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
653 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
654{
655 Stmt **Stmts = getStmts();
656 Stmts[0] = atTryStmt;
657 for (unsigned I = 0; I != NumCatchStmts; ++I)
658 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000659
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000660 if (HasFinally)
661 Stmts[NumCatchStmts + 1] = atFinallyStmt;
662}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000663
Chad Rosier0e2a8682012-08-07 23:12:23 +0000664ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
665 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000666 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000667 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000668 unsigned NumCatchStmts,
669 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000670 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000671 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000672 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000673 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
674 atFinallyStmt);
675}
Ted Kremenekff981022008-02-01 21:28:59 +0000676
Chad Rosier0e2a8682012-08-07 23:12:23 +0000677ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000678 unsigned NumCatchStmts,
679 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000680 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000681 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000682 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000683 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000684}
Nico Weber608b17f2008-08-05 23:15:29 +0000685
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000686SourceRange ObjCAtTryStmt::getSourceRange() const {
687 SourceLocation EndLoc;
688 if (HasFinally)
689 EndLoc = getFinallyStmt()->getLocEnd();
690 else if (NumCatchStmts)
691 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
692 else
693 EndLoc = getTryBody()->getLocEnd();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000694
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000695 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000696}
697
Sam Weiniga1a396d2010-02-03 03:56:39 +0000698CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000699 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000700 unsigned numHandlers) {
701 std::size_t Size = sizeof(CXXTryStmt);
702 Size += ((numHandlers + 1) * sizeof(Stmt));
703
Chris Lattner32488542010-10-30 05:14:06 +0000704 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000705 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
706}
707
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000708CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
709 unsigned numHandlers) {
710 std::size_t Size = sizeof(CXXTryStmt);
711 Size += ((numHandlers + 1) * sizeof(Stmt));
712
Chris Lattner32488542010-10-30 05:14:06 +0000713 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000714 return new (Mem) CXXTryStmt(Empty, numHandlers);
715}
716
Sam Weiniga1a396d2010-02-03 03:56:39 +0000717CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000718 Stmt **handlers, unsigned numHandlers)
719 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000720 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000721 Stmts[0] = tryBlock;
722 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
723}
724
Richard Smithad762fc2011-04-14 22:09:26 +0000725CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
726 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
727 Stmt *Body, SourceLocation FL,
728 SourceLocation CL, SourceLocation RPL)
729 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
730 SubExprs[RANGE] = Range;
731 SubExprs[BEGINEND] = BeginEndStmt;
732 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
733 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
734 SubExprs[LOOPVAR] = LoopVar;
735 SubExprs[BODY] = Body;
736}
737
738Expr *CXXForRangeStmt::getRangeInit() {
739 DeclStmt *RangeStmt = getRangeStmt();
740 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
741 assert(RangeDecl &&& "for-range should have a single var decl");
742 return RangeDecl->getInit();
743}
744
745const Expr *CXXForRangeStmt::getRangeInit() const {
746 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
747}
748
749VarDecl *CXXForRangeStmt::getLoopVariable() {
750 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
751 assert(LV && "No loop variable in CXXForRangeStmt");
752 return cast<VarDecl>(LV);
753}
754
755const VarDecl *CXXForRangeStmt::getLoopVariable() const {
756 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
757}
758
Chad Rosier0e2a8682012-08-07 23:12:23 +0000759IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000760 Stmt *then, SourceLocation EL, Stmt *elsev)
761 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000762{
763 setConditionVariable(C, var);
764 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
765 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000766 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000767}
768
769VarDecl *IfStmt::getConditionVariable() const {
770 if (!SubExprs[VAR])
771 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000772
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000773 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
774 return cast<VarDecl>(DS->getSingleDecl());
775}
776
777void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
778 if (!V) {
779 SubExprs[VAR] = 0;
780 return;
781 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000782
Daniel Dunbar96a00142012-03-09 18:35:03 +0000783 SourceRange VarRange = V->getSourceRange();
784 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
785 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000786}
787
Chad Rosier0e2a8682012-08-07 23:12:23 +0000788ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
789 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000790 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000791 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000792{
793 SubExprs[INIT] = Init;
794 setConditionVariable(C, condVar);
795 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
796 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
797 SubExprs[BODY] = Body;
798}
799
800VarDecl *ForStmt::getConditionVariable() const {
801 if (!SubExprs[CONDVAR])
802 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000803
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000804 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
805 return cast<VarDecl>(DS->getSingleDecl());
806}
807
808void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
809 if (!V) {
810 SubExprs[CONDVAR] = 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[CONDVAR] = 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 +0000819SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
820 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000821{
822 setConditionVariable(C, Var);
823 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
824 SubExprs[BODY] = NULL;
825}
826
827VarDecl *SwitchStmt::getConditionVariable() const {
828 if (!SubExprs[VAR])
829 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000830
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000831 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
832 return cast<VarDecl>(DS->getSingleDecl());
833}
834
835void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
836 if (!V) {
837 SubExprs[VAR] = 0;
838 return;
839 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000840
Daniel Dunbar96a00142012-03-09 18:35:03 +0000841 SourceRange VarRange = V->getSourceRange();
842 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
843 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000844}
845
John McCall63c00d72011-02-09 08:16:59 +0000846Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000847 if (isa<CaseStmt>(this))
848 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000849 return cast<DefaultStmt>(this)->getSubStmt();
850}
851
Chad Rosier0e2a8682012-08-07 23:12:23 +0000852WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000853 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000854 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000855 setConditionVariable(C, Var);
856 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
857 SubExprs[BODY] = body;
858 WhileLoc = WL;
859}
860
861VarDecl *WhileStmt::getConditionVariable() const {
862 if (!SubExprs[VAR])
863 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000864
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000865 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
866 return cast<VarDecl>(DS->getSingleDecl());
867}
868
869void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
870 if (!V) {
871 SubExprs[VAR] = 0;
872 return;
873 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000874
875 SourceRange VarRange = V->getSourceRange();
876 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
877 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000878}
879
Ted Kremenek82977772007-08-24 21:09:09 +0000880// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000881LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000882 if (AddrLabelExpr *E =
883 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
884 return E->getLabel();
885 return 0;
886}
Ted Kremenek82977772007-08-24 21:09:09 +0000887
Ted Kremenek82977772007-08-24 21:09:09 +0000888// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000889const Expr* ReturnStmt::getRetValue() const {
890 return cast_or_null<Expr>(RetExpr);
891}
892Expr* ReturnStmt::getRetValue() {
893 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000894}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000895
896SEHTryStmt::SEHTryStmt(bool IsCXXTry,
897 SourceLocation TryLoc,
898 Stmt *TryBlock,
899 Stmt *Handler)
900 : Stmt(SEHTryStmtClass),
901 IsCXXTry(IsCXXTry),
902 TryLoc(TryLoc)
903{
904 Children[TRY] = TryBlock;
905 Children[HANDLER] = Handler;
906}
907
908SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
909 bool IsCXXTry,
910 SourceLocation TryLoc,
911 Stmt *TryBlock,
912 Stmt *Handler) {
913 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
914}
915
916SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
917 return dyn_cast<SEHExceptStmt>(getHandler());
918}
919
920SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
921 return dyn_cast<SEHFinallyStmt>(getHandler());
922}
923
924SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
925 Expr *FilterExpr,
926 Stmt *Block)
927 : Stmt(SEHExceptStmtClass),
928 Loc(Loc)
929{
930 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
931 Children[BLOCK] = Block;
932}
933
934SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
935 SourceLocation Loc,
936 Expr *FilterExpr,
937 Stmt *Block) {
938 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
939}
940
941SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
942 Stmt *Block)
943 : Stmt(SEHFinallyStmtClass),
944 Loc(Loc),
945 Block(Block)
946{}
947
948SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
949 SourceLocation Loc,
950 Stmt *Block) {
951 return new(C)SEHFinallyStmt(Loc,Block);
952}