]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/Stmt.h
Merge clang trunk r338150, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / Stmt.h
1 //===- Stmt.h - Classes for representing statements -------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the Stmt interface and subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_STMT_H
15 #define LLVM_CLANG_AST_STMT_H
16
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"
31 #include <algorithm>
32 #include <cassert>
33 #include <cstddef>
34 #include <iterator>
35 #include <string>
36
37 namespace llvm {
38
39 class FoldingSetNodeID;
40
41 } // namespace llvm
42
43 namespace clang {
44
45 class ASTContext;
46 class Attr;
47 class CapturedDecl;
48 class Decl;
49 class Expr;
50 class LabelDecl;
51 class ODRHash;
52 class PrinterHelper;
53 struct PrintingPolicy;
54 class RecordDecl;
55 class SourceManager;
56 class StringLiteral;
57 class Token;
58 class VarDecl;
59
60 //===----------------------------------------------------------------------===//
61 // AST classes for statements.
62 //===----------------------------------------------------------------------===//
63
64 /// Stmt - This represents one statement.
65 ///
66 class alignas(void *) Stmt {
67 public:
68   enum StmtClass {
69     NoStmtClass = 0,
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"
77   };
78
79   // Make vanilla 'new' and 'delete' illegal for Stmts.
80 protected:
81   friend class ASTStmtReader;
82   friend class ASTStmtWriter;
83
84   void *operator new(size_t bytes) noexcept {
85     llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
86   }
87
88   void operator delete(void *data) noexcept {
89     llvm_unreachable("Stmts cannot be released with regular 'delete'.");
90   }
91
92   class StmtBitfields {
93     friend class Stmt;
94
95     /// The statement class.
96     unsigned sClass : 8;
97   };
98   enum { NumStmtBits = 8 };
99
100   class CompoundStmtBitfields {
101     friend class CompoundStmt;
102
103     unsigned : NumStmtBits;
104
105     unsigned NumStmts : 32 - NumStmtBits;
106   };
107
108   class IfStmtBitfields {
109     friend class IfStmt;
110
111     unsigned : NumStmtBits;
112
113     unsigned IsConstexpr : 1;
114   };
115
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
128     friend class Expr;
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
139
140     unsigned : NumStmtBits;
141
142     unsigned ValueKind : 2;
143     unsigned ObjectKind : 3;
144     unsigned TypeDependent : 1;
145     unsigned ValueDependent : 1;
146     unsigned InstantiationDependent : 1;
147     unsigned ContainsUnexpandedParameterPack : 1;
148   };
149   enum { NumExprBits = 17 };
150
151   class CharacterLiteralBitfields {
152     friend class CharacterLiteral;
153
154     unsigned : NumExprBits;
155
156     unsigned Kind : 3;
157   };
158
159   enum APFloatSemantics {
160     IEEEhalf,
161     IEEEsingle,
162     IEEEdouble,
163     x87DoubleExtended,
164     IEEEquad,
165     PPCDoubleDouble
166   };
167
168   class FloatingLiteralBitfields {
169     friend class FloatingLiteral;
170
171     unsigned : NumExprBits;
172
173     unsigned Semantics : 3; // Provides semantics for APFloat construction
174     unsigned IsExact : 1;
175   };
176
177   class UnaryExprOrTypeTraitExprBitfields {
178     friend class UnaryExprOrTypeTraitExpr;
179
180     unsigned : NumExprBits;
181
182     unsigned Kind : 2;
183     unsigned IsType : 1; // true if operand is a type, false if an expression.
184   };
185
186   class DeclRefExprBitfields {
187     friend class ASTStmtReader; // deserialization
188     friend class DeclRefExpr;
189
190     unsigned : NumExprBits;
191
192     unsigned HasQualifier : 1;
193     unsigned HasTemplateKWAndArgsInfo : 1;
194     unsigned HasFoundDecl : 1;
195     unsigned HadMultipleCandidates : 1;
196     unsigned RefersToEnclosingVariableOrCapture : 1;
197   };
198
199   class CastExprBitfields {
200     friend class CastExpr;
201     friend class ImplicitCastExpr;
202
203     unsigned : NumExprBits;
204
205     unsigned Kind : 6;
206     unsigned PartOfExplicitCast : 1; // Only set for ImplicitCastExpr.
207     unsigned BasePathSize : 32 - 6 - 1 - NumExprBits;
208   };
209
210   class CallExprBitfields {
211     friend class CallExpr;
212
213     unsigned : NumExprBits;
214
215     unsigned NumPreArgs : 1;
216   };
217
218   class ExprWithCleanupsBitfields {
219     friend class ASTStmtReader; // deserialization
220     friend class ExprWithCleanups;
221
222     unsigned : NumExprBits;
223
224     // When false, it must not have side effects.
225     unsigned CleanupsHaveSideEffects : 1;
226
227     unsigned NumObjects : 32 - 1 - NumExprBits;
228   };
229
230   class PseudoObjectExprBitfields {
231     friend class ASTStmtReader; // deserialization
232     friend class PseudoObjectExpr;
233
234     unsigned : NumExprBits;
235
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;
240   };
241
242   class OpaqueValueExprBitfields {
243     friend class OpaqueValueExpr;
244
245     unsigned : NumExprBits;
246
247     /// The OVE is a unique semantic reference to its source expressio if this
248     /// bit is set to true.
249     unsigned IsUnique : 1;
250   };
251
252   class ObjCIndirectCopyRestoreExprBitfields {
253     friend class ObjCIndirectCopyRestoreExpr;
254
255     unsigned : NumExprBits;
256
257     unsigned ShouldCopy : 1;
258   };
259
260   class InitListExprBitfields {
261     friend class InitListExpr;
262
263     unsigned : NumExprBits;
264
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;
268   };
269
270   class TypeTraitExprBitfields {
271     friend class ASTStmtReader;
272     friend class ASTStmtWriter;
273     friend class TypeTraitExpr;
274     
275     unsigned : NumExprBits;
276     
277     /// The kind of type trait, which is a value of a TypeTrait enumerator.
278     unsigned Kind : 8;
279     
280     /// If this expression is not value-dependent, this indicates whether
281     /// the trait evaluated true or false.
282     unsigned Value : 1;
283
284     /// The number of arguments to this type trait.
285     unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
286   };
287
288   class CoawaitExprBitfields {
289     friend class CoawaitExpr;
290
291     unsigned : NumExprBits;
292
293     unsigned IsImplicit : 1;
294   };
295
296   union {
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;
314   };
315
316 public:
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);
321
322   void* operator new(size_t bytes, const ASTContext* C,
323                      unsigned alignment = 8) {
324     return operator new(bytes, *C, alignment);
325   }
326
327   void *operator new(size_t bytes, void *mem) noexcept { return mem; }
328
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 {}
333
334 public:
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 {};
339
340 protected:
341   /// Iterator for iterating over Stmt * arrays that contain only Expr *
342   ///
343   /// This is needed because AST nodes use Stmt* arrays to store
344   /// references to children (to be compatible with StmtIterator).
345   struct ExprIterator
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) {}
350
351     reference operator*() const {
352       assert((*I)->getStmtClass() >= firstExprConstant &&
353              (*I)->getStmtClass() <= lastExprConstant);
354       return *reinterpret_cast<Expr **>(I);
355     }
356   };
357
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,
362                                     const Expr *const> {
363     ConstExprIterator() : iterator_adaptor_base(nullptr) {}
364     ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {}
365
366     reference operator*() const {
367       assert((*I)->getStmtClass() >= firstExprConstant &&
368              (*I)->getStmtClass() <= lastExprConstant);
369       return *reinterpret_cast<const Expr *const *>(I);
370     }
371   };
372
373 private:
374   /// Whether statistic collection is enabled.
375   static bool StatisticsEnabled;
376
377 protected:
378   /// Construct an empty statement.
379   explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
380
381 public:
382   Stmt(StmtClass 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);
389   }
390
391   StmtClass getStmtClass() const {
392     return static_cast<StmtClass>(StmtBits.sClass);
393   }
394
395   const char *getStmtClassName() const;
396
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;
402   SourceLocation getLocEnd() const LLVM_READONLY;
403
404   // global temp stats (until we have a per-module visitor)
405   static void addStmtClass(const StmtClass s);
406   static void EnableStatistics();
407   static void PrintStats();
408
409   /// Dumps the specified AST fragment and all subtrees to
410   /// \c llvm::errs().
411   void dump() const;
412   void dump(SourceManager &SM) const;
413   void dump(raw_ostream &OS, SourceManager &SM) const;
414   void dump(raw_ostream &OS) const;
415
416   /// dumpColor - same as dump(), but forces color highlighting.
417   void dumpColor() const;
418
419   /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
420   /// back to its original source language syntax.
421   void dumpPretty(const ASTContext &Context) const;
422   void printPretty(raw_ostream &OS, PrinterHelper *Helper,
423                    const PrintingPolicy &Policy, unsigned Indentation = 0,
424                    const ASTContext *Context = nullptr) const;
425
426   /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
427   ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
428   void viewAST() const;
429
430   /// Skip past any implicit AST nodes which might surround this
431   /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
432   Stmt *IgnoreImplicit();
433   const Stmt *IgnoreImplicit() const {
434     return const_cast<Stmt *>(this)->IgnoreImplicit();
435   }
436
437   /// Skip no-op (attributed, compound) container stmts and skip captured
438   /// stmt at the top, if \a IgnoreCaptured is true.
439   Stmt *IgnoreContainers(bool IgnoreCaptured = false);
440   const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
441     return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
442   }
443
444   const Stmt *stripLabelLikeStatements() const;
445   Stmt *stripLabelLikeStatements() {
446     return const_cast<Stmt*>(
447       const_cast<const Stmt*>(this)->stripLabelLikeStatements());
448   }
449
450   /// Child Iterators: All subclasses must implement 'children'
451   /// to permit easy iteration over the substatements/subexpessions of an
452   /// AST node.  This permits easy iteration over all nodes in the AST.
453   using child_iterator = StmtIterator;
454   using const_child_iterator = ConstStmtIterator;
455
456   using child_range = llvm::iterator_range<child_iterator>;
457   using const_child_range = llvm::iterator_range<const_child_iterator>;
458
459   child_range children();
460
461   const_child_range children() const {
462     auto Children = const_cast<Stmt *>(this)->children();
463     return const_child_range(Children.begin(), Children.end());
464   }
465
466   child_iterator child_begin() { return children().begin(); }
467   child_iterator child_end() { return children().end(); }
468
469   const_child_iterator child_begin() const { return children().begin(); }
470   const_child_iterator child_end() const { return children().end(); }
471
472   /// Produce a unique representation of the given statement.
473   ///
474   /// \param ID once the profiling operation is complete, will contain
475   /// the unique representation of the given statement.
476   ///
477   /// \param Context the AST context in which the statement resides
478   ///
479   /// \param Canonical whether the profile should be based on the canonical
480   /// representation of this statement (e.g., where non-type template
481   /// parameters are identified by index/level rather than their
482   /// declaration pointers) or the exact representation of the statement as
483   /// written in the source.
484   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
485                bool Canonical) const;
486
487   /// Calculate a unique representation for a statement that is
488   /// stable across compiler invocations.
489   ///
490   /// \param ID profile information will be stored in ID.
491   ///
492   /// \param Hash an ODRHash object which will be called where pointers would
493   /// have been used in the Profile function.
494   void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
495 };
496
497 /// DeclStmt - Adaptor class for mixing declarations with statements and
498 /// expressions. For example, CompoundStmt mixes statements, expressions
499 /// and declarations (variables, types). Another example is ForStmt, where
500 /// the first statement can be an expression or a declaration.
501 class DeclStmt : public Stmt {
502   DeclGroupRef DG;
503   SourceLocation StartLoc, EndLoc;
504
505 public:
506   DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
507       : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
508
509   /// Build an empty declaration statement.
510   explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
511
512   /// isSingleDecl - This method returns true if this DeclStmt refers
513   /// to a single Decl.
514   bool isSingleDecl() const {
515     return DG.isSingleDecl();
516   }
517
518   const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
519   Decl *getSingleDecl() { return DG.getSingleDecl(); }
520
521   const DeclGroupRef getDeclGroup() const { return DG; }
522   DeclGroupRef getDeclGroup() { return DG; }
523   void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
524
525   SourceLocation getStartLoc() const { return StartLoc; }
526   void setStartLoc(SourceLocation L) { StartLoc = L; }
527   SourceLocation getEndLoc() const { return EndLoc; }
528   void setEndLoc(SourceLocation L) { EndLoc = L; }
529
530   SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
531   SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
532
533   static bool classof(const Stmt *T) {
534     return T->getStmtClass() == DeclStmtClass;
535   }
536
537   // Iterators over subexpressions.
538   child_range children() {
539     return child_range(child_iterator(DG.begin(), DG.end()),
540                        child_iterator(DG.end(), DG.end()));
541   }
542
543   using decl_iterator = DeclGroupRef::iterator;
544   using const_decl_iterator = DeclGroupRef::const_iterator;
545   using decl_range = llvm::iterator_range<decl_iterator>;
546   using decl_const_range = llvm::iterator_range<const_decl_iterator>;
547
548   decl_range decls() { return decl_range(decl_begin(), decl_end()); }
549
550   decl_const_range decls() const {
551     return decl_const_range(decl_begin(), decl_end());
552   }
553
554   decl_iterator decl_begin() { return DG.begin(); }
555   decl_iterator decl_end() { return DG.end(); }
556   const_decl_iterator decl_begin() const { return DG.begin(); }
557   const_decl_iterator decl_end() const { return DG.end(); }
558
559   using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
560
561   reverse_decl_iterator decl_rbegin() {
562     return reverse_decl_iterator(decl_end());
563   }
564
565   reverse_decl_iterator decl_rend() {
566     return reverse_decl_iterator(decl_begin());
567   }
568 };
569
570 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
571 ///
572 class NullStmt : public Stmt {
573   SourceLocation SemiLoc;
574
575   /// True if the null statement was preceded by an empty macro, e.g:
576   /// @code
577   ///   #define CALL(x)
578   ///   CALL(0);
579   /// @endcode
580   bool HasLeadingEmptyMacro = false;
581
582 public:
583   friend class ASTStmtReader;
584   friend class ASTStmtWriter;
585
586   NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
587       : Stmt(NullStmtClass), SemiLoc(L),
588         HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
589
590   /// Build an empty null statement.
591   explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
592
593   SourceLocation getSemiLoc() const { return SemiLoc; }
594   void setSemiLoc(SourceLocation L) { SemiLoc = L; }
595
596   bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
597
598   SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
599   SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
600
601   static bool classof(const Stmt *T) {
602     return T->getStmtClass() == NullStmtClass;
603   }
604
605   child_range children() {
606     return child_range(child_iterator(), child_iterator());
607   }
608 };
609
610 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
611 class CompoundStmt final : public Stmt,
612                            private llvm::TrailingObjects<CompoundStmt, Stmt *> {
613   friend class ASTStmtReader;
614   friend TrailingObjects;
615
616   SourceLocation LBraceLoc, RBraceLoc;
617
618   CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
619   explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
620
621   void setStmts(ArrayRef<Stmt *> Stmts);
622
623 public:
624   static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
625                               SourceLocation LB, SourceLocation RB);
626
627   // Build an empty compound statement with a location.
628   explicit CompoundStmt(SourceLocation Loc)
629       : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(Loc) {
630     CompoundStmtBits.NumStmts = 0;
631   }
632
633   // Build an empty compound statement.
634   static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
635
636   bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
637   unsigned size() const { return CompoundStmtBits.NumStmts; }
638
639   using body_iterator = Stmt **;
640   using body_range = llvm::iterator_range<body_iterator>;
641
642   body_range body() { return body_range(body_begin(), body_end()); }
643   body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
644   body_iterator body_end() { return body_begin() + size(); }
645   Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
646
647   Stmt *body_back() {
648     return !body_empty() ? body_begin()[size() - 1] : nullptr;
649   }
650
651   void setLastStmt(Stmt *S) {
652     assert(!body_empty() && "setLastStmt");
653     body_begin()[size() - 1] = S;
654   }
655
656   using const_body_iterator = Stmt* const *;
657   using body_const_range = llvm::iterator_range<const_body_iterator>;
658
659   body_const_range body() const {
660     return body_const_range(body_begin(), body_end());
661   }
662
663   const_body_iterator body_begin() const {
664     return getTrailingObjects<Stmt *>();
665   }
666
667   const_body_iterator body_end() const { return body_begin() + size(); }
668
669   const Stmt *body_front() const {
670     return !body_empty() ? body_begin()[0] : nullptr;
671   }
672
673   const Stmt *body_back() const {
674     return !body_empty() ? body_begin()[size() - 1] : nullptr;
675   }
676
677   using reverse_body_iterator = std::reverse_iterator<body_iterator>;
678
679   reverse_body_iterator body_rbegin() {
680     return reverse_body_iterator(body_end());
681   }
682
683   reverse_body_iterator body_rend() {
684     return reverse_body_iterator(body_begin());
685   }
686
687   using const_reverse_body_iterator =
688       std::reverse_iterator<const_body_iterator>;
689
690   const_reverse_body_iterator body_rbegin() const {
691     return const_reverse_body_iterator(body_end());
692   }
693
694   const_reverse_body_iterator body_rend() const {
695     return const_reverse_body_iterator(body_begin());
696   }
697
698   SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; }
699   SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; }
700
701   SourceLocation getLBracLoc() const { return LBraceLoc; }
702   SourceLocation getRBracLoc() const { return RBraceLoc; }
703
704   static bool classof(const Stmt *T) {
705     return T->getStmtClass() == CompoundStmtClass;
706   }
707
708   // Iterators
709   child_range children() { return child_range(body_begin(), body_end()); }
710
711   const_child_range children() const {
712     return const_child_range(body_begin(), body_end());
713   }
714 };
715
716 // SwitchCase is the base class for CaseStmt and DefaultStmt,
717 class SwitchCase : public Stmt {
718 protected:
719   // A pointer to the following CaseStmt or DefaultStmt class,
720   // used by SwitchStmt.
721   SwitchCase *NextSwitchCase = nullptr;
722   SourceLocation KeywordLoc;
723   SourceLocation ColonLoc;
724
725   SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
726       : Stmt(SC), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {}
727
728   SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
729
730 public:
731   const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
732
733   SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
734
735   void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
736
737   SourceLocation getKeywordLoc() const { return KeywordLoc; }
738   void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
739   SourceLocation getColonLoc() const { return ColonLoc; }
740   void setColonLoc(SourceLocation L) { ColonLoc = L; }
741
742   Stmt *getSubStmt();
743   const Stmt *getSubStmt() const {
744     return const_cast<SwitchCase*>(this)->getSubStmt();
745   }
746
747   SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
748   SourceLocation getLocEnd() const LLVM_READONLY;
749
750   static bool classof(const Stmt *T) {
751     return T->getStmtClass() == CaseStmtClass ||
752            T->getStmtClass() == DefaultStmtClass;
753   }
754 };
755
756 class CaseStmt : public SwitchCase {
757   SourceLocation EllipsisLoc;
758   enum { LHS, RHS, SUBSTMT, END_EXPR };
759   Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
760                              // GNU "case 1 ... 4" extension
761
762 public:
763   CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
764            SourceLocation ellipsisLoc, SourceLocation colonLoc)
765     : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
766     SubExprs[SUBSTMT] = nullptr;
767     SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
768     SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
769     EllipsisLoc = ellipsisLoc;
770   }
771
772   /// Build an empty switch case statement.
773   explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) {}
774
775   SourceLocation getCaseLoc() const { return KeywordLoc; }
776   void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
777   SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
778   void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
779   SourceLocation getColonLoc() const { return ColonLoc; }
780   void setColonLoc(SourceLocation L) { ColonLoc = L; }
781
782   Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
783   Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
784   Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
785
786   const Expr *getLHS() const {
787     return reinterpret_cast<const Expr*>(SubExprs[LHS]);
788   }
789
790   const Expr *getRHS() const {
791     return reinterpret_cast<const Expr*>(SubExprs[RHS]);
792   }
793
794   const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
795
796   void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
797   void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
798   void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
799
800   SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
801
802   SourceLocation getLocEnd() const LLVM_READONLY {
803     // Handle deeply nested case statements with iteration instead of recursion.
804     const CaseStmt *CS = this;
805     while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
806       CS = CS2;
807
808     return CS->getSubStmt()->getLocEnd();
809   }
810
811   static bool classof(const Stmt *T) {
812     return T->getStmtClass() == CaseStmtClass;
813   }
814
815   // Iterators
816   child_range children() {
817     return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
818   }
819 };
820
821 class DefaultStmt : public SwitchCase {
822   Stmt* SubStmt;
823
824 public:
825   DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
826     SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
827
828   /// Build an empty default statement.
829   explicit DefaultStmt(EmptyShell Empty)
830       : SwitchCase(DefaultStmtClass, Empty) {}
831
832   Stmt *getSubStmt() { return SubStmt; }
833   const Stmt *getSubStmt() const { return SubStmt; }
834   void setSubStmt(Stmt *S) { SubStmt = S; }
835
836   SourceLocation getDefaultLoc() const { return KeywordLoc; }
837   void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
838   SourceLocation getColonLoc() const { return ColonLoc; }
839   void setColonLoc(SourceLocation L) { ColonLoc = L; }
840
841   SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
842   SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
843
844   static bool classof(const Stmt *T) {
845     return T->getStmtClass() == DefaultStmtClass;
846   }
847
848   // Iterators
849   child_range children() { return child_range(&SubStmt, &SubStmt+1); }
850 };
851
852 inline SourceLocation SwitchCase::getLocEnd() const {
853   if (const auto *CS = dyn_cast<CaseStmt>(this))
854     return CS->getLocEnd();
855   return cast<DefaultStmt>(this)->getLocEnd();
856 }
857
858 /// LabelStmt - Represents a label, which has a substatement.  For example:
859 ///    foo: return;
860 class LabelStmt : public Stmt {
861   SourceLocation IdentLoc;
862   LabelDecl *TheDecl;
863   Stmt *SubStmt;
864
865 public:
866   LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
867       : Stmt(LabelStmtClass), IdentLoc(IL), TheDecl(D), SubStmt(substmt) {
868     static_assert(sizeof(LabelStmt) ==
869                       2 * sizeof(SourceLocation) + 2 * sizeof(void *),
870                   "LabelStmt too big");
871   }
872
873   // Build an empty label statement.
874   explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) {}
875
876   SourceLocation getIdentLoc() const { return IdentLoc; }
877   LabelDecl *getDecl() const { return TheDecl; }
878   void setDecl(LabelDecl *D) { TheDecl = D; }
879   const char *getName() const;
880   Stmt *getSubStmt() { return SubStmt; }
881   const Stmt *getSubStmt() const { return SubStmt; }
882   void setIdentLoc(SourceLocation L) { IdentLoc = L; }
883   void setSubStmt(Stmt *SS) { SubStmt = SS; }
884
885   SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
886   SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
887
888   child_range children() { return child_range(&SubStmt, &SubStmt+1); }
889
890   static bool classof(const Stmt *T) {
891     return T->getStmtClass() == LabelStmtClass;
892   }
893 };
894
895 /// Represents an attribute applied to a statement.
896 ///
897 /// Represents an attribute applied to a statement. For example:
898 ///   [[omp::for(...)]] for (...) { ... }
899 class AttributedStmt final
900     : public Stmt,
901       private llvm::TrailingObjects<AttributedStmt, const Attr *> {
902   friend class ASTStmtReader;
903   friend TrailingObjects;
904
905   Stmt *SubStmt;
906   SourceLocation AttrLoc;
907   unsigned NumAttrs;
908
909   AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
910     : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
911       NumAttrs(Attrs.size()) {
912     std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
913   }
914
915   explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
916       : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
917     std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
918   }
919
920   const Attr *const *getAttrArrayPtr() const {
921     return getTrailingObjects<const Attr *>();
922   }
923   const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
924
925 public:
926   static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
927                                 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
928
929   // Build an empty attributed statement.
930   static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
931
932   SourceLocation getAttrLoc() const { return AttrLoc; }
933   ArrayRef<const Attr*> getAttrs() const {
934     return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs);
935   }
936
937   Stmt *getSubStmt() { return SubStmt; }
938   const Stmt *getSubStmt() const { return SubStmt; }
939
940   SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
941   SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
942
943   child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
944
945   static bool classof(const Stmt *T) {
946     return T->getStmtClass() == AttributedStmtClass;
947   }
948 };
949
950 /// IfStmt - This represents an if/then/else.
951 class IfStmt : public Stmt {
952   enum { INIT, VAR, COND, THEN, ELSE, END_EXPR };
953   Stmt* SubExprs[END_EXPR];
954
955   SourceLocation IfLoc;
956   SourceLocation ElseLoc;
957
958 public:
959   IfStmt(const ASTContext &C, SourceLocation IL,
960          bool IsConstexpr, Stmt *init, VarDecl *var, Expr *cond,
961          Stmt *then, SourceLocation EL = SourceLocation(),
962          Stmt *elsev = nullptr);
963
964   /// Build an empty if/then/else statement
965   explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) {}
966
967   /// Retrieve the variable declared in this "if" statement, if any.
968   ///
969   /// In the following example, "x" is the condition variable.
970   /// \code
971   /// if (int x = foo()) {
972   ///   printf("x is %d", x);
973   /// }
974   /// \endcode
975   VarDecl *getConditionVariable() const;
976   void setConditionVariable(const ASTContext &C, VarDecl *V);
977
978   /// If this IfStmt has a condition variable, return the faux DeclStmt
979   /// associated with the creation of that condition variable.
980   const DeclStmt *getConditionVariableDeclStmt() const {
981     return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
982   }
983
984   Stmt *getInit() { return SubExprs[INIT]; }
985   const Stmt *getInit() const { return SubExprs[INIT]; }
986   void setInit(Stmt *S) { SubExprs[INIT] = S; }
987   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
988   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
989   const Stmt *getThen() const { return SubExprs[THEN]; }
990   void setThen(Stmt *S) { SubExprs[THEN] = S; }
991   const Stmt *getElse() const { return SubExprs[ELSE]; }
992   void setElse(Stmt *S) { SubExprs[ELSE] = S; }
993
994   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
995   Stmt *getThen() { return SubExprs[THEN]; }
996   Stmt *getElse() { return SubExprs[ELSE]; }
997
998   SourceLocation getIfLoc() const { return IfLoc; }
999   void setIfLoc(SourceLocation L) { IfLoc = L; }
1000   SourceLocation getElseLoc() const { return ElseLoc; }
1001   void setElseLoc(SourceLocation L) { ElseLoc = L; }
1002
1003   bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
1004   void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
1005
1006   bool isObjCAvailabilityCheck() const;
1007
1008   SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
1009
1010   SourceLocation getLocEnd() const LLVM_READONLY {
1011     if (SubExprs[ELSE])
1012       return SubExprs[ELSE]->getLocEnd();
1013     else
1014       return SubExprs[THEN]->getLocEnd();
1015   }
1016
1017   // Iterators over subexpressions.  The iterators will include iterating
1018   // over the initialization expression referenced by the condition variable.
1019   child_range children() {
1020     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1021   }
1022
1023   static bool classof(const Stmt *T) {
1024     return T->getStmtClass() == IfStmtClass;
1025   }
1026 };
1027
1028 /// SwitchStmt - This represents a 'switch' stmt.
1029 class SwitchStmt : public Stmt {
1030   SourceLocation SwitchLoc;
1031   enum { INIT, VAR, COND, BODY, END_EXPR };
1032   Stmt* SubExprs[END_EXPR];
1033
1034   // This points to a linked list of case and default statements and, if the
1035   // SwitchStmt is a switch on an enum value, records whether all the enum
1036   // values were covered by CaseStmts.  The coverage information value is meant
1037   // to be a hint for possible clients.
1038   llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase;
1039
1040 public:
1041   SwitchStmt(const ASTContext &C, Stmt *Init, VarDecl *Var, Expr *cond);
1042
1043   /// Build a empty switch statement.
1044   explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) {}
1045
1046   /// Retrieve the variable declared in this "switch" statement, if any.
1047   ///
1048   /// In the following example, "x" is the condition variable.
1049   /// \code
1050   /// switch (int x = foo()) {
1051   ///   case 0: break;
1052   ///   // ...
1053   /// }
1054   /// \endcode
1055   VarDecl *getConditionVariable() const;
1056   void setConditionVariable(const ASTContext &C, VarDecl *V);
1057
1058   /// If this SwitchStmt has a condition variable, return the faux DeclStmt
1059   /// associated with the creation of that condition variable.
1060   const DeclStmt *getConditionVariableDeclStmt() const {
1061     return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1062   }
1063
1064   Stmt *getInit() { return SubExprs[INIT]; }
1065   const Stmt *getInit() const { return SubExprs[INIT]; }
1066   void setInit(Stmt *S) { SubExprs[INIT] = S; }
1067   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1068   const Stmt *getBody() const { return SubExprs[BODY]; }
1069   const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); }
1070
1071   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1072   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
1073   Stmt *getBody() { return SubExprs[BODY]; }
1074   void setBody(Stmt *S) { SubExprs[BODY] = S; }
1075   SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); }
1076
1077   /// Set the case list for this switch statement.
1078   void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); }
1079
1080   SourceLocation getSwitchLoc() const { return SwitchLoc; }
1081   void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
1082
1083   void setBody(Stmt *S, SourceLocation SL) {
1084     SubExprs[BODY] = S;
1085     SwitchLoc = SL;
1086   }
1087
1088   void addSwitchCase(SwitchCase *SC) {
1089     assert(!SC->getNextSwitchCase()
1090            && "case/default already added to a switch");
1091     SC->setNextSwitchCase(FirstCase.getPointer());
1092     FirstCase.setPointer(SC);
1093   }
1094
1095   /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1096   /// switch over an enum value then all cases have been explicitly covered.
1097   void setAllEnumCasesCovered() { FirstCase.setInt(true); }
1098
1099   /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1100   /// have been explicitly covered.
1101   bool isAllEnumCasesCovered() const { return FirstCase.getInt(); }
1102
1103   SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1104
1105   SourceLocation getLocEnd() const LLVM_READONLY {
1106     return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
1107   }
1108
1109   // Iterators
1110   child_range children() {
1111     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1112   }
1113
1114   static bool classof(const Stmt *T) {
1115     return T->getStmtClass() == SwitchStmtClass;
1116   }
1117 };
1118
1119 /// WhileStmt - This represents a 'while' stmt.
1120 class WhileStmt : public Stmt {
1121   SourceLocation WhileLoc;
1122   enum { VAR, COND, BODY, END_EXPR };
1123   Stmt* SubExprs[END_EXPR];
1124
1125 public:
1126   WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1127             SourceLocation WL);
1128
1129   /// Build an empty while statement.
1130   explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) {}
1131
1132   /// Retrieve the variable declared in this "while" statement, if any.
1133   ///
1134   /// In the following example, "x" is the condition variable.
1135   /// \code
1136   /// while (int x = random()) {
1137   ///   // ...
1138   /// }
1139   /// \endcode
1140   VarDecl *getConditionVariable() const;
1141   void setConditionVariable(const ASTContext &C, VarDecl *V);
1142
1143   /// If this WhileStmt has a condition variable, return the faux DeclStmt
1144   /// associated with the creation of that condition variable.
1145   const DeclStmt *getConditionVariableDeclStmt() const {
1146     return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1147   }
1148
1149   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1150   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1151   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1152   Stmt *getBody() { return SubExprs[BODY]; }
1153   const Stmt *getBody() const { return SubExprs[BODY]; }
1154   void setBody(Stmt *S) { SubExprs[BODY] = S; }
1155
1156   SourceLocation getWhileLoc() const { return WhileLoc; }
1157   void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1158
1159   SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1160
1161   SourceLocation getLocEnd() const LLVM_READONLY {
1162     return SubExprs[BODY]->getLocEnd();
1163   }
1164
1165   static bool classof(const Stmt *T) {
1166     return T->getStmtClass() == WhileStmtClass;
1167   }
1168
1169   // Iterators
1170   child_range children() {
1171     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1172   }
1173 };
1174
1175 /// DoStmt - This represents a 'do/while' stmt.
1176 class DoStmt : public Stmt {
1177   SourceLocation DoLoc;
1178   enum { BODY, COND, END_EXPR };
1179   Stmt* SubExprs[END_EXPR];
1180   SourceLocation WhileLoc;
1181   SourceLocation RParenLoc;  // Location of final ')' in do stmt condition.
1182
1183 public:
1184   DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1185          SourceLocation RP)
1186     : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1187     SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1188     SubExprs[BODY] = body;
1189   }
1190
1191   /// Build an empty do-while statement.
1192   explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
1193
1194   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1195   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1196   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1197   Stmt *getBody() { return SubExprs[BODY]; }
1198   const Stmt *getBody() const { return SubExprs[BODY]; }
1199   void setBody(Stmt *S) { SubExprs[BODY] = S; }
1200
1201   SourceLocation getDoLoc() const { return DoLoc; }
1202   void setDoLoc(SourceLocation L) { DoLoc = L; }
1203   SourceLocation getWhileLoc() const { return WhileLoc; }
1204   void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1205
1206   SourceLocation getRParenLoc() const { return RParenLoc; }
1207   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1208
1209   SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1210   SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1211
1212   static bool classof(const Stmt *T) {
1213     return T->getStmtClass() == DoStmtClass;
1214   }
1215
1216   // Iterators
1217   child_range children() {
1218     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1219   }
1220 };
1221
1222 /// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
1223 /// the init/cond/inc parts of the ForStmt will be null if they were not
1224 /// specified in the source.
1225 class ForStmt : public Stmt {
1226   SourceLocation ForLoc;
1227   enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1228   Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1229   SourceLocation LParenLoc, RParenLoc;
1230
1231 public:
1232   ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1233           Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1234           SourceLocation RP);
1235
1236   /// Build an empty for statement.
1237   explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
1238
1239   Stmt *getInit() { return SubExprs[INIT]; }
1240
1241   /// Retrieve the variable declared in this "for" statement, if any.
1242   ///
1243   /// In the following example, "y" is the condition variable.
1244   /// \code
1245   /// for (int x = random(); int y = mangle(x); ++x) {
1246   ///   // ...
1247   /// }
1248   /// \endcode
1249   VarDecl *getConditionVariable() const;
1250   void setConditionVariable(const ASTContext &C, VarDecl *V);
1251
1252   /// If this ForStmt has a condition variable, return the faux DeclStmt
1253   /// associated with the creation of that condition variable.
1254   const DeclStmt *getConditionVariableDeclStmt() const {
1255     return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1256   }
1257
1258   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1259   Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1260   Stmt *getBody() { return SubExprs[BODY]; }
1261
1262   const Stmt *getInit() const { return SubExprs[INIT]; }
1263   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1264   const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1265   const Stmt *getBody() const { return SubExprs[BODY]; }
1266
1267   void setInit(Stmt *S) { SubExprs[INIT] = S; }
1268   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1269   void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1270   void setBody(Stmt *S) { SubExprs[BODY] = S; }
1271
1272   SourceLocation getForLoc() const { return ForLoc; }
1273   void setForLoc(SourceLocation L) { ForLoc = L; }
1274   SourceLocation getLParenLoc() const { return LParenLoc; }
1275   void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1276   SourceLocation getRParenLoc() const { return RParenLoc; }
1277   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1278
1279   SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1280
1281   SourceLocation getLocEnd() const LLVM_READONLY {
1282     return SubExprs[BODY]->getLocEnd();
1283   }
1284
1285   static bool classof(const Stmt *T) {
1286     return T->getStmtClass() == ForStmtClass;
1287   }
1288
1289   // Iterators
1290   child_range children() {
1291     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1292   }
1293 };
1294
1295 /// GotoStmt - This represents a direct goto.
1296 class GotoStmt : public Stmt {
1297   LabelDecl *Label;
1298   SourceLocation GotoLoc;
1299   SourceLocation LabelLoc;
1300
1301 public:
1302   GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1303       : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1304
1305   /// Build an empty goto statement.
1306   explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
1307
1308   LabelDecl *getLabel() const { return Label; }
1309   void setLabel(LabelDecl *D) { Label = D; }
1310
1311   SourceLocation getGotoLoc() const { return GotoLoc; }
1312   void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1313   SourceLocation getLabelLoc() const { return LabelLoc; }
1314   void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1315
1316   SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1317   SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1318
1319   static bool classof(const Stmt *T) {
1320     return T->getStmtClass() == GotoStmtClass;
1321   }
1322
1323   // Iterators
1324   child_range children() {
1325     return child_range(child_iterator(), child_iterator());
1326   }
1327 };
1328
1329 /// IndirectGotoStmt - This represents an indirect goto.
1330 class IndirectGotoStmt : public Stmt {
1331   SourceLocation GotoLoc;
1332   SourceLocation StarLoc;
1333   Stmt *Target;
1334
1335 public:
1336   IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1337                    Expr *target)
1338     : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1339       Target((Stmt*)target) {}
1340
1341   /// Build an empty indirect goto statement.
1342   explicit IndirectGotoStmt(EmptyShell Empty)
1343       : Stmt(IndirectGotoStmtClass, Empty) {}
1344
1345   void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1346   SourceLocation getGotoLoc() const { return GotoLoc; }
1347   void setStarLoc(SourceLocation L) { StarLoc = L; }
1348   SourceLocation getStarLoc() const { return StarLoc; }
1349
1350   Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1351   const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1352   void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1353
1354   /// getConstantTarget - Returns the fixed target of this indirect
1355   /// goto, if one exists.
1356   LabelDecl *getConstantTarget();
1357   const LabelDecl *getConstantTarget() const {
1358     return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1359   }
1360
1361   SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1362   SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1363
1364   static bool classof(const Stmt *T) {
1365     return T->getStmtClass() == IndirectGotoStmtClass;
1366   }
1367
1368   // Iterators
1369   child_range children() { return child_range(&Target, &Target+1); }
1370 };
1371
1372 /// ContinueStmt - This represents a continue.
1373 class ContinueStmt : public Stmt {
1374   SourceLocation ContinueLoc;
1375
1376 public:
1377   ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1378
1379   /// Build an empty continue statement.
1380   explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
1381
1382   SourceLocation getContinueLoc() const { return ContinueLoc; }
1383   void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1384
1385   SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1386   SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1387
1388   static bool classof(const Stmt *T) {
1389     return T->getStmtClass() == ContinueStmtClass;
1390   }
1391
1392   // Iterators
1393   child_range children() {
1394     return child_range(child_iterator(), child_iterator());
1395   }
1396 };
1397
1398 /// BreakStmt - This represents a break.
1399 class BreakStmt : public Stmt {
1400   SourceLocation BreakLoc;
1401
1402 public:
1403   BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {
1404     static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation),
1405                   "BreakStmt too large");
1406   }
1407
1408   /// Build an empty break statement.
1409   explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
1410
1411   SourceLocation getBreakLoc() const { return BreakLoc; }
1412   void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1413
1414   SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1415   SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1416
1417   static bool classof(const Stmt *T) {
1418     return T->getStmtClass() == BreakStmtClass;
1419   }
1420
1421   // Iterators
1422   child_range children() {
1423     return child_range(child_iterator(), child_iterator());
1424   }
1425 };
1426
1427 /// ReturnStmt - This represents a return, optionally of an expression:
1428 ///   return;
1429 ///   return 4;
1430 ///
1431 /// Note that GCC allows return with no argument in a function declared to
1432 /// return a value, and it allows returning a value in functions declared to
1433 /// return void.  We explicitly model this in the AST, which means you can't
1434 /// depend on the return type of the function and the presence of an argument.
1435 class ReturnStmt : public Stmt {
1436   SourceLocation RetLoc;
1437   Stmt *RetExpr;
1438   const VarDecl *NRVOCandidate;
1439
1440 public:
1441   explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {}
1442
1443   ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1444       : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E),
1445         NRVOCandidate(NRVOCandidate) {}
1446
1447   /// Build an empty return expression.
1448   explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) {}
1449
1450   const Expr *getRetValue() const;
1451   Expr *getRetValue();
1452   void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1453
1454   SourceLocation getReturnLoc() const { return RetLoc; }
1455   void setReturnLoc(SourceLocation L) { RetLoc = L; }
1456
1457   /// Retrieve the variable that might be used for the named return
1458   /// value optimization.
1459   ///
1460   /// The optimization itself can only be performed if the variable is
1461   /// also marked as an NRVO object.
1462   const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1463   void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1464
1465   SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1466
1467   SourceLocation getLocEnd() const LLVM_READONLY {
1468     return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1469   }
1470
1471   static bool classof(const Stmt *T) {
1472     return T->getStmtClass() == ReturnStmtClass;
1473   }
1474
1475   // Iterators
1476   child_range children() {
1477     if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1478     return child_range(child_iterator(), child_iterator());
1479   }
1480 };
1481
1482 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1483 class AsmStmt : public Stmt {
1484 protected:
1485   friend class ASTStmtReader;
1486
1487   SourceLocation AsmLoc;
1488
1489   /// True if the assembly statement does not have any input or output
1490   /// operands.
1491   bool IsSimple;
1492
1493   /// If true, treat this inline assembly as having side effects.
1494   /// This assembly statement should not be optimized, deleted or moved.
1495   bool IsVolatile;
1496
1497   unsigned NumOutputs;
1498   unsigned NumInputs;
1499   unsigned NumClobbers;
1500
1501   Stmt **Exprs = nullptr;
1502
1503   AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1504           unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
1505       : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1506         NumOutputs(numoutputs), NumInputs(numinputs),
1507         NumClobbers(numclobbers) {}
1508
1509 public:
1510   /// Build an empty inline-assembly statement.
1511   explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
1512
1513   SourceLocation getAsmLoc() const { return AsmLoc; }
1514   void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1515
1516   bool isSimple() const { return IsSimple; }
1517   void setSimple(bool V) { IsSimple = V; }
1518
1519   bool isVolatile() const { return IsVolatile; }
1520   void setVolatile(bool V) { IsVolatile = V; }
1521
1522   SourceLocation getLocStart() const LLVM_READONLY { return {}; }
1523   SourceLocation getLocEnd() const LLVM_READONLY { return {}; }
1524
1525   //===--- Asm String Analysis ---===//
1526
1527   /// Assemble final IR asm string.
1528   std::string generateAsmString(const ASTContext &C) const;
1529
1530   //===--- Output operands ---===//
1531
1532   unsigned getNumOutputs() const { return NumOutputs; }
1533
1534   /// getOutputConstraint - Return the constraint string for the specified
1535   /// output operand.  All output constraints are known to be non-empty (either
1536   /// '=' or '+').
1537   StringRef getOutputConstraint(unsigned i) const;
1538
1539   /// isOutputPlusConstraint - Return true if the specified output constraint
1540   /// is a "+" constraint (which is both an input and an output) or false if it
1541   /// is an "=" constraint (just an output).
1542   bool isOutputPlusConstraint(unsigned i) const {
1543     return getOutputConstraint(i)[0] == '+';
1544   }
1545
1546   const Expr *getOutputExpr(unsigned i) const;
1547
1548   /// getNumPlusOperands - Return the number of output operands that have a "+"
1549   /// constraint.
1550   unsigned getNumPlusOperands() const;
1551
1552   //===--- Input operands ---===//
1553
1554   unsigned getNumInputs() const { return NumInputs; }
1555
1556   /// getInputConstraint - Return the specified input constraint.  Unlike output
1557   /// constraints, these can be empty.
1558   StringRef getInputConstraint(unsigned i) const;
1559   
1560   const Expr *getInputExpr(unsigned i) const;
1561
1562   //===--- Other ---===//
1563
1564   unsigned getNumClobbers() const { return NumClobbers; }
1565   StringRef getClobber(unsigned i) const;
1566
1567   static bool classof(const Stmt *T) {
1568     return T->getStmtClass() == GCCAsmStmtClass ||
1569       T->getStmtClass() == MSAsmStmtClass;
1570   }
1571
1572   // Input expr iterators.
1573
1574   using inputs_iterator = ExprIterator;
1575   using const_inputs_iterator = ConstExprIterator;
1576   using inputs_range = llvm::iterator_range<inputs_iterator>;
1577   using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
1578
1579   inputs_iterator begin_inputs() {
1580     return &Exprs[0] + NumOutputs;
1581   }
1582
1583   inputs_iterator end_inputs() {
1584     return &Exprs[0] + NumOutputs + NumInputs;
1585   }
1586
1587   inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1588
1589   const_inputs_iterator begin_inputs() const {
1590     return &Exprs[0] + NumOutputs;
1591   }
1592
1593   const_inputs_iterator end_inputs() const {
1594     return &Exprs[0] + NumOutputs + NumInputs;
1595   }
1596
1597   inputs_const_range inputs() const {
1598     return inputs_const_range(begin_inputs(), end_inputs());
1599   }
1600
1601   // Output expr iterators.
1602
1603   using outputs_iterator = ExprIterator;
1604   using const_outputs_iterator = ConstExprIterator;
1605   using outputs_range = llvm::iterator_range<outputs_iterator>;
1606   using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
1607
1608   outputs_iterator begin_outputs() {
1609     return &Exprs[0];
1610   }
1611
1612   outputs_iterator end_outputs() {
1613     return &Exprs[0] + NumOutputs;
1614   }
1615
1616   outputs_range outputs() {
1617     return outputs_range(begin_outputs(), end_outputs());
1618   }
1619
1620   const_outputs_iterator begin_outputs() const {
1621     return &Exprs[0];
1622   }
1623
1624   const_outputs_iterator end_outputs() const {
1625     return &Exprs[0] + NumOutputs;
1626   }
1627
1628   outputs_const_range outputs() const {
1629     return outputs_const_range(begin_outputs(), end_outputs());
1630   }
1631
1632   child_range children() {
1633     return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1634   }
1635 };
1636
1637 /// This represents a GCC inline-assembly statement extension.
1638 class GCCAsmStmt : public AsmStmt {
1639   friend class ASTStmtReader;
1640
1641   SourceLocation RParenLoc;
1642   StringLiteral *AsmStr;
1643
1644   // FIXME: If we wanted to, we could allocate all of these in one big array.
1645   StringLiteral **Constraints = nullptr;
1646   StringLiteral **Clobbers = nullptr;
1647   IdentifierInfo **Names = nullptr;
1648
1649 public:
1650   GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1651              bool isvolatile, unsigned numoutputs, unsigned numinputs,
1652              IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1653              StringLiteral *asmstr, unsigned numclobbers,
1654              StringLiteral **clobbers, SourceLocation rparenloc);
1655
1656   /// Build an empty inline-assembly statement.
1657   explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
1658
1659   SourceLocation getRParenLoc() const { return RParenLoc; }
1660   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1661
1662   //===--- Asm String Analysis ---===//
1663
1664   const StringLiteral *getAsmString() const { return AsmStr; }
1665   StringLiteral *getAsmString() { return AsmStr; }
1666   void setAsmString(StringLiteral *E) { AsmStr = E; }
1667
1668   /// AsmStringPiece - this is part of a decomposed asm string specification
1669   /// (for use with the AnalyzeAsmString function below).  An asm string is
1670   /// considered to be a concatenation of these parts.
1671   class AsmStringPiece {
1672   public:
1673     enum Kind {
1674       String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1675       Operand  // Operand reference, with optional modifier %c4.
1676     };
1677
1678   private:
1679     Kind MyKind;
1680     std::string Str;
1681     unsigned OperandNo;
1682
1683     // Source range for operand references.
1684     CharSourceRange Range;
1685
1686   public:
1687     AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1688     AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
1689                    SourceLocation End)
1690         : MyKind(Operand), Str(S), OperandNo(OpNo),
1691           Range(CharSourceRange::getCharRange(Begin, End)) {}
1692
1693     bool isString() const { return MyKind == String; }
1694     bool isOperand() const { return MyKind == Operand; }
1695
1696     const std::string &getString() const { return Str; }
1697
1698     unsigned getOperandNo() const {
1699       assert(isOperand());
1700       return OperandNo;
1701     }
1702
1703     CharSourceRange getRange() const {
1704       assert(isOperand() && "Range is currently used only for Operands.");
1705       return Range;
1706     }
1707
1708     /// getModifier - Get the modifier for this operand, if present.  This
1709     /// returns '\0' if there was no modifier.
1710     char getModifier() const;
1711   };
1712
1713   /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1714   /// it into pieces.  If the asm string is erroneous, emit errors and return
1715   /// true, otherwise return false.  This handles canonicalization and
1716   /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1717   //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1718   unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1719                             const ASTContext &C, unsigned &DiagOffs) const;
1720
1721   /// Assemble final IR asm string.
1722   std::string generateAsmString(const ASTContext &C) const;
1723
1724   //===--- Output operands ---===//
1725
1726   IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; }
1727
1728   StringRef getOutputName(unsigned i) const {
1729     if (IdentifierInfo *II = getOutputIdentifier(i))
1730       return II->getName();
1731
1732     return {};
1733   }
1734
1735   StringRef getOutputConstraint(unsigned i) const;
1736
1737   const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1738     return Constraints[i];
1739   }
1740   StringLiteral *getOutputConstraintLiteral(unsigned i) {
1741     return Constraints[i];
1742   }
1743
1744   Expr *getOutputExpr(unsigned i);
1745
1746   const Expr *getOutputExpr(unsigned i) const {
1747     return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1748   }
1749
1750   //===--- Input operands ---===//
1751
1752   IdentifierInfo *getInputIdentifier(unsigned i) const {
1753     return Names[i + NumOutputs];
1754   }
1755
1756   StringRef getInputName(unsigned i) const {
1757     if (IdentifierInfo *II = getInputIdentifier(i))
1758       return II->getName();
1759
1760     return {};
1761   }
1762
1763   StringRef getInputConstraint(unsigned i) const;
1764
1765   const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1766     return Constraints[i + NumOutputs];
1767   }
1768   StringLiteral *getInputConstraintLiteral(unsigned i) {
1769     return Constraints[i + NumOutputs];
1770   }
1771
1772   Expr *getInputExpr(unsigned i);
1773   void setInputExpr(unsigned i, Expr *E);
1774
1775   const Expr *getInputExpr(unsigned i) const {
1776     return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1777   }
1778
1779 private:
1780   void setOutputsAndInputsAndClobbers(const ASTContext &C,
1781                                       IdentifierInfo **Names,
1782                                       StringLiteral **Constraints,
1783                                       Stmt **Exprs,
1784                                       unsigned NumOutputs,
1785                                       unsigned NumInputs,
1786                                       StringLiteral **Clobbers,
1787                                       unsigned NumClobbers);
1788
1789 public:
1790   //===--- Other ---===//
1791
1792   /// getNamedOperand - Given a symbolic operand reference like %[foo],
1793   /// translate this into a numeric value needed to reference the same operand.
1794   /// This returns -1 if the operand name is invalid.
1795   int getNamedOperand(StringRef SymbolicName) const;
1796
1797   StringRef getClobber(unsigned i) const;
1798
1799   StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1800   const StringLiteral *getClobberStringLiteral(unsigned i) const {
1801     return Clobbers[i];
1802   }
1803
1804   SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1805   SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1806
1807   static bool classof(const Stmt *T) {
1808     return T->getStmtClass() == GCCAsmStmtClass;
1809   }
1810 };
1811
1812 /// This represents a Microsoft inline-assembly statement extension.
1813 class MSAsmStmt : public AsmStmt {
1814   friend class ASTStmtReader;
1815
1816   SourceLocation LBraceLoc, EndLoc;
1817   StringRef AsmStr;
1818
1819   unsigned NumAsmToks = 0;
1820
1821   Token *AsmToks = nullptr;
1822   StringRef *Constraints = nullptr;
1823   StringRef *Clobbers = nullptr;
1824
1825 public:
1826   MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1827             SourceLocation lbraceloc, bool issimple, bool isvolatile,
1828             ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1829             ArrayRef<StringRef> constraints,
1830             ArrayRef<Expr*> exprs, StringRef asmstr,
1831             ArrayRef<StringRef> clobbers, SourceLocation endloc);
1832
1833   /// Build an empty MS-style inline-assembly statement.
1834   explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
1835
1836   SourceLocation getLBraceLoc() const { return LBraceLoc; }
1837   void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1838   SourceLocation getEndLoc() const { return EndLoc; }
1839   void setEndLoc(SourceLocation L) { EndLoc = L; }
1840
1841   bool hasBraces() const { return LBraceLoc.isValid(); }
1842
1843   unsigned getNumAsmToks() { return NumAsmToks; }
1844   Token *getAsmToks() { return AsmToks; }
1845
1846   //===--- Asm String Analysis ---===//
1847   StringRef getAsmString() const { return AsmStr; }
1848
1849   /// Assemble final IR asm string.
1850   std::string generateAsmString(const ASTContext &C) const;
1851
1852   //===--- Output operands ---===//
1853
1854   StringRef getOutputConstraint(unsigned i) const {
1855     assert(i < NumOutputs);
1856     return Constraints[i];
1857   }
1858
1859   Expr *getOutputExpr(unsigned i);
1860
1861   const Expr *getOutputExpr(unsigned i) const {
1862     return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1863   }
1864
1865   //===--- Input operands ---===//
1866
1867   StringRef getInputConstraint(unsigned i) const {
1868     assert(i < NumInputs);
1869     return Constraints[i + NumOutputs];
1870   }
1871
1872   Expr *getInputExpr(unsigned i);
1873   void setInputExpr(unsigned i, Expr *E);
1874
1875   const Expr *getInputExpr(unsigned i) const {
1876     return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1877   }
1878
1879   //===--- Other ---===//
1880
1881   ArrayRef<StringRef> getAllConstraints() const {
1882     return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
1883   }
1884
1885   ArrayRef<StringRef> getClobbers() const {
1886     return llvm::makeArrayRef(Clobbers, NumClobbers);
1887   }
1888
1889   ArrayRef<Expr*> getAllExprs() const {
1890     return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
1891                               NumInputs + NumOutputs);
1892   }
1893
1894   StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1895
1896 private:
1897   void initialize(const ASTContext &C, StringRef AsmString,
1898                   ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1899                   ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1900
1901 public:
1902   SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1903   SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1904
1905   static bool classof(const Stmt *T) {
1906     return T->getStmtClass() == MSAsmStmtClass;
1907   }
1908
1909   child_range children() {
1910     return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1911   }
1912 };
1913
1914 class SEHExceptStmt : public Stmt {
1915   friend class ASTReader;
1916   friend class ASTStmtReader;
1917
1918   SourceLocation  Loc;
1919   Stmt *Children[2];
1920
1921   enum { FILTER_EXPR, BLOCK };
1922
1923   SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
1924   explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
1925
1926 public:
1927   static SEHExceptStmt* Create(const ASTContext &C,
1928                                SourceLocation ExceptLoc,
1929                                Expr *FilterExpr,
1930                                Stmt *Block);
1931
1932   SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1933   SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1934
1935   SourceLocation getExceptLoc() const { return Loc; }
1936   SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1937
1938   Expr *getFilterExpr() const {
1939     return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1940   }
1941
1942   CompoundStmt *getBlock() const {
1943     return cast<CompoundStmt>(Children[BLOCK]);
1944   }
1945
1946   child_range children() {
1947     return child_range(Children, Children+2);
1948   }
1949
1950   static bool classof(const Stmt *T) {
1951     return T->getStmtClass() == SEHExceptStmtClass;
1952   }
1953 };
1954
1955 class SEHFinallyStmt : public Stmt {
1956   friend class ASTReader;
1957   friend class ASTStmtReader;
1958
1959   SourceLocation  Loc;
1960   Stmt *Block;
1961
1962   SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
1963   explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
1964
1965 public:
1966   static SEHFinallyStmt* Create(const ASTContext &C,
1967                                 SourceLocation FinallyLoc,
1968                                 Stmt *Block);
1969
1970   SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1971   SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1972
1973   SourceLocation getFinallyLoc() const { return Loc; }
1974   SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1975
1976   CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1977
1978   child_range children() {
1979     return child_range(&Block,&Block+1);
1980   }
1981
1982   static bool classof(const Stmt *T) {
1983     return T->getStmtClass() == SEHFinallyStmtClass;
1984   }
1985 };
1986
1987 class SEHTryStmt : public Stmt {
1988   friend class ASTReader;
1989   friend class ASTStmtReader;
1990
1991   bool IsCXXTry;
1992   SourceLocation  TryLoc;
1993   Stmt *Children[2];
1994
1995   enum { TRY = 0, HANDLER = 1 };
1996
1997   SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1998              SourceLocation TryLoc,
1999              Stmt *TryBlock,
2000              Stmt *Handler);
2001
2002   explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
2003
2004 public:
2005   static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
2006                             SourceLocation TryLoc, Stmt *TryBlock,
2007                             Stmt *Handler);
2008
2009   SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
2010   SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
2011
2012   SourceLocation getTryLoc() const { return TryLoc; }
2013   SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
2014
2015   bool getIsCXXTry() const { return IsCXXTry; }
2016
2017   CompoundStmt* getTryBlock() const {
2018     return cast<CompoundStmt>(Children[TRY]);
2019   }
2020
2021   Stmt *getHandler() const { return Children[HANDLER]; }
2022
2023   /// Returns 0 if not defined
2024   SEHExceptStmt  *getExceptHandler() const;
2025   SEHFinallyStmt *getFinallyHandler() const;
2026
2027   child_range children() {
2028     return child_range(Children, Children+2);
2029   }
2030
2031   static bool classof(const Stmt *T) {
2032     return T->getStmtClass() == SEHTryStmtClass;
2033   }
2034 };
2035
2036 /// Represents a __leave statement.
2037 class SEHLeaveStmt : public Stmt {
2038   SourceLocation LeaveLoc;
2039
2040 public:
2041   explicit SEHLeaveStmt(SourceLocation LL)
2042       : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
2043
2044   /// Build an empty __leave statement.
2045   explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
2046
2047   SourceLocation getLeaveLoc() const { return LeaveLoc; }
2048   void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
2049
2050   SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; }
2051   SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; }
2052
2053   static bool classof(const Stmt *T) {
2054     return T->getStmtClass() == SEHLeaveStmtClass;
2055   }
2056
2057   // Iterators
2058   child_range children() {
2059     return child_range(child_iterator(), child_iterator());
2060   }
2061 };
2062
2063 /// This captures a statement into a function. For example, the following
2064 /// pragma annotated compound statement can be represented as a CapturedStmt,
2065 /// and this compound statement is the body of an anonymous outlined function.
2066 /// @code
2067 /// #pragma omp parallel
2068 /// {
2069 ///   compute();
2070 /// }
2071 /// @endcode
2072 class CapturedStmt : public Stmt {
2073 public:
2074   /// The different capture forms: by 'this', by reference, capture for
2075   /// variable-length array type etc.
2076   enum VariableCaptureKind {
2077     VCK_This,
2078     VCK_ByRef,
2079     VCK_ByCopy,
2080     VCK_VLAType,
2081   };
2082
2083   /// Describes the capture of either a variable, or 'this', or
2084   /// variable-length array type.
2085   class Capture {
2086     llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2087     SourceLocation Loc;
2088
2089   public:
2090     friend class ASTStmtReader;
2091
2092     /// Create a new capture.
2093     ///
2094     /// \param Loc The source location associated with this capture.
2095     ///
2096     /// \param Kind The kind of capture (this, ByRef, ...).
2097     ///
2098     /// \param Var The variable being captured, or null if capturing this.
2099     Capture(SourceLocation Loc, VariableCaptureKind Kind,
2100             VarDecl *Var = nullptr);
2101
2102     /// Determine the kind of capture.
2103     VariableCaptureKind getCaptureKind() const;
2104
2105     /// Retrieve the source location at which the variable or 'this' was
2106     /// first used.
2107     SourceLocation getLocation() const { return Loc; }
2108
2109     /// Determine whether this capture handles the C++ 'this' pointer.
2110     bool capturesThis() const { return getCaptureKind() == VCK_This; }
2111
2112     /// Determine whether this capture handles a variable (by reference).
2113     bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2114
2115     /// Determine whether this capture handles a variable by copy.
2116     bool capturesVariableByCopy() const {
2117       return getCaptureKind() == VCK_ByCopy;
2118     }
2119
2120     /// Determine whether this capture handles a variable-length array
2121     /// type.
2122     bool capturesVariableArrayType() const {
2123       return getCaptureKind() == VCK_VLAType;
2124     }
2125
2126     /// Retrieve the declaration of the variable being captured.
2127     ///
2128     /// This operation is only valid if this capture captures a variable.
2129     VarDecl *getCapturedVar() const;
2130   };
2131
2132 private:
2133   /// The number of variable captured, including 'this'.
2134   unsigned NumCaptures;
2135
2136   /// The pointer part is the implicit the outlined function and the 
2137   /// int part is the captured region kind, 'CR_Default' etc.
2138   llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind;
2139
2140   /// The record for captured variables, a RecordDecl or CXXRecordDecl.
2141   RecordDecl *TheRecordDecl = nullptr;
2142
2143   /// Construct a captured statement.
2144   CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2145                ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2146
2147   /// Construct an empty captured statement.
2148   CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2149
2150   Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
2151
2152   Stmt *const *getStoredStmts() const {
2153     return reinterpret_cast<Stmt *const *>(this + 1);
2154   }
2155
2156   Capture *getStoredCaptures() const;
2157
2158   void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2159
2160 public:
2161   friend class ASTStmtReader;
2162
2163   static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2164                               CapturedRegionKind Kind,
2165                               ArrayRef<Capture> Captures,
2166                               ArrayRef<Expr *> CaptureInits,
2167                               CapturedDecl *CD, RecordDecl *RD);
2168
2169   static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2170                                           unsigned NumCaptures);
2171
2172   /// Retrieve the statement being captured.
2173   Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2174   const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
2175
2176   /// Retrieve the outlined function declaration.
2177   CapturedDecl *getCapturedDecl();
2178   const CapturedDecl *getCapturedDecl() const;
2179
2180   /// Set the outlined function declaration.
2181   void setCapturedDecl(CapturedDecl *D);
2182
2183   /// Retrieve the captured region kind.
2184   CapturedRegionKind getCapturedRegionKind() const;
2185
2186   /// Set the captured region kind.
2187   void setCapturedRegionKind(CapturedRegionKind Kind);
2188
2189   /// Retrieve the record declaration for captured variables.
2190   const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2191
2192   /// Set the record declaration for captured variables.
2193   void setCapturedRecordDecl(RecordDecl *D) {
2194     assert(D && "null RecordDecl");
2195     TheRecordDecl = D;
2196   }
2197
2198   /// True if this variable has been captured.
2199   bool capturesVariable(const VarDecl *Var) const;
2200
2201   /// An iterator that walks over the captures.
2202   using capture_iterator = Capture *;
2203   using const_capture_iterator = const Capture *;
2204   using capture_range = llvm::iterator_range<capture_iterator>;
2205   using capture_const_range = llvm::iterator_range<const_capture_iterator>;
2206
2207   capture_range captures() {
2208     return capture_range(capture_begin(), capture_end());
2209   }
2210   capture_const_range captures() const {
2211     return capture_const_range(capture_begin(), capture_end());
2212   }
2213
2214   /// Retrieve an iterator pointing to the first capture.
2215   capture_iterator capture_begin() { return getStoredCaptures(); }
2216   const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2217
2218   /// Retrieve an iterator pointing past the end of the sequence of
2219   /// captures.
2220   capture_iterator capture_end() const {
2221     return getStoredCaptures() + NumCaptures;
2222   }
2223
2224   /// Retrieve the number of captures, including 'this'.
2225   unsigned capture_size() const { return NumCaptures; }
2226
2227   /// Iterator that walks over the capture initialization arguments.
2228   using capture_init_iterator = Expr **;
2229   using capture_init_range = llvm::iterator_range<capture_init_iterator>;
2230
2231   /// Const iterator that walks over the capture initialization
2232   /// arguments.
2233   using const_capture_init_iterator = Expr *const *;
2234   using const_capture_init_range =
2235       llvm::iterator_range<const_capture_init_iterator>;
2236
2237   capture_init_range capture_inits() {
2238     return capture_init_range(capture_init_begin(), capture_init_end());
2239   }
2240
2241   const_capture_init_range capture_inits() const {
2242     return const_capture_init_range(capture_init_begin(), capture_init_end());
2243   }
2244
2245   /// Retrieve the first initialization argument.
2246   capture_init_iterator capture_init_begin() {
2247     return reinterpret_cast<Expr **>(getStoredStmts());
2248   }
2249
2250   const_capture_init_iterator capture_init_begin() const {
2251     return reinterpret_cast<Expr *const *>(getStoredStmts());
2252   }
2253
2254   /// Retrieve the iterator pointing one past the last initialization
2255   /// argument.
2256   capture_init_iterator capture_init_end() {
2257     return capture_init_begin() + NumCaptures;
2258   }
2259
2260   const_capture_init_iterator capture_init_end() const {
2261     return capture_init_begin() + NumCaptures;
2262   }
2263
2264   SourceLocation getLocStart() const LLVM_READONLY {
2265     return getCapturedStmt()->getLocStart();
2266   }
2267
2268   SourceLocation getLocEnd() const LLVM_READONLY {
2269     return getCapturedStmt()->getLocEnd();
2270   }
2271
2272   SourceRange getSourceRange() const LLVM_READONLY {
2273     return getCapturedStmt()->getSourceRange();
2274   }
2275
2276   static bool classof(const Stmt *T) {
2277     return T->getStmtClass() == CapturedStmtClass;
2278   }
2279
2280   child_range children();
2281 };
2282
2283 } // namespace clang
2284
2285 #endif // LLVM_CLANG_AST_STMT_H