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