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 /// \brief 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;
202 unsigned : NumExprBits;
205 unsigned BasePathSize : 32 - 6 - NumExprBits;
208 class CallExprBitfields {
209 friend class CallExpr;
211 unsigned : NumExprBits;
213 unsigned NumPreArgs : 1;
216 class ExprWithCleanupsBitfields {
217 friend class ASTStmtReader; // deserialization
218 friend class ExprWithCleanups;
220 unsigned : NumExprBits;
222 // When false, it must not have side effects.
223 unsigned CleanupsHaveSideEffects : 1;
225 unsigned NumObjects : 32 - 1 - NumExprBits;
228 class PseudoObjectExprBitfields {
229 friend class ASTStmtReader; // deserialization
230 friend class PseudoObjectExpr;
232 unsigned : NumExprBits;
234 // These don't need to be particularly wide, because they're
235 // strictly limited by the forms of expressions we permit.
236 unsigned NumSubExprs : 8;
237 unsigned ResultIndex : 32 - 8 - NumExprBits;
240 class ObjCIndirectCopyRestoreExprBitfields {
241 friend class ObjCIndirectCopyRestoreExpr;
243 unsigned : NumExprBits;
245 unsigned ShouldCopy : 1;
248 class InitListExprBitfields {
249 friend class InitListExpr;
251 unsigned : NumExprBits;
253 /// Whether this initializer list originally had a GNU array-range
254 /// designator in it. This is a temporary marker used by CodeGen.
255 unsigned HadArrayRangeDesignator : 1;
258 class TypeTraitExprBitfields {
259 friend class ASTStmtReader;
260 friend class ASTStmtWriter;
261 friend class TypeTraitExpr;
263 unsigned : NumExprBits;
265 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
268 /// \brief If this expression is not value-dependent, this indicates whether
269 /// the trait evaluated true or false.
272 /// \brief The number of arguments to this type trait.
273 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
276 class CoawaitExprBitfields {
277 friend class CoawaitExpr;
279 unsigned : NumExprBits;
281 unsigned IsImplicit : 1;
285 StmtBitfields StmtBits;
286 CompoundStmtBitfields CompoundStmtBits;
287 IfStmtBitfields IfStmtBits;
288 ExprBitfields ExprBits;
289 CharacterLiteralBitfields CharacterLiteralBits;
290 FloatingLiteralBitfields FloatingLiteralBits;
291 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
292 DeclRefExprBitfields DeclRefExprBits;
293 CastExprBitfields CastExprBits;
294 CallExprBitfields CallExprBits;
295 ExprWithCleanupsBitfields ExprWithCleanupsBits;
296 PseudoObjectExprBitfields PseudoObjectExprBits;
297 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
298 InitListExprBitfields InitListExprBits;
299 TypeTraitExprBitfields TypeTraitExprBits;
300 CoawaitExprBitfields CoawaitBits;
304 // Only allow allocation of Stmts using the allocator in ASTContext
305 // or by doing a placement new.
306 void* operator new(size_t bytes, const ASTContext& C,
307 unsigned alignment = 8);
309 void* operator new(size_t bytes, const ASTContext* C,
310 unsigned alignment = 8) {
311 return operator new(bytes, *C, alignment);
314 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
316 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
317 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
318 void operator delete(void *, size_t) noexcept {}
319 void operator delete(void *, void *) noexcept {}
322 /// \brief A placeholder type used to construct an empty shell of a
323 /// type, that will be filled in later (e.g., by some
324 /// de-serialization).
325 struct EmptyShell {};
328 /// Iterator for iterating over Stmt * arrays that contain only Expr *
330 /// This is needed because AST nodes use Stmt* arrays to store
331 /// references to children (to be compatible with StmtIterator).
333 : llvm::iterator_adaptor_base<ExprIterator, Stmt **,
334 std::random_access_iterator_tag, Expr *> {
335 ExprIterator() : iterator_adaptor_base(nullptr) {}
336 ExprIterator(Stmt **I) : iterator_adaptor_base(I) {}
338 reference operator*() const {
339 assert((*I)->getStmtClass() >= firstExprConstant &&
340 (*I)->getStmtClass() <= lastExprConstant);
341 return *reinterpret_cast<Expr **>(I);
345 /// Const iterator for iterating over Stmt * arrays that contain only Expr *
346 struct ConstExprIterator
347 : llvm::iterator_adaptor_base<ConstExprIterator, const Stmt *const *,
348 std::random_access_iterator_tag,
350 ConstExprIterator() : iterator_adaptor_base(nullptr) {}
351 ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {}
353 reference operator*() const {
354 assert((*I)->getStmtClass() >= firstExprConstant &&
355 (*I)->getStmtClass() <= lastExprConstant);
356 return *reinterpret_cast<const Expr *const *>(I);
361 /// \brief Whether statistic collection is enabled.
362 static bool StatisticsEnabled;
365 /// \brief Construct an empty statement.
366 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
370 static_assert(sizeof(*this) == sizeof(void *),
371 "changing bitfields changed sizeof(Stmt)");
372 static_assert(sizeof(*this) % alignof(void *) == 0,
373 "Insufficient alignment!");
374 StmtBits.sClass = SC;
375 if (StatisticsEnabled) Stmt::addStmtClass(SC);
378 StmtClass getStmtClass() const {
379 return static_cast<StmtClass>(StmtBits.sClass);
382 const char *getStmtClassName() const;
384 /// SourceLocation tokens are not useful in isolation - they are low level
385 /// value objects created/interpreted by SourceManager. We assume AST
386 /// clients will have a pointer to the respective SourceManager.
387 SourceRange getSourceRange() const LLVM_READONLY;
388 SourceLocation getLocStart() const LLVM_READONLY;
389 SourceLocation getLocEnd() const LLVM_READONLY;
391 // global temp stats (until we have a per-module visitor)
392 static void addStmtClass(const StmtClass s);
393 static void EnableStatistics();
394 static void PrintStats();
396 /// \brief Dumps the specified AST fragment and all subtrees to
399 void dump(SourceManager &SM) const;
400 void dump(raw_ostream &OS, SourceManager &SM) const;
401 void dump(raw_ostream &OS) const;
403 /// dumpColor - same as dump(), but forces color highlighting.
404 void dumpColor() const;
406 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
407 /// back to its original source language syntax.
408 void dumpPretty(const ASTContext &Context) const;
409 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
410 const PrintingPolicy &Policy, unsigned Indentation = 0,
411 const ASTContext *Context = nullptr) const;
413 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
414 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
415 void viewAST() const;
417 /// Skip past any implicit AST nodes which might surround this
418 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
419 Stmt *IgnoreImplicit();
420 const Stmt *IgnoreImplicit() const {
421 return const_cast<Stmt *>(this)->IgnoreImplicit();
424 /// \brief Skip no-op (attributed, compound) container stmts and skip captured
425 /// stmt at the top, if \a IgnoreCaptured is true.
426 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
427 const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
428 return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
431 const Stmt *stripLabelLikeStatements() const;
432 Stmt *stripLabelLikeStatements() {
433 return const_cast<Stmt*>(
434 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
437 /// Child Iterators: All subclasses must implement 'children'
438 /// to permit easy iteration over the substatements/subexpessions of an
439 /// AST node. This permits easy iteration over all nodes in the AST.
440 using child_iterator = StmtIterator;
441 using const_child_iterator = ConstStmtIterator;
443 using child_range = llvm::iterator_range<child_iterator>;
444 using const_child_range = llvm::iterator_range<const_child_iterator>;
446 child_range children();
447 const_child_range children() const {
448 auto Children = const_cast<Stmt *>(this)->children();
449 return const_child_range(Children.begin(), Children.end());
452 child_iterator child_begin() { return children().begin(); }
453 child_iterator child_end() { return children().end(); }
455 const_child_iterator child_begin() const { return children().begin(); }
456 const_child_iterator child_end() const { return children().end(); }
458 /// \brief Produce a unique representation of the given statement.
460 /// \param ID once the profiling operation is complete, will contain
461 /// the unique representation of the given statement.
463 /// \param Context the AST context in which the statement resides
465 /// \param Canonical whether the profile should be based on the canonical
466 /// representation of this statement (e.g., where non-type template
467 /// parameters are identified by index/level rather than their
468 /// declaration pointers) or the exact representation of the statement as
469 /// written in the source.
470 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
471 bool Canonical) const;
473 /// \brief Calculate a unique representation for a statement that is
474 /// stable across compiler invocations.
476 /// \param ID profile information will be stored in ID.
478 /// \param Hash an ODRHash object which will be called where pointers would
479 /// have been used in the Profile function.
480 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
483 /// DeclStmt - Adaptor class for mixing declarations with statements and
484 /// expressions. For example, CompoundStmt mixes statements, expressions
485 /// and declarations (variables, types). Another example is ForStmt, where
486 /// the first statement can be an expression or a declaration.
487 class DeclStmt : public Stmt {
489 SourceLocation StartLoc, EndLoc;
492 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
493 : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
495 /// \brief Build an empty declaration statement.
496 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
498 /// isSingleDecl - This method returns true if this DeclStmt refers
499 /// to a single Decl.
500 bool isSingleDecl() const {
501 return DG.isSingleDecl();
504 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
505 Decl *getSingleDecl() { return DG.getSingleDecl(); }
507 const DeclGroupRef getDeclGroup() const { return DG; }
508 DeclGroupRef getDeclGroup() { return DG; }
509 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
511 SourceLocation getStartLoc() const { return StartLoc; }
512 void setStartLoc(SourceLocation L) { StartLoc = L; }
513 SourceLocation getEndLoc() const { return EndLoc; }
514 void setEndLoc(SourceLocation L) { EndLoc = L; }
516 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
517 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
519 static bool classof(const Stmt *T) {
520 return T->getStmtClass() == DeclStmtClass;
523 // Iterators over subexpressions.
524 child_range children() {
525 return child_range(child_iterator(DG.begin(), DG.end()),
526 child_iterator(DG.end(), DG.end()));
529 using decl_iterator = DeclGroupRef::iterator;
530 using const_decl_iterator = DeclGroupRef::const_iterator;
531 using decl_range = llvm::iterator_range<decl_iterator>;
532 using decl_const_range = llvm::iterator_range<const_decl_iterator>;
534 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
535 decl_const_range decls() const {
536 return decl_const_range(decl_begin(), decl_end());
538 decl_iterator decl_begin() { return DG.begin(); }
539 decl_iterator decl_end() { return DG.end(); }
540 const_decl_iterator decl_begin() const { return DG.begin(); }
541 const_decl_iterator decl_end() const { return DG.end(); }
543 using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
545 reverse_decl_iterator decl_rbegin() {
546 return reverse_decl_iterator(decl_end());
549 reverse_decl_iterator decl_rend() {
550 return reverse_decl_iterator(decl_begin());
554 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
556 class NullStmt : public Stmt {
557 SourceLocation SemiLoc;
559 /// \brief True if the null statement was preceded by an empty macro, e.g:
564 bool HasLeadingEmptyMacro = false;
567 friend class ASTStmtReader;
568 friend class ASTStmtWriter;
570 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
571 : Stmt(NullStmtClass), SemiLoc(L),
572 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
574 /// \brief Build an empty null statement.
575 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
577 SourceLocation getSemiLoc() const { return SemiLoc; }
578 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
580 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
582 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
583 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
585 static bool classof(const Stmt *T) {
586 return T->getStmtClass() == NullStmtClass;
589 child_range children() {
590 return child_range(child_iterator(), child_iterator());
594 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
595 class CompoundStmt final : public Stmt,
596 private llvm::TrailingObjects<CompoundStmt, Stmt *> {
597 friend class ASTStmtReader;
598 friend TrailingObjects;
600 SourceLocation LBraceLoc, RBraceLoc;
602 CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
603 explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
605 void setStmts(ArrayRef<Stmt *> Stmts);
608 static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
609 SourceLocation LB, SourceLocation RB);
611 // \brief Build an empty compound statement with a location.
612 explicit CompoundStmt(SourceLocation Loc)
613 : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(Loc) {
614 CompoundStmtBits.NumStmts = 0;
617 // \brief Build an empty compound statement.
618 static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
620 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
621 unsigned size() const { return CompoundStmtBits.NumStmts; }
623 using body_iterator = Stmt **;
624 using body_range = llvm::iterator_range<body_iterator>;
626 body_range body() { return body_range(body_begin(), body_end()); }
627 body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
628 body_iterator body_end() { return body_begin() + size(); }
629 Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
631 return !body_empty() ? body_begin()[size() - 1] : nullptr;
634 void setLastStmt(Stmt *S) {
635 assert(!body_empty() && "setLastStmt");
636 body_begin()[size() - 1] = S;
639 using const_body_iterator = Stmt* const *;
640 using body_const_range = llvm::iterator_range<const_body_iterator>;
642 body_const_range body() const {
643 return body_const_range(body_begin(), body_end());
646 const_body_iterator body_begin() const {
647 return getTrailingObjects<Stmt *>();
649 const_body_iterator body_end() const { return body_begin() + size(); }
651 const Stmt *body_front() const {
652 return !body_empty() ? body_begin()[0] : nullptr;
655 const Stmt *body_back() const {
656 return !body_empty() ? body_begin()[size() - 1] : nullptr;
659 using reverse_body_iterator = std::reverse_iterator<body_iterator>;
661 reverse_body_iterator body_rbegin() {
662 return reverse_body_iterator(body_end());
665 reverse_body_iterator body_rend() {
666 return reverse_body_iterator(body_begin());
669 using const_reverse_body_iterator =
670 std::reverse_iterator<const_body_iterator>;
672 const_reverse_body_iterator body_rbegin() const {
673 return const_reverse_body_iterator(body_end());
676 const_reverse_body_iterator body_rend() const {
677 return const_reverse_body_iterator(body_begin());
680 SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; }
681 SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; }
683 SourceLocation getLBracLoc() const { return LBraceLoc; }
684 SourceLocation getRBracLoc() const { return RBraceLoc; }
686 static bool classof(const Stmt *T) {
687 return T->getStmtClass() == CompoundStmtClass;
691 child_range children() { return child_range(body_begin(), body_end()); }
693 const_child_range children() const {
694 return const_child_range(body_begin(), body_end());
698 // SwitchCase is the base class for CaseStmt and DefaultStmt,
699 class SwitchCase : public Stmt {
701 // A pointer to the following CaseStmt or DefaultStmt class,
702 // used by SwitchStmt.
703 SwitchCase *NextSwitchCase = nullptr;
704 SourceLocation KeywordLoc;
705 SourceLocation ColonLoc;
707 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
708 : Stmt(SC), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {}
710 SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
713 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
715 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
717 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
719 SourceLocation getKeywordLoc() const { return KeywordLoc; }
720 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
721 SourceLocation getColonLoc() const { return ColonLoc; }
722 void setColonLoc(SourceLocation L) { ColonLoc = L; }
725 const Stmt *getSubStmt() const {
726 return const_cast<SwitchCase*>(this)->getSubStmt();
729 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
730 SourceLocation getLocEnd() const LLVM_READONLY;
732 static bool classof(const Stmt *T) {
733 return T->getStmtClass() == CaseStmtClass ||
734 T->getStmtClass() == DefaultStmtClass;
738 class CaseStmt : public SwitchCase {
739 SourceLocation EllipsisLoc;
740 enum { LHS, RHS, SUBSTMT, END_EXPR };
741 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
742 // GNU "case 1 ... 4" extension
745 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
746 SourceLocation ellipsisLoc, SourceLocation colonLoc)
747 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
748 SubExprs[SUBSTMT] = nullptr;
749 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
750 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
751 EllipsisLoc = ellipsisLoc;
754 /// \brief Build an empty switch case statement.
755 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) {}
757 SourceLocation getCaseLoc() const { return KeywordLoc; }
758 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
759 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
760 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
761 SourceLocation getColonLoc() const { return ColonLoc; }
762 void setColonLoc(SourceLocation L) { ColonLoc = L; }
764 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
765 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
766 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
768 const Expr *getLHS() const {
769 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
772 const Expr *getRHS() const {
773 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
776 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
778 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
779 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
780 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
782 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
784 SourceLocation getLocEnd() const LLVM_READONLY {
785 // Handle deeply nested case statements with iteration instead of recursion.
786 const CaseStmt *CS = this;
787 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
790 return CS->getSubStmt()->getLocEnd();
793 static bool classof(const Stmt *T) {
794 return T->getStmtClass() == CaseStmtClass;
798 child_range children() {
799 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
803 class DefaultStmt : public SwitchCase {
807 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
808 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
810 /// \brief Build an empty default statement.
811 explicit DefaultStmt(EmptyShell Empty)
812 : SwitchCase(DefaultStmtClass, Empty) {}
814 Stmt *getSubStmt() { return SubStmt; }
815 const Stmt *getSubStmt() const { return SubStmt; }
816 void setSubStmt(Stmt *S) { SubStmt = S; }
818 SourceLocation getDefaultLoc() const { return KeywordLoc; }
819 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
820 SourceLocation getColonLoc() const { return ColonLoc; }
821 void setColonLoc(SourceLocation L) { ColonLoc = L; }
823 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
824 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
826 static bool classof(const Stmt *T) {
827 return T->getStmtClass() == DefaultStmtClass;
831 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
834 inline SourceLocation SwitchCase::getLocEnd() const {
835 if (const CaseStmt *CS = dyn_cast<CaseStmt>(this))
836 return CS->getLocEnd();
837 return cast<DefaultStmt>(this)->getLocEnd();
840 /// LabelStmt - Represents a label, which has a substatement. For example:
842 class LabelStmt : public Stmt {
843 SourceLocation IdentLoc;
848 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
849 : Stmt(LabelStmtClass), IdentLoc(IL), TheDecl(D), SubStmt(substmt) {
850 static_assert(sizeof(LabelStmt) ==
851 2 * sizeof(SourceLocation) + 2 * sizeof(void *),
852 "LabelStmt too big");
855 // \brief Build an empty label statement.
856 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) {}
858 SourceLocation getIdentLoc() const { return IdentLoc; }
859 LabelDecl *getDecl() const { return TheDecl; }
860 void setDecl(LabelDecl *D) { TheDecl = D; }
861 const char *getName() const;
862 Stmt *getSubStmt() { return SubStmt; }
863 const Stmt *getSubStmt() const { return SubStmt; }
864 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
865 void setSubStmt(Stmt *SS) { SubStmt = SS; }
867 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
868 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
870 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
872 static bool classof(const Stmt *T) {
873 return T->getStmtClass() == LabelStmtClass;
877 /// \brief Represents an attribute applied to a statement.
879 /// Represents an attribute applied to a statement. For example:
880 /// [[omp::for(...)]] for (...) { ... }
881 class AttributedStmt final
883 private llvm::TrailingObjects<AttributedStmt, const Attr *> {
884 friend class ASTStmtReader;
885 friend TrailingObjects;
888 SourceLocation AttrLoc;
891 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
892 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
893 NumAttrs(Attrs.size()) {
894 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
897 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
898 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
899 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
902 const Attr *const *getAttrArrayPtr() const {
903 return getTrailingObjects<const Attr *>();
905 const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
908 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
909 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
911 // \brief Build an empty attributed statement.
912 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
914 SourceLocation getAttrLoc() const { return AttrLoc; }
915 ArrayRef<const Attr*> getAttrs() const {
916 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs);
919 Stmt *getSubStmt() { return SubStmt; }
920 const Stmt *getSubStmt() const { return SubStmt; }
922 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
923 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
925 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
927 static bool classof(const Stmt *T) {
928 return T->getStmtClass() == AttributedStmtClass;
932 /// IfStmt - This represents an if/then/else.
933 class IfStmt : public Stmt {
934 enum { INIT, VAR, COND, THEN, ELSE, END_EXPR };
935 Stmt* SubExprs[END_EXPR];
937 SourceLocation IfLoc;
938 SourceLocation ElseLoc;
941 IfStmt(const ASTContext &C, SourceLocation IL,
942 bool IsConstexpr, Stmt *init, VarDecl *var, Expr *cond,
943 Stmt *then, SourceLocation EL = SourceLocation(),
944 Stmt *elsev = nullptr);
946 /// \brief Build an empty if/then/else statement
947 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) {}
949 /// \brief Retrieve the variable declared in this "if" statement, if any.
951 /// In the following example, "x" is the condition variable.
953 /// if (int x = foo()) {
954 /// printf("x is %d", x);
957 VarDecl *getConditionVariable() const;
958 void setConditionVariable(const ASTContext &C, VarDecl *V);
960 /// If this IfStmt has a condition variable, return the faux DeclStmt
961 /// associated with the creation of that condition variable.
962 const DeclStmt *getConditionVariableDeclStmt() const {
963 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
966 Stmt *getInit() { return SubExprs[INIT]; }
967 const Stmt *getInit() const { return SubExprs[INIT]; }
968 void setInit(Stmt *S) { SubExprs[INIT] = S; }
969 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
970 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
971 const Stmt *getThen() const { return SubExprs[THEN]; }
972 void setThen(Stmt *S) { SubExprs[THEN] = S; }
973 const Stmt *getElse() const { return SubExprs[ELSE]; }
974 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
976 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
977 Stmt *getThen() { return SubExprs[THEN]; }
978 Stmt *getElse() { return SubExprs[ELSE]; }
980 SourceLocation getIfLoc() const { return IfLoc; }
981 void setIfLoc(SourceLocation L) { IfLoc = L; }
982 SourceLocation getElseLoc() const { return ElseLoc; }
983 void setElseLoc(SourceLocation L) { ElseLoc = L; }
985 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
986 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
988 bool isObjCAvailabilityCheck() const;
990 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
992 SourceLocation getLocEnd() const LLVM_READONLY {
994 return SubExprs[ELSE]->getLocEnd();
996 return SubExprs[THEN]->getLocEnd();
999 // Iterators over subexpressions. The iterators will include iterating
1000 // over the initialization expression referenced by the condition variable.
1001 child_range children() {
1002 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1005 static bool classof(const Stmt *T) {
1006 return T->getStmtClass() == IfStmtClass;
1010 /// SwitchStmt - This represents a 'switch' stmt.
1011 class SwitchStmt : public Stmt {
1012 SourceLocation SwitchLoc;
1013 enum { INIT, VAR, COND, BODY, END_EXPR };
1014 Stmt* SubExprs[END_EXPR];
1016 // This points to a linked list of case and default statements and, if the
1017 // SwitchStmt is a switch on an enum value, records whether all the enum
1018 // values were covered by CaseStmts. The coverage information value is meant
1019 // to be a hint for possible clients.
1020 llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase;
1023 SwitchStmt(const ASTContext &C, Stmt *Init, VarDecl *Var, Expr *cond);
1025 /// \brief Build a empty switch statement.
1026 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) {}
1028 /// \brief Retrieve the variable declared in this "switch" statement, if any.
1030 /// In the following example, "x" is the condition variable.
1032 /// switch (int x = foo()) {
1037 VarDecl *getConditionVariable() const;
1038 void setConditionVariable(const ASTContext &C, VarDecl *V);
1040 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
1041 /// associated with the creation of that condition variable.
1042 const DeclStmt *getConditionVariableDeclStmt() const {
1043 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1046 Stmt *getInit() { return SubExprs[INIT]; }
1047 const Stmt *getInit() const { return SubExprs[INIT]; }
1048 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1049 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1050 const Stmt *getBody() const { return SubExprs[BODY]; }
1051 const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); }
1053 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1054 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
1055 Stmt *getBody() { return SubExprs[BODY]; }
1056 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1057 SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); }
1059 /// \brief Set the case list for this switch statement.
1060 void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); }
1062 SourceLocation getSwitchLoc() const { return SwitchLoc; }
1063 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
1065 void setBody(Stmt *S, SourceLocation SL) {
1070 void addSwitchCase(SwitchCase *SC) {
1071 assert(!SC->getNextSwitchCase()
1072 && "case/default already added to a switch");
1073 SC->setNextSwitchCase(FirstCase.getPointer());
1074 FirstCase.setPointer(SC);
1077 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1078 /// switch over an enum value then all cases have been explicitly covered.
1079 void setAllEnumCasesCovered() { FirstCase.setInt(true); }
1081 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1082 /// have been explicitly covered.
1083 bool isAllEnumCasesCovered() const { return FirstCase.getInt(); }
1085 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1087 SourceLocation getLocEnd() const LLVM_READONLY {
1088 return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
1092 child_range children() {
1093 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1096 static bool classof(const Stmt *T) {
1097 return T->getStmtClass() == SwitchStmtClass;
1101 /// WhileStmt - This represents a 'while' stmt.
1102 class WhileStmt : public Stmt {
1103 SourceLocation WhileLoc;
1104 enum { VAR, COND, BODY, END_EXPR };
1105 Stmt* SubExprs[END_EXPR];
1108 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1111 /// \brief Build an empty while statement.
1112 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) {}
1114 /// \brief Retrieve the variable declared in this "while" statement, if any.
1116 /// In the following example, "x" is the condition variable.
1118 /// while (int x = random()) {
1122 VarDecl *getConditionVariable() const;
1123 void setConditionVariable(const ASTContext &C, VarDecl *V);
1125 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1126 /// associated with the creation of that condition variable.
1127 const DeclStmt *getConditionVariableDeclStmt() const {
1128 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1131 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1132 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1133 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1134 Stmt *getBody() { return SubExprs[BODY]; }
1135 const Stmt *getBody() const { return SubExprs[BODY]; }
1136 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1138 SourceLocation getWhileLoc() const { return WhileLoc; }
1139 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1141 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1143 SourceLocation getLocEnd() const LLVM_READONLY {
1144 return SubExprs[BODY]->getLocEnd();
1147 static bool classof(const Stmt *T) {
1148 return T->getStmtClass() == WhileStmtClass;
1152 child_range children() {
1153 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1157 /// DoStmt - This represents a 'do/while' stmt.
1158 class DoStmt : public Stmt {
1159 SourceLocation DoLoc;
1160 enum { BODY, COND, END_EXPR };
1161 Stmt* SubExprs[END_EXPR];
1162 SourceLocation WhileLoc;
1163 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1166 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1168 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1169 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1170 SubExprs[BODY] = body;
1173 /// \brief Build an empty do-while statement.
1174 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
1176 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1177 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1178 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1179 Stmt *getBody() { return SubExprs[BODY]; }
1180 const Stmt *getBody() const { return SubExprs[BODY]; }
1181 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1183 SourceLocation getDoLoc() const { return DoLoc; }
1184 void setDoLoc(SourceLocation L) { DoLoc = L; }
1185 SourceLocation getWhileLoc() const { return WhileLoc; }
1186 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1188 SourceLocation getRParenLoc() const { return RParenLoc; }
1189 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1191 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1192 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1194 static bool classof(const Stmt *T) {
1195 return T->getStmtClass() == DoStmtClass;
1199 child_range children() {
1200 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1204 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1205 /// the init/cond/inc parts of the ForStmt will be null if they were not
1206 /// specified in the source.
1207 class ForStmt : public Stmt {
1208 SourceLocation ForLoc;
1209 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1210 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1211 SourceLocation LParenLoc, RParenLoc;
1214 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1215 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1218 /// \brief Build an empty for statement.
1219 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
1221 Stmt *getInit() { return SubExprs[INIT]; }
1223 /// \brief Retrieve the variable declared in this "for" statement, if any.
1225 /// In the following example, "y" is the condition variable.
1227 /// for (int x = random(); int y = mangle(x); ++x) {
1231 VarDecl *getConditionVariable() const;
1232 void setConditionVariable(const ASTContext &C, VarDecl *V);
1234 /// If this ForStmt has a condition variable, return the faux DeclStmt
1235 /// associated with the creation of that condition variable.
1236 const DeclStmt *getConditionVariableDeclStmt() const {
1237 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1240 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1241 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1242 Stmt *getBody() { return SubExprs[BODY]; }
1244 const Stmt *getInit() const { return SubExprs[INIT]; }
1245 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1246 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1247 const Stmt *getBody() const { return SubExprs[BODY]; }
1249 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1250 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1251 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1252 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1254 SourceLocation getForLoc() const { return ForLoc; }
1255 void setForLoc(SourceLocation L) { ForLoc = L; }
1256 SourceLocation getLParenLoc() const { return LParenLoc; }
1257 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1258 SourceLocation getRParenLoc() const { return RParenLoc; }
1259 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1261 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1263 SourceLocation getLocEnd() const LLVM_READONLY {
1264 return SubExprs[BODY]->getLocEnd();
1267 static bool classof(const Stmt *T) {
1268 return T->getStmtClass() == ForStmtClass;
1272 child_range children() {
1273 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1277 /// GotoStmt - This represents a direct goto.
1278 class GotoStmt : public Stmt {
1280 SourceLocation GotoLoc;
1281 SourceLocation LabelLoc;
1284 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1285 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1287 /// \brief Build an empty goto statement.
1288 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
1290 LabelDecl *getLabel() const { return Label; }
1291 void setLabel(LabelDecl *D) { Label = D; }
1293 SourceLocation getGotoLoc() const { return GotoLoc; }
1294 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1295 SourceLocation getLabelLoc() const { return LabelLoc; }
1296 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1298 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1299 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1301 static bool classof(const Stmt *T) {
1302 return T->getStmtClass() == GotoStmtClass;
1306 child_range children() {
1307 return child_range(child_iterator(), child_iterator());
1311 /// IndirectGotoStmt - This represents an indirect goto.
1312 class IndirectGotoStmt : public Stmt {
1313 SourceLocation GotoLoc;
1314 SourceLocation StarLoc;
1318 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1320 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1321 Target((Stmt*)target) {}
1323 /// \brief Build an empty indirect goto statement.
1324 explicit IndirectGotoStmt(EmptyShell Empty)
1325 : Stmt(IndirectGotoStmtClass, Empty) {}
1327 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1328 SourceLocation getGotoLoc() const { return GotoLoc; }
1329 void setStarLoc(SourceLocation L) { StarLoc = L; }
1330 SourceLocation getStarLoc() const { return StarLoc; }
1332 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1333 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1334 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1336 /// getConstantTarget - Returns the fixed target of this indirect
1337 /// goto, if one exists.
1338 LabelDecl *getConstantTarget();
1339 const LabelDecl *getConstantTarget() const {
1340 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1343 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1344 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1346 static bool classof(const Stmt *T) {
1347 return T->getStmtClass() == IndirectGotoStmtClass;
1351 child_range children() { return child_range(&Target, &Target+1); }
1354 /// ContinueStmt - This represents a continue.
1355 class ContinueStmt : public Stmt {
1356 SourceLocation ContinueLoc;
1359 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1361 /// \brief Build an empty continue statement.
1362 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
1364 SourceLocation getContinueLoc() const { return ContinueLoc; }
1365 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1367 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1368 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1370 static bool classof(const Stmt *T) {
1371 return T->getStmtClass() == ContinueStmtClass;
1375 child_range children() {
1376 return child_range(child_iterator(), child_iterator());
1380 /// BreakStmt - This represents a break.
1381 class BreakStmt : public Stmt {
1382 SourceLocation BreakLoc;
1385 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {
1386 static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation),
1387 "BreakStmt too large");
1390 /// \brief Build an empty break statement.
1391 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
1393 SourceLocation getBreakLoc() const { return BreakLoc; }
1394 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1396 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1397 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1399 static bool classof(const Stmt *T) {
1400 return T->getStmtClass() == BreakStmtClass;
1404 child_range children() {
1405 return child_range(child_iterator(), child_iterator());
1409 /// ReturnStmt - This represents a return, optionally of an expression:
1413 /// Note that GCC allows return with no argument in a function declared to
1414 /// return a value, and it allows returning a value in functions declared to
1415 /// return void. We explicitly model this in the AST, which means you can't
1416 /// depend on the return type of the function and the presence of an argument.
1417 class ReturnStmt : public Stmt {
1418 SourceLocation RetLoc;
1420 const VarDecl *NRVOCandidate;
1423 explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {}
1425 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1426 : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E),
1427 NRVOCandidate(NRVOCandidate) {}
1429 /// \brief Build an empty return expression.
1430 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) {}
1432 const Expr *getRetValue() const;
1433 Expr *getRetValue();
1434 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1436 SourceLocation getReturnLoc() const { return RetLoc; }
1437 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1439 /// \brief Retrieve the variable that might be used for the named return
1440 /// value optimization.
1442 /// The optimization itself can only be performed if the variable is
1443 /// also marked as an NRVO object.
1444 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1445 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1447 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1449 SourceLocation getLocEnd() const LLVM_READONLY {
1450 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1453 static bool classof(const Stmt *T) {
1454 return T->getStmtClass() == ReturnStmtClass;
1458 child_range children() {
1459 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1460 return child_range(child_iterator(), child_iterator());
1464 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1465 class AsmStmt : public Stmt {
1467 friend class ASTStmtReader;
1469 SourceLocation AsmLoc;
1471 /// \brief True if the assembly statement does not have any input or output
1475 /// \brief If true, treat this inline assembly as having side effects.
1476 /// This assembly statement should not be optimized, deleted or moved.
1479 unsigned NumOutputs;
1481 unsigned NumClobbers;
1483 Stmt **Exprs = nullptr;
1485 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1486 unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
1487 : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1488 NumOutputs(numoutputs), NumInputs(numinputs),
1489 NumClobbers(numclobbers) {}
1492 /// \brief Build an empty inline-assembly statement.
1493 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
1495 SourceLocation getAsmLoc() const { return AsmLoc; }
1496 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1498 bool isSimple() const { return IsSimple; }
1499 void setSimple(bool V) { IsSimple = V; }
1501 bool isVolatile() const { return IsVolatile; }
1502 void setVolatile(bool V) { IsVolatile = V; }
1504 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1505 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1507 //===--- Asm String Analysis ---===//
1509 /// Assemble final IR asm string.
1510 std::string generateAsmString(const ASTContext &C) const;
1512 //===--- Output operands ---===//
1514 unsigned getNumOutputs() const { return NumOutputs; }
1516 /// getOutputConstraint - Return the constraint string for the specified
1517 /// output operand. All output constraints are known to be non-empty (either
1519 StringRef getOutputConstraint(unsigned i) const;
1521 /// isOutputPlusConstraint - Return true if the specified output constraint
1522 /// is a "+" constraint (which is both an input and an output) or false if it
1523 /// is an "=" constraint (just an output).
1524 bool isOutputPlusConstraint(unsigned i) const {
1525 return getOutputConstraint(i)[0] == '+';
1528 const Expr *getOutputExpr(unsigned i) const;
1530 /// getNumPlusOperands - Return the number of output operands that have a "+"
1532 unsigned getNumPlusOperands() const;
1534 //===--- Input operands ---===//
1536 unsigned getNumInputs() const { return NumInputs; }
1538 /// getInputConstraint - Return the specified input constraint. Unlike output
1539 /// constraints, these can be empty.
1540 StringRef getInputConstraint(unsigned i) const;
1542 const Expr *getInputExpr(unsigned i) const;
1544 //===--- Other ---===//
1546 unsigned getNumClobbers() const { return NumClobbers; }
1547 StringRef getClobber(unsigned i) const;
1549 static bool classof(const Stmt *T) {
1550 return T->getStmtClass() == GCCAsmStmtClass ||
1551 T->getStmtClass() == MSAsmStmtClass;
1554 // Input expr iterators.
1556 using inputs_iterator = ExprIterator;
1557 using const_inputs_iterator = ConstExprIterator;
1558 using inputs_range = llvm::iterator_range<inputs_iterator>;
1559 using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
1561 inputs_iterator begin_inputs() {
1562 return &Exprs[0] + NumOutputs;
1565 inputs_iterator end_inputs() {
1566 return &Exprs[0] + NumOutputs + NumInputs;
1569 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1571 const_inputs_iterator begin_inputs() const {
1572 return &Exprs[0] + NumOutputs;
1575 const_inputs_iterator end_inputs() const {
1576 return &Exprs[0] + NumOutputs + NumInputs;
1579 inputs_const_range inputs() const {
1580 return inputs_const_range(begin_inputs(), end_inputs());
1583 // Output expr iterators.
1585 using outputs_iterator = ExprIterator;
1586 using const_outputs_iterator = ConstExprIterator;
1587 using outputs_range = llvm::iterator_range<outputs_iterator>;
1588 using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
1590 outputs_iterator begin_outputs() {
1594 outputs_iterator end_outputs() {
1595 return &Exprs[0] + NumOutputs;
1598 outputs_range outputs() {
1599 return outputs_range(begin_outputs(), end_outputs());
1602 const_outputs_iterator begin_outputs() const {
1606 const_outputs_iterator end_outputs() const {
1607 return &Exprs[0] + NumOutputs;
1610 outputs_const_range outputs() const {
1611 return outputs_const_range(begin_outputs(), end_outputs());
1614 child_range children() {
1615 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1619 /// This represents a GCC inline-assembly statement extension.
1620 class GCCAsmStmt : public AsmStmt {
1621 friend class ASTStmtReader;
1623 SourceLocation RParenLoc;
1624 StringLiteral *AsmStr;
1626 // FIXME: If we wanted to, we could allocate all of these in one big array.
1627 StringLiteral **Constraints = nullptr;
1628 StringLiteral **Clobbers = nullptr;
1629 IdentifierInfo **Names = nullptr;
1632 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1633 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1634 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1635 StringLiteral *asmstr, unsigned numclobbers,
1636 StringLiteral **clobbers, SourceLocation rparenloc);
1638 /// \brief Build an empty inline-assembly statement.
1639 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
1641 SourceLocation getRParenLoc() const { return RParenLoc; }
1642 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1644 //===--- Asm String Analysis ---===//
1646 const StringLiteral *getAsmString() const { return AsmStr; }
1647 StringLiteral *getAsmString() { return AsmStr; }
1648 void setAsmString(StringLiteral *E) { AsmStr = E; }
1650 /// AsmStringPiece - this is part of a decomposed asm string specification
1651 /// (for use with the AnalyzeAsmString function below). An asm string is
1652 /// considered to be a concatenation of these parts.
1653 class AsmStringPiece {
1656 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1657 Operand // Operand reference, with optional modifier %c4.
1665 // Source range for operand references.
1666 CharSourceRange Range;
1669 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1670 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
1672 : MyKind(Operand), Str(S), OperandNo(OpNo),
1673 Range(CharSourceRange::getCharRange(Begin, End)) {}
1675 bool isString() const { return MyKind == String; }
1676 bool isOperand() const { return MyKind == Operand; }
1678 const std::string &getString() const {
1682 unsigned getOperandNo() const {
1683 assert(isOperand());
1687 CharSourceRange getRange() const {
1688 assert(isOperand() && "Range is currently used only for Operands.");
1692 /// getModifier - Get the modifier for this operand, if present. This
1693 /// returns '\0' if there was no modifier.
1694 char getModifier() const;
1697 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1698 /// it into pieces. If the asm string is erroneous, emit errors and return
1699 /// true, otherwise return false. This handles canonicalization and
1700 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1701 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1702 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1703 const ASTContext &C, unsigned &DiagOffs) const;
1705 /// Assemble final IR asm string.
1706 std::string generateAsmString(const ASTContext &C) const;
1708 //===--- Output operands ---===//
1710 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1714 StringRef getOutputName(unsigned i) const {
1715 if (IdentifierInfo *II = getOutputIdentifier(i))
1716 return II->getName();
1721 StringRef getOutputConstraint(unsigned i) const;
1723 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1724 return Constraints[i];
1726 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1727 return Constraints[i];
1730 Expr *getOutputExpr(unsigned i);
1732 const Expr *getOutputExpr(unsigned i) const {
1733 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1736 //===--- Input operands ---===//
1738 IdentifierInfo *getInputIdentifier(unsigned i) const {
1739 return Names[i + NumOutputs];
1742 StringRef getInputName(unsigned i) const {
1743 if (IdentifierInfo *II = getInputIdentifier(i))
1744 return II->getName();
1749 StringRef getInputConstraint(unsigned i) const;
1751 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1752 return Constraints[i + NumOutputs];
1754 StringLiteral *getInputConstraintLiteral(unsigned i) {
1755 return Constraints[i + NumOutputs];
1758 Expr *getInputExpr(unsigned i);
1759 void setInputExpr(unsigned i, Expr *E);
1761 const Expr *getInputExpr(unsigned i) const {
1762 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1766 void setOutputsAndInputsAndClobbers(const ASTContext &C,
1767 IdentifierInfo **Names,
1768 StringLiteral **Constraints,
1770 unsigned NumOutputs,
1772 StringLiteral **Clobbers,
1773 unsigned NumClobbers);
1776 //===--- Other ---===//
1778 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1779 /// translate this into a numeric value needed to reference the same operand.
1780 /// This returns -1 if the operand name is invalid.
1781 int getNamedOperand(StringRef SymbolicName) const;
1783 StringRef getClobber(unsigned i) const;
1785 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1786 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1790 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1791 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1793 static bool classof(const Stmt *T) {
1794 return T->getStmtClass() == GCCAsmStmtClass;
1798 /// This represents a Microsoft inline-assembly statement extension.
1799 class MSAsmStmt : public AsmStmt {
1800 friend class ASTStmtReader;
1802 SourceLocation LBraceLoc, EndLoc;
1805 unsigned NumAsmToks = 0;
1807 Token *AsmToks = nullptr;
1808 StringRef *Constraints = nullptr;
1809 StringRef *Clobbers = nullptr;
1812 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1813 SourceLocation lbraceloc, bool issimple, bool isvolatile,
1814 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1815 ArrayRef<StringRef> constraints,
1816 ArrayRef<Expr*> exprs, StringRef asmstr,
1817 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1819 /// \brief Build an empty MS-style inline-assembly statement.
1820 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
1822 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1823 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1824 SourceLocation getEndLoc() const { return EndLoc; }
1825 void setEndLoc(SourceLocation L) { EndLoc = L; }
1827 bool hasBraces() const { return LBraceLoc.isValid(); }
1829 unsigned getNumAsmToks() { return NumAsmToks; }
1830 Token *getAsmToks() { return AsmToks; }
1832 //===--- Asm String Analysis ---===//
1833 StringRef getAsmString() const { return AsmStr; }
1835 /// Assemble final IR asm string.
1836 std::string generateAsmString(const ASTContext &C) const;
1838 //===--- Output operands ---===//
1840 StringRef getOutputConstraint(unsigned i) const {
1841 assert(i < NumOutputs);
1842 return Constraints[i];
1845 Expr *getOutputExpr(unsigned i);
1847 const Expr *getOutputExpr(unsigned i) const {
1848 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1851 //===--- Input operands ---===//
1853 StringRef getInputConstraint(unsigned i) const {
1854 assert(i < NumInputs);
1855 return Constraints[i + NumOutputs];
1858 Expr *getInputExpr(unsigned i);
1859 void setInputExpr(unsigned i, Expr *E);
1861 const Expr *getInputExpr(unsigned i) const {
1862 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1865 //===--- Other ---===//
1867 ArrayRef<StringRef> getAllConstraints() const {
1868 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
1871 ArrayRef<StringRef> getClobbers() const {
1872 return llvm::makeArrayRef(Clobbers, NumClobbers);
1875 ArrayRef<Expr*> getAllExprs() const {
1876 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
1877 NumInputs + NumOutputs);
1880 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1883 void initialize(const ASTContext &C, StringRef AsmString,
1884 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1885 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1888 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1889 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1891 static bool classof(const Stmt *T) {
1892 return T->getStmtClass() == MSAsmStmtClass;
1895 child_range children() {
1896 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1900 class SEHExceptStmt : public Stmt {
1901 friend class ASTReader;
1902 friend class ASTStmtReader;
1907 enum { FILTER_EXPR, BLOCK };
1909 SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
1910 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
1913 static SEHExceptStmt* Create(const ASTContext &C,
1914 SourceLocation ExceptLoc,
1918 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1919 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1921 SourceLocation getExceptLoc() const { return Loc; }
1922 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1924 Expr *getFilterExpr() const {
1925 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1928 CompoundStmt *getBlock() const {
1929 return cast<CompoundStmt>(Children[BLOCK]);
1932 child_range children() {
1933 return child_range(Children,Children+2);
1936 static bool classof(const Stmt *T) {
1937 return T->getStmtClass() == SEHExceptStmtClass;
1941 class SEHFinallyStmt : public Stmt {
1942 friend class ASTReader;
1943 friend class ASTStmtReader;
1948 SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
1949 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
1952 static SEHFinallyStmt* Create(const ASTContext &C,
1953 SourceLocation FinallyLoc,
1956 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1957 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1959 SourceLocation getFinallyLoc() const { return Loc; }
1960 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1962 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1964 child_range children() {
1965 return child_range(&Block,&Block+1);
1968 static bool classof(const Stmt *T) {
1969 return T->getStmtClass() == SEHFinallyStmtClass;
1973 class SEHTryStmt : public Stmt {
1974 friend class ASTReader;
1975 friend class ASTStmtReader;
1978 SourceLocation TryLoc;
1981 enum { TRY = 0, HANDLER = 1 };
1983 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1984 SourceLocation TryLoc,
1988 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
1991 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
1992 SourceLocation TryLoc, Stmt *TryBlock,
1995 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1996 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1998 SourceLocation getTryLoc() const { return TryLoc; }
1999 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
2001 bool getIsCXXTry() const { return IsCXXTry; }
2003 CompoundStmt* getTryBlock() const {
2004 return cast<CompoundStmt>(Children[TRY]);
2007 Stmt *getHandler() const { return Children[HANDLER]; }
2009 /// Returns 0 if not defined
2010 SEHExceptStmt *getExceptHandler() const;
2011 SEHFinallyStmt *getFinallyHandler() const;
2013 child_range children() {
2014 return child_range(Children,Children+2);
2017 static bool classof(const Stmt *T) {
2018 return T->getStmtClass() == SEHTryStmtClass;
2022 /// Represents a __leave statement.
2023 class SEHLeaveStmt : public Stmt {
2024 SourceLocation LeaveLoc;
2027 explicit SEHLeaveStmt(SourceLocation LL)
2028 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
2030 /// \brief Build an empty __leave statement.
2031 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
2033 SourceLocation getLeaveLoc() const { return LeaveLoc; }
2034 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
2036 SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; }
2037 SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; }
2039 static bool classof(const Stmt *T) {
2040 return T->getStmtClass() == SEHLeaveStmtClass;
2044 child_range children() {
2045 return child_range(child_iterator(), child_iterator());
2049 /// \brief This captures a statement into a function. For example, the following
2050 /// pragma annotated compound statement can be represented as a CapturedStmt,
2051 /// and this compound statement is the body of an anonymous outlined function.
2053 /// #pragma omp parallel
2058 class CapturedStmt : public Stmt {
2060 /// \brief The different capture forms: by 'this', by reference, capture for
2061 /// variable-length array type etc.
2062 enum VariableCaptureKind {
2069 /// \brief Describes the capture of either a variable, or 'this', or
2070 /// variable-length array type.
2072 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2076 friend class ASTStmtReader;
2078 /// \brief Create a new capture.
2080 /// \param Loc The source location associated with this capture.
2082 /// \param Kind The kind of capture (this, ByRef, ...).
2084 /// \param Var The variable being captured, or null if capturing this.
2085 Capture(SourceLocation Loc, VariableCaptureKind Kind,
2086 VarDecl *Var = nullptr);
2088 /// \brief Determine the kind of capture.
2089 VariableCaptureKind getCaptureKind() const;
2091 /// \brief Retrieve the source location at which the variable or 'this' was
2093 SourceLocation getLocation() const { return Loc; }
2095 /// \brief Determine whether this capture handles the C++ 'this' pointer.
2096 bool capturesThis() const { return getCaptureKind() == VCK_This; }
2098 /// \brief Determine whether this capture handles a variable (by reference).
2099 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2101 /// \brief Determine whether this capture handles a variable by copy.
2102 bool capturesVariableByCopy() const {
2103 return getCaptureKind() == VCK_ByCopy;
2106 /// \brief Determine whether this capture handles a variable-length array
2108 bool capturesVariableArrayType() const {
2109 return getCaptureKind() == VCK_VLAType;
2112 /// \brief Retrieve the declaration of the variable being captured.
2114 /// This operation is only valid if this capture captures a variable.
2115 VarDecl *getCapturedVar() const;
2119 /// \brief The number of variable captured, including 'this'.
2120 unsigned NumCaptures;
2122 /// \brief The pointer part is the implicit the outlined function and the
2123 /// int part is the captured region kind, 'CR_Default' etc.
2124 llvm::PointerIntPair<CapturedDecl *, 1, CapturedRegionKind> CapDeclAndKind;
2126 /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl.
2127 RecordDecl *TheRecordDecl = nullptr;
2129 /// \brief Construct a captured statement.
2130 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2131 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2133 /// \brief Construct an empty captured statement.
2134 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2136 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
2138 Stmt *const *getStoredStmts() const {
2139 return reinterpret_cast<Stmt *const *>(this + 1);
2142 Capture *getStoredCaptures() const;
2144 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2147 friend class ASTStmtReader;
2149 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2150 CapturedRegionKind Kind,
2151 ArrayRef<Capture> Captures,
2152 ArrayRef<Expr *> CaptureInits,
2153 CapturedDecl *CD, RecordDecl *RD);
2155 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2156 unsigned NumCaptures);
2158 /// \brief Retrieve the statement being captured.
2159 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2160 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
2162 /// \brief Retrieve the outlined function declaration.
2163 CapturedDecl *getCapturedDecl();
2164 const CapturedDecl *getCapturedDecl() const;
2166 /// \brief Set the outlined function declaration.
2167 void setCapturedDecl(CapturedDecl *D);
2169 /// \brief Retrieve the captured region kind.
2170 CapturedRegionKind getCapturedRegionKind() const;
2172 /// \brief Set the captured region kind.
2173 void setCapturedRegionKind(CapturedRegionKind Kind);
2175 /// \brief Retrieve the record declaration for captured variables.
2176 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2178 /// \brief Set the record declaration for captured variables.
2179 void setCapturedRecordDecl(RecordDecl *D) {
2180 assert(D && "null RecordDecl");
2184 /// \brief True if this variable has been captured.
2185 bool capturesVariable(const VarDecl *Var) const;
2187 /// \brief An iterator that walks over the captures.
2188 using capture_iterator = Capture *;
2189 using const_capture_iterator = const Capture *;
2190 using capture_range = llvm::iterator_range<capture_iterator>;
2191 using capture_const_range = llvm::iterator_range<const_capture_iterator>;
2193 capture_range captures() {
2194 return capture_range(capture_begin(), capture_end());
2196 capture_const_range captures() const {
2197 return capture_const_range(capture_begin(), capture_end());
2200 /// \brief Retrieve an iterator pointing to the first capture.
2201 capture_iterator capture_begin() { return getStoredCaptures(); }
2202 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2204 /// \brief Retrieve an iterator pointing past the end of the sequence of
2206 capture_iterator capture_end() const {
2207 return getStoredCaptures() + NumCaptures;
2210 /// \brief Retrieve the number of captures, including 'this'.
2211 unsigned capture_size() const { return NumCaptures; }
2213 /// \brief Iterator that walks over the capture initialization arguments.
2214 using capture_init_iterator = Expr **;
2215 using capture_init_range = llvm::iterator_range<capture_init_iterator>;
2217 /// \brief Const iterator that walks over the capture initialization
2219 using const_capture_init_iterator = Expr *const *;
2220 using const_capture_init_range =
2221 llvm::iterator_range<const_capture_init_iterator>;
2223 capture_init_range capture_inits() {
2224 return capture_init_range(capture_init_begin(), capture_init_end());
2227 const_capture_init_range capture_inits() const {
2228 return const_capture_init_range(capture_init_begin(), capture_init_end());
2231 /// \brief Retrieve the first initialization argument.
2232 capture_init_iterator capture_init_begin() {
2233 return reinterpret_cast<Expr **>(getStoredStmts());
2236 const_capture_init_iterator capture_init_begin() const {
2237 return reinterpret_cast<Expr *const *>(getStoredStmts());
2240 /// \brief Retrieve the iterator pointing one past the last initialization
2242 capture_init_iterator capture_init_end() {
2243 return capture_init_begin() + NumCaptures;
2246 const_capture_init_iterator capture_init_end() const {
2247 return capture_init_begin() + NumCaptures;
2250 SourceLocation getLocStart() const LLVM_READONLY {
2251 return getCapturedStmt()->getLocStart();
2254 SourceLocation getLocEnd() const LLVM_READONLY {
2255 return getCapturedStmt()->getLocEnd();
2258 SourceRange getSourceRange() const LLVM_READONLY {
2259 return getCapturedStmt()->getSourceRange();
2262 static bool classof(const Stmt *T) {
2263 return T->getStmtClass() == CapturedStmtClass;
2266 child_range children();
2269 } // namespace clang
2271 #endif // LLVM_CLANG_AST_STMT_H