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