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