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