blob: ddb15607238f32a39a566e6136078e1e663f1659 [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
Benjamin Kramera93d0f22012-12-01 17:12:56 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ExprCXX.h"
Steve Narofff494b572008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Benjamin Kramera93d0f22012-12-01 17:12:56 +000018#include "clang/AST/Stmt.h"
Chris Lattner16f00492009-04-26 01:32:48 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtObjC.h"
Sebastian Redl4b07b292008-12-22 19:15:10 +000021#include "clang/AST/Type.h"
Chris Lattner9bffb072010-04-23 16:29:58 +000022#include "clang/Basic/TargetInfo.h"
Benjamin Kramera93d0f22012-12-01 17:12:56 +000023#include "clang/Lex/Token.h"
Chad Rosierbe3ace82012-08-24 17:05:45 +000024#include "llvm/ADT/StringExtras.h"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000025#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Reid Spencer5f016e22007-07-11 17:01:13 +000028static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000029 const char *Name;
30 unsigned Counter;
31 unsigned Size;
Sean Hunt4bfe1962010-05-05 15:24:00 +000032} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000033
34static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
35 static bool Initialized = false;
36 if (Initialized)
37 return StmtClassInfo[E];
38
39 // Intialize the table on the first use.
40 Initialized = true;
Sean Hunt7381d5c2010-05-18 06:22:21 +000041#define ABSTRACT_STMT(STMT)
Douglas Gregorf2cad862008-11-14 12:46:07 +000042#define STMT(CLASS, PARENT) \
43 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
44 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Sean Hunt4bfe1962010-05-05 15:24:00 +000045#include "clang/AST/StmtNodes.inc"
Nico Weber608b17f2008-08-05 23:15:29 +000046
Chris Lattner63381352007-08-25 01:42:24 +000047 return StmtClassInfo[E];
48}
49
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000050void *Stmt::operator new(size_t bytes, ASTContext& C,
51 unsigned alignment) throw() {
52 return ::operator new(bytes, C, alignment);
53}
54
55void *Stmt::operator new(size_t bytes, ASTContext* C,
56 unsigned alignment) throw() {
57 return ::operator new(bytes, *C, alignment);
58}
59
Reid Spencer5f016e22007-07-11 17:01:13 +000060const char *Stmt::getStmtClassName() const {
John McCall8e6285a2010-10-26 08:39:16 +000061 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000062}
63
64void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000065 // Ensure the table is primed.
66 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000067
Reid Spencer5f016e22007-07-11 17:01:13 +000068 unsigned sum = 0;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000069 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Sean Hunt4bfe1962010-05-05 15:24:00 +000070 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000071 if (StmtClassInfo[i].Name == 0) continue;
72 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000073 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000074 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000075 sum = 0;
Sean Hunt4bfe1962010-05-05 15:24:00 +000076 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000077 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000078 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000079 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
80 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
81 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
82 << " bytes)\n";
Chris Lattner63381352007-08-25 01:42:24 +000083 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000084 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000085
86 llvm::errs() << "Total bytes = " << sum << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
89void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000090 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000091}
92
Daniel Dunbar02892a62012-03-05 21:42:49 +000093bool Stmt::StatisticsEnabled = false;
94void Stmt::EnableStatistics() {
95 StatisticsEnabled = true;
Reid Spencer5f016e22007-07-11 17:01:13 +000096}
97
John McCall7e5e5f42011-07-07 06:58:02 +000098Stmt *Stmt::IgnoreImplicit() {
99 Stmt *s = this;
100
101 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
102 s = ewc->getSubExpr();
103
104 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
105 s = ice->getSubExpr();
106
107 return s;
108}
109
Chandler Carrutha1364be2011-09-10 00:02:34 +0000110/// \brief Strip off all label-like statements.
111///
Richard Smith534986f2012-04-14 00:33:13 +0000112/// This will strip off label statements, case statements, attributed
113/// statements and default statements recursively.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000114const Stmt *Stmt::stripLabelLikeStatements() const {
115 const Stmt *S = this;
116 while (true) {
117 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
118 S = LS->getSubStmt();
119 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
120 S = SC->getSubStmt();
Richard Smith534986f2012-04-14 00:33:13 +0000121 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
122 S = AS->getSubStmt();
Chandler Carrutha1364be2011-09-10 00:02:34 +0000123 else
124 return S;
125 }
126}
127
John McCall63c00d72011-02-09 08:16:59 +0000128namespace {
129 struct good {};
130 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +0000131
132 // These silly little functions have to be static inline to suppress
133 // unused warnings, and they have to be defined to suppress other
134 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000135 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +0000136
137 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000138 template <class T> good implements_children(children_t T::*) {
139 return good();
140 }
141 static inline bad implements_children(children_t Stmt::*) {
142 return bad();
143 }
John McCall63c00d72011-02-09 08:16:59 +0000144
145 typedef SourceRange getSourceRange_t() const;
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000146 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
147 return good();
148 }
149 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
150 return bad();
151 }
John McCall63c00d72011-02-09 08:16:59 +0000152
153#define ASSERT_IMPLEMENTS_children(type) \
154 (void) sizeof(is_good(implements_children(&type::children)))
155#define ASSERT_IMPLEMENTS_getSourceRange(type) \
156 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
157}
158
159/// Check whether the various Stmt classes implement their member
160/// functions.
161static inline void check_implementations() {
162#define ABSTRACT_STMT(type)
163#define STMT(type, base) \
164 ASSERT_IMPLEMENTS_children(type); \
165 ASSERT_IMPLEMENTS_getSourceRange(type);
166#include "clang/AST/StmtNodes.inc"
167}
168
169Stmt::child_range Stmt::children() {
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<type*>(this)->children();
176#include "clang/AST/StmtNodes.inc"
177 }
178 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000179}
180
181SourceRange Stmt::getSourceRange() const {
182 switch (getStmtClass()) {
183 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
184#define ABSTRACT_STMT(type)
185#define STMT(type, base) \
186 case Stmt::type##Class: \
187 return static_cast<const type*>(this)->getSourceRange();
188#include "clang/AST/StmtNodes.inc"
189 }
190 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000191}
192
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000193// Amusing macro metaprogramming hack: check whether a class provides
194// a more specific implementation of getLocStart() and getLocEnd().
195//
196// See also Expr.cpp:getExprLoc().
197namespace {
198 /// This implementation is used when a class provides a custom
199 /// implementation of getLocStart.
200 template <class S, class T>
201 SourceLocation getLocStartImpl(const Stmt *stmt,
202 SourceLocation (T::*v)() const) {
203 return static_cast<const S*>(stmt)->getLocStart();
204 }
205
206 /// This implementation is used when a class doesn't provide a custom
207 /// implementation of getLocStart. Overload resolution should pick it over
208 /// the implementation above because it's more specialized according to
209 /// function template partial ordering.
210 template <class S>
211 SourceLocation getLocStartImpl(const Stmt *stmt,
212 SourceLocation (Stmt::*v)() const) {
213 return static_cast<const S*>(stmt)->getSourceRange().getBegin();
214 }
215
216 /// This implementation is used when a class provides a custom
217 /// implementation of getLocEnd.
218 template <class S, class T>
219 SourceLocation getLocEndImpl(const Stmt *stmt,
220 SourceLocation (T::*v)() const) {
221 return static_cast<const S*>(stmt)->getLocEnd();
222 }
223
224 /// This implementation is used when a class doesn't provide a custom
225 /// implementation of getLocEnd. Overload resolution should pick it over
226 /// the implementation above because it's more specialized according to
227 /// function template partial ordering.
228 template <class S>
229 SourceLocation getLocEndImpl(const Stmt *stmt,
230 SourceLocation (Stmt::*v)() const) {
231 return static_cast<const S*>(stmt)->getSourceRange().getEnd();
232 }
233}
234
235SourceLocation Stmt::getLocStart() 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 getLocStartImpl<type>(this, &type::getLocStart);
242#include "clang/AST/StmtNodes.inc"
243 }
244 llvm_unreachable("unknown statement kind");
245}
246
247SourceLocation Stmt::getLocEnd() const {
248 switch (getStmtClass()) {
249 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
250#define ABSTRACT_STMT(type)
251#define STMT(type, base) \
252 case Stmt::type##Class: \
253 return getLocEndImpl<type>(this, &type::getLocEnd);
254#include "clang/AST/StmtNodes.inc"
255 }
256 llvm_unreachable("unknown statement kind");
257}
258
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000259CompoundStmt::CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts,
260 SourceLocation LB, SourceLocation RB)
261 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
262 CompoundStmtBits.NumStmts = NumStmts;
263 assert(CompoundStmtBits.NumStmts == NumStmts &&
264 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
265
266 if (NumStmts == 0) {
267 Body = 0;
268 return;
269 }
270
271 Body = new (C) Stmt*[NumStmts];
272 memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
273}
274
Douglas Gregor025452f2009-04-17 00:04:06 +0000275void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
276 if (this->Body)
277 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000278 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000279
280 Body = new (C) Stmt*[NumStmts];
281 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
282}
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000285 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000286}
287
Alexander Kornienko49908902012-07-09 10:04:07 +0000288AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
289 ArrayRef<const Attr*> Attrs,
290 Stmt *SubStmt) {
291 void *Mem = C.Allocate(sizeof(AttributedStmt) +
292 sizeof(Attr*) * (Attrs.size() - 1),
293 llvm::alignOf<AttributedStmt>());
294 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
295}
296
297AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
298 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
299 void *Mem = C.Allocate(sizeof(AttributedStmt) +
300 sizeof(Attr*) * (NumAttrs - 1),
301 llvm::alignOf<AttributedStmt>());
302 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
303}
304
Steve Naroff507f2d52007-08-31 23:49:30 +0000305// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000306SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000307 if (RetExpr)
308 return SourceRange(RetLoc, RetExpr->getLocEnd());
309 else
310 return SourceRange(RetLoc);
311}
312
Ted Kremenekd48ade62007-10-01 16:34:52 +0000313bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000314 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000315 default:
316 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000317
Ted Kremenekd48ade62007-10-01 16:34:52 +0000318 case CallExprClass:
319 case ConditionalOperatorClass:
320 case ChooseExprClass:
321 case StmtExprClass:
322 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000323 return true;
324
Ted Kremenekd48ade62007-10-01 16:34:52 +0000325 case Stmt::BinaryOperatorClass: {
326 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000327 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000328 return true;
329 else
330 return false;
331 }
332 }
333}
334
Chad Rosieraba59aa2012-08-28 17:43:23 +0000335std::string AsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000336 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
337 return gccAsmStmt->generateAsmString(C);
338 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
339 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000340 llvm_unreachable("unknown asm statement kind!");
341}
342
343StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000344 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
345 return gccAsmStmt->getOutputConstraint(i);
346 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
347 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000348 llvm_unreachable("unknown asm statement kind!");
349}
350
351const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000352 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
353 return gccAsmStmt->getOutputExpr(i);
354 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
355 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000356 llvm_unreachable("unknown asm statement kind!");
357}
358
359StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000360 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
361 return gccAsmStmt->getInputConstraint(i);
362 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
363 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000364 llvm_unreachable("unknown asm statement kind!");
365}
366
367const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000368 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
369 return gccAsmStmt->getInputExpr(i);
370 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
371 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000372 llvm_unreachable("unknown asm statement kind!");
373}
374
375StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000376 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
377 return gccAsmStmt->getClobber(i);
378 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
379 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000380 llvm_unreachable("unknown asm statement kind!");
381}
382
Chad Rosierc4fb2212012-08-28 00:24:05 +0000383/// getNumPlusOperands - Return the number of output operands that have a "+"
384/// constraint.
385unsigned AsmStmt::getNumPlusOperands() const {
386 unsigned Res = 0;
387 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
388 if (isOutputPlusConstraint(i))
389 ++Res;
390 return Res;
391}
392
Chad Rosier33f05582012-08-27 23:47:56 +0000393StringRef GCCAsmStmt::getClobber(unsigned i) const {
394 return getClobberStringLiteral(i)->getString();
395}
396
Chad Rosierdf5faf52012-08-25 00:11:56 +0000397Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000398 return cast<Expr>(Exprs[i]);
399}
Chris Lattnerb3277932009-03-10 04:59:06 +0000400
401/// getOutputConstraint - Return the constraint string for the specified
402/// output operand. All output constraints are known to be non-empty (either
403/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000404StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000405 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000406}
Chris Lattnerb3277932009-03-10 04:59:06 +0000407
Chad Rosierdf5faf52012-08-25 00:11:56 +0000408Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000409 return cast<Expr>(Exprs[i + NumOutputs]);
410}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000411void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000412 Exprs[i + NumOutputs] = E;
413}
414
Chris Lattnerb3277932009-03-10 04:59:06 +0000415/// getInputConstraint - Return the specified input constraint. Unlike output
416/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000417StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000418 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000419}
420
Chad Rosierdf5faf52012-08-25 00:11:56 +0000421void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000422 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000423 StringLiteral **Constraints,
424 Stmt **Exprs,
425 unsigned NumOutputs,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000426 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000427 StringLiteral **Clobbers,
428 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000429 this->NumOutputs = NumOutputs;
430 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000431 this->NumClobbers = NumClobbers;
432
433 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000434
Anders Carlsson966146e2010-01-30 23:19:41 +0000435 C.Deallocate(this->Names);
436 this->Names = new (C) IdentifierInfo*[NumExprs];
437 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000438
Anders Carlsson966146e2010-01-30 23:19:41 +0000439 C.Deallocate(this->Exprs);
440 this->Exprs = new (C) Stmt*[NumExprs];
441 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000442
Anders Carlsson966146e2010-01-30 23:19:41 +0000443 C.Deallocate(this->Constraints);
444 this->Constraints = new (C) StringLiteral*[NumExprs];
445 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000446
Anders Carlsson966146e2010-01-30 23:19:41 +0000447 C.Deallocate(this->Clobbers);
448 this->Clobbers = new (C) StringLiteral*[NumClobbers];
449 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000450}
451
Chris Lattner10ca96a2009-03-10 06:33:24 +0000452/// getNamedOperand - Given a symbolic operand reference like %[foo],
453/// translate this into a numeric value needed to reference the same operand.
454/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000455int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000456 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattner10ca96a2009-03-10 06:33:24 +0000458 // Check if this is an output operand.
459 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
460 if (getOutputName(i) == SymbolicName)
461 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000462 }
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattner10ca96a2009-03-10 06:33:24 +0000464 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
465 if (getInputName(i) == SymbolicName)
466 return getNumOutputs() + NumPlusOperands + i;
467
468 // Not found.
469 return -1;
470}
471
Chris Lattner458cd9c2009-03-10 23:21:44 +0000472/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
473/// it into pieces. If the asm string is erroneous, emit errors and return
474/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000475unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000476 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000477 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000478 const char *StrStart = Str.begin();
479 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000480 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattner458cd9c2009-03-10 23:21:44 +0000482 // "Simple" inline asms have no constraints or operands, just convert the asm
483 // string to escape $'s.
484 if (isSimple()) {
485 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000486 for (; CurPtr != StrEnd; ++CurPtr) {
487 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000488 case '$':
489 Result += "$$";
490 break;
491 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000492 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000493 break;
494 }
495 }
496 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000497 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000498 }
499
500 // CurStringPiece - The current string that we are building up as we scan the
501 // asm string.
502 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000504 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000505
Chris Lattner458cd9c2009-03-10 23:21:44 +0000506 while (1) {
507 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000508 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000509 if (!CurStringPiece.empty())
510 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000511 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattner3182db12009-03-10 23:51:40 +0000514 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000515 switch (CurChar) {
516 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000517 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
518 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
519 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000520 case '%':
521 break;
522 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000523 CurStringPiece += CurChar;
524 continue;
525 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000526
Chris Lattner458cd9c2009-03-10 23:21:44 +0000527 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000528 if (CurPtr == StrEnd) {
529 // % at end of string is invalid (no escape).
530 DiagOffs = CurPtr-StrStart-1;
531 return diag::err_asm_invalid_escape;
532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattner3182db12009-03-10 23:51:40 +0000534 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000535 if (EscapedChar == '%') { // %% -> %
536 // Escaped percentage sign.
537 CurStringPiece += '%';
538 continue;
539 }
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattner458cd9c2009-03-10 23:21:44 +0000541 if (EscapedChar == '=') { // %= -> Generate an unique ID.
542 CurStringPiece += "${:uid}";
543 continue;
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattner458cd9c2009-03-10 23:21:44 +0000546 // Otherwise, we have an operand. If we have accumulated a string so far,
547 // add it to the Pieces list.
548 if (!CurStringPiece.empty()) {
549 Pieces.push_back(AsmStringPiece(CurStringPiece));
550 CurStringPiece.clear();
551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattner458cd9c2009-03-10 23:21:44 +0000553 // Handle %x4 and %x[foo] by capturing x as the modifier character.
554 char Modifier = '\0';
555 if (isalpha(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000556 if (CurPtr == StrEnd) { // Premature end.
557 DiagOffs = CurPtr-StrStart-1;
558 return diag::err_asm_invalid_escape;
559 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000560 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000561 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000562 }
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Chris Lattner458cd9c2009-03-10 23:21:44 +0000564 if (isdigit(EscapedChar)) {
565 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000566 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattnercafc2222009-03-11 22:52:17 +0000568 --CurPtr;
569 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000570 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattner85759272009-03-11 00:23:13 +0000572 unsigned NumOperands =
573 getNumOutputs() + getNumPlusOperands() + getNumInputs();
574 if (N >= NumOperands) {
575 DiagOffs = CurPtr-StrStart-1;
576 return diag::err_asm_invalid_operand_number;
577 }
578
Chris Lattner458cd9c2009-03-10 23:21:44 +0000579 Pieces.push_back(AsmStringPiece(N, Modifier));
580 continue;
581 }
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Chris Lattner458cd9c2009-03-10 23:21:44 +0000583 // Handle %[foo], a symbolic operand reference.
584 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000585 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000587 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000588 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000589 if (NameEnd == 0)
590 return diag::err_asm_unterminated_symbolic_operand_name;
591 if (NameEnd == CurPtr)
592 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Chris Lattner5f9e2722011-07-23 10:55:15 +0000594 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Chris Lattner458cd9c2009-03-10 23:21:44 +0000596 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000597 if (N == -1) {
598 // Verify that an operand with that name exists.
599 DiagOffs = CurPtr-StrStart;
600 return diag::err_asm_unknown_symbolic_operand_name;
601 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000602 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000604 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000605 continue;
606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Chris Lattner2ff0f422009-03-10 23:57:07 +0000608 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000609 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000610 }
611}
Chad Rosierda083b22012-08-27 20:23:31 +0000612
613/// Assemble final IR asm string (GCC-style).
614std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000615 // Analyze the asm string to decompose it into its pieces. We know that Sema
616 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000617 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000618 unsigned DiagOffs;
619 AnalyzeAsmString(Pieces, C, DiagOffs);
620
621 std::string AsmString;
622 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
623 if (Pieces[i].isString())
624 AsmString += Pieces[i].getString();
625 else if (Pieces[i].getModifier() == '\0')
626 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
627 else
628 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
629 Pieces[i].getModifier() + '}';
630 }
631 return AsmString;
632}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000633
Chad Rosierda083b22012-08-27 20:23:31 +0000634/// Assemble final IR asm string (MS-style).
635std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
636 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000637 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000638}
639
Chad Rosier633abb02012-08-24 00:07:09 +0000640Expr *MSAsmStmt::getOutputExpr(unsigned i) {
641 return cast<Expr>(Exprs[i]);
642}
643
644Expr *MSAsmStmt::getInputExpr(unsigned i) {
645 return cast<Expr>(Exprs[i + NumOutputs]);
646}
647void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
648 Exprs[i + NumOutputs] = E;
649}
650
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000651QualType CXXCatchStmt::getCaughtType() const {
652 if (ExceptionDecl)
653 return ExceptionDecl->getType();
654 return QualType();
655}
656
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000657//===----------------------------------------------------------------------===//
658// Constructors
659//===----------------------------------------------------------------------===//
660
Chad Rosierdf5faf52012-08-25 00:11:56 +0000661GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
662 bool isvolatile, unsigned numoutputs, unsigned numinputs,
663 IdentifierInfo **names, StringLiteral **constraints,
664 Expr **exprs, StringLiteral *asmstr,
665 unsigned numclobbers, StringLiteral **clobbers,
666 SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000667 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
668 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000669
Chad Rosier0e2a8682012-08-07 23:12:23 +0000670 unsigned NumExprs = NumOutputs + NumInputs;
671
Anders Carlsson966146e2010-01-30 23:19:41 +0000672 Names = new (C) IdentifierInfo*[NumExprs];
673 std::copy(names, names + NumExprs, Names);
674
675 Exprs = new (C) Stmt*[NumExprs];
676 std::copy(exprs, exprs + NumExprs, Exprs);
677
678 Constraints = new (C) StringLiteral*[NumExprs];
679 std::copy(constraints, constraints + NumExprs, Constraints);
680
681 Clobbers = new (C) StringLiteral*[NumClobbers];
682 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000683}
684
Chad Rosier058ab172012-08-16 00:06:53 +0000685MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
686 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosiere54cba12012-10-16 21:55:39 +0000687 ArrayRef<Token> asmtoks, unsigned numoutputs,
688 unsigned numinputs, ArrayRef<IdentifierInfo*> names,
689 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
690 StringRef asmstr, ArrayRef<StringRef> clobbers,
691 SourceLocation endloc)
692 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
693 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
Chad Rosier12603e22012-09-04 16:36:26 +0000694 EndLoc(endloc), AsmStr(asmstr.str()), NumAsmToks(asmtoks.size()) {
Chad Rosier058ab172012-08-16 00:06:53 +0000695
696 unsigned NumExprs = NumOutputs + NumInputs;
697
698 Names = new (C) IdentifierInfo*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000699 for (unsigned i = 0, e = NumExprs; i != e; ++i)
700 Names[i] = names[i];
Chad Rosier79efe242012-08-07 00:29:06 +0000701
Chad Rosier633abb02012-08-24 00:07:09 +0000702 Exprs = new (C) Stmt*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000703 for (unsigned i = 0, e = NumExprs; i != e; ++i)
704 Exprs[i] = exprs[i];
Chad Rosier633abb02012-08-24 00:07:09 +0000705
Chad Rosier79efe242012-08-07 00:29:06 +0000706 AsmToks = new (C) Token[NumAsmToks];
707 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
708 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000709
Chad Rosier89fb6d72012-08-28 20:28:20 +0000710 Constraints = new (C) StringRef[NumExprs];
711 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
712 size_t size = constraints[i].size();
713 char *dest = new (C) char[size];
714 std::strncpy(dest, constraints[i].data(), size);
715 Constraints[i] = StringRef(dest, size);
716 }
717
Chad Rosier33c72e12012-08-10 21:36:25 +0000718 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000719 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
720 // FIXME: Avoid the allocation/copy if at all possible.
721 size_t size = clobbers[i].size();
722 char *dest = new (C) char[size];
723 std::strncpy(dest, clobbers[i].data(), size);
Chad Rosier33c72e12012-08-10 21:36:25 +0000724 Clobbers[i] = StringRef(dest, size);
Chad Rosiere790bc32012-08-10 21:06:19 +0000725 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000726}
727
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000728ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
729 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000730 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000731: Stmt(ObjCForCollectionStmtClass) {
732 SubExprs[ELEM] = Elem;
733 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
734 SubExprs[BODY] = Body;
735 ForLoc = FCL;
736 RParenLoc = RPL;
737}
738
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000739ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
740 Stmt **CatchStmts, unsigned NumCatchStmts,
741 Stmt *atFinallyStmt)
742 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
743 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
744{
745 Stmt **Stmts = getStmts();
746 Stmts[0] = atTryStmt;
747 for (unsigned I = 0; I != NumCatchStmts; ++I)
748 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000749
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000750 if (HasFinally)
751 Stmts[NumCatchStmts + 1] = atFinallyStmt;
752}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000753
Chad Rosier0e2a8682012-08-07 23:12:23 +0000754ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
755 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000756 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000757 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000758 unsigned NumCatchStmts,
759 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000760 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000761 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000762 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000763 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
764 atFinallyStmt);
765}
Ted Kremenekff981022008-02-01 21:28:59 +0000766
Chad Rosier0e2a8682012-08-07 23:12:23 +0000767ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000768 unsigned NumCatchStmts,
769 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000770 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000771 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000772 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000773 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000774}
Nico Weber608b17f2008-08-05 23:15:29 +0000775
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000776SourceRange ObjCAtTryStmt::getSourceRange() const {
777 SourceLocation EndLoc;
778 if (HasFinally)
779 EndLoc = getFinallyStmt()->getLocEnd();
780 else if (NumCatchStmts)
781 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
782 else
783 EndLoc = getTryBody()->getLocEnd();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000784
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000785 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000786}
787
Sam Weiniga1a396d2010-02-03 03:56:39 +0000788CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000789 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000790 unsigned numHandlers) {
791 std::size_t Size = sizeof(CXXTryStmt);
792 Size += ((numHandlers + 1) * sizeof(Stmt));
793
Chris Lattner32488542010-10-30 05:14:06 +0000794 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000795 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
796}
797
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000798CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
799 unsigned numHandlers) {
800 std::size_t Size = sizeof(CXXTryStmt);
801 Size += ((numHandlers + 1) * sizeof(Stmt));
802
Chris Lattner32488542010-10-30 05:14:06 +0000803 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000804 return new (Mem) CXXTryStmt(Empty, numHandlers);
805}
806
Sam Weiniga1a396d2010-02-03 03:56:39 +0000807CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000808 Stmt **handlers, unsigned numHandlers)
809 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000810 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000811 Stmts[0] = tryBlock;
812 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
813}
814
Richard Smithad762fc2011-04-14 22:09:26 +0000815CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
816 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
817 Stmt *Body, SourceLocation FL,
818 SourceLocation CL, SourceLocation RPL)
819 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
820 SubExprs[RANGE] = Range;
821 SubExprs[BEGINEND] = BeginEndStmt;
822 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
823 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
824 SubExprs[LOOPVAR] = LoopVar;
825 SubExprs[BODY] = Body;
826}
827
828Expr *CXXForRangeStmt::getRangeInit() {
829 DeclStmt *RangeStmt = getRangeStmt();
830 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
831 assert(RangeDecl &&& "for-range should have a single var decl");
832 return RangeDecl->getInit();
833}
834
835const Expr *CXXForRangeStmt::getRangeInit() const {
836 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
837}
838
839VarDecl *CXXForRangeStmt::getLoopVariable() {
840 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
841 assert(LV && "No loop variable in CXXForRangeStmt");
842 return cast<VarDecl>(LV);
843}
844
845const VarDecl *CXXForRangeStmt::getLoopVariable() const {
846 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
847}
848
Chad Rosier0e2a8682012-08-07 23:12:23 +0000849IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000850 Stmt *then, SourceLocation EL, Stmt *elsev)
851 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000852{
853 setConditionVariable(C, var);
854 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
855 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000856 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000857}
858
859VarDecl *IfStmt::getConditionVariable() const {
860 if (!SubExprs[VAR])
861 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000862
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000863 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
864 return cast<VarDecl>(DS->getSingleDecl());
865}
866
867void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
868 if (!V) {
869 SubExprs[VAR] = 0;
870 return;
871 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000872
Daniel Dunbar96a00142012-03-09 18:35:03 +0000873 SourceRange VarRange = V->getSourceRange();
874 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
875 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000876}
877
Chad Rosier0e2a8682012-08-07 23:12:23 +0000878ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
879 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000880 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000881 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000882{
883 SubExprs[INIT] = Init;
884 setConditionVariable(C, condVar);
885 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
886 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
887 SubExprs[BODY] = Body;
888}
889
890VarDecl *ForStmt::getConditionVariable() const {
891 if (!SubExprs[CONDVAR])
892 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000893
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000894 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
895 return cast<VarDecl>(DS->getSingleDecl());
896}
897
898void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
899 if (!V) {
900 SubExprs[CONDVAR] = 0;
901 return;
902 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000903
Daniel Dunbar96a00142012-03-09 18:35:03 +0000904 SourceRange VarRange = V->getSourceRange();
905 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
906 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000907}
908
Chad Rosier0e2a8682012-08-07 23:12:23 +0000909SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
910 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000911{
912 setConditionVariable(C, Var);
913 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
914 SubExprs[BODY] = NULL;
915}
916
917VarDecl *SwitchStmt::getConditionVariable() const {
918 if (!SubExprs[VAR])
919 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000920
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000921 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
922 return cast<VarDecl>(DS->getSingleDecl());
923}
924
925void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
926 if (!V) {
927 SubExprs[VAR] = 0;
928 return;
929 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000930
Daniel Dunbar96a00142012-03-09 18:35:03 +0000931 SourceRange VarRange = V->getSourceRange();
932 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
933 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000934}
935
John McCall63c00d72011-02-09 08:16:59 +0000936Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000937 if (isa<CaseStmt>(this))
938 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000939 return cast<DefaultStmt>(this)->getSubStmt();
940}
941
Chad Rosier0e2a8682012-08-07 23:12:23 +0000942WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000943 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000944 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000945 setConditionVariable(C, Var);
946 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
947 SubExprs[BODY] = body;
948 WhileLoc = WL;
949}
950
951VarDecl *WhileStmt::getConditionVariable() const {
952 if (!SubExprs[VAR])
953 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000954
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000955 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
956 return cast<VarDecl>(DS->getSingleDecl());
957}
958
959void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
960 if (!V) {
961 SubExprs[VAR] = 0;
962 return;
963 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000964
965 SourceRange VarRange = V->getSourceRange();
966 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
967 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000968}
969
Ted Kremenek82977772007-08-24 21:09:09 +0000970// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000971LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000972 if (AddrLabelExpr *E =
973 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
974 return E->getLabel();
975 return 0;
976}
Ted Kremenek82977772007-08-24 21:09:09 +0000977
Ted Kremenek82977772007-08-24 21:09:09 +0000978// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000979const Expr* ReturnStmt::getRetValue() const {
980 return cast_or_null<Expr>(RetExpr);
981}
982Expr* ReturnStmt::getRetValue() {
983 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000984}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000985
986SEHTryStmt::SEHTryStmt(bool IsCXXTry,
987 SourceLocation TryLoc,
988 Stmt *TryBlock,
989 Stmt *Handler)
990 : Stmt(SEHTryStmtClass),
991 IsCXXTry(IsCXXTry),
992 TryLoc(TryLoc)
993{
994 Children[TRY] = TryBlock;
995 Children[HANDLER] = Handler;
996}
997
998SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
999 bool IsCXXTry,
1000 SourceLocation TryLoc,
1001 Stmt *TryBlock,
1002 Stmt *Handler) {
1003 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
1004}
1005
1006SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1007 return dyn_cast<SEHExceptStmt>(getHandler());
1008}
1009
1010SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1011 return dyn_cast<SEHFinallyStmt>(getHandler());
1012}
1013
1014SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
1015 Expr *FilterExpr,
1016 Stmt *Block)
1017 : Stmt(SEHExceptStmtClass),
1018 Loc(Loc)
1019{
1020 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
1021 Children[BLOCK] = Block;
1022}
1023
1024SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
1025 SourceLocation Loc,
1026 Expr *FilterExpr,
1027 Stmt *Block) {
1028 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1029}
1030
1031SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1032 Stmt *Block)
1033 : Stmt(SEHFinallyStmtClass),
1034 Loc(Loc),
1035 Block(Block)
1036{}
1037
1038SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
1039 SourceLocation Loc,
1040 Stmt *Block) {
1041 return new(C)SEHFinallyStmt(Loc,Block);
1042}