clang API Documentation

Stmt.h
Go to the documentation of this file.
00001 //===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the Stmt interface and subclasses.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_AST_STMT_H
00015 #define LLVM_CLANG_AST_STMT_H
00016 
00017 #include "clang/Basic/LLVM.h"
00018 #include "clang/Basic/SourceLocation.h"
00019 #include "clang/AST/PrettyPrinter.h"
00020 #include "clang/AST/StmtIterator.h"
00021 #include "clang/AST/DeclGroup.h"
00022 #include "clang/AST/ASTContext.h"
00023 #include "clang/AST/Attr.h"
00024 #include "llvm/ADT/SmallVector.h"
00025 #include "llvm/Support/Compiler.h"
00026 #include "llvm/Support/raw_ostream.h"
00027 #include <string>
00028 
00029 namespace llvm {
00030   class FoldingSetNodeID;
00031 }
00032 
00033 namespace clang {
00034   class ASTContext;
00035   class Expr;
00036   class Decl;
00037   class ParmVarDecl;
00038   class QualType;
00039   class IdentifierInfo;
00040   class SourceManager;
00041   class StringLiteral;
00042   class SwitchStmt;
00043 
00044   //===--------------------------------------------------------------------===//
00045   // ExprIterator - Iterators for iterating over Stmt* arrays that contain
00046   //  only Expr*.  This is needed because AST nodes use Stmt* arrays to store
00047   //  references to children (to be compatible with StmtIterator).
00048   //===--------------------------------------------------------------------===//
00049 
00050   class Stmt;
00051   class Expr;
00052 
00053   class ExprIterator {
00054     Stmt** I;
00055   public:
00056     ExprIterator(Stmt** i) : I(i) {}
00057     ExprIterator() : I(0) {}
00058     ExprIterator& operator++() { ++I; return *this; }
00059     ExprIterator operator-(size_t i) { return I-i; }
00060     ExprIterator operator+(size_t i) { return I+i; }
00061     Expr* operator[](size_t idx);
00062     // FIXME: Verify that this will correctly return a signed distance.
00063     signed operator-(const ExprIterator& R) const { return I - R.I; }
00064     Expr* operator*() const;
00065     Expr* operator->() const;
00066     bool operator==(const ExprIterator& R) const { return I == R.I; }
00067     bool operator!=(const ExprIterator& R) const { return I != R.I; }
00068     bool operator>(const ExprIterator& R) const { return I > R.I; }
00069     bool operator>=(const ExprIterator& R) const { return I >= R.I; }
00070   };
00071 
00072   class ConstExprIterator {
00073     const Stmt * const *I;
00074   public:
00075     ConstExprIterator(const Stmt * const *i) : I(i) {}
00076     ConstExprIterator() : I(0) {}
00077     ConstExprIterator& operator++() { ++I; return *this; }
00078     ConstExprIterator operator+(size_t i) const { return I+i; }
00079     ConstExprIterator operator-(size_t i) const { return I-i; }
00080     const Expr * operator[](size_t idx) const;
00081     signed operator-(const ConstExprIterator& R) const { return I - R.I; }
00082     const Expr * operator*() const;
00083     const Expr * operator->() const;
00084     bool operator==(const ConstExprIterator& R) const { return I == R.I; }
00085     bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
00086     bool operator>(const ConstExprIterator& R) const { return I > R.I; }
00087     bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
00088   };
00089 
00090 //===----------------------------------------------------------------------===//
00091 // AST classes for statements.
00092 //===----------------------------------------------------------------------===//
00093 
00094 /// Stmt - This represents one statement.
00095 ///
00096 class Stmt {
00097 public:
00098   enum StmtClass {
00099     NoStmtClass = 0,
00100 #define STMT(CLASS, PARENT) CLASS##Class,
00101 #define STMT_RANGE(BASE, FIRST, LAST) \
00102         first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
00103 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
00104         first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
00105 #define ABSTRACT_STMT(STMT)
00106 #include "clang/AST/StmtNodes.inc"
00107   };
00108 
00109   // Make vanilla 'new' and 'delete' illegal for Stmts.
00110 protected:
00111   void* operator new(size_t bytes) throw() {
00112     llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
00113   }
00114   void operator delete(void* data) throw() {
00115     llvm_unreachable("Stmts cannot be released with regular 'delete'.");
00116   }
00117 
00118   class StmtBitfields {
00119     friend class Stmt;
00120 
00121     /// \brief The statement class.
00122     unsigned sClass : 8;
00123   };
00124   enum { NumStmtBits = 8 };
00125 
00126   class CompoundStmtBitfields {
00127     friend class CompoundStmt;
00128     unsigned : NumStmtBits;
00129 
00130     unsigned NumStmts : 32 - NumStmtBits;
00131   };
00132 
00133   class ExprBitfields {
00134     friend class Expr;
00135     friend class DeclRefExpr; // computeDependence
00136     friend class InitListExpr; // ctor
00137     friend class DesignatedInitExpr; // ctor
00138     friend class BlockDeclRefExpr; // ctor
00139     friend class ASTStmtReader; // deserialization
00140     friend class CXXNewExpr; // ctor
00141     friend class DependentScopeDeclRefExpr; // ctor
00142     friend class CXXConstructExpr; // ctor
00143     friend class CallExpr; // ctor
00144     friend class OffsetOfExpr; // ctor
00145     friend class ObjCMessageExpr; // ctor
00146     friend class ObjCArrayLiteral; // ctor
00147     friend class ObjCDictionaryLiteral; // ctor
00148     friend class ShuffleVectorExpr; // ctor
00149     friend class ParenListExpr; // ctor
00150     friend class CXXUnresolvedConstructExpr; // ctor
00151     friend class CXXDependentScopeMemberExpr; // ctor
00152     friend class OverloadExpr; // ctor
00153     friend class PseudoObjectExpr; // ctor
00154     friend class AtomicExpr; // ctor
00155     unsigned : NumStmtBits;
00156 
00157     unsigned ValueKind : 2;
00158     unsigned ObjectKind : 2;
00159     unsigned TypeDependent : 1;
00160     unsigned ValueDependent : 1;
00161     unsigned InstantiationDependent : 1;
00162     unsigned ContainsUnexpandedParameterPack : 1;
00163   };
00164   enum { NumExprBits = 16 };
00165 
00166   class CharacterLiteralBitfields {
00167     friend class CharacterLiteral;
00168     unsigned : NumExprBits;
00169 
00170     unsigned Kind : 2;
00171   };
00172 
00173   class FloatingLiteralBitfields {
00174     friend class FloatingLiteral;
00175     unsigned : NumExprBits;
00176 
00177     unsigned IsIEEE : 1; // Distinguishes between PPC128 and IEEE128.
00178     unsigned IsExact : 1;
00179   };
00180 
00181   class UnaryExprOrTypeTraitExprBitfields {
00182     friend class UnaryExprOrTypeTraitExpr;
00183     unsigned : NumExprBits;
00184 
00185     unsigned Kind : 2;
00186     unsigned IsType : 1; // true if operand is a type, false if an expression.
00187   };
00188 
00189   class DeclRefExprBitfields {
00190     friend class DeclRefExpr;
00191     friend class ASTStmtReader; // deserialization
00192     unsigned : NumExprBits;
00193 
00194     unsigned HasQualifier : 1;
00195     unsigned HasTemplateKWAndArgsInfo : 1;
00196     unsigned HasFoundDecl : 1;
00197     unsigned HadMultipleCandidates : 1;
00198     unsigned RefersToEnclosingLocal : 1;
00199   };
00200 
00201   class CastExprBitfields {
00202     friend class CastExpr;
00203     unsigned : NumExprBits;
00204 
00205     unsigned Kind : 6;
00206     unsigned BasePathSize : 32 - 6 - NumExprBits;
00207   };
00208 
00209   class CallExprBitfields {
00210     friend class CallExpr;
00211     unsigned : NumExprBits;
00212 
00213     unsigned NumPreArgs : 1;
00214   };
00215 
00216   class ExprWithCleanupsBitfields {
00217     friend class ExprWithCleanups;
00218     friend class ASTStmtReader; // deserialization
00219 
00220     unsigned : NumExprBits;
00221 
00222     unsigned NumObjects : 32 - NumExprBits;
00223   };
00224 
00225   class PseudoObjectExprBitfields {
00226     friend class PseudoObjectExpr;
00227     friend class ASTStmtReader; // deserialization
00228 
00229     unsigned : NumExprBits;
00230 
00231     // These don't need to be particularly wide, because they're
00232     // strictly limited by the forms of expressions we permit.
00233     unsigned NumSubExprs : 8;
00234     unsigned ResultIndex : 32 - 8 - NumExprBits;
00235   };
00236 
00237   class ObjCIndirectCopyRestoreExprBitfields {
00238     friend class ObjCIndirectCopyRestoreExpr;
00239     unsigned : NumExprBits;
00240 
00241     unsigned ShouldCopy : 1;
00242   };
00243 
00244   class InitListExprBitfields {
00245     friend class InitListExpr;
00246 
00247     unsigned : NumExprBits;
00248 
00249     /// Whether this initializer list originally had a GNU array-range
00250     /// designator in it. This is a temporary marker used by CodeGen.
00251     unsigned HadArrayRangeDesignator : 1;
00252 
00253     /// Whether this initializer list initializes a std::initializer_list
00254     /// object.
00255     unsigned InitializesStdInitializerList : 1;
00256   };
00257 
00258   class TypeTraitExprBitfields {
00259     friend class TypeTraitExpr;
00260     friend class ASTStmtReader;
00261     friend class ASTStmtWriter;
00262     
00263     unsigned : NumExprBits;
00264     
00265     /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
00266     unsigned Kind : 8;
00267     
00268     /// \brief If this expression is not value-dependent, this indicates whether
00269     /// the trait evaluated true or false.
00270     unsigned Value : 1;
00271 
00272     /// \brief The number of arguments to this type trait.
00273     unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
00274   };
00275   
00276   union {
00277     // FIXME: this is wasteful on 64-bit platforms.
00278     void *Aligner;
00279 
00280     StmtBitfields StmtBits;
00281     CompoundStmtBitfields CompoundStmtBits;
00282     ExprBitfields ExprBits;
00283     CharacterLiteralBitfields CharacterLiteralBits;
00284     FloatingLiteralBitfields FloatingLiteralBits;
00285     UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
00286     DeclRefExprBitfields DeclRefExprBits;
00287     CastExprBitfields CastExprBits;
00288     CallExprBitfields CallExprBits;
00289     ExprWithCleanupsBitfields ExprWithCleanupsBits;
00290     PseudoObjectExprBitfields PseudoObjectExprBits;
00291     ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
00292     InitListExprBitfields InitListExprBits;
00293     TypeTraitExprBitfields TypeTraitExprBits;
00294   };
00295 
00296   friend class ASTStmtReader;
00297   friend class ASTStmtWriter;
00298 
00299 public:
00300   // Only allow allocation of Stmts using the allocator in ASTContext
00301   // or by doing a placement new.
00302   void* operator new(size_t bytes, ASTContext& C,
00303                      unsigned alignment = 8) throw() {
00304     return ::operator new(bytes, C, alignment);
00305   }
00306 
00307   void* operator new(size_t bytes, ASTContext* C,
00308                      unsigned alignment = 8) throw() {
00309     return ::operator new(bytes, *C, alignment);
00310   }
00311 
00312   void* operator new(size_t bytes, void* mem) throw() {
00313     return mem;
00314   }
00315 
00316   void operator delete(void*, ASTContext&, unsigned) throw() { }
00317   void operator delete(void*, ASTContext*, unsigned) throw() { }
00318   void operator delete(void*, std::size_t) throw() { }
00319   void operator delete(void*, void*) throw() { }
00320 
00321 public:
00322   /// \brief A placeholder type used to construct an empty shell of a
00323   /// type, that will be filled in later (e.g., by some
00324   /// de-serialization).
00325   struct EmptyShell { };
00326 
00327 private:
00328   /// \brief Whether statistic collection is enabled.
00329   static bool StatisticsEnabled;
00330 
00331 protected:
00332   /// \brief Construct an empty statement.
00333   explicit Stmt(StmtClass SC, EmptyShell) {
00334     StmtBits.sClass = SC;
00335     if (StatisticsEnabled) Stmt::addStmtClass(SC);
00336   }
00337 
00338 public:
00339   Stmt(StmtClass SC) {
00340     StmtBits.sClass = SC;
00341     if (StatisticsEnabled) Stmt::addStmtClass(SC);
00342   }
00343 
00344   StmtClass getStmtClass() const {
00345     return static_cast<StmtClass>(StmtBits.sClass);
00346   }
00347   const char *getStmtClassName() const;
00348 
00349   /// SourceLocation tokens are not useful in isolation - they are low level
00350   /// value objects created/interpreted by SourceManager. We assume AST
00351   /// clients will have a pointer to the respective SourceManager.
00352   SourceRange getSourceRange() const LLVM_READONLY;
00353   SourceLocation getLocStart() const LLVM_READONLY;
00354   SourceLocation getLocEnd() const LLVM_READONLY;
00355 
00356   // global temp stats (until we have a per-module visitor)
00357   static void addStmtClass(const StmtClass s);
00358   static void EnableStatistics();
00359   static void PrintStats();
00360 
00361   /// dump - This does a local dump of the specified AST fragment.  It dumps the
00362   /// specified node and a few nodes underneath it, but not the whole subtree.
00363   /// This is useful in a debugger.
00364   LLVM_ATTRIBUTE_USED void dump() const;
00365   LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
00366   void dump(raw_ostream &OS, SourceManager &SM) const;
00367 
00368   /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
00369   void dumpAll() const;
00370   void dumpAll(SourceManager &SM) const;
00371 
00372   /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
00373   /// back to its original source language syntax.
00374   void dumpPretty(ASTContext& Context) const;
00375   void printPretty(raw_ostream &OS, PrinterHelper *Helper,
00376                    const PrintingPolicy &Policy,
00377                    unsigned Indentation = 0) const {
00378     printPretty(OS, *(ASTContext*)0, Helper, Policy, Indentation);
00379   }
00380   void printPretty(raw_ostream &OS, ASTContext &Context,
00381                    PrinterHelper *Helper,
00382                    const PrintingPolicy &Policy,
00383                    unsigned Indentation = 0) const;
00384 
00385   /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
00386   ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
00387   void viewAST() const;
00388 
00389   /// Skip past any implicit AST nodes which might surround this
00390   /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
00391   Stmt *IgnoreImplicit();
00392 
00393   const Stmt *stripLabelLikeStatements() const;
00394   Stmt *stripLabelLikeStatements() {
00395     return const_cast<Stmt*>(
00396       const_cast<const Stmt*>(this)->stripLabelLikeStatements());
00397   }
00398 
00399   // Implement isa<T> support.
00400   static bool classof(const Stmt *) { return true; }
00401 
00402   /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
00403   ///  contain implicit control-flow in the order their subexpressions
00404   ///  are evaluated.  This predicate returns true if this statement has
00405   ///  such implicit control-flow.  Such statements are also specially handled
00406   ///  within CFGs.
00407   bool hasImplicitControlFlow() const;
00408 
00409   /// Child Iterators: All subclasses must implement 'children'
00410   /// to permit easy iteration over the substatements/subexpessions of an
00411   /// AST node.  This permits easy iteration over all nodes in the AST.
00412   typedef StmtIterator       child_iterator;
00413   typedef ConstStmtIterator  const_child_iterator;
00414 
00415   typedef StmtRange          child_range;
00416   typedef ConstStmtRange     const_child_range;
00417 
00418   child_range children();
00419   const_child_range children() const {
00420     return const_cast<Stmt*>(this)->children();
00421   }
00422 
00423   child_iterator child_begin() { return children().first; }
00424   child_iterator child_end() { return children().second; }
00425 
00426   const_child_iterator child_begin() const { return children().first; }
00427   const_child_iterator child_end() const { return children().second; }
00428 
00429   /// \brief Produce a unique representation of the given statement.
00430   ///
00431   /// \brief ID once the profiling operation is complete, will contain
00432   /// the unique representation of the given statement.
00433   ///
00434   /// \brief Context the AST context in which the statement resides
00435   ///
00436   /// \brief Canonical whether the profile should be based on the canonical
00437   /// representation of this statement (e.g., where non-type template
00438   /// parameters are identified by index/level rather than their
00439   /// declaration pointers) or the exact representation of the statement as
00440   /// written in the source.
00441   void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
00442                bool Canonical) const;
00443 };
00444 
00445 /// DeclStmt - Adaptor class for mixing declarations with statements and
00446 /// expressions. For example, CompoundStmt mixes statements, expressions
00447 /// and declarations (variables, types). Another example is ForStmt, where
00448 /// the first statement can be an expression or a declaration.
00449 ///
00450 class DeclStmt : public Stmt {
00451   DeclGroupRef DG;
00452   SourceLocation StartLoc, EndLoc;
00453 
00454 public:
00455   DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
00456            SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
00457                                     StartLoc(startLoc), EndLoc(endLoc) {}
00458 
00459   /// \brief Build an empty declaration statement.
00460   explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
00461 
00462   /// isSingleDecl - This method returns true if this DeclStmt refers
00463   /// to a single Decl.
00464   bool isSingleDecl() const {
00465     return DG.isSingleDecl();
00466   }
00467 
00468   const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
00469   Decl *getSingleDecl() { return DG.getSingleDecl(); }
00470 
00471   const DeclGroupRef getDeclGroup() const { return DG; }
00472   DeclGroupRef getDeclGroup() { return DG; }
00473   void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
00474 
00475   SourceLocation getStartLoc() const { return StartLoc; }
00476   void setStartLoc(SourceLocation L) { StartLoc = L; }
00477   SourceLocation getEndLoc() const { return EndLoc; }
00478   void setEndLoc(SourceLocation L) { EndLoc = L; }
00479 
00480   SourceRange getSourceRange() const LLVM_READONLY {
00481     return SourceRange(StartLoc, EndLoc);
00482   }
00483 
00484   static bool classof(const Stmt *T) {
00485     return T->getStmtClass() == DeclStmtClass;
00486   }
00487   static bool classof(const DeclStmt *) { return true; }
00488 
00489   // Iterators over subexpressions.
00490   child_range children() {
00491     return child_range(child_iterator(DG.begin(), DG.end()),
00492                        child_iterator(DG.end(), DG.end()));
00493   }
00494 
00495   typedef DeclGroupRef::iterator decl_iterator;
00496   typedef DeclGroupRef::const_iterator const_decl_iterator;
00497 
00498   decl_iterator decl_begin() { return DG.begin(); }
00499   decl_iterator decl_end() { return DG.end(); }
00500   const_decl_iterator decl_begin() const { return DG.begin(); }
00501   const_decl_iterator decl_end() const { return DG.end(); }
00502 };
00503 
00504 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
00505 ///
00506 class NullStmt : public Stmt {
00507   SourceLocation SemiLoc;
00508 
00509   /// \brief True if the null statement was preceded by an empty macro, e.g:
00510   /// @code
00511   ///   #define CALL(x)
00512   ///   CALL(0);
00513   /// @endcode
00514   bool HasLeadingEmptyMacro;
00515 public:
00516   NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
00517     : Stmt(NullStmtClass), SemiLoc(L),
00518       HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
00519 
00520   /// \brief Build an empty null statement.
00521   explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
00522       HasLeadingEmptyMacro(false) { }
00523 
00524   SourceLocation getSemiLoc() const { return SemiLoc; }
00525   void setSemiLoc(SourceLocation L) { SemiLoc = L; }
00526 
00527   bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
00528 
00529   SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(SemiLoc); }
00530 
00531   static bool classof(const Stmt *T) {
00532     return T->getStmtClass() == NullStmtClass;
00533   }
00534   static bool classof(const NullStmt *) { return true; }
00535 
00536   child_range children() { return child_range(); }
00537 
00538   friend class ASTStmtReader;
00539   friend class ASTStmtWriter;
00540 };
00541 
00542 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
00543 ///
00544 class CompoundStmt : public Stmt {
00545   Stmt** Body;
00546   SourceLocation LBracLoc, RBracLoc;
00547 public:
00548   CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned NumStmts,
00549                SourceLocation LB, SourceLocation RB)
00550   : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
00551     CompoundStmtBits.NumStmts = NumStmts;
00552     assert(CompoundStmtBits.NumStmts == NumStmts &&
00553            "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
00554 
00555     if (NumStmts == 0) {
00556       Body = 0;
00557       return;
00558     }
00559 
00560     Body = new (C) Stmt*[NumStmts];
00561     memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
00562   }
00563 
00564   // \brief Build an empty compound statement.
00565   explicit CompoundStmt(EmptyShell Empty)
00566     : Stmt(CompoundStmtClass, Empty), Body(0) {
00567     CompoundStmtBits.NumStmts = 0;
00568   }
00569 
00570   void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
00571 
00572   bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
00573   unsigned size() const { return CompoundStmtBits.NumStmts; }
00574 
00575   typedef Stmt** body_iterator;
00576   body_iterator body_begin() { return Body; }
00577   body_iterator body_end() { return Body + size(); }
00578   Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
00579 
00580   void setLastStmt(Stmt *S) {
00581     assert(!body_empty() && "setLastStmt");
00582     Body[size()-1] = S;
00583   }
00584 
00585   typedef Stmt* const * const_body_iterator;
00586   const_body_iterator body_begin() const { return Body; }
00587   const_body_iterator body_end() const { return Body + size(); }
00588   const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
00589 
00590   typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
00591   reverse_body_iterator body_rbegin() {
00592     return reverse_body_iterator(body_end());
00593   }
00594   reverse_body_iterator body_rend() {
00595     return reverse_body_iterator(body_begin());
00596   }
00597 
00598   typedef std::reverse_iterator<const_body_iterator>
00599           const_reverse_body_iterator;
00600 
00601   const_reverse_body_iterator body_rbegin() const {
00602     return const_reverse_body_iterator(body_end());
00603   }
00604 
00605   const_reverse_body_iterator body_rend() const {
00606     return const_reverse_body_iterator(body_begin());
00607   }
00608 
00609   SourceRange getSourceRange() const LLVM_READONLY {
00610     return SourceRange(LBracLoc, RBracLoc);
00611   }
00612 
00613   SourceLocation getLBracLoc() const { return LBracLoc; }
00614   void setLBracLoc(SourceLocation L) { LBracLoc = L; }
00615   SourceLocation getRBracLoc() const { return RBracLoc; }
00616   void setRBracLoc(SourceLocation L) { RBracLoc = L; }
00617 
00618   static bool classof(const Stmt *T) {
00619     return T->getStmtClass() == CompoundStmtClass;
00620   }
00621   static bool classof(const CompoundStmt *) { return true; }
00622 
00623   // Iterators
00624   child_range children() {
00625     return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
00626   }
00627 
00628   const_child_range children() const {
00629     return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
00630   }
00631 };
00632 
00633 // SwitchCase is the base class for CaseStmt and DefaultStmt,
00634 class SwitchCase : public Stmt {
00635 protected:
00636   // A pointer to the following CaseStmt or DefaultStmt class,
00637   // used by SwitchStmt.
00638   SwitchCase *NextSwitchCase;
00639 
00640   SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
00641 
00642 public:
00643   const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
00644 
00645   SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
00646 
00647   void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
00648 
00649   Stmt *getSubStmt();
00650   const Stmt *getSubStmt() const {
00651     return const_cast<SwitchCase*>(this)->getSubStmt();
00652   }
00653 
00654   SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(); }
00655 
00656   static bool classof(const Stmt *T) {
00657     return T->getStmtClass() == CaseStmtClass ||
00658            T->getStmtClass() == DefaultStmtClass;
00659   }
00660   static bool classof(const SwitchCase *) { return true; }
00661 };
00662 
00663 class CaseStmt : public SwitchCase {
00664   enum { LHS, RHS, SUBSTMT, END_EXPR };
00665   Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
00666                              // GNU "case 1 ... 4" extension
00667   SourceLocation CaseLoc;
00668   SourceLocation EllipsisLoc;
00669   SourceLocation ColonLoc;
00670 public:
00671   CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
00672            SourceLocation ellipsisLoc, SourceLocation colonLoc)
00673     : SwitchCase(CaseStmtClass) {
00674     SubExprs[SUBSTMT] = 0;
00675     SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
00676     SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
00677     CaseLoc = caseLoc;
00678     EllipsisLoc = ellipsisLoc;
00679     ColonLoc = colonLoc;
00680   }
00681 
00682   /// \brief Build an empty switch case statement.
00683   explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
00684 
00685   SourceLocation getCaseLoc() const { return CaseLoc; }
00686   void setCaseLoc(SourceLocation L) { CaseLoc = L; }
00687   SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
00688   void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
00689   SourceLocation getColonLoc() const { return ColonLoc; }
00690   void setColonLoc(SourceLocation L) { ColonLoc = L; }
00691 
00692   Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
00693   Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
00694   Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
00695 
00696   const Expr *getLHS() const {
00697     return reinterpret_cast<const Expr*>(SubExprs[LHS]);
00698   }
00699   const Expr *getRHS() const {
00700     return reinterpret_cast<const Expr*>(SubExprs[RHS]);
00701   }
00702   const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
00703 
00704   void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
00705   void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
00706   void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
00707 
00708 
00709   SourceRange getSourceRange() const LLVM_READONLY {
00710     // Handle deeply nested case statements with iteration instead of recursion.
00711     const CaseStmt *CS = this;
00712     while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
00713       CS = CS2;
00714 
00715     return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
00716   }
00717   static bool classof(const Stmt *T) {
00718     return T->getStmtClass() == CaseStmtClass;
00719   }
00720   static bool classof(const CaseStmt *) { return true; }
00721 
00722   // Iterators
00723   child_range children() {
00724     return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
00725   }
00726 };
00727 
00728 class DefaultStmt : public SwitchCase {
00729   Stmt* SubStmt;
00730   SourceLocation DefaultLoc;
00731   SourceLocation ColonLoc;
00732 public:
00733   DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
00734     SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
00735     ColonLoc(CL) {}
00736 
00737   /// \brief Build an empty default statement.
00738   explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
00739 
00740   Stmt *getSubStmt() { return SubStmt; }
00741   const Stmt *getSubStmt() const { return SubStmt; }
00742   void setSubStmt(Stmt *S) { SubStmt = S; }
00743 
00744   SourceLocation getDefaultLoc() const { return DefaultLoc; }
00745   void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
00746   SourceLocation getColonLoc() const { return ColonLoc; }
00747   void setColonLoc(SourceLocation L) { ColonLoc = L; }
00748 
00749   SourceRange getSourceRange() const LLVM_READONLY {
00750     return SourceRange(DefaultLoc, SubStmt->getLocEnd());
00751   }
00752   static bool classof(const Stmt *T) {
00753     return T->getStmtClass() == DefaultStmtClass;
00754   }
00755   static bool classof(const DefaultStmt *) { return true; }
00756 
00757   // Iterators
00758   child_range children() { return child_range(&SubStmt, &SubStmt+1); }
00759 };
00760 
00761 
00762 /// LabelStmt - Represents a label, which has a substatement.  For example:
00763 ///    foo: return;
00764 ///
00765 class LabelStmt : public Stmt {
00766   LabelDecl *TheDecl;
00767   Stmt *SubStmt;
00768   SourceLocation IdentLoc;
00769 public:
00770   LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
00771     : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
00772   }
00773 
00774   // \brief Build an empty label statement.
00775   explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
00776 
00777   SourceLocation getIdentLoc() const { return IdentLoc; }
00778   LabelDecl *getDecl() const { return TheDecl; }
00779   void setDecl(LabelDecl *D) { TheDecl = D; }
00780   const char *getName() const;
00781   Stmt *getSubStmt() { return SubStmt; }
00782   const Stmt *getSubStmt() const { return SubStmt; }
00783   void setIdentLoc(SourceLocation L) { IdentLoc = L; }
00784   void setSubStmt(Stmt *SS) { SubStmt = SS; }
00785 
00786   SourceRange getSourceRange() const LLVM_READONLY {
00787     return SourceRange(IdentLoc, SubStmt->getLocEnd());
00788   }
00789   child_range children() { return child_range(&SubStmt, &SubStmt+1); }
00790 
00791   static bool classof(const Stmt *T) {
00792     return T->getStmtClass() == LabelStmtClass;
00793   }
00794   static bool classof(const LabelStmt *) { return true; }
00795 };
00796 
00797 
00798 /// \brief Represents an attribute applied to a statement.
00799 ///
00800 /// Represents an attribute applied to a statement. For example:
00801 ///   [[omp::for(...)]] for (...) { ... }
00802 ///
00803 class AttributedStmt : public Stmt {
00804   Stmt *SubStmt;
00805   SourceLocation AttrLoc;
00806   AttrVec Attrs;
00807   // TODO: It can be done as Attr *Attrs[1]; and variable size array as in
00808   // StringLiteral
00809 
00810   friend class ASTStmtReader;
00811 
00812 public:
00813   AttributedStmt(SourceLocation loc, const AttrVec &attrs, Stmt *substmt)
00814     : Stmt(AttributedStmtClass), SubStmt(substmt), AttrLoc(loc), Attrs(attrs) {
00815   }
00816 
00817   // \brief Build an empty attributed statement.
00818   explicit AttributedStmt(EmptyShell Empty)
00819     : Stmt(AttributedStmtClass, Empty) {
00820   }
00821 
00822   SourceLocation getAttrLoc() const { return AttrLoc; }
00823   const AttrVec &getAttrs() const { return Attrs; }
00824   Stmt *getSubStmt() { return SubStmt; }
00825   const Stmt *getSubStmt() const { return SubStmt; }
00826 
00827   SourceRange getSourceRange() const LLVM_READONLY {
00828     return SourceRange(AttrLoc, SubStmt->getLocEnd());
00829   }
00830   child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
00831 
00832   static bool classof(const Stmt *T) {
00833     return T->getStmtClass() == AttributedStmtClass;
00834   }
00835   static bool classof(const AttributedStmt *) { return true; }
00836 };
00837 
00838 
00839 /// IfStmt - This represents an if/then/else.
00840 ///
00841 class IfStmt : public Stmt {
00842   enum { VAR, COND, THEN, ELSE, END_EXPR };
00843   Stmt* SubExprs[END_EXPR];
00844 
00845   SourceLocation IfLoc;
00846   SourceLocation ElseLoc;
00847 
00848 public:
00849   IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
00850          Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
00851 
00852   /// \brief Build an empty if/then/else statement
00853   explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
00854 
00855   /// \brief Retrieve the variable declared in this "if" statement, if any.
00856   ///
00857   /// In the following example, "x" is the condition variable.
00858   /// \code
00859   /// if (int x = foo()) {
00860   ///   printf("x is %d", x);
00861   /// }
00862   /// \endcode
00863   VarDecl *getConditionVariable() const;
00864   void setConditionVariable(ASTContext &C, VarDecl *V);
00865 
00866   /// If this IfStmt has a condition variable, return the faux DeclStmt
00867   /// associated with the creation of that condition variable.
00868   const DeclStmt *getConditionVariableDeclStmt() const {
00869     return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
00870   }
00871 
00872   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
00873   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
00874   const Stmt *getThen() const { return SubExprs[THEN]; }
00875   void setThen(Stmt *S) { SubExprs[THEN] = S; }
00876   const Stmt *getElse() const { return SubExprs[ELSE]; }
00877   void setElse(Stmt *S) { SubExprs[ELSE] = S; }
00878 
00879   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
00880   Stmt *getThen() { return SubExprs[THEN]; }
00881   Stmt *getElse() { return SubExprs[ELSE]; }
00882 
00883   SourceLocation getIfLoc() const { return IfLoc; }
00884   void setIfLoc(SourceLocation L) { IfLoc = L; }
00885   SourceLocation getElseLoc() const { return ElseLoc; }
00886   void setElseLoc(SourceLocation L) { ElseLoc = L; }
00887 
00888   SourceRange getSourceRange() const LLVM_READONLY {
00889     if (SubExprs[ELSE])
00890       return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
00891     else
00892       return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
00893   }
00894 
00895   // Iterators over subexpressions.  The iterators will include iterating
00896   // over the initialization expression referenced by the condition variable.
00897   child_range children() {
00898     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
00899   }
00900 
00901   static bool classof(const Stmt *T) {
00902     return T->getStmtClass() == IfStmtClass;
00903   }
00904   static bool classof(const IfStmt *) { return true; }
00905 };
00906 
00907 /// SwitchStmt - This represents a 'switch' stmt.
00908 ///
00909 class SwitchStmt : public Stmt {
00910   enum { VAR, COND, BODY, END_EXPR };
00911   Stmt* SubExprs[END_EXPR];
00912   // This points to a linked list of case and default statements.
00913   SwitchCase *FirstCase;
00914   SourceLocation SwitchLoc;
00915 
00916   /// If the SwitchStmt is a switch on an enum value, this records whether
00917   /// all the enum values were covered by CaseStmts.  This value is meant to
00918   /// be a hint for possible clients.
00919   unsigned AllEnumCasesCovered : 1;
00920 
00921 public:
00922   SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
00923 
00924   /// \brief Build a empty switch statement.
00925   explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
00926 
00927   /// \brief Retrieve the variable declared in this "switch" statement, if any.
00928   ///
00929   /// In the following example, "x" is the condition variable.
00930   /// \code
00931   /// switch (int x = foo()) {
00932   ///   case 0: break;
00933   ///   // ...
00934   /// }
00935   /// \endcode
00936   VarDecl *getConditionVariable() const;
00937   void setConditionVariable(ASTContext &C, VarDecl *V);
00938 
00939   /// If this SwitchStmt has a condition variable, return the faux DeclStmt
00940   /// associated with the creation of that condition variable.
00941   const DeclStmt *getConditionVariableDeclStmt() const {
00942     return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
00943   }
00944 
00945   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
00946   const Stmt *getBody() const { return SubExprs[BODY]; }
00947   const SwitchCase *getSwitchCaseList() const { return FirstCase; }
00948 
00949   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
00950   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
00951   Stmt *getBody() { return SubExprs[BODY]; }
00952   void setBody(Stmt *S) { SubExprs[BODY] = S; }
00953   SwitchCase *getSwitchCaseList() { return FirstCase; }
00954 
00955   /// \brief Set the case list for this switch statement.
00956   ///
00957   /// The caller is responsible for incrementing the retain counts on
00958   /// all of the SwitchCase statements in this list.
00959   void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
00960 
00961   SourceLocation getSwitchLoc() const { return SwitchLoc; }
00962   void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
00963 
00964   void setBody(Stmt *S, SourceLocation SL) {
00965     SubExprs[BODY] = S;
00966     SwitchLoc = SL;
00967   }
00968   void addSwitchCase(SwitchCase *SC) {
00969     assert(!SC->getNextSwitchCase()
00970            && "case/default already added to a switch");
00971     SC->setNextSwitchCase(FirstCase);
00972     FirstCase = SC;
00973   }
00974 
00975   /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
00976   /// switch over an enum value then all cases have been explicitly covered.
00977   void setAllEnumCasesCovered() {
00978     AllEnumCasesCovered = 1;
00979   }
00980 
00981   /// Returns true if the SwitchStmt is a switch of an enum value and all cases
00982   /// have been explicitly covered.
00983   bool isAllEnumCasesCovered() const {
00984     return (bool) AllEnumCasesCovered;
00985   }
00986 
00987   SourceRange getSourceRange() const LLVM_READONLY {
00988     return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
00989   }
00990   // Iterators
00991   child_range children() {
00992     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
00993   }
00994 
00995   static bool classof(const Stmt *T) {
00996     return T->getStmtClass() == SwitchStmtClass;
00997   }
00998   static bool classof(const SwitchStmt *) { return true; }
00999 };
01000 
01001 
01002 /// WhileStmt - This represents a 'while' stmt.
01003 ///
01004 class WhileStmt : public Stmt {
01005   enum { VAR, COND, BODY, END_EXPR };
01006   Stmt* SubExprs[END_EXPR];
01007   SourceLocation WhileLoc;
01008 public:
01009   WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
01010             SourceLocation WL);
01011 
01012   /// \brief Build an empty while statement.
01013   explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
01014 
01015   /// \brief Retrieve the variable declared in this "while" statement, if any.
01016   ///
01017   /// In the following example, "x" is the condition variable.
01018   /// \code
01019   /// while (int x = random()) {
01020   ///   // ...
01021   /// }
01022   /// \endcode
01023   VarDecl *getConditionVariable() const;
01024   void setConditionVariable(ASTContext &C, VarDecl *V);
01025 
01026   /// If this WhileStmt has a condition variable, return the faux DeclStmt
01027   /// associated with the creation of that condition variable.
01028   const DeclStmt *getConditionVariableDeclStmt() const {
01029     return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
01030   }
01031 
01032   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
01033   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
01034   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
01035   Stmt *getBody() { return SubExprs[BODY]; }
01036   const Stmt *getBody() const { return SubExprs[BODY]; }
01037   void setBody(Stmt *S) { SubExprs[BODY] = S; }
01038 
01039   SourceLocation getWhileLoc() const { return WhileLoc; }
01040   void setWhileLoc(SourceLocation L) { WhileLoc = L; }
01041 
01042   SourceRange getSourceRange() const LLVM_READONLY {
01043     return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
01044   }
01045   static bool classof(const Stmt *T) {
01046     return T->getStmtClass() == WhileStmtClass;
01047   }
01048   static bool classof(const WhileStmt *) { return true; }
01049 
01050   // Iterators
01051   child_range children() {
01052     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
01053   }
01054 };
01055 
01056 /// DoStmt - This represents a 'do/while' stmt.
01057 ///
01058 class DoStmt : public Stmt {
01059   enum { BODY, COND, END_EXPR };
01060   Stmt* SubExprs[END_EXPR];
01061   SourceLocation DoLoc;
01062   SourceLocation WhileLoc;
01063   SourceLocation RParenLoc;  // Location of final ')' in do stmt condition.
01064 
01065 public:
01066   DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
01067          SourceLocation RP)
01068     : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
01069     SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
01070     SubExprs[BODY] = body;
01071   }
01072 
01073   /// \brief Build an empty do-while statement.
01074   explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
01075 
01076   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
01077   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
01078   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
01079   Stmt *getBody() { return SubExprs[BODY]; }
01080   const Stmt *getBody() const { return SubExprs[BODY]; }
01081   void setBody(Stmt *S) { SubExprs[BODY] = S; }
01082 
01083   SourceLocation getDoLoc() const { return DoLoc; }
01084   void setDoLoc(SourceLocation L) { DoLoc = L; }
01085   SourceLocation getWhileLoc() const { return WhileLoc; }
01086   void setWhileLoc(SourceLocation L) { WhileLoc = L; }
01087 
01088   SourceLocation getRParenLoc() const { return RParenLoc; }
01089   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
01090 
01091   SourceRange getSourceRange() const LLVM_READONLY {
01092     return SourceRange(DoLoc, RParenLoc);
01093   }
01094   static bool classof(const Stmt *T) {
01095     return T->getStmtClass() == DoStmtClass;
01096   }
01097   static bool classof(const DoStmt *) { return true; }
01098 
01099   // Iterators
01100   child_range children() {
01101     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
01102   }
01103 };
01104 
01105 
01106 /// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
01107 /// the init/cond/inc parts of the ForStmt will be null if they were not
01108 /// specified in the source.
01109 ///
01110 class ForStmt : public Stmt {
01111   enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
01112   Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
01113   SourceLocation ForLoc;
01114   SourceLocation LParenLoc, RParenLoc;
01115 
01116 public:
01117   ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
01118           Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
01119 
01120   /// \brief Build an empty for statement.
01121   explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
01122 
01123   Stmt *getInit() { return SubExprs[INIT]; }
01124 
01125   /// \brief Retrieve the variable declared in this "for" statement, if any.
01126   ///
01127   /// In the following example, "y" is the condition variable.
01128   /// \code
01129   /// for (int x = random(); int y = mangle(x); ++x) {
01130   ///   // ...
01131   /// }
01132   /// \endcode
01133   VarDecl *getConditionVariable() const;
01134   void setConditionVariable(ASTContext &C, VarDecl *V);
01135 
01136   /// If this ForStmt has a condition variable, return the faux DeclStmt
01137   /// associated with the creation of that condition variable.
01138   const DeclStmt *getConditionVariableDeclStmt() const {
01139     return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
01140   }
01141 
01142   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
01143   Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
01144   Stmt *getBody() { return SubExprs[BODY]; }
01145 
01146   const Stmt *getInit() const { return SubExprs[INIT]; }
01147   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
01148   const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
01149   const Stmt *getBody() const { return SubExprs[BODY]; }
01150 
01151   void setInit(Stmt *S) { SubExprs[INIT] = S; }
01152   void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
01153   void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
01154   void setBody(Stmt *S) { SubExprs[BODY] = S; }
01155 
01156   SourceLocation getForLoc() const { return ForLoc; }
01157   void setForLoc(SourceLocation L) { ForLoc = L; }
01158   SourceLocation getLParenLoc() const { return LParenLoc; }
01159   void setLParenLoc(SourceLocation L) { LParenLoc = L; }
01160   SourceLocation getRParenLoc() const { return RParenLoc; }
01161   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
01162 
01163   SourceRange getSourceRange() const LLVM_READONLY {
01164     return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
01165   }
01166   static bool classof(const Stmt *T) {
01167     return T->getStmtClass() == ForStmtClass;
01168   }
01169   static bool classof(const ForStmt *) { return true; }
01170 
01171   // Iterators
01172   child_range children() {
01173     return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
01174   }
01175 };
01176 
01177 /// GotoStmt - This represents a direct goto.
01178 ///
01179 class GotoStmt : public Stmt {
01180   LabelDecl *Label;
01181   SourceLocation GotoLoc;
01182   SourceLocation LabelLoc;
01183 public:
01184   GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
01185     : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
01186 
01187   /// \brief Build an empty goto statement.
01188   explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
01189 
01190   LabelDecl *getLabel() const { return Label; }
01191   void setLabel(LabelDecl *D) { Label = D; }
01192 
01193   SourceLocation getGotoLoc() const { return GotoLoc; }
01194   void setGotoLoc(SourceLocation L) { GotoLoc = L; }
01195   SourceLocation getLabelLoc() const { return LabelLoc; }
01196   void setLabelLoc(SourceLocation L) { LabelLoc = L; }
01197 
01198   SourceRange getSourceRange() const LLVM_READONLY {
01199     return SourceRange(GotoLoc, LabelLoc);
01200   }
01201   static bool classof(const Stmt *T) {
01202     return T->getStmtClass() == GotoStmtClass;
01203   }
01204   static bool classof(const GotoStmt *) { return true; }
01205 
01206   // Iterators
01207   child_range children() { return child_range(); }
01208 };
01209 
01210 /// IndirectGotoStmt - This represents an indirect goto.
01211 ///
01212 class IndirectGotoStmt : public Stmt {
01213   SourceLocation GotoLoc;
01214   SourceLocation StarLoc;
01215   Stmt *Target;
01216 public:
01217   IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
01218                    Expr *target)
01219     : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
01220       Target((Stmt*)target) {}
01221 
01222   /// \brief Build an empty indirect goto statement.
01223   explicit IndirectGotoStmt(EmptyShell Empty)
01224     : Stmt(IndirectGotoStmtClass, Empty) { }
01225 
01226   void setGotoLoc(SourceLocation L) { GotoLoc = L; }
01227   SourceLocation getGotoLoc() const { return GotoLoc; }
01228   void setStarLoc(SourceLocation L) { StarLoc = L; }
01229   SourceLocation getStarLoc() const { return StarLoc; }
01230 
01231   Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
01232   const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
01233   void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
01234 
01235   /// getConstantTarget - Returns the fixed target of this indirect
01236   /// goto, if one exists.
01237   LabelDecl *getConstantTarget();
01238   const LabelDecl *getConstantTarget() const {
01239     return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
01240   }
01241 
01242   SourceRange getSourceRange() const LLVM_READONLY {
01243     return SourceRange(GotoLoc, Target->getLocEnd());
01244   }
01245 
01246   static bool classof(const Stmt *T) {
01247     return T->getStmtClass() == IndirectGotoStmtClass;
01248   }
01249   static bool classof(const IndirectGotoStmt *) { return true; }
01250 
01251   // Iterators
01252   child_range children() { return child_range(&Target, &Target+1); }
01253 };
01254 
01255 
01256 /// ContinueStmt - This represents a continue.
01257 ///
01258 class ContinueStmt : public Stmt {
01259   SourceLocation ContinueLoc;
01260 public:
01261   ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
01262 
01263   /// \brief Build an empty continue statement.
01264   explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
01265 
01266   SourceLocation getContinueLoc() const { return ContinueLoc; }
01267   void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
01268 
01269   SourceRange getSourceRange() const LLVM_READONLY {
01270     return SourceRange(ContinueLoc);
01271   }
01272 
01273   static bool classof(const Stmt *T) {
01274     return T->getStmtClass() == ContinueStmtClass;
01275   }
01276   static bool classof(const ContinueStmt *) { return true; }
01277 
01278   // Iterators
01279   child_range children() { return child_range(); }
01280 };
01281 
01282 /// BreakStmt - This represents a break.
01283 ///
01284 class BreakStmt : public Stmt {
01285   SourceLocation BreakLoc;
01286 public:
01287   BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
01288 
01289   /// \brief Build an empty break statement.
01290   explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
01291 
01292   SourceLocation getBreakLoc() const { return BreakLoc; }
01293   void setBreakLoc(SourceLocation L) { BreakLoc = L; }
01294 
01295   SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(BreakLoc); }
01296 
01297   static bool classof(const Stmt *T) {
01298     return T->getStmtClass() == BreakStmtClass;
01299   }
01300   static bool classof(const BreakStmt *) { return true; }
01301 
01302   // Iterators
01303   child_range children() { return child_range(); }
01304 };
01305 
01306 
01307 /// ReturnStmt - This represents a return, optionally of an expression:
01308 ///   return;
01309 ///   return 4;
01310 ///
01311 /// Note that GCC allows return with no argument in a function declared to
01312 /// return a value, and it allows returning a value in functions declared to
01313 /// return void.  We explicitly model this in the AST, which means you can't
01314 /// depend on the return type of the function and the presence of an argument.
01315 ///
01316 class ReturnStmt : public Stmt {
01317   Stmt *RetExpr;
01318   SourceLocation RetLoc;
01319   const VarDecl *NRVOCandidate;
01320 
01321 public:
01322   ReturnStmt(SourceLocation RL)
01323     : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
01324 
01325   ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
01326     : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
01327       NRVOCandidate(NRVOCandidate) {}
01328 
01329   /// \brief Build an empty return expression.
01330   explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
01331 
01332   const Expr *getRetValue() const;
01333   Expr *getRetValue();
01334   void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
01335 
01336   SourceLocation getReturnLoc() const { return RetLoc; }
01337   void setReturnLoc(SourceLocation L) { RetLoc = L; }
01338 
01339   /// \brief Retrieve the variable that might be used for the named return
01340   /// value optimization.
01341   ///
01342   /// The optimization itself can only be performed if the variable is
01343   /// also marked as an NRVO object.
01344   const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
01345   void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
01346 
01347   SourceRange getSourceRange() const LLVM_READONLY;
01348 
01349   static bool classof(const Stmt *T) {
01350     return T->getStmtClass() == ReturnStmtClass;
01351   }
01352   static bool classof(const ReturnStmt *) { return true; }
01353 
01354   // Iterators
01355   child_range children() {
01356     if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
01357     return child_range();
01358   }
01359 };
01360 
01361 /// AsmStmt - This represents a GNU inline-assembly statement extension.
01362 ///
01363 class AsmStmt : public Stmt {
01364   SourceLocation AsmLoc, RParenLoc;
01365   StringLiteral *AsmStr;
01366 
01367   bool IsSimple;
01368   bool IsVolatile;
01369   bool MSAsm;
01370 
01371   unsigned NumOutputs;
01372   unsigned NumInputs;
01373   unsigned NumClobbers;
01374 
01375   // FIXME: If we wanted to, we could allocate all of these in one big array.
01376   IdentifierInfo **Names;
01377   StringLiteral **Constraints;
01378   Stmt **Exprs;
01379   StringLiteral **Clobbers;
01380 
01381 public:
01382   AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
01383           bool msasm, unsigned numoutputs, unsigned numinputs,
01384           IdentifierInfo **names, StringLiteral **constraints,
01385           Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
01386           StringLiteral **clobbers, SourceLocation rparenloc);
01387 
01388   /// \brief Build an empty inline-assembly statement.
01389   explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
01390     Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
01391 
01392   SourceLocation getAsmLoc() const { return AsmLoc; }
01393   void setAsmLoc(SourceLocation L) { AsmLoc = L; }
01394   SourceLocation getRParenLoc() const { return RParenLoc; }
01395   void setRParenLoc(SourceLocation L) { RParenLoc = L; }
01396 
01397   bool isVolatile() const { return IsVolatile; }
01398   void setVolatile(bool V) { IsVolatile = V; }
01399   bool isSimple() const { return IsSimple; }
01400   void setSimple(bool V) { IsSimple = V; }
01401   bool isMSAsm() const { return MSAsm; }
01402   void setMSAsm(bool V) { MSAsm = V; }
01403 
01404   //===--- Asm String Analysis ---===//
01405 
01406   const StringLiteral *getAsmString() const { return AsmStr; }
01407   StringLiteral *getAsmString() { return AsmStr; }
01408   void setAsmString(StringLiteral *E) { AsmStr = E; }
01409 
01410   /// AsmStringPiece - this is part of a decomposed asm string specification
01411   /// (for use with the AnalyzeAsmString function below).  An asm string is
01412   /// considered to be a concatenation of these parts.
01413   class AsmStringPiece {
01414   public:
01415     enum Kind {
01416       String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
01417       Operand  // Operand reference, with optional modifier %c4.
01418     };
01419   private:
01420     Kind MyKind;
01421     std::string Str;
01422     unsigned OperandNo;
01423   public:
01424     AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
01425     AsmStringPiece(unsigned OpNo, char Modifier)
01426       : MyKind(Operand), Str(), OperandNo(OpNo) {
01427       Str += Modifier;
01428     }
01429 
01430     bool isString() const { return MyKind == String; }
01431     bool isOperand() const { return MyKind == Operand; }
01432 
01433     const std::string &getString() const {
01434       assert(isString());
01435       return Str;
01436     }
01437 
01438     unsigned getOperandNo() const {
01439       assert(isOperand());
01440       return OperandNo;
01441     }
01442 
01443     /// getModifier - Get the modifier for this operand, if present.  This
01444     /// returns '\0' if there was no modifier.
01445     char getModifier() const {
01446       assert(isOperand());
01447       return Str[0];
01448     }
01449   };
01450 
01451   /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
01452   /// it into pieces.  If the asm string is erroneous, emit errors and return
01453   /// true, otherwise return false.  This handles canonicalization and
01454   /// translation of strings from GCC syntax to LLVM IR syntax, and handles
01455   //// flattening of named references like %[foo] to Operand AsmStringPiece's.
01456   unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
01457                             ASTContext &C, unsigned &DiagOffs) const;
01458 
01459 
01460   //===--- Output operands ---===//
01461 
01462   unsigned getNumOutputs() const { return NumOutputs; }
01463 
01464   IdentifierInfo *getOutputIdentifier(unsigned i) const {
01465     return Names[i];
01466   }
01467 
01468   StringRef getOutputName(unsigned i) const {
01469     if (IdentifierInfo *II = getOutputIdentifier(i))
01470       return II->getName();
01471 
01472     return StringRef();
01473   }
01474 
01475   /// getOutputConstraint - Return the constraint string for the specified
01476   /// output operand.  All output constraints are known to be non-empty (either
01477   /// '=' or '+').
01478   StringRef getOutputConstraint(unsigned i) const;
01479 
01480   const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
01481     return Constraints[i];
01482   }
01483   StringLiteral *getOutputConstraintLiteral(unsigned i) {
01484     return Constraints[i];
01485   }
01486 
01487   Expr *getOutputExpr(unsigned i);
01488 
01489   const Expr *getOutputExpr(unsigned i) const {
01490     return const_cast<AsmStmt*>(this)->getOutputExpr(i);
01491   }
01492 
01493   /// isOutputPlusConstraint - Return true if the specified output constraint
01494   /// is a "+" constraint (which is both an input and an output) or false if it
01495   /// is an "=" constraint (just an output).
01496   bool isOutputPlusConstraint(unsigned i) const {
01497     return getOutputConstraint(i)[0] == '+';
01498   }
01499 
01500   /// getNumPlusOperands - Return the number of output operands that have a "+"
01501   /// constraint.
01502   unsigned getNumPlusOperands() const;
01503 
01504   //===--- Input operands ---===//
01505 
01506   unsigned getNumInputs() const { return NumInputs; }
01507 
01508   IdentifierInfo *getInputIdentifier(unsigned i) const {
01509     return Names[i + NumOutputs];
01510   }
01511 
01512   StringRef getInputName(unsigned i) const {
01513     if (IdentifierInfo *II = getInputIdentifier(i))
01514       return II->getName();
01515 
01516     return StringRef();
01517   }
01518 
01519   /// getInputConstraint - Return the specified input constraint.  Unlike output
01520   /// constraints, these can be empty.
01521   StringRef getInputConstraint(unsigned i) const;
01522 
01523   const StringLiteral *getInputConstraintLiteral(unsigned i) const {
01524     return Constraints[i + NumOutputs];
01525   }
01526   StringLiteral *getInputConstraintLiteral(unsigned i) {
01527     return Constraints[i + NumOutputs];
01528   }
01529 
01530   Expr *getInputExpr(unsigned i);
01531   void setInputExpr(unsigned i, Expr *E);
01532 
01533   const Expr *getInputExpr(unsigned i) const {
01534     return const_cast<AsmStmt*>(this)->getInputExpr(i);
01535   }
01536 
01537   void setOutputsAndInputsAndClobbers(ASTContext &C,
01538                                       IdentifierInfo **Names,
01539                                       StringLiteral **Constraints,
01540                                       Stmt **Exprs,
01541                                       unsigned NumOutputs,
01542                                       unsigned NumInputs,
01543                                       StringLiteral **Clobbers,
01544                                       unsigned NumClobbers);
01545 
01546   //===--- Other ---===//
01547 
01548   /// getNamedOperand - Given a symbolic operand reference like %[foo],
01549   /// translate this into a numeric value needed to reference the same operand.
01550   /// This returns -1 if the operand name is invalid.
01551   int getNamedOperand(StringRef SymbolicName) const;
01552 
01553   unsigned getNumClobbers() const { return NumClobbers; }
01554   StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
01555   const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
01556 
01557   SourceRange getSourceRange() const LLVM_READONLY {
01558     return SourceRange(AsmLoc, RParenLoc);
01559   }
01560 
01561   static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
01562   static bool classof(const AsmStmt *) { return true; }
01563 
01564   // Input expr iterators.
01565 
01566   typedef ExprIterator inputs_iterator;
01567   typedef ConstExprIterator const_inputs_iterator;
01568 
01569   inputs_iterator begin_inputs() {
01570     return &Exprs[0] + NumOutputs;
01571   }
01572 
01573   inputs_iterator end_inputs() {
01574     return &Exprs[0] + NumOutputs + NumInputs;
01575   }
01576 
01577   const_inputs_iterator begin_inputs() const {
01578     return &Exprs[0] + NumOutputs;
01579   }
01580 
01581   const_inputs_iterator end_inputs() const {
01582     return &Exprs[0] + NumOutputs + NumInputs;
01583   }
01584 
01585   // Output expr iterators.
01586 
01587   typedef ExprIterator outputs_iterator;
01588   typedef ConstExprIterator const_outputs_iterator;
01589 
01590   outputs_iterator begin_outputs() {
01591     return &Exprs[0];
01592   }
01593   outputs_iterator end_outputs() {
01594     return &Exprs[0] + NumOutputs;
01595   }
01596 
01597   const_outputs_iterator begin_outputs() const {
01598     return &Exprs[0];
01599   }
01600   const_outputs_iterator end_outputs() const {
01601     return &Exprs[0] + NumOutputs;
01602   }
01603 
01604   child_range children() {
01605     return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
01606   }
01607 };
01608 
01609 class SEHExceptStmt : public Stmt {
01610   SourceLocation  Loc;
01611   Stmt           *Children[2];
01612 
01613   enum { FILTER_EXPR, BLOCK };
01614 
01615   SEHExceptStmt(SourceLocation Loc,
01616                 Expr *FilterExpr,
01617                 Stmt *Block);
01618 
01619   friend class ASTReader;
01620   friend class ASTStmtReader;
01621   explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
01622 
01623 public:
01624   static SEHExceptStmt* Create(ASTContext &C,
01625                                SourceLocation ExceptLoc,
01626                                Expr *FilterExpr,
01627                                Stmt *Block);
01628   SourceRange getSourceRange() const LLVM_READONLY {
01629     return SourceRange(getExceptLoc(), getEndLoc());
01630   }
01631 
01632   SourceLocation getExceptLoc() const { return Loc; }
01633   SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
01634 
01635   Expr *getFilterExpr() const {
01636     return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
01637   }
01638 
01639   CompoundStmt *getBlock() const {
01640     return llvm::cast<CompoundStmt>(Children[BLOCK]);
01641   }
01642 
01643   child_range children() {
01644     return child_range(Children,Children+2);
01645   }
01646 
01647   static bool classof(const Stmt *T) {
01648     return T->getStmtClass() == SEHExceptStmtClass;
01649   }
01650 
01651   static bool classof(SEHExceptStmt *) { return true; }
01652 
01653 };
01654 
01655 class SEHFinallyStmt : public Stmt {
01656   SourceLocation  Loc;
01657   Stmt           *Block;
01658 
01659   SEHFinallyStmt(SourceLocation Loc,
01660                  Stmt *Block);
01661 
01662   friend class ASTReader;
01663   friend class ASTStmtReader;
01664   explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
01665 
01666 public:
01667   static SEHFinallyStmt* Create(ASTContext &C,
01668                                 SourceLocation FinallyLoc,
01669                                 Stmt *Block);
01670 
01671   SourceRange getSourceRange() const LLVM_READONLY {
01672     return SourceRange(getFinallyLoc(), getEndLoc());
01673   }
01674 
01675   SourceLocation getFinallyLoc() const { return Loc; }
01676   SourceLocation getEndLoc() const { return Block->getLocEnd(); }
01677 
01678   CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
01679 
01680   child_range children() {
01681     return child_range(&Block,&Block+1);
01682   }
01683 
01684   static bool classof(const Stmt *T) {
01685     return T->getStmtClass() == SEHFinallyStmtClass;
01686   }
01687 
01688   static bool classof(SEHFinallyStmt *) { return true; }
01689 
01690 };
01691 
01692 class SEHTryStmt : public Stmt {
01693   bool            IsCXXTry;
01694   SourceLocation  TryLoc;
01695   Stmt           *Children[2];
01696 
01697   enum { TRY = 0, HANDLER = 1 };
01698 
01699   SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
01700              SourceLocation TryLoc,
01701              Stmt *TryBlock,
01702              Stmt *Handler);
01703 
01704   friend class ASTReader;
01705   friend class ASTStmtReader;
01706   explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
01707 
01708 public:
01709   static SEHTryStmt* Create(ASTContext &C,
01710                             bool isCXXTry,
01711                             SourceLocation TryLoc,
01712                             Stmt *TryBlock,
01713                             Stmt *Handler);
01714 
01715   SourceRange getSourceRange() const LLVM_READONLY {
01716     return SourceRange(getTryLoc(), getEndLoc());
01717   }
01718 
01719   SourceLocation getTryLoc() const { return TryLoc; }
01720   SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
01721 
01722   bool getIsCXXTry() const { return IsCXXTry; }
01723 
01724   CompoundStmt* getTryBlock() const {
01725     return llvm::cast<CompoundStmt>(Children[TRY]);
01726   }
01727 
01728   Stmt *getHandler() const { return Children[HANDLER]; }
01729 
01730   /// Returns 0 if not defined
01731   SEHExceptStmt  *getExceptHandler() const;
01732   SEHFinallyStmt *getFinallyHandler() const;
01733 
01734   child_range children() {
01735     return child_range(Children,Children+2);
01736   }
01737 
01738   static bool classof(const Stmt *T) {
01739     return T->getStmtClass() == SEHTryStmtClass;
01740   }
01741 
01742   static bool classof(SEHTryStmt *) { return true; }
01743 };
01744 
01745 }  // end namespace clang
01746 
01747 #endif