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