1 //===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Stmt class and statement subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTDiagnostic.h"
16 #include "clang/AST/ExprCXX.h"
17 #include "clang/AST/ExprObjC.h"
18 #include "clang/AST/ExprOpenMP.h"
19 #include "clang/AST/Stmt.h"
20 #include "clang/AST/StmtCXX.h"
21 #include "clang/AST/StmtObjC.h"
22 #include "clang/AST/StmtOpenMP.h"
23 #include "clang/AST/Type.h"
24 #include "clang/Basic/CharInfo.h"
25 #include "clang/Basic/TargetInfo.h"
26 #include "clang/Lex/Token.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace clang;
30
31 static struct StmtClassNameTable {
32 const char *Name;
33 unsigned Counter;
34 unsigned Size;
35 } StmtClassInfo[Stmt::lastStmtConstant+1];
36
getStmtInfoTableEntry(Stmt::StmtClass E)37 static 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;
44 #define ABSTRACT_STMT(STMT)
45 #define STMT(CLASS, PARENT) \
46 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
47 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
48 #include "clang/AST/StmtNodes.inc"
49
50 return StmtClassInfo[E];
51 }
52
operator new(size_t bytes,const ASTContext & C,unsigned alignment)53 void *Stmt::operator new(size_t bytes, const ASTContext& C,
54 unsigned alignment) {
55 return ::operator new(bytes, C, alignment);
56 }
57
getStmtClassName() const58 const char *Stmt::getStmtClassName() const {
59 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
60 }
61
PrintStats()62 void Stmt::PrintStats() {
63 // Ensure the table is primed.
64 getStmtInfoTableEntry(Stmt::NullStmtClass);
65
66 unsigned sum = 0;
67 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
68 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
69 if (StmtClassInfo[i].Name == nullptr) continue;
70 sum += StmtClassInfo[i].Counter;
71 }
72 llvm::errs() << " " << sum << " stmts/exprs total.\n";
73 sum = 0;
74 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
75 if (StmtClassInfo[i].Name == nullptr) continue;
76 if (StmtClassInfo[i].Counter == 0) continue;
77 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
78 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
79 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
80 << " bytes)\n";
81 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
82 }
83
84 llvm::errs() << "Total bytes = " << sum << "\n";
85 }
86
addStmtClass(StmtClass s)87 void Stmt::addStmtClass(StmtClass s) {
88 ++getStmtInfoTableEntry(s).Counter;
89 }
90
91 bool Stmt::StatisticsEnabled = false;
EnableStatistics()92 void Stmt::EnableStatistics() {
93 StatisticsEnabled = true;
94 }
95
IgnoreImplicit()96 Stmt *Stmt::IgnoreImplicit() {
97 Stmt *s = this;
98
99 if (auto *ewc = dyn_cast<ExprWithCleanups>(s))
100 s = ewc->getSubExpr();
101
102 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))
109 s = ice->getSubExpr();
110
111 return s;
112 }
113
114 /// \brief Skip no-op (attributed, compound) container stmts and skip captured
115 /// stmt at the top, if \a IgnoreCaptured is true.
IgnoreContainers(bool IgnoreCaptured)116 Stmt *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
134 /// \brief Strip off all label-like statements.
135 ///
136 /// This will strip off label statements, case statements, attributed
137 /// statements and default statements recursively.
stripLabelLikeStatements() const138 const 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();
145 else if (const AttributedStmt *AS = dyn_cast<AttributedStmt>(S))
146 S = AS->getSubStmt();
147 else
148 return S;
149 }
150 }
151
152 namespace {
153 struct good {};
154 struct bad {};
155
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.
is_good(good)159 static inline good is_good(good) { return good(); }
160
161 typedef Stmt::child_range children_t();
implements_children(children_t T::*)162 template <class T> good implements_children(children_t T::*) {
163 return good();
164 }
165 LLVM_ATTRIBUTE_UNUSED
implements_children(children_t Stmt::*)166 static inline bad implements_children(children_t Stmt::*) {
167 return bad();
168 }
169
170 typedef SourceLocation getLocStart_t() const;
implements_getLocStart(getLocStart_t T::*)171 template <class T> good implements_getLocStart(getLocStart_t T::*) {
172 return good();
173 }
174 LLVM_ATTRIBUTE_UNUSED
implements_getLocStart(getLocStart_t Stmt::*)175 static inline bad implements_getLocStart(getLocStart_t Stmt::*) {
176 return bad();
177 }
178
179 typedef SourceLocation getLocEnd_t() const;
implements_getLocEnd(getLocEnd_t T::*)180 template <class T> good implements_getLocEnd(getLocEnd_t T::*) {
181 return good();
182 }
183 LLVM_ATTRIBUTE_UNUSED
implements_getLocEnd(getLocEnd_t Stmt::*)184 static inline bad implements_getLocEnd(getLocEnd_t Stmt::*) {
185 return bad();
186 }
187
188 #define ASSERT_IMPLEMENTS_children(type) \
189 (void) is_good(implements_children(&type::children))
190 #define ASSERT_IMPLEMENTS_getLocStart(type) \
191 (void) is_good(implements_getLocStart(&type::getLocStart))
192 #define ASSERT_IMPLEMENTS_getLocEnd(type) \
193 (void) is_good(implements_getLocEnd(&type::getLocEnd))
194 }
195
196 /// Check whether the various Stmt classes implement their member
197 /// functions.
198 LLVM_ATTRIBUTE_UNUSED
check_implementations()199 static inline void check_implementations() {
200 #define ABSTRACT_STMT(type)
201 #define STMT(type, base) \
202 ASSERT_IMPLEMENTS_children(type); \
203 ASSERT_IMPLEMENTS_getLocStart(type); \
204 ASSERT_IMPLEMENTS_getLocEnd(type);
205 #include "clang/AST/StmtNodes.inc"
206 }
207
children()208 Stmt::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!");
218 }
219
220 // Amusing macro metaprogramming hack: check whether a class provides
221 // a more specific implementation of getSourceRange.
222 //
223 // See also Expr.cpp:getExprLoc().
224 namespace {
225 /// This implementation is used when a class provides a custom
226 /// implementation of getSourceRange.
227 template <class S, class T>
getSourceRangeImpl(const Stmt * stmt,SourceRange (T::* v)()const)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>
getSourceRangeImpl(const Stmt * stmt,SourceRange (Stmt::* v)()const)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 }
243 }
244
getSourceRange() const245 SourceRange 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: \
251 return getSourceRangeImpl<type>(this, &type::getSourceRange);
252 #include "clang/AST/StmtNodes.inc"
253 }
254 llvm_unreachable("unknown statement kind!");
255 }
256
getLocStart() const257 SourceLocation Stmt::getLocStart() const {
258 // llvm::errs() << "getLocStart() for " << getStmtClassName() << "\n";
259 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: \
264 return static_cast<const type*>(this)->getLocStart();
265 #include "clang/AST/StmtNodes.inc"
266 }
267 llvm_unreachable("unknown statement kind");
268 }
269
getLocEnd() const270 SourceLocation 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: \
276 return static_cast<const type*>(this)->getLocEnd();
277 #include "clang/AST/StmtNodes.inc"
278 }
279 llvm_unreachable("unknown statement kind");
280 }
281
CompoundStmt(const ASTContext & C,ArrayRef<Stmt * > Stmts,SourceLocation LB,SourceLocation RB)282 CompoundStmt::CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
283 SourceLocation LB, SourceLocation RB)
284 : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {
285 CompoundStmtBits.NumStmts = Stmts.size();
286 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
287 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
288
289 if (Stmts.size() == 0) {
290 Body = nullptr;
291 return;
292 }
293
294 Body = new (C) Stmt*[Stmts.size()];
295 std::copy(Stmts.begin(), Stmts.end(), Body);
296 }
297
setStmts(const ASTContext & C,ArrayRef<Stmt * > Stmts)298 void CompoundStmt::setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts) {
299 if (Body)
300 C.Deallocate(Body);
301 CompoundStmtBits.NumStmts = Stmts.size();
302 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
303 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
304
305 Body = new (C) Stmt*[Stmts.size()];
306 std::copy(Stmts.begin(), Stmts.end(), Body);
307 }
308
getName() const309 const char *LabelStmt::getName() const {
310 return getDecl()->getIdentifier()->getNameStart();
311 }
312
Create(const ASTContext & C,SourceLocation Loc,ArrayRef<const Attr * > Attrs,Stmt * SubStmt)313 AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
314 ArrayRef<const Attr*> Attrs,
315 Stmt *SubStmt) {
316 assert(!Attrs.empty() && "Attrs should not be empty");
317 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * Attrs.size(),
318 llvm::alignOf<AttributedStmt>());
319 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
320 }
321
CreateEmpty(const ASTContext & C,unsigned NumAttrs)322 AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
323 unsigned NumAttrs) {
324 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
325 void *Mem = C.Allocate(sizeof(AttributedStmt) + sizeof(Attr *) * NumAttrs,
326 llvm::alignOf<AttributedStmt>());
327 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
328 }
329
generateAsmString(const ASTContext & C) const330 std::string AsmStmt::generateAsmString(const ASTContext &C) const {
331 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);
335 llvm_unreachable("unknown asm statement kind!");
336 }
337
getOutputConstraint(unsigned i) const338 StringRef AsmStmt::getOutputConstraint(unsigned i) const {
339 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);
343 llvm_unreachable("unknown asm statement kind!");
344 }
345
getOutputExpr(unsigned i) const346 const Expr *AsmStmt::getOutputExpr(unsigned i) const {
347 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);
351 llvm_unreachable("unknown asm statement kind!");
352 }
353
getInputConstraint(unsigned i) const354 StringRef AsmStmt::getInputConstraint(unsigned i) const {
355 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);
359 llvm_unreachable("unknown asm statement kind!");
360 }
361
getInputExpr(unsigned i) const362 const Expr *AsmStmt::getInputExpr(unsigned i) const {
363 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);
367 llvm_unreachable("unknown asm statement kind!");
368 }
369
getClobber(unsigned i) const370 StringRef AsmStmt::getClobber(unsigned i) const {
371 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);
375 llvm_unreachable("unknown asm statement kind!");
376 }
377
378 /// getNumPlusOperands - Return the number of output operands that have a "+"
379 /// constraint.
getNumPlusOperands() const380 unsigned 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
getModifier() const388 char GCCAsmStmt::AsmStringPiece::getModifier() const {
389 assert(isOperand() && "Only Operands can have modifiers.");
390 return isLetter(Str[0]) ? Str[0] : '\0';
391 }
392
getClobber(unsigned i) const393 StringRef GCCAsmStmt::getClobber(unsigned i) const {
394 return getClobberStringLiteral(i)->getString();
395 }
396
getOutputExpr(unsigned i)397 Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
398 return cast<Expr>(Exprs[i]);
399 }
400
401 /// getOutputConstraint - Return the constraint string for the specified
402 /// output operand. All output constraints are known to be non-empty (either
403 /// '=' or '+').
getOutputConstraint(unsigned i) const404 StringRef GCCAsmStmt::getOutputConstraint(unsigned i) const {
405 return getOutputConstraintLiteral(i)->getString();
406 }
407
getInputExpr(unsigned i)408 Expr *GCCAsmStmt::getInputExpr(unsigned i) {
409 return cast<Expr>(Exprs[i + NumOutputs]);
410 }
setInputExpr(unsigned i,Expr * E)411 void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
412 Exprs[i + NumOutputs] = E;
413 }
414
415 /// getInputConstraint - Return the specified input constraint. Unlike output
416 /// constraints, these can be empty.
getInputConstraint(unsigned i) const417 StringRef GCCAsmStmt::getInputConstraint(unsigned i) const {
418 return getInputConstraintLiteral(i)->getString();
419 }
420
setOutputsAndInputsAndClobbers(const ASTContext & C,IdentifierInfo ** Names,StringLiteral ** Constraints,Stmt ** Exprs,unsigned NumOutputs,unsigned NumInputs,StringLiteral ** Clobbers,unsigned NumClobbers)421 void 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) {
429 this->NumOutputs = NumOutputs;
430 this->NumInputs = NumInputs;
431 this->NumClobbers = NumClobbers;
432
433 unsigned NumExprs = NumOutputs + NumInputs;
434
435 C.Deallocate(this->Names);
436 this->Names = new (C) IdentifierInfo*[NumExprs];
437 std::copy(Names, Names + NumExprs, this->Names);
438
439 C.Deallocate(this->Exprs);
440 this->Exprs = new (C) Stmt*[NumExprs];
441 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
442
443 C.Deallocate(this->Constraints);
444 this->Constraints = new (C) StringLiteral*[NumExprs];
445 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
446
447 C.Deallocate(this->Clobbers);
448 this->Clobbers = new (C) StringLiteral*[NumClobbers];
449 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
450 }
451
452 /// 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.
getNamedOperand(StringRef SymbolicName) const455 int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
456 unsigned NumPlusOperands = 0;
457
458 // 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;
462 }
463
464 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
472 /// 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.
AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> & Pieces,const ASTContext & C,unsigned & DiagOffs) const475 unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
476 const ASTContext &C, unsigned &DiagOffs) const {
477 StringRef Str = getAsmString()->getString();
478 const char *StrStart = Str.begin();
479 const char *StrEnd = Str.end();
480 const char *CurPtr = StrStart;
481
482 // "Simple" inline asms have no constraints or operands, just convert the asm
483 // string to escape $'s.
484 if (isSimple()) {
485 std::string Result;
486 for (; CurPtr != StrEnd; ++CurPtr) {
487 switch (*CurPtr) {
488 case '$':
489 Result += "$$";
490 break;
491 default:
492 Result += *CurPtr;
493 break;
494 }
495 }
496 Pieces.push_back(AsmStringPiece(Result));
497 return 0;
498 }
499
500 // CurStringPiece - The current string that we are building up as we scan the
501 // asm string.
502 std::string CurStringPiece;
503
504 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
505
506 unsigned LastAsmStringToken = 0;
507 unsigned LastAsmStringOffset = 0;
508
509 while (1) {
510 // Done with the string?
511 if (CurPtr == StrEnd) {
512 if (!CurStringPiece.empty())
513 Pieces.push_back(AsmStringPiece(CurStringPiece));
514 return 0;
515 }
516
517 char CurChar = *CurPtr++;
518 switch (CurChar) {
519 case '$': CurStringPiece += "$$"; continue;
520 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
521 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
522 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
523 case '%':
524 break;
525 default:
526 CurStringPiece += CurChar;
527 continue;
528 }
529
530 // Escaped "%" character in asm string.
531 if (CurPtr == StrEnd) {
532 // % at end of string is invalid (no escape).
533 DiagOffs = CurPtr-StrStart-1;
534 return diag::err_asm_invalid_escape;
535 }
536
537 char EscapedChar = *CurPtr++;
538 if (EscapedChar == '%') { // %% -> %
539 // Escaped percentage sign.
540 CurStringPiece += '%';
541 continue;
542 }
543
544 if (EscapedChar == '=') { // %= -> Generate an unique ID.
545 CurStringPiece += "${:uid}";
546 continue;
547 }
548
549 // Otherwise, we have an operand. If we have accumulated a string so far,
550 // add it to the Pieces list.
551 if (!CurStringPiece.empty()) {
552 Pieces.push_back(AsmStringPiece(CurStringPiece));
553 CurStringPiece.clear();
554 }
555
556 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
557 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
558
559 const char *Begin = CurPtr - 1; // Points to the character following '%'.
560 const char *Percent = Begin - 1; // Points to '%'.
561
562 if (isLetter(EscapedChar)) {
563 if (CurPtr == StrEnd) { // Premature end.
564 DiagOffs = CurPtr-StrStart-1;
565 return diag::err_asm_invalid_escape;
566 }
567 EscapedChar = *CurPtr++;
568 }
569
570 const TargetInfo &TI = C.getTargetInfo();
571 const SourceManager &SM = C.getSourceManager();
572 const LangOptions &LO = C.getLangOpts();
573
574 // Handle operands that don't have asmSymbolicName (e.g., %x4).
575 if (isDigit(EscapedChar)) {
576 // %n - Assembler operand n
577 unsigned N = 0;
578
579 --CurPtr;
580 while (CurPtr != StrEnd && isDigit(*CurPtr))
581 N = N*10 + ((*CurPtr++)-'0');
582
583 unsigned NumOperands =
584 getNumOutputs() + getNumPlusOperands() + getNumInputs();
585 if (N >= NumOperands) {
586 DiagOffs = CurPtr-StrStart-1;
587 return diag::err_asm_invalid_operand_number;
588 }
589
590 // Str contains "x4" (Operand without the leading %).
591 std::string Str(Begin, CurPtr - Begin);
592
593 // (BeginLoc, EndLoc) represents the range of the operand we are currently
594 // processing. Unlike Str, the range includes the leading '%'.
595 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
596 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
597 &LastAsmStringOffset);
598 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
599 CurPtr - StrStart, SM, LO, TI, &LastAsmStringToken,
600 &LastAsmStringOffset);
601
602 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
603 continue;
604 }
605
606 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
607 if (EscapedChar == '[') {
608 DiagOffs = CurPtr-StrStart-1;
609
610 // Find the ']'.
611 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
612 if (NameEnd == nullptr)
613 return diag::err_asm_unterminated_symbolic_operand_name;
614 if (NameEnd == CurPtr)
615 return diag::err_asm_empty_symbolic_operand_name;
616
617 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
618
619 int N = getNamedOperand(SymbolicName);
620 if (N == -1) {
621 // Verify that an operand with that name exists.
622 DiagOffs = CurPtr-StrStart;
623 return diag::err_asm_unknown_symbolic_operand_name;
624 }
625
626 // Str contains "x[foo]" (Operand without the leading %).
627 std::string Str(Begin, NameEnd + 1 - Begin);
628
629 // (BeginLoc, EndLoc) represents the range of the operand we are currently
630 // processing. Unlike Str, the range includes the leading '%'.
631 SourceLocation BeginLoc = getAsmString()->getLocationOfByte(
632 Percent - StrStart, SM, LO, TI, &LastAsmStringToken,
633 &LastAsmStringOffset);
634 SourceLocation EndLoc = getAsmString()->getLocationOfByte(
635 NameEnd + 1 - StrStart, SM, LO, TI, &LastAsmStringToken,
636 &LastAsmStringOffset);
637
638 Pieces.emplace_back(N, std::move(Str), BeginLoc, EndLoc);
639
640 CurPtr = NameEnd+1;
641 continue;
642 }
643
644 DiagOffs = CurPtr-StrStart-1;
645 return diag::err_asm_invalid_escape;
646 }
647 }
648
649 /// Assemble final IR asm string (GCC-style).
generateAsmString(const ASTContext & C) const650 std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
651 // Analyze the asm string to decompose it into its pieces. We know that Sema
652 // has already done this, so it is guaranteed to be successful.
653 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
654 unsigned DiagOffs;
655 AnalyzeAsmString(Pieces, C, DiagOffs);
656
657 std::string AsmString;
658 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
659 if (Pieces[i].isString())
660 AsmString += Pieces[i].getString();
661 else if (Pieces[i].getModifier() == '\0')
662 AsmString += '$' + llvm::utostr(Pieces[i].getOperandNo());
663 else
664 AsmString += "${" + llvm::utostr(Pieces[i].getOperandNo()) + ':' +
665 Pieces[i].getModifier() + '}';
666 }
667 return AsmString;
668 }
669
670 /// Assemble final IR asm string (MS-style).
generateAsmString(const ASTContext & C) const671 std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
672 // FIXME: This needs to be translated into the IR string representation.
673 return AsmStr;
674 }
675
getOutputExpr(unsigned i)676 Expr *MSAsmStmt::getOutputExpr(unsigned i) {
677 return cast<Expr>(Exprs[i]);
678 }
679
getInputExpr(unsigned i)680 Expr *MSAsmStmt::getInputExpr(unsigned i) {
681 return cast<Expr>(Exprs[i + NumOutputs]);
682 }
setInputExpr(unsigned i,Expr * E)683 void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
684 Exprs[i + NumOutputs] = E;
685 }
686
687 //===----------------------------------------------------------------------===//
688 // Constructors
689 //===----------------------------------------------------------------------===//
690
GCCAsmStmt(const ASTContext & C,SourceLocation asmloc,bool issimple,bool isvolatile,unsigned numoutputs,unsigned numinputs,IdentifierInfo ** names,StringLiteral ** constraints,Expr ** exprs,StringLiteral * asmstr,unsigned numclobbers,StringLiteral ** clobbers,SourceLocation rparenloc)691 GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
692 bool issimple, bool isvolatile, unsigned numoutputs,
693 unsigned numinputs, IdentifierInfo **names,
694 StringLiteral **constraints, Expr **exprs,
695 StringLiteral *asmstr, unsigned numclobbers,
696 StringLiteral **clobbers, SourceLocation rparenloc)
697 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
698 numinputs, numclobbers), RParenLoc(rparenloc), AsmStr(asmstr) {
699
700 unsigned NumExprs = NumOutputs + NumInputs;
701
702 Names = new (C) IdentifierInfo*[NumExprs];
703 std::copy(names, names + NumExprs, Names);
704
705 Exprs = new (C) Stmt*[NumExprs];
706 std::copy(exprs, exprs + NumExprs, Exprs);
707
708 Constraints = new (C) StringLiteral*[NumExprs];
709 std::copy(constraints, constraints + NumExprs, Constraints);
710
711 Clobbers = new (C) StringLiteral*[NumClobbers];
712 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
713 }
714
MSAsmStmt(const ASTContext & C,SourceLocation asmloc,SourceLocation lbraceloc,bool issimple,bool isvolatile,ArrayRef<Token> asmtoks,unsigned numoutputs,unsigned numinputs,ArrayRef<StringRef> constraints,ArrayRef<Expr * > exprs,StringRef asmstr,ArrayRef<StringRef> clobbers,SourceLocation endloc)715 MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
716 SourceLocation lbraceloc, bool issimple, bool isvolatile,
717 ArrayRef<Token> asmtoks, unsigned numoutputs,
718 unsigned numinputs,
719 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
720 StringRef asmstr, ArrayRef<StringRef> clobbers,
721 SourceLocation endloc)
722 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
723 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
724 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
725
726 initialize(C, asmstr, asmtoks, constraints, exprs, clobbers);
727 }
728
copyIntoContext(const ASTContext & C,StringRef str)729 static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
730 return str.copy(C);
731 }
732
initialize(const ASTContext & C,StringRef asmstr,ArrayRef<Token> asmtoks,ArrayRef<StringRef> constraints,ArrayRef<Expr * > exprs,ArrayRef<StringRef> clobbers)733 void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
734 ArrayRef<Token> asmtoks,
735 ArrayRef<StringRef> constraints,
736 ArrayRef<Expr*> exprs,
737 ArrayRef<StringRef> clobbers) {
738 assert(NumAsmToks == asmtoks.size());
739 assert(NumClobbers == clobbers.size());
740
741 assert(exprs.size() == NumOutputs + NumInputs);
742 assert(exprs.size() == constraints.size());
743
744 AsmStr = copyIntoContext(C, asmstr);
745
746 Exprs = new (C) Stmt*[exprs.size()];
747 std::copy(exprs.begin(), exprs.end(), Exprs);
748
749 AsmToks = new (C) Token[asmtoks.size()];
750 std::copy(asmtoks.begin(), asmtoks.end(), AsmToks);
751
752 Constraints = new (C) StringRef[exprs.size()];
753 std::transform(constraints.begin(), constraints.end(), Constraints,
754 [&](StringRef Constraint) {
755 return copyIntoContext(C, Constraint);
756 });
757
758 Clobbers = new (C) StringRef[NumClobbers];
759 // FIXME: Avoid the allocation/copy if at all possible.
760 std::transform(clobbers.begin(), clobbers.end(), Clobbers,
761 [&](StringRef Clobber) {
762 return copyIntoContext(C, Clobber);
763 });
764 }
765
IfStmt(const ASTContext & C,SourceLocation IL,bool IsConstexpr,Stmt * init,VarDecl * var,Expr * cond,Stmt * then,SourceLocation EL,Stmt * elsev)766 IfStmt::IfStmt(const ASTContext &C, SourceLocation IL, bool IsConstexpr,
767 Stmt *init, VarDecl *var, Expr *cond, Stmt *then,
768 SourceLocation EL, Stmt *elsev)
769 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL) {
770 setConstexpr(IsConstexpr);
771 setConditionVariable(C, var);
772 SubExprs[INIT] = init;
773 SubExprs[COND] = cond;
774 SubExprs[THEN] = then;
775 SubExprs[ELSE] = elsev;
776 }
777
getConditionVariable() const778 VarDecl *IfStmt::getConditionVariable() const {
779 if (!SubExprs[VAR])
780 return nullptr;
781
782 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
783 return cast<VarDecl>(DS->getSingleDecl());
784 }
785
setConditionVariable(const ASTContext & C,VarDecl * V)786 void IfStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
787 if (!V) {
788 SubExprs[VAR] = nullptr;
789 return;
790 }
791
792 SourceRange VarRange = V->getSourceRange();
793 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
794 VarRange.getEnd());
795 }
796
ForStmt(const ASTContext & C,Stmt * Init,Expr * Cond,VarDecl * condVar,Expr * Inc,Stmt * Body,SourceLocation FL,SourceLocation LP,SourceLocation RP)797 ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
798 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
799 SourceLocation RP)
800 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
801 {
802 SubExprs[INIT] = Init;
803 setConditionVariable(C, condVar);
804 SubExprs[COND] = Cond;
805 SubExprs[INC] = Inc;
806 SubExprs[BODY] = Body;
807 }
808
getConditionVariable() const809 VarDecl *ForStmt::getConditionVariable() const {
810 if (!SubExprs[CONDVAR])
811 return nullptr;
812
813 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
814 return cast<VarDecl>(DS->getSingleDecl());
815 }
816
setConditionVariable(const ASTContext & C,VarDecl * V)817 void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
818 if (!V) {
819 SubExprs[CONDVAR] = nullptr;
820 return;
821 }
822
823 SourceRange VarRange = V->getSourceRange();
824 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
825 VarRange.getEnd());
826 }
827
SwitchStmt(const ASTContext & C,Stmt * init,VarDecl * Var,Expr * cond)828 SwitchStmt::SwitchStmt(const ASTContext &C, Stmt *init, VarDecl *Var,
829 Expr *cond)
830 : Stmt(SwitchStmtClass), FirstCase(nullptr, false) {
831 setConditionVariable(C, Var);
832 SubExprs[INIT] = init;
833 SubExprs[COND] = cond;
834 SubExprs[BODY] = nullptr;
835 }
836
getConditionVariable() const837 VarDecl *SwitchStmt::getConditionVariable() const {
838 if (!SubExprs[VAR])
839 return nullptr;
840
841 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
842 return cast<VarDecl>(DS->getSingleDecl());
843 }
844
setConditionVariable(const ASTContext & C,VarDecl * V)845 void SwitchStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
846 if (!V) {
847 SubExprs[VAR] = nullptr;
848 return;
849 }
850
851 SourceRange VarRange = V->getSourceRange();
852 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
853 VarRange.getEnd());
854 }
855
getSubStmt()856 Stmt *SwitchCase::getSubStmt() {
857 if (isa<CaseStmt>(this))
858 return cast<CaseStmt>(this)->getSubStmt();
859 return cast<DefaultStmt>(this)->getSubStmt();
860 }
861
WhileStmt(const ASTContext & C,VarDecl * Var,Expr * cond,Stmt * body,SourceLocation WL)862 WhileStmt::WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
863 SourceLocation WL)
864 : Stmt(WhileStmtClass) {
865 setConditionVariable(C, Var);
866 SubExprs[COND] = cond;
867 SubExprs[BODY] = body;
868 WhileLoc = WL;
869 }
870
getConditionVariable() const871 VarDecl *WhileStmt::getConditionVariable() const {
872 if (!SubExprs[VAR])
873 return nullptr;
874
875 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
876 return cast<VarDecl>(DS->getSingleDecl());
877 }
878
setConditionVariable(const ASTContext & C,VarDecl * V)879 void WhileStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
880 if (!V) {
881 SubExprs[VAR] = nullptr;
882 return;
883 }
884
885 SourceRange VarRange = V->getSourceRange();
886 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
887 VarRange.getEnd());
888 }
889
890 // IndirectGotoStmt
getConstantTarget()891 LabelDecl *IndirectGotoStmt::getConstantTarget() {
892 if (AddrLabelExpr *E =
893 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
894 return E->getLabel();
895 return nullptr;
896 }
897
898 // ReturnStmt
getRetValue() const899 const Expr* ReturnStmt::getRetValue() const {
900 return cast_or_null<Expr>(RetExpr);
901 }
getRetValue()902 Expr* ReturnStmt::getRetValue() {
903 return cast_or_null<Expr>(RetExpr);
904 }
905
SEHTryStmt(bool IsCXXTry,SourceLocation TryLoc,Stmt * TryBlock,Stmt * Handler)906 SEHTryStmt::SEHTryStmt(bool IsCXXTry,
907 SourceLocation TryLoc,
908 Stmt *TryBlock,
909 Stmt *Handler)
910 : Stmt(SEHTryStmtClass),
911 IsCXXTry(IsCXXTry),
912 TryLoc(TryLoc)
913 {
914 Children[TRY] = TryBlock;
915 Children[HANDLER] = Handler;
916 }
917
Create(const ASTContext & C,bool IsCXXTry,SourceLocation TryLoc,Stmt * TryBlock,Stmt * Handler)918 SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
919 SourceLocation TryLoc, Stmt *TryBlock,
920 Stmt *Handler) {
921 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
922 }
923
getExceptHandler() const924 SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
925 return dyn_cast<SEHExceptStmt>(getHandler());
926 }
927
getFinallyHandler() const928 SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
929 return dyn_cast<SEHFinallyStmt>(getHandler());
930 }
931
SEHExceptStmt(SourceLocation Loc,Expr * FilterExpr,Stmt * Block)932 SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
933 Expr *FilterExpr,
934 Stmt *Block)
935 : Stmt(SEHExceptStmtClass),
936 Loc(Loc)
937 {
938 Children[FILTER_EXPR] = FilterExpr;
939 Children[BLOCK] = Block;
940 }
941
Create(const ASTContext & C,SourceLocation Loc,Expr * FilterExpr,Stmt * Block)942 SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
943 Expr *FilterExpr, Stmt *Block) {
944 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
945 }
946
SEHFinallyStmt(SourceLocation Loc,Stmt * Block)947 SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
948 Stmt *Block)
949 : Stmt(SEHFinallyStmtClass),
950 Loc(Loc),
951 Block(Block)
952 {}
953
Create(const ASTContext & C,SourceLocation Loc,Stmt * Block)954 SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
955 Stmt *Block) {
956 return new(C)SEHFinallyStmt(Loc,Block);
957 }
958
Capture(SourceLocation Loc,VariableCaptureKind Kind,VarDecl * Var)959 CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
960 VarDecl *Var)
961 : VarAndKind(Var, Kind), Loc(Loc) {
962 switch (Kind) {
963 case VCK_This:
964 assert(!Var && "'this' capture cannot have a variable!");
965 break;
966 case VCK_ByRef:
967 assert(Var && "capturing by reference must have a variable!");
968 break;
969 case VCK_ByCopy:
970 assert(Var && "capturing by copy must have a variable!");
971 assert(
972 (Var->getType()->isScalarType() || (Var->getType()->isReferenceType() &&
973 Var->getType()
974 ->castAs<ReferenceType>()
975 ->getPointeeType()
976 ->isScalarType())) &&
977 "captures by copy are expected to have a scalar type!");
978 break;
979 case VCK_VLAType:
980 assert(!Var &&
981 "Variable-length array type capture cannot have a variable!");
982 break;
983 }
984 }
985
986 CapturedStmt::VariableCaptureKind
getCaptureKind() const987 CapturedStmt::Capture::getCaptureKind() const {
988 return VarAndKind.getInt();
989 }
990
getCapturedVar() const991 VarDecl *CapturedStmt::Capture::getCapturedVar() const {
992 assert((capturesVariable() || capturesVariableByCopy()) &&
993 "No variable available for 'this' or VAT capture");
994 return VarAndKind.getPointer();
995 }
996
getStoredCaptures() const997 CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
998 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
999
1000 // Offset of the first Capture object.
1001 unsigned FirstCaptureOffset = llvm::alignTo(Size, llvm::alignOf<Capture>());
1002
1003 return reinterpret_cast<Capture *>(
1004 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1005 + FirstCaptureOffset);
1006 }
1007
CapturedStmt(Stmt * S,CapturedRegionKind Kind,ArrayRef<Capture> Captures,ArrayRef<Expr * > CaptureInits,CapturedDecl * CD,RecordDecl * RD)1008 CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1009 ArrayRef<Capture> Captures,
1010 ArrayRef<Expr *> CaptureInits,
1011 CapturedDecl *CD,
1012 RecordDecl *RD)
1013 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
1014 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
1015 assert( S && "null captured statement");
1016 assert(CD && "null captured declaration for captured statement");
1017 assert(RD && "null record declaration for captured statement");
1018
1019 // Copy initialization expressions.
1020 Stmt **Stored = getStoredStmts();
1021 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1022 *Stored++ = CaptureInits[I];
1023
1024 // Copy the statement being captured.
1025 *Stored = S;
1026
1027 // Copy all Capture objects.
1028 Capture *Buffer = getStoredCaptures();
1029 std::copy(Captures.begin(), Captures.end(), Buffer);
1030 }
1031
CapturedStmt(EmptyShell Empty,unsigned NumCaptures)1032 CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1033 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
1034 CapDeclAndKind(nullptr, CR_Default), TheRecordDecl(nullptr) {
1035 getStoredStmts()[NumCaptures] = nullptr;
1036 }
1037
Create(const ASTContext & Context,Stmt * S,CapturedRegionKind Kind,ArrayRef<Capture> Captures,ArrayRef<Expr * > CaptureInits,CapturedDecl * CD,RecordDecl * RD)1038 CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
1039 CapturedRegionKind Kind,
1040 ArrayRef<Capture> Captures,
1041 ArrayRef<Expr *> CaptureInits,
1042 CapturedDecl *CD,
1043 RecordDecl *RD) {
1044 // The layout is
1045 //
1046 // -----------------------------------------------------------
1047 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1048 // ----------------^-------------------^----------------------
1049 // getStoredStmts() getStoredCaptures()
1050 //
1051 // where S is the statement being captured.
1052 //
1053 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1054
1055 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1056 if (!Captures.empty()) {
1057 // Realign for the following Capture array.
1058 Size = llvm::alignTo(Size, llvm::alignOf<Capture>());
1059 Size += sizeof(Capture) * Captures.size();
1060 }
1061
1062 void *Mem = Context.Allocate(Size);
1063 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
1064 }
1065
CreateDeserialized(const ASTContext & Context,unsigned NumCaptures)1066 CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
1067 unsigned NumCaptures) {
1068 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1069 if (NumCaptures > 0) {
1070 // Realign for the following Capture array.
1071 Size = llvm::alignTo(Size, llvm::alignOf<Capture>());
1072 Size += sizeof(Capture) * NumCaptures;
1073 }
1074
1075 void *Mem = Context.Allocate(Size);
1076 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1077 }
1078
children()1079 Stmt::child_range CapturedStmt::children() {
1080 // Children are captured field initilizers.
1081 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1082 }
1083
getCapturedDecl()1084 CapturedDecl *CapturedStmt::getCapturedDecl() {
1085 return CapDeclAndKind.getPointer();
1086 }
getCapturedDecl() const1087 const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1088 return CapDeclAndKind.getPointer();
1089 }
1090
1091 /// \brief Set the outlined function declaration.
setCapturedDecl(CapturedDecl * D)1092 void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1093 assert(D && "null CapturedDecl");
1094 CapDeclAndKind.setPointer(D);
1095 }
1096
1097 /// \brief Retrieve the captured region kind.
getCapturedRegionKind() const1098 CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1099 return CapDeclAndKind.getInt();
1100 }
1101
1102 /// \brief Set the captured region kind.
setCapturedRegionKind(CapturedRegionKind Kind)1103 void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1104 CapDeclAndKind.setInt(Kind);
1105 }
1106
capturesVariable(const VarDecl * Var) const1107 bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
1108 for (const auto &I : captures()) {
1109 if (!I.capturesVariable())
1110 continue;
1111
1112 // This does not handle variable redeclarations. This should be
1113 // extended to capture variables with redeclarations, for example
1114 // a thread-private variable in OpenMP.
1115 if (I.getCapturedVar() == Var)
1116 return true;
1117 }
1118
1119 return false;
1120 }
1121