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