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/iterator.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/ErrorHandling.h"
31 class FoldingSetNodeID;
45 struct PrintingPolicy;
54 //===----------------------------------------------------------------------===//
55 // AST classes for statements.
56 //===----------------------------------------------------------------------===//
58 /// Stmt - This represents one statement.
60 class alignas(void *) Stmt {
64 #define STMT(CLASS, PARENT) CLASS##Class,
65 #define STMT_RANGE(BASE, FIRST, LAST) \
66 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
67 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
68 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
69 #define ABSTRACT_STMT(STMT)
70 #include "clang/AST/StmtNodes.inc"
73 // Make vanilla 'new' and 'delete' illegal for Stmts.
75 void *operator new(size_t bytes) noexcept {
76 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
78 void operator delete(void *data) noexcept {
79 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
85 /// \brief The statement class.
88 enum { NumStmtBits = 8 };
90 class CompoundStmtBitfields {
91 friend class CompoundStmt;
92 unsigned : NumStmtBits;
94 unsigned NumStmts : 32 - NumStmtBits;
97 class IfStmtBitfields {
99 unsigned : NumStmtBits;
101 unsigned IsConstexpr : 1;
104 class ExprBitfields {
106 friend class DeclRefExpr; // computeDependence
107 friend class InitListExpr; // ctor
108 friend class DesignatedInitExpr; // ctor
109 friend class BlockDeclRefExpr; // ctor
110 friend class ASTStmtReader; // deserialization
111 friend class CXXNewExpr; // ctor
112 friend class DependentScopeDeclRefExpr; // ctor
113 friend class CXXConstructExpr; // ctor
114 friend class CallExpr; // ctor
115 friend class OffsetOfExpr; // ctor
116 friend class ObjCMessageExpr; // ctor
117 friend class ObjCArrayLiteral; // ctor
118 friend class ObjCDictionaryLiteral; // ctor
119 friend class ShuffleVectorExpr; // ctor
120 friend class ParenListExpr; // ctor
121 friend class CXXUnresolvedConstructExpr; // ctor
122 friend class CXXDependentScopeMemberExpr; // ctor
123 friend class OverloadExpr; // ctor
124 friend class PseudoObjectExpr; // ctor
125 friend class AtomicExpr; // ctor
126 friend class OpaqueValueExpr; // ctor
127 unsigned : NumStmtBits;
129 unsigned ValueKind : 2;
130 unsigned ObjectKind : 3;
131 unsigned TypeDependent : 1;
132 unsigned ValueDependent : 1;
133 unsigned InstantiationDependent : 1;
134 unsigned ContainsUnexpandedParameterPack : 1;
136 enum { NumExprBits = 17 };
138 class CharacterLiteralBitfields {
139 friend class CharacterLiteral;
140 unsigned : NumExprBits;
145 enum APFloatSemantics {
154 class FloatingLiteralBitfields {
155 friend class FloatingLiteral;
156 unsigned : NumExprBits;
158 unsigned Semantics : 3; // Provides semantics for APFloat construction
159 unsigned IsExact : 1;
162 class UnaryExprOrTypeTraitExprBitfields {
163 friend class UnaryExprOrTypeTraitExpr;
164 unsigned : NumExprBits;
167 unsigned IsType : 1; // true if operand is a type, false if an expression.
170 class DeclRefExprBitfields {
171 friend class DeclRefExpr;
172 friend class ASTStmtReader; // deserialization
173 unsigned : NumExprBits;
175 unsigned HasQualifier : 1;
176 unsigned HasTemplateKWAndArgsInfo : 1;
177 unsigned HasFoundDecl : 1;
178 unsigned HadMultipleCandidates : 1;
179 unsigned RefersToEnclosingVariableOrCapture : 1;
182 class CastExprBitfields {
183 friend class CastExpr;
184 unsigned : NumExprBits;
187 unsigned BasePathSize : 32 - 6 - NumExprBits;
190 class CallExprBitfields {
191 friend class CallExpr;
192 unsigned : NumExprBits;
194 unsigned NumPreArgs : 1;
197 class ExprWithCleanupsBitfields {
198 friend class ExprWithCleanups;
199 friend class ASTStmtReader; // deserialization
201 unsigned : NumExprBits;
203 // When false, it must not have side effects.
204 unsigned CleanupsHaveSideEffects : 1;
206 unsigned NumObjects : 32 - 1 - NumExprBits;
209 class PseudoObjectExprBitfields {
210 friend class PseudoObjectExpr;
211 friend class ASTStmtReader; // deserialization
213 unsigned : NumExprBits;
215 // These don't need to be particularly wide, because they're
216 // strictly limited by the forms of expressions we permit.
217 unsigned NumSubExprs : 8;
218 unsigned ResultIndex : 32 - 8 - NumExprBits;
221 class ObjCIndirectCopyRestoreExprBitfields {
222 friend class ObjCIndirectCopyRestoreExpr;
223 unsigned : NumExprBits;
225 unsigned ShouldCopy : 1;
228 class InitListExprBitfields {
229 friend class InitListExpr;
231 unsigned : NumExprBits;
233 /// Whether this initializer list originally had a GNU array-range
234 /// designator in it. This is a temporary marker used by CodeGen.
235 unsigned HadArrayRangeDesignator : 1;
238 class TypeTraitExprBitfields {
239 friend class TypeTraitExpr;
240 friend class ASTStmtReader;
241 friend class ASTStmtWriter;
243 unsigned : NumExprBits;
245 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
248 /// \brief If this expression is not value-dependent, this indicates whether
249 /// the trait evaluated true or false.
252 /// \brief The number of arguments to this type trait.
253 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
256 class CoawaitExprBitfields {
257 friend class CoawaitExpr;
259 unsigned : NumExprBits;
261 unsigned IsImplicit : 1;
265 StmtBitfields StmtBits;
266 CompoundStmtBitfields CompoundStmtBits;
267 IfStmtBitfields IfStmtBits;
268 ExprBitfields ExprBits;
269 CharacterLiteralBitfields CharacterLiteralBits;
270 FloatingLiteralBitfields FloatingLiteralBits;
271 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
272 DeclRefExprBitfields DeclRefExprBits;
273 CastExprBitfields CastExprBits;
274 CallExprBitfields CallExprBits;
275 ExprWithCleanupsBitfields ExprWithCleanupsBits;
276 PseudoObjectExprBitfields PseudoObjectExprBits;
277 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
278 InitListExprBitfields InitListExprBits;
279 TypeTraitExprBitfields TypeTraitExprBits;
280 CoawaitExprBitfields CoawaitBits;
283 friend class ASTStmtReader;
284 friend class ASTStmtWriter;
287 // Only allow allocation of Stmts using the allocator in ASTContext
288 // or by doing a placement new.
289 void* operator new(size_t bytes, const ASTContext& C,
290 unsigned alignment = 8);
292 void* operator new(size_t bytes, const ASTContext* C,
293 unsigned alignment = 8) {
294 return operator new(bytes, *C, alignment);
297 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
299 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
300 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
301 void operator delete(void *, size_t) noexcept {}
302 void operator delete(void *, void *) noexcept {}
305 /// \brief A placeholder type used to construct an empty shell of a
306 /// type, that will be filled in later (e.g., by some
307 /// de-serialization).
308 struct EmptyShell { };
311 /// Iterator for iterating over Stmt * arrays that contain only Expr *
313 /// This is needed because AST nodes use Stmt* arrays to store
314 /// references to children (to be compatible with StmtIterator).
316 : llvm::iterator_adaptor_base<ExprIterator, Stmt **,
317 std::random_access_iterator_tag, Expr *> {
318 ExprIterator() : iterator_adaptor_base(nullptr) {}
319 ExprIterator(Stmt **I) : iterator_adaptor_base(I) {}
321 reference operator*() const {
322 assert((*I)->getStmtClass() >= firstExprConstant &&
323 (*I)->getStmtClass() <= lastExprConstant);
324 return *reinterpret_cast<Expr **>(I);
328 /// Const iterator for iterating over Stmt * arrays that contain only Expr *
329 struct ConstExprIterator
330 : llvm::iterator_adaptor_base<ConstExprIterator, const Stmt *const *,
331 std::random_access_iterator_tag,
333 ConstExprIterator() : iterator_adaptor_base(nullptr) {}
334 ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {}
336 reference operator*() const {
337 assert((*I)->getStmtClass() >= firstExprConstant &&
338 (*I)->getStmtClass() <= lastExprConstant);
339 return *reinterpret_cast<const Expr *const *>(I);
344 /// \brief Whether statistic collection is enabled.
345 static bool StatisticsEnabled;
348 /// \brief Construct an empty statement.
349 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
353 static_assert(sizeof(*this) == sizeof(void *),
354 "changing bitfields changed sizeof(Stmt)");
355 static_assert(sizeof(*this) % alignof(void *) == 0,
356 "Insufficient alignment!");
357 StmtBits.sClass = SC;
358 if (StatisticsEnabled) Stmt::addStmtClass(SC);
361 StmtClass getStmtClass() const {
362 return static_cast<StmtClass>(StmtBits.sClass);
364 const char *getStmtClassName() const;
366 /// SourceLocation tokens are not useful in isolation - they are low level
367 /// value objects created/interpreted by SourceManager. We assume AST
368 /// clients will have a pointer to the respective SourceManager.
369 SourceRange getSourceRange() const LLVM_READONLY;
370 SourceLocation getLocStart() const LLVM_READONLY;
371 SourceLocation getLocEnd() const LLVM_READONLY;
373 // global temp stats (until we have a per-module visitor)
374 static void addStmtClass(const StmtClass s);
375 static void EnableStatistics();
376 static void PrintStats();
378 /// \brief Dumps the specified AST fragment and all subtrees to
381 void dump(SourceManager &SM) const;
382 void dump(raw_ostream &OS, SourceManager &SM) const;
383 void dump(raw_ostream &OS) const;
385 /// dumpColor - same as dump(), but forces color highlighting.
386 void dumpColor() const;
388 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
389 /// back to its original source language syntax.
390 void dumpPretty(const ASTContext &Context) const;
391 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
392 const PrintingPolicy &Policy,
393 unsigned Indentation = 0) const;
395 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
396 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
397 void viewAST() const;
399 /// Skip past any implicit AST nodes which might surround this
400 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
401 Stmt *IgnoreImplicit();
402 const Stmt *IgnoreImplicit() const {
403 return const_cast<Stmt *>(this)->IgnoreImplicit();
406 /// \brief Skip no-op (attributed, compound) container stmts and skip captured
407 /// stmt at the top, if \a IgnoreCaptured is true.
408 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
410 const Stmt *stripLabelLikeStatements() const;
411 Stmt *stripLabelLikeStatements() {
412 return const_cast<Stmt*>(
413 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
416 /// Child Iterators: All subclasses must implement 'children'
417 /// to permit easy iteration over the substatements/subexpessions of an
418 /// AST node. This permits easy iteration over all nodes in the AST.
419 typedef StmtIterator child_iterator;
420 typedef ConstStmtIterator const_child_iterator;
422 typedef llvm::iterator_range<child_iterator> child_range;
423 typedef llvm::iterator_range<const_child_iterator> const_child_range;
425 child_range children();
426 const_child_range children() const {
427 auto Children = const_cast<Stmt *>(this)->children();
428 return const_child_range(Children.begin(), Children.end());
431 child_iterator child_begin() { return children().begin(); }
432 child_iterator child_end() { return children().end(); }
434 const_child_iterator child_begin() const { return children().begin(); }
435 const_child_iterator child_end() const { return children().end(); }
437 /// \brief Produce a unique representation of the given statement.
439 /// \param ID once the profiling operation is complete, will contain
440 /// the unique representation of the given statement.
442 /// \param Context the AST context in which the statement resides
444 /// \param Canonical whether the profile should be based on the canonical
445 /// representation of this statement (e.g., where non-type template
446 /// parameters are identified by index/level rather than their
447 /// declaration pointers) or the exact representation of the statement as
448 /// written in the source.
449 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
450 bool Canonical) const;
452 /// \brief Calculate a unique representation for a statement that is
453 /// stable across compiler invocations.
455 /// \param ID profile information will be stored in ID.
457 /// \param Hash an ODRHash object which will be called where pointers would
458 /// have been used in the Profile function.
459 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
462 /// DeclStmt - Adaptor class for mixing declarations with statements and
463 /// expressions. For example, CompoundStmt mixes statements, expressions
464 /// and declarations (variables, types). Another example is ForStmt, where
465 /// the first statement can be an expression or a declaration.
467 class DeclStmt : public Stmt {
469 SourceLocation StartLoc, EndLoc;
472 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
473 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
474 StartLoc(startLoc), EndLoc(endLoc) {}
476 /// \brief Build an empty declaration statement.
477 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
479 /// isSingleDecl - This method returns true if this DeclStmt refers
480 /// to a single Decl.
481 bool isSingleDecl() const {
482 return DG.isSingleDecl();
485 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
486 Decl *getSingleDecl() { return DG.getSingleDecl(); }
488 const DeclGroupRef getDeclGroup() const { return DG; }
489 DeclGroupRef getDeclGroup() { return DG; }
490 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
492 SourceLocation getStartLoc() const { return StartLoc; }
493 void setStartLoc(SourceLocation L) { StartLoc = L; }
494 SourceLocation getEndLoc() const { return EndLoc; }
495 void setEndLoc(SourceLocation L) { EndLoc = L; }
497 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
498 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
500 static bool classof(const Stmt *T) {
501 return T->getStmtClass() == DeclStmtClass;
504 // Iterators over subexpressions.
505 child_range children() {
506 return child_range(child_iterator(DG.begin(), DG.end()),
507 child_iterator(DG.end(), DG.end()));
510 typedef DeclGroupRef::iterator decl_iterator;
511 typedef DeclGroupRef::const_iterator const_decl_iterator;
512 typedef llvm::iterator_range<decl_iterator> decl_range;
513 typedef llvm::iterator_range<const_decl_iterator> decl_const_range;
515 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
516 decl_const_range decls() const {
517 return decl_const_range(decl_begin(), decl_end());
519 decl_iterator decl_begin() { return DG.begin(); }
520 decl_iterator decl_end() { return DG.end(); }
521 const_decl_iterator decl_begin() const { return DG.begin(); }
522 const_decl_iterator decl_end() const { return DG.end(); }
524 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
525 reverse_decl_iterator decl_rbegin() {
526 return reverse_decl_iterator(decl_end());
528 reverse_decl_iterator decl_rend() {
529 return reverse_decl_iterator(decl_begin());
533 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
535 class NullStmt : public Stmt {
536 SourceLocation SemiLoc;
538 /// \brief True if the null statement was preceded by an empty macro, e.g:
543 bool HasLeadingEmptyMacro;
545 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
546 : Stmt(NullStmtClass), SemiLoc(L),
547 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
549 /// \brief Build an empty null statement.
550 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
551 HasLeadingEmptyMacro(false) { }
553 SourceLocation getSemiLoc() const { return SemiLoc; }
554 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
556 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
558 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
559 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
561 static bool classof(const Stmt *T) {
562 return T->getStmtClass() == NullStmtClass;
565 child_range children() {
566 return child_range(child_iterator(), child_iterator());
569 friend class ASTStmtReader;
570 friend class ASTStmtWriter;
573 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
575 class CompoundStmt : public Stmt {
577 SourceLocation LBraceLoc, RBraceLoc;
579 friend class ASTStmtReader;
582 CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
583 SourceLocation LB, SourceLocation RB);
585 // \brief Build an empty compound statement with a location.
586 explicit CompoundStmt(SourceLocation Loc)
587 : Stmt(CompoundStmtClass), Body(nullptr), LBraceLoc(Loc), RBraceLoc(Loc) {
588 CompoundStmtBits.NumStmts = 0;
591 // \brief Build an empty compound statement.
592 explicit CompoundStmt(EmptyShell Empty)
593 : Stmt(CompoundStmtClass, Empty), Body(nullptr) {
594 CompoundStmtBits.NumStmts = 0;
597 void setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts);
599 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
600 unsigned size() const { return CompoundStmtBits.NumStmts; }
602 typedef Stmt** body_iterator;
603 typedef llvm::iterator_range<body_iterator> body_range;
605 body_range body() { return body_range(body_begin(), body_end()); }
606 body_iterator body_begin() { return Body; }
607 body_iterator body_end() { return Body + size(); }
608 Stmt *body_front() { return !body_empty() ? Body[0] : nullptr; }
609 Stmt *body_back() { return !body_empty() ? Body[size()-1] : nullptr; }
611 void setLastStmt(Stmt *S) {
612 assert(!body_empty() && "setLastStmt");
616 typedef Stmt* const * const_body_iterator;
617 typedef llvm::iterator_range<const_body_iterator> body_const_range;
619 body_const_range body() const {
620 return body_const_range(body_begin(), body_end());
622 const_body_iterator body_begin() const { return Body; }
623 const_body_iterator body_end() const { return Body + size(); }
624 const Stmt *body_front() const {
625 return !body_empty() ? Body[0] : nullptr;
627 const Stmt *body_back() const {
628 return !body_empty() ? Body[size() - 1] : nullptr;
631 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
632 reverse_body_iterator body_rbegin() {
633 return reverse_body_iterator(body_end());
635 reverse_body_iterator body_rend() {
636 return reverse_body_iterator(body_begin());
639 typedef std::reverse_iterator<const_body_iterator>
640 const_reverse_body_iterator;
642 const_reverse_body_iterator body_rbegin() const {
643 return const_reverse_body_iterator(body_end());
646 const_reverse_body_iterator body_rend() const {
647 return const_reverse_body_iterator(body_begin());
650 SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; }
651 SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; }
653 SourceLocation getLBracLoc() const { return LBraceLoc; }
654 SourceLocation getRBracLoc() const { return RBraceLoc; }
656 static bool classof(const Stmt *T) {
657 return T->getStmtClass() == CompoundStmtClass;
661 child_range children() {
662 return child_range(Body, Body + CompoundStmtBits.NumStmts);
665 const_child_range children() const {
666 return const_child_range(child_iterator(Body),
667 child_iterator(Body + CompoundStmtBits.NumStmts));
671 // SwitchCase is the base class for CaseStmt and DefaultStmt,
672 class SwitchCase : public Stmt {
674 // A pointer to the following CaseStmt or DefaultStmt class,
675 // used by SwitchStmt.
676 SwitchCase *NextSwitchCase;
677 SourceLocation KeywordLoc;
678 SourceLocation ColonLoc;
680 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
681 : Stmt(SC), NextSwitchCase(nullptr), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {
684 SwitchCase(StmtClass SC, EmptyShell)
685 : Stmt(SC), NextSwitchCase(nullptr) {}
688 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
690 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
692 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
694 SourceLocation getKeywordLoc() const { return KeywordLoc; }
695 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
696 SourceLocation getColonLoc() const { return ColonLoc; }
697 void setColonLoc(SourceLocation L) { ColonLoc = L; }
700 const Stmt *getSubStmt() const {
701 return const_cast<SwitchCase*>(this)->getSubStmt();
704 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
705 SourceLocation getLocEnd() const LLVM_READONLY;
707 static bool classof(const Stmt *T) {
708 return T->getStmtClass() == CaseStmtClass ||
709 T->getStmtClass() == DefaultStmtClass;
713 class CaseStmt : public SwitchCase {
714 SourceLocation EllipsisLoc;
715 enum { LHS, RHS, SUBSTMT, END_EXPR };
716 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
717 // GNU "case 1 ... 4" extension
719 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
720 SourceLocation ellipsisLoc, SourceLocation colonLoc)
721 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
722 SubExprs[SUBSTMT] = nullptr;
723 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
724 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
725 EllipsisLoc = ellipsisLoc;
728 /// \brief Build an empty switch case statement.
729 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) { }
731 SourceLocation getCaseLoc() const { return KeywordLoc; }
732 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
733 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
734 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
735 SourceLocation getColonLoc() const { return ColonLoc; }
736 void setColonLoc(SourceLocation L) { ColonLoc = L; }
738 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
739 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
740 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
742 const Expr *getLHS() const {
743 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
745 const Expr *getRHS() const {
746 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
748 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
750 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
751 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
752 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
754 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
755 SourceLocation getLocEnd() const LLVM_READONLY {
756 // Handle deeply nested case statements with iteration instead of recursion.
757 const CaseStmt *CS = this;
758 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
761 return CS->getSubStmt()->getLocEnd();
764 static bool classof(const Stmt *T) {
765 return T->getStmtClass() == CaseStmtClass;
769 child_range children() {
770 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
774 class DefaultStmt : public SwitchCase {
777 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
778 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
780 /// \brief Build an empty default statement.
781 explicit DefaultStmt(EmptyShell Empty)
782 : SwitchCase(DefaultStmtClass, Empty) { }
784 Stmt *getSubStmt() { return SubStmt; }
785 const Stmt *getSubStmt() const { return SubStmt; }
786 void setSubStmt(Stmt *S) { SubStmt = S; }
788 SourceLocation getDefaultLoc() const { return KeywordLoc; }
789 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
790 SourceLocation getColonLoc() const { return ColonLoc; }
791 void setColonLoc(SourceLocation L) { ColonLoc = L; }
793 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
794 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
796 static bool classof(const Stmt *T) {
797 return T->getStmtClass() == DefaultStmtClass;
801 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
804 inline SourceLocation SwitchCase::getLocEnd() const {
805 if (const CaseStmt *CS = dyn_cast<CaseStmt>(this))
806 return CS->getLocEnd();
807 return cast<DefaultStmt>(this)->getLocEnd();
810 /// LabelStmt - Represents a label, which has a substatement. For example:
813 class LabelStmt : public Stmt {
814 SourceLocation IdentLoc;
819 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
820 : Stmt(LabelStmtClass), IdentLoc(IL), TheDecl(D), SubStmt(substmt) {
821 static_assert(sizeof(LabelStmt) ==
822 2 * sizeof(SourceLocation) + 2 * sizeof(void *),
823 "LabelStmt too big");
826 // \brief Build an empty label statement.
827 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
829 SourceLocation getIdentLoc() const { return IdentLoc; }
830 LabelDecl *getDecl() const { return TheDecl; }
831 void setDecl(LabelDecl *D) { TheDecl = D; }
832 const char *getName() const;
833 Stmt *getSubStmt() { return SubStmt; }
834 const Stmt *getSubStmt() const { return SubStmt; }
835 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
836 void setSubStmt(Stmt *SS) { SubStmt = SS; }
838 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
839 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
841 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
843 static bool classof(const Stmt *T) {
844 return T->getStmtClass() == LabelStmtClass;
849 /// \brief Represents an attribute applied to a statement.
851 /// Represents an attribute applied to a statement. For example:
852 /// [[omp::for(...)]] for (...) { ... }
854 class AttributedStmt : public Stmt {
856 SourceLocation AttrLoc;
859 friend class ASTStmtReader;
861 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
862 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
863 NumAttrs(Attrs.size()) {
864 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
867 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
868 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
869 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
872 const Attr *const *getAttrArrayPtr() const {
873 return reinterpret_cast<const Attr *const *>(this + 1);
875 const Attr **getAttrArrayPtr() {
876 return reinterpret_cast<const Attr **>(this + 1);
880 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
881 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
882 // \brief Build an empty attributed statement.
883 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
885 SourceLocation getAttrLoc() const { return AttrLoc; }
886 ArrayRef<const Attr*> getAttrs() const {
887 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs);
889 Stmt *getSubStmt() { return SubStmt; }
890 const Stmt *getSubStmt() const { return SubStmt; }
892 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
893 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
895 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
897 static bool classof(const Stmt *T) {
898 return T->getStmtClass() == AttributedStmtClass;
903 /// IfStmt - This represents an if/then/else.
905 class IfStmt : public Stmt {
906 enum { INIT, VAR, COND, THEN, ELSE, END_EXPR };
907 Stmt* SubExprs[END_EXPR];
909 SourceLocation IfLoc;
910 SourceLocation ElseLoc;
913 IfStmt(const ASTContext &C, SourceLocation IL,
914 bool IsConstexpr, Stmt *init, VarDecl *var, Expr *cond,
915 Stmt *then, SourceLocation EL = SourceLocation(),
916 Stmt *elsev = nullptr);
918 /// \brief Build an empty if/then/else statement
919 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
921 /// \brief Retrieve the variable declared in this "if" statement, if any.
923 /// In the following example, "x" is the condition variable.
925 /// if (int x = foo()) {
926 /// printf("x is %d", x);
929 VarDecl *getConditionVariable() const;
930 void setConditionVariable(const ASTContext &C, VarDecl *V);
932 /// If this IfStmt has a condition variable, return the faux DeclStmt
933 /// associated with the creation of that condition variable.
934 const DeclStmt *getConditionVariableDeclStmt() const {
935 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
938 Stmt *getInit() { return SubExprs[INIT]; }
939 const Stmt *getInit() const { return SubExprs[INIT]; }
940 void setInit(Stmt *S) { SubExprs[INIT] = S; }
941 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
942 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
943 const Stmt *getThen() const { return SubExprs[THEN]; }
944 void setThen(Stmt *S) { SubExprs[THEN] = S; }
945 const Stmt *getElse() const { return SubExprs[ELSE]; }
946 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
948 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
949 Stmt *getThen() { return SubExprs[THEN]; }
950 Stmt *getElse() { return SubExprs[ELSE]; }
952 SourceLocation getIfLoc() const { return IfLoc; }
953 void setIfLoc(SourceLocation L) { IfLoc = L; }
954 SourceLocation getElseLoc() const { return ElseLoc; }
955 void setElseLoc(SourceLocation L) { ElseLoc = L; }
957 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
958 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
960 bool isObjCAvailabilityCheck() const;
962 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
963 SourceLocation getLocEnd() const LLVM_READONLY {
965 return SubExprs[ELSE]->getLocEnd();
967 return SubExprs[THEN]->getLocEnd();
970 // Iterators over subexpressions. The iterators will include iterating
971 // over the initialization expression referenced by the condition variable.
972 child_range children() {
973 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
976 static bool classof(const Stmt *T) {
977 return T->getStmtClass() == IfStmtClass;
981 /// SwitchStmt - This represents a 'switch' stmt.
983 class SwitchStmt : public Stmt {
984 SourceLocation SwitchLoc;
985 enum { INIT, VAR, COND, BODY, END_EXPR };
986 Stmt* SubExprs[END_EXPR];
987 // This points to a linked list of case and default statements and, if the
988 // SwitchStmt is a switch on an enum value, records whether all the enum
989 // values were covered by CaseStmts. The coverage information value is meant
990 // to be a hint for possible clients.
991 llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase;
994 SwitchStmt(const ASTContext &C, Stmt *Init, VarDecl *Var, Expr *cond);
996 /// \brief Build a empty switch statement.
997 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
999 /// \brief Retrieve the variable declared in this "switch" statement, if any.
1001 /// In the following example, "x" is the condition variable.
1003 /// switch (int x = foo()) {
1008 VarDecl *getConditionVariable() const;
1009 void setConditionVariable(const ASTContext &C, VarDecl *V);
1011 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
1012 /// associated with the creation of that condition variable.
1013 const DeclStmt *getConditionVariableDeclStmt() const {
1014 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1017 Stmt *getInit() { return SubExprs[INIT]; }
1018 const Stmt *getInit() const { return SubExprs[INIT]; }
1019 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1020 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1021 const Stmt *getBody() const { return SubExprs[BODY]; }
1022 const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); }
1024 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1025 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
1026 Stmt *getBody() { return SubExprs[BODY]; }
1027 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1028 SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); }
1030 /// \brief Set the case list for this switch statement.
1031 void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); }
1033 SourceLocation getSwitchLoc() const { return SwitchLoc; }
1034 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
1036 void setBody(Stmt *S, SourceLocation SL) {
1040 void addSwitchCase(SwitchCase *SC) {
1041 assert(!SC->getNextSwitchCase()
1042 && "case/default already added to a switch");
1043 SC->setNextSwitchCase(FirstCase.getPointer());
1044 FirstCase.setPointer(SC);
1047 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1048 /// switch over an enum value then all cases have been explicitly covered.
1049 void setAllEnumCasesCovered() { FirstCase.setInt(true); }
1051 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1052 /// have been explicitly covered.
1053 bool isAllEnumCasesCovered() const { return FirstCase.getInt(); }
1055 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1056 SourceLocation getLocEnd() const LLVM_READONLY {
1057 return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
1061 child_range children() {
1062 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1065 static bool classof(const Stmt *T) {
1066 return T->getStmtClass() == SwitchStmtClass;
1071 /// WhileStmt - This represents a 'while' stmt.
1073 class WhileStmt : public Stmt {
1074 SourceLocation WhileLoc;
1075 enum { VAR, COND, BODY, END_EXPR };
1076 Stmt* SubExprs[END_EXPR];
1078 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1081 /// \brief Build an empty while statement.
1082 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1084 /// \brief Retrieve the variable declared in this "while" statement, if any.
1086 /// In the following example, "x" is the condition variable.
1088 /// while (int x = random()) {
1092 VarDecl *getConditionVariable() const;
1093 void setConditionVariable(const ASTContext &C, VarDecl *V);
1095 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1096 /// associated with the creation of that condition variable.
1097 const DeclStmt *getConditionVariableDeclStmt() const {
1098 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1101 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1102 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1103 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1104 Stmt *getBody() { return SubExprs[BODY]; }
1105 const Stmt *getBody() const { return SubExprs[BODY]; }
1106 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1108 SourceLocation getWhileLoc() const { return WhileLoc; }
1109 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1111 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1112 SourceLocation getLocEnd() const LLVM_READONLY {
1113 return SubExprs[BODY]->getLocEnd();
1116 static bool classof(const Stmt *T) {
1117 return T->getStmtClass() == WhileStmtClass;
1121 child_range children() {
1122 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1126 /// DoStmt - This represents a 'do/while' stmt.
1128 class DoStmt : public Stmt {
1129 SourceLocation DoLoc;
1130 enum { BODY, COND, END_EXPR };
1131 Stmt* SubExprs[END_EXPR];
1132 SourceLocation WhileLoc;
1133 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1136 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1138 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1139 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1140 SubExprs[BODY] = body;
1143 /// \brief Build an empty do-while statement.
1144 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1146 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1147 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1148 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1149 Stmt *getBody() { return SubExprs[BODY]; }
1150 const Stmt *getBody() const { return SubExprs[BODY]; }
1151 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1153 SourceLocation getDoLoc() const { return DoLoc; }
1154 void setDoLoc(SourceLocation L) { DoLoc = L; }
1155 SourceLocation getWhileLoc() const { return WhileLoc; }
1156 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1158 SourceLocation getRParenLoc() const { return RParenLoc; }
1159 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1161 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1162 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1164 static bool classof(const Stmt *T) {
1165 return T->getStmtClass() == DoStmtClass;
1169 child_range children() {
1170 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1175 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1176 /// the init/cond/inc parts of the ForStmt will be null if they were not
1177 /// specified in the source.
1179 class ForStmt : public Stmt {
1180 SourceLocation ForLoc;
1181 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1182 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1183 SourceLocation LParenLoc, RParenLoc;
1186 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1187 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1190 /// \brief Build an empty for statement.
1191 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1193 Stmt *getInit() { return SubExprs[INIT]; }
1195 /// \brief Retrieve the variable declared in this "for" statement, if any.
1197 /// In the following example, "y" is the condition variable.
1199 /// for (int x = random(); int y = mangle(x); ++x) {
1203 VarDecl *getConditionVariable() const;
1204 void setConditionVariable(const ASTContext &C, VarDecl *V);
1206 /// If this ForStmt has a condition variable, return the faux DeclStmt
1207 /// associated with the creation of that condition variable.
1208 const DeclStmt *getConditionVariableDeclStmt() const {
1209 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1212 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1213 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1214 Stmt *getBody() { return SubExprs[BODY]; }
1216 const Stmt *getInit() const { return SubExprs[INIT]; }
1217 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1218 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1219 const Stmt *getBody() const { return SubExprs[BODY]; }
1221 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1222 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1223 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1224 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1226 SourceLocation getForLoc() const { return ForLoc; }
1227 void setForLoc(SourceLocation L) { ForLoc = L; }
1228 SourceLocation getLParenLoc() const { return LParenLoc; }
1229 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1230 SourceLocation getRParenLoc() const { return RParenLoc; }
1231 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1233 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1234 SourceLocation getLocEnd() const LLVM_READONLY {
1235 return SubExprs[BODY]->getLocEnd();
1238 static bool classof(const Stmt *T) {
1239 return T->getStmtClass() == ForStmtClass;
1243 child_range children() {
1244 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1248 /// GotoStmt - This represents a direct goto.
1250 class GotoStmt : public Stmt {
1252 SourceLocation GotoLoc;
1253 SourceLocation LabelLoc;
1255 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1256 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1258 /// \brief Build an empty goto statement.
1259 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1261 LabelDecl *getLabel() const { return Label; }
1262 void setLabel(LabelDecl *D) { Label = D; }
1264 SourceLocation getGotoLoc() const { return GotoLoc; }
1265 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1266 SourceLocation getLabelLoc() const { return LabelLoc; }
1267 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1269 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1270 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1272 static bool classof(const Stmt *T) {
1273 return T->getStmtClass() == GotoStmtClass;
1277 child_range children() {
1278 return child_range(child_iterator(), child_iterator());
1282 /// IndirectGotoStmt - This represents an indirect goto.
1284 class IndirectGotoStmt : public Stmt {
1285 SourceLocation GotoLoc;
1286 SourceLocation StarLoc;
1289 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1291 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1292 Target((Stmt*)target) {}
1294 /// \brief Build an empty indirect goto statement.
1295 explicit IndirectGotoStmt(EmptyShell Empty)
1296 : Stmt(IndirectGotoStmtClass, Empty) { }
1298 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1299 SourceLocation getGotoLoc() const { return GotoLoc; }
1300 void setStarLoc(SourceLocation L) { StarLoc = L; }
1301 SourceLocation getStarLoc() const { return StarLoc; }
1303 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1304 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1305 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1307 /// getConstantTarget - Returns the fixed target of this indirect
1308 /// goto, if one exists.
1309 LabelDecl *getConstantTarget();
1310 const LabelDecl *getConstantTarget() const {
1311 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1314 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1315 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1317 static bool classof(const Stmt *T) {
1318 return T->getStmtClass() == IndirectGotoStmtClass;
1322 child_range children() { return child_range(&Target, &Target+1); }
1326 /// ContinueStmt - This represents a continue.
1328 class ContinueStmt : public Stmt {
1329 SourceLocation ContinueLoc;
1331 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1333 /// \brief Build an empty continue statement.
1334 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1336 SourceLocation getContinueLoc() const { return ContinueLoc; }
1337 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1339 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1340 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1342 static bool classof(const Stmt *T) {
1343 return T->getStmtClass() == ContinueStmtClass;
1347 child_range children() {
1348 return child_range(child_iterator(), child_iterator());
1352 /// BreakStmt - This represents a break.
1354 class BreakStmt : public Stmt {
1355 SourceLocation BreakLoc;
1358 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {
1359 static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation),
1360 "BreakStmt too large");
1363 /// \brief Build an empty break statement.
1364 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1366 SourceLocation getBreakLoc() const { return BreakLoc; }
1367 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1369 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1370 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1372 static bool classof(const Stmt *T) {
1373 return T->getStmtClass() == BreakStmtClass;
1377 child_range children() {
1378 return child_range(child_iterator(), child_iterator());
1383 /// ReturnStmt - This represents a return, optionally of an expression:
1387 /// Note that GCC allows return with no argument in a function declared to
1388 /// return a value, and it allows returning a value in functions declared to
1389 /// return void. We explicitly model this in the AST, which means you can't
1390 /// depend on the return type of the function and the presence of an argument.
1392 class ReturnStmt : public Stmt {
1393 SourceLocation RetLoc;
1395 const VarDecl *NRVOCandidate;
1398 explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {}
1400 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1401 : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E),
1402 NRVOCandidate(NRVOCandidate) {}
1404 /// \brief Build an empty return expression.
1405 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1407 const Expr *getRetValue() const;
1408 Expr *getRetValue();
1409 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1411 SourceLocation getReturnLoc() const { return RetLoc; }
1412 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1414 /// \brief Retrieve the variable that might be used for the named return
1415 /// value optimization.
1417 /// The optimization itself can only be performed if the variable is
1418 /// also marked as an NRVO object.
1419 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1420 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1422 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1423 SourceLocation getLocEnd() const LLVM_READONLY {
1424 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1427 static bool classof(const Stmt *T) {
1428 return T->getStmtClass() == ReturnStmtClass;
1432 child_range children() {
1433 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1434 return child_range(child_iterator(), child_iterator());
1438 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1440 class AsmStmt : public Stmt {
1442 SourceLocation AsmLoc;
1443 /// \brief True if the assembly statement does not have any input or output
1447 /// \brief If true, treat this inline assembly as having side effects.
1448 /// This assembly statement should not be optimized, deleted or moved.
1451 unsigned NumOutputs;
1453 unsigned NumClobbers;
1457 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1458 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) :
1459 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1460 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { }
1462 friend class ASTStmtReader;
1465 /// \brief Build an empty inline-assembly statement.
1466 explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1467 Stmt(SC, Empty), Exprs(nullptr) { }
1469 SourceLocation getAsmLoc() const { return AsmLoc; }
1470 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1472 bool isSimple() const { return IsSimple; }
1473 void setSimple(bool V) { IsSimple = V; }
1475 bool isVolatile() const { return IsVolatile; }
1476 void setVolatile(bool V) { IsVolatile = V; }
1478 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1479 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1481 //===--- Asm String Analysis ---===//
1483 /// Assemble final IR asm string.
1484 std::string generateAsmString(const ASTContext &C) const;
1486 //===--- Output operands ---===//
1488 unsigned getNumOutputs() const { return NumOutputs; }
1490 /// getOutputConstraint - Return the constraint string for the specified
1491 /// output operand. All output constraints are known to be non-empty (either
1493 StringRef getOutputConstraint(unsigned i) const;
1495 /// isOutputPlusConstraint - Return true if the specified output constraint
1496 /// is a "+" constraint (which is both an input and an output) or false if it
1497 /// is an "=" constraint (just an output).
1498 bool isOutputPlusConstraint(unsigned i) const {
1499 return getOutputConstraint(i)[0] == '+';
1502 const Expr *getOutputExpr(unsigned i) const;
1504 /// getNumPlusOperands - Return the number of output operands that have a "+"
1506 unsigned getNumPlusOperands() const;
1508 //===--- Input operands ---===//
1510 unsigned getNumInputs() const { return NumInputs; }
1512 /// getInputConstraint - Return the specified input constraint. Unlike output
1513 /// constraints, these can be empty.
1514 StringRef getInputConstraint(unsigned i) const;
1516 const Expr *getInputExpr(unsigned i) const;
1518 //===--- Other ---===//
1520 unsigned getNumClobbers() const { return NumClobbers; }
1521 StringRef getClobber(unsigned i) const;
1523 static bool classof(const Stmt *T) {
1524 return T->getStmtClass() == GCCAsmStmtClass ||
1525 T->getStmtClass() == MSAsmStmtClass;
1528 // Input expr iterators.
1530 typedef ExprIterator inputs_iterator;
1531 typedef ConstExprIterator const_inputs_iterator;
1532 typedef llvm::iterator_range<inputs_iterator> inputs_range;
1533 typedef llvm::iterator_range<const_inputs_iterator> inputs_const_range;
1535 inputs_iterator begin_inputs() {
1536 return &Exprs[0] + NumOutputs;
1539 inputs_iterator end_inputs() {
1540 return &Exprs[0] + NumOutputs + NumInputs;
1543 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1545 const_inputs_iterator begin_inputs() const {
1546 return &Exprs[0] + NumOutputs;
1549 const_inputs_iterator end_inputs() const {
1550 return &Exprs[0] + NumOutputs + NumInputs;
1553 inputs_const_range inputs() const {
1554 return inputs_const_range(begin_inputs(), end_inputs());
1557 // Output expr iterators.
1559 typedef ExprIterator outputs_iterator;
1560 typedef ConstExprIterator const_outputs_iterator;
1561 typedef llvm::iterator_range<outputs_iterator> outputs_range;
1562 typedef llvm::iterator_range<const_outputs_iterator> outputs_const_range;
1564 outputs_iterator begin_outputs() {
1567 outputs_iterator end_outputs() {
1568 return &Exprs[0] + NumOutputs;
1570 outputs_range outputs() {
1571 return outputs_range(begin_outputs(), end_outputs());
1574 const_outputs_iterator begin_outputs() const {
1577 const_outputs_iterator end_outputs() const {
1578 return &Exprs[0] + NumOutputs;
1580 outputs_const_range outputs() const {
1581 return outputs_const_range(begin_outputs(), end_outputs());
1584 child_range children() {
1585 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1589 /// This represents a GCC inline-assembly statement extension.
1591 class GCCAsmStmt : public AsmStmt {
1592 SourceLocation RParenLoc;
1593 StringLiteral *AsmStr;
1595 // FIXME: If we wanted to, we could allocate all of these in one big array.
1596 StringLiteral **Constraints;
1597 StringLiteral **Clobbers;
1598 IdentifierInfo **Names;
1600 friend class ASTStmtReader;
1603 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1604 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1605 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1606 StringLiteral *asmstr, unsigned numclobbers,
1607 StringLiteral **clobbers, SourceLocation rparenloc);
1609 /// \brief Build an empty inline-assembly statement.
1610 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1611 Constraints(nullptr), Clobbers(nullptr), Names(nullptr) { }
1613 SourceLocation getRParenLoc() const { return RParenLoc; }
1614 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1616 //===--- Asm String Analysis ---===//
1618 const StringLiteral *getAsmString() const { return AsmStr; }
1619 StringLiteral *getAsmString() { return AsmStr; }
1620 void setAsmString(StringLiteral *E) { AsmStr = E; }
1622 /// AsmStringPiece - this is part of a decomposed asm string specification
1623 /// (for use with the AnalyzeAsmString function below). An asm string is
1624 /// considered to be a concatenation of these parts.
1625 class AsmStringPiece {
1628 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1629 Operand // Operand reference, with optional modifier %c4.
1636 // Source range for operand references.
1637 CharSourceRange Range;
1639 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1640 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
1642 : MyKind(Operand), Str(S), OperandNo(OpNo),
1643 Range(CharSourceRange::getCharRange(Begin, End)) {
1646 bool isString() const { return MyKind == String; }
1647 bool isOperand() const { return MyKind == Operand; }
1649 const std::string &getString() const {
1653 unsigned getOperandNo() const {
1654 assert(isOperand());
1658 CharSourceRange getRange() const {
1659 assert(isOperand() && "Range is currently used only for Operands.");
1663 /// getModifier - Get the modifier for this operand, if present. This
1664 /// returns '\0' if there was no modifier.
1665 char getModifier() const;
1668 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1669 /// it into pieces. If the asm string is erroneous, emit errors and return
1670 /// true, otherwise return false. This handles canonicalization and
1671 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1672 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1673 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1674 const ASTContext &C, unsigned &DiagOffs) const;
1676 /// Assemble final IR asm string.
1677 std::string generateAsmString(const ASTContext &C) const;
1679 //===--- Output operands ---===//
1681 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1685 StringRef getOutputName(unsigned i) const {
1686 if (IdentifierInfo *II = getOutputIdentifier(i))
1687 return II->getName();
1692 StringRef getOutputConstraint(unsigned i) const;
1694 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1695 return Constraints[i];
1697 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1698 return Constraints[i];
1701 Expr *getOutputExpr(unsigned i);
1703 const Expr *getOutputExpr(unsigned i) const {
1704 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1707 //===--- Input operands ---===//
1709 IdentifierInfo *getInputIdentifier(unsigned i) const {
1710 return Names[i + NumOutputs];
1713 StringRef getInputName(unsigned i) const {
1714 if (IdentifierInfo *II = getInputIdentifier(i))
1715 return II->getName();
1720 StringRef getInputConstraint(unsigned i) const;
1722 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1723 return Constraints[i + NumOutputs];
1725 StringLiteral *getInputConstraintLiteral(unsigned i) {
1726 return Constraints[i + NumOutputs];
1729 Expr *getInputExpr(unsigned i);
1730 void setInputExpr(unsigned i, Expr *E);
1732 const Expr *getInputExpr(unsigned i) const {
1733 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1737 void setOutputsAndInputsAndClobbers(const ASTContext &C,
1738 IdentifierInfo **Names,
1739 StringLiteral **Constraints,
1741 unsigned NumOutputs,
1743 StringLiteral **Clobbers,
1744 unsigned NumClobbers);
1747 //===--- Other ---===//
1749 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1750 /// translate this into a numeric value needed to reference the same operand.
1751 /// This returns -1 if the operand name is invalid.
1752 int getNamedOperand(StringRef SymbolicName) const;
1754 StringRef getClobber(unsigned i) const;
1755 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1756 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1760 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1761 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1763 static bool classof(const Stmt *T) {
1764 return T->getStmtClass() == GCCAsmStmtClass;
1768 /// This represents a Microsoft inline-assembly statement extension.
1770 class MSAsmStmt : public AsmStmt {
1771 SourceLocation LBraceLoc, EndLoc;
1774 unsigned NumAsmToks;
1777 StringRef *Constraints;
1778 StringRef *Clobbers;
1780 friend class ASTStmtReader;
1783 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1784 SourceLocation lbraceloc, bool issimple, bool isvolatile,
1785 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1786 ArrayRef<StringRef> constraints,
1787 ArrayRef<Expr*> exprs, StringRef asmstr,
1788 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1790 /// \brief Build an empty MS-style inline-assembly statement.
1791 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1792 NumAsmToks(0), AsmToks(nullptr), Constraints(nullptr), Clobbers(nullptr) { }
1794 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1795 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1796 SourceLocation getEndLoc() const { return EndLoc; }
1797 void setEndLoc(SourceLocation L) { EndLoc = L; }
1799 bool hasBraces() const { return LBraceLoc.isValid(); }
1801 unsigned getNumAsmToks() { return NumAsmToks; }
1802 Token *getAsmToks() { return AsmToks; }
1804 //===--- Asm String Analysis ---===//
1805 StringRef getAsmString() const { return AsmStr; }
1807 /// Assemble final IR asm string.
1808 std::string generateAsmString(const ASTContext &C) const;
1810 //===--- Output operands ---===//
1812 StringRef getOutputConstraint(unsigned i) const {
1813 assert(i < NumOutputs);
1814 return Constraints[i];
1817 Expr *getOutputExpr(unsigned i);
1819 const Expr *getOutputExpr(unsigned i) const {
1820 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1823 //===--- Input operands ---===//
1825 StringRef getInputConstraint(unsigned i) const {
1826 assert(i < NumInputs);
1827 return Constraints[i + NumOutputs];
1830 Expr *getInputExpr(unsigned i);
1831 void setInputExpr(unsigned i, Expr *E);
1833 const Expr *getInputExpr(unsigned i) const {
1834 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1837 //===--- Other ---===//
1839 ArrayRef<StringRef> getAllConstraints() const {
1840 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
1842 ArrayRef<StringRef> getClobbers() const {
1843 return llvm::makeArrayRef(Clobbers, NumClobbers);
1845 ArrayRef<Expr*> getAllExprs() const {
1846 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
1847 NumInputs + NumOutputs);
1850 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1853 void initialize(const ASTContext &C, StringRef AsmString,
1854 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1855 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1858 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1859 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1861 static bool classof(const Stmt *T) {
1862 return T->getStmtClass() == MSAsmStmtClass;
1865 child_range children() {
1866 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1870 class SEHExceptStmt : public Stmt {
1874 enum { FILTER_EXPR, BLOCK };
1876 SEHExceptStmt(SourceLocation Loc,
1880 friend class ASTReader;
1881 friend class ASTStmtReader;
1882 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1885 static SEHExceptStmt* Create(const ASTContext &C,
1886 SourceLocation ExceptLoc,
1890 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1891 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1893 SourceLocation getExceptLoc() const { return Loc; }
1894 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1896 Expr *getFilterExpr() const {
1897 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1900 CompoundStmt *getBlock() const {
1901 return cast<CompoundStmt>(Children[BLOCK]);
1904 child_range children() {
1905 return child_range(Children,Children+2);
1908 static bool classof(const Stmt *T) {
1909 return T->getStmtClass() == SEHExceptStmtClass;
1914 class SEHFinallyStmt : public Stmt {
1918 SEHFinallyStmt(SourceLocation Loc,
1921 friend class ASTReader;
1922 friend class ASTStmtReader;
1923 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1926 static SEHFinallyStmt* Create(const ASTContext &C,
1927 SourceLocation FinallyLoc,
1930 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1931 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1933 SourceLocation getFinallyLoc() const { return Loc; }
1934 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1936 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1938 child_range children() {
1939 return child_range(&Block,&Block+1);
1942 static bool classof(const Stmt *T) {
1943 return T->getStmtClass() == SEHFinallyStmtClass;
1948 class SEHTryStmt : public Stmt {
1950 SourceLocation TryLoc;
1953 enum { TRY = 0, HANDLER = 1 };
1955 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1956 SourceLocation TryLoc,
1960 friend class ASTReader;
1961 friend class ASTStmtReader;
1962 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1965 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
1966 SourceLocation TryLoc, Stmt *TryBlock,
1969 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1970 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1972 SourceLocation getTryLoc() const { return TryLoc; }
1973 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1975 bool getIsCXXTry() const { return IsCXXTry; }
1977 CompoundStmt* getTryBlock() const {
1978 return cast<CompoundStmt>(Children[TRY]);
1981 Stmt *getHandler() const { return Children[HANDLER]; }
1983 /// Returns 0 if not defined
1984 SEHExceptStmt *getExceptHandler() const;
1985 SEHFinallyStmt *getFinallyHandler() const;
1987 child_range children() {
1988 return child_range(Children,Children+2);
1991 static bool classof(const Stmt *T) {
1992 return T->getStmtClass() == SEHTryStmtClass;
1996 /// Represents a __leave statement.
1998 class SEHLeaveStmt : public Stmt {
1999 SourceLocation LeaveLoc;
2001 explicit SEHLeaveStmt(SourceLocation LL)
2002 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
2004 /// \brief Build an empty __leave statement.
2005 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) { }
2007 SourceLocation getLeaveLoc() const { return LeaveLoc; }
2008 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
2010 SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; }
2011 SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; }
2013 static bool classof(const Stmt *T) {
2014 return T->getStmtClass() == SEHLeaveStmtClass;
2018 child_range children() {
2019 return child_range(child_iterator(), child_iterator());
2023 /// \brief This captures a statement into a function. For example, the following
2024 /// pragma annotated compound statement can be represented as a CapturedStmt,
2025 /// and this compound statement is the body of an anonymous outlined function.
2027 /// #pragma omp parallel
2032 class CapturedStmt : public Stmt {
2034 /// \brief The different capture forms: by 'this', by reference, capture for
2035 /// variable-length array type etc.
2036 enum VariableCaptureKind {
2043 /// \brief Describes the capture of either a variable, or 'this', or
2044 /// variable-length array type.
2046 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2050 /// \brief Create a new capture.
2052 /// \param Loc The source location associated with this capture.
2054 /// \param Kind The kind of capture (this, ByRef, ...).
2056 /// \param Var The variable being captured, or null if capturing this.
2058 Capture(SourceLocation Loc, VariableCaptureKind Kind,
2059 VarDecl *Var = nullptr);
2061 /// \brief Determine the kind of capture.
2062 VariableCaptureKind getCaptureKind() const;
2064 /// \brief Retrieve the source location at which the variable or 'this' was
2066 SourceLocation getLocation() const { return Loc; }
2068 /// \brief Determine whether this capture handles the C++ 'this' pointer.
2069 bool capturesThis() const { return getCaptureKind() == VCK_This; }
2071 /// \brief Determine whether this capture handles a variable (by reference).
2072 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2074 /// \brief Determine whether this capture handles a variable by copy.
2075 bool capturesVariableByCopy() const {
2076 return getCaptureKind() == VCK_ByCopy;
2079 /// \brief Determine whether this capture handles a variable-length array
2081 bool capturesVariableArrayType() const {
2082 return getCaptureKind() == VCK_VLAType;
2085 /// \brief Retrieve the declaration of the variable being captured.
2087 /// This operation is only valid if this capture captures a variable.
2088 VarDecl *getCapturedVar() const;
2090 friend class ASTStmtReader;
2094 /// \brief The number of variable captured, including 'this'.
2095 unsigned NumCaptures;
2097 /// \brief The pointer part is the implicit the outlined function and the
2098 /// int part is the captured region kind, 'CR_Default' etc.
2099 llvm::PointerIntPair<CapturedDecl *, 1, CapturedRegionKind> CapDeclAndKind;
2101 /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl.
2102 RecordDecl *TheRecordDecl;
2104 /// \brief Construct a captured statement.
2105 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2106 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2108 /// \brief Construct an empty captured statement.
2109 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2111 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
2113 Stmt *const *getStoredStmts() const {
2114 return reinterpret_cast<Stmt *const *>(this + 1);
2117 Capture *getStoredCaptures() const;
2119 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2122 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2123 CapturedRegionKind Kind,
2124 ArrayRef<Capture> Captures,
2125 ArrayRef<Expr *> CaptureInits,
2126 CapturedDecl *CD, RecordDecl *RD);
2128 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2129 unsigned NumCaptures);
2131 /// \brief Retrieve the statement being captured.
2132 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2133 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
2135 /// \brief Retrieve the outlined function declaration.
2136 CapturedDecl *getCapturedDecl();
2137 const CapturedDecl *getCapturedDecl() const;
2139 /// \brief Set the outlined function declaration.
2140 void setCapturedDecl(CapturedDecl *D);
2142 /// \brief Retrieve the captured region kind.
2143 CapturedRegionKind getCapturedRegionKind() const;
2145 /// \brief Set the captured region kind.
2146 void setCapturedRegionKind(CapturedRegionKind Kind);
2148 /// \brief Retrieve the record declaration for captured variables.
2149 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2151 /// \brief Set the record declaration for captured variables.
2152 void setCapturedRecordDecl(RecordDecl *D) {
2153 assert(D && "null RecordDecl");
2157 /// \brief True if this variable has been captured.
2158 bool capturesVariable(const VarDecl *Var) const;
2160 /// \brief An iterator that walks over the captures.
2161 typedef Capture *capture_iterator;
2162 typedef const Capture *const_capture_iterator;
2163 typedef llvm::iterator_range<capture_iterator> capture_range;
2164 typedef llvm::iterator_range<const_capture_iterator> capture_const_range;
2166 capture_range captures() {
2167 return capture_range(capture_begin(), capture_end());
2169 capture_const_range captures() const {
2170 return capture_const_range(capture_begin(), capture_end());
2173 /// \brief Retrieve an iterator pointing to the first capture.
2174 capture_iterator capture_begin() { return getStoredCaptures(); }
2175 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2177 /// \brief Retrieve an iterator pointing past the end of the sequence of
2179 capture_iterator capture_end() const {
2180 return getStoredCaptures() + NumCaptures;
2183 /// \brief Retrieve the number of captures, including 'this'.
2184 unsigned capture_size() const { return NumCaptures; }
2186 /// \brief Iterator that walks over the capture initialization arguments.
2187 typedef Expr **capture_init_iterator;
2188 typedef llvm::iterator_range<capture_init_iterator> capture_init_range;
2190 /// \brief Const iterator that walks over the capture initialization
2192 typedef Expr *const *const_capture_init_iterator;
2193 typedef llvm::iterator_range<const_capture_init_iterator>
2194 const_capture_init_range;
2196 capture_init_range capture_inits() {
2197 return capture_init_range(capture_init_begin(), capture_init_end());
2200 const_capture_init_range capture_inits() const {
2201 return const_capture_init_range(capture_init_begin(), capture_init_end());
2204 /// \brief Retrieve the first initialization argument.
2205 capture_init_iterator capture_init_begin() {
2206 return reinterpret_cast<Expr **>(getStoredStmts());
2209 const_capture_init_iterator capture_init_begin() const {
2210 return reinterpret_cast<Expr *const *>(getStoredStmts());
2213 /// \brief Retrieve the iterator pointing one past the last initialization
2215 capture_init_iterator capture_init_end() {
2216 return capture_init_begin() + NumCaptures;
2219 const_capture_init_iterator capture_init_end() const {
2220 return capture_init_begin() + NumCaptures;
2223 SourceLocation getLocStart() const LLVM_READONLY {
2224 return getCapturedStmt()->getLocStart();
2226 SourceLocation getLocEnd() const LLVM_READONLY {
2227 return getCapturedStmt()->getLocEnd();
2229 SourceRange getSourceRange() const LLVM_READONLY {
2230 return getCapturedStmt()->getSourceRange();
2233 static bool classof(const Stmt *T) {
2234 return T->getStmtClass() == CapturedStmtClass;
2237 child_range children();
2239 friend class ASTStmtReader;
2242 } // end namespace clang