15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the Stmt interface and subclasses.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_AST_STMT_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_AST_STMT_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
172fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/DeclGroup.h"
189caf8b1ca6beb254f420dada3c0e94d5ef027f58Ted Kremenek#include "clang/AST/StmtIterator.h"
199fd6b8f5a73788f288edd01fa99d434d1e6588adWei Pan#include "clang/Basic/CapturedStmt.h"
202fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/Basic/IdentifierTable.h"
212fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/Basic/LLVM.h"
222fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/Basic/SourceLocation.h"
232fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "llvm/ADT/ArrayRef.h"
243a2f91280a49f4747063f983dc6a3296bd9359d2Ben Langmuir#include "llvm/ADT/PointerIntPair.h"
25aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar#include "llvm/Support/Compiler.h"
26a93d0f280693b8418bc88cf7a8c93325f7fcf4c6Benjamin Kramer#include "llvm/Support/ErrorHandling.h"
276e340496341a4704be0ede9c1ff4f8eacea7ee2cChris Lattner#include <string>
287573098b83e780d1c5bea13b384b610d8f155676Steve Naroff
2941ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregornamespace llvm {
3041ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor  class FoldingSetNodeID;
3141ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor}
3241ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
34e2563ca02a519c2ad6d64dfed87d6e86c5d3c072Sam Bishop  class ASTContext;
352fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  class Attr;
366afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj  class CapturedDecl;
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class Decl;
382fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  class Expr;
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class IdentifierInfo;
40478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  class LabelDecl;
412fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  class ParmVarDecl;
42a93d0f280693b8418bc88cf7a8c93325f7fcf4c6Benjamin Kramer  class PrinterHelper;
43a93d0f280693b8418bc88cf7a8c93325f7fcf4c6Benjamin Kramer  struct PrintingPolicy;
442fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  class QualType;
45051303ce09291dfbed537fa33b0d8a4d92c82b75Tareq A. Siraj  class RecordDecl;
460c727a35718556866a978f64ac549d9798735f08Chris Lattner  class SourceManager;
476a0ef4b83c91a6d6d5acb4ed5577c4659fe022a3Anders Carlsson  class StringLiteral;
486c36be5b383875b490684bcf439d6d427298c1afChris Lattner  class SwitchStmt;
49a93d0f280693b8418bc88cf7a8c93325f7fcf4c6Benjamin Kramer  class Token;
50478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer  class VarDecl;
511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  //===--------------------------------------------------------------------===//
53ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  // ExprIterator - Iterators for iterating over Stmt* arrays that contain
54ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  //  only Expr*.  This is needed because AST nodes use Stmt* arrays to store
55ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  //  references to children (to be compatible with StmtIterator).
56ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  //===--------------------------------------------------------------------===//
571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  class Stmt;
59ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  class Expr;
601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  class ExprIterator {
62ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    Stmt** I;
63ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  public:
64ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    ExprIterator(Stmt** i) : I(i) {}
656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ExprIterator() : I(nullptr) {}
66ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    ExprIterator& operator++() { ++I; return *this; }
67ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    ExprIterator operator-(size_t i) { return I-i; }
68ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    ExprIterator operator+(size_t i) { return I+i; }
69ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    Expr* operator[](size_t idx);
70ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    // FIXME: Verify that this will correctly return a signed distance.
71ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    signed operator-(const ExprIterator& R) const { return I - R.I; }
72ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    Expr* operator*() const;
73ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    Expr* operator->() const;
74ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    bool operator==(const ExprIterator& R) const { return I == R.I; }
75ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    bool operator!=(const ExprIterator& R) const { return I != R.I; }
76ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    bool operator>(const ExprIterator& R) const { return I > R.I; }
77ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    bool operator>=(const ExprIterator& R) const { return I >= R.I; }
78ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  };
791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  class ConstExprIterator {
811705fe9ec0efb65f77a46e669e48302923204fe8Benjamin Kramer    const Stmt * const *I;
82ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek  public:
831705fe9ec0efb65f77a46e669e48302923204fe8Benjamin Kramer    ConstExprIterator(const Stmt * const *i) : I(i) {}
846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ConstExprIterator() : I(nullptr) {}
85ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    ConstExprIterator& operator++() { ++I; return *this; }
861705fe9ec0efb65f77a46e669e48302923204fe8Benjamin Kramer    ConstExprIterator operator+(size_t i) const { return I+i; }
871705fe9ec0efb65f77a46e669e48302923204fe8Benjamin Kramer    ConstExprIterator operator-(size_t i) const { return I-i; }
88ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    const Expr * operator[](size_t idx) const;
89ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    signed operator-(const ConstExprIterator& R) const { return I - R.I; }
90ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    const Expr * operator*() const;
91ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    const Expr * operator->() const;
92ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    bool operator==(const ConstExprIterator& R) const { return I == R.I; }
93ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
94ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    bool operator>(const ConstExprIterator& R) const { return I > R.I; }
95ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek    bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  };
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
98ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
99ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek// AST classes for statements.
100ce2fc3a343ea6098a96d587071cee7299f11957aTed Kremenek//===----------------------------------------------------------------------===//
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stmt - This represents one statement.
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass Stmt {
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  enum StmtClass {
107f2cad8633e46cce12fc3d77c0bd451ffd7264bafDouglas Gregor    NoStmtClass = 0,
108f2cad8633e46cce12fc3d77c0bd451ffd7264bafDouglas Gregor#define STMT(CLASS, PARENT) CLASS##Class,
1097381d5cfbd599fa2b9e215011ad7cbd449de231aSean Hunt#define STMT_RANGE(BASE, FIRST, LAST) \
1109a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
1117381d5cfbd599fa2b9e215011ad7cbd449de231aSean Hunt#define LAST_STMT_RANGE(BASE, FIRST, LAST) \
1129a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
1137381d5cfbd599fa2b9e215011ad7cbd449de231aSean Hunt#define ABSTRACT_STMT(STMT)
1144bfe1968410ea8ffe3b4f629addd7c4bcf484765Sean Hunt#include "clang/AST/StmtNodes.inc"
1158e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  };
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1178189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  // Make vanilla 'new' and 'delete' illegal for Stmts.
1188189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenekprotected:
1198189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  void* operator new(size_t bytes) throw() {
120b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
1218189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  }
1228189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  void operator delete(void* data) throw() {
123b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Stmts cannot be released with regular 'delete'.");
1248189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  }
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1268e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  class StmtBitfields {
1278e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    friend class Stmt;
1288e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall
1298e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    /// \brief The statement class.
1308e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    unsigned sClass : 8;
1318e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  };
1328e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  enum { NumStmtBits = 8 };
1338e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall
1348e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  class CompoundStmtBitfields {
1358e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    friend class CompoundStmt;
1368e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    unsigned : NumStmtBits;
1378e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall
1388e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    unsigned NumStmts : 32 - NumStmtBits;
1398e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  };
1408e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall
1418e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  class ExprBitfields {
1428e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    friend class Expr;
1438e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    friend class DeclRefExpr; // computeDependence
1448e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    friend class InitListExpr; // ctor
1458e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    friend class DesignatedInitExpr; // ctor
146d967e31ee796efff24b84b704a063634f6b55627Douglas Gregor    friend class BlockDeclRefExpr; // ctor
147bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class ASTStmtReader; // deserialization
148bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class CXXNewExpr; // ctor
149bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class DependentScopeDeclRefExpr; // ctor
150bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class CXXConstructExpr; // ctor
151bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class CallExpr; // ctor
152bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class OffsetOfExpr; // ctor
153bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class ObjCMessageExpr; // ctor
154ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    friend class ObjCArrayLiteral; // ctor
155ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    friend class ObjCDictionaryLiteral; // ctor
156bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class ShuffleVectorExpr; // ctor
157bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class ParenListExpr; // ctor
158bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class CXXUnresolvedConstructExpr; // ctor
159bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class CXXDependentScopeMemberExpr; // ctor
160bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor    friend class OverloadExpr; // ctor
1614b9c2d235fb9449e249d74f48ecfec601650de93John McCall    friend class PseudoObjectExpr; // ctor
162dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman    friend class AtomicExpr; // ctor
1638e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    unsigned : NumStmtBits;
1648e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall
1658e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    unsigned ValueKind : 2;
166f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    unsigned ObjectKind : 2;
1678e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    unsigned TypeDependent : 1;
1688e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    unsigned ValueDependent : 1;
169561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    unsigned InstantiationDependent : 1;
170d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    unsigned ContainsUnexpandedParameterPack : 1;
1718e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  };
172561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  enum { NumExprBits = 16 };
1738e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall
174bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer  class CharacterLiteralBitfields {
175bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    friend class CharacterLiteral;
176bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    unsigned : NumExprBits;
177bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer
178bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    unsigned Kind : 2;
179bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer  };
180bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer
1819ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  enum APFloatSemantics {
1829ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    IEEEhalf,
1839ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    IEEEsingle,
1849ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    IEEEdouble,
1859ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    x87DoubleExtended,
1869ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    IEEEquad,
1879ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    PPCDoubleDouble
1889ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover  };
1899ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover
190bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer  class FloatingLiteralBitfields {
191bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    friend class FloatingLiteral;
192bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    unsigned : NumExprBits;
193bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer
1949ec55f24b8f848bb37a9971100cf2fca379d5572Tim Northover    unsigned Semantics : 3; // Provides semantics for APFloat construction
195bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    unsigned IsExact : 1;
196bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer  };
197bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer
198bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer  class UnaryExprOrTypeTraitExprBitfields {
199bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    friend class UnaryExprOrTypeTraitExpr;
200bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    unsigned : NumExprBits;
201bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer
202bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    unsigned Kind : 2;
203bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    unsigned IsType : 1; // true if operand is a type, false if an expression.
204bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer  };
205bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer
206cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth  class DeclRefExprBitfields {
207cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth    friend class DeclRefExpr;
208cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth    friend class ASTStmtReader; // deserialization
209cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth    unsigned : NumExprBits;
210cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth
211cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth    unsigned HasQualifier : 1;
212e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    unsigned HasTemplateKWAndArgsInfo : 1;
2133aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    unsigned HasFoundDecl : 1;
2147cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara    unsigned HadMultipleCandidates : 1;
215f4b88a45902af1802a1cb42ba48b1c474474f228John McCall    unsigned RefersToEnclosingLocal : 1;
216cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth  };
217cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth
2188e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  class CastExprBitfields {
2198e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    friend class CastExpr;
2208e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    unsigned : NumExprBits;
2218e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall
2222bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    unsigned Kind : 6;
2232bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall    unsigned BasePathSize : 32 - 6 - NumExprBits;
2248e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  };
2258e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall
226cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  class CallExprBitfields {
227cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    friend class CallExpr;
228cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    unsigned : NumExprBits;
229cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne
230cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    unsigned NumPreArgs : 1;
231cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne  };
232cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne
23380ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  class ExprWithCleanupsBitfields {
23480ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    friend class ExprWithCleanups;
23580ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    friend class ASTStmtReader; // deserialization
23680ee6e878a169e6255d4686a91bb696151ff229fJohn McCall
23780ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    unsigned : NumExprBits;
23880ee6e878a169e6255d4686a91bb696151ff229fJohn McCall
23980ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    unsigned NumObjects : 32 - NumExprBits;
24080ee6e878a169e6255d4686a91bb696151ff229fJohn McCall  };
24180ee6e878a169e6255d4686a91bb696151ff229fJohn McCall
2424b9c2d235fb9449e249d74f48ecfec601650de93John McCall  class PseudoObjectExprBitfields {
2434b9c2d235fb9449e249d74f48ecfec601650de93John McCall    friend class PseudoObjectExpr;
2444b9c2d235fb9449e249d74f48ecfec601650de93John McCall    friend class ASTStmtReader; // deserialization
2454b9c2d235fb9449e249d74f48ecfec601650de93John McCall
2464b9c2d235fb9449e249d74f48ecfec601650de93John McCall    unsigned : NumExprBits;
2474b9c2d235fb9449e249d74f48ecfec601650de93John McCall
2484b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // These don't need to be particularly wide, because they're
2494b9c2d235fb9449e249d74f48ecfec601650de93John McCall    // strictly limited by the forms of expressions we permit.
2504b9c2d235fb9449e249d74f48ecfec601650de93John McCall    unsigned NumSubExprs : 8;
2514b9c2d235fb9449e249d74f48ecfec601650de93John McCall    unsigned ResultIndex : 32 - 8 - NumExprBits;
2524b9c2d235fb9449e249d74f48ecfec601650de93John McCall  };
2534b9c2d235fb9449e249d74f48ecfec601650de93John McCall
254f85e193739c953358c865005855253af4f68a497John McCall  class ObjCIndirectCopyRestoreExprBitfields {
255f85e193739c953358c865005855253af4f68a497John McCall    friend class ObjCIndirectCopyRestoreExpr;
256f85e193739c953358c865005855253af4f68a497John McCall    unsigned : NumExprBits;
257f85e193739c953358c865005855253af4f68a497John McCall
258f85e193739c953358c865005855253af4f68a497John McCall    unsigned ShouldCopy : 1;
259f85e193739c953358c865005855253af4f68a497John McCall  };
260f85e193739c953358c865005855253af4f68a497John McCall
26132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  class InitListExprBitfields {
26232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    friend class InitListExpr;
26332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
26432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    unsigned : NumExprBits;
26532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
26632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    /// Whether this initializer list originally had a GNU array-range
26732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    /// designator in it. This is a temporary marker used by CodeGen.
26832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    unsigned HadArrayRangeDesignator : 1;
26932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  };
27032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
2714ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  class TypeTraitExprBitfields {
2724ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    friend class TypeTraitExpr;
2734ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    friend class ASTStmtReader;
2744ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    friend class ASTStmtWriter;
2754ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
2764ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    unsigned : NumExprBits;
2774ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
2784ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
2794ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    unsigned Kind : 8;
2804ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
2814ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    /// \brief If this expression is not value-dependent, this indicates whether
2824ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    /// the trait evaluated true or false.
2834ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    unsigned Value : 1;
2844ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor
2854ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    /// \brief The number of arguments to this type trait.
2864ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
2874ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  };
288211c8ddb5b500ed84833751363d0cfe1115f4dd3Richard Smith
2898e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  union {
29063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    // FIXME: this is wasteful on 64-bit platforms.
29163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    void *Aligner;
29263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
2938e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    StmtBitfields StmtBits;
2948e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    CompoundStmtBitfields CompoundStmtBits;
2958e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    ExprBitfields ExprBits;
296bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    CharacterLiteralBitfields CharacterLiteralBits;
297bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    FloatingLiteralBitfields FloatingLiteralBits;
298bb8a897f350f46475f36140480e82289f6baa020Benjamin Kramer    UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
299cb66cff8fdf641f57f85dedb515a5f3240e3a9bbChandler Carruth    DeclRefExprBitfields DeclRefExprBits;
3008e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    CastExprBitfields CastExprBits;
301cc324ad80ab940efca006b0064f7ca70a6181816Peter Collingbourne    CallExprBitfields CallExprBits;
30280ee6e878a169e6255d4686a91bb696151ff229fJohn McCall    ExprWithCleanupsBitfields ExprWithCleanupsBits;
3034b9c2d235fb9449e249d74f48ecfec601650de93John McCall    PseudoObjectExprBitfields PseudoObjectExprBits;
304f85e193739c953358c865005855253af4f68a497John McCall    ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
30532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    InitListExprBitfields InitListExprBits;
3064ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor    TypeTraitExprBitfields TypeTraitExprBits;
3078e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  };
3088e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall
309d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  friend class ASTStmtReader;
3104ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor  friend class ASTStmtWriter;
311d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
3128189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenekpublic:
3138189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  // Only allow allocation of Stmts using the allocator in ASTContext
3141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // or by doing a placement new.
31505ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper  void* operator new(size_t bytes, const ASTContext& C,
3165b3ebb4356ac62abd8767c4c3388b773d9d61802Craig Topper                     unsigned alignment = 8);
3171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
318bdf253de659442bc3fd4fc28d6145ad46113a0a7Craig Topper  void* operator new(size_t bytes, const ASTContext* C,
319536bcedb8e6fea6458789f99f0ef554ce65514ccCraig Topper                     unsigned alignment = 8) {
320536bcedb8e6fea6458789f99f0ef554ce65514ccCraig Topper    return operator new(bytes, *C, alignment);
321536bcedb8e6fea6458789f99f0ef554ce65514ccCraig Topper  }
322bdf253de659442bc3fd4fc28d6145ad46113a0a7Craig Topper
3238189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  void* operator new(size_t bytes, void* mem) throw() {
3248189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return mem;
3258189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  }
326e2dedf8f61b8f306f704781456b482eb61871e8eDouglas Gregor
32705ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper  void operator delete(void*, const ASTContext&, unsigned) throw() { }
32805ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper  void operator delete(void*, const ASTContext*, unsigned) throw() { }
32905ed1a0587edcf3b8ee84a67d8c8ca76753d1fc1Craig Topper  void operator delete(void*, size_t) throw() { }
330fb523e16dd1f860ff02a3ae03e5e3e25327a5860Chris Lattner  void operator delete(void*, void*) throw() { }
331e2dedf8f61b8f306f704781456b482eb61871e8eDouglas Gregor
332025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregorpublic:
333025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  /// \brief A placeholder type used to construct an empty shell of a
334025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  /// type, that will be filled in later (e.g., by some
335025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  /// de-serialization).
336025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  struct EmptyShell { };
337025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor
33802892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbarprivate:
33902892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar  /// \brief Whether statistic collection is enabled.
34002892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar  static bool StatisticsEnabled;
34102892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar
3429c1863ef36a74e8203f00289d19856ad956f48b9Ted Kremenekprotected:
343025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  /// \brief Construct an empty statement.
3448e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  explicit Stmt(StmtClass SC, EmptyShell) {
3458e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    StmtBits.sClass = SC;
34602892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar    if (StatisticsEnabled) Stmt::addStmtClass(SC);
347025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  }
348025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor
3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
3508e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  Stmt(StmtClass SC) {
3518e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    StmtBits.sClass = SC;
35202892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar    if (StatisticsEnabled) Stmt::addStmtClass(SC);
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
355ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  StmtClass getStmtClass() const {
3568e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    return static_cast<StmtClass>(StmtBits.sClass);
357b2f81cf7f8445e0c65c0428f4fbb0442566916b8Douglas Gregor  }
3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *getStmtClassName() const;
3591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
360b5a69586f1b8855ee4c1f0bb7a8f0ff4fe32ce09Steve Naroff  /// SourceLocation tokens are not useful in isolation - they are low level
361b5a69586f1b8855ee4c1f0bb7a8f0ff4fe32ce09Steve Naroff  /// value objects created/interpreted by SourceManager. We assume AST
362b5a69586f1b8855ee4c1f0bb7a8f0ff4fe32ce09Steve Naroff  /// clients will have a pointer to the respective SourceManager.
363aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar  SourceRange getSourceRange() const LLVM_READONLY;
364aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar  SourceLocation getLocStart() const LLVM_READONLY;
365aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar  SourceLocation getLocEnd() const LLVM_READONLY;
3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // global temp stats (until we have a per-module visitor)
3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static void addStmtClass(const StmtClass s);
36902892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar  static void EnableStatistics();
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static void PrintStats();
3716000dace22f110d8768476989313e9d981d690d0Chris Lattner
37295f6190acb664b345b1395abaea84ee451740c5bDmitri Gribenko  /// \brief Dumps the specified AST fragment and all subtrees to
37395f6190acb664b345b1395abaea84ee451740c5bDmitri Gribenko  /// \c llvm::errs().
374651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dump() const;
375651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dump(SourceManager &SM) const;
3768cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  void dump(raw_ostream &OS, SourceManager &SM) const;
3776000dace22f110d8768476989313e9d981d690d0Chris Lattner
3787ba443aa8cfb222737d3a964a19ad4fb1328c4b5Richard Trieu  /// dumpColor - same as dump(), but forces color highlighting.
379651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpColor() const;
3807ba443aa8cfb222737d3a964a19ad4fb1328c4b5Richard Trieu
3816000dace22f110d8768476989313e9d981d690d0Chris Lattner  /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
3826000dace22f110d8768476989313e9d981d690d0Chris Lattner  /// back to its original source language syntax.
3838b4b98b7cb18cc4a99cca0aefa515cc8756dc06dCraig Topper  void dumpPretty(const ASTContext &Context) const;
3848cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  void printPretty(raw_ostream &OS, PrinterHelper *Helper,
385e4f2142d00fa5fdb580c4e2413da91882d955381Chris Lattner                   const PrintingPolicy &Policy,
386d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor                   unsigned Indentation = 0) const;
3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
388d2a4a1af1088fca80e2dc76eb3369db0fbbfdefdTed Kremenek  /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
389d2a4a1af1088fca80e2dc76eb3369db0fbbfdefdTed Kremenek  ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
390d2a4a1af1088fca80e2dc76eb3369db0fbbfdefdTed Kremenek  void viewAST() const;
3911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3927e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall  /// Skip past any implicit AST nodes which might surround this
3937e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall  /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
3947e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall  Stmt *IgnoreImplicit();
3957e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall
396a1364be341550d71dff27dd8de0c6872ba6c707eChandler Carruth  const Stmt *stripLabelLikeStatements() const;
397a1364be341550d71dff27dd8de0c6872ba6c707eChandler Carruth  Stmt *stripLabelLikeStatements() {
398a1364be341550d71dff27dd8de0c6872ba6c707eChandler Carruth    return const_cast<Stmt*>(
399a1364be341550d71dff27dd8de0c6872ba6c707eChandler Carruth      const_cast<const Stmt*>(this)->stripLabelLikeStatements());
400a1364be341550d71dff27dd8de0c6872ba6c707eChandler Carruth  }
401a1364be341550d71dff27dd8de0c6872ba6c707eChandler Carruth
40263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// Child Iterators: All subclasses must implement 'children'
40363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// to permit easy iteration over the substatements/subexpessions of an
40463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  /// AST node.  This permits easy iteration over all nodes in the AST.
4059caf8b1ca6beb254f420dada3c0e94d5ef027f58Ted Kremenek  typedef StmtIterator       child_iterator;
4069caf8b1ca6beb254f420dada3c0e94d5ef027f58Ted Kremenek  typedef ConstStmtIterator  const_child_iterator;
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  typedef StmtRange          child_range;
40963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  typedef ConstStmtRange     const_child_range;
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  child_range children();
41263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  const_child_range children() const {
41363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return const_cast<Stmt*>(this)->children();
41477ed8e4edf6ed78c53fb20ec3210aff2a59c9d87Ted Kremenek  }
4151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  child_iterator child_begin() { return children().first; }
41763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  child_iterator child_end() { return children().second; }
41863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
41963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  const_child_iterator child_begin() const { return children().first; }
42063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  const_child_iterator child_end() const { return children().second; }
42141ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor
42241ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor  /// \brief Produce a unique representation of the given statement.
42341ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor  ///
42470517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// \param ID once the profiling operation is complete, will contain
42541ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor  /// the unique representation of the given statement.
42641ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor  ///
42770517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// \param Context the AST context in which the statement resides
42841ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor  ///
42970517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// \param Canonical whether the profile should be based on the canonical
43041ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor  /// representation of this statement (e.g., where non-type template
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// parameters are identified by index/level rather than their
43241ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor  /// declaration pointers) or the exact representation of the statement as
43341ef0c3472a3d09c29bc1792f3d26842f2b8a695Douglas Gregor  /// written in the source.
4344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad  void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
435b11382497a923b0d7009e85a1d8eb7bf93ec6d0dChandler Carruth               bool Canonical) const;
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DeclStmt - Adaptor class for mixing declarations with statements and
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// expressions. For example, CompoundStmt mixes statements, expressions
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// and declarations (variables, types). Another example is ForStmt, where
4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the first statement can be an expression or a declaration.
4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass DeclStmt : public Stmt {
4449653db7bd0e3665b955a0445859285f2e1e7dacdDouglas Gregor  DeclGroupRef DG;
44581c018d9482e7cc2addadc6202dcf162a01faefdChris Lattner  SourceLocation StartLoc, EndLoc;
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
4498ffb159441e923322bef6b5dee1aaf24c738d75eTed Kremenek           SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
4508ffb159441e923322bef6b5dee1aaf24c738d75eTed Kremenek                                    StartLoc(startLoc), EndLoc(endLoc) {}
4511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45284f2170062014d268951902164bed0d8bdea0e82Douglas Gregor  /// \brief Build an empty declaration statement.
45384f2170062014d268951902164bed0d8bdea0e82Douglas Gregor  explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
45484f2170062014d268951902164bed0d8bdea0e82Douglas Gregor
4557e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner  /// isSingleDecl - This method returns true if this DeclStmt refers
456fe95deaf66e4fbd82d44b5f6afa8162fa69cb85cChris Lattner  /// to a single Decl.
4577e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner  bool isSingleDecl() const {
458fe95deaf66e4fbd82d44b5f6afa8162fa69cb85cChris Lattner    return DG.isSingleDecl();
4598ffb159441e923322bef6b5dee1aaf24c738d75eTed Kremenek  }
4601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4617e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner  const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *getSingleDecl() { return DG.getSingleDecl(); }
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
464e66a8cf9117e5fb95a05ff76ec06615e63dd5adeChris Lattner  const DeclGroupRef getDeclGroup() const { return DG; }
465e66a8cf9117e5fb95a05ff76ec06615e63dd5adeChris Lattner  DeclGroupRef getDeclGroup() { return DG; }
46684f2170062014d268951902164bed0d8bdea0e82Douglas Gregor  void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
467b5a69586f1b8855ee4c1f0bb7a8f0ff4fe32ce09Steve Naroff
46881c018d9482e7cc2addadc6202dcf162a01faefdChris Lattner  SourceLocation getStartLoc() const { return StartLoc; }
46984f2170062014d268951902164bed0d8bdea0e82Douglas Gregor  void setStartLoc(SourceLocation L) { StartLoc = L; }
47081c018d9482e7cc2addadc6202dcf162a01faefdChris Lattner  SourceLocation getEndLoc() const { return EndLoc; }
47184f2170062014d268951902164bed0d8bdea0e82Douglas Gregor  void setEndLoc(SourceLocation L) { EndLoc = L; }
47284f2170062014d268951902164bed0d8bdea0e82Douglas Gregor
47365d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
47465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static bool classof(const Stmt *T) {
4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return T->getStmtClass() == DeclStmtClass;
4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48014f8b4ff660bcaa763974b8d0fae81857c594495Ted Kremenek  // Iterators over subexpressions.
48163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  child_range children() {
48263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    return child_range(child_iterator(DG.begin(), DG.end()),
48363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                       child_iterator(DG.end(), DG.end()));
48463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
4851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4860632dd6fe068011af5710c0d6a745724021ff620Chris Lattner  typedef DeclGroupRef::iterator decl_iterator;
4870632dd6fe068011af5710c0d6a745724021ff620Chris Lattner  typedef DeclGroupRef::const_iterator const_decl_iterator;
488651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<decl_iterator> decl_range;
489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<const_decl_iterator> decl_const_range;
4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
491651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  decl_range decls() { return decl_range(decl_begin(), decl_end()); }
492651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  decl_const_range decls() const {
493651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return decl_const_range(decl_begin(), decl_end());
494651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
4958ffb159441e923322bef6b5dee1aaf24c738d75eTed Kremenek  decl_iterator decl_begin() { return DG.begin(); }
4968ffb159441e923322bef6b5dee1aaf24c738d75eTed Kremenek  decl_iterator decl_end() { return DG.end(); }
4978ffb159441e923322bef6b5dee1aaf24c738d75eTed Kremenek  const_decl_iterator decl_begin() const { return DG.begin(); }
4988ffb159441e923322bef6b5dee1aaf24c738d75eTed Kremenek  const_decl_iterator decl_end() const { return DG.end(); }
499fd8b43596478b779b6777cb3a595e69d7856c378Jordan Rose
500fd8b43596478b779b6777cb3a595e69d7856c378Jordan Rose  typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
501fd8b43596478b779b6777cb3a595e69d7856c378Jordan Rose  reverse_decl_iterator decl_rbegin() {
502fd8b43596478b779b6777cb3a595e69d7856c378Jordan Rose    return reverse_decl_iterator(decl_end());
503fd8b43596478b779b6777cb3a595e69d7856c378Jordan Rose  }
504fd8b43596478b779b6777cb3a595e69d7856c378Jordan Rose  reverse_decl_iterator decl_rend() {
505fd8b43596478b779b6777cb3a595e69d7856c378Jordan Rose    return reverse_decl_iterator(decl_begin());
506fd8b43596478b779b6777cb3a595e69d7856c378Jordan Rose  }
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NullStmt - This is the null statement ";": C99 6.8.3p3.
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass NullStmt : public Stmt {
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation SemiLoc;
51344aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis
514e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis  /// \brief True if the null statement was preceded by an empty macro, e.g:
51544aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis  /// @code
51644aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis  ///   #define CALL(x)
51744aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis  ///   CALL(0);
51844aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis  /// @endcode
519e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis  bool HasLeadingEmptyMacro;
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
521e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis  NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
522e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis    : Stmt(NullStmtClass), SemiLoc(L),
523e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis      HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
525025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  /// \brief Build an empty null statement.
526e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
527e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis      HasLeadingEmptyMacro(false) { }
528025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation getSemiLoc() const { return SemiLoc; }
530025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  void setSemiLoc(SourceLocation L) { SemiLoc = L; }
531b5a69586f1b8855ee4c1f0bb7a8f0ff4fe32ce09Steve Naroff
532e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis  bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
53344aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis
53465d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
53565d78312ce026092cb6e7b1d4d06f05e18d02aa0Erik Verbruggen  SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static bool classof(const Stmt *T) {
5381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return T->getStmtClass() == NullStmtClass;
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  child_range children() { return child_range(); }
54244aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis
54344aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis  friend class ASTStmtReader;
54444aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis  friend class ASTStmtWriter;
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// CompoundStmt - This represents a group of statements like { stmt stmt }.
5485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass CompoundStmt : public Stmt {
5508189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  Stmt** Body;
551b5a69586f1b8855ee4c1f0bb7a8f0ff4fe32ce09Steve Naroff  SourceLocation LBracLoc, RBracLoc;
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
5530b861560d9b4b0eb2b36d0f32cc7819efe9ee5f2Craig Topper  CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
5543a2d0fb726aca3096b5c1ea9be734417060f34d7Benjamin Kramer               SourceLocation LB, SourceLocation RB);
5553a2d0fb726aca3096b5c1ea9be734417060f34d7Benjamin Kramer
556063820655db8121f0022a7c51458463c7250324cBenjamin Kramer  // \brief Build an empty compound statement with a location.
5573a2d0fb726aca3096b5c1ea9be734417060f34d7Benjamin Kramer  explicit CompoundStmt(SourceLocation Loc)
5586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : Stmt(CompoundStmtClass), Body(nullptr), LBracLoc(Loc), RBracLoc(Loc) {
5593a2d0fb726aca3096b5c1ea9be734417060f34d7Benjamin Kramer    CompoundStmtBits.NumStmts = 0;
5601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
561025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor
562025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  // \brief Build an empty compound statement.
563025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor  explicit CompoundStmt(EmptyShell Empty)
5646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : Stmt(CompoundStmtClass, Empty), Body(nullptr) {
5658e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    CompoundStmtBits.NumStmts = 0;
5668e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  }
567025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor
5688b4b98b7cb18cc4a99cca0aefa515cc8756dc06dCraig Topper  void setStmts(const ASTContext &C, Stmt **Stmts, unsigned NumStmts);
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5708e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
5718e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  unsigned size() const { return CompoundStmtBits.NumStmts; }
572025452fa0eda63e150cfaeebe64f0a19c96b3a06Douglas Gregor
5738189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  typedef Stmt** body_iterator;
574651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<body_iterator> body_range;
575651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
576651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  body_range body() { return body_range(body_begin(), body_end()); }
5778189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  body_iterator body_begin() { return Body; }
5788e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  body_iterator body_end() { return Body + size(); }
5796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Stmt *body_back() { return !body_empty() ? Body[size()-1] : nullptr; }
580ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
5818e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  void setLastStmt(Stmt *S) {
5828e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    assert(!body_empty() && "setLastStmt");
5838e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall    Body[size()-1] = S;
5848e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  }
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5868189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  typedef Stmt* const * const_body_iterator;
587651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<const_body_iterator> body_const_range;
588651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
589651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  body_const_range body() const {
590651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return body_const_range(body_begin(), body_end());
591651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
5928189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  const_body_iterator body_begin() const { return Body; }
5938e6285af719adc6f86d6faa235d22a08eb68ee3aJohn McCall  const_body_iterator body_end() const { return Body + size(); }
5946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const Stmt *body_back() const {
5956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return !body_empty() ? Body[size() - 1] : nullptr;
5966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
5978189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek
5988189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
5998189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  reverse_body_iterator body_rbegin() {
6008189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return reverse_body_iterator(body_end());
6018189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  }
6028189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  reverse_body_iterator body_rend() {
6038189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return reverse_body_iterator(body_begin());
6048189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  }
6054ce854736dd196e2304f554ebeac8b43c89cf9e2Ted Kremenek
6068189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  typedef std::reverse_iterator<const_body_iterator>
6078189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek          const_reverse_body_iterator;
6084ce854736dd196e2304f554ebeac8b43c89cf9e2Ted Kremenek
6098189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  const_reverse_body_iterator body_rbegin() const {
6108189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return const_reverse_body_iterator(body_end());
6118189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  }
6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6138189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  const_reverse_body_iterator body_rend() const {
6148189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek    return const_reverse_body_iterator(body_begin());
6158189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek  }
616