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