Stmt.h revision aeeacf725c9e0ddd64ea9764bd008e5b6873ce51
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  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
1397  friend class ASTStmtReader;
1398
1399public:
1400  /// \brief Build an empty inline-assembly statement.
1401  explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1402    Stmt(SC, Empty), Exprs(0) { }
1403
1404  SourceLocation getAsmLoc() const { return AsmLoc; }
1405  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1406
1407  bool isSimple() const { return IsSimple; }
1408  void setSimple(bool V) { IsSimple = V; }
1409
1410  bool isVolatile() const { return IsVolatile; }
1411  void setVolatile(bool V) { IsVolatile = V; }
1412
1413  SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1414  SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1415
1416  //===--- Asm String Analysis ---===//
1417
1418  /// Assemble final IR asm string.
1419  std::string generateAsmString(ASTContext &C) const;
1420
1421  //===--- Output operands ---===//
1422
1423  unsigned getNumOutputs() const { return NumOutputs; }
1424
1425  /// getOutputConstraint - Return the constraint string for the specified
1426  /// output operand.  All output constraints are known to be non-empty (either
1427  /// '=' or '+').
1428  StringRef getOutputConstraint(unsigned i) const;
1429
1430  /// isOutputPlusConstraint - Return true if the specified output constraint
1431  /// is a "+" constraint (which is both an input and an output) or false if it
1432  /// is an "=" constraint (just an output).
1433  bool isOutputPlusConstraint(unsigned i) const {
1434    return getOutputConstraint(i)[0] == '+';
1435  }
1436
1437  const Expr *getOutputExpr(unsigned i) const;
1438
1439  /// getNumPlusOperands - Return the number of output operands that have a "+"
1440  /// constraint.
1441  unsigned getNumPlusOperands() const;
1442
1443  //===--- Input operands ---===//
1444
1445  unsigned getNumInputs() const { return NumInputs; }
1446
1447  /// getInputConstraint - Return the specified input constraint.  Unlike output
1448  /// constraints, these can be empty.
1449  StringRef getInputConstraint(unsigned i) const;
1450
1451  const Expr *getInputExpr(unsigned i) const;
1452
1453  //===--- Other ---===//
1454
1455  unsigned getNumClobbers() const { return NumClobbers; }
1456  StringRef getClobber(unsigned i) const;
1457
1458  static bool classof(const Stmt *T) {
1459    return T->getStmtClass() == GCCAsmStmtClass ||
1460      T->getStmtClass() == MSAsmStmtClass;
1461  }
1462
1463  // Input expr iterators.
1464
1465  typedef ExprIterator inputs_iterator;
1466  typedef ConstExprIterator const_inputs_iterator;
1467
1468  inputs_iterator begin_inputs() {
1469    return &Exprs[0] + NumOutputs;
1470  }
1471
1472  inputs_iterator end_inputs() {
1473    return &Exprs[0] + NumOutputs + NumInputs;
1474  }
1475
1476  const_inputs_iterator begin_inputs() const {
1477    return &Exprs[0] + NumOutputs;
1478  }
1479
1480  const_inputs_iterator end_inputs() const {
1481    return &Exprs[0] + NumOutputs + NumInputs;
1482  }
1483
1484  // Output expr iterators.
1485
1486  typedef ExprIterator outputs_iterator;
1487  typedef ConstExprIterator const_outputs_iterator;
1488
1489  outputs_iterator begin_outputs() {
1490    return &Exprs[0];
1491  }
1492  outputs_iterator end_outputs() {
1493    return &Exprs[0] + NumOutputs;
1494  }
1495
1496  const_outputs_iterator begin_outputs() const {
1497    return &Exprs[0];
1498  }
1499  const_outputs_iterator end_outputs() const {
1500    return &Exprs[0] + NumOutputs;
1501  }
1502
1503  child_range children() {
1504    return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1505  }
1506};
1507
1508/// This represents a GCC inline-assembly statement extension.
1509///
1510class GCCAsmStmt : public AsmStmt {
1511  SourceLocation RParenLoc;
1512  StringLiteral *AsmStr;
1513
1514  // FIXME: If we wanted to, we could allocate all of these in one big array.
1515  StringLiteral **Constraints;
1516  StringLiteral **Clobbers;
1517  IdentifierInfo **Names;
1518
1519  friend class ASTStmtReader;
1520
1521public:
1522  GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
1523             bool isvolatile, unsigned numoutputs, unsigned numinputs,
1524             IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1525             StringLiteral *asmstr, unsigned numclobbers,
1526             StringLiteral **clobbers, SourceLocation rparenloc);
1527
1528  /// \brief Build an empty inline-assembly statement.
1529  explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1530    Constraints(0), Clobbers(0), Names(0) { }
1531
1532  SourceLocation getRParenLoc() const { return RParenLoc; }
1533  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1534
1535  //===--- Asm String Analysis ---===//
1536
1537  const StringLiteral *getAsmString() const { return AsmStr; }
1538  StringLiteral *getAsmString() { return AsmStr; }
1539  void setAsmString(StringLiteral *E) { AsmStr = E; }
1540
1541  /// AsmStringPiece - this is part of a decomposed asm string specification
1542  /// (for use with the AnalyzeAsmString function below).  An asm string is
1543  /// considered to be a concatenation of these parts.
1544  class AsmStringPiece {
1545  public:
1546    enum Kind {
1547      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1548      Operand  // Operand reference, with optional modifier %c4.
1549    };
1550  private:
1551    Kind MyKind;
1552    std::string Str;
1553    unsigned OperandNo;
1554  public:
1555    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1556    AsmStringPiece(unsigned OpNo, char Modifier)
1557      : MyKind(Operand), Str(), OperandNo(OpNo) {
1558      Str += Modifier;
1559    }
1560
1561    bool isString() const { return MyKind == String; }
1562    bool isOperand() const { return MyKind == Operand; }
1563
1564    const std::string &getString() const {
1565      assert(isString());
1566      return Str;
1567    }
1568
1569    unsigned getOperandNo() const {
1570      assert(isOperand());
1571      return OperandNo;
1572    }
1573
1574    /// getModifier - Get the modifier for this operand, if present.  This
1575    /// returns '\0' if there was no modifier.
1576    char getModifier() const {
1577      assert(isOperand());
1578      return Str[0];
1579    }
1580  };
1581
1582  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1583  /// it into pieces.  If the asm string is erroneous, emit errors and return
1584  /// true, otherwise return false.  This handles canonicalization and
1585  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1586  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1587  unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1588                            ASTContext &C, unsigned &DiagOffs) const;
1589
1590  /// Assemble final IR asm string.
1591  std::string generateAsmString(ASTContext &C) const;
1592
1593  //===--- Output operands ---===//
1594
1595  IdentifierInfo *getOutputIdentifier(unsigned i) const {
1596    return Names[i];
1597  }
1598
1599  StringRef getOutputName(unsigned i) const {
1600    if (IdentifierInfo *II = getOutputIdentifier(i))
1601      return II->getName();
1602
1603    return StringRef();
1604  }
1605
1606  StringRef getOutputConstraint(unsigned i) const;
1607
1608  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1609    return Constraints[i];
1610  }
1611  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1612    return Constraints[i];
1613  }
1614
1615  Expr *getOutputExpr(unsigned i);
1616
1617  const Expr *getOutputExpr(unsigned i) const {
1618    return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1619  }
1620
1621  //===--- Input operands ---===//
1622
1623  IdentifierInfo *getInputIdentifier(unsigned i) const {
1624    return Names[i + NumOutputs];
1625  }
1626
1627  StringRef getInputName(unsigned i) const {
1628    if (IdentifierInfo *II = getInputIdentifier(i))
1629      return II->getName();
1630
1631    return StringRef();
1632  }
1633
1634  StringRef getInputConstraint(unsigned i) const;
1635
1636  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1637    return Constraints[i + NumOutputs];
1638  }
1639  StringLiteral *getInputConstraintLiteral(unsigned i) {
1640    return Constraints[i + NumOutputs];
1641  }
1642
1643  Expr *getInputExpr(unsigned i);
1644  void setInputExpr(unsigned i, Expr *E);
1645
1646  const Expr *getInputExpr(unsigned i) const {
1647    return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1648  }
1649
1650private:
1651  void setOutputsAndInputsAndClobbers(ASTContext &C,
1652                                      IdentifierInfo **Names,
1653                                      StringLiteral **Constraints,
1654                                      Stmt **Exprs,
1655                                      unsigned NumOutputs,
1656                                      unsigned NumInputs,
1657                                      StringLiteral **Clobbers,
1658                                      unsigned NumClobbers);
1659public:
1660
1661  //===--- Other ---===//
1662
1663  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1664  /// translate this into a numeric value needed to reference the same operand.
1665  /// This returns -1 if the operand name is invalid.
1666  int getNamedOperand(StringRef SymbolicName) const;
1667
1668  StringRef getClobber(unsigned i) const;
1669  StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1670  const StringLiteral *getClobberStringLiteral(unsigned i) const {
1671    return Clobbers[i];
1672  }
1673
1674  SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1675  SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1676
1677  static bool classof(const Stmt *T) {
1678    return T->getStmtClass() == GCCAsmStmtClass;
1679  }
1680};
1681
1682/// This represents a Microsoft inline-assembly statement extension.
1683///
1684class MSAsmStmt : public AsmStmt {
1685  SourceLocation LBraceLoc, EndLoc;
1686  StringRef AsmStr;
1687
1688  unsigned NumAsmToks;
1689
1690  Token *AsmToks;
1691  StringRef *Constraints;
1692  StringRef *Clobbers;
1693
1694  friend class ASTStmtReader;
1695
1696public:
1697  MSAsmStmt(ASTContext &C, SourceLocation asmloc, SourceLocation lbraceloc,
1698            bool issimple, bool isvolatile, ArrayRef<Token> asmtoks,
1699            unsigned numoutputs, unsigned numinputs,
1700            ArrayRef<StringRef> constraints,
1701            ArrayRef<Expr*> exprs, StringRef asmstr,
1702            ArrayRef<StringRef> clobbers, SourceLocation endloc);
1703
1704  /// \brief Build an empty MS-style inline-assembly statement.
1705  explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1706    NumAsmToks(0), AsmToks(0), Constraints(0), Clobbers(0) { }
1707
1708  SourceLocation getLBraceLoc() const { return LBraceLoc; }
1709  void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1710  SourceLocation getEndLoc() const { return EndLoc; }
1711  void setEndLoc(SourceLocation L) { EndLoc = L; }
1712
1713  bool hasBraces() const { return LBraceLoc.isValid(); }
1714
1715  unsigned getNumAsmToks() { return NumAsmToks; }
1716  Token *getAsmToks() { return AsmToks; }
1717
1718  //===--- Asm String Analysis ---===//
1719  StringRef getAsmString() const { return AsmStr; }
1720
1721  /// Assemble final IR asm string.
1722  std::string generateAsmString(ASTContext &C) const;
1723
1724  //===--- Output operands ---===//
1725
1726  StringRef getOutputConstraint(unsigned i) const {
1727    assert(i < NumOutputs);
1728    return Constraints[i];
1729  }
1730
1731  Expr *getOutputExpr(unsigned i);
1732
1733  const Expr *getOutputExpr(unsigned i) const {
1734    return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1735  }
1736
1737  //===--- Input operands ---===//
1738
1739  StringRef getInputConstraint(unsigned i) const {
1740    assert(i < NumInputs);
1741    return Constraints[i + NumOutputs];
1742  }
1743
1744  Expr *getInputExpr(unsigned i);
1745  void setInputExpr(unsigned i, Expr *E);
1746
1747  const Expr *getInputExpr(unsigned i) const {
1748    return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1749  }
1750
1751  //===--- Other ---===//
1752
1753  ArrayRef<StringRef> getAllConstraints() const {
1754    return ArrayRef<StringRef>(Constraints, NumInputs + NumOutputs);
1755  }
1756  ArrayRef<StringRef> getClobbers() const {
1757    return ArrayRef<StringRef>(Clobbers, NumClobbers);
1758  }
1759  ArrayRef<Expr*> getAllExprs() const {
1760    return ArrayRef<Expr*>(reinterpret_cast<Expr**>(Exprs),
1761                           NumInputs + NumOutputs);
1762  }
1763
1764  StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1765
1766private:
1767  void initialize(ASTContext &C,
1768                  StringRef AsmString,
1769                  ArrayRef<Token> AsmToks,
1770                  ArrayRef<StringRef> Constraints,
1771                  ArrayRef<Expr*> Exprs,
1772                  ArrayRef<StringRef> Clobbers);
1773public:
1774
1775  SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1776  SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1777
1778  static bool classof(const Stmt *T) {
1779    return T->getStmtClass() == MSAsmStmtClass;
1780  }
1781
1782  child_range children() {
1783    return child_range(&Exprs[0], &Exprs[0]);
1784  }
1785};
1786
1787class SEHExceptStmt : public Stmt {
1788  SourceLocation  Loc;
1789  Stmt           *Children[2];
1790
1791  enum { FILTER_EXPR, BLOCK };
1792
1793  SEHExceptStmt(SourceLocation Loc,
1794                Expr *FilterExpr,
1795                Stmt *Block);
1796
1797  friend class ASTReader;
1798  friend class ASTStmtReader;
1799  explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1800
1801public:
1802  static SEHExceptStmt* Create(ASTContext &C,
1803                               SourceLocation ExceptLoc,
1804                               Expr *FilterExpr,
1805                               Stmt *Block);
1806
1807  SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1808  SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1809
1810  SourceLocation getExceptLoc() const { return Loc; }
1811  SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1812
1813  Expr *getFilterExpr() const {
1814    return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1815  }
1816
1817  CompoundStmt *getBlock() const {
1818    return cast<CompoundStmt>(Children[BLOCK]);
1819  }
1820
1821  child_range children() {
1822    return child_range(Children,Children+2);
1823  }
1824
1825  static bool classof(const Stmt *T) {
1826    return T->getStmtClass() == SEHExceptStmtClass;
1827  }
1828
1829};
1830
1831class SEHFinallyStmt : public Stmt {
1832  SourceLocation  Loc;
1833  Stmt           *Block;
1834
1835  SEHFinallyStmt(SourceLocation Loc,
1836                 Stmt *Block);
1837
1838  friend class ASTReader;
1839  friend class ASTStmtReader;
1840  explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1841
1842public:
1843  static SEHFinallyStmt* Create(ASTContext &C,
1844                                SourceLocation FinallyLoc,
1845                                Stmt *Block);
1846
1847  SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1848  SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1849
1850  SourceLocation getFinallyLoc() const { return Loc; }
1851  SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1852
1853  CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1854
1855  child_range children() {
1856    return child_range(&Block,&Block+1);
1857  }
1858
1859  static bool classof(const Stmt *T) {
1860    return T->getStmtClass() == SEHFinallyStmtClass;
1861  }
1862
1863};
1864
1865class SEHTryStmt : public Stmt {
1866  bool            IsCXXTry;
1867  SourceLocation  TryLoc;
1868  Stmt           *Children[2];
1869
1870  enum { TRY = 0, HANDLER = 1 };
1871
1872  SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1873             SourceLocation TryLoc,
1874             Stmt *TryBlock,
1875             Stmt *Handler);
1876
1877  friend class ASTReader;
1878  friend class ASTStmtReader;
1879  explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1880
1881public:
1882  static SEHTryStmt* Create(ASTContext &C,
1883                            bool isCXXTry,
1884                            SourceLocation TryLoc,
1885                            Stmt *TryBlock,
1886                            Stmt *Handler);
1887
1888  SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1889  SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1890
1891  SourceLocation getTryLoc() const { return TryLoc; }
1892  SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1893
1894  bool getIsCXXTry() const { return IsCXXTry; }
1895
1896  CompoundStmt* getTryBlock() const {
1897    return cast<CompoundStmt>(Children[TRY]);
1898  }
1899
1900  Stmt *getHandler() const { return Children[HANDLER]; }
1901
1902  /// Returns 0 if not defined
1903  SEHExceptStmt  *getExceptHandler() const;
1904  SEHFinallyStmt *getFinallyHandler() const;
1905
1906  child_range children() {
1907    return child_range(Children,Children+2);
1908  }
1909
1910  static bool classof(const Stmt *T) {
1911    return T->getStmtClass() == SEHTryStmtClass;
1912  }
1913};
1914
1915/// \brief This captures a statement into a function. For example, the following
1916/// pragma annotated compound statement can be represented as a CapturedStmt,
1917/// and this compound statement is the body of an anonymous outlined function.
1918/// @code
1919/// #pragma omp parallel
1920/// {
1921///   compute();
1922/// }
1923/// @endcode
1924class CapturedStmt : public Stmt {
1925public:
1926  /// \brief The different capture forms: by 'this' or by reference, etc.
1927  enum VariableCaptureKind {
1928    VCK_This,
1929    VCK_ByRef
1930  };
1931
1932  /// \brief Describes the capture of either a variable or 'this'.
1933  class Capture {
1934    llvm::PointerIntPair<VarDecl *, 1, VariableCaptureKind> VarAndKind;
1935    SourceLocation Loc;
1936
1937  public:
1938    /// \brief Create a new capture.
1939    ///
1940    /// \param Loc The source location associated with this capture.
1941    ///
1942    /// \param Kind The kind of capture (this, ByRef, ...).
1943    ///
1944    /// \param Var The variable being captured, or null if capturing this.
1945    ///
1946    Capture(SourceLocation Loc, VariableCaptureKind Kind, VarDecl *Var = 0)
1947      : VarAndKind(Var, Kind), Loc(Loc) {
1948      switch (Kind) {
1949      case VCK_This:
1950        assert(Var == 0 && "'this' capture cannot have a variable!");
1951        break;
1952      case VCK_ByRef:
1953        assert(Var && "capturing by reference must have a variable!");
1954        break;
1955      }
1956    }
1957
1958    /// \brief Determine the kind of capture.
1959    VariableCaptureKind getCaptureKind() const { return VarAndKind.getInt(); }
1960
1961    /// \brief Retrieve the source location at which the variable or 'this' was
1962    /// first used.
1963    SourceLocation getLocation() const { return Loc; }
1964
1965    /// \brief Determine whether this capture handles the C++ 'this' pointer.
1966    bool capturesThis() const { return getCaptureKind() == VCK_This; }
1967
1968    /// \brief Determine whether this capture handles a variable.
1969    bool capturesVariable() const { return getCaptureKind() != VCK_This; }
1970
1971    /// \brief Retrieve the declaration of the variable being captured.
1972    ///
1973    /// This operation is only valid if this capture does not capture 'this'.
1974    VarDecl *getCapturedVar() const {
1975      assert(!capturesThis() && "No variable available for 'this' capture");
1976      return VarAndKind.getPointer();
1977    }
1978  };
1979
1980private:
1981  /// \brief The number of variable captured, including 'this'.
1982  unsigned NumCaptures;
1983
1984  /// \brief The implicit outlined function.
1985  CapturedDecl *TheCapturedDecl;
1986
1987  /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl.
1988  RecordDecl *TheRecordDecl;
1989
1990  /// \brief Construct a captured statement.
1991  CapturedStmt(Stmt *S, ArrayRef<Capture> Captures,
1992               ArrayRef<Expr *> CaptureInits,
1993               CapturedDecl *CD, RecordDecl *RD);
1994
1995  /// \brief Construct an empty captured statement.
1996  CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
1997
1998  Stmt **getStoredStmts() const {
1999    return reinterpret_cast<Stmt **>(const_cast<CapturedStmt *>(this) + 1);
2000  }
2001
2002  Capture *getStoredCaptures() const;
2003
2004public:
2005  static CapturedStmt *Create(ASTContext &Context, Stmt *S,
2006                              ArrayRef<Capture> Captures,
2007                              ArrayRef<Expr *> CaptureInits,
2008                              CapturedDecl *CD, RecordDecl *RD);
2009
2010  static CapturedStmt *CreateDeserialized(ASTContext &Context,
2011                                          unsigned NumCaptures);
2012
2013  /// \brief Retrieve the statement being captured.
2014  Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2015  const Stmt *getCapturedStmt() const {
2016    return const_cast<CapturedStmt *>(this)->getCapturedStmt();
2017  }
2018
2019  /// \brief Retrieve the outlined function declaration.
2020  CapturedDecl *getCapturedDecl() const { return TheCapturedDecl; }
2021
2022  /// \brief Retrieve the record declaration for captured variables.
2023  const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2024
2025  /// \brief True if this variable has been captured.
2026  bool capturesVariable(const VarDecl *Var) const;
2027
2028  /// \brief An iterator that walks over the captures.
2029  typedef const Capture *capture_iterator;
2030
2031  /// \brief Retrieve an iterator pointing to the first capture.
2032  capture_iterator capture_begin() const { return getStoredCaptures(); }
2033
2034  /// \brief Retrieve an iterator pointing past the end of the sequence of
2035  /// captures.
2036  capture_iterator capture_end() const {
2037    return getStoredCaptures() + NumCaptures;
2038  }
2039
2040  /// \brief Retrieve the number of captures, including 'this'.
2041  unsigned capture_size() const { return NumCaptures; }
2042
2043  /// \brief Iterator that walks over the capture initialization arguments.
2044  typedef Expr **capture_init_iterator;
2045
2046  /// \brief Retrieve the first initialization argument.
2047  capture_init_iterator capture_init_begin() const {
2048    return reinterpret_cast<Expr **>(getStoredStmts());
2049  }
2050
2051  /// \brief Retrieve the iterator pointing one past the last initialization
2052  /// argument.
2053  capture_init_iterator capture_init_end() const {
2054    return capture_init_begin() + NumCaptures;
2055  }
2056
2057  SourceLocation getLocStart() const LLVM_READONLY {
2058    return getCapturedStmt()->getLocStart();
2059  }
2060  SourceLocation getLocEnd() const LLVM_READONLY {
2061    return getCapturedStmt()->getLocEnd();
2062  }
2063  SourceRange getSourceRange() const LLVM_READONLY {
2064    return getCapturedStmt()->getSourceRange();
2065  }
2066
2067  static bool classof(const Stmt *T) {
2068    return T->getStmtClass() == CapturedStmtClass;
2069  }
2070
2071  child_range children();
2072};
2073
2074}  // end namespace clang
2075
2076#endif
2077