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