blob: 952f0ddf25dd4f6c08b805fbb577830313e7c8d2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Stmt.h"
15#include "clang/AST/ExprCXX.h"
Steve Narofff494b572008-05-29 21:12:08 +000016#include "clang/AST/ExprObjC.h"
Chris Lattner16f00492009-04-26 01:32:48 +000017#include "clang/AST/StmtCXX.h"
18#include "clang/AST/StmtObjC.h"
Sebastian Redl4b07b292008-12-22 19:15:10 +000019#include "clang/AST/Type.h"
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000020#include "clang/AST/ASTContext.h"
Chris Lattner3182db12009-03-10 23:51:40 +000021#include "clang/AST/ASTDiagnostic.h"
Chris Lattner9bffb072010-04-23 16:29:58 +000022#include "clang/Basic/TargetInfo.h"
Chad Rosierbe3ace82012-08-24 17:05:45 +000023#include "llvm/ADT/StringExtras.h"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000024#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Reid Spencer5f016e22007-07-11 17:01:13 +000027static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000028 const char *Name;
29 unsigned Counter;
30 unsigned Size;
Sean Hunt4bfe1962010-05-05 15:24:00 +000031} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000032
33static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
34 static bool Initialized = false;
35 if (Initialized)
36 return StmtClassInfo[E];
37
38 // Intialize the table on the first use.
39 Initialized = true;
Sean Hunt7381d5c2010-05-18 06:22:21 +000040#define ABSTRACT_STMT(STMT)
Douglas Gregorf2cad862008-11-14 12:46:07 +000041#define STMT(CLASS, PARENT) \
42 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
43 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Sean Hunt4bfe1962010-05-05 15:24:00 +000044#include "clang/AST/StmtNodes.inc"
Nico Weber608b17f2008-08-05 23:15:29 +000045
Chris Lattner63381352007-08-25 01:42:24 +000046 return StmtClassInfo[E];
47}
48
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000049void *Stmt::operator new(size_t bytes, ASTContext& C,
50 unsigned alignment) throw() {
51 return ::operator new(bytes, C, alignment);
52}
53
54void *Stmt::operator new(size_t bytes, ASTContext* C,
55 unsigned alignment) throw() {
56 return ::operator new(bytes, *C, alignment);
57}
58
Reid Spencer5f016e22007-07-11 17:01:13 +000059const char *Stmt::getStmtClassName() const {
John McCall8e6285a2010-10-26 08:39:16 +000060 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000061}
62
63void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000064 // Ensure the table is primed.
65 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000066
Reid Spencer5f016e22007-07-11 17:01:13 +000067 unsigned sum = 0;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000068 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Sean Hunt4bfe1962010-05-05 15:24:00 +000069 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000070 if (StmtClassInfo[i].Name == 0) continue;
71 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000073 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000074 sum = 0;
Sean Hunt4bfe1962010-05-05 15:24:00 +000075 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000076 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000077 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000078 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
79 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
80 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
81 << " bytes)\n";
Chris Lattner63381352007-08-25 01:42:24 +000082 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000083 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000084
85 llvm::errs() << "Total bytes = " << sum << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
88void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000089 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000090}
91
Daniel Dunbar02892a62012-03-05 21:42:49 +000092bool Stmt::StatisticsEnabled = false;
93void Stmt::EnableStatistics() {
94 StatisticsEnabled = true;
Reid Spencer5f016e22007-07-11 17:01:13 +000095}
96
John McCall7e5e5f42011-07-07 06:58:02 +000097Stmt *Stmt::IgnoreImplicit() {
98 Stmt *s = this;
99
100 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
101 s = ewc->getSubExpr();
102
103 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
104 s = ice->getSubExpr();
105
106 return s;
107}
108
Chandler Carrutha1364be2011-09-10 00:02:34 +0000109/// \brief Strip off all label-like statements.
110///
Richard Smith534986f2012-04-14 00:33:13 +0000111/// This will strip off label statements, case statements, attributed
112/// statements and default statements recursively.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000113const Stmt *Stmt::stripLabelLikeStatements() const {
114 const Stmt *S = this;
115 while (true) {
116 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
117 S = LS->getSubStmt();
118 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
119 S = SC->getSubStmt();
Richard Smith534986f2012-04-14 00:33:13 +0000120 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
121 S = AS->getSubStmt();
Chandler Carrutha1364be2011-09-10 00:02:34 +0000122 else
123 return S;
124 }
125}
126
John McCall63c00d72011-02-09 08:16:59 +0000127namespace {
128 struct good {};
129 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +0000130
131 // These silly little functions have to be static inline to suppress
132 // unused warnings, and they have to be defined to suppress other
133 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000134 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +0000135
136 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000137 template <class T> good implements_children(children_t T::*) {
138 return good();
139 }
140 static inline bad implements_children(children_t Stmt::*) {
141 return bad();
142 }
John McCall63c00d72011-02-09 08:16:59 +0000143
144 typedef SourceRange getSourceRange_t() const;
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000145 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
146 return good();
147 }
148 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
149 return bad();
150 }
John McCall63c00d72011-02-09 08:16:59 +0000151
152#define ASSERT_IMPLEMENTS_children(type) \
153 (void) sizeof(is_good(implements_children(&type::children)))
154#define ASSERT_IMPLEMENTS_getSourceRange(type) \
155 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
156}
157
158/// Check whether the various Stmt classes implement their member
159/// functions.
160static inline void check_implementations() {
161#define ABSTRACT_STMT(type)
162#define STMT(type, base) \
163 ASSERT_IMPLEMENTS_children(type); \
164 ASSERT_IMPLEMENTS_getSourceRange(type);
165#include "clang/AST/StmtNodes.inc"
166}
167
168Stmt::child_range Stmt::children() {
169 switch (getStmtClass()) {
170 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
171#define ABSTRACT_STMT(type)
172#define STMT(type, base) \
173 case Stmt::type##Class: \
174 return static_cast<type*>(this)->children();
175#include "clang/AST/StmtNodes.inc"
176 }
177 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000178}
179
180SourceRange Stmt::getSourceRange() const {
181 switch (getStmtClass()) {
182 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
183#define ABSTRACT_STMT(type)
184#define STMT(type, base) \
185 case Stmt::type##Class: \
186 return static_cast<const type*>(this)->getSourceRange();
187#include "clang/AST/StmtNodes.inc"
188 }
189 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000190}
191
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000192// Amusing macro metaprogramming hack: check whether a class provides
193// a more specific implementation of getLocStart() and getLocEnd().
194//
195// See also Expr.cpp:getExprLoc().
196namespace {
197 /// This implementation is used when a class provides a custom
198 /// implementation of getLocStart.
199 template <class S, class T>
200 SourceLocation getLocStartImpl(const Stmt *stmt,
201 SourceLocation (T::*v)() const) {
202 return static_cast<const S*>(stmt)->getLocStart();
203 }
204
205 /// This implementation is used when a class doesn't provide a custom
206 /// implementation of getLocStart. Overload resolution should pick it over
207 /// the implementation above because it's more specialized according to
208 /// function template partial ordering.
209 template <class S>
210 SourceLocation getLocStartImpl(const Stmt *stmt,
211 SourceLocation (Stmt::*v)() const) {
212 return static_cast<const S*>(stmt)->getSourceRange().getBegin();
213 }
214
215 /// This implementation is used when a class provides a custom
216 /// implementation of getLocEnd.
217 template <class S, class T>
218 SourceLocation getLocEndImpl(const Stmt *stmt,
219 SourceLocation (T::*v)() const) {
220 return static_cast<const S*>(stmt)->getLocEnd();
221 }
222
223 /// This implementation is used when a class doesn't provide a custom
224 /// implementation of getLocEnd. Overload resolution should pick it over
225 /// the implementation above because it's more specialized according to
226 /// function template partial ordering.
227 template <class S>
228 SourceLocation getLocEndImpl(const Stmt *stmt,
229 SourceLocation (Stmt::*v)() const) {
230 return static_cast<const S*>(stmt)->getSourceRange().getEnd();
231 }
232}
233
234SourceLocation Stmt::getLocStart() const {
235 switch (getStmtClass()) {
236 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
237#define ABSTRACT_STMT(type)
238#define STMT(type, base) \
239 case Stmt::type##Class: \
240 return getLocStartImpl<type>(this, &type::getLocStart);
241#include "clang/AST/StmtNodes.inc"
242 }
243 llvm_unreachable("unknown statement kind");
244}
245
246SourceLocation Stmt::getLocEnd() const {
247 switch (getStmtClass()) {
248 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
249#define ABSTRACT_STMT(type)
250#define STMT(type, base) \
251 case Stmt::type##Class: \
252 return getLocEndImpl<type>(this, &type::getLocEnd);
253#include "clang/AST/StmtNodes.inc"
254 }
255 llvm_unreachable("unknown statement kind");
256}
257
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000258CompoundStmt::CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts,
259 SourceLocation LB, SourceLocation RB)
260 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
261 CompoundStmtBits.NumStmts = NumStmts;
262 assert(CompoundStmtBits.NumStmts == NumStmts &&
263 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
264
265 if (NumStmts == 0) {
266 Body = 0;
267 return;
268 }
269
270 Body = new (C) Stmt*[NumStmts];
271 memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
272}
273
Douglas Gregor025452f2009-04-17 00:04:06 +0000274void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
275 if (this->Body)
276 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000277 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000278
279 Body = new (C) Stmt*[NumStmts];
280 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
281}
Reid Spencer5f016e22007-07-11 17:01:13 +0000282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000284 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000285}
286
Alexander Kornienko49908902012-07-09 10:04:07 +0000287AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
288 ArrayRef<const Attr*> Attrs,
289 Stmt *SubStmt) {
290 void *Mem = C.Allocate(sizeof(AttributedStmt) +
291 sizeof(Attr*) * (Attrs.size() - 1),
292 llvm::alignOf<AttributedStmt>());
293 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
294}
295
296AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
297 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
298 void *Mem = C.Allocate(sizeof(AttributedStmt) +
299 sizeof(Attr*) * (NumAttrs - 1),
300 llvm::alignOf<AttributedStmt>());
301 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
302}
303
Steve Naroff507f2d52007-08-31 23:49:30 +0000304// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000305SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000306 if (RetExpr)
307 return SourceRange(RetLoc, RetExpr->getLocEnd());
308 else
309 return SourceRange(RetLoc);
310}
311
Ted Kremenekd48ade62007-10-01 16:34:52 +0000312bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000313 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000314 default:
315 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000316
Ted Kremenekd48ade62007-10-01 16:34:52 +0000317 case CallExprClass:
318 case ConditionalOperatorClass:
319 case ChooseExprClass:
320 case StmtExprClass:
321 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000322 return true;
323
Ted Kremenekd48ade62007-10-01 16:34:52 +0000324 case Stmt::BinaryOperatorClass: {
325 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000326 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000327 return true;
328 else
329 return false;
330 }
331 }
332}
333
Chad Rosieraba59aa2012-08-28 17:43:23 +0000334std::string AsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000335 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
336 return gccAsmStmt->generateAsmString(C);
337 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
338 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000339 llvm_unreachable("unknown asm statement kind!");
340}
341
342StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000343 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
344 return gccAsmStmt->getOutputConstraint(i);
345 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
346 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000347 llvm_unreachable("unknown asm statement kind!");
348}
349
350const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000351 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
352 return gccAsmStmt->getOutputExpr(i);
353 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
354 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000355 llvm_unreachable("unknown asm statement kind!");
356}
357
358StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000359 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
360 return gccAsmStmt->getInputConstraint(i);
361 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
362 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000363 llvm_unreachable("unknown asm statement kind!");
364}
365
366const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000367 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
368 return gccAsmStmt->getInputExpr(i);
369 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
370 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000371 llvm_unreachable("unknown asm statement kind!");
372}
373
374StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000375 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
376 return gccAsmStmt->getClobber(i);
377 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
378 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000379 llvm_unreachable("unknown asm statement kind!");
380}
381
Chad Rosierc4fb2212012-08-28 00:24:05 +0000382/// getNumPlusOperands - Return the number of output operands that have a "+"
383/// constraint.
384unsigned AsmStmt::getNumPlusOperands() const {
385 unsigned Res = 0;
386 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
387 if (isOutputPlusConstraint(i))
388 ++Res;
389 return Res;
390}
391
Chad Rosier33f05582012-08-27 23:47:56 +0000392StringRef GCCAsmStmt::getClobber(unsigned i) const {
393 return getClobberStringLiteral(i)->getString();
394}
395
Chad Rosierdf5faf52012-08-25 00:11:56 +0000396Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000397 return cast<Expr>(Exprs[i]);
398}
Chris Lattnerb3277932009-03-10 04:59:06 +0000399
400/// getOutputConstraint - Return the constraint string for the specified
401/// output operand. All output constraints are known to be non-empty (either
402/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000403StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000404 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000405}
Chris Lattnerb3277932009-03-10 04:59:06 +0000406
Chad Rosierdf5faf52012-08-25 00:11:56 +0000407Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000408 return cast<Expr>(Exprs[i + NumOutputs]);
409}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000410void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000411 Exprs[i + NumOutputs] = E;
412}
413
Chris Lattnerb3277932009-03-10 04:59:06 +0000414/// getInputConstraint - Return the specified input constraint. Unlike output
415/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000416StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000417 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000418}
419
Chad Rosierdf5faf52012-08-25 00:11:56 +0000420void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000421 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000422 StringLiteral **Constraints,
423 Stmt **Exprs,
424 unsigned NumOutputs,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000425 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000426 StringLiteral **Clobbers,
427 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000428 this->NumOutputs = NumOutputs;
429 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000430 this->NumClobbers = NumClobbers;
431
432 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000433
Anders Carlsson966146e2010-01-30 23:19:41 +0000434 C.Deallocate(this->Names);
435 this->Names = new (C) IdentifierInfo*[NumExprs];
436 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000437
Anders Carlsson966146e2010-01-30 23:19:41 +0000438 C.Deallocate(this->Exprs);
439 this->Exprs = new (C) Stmt*[NumExprs];
440 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000441
Anders Carlsson966146e2010-01-30 23:19:41 +0000442 C.Deallocate(this->Constraints);
443 this->Constraints = new (C) StringLiteral*[NumExprs];
444 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000445
Anders Carlsson966146e2010-01-30 23:19:41 +0000446 C.Deallocate(this->Clobbers);
447 this->Clobbers = new (C) StringLiteral*[NumClobbers];
448 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000449}
450
Chris Lattner10ca96a2009-03-10 06:33:24 +0000451/// getNamedOperand - Given a symbolic operand reference like %[foo],
452/// translate this into a numeric value needed to reference the same operand.
453/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000454int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000455 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner10ca96a2009-03-10 06:33:24 +0000457 // Check if this is an output operand.
458 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
459 if (getOutputName(i) == SymbolicName)
460 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattner10ca96a2009-03-10 06:33:24 +0000463 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
464 if (getInputName(i) == SymbolicName)
465 return getNumOutputs() + NumPlusOperands + i;
466
467 // Not found.
468 return -1;
469}
470
Chris Lattner458cd9c2009-03-10 23:21:44 +0000471/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
472/// it into pieces. If the asm string is erroneous, emit errors and return
473/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000474unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000475 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000476 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000477 const char *StrStart = Str.begin();
478 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000479 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner458cd9c2009-03-10 23:21:44 +0000481 // "Simple" inline asms have no constraints or operands, just convert the asm
482 // string to escape $'s.
483 if (isSimple()) {
484 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000485 for (; CurPtr != StrEnd; ++CurPtr) {
486 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000487 case '$':
488 Result += "$$";
489 break;
490 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000491 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000492 break;
493 }
494 }
495 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000496 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000497 }
498
499 // CurStringPiece - The current string that we are building up as we scan the
500 // asm string.
501 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000503 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000504
Chris Lattner458cd9c2009-03-10 23:21:44 +0000505 while (1) {
506 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000507 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000508 if (!CurStringPiece.empty())
509 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000510 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattner3182db12009-03-10 23:51:40 +0000513 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000514 switch (CurChar) {
515 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000516 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
517 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
518 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000519 case '%':
520 break;
521 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000522 CurStringPiece += CurChar;
523 continue;
524 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000525
Chris Lattner458cd9c2009-03-10 23:21:44 +0000526 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000527 if (CurPtr == StrEnd) {
528 // % at end of string is invalid (no escape).
529 DiagOffs = CurPtr-StrStart-1;
530 return diag::err_asm_invalid_escape;
531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattner3182db12009-03-10 23:51:40 +0000533 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000534 if (EscapedChar == '%') { // %% -> %
535 // Escaped percentage sign.
536 CurStringPiece += '%';
537 continue;
538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattner458cd9c2009-03-10 23:21:44 +0000540 if (EscapedChar == '=') { // %= -> Generate an unique ID.
541 CurStringPiece += "${:uid}";
542 continue;
543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattner458cd9c2009-03-10 23:21:44 +0000545 // Otherwise, we have an operand. If we have accumulated a string so far,
546 // add it to the Pieces list.
547 if (!CurStringPiece.empty()) {
548 Pieces.push_back(AsmStringPiece(CurStringPiece));
549 CurStringPiece.clear();
550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattner458cd9c2009-03-10 23:21:44 +0000552 // Handle %x4 and %x[foo] by capturing x as the modifier character.
553 char Modifier = '\0';
554 if (isalpha(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000555 if (CurPtr == StrEnd) { // Premature end.
556 DiagOffs = CurPtr-StrStart-1;
557 return diag::err_asm_invalid_escape;
558 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000559 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000560 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000561 }
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Chris Lattner458cd9c2009-03-10 23:21:44 +0000563 if (isdigit(EscapedChar)) {
564 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000565 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattnercafc2222009-03-11 22:52:17 +0000567 --CurPtr;
568 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000569 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner85759272009-03-11 00:23:13 +0000571 unsigned NumOperands =
572 getNumOutputs() + getNumPlusOperands() + getNumInputs();
573 if (N >= NumOperands) {
574 DiagOffs = CurPtr-StrStart-1;
575 return diag::err_asm_invalid_operand_number;
576 }
577
Chris Lattner458cd9c2009-03-10 23:21:44 +0000578 Pieces.push_back(AsmStringPiece(N, Modifier));
579 continue;
580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Chris Lattner458cd9c2009-03-10 23:21:44 +0000582 // Handle %[foo], a symbolic operand reference.
583 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000584 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000586 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000587 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000588 if (NameEnd == 0)
589 return diag::err_asm_unterminated_symbolic_operand_name;
590 if (NameEnd == CurPtr)
591 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Chris Lattner5f9e2722011-07-23 10:55:15 +0000593 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Chris Lattner458cd9c2009-03-10 23:21:44 +0000595 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000596 if (N == -1) {
597 // Verify that an operand with that name exists.
598 DiagOffs = CurPtr-StrStart;
599 return diag::err_asm_unknown_symbolic_operand_name;
600 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000601 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000603 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000604 continue;
605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Chris Lattner2ff0f422009-03-10 23:57:07 +0000607 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000608 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000609 }
610}
Chad Rosierda083b22012-08-27 20:23:31 +0000611
612/// Assemble final IR asm string (GCC-style).
613std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000614 // Analyze the asm string to decompose it into its pieces. We know that Sema
615 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000616 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000617 unsigned DiagOffs;
618 AnalyzeAsmString(Pieces, C, DiagOffs);
619
620 std::string AsmString;
621 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
622 if (Pieces[i].isString())
623 AsmString += Pieces[i].getString();
624 else if (Pieces[i].getModifier() == '\0')
625 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
626 else
627 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
628 Pieces[i].getModifier() + '}';
629 }
630 return AsmString;
631}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000632
Chad Rosierda083b22012-08-27 20:23:31 +0000633/// Assemble final IR asm string (MS-style).
634std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
635 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000636 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000637}
638
Chad Rosier633abb02012-08-24 00:07:09 +0000639Expr *MSAsmStmt::getOutputExpr(unsigned i) {
640 return cast<Expr>(Exprs[i]);
641}
642
643Expr *MSAsmStmt::getInputExpr(unsigned i) {
644 return cast<Expr>(Exprs[i + NumOutputs]);
645}
646void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
647 Exprs[i + NumOutputs] = E;
648}
649
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000650QualType CXXCatchStmt::getCaughtType() const {
651 if (ExceptionDecl)
652 return ExceptionDecl->getType();
653 return QualType();
654}
655
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000656//===----------------------------------------------------------------------===//
657// Constructors
658//===----------------------------------------------------------------------===//
659
Chad Rosierdf5faf52012-08-25 00:11:56 +0000660GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
661 bool isvolatile, unsigned numoutputs, unsigned numinputs,
662 IdentifierInfo **names, StringLiteral **constraints,
663 Expr **exprs, StringLiteral *asmstr,
664 unsigned numclobbers, StringLiteral **clobbers,
665 SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000666 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
667 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000668
Chad Rosier0e2a8682012-08-07 23:12:23 +0000669 unsigned NumExprs = NumOutputs + NumInputs;
670
Anders Carlsson966146e2010-01-30 23:19:41 +0000671 Names = new (C) IdentifierInfo*[NumExprs];
672 std::copy(names, names + NumExprs, Names);
673
674 Exprs = new (C) Stmt*[NumExprs];
675 std::copy(exprs, exprs + NumExprs, Exprs);
676
677 Constraints = new (C) StringLiteral*[NumExprs];
678 std::copy(constraints, constraints + NumExprs, Constraints);
679
680 Clobbers = new (C) StringLiteral*[NumClobbers];
681 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000682}
683
Chad Rosier058ab172012-08-16 00:06:53 +0000684MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
685 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosiere54cba12012-10-16 21:55:39 +0000686 ArrayRef<Token> asmtoks, unsigned numoutputs,
687 unsigned numinputs, ArrayRef<IdentifierInfo*> names,
688 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
689 StringRef asmstr, ArrayRef<StringRef> clobbers,
690 SourceLocation endloc)
691 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
692 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
Chad Rosier12603e22012-09-04 16:36:26 +0000693 EndLoc(endloc), AsmStr(asmstr.str()), NumAsmToks(asmtoks.size()) {
Chad Rosier058ab172012-08-16 00:06:53 +0000694
695 unsigned NumExprs = NumOutputs + NumInputs;
696
697 Names = new (C) IdentifierInfo*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000698 for (unsigned i = 0, e = NumExprs; i != e; ++i)
699 Names[i] = names[i];
Chad Rosier79efe242012-08-07 00:29:06 +0000700
Chad Rosier633abb02012-08-24 00:07:09 +0000701 Exprs = new (C) Stmt*[NumExprs];
Chad Rosiere54cba12012-10-16 21:55:39 +0000702 for (unsigned i = 0, e = NumExprs; i != e; ++i)
703 Exprs[i] = exprs[i];
Chad Rosier633abb02012-08-24 00:07:09 +0000704
Chad Rosier79efe242012-08-07 00:29:06 +0000705 AsmToks = new (C) Token[NumAsmToks];
706 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
707 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000708
Chad Rosier89fb6d72012-08-28 20:28:20 +0000709 Constraints = new (C) StringRef[NumExprs];
710 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
711 size_t size = constraints[i].size();
712 char *dest = new (C) char[size];
713 std::strncpy(dest, constraints[i].data(), size);
714 Constraints[i] = StringRef(dest, size);
715 }
716
Chad Rosier33c72e12012-08-10 21:36:25 +0000717 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000718 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
719 // FIXME: Avoid the allocation/copy if at all possible.
720 size_t size = clobbers[i].size();
721 char *dest = new (C) char[size];
722 std::strncpy(dest, clobbers[i].data(), size);
Chad Rosier33c72e12012-08-10 21:36:25 +0000723 Clobbers[i] = StringRef(dest, size);
Chad Rosiere790bc32012-08-10 21:06:19 +0000724 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000725}
726
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000727ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
728 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000729 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000730: Stmt(ObjCForCollectionStmtClass) {
731 SubExprs[ELEM] = Elem;
732 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
733 SubExprs[BODY] = Body;
734 ForLoc = FCL;
735 RParenLoc = RPL;
736}
737
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000738ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
739 Stmt **CatchStmts, unsigned NumCatchStmts,
740 Stmt *atFinallyStmt)
741 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
742 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
743{
744 Stmt **Stmts = getStmts();
745 Stmts[0] = atTryStmt;
746 for (unsigned I = 0; I != NumCatchStmts; ++I)
747 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000748
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000749 if (HasFinally)
750 Stmts[NumCatchStmts + 1] = atFinallyStmt;
751}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000752
Chad Rosier0e2a8682012-08-07 23:12:23 +0000753ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
754 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000755 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000756 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000757 unsigned NumCatchStmts,
758 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000759 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000760 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000761 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000762 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
763 atFinallyStmt);
764}
Ted Kremenekff981022008-02-01 21:28:59 +0000765
Chad Rosier0e2a8682012-08-07 23:12:23 +0000766ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000767 unsigned NumCatchStmts,
768 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000769 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000770 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000771 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000772 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000773}
Nico Weber608b17f2008-08-05 23:15:29 +0000774
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000775SourceRange ObjCAtTryStmt::getSourceRange() const {
776 SourceLocation EndLoc;
777 if (HasFinally)
778 EndLoc = getFinallyStmt()->getLocEnd();
779 else if (NumCatchStmts)
780 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
781 else
782 EndLoc = getTryBody()->getLocEnd();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000783
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000784 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000785}
786
Sam Weiniga1a396d2010-02-03 03:56:39 +0000787CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000788 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000789 unsigned numHandlers) {
790 std::size_t Size = sizeof(CXXTryStmt);
791 Size += ((numHandlers + 1) * sizeof(Stmt));
792
Chris Lattner32488542010-10-30 05:14:06 +0000793 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000794 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
795}
796
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000797CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
798 unsigned numHandlers) {
799 std::size_t Size = sizeof(CXXTryStmt);
800 Size += ((numHandlers + 1) * sizeof(Stmt));
801
Chris Lattner32488542010-10-30 05:14:06 +0000802 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000803 return new (Mem) CXXTryStmt(Empty, numHandlers);
804}
805
Sam Weiniga1a396d2010-02-03 03:56:39 +0000806CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000807 Stmt **handlers, unsigned numHandlers)
808 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000809 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000810 Stmts[0] = tryBlock;
811 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
812}
813
Richard Smithad762fc2011-04-14 22:09:26 +0000814CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
815 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
816 Stmt *Body, SourceLocation FL,
817 SourceLocation CL, SourceLocation RPL)
818 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
819 SubExprs[RANGE] = Range;
820 SubExprs[BEGINEND] = BeginEndStmt;
821 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
822 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
823 SubExprs[LOOPVAR] = LoopVar;
824 SubExprs[BODY] = Body;
825}
826
827Expr *CXXForRangeStmt::getRangeInit() {
828 DeclStmt *RangeStmt = getRangeStmt();
829 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
830 assert(RangeDecl &&& "for-range should have a single var decl");
831 return RangeDecl->getInit();
832}
833
834const Expr *CXXForRangeStmt::getRangeInit() const {
835 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
836}
837
838VarDecl *CXXForRangeStmt::getLoopVariable() {
839 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
840 assert(LV && "No loop variable in CXXForRangeStmt");
841 return cast<VarDecl>(LV);
842}
843
844const VarDecl *CXXForRangeStmt::getLoopVariable() const {
845 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
846}
847
Chad Rosier0e2a8682012-08-07 23:12:23 +0000848IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000849 Stmt *then, SourceLocation EL, Stmt *elsev)
850 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000851{
852 setConditionVariable(C, var);
853 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
854 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000855 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000856}
857
858VarDecl *IfStmt::getConditionVariable() const {
859 if (!SubExprs[VAR])
860 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000861
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000862 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
863 return cast<VarDecl>(DS->getSingleDecl());
864}
865
866void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
867 if (!V) {
868 SubExprs[VAR] = 0;
869 return;
870 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000871
Daniel Dunbar96a00142012-03-09 18:35:03 +0000872 SourceRange VarRange = V->getSourceRange();
873 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
874 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000875}
876
Chad Rosier0e2a8682012-08-07 23:12:23 +0000877ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
878 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000879 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000880 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000881{
882 SubExprs[INIT] = Init;
883 setConditionVariable(C, condVar);
884 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
885 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
886 SubExprs[BODY] = Body;
887}
888
889VarDecl *ForStmt::getConditionVariable() const {
890 if (!SubExprs[CONDVAR])
891 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000892
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000893 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
894 return cast<VarDecl>(DS->getSingleDecl());
895}
896
897void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
898 if (!V) {
899 SubExprs[CONDVAR] = 0;
900 return;
901 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000902
Daniel Dunbar96a00142012-03-09 18:35:03 +0000903 SourceRange VarRange = V->getSourceRange();
904 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
905 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000906}
907
Chad Rosier0e2a8682012-08-07 23:12:23 +0000908SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
909 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000910{
911 setConditionVariable(C, Var);
912 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
913 SubExprs[BODY] = NULL;
914}
915
916VarDecl *SwitchStmt::getConditionVariable() const {
917 if (!SubExprs[VAR])
918 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000919
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000920 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
921 return cast<VarDecl>(DS->getSingleDecl());
922}
923
924void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
925 if (!V) {
926 SubExprs[VAR] = 0;
927 return;
928 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000929
Daniel Dunbar96a00142012-03-09 18:35:03 +0000930 SourceRange VarRange = V->getSourceRange();
931 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
932 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000933}
934
John McCall63c00d72011-02-09 08:16:59 +0000935Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000936 if (isa<CaseStmt>(this))
937 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000938 return cast<DefaultStmt>(this)->getSubStmt();
939}
940
Chad Rosier0e2a8682012-08-07 23:12:23 +0000941WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000942 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000943 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000944 setConditionVariable(C, Var);
945 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
946 SubExprs[BODY] = body;
947 WhileLoc = WL;
948}
949
950VarDecl *WhileStmt::getConditionVariable() const {
951 if (!SubExprs[VAR])
952 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000953
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000954 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
955 return cast<VarDecl>(DS->getSingleDecl());
956}
957
958void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
959 if (!V) {
960 SubExprs[VAR] = 0;
961 return;
962 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000963
964 SourceRange VarRange = V->getSourceRange();
965 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
966 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000967}
968
Ted Kremenek82977772007-08-24 21:09:09 +0000969// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000970LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000971 if (AddrLabelExpr *E =
972 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
973 return E->getLabel();
974 return 0;
975}
Ted Kremenek82977772007-08-24 21:09:09 +0000976
Ted Kremenek82977772007-08-24 21:09:09 +0000977// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000978const Expr* ReturnStmt::getRetValue() const {
979 return cast_or_null<Expr>(RetExpr);
980}
981Expr* ReturnStmt::getRetValue() {
982 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000983}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000984
985SEHTryStmt::SEHTryStmt(bool IsCXXTry,
986 SourceLocation TryLoc,
987 Stmt *TryBlock,
988 Stmt *Handler)
989 : Stmt(SEHTryStmtClass),
990 IsCXXTry(IsCXXTry),
991 TryLoc(TryLoc)
992{
993 Children[TRY] = TryBlock;
994 Children[HANDLER] = Handler;
995}
996
997SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
998 bool IsCXXTry,
999 SourceLocation TryLoc,
1000 Stmt *TryBlock,
1001 Stmt *Handler) {
1002 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
1003}
1004
1005SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1006 return dyn_cast<SEHExceptStmt>(getHandler());
1007}
1008
1009SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1010 return dyn_cast<SEHFinallyStmt>(getHandler());
1011}
1012
1013SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
1014 Expr *FilterExpr,
1015 Stmt *Block)
1016 : Stmt(SEHExceptStmtClass),
1017 Loc(Loc)
1018{
1019 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
1020 Children[BLOCK] = Block;
1021}
1022
1023SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
1024 SourceLocation Loc,
1025 Expr *FilterExpr,
1026 Stmt *Block) {
1027 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1028}
1029
1030SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1031 Stmt *Block)
1032 : Stmt(SEHFinallyStmtClass),
1033 Loc(Loc),
1034 Block(Block)
1035{}
1036
1037SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
1038 SourceLocation Loc,
1039 Stmt *Block) {
1040 return new(C)SEHFinallyStmt(Loc,Block);
1041}