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