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/Support/Compiler.h"
26 #include "llvm/Support/ErrorHandling.h"
30 class FoldingSetNodeID;
43 struct PrintingPolicy;
52 //===--------------------------------------------------------------------===//
53 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
54 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
55 // references to children (to be compatible with StmtIterator).
56 //===--------------------------------------------------------------------===//
61 class ExprIterator : public std::iterator<std::forward_iterator_tag,
66 ExprIterator(Stmt** i) : I(i) {}
67 ExprIterator() : I(nullptr) {}
68 ExprIterator& operator++() { ++I; return *this; }
69 ExprIterator operator-(size_t i) { return I-i; }
70 ExprIterator operator+(size_t i) { return I+i; }
71 Expr* operator[](size_t idx);
72 // FIXME: Verify that this will correctly return a signed distance.
73 signed operator-(const ExprIterator& R) const { return I - R.I; }
74 Expr* operator*() const;
75 Expr* operator->() const;
76 bool operator==(const ExprIterator& R) const { return I == R.I; }
77 bool operator!=(const ExprIterator& R) const { return I != R.I; }
78 bool operator>(const ExprIterator& R) const { return I > R.I; }
79 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
82 class ConstExprIterator : public std::iterator<std::forward_iterator_tag,
83 const Expr *&, ptrdiff_t,
86 const Stmt * const *I;
88 ConstExprIterator(const Stmt * const *i) : I(i) {}
89 ConstExprIterator() : I(nullptr) {}
90 ConstExprIterator& operator++() { ++I; return *this; }
91 ConstExprIterator operator+(size_t i) const { return I+i; }
92 ConstExprIterator operator-(size_t i) const { return I-i; }
93 const Expr * operator[](size_t idx) const;
94 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
95 const Expr * operator*() const;
96 const Expr * operator->() const;
97 bool operator==(const ConstExprIterator& R) const { return I == R.I; }
98 bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
99 bool operator>(const ConstExprIterator& R) const { return I > R.I; }
100 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
103 //===----------------------------------------------------------------------===//
104 // AST classes for statements.
105 //===----------------------------------------------------------------------===//
107 /// Stmt - This represents one statement.
109 class LLVM_ALIGNAS(LLVM_PTR_SIZE) Stmt {
113 #define STMT(CLASS, PARENT) CLASS##Class,
114 #define STMT_RANGE(BASE, FIRST, LAST) \
115 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
116 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
117 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
118 #define ABSTRACT_STMT(STMT)
119 #include "clang/AST/StmtNodes.inc"
122 // Make vanilla 'new' and 'delete' illegal for Stmts.
124 void* operator new(size_t bytes) throw() {
125 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
127 void operator delete(void* data) throw() {
128 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
131 class StmtBitfields {
134 /// \brief The statement class.
137 enum { NumStmtBits = 8 };
139 class CompoundStmtBitfields {
140 friend class CompoundStmt;
141 unsigned : NumStmtBits;
143 unsigned NumStmts : 32 - NumStmtBits;
146 class ExprBitfields {
148 friend class DeclRefExpr; // computeDependence
149 friend class InitListExpr; // ctor
150 friend class DesignatedInitExpr; // ctor
151 friend class BlockDeclRefExpr; // ctor
152 friend class ASTStmtReader; // deserialization
153 friend class CXXNewExpr; // ctor
154 friend class DependentScopeDeclRefExpr; // ctor
155 friend class CXXConstructExpr; // ctor
156 friend class CallExpr; // ctor
157 friend class OffsetOfExpr; // ctor
158 friend class ObjCMessageExpr; // ctor
159 friend class ObjCArrayLiteral; // ctor
160 friend class ObjCDictionaryLiteral; // ctor
161 friend class ShuffleVectorExpr; // ctor
162 friend class ParenListExpr; // ctor
163 friend class CXXUnresolvedConstructExpr; // ctor
164 friend class CXXDependentScopeMemberExpr; // ctor
165 friend class OverloadExpr; // ctor
166 friend class PseudoObjectExpr; // ctor
167 friend class AtomicExpr; // ctor
168 unsigned : NumStmtBits;
170 unsigned ValueKind : 2;
171 unsigned ObjectKind : 2;
172 unsigned TypeDependent : 1;
173 unsigned ValueDependent : 1;
174 unsigned InstantiationDependent : 1;
175 unsigned ContainsUnexpandedParameterPack : 1;
177 enum { NumExprBits = 16 };
179 class CharacterLiteralBitfields {
180 friend class CharacterLiteral;
181 unsigned : NumExprBits;
186 enum APFloatSemantics {
195 class FloatingLiteralBitfields {
196 friend class FloatingLiteral;
197 unsigned : NumExprBits;
199 unsigned Semantics : 3; // Provides semantics for APFloat construction
200 unsigned IsExact : 1;
203 class UnaryExprOrTypeTraitExprBitfields {
204 friend class UnaryExprOrTypeTraitExpr;
205 unsigned : NumExprBits;
208 unsigned IsType : 1; // true if operand is a type, false if an expression.
211 class DeclRefExprBitfields {
212 friend class DeclRefExpr;
213 friend class ASTStmtReader; // deserialization
214 unsigned : NumExprBits;
216 unsigned HasQualifier : 1;
217 unsigned HasTemplateKWAndArgsInfo : 1;
218 unsigned HasFoundDecl : 1;
219 unsigned HadMultipleCandidates : 1;
220 unsigned RefersToEnclosingVariableOrCapture : 1;
223 class CastExprBitfields {
224 friend class CastExpr;
225 unsigned : NumExprBits;
228 unsigned BasePathSize : 32 - 6 - NumExprBits;
231 class CallExprBitfields {
232 friend class CallExpr;
233 unsigned : NumExprBits;
235 unsigned NumPreArgs : 1;
238 class ExprWithCleanupsBitfields {
239 friend class ExprWithCleanups;
240 friend class ASTStmtReader; // deserialization
242 unsigned : NumExprBits;
244 unsigned NumObjects : 32 - NumExprBits;
247 class PseudoObjectExprBitfields {
248 friend class PseudoObjectExpr;
249 friend class ASTStmtReader; // deserialization
251 unsigned : NumExprBits;
253 // These don't need to be particularly wide, because they're
254 // strictly limited by the forms of expressions we permit.
255 unsigned NumSubExprs : 8;
256 unsigned ResultIndex : 32 - 8 - NumExprBits;
259 class ObjCIndirectCopyRestoreExprBitfields {
260 friend class ObjCIndirectCopyRestoreExpr;
261 unsigned : NumExprBits;
263 unsigned ShouldCopy : 1;
266 class InitListExprBitfields {
267 friend class InitListExpr;
269 unsigned : NumExprBits;
271 /// Whether this initializer list originally had a GNU array-range
272 /// designator in it. This is a temporary marker used by CodeGen.
273 unsigned HadArrayRangeDesignator : 1;
276 class TypeTraitExprBitfields {
277 friend class TypeTraitExpr;
278 friend class ASTStmtReader;
279 friend class ASTStmtWriter;
281 unsigned : NumExprBits;
283 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
286 /// \brief If this expression is not value-dependent, this indicates whether
287 /// the trait evaluated true or false.
290 /// \brief The number of arguments to this type trait.
291 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
295 StmtBitfields StmtBits;
296 CompoundStmtBitfields CompoundStmtBits;
297 ExprBitfields ExprBits;
298 CharacterLiteralBitfields CharacterLiteralBits;
299 FloatingLiteralBitfields FloatingLiteralBits;
300 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
301 DeclRefExprBitfields DeclRefExprBits;
302 CastExprBitfields CastExprBits;
303 CallExprBitfields CallExprBits;
304 ExprWithCleanupsBitfields ExprWithCleanupsBits;
305 PseudoObjectExprBitfields PseudoObjectExprBits;
306 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
307 InitListExprBitfields InitListExprBits;
308 TypeTraitExprBitfields TypeTraitExprBits;
311 friend class ASTStmtReader;
312 friend class ASTStmtWriter;
315 // Only allow allocation of Stmts using the allocator in ASTContext
316 // or by doing a placement new.
317 void* operator new(size_t bytes, const ASTContext& C,
318 unsigned alignment = 8);
320 void* operator new(size_t bytes, const ASTContext* C,
321 unsigned alignment = 8) {
322 return operator new(bytes, *C, alignment);
325 void* operator new(size_t bytes, void* mem) throw() {
329 void operator delete(void*, const ASTContext&, unsigned) throw() { }
330 void operator delete(void*, const ASTContext*, unsigned) throw() { }
331 void operator delete(void*, size_t) throw() { }
332 void operator delete(void*, void*) throw() { }
335 /// \brief A placeholder type used to construct an empty shell of a
336 /// type, that will be filled in later (e.g., by some
337 /// de-serialization).
338 struct EmptyShell { };
341 /// \brief Whether statistic collection is enabled.
342 static bool StatisticsEnabled;
345 /// \brief Construct an empty statement.
346 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
350 static_assert(sizeof(*this) % llvm::AlignOf<void *>::Alignment == 0,
351 "Insufficient alignment!");
352 StmtBits.sClass = SC;
353 if (StatisticsEnabled) Stmt::addStmtClass(SC);
356 StmtClass getStmtClass() const {
357 return static_cast<StmtClass>(StmtBits.sClass);
359 const char *getStmtClassName() const;
361 /// SourceLocation tokens are not useful in isolation - they are low level
362 /// value objects created/interpreted by SourceManager. We assume AST
363 /// clients will have a pointer to the respective SourceManager.
364 SourceRange getSourceRange() const LLVM_READONLY;
365 SourceLocation getLocStart() const LLVM_READONLY;
366 SourceLocation getLocEnd() const LLVM_READONLY;
368 // global temp stats (until we have a per-module visitor)
369 static void addStmtClass(const StmtClass s);
370 static void EnableStatistics();
371 static void PrintStats();
373 /// \brief Dumps the specified AST fragment and all subtrees to
376 void dump(SourceManager &SM) const;
377 void dump(raw_ostream &OS, SourceManager &SM) const;
378 void dump(raw_ostream &OS) const;
380 /// dumpColor - same as dump(), but forces color highlighting.
381 void dumpColor() const;
383 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
384 /// back to its original source language syntax.
385 void dumpPretty(const ASTContext &Context) const;
386 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
387 const PrintingPolicy &Policy,
388 unsigned Indentation = 0) const;
390 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
391 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
392 void viewAST() const;
394 /// Skip past any implicit AST nodes which might surround this
395 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
396 Stmt *IgnoreImplicit();
398 /// \brief Skip no-op (attributed, compound) container stmts and skip captured
399 /// stmt at the top, if \a IgnoreCaptured is true.
400 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
402 const Stmt *stripLabelLikeStatements() const;
403 Stmt *stripLabelLikeStatements() {
404 return const_cast<Stmt*>(
405 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
408 /// Child Iterators: All subclasses must implement 'children'
409 /// to permit easy iteration over the substatements/subexpessions of an
410 /// AST node. This permits easy iteration over all nodes in the AST.
411 typedef StmtIterator child_iterator;
412 typedef ConstStmtIterator const_child_iterator;
414 typedef StmtRange child_range;
415 typedef ConstStmtRange const_child_range;
417 child_range children();
418 const_child_range children() const {
419 return const_cast<Stmt*>(this)->children();
422 child_iterator child_begin() { return children().first; }
423 child_iterator child_end() { return children().second; }
425 const_child_iterator child_begin() const { return children().first; }
426 const_child_iterator child_end() const { return children().second; }
428 /// \brief Produce a unique representation of the given statement.
430 /// \param ID once the profiling operation is complete, will contain
431 /// the unique representation of the given statement.
433 /// \param Context the AST context in which the statement resides
435 /// \param Canonical whether the profile should be based on the canonical
436 /// representation of this statement (e.g., where non-type template
437 /// parameters are identified by index/level rather than their
438 /// declaration pointers) or the exact representation of the statement as
439 /// written in the source.
440 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
441 bool Canonical) const;
444 /// DeclStmt - Adaptor class for mixing declarations with statements and
445 /// expressions. For example, CompoundStmt mixes statements, expressions
446 /// and declarations (variables, types). Another example is ForStmt, where
447 /// the first statement can be an expression or a declaration.
449 class DeclStmt : public Stmt {
451 SourceLocation StartLoc, EndLoc;
454 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
455 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
456 StartLoc(startLoc), EndLoc(endLoc) {}
458 /// \brief Build an empty declaration statement.
459 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
461 /// isSingleDecl - This method returns true if this DeclStmt refers
462 /// to a single Decl.
463 bool isSingleDecl() const {
464 return DG.isSingleDecl();
467 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
468 Decl *getSingleDecl() { return DG.getSingleDecl(); }
470 const DeclGroupRef getDeclGroup() const { return DG; }
471 DeclGroupRef getDeclGroup() { return DG; }
472 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
474 SourceLocation getStartLoc() const { return StartLoc; }
475 void setStartLoc(SourceLocation L) { StartLoc = L; }
476 SourceLocation getEndLoc() const { return EndLoc; }
477 void setEndLoc(SourceLocation L) { EndLoc = L; }
479 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
480 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
482 static bool classof(const Stmt *T) {
483 return T->getStmtClass() == DeclStmtClass;
486 // Iterators over subexpressions.
487 child_range children() {
488 return child_range(child_iterator(DG.begin(), DG.end()),
489 child_iterator(DG.end(), DG.end()));
492 typedef DeclGroupRef::iterator decl_iterator;
493 typedef DeclGroupRef::const_iterator const_decl_iterator;
494 typedef llvm::iterator_range<decl_iterator> decl_range;
495 typedef llvm::iterator_range<const_decl_iterator> decl_const_range;
497 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
498 decl_const_range decls() const {
499 return decl_const_range(decl_begin(), decl_end());
501 decl_iterator decl_begin() { return DG.begin(); }
502 decl_iterator decl_end() { return DG.end(); }
503 const_decl_iterator decl_begin() const { return DG.begin(); }
504 const_decl_iterator decl_end() const { return DG.end(); }
506 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
507 reverse_decl_iterator decl_rbegin() {
508 return reverse_decl_iterator(decl_end());
510 reverse_decl_iterator decl_rend() {
511 return reverse_decl_iterator(decl_begin());
515 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
517 class NullStmt : public Stmt {
518 SourceLocation SemiLoc;
520 /// \brief True if the null statement was preceded by an empty macro, e.g:
525 bool HasLeadingEmptyMacro;
527 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
528 : Stmt(NullStmtClass), SemiLoc(L),
529 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
531 /// \brief Build an empty null statement.
532 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
533 HasLeadingEmptyMacro(false) { }
535 SourceLocation getSemiLoc() const { return SemiLoc; }
536 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
538 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
540 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
541 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
543 static bool classof(const Stmt *T) {
544 return T->getStmtClass() == NullStmtClass;
547 child_range children() { return child_range(); }
549 friend class ASTStmtReader;
550 friend class ASTStmtWriter;
553 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
555 class CompoundStmt : public Stmt {
557 SourceLocation LBraceLoc, RBraceLoc;
559 friend class ASTStmtReader;
562 CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
563 SourceLocation LB, SourceLocation RB);
565 // \brief Build an empty compound statement with a location.
566 explicit CompoundStmt(SourceLocation Loc)
567 : Stmt(CompoundStmtClass), Body(nullptr), LBraceLoc(Loc), RBraceLoc(Loc) {
568 CompoundStmtBits.NumStmts = 0;
571 // \brief Build an empty compound statement.
572 explicit CompoundStmt(EmptyShell Empty)
573 : Stmt(CompoundStmtClass, Empty), Body(nullptr) {
574 CompoundStmtBits.NumStmts = 0;
577 void setStmts(const ASTContext &C, Stmt **Stmts, unsigned NumStmts);
579 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
580 unsigned size() const { return CompoundStmtBits.NumStmts; }
582 typedef Stmt** body_iterator;
583 typedef llvm::iterator_range<body_iterator> body_range;
585 body_range body() { return body_range(body_begin(), body_end()); }
586 body_iterator body_begin() { return Body; }
587 body_iterator body_end() { return Body + size(); }
588 Stmt *body_front() { return !body_empty() ? Body[0] : nullptr; }
589 Stmt *body_back() { return !body_empty() ? Body[size()-1] : nullptr; }
591 void setLastStmt(Stmt *S) {
592 assert(!body_empty() && "setLastStmt");
596 typedef Stmt* const * const_body_iterator;
597 typedef llvm::iterator_range<const_body_iterator> body_const_range;
599 body_const_range body() const {
600 return body_const_range(body_begin(), body_end());
602 const_body_iterator body_begin() const { return Body; }
603 const_body_iterator body_end() const { return Body + size(); }
604 const Stmt *body_front() const {
605 return !body_empty() ? Body[0] : nullptr;
607 const Stmt *body_back() const {
608 return !body_empty() ? Body[size() - 1] : nullptr;
611 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
612 reverse_body_iterator body_rbegin() {
613 return reverse_body_iterator(body_end());
615 reverse_body_iterator body_rend() {
616 return reverse_body_iterator(body_begin());
619 typedef std::reverse_iterator<const_body_iterator>
620 const_reverse_body_iterator;
622 const_reverse_body_iterator body_rbegin() const {
623 return const_reverse_body_iterator(body_end());
626 const_reverse_body_iterator body_rend() const {
627 return const_reverse_body_iterator(body_begin());
630 SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; }
631 SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; }
633 SourceLocation getLBracLoc() const { return LBraceLoc; }
634 SourceLocation getRBracLoc() const { return RBraceLoc; }
636 static bool classof(const Stmt *T) {
637 return T->getStmtClass() == CompoundStmtClass;
641 child_range children() {
642 return child_range(Body, Body + CompoundStmtBits.NumStmts);
645 const_child_range children() const {
646 return child_range(Body, Body + CompoundStmtBits.NumStmts);
650 // SwitchCase is the base class for CaseStmt and DefaultStmt,
651 class SwitchCase : public Stmt {
653 // A pointer to the following CaseStmt or DefaultStmt class,
654 // used by SwitchStmt.
655 SwitchCase *NextSwitchCase;
656 SourceLocation KeywordLoc;
657 SourceLocation ColonLoc;
659 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
660 : Stmt(SC), NextSwitchCase(nullptr), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {
663 SwitchCase(StmtClass SC, EmptyShell)
664 : Stmt(SC), NextSwitchCase(nullptr) {}
667 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
669 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
671 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
673 SourceLocation getKeywordLoc() const { return KeywordLoc; }
674 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
675 SourceLocation getColonLoc() const { return ColonLoc; }
676 void setColonLoc(SourceLocation L) { ColonLoc = L; }
679 const Stmt *getSubStmt() const {
680 return const_cast<SwitchCase*>(this)->getSubStmt();
683 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
684 SourceLocation getLocEnd() const LLVM_READONLY;
686 static bool classof(const Stmt *T) {
687 return T->getStmtClass() == CaseStmtClass ||
688 T->getStmtClass() == DefaultStmtClass;
692 class CaseStmt : public SwitchCase {
693 SourceLocation EllipsisLoc;
694 enum { LHS, RHS, SUBSTMT, END_EXPR };
695 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
696 // GNU "case 1 ... 4" extension
698 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
699 SourceLocation ellipsisLoc, SourceLocation colonLoc)
700 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
701 SubExprs[SUBSTMT] = nullptr;
702 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
703 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
704 EllipsisLoc = ellipsisLoc;
707 /// \brief Build an empty switch case statement.
708 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) { }
710 SourceLocation getCaseLoc() const { return KeywordLoc; }
711 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
712 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
713 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
714 SourceLocation getColonLoc() const { return ColonLoc; }
715 void setColonLoc(SourceLocation L) { ColonLoc = L; }
717 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
718 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
719 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
721 const Expr *getLHS() const {
722 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
724 const Expr *getRHS() const {
725 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
727 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
729 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
730 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
731 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
733 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
734 SourceLocation getLocEnd() const LLVM_READONLY {
735 // Handle deeply nested case statements with iteration instead of recursion.
736 const CaseStmt *CS = this;
737 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
740 return CS->getSubStmt()->getLocEnd();
743 static bool classof(const Stmt *T) {
744 return T->getStmtClass() == CaseStmtClass;
748 child_range children() {
749 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
753 class DefaultStmt : public SwitchCase {
756 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
757 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
759 /// \brief Build an empty default statement.
760 explicit DefaultStmt(EmptyShell Empty)
761 : SwitchCase(DefaultStmtClass, Empty) { }
763 Stmt *getSubStmt() { return SubStmt; }
764 const Stmt *getSubStmt() const { return SubStmt; }
765 void setSubStmt(Stmt *S) { SubStmt = S; }
767 SourceLocation getDefaultLoc() const { return KeywordLoc; }
768 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
769 SourceLocation getColonLoc() const { return ColonLoc; }
770 void setColonLoc(SourceLocation L) { ColonLoc = L; }
772 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
773 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
775 static bool classof(const Stmt *T) {
776 return T->getStmtClass() == DefaultStmtClass;
780 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
783 inline SourceLocation SwitchCase::getLocEnd() const {
784 if (const CaseStmt *CS = dyn_cast<CaseStmt>(this))
785 return CS->getLocEnd();
786 return cast<DefaultStmt>(this)->getLocEnd();
789 /// LabelStmt - Represents a label, which has a substatement. For example:
792 class LabelStmt : public Stmt {
793 SourceLocation IdentLoc;
798 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
799 : Stmt(LabelStmtClass), IdentLoc(IL), TheDecl(D), SubStmt(substmt) {
800 static_assert(sizeof(LabelStmt) ==
801 2 * sizeof(SourceLocation) + 2 * sizeof(void *),
802 "LabelStmt too big");
805 // \brief Build an empty label statement.
806 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
808 SourceLocation getIdentLoc() const { return IdentLoc; }
809 LabelDecl *getDecl() const { return TheDecl; }
810 void setDecl(LabelDecl *D) { TheDecl = D; }
811 const char *getName() const;
812 Stmt *getSubStmt() { return SubStmt; }
813 const Stmt *getSubStmt() const { return SubStmt; }
814 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
815 void setSubStmt(Stmt *SS) { SubStmt = SS; }
817 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
818 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
820 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
822 static bool classof(const Stmt *T) {
823 return T->getStmtClass() == LabelStmtClass;
828 /// \brief Represents an attribute applied to a statement.
830 /// Represents an attribute applied to a statement. For example:
831 /// [[omp::for(...)]] for (...) { ... }
833 class AttributedStmt : public Stmt {
835 SourceLocation AttrLoc;
838 friend class ASTStmtReader;
840 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
841 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
842 NumAttrs(Attrs.size()) {
843 memcpy(getAttrArrayPtr(), Attrs.data(), Attrs.size() * sizeof(Attr *));
846 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
847 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
848 memset(getAttrArrayPtr(), 0, NumAttrs * sizeof(Attr *));
851 Attr *const *getAttrArrayPtr() const {
852 return reinterpret_cast<Attr *const *>(this + 1);
854 Attr **getAttrArrayPtr() { return reinterpret_cast<Attr **>(this + 1); }
857 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
858 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
859 // \brief Build an empty attributed statement.
860 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
862 SourceLocation getAttrLoc() const { return AttrLoc; }
863 ArrayRef<const Attr*> getAttrs() const {
864 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs);
866 Stmt *getSubStmt() { return SubStmt; }
867 const Stmt *getSubStmt() const { return SubStmt; }
869 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
870 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
872 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
874 static bool classof(const Stmt *T) {
875 return T->getStmtClass() == AttributedStmtClass;
880 /// IfStmt - This represents an if/then/else.
882 class IfStmt : public Stmt {
883 enum { VAR, COND, THEN, ELSE, END_EXPR };
884 Stmt* SubExprs[END_EXPR];
886 SourceLocation IfLoc;
887 SourceLocation ElseLoc;
890 IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
891 Stmt *then, SourceLocation EL = SourceLocation(),
892 Stmt *elsev = nullptr);
894 /// \brief Build an empty if/then/else statement
895 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
897 /// \brief Retrieve the variable declared in this "if" statement, if any.
899 /// In the following example, "x" is the condition variable.
901 /// if (int x = foo()) {
902 /// printf("x is %d", x);
905 VarDecl *getConditionVariable() const;
906 void setConditionVariable(const ASTContext &C, VarDecl *V);
908 /// If this IfStmt has a condition variable, return the faux DeclStmt
909 /// associated with the creation of that condition variable.
910 const DeclStmt *getConditionVariableDeclStmt() const {
911 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
914 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
915 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
916 const Stmt *getThen() const { return SubExprs[THEN]; }
917 void setThen(Stmt *S) { SubExprs[THEN] = S; }
918 const Stmt *getElse() const { return SubExprs[ELSE]; }
919 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
921 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
922 Stmt *getThen() { return SubExprs[THEN]; }
923 Stmt *getElse() { return SubExprs[ELSE]; }
925 SourceLocation getIfLoc() const { return IfLoc; }
926 void setIfLoc(SourceLocation L) { IfLoc = L; }
927 SourceLocation getElseLoc() const { return ElseLoc; }
928 void setElseLoc(SourceLocation L) { ElseLoc = L; }
930 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
931 SourceLocation getLocEnd() const LLVM_READONLY {
933 return SubExprs[ELSE]->getLocEnd();
935 return SubExprs[THEN]->getLocEnd();
938 // Iterators over subexpressions. The iterators will include iterating
939 // over the initialization expression referenced by the condition variable.
940 child_range children() {
941 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
944 static bool classof(const Stmt *T) {
945 return T->getStmtClass() == IfStmtClass;
949 /// SwitchStmt - This represents a 'switch' stmt.
951 class SwitchStmt : public Stmt {
952 SourceLocation SwitchLoc;
953 enum { VAR, COND, BODY, END_EXPR };
954 Stmt* SubExprs[END_EXPR];
955 // This points to a linked list of case and default statements and, if the
956 // SwitchStmt is a switch on an enum value, records whether all the enum
957 // values were covered by CaseStmts. The coverage information value is meant
958 // to be a hint for possible clients.
959 llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase;
962 SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond);
964 /// \brief Build a empty switch statement.
965 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
967 /// \brief Retrieve the variable declared in this "switch" statement, if any.
969 /// In the following example, "x" is the condition variable.
971 /// switch (int x = foo()) {
976 VarDecl *getConditionVariable() const;
977 void setConditionVariable(const ASTContext &C, VarDecl *V);
979 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
980 /// associated with the creation of that condition variable.
981 const DeclStmt *getConditionVariableDeclStmt() const {
982 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
985 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
986 const Stmt *getBody() const { return SubExprs[BODY]; }
987 const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); }
989 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
990 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
991 Stmt *getBody() { return SubExprs[BODY]; }
992 void setBody(Stmt *S) { SubExprs[BODY] = S; }
993 SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); }
995 /// \brief Set the case list for this switch statement.
996 void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); }
998 SourceLocation getSwitchLoc() const { return SwitchLoc; }
999 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
1001 void setBody(Stmt *S, SourceLocation SL) {
1005 void addSwitchCase(SwitchCase *SC) {
1006 assert(!SC->getNextSwitchCase()
1007 && "case/default already added to a switch");
1008 SC->setNextSwitchCase(FirstCase.getPointer());
1009 FirstCase.setPointer(SC);
1012 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1013 /// switch over an enum value then all cases have been explicitly covered.
1014 void setAllEnumCasesCovered() { FirstCase.setInt(true); }
1016 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1017 /// have been explicitly covered.
1018 bool isAllEnumCasesCovered() const { return FirstCase.getInt(); }
1020 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1021 SourceLocation getLocEnd() const LLVM_READONLY {
1022 return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
1026 child_range children() {
1027 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1030 static bool classof(const Stmt *T) {
1031 return T->getStmtClass() == SwitchStmtClass;
1036 /// WhileStmt - This represents a 'while' stmt.
1038 class WhileStmt : public Stmt {
1039 SourceLocation WhileLoc;
1040 enum { VAR, COND, BODY, END_EXPR };
1041 Stmt* SubExprs[END_EXPR];
1043 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1046 /// \brief Build an empty while statement.
1047 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1049 /// \brief Retrieve the variable declared in this "while" statement, if any.
1051 /// In the following example, "x" is the condition variable.
1053 /// while (int x = random()) {
1057 VarDecl *getConditionVariable() const;
1058 void setConditionVariable(const ASTContext &C, VarDecl *V);
1060 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1061 /// associated with the creation of that condition variable.
1062 const DeclStmt *getConditionVariableDeclStmt() const {
1063 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1066 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1067 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1068 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1069 Stmt *getBody() { return SubExprs[BODY]; }
1070 const Stmt *getBody() const { return SubExprs[BODY]; }
1071 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1073 SourceLocation getWhileLoc() const { return WhileLoc; }
1074 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1076 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1077 SourceLocation getLocEnd() const LLVM_READONLY {
1078 return SubExprs[BODY]->getLocEnd();
1081 static bool classof(const Stmt *T) {
1082 return T->getStmtClass() == WhileStmtClass;
1086 child_range children() {
1087 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1091 /// DoStmt - This represents a 'do/while' stmt.
1093 class DoStmt : public Stmt {
1094 SourceLocation DoLoc;
1095 enum { BODY, COND, END_EXPR };
1096 Stmt* SubExprs[END_EXPR];
1097 SourceLocation WhileLoc;
1098 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1101 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1103 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1104 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1105 SubExprs[BODY] = body;
1108 /// \brief Build an empty do-while statement.
1109 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1111 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1112 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1113 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1114 Stmt *getBody() { return SubExprs[BODY]; }
1115 const Stmt *getBody() const { return SubExprs[BODY]; }
1116 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1118 SourceLocation getDoLoc() const { return DoLoc; }
1119 void setDoLoc(SourceLocation L) { DoLoc = L; }
1120 SourceLocation getWhileLoc() const { return WhileLoc; }
1121 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1123 SourceLocation getRParenLoc() const { return RParenLoc; }
1124 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1126 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1127 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1129 static bool classof(const Stmt *T) {
1130 return T->getStmtClass() == DoStmtClass;
1134 child_range children() {
1135 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1140 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1141 /// the init/cond/inc parts of the ForStmt will be null if they were not
1142 /// specified in the source.
1144 class ForStmt : public Stmt {
1145 SourceLocation ForLoc;
1146 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1147 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1148 SourceLocation LParenLoc, RParenLoc;
1151 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1152 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1155 /// \brief Build an empty for statement.
1156 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1158 Stmt *getInit() { return SubExprs[INIT]; }
1160 /// \brief Retrieve the variable declared in this "for" statement, if any.
1162 /// In the following example, "y" is the condition variable.
1164 /// for (int x = random(); int y = mangle(x); ++x) {
1168 VarDecl *getConditionVariable() const;
1169 void setConditionVariable(const ASTContext &C, VarDecl *V);
1171 /// If this ForStmt has a condition variable, return the faux DeclStmt
1172 /// associated with the creation of that condition variable.
1173 const DeclStmt *getConditionVariableDeclStmt() const {
1174 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1177 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1178 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1179 Stmt *getBody() { return SubExprs[BODY]; }
1181 const Stmt *getInit() const { return SubExprs[INIT]; }
1182 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1183 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1184 const Stmt *getBody() const { return SubExprs[BODY]; }
1186 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1187 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1188 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1189 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1191 SourceLocation getForLoc() const { return ForLoc; }
1192 void setForLoc(SourceLocation L) { ForLoc = L; }
1193 SourceLocation getLParenLoc() const { return LParenLoc; }
1194 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1195 SourceLocation getRParenLoc() const { return RParenLoc; }
1196 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1198 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1199 SourceLocation getLocEnd() const LLVM_READONLY {
1200 return SubExprs[BODY]->getLocEnd();
1203 static bool classof(const Stmt *T) {
1204 return T->getStmtClass() == ForStmtClass;
1208 child_range children() {
1209 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1213 /// GotoStmt - This represents a direct goto.
1215 class GotoStmt : public Stmt {
1217 SourceLocation GotoLoc;
1218 SourceLocation LabelLoc;
1220 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1221 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1223 /// \brief Build an empty goto statement.
1224 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1226 LabelDecl *getLabel() const { return Label; }
1227 void setLabel(LabelDecl *D) { Label = D; }
1229 SourceLocation getGotoLoc() const { return GotoLoc; }
1230 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1231 SourceLocation getLabelLoc() const { return LabelLoc; }
1232 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1234 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1235 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1237 static bool classof(const Stmt *T) {
1238 return T->getStmtClass() == GotoStmtClass;
1242 child_range children() { return child_range(); }
1245 /// IndirectGotoStmt - This represents an indirect goto.
1247 class IndirectGotoStmt : public Stmt {
1248 SourceLocation GotoLoc;
1249 SourceLocation StarLoc;
1252 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1254 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1255 Target((Stmt*)target) {}
1257 /// \brief Build an empty indirect goto statement.
1258 explicit IndirectGotoStmt(EmptyShell Empty)
1259 : Stmt(IndirectGotoStmtClass, Empty) { }
1261 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1262 SourceLocation getGotoLoc() const { return GotoLoc; }
1263 void setStarLoc(SourceLocation L) { StarLoc = L; }
1264 SourceLocation getStarLoc() const { return StarLoc; }
1266 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1267 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1268 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1270 /// getConstantTarget - Returns the fixed target of this indirect
1271 /// goto, if one exists.
1272 LabelDecl *getConstantTarget();
1273 const LabelDecl *getConstantTarget() const {
1274 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1277 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1278 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1280 static bool classof(const Stmt *T) {
1281 return T->getStmtClass() == IndirectGotoStmtClass;
1285 child_range children() { return child_range(&Target, &Target+1); }
1289 /// ContinueStmt - This represents a continue.
1291 class ContinueStmt : public Stmt {
1292 SourceLocation ContinueLoc;
1294 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1296 /// \brief Build an empty continue statement.
1297 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1299 SourceLocation getContinueLoc() const { return ContinueLoc; }
1300 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1302 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1303 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1305 static bool classof(const Stmt *T) {
1306 return T->getStmtClass() == ContinueStmtClass;
1310 child_range children() { return child_range(); }
1313 /// BreakStmt - This represents a break.
1315 class BreakStmt : public Stmt {
1316 SourceLocation BreakLoc;
1319 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {
1320 static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation),
1321 "BreakStmt too large");
1324 /// \brief Build an empty break statement.
1325 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1327 SourceLocation getBreakLoc() const { return BreakLoc; }
1328 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1330 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1331 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1333 static bool classof(const Stmt *T) {
1334 return T->getStmtClass() == BreakStmtClass;
1338 child_range children() { return child_range(); }
1342 /// ReturnStmt - This represents a return, optionally of an expression:
1346 /// Note that GCC allows return with no argument in a function declared to
1347 /// return a value, and it allows returning a value in functions declared to
1348 /// return void. We explicitly model this in the AST, which means you can't
1349 /// depend on the return type of the function and the presence of an argument.
1351 class ReturnStmt : public Stmt {
1352 SourceLocation RetLoc;
1354 const VarDecl *NRVOCandidate;
1357 explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {}
1359 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1360 : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E),
1361 NRVOCandidate(NRVOCandidate) {}
1363 /// \brief Build an empty return expression.
1364 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1366 const Expr *getRetValue() const;
1367 Expr *getRetValue();
1368 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1370 SourceLocation getReturnLoc() const { return RetLoc; }
1371 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1373 /// \brief Retrieve the variable that might be used for the named return
1374 /// value optimization.
1376 /// The optimization itself can only be performed if the variable is
1377 /// also marked as an NRVO object.
1378 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1379 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1381 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1382 SourceLocation getLocEnd() const LLVM_READONLY {
1383 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1386 static bool classof(const Stmt *T) {
1387 return T->getStmtClass() == ReturnStmtClass;
1391 child_range children() {
1392 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1393 return child_range();
1397 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1399 class AsmStmt : public Stmt {
1401 SourceLocation AsmLoc;
1402 /// \brief True if the assembly statement does not have any input or output
1406 /// \brief If true, treat this inline assembly as having side effects.
1407 /// This assembly statement should not be optimized, deleted or moved.
1410 unsigned NumOutputs;
1412 unsigned NumClobbers;
1416 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1417 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) :
1418 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1419 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { }
1421 friend class ASTStmtReader;
1424 /// \brief Build an empty inline-assembly statement.
1425 explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1426 Stmt(SC, Empty), Exprs(nullptr) { }
1428 SourceLocation getAsmLoc() const { return AsmLoc; }
1429 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1431 bool isSimple() const { return IsSimple; }
1432 void setSimple(bool V) { IsSimple = V; }
1434 bool isVolatile() const { return IsVolatile; }
1435 void setVolatile(bool V) { IsVolatile = V; }
1437 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1438 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1440 //===--- Asm String Analysis ---===//
1442 /// Assemble final IR asm string.
1443 std::string generateAsmString(const ASTContext &C) const;
1445 //===--- Output operands ---===//
1447 unsigned getNumOutputs() const { return NumOutputs; }
1449 /// getOutputConstraint - Return the constraint string for the specified
1450 /// output operand. All output constraints are known to be non-empty (either
1452 StringRef getOutputConstraint(unsigned i) const;
1454 /// isOutputPlusConstraint - Return true if the specified output constraint
1455 /// is a "+" constraint (which is both an input and an output) or false if it
1456 /// is an "=" constraint (just an output).
1457 bool isOutputPlusConstraint(unsigned i) const {
1458 return getOutputConstraint(i)[0] == '+';
1461 const Expr *getOutputExpr(unsigned i) const;
1463 /// getNumPlusOperands - Return the number of output operands that have a "+"
1465 unsigned getNumPlusOperands() const;
1467 //===--- Input operands ---===//
1469 unsigned getNumInputs() const { return NumInputs; }
1471 /// getInputConstraint - Return the specified input constraint. Unlike output
1472 /// constraints, these can be empty.
1473 StringRef getInputConstraint(unsigned i) const;
1475 const Expr *getInputExpr(unsigned i) const;
1477 //===--- Other ---===//
1479 unsigned getNumClobbers() const { return NumClobbers; }
1480 StringRef getClobber(unsigned i) const;
1482 static bool classof(const Stmt *T) {
1483 return T->getStmtClass() == GCCAsmStmtClass ||
1484 T->getStmtClass() == MSAsmStmtClass;
1487 // Input expr iterators.
1489 typedef ExprIterator inputs_iterator;
1490 typedef ConstExprIterator const_inputs_iterator;
1491 typedef llvm::iterator_range<inputs_iterator> inputs_range;
1492 typedef llvm::iterator_range<const_inputs_iterator> inputs_const_range;
1494 inputs_iterator begin_inputs() {
1495 return &Exprs[0] + NumOutputs;
1498 inputs_iterator end_inputs() {
1499 return &Exprs[0] + NumOutputs + NumInputs;
1502 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1504 const_inputs_iterator begin_inputs() const {
1505 return &Exprs[0] + NumOutputs;
1508 const_inputs_iterator end_inputs() const {
1509 return &Exprs[0] + NumOutputs + NumInputs;
1512 inputs_const_range inputs() const {
1513 return inputs_const_range(begin_inputs(), end_inputs());
1516 // Output expr iterators.
1518 typedef ExprIterator outputs_iterator;
1519 typedef ConstExprIterator const_outputs_iterator;
1520 typedef llvm::iterator_range<outputs_iterator> outputs_range;
1521 typedef llvm::iterator_range<const_outputs_iterator> outputs_const_range;
1523 outputs_iterator begin_outputs() {
1526 outputs_iterator end_outputs() {
1527 return &Exprs[0] + NumOutputs;
1529 outputs_range outputs() {
1530 return outputs_range(begin_outputs(), end_outputs());
1533 const_outputs_iterator begin_outputs() const {
1536 const_outputs_iterator end_outputs() const {
1537 return &Exprs[0] + NumOutputs;
1539 outputs_const_range outputs() const {
1540 return outputs_const_range(begin_outputs(), end_outputs());
1543 child_range children() {
1544 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1548 /// This represents a GCC inline-assembly statement extension.
1550 class GCCAsmStmt : public AsmStmt {
1551 SourceLocation RParenLoc;
1552 StringLiteral *AsmStr;
1554 // FIXME: If we wanted to, we could allocate all of these in one big array.
1555 StringLiteral **Constraints;
1556 StringLiteral **Clobbers;
1557 IdentifierInfo **Names;
1559 friend class ASTStmtReader;
1562 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1563 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1564 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1565 StringLiteral *asmstr, unsigned numclobbers,
1566 StringLiteral **clobbers, SourceLocation rparenloc);
1568 /// \brief Build an empty inline-assembly statement.
1569 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1570 Constraints(nullptr), Clobbers(nullptr), Names(nullptr) { }
1572 SourceLocation getRParenLoc() const { return RParenLoc; }
1573 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1575 //===--- Asm String Analysis ---===//
1577 const StringLiteral *getAsmString() const { return AsmStr; }
1578 StringLiteral *getAsmString() { return AsmStr; }
1579 void setAsmString(StringLiteral *E) { AsmStr = E; }
1581 /// AsmStringPiece - this is part of a decomposed asm string specification
1582 /// (for use with the AnalyzeAsmString function below). An asm string is
1583 /// considered to be a concatenation of these parts.
1584 class AsmStringPiece {
1587 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1588 Operand // Operand reference, with optional modifier %c4.
1595 // Source range for operand references.
1596 CharSourceRange Range;
1598 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1599 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
1601 : MyKind(Operand), Str(S), OperandNo(OpNo),
1602 Range(CharSourceRange::getCharRange(Begin, End)) {
1605 bool isString() const { return MyKind == String; }
1606 bool isOperand() const { return MyKind == Operand; }
1608 const std::string &getString() const {
1612 unsigned getOperandNo() const {
1613 assert(isOperand());
1617 CharSourceRange getRange() const {
1618 assert(isOperand() && "Range is currently used only for Operands.");
1622 /// getModifier - Get the modifier for this operand, if present. This
1623 /// returns '\0' if there was no modifier.
1624 char getModifier() const;
1627 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1628 /// it into pieces. If the asm string is erroneous, emit errors and return
1629 /// true, otherwise return false. This handles canonicalization and
1630 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1631 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1632 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1633 const ASTContext &C, unsigned &DiagOffs) const;
1635 /// Assemble final IR asm string.
1636 std::string generateAsmString(const ASTContext &C) const;
1638 //===--- Output operands ---===//
1640 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1644 StringRef getOutputName(unsigned i) const {
1645 if (IdentifierInfo *II = getOutputIdentifier(i))
1646 return II->getName();
1651 StringRef getOutputConstraint(unsigned i) const;
1653 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1654 return Constraints[i];
1656 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1657 return Constraints[i];
1660 Expr *getOutputExpr(unsigned i);
1662 const Expr *getOutputExpr(unsigned i) const {
1663 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1666 //===--- Input operands ---===//
1668 IdentifierInfo *getInputIdentifier(unsigned i) const {
1669 return Names[i + NumOutputs];
1672 StringRef getInputName(unsigned i) const {
1673 if (IdentifierInfo *II = getInputIdentifier(i))
1674 return II->getName();
1679 StringRef getInputConstraint(unsigned i) const;
1681 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1682 return Constraints[i + NumOutputs];
1684 StringLiteral *getInputConstraintLiteral(unsigned i) {
1685 return Constraints[i + NumOutputs];
1688 Expr *getInputExpr(unsigned i);
1689 void setInputExpr(unsigned i, Expr *E);
1691 const Expr *getInputExpr(unsigned i) const {
1692 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1696 void setOutputsAndInputsAndClobbers(const ASTContext &C,
1697 IdentifierInfo **Names,
1698 StringLiteral **Constraints,
1700 unsigned NumOutputs,
1702 StringLiteral **Clobbers,
1703 unsigned NumClobbers);
1706 //===--- Other ---===//
1708 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1709 /// translate this into a numeric value needed to reference the same operand.
1710 /// This returns -1 if the operand name is invalid.
1711 int getNamedOperand(StringRef SymbolicName) const;
1713 StringRef getClobber(unsigned i) const;
1714 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1715 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1719 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1720 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1722 static bool classof(const Stmt *T) {
1723 return T->getStmtClass() == GCCAsmStmtClass;
1727 /// This represents a Microsoft inline-assembly statement extension.
1729 class MSAsmStmt : public AsmStmt {
1730 SourceLocation LBraceLoc, EndLoc;
1733 unsigned NumAsmToks;
1736 StringRef *Constraints;
1737 StringRef *Clobbers;
1739 friend class ASTStmtReader;
1742 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1743 SourceLocation lbraceloc, bool issimple, bool isvolatile,
1744 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1745 ArrayRef<StringRef> constraints,
1746 ArrayRef<Expr*> exprs, StringRef asmstr,
1747 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1749 /// \brief Build an empty MS-style inline-assembly statement.
1750 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1751 NumAsmToks(0), AsmToks(nullptr), Constraints(nullptr), Clobbers(nullptr) { }
1753 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1754 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1755 SourceLocation getEndLoc() const { return EndLoc; }
1756 void setEndLoc(SourceLocation L) { EndLoc = L; }
1758 bool hasBraces() const { return LBraceLoc.isValid(); }
1760 unsigned getNumAsmToks() { return NumAsmToks; }
1761 Token *getAsmToks() { return AsmToks; }
1763 //===--- Asm String Analysis ---===//
1764 StringRef getAsmString() const { return AsmStr; }
1766 /// Assemble final IR asm string.
1767 std::string generateAsmString(const ASTContext &C) const;
1769 //===--- Output operands ---===//
1771 StringRef getOutputConstraint(unsigned i) const {
1772 assert(i < NumOutputs);
1773 return Constraints[i];
1776 Expr *getOutputExpr(unsigned i);
1778 const Expr *getOutputExpr(unsigned i) const {
1779 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1782 //===--- Input operands ---===//
1784 StringRef getInputConstraint(unsigned i) const {
1785 assert(i < NumInputs);
1786 return Constraints[i + NumOutputs];
1789 Expr *getInputExpr(unsigned i);
1790 void setInputExpr(unsigned i, Expr *E);
1792 const Expr *getInputExpr(unsigned i) const {
1793 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1796 //===--- Other ---===//
1798 ArrayRef<StringRef> getAllConstraints() const {
1799 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
1801 ArrayRef<StringRef> getClobbers() const {
1802 return llvm::makeArrayRef(Clobbers, NumClobbers);
1804 ArrayRef<Expr*> getAllExprs() const {
1805 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
1806 NumInputs + NumOutputs);
1809 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1812 void initialize(const ASTContext &C, StringRef AsmString,
1813 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1814 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1817 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1818 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1820 static bool classof(const Stmt *T) {
1821 return T->getStmtClass() == MSAsmStmtClass;
1824 child_range children() {
1825 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1829 class SEHExceptStmt : public Stmt {
1833 enum { FILTER_EXPR, BLOCK };
1835 SEHExceptStmt(SourceLocation Loc,
1839 friend class ASTReader;
1840 friend class ASTStmtReader;
1841 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1844 static SEHExceptStmt* Create(const ASTContext &C,
1845 SourceLocation ExceptLoc,
1849 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1850 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1852 SourceLocation getExceptLoc() const { return Loc; }
1853 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1855 Expr *getFilterExpr() const {
1856 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1859 CompoundStmt *getBlock() const {
1860 return cast<CompoundStmt>(Children[BLOCK]);
1863 child_range children() {
1864 return child_range(Children,Children+2);
1867 static bool classof(const Stmt *T) {
1868 return T->getStmtClass() == SEHExceptStmtClass;
1873 class SEHFinallyStmt : public Stmt {
1877 SEHFinallyStmt(SourceLocation Loc,
1880 friend class ASTReader;
1881 friend class ASTStmtReader;
1882 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1885 static SEHFinallyStmt* Create(const ASTContext &C,
1886 SourceLocation FinallyLoc,
1889 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1890 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1892 SourceLocation getFinallyLoc() const { return Loc; }
1893 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1895 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1897 child_range children() {
1898 return child_range(&Block,&Block+1);
1901 static bool classof(const Stmt *T) {
1902 return T->getStmtClass() == SEHFinallyStmtClass;
1907 class SEHTryStmt : public Stmt {
1909 SourceLocation TryLoc;
1912 enum { TRY = 0, HANDLER = 1 };
1914 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1915 SourceLocation TryLoc,
1919 friend class ASTReader;
1920 friend class ASTStmtReader;
1921 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1924 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
1925 SourceLocation TryLoc, Stmt *TryBlock,
1928 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1929 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1931 SourceLocation getTryLoc() const { return TryLoc; }
1932 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1934 bool getIsCXXTry() const { return IsCXXTry; }
1936 CompoundStmt* getTryBlock() const {
1937 return cast<CompoundStmt>(Children[TRY]);
1940 Stmt *getHandler() const { return Children[HANDLER]; }
1942 /// Returns 0 if not defined
1943 SEHExceptStmt *getExceptHandler() const;
1944 SEHFinallyStmt *getFinallyHandler() const;
1946 child_range children() {
1947 return child_range(Children,Children+2);
1950 static bool classof(const Stmt *T) {
1951 return T->getStmtClass() == SEHTryStmtClass;
1955 /// Represents a __leave statement.
1957 class SEHLeaveStmt : public Stmt {
1958 SourceLocation LeaveLoc;
1960 explicit SEHLeaveStmt(SourceLocation LL)
1961 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
1963 /// \brief Build an empty __leave statement.
1964 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) { }
1966 SourceLocation getLeaveLoc() const { return LeaveLoc; }
1967 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
1969 SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; }
1970 SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; }
1972 static bool classof(const Stmt *T) {
1973 return T->getStmtClass() == SEHLeaveStmtClass;
1977 child_range children() { return child_range(); }
1980 /// \brief This captures a statement into a function. For example, the following
1981 /// pragma annotated compound statement can be represented as a CapturedStmt,
1982 /// and this compound statement is the body of an anonymous outlined function.
1984 /// #pragma omp parallel
1989 class CapturedStmt : public Stmt {
1991 /// \brief The different capture forms: by 'this', by reference, capture for
1992 /// variable-length array type etc.
1993 enum VariableCaptureKind {
1999 /// \brief Describes the capture of either a variable, or 'this', or
2000 /// variable-length array type.
2002 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2006 /// \brief Create a new capture.
2008 /// \param Loc The source location associated with this capture.
2010 /// \param Kind The kind of capture (this, ByRef, ...).
2012 /// \param Var The variable being captured, or null if capturing this.
2014 Capture(SourceLocation Loc, VariableCaptureKind Kind,
2015 VarDecl *Var = nullptr)
2016 : VarAndKind(Var, Kind), Loc(Loc) {
2019 assert(!Var && "'this' capture cannot have a variable!");
2022 assert(Var && "capturing by reference must have a variable!");
2026 "Variable-length array type capture cannot have a variable!");
2031 /// \brief Determine the kind of capture.
2032 VariableCaptureKind getCaptureKind() const { return VarAndKind.getInt(); }
2034 /// \brief Retrieve the source location at which the variable or 'this' was
2036 SourceLocation getLocation() const { return Loc; }
2038 /// \brief Determine whether this capture handles the C++ 'this' pointer.
2039 bool capturesThis() const { return getCaptureKind() == VCK_This; }
2041 /// \brief Determine whether this capture handles a variable.
2042 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2044 /// \brief Determine whether this capture handles a variable-length array
2046 bool capturesVariableArrayType() const {
2047 return getCaptureKind() == VCK_VLAType;
2050 /// \brief Retrieve the declaration of the variable being captured.
2052 /// This operation is only valid if this capture captures a variable.
2053 VarDecl *getCapturedVar() const {
2054 assert(capturesVariable() &&
2055 "No variable available for 'this' or VAT capture");
2056 return VarAndKind.getPointer();
2058 friend class ASTStmtReader;
2062 /// \brief The number of variable captured, including 'this'.
2063 unsigned NumCaptures;
2065 /// \brief The pointer part is the implicit the outlined function and the
2066 /// int part is the captured region kind, 'CR_Default' etc.
2067 llvm::PointerIntPair<CapturedDecl *, 1, CapturedRegionKind> CapDeclAndKind;
2069 /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl.
2070 RecordDecl *TheRecordDecl;
2072 /// \brief Construct a captured statement.
2073 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2074 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2076 /// \brief Construct an empty captured statement.
2077 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2079 Stmt **getStoredStmts() const {
2080 return reinterpret_cast<Stmt **>(const_cast<CapturedStmt *>(this) + 1);
2083 Capture *getStoredCaptures() const;
2085 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2088 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2089 CapturedRegionKind Kind,
2090 ArrayRef<Capture> Captures,
2091 ArrayRef<Expr *> CaptureInits,
2092 CapturedDecl *CD, RecordDecl *RD);
2094 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2095 unsigned NumCaptures);
2097 /// \brief Retrieve the statement being captured.
2098 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2099 const Stmt *getCapturedStmt() const {
2100 return const_cast<CapturedStmt *>(this)->getCapturedStmt();
2103 /// \brief Retrieve the outlined function declaration.
2104 CapturedDecl *getCapturedDecl() { return CapDeclAndKind.getPointer(); }
2105 const CapturedDecl *getCapturedDecl() const {
2106 return const_cast<CapturedStmt *>(this)->getCapturedDecl();
2109 /// \brief Set the outlined function declaration.
2110 void setCapturedDecl(CapturedDecl *D) {
2111 assert(D && "null CapturedDecl");
2112 CapDeclAndKind.setPointer(D);
2115 /// \brief Retrieve the captured region kind.
2116 CapturedRegionKind getCapturedRegionKind() const {
2117 return CapDeclAndKind.getInt();
2120 /// \brief Set the captured region kind.
2121 void setCapturedRegionKind(CapturedRegionKind Kind) {
2122 CapDeclAndKind.setInt(Kind);
2125 /// \brief Retrieve the record declaration for captured variables.
2126 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2128 /// \brief Set the record declaration for captured variables.
2129 void setCapturedRecordDecl(RecordDecl *D) {
2130 assert(D && "null RecordDecl");
2134 /// \brief True if this variable has been captured.
2135 bool capturesVariable(const VarDecl *Var) const;
2137 /// \brief An iterator that walks over the captures.
2138 typedef Capture *capture_iterator;
2139 typedef const Capture *const_capture_iterator;
2140 typedef llvm::iterator_range<capture_iterator> capture_range;
2141 typedef llvm::iterator_range<const_capture_iterator> capture_const_range;
2143 capture_range captures() {
2144 return capture_range(capture_begin(), capture_end());
2146 capture_const_range captures() const {
2147 return capture_const_range(capture_begin(), capture_end());
2150 /// \brief Retrieve an iterator pointing to the first capture.
2151 capture_iterator capture_begin() { return getStoredCaptures(); }
2152 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2154 /// \brief Retrieve an iterator pointing past the end of the sequence of
2156 capture_iterator capture_end() const {
2157 return getStoredCaptures() + NumCaptures;
2160 /// \brief Retrieve the number of captures, including 'this'.
2161 unsigned capture_size() const { return NumCaptures; }
2163 /// \brief Iterator that walks over the capture initialization arguments.
2164 typedef Expr **capture_init_iterator;
2165 typedef llvm::iterator_range<capture_init_iterator> capture_init_range;
2167 capture_init_range capture_inits() const {
2168 return capture_init_range(capture_init_begin(), capture_init_end());
2171 /// \brief Retrieve the first initialization argument.
2172 capture_init_iterator capture_init_begin() const {
2173 return reinterpret_cast<Expr **>(getStoredStmts());
2176 /// \brief Retrieve the iterator pointing one past the last initialization
2178 capture_init_iterator capture_init_end() const {
2179 return capture_init_begin() + NumCaptures;
2182 SourceLocation getLocStart() const LLVM_READONLY {
2183 return getCapturedStmt()->getLocStart();
2185 SourceLocation getLocEnd() const LLVM_READONLY {
2186 return getCapturedStmt()->getLocEnd();
2188 SourceRange getSourceRange() const LLVM_READONLY {
2189 return getCapturedStmt()->getSourceRange();
2192 static bool classof(const Stmt *T) {
2193 return T->getStmtClass() == CapturedStmtClass;
2196 child_range children();
2198 friend class ASTStmtReader;
2201 } // end namespace clang