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