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
172fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/AttrIterator.h"
18514d3b6b93c83c0841d2f9dd7af8ecc2877fe921Douglas Gregor#include "clang/AST/DeclarationName.h"
19ad2b804faf29042e6c4e331d0987f103f1e2fd31John McCall#include "clang/Basic/Specifiers.h"
20ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner#include "llvm/ADT/PointerUnion.h"
21651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/ADT/iterator_range.h"
22aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar#include "llvm/Support/Compiler.h"
23aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar#include "llvm/Support/PrettyStackTrace.h"
240ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
250ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnernamespace clang {
269852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass ASTMutationListener;
279852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass BlockDecl;
28d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidisclass CXXRecordDecl;
299852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass CompoundStmt;
309852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass DeclContext;
319852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass DeclarationName;
329852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass DependentDiagnostic;
33b048c9835969c4f7fe06264748be18ed4b442116Chris Lattnerclass EnumDecl;
349852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass FunctionDecl;
35651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesclass FunctionType;
366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesenum Linkage : unsigned char;
37dc0705645276258afddb06cdf0fd7c8840251ee0Rafael Espindolaclass LinkageComputer;
389852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass LinkageSpecDecl;
39ca2ab45341c448284cf93770018c717810575f86Douglas Gregorclass Module;
409852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass NamedDecl;
419852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass NamespaceDecl;
420701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroffclass ObjCCategoryDecl;
430701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroffclass ObjCCategoryImplDecl;
449852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass ObjCContainerDecl;
45bfb498d0996ef049efe9476f2802976fd145cd60Argyrios Kyrtzidisclass ObjCImplDecl;
469852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass ObjCImplementationDecl;
479852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass ObjCInterfaceDecl;
489852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass ObjCMethodDecl;
499852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass ObjCProtocolDecl;
509852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerstruct PrintingPolicy;
519852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass Stmt;
520c01d18094100db92d38daa923c95661512db203John McCallclass StoredDeclsMap;
539852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass TranslationUnitDecl;
549852f58f50b4fc20914fbce5b4454135a42343f4Benjamin Kramerclass UsingDirectiveDecl;
550eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner}
560eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattner
570eda3b31a672ea486fa92b9bc49a2c91be856b53Chris Lattnernamespace clang {
580ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
590a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \brief Captures the result of checking the availability of a
600a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// declaration.
610a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  enum AvailabilityResult {
620a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    AR_Available = 0,
630a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    AR_NotYetIntroduced,
640a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    AR_Deprecated,
650a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    AR_Unavailable
660a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  };
670a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Decl - This represents one declaration (or definition), e.g. a variable,
691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typedef, function, struct, etc.
700ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///
710ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerclass Decl {
720ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerpublic:
7364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor  /// \brief Lists the kind of concrete classes of Decl.
740ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  enum Kind {
759a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL(DERIVED, BASE) DERIVED,
769a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define ABSTRACT_DECL(DECL)
779a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define DECL_RANGE(BASE, START, END) \
789a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        first##BASE = START, last##BASE = END,
799a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#define LAST_DECL_RANGE(BASE, START, END) \
809a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt        first##BASE = START, last##BASE = END
819a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt#include "clang/AST/DeclNodes.inc"
820ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
830ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
846ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  /// \brief A placeholder type used to construct an empty shell of a
856ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  /// decl-derived type that will be filled in later (e.g., by some
866ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  /// deserialization method).
876ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  struct EmptyShell { };
886ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
890d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  /// IdentifierNamespace - The different namespaces in which
900d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  /// declarations may appear.  According to C99 6.2.3, there are
910d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  /// four namespaces, labels, tags, members and ordinary
920d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  /// identifiers.  C++ describes lookup completely differently:
930d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  /// certain lookups merely "ignore" certain kinds of declarations,
940d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  /// usually based on whether the declaration is of a type, etc.
95ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  ///
960d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  /// These are meant as bitmasks, so that searches in
970d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  /// C++ can look into the "tag" namespace during ordinary lookup.
980d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  ///
9907a353c2af3a3c557205466d4f4ed2513462ebfeSebastian Redl  /// Decl currently provides 15 bits of IDNS bits.
1000ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  enum IdentifierNamespace {
1010d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// Labels, declared with 'x:' and referenced with 'goto x'.
1020d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    IDNS_Label               = 0x0001,
1030d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
1040d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// Tags, declared with 'struct foo;' and referenced with
1050d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// 'struct foo'.  All tags are also types.  This is what
1060d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// elaborated-type-specifiers look for in C.
1070d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    IDNS_Tag                 = 0x0002,
1080d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
1090d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// Types, declared with 'struct foo', typedefs, etc.
1100d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// This is what elaborated-type-specifiers look for in C++,
1110d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// but note that it's ill-formed to find a non-tag.
1120d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    IDNS_Type                = 0x0004,
1130d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
1140d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// Members, declared with object declarations within tag
1150d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// definitions.  In C, these can only be found by "qualified"
1160d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// lookup in member expressions.  In C++, they're found by
1170d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// normal lookup.
1180d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    IDNS_Member              = 0x0008,
1190d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
1200d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// Namespaces, declared with 'namespace foo {}'.
1210d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// Lookup for nested-name-specifiers find these.
1220d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    IDNS_Namespace           = 0x0010,
1230d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
1240d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// Ordinary names.  In C, everything that's not a label, tag,
1250d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// or member ends up here.
1260d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    IDNS_Ordinary            = 0x0020,
1270d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
128d4582b8e6d056f5d991f1b8372e89a6aae58feaeJordan Rose    /// Objective C \@protocol.
1290d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    IDNS_ObjCProtocol        = 0x0040,
1300d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
1310d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// This declaration is a friend function.  A friend function
1320d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// declaration is always in this namespace but may also be in
1330d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// IDNS_Ordinary if it was previously declared.
1340d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    IDNS_OrdinaryFriend      = 0x0080,
1350d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
1360d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// This declaration is a friend class.  A friend class
1370d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// declaration is always in this namespace but may also be in
1380d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// IDNS_Tag|IDNS_Type if it was previously declared.
1390d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    IDNS_TagFriend           = 0x0100,
1400d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall
1410d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// This declaration is a using declaration.  A using declaration
1420d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// *introduces* a number of other declarations into the current
1430d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// scope, and those declarations use the IDNS of their targets,
1440d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    /// but the actual using declarations go in this namespace.
14576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    IDNS_Using               = 0x0200,
14676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
14776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    /// This declaration is a C++ operator declared in a non-class
14876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    /// context.  All such operators are also in IDNS_Ordinary.
14976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    /// C++ lexical operator lookup looks for these.
150a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    IDNS_NonMemberOperator   = 0x0400,
151a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
152a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    /// This declaration is a function-local extern declaration of a
153a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    /// variable or function. This may also be IDNS_Ordinary if it
154a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    /// has been declared outside any function.
155a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    IDNS_LocalExtern         = 0x0800
1560ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15809e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
15909e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// parameter types in method declarations.  Other than remembering
16009e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// them and mangling them into the method's signature string, these
16109e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// are ignored by the compiler; they are consumed by certain
16209e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// remote-messaging frameworks.
16309e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  ///
16409e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// in, inout, and out are mutually exclusive and apply only to
16509e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// method parameters.  bycopy and byref are mutually exclusive and
16609e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// apply only to method parameters (?).  oneway applies only to
16709e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// results.  All of these expect their corresponding parameter to
16809e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// have a particular type.  None of this is currently enforced by
16909e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// clang.
17009e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  ///
17109e2c524a18435211cfbc2fb355f91e1ac43ea89John McCall  /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
1720ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  enum ObjCDeclQualifier {
1730ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_None = 0x0,
1740ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_In = 0x1,
1750ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Inout = 0x2,
1760ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Out = 0x4,
1770ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Bycopy = 0x8,
1780ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Byref = 0x10,
1790ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    OBJC_TQ_Oneway = 0x20
1800ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  };
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregorprotected:
18346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  // Enumeration values used in the bits stored in NextInContextAndBits.
18446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  enum {
18546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    /// \brief Whether this declaration is a top-level declaration (function,
18646cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    /// global variable, etc.) that is lexically inside an objc container
18746cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    /// definition.
18846cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    TopLevelDeclInObjCContainerFlag = 0x01,
18946cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor
19046cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    /// \brief Whether this declaration is private to the module in which it was
19146cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    /// defined.
19246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    ModulePrivateFlag = 0x02
19346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  };
19446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor
19546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  /// \brief The next declaration within the same lexical
1964afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// DeclContext. These pointers form the linked list that is
1974afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// traversed via DeclContext's decls_begin()/decls_end().
19846cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  ///
19946cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  /// The extra two bits are used for the TopLevelDeclInObjCContainer and
20046cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  /// ModulePrivate bits.
20146cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  llvm::PointerIntPair<Decl *, 2, unsigned> NextInContextAndBits;
2024afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
20346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregorprivate:
2044afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  friend class DeclContext;
2054afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
206ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  struct MultipleDC {
207ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    DeclContext *SemanticDC;
208ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    DeclContext *LexicalDC;
209ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  };
2101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2124afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
2134afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// For declarations that don't contain C++ scope specifiers, it contains
2144afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// the DeclContext where the Decl was declared.
2154afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// For declarations with C++ scope specifiers, it contains a MultipleDC*
2164afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// with the context where it semantically belongs (SemanticDC) and the
2174afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// context where it was lexically declared (LexicalDC).
2184afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// e.g.:
2194afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///
2204afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   namespace A {
2214afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
2224afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   }
2234afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   void A::f(); // SemanticDC == namespace 'A'
2244afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///                // LexicalDC == global namespace
225ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
2264afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
227ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  inline bool isInSemaDC() const    { return DeclCtx.is<DeclContext*>(); }
228ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner  inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
2294afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  inline MultipleDC *getMultipleDC() const {
230ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    return DeclCtx.get<MultipleDC*>();
2314afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
23210d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner  inline DeclContext *getSemanticDC() const {
233ee219fd5f2776d8dd39d857f87304297b5ed743aChris Lattner    return DeclCtx.get<DeclContext*>();
23410d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner  }
2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
236381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// Loc - The location of this decl.
23739d7650656fdd1be879ed7d0c722cfeae95e56d5Daniel Dunbar  SourceLocation Loc;
2381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2390ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// DeclKind - This indicates which class this is.
240838dc597e25614c653a062a124b06a04d6b7f5ebRafael Espindola  unsigned DeclKind : 8;
2411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2420ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// InvalidDecl - This indicates a semantic error occurred.
24335043e56150dc4eda882acd5ddfe0f8e3c0a8cb1John McCall  unsigned InvalidDecl :  1;
2441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2450ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// HasAttrs - This indicates whether the decl has attributes or not.
24635043e56150dc4eda882acd5ddfe0f8e3c0a8cb1John McCall  unsigned HasAttrs : 1;
247d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis
2486b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// Implicit - Whether this declaration was implicitly generated by
2496b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// the implementation rather than explicitly written by the user.
25035043e56150dc4eda882acd5ddfe0f8e3c0a8cb1John McCall  unsigned Implicit : 1;
2516b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor
252e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  /// \brief Whether this declaration was "used", meaning that a definition is
253e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  /// required.
25435043e56150dc4eda882acd5ddfe0f8e3c0a8cb1John McCall  unsigned Used : 1;
2559cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor
2566b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  /// \brief Whether this declaration was "referenced".
2576b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  /// The difference with 'Used' is whether the reference appears in a
2586b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  /// evaluated context or not, e.g. functions used in uninstantiated templates
2596b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  /// are regarded as "referenced" but not "used".
2606b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  unsigned Referenced : 1;
261ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
26202892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar  /// \brief Whether statistic collection is enabled.
26302892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar  static bool StatisticsEnabled;
26402892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar
2659cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregorprotected:
2667d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  /// Access - Used by C++ decls for the access specifier.
2677d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
2687d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  unsigned Access : 2;
2697d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  friend class CXXClassMemberWrapper;
2700b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl
27108e0bc16b3312c27e87d33be7dcf3d4fe5bdd2e2Douglas Gregor  /// \brief Whether this declaration was loaded from an AST file.
27208e0bc16b3312c27e87d33be7dcf3d4fe5bdd2e2Douglas Gregor  unsigned FromASTFile : 1;
273ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
274f143ffc4a9af79ac1d822fea6995af4bf45d17dcDouglas Gregor  /// \brief Whether this declaration is hidden from normal name lookup, e.g.,
275f143ffc4a9af79ac1d822fea6995af4bf45d17dcDouglas Gregor  /// because it is was loaded from an AST file is either module-private or
276f143ffc4a9af79ac1d822fea6995af4bf45d17dcDouglas Gregor  /// because its submodule has not been made visible.
277f143ffc4a9af79ac1d822fea6995af4bf45d17dcDouglas Gregor  unsigned Hidden : 1;
278f143ffc4a9af79ac1d822fea6995af4bf45d17dcDouglas Gregor
279769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
280381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  unsigned IdentifierNamespace : 12;
2811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
282a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola  /// \brief If 0, we have not computed the linkage of this declaration.
283a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola  /// Otherwise, it is the linkage + 1.
284a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola  mutable unsigned CacheValidAndLinkage : 3;
285ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2868d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor  friend class ASTDeclWriter;
2878d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor  friend class ASTDeclReader;
288ecc2c090e7146c029dd9ee9a5a2fd66b275c01c0Douglas Gregor  friend class ASTReader;
289dc0705645276258afddb06cdf0fd7c8840251ee0Rafael Espindola  friend class LinkageComputer;
2908d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor
29122050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith  template<typename decl_type> friend class Redeclarable;
29222050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith
293651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \brief Allocate memory for a deserialized declaration.
294651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///
295651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// This routine must be used to allocate memory for any declaration that is
296651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// deserialized from a module file.
297651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///
298651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \param Size The size of the allocated object.
299651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \param Ctx The context in which we will allocate memory.
300651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \param ID The global ID of the deserialized declaration.
301651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \param Extra The amount of extra space to allocate after the object.
302651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void *operator new(std::size_t Size, const ASTContext &Ctx, unsigned ID,
303651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                     std::size_t Extra = 0);
304651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
305651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \brief Allocate memory for a non-deserialized declaration.
306651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void *operator new(std::size_t Size, const ASTContext &Ctx,
307651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                     DeclContext *Parent, std::size_t Extra = 0);
308651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3099cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregorprivate:
310651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool AccessDeclContextSanity() const;
3111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3124afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorprotected:
313d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis
3141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl(Kind DK, DeclContext *DC, SourceLocation L)
31546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    : NextInContextAndBits(), DeclCtx(DC),
31639d7650656fdd1be879ed7d0c722cfeae95e56d5Daniel Dunbar      Loc(L), DeclKind(DK), InvalidDecl(0),
3176b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis      HasAttrs(false), Implicit(false), Used(false), Referenced(false),
31846cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      Access(AS_none), FromASTFile(0), Hidden(0),
319381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
320a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola      CacheValidAndLinkage(0)
321381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  {
32202892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar    if (StatisticsEnabled) add(DK);
3230ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
3241bb19638f2ec0d63ed131b51ca8d9542d1a9afeeSam Bishop
3256764334dfa73d67cbbb1b1fc8fe00440aad00f2aArgyrios Kyrtzidis  Decl(Kind DK, EmptyShell Empty)
32646cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0),
3276b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis      HasAttrs(false), Implicit(false), Used(false), Referenced(false),
32846cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      Access(AS_none), FromASTFile(0), Hidden(0),
329381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
330a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola      CacheValidAndLinkage(0)
331381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  {
33202892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar    if (StatisticsEnabled) add(DK);
3336764334dfa73d67cbbb1b1fc8fe00440aad00f2aArgyrios Kyrtzidis  }
3346764334dfa73d67cbbb1b1fc8fe00440aad00f2aArgyrios Kyrtzidis
335da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  virtual ~Decl();
336da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
3376bd992946bda92193fadce7e4890d4465d2702f4Douglas Gregor  /// \brief Update a potentially out-of-date declaration.
3386bd992946bda92193fadce7e4890d4465d2702f4Douglas Gregor  void updateOutOfDate(IdentifierInfo &II) const;
3396bd992946bda92193fadce7e4890d4465d2702f4Douglas Gregor
340a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola  Linkage getCachedLinkage() const {
341a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola    return Linkage(CacheValidAndLinkage - 1);
342a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola  }
343a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola
344a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola  void setCachedLinkage(Linkage L) const {
345a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola    CacheValidAndLinkage = L + 1;
346a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola  }
347a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola
348a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola  bool hasCachedLinkage() const {
349a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola    return CacheValidAndLinkage;
350a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola  }
351a99ecbcc4c431d52df0b01539035ab5281d54656Rafael Espindola
3521bb19638f2ec0d63ed131b51ca8d9542d1a9afeeSam Bishoppublic:
35355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
35455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  /// \brief Source range that this declaration covers.
355aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar  virtual SourceRange getSourceRange() const LLVM_READONLY {
356da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    return SourceRange(getLocation(), getLocation());
357da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  }
358aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar  SourceLocation getLocStart() const LLVM_READONLY {
359aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar    return getSourceRange().getBegin();
360aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar  }
361aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar  SourceLocation getLocEnd() const LLVM_READONLY {
362aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar    return getSourceRange().getEnd();
363aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar  }
36455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
3650ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  SourceLocation getLocation() const { return Loc; }
3660ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  void setLocation(SourceLocation L) { Loc = L; }
3670ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
36835043e56150dc4eda882acd5ddfe0f8e3c0a8cb1John McCall  Kind getKind() const { return static_cast<Kind>(DeclKind); }
3690ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  const char *getDeclKindName() const;
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37146cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
37246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
37396f4468b6b1bf69bb01c1207f716a3ffaeb4a8d3Chris Lattner
3744afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  DeclContext *getDeclContext() {
37510d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner    if (isInSemaDC())
37610d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner      return getSemanticDC();
37710d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner    return getMultipleDC()->SemanticDC;
3784afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
3790cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  const DeclContext *getDeclContext() const {
3800cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    return const_cast<Decl*>(this)->getDeclContext();
3810cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  }
3823708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
383f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  /// Find the innermost non-closure ancestor of this declaration,
384f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  /// walking up through blocks, lambdas, etc.  If that ancestor is
385f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  /// not a code context (!isFunctionOrMethod()), returns null.
386f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  ///
387f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  /// A declaration may be its own non-closure context.
388f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  Decl *getNonClosureContext();
389f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  const Decl *getNonClosureContext() const {
390aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall    return const_cast<Decl*>(this)->getNonClosureContext();
391aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  }
392aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall
3933708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  TranslationUnitDecl *getTranslationUnitDecl();
3943708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  const TranslationUnitDecl *getTranslationUnitDecl() const {
3953708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis    return const_cast<Decl*>(this)->getTranslationUnitDecl();
3963708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis  }
3973708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis
3989aeed32282fe8a775c24c01c923717ca86695685John McCall  bool isInAnonymousNamespace() const;
3999aeed32282fe8a775c24c01c923717ca86695685John McCall
4006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool isInStdNamespace() const;
4016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
402aa49a7d70e58dac2aeb40664ba16d2ea571b8c95Daniel Dunbar  ASTContext &getASTContext() const LLVM_READONLY;
4031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4041329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson  void setAccess(AccessSpecifier AS) {
4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Access = AS;
406651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    assert(AccessDeclContextSanity());
4071329c274628cc8c4e8ad472b41d1a78c8123f611Anders Carlsson  }
4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  AccessSpecifier getAccess() const {
410651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    assert(AccessDeclContextSanity());
4111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return AccessSpecifier(Access);
412b8547e800f9553a72c2f3635ae2371692bd4411aAnders Carlsson  }
4134afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
414109f5fc8dff6f3bd707e0e4140dc99ef7d4f3e88Douglas Gregor  /// \brief Retrieve the access specifier for this declaration, even though
415109f5fc8dff6f3bd707e0e4140dc99ef7d4f3e88Douglas Gregor  /// it may not yet have been properly set.
416109f5fc8dff6f3bd707e0e4140dc99ef7d4f3e88Douglas Gregor  AccessSpecifier getAccessUnsafe() const {
417109f5fc8dff6f3bd707e0e4140dc99ef7d4f3e88Douglas Gregor    return AccessSpecifier(Access);
418109f5fc8dff6f3bd707e0e4140dc99ef7d4f3e88Douglas Gregor  }
419109f5fc8dff6f3bd707e0e4140dc99ef7d4f3e88Douglas Gregor
42076a642ff4fce7a0648c79a1f01324a8c3880e251Chris Lattner  bool hasAttrs() const { return HasAttrs; }
4214bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  void setAttrs(const AttrVec& Attrs) {
4224bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis    return setAttrsImpl(Attrs, getASTContext());
4234bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  }
424ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  AttrVec &getAttrs() {
425cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
42681abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattner  }
427cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  const AttrVec &getAttrs() const;
428cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  void dropAttrs();
4290ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
430b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  void addAttr(Attr *A) {
431b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor    if (hasAttrs())
432b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor      getAttrs().push_back(A);
433b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor    else
434b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor      setAttrs(AttrVec(1, A));
435b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  }
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
437cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  typedef AttrVec::const_iterator attr_iterator;
438651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<attr_iterator> attr_range;
439651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
440651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  attr_range attrs() const {
441651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return attr_range(attr_begin(), attr_end());
442651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
443cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
444cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  attr_iterator attr_begin() const {
4456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return hasAttrs() ? getAttrs().begin() : nullptr;
446cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  }
447cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  attr_iterator attr_end() const {
4486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return hasAttrs() ? getAttrs().end() : nullptr;
449cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  }
450ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
451c3ca14d13741889a9b924d0ca32e818f07a9834aFariborz Jahanian  template <typename T>
4528cf0f52037e0c4bf656953ae63a07e8156aeb5fbFariborz Jahanian  void dropAttr() {
4538cf0f52037e0c4bf656953ae63a07e8156aeb5fbFariborz Jahanian    if (!HasAttrs) return;
454ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
45510be5bad483cfe66bb5eb5ff948c03c4628ce774Benjamin Kramer    AttrVec &Vec = getAttrs();
45610be5bad483cfe66bb5eb5ff948c03c4628ce774Benjamin Kramer    Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end());
45710be5bad483cfe66bb5eb5ff948c03c4628ce774Benjamin Kramer
45810be5bad483cfe66bb5eb5ff948c03c4628ce774Benjamin Kramer    if (Vec.empty())
4598cf0f52037e0c4bf656953ae63a07e8156aeb5fbFariborz Jahanian      HasAttrs = false;
4608cf0f52037e0c4bf656953ae63a07e8156aeb5fbFariborz Jahanian  }
461ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
462cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  template <typename T>
463651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const {
464651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return llvm::iterator_range<specific_attr_iterator<T>>(
465651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        specific_attr_begin<T>(), specific_attr_end<T>());
466651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
467651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
468651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  template <typename T>
469cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  specific_attr_iterator<T> specific_attr_begin() const {
470cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    return specific_attr_iterator<T>(attr_begin());
471cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  }
472cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  template <typename T>
473cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  specific_attr_iterator<T> specific_attr_end() const {
474cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    return specific_attr_iterator<T>(attr_end());
475cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  }
476cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
477cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  template<typename T> T *getAttr() const {
4786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : nullptr;
479cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  }
48040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  template<typename T> bool hasAttr() const {
481cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    return hasAttrs() && hasSpecificAttr<T>(getAttrs());
482cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  }
483cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
484cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  /// getMaxAlignment - return the maximum alignment specified by attributes
485cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  /// on this decl, 0 if there are none.
4862fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer  unsigned getMaxAlignment() const;
4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4880ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// setInvalidDecl - Indicates the Decl had a semantic error. This
4890ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// allows for graceful error recovery.
49042738573253da1bd61f9c44f8d77f600d3b0cd1cDouglas Gregor  void setInvalidDecl(bool Invalid = true);
4910ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  bool isInvalidDecl() const { return (bool) InvalidDecl; }
4926b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor
4936b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// isImplicit - Indicates whether the declaration was implicitly
4946b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// generated by the implementation. If false, this declaration
4956b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  /// was written explicitly in the source code.
4966b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  bool isImplicit() const { return Implicit; }
4976b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor  void setImplicit(bool I = true) { Implicit = I; }
4981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
499e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  /// \brief Whether this declaration was used, meaning that a definition
500e0762c92110dfdcdd207db461a4ea17afd168f1eDouglas Gregor  /// is required.
501c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  ///
502c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  /// \param CheckUsedAttr When true, also consider the "used" attribute
503c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  /// (in addition to the "used" bit set by \c setUsed()) when determining
504c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  /// whether the function is used.
505c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  bool isUsed(bool CheckUsedAttr = true) const;
506cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
50786164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  /// \brief Set whether the declaration is used, in the sense of odr-use.
50886164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  ///
50986164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  /// This should only be used immediately after creating a declaration.
510e7bd89af8aa96a779c0031baf1a21e960a51d0f0Rafael Espindola  void setIsUsed() { Used = true; }
51186164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman
51286164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  /// \brief Mark the declaration used, in the sense of odr-use.
51386164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  ///
51486164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  /// This notifies any mutation listeners in addition to setting a bit
51586164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  /// indicating the declaration is used.
51686164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman  void markUsed(ASTContext &C);
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5186b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  /// \brief Whether this declaration was referenced.
5196b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  bool isReferenced() const;
5206b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
5216b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  void setReferenced(bool R = true) { Referenced = R; }
5226b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis
523c14a03dffff69b5e1c55cc118fc52d8fd9f3a28dArgyrios Kyrtzidis  /// \brief Whether this declaration is a top-level declaration (function,
524c14a03dffff69b5e1c55cc118fc52d8fd9f3a28dArgyrios Kyrtzidis  /// global variable, etc.) that is lexically inside an objc container
525c14a03dffff69b5e1c55cc118fc52d8fd9f3a28dArgyrios Kyrtzidis  /// definition.
526c14a03dffff69b5e1c55cc118fc52d8fd9f3a28dArgyrios Kyrtzidis  bool isTopLevelDeclInObjCContainer() const {
52746cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    return NextInContextAndBits.getInt() & TopLevelDeclInObjCContainerFlag;
528c14a03dffff69b5e1c55cc118fc52d8fd9f3a28dArgyrios Kyrtzidis  }
529c14a03dffff69b5e1c55cc118fc52d8fd9f3a28dArgyrios Kyrtzidis
530c14a03dffff69b5e1c55cc118fc52d8fd9f3a28dArgyrios Kyrtzidis  void setTopLevelDeclInObjCContainer(bool V = true) {
53146cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    unsigned Bits = NextInContextAndBits.getInt();
53246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    if (V)
53346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      Bits |= TopLevelDeclInObjCContainerFlag;
53446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    else
53546cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      Bits &= ~TopLevelDeclInObjCContainerFlag;
53646cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    NextInContextAndBits.setInt(Bits);
53746cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  }
53846cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor
53946cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  /// \brief Whether this declaration was marked as being private to the
54046cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  /// module in which it was defined.
541b775100fea6d8955149897dae1adca50ca471d17Richard Smith  bool isModulePrivate() const {
54246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    return NextInContextAndBits.getInt() & ModulePrivateFlag;
54346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  }
544b775100fea6d8955149897dae1adca50ca471d17Richard Smith
545b775100fea6d8955149897dae1adca50ca471d17Richard Smithprotected:
54646cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  /// \brief Specify whether this declaration was marked as being private
54746cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  /// to the module in which it was defined.
54846cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor  void setModulePrivate(bool MP = true) {
54946cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    unsigned Bits = NextInContextAndBits.getInt();
55046cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    if (MP)
55146cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      Bits |= ModulePrivateFlag;
55246cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    else
55346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor      Bits &= ~ModulePrivateFlag;
55446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    NextInContextAndBits.setInt(Bits);
555c14a03dffff69b5e1c55cc118fc52d8fd9f3a28dArgyrios Kyrtzidis  }
556c14a03dffff69b5e1c55cc118fc52d8fd9f3a28dArgyrios Kyrtzidis
557c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  /// \brief Set the owning module ID.
558c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  void setOwningModuleID(unsigned ID) {
559c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor    assert(isFromASTFile() && "Only works on a deserialized declaration");
560c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor    *((unsigned*)this - 2) = ID;
561c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  }
562c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor
56346cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregorpublic:
56446cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor
5650a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \brief Determine the availability of the given declaration.
5660a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  ///
5670a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// This routine will determine the most restrictive availability of
5680a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// the given declaration (e.g., preferring 'unavailable' to
5690a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// 'deprecated').
5700a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  ///
5710a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \param Message If non-NULL and the result is not \c
5720a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// AR_Available, will be set to a (possibly empty) message
5730a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// describing why the declaration has not been introduced, is
5740a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// deprecated, or is unavailable.
5756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  AvailabilityResult getAvailability(std::string *Message = nullptr) const;
5760a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
5770a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \brief Determine whether this declaration is marked 'deprecated'.
5780a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  ///
5790a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \param Message If non-NULL and the declaration is deprecated,
5800a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// this will be set to the message describing why the declaration
5810a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// was deprecated (which may be empty).
5826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool isDeprecated(std::string *Message = nullptr) const {
5830a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return getAvailability(Message) == AR_Deprecated;
5840a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
5850a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
5860a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \brief Determine whether this declaration is marked 'unavailable'.
5870a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  ///
5880a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \param Message If non-NULL and the declaration is unavailable,
5890a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// this will be set to the message describing why the declaration
5900a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// was made unavailable (which may be empty).
5916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool isUnavailable(std::string *Message = nullptr) const {
5920a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    return getAvailability(Message) == AR_Unavailable;
5930a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
5940a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
5950a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \brief Determine whether this is a weak-imported symbol.
5960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  ///
5970a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// Weak-imported symbols are typically marked with the
5987f3ad231bed2184694e407772689e8903068bdcaEli Friedman  /// 'weak_import' attribute, but may also be marked with an
5990a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// 'availability' attribute where we're targing a platform prior to
6000a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// the introduction of this feature.
6010a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  bool isWeakImported() const;
6020a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
6030a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \brief Determines whether this symbol can be weak-imported,
6040a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// e.g., whether it would be well-formed to add the weak_import
6050a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// attribute.
6060a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  ///
6070a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// \param IsDefinition Set to \c true to indicate that this
6080a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  /// declaration cannot be weak-imported because it has a definition.
6090a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  bool canBeWeakImported(bool &IsDefinition) const;
6100a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
611919814def90acecd25a35445862f0c0ff72a9b05Douglas Gregor  /// \brief Determine whether this declaration came from an AST file (such as
612919814def90acecd25a35445862f0c0ff72a9b05Douglas Gregor  /// a precompiled header or module) rather than having been parsed.
61308e0bc16b3312c27e87d33be7dcf3d4fe5bdd2e2Douglas Gregor  bool isFromASTFile() const { return FromASTFile; }
614ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
615b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  /// \brief Retrieve the global declaration ID associated with this
616b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  /// declaration, which specifies where in the
617b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  unsigned getGlobalID() const {
618b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor    if (isFromASTFile())
619b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor      return *((const unsigned*)this - 1);
620b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor    return 0;
621b6b60c1521a2e65f60e93c5fd56c103cf027df63Douglas Gregor  }
622c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor
623c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  /// \brief Retrieve the global ID of the module that owns this particular
624c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  /// declaration.
625c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  unsigned getOwningModuleID() const {
626c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor    if (isFromASTFile())
627c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor      return *((const unsigned*)this - 2);
628c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor
629c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor    return 0;
630c6c8e0ec96bb64f1b9f543d7c8317c6090f80a30Douglas Gregor  }
631ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
632ca2ab45341c448284cf93770018c717810575f86Douglas Gregorprivate:
633ca2ab45341c448284cf93770018c717810575f86Douglas Gregor  Module *getOwningModuleSlow() const;
634ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
635ca2ab45341c448284cf93770018c717810575f86Douglas Gregorpublic:
636ca2ab45341c448284cf93770018c717810575f86Douglas Gregor  Module *getOwningModule() const {
637ca2ab45341c448284cf93770018c717810575f86Douglas Gregor    if (!isFromASTFile())
6386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
639ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
640ca2ab45341c448284cf93770018c717810575f86Douglas Gregor    return getOwningModuleSlow();
641ca2ab45341c448284cf93770018c717810575f86Douglas Gregor  }
642ca2ab45341c448284cf93770018c717810575f86Douglas Gregor
643cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  unsigned getIdentifierNamespace() const {
644769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner    return IdentifierNamespace;
6450ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
646d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner  bool isInIdentifierNamespace(unsigned NS) const {
647d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner    return getIdentifierNamespace() & NS;
648d62fdc448b3ace606c8c9c9f13e43218fa204dbfChris Lattner  }
649769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner  static unsigned getIdentifierNamespaceForKind(Kind DK);
650769dbdf467681f6020ff248b969c2d41a4fdccd3Chris Lattner
6510d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  bool hasTagIdentifierNamespace() const {
6520d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    return isTagIdentifierNamespace(getIdentifierNamespace());
6530d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  }
6540d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  static bool isTagIdentifierNamespace(unsigned NS) {
6550d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    // TagDecls have Tag and Type set and may also have TagFriend.
6560d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
6570d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall  }
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6594afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// getLexicalDeclContext - The declaration context where this Decl was
6604afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// lexically declared (LexicalDC). May be different from
6614afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// getDeclContext() (SemanticDC).
6624afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  /// e.g.:
6634afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///
6644afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   namespace A {
6654afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
6664afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   }
6674afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///   void A::f(); // SemanticDC == namespace 'A'
6684afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  ///                // LexicalDC == global namespace
66910d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner  DeclContext *getLexicalDeclContext() {
6704afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    if (isInSemaDC())
67110d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner      return getSemanticDC();
6724afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    return getMultipleDC()->LexicalDC;
6734afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
67410d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner  const DeclContext *getLexicalDeclContext() const {
67510d8379676f32b493740a5509e2cd7f0d6433139Chris Lattner    return const_cast<Decl*>(this)->getLexicalDeclContext();
6764afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  }
677f5cecfbdcd20be224861f9e67c5973a9a2b61512Argyrios Kyrtzidis
678da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  virtual bool isOutOfLine() const {
679da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    return getLexicalDeclContext() != getDeclContext();
680da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  }
6811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6826ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  /// setDeclContext - Set both the semantic and lexical DeclContext
6836ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  /// to DC.
6846ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  void setDeclContext(DeclContext *DC);
6856ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor
6864afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  void setLexicalDeclContext(DeclContext *DC);
6874afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
6887a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
6897a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// scoped decl is defined outside the current function or method.  This is
6907a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// roughly global variables and functions, but also handles enums (which
6917a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// could be defined inside or outside a function etc).
692c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  bool isDefinedOutsideFunctionOrMethod() const {
6936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return getParentFunctionOrMethod() == nullptr;
694c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  }
695c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis
696c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  /// \brief If this decl is defined inside a function/method/block it returns
697c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  /// the corresponding DeclContext, otherwise it returns null.
698c8680f46970a5a53d07e05edd93657e64764da3cArgyrios Kyrtzidis  const DeclContext *getParentFunctionOrMethod() const;
699fcc1e50ebcb441c6c610ccd539f57c57549bd66cArgyrios Kyrtzidis  DeclContext *getParentFunctionOrMethod() {
700fcc1e50ebcb441c6c610ccd539f57c57549bd66cArgyrios Kyrtzidis    return const_cast<DeclContext*>(
701fcc1e50ebcb441c6c610ccd539f57c57549bd66cArgyrios Kyrtzidis                    const_cast<const Decl*>(this)->getParentFunctionOrMethod());
702fcc1e50ebcb441c6c610ccd539f57c57549bd66cArgyrios Kyrtzidis  }
7034afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
704b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis  /// \brief Retrieves the "canonical" declaration of the given declaration.
705da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  virtual Decl *getCanonicalDecl() { return this; }
706b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis  const Decl *getCanonicalDecl() const {
707b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis    return const_cast<Decl*>(this)->getCanonicalDecl();
708b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis  }
709fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
710ac4e3794c60fc194fb2bd9764aa81d4cae530eccArgyrios Kyrtzidis  /// \brief Whether this particular Decl is a canonical one.
711b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis  bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
712c896ad064fd6ee73a7e0536d94df77b2cc7313b6Douglas Gregor
7131e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidisprotected:
7141e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  /// \brief Returns the next redeclaration or itself if this is the only decl.
7151e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  ///
7161e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  /// Decl subclasses that can be redeclared should override this method so that
7171e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  /// Decl::redecl_iterator can iterate over them.
7186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  virtual Decl *getNextRedeclarationImpl() { return this; }
7191e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis
720ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  /// \brief Implementation of getPreviousDecl(), to be overridden by any
721ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  /// subclass that has a redeclaration chain.
7226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  virtual Decl *getPreviousDeclImpl() { return nullptr; }
7236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
724ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  /// \brief Implementation of getMostRecentDecl(), to be overridden by any
7256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// subclass that has a redeclaration chain.
726ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  virtual Decl *getMostRecentDeclImpl() { return this; }
7276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
7281e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidispublic:
7291e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  /// \brief Iterates through all the redeclarations of the same decl.
7301e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  class redecl_iterator {
7311e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    /// Current - The current declaration.
7321e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    Decl *Current;
7331e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    Decl *Starter;
7341e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis
7351e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  public:
736581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef Decl *value_type;
737581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef const value_type &reference;
738581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef const value_type *pointer;
7391e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    typedef std::forward_iterator_tag iterator_category;
740581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef std::ptrdiff_t difference_type;
7411e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis
7426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    redecl_iterator() : Current(nullptr) { }
7431e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
7441e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis
745581deb3da481053c4993c7600f97acf7768caac5David Blaikie    reference operator*() const { return Current; }
746581deb3da481053c4993c7600f97acf7768caac5David Blaikie    value_type operator->() const { return Current; }
7471e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis
7481e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    redecl_iterator& operator++() {
7491e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis      assert(Current && "Advancing while iterator has reached end");
7501e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis      // Get either previous decl or latest decl.
7516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Decl *Next = Current->getNextRedeclarationImpl();
75222cbd2b794676c3b29c2b401c26730ed7047809eArgyrios Kyrtzidis      assert(Next && "Should return next redeclaration or itself, never null!");
7536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Current = (Next != Starter) ? Next : nullptr;
7541e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis      return *this;
7551e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    }
7561e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis
7571e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    redecl_iterator operator++(int) {
7581e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis      redecl_iterator tmp(*this);
7591e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis      ++(*this);
7601e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis      return tmp;
7611e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    }
7621e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    friend bool operator==(redecl_iterator x, redecl_iterator y) {
7641e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis      return x.Current == y.Current;
7651e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    }
7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    friend bool operator!=(redecl_iterator x, redecl_iterator y) {
7671e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis      return x.Current != y.Current;
7681e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis    }
7691e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  };
7701e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis
771651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<redecl_iterator> redecl_range;
772651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
773651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \brief Returns an iterator range for all the redeclarations of the same
774651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// decl. It will iterate at least once (when this decl is the only one).
775651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  redecl_range redecls() const {
776651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return redecl_range(redecls_begin(), redecls_end());
777651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
778651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
7791e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  redecl_iterator redecls_begin() const {
780651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return redecl_iterator(const_cast<Decl *>(this));
7811e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  }
7821e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis  redecl_iterator redecls_end() const { return redecl_iterator(); }
783fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
784f785a7d611404cf4747287a2bbc59b4d0e6a5a8cDouglas Gregor  /// \brief Retrieve the previous declaration that declares the same entity
785f785a7d611404cf4747287a2bbc59b4d0e6a5a8cDouglas Gregor  /// as this declaration, or NULL if there is no previous declaration.
786ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
787f785a7d611404cf4747287a2bbc59b4d0e6a5a8cDouglas Gregor
788f785a7d611404cf4747287a2bbc59b4d0e6a5a8cDouglas Gregor  /// \brief Retrieve the most recent declaration that declares the same entity
789ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  /// as this declaration, or NULL if there is no previous declaration.
790ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  const Decl *getPreviousDecl() const {
791ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    return const_cast<Decl *>(this)->getPreviousDeclImpl();
792ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  }
7937693b32af6863c63fcaf4de087760740ee675f71Rafael Espindola
794e6ddd7f610cf6caf3b5691b0f3d60e00e8c95741Rafael Espindola  /// \brief True if this is the first declaration in its redeclaration chain.
7957693b32af6863c63fcaf4de087760740ee675f71Rafael Espindola  bool isFirstDecl() const {
7966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return getPreviousDecl() == nullptr;
7977693b32af6863c63fcaf4de087760740ee675f71Rafael Espindola  }
7987693b32af6863c63fcaf4de087760740ee675f71Rafael Espindola
799ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  /// \brief Retrieve the most recent declaration that declares the same entity
800ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  /// as this declaration (which may be this declaration).
801ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
802ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor
803ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  /// \brief Retrieve the most recent declaration that declares the same entity
804ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  /// as this declaration (which may be this declaration).
805ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  const Decl *getMostRecentDecl() const {
806ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    return const_cast<Decl *>(this)->getMostRecentDeclImpl();
807ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  }
808ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor
809d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  /// getBody - If this Decl represents a declaration for a body of code,
810d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  ///  such as a function or method definition, this method returns the
811d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  ///  top-level Stmt* of that body.  Otherwise this method returns null.
8126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  virtual Stmt* getBody() const { return nullptr; }
813d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
814a438e68ad7da48e7e4903785e5e96732abcdadfbManuel Klimek  /// \brief Returns true if this \c Decl represents a declaration for a body of
81506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  /// code, such as a function or method definition.
816a438e68ad7da48e7e4903785e5e96732abcdadfbManuel Klimek  /// Note that \c hasBody can also return true if any redeclaration of this
817a438e68ad7da48e7e4903785e5e96732abcdadfbManuel Klimek  /// \c Decl represents a declaration for a body of code.
8186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  virtual bool hasBody() const { return getBody() != nullptr; }
81906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
820d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  /// getBodyRBrace - Gets the right brace of the body, if a body exists.
821d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl  /// This works whether the body is a CompoundStmt or a CXXTryStmt.
8226fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  SourceLocation getBodyRBrace() const;
823d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl
8240ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  // global temp stats (until we have a per-module visitor)
8259a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt  static void add(Kind k);
82602892a65b18875a04c7ed5eadb3a13be801ab477Daniel Dunbar  static void EnableStatistics();
8270ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  static void PrintStats();
8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82967e332009c6e349dc34700f539747afcff990336Anders Carlsson  /// isTemplateParameter - Determines whether this declaration is a
830f57172b24f08a68d179675989813d5479dc87829Douglas Gregor  /// template parameter.
831f57172b24f08a68d179675989813d5479dc87829Douglas Gregor  bool isTemplateParameter() const;
8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83367e332009c6e349dc34700f539747afcff990336Anders Carlsson  /// isTemplateParameter - Determines whether this declaration is a
83467e332009c6e349dc34700f539747afcff990336Anders Carlsson  /// template parameter pack.
83567e332009c6e349dc34700f539747afcff990336Anders Carlsson  bool isTemplateParameterPack() const;
836f57172b24f08a68d179675989813d5479dc87829Douglas Gregor
8371fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  /// \brief Whether this declaration is a parameter pack.
8381fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  bool isParameterPack() const;
839ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
840ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski  /// \brief returns true if this declaration is a template
841ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski  bool isTemplateDecl() const;
842ed9d84a2112e2bd56befb5f4fa8fc5bdf71fafa3Caitlin Sadowski
843e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  /// \brief Whether this declaration is a function or function template.
844651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isFunctionOrFunctionTemplate() const {
845651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return (DeclKind >= Decl::firstFunction &&
846651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            DeclKind <= Decl::lastFunction) ||
847651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines           DeclKind == FunctionTemplate;
848651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
849651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
850651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \brief Returns the function itself, or the templated function if this is a
851651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// function template.
852651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  FunctionDecl *getAsFunction() LLVM_READONLY;
853651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
854651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const FunctionDecl *getAsFunction() const {
855651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return const_cast<Decl *>(this)->getAsFunction();
856651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
85702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
85802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  /// \brief Changes the namespace of this declaration to reflect that it's
859a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  /// a function-local extern declaration.
860a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  ///
861a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  /// These declarations appear in the lexical context of the extern
862a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  /// declaration, but in the semantic context of the enclosing namespace
863a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  /// scope.
864a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  void setLocalExternDecl() {
865a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    assert((IdentifierNamespace == IDNS_Ordinary ||
866a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith            IdentifierNamespace == IDNS_OrdinaryFriend) &&
867a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith           "namespace is not ordinary");
868a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
869a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    Decl *Prev = getPreviousDecl();
870a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    IdentifierNamespace &= ~IDNS_Ordinary;
871a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
872a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    IdentifierNamespace |= IDNS_LocalExtern;
873a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    if (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary)
874a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      IdentifierNamespace |= IDNS_Ordinary;
875a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  }
876a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
877a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  /// \brief Determine whether this is a block-scope declaration with linkage.
878a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  /// This will either be a local variable declaration declared 'extern', or a
879a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  /// local function declaration.
880a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  bool isLocalExternDecl() {
881a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    return IdentifierNamespace & IDNS_LocalExtern;
882a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  }
883a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
884a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  /// \brief Changes the namespace of this declaration to reflect that it's
88502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  /// the object of a friend declaration.
88602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  ///
88702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  /// These declarations appear in the lexical context of the friending
88802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  /// class, but in the semantic context of the actual entity.  This property
88902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  /// applies only to a specific decl object;  other redeclarations of the
89002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  /// same entity may not (and probably don't) share this property.
89122050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith  void setObjectOfFriendDecl(bool PerformFriendInjection = false) {
89202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    unsigned OldNS = IdentifierNamespace;
893b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
894a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                     IDNS_TagFriend | IDNS_OrdinaryFriend |
895a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                     IDNS_LocalExtern)) &&
896b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall           "namespace includes neither ordinary nor tag");
8970d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall    assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
898a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                       IDNS_TagFriend | IDNS_OrdinaryFriend |
899a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                       IDNS_LocalExtern)) &&
900b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall           "namespace includes other than ordinary or tag");
901b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
90222050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Decl *Prev = getPreviousDecl();
903a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    IdentifierNamespace &= ~(IDNS_Ordinary | IDNS_Tag | IDNS_Type);
904a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
905b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
90602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall      IdentifierNamespace |= IDNS_TagFriend;
907a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      if (PerformFriendInjection ||
90822050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith          (Prev && Prev->getIdentifierNamespace() & IDNS_Tag))
90922050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith        IdentifierNamespace |= IDNS_Tag | IDNS_Type;
910b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
911b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
912a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend | IDNS_LocalExtern)) {
91302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall      IdentifierNamespace |= IDNS_OrdinaryFriend;
91422050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith      if (PerformFriendInjection ||
91522050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith          (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary))
91622050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith        IdentifierNamespace |= IDNS_Ordinary;
917b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
91802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
91902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
92002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  enum FriendObjectKind {
9217841abf94eb8653b26aa8d5bf67f4ad0ff074767David Majnemer    FOK_None,      ///< Not a friend object.
9227841abf94eb8653b26aa8d5bf67f4ad0ff074767David Majnemer    FOK_Declared,  ///< A friend of a previously-declared entity.
9237841abf94eb8653b26aa8d5bf67f4ad0ff074767David Majnemer    FOK_Undeclared ///< A friend of a previously-undeclared entity.
92402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  };
92502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
92602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  /// \brief Determines whether this declaration is the object of a
92702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  /// friend declaration and, if so, what kind.
92802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  ///
92902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  /// There is currently no direct way to find the associated FriendDecl.
93002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendObjectKind getFriendObjectKind() const {
9312946cebdf49d3a6eeba8df93be291a07b2d58cbcDavid Majnemer    unsigned mask =
9322946cebdf49d3a6eeba8df93be291a07b2d58cbcDavid Majnemer        (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
93302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!mask) return FOK_None;
9342946cebdf49d3a6eeba8df93be291a07b2d58cbcDavid Majnemer    return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? FOK_Declared
9352946cebdf49d3a6eeba8df93be291a07b2d58cbcDavid Majnemer                                                             : FOK_Undeclared);
93602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
9371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  /// Specifies that this declaration is a C++ overloaded non-member.
93976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  void setNonMemberOperator() {
94076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    assert(getKind() == Function || getKind() == FunctionTemplate);
94176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    assert((IdentifierNamespace & IDNS_Ordinary) &&
94276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall           "visible non-member operators should be in ordinary namespace");
94376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    IdentifierNamespace |= IDNS_NonMemberOperator;
94476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  }
94576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
94680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) { return true; }
94742220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static DeclContext *castToDeclContext(const Decl *);
94842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static Decl *castFromDeclContext(const DeclContext *);
9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9505cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu  void print(raw_ostream &Out, unsigned Indentation = 0,
9515cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu             bool PrintInstantiation = false) const;
9528cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  void print(raw_ostream &Out, const PrintingPolicy &Policy,
9535cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu             unsigned Indentation = 0, bool PrintInstantiation = false) const;
95442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  static void printGroup(Decl** Begin, unsigned NumDecls,
9558cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner                         raw_ostream &Out, const PrintingPolicy &Policy,
95642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman                         unsigned Indentation = 0);
957559b928b05aeacc9c984a9d633ad586bbe858196Alexander Kornienko  // Debuggers don't usually respect default arguments.
958651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dump() const;
9597ba443aa8cfb222737d3a964a19ad4fb1328c4b5Richard Trieu  // Same as dump(), but forces color printing.
960651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpColor() const;
961559b928b05aeacc9c984a9d633ad586bbe858196Alexander Kornienko  void dump(raw_ostream &Out) const;
9624fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
963651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \brief Looks through the Decl's underlying type to extract a FunctionType
964651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// when possible. Will return null if the type underlying the Decl does not
965651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// have a FunctionType.
966651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  const FunctionType *getFunctionType(bool BlocksToo = true) const;
967651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
96881abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattnerprivate:
9694bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
9704bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis  void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
9714bbb8501d9db2ae72b1e39afaafa5795d67ffe03Argyrios Kyrtzidis                           ASTContext &Ctx);
97281abbdd848aa02c30242bd22dcc6ffe024ae2957Chris Lattner
9737b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidisprotected:
9747b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  ASTMutationListener *getASTMutationListener() const;
9750ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
9760ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
977eaa9511dd2bcb0c7f90f47a236a91c82048f484eDouglas Gregor/// \brief Determine whether two declarations declare the same entity.
978eaa9511dd2bcb0c7f90f47a236a91c82048f484eDouglas Gregorinline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
979eaa9511dd2bcb0c7f90f47a236a91c82048f484eDouglas Gregor  if (!D1 || !D2)
980eaa9511dd2bcb0c7f90f47a236a91c82048f484eDouglas Gregor    return false;
981eaa9511dd2bcb0c7f90f47a236a91c82048f484eDouglas Gregor
982dec1cc4ec73e96f315ab7dee51459700c0ad821dDouglas Gregor  if (D1 == D2)
983dec1cc4ec73e96f315ab7dee51459700c0ad821dDouglas Gregor    return true;
984dec1cc4ec73e96f315ab7dee51459700c0ad821dDouglas Gregor
985eaa9511dd2bcb0c7f90f47a236a91c82048f484eDouglas Gregor  return D1->getCanonicalDecl() == D2->getCanonicalDecl();
986eaa9511dd2bcb0c7f90f47a236a91c82048f484eDouglas Gregor}
987eaa9511dd2bcb0c7f90f47a236a91c82048f484eDouglas Gregor
98849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
98949f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner/// doing something to a specific decl.
99049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattnerclass PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
991c52365674b5b157a0486f75c12dd9f4cc41d8089Daniel Dunbar  const Decl *TheDecl;
99249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceLocation Loc;
99349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  SourceManager &SM;
99449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  const char *Message;
99549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattnerpublic:
996c52365674b5b157a0486f75c12dd9f4cc41d8089Daniel Dunbar  PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
99749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                       SourceManager &sm, const char *Msg)
99849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
9991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1000651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void print(raw_ostream &OS) const override;
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump};
10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1003ef8225444452a1486bd721f3285301fe84643b00Stephen Hinestypedef MutableArrayRef<NamedDecl *> DeclContextLookupResult;
1004ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall
1005cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenkotypedef ArrayRef<NamedDecl *> DeclContextLookupConstResult;
100649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner
1007b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner/// DeclContext - This is used only as base class of specific decl types that
10081ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis/// can act as declaration contexts. These decls are (only the top classes
10091ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis/// that directly derive from DeclContext are mentioned, not their subclasses):
10100ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///
1011ef177820100ab583b08fd3056e2a5a52ee4b1629Argyrios Kyrtzidis///   TranslationUnitDecl
10122d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis///   NamespaceDecl
10130ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner///   FunctionDecl
10141ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis///   TagDecl
1015d3bb44f0f1a83cb208d3e61ee80afe6a4d20d2d8Argyrios Kyrtzidis///   ObjCMethodDecl
10161ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis///   ObjCContainerDecl
1017074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor///   LinkageSpecDecl
101856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff///   BlockDecl
10191ad4dd78ec53c24ea9e752b216035d7aa666fe49Argyrios Kyrtzidis///
1020b048c9835969c4f7fe06264748be18ed4b442116Chris Lattnerclass DeclContext {
10210ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  /// DeclKind - This indicates which class this is.
102235043e56150dc4eda882acd5ddfe0f8e3c0a8cb1John McCall  unsigned DeclKind : 8;
10230ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
10242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief Whether this declaration context also has some external
10252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// storage that contains additional declarations that are lexically
10262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// part of this context.
1027bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  mutable bool ExternalLexicalStorage : 1;
10282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
10292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief Whether this declaration context also has some external
10302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// storage that contains additional declarations that are visible
10312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// in this context.
1032bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  mutable bool ExternalVisibleStorage : 1;
1033bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith
1034bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  /// \brief Whether this declaration context has had external visible
1035bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  /// storage added since the last lookup. In this case, \c LookupPtr's
1036bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  /// invariant may not hold and needs to be fixed before we perform
1037bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  /// another lookup.
1038bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  mutable bool NeedToReconcileExternalVisibleStorage : 1;
10392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1040c36c540c5bfce941f3d892919394d092491211f2Douglas Gregor  /// \brief Pointer to the data structure used to lookup declarations
10410c01d18094100db92d38daa923c95661512db203John McCall  /// within this context (or a DependentStoredDeclsMap if this is a
1042c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  /// dependent context), and a bool indicating whether we have lazily
1043c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  /// omitted any declarations from the map. We maintain the invariant
1044bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  /// that, if the map contains an entry for a DeclarationName (and we
1045bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  /// haven't lazily omitted anything), then it contains all relevant
1046bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  /// entries for that name.
1047c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  mutable llvm::PointerIntPair<StoredDeclsMap*, 1, bool> LookupPtr;
104844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1049eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisprotected:
10506037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// FirstDecl - The first declaration stored within this declaration
10516037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// context.
10522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  mutable Decl *FirstDecl;
10536037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
10546037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// LastDecl - The last declaration stored within this declaration
10556037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// context. FIXME: We could probably cache this value somewhere
10566037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// outside of the DeclContext, to reduce the size of DeclContext by
10576037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  /// another pointer.
10582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  mutable Decl *LastDecl;
10597643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
106076bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall  friend class ExternalASTSource;
1061e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  friend class ASTDeclReader;
1062643586fe4fcd42b0978efd3566832ab4dce50367Argyrios Kyrtzidis  friend class ASTWriter;
106376bd1f387e6a7b7abfe53f63b3bd429b97bb80f0John McCall
1064eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  /// \brief Build up a chain of declarations.
1065eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ///
1066eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  /// \returns the first/last pair of declarations.
1067eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  static std::pair<Decl *, Decl *>
1068341785ec52f87c0803ba52dc88faac4e136f8593Bill Wendling  BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
1069eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
1070bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith  DeclContext(Decl::Kind K)
1071bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith      : DeclKind(K), ExternalLexicalStorage(false),
1072bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith        ExternalVisibleStorage(false),
10736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        NeedToReconcileExternalVisibleStorage(false), LookupPtr(nullptr, false),
10746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        FirstDecl(nullptr), LastDecl(nullptr) {}
107544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10760ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnerpublic:
107744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ~DeclContext();
107844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10799b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis  Decl::Kind getDeclKind() const {
108035043e56150dc4eda882acd5ddfe0f8e3c0a8cb1John McCall    return static_cast<Decl::Kind>(DeclKind);
10819b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis  }
10820a4739305a984ef9b821cedad5f4fe235eb6ef7dSteve Naroff  const char *getDeclKindName() const;
10839b9ca01ff3136a7539dc08a4c4e1a3dd8c8d3a7dArgyrios Kyrtzidis
1084305ec42f971a2b3e114eb35256c23e955c497a1cArgyrios Kyrtzidis  /// getParent - Returns the containing DeclContext.
10850cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  DeclContext *getParent() {
1086305ec42f971a2b3e114eb35256c23e955c497a1cArgyrios Kyrtzidis    return cast<Decl>(this)->getDeclContext();
1087305ec42f971a2b3e114eb35256c23e955c497a1cArgyrios Kyrtzidis  }
10880cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  const DeclContext *getParent() const {
10890cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    return const_cast<DeclContext*>(this)->getParent();
1090d2595ecce5f8350e485c83bfe767549a522b2802Argyrios Kyrtzidis  }
10911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
109277407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  /// getLexicalParent - Returns the containing lexical DeclContext. May be
109344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// different from getParent, e.g.:
109444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///
109544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   namespace A {
109644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///      struct S;
109744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   }
109844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   struct A::S {}; // getParent() == namespace 'A'
109977407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  ///                   // getLexicalParent() == translation unit
110077407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  ///
110177407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  DeclContext *getLexicalParent() {
11020cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    return cast<Decl>(this)->getLexicalDeclContext();
110377407b802130b1c44b1f63b855722a5376f57bcaArgyrios Kyrtzidis  }
11040cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner  const DeclContext *getLexicalParent() const {
11050cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    return const_cast<DeclContext*>(this)->getLexicalParent();
11061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1107048f30a483ca352ee7f235a5be181b9dcc5f9d9cArgyrios Kyrtzidis
1108e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  DeclContext *getLookupParent();
1109ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1110e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  const DeclContext *getLookupParent() const {
1111e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor    return const_cast<DeclContext*>(this)->getLookupParent();
1112e942bbe02b6fb332d1f13d38c6e1980b416cf89aDouglas Gregor  }
1113ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1114048f30a483ca352ee7f235a5be181b9dcc5f9d9cArgyrios Kyrtzidis  ASTContext &getParentASTContext() const {
1115048f30a483ca352ee7f235a5be181b9dcc5f9d9cArgyrios Kyrtzidis    return cast<Decl>(this)->getASTContext();
1116048f30a483ca352ee7f235a5be181b9dcc5f9d9cArgyrios Kyrtzidis  }
1117048f30a483ca352ee7f235a5be181b9dcc5f9d9cArgyrios Kyrtzidis
1118aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  bool isClosure() const {
1119aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall    return DeclKind == Decl::Block;
1120aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall  }
1121aab9e315184d344bbd733f13b68915d02db7b32bJohn McCall
1122a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  bool isObjCContainer() const {
112380f775359357f90f34b6adb8104506f13a816a67Fariborz Jahanian    switch (DeclKind) {
112480f775359357f90f34b6adb8104506f13a816a67Fariborz Jahanian        case Decl::ObjCCategory:
112580f775359357f90f34b6adb8104506f13a816a67Fariborz Jahanian        case Decl::ObjCCategoryImpl:
112680f775359357f90f34b6adb8104506f13a816a67Fariborz Jahanian        case Decl::ObjCImplementation:
112780f775359357f90f34b6adb8104506f13a816a67Fariborz Jahanian        case Decl::ObjCInterface:
112880f775359357f90f34b6adb8104506f13a816a67Fariborz Jahanian        case Decl::ObjCProtocol:
112980f775359357f90f34b6adb8104506f13a816a67Fariborz Jahanian            return true;
113080f775359357f90f34b6adb8104506f13a816a67Fariborz Jahanian    }
113180f775359357f90f34b6adb8104506f13a816a67Fariborz Jahanian    return false;
1132a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  }
1133a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
11340ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  bool isFunctionOrMethod() const {
11350ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    switch (DeclKind) {
11360cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    case Decl::Block:
11376afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    case Decl::Captured:
11380cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    case Decl::ObjCMethod:
11390cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner      return true;
11400cf2b1990c82121d03a004dafe498ba43bf4b42aChris Lattner    default:
11419a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt      return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
11420ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner    }
11430ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
11440ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1145ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  bool isFileContext() const {
1146ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis    return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
1147ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis  }
1148ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis
11495f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  bool isTranslationUnit() const {
11505f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor    return DeclKind == Decl::TranslationUnit;
11515f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  }
11525f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
1153bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  bool isRecord() const {
11549a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
1155c7ed9c60b8ee04b119e23441cae2cfec74536ba9Argyrios Kyrtzidis  }
1156c7ed9c60b8ee04b119e23441cae2cfec74536ba9Argyrios Kyrtzidis
115744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  bool isNamespace() const {
115844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return DeclKind == Decl::Namespace;
115944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
116044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
11616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool isStdNamespace() const;
11626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1163410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  bool isInlineNamespace() const;
1164410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl
1165bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  /// \brief Determines whether this context is dependent on a
1166bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  /// template parameter.
1167bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor  bool isDependentContext() const;
1168bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
1169074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// isTransparentContext - Determines whether this context is a
1170074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// "transparent" context, meaning that the members declared in this
1171074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// context are semantically declared in the nearest enclosing
1172074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// non-transparent (opaque) context but are lexically declared in
1173074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// this context. For example, consider the enumerators of an
11741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// enumeration type:
1175074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// @code
1176074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// enum E {
11771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///   Val1
1178074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// };
1179074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// @endcode
1180074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// Here, E is a transparent context, so its enumerator (Val1) will
1181074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// appear (semantically) that it is in the same context of E.
1182074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  /// Examples of transparent contexts include: enumerations (except for
1183410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  /// C++0x scoped enums), and C++ linkage specifications.
1184074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor  bool isTransparentContext() const;
1185074149e11baf5f7db12f84efd5c34ba6e35d5cdfDouglas Gregor
1186142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  /// \brief Determines whether this context or some of its ancestors is a
1187142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  /// linkage specification context that specifies C linkage.
1188142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  bool isExternCContext() const;
1189142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov
1190142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  /// \brief Determines whether this context or some of its ancestors is a
1191142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  /// linkage specification context that specifies C++ linkage.
1192142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov  bool isExternCXXContext() const;
1193142ab06ffb2ec286917554aa5d945323a1ebf359Serge Pavlov
119461481dad22ebca3fba3fe4fb67a3b926d0895e9cDouglas Gregor  /// \brief Determine whether this declaration context is equivalent
119561481dad22ebca3fba3fe4fb67a3b926d0895e9cDouglas Gregor  /// to the declaration context DC.
11967a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  bool Equals(const DeclContext *DC) const {
1197dbdf5e7d0b6f1f8d8c496c1a0ada6f706cddf100Douglas Gregor    return DC && this->getPrimaryContext() == DC->getPrimaryContext();
119861481dad22ebca3fba3fe4fb67a3b926d0895e9cDouglas Gregor  }
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12006dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  /// \brief Determine whether this declaration context encloses the
12016dd38daf1495367db8fe9e9a5cacb7420cf08e27Douglas Gregor  /// declaration context DC.
12027a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  bool Encloses(const DeclContext *DC) const;
1203ef6e647b8d3268a765c2c4dd7f8a73cad281a8e6Argyrios Kyrtzidis
12044b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// \brief Find the nearest non-closure ancestor of this context,
12054b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// i.e. the innermost semantic parent of this context which is not
12064b9c2d235fb9449e249d74f48ecfec601650de93John McCall  /// a closure.  A context may be its own non-closure ancestor.
1207f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  Decl *getNonClosureAncestor();
1208f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall  const Decl *getNonClosureAncestor() const {
12094b9c2d235fb9449e249d74f48ecfec601650de93John McCall    return const_cast<DeclContext*>(this)->getNonClosureAncestor();
12104b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
12114b9c2d235fb9449e249d74f48ecfec601650de93John McCall
121244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// getPrimaryContext - There may be many different
121344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// declarations of the same entity (including forward declarations
121444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// of classes, multiple definitions of namespaces, etc.), each with
121544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// a different set of declarations. This routine returns the
121644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// "primary" DeclContext structure, which will contain the
121744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// information needed to perform name lookup into this context.
12180701bbb228dfd87e1fe82a0a4b7b9facfecb43daSteve Naroff  DeclContext *getPrimaryContext();
12190c01d18094100db92d38daa923c95661512db203John McCall  const DeclContext *getPrimaryContext() const {
12200c01d18094100db92d38daa923c95661512db203John McCall    return const_cast<DeclContext*>(this)->getPrimaryContext();
12210c01d18094100db92d38daa923c95661512db203John McCall  }
122244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
12237a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// getRedeclContext - Retrieve the context in which an entity conflicts with
12247a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// other entities of the same name, or where it is a redeclaration if the
1225410c4f2eb5e6a8c2318cacb9a6751f1b4fcff166Sebastian Redl  /// two entities are compatible. This skips through transparent contexts.
12267a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  DeclContext *getRedeclContext();
12277a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  const DeclContext *getRedeclContext() const {
12287a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    return const_cast<DeclContext *>(this)->getRedeclContext();
122917a9b9e3ee75f5dbb4819cc8ebf40eec8015f84aDouglas Gregor  }
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
123188b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  /// \brief Retrieve the nearest enclosing namespace context.
123288b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  DeclContext *getEnclosingNamespaceContext();
123388b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  const DeclContext *getEnclosingNamespaceContext() const {
123488b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor    return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
123588b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor  }
123688b7094185b9d4fe9820c731b6936d8d37f6143eDouglas Gregor
12377a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// \brief Test if this context is part of the enclosing namespace set of
12387a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// the context NS, as defined in C++0x [namespace.def]p9. If either context
12397a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// isn't a namespace, this is equivalent to Equals().
12407a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  ///
12417a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// The enclosing namespace set of a namespace is the namespace and, if it is
12427a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  /// inline, its enclosing namespace, recursively.
12437a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
12447a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
124518483f583333f8d940393e64ce111c6979639665James Dennett  /// \brief Collects all of the declaration contexts that are semantically
1246f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// connected to this declaration context.
1247f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  ///
1248f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// For declaration contexts that have multiple semantically connected but
1249f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// syntactically distinct contexts, such as C++ namespaces, this routine
1250f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// retrieves the complete set of such declaration contexts in source order.
1251f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// For example, given:
1252f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  ///
1253f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// \code
125444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// namespace N {
125544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   int x;
125644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// }
125744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// namespace N {
125844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  ///   int y;
125944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// }
1260f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// \endcode
1261f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  ///
1262f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// The \c Contexts parameter will contain both definitions of N.
1263f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  ///
1264f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// \param Contexts Will be cleared and set to the set of declaration
1265f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// contexts that are semanticaly connected to this declaration context,
1266f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// in source order, including this context (which may be the only result,
1267f5c9f9fd6f5e2850b9b0f19283430245b696c6e5Douglas Gregor  /// for non-namespace contexts).
1268cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts);
126944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
127044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// decl_iterator - Iterates through the declarations stored
127144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// within this context.
12726037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  class decl_iterator {
12736037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    /// Current - The current declaration.
12744afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    Decl *Current;
12756037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
12766037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  public:
1277581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef Decl *value_type;
1278581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef const value_type &reference;
1279581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef const value_type *pointer;
12806037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    typedef std::forward_iterator_tag iterator_category;
12816037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    typedef std::ptrdiff_t            difference_type;
12826037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
12836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    decl_iterator() : Current(nullptr) { }
12844afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor    explicit decl_iterator(Decl *C) : Current(C) { }
12856037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
12866037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    reference operator*() const { return Current; }
1287581deb3da481053c4993c7600f97acf7768caac5David Blaikie    // This doesn't meet the iterator requirements, but it's convenient
1288581deb3da481053c4993c7600f97acf7768caac5David Blaikie    value_type operator->() const { return Current; }
12896037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
129096f4468b6b1bf69bb01c1207f716a3ffaeb4a8d3Chris Lattner    decl_iterator& operator++() {
1291244a67d911d08c3757a18ad666e4a268cf3ee285Chris Lattner      Current = Current->getNextDeclInContext();
129296f4468b6b1bf69bb01c1207f716a3ffaeb4a8d3Chris Lattner      return *this;
129396f4468b6b1bf69bb01c1207f716a3ffaeb4a8d3Chris Lattner    }
12946037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
12956037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    decl_iterator operator++(int) {
12966037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      decl_iterator tmp(*this);
12976037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      ++(*this);
12986037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      return tmp;
12996037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    }
13006037fcba3431b47de1a994c9b286feac17894effDouglas Gregor
13011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    friend bool operator==(decl_iterator x, decl_iterator y) {
13026037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      return x.Current == y.Current;
13036037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    }
13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    friend bool operator!=(decl_iterator x, decl_iterator y) {
13056037fcba3431b47de1a994c9b286feac17894effDouglas Gregor      return x.Current != y.Current;
13066037fcba3431b47de1a994c9b286feac17894effDouglas Gregor    }
13076037fcba3431b47de1a994c9b286feac17894effDouglas Gregor  };
130844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1309651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<decl_iterator> decl_range;
1310651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
131144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// decls_begin/decls_end - Iterate over the declarations stored in
13121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// this context.
1313651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  decl_range decls() const { return decl_range(decls_begin(), decls_end()); }
131417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  decl_iterator decls_begin() const;
1315581deb3da481053c4993c7600f97acf7768caac5David Blaikie  decl_iterator decls_end() const { return decl_iterator(); }
131617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  bool decls_empty() const;
131744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1318681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  /// noload_decls_begin/end - Iterate over the declarations stored in this
1319681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  /// context that are currently loaded; don't attempt to retrieve anything
1320681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  /// from an external source.
1321651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  decl_range noload_decls() const {
1322651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return decl_range(noload_decls_begin(), noload_decls_end());
1323651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
1324651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  decl_iterator noload_decls_begin() const { return decl_iterator(FirstDecl); }
1325581deb3da481053c4993c7600f97acf7768caac5David Blaikie  decl_iterator noload_decls_end() const { return decl_iterator(); }
1326681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl
1327f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  /// specific_decl_iterator - Iterates over a subrange of
1328f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  /// declarations stored in a DeclContext, providing only those that
1329669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// are of type SpecificDecl (or a class derived from it). This
1330f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  /// iterator is used, for example, to provide iteration over just
1331669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
1332f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  template<typename SpecificDecl>
1333f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  class specific_decl_iterator {
1334f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// Current - The current, underlying declaration iterator, which
1335d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// will either be NULL or will point to a declaration of
1336f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// type SpecificDecl.
1337f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    DeclContext::decl_iterator Current;
13381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1339f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// SkipToNextDecl - Advances the current position up to the next
1340f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// declaration of type SpecificDecl that also meets the criteria
1341f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// required by Acceptable.
1342f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    void SkipToNextDecl() {
1343669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      while (*Current && !isa<SpecificDecl>(*Current))
1344f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor        ++Current;
1345f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
1346f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
1347f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  public:
1348581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef SpecificDecl *value_type;
1349581deb3da481053c4993c7600f97acf7768caac5David Blaikie    // TODO: Add reference and pointer typedefs (with some appropriate proxy
1350581deb3da481053c4993c7600f97acf7768caac5David Blaikie    // type) if we ever have a need for them.
1351581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef void reference;
1352581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef void pointer;
1353f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1354f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      difference_type;
1355f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    typedef std::forward_iterator_tag iterator_category;
1356f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
1357669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    specific_decl_iterator() : Current() { }
1358f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
1359f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    /// specific_decl_iterator - Construct a new iterator over a
1360d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// subset of the declarations the range [C,
1361d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// end-of-declarations). If A is non-NULL, it is a pointer to a
1362d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// member function of SpecificDecl that should return true for
1363d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// all of the SpecificDecl instances that will be in the subset
1364d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// of iterators. For example, if you want Objective-C instance
1365d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1366d6f0b4e97e681ea5d165125960f34b3b8c19e1dcDouglas Gregor    /// &ObjCMethodDecl::isInstanceMethod.
1367669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1368f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      SkipToNextDecl();
1369f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
1370f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
1371581deb3da481053c4993c7600f97acf7768caac5David Blaikie    value_type operator*() const { return cast<SpecificDecl>(*Current); }
1372581deb3da481053c4993c7600f97acf7768caac5David Blaikie    // This doesn't meet the iterator requirements, but it's convenient
1373581deb3da481053c4993c7600f97acf7768caac5David Blaikie    value_type operator->() const { return **this; }
1374f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
1375f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    specific_decl_iterator& operator++() {
1376f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      ++Current;
1377f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      SkipToNextDecl();
1378f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return *this;
1379f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
1380f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
1381f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    specific_decl_iterator operator++(int) {
1382f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      specific_decl_iterator tmp(*this);
1383f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      ++(*this);
1384f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return tmp;
1385f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1387ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    friend bool operator==(const specific_decl_iterator& x,
1388ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                           const specific_decl_iterator& y) {
1389f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return x.Current == y.Current;
1390f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
13911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1392ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    friend bool operator!=(const specific_decl_iterator& x,
1393ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                           const specific_decl_iterator& y) {
1394f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor      return x.Current != y.Current;
1395f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor    }
1396f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  };
1397f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor
1398669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// \brief Iterates over a filtered subrange of declarations stored
1399669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// in a DeclContext.
1400669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  ///
1401669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// This iterator visits only those declarations that are of type
1402669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// SpecificDecl (or a class derived from it) and that meet some
1403669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// additional run-time criteria. This iterator is used, for
1404669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// example, to provide access to the instance methods within an
1405669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1406669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1407669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1408669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  class filtered_decl_iterator {
1409669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// Current - The current, underlying declaration iterator, which
1410669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// will either be NULL or will point to a declaration of
1411669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// type SpecificDecl.
1412669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    DeclContext::decl_iterator Current;
14131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1414669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// SkipToNextDecl - Advances the current position up to the next
1415669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// declaration of type SpecificDecl that also meets the criteria
1416669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// required by Acceptable.
1417669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    void SkipToNextDecl() {
1418669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      while (*Current &&
1419669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor             (!isa<SpecificDecl>(*Current) ||
1420669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor              (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1421669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor        ++Current;
1422669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
1423669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
1424669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  public:
1425581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef SpecificDecl *value_type;
1426581deb3da481053c4993c7600f97acf7768caac5David Blaikie    // TODO: Add reference and pointer typedefs (with some appropriate proxy
1427581deb3da481053c4993c7600f97acf7768caac5David Blaikie    // type) if we ever have a need for them.
1428581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef void reference;
1429581deb3da481053c4993c7600f97acf7768caac5David Blaikie    typedef void pointer;
1430669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1431669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      difference_type;
1432669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    typedef std::forward_iterator_tag iterator_category;
1433669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
1434669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    filtered_decl_iterator() : Current() { }
1435669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
1436bd4fa45e7cf6a2f0adcb0ab66d5b3d338ec56c3cDavid Blaikie    /// filtered_decl_iterator - Construct a new iterator over a
1437669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// subset of the declarations the range [C,
1438669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// end-of-declarations). If A is non-NULL, it is a pointer to a
1439669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// member function of SpecificDecl that should return true for
1440669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// all of the SpecificDecl instances that will be in the subset
1441669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// of iterators. For example, if you want Objective-C instance
1442669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1443669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    /// &ObjCMethodDecl::isInstanceMethod.
1444669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1445669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      SkipToNextDecl();
1446669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
1447669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
1448581deb3da481053c4993c7600f97acf7768caac5David Blaikie    value_type operator*() const { return cast<SpecificDecl>(*Current); }
1449581deb3da481053c4993c7600f97acf7768caac5David Blaikie    value_type operator->() const { return cast<SpecificDecl>(*Current); }
1450669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
1451669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    filtered_decl_iterator& operator++() {
1452669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      ++Current;
1453669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      SkipToNextDecl();
1454669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return *this;
1455669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
1456669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
1457669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    filtered_decl_iterator operator++(int) {
1458669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      filtered_decl_iterator tmp(*this);
1459669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      ++(*this);
1460669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return tmp;
1461669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
14621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1463ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    friend bool operator==(const filtered_decl_iterator& x,
1464ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                           const filtered_decl_iterator& y) {
1465669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return x.Current == y.Current;
1466669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
14671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1468ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    friend bool operator!=(const filtered_decl_iterator& x,
1469ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                           const filtered_decl_iterator& y) {
1470669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor      return x.Current != y.Current;
1471669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor    }
1472669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor  };
1473669c9a28fa4be35e6b6322aa7f2f3b2968189b80Douglas Gregor
147440f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// @brief Add the declaration D into this context.
147540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
147640f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// This routine should be invoked when the declaration D has first
147740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// been declared, to place D into the context where it was
147840f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// (lexically) defined. Every declaration must be added to one
147940f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// (and only one!) context, where it can be visited via
148040f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// [decls_begin(), decls_end()). Once a declaration has been added
148140f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// to its lexical context, the corresponding DeclContext owns the
148240f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// declaration.
148340f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
148440f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// If D is also a NamedDecl, it will be made visible within its
148540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// semantic context via makeDeclVisibleInContext.
148617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  void addDecl(Decl *D);
1487381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan
1488381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// @brief Add the declaration D into this context, but suppress
1489381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// searches for external declarations with the same name.
1490381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  ///
1491381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// Although analogous in function to addDecl, this removes an
1492381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// important check.  This is only useful if the Decl is being
1493381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// added in response to an external search; in all other cases,
1494381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// addDecl() is the right function to use.
1495381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// See the ASTImporter for use cases.
14969faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  void addDeclInternal(Decl *D);
149744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
14983f9a8a60614b763785d54ad08821745d03a4af70John McCall  /// @brief Add the declaration D to this context without modifying
14993f9a8a60614b763785d54ad08821745d03a4af70John McCall  /// any lookup tables.
15003f9a8a60614b763785d54ad08821745d03a4af70John McCall  ///
15013f9a8a60614b763785d54ad08821745d03a4af70John McCall  /// This is useful for some operations in dependent contexts where
15023f9a8a60614b763785d54ad08821745d03a4af70John McCall  /// the semantic context might not be dependent;  this basically
15033f9a8a60614b763785d54ad08821745d03a4af70John McCall  /// only happens with friends.
15043f9a8a60614b763785d54ad08821745d03a4af70John McCall  void addHiddenDecl(Decl *D);
15053f9a8a60614b763785d54ad08821745d03a4af70John McCall
15069f54ad4381370c6b771424b53d219e661d6d6706John McCall  /// @brief Removes a declaration from this context.
15079f54ad4381370c6b771424b53d219e661d6d6706John McCall  void removeDecl(Decl *D);
1508cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan
1509cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan  /// @brief Checks whether a declaration is in this context.
1510cd904e8864637e427f5ea3bf35a26e79b3dbbadfSean Callanan  bool containsDecl(Decl *D) const;
15119f54ad4381370c6b771424b53d219e661d6d6706John McCall
151244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// lookup_iterator - An iterator that provides access to the results
151344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// of looking up a name within this context.
15144afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  typedef NamedDecl **lookup_iterator;
151544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
151644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// lookup_const_iterator - An iterator that provides non-mutable
151744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// access to the results of lookup up a name within this context.
15184afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor  typedef NamedDecl * const * lookup_const_iterator;
151944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1520ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall  typedef DeclContextLookupResult lookup_result;
1521ea318642072d3d94b5c3cff0fa6f4b33d2db0768John McCall  typedef DeclContextLookupConstResult lookup_const_result;
152244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
152344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// lookup - Find the declarations (if any) with the given Name in
152444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  /// this context. Returns a range of iterators that contains all of
152540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// the declarations with this name, with object, function, member,
152640f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// and enumerator names preceding any tag name. Note that this
152740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// routine will not look into parent contexts.
152817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  lookup_result lookup(DeclarationName Name);
1529514d3b6b93c83c0841d2f9dd7af8ecc2877fe921Douglas Gregor  lookup_const_result lookup(DeclarationName Name) const {
1530514d3b6b93c83c0841d2f9dd7af8ecc2877fe921Douglas Gregor    return const_cast<DeclContext*>(this)->lookup(Name);
1531514d3b6b93c83c0841d2f9dd7af8ecc2877fe921Douglas Gregor  }
153244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1533e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  /// \brief Find the declarations with the given name that are visible
1534e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  /// within this context; don't attempt to retrieve anything from an
1535e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  /// external source.
1536e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  lookup_result noload_lookup(DeclarationName Name);
1537e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith
1538b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  /// \brief A simplistic name lookup mechanism that performs name lookup
1539b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  /// into this declaration context without consulting the external source.
1540b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  ///
1541ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// This function should almost never be used, because it subverts the
1542b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  /// usual relationship between a DeclContext and the external source.
1543b75a3451bcae1301875282e73a13934c90b6574cDouglas Gregor  /// See the ASTImporter for the (few, but important) use cases.
1544e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  ///
1545e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  /// FIXME: This is very inefficient; replace uses of it with uses of
1546e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  /// noload_lookup.
1547ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  void localUncachedLookup(DeclarationName Name,
1548cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                           SmallVectorImpl<NamedDecl *> &Results);
1549ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
155040f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// @brief Makes a declaration visible within this context.
155140f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
155240f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// This routine makes the declaration D visible to name lookup
155340f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// within this context and, if this is a transparent context,
155440f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// within its parent contexts up to the first enclosing
155540f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// non-transparent context. Making a declaration visible within a
155640f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// context does not transfer ownership of a declaration, and a
155740f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// declaration can be visible in many contexts that aren't its
155840f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// lexical context.
155940f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  ///
156040f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// If D is a redeclaration of an existing declaration that is
156140f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// visible from this context, as determined by
156240f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// NamedDecl::declarationReplaces, the previous declaration will be
156340f4e69002af9623a1f959bd57b99afda186a6a7Douglas Gregor  /// replaced with D.
15641b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  void makeDeclVisibleInContext(NamedDecl *D);
15657643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
1566173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  /// all_lookups_iterator - An iterator that provides a view over the results
1567173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  /// of looking up every possible name.
1568173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  class all_lookups_iterator;
1569173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
1570651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<all_lookups_iterator> lookups_range;
1571651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1572651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  lookups_range lookups() const;
1573651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  lookups_range noload_lookups() const;
1574651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
15756e322c08b187ba93019732d2295c84c339813e0dRichard Smith  /// \brief Iterators over all possible lookups within this context.
1576173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  all_lookups_iterator lookups_begin() const;
1577173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  all_lookups_iterator lookups_end() const;
1578173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
15796e322c08b187ba93019732d2295c84c339813e0dRichard Smith  /// \brief Iterators over all possible lookups within this context that are
15806e322c08b187ba93019732d2295c84c339813e0dRichard Smith  /// currently loaded; don't attempt to retrieve anything from an external
15816e322c08b187ba93019732d2295c84c339813e0dRichard Smith  /// source.
15826e322c08b187ba93019732d2295c84c339813e0dRichard Smith  all_lookups_iterator noload_lookups_begin() const;
15836e322c08b187ba93019732d2295c84c339813e0dRichard Smith  all_lookups_iterator noload_lookups_end() const;
15846e322c08b187ba93019732d2295c84c339813e0dRichard Smith
1585651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<UsingDirectiveDecl * const *> udir_range;
15862a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
1587651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  udir_range using_directives() const;
15882a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor
15890c01d18094100db92d38daa923c95661512db203John McCall  // These are all defined in DependentDiagnostic.h.
15900c01d18094100db92d38daa923c95661512db203John McCall  class ddiag_iterator;
1591651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef llvm::iterator_range<DeclContext::ddiag_iterator> ddiag_range;
1592651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1593651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  inline ddiag_range ddiags() const;
15940c01d18094100db92d38daa923c95661512db203John McCall
1595c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor  // Low-level accessors
15965b8d0af4234252b38229a5bae1d615ac9769f73fRichard Smith
1597d5a20c19416d7143771b4d503fa808ae01c4e7adSean Callanan  /// \brief Mark the lookup table as needing to be built.  This should be
159863a9514a2330b144575a136e38875193760126f7Richard Smith  /// used only if setHasExternalLexicalStorage() has been called on any
159963a9514a2330b144575a136e38875193760126f7Richard Smith  /// decl context for which this is the primary context.
1600d5a20c19416d7143771b4d503fa808ae01c4e7adSean Callanan  void setMustBuildLookupTable() {
1601d5a20c19416d7143771b4d503fa808ae01c4e7adSean Callanan    LookupPtr.setInt(true);
1602d5a20c19416d7143771b4d503fa808ae01c4e7adSean Callanan  }
1603c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
1604c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor  /// \brief Retrieve the internal representation of the lookup structure.
1605c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  /// This may omit some names if we are lazily building the structure.
1606c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap *getLookupPtr() const { return LookupPtr.getPointer(); }
1607c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith
1608c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  /// \brief Ensure the lookup structure is fully-built and return it.
1609c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  StoredDeclsMap *buildLookup();
1610c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
16112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief Whether this DeclContext has external storage containing
16122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// additional declarations that are lexically in this context.
16132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
16142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
16152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief State whether this DeclContext has external storage for
16162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// declarations lexically in this context.
16171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void setHasExternalLexicalStorage(bool ES = true) {
16181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ExternalLexicalStorage = ES;
16192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
16202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
16212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief Whether this DeclContext has external storage containing
16222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// additional declarations that are visible in this context.
16232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
16242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
16252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// \brief State whether this DeclContext has external storage for
16262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  /// declarations visible in this context.
16271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void setHasExternalVisibleStorage(bool ES = true) {
16281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ExternalVisibleStorage = ES;
16298896339a3c654dc44877d52a926eb89ab3ef30b5Richard Smith    if (ES && LookupPtr.getPointer())
1630bbcd0f3ba215d5a8857b224e32b0330586a00dc6Richard Smith      NeedToReconcileExternalVisibleStorage = true;
16312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
16322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
16332ea054fbc59ed19b8e3304e7e7cbdd56a5a5120fDouglas Gregor  /// \brief Determine whether the given declaration is stored in the list of
16342ea054fbc59ed19b8e3304e7e7cbdd56a5a5120fDouglas Gregor  /// declarations lexically within this context.
16352ea054fbc59ed19b8e3304e7e7cbdd56a5a5120fDouglas Gregor  bool isDeclInLexicalTraversal(const Decl *D) const {
163646cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor    return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
163746cd2186bddc3e046140cb2d56932ee7faf7e3aeDouglas Gregor                 D == LastDecl);
16382ea054fbc59ed19b8e3304e7e7cbdd56a5a5120fDouglas Gregor  }
1639ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
16403d7641e090cf113dec64306a1597d3e4523e2a55Argyrios Kyrtzidis  static bool classof(const Decl *D);
1641b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static bool classof(const DeclContext *D) { return true; }
16427643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidis
1643651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpDeclContext() const;
1644651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpLookups() const;
1645651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpLookups(llvm::raw_ostream &OS) const;
16462b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson
16477643536c36b0449256d5ee2efc03a7e4a784a0b3Argyrios Kyrtzidisprivate:
1648651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void reconcileExternalVisibleStorage() const;
164917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  void LoadLexicalDeclsFromExternalStorage() const;
1650ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1651ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// @brief Makes a declaration visible within this context, but
1652381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// suppresses searches for external declarations with the same
1653381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// name.
1654381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  ///
1655381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// Analogous to makeDeclVisibleInContext, but for the exclusive
1656381509ea9965ab6bde9b36b4131ad08bf851d9b4Sean Callanan  /// use of addDeclInternal().
16571b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith  void makeDeclVisibleInContextInternal(NamedDecl *D);
16582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
16590c01d18094100db92d38daa923c95661512db203John McCall  friend class DependentDiagnostic;
16600c01d18094100db92d38daa923c95661512db203John McCall  StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
16610c01d18094100db92d38daa923c95661512db203John McCall
1662e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith  template<decl_iterator (DeclContext::*Begin)() const,
1663e7bae1597f4a7088f5048695c14a8f1013a86108Richard Smith           decl_iterator (DeclContext::*End)() const>
1664c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  void buildLookupImpl(DeclContext *DCtx);
1665c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith  void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1666c5d3e80c64af9604ad798282cc6861f9cd2afc52Richard Smith                                         bool Rediscoverable);
16679faf810f08132aabb34a478297dfeea89c3bbe17Sean Callanan  void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
16680ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
16690ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1670f57172b24f08a68d179675989813d5479dc87829Douglas Gregorinline bool Decl::isTemplateParameter() const {
167179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
167279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor         getKind() == TemplateTemplateParm;
16734afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor}
16744afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
167580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall// Specialization selected when ToTy is not a known subclass of DeclContext.
167680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCalltemplate <class ToTy,
1677651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value>
167880cd64a8450d8e2c079dc134d9711cd45ba89d63John McCallstruct cast_convert_decl_context {
167980cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static const ToTy *doit(const DeclContext *Val) {
168080cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
168180cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  }
168280cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall
168380cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static ToTy *doit(DeclContext *Val) {
168480cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
168580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  }
168680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall};
168780cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall
168880cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall// Specialization selected when ToTy is a known subclass of DeclContext.
168980cd64a8450d8e2c079dc134d9711cd45ba89d63John McCalltemplate <class ToTy>
169080cd64a8450d8e2c079dc134d9711cd45ba89d63John McCallstruct cast_convert_decl_context<ToTy, true> {
169180cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static const ToTy *doit(const DeclContext *Val) {
169280cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return static_cast<const ToTy*>(Val);
169380cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  }
169480cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall
169580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static ToTy *doit(DeclContext *Val) {
169680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return static_cast<ToTy*>(Val);
169780cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  }
169880cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall};
169980cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall
170080cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall
17010ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner} // end clang.
17020ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
17030ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnernamespace llvm {
17040ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
170580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall/// isa<T>(DeclContext*)
17064d509341bd5db06a517daa311379f52bb540bc34Eli Friedmantemplate <typename To>
17074d509341bd5db06a517daa311379f52bb540bc34Eli Friedmanstruct isa_impl<To, ::clang::DeclContext> {
170842220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis  static bool doit(const ::clang::DeclContext &Val) {
17094d509341bd5db06a517daa311379f52bb540bc34Eli Friedman    return To::classofKind(Val.getDeclKind());
17100ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
17110ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
17120ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
171380cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall/// cast<T>(DeclContext*)
171480cd64a8450d8e2c079dc134d9711cd45ba89d63John McCalltemplate<class ToTy>
171580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCallstruct cast_convert_val<ToTy,
171680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall                        const ::clang::DeclContext,const ::clang::DeclContext> {
171780cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static const ToTy &doit(const ::clang::DeclContext &Val) {
171880cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
171980cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  }
172080cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall};
172180cd64a8450d8e2c079dc134d9711cd45ba89d63John McCalltemplate<class ToTy>
172280cd64a8450d8e2c079dc134d9711cd45ba89d63John McCallstruct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
172380cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static ToTy &doit(::clang::DeclContext &Val) {
172480cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
172580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  }
172680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall};
172780cd64a8450d8e2c079dc134d9711cd45ba89d63John McCalltemplate<class ToTy>
172880cd64a8450d8e2c079dc134d9711cd45ba89d63John McCallstruct cast_convert_val<ToTy,
172980cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall                     const ::clang::DeclContext*, const ::clang::DeclContext*> {
173080cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static const ToTy *doit(const ::clang::DeclContext *Val) {
173180cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
173280cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  }
173380cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall};
173480cd64a8450d8e2c079dc134d9711cd45ba89d63John McCalltemplate<class ToTy>
173580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCallstruct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
173680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static ToTy *doit(::clang::DeclContext *Val) {
173780cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
173880cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  }
173980cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall};
174080cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall
174142220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis/// Implement cast_convert_val for Decl -> DeclContext conversions.
17420ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnertemplate<class FromTy>
174342220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
1744b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static ::clang::DeclContext &doit(const FromTy &Val) {
174542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return *FromTy::castToDeclContext(&Val);
17460ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
17470ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
17480ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
17490ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattnertemplate<class FromTy>
175042220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidisstruct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
1751b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner  static ::clang::DeclContext *doit(const FromTy *Val) {
175242220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis    return FromTy::castToDeclContext(Val);
17530ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner  }
17540ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner};
175542220c5432c141d47cc8ce786e472b49dc907378Argyrios Kyrtzidis
175644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregortemplate<class FromTy>
175744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorstruct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
175844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  static const ::clang::DeclContext &doit(const FromTy &Val) {
175944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return *FromTy::castToDeclContext(&Val);
176044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
176144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor};
176244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
176344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregortemplate<class FromTy>
176444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorstruct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
176544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  static const ::clang::DeclContext *doit(const FromTy *Val) {
176644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    return FromTy::castToDeclContext(Val);
176744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  }
176844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor};
176944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
17700ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner} // end namespace llvm
17710ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
17720ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner#endif
1773