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;
44 struct PrintingPolicy;
53 //===----------------------------------------------------------------------===//
54 // AST classes for statements.
55 //===----------------------------------------------------------------------===//
57 /// Stmt - This represents one statement.
59 class alignas(void *) Stmt {
63 #define STMT(CLASS, PARENT) CLASS##Class,
64 #define STMT_RANGE(BASE, FIRST, LAST) \
65 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
66 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
67 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
68 #define ABSTRACT_STMT(STMT)
69 #include "clang/AST/StmtNodes.inc"
72 // Make vanilla 'new' and 'delete' illegal for Stmts.
74 void *operator new(size_t bytes) noexcept {
75 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
77 void operator delete(void *data) noexcept {
78 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
84 /// \brief The statement class.
87 enum { NumStmtBits = 8 };
89 class CompoundStmtBitfields {
90 friend class CompoundStmt;
91 unsigned : NumStmtBits;
93 unsigned NumStmts : 32 - NumStmtBits;
96 class IfStmtBitfields {
98 unsigned : NumStmtBits;
100 unsigned IsConstexpr : 1;
103 class ExprBitfields {
105 friend class DeclRefExpr; // computeDependence
106 friend class InitListExpr; // ctor
107 friend class DesignatedInitExpr; // ctor
108 friend class BlockDeclRefExpr; // ctor
109 friend class ASTStmtReader; // deserialization
110 friend class CXXNewExpr; // ctor
111 friend class DependentScopeDeclRefExpr; // ctor
112 friend class CXXConstructExpr; // ctor
113 friend class CallExpr; // ctor
114 friend class OffsetOfExpr; // ctor
115 friend class ObjCMessageExpr; // ctor
116 friend class ObjCArrayLiteral; // ctor
117 friend class ObjCDictionaryLiteral; // ctor
118 friend class ShuffleVectorExpr; // ctor
119 friend class ParenListExpr; // ctor
120 friend class CXXUnresolvedConstructExpr; // ctor
121 friend class CXXDependentScopeMemberExpr; // ctor
122 friend class OverloadExpr; // ctor
123 friend class PseudoObjectExpr; // ctor
124 friend class AtomicExpr; // ctor
125 friend class OpaqueValueExpr; // ctor
126 unsigned : NumStmtBits;
128 unsigned ValueKind : 2;
129 unsigned ObjectKind : 2;
130 unsigned TypeDependent : 1;
131 unsigned ValueDependent : 1;
132 unsigned InstantiationDependent : 1;
133 unsigned ContainsUnexpandedParameterPack : 1;
135 enum { NumExprBits = 16 };
137 class CharacterLiteralBitfields {
138 friend class CharacterLiteral;
139 unsigned : NumExprBits;
144 enum APFloatSemantics {
153 class FloatingLiteralBitfields {
154 friend class FloatingLiteral;
155 unsigned : NumExprBits;
157 unsigned Semantics : 3; // Provides semantics for APFloat construction
158 unsigned IsExact : 1;
161 class UnaryExprOrTypeTraitExprBitfields {
162 friend class UnaryExprOrTypeTraitExpr;
163 unsigned : NumExprBits;
166 unsigned IsType : 1; // true if operand is a type, false if an expression.
169 class DeclRefExprBitfields {
170 friend class DeclRefExpr;
171 friend class ASTStmtReader; // deserialization
172 unsigned : NumExprBits;
174 unsigned HasQualifier : 1;
175 unsigned HasTemplateKWAndArgsInfo : 1;
176 unsigned HasFoundDecl : 1;
177 unsigned HadMultipleCandidates : 1;
178 unsigned RefersToEnclosingVariableOrCapture : 1;
181 class CastExprBitfields {
182 friend class CastExpr;
183 unsigned : NumExprBits;
186 unsigned BasePathSize : 32 - 6 - NumExprBits;
189 class CallExprBitfields {
190 friend class CallExpr;
191 unsigned : NumExprBits;
193 unsigned NumPreArgs : 1;
196 class ExprWithCleanupsBitfields {
197 friend class ExprWithCleanups;
198 friend class ASTStmtReader; // deserialization
200 unsigned : NumExprBits;
202 // When false, it must not have side effects.
203 unsigned CleanupsHaveSideEffects : 1;
205 unsigned NumObjects : 32 - 1 - NumExprBits;
208 class PseudoObjectExprBitfields {
209 friend class PseudoObjectExpr;
210 friend class ASTStmtReader; // deserialization
212 unsigned : NumExprBits;
214 // These don't need to be particularly wide, because they're
215 // strictly limited by the forms of expressions we permit.
216 unsigned NumSubExprs : 8;
217 unsigned ResultIndex : 32 - 8 - NumExprBits;
220 class ObjCIndirectCopyRestoreExprBitfields {
221 friend class ObjCIndirectCopyRestoreExpr;
222 unsigned : NumExprBits;
224 unsigned ShouldCopy : 1;
227 class InitListExprBitfields {
228 friend class InitListExpr;
230 unsigned : NumExprBits;
232 /// Whether this initializer list originally had a GNU array-range
233 /// designator in it. This is a temporary marker used by CodeGen.
234 unsigned HadArrayRangeDesignator : 1;
237 class TypeTraitExprBitfields {
238 friend class TypeTraitExpr;
239 friend class ASTStmtReader;
240 friend class ASTStmtWriter;
242 unsigned : NumExprBits;
244 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
247 /// \brief If this expression is not value-dependent, this indicates whether
248 /// the trait evaluated true or false.
251 /// \brief The number of arguments to this type trait.
252 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
256 StmtBitfields StmtBits;
257 CompoundStmtBitfields CompoundStmtBits;
258 IfStmtBitfields IfStmtBits;
259 ExprBitfields ExprBits;
260 CharacterLiteralBitfields CharacterLiteralBits;
261 FloatingLiteralBitfields FloatingLiteralBits;
262 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
263 DeclRefExprBitfields DeclRefExprBits;
264 CastExprBitfields CastExprBits;
265 CallExprBitfields CallExprBits;
266 ExprWithCleanupsBitfields ExprWithCleanupsBits;
267 PseudoObjectExprBitfields PseudoObjectExprBits;
268 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
269 InitListExprBitfields InitListExprBits;
270 TypeTraitExprBitfields TypeTraitExprBits;
273 friend class ASTStmtReader;
274 friend class ASTStmtWriter;
277 // Only allow allocation of Stmts using the allocator in ASTContext
278 // or by doing a placement new.
279 void* operator new(size_t bytes, const ASTContext& C,
280 unsigned alignment = 8);
282 void* operator new(size_t bytes, const ASTContext* C,
283 unsigned alignment = 8) {
284 return operator new(bytes, *C, alignment);
287 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
289 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
290 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
291 void operator delete(void *, size_t) noexcept {}
292 void operator delete(void *, void *) noexcept {}
295 /// \brief A placeholder type used to construct an empty shell of a
296 /// type, that will be filled in later (e.g., by some
297 /// de-serialization).
298 struct EmptyShell { };
301 /// Iterator for iterating over Stmt * arrays that contain only Expr *
303 /// This is needed because AST nodes use Stmt* arrays to store
304 /// references to children (to be compatible with StmtIterator).
306 : llvm::iterator_adaptor_base<ExprIterator, Stmt **,
307 std::random_access_iterator_tag, Expr *> {
308 ExprIterator() : iterator_adaptor_base(nullptr) {}
309 ExprIterator(Stmt **I) : iterator_adaptor_base(I) {}
311 reference operator*() const {
312 assert((*I)->getStmtClass() >= firstExprConstant &&
313 (*I)->getStmtClass() <= lastExprConstant);
314 return *reinterpret_cast<Expr **>(I);
318 /// Const iterator for iterating over Stmt * arrays that contain only Expr *
319 struct ConstExprIterator
320 : llvm::iterator_adaptor_base<ConstExprIterator, const Stmt *const *,
321 std::random_access_iterator_tag,
323 ConstExprIterator() : iterator_adaptor_base(nullptr) {}
324 ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {}
326 reference operator*() const {
327 assert((*I)->getStmtClass() >= firstExprConstant &&
328 (*I)->getStmtClass() <= lastExprConstant);
329 return *reinterpret_cast<const Expr *const *>(I);
334 /// \brief Whether statistic collection is enabled.
335 static bool StatisticsEnabled;
338 /// \brief Construct an empty statement.
339 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
343 static_assert(sizeof(*this) % alignof(void *) == 0,
344 "Insufficient alignment!");
345 StmtBits.sClass = SC;
346 if (StatisticsEnabled) Stmt::addStmtClass(SC);
349 StmtClass getStmtClass() const {
350 return static_cast<StmtClass>(StmtBits.sClass);
352 const char *getStmtClassName() const;
354 /// SourceLocation tokens are not useful in isolation - they are low level
355 /// value objects created/interpreted by SourceManager. We assume AST
356 /// clients will have a pointer to the respective SourceManager.
357 SourceRange getSourceRange() const LLVM_READONLY;
358 SourceLocation getLocStart() const LLVM_READONLY;
359 SourceLocation getLocEnd() const LLVM_READONLY;
361 // global temp stats (until we have a per-module visitor)
362 static void addStmtClass(const StmtClass s);
363 static void EnableStatistics();
364 static void PrintStats();
366 /// \brief Dumps the specified AST fragment and all subtrees to
369 void dump(SourceManager &SM) const;
370 void dump(raw_ostream &OS, SourceManager &SM) const;
371 void dump(raw_ostream &OS) const;
373 /// dumpColor - same as dump(), but forces color highlighting.
374 void dumpColor() const;
376 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
377 /// back to its original source language syntax.
378 void dumpPretty(const ASTContext &Context) const;
379 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
380 const PrintingPolicy &Policy,
381 unsigned Indentation = 0) const;
383 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
384 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
385 void viewAST() const;
387 /// Skip past any implicit AST nodes which might surround this
388 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
389 Stmt *IgnoreImplicit();
390 const Stmt *IgnoreImplicit() const {
391 return const_cast<Stmt *>(this)->IgnoreImplicit();
394 /// \brief Skip no-op (attributed, compound) container stmts and skip captured
395 /// stmt at the top, if \a IgnoreCaptured is true.
396 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
398 const Stmt *stripLabelLikeStatements() const;
399 Stmt *stripLabelLikeStatements() {
400 return const_cast<Stmt*>(
401 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
404 /// Child Iterators: All subclasses must implement 'children'
405 /// to permit easy iteration over the substatements/subexpessions of an
406 /// AST node. This permits easy iteration over all nodes in the AST.
407 typedef StmtIterator child_iterator;
408 typedef ConstStmtIterator const_child_iterator;
410 typedef llvm::iterator_range<child_iterator> child_range;
411 typedef llvm::iterator_range<const_child_iterator> const_child_range;
413 child_range children();
414 const_child_range children() const {
415 auto Children = const_cast<Stmt *>(this)->children();
416 return const_child_range(Children.begin(), Children.end());
419 child_iterator child_begin() { return children().begin(); }
420 child_iterator child_end() { return children().end(); }
422 const_child_iterator child_begin() const { return children().begin(); }
423 const_child_iterator child_end() const { return children().end(); }
425 /// \brief Produce a unique representation of the given statement.
427 /// \param ID once the profiling operation is complete, will contain
428 /// the unique representation of the given statement.
430 /// \param Context the AST context in which the statement resides
432 /// \param Canonical whether the profile should be based on the canonical
433 /// representation of this statement (e.g., where non-type template
434 /// parameters are identified by index/level rather than their
435 /// declaration pointers) or the exact representation of the statement as
436 /// written in the source.
437 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
438 bool Canonical) const;
441 /// DeclStmt - Adaptor class for mixing declarations with statements and
442 /// expressions. For example, CompoundStmt mixes statements, expressions
443 /// and declarations (variables, types). Another example is ForStmt, where
444 /// the first statement can be an expression or a declaration.
446 class DeclStmt : public Stmt {
448 SourceLocation StartLoc, EndLoc;
451 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
452 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
453 StartLoc(startLoc), EndLoc(endLoc) {}
455 /// \brief Build an empty declaration statement.
456 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
458 /// isSingleDecl - This method returns true if this DeclStmt refers
459 /// to a single Decl.
460 bool isSingleDecl() const {
461 return DG.isSingleDecl();
464 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
465 Decl *getSingleDecl() { return DG.getSingleDecl(); }
467 const DeclGroupRef getDeclGroup() const { return DG; }
468 DeclGroupRef getDeclGroup() { return DG; }
469 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
471 SourceLocation getStartLoc() const { return StartLoc; }
472 void setStartLoc(SourceLocation L) { StartLoc = L; }
473 SourceLocation getEndLoc() const { return EndLoc; }
474 void setEndLoc(SourceLocation L) { EndLoc = L; }
476 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
477 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
479 static bool classof(const Stmt *T) {
480 return T->getStmtClass() == DeclStmtClass;
483 // Iterators over subexpressions.
484 child_range children() {
485 return child_range(child_iterator(DG.begin(), DG.end()),
486 child_iterator(DG.end(), DG.end()));
489 typedef DeclGroupRef::iterator decl_iterator;
490 typedef DeclGroupRef::const_iterator const_decl_iterator;
491 typedef llvm::iterator_range<decl_iterator> decl_range;
492 typedef llvm::iterator_range<const_decl_iterator> decl_const_range;
494 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
495 decl_const_range decls() const {
496 return decl_const_range(decl_begin(), decl_end());
498 decl_iterator decl_begin() { return DG.begin(); }
499 decl_iterator decl_end() { return DG.end(); }
500 const_decl_iterator decl_begin() const { return DG.begin(); }
501 const_decl_iterator decl_end() const { return DG.end(); }
503 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
504 reverse_decl_iterator decl_rbegin() {
505 return reverse_decl_iterator(decl_end());
507 reverse_decl_iterator decl_rend() {
508 return reverse_decl_iterator(decl_begin());
512 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
514 class NullStmt : public Stmt {
515 SourceLocation SemiLoc;
517 /// \brief True if the null statement was preceded by an empty macro, e.g:
522 bool HasLeadingEmptyMacro;
524 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
525 : Stmt(NullStmtClass), SemiLoc(L),
526 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
528 /// \brief Build an empty null statement.
529 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
530 HasLeadingEmptyMacro(false) { }
532 SourceLocation getSemiLoc() const { return SemiLoc; }
533 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
535 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
537 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
538 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
540 static bool classof(const Stmt *T) {
541 return T->getStmtClass() == NullStmtClass;
544 child_range children() {
545 return child_range(child_iterator(), child_iterator());
548 friend class ASTStmtReader;
549 friend class ASTStmtWriter;
552 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
554 class CompoundStmt : public Stmt {
556 SourceLocation LBraceLoc, RBraceLoc;
558 friend class ASTStmtReader;
561 CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
562 SourceLocation LB, SourceLocation RB);
564 // \brief Build an empty compound statement with a location.
565 explicit CompoundStmt(SourceLocation Loc)
566 : Stmt(CompoundStmtClass), Body(nullptr), LBraceLoc(Loc), RBraceLoc(Loc) {
567 CompoundStmtBits.NumStmts = 0;
570 // \brief Build an empty compound statement.
571 explicit CompoundStmt(EmptyShell Empty)
572 : Stmt(CompoundStmtClass, Empty), Body(nullptr) {
573 CompoundStmtBits.NumStmts = 0;
576 void setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts);
578 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
579 unsigned size() const { return CompoundStmtBits.NumStmts; }
581 typedef Stmt** body_iterator;
582 typedef llvm::iterator_range<body_iterator> body_range;
584 body_range body() { return body_range(body_begin(), body_end()); }
585 body_iterator body_begin() { return Body; }
586 body_iterator body_end() { return Body + size(); }
587 Stmt *body_front() { return !body_empty() ? Body[0] : nullptr; }
588 Stmt *body_back() { return !body_empty() ? Body[size()-1] : nullptr; }
590 void setLastStmt(Stmt *S) {
591 assert(!body_empty() && "setLastStmt");
595 typedef Stmt* const * const_body_iterator;
596 typedef llvm::iterator_range<const_body_iterator> body_const_range;
598 body_const_range body() const {
599 return body_const_range(body_begin(), body_end());
601 const_body_iterator body_begin() const { return Body; }
602 const_body_iterator body_end() const { return Body + size(); }
603 const Stmt *body_front() const {
604 return !body_empty() ? Body[0] : nullptr;
606 const Stmt *body_back() const {
607 return !body_empty() ? Body[size() - 1] : nullptr;
610 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
611 reverse_body_iterator body_rbegin() {
612 return reverse_body_iterator(body_end());
614 reverse_body_iterator body_rend() {
615 return reverse_body_iterator(body_begin());
618 typedef std::reverse_iterator<const_body_iterator>
619 const_reverse_body_iterator;
621 const_reverse_body_iterator body_rbegin() const {
622 return const_reverse_body_iterator(body_end());
625 const_reverse_body_iterator body_rend() const {
626 return const_reverse_body_iterator(body_begin());
629 SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; }
630 SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; }
632 SourceLocation getLBracLoc() const { return LBraceLoc; }
633 SourceLocation getRBracLoc() const { return RBraceLoc; }
635 static bool classof(const Stmt *T) {
636 return T->getStmtClass() == CompoundStmtClass;
640 child_range children() {
641 return child_range(Body, Body + CompoundStmtBits.NumStmts);
644 const_child_range children() const {
645 return const_child_range(child_iterator(Body),
646 child_iterator(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 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
846 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
847 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
848 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
851 const Attr *const *getAttrArrayPtr() const {
852 return reinterpret_cast<const Attr *const *>(this + 1);
854 const Attr **getAttrArrayPtr() {
855 return reinterpret_cast<const Attr **>(this + 1);
859 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
860 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
861 // \brief Build an empty attributed statement.
862 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
864 SourceLocation getAttrLoc() const { return AttrLoc; }
865 ArrayRef<const Attr*> getAttrs() const {
866 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs);
868 Stmt *getSubStmt() { return SubStmt; }
869 const Stmt *getSubStmt() const { return SubStmt; }
871 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
872 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
874 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
876 static bool classof(const Stmt *T) {
877 return T->getStmtClass() == AttributedStmtClass;
882 /// IfStmt - This represents an if/then/else.
884 class IfStmt : public Stmt {
885 enum { INIT, VAR, COND, THEN, ELSE, END_EXPR };
886 Stmt* SubExprs[END_EXPR];
888 SourceLocation IfLoc;
889 SourceLocation ElseLoc;
892 IfStmt(const ASTContext &C, SourceLocation IL,
893 bool IsConstexpr, Stmt *init, VarDecl *var, Expr *cond,
894 Stmt *then, SourceLocation EL = SourceLocation(),
895 Stmt *elsev = nullptr);
897 /// \brief Build an empty if/then/else statement
898 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
900 /// \brief Retrieve the variable declared in this "if" statement, if any.
902 /// In the following example, "x" is the condition variable.
904 /// if (int x = foo()) {
905 /// printf("x is %d", x);
908 VarDecl *getConditionVariable() const;
909 void setConditionVariable(const ASTContext &C, VarDecl *V);
911 /// If this IfStmt has a condition variable, return the faux DeclStmt
912 /// associated with the creation of that condition variable.
913 const DeclStmt *getConditionVariableDeclStmt() const {
914 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
917 Stmt *getInit() { return SubExprs[INIT]; }
918 const Stmt *getInit() const { return SubExprs[INIT]; }
919 void setInit(Stmt *S) { SubExprs[INIT] = S; }
920 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
921 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
922 const Stmt *getThen() const { return SubExprs[THEN]; }
923 void setThen(Stmt *S) { SubExprs[THEN] = S; }
924 const Stmt *getElse() const { return SubExprs[ELSE]; }
925 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
927 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
928 Stmt *getThen() { return SubExprs[THEN]; }
929 Stmt *getElse() { return SubExprs[ELSE]; }
931 SourceLocation getIfLoc() const { return IfLoc; }
932 void setIfLoc(SourceLocation L) { IfLoc = L; }
933 SourceLocation getElseLoc() const { return ElseLoc; }
934 void setElseLoc(SourceLocation L) { ElseLoc = L; }
936 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
937 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
939 bool isObjCAvailabilityCheck() const;
941 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
942 SourceLocation getLocEnd() const LLVM_READONLY {
944 return SubExprs[ELSE]->getLocEnd();
946 return SubExprs[THEN]->getLocEnd();
949 // Iterators over subexpressions. The iterators will include iterating
950 // over the initialization expression referenced by the condition variable.
951 child_range children() {
952 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
955 static bool classof(const Stmt *T) {
956 return T->getStmtClass() == IfStmtClass;
960 /// SwitchStmt - This represents a 'switch' stmt.
962 class SwitchStmt : public Stmt {
963 SourceLocation SwitchLoc;
964 enum { INIT, VAR, COND, BODY, END_EXPR };
965 Stmt* SubExprs[END_EXPR];
966 // This points to a linked list of case and default statements and, if the
967 // SwitchStmt is a switch on an enum value, records whether all the enum
968 // values were covered by CaseStmts. The coverage information value is meant
969 // to be a hint for possible clients.
970 llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase;
973 SwitchStmt(const ASTContext &C, Stmt *Init, VarDecl *Var, Expr *cond);
975 /// \brief Build a empty switch statement.
976 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
978 /// \brief Retrieve the variable declared in this "switch" statement, if any.
980 /// In the following example, "x" is the condition variable.
982 /// switch (int x = foo()) {
987 VarDecl *getConditionVariable() const;
988 void setConditionVariable(const ASTContext &C, VarDecl *V);
990 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
991 /// associated with the creation of that condition variable.
992 const DeclStmt *getConditionVariableDeclStmt() const {
993 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
996 Stmt *getInit() { return SubExprs[INIT]; }
997 const Stmt *getInit() const { return SubExprs[INIT]; }
998 void setInit(Stmt *S) { SubExprs[INIT] = S; }
999 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1000 const Stmt *getBody() const { return SubExprs[BODY]; }
1001 const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); }
1003 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1004 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
1005 Stmt *getBody() { return SubExprs[BODY]; }
1006 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1007 SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); }
1009 /// \brief Set the case list for this switch statement.
1010 void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); }
1012 SourceLocation getSwitchLoc() const { return SwitchLoc; }
1013 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
1015 void setBody(Stmt *S, SourceLocation SL) {
1019 void addSwitchCase(SwitchCase *SC) {
1020 assert(!SC->getNextSwitchCase()
1021 && "case/default already added to a switch");
1022 SC->setNextSwitchCase(FirstCase.getPointer());
1023 FirstCase.setPointer(SC);
1026 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1027 /// switch over an enum value then all cases have been explicitly covered.
1028 void setAllEnumCasesCovered() { FirstCase.setInt(true); }
1030 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1031 /// have been explicitly covered.
1032 bool isAllEnumCasesCovered() const { return FirstCase.getInt(); }
1034 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1035 SourceLocation getLocEnd() const LLVM_READONLY {
1036 return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
1040 child_range children() {
1041 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1044 static bool classof(const Stmt *T) {
1045 return T->getStmtClass() == SwitchStmtClass;
1050 /// WhileStmt - This represents a 'while' stmt.
1052 class WhileStmt : public Stmt {
1053 SourceLocation WhileLoc;
1054 enum { VAR, COND, BODY, END_EXPR };
1055 Stmt* SubExprs[END_EXPR];
1057 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1060 /// \brief Build an empty while statement.
1061 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1063 /// \brief Retrieve the variable declared in this "while" statement, if any.
1065 /// In the following example, "x" is the condition variable.
1067 /// while (int x = random()) {
1071 VarDecl *getConditionVariable() const;
1072 void setConditionVariable(const ASTContext &C, VarDecl *V);
1074 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1075 /// associated with the creation of that condition variable.
1076 const DeclStmt *getConditionVariableDeclStmt() const {
1077 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1080 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1081 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1082 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1083 Stmt *getBody() { return SubExprs[BODY]; }
1084 const Stmt *getBody() const { return SubExprs[BODY]; }
1085 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1087 SourceLocation getWhileLoc() const { return WhileLoc; }
1088 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1090 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1091 SourceLocation getLocEnd() const LLVM_READONLY {
1092 return SubExprs[BODY]->getLocEnd();
1095 static bool classof(const Stmt *T) {
1096 return T->getStmtClass() == WhileStmtClass;
1100 child_range children() {
1101 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1105 /// DoStmt - This represents a 'do/while' stmt.
1107 class DoStmt : public Stmt {
1108 SourceLocation DoLoc;
1109 enum { BODY, COND, END_EXPR };
1110 Stmt* SubExprs[END_EXPR];
1111 SourceLocation WhileLoc;
1112 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1115 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1117 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1118 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1119 SubExprs[BODY] = body;
1122 /// \brief Build an empty do-while statement.
1123 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1125 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1126 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1127 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1128 Stmt *getBody() { return SubExprs[BODY]; }
1129 const Stmt *getBody() const { return SubExprs[BODY]; }
1130 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1132 SourceLocation getDoLoc() const { return DoLoc; }
1133 void setDoLoc(SourceLocation L) { DoLoc = L; }
1134 SourceLocation getWhileLoc() const { return WhileLoc; }
1135 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1137 SourceLocation getRParenLoc() const { return RParenLoc; }
1138 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1140 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1141 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1143 static bool classof(const Stmt *T) {
1144 return T->getStmtClass() == DoStmtClass;
1148 child_range children() {
1149 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1154 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1155 /// the init/cond/inc parts of the ForStmt will be null if they were not
1156 /// specified in the source.
1158 class ForStmt : public Stmt {
1159 SourceLocation ForLoc;
1160 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1161 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1162 SourceLocation LParenLoc, RParenLoc;
1165 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1166 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1169 /// \brief Build an empty for statement.
1170 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1172 Stmt *getInit() { return SubExprs[INIT]; }
1174 /// \brief Retrieve the variable declared in this "for" statement, if any.
1176 /// In the following example, "y" is the condition variable.
1178 /// for (int x = random(); int y = mangle(x); ++x) {
1182 VarDecl *getConditionVariable() const;
1183 void setConditionVariable(const ASTContext &C, VarDecl *V);
1185 /// If this ForStmt has a condition variable, return the faux DeclStmt
1186 /// associated with the creation of that condition variable.
1187 const DeclStmt *getConditionVariableDeclStmt() const {
1188 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1191 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1192 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1193 Stmt *getBody() { return SubExprs[BODY]; }
1195 const Stmt *getInit() const { return SubExprs[INIT]; }
1196 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1197 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1198 const Stmt *getBody() const { return SubExprs[BODY]; }
1200 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1201 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1202 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1203 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1205 SourceLocation getForLoc() const { return ForLoc; }
1206 void setForLoc(SourceLocation L) { ForLoc = L; }
1207 SourceLocation getLParenLoc() const { return LParenLoc; }
1208 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1209 SourceLocation getRParenLoc() const { return RParenLoc; }
1210 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1212 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1213 SourceLocation getLocEnd() const LLVM_READONLY {
1214 return SubExprs[BODY]->getLocEnd();
1217 static bool classof(const Stmt *T) {
1218 return T->getStmtClass() == ForStmtClass;
1222 child_range children() {
1223 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1227 /// GotoStmt - This represents a direct goto.
1229 class GotoStmt : public Stmt {
1231 SourceLocation GotoLoc;
1232 SourceLocation LabelLoc;
1234 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1235 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1237 /// \brief Build an empty goto statement.
1238 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1240 LabelDecl *getLabel() const { return Label; }
1241 void setLabel(LabelDecl *D) { Label = D; }
1243 SourceLocation getGotoLoc() const { return GotoLoc; }
1244 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1245 SourceLocation getLabelLoc() const { return LabelLoc; }
1246 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1248 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1249 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1251 static bool classof(const Stmt *T) {
1252 return T->getStmtClass() == GotoStmtClass;
1256 child_range children() {
1257 return child_range(child_iterator(), child_iterator());
1261 /// IndirectGotoStmt - This represents an indirect goto.
1263 class IndirectGotoStmt : public Stmt {
1264 SourceLocation GotoLoc;
1265 SourceLocation StarLoc;
1268 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1270 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1271 Target((Stmt*)target) {}
1273 /// \brief Build an empty indirect goto statement.
1274 explicit IndirectGotoStmt(EmptyShell Empty)
1275 : Stmt(IndirectGotoStmtClass, Empty) { }
1277 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1278 SourceLocation getGotoLoc() const { return GotoLoc; }
1279 void setStarLoc(SourceLocation L) { StarLoc = L; }
1280 SourceLocation getStarLoc() const { return StarLoc; }
1282 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1283 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1284 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1286 /// getConstantTarget - Returns the fixed target of this indirect
1287 /// goto, if one exists.
1288 LabelDecl *getConstantTarget();
1289 const LabelDecl *getConstantTarget() const {
1290 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1293 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1294 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1296 static bool classof(const Stmt *T) {
1297 return T->getStmtClass() == IndirectGotoStmtClass;
1301 child_range children() { return child_range(&Target, &Target+1); }
1305 /// ContinueStmt - This represents a continue.
1307 class ContinueStmt : public Stmt {
1308 SourceLocation ContinueLoc;
1310 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1312 /// \brief Build an empty continue statement.
1313 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1315 SourceLocation getContinueLoc() const { return ContinueLoc; }
1316 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1318 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1319 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1321 static bool classof(const Stmt *T) {
1322 return T->getStmtClass() == ContinueStmtClass;
1326 child_range children() {
1327 return child_range(child_iterator(), child_iterator());
1331 /// BreakStmt - This represents a break.
1333 class BreakStmt : public Stmt {
1334 SourceLocation BreakLoc;
1337 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {
1338 static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation),
1339 "BreakStmt too large");
1342 /// \brief Build an empty break statement.
1343 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1345 SourceLocation getBreakLoc() const { return BreakLoc; }
1346 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1348 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1349 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1351 static bool classof(const Stmt *T) {
1352 return T->getStmtClass() == BreakStmtClass;
1356 child_range children() {
1357 return child_range(child_iterator(), child_iterator());
1362 /// ReturnStmt - This represents a return, optionally of an expression:
1366 /// Note that GCC allows return with no argument in a function declared to
1367 /// return a value, and it allows returning a value in functions declared to
1368 /// return void. We explicitly model this in the AST, which means you can't
1369 /// depend on the return type of the function and the presence of an argument.
1371 class ReturnStmt : public Stmt {
1372 SourceLocation RetLoc;
1374 const VarDecl *NRVOCandidate;
1377 explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {}
1379 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1380 : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E),
1381 NRVOCandidate(NRVOCandidate) {}
1383 /// \brief Build an empty return expression.
1384 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1386 const Expr *getRetValue() const;
1387 Expr *getRetValue();
1388 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1390 SourceLocation getReturnLoc() const { return RetLoc; }
1391 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1393 /// \brief Retrieve the variable that might be used for the named return
1394 /// value optimization.
1396 /// The optimization itself can only be performed if the variable is
1397 /// also marked as an NRVO object.
1398 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1399 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1401 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1402 SourceLocation getLocEnd() const LLVM_READONLY {
1403 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1406 static bool classof(const Stmt *T) {
1407 return T->getStmtClass() == ReturnStmtClass;
1411 child_range children() {
1412 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1413 return child_range(child_iterator(), child_iterator());
1417 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1419 class AsmStmt : public Stmt {
1421 SourceLocation AsmLoc;
1422 /// \brief True if the assembly statement does not have any input or output
1426 /// \brief If true, treat this inline assembly as having side effects.
1427 /// This assembly statement should not be optimized, deleted or moved.
1430 unsigned NumOutputs;
1432 unsigned NumClobbers;
1436 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1437 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) :
1438 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1439 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { }
1441 friend class ASTStmtReader;
1444 /// \brief Build an empty inline-assembly statement.
1445 explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1446 Stmt(SC, Empty), Exprs(nullptr) { }
1448 SourceLocation getAsmLoc() const { return AsmLoc; }
1449 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1451 bool isSimple() const { return IsSimple; }
1452 void setSimple(bool V) { IsSimple = V; }
1454 bool isVolatile() const { return IsVolatile; }
1455 void setVolatile(bool V) { IsVolatile = V; }
1457 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1458 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1460 //===--- Asm String Analysis ---===//
1462 /// Assemble final IR asm string.
1463 std::string generateAsmString(const ASTContext &C) const;
1465 //===--- Output operands ---===//
1467 unsigned getNumOutputs() const { return NumOutputs; }
1469 /// getOutputConstraint - Return the constraint string for the specified
1470 /// output operand. All output constraints are known to be non-empty (either
1472 StringRef getOutputConstraint(unsigned i) const;
1474 /// isOutputPlusConstraint - Return true if the specified output constraint
1475 /// is a "+" constraint (which is both an input and an output) or false if it
1476 /// is an "=" constraint (just an output).
1477 bool isOutputPlusConstraint(unsigned i) const {
1478 return getOutputConstraint(i)[0] == '+';
1481 const Expr *getOutputExpr(unsigned i) const;
1483 /// getNumPlusOperands - Return the number of output operands that have a "+"
1485 unsigned getNumPlusOperands() const;
1487 //===--- Input operands ---===//
1489 unsigned getNumInputs() const { return NumInputs; }
1491 /// getInputConstraint - Return the specified input constraint. Unlike output
1492 /// constraints, these can be empty.
1493 StringRef getInputConstraint(unsigned i) const;
1495 const Expr *getInputExpr(unsigned i) const;
1497 //===--- Other ---===//
1499 unsigned getNumClobbers() const { return NumClobbers; }
1500 StringRef getClobber(unsigned i) const;
1502 static bool classof(const Stmt *T) {
1503 return T->getStmtClass() == GCCAsmStmtClass ||
1504 T->getStmtClass() == MSAsmStmtClass;
1507 // Input expr iterators.
1509 typedef ExprIterator inputs_iterator;
1510 typedef ConstExprIterator const_inputs_iterator;
1511 typedef llvm::iterator_range<inputs_iterator> inputs_range;
1512 typedef llvm::iterator_range<const_inputs_iterator> inputs_const_range;
1514 inputs_iterator begin_inputs() {
1515 return &Exprs[0] + NumOutputs;
1518 inputs_iterator end_inputs() {
1519 return &Exprs[0] + NumOutputs + NumInputs;
1522 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1524 const_inputs_iterator begin_inputs() const {
1525 return &Exprs[0] + NumOutputs;
1528 const_inputs_iterator end_inputs() const {
1529 return &Exprs[0] + NumOutputs + NumInputs;
1532 inputs_const_range inputs() const {
1533 return inputs_const_range(begin_inputs(), end_inputs());
1536 // Output expr iterators.
1538 typedef ExprIterator outputs_iterator;
1539 typedef ConstExprIterator const_outputs_iterator;
1540 typedef llvm::iterator_range<outputs_iterator> outputs_range;
1541 typedef llvm::iterator_range<const_outputs_iterator> outputs_const_range;
1543 outputs_iterator begin_outputs() {
1546 outputs_iterator end_outputs() {
1547 return &Exprs[0] + NumOutputs;
1549 outputs_range outputs() {
1550 return outputs_range(begin_outputs(), end_outputs());
1553 const_outputs_iterator begin_outputs() const {
1556 const_outputs_iterator end_outputs() const {
1557 return &Exprs[0] + NumOutputs;
1559 outputs_const_range outputs() const {
1560 return outputs_const_range(begin_outputs(), end_outputs());
1563 child_range children() {
1564 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1568 /// This represents a GCC inline-assembly statement extension.
1570 class GCCAsmStmt : public AsmStmt {
1571 SourceLocation RParenLoc;
1572 StringLiteral *AsmStr;
1574 // FIXME: If we wanted to, we could allocate all of these in one big array.
1575 StringLiteral **Constraints;
1576 StringLiteral **Clobbers;
1577 IdentifierInfo **Names;
1579 friend class ASTStmtReader;
1582 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1583 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1584 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1585 StringLiteral *asmstr, unsigned numclobbers,
1586 StringLiteral **clobbers, SourceLocation rparenloc);
1588 /// \brief Build an empty inline-assembly statement.
1589 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1590 Constraints(nullptr), Clobbers(nullptr), Names(nullptr) { }
1592 SourceLocation getRParenLoc() const { return RParenLoc; }
1593 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1595 //===--- Asm String Analysis ---===//
1597 const StringLiteral *getAsmString() const { return AsmStr; }
1598 StringLiteral *getAsmString() { return AsmStr; }
1599 void setAsmString(StringLiteral *E) { AsmStr = E; }
1601 /// AsmStringPiece - this is part of a decomposed asm string specification
1602 /// (for use with the AnalyzeAsmString function below). An asm string is
1603 /// considered to be a concatenation of these parts.
1604 class AsmStringPiece {
1607 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1608 Operand // Operand reference, with optional modifier %c4.
1615 // Source range for operand references.
1616 CharSourceRange Range;
1618 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1619 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
1621 : MyKind(Operand), Str(S), OperandNo(OpNo),
1622 Range(CharSourceRange::getCharRange(Begin, End)) {
1625 bool isString() const { return MyKind == String; }
1626 bool isOperand() const { return MyKind == Operand; }
1628 const std::string &getString() const {
1632 unsigned getOperandNo() const {
1633 assert(isOperand());
1637 CharSourceRange getRange() const {
1638 assert(isOperand() && "Range is currently used only for Operands.");
1642 /// getModifier - Get the modifier for this operand, if present. This
1643 /// returns '\0' if there was no modifier.
1644 char getModifier() const;
1647 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1648 /// it into pieces. If the asm string is erroneous, emit errors and return
1649 /// true, otherwise return false. This handles canonicalization and
1650 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1651 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1652 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1653 const ASTContext &C, unsigned &DiagOffs) const;
1655 /// Assemble final IR asm string.
1656 std::string generateAsmString(const ASTContext &C) const;
1658 //===--- Output operands ---===//
1660 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1664 StringRef getOutputName(unsigned i) const {
1665 if (IdentifierInfo *II = getOutputIdentifier(i))
1666 return II->getName();
1671 StringRef getOutputConstraint(unsigned i) const;
1673 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1674 return Constraints[i];
1676 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1677 return Constraints[i];
1680 Expr *getOutputExpr(unsigned i);
1682 const Expr *getOutputExpr(unsigned i) const {
1683 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1686 //===--- Input operands ---===//
1688 IdentifierInfo *getInputIdentifier(unsigned i) const {
1689 return Names[i + NumOutputs];
1692 StringRef getInputName(unsigned i) const {
1693 if (IdentifierInfo *II = getInputIdentifier(i))
1694 return II->getName();
1699 StringRef getInputConstraint(unsigned i) const;
1701 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1702 return Constraints[i + NumOutputs];
1704 StringLiteral *getInputConstraintLiteral(unsigned i) {
1705 return Constraints[i + NumOutputs];
1708 Expr *getInputExpr(unsigned i);
1709 void setInputExpr(unsigned i, Expr *E);
1711 const Expr *getInputExpr(unsigned i) const {
1712 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1716 void setOutputsAndInputsAndClobbers(const ASTContext &C,
1717 IdentifierInfo **Names,
1718 StringLiteral **Constraints,
1720 unsigned NumOutputs,
1722 StringLiteral **Clobbers,
1723 unsigned NumClobbers);
1726 //===--- Other ---===//
1728 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1729 /// translate this into a numeric value needed to reference the same operand.
1730 /// This returns -1 if the operand name is invalid.
1731 int getNamedOperand(StringRef SymbolicName) const;
1733 StringRef getClobber(unsigned i) const;
1734 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1735 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1739 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1740 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1742 static bool classof(const Stmt *T) {
1743 return T->getStmtClass() == GCCAsmStmtClass;
1747 /// This represents a Microsoft inline-assembly statement extension.
1749 class MSAsmStmt : public AsmStmt {
1750 SourceLocation LBraceLoc, EndLoc;
1753 unsigned NumAsmToks;
1756 StringRef *Constraints;
1757 StringRef *Clobbers;
1759 friend class ASTStmtReader;
1762 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1763 SourceLocation lbraceloc, bool issimple, bool isvolatile,
1764 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1765 ArrayRef<StringRef> constraints,
1766 ArrayRef<Expr*> exprs, StringRef asmstr,
1767 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1769 /// \brief Build an empty MS-style inline-assembly statement.
1770 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1771 NumAsmToks(0), AsmToks(nullptr), Constraints(nullptr), Clobbers(nullptr) { }
1773 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1774 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1775 SourceLocation getEndLoc() const { return EndLoc; }
1776 void setEndLoc(SourceLocation L) { EndLoc = L; }
1778 bool hasBraces() const { return LBraceLoc.isValid(); }
1780 unsigned getNumAsmToks() { return NumAsmToks; }
1781 Token *getAsmToks() { return AsmToks; }
1783 //===--- Asm String Analysis ---===//
1784 StringRef getAsmString() const { return AsmStr; }
1786 /// Assemble final IR asm string.
1787 std::string generateAsmString(const ASTContext &C) const;
1789 //===--- Output operands ---===//
1791 StringRef getOutputConstraint(unsigned i) const {
1792 assert(i < NumOutputs);
1793 return Constraints[i];
1796 Expr *getOutputExpr(unsigned i);
1798 const Expr *getOutputExpr(unsigned i) const {
1799 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1802 //===--- Input operands ---===//
1804 StringRef getInputConstraint(unsigned i) const {
1805 assert(i < NumInputs);
1806 return Constraints[i + NumOutputs];
1809 Expr *getInputExpr(unsigned i);
1810 void setInputExpr(unsigned i, Expr *E);
1812 const Expr *getInputExpr(unsigned i) const {
1813 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1816 //===--- Other ---===//
1818 ArrayRef<StringRef> getAllConstraints() const {
1819 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
1821 ArrayRef<StringRef> getClobbers() const {
1822 return llvm::makeArrayRef(Clobbers, NumClobbers);
1824 ArrayRef<Expr*> getAllExprs() const {
1825 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
1826 NumInputs + NumOutputs);
1829 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1832 void initialize(const ASTContext &C, StringRef AsmString,
1833 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1834 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1837 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1838 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1840 static bool classof(const Stmt *T) {
1841 return T->getStmtClass() == MSAsmStmtClass;
1844 child_range children() {
1845 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1849 class SEHExceptStmt : public Stmt {
1853 enum { FILTER_EXPR, BLOCK };
1855 SEHExceptStmt(SourceLocation Loc,
1859 friend class ASTReader;
1860 friend class ASTStmtReader;
1861 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1864 static SEHExceptStmt* Create(const ASTContext &C,
1865 SourceLocation ExceptLoc,
1869 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1870 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1872 SourceLocation getExceptLoc() const { return Loc; }
1873 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1875 Expr *getFilterExpr() const {
1876 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1879 CompoundStmt *getBlock() const {
1880 return cast<CompoundStmt>(Children[BLOCK]);
1883 child_range children() {
1884 return child_range(Children,Children+2);
1887 static bool classof(const Stmt *T) {
1888 return T->getStmtClass() == SEHExceptStmtClass;
1893 class SEHFinallyStmt : public Stmt {
1897 SEHFinallyStmt(SourceLocation Loc,
1900 friend class ASTReader;
1901 friend class ASTStmtReader;
1902 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1905 static SEHFinallyStmt* Create(const ASTContext &C,
1906 SourceLocation FinallyLoc,
1909 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1910 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1912 SourceLocation getFinallyLoc() const { return Loc; }
1913 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1915 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1917 child_range children() {
1918 return child_range(&Block,&Block+1);
1921 static bool classof(const Stmt *T) {
1922 return T->getStmtClass() == SEHFinallyStmtClass;
1927 class SEHTryStmt : public Stmt {
1929 SourceLocation TryLoc;
1932 enum { TRY = 0, HANDLER = 1 };
1934 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1935 SourceLocation TryLoc,
1939 friend class ASTReader;
1940 friend class ASTStmtReader;
1941 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1944 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
1945 SourceLocation TryLoc, Stmt *TryBlock,
1948 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1949 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1951 SourceLocation getTryLoc() const { return TryLoc; }
1952 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1954 bool getIsCXXTry() const { return IsCXXTry; }
1956 CompoundStmt* getTryBlock() const {
1957 return cast<CompoundStmt>(Children[TRY]);
1960 Stmt *getHandler() const { return Children[HANDLER]; }
1962 /// Returns 0 if not defined
1963 SEHExceptStmt *getExceptHandler() const;
1964 SEHFinallyStmt *getFinallyHandler() const;
1966 child_range children() {
1967 return child_range(Children,Children+2);
1970 static bool classof(const Stmt *T) {
1971 return T->getStmtClass() == SEHTryStmtClass;
1975 /// Represents a __leave statement.
1977 class SEHLeaveStmt : public Stmt {
1978 SourceLocation LeaveLoc;
1980 explicit SEHLeaveStmt(SourceLocation LL)
1981 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
1983 /// \brief Build an empty __leave statement.
1984 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) { }
1986 SourceLocation getLeaveLoc() const { return LeaveLoc; }
1987 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
1989 SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; }
1990 SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; }
1992 static bool classof(const Stmt *T) {
1993 return T->getStmtClass() == SEHLeaveStmtClass;
1997 child_range children() {
1998 return child_range(child_iterator(), child_iterator());
2002 /// \brief This captures a statement into a function. For example, the following
2003 /// pragma annotated compound statement can be represented as a CapturedStmt,
2004 /// and this compound statement is the body of an anonymous outlined function.
2006 /// #pragma omp parallel
2011 class CapturedStmt : public Stmt {
2013 /// \brief The different capture forms: by 'this', by reference, capture for
2014 /// variable-length array type etc.
2015 enum VariableCaptureKind {
2022 /// \brief Describes the capture of either a variable, or 'this', or
2023 /// variable-length array type.
2025 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2029 /// \brief Create a new capture.
2031 /// \param Loc The source location associated with this capture.
2033 /// \param Kind The kind of capture (this, ByRef, ...).
2035 /// \param Var The variable being captured, or null if capturing this.
2037 Capture(SourceLocation Loc, VariableCaptureKind Kind,
2038 VarDecl *Var = nullptr);
2040 /// \brief Determine the kind of capture.
2041 VariableCaptureKind getCaptureKind() const;
2043 /// \brief Retrieve the source location at which the variable or 'this' was
2045 SourceLocation getLocation() const { return Loc; }
2047 /// \brief Determine whether this capture handles the C++ 'this' pointer.
2048 bool capturesThis() const { return getCaptureKind() == VCK_This; }
2050 /// \brief Determine whether this capture handles a variable (by reference).
2051 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2053 /// \brief Determine whether this capture handles a variable by copy.
2054 bool capturesVariableByCopy() const {
2055 return getCaptureKind() == VCK_ByCopy;
2058 /// \brief Determine whether this capture handles a variable-length array
2060 bool capturesVariableArrayType() const {
2061 return getCaptureKind() == VCK_VLAType;
2064 /// \brief Retrieve the declaration of the variable being captured.
2066 /// This operation is only valid if this capture captures a variable.
2067 VarDecl *getCapturedVar() const;
2069 friend class ASTStmtReader;
2073 /// \brief The number of variable captured, including 'this'.
2074 unsigned NumCaptures;
2076 /// \brief The pointer part is the implicit the outlined function and the
2077 /// int part is the captured region kind, 'CR_Default' etc.
2078 llvm::PointerIntPair<CapturedDecl *, 1, CapturedRegionKind> CapDeclAndKind;
2080 /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl.
2081 RecordDecl *TheRecordDecl;
2083 /// \brief Construct a captured statement.
2084 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2085 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2087 /// \brief Construct an empty captured statement.
2088 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2090 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
2092 Stmt *const *getStoredStmts() const {
2093 return reinterpret_cast<Stmt *const *>(this + 1);
2096 Capture *getStoredCaptures() const;
2098 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2101 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2102 CapturedRegionKind Kind,
2103 ArrayRef<Capture> Captures,
2104 ArrayRef<Expr *> CaptureInits,
2105 CapturedDecl *CD, RecordDecl *RD);
2107 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2108 unsigned NumCaptures);
2110 /// \brief Retrieve the statement being captured.
2111 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2112 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
2114 /// \brief Retrieve the outlined function declaration.
2115 CapturedDecl *getCapturedDecl();
2116 const CapturedDecl *getCapturedDecl() const;
2118 /// \brief Set the outlined function declaration.
2119 void setCapturedDecl(CapturedDecl *D);
2121 /// \brief Retrieve the captured region kind.
2122 CapturedRegionKind getCapturedRegionKind() const;
2124 /// \brief Set the captured region kind.
2125 void setCapturedRegionKind(CapturedRegionKind Kind);
2127 /// \brief Retrieve the record declaration for captured variables.
2128 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2130 /// \brief Set the record declaration for captured variables.
2131 void setCapturedRecordDecl(RecordDecl *D) {
2132 assert(D && "null RecordDecl");
2136 /// \brief True if this variable has been captured.
2137 bool capturesVariable(const VarDecl *Var) const;
2139 /// \brief An iterator that walks over the captures.
2140 typedef Capture *capture_iterator;
2141 typedef const Capture *const_capture_iterator;
2142 typedef llvm::iterator_range<capture_iterator> capture_range;
2143 typedef llvm::iterator_range<const_capture_iterator> capture_const_range;
2145 capture_range captures() {
2146 return capture_range(capture_begin(), capture_end());
2148 capture_const_range captures() const {
2149 return capture_const_range(capture_begin(), capture_end());
2152 /// \brief Retrieve an iterator pointing to the first capture.
2153 capture_iterator capture_begin() { return getStoredCaptures(); }
2154 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2156 /// \brief Retrieve an iterator pointing past the end of the sequence of
2158 capture_iterator capture_end() const {
2159 return getStoredCaptures() + NumCaptures;
2162 /// \brief Retrieve the number of captures, including 'this'.
2163 unsigned capture_size() const { return NumCaptures; }
2165 /// \brief Iterator that walks over the capture initialization arguments.
2166 typedef Expr **capture_init_iterator;
2167 typedef llvm::iterator_range<capture_init_iterator> capture_init_range;
2169 /// \brief Const iterator that walks over the capture initialization
2171 typedef Expr *const *const_capture_init_iterator;
2172 typedef llvm::iterator_range<const_capture_init_iterator>
2173 const_capture_init_range;
2175 capture_init_range capture_inits() {
2176 return capture_init_range(capture_init_begin(), capture_init_end());
2179 const_capture_init_range capture_inits() const {
2180 return const_capture_init_range(capture_init_begin(), capture_init_end());
2183 /// \brief Retrieve the first initialization argument.
2184 capture_init_iterator capture_init_begin() {
2185 return reinterpret_cast<Expr **>(getStoredStmts());
2188 const_capture_init_iterator capture_init_begin() const {
2189 return reinterpret_cast<Expr *const *>(getStoredStmts());
2192 /// \brief Retrieve the iterator pointing one past the last initialization
2194 capture_init_iterator capture_init_end() {
2195 return capture_init_begin() + NumCaptures;
2198 const_capture_init_iterator capture_init_end() const {
2199 return capture_init_begin() + NumCaptures;
2202 SourceLocation getLocStart() const LLVM_READONLY {
2203 return getCapturedStmt()->getLocStart();
2205 SourceLocation getLocEnd() const LLVM_READONLY {
2206 return getCapturedStmt()->getLocEnd();
2208 SourceRange getSourceRange() const LLVM_READONLY {
2209 return getCapturedStmt()->getSourceRange();
2212 static bool classof(const Stmt *T) {
2213 return T->getStmtClass() == CapturedStmtClass;
2216 child_range children();
2218 friend class ASTStmtReader;
2221 } // end namespace clang