DeclBase.h revision e53060fa78ad7e98352049f72787bdb7543e2a48
16ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor//===-- DeclBase.h - Base Classes for representing declarations -*- C++ -*-===//
20ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner//
30ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner//                     The LLVM Compiler Infrastructure
40ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner//
50ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner// This file is distributed under the University of Illinois Open Source
60ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner// License. See LICENSE.TXT for details.
70ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner//
80ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner//===----------------------------------------------------------------------===//
90ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner//
10b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner//  This file defines the Decl and DeclContext interfaces.
110ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner//
120ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner//===----------------------------------------------------------------------===//
130ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
140ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner#ifndef LLVM_CLANG_AST_DECLBASE_H
150ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner#define LLVM_CLANG_AST_DECLBASE_H
160ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
170ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner#include "clang/AST/Attr.h"
180ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner#include "clang/AST/Type.h"
194afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// FIXME: Layering violation
204afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor#include "clang/Parse/AccessSpecifier.h"
2149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner#include "llvm/Support/PrettyStackTrace.h"
22ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner#include "llvm/ADT/PointerUnion.h"
230ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
240ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnernamespace clang {
2542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisclass DeclContext;
26ef177820100ab583b08fd3056e2a5a52ee4b1629Argyrios Kyrtzidisclass TranslationUnitDecl;
272d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidisclass NamespaceDecl;
282a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregorclass UsingDirectiveDecl;
2944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorclass NamedDecl;
300ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerclass FunctionDecl;
31d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidisclass CXXRecordDecl;
32b048c9835969c4f7fe06264748be18ed4b442116Chris Lattnerclass EnumDecl;
33d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidisclass ObjCMethodDecl;
3464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregorclass ObjCContainerDecl;
350ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerclass ObjCInterfaceDecl;
360701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroffclass ObjCCategoryDecl;
370701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroffclass ObjCProtocolDecl;
380701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroffclass ObjCImplementationDecl;
390701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroffclass ObjCCategoryImplDecl;
40074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregorclass LinkageSpecDecl;
4156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffclass BlockDecl;
4244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorclass DeclarationName;
43eaab20669b6a9910a5deb0110fdd8f7581d86a36Ted Kremenekclass CompoundStmt;
440eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner}
450eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner
460eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattnernamespace llvm {
470eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner// DeclContext* is only 4-byte aligned on 32-bit systems.
480eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattnertemplate<>
490eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner  class PointerLikeTypeTraits<clang::DeclContext*> {
500eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner  typedef clang::DeclContext* PT;
510eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattnerpublic:
520eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner  static inline void *getAsVoidPointer(PT P) { return P; }
530eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner  static inline PT getFromVoidPointer(void *P) {
540eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner    return static_cast<PT>(P);
550eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner  }
560eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner  enum { NumLowBitsAvailable = 2 };
570eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner};
580eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner}
590eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner
600eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattnernamespace clang {
610ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
620ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner/// Decl - This represents one declaration (or definition), e.g. a variable,
630ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner/// typedef, function, struct, etc.
640ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///
650ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerclass Decl {
660ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerpublic:
6764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  /// \brief Lists the kind of concrete classes of Decl.
680ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  enum Kind {
6964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) Derived,
7064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL_RANGE(CommonBase, Start, End) \
7164650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    CommonBase##First = Start, CommonBase##Last = End,
7264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define LAST_DECL_RANGE(CommonBase, Start, End) \
7364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    CommonBase##First = Start, CommonBase##Last = End
7464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
750ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
760ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
778fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor  /// IdentifierNamespace - According to C99 6.2.3, there are four
788fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor  /// namespaces, labels, tags, members and ordinary
798fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor  /// identifiers. These are meant as bitmasks, so that searches in
808fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor  /// C++ can look into the "tag" namespace during ordinary lookup. We
818fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor  /// use additional namespaces for Objective-C entities.
820ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  enum IdentifierNamespace {
832ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    IDNS_Label = 0x1,
842ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    IDNS_Tag = 0x2,
852ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    IDNS_Member = 0x4,
867dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor    IDNS_Ordinary = 0x8,
878fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor    IDNS_ObjCProtocol = 0x10,
888fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor    IDNS_ObjCImplementation = 0x20,
898fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor    IDNS_ObjCCategoryImpl = 0x40
900ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
910ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
920ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// ObjCDeclQualifier - Qualifier used on types in method declarations
930ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// for remote messaging. They are meant for the arguments though and
940ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
950ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  enum ObjCDeclQualifier {
960ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_None = 0x0,
970ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_In = 0x1,
980ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Inout = 0x2,
990ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Out = 0x4,
1000ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Bycopy = 0x8,
1010ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Byref = 0x10,
1020ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Oneway = 0x20
1030ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
1040ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1050ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerprivate:
106244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  /// NextDeclInContext - The next declaration within the same lexical
1074afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// DeclContext. These pointers form the linked list that is
1084afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// traversed via DeclContext's decls_begin()/decls_end().
109244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  Decl *NextDeclInContext;
1104afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1114afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  friend class DeclContext;
1124afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
113ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  struct MultipleDC {
114ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    DeclContext *SemanticDC;
115ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    DeclContext *LexicalDC;
116ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  };
117ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner
118ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner
1194afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
1204afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// For declarations that don't contain C++ scope specifiers, it contains
1214afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// the DeclContext where the Decl was declared.
1224afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// For declarations with C++ scope specifiers, it contains a MultipleDC*
1234afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// with the context where it semantically belongs (SemanticDC) and the
1244afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// context where it was lexically declared (LexicalDC).
1254afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// e.g.:
1264afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///
1274afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   namespace A {
1284afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
1294afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   }
1304afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   void A::f(); // SemanticDC == namespace 'A'
1314afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///                // LexicalDC == global namespace
132ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
1334afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
134ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  inline bool isInSemaDC() const    { return DeclCtx.is<DeclContext*>(); }
135ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
1364afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  inline MultipleDC *getMultipleDC() const {
137ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    return DeclCtx.get<MultipleDC*>();
1384afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
13910d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner  inline DeclContext *getSemanticDC() const {
140ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    return DeclCtx.get<DeclContext*>();
14110d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner  }
14210d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner
14339d7650656fdd1be879ed7d0c722cfeae95e56d5Daniel Dunbar  /// Loc - The location that this decl.
14439d7650656fdd1be879ed7d0c722cfeae95e56d5Daniel Dunbar  SourceLocation Loc;
14539d7650656fdd1be879ed7d0c722cfeae95e56d5Daniel Dunbar
1460ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// DeclKind - This indicates which class this is.
1470ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  Kind DeclKind   :  8;
1480ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1490ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// InvalidDecl - This indicates a semantic error occurred.
1500ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  unsigned int InvalidDecl :  1;
1510ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1520ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// HasAttrs - This indicates whether the decl has attributes or not.
1530ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  unsigned int HasAttrs : 1;
154d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis
1556b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// Implicit - Whether this declaration was implicitly generated by
1566b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// the implementation rather than explicitly written by the user.
1576b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  bool Implicit : 1;
1586b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor
159e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  /// \brief Whether this declaration was "used", meaning that a definition is
160e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  /// required.
161e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  bool Used : 1;
1629cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor
1639cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregorprotected:
164769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
1658fc463adf0116fdcbff86e9cca11955aad1649feDouglas Gregor  unsigned IdentifierNamespace : 8;
166769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
1679cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregorprivate:
1681329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson#ifndef NDEBUG
1691329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson  void CheckAccessDeclContext() const;
1701329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson#else
1711329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson  void CheckAccessDeclContext() const { }
1721329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson#endif
1731329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
1744afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorprotected:
175d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis  /// Access - Used by C++ decls for the access specifier.
176d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis  // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
177d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis  unsigned Access : 2;
178d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis  friend class CXXClassMemberWrapper;
179d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis
1804afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  Decl(Kind DK, DeclContext *DC, SourceLocation L)
181682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner    : NextDeclInContext(0), DeclCtx(DC),
18239d7650656fdd1be879ed7d0c722cfeae95e56d5Daniel Dunbar      Loc(L), DeclKind(DK), InvalidDecl(0),
183e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor      HasAttrs(false), Implicit(false), Used(false),
184769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner      IdentifierNamespace(getIdentifierNamespaceForKind(DK)), Access(AS_none) {
1850ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    if (Decl::CollectingStats()) addDeclKind(DK);
1860ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
1871bb19638f2ec0d63ed131b51ca8d9542d1a9afeeSam Bishop
1880ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  virtual ~Decl();
1891bb19638f2ec0d63ed131b51ca8d9542d1a9afeeSam Bishop
1901bb19638f2ec0d63ed131b51ca8d9542d1a9afeeSam Bishoppublic:
19155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
19255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  /// \brief Source range that this declaration covers.
19355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  virtual SourceRange getSourceRange() const {
19455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    return SourceRange(getLocation(), getLocation());
19555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  }
19655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
19755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
19855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
1990ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  SourceLocation getLocation() const { return Loc; }
2000ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  void setLocation(SourceLocation L) { Loc = L; }
2010ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
2020ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  Kind getKind() const { return DeclKind; }
2030ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  const char *getDeclKindName() const;
2040ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
205244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  Decl *getNextDeclInContext() { return NextDeclInContext; }
206244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner  const Decl *getNextDeclInContext() const { return NextDeclInContext; }
20796f4468b6b1bf69bb01c1207f716a3ffaeb4a8d3Chris Lattner
2084afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  DeclContext *getDeclContext() {
20910d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner    if (isInSemaDC())
21010d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner      return getSemanticDC();
21110d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner    return getMultipleDC()->SemanticDC;
2124afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
2130cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  const DeclContext *getDeclContext() const {
2140cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    return const_cast<Decl*>(this)->getDeclContext();
2150cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  }
2160cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner
2171329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson  void setAccess(AccessSpecifier AS) {
218b8547e800f9553a72c2f3635ae2371692bd4411aAnders Carlsson    Access = AS;
2191329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson    CheckAccessDeclContext();
2201329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson  }
2211329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson
2221329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson  AccessSpecifier getAccess() const {
2231329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson    CheckAccessDeclContext();
2241329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson    return AccessSpecifier(Access);
225b8547e800f9553a72c2f3635ae2371692bd4411aAnders Carlsson  }
2264afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
22776a642ff4fce7a0648c79a1f01324a8c3880e251Chris Lattner  bool hasAttrs() const { return HasAttrs; }
22868584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  void addAttr(ASTContext &Context, Attr *attr);
22968584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  const Attr *getAttrs(ASTContext &Context) const {
23081abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattner    if (!HasAttrs) return 0;  // common case, no attributes.
23168584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    return getAttrsImpl(Context);    // Uncommon case, out of line hash lookup.
23281abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattner  }
23368584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  void swapAttrs(ASTContext &Context, Decl *D);
23468584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  void invalidateAttrs(ASTContext &Context);
2350ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
23668584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  template<typename T> const T *getAttr(ASTContext &Context) const {
23768584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    for (const Attr *attr = getAttrs(Context); attr; attr = attr->getNext())
2380ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      if (const T *V = dyn_cast<T>(attr))
2390ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner        return V;
2400ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    return 0;
2410ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
2420ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
24368584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  template<typename T> bool hasAttr(ASTContext &Context) const {
24468584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    return getAttr<T>(Context) != 0;
245115cafcafec3572756635ca81ef83999ae0c8af7Chris Lattner  }
246115cafcafec3572756635ca81ef83999ae0c8af7Chris Lattner
2470ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// setInvalidDecl - Indicates the Decl had a semantic error. This
2480ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// allows for graceful error recovery.
2496ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; }
2500ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  bool isInvalidDecl() const { return (bool) InvalidDecl; }
2516b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor
2526b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// isImplicit - Indicates whether the declaration was implicitly
2536b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// generated by the implementation. If false, this declaration
2546b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// was written explicitly in the source code.
2556b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  bool isImplicit() const { return Implicit; }
2566b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  void setImplicit(bool I = true) { Implicit = I; }
2570ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
258e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  /// \brief Whether this declaration was used, meaning that a definition
259e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  /// is required.
260e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  bool isUsed() const { return Used; }
261e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  void setUsed(bool U = true) { Used = U; }
262e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor
263cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  unsigned getIdentifierNamespace() const {
264769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    return IdentifierNamespace;
2650ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
266d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner  bool isInIdentifierNamespace(unsigned NS) const {
267d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner    return getIdentifierNamespace() & NS;
268d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner  }
269769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  static unsigned getIdentifierNamespaceForKind(Kind DK);
270769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
271d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner
2724afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// getLexicalDeclContext - The declaration context where this Decl was
2734afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// lexically declared (LexicalDC). May be different from
2744afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// getDeclContext() (SemanticDC).
2754afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// e.g.:
2764afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///
2774afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   namespace A {
2784afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
2794afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   }
2804afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   void A::f(); // SemanticDC == namespace 'A'
2814afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///                // LexicalDC == global namespace
28210d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner  DeclContext *getLexicalDeclContext() {
2834afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    if (isInSemaDC())
28410d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner      return getSemanticDC();
2854afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return getMultipleDC()->LexicalDC;
2864afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
28710d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner  const DeclContext *getLexicalDeclContext() const {
28810d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner    return const_cast<Decl*>(this)->getLexicalDeclContext();
2894afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
290f5cecfbdcd20be224861f9e67c5973a9a2b61512Argyrios Kyrtzidis
291f5cecfbdcd20be224861f9e67c5973a9a2b61512Argyrios Kyrtzidis  bool isOutOfLine() const {
292f5cecfbdcd20be224861f9e67c5973a9a2b61512Argyrios Kyrtzidis    return getLexicalDeclContext() != getDeclContext();
293f5cecfbdcd20be224861f9e67c5973a9a2b61512Argyrios Kyrtzidis  }
29410d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner
2956ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  /// setDeclContext - Set both the semantic and lexical DeclContext
2966ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  /// to DC.
2976ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  void setDeclContext(DeclContext *DC);
2986ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
2994afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  void setLexicalDeclContext(DeclContext *DC);
3004afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
3014afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
3024afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // scoped decl is defined outside the current function or method.  This is
3034afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // roughly global variables and functions, but also handles enums (which could
3044afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // be defined inside or outside a function etc).
3054afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  bool isDefinedOutsideFunctionOrMethod() const;
3064afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
307d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  /// getBody - If this Decl represents a declaration for a body of code,
308d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  ///  such as a function or method definition, this method returns the
309d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  ///  top-level Stmt* of that body.  Otherwise this method returns null.
310d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  virtual Stmt* getBody(ASTContext &Context) const { return 0; }
311d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
312d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt.
313d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  CompoundStmt* getCompoundBody(ASTContext &Context) const;
314d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
315d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  /// getBodyRBrace - Gets the right brace of the body, if a body exists.
316d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  /// This works whether the body is a CompoundStmt or a CXXTryStmt.
317d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  SourceLocation getBodyRBrace(ASTContext &Context) const;
318d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
3190ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  // global temp stats (until we have a per-module visitor)
3200ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static void addDeclKind(Kind k);
3210ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static bool CollectingStats(bool Enable = false);
3220ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static void PrintStats();
3230ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
32467e332009c6e349dc34700f539747afcff990336Anders Carlsson  /// isTemplateParameter - Determines whether this declaration is a
325f57172b24f08a68d179675989813d5479dc87829Douglas Gregor  /// template parameter.
326f57172b24f08a68d179675989813d5479dc87829Douglas Gregor  bool isTemplateParameter() const;
32767e332009c6e349dc34700f539747afcff990336Anders Carlsson
32867e332009c6e349dc34700f539747afcff990336Anders Carlsson  /// isTemplateParameter - Determines whether this declaration is a
32967e332009c6e349dc34700f539747afcff990336Anders Carlsson  /// template parameter pack.
33067e332009c6e349dc34700f539747afcff990336Anders Carlsson  bool isTemplateParameterPack() const;
331f57172b24f08a68d179675989813d5479dc87829Douglas Gregor
332e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  /// \brief Whether this declaration is a function or function template.
333e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool isFunctionOrFunctionTemplate() const;
334e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
3350ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  // Implement isa/cast/dyncast/etc.
3360ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static bool classof(const Decl *) { return true; }
33742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static DeclContext *castToDeclContext(const Decl *);
33842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static Decl *castFromDeclContext(const DeclContext *);
3390ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
340bb45c512e0dfbe96bfe377ac642e726c0ba0affaSam Bishop  /// Destroy - Call destructors and release memory.
34127f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek  virtual void Destroy(ASTContext& C);
342bb45c512e0dfbe96bfe377ac642e726c0ba0affaSam Bishop
3434fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  void print(llvm::raw_ostream &Out, ASTContext &Context,
3444fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor             unsigned Indentation = 0);
3454fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  void print(llvm::raw_ostream &Out, ASTContext &Context,
3464fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor             const PrintingPolicy &Policy, unsigned Indentation = 0);
34742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  static void printGroup(Decl** Begin, unsigned NumDecls,
34842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman                         llvm::raw_ostream &Out, ASTContext &Context,
34942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman                         const PrintingPolicy &Policy,
35042f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman                         unsigned Indentation = 0);
3514fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  void dump(ASTContext &Context);
3524fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
35381abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattnerprivate:
35468584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  const Attr *getAttrsImpl(ASTContext &Context) const;
35581abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattner
3560ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
3570ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
35849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
35949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner/// doing something to a specific decl.
36049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattnerclass PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
36149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  Decl *TheDecl;
36249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceLocation Loc;
36349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceManager &SM;
36449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  const char *Message;
36549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattnerpublic:
36649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  PrettyStackTraceDecl(Decl *theDecl, SourceLocation L,
36749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                       SourceManager &sm, const char *Msg)
36849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
36949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
37049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  virtual void print(llvm::raw_ostream &OS) const;
37149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner};
37249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
37349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
374b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner/// DeclContext - This is used only as base class of specific decl types that
3751ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis/// can act as declaration contexts. These decls are (only the top classes
3761ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis/// that directly derive from DeclContext are mentioned, not their subclasses):
3770ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///
378ef177820100ab583b08fd3056e2a5a52ee4b1629Argyrios Kyrtzidis///   TranslationUnitDecl
3792d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis///   NamespaceDecl
3800ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///   FunctionDecl
3811ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis///   TagDecl
382d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis///   ObjCMethodDecl
3831ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis///   ObjCContainerDecl
3841ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis///   ObjCCategoryImplDecl
3851ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis///   ObjCImplementationDecl
386074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor///   LinkageSpecDecl
38756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff///   BlockDecl
3881ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis///
389b048c9835969c4f7fe06264748be18ed4b442116Chris Lattnerclass DeclContext {
3900ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// DeclKind - This indicates which class this is.
3910ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  Decl::Kind DeclKind   :  8;
3920ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
3932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief Whether this declaration context also has some external
3942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// storage that contains additional declarations that are lexically
3952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// part of this context.
3962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  mutable bool ExternalLexicalStorage : 1;
3972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
3982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief Whether this declaration context also has some external
3992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// storage that contains additional declarations that are visible
4002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// in this context.
4012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  mutable bool ExternalVisibleStorage : 1;
4022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
403c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  /// \brief Pointer to the data structure used to lookup declarations
404c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  /// within this context, which is a DenseMap<DeclarationName,
405c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  /// StoredDeclsList>.
4062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  mutable void* LookupPtr;
40744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4086037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// FirstDecl - The first declaration stored within this declaration
4096037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// context.
4102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  mutable Decl *FirstDecl;
4116037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
4126037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// LastDecl - The last declaration stored within this declaration
4136037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// context. FIXME: We could probably cache this value somewhere
4146037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// outside of the DeclContext, to reduce the size of DeclContext by
4156037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// another pointer.
4162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  mutable Decl *LastDecl;
4177643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
4180ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerprotected:
4196037fcba3431b47de1a994c9b286feac17894effDouglas Gregor   DeclContext(Decl::Kind K)
4202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor     : DeclKind(K), ExternalLexicalStorage(false),
4212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor       ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0),
4222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor       LastDecl(0) { }
42344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
42444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  void DestroyDecls(ASTContext &C);
4250ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
4260ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerpublic:
42744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ~DeclContext();
42844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4299b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis  Decl::Kind getDeclKind() const {
4309b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis    return DeclKind;
4319b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis  }
4320a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  const char *getDeclKindName() const;
4339b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis
434305ec42f971a2b3e114eb35256c23e955c497a1cArgyrios Kyrtzidis  /// getParent - Returns the containing DeclContext.
4350cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *getParent() {
436305ec42f971a2b3e114eb35256c23e955c497a1cArgyrios Kyrtzidis    return cast<Decl>(this)->getDeclContext();
437305ec42f971a2b3e114eb35256c23e955c497a1cArgyrios Kyrtzidis  }
4380cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  const DeclContext *getParent() const {
4390cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    return const_cast<DeclContext*>(this)->getParent();
440d2595ecce5f8350e485c83bfe767549a522b2802Argyrios Kyrtzidis  }
4410cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner
44277407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  /// getLexicalParent - Returns the containing lexical DeclContext. May be
44344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// different from getParent, e.g.:
44444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///
44544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   namespace A {
44644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///      struct S;
44744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   }
44844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   struct A::S {}; // getParent() == namespace 'A'
44977407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  ///                   // getLexicalParent() == translation unit
45077407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  ///
45177407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  DeclContext *getLexicalParent() {
4520cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    return cast<Decl>(this)->getLexicalDeclContext();
45377407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  }
4540cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  const DeclContext *getLexicalParent() const {
4550cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    return const_cast<DeclContext*>(this)->getLexicalParent();
4560cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  }
4570cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner
4580ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  bool isFunctionOrMethod() const {
4590ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    switch (DeclKind) {
4600cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    case Decl::Block:
4610cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    case Decl::ObjCMethod:
4620cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner      return true;
4630cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    default:
4640cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner      return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast;
4650ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    }
4660ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
4670ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
468ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  bool isFileContext() const {
469ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis    return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
470ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  }
471ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis
4725f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  bool isTranslationUnit() const {
4735f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor    return DeclKind == Decl::TranslationUnit;
4745f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  }
4755f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
476bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  bool isRecord() const {
47765100792a69a16895bd80f1d639b99e7ad903386Douglas Gregor    return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast;
478c7ed9c60b8ee04b119e23441cae2cfec74536ba9Argyrios Kyrtzidis  }
479c7ed9c60b8ee04b119e23441cae2cfec74536ba9Argyrios Kyrtzidis
48044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  bool isNamespace() const {
48144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return DeclKind == Decl::Namespace;
48244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
48344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
484bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  /// \brief Determines whether this context is dependent on a
485bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  /// template parameter.
486bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  bool isDependentContext() const;
487bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
488074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// isTransparentContext - Determines whether this context is a
489074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// "transparent" context, meaning that the members declared in this
490074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// context are semantically declared in the nearest enclosing
491074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// non-transparent (opaque) context but are lexically declared in
492074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// this context. For example, consider the enumerators of an
493074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// enumeration type:
494074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// @code
495074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// enum E {
496074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  ///   Val1
497074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// };
498074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// @endcode
499074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// Here, E is a transparent context, so its enumerator (Val1) will
500074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// appear (semantically) that it is in the same context of E.
501074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// Examples of transparent contexts include: enumerations (except for
502074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// C++0x scoped enums), C++ linkage specifications, and C++0x
503074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// inline namespaces.
504074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  bool isTransparentContext() const;
505074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
506ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  bool Encloses(DeclContext *DC) const {
507ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis    for (; DC; DC = DC->getParent())
508ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis      if (DC == this)
509ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis        return true;
510ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis    return false;
511ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  }
512ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis
51344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// getPrimaryContext - There may be many different
51444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// declarations of the same entity (including forward declarations
51544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// of classes, multiple definitions of namespaces, etc.), each with
51644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// a different set of declarations. This routine returns the
51744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// "primary" DeclContext structure, which will contain the
51844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// information needed to perform name lookup into this context.
5190701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *getPrimaryContext();
52044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
521ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  /// getLookupContext - Retrieve the innermost non-transparent
522ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  /// context of this context, which corresponds to the innermost
523ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  /// location from which name lookup can find the entities in this
524ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  /// context.
5250cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *getLookupContext();
5260cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  const DeclContext *getLookupContext() const {
5270cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    return const_cast<DeclContext *>(this)->getLookupContext();
52817a9b9e3ee75f5dbb4819cc8ebf40eec8015f84aDouglas Gregor  }
5290cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner
53088b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  /// \brief Retrieve the nearest enclosing namespace context.
53188b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  DeclContext *getEnclosingNamespaceContext();
53288b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  const DeclContext *getEnclosingNamespaceContext() const {
53388b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor    return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
53488b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  }
53588b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor
53644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// getNextContext - If this is a DeclContext that may have other
53744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// DeclContexts that are semantically connected but syntactically
53844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// different, such as C++ namespaces, this routine retrieves the
53944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// next DeclContext in the link. Iteration through the chain of
54044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// DeclContexts should begin at the primary DeclContext and
54144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// continue until this function returns NULL. For example, given:
54244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// @code
54344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// namespace N {
54444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   int x;
54544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// }
54644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// namespace N {
54744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   int y;
54844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// }
54944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// @endcode
55044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// The first occurrence of namespace N will be the primary
55144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// DeclContext. Its getNextContext will return the second
55244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// occurrence of namespace N.
55344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  DeclContext *getNextContext();
55444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
55544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// decl_iterator - Iterates through the declarations stored
55644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// within this context.
5576037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  class decl_iterator {
5586037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    /// Current - The current declaration.
5594afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    Decl *Current;
5606037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5616037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  public:
5624afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    typedef Decl*                     value_type;
5634afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    typedef Decl*                     reference;
5644afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    typedef Decl*                     pointer;
5656037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    typedef std::forward_iterator_tag iterator_category;
5666037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    typedef std::ptrdiff_t            difference_type;
5676037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5686037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    decl_iterator() : Current(0) { }
5694afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    explicit decl_iterator(Decl *C) : Current(C) { }
5706037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5716037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    reference operator*() const { return Current; }
5726037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    pointer operator->() const { return Current; }
5736037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
57496f4468b6b1bf69bb01c1207f716a3ffaeb4a8d3Chris Lattner    decl_iterator& operator++() {
575244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner      Current = Current->getNextDeclInContext();
57696f4468b6b1bf69bb01c1207f716a3ffaeb4a8d3Chris Lattner      return *this;
57796f4468b6b1bf69bb01c1207f716a3ffaeb4a8d3Chris Lattner    }
5786037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5796037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    decl_iterator operator++(int) {
5806037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      decl_iterator tmp(*this);
5816037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      ++(*this);
5826037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      return tmp;
5836037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    }
5846037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5856037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    friend bool operator==(decl_iterator x, decl_iterator y) {
5866037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      return x.Current == y.Current;
5876037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    }
5886037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    friend bool operator!=(decl_iterator x, decl_iterator y) {
5896037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      return x.Current != y.Current;
5906037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    }
5916037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  };
59244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
59344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// decls_begin/decls_end - Iterate over the declarations stored in
59444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// this context.
5956ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  decl_iterator decls_begin(ASTContext &Context) const;
5966ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  decl_iterator decls_end(ASTContext &Context) const;
5978038d5182b72dcdef292f6fb8539ad77f338855aDouglas Gregor  bool decls_empty(ASTContext &Context) const;
59844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
599f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  /// specific_decl_iterator - Iterates over a subrange of
600f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  /// declarations stored in a DeclContext, providing only those that
601669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// are of type SpecificDecl (or a class derived from it). This
602f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  /// iterator is used, for example, to provide iteration over just
603669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
604f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  template<typename SpecificDecl>
605f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  class specific_decl_iterator {
606f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// Current - The current, underlying declaration iterator, which
607d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// will either be NULL or will point to a declaration of
608f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// type SpecificDecl.
609f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    DeclContext::decl_iterator Current;
610f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
611f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// SkipToNextDecl - Advances the current position up to the next
612f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// declaration of type SpecificDecl that also meets the criteria
613f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// required by Acceptable.
614f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    void SkipToNextDecl() {
615669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      while (*Current && !isa<SpecificDecl>(*Current))
616f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor        ++Current;
617f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
618f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
619f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  public:
620f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef SpecificDecl* value_type;
621f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef SpecificDecl* reference;
622f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef SpecificDecl* pointer;
623f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
624f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      difference_type;
625f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef std::forward_iterator_tag iterator_category;
626f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
627669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    specific_decl_iterator() : Current() { }
628f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
629f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// specific_decl_iterator - Construct a new iterator over a
630d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// subset of the declarations the range [C,
631d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// end-of-declarations). If A is non-NULL, it is a pointer to a
632d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// member function of SpecificDecl that should return true for
633d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// all of the SpecificDecl instances that will be in the subset
634d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// of iterators. For example, if you want Objective-C instance
635d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
636d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// &ObjCMethodDecl::isInstanceMethod.
637669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
638f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      SkipToNextDecl();
639f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
640f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
6416037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    reference operator*() const { return cast<SpecificDecl>(*Current); }
6426037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    pointer operator->() const { return cast<SpecificDecl>(*Current); }
643f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
644f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    specific_decl_iterator& operator++() {
645f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      ++Current;
646f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      SkipToNextDecl();
647f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return *this;
648f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
649f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
650f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    specific_decl_iterator operator++(int) {
651f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      specific_decl_iterator tmp(*this);
652f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      ++(*this);
653f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return tmp;
654f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
655f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
656f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    friend bool
657f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
658f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return x.Current == y.Current;
659f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
660f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
661f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    friend bool
662f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
663f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return x.Current != y.Current;
664f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
665f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  };
666f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
667669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// \brief Iterates over a filtered subrange of declarations stored
668669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// in a DeclContext.
669669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  ///
670669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// This iterator visits only those declarations that are of type
671669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// SpecificDecl (or a class derived from it) and that meet some
672669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// additional run-time criteria. This iterator is used, for
673669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// example, to provide access to the instance methods within an
674669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
675669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// Acceptable = ObjCMethodDecl::isInstanceMethod).
676669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
677669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  class filtered_decl_iterator {
678669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// Current - The current, underlying declaration iterator, which
679669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// will either be NULL or will point to a declaration of
680669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// type SpecificDecl.
681669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    DeclContext::decl_iterator Current;
682669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
683669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// SkipToNextDecl - Advances the current position up to the next
684669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// declaration of type SpecificDecl that also meets the criteria
685669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// required by Acceptable.
686669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    void SkipToNextDecl() {
687669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      while (*Current &&
688669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor             (!isa<SpecificDecl>(*Current) ||
689669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor              (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
690669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor        ++Current;
691669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
692669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
693669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  public:
694669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef SpecificDecl* value_type;
695669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef SpecificDecl* reference;
696669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef SpecificDecl* pointer;
697669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
698669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      difference_type;
699669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef std::forward_iterator_tag iterator_category;
700669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
701669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    filtered_decl_iterator() : Current() { }
702669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
703669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// specific_decl_iterator - Construct a new iterator over a
704669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// subset of the declarations the range [C,
705669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// end-of-declarations). If A is non-NULL, it is a pointer to a
706669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// member function of SpecificDecl that should return true for
707669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// all of the SpecificDecl instances that will be in the subset
708669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// of iterators. For example, if you want Objective-C instance
709669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
710669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// &ObjCMethodDecl::isInstanceMethod.
711669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
712669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      SkipToNextDecl();
713669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
714669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
715669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    reference operator*() const { return cast<SpecificDecl>(*Current); }
716669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    pointer operator->() const { return cast<SpecificDecl>(*Current); }
717669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
718669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    filtered_decl_iterator& operator++() {
719669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      ++Current;
720669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      SkipToNextDecl();
721669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return *this;
722669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
723669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
724669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    filtered_decl_iterator operator++(int) {
725669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      filtered_decl_iterator tmp(*this);
726669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      ++(*this);
727669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return tmp;
728669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
729669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
730669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    friend bool
731669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
732669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return x.Current == y.Current;
733669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
734669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
735669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    friend bool
736669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
737669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return x.Current != y.Current;
738669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
739669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  };
740669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
74140f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// @brief Add the declaration D into this context.
74240f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
74340f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// This routine should be invoked when the declaration D has first
74440f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// been declared, to place D into the context where it was
74540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// (lexically) defined. Every declaration must be added to one
74640f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// (and only one!) context, where it can be visited via
74740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// [decls_begin(), decls_end()). Once a declaration has been added
74840f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// to its lexical context, the corresponding DeclContext owns the
74940f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// declaration.
75040f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
75140f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// If D is also a NamedDecl, it will be made visible within its
75240f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// semantic context via makeDeclVisibleInContext.
7536ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  void addDecl(ASTContext &Context, Decl *D);
75444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
75544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// lookup_iterator - An iterator that provides access to the results
75644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// of looking up a name within this context.
7574afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  typedef NamedDecl **lookup_iterator;
75844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
75944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// lookup_const_iterator - An iterator that provides non-mutable
76044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// access to the results of lookup up a name within this context.
7614afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  typedef NamedDecl * const * lookup_const_iterator;
76244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
76344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
76444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  typedef std::pair<lookup_const_iterator, lookup_const_iterator>
76544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    lookup_const_result;
76644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
76744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// lookup - Find the declarations (if any) with the given Name in
76844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// this context. Returns a range of iterators that contains all of
76940f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// the declarations with this name, with object, function, member,
77040f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// and enumerator names preceding any tag name. Note that this
77140f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// routine will not look into parent contexts.
7726ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  lookup_result lookup(ASTContext &Context, DeclarationName Name);
7736ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  lookup_const_result lookup(ASTContext &Context, DeclarationName Name) const;
77444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
77540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// @brief Makes a declaration visible within this context.
77640f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
77740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// This routine makes the declaration D visible to name lookup
77840f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// within this context and, if this is a transparent context,
77940f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// within its parent contexts up to the first enclosing
78040f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// non-transparent context. Making a declaration visible within a
78140f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// context does not transfer ownership of a declaration, and a
78240f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// declaration can be visible in many contexts that aren't its
78340f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// lexical context.
78440f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
78540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// If D is a redeclaration of an existing declaration that is
78640f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// visible from this context, as determined by
78740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// NamedDecl::declarationReplaces, the previous declaration will be
78840f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// replaced with D.
7896ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  void makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D);
7907643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
7912a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  /// udir_iterator - Iterates through the using-directives stored
7922a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  /// within this context.
7932a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  typedef UsingDirectiveDecl * const * udir_iterator;
7942a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
7952a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
7962a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
7976ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  udir_iterator_range getUsingDirectives(ASTContext &Context) const;
7982a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
7996ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  udir_iterator using_directives_begin(ASTContext &Context) const {
8006ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    return getUsingDirectives(Context).first;
8012a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
8022a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
8036ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  udir_iterator using_directives_end(ASTContext &Context) const {
8046ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor    return getUsingDirectives(Context).second;
8052a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
8062a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
807c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor  // Low-level accessors
808c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
809c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor  /// \brief Retrieve the internal representation of the lookup structure.
810c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  void* getLookupPtr() const { return LookupPtr; }
811c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
8122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief Whether this DeclContext has external storage containing
8132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// additional declarations that are lexically in this context.
8142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
8152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
8162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief State whether this DeclContext has external storage for
8172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// declarations lexically in this context.
8182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  void setHasExternalLexicalStorage(bool ES = true) {
8192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalLexicalStorage = ES;
8202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
8212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
8222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief Whether this DeclContext has external storage containing
8232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// additional declarations that are visible in this context.
8242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
8252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
8262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief State whether this DeclContext has external storage for
8272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// declarations visible in this context.
8282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  void setHasExternalVisibleStorage(bool ES = true) {
8292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalVisibleStorage = ES;
8302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
8312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
8323d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  static bool classof(const Decl *D);
833b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static bool classof(const DeclContext *D) { return true; }
83464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL_CONTEXT(Name) \
83564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  static bool classof(const Name##Decl *D) { return true; }
83664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
8377643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
8387643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidisprivate:
8392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  void LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const;
8402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  void LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const;
8412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
8426ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  void buildLookup(ASTContext &Context, DeclContext *DCtx);
8436ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  void makeDeclVisibleInContextImpl(ASTContext &Context, NamedDecl *D);
8440ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
8450ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
846f57172b24f08a68d179675989813d5479dc87829Douglas Gregorinline bool Decl::isTemplateParameter() const {
847f57172b24f08a68d179675989813d5479dc87829Douglas Gregor  return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm;
848f57172b24f08a68d179675989813d5479dc87829Douglas Gregor}
849f57172b24f08a68d179675989813d5479dc87829Douglas Gregor
8504afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorinline bool Decl::isDefinedOutsideFunctionOrMethod() const {
8514afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (getDeclContext())
8524afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return !getDeclContext()->getLookupContext()->isFunctionOrMethod();
85396f4468b6b1bf69bb01c1207f716a3ffaeb4a8d3Chris Lattner  return true;
8544afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
8554afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
8560ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner} // end clang.
8570ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
8580ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnernamespace llvm {
8590ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
86042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis/// Implement a isa_impl_wrap specialization to check whether a DeclContext is
86142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis/// a specific Decl.
86242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
86342220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct isa_impl_wrap<ToTy,
86442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                     const ::clang::DeclContext,const ::clang::DeclContext> {
86542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static bool doit(const ::clang::DeclContext &Val) {
86642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return ToTy::classof(::clang::Decl::castFromDeclContext(&Val));
8670ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
8680ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
86942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
87042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
87142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  : public isa_impl_wrap<ToTy,
87242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                      const ::clang::DeclContext,const ::clang::DeclContext> {};
8730ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
87442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis/// Implement cast_convert_val for Decl -> DeclContext conversions.
8750ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnertemplate<class FromTy>
87642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
877b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static ::clang::DeclContext &doit(const FromTy &Val) {
87842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return *FromTy::castToDeclContext(&Val);
8790ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
8800ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
8810ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
8820ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnertemplate<class FromTy>
88342220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
884b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static ::clang::DeclContext *doit(const FromTy *Val) {
88542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return FromTy::castToDeclContext(Val);
8860ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
8870ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
88842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
88944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregortemplate<class FromTy>
89044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorstruct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
89144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  static const ::clang::DeclContext &doit(const FromTy &Val) {
89244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return *FromTy::castToDeclContext(&Val);
89344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
89444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor};
89544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
89644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregortemplate<class FromTy>
89744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorstruct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
89844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  static const ::clang::DeclContext *doit(const FromTy *Val) {
89944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return FromTy::castToDeclContext(Val);
90044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
90144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor};
90244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
90342220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis/// Implement cast_convert_val for DeclContext -> Decl conversions.
90442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
90542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val<ToTy,
90642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                        const ::clang::DeclContext,const ::clang::DeclContext> {
90742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static ToTy &doit(const ::clang::DeclContext &Val) {
90842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val));
90942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  }
91042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis};
91142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
91242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext>
91342220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  : public cast_convert_val<ToTy,
91442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                      const ::clang::DeclContext,const ::clang::DeclContext> {};
91542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
91642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
91742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val<ToTy,
91842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                     const ::clang::DeclContext*, const ::clang::DeclContext*> {
91942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static ToTy *doit(const ::clang::DeclContext *Val) {
92042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val));
92142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  }
92242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis};
92342220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
92442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*>
92542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  : public cast_convert_val<ToTy,
92642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                    const ::clang::DeclContext*,const ::clang::DeclContext*> {};
9270ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
9280ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner} // end namespace llvm
9290ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
9300ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner#endif
931