blob: 2ae5a1266c187d3602d79267190a77f908682f71 [file] [log] [blame]
Chris Lattnerf42cce72006-10-25 04:09:21 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf42cce72006-10-25 04:09:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer444a1302012-12-01 17:12:56 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTDiagnostic.h"
Chris Lattner29375652006-12-04 18:06:35 +000016#include "clang/AST/ExprCXX.h"
Steve Naroff021ca182008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000018#include "clang/AST/Stmt.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtObjC.h"
Sebastian Redl54c04d42008-12-22 19:15:10 +000021#include "clang/AST/Type.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
Chris Lattner1a8f3942010-04-23 16:29:58 +000023#include "clang/Basic/TargetInfo.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000024#include "clang/Lex/Token.h"
Chad Rosier14836ba2012-08-24 17:05:45 +000025#include "llvm/ADT/StringExtras.h"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnerf42cce72006-10-25 04:09:21 +000027using namespace clang;
28
Steve Narofff84d11f2007-05-23 21:48:04 +000029static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000030 const char *Name;
31 unsigned Counter;
32 unsigned Size;
Alexis Hunt656bb312010-05-05 15:24:00 +000033} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner4d15a0d2007-08-25 01:42:24 +000034
35static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
36 static bool Initialized = false;
37 if (Initialized)
38 return StmtClassInfo[E];
39
40 // Intialize the table on the first use.
41 Initialized = true;
Alexis Huntabb2ac82010-05-18 06:22:21 +000042#define ABSTRACT_STMT(STMT)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000043#define STMT(CLASS, PARENT) \
44 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
45 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Alexis Hunt656bb312010-05-05 15:24:00 +000046#include "clang/AST/StmtNodes.inc"
Nico Weberde565e32008-08-05 23:15:29 +000047
Chris Lattner4d15a0d2007-08-25 01:42:24 +000048 return StmtClassInfo[E];
49}
50
Benjamin Kramerea70eb32012-12-01 15:09:41 +000051void *Stmt::operator new(size_t bytes, ASTContext& C,
52 unsigned alignment) throw() {
53 return ::operator new(bytes, C, alignment);
54}
55
56void *Stmt::operator new(size_t bytes, ASTContext* C,
57 unsigned alignment) throw() {
58 return ::operator new(bytes, *C, alignment);
59}
60
Steve Narofff1e53692007-03-23 22:27:02 +000061const char *Stmt::getStmtClassName() const {
John McCall925b16622010-10-26 08:39:16 +000062 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000063}
Steve Narofff84d11f2007-05-23 21:48:04 +000064
65void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000066 // Ensure the table is primed.
67 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000068
Steve Narofff84d11f2007-05-23 21:48:04 +000069 unsigned sum = 0;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000070 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Alexis Hunt656bb312010-05-05 15:24:00 +000071 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000072 if (StmtClassInfo[i].Name == 0) continue;
73 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000074 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000075 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000076 sum = 0;
Alexis Hunt656bb312010-05-05 15:24:00 +000077 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000078 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +000079 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000080 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
81 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
82 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
83 << " bytes)\n";
Chris Lattner4d15a0d2007-08-25 01:42:24 +000084 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +000085 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000086
87 llvm::errs() << "Total bytes = " << sum << "\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000088}
89
90void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000091 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000092}
93
Daniel Dunbar62905572012-03-05 21:42:49 +000094bool Stmt::StatisticsEnabled = false;
95void Stmt::EnableStatistics() {
96 StatisticsEnabled = true;
Steve Narofff84d11f2007-05-23 21:48:04 +000097}
98
John McCall4db5c3c2011-07-07 06:58:02 +000099Stmt *Stmt::IgnoreImplicit() {
100 Stmt *s = this;
101
102 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
103 s = ewc->getSubExpr();
104
105 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
106 s = ice->getSubExpr();
107
108 return s;
109}
110
Chandler Carrutha626d642011-09-10 00:02:34 +0000111/// \brief Strip off all label-like statements.
112///
Richard Smithc202b282012-04-14 00:33:13 +0000113/// This will strip off label statements, case statements, attributed
114/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000115const Stmt *Stmt::stripLabelLikeStatements() const {
116 const Stmt *S = this;
117 while (true) {
118 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
119 S = LS->getSubStmt();
120 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
121 S = SC->getSubStmt();
Richard Smithc202b282012-04-14 00:33:13 +0000122 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
123 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000124 else
125 return S;
126 }
127}
128
John McCallbd066782011-02-09 08:16:59 +0000129namespace {
130 struct good {};
131 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000132
133 // These silly little functions have to be static inline to suppress
134 // unused warnings, and they have to be defined to suppress other
135 // warnings.
Nick Lewyckybae992f2011-02-09 08:42:57 +0000136 static inline good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000137
138 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000139 template <class T> good implements_children(children_t T::*) {
140 return good();
141 }
142 static inline bad implements_children(children_t Stmt::*) {
143 return bad();
144 }
John McCallbd066782011-02-09 08:16:59 +0000145
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000146 typedef SourceLocation getLocStart_t() const;
147 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000148 return good();
149 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000150 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
151 return bad();
152 }
153
154 typedef SourceLocation getLocEnd_t() const;
155 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
156 return good();
157 }
158 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000159 return bad();
160 }
John McCallbd066782011-02-09 08:16:59 +0000161
162#define ASSERT_IMPLEMENTS_children(type) \
163 (void) sizeof(is_good(implements_children(&type::children)))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000164#define ASSERT_IMPLEMENTS_getLocStart(type) \
165 (void) sizeof(is_good(implements_getLocStart(&type::getLocStart)))
166#define ASSERT_IMPLEMENTS_getLocEnd(type) \
167 (void) sizeof(is_good(implements_getLocEnd(&type::getLocEnd)))
John McCallbd066782011-02-09 08:16:59 +0000168}
169
170/// Check whether the various Stmt classes implement their member
171/// functions.
172static inline void check_implementations() {
173#define ABSTRACT_STMT(type)
174#define STMT(type, base) \
175 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000176 ASSERT_IMPLEMENTS_getLocStart(type); \
177 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCallbd066782011-02-09 08:16:59 +0000178#include "clang/AST/StmtNodes.inc"
179}
180
181Stmt::child_range Stmt::children() {
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<type*>(this)->children();
188#include "clang/AST/StmtNodes.inc"
189 }
190 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000191}
192
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000193// Amusing macro metaprogramming hack: check whether a class provides
194// a more specific implementation of getSourceRange.
195//
196// See also Expr.cpp:getExprLoc().
197namespace {
198 /// This implementation is used when a class provides a custom
199 /// implementation of getSourceRange.
200 template <class S, class T>
201 SourceRange getSourceRangeImpl(const Stmt *stmt,
202 SourceRange (T::*v)() const) {
203 return static_cast<const S*>(stmt)->getSourceRange();
204 }
205
206 /// This implementation is used when a class doesn't provide a custom
207 /// implementation of getSourceRange. 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 SourceRange getSourceRangeImpl(const Stmt *stmt,
212 SourceRange (Stmt::*v)() const) {
213 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
214 static_cast<const S*>(stmt)->getLocEnd());
215 }
216}
217
John McCallbd066782011-02-09 08:16:59 +0000218SourceRange Stmt::getSourceRange() const {
219 switch (getStmtClass()) {
220 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
221#define ABSTRACT_STMT(type)
222#define STMT(type, base) \
223 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000224 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000225#include "clang/AST/StmtNodes.inc"
226 }
227 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000228}
229
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000230SourceLocation Stmt::getLocStart() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000231// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000232 switch (getStmtClass()) {
233 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
234#define ABSTRACT_STMT(type)
235#define STMT(type, base) \
236 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000237 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000238#include "clang/AST/StmtNodes.inc"
239 }
240 llvm_unreachable("unknown statement kind");
241}
242
243SourceLocation Stmt::getLocEnd() const {
244 switch (getStmtClass()) {
245 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
246#define ABSTRACT_STMT(type)
247#define STMT(type, base) \
248 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000249 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000250#include "clang/AST/StmtNodes.inc"
251 }
252 llvm_unreachable("unknown statement kind");
253}
254
Nico Webera2a0eb92012-12-29 20:03:39 +0000255CompoundStmt::CompoundStmt(ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000256 SourceLocation LB, SourceLocation RB)
257 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000258 CompoundStmtBits.NumStmts = Stmts.size();
259 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000260 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
261
Nico Webera2a0eb92012-12-29 20:03:39 +0000262 if (Stmts.size() == 0) {
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000263 Body = 0;
264 return;
265 }
266
Nico Webera2a0eb92012-12-29 20:03:39 +0000267 Body = new (C) Stmt*[Stmts.size()];
268 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000269}
270
Douglas Gregora9af1d12009-04-17 00:04:06 +0000271void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
272 if (this->Body)
273 C.Deallocate(Body);
John McCall925b16622010-10-26 08:39:16 +0000274 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregora9af1d12009-04-17 00:04:06 +0000275
276 Body = new (C) Stmt*[NumStmts];
277 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
278}
Steve Narofff84d11f2007-05-23 21:48:04 +0000279
Chris Lattnereefa10e2007-05-28 06:56:27 +0000280const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000281 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000282}
283
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000284AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
285 ArrayRef<const Attr*> Attrs,
286 Stmt *SubStmt) {
287 void *Mem = C.Allocate(sizeof(AttributedStmt) +
288 sizeof(Attr*) * (Attrs.size() - 1),
289 llvm::alignOf<AttributedStmt>());
290 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
291}
292
293AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
294 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
295 void *Mem = C.Allocate(sizeof(AttributedStmt) +
296 sizeof(Attr*) * (NumAttrs - 1),
297 llvm::alignOf<AttributedStmt>());
298 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
299}
300
Ted Kremenek7f74e132007-10-01 16:34:52 +0000301bool Stmt::hasImplicitControlFlow() const {
John McCall925b16622010-10-26 08:39:16 +0000302 switch (StmtBits.sClass) {
Ted Kremenek7f74e132007-10-01 16:34:52 +0000303 default:
304 return false;
Nico Weberde565e32008-08-05 23:15:29 +0000305
Ted Kremenek7f74e132007-10-01 16:34:52 +0000306 case CallExprClass:
307 case ConditionalOperatorClass:
308 case ChooseExprClass:
309 case StmtExprClass:
310 case DeclStmtClass:
Nico Weberde565e32008-08-05 23:15:29 +0000311 return true;
312
Ted Kremenek7f74e132007-10-01 16:34:52 +0000313 case Stmt::BinaryOperatorClass: {
314 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +0000315 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenek7f74e132007-10-01 16:34:52 +0000316 return true;
317 else
318 return false;
319 }
320 }
321}
322
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000323std::string AsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000324 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
325 return gccAsmStmt->generateAsmString(C);
326 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
327 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000328 llvm_unreachable("unknown asm statement kind!");
329}
330
331StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000332 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
333 return gccAsmStmt->getOutputConstraint(i);
334 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
335 return msAsmStmt->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000336 llvm_unreachable("unknown asm statement kind!");
337}
338
339const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000340 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
341 return gccAsmStmt->getOutputExpr(i);
342 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
343 return msAsmStmt->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000344 llvm_unreachable("unknown asm statement kind!");
345}
346
347StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000348 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
349 return gccAsmStmt->getInputConstraint(i);
350 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
351 return msAsmStmt->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000352 llvm_unreachable("unknown asm statement kind!");
353}
354
355const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000356 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
357 return gccAsmStmt->getInputExpr(i);
358 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
359 return msAsmStmt->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000360 llvm_unreachable("unknown asm statement kind!");
361}
362
363StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000364 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
365 return gccAsmStmt->getClobber(i);
366 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
367 return msAsmStmt->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000368 llvm_unreachable("unknown asm statement kind!");
369}
370
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000371/// getNumPlusOperands - Return the number of output operands that have a "+"
372/// constraint.
373unsigned AsmStmt::getNumPlusOperands() const {
374 unsigned Res = 0;
375 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
376 if (isOutputPlusConstraint(i))
377 ++Res;
378 return Res;
379}
380
Chad Rosier6100ae12012-08-27 23:47:56 +0000381StringRef GCCAsmStmt::getClobber(unsigned i) const {
382 return getClobberStringLiteral(i)->getString();
383}
384
Chad Rosierde70e0e2012-08-25 00:11:56 +0000385Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000386 return cast<Expr>(Exprs[i]);
387}
Chris Lattner72bbf172009-03-10 04:59:06 +0000388
389/// getOutputConstraint - Return the constraint string for the specified
390/// output operand. All output constraints are known to be non-empty (either
391/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000392StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000393 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000394}
Chris Lattner72bbf172009-03-10 04:59:06 +0000395
Chad Rosierde70e0e2012-08-25 00:11:56 +0000396Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000397 return cast<Expr>(Exprs[i + NumOutputs]);
398}
Chad Rosierde70e0e2012-08-25 00:11:56 +0000399void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000400 Exprs[i + NumOutputs] = E;
401}
402
Chris Lattner72bbf172009-03-10 04:59:06 +0000403/// getInputConstraint - Return the specified input constraint. Unlike output
404/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000405StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000406 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000407}
408
Chad Rosierde70e0e2012-08-25 00:11:56 +0000409void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlsson9a020f92010-01-30 22:25:16 +0000410 IdentifierInfo **Names,
Anders Carlsson96fe0b52010-01-30 19:34:25 +0000411 StringLiteral **Constraints,
412 Stmt **Exprs,
413 unsigned NumOutputs,
Chad Rosier42032fa2012-08-07 23:12:23 +0000414 unsigned NumInputs,
Anders Carlsson96fe0b52010-01-30 19:34:25 +0000415 StringLiteral **Clobbers,
416 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000417 this->NumOutputs = NumOutputs;
418 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000419 this->NumClobbers = NumClobbers;
420
421 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000422
Anders Carlsson98323d22010-01-30 23:19:41 +0000423 C.Deallocate(this->Names);
424 this->Names = new (C) IdentifierInfo*[NumExprs];
425 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000426
Anders Carlsson98323d22010-01-30 23:19:41 +0000427 C.Deallocate(this->Exprs);
428 this->Exprs = new (C) Stmt*[NumExprs];
429 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000430
Anders Carlsson98323d22010-01-30 23:19:41 +0000431 C.Deallocate(this->Constraints);
432 this->Constraints = new (C) StringLiteral*[NumExprs];
433 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000434
Anders Carlsson98323d22010-01-30 23:19:41 +0000435 C.Deallocate(this->Clobbers);
436 this->Clobbers = new (C) StringLiteral*[NumClobbers];
437 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000438}
439
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000440/// getNamedOperand - Given a symbolic operand reference like %[foo],
441/// translate this into a numeric value needed to reference the same operand.
442/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000443int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000444 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000446 // Check if this is an output operand.
447 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
448 if (getOutputName(i) == SymbolicName)
449 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000452 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
453 if (getInputName(i) == SymbolicName)
454 return getNumOutputs() + NumPlusOperands + i;
455
456 // Not found.
457 return -1;
458}
459
Chris Lattner35b58362009-03-10 23:21:44 +0000460/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
461/// it into pieces. If the asm string is erroneous, emit errors and return
462/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000463unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerd8c7ba22009-03-10 23:41:04 +0000464 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000465 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000466 const char *StrStart = Str.begin();
467 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000468 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000469
Chris Lattner35b58362009-03-10 23:21:44 +0000470 // "Simple" inline asms have no constraints or operands, just convert the asm
471 // string to escape $'s.
472 if (isSimple()) {
473 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000474 for (; CurPtr != StrEnd; ++CurPtr) {
475 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000476 case '$':
477 Result += "$$";
478 break;
479 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000480 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000481 break;
482 }
483 }
484 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000485 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000486 }
487
488 // CurStringPiece - The current string that we are building up as we scan the
489 // asm string.
490 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000491
Douglas Gregore8bbc122011-09-02 00:18:52 +0000492 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000493
Chris Lattner35b58362009-03-10 23:21:44 +0000494 while (1) {
495 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000496 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000497 if (!CurStringPiece.empty())
498 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000499 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000500 }
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattnera41b8472009-03-10 23:51:40 +0000502 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000503 switch (CurChar) {
504 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000505 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
506 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
507 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000508 case '%':
509 break;
510 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000511 CurStringPiece += CurChar;
512 continue;
513 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000514
Chris Lattner35b58362009-03-10 23:21:44 +0000515 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000516 if (CurPtr == StrEnd) {
517 // % at end of string is invalid (no escape).
518 DiagOffs = CurPtr-StrStart-1;
519 return diag::err_asm_invalid_escape;
520 }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Chris Lattnera41b8472009-03-10 23:51:40 +0000522 char EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000523 if (EscapedChar == '%') { // %% -> %
524 // Escaped percentage sign.
525 CurStringPiece += '%';
526 continue;
527 }
Mike Stump11289f42009-09-09 15:08:12 +0000528
Chris Lattner35b58362009-03-10 23:21:44 +0000529 if (EscapedChar == '=') { // %= -> Generate an unique ID.
530 CurStringPiece += "${:uid}";
531 continue;
532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Chris Lattner35b58362009-03-10 23:21:44 +0000534 // Otherwise, we have an operand. If we have accumulated a string so far,
535 // add it to the Pieces list.
536 if (!CurStringPiece.empty()) {
537 Pieces.push_back(AsmStringPiece(CurStringPiece));
538 CurStringPiece.clear();
539 }
Mike Stump11289f42009-09-09 15:08:12 +0000540
Chris Lattner35b58362009-03-10 23:21:44 +0000541 // Handle %x4 and %x[foo] by capturing x as the modifier character.
542 char Modifier = '\0';
Jordan Rosea7d03842013-02-08 22:30:41 +0000543 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000544 if (CurPtr == StrEnd) { // Premature end.
545 DiagOffs = CurPtr-StrStart-1;
546 return diag::err_asm_invalid_escape;
547 }
Chris Lattner35b58362009-03-10 23:21:44 +0000548 Modifier = EscapedChar;
Chris Lattnera41b8472009-03-10 23:51:40 +0000549 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Jordan Rosea7d03842013-02-08 22:30:41 +0000552 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000553 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000554 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000555
Chris Lattner99d892b2009-03-11 22:52:17 +0000556 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000557 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000558 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattner14311922009-03-11 00:23:13 +0000560 unsigned NumOperands =
561 getNumOutputs() + getNumPlusOperands() + getNumInputs();
562 if (N >= NumOperands) {
563 DiagOffs = CurPtr-StrStart-1;
564 return diag::err_asm_invalid_operand_number;
565 }
566
Chris Lattner35b58362009-03-10 23:21:44 +0000567 Pieces.push_back(AsmStringPiece(N, Modifier));
568 continue;
569 }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Chris Lattner35b58362009-03-10 23:21:44 +0000571 // Handle %[foo], a symbolic operand reference.
572 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000573 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000574
Chris Lattner3fa25c62009-03-11 00:06:36 +0000575 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000576 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000577 if (NameEnd == 0)
578 return diag::err_asm_unterminated_symbolic_operand_name;
579 if (NameEnd == CurPtr)
580 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000581
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000582 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000583
Chris Lattner35b58362009-03-10 23:21:44 +0000584 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000585 if (N == -1) {
586 // Verify that an operand with that name exists.
587 DiagOffs = CurPtr-StrStart;
588 return diag::err_asm_unknown_symbolic_operand_name;
589 }
Chris Lattner35b58362009-03-10 23:21:44 +0000590 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump11289f42009-09-09 15:08:12 +0000591
Chris Lattner3fa25c62009-03-11 00:06:36 +0000592 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000593 continue;
594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000596 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000597 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000598 }
599}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000600
601/// Assemble final IR asm string (GCC-style).
602std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000603 // Analyze the asm string to decompose it into its pieces. We know that Sema
604 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000605 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000606 unsigned DiagOffs;
607 AnalyzeAsmString(Pieces, C, DiagOffs);
608
609 std::string AsmString;
610 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
611 if (Pieces[i].isString())
612 AsmString += Pieces[i].getString();
613 else if (Pieces[i].getModifier() == '\0')
614 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
615 else
616 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
617 Pieces[i].getModifier() + '}';
618 }
619 return AsmString;
620}
Chris Lattner35b58362009-03-10 23:21:44 +0000621
Chad Rosier3b0c2602012-08-27 20:23:31 +0000622/// Assemble final IR asm string (MS-style).
623std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
624 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000625 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000626}
627
Chad Rosierfe31e622012-08-24 00:07:09 +0000628Expr *MSAsmStmt::getOutputExpr(unsigned i) {
629 return cast<Expr>(Exprs[i]);
630}
631
632Expr *MSAsmStmt::getInputExpr(unsigned i) {
633 return cast<Expr>(Exprs[i + NumOutputs]);
634}
635void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
636 Exprs[i + NumOutputs] = E;
637}
638
Sam Weinigebcea982010-02-03 02:09:59 +0000639QualType CXXCatchStmt::getCaughtType() const {
640 if (ExceptionDecl)
641 return ExceptionDecl->getType();
642 return QualType();
643}
644
Chris Lattner86f5e132008-01-30 05:01:46 +0000645//===----------------------------------------------------------------------===//
646// Constructors
647//===----------------------------------------------------------------------===//
648
Chad Rosierde70e0e2012-08-25 00:11:56 +0000649GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
650 bool isvolatile, unsigned numoutputs, unsigned numinputs,
651 IdentifierInfo **names, StringLiteral **constraints,
652 Expr **exprs, StringLiteral *asmstr,
653 unsigned numclobbers, StringLiteral **clobbers,
654 SourceLocation rparenloc)
Chad Rosierfe3352e2012-08-27 19:38:01 +0000655 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
656 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weberde565e32008-08-05 23:15:29 +0000657
Chad Rosier42032fa2012-08-07 23:12:23 +0000658 unsigned NumExprs = NumOutputs + NumInputs;
659
Anders Carlsson98323d22010-01-30 23:19:41 +0000660 Names = new (C) IdentifierInfo*[NumExprs];
661 std::copy(names, names + NumExprs, Names);
662
663 Exprs = new (C) Stmt*[NumExprs];
664 std::copy(exprs, exprs + NumExprs, Exprs);
665
666 Constraints = new (C) StringLiteral*[NumExprs];
667 std::copy(constraints, constraints + NumExprs, Constraints);
668
669 Clobbers = new (C) StringLiteral*[NumClobbers];
670 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000671}
672
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000673MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
674 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000675 ArrayRef<Token> asmtoks, unsigned numoutputs,
676 unsigned numinputs, ArrayRef<IdentifierInfo*> names,
677 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
678 StringRef asmstr, ArrayRef<StringRef> clobbers,
679 SourceLocation endloc)
680 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
681 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
Chad Rosier74f37162012-09-04 16:36:26 +0000682 EndLoc(endloc), AsmStr(asmstr.str()), NumAsmToks(asmtoks.size()) {
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000683
684 unsigned NumExprs = NumOutputs + NumInputs;
685
686 Names = new (C) IdentifierInfo*[NumExprs];
Chad Rosierf8037a12012-10-16 21:55:39 +0000687 for (unsigned i = 0, e = NumExprs; i != e; ++i)
688 Names[i] = names[i];
Chad Rosier99fc3812012-08-07 00:29:06 +0000689
Chad Rosierfe31e622012-08-24 00:07:09 +0000690 Exprs = new (C) Stmt*[NumExprs];
Chad Rosierf8037a12012-10-16 21:55:39 +0000691 for (unsigned i = 0, e = NumExprs; i != e; ++i)
692 Exprs[i] = exprs[i];
Chad Rosierfe31e622012-08-24 00:07:09 +0000693
Chad Rosier99fc3812012-08-07 00:29:06 +0000694 AsmToks = new (C) Token[NumAsmToks];
695 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
696 AsmToks[i] = asmtoks[i];
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000697
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000698 Constraints = new (C) StringRef[NumExprs];
699 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
700 size_t size = constraints[i].size();
701 char *dest = new (C) char[size];
702 std::strncpy(dest, constraints[i].data(), size);
703 Constraints[i] = StringRef(dest, size);
704 }
705
Chad Rosierbaf53f92012-08-10 21:36:25 +0000706 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosierd6ef7042012-08-10 21:06:19 +0000707 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
708 // FIXME: Avoid the allocation/copy if at all possible.
709 size_t size = clobbers[i].size();
710 char *dest = new (C) char[size];
711 std::strncpy(dest, clobbers[i].data(), size);
Chad Rosierbaf53f92012-08-10 21:36:25 +0000712 Clobbers[i] = StringRef(dest, size);
Chad Rosierd6ef7042012-08-10 21:06:19 +0000713 }
Chad Rosier32503022012-06-11 20:47:18 +0000714}
715
Chris Lattner86f5e132008-01-30 05:01:46 +0000716ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
717 Stmt *Body, SourceLocation FCL,
Nico Weberde565e32008-08-05 23:15:29 +0000718 SourceLocation RPL)
Chris Lattner86f5e132008-01-30 05:01:46 +0000719: Stmt(ObjCForCollectionStmtClass) {
720 SubExprs[ELEM] = Elem;
721 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
722 SubExprs[BODY] = Body;
723 ForLoc = FCL;
724 RParenLoc = RPL;
725}
726
Douglas Gregor96c79492010-04-23 22:50:49 +0000727ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
728 Stmt **CatchStmts, unsigned NumCatchStmts,
729 Stmt *atFinallyStmt)
730 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
731 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
732{
733 Stmt **Stmts = getStmts();
734 Stmts[0] = atTryStmt;
735 for (unsigned I = 0; I != NumCatchStmts; ++I)
736 Stmts[I + 1] = CatchStmts[I];
Chad Rosier42032fa2012-08-07 23:12:23 +0000737
Douglas Gregor96c79492010-04-23 22:50:49 +0000738 if (HasFinally)
739 Stmts[NumCatchStmts + 1] = atFinallyStmt;
740}
Chris Lattner86f5e132008-01-30 05:01:46 +0000741
Chad Rosier42032fa2012-08-07 23:12:23 +0000742ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
743 SourceLocation atTryLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +0000744 Stmt *atTryStmt,
Chad Rosier42032fa2012-08-07 23:12:23 +0000745 Stmt **CatchStmts,
Douglas Gregor96c79492010-04-23 22:50:49 +0000746 unsigned NumCatchStmts,
747 Stmt *atFinallyStmt) {
Chad Rosier42032fa2012-08-07 23:12:23 +0000748 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor96c79492010-04-23 22:50:49 +0000749 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner5c0b4052010-10-30 05:14:06 +0000750 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor96c79492010-04-23 22:50:49 +0000751 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
752 atFinallyStmt);
753}
Ted Kremeneka4965842008-02-01 21:28:59 +0000754
Chad Rosier42032fa2012-08-07 23:12:23 +0000755ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor96c79492010-04-23 22:50:49 +0000756 unsigned NumCatchStmts,
757 bool HasFinally) {
Chad Rosier42032fa2012-08-07 23:12:23 +0000758 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor96c79492010-04-23 22:50:49 +0000759 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner5c0b4052010-10-30 05:14:06 +0000760 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier42032fa2012-08-07 23:12:23 +0000761 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor96c79492010-04-23 22:50:49 +0000762}
Nico Weberde565e32008-08-05 23:15:29 +0000763
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000764SourceLocation ObjCAtTryStmt::getLocEnd() const {
Douglas Gregor96c79492010-04-23 22:50:49 +0000765 if (HasFinally)
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000766 return getFinallyStmt()->getLocEnd();
767 if (NumCatchStmts)
768 return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
769 return getTryBody()->getLocEnd();
Chris Lattner86f5e132008-01-30 05:01:46 +0000770}
771
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000772CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Nico Weber9dff3782012-12-29 20:13:03 +0000773 Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000774 std::size_t Size = sizeof(CXXTryStmt);
Nico Weber9dff3782012-12-29 20:13:03 +0000775 Size += ((handlers.size() + 1) * sizeof(Stmt));
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000776
Chris Lattner5c0b4052010-10-30 05:14:06 +0000777 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Nico Weber9dff3782012-12-29 20:13:03 +0000778 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000779}
780
Argyrios Kyrtzidis47cd7a92010-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 Lattner5c0b4052010-10-30 05:14:06 +0000786 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis47cd7a92010-07-22 16:03:56 +0000787 return new (Mem) CXXTryStmt(Empty, numHandlers);
788}
789
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000790CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Nico Weber9dff3782012-12-29 20:13:03 +0000791 ArrayRef<Stmt*> handlers)
792 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
Sam Weiniga16b0dd2010-02-03 03:56:39 +0000793 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigebcea982010-02-03 02:09:59 +0000794 Stmts[0] = tryBlock;
Nico Weber9dff3782012-12-29 20:13:03 +0000795 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
Sam Weinigebcea982010-02-03 02:09:59 +0000796}
797
Richard Smith02e85f32011-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 Rosier42032fa2012-08-07 23:12:23 +0000832IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000833 Stmt *then, SourceLocation EL, Stmt *elsev)
834 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000835{
836 setConditionVariable(C, var);
837 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
838 SubExprs[THEN] = then;
Chad Rosier42032fa2012-08-07 23:12:23 +0000839 SubExprs[ELSE] = elsev;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000840}
841
842VarDecl *IfStmt::getConditionVariable() const {
843 if (!SubExprs[VAR])
844 return 0;
Chad Rosier42032fa2012-08-07 23:12:23 +0000845
Douglas Gregor27b98ea2010-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 Rosier42032fa2012-08-07 23:12:23 +0000855
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000856 SourceRange VarRange = V->getSourceRange();
857 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
858 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000859}
860
Chad Rosier42032fa2012-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 Gregor27b98ea2010-06-21 23:44:13 +0000863 SourceLocation RP)
Chad Rosier42032fa2012-08-07 23:12:23 +0000864 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-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 Rosier42032fa2012-08-07 23:12:23 +0000876
Douglas Gregor27b98ea2010-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 Rosier42032fa2012-08-07 23:12:23 +0000886
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000887 SourceRange VarRange = V->getSourceRange();
888 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
889 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000890}
891
Chad Rosier42032fa2012-08-07 23:12:23 +0000892SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
893 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor27b98ea2010-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 Rosier42032fa2012-08-07 23:12:23 +0000903
Douglas Gregor27b98ea2010-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 Rosier42032fa2012-08-07 23:12:23 +0000913
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000914 SourceRange VarRange = V->getSourceRange();
915 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
916 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000917}
918
John McCallbd066782011-02-09 08:16:59 +0000919Stmt *SwitchCase::getSubStmt() {
Chris Lattner80d51eb2011-02-28 00:18:06 +0000920 if (isa<CaseStmt>(this))
921 return cast<CaseStmt>(this)->getSubStmt();
John McCallbd066782011-02-09 08:16:59 +0000922 return cast<DefaultStmt>(this)->getSubStmt();
923}
924
Chad Rosier42032fa2012-08-07 23:12:23 +0000925WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000926 SourceLocation WL)
Chris Lattner80d51eb2011-02-28 00:18:06 +0000927 : Stmt(WhileStmtClass) {
Douglas Gregor27b98ea2010-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 Rosier42032fa2012-08-07 23:12:23 +0000937
Douglas Gregor27b98ea2010-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 Dunbar62ee6412012-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 Gregor27b98ea2010-06-21 23:44:13 +0000951}
952
Ted Kremenek066dd932007-08-24 21:09:09 +0000953// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000954LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall9de91602010-10-28 08:53:48 +0000955 if (AddrLabelExpr *E =
956 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
957 return E->getLabel();
958 return 0;
959}
Ted Kremenek066dd932007-08-24 21:09:09 +0000960
Ted Kremenek066dd932007-08-24 21:09:09 +0000961// ReturnStmt
Ted Kremenekc6501db2008-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 Kremenek066dd932007-08-24 21:09:09 +0000967}
John Wiegley1c0675e2011-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}