DeclBase.h revision 7dda67d8decef1b3621a151488c4b83bd8372d6a
10ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner//===-- 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"
210ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner#include "clang/Basic/SourceLocation.h"
2244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor#include "llvm/ADT/PointerIntPair.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;
430ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
440ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner/// Decl - This represents one declaration (or definition), e.g. a variable,
450ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner/// typedef, function, struct, etc.
460ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///
470ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerclass Decl {
480ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerpublic:
4964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  /// \brief Lists the kind of concrete classes of Decl.
500ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  enum Kind {
5164650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL(Derived, Base) Derived,
5264650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL_RANGE(CommonBase, Start, End) \
5364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    CommonBase##First = Start, CommonBase##Last = End,
5464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define LAST_DECL_RANGE(CommonBase, Start, End) \
5564650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor    CommonBase##First = Start, CommonBase##Last = End
5664650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
570ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
580ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
590ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces,
602ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  /// labels, tags, members and ordinary identifiers. These are meant
612ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  /// as bitmasks, so that searches in C++ can look into the "tag" namespace
622ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  /// during ordinary lookup.
630ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  enum IdentifierNamespace {
642ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    IDNS_Label = 0x1,
652ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    IDNS_Tag = 0x2,
662ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    IDNS_Member = 0x4,
677dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor    IDNS_Ordinary = 0x8,
687dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor    IDNS_Protocol = 0x10
690ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
700ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
710ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// ObjCDeclQualifier - Qualifier used on types in method declarations
720ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// for remote messaging. They are meant for the arguments though and
730ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
740ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  enum ObjCDeclQualifier {
750ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_None = 0x0,
760ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_In = 0x1,
770ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Inout = 0x2,
780ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Out = 0x4,
790ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Bycopy = 0x8,
800ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Byref = 0x10,
810ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Oneway = 0x20
820ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
830ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
840ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerprivate:
850ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// Loc - The location that this decl.
860ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  SourceLocation Loc;
870ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
884afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// NextDeclarator - If this decl was part of a multi-declarator declaration,
894afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// such as "int X, Y, *Z;" this indicates Decl for the next declarator.
904afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  Decl *NextDeclarator;
914afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
924afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// NextDeclInScope - The next declaration within the same lexical
934afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// DeclContext. These pointers form the linked list that is
944afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// traversed via DeclContext's decls_begin()/decls_end().
954afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// FIXME: If NextDeclarator is non-NULL, will it always be the same
964afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// as NextDeclInScope? If so, we can use a
974afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// PointerIntPair<Decl*, 1> to make Decl smaller.
984afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  Decl *NextDeclInScope;
994afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1004afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  friend class DeclContext;
1014afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1024afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
1034afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// For declarations that don't contain C++ scope specifiers, it contains
1044afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// the DeclContext where the Decl was declared.
1054afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// For declarations with C++ scope specifiers, it contains a MultipleDC*
1064afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// with the context where it semantically belongs (SemanticDC) and the
1074afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// context where it was lexically declared (LexicalDC).
1084afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// e.g.:
1094afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///
1104afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   namespace A {
1114afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
1124afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   }
1134afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   void A::f(); // SemanticDC == namespace 'A'
1144afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///                // LexicalDC == global namespace
1154afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  uintptr_t DeclCtx;
1164afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1174afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  struct MultipleDC {
1184afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    DeclContext *SemanticDC;
1194afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    DeclContext *LexicalDC;
1204afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  };
1214afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1224afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  inline bool isInSemaDC() const { return (DeclCtx & 0x1) == 0; }
1234afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  inline bool isOutOfSemaDC() const { return (DeclCtx & 0x1) != 0; }
1244afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  inline MultipleDC *getMultipleDC() const {
1254afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return reinterpret_cast<MultipleDC*>(DeclCtx & ~0x1);
1264afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
1274afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1280ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// DeclKind - This indicates which class this is.
1290ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  Kind DeclKind   :  8;
1300ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1310ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// InvalidDecl - This indicates a semantic error occurred.
1320ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  unsigned int InvalidDecl :  1;
1330ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1340ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// HasAttrs - This indicates whether the decl has attributes or not.
1350ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  unsigned int HasAttrs : 1;
136d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis
1376b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// Implicit - Whether this declaration was implicitly generated by
1386b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// the implementation rather than explicitly written by the user.
1396b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  bool Implicit : 1;
1406b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor
1414afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorprotected:
142d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis  /// Access - Used by C++ decls for the access specifier.
143d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis  // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
144d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis  unsigned Access : 2;
145d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis  friend class CXXClassMemberWrapper;
146d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis
1474afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  Decl(Kind DK, DeclContext *DC, SourceLocation L)
1484afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    : Loc(L), NextDeclarator(0), NextDeclInScope(0),
1494afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      DeclCtx(reinterpret_cast<uintptr_t>(DC)), DeclKind(DK), InvalidDecl(0),
1504afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      HasAttrs(false), Implicit(false) {
1510ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    if (Decl::CollectingStats()) addDeclKind(DK);
1520ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
1531bb19638f2ec0d63ed131b51ca8d9542d1a9afeeSam Bishop
1540ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  virtual ~Decl();
1551bb19638f2ec0d63ed131b51ca8d9542d1a9afeeSam Bishop
1564afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// setDeclContext - Set both the semantic and lexical DeclContext
1574afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// to DC.
1584afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  void setDeclContext(DeclContext *DC);
1594afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1601bb19638f2ec0d63ed131b51ca8d9542d1a9afeeSam Bishoppublic:
1610ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  SourceLocation getLocation() const { return Loc; }
1620ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  void setLocation(SourceLocation L) { Loc = L; }
1630ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1640ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  Kind getKind() const { return DeclKind; }
1650ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  const char *getDeclKindName() const;
1660ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1674afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  const DeclContext *getDeclContext() const {
1684afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    if (isInSemaDC())
1694afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      return reinterpret_cast<DeclContext*>(DeclCtx);
1704afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return getMultipleDC()->SemanticDC;
1714afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
1724afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  DeclContext *getDeclContext() {
1734afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return const_cast<DeclContext*>(
1744afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                         const_cast<const Decl*>(this)->getDeclContext());
1754afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
1764afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1774afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  void setAccess(AccessSpecifier AS) { Access = AS; }
1784afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
1794afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
1800ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  void addAttr(Attr *attr);
1810ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  const Attr *getAttrs() const;
182a212c56e9b7533bcc2d6be90efd52ad241bf894eChris Lattner  void swapAttrs(Decl *D);
1839141bee299a05349924a34a7153d7064c9e0a107Nuno Lopes  void invalidateAttrs();
1840ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1850ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  template<typename T> const T *getAttr() const {
1860ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
1870ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      if (const T *V = dyn_cast<T>(attr))
1880ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner        return V;
1890ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1900ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    return 0;
1910ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
1920ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1930ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// setInvalidDecl - Indicates the Decl had a semantic error. This
1940ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// allows for graceful error recovery.
1950ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  void setInvalidDecl() { InvalidDecl = 1; }
1960ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  bool isInvalidDecl() const { return (bool) InvalidDecl; }
1976b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor
1986b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// isImplicit - Indicates whether the declaration was implicitly
1996b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// generated by the implementation. If false, this declaration
2006b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// was written explicitly in the source code.
2016b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  bool isImplicit() const { return Implicit; }
2026b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  void setImplicit(bool I = true) { Implicit = I; }
2030ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
2040ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  IdentifierNamespace getIdentifierNamespace() const {
2050ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    switch (DeclKind) {
2069d35097bc0fefb2f77638be513cac72d1c09d840Douglas Gregor    default:
2079d35097bc0fefb2f77638be513cac72d1c09d840Douglas Gregor      if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
2089d35097bc0fefb2f77638be513cac72d1c09d840Douglas Gregor        return IDNS_Ordinary;
2099d35097bc0fefb2f77638be513cac72d1c09d840Douglas Gregor      assert(0 && "Unknown decl kind!");
2103fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor    case OverloadedFunction:
2110ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    case Typedef:
2123fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor    case EnumConstant:
213248a753f6b670692523c99afaeb8fe98f7ae3ca7Steve Naroff    case Var:
2143fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor    case CXXClassVar:
2153fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor    case ImplicitParam:
2160ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    case ParmVar:
21773da9e462576faedc2cdf96b37a1c072b404b73dFariborz Jahanian    case OriginalParmVar:
21872c3f314d92d65c050ee1c07b7753623c044d6c7Douglas Gregor    case NonTypeTemplateParm:
2193fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor    case ObjCMethod:
2203fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor    case ObjCContainer:
2213fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor    case ObjCCategory:
2220ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    case ObjCInterface:
2233fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor    case ObjCCategoryImpl:
2243fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor    case ObjCProperty:
2250ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    case ObjCCompatibleAlias:
2260ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      return IDNS_Ordinary;
22772de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
2287dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor    case ObjCProtocol:
2297dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor      return IDNS_Protocol;
2307dda67d8decef1b3621a151488c4b83bd8372d6aDouglas Gregor
23172de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor    case Field:
23272de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor    case ObjCAtDefsField:
23372de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor    case ObjCIvar:
23472de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor      return IDNS_Member;
23572de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
23635bc0821c4f80041724cd4c5c4889b2581546a41Argyrios Kyrtzidis    case Record:
23735bc0821c4f80041724cd4c5c4889b2581546a41Argyrios Kyrtzidis    case CXXRecord:
2380ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    case Enum:
239efe38bdea21953f89c1504cfd1c63bee5f15b1b6Douglas Gregor    case TemplateTypeParm:
2400ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      return IDNS_Tag;
2413fd56d755aa74f01fbe963195c95c963ea1fee91Douglas Gregor
2422d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis    case Namespace:
243aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor    case Template:
244aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor    case FunctionTemplate:
245aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor    case ClassTemplate:
246aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor    case TemplateTemplateParm:
2472d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis      return IdentifierNamespace(IDNS_Tag | IDNS_Ordinary);
2480ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    }
2490ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
250792481eec23d8c1aa92173be589e2ae9d02514a5Ted Kremenek
251d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner  bool isInIdentifierNamespace(unsigned NS) const {
252d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner    return getIdentifierNamespace() & NS;
253d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner  }
254d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner
2554afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// getLexicalDeclContext - The declaration context where this Decl was
2564afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// lexically declared (LexicalDC). May be different from
2574afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// getDeclContext() (SemanticDC).
2584afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// e.g.:
2594afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///
2604afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   namespace A {
2614afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
2624afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   }
2634afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   void A::f(); // SemanticDC == namespace 'A'
2644afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///                // LexicalDC == global namespace
2654afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  const DeclContext *getLexicalDeclContext() const {
2664afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    if (isInSemaDC())
2674afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor      return reinterpret_cast<DeclContext*>(DeclCtx);
2684afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return getMultipleDC()->LexicalDC;
2694afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
2704afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  DeclContext *getLexicalDeclContext() {
2714afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return const_cast<DeclContext*>(
2724afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                  const_cast<const Decl*>(this)->getLexicalDeclContext());
2734afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
2744afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2754afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  void setLexicalDeclContext(DeclContext *DC);
2764afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2774afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// getNextDeclarator - If this decl was part of a multi-declarator
2784afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// declaration, such as "int X, Y, *Z;" this returns the decl for the next
2794afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// declarator.  Otherwise it returns null.
2804afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  Decl *getNextDeclarator() { return NextDeclarator; }
2814afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  const Decl *getNextDeclarator() const { return NextDeclarator; }
2824afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  void setNextDeclarator(Decl *N) { NextDeclarator = N; }
2834afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
2844afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
2854afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // scoped decl is defined outside the current function or method.  This is
2864afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // roughly global variables and functions, but also handles enums (which could
2874afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  // be defined inside or outside a function etc).
2884afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  bool isDefinedOutsideFunctionOrMethod() const;
2894afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
29069c8f0aab655257e9e532d9d53756acf4f7a2d78Ted Kremenek  // getBody - If this Decl represents a declaration for a body of code,
291792481eec23d8c1aa92173be589e2ae9d02514a5Ted Kremenek  //  such as a function or method definition, this method returns the top-level
29269c8f0aab655257e9e532d9d53756acf4f7a2d78Ted Kremenek  //  Stmt* of that body.  Otherwise this method returns null.
29369c8f0aab655257e9e532d9d53756acf4f7a2d78Ted Kremenek  virtual Stmt* getBody() const { return 0; }
294792481eec23d8c1aa92173be589e2ae9d02514a5Ted Kremenek
2950ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  // global temp stats (until we have a per-module visitor)
2960ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static void addDeclKind(Kind k);
2970ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static bool CollectingStats(bool Enable = false);
2980ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static void PrintStats();
2990ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
300f57172b24f08a68d179675989813d5479dc87829Douglas Gregor  /// isTemplateParameter - Determines whether this declartion is a
301f57172b24f08a68d179675989813d5479dc87829Douglas Gregor  /// template parameter.
302f57172b24f08a68d179675989813d5479dc87829Douglas Gregor  bool isTemplateParameter() const;
303f57172b24f08a68d179675989813d5479dc87829Douglas Gregor
3040ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  // Implement isa/cast/dyncast/etc.
3050ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static bool classof(const Decl *) { return true; }
30642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static DeclContext *castToDeclContext(const Decl *);
30742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static Decl *castFromDeclContext(const DeclContext *);
3080ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
3090ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// Emit - Serialize this Decl to Bitcode.
3100ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  void Emit(llvm::Serializer& S) const;
3110ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
3120ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// Create - Deserialize a Decl from Bitcode.
313e2563ca02a519c2ad6d64dfed87d6e86c5d3c072Sam Bishop  static Decl* Create(llvm::Deserializer& D, ASTContext& C);
3140ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
315bb45c512e0dfbe96bfe377ac642e726c0ba0affaSam Bishop  /// Destroy - Call destructors and release memory.
31627f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek  virtual void Destroy(ASTContext& C);
317bb45c512e0dfbe96bfe377ac642e726c0ba0affaSam Bishop
3180ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerprotected:
3190ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// EmitImpl - Provides the subclass-specific serialization logic for
3200ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  ///   serializing out a decl.
3210ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  virtual void EmitImpl(llvm::Serializer& S) const {
3220ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    // FIXME: This will eventually be a pure virtual function.
3230ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    assert (false && "Not implemented.");
3240ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
3250ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
3260ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
327b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner/// DeclContext - This is used only as base class of specific decl types that
3280ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner/// can act as declaration contexts. These decls are:
3290ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///
330ef177820100ab583b08fd3056e2a5a52ee4b1629Argyrios Kyrtzidis///   TranslationUnitDecl
3312d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis///   NamespaceDecl
3320ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///   FunctionDecl
33344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor///   RecordDecl/CXXRecordDecl
334b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner///   EnumDecl
335d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis///   ObjCMethodDecl
3360ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///   ObjCInterfaceDecl
337074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor///   LinkageSpecDecl
33856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff///   BlockDecl
339b048c9835969c4f7fe06264748be18ed4b442116Chris Lattnerclass DeclContext {
3400ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// DeclKind - This indicates which class this is.
3410ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  Decl::Kind DeclKind   :  8;
3420ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
34344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// LookupPtrKind - Describes what kind of pointer LookupPtr
34444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// actually is.
34544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  enum LookupPtrKind {
3463fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor    /// LookupIsMap - Indicates that LookupPtr is actually a map.
34744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    LookupIsMap = 7
34844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  };
34944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
35044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// LookupPtr - Pointer to a data structure used to lookup
35144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// declarations within this context. If the context contains fewer
35244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// than seven declarations, the number of declarations is provided
35344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// in the 3 lowest-order bits and the upper bits are treated as a
3544afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// pointer to an array of NamedDecl pointers. If the context
35544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// contains seven or more declarations, the upper bits are treated
3564afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// as a pointer to a DenseMap<DeclarationName, std::vector<NamedDecl*>>.
3573fc749d899dfc194162128c1a88933148a39b68dDouglas Gregor  /// FIXME: We need a better data structure for this.
35844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  llvm::PointerIntPair<void*, 3> LookupPtr;
35944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
3606037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// FirstDecl - The first declaration stored within this declaration
3616037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// context.
3624afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  Decl *FirstDecl;
3636037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
3646037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// LastDecl - The last declaration stored within this declaration
3656037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// context. FIXME: We could probably cache this value somewhere
3666037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// outside of the DeclContext, to reduce the size of DeclContext by
3676037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// another pointer.
3684afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  Decl *LastDecl;
3697643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
3700ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  // Used in the CastTo template to get the DeclKind
371b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  // from a Decl or a DeclContext. DeclContext doesn't have a getKind() method
3720ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  // to avoid 'ambiguous access' compiler errors.
3730ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  template<typename T> struct KindTrait {
3740ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    static Decl::Kind getKind(const T *D) { return D->getKind(); }
3750ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
3760ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
3770ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  // Used only by the ToDecl and FromDecl methods
3780ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  template<typename To, typename From>
3790ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static To *CastTo(const From *D) {
3800ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    Decl::Kind DK = KindTrait<From>::getKind(D);
3810ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    switch(DK) {
382ef177820100ab583b08fd3056e2a5a52ee4b1629Argyrios Kyrtzidis      case Decl::TranslationUnit:
383ef177820100ab583b08fd3056e2a5a52ee4b1629Argyrios Kyrtzidis        return static_cast<TranslationUnitDecl*>(const_cast<From*>(D));
3842d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis      case Decl::Namespace:
3852d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis        return static_cast<NamespaceDecl*>(const_cast<From*>(D));
386d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis      case Decl::Enum:
387d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis        return static_cast<EnumDecl*>(const_cast<From*>(D));
38844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      case Decl::Record:
38944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor        return static_cast<RecordDecl*>(const_cast<From*>(D));
39035bc0821c4f80041724cd4c5c4889b2581546a41Argyrios Kyrtzidis      case Decl::CXXRecord:
39135bc0821c4f80041724cd4c5c4889b2581546a41Argyrios Kyrtzidis        return static_cast<CXXRecordDecl*>(const_cast<From*>(D));
3920ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      case Decl::ObjCMethod:
3930ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner        return static_cast<ObjCMethodDecl*>(const_cast<From*>(D));
3940ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      case Decl::ObjCInterface:
3950ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner        return static_cast<ObjCInterfaceDecl*>(const_cast<From*>(D));
3960701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff      case Decl::ObjCCategory:
3970701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff        return static_cast<ObjCCategoryDecl*>(const_cast<From*>(D));
3980701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff      case Decl::ObjCProtocol:
3990701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff        return static_cast<ObjCProtocolDecl*>(const_cast<From*>(D));
4000701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff      case Decl::ObjCImplementation:
4010701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff        return static_cast<ObjCImplementationDecl*>(const_cast<From*>(D));
4020701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff      case Decl::ObjCCategoryImpl:
4030701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff        return static_cast<ObjCCategoryImplDecl*>(const_cast<From*>(D));
404074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      case Decl::LinkageSpec:
405074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor        return static_cast<LinkageSpecDecl*>(const_cast<From*>(D));
406074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor      case Decl::Block:
407074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor        return static_cast<BlockDecl*>(const_cast<From*>(D));
4080ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      default:
409d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis        if (DK >= Decl::FunctionFirst && DK <= Decl::FunctionLast)
410d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis          return static_cast<FunctionDecl*>(const_cast<From*>(D));
411d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis
412b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner        assert(false && "a decl that inherits DeclContext isn't handled");
4130ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner        return 0;
4140ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    }
4150ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
4160ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
41744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// isLookupMap - Determine if the lookup structure is a
41844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// DenseMap. Othewise, it is an array.
41944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; }
42044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4210cba85577ae613bce5768f3089003629a46b0e7fDouglas Gregor  static Decl *getNextDeclInScope(Decl *D) { return D->NextDeclInScope; }
4220cba85577ae613bce5768f3089003629a46b0e7fDouglas Gregor
4230ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerprotected:
4246037fcba3431b47de1a994c9b286feac17894effDouglas Gregor   DeclContext(Decl::Kind K)
4256037fcba3431b47de1a994c9b286feac17894effDouglas Gregor     : DeclKind(K), LookupPtr(), FirstDecl(0), LastDecl(0) { }
42644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
42744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  void DestroyDecls(ASTContext &C);
4280ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
4290ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerpublic:
43044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ~DeclContext();
43144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4329b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis  Decl::Kind getDeclKind() const {
4339b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis    return DeclKind;
4349b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis  }
4350a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  const char *getDeclKindName() const;
4369b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis
4374afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// getParent - Returns the containing DeclContext if this is a Decl,
4380ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// else returns NULL.
43920bc6762282d192bd19be03094d4f311710e020cArgyrios Kyrtzidis  const DeclContext *getParent() const;
44020bc6762282d192bd19be03094d4f311710e020cArgyrios Kyrtzidis  DeclContext *getParent() {
44120bc6762282d192bd19be03094d4f311710e020cArgyrios Kyrtzidis    return const_cast<DeclContext*>(
44220bc6762282d192bd19be03094d4f311710e020cArgyrios Kyrtzidis                             const_cast<const DeclContext*>(this)->getParent());
443d2595ecce5f8350e485c83bfe767549a522b2802Argyrios Kyrtzidis  }
4440ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
44577407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  /// getLexicalParent - Returns the containing lexical DeclContext. May be
44644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// different from getParent, e.g.:
44744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///
44844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   namespace A {
44944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///      struct S;
45044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   }
45144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   struct A::S {}; // getParent() == namespace 'A'
45277407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  ///                   // getLexicalParent() == translation unit
45377407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  ///
45477407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  const DeclContext *getLexicalParent() const;
45577407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  DeclContext *getLexicalParent() {
45677407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis    return const_cast<DeclContext*>(
45777407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis                      const_cast<const DeclContext*>(this)->getLexicalParent());
45877407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  }
45977407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis
4600ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  bool isFunctionOrMethod() const {
4610ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    switch (DeclKind) {
46256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff      case Decl::Block:
4630ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      case Decl::ObjCMethod:
4640ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner        return true;
46544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4660ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      default:
46744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor       if (DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast)
46844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor         return true;
4690ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner        return false;
4700ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    }
4710ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
4720ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
473ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  bool isFileContext() const {
474ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis    return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
475ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  }
476ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis
477bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  bool isRecord() const {
478bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor    return DeclKind == Decl::Record || DeclKind == Decl::CXXRecord;
479c7ed9c60b8ee04b119e23441cae2cfec74536ba9Argyrios Kyrtzidis  }
480c7ed9c60b8ee04b119e23441cae2cfec74536ba9Argyrios Kyrtzidis
48144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  bool isNamespace() const {
48244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return DeclKind == Decl::Namespace;
48344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
48444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
485074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// isTransparentContext - Determines whether this context is a
486074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// "transparent" context, meaning that the members declared in this
487074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// context are semantically declared in the nearest enclosing
488074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// non-transparent (opaque) context but are lexically declared in
489074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// this context. For example, consider the enumerators of an
490074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// enumeration type:
491074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// @code
492074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// enum E {
493074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  ///   Val1
494074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// };
495074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// @endcode
496074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// Here, E is a transparent context, so its enumerator (Val1) will
497074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// appear (semantically) that it is in the same context of E.
498074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// Examples of transparent contexts include: enumerations (except for
499074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// C++0x scoped enums), C++ linkage specifications, and C++0x
500074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// inline namespaces.
501074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  bool isTransparentContext() const;
502074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
503ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  bool Encloses(DeclContext *DC) const {
504ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis    for (; DC; DC = DC->getParent())
505ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis      if (DC == this)
506ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis        return true;
507ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis    return false;
508ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  }
509ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis
51044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// getPrimaryContext - There may be many different
51144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// declarations of the same entity (including forward declarations
51244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// of classes, multiple definitions of namespaces, etc.), each with
51344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// a different set of declarations. This routine returns the
51444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// "primary" DeclContext structure, which will contain the
51544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// information needed to perform name lookup into this context.
5160701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *getPrimaryContext();
51744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
518ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  /// getLookupContext - Retrieve the innermost non-transparent
519ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  /// context of this context, which corresponds to the innermost
520ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  /// location from which name lookup can find the entities in this
521ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor  /// context.
52217a9b9e3ee75f5dbb4819cc8ebf40eec8015f84aDouglas Gregor  DeclContext *getLookupContext() {
52317a9b9e3ee75f5dbb4819cc8ebf40eec8015f84aDouglas Gregor    return const_cast<DeclContext *>(
52417a9b9e3ee75f5dbb4819cc8ebf40eec8015f84aDouglas Gregor             const_cast<const DeclContext *>(this)->getLookupContext());
52517a9b9e3ee75f5dbb4819cc8ebf40eec8015f84aDouglas Gregor  }
52617a9b9e3ee75f5dbb4819cc8ebf40eec8015f84aDouglas Gregor  const DeclContext *getLookupContext() const;
527ce35607c282c845b3285d0f6e106489d8bbeba13Douglas Gregor
52844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// getNextContext - If this is a DeclContext that may have other
52944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// DeclContexts that are semantically connected but syntactically
53044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// different, such as C++ namespaces, this routine retrieves the
53144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// next DeclContext in the link. Iteration through the chain of
53244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// DeclContexts should begin at the primary DeclContext and
53344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// continue until this function returns NULL. For example, given:
53444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// @code
53544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// namespace N {
53644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   int x;
53744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// }
53844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// namespace N {
53944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   int y;
54044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// }
54144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// @endcode
54244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// The first occurrence of namespace N will be the primary
54344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// DeclContext. Its getNextContext will return the second
54444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// occurrence of namespace N.
54544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  DeclContext *getNextContext();
54644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
54744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// decl_iterator - Iterates through the declarations stored
54844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// within this context.
5496037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  class decl_iterator {
5506037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    /// Current - The current declaration.
5514afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    Decl *Current;
5526037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5536037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  public:
5544afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    typedef Decl*                     value_type;
5554afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    typedef Decl*                     reference;
5564afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    typedef Decl*                     pointer;
5576037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    typedef std::forward_iterator_tag iterator_category;
5586037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    typedef std::ptrdiff_t            difference_type;
5596037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5606037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    decl_iterator() : Current(0) { }
5614afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    explicit decl_iterator(Decl *C) : Current(C) { }
5626037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5636037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    reference operator*() const { return Current; }
5646037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    pointer operator->() const { return Current; }
5656037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5666037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    decl_iterator& operator++();
5676037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5686037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    decl_iterator operator++(int) {
5696037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      decl_iterator tmp(*this);
5706037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      ++(*this);
5716037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      return tmp;
5726037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    }
5736037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
5746037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    friend bool operator==(decl_iterator x, decl_iterator y) {
5756037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      return x.Current == y.Current;
5766037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    }
5776037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    friend bool operator!=(decl_iterator x, decl_iterator y) {
5786037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      return x.Current != y.Current;
5796037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    }
5806037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  };
58144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
58244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// decls_begin/decls_end - Iterate over the declarations stored in
58344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// this context.
5846037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  decl_iterator decls_begin() const { return decl_iterator(FirstDecl); }
5856037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  decl_iterator decls_end()   const { return decl_iterator(); }
58644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
587f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  /// specific_decl_iterator - Iterates over a subrange of
588f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  /// declarations stored in a DeclContext, providing only those that
589669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// are of type SpecificDecl (or a class derived from it). This
590f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  /// iterator is used, for example, to provide iteration over just
591669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
592f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  template<typename SpecificDecl>
593f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  class specific_decl_iterator {
594f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// Current - The current, underlying declaration iterator, which
595d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// will either be NULL or will point to a declaration of
596f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// type SpecificDecl.
597f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    DeclContext::decl_iterator Current;
598f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
599f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// SkipToNextDecl - Advances the current position up to the next
600f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// declaration of type SpecificDecl that also meets the criteria
601f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// required by Acceptable.
602f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    void SkipToNextDecl() {
603669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      while (*Current && !isa<SpecificDecl>(*Current))
604f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor        ++Current;
605f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
606f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
607f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  public:
608f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef SpecificDecl* value_type;
609f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef SpecificDecl* reference;
610f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef SpecificDecl* pointer;
611f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
612f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      difference_type;
613f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef std::forward_iterator_tag iterator_category;
614f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
615669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    specific_decl_iterator() : Current() { }
616f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
617f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// specific_decl_iterator - Construct a new iterator over a
618d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// subset of the declarations the range [C,
619d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// end-of-declarations). If A is non-NULL, it is a pointer to a
620d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// member function of SpecificDecl that should return true for
621d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// all of the SpecificDecl instances that will be in the subset
622d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// of iterators. For example, if you want Objective-C instance
623d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
624d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// &ObjCMethodDecl::isInstanceMethod.
625669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
626f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      SkipToNextDecl();
627f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
628f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
6296037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    reference operator*() const { return cast<SpecificDecl>(*Current); }
6306037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    pointer operator->() const { return cast<SpecificDecl>(*Current); }
631f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
632f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    specific_decl_iterator& operator++() {
633f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      ++Current;
634f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      SkipToNextDecl();
635f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return *this;
636f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
637f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
638f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    specific_decl_iterator operator++(int) {
639f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      specific_decl_iterator tmp(*this);
640f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      ++(*this);
641f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return tmp;
642f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
643f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
644f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    friend bool
645f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
646f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return x.Current == y.Current;
647f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
648f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
649f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    friend bool
650f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
651f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return x.Current != y.Current;
652f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
653f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  };
654f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
655669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// \brief Iterates over a filtered subrange of declarations stored
656669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// in a DeclContext.
657669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  ///
658669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// This iterator visits only those declarations that are of type
659669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// SpecificDecl (or a class derived from it) and that meet some
660669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// additional run-time criteria. This iterator is used, for
661669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// example, to provide access to the instance methods within an
662669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
663669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// Acceptable = ObjCMethodDecl::isInstanceMethod).
664669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
665669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  class filtered_decl_iterator {
666669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// Current - The current, underlying declaration iterator, which
667669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// will either be NULL or will point to a declaration of
668669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// type SpecificDecl.
669669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    DeclContext::decl_iterator Current;
670669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
671669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// SkipToNextDecl - Advances the current position up to the next
672669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// declaration of type SpecificDecl that also meets the criteria
673669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// required by Acceptable.
674669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    void SkipToNextDecl() {
675669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      while (*Current &&
676669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor             (!isa<SpecificDecl>(*Current) ||
677669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor              (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
678669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor        ++Current;
679669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
680669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
681669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  public:
682669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef SpecificDecl* value_type;
683669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef SpecificDecl* reference;
684669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef SpecificDecl* pointer;
685669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
686669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      difference_type;
687669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef std::forward_iterator_tag iterator_category;
688669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
689669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    filtered_decl_iterator() : Current() { }
690669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
691669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// specific_decl_iterator - Construct a new iterator over a
692669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// subset of the declarations the range [C,
693669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// end-of-declarations). If A is non-NULL, it is a pointer to a
694669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// member function of SpecificDecl that should return true for
695669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// all of the SpecificDecl instances that will be in the subset
696669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// of iterators. For example, if you want Objective-C instance
697669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
698669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// &ObjCMethodDecl::isInstanceMethod.
699669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
700669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      SkipToNextDecl();
701669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
702669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
703669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    reference operator*() const { return cast<SpecificDecl>(*Current); }
704669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    pointer operator->() const { return cast<SpecificDecl>(*Current); }
705669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
706669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    filtered_decl_iterator& operator++() {
707669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      ++Current;
708669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      SkipToNextDecl();
709669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return *this;
710669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
711669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
712669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    filtered_decl_iterator operator++(int) {
713669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      filtered_decl_iterator tmp(*this);
714669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      ++(*this);
715669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return tmp;
716669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
717669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
718669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    friend bool
719669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
720669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return x.Current == y.Current;
721669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
722669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
723669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    friend bool
724669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
725669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return x.Current != y.Current;
726669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
727669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  };
728669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
72940f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// @brief Add the declaration D into this context.
73040f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
73140f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// This routine should be invoked when the declaration D has first
73240f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// been declared, to place D into the context where it was
73340f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// (lexically) defined. Every declaration must be added to one
73440f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// (and only one!) context, where it can be visited via
73540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// [decls_begin(), decls_end()). Once a declaration has been added
73640f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// to its lexical context, the corresponding DeclContext owns the
73740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// declaration.
73840f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
73940f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// If D is also a NamedDecl, it will be made visible within its
74040f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// semantic context via makeDeclVisibleInContext.
7414afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  void addDecl(Decl *D);
74244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
74344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// lookup_iterator - An iterator that provides access to the results
74444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// of looking up a name within this context.
7454afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  typedef NamedDecl **lookup_iterator;
74644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
74744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// lookup_const_iterator - An iterator that provides non-mutable
74844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// access to the results of lookup up a name within this context.
7494afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  typedef NamedDecl * const * lookup_const_iterator;
75044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
75144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
75244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  typedef std::pair<lookup_const_iterator, lookup_const_iterator>
75344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    lookup_const_result;
75444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
75544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// lookup - Find the declarations (if any) with the given Name in
75644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// this context. Returns a range of iterators that contains all of
75740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// the declarations with this name, with object, function, member,
75840f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// and enumerator names preceding any tag name. Note that this
75940f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// routine will not look into parent contexts.
7600701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  lookup_result lookup(DeclarationName Name);
7610701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  lookup_const_result lookup(DeclarationName Name) const;
76244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
76340f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// @brief Makes a declaration visible within this context.
76440f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
76540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// This routine makes the declaration D visible to name lookup
76640f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// within this context and, if this is a transparent context,
76740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// within its parent contexts up to the first enclosing
76840f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// non-transparent context. Making a declaration visible within a
76940f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// context does not transfer ownership of a declaration, and a
77040f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// declaration can be visible in many contexts that aren't its
77140f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// lexical context.
77240f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
77340f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// If D is a redeclaration of an existing declaration that is
77440f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// visible from this context, as determined by
77540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// NamedDecl::declarationReplaces, the previous declaration will be
77640f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// replaced with D.
77740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  void makeDeclVisibleInContext(NamedDecl *D);
7787643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
7792a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  /// udir_iterator - Iterates through the using-directives stored
7802a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  /// within this context.
7812a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  typedef UsingDirectiveDecl * const * udir_iterator;
7822a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
7832a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
7842a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
7852a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  udir_iterator_range getUsingDirectives() const;
7862a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
7872a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  udir_iterator using_directives_begin() const {
7882a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return getUsingDirectives().first;
7892a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
7902a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
7912a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  udir_iterator using_directives_end() const {
7922a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return getUsingDirectives().second;
7932a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
7942a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
7950ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static bool classof(const Decl *D) {
7960ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    switch (D->getKind()) {
79764650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL_CONTEXT(Name) case Decl::Name:
79864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
7990ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner        return true;
8000ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      default:
801d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis        if (D->getKind() >= Decl::FunctionFirst &&
802d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis            D->getKind() <= Decl::FunctionLast)
803d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis          return true;
804b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner        return false;
8050ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    }
8060ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
807b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static bool classof(const DeclContext *D) { return true; }
80864650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#define DECL_CONTEXT(Name) \
80964650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  static bool classof(const Name##Decl *D) { return true; }
81064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor#include "clang/AST/DeclNodes.def"
8117643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
8127643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidisprivate:
81340f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  void buildLookup(DeclContext *DCtx);
81440f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  void makeDeclVisibleInContextImpl(NamedDecl *D);
81544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
8167643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis  void EmitOutRec(llvm::Serializer& S) const;
8177643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis  void ReadOutRec(llvm::Deserializer& D, ASTContext& C);
8187643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
8197643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis  friend class Decl;
8200ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
8210ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
822b048c9835969c4f7fe06264748be18ed4b442116Chris Lattnertemplate<> struct DeclContext::KindTrait<DeclContext> {
823b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; }
8240ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
8250ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
826f57172b24f08a68d179675989813d5479dc87829Douglas Gregorinline bool Decl::isTemplateParameter() const {
827f57172b24f08a68d179675989813d5479dc87829Douglas Gregor  return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm;
828f57172b24f08a68d179675989813d5479dc87829Douglas Gregor}
829f57172b24f08a68d179675989813d5479dc87829Douglas Gregor
8304afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorinline bool Decl::isDefinedOutsideFunctionOrMethod() const {
8314afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  if (getDeclContext())
8324afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return !getDeclContext()->getLookupContext()->isFunctionOrMethod();
8334afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  else
8344afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return true;
8354afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
8364afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
8374afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorinline DeclContext::decl_iterator& DeclContext::decl_iterator::operator++() {
8380cba85577ae613bce5768f3089003629a46b0e7fDouglas Gregor  Current = getNextDeclInScope(Current);
8394afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  return *this;
8404afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
8414afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
8420ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner} // end clang.
8430ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
8440ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnernamespace llvm {
8450ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
84642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis/// Implement a isa_impl_wrap specialization to check whether a DeclContext is
84742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis/// a specific Decl.
84842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
84942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct isa_impl_wrap<ToTy,
85042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                     const ::clang::DeclContext,const ::clang::DeclContext> {
85142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static bool doit(const ::clang::DeclContext &Val) {
85242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return ToTy::classof(::clang::Decl::castFromDeclContext(&Val));
8530ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
8540ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
85542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
85642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
85742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  : public isa_impl_wrap<ToTy,
85842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                      const ::clang::DeclContext,const ::clang::DeclContext> {};
8590ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
86042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis/// Implement cast_convert_val for Decl -> DeclContext conversions.
8610ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnertemplate<class FromTy>
86242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
863b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static ::clang::DeclContext &doit(const FromTy &Val) {
86442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return *FromTy::castToDeclContext(&Val);
8650ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
8660ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
8670ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
8680ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnertemplate<class FromTy>
86942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
870b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static ::clang::DeclContext *doit(const FromTy *Val) {
87142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return FromTy::castToDeclContext(Val);
8720ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
8730ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
87442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
87544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregortemplate<class FromTy>
87644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorstruct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
87744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  static const ::clang::DeclContext &doit(const FromTy &Val) {
87844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return *FromTy::castToDeclContext(&Val);
87944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
88044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor};
88144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
88244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregortemplate<class FromTy>
88344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorstruct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
88444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  static const ::clang::DeclContext *doit(const FromTy *Val) {
88544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return FromTy::castToDeclContext(Val);
88644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
88744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor};
88844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
88942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis/// Implement cast_convert_val for DeclContext -> Decl conversions.
89042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
89142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val<ToTy,
89242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                        const ::clang::DeclContext,const ::clang::DeclContext> {
89342220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static ToTy &doit(const ::clang::DeclContext &Val) {
89442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val));
89542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  }
89642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis};
89742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
89842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext>
89942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  : public cast_convert_val<ToTy,
90042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                      const ::clang::DeclContext,const ::clang::DeclContext> {};
90142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
90242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
90342220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val<ToTy,
90442220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                     const ::clang::DeclContext*, const ::clang::DeclContext*> {
90542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static ToTy *doit(const ::clang::DeclContext *Val) {
90642220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val));
90742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  }
90842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis};
90942220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidistemplate<class ToTy>
91042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*>
91142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  : public cast_convert_val<ToTy,
91242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis                    const ::clang::DeclContext*,const ::clang::DeclContext*> {};
9130ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
9140ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner} // end namespace llvm
9150ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
9160ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner#endif
917