1 //===- Stmt.h - Classes for representing statements -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Stmt interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_STMT_H
15 #define LLVM_CLANG_AST_STMT_H
17 #include "clang/AST/DeclGroup.h"
18 #include "clang/AST/StmtIterator.h"
19 #include "clang/Basic/CapturedStmt.h"
20 #include "clang/Basic/IdentifierTable.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/iterator.h"
27 #include "llvm/ADT/iterator_range.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/ErrorHandling.h"
39 class FoldingSetNodeID;
53 struct PrintingPolicy;
60 //===----------------------------------------------------------------------===//
61 // AST classes for statements.
62 //===----------------------------------------------------------------------===//
64 /// Stmt - This represents one statement.
66 class alignas(void *) Stmt {
70 #define STMT(CLASS, PARENT) CLASS##Class,
71 #define STMT_RANGE(BASE, FIRST, LAST) \
72 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
73 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
74 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
75 #define ABSTRACT_STMT(STMT)
76 #include "clang/AST/StmtNodes.inc"
79 // Make vanilla 'new' and 'delete' illegal for Stmts.
81 friend class ASTStmtReader;
82 friend class ASTStmtWriter;
84 void *operator new(size_t bytes) noexcept {
85 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
88 void operator delete(void *data) noexcept {
89 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
95 /// The statement class.
98 enum { NumStmtBits = 8 };
100 class CompoundStmtBitfields {
101 friend class CompoundStmt;
103 unsigned : NumStmtBits;
105 unsigned NumStmts : 32 - NumStmtBits;
108 class IfStmtBitfields {
111 unsigned : NumStmtBits;
113 unsigned IsConstexpr : 1;
116 class ExprBitfields {
117 friend class ASTStmtReader; // deserialization
118 friend class AtomicExpr; // ctor
119 friend class BlockDeclRefExpr; // ctor
120 friend class CallExpr; // ctor
121 friend class CXXConstructExpr; // ctor
122 friend class CXXDependentScopeMemberExpr; // ctor
123 friend class CXXNewExpr; // ctor
124 friend class CXXUnresolvedConstructExpr; // ctor
125 friend class DeclRefExpr; // computeDependence
126 friend class DependentScopeDeclRefExpr; // ctor
127 friend class DesignatedInitExpr; // ctor
129 friend class InitListExpr; // ctor
130 friend class ObjCArrayLiteral; // ctor
131 friend class ObjCDictionaryLiteral; // ctor
132 friend class ObjCMessageExpr; // ctor
133 friend class OffsetOfExpr; // ctor
134 friend class OpaqueValueExpr; // ctor
135 friend class OverloadExpr; // ctor
136 friend class ParenListExpr; // ctor
137 friend class PseudoObjectExpr; // ctor
138 friend class ShuffleVectorExpr; // ctor
140 unsigned : NumStmtBits;
142 unsigned ValueKind : 2;
143 unsigned ObjectKind : 3;
144 unsigned TypeDependent : 1;
145 unsigned ValueDependent : 1;
146 unsigned InstantiationDependent : 1;
147 unsigned ContainsUnexpandedParameterPack : 1;
149 enum { NumExprBits = 17 };
151 class CharacterLiteralBitfields {
152 friend class CharacterLiteral;
154 unsigned : NumExprBits;
159 enum APFloatSemantics {
168 class FloatingLiteralBitfields {
169 friend class FloatingLiteral;
171 unsigned : NumExprBits;
173 unsigned Semantics : 3; // Provides semantics for APFloat construction
174 unsigned IsExact : 1;
177 class UnaryExprOrTypeTraitExprBitfields {
178 friend class UnaryExprOrTypeTraitExpr;
180 unsigned : NumExprBits;
183 unsigned IsType : 1; // true if operand is a type, false if an expression.
186 class DeclRefExprBitfields {
187 friend class ASTStmtReader; // deserialization
188 friend class DeclRefExpr;
190 unsigned : NumExprBits;
192 unsigned HasQualifier : 1;
193 unsigned HasTemplateKWAndArgsInfo : 1;
194 unsigned HasFoundDecl : 1;
195 unsigned HadMultipleCandidates : 1;
196 unsigned RefersToEnclosingVariableOrCapture : 1;
199 class CastExprBitfields {
200 friend class CastExpr;
201 friend class ImplicitCastExpr;
203 unsigned : NumExprBits;
206 unsigned PartOfExplicitCast : 1; // Only set for ImplicitCastExpr.
207 unsigned BasePathSize : 32 - 6 - 1 - NumExprBits;
210 class CallExprBitfields {
211 friend class CallExpr;
213 unsigned : NumExprBits;
215 unsigned NumPreArgs : 1;
218 class ExprWithCleanupsBitfields {
219 friend class ASTStmtReader; // deserialization
220 friend class ExprWithCleanups;
222 unsigned : NumExprBits;
224 // When false, it must not have side effects.
225 unsigned CleanupsHaveSideEffects : 1;
227 unsigned NumObjects : 32 - 1 - NumExprBits;
230 class PseudoObjectExprBitfields {
231 friend class ASTStmtReader; // deserialization
232 friend class PseudoObjectExpr;
234 unsigned : NumExprBits;
236 // These don't need to be particularly wide, because they're
237 // strictly limited by the forms of expressions we permit.
238 unsigned NumSubExprs : 8;
239 unsigned ResultIndex : 32 - 8 - NumExprBits;
242 class OpaqueValueExprBitfields {
243 friend class OpaqueValueExpr;
245 unsigned : NumExprBits;
247 /// The OVE is a unique semantic reference to its source expressio if this
248 /// bit is set to true.
249 unsigned IsUnique : 1;
252 class ObjCIndirectCopyRestoreExprBitfields {
253 friend class ObjCIndirectCopyRestoreExpr;
255 unsigned : NumExprBits;
257 unsigned ShouldCopy : 1;
260 class InitListExprBitfields {
261 friend class InitListExpr;
263 unsigned : NumExprBits;
265 /// Whether this initializer list originally had a GNU array-range
266 /// designator in it. This is a temporary marker used by CodeGen.
267 unsigned HadArrayRangeDesignator : 1;
270 class TypeTraitExprBitfields {
271 friend class ASTStmtReader;
272 friend class ASTStmtWriter;
273 friend class TypeTraitExpr;
275 unsigned : NumExprBits;
277 /// The kind of type trait, which is a value of a TypeTrait enumerator.
280 /// If this expression is not value-dependent, this indicates whether
281 /// the trait evaluated true or false.
284 /// The number of arguments to this type trait.
285 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
288 class CoawaitExprBitfields {
289 friend class CoawaitExpr;
291 unsigned : NumExprBits;
293 unsigned IsImplicit : 1;
297 StmtBitfields StmtBits;
298 CompoundStmtBitfields CompoundStmtBits;
299 IfStmtBitfields IfStmtBits;
300 ExprBitfields ExprBits;
301 CharacterLiteralBitfields CharacterLiteralBits;
302 FloatingLiteralBitfields FloatingLiteralBits;
303 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
304 DeclRefExprBitfields DeclRefExprBits;
305 CastExprBitfields CastExprBits;
306 CallExprBitfields CallExprBits;
307 ExprWithCleanupsBitfields ExprWithCleanupsBits;
308 PseudoObjectExprBitfields PseudoObjectExprBits;
309 OpaqueValueExprBitfields OpaqueValueExprBits;
310 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
311 InitListExprBitfields InitListExprBits;
312 TypeTraitExprBitfields TypeTraitExprBits;
313 CoawaitExprBitfields CoawaitBits;
317 // Only allow allocation of Stmts using the allocator in ASTContext
318 // or by doing a placement new.
319 void* operator new(size_t bytes, const ASTContext& C,
320 unsigned alignment = 8);
322 void* operator new(size_t bytes, const ASTContext* C,
323 unsigned alignment = 8) {
324 return operator new(bytes, *C, alignment);
327 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
329 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
330 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
331 void operator delete(void *, size_t) noexcept {}
332 void operator delete(void *, void *) noexcept {}
335 /// A placeholder type used to construct an empty shell of a
336 /// type, that will be filled in later (e.g., by some
337 /// de-serialization).
338 struct EmptyShell {};
341 /// Iterator for iterating over Stmt * arrays that contain only Expr *
343 /// This is needed because AST nodes use Stmt* arrays to store
344 /// references to children (to be compatible with StmtIterator).
346 : llvm::iterator_adaptor_base<ExprIterator, Stmt **,
347 std::random_access_iterator_tag, Expr *> {
348 ExprIterator() : iterator_adaptor_base(nullptr) {}
349 ExprIterator(Stmt **I) : iterator_adaptor_base(I) {}
351 reference operator*() const {
352 assert((*I)->getStmtClass() >= firstExprConstant &&
353 (*I)->getStmtClass() <= lastExprConstant);
354 return *reinterpret_cast<Expr **>(I);
358 /// Const iterator for iterating over Stmt * arrays that contain only Expr *
359 struct ConstExprIterator
360 : llvm::iterator_adaptor_base<ConstExprIterator, const Stmt *const *,
361 std::random_access_iterator_tag,
363 ConstExprIterator() : iterator_adaptor_base(nullptr) {}
364 ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {}
366 reference operator*() const {
367 assert((*I)->getStmtClass() >= firstExprConstant &&
368 (*I)->getStmtClass() <= lastExprConstant);
369 return *reinterpret_cast<const Expr *const *>(I);
374 /// Whether statistic collection is enabled.
375 static bool StatisticsEnabled;
378 /// Construct an empty statement.
379 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
383 static_assert(sizeof(*this) == sizeof(void *),
384 "changing bitfields changed sizeof(Stmt)");
385 static_assert(sizeof(*this) % alignof(void *) == 0,
386 "Insufficient alignment!");
387 StmtBits.sClass = SC;
388 if (StatisticsEnabled) Stmt::addStmtClass(SC);
391 StmtClass getStmtClass() const {
392 return static_cast<StmtClass>(StmtBits.sClass);
395 const char *getStmtClassName() const;
397 /// SourceLocation tokens are not useful in isolation - they are low level
398 /// value objects created/interpreted by SourceManager. We assume AST
399 /// clients will have a pointer to the respective SourceManager.
400 SourceRange getSourceRange() const LLVM_READONLY;
401 SourceLocation getLocStart() const LLVM_READONLY;
402 SourceLocation getLocEnd() const LLVM_READONLY;
404 // global temp stats (until we have a per-module visitor)
405 static void addStmtClass(const StmtClass s);
406 static void EnableStatistics();
407 static void PrintStats();
409 /// Dumps the specified AST fragment and all subtrees to
412 void dump(SourceManager &SM) const;
413 void dump(raw_ostream &OS, SourceManager &SM) const;
414 void dump(raw_ostream &OS) const;
416 /// dumpColor - same as dump(), but forces color highlighting.
417 void dumpColor() const;
419 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
420 /// back to its original source language syntax.
421 void dumpPretty(const ASTContext &Context) const;
422 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
423 const PrintingPolicy &Policy, unsigned Indentation = 0,
424 const ASTContext *Context = nullptr) const;
426 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
427 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
428 void viewAST() const;
430 /// Skip past any implicit AST nodes which might surround this
431 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
432 Stmt *IgnoreImplicit();
433 const Stmt *IgnoreImplicit() const {
434 return const_cast<Stmt *>(this)->IgnoreImplicit();
437 /// Skip no-op (attributed, compound) container stmts and skip captured
438 /// stmt at the top, if \a IgnoreCaptured is true.
439 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
440 const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
441 return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
444 const Stmt *stripLabelLikeStatements() const;
445 Stmt *stripLabelLikeStatements() {
446 return const_cast<Stmt*>(
447 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
450 /// Child Iterators: All subclasses must implement 'children'
451 /// to permit easy iteration over the substatements/subexpessions of an
452 /// AST node. This permits easy iteration over all nodes in the AST.
453 using child_iterator = StmtIterator;
454 using const_child_iterator = ConstStmtIterator;
456 using child_range = llvm::iterator_range<child_iterator>;
457 using const_child_range = llvm::iterator_range<const_child_iterator>;
459 child_range children();
461 const_child_range children() const {
462 auto Children = const_cast<Stmt *>(this)->children();
463 return const_child_range(Children.begin(), Children.end());
466 child_iterator child_begin() { return children().begin(); }
467 child_iterator child_end() { return children().end(); }
469 const_child_iterator child_begin() const { return children().begin(); }
470 const_child_iterator child_end() const { return children().end(); }
472 /// Produce a unique representation of the given statement.
474 /// \param ID once the profiling operation is complete, will contain
475 /// the unique representation of the given statement.
477 /// \param Context the AST context in which the statement resides
479 /// \param Canonical whether the profile should be based on the canonical
480 /// representation of this statement (e.g., where non-type template
481 /// parameters are identified by index/level rather than their
482 /// declaration pointers) or the exact representation of the statement as
483 /// written in the source.
484 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
485 bool Canonical) const;
487 /// Calculate a unique representation for a statement that is
488 /// stable across compiler invocations.
490 /// \param ID profile information will be stored in ID.
492 /// \param Hash an ODRHash object which will be called where pointers would
493 /// have been used in the Profile function.
494 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
497 /// DeclStmt - Adaptor class for mixing declarations with statements and
498 /// expressions. For example, CompoundStmt mixes statements, expressions
499 /// and declarations (variables, types). Another example is ForStmt, where
500 /// the first statement can be an expression or a declaration.
501 class DeclStmt : public Stmt {
503 SourceLocation StartLoc, EndLoc;
506 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
507 : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
509 /// Build an empty declaration statement.
510 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
512 /// isSingleDecl - This method returns true if this DeclStmt refers
513 /// to a single Decl.
514 bool isSingleDecl() const {
515 return DG.isSingleDecl();
518 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
519 Decl *getSingleDecl() { return DG.getSingleDecl(); }
521 const DeclGroupRef getDeclGroup() const { return DG; }
522 DeclGroupRef getDeclGroup() { return DG; }
523 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
525 SourceLocation getStartLoc() const { return StartLoc; }
526 void setStartLoc(SourceLocation L) { StartLoc = L; }
527 SourceLocation getEndLoc() const { return EndLoc; }
528 void setEndLoc(SourceLocation L) { EndLoc = L; }
530 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
531 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
533 static bool classof(const Stmt *T) {
534 return T->getStmtClass() == DeclStmtClass;
537 // Iterators over subexpressions.
538 child_range children() {
539 return child_range(child_iterator(DG.begin(), DG.end()),
540 child_iterator(DG.end(), DG.end()));
543 using decl_iterator = DeclGroupRef::iterator;
544 using const_decl_iterator = DeclGroupRef::const_iterator;
545 using decl_range = llvm::iterator_range<decl_iterator>;
546 using decl_const_range = llvm::iterator_range<const_decl_iterator>;
548 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
550 decl_const_range decls() const {
551 return decl_const_range(decl_begin(), decl_end());
554 decl_iterator decl_begin() { return DG.begin(); }
555 decl_iterator decl_end() { return DG.end(); }
556 const_decl_iterator decl_begin() const { return DG.begin(); }
557 const_decl_iterator decl_end() const { return DG.end(); }
559 using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
561 reverse_decl_iterator decl_rbegin() {
562 return reverse_decl_iterator(decl_end());
565 reverse_decl_iterator decl_rend() {
566 return reverse_decl_iterator(decl_begin());
570 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
572 class NullStmt : public Stmt {
573 SourceLocation SemiLoc;
575 /// True if the null statement was preceded by an empty macro, e.g:
580 bool HasLeadingEmptyMacro = false;
583 friend class ASTStmtReader;
584 friend class ASTStmtWriter;
586 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
587 : Stmt(NullStmtClass), SemiLoc(L),
588 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
590 /// Build an empty null statement.
591 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
593 SourceLocation getSemiLoc() const { return SemiLoc; }
594 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
596 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
598 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
599 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
601 static bool classof(const Stmt *T) {
602 return T->getStmtClass() == NullStmtClass;
605 child_range children() {
606 return child_range(child_iterator(), child_iterator());
610 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
611 class CompoundStmt final : public Stmt,
612 private llvm::TrailingObjects<CompoundStmt, Stmt *> {
613 friend class ASTStmtReader;
614 friend TrailingObjects;
616 SourceLocation LBraceLoc, RBraceLoc;
618 CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
619 explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
621 void setStmts(ArrayRef<Stmt *> Stmts);
624 static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
625 SourceLocation LB, SourceLocation RB);
627 // Build an empty compound statement with a location.
628 explicit CompoundStmt(SourceLocation Loc)
629 : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(Loc) {
630 CompoundStmtBits.NumStmts = 0;
633 // Build an empty compound statement.
634 static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
636 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
637 unsigned size() const { return CompoundStmtBits.NumStmts; }
639 using body_iterator = Stmt **;
640 using body_range = llvm::iterator_range<body_iterator>;
642 body_range body() { return body_range(body_begin(), body_end()); }
643 body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
644 body_iterator body_end() { return body_begin() + size(); }
645 Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
648 return !body_empty() ? body_begin()[size() - 1] : nullptr;
651 void setLastStmt(Stmt *S) {
652 assert(!body_empty() && "setLastStmt");
653 body_begin()[size() - 1] = S;
656 using const_body_iterator = Stmt* const *;
657 using body_const_range = llvm::iterator_range<const_body_iterator>;
659 body_const_range body() const {
660 return body_const_range(body_begin(), body_end());
663 const_body_iterator body_begin() const {
664 return getTrailingObjects<Stmt *>();
667 const_body_iterator body_end() const { return body_begin() + size(); }
669 const Stmt *body_front() const {
670 return !body_empty() ? body_begin()[0] : nullptr;
673 const Stmt *body_back() const {
674 return !body_empty() ? body_begin()[size() - 1] : nullptr;
677 using reverse_body_iterator = std::reverse_iterator<body_iterator>;
679 reverse_body_iterator body_rbegin() {
680 return reverse_body_iterator(body_end());
683 reverse_body_iterator body_rend() {
684 return reverse_body_iterator(body_begin());
687 using const_reverse_body_iterator =
688 std::reverse_iterator<const_body_iterator>;
690 const_reverse_body_iterator body_rbegin() const {
691 return const_reverse_body_iterator(body_end());
694 const_reverse_body_iterator body_rend() const {
695 return const_reverse_body_iterator(body_begin());
698 SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; }
699 SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; }
701 SourceLocation getLBracLoc() const { return LBraceLoc; }
702 SourceLocation getRBracLoc() const { return RBraceLoc; }
704 static bool classof(const Stmt *T) {
705 return T->getStmtClass() == CompoundStmtClass;
709 child_range children() { return child_range(body_begin(), body_end()); }
711 const_child_range children() const {
712 return const_child_range(body_begin(), body_end());
716 // SwitchCase is the base class for CaseStmt and DefaultStmt,
717 class SwitchCase : public Stmt {
719 // A pointer to the following CaseStmt or DefaultStmt class,
720 // used by SwitchStmt.
721 SwitchCase *NextSwitchCase = nullptr;
722 SourceLocation KeywordLoc;
723 SourceLocation ColonLoc;
725 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
726 : Stmt(SC), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {}
728 SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
731 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
733 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
735 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
737 SourceLocation getKeywordLoc() const { return KeywordLoc; }
738 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
739 SourceLocation getColonLoc() const { return ColonLoc; }
740 void setColonLoc(SourceLocation L) { ColonLoc = L; }
743 const Stmt *getSubStmt() const {
744 return const_cast<SwitchCase*>(this)->getSubStmt();
747 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
748 SourceLocation getLocEnd() const LLVM_READONLY;
750 static bool classof(const Stmt *T) {
751 return T->getStmtClass() == CaseStmtClass ||
752 T->getStmtClass() == DefaultStmtClass;
756 class CaseStmt : public SwitchCase {
757 SourceLocation EllipsisLoc;
758 enum { LHS, RHS, SUBSTMT, END_EXPR };
759 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
760 // GNU "case 1 ... 4" extension
763 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
764 SourceLocation ellipsisLoc, SourceLocation colonLoc)
765 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
766 SubExprs[SUBSTMT] = nullptr;
767 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
768 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
769 EllipsisLoc = ellipsisLoc;
772 /// Build an empty switch case statement.
773 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) {}
775 SourceLocation getCaseLoc() const { return KeywordLoc; }
776 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
777 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
778 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
779 SourceLocation getColonLoc() const { return ColonLoc; }
780 void setColonLoc(SourceLocation L) { ColonLoc = L; }
782 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
783 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
784 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
786 const Expr *getLHS() const {
787 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
790 const Expr *getRHS() const {
791 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
794 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
796 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
797 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
798 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
800 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
802 SourceLocation getLocEnd() const LLVM_READONLY {
803 // Handle deeply nested case statements with iteration instead of recursion.
804 const CaseStmt *CS = this;
805 while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
808 return CS->getSubStmt()->getLocEnd();
811 static bool classof(const Stmt *T) {
812 return T->getStmtClass() == CaseStmtClass;
816 child_range children() {
817 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
821 class DefaultStmt : public SwitchCase {
825 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
826 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
828 /// Build an empty default statement.
829 explicit DefaultStmt(EmptyShell Empty)
830 : SwitchCase(DefaultStmtClass, Empty) {}
832 Stmt *getSubStmt() { return SubStmt; }
833 const Stmt *getSubStmt() const { return SubStmt; }
834 void setSubStmt(Stmt *S) { SubStmt = S; }
836 SourceLocation getDefaultLoc() const { return KeywordLoc; }
837 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
838 SourceLocation getColonLoc() const { return ColonLoc; }
839 void setColonLoc(SourceLocation L) { ColonLoc = L; }
841 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
842 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
844 static bool classof(const Stmt *T) {
845 return T->getStmtClass() == DefaultStmtClass;
849 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
852 inline SourceLocation SwitchCase::getLocEnd() const {
853 if (const auto *CS = dyn_cast<CaseStmt>(this))
854 return CS->getLocEnd();
855 return cast<DefaultStmt>(this)->getLocEnd();
858 /// LabelStmt - Represents a label, which has a substatement. For example:
860 class LabelStmt : public Stmt {
861 SourceLocation IdentLoc;
866 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
867 : Stmt(LabelStmtClass), IdentLoc(IL), TheDecl(D), SubStmt(substmt) {
868 static_assert(sizeof(LabelStmt) ==
869 2 * sizeof(SourceLocation) + 2 * sizeof(void *),
870 "LabelStmt too big");
873 // Build an empty label statement.
874 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) {}
876 SourceLocation getIdentLoc() const { return IdentLoc; }
877 LabelDecl *getDecl() const { return TheDecl; }
878 void setDecl(LabelDecl *D) { TheDecl = D; }
879 const char *getName() const;
880 Stmt *getSubStmt() { return SubStmt; }
881 const Stmt *getSubStmt() const { return SubStmt; }
882 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
883 void setSubStmt(Stmt *SS) { SubStmt = SS; }
885 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
886 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
888 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
890 static bool classof(const Stmt *T) {
891 return T->getStmtClass() == LabelStmtClass;
895 /// Represents an attribute applied to a statement.
897 /// Represents an attribute applied to a statement. For example:
898 /// [[omp::for(...)]] for (...) { ... }
899 class AttributedStmt final
901 private llvm::TrailingObjects<AttributedStmt, const Attr *> {
902 friend class ASTStmtReader;
903 friend TrailingObjects;
906 SourceLocation AttrLoc;
909 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
910 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
911 NumAttrs(Attrs.size()) {
912 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
915 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
916 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
917 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
920 const Attr *const *getAttrArrayPtr() const {
921 return getTrailingObjects<const Attr *>();
923 const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
926 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
927 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
929 // Build an empty attributed statement.
930 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
932 SourceLocation getAttrLoc() const { return AttrLoc; }
933 ArrayRef<const Attr*> getAttrs() const {
934 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs);
937 Stmt *getSubStmt() { return SubStmt; }
938 const Stmt *getSubStmt() const { return SubStmt; }
940 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
941 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
943 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
945 static bool classof(const Stmt *T) {
946 return T->getStmtClass() == AttributedStmtClass;
950 /// IfStmt - This represents an if/then/else.
951 class IfStmt : public Stmt {
952 enum { INIT, VAR, COND, THEN, ELSE, END_EXPR };
953 Stmt* SubExprs[END_EXPR];
955 SourceLocation IfLoc;
956 SourceLocation ElseLoc;
959 IfStmt(const ASTContext &C, SourceLocation IL,
960 bool IsConstexpr, Stmt *init, VarDecl *var, Expr *cond,
961 Stmt *then, SourceLocation EL = SourceLocation(),
962 Stmt *elsev = nullptr);
964 /// Build an empty if/then/else statement
965 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) {}
967 /// Retrieve the variable declared in this "if" statement, if any.
969 /// In the following example, "x" is the condition variable.
971 /// if (int x = foo()) {
972 /// printf("x is %d", x);
975 VarDecl *getConditionVariable() const;
976 void setConditionVariable(const ASTContext &C, VarDecl *V);
978 /// If this IfStmt has a condition variable, return the faux DeclStmt
979 /// associated with the creation of that condition variable.
980 const DeclStmt *getConditionVariableDeclStmt() const {
981 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
984 Stmt *getInit() { return SubExprs[INIT]; }
985 const Stmt *getInit() const { return SubExprs[INIT]; }
986 void setInit(Stmt *S) { SubExprs[INIT] = S; }
987 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
988 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
989 const Stmt *getThen() const { return SubExprs[THEN]; }
990 void setThen(Stmt *S) { SubExprs[THEN] = S; }
991 const Stmt *getElse() const { return SubExprs[ELSE]; }
992 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
994 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
995 Stmt *getThen() { return SubExprs[THEN]; }
996 Stmt *getElse() { return SubExprs[ELSE]; }
998 SourceLocation getIfLoc() const { return IfLoc; }
999 void setIfLoc(SourceLocation L) { IfLoc = L; }
1000 SourceLocation getElseLoc() const { return ElseLoc; }
1001 void setElseLoc(SourceLocation L) { ElseLoc = L; }
1003 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
1004 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
1006 bool isObjCAvailabilityCheck() const;
1008 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
1010 SourceLocation getLocEnd() const LLVM_READONLY {
1012 return SubExprs[ELSE]->getLocEnd();
1014 return SubExprs[THEN]->getLocEnd();
1017 // Iterators over subexpressions. The iterators will include iterating
1018 // over the initialization expression referenced by the condition variable.
1019 child_range children() {
1020 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1023 static bool classof(const Stmt *T) {
1024 return T->getStmtClass() == IfStmtClass;
1028 /// SwitchStmt - This represents a 'switch' stmt.
1029 class SwitchStmt : public Stmt {
1030 SourceLocation SwitchLoc;
1031 enum { INIT, VAR, COND, BODY, END_EXPR };
1032 Stmt* SubExprs[END_EXPR];
1034 // This points to a linked list of case and default statements and, if the
1035 // SwitchStmt is a switch on an enum value, records whether all the enum
1036 // values were covered by CaseStmts. The coverage information value is meant
1037 // to be a hint for possible clients.
1038 llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase;
1041 SwitchStmt(const ASTContext &C, Stmt *Init, VarDecl *Var, Expr *cond);
1043 /// Build a empty switch statement.
1044 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) {}
1046 /// Retrieve the variable declared in this "switch" statement, if any.
1048 /// In the following example, "x" is the condition variable.
1050 /// switch (int x = foo()) {
1055 VarDecl *getConditionVariable() const;
1056 void setConditionVariable(const ASTContext &C, VarDecl *V);
1058 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
1059 /// associated with the creation of that condition variable.
1060 const DeclStmt *getConditionVariableDeclStmt() const {
1061 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1064 Stmt *getInit() { return SubExprs[INIT]; }
1065 const Stmt *getInit() const { return SubExprs[INIT]; }
1066 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1067 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1068 const Stmt *getBody() const { return SubExprs[BODY]; }
1069 const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); }
1071 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1072 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
1073 Stmt *getBody() { return SubExprs[BODY]; }
1074 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1075 SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); }
1077 /// Set the case list for this switch statement.
1078 void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); }
1080 SourceLocation getSwitchLoc() const { return SwitchLoc; }
1081 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
1083 void setBody(Stmt *S, SourceLocation SL) {
1088 void addSwitchCase(SwitchCase *SC) {
1089 assert(!SC->getNextSwitchCase()
1090 && "case/default already added to a switch");
1091 SC->setNextSwitchCase(FirstCase.getPointer());
1092 FirstCase.setPointer(SC);
1095 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1096 /// switch over an enum value then all cases have been explicitly covered.
1097 void setAllEnumCasesCovered() { FirstCase.setInt(true); }
1099 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1100 /// have been explicitly covered.
1101 bool isAllEnumCasesCovered() const { return FirstCase.getInt(); }
1103 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1105 SourceLocation getLocEnd() const LLVM_READONLY {
1106 return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
1110 child_range children() {
1111 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1114 static bool classof(const Stmt *T) {
1115 return T->getStmtClass() == SwitchStmtClass;
1119 /// WhileStmt - This represents a 'while' stmt.
1120 class WhileStmt : public Stmt {
1121 SourceLocation WhileLoc;
1122 enum { VAR, COND, BODY, END_EXPR };
1123 Stmt* SubExprs[END_EXPR];
1126 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1129 /// Build an empty while statement.
1130 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) {}
1132 /// Retrieve the variable declared in this "while" statement, if any.
1134 /// In the following example, "x" is the condition variable.
1136 /// while (int x = random()) {
1140 VarDecl *getConditionVariable() const;
1141 void setConditionVariable(const ASTContext &C, VarDecl *V);
1143 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1144 /// associated with the creation of that condition variable.
1145 const DeclStmt *getConditionVariableDeclStmt() const {
1146 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1149 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1150 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1151 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1152 Stmt *getBody() { return SubExprs[BODY]; }
1153 const Stmt *getBody() const { return SubExprs[BODY]; }
1154 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1156 SourceLocation getWhileLoc() const { return WhileLoc; }
1157 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1159 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1161 SourceLocation getLocEnd() const LLVM_READONLY {
1162 return SubExprs[BODY]->getLocEnd();
1165 static bool classof(const Stmt *T) {
1166 return T->getStmtClass() == WhileStmtClass;
1170 child_range children() {
1171 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1175 /// DoStmt - This represents a 'do/while' stmt.
1176 class DoStmt : public Stmt {
1177 SourceLocation DoLoc;
1178 enum { BODY, COND, END_EXPR };
1179 Stmt* SubExprs[END_EXPR];
1180 SourceLocation WhileLoc;
1181 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1184 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1186 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1187 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1188 SubExprs[BODY] = body;
1191 /// Build an empty do-while statement.
1192 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
1194 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1195 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1196 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1197 Stmt *getBody() { return SubExprs[BODY]; }
1198 const Stmt *getBody() const { return SubExprs[BODY]; }
1199 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1201 SourceLocation getDoLoc() const { return DoLoc; }
1202 void setDoLoc(SourceLocation L) { DoLoc = L; }
1203 SourceLocation getWhileLoc() const { return WhileLoc; }
1204 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1206 SourceLocation getRParenLoc() const { return RParenLoc; }
1207 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1209 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1210 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1212 static bool classof(const Stmt *T) {
1213 return T->getStmtClass() == DoStmtClass;
1217 child_range children() {
1218 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1222 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1223 /// the init/cond/inc parts of the ForStmt will be null if they were not
1224 /// specified in the source.
1225 class ForStmt : public Stmt {
1226 SourceLocation ForLoc;
1227 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1228 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1229 SourceLocation LParenLoc, RParenLoc;
1232 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1233 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1236 /// Build an empty for statement.
1237 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
1239 Stmt *getInit() { return SubExprs[INIT]; }
1241 /// Retrieve the variable declared in this "for" statement, if any.
1243 /// In the following example, "y" is the condition variable.
1245 /// for (int x = random(); int y = mangle(x); ++x) {
1249 VarDecl *getConditionVariable() const;
1250 void setConditionVariable(const ASTContext &C, VarDecl *V);
1252 /// If this ForStmt has a condition variable, return the faux DeclStmt
1253 /// associated with the creation of that condition variable.
1254 const DeclStmt *getConditionVariableDeclStmt() const {
1255 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1258 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1259 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1260 Stmt *getBody() { return SubExprs[BODY]; }
1262 const Stmt *getInit() const { return SubExprs[INIT]; }
1263 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1264 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1265 const Stmt *getBody() const { return SubExprs[BODY]; }
1267 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1268 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1269 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1270 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1272 SourceLocation getForLoc() const { return ForLoc; }
1273 void setForLoc(SourceLocation L) { ForLoc = L; }
1274 SourceLocation getLParenLoc() const { return LParenLoc; }
1275 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1276 SourceLocation getRParenLoc() const { return RParenLoc; }
1277 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1279 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1281 SourceLocation getLocEnd() const LLVM_READONLY {
1282 return SubExprs[BODY]->getLocEnd();
1285 static bool classof(const Stmt *T) {
1286 return T->getStmtClass() == ForStmtClass;
1290 child_range children() {
1291 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1295 /// GotoStmt - This represents a direct goto.
1296 class GotoStmt : public Stmt {
1298 SourceLocation GotoLoc;
1299 SourceLocation LabelLoc;
1302 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1303 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1305 /// Build an empty goto statement.
1306 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
1308 LabelDecl *getLabel() const { return Label; }
1309 void setLabel(LabelDecl *D) { Label = D; }
1311 SourceLocation getGotoLoc() const { return GotoLoc; }
1312 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1313 SourceLocation getLabelLoc() const { return LabelLoc; }
1314 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1316 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1317 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1319 static bool classof(const Stmt *T) {
1320 return T->getStmtClass() == GotoStmtClass;
1324 child_range children() {
1325 return child_range(child_iterator(), child_iterator());
1329 /// IndirectGotoStmt - This represents an indirect goto.
1330 class IndirectGotoStmt : public Stmt {
1331 SourceLocation GotoLoc;
1332 SourceLocation StarLoc;
1336 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1338 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1339 Target((Stmt*)target) {}
1341 /// Build an empty indirect goto statement.
1342 explicit IndirectGotoStmt(EmptyShell Empty)
1343 : Stmt(IndirectGotoStmtClass, Empty) {}
1345 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1346 SourceLocation getGotoLoc() const { return GotoLoc; }
1347 void setStarLoc(SourceLocation L) { StarLoc = L; }
1348 SourceLocation getStarLoc() const { return StarLoc; }
1350 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1351 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1352 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1354 /// getConstantTarget - Returns the fixed target of this indirect
1355 /// goto, if one exists.
1356 LabelDecl *getConstantTarget();
1357 const LabelDecl *getConstantTarget() const {
1358 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1361 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1362 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1364 static bool classof(const Stmt *T) {
1365 return T->getStmtClass() == IndirectGotoStmtClass;
1369 child_range children() { return child_range(&Target, &Target+1); }
1372 /// ContinueStmt - This represents a continue.
1373 class ContinueStmt : public Stmt {
1374 SourceLocation ContinueLoc;
1377 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1379 /// Build an empty continue statement.
1380 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
1382 SourceLocation getContinueLoc() const { return ContinueLoc; }
1383 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1385 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1386 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1388 static bool classof(const Stmt *T) {
1389 return T->getStmtClass() == ContinueStmtClass;
1393 child_range children() {
1394 return child_range(child_iterator(), child_iterator());
1398 /// BreakStmt - This represents a break.
1399 class BreakStmt : public Stmt {
1400 SourceLocation BreakLoc;
1403 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {
1404 static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation),
1405 "BreakStmt too large");
1408 /// Build an empty break statement.
1409 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
1411 SourceLocation getBreakLoc() const { return BreakLoc; }
1412 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1414 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1415 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1417 static bool classof(const Stmt *T) {
1418 return T->getStmtClass() == BreakStmtClass;
1422 child_range children() {
1423 return child_range(child_iterator(), child_iterator());
1427 /// ReturnStmt - This represents a return, optionally of an expression:
1431 /// Note that GCC allows return with no argument in a function declared to
1432 /// return a value, and it allows returning a value in functions declared to
1433 /// return void. We explicitly model this in the AST, which means you can't
1434 /// depend on the return type of the function and the presence of an argument.
1435 class ReturnStmt : public Stmt {
1436 SourceLocation RetLoc;
1438 const VarDecl *NRVOCandidate;
1441 explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {}
1443 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1444 : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E),
1445 NRVOCandidate(NRVOCandidate) {}
1447 /// Build an empty return expression.
1448 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) {}
1450 const Expr *getRetValue() const;
1451 Expr *getRetValue();
1452 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1454 SourceLocation getReturnLoc() const { return RetLoc; }
1455 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1457 /// Retrieve the variable that might be used for the named return
1458 /// value optimization.
1460 /// The optimization itself can only be performed if the variable is
1461 /// also marked as an NRVO object.
1462 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1463 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1465 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1467 SourceLocation getLocEnd() const LLVM_READONLY {
1468 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1471 static bool classof(const Stmt *T) {
1472 return T->getStmtClass() == ReturnStmtClass;
1476 child_range children() {
1477 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1478 return child_range(child_iterator(), child_iterator());
1482 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1483 class AsmStmt : public Stmt {
1485 friend class ASTStmtReader;
1487 SourceLocation AsmLoc;
1489 /// True if the assembly statement does not have any input or output
1493 /// If true, treat this inline assembly as having side effects.
1494 /// This assembly statement should not be optimized, deleted or moved.
1497 unsigned NumOutputs;
1499 unsigned NumClobbers;
1501 Stmt **Exprs = nullptr;
1503 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1504 unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
1505 : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1506 NumOutputs(numoutputs), NumInputs(numinputs),
1507 NumClobbers(numclobbers) {}
1510 /// Build an empty inline-assembly statement.
1511 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
1513 SourceLocation getAsmLoc() const { return AsmLoc; }
1514 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1516 bool isSimple() const { return IsSimple; }
1517 void setSimple(bool V) { IsSimple = V; }
1519 bool isVolatile() const { return IsVolatile; }
1520 void setVolatile(bool V) { IsVolatile = V; }
1522 SourceLocation getLocStart() const LLVM_READONLY { return {}; }
1523 SourceLocation getLocEnd() const LLVM_READONLY { return {}; }
1525 //===--- Asm String Analysis ---===//
1527 /// Assemble final IR asm string.
1528 std::string generateAsmString(const ASTContext &C) const;
1530 //===--- Output operands ---===//
1532 unsigned getNumOutputs() const { return NumOutputs; }
1534 /// getOutputConstraint - Return the constraint string for the specified
1535 /// output operand. All output constraints are known to be non-empty (either
1537 StringRef getOutputConstraint(unsigned i) const;
1539 /// isOutputPlusConstraint - Return true if the specified output constraint
1540 /// is a "+" constraint (which is both an input and an output) or false if it
1541 /// is an "=" constraint (just an output).
1542 bool isOutputPlusConstraint(unsigned i) const {
1543 return getOutputConstraint(i)[0] == '+';
1546 const Expr *getOutputExpr(unsigned i) const;
1548 /// getNumPlusOperands - Return the number of output operands that have a "+"
1550 unsigned getNumPlusOperands() const;
1552 //===--- Input operands ---===//
1554 unsigned getNumInputs() const { return NumInputs; }
1556 /// getInputConstraint - Return the specified input constraint. Unlike output
1557 /// constraints, these can be empty.
1558 StringRef getInputConstraint(unsigned i) const;
1560 const Expr *getInputExpr(unsigned i) const;
1562 //===--- Other ---===//
1564 unsigned getNumClobbers() const { return NumClobbers; }
1565 StringRef getClobber(unsigned i) const;
1567 static bool classof(const Stmt *T) {
1568 return T->getStmtClass() == GCCAsmStmtClass ||
1569 T->getStmtClass() == MSAsmStmtClass;
1572 // Input expr iterators.
1574 using inputs_iterator = ExprIterator;
1575 using const_inputs_iterator = ConstExprIterator;
1576 using inputs_range = llvm::iterator_range<inputs_iterator>;
1577 using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
1579 inputs_iterator begin_inputs() {
1580 return &Exprs[0] + NumOutputs;
1583 inputs_iterator end_inputs() {
1584 return &Exprs[0] + NumOutputs + NumInputs;
1587 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1589 const_inputs_iterator begin_inputs() const {
1590 return &Exprs[0] + NumOutputs;
1593 const_inputs_iterator end_inputs() const {
1594 return &Exprs[0] + NumOutputs + NumInputs;
1597 inputs_const_range inputs() const {
1598 return inputs_const_range(begin_inputs(), end_inputs());
1601 // Output expr iterators.
1603 using outputs_iterator = ExprIterator;
1604 using const_outputs_iterator = ConstExprIterator;
1605 using outputs_range = llvm::iterator_range<outputs_iterator>;
1606 using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
1608 outputs_iterator begin_outputs() {
1612 outputs_iterator end_outputs() {
1613 return &Exprs[0] + NumOutputs;
1616 outputs_range outputs() {
1617 return outputs_range(begin_outputs(), end_outputs());
1620 const_outputs_iterator begin_outputs() const {
1624 const_outputs_iterator end_outputs() const {
1625 return &Exprs[0] + NumOutputs;
1628 outputs_const_range outputs() const {
1629 return outputs_const_range(begin_outputs(), end_outputs());
1632 child_range children() {
1633 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1637 /// This represents a GCC inline-assembly statement extension.
1638 class GCCAsmStmt : public AsmStmt {
1639 friend class ASTStmtReader;
1641 SourceLocation RParenLoc;
1642 StringLiteral *AsmStr;
1644 // FIXME: If we wanted to, we could allocate all of these in one big array.
1645 StringLiteral **Constraints = nullptr;
1646 StringLiteral **Clobbers = nullptr;
1647 IdentifierInfo **Names = nullptr;
1650 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1651 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1652 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1653 StringLiteral *asmstr, unsigned numclobbers,
1654 StringLiteral **clobbers, SourceLocation rparenloc);
1656 /// Build an empty inline-assembly statement.
1657 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
1659 SourceLocation getRParenLoc() const { return RParenLoc; }
1660 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1662 //===--- Asm String Analysis ---===//
1664 const StringLiteral *getAsmString() const { return AsmStr; }
1665 StringLiteral *getAsmString() { return AsmStr; }
1666 void setAsmString(StringLiteral *E) { AsmStr = E; }
1668 /// AsmStringPiece - this is part of a decomposed asm string specification
1669 /// (for use with the AnalyzeAsmString function below). An asm string is
1670 /// considered to be a concatenation of these parts.
1671 class AsmStringPiece {
1674 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1675 Operand // Operand reference, with optional modifier %c4.
1683 // Source range for operand references.
1684 CharSourceRange Range;
1687 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1688 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
1690 : MyKind(Operand), Str(S), OperandNo(OpNo),
1691 Range(CharSourceRange::getCharRange(Begin, End)) {}
1693 bool isString() const { return MyKind == String; }
1694 bool isOperand() const { return MyKind == Operand; }
1696 const std::string &getString() const { return Str; }
1698 unsigned getOperandNo() const {
1699 assert(isOperand());
1703 CharSourceRange getRange() const {
1704 assert(isOperand() && "Range is currently used only for Operands.");
1708 /// getModifier - Get the modifier for this operand, if present. This
1709 /// returns '\0' if there was no modifier.
1710 char getModifier() const;
1713 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1714 /// it into pieces. If the asm string is erroneous, emit errors and return
1715 /// true, otherwise return false. This handles canonicalization and
1716 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1717 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1718 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1719 const ASTContext &C, unsigned &DiagOffs) const;
1721 /// Assemble final IR asm string.
1722 std::string generateAsmString(const ASTContext &C) const;
1724 //===--- Output operands ---===//
1726 IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; }
1728 StringRef getOutputName(unsigned i) const {
1729 if (IdentifierInfo *II = getOutputIdentifier(i))
1730 return II->getName();
1735 StringRef getOutputConstraint(unsigned i) const;
1737 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1738 return Constraints[i];
1740 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1741 return Constraints[i];
1744 Expr *getOutputExpr(unsigned i);
1746 const Expr *getOutputExpr(unsigned i) const {
1747 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1750 //===--- Input operands ---===//
1752 IdentifierInfo *getInputIdentifier(unsigned i) const {
1753 return Names[i + NumOutputs];
1756 StringRef getInputName(unsigned i) const {
1757 if (IdentifierInfo *II = getInputIdentifier(i))
1758 return II->getName();
1763 StringRef getInputConstraint(unsigned i) const;
1765 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1766 return Constraints[i + NumOutputs];
1768 StringLiteral *getInputConstraintLiteral(unsigned i) {
1769 return Constraints[i + NumOutputs];
1772 Expr *getInputExpr(unsigned i);
1773 void setInputExpr(unsigned i, Expr *E);
1775 const Expr *getInputExpr(unsigned i) const {
1776 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1780 void setOutputsAndInputsAndClobbers(const ASTContext &C,
1781 IdentifierInfo **Names,
1782 StringLiteral **Constraints,
1784 unsigned NumOutputs,
1786 StringLiteral **Clobbers,
1787 unsigned NumClobbers);
1790 //===--- Other ---===//
1792 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1793 /// translate this into a numeric value needed to reference the same operand.
1794 /// This returns -1 if the operand name is invalid.
1795 int getNamedOperand(StringRef SymbolicName) const;
1797 StringRef getClobber(unsigned i) const;
1799 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1800 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1804 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1805 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1807 static bool classof(const Stmt *T) {
1808 return T->getStmtClass() == GCCAsmStmtClass;
1812 /// This represents a Microsoft inline-assembly statement extension.
1813 class MSAsmStmt : public AsmStmt {
1814 friend class ASTStmtReader;
1816 SourceLocation LBraceLoc, EndLoc;
1819 unsigned NumAsmToks = 0;
1821 Token *AsmToks = nullptr;
1822 StringRef *Constraints = nullptr;
1823 StringRef *Clobbers = nullptr;
1826 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1827 SourceLocation lbraceloc, bool issimple, bool isvolatile,
1828 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1829 ArrayRef<StringRef> constraints,
1830 ArrayRef<Expr*> exprs, StringRef asmstr,
1831 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1833 /// Build an empty MS-style inline-assembly statement.
1834 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
1836 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1837 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1838 SourceLocation getEndLoc() const { return EndLoc; }
1839 void setEndLoc(SourceLocation L) { EndLoc = L; }
1841 bool hasBraces() const { return LBraceLoc.isValid(); }
1843 unsigned getNumAsmToks() { return NumAsmToks; }
1844 Token *getAsmToks() { return AsmToks; }
1846 //===--- Asm String Analysis ---===//
1847 StringRef getAsmString() const { return AsmStr; }
1849 /// Assemble final IR asm string.
1850 std::string generateAsmString(const ASTContext &C) const;
1852 //===--- Output operands ---===//
1854 StringRef getOutputConstraint(unsigned i) const {
1855 assert(i < NumOutputs);
1856 return Constraints[i];
1859 Expr *getOutputExpr(unsigned i);
1861 const Expr *getOutputExpr(unsigned i) const {
1862 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1865 //===--- Input operands ---===//
1867 StringRef getInputConstraint(unsigned i) const {
1868 assert(i < NumInputs);
1869 return Constraints[i + NumOutputs];
1872 Expr *getInputExpr(unsigned i);
1873 void setInputExpr(unsigned i, Expr *E);
1875 const Expr *getInputExpr(unsigned i) const {
1876 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1879 //===--- Other ---===//
1881 ArrayRef<StringRef> getAllConstraints() const {
1882 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
1885 ArrayRef<StringRef> getClobbers() const {
1886 return llvm::makeArrayRef(Clobbers, NumClobbers);
1889 ArrayRef<Expr*> getAllExprs() const {
1890 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
1891 NumInputs + NumOutputs);
1894 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1897 void initialize(const ASTContext &C, StringRef AsmString,
1898 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1899 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1902 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1903 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1905 static bool classof(const Stmt *T) {
1906 return T->getStmtClass() == MSAsmStmtClass;
1909 child_range children() {
1910 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1914 class SEHExceptStmt : public Stmt {
1915 friend class ASTReader;
1916 friend class ASTStmtReader;
1921 enum { FILTER_EXPR, BLOCK };
1923 SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
1924 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
1927 static SEHExceptStmt* Create(const ASTContext &C,
1928 SourceLocation ExceptLoc,
1932 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1933 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1935 SourceLocation getExceptLoc() const { return Loc; }
1936 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1938 Expr *getFilterExpr() const {
1939 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1942 CompoundStmt *getBlock() const {
1943 return cast<CompoundStmt>(Children[BLOCK]);
1946 child_range children() {
1947 return child_range(Children, Children+2);
1950 static bool classof(const Stmt *T) {
1951 return T->getStmtClass() == SEHExceptStmtClass;
1955 class SEHFinallyStmt : public Stmt {
1956 friend class ASTReader;
1957 friend class ASTStmtReader;
1962 SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
1963 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
1966 static SEHFinallyStmt* Create(const ASTContext &C,
1967 SourceLocation FinallyLoc,
1970 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1971 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1973 SourceLocation getFinallyLoc() const { return Loc; }
1974 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1976 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1978 child_range children() {
1979 return child_range(&Block,&Block+1);
1982 static bool classof(const Stmt *T) {
1983 return T->getStmtClass() == SEHFinallyStmtClass;
1987 class SEHTryStmt : public Stmt {
1988 friend class ASTReader;
1989 friend class ASTStmtReader;
1992 SourceLocation TryLoc;
1995 enum { TRY = 0, HANDLER = 1 };
1997 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1998 SourceLocation TryLoc,
2002 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
2005 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
2006 SourceLocation TryLoc, Stmt *TryBlock,
2009 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
2010 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
2012 SourceLocation getTryLoc() const { return TryLoc; }
2013 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
2015 bool getIsCXXTry() const { return IsCXXTry; }
2017 CompoundStmt* getTryBlock() const {
2018 return cast<CompoundStmt>(Children[TRY]);
2021 Stmt *getHandler() const { return Children[HANDLER]; }
2023 /// Returns 0 if not defined
2024 SEHExceptStmt *getExceptHandler() const;
2025 SEHFinallyStmt *getFinallyHandler() const;
2027 child_range children() {
2028 return child_range(Children, Children+2);
2031 static bool classof(const Stmt *T) {
2032 return T->getStmtClass() == SEHTryStmtClass;
2036 /// Represents a __leave statement.
2037 class SEHLeaveStmt : public Stmt {
2038 SourceLocation LeaveLoc;
2041 explicit SEHLeaveStmt(SourceLocation LL)
2042 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
2044 /// Build an empty __leave statement.
2045 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
2047 SourceLocation getLeaveLoc() const { return LeaveLoc; }
2048 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
2050 SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; }
2051 SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; }
2053 static bool classof(const Stmt *T) {
2054 return T->getStmtClass() == SEHLeaveStmtClass;
2058 child_range children() {
2059 return child_range(child_iterator(), child_iterator());
2063 /// This captures a statement into a function. For example, the following
2064 /// pragma annotated compound statement can be represented as a CapturedStmt,
2065 /// and this compound statement is the body of an anonymous outlined function.
2067 /// #pragma omp parallel
2072 class CapturedStmt : public Stmt {
2074 /// The different capture forms: by 'this', by reference, capture for
2075 /// variable-length array type etc.
2076 enum VariableCaptureKind {
2083 /// Describes the capture of either a variable, or 'this', or
2084 /// variable-length array type.
2086 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2090 friend class ASTStmtReader;
2092 /// Create a new capture.
2094 /// \param Loc The source location associated with this capture.
2096 /// \param Kind The kind of capture (this, ByRef, ...).
2098 /// \param Var The variable being captured, or null if capturing this.
2099 Capture(SourceLocation Loc, VariableCaptureKind Kind,
2100 VarDecl *Var = nullptr);
2102 /// Determine the kind of capture.
2103 VariableCaptureKind getCaptureKind() const;
2105 /// Retrieve the source location at which the variable or 'this' was
2107 SourceLocation getLocation() const { return Loc; }
2109 /// Determine whether this capture handles the C++ 'this' pointer.
2110 bool capturesThis() const { return getCaptureKind() == VCK_This; }
2112 /// Determine whether this capture handles a variable (by reference).
2113 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2115 /// Determine whether this capture handles a variable by copy.
2116 bool capturesVariableByCopy() const {
2117 return getCaptureKind() == VCK_ByCopy;
2120 /// Determine whether this capture handles a variable-length array
2122 bool capturesVariableArrayType() const {
2123 return getCaptureKind() == VCK_VLAType;
2126 /// Retrieve the declaration of the variable being captured.
2128 /// This operation is only valid if this capture captures a variable.
2129 VarDecl *getCapturedVar() const;
2133 /// The number of variable captured, including 'this'.
2134 unsigned NumCaptures;
2136 /// The pointer part is the implicit the outlined function and the
2137 /// int part is the captured region kind, 'CR_Default' etc.
2138 llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind;
2140 /// The record for captured variables, a RecordDecl or CXXRecordDecl.
2141 RecordDecl *TheRecordDecl = nullptr;
2143 /// Construct a captured statement.
2144 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2145 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2147 /// Construct an empty captured statement.
2148 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2150 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
2152 Stmt *const *getStoredStmts() const {
2153 return reinterpret_cast<Stmt *const *>(this + 1);
2156 Capture *getStoredCaptures() const;
2158 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2161 friend class ASTStmtReader;
2163 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2164 CapturedRegionKind Kind,
2165 ArrayRef<Capture> Captures,
2166 ArrayRef<Expr *> CaptureInits,
2167 CapturedDecl *CD, RecordDecl *RD);
2169 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2170 unsigned NumCaptures);
2172 /// Retrieve the statement being captured.
2173 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2174 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
2176 /// Retrieve the outlined function declaration.
2177 CapturedDecl *getCapturedDecl();
2178 const CapturedDecl *getCapturedDecl() const;
2180 /// Set the outlined function declaration.
2181 void setCapturedDecl(CapturedDecl *D);
2183 /// Retrieve the captured region kind.
2184 CapturedRegionKind getCapturedRegionKind() const;
2186 /// Set the captured region kind.
2187 void setCapturedRegionKind(CapturedRegionKind Kind);
2189 /// Retrieve the record declaration for captured variables.
2190 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2192 /// Set the record declaration for captured variables.
2193 void setCapturedRecordDecl(RecordDecl *D) {
2194 assert(D && "null RecordDecl");
2198 /// True if this variable has been captured.
2199 bool capturesVariable(const VarDecl *Var) const;
2201 /// An iterator that walks over the captures.
2202 using capture_iterator = Capture *;
2203 using const_capture_iterator = const Capture *;
2204 using capture_range = llvm::iterator_range<capture_iterator>;
2205 using capture_const_range = llvm::iterator_range<const_capture_iterator>;
2207 capture_range captures() {
2208 return capture_range(capture_begin(), capture_end());
2210 capture_const_range captures() const {
2211 return capture_const_range(capture_begin(), capture_end());
2214 /// Retrieve an iterator pointing to the first capture.
2215 capture_iterator capture_begin() { return getStoredCaptures(); }
2216 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2218 /// Retrieve an iterator pointing past the end of the sequence of
2220 capture_iterator capture_end() const {
2221 return getStoredCaptures() + NumCaptures;
2224 /// Retrieve the number of captures, including 'this'.
2225 unsigned capture_size() const { return NumCaptures; }
2227 /// Iterator that walks over the capture initialization arguments.
2228 using capture_init_iterator = Expr **;
2229 using capture_init_range = llvm::iterator_range<capture_init_iterator>;
2231 /// Const iterator that walks over the capture initialization
2233 using const_capture_init_iterator = Expr *const *;
2234 using const_capture_init_range =
2235 llvm::iterator_range<const_capture_init_iterator>;
2237 capture_init_range capture_inits() {
2238 return capture_init_range(capture_init_begin(), capture_init_end());
2241 const_capture_init_range capture_inits() const {
2242 return const_capture_init_range(capture_init_begin(), capture_init_end());
2245 /// Retrieve the first initialization argument.
2246 capture_init_iterator capture_init_begin() {
2247 return reinterpret_cast<Expr **>(getStoredStmts());
2250 const_capture_init_iterator capture_init_begin() const {
2251 return reinterpret_cast<Expr *const *>(getStoredStmts());
2254 /// Retrieve the iterator pointing one past the last initialization
2256 capture_init_iterator capture_init_end() {
2257 return capture_init_begin() + NumCaptures;
2260 const_capture_init_iterator capture_init_end() const {
2261 return capture_init_begin() + NumCaptures;
2264 SourceLocation getLocStart() const LLVM_READONLY {
2265 return getCapturedStmt()->getLocStart();
2268 SourceLocation getLocEnd() const LLVM_READONLY {
2269 return getCapturedStmt()->getLocEnd();
2272 SourceRange getSourceRange() const LLVM_READONLY {
2273 return getCapturedStmt()->getSourceRange();
2276 static bool classof(const Stmt *T) {
2277 return T->getStmtClass() == CapturedStmtClass;
2280 child_range children();
2283 } // namespace clang
2285 #endif // LLVM_CLANG_AST_STMT_H