blob: 1ef80c95c40666f176b4f18839c47f65776f786e [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
Erik Verbruggen65d78312012-12-25 14:51:39 +0000145 typedef SourceLocation getLocStart_t() const;
146 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000147 return good();
148 }
Erik Verbruggen65d78312012-12-25 14:51:39 +0000149 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
150 return bad();
151 }
152
153 typedef SourceLocation getLocEnd_t() const;
154 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
155 return good();
156 }
157 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000158 return bad();
159 }
John McCall63c00d72011-02-09 08:16:59 +0000160
161#define ASSERT_IMPLEMENTS_children(type) \
162 (void) sizeof(is_good(implements_children(&type::children)))
Erik Verbruggen65d78312012-12-25 14:51:39 +0000163#define ASSERT_IMPLEMENTS_getLocStart(type) \
164 (void) sizeof(is_good(implements_getLocStart(&type::getLocStart)))
165#define ASSERT_IMPLEMENTS_getLocEnd(type) \
166 (void) sizeof(is_good(implements_getLocEnd(&type::getLocEnd)))
John McCall63c00d72011-02-09 08:16:59 +0000167}
168
169/// Check whether the various Stmt classes implement their member
170/// functions.
171static inline void check_implementations() {
172#define ABSTRACT_STMT(type)
173#define STMT(type, base) \
174 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000175 ASSERT_IMPLEMENTS_getLocStart(type); \
176 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCall63c00d72011-02-09 08:16:59 +0000177#include "clang/AST/StmtNodes.inc"
178}
179
180Stmt::child_range Stmt::children() {
181 switch (getStmtClass()) {
182 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
183#define ABSTRACT_STMT(type)
184#define STMT(type, base) \
185 case Stmt::type##Class: \
186 return static_cast<type*>(this)->children();
187#include "clang/AST/StmtNodes.inc"
188 }
189 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000190}
191
Erik Verbruggen65d78312012-12-25 14:51:39 +0000192// Amusing macro metaprogramming hack: check whether a class provides
193// a more specific implementation of getSourceRange.
194//
195// See also Expr.cpp:getExprLoc().
196namespace {
197 /// This implementation is used when a class provides a custom
198 /// implementation of getSourceRange.
199 template <class S, class T>
200 SourceRange getSourceRangeImpl(const Stmt *stmt,
201 SourceRange (T::*v)() const) {
202 return static_cast<const S*>(stmt)->getSourceRange();
203 }
204
205 /// This implementation is used when a class doesn't provide a custom
206 /// implementation of getSourceRange. Overload resolution should pick it over
207 /// the implementation above because it's more specialized according to
208 /// function template partial ordering.
209 template <class S>
210 SourceRange getSourceRangeImpl(const Stmt *stmt,
211 SourceRange (Stmt::*v)() const) {
212 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
213 static_cast<const S*>(stmt)->getLocEnd());
214 }
215}
216
John McCall63c00d72011-02-09 08:16:59 +0000217SourceRange Stmt::getSourceRange() const {
218 switch (getStmtClass()) {
219 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
220#define ABSTRACT_STMT(type)
221#define STMT(type, base) \
222 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000223 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCall63c00d72011-02-09 08:16:59 +0000224#include "clang/AST/StmtNodes.inc"
225 }
226 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000227}
228
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000229SourceLocation Stmt::getLocStart() const {
Erik Verbruggen65d78312012-12-25 14:51:39 +0000230// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000231 switch (getStmtClass()) {
232 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
233#define ABSTRACT_STMT(type)
234#define STMT(type, base) \
235 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000236 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000237#include "clang/AST/StmtNodes.inc"
238 }
239 llvm_unreachable("unknown statement kind");
240}
241
242SourceLocation Stmt::getLocEnd() const {
243 switch (getStmtClass()) {
244 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
245#define ABSTRACT_STMT(type)
246#define STMT(type, base) \
247 case Stmt::type##Class: \
Erik Verbruggen65d78312012-12-25 14:51:39 +0000248 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000249#include "clang/AST/StmtNodes.inc"
250 }
251 llvm_unreachable("unknown statement kind");
252}
253
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000254CompoundStmt::CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts,
255 SourceLocation LB, SourceLocation RB)
256 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
257 CompoundStmtBits.NumStmts = NumStmts;
258 assert(CompoundStmtBits.NumStmts == NumStmts &&
259 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
260
261 if (NumStmts == 0) {
262 Body = 0;
263 return;
264 }
265
266 Body = new (C) Stmt*[NumStmts];
267 memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
268}
269
Douglas Gregor025452f2009-04-17 00:04:06 +0000270void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
271 if (this->Body)
272 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000273 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000274
275 Body = new (C) Stmt*[NumStmts];
276 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
277}
Reid Spencer5f016e22007-07-11 17:01:13 +0000278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000280 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000281}
282
Alexander Kornienko49908902012-07-09 10:04:07 +0000283AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
284 ArrayRef<const Attr*> Attrs,
285 Stmt *SubStmt) {
286 void *Mem = C.Allocate(sizeof(AttributedStmt) +
287 sizeof(Attr*) * (Attrs.size() - 1),
288 llvm::alignOf<AttributedStmt>());
289 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
290}
291
292AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
293 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
294 void *Mem = C.Allocate(sizeof(AttributedStmt) +
295 sizeof(Attr*) * (NumAttrs - 1),
296 llvm::alignOf<AttributedStmt>());
297 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
298}
299
Ted Kremenekd48ade62007-10-01 16:34:52 +0000300bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000301 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000302 default:
303 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000304
Ted Kremenekd48ade62007-10-01 16:34:52 +0000305 case CallExprClass:
306 case ConditionalOperatorClass:
307 case ChooseExprClass:
308 case StmtExprClass:
309 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000310 return true;
311
Ted Kremenekd48ade62007-10-01 16:34:52 +0000312 case Stmt::BinaryOperatorClass: {
313 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000314 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000315 return true;
316 else
317 return false;
318 }
319 }
320}
321
Chad Rosieraba59aa2012-08-28 17:43:23 +0000322std::string AsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000323 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
324 return gccAsmStmt->generateAsmString(C);
325 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
326 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000327 llvm_unreachable("unknown asm statement kind!");
328}
329
330StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000331 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
332 return gccAsmStmt->getOutputConstraint(i);
333 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
334 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000335 llvm_unreachable("unknown asm statement kind!");
336}
337
338const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000339 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
340 return gccAsmStmt->getOutputExpr(i);
341 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
342 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000343 llvm_unreachable("unknown asm statement kind!");
344}
345
346StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000347 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
348 return gccAsmStmt->getInputConstraint(i);
349 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
350 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000351 llvm_unreachable("unknown asm statement kind!");
352}
353
354const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000355 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
356 return gccAsmStmt->getInputExpr(i);
357 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
358 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000359 llvm_unreachable("unknown asm statement kind!");
360}
361
362StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000363 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
364 return gccAsmStmt->getClobber(i);
365 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
366 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000367 llvm_unreachable("unknown asm statement kind!");
368}
369
Chad Rosierc4fb2212012-08-28 00:24:05 +0000370/// getNumPlusOperands - Return the number of output operands that have a "+"
371/// constraint.
372unsigned AsmStmt::getNumPlusOperands() const {
373 unsigned Res = 0;
374 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
375 if (isOutputPlusConstraint(i))
376 ++Res;
377 return Res;
378}
379
Chad Rosier33f05582012-08-27 23:47:56 +0000380StringRef GCCAsmStmt::getClobber(unsigned i) const {
381 return getClobberStringLiteral(i)->getString();
382}
383
Chad Rosierdf5faf52012-08-25 00:11:56 +0000384Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000385 return cast<Expr>(Exprs[i]);
386}
Chris Lattnerb3277932009-03-10 04:59:06 +0000387
388/// getOutputConstraint - Return the constraint string for the specified
389/// output operand. All output constraints are known to be non-empty (either
390/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000391StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000392 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000393}
Chris Lattnerb3277932009-03-10 04:59:06 +0000394
Chad Rosierdf5faf52012-08-25 00:11:56 +0000395Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000396 return cast<Expr>(Exprs[i + NumOutputs]);
397}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000398void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000399 Exprs[i + NumOutputs] = E;
400}
401
Chris Lattnerb3277932009-03-10 04:59:06 +0000402/// getInputConstraint - Return the specified input constraint. Unlike output
403/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000404StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000405 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000406}
407
Chad Rosierdf5faf52012-08-25 00:11:56 +0000408void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000409 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000410 StringLiteral **Constraints,
411 Stmt **Exprs,
412 unsigned NumOutputs,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000413 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000414 StringLiteral **Clobbers,
415 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000416 this->NumOutputs = NumOutputs;
417 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000418 this->NumClobbers = NumClobbers;
419
420 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000421
Anders Carlsson966146e2010-01-30 23:19:41 +0000422 C.Deallocate(this->Names);
423 this->Names = new (C) IdentifierInfo*[NumExprs];
424 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000425
Anders Carlsson966146e2010-01-30 23:19:41 +0000426 C.Deallocate(this->Exprs);
427 this->Exprs = new (C) Stmt*[NumExprs];
428 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000429
Anders Carlsson966146e2010-01-30 23:19:41 +0000430 C.Deallocate(this->Constraints);
431 this->Constraints = new (C) StringLiteral*[NumExprs];
432 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000433
Anders Carlsson966146e2010-01-30 23:19:41 +0000434 C.Deallocate(this->Clobbers);
435 this->Clobbers = new (C) StringLiteral*[NumClobbers];
436 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000437}
438
Chris Lattner10ca96a2009-03-10 06:33:24 +0000439/// getNamedOperand - Given a symbolic operand reference like %[foo],
440/// translate this into a numeric value needed to reference the same operand.
441/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000442int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000443 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner10ca96a2009-03-10 06:33:24 +0000445 // Check if this is an output operand.
446 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
447 if (getOutputName(i) == SymbolicName)
448 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner10ca96a2009-03-10 06:33:24 +0000451 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
452 if (getInputName(i) == SymbolicName)
453 return getNumOutputs() + NumPlusOperands + i;
454
455 // Not found.
456 return -1;
457}
458
Chris Lattner458cd9c2009-03-10 23:21:44 +0000459/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
460/// it into pieces. If the asm string is erroneous, emit errors and return
461/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000462unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000463 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000464 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000465 const char *StrStart = Str.begin();
466 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000467 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Chris Lattner458cd9c2009-03-10 23:21:44 +0000469 // "Simple" inline asms have no constraints or operands, just convert the asm
470 // string to escape $'s.
471 if (isSimple()) {
472 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000473 for (; CurPtr != StrEnd; ++CurPtr) {
474 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000475 case '$':
476 Result += "$$";
477 break;
478 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000479 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000480 break;
481 }
482 }
483 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000484 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000485 }
486
487 // CurStringPiece - The current string that we are building up as we scan the
488 // asm string.
489 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000491 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000492
Chris Lattner458cd9c2009-03-10 23:21:44 +0000493 while (1) {
494 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000495 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000496 if (!CurStringPiece.empty())
497 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000498 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Chris Lattner3182db12009-03-10 23:51:40 +0000501 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000502 switch (CurChar) {
503 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000504 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
505 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
506 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000507 case '%':
508 break;
509 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000510 CurStringPiece += CurChar;
511 continue;
512 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000513
Chris Lattner458cd9c2009-03-10 23:21:44 +0000514 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000515 if (CurPtr == StrEnd) {
516 // % at end of string is invalid (no escape).
517 DiagOffs = CurPtr-StrStart-1;
518 return diag::err_asm_invalid_escape;
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner3182db12009-03-10 23:51:40 +0000521 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000522 if (EscapedChar == '%') { // %% -> %
523 // Escaped percentage sign.
524 CurStringPiece += '%';
525 continue;
526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Chris Lattner458cd9c2009-03-10 23:21:44 +0000528 if (EscapedChar == '=') { // %= -> Generate an unique ID.
529 CurStringPiece += "${:uid}";
530 continue;
531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattner458cd9c2009-03-10 23:21:44 +0000533 // Otherwise, we have an operand. If we have accumulated a string so far,
534 // add it to the Pieces list.
535 if (!CurStringPiece.empty()) {
536 Pieces.push_back(AsmStringPiece(CurStringPiece));
537 CurStringPiece.clear();
538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattner458cd9c2009-03-10 23:21:44 +0000540 // Handle %x4 and %x[foo] by capturing x as the modifier character.
541 char Modifier = '\0';
542 if (isalpha(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000543 if (CurPtr == StrEnd) { // Premature end.
544 DiagOffs = CurPtr-StrStart-1;
545 return diag::err_asm_invalid_escape;
546 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000547 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000548 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner458cd9c2009-03-10 23:21:44 +0000551 if (isdigit(EscapedChar)) {
552 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000553 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattnercafc2222009-03-11 22:52:17 +0000555 --CurPtr;
556 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000557 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner85759272009-03-11 00:23:13 +0000559 unsigned NumOperands =
560 getNumOutputs() + getNumPlusOperands() + getNumInputs();
561 if (N >= NumOperands) {
562 DiagOffs = CurPtr-StrStart-1;
563 return diag::err_asm_invalid_operand_number;
564 }
565
Chris Lattner458cd9c2009-03-10 23:21:44 +0000566 Pieces.push_back(AsmStringPiece(N, Modifier));
567 continue;
568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner458cd9c2009-03-10 23:21:44 +0000570 // Handle %[foo], a symbolic operand reference.
571 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000572 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000574 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000575 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000576 if (NameEnd == 0)
577 return diag::err_asm_unterminated_symbolic_operand_name;
578 if (NameEnd == CurPtr)
579 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattner5f9e2722011-07-23 10:55:15 +0000581 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Chris Lattner458cd9c2009-03-10 23:21:44 +0000583 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000584 if (N == -1) {
585 // Verify that an operand with that name exists.
586 DiagOffs = CurPtr-StrStart;
587 return diag::err_asm_unknown_symbolic_operand_name;
588 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000589 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000591 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000592 continue;
593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Chris Lattner2ff0f422009-03-10 23:57:07 +0000595 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000596 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000597 }
598}
Chad Rosierda083b22012-08-27 20:23:31 +0000599
600/// Assemble final IR asm string (GCC-style).
601std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000602 // Analyze the asm string to decompose it into its pieces. We know that Sema
603 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000604 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000605 unsigned DiagOffs;
606 AnalyzeAsmString(Pieces, C, DiagOffs);
607
608 std::string AsmString;
609 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
610 if (Pieces[i].isString())
611 AsmString += Pieces[i].getString();
612 else if (Pieces[i].getModifier() == '\0')
613 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
614 else
615 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
616 Pieces[i].getModifier() + '}';
617 }
618 return AsmString;
619}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000620
Chad Rosierda083b22012-08-27 20:23:31 +0000621/// Assemble final IR asm string (MS-style).
622std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
623 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000624 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000625}
626
Chad Rosier633abb02012-08-24 00:07:09 +0000627Expr *MSAsmStmt::getOutputExpr(unsigned i) {
628 return cast<Expr>(Exprs[i]);
629}
630
631Expr *MSAsmStmt::getInputExpr(unsigned i) {
632 return cast<Expr>(Exprs[i + NumOutputs]);
633}
634void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
635 Exprs[i + NumOutputs] = E;
636}
637
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000638QualType CXXCatchStmt::getCaughtType() const {
639 if (ExceptionDecl)
640 return ExceptionDecl->getType();
641 return QualType();
642}
643
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000644//===----------------------------------------------------------------------===//
645// Constructors
646//===----------------------------------------------------------------------===//
647
Chad Rosierdf5faf52012-08-25 00:11:56 +0000648GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
649 bool isvolatile, unsigned numoutputs, unsigned numinputs,
650 IdentifierInfo **names, StringLiteral **constraints,
651 Expr **exprs, StringLiteral *asmstr,
652 unsigned numclobbers, StringLiteral **clobbers,
653 SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000654 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
655 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000656
Chad Rosier0e2a8682012-08-07 23:12:23 +0000657 unsigned NumExprs = NumOutputs + NumInputs;
658
Anders Carlsson966146e2010-01-30 23:19:41 +0000659 Names = new (C) IdentifierInfo*[NumExprs];
660 std::copy(names, names + NumExprs, Names);
661
662 Exprs = new (C) Stmt*[NumExprs];
663 std::copy(exprs, exprs + NumExprs, Exprs);
664
665 Constraints = new (C) StringLiteral*[NumExprs];
666 std::copy(constraints, constraints + NumExprs, Constraints);
667
668 Clobbers = new (C) StringLiteral*[NumClobbers];
669 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000670}
671
Chad Rosier058ab172012-08-16 00:06:53 +0000672MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
673 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosiere54cba12012-10-16 21:55:39 +0000674 ArrayRef<Token> asmtoks, unsigned numoutputs,
675 unsigned numinputs, ArrayRef<IdentifierInfo*> names,
676 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
677 StringRef asmstr, ArrayRef<StringRef> clobbers,
678 SourceLocation endloc)
679 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
680 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
Chad Rosier12603e22012-09-04 16:36:26 +0000681 EndLoc(endloc), AsmStr(asmstr.str()), NumAsmToks(asmtoks.size()) {
Chad Rosier058ab172012-08-16 00:06:53 +0000682
683 unsigned NumExprs = NumOutputs + NumInputs;
684
685 Names = new (C) IdentifierInfo*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000686 for (unsigned i = 0, e = NumExprs; i != e; ++i)
687 Names[i] = names[i];
Chad Rosier79efe242012-08-07 00:29:06 +0000688
Chad Rosier633abb02012-08-24 00:07:09 +0000689 Exprs = new (C) Stmt*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000690 for (unsigned i = 0, e = NumExprs; i != e; ++i)
691 Exprs[i] = exprs[i];
Chad Rosier633abb02012-08-24 00:07:09 +0000692
Chad Rosier79efe242012-08-07 00:29:06 +0000693 AsmToks = new (C) Token[NumAsmToks];
694 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
695 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000696
Chad Rosier89fb6d72012-08-28 20:28:20 +0000697 Constraints = new (C) StringRef[NumExprs];
698 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
699 size_t size = constraints[i].size();
700 char *dest = new (C) char[size];
701 std::strncpy(dest, constraints[i].data(), size);
702 Constraints[i] = StringRef(dest, size);
703 }
704
Chad Rosier33c72e12012-08-10 21:36:25 +0000705 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000706 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
707 // FIXME: Avoid the allocation/copy if at all possible.
708 size_t size = clobbers[i].size();
709 char *dest = new (C) char[size];
710 std::strncpy(dest, clobbers[i].data(), size);
Chad Rosier33c72e12012-08-10 21:36:25 +0000711 Clobbers[i] = StringRef(dest, size);
Chad Rosiere790bc32012-08-10 21:06:19 +0000712 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000713}
714
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000715ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
716 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000717 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000718: Stmt(ObjCForCollectionStmtClass) {
719 SubExprs[ELEM] = Elem;
720 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
721 SubExprs[BODY] = Body;
722 ForLoc = FCL;
723 RParenLoc = RPL;
724}
725
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000726ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
727 Stmt **CatchStmts, unsigned NumCatchStmts,
728 Stmt *atFinallyStmt)
729 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
730 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
731{
732 Stmt **Stmts = getStmts();
733 Stmts[0] = atTryStmt;
734 for (unsigned I = 0; I != NumCatchStmts; ++I)
735 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000736
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000737 if (HasFinally)
738 Stmts[NumCatchStmts + 1] = atFinallyStmt;
739}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000740
Chad Rosier0e2a8682012-08-07 23:12:23 +0000741ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
742 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000743 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000744 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000745 unsigned NumCatchStmts,
746 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000747 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000748 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000749 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000750 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
751 atFinallyStmt);
752}
Ted Kremenekff981022008-02-01 21:28:59 +0000753
Chad Rosier0e2a8682012-08-07 23:12:23 +0000754ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000755 unsigned NumCatchStmts,
756 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000757 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000758 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000759 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000760 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000761}
Nico Weber608b17f2008-08-05 23:15:29 +0000762
Erik Verbruggen65d78312012-12-25 14:51:39 +0000763SourceLocation ObjCAtTryStmt::getLocEnd() const {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000764 if (HasFinally)
Erik Verbruggen65d78312012-12-25 14:51:39 +0000765 return getFinallyStmt()->getLocEnd();
766 if (NumCatchStmts)
767 return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
768 return getTryBody()->getLocEnd();
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000769}
770
Sam Weiniga1a396d2010-02-03 03:56:39 +0000771CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000772 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000773 unsigned numHandlers) {
774 std::size_t Size = sizeof(CXXTryStmt);
775 Size += ((numHandlers + 1) * sizeof(Stmt));
776
Chris Lattner32488542010-10-30 05:14:06 +0000777 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000778 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
779}
780
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000781CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
782 unsigned numHandlers) {
783 std::size_t Size = sizeof(CXXTryStmt);
784 Size += ((numHandlers + 1) * sizeof(Stmt));
785
Chris Lattner32488542010-10-30 05:14:06 +0000786 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000787 return new (Mem) CXXTryStmt(Empty, numHandlers);
788}
789
Sam Weiniga1a396d2010-02-03 03:56:39 +0000790CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000791 Stmt **handlers, unsigned numHandlers)
792 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000793 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000794 Stmts[0] = tryBlock;
795 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
796}
797
Richard Smithad762fc2011-04-14 22:09:26 +0000798CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
799 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
800 Stmt *Body, SourceLocation FL,
801 SourceLocation CL, SourceLocation RPL)
802 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
803 SubExprs[RANGE] = Range;
804 SubExprs[BEGINEND] = BeginEndStmt;
805 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
806 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
807 SubExprs[LOOPVAR] = LoopVar;
808 SubExprs[BODY] = Body;
809}
810
811Expr *CXXForRangeStmt::getRangeInit() {
812 DeclStmt *RangeStmt = getRangeStmt();
813 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
814 assert(RangeDecl &&& "for-range should have a single var decl");
815 return RangeDecl->getInit();
816}
817
818const Expr *CXXForRangeStmt::getRangeInit() const {
819 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
820}
821
822VarDecl *CXXForRangeStmt::getLoopVariable() {
823 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
824 assert(LV && "No loop variable in CXXForRangeStmt");
825 return cast<VarDecl>(LV);
826}
827
828const VarDecl *CXXForRangeStmt::getLoopVariable() const {
829 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
830}
831
Chad Rosier0e2a8682012-08-07 23:12:23 +0000832IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000833 Stmt *then, SourceLocation EL, Stmt *elsev)
834 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000835{
836 setConditionVariable(C, var);
837 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
838 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000839 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000840}
841
842VarDecl *IfStmt::getConditionVariable() const {
843 if (!SubExprs[VAR])
844 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000845
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000846 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
847 return cast<VarDecl>(DS->getSingleDecl());
848}
849
850void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
851 if (!V) {
852 SubExprs[VAR] = 0;
853 return;
854 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000855
Daniel Dunbar96a00142012-03-09 18:35:03 +0000856 SourceRange VarRange = V->getSourceRange();
857 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
858 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000859}
860
Chad Rosier0e2a8682012-08-07 23:12:23 +0000861ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
862 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000863 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000864 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000865{
866 SubExprs[INIT] = Init;
867 setConditionVariable(C, condVar);
868 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
869 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
870 SubExprs[BODY] = Body;
871}
872
873VarDecl *ForStmt::getConditionVariable() const {
874 if (!SubExprs[CONDVAR])
875 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000876
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000877 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
878 return cast<VarDecl>(DS->getSingleDecl());
879}
880
881void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
882 if (!V) {
883 SubExprs[CONDVAR] = 0;
884 return;
885 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000886
Daniel Dunbar96a00142012-03-09 18:35:03 +0000887 SourceRange VarRange = V->getSourceRange();
888 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
889 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000890}
891
Chad Rosier0e2a8682012-08-07 23:12:23 +0000892SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
893 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000894{
895 setConditionVariable(C, Var);
896 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
897 SubExprs[BODY] = NULL;
898}
899
900VarDecl *SwitchStmt::getConditionVariable() const {
901 if (!SubExprs[VAR])
902 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000903
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000904 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
905 return cast<VarDecl>(DS->getSingleDecl());
906}
907
908void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
909 if (!V) {
910 SubExprs[VAR] = 0;
911 return;
912 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000913
Daniel Dunbar96a00142012-03-09 18:35:03 +0000914 SourceRange VarRange = V->getSourceRange();
915 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
916 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000917}
918
John McCall63c00d72011-02-09 08:16:59 +0000919Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000920 if (isa<CaseStmt>(this))
921 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000922 return cast<DefaultStmt>(this)->getSubStmt();
923}
924
Chad Rosier0e2a8682012-08-07 23:12:23 +0000925WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000926 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000927 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000928 setConditionVariable(C, Var);
929 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
930 SubExprs[BODY] = body;
931 WhileLoc = WL;
932}
933
934VarDecl *WhileStmt::getConditionVariable() const {
935 if (!SubExprs[VAR])
936 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000937
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000938 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
939 return cast<VarDecl>(DS->getSingleDecl());
940}
941
942void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
943 if (!V) {
944 SubExprs[VAR] = 0;
945 return;
946 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000947
948 SourceRange VarRange = V->getSourceRange();
949 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
950 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000951}
952
Ted Kremenek82977772007-08-24 21:09:09 +0000953// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000954LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000955 if (AddrLabelExpr *E =
956 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
957 return E->getLabel();
958 return 0;
959}
Ted Kremenek82977772007-08-24 21:09:09 +0000960
Ted Kremenek82977772007-08-24 21:09:09 +0000961// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000962const Expr* ReturnStmt::getRetValue() const {
963 return cast_or_null<Expr>(RetExpr);
964}
965Expr* ReturnStmt::getRetValue() {
966 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000967}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000968
969SEHTryStmt::SEHTryStmt(bool IsCXXTry,
970 SourceLocation TryLoc,
971 Stmt *TryBlock,
972 Stmt *Handler)
973 : Stmt(SEHTryStmtClass),
974 IsCXXTry(IsCXXTry),
975 TryLoc(TryLoc)
976{
977 Children[TRY] = TryBlock;
978 Children[HANDLER] = Handler;
979}
980
981SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
982 bool IsCXXTry,
983 SourceLocation TryLoc,
984 Stmt *TryBlock,
985 Stmt *Handler) {
986 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
987}
988
989SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
990 return dyn_cast<SEHExceptStmt>(getHandler());
991}
992
993SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
994 return dyn_cast<SEHFinallyStmt>(getHandler());
995}
996
997SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
998 Expr *FilterExpr,
999 Stmt *Block)
1000 : Stmt(SEHExceptStmtClass),
1001 Loc(Loc)
1002{
1003 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
1004 Children[BLOCK] = Block;
1005}
1006
1007SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
1008 SourceLocation Loc,
1009 Expr *FilterExpr,
1010 Stmt *Block) {
1011 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1012}
1013
1014SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1015 Stmt *Block)
1016 : Stmt(SEHFinallyStmtClass),
1017 Loc(Loc),
1018 Block(Block)
1019{}
1020
1021SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
1022 SourceLocation Loc,
1023 Stmt *Block) {
1024 return new(C)SEHFinallyStmt(Loc,Block);
1025}