Home | History | Annotate | Download | only in AST
      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/Basic/LLVM.h"
     18 #include "clang/Basic/SourceLocation.h"
     19 #include "clang/AST/PrettyPrinter.h"
     20 #include "clang/AST/StmtIterator.h"
     21 #include "clang/AST/DeclGroup.h"
     22 #include "clang/AST/ASTContext.h"
     23 #include "llvm/Support/raw_ostream.h"
     24 #include "llvm/ADT/SmallVector.h"
     25 #include <string>
     26 
     27 namespace llvm {
     28   class FoldingSetNodeID;
     29 }
     30 
     31 namespace clang {
     32   class ASTContext;
     33   class Expr;
     34   class Decl;
     35   class ParmVarDecl;
     36   class QualType;
     37   class IdentifierInfo;
     38   class SourceManager;
     39   class StringLiteral;
     40   class SwitchStmt;
     41 
     42   //===----------------------------------------------------------------------===//
     43   // ExprIterator - Iterators for iterating over Stmt* arrays that contain
     44   //  only Expr*.  This is needed because AST nodes use Stmt* arrays to store
     45   //  references to children (to be compatible with StmtIterator).
     46   //===----------------------------------------------------------------------===//
     47 
     48   class Stmt;
     49   class Expr;
     50 
     51   class ExprIterator {
     52     Stmt** I;
     53   public:
     54     ExprIterator(Stmt** i) : I(i) {}
     55     ExprIterator() : I(0) {}
     56     ExprIterator& operator++() { ++I; return *this; }
     57     ExprIterator operator-(size_t i) { return I-i; }
     58     ExprIterator operator+(size_t i) { return I+i; }
     59     Expr* operator[](size_t idx);
     60     // FIXME: Verify that this will correctly return a signed distance.
     61     signed operator-(const ExprIterator& R) const { return I - R.I; }
     62     Expr* operator*() const;
     63     Expr* operator->() const;
     64     bool operator==(const ExprIterator& R) const { return I == R.I; }
     65     bool operator!=(const ExprIterator& R) const { return I != R.I; }
     66     bool operator>(const ExprIterator& R) const { return I > R.I; }
     67     bool operator>=(const ExprIterator& R) const { return I >= R.I; }
     68   };
     69 
     70   class ConstExprIterator {
     71     const Stmt * const *I;
     72   public:
     73     ConstExprIterator(const Stmt * const *i) : I(i) {}
     74     ConstExprIterator() : I(0) {}
     75     ConstExprIterator& operator++() { ++I; return *this; }
     76     ConstExprIterator operator+(size_t i) const { return I+i; }
     77     ConstExprIterator operator-(size_t i) const { return I-i; }
     78     const Expr * operator[](size_t idx) const;
     79     signed operator-(const ConstExprIterator& R) const { return I - R.I; }
     80     const Expr * operator*() const;
     81     const Expr * operator->() const;
     82     bool operator==(const ConstExprIterator& R) const { return I == R.I; }
     83     bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
     84     bool operator>(const ConstExprIterator& R) const { return I > R.I; }
     85     bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
     86   };
     87 
     88 //===----------------------------------------------------------------------===//
     89 // AST classes for statements.
     90 //===----------------------------------------------------------------------===//
     91 
     92 /// Stmt - This represents one statement.
     93 ///
     94 class Stmt {
     95 public:
     96   enum StmtClass {
     97     NoStmtClass = 0,
     98 #define STMT(CLASS, PARENT) CLASS##Class,
     99 #define STMT_RANGE(BASE, FIRST, LAST) \
    100         first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
    101 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
    102         first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
    103 #define ABSTRACT_STMT(STMT)
    104 #include "clang/AST/StmtNodes.inc"
    105   };
    106 
    107   // Make vanilla 'new' and 'delete' illegal for Stmts.
    108 protected:
    109   void* operator new(size_t bytes) throw() {
    110     llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
    111   }
    112   void operator delete(void* data) throw() {
    113     llvm_unreachable("Stmts cannot be released with regular 'delete'.");
    114   }
    115 
    116   class StmtBitfields {
    117     friend class Stmt;
    118 
    119     /// \brief The statement class.
    120     unsigned sClass : 8;
    121   };
    122   enum { NumStmtBits = 8 };
    123 
    124   class CompoundStmtBitfields {
    125     friend class CompoundStmt;
    126     unsigned : NumStmtBits;
    127 
    128     unsigned NumStmts : 32 - NumStmtBits;
    129   };
    130 
    131   class ExprBitfields {
    132     friend class Expr;
    133     friend class DeclRefExpr; // computeDependence
    134     friend class InitListExpr; // ctor
    135     friend class DesignatedInitExpr; // ctor
    136     friend class BlockDeclRefExpr; // ctor
    137     friend class ASTStmtReader; // deserialization
    138     friend class CXXNewExpr; // ctor
    139     friend class DependentScopeDeclRefExpr; // ctor
    140     friend class CXXConstructExpr; // ctor
    141     friend class CallExpr; // ctor
    142     friend class OffsetOfExpr; // ctor
    143     friend class ObjCMessageExpr; // ctor
    144     friend class ShuffleVectorExpr; // ctor
    145     friend class ParenListExpr; // ctor
    146     friend class CXXUnresolvedConstructExpr; // ctor
    147     friend class CXXDependentScopeMemberExpr; // ctor
    148     friend class OverloadExpr; // ctor
    149     friend class AtomicExpr; // ctor
    150     unsigned : NumStmtBits;
    151 
    152     unsigned ValueKind : 2;
    153     unsigned ObjectKind : 2;
    154     unsigned TypeDependent : 1;
    155     unsigned ValueDependent : 1;
    156     unsigned InstantiationDependent : 1;
    157     unsigned ContainsUnexpandedParameterPack : 1;
    158   };
    159   enum { NumExprBits = 16 };
    160 
    161   class DeclRefExprBitfields {
    162     friend class DeclRefExpr;
    163     friend class ASTStmtReader; // deserialization
    164     unsigned : NumExprBits;
    165 
    166     unsigned HasQualifier : 1;
    167     unsigned HasExplicitTemplateArgs : 1;
    168     unsigned HasFoundDecl : 1;
    169     unsigned HadMultipleCandidates : 1;
    170   };
    171 
    172   class CastExprBitfields {
    173     friend class CastExpr;
    174     unsigned : NumExprBits;
    175 
    176     unsigned Kind : 6;
    177     unsigned BasePathSize : 32 - 6 - NumExprBits;
    178   };
    179 
    180   class CallExprBitfields {
    181     friend class CallExpr;
    182     unsigned : NumExprBits;
    183 
    184     unsigned NumPreArgs : 1;
    185   };
    186 
    187   class ObjCIndirectCopyRestoreExprBitfields {
    188     friend class ObjCIndirectCopyRestoreExpr;
    189     unsigned : NumExprBits;
    190 
    191     unsigned ShouldCopy : 1;
    192   };
    193 
    194   union {
    195     // FIXME: this is wasteful on 64-bit platforms.
    196     void *Aligner;
    197 
    198     StmtBitfields StmtBits;
    199     CompoundStmtBitfields CompoundStmtBits;
    200     ExprBitfields ExprBits;
    201     DeclRefExprBitfields DeclRefExprBits;
    202     CastExprBitfields CastExprBits;
    203     CallExprBitfields CallExprBits;
    204     ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
    205   };
    206 
    207   friend class ASTStmtReader;
    208 
    209 public:
    210   // Only allow allocation of Stmts using the allocator in ASTContext
    211   // or by doing a placement new.
    212   void* operator new(size_t bytes, ASTContext& C,
    213                      unsigned alignment = 8) throw() {
    214     return ::operator new(bytes, C, alignment);
    215   }
    216 
    217   void* operator new(size_t bytes, ASTContext* C,
    218                      unsigned alignment = 8) throw() {
    219     return ::operator new(bytes, *C, alignment);
    220   }
    221 
    222   void* operator new(size_t bytes, void* mem) throw() {
    223     return mem;
    224   }
    225 
    226   void operator delete(void*, ASTContext&, unsigned) throw() { }
    227   void operator delete(void*, ASTContext*, unsigned) throw() { }
    228   void operator delete(void*, std::size_t) throw() { }
    229   void operator delete(void*, void*) throw() { }
    230 
    231 public:
    232   /// \brief A placeholder type used to construct an empty shell of a
    233   /// type, that will be filled in later (e.g., by some
    234   /// de-serialization).
    235   struct EmptyShell { };
    236 
    237 protected:
    238   /// \brief Construct an empty statement.
    239   explicit Stmt(StmtClass SC, EmptyShell) {
    240     StmtBits.sClass = SC;
    241     if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
    242   }
    243 
    244 public:
    245   Stmt(StmtClass SC) {
    246     StmtBits.sClass = SC;
    247     if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
    248   }
    249 
    250   StmtClass getStmtClass() const {
    251     return static_cast<StmtClass>(StmtBits.sClass);
    252   }
    253   const char *getStmtClassName() const;
    254 
    255   /// SourceLocation tokens are not useful in isolation - they are low level
    256   /// value objects created/interpreted by SourceManager. We assume AST
    257   /// clients will have a pointer to the respective SourceManager.
    258   SourceRange getSourceRange() const;
    259 
    260   SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
    261   SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
    262 
    263   // global temp stats (until we have a per-module visitor)
    264   static void addStmtClass(const StmtClass s);
    265   static bool CollectingStats(bool Enable = false);
    266   static void PrintStats();
    267 
    268   /// dump - This does a local dump of the specified AST fragment.  It dumps the
    269   /// specified node and a few nodes underneath it, but not the whole subtree.
    270   /// This is useful in a debugger.
    271   void dump() const;
    272   void dump(SourceManager &SM) const;
    273   void dump(raw_ostream &OS, SourceManager &SM) const;
    274 
    275   /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
    276   void dumpAll() const;
    277   void dumpAll(SourceManager &SM) const;
    278 
    279   /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
    280   /// back to its original source language syntax.
    281   void dumpPretty(ASTContext& Context) const;
    282   void printPretty(raw_ostream &OS, PrinterHelper *Helper,
    283                    const PrintingPolicy &Policy,
    284                    unsigned Indentation = 0) const {
    285     printPretty(OS, *(ASTContext*)0, Helper, Policy, Indentation);
    286   }
    287   void printPretty(raw_ostream &OS, ASTContext &Context,
    288                    PrinterHelper *Helper,
    289                    const PrintingPolicy &Policy,
    290                    unsigned Indentation = 0) const;
    291 
    292   /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
    293   ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
    294   void viewAST() const;
    295 
    296   /// Skip past any implicit AST nodes which might surround this
    297   /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
    298   Stmt *IgnoreImplicit();
    299 
    300   const Stmt *stripLabelLikeStatements() const;
    301   Stmt *stripLabelLikeStatements() {
    302     return const_cast<Stmt*>(
    303       const_cast<const Stmt*>(this)->stripLabelLikeStatements());
    304   }
    305 
    306   // Implement isa<T> support.
    307   static bool classof(const Stmt *) { return true; }
    308 
    309   /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
    310   ///  contain implicit control-flow in the order their subexpressions
    311   ///  are evaluated.  This predicate returns true if this statement has
    312   ///  such implicit control-flow.  Such statements are also specially handled
    313   ///  within CFGs.
    314   bool hasImplicitControlFlow() const;
    315 
    316   /// Child Iterators: All subclasses must implement 'children'
    317   /// to permit easy iteration over the substatements/subexpessions of an
    318   /// AST node.  This permits easy iteration over all nodes in the AST.
    319   typedef StmtIterator       child_iterator;
    320   typedef ConstStmtIterator  const_child_iterator;
    321 
    322   typedef StmtRange          child_range;
    323   typedef ConstStmtRange     const_child_range;
    324 
    325   child_range children();
    326   const_child_range children() const {
    327     return const_cast<Stmt*>(this)->children();
    328   }
    329 
    330   child_iterator child_begin() { return children().first; }
    331   child_iterator child_end() { return children().second; }
    332 
    333   const_child_iterator child_begin() const { return children().first; }
    334   const_child_iterator child_end() const { return children().second; }
    335 
    336   /// \brief Produce a unique representation of the given statement.
    337   ///
    338   /// \brief ID once the profiling operation is complete, will contain
    339   /// the unique representation of the given statement.
    340   ///
    341   /// \brief Context the AST context in which the statement resides
    342   ///
    343   /// \brief Canonical whether the profile should be based on the canonical
    344   /// representation of this statement (e.g., where non-type template
    345   /// parameters are identified by index/level rather than their
    346   /// declaration pointers) or the exact representation of the statement as
    347   /// written in the source.
    348   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
    349                bool Canonical) const;
    350 };
    351 
    352 /// DeclStmt - Adaptor class for mixing declarations with statements and
    353 /// expressions. For example, CompoundStmt mixes statements, expressions
    354 /// and declarations (variables, types). Another example is ForStmt, where
    355 /// the first statement can be an expression or a declaration.
    356 ///
    357 class DeclStmt : public Stmt {
    358   DeclGroupRef DG;
    359   SourceLocation StartLoc, EndLoc;
    360 
    361 public:
    362   DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
    363            SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
    364                                     StartLoc(startLoc), EndLoc(endLoc) {}
    365 
    366   /// \brief Build an empty declaration statement.
    367   explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
    368 
    369   /// isSingleDecl - This method returns true if this DeclStmt refers
    370   /// to a single Decl.
    371   bool isSingleDecl() const {
    372     return DG.isSingleDecl();
    373   }
    374 
    375   const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
    376   Decl *getSingleDecl() { return DG.getSingleDecl(); }
    377 
    378   const DeclGroupRef getDeclGroup() const { return DG; }
    379   DeclGroupRef getDeclGroup() { return DG; }
    380   void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
    381 
    382   SourceLocation getStartLoc() const { return StartLoc; }
    383   void setStartLoc(SourceLocation L) { StartLoc = L; }
    384   SourceLocation getEndLoc() const { return EndLoc; }
    385   void setEndLoc(SourceLocation L) { EndLoc = L; }
    386 
    387   SourceRange getSourceRange() const {
    388     return SourceRange(StartLoc, EndLoc);
    389   }
    390 
    391   static bool classof(const Stmt *T) {
    392     return T->getStmtClass() == DeclStmtClass;
    393   }
    394   static bool classof(const DeclStmt *) { return true; }
    395 
    396   // Iterators over subexpressions.
    397   child_range children() {
    398     return child_range(child_iterator(DG.begin(), DG.end()),
    399                        child_iterator(DG.end(), DG.end()));
    400   }
    401 
    402   typedef DeclGroupRef::iterator decl_iterator;
    403   typedef DeclGroupRef::const_iterator const_decl_iterator;
    404 
    405   decl_iterator decl_begin() { return DG.begin(); }
    406   decl_iterator decl_end() { return DG.end(); }
    407   const_decl_iterator decl_begin() const { return DG.begin(); }
    408   const_decl_iterator decl_end() const { return DG.end(); }
    409 };
    410 
    411 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
    412 ///
    413 class NullStmt : public Stmt {
    414   SourceLocation SemiLoc;
    415 
    416   /// \brief True if the null statement was preceded by an empty macro, e.g:
    417   /// @code
    418   ///   #define CALL(x)
    419   ///   CALL(0);
    420   /// @endcode
    421   bool HasLeadingEmptyMacro;
    422 public:
    423   NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
    424     : Stmt(NullStmtClass), SemiLoc(L),
    425       HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
    426 
    427   /// \brief Build an empty null statement.
    428   explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
    429       HasLeadingEmptyMacro(false) { }
    430 
    431   SourceLocation getSemiLoc() const { return SemiLoc; }
    432   void setSemiLoc(SourceLocation L) { SemiLoc = L; }
    433 
    434   bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
    435 
    436   SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
    437 
    438   static bool classof(const Stmt *T) {
    439     return T->getStmtClass() == NullStmtClass;
    440   }
    441   static bool classof(const NullStmt *) { return true; }
    442 
    443   child_range children() { return child_range(); }
    444 
    445   friend class ASTStmtReader;
    446   friend class ASTStmtWriter;
    447 };
    448 
    449 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
    450 ///
    451 class CompoundStmt : public Stmt {
    452   Stmt** Body;
    453   SourceLocation LBracLoc, RBracLoc;
    454 public:
    455   CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned NumStmts,
    456                SourceLocation LB, SourceLocation RB)
    457   : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
    458     CompoundStmtBits.NumStmts = NumStmts;
    459     assert(CompoundStmtBits.NumStmts == NumStmts &&
    460            "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
    461 
    462     if (NumStmts == 0) {
    463       Body = 0;
    464       return;
    465     }
    466 
    467     Body = new (C) Stmt*[NumStmts];
    468     memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
    469   }
    470 
    471   // \brief Build an empty compound statement.
    472   explicit CompoundStmt(EmptyShell Empty)
    473     : Stmt(CompoundStmtClass, Empty), Body(0) {
    474     CompoundStmtBits.NumStmts = 0;
    475   }
    476 
    477   void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
    478 
    479   bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
    480   unsigned size() const { return CompoundStmtBits.NumStmts; }
    481 
    482   typedef Stmt** body_iterator;
    483   body_iterator body_begin() { return Body; }
    484   body_iterator body_end() { return Body + size(); }
    485   Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
    486 
    487   void setLastStmt(Stmt *S) {
    488     assert(!body_empty() && "setLastStmt");
    489     Body[size()-1] = S;
    490   }
    491 
    492   typedef Stmt* const * const_body_iterator;
    493   const_body_iterator body_begin() const { return Body; }
    494   const_body_iterator body_end() const { return Body + size(); }
    495   const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
    496 
    497   typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
    498   reverse_body_iterator body_rbegin() {
    499     return reverse_body_iterator(body_end());
    500   }
    501   reverse_body_iterator body_rend() {
    502     return reverse_body_iterator(body_begin());
    503   }
    504 
    505   typedef std::reverse_iterator<const_body_iterator>
    506           const_reverse_body_iterator;
    507 
    508   const_reverse_body_iterator body_rbegin() const {
    509     return const_reverse_body_iterator(body_end());
    510   }
    511 
    512   const_reverse_body_iterator body_rend() const {
    513     return const_reverse_body_iterator(body_begin());
    514   }
    515 
    516   SourceRange getSourceRange() const {
    517     return SourceRange(LBracLoc, RBracLoc);
    518   }
    519 
    520   SourceLocation getLBracLoc() const { return LBracLoc; }
    521   void setLBracLoc(SourceLocation L) { LBracLoc = L; }
    522   SourceLocation getRBracLoc() const { return RBracLoc; }
    523   void setRBracLoc(SourceLocation L) { RBracLoc = L; }
    524 
    525   static bool classof(const Stmt *T) {
    526     return T->getStmtClass() == CompoundStmtClass;
    527   }
    528   static bool classof(const CompoundStmt *) { return true; }
    529 
    530   // Iterators
    531   child_range children() {
    532     return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
    533   }
    534 
    535   const_child_range children() const {
    536     return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
    537   }
    538 };
    539 
    540 // SwitchCase is the base class for CaseStmt and DefaultStmt,
    541 class SwitchCase : public Stmt {
    542 protected:
    543   // A pointer to the following CaseStmt or DefaultStmt class,
    544   // used by SwitchStmt.
    545   SwitchCase *NextSwitchCase;
    546 
    547   SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
    548 
    549 public:
    550   const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
    551 
    552   SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
    553 
    554   void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
    555 
    556   Stmt *getSubStmt();
    557   const Stmt *getSubStmt() const {
    558     return const_cast<SwitchCase*>(this)->getSubStmt();
    559   }
    560 
    561   SourceRange getSourceRange() const { return SourceRange(); }
    562 
    563   static bool classof(const Stmt *T) {
    564     return T->getStmtClass() == CaseStmtClass ||
    565            T->getStmtClass() == DefaultStmtClass;
    566   }
    567   static bool classof(const SwitchCase *) { return true; }
    568 };
    569 
    570 class CaseStmt : public SwitchCase {
    571   enum { LHS, RHS, SUBSTMT, END_EXPR };
    572   Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
    573                              // GNU "case 1 ... 4" extension
    574   SourceLocation CaseLoc;
    575   SourceLocation EllipsisLoc;
    576   SourceLocation ColonLoc;
    577 public:
    578   CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
    579            SourceLocation ellipsisLoc, SourceLocation colonLoc)
    580     : SwitchCase(CaseStmtClass) {
    581     SubExprs[SUBSTMT] = 0;
    582     SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
    583     SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
    584     CaseLoc = caseLoc;
    585     EllipsisLoc = ellipsisLoc;
    586     ColonLoc = colonLoc;
    587   }
    588 
    589   /// \brief Build an empty switch case statement.
    590   explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
    591 
    592   SourceLocation getCaseLoc() const { return CaseLoc; }
    593   void setCaseLoc(SourceLocation L) { CaseLoc = L; }
    594   SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
    595   void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
    596   SourceLocation getColonLoc() const { return ColonLoc; }
    597   void setColonLoc(SourceLocation L) { ColonLoc = L; }
    598 
    599   Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
    600   Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
    601   Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
    602 
    603   const Expr *getLHS() const {
    604     return reinterpret_cast<const Expr*>(SubExprs[LHS]);
    605   }
    606   const Expr *getRHS() const {
    607     return reinterpret_cast<const Expr*>(SubExprs[RHS]);
    608   }
    609   const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
    610 
    611   void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
    612   void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
    613   void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
    614 
    615 
    616   SourceRange getSourceRange() const {
    617     // Handle deeply nested case statements with iteration instead of recursion.
    618     const CaseStmt *CS = this;
    619     while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
    620       CS = CS2;
    621 
    622     return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
    623   }
    624   static bool classof(const Stmt *T) {
    625     return T->getStmtClass() == CaseStmtClass;
    626   }
    627   static bool classof(const CaseStmt *) { return true; }
    628 
    629   // Iterators
    630   child_range children() {
    631     return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
    632   }
    633 };
    634 
    635 class DefaultStmt : public SwitchCase {
    636   Stmt* SubStmt;
    637   SourceLocation DefaultLoc;
    638   SourceLocation ColonLoc;
    639 public:
    640   DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
    641     SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
    642     ColonLoc(CL) {}
    643 
    644   /// \brief Build an empty default statement.
    645   explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
    646 
    647   Stmt *getSubStmt() { return SubStmt; }
    648   const Stmt *getSubStmt() const { return SubStmt; }
    649   void setSubStmt(Stmt *S) { SubStmt = S; }
    650 
    651   SourceLocation getDefaultLoc() const { return DefaultLoc; }
    652   void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
    653   SourceLocation getColonLoc() const { return ColonLoc; }
    654   void setColonLoc(SourceLocation L) { ColonLoc = L; }
    655 
    656   SourceRange getSourceRange() const {
    657     return SourceRange(DefaultLoc, SubStmt->getLocEnd());
    658   }
    659   static bool classof(const Stmt *T) {
    660     return T->getStmtClass() == DefaultStmtClass;
    661   }
    662   static bool classof(const DefaultStmt *) { return true; }
    663 
    664   // Iterators
    665   child_range children() { return child_range(&SubStmt, &SubStmt+1); }
    666 };
    667 
    668 
    669 /// LabelStmt - Represents a label, which has a substatement.  For example:
    670 ///    foo: return;
    671 ///
    672 class LabelStmt : public Stmt {
    673   LabelDecl *TheDecl;
    674   Stmt *SubStmt;
    675   SourceLocation IdentLoc;
    676 public:
    677   LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
    678     : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
    679   }
    680 
    681   // \brief Build an empty label statement.
    682   explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
    683 
    684   SourceLocation getIdentLoc() const { return IdentLoc; }
    685   LabelDecl *getDecl() const { return TheDecl; }
    686   void setDecl(LabelDecl *D) { TheDecl = D; }
    687   const char *getName() const;
    688   Stmt *getSubStmt() { return SubStmt; }
    689   const Stmt *getSubStmt() const { return SubStmt; }
    690   void setIdentLoc(SourceLocation L) { IdentLoc = L; }
    691   void setSubStmt(Stmt *SS) { SubStmt = SS; }
    692 
    693   SourceRange getSourceRange() const {
    694     return SourceRange(IdentLoc, SubStmt->getLocEnd());
    695   }
    696   child_range children() { return child_range(&SubStmt, &SubStmt+1); }
    697 
    698   static bool classof(const Stmt *T) {
    699     return T->getStmtClass() == LabelStmtClass;
    700   }
    701   static bool classof(const LabelStmt *) { return true; }
    702 };
    703 
    704 
    705 /// IfStmt - This represents an if/then/else.
    706 ///
    707 class IfStmt : public Stmt {
    708   enum { VAR, COND, THEN, ELSE, END_EXPR };
    709   Stmt* SubExprs[END_EXPR];
    710 
    711   SourceLocation IfLoc;
    712   SourceLocation ElseLoc;
    713 
    714 public:
    715   IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
    716          Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
    717 
    718   /// \brief Build an empty if/then/else statement
    719   explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
    720 
    721   /// \brief Retrieve the variable declared in this "if" statement, if any.
    722   ///
    723   /// In the following example, "x" is the condition variable.
    724   /// \code
    725   /// if (int x = foo()) {
    726   ///   printf("x is %d", x);
    727   /// }
    728   /// \endcode
    729   VarDecl *getConditionVariable() const;
    730   void setConditionVariable(ASTContext &C, VarDecl *V);
    731 
    732   /// If this IfStmt has a condition variable, return the faux DeclStmt
    733   /// associated with the creation of that condition variable.
    734   const DeclStmt *getConditionVariableDeclStmt() const {
    735     return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
    736   }
    737 
    738   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
    739   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
    740   const Stmt *getThen() const { return SubExprs[THEN]; }
    741   void setThen(Stmt *S) { SubExprs[THEN] = S; }
    742   const Stmt *getElse() const { return SubExprs[ELSE]; }
    743   void setElse(Stmt *S) { SubExprs[ELSE] = S; }
    744 
    745   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
    746   Stmt *getThen() { return SubExprs[THEN]; }
    747   Stmt *getElse() { return SubExprs[ELSE]; }
    748 
    749   SourceLocation getIfLoc() const { return IfLoc; }
    750   void setIfLoc(SourceLocation L) { IfLoc = L; }
    751   SourceLocation getElseLoc() const { return ElseLoc; }
    752   void setElseLoc(SourceLocation L) { ElseLoc = L; }
    753 
    754   SourceRange getSourceRange() const {
    755     if (SubExprs[ELSE])
    756       return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
    757     else
    758       return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
    759   }
    760 
    761   // Iterators over subexpressions.  The iterators will include iterating
    762   // over the initialization expression referenced by the condition variable.
    763   child_range children() {
    764     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
    765   }
    766 
    767   static bool classof(const Stmt *T) {
    768     return T->getStmtClass() == IfStmtClass;
    769   }
    770   static bool classof(const IfStmt *) { return true; }
    771 };
    772 
    773 /// SwitchStmt - This represents a 'switch' stmt.
    774 ///
    775 class SwitchStmt : public Stmt {
    776   enum { VAR, COND, BODY, END_EXPR };
    777   Stmt* SubExprs[END_EXPR];
    778   // This points to a linked list of case and default statements.
    779   SwitchCase *FirstCase;
    780   SourceLocation SwitchLoc;
    781 
    782   /// If the SwitchStmt is a switch on an enum value, this records whether
    783   /// all the enum values were covered by CaseStmts.  This value is meant to
    784   /// be a hint for possible clients.
    785   unsigned AllEnumCasesCovered : 1;
    786 
    787 public:
    788   SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
    789 
    790   /// \brief Build a empty switch statement.
    791   explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
    792 
    793   /// \brief Retrieve the variable declared in this "switch" statement, if any.
    794   ///
    795   /// In the following example, "x" is the condition variable.
    796   /// \code
    797   /// switch (int x = foo()) {
    798   ///   case 0: break;
    799   ///   // ...
    800   /// }
    801   /// \endcode
    802   VarDecl *getConditionVariable() const;
    803   void setConditionVariable(ASTContext &C, VarDecl *V);
    804 
    805   /// If this SwitchStmt has a condition variable, return the faux DeclStmt
    806   /// associated with the creation of that condition variable.
    807   const DeclStmt *getConditionVariableDeclStmt() const {
    808     return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
    809   }
    810 
    811   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
    812   const Stmt *getBody() const { return SubExprs[BODY]; }
    813   const SwitchCase *getSwitchCaseList() const { return FirstCase; }
    814 
    815   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
    816   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
    817   Stmt *getBody() { return SubExprs[BODY]; }
    818   void setBody(Stmt *S) { SubExprs[BODY] = S; }
    819   SwitchCase *getSwitchCaseList() { return FirstCase; }
    820 
    821   /// \brief Set the case list for this switch statement.
    822   ///
    823   /// The caller is responsible for incrementing the retain counts on
    824   /// all of the SwitchCase statements in this list.
    825   void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
    826 
    827   SourceLocation getSwitchLoc() const { return SwitchLoc; }
    828   void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
    829 
    830   void setBody(Stmt *S, SourceLocation SL) {
    831     SubExprs[BODY] = S;
    832     SwitchLoc = SL;
    833   }
    834   void addSwitchCase(SwitchCase *SC) {
    835     assert(!SC->getNextSwitchCase() && "case/default already added to a switch");
    836     SC->setNextSwitchCase(FirstCase);
    837     FirstCase = SC;
    838   }
    839 
    840   /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
    841   /// switch over an enum value then all cases have been explicitly covered.
    842   void setAllEnumCasesCovered() {
    843     AllEnumCasesCovered = 1;
    844   }
    845 
    846   /// Returns true if the SwitchStmt is a switch of an enum value and all cases
    847   /// have been explicitly covered.
    848   bool isAllEnumCasesCovered() const {
    849     return (bool) AllEnumCasesCovered;
    850   }
    851 
    852   SourceRange getSourceRange() const {
    853     return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
    854   }
    855   // Iterators
    856   child_range children() {
    857     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
    858   }
    859 
    860   static bool classof(const Stmt *T) {
    861     return T->getStmtClass() == SwitchStmtClass;
    862   }
    863   static bool classof(const SwitchStmt *) { return true; }
    864 };
    865 
    866 
    867 /// WhileStmt - This represents a 'while' stmt.
    868 ///
    869 class WhileStmt : public Stmt {
    870   enum { VAR, COND, BODY, END_EXPR };
    871   Stmt* SubExprs[END_EXPR];
    872   SourceLocation WhileLoc;
    873 public:
    874   WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
    875             SourceLocation WL);
    876 
    877   /// \brief Build an empty while statement.
    878   explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
    879 
    880   /// \brief Retrieve the variable declared in this "while" statement, if any.
    881   ///
    882   /// In the following example, "x" is the condition variable.
    883   /// \code
    884   /// while (int x = random()) {
    885   ///   // ...
    886   /// }
    887   /// \endcode
    888   VarDecl *getConditionVariable() const;
    889   void setConditionVariable(ASTContext &C, VarDecl *V);
    890 
    891   /// If this WhileStmt has a condition variable, return the faux DeclStmt
    892   /// associated with the creation of that condition variable.
    893   const DeclStmt *getConditionVariableDeclStmt() const {
    894     return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
    895   }
    896 
    897   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
    898   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
    899   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
    900   Stmt *getBody() { return SubExprs[BODY]; }
    901   const Stmt *getBody() const { return SubExprs[BODY]; }
    902   void setBody(Stmt *S) { SubExprs[BODY] = S; }
    903 
    904   SourceLocation getWhileLoc() const { return WhileLoc; }
    905   void setWhileLoc(SourceLocation L) { WhileLoc = L; }
    906 
    907   SourceRange getSourceRange() const {
    908     return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
    909   }
    910   static bool classof(const Stmt *T) {
    911     return T->getStmtClass() == WhileStmtClass;
    912   }
    913   static bool classof(const WhileStmt *) { return true; }
    914 
    915   // Iterators
    916   child_range children() {
    917     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
    918   }
    919 };
    920 
    921 /// DoStmt - This represents a 'do/while' stmt.
    922 ///
    923 class DoStmt : public Stmt {
    924   enum { BODY, COND, END_EXPR };
    925   Stmt* SubExprs[END_EXPR];
    926   SourceLocation DoLoc;
    927   SourceLocation WhileLoc;
    928   SourceLocation RParenLoc;  // Location of final ')' in do stmt condition.
    929 
    930 public:
    931   DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
    932          SourceLocation RP)
    933     : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
    934     SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
    935     SubExprs[BODY] = body;
    936   }
    937 
    938   /// \brief Build an empty do-while statement.
    939   explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
    940 
    941   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
    942   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
    943   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
    944   Stmt *getBody() { return SubExprs[BODY]; }
    945   const Stmt *getBody() const { return SubExprs[BODY]; }
    946   void setBody(Stmt *S) { SubExprs[BODY] = S; }
    947 
    948   SourceLocation getDoLoc() const { return DoLoc; }
    949   void setDoLoc(SourceLocation L) { DoLoc = L; }
    950   SourceLocation getWhileLoc() const { return WhileLoc; }
    951   void setWhileLoc(SourceLocation L) { WhileLoc = L; }
    952 
    953   SourceLocation getRParenLoc() const { return RParenLoc; }
    954   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
    955 
    956   SourceRange getSourceRange() const {
    957     return SourceRange(DoLoc, RParenLoc);
    958   }
    959   static bool classof(const Stmt *T) {
    960     return T->getStmtClass() == DoStmtClass;
    961   }
    962   static bool classof(const DoStmt *) { return true; }
    963 
    964   // Iterators
    965   child_range children() {
    966     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
    967   }
    968 };
    969 
    970 
    971 /// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
    972 /// the init/cond/inc parts of the ForStmt will be null if they were not
    973 /// specified in the source.
    974 ///
    975 class ForStmt : public Stmt {
    976   enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
    977   Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
    978   SourceLocation ForLoc;
    979   SourceLocation LParenLoc, RParenLoc;
    980 
    981 public:
    982   ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
    983           Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
    984 
    985   /// \brief Build an empty for statement.
    986   explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
    987 
    988   Stmt *getInit() { return SubExprs[INIT]; }
    989 
    990   /// \brief Retrieve the variable declared in this "for" statement, if any.
    991   ///
    992   /// In the following example, "y" is the condition variable.
    993   /// \code
    994   /// for (int x = random(); int y = mangle(x); ++x) {
    995   ///   // ...
    996   /// }
    997   /// \endcode
    998   VarDecl *getConditionVariable() const;
    999   void setConditionVariable(ASTContext &C, VarDecl *V);
   1000 
   1001   /// If this ForStmt has a condition variable, return the faux DeclStmt
   1002   /// associated with the creation of that condition variable.
   1003   const DeclStmt *getConditionVariableDeclStmt() const {
   1004     return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
   1005   }
   1006 
   1007   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
   1008   Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
   1009   Stmt *getBody() { return SubExprs[BODY]; }
   1010 
   1011   const Stmt *getInit() const { return SubExprs[INIT]; }
   1012   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
   1013   const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
   1014   const Stmt *getBody() const { return SubExprs[BODY]; }
   1015 
   1016   void setInit(Stmt *S) { SubExprs[INIT] = S; }
   1017   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
   1018   void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
   1019   void setBody(Stmt *S) { SubExprs[BODY] = S; }
   1020 
   1021   SourceLocation getForLoc() const { return ForLoc; }
   1022   void setForLoc(SourceLocation L) { ForLoc = L; }
   1023   SourceLocation getLParenLoc() const { return LParenLoc; }
   1024   void setLParenLoc(SourceLocation L) { LParenLoc = L; }
   1025   SourceLocation getRParenLoc() const { return RParenLoc; }
   1026   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
   1027 
   1028   SourceRange getSourceRange() const {
   1029     return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
   1030   }
   1031   static bool classof(const Stmt *T) {
   1032     return T->getStmtClass() == ForStmtClass;
   1033   }
   1034   static bool classof(const ForStmt *) { return true; }
   1035 
   1036   // Iterators
   1037   child_range children() {
   1038     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
   1039   }
   1040 };
   1041 
   1042 /// GotoStmt - This represents a direct goto.
   1043 ///
   1044 class GotoStmt : public Stmt {
   1045   LabelDecl *Label;
   1046   SourceLocation GotoLoc;
   1047   SourceLocation LabelLoc;
   1048 public:
   1049   GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
   1050     : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
   1051 
   1052   /// \brief Build an empty goto statement.
   1053   explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
   1054 
   1055   LabelDecl *getLabel() const { return Label; }
   1056   void setLabel(LabelDecl *D) { Label = D; }
   1057 
   1058   SourceLocation getGotoLoc() const { return GotoLoc; }
   1059   void setGotoLoc(SourceLocation L) { GotoLoc = L; }
   1060   SourceLocation getLabelLoc() const { return LabelLoc; }
   1061   void setLabelLoc(SourceLocation L) { LabelLoc = L; }
   1062 
   1063   SourceRange getSourceRange() const {
   1064     return SourceRange(GotoLoc, LabelLoc);
   1065   }
   1066   static bool classof(const Stmt *T) {
   1067     return T->getStmtClass() == GotoStmtClass;
   1068   }
   1069   static bool classof(const GotoStmt *) { return true; }
   1070 
   1071   // Iterators
   1072   child_range children() { return child_range(); }
   1073 };
   1074 
   1075 /// IndirectGotoStmt - This represents an indirect goto.
   1076 ///
   1077 class IndirectGotoStmt : public Stmt {
   1078   SourceLocation GotoLoc;
   1079   SourceLocation StarLoc;
   1080   Stmt *Target;
   1081 public:
   1082   IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
   1083                    Expr *target)
   1084     : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
   1085       Target((Stmt*)target) {}
   1086 
   1087   /// \brief Build an empty indirect goto statement.
   1088   explicit IndirectGotoStmt(EmptyShell Empty)
   1089     : Stmt(IndirectGotoStmtClass, Empty) { }
   1090 
   1091   void setGotoLoc(SourceLocation L) { GotoLoc = L; }
   1092   SourceLocation getGotoLoc() const { return GotoLoc; }
   1093   void setStarLoc(SourceLocation L) { StarLoc = L; }
   1094   SourceLocation getStarLoc() const { return StarLoc; }
   1095 
   1096   Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
   1097   const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
   1098   void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
   1099 
   1100   /// getConstantTarget - Returns the fixed target of this indirect
   1101   /// goto, if one exists.
   1102   LabelDecl *getConstantTarget();
   1103   const LabelDecl *getConstantTarget() const {
   1104     return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
   1105   }
   1106 
   1107   SourceRange getSourceRange() const {
   1108     return SourceRange(GotoLoc, Target->getLocEnd());
   1109   }
   1110 
   1111   static bool classof(const Stmt *T) {
   1112     return T->getStmtClass() == IndirectGotoStmtClass;
   1113   }
   1114   static bool classof(const IndirectGotoStmt *) { return true; }
   1115 
   1116   // Iterators
   1117   child_range children() { return child_range(&Target, &Target+1); }
   1118 };
   1119 
   1120 
   1121 /// ContinueStmt - This represents a continue.
   1122 ///
   1123 class ContinueStmt : public Stmt {
   1124   SourceLocation ContinueLoc;
   1125 public:
   1126   ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
   1127 
   1128   /// \brief Build an empty continue statement.
   1129   explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
   1130 
   1131   SourceLocation getContinueLoc() const { return ContinueLoc; }
   1132   void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
   1133 
   1134   SourceRange getSourceRange() const {
   1135     return SourceRange(ContinueLoc);
   1136   }
   1137 
   1138   static bool classof(const Stmt *T) {
   1139     return T->getStmtClass() == ContinueStmtClass;
   1140   }
   1141   static bool classof(const ContinueStmt *) { return true; }
   1142 
   1143   // Iterators
   1144   child_range children() { return child_range(); }
   1145 };
   1146 
   1147 /// BreakStmt - This represents a break.
   1148 ///
   1149 class BreakStmt : public Stmt {
   1150   SourceLocation BreakLoc;
   1151 public:
   1152   BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
   1153 
   1154   /// \brief Build an empty break statement.
   1155   explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
   1156 
   1157   SourceLocation getBreakLoc() const { return BreakLoc; }
   1158   void setBreakLoc(SourceLocation L) { BreakLoc = L; }
   1159 
   1160   SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
   1161 
   1162   static bool classof(const Stmt *T) {
   1163     return T->getStmtClass() == BreakStmtClass;
   1164   }
   1165   static bool classof(const BreakStmt *) { return true; }
   1166 
   1167   // Iterators
   1168   child_range children() { return child_range(); }
   1169 };
   1170 
   1171 
   1172 /// ReturnStmt - This represents a return, optionally of an expression:
   1173 ///   return;
   1174 ///   return 4;
   1175 ///
   1176 /// Note that GCC allows return with no argument in a function declared to
   1177 /// return a value, and it allows returning a value in functions declared to
   1178 /// return void.  We explicitly model this in the AST, which means you can't
   1179 /// depend on the return type of the function and the presence of an argument.
   1180 ///
   1181 class ReturnStmt : public Stmt {
   1182   Stmt *RetExpr;
   1183   SourceLocation RetLoc;
   1184   const VarDecl *NRVOCandidate;
   1185 
   1186 public:
   1187   ReturnStmt(SourceLocation RL)
   1188     : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
   1189 
   1190   ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
   1191     : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
   1192       NRVOCandidate(NRVOCandidate) {}
   1193 
   1194   /// \brief Build an empty return expression.
   1195   explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
   1196 
   1197   const Expr *getRetValue() const;
   1198   Expr *getRetValue();
   1199   void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
   1200 
   1201   SourceLocation getReturnLoc() const { return RetLoc; }
   1202   void setReturnLoc(SourceLocation L) { RetLoc = L; }
   1203 
   1204   /// \brief Retrieve the variable that might be used for the named return
   1205   /// value optimization.
   1206   ///
   1207   /// The optimization itself can only be performed if the variable is
   1208   /// also marked as an NRVO object.
   1209   const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
   1210   void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
   1211 
   1212   SourceRange getSourceRange() const;
   1213 
   1214   static bool classof(const Stmt *T) {
   1215     return T->getStmtClass() == ReturnStmtClass;
   1216   }
   1217   static bool classof(const ReturnStmt *) { return true; }
   1218 
   1219   // Iterators
   1220   child_range children() {
   1221     if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
   1222     return child_range();
   1223   }
   1224 };
   1225 
   1226 /// AsmStmt - This represents a GNU inline-assembly statement extension.
   1227 ///
   1228 class AsmStmt : public Stmt {
   1229   SourceLocation AsmLoc, RParenLoc;
   1230   StringLiteral *AsmStr;
   1231 
   1232   bool IsSimple;
   1233   bool IsVolatile;
   1234   bool MSAsm;
   1235 
   1236   unsigned NumOutputs;
   1237   unsigned NumInputs;
   1238   unsigned NumClobbers;
   1239 
   1240   // FIXME: If we wanted to, we could allocate all of these in one big array.
   1241   IdentifierInfo **Names;
   1242   StringLiteral **Constraints;
   1243   Stmt **Exprs;
   1244   StringLiteral **Clobbers;
   1245 
   1246 public:
   1247   AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
   1248           bool msasm, unsigned numoutputs, unsigned numinputs,
   1249           IdentifierInfo **names, StringLiteral **constraints,
   1250           Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
   1251           StringLiteral **clobbers, SourceLocation rparenloc);
   1252 
   1253   /// \brief Build an empty inline-assembly statement.
   1254   explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
   1255     Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
   1256 
   1257   SourceLocation getAsmLoc() const { return AsmLoc; }
   1258   void setAsmLoc(SourceLocation L) { AsmLoc = L; }
   1259   SourceLocation getRParenLoc() const { return RParenLoc; }
   1260   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
   1261 
   1262   bool isVolatile() const { return IsVolatile; }
   1263   void setVolatile(bool V) { IsVolatile = V; }
   1264   bool isSimple() const { return IsSimple; }
   1265   void setSimple(bool V) { IsSimple = V; }
   1266   bool isMSAsm() const { return MSAsm; }
   1267   void setMSAsm(bool V) { MSAsm = V; }
   1268 
   1269   //===--- Asm String Analysis ---===//
   1270 
   1271   const StringLiteral *getAsmString() const { return AsmStr; }
   1272   StringLiteral *getAsmString() { return AsmStr; }
   1273   void setAsmString(StringLiteral *E) { AsmStr = E; }
   1274 
   1275   /// AsmStringPiece - this is part of a decomposed asm string specification
   1276   /// (for use with the AnalyzeAsmString function below).  An asm string is
   1277   /// considered to be a concatenation of these parts.
   1278   class AsmStringPiece {
   1279   public:
   1280     enum Kind {
   1281       String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
   1282       Operand  // Operand reference, with optional modifier %c4.
   1283     };
   1284   private:
   1285     Kind MyKind;
   1286     std::string Str;
   1287     unsigned OperandNo;
   1288   public:
   1289     AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
   1290     AsmStringPiece(unsigned OpNo, char Modifier)
   1291       : MyKind(Operand), Str(), OperandNo(OpNo) {
   1292       Str += Modifier;
   1293     }
   1294 
   1295     bool isString() const { return MyKind == String; }
   1296     bool isOperand() const { return MyKind == Operand; }
   1297 
   1298     const std::string &getString() const {
   1299       assert(isString());
   1300       return Str;
   1301     }
   1302 
   1303     unsigned getOperandNo() const {
   1304       assert(isOperand());
   1305       return OperandNo;
   1306     }
   1307 
   1308     /// getModifier - Get the modifier for this operand, if present.  This
   1309     /// returns '\0' if there was no modifier.
   1310     char getModifier() const {
   1311       assert(isOperand());
   1312       return Str[0];
   1313     }
   1314   };
   1315 
   1316   /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
   1317   /// it into pieces.  If the asm string is erroneous, emit errors and return
   1318   /// true, otherwise return false.  This handles canonicalization and
   1319   /// translation of strings from GCC syntax to LLVM IR syntax, and handles
   1320   //// flattening of named references like %[foo] to Operand AsmStringPiece's.
   1321   unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
   1322                             ASTContext &C, unsigned &DiagOffs) const;
   1323 
   1324 
   1325   //===--- Output operands ---===//
   1326 
   1327   unsigned getNumOutputs() const { return NumOutputs; }
   1328 
   1329   IdentifierInfo *getOutputIdentifier(unsigned i) const {
   1330     return Names[i];
   1331   }
   1332 
   1333   StringRef getOutputName(unsigned i) const {
   1334     if (IdentifierInfo *II = getOutputIdentifier(i))
   1335       return II->getName();
   1336 
   1337     return StringRef();
   1338   }
   1339 
   1340   /// getOutputConstraint - Return the constraint string for the specified
   1341   /// output operand.  All output constraints are known to be non-empty (either
   1342   /// '=' or '+').
   1343   StringRef getOutputConstraint(unsigned i) const;
   1344 
   1345   const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
   1346     return Constraints[i];
   1347   }
   1348   StringLiteral *getOutputConstraintLiteral(unsigned i) {
   1349     return Constraints[i];
   1350   }
   1351 
   1352   Expr *getOutputExpr(unsigned i);
   1353 
   1354   const Expr *getOutputExpr(unsigned i) const {
   1355     return const_cast<AsmStmt*>(this)->getOutputExpr(i);
   1356   }
   1357 
   1358   /// isOutputPlusConstraint - Return true if the specified output constraint
   1359   /// is a "+" constraint (which is both an input and an output) or false if it
   1360   /// is an "=" constraint (just an output).
   1361   bool isOutputPlusConstraint(unsigned i) const {
   1362     return getOutputConstraint(i)[0] == '+';
   1363   }
   1364 
   1365   /// getNumPlusOperands - Return the number of output operands that have a "+"
   1366   /// constraint.
   1367   unsigned getNumPlusOperands() const;
   1368 
   1369   //===--- Input operands ---===//
   1370 
   1371   unsigned getNumInputs() const { return NumInputs; }
   1372 
   1373   IdentifierInfo *getInputIdentifier(unsigned i) const {
   1374     return Names[i + NumOutputs];
   1375   }
   1376 
   1377   StringRef getInputName(unsigned i) const {
   1378     if (IdentifierInfo *II = getInputIdentifier(i))
   1379       return II->getName();
   1380 
   1381     return StringRef();
   1382   }
   1383 
   1384   /// getInputConstraint - Return the specified input constraint.  Unlike output
   1385   /// constraints, these can be empty.
   1386   StringRef getInputConstraint(unsigned i) const;
   1387 
   1388   const StringLiteral *getInputConstraintLiteral(unsigned i) const {
   1389     return Constraints[i + NumOutputs];
   1390   }
   1391   StringLiteral *getInputConstraintLiteral(unsigned i) {
   1392     return Constraints[i + NumOutputs];
   1393   }
   1394 
   1395   Expr *getInputExpr(unsigned i);
   1396   void setInputExpr(unsigned i, Expr *E);
   1397 
   1398   const Expr *getInputExpr(unsigned i) const {
   1399     return const_cast<AsmStmt*>(this)->getInputExpr(i);
   1400   }
   1401 
   1402   void setOutputsAndInputsAndClobbers(ASTContext &C,
   1403                                       IdentifierInfo **Names,
   1404                                       StringLiteral **Constraints,
   1405                                       Stmt **Exprs,
   1406                                       unsigned NumOutputs,
   1407                                       unsigned NumInputs,
   1408                                       StringLiteral **Clobbers,
   1409                                       unsigned NumClobbers);
   1410 
   1411   //===--- Other ---===//
   1412 
   1413   /// getNamedOperand - Given a symbolic operand reference like %[foo],
   1414   /// translate this into a numeric value needed to reference the same operand.
   1415   /// This returns -1 if the operand name is invalid.
   1416   int getNamedOperand(StringRef SymbolicName) const;
   1417 
   1418   unsigned getNumClobbers() const { return NumClobbers; }
   1419   StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
   1420   const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
   1421 
   1422   SourceRange getSourceRange() const {
   1423     return SourceRange(AsmLoc, RParenLoc);
   1424   }
   1425 
   1426   static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
   1427   static bool classof(const AsmStmt *) { return true; }
   1428 
   1429   // Input expr iterators.
   1430 
   1431   typedef ExprIterator inputs_iterator;
   1432   typedef ConstExprIterator const_inputs_iterator;
   1433 
   1434   inputs_iterator begin_inputs() {
   1435     return &Exprs[0] + NumOutputs;
   1436   }
   1437 
   1438   inputs_iterator end_inputs() {
   1439     return &Exprs[0] + NumOutputs + NumInputs;
   1440   }
   1441 
   1442   const_inputs_iterator begin_inputs() const {
   1443     return &Exprs[0] + NumOutputs;
   1444   }
   1445 
   1446   const_inputs_iterator end_inputs() const {
   1447     return &Exprs[0] + NumOutputs + NumInputs;
   1448   }
   1449 
   1450   // Output expr iterators.
   1451 
   1452   typedef ExprIterator outputs_iterator;
   1453   typedef ConstExprIterator const_outputs_iterator;
   1454 
   1455   outputs_iterator begin_outputs() {
   1456     return &Exprs[0];
   1457   }
   1458   outputs_iterator end_outputs() {
   1459     return &Exprs[0] + NumOutputs;
   1460   }
   1461 
   1462   const_outputs_iterator begin_outputs() const {
   1463     return &Exprs[0];
   1464   }
   1465   const_outputs_iterator end_outputs() const {
   1466     return &Exprs[0] + NumOutputs;
   1467   }
   1468 
   1469   child_range children() {
   1470     return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
   1471   }
   1472 };
   1473 
   1474 class SEHExceptStmt : public Stmt {
   1475   SourceLocation  Loc;
   1476   Stmt           *Children[2];
   1477 
   1478   enum { FILTER_EXPR, BLOCK };
   1479 
   1480   SEHExceptStmt(SourceLocation Loc,
   1481                 Expr *FilterExpr,
   1482                 Stmt *Block);
   1483 
   1484   friend class ASTReader;
   1485   friend class ASTStmtReader;
   1486   explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
   1487 
   1488 public:
   1489   static SEHExceptStmt* Create(ASTContext &C,
   1490                                SourceLocation ExceptLoc,
   1491                                Expr *FilterExpr,
   1492                                Stmt *Block);
   1493   SourceRange getSourceRange() const {
   1494     return SourceRange(getExceptLoc(), getEndLoc());
   1495   }
   1496 
   1497   SourceLocation getExceptLoc() const { return Loc; }
   1498   SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
   1499 
   1500   Expr *getFilterExpr() const { return reinterpret_cast<Expr*>(Children[FILTER_EXPR]); }
   1501   CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Children[BLOCK]); }
   1502 
   1503   child_range children() {
   1504     return child_range(Children,Children+2);
   1505   }
   1506 
   1507   static bool classof(const Stmt *T) {
   1508     return T->getStmtClass() == SEHExceptStmtClass;
   1509   }
   1510 
   1511   static bool classof(SEHExceptStmt *) { return true; }
   1512 
   1513 };
   1514 
   1515 class SEHFinallyStmt : public Stmt {
   1516   SourceLocation  Loc;
   1517   Stmt           *Block;
   1518 
   1519   SEHFinallyStmt(SourceLocation Loc,
   1520                  Stmt *Block);
   1521 
   1522   friend class ASTReader;
   1523   friend class ASTStmtReader;
   1524   explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
   1525 
   1526 public:
   1527   static SEHFinallyStmt* Create(ASTContext &C,
   1528                                 SourceLocation FinallyLoc,
   1529                                 Stmt *Block);
   1530 
   1531   SourceRange getSourceRange() const {
   1532     return SourceRange(getFinallyLoc(), getEndLoc());
   1533   }
   1534 
   1535   SourceLocation getFinallyLoc() const { return Loc; }
   1536   SourceLocation getEndLoc() const { return Block->getLocEnd(); }
   1537 
   1538   CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
   1539 
   1540   child_range children() {
   1541     return child_range(&Block,&Block+1);
   1542   }
   1543 
   1544   static bool classof(const Stmt *T) {
   1545     return T->getStmtClass() == SEHFinallyStmtClass;
   1546   }
   1547 
   1548   static bool classof(SEHFinallyStmt *) { return true; }
   1549 
   1550 };
   1551 
   1552 class SEHTryStmt : public Stmt {
   1553   bool            IsCXXTry;
   1554   SourceLocation  TryLoc;
   1555   Stmt           *Children[2];
   1556 
   1557   enum { TRY = 0, HANDLER = 1 };
   1558 
   1559   SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
   1560              SourceLocation TryLoc,
   1561              Stmt *TryBlock,
   1562              Stmt *Handler);
   1563 
   1564   friend class ASTReader;
   1565   friend class ASTStmtReader;
   1566   explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
   1567 
   1568 public:
   1569   static SEHTryStmt* Create(ASTContext &C,
   1570                             bool isCXXTry,
   1571                             SourceLocation TryLoc,
   1572                             Stmt *TryBlock,
   1573                             Stmt *Handler);
   1574 
   1575   SourceRange getSourceRange() const {
   1576     return SourceRange(getTryLoc(), getEndLoc());
   1577   }
   1578 
   1579   SourceLocation getTryLoc() const { return TryLoc; }
   1580   SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
   1581 
   1582   bool getIsCXXTry() const { return IsCXXTry; }
   1583   CompoundStmt* getTryBlock() const { return llvm::cast<CompoundStmt>(Children[TRY]); }
   1584   Stmt *getHandler() const { return Children[HANDLER]; }
   1585 
   1586   /// Returns 0 if not defined
   1587   SEHExceptStmt  *getExceptHandler() const;
   1588   SEHFinallyStmt *getFinallyHandler() const;
   1589 
   1590   child_range children() {
   1591     return child_range(Children,Children+2);
   1592   }
   1593 
   1594   static bool classof(const Stmt *T) {
   1595     return T->getStmtClass() == SEHTryStmtClass;
   1596   }
   1597 
   1598   static bool classof(SEHTryStmt *) { return true; }
   1599 
   1600 };
   1601 
   1602 }  // end namespace clang
   1603 
   1604 #endif
   1605