blob: d9d79f4cccdda84e788c99c87ccd26887986e2ac [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"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
Chris Lattner9bffb072010-04-23 16:29:58 +000023#include "clang/Basic/TargetInfo.h"
Benjamin Kramera93d0f22012-12-01 17:12:56 +000024#include "clang/Lex/Token.h"
Chad Rosierbe3ace82012-08-24 17:05:45 +000025#include "llvm/ADT/StringExtras.h"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000026#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
Reid Spencer5f016e22007-07-11 17:01:13 +000029static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000030 const char *Name;
31 unsigned Counter;
32 unsigned Size;
Sean Hunt4bfe1962010-05-05 15:24:00 +000033} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner63381352007-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;
Sean Hunt7381d5c2010-05-18 06:22:21 +000042#define ABSTRACT_STMT(STMT)
Douglas Gregorf2cad862008-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);
Sean Hunt4bfe1962010-05-05 15:24:00 +000046#include "clang/AST/StmtNodes.inc"
Nico Weber608b17f2008-08-05 23:15:29 +000047
Chris Lattner63381352007-08-25 01:42:24 +000048 return StmtClassInfo[E];
49}
50
Benjamin Kramer2fa67ef2012-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
Reid Spencer5f016e22007-07-11 17:01:13 +000061const char *Stmt::getStmtClassName() const {
John McCall8e6285a2010-10-26 08:39:16 +000062 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000063}
64
65void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000066 // Ensure the table is primed.
67 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000068
Reid Spencer5f016e22007-07-11 17:01:13 +000069 unsigned sum = 0;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000070 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Sean Hunt4bfe1962010-05-05 15:24:00 +000071 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000072 if (StmtClassInfo[i].Name == 0) continue;
73 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000075 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000076 sum = 0;
Sean Hunt4bfe1962010-05-05 15:24:00 +000077 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000078 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000079 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthb43c8ec2011-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 Lattner63381352007-08-25 01:42:24 +000084 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000085 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000086
87 llvm::errs() << "Total bytes = " << sum << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000088}
89
90void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000091 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000092}
93
Daniel Dunbar02892a62012-03-05 21:42:49 +000094bool Stmt::StatisticsEnabled = false;
95void Stmt::EnableStatistics() {
96 StatisticsEnabled = true;
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
John McCall7e5e5f42011-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 Carrutha1364be2011-09-10 00:02:34 +0000111/// \brief Strip off all label-like statements.
112///
Richard Smith534986f2012-04-14 00:33:13 +0000113/// This will strip off label statements, case statements, attributed
114/// statements and default statements recursively.
Chandler Carrutha1364be2011-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 Smith534986f2012-04-14 00:33:13 +0000122 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
123 S = AS->getSubStmt();
Chandler Carrutha1364be2011-09-10 00:02:34 +0000124 else
125 return S;
126 }
127}
128
John McCall63c00d72011-02-09 08:16:59 +0000129namespace {
130 struct good {};
131 struct bad {};
John McCallf8c7fdb2011-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 Lewycky086eb9f2011-02-09 08:42:57 +0000136 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +0000137
138 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-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 McCall63c00d72011-02-09 08:16:59 +0000145
Erik Verbruggen65d78312012-12-25 14:51:39 +0000146 typedef SourceLocation getLocStart_t() const;
147 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000148 return good();
149 }
Erik Verbruggen65d78312012-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 Lewycky086eb9f2011-02-09 08:42:57 +0000159 return bad();
160 }
John McCall63c00d72011-02-09 08:16:59 +0000161
162#define ASSERT_IMPLEMENTS_children(type) \
163 (void) sizeof(is_good(implements_children(&type::children)))
Erik Verbruggen65d78312012-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 McCall63c00d72011-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 Verbruggen65d78312012-12-25 14:51:39 +0000176 ASSERT_IMPLEMENTS_getLocStart(type); \
177 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCall63c00d72011-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 McCall63c00d72011-02-09 08:16:59 +0000191}
192
Erik Verbruggen65d78312012-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 McCall63c00d72011-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 Verbruggen65d78312012-12-25 14:51:39 +0000224 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCall63c00d72011-02-09 08:16:59 +0000225#include "clang/AST/StmtNodes.inc"
226 }
227 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000228}
229
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000230SourceLocation Stmt::getLocStart() const {
Erik Verbruggen65d78312012-12-25 14:51:39 +0000231// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbar90e25a82012-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 Verbruggen65d78312012-12-25 14:51:39 +0000237 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbar90e25a82012-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 Verbruggen65d78312012-12-25 14:51:39 +0000249 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000250#include "clang/AST/StmtNodes.inc"
251 }
252 llvm_unreachable("unknown statement kind");
253}
254
Nico Weberd36aa352012-12-29 20:03:39 +0000255CompoundStmt::CompoundStmt(ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000256 SourceLocation LB, SourceLocation RB)
257 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
Nico Weberd36aa352012-12-29 20:03:39 +0000258 CompoundStmtBits.NumStmts = Stmts.size();
259 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000260 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
261
Nico Weberd36aa352012-12-29 20:03:39 +0000262 if (Stmts.size() == 0) {
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000263 Body = 0;
264 return;
265 }
266
Nico Weberd36aa352012-12-29 20:03:39 +0000267 Body = new (C) Stmt*[Stmts.size()];
268 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000269}
270
Douglas Gregor025452f2009-04-17 00:04:06 +0000271void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
272 if (this->Body)
273 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000274 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000275
276 Body = new (C) Stmt*[NumStmts];
277 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
278}
Reid Spencer5f016e22007-07-11 17:01:13 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000281 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000282}
283
Alexander Kornienko49908902012-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
Chad Rosieraba59aa2012-08-28 17:43:23 +0000301std::string AsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000302 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
303 return gccAsmStmt->generateAsmString(C);
304 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
305 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000306 llvm_unreachable("unknown asm statement kind!");
307}
308
309StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000310 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
311 return gccAsmStmt->getOutputConstraint(i);
312 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
313 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000314 llvm_unreachable("unknown asm statement kind!");
315}
316
317const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000318 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
319 return gccAsmStmt->getOutputExpr(i);
320 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
321 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000322 llvm_unreachable("unknown asm statement kind!");
323}
324
325StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000326 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
327 return gccAsmStmt->getInputConstraint(i);
328 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
329 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000330 llvm_unreachable("unknown asm statement kind!");
331}
332
333const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000334 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
335 return gccAsmStmt->getInputExpr(i);
336 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
337 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000338 llvm_unreachable("unknown asm statement kind!");
339}
340
341StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000342 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
343 return gccAsmStmt->getClobber(i);
344 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
345 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000346 llvm_unreachable("unknown asm statement kind!");
347}
348
Chad Rosierc4fb2212012-08-28 00:24:05 +0000349/// getNumPlusOperands - Return the number of output operands that have a "+"
350/// constraint.
351unsigned AsmStmt::getNumPlusOperands() const {
352 unsigned Res = 0;
353 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
354 if (isOutputPlusConstraint(i))
355 ++Res;
356 return Res;
357}
358
Chad Rosier33f05582012-08-27 23:47:56 +0000359StringRef GCCAsmStmt::getClobber(unsigned i) const {
360 return getClobberStringLiteral(i)->getString();
361}
362
Chad Rosierdf5faf52012-08-25 00:11:56 +0000363Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000364 return cast<Expr>(Exprs[i]);
365}
Chris Lattnerb3277932009-03-10 04:59:06 +0000366
367/// getOutputConstraint - Return the constraint string for the specified
368/// output operand. All output constraints are known to be non-empty (either
369/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000370StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000371 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000372}
Chris Lattnerb3277932009-03-10 04:59:06 +0000373
Chad Rosierdf5faf52012-08-25 00:11:56 +0000374Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000375 return cast<Expr>(Exprs[i + NumOutputs]);
376}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000377void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000378 Exprs[i + NumOutputs] = E;
379}
380
Chris Lattnerb3277932009-03-10 04:59:06 +0000381/// getInputConstraint - Return the specified input constraint. Unlike output
382/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000383StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000384 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000385}
386
Chad Rosierdf5faf52012-08-25 00:11:56 +0000387void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000388 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000389 StringLiteral **Constraints,
390 Stmt **Exprs,
391 unsigned NumOutputs,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000392 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000393 StringLiteral **Clobbers,
394 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000395 this->NumOutputs = NumOutputs;
396 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000397 this->NumClobbers = NumClobbers;
398
399 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000400
Anders Carlsson966146e2010-01-30 23:19:41 +0000401 C.Deallocate(this->Names);
402 this->Names = new (C) IdentifierInfo*[NumExprs];
403 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000404
Anders Carlsson966146e2010-01-30 23:19:41 +0000405 C.Deallocate(this->Exprs);
406 this->Exprs = new (C) Stmt*[NumExprs];
407 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000408
Anders Carlsson966146e2010-01-30 23:19:41 +0000409 C.Deallocate(this->Constraints);
410 this->Constraints = new (C) StringLiteral*[NumExprs];
411 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000412
Anders Carlsson966146e2010-01-30 23:19:41 +0000413 C.Deallocate(this->Clobbers);
414 this->Clobbers = new (C) StringLiteral*[NumClobbers];
415 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000416}
417
Chris Lattner10ca96a2009-03-10 06:33:24 +0000418/// getNamedOperand - Given a symbolic operand reference like %[foo],
419/// translate this into a numeric value needed to reference the same operand.
420/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000421int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000422 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner10ca96a2009-03-10 06:33:24 +0000424 // Check if this is an output operand.
425 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
426 if (getOutputName(i) == SymbolicName)
427 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattner10ca96a2009-03-10 06:33:24 +0000430 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
431 if (getInputName(i) == SymbolicName)
432 return getNumOutputs() + NumPlusOperands + i;
433
434 // Not found.
435 return -1;
436}
437
Chris Lattner458cd9c2009-03-10 23:21:44 +0000438/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
439/// it into pieces. If the asm string is erroneous, emit errors and return
440/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000441unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000442 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000443 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000444 const char *StrStart = Str.begin();
445 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000446 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner458cd9c2009-03-10 23:21:44 +0000448 // "Simple" inline asms have no constraints or operands, just convert the asm
449 // string to escape $'s.
450 if (isSimple()) {
451 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000452 for (; CurPtr != StrEnd; ++CurPtr) {
453 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000454 case '$':
455 Result += "$$";
456 break;
457 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000458 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000459 break;
460 }
461 }
462 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000463 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000464 }
465
466 // CurStringPiece - The current string that we are building up as we scan the
467 // asm string.
468 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000470 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000471
Chris Lattner458cd9c2009-03-10 23:21:44 +0000472 while (1) {
473 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000474 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000475 if (!CurStringPiece.empty())
476 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000477 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattner3182db12009-03-10 23:51:40 +0000480 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000481 switch (CurChar) {
482 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000483 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
484 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
485 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000486 case '%':
487 break;
488 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000489 CurStringPiece += CurChar;
490 continue;
491 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000492
Chris Lattner458cd9c2009-03-10 23:21:44 +0000493 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000494 if (CurPtr == StrEnd) {
495 // % at end of string is invalid (no escape).
496 DiagOffs = CurPtr-StrStart-1;
497 return diag::err_asm_invalid_escape;
498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattner3182db12009-03-10 23:51:40 +0000500 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000501 if (EscapedChar == '%') { // %% -> %
502 // Escaped percentage sign.
503 CurStringPiece += '%';
504 continue;
505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Chris Lattner458cd9c2009-03-10 23:21:44 +0000507 if (EscapedChar == '=') { // %= -> Generate an unique ID.
508 CurStringPiece += "${:uid}";
509 continue;
510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Chris Lattner458cd9c2009-03-10 23:21:44 +0000512 // Otherwise, we have an operand. If we have accumulated a string so far,
513 // add it to the Pieces list.
514 if (!CurStringPiece.empty()) {
515 Pieces.push_back(AsmStringPiece(CurStringPiece));
516 CurStringPiece.clear();
517 }
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattner458cd9c2009-03-10 23:21:44 +0000519 // Handle %x4 and %x[foo] by capturing x as the modifier character.
520 char Modifier = '\0';
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000521 if (isLetter(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000522 if (CurPtr == StrEnd) { // Premature end.
523 DiagOffs = CurPtr-StrStart-1;
524 return diag::err_asm_invalid_escape;
525 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000526 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000527 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000530 if (isDigit(EscapedChar)) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000531 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000532 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattnercafc2222009-03-11 22:52:17 +0000534 --CurPtr;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000535 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000536 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Chris Lattner85759272009-03-11 00:23:13 +0000538 unsigned NumOperands =
539 getNumOutputs() + getNumPlusOperands() + getNumInputs();
540 if (N >= NumOperands) {
541 DiagOffs = CurPtr-StrStart-1;
542 return diag::err_asm_invalid_operand_number;
543 }
544
Chris Lattner458cd9c2009-03-10 23:21:44 +0000545 Pieces.push_back(AsmStringPiece(N, Modifier));
546 continue;
547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattner458cd9c2009-03-10 23:21:44 +0000549 // Handle %[foo], a symbolic operand reference.
550 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000551 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000553 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000554 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000555 if (NameEnd == 0)
556 return diag::err_asm_unterminated_symbolic_operand_name;
557 if (NameEnd == CurPtr)
558 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattner5f9e2722011-07-23 10:55:15 +0000560 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattner458cd9c2009-03-10 23:21:44 +0000562 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000563 if (N == -1) {
564 // Verify that an operand with that name exists.
565 DiagOffs = CurPtr-StrStart;
566 return diag::err_asm_unknown_symbolic_operand_name;
567 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000568 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000570 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000571 continue;
572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Chris Lattner2ff0f422009-03-10 23:57:07 +0000574 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000575 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000576 }
577}
Chad Rosierda083b22012-08-27 20:23:31 +0000578
579/// Assemble final IR asm string (GCC-style).
580std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000581 // Analyze the asm string to decompose it into its pieces. We know that Sema
582 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000583 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000584 unsigned DiagOffs;
585 AnalyzeAsmString(Pieces, C, DiagOffs);
586
587 std::string AsmString;
588 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
589 if (Pieces[i].isString())
590 AsmString += Pieces[i].getString();
591 else if (Pieces[i].getModifier() == '\0')
592 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
593 else
594 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
595 Pieces[i].getModifier() + '}';
596 }
597 return AsmString;
598}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000599
Chad Rosierda083b22012-08-27 20:23:31 +0000600/// Assemble final IR asm string (MS-style).
601std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
602 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000603 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000604}
605
Chad Rosier633abb02012-08-24 00:07:09 +0000606Expr *MSAsmStmt::getOutputExpr(unsigned i) {
607 return cast<Expr>(Exprs[i]);
608}
609
610Expr *MSAsmStmt::getInputExpr(unsigned i) {
611 return cast<Expr>(Exprs[i + NumOutputs]);
612}
613void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
614 Exprs[i + NumOutputs] = E;
615}
616
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000617QualType CXXCatchStmt::getCaughtType() const {
618 if (ExceptionDecl)
619 return ExceptionDecl->getType();
620 return QualType();
621}
622
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000623//===----------------------------------------------------------------------===//
624// Constructors
625//===----------------------------------------------------------------------===//
626
Chad Rosierdf5faf52012-08-25 00:11:56 +0000627GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
628 bool isvolatile, unsigned numoutputs, unsigned numinputs,
629 IdentifierInfo **names, StringLiteral **constraints,
630 Expr **exprs, StringLiteral *asmstr,
631 unsigned numclobbers, StringLiteral **clobbers,
632 SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000633 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
634 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000635
Chad Rosier0e2a8682012-08-07 23:12:23 +0000636 unsigned NumExprs = NumOutputs + NumInputs;
637
Anders Carlsson966146e2010-01-30 23:19:41 +0000638 Names = new (C) IdentifierInfo*[NumExprs];
639 std::copy(names, names + NumExprs, Names);
640
641 Exprs = new (C) Stmt*[NumExprs];
642 std::copy(exprs, exprs + NumExprs, Exprs);
643
644 Constraints = new (C) StringLiteral*[NumExprs];
645 std::copy(constraints, constraints + NumExprs, Constraints);
646
647 Clobbers = new (C) StringLiteral*[NumClobbers];
648 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000649}
650
Chad Rosier058ab172012-08-16 00:06:53 +0000651MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
652 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosiere54cba12012-10-16 21:55:39 +0000653 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallaeeacf72013-05-03 00:10:13 +0000654 unsigned numinputs,
Chad Rosiere54cba12012-10-16 21:55:39 +0000655 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
656 StringRef asmstr, ArrayRef<StringRef> clobbers,
657 SourceLocation endloc)
658 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
659 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
John McCallaeeacf72013-05-03 00:10:13 +0000660 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
Chad Rosier058ab172012-08-16 00:06:53 +0000661
John McCallaeeacf72013-05-03 00:10:13 +0000662 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
663}
Chad Rosier058ab172012-08-16 00:06:53 +0000664
John McCallaeeacf72013-05-03 00:10:13 +0000665static StringRef copyIntoContext(ASTContext &C, StringRef str) {
666 size_t size = str.size();
667 char *buffer = new (C) char[size];
668 memcpy(buffer, str.data(), size);
669 return StringRef(buffer, size);
670}
671
672void MSAsmStmt::initialize(ASTContext &C,
673 StringRef asmstr,
674 ArrayRef<Token> asmtoks,
675 ArrayRef<StringRef> constraints,
676 ArrayRef<Expr*> exprs,
677 ArrayRef<StringRef> clobbers) {
678 assert(NumAsmToks == asmtoks.size());
679 assert(NumClobbers == clobbers.size());
680
681 unsigned NumExprs = exprs.size();
682 assert(NumExprs == NumOutputs + NumInputs);
683 assert(NumExprs == constraints.size());
684
685 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier79efe242012-08-07 00:29:06 +0000686
Chad Rosier633abb02012-08-24 00:07:09 +0000687 Exprs = new (C) Stmt*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000688 for (unsigned i = 0, e = NumExprs; i != e; ++i)
689 Exprs[i] = exprs[i];
Chad Rosier633abb02012-08-24 00:07:09 +0000690
Chad Rosier79efe242012-08-07 00:29:06 +0000691 AsmToks = new (C) Token[NumAsmToks];
692 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
693 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000694
Chad Rosier89fb6d72012-08-28 20:28:20 +0000695 Constraints = new (C) StringRef[NumExprs];
696 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
John McCallaeeacf72013-05-03 00:10:13 +0000697 Constraints[i] = copyIntoContext(C, constraints[i]);
Chad Rosier89fb6d72012-08-28 20:28:20 +0000698 }
699
Chad Rosier33c72e12012-08-10 21:36:25 +0000700 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000701 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
702 // FIXME: Avoid the allocation/copy if at all possible.
John McCallaeeacf72013-05-03 00:10:13 +0000703 Clobbers[i] = copyIntoContext(C, clobbers[i]);
Chad Rosiere790bc32012-08-10 21:06:19 +0000704 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000705}
706
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000707ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
708 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000709 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000710: Stmt(ObjCForCollectionStmtClass) {
711 SubExprs[ELEM] = Elem;
712 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
713 SubExprs[BODY] = Body;
714 ForLoc = FCL;
715 RParenLoc = RPL;
716}
717
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000718ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
719 Stmt **CatchStmts, unsigned NumCatchStmts,
720 Stmt *atFinallyStmt)
721 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
722 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
723{
724 Stmt **Stmts = getStmts();
725 Stmts[0] = atTryStmt;
726 for (unsigned I = 0; I != NumCatchStmts; ++I)
727 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000728
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000729 if (HasFinally)
730 Stmts[NumCatchStmts + 1] = atFinallyStmt;
731}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000732
Chad Rosier0e2a8682012-08-07 23:12:23 +0000733ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
734 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000735 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000736 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000737 unsigned NumCatchStmts,
738 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000739 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000740 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000741 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000742 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
743 atFinallyStmt);
744}
Ted Kremenekff981022008-02-01 21:28:59 +0000745
Chad Rosier0e2a8682012-08-07 23:12:23 +0000746ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000747 unsigned NumCatchStmts,
748 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000749 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000750 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000751 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000752 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000753}
Nico Weber608b17f2008-08-05 23:15:29 +0000754
Erik Verbruggen65d78312012-12-25 14:51:39 +0000755SourceLocation ObjCAtTryStmt::getLocEnd() const {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000756 if (HasFinally)
Erik Verbruggen65d78312012-12-25 14:51:39 +0000757 return getFinallyStmt()->getLocEnd();
758 if (NumCatchStmts)
759 return getCatchStmt(NumCatchStmts - 1)->getLocEnd();
760 return getTryBody()->getLocEnd();
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000761}
762
Sam Weiniga1a396d2010-02-03 03:56:39 +0000763CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Nico Weber07cf58c2012-12-29 20:13:03 +0000764 Stmt *tryBlock, ArrayRef<Stmt*> handlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000765 std::size_t Size = sizeof(CXXTryStmt);
Nico Weber07cf58c2012-12-29 20:13:03 +0000766 Size += ((handlers.size() + 1) * sizeof(Stmt));
Sam Weiniga1a396d2010-02-03 03:56:39 +0000767
Chris Lattner32488542010-10-30 05:14:06 +0000768 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Nico Weber07cf58c2012-12-29 20:13:03 +0000769 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
Sam Weiniga1a396d2010-02-03 03:56:39 +0000770}
771
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000772CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
773 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>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000778 return new (Mem) CXXTryStmt(Empty, numHandlers);
779}
780
Sam Weiniga1a396d2010-02-03 03:56:39 +0000781CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Nico Weber07cf58c2012-12-29 20:13:03 +0000782 ArrayRef<Stmt*> handlers)
783 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000784 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000785 Stmts[0] = tryBlock;
Nico Weber07cf58c2012-12-29 20:13:03 +0000786 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000787}
788
Richard Smithad762fc2011-04-14 22:09:26 +0000789CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
790 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
791 Stmt *Body, SourceLocation FL,
792 SourceLocation CL, SourceLocation RPL)
793 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
794 SubExprs[RANGE] = Range;
795 SubExprs[BEGINEND] = BeginEndStmt;
796 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
797 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
798 SubExprs[LOOPVAR] = LoopVar;
799 SubExprs[BODY] = Body;
800}
801
802Expr *CXXForRangeStmt::getRangeInit() {
803 DeclStmt *RangeStmt = getRangeStmt();
804 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
805 assert(RangeDecl &&& "for-range should have a single var decl");
806 return RangeDecl->getInit();
807}
808
809const Expr *CXXForRangeStmt::getRangeInit() const {
810 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
811}
812
813VarDecl *CXXForRangeStmt::getLoopVariable() {
814 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
815 assert(LV && "No loop variable in CXXForRangeStmt");
816 return cast<VarDecl>(LV);
817}
818
819const VarDecl *CXXForRangeStmt::getLoopVariable() const {
820 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
821}
822
Chad Rosier0e2a8682012-08-07 23:12:23 +0000823IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000824 Stmt *then, SourceLocation EL, Stmt *elsev)
825 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000826{
827 setConditionVariable(C, var);
828 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
829 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000830 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000831}
832
833VarDecl *IfStmt::getConditionVariable() const {
834 if (!SubExprs[VAR])
835 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000836
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000837 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
838 return cast<VarDecl>(DS->getSingleDecl());
839}
840
841void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
842 if (!V) {
843 SubExprs[VAR] = 0;
844 return;
845 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000846
Daniel Dunbar96a00142012-03-09 18:35:03 +0000847 SourceRange VarRange = V->getSourceRange();
848 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
849 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000850}
851
Chad Rosier0e2a8682012-08-07 23:12:23 +0000852ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
853 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000854 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000855 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000856{
857 SubExprs[INIT] = Init;
858 setConditionVariable(C, condVar);
859 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
860 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
861 SubExprs[BODY] = Body;
862}
863
864VarDecl *ForStmt::getConditionVariable() const {
865 if (!SubExprs[CONDVAR])
866 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000867
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000868 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
869 return cast<VarDecl>(DS->getSingleDecl());
870}
871
872void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
873 if (!V) {
874 SubExprs[CONDVAR] = 0;
875 return;
876 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000877
Daniel Dunbar96a00142012-03-09 18:35:03 +0000878 SourceRange VarRange = V->getSourceRange();
879 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
880 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000881}
882
Chad Rosier0e2a8682012-08-07 23:12:23 +0000883SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
884 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000885{
886 setConditionVariable(C, Var);
887 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
888 SubExprs[BODY] = NULL;
889}
890
891VarDecl *SwitchStmt::getConditionVariable() const {
892 if (!SubExprs[VAR])
893 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000894
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000895 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
896 return cast<VarDecl>(DS->getSingleDecl());
897}
898
899void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
900 if (!V) {
901 SubExprs[VAR] = 0;
902 return;
903 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000904
Daniel Dunbar96a00142012-03-09 18:35:03 +0000905 SourceRange VarRange = V->getSourceRange();
906 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
907 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000908}
909
John McCall63c00d72011-02-09 08:16:59 +0000910Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000911 if (isa<CaseStmt>(this))
912 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000913 return cast<DefaultStmt>(this)->getSubStmt();
914}
915
Chad Rosier0e2a8682012-08-07 23:12:23 +0000916WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000917 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000918 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000919 setConditionVariable(C, Var);
920 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
921 SubExprs[BODY] = body;
922 WhileLoc = WL;
923}
924
925VarDecl *WhileStmt::getConditionVariable() const {
926 if (!SubExprs[VAR])
927 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000928
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000929 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
930 return cast<VarDecl>(DS->getSingleDecl());
931}
932
933void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
934 if (!V) {
935 SubExprs[VAR] = 0;
936 return;
937 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000938
939 SourceRange VarRange = V->getSourceRange();
940 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
941 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000942}
943
Ted Kremenek82977772007-08-24 21:09:09 +0000944// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000945LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000946 if (AddrLabelExpr *E =
947 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
948 return E->getLabel();
949 return 0;
950}
Ted Kremenek82977772007-08-24 21:09:09 +0000951
Ted Kremenek82977772007-08-24 21:09:09 +0000952// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000953const Expr* ReturnStmt::getRetValue() const {
954 return cast_or_null<Expr>(RetExpr);
955}
956Expr* ReturnStmt::getRetValue() {
957 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000958}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000959
960SEHTryStmt::SEHTryStmt(bool IsCXXTry,
961 SourceLocation TryLoc,
962 Stmt *TryBlock,
963 Stmt *Handler)
964 : Stmt(SEHTryStmtClass),
965 IsCXXTry(IsCXXTry),
966 TryLoc(TryLoc)
967{
968 Children[TRY] = TryBlock;
969 Children[HANDLER] = Handler;
970}
971
972SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
973 bool IsCXXTry,
974 SourceLocation TryLoc,
975 Stmt *TryBlock,
976 Stmt *Handler) {
977 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
978}
979
980SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
981 return dyn_cast<SEHExceptStmt>(getHandler());
982}
983
984SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
985 return dyn_cast<SEHFinallyStmt>(getHandler());
986}
987
988SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
989 Expr *FilterExpr,
990 Stmt *Block)
991 : Stmt(SEHExceptStmtClass),
992 Loc(Loc)
993{
994 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
995 Children[BLOCK] = Block;
996}
997
998SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
999 SourceLocation Loc,
1000 Expr *FilterExpr,
1001 Stmt *Block) {
1002 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1003}
1004
1005SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1006 Stmt *Block)
1007 : Stmt(SEHFinallyStmtClass),
1008 Loc(Loc),
1009 Block(Block)
1010{}
1011
1012SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
1013 SourceLocation Loc,
1014 Stmt *Block) {
1015 return new(C)SEHFinallyStmt(Loc,Block);
1016}
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001017
1018CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1019 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1020
1021 // Offset of the first Capture object.
1022 unsigned FirstCaptureOffset =
1023 llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1024
1025 return reinterpret_cast<Capture *>(
1026 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1027 + FirstCaptureOffset);
1028}
1029
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001030CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1031 ArrayRef<Capture> Captures,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001032 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001033 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001034 RecordDecl *RD)
1035 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001036 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001037 assert( S && "null captured statement");
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001038 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001039 assert(RD && "null record declaration for captured statement");
1040
1041 // Copy initialization expressions.
1042 Stmt **Stored = getStoredStmts();
1043 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1044 *Stored++ = CaptureInits[I];
1045
1046 // Copy the statement being captured.
1047 *Stored = S;
1048
1049 // Copy all Capture objects.
1050 Capture *Buffer = getStoredCaptures();
1051 std::copy(Captures.begin(), Captures.end(), Buffer);
1052}
1053
1054CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1055 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001056 CapDeclAndKind(0, CR_Default), TheRecordDecl(0) {
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001057 getStoredStmts()[NumCaptures] = 0;
1058}
1059
1060CapturedStmt *CapturedStmt::Create(ASTContext &Context, Stmt *S,
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001061 CapturedRegionKind Kind,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001062 ArrayRef<Capture> Captures,
1063 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001064 CapturedDecl *CD,
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001065 RecordDecl *RD) {
1066 // The layout is
1067 //
1068 // -----------------------------------------------------------
1069 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1070 // ----------------^-------------------^----------------------
1071 // getStoredStmts() getStoredCaptures()
1072 //
1073 // where S is the statement being captured.
1074 //
1075 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1076
1077 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1078 if (!Captures.empty()) {
1079 // Realign for the following Capture array.
1080 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1081 Size += sizeof(Capture) * Captures.size();
1082 }
1083
1084 void *Mem = Context.Allocate(Size);
Wei Pan9fd6b8f2013-05-04 03:59:06 +00001085 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001086}
1087
1088CapturedStmt *CapturedStmt::CreateDeserialized(ASTContext &Context,
1089 unsigned NumCaptures) {
1090 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1091 if (NumCaptures > 0) {
1092 // Realign for the following Capture array.
1093 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<Capture>());
1094 Size += sizeof(Capture) * NumCaptures;
1095 }
1096
1097 void *Mem = Context.Allocate(Size);
1098 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1099}
1100
1101Stmt::child_range CapturedStmt::children() {
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001102 // Children are captured field initilizers.
1103 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001104}
1105
1106bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Ben Langmuirdc5be4f2013-05-03 19:20:19 +00001107 for (const_capture_iterator I = capture_begin(),
1108 E = capture_end(); I != E; ++I) {
Richard Smith0d8e9642013-05-16 06:20:58 +00001109 if (!I->capturesVariable())
Tareq A. Siraj051303c2013-04-16 18:53:08 +00001110 continue;
1111
1112 // This does not handle variable redeclarations. This should be
1113 // extended to capture variables with redeclarations, for example
1114 // a thread-private variable in OpenMP.
1115 if (I->getCapturedVar() == Var)
1116 return true;
1117 }
1118
1119 return false;
1120}