blob: 7dfa3a9df13ec98da5b5c3fa1013deffc954926b [file] [log] [blame]
Chris Lattnerf42cce72006-10-25 04:09:21 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf42cce72006-10-25 04:09:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer444a1302012-12-01 17:12:56 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTDiagnostic.h"
Chris Lattner29375652006-12-04 18:06:35 +000016#include "clang/AST/ExprCXX.h"
Steve Naroff021ca182008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000018#include "clang/AST/ExprOpenMP.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000019#include "clang/AST/Stmt.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000020#include "clang/AST/StmtCXX.h"
21#include "clang/AST/StmtObjC.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtOpenMP.h"
Sebastian Redl54c04d42008-12-22 19:15:10 +000023#include "clang/AST/Type.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000024#include "clang/Basic/CharInfo.h"
Chris Lattner1a8f3942010-04-23 16:29:58 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000026#include "clang/Lex/Token.h"
Chad Rosier14836ba2012-08-24 17:05:45 +000027#include "llvm/ADT/StringExtras.h"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnerf42cce72006-10-25 04:09:21 +000029using namespace clang;
30
Steve Narofff84d11f2007-05-23 21:48:04 +000031static struct StmtClassNameTable {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000032 const char *Name;
33 unsigned Counter;
34 unsigned Size;
Alexis Hunt656bb312010-05-05 15:24:00 +000035} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner4d15a0d2007-08-25 01:42:24 +000036
37static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
38 static bool Initialized = false;
39 if (Initialized)
40 return StmtClassInfo[E];
41
42 // Intialize the table on the first use.
43 Initialized = true;
Alexis Huntabb2ac82010-05-18 06:22:21 +000044#define ABSTRACT_STMT(STMT)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000045#define STMT(CLASS, PARENT) \
46 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
47 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Alexis Hunt656bb312010-05-05 15:24:00 +000048#include "clang/AST/StmtNodes.inc"
Nico Weberde565e32008-08-05 23:15:29 +000049
Chris Lattner4d15a0d2007-08-25 01:42:24 +000050 return StmtClassInfo[E];
51}
52
Craig Topper37932912013-08-18 10:09:15 +000053void *Stmt::operator new(size_t bytes, const ASTContext& C,
Craig Topper5a050012013-08-18 17:45:38 +000054 unsigned alignment) {
Benjamin Kramerea70eb32012-12-01 15:09:41 +000055 return ::operator new(bytes, C, alignment);
56}
57
Steve Narofff1e53692007-03-23 22:27:02 +000058const char *Stmt::getStmtClassName() const {
John McCall925b16622010-10-26 08:39:16 +000059 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Steve Narofff1e53692007-03-23 22:27:02 +000060}
Steve Narofff84d11f2007-05-23 21:48:04 +000061
62void Stmt::PrintStats() {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000063 // Ensure the table is primed.
64 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weberde565e32008-08-05 23:15:29 +000065
Steve Narofff84d11f2007-05-23 21:48:04 +000066 unsigned sum = 0;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000067 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Alexis Hunt656bb312010-05-05 15:24:00 +000068 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000069 if (StmtClassInfo[i].Name == nullptr) continue;
Chris Lattner4d15a0d2007-08-25 01:42:24 +000070 sum += StmtClassInfo[i].Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000071 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000072 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000073 sum = 0;
Alexis Hunt656bb312010-05-05 15:24:00 +000074 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Craig Topper36250ad2014-05-12 05:36:57 +000075 if (StmtClassInfo[i].Name == nullptr) continue;
Douglas Gregora30d0462009-05-26 14:40:08 +000076 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthbfb154a2011-07-04 06:13:27 +000077 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
78 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
79 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
80 << " bytes)\n";
Chris Lattner4d15a0d2007-08-25 01:42:24 +000081 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Steve Narofff84d11f2007-05-23 21:48:04 +000082 }
Chandler Carruthbfb154a2011-07-04 06:13:27 +000083
84 llvm::errs() << "Total bytes = " << sum << "\n";
Steve Narofff84d11f2007-05-23 21:48:04 +000085}
86
87void Stmt::addStmtClass(StmtClass s) {
Chris Lattner4d15a0d2007-08-25 01:42:24 +000088 ++getStmtInfoTableEntry(s).Counter;
Steve Narofff84d11f2007-05-23 21:48:04 +000089}
90
Daniel Dunbar62905572012-03-05 21:42:49 +000091bool Stmt::StatisticsEnabled = false;
92void Stmt::EnableStatistics() {
93 StatisticsEnabled = true;
Steve Narofff84d11f2007-05-23 21:48:04 +000094}
95
John McCall4db5c3c2011-07-07 06:58:02 +000096Stmt *Stmt::IgnoreImplicit() {
97 Stmt *s = this;
98
Richard Smith520449d2015-02-05 06:15:50 +000099 if (auto *ewc = dyn_cast<ExprWithCleanups>(s))
John McCall4db5c3c2011-07-07 06:58:02 +0000100 s = ewc->getSubExpr();
101
Richard Smith520449d2015-02-05 06:15:50 +0000102 if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s))
103 s = mte->GetTemporaryExpr();
104
105 if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s))
106 s = bte->getSubExpr();
107
108 while (auto *ice = dyn_cast<ImplicitCastExpr>(s))
John McCall4db5c3c2011-07-07 06:58:02 +0000109 s = ice->getSubExpr();
110
111 return s;
112}
113
Alexander Musmana5f070a2014-10-01 06:03:56 +0000114/// \brief Skip no-op (attributed, compound) container stmts and skip captured
115/// stmt at the top, if \a IgnoreCaptured is true.
116Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
117 Stmt *S = this;
118 if (IgnoreCaptured)
119 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
120 S = CapS->getCapturedStmt();
121 while (true) {
122 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
123 S = AS->getSubStmt();
124 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
125 if (CS->size() != 1)
126 break;
127 S = CS->body_back();
128 } else
129 break;
130 }
131 return S;
132}
133
Chandler Carrutha626d642011-09-10 00:02:34 +0000134/// \brief Strip off all label-like statements.
135///
Richard Smithc202b282012-04-14 00:33:13 +0000136/// This will strip off label statements, case statements, attributed
137/// statements and default statements recursively.
Chandler Carrutha626d642011-09-10 00:02:34 +0000138const Stmt *Stmt::stripLabelLikeStatements() const {
139 const Stmt *S = this;
140 while (true) {
141 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
142 S = LS->getSubStmt();
143 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
144 S = SC->getSubStmt();
Richard Smithc202b282012-04-14 00:33:13 +0000145 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
146 S = AS->getSubStmt();
Chandler Carrutha626d642011-09-10 00:02:34 +0000147 else
148 return S;
149 }
150}
151
John McCallbd066782011-02-09 08:16:59 +0000152namespace {
153 struct good {};
154 struct bad {};
John McCallf75152f2011-02-09 08:31:17 +0000155
156 // These silly little functions have to be static inline to suppress
157 // unused warnings, and they have to be defined to suppress other
158 // warnings.
Nick Lewyckybae992f2011-02-09 08:42:57 +0000159 static inline good is_good(good) { return good(); }
John McCallbd066782011-02-09 08:16:59 +0000160
161 typedef Stmt::child_range children_t();
Nick Lewyckybae992f2011-02-09 08:42:57 +0000162 template <class T> good implements_children(children_t T::*) {
163 return good();
164 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000165 LLVM_ATTRIBUTE_UNUSED
Nick Lewyckybae992f2011-02-09 08:42:57 +0000166 static inline bad implements_children(children_t Stmt::*) {
167 return bad();
168 }
John McCallbd066782011-02-09 08:16:59 +0000169
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000170 typedef SourceLocation getLocStart_t() const;
171 template <class T> good implements_getLocStart(getLocStart_t T::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000172 return good();
173 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000174 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000175 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
176 return bad();
177 }
178
179 typedef SourceLocation getLocEnd_t() const;
180 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
181 return good();
182 }
Eli Friedmandc41d792013-09-10 22:57:15 +0000183 LLVM_ATTRIBUTE_UNUSED
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000184 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
Nick Lewyckybae992f2011-02-09 08:42:57 +0000185 return bad();
186 }
John McCallbd066782011-02-09 08:16:59 +0000187
188#define ASSERT_IMPLEMENTS_children(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000189 (void) is_good(implements_children(&type::children))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000190#define ASSERT_IMPLEMENTS_getLocStart(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000191 (void) is_good(implements_getLocStart(&type::getLocStart))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000192#define ASSERT_IMPLEMENTS_getLocEnd(type) \
Eli Friedmandc41d792013-09-10 22:57:15 +0000193 (void) is_good(implements_getLocEnd(&type::getLocEnd))
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000194}
John McCallbd066782011-02-09 08:16:59 +0000195
196/// Check whether the various Stmt classes implement their member
197/// functions.
Eli Friedmandc41d792013-09-10 22:57:15 +0000198LLVM_ATTRIBUTE_UNUSED
John McCallbd066782011-02-09 08:16:59 +0000199static inline void check_implementations() {
200#define ABSTRACT_STMT(type)
201#define STMT(type, base) \
202 ASSERT_IMPLEMENTS_children(type); \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000203 ASSERT_IMPLEMENTS_getLocStart(type); \
204 ASSERT_IMPLEMENTS_getLocEnd(type);
John McCallbd066782011-02-09 08:16:59 +0000205#include "clang/AST/StmtNodes.inc"
206}
207
208Stmt::child_range Stmt::children() {
209 switch (getStmtClass()) {
210 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
211#define ABSTRACT_STMT(type)
212#define STMT(type, base) \
213 case Stmt::type##Class: \
214 return static_cast<type*>(this)->children();
215#include "clang/AST/StmtNodes.inc"
216 }
217 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000218}
219
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000220// Amusing macro metaprogramming hack: check whether a class provides
221// a more specific implementation of getSourceRange.
222//
223// See also Expr.cpp:getExprLoc().
224namespace {
225 /// This implementation is used when a class provides a custom
226 /// implementation of getSourceRange.
227 template <class S, class T>
228 SourceRange getSourceRangeImpl(const Stmt *stmt,
229 SourceRange (T::*v)() const) {
230 return static_cast<const S*>(stmt)->getSourceRange();
231 }
232
233 /// This implementation is used when a class doesn't provide a custom
234 /// implementation of getSourceRange. Overload resolution should pick it over
235 /// the implementation above because it's more specialized according to
236 /// function template partial ordering.
237 template <class S>
238 SourceRange getSourceRangeImpl(const Stmt *stmt,
239 SourceRange (Stmt::*v)() const) {
240 return SourceRange(static_cast<const S*>(stmt)->getLocStart(),
241 static_cast<const S*>(stmt)->getLocEnd());
242 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000243}
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000244
John McCallbd066782011-02-09 08:16:59 +0000245SourceRange Stmt::getSourceRange() const {
246 switch (getStmtClass()) {
247 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
248#define ABSTRACT_STMT(type)
249#define STMT(type, base) \
250 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000251 return getSourceRangeImpl<type>(this, &type::getSourceRange);
John McCallbd066782011-02-09 08:16:59 +0000252#include "clang/AST/StmtNodes.inc"
253 }
254 llvm_unreachable("unknown statement kind!");
John McCallbd066782011-02-09 08:16:59 +0000255}
256
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000257SourceLocation Stmt::getLocStart() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000258// llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000259 switch (getStmtClass()) {
260 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
261#define ABSTRACT_STMT(type)
262#define STMT(type, base) \
263 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000264 return static_cast<const type*>(this)->getLocStart();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000265#include "clang/AST/StmtNodes.inc"
266 }
267 llvm_unreachable("unknown statement kind");
268}
269
270SourceLocation Stmt::getLocEnd() const {
271 switch (getStmtClass()) {
272 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
273#define ABSTRACT_STMT(type)
274#define STMT(type, base) \
275 case Stmt::type##Class: \
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000276 return static_cast<const type*>(this)->getLocEnd();
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000277#include "clang/AST/StmtNodes.inc"
278 }
279 llvm_unreachable("unknown statement kind");
280}
281
Craig Toppere6960e22013-08-22 05:28:54 +0000282CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000283 SourceLocation LB, SourceLocation RB)
Aaron Ballmance6c67e2014-10-22 21:06:18 +0000284 : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {
Nico Webera2a0eb92012-12-29 20:03:39 +0000285 CompoundStmtBits.NumStmts = Stmts.size();
286 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000287 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
288
Nico Webera2a0eb92012-12-29 20:03:39 +0000289 if (Stmts.size() == 0) {
Craig Topper36250ad2014-05-12 05:36:57 +0000290 Body = nullptr;
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000291 return;
292 }
293
Nico Webera2a0eb92012-12-29 20:03:39 +0000294 Body = new (C) Stmt*[Stmts.size()];
295 std::copy(Stmts.begin(), Stmts.end(), Body);
Benjamin Kramere2a929d2012-07-04 17:03:41 +0000296}
297
Craig Topper9ee84ad2015-12-04 05:01:44 +0000298void CompoundStmt::setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts) {
299 if (Body)
Douglas Gregora9af1d12009-04-17 00:04:06 +0000300 C.Deallocate(Body);
Craig Topper9ee84ad2015-12-04 05:01:44 +0000301 CompoundStmtBits.NumStmts = Stmts.size();
302 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
303 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
Douglas Gregora9af1d12009-04-17 00:04:06 +0000304
Craig Topper9ee84ad2015-12-04 05:01:44 +0000305 Body = new (C) Stmt*[Stmts.size()];
306 std::copy(Stmts.begin(), Stmts.end(), Body);
Douglas Gregora9af1d12009-04-17 00:04:06 +0000307}
Steve Narofff84d11f2007-05-23 21:48:04 +0000308
Chris Lattnereefa10e2007-05-28 06:56:27 +0000309const char *LabelStmt::getName() const {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000310 return getDecl()->getIdentifier()->getNameStart();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000311}
312
Craig Toppere6960e22013-08-22 05:28:54 +0000313AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000314 ArrayRef<const Attr*> Attrs,
315 Stmt *SubStmt) {
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000316 assert(!Attrs.empty() && "Attrs should not be empty");
317 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * Attrs.size(),
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000318 llvm::alignOf<AttributedStmt>());
319 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
320}
321
Craig Toppere6960e22013-08-22 05:28:54 +0000322AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
323 unsigned NumAttrs) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000324 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
Aaron Ballmanf3d9b092014-05-13 14:55:01 +0000325 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * NumAttrs,
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000326 llvm::alignOf<AttributedStmt>());
327 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
328}
329
Craig Topperc571c812013-08-22 06:02:26 +0000330std::string AsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000331 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
332 return gccAsmStmt->generateAsmString(C);
333 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
334 return msAsmStmt->generateAsmString(C);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000335 llvm_unreachable("unknown asm statement kind!");
336}
337
338StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000339 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
340 return gccAsmStmt->getOutputConstraint(i);
341 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
342 return msAsmStmt->getOutputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000343 llvm_unreachable("unknown asm statement kind!");
344}
345
346const Expr *AsmStmt::getOutputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000347 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
348 return gccAsmStmt->getOutputExpr(i);
349 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
350 return msAsmStmt->getOutputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000351 llvm_unreachable("unknown asm statement kind!");
352}
353
354StringRef AsmStmt::getInputConstraint(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000355 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
356 return gccAsmStmt->getInputConstraint(i);
357 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
358 return msAsmStmt->getInputConstraint(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000359 llvm_unreachable("unknown asm statement kind!");
360}
361
362const Expr *AsmStmt::getInputExpr(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000363 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
364 return gccAsmStmt->getInputExpr(i);
365 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
366 return msAsmStmt->getInputExpr(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000367 llvm_unreachable("unknown asm statement kind!");
368}
369
370StringRef AsmStmt::getClobber(unsigned i) const {
Chad Rosierf70b7e22012-08-28 18:21:14 +0000371 if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(this))
372 return gccAsmStmt->getClobber(i);
373 if (const MSAsmStmt *msAsmStmt = dyn_cast<MSAsmStmt>(this))
374 return msAsmStmt->getClobber(i);
Chad Rosierbbbe9ab2012-08-28 17:43:23 +0000375 llvm_unreachable("unknown asm statement kind!");
376}
377
Chad Rosiera1b5c8c2012-08-28 00:24:05 +0000378/// getNumPlusOperands - Return the number of output operands that have a "+"
379/// constraint.
380unsigned AsmStmt::getNumPlusOperands() const {
381 unsigned Res = 0;
382 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
383 if (isOutputPlusConstraint(i))
384 ++Res;
385 return Res;
386}
387
Akira Hatanaka987f1862014-08-22 06:05:21 +0000388char GCCAsmStmt::AsmStringPiece::getModifier() const {
389 assert(isOperand() && "Only Operands can have modifiers.");
390 return isLetter(Str[0]) ? Str[0] : '\0';
391}
392
Chad Rosier6100ae12012-08-27 23:47:56 +0000393StringRef GCCAsmStmt::getClobber(unsigned i) const {
394 return getClobberStringLiteral(i)->getString();
395}
396
Chad Rosierde70e0e2012-08-25 00:11:56 +0000397Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000398 return cast<Expr>(Exprs[i]);
399}
Chris Lattner72bbf172009-03-10 04:59:06 +0000400
401/// getOutputConstraint - Return the constraint string for the specified
402/// output operand. All output constraints are known to be non-empty (either
403/// '=' or '+').
Chad Rosierde70e0e2012-08-25 00:11:56 +0000404StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000405 return getOutputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000406}
Chris Lattner72bbf172009-03-10 04:59:06 +0000407
Chad Rosierde70e0e2012-08-25 00:11:56 +0000408Expr *GCCAsmStmt::getInputExpr(unsigned i) {
Ted Kremenek5778acf2008-10-27 18:40:21 +0000409 return cast<Expr>(Exprs[i + NumOutputs]);
410}
Chad Rosierde70e0e2012-08-25 00:11:56 +0000411void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
Chris Lattner93ede022011-02-21 22:09:29 +0000412 Exprs[i + NumOutputs] = E;
413}
414
Chris Lattner72bbf172009-03-10 04:59:06 +0000415/// getInputConstraint - Return the specified input constraint. Unlike output
416/// constraints, these can be empty.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000417StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
Anders Carlsson66de0812010-01-30 20:38:10 +0000418 return getInputConstraintLiteral(i)->getString();
Ted Kremenek5778acf2008-10-27 18:40:21 +0000419}
420
Craig Topperc571c812013-08-22 06:02:26 +0000421void GCCAsmStmt::setOutputsAndInputsAndClobbers(const ASTContext &C,
422 IdentifierInfo **Names,
423 StringLiteral **Constraints,
424 Stmt **Exprs,
425 unsigned NumOutputs,
426 unsigned NumInputs,
427 StringLiteral **Clobbers,
428 unsigned NumClobbers) {
Douglas Gregorf994f062009-04-17 20:57:14 +0000429 this->NumOutputs = NumOutputs;
430 this->NumInputs = NumInputs;
Anders Carlsson98323d22010-01-30 23:19:41 +0000431 this->NumClobbers = NumClobbers;
432
433 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosier42032fa2012-08-07 23:12:23 +0000434
Anders Carlsson98323d22010-01-30 23:19:41 +0000435 C.Deallocate(this->Names);
436 this->Names = new (C) IdentifierInfo*[NumExprs];
437 std::copy(Names, Names + NumExprs, this->Names);
Chad Rosier42032fa2012-08-07 23:12:23 +0000438
Anders Carlsson98323d22010-01-30 23:19:41 +0000439 C.Deallocate(this->Exprs);
440 this->Exprs = new (C) Stmt*[NumExprs];
441 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Chad Rosier42032fa2012-08-07 23:12:23 +0000442
Anders Carlsson98323d22010-01-30 23:19:41 +0000443 C.Deallocate(this->Constraints);
444 this->Constraints = new (C) StringLiteral*[NumExprs];
445 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Chad Rosier42032fa2012-08-07 23:12:23 +0000446
Anders Carlsson98323d22010-01-30 23:19:41 +0000447 C.Deallocate(this->Clobbers);
448 this->Clobbers = new (C) StringLiteral*[NumClobbers];
449 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorf994f062009-04-17 20:57:14 +0000450}
451
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000452/// getNamedOperand - Given a symbolic operand reference like %[foo],
453/// translate this into a numeric value needed to reference the same operand.
454/// This returns -1 if the operand name is invalid.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000455int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000456 unsigned NumPlusOperands = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000458 // Check if this is an output operand.
459 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
460 if (getOutputName(i) == SymbolicName)
461 return i;
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattnerd7d5fdf2009-03-10 06:33:24 +0000464 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
465 if (getInputName(i) == SymbolicName)
466 return getNumOutputs() + NumPlusOperands + i;
467
468 // Not found.
469 return -1;
470}
471
Chris Lattner35b58362009-03-10 23:21:44 +0000472/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
473/// it into pieces. If the asm string is erroneous, emit errors and return
474/// true, otherwise return false.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000475unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Craig Topperc571c812013-08-22 06:02:26 +0000476 const ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000477 StringRef Str = getAsmString()->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000478 const char *StrStart = Str.begin();
479 const char *StrEnd = Str.end();
Chris Lattnera41b8472009-03-10 23:51:40 +0000480 const char *CurPtr = StrStart;
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattner35b58362009-03-10 23:21:44 +0000482 // "Simple" inline asms have no constraints or operands, just convert the asm
483 // string to escape $'s.
484 if (isSimple()) {
485 std::string Result;
Chris Lattnera41b8472009-03-10 23:51:40 +0000486 for (; CurPtr != StrEnd; ++CurPtr) {
487 switch (*CurPtr) {
Chris Lattner35b58362009-03-10 23:21:44 +0000488 case '$':
489 Result += "$$";
490 break;
491 default:
Chris Lattnera41b8472009-03-10 23:51:40 +0000492 Result += *CurPtr;
Chris Lattner35b58362009-03-10 23:21:44 +0000493 break;
494 }
495 }
496 Pieces.push_back(AsmStringPiece(Result));
Chris Lattnera41b8472009-03-10 23:51:40 +0000497 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000498 }
499
500 // CurStringPiece - The current string that we are building up as we scan the
501 // asm string.
502 std::string CurStringPiece;
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregore8bbc122011-09-02 00:18:52 +0000504 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Chad Rosier42032fa2012-08-07 23:12:23 +0000505
Chris Lattner35b58362009-03-10 23:21:44 +0000506 while (1) {
507 // Done with the string?
Chris Lattnera41b8472009-03-10 23:51:40 +0000508 if (CurPtr == StrEnd) {
Chris Lattner35b58362009-03-10 23:21:44 +0000509 if (!CurStringPiece.empty())
510 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattnera41b8472009-03-10 23:51:40 +0000511 return 0;
Chris Lattner35b58362009-03-10 23:21:44 +0000512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattnera41b8472009-03-10 23:51:40 +0000514 char CurChar = *CurPtr++;
Chris Lattnerda081a82010-04-05 18:44:00 +0000515 switch (CurChar) {
516 case '$': CurStringPiece += "$$"; continue;
Chris Lattner1a8f3942010-04-23 16:29:58 +0000517 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
518 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
519 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattnerda081a82010-04-05 18:44:00 +0000520 case '%':
521 break;
522 default:
Chris Lattner35b58362009-03-10 23:21:44 +0000523 CurStringPiece += CurChar;
524 continue;
525 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000526
Chris Lattner35b58362009-03-10 23:21:44 +0000527 // Escaped "%" character in asm string.
Chris Lattner3fa25c62009-03-11 00:06:36 +0000528 if (CurPtr == StrEnd) {
529 // % at end of string is invalid (no escape).
530 DiagOffs = CurPtr-StrStart-1;
531 return diag::err_asm_invalid_escape;
532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Chris Lattnera41b8472009-03-10 23:51:40 +0000534 char EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000535 if (EscapedChar == '%') { // %% -> %
536 // Escaped percentage sign.
537 CurStringPiece += '%';
538 continue;
539 }
Mike Stump11289f42009-09-09 15:08:12 +0000540
Chris Lattner35b58362009-03-10 23:21:44 +0000541 if (EscapedChar == '=') { // %= -> Generate an unique ID.
542 CurStringPiece += "${:uid}";
543 continue;
544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Chris Lattner35b58362009-03-10 23:21:44 +0000546 // Otherwise, we have an operand. If we have accumulated a string so far,
547 // add it to the Pieces list.
548 if (!CurStringPiece.empty()) {
549 Pieces.push_back(AsmStringPiece(CurStringPiece));
550 CurStringPiece.clear();
551 }
Mike Stump11289f42009-09-09 15:08:12 +0000552
Akira Hatanaka987f1862014-08-22 06:05:21 +0000553 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
554 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
555
556 const char *Begin = CurPtr - 1; // Points to the character following '%'.
557 const char *Percent = Begin - 1; // Points to '%'.
558
Jordan Rosea7d03842013-02-08 22:30:41 +0000559 if (isLetter(EscapedChar)) {
Benjamin Kramere87c38b2011-07-05 11:13:37 +0000560 if (CurPtr == StrEnd) { // Premature end.
561 DiagOffs = CurPtr-StrStart-1;
562 return diag::err_asm_invalid_escape;
563 }
Chris Lattnera41b8472009-03-10 23:51:40 +0000564 EscapedChar = *CurPtr++;
Chris Lattner35b58362009-03-10 23:21:44 +0000565 }
Mike Stump11289f42009-09-09 15:08:12 +0000566
Akira Hatanaka987f1862014-08-22 06:05:21 +0000567 const TargetInfo &TI = C.getTargetInfo();
568 const SourceManager &SM = C.getSourceManager();
569 const LangOptions &LO = C.getLangOpts();
570
571 // Handle operands that don't have asmSymbolicName (e.g., %x4).
Jordan Rosea7d03842013-02-08 22:30:41 +0000572 if (isDigit(EscapedChar)) {
Chris Lattner35b58362009-03-10 23:21:44 +0000573 // %n - Assembler operand n
Chris Lattner99d892b2009-03-11 22:52:17 +0000574 unsigned N = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000575
Chris Lattner99d892b2009-03-11 22:52:17 +0000576 --CurPtr;
Jordan Rosea7d03842013-02-08 22:30:41 +0000577 while (CurPtr != StrEnd && isDigit(*CurPtr))
Chris Lattner84f3afa2009-03-11 23:09:16 +0000578 N = N*10 + ((*CurPtr++)-'0');
Mike Stump11289f42009-09-09 15:08:12 +0000579
Chris Lattner14311922009-03-11 00:23:13 +0000580 unsigned NumOperands =
581 getNumOutputs() + getNumPlusOperands() + getNumInputs();
582 if (N >= NumOperands) {
583 DiagOffs = CurPtr-StrStart-1;
584 return diag::err_asm_invalid_operand_number;
585 }
586
Akira Hatanaka987f1862014-08-22 06:05:21 +0000587 // Str contains "x4" (Operand without the leading %).
588 std::string Str(Begin, CurPtr - Begin);
589
590 // (BeginLoc, EndLoc) represents the range of the operand we are currently
591 // processing. Unlike Str, the range includes the leading '%'.
592 SourceLocation BeginLoc =
593 getAsmString()->getLocationOfByte(Percent - StrStart, SM, LO, TI);
594 SourceLocation EndLoc =
595 getAsmString()->getLocationOfByte(CurPtr - StrStart, SM, LO, TI);
596
Benjamin Kramer3204b152015-05-29 19:42:19 +0000597 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Chris Lattner35b58362009-03-10 23:21:44 +0000598 continue;
599 }
Mike Stump11289f42009-09-09 15:08:12 +0000600
Akira Hatanaka987f1862014-08-22 06:05:21 +0000601 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
Chris Lattner35b58362009-03-10 23:21:44 +0000602 if (EscapedChar == '[') {
Chris Lattner3fa25c62009-03-11 00:06:36 +0000603 DiagOffs = CurPtr-StrStart-1;
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattner3fa25c62009-03-11 00:06:36 +0000605 // Find the ']'.
Chris Lattnera41b8472009-03-10 23:51:40 +0000606 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Craig Topper36250ad2014-05-12 05:36:57 +0000607 if (NameEnd == nullptr)
Chris Lattner3fa25c62009-03-11 00:06:36 +0000608 return diag::err_asm_unterminated_symbolic_operand_name;
609 if (NameEnd == CurPtr)
610 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump11289f42009-09-09 15:08:12 +0000611
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000612 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000613
Chris Lattner35b58362009-03-10 23:21:44 +0000614 int N = getNamedOperand(SymbolicName);
Chris Lattner3fa25c62009-03-11 00:06:36 +0000615 if (N == -1) {
616 // Verify that an operand with that name exists.
617 DiagOffs = CurPtr-StrStart;
618 return diag::err_asm_unknown_symbolic_operand_name;
619 }
Akira Hatanaka987f1862014-08-22 06:05:21 +0000620
621 // Str contains "x[foo]" (Operand without the leading %).
622 std::string Str(Begin, NameEnd + 1 - Begin);
623
624 // (BeginLoc, EndLoc) represents the range of the operand we are currently
625 // processing. Unlike Str, the range includes the leading '%'.
626 SourceLocation BeginLoc =
627 getAsmString()->getLocationOfByte(Percent - StrStart, SM, LO, TI);
628 SourceLocation EndLoc =
629 getAsmString()->getLocationOfByte(NameEnd + 1 - StrStart, SM, LO, TI);
630
Benjamin Kramer3204b152015-05-29 19:42:19 +0000631 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000632
Chris Lattner3fa25c62009-03-11 00:06:36 +0000633 CurPtr = NameEnd+1;
Chris Lattner35b58362009-03-10 23:21:44 +0000634 continue;
635 }
Mike Stump11289f42009-09-09 15:08:12 +0000636
Chris Lattner0cdaa2e2009-03-10 23:57:07 +0000637 DiagOffs = CurPtr-StrStart-1;
Chris Lattnera41b8472009-03-10 23:51:40 +0000638 return diag::err_asm_invalid_escape;
Chris Lattner35b58362009-03-10 23:21:44 +0000639 }
640}
Chad Rosier3b0c2602012-08-27 20:23:31 +0000641
642/// Assemble final IR asm string (GCC-style).
Craig Topperc571c812013-08-22 06:02:26 +0000643std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier14836ba2012-08-24 17:05:45 +0000644 // Analyze the asm string to decompose it into its pieces. We know that Sema
645 // has already done this, so it is guaranteed to be successful.
Chad Rosierde70e0e2012-08-25 00:11:56 +0000646 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
Chad Rosier14836ba2012-08-24 17:05:45 +0000647 unsigned DiagOffs;
648 AnalyzeAsmString(Pieces, C, DiagOffs);
649
650 std::string AsmString;
651 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
652 if (Pieces[i].isString())
653 AsmString += Pieces[i].getString();
654 else if (Pieces[i].getModifier() == '\0')
655 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
656 else
657 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
658 Pieces[i].getModifier() + '}';
659 }
660 return AsmString;
661}
Chris Lattner35b58362009-03-10 23:21:44 +0000662
Chad Rosier3b0c2602012-08-27 20:23:31 +0000663/// Assemble final IR asm string (MS-style).
Craig Topperc571c812013-08-22 06:02:26 +0000664std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
Chad Rosier3b0c2602012-08-27 20:23:31 +0000665 // FIXME: This needs to be translated into the IR string representation.
Chad Rosier0bca4692012-08-28 20:33:49 +0000666 return AsmStr;
Chad Rosier3b0c2602012-08-27 20:23:31 +0000667}
668
Chad Rosierfe31e622012-08-24 00:07:09 +0000669Expr *MSAsmStmt::getOutputExpr(unsigned i) {
670 return cast<Expr>(Exprs[i]);
671}
672
673Expr *MSAsmStmt::getInputExpr(unsigned i) {
674 return cast<Expr>(Exprs[i + NumOutputs]);
675}
676void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
677 Exprs[i + NumOutputs] = E;
678}
679
Chris Lattner86f5e132008-01-30 05:01:46 +0000680//===----------------------------------------------------------------------===//
681// Constructors
682//===----------------------------------------------------------------------===//
683
Craig Toppere6960e22013-08-22 05:28:54 +0000684GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
685 bool issimple, bool isvolatile, unsigned numoutputs,
686 unsigned numinputs, IdentifierInfo **names,
687 StringLiteral **constraints, Expr **exprs,
688 StringLiteral *asmstr, unsigned numclobbers,
689 StringLiteral **clobbers, SourceLocation rparenloc)
Chad Rosierfe3352e2012-08-27 19:38:01 +0000690 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
691 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
Nico Weberde565e32008-08-05 23:15:29 +0000692
Chad Rosier42032fa2012-08-07 23:12:23 +0000693 unsigned NumExprs = NumOutputs + NumInputs;
694
Anders Carlsson98323d22010-01-30 23:19:41 +0000695 Names = new (C) IdentifierInfo*[NumExprs];
696 std::copy(names, names + NumExprs, Names);
697
698 Exprs = new (C) Stmt*[NumExprs];
699 std::copy(exprs, exprs + NumExprs, Exprs);
700
701 Constraints = new (C) StringLiteral*[NumExprs];
702 std::copy(constraints, constraints + NumExprs, Constraints);
703
704 Clobbers = new (C) StringLiteral*[NumClobbers];
705 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000706}
707
Craig Toppere6960e22013-08-22 05:28:54 +0000708MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000709 SourceLocation lbraceloc, bool issimple, bool isvolatile,
Chad Rosierf8037a12012-10-16 21:55:39 +0000710 ArrayRef<Token> asmtoks, unsigned numoutputs,
John McCallf413f5e2013-05-03 00:10:13 +0000711 unsigned numinputs,
Chad Rosierf8037a12012-10-16 21:55:39 +0000712 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
713 StringRef asmstr, ArrayRef<StringRef> clobbers,
714 SourceLocation endloc)
715 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
716 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
John McCallf413f5e2013-05-03 00:10:13 +0000717 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000718
John McCallf413f5e2013-05-03 00:10:13 +0000719 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
720}
Chad Rosier7dbef3e2012-08-16 00:06:53 +0000721
Craig Toppere6960e22013-08-22 05:28:54 +0000722static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
Benjamin Kramer2ab0d882015-08-04 12:34:23 +0000723 return str.copy(C);
John McCallf413f5e2013-05-03 00:10:13 +0000724}
725
Craig Toppere6960e22013-08-22 05:28:54 +0000726void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
John McCallf413f5e2013-05-03 00:10:13 +0000727 ArrayRef<Token> asmtoks,
728 ArrayRef<StringRef> constraints,
729 ArrayRef<Expr*> exprs,
730 ArrayRef<StringRef> clobbers) {
731 assert(NumAsmToks == asmtoks.size());
732 assert(NumClobbers == clobbers.size());
733
Craig Toppercaf138e2015-12-05 07:41:42 +0000734 assert(exprs.size() == NumOutputs + NumInputs);
735 assert(exprs.size() == constraints.size());
John McCallf413f5e2013-05-03 00:10:13 +0000736
737 AsmStr = copyIntoContext(C, asmstr);
Chad Rosier99fc3812012-08-07 00:29:06 +0000738
Craig Toppercaf138e2015-12-05 07:41:42 +0000739 Exprs = new (C) Stmt*[exprs.size()];
740 std::copy(exprs.begin(), exprs.end(), Exprs);
Chad Rosierfe31e622012-08-24 00:07:09 +0000741
Craig Toppercaf138e2015-12-05 07:41:42 +0000742 AsmToks = new (C) Token[asmtoks.size()];
743 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
Chad Rosier3ed0bd92012-08-08 19:48:07 +0000744
Craig Toppercaf138e2015-12-05 07:41:42 +0000745 Constraints = new (C) StringRef[exprs.size()];
746 std::transform(constraints.begin(), constraints.end(), Constraints,
747 [&](StringRef Constraint) {
748 return copyIntoContext(C, Constraint);
749 });
Chad Rosier3dd7bf22012-08-28 20:28:20 +0000750
Chad Rosierbaf53f92012-08-10 21:36:25 +0000751 Clobbers = new (C) StringRef[NumClobbers];
Craig Toppercaf138e2015-12-05 07:41:42 +0000752 // FIXME: Avoid the allocation/copy if at all possible.
753 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
754 [&](StringRef Clobber) {
755 return copyIntoContext(C, Clobber);
756 });
Chad Rosier32503022012-06-11 20:47:18 +0000757}
758
Craig Toppere6960e22013-08-22 05:28:54 +0000759IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000760 Stmt *then, SourceLocation EL, Stmt *elsev)
761 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000762{
763 setConditionVariable(C, var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000764 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000765 SubExprs[THEN] = then;
Chad Rosier42032fa2012-08-07 23:12:23 +0000766 SubExprs[ELSE] = elsev;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000767}
768
769VarDecl *IfStmt::getConditionVariable() const {
770 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000771 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000772
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000773 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
774 return cast<VarDecl>(DS->getSingleDecl());
775}
776
Craig Toppere6960e22013-08-22 05:28:54 +0000777void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000778 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000779 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000780 return;
781 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000782
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000783 SourceRange VarRange = V->getSourceRange();
784 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
785 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000786}
787
Craig Toppere6960e22013-08-22 05:28:54 +0000788ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
Chad Rosier42032fa2012-08-07 23:12:23 +0000789 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000790 SourceLocation RP)
Chad Rosier42032fa2012-08-07 23:12:23 +0000791 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000792{
793 SubExprs[INIT] = Init;
794 setConditionVariable(C, condVar);
Pavel Labath515f4db2013-09-03 14:41:16 +0000795 SubExprs[COND] = Cond;
796 SubExprs[INC] = Inc;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000797 SubExprs[BODY] = Body;
798}
799
800VarDecl *ForStmt::getConditionVariable() const {
801 if (!SubExprs[CONDVAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000802 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000803
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000804 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
805 return cast<VarDecl>(DS->getSingleDecl());
806}
807
Craig Toppere6960e22013-08-22 05:28:54 +0000808void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000809 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000810 SubExprs[CONDVAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000811 return;
812 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000813
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000814 SourceRange VarRange = V->getSourceRange();
815 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
816 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000817}
818
Craig Toppere6960e22013-08-22 05:28:54 +0000819SwitchStmt::SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond)
Benjamin Kramer475386d2015-04-02 15:29:07 +0000820 : Stmt(SwitchStmtClass), FirstCase(nullptr, false) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000821 setConditionVariable(C, Var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000822 SubExprs[COND] = cond;
Craig Topper36250ad2014-05-12 05:36:57 +0000823 SubExprs[BODY] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000824}
825
826VarDecl *SwitchStmt::getConditionVariable() const {
827 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000828 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000829
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000830 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
831 return cast<VarDecl>(DS->getSingleDecl());
832}
833
Craig Toppere6960e22013-08-22 05:28:54 +0000834void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000835 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000836 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000837 return;
838 }
Chad Rosier42032fa2012-08-07 23:12:23 +0000839
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000840 SourceRange VarRange = V->getSourceRange();
841 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
842 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000843}
844
John McCallbd066782011-02-09 08:16:59 +0000845Stmt *SwitchCase::getSubStmt() {
Chris Lattner80d51eb2011-02-28 00:18:06 +0000846 if (isa<CaseStmt>(this))
847 return cast<CaseStmt>(this)->getSubStmt();
John McCallbd066782011-02-09 08:16:59 +0000848 return cast<DefaultStmt>(this)->getSubStmt();
849}
850
Craig Toppere6960e22013-08-22 05:28:54 +0000851WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000852 SourceLocation WL)
Chris Lattner80d51eb2011-02-28 00:18:06 +0000853 : Stmt(WhileStmtClass) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000854 setConditionVariable(C, Var);
Pavel Labath515f4db2013-09-03 14:41:16 +0000855 SubExprs[COND] = cond;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000856 SubExprs[BODY] = body;
857 WhileLoc = WL;
858}
859
860VarDecl *WhileStmt::getConditionVariable() const {
861 if (!SubExprs[VAR])
Craig Topper36250ad2014-05-12 05:36:57 +0000862 return nullptr;
Chad Rosier42032fa2012-08-07 23:12:23 +0000863
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000864 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
865 return cast<VarDecl>(DS->getSingleDecl());
866}
867
Craig Toppere6960e22013-08-22 05:28:54 +0000868void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000869 if (!V) {
Craig Topper36250ad2014-05-12 05:36:57 +0000870 SubExprs[VAR] = nullptr;
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000871 return;
872 }
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000873
874 SourceRange VarRange = V->getSourceRange();
875 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
876 VarRange.getEnd());
Douglas Gregor27b98ea2010-06-21 23:44:13 +0000877}
878
Ted Kremenek066dd932007-08-24 21:09:09 +0000879// IndirectGotoStmt
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000880LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall9de91602010-10-28 08:53:48 +0000881 if (AddrLabelExpr *E =
882 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
883 return E->getLabel();
Craig Topper36250ad2014-05-12 05:36:57 +0000884 return nullptr;
John McCall9de91602010-10-28 08:53:48 +0000885}
Ted Kremenek066dd932007-08-24 21:09:09 +0000886
Ted Kremenek066dd932007-08-24 21:09:09 +0000887// ReturnStmt
Ted Kremenekc6501db2008-06-17 03:11:08 +0000888const Expr* ReturnStmt::getRetValue() const {
889 return cast_or_null<Expr>(RetExpr);
890}
891Expr* ReturnStmt::getRetValue() {
892 return cast_or_null<Expr>(RetExpr);
Ted Kremenek066dd932007-08-24 21:09:09 +0000893}
John Wiegley1c0675e2011-04-28 01:08:34 +0000894
Warren Huntf6be4cb2014-07-25 20:52:51 +0000895SEHTryStmt::SEHTryStmt(bool IsCXXTry,
896 SourceLocation TryLoc,
897 Stmt *TryBlock,
898 Stmt *Handler)
899 : Stmt(SEHTryStmtClass),
900 IsCXXTry(IsCXXTry),
901 TryLoc(TryLoc)
902{
903 Children[TRY] = TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000904 Children[HANDLER] = Handler;
905}
906
Warren Huntf6be4cb2014-07-25 20:52:51 +0000907SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
Craig Toppere6960e22013-08-22 05:28:54 +0000908 SourceLocation TryLoc, Stmt *TryBlock,
Warren Huntf6be4cb2014-07-25 20:52:51 +0000909 Stmt *Handler) {
910 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
John Wiegley1c0675e2011-04-28 01:08:34 +0000911}
912
913SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
914 return dyn_cast<SEHExceptStmt>(getHandler());
915}
916
917SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
918 return dyn_cast<SEHFinallyStmt>(getHandler());
919}
920
921SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
922 Expr *FilterExpr,
923 Stmt *Block)
924 : Stmt(SEHExceptStmtClass),
925 Loc(Loc)
926{
Pavel Labath515f4db2013-09-03 14:41:16 +0000927 Children[FILTER_EXPR] = FilterExpr;
John Wiegley1c0675e2011-04-28 01:08:34 +0000928 Children[BLOCK] = Block;
929}
930
Craig Toppere6960e22013-08-22 05:28:54 +0000931SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
932 Expr *FilterExpr, Stmt *Block) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000933 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
934}
935
936SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
937 Stmt *Block)
938 : Stmt(SEHFinallyStmtClass),
939 Loc(Loc),
940 Block(Block)
941{}
942
Craig Toppere6960e22013-08-22 05:28:54 +0000943SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
John Wiegley1c0675e2011-04-28 01:08:34 +0000944 Stmt *Block) {
945 return new(C)SEHFinallyStmt(Loc,Block);
946}
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000947
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000948CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
949 VarDecl *Var)
950 : VarAndKind(Var, Kind), Loc(Loc) {
951 switch (Kind) {
952 case VCK_This:
953 assert(!Var && "'this' capture cannot have a variable!");
954 break;
955 case VCK_ByRef:
956 assert(Var && "capturing by reference must have a variable!");
957 break;
958 case VCK_ByCopy:
959 assert(Var && "capturing by copy must have a variable!");
960 assert(
961 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
962 Var->getType()
963 ->castAs<ReferenceType>()
964 ->getPointeeType()
965 ->isScalarType())) &&
966 "captures by copy are expected to have a scalar type!");
967 break;
968 case VCK_VLAType:
969 assert(!Var &&
970 "Variable-length array type capture cannot have a variable!");
971 break;
972 }
973}
974
Chandler Carruth21c90602015-12-30 03:24:14 +0000975CapturedStmt::VariableCaptureKind
976CapturedStmt::Capture::getCaptureKind() const {
977 return VarAndKind.getInt();
978}
979
980VarDecl *CapturedStmt::Capture::getCapturedVar() const {
981 assert((capturesVariable() || capturesVariableByCopy()) &&
982 "No variable available for 'this' or VAT capture");
983 return VarAndKind.getPointer();
984}
985
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000986CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
987 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
988
989 // Offset of the first Capture object.
Rui Ueyama83aa9792016-01-14 21:00:27 +0000990 unsigned FirstCaptureOffset = llvm::alignTo(Size, llvm::alignOf<Capture>());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000991
992 return reinterpret_cast<Capture *>(
993 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
994 + FirstCaptureOffset);
995}
996
Wei Pan17fbf6e2013-05-04 03:59:06 +0000997CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
998 ArrayRef<Capture> Captures,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000999 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001000 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001001 RecordDecl *RD)
1002 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
Wei Pan17fbf6e2013-05-04 03:59:06 +00001003 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001004 assert( S && "null captured statement");
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001005 assert(CD && "null captured declaration for captured statement");
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001006 assert(RD && "null record declaration for captured statement");
1007
1008 // Copy initialization expressions.
1009 Stmt **Stored = getStoredStmts();
1010 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1011 *Stored++ = CaptureInits[I];
1012
1013 // Copy the statement being captured.
1014 *Stored = S;
1015
1016 // Copy all Capture objects.
1017 Capture *Buffer = getStoredCaptures();
1018 std::copy(Captures.begin(), Captures.end(), Buffer);
1019}
1020
1021CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1022 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
Craig Topper36250ad2014-05-12 05:36:57 +00001023 CapDeclAndKind(nullptr, CR_Default), TheRecordDecl(nullptr) {
1024 getStoredStmts()[NumCaptures] = nullptr;
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001025}
1026
Craig Toppere6960e22013-08-22 05:28:54 +00001027CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
Wei Pan17fbf6e2013-05-04 03:59:06 +00001028 CapturedRegionKind Kind,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001029 ArrayRef<Capture> Captures,
1030 ArrayRef<Expr *> CaptureInits,
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001031 CapturedDecl *CD,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001032 RecordDecl *RD) {
1033 // The layout is
1034 //
1035 // -----------------------------------------------------------
1036 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1037 // ----------------^-------------------^----------------------
1038 // getStoredStmts() getStoredCaptures()
1039 //
1040 // where S is the statement being captured.
1041 //
1042 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1043
1044 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1045 if (!Captures.empty()) {
1046 // Realign for the following Capture array.
Rui Ueyama83aa9792016-01-14 21:00:27 +00001047 Size = llvm::alignTo(Size, llvm::alignOf<Capture>());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001048 Size += sizeof(Capture) * Captures.size();
1049 }
1050
1051 void *Mem = Context.Allocate(Size);
Wei Pan17fbf6e2013-05-04 03:59:06 +00001052 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001053}
1054
Craig Toppere6960e22013-08-22 05:28:54 +00001055CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001056 unsigned NumCaptures) {
1057 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1058 if (NumCaptures > 0) {
1059 // Realign for the following Capture array.
Rui Ueyama83aa9792016-01-14 21:00:27 +00001060 Size = llvm::alignTo(Size, llvm::alignOf<Capture>());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001061 Size += sizeof(Capture) * NumCaptures;
1062 }
1063
1064 void *Mem = Context.Allocate(Size);
1065 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1066}
1067
1068Stmt::child_range CapturedStmt::children() {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001069 // Children are captured field initilizers.
1070 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001071}
1072
Chandler Carruth21c90602015-12-30 03:24:14 +00001073CapturedDecl *CapturedStmt::getCapturedDecl() {
1074 return CapDeclAndKind.getPointer();
1075}
1076const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1077 return CapDeclAndKind.getPointer();
1078}
1079
1080/// \brief Set the outlined function declaration.
1081void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1082 assert(D && "null CapturedDecl");
1083 CapDeclAndKind.setPointer(D);
1084}
1085
1086/// \brief Retrieve the captured region kind.
1087CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1088 return CapDeclAndKind.getInt();
1089}
1090
1091/// \brief Set the captured region kind.
1092void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1093 CapDeclAndKind.setInt(Kind);
1094}
1095
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001096bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001097 for (const auto &I : captures()) {
1098 if (!I.capturesVariable())
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001099 continue;
1100
1101 // This does not handle variable redeclarations. This should be
1102 // extended to capture variables with redeclarations, for example
1103 // a thread-private variable in OpenMP.
Aaron Ballmanc656303a2014-03-14 18:08:33 +00001104 if (I.getCapturedVar() == Var)
Tareq A. Siraj24110cc2013-04-16 18:53:08 +00001105 return true;
1106 }
1107
1108 return false;
1109}