blob: db13f67e90f7df1cd127359b983ca335f8d7bd83 [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
Reid Spencer5f016e22007-07-11 17:01:13 +000049const char *Stmt::getStmtClassName() const {
John McCall8e6285a2010-10-26 08:39:16 +000050 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000051}
52
53void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000054 // Ensure the table is primed.
55 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000056
Reid Spencer5f016e22007-07-11 17:01:13 +000057 unsigned sum = 0;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000058 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Sean Hunt4bfe1962010-05-05 15:24:00 +000059 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000060 if (StmtClassInfo[i].Name == 0) continue;
61 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000062 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000063 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000064 sum = 0;
Sean Hunt4bfe1962010-05-05 15:24:00 +000065 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000066 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000067 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000068 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
69 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
70 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
71 << " bytes)\n";
Chris Lattner63381352007-08-25 01:42:24 +000072 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000073 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000074
75 llvm::errs() << "Total bytes = " << sum << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000076}
77
78void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000079 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000080}
81
Daniel Dunbar02892a62012-03-05 21:42:49 +000082bool Stmt::StatisticsEnabled = false;
83void Stmt::EnableStatistics() {
84 StatisticsEnabled = true;
Reid Spencer5f016e22007-07-11 17:01:13 +000085}
86
John McCall7e5e5f42011-07-07 06:58:02 +000087Stmt *Stmt::IgnoreImplicit() {
88 Stmt *s = this;
89
90 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
91 s = ewc->getSubExpr();
92
93 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
94 s = ice->getSubExpr();
95
96 return s;
97}
98
Chandler Carrutha1364be2011-09-10 00:02:34 +000099/// \brief Strip off all label-like statements.
100///
Richard Smith534986f2012-04-14 00:33:13 +0000101/// This will strip off label statements, case statements, attributed
102/// statements and default statements recursively.
Chandler Carrutha1364be2011-09-10 00:02:34 +0000103const Stmt *Stmt::stripLabelLikeStatements() const {
104 const Stmt *S = this;
105 while (true) {
106 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
107 S = LS->getSubStmt();
108 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
109 S = SC->getSubStmt();
Richard Smith534986f2012-04-14 00:33:13 +0000110 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
111 S = AS->getSubStmt();
Chandler Carrutha1364be2011-09-10 00:02:34 +0000112 else
113 return S;
114 }
115}
116
John McCall63c00d72011-02-09 08:16:59 +0000117namespace {
118 struct good {};
119 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +0000120
121 // These silly little functions have to be static inline to suppress
122 // unused warnings, and they have to be defined to suppress other
123 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000124 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +0000125
126 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000127 template <class T> good implements_children(children_t T::*) {
128 return good();
129 }
130 static inline bad implements_children(children_t Stmt::*) {
131 return bad();
132 }
John McCall63c00d72011-02-09 08:16:59 +0000133
134 typedef SourceRange getSourceRange_t() const;
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000135 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
136 return good();
137 }
138 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
139 return bad();
140 }
John McCall63c00d72011-02-09 08:16:59 +0000141
142#define ASSERT_IMPLEMENTS_children(type) \
143 (void) sizeof(is_good(implements_children(&type::children)))
144#define ASSERT_IMPLEMENTS_getSourceRange(type) \
145 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
146}
147
148/// Check whether the various Stmt classes implement their member
149/// functions.
150static inline void check_implementations() {
151#define ABSTRACT_STMT(type)
152#define STMT(type, base) \
153 ASSERT_IMPLEMENTS_children(type); \
154 ASSERT_IMPLEMENTS_getSourceRange(type);
155#include "clang/AST/StmtNodes.inc"
156}
157
158Stmt::child_range Stmt::children() {
159 switch (getStmtClass()) {
160 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
161#define ABSTRACT_STMT(type)
162#define STMT(type, base) \
163 case Stmt::type##Class: \
164 return static_cast<type*>(this)->children();
165#include "clang/AST/StmtNodes.inc"
166 }
167 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000168}
169
170SourceRange Stmt::getSourceRange() const {
171 switch (getStmtClass()) {
172 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
173#define ABSTRACT_STMT(type)
174#define STMT(type, base) \
175 case Stmt::type##Class: \
176 return static_cast<const type*>(this)->getSourceRange();
177#include "clang/AST/StmtNodes.inc"
178 }
179 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000180}
181
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000182// Amusing macro metaprogramming hack: check whether a class provides
183// a more specific implementation of getLocStart() and getLocEnd().
184//
185// See also Expr.cpp:getExprLoc().
186namespace {
187 /// This implementation is used when a class provides a custom
188 /// implementation of getLocStart.
189 template <class S, class T>
190 SourceLocation getLocStartImpl(const Stmt *stmt,
191 SourceLocation (T::*v)() const) {
192 return static_cast<const S*>(stmt)->getLocStart();
193 }
194
195 /// This implementation is used when a class doesn't provide a custom
196 /// implementation of getLocStart. Overload resolution should pick it over
197 /// the implementation above because it's more specialized according to
198 /// function template partial ordering.
199 template <class S>
200 SourceLocation getLocStartImpl(const Stmt *stmt,
201 SourceLocation (Stmt::*v)() const) {
202 return static_cast<const S*>(stmt)->getSourceRange().getBegin();
203 }
204
205 /// This implementation is used when a class provides a custom
206 /// implementation of getLocEnd.
207 template <class S, class T>
208 SourceLocation getLocEndImpl(const Stmt *stmt,
209 SourceLocation (T::*v)() const) {
210 return static_cast<const S*>(stmt)->getLocEnd();
211 }
212
213 /// This implementation is used when a class doesn't provide a custom
214 /// implementation of getLocEnd. Overload resolution should pick it over
215 /// the implementation above because it's more specialized according to
216 /// function template partial ordering.
217 template <class S>
218 SourceLocation getLocEndImpl(const Stmt *stmt,
219 SourceLocation (Stmt::*v)() const) {
220 return static_cast<const S*>(stmt)->getSourceRange().getEnd();
221 }
222}
223
224SourceLocation Stmt::getLocStart() const {
225 switch (getStmtClass()) {
226 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
227#define ABSTRACT_STMT(type)
228#define STMT(type, base) \
229 case Stmt::type##Class: \
230 return getLocStartImpl<type>(this, &type::getLocStart);
231#include "clang/AST/StmtNodes.inc"
232 }
233 llvm_unreachable("unknown statement kind");
234}
235
236SourceLocation Stmt::getLocEnd() const {
237 switch (getStmtClass()) {
238 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
239#define ABSTRACT_STMT(type)
240#define STMT(type, base) \
241 case Stmt::type##Class: \
242 return getLocEndImpl<type>(this, &type::getLocEnd);
243#include "clang/AST/StmtNodes.inc"
244 }
245 llvm_unreachable("unknown statement kind");
246}
247
Benjamin Kramer3a2d0fb2012-07-04 17:03:41 +0000248CompoundStmt::CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts,
249 SourceLocation LB, SourceLocation RB)
250 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
251 CompoundStmtBits.NumStmts = NumStmts;
252 assert(CompoundStmtBits.NumStmts == NumStmts &&
253 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
254
255 if (NumStmts == 0) {
256 Body = 0;
257 return;
258 }
259
260 Body = new (C) Stmt*[NumStmts];
261 memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
262}
263
Douglas Gregor025452f2009-04-17 00:04:06 +0000264void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
265 if (this->Body)
266 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000267 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000268
269 Body = new (C) Stmt*[NumStmts];
270 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
271}
Reid Spencer5f016e22007-07-11 17:01:13 +0000272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000274 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000275}
276
Alexander Kornienko49908902012-07-09 10:04:07 +0000277AttributedStmt *AttributedStmt::Create(ASTContext &C, SourceLocation Loc,
278 ArrayRef<const Attr*> Attrs,
279 Stmt *SubStmt) {
280 void *Mem = C.Allocate(sizeof(AttributedStmt) +
281 sizeof(Attr*) * (Attrs.size() - 1),
282 llvm::alignOf<AttributedStmt>());
283 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
284}
285
286AttributedStmt *AttributedStmt::CreateEmpty(ASTContext &C, unsigned NumAttrs) {
287 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
288 void *Mem = C.Allocate(sizeof(AttributedStmt) +
289 sizeof(Attr*) * (NumAttrs - 1),
290 llvm::alignOf<AttributedStmt>());
291 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
292}
293
Steve Naroff507f2d52007-08-31 23:49:30 +0000294// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000295SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000296 if (RetExpr)
297 return SourceRange(RetLoc, RetExpr->getLocEnd());
298 else
299 return SourceRange(RetLoc);
300}
301
Ted Kremenekd48ade62007-10-01 16:34:52 +0000302bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000303 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000304 default:
305 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000306
Ted Kremenekd48ade62007-10-01 16:34:52 +0000307 case CallExprClass:
308 case ConditionalOperatorClass:
309 case ChooseExprClass:
310 case StmtExprClass:
311 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000312 return true;
313
Ted Kremenekd48ade62007-10-01 16:34:52 +0000314 case Stmt::BinaryOperatorClass: {
315 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000316 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000317 return true;
318 else
319 return false;
320 }
321 }
322}
323
Chad Rosieraba59aa2012-08-28 17:43:23 +0000324std::string AsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosier92527012012-08-28 18:21:14 +0000325 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
326 return gccAsmStmt->generateAsmString(C);
327 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
328 return msAsmStmt->generateAsmString(C);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000329 llvm_unreachable("unknown asm statement kind!");
330}
331
332StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000333 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
334 return gccAsmStmt->getOutputConstraint(i);
335 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
336 return msAsmStmt->getOutputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000337 llvm_unreachable("unknown asm statement kind!");
338}
339
340const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000341 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
342 return gccAsmStmt->getOutputExpr(i);
343 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
344 return msAsmStmt->getOutputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000345 llvm_unreachable("unknown asm statement kind!");
346}
347
348StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000349 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
350 return gccAsmStmt->getInputConstraint(i);
351 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
352 return msAsmStmt->getInputConstraint(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000353 llvm_unreachable("unknown asm statement kind!");
354}
355
356const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000357 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
358 return gccAsmStmt->getInputExpr(i);
359 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
360 return msAsmStmt->getInputExpr(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000361 llvm_unreachable("unknown asm statement kind!");
362}
363
364StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosier92527012012-08-28 18:21:14 +0000365 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
366 return gccAsmStmt->getClobber(i);
367 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
368 return msAsmStmt->getClobber(i);
Chad Rosieraba59aa2012-08-28 17:43:23 +0000369 llvm_unreachable("unknown asm statement kind!");
370}
371
Chad Rosierc4fb2212012-08-28 00:24:05 +0000372/// getNumPlusOperands - Return the number of output operands that have a "+"
373/// constraint.
374unsigned AsmStmt::getNumPlusOperands() const {
375 unsigned Res = 0;
376 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
377 if (isOutputPlusConstraint(i))
378 ++Res;
379 return Res;
380}
381
Chad Rosier33f05582012-08-27 23:47:56 +0000382StringRef GCCAsmStmt::getClobber(unsigned i) const {
383 return getClobberStringLiteral(i)->getString();
384}
385
Chad Rosierdf5faf52012-08-25 00:11:56 +0000386Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000387 return cast<Expr>(Exprs[i]);
388}
Chris Lattnerb3277932009-03-10 04:59:06 +0000389
390/// getOutputConstraint - Return the constraint string for the specified
391/// output operand. All output constraints are known to be non-empty (either
392/// '=' or '+').
Chad Rosierdf5faf52012-08-25 00:11:56 +0000393StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000394 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000395}
Chris Lattnerb3277932009-03-10 04:59:06 +0000396
Chad Rosierdf5faf52012-08-25 00:11:56 +0000397Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000398 return cast<Expr>(Exprs[i + NumOutputs]);
399}
Chad Rosierdf5faf52012-08-25 00:11:56 +0000400void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner935f0f02011-02-21 22:09:29 +0000401 Exprs[i + NumOutputs] = E;
402}
403
Chris Lattnerb3277932009-03-10 04:59:06 +0000404/// getInputConstraint - Return the specified input constraint. Unlike output
405/// constraints, these can be empty.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000406StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000407 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000408}
409
Chad Rosierdf5faf52012-08-25 00:11:56 +0000410void GCCAsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000411 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000412 StringLiteral **Constraints,
413 Stmt **Exprs,
414 unsigned NumOutputs,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000415 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000416 StringLiteral **Clobbers,
417 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000418 this->NumOutputs = NumOutputs;
419 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000420 this->NumClobbers = NumClobbers;
421
422 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000423
Anders Carlsson966146e2010-01-30 23:19:41 +0000424 C.Deallocate(this->Names);
425 this->Names = new (C) IdentifierInfo*[NumExprs];
426 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000427
Anders Carlsson966146e2010-01-30 23:19:41 +0000428 C.Deallocate(this->Exprs);
429 this->Exprs = new (C) Stmt*[NumExprs];
430 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000431
Anders Carlsson966146e2010-01-30 23:19:41 +0000432 C.Deallocate(this->Constraints);
433 this->Constraints = new (C) StringLiteral*[NumExprs];
434 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier0e2a8682012-08-07 23:12:23 +0000435
Anders Carlsson966146e2010-01-30 23:19:41 +0000436 C.Deallocate(this->Clobbers);
437 this->Clobbers = new (C) StringLiteral*[NumClobbers];
438 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000439}
440
Chris Lattner10ca96a2009-03-10 06:33:24 +0000441/// getNamedOperand - Given a symbolic operand reference like %[foo],
442/// translate this into a numeric value needed to reference the same operand.
443/// This returns -1 if the operand name is invalid.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000444int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000445 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner10ca96a2009-03-10 06:33:24 +0000447 // Check if this is an output operand.
448 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
449 if (getOutputName(i) == SymbolicName)
450 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner10ca96a2009-03-10 06:33:24 +0000453 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
454 if (getInputName(i) == SymbolicName)
455 return getNumOutputs() + NumPlusOperands + i;
456
457 // Not found.
458 return -1;
459}
460
Chris Lattner458cd9c2009-03-10 23:21:44 +0000461/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
462/// it into pieces. If the asm string is erroneous, emit errors and return
463/// true, otherwise return false.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000464unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000465 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000466 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000467 const char *StrStart = Str.begin();
468 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000469 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattner458cd9c2009-03-10 23:21:44 +0000471 // "Simple" inline asms have no constraints or operands, just convert the asm
472 // string to escape $'s.
473 if (isSimple()) {
474 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000475 for (; CurPtr != StrEnd; ++CurPtr) {
476 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000477 case '$':
478 Result += "$$";
479 break;
480 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000481 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000482 break;
483 }
484 }
485 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000486 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000487 }
488
489 // CurStringPiece - The current string that we are building up as we scan the
490 // asm string.
491 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000493 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000494
Chris Lattner458cd9c2009-03-10 23:21:44 +0000495 while (1) {
496 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000497 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000498 if (!CurStringPiece.empty())
499 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000500 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattner3182db12009-03-10 23:51:40 +0000503 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000504 switch (CurChar) {
505 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000506 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
507 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
508 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000509 case '%':
510 break;
511 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000512 CurStringPiece += CurChar;
513 continue;
514 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000515
Chris Lattner458cd9c2009-03-10 23:21:44 +0000516 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000517 if (CurPtr == StrEnd) {
518 // % at end of string is invalid (no escape).
519 DiagOffs = CurPtr-StrStart-1;
520 return diag::err_asm_invalid_escape;
521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Chris Lattner3182db12009-03-10 23:51:40 +0000523 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000524 if (EscapedChar == '%') { // %% -> %
525 // Escaped percentage sign.
526 CurStringPiece += '%';
527 continue;
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattner458cd9c2009-03-10 23:21:44 +0000530 if (EscapedChar == '=') { // %= -> Generate an unique ID.
531 CurStringPiece += "${:uid}";
532 continue;
533 }
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattner458cd9c2009-03-10 23:21:44 +0000535 // Otherwise, we have an operand. If we have accumulated a string so far,
536 // add it to the Pieces list.
537 if (!CurStringPiece.empty()) {
538 Pieces.push_back(AsmStringPiece(CurStringPiece));
539 CurStringPiece.clear();
540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Chris Lattner458cd9c2009-03-10 23:21:44 +0000542 // Handle %x4 and %x[foo] by capturing x as the modifier character.
543 char Modifier = '\0';
544 if (isalpha(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000545 if (CurPtr == StrEnd) { // Premature end.
546 DiagOffs = CurPtr-StrStart-1;
547 return diag::err_asm_invalid_escape;
548 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000549 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000550 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattner458cd9c2009-03-10 23:21:44 +0000553 if (isdigit(EscapedChar)) {
554 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000555 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattnercafc2222009-03-11 22:52:17 +0000557 --CurPtr;
558 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000559 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner85759272009-03-11 00:23:13 +0000561 unsigned NumOperands =
562 getNumOutputs() + getNumPlusOperands() + getNumInputs();
563 if (N >= NumOperands) {
564 DiagOffs = CurPtr-StrStart-1;
565 return diag::err_asm_invalid_operand_number;
566 }
567
Chris Lattner458cd9c2009-03-10 23:21:44 +0000568 Pieces.push_back(AsmStringPiece(N, Modifier));
569 continue;
570 }
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattner458cd9c2009-03-10 23:21:44 +0000572 // Handle %[foo], a symbolic operand reference.
573 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000574 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000576 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000577 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000578 if (NameEnd == 0)
579 return diag::err_asm_unterminated_symbolic_operand_name;
580 if (NameEnd == CurPtr)
581 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Chris Lattner5f9e2722011-07-23 10:55:15 +0000583 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner458cd9c2009-03-10 23:21:44 +0000585 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000586 if (N == -1) {
587 // Verify that an operand with that name exists.
588 DiagOffs = CurPtr-StrStart;
589 return diag::err_asm_unknown_symbolic_operand_name;
590 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000591 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000593 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000594 continue;
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Chris Lattner2ff0f422009-03-10 23:57:07 +0000597 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000598 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000599 }
600}
Chad Rosierda083b22012-08-27 20:23:31 +0000601
602/// Assemble final IR asm string (GCC-style).
603std::string GCCAsmStmt::generateAsmString(ASTContext &C) const {
Chad Rosierbe3ace82012-08-24 17:05:45 +0000604 // Analyze the asm string to decompose it into its pieces. We know that Sema
605 // has already done this, so it is guaranteed to be successful.
Chad Rosierdf5faf52012-08-25 00:11:56 +0000606 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosierbe3ace82012-08-24 17:05:45 +0000607 unsigned DiagOffs;
608 AnalyzeAsmString(Pieces, C, DiagOffs);
609
610 std::string AsmString;
611 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
612 if (Pieces[i].isString())
613 AsmString += Pieces[i].getString();
614 else if (Pieces[i].getModifier() == '\0')
615 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
616 else
617 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
618 Pieces[i].getModifier() + '}';
619 }
620 return AsmString;
621}
Chris Lattner458cd9c2009-03-10 23:21:44 +0000622
Chad Rosierda083b22012-08-27 20:23:31 +0000623/// Assemble final IR asm string (MS-style).
624std::string MSAsmStmt::generateAsmString(ASTContext &C) const {
625 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier00d16372012-08-28 20:33:49 +0000626 return AsmStr;
Chad Rosierda083b22012-08-27 20:23:31 +0000627}
628
Chad Rosier633abb02012-08-24 00:07:09 +0000629Expr *MSAsmStmt::getOutputExpr(unsigned i) {
630 return cast<Expr>(Exprs[i]);
631}
632
633Expr *MSAsmStmt::getInputExpr(unsigned i) {
634 return cast<Expr>(Exprs[i + NumOutputs]);
635}
636void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
637 Exprs[i + NumOutputs] = E;
638}
639
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000640QualType CXXCatchStmt::getCaughtType() const {
641 if (ExceptionDecl)
642 return ExceptionDecl->getType();
643 return QualType();
644}
645
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000646//===----------------------------------------------------------------------===//
647// Constructors
648//===----------------------------------------------------------------------===//
649
Chad Rosierdf5faf52012-08-25 00:11:56 +0000650GCCAsmStmt::GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
651 bool isvolatile, unsigned numoutputs, unsigned numinputs,
652 IdentifierInfo **names, StringLiteral **constraints,
653 Expr **exprs, StringLiteral *asmstr,
654 unsigned numclobbers, StringLiteral **clobbers,
655 SourceLocation rparenloc)
Chad Rosier066ef862012-08-27 19:38:01 +0000656 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
657 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weber608b17f2008-08-05 23:15:29 +0000658
Chad Rosier0e2a8682012-08-07 23:12:23 +0000659 unsigned NumExprs = NumOutputs + NumInputs;
660
Anders Carlsson966146e2010-01-30 23:19:41 +0000661 Names = new (C) IdentifierInfo*[NumExprs];
662 std::copy(names, names + NumExprs, Names);
663
664 Exprs = new (C) Stmt*[NumExprs];
665 std::copy(exprs, exprs + NumExprs, Exprs);
666
667 Constraints = new (C) StringLiteral*[NumExprs];
668 std::copy(constraints, constraints + NumExprs, Constraints);
669
670 Clobbers = new (C) StringLiteral*[NumClobbers];
671 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000672}
673
Chad Rosier058ab172012-08-16 00:06:53 +0000674MSAsmStmt::MSAsmStmt(ASTContext &C, SourceLocation asmloc,
675 SourceLocation lbraceloc, bool issimple, bool isvolatile,
676 ArrayRef<Token> asmtoks, ArrayRef<IdentifierInfo*> inputs,
Chad Rosier633abb02012-08-24 00:07:09 +0000677 ArrayRef<IdentifierInfo*> outputs,
678 ArrayRef<Expr*> inputexprs, ArrayRef<Expr*> outputexprs,
Chad Rosier89fb6d72012-08-28 20:28:20 +0000679 StringRef asmstr, ArrayRef<StringRef> constraints,
680 ArrayRef<StringRef> clobbers, SourceLocation endloc)
Chad Rosier12603e22012-09-04 16:36:26 +0000681 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, outputs.size(),
682 inputs.size(), clobbers.size()), LBraceLoc(lbraceloc),
683 EndLoc(endloc), AsmStr(asmstr.str()), NumAsmToks(asmtoks.size()) {
Chad Rosier633abb02012-08-24 00:07:09 +0000684 assert (inputs.size() == inputexprs.size() && "Input expr size mismatch!");
685 assert (outputs.size() == outputexprs.size() && "Input expr size mismatch!");
Chad Rosier058ab172012-08-16 00:06:53 +0000686
687 unsigned NumExprs = NumOutputs + NumInputs;
688
689 Names = new (C) IdentifierInfo*[NumExprs];
690 for (unsigned i = 0, e = NumOutputs; i != e; ++i)
691 Names[i] = outputs[i];
Chad Rosier12603e22012-09-04 16:36:26 +0000692 for (unsigned i = NumOutputs, j = 0, e = NumExprs; i != e; ++i, ++j)
693 Names[i] = inputs[j];
Chad Rosier79efe242012-08-07 00:29:06 +0000694
Chad Rosier633abb02012-08-24 00:07:09 +0000695 Exprs = new (C) Stmt*[NumExprs];
696 for (unsigned i = 0, e = NumOutputs; i != e; ++i)
697 Exprs[i] = outputexprs[i];
Chad Rosier12603e22012-09-04 16:36:26 +0000698 for (unsigned i = NumOutputs, j = 0, e = NumExprs; i != e; ++i, ++j)
699 Exprs[i] = inputexprs[j];
Chad Rosier633abb02012-08-24 00:07:09 +0000700
Chad Rosier79efe242012-08-07 00:29:06 +0000701 AsmToks = new (C) Token[NumAsmToks];
702 for (unsigned i = 0, e = NumAsmToks; i != e; ++i)
703 AsmToks[i] = asmtoks[i];
Chad Rosier62f22b82012-08-08 19:48:07 +0000704
Chad Rosier89fb6d72012-08-28 20:28:20 +0000705 Constraints = new (C) StringRef[NumExprs];
706 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
707 size_t size = constraints[i].size();
708 char *dest = new (C) char[size];
709 std::strncpy(dest, constraints[i].data(), size);
710 Constraints[i] = StringRef(dest, size);
711 }
712
Chad Rosier33c72e12012-08-10 21:36:25 +0000713 Clobbers = new (C) StringRef[NumClobbers];
Chad Rosiere790bc32012-08-10 21:06:19 +0000714 for (unsigned i = 0, e = NumClobbers; i != e; ++i) {
715 // FIXME: Avoid the allocation/copy if at all possible.
716 size_t size = clobbers[i].size();
717 char *dest = new (C) char[size];
718 std::strncpy(dest, clobbers[i].data(), size);
Chad Rosier33c72e12012-08-10 21:36:25 +0000719 Clobbers[i] = StringRef(dest, size);
Chad Rosiere790bc32012-08-10 21:06:19 +0000720 }
Chad Rosier8cd64b42012-06-11 20:47:18 +0000721}
722
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000723ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
724 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000725 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000726: Stmt(ObjCForCollectionStmtClass) {
727 SubExprs[ELEM] = Elem;
728 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
729 SubExprs[BODY] = Body;
730 ForLoc = FCL;
731 RParenLoc = RPL;
732}
733
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000734ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
735 Stmt **CatchStmts, unsigned NumCatchStmts,
736 Stmt *atFinallyStmt)
737 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
738 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
739{
740 Stmt **Stmts = getStmts();
741 Stmts[0] = atTryStmt;
742 for (unsigned I = 0; I != NumCatchStmts; ++I)
743 Stmts[I + 1] = CatchStmts[I];
Chad Rosier0e2a8682012-08-07 23:12:23 +0000744
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000745 if (HasFinally)
746 Stmts[NumCatchStmts + 1] = atFinallyStmt;
747}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000748
Chad Rosier0e2a8682012-08-07 23:12:23 +0000749ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
750 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000751 Stmt *atTryStmt,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000752 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000753 unsigned NumCatchStmts,
754 Stmt *atFinallyStmt) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000755 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000756 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000757 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000758 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
759 atFinallyStmt);
760}
Ted Kremenekff981022008-02-01 21:28:59 +0000761
Chad Rosier0e2a8682012-08-07 23:12:23 +0000762ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000763 unsigned NumCatchStmts,
764 bool HasFinally) {
Chad Rosier0e2a8682012-08-07 23:12:23 +0000765 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000766 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000767 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Chad Rosier0e2a8682012-08-07 23:12:23 +0000768 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000769}
Nico Weber608b17f2008-08-05 23:15:29 +0000770
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000771SourceRange ObjCAtTryStmt::getSourceRange() const {
772 SourceLocation EndLoc;
773 if (HasFinally)
774 EndLoc = getFinallyStmt()->getLocEnd();
775 else if (NumCatchStmts)
776 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
777 else
778 EndLoc = getTryBody()->getLocEnd();
Chad Rosier0e2a8682012-08-07 23:12:23 +0000779
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000780 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000781}
782
Sam Weiniga1a396d2010-02-03 03:56:39 +0000783CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Chad Rosier0e2a8682012-08-07 23:12:23 +0000784 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000785 unsigned numHandlers) {
786 std::size_t Size = sizeof(CXXTryStmt);
787 Size += ((numHandlers + 1) * sizeof(Stmt));
788
Chris Lattner32488542010-10-30 05:14:06 +0000789 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000790 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
791}
792
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000793CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
794 unsigned numHandlers) {
795 std::size_t Size = sizeof(CXXTryStmt);
796 Size += ((numHandlers + 1) * sizeof(Stmt));
797
Chris Lattner32488542010-10-30 05:14:06 +0000798 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000799 return new (Mem) CXXTryStmt(Empty, numHandlers);
800}
801
Sam Weiniga1a396d2010-02-03 03:56:39 +0000802CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000803 Stmt **handlers, unsigned numHandlers)
804 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000805 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000806 Stmts[0] = tryBlock;
807 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
808}
809
Richard Smithad762fc2011-04-14 22:09:26 +0000810CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
811 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
812 Stmt *Body, SourceLocation FL,
813 SourceLocation CL, SourceLocation RPL)
814 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
815 SubExprs[RANGE] = Range;
816 SubExprs[BEGINEND] = BeginEndStmt;
817 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
818 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
819 SubExprs[LOOPVAR] = LoopVar;
820 SubExprs[BODY] = Body;
821}
822
823Expr *CXXForRangeStmt::getRangeInit() {
824 DeclStmt *RangeStmt = getRangeStmt();
825 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
826 assert(RangeDecl &&& "for-range should have a single var decl");
827 return RangeDecl->getInit();
828}
829
830const Expr *CXXForRangeStmt::getRangeInit() const {
831 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
832}
833
834VarDecl *CXXForRangeStmt::getLoopVariable() {
835 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
836 assert(LV && "No loop variable in CXXForRangeStmt");
837 return cast<VarDecl>(LV);
838}
839
840const VarDecl *CXXForRangeStmt::getLoopVariable() const {
841 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
842}
843
Chad Rosier0e2a8682012-08-07 23:12:23 +0000844IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000845 Stmt *then, SourceLocation EL, Stmt *elsev)
846 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000847{
848 setConditionVariable(C, var);
849 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
850 SubExprs[THEN] = then;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000851 SubExprs[ELSE] = elsev;
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000852}
853
854VarDecl *IfStmt::getConditionVariable() const {
855 if (!SubExprs[VAR])
856 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000857
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000858 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
859 return cast<VarDecl>(DS->getSingleDecl());
860}
861
862void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
863 if (!V) {
864 SubExprs[VAR] = 0;
865 return;
866 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000867
Daniel Dunbar96a00142012-03-09 18:35:03 +0000868 SourceRange VarRange = V->getSourceRange();
869 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
870 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000871}
872
Chad Rosier0e2a8682012-08-07 23:12:23 +0000873ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
874 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000875 SourceLocation RP)
Chad Rosier0e2a8682012-08-07 23:12:23 +0000876 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000877{
878 SubExprs[INIT] = Init;
879 setConditionVariable(C, condVar);
880 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
881 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
882 SubExprs[BODY] = Body;
883}
884
885VarDecl *ForStmt::getConditionVariable() const {
886 if (!SubExprs[CONDVAR])
887 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000888
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000889 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
890 return cast<VarDecl>(DS->getSingleDecl());
891}
892
893void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
894 if (!V) {
895 SubExprs[CONDVAR] = 0;
896 return;
897 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000898
Daniel Dunbar96a00142012-03-09 18:35:03 +0000899 SourceRange VarRange = V->getSourceRange();
900 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
901 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000902}
903
Chad Rosier0e2a8682012-08-07 23:12:23 +0000904SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
905 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000906{
907 setConditionVariable(C, Var);
908 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
909 SubExprs[BODY] = NULL;
910}
911
912VarDecl *SwitchStmt::getConditionVariable() const {
913 if (!SubExprs[VAR])
914 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000915
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000916 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
917 return cast<VarDecl>(DS->getSingleDecl());
918}
919
920void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
921 if (!V) {
922 SubExprs[VAR] = 0;
923 return;
924 }
Chad Rosier0e2a8682012-08-07 23:12:23 +0000925
Daniel Dunbar96a00142012-03-09 18:35:03 +0000926 SourceRange VarRange = V->getSourceRange();
927 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
928 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000929}
930
John McCall63c00d72011-02-09 08:16:59 +0000931Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000932 if (isa<CaseStmt>(this))
933 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000934 return cast<DefaultStmt>(this)->getSubStmt();
935}
936
Chad Rosier0e2a8682012-08-07 23:12:23 +0000937WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000938 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000939 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000940 setConditionVariable(C, Var);
941 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
942 SubExprs[BODY] = body;
943 WhileLoc = WL;
944}
945
946VarDecl *WhileStmt::getConditionVariable() const {
947 if (!SubExprs[VAR])
948 return 0;
Chad Rosier0e2a8682012-08-07 23:12:23 +0000949
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000950 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
951 return cast<VarDecl>(DS->getSingleDecl());
952}
953
954void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
955 if (!V) {
956 SubExprs[VAR] = 0;
957 return;
958 }
Daniel Dunbar96a00142012-03-09 18:35:03 +0000959
960 SourceRange VarRange = V->getSourceRange();
961 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
962 VarRange.getEnd());
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000963}
964
Ted Kremenek82977772007-08-24 21:09:09 +0000965// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000966LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000967 if (AddrLabelExpr *E =
968 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
969 return E->getLabel();
970 return 0;
971}
Ted Kremenek82977772007-08-24 21:09:09 +0000972
Ted Kremenek82977772007-08-24 21:09:09 +0000973// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000974const Expr* ReturnStmt::getRetValue() const {
975 return cast_or_null<Expr>(RetExpr);
976}
977Expr* ReturnStmt::getRetValue() {
978 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000979}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000980
981SEHTryStmt::SEHTryStmt(bool IsCXXTry,
982 SourceLocation TryLoc,
983 Stmt *TryBlock,
984 Stmt *Handler)
985 : Stmt(SEHTryStmtClass),
986 IsCXXTry(IsCXXTry),
987 TryLoc(TryLoc)
988{
989 Children[TRY] = TryBlock;
990 Children[HANDLER] = Handler;
991}
992
993SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
994 bool IsCXXTry,
995 SourceLocation TryLoc,
996 Stmt *TryBlock,
997 Stmt *Handler) {
998 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
999}
1000
1001SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1002 return dyn_cast<SEHExceptStmt>(getHandler());
1003}
1004
1005SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1006 return dyn_cast<SEHFinallyStmt>(getHandler());
1007}
1008
1009SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
1010 Expr *FilterExpr,
1011 Stmt *Block)
1012 : Stmt(SEHExceptStmtClass),
1013 Loc(Loc)
1014{
1015 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
1016 Children[BLOCK] = Block;
1017}
1018
1019SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
1020 SourceLocation Loc,
1021 Expr *FilterExpr,
1022 Stmt *Block) {
1023 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1024}
1025
1026SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
1027 Stmt *Block)
1028 : Stmt(SEHFinallyStmtClass),
1029 Loc(Loc),
1030 Block(Block)
1031{}
1032
1033SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
1034 SourceLocation Loc,
1035 Stmt *Block) {
1036 return new(C)SEHFinallyStmt(Loc,Block);
1037}