1 //===- Stmt.h - Classes for representing statements -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Stmt interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_STMT_H
15 #define LLVM_CLANG_AST_STMT_H
17 #include "clang/AST/DeclGroup.h"
18 #include "clang/AST/StmtIterator.h"
19 #include "clang/Basic/CapturedStmt.h"
20 #include "clang/Basic/IdentifierTable.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/iterator.h"
27 #include "llvm/ADT/iterator_range.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/ErrorHandling.h"
39 class FoldingSetNodeID;
53 struct PrintingPolicy;
60 //===----------------------------------------------------------------------===//
61 // AST classes for statements.
62 //===----------------------------------------------------------------------===//
64 /// Stmt - This represents one statement.
66 class alignas(void *) Stmt {
70 #define STMT(CLASS, PARENT) CLASS##Class,
71 #define STMT_RANGE(BASE, FIRST, LAST) \
72 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
73 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
74 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
75 #define ABSTRACT_STMT(STMT)
76 #include "clang/AST/StmtNodes.inc"
79 // Make vanilla 'new' and 'delete' illegal for Stmts.
81 friend class ASTStmtReader;
82 friend class ASTStmtWriter;
84 void *operator new(size_t bytes) noexcept {
85 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
88 void operator delete(void *data) noexcept {
89 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
95 /// The statement class.
98 enum { NumStmtBits = 8 };
100 class CompoundStmtBitfields {
101 friend class CompoundStmt;
103 unsigned : NumStmtBits;
105 unsigned NumStmts : 32 - NumStmtBits;
108 class IfStmtBitfields {
111 unsigned : NumStmtBits;
113 unsigned IsConstexpr : 1;
116 class ExprBitfields {
117 friend class ASTStmtReader; // deserialization
118 friend class AtomicExpr; // ctor
119 friend class BlockDeclRefExpr; // ctor
120 friend class CallExpr; // ctor
121 friend class CXXConstructExpr; // ctor
122 friend class CXXDependentScopeMemberExpr; // ctor
123 friend class CXXNewExpr; // ctor
124 friend class CXXUnresolvedConstructExpr; // ctor
125 friend class DeclRefExpr; // computeDependence
126 friend class DependentScopeDeclRefExpr; // ctor
127 friend class DesignatedInitExpr; // ctor
129 friend class InitListExpr; // ctor
130 friend class ObjCArrayLiteral; // ctor
131 friend class ObjCDictionaryLiteral; // ctor
132 friend class ObjCMessageExpr; // ctor
133 friend class OffsetOfExpr; // ctor
134 friend class OpaqueValueExpr; // ctor
135 friend class OverloadExpr; // ctor
136 friend class ParenListExpr; // ctor
137 friend class PseudoObjectExpr; // ctor
138 friend class ShuffleVectorExpr; // ctor
140 unsigned : NumStmtBits;
142 unsigned ValueKind : 2;
143 unsigned ObjectKind : 3;
144 unsigned TypeDependent : 1;
145 unsigned ValueDependent : 1;
146 unsigned InstantiationDependent : 1;
147 unsigned ContainsUnexpandedParameterPack : 1;
149 enum { NumExprBits = 17 };
151 class CharacterLiteralBitfields {
152 friend class CharacterLiteral;
154 unsigned : NumExprBits;
159 enum APFloatSemantics {
168 class FloatingLiteralBitfields {
169 friend class FloatingLiteral;
171 unsigned : NumExprBits;
173 unsigned Semantics : 3; // Provides semantics for APFloat construction
174 unsigned IsExact : 1;
177 class UnaryExprOrTypeTraitExprBitfields {
178 friend class UnaryExprOrTypeTraitExpr;
180 unsigned : NumExprBits;
183 unsigned IsType : 1; // true if operand is a type, false if an expression.
186 class DeclRefExprBitfields {
187 friend class ASTStmtReader; // deserialization
188 friend class DeclRefExpr;
190 unsigned : NumExprBits;
192 unsigned HasQualifier : 1;
193 unsigned HasTemplateKWAndArgsInfo : 1;
194 unsigned HasFoundDecl : 1;
195 unsigned HadMultipleCandidates : 1;
196 unsigned RefersToEnclosingVariableOrCapture : 1;
199 class CastExprBitfields {
200 friend class CastExpr;
201 friend class ImplicitCastExpr;
203 unsigned : NumExprBits;
206 unsigned PartOfExplicitCast : 1; // Only set for ImplicitCastExpr.
207 unsigned BasePathIsEmpty : 1;
210 class CallExprBitfields {
211 friend class CallExpr;
213 unsigned : NumExprBits;
215 unsigned NumPreArgs : 1;
218 class ExprWithCleanupsBitfields {
219 friend class ASTStmtReader; // deserialization
220 friend class ExprWithCleanups;
222 unsigned : NumExprBits;
224 // When false, it must not have side effects.
225 unsigned CleanupsHaveSideEffects : 1;
227 unsigned NumObjects : 32 - 1 - NumExprBits;
230 class PseudoObjectExprBitfields {
231 friend class ASTStmtReader; // deserialization
232 friend class PseudoObjectExpr;
234 unsigned : NumExprBits;
236 // These don't need to be particularly wide, because they're
237 // strictly limited by the forms of expressions we permit.
238 unsigned NumSubExprs : 8;
239 unsigned ResultIndex : 32 - 8 - NumExprBits;
242 class OpaqueValueExprBitfields {
243 friend class OpaqueValueExpr;
245 unsigned : NumExprBits;
247 /// The OVE is a unique semantic reference to its source expressio if this
248 /// bit is set to true.
249 unsigned IsUnique : 1;
252 class ObjCIndirectCopyRestoreExprBitfields {
253 friend class ObjCIndirectCopyRestoreExpr;
255 unsigned : NumExprBits;
257 unsigned ShouldCopy : 1;
260 class InitListExprBitfields {
261 friend class InitListExpr;
263 unsigned : NumExprBits;
265 /// Whether this initializer list originally had a GNU array-range
266 /// designator in it. This is a temporary marker used by CodeGen.
267 unsigned HadArrayRangeDesignator : 1;
270 class TypeTraitExprBitfields {
271 friend class ASTStmtReader;
272 friend class ASTStmtWriter;
273 friend class TypeTraitExpr;
275 unsigned : NumExprBits;
277 /// The kind of type trait, which is a value of a TypeTrait enumerator.
280 /// If this expression is not value-dependent, this indicates whether
281 /// the trait evaluated true or false.
284 /// The number of arguments to this type trait.
285 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
288 class CoawaitExprBitfields {
289 friend class CoawaitExpr;
291 unsigned : NumExprBits;
293 unsigned IsImplicit : 1;
297 StmtBitfields StmtBits;
298 CompoundStmtBitfields CompoundStmtBits;
299 IfStmtBitfields IfStmtBits;
300 ExprBitfields ExprBits;
301 CharacterLiteralBitfields CharacterLiteralBits;
302 FloatingLiteralBitfields FloatingLiteralBits;
303 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
304 DeclRefExprBitfields DeclRefExprBits;
305 CastExprBitfields CastExprBits;
306 CallExprBitfields CallExprBits;
307 ExprWithCleanupsBitfields ExprWithCleanupsBits;
308 PseudoObjectExprBitfields PseudoObjectExprBits;
309 OpaqueValueExprBitfields OpaqueValueExprBits;
310 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
311 InitListExprBitfields InitListExprBits;
312 TypeTraitExprBitfields TypeTraitExprBits;
313 CoawaitExprBitfields CoawaitBits;
317 // Only allow allocation of Stmts using the allocator in ASTContext
318 // or by doing a placement new.
319 void* operator new(size_t bytes, const ASTContext& C,
320 unsigned alignment = 8);
322 void* operator new(size_t bytes, const ASTContext* C,
323 unsigned alignment = 8) {
324 return operator new(bytes, *C, alignment);
327 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
329 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
330 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
331 void operator delete(void *, size_t) noexcept {}
332 void operator delete(void *, void *) noexcept {}
335 /// A placeholder type used to construct an empty shell of a
336 /// type, that will be filled in later (e.g., by some
337 /// de-serialization).
338 struct EmptyShell {};
341 /// Iterator for iterating over Stmt * arrays that contain only Expr *
343 /// This is needed because AST nodes use Stmt* arrays to store
344 /// references to children (to be compatible with StmtIterator).
346 : llvm::iterator_adaptor_base<ExprIterator, Stmt **,
347 std::random_access_iterator_tag, Expr *> {
348 ExprIterator() : iterator_adaptor_base(nullptr) {}
349 ExprIterator(Stmt **I) : iterator_adaptor_base(I) {}
351 reference operator*() const {
352 assert((*I)->getStmtClass() >= firstExprConstant &&
353 (*I)->getStmtClass() <= lastExprConstant);
354 return *reinterpret_cast<Expr **>(I);
358 /// Const iterator for iterating over Stmt * arrays that contain only Expr *
359 struct ConstExprIterator
360 : llvm::iterator_adaptor_base<ConstExprIterator, const Stmt *const *,
361 std::random_access_iterator_tag,
363 ConstExprIterator() : iterator_adaptor_base(nullptr) {}
364 ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {}
366 reference operator*() const {
367 assert((*I)->getStmtClass() >= firstExprConstant &&
368 (*I)->getStmtClass() <= lastExprConstant);
369 return *reinterpret_cast<const Expr *const *>(I);
374 /// Whether statistic collection is enabled.
375 static bool StatisticsEnabled;
378 /// Construct an empty statement.
379 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
383 static_assert(sizeof(*this) == sizeof(void *),
384 "changing bitfields changed sizeof(Stmt)");
385 static_assert(sizeof(*this) % alignof(void *) == 0,
386 "Insufficient alignment!");
387 StmtBits.sClass = SC;
388 if (StatisticsEnabled) Stmt::addStmtClass(SC);
391 StmtClass getStmtClass() const {
392 return static_cast<StmtClass>(StmtBits.sClass);
395 const char *getStmtClassName() const;
397 /// SourceLocation tokens are not useful in isolation - they are low level
398 /// value objects created/interpreted by SourceManager. We assume AST
399 /// clients will have a pointer to the respective SourceManager.
400 SourceRange getSourceRange() const LLVM_READONLY;
401 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
402 SourceLocation getBeginLoc() const LLVM_READONLY;
403 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
404 SourceLocation getEndLoc() const LLVM_READONLY;
406 // global temp stats (until we have a per-module visitor)
407 static void addStmtClass(const StmtClass s);
408 static void EnableStatistics();
409 static void PrintStats();
411 /// Dumps the specified AST fragment and all subtrees to
414 void dump(SourceManager &SM) const;
415 void dump(raw_ostream &OS, SourceManager &SM) const;
416 void dump(raw_ostream &OS) const;
418 /// dumpColor - same as dump(), but forces color highlighting.
419 void dumpColor() const;
421 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
422 /// back to its original source language syntax.
423 void dumpPretty(const ASTContext &Context) const;
424 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
425 const PrintingPolicy &Policy, unsigned Indentation = 0,
426 const ASTContext *Context = nullptr) const;
428 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
429 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
430 void viewAST() const;
432 /// Skip past any implicit AST nodes which might surround this
433 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
434 Stmt *IgnoreImplicit();
435 const Stmt *IgnoreImplicit() const {
436 return const_cast<Stmt *>(this)->IgnoreImplicit();
439 /// Skip no-op (attributed, compound) container stmts and skip captured
440 /// stmt at the top, if \a IgnoreCaptured is true.
441 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
442 const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
443 return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
446 const Stmt *stripLabelLikeStatements() const;
447 Stmt *stripLabelLikeStatements() {
448 return const_cast<Stmt*>(
449 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
452 /// Child Iterators: All subclasses must implement 'children'
453 /// to permit easy iteration over the substatements/subexpessions of an
454 /// AST node. This permits easy iteration over all nodes in the AST.
455 using child_iterator = StmtIterator;
456 using const_child_iterator = ConstStmtIterator;
458 using child_range = llvm::iterator_range<child_iterator>;
459 using const_child_range = llvm::iterator_range<const_child_iterator>;
461 child_range children();
463 const_child_range children() const {
464 auto Children = const_cast<Stmt *>(this)->children();
465 return const_child_range(Children.begin(), Children.end());
468 child_iterator child_begin() { return children().begin(); }
469 child_iterator child_end() { return children().end(); }
471 const_child_iterator child_begin() const { return children().begin(); }
472 const_child_iterator child_end() const { return children().end(); }
474 /// Produce a unique representation of the given statement.
476 /// \param ID once the profiling operation is complete, will contain
477 /// the unique representation of the given statement.
479 /// \param Context the AST context in which the statement resides
481 /// \param Canonical whether the profile should be based on the canonical
482 /// representation of this statement (e.g., where non-type template
483 /// parameters are identified by index/level rather than their
484 /// declaration pointers) or the exact representation of the statement as
485 /// written in the source.
486 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
487 bool Canonical) const;
489 /// Calculate a unique representation for a statement that is
490 /// stable across compiler invocations.
492 /// \param ID profile information will be stored in ID.
494 /// \param Hash an ODRHash object which will be called where pointers would
495 /// have been used in the Profile function.
496 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
499 /// DeclStmt - Adaptor class for mixing declarations with statements and
500 /// expressions. For example, CompoundStmt mixes statements, expressions
501 /// and declarations (variables, types). Another example is ForStmt, where
502 /// the first statement can be an expression or a declaration.
503 class DeclStmt : public Stmt {
505 SourceLocation StartLoc, EndLoc;
508 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
509 : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
511 /// Build an empty declaration statement.
512 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
514 /// isSingleDecl - This method returns true if this DeclStmt refers
515 /// to a single Decl.
516 bool isSingleDecl() const {
517 return DG.isSingleDecl();
520 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
521 Decl *getSingleDecl() { return DG.getSingleDecl(); }
523 const DeclGroupRef getDeclGroup() const { return DG; }
524 DeclGroupRef getDeclGroup() { return DG; }
525 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
527 SourceLocation getStartLoc() const LLVM_READONLY { return getBeginLoc(); }
528 void setStartLoc(SourceLocation L) { StartLoc = L; }
529 SourceLocation getEndLoc() const { return EndLoc; }
530 void setEndLoc(SourceLocation L) { EndLoc = L; }
532 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
533 SourceLocation getBeginLoc() const LLVM_READONLY { return StartLoc; }
534 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
536 static bool classof(const Stmt *T) {
537 return T->getStmtClass() == DeclStmtClass;
540 // Iterators over subexpressions.
541 child_range children() {
542 return child_range(child_iterator(DG.begin(), DG.end()),
543 child_iterator(DG.end(), DG.end()));
546 using decl_iterator = DeclGroupRef::iterator;
547 using const_decl_iterator = DeclGroupRef::const_iterator;
548 using decl_range = llvm::iterator_range<decl_iterator>;
549 using decl_const_range = llvm::iterator_range<const_decl_iterator>;
551 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
553 decl_const_range decls() const {
554 return decl_const_range(decl_begin(), decl_end());
557 decl_iterator decl_begin() { return DG.begin(); }
558 decl_iterator decl_end() { return DG.end(); }
559 const_decl_iterator decl_begin() const { return DG.begin(); }
560 const_decl_iterator decl_end() const { return DG.end(); }
562 using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
564 reverse_decl_iterator decl_rbegin() {
565 return reverse_decl_iterator(decl_end());
568 reverse_decl_iterator decl_rend() {
569 return reverse_decl_iterator(decl_begin());
573 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
575 class NullStmt : public Stmt {
576 SourceLocation SemiLoc;
578 /// True if the null statement was preceded by an empty macro, e.g:
583 bool HasLeadingEmptyMacro = false;
586 friend class ASTStmtReader;
587 friend class ASTStmtWriter;
589 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
590 : Stmt(NullStmtClass), SemiLoc(L),
591 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
593 /// Build an empty null statement.
594 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
596 SourceLocation getSemiLoc() const { return SemiLoc; }
597 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
599 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
601 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
602 SourceLocation getBeginLoc() const LLVM_READONLY { return SemiLoc; }
603 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
604 SourceLocation getEndLoc() const LLVM_READONLY { return SemiLoc; }
606 static bool classof(const Stmt *T) {
607 return T->getStmtClass() == NullStmtClass;
610 child_range children() {
611 return child_range(child_iterator(), child_iterator());
615 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
616 class CompoundStmt final : public Stmt,
617 private llvm::TrailingObjects<CompoundStmt, Stmt *> {
618 friend class ASTStmtReader;
619 friend TrailingObjects;
621 SourceLocation LBraceLoc, RBraceLoc;
623 CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
624 explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
626 void setStmts(ArrayRef<Stmt *> Stmts);
629 static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
630 SourceLocation LB, SourceLocation RB);
632 // Build an empty compound statement with a location.
633 explicit CompoundStmt(SourceLocation Loc)
634 : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(Loc) {
635 CompoundStmtBits.NumStmts = 0;
638 // Build an empty compound statement.
639 static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
641 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
642 unsigned size() const { return CompoundStmtBits.NumStmts; }
644 using body_iterator = Stmt **;
645 using body_range = llvm::iterator_range<body_iterator>;
647 body_range body() { return body_range(body_begin(), body_end()); }
648 body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
649 body_iterator body_end() { return body_begin() + size(); }
650 Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
653 return !body_empty() ? body_begin()[size() - 1] : nullptr;
656 void setLastStmt(Stmt *S) {
657 assert(!body_empty() && "setLastStmt");
658 body_begin()[size() - 1] = S;
661 using const_body_iterator = Stmt* const *;
662 using body_const_range = llvm::iterator_range<const_body_iterator>;
664 body_const_range body() const {
665 return body_const_range(body_begin(), body_end());
668 const_body_iterator body_begin() const {
669 return getTrailingObjects<Stmt *>();
672 const_body_iterator body_end() const { return body_begin() + size(); }
674 const Stmt *body_front() const {
675 return !body_empty() ? body_begin()[0] : nullptr;
678 const Stmt *body_back() const {
679 return !body_empty() ? body_begin()[size() - 1] : nullptr;
682 using reverse_body_iterator = std::reverse_iterator<body_iterator>;
684 reverse_body_iterator body_rbegin() {
685 return reverse_body_iterator(body_end());
688 reverse_body_iterator body_rend() {
689 return reverse_body_iterator(body_begin());
692 using const_reverse_body_iterator =
693 std::reverse_iterator<const_body_iterator>;
695 const_reverse_body_iterator body_rbegin() const {
696 return const_reverse_body_iterator(body_end());
699 const_reverse_body_iterator body_rend() const {
700 return const_reverse_body_iterator(body_begin());
703 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
704 SourceLocation getBeginLoc() const LLVM_READONLY { return LBraceLoc; }
705 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
706 SourceLocation getEndLoc() const LLVM_READONLY { return RBraceLoc; }
708 SourceLocation getLBracLoc() const { return LBraceLoc; }
709 SourceLocation getRBracLoc() const { return RBraceLoc; }
711 static bool classof(const Stmt *T) {
712 return T->getStmtClass() == CompoundStmtClass;
716 child_range children() { return child_range(body_begin(), body_end()); }
718 const_child_range children() const {
719 return const_child_range(body_begin(), body_end());
723 // SwitchCase is the base class for CaseStmt and DefaultStmt,
724 class SwitchCase : public Stmt {
726 // A pointer to the following CaseStmt or DefaultStmt class,
727 // used by SwitchStmt.
728 SwitchCase *NextSwitchCase = nullptr;
729 SourceLocation KeywordLoc;
730 SourceLocation ColonLoc;
732 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
733 : Stmt(SC), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {}
735 SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
738 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
740 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
742 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
744 SourceLocation getKeywordLoc() const { return KeywordLoc; }
745 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
746 SourceLocation getColonLoc() const { return ColonLoc; }
747 void setColonLoc(SourceLocation L) { ColonLoc = L; }
750 const Stmt *getSubStmt() const {
751 return const_cast<SwitchCase*>(this)->getSubStmt();
754 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
755 SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
756 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
757 SourceLocation getEndLoc() const LLVM_READONLY;
759 static bool classof(const Stmt *T) {
760 return T->getStmtClass() == CaseStmtClass ||
761 T->getStmtClass() == DefaultStmtClass;
765 class CaseStmt : public SwitchCase {
766 SourceLocation EllipsisLoc;
767 enum { LHS, RHS, SUBSTMT, END_EXPR };
768 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
769 // GNU "case 1 ... 4" extension
772 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
773 SourceLocation ellipsisLoc, SourceLocation colonLoc)
774 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
775 SubExprs[SUBSTMT] = nullptr;
776 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
777 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
778 EllipsisLoc = ellipsisLoc;
781 /// Build an empty switch case statement.
782 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) {}
784 SourceLocation getCaseLoc() const { return KeywordLoc; }
785 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
786 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
787 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
788 SourceLocation getColonLoc() const { return ColonLoc; }
789 void setColonLoc(SourceLocation L) { ColonLoc = L; }
791 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
792 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
793 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
795 const Expr *getLHS() const {
796 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
799 const Expr *getRHS() const {
800 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
803 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
805 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
806 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
807 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
809 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
810 SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
812 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
813 SourceLocation getEndLoc() const LLVM_READONLY {
814 // Handle deeply nested case statements with iteration instead of recursion.
815 const CaseStmt *CS = this;
816 while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
819 return CS->getSubStmt()->getLocEnd();
822 static bool classof(const Stmt *T) {
823 return T->getStmtClass() == CaseStmtClass;
827 child_range children() {
828 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
832 class DefaultStmt : public SwitchCase {
836 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
837 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
839 /// Build an empty default statement.
840 explicit DefaultStmt(EmptyShell Empty)
841 : SwitchCase(DefaultStmtClass, Empty) {}
843 Stmt *getSubStmt() { return SubStmt; }
844 const Stmt *getSubStmt() const { return SubStmt; }
845 void setSubStmt(Stmt *S) { SubStmt = S; }
847 SourceLocation getDefaultLoc() const { return KeywordLoc; }
848 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
849 SourceLocation getColonLoc() const { return ColonLoc; }
850 void setColonLoc(SourceLocation L) { ColonLoc = L; }
852 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
853 SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
854 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
855 SourceLocation getEndLoc() const LLVM_READONLY {
856 return SubStmt->getLocEnd();
859 static bool classof(const Stmt *T) {
860 return T->getStmtClass() == DefaultStmtClass;
864 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
867 inline SourceLocation SwitchCase::getEndLoc() const {
868 if (const auto *CS = dyn_cast<CaseStmt>(this))
869 return CS->getLocEnd();
870 return cast<DefaultStmt>(this)->getLocEnd();
873 /// LabelStmt - Represents a label, which has a substatement. For example:
875 class LabelStmt : public Stmt {
876 SourceLocation IdentLoc;
881 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
882 : Stmt(LabelStmtClass), IdentLoc(IL), TheDecl(D), SubStmt(substmt) {
883 static_assert(sizeof(LabelStmt) ==
884 2 * sizeof(SourceLocation) + 2 * sizeof(void *),
885 "LabelStmt too big");
888 // Build an empty label statement.
889 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) {}
891 SourceLocation getIdentLoc() const { return IdentLoc; }
892 LabelDecl *getDecl() const { return TheDecl; }
893 void setDecl(LabelDecl *D) { TheDecl = D; }
894 const char *getName() const;
895 Stmt *getSubStmt() { return SubStmt; }
896 const Stmt *getSubStmt() const { return SubStmt; }
897 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
898 void setSubStmt(Stmt *SS) { SubStmt = SS; }
900 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
901 SourceLocation getBeginLoc() const LLVM_READONLY { return IdentLoc; }
902 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
903 SourceLocation getEndLoc() const LLVM_READONLY {
904 return SubStmt->getLocEnd();
907 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
909 static bool classof(const Stmt *T) {
910 return T->getStmtClass() == LabelStmtClass;
914 /// Represents an attribute applied to a statement.
916 /// Represents an attribute applied to a statement. For example:
917 /// [[omp::for(...)]] for (...) { ... }
918 class AttributedStmt final
920 private llvm::TrailingObjects<AttributedStmt, const Attr *> {
921 friend class ASTStmtReader;
922 friend TrailingObjects;
925 SourceLocation AttrLoc;
928 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
929 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
930 NumAttrs(Attrs.size()) {
931 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
934 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
935 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
936 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
939 const Attr *const *getAttrArrayPtr() const {
940 return getTrailingObjects<const Attr *>();
942 const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
945 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
946 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
948 // Build an empty attributed statement.
949 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
951 SourceLocation getAttrLoc() const { return AttrLoc; }
952 ArrayRef<const Attr*> getAttrs() const {
953 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs);
956 Stmt *getSubStmt() { return SubStmt; }
957 const Stmt *getSubStmt() const { return SubStmt; }
959 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
960 SourceLocation getBeginLoc() const LLVM_READONLY { return AttrLoc; }
961 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
962 SourceLocation getEndLoc() const LLVM_READONLY {
963 return SubStmt->getLocEnd();
966 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
968 static bool classof(const Stmt *T) {
969 return T->getStmtClass() == AttributedStmtClass;
973 /// IfStmt - This represents an if/then/else.
974 class IfStmt : public Stmt {
975 enum { INIT, VAR, COND, THEN, ELSE, END_EXPR };
976 Stmt* SubExprs[END_EXPR];
978 SourceLocation IfLoc;
979 SourceLocation ElseLoc;
982 IfStmt(const ASTContext &C, SourceLocation IL,
983 bool IsConstexpr, Stmt *init, VarDecl *var, Expr *cond,
984 Stmt *then, SourceLocation EL = SourceLocation(),
985 Stmt *elsev = nullptr);
987 /// Build an empty if/then/else statement
988 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) {}
990 /// Retrieve the variable declared in this "if" statement, if any.
992 /// In the following example, "x" is the condition variable.
994 /// if (int x = foo()) {
995 /// printf("x is %d", x);
998 VarDecl *getConditionVariable() const;
999 void setConditionVariable(const ASTContext &C, VarDecl *V);
1001 /// If this IfStmt has a condition variable, return the faux DeclStmt
1002 /// associated with the creation of that condition variable.
1003 const DeclStmt *getConditionVariableDeclStmt() const {
1004 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1007 Stmt *getInit() { return SubExprs[INIT]; }
1008 const Stmt *getInit() const { return SubExprs[INIT]; }
1009 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1010 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1011 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
1012 const Stmt *getThen() const { return SubExprs[THEN]; }
1013 void setThen(Stmt *S) { SubExprs[THEN] = S; }
1014 const Stmt *getElse() const { return SubExprs[ELSE]; }
1015 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
1017 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1018 Stmt *getThen() { return SubExprs[THEN]; }
1019 Stmt *getElse() { return SubExprs[ELSE]; }
1021 SourceLocation getIfLoc() const { return IfLoc; }
1022 void setIfLoc(SourceLocation L) { IfLoc = L; }
1023 SourceLocation getElseLoc() const { return ElseLoc; }
1024 void setElseLoc(SourceLocation L) { ElseLoc = L; }
1026 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
1027 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
1029 bool isObjCAvailabilityCheck() const;
1031 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1032 SourceLocation getBeginLoc() const LLVM_READONLY { return IfLoc; }
1034 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1035 SourceLocation getEndLoc() const LLVM_READONLY {
1037 return SubExprs[ELSE]->getLocEnd();
1039 return SubExprs[THEN]->getLocEnd();
1042 // Iterators over subexpressions. The iterators will include iterating
1043 // over the initialization expression referenced by the condition variable.
1044 child_range children() {
1045 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1048 static bool classof(const Stmt *T) {
1049 return T->getStmtClass() == IfStmtClass;
1053 /// SwitchStmt - This represents a 'switch' stmt.
1054 class SwitchStmt : public Stmt {
1055 SourceLocation SwitchLoc;
1056 enum { INIT, VAR, COND, BODY, END_EXPR };
1057 Stmt* SubExprs[END_EXPR];
1059 // This points to a linked list of case and default statements and, if the
1060 // SwitchStmt is a switch on an enum value, records whether all the enum
1061 // values were covered by CaseStmts. The coverage information value is meant
1062 // to be a hint for possible clients.
1063 llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase;
1066 SwitchStmt(const ASTContext &C, Stmt *Init, VarDecl *Var, Expr *cond);
1068 /// Build a empty switch statement.
1069 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) {}
1071 /// Retrieve the variable declared in this "switch" statement, if any.
1073 /// In the following example, "x" is the condition variable.
1075 /// switch (int x = foo()) {
1080 VarDecl *getConditionVariable() const;
1081 void setConditionVariable(const ASTContext &C, VarDecl *V);
1083 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
1084 /// associated with the creation of that condition variable.
1085 const DeclStmt *getConditionVariableDeclStmt() const {
1086 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1089 Stmt *getInit() { return SubExprs[INIT]; }
1090 const Stmt *getInit() const { return SubExprs[INIT]; }
1091 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1092 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1093 const Stmt *getBody() const { return SubExprs[BODY]; }
1094 const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); }
1096 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1097 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
1098 Stmt *getBody() { return SubExprs[BODY]; }
1099 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1100 SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); }
1102 /// Set the case list for this switch statement.
1103 void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); }
1105 SourceLocation getSwitchLoc() const { return SwitchLoc; }
1106 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
1108 void setBody(Stmt *S, SourceLocation SL) {
1113 void addSwitchCase(SwitchCase *SC) {
1114 assert(!SC->getNextSwitchCase()
1115 && "case/default already added to a switch");
1116 SC->setNextSwitchCase(FirstCase.getPointer());
1117 FirstCase.setPointer(SC);
1120 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1121 /// switch over an enum value then all cases have been explicitly covered.
1122 void setAllEnumCasesCovered() { FirstCase.setInt(true); }
1124 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1125 /// have been explicitly covered.
1126 bool isAllEnumCasesCovered() const { return FirstCase.getInt(); }
1128 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1129 SourceLocation getBeginLoc() const LLVM_READONLY { return SwitchLoc; }
1131 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1132 SourceLocation getEndLoc() const LLVM_READONLY {
1133 return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
1137 child_range children() {
1138 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1141 static bool classof(const Stmt *T) {
1142 return T->getStmtClass() == SwitchStmtClass;
1146 /// WhileStmt - This represents a 'while' stmt.
1147 class WhileStmt : public Stmt {
1148 SourceLocation WhileLoc;
1149 enum { VAR, COND, BODY, END_EXPR };
1150 Stmt* SubExprs[END_EXPR];
1153 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1156 /// Build an empty while statement.
1157 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) {}
1159 /// Retrieve the variable declared in this "while" statement, if any.
1161 /// In the following example, "x" is the condition variable.
1163 /// while (int x = random()) {
1167 VarDecl *getConditionVariable() const;
1168 void setConditionVariable(const ASTContext &C, VarDecl *V);
1170 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1171 /// associated with the creation of that condition variable.
1172 const DeclStmt *getConditionVariableDeclStmt() const {
1173 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1176 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1177 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1178 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1179 Stmt *getBody() { return SubExprs[BODY]; }
1180 const Stmt *getBody() const { return SubExprs[BODY]; }
1181 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1183 SourceLocation getWhileLoc() const { return WhileLoc; }
1184 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1186 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1187 SourceLocation getBeginLoc() const LLVM_READONLY { return WhileLoc; }
1189 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1190 SourceLocation getEndLoc() const LLVM_READONLY {
1191 return SubExprs[BODY]->getLocEnd();
1194 static bool classof(const Stmt *T) {
1195 return T->getStmtClass() == WhileStmtClass;
1199 child_range children() {
1200 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1204 /// DoStmt - This represents a 'do/while' stmt.
1205 class DoStmt : public Stmt {
1206 SourceLocation DoLoc;
1207 enum { BODY, COND, END_EXPR };
1208 Stmt* SubExprs[END_EXPR];
1209 SourceLocation WhileLoc;
1210 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1213 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1215 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1216 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1217 SubExprs[BODY] = body;
1220 /// Build an empty do-while statement.
1221 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
1223 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1224 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1225 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1226 Stmt *getBody() { return SubExprs[BODY]; }
1227 const Stmt *getBody() const { return SubExprs[BODY]; }
1228 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1230 SourceLocation getDoLoc() const { return DoLoc; }
1231 void setDoLoc(SourceLocation L) { DoLoc = L; }
1232 SourceLocation getWhileLoc() const { return WhileLoc; }
1233 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1235 SourceLocation getRParenLoc() const { return RParenLoc; }
1236 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1238 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1239 SourceLocation getBeginLoc() const LLVM_READONLY { return DoLoc; }
1240 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1241 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
1243 static bool classof(const Stmt *T) {
1244 return T->getStmtClass() == DoStmtClass;
1248 child_range children() {
1249 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1253 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1254 /// the init/cond/inc parts of the ForStmt will be null if they were not
1255 /// specified in the source.
1256 class ForStmt : public Stmt {
1257 SourceLocation ForLoc;
1258 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1259 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1260 SourceLocation LParenLoc, RParenLoc;
1263 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1264 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1267 /// Build an empty for statement.
1268 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
1270 Stmt *getInit() { return SubExprs[INIT]; }
1272 /// Retrieve the variable declared in this "for" statement, if any.
1274 /// In the following example, "y" is the condition variable.
1276 /// for (int x = random(); int y = mangle(x); ++x) {
1280 VarDecl *getConditionVariable() const;
1281 void setConditionVariable(const ASTContext &C, VarDecl *V);
1283 /// If this ForStmt has a condition variable, return the faux DeclStmt
1284 /// associated with the creation of that condition variable.
1285 const DeclStmt *getConditionVariableDeclStmt() const {
1286 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1289 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1290 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1291 Stmt *getBody() { return SubExprs[BODY]; }
1293 const Stmt *getInit() const { return SubExprs[INIT]; }
1294 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1295 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1296 const Stmt *getBody() const { return SubExprs[BODY]; }
1298 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1299 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1300 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1301 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1303 SourceLocation getForLoc() const { return ForLoc; }
1304 void setForLoc(SourceLocation L) { ForLoc = L; }
1305 SourceLocation getLParenLoc() const { return LParenLoc; }
1306 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1307 SourceLocation getRParenLoc() const { return RParenLoc; }
1308 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1310 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1311 SourceLocation getBeginLoc() const LLVM_READONLY { return ForLoc; }
1313 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1314 SourceLocation getEndLoc() const LLVM_READONLY {
1315 return SubExprs[BODY]->getLocEnd();
1318 static bool classof(const Stmt *T) {
1319 return T->getStmtClass() == ForStmtClass;
1323 child_range children() {
1324 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1328 /// GotoStmt - This represents a direct goto.
1329 class GotoStmt : public Stmt {
1331 SourceLocation GotoLoc;
1332 SourceLocation LabelLoc;
1335 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1336 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1338 /// Build an empty goto statement.
1339 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
1341 LabelDecl *getLabel() const { return Label; }
1342 void setLabel(LabelDecl *D) { Label = D; }
1344 SourceLocation getGotoLoc() const { return GotoLoc; }
1345 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1346 SourceLocation getLabelLoc() const { return LabelLoc; }
1347 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1349 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1350 SourceLocation getBeginLoc() const LLVM_READONLY { return GotoLoc; }
1351 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1352 SourceLocation getEndLoc() const LLVM_READONLY { return LabelLoc; }
1354 static bool classof(const Stmt *T) {
1355 return T->getStmtClass() == GotoStmtClass;
1359 child_range children() {
1360 return child_range(child_iterator(), child_iterator());
1364 /// IndirectGotoStmt - This represents an indirect goto.
1365 class IndirectGotoStmt : public Stmt {
1366 SourceLocation GotoLoc;
1367 SourceLocation StarLoc;
1371 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1373 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1374 Target((Stmt*)target) {}
1376 /// Build an empty indirect goto statement.
1377 explicit IndirectGotoStmt(EmptyShell Empty)
1378 : Stmt(IndirectGotoStmtClass, Empty) {}
1380 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1381 SourceLocation getGotoLoc() const { return GotoLoc; }
1382 void setStarLoc(SourceLocation L) { StarLoc = L; }
1383 SourceLocation getStarLoc() const { return StarLoc; }
1385 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1386 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1387 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1389 /// getConstantTarget - Returns the fixed target of this indirect
1390 /// goto, if one exists.
1391 LabelDecl *getConstantTarget();
1392 const LabelDecl *getConstantTarget() const {
1393 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1396 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1397 SourceLocation getBeginLoc() const LLVM_READONLY { return GotoLoc; }
1398 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1399 SourceLocation getEndLoc() const LLVM_READONLY { return Target->getLocEnd(); }
1401 static bool classof(const Stmt *T) {
1402 return T->getStmtClass() == IndirectGotoStmtClass;
1406 child_range children() { return child_range(&Target, &Target+1); }
1409 /// ContinueStmt - This represents a continue.
1410 class ContinueStmt : public Stmt {
1411 SourceLocation ContinueLoc;
1414 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1416 /// Build an empty continue statement.
1417 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
1419 SourceLocation getContinueLoc() const { return ContinueLoc; }
1420 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1422 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1423 SourceLocation getBeginLoc() const LLVM_READONLY { return ContinueLoc; }
1424 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1425 SourceLocation getEndLoc() const LLVM_READONLY { return ContinueLoc; }
1427 static bool classof(const Stmt *T) {
1428 return T->getStmtClass() == ContinueStmtClass;
1432 child_range children() {
1433 return child_range(child_iterator(), child_iterator());
1437 /// BreakStmt - This represents a break.
1438 class BreakStmt : public Stmt {
1439 SourceLocation BreakLoc;
1442 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {
1443 static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation),
1444 "BreakStmt too large");
1447 /// Build an empty break statement.
1448 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
1450 SourceLocation getBreakLoc() const { return BreakLoc; }
1451 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1453 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1454 SourceLocation getBeginLoc() const LLVM_READONLY { return BreakLoc; }
1455 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1456 SourceLocation getEndLoc() const LLVM_READONLY { return BreakLoc; }
1458 static bool classof(const Stmt *T) {
1459 return T->getStmtClass() == BreakStmtClass;
1463 child_range children() {
1464 return child_range(child_iterator(), child_iterator());
1468 /// ReturnStmt - This represents a return, optionally of an expression:
1472 /// Note that GCC allows return with no argument in a function declared to
1473 /// return a value, and it allows returning a value in functions declared to
1474 /// return void. We explicitly model this in the AST, which means you can't
1475 /// depend on the return type of the function and the presence of an argument.
1476 class ReturnStmt : public Stmt {
1477 SourceLocation RetLoc;
1479 const VarDecl *NRVOCandidate;
1482 explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {}
1484 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1485 : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E),
1486 NRVOCandidate(NRVOCandidate) {}
1488 /// Build an empty return expression.
1489 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) {}
1491 const Expr *getRetValue() const;
1492 Expr *getRetValue();
1493 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1495 SourceLocation getReturnLoc() const { return RetLoc; }
1496 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1498 /// Retrieve the variable that might be used for the named return
1499 /// value optimization.
1501 /// The optimization itself can only be performed if the variable is
1502 /// also marked as an NRVO object.
1503 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1504 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1506 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1507 SourceLocation getBeginLoc() const LLVM_READONLY { return RetLoc; }
1509 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1510 SourceLocation getEndLoc() const LLVM_READONLY {
1511 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1514 static bool classof(const Stmt *T) {
1515 return T->getStmtClass() == ReturnStmtClass;
1519 child_range children() {
1520 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1521 return child_range(child_iterator(), child_iterator());
1525 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1526 class AsmStmt : public Stmt {
1528 friend class ASTStmtReader;
1530 SourceLocation AsmLoc;
1532 /// True if the assembly statement does not have any input or output
1536 /// If true, treat this inline assembly as having side effects.
1537 /// This assembly statement should not be optimized, deleted or moved.
1540 unsigned NumOutputs;
1542 unsigned NumClobbers;
1544 Stmt **Exprs = nullptr;
1546 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1547 unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
1548 : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1549 NumOutputs(numoutputs), NumInputs(numinputs),
1550 NumClobbers(numclobbers) {}
1553 /// Build an empty inline-assembly statement.
1554 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
1556 SourceLocation getAsmLoc() const { return AsmLoc; }
1557 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1559 bool isSimple() const { return IsSimple; }
1560 void setSimple(bool V) { IsSimple = V; }
1562 bool isVolatile() const { return IsVolatile; }
1563 void setVolatile(bool V) { IsVolatile = V; }
1565 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1566 SourceLocation getBeginLoc() const LLVM_READONLY { return {}; }
1567 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1568 SourceLocation getEndLoc() const LLVM_READONLY { return {}; }
1570 //===--- Asm String Analysis ---===//
1572 /// Assemble final IR asm string.
1573 std::string generateAsmString(const ASTContext &C) const;
1575 //===--- Output operands ---===//
1577 unsigned getNumOutputs() const { return NumOutputs; }
1579 /// getOutputConstraint - Return the constraint string for the specified
1580 /// output operand. All output constraints are known to be non-empty (either
1582 StringRef getOutputConstraint(unsigned i) const;
1584 /// isOutputPlusConstraint - Return true if the specified output constraint
1585 /// is a "+" constraint (which is both an input and an output) or false if it
1586 /// is an "=" constraint (just an output).
1587 bool isOutputPlusConstraint(unsigned i) const {
1588 return getOutputConstraint(i)[0] == '+';
1591 const Expr *getOutputExpr(unsigned i) const;
1593 /// getNumPlusOperands - Return the number of output operands that have a "+"
1595 unsigned getNumPlusOperands() const;
1597 //===--- Input operands ---===//
1599 unsigned getNumInputs() const { return NumInputs; }
1601 /// getInputConstraint - Return the specified input constraint. Unlike output
1602 /// constraints, these can be empty.
1603 StringRef getInputConstraint(unsigned i) const;
1605 const Expr *getInputExpr(unsigned i) const;
1607 //===--- Other ---===//
1609 unsigned getNumClobbers() const { return NumClobbers; }
1610 StringRef getClobber(unsigned i) const;
1612 static bool classof(const Stmt *T) {
1613 return T->getStmtClass() == GCCAsmStmtClass ||
1614 T->getStmtClass() == MSAsmStmtClass;
1617 // Input expr iterators.
1619 using inputs_iterator = ExprIterator;
1620 using const_inputs_iterator = ConstExprIterator;
1621 using inputs_range = llvm::iterator_range<inputs_iterator>;
1622 using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
1624 inputs_iterator begin_inputs() {
1625 return &Exprs[0] + NumOutputs;
1628 inputs_iterator end_inputs() {
1629 return &Exprs[0] + NumOutputs + NumInputs;
1632 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1634 const_inputs_iterator begin_inputs() const {
1635 return &Exprs[0] + NumOutputs;
1638 const_inputs_iterator end_inputs() const {
1639 return &Exprs[0] + NumOutputs + NumInputs;
1642 inputs_const_range inputs() const {
1643 return inputs_const_range(begin_inputs(), end_inputs());
1646 // Output expr iterators.
1648 using outputs_iterator = ExprIterator;
1649 using const_outputs_iterator = ConstExprIterator;
1650 using outputs_range = llvm::iterator_range<outputs_iterator>;
1651 using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
1653 outputs_iterator begin_outputs() {
1657 outputs_iterator end_outputs() {
1658 return &Exprs[0] + NumOutputs;
1661 outputs_range outputs() {
1662 return outputs_range(begin_outputs(), end_outputs());
1665 const_outputs_iterator begin_outputs() const {
1669 const_outputs_iterator end_outputs() const {
1670 return &Exprs[0] + NumOutputs;
1673 outputs_const_range outputs() const {
1674 return outputs_const_range(begin_outputs(), end_outputs());
1677 child_range children() {
1678 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1682 /// This represents a GCC inline-assembly statement extension.
1683 class GCCAsmStmt : public AsmStmt {
1684 friend class ASTStmtReader;
1686 SourceLocation RParenLoc;
1687 StringLiteral *AsmStr;
1689 // FIXME: If we wanted to, we could allocate all of these in one big array.
1690 StringLiteral **Constraints = nullptr;
1691 StringLiteral **Clobbers = nullptr;
1692 IdentifierInfo **Names = nullptr;
1695 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1696 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1697 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1698 StringLiteral *asmstr, unsigned numclobbers,
1699 StringLiteral **clobbers, SourceLocation rparenloc);
1701 /// Build an empty inline-assembly statement.
1702 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
1704 SourceLocation getRParenLoc() const { return RParenLoc; }
1705 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1707 //===--- Asm String Analysis ---===//
1709 const StringLiteral *getAsmString() const { return AsmStr; }
1710 StringLiteral *getAsmString() { return AsmStr; }
1711 void setAsmString(StringLiteral *E) { AsmStr = E; }
1713 /// AsmStringPiece - this is part of a decomposed asm string specification
1714 /// (for use with the AnalyzeAsmString function below). An asm string is
1715 /// considered to be a concatenation of these parts.
1716 class AsmStringPiece {
1719 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1720 Operand // Operand reference, with optional modifier %c4.
1728 // Source range for operand references.
1729 CharSourceRange Range;
1732 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1733 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
1735 : MyKind(Operand), Str(S), OperandNo(OpNo),
1736 Range(CharSourceRange::getCharRange(Begin, End)) {}
1738 bool isString() const { return MyKind == String; }
1739 bool isOperand() const { return MyKind == Operand; }
1741 const std::string &getString() const { return Str; }
1743 unsigned getOperandNo() const {
1744 assert(isOperand());
1748 CharSourceRange getRange() const {
1749 assert(isOperand() && "Range is currently used only for Operands.");
1753 /// getModifier - Get the modifier for this operand, if present. This
1754 /// returns '\0' if there was no modifier.
1755 char getModifier() const;
1758 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1759 /// it into pieces. If the asm string is erroneous, emit errors and return
1760 /// true, otherwise return false. This handles canonicalization and
1761 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1762 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1763 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1764 const ASTContext &C, unsigned &DiagOffs) const;
1766 /// Assemble final IR asm string.
1767 std::string generateAsmString(const ASTContext &C) const;
1769 //===--- Output operands ---===//
1771 IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; }
1773 StringRef getOutputName(unsigned i) const {
1774 if (IdentifierInfo *II = getOutputIdentifier(i))
1775 return II->getName();
1780 StringRef getOutputConstraint(unsigned i) const;
1782 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1783 return Constraints[i];
1785 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1786 return Constraints[i];
1789 Expr *getOutputExpr(unsigned i);
1791 const Expr *getOutputExpr(unsigned i) const {
1792 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1795 //===--- Input operands ---===//
1797 IdentifierInfo *getInputIdentifier(unsigned i) const {
1798 return Names[i + NumOutputs];
1801 StringRef getInputName(unsigned i) const {
1802 if (IdentifierInfo *II = getInputIdentifier(i))
1803 return II->getName();
1808 StringRef getInputConstraint(unsigned i) const;
1810 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1811 return Constraints[i + NumOutputs];
1813 StringLiteral *getInputConstraintLiteral(unsigned i) {
1814 return Constraints[i + NumOutputs];
1817 Expr *getInputExpr(unsigned i);
1818 void setInputExpr(unsigned i, Expr *E);
1820 const Expr *getInputExpr(unsigned i) const {
1821 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1825 void setOutputsAndInputsAndClobbers(const ASTContext &C,
1826 IdentifierInfo **Names,
1827 StringLiteral **Constraints,
1829 unsigned NumOutputs,
1831 StringLiteral **Clobbers,
1832 unsigned NumClobbers);
1835 //===--- Other ---===//
1837 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1838 /// translate this into a numeric value needed to reference the same operand.
1839 /// This returns -1 if the operand name is invalid.
1840 int getNamedOperand(StringRef SymbolicName) const;
1842 StringRef getClobber(unsigned i) const;
1844 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1845 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1849 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1850 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
1851 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1852 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
1854 static bool classof(const Stmt *T) {
1855 return T->getStmtClass() == GCCAsmStmtClass;
1859 /// This represents a Microsoft inline-assembly statement extension.
1860 class MSAsmStmt : public AsmStmt {
1861 friend class ASTStmtReader;
1863 SourceLocation LBraceLoc, EndLoc;
1866 unsigned NumAsmToks = 0;
1868 Token *AsmToks = nullptr;
1869 StringRef *Constraints = nullptr;
1870 StringRef *Clobbers = nullptr;
1873 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1874 SourceLocation lbraceloc, bool issimple, bool isvolatile,
1875 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1876 ArrayRef<StringRef> constraints,
1877 ArrayRef<Expr*> exprs, StringRef asmstr,
1878 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1880 /// Build an empty MS-style inline-assembly statement.
1881 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
1883 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1884 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1885 SourceLocation getEndLoc() const { return EndLoc; }
1886 void setEndLoc(SourceLocation L) { EndLoc = L; }
1888 bool hasBraces() const { return LBraceLoc.isValid(); }
1890 unsigned getNumAsmToks() { return NumAsmToks; }
1891 Token *getAsmToks() { return AsmToks; }
1893 //===--- Asm String Analysis ---===//
1894 StringRef getAsmString() const { return AsmStr; }
1896 /// Assemble final IR asm string.
1897 std::string generateAsmString(const ASTContext &C) const;
1899 //===--- Output operands ---===//
1901 StringRef getOutputConstraint(unsigned i) const {
1902 assert(i < NumOutputs);
1903 return Constraints[i];
1906 Expr *getOutputExpr(unsigned i);
1908 const Expr *getOutputExpr(unsigned i) const {
1909 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1912 //===--- Input operands ---===//
1914 StringRef getInputConstraint(unsigned i) const {
1915 assert(i < NumInputs);
1916 return Constraints[i + NumOutputs];
1919 Expr *getInputExpr(unsigned i);
1920 void setInputExpr(unsigned i, Expr *E);
1922 const Expr *getInputExpr(unsigned i) const {
1923 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1926 //===--- Other ---===//
1928 ArrayRef<StringRef> getAllConstraints() const {
1929 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
1932 ArrayRef<StringRef> getClobbers() const {
1933 return llvm::makeArrayRef(Clobbers, NumClobbers);
1936 ArrayRef<Expr*> getAllExprs() const {
1937 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
1938 NumInputs + NumOutputs);
1941 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1944 void initialize(const ASTContext &C, StringRef AsmString,
1945 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1946 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1949 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1950 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
1951 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1953 static bool classof(const Stmt *T) {
1954 return T->getStmtClass() == MSAsmStmtClass;
1957 child_range children() {
1958 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1962 class SEHExceptStmt : public Stmt {
1963 friend class ASTReader;
1964 friend class ASTStmtReader;
1969 enum { FILTER_EXPR, BLOCK };
1971 SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
1972 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
1975 static SEHExceptStmt* Create(const ASTContext &C,
1976 SourceLocation ExceptLoc,
1980 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1981 SourceLocation getBeginLoc() const LLVM_READONLY { return getExceptLoc(); }
1982 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1984 SourceLocation getExceptLoc() const { return Loc; }
1985 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1987 Expr *getFilterExpr() const {
1988 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1991 CompoundStmt *getBlock() const {
1992 return cast<CompoundStmt>(Children[BLOCK]);
1995 child_range children() {
1996 return child_range(Children, Children+2);
1999 static bool classof(const Stmt *T) {
2000 return T->getStmtClass() == SEHExceptStmtClass;
2004 class SEHFinallyStmt : public Stmt {
2005 friend class ASTReader;
2006 friend class ASTStmtReader;
2011 SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
2012 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
2015 static SEHFinallyStmt* Create(const ASTContext &C,
2016 SourceLocation FinallyLoc,
2019 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
2020 SourceLocation getBeginLoc() const LLVM_READONLY { return getFinallyLoc(); }
2021 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
2023 SourceLocation getFinallyLoc() const { return Loc; }
2024 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
2026 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
2028 child_range children() {
2029 return child_range(&Block,&Block+1);
2032 static bool classof(const Stmt *T) {
2033 return T->getStmtClass() == SEHFinallyStmtClass;
2037 class SEHTryStmt : public Stmt {
2038 friend class ASTReader;
2039 friend class ASTStmtReader;
2042 SourceLocation TryLoc;
2045 enum { TRY = 0, HANDLER = 1 };
2047 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
2048 SourceLocation TryLoc,
2052 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
2055 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
2056 SourceLocation TryLoc, Stmt *TryBlock,
2059 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
2060 SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
2061 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
2063 SourceLocation getTryLoc() const { return TryLoc; }
2064 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
2066 bool getIsCXXTry() const { return IsCXXTry; }
2068 CompoundStmt* getTryBlock() const {
2069 return cast<CompoundStmt>(Children[TRY]);
2072 Stmt *getHandler() const { return Children[HANDLER]; }
2074 /// Returns 0 if not defined
2075 SEHExceptStmt *getExceptHandler() const;
2076 SEHFinallyStmt *getFinallyHandler() const;
2078 child_range children() {
2079 return child_range(Children, Children+2);
2082 static bool classof(const Stmt *T) {
2083 return T->getStmtClass() == SEHTryStmtClass;
2087 /// Represents a __leave statement.
2088 class SEHLeaveStmt : public Stmt {
2089 SourceLocation LeaveLoc;
2092 explicit SEHLeaveStmt(SourceLocation LL)
2093 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
2095 /// Build an empty __leave statement.
2096 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
2098 SourceLocation getLeaveLoc() const { return LeaveLoc; }
2099 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
2101 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
2102 SourceLocation getBeginLoc() const LLVM_READONLY { return LeaveLoc; }
2103 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
2104 SourceLocation getEndLoc() const LLVM_READONLY { return LeaveLoc; }
2106 static bool classof(const Stmt *T) {
2107 return T->getStmtClass() == SEHLeaveStmtClass;
2111 child_range children() {
2112 return child_range(child_iterator(), child_iterator());
2116 /// This captures a statement into a function. For example, the following
2117 /// pragma annotated compound statement can be represented as a CapturedStmt,
2118 /// and this compound statement is the body of an anonymous outlined function.
2120 /// #pragma omp parallel
2125 class CapturedStmt : public Stmt {
2127 /// The different capture forms: by 'this', by reference, capture for
2128 /// variable-length array type etc.
2129 enum VariableCaptureKind {
2136 /// Describes the capture of either a variable, or 'this', or
2137 /// variable-length array type.
2139 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2143 friend class ASTStmtReader;
2145 /// Create a new capture.
2147 /// \param Loc The source location associated with this capture.
2149 /// \param Kind The kind of capture (this, ByRef, ...).
2151 /// \param Var The variable being captured, or null if capturing this.
2152 Capture(SourceLocation Loc, VariableCaptureKind Kind,
2153 VarDecl *Var = nullptr);
2155 /// Determine the kind of capture.
2156 VariableCaptureKind getCaptureKind() const;
2158 /// Retrieve the source location at which the variable or 'this' was
2160 SourceLocation getLocation() const { return Loc; }
2162 /// Determine whether this capture handles the C++ 'this' pointer.
2163 bool capturesThis() const { return getCaptureKind() == VCK_This; }
2165 /// Determine whether this capture handles a variable (by reference).
2166 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2168 /// Determine whether this capture handles a variable by copy.
2169 bool capturesVariableByCopy() const {
2170 return getCaptureKind() == VCK_ByCopy;
2173 /// Determine whether this capture handles a variable-length array
2175 bool capturesVariableArrayType() const {
2176 return getCaptureKind() == VCK_VLAType;
2179 /// Retrieve the declaration of the variable being captured.
2181 /// This operation is only valid if this capture captures a variable.
2182 VarDecl *getCapturedVar() const;
2186 /// The number of variable captured, including 'this'.
2187 unsigned NumCaptures;
2189 /// The pointer part is the implicit the outlined function and the
2190 /// int part is the captured region kind, 'CR_Default' etc.
2191 llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind;
2193 /// The record for captured variables, a RecordDecl or CXXRecordDecl.
2194 RecordDecl *TheRecordDecl = nullptr;
2196 /// Construct a captured statement.
2197 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2198 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2200 /// Construct an empty captured statement.
2201 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2203 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
2205 Stmt *const *getStoredStmts() const {
2206 return reinterpret_cast<Stmt *const *>(this + 1);
2209 Capture *getStoredCaptures() const;
2211 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2214 friend class ASTStmtReader;
2216 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2217 CapturedRegionKind Kind,
2218 ArrayRef<Capture> Captures,
2219 ArrayRef<Expr *> CaptureInits,
2220 CapturedDecl *CD, RecordDecl *RD);
2222 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2223 unsigned NumCaptures);
2225 /// Retrieve the statement being captured.
2226 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2227 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
2229 /// Retrieve the outlined function declaration.
2230 CapturedDecl *getCapturedDecl();
2231 const CapturedDecl *getCapturedDecl() const;
2233 /// Set the outlined function declaration.
2234 void setCapturedDecl(CapturedDecl *D);
2236 /// Retrieve the captured region kind.
2237 CapturedRegionKind getCapturedRegionKind() const;
2239 /// Set the captured region kind.
2240 void setCapturedRegionKind(CapturedRegionKind Kind);
2242 /// Retrieve the record declaration for captured variables.
2243 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2245 /// Set the record declaration for captured variables.
2246 void setCapturedRecordDecl(RecordDecl *D) {
2247 assert(D && "null RecordDecl");
2251 /// True if this variable has been captured.
2252 bool capturesVariable(const VarDecl *Var) const;
2254 /// An iterator that walks over the captures.
2255 using capture_iterator = Capture *;
2256 using const_capture_iterator = const Capture *;
2257 using capture_range = llvm::iterator_range<capture_iterator>;
2258 using capture_const_range = llvm::iterator_range<const_capture_iterator>;
2260 capture_range captures() {
2261 return capture_range(capture_begin(), capture_end());
2263 capture_const_range captures() const {
2264 return capture_const_range(capture_begin(), capture_end());
2267 /// Retrieve an iterator pointing to the first capture.
2268 capture_iterator capture_begin() { return getStoredCaptures(); }
2269 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2271 /// Retrieve an iterator pointing past the end of the sequence of
2273 capture_iterator capture_end() const {
2274 return getStoredCaptures() + NumCaptures;
2277 /// Retrieve the number of captures, including 'this'.
2278 unsigned capture_size() const { return NumCaptures; }
2280 /// Iterator that walks over the capture initialization arguments.
2281 using capture_init_iterator = Expr **;
2282 using capture_init_range = llvm::iterator_range<capture_init_iterator>;
2284 /// Const iterator that walks over the capture initialization
2286 using const_capture_init_iterator = Expr *const *;
2287 using const_capture_init_range =
2288 llvm::iterator_range<const_capture_init_iterator>;
2290 capture_init_range capture_inits() {
2291 return capture_init_range(capture_init_begin(), capture_init_end());
2294 const_capture_init_range capture_inits() const {
2295 return const_capture_init_range(capture_init_begin(), capture_init_end());
2298 /// Retrieve the first initialization argument.
2299 capture_init_iterator capture_init_begin() {
2300 return reinterpret_cast<Expr **>(getStoredStmts());
2303 const_capture_init_iterator capture_init_begin() const {
2304 return reinterpret_cast<Expr *const *>(getStoredStmts());
2307 /// Retrieve the iterator pointing one past the last initialization
2309 capture_init_iterator capture_init_end() {
2310 return capture_init_begin() + NumCaptures;
2313 const_capture_init_iterator capture_init_end() const {
2314 return capture_init_begin() + NumCaptures;
2317 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
2318 SourceLocation getBeginLoc() const LLVM_READONLY {
2319 return getCapturedStmt()->getLocStart();
2322 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
2323 SourceLocation getEndLoc() const LLVM_READONLY {
2324 return getCapturedStmt()->getLocEnd();
2327 SourceRange getSourceRange() const LLVM_READONLY {
2328 return getCapturedStmt()->getSourceRange();
2331 static bool classof(const Stmt *T) {
2332 return T->getStmtClass() == CapturedStmtClass;
2335 child_range children();
2338 } // namespace clang
2340 #endif // LLVM_CLANG_AST_STMT_H